Commit Graph

549 Commits

Author SHA1 Message Date
Sam Clegg 7e7566323d toString function take a const refs where possible
Differential Revision: https://reviews.llvm.org/D40824

llvm-svn: 319787
2017-12-05 16:50:46 +00:00
Peter Smith 732cd8cbef [ELF] Implement scanner for Cortex-A53 Erratum 843419
Add a new file AArch64ErrataFix.cpp that implements the logic to scan for
the Cortex-A53 Erratum 843419. This involves finding all the executable
code, disassembling the instructions that might trigger the erratum and
reporting a message if the sequence is detected.

At this stage we do not attempt to fix the erratum, this functionality
will be added in a later patch. See D36749 for proposal.

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

llvm-svn: 319780
2017-12-05 15:59:05 +00:00
Rui Ueyama be61cc90c4 Make the behavior of the -v option more closer to GNU linkers.
Previously, lld exited with an error status if the only option given to
the command was -v. GNU linkers gracefully exit in that case. This patch
makes lld behave like GNU.

Note that even with this patch, lld's -v and --version options behave
slightly differently than GNU linkers' counterparts. For example,
if you run `ld.bfd -v -v`, the version string is printed out twice.
But that is an edge case that I don't think we need to take care of.

Fixes https://bugs.llvm.org/show_bug.cgi?id=31582

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

llvm-svn: 319717
2017-12-05 00:03:41 +00:00
Rafael Espindola 477ff12411 Error on -no-define-common if the output is not relocatable.
The ELF spec says

Symbols with section index SHN_COMMON may appear only in relocatable
objects.

Currently lld can produce file that break that requirement.

llvm-svn: 319473
2017-11-30 20:46:33 +00:00
Rui Ueyama 2017d52b54 Move Memory.{h,cpp} to Common.
Differential Revision: https://reviews.llvm.org/D40571

llvm-svn: 319221
2017-11-28 20:39:17 +00:00
Rui Ueyama 3e03944f02 Factor out more code to Common/Args.cpp.
Differential Revision: https://reviews.llvm.org/D40540

llvm-svn: 319211
2017-11-28 19:58:45 +00:00
Peter Smith 57eb046984 [ELF] Read ARM BuildAttributes section to determine supported features.
lld assumes some ARM features that are not available in all Arm
processors. In particular:
- The blx instruction present for interworking.
- The movt/movw instructions are used in Thunks.
- The J1=1 J2=1 encoding of branch immediates to improve Thumb wide
  branch range are assumed to be present.

This patch reads the ARM Attributes section to check for the
architecture the object file was compiled with. If none of the objects
have an architecture that supports either of these features a warning
will be given. This is most likely to affect armv6 as used in the first
Raspberry Pi.

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

llvm-svn: 319169
2017-11-28 13:51:48 +00:00
Alexander Richardson 1de78471f5 [ELF] Fall back to search dirs for linker scripts specified with -T
Summary:
This matches the behaviour of ld.bfd:
https://sourceware.org/binutils/docs/ld/Options.html#Options

If scriptfile does not exist in the current directory, ld looks for it in
the directories specified by any preceding '-L' options. Multiple '-T'
options accumulate.

Reviewers: ruiu, grimar

Reviewed By: ruiu, grimar

Subscribers: emaste, llvm-commits

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

llvm-svn: 318655
2017-11-20 15:43:20 +00:00
Rafael Espindola 37a575f8b6 Delete dead code.
llvm-svn: 317633
2017-11-07 23:12:41 +00:00
Peter Collingbourne 6c55a70838 ELF: Remove DefinedCommon.
Common symbols are now represented with a DefinedRegular that points
to a BssSection, even during symbol resolution.

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

llvm-svn: 317447
2017-11-06 04:33:58 +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
George Rimar ddd2424929 [ELF] - Fix error reporting with --strip-debug/--strip-all.
Currently LLD tries to use information about functions and variables location
taking it from debug sections. When --strip-* is given we discard such sections
and that breaks error reporting.
Patch stops discarding such sections and just removes them from InputSections list.

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

llvm-svn: 317405
2017-11-04 08:20:30 +00:00
Petr Hosek 8c7e8cce99 [ELF] Support expressions with -defsym option
Fixes PR34948.

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

llvm-svn: 317396
2017-11-04 02:03:58 +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 700b1f8a56 Add --no-omagic and --no-print-gc-sections.
llvm-svn: 317068
2017-11-01 02:04:43 +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
Oleg Ranevskyy 0cf24ed9aa [lld] Fix --exclude-libs broken when --whole-archive is used
Summary:
**Problem**
`--exclude-libs` does not work for static libraries affected by the `--whole-archive` option.

