Creating relocatable shared library
Posted: Sat Mar 28, 2009 5:46 am
Hey guys,
I've spent some time thinking about this, but I still could not figure out how to solve it. I'd like to write shared libraries for my OS (using ELF), but as opposed to shared libraries on Linux systems, they should not be position independent, but relocatable. (I.e. they should neither have a GOT through which addresses are routed, nor a PLT.) The reason for this choice is that PIC always has some overhead routing everything through the GOT and wastes a register to store the GOT address. Relocatable code needs some more work relocating it at load time, but results in faster executables at run time.
So, what I need to create is some ELF file that does not contain GOT or PLT sections or any other sections related to them. However, I still need dynamic linking information, like a table of exported symbols. These should be stored in a .dynsym section, as it would be in any other shared library. I also need .dynstr and .dynamic sections, to store dynamic function names, module names and things like DT_INIT and DT_FINI functions. And of course I need relocation sections. (There are some libraries that will be mapped to a fixed location in every address space, like a library with kernel services, that encapsulates the kernel interface. These libraries can be linked at a fixed location and don't need to be relocated at load time, but they still need sections for dynamic linking of executables against them.)
My questions are:
Thanks in advance!
I've spent some time thinking about this, but I still could not figure out how to solve it. I'd like to write shared libraries for my OS (using ELF), but as opposed to shared libraries on Linux systems, they should not be position independent, but relocatable. (I.e. they should neither have a GOT through which addresses are routed, nor a PLT.) The reason for this choice is that PIC always has some overhead routing everything through the GOT and wastes a register to store the GOT address. Relocatable code needs some more work relocating it at load time, but results in faster executables at run time.
So, what I need to create is some ELF file that does not contain GOT or PLT sections or any other sections related to them. However, I still need dynamic linking information, like a table of exported symbols. These should be stored in a .dynsym section, as it would be in any other shared library. I also need .dynstr and .dynamic sections, to store dynamic function names, module names and things like DT_INIT and DT_FINI functions. And of course I need relocation sections. (There are some libraries that will be mapped to a fixed location in every address space, like a library with kernel services, that encapsulates the kernel interface. These libraries can be linked at a fixed location and don't need to be relocated at load time, but they still need sections for dynamic linking of executables against them.)
My questions are:
- How do I create such a library, that contains all these sections for dynamic linking, but without GOT, PLT and such?
- How do I specify what has to be relocated at load time? Is the --emit-relocs option sufficient? It looks quite unspecific. Do I have to relocate everything that has been relocated at link time, or does linking the source files into a shared library already resolve some relocations, that don't have to be re-done when the library is loaded? Does --emit-relocs or --relocatable put any "unneeded" relocations into the library?
- How do I tell my compiler / linker which functions / objects should be exported (i.e. placed in .dynsym), and which should not? (Without giving something like a visibility attribute to every function that should not be exported - there will be a lot more of them than exported functions, so not exporting the function is the default.)
- How do I link an executable against such a library? Of course, I know how to do the dynamic linking at load time - I just check the DT_NEEDED entries in my executable, load the required libraries, look up any symbols and perform relocations. But how do I get all this dynamic linking stuff into my executable? How do I specify which libraries are needed?
Thanks in advance!