Hi,
Now, I'm trying to implement the relocating codes to the Nios's dynamic linker. At first, I must make the 'static' linker ' nios2-wrs-linux-gnu-ld' or 'ld' put relocating information. So I added some codes for two functions 'nios2_elf32_relocate_section' and 'nios2_elf32_check_relocs' in the file '/nios2gcc4/src/binutils-2.17.50/bfd/elf32-nios2.c'. About the details, please refer the attached file.
With these 'static' linkers, we can compile the 'Samples', main.c, a.c and b.c as follows
nios2-wrs-linux-gnu-gcc -g -shared -Wl,-Bsymbolic -G0 a.c -o a.so
nios2-wrs-linux-gnu-gcc -g -shared -Wl,-Bsymbolic -G0 b.c -o b.so
nios2-wrs-linux-gnu-gcc -g main.c a.so b.so -o main
The relocation information of shared libraries are as follows.
In 'a.readelf'
Relocation section '.rela.dyn' at offset 0x308 contains 18 entries:
Offset Info Type Sym.Value Sym. Name + Addend
00001834 00000027 R_NIOS2_RELATIVE 00001748
00001838 00000027 R_NIOS2_RELATIVE 00001864
0000183c 00000027 R_NIOS2_RELATIVE 00001860
00001840 00000027 R_NIOS2_RELATIVE 000004b0
00001844 00000027 R_NIOS2_RELATIVE 0000056c
00001848 00000027 R_NIOS2_RELATIVE 0000173c
0000184c 00000027 R_NIOS2_RELATIVE 0000062c
00001854 00000027 R_NIOS2_RELATIVE 0000185c
0000185c 00000027 R_NIOS2_RELATIVE 0000185c
00001860 00000027 R_NIOS2_RELATIVE 00001744
000005f4 00001104 R_NIOS2_CALL26 00000000 func_b + 0
00000614 00001104 R_NIOS2_CALL26 00000000 func_b + 0
000005fc 0000100b R_NIOS2_HIADJ16 00000000 j + 0
00000600 0000100a R_NIOS2_LO16 00000000 j + 0
00000608 0000100b R_NIOS2_HIADJ16 00000000 j + 0
0000060c 0000100a R_NIOS2_LO16 00000000 j + 0
00001850 00000b25 R_NIOS2_GLOB_DAT 00000000 _Jv_RegisterClasses + 0
00001858 00000f25 R_NIOS2_GLOB_DAT 00000000 __cxa_finalize + 0
Relocation section '.rela.plt' at offset 0x3e0 contains 2 entries:
Offset Info Type Sym.Value Sym. Name + Addend
0000182c 00000f26 R_NIOS2_JUMP_SLOT 00000000 __cxa_finalize + 0
00001830 00001126 R_NIOS2_JUMP_SLOT 00000000 func_b + 0
There are no unwind sections in this file.
Symbol table '.dynsym' contains 21 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 00000000 0 NOTYPE LOCAL DEFAULT UND
1: 000003f8 0 SECTION LOCAL DEFAULT 8
2: 000004b0 0 SECTION LOCAL DEFAULT 10
3: 000006bc 0 SECTION LOCAL DEFAULT 11
4: 00000734 0 SECTION LOCAL DEFAULT 12
5: 00001738 0 SECTION LOCAL DEFAULT 13
6: 00001740 0 SECTION LOCAL DEFAULT 14
7: 00001748 0 SECTION LOCAL DEFAULT 15
8: 0000185c 0 SECTION LOCAL DEFAULT 18
9: 00001864 0 SECTION LOCAL DEFAULT 19
10: 000005dc 80 FUNC GLOBAL DEFAULT 10 func_a
11: 00000000 0 NOTYPE WEAK DEFAULT UND _Jv_RegisterClasses
12: 000006bc 0 NOTYPE GLOBAL DEFAULT 11 _fini
13: 00009810 0 NOTYPE GLOBAL DEFAULT ABS _gp
14: 00001864 0 NOTYPE GLOBAL DEFAULT ABS __bss_start
15: 00000000 356 FUNC WEAK DEFAULT UND __cxa_finalize@GLIBC_2.10 (2)
16: 00000000 0 NOTYPE GLOBAL DEFAULT UND j
17: 00000000 0 NOTYPE GLOBAL DEFAULT UND func_b
18: 00001868 0 NOTYPE GLOBAL DEFAULT ABS _end
19: 00001864 0 NOTYPE GLOBAL DEFAULT ABS _edata
20: 000003f8 0 NOTYPE GLOBAL DEFAULT 8 _init
.
In 'b.readelf'
Relocation section '.rela.dyn' at offset 0x300 contains 17 entries:
Offset Info Type Sym.Value Sym. Name + Addend
00001850 00000027 R_NIOS2_RELATIVE 0000175c
00001854 00000027 R_NIOS2_RELATIVE 00001880
00001858 00000027 R_NIOS2_RELATIVE 0000187c
0000185c 00000027 R_NIOS2_RELATIVE 00000484
00001860 00000027 R_NIOS2_RELATIVE 00000540
00001864 00000027 R_NIOS2_RELATIVE 00001750
00001868 00000027 R_NIOS2_RELATIVE 00000640
00001870 00000027 R_NIOS2_RELATIVE 00001878
00001878 00000027 R_NIOS2_RELATIVE 00001878
0000187c 00000027 R_NIOS2_RELATIVE 00001758
000005f8 00000004 R_NIOS2_CALL26 000005b0
00000600 0000000b R_NIOS2_HIADJ16 00001830
00000604 0000000a R_NIOS2_LO16 00001830
00000618 0000000b R_NIOS2_HIADJ16 00001830
0000061c 0000000a R_NIOS2_LO16 00001830
0000186c 00000b25 R_NIOS2_GLOB_DAT 00000000 _Jv_RegisterClasses + 0
00001874 00000f25 R_NIOS2_GLOB_DAT 00000000 __cxa_finalize + 0
Relocation section '.rela.plt' at offset 0x3cc contains 1 entries:
Offset Info Type Sym.Value Sym. Name + Addend
0000184c 00000f26 R_NIOS2_JUMP_SLOT 00000000 __cxa_finalize + 0
There are no unwind sections in this file.
Symbol table '.dynsym' contains 21 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 00000000 0 NOTYPE LOCAL DEFAULT UND
1: 000003d8 0 SECTION LOCAL DEFAULT 8
2: 00000484 0 SECTION LOCAL DEFAULT 10
3: 000006d0 0 SECTION LOCAL DEFAULT 11
4: 00000748 0 SECTION LOCAL DEFAULT 12
5: 0000174c 0 SECTION LOCAL DEFAULT 13
6: 00001754 0 SECTION LOCAL DEFAULT 14
7: 0000175c 0 SECTION LOCAL DEFAULT 15
8: 00001830 0 SECTION LOCAL DEFAULT 17
9: 00001878 0 SECTION LOCAL DEFAULT 19
10: 00001880 0 SECTION LOCAL DEFAULT 20
11: 00000000 0 NOTYPE WEAK DEFAULT UND _Jv_RegisterClasses
12: 000006d0 0 NOTYPE GLOBAL DEFAULT 11 _fini
13: 00009830 0 NOTYPE GLOBAL DEFAULT ABS _gp
14: 00001880 0 NOTYPE GLOBAL DEFAULT ABS __bss_start
15: 00000000 356 FUNC WEAK DEFAULT UND __cxa_finalize@GLIBC_2.10 (2)
16: 00001830 4 OBJECT GLOBAL DEFAULT 17 j
17: 000005e0 96 FUNC GLOBAL DEFAULT 10 func_b
18: 00001884 0 NOTYPE GLOBAL DEFAULT ABS _end
19: 00001880 0 NOTYPE GLOBAL DEFAULT ABS _edata
20: 000003d8 0 NOTYPE GLOBAL DEFAULT 8 _init
.
And for the 'dynamic' linker, I put some relocating codes to the machine dependent function 'elf_machine_rela' of '/nios2gcc4/src/glibc-ports-2.5/sysdeps/nios2/dl-machine.h'.
Unfortunately this relocation rewrites the 'text' section, so we must set the flag of elf's 'Program Headers' like
Program Headers:
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
LOAD 0x000000 0x00000000 0x00000000 0x00738 0x00738 RWE 0x1000 <-- Flg must be RWE instead of R E.
LOAD 0x000738 0x00001738 0x00001738 0x0012c 0x0012d RW 0x1000
DYNAMIC 0x00074c 0x0000174c 0x0000174c 0x000c8 0x000c8 RW 0x4
.
Now I can't do this through the linker, so now I used a 'binary editor' and rewrote it.
Anyway, we can make the position dependent codes to shared libraries, but we need switches '-Bsymbolic' and '-G0' (this is needed to avoid the use of gp register for optimization), instead of '-fPIC'. I'm not sure that this direction is right or not.
Kazu