"gcc -g -feliminate-dwarf2-dups" may generate symbols relative to debug section. When "ld -s" removes debug section, it changes those symbols to absolute. The resulting shared library may not be usable.
Created attachment 1338 [details] A testcase [hjl@gnu-13 dwarf-7]$ make gcc -g -feliminate-dwarf2-dups -c -o a.o a.c gcc -g -feliminate-dwarf2-dups -fPIC -c -o c.o c.c gcc -s -shared -o libc.so c.o readelf -s libc.so | grep DW 19: 0000000000000019 0 NOTYPE GLOBAL DEFAULT ABS DW.z.h.f1c9b137.0 gcc -o a libc.so a.o a.o: In function `main': /export/home/hjl/bugs/binutils/dwarf-7/a.c:9: multiple definition of `DW.z.h.f1c9b137.0' collect2: ld returned 1 exit status make: *** [a] Error 1 [hjl@gnu-13 dwarf-7]$
strip has the same issue.
strip is even worse. When .debug_info section is removed, we leave 5: 000000000000008d 0 NOTYPE GLOBAL DEFAULT 12 DW.z.h.f1c9b137.0 section index is wrong. [hjl@gnu-13 dwarf-7]$ make STRIP=strip libb.so gcc -g -feliminate-dwarf2-dups -fPIC -c -o c.o c.c ./ld -shared -o libb-g.so c.o strip -g libb-g.so -o libb.so [hjl@gnu-13 dwarf-7]$ readelf -sS libb.so There are 13 section headers, starting at offset 0x420: Section Headers: [Nr] Name Type Address Offset Size EntSize Flags Link Info Align [ 0] NULL 0000000000000000 00000000 0000000000000000 0000000000000000 0 0 0 [ 1] .hash HASH 0000000000000120 00000120 000000000000003c 0000000000000004 A 2 0 8 [ 2] .dynsym DYNSYM 0000000000000160 00000160 00000000000000f0 0000000000000018 A 3 4 8 [ 3] .dynstr STRTAB 0000000000000250 00000250 0000000000000038 0000000000000000 A 0 0 1 [ 4] .text PROGBITS 0000000000000288 00000288 0000000000000006 0000000000000000 AX 0 0 4 [ 5] .eh_frame PROGBITS 0000000000000290 00000290 0000000000000038 0000000000000000 A 0 0 8 [ 6] .dynamic DYNAMIC 00000000002002c8 000002c8 00000000000000b0 0000000000000010 WA 3 0 8 [ 7] .got.plt PROGBITS 0000000000200378 00000378 0000000000000018 0000000000000008 WA 0 0 8 [ 8] .bss NOBITS 0000000000200390 00000390 0000000000000008 0000000000000000 WA 0 0 4 [ 9] .comment PROGBITS 0000000000000000 00000390 000000000000002d 0000000000000000 0 0 1 [10] .shstrtab STRTAB 0000000000000000 000003bd 0000000000000061 0000000000000000 0 0 1 [11] .symtab SYMTAB 0000000000000000 00000760 0000000000000198 0000000000000018 12 12 8 [12] .strtab STRTAB 0000000000000000 000008f8 0000000000000045 0000000000000000 0 0 1 Key to Flags: W (write), A (alloc), X (execute), M (merge), S (strings) I (info), L (link order), G (group), x (unknown) O (extra OS processing required) o (OS specific), p (processor specific) Symbol table '.dynsym' contains 10 entries: Num: Value Size Type Bind Vis Ndx Name 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND 1: 0000000000000288 0 SECTION LOCAL DEFAULT 4 2: 0000000000000290 0 SECTION LOCAL DEFAULT 5 3: 0000000000200390 0 SECTION LOCAL DEFAULT 8 4: 0000000000000288 6 FUNC GLOBAL DEFAULT 4 doprintf 5: 000000000000008d 0 NOTYPE GLOBAL DEFAULT 12 DW.z.h.f1c9b137.0 6: 0000000000200390 0 NOTYPE GLOBAL DEFAULT ABS __bss_start 7: 0000000000200390 4 OBJECT GLOBAL DEFAULT 8 foo 8: 0000000000200390 0 NOTYPE GLOBAL DEFAULT ABS _edata 9: 0000000000200398 0 NOTYPE GLOBAL DEFAULT ABS _end Symbol table '.symtab' contains 17 entries: Num: Value Size Type Bind Vis Ndx Name 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND 1: 00000000002002c8 0 OBJECT LOCAL HIDDEN ABS _DYNAMIC 2: 0000000000200378 0 OBJECT LOCAL HIDDEN ABS _GLOBAL_OFFSET_TABLE_ 3: 0000000000000120 0 SECTION LOCAL DEFAULT 1 4: 0000000000000160 0 SECTION LOCAL DEFAULT 2 5: 0000000000000250 0 SECTION LOCAL DEFAULT 3 6: 0000000000000288 0 SECTION LOCAL DEFAULT 4 7: 0000000000000290 0 SECTION LOCAL DEFAULT 5 8: 00000000002002c8 0 SECTION LOCAL DEFAULT 6 9: 0000000000200378 0 SECTION LOCAL DEFAULT 7 10: 0000000000200390 0 SECTION LOCAL DEFAULT 8 11: 0000000000000000 0 SECTION LOCAL DEFAULT 9 12: 0000000000000288 6 FUNC GLOBAL DEFAULT 4 doprintf 13: 0000000000200390 0 NOTYPE GLOBAL DEFAULT ABS __bss_start 14: 0000000000200390 4 OBJECT GLOBAL DEFAULT 8 foo 15: 0000000000200390 0 NOTYPE GLOBAL DEFAULT ABS _edata 16: 0000000000200398 0 NOTYPE GLOBAL DEFAULT ABS _end [hjl@gnu-13 dwarf-7]$
One way to work around it is to make debug symbols local or hidden in DSO.
A patch is posted at http://sourceware.org/ml/binutils/2006-09/msg00390.html
The updated patch is at http://sourceware.org/ml/binutils/2006-10/msg00002.html
Fixed by http://sourceware.org/ml/binutils/2006-10/msg00116.html
Do we need to do this even when info->relocatable? PGI apparently uses (or used?) global symbols in .debug_info sections and referenced them from within object's .debug_info section. With this change, ld -r on one or more *.o files which define such global symbols will make those symbols STB_LOCAL and so a final link fails. IMHO either the debug symbols should be made global, but hidden, or made local only when not ld -r. --- bfd/elflink.c 2 Mar 2008 22:26:09 -0000 1.299 +++ bfd/elflink.c 25 Mar 2008 15:01:19 -0000 @@ -4294,7 +4294,7 @@ elf_link_add_object_symbols (bfd *abfd, dynsym = TRUE; } - if (definition && (sec->flags & SEC_DEBUGGING)) + if (definition && (sec->flags & SEC_DEBUGGING) && !info->relocatable) { /* We don't want to make debug symbol dynamic. */ (*bed->elf_backend_hide_symbol) (info, h, TRUE); works for me.
Subject: Re: Linker creates dynamic debug symbols in DSO Hi Jakub, > --- bfd/elflink.c 2 Mar 2008 22:26:09 -0000 1.299 > +++ bfd/elflink.c 25 Mar 2008 15:01:19 -0000 > @@ -4294,7 +4294,7 @@ elf_link_add_object_symbols (bfd *abfd, > dynsym = TRUE; > } > > - if (definition && (sec->flags & SEC_DEBUGGING)) > + if (definition && (sec->flags & SEC_DEBUGGING) && !info->relocatable) > { > /* We don't want to make debug symbol dynamic. */ > (*bed->elf_backend_hide_symbol) (info, h, TRUE); Well noone seems to object to this, so please go ahead an apply it. Along with a changelog entry of course. Actually, adding a test case would be a good thing too... Cheers Nick
Jakub's fix for the Comment 8 and Comment 9 has been committed with a testcase: http://sourceware.org/ml/binutils/2008-08/msg00188.html http://sourceware.org/ml/binutils-cvs/2008-08/msg00127.html bfd/ Fix PR ld/3290 regression for cross-files DW_FORM_ref_addr relocations. * elflink.c (elf_link_add_object_symbols): Make debug symbols local now only for non-RELOCATABLE targets. ld/testsuite/ * ld-x86-64/dwarfreloc.exp, ld-x86-64/dwarfreloc.rd, ld-x86-64/dwarfreloc1.s, ld-x86-64/dwarfreloc2.s: New test.