Commit Graph

1327 Commits

Author SHA1 Message Date
Zaara Syeda 52ed6eb8d9 [ELF] Add basic support for PPC LE
This patch adds changes to start supporting the Power 64-Bit ELF V2 ABI.
This includes:
- Changing the ElfSym::GlobalOffsetTable to be named .TOC.
- Creating a GotHeader so the first entry in the .got is .TOC.
- Setting the e_flags to be 1 for ELF V1 and 2 for ELF V2

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

llvm-svn: 327871
2018-03-19 17:40:14 +00:00
Peter Smith 3d044f57d4 [ELF] Recommit 327248 with Arm using the .got for _GLOBAL_OFFSET_TABLE_
This is the same as 327248 except Arm defining _GLOBAL_OFFSET_TABLE_ to
be the base of the .got section as some existing code is relying upon it.

For most Targets the _GLOBAL_OFFSET_TABLE_ symbol is expected to be at
the start of the .got.plt section so that _GLOBAL_OFFSET_TABLE_[0] =
reserved value that is by convention the address of the dynamic section.
Previously we had defined _GLOBAL_OFFSET_TABLE_ as either the start or end
of the .got section with the intention that the .got.plt section would
follow the .got. However this does not always hold with the current
default section ordering so _GLOBAL_OFFSET_TABLE_[0] may not be consistent
with the reserved first entry of the .got.plt.

X86, X86_64 and AArch64 will use the .got.plt. Arm, Mips and Power use .got

Fixes PR36555

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

llvm-svn: 327823
2018-03-19 06:52:51 +00:00
Peter Collingbourne 5c902845e5 Revert r327248, "For most Targets the _GLOBAL_OFFSET_TABLE_ symbol is expected to be at"
This change broke ARM code that expects to be able to add
_GLOBAL_OFFSET_TABLE_ to the result of an R_ARM_REL32.

I will provide a reproducer on llvm-commits.

llvm-svn: 327688
2018-03-16 01:01:44 +00:00
Rui Ueyama db46a62e2b Implement --cref.
This is an option to print out a table of symbols and filenames.
The output format of this option is the same as GNU, so that it can be
processed by the same scripts as before after migrating from GNU to lld.

This option is mildly useful; we can live without it. But it is pretty
convenient sometimes, and it can be implemented in 50 lines of code, so
I think lld should support this option.

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

llvm-svn: 327565
2018-03-14 20:29:45 +00:00
George Rimar 2313086726 [ELF] - Restrict section offsets that exceeds file size.
This is part of PR36515.

With some linkerscripts it is possible to get file offset overlaps
and overflows. Currently LLD checks overlaps in checkNoOverlappingSections().
And also we allow broken output with --no-inhibit-exec.
Problem is that sometimes final offset of sections is completely broken
and we calculate output file size wrong and might crash.

Patch implements check to verify that there is no output section
which offset exceeds file size.

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

llvm-svn: 327376
2018-03-13 08:47:17 +00:00
Peter Smith 18aa0be36e For most Targets the _GLOBAL_OFFSET_TABLE_ symbol is expected to be at
the start of the .got.plt section so that _GLOBAL_OFFSET_TABLE_[0] =
reserved value that is by convention the address of the dynamic section.
Previously we had defined _GLOBAL_OFFSET_TABLE_ as either the start or end
of the .got section with the intention that the .got.plt section would
follow the .got. However this does not always hold with the current
default section ordering so _GLOBAL_OFFSET_TABLE_[0] may not be consistent
with the reserved first entry of the .got.plt.

X86, X86_64, Arm and AArch64 will use the .got.plt. Mips and Power use .got

Fixes PR36555

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

llvm-svn: 327248
2018-03-11 20:58:18 +00:00
Rui Ueyama 301305fd3d Use exact uint32_t for uint32_t ELF field. NFC.
llvm-svn: 326934
2018-03-07 19:25:36 +00:00
George Rimar bf3c384673 [ELF] - Adjust rangeToString to report ranges in a different format.
It was raised during the review of D43819.

LLD usually use [X, Y] for reporting ranges, like below:
"relocation R_386_16 out of range: 65536 is not in [0, 65535]"

Patch changes rangeToString() to do the same.

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

llvm-svn: 326918
2018-03-07 17:54:25 +00:00
Rui Ueyama b0084a0eee Revert r326911: Improve --warn-symbol-ordering.
This reverts commit r326911 because it was committed by accident.

