Bug 28926

Summary: objcopy --weaken-symbol does not weaken STB_GNU_UNIQUE symbols
Product: binutils Reporter: Fangrui Song <maskray>
Component: binutilsAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal    
Priority: P2    
Version: unspecified   
Target Milestone: ---   
Host: Target:
Build: Last reconfirmed:
Project(s) to access: ssh public key:

Description Fangrui Song 2022-02-28 00:56:09 UTC
echo '.data; .type x, @gnu_unique_object; x:' | as - -o a.o
objcopy -W x a.o
readelf -Ws a.o

The symbol 'x' remains STB_GNU_UNIQUE:

Symbol table '.symtab' contains 2 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000000000     0 OBJECT  UNIQUE DEFAULT    2 x

I think with the explicit request, it makes sense to weaken 'x'.

The documentation for --weaken is:

      --weaken
           Change all global symbols in the file to be weak.  This can be useful when building an object which will be linked against other objects using the -R option to the linker.  This option is only effective when using an object file format which supports weak symbols.

If we consider STB_GNU_UNIQUE "global", then --weaken probably should weaken STB_GNU_UNIQUE, too.
Comment 2 Sourceware Commits 2022-03-16 16:43:16 UTC
The master branch has been updated by Fangrui Song <maskray@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=260ecdcec427146f760836092a040f957427760b

commit 260ecdcec427146f760836092a040f957427760b
Author: Fangrui Song <i@maskray.me>
Date:   Wed Mar 16 09:38:37 2022 -0700

    objcopy --weaken-symbol: apply to STB_GNU_UNIQUE symbols
    
        PR binutils/28926
        * objcopy.c (filter_symbols): Apply weaken to STB_GNU_UNIQUE symbols
        * NEWS: Mention feature.
        * testsuite/binutils-all/objcopy.exp (objcopy_test_symbol_manipulation): New test.
        * testsuite/binutils-all/weaken-gnu-unique.s: New.
Comment 3 Fangrui Song 2022-03-16 16:47:04 UTC
Fixed for 2.39