[PATCH 2/2] gas: Anchor DWARF subprograms to the definitions

Longjun Luo luolongjuna@gmail.com
Fri Nov 27 17:09:04 GMT 2026


> Hmm, isn't this something the linker ought to sort?

Actually two things here:

1. For weak symbol resolution, you are right. The linker will choose
   only one. The strong one will override the weak one.

2. For function DIEs in DWARF, that's different. DWARF keeps both DIEs
   for the weak and the strong. All function DIEs should describe
   themselves, otherwise it will lead to downstream errors.

In GAS now, the DIE of the weak function uses the symbol f, so after
linking it gets the address of the strong one. And it may cause errors
like this example.

Say, we have two assembly files:

weak.s

    .text
    .weak f
    .type f, @function
f:
    mov $11, %eax
    ret
    .size f, .-f

strong.s

    .text
    .globl f
    .type f, @function
f:
    xor %eax, %eax
    ret
    .size f, .-f

compile:

$ as -g -o weak.o weak.s
$ as -g -o strong.o strong.s
$ ld -e f -Ttext=0x1000 weak.o strong.o

check results:

$ addr2line -f 0x1000 0x1006
??
.../weak.s:5
f
??:?

$ llvm-dwarfdump --verify
Verifying unit: 1 / 2, "weak.s"
error: DIE address ranges are not contained in its parent's ranges:

Problem here is the DIE of the weak function is inconsistent:

    address -> from the strong one
    size    -> from the weak one itself

GCC and Clang use local labels to anchor function address of DIE and
work as expected. This patch makes GAS behave the same way.

Longjun


More information about the Binutils mailing list