Commit Graph

217 Commits

Author SHA1 Message Date
Fangrui Song b4744d306c [ELF] Support --{,no-}allow-shlib-undefined
Summary:
In ld.bfd/gold, --no-allow-shlib-undefined is the default when linking
an executable. This patch implements a check to error on undefined
symbols in a shared object, if all of its DT_NEEDED entries are seen.

Our approach resembles the one used in gold, achieves a good balance to
be useful but not too smart (ld.bfd traces all DSOs and emulates the
behavior of a dynamic linker to catch more cases).

The error is issued based on the symbol table, different from undefined
reference errors issued for relocations. It is most effective when there
are DSOs that were not linked with -z defs (e.g. when static sanitizers
runtime is used).

gold has a comment that some system libraries on GNU/Linux may have
spurious undefined references and thus system libraries should be
excluded (https://sourceware.org/bugzilla/show_bug.cgi?id=6811). The
story may have changed now but we make --allow-shlib-undefined the
default for now. Its interaction with -shared can be discussed in the
future.

Reviewers: ruiu, grimar, pcc, espindola

Reviewed By: ruiu

Subscribers: joerg, emaste, arichardson, llvm-commits

Differential Revision: https://reviews.llvm.org/D57385

llvm-svn: 352826
2019-02-01 02:25:05 +00:00
Chandler Carruth 2946cd7010 Update the file headers across all of the LLVM projects in the monorepo
to reflect the new license.

We understand that people may be surprised that we're moving the header
entirely to discuss the new license. We checked this carefully with the
Foundation's lawyer and we believe this is the correct approach.

Essentially, all code in the project is now made available by the LLVM
project under our new license, so you will see that the license headers
include that license only. Some of our contributors have contributed
code under our old license, and accordingly, we have retained a copy of
our old license notice in the top-level files in each project and
repository.

llvm-svn: 351636
2019-01-19 08:50:56 +00:00
Fangrui Song 50394f6e01 [ELF] A shared object is needed if any of its occurrences is needed
Summary:
If a DSO appears more than once with and without --as-needed, ld.bfd and gold consider --no-as-needed to takes precedence over --as-needed. lld didn't and this patch makes it do so.

This makes it a bit away from the position-dependent behavior (how
different occurrences of the same DSO interact) and protects us from
some mysterious runtime errors: if some interceptor libraries add their
own --no-as-needed dependencies (e.g. librt.so), and the user
application specifies -Wl,--as-needed -lrt , the absence of the
DT_NEEDED entry would make dlsym(RTLD_NEXT, "clock_gettime") return NULL
and would break at runtime.

Reviewers: ruiu, espindola

Reviewed By: ruiu

Subscribers: emaste, arichardson, llvm-commits

Differential Revision: https://reviews.llvm.org/D56089

llvm-svn: 350105
2018-12-27 22:24:45 +00:00
George Rimar 94a16cb611 [ELF] - Make SymbolTable::addDefined return Defined.
Now it returns Symbol. This should be NFC that
avoids doing cast at the caller's sides.

Differential revision: https://reviews.llvm.org/D54627

llvm-svn: 347455
2018-11-22 11:40:08 +00:00
Rui Ueyama f3fad55787 Remove `Type` parameter from SymbolTable::insert(). NFC.
`Type` parameter was used only to check for TLS attribute mismatch,
but we can do that when we actually replace symbols, so we don't need
to type as an argument. This change should simplify the interface of
the symbol table a bit.

llvm-svn: 344394
2018-10-12 18:29:18 +00:00
Rui Ueyama b0d8502049 Remove SymbolTable::addAbsolute().
addAbsolute() could be implemented as a non-member function.

llvm-svn: 344305
2018-10-11 22:15:41 +00:00
Rui Ueyama 2600e0946b Rename SymbolTable::addRegular -> SymbolTable::addDefined.
We have addAbsolute, addBitcode, addCommon, etc. addRegular looked a
bit inconsistent.

llvm-svn: 344294
2018-10-11 20:43:01 +00:00
Rui Ueyama cf41adaaa0 Remove unused default arguments.
llvm-svn: 344292
2018-10-11 20:38:34 +00:00
Rui Ueyama c7497d3ac5 Remove SymbolTable::addUndefined<ELF32LE>(StringRef).
Because we can implement the function as a non-member function.

llvm-svn: 344290
2018-10-11 20:34:29 +00:00
Rui Ueyama 3c4344810a Make a member function private and rename it to avoid function overloading.
llvm-svn: 344196
2018-10-10 22:49:29 +00:00
Rui Ueyama 07b4536bb7 Change how we handle -wrap.
We have an issue with -wrap that the option doesn't work well when
renamed symbols get PLT entries. I'll explain what is the issue and
how this patch solves it.

For one -wrap option, we have three symbols: foo, wrap_foo and real_foo.
Currently, we use memcpy to overwrite wrapped symbols so that they get
the same contents. This works in most cases but doesn't when the relocation
processor sets some flags in the symbol. memcpy'ed symbols are just
aliases, so they always have to have the same contents, but the
relocation processor breaks that assumption.

r336609 is an attempt to fix the issue by memcpy'ing again after
processing relocations, so that symbols that are out of sync get the
same contents again. That works in most cases as well, but it breaks
ASan build in a mysterious way.

We could probably fix the issue by choosing symbol attributes that need
to be copied after they are updated. But it feels too complicated to me.

So, in this patch, I fixed it once and for all. With this patch, we no
longer memcpy symbols. All references to renamed symbols point to new
symbols after wrapSymbols() is done.

Differential Revision: https://reviews.llvm.org/D50569

llvm-svn: 340387
2018-08-22 07:02:26 +00:00
Rui Ueyama f43fba739c Revert r336609: Fix direct calls to __wrap_sym when it is relocated.
This reverts commit r336609 as it doesn't seem to work with AArch64
thunk creation when used with ASan.

llvm-svn: 337413
2018-07-18 18:24:46 +00:00
Rui Ueyama 3e730b8ae6 Fix direct calls to __wrap_sym when it is relocated.
Patch by Matthew Koontz!

Before, direct calls to __wrap_sym would not map to valid PLT entries,
so they would crash at runtime. This change maps such calls to the same
PLT entry as calls to sym that are then wrapped.

Differential Revision: https://reviews.llvm.org/D48502

llvm-svn: 336609
2018-07-09 22:03:05 +00:00
Rui Ueyama cc013f62c1 Make fetchIfLazy only fetch an object file. NFC.
Previously, fetchIfLazy did more than the name says. Now, setting
to UsedInRegularObj is moved to another function.

llvm-svn: 329092
2018-04-03 18:01:18 +00:00
George Rimar 1ef746ba21 [ELF] - Eliminate Lazy class.
Patch removes Lazy class which
is just an excessive layer.

Differential revision: https://reviews.llvm.org/D45083

llvm-svn: 329086
2018-04-03 17:16:52 +00:00
Rui Ueyama ee17371897 Merge {COFF,ELF}/Strings.cpp to Common/Strings.cpp.
This should resolve the issue that lld build fails in some hosts
that uses case-insensitive file system.

Differential Revision: https://reviews.llvm.org/D43788

llvm-svn: 326339
2018-02-28 17:38:19 +00:00
Rafael Espindola 3f4c673d38 Put undefined symbols from shared libraries in the symbol table.
With the recent fixes these symbols have more in common than not with
regular undefined symbols.

llvm-svn: 326242
2018-02-27 20:31:22 +00:00
Peter Collingbourne 09e04af42f ELF: Stop collecting a list of symbols in ArchiveFile.
There seems to be no reason to collect this list of symbols.

Also fix a bug where --exclude-libs would apply to all symbols that
appear in an archive's symbol table, even if the relevant archive
member was not added to the link.

Differential Revision: https://reviews.llvm.org/D43369

llvm-svn: 325380
2018-02-16 20:23:54 +00:00
George Rimar cd141ce3e3 [ELF] - Remove dead declaration. NFC.
llvm-svn: 323747
2018-01-30 11:03:27 +00:00
Rafael Espindola 9a84f6b954 Detemplate reportDuplicate.
We normally avoid "switch (Config->EKind)", but in this case I think
it is worth it.

It is only executed when there is an error and it allows detemplating
a lot of code.

llvm-svn: 321404
2017-12-23 17:21:39 +00:00
Rafael Espindola 8cd6674f5b Use a reference in addLazyArchive. NFC.
llvm-svn: 321194
2017-12-20 17:48:28 +00:00
Rafael Espindola a32ddc4639 Use a reference for the shared symbol file.
Every shared symbol has a file, so we can use a reference.

llvm-svn: 321187
2017-12-20 16:28:19 +00:00
Rafael Espindola 7b5cc6c5dc Use a reference for a value that is never null. NFC.
llvm-svn: 321186
2017-12-20 16:19:48 +00:00
Rafael Espindola f1687125ba Use a reference for a value that is never null. NFC.
llvm-svn: 321185
2017-12-20 16:16:40 +00:00
Rafael Espindola 8f619ab826 Compact symbols from 96 to 88 bytes.
By using an index instead of a pointer for verdef we can put the index
next to the alignment field. This uses the otherwise wasted area and
reduces the shared symbol size.

By itself the performance change of this is in the noise, but I have a
followup patch to remove another 8 bytes that improves performance
when combined with this.

llvm-svn: 320449
2017-12-12 01:45:49 +00:00
Rafael Espindola d26b52fd34 Remove some includes from InputFiles.h.
They were not used in InputFiles.h and it was getting too easy to add
circular includes.

llvm-svn: 320256
2017-12-09 16:56:18 +00:00
Sam Clegg a80d94d52b Rename `Symtab` private memory to avoid confusion with global `Symtab`
This is also consistent with SymVector that exists in COFF port
and soon to be added to the wasm port.

Split off as part of https://reviews.llvm.org/D40371

Differential Revision: https://reviews.llvm.org/D40525

llvm-svn: 319113
2017-11-27 23:16:06 +00:00
Rafael Espindola bec3765bea Remove IsLocal.
Since we always have Binding in the current symbol design IsLocal is
redundant.

llvm-svn: 318497
2017-11-17 01:37:50 +00:00
Rafael Espindola 99f9e1373b Simplify. NFC.
copyFrom doesn't copy the Binding, so this was a nop.

llvm-svn: 317965
2017-11-11 01:59:47 +00:00
Peter Collingbourne 38082ac62d ELF: Remove SymbolTable::SymIndex class.
The Traced flag is unnecessary because we only need to set the index
to -1 to mark a symbol for tracing.

Differential Revision: https://reviews.llvm.org/D39672

llvm-svn: 317450
2017-11-06 04:58:04 +00:00
Peter Collingbourne e9a9e0a1e7 ELF: Merge DefinedRegular and Defined.
Now that DefinedRegular is the only remaining derived class of
Defined, we can merge the two classes.

Differential Revision: https://reviews.llvm.org/D39667

llvm-svn: 317448
2017-11-06 04:35:31 +00:00
Rui Ueyama dc0b0b0df3 Rewrite addSymbolWrap and applySymbolWrap. NFCI.
r317396 changed the way how we handle the -defsym option. The option is
now handled using the infrastructure for the linker script.

We used to handle both -defsym and -wrap using the same set of functions
in the symbol table. Now, we don't need to do that.

This patch rewrites the functions so that they become more straightforward.
The new functions directly handle -wrap rather than abstract it.

llvm-svn: 317426
2017-11-04 23:09:43 +00:00
Rui Ueyama 45b81402e3 Remove dead code and update comment.
llvm-svn: 317425
2017-11-04 22:32:56 +00:00
Rui Ueyama f52496e1e0 Rename SymbolBody -> Symbol
Now that we have only SymbolBody as the symbol class. So, "SymbolBody"
is a bit strange name now. This is a mechanical change generated by

  perl -i -pe s/SymbolBody/Symbol/g $(git grep -l SymbolBody lld/ELF lld/COFF)

nd clang-format-diff.

Differential Revision: https://reviews.llvm.org/D39459

llvm-svn: 317370
2017-11-03 21:21:47 +00:00
Rui Ueyama f1f00841d9 Merge SymbolBody and Symbol into one class, SymbolBody.
SymbolBody and Symbol were separated classes due to a historical reason.
Symbol used to be a pointer to a SymbolBody, and the relationship
between Symbol and SymbolBody was n:1.

r2681780 changed that. Since that patch, SymbolBody and Symbol are
allocated next to each other to improve memory locality, and they have
1:1 relationship now. So, the separation of Symbol and SymbolBody no
longer makes sense.

This patch merges them into one class. In order to avoid updating too
many places, I chose SymbolBody as a unified name. I'll rename it Symbol
in a follow-up patch.

Differential Revision: https://reviews.llvm.org/D39406

llvm-svn: 317006
2017-10-31 16:07:41 +00:00
Rui Ueyama 7f9694a42d Pass symbol attributes instead of ElfSym to Shared symbol ctor.
This change allows us to use less templates for Shared symbol and
the functions that deals with shared symbols.

llvm-svn: 316841
2017-10-28 20:15:56 +00:00
Konstantin Zhuravlyov 3f9b4b7c91 Revert r315877: Simplify.
It causes MSVC 2015 to emit "C4307: '-': integral constant overflow" warning

Differential Revision: https://reviews.llvm.org/D38955

llvm-svn: 315926
2017-10-16 18:49:28 +00:00
Rui Ueyama 7be790b1e0 Simplify.
llvm-svn: 315877
2017-10-15 21:07:25 +00:00
Rafael Espindola 8bd0c45c7a Inline small function. NFC.
llvm-svn: 315141
2017-10-07 01:08:28 +00:00
Rafael Espindola 469350810a Refine our --wrap implementation.
Before this patch we would copy foo into real_foo and wrap_foo into
foo. The net result is that __wrap_foo shows up twice in the symbol
table.

With this patch we:

* save a copy of __real_foo before copying foo.
* drop one of the __wrap_foo from the symbol table.
* if __real_foo was not undefined, add a *new* symbol with that content to
  the symbol table.

The net result is that

Anything using foo now uses __wrap_foo
Anything using __real_foo now uses foo.
Anything using __wrap_foo still does.

And the symbol table has foo, __wrap_foo and __real_foo (if defined).

Which I think is the desired behavior.

llvm-svn: 315097
2017-10-06 20:09:34 +00:00
Rui Ueyama fa105f8deb Inline a small function.
llvm-svn: 314841
2017-10-03 20:44:54 +00:00
Igor Kudrin fb7f8befec [ELF] Avoid promoting an undefined weak entry symbol to global.
Without this patch, lld emits "error: undefined symbol: _start"
if it encountered only weak references to that symbol.

llvm-svn: 314790
2017-10-03 12:23:46 +00:00
Rui Ueyama de3d0cc894 Make parameter lists of SymbolTable::add* functions more consistent. NFC.
llvm-svn: 314591
2017-09-30 12:41:34 +00:00
Rui Ueyama bbfe33c6ba Move Config->SymbolRenaming to SymbolTable.
In order to keep track of symbol renaming, we used to have
Config->SymbolRenaming, and whether a symbol is in the map or not
affects its symbol attribute (i.e. "LinkeRedefined" bit).

This patch adds "CanInline" bit to Symbol to aggreagate symbol
information in one place and removed the member from Config since
no one except SymbolTable now uses the table.

llvm-svn: 314088
2017-09-25 00:57:18 +00:00
Rafael Espindola d72d97b3be If --dynamic-list is given, only those symbols are preemptible.
This allows combining --dynamic-list and version scripts too. The
version script controls which symbols are visible, and
--dynamic-list controls which of those are preemptible.

Unlike previous versions, undefined symbols are still considered
preemptible, which was the issue breaking the cfi tests.

This fixes pr34053.

llvm-svn: 312806
2017-09-08 18:16:59 +00:00
Rafael Espindola 0ff545c018 Revert "Revert "Revert r311468: If --dynamic-list is given, only those symbols are preemptible""
This reverts commit r312757.

Evgenii Stepanov reports that it broke some tests.

llvm-svn: 312771
2017-09-08 01:09:52 +00:00
Rafael Espindola b7d0be5896 Revert "Revert r311468: If --dynamic-list is given, only those symbols are preemptible"
If --dynamic-list is given, only those symbols are preemptible.

This allows combining --dynamic-list and version scripts too. The
version script controls which symbols are visible, and --dynamic-list
controls which of those are preemptible.

This fixes pr34053.

llvm-svn: 312757
2017-09-07 23:19:09 +00:00
Rui Ueyama e158f7c329 Revert r311468: If --dynamic-list is given, only those symbols are preemptible
This reverts commit r311468 because it broke some CFI bots.

llvm-svn: 311497
2017-08-22 21:54:58 +00:00
Rui Ueyama 9cbbacb910 If --dynamic-list is given, only those symbols are preemptible
Patch by Rafael Espíndola.

This is PR34053.

The implementation is a bit of a hack, given the precise location where
IsPreemtible is set, it cannot be used from
SymbolTable::handleAnonymousVersion.

I could add another method to SymbolTable if you think that would be
better.

Differential Revision: https://reviews.llvm.org/D36499

llvm-svn: 311468
2017-08-22 16:31:47 +00:00
Rui Ueyama 43099e4409 Remove SymbolTable::findInCurrentDSO.
This function doesn't seem to add value to the symbol table as
it is easy to write code without it.

llvm-svn: 310925
2017-08-15 16:03:11 +00:00