**Description**
`--exclude-libs` creates a list of static library paths and does library lookups in this list.
`--whole-archive` splits the static libraries that follow it into separate objects. As a result, lld no longer sees static libraries among linked files and does no `--exclude-libs` lookups.

**Solution**
The proposed solution is to make `--exclude-libs` consider object files too. When lld finds an object file it checks whether this file originates from an archive and, if so, looks the archive up in the `--exclude-libs` list.

Reviewers: ruiu, rafael

Reviewed By: ruiu

Subscribers: asl, ikudrin, llvm-commits, emaste

Tags: #lld

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

llvm-svn: 316998
2017-10-31 13:51:06 +00:00
Peter Collingbourne 5c54f15c55 ELF: Add support for emitting dynamic relocations in the Android relocation packing format.
The Android relocation packing format is a more compact
format for dynamic relocations in executables and DSOs
that is based on delta encoding and SLEBs. An overview
of the format can be found in the Android source code:
https://android.googlesource.com/platform/bionic/+/refs/heads/master/tools/relocation_packer/src/delta_encoder.h

This patch implements relocation packing using that format.

This implementation uses a more intelligent algorithm for compressing
relative relocations than Android's own relocation packer. As a
result it can generally create smaller relocation sections than
that packer. If I link Chromium for Android targeting ARM32 I get a
.rel.dyn of size 174693 bytes, as compared to 371832 bytes with gold
and the Android packer.

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

