2

My project have a several object files with project internal functions, and one object file which exposes functions for a public interface. My goal is to use incremental linking to combine all object files to a single file, and have this single file only expose the symbols needed for the public interface.

foo.c, public interface.

void bar(void);

void foo(void)
{
    bar();
}

bar.c, project internal function.

void bar(void)
{
}
$ gcc -c foo.c
$ gcc -c bar.c
$ ld -r -o combined.o foo.o bar.o
$ strip -N bar combined.o  
strip: not stripping symbol `bar' because it is named in a relocation

Both functions are already part of the same segment in the same object file, so I believe it should be possible to resolve the relocation during the link step? Is it possible to alter the ld command in order for the relocation not to be needed?

frol
  • 51
  • 2
  • *bar* is not static so can be needed to link combined.o with other objects ? – bruno Aug 14 '20 at 12:46
  • the old question https://stackoverflow.com/questions/20294624/how-to-fix-strip-failure-because-it-is-named-in-a-relocation had no answer – bruno Aug 14 '20 at 12:49
  • bar() is not static because I need to access it from the foo file. But after linking combined.o, I do not want bar() to be visible anymore. Ideally ld should do the relocation so the symbol is not needed anymore, but I cannot convince ld to do that. I have tried e.g. `-fvisibility=hidden` when compiling bar.c, but not been able to find a solution. – frol Aug 14 '20 at 13:30
  • 1
    Why the votes down? This is a good question properly posed. – Eric Postpischil Aug 14 '20 at 13:51
  • 1
    @EricPostpischil I agree, so I UV – bruno Aug 14 '20 at 13:54
  • 1
    Apple’s ld has options for this. For example, you can use `ld -r -exported_symbol _foo -o combined.o foo.o bar.o` to request that `_foo` be exported, in which case unlisted symbols will be marked local instead of global, after which `strip -x` will remove them. There are related options to use a file to pass a list of symbols to be exported or to specify symbols not to be exported instead of specifying those to be exported. I do not know if these are on the mainline (non-Apple) `ld`. – Eric Postpischil Aug 14 '20 at 14:09

0 Answers0