<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/135223>135223</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[LLD] relinking objects that contain COMMON symbols adds superfluous COMMON section.
</td>
</tr>
<tr>
<th>Labels</th>
<td>
lld:ELF
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
mandlebug
</td>
</tr>
</table>
<pre>
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
```
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJy0V0tv4zYQ_jX0ZSBBouTXwQcljosW2Q3QTbHHghJHEluKFEgqWffXF6TkR2x3mw1aI4Hk4fCbbx6cMZm1olGIGzK_I_PtjA2u1WbTMcUllkMzKzXfb762qKBHU2vTCdWAUJXBDpVjEqRQf3qZVqDLP7ByUAuJFlzLHFRaOSYU2H1XamnhVbgW7p8-fXr6HDayBgm9B9ci6MH1gztgvAopj7vDshGNUEwedh8Qy2FSZpwDO65i5YRWMTy3wl4IARW3MPTQoHOeeaV7gRyEcjqYiktrj8q6DrI62C6FYmYfk6QgSfEr9kbzoUJDsmIUkaRokXE0ceu_LZLpLymEcsBIdje9luMrSYoXLTjUWhO68q-ErselIC-ZIXQllCO0OK6cw5Kk6JhQcTVROF-imVCVHDgCofRIi9IjVU_E7z6ZBrL0FgAYkGwL6WgQJn5HBgAG3WDUwSOy3F7zqrX-AK3rcBw4jbEgdMFOkbhlt2Tmo3aP4QZCC9GfByQIQlDo92wPQnJgiofihkp3_iDZazaVZKqBqIKo9kpawZhGCFGD0YcAOWqGVR1WdVjVEGlQ-neD0_mL9eDGHZLHUnKIopbZNrJuL5Fk20YNEEXYRrVhHUYtNxB1gLJe5LLvK_B4I1ga67f2InNC_Tgsjc8NTD6YcydPepEOneL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