bfd/objcopy handling of localizing common symbols

Jordan Rupprecht via binutils
Sat Oct 27 00:20:00 GMT 2018

It's me again, with some more archaeology questions about libbfd :)

I'm looking at a project that uses objcopy --localize-hidden,
It turns out that objcopy is not actually localizing all the hidden
symbols; most noticeably, common symbols are not being localized. This
works for them, but when dropping in llvm-objcopy (which always
localizes), it breaks. tl;dr: is this expected?

For example, a common symbol:
$ echo 'int foo;' | gcc -xc - -c -o /tmp/common.o; nm /tmp/common.o;
objdump -t /tmp/common.o | grep foo; objcopy --localize-symbol foo
/tmp/common.o; nm /tmp/common.o; objdump -t /tmp/common.o | grep foo
0000000000000004 C foo
0000000000000004       O *COM*  0000000000000004 foo
0000000000000004 C foo
0000000000000004       O *COM*  0000000000000004 foo

objdump actually reports it as neither a global nor a local symbol.

Compared to a non-common symbol:
$ echo 'int foo=0;' | gcc -xc - -c -o /tmp/common.o; nm /tmp/common.o;
objdump -t /tmp/common.o | grep foo; objcopy --localize-symbol foo
/tmp/common.o; nm /tmp/common.o;
objdump -t /tmp/common.o | grep foo
0000000000000000 B foo
0000000000000000 g     O .bss   0000000000000004 foo
0000000000000000 b foo
0000000000000000 l     O .bss   0000000000000004 foo
As expected, foo goes from global to local.

I noticed the same thing with hidden symbols, e.g. the dynamorio
example above, which compiles with -fvisibility=hidden and then runs
objcopy --localize-hidden.

I dug into the source a bit, and it turns out the flags for a global,
common symbol does not include BSF_GLOBAL because of this line:;a=blob;f=bfd/elfcode.h;h=f224c8b79d2fb0516a90c1a8ad818d11a1a971a5;hb=HEAD#l1298
Which then causes the "flags & (BSF_GLOBAL | BSF_WEAK)" check to fail

I think I've connected the dots on why this is happening, but what I
couldn't figure out from the code or commit history is whether any of
this is intentional or desired. Does anyone know if this is expected?

(For completeness, I filed the bug against llvm-objcopy here:

Thanks -- Jordan
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4849 bytes
Desc: S/MIME Cryptographic Signature
URL: <>

More information about the Binutils mailing list