Commit Graph

5451 Commits

Author SHA1 Message Date
Sean Fertile 367a7b7d66 [PPC64] Add toc-optimizations for got based relocations.
Differential Revision: https://reviews.llvm.org/D54907

llvm-svn: 349772
2018-12-20 17:00:33 +00:00
Rui Ueyama 392f0b2b71 Simplify. NFC.
Differential Revision: https://reviews.llvm.org/D55903

llvm-svn: 349697
2018-12-19 23:25:02 +00:00
George Rimar 751e6e1cf5 [LLD][ELF] - Report a location for symbols from the linker script when reporting an error.
When we report an error for symbols defined in the linker script,
we do not report the location properly.

For example:

ld.lld: error: relocation R_AARCH64_CALL26 cannot refer to absolute symbol: aliasto__text
>>> defined in <internal>
>>> referenced by rtoabs.o:(.text+0x4)

This patch fixes that.

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

llvm-svn: 349612
2018-12-19 10:19:40 +00:00
Rui Ueyama 2d7d128117 Use unique_ptr to manage a TarWriter instance. NFC.
llvm-svn: 349581
2018-12-18 23:50:37 +00:00
Rui Ueyama 5961809640 Fix error message.
Previously, this code printed out an error message like this

  ld.lld: error: --reproduce: failed to open /foo: cannot open /foo

Apparently "failed to open /foo:" part is redundant.

llvm-svn: 349571
2018-12-18 23:33:10 +00:00
Rui Ueyama a01246b7a7 Reject .so files if -static is given.
Previously, if you pass -static to lld, lld searches for only foo.a
and skips foo.so for -lfoo option. However, it didn't reject .so files
if you directly pass their pathnames via the command line, which is a bug.

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

llvm-svn: 349557
2018-12-18 22:30:23 +00:00
Fangrui Song 41031d97f2 [ELF] Place .note in the first page to ensure they are available in core files
Summary:
Other large sections (e.g. .rela.dyn .dynstr) may push .note.* off the
first page. They won't be available in core files if RLIMIT_CORE is
limited.

This patch gives priority to alloctable SHT_NOTE sections so that they
are assuredly in the first page and will be available in core files.
They are small and contain important information (e.g. .note.gnu.build-id
identifies the origin of the core, .note.tag stores NT_FREEBSD_ABI_TAG).

Note: gold Output_section_order has a similar rule:

  // Loadable read-only note sections come next so that the PT_NOTE
  // segment is on the first page of the executable.
  ORDER_RO_NOTE,

Reviewers: ruiu, pcc, espindola

Subscribers: emaste, arichardson, llvm-commits

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

llvm-svn: 349524
2018-12-18 19:16:37 +00:00
Sean Fertile 09a5bc0107 [PPC64] Support got-based relocations.
Differential Revison: https://reviews.llvm.org/D54859

llvm-svn: 349511
2018-12-18 17:34:26 +00:00
Peter Collingbourne 4ca86d289f ELF: AArch64: Fix errata patch address calculation.
The code here wants the output section offset of the instruction
requiring the errata patch, not the virtual address. Without this
change we can end up placing a patch out of range if the virtual
address of the code section is large enough.

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

llvm-svn: 349386
2018-12-17 20:15:12 +00:00
Peter Smith 6ece0ad429 [ELF][ARM] Add support for architecture v6m thunks
ARM Architecture v6m is used by the smallest microcontrollers such as the
cortex-m0. It is Thumb only (no Thumb 2) which prevents it from using the
existing Thumb 2 range extension thunks as these use the Thumb 2 movt/movw
instructions. Range extension thunks are not usually needed for
microcontrollers due to the small amount of flash and ram on the device,
however if code is copied from flash into ram then a range extension thunk
is required to call that code.

This change adds support for v6m range extension thunks. The procedure call
standard APCS permits a thunk to corrupt the intra-procedural scratch
register r12 (referred to as ip in the APCS). Most Thumb instructions do
not permit access to high registers (r8 - r15) so the thunks must spill
some low registers (r0 - r7) to perform the control transfer.

Fixes pr39922

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

llvm-svn: 349337
2018-12-17 10:33:47 +00:00
Peter Collingbourne ae15e7232a ELF: Handle R_ARM_V4BX correctly in PIC output files.
Previously we considered R_ARM_V4BX to be an absolute relocation,
which meant that we rejected it in read-only sections in PIC output
files. Instead, treat it as a hint relocation so that relocation
processing ignores it entirely.

