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

    <tr>
        <th>Summary</th>
        <td>
            lld::elf::link returns false from the second time on its called
        </td>
    </tr>

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

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

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

<pre>
    Using LLVM with JIT-compilation requires for some target architectures to call the linker several times, i.e. for AMD GPU kernels to turn an `.o` file into an `.so` file.

LLVM 15 and also the current code have the following problem:

`bool lld::elf::link` returns `false` from the second time on its called. I.e. the first call works fine, but subsequent calls fail.
Here's small reproducer:

```
#include "lld/Common/Driver.h"
#include "lld/Common/ErrorHandler.h"
#include "lld/Common/Memory.h"
#include <iostream>

int main()
{
 std::cout << "First call ... ";
  bool call_a = lld::elf::link( { "ld.lld" , "-shared" , "../kernel.o" , "-o" , "../kernel.so" } , llvm::outs(), llvm::errs(), true, true);
  std::cout << call_a << std::endl;


  std::cout << "Second call ... ";
  bool call_b = lld::elf::link( { "ld.lld" , "-shared" , "../kernel.o" , "-o" , "../kernel.so" } , llvm::outs(), llvm::errs(), true, true);
  std::cout << call_b << std::endl;
}

```

I tested various systems but the output is always:
First call ... 1
Second call ... 0

This behavior is agnostic to the architecture. Initially I ran into this with AMDGPU objects, but could reproduce with X86 objects.

A quick gdb session reveals that during the second call the link procedure is aborted here:

./llvm-project/lld/ELF/Driver.cpp:2624

```
  // Return if there were name resolution errors.
  if (errorCount())
 return;

```
Please help.

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzkVlFv2zgM_jXKCzHDkZPYechD1iy3HlZguNsO93aQbTrWVZYyUkrQf3-QnKRJt259P6BQLZKi-JHUxyhmvbOIKzF_L-abiQq-d7Tqjtp6pEnt2qfVV9Z2B58-_fUAR-17-P3-y7vGDXttlNfO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