<div dir="ltr"><div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Mar 14, 2019 at 5:58 PM Rui Ueyama <<a href="mailto:ruiu@google.com">ruiu@google.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr">On Thu, Mar 14, 2019 at 9:45 AM bd1976 llvm via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">On Thu, Mar 14, 2019 at 3:32 PM Peter Smith <<a href="mailto:peter.smith@linaro.org" target="_blank">peter.smith@linaro.org</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hello,<br>
<br>
I've put some comments on the proposal inline. Having to had to debug<br>
library selection problems where all the libraries are visible on the<br>
linker command line, I would prefer if people didn't embed difficult<br>
to find directives in object files, but I'm guessing in some languages<br>
this is the natural way of adding libraries.<br>
<br>
On Thu, 14 Mar 2019 at 13:08, bd1976 llvm via llvm-dev<br>
<<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br>
><br>
> At Sony we offer autolinking as a feature in our ELF toolchain. We would like to see full support for this feature upstream as there is anecdotal evidence that it would find use beyond Sony.<br>
><br>
<br>
I've not got any use of the existing code. Personally I've not come<br>
across anyone wanting this type of feature, but that is also anecdotal<br>
on my part.<br>
<br>
><br>
> For ELF we need limited autolinking support. Specifically, we only need support for "comment lib" pragmas (<a href="https://docs.microsoft.com/en-us/cpp/preprocessor/comment-c-cpp?view=vs-2017" rel="noreferrer" target="_blank">https://docs.microsoft.com/en-us/cpp/preprocessor/comment-c-cpp?view=vs-2017</a>) in C/C++ e.g. #pragma comment(lib, "foo"). My suggestion that we keep the implementation as lean as possible.<br>
><br>
> Principles to guide the implementation:<br>
> - Developers should be able to easily understand autolinking behavior.<br>
> - Developers should be able to override autolinking from the linker command line.<br>
> - Inputs specified via pragmas should be handled in a general way to allow the same source code to work in different environments.<br>
><br>
> I would like to propose that we focus on autolinking exclusively and that we divorce the implementation from the idea of "linker options" which, by nature, would tie source code to the vagaries of particular linkers. I don't see much value in supporting other linker operations so I suggest that the binary representation be a mergable string section (SHF_MERGE, SHF_STRINGS), called .autolink, with custom type SHT_LLVM_AUTOLINK (0x6fff4c04), and SHF_EXCLUDE set (to avoid the contents appearing in the output). The compiler can form this section by concatenating the arguments of the "comment lib" pragmas in the order they are encountered. Partial (-r, -Ur) links can be handled by concatenating .autolink sections with the normal mergeable string section rules. The current .linker-options can remain (or be removed); but, "comment lib" pragmas for ELF should be lowered to .autolink not to .linker-options. This makes sense as there is no linker option that "comment lib" pragmas map directly to. As an example, #pragma comment(lib, "foo") would result in:<br>
><br>
> .section ".autolink","eMS",@llvm_autolink,1<br>
>         .asciz "foo"<br>
><br>
> For LTO, equivalent information to the contents of a the .autolink section will be written to the IRSymtab so that it is available to the linker for symbol resolution.<br>
><br>
<br>
I'm not sure I understand the bit about "for symbol resolution". I<br>
think that what you mean is that you will encode the autolink section<br>
using symbols instead of as a section, and the linker is expected to<br>
extract this when it reads the symbol table?<br>
<br></blockquote><div><br></div><div>Whoops... might have used a bit of a colloquialism there; sorry. All I mean is that there will be a method on the IRSymtab that LLD can use to retrieve the same set of strings that would be written into the the .autolink section of the relocatable object files by the backend.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
> The linker will process the .autolink strings in the following way:<br>
><br>
> 1. Inputs from the .autolink sections of a relocatable object file are added when the linker decides to include that file (which could itself be in a library) in the link. Autolinked inputs behave as if they were appended to the command line as a group after all other options. As a consequence the set of autolinked libraries are searched last to resolve symbols.<br>
> 2. It is an error if a file cannot be found for a given string.<br>
> 3. Any command line options in effect at the end of the command line parsing apply to autolinked inputs, e.g. --whole-archive.<br>
<br>
I've not got any experience of autolinking as a user, so I'm<br>
struggling a bit with this one. I'm guessing that autolinking is<br>
useful because someone can do the equivalent of #include <library.h><br>
and #pragma comment lib "library.so" in the same place without having<br>
to fight the build system. </blockquote><div><br></div><div>Right. Consider that many codebases have multiple build configurations and the linker needs to be given the correct version of a library to use for the particular build configuration. This is often easier to do using the preprocessor than in the build system. Also, if a program is dependent on an external library, autolinking allows the library writer to reorganize how that library is structured transparently to the users of the library. There are notes about utility in <a href="https://stackoverflow.com/questions/1685206/pragma-commentlib-xxx-lib-equivalent-under-linux" target="_blank">https://stackoverflow.com/questions/1685206/pragma-commentlib-xxx-lib-equivalent-under-linux</a> and <a href="https://stackoverflow.com/questions/3851956/whats-pragma-comment-lib-lib-glut32-lib?noredirect=1&lq=1" target="_blank">https://stackoverflow.com/questions/3851956/whats-pragma-comment-lib-lib-glut32-lib?noredirect=1&lq=1</a>.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">I'm less convinced about --whole-archive as<br>
I think this tends to be a way of structuring the build and would be<br>
best made explicit in the build system. Moreover, what if someone<br>
wants to not use --whole-archive, for their autolink, but one already<br>
exists. </blockquote><div><br></div><div>Then they can specify --no-whole-archive on the end of the command line, no?</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">This could be quite difficult to check with a large project.<br>
Personally I'd have the user be explicit in the .autolink whether they<br>
were intending it to be whole-archive or not.<br></blockquote><div><br></div><div>I was hoping to avoid this as I want to avoid getting into how to specify linker specific options in the frontend. If we dislike the idea that the state of the command line parser at the end of the linker command line affects the autolinked libraries then I would rather go for a scheme in which the default state of the command line parser applies when linking the autolinked libraries; however, that seems harder to implement in LLD and gives the user less control over autolinking.</div></div></div></div></div></div></blockquote><div><br></div><div>I think that handling .autolink'ed files in the default state is simpler, and it doesn't seem too hard to implement.</div></div></div></blockquote><div><br></div><div>Right.. definitely possible to implement. So the trade offs are that it is possibly confusing if options like --whole-archive start applying to the "invisible" autolinked inputs. OTOH why not allow command line options to affect the autolinked inputs? It gives developers some more control at no cost (apart form the possible confusion).</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><div><br></div><div>The other option is to handle autolinked libraries as soon as we find them, so that if foo.o autolinks libbar, the linker would act as if foo.o in the command line is followed by -lbar. I'd think that's not too bad or arguably more straightforward semantics than autolinking everything all at once at the end.</div></div></div></blockquote><div><br></div><div>So I played around with this idea a bit. Some background info:</div><div><br></div><div><div>MSVC searches libraries added via "comment lib" pragmas last, after searching all of the libraries specified on the command line; however, symbols that are unresolved when bringing in an object file from a library are searched for in that library first (<a href="https://docs.microsoft.com/en-us/cpp/build/reference/link-input-files?view=vs-2017">https://docs.microsoft.com/en-us/cpp/build/reference/link-input-files?view=vs-2017</a>).</div><div><br></div><div>In the upstream discussion for autolinking, Cary Coutant offered the following as a good compromise for traditional ELF linkers (<a href="http://lists.llvm.org/pipermail/llvm-dev/2018-January/120382.html.">http://lists.llvm.org/pipermail/llvm-dev/2018-January/120382.html.</a>):</div><div><br></div><div>"""I think what would work is to insert each requested object or shared</div><div>library into the link order immediately after the object that requests</div><div>it, but only if the object hasn't already been inserted and isn't</div><div>already listed on the command line (i.e., we won't try to load the</div><div>same file twice); and to search each requested archive library</div><div>immediately after each object that requests it (of course, because of</div><div>how library searching works, we would load a given archive member once</div><div>at most). With this method, libm would be searched after both a.o and</div><div>b.o, so we'd load any members needed by a.o before b.o, and any</div><div>remaining members needed by b.o before c.o."""</div></div><div><br></div><div>The problem with what your suggesting is that with the GNU linkers it is always possible to define "where" in the command line parsing you are. However for MSVC or LLD it is not always possible.. think of a object file in a library that autolinks foo.a that gets pulled into the link (by a undefined symbol) much later on in the link order. My RFC is careful to try to set out a scheme that all linkers can implement (as much as is possible).</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
> 4. Duplicate autolinked inputs are ignored.<br>
<br>
If we take the issue of --whole-archive off the table does it matter<br>
that there are duplicate libraries? Unresolved symbols will match<br>
against the first library.</blockquote><div><br></div><div>It doesn't matter for libraries in LLD; but, it is important for object files. I think that this mechanism should be usable for object files an libraries. This is common in ELF linkers - for example the --library command line option can be used to link object files. </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"></blockquote></div></div></div></div></div></blockquote><div><br></div><div>Do you actually often link .o file using -l? It seems a bit weird use of the option. To me, it seems better to limit the ability of autolinking to link against .so or .a.</div><div><br></div></div></div></blockquote><div><br></div><div>I don't personally but it does seem useful to be able to find .o files on the library search paths.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><div></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> I guess it might make a difference if this<br>
feature is implemented in ld.lld and ld.gold, where you'd have to wrap<br>
the libraries in a start-group, end-group, but is this likely to<br>
happen?<br></blockquote><div><br></div><div>I would like the design to be such that it could be implemented by GNU.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
> 5. The linker tries to add a library or relocatable object file from each of the strings in a .autolink section by; first, handling the string as if it was specified on the commandline; second, by looking for the string in each of the library search paths in turn; third, by looking for a lib<string>.a or lib<string>.so (depending on the current mode of the linker) in each of the library search paths.<br>
<br>
There is some precedent for including files and libraries from<br>
linkerscripts <a href="https://sourceware.org/binutils/docs/ld/File-Commands.html#File-Commands" rel="noreferrer" target="_blank">https://sourceware.org/binutils/docs/ld/File-Commands.html#File-Commands</a><br>
, these distinguish between "-lfile" and "file". Would this be a<br>
better fit for a ld.bfd interface compatible linker?<br>
<br></blockquote><div><br></div><div>I was hoping to avoid GNUism's and use a "general" mechanism. MSVC source code compatibility is a usecase.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
> 6. A new command line option --no-llvm-autolink will tell LLD to ignore the .autolink sections.<br>
<br>
Personally I would have thought --no-llvm-autolink would error if it<br>
found a .autolink section, on the grounds that I wanted all the<br>
libraries to be defined on the command-line or linker script rather<br>
than hidden in object files. I would have thought ignoring the<br>
autolink sections would in most cases result in undefined symbols. If<br>
there is a use case for it, perhaps --ignore-llvm-autolink.<br>
<br></blockquote><div><br></div><div>The usecase that I had in mind is that you need to override autolinking. To do so you tell the linker to ignore the embedded autolinking information and construct an equivalent command line. I think your proposed  --ignore-llvm-autolink is a better name for this option given the intended semantics.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
> Rationale for the above points:<br>
><br>
> 1. Adding the autolinked inputs last makes the process simple to understand from a developers perspective. All linkers are able to implement this scheme.<br>
> 2. Error-ing for libraries that are not found seems like better behavior than failing the link during symbol resolution.<br>
> 3. It seems useful for the user to be able to apply command line options which will affect all of the autolinked input files. There is a potential problem of surprise for developers, who might not realize that these options would apply to the "invisible" autolinked input files; however, despite the potential for surprise, this is easy for developers to reason about and gives developers the control that they may require.<br>
> 4. Unlike on the command line it is probably easy to include the same input file twice via pragmas and might be a pain to fix; think of Third-party libraries supplied as binaries.<br>
> 5. This algorithm takes into account all of the different ways that ELF linkers find input files. The different search methods are tried by the linker in most obvious to least obvious order.<br>
> 6. I considered adding finer grained control over which .autolink inputs were ignored (e.g. MSVC has /nodefaultlib:<library>); however, I concluded that this is not necessary: if finer control is required developers can recreate the same effect autolinking would have had using command line options.<br>
><br>
> Thoughts?<br>
><br>
> _______________________________________________<br>
> LLVM Developers mailing list<br>
> <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
> <a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div></div></div></div></div>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div></div>
</blockquote></div></div></div>