Commit Graph

4820 Commits

Author SHA1 Message Date
Shoaib Meenai 0c958fba14 [ELF] Only scan executables for shlib undefined symbols
If using a version script with a `local: *` in it, symbols in shared
libraries will still get default visibility if another shared library on
the link line has an undefined reference to the symbol. This is quite
surprising. Neither bfd nor gold have this behavior when linking a
shared library, and none of LLD's tests fail without this behavior, so
it seems safe to limit scanShlibUndefined to executables.

As far as executables are concerned, gold doesn't do any automatic
default visibility marking, and bfd issues a link error about a shared
library having a reference to a hidden symbol rather than silently
giving that symbol default visibility. I think bfd's behavior here is
preferable to LLD's, but that's something to be considered in a
follow-up.

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

llvm-svn: 321578
2017-12-30 08:00:44 +00:00
Rafael Espindola 465e7c94ed Allow copy relocation with -z notext.
This makes adjustExpr a bit simpler too IMHO.

It seems that some of the complication around relocation processing
is that we are trying to create copy relocations too early. It seems
we could handle a few simple cases first and continue.

llvm-svn: 321507
2017-12-28 00:23:49 +00:00
Rafael Espindola e2e070c6c4 Don't try to preempt protected symbols with -z notext.
I will send a followup patch removing the FIXME this patch adds.

llvm-svn: 321499
2017-12-27 20:53:13 +00:00
George Rimar a0ab8d7a58 [ELF] - Allow relocation to a weak undefined symbol when -z notext is given.
Previously we failed to resolve them when produced executables:
"relocation R_X86_64_32 cannot be used against shared object; recompile with -fPIC"

Patch fixes it so that we resolve them to 0 for executables. 
And for -shared case we still should produce the relocation.

This finishes fixing PR35720.

DIfferential revision: https://reviews.llvm.org/D41551

llvm-svn: 321473
2017-12-27 07:29:55 +00:00
Rui Ueyama 17a3077f59 Make it clear where is a placeholder for later binary patching.
This is an aesthetic change to represent a placeholder for later
binary patching as "0, 0, 0, 0" instead of "0x00, 0x00, 0x00, 0x00".
The former is how we represent it in COFF, and I found it easier to
read than the latter.

llvm-svn: 321471
2017-12-27 06:54:18 +00:00
Rafael Espindola 63a4a98e4c Add a comment about subtracting two non abs symbols. NFC.
llvm-svn: 321458
2017-12-26 18:11:14 +00:00
Rui Ueyama c67d6b2da0 Simplify script lexer.
Differential Revision: https://reviews.llvm.org/D41577

llvm-svn: 321453
2017-12-26 10:13:10 +00:00
Rafael Espindola a9c490b71d Allow relocations in rw sections to create plt entries.
If a relocation cannot be implemented by the dynamic linker and the
section is rw, allow creating a plt entry to use as the function
address as if the section was ro.

This matches bfd and gold. It also matches our behavior with -z
notext.

llvm-svn: 321430
2017-12-24 19:02:10 +00:00
Rafael Espindola 6a97f80755 Fix output section offset and contents when linker script uses memory region and data commands.
Advance the memory region offset when handling a linker script data
command such as BYTE or LONG.  Failure to advance the offset results
in corrupted output with overlapping sections.

Update tests to check for this combination of both a) memory regions
and b) data commands.

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

Patch by Owen Shaw!

llvm-svn: 321418
2017-12-24 03:46:35 +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
George Rimar d70da0e55f [ELF] - Fix mistype in comment. NFC.
llvm-svn: 321403
2017-12-23 16:34:58 +00:00
George Rimar 3bdf6baa25 [ELF] - Allow using PLT relocations when "-z notext" is given.
This is part of PR35720.

Currently LLD allows dynamic relocations against text when -z notext is given.
Though for non-PIC relocations like R_X86_64_PC32 that does not work,
we produce "relocation R_X86_64_PC32 cannot be used against shared object;"
error because they may overflow in runtime.

Solution implemented is to use PLT for them.

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

llvm-svn: 321400
2017-12-23 09:00:37 +00:00
Rafael Espindola 9f37543cf8 Detemplate isCompatible(). NFC.
llvm-svn: 321393
2017-12-23 00:04:34 +00:00
Rafael Espindola 9cbb6dd1fc Result of subtracting two symbols should be absolute.
When two linker script symbols are subtracted, the result should be absolute.

This is the behavior of binutils' ld.

Patch by Erick Reyes!

llvm-svn: 321390
2017-12-22 21:55:28 +00:00
Rafael Espindola b9a18fd0a2 Define isUndefWeak inline.
This small function was showing up in the profile. Defining it inline
gives about 0.3% speedup.

llvm-svn: 321317
2017-12-21 22:26:44 +00:00
Rafael Espindola a973cc2282 Call isStaticLinkTimeConstant only once per relocation.
It is a pretty expensive function. Some of the speedups:

clang: 1.92%
chrome: 1.15%
linux-kernel: 1.40%
llvm-svn: 321311
2017-12-21 21:45:35 +00:00
Rafael Espindola ce3b52c186 Pass an InputFile to the InputSection constructor.
This simplifies toRegularSection and reduces the noise in a followup
patch.

llvm-svn: 321240
2017-12-21 02:11:51 +00:00
Rafael Espindola 604032729c Convert a few more InputFiles to references.
We use null files in sections to represent linker created sections,
so ObjFile<ELFT> is never null.

llvm-svn: 321238
2017-12-21 02:03:39 +00:00
Rafael Espindola 5c73c49c9f Detemplate createCommentSection.
It was only templated so it could create a dummy section header that
was immediately parsed back.

llvm-svn: 321235
2017-12-21 01:21:59 +00:00
Rafael Espindola bb3198949f Use dyn_cast instead of dyn_cast_or_null.
There should be no null sections in InputSections.

llvm-svn: 321219
2017-12-20 20:46:08 +00:00
Rafael Espindola 920d7d80e2 clang-format. NFC.
llvm-svn: 321216
2017-12-20 19:59:47 +00:00
Rafael Espindola efb483f63a Pass a InputFile reference to the Lazy constructor. NFC.
llvm-svn: 321199
2017-12-20 18:01:32 +00:00
Rafael Espindola 8276f1bda6 Use a reference to a file in the LazyArchive symbol.
It is never null.

llvm-svn: 321198
2017-12-20 17:59:43 +00:00
Rafael Espindola 2e5c71eadc LazyObject's file is never null, use a reference.
llvm-svn: 321196
2017-12-20 17:52:36 +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
Igor Kudrin 5966d15943 [ELF] Fix an assignment command at the end of an .ARM.exidx section.
The value of the symbol in the assignment should include the sentinel entry.

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

llvm-svn: 321154
2017-12-20 08:56:10 +00:00
Rafael Espindola 092ba58bee Replace a dyn_cast_or_null with dyn_cast.
The variable being casted was accessed in the previous line.

llvm-svn: 321140
2017-12-20 01:57:19 +00:00
Rafael Espindola 1037eef8e0 Use references instead of pointers. NFC.
These values are trivially never null. While at it, also use
InputSection instead of InputSectionBase when possible.

llvm-svn: 321126
2017-12-19 23:59:35 +00:00
Rafael Espindola fc29b8dd63 Remove unnecessary this->. NFC.
llvm-svn: 321081
2017-12-19 16:29:02 +00:00
Rafael Espindola 4c74feffd4 Don't write preemptible symbol values to the .got.
It is not necessary and matches what bfd and gold do.

This was a regression from r315658.

llvm-svn: 321023
2017-12-18 20:35:15 +00:00
Shoaib Meenai 8c023a9cd9 [ELF] Fix typo in comment. NFC
llvm-svn: 321022
2017-12-18 20:33:27 +00:00
Peter Smith 3c73a41128 [ELF] Optimize Arm PLT sequences
A more efficient PLT sequence can be used when the distance between the
.plt and the end of the .plt.got is less than 128 Megabytes, which is
frequently true. We fall back to the old sequence when the offset is larger
than 128 Megabytes. This gives us an alternative to forcing the longer
entries with --long-plt as we gracefully fall back to it as needed. 

See ELF for the ARM Architecture Appendix A for details of the PLT sequence.

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

llvm-svn: 320987
2017-12-18 14:46:27 +00:00
Rui Ueyama 22e5551ed7 Do not add .dynamic entries that don't have to be added early in the ctor. NFC.
We add dynamic section entries both in the ctor of the class and
DynamicSection::finalizeContents(). Some entries need to be added early
in the ctor because they add strings to .dynstr. Other entries were
intended to be added in finalizeContents(). However, some entries are
added in the ctor even though they don't add strings. This patch
fix the issue.