Also fix a problem with the test case where it was never being run
because it has a .yaml extension and we don't run tests with that
extension.

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

llvm-svn: 349216
2018-12-15 00:20:17 +00:00
Rui Ueyama 9f49990976 Add --plugin-opt=emit-llvm option.
`--plugin-opt=emit-llvm` is an option for LTO. It makes the linker to
combine all bitcode files and write the result to an output file without
doing codegen. Gold LTO plugin has this option.

This option is being used for some post-link code analysis tools that
have to see a whole program but don't need to see them in the native
machine code.

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

llvm-svn: 349198
2018-12-14 21:58:49 +00:00
Peter Smith fe3015d164 [ELF][AArch64] Fix adrp to undefined weak reference.
In the ABI for the 64-bit Arm architecture the section on weak references
states:
During linking, the symbol value of an undefined weak reference is:
- Zero if the relocation type is absolute
- The address of the place if the relocation type is pc-relative.

The relocations associated with an ADRP are relative so we should resolve
the undefined weak reference to the place instead of 0. This matches GNU
ld.bfd behaviour.

fixes pr34928

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

llvm-svn: 349024
2018-12-13 11:13:01 +00:00
George Rimar 1f958ed269 [LLD][ELF] - Support discarding the .dynamic section.
This is a part of https://bugs.llvm.org/show_bug.cgi?id=39810.

Seems it turns out that supporting /DISCARD/ for the .dynamic section with the
linker script is something we can do easily. The patch does this.

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

llvm-svn: 348749
2018-12-10 09:24:49 +00:00
George Rimar ad667661c4 [ELF] - Allow discarding .dynsym from the linker script.
This is a part of https://bugs.llvm.org/show_bug.cgi?id=39810.
The patch allows discarding the .dynsym section using linker script.

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

llvm-svn: 348748
2018-12-10 09:13:36 +00:00
George Rimar 4af28e46ca [LLD][ELF] - Support discarding .dynstr section.
This is a part of https://bugs.llvm.org/show_bug.cgi?id=39810.
The patch allows discarding the .dynstr section using linker script.

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

llvm-svn: 348746
2018-12-10 09:07:30 +00:00
George Rimar e1fd3f940b [LLD][ELF] - Use auto. NFC.
This addresses the missed review comment.

llvm-svn: 348480
2018-12-06 10:56:11 +00:00
George Rimar b3be390f94 [ELF] - (-Map file) Implement printing of LMA for assignments outside of section declarations.
This was a missing piece.
We started to print LMAs and information about assignments,
but did not do that for assignments outside of section declarations yet.
The patch implements it.

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

llvm-svn: 348468
2018-12-06 09:04:52 +00:00
George Rimar f49fe218c2 [LLD][ELF] - Linker script: accept using a file name without a list of sections.
This is a part of
https://bugs.llvm.org/show_bug.cgi?id=39885

Linker script specification says:
"You can specify a file name to include sections from a particular file. You would
do this if one or more of your files contain special data that needs to be at a
particular location in memory."

LLD did not accept this syntax. The patch implements it.

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

llvm-svn: 348463
2018-12-06 08:34:52 +00:00
Rui Ueyama c9c34bdc1a Do not use a hash table to uniquify mergeable strings.
Previously, we have a hash table containing strings and their offsets
to manage mergeable strings. Technically we can live without that, because
we can do binary search on a vector of mergeable strings to find a mergeable
strings.

We did have both the hash table and the binary search because we thought
that that is faster.

We recently observed that lld tend to consume more memory than gold when
building an output with debug info. A few percent of memory is consumed by
the hash table. So, we needed to reevaluate whether or not having the extra
hash table is a good CPU/memory tradeoff. I run a few benchmarks with and
without the hash table.

