llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

ELF dependent-library specifier to itself in an object file causes LLD to hang

Open bd1976bris opened this issue 7 years ago • 4 comments

Bugzilla Link 42460
Version unspecified
OS All
CC @rui314,@smithp35

Extended Description

In the upstream review for ELF dependent-libraries it was decided that we only need to support libraries being specified to LLD via the dependent-libraries metadata and not object files. However, to keep the implementation simple, the upstream code does allow object files to be specified.

The simplest problem is: if an object file has a dependent-library specifier to itself the linker will loop forever (assuming that it does not fail with e.g. multiply defined symbol errors). More generally, the linker will never finish if there is a loop of dependent library specifiers in a set of object files(and again that linking those object files multiple times does not cause the link to fail for another reason).

I think this extremely unlikely to occur in real use cases as in normal use linking the same object file twice will cause a linking error.

bd1976bris avatar Jul 01 '19 10:07 bd1976bris

I like your solution 3. What would you think about introducing an option to LLD to de-duplicate input files? We can then use this code path in to fix this bug with dependent-library cycles?

Input file de-duplication has proved useful to some of our customers who have build systems that auto-magically compose the linker command line. Sometimes they find it easier to allow duplicates in the command line and then have the linker ignore any duplicates.

Update: Discussion of deduplicating inputs here: https://reviews.llvm.org/D88944

bd1976bris avatar Jul 02 '19 22:07 bd1976bris

This is an interesting problem. One thing I'd like to point out first is that lld doesn't do anything to de-duplicate files that are given via the command line. If an object file is specified twice on the command line, the file is linked just twice as if they were two different files. The same is true to DSOs. So, if we want to avoid an infinite loop, lld needs to behave differently for dependent-libraries.

There are many different ways to solve it:

  1. Do nothing -- this does not really solve the problem, but we can say that it is a user's responsibility to not create an infinite loop in a dependency graph.

  2. Detect a circle in a dependency graph. In order to do that, we need to keep track which dependent-library is loaded from which file. That might be too much.

  3. Load a dependent-library only once if a file is specified by a dependent-library specifier more than once. In order to do that, we need to maintain a set of pathnames that are added to an input file list by a dependent-library feature. Maybe this is the easiest solution?

rui314 avatar Jul 02 '19 09:07 rui314

I experimented with an approach of ignoring object files when matching a dependent library specifier to a file. This involves opening the file and checking if it is a library using the magic bytes for library files. However, this presents a problem of what to do if the file cannot be read (and therefore can't be identified). This simply doesn't feel like a natural fit for the way that LLD works.

Another approach would be to allow dependent library specifiers to match object files; but, to deduplicate object files that are added via dependent library specifiers. IMO this is a more natural fit for LLD and is similar to how the Microsoft linker (which inspired the dependent-library feature) works.

bd1976bris avatar Jul 01 '19 10:07 bd1976bris

This problem is arguably user error. It can be diagnosed from the --verbose output and it can be worked around by adding a dangling --start-lib option to LLD's command line (dangling options apply to the auto-linked files), so that auto-linked object files are treated as if they were in an archive.

c:\temp>type foo.c
#pragma comment(lib, "foo.o")
static void foo() {}

c:\temp>ld.lld.exe -shared foo.o --verbose
ld.lld: foo.o
ld.lld: foo.o
ld.lld: foo.o
<continues forever...>

c:\temp>ld.lld.exe -shared foo.o --verbose --start-lib
ld.lld: foo.o
ld.lld: foo.o

bd1976bris avatar Feb 09 '22 12:02 bd1976bris