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

    <tr>
        <th>Summary</th>
        <td>
            lld mingw undefined weak symbol, --wrap, import library, and relocation results in garbage pointer
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            lld,
            lld:COFF
      </td>
    </tr>

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

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

<pre>
    There are several moving parts here that seem to be necessary to reproduce this, so bear with me.  This is an attempt to reduce something from a more complicated situation inside Cygwin startup code.

testreal.c
```c
void foo (void)
{ }
```

testweak.c
```c
void __wrap_foo (void)
{ }

extern __attribute__ ((weak)) void foo (void) __asm__ ("__real_foo");

void (*bar)(void) = &foo;

int main (void)
{
        return !!bar;
}
```

```
clang -shared -o testreal.dll -Wl,-out-implib,testreal.a testreal.c -Wl,--wrap,foo
clang -o testweak testweak.c testreal.a -fuse-ld=lld -Wl,--wrap,foo
```

With lld running testweak returns 1, with bfd it returns 0.  If you inspect the pointer in a debugger, it is garbage with lld.
```
Thread 1 hit Breakpoint 1, 0x00007ff7a3da146f in main ()
(gdb) p (void *)bar
$1 = (void *) 0x7ff663da0000
```

If you remove the dynamicbase flag (so the testweak.exe does not need to be relocated) it returns 0 with lld
```
clang -o testweak testweak.c testreal.a -Wl,--wrap,foo -fuse-ld=lld -Wl,--disable-dynamicbase
```

Tested with x86_64-w64-mingw32 and x86_64-pc-cygwin targets

@mstorsjo @kikairoya @tyan0
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyEVc1u4zoPfRplQziQ5b9k4UWaIsC3-jYFZhnQFm1rKluGJLfN21_Q-Wkx7dwbBIhlHR6Sh0cKhmD6iagWxZMonje4xMH5-jd5Gi9ayXS_aZy-1C8DeQL0BIHeyKOF0b2ZqYcZfQyw7sYBIwSiEaKDhmCilkJAf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