llvm-svn: 320851
2017-12-15 19:39:59 +00:00
Rafael Espindola 75ebe9a3bf Handle a VersymIndex of 0 as an error.
I noticed that the continue this patch deletes was not tested. Trying
to add a test I realized that we never put a VER_NDX_LOCAL symbol in
the dynamic symbol table. There doesn't seem to be any reason for a
linker to use VER_NDX_LOCAL for a defined shared symbol.

llvm-svn: 320817
2017-12-15 14:52:40 +00:00
Peter Smith 96ca4f5e91 [ELF] Remove Duplicate .ARM.exidx sections
The ARM.exidx section contains a table of 8-byte entries with the first
word of each entry an offset to the function it describes and the second
word instructions for unwinding if an exception is thrown from that
function. The SHF_LINK_ORDER processing will order the table in ascending
order of the functions described by the exception table entries. As the
address range of an exception table entry is terminated by the next table
entry, it is possible to merge consecutive table entries that have
identical unwind instructions.

For this implementation we define a table entry to be identical if:
- Both entries are the special EXIDX_CANTUNWIND.
- Both entries have the same inline unwind instructions.
We do not attempt to establish if table entries that are references to
.ARM.extab sections are identical.

This implementation works at a granularity of a single .ARM.exidx
InputSection. If all entries in the InputSection are identical to the
previous table entry we can remove the InputSection. A more sophisticated
but more complex implementation would rewrite InputSection contents so that
duplicates within a .ARM.exidx InputSection can be merged.

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

llvm-svn: 320803
2017-12-15 11:09:41 +00:00
Peter Smith 5597bcb5fc [ELF] Fix buildbot warning error.
Forward declare InputSectionDescription as struct not class.

llvm-svn: 320801
2017-12-15 10:47:44 +00:00
Peter Smith cf354873c1 [ELF] Complete implementation of --fix-cortex-a53-843419
This patch provides the mechanism to fix instances of the instruction
sequence that may trigger the cortex-a53 843419 erratum. The fix is
provided by an alternative instruction sequence to remove one of the
erratum conditions. To reach this alternative instruction sequence we
replace the original instruction with a branch to the alternative
sequence. The alternative sequence is responsible for branching back to
the original.

As there is only erratum to fix the implementation is specific to
AArch64 and the specific erratum conditions. It should be generalizable
to other targets and erratum if needed.

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

llvm-svn: 320800
2017-12-15 10:32:34 +00:00
Rui Ueyama 29ceba7961 Fix error messages.
llvm-svn: 320772
2017-12-15 00:07:15 +00:00
Rui Ueyama fbe68a3584 Use warn() instead of error() to report a bad symbol in a DSO.
Specifically, libwidevinecdm.so in Chrome has such bad symbol.
It seems the BFD linker handles them as local symbols, so instead
of inserting them to the symbol table, we should skip them too.

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

llvm-svn: 320770
2017-12-15 00:01:33 +00:00
Igor Kudrin a569163b00 Fix a build bot failure.
llvm-svn: 320671
2017-12-14 07:28:11 +00:00
Igor Kudrin f01caab4b7 [ELF] Prevent crash in writing an .ARM.exidx sentinel entry.
We might crash in 'ARMExidxSentinelSection::writeTo()' because it expected
the sentinel entry to be put in the same 'InputSectionDescription' as
the last real entry. This assumption fails if the last output section command
for .ARM.exidx is anything but an input section description, because in this
case 'OutputSection::addSection()' creates a new 'InputSectionDescription'.

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

llvm-svn: 320668
2017-12-14 06:23:50 +00:00
Rafael Espindola b308cace84 Fix crash on invalid.
We would fail an assert if a shared library had a local symbol after
sh_info.

llvm-svn: 320667
2017-12-14 05:01:49 +00:00
Rafael Espindola f4fb5fd752 Move Repl to SectionBase.
It is currently in InputSectionBase. Only InputSections are used in
ICF, so Repl should be move to InputSection to clear the class
hierarchy or, like this patch does, to SectionBase for convenience.

The convenience of having it on the base class is that we can just
access the replacement without having to first check if it is an
InputSection. It is a bit less code and a bit faster as some of this
code is very hot.

I got up to 1.77% improvement in clang-gdb-index and no regressions
according to lnt.

llvm-svn: 320654
2017-12-13 22:59:23 +00:00
Rui Ueyama d44a81c3a8 Inline a small function.
Differential Revision: https://reviews.llvm.org/D41204

llvm-svn: 320652
2017-12-13 22:53:59 +00:00
Rafael Espindola b3973eed3c Move Repl check to a symbol method.
Having a SectionBase method check Repl is inconsistent with how we
handle other section information.

For example, if a section is replaced, Sec->Live is false and it is
natural for Sec->getOutputSection() to be null.

It is the symbol that is moved to the replacement section.

llvm-svn: 320599
2017-12-13 17:46:29 +00:00
Rafael Espindola 13dbf944d8 Simplify. NFC.
If a section has been replaced (Sec != Sec->Repl) it is not Live.

llvm-svn: 320597
2017-12-13 17:36:53 +00:00
Rafael Espindola cee2933408 Remove unnecessary use of Repl.
This runs before ICF, so Sec->Repl == Sec.

llvm-svn: 320543
2017-12-13 02:09:14 +00:00
Rafael Espindola fd901ccdda Make a method private. NFC.
llvm-svn: 320542
2017-12-13 02:00:45 +00:00
Rafael Espindola b01cd86458 Fix the type of the Discared section.
It is constructed with a kind of Regular and will dyn_cast to
InputSection, but is declared to be an InputSectionBase.

llvm-svn: 320539
2017-12-13 01:39:35 +00:00
Rui Ueyama 1ce416c635 Remove trailing whitespace.
llvm-svn: 320520
2017-12-12 20:00:30 +00:00
Rafael Espindola 10bcc1cf90 Fix line endings. NFC.
llvm-svn: 320502
2017-12-12 17:37:01 +00:00
Peter Smith 2128df7e7b [ELF] Refactor to remove loop copying all Sections in OS->finalize() [NFC]
Moving the SHF_LINK_ORDER processing out of OutputSection::finalize()
means that we no longer need to copy all InputSections as we now only need
the first one.

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

llvm-svn: 320478
2017-12-12 13:38:51 +00:00
Peter Smith 03cbf468cd [ELF] Move SHF_LINK_ORDER processing earlier in Writer.cpp [NFC]
By moving this step before thunk creation and other processing that depends
on the size of sections, we permit removal of duplicates in the .ARM.exidx
section.

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

llvm-svn: 320477
2017-12-12 13:30:44 +00:00
James Henderson 8d0efdd5db [ELF] Reset OutputSection size prior to processing linker script commands
The size of an OutputSection is calculated early, to aid handling of compressed
debug sections. However, subsequent to this point, unused synthetic sections are
removed. In the event that an OutputSection, from which such an InputSection is
removed, is still required (e.g. because it has a symbol assignment), and no longer
has any InputSections, dot assignments, or BYTE()-family directives, the size
member is never updated when processing the commands. If the removed InputSection
had a non-zero size (such as a .got.plt section), the section ends up with the
wrong size in the output.

The fix is to reset the OutputSection size prior to processing the linker script
commands relating to that OutputSection. This ensures that the size is correct even
in the above situation.

Additionally, to reduce the risk of developers misusing OutputSection Size and
InputSection OutSecOff, they are set to simply the number of InputSections in an
OutputSection, and the corresponding index respectively. We cannot completely
stop using them, due to SHF_LINK_ORDER sections requiring them.

Compressed debug sections also require the full size. This is now calculated in
maybeCompress for these kinds of sections.

Reviewers: ruiu, rafael

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

llvm-svn: 320472
2017-12-12 11:51:13 +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 814ece6854 Add an option for ICFing data.
An internal linker has support for merging identical data and in some
cases it can be a significant win.

This is behind an off by default flag so it has to be requested
explicitly.

llvm-svn: 320448
2017-12-12 01:36:24 +00:00
Petr Hosek d663de3e18 [ELF] Don't set the executable bit for relocatable files
These are not executable files so they shouldn't be marked as such.

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

llvm-svn: 320438
2017-12-11 23:30:54 +00:00
Jake Ehrlich 0ca350a92d [ELF] Change default output section type to SHT_NOBITS
When an output section has no byte commands and has no input sections then it
would be ideal if the type of the section is SHT_NOBITS so that the file can
take up less space. This change sets the default type of of output sections to
SHT_NOBITS instead of SHT_PROGBITS to allow this. This required some minor test
changes (which double as tests for this new behavior) but extend-pt-load.s had
be changed in a non-trivial way. Since it seems to me that the point of the
test is to point out the consequences of how flags are assigned to output
sections that don't have input sections I changed the test to work and still
show how the memsize of the executable segment was changed.

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

