I tried enabling -shared in the ELF toolchain and so far everything looks good. We get /lib/ld.so.1 as the canonical dynamic linker address, which is good, because it is the standard cross-platform ELF interpreter. When using the modified binutils, everything appears to work correctly. I see no difference between shared on the two ELF linkers.
which is kind or orthogonal to -shared with -nostartfiles -nostdlib -nostdinc -fPIC
It seems sad to disable relocatable objects in the ELF toolchain just because Newlib build script doesn't make shared objects. As far as I can tell the objects are exactly the same. This is unrelated to calling conventions or any
I have a test repo, but this disablement means I have to use the Linux toolchain when all I want is a baremetal toolchain that allows -shared in its binutils. It seems odd to disable a feature that is well defined. It's not like we are going to change the Embedded ELF tools to use something other than RELA for PT_DYNAMIC relocations is it? The GOT and PLT (and no PLT when using -static PIE) seems pretty well defined as far as I can tell. Of course FD-PIC would be a new ABI and this presumably would be added to both tuples so we could enable no-mmu Linux.
Anyway from my side, it appears to work when I remove the disablement. Of course I could be wrong and we might have plans to design an incompatible shared library ABI for the embedded binutils? Yes?
Michael.
Here is the reloc code with the REL loop removed as I presume RISC-V only emits RELA?:
diff --git a/ld/emulparams/elf32lriscv-defs.sh b/ld/emulparams/elf32lriscv-defs.sh
@@ -16,15 +16,8 @@ if test `echo "$host" | sed -e s/64//` = `echo "$target" | sed -e s/64//`; then
-# Enable shared library support for everything except an embedded elf target.
mjc@miqi:~/src/sifive/riscv-disassembler$ riscv64-unknown-elf-readelf -a test.so
ELF Header:
Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
Class: ELF64
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: DYN (Shared object file)
Machine: RISC-V
Version: 0x1
Entry point address: 0x1d0
Start of program headers: 64 (bytes into file)
Start of section headers: 1592 (bytes into file)
Flags: 0x5, RVC, double-float ABI
Size of this header: 64 (bytes)
Size of program headers: 56 (bytes)
Number of program headers: 5
Size of section headers: 64 (bytes)
Number of section headers: 15
Section header string table index: 14
Section Headers:
[Nr] Name Type Address Offset
Size EntSize Flags Link Info Align
[ 0] NULL 0000000000000000 00000000
0000000000000000 0000000000000000 0 0 0
[ 1] .interp PROGBITS 0000000000000158 00000158
000000000000000d 0000000000000000 A 0 0 1
[ 2] .hash HASH 0000000000000168 00000168
0000000000000014 0000000000000004 A 3 0 8
[ 3] .dynsym DYNSYM 0000000000000180 00000180
0000000000000030 0000000000000018 A 4 2 8
[ 4] .dynstr STRTAB 00000000000001b0 000001b0
0000000000000001 0000000000000000 A 0 0 1
[ 5] .rela.dyn RELA 00000000000001b8 000001b8
0000000000000018 0000000000000018 A 3 0 8
[ 6] .text PROGBITS 00000000000001d0 000001d0
0000000000000008 0000000000000000 AX 0 0 2
[ 7] .dynamic DYNAMIC 00000000000011d8 000001d8
0000000000000100 0000000000000010 WA 4 0 8
[ 8] .data PROGBITS 00000000000012d8 000002d8
0000000000000008 0000000000000000 WA 0 0 8
[ 9] .got PROGBITS 00000000000012e0 000002e0
0000000000000008 0000000000000008 WA 0 0 8
[10] .bss NOBITS 00000000000012e8 000002e8
0000000000000008 0000000000000000 WA 0 0 4
[11] .comment PROGBITS 0000000000000000 000002e8
0000000000000011 0000000000000001 MS 0 0 1
[12] .symtab SYMTAB 0000000000000000 00000300
0000000000000258 0000000000000018 13 17 8
[13] .strtab STRTAB 0000000000000000 00000558
0000000000000073 0000000000000000 0 0 1
[14] .shstrtab STRTAB 0000000000000000 000005cb
000000000000006b 0000000000000000 0 0 1
Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
L (link order), O (extra OS processing required), G (group), T (TLS),
C (compressed), x (unknown), o (OS specific), E (exclude),
p (processor specific)
There are no section groups in this file.
Program Headers:
Type Offset VirtAddr PhysAddr
FileSiz MemSiz Flags Align
PHDR 0x0000000000000040 0x0000000000000040 0x0000000000000040
0x0000000000000118 0x0000000000000118 R 0x8
INTERP 0x0000000000000158 0x0000000000000158 0x0000000000000158
0x000000000000000d 0x000000000000000d R 0x1
[Requesting program interpreter: /lib/ld.so.1]
LOAD 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x00000000000001d8 0x00000000000001d8 R E 0x1000
LOAD 0x00000000000001d8 0x00000000000011d8 0x00000000000011d8
0x0000000000000110 0x0000000000000118 RW 0x1000
DYNAMIC 0x00000000000001d8 0x00000000000011d8 0x00000000000011d8
0x0000000000000100 0x0000000000000100 RW 0x8
Section to Segment mapping:
Segment Sections...
00
01 .interp
02 .interp .hash .dynsym .dynstr .rela.dyn .text
03 .dynamic .data .got .bss
04 .dynamic
Dynamic section at offset 0x1d8 contains 12 entries:
Tag Type Name/Value
0x0000000000000004 (HASH) 0x168
0x0000000000000005 (STRTAB) 0x1b0
0x0000000000000006 (SYMTAB) 0x180
0x000000000000000a (STRSZ) 1 (bytes)
0x000000000000000b (SYMENT) 24 (bytes)
0x0000000000000015 (DEBUG) 0x0
0x0000000000000007 (RELA) 0x1b8
0x0000000000000008 (RELASZ) 24 (bytes)
0x0000000000000009 (RELAENT) 24 (bytes)
0x000000006ffffffb (FLAGS_1) Flags: PIE
0x000000006ffffff9 (RELACOUNT) 1
0x0000000000000000 (NULL) 0x0
Relocation section '.rela.dyn' at offset 0x1b8 contains 1 entry:
Offset Info Type Sym. Value Sym. Name + Addend
0000000012d8 000000000003 R_RISCV_RELATIVE 12e8
The decoding of unwind sections for machine type RISC-V is not currently supported.
Symbol table '.dynsym' contains 2 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 00000000000001d0 0 SECTION LOCAL DEFAULT 6
Symbol table '.symtab' contains 25 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000000158 0 SECTION LOCAL DEFAULT 1
2: 0000000000000168 0 SECTION LOCAL DEFAULT 2
3: 0000000000000180 0 SECTION LOCAL DEFAULT 3
4: 00000000000001b0 0 SECTION LOCAL DEFAULT 4
5: 00000000000001b8 0 SECTION LOCAL DEFAULT 5
6: 00000000000001d0 0 SECTION LOCAL DEFAULT 6
7: 00000000000011d8 0 SECTION LOCAL DEFAULT 7
8: 00000000000012d8 0 SECTION LOCAL DEFAULT 8
9: 00000000000012e0 0 SECTION LOCAL DEFAULT 9
10: 00000000000012e8 0 SECTION LOCAL DEFAULT 10
11: 0000000000000000 0 SECTION LOCAL DEFAULT 11
12: 0000000000000000 0 FILE LOCAL DEFAULT ABS test.c
13: 0000000000000000 0 FILE LOCAL DEFAULT ABS
14: 00000000000011d8 0 OBJECT LOCAL DEFAULT ABS _DYNAMIC
15: 00000000000001d0 0 OBJECT LOCAL DEFAULT ABS _PROCEDURE_LINKAGE_TABLE_
16: 00000000000012e0 0 OBJECT LOCAL DEFAULT ABS _GLOBAL_OFFSET_TABLE_
17: 0000000000001ae8 0 NOTYPE GLOBAL DEFAULT 9 __global_pointer$
18: 00000000000012d8 8 OBJECT GLOBAL DEFAULT 8 b
19: 00000000000001d2 6 FUNC GLOBAL DEFAULT 6 hello
20: 00000000000001d0 2 FUNC GLOBAL DEFAULT 6 _start
21: 00000000000012e8 0 NOTYPE GLOBAL DEFAULT 10 __bss_start
22: 00000000000012e8 0 NOTYPE GLOBAL DEFAULT 9 _edata
23: 00000000000012f0 0 NOTYPE GLOBAL DEFAULT 10 _end
24: 00000000000012e8 4 OBJECT GLOBAL DEFAULT 10 a
Histogram for bucket list length (total of 1 bucket):
Length Number % of total Coverage
0 1 (100.0%)
No version information found in this file.
Here is the disassembly of the bare metal relocation code. Nice! Its only 180 bytes using -Os:
$ riscv64-unknown-elf-objdump -d a.o
a.o: file format elf64-littleriscv
Disassembly of section .text:
0000000000000000 <__static_pie_reloc>:
0: 714d addi sp,sp,-336
2: e2a2 sd s0,320(sp)
4: 08000613 li a2,128
8: 4581 li a1,0
a: 842a mv s0,a0
c: 850a mv a0,sp
e: e686 sd ra,328(sp)
10: 00000097 auipc ra,0x0
14: 000080e7 jalr ra # 10 <__static_pie_reloc+0x10>
18: 0c000613 li a2,192
1c: 4581 li a1,0
1e: 0108 addi a0,sp,128
20: 00000097 auipc ra,0x0
24: 000080e7 jalr ra # 20 <__static_pie_reloc+0x20>
0000000000000028 <.L2>:
28: 601c ld a5,0(s0)
2a: 0421 addi s0,s0,8
2c: fff5 bnez a5,28 <.L2>
000000000000002e <.L3>:
2e: 601c ld a5,0(s0)
30: 0421 addi s0,s0,8
32: fff5 bnez a5,2e <.L3>
34: 473d li a4,15
0000000000000036 <.L4>:
36: 601c ld a5,0(s0)
38: e3a9 bnez a5,7a <.L6>
3a: 7762 ld a4,56(sp)
3c: 67e2 ld a5,24(sp)
3e: e701 bnez a4,46 <.L7>
40: 777d lui a4,0xfffff
42: 8f7d and a4,a4,a5
44: fc3a sd a4,56(sp)
0000000000000046 <.L7>:
46: 7682 ld a3,32(sp)
48: 4609 li a2,2
000000000000004a <.L8>:
4a: 6398 ld a4,0(a5)
4c: c719 beqz a4,5a <.L12>
4e: 4398 lw a4,0(a5)
50: 02c71f63 bne a4,a2,8e <.L9>
54: 6b98 ld a4,16(a5)
56: 77e2 ld a5,56(sp)
58: 97ba add a5,a5,a4
000000000000005a <.L12>:
5a: 46dd li a3,23
000000000000005c <.L10>:
5c: 6398 ld a4,0(a5)
5e: eb15 bnez a4,92 <.L14>
60: 77ea ld a5,184(sp)
62: cb81 beqz a5,72 <.L1>
64: 76e2 ld a3,56(sp)
66: 670e ld a4,192(sp)
68: 652e ld a0,200(sp)
6a: 97b6 add a5,a5,a3
6c: 973e add a4,a4,a5
000000000000006e <.L16>:
6e: 02e7ec63 bltu a5,a4,a6 <.L17>
0000000000000072 <.L1>:
72: 60b6 ld ra,328(sp)
74: 6416 ld s0,320(sp)
76: 6171 addi sp,sp,336
78: 8082 ret
000000000000007a <.L6>:
7a: 00f76863 bltu a4,a5,8a <.L5>
7e: 0294 addi a3,sp,320
80: 078e slli a5,a5,0x3
82: 97b6 add a5,a5,a3
84: 6414 ld a3,8(s0)
86: ecd7b023 sd a3,-320(a5)
000000000000008a <.L5>:
8a: 0441 addi s0,s0,16
8c: b76d j 36 <.L4>
000000000000008e <.L9>:
8e: 97b6 add a5,a5,a3
90: bf6d j 4a <.L8>
0000000000000092 <.L14>:
92: 00e6e863 bltu a3,a4,a2 <.L13>
96: 0290 addi a2,sp,320
98: 070e slli a4,a4,0x3
9a: 9732 add a4,a4,a2
9c: 6790 ld a2,8(a5)
9e: f4c73023 sd a2,-192(a4) # ffffffffffffef40 <.L17+0xffffffffffffee9a>
00000000000000a2 <.L13>:
a2: 07c1 addi a5,a5,16
a4: bf65 j 5c <.L10>
00000000000000a6 <.L17>:
a6: 6390 ld a2,0(a5)
a8: 6b8c ld a1,16(a5)
aa: 97aa add a5,a5,a0
ac: 9636 add a2,a2,a3
ae: 95b6 add a1,a1,a3
b0: e20c sd a1,0(a2)
b2: bf75 j 6e <.L16>