<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/86047>86047</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            LLD `--just-symbols` behavior differs from BFD and Gold
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            lld
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          Prabhuk
      </td>
    </tr>
</table>

<pre>
    

This bug summarizes a difference in behavior in LLD compared to BFD and Gold linkers when `--just-symbols` is involved.


## Experiment

**Consider `[main.cc](main.cc)` which we compile into `main\_foo\_bar\_baz.o`.**

$ clang -c main.cc -o main\_foo\_bar\_baz.o


```
extern "C" int foo();
extern "C" int baz();
extern "C" int bar();

int try_link() {
  return baz() + foo() + bar(); 
}
```


**Consider `foo_and_bar.S`,**


<table>
  <tr>
   <td>foo_and_bar.S
   </td>
   <td>Create “foo_bar_from_assembly.o”
   </td>
  </tr>
  <tr>
   <td style="background-color: #333333"><code>.text \
        .globl  bar \
        .type   bar,@function \
bar: \
        movl    $45, %eax \
        retq \
 \
.globl  foo \
        .type   foo,@function \
foo: \
        movl    $65, %eax \
        retq</code>
   </td>
   <td style="background-color: #333333"><code>$ clang -c foo_and_bar.S -o foo_bar_from_assembly.o \
 \
$ llvm-nm foo_bar_from_assembly.o \
 \
0000000000000000 T bar \
0000000000000006 T foo</code>
   </td>
  </tr>
</table>


**Consider `foo_and_baz.S`,**


<table>
  <tr>
   <td>foo_and_baz.S
   </td>
   <td>Create “foo_baz_linked_bin_elf” 
   </td>
  </tr>
  <tr>
   <td style="background-color: #333333"><code>.text \
        .globl  baz \
        .type   baz,@function \
bar: \
        movl    $55, %eax \
        retq \
 \
.globl  foo \
        .type   foo,@function \
foo: \
        movl    $65, %eax \
        retq</code>
   </td>
   <td style="background-color: #333333"><code>$ clang -c foo_and_baz.S -o foo_baz_from_assembly.o \
 \
$ llvm-nm foo_baz_from_assembly.o \
 \
0000000000000000 T baz \
0000000000000006 T foo \
 \
$ ld.lld foo_baz_from_assembly.o -o foo_baz_linked_bin_elf \
 \
$ llvm-nm foo_baz_linked_bin_elf  \
0000000000201120 T baz \
0000000000201126 T foo</code>
   </td>
  </tr>
</table>



## BFD linker


```
$ ld.bfd --just-symbols foo_baz_linked_bin_elf -o bin_foo_bar_baz_bfd foo_bar_from_assembly.o main_foo_bar_baz.o
ld.bfd: warning: foo_bar_from_assembly.o: missing .note.GNU-stack section implies executable stack
ld.bfd: NOTE: This behaviour is deprecated and will be removed in a future version of the linker
ld.bfd: warning: cannot find entry symbol _start; defaulting to 0000000000401000

$ llvm-nm bin_foo_bar_baz_bfd
0000000000401010 T _Z8try_linkv
0000000000403000 R __bss_start
0000000000403000 R _edata
0000000000403000 R _end
                 U _start
0000000000401000 T bar
0000000000201120 A baz
0000000000201126 A foo
```



## Gold linker


```
$ ld.gold --just-symbols foo_baz_linked_bin_elf -o bin_foo_bar_baz_gold foo_bar_from_assembly.o main_foo_bar_baz.o

$ llvm-nm bin_foo_bar_baz_gold
00000000004000c0 T _Z8try_linkv
0000000000000000 A __bss_start
0000000000000000 A _edata
0000000000000000 A _end
00000000004000b0 T bar
0000000000201120 A baz
0000000000201126 A foo
```



## LLD linker

LLD throws a duplicate symbol failure.


```
$ ld.lld --just-symbols foo_baz_linked_bin_elf -o bin_foo_bar_baz_lld foo_bar_from_assembly.o main_foo_bar_baz.o
ld.lld: error: duplicate symbol: foo
>>> defined in foo_baz_linked_bin_elf
>>> defined in foo_bar_from_assembly.o
```


**Question: Is this difference in behavior expected with LLD or is this a bug?**

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWEtv4zgM_jXKhYihyM7rkEObNIMFBrOvzmUvgWzTiaaylJXktM2vX9BO2jycptPBLPawhuGHxJdIfjRl6b1aGsQJ69-y_qwjq7CybvKbk-mqeuikNn-eMD5j_Ka53q-Uh7Ragq_KUjq1RQ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