llvm-svn: 320437
2017-12-11 23:25:27 +00:00
Rui Ueyama 6074e6b094 Remove redundant local variables.
llvm-svn: 320436
2017-12-11 23:19:11 +00:00
Peter Collingbourne 17f71de70b ELF: Do not follow relocation edges to output sections during GC.
This fixes an assertion error introduced by r320390.

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

llvm-svn: 320431
2017-12-11 22:47:43 +00:00
Rafael Espindola 9c8d7f8dcc Return R_PLT_PC for R_PPC_PLTREL24.
The PPC port doesn't support PLT yet, but the architecture independent
code optimizes PLT access for non preemptible symbols, which is
exactly what returning R_PC was trying to implement.

llvm-svn: 320430
2017-12-11 22:40:18 +00:00
Alexander Richardson d2481bed05 [ELF] When a relocation is out of range print the value and the range
Reviewers: ruiu, grimar

Reviewed By: ruiu

Subscribers: emaste, nemanjai, javed.absar, kbarton, llvm-commits

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

llvm-svn: 320416
2017-12-11 20:47:21 +00:00
Rafael Espindola af57d48ed4 Handle symbols pointing to output sections.
Now that gc sections runs after linker defined symbols are added it
can see symbols that point to an OutputSection.

Should fix a bot failure.

llvm-svn: 320412
2017-12-11 19:45:36 +00:00
Rafael Espindola 63fcc5cccc Create reserved symbols early so they can be versioned.
This fixes pr35570.

We were creating these symbols after parsing version scripts, so they
could not be versioned.

We cannot move the version script parsing later because we need it for
lto.

One option is to move both addReservedSymbols and
createSyntheticSections earlier. The disadvantage is that some
sections created by createSyntheticSections replace other input
sections. For example, gdb index replaces .debug_gnu_pubnames, so it
wants to run after gc sections so that it can set S->Live to false.

What this patch does instead is to move just the ElfHeader creation
early.

llvm-svn: 320390
2017-12-11 17:23:28 +00:00
Rafael Espindola 87e0deaea7 Move RelaPlt and RelaIplt to InX. NFC.
llvm-svn: 320327
2017-12-10 20:07:03 +00:00
Rafael Espindola 58946cdb08 Move RelaDyn to InX. NFC.
llvm-svn: 320324
2017-12-10 19:44:42 +00:00
Rafael Espindola 6d907105c5 Remove this->. NFC.
llvm-svn: 320323
2017-12-10 19:28:32 +00:00
Tim Northover 8f1bc370e2 PPC32: Support R_PPC_PLTREL32 in static mode.
See https://reviews.llvm.org/D39226

Patch by vit9696 <vit9696@avp.su>

llvm-svn: 320286
2017-12-10 08:42:34 +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
Rafael Espindola 5af073e3bf Make createSyntheticSections a static helper. NFC.
llvm-svn: 320200
2017-12-08 21:50:29 +00:00
Rafael Espindola 471001a7b4 Make findSection a static function. NFC.
llvm-svn: 320198
2017-12-08 21:44:11 +00:00
Peter Collingbourne d1eefa993b ELF: Ignore --long-plt flag.
This flag can be ignored because we always emit long PLTs.

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

llvm-svn: 320178
2017-12-08 19:36:19 +00:00
Rafael Espindola 1dd30ddd45 Make addReservedSymbols a static helper. NFC.
llvm-svn: 320175
2017-12-08 19:13:27 +00:00
Alexander Richardson f5ef4e5616 Print the bad value and required alignment for unaligned relocations
Reviewers: ruiu, grimar

Reviewed By: ruiu

Subscribers: emaste, javed.absar, llvm-commits

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

llvm-svn: 320147
2017-12-08 14:53:14 +00:00
Rui Ueyama 04c821c7e3 Use lambda to simplify code.
Differential Revision: https://reviews.llvm.org/D40994

llvm-svn: 320130
2017-12-08 02:20:50 +00:00
Rafael Espindola d182aaa69d Further simplify .gnu.hash writing. NFC.
llvm-svn: 320072
2017-12-07 18:59:29 +00:00
Rafael Espindola f9f2abe7bd Simplify .gnu.hash writing. NFC.
llvm-svn: 320070
2017-12-07 18:51:19 +00:00
Rafael Espindola 50ca10bb58 Avoid using a temporary std::vector.
With this memory usage when linking clang goes from 174.62MB to
172.77MB.

llvm-svn: 320069
2017-12-07 18:46:03 +00:00
Alexander Richardson 280252c6d1 [ELF][mips] Print the full file path for files with incompatible ISA
Summary:
I also changed the message to print both the ISA and the the architecture
name for incompatible files. Previously it would be quite hard to find the
actual path of the incompatible object files in projects that have many
object files with the same name in different directories.

Reviewers: atanasyan, ruiu

Reviewed By: atanasyan

Subscribers: emaste, sdardis, llvm-commits

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

llvm-svn: 320056
2017-12-07 16:08:59 +00:00
Igor Kudrin 892b14658e [ELF] Handle multiple "--version-script" options.
Both ld.bfd and ld.gold can handle this case.

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

llvm-svn: 320006
2017-12-07 03:25:39 +00:00
Rui Ueyama c0081639cc Remove checkToString functions and use toString instead.
Differential Revision: https://reviews.llvm.org/D40928

llvm-svn: 320005
2017-12-07 03:24:57 +00:00
Rafael Espindola 91fac59a81 Add a call to std::vector::reserve.
This reduces total allocations when linking clang fsds from 263.21MB
to 174.62MB.

This also has some very nice speed improvements on some
benchmarks. Chromium and clang fsds link 6% faster.

llvm-svn: 319976
2017-12-06 22:32:19 +00:00
Rui Ueyama bdc5150984 Always evaluate the second argument for CHECK() lazily.
This patch is to rename check CHECK and make it a C macro, so that
we can evaluate the second argument lazily.

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

llvm-svn: 319974
2017-12-06 22:08:17 +00:00
Rafael Espindola b6e2ca4597 Convert a check to checkLazy.
This brings memory allocations when linking clang from 270.96MB to
267.80MB.

llvm-svn: 319932
2017-12-06 19:17:20 +00:00
Rafael Espindola 0ab9d8b6ed Add an early return.
Total memory allocation when linking clang goes from 281.80MB to
270.96MB.

llvm-svn: 319930
2017-12-06 19:13:23 +00:00
Rafael Espindola aca3df5479 Convert a few uses of check to checkLazy.
Linking clang goes from 292.68MB to 281.80MB allocated.

llvm-svn: 319927
2017-12-06 19:08:10 +00:00
Rafael Espindola 5b491a29fb Convert a call to check to checkLazy.
Linking clang goes from 300.82MB to 292.68MB allocated.

llvm-svn: 319926
2017-12-06 19:02:12 +00:00
Rafael Espindola c8dfde2051 Replace one use of check with checkLazy.
Reduce total allocation when linking clang from 320.04MB to 300.82MB.

llvm-svn: 319924
2017-12-06 18:56:22 +00:00
Rafael Espindola 9ffa988b5d Add a checkLazy error checking variant.
This avoids allocating the error message when there is no error that
check requires.

It avoids the code duplication of inlining check.

llvm-svn: 319922
2017-12-06 18:52:13 +00:00
Rafael Espindola 8b97611190 Don't allocate memory for an error message on success.
This takes memory allocations when linking clang-fsds from 342.08MB to
320.04MB.

llvm-svn: 319918
2017-12-06 18:39:22 +00:00
Rafael Espindola f4c3239824 Don't allocate an error message when there is no error.
According to heaptrack this takes "bytes allocated in total" when
linking clang-fsds from 405.69MB to 342.08MB.

llvm-svn: 319916
2017-12-06 18:31:11 +00:00
George Rimar ba7afd7043 [ELF] - Show .plt symbols in -Map output.
As mentioned in PR35471, shared functions for which
.plt entry address is used shows up in bfd's map files. 
Patch teaches LLD to do the same.

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

llvm-svn: 319879
2017-12-06 08:36:21 +00:00
Rafael Espindola a834b529c3 Fix a crash.
This is a constructed testcase I noticed while working on another
patch.

llvm-svn: 319874
2017-12-06 02:53:43 +00:00
Rui Ueyama c4e50bfc55 Fix broken .gnu.hash section if -no-rosegment is given.
We fill executable sections with trap instructions (0xcc or equivalent).
If a .gnu.hash section was put into an executable segment, we created
corrupted .gnu.hash section. This patch fixes the issue.