llvm-svn: 316775
2017-10-27 17:49:40 +00:00
Bob Haarman b8a59c8aa5 [lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.

Reviewers: ruiu

Reviewed By: ruiu

Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits

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

llvm-svn: 316624
2017-10-25 22:28:38 +00:00
George Rimar 9814d15136 [ELF] - Implement --orphan-handling option.
It is PR34946.

Spec (http://man7.org/linux/man-pages/man1/ld.1.html) tells about
--orphan-handling=MODE, option where MODE can be one of four:
"place", "discard", "warn", "error".
Currently we already report orphans when -verbose given,
what becomes excessive with option implemented.

Patch stops reporting orphans when -versbose is given,
and support "place", "warn" and "error" modes.
It is not yet clear that "discard" mode is useful so it is not supported.

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

llvm-svn: 316583
2017-10-25 15:20:30 +00:00
Rui Ueyama ac7aaeb770 Use ArgList::hasFlag().
I just didn't know that ArgList had that function.

llvm-svn: 316499
2017-10-24 20:59:55 +00:00
Konstantin Zhuravlyov e7f1734f1a LLD/ELF: Allow targets to set e_flags
Differential Revision: https://reviews.llvm.org/D39139

llvm-svn: 316460
2017-10-24 17:01:40 +00:00
Bob Haarman 4f5c8c29ac [lld] Move Threads to Common
Summary:
This will allow using the functionality from other linkers. It is also
a prerequisite for sharing the error logging code.

Reviewers: ruiu

Reviewed By: ruiu

Subscribers: emaste, mgorny, llvm-commits

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

llvm-svn: 315725
2017-10-13 18:22:55 +00:00
Rui Ueyama 2b714b56a9 Split decompressAndMergeSection into two separate functions.
Even though they are called sequentially, they are separate
operations, so it is better to split it.

llvm-svn: 315422
2017-10-11 03:12:53 +00:00
Rui Ueyama ac27de9dc7 Remove ScriptConfiguration class and move the members to LinkerScript class.
ScriptConfiguration was a class to contain parsed results of
linker scripts. LinkerScript is a class to interpret it.

That ditinction was needed because we haven't instantiated
LinkerScript early (because, IIRC, LinkerScript class was a
ELFT template function). So, when we parse linker scripts,
we couldn't directly store the result to a LinkerScript instance.

Now, that limitation is gone. We instantiate LinkerScript
at the very beginning of our main function. We can directly
store parse results to a LinkerScript instance.

llvm-svn: 315403
2017-10-11 01:19:33 +00:00
Rui Ueyama a1b79dff2a Handle input section liveness only in MarkLive.cpp.
The condition whether a section is alive or not by default
is becoming increasingly complex, so the decision of garbage
collection is spreading over InputSection.h and MarkLive.cpp,
which is not a good state.

This moves the code to MarkLive.cpp, to keep the file the central
place to make decisions about garbage collection.

llvm-svn: 315384
2017-10-10 22:59:32 +00:00
James Henderson b5ca92ef73 [ELF] Set Dot initially to --image-base value when using linker scripts
When parsing linker scripts, LLD previously started with a '.' value of 0,
regardless of the internal default image base for the target, and regardless of
switches such as --image-base. It seems reasonable to use a different image base
value when using linker scripts and --image-base is specified, since otherwise the
switch has no effect. This change does this, as well as removing unnecessary
initialisation of Dot where it is not used.

The default image base should not be used when processing linker
scripts, because this will change the behaviour for existing linker script users,
and potentially result in invalid output being produced, as a subsequent assignment
to Dot could move the location counter backwards. Instead, we maintain the existing
behaviour of starting from 0 if --image-base is not specified.

Reviewers: ruiu

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

llvm-svn: 315293
2017-10-10 10:09:35 +00:00
Peter Collingbourne d6924d7474 Reland r315114, "ELF: Export preempted symbols even if there is a dynamic list." which was reverted in r315116.
I hadn't synced past the change that changed the default hash style
to --hash-style=both, so my test had the symbols in the wrong order.

llvm-svn: 315119
2017-10-06 22:09:03 +00:00
Peter Collingbourne accab5908d Revert r315114, "ELF: Export preempted symbols even if there is a dynamic list."
For some reason the symbols get emitted in the wrong order on one of the
buildbots: http://bb9.pgr.jp/#builders/15/builds/180

llvm-svn: 315116
2017-10-06 21:48:39 +00:00
Peter Collingbourne 5a8928c605 ELF: Export preempted symbols even if there is a dynamic list.
Dynamic lists in an executable are additive, not restrictive, so we
must continue to export preempted symbols even with a dynamic list.

This fixes sanitizer interception of libc symbols (and should also fix
symbol preemption by users of sanitizers).

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

llvm-svn: 315114
2017-10-06 21:33:59 +00:00
George Rimar d46753e421 [ELF] - Do --hash-style=both by default.
Its PR34712,

GNU linkers recently changed default values to "both" of "sysv".
Patch do the same for all targets except MIPS, where .gnu.hash
section is not yet supported.

Code suggested by Rui Ueyama.

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

llvm-svn: 315051
2017-10-06 09:37:44 +00:00
Rui Ueyama 945cd64471 Wait for all threads to terminate before exitting.
I think it is not defined what would happen to detached threads
when the main thread tries to exit. That means it was not guaranteed
that unlinkAsync correctly removes a temporary file. It was also
reported that this unlinkAsync caused a crash on Windows.

This patch adds a few new functions so that the main thread always
waits for non-main threads before exitting.

I don't actually like the new two functions, runBackground and
waitForBackgroundThreads, because it looks like it is a bit
overdesigned. After all, what we are doing with these functions
is to just remove a file.

An alternative would be to do fork(2) and make the child process
remove a file asynchronously. However, it has its own problems.
Correctly forking and reclaiming a resource using waitpid(2) is not
doable unless we know our process-wide settings (such as signal mask),
but we can't make any assumption on it when lld is embedded to other
process. So I chose to stick with threads instead of multi-processes.

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

llvm-svn: 315033
2017-10-05 23:01:11 +00:00
Rui Ueyama 7d1f5fd840 Call exitLld() from elf::link.
We used to call exitLld() from a leaf function, Writer::run(), because
we had objects on the stack whose dtors are expensive. Now we no longer
have such objects on the stack, so there's no reason to exist from the
leaf function.

llvm-svn: 314869
2017-10-04 00:50:11 +00:00
Rui Ueyama a215e7cbe8 Move fetchIfLazy up so that the following comment makes sense.
We have this comment in LinkerDriver::link

  After this, no new names except a few linker-synthesized ones
  will be added to the symbol table.

but that was not true because new symbols could be added by processing
the -u option.

llvm-svn: 314842
2017-10-03 20:45:09 +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 3f851704c1 Move new lld's code to Common subdirectory.
New lld's files are spread under lib subdirectory, and it isn't easy
to find which files are actually maintained. This patch moves maintained
files to Common subdirectory.

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

llvm-svn: 314719
2017-10-02 21:00:41 +00:00
Simon Atanasyan 649e4d328f [MIPS] Fix PLT entries generation in case of linking regular and microMIPS code
Currently LLD calls the `isMicroMips` routine to determine type of PLT entries
needs to be generated: regular or microMIPS. This routine checks ELF
header flags in the `FirstObj` to retrieve type of linked object files.
So if the first file does not contain microMIPS code, LLD will generate
PLT entries with regular (non-microMIPS) code only.

Ideally, if a PLT entry is referenced by microMIPS code only this entry
should contain microMIPS code, if a PLT entry is referenced by regular
code this entry should contain regular code. In a "mixed" case the PLT
entry can be either microMIPS or regular, but each "cross-mode-call" has
additional cost.

It's rather difficult to implement this ideal solution. But we can
assume that if there is an input object file with microMIPS code, the
most part of the code is microMIPS too. So we need to deduce type of PLT
entries based on finally calculated ELF header flags and do not check
only the first input object file.

This change implements this.
  - The `getMipsEFlags` renamed to the `calcMipsEFlags`. The function
    called from the `LinkerDriver::link`. Result is stored in
    the Configuration::MipsEFlags field.
  - The `isMicroMips` and `isMipsR6` routines access the `MipsEFlags`
    field to get and check calculated ELF flags.
  - New types of PLT records created when necessary.

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

llvm-svn: 314675
2017-10-02 14:56:41 +00:00
Ben Dunbobbin 73eabf23a4 [ELF] Simpler scheme for handling common symbols
Convert all common symbols to regular symbols after scan.
This means that the downstream code does not to handle common symbols as a special case.

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

llvm-svn: 314495
2017-09-29 09:08:26 +00:00
Evgeny Mankov 31fef4d3f0 [ELF][fix] Using statically linked lld leads to segmentation fault on linking ELF
[Synopsys]
Using function elf::link(...) leads to segmentation fault on its second call. First call finishes correctly.
[Solution]
Clear the rest of globals.

Reviewed by: George Rimar and Rui Ueyama

Differential Revision: http://reviews.llvm.org/D38131

llvm-svn: 314108
2017-09-25 14:42:15 +00:00
Rafael Espindola 9e50291065 Include thin --whole-archive members in --reproduce.
We were only including the thin archive members used via a lazy symbol
before.

llvm-svn: 313832
2017-09-20 22:59:50 +00:00
George Rimar 696a7f9ac6 [ELF] - Introduce std::vector<InputFile *> global arrays.
This patch removes lot of static Instances arrays from different input file 
classes and introduces global arrays for access instead. Similar to arrays we
have for InputSections/OutputSectionCommands.

It allows to iterate over input files in a non-templated code.

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

llvm-svn: 313619
2017-09-19 09:20:54 +00:00
Rafael Espindola e05e2f8b34 Keep some relocations with undefined weak symbols.
This fixes pr34301.

As the bug points out, we want to keep some relocations with undefined
weak symbols. This means that we cannot always claim that these
symbols are not preemptible as we do now.

Unfortunately, we cannot also just always claim that they are
preemptible. Doing so would, for example, cause us to try to create a
plt entry when we don't even have a dynamic symbol table.

What almost works is to say that weak undefined symbols are
preemptible if and only if we have a dynamic symbol table. Almost
because we don't want to fail the build trying to create a copy
relocation to a weak undefined.

llvm-svn: 313372
2017-09-15 18:05:02 +00:00
Martell Malone 894dbbe8eb LLD: Introduce a GNU LD style driver for COFF
When building COFF programs many targets such as mingw prefer
to have a gnu ld frontend. Rather then having a fully fledged
standalone driver we wrap a shim around the LINK driver.

Extra tests were provided by mstorsjo

Reviewers: mstorsjo, ruiu

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

llvm-svn: 312926
2017-09-11 17:02:59 +00:00
Rui Ueyama e2f52eb22f Add -no-gdb-index which negates -gdb-index option.
llvm-svn: 312753
2017-09-07 22:40:54 +00:00
Rui Ueyama 38eab87671 Add -no-eh-frame-hdr which negates -eh-frame-hdr.
Note that ld.bfd and ld.gold have the option.

llvm-svn: 312010
2017-08-29 16:53:24 +00:00
Rui Ueyama 00cf731ecd Make empty arguments visible in error messages.
Before this patch, lld printed out something like

  error: -O: number expected, but got

After this patch, it prints out the same error message like this:

  error: -O: number expected, but got ''

Fixes https://bugs.llvm.org/show_bug.cgi?id=34311

llvm-svn: 311681
2017-08-24 18:34:32 +00:00
George Rimar 51d193f8b3 [ELF] - Fix for "Bug 34238 - LTO is optimizing away symbols referenced from linker scripts"
Code suggested by Rui Ueyama in PR34238 comments.

Previously LTO optimized away symbols referenced from linker script
because did not see that them are used from regular objects.

Patch adds such symbols as undefined earlier, before running LTO,
what sets IsUsedInRegularObj for them and fixes the issue.

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

llvm-svn: 311534
2017-08-23 08:37:22 +00:00
Davide Italiano aa33e74f4b [ELF/LTO] -function-sections/-data-sections are exact options.
Also add a test. This fixes r310995 and PR34200.

llvm-svn: 310998
2017-08-16 08:21:04 +00:00
George Rimar d6d24ac437 [ELF] - Ignore -plugin-opt=-function-sections/-plugin-opt=-data-sections. NFC.
Clang passes this options to linker. We should ignore them,
anyways they are always enabled by default atm.

Fixes https://bugs.llvm.org/show_bug.cgi?id=34200.

llvm-svn: 310995
2017-08-16 07:25:09 +00:00