Commit Graph

1292 Commits

Author SHA1 Message Date
Rafael Espindola a0d7df3988 Put the header in the first PT_LOAD even if that PT_LOAD has a LMAExpr.
This should fix PR36017.

The root problem is that we were creating a PT_LOAD just for the
header. That was technically valid, but inconvenient: we should not be
making the ELF discontinuous.

The solution is to allow a section with LMAExpr to be added to a
PT_LOAD if that PT_LOAD doesn't already have a LMAExpr.

llvm-svn: 323625
2018-01-29 03:44:44 +00:00
Rafael Espindola db9dd5b43e Improve LMARegion handling.
This fixes the crash reported at PR36083.

The issue is that we were trying to put all the sections in the same
PT_LOAD and crashing trying to write past the end of the file.

This also adds accounting for used space in LMARegion, without it all
3 PT_LOADs would have the same physical address.

llvm-svn: 323449
2018-01-25 17:42:03 +00:00
Rafael Espindola a9263c8963 Fix lld crash introduced by r321154.
Since SyntheticSection::getParent() may return null, dereferencing
this pointer in ARMExidxSentinelSection::empty() call from
removeUnusedSyntheticSections() results in crashes when linking ARM
binaries.

Patch by vit9696!

llvm-svn: 323366
2018-01-24 19:16:31 +00:00
Rui Ueyama e2dfdbf0aa Do not pass an argument that can be easily inferred from other argument.
llvm-svn: 322416
2018-01-12 22:29:29 +00:00
George Rimar 9fc2c64b35 [ELF] - Do not use HeaderSize for conditions in PltSection.
Previously we checked (HeaderSize == 0) to find out if
PltSection section is IPLT or PLT. Some targets does not set
HeaderSize though. For example PPC64 has no lazy binding implemented
and does not set PltHeaderSize constant.

Because of that using of both IPLT and PLT relocations worked
incorrectly there (testcase is provided).

Patch fixes the issue.

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

llvm-svn: 322362
2018-01-12 09:35:57 +00:00
Rui Ueyama 0657e5c3f2 Do not use parallelForEach to call maybeCompress().
Currently LLVM's paralellForEach has a problem with reentracy.
That caused https://bugs.llvm.org/show_bug.cgi?id=35788 (lld somtimes
hangs while linking Ruby 2.4) because maybeCompress calls writeTo which
uses paralellForEach.

This patch is to avoid using paralellForEach to call maybeCompress
to workaround the issue.

llvm-svn: 322041
2018-01-08 23:12:42 +00:00
James Henderson e1689689d8 [ELF] Compress debug sections after assignAddresses and support custom layout
Previously, in r320472, I moved the calculation of section offsets and sizes
for compressed debug sections into maybeCompress, which happens before
assignAddresses, so that the compression had the required information. However,
I failed to take account of relocations that patch such sections. This had two
effects:

1. A race condition existed when a debug section referred to a different debug
section (see PR35788).
2. References to symbols in non-debug sections would be patched incorrectly.
This is because the addresses of such symbols are not calculated until after
assignAddresses (this was a partial regression caused by r320472, but they
could still have been broken before, in the event that a custom layout was used
in a linker script).

assignAddresses does not need to know about the output section size of
non-allocatable sections, because they do not affect the value of Dot. This
means that there is no longer a reason not to support custom layout of
compressed debug sections, as far as I'm aware. These two points allow for
delaying when maybeCompress can be called, removing the need for the loop I
previously added to calculate the section size, and therefore the race
condition. Furthermore, by delaying, we fix the issues of relocations getting
incorrect symbol values, because they have now all been finalized.

llvm-svn: 321986
2018-01-08 10:17:03 +00:00
Rafael Espindola 2640a0a5e5 Align SHT_NOBITS sections is they are the first on a PT_LOAD.
We normally want to ignore SHT_NOBITS sections when computing
offsets. The sh_offset of section itself seems to be irrelevant and

- If the section is in the middle of a PT_LOAD, it will make no
  difference on the computed offset of the followup section.

- If it is in the end of a PT_LOAD, we want to avoid its alignment
  changing the offset of the followup sections.

The issue is if it is at the start of the PT_LOAD. In that case we do
have to align it so that the following sections have congruent address
and offset module the page size. We were not handling this case.

This should fix freebsd kernel link.

llvm-svn: 321657
2018-01-02 16:46:30 +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
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
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 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
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
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
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
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 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
Rafael Espindola 1dd30ddd45 Make addReservedSymbols a static helper. NFC.
llvm-svn: 320175
2017-12-08 19:13:27 +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
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
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 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
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
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
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
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
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
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
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 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 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
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
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 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 8c0cdca99e [ELF] - Removed unused variable. NFC.
llvm-svn: 317316
2017-11-03 11:51:58 +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