llvm-svn: 319863
2017-12-06 00:49:48 +00:00
Evgeniy Stepanov 9ac3154d10 [lld] Fix handling of wildcards in dynamic lists.
Reviewers: pcc, ruiu

Subscribers: llvm-commits, emaste

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

llvm-svn: 319860
2017-12-06 00:14:04 +00:00
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 6a954157dd [ELF] AArch64 Errata fix, add missing files.
Forgot these from r319780

llvm-svn: 319781
2017-12-05 16:04:45 +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
George Rimar 31cd5da582 Recommit r319747 "[ELF] - Show copy relocated symbols in a --Map file output."
With fix:
Specify -soname for input dso to fix up the .dynstr section
size in different environments.

Original commit message:

As mentioned in PR35471, copied symbols did not show
in --Map output. Patch fixes that.

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

llvm-svn: 319769
2017-12-05 14:23:18 +00:00
Peter Smith 7c40f93f38 [ELF] InX::BssRelRo should check section contents before marking relro
When a linker script is used with a pattern like { *(.bss .bss.*) } the
InX::BssRelRo section will match against .bss.*. By matching on the name
only, in the same way that .data.rel.ro works we prevent this
from happening, but permit scripts that want to explicitly provide
a .bss.rel.ro OutputSection.
    
Differential Revision: https://reviews.llvm.org/D40735

llvm-svn: 319755
2017-12-05 11:15:58 +00:00
George Rimar fe52e0064b [ELF] - Revert r319747 "[ELF] - Show copy relocated symbols in a --Map file output.".
It broke bots:
http://lab.llvm.org:8011/builders/lld-x86_64-darwin13/builds/15570/steps/test_lld/logs/stdio
http://lab.llvm.org:8011/builders/sanitizer-x86_64-linux-fast/builds/10930/steps/check-lld%20asan/logs/stdio
http://lab.llvm.org:8011/builders/lld-x86_64-win7/builds/16336/steps/test%20lld/logs/stdio

According to outputs,
.dynstr section has different sizes for some reason.

llvm-svn: 319749
2017-12-05 09:38:05 +00:00
George Rimar d9ae97cdb6 [ELF] - Show copy relocated symbols in a --Map file output.
As mentioned in PR35471, copied symbols did not show
in --Map output. Patch fixes that.

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

llvm-svn: 319747
2017-12-05 09:13:45 +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
Rui Ueyama 2278826c60 Cache modulo values for the .gnu.hash section.
This change actually makes the linker slightly faster. My observation
is that, with this patch, link time of clang without debug is about 1%
faster.

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

llvm-svn: 319600
2017-12-02 00:37:13 +00:00
Rafael Espindola b70824c1b8 Revert r318924 Skip over empty sections when checking for contiguous relro
PR35478 https://bugs.llvm.org/show_bug.cgi?id=35478 points out a flaw
in the implementation of r318924 from D40364. The implementation
depends on the Size field being set or the SyntheticSection::empty()
being accurate. These functions are not reliable as some linker script
commands that have yet to be processed may affect the results, causing
some non-zero size sections to be reported as zero size.

I think the first step is to revert r318924 and come up with a better
solution for the underlying problem rather than trying to layer more
heuristics onto the zero sized output section.

Chances are I'll be out of office by the time anyone sees this so feel
free to commit the revert if you agree with me.

Fixes PR35478

Current thoughts on the underlying problem:

Revisiting the motivation for adding the zero size check in the first
place; it was to prevent 0 sized SyntheticSections that a user does
not have full control over from needlessly breaking the PT_GNU_RELRO,
rather than trying to accommodate arbitrarily complex linker
scripts. Looking at the code, it looks like
removeUnusedSyntheticSections() should remove zero sized synthetic
sections. It does, but it doesn't set the Parent to nullptr, this has
the side effect that Sec == InX::BssRelRo->getParent() will make the
parent OutputSection of InX::BssRelRo RelRo even if there is no
InX::BssRelRo.

I tried a quick experiment with setting the Parent to nullptr and this
flushed out a few interesting test failures, it feels like playing
Jenga with every change:

    In the isRelroSection() we have to consider the case where there
    is no .plt and .plt.got but there is a ifunc plt with accompanying
    (ifunc .got or .plt.got)

    The PPC64 has PltHeaderSize == 0. Unfortunately HeaderSize == 0 is
    used to choose between the ifunc plt or normal plt. We seem to get
    away with this at the moment, but tests start to fail when Parent
    is set to nullptr for the .got.plt.

    The InX::BssRelRo and InX::Bss never get their sizes set and they
    are always removed by removeUnusedSyntheticSections(), their
    purpose seems to be as some kind of proxy for add .bss or
    .bss.relro InputSections into their parent OutputSections, they
    therefore don't behave like other SyntheticSections anyway.

My thinking is that some work is needed to make sure that the Sec ==
SyntheticSection->getParent() does a bit more checking before
returning true, particularly for InX::BssRelRo as that has special
behaviour. I'll hope to post something for review as soon as possible.

Patch by Peter Smith!

llvm-svn: 319563
2017-12-01 18:14:14 +00:00
George Rimar 196be4a18e [ELF] - Rename excessive variable. NFC.
It is a follow up for 
https://reviews.llvm.org/D40652 requested during review.

llvm-svn: 319527
2017-12-01 09:14:56 +00:00
George Rimar 78e27e830d [ELF] - Produce relocation section name consistent with output section name when --emit-reloc used with linker script.
This is for "Bug 35474 - --emit-relocs produces wrongly-named reloc sections".

LLD currently for scripts like:

.text.boot : { *(.text.boot) }
emits relocation section with name .rela.text because does not take
redefined name of output section into account and builds section name
using rules for non-scripted case. Patch fixes this oddness.

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

llvm-svn: 319526
2017-12-01 09:04:52 +00:00
Rui Ueyama 1cf7f9cc80 Make .gnu.hash section smaller.
Our on-disk hash table was unnecessarily large. The cost of collision is
not high in the .gnu.hash table because each symbol in the .gnu.hash
table has a hash value with it. So, for each collided symbol, the
dynamic linker just compares an integer, which is pretty cheap.

This patch increases the load factor by about 8. Here's a comparison.

  $ readelf --histogram libclangSema.so.6.0.0svn-new-lld
  Histogram for `.gnu.hash' bucket list length (total of 582 buckets):
   Length  Number     % of total  Coverage
        0  11         (  1.9%)
        1  35         (  6.0%)      1.5%
        2  93         ( 16.0%)      9.5%
        3  108        ( 18.6%)     23.4%
        4  121        ( 20.8%)     44.1%
        5  86         ( 14.8%)     62.6%
        6  63         ( 10.8%)     78.8%
        7  38         (  6.5%)     90.2%
        8  18         (  3.1%)     96.4%
        9  6          (  1.0%)     98.7%
       10  3          (  0.5%)    100.0%

  $ readelf --histogram libclangSema.so.6.0.0svn-old-lld
  Histogram for `.gnu.hash' bucket list length (total of 4093 buckets):
   Length  Number     % of total  Coverage
        0  1498       ( 36.6%)
        1  1545       ( 37.7%)     37.7%
        2  712        ( 17.4%)     72.5%
        3  251        (  6.1%)     90.9%
        4  66         (  1.6%)     97.3%
        5  16         (  0.4%)     99.3%
        6  5          (  0.1%)    100.0%

  $ readelf --histogram libclangSema.so.6.0.0svn-bfd
  Histogram for `.gnu.hash' bucket list length (total of 1004 buckets):
   Length  Number     % of total  Coverage
      0  92         (  9.2%)
        1  227        ( 22.6%)      9.8%
        2  266        ( 26.5%)     32.6%
        3  222        ( 22.1%)     61.2%
        4  115        ( 11.5%)     81.0%
        5  55         (  5.5%)     92.8%
        6  21         (  2.1%)     98.2%
        7  6          (  0.6%)    100.0%

  $ readelf --histogram libclangSema.so.6.0.0svn-gold
  Histogram for `.gnu.hash' bucket list length (total of 2053 buckets):
   Length  Number     % of total  Coverage
        0  671        ( 32.7%)
        1  709        ( 34.5%)     30.4%
        2  470        ( 22.9%)     70.7%
        3  141        (  6.9%)     88.9%
        4  54         (  2.6%)     98.2%
        5  5          (  0.2%)     99.2%
        6  3          (  0.1%)    100.0%

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