I got a mixed result for the benchmark. We observed a regression for some
programs by removing the hash table (that's what we expected), but we also
observed that performance imrpovements for some programs. This is perhaps
due to reduced memory usage.

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

llvm-svn: 348401
2018-12-05 19:13:31 +00:00
Fangrui Song 01fbb06b12 [ELF] Simplify getSectionPiece
Reviewers: ruiu, espindola

Reviewed By: ruiu

Subscribers: grimar, emaste, arichardson, llvm-commits

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

llvm-svn: 348311
2018-12-04 22:25:05 +00:00
Rui Ueyama e84f14ee39 Remove unreachable code.
llvm-svn: 348294
2018-12-04 19:00:56 +00:00
Rui Ueyama a592aeb35a ELF: allow non allocated sections to go into allocated sections
Patch from Andrew Kelley.

For context, see https://bugs.llvm.org/show_bug.cgi?id=39862

The use case is embedded / OS programming where the kernel wants
access to its own debug info via mapped dwarf info. I have a proof of
concept of this working, using this linker script snippet:

  .rodata : ALIGN(4K) {
    *(.rodata)
    __debug_info_start = .;
    KEEP(*(.debug_info))
    __debug_info_end = .;
    __debug_abbrev_start = .;
    KEEP(*(.debug_abbrev))
    __debug_abbrev_end = .;
    __debug_str_start = .;
    KEEP(*(.debug_str))
    __debug_str_end = .;
    __debug_line_start = .;
    KEEP(*(.debug_line))
    __debug_line_end =
    .;
    __debug_ranges_start
    = .;
    KEEP(*(.debug_ranges))
    __debug_ranges_end
    = .;
  }

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

llvm-svn: 348291
2018-12-04 18:47:44 +00:00
Martell Malone 15b6c453b8 [ELF] Allow discarding of .rela.plt
When linking the linux kernel on ppc64le

ld.lld -EL -m elf64lppc -Bstatic --orphan-handling=warn --build-id -o
.tmp_vmlinux1 -T ./arch/powerpc/kernel/vmlinux.lds --whole-archive
built-in.a --no-whole-archive --start-group lib/lib.a --end-group
ld.lld: error: discarding .rela.plt section is not allowed

The linker script discards with the following matches
*(.glink .iplt .plt .rela* .comment)

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

llvm-svn: 348258
2018-12-04 12:37:56 +00:00
Martell Malone aa6086a64c [PPC][PPC64] PPC_REL14 and PPC64_REL14 relocations
When linking the linux kernel on ppc64 and ppc
ld.lld: error: unrecognized reloc 11
11 is PPC_REL14 and PPC64_REL14

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

llvm-svn: 348255
2018-12-04 12:26:21 +00:00
Rui Ueyama 3b6cd2c602 Show a proper error message if output file is too large.
At least on Linux, if a file size given to FileOutputBuffer is greater
than 2^63, it fails with "Invalid argument" error, which is not a
user-friendly error message. With this patch, lld prints out "output
file too large" instead.

llvm-svn: 348153
2018-12-03 17:42:57 +00:00
George Rimar 89230f7bc2 [LLD][ELF] - Remove the excessive condition. NFC.
There is no need to check that In.DynSymTab != nullptr,
because `includeInDynsym` already checks for `!Config->HasDynSymTab`
and `HasDynSymTab` is the pre-condition for In.DynSymTab creation.

llvm-svn: 348143
2018-12-03 14:44:02 +00:00
George Rimar c2bea66cf2 [LLD][ELF] - Improve the DWARF v5 suport for building .gdb_index.
Now LLD might build the broken/incomplete .gdb_index when some DWARF v5
sections (like .debug_rnglists and .debug_addr) are used.

Particularly, for the case above, we emit an empty address area. 
A test case is provided and patch fixes the issue.

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

llvm-svn: 348119
2018-12-03 10:48:57 +00:00
Rui Ueyama aea706083f Inline a function template that is used only once. NFC.
llvm-svn: 348013
2018-11-30 18:19:15 +00:00
Peter Smith fd8aeb2c2a [LLD][ELF] Error if _GLOBAL_OFFSET_TABLE_ is defined in input objects
The _GLOBAL_OFFSET_TABLE_ is a linker defined symbol that is placed at
some location relative to the .got, .got.plt or .toc section. On some
targets such as Arm the correctness of some code sequences using a
relocation to _GLOBAL_OFFSET_TABLE_ depend on the value of the symbol
being in the linker defined place. Follow the ld.gold example and give
a multiple symbol definition error. The ld.bfd behaviour is to ignore the
definition in the input object and redefine it, which seems like it could
be more surprising.

fixes pr39587

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

llvm-svn: 347854
2018-11-29 11:18:07 +00:00
Fangrui Song f2143761d6 [ELF] --gdb-index: use lower_bound to compute relative CU index in the object file
Summary:
This reinstates what I originally intended to do in D54361.
It removes the assumption that .debug_gnu_pubnames has increasing CuOffset.

Now we do better than gold here: when .debug_gnu_pubnames contains
multiple sets, gold would think every set has the same CU index as the
first set (incorrect).

Reviewed By: ruiu

Reviewers: ruiu, dblaikie, espindola

Subscribers: emaste, arichardson, arphaman, llvm-commits

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

llvm-svn: 347820
2018-11-29 00:17:00 +00:00
Rui Ueyama 63d397ea6e Simplify Symbol::getPltVA.
This patch also makes getPltEntryOffset a non-member function because
it doesn't depend on any private members of the TargetInfo class.

I tried a few different ideas, and it seems this change fits in best to me.

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

llvm-svn: 347781
2018-11-28 17:42:59 +00:00
Haojian Wu ac6e1fb89d Fix a typo.
llvm-svn: 347751
2018-11-28 13:20:39 +00:00
Haojian Wu 00e82a6f66 Fix build error.
llvm-svn: 347750
2018-11-28 13:15:06 +00:00
Simon Atanasyan 69c61200a9 [ELF][MIPS] Handle mips in the OUTPUT_FORMAT directive
Differential Revision: http://reviews.llvm.org/D54920

llvm-svn: 347742
2018-11-28 11:41:20 +00:00
Peter Smith 7dc5af75ae [ELF] Use more specific method to calculate DT_PLTRELSZ
The DT_PLTRELSZ dynamic tag is calculated using the size of the
OutputSection containing the In.RelaPlt InputSection. This will work for the
default no linker script case and the majority of linker scripts.
Unfortunately it doesn't work for some 'almost' sensible linker scripts. It
is permitted by ELF to have a single OutputSection containing both
In.RelaDyn, In.RelaPlt and In.RelaIPlt. It is also permissible for the range
of memory [DT_RELA, DT_RELA + DT_RELASZ) and the range
[DT_JMPREL, DT_JMPREL + DT_JMPRELSZ) to overlap as long as the the latter
range is at the end.

To support this type of linker script use the specific InputSection sizes.

Fixes pr39678

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

llvm-svn: 347736
2018-11-28 10:04:55 +00:00
Rui Ueyama 418bcd0830 Make a member function non-member. NFC.
llvm-svn: 347678
2018-11-27 17:47:24 +00:00
George Rimar d2f8db827d [ELF] - Fix R_AARCH64_ADR_GOT_PAGE, R_AARCH64_LD64_GOT_LO12 handling against IFUNC symbols.
This is https://bugs.llvm.org/show_bug.cgi?id=38074.

The issue is that when calling a function, LLD generates a
.got entry that points to the IFUNC resolver function when
instead, it should use the PLT entries properly for
handling the IFUNC.

So we should create a got entry that points to PLT entry,
which itself loads the value from 
.got.plt, relocated with R_*_IRELATIVE to make things work.
Patch do that.

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

llvm-svn: 347650
2018-11-27 10:30:46 +00:00
Peter Smith 8a3e7178f6 [AArch64] Cortex-a53-843419 erratum should not apply to relaxed TLS.
The changes to the instructions performed by TLS relaxation and the errata
patching are performed with relocations. As these are applied so late the
errata scanning won't see the changes in the section data made by the TLS
relaxation. This can lead to a TLS relaxed sequence being patched when it
doesn't need to be.

The fix checks to see if there is a R_RELAX_TLS_IE_TO_LE instruction at the
same address as the ADRP as this indicates the presence of a relaxation
of a sequence that might get recognised as a patch.

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

llvm-svn: 347649
2018-11-27 10:17:35 +00:00
George Rimar a1b3ddbfec [ELF] - Implement -z nodefaultlib
This is https://bugs.llvm.org//show_bug.cgi?id=38978

Spec says that:
"Objects may be built with the -z nodefaultlib option to
suppress any search of the default locations at runtime.
Use of this option implies that all the dependencies of an
object can be located using its runpaths.
Without this option, which is the most common case, no
matter how you augment the runtime linker's library
search path, its last element is always /usr/lib for 32-bit
objects and /usr/lib/64 for 64-bit objects."

The patch implements this option.

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

llvm-svn: 347647
2018-11-27 09:48:17 +00:00
Peter Collingbourne c81be65166 ELF: ICF: Include contents of referenced sections in initial partitioning hash. NFCI.
On my machine this reduced median link time of lld-speed-test/chrome
from 2.68s to 2.41s. It also reduces link time of Chrome for Android
with a prototype compiler change that causes the compiler to create
large numbers of identical (modulo relocations) sections from >15
minutes to a few seconds.

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

llvm-svn: 347594
2018-11-26 21:15:47 +00:00
George Rimar c152281632 [LLD][ELF] - Do not crash when parsing the -defsym option from a error state.
When we are in a error state, script parser will not parse the -defsym
expression and hence will not tokenize it. Then ScriptLexer::Pos will be 0
and LLD will assert and crash here:

MemoryBufferRef ScriptLexer::getCurrentMB() {
  assert(!MBs.empty() && Pos > 0); // Bang !

Solution - stop parsing the defsym in a error state. That is consistent
with the regular case (when we parse the linker script).

llvm-svn: 347549
2018-11-26 12:29:56 +00:00
George Rimar a44c0f27c2 [LLD][ELF] - Remove the excessive safety return. NFC.
We explicitly call finalizeContents() only once for
DynamicSection. The code testing we do not do it twice is
just excessive.

It could be an assert, but we don't do
that for other sections, so does not seem we
should do it here too.

llvm-svn: 347543
2018-11-26 10:33:29 +00:00
George Rimar 0e0cd5be40 [LLD][ELF] - Add llvm_unreachable. NFC.
We never should call writeTo() for BSS section.

llvm-svn: 347540
2018-11-26 10:07:10 +00:00
George Rimar 0fc5dcd1c8 [LLD][ELF] - Simplify. NFCI.
This makes getRISCVPCRelHi20 to be static local helper,
and rotates the 'if' condition.

llvm-svn: 347497
2018-11-23 15:13:26 +00:00
George Rimar 94a16cb611 [ELF] - Make SymbolTable::addDefined return Defined.
Now it returns Symbol. This should be NFC that
avoids doing cast at the caller's sides.

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

llvm-svn: 347455
2018-11-22 11:40:08 +00:00
Fangrui Song f5badf4905 [ELF] Write IPLT header in -static -z retpolineplt mode
Summary:
This fixes PR39711: -static -z retpolineplt does not produce retpoline PLT header.
-z now is not relevant.

Statically linked executable does not have PLT, but may have IPLT with no header. When -z retpolineplt is specified, however, the repoline PLT header should still be emitted.

I've checked that this fixes the FreeBSD reproduce in PR39711 and a Linux program statically linked against glibc. The programm print "Hi" rather than SIGILL/SIGSEGV.

getPltEntryOffset may look dirty after this patch, but it can be cleaned up later.

Another possible improvement is that when there are non-preemptible IFUNC symbols (rare case, e.g. -Bsymbolic), both In.Plt and In.Iplt can be non-empty and we'll emit the retpoline PLT header twice.

Reviewers: espindola, emaste, chandlerc, ruiu

Reviewed By: emaste

Subscribers: emaste, arichardson, krytarowski, llvm-commits

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

llvm-svn: 347404
2018-11-21 18:10:00 +00:00
Fangrui Song 537a5bfb7f [ELF] Allow --noinhibit-exec to produce corrupted executable with relocation overflow
Summary:
When --noinhibit-exec is used, ld.bfd/gold emit errors but allow to produce corrupted executable, which is handy for debugging purpose. lld's --noinhibit-exec has a different meaning and changes some errors to warnings. This patch replaces "error" with "errorOrWarn" to exploit that property.

We may revisit this: if we should keep them as errors (as ld.bfd/gold do) but allow to produce a (corrupted) executable.

Reviewers: ruiu, grimar, espindola

Reviewed By: grimar

Subscribers: Timmmm, jhenderson, emaste, arichardson, llvm-commits

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

llvm-svn: 347327
2018-11-20 17:04:11 +00:00
Fangrui Song 113d868ff1 Support ARM_V4BX relocation
Summary: This patch implementation the handler for ARM_V4BX. This relocation is used by GNU runtime files and other armv4 applications.

Patch by Yin Ma

Reviewers: espindola, MaskRay, ruiu, peter.smith, pcc

Reviewed By: ruiu

Subscribers: yinma, pcc, peter.smith, MaskRay, rovka, efriedma, emaste, javed.absar, arichardson, kristof.beyls, chrib, llvm-commits

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

llvm-svn: 347077
2018-11-16 19:24:45 +00:00
Simon Atanasyan 275a5b442b [ELF][MIPS] Use MIPS R6 `sigrie` as a trap instruction
Current value using as a trap instruction (0xefefefef) is not a good choice
for MIPS because it's a valid MIPS instruction `swc3 $15,-4113(ra)`. This
patch replaces 0xefefefef by 0x04170001. For all MIPS ISA revisions before
R6, this value is just invalid instruction. Starting from MIPS R6 it's
a valid instruction `sigrie 1` which signals a Reserved Instruction exception.

mips-traps.s test case is added to test trap encoding. Other test cases
are modified to remove redundant checking.

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

llvm-svn: 347029
2018-11-16 05:30:47 +00:00