0

I have a platform specific library (libMyPlatform.a) that has various symbols exposed in specific sections of a ELF binary (the bootloader on our system parses these). This library may be statically linked to other binaries by third party developers.

In the linking stage, it looks the ELF sections and the unused symbols from libMyPlatform.a aren't propagated to the third party binaries--probably because the linker discards them as they're unused.

How can I force the symbols from libMyPlatform.a to be visible in the final binary that's produced?

D.R.
  • 1
  • Related? https://stackoverflow.com/q/29545191/1531971 –  Oct 17 '17 at 16:29
  • @jdv kind of--but my question is more of a general case. https://stackoverflow.com/a/29545417/8791029 works great if I'm compiling to an executable. But it doesn't work if I compile it to a library, then link it to an executable, i.e. the symbol will be in the library but not the binary. I'd like for it to be present in the binary as well. – D.R. Oct 17 '17 at 18:28

1 Answers1

0

If you can control users LDFLAGS you could add -Wl,--whole-archive -lyourlib Wl,--no-whole-archive.

If you can't, the only way is to forces references to all needed symbols in object file which is guaranteed to be linked by application (e.g. contains core API which will always be used, etc.):

static __attribute__((used)) void *dummy[] = {
  // Contents can be auto-generated
  mysym1,
  mysym2,
  ...
};

This of course forces you to loose some precious memory on dummy array that's never used.

yugr
  • 19,769
  • 3
  • 51
  • 96