<div dir="ltr">Oh sorry, maybe I should have put .so before .a in the test case. I'll fix that.</div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Feb 22, 2018 at 5:05 PM, Rafael Avila de Espindola <span dir="ltr"><<a href="mailto:rafael.espindola@gmail.com" target="_blank">rafael.espindola@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I get errors from both gold and bfd if I try to link the included<br>
testcase. Maybe it has been reduced a bit too much?<br>
<br>
Cheers,<br>
Rafael<br>
<div class="HOEnZb"><div class="h5"><br>
Rui Ueyama via Phabricator <<a href="mailto:reviews@reviews.llvm.org">reviews@reviews.llvm.org</a>> writes:<br>
<br>
> ruiu created this revision.<br>
> ruiu added a reviewer: rafael.<br>
> Herald added subscribers: arichardson, emaste.<br>
><br>
> We have an internal program that does't link without this patch. I don't<br>
> know of any open-source program that needs this, but there might be.<br>
> Since this patch improves compatibility with GNU linkers with a few lines<br>
> of code, I think it's worth to be committed.<br>
><br>
> The problem is about undefined symbols in DSOs. Some programs depend on<br>
> the GNU linkers' behavior that they pull out object files from archive<br>
> files to resolve undefined symbols in DSOs. We already allow that kind of<br>
> "reverse" dependency (from DSOs to the main executable) for regular<br>
> symbols, in particular, for "__progname" symbol, but that didn't work<br>
> if the symbol is in an archive file. This patch is to make it work.<br>
><br>
><br>
> <a href="https://reviews.llvm.org/D43658" rel="noreferrer" target="_blank">https://reviews.llvm.org/<wbr>D43658</a><br>
><br>
> Files:<br>
>   lld/ELF/SymbolTable.cpp<br>
>   lld/test/ELF/shlib-undefined-<wbr>archive.s<br>
><br>
><br>
> Index: lld/test/ELF/shlib-undefined-<wbr>archive.s<br>
> ==============================<wbr>==============================<wbr>=======<br>
> --- /dev/null<br>
> +++ lld/test/ELF/shlib-undefined-<wbr>archive.s<br>
> @@ -0,0 +1,18 @@<br>
> +# REQUIRES: x86<br>
> +<br>
> +# Undefined symbols in a DSO should pull out object files from archives<br>
> +# to resolve them.<br>
> +<br>
> +# RUN: echo '.globl foo' | llvm-mc -filetype=obj -triple=x86_64-linux-gnu -o %t1.o -<br>
> +# RUN: ld.lld -shared -o %t.so %t1.o<br>
> +<br>
> +# RUN: llvm-mc -filetype=obj -triple=x86_64-linux-gnu -o %t2.o %s<br>
> +# RUN: llvm-ar cru %t.a %t2.o<br>
> +# RUN: ld.lld -o %t.exe %t.a %t.so<br>
> +# RUN: llvm-nm -D %t.exe | FileCheck %s<br>
> +<br>
> +# CHECK: T foo<br>
> +<br>
> +.globl foo<br>
> +foo:<br>
> +  ret<br>
> Index: lld/ELF/SymbolTable.cpp<br>
> ==============================<wbr>==============================<wbr>=======<br>
> --- lld/ELF/SymbolTable.cpp<br>
> +++ lld/ELF/SymbolTable.cpp<br>
> @@ -599,9 +599,14 @@<br>
>    for (InputFile *F : SharedFiles) {<br>
>      for (StringRef U : cast<SharedFile<ELFT>>(F)-><wbr>getUndefinedSymbols()) {<br>
>        Symbol *Sym = find(U);<br>
> -      if (!Sym || !Sym->isDefined())<br>
> +      if (!Sym)<br>
>          continue;<br>
> -      Sym->ExportDynamic = true;<br>
> +      if (auto *L = dyn_cast<Lazy>(Sym))<br>
> +        if (InputFile *File = L->fetch())<br>
> +          addFile<ELFT>(File);<br>
> +<br>
> +      if (Sym->isDefined())<br>
> +        Sym->ExportDynamic = true;<br>
>      }<br>
>    }<br>
>  }<br>
><br>
><br>
> Index: lld/test/ELF/shlib-undefined-<wbr>archive.s<br>
> ==============================<wbr>==============================<wbr>=======<br>
> --- /dev/null<br>
> +++ lld/test/ELF/shlib-undefined-<wbr>archive.s<br>
> @@ -0,0 +1,18 @@<br>
> +# REQUIRES: x86<br>
> +<br>
> +# Undefined symbols in a DSO should pull out object files from archives<br>
> +# to resolve them.<br>
> +<br>
> +# RUN: echo '.globl foo' | llvm-mc -filetype=obj -triple=x86_64-linux-gnu -o %t1.o -<br>
> +# RUN: ld.lld -shared -o %t.so %t1.o<br>
> +<br>
> +# RUN: llvm-mc -filetype=obj -triple=x86_64-linux-gnu -o %t2.o %s<br>
> +# RUN: llvm-ar cru %t.a %t2.o<br>
> +# RUN: ld.lld -o %t.exe %t.a %t.so<br>
> +# RUN: llvm-nm -D %t.exe | FileCheck %s<br>
> +<br>
> +# CHECK: T foo<br>
> +<br>
> +.globl foo<br>
> +foo:<br>
> +  ret<br>
> Index: lld/ELF/SymbolTable.cpp<br>
> ==============================<wbr>==============================<wbr>=======<br>
> --- lld/ELF/SymbolTable.cpp<br>
> +++ lld/ELF/SymbolTable.cpp<br>
> @@ -599,9 +599,14 @@<br>
>    for (InputFile *F : SharedFiles) {<br>
>      for (StringRef U : cast<SharedFile<ELFT>>(F)-><wbr>getUndefinedSymbols()) {<br>
>        Symbol *Sym = find(U);<br>
> -      if (!Sym || !Sym->isDefined())<br>
> +      if (!Sym)<br>
>          continue;<br>
> -      Sym->ExportDynamic = true;<br>
> +      if (auto *L = dyn_cast<Lazy>(Sym))<br>
> +        if (InputFile *File = L->fetch())<br>
> +          addFile<ELFT>(File);<br>
> +<br>
> +      if (Sym->isDefined())<br>
> +        Sym->ExportDynamic = true;<br>
>      }<br>
>    }<br>
>  }<br>
</div></div></blockquote></div><br></div>