llvm-svn: 319503
2017-11-30 23:59:40 +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
Rafael Espindola b2ee25ac8a Simplify. NFC.
This particular reportDuplicate is only used with Defined.

llvm-svn: 319451
2017-11-30 18:02:04 +00:00
Rafael Espindola de38b3d22f Handle copy relocations in symbol assignments.
When a linker script has "foo = bar" and bar is the result of a copy
relocation foo should point to the same location in .bss.

This is part of a growing evidence that copy relocations should be
implemented by using replaceSymbol to replace the SharedSymbol with a
Defined.

llvm-svn: 319449
2017-11-30 17:51:10 +00:00
George Rimar 2e610cd470 [ELF] - Handle EhInputSection Live bit in MarkLive.cpp
Since MarkLive.cpp is the place where we set Live flags for
other sections, it looks correct to do that there.
Benefit is that we stop spreading GC logic outsize of MarkLive.cpp.

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

llvm-svn: 319435
2017-11-30 14:01:06 +00:00
Rafael Espindola 568ee2c089 Don't crash on invalid.
llvm-svn: 319404
2017-11-30 06:18:31 +00:00
Rafael Espindola bdcfb178b5 Delete dead code.
llvm-svn: 319403
2017-11-30 05:52:42 +00:00
Rafael Espindola 8ce6db0c4b Error instead of ignoring broken debug info.
Thanks to Davide for noticing.

llvm-svn: 319401
2017-11-30 05:37:35 +00:00
Rui Ueyama 1bf618a9da Remove unused condition.
Looks like all tests pass without this `isLocal()`.

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

llvm-svn: 319395
2017-11-30 01:08:09 +00:00
Rui Ueyama 8ed548a481 Fix typo.
llvm-svn: 319394
2017-11-30 01:04:26 +00:00
Rafael Espindola dfebd3601d Use Symbol::File directly.
We are already paying the cost of storing a InputFile in every
Symbol, so use it uniformly.

llvm-svn: 319378
2017-11-29 22:47:35 +00:00
Rafael Espindola 07b7ee4394 Simplify. NFC.
llvm-svn: 319370
2017-11-29 22:20:03 +00:00
Rafael Espindola f893396d1b Don't crash on broken debug info.
llvm-svn: 319367
2017-11-29 22:09:16 +00:00
Rafael Espindola cf5dc9f24c Replace a dyn_cast with a cast. NFC.
llvm-svn: 319361
2017-11-29 21:29:52 +00:00
Rafael Espindola ea5610648c Replace another dyn_cast with a cast.
llvm-svn: 319335
2017-11-29 18:43:34 +00:00
Rafael Espindola d42f7e5cae Replace a dyn_cast with a cast.
It is always accessed, so there is no value in a dyn_cast.

llvm-svn: 319334
2017-11-29 18:32:57 +00:00
Peter Smith 31dddc97ae [ELF][AArch64] Add support for AArch64 range thunks.
The AArch64 unconditional branch and branch and link instructions have a
maximum range of 128 Mib. This is usually enough for most programs but
there are cases when it isn't enough. This change adds support for range
extension thunks to AArch64. For pc-relative thunks we follow the small
code model and use ADRP, ADD, BR. This has a limit of 4 gigabytes.

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

llvm-svn: 319307
2017-11-29 11:15:12 +00:00
Peter Smith 8fca2e12ab [ELF] Make sure SHT_ARM_ATTRIBUTES is only recognized by Arm Targets
The SHT_ARM_ATTRIBUTES section type is in the processor specific space
adding guard to make sure that it is only recognized when EMachine is
EM_ARM.

llvm-svn: 319304
2017-11-29 10:20:46 +00:00
Rafael Espindola 09b53f6fd8 Delete dead code. NFC.
llvm-svn: 319274
2017-11-29 01:55:03 +00:00
Rafael Espindola b262cbe6b7 Replace copyFrom with memcpy.
It was only used for --wrap and I don't think the fields with special
treatment had a meaningful impact on that feature.

llvm-svn: 319265
2017-11-29 00:31:39 +00:00
Rafael Espindola 97d05f95b9 Copy the visibility in copyFrom.
This is simpler and matches bfd's behavior on the changed test.

llvm-svn: 319248
2017-11-28 23:06:09 +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
Rafael Espindola 1d4b3023dc Bring back r319008.
This includes a fix to mark copy reloc aliases as used.

Original message:

[ELF] Do not keep symbols if they referenced only from discarded sections.

This patch also ensures that in case of "--as-needed" is used,
DT_NEEDED entries are not created if they are required only by
these eliminated symbols.

llvm-svn: 319215
2017-11-28 20:17:58 +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
Rafael Espindola b45c164fc2 Handle binding in --wrap.
Copying the binding seems reasonable and matches the bfd behavior.

llvm-svn: 319196
2017-11-28 18:02:22 +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
Igor Kudrin a46522f369 Revert r319008, "[ELF] Do not keep symbols if they referenced only from discarded sections."
and r319051, "Add a missing test."

r319008 broke the LTO bots;
r319051 depends on changes in r319008.

llvm-svn: 319154
2017-11-28 08:39:40 +00:00
Martin Storsjo 2d614ced55 Fix shared library builds of lld, broken in SVN r319138
llvm-svn: 319152
2017-11-28 08:12:03 +00:00
Rui Ueyama 53fe469162 Factor out common code to Common/Strings.cpp.
Differential Revision: https://reviews.llvm.org/D40530

llvm-svn: 319138
2017-11-28 02:15:26 +00:00
Rafael Espindola 9e3381e8dc Store the real binding of shared symbols.
Currently we mark every shared symbol as STB_WEAK.

That is a hack to make it easy to decide when a .so is needed or not
because of a reference to a given symbol.

That hack leaks when we create copy relocations as shown by the update
to relocation-copy-alias.s.

This patch stores the original binding when we first read a shared
symbol. We still have to update the binding to weak if we see a weak
undef, but I find the logic easier to read where it is now.

llvm-svn: 319127
2017-11-28 01:04:51 +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
Sam Clegg e2ed092e01 Consistent use of <internal> when displaying internal symbol names
Differential Revision: https://reviews.llvm.org/D40510

llvm-svn: 319108
2017-11-27 22:49:16 +00:00
Peter Smith 2809926c4d [ELF][ARM] Refine check for when undefined weak needs a Thunk
When an undefined weak reference has a PLT entry we must generate a range
extension thunk for any B or BL that can't reach the PLT entry.

This change explicitly looks for whether a PLT entry exists rather than
assuming that weak references never need PLT entries unless Config->Shared
is in operation. This covers the case where we are linking an executable
with dynamic linking, hence a PLT entry will be needed for undefined weak
references. This case comes up in real programs over 32 Mb in size as there
is a B to a weak reference __gmon__start__ in the Arm crti.o for glibc.

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

llvm-svn: 319020
2017-11-27 11:49:18 +00:00
Igor Kudrin 40007586fc [ELF] Do not keep symbols if they referenced only from discarded sections.
This patch also ensures that in case of "--as-needed" is used,
DT_NEEDED entries are not created if they are required only by
these eliminated symbols.

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

llvm-svn: 319008
2017-11-27 05:51:10 +00:00
Rafael Espindola f8e405db46 Delete dead code.
The parent constructor is already setting the binding.

llvm-svn: 318962
2017-11-24 19:06:14 +00:00
Peter Smith 80e4be7eae [ELF] Rename .bss.rel.ro to .data.rel.ro.bss for some Linker Scripts.
LLD uses .bss.rel.ro for read-only copy relocations whereas the ld.bfd and
gold linkers use .data.rel.ro. In some linker scripts including ld.bfd's
internal linker script, the relro sections are placed sequentially assuming
.data.rel.ro is used. LLD's use of .bss.rel.ro means that the copy
relocations get matched into the .bss section causing the relro sections to
be non-contiguous.

This change checks for a .data.rel.ro OutputSection when a linker script
with the SECTIONS command is used. The section will match in the
.data.rel.ro output section and will maintain contiguous relro.

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

Fixes PR35265

llvm-svn: 318940
2017-11-24 08:48:29 +00:00
Rui Ueyama 15475e92c6 Use lambdas to store lazy-evaluated .dynamic entries.
Differential Revision: https://reviews.llvm.org/D40338

llvm-svn: 318938
2017-11-24 02:15:51 +00:00
Peter Smith c076b280df [ELF] Skip over empty sections when checking for contiguous relro
When checking for contiguous relro sections we can skip over empty sections.
If there is an empty non-relro section in the middle of a contiguous block
of relro sections then it cannot be written to so it is safe to include in
PT_GNU_RELRO header. If there is a contiguous block of empty relro sections
then no PT_GNU_RELRO header is required for them.

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

