2

From what I understand, the ELF format doesn't specify which symbols come from which file - Every ELF that uses import has a list of symbols to import and list of file names, and the loader is trying to locate those symbols in the file names.

But what happens if there is a collision - the same symbol appears twice, in different files? Is it possible to somehow force by the ELF format the destination file to look for a specific symbol?

3

This should be the problem of the linker at compile time. These things are not solved dynamically but statically. It should produce an error of the kind multiple definition as illustrated on the following example (taken from here):

/tmp/ccscmcbS.o:(.bss+0x0): multiple definition of `global_base'
/tmp/ccyjhjQC.o:(.bss+0x0): first defined here
/tmp/ccscmcbS.o: In function `find_free_block':
support.c:(.text+0x0): multiple definition of `find_free_block'
/tmp/ccyjhjQC.o:main.c:(.text+0x0): first defined here
/tmp/ccscmcbS.o: In function `request_space':
support.c:(.text+0x55): multiple definition of `request_space'
/tmp/ccyjhjQC.o:main.c:(.text+0x55): first defined here
/tmp/ccscmcbS.o: In function `get_block_ptr':
support.c:(.text+0xfe): multiple definition of `get_block_ptr'
/tmp/ccyjhjQC.o:main.c:(.text+0xfe): first defined here
collect2: error: ld returned 1 exit status
| improve this answer | |
  • I understand the compile-time, but my question is about the run time because then It's possible to do some naughty stuff – macro_controller Feb 5 at 15:51
  • Well, it can't be possible at runtime because it would be stopped at compile time... except if you use dlopen. But, in this case, the first loaded symbol will stay and anything with the same name will be rejected. – perror Feb 5 at 15:57
  • So basically the resolve order is the key? If the symbol name is found, the search is over, and there is no chance for the other name to be loaded too? – macro_controller Feb 5 at 16:38
  • The thing is that two same symbols cannot be loaded. I one is already present, the second won't make it. At build time or at run time... Collisions are avoided at any price. – perror Feb 5 at 17:32
  • @macr0controller correct. You may want to check out LD_PRELOAD and friends. macOS has a similar facility (DYLD_INSERT_LIBRARIES). – 0xC0000022L Feb 6 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.