Issue 135223
Summary [LLD] relinking objects that contain COMMON symbols adds superfluous COMMON section.
Labels lld:ELF
Assignees
Reporter mandlebug
    When performing incremental linking on object files that contain symbols with COMMON linkage, the output object will contain the original COMMON symbols but will add a COMMON section. This COMMON section ends up getting copied into the .bss section of the final binary.

Reproducer:


header.h
```
int a;
int b;

void foo(void);
void bar(int*);
```

main.c:
```
#include "header.h"


int main(void) {
  a = 1;
  foo();
  return a;
}
```

foo.c:
```
#include "header.h"

void foo(void) {
 bar(&a);
}
```

bar.c:
```
#include "header.h"

void bar(int *ip) {
  *ip = 2;
}
```

build and link commands:
```
clang -c -fcommon main.c foo.c bar.c

clang main.o foo.o bar.o -o no_relinking.out

ld.lld --hash-style=gnu --eh-frame-hdr -m elf64lppc  -o relink1.o main.o foo.o -r
ld.lld  --hash-style=gnu --eh-frame-hdr -m elf64lppc  -o relink2.o relink1.o bar.o -r

clang relink2.o -o with_relinking.out
```

examining relink1.o we have both 'a' and 'b' as COMMON symbols still defined, and a section named COMMON with the same length of sizeof(a) + sizeof(b).

`readelf --sections --wide --syms relink1.o | grep COM`
```

  [ 1] COMMON            NOBITS          0000000000000000 000040 000008 00  WA  0   0  4
     9: 0000000000000004     4 OBJECT  GLOBAL DEFAULT  COM a
    11: 0000000000000004     4 OBJECT  GLOBAL DEFAULT  COM b
```

Similarly for relink2.o we have 'a' and 'b' still defined as tentative definitions, and a 16 byte long COMMON section.

Comparing with_relinking.out and no_relinking.out bss sections we see the COMMON section has been retained in the final image.

no_relinking.out:
```
 [26] .bss              NOBITS          0000000000030da0 000da0 000009 00  WA 0   0  4

no_relinking.out:     file format elf64-powerpcle


Disassembly of section .bss:

0000000000030da0 <a>:
 30da0:       00 00 00 00     .long 0x0

0000000000030da4 <b>:
   30da4: 00 00 00 00     .long 0x0

0000000000030da8 <completed.0>:
   30da8: Address 0x0000000000030da8 is out of bounds.

```

with_relinking.out:
```
 [27] .bss              NOBITS 0000000000030da0 000da0 00001c 00  WA  0   0  4

Disassembly of section .bss:

0000000000030da0 <a>:
   30da0:       00 00 00 00     .long 0x0

0000000000030da4 <b>:
   30da4:       00 00 00 00     .long 0x0

0000000000030da8 <completed.0>:
   30da8:       00 00 00 00     .long 0x0
   30dac:       00 00 00 00     .long 0x0
   30db0:       00 00 00 00 .long 0x0
   30db4:       00 00 00 00     .long 0x0
   30db8:       00 00 00 00     .long 0x0
```
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to