llvm-svn: 318924
2017-11-23 15:10:00 +00:00
Peter Smith dc3bd88b22 [ELF] Give error message when relro sections are not contiguous.
If a linker script is used that names linker generated synthetic sections
it is possible that the OutputSections for which isRelroSection() is true
are not contiguous. When the relro sections are not contiguous we cannot
describe them with a single PT_GNU_RELRO PHDR. Unfortunately at least one
contemporary dynamic loader only supports one PT_GNU_RELRO PHDR so we
cannot output more than one of these PHDRs. As not including relro
sections in the PHDR will lead to security sensitive sections being
writeable we choose to give an error message instead.

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

[ELF] Skip over empty sections when checking for contiguous relro

llvm-svn: 318920
2017-11-23 14:53:10 +00:00
Rafael Espindola de56343cf0 Simplify as-needed handling.
This is a reduction of a patch by Rui Ueyama.

llvm-svn: 318852
2017-11-22 17:50:42 +00:00
Simon Atanasyan 167306a56c [MIPS] Write PLT0 entry in case of linking N64 ABI code
llvm-svn: 318831
2017-11-22 12:34:29 +00:00
James Henderson f70c5beb22 [ELF] Fix DT_MIPS_LOCAL_GOTNO value for thunks and linker scripts
The MIPS GOT section has a number of local entries based on the number of pages
needed for output sections referenced by GOT page relocations. The number is
recorded in the DT_MIPS_LOCAL_GOTNO dynamic section tag. However, the dynamic tag
is added before assignAddresses has been called, meaning that any section size used
to calculate the value will not include size modifications caused by, for example,
linker scripts and thunks.

This change moves the calculation of DT_MIPS_LOCAL_GOTNO until writeTo, by which
time the output section sizes have been finalized.

Reviewers: ruiu, rafael

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

llvm-svn: 318828
2017-11-22 12:04:21 +00:00
Alexander Richardson d642494828 [ELF] Fix rewriting of --sysroot in reproducer tars
Summary:
I noticed that the reproducers files I was getting from building CheriBSD
didn't work because the --sysroot option was not being rewritten. I've
updated the test to also verify that the rewritten path matches uses a
FileCheck capature instead of a {{.+}} regex

Reviewers: ruiu

Reviewed By: ruiu

Subscribers: llvm-commits, emaste

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

llvm-svn: 318656
2017-11-20 15:49: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
Alexander Richardson f463042312 [ELF][MIPS] Fix crash in LLD when linking code that needs PIC thunks
Summary:
The bug triggers when the following conditions are met:
    - A thunk is created in a given input section S
    - A linker script is specified
    - There is at least one matcher in the linker script .text section output
      that does not match any of the sections in the input files, before the matcher
      that matches section S.

The issue was found when linking the FreeBSD kernel for MIPS when built
with -fPIC. Patch by Alfredo Mazzinghi.

Reviewers: ruiu, psmith, atanasyan

Reviewed By: ruiu

Subscribers: peter.smith, emaste, sdardis, krytarowski, llvm-commits

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

llvm-svn: 318653
2017-11-20 15:37:19 +00:00
Rafael Espindola d0929caa21 Don't use llvm/Config/config.h.
It is private to llvm. Instead use llvm/Config/llvm-config.h and check
LLVM_ON_UNIX. That is the same guard that clang uses before including
unistd.h.

llvm-svn: 318558
2017-11-17 21:40:38 +00:00
George Rimar 5f928ff133 [ELF] - Teach LLD to use information from .debug_str for error reporting.
Recently we teached LLD to report line numbers for duplicate variables
definitions, though currently LLD is unable to do that for case when
strings are not built in .debug_info, but stored in .debug_str instead.
That is because out LLDDwarfObj does not handle .debug_str yet. 
Patch fixes that.

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

llvm-svn: 318519
2017-11-17 11:57:47 +00:00
George Rimar 80355234f7 [ELF] - Allow applying SHF_MERGE optimization for relocatable output.
This fixes PR35223.

Here I enabled SHF_MERGE section content merging for -r like
we do for regular linking.

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

llvm-svn: 318516
2017-11-17 11:27:57 +00:00
Rui Ueyama 11adb536e1 Ignore -no-ctors-in-init-array.
Patch by dxf@google.com.

llvm-svn: 318513
2017-11-17 08:17:36 +00:00
Rui Ueyama 7212f0ff11 Add a missing #include.
llvm-svn: 318512
2017-11-17 08:17:21 +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
George Rimar 661a2c1970 [ELF] - Don't emit broken relocations for SHF_MERGE sections when --emit-relocs is used.
Previously our relocations we rewrote were broken for that case.
We emited incorrect addend and broken relocation info field
because did not produce section symbol for mergeable synthetic sections.

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

llvm-svn: 318394
2017-11-16 12:33:36 +00:00
Rafael Espindola 8bc2a19ef8 Drop conflicting sh_entsize values.
An output section can include elements from two input sections with
different sh_entsize. When that happens the output section itself
should not have a sh_entsize.

llvm-svn: 318311
2017-11-15 17:35:22 +00:00
Rafael Espindola 3f0b575363 Remove an unnecessary constraint.
Our current implementation of SHF_MERGE can already handle over
aligned elements.

llvm-svn: 318310
2017-11-15 17:31:27 +00:00
Rafael Espindola a5d43d004a Propagate sh_entsize out.
No difference in practice other than having sh_entsize in the output.

This should simplify the patch for handling SHF_MERGE in -r.

Based on a patch by George Rimar.

llvm-svn: 318306
2017-11-15 16:56:20 +00:00
Simon Atanasyan cfa8aa7edb [MIPS] Set STO_MIPS_MICROMIPS flag and less-significant bit for microMIPS symbols
microMIPS symbols including microMIPS PLT records created for regular
symbols needs to be marked by STO_MIPS_MICROMIPS flag in a symbol table.
Additionally microMIPS entries in a dynamic symbol table should have
configured less-significant bit. That allows to escape teaching a
dynamic linker about microMIPS symbols.

llvm-svn: 318097
2017-11-13 22:40:36 +00:00
Rafael Espindola a4884cc38f Try harder to delete the temporary file.
It is really hard to cover restarts in a debugger, SIGKILL or power
failures. I will try to handle them in a followup patch, but it will
not support all the systems lld has to run on.

RemoveFileOnSignal takes care of crashes.

So what is left is making sure all regular exits delete the file. This
patch does that by moving the buffer to error handling. That is a bit
of a hack, but seemed better than to generalize it to take a callback on
construction.

I will implement this on COFF on the next patch.

llvm-svn: 318060
2017-11-13 18:06:43 +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
Rafael Espindola 5c22661255 Don't produce a duplicated __wrap_foo if there is no __real_foo.
This fixes a regression from r317426.

llvm-svn: 317956
2017-11-11 00:53:52 +00:00
Simon Atanasyan 509834d7d3 [MIPS] Fix calculation of the R_MICROMIPS_LO16 / HI16 relocations
llvm-svn: 317781
2017-11-09 12:10:14 +00:00
Simon Atanasyan 3a7044ef78 [MIPS] Setup less-significant bit in a symbol value in microMIPS thunks
The less-significant bit signals about microMIPS code for jump/branch instructions.

llvm-svn: 317778
2017-11-09 10:42:22 +00:00
Simon Atanasyan 5a4e21364c [MIPS] Setup less-significant bit in the .got and .got.plt entries in case of microMIPS code
The less-significant bit signals about microMIPS code for jump/branch
instructions.

llvm-svn: 317741
2017-11-08 23:34:34 +00:00
Rafael Espindola 85b8d0ca7c Handle "-" in tryCreateFile.
Otherwise we would fail with -M if the we didn't have write
permissions to the current directory.

llvm-svn: 317740
2017-11-08 23:07:32 +00:00
Rui Ueyama 1e8813c6b4 Remove a given file even if openFileForRead fails.
I think the constract of this function is to remove a file in some way,
whether in background or in foreground. This patch makes sure that it
removes a given file.

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

llvm-svn: 317663
2017-11-08 04:22:40 +00:00
Rafael Espindola 5f903f3848 Update for llvm change.
llvm-svn: 317657
2017-11-08 01:50:34 +00:00
Rafael Espindola f7a57294d7 Update for llvm change.
llvm-svn: 317650
2017-11-08 01:05:52 +00:00
Rafael Espindola 367ca01478 Fix unused variable error.
llvm-svn: 317637
2017-11-07 23:43:01 +00:00
Rafael Espindola 37a575f8b6 Delete dead code.
llvm-svn: 317633
2017-11-07 23:12:41 +00:00
Rafael Espindola c78890640e Use std::thread directly.
Now that this code is posix only we don't need to use runBackground.