llvm-svn: 326914
2018-03-07 17:24:46 +00:00
Rui Ueyama ebfab927d9 Improve --warn-symbol-ordering.
Summary:
I originally tried to simplify code and then noticed that lld doesn't
do what it tells to the user by warn(). It says "unable to order
discarded symbol" but it actually can for sections eliminated by ICF.
With this patch, lld doesn't sort such sections.

Reviewers: jhenderson, rafael

Subscribers: emaste, arichardson, llvm-commits

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

llvm-svn: 326911
2018-03-07 17:15:06 +00:00
Rui Ueyama 37ad8161e4 Do not create multiple NOTE segments.
Previously, we didn't try to make effort to put .note sections next
to each other in the output file, so two .note sections were likely
to be stored to two separate NOTE segments. That's undesirable because
we should create as few segments as possible in general.

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

llvm-svn: 326410
2018-03-01 02:31:29 +00:00
Rafael Espindola 852bd5c062 Simplify removing empty output sections.
With this the meaning of the Live bit in output sections is clear: we
have at some point added a input section into it.

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

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

llvm-svn: 326339
2018-02-28 17:38:19 +00:00
George Rimar 46045364b2 [ELF] - Format, fix mistype. NFC.
llvm-svn: 326198
2018-02-27 14:06:47 +00:00
George Rimar a77b963c90 [ELF] - Fix case of using both --icf and --symbol-ordering-file together.
Imagine that we have sections A, B, C, where A == C and
symbol ordering file containing symbols: symC, symB, symA

Previously because of ICF it was possible that final order would be
B, A or B, C. That violates order specified in ordering file.
Patch changes that.

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

llvm-svn: 326179
2018-02-27 08:26:38 +00:00
Rafael Espindola 79c23eec04 Keep flags from phantom synthetic sections.
This fixes pr36475.

I think this code can be simplified a bit, but I would like to check
in the more direct fix if we are in agreement on the direction and
then refactor.

This is not something that bfd does. The issue is not noticed in bfd
because it keeps fewer sections from the linkerscript in the output.

The reasons why it seems reasonable to do this:

- As George noticed, we would still keep the flags if the output
  section had both an empty synthetic section and a regular section
- We need an heuristic to find the flags of output sections. Using the
  flags of a synthetic section that would have been there seems a
  reasonable heuristic.

llvm-svn: 326137
2018-02-26 22:32:15 +00:00
George Rimar 0c97f4eba4 [ELF] - Fix variable name and mistype in comment. NFC.
llvm-svn: 326090
2018-02-26 15:16:03 +00:00
Simon Atanasyan a4cde9d2e2 [ELF][MIPS] Set EI_ABIVERSION flag accordingly to MIPS ABIs requirement
MIPS ABIs require that if an executable file uses non-PIC model, the
EI_ABIVERSION entry in the ELF header should be incremented from 0 to 1.
That allows obsoleted / limited dynamic linkers refuse to link them.

llvm-svn: 325890
2018-02-23 11:28:57 +00:00
George Rimar db1a062447 [ELF] - Do not remove empty output sections that are explicitly assigned to phdr in script.
This continues direction started in D43069.

We can keep sections that are explicitly assigned to segment in script.
It helps to simplify code.

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

llvm-svn: 325887
2018-02-23 10:53:04 +00:00
Fangrui Song 9cd5df0e15 [ELF] Add comment for preemptible and fix typo. NFC
Subscribers: emaste, arichardson, llvm-commits

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

llvm-svn: 325855
2018-02-23 02:05:48 +00:00
George Rimar 925529b821 [ELF] - Rewrote outdated comment. NFC.
llvm-svn: 325809
2018-02-22 17:52:43 +00:00
George Rimar 563e4f2f58 [ELF] - Introduce getInputSections() helper.
We sometimes need to iterate over input sections for a given
output section. It is not very convinent because we have to iterate
over section descriptions.
Patch introduces getInputSections helper, it simplifies things.

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

llvm-svn: 325763
2018-02-22 09:55:28 +00:00
George Rimar 5bd90e2db1 [ELF] - Simplify. NFC.
llvm-svn: 325681
2018-02-21 14:21:23 +00:00
Igor Kudrin 25f917341e [ELF] Simplify handling of AT section attribute.
This also makes the behavior close to GNU ld's.

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

