How do shared objects work?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
purevoid

How do shared objects work?

Post by purevoid »

Hi,

I've been doing a little bit of research, but haven't discovered enough to satisfy my curiosity, so here goes...

Can someone explain how shared objects work (the linux/unix way would be enough)?

I think I get how it works at the compilation level, but some in depth information about the actual process of loading an executable that uses a shared object works would be nice.

I'm also especially interested in the case that a 2nd application requires the same shared object and the first is still running.

Jonathan
guest

Re:How do shared objects work?

Post by guest »

I recommend getting a copy of the ELF Specification.

On Linux, a program starts by the kernel loading the dynamic linker into its address space (ld.so) [provided the program isn't a fork], the dynamic linker maps in the program and its required libraries. The libraries are listed in the ELF headers. The linker will then either Lazy bind or directly bind all the symbols, lazy binding means that it binds them the first time they're used which gives a faster startup for programs with lots of libraries and imported symbols, direct binding resolves all the symbols immediately. The method used is decided by the program (the flag is in the headers), once the symbol binding is complete the loader then jumps to the program entry point.

I'm not certain of the exact mechanism used in Linux, but I'm reasonably sure that the loader memory maps the library and program files into the address space whilst using Copy-on-Write [this is possibly an automatic kernel feature] to prevent redundant copies of unchanged information.

Symbol binding and library loading is a bit fiddly to do, the first stage once the library is loaded is to examine its relocation tables if it hasn't been loaded at its required address, once the table is examined you will have a list of symbols and "methods" that need to be relocated in the PLT and GOT, you perform the required calculations and then store the addresses in the GOT as required. Once this is done you then need to examine the dynamic symbols tables to find unbound symbols and attach them to public symbols in other libraries or the program [if the binding is direct]. In lazy binding you can just run the program without binding the symbols then just do the symbol lookups on demand which is more efficient in most [but not necessarily all] cases. Lazy binding works by installing a fake bond to the loader's dlsym or equivalent function in all the PLT entries.

Windows works pretty much the same way except that its loader is in the kernel, this gives them a minor performance improvement at startup. PE uses a thunk table which is conceptually similar to a PLT as well as an equivalent relocation and dynamic symbols export/import tables. By comparison, the only really noteworthy thing about PE is that each library has a separate namespace, meaning that you don't need to search all libraries for a given symbol because the library is specified, this removes the name collision problem of ELF [When 2 libraries export a function with the same name you get a collision which makes them incompatible].

Note: It's been a while since I last looked into this and am a bit hazy on the more specific details.
Post Reply