llvm-svn: 317632
2017-11-07 23:09:54 +00:00
Rafael Espindola 1501a26b39 Simplify unlinkAsync.
It used to rename the file before removing it in a background thread.

A simpler way to delay the removal is simply to open the file.

Since this is a posix specific issue, simplify the windows
implementation.

llvm-svn: 317631
2017-11-07 23:01:37 +00:00
Rafael Espindola 03e4ae42d5 Delete unused constructor.
llvm-svn: 317625
2017-11-07 22:30:53 +00:00
Paul Robinson e5400f8a6e [DWARFv5] Support DW_FORM_strp in the .debug_line header.
Supporting this form in .debug_line.dwo will be done as a follow-up.

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

llvm-svn: 317607
2017-11-07 19:57:12 +00:00
Rafael Espindola fcd7494832 Use Path instead of Config->OutputFile. NFC.
This function is always called with Config->OutputFile. That is not
obvious from reading the function. It should always use Path or take
no argument and always use Config->OutputFile.

llvm-svn: 317536
2017-11-07 02:00:51 +00:00
Rui Ueyama 989b1fcb01 Remove dead code.
llvm-svn: 317530
2017-11-07 00:26:04 +00:00
Rui Ueyama d5e2e8393b Report an error if an inferred alignment for a shared symbol is too large.
Differential Revision: https://reviews.llvm.org/D39697

llvm-svn: 317528
2017-11-07 00:12:05 +00:00
Rui Ueyama 7957b08e87 Move MIPS-specific code from Symbols.cpp to MIPS.cpp.
We have a lot of "if (MIPS)" conditions in lld because the MIPS' ABI
is different at various places than other arch's ABIs at where it
don't have to be different, but we at least want to reduce MIPS-ness
from the regular classes.

llvm-svn: 317525
2017-11-07 00:04:22 +00:00
Peter Smith a2db4349d5 [ELF] Fix typo in comment for getISDThunkSec [NFC]
The ISR in the comment should read ISD for InputSectionDescription. The use
of ISR (InputSectionRange) was from the original implementation that did not
use the sections from InputSectionDescription directly.

llvm-svn: 317469
2017-11-06 10:36:18 +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 b472aa0268 ELF: Remove function Symbol::isInCurrentOutput().
This function is now equivalent to isDefined().

llvm-svn: 317449
2017-11-06 04:39:07 +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
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 32665f702e Do not consider Shared symbols as defined symbols.
I don't remember why I made shared symbols one type of defined symbols.
Shared symbols aren't undefined, so it could be considered defined, but
categorizing three symbols as:

 - defined
   - really defined
   - shared
 - undefined

is not as intuitive as

 - defined
 - shared
 - undefined

to me. So, in this patch, I made a change to stop handling shared
symbols as defined symbols.

Surprisingly, I didn't have to update any tests for this change.

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

llvm-svn: 317446
2017-11-06 04:13:24 +00:00
Rui Ueyama 5c4cb8a9e3 Inline a small function.
llvm-svn: 317428
2017-11-04 23:57:51 +00:00
Rui Ueyama aa8523e4b6 Move OutputSectionFactory to LinkerScript.cpp. NFC.
That class is used only by LinkerScript.cpp, so we should move it to
that file. Also, it no longer has to be a "factory" class. It can just
be a non-member function.

llvm-svn: 317427
2017-11-04 23:54:25 +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
George Rimar 343e8227b7 [ELF] - Stop using SectionKey for creating output sections.
Stop using SectionKey for creating output sections.

Initially SectionKey was designed because we merged section with
use of Flags and Alignment fields. Currently LLD merges them by name only,
except the case when -relocatable output is produced. In that case
we still merge sections only with the same flags and alignment.
There is probably no issue at all to stop using Flags and Alignment for -r and
just disable the merging in that case.

After doing that change we can get rid of using SectionKey. That is not only
simplifies the code, but also gives some perfomance boost.

I tried to link chrome and mozilla, results are next:
* chrome link time goes from 1,666750355s to 1,551585364s, that is about 7%.
* mozilla time changes from 3,210261947 to 3,153782940, or about 2%.

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

