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

    <tr>
        <th>Summary</th>
        <td>
            ld64.lld reports duplicated symbol error when linking archives created in certain ways
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

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

<pre>
    
How to produce:
main.cpp
```C++
int foo();
int bar();

int main(int argc, char** argv) {
  bar();
  foo();
  return 0;
}
```

foo1.cpp
```C++
int foo(){
  return 1;
}
```

foo2.cpp
```C++
int foo(){
  return 1;
}

int bar(){
  return 1;
}
```

```bash
clang -c foo1.cpp foo2.cpp main.cpp
ar rcs libfoo.a foo1.o foo2.o
clang -fuse-ld=/Users/bytedance/github/llvm-project/build/bin/ld64.lld main.o libfoo.a
```
Then lld will report the following error:
```
ld64.lld: error: duplicate symbol: __Z3foov
>>> defined in libfoo.a(foo1.o)
>>> defined in libfoo.a(foo2.o)
clang: error: linker command failed with exit code 1 (use -v to see invocation)
```
However, using Apple OSX's ld64 is fine (just remove -fuse-ld option and rerun the last shell command)

It is interesting that if you exchange the content of foo1.cpp and foo2.cpp:

foo1.cpp
```C++
int foo(){
  return 1;
}

int bar(){
  return 1;
}
```

foo2.cpp
```C++
int foo(){
  return 1;
}
```

Move the function `bar()` to foo1.cpp, and then rerun the shell commands above. This time lld would work fine.

I inspected the source code of ld64.lld a little bit, and my guess is that, when ld64.lld was looking for symbol for  `foo()`, it loads the first object file it finds. If this object file contains the symbol for `bar()`, then lld will not bother to load the second object file, because it already has everything it needs to link the final executable file. However, if the first object file contains the symbol for `foo()` only, and doesn't contains the symbol for `bar()`, lld will try to load the second object file in order to search the symbol for `bar()`. At this time, lld realized that it already has loaded the `foo()`, so it throws the duplicated symbol error.

I'm not sure whether this is the correct behavior, but many Apps in my company suffer from this because they have lots of duplicated symbols. When we were using Apple's ld64, it just works magically (maybe because of the certain linking order of our input object files). Therefore, we fail to use lld as a drop-in replacement for ld64.

I know the right way to solve this problem is to refactor our codes (could be difficult, since there are tons of them...), but I'm wondering whether is there any other easier way to work around this?
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy1Vk1z2zYQ_TXUBWOORFofPujgxvE0h35Mk0w7vWRAcikiBgkOAEpVf33fAhQlOU6TdJqxbMkAuPt239snFKY6bpP5QzK__9EchDeit6YaSkry-7jcStWlZd_H_5LVPL5eJdkP_AqrqvOiNibJNkl2l-QXq4W0z1bPexwZm_xR2l2ZZK9E2fD5e7x4aY_nRLIeHxQvBBMvpBXCkh9sJ-bnnOuHZ_AvsSDE4tsqXD9Ptfj6VNl3SPVCu_8zxGmxkK6JS6WW3U7clOLUKXGqQ1yrQ1phSye0KnAglfG8iafNVax6cHSjqyR_SLLH946sw3tx9FTJDuLLHnfKN0OBD1rv2xuI8iOVns8MCo_hnbXzqKvVbap1FXGYKfOL9b1rqBN8-KC0Rlt6Y73wDQGf1uaggIusNXaS_rPnT8mwPx0U1dBrVUpPwh3bwmhe-_Dhzxww9mOU_HV8iYpq1VElVHcGmm1ik5izrz2eXRwP_bxCpFX3RFaUpm1lV4laKk1cs28E_aU8NioSC4FQ4EDc7HnoHRHS7A0KUaY7Y7luAByC9mR5UAfH7brve03il7d_JNkatKM_QjnBqDn8x8F5tLk1e5oIF6bnDIKRWbJDFwjQEiddQ6BlhH2GEP6-8RwYCidLznNq30is1eJoBpQF3-h2FGKVBqcwCqY-yzX04TR6E7vfc_j_z4n8TqbxUqqfmKswEkNXBqKCEZxKWM1ZLFPLoAPurOe5OpN5xaMTskDMVLxrQKBXLcUJNEP4a5-CWtIrrsGz6zHtVMV4ZrAlRdmC1GnkJZTuPfRXKH-C0h7FbiDnWC2sEF4_hLE_PXWQEKoxT6yh2thxasNHrvXcQzQGD2NetJGVi01RFjo1BVsR_kFqxe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