elf - Creating a relocatable shared library with binutils -
i have custom toolchain generates relocatable shared libraries. works equally custom elf loader loads these memory, fixing them up. trying persuade gcc , binutils produce elf files compatible loader.
unfortunately, appear binutils refusing generate relocatable shared objects. it'll generate pic shared objects, don't want due overhead of got/plt (and besides, custom elf loader doesn't support it). , it'll generate relocatable objects, they're not dynamic objects , don't have appropriate sections elf loader wants able load them.
i'm not clear why gnu ld refuses allow me specify --relocatable , -shared on same command line. can enlighten me? , know incantation make ld generate object files i'm looking for?
i see nobody has answered question, think i'll try.
i'll try show how works means of practical example. here c code, has artificial purposeful mix of external global functions , data -- bread , butter of relocations.
/* hello.c */ char* hello = "hello"; /* say.c */ #include "stdio.h" extern char* hello; void say(void){ printf(hello); } /* main.c */ extern void say(void); void please_say(void){ say(); } int main(void){ please_say(); return 0; }
now usual way shared object/library compile each c file -fpic, , link lot -shared. after that, can use readelf examine relocations. this:
gcc -fpic -c *.c gcc -shared -o libtemp.so *.o readelf -r libtemp.so
the relocation data follows:
relocation section '.rel.dyn' @ offset 0x34c contains 6 entries: offset info type sym.value sym. name 000016dc 00000008 r_386_relative 000016e0 00000008 r_386_relative 000016ac 00000106 r_386_glob_dat 00000000 __gmon_start__ 000016b0 00000206 r_386_glob_dat 00000000 _jv_registerclasses 000016b4 00000d06 r_386_glob_dat 000016e0 hello 000016b8 00000406 r_386_glob_dat 00000000 __cxa_finalize relocation section '.rel.plt' @ offset 0x37c contains 5 entries: offset info type sym.value sym. name 000016c8 00000107 r_386_jump_slot 00000000 __gmon_start__ 000016cc 00000507 r_386_jump_slot 000004fc please_say 000016d0 00000807 r_386_jump_slot 00000540 say 000016d4 00000307 r_386_jump_slot 00000000 printf 000016d8 00000407 r_386_jump_slot 00000000 __cxa_finalize
the r_386_glob_dat item hello got entry. similarly, r_386_jump_slot items say, please_say , printf plt entries. come use of position independent code, not fact have made shared object.
after doing same build process without -fpic, different relocations. so
gcc -c *.c gcc -shared -o libtemp.so *.o readelf -r libtemp.so
gives us
relocation section '.rel.dyn' @ offset 0x34c contains 9 entries: offset info type sym.value sym. name 00001674 00000008 r_386_relative 00001678 00000008 r_386_relative 000004d3 00000802 r_386_pc32 000004f0 say 000004e0 00000502 r_386_pc32 000004cc please_say 000004f7 00000d01 r_386_32 00001678 hello 000004ff 00000302 r_386_pc32 00000000 printf 00001654 00000106 r_386_glob_dat 00000000 __gmon_start__ 00001658 00000206 r_386_glob_dat 00000000 _jv_registerclasses 0000165c 00000406 r_386_glob_dat 00000000 __cxa_finalize relocation section '.rel.plt' @ offset 0x394 contains 2 entries: offset info type sym.value sym. name 0000166c 00000107 r_386_jump_slot 00000000 __gmon_start__ 00001670 00000407 r_386_jump_slot 00000000 __cxa_finalize
now shared object has familiar relocations definitions. there absolute relocation of hello, , pc relative relocations functions.
what mean? got , plt tables still there. there 2 important things note. first there no got or plt entries compiled code. second got , plt tables still needed. being used initialisation , cleanup (possibly standard library). since using custom elf loader, advisable implement basic support got , plt entries, if main application standard relocations instead.
your application pay price of relocation, not of position independence.
Comments
Post a Comment