llvm-svn: 325213
2018-02-15 06:13:52 +00:00
James Henderson de300e66bb [ELF] Add warnings for various symbols that cannot be ordered
There are a number of different situations when symbols are requested
to be ordered in the --symbol-ordering-file that cannot be ordered for
some reason. To assist with identifying these symbols, and either
tidying up the order file, or the inputs, a number of warnings have
been added. As some users may find these warnings unhelpful, due to how
they use the symbol ordering file, a switch has also been added to
disable these warnings.

The cases where we now warn are:

 * Entries in the order file that don't correspond to any symbol in the input
 * Undefined symbols
 * Absolute symbols
 * Symbols imported from shared objects
 * Symbols that are discarded, due to e.g. --gc-sections or /DISCARD/ linker script sections
 * Multiple of the same entry in the order file

Reviewed by: rafael, ruiu

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

llvm-svn: 325125
2018-02-14 13:36:22 +00:00
Rafael Espindola 1d76120d9a Use a stricter return type in buildSectionOrder. NFC.
We sort inside output sections, so all the sections we see should be
InputSectionBase.

I noticed the patch adding callgraph based section ordering used this
type and changing this separately makes the merge easier.

llvm-svn: 325094
2018-02-14 01:42:26 +00:00
Rui Ueyama 3cd48fb124 Remove 'z' in .zdebug when decompressing a section.
When decompressing a compressed debug section, we drop SHF_COMPRESSED
flag but we didn't drop "z" in ".zdebug" section name. This patch does
that for consistency.

This change also fixes the issue that .zdebug_gnu_pubnames are not
dropped when we are creating a .gdb_index section.

llvm-svn: 324949
2018-02-12 22:25:45 +00:00
Rafael Espindola ecfd73457c Refactor how we decide which sections to sort.
This is a bit more verbose, but it has a few advantages.

The logic on what to do with special sections like .init_array is not
duplicated. Before we would need keep isKnownNonreorderableSection in
sync.

I think with this the call graph based sorting can be implemented by
"just" returning a new order from buildSectionOrder.

llvm-svn: 324744
2018-02-09 16:09:22 +00:00
Michael J. Spencer e43db0e89e [ELF] Don't sort non reorderable sections with --symbol-ordering-file
Differential Revision: https://reviews.llvm.org/D43038

llvm-svn: 324656
2018-02-08 22:03:23 +00:00
George Rimar 3d5e86e5ee [ELF] - Remove unused synthetic sections correctly.
This is PR35740 which now crashes
because we remove unused synthetic sections incorrectly.

We can keep input section description and corresponding output
section live even if it must be empty and dead. 
This results in a crash because SHF_LINK_ORDER handling code
tries to access first section which is nullptr in this case.

Patch fixes the issue.

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

llvm-svn: 324463
2018-02-07 09:11:07 +00:00
Rafael Espindola 339dbb8af2 Convert a use of Config->Static.
In lld this was the only use of Config->Static where it meant anything
else other than "use .a instead of .so".

If a program turns out to not use any dynamic libraries, we should
produce the same result with and without -static.

llvm-svn: 324421
2018-02-06 22:59:24 +00:00
Rui Ueyama 6a8e79b8e5 Add -{no,}-check-sections flags to enable/disable section overlchecking
GNU linkers have this option.

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

llvm-svn: 324150
2018-02-02 22:24:06 +00:00
Alexander Richardson 6b367faa45 [ELF] Make overlapping output sections an error
Summary:
While trying to make a linker script behave the same way with lld as it did
with bfd, I discovered that lld currently doesn't diagnose overlapping
output sections. I was getting very strange runtime failures which I
tracked down to overlapping sections in the resulting binary. When linking
with ld.bfd overlapping output sections are an error unless
--noinhibit-exec is passed and I believe lld should behave the same way
here to avoid surprising crashes at runtime.

The patch also uncovered an errors in the tests: arm-thumb-interwork-thunk
was creating a binary where .got.plt was placed at an address overlapping
with .got.

Reviewers: ruiu, grimar, rafael

Reviewed By: ruiu

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

llvm-svn: 323856
2018-01-31 09:22:44 +00:00
Rafael Espindola c7945c827d Move function to the file where it is used.
llvm-svn: 323780
2018-01-30 16:24:04 +00:00
Rafael Espindola 22d533568b Sort orphan section if --symbol-ordering-file is given.
Before this patch orphan sections were not sorted.

llvm-svn: 323779
2018-01-30 16:20:08 +00:00
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