llvm-svn: 317406
2017-11-04 09:11:27 +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 48882249dd Rename Body -> Sym.
This patch is mechanically created by

  sed -i -e s/Body/Sym/g lld/ELF/*.{cpp,h}

and clang-format-diff.

llvm-svn: 317392
2017-11-04 00:31:04 +00:00
Rui Ueyama f483da0038 Rename replaceBody -> replaceSymbol.
llvm-svn: 317383
2017-11-03 22:48:47 +00:00
Zachary Turner a104d558d0 Fix variable name.
A mechanical rename caused an illegal name conflict between
a variable and a type, which was picked up by GCC.

llvm-svn: 317380
2017-11-03 22:33:49 +00:00
Bob Haarman 3fae5a6179 Revert "[ELF] - Teach LLD to use information from .debug_str for error reporting."
This reverts commit 00b7acb8f6c8a4663bb7c8396d217c210209b562.

It was causing some links to execute llvm_unreachable.

llvm-svn: 317378
2017-11-03 22:23:19 +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
George Rimar 1d415c335e [ELF] - Remove excessive field initialization. NFC.
It is already initialized in header file to same value.

llvm-svn: 317320
2017-11-03 13:19:41 +00:00
George Rimar 31b6b0a820 [ELf] - Fix compilation after r317307.
Not sure why that seems did not break any llvm bots or
my windows local build, but is was required to fix compilation
breakage of my ubuntu build when using
gcc version 8.0.0 20171019 (experimental)

llvm-svn: 317317
2017-11-03 11:57:01 +00:00
George Rimar 8c0cdca99e [ELF] - Removed unused variable. NFC.
llvm-svn: 317316
2017-11-03 11:51:58 +00:00
George Rimar 8c825db25e [ELF] - Linkerscript: fixed non-determinism when handling MEMORY.
When findMemoryRegion do search to find a region for output section it
iterates over MemoryRegions which is DenseMap and so does not
guarantee iteration in insertion order. As a result selected region depends
on its name and not on its definition position
Testcase shows the issue, patch fixes it. Behavior after applying the patch
seems consistent with bfd.

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

llvm-svn: 317307
2017-11-03 08:21:51 +00:00
George Rimar cbed0e615b [ELF] - Teach LLD to strip .zdebug_* sections when --strip-debug/--strip-all is specified.
Currently we do not strip .zdebug_*, what looks wrong.
Also this simplifies the testcase we have for this options.

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

llvm-svn: 317306
2017-11-03 08:13:16 +00:00
George Rimar 5b262363b4 [ELF] - Teach LLD to use information from .debug_str for error reporting.
Recently we teached LLD to report line numbers for duplicate variables
definitions, though currently LLD is unable to do that for case when
strings are not built in .debug_info, but stored in .debug_str instead.
That is because out LLDDwarfObj does not handle .debug_str yet. 
Patch fixes that.

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

llvm-svn: 317305
2017-11-03 08:04:27 +00:00
George Rimar 1b45d377e3 [ELF] - Cleanup of processSectionCommands().
The way we handle ONLY_IF_RO/ONLY_IF_RW constraints in
processSectionCommands is a bit tricky. If input sections
does no satisfy given constraint we remove command from
commands list. It seems too complex, what we can do instead
is to make the OutputCommand empty. So that at later steps
LLD will remove it just like it deal with other empty output commands.
That allows to simplify the loop a bit.

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

llvm-svn: 317082
2017-11-01 08:40:28 +00:00
George Rimar 82f0c42dad [ELF] - Teach LLD to report line numbers for data symbols.
This is PR34826.

Currently LLD is unable to report line number when reporting
duplicate declaration of some variable.

That happens because for extracting line information we always use
.debug_line section content which describes mapping from machine
instructions to source file locations, what does not help for
variables as does not describe them.

In this patch I am taking the approproate information about
variables locations from the .debug_info section.

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

llvm-svn: 317080
2017-11-01 07:42:38 +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 95c142e208 Revert r316305: Remove a fast lookup table from MergeInputSection.
This reverts commit r316305 because performance regression was observed.

llvm-svn: 317026
2017-10-31 19:14:06 +00:00
James Henderson 50baa593f1 [ELF] Remove unnecessary function call. NFC.
StringTableSection does not implement postThunkContents, so calling it on these 
sections is pointless (it calls an empty virtual function), and we can remove it.

Reviewers: ruiu

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

llvm-svn: 317014
2017-10-31 17:28:44 +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
George Rimar f173a6309f [ELF] - Remove useless code. NFC.
We set Type and Flags inside OutputSection::addSection,
so this lines looks to be excessive.

llvm-svn: 317002
2017-10-31 14:21:30 +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
George Rimar f9b04fd91f [ELF] - Simplify output section creation.
When there is no SECTION commands given, all sections are
technically orphans, but now we handle script orphans sections
and regular "orphans" sections for non-scripted case differently,
though we can handle them at one place.

Patch do that change.

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

llvm-svn: 316984
2017-10-31 10:31:58 +00:00
Jake Ehrlich 1128dc5e30 Give .note.gnu.build-id section alignment 4
All SHT_NOTE sections should have minimum alignment 4.

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

llvm-svn: 316961
2017-10-30 22:08:11 +00:00
Rafael Espindola 458173e802 Use 64 bits for the shared symbol size again.
We might want to drop support for such large symbols, but that should
be an explicit decision with a testcase.

llvm-svn: 316931
2017-10-30 17:43:16 +00:00
Rafael Espindola 566dbdc2fd Skip abs symbols when handling copy reloc aliases.
Since we now only check st_value, we have to consider the case where
the section index is special.

llvm-svn: 316928
2017-10-30 17:26:12 +00:00
George Rimar a6b1fbece1 [ELF] - Stop sorting input sections in createSections().
It does not seem that createSections() is a good place for
applying sorting. Patch changes code to do that inside
sortSections(), which looks more appropriate place.

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

llvm-svn: 316893
2017-10-30 10:12:49 +00:00
Rui Ueyama 27f95a9ac3 Remove unnecessary code.
Discarded section's Repl always points to itself, so returning
Sec is not different from returning Sec->Repl.

llvm-svn: 316880
2017-10-29 23:41:16 +00:00
Rui Ueyama ea1398eeb3 Move "Assigned" bit from SectionBase to InputSectionBase.
This bit is to manage whether an input section has already been assigned
to some output section by linker scripts or not. So it logically belongs
to InputSectionBase. SectionBase is a common base class for input and
output sections, so that wasn't the right place to define the bit.

llvm-svn: 316879
2017-10-29 23:32:23 +00:00
Rui Ueyama 63edd9f831 Remove an obscure comment.
llvm-svn: 316878
2017-10-29 22:38:00 +00:00
Peter Collingbourne e662436114 ELF: Correctly set edata if there are no .bss sections.
edata needs to be set to the end of the last mapped initialized
section. We were previously mishandling the case where there were no
non-mapped sections by setting it to the end of the last section in
the output file.

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

llvm-svn: 316877
2017-10-29 22:31:48 +00:00
Rui Ueyama da4d26dfa3 Initialize members not by assignment but by the member initializer list.
llvm-svn: 316876
2017-10-29 22:26:52 +00:00
Rui Ueyama b6f4e722b1 Fix ubsan error that shift amount 64 is too large.
llvm-svn: 316863
2017-10-29 16:49:42 +00:00
Rui Ueyama ac6472535b Remove a redundant temporary variable.
llvm-svn: 316862
2017-10-29 16:46:39 +00:00
Rui Ueyama da42135cef Fix ubsan error.
This patch fixes ubsan's "shift exponent 64 is too large for 32-bit type
'int'" error.

llvm-svn: 316849
2017-10-28 22:18:29 +00:00
Rui Ueyama bd730e3ca7 Reduce sizeof(Symbol) from 104 bytes to 88 bytes.
Finding aliases for shared symbols doesn't need st_shndx because
we can just compare st_value.

llvm-svn: 316848
2017-10-28 22:18:17 +00:00
Rui Ueyama 4a22edabec Do not handle DefinedCommon symbols in the MapFile writer.
Because of r314495 which converts DefinedCommon symbols to DefinedRegular
symbols, common symbols are no longer reachable to the MapFile writer.
So the code to handle common symbols is now dead.

llvm-svn: 316846
2017-10-28 21:11:38 +00:00
Rui Ueyama 1392e84caf Rename V -> Ver for readability.
llvm-svn: 316842
2017-10-28 20:16:11 +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
Rui Ueyama f405b11224 Simplify error handling.
I don't think we have to aim for precise bug compatibility.
We can return a nullptr if a section is consumed by the linker, and
the rest should naturally work.

llvm-svn: 316817
2017-10-28 00:05:12 +00:00
Rui Ueyama da524d021a Remove unnecessary namespace specifiers.
llvm-svn: 316812
2017-10-27 23:29:58 +00:00
Rui Ueyama 7833afdd6b Inline trivial symbol constructors.
llvm-svn: 316811
2017-10-27 23:26:46 +00:00
Rui Ueyama bda337a8d8 Rename isInCurrentDSO -> isInCurrentOutput.
DSO is short for dynamic shared object, so the function name was a
little confusing because it sounded like it didn't work when we were
a creating statically-linked executable or something.

What we mean by "DSO" here is the current output file that we are
creating. Thus the new name. Alternatively, we could call it the current
ELF module, but "module" is a overloaded word, so I avoided that.

llvm-svn: 316809
2017-10-27 22:54:16 +00:00
Rafael Espindola bc2b1650d7 Handle all symbol types in --trace.
One case I was not sure was lazy symbols. bfd skips them completely,
but that is probably an artifact of how they implement archives.

llvm-svn: 316782
2017-10-27 18:30:11 +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
George Rimar 90dacc256f [ELF] - Ignore non-absolute R_386_GOTPC relocation in debug sections.
This is for PR34852.

GCC 8.0 or earlier have a bug that it emits R_386_GOTPC relocations
against _GLOBAL_OFFSET_TABLE for .debug_info. The bug seems to have
been fixed in 2017: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82630,
but we do not want LLD to report errors for such inputs.
In this patch we ignore such relocations.

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

llvm-svn: 316761
2017-10-27 11:49:24 +00:00
George Rimar 3396bf07a2 [ELF] - Stop naming relocation sections with first input section name.
It was reported (https://reviews.llvm.org/D38724#902841) that when we use 
-ffunction-sections --emit-relocs build, REL[A] output section receives the name of first
input section, like .rela.text.first_function_in_text rather than .rela.text.

It is probably not really an issue as sh_info still points to correct target section, but
it does not look clean in output and allows internal section name to leak there,
what at least looks confusing and is not consistent with ld.bfd.

Patch changes this behavior so that target output section name is used as a base.

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

llvm-svn: 316760
2017-10-27 11:38:31 +00:00
George Rimar 96b1157814 [ELF] - Simplify reporting of garbage collected sections.
This moves reporting of garbage collected sections right after
we do GC. That simplifies things.

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

llvm-svn: 316759
2017-10-27 11:32:22 +00:00
Peter Smith 6c9df3fce5 [ELF] Add support for multiple passes to createThunks()
This change allows Thunks to be added on multiple passes. To do this we must
merge only the thunks added in each pass, and deal with thunks that have
drifted out of range of their callers.

A thunk may end out of range of its caller if enough thunks are added in
between the caller and the thunk. To handle this we create another thunk.

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

llvm-svn: 316754
2017-10-27 09:07:10 +00:00
Peter Smith 75030b6d56 [ELF] Introduce range extension thunks for ARM
This change adds initial support for range extension thunks. All thunks must
be created within the first pass so some corner cases are not supported. A
follow up patch will add support for multiple passes.

With this change the existing tests arm-branch-error.s and
arm-thumb-branch-error.s now no longer fail with an out of range branch.
These have been renamed and tests added for the range extension thunk.

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

llvm-svn: 316752
2017-10-27 09:04:11 +00:00
Peter Smith f0c70f8d34 [ELF] Pre-create ThunkSections at Target specific intervals
When an OutputSection is larger than the branch range for a Target we
need to place thunks such that they are always in range of their caller,
and sufficiently spaced to maximise the number of callers that can use
the thunk. We use the simple heuristic of placing the
ThunkSection at intervals corresponding to a target specific branch range.
If the OutputSection is small we put the thunks at the end of the executable
sections.

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

llvm-svn: 316751
2017-10-27 08:58:28 +00:00
Peter Smith 4a8e11595c [ELF] Record created ThunkSections in InputSectionDescription [NFC].
Instead of maintaining a map of the std::vector to ThunkSections, record the
ThunkSections directly in InputSectionDescription.

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

llvm-svn: 316750
2017-10-27 08:56:20 +00:00
Rui Ueyama 0f46e45b03 Simplify logic to find the last executable segment. NFC.
llvm-svn: 316742
2017-10-27 05:08:39 +00:00
Rui Ueyama df15559c80 Fix typo.
llvm-svn: 316741
2017-10-27 04:59:33 +00:00