Pre-link static binaries/remove

Thomas Richter thomas.richter@iis.fraunhofer.de
Fri Nov 14 07:11:20 GMT 2025


Hi Simon,

> but all we've gained here is to make the symbol local. Final relocation 
> will still only happen during final linking, and we need a relaxation 
> pass there anyway to replace the second call instruction.
> 
> If you want to turn the hidden symbols local, there is objcopy
> 
>      $ objcopy --localize-hidden test.o test2.o
> 
> That will still not resolve these symbols or apply relaxations, but that 
> is fine, all of that is handled during final link.
> 
> For more aggressive optimization, use -flto to link before compiling. I 
> think it should be possible to combine that with "ld -r" and "objcopy -- 
> localize-hidden", and it should also remove unused functions.
Thanks, I played a bit, but no avail. I compile with -flto 
-fvisibility=internal, I also tried -flto -fvisibility=hidden, all 
symbols to be exported are marked as __attribute__ ((visibility 
("default"))). Then I link with

g++ -flto -o target.a -r --whole-file ...


The "--whole-archive" option does not exist, I assume you mean 
"--whole-file?"

Finally, I run objcopy --localize-hidden, but still all the symbols are 
in. "nm" lists the hidden symbols as weak ("W"), but I still can see them.

Greetings,
	Thomas








More information about the Binutils mailing list