Commit Graph

11835 Commits

Author SHA1 Message Date
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
Alexandre Ganea 66894975b2 [PDB] Quote linker arguments containing spaces (mimic MSVC)
Initial patch by Will Wilson (@lantictac)

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

llvm-svn: 348001
2018-11-30 16:36:40 +00:00
Rui Ueyama c310742dc3 Do not assume .idata is zero-initialized.
We initialize .text section with 0xcc (INT3 instruction), so we need to
explicitly write data even if it is zero if it can be in a .text section.
If you specify /merge:.rdata=.text, .rdata (which contains .idata) is put
to .text, so we need to do this.

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

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

llvm-svn: 348000
2018-11-30 16:34:56 +00:00
Sam Clegg 0f90191faa [WebAssembly] Allow undefined symbols when building shared libraries
Differential Revision: https://reviews.llvm.org/D55043

llvm-svn: 347909
2018-11-29 20:07:13 +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
Sam Clegg db8dd23204 [WebAssembly] Update docs
This is an reland of rL343155 which got reverted because
of a sphinx failure on the buildbot.

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

llvm-svn: 347830
2018-11-29 02:55:25 +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
Martin Storsjo 333e0d180f [COFF] Remove empty sections before calculating the size of section headers
The number of sections is used in assignAddresses (in
finalizeAddresses) and the space for all sections is permanent from
that point on, even if we later decide we won't write some of them.

The VirtualSize field also gets calculated in assignAddresses, so we
need to manually check whether the section is empty here instead.

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

llvm-svn: 347704
2018-11-27 20:48:09 +00:00
Reid Kleckner 291d015de4 [PDB] Add symbol records in bulk
Summary:
This speeds up linking clang.exe/pdb with /DEBUG:GHASH by 31%, from
12.9s to 9.8s.

Symbol records are typically small (16.7 bytes on average), but we
processed them one at a time. CVSymbol is a relatively "large" type. It
wraps an ArrayRef<uint8_t> with a kind an optional 32-bit hash, which we
don't need. Before this change, each DbiModuleDescriptorBuilder would
maintain an array of CVSymbols, and would write them individually with a
BinaryItemStream.

With this change, we now add symbols that happen to appear contiguously
in bulk. For each .debug$S section (roughly one per function), we
allocate two copies, one for relocation, and one for realignment
purposes. For runs of symbols that go in the module stream, which is
most symbols, we now add them as a single ArrayRef<uint8_t>, so the
vector DbiModuleDescriptorBuilder is roughly linear in the number of
.debug$S sections (O(# funcs)) instead of the number of symbol records
(very large).

Some stats on symbol sizes for the curious:
  PDB size: 507M
  sym bytes: 316,508,016
  sym count:  18,954,971
  sym byte avg: 16.7

As future work, we may be able to skip copying symbol records in the
linker for realignment purposes if we make LLVM write them aligned into
the object file. We need to double check that such symbol records are
still compatible with link.exe, but if so, it's definitely worth doing,
since my profile shows we spend 500ms in memcpy in the symbol merging
code. We could potentially cut that in half by saving a copy.
Alternatively, we could apply the relocations *after* we iterate the
symbols. This would require some careful re-engineering of the
relocation processing code, though.

Reviewers: zturner, aganea, ruiu

Subscribers: hiraditya, llvm-commits

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

llvm-svn: 347687
2018-11-27 19:00:23 +00:00
Rui Ueyama e3459295ee Remove SaveAndRestore and SmallVectorImpl from lld/Common/LLVM.h. NFC.
llvm-svn: 347680
2018-11-27 17:54:15 +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
Martin Storsjo 3c046af5a9 [COFF] Generate a codeview build id signature for MinGW even when not creating a PDB
GNU ld, which doesn't generate PDBs, can optionally generate a
build id by passing the --build-id option. LLD's MinGW frontend knows
about this option but ignores it, as I had falsely assumed that LLD
already generated build IDs even in those cases.

If debug info is requested and no PDB path is set, generate a
build id signature as a hash of the binary itself. This allows
associating a binary to a minidump, even if debug info isn't
written in PDB form by the linker.

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

llvm-svn: 347645
2018-11-27 09:20:55 +00:00
Reid Kleckner a37d672da9 [COFF] Add exported functions to gfids table for /guard:cf
Summary:
MSVC does this, and we should to.

The .gfids table is a table of RVAs, so it's impossible for a DLL to
indicate that an imported symbol is address taken. Therefore, exports
appear to be listed as address taken by the DLL that exports them.

This fixes an issue that Firefox ran into here:
https://bugzilla.mozilla.org/show_bug.cgi?id=1485016#c12

In Firefox, the export directive came from a .def file, but we need to
do this for any kind of export.

Reviewers: dmajor, hans, amccarth, alex

Subscribers: llvm-commits

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

llvm-svn: 347623
2018-11-27 01:50:17 +00:00
Sam Clegg 45218f4af9 [WebAssembly] Remove `using` statements from header files. NFC.
Differential Revision: https://reviews.llvm.org/D54758

llvm-svn: 347621
2018-11-27 01:08:16 +00:00
Fangrui Song 41828e7e52 [Common] Threads: use function_ref instead of std::function
The benefits are:

a) Performance (very minor): it removes a heap allocation of the std::function constructor (template<class F> function(F f))
b) Clarity: it suggests that the callable's lifetime should end after the callee returns. Such callback is widely used in llvm. lld also uses it a lot.

Reviewers: ruiu, rnk, pcc

Reviewed By: ruiu

Subscribers: llvm-commits

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

llvm-svn: 347620
2018-11-27 00:26:50 +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
Fangrui Song 4ed350d6c4 [COFF] ICF: use parallelForEach{,N}
Summary: They have an additional `ThreadsEnabled` check, which does not matter much.

Reviewers: pcc, ruiu, rnk

Reviewed By: ruiu

Subscribers: llvm-commits

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

llvm-svn: 347587
2018-11-26 20:07:07 +00:00
George Rimar 6bc22e1b8c [LLD][ELF] - Added a test for "-image-base: number expected" message. NFC.
We had no such test.

llvm-svn: 347553
2018-11-26 13:15:57 +00:00
George Rimar e9ab0d0650 [LLD][ELF] - Add a test for "unbalanced --push-state/--pop-state" error.
We had no such test.

llvm-svn: 347552
2018-11-26 12:58:51 +00:00
George Rimar 15b8cd9190 [LLD][ELF] - Add a check for --split-stack-adjust-size error message. NFCI.
"--split-stack-adjust-size: size must be >= 0" message
was never tested.

llvm-svn: 347550
2018-11-26 12:36:16 +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 717412b5f9 [ELF] - Added test case for invalid relocation target errors. NFCI.
We had a proper error reporting, but no test cases.

llvm-svn: 347536
2018-11-26 09:25:08 +00:00
George Rimar 294c0135ce [LLD][ELF] - Add a test for non-null terminated mergeable strings section. NFCI.
LLD reports an error in this case, but we had no test.

llvm-svn: 347535
2018-11-26 08:40:20 +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 5f8fc76b4e [LLD][ELF] - Fix mistype. NFC.
llvm-svn: 347485
2018-11-23 07:59:28 +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
Peter Collingbourne b007cabb87 COFF: ICF: Include contents of referenced sections in initial partitioning hash. NFCI.
Previously we were taking over 13 minutes to link Firefox's xul.dll
on ARM64; this reduces link time to around 18s on my machine.

The root cause of the problem was that all of the input .pdata sections
had the same unrelocated section data and therefore the same hash,
which made segregation quadratic in the number of .pdata sections. The
reason why we weren't observing this on other architectures was that
ARM has a different .pdata format. On non-ARM the format is (start
address, end address, .xdata), which caused the size of the function
to appear in the unrelocated section data where the end address field
is. However, the ARM format omits the end address field.

Fixes PR39667.

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

llvm-svn: 347429
2018-11-21 21:29:35 +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
Peter Smith 4923930be5 [ARM] Change REQUIRES: ARM to Requires: arm
When REQUIRES: ARM is used the test is skipped as ARM is not recognized.
Change to REQUIRES: arm so that it is run. This required updating one of the
tests due to changes in expected output.

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

llvm-svn: 347388
2018-11-21 11:30:03 +00:00
Heejin Ahn b6254c515f [WebAssembly] Delete unused using statements (NFC)
Reviewers: sbc100, dschuff

Subscribers: mehdi_amini, jgravelle-google, sunfish, steven_wu, dexonsmith, llvm-commits

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

llvm-svn: 347370
2018-11-21 00:51:37 +00:00
Zachary Turner d16944eefe [CodeView] RelocPtr points to little endian data.
Don't use a uint32_t*, use a ulittle32_t* to make this correct
on big endian systems.

Patch by James Clarke
Differential Revision: https://reviews.llvm.org/D54421

llvm-svn: 347349
2018-11-20 21:30:11 +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
Heejin Ahn 6f4286fb97 [WebAssembly] Fix inaccurate comments / assertion messages
Reviewers: sbc100

Subscribers: dschuff, jgravelle-google, sunfish, llvm-commits

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

llvm-svn: 347273
2018-11-19 23:31:28 +00:00
Heejin Ahn aeaab9925e [WebAssembly] Make starting indices calcaulation simpler (NFC)
Summary:
At the beginning of `assignIndexes() function, when `FunctionIndex` and
`GlobalIndex` variables are created, `InputFunctions` and `InputGlobals`
vectors are guaranteed to be empty, because those vectors are only
populated in `assignIndexes()` function. Current code looks like they
are nonempty, so this patch deletes them for better readability.

Reviewers: sbc100

Subscribers: dschuff, jgravelle-google, sunfish, llvm-commits

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

llvm-svn: 347272
2018-11-19 23:21:25 +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
Sam Clegg 2dad4e2c6d [WebAssembly] Import the stack pointer when building shared libraries
Differential Revision: https://reviews.llvm.org/D54558

llvm-svn: 346974
2018-11-15 18:15:54 +00:00
Sam Clegg d5f650631c [WebAssembly] Refactor config setting and checking. NFC.
This matches the way the ELF backend does it.

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

llvm-svn: 346972
2018-11-15 18:09:41 +00:00
George Rimar 8329028b49 [ELF] - Renamed few more AArch64 specific relocation expressions. NFC.
They are AArch64 only, so have to have AARCH64_* prefix.

llvm-svn: 346963
2018-11-15 15:35:44 +00:00
Martin Storsjo 0db45d954b [ELF] Fix compilation with GCC 5
Remove the default initializer for TrapInstr; all subclasses overwrite
the defaults in their constructors anyway.

This fixes compilation errors like these, with GCC 5.4 on Ubuntu 16.04,
present since SVN r346893:

 In file included from ../tools/lld/ELF/Arch/AArch64.cpp:12:0:
 ../tools/lld/ELF/Target.h:125:49: error: array must be initialized with a brace-enclosed initializer
    std::array<uint8_t, 4> TrapInstr = {0, 0, 0, 0};
                                                  ^
 ../tools/lld/ELF/Target.h:125:49: error: too many initializers for ‘std::array<unsigned char, 4ul>’

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

llvm-svn: 346934
2018-11-15 08:20:18 +00:00
Sam Clegg bfb75348e2 [WebAssembly] Initial support for shared objects (-shared)
Based on the initial spec proposal:
https://github.com/WebAssembly/tool-conventions/blob/master/DynamicLinking.md

The llvm/codegen side of this is still missing but I believe this change is
still worth landing as an incremental step

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

llvm-svn: 346918
2018-11-15 00:37:21 +00:00
Simon Atanasyan b0486051d2 [ELF] Make TrapInstr and Filler byte arrays. NFC.
The uint32_t type does not clearly convey that these fields are interpreted
in the target endianness. Converting them to byte arrays should make this
more obvious and less error-prone.

Patch by James Clarke

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

llvm-svn: 346893
2018-11-14 21:05:20 +00:00
Sean Fertile 614dc11ca8 [PPC64] Long branch thunks.
On PowerPC64, when a function call offset is too large to encode in a call
instruction the address is stored in a table in the data segment. A thunk is
used to load the branch target address from the table relative to the
TOC-pointer and indirectly branch to the callee. When linking position-dependent
code the addresses are stored directly in the table, for position-independent
code the table is allocated and filled in at load time by the dynamic linker.

For position-independent code the branch targets could have gone in the .got.plt
but using the .branch_lt section for both position dependent and position
independent binaries keeps it consitent and helps keep this PPC64 specific logic
seperated from the target-independent code handling the .got.plt.

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

llvm-svn: 346877
2018-11-14 17:56:43 +00:00
Sean Fertile 36eef925c0 [NFC] Change address __tls_getaddr is defined at so it does not need a thunk.
Minor update to a ppc64 tls test so that it won't need to use a thunk once the
range-extending thunk patch is landed.

llvm-svn: 346876
2018-11-14 17:56:38 +00:00
Peter Smith ad51cee866 [AArch64] Fix resolution of R_PLT_PAGE RelExpr
The R_AARCH64_ADR_PREL_PG_HI21 relocation type is given the R_PAGE_PC
RelExpr. This can be transformed to R_PLT_PAGE_PC via toPlt().
Unfortunately the resolution is identical to R_PAGE_PC so instead of
getting the address of the PLT entry we get the address of the symbol
which may not be correct in the case of static ifuncs. The fix is to
handle the cases separately and use getPltVA() + A with R_PLT_PAGE_PC.

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

llvm-svn: 346863
2018-11-14 13:53:47 +00:00
Martin Storsjo 49037d2b3c [COFF] Fix a longstanding typo in a variable name. NFC.
llvm-svn: 346846
2018-11-14 10:26:47 +00:00
Diana Picus 8b56ab4561 Fix r346747 and r346796
Require x86 for the tests in order to fix non-x86 bots. This seems to be
the case for all other tests in that directory.

llvm-svn: 346842
2018-11-14 09:58:17 +00:00
Reid Kleckner 944843c880 [PDB] Simplify symbol handling code, NFC
- Make mergeSymbolRecords a method of PDBLinker to reduce the number of
parameters it needs.

- Remove a stale FIXME comment about error handling. We already drop
unknown symbol records, log them, and continue.

- Update a comment about why we're copying the symbol record. We do it
to realign the record. We can already mutate the symbol record memory,
it's memory allocated by relocateDebugChunk.

- Avoid the extra `CVSymbol NewSym` variable. We can mutate Sym in
place, which is best, since we're mutating the underlying record anyway.

llvm-svn: 346817
2018-11-13 23:44:39 +00:00
Fangrui Song c2858f63e5 [ELF] Add a better test for the multi-CU .gdb_index bug that D54361 fixed
gdb-index-multiple-cu-2.s puts the symbol in question to another object file %t1.o, so that its CuIndex is affected by the number of CUs in %t.o

Also change `Kind:` in a comment to `Attributes:` as a follow-up of D54480 and D54481

llvm-svn: 346796
2018-11-13 20:49:36 +00:00
Fangrui Song 0736461b24 [ELF] Rename NameTypeEntry to NameAttrEntry and its field "Type" to CuIndexAndAttrs
Summary:
NameTypeEntry::Type is a bit-packed value of CU index+attributes (https://sourceware.org/gdb//onlinedocs/gdb/Index-Section-Format.html), which is named cu_index_and_attrs in a local variable in gdb/dwarf2read.c:dw2_symtab_iter_next

The new name CuIndexAndAttrs is more meaningful.

Reviewers: ruiu, dblaikie, espindola

Reviewed By: dblaikie

Subscribers: emaste, aprantl, arichardson, JDevlieghere, arphaman, llvm-commits

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

llvm-svn: 346794
2018-11-13 20:25:51 +00:00
Reid Kleckner 551acf03dc [COFF] Simplify relocation to discarded section diagnostic code, NFC
Move it out of the loop that applies relocations for readability.

llvm-svn: 346777
2018-11-13 18:30:31 +00:00
George Rimar 8ef9babb67 [ELF] - Renamed AArch64 specific relocations expressions. NFC.
They did not have AArch64 prefix. Now they do.

llvm-svn: 346749
2018-11-13 10:16:36 +00:00
Fangrui Song 9596848037 [ELF] .gdb_index: fix CuOff when a .debug_info section contains more than 1 DW_TAG_compile_unit
Summary:
Idx passed to readPubNamesAndTypes was an index into Chunks, not an
index into the CU list. This would be incorrect if some .debug_info
section contained more than 1 DW_TAG_compile_unit.

In real world, glibc Scrt1.o is a partial link of start.os abi-note.o init.o and contains 2 CUs in debug builds.
Without this patch, any application linking such Scrt1.o would have invalid .gdb_index
The issue could be demonstrated by:

    (gdb) py print(gdb.lookup_global_symbol('main'))
    None

Reviewers: espindola, ruiu

Reviewed By: ruiu

Subscribers: Higuoxing, grimar, dblaikie, emaste, aprantl, arichardson, JDevlieghere, arphaman, llvm-commits

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

llvm-svn: 346747
2018-11-13 08:43:07 +00:00
Fangrui Song 31be2f15a6 [ELF] Change GnuPub{Names,Types}Section from StringRef to LLDDWARFSection
Summary:
The debug_info_offset value may be relocated.

This is lld side change of D54375.

Reviewers: ruiu, dblaikie, grimar, espindola

Subscribers: emaste, arichardson, JDevlieghere, llvm-commits

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

llvm-svn: 346616
2018-11-11 18:57:35 +00:00
Fangrui Song f8f140dd55 [ELF] Fix relocation-common.s after rL346610
llvm-svn: 346614
2018-11-11 18:54:58 +00:00
Fangrui Song bdd036abeb [ELF] Fix objdump tests after rL346610
llvm-svn: 346613
2018-11-11 18:49:05 +00:00
Kristina Brooks 8e719269ea [lld][test] Update tests using objdump
Followup fix for LLD test for new format in rL346610. 

llvm-svn: 346612
2018-11-11 18:42:59 +00:00
Reid Kleckner 9ba2c72deb [PDB] Simplify some ghash code, NFC
Instead of calling the same function twice with different parameters,
make the parameters depend on the condition.

llvm-svn: 346578
2018-11-10 01:36:02 +00:00
Sam Clegg 37125f080f [WebAssembly] Respect `--no-mangle` in more locations
`--no-demangle` now also applies to the name section.  This change
was motivated by the rust team that have a slightly different name
mangling scheme to the standard C++ itanium one and prefer to do their
de-mangling as a post-link setp.

Patch by Alex Crichton!

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

llvm-svn: 346516
2018-11-09 16:57:41 +00:00
Reid Kleckner f3dc9649ce Fix -Wextra-qualification warning
llvm-svn: 346431
2018-11-08 18:53:56 +00:00
Reid Kleckner 7a44fe956a [COFF] Improve relocation against discarded section error
Summary:
Reuse the "referenced by" note diagnostic code that we already use for
undefined symbols. In my case, it turned this:
  lld-link: error: relocation against symbol in discarded section: .text
  lld-link: error: relocation against symbol in discarded section: .text
  ...

Into this:
  lld-link: error: relocation against symbol in discarded section: .text
  >>> referenced by libANGLE.lib(CompilerGL.obj):(.SCOVP$M)
  >>> referenced by libANGLE.lib(CompilerGL.obj):(.SCOVP$M)
  ...

  lld-link: error: relocation against symbol in discarded section: .text
  >>> referenced by obj/third_party/angle/libGLESv2/entry_points_egl_ext.obj:(.SCOVP$M)
  >>> referenced by obj/third_party/angle/libGLESv2/entry_points_egl_ext.obj:(.SCOVP$M)
  ...

I think the new output is more useful.

Reviewers: ruiu, pcc

Subscribers: llvm-commits

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

llvm-svn: 346427
2018-11-08 18:38:17 +00:00
Alexandre Ganea 4b2957243b [LLD] Fix Microsoft precompiled headers cross-compile on Linux
Differential revision: https://reviews.llvm.org/D54122

llvm-svn: 346403
2018-11-08 14:42:37 +00:00
Paul Robinson 746c22389c [DWARFv5] Read and dump multiple .debug_info sections.
Type units go in .debug_info comdats, not .debug_types, in v5.

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

llvm-svn: 346360
2018-11-07 21:39:09 +00:00
Fangrui Song db22af0f45 [PPC64] Use INT32_MIN instead of std::numeric_limits<int32_t>::min()
Summary:
D53821 fixed the bogus MSVC (at least 2017) C4146 warning (unary minus applied on unsigned type)
by using std::numeric_limits<int32_t>::min().
The warning was because -2147483648 is incorrectly treated as unsigned long instead of long long)

Let's use INT32_MIN which is arguably more readable.
Note, on GCC or clang, -0x80000000 works fine (ILP64: long, LP64: long long).

Reviewers: ruiu, jhenderson, sfertile, espindola

Reviewed By: sfertile

Subscribers: emaste, nemanjai, arichardson, kbarton, jsji, llvm-commits

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

llvm-svn: 346356
2018-11-07 21:14:54 +00:00
Jordan Rupprecht d768ecf5a7 [lld][NFC] Use explicit --symbols instead of -t in tests using llvm-readelf.
llvm-svn: 346260
2018-11-06 19:34:22 +00:00
Derek Schuff 786760a9f6 [WebAssembly] Address review comments from r346248 [NFC]
llvm-svn: 346249
2018-11-06 18:02:39 +00:00
Derek Schuff 3bea8bcae5 [WebAssembly] Support creation and import of shared memories
Used for WebAssembly threads proposal. Add a flag --shared-memory
which sets the IS_SHARED bit in WasmLimits

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

llvm-svn: 346248
2018-11-06 17:59:32 +00:00
Jordan Rupprecht 5bd334aef7 [lld][NFC] Update tests to use -S instead of -s when using llvm-readelf.
Summary: llvm-readobj/readelf accepts both -s and -S as aliases for --sections. However with GNU readelf only -S means --section, and -s means --symbols. I would like to make llvm-readelf more compatible.

Reviewers: MaskRay, espindola

Reviewed By: MaskRay

Subscribers: emaste, arichardson, steven_wu, dexonsmith, llvm-commits

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

llvm-svn: 346164
2018-11-05 20:39:06 +00:00
Alexandre Ganea 625984b927 Disable precomp test on Linux until I fix it.
llvm-svn: 346163
2018-11-05 20:39:02 +00:00
Alexandre Ganea 8a0eb44398 Fix build breakerage on GCC 5.4:
/home/buildslave/slave_as-bldslv8/lld-perf-testsuite/llvm/tools/lld/COFF/PDB.cpp:365:51: error: 'auto' not allowed in lambda parameter
  auto DbgIt = find_if(File->getDebugChunks(), [](auto &C) {
                                                  ^~~~

http://lab.llvm.org:8011/builders/lld-perf-testsuite/builds/8717/steps/build-bin%2Flld/logs/stdio

llvm-svn: 346160
2018-11-05 19:43:34 +00:00
Alexandre Ganea 71c43ceaf8 [COFF][LLD] Add link support for Microsoft precompiled headers OBJs
This change allows for link-time merging of debugging information from
Microsoft precompiled types OBJs compiled with cl.exe /Z7 /Yc and /Yu.

This fixes llvm.org/PR34278

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

llvm-svn: 346154
2018-11-05 19:20:47 +00:00
Galina Kistanova 4ec5d67e49 Handle OUTPUT_FORMAT arguments in quotation marks
llvm-svn: 346068
2018-11-03 05:25:49 +00:00
Fangrui Song 17a7b49e95 [DWARF] Fix typo, .gnu_index -> .gdb_index
llvm-svn: 346040
2018-11-02 20:34:42 +00:00
Fangrui Song 7bafaf8ffe Reland "Change the timestamp of llvmcache-foo file to meet the thinLTO prune policy"
Summary:
It is difficult to touch a file with a relative mtime across different OSes as POSIX touch -d is rigid. While we may construct relative timestamps with `date`, POSIX date is inadequate to do so as various OSes' date do not agree on a common format (OpenBSD uses `date -r seconds`, FreeBSD uses `date -v-2M` while GNU accepts `-d '-2 min'`)

Just use python os.utime()

Original description:

    The case may randomly fail if we test it with command "
     while llvm-lit tools/lld/test/ELF/lto/cache.ll; do true; done". It is because the llvmcache-foo file is younger than llvmcache-349F039B8EB076D412007D82778442BED3148C4E and llvmcache-A8107945C65C2B2BBEE8E61AA604C311D60D58D6. But due to timestamp precision reason their timestamp is the same. Given the same timestamp, the file prune policy is to remove bigger size file first, so mostly foo file is removed for its bigger size. And the files size is under threshold after deleting foo file. That's what test case expect.

    However sometimes, the precision is enough to measure that timestamp of llvmcache-349F039B8EB076D412007D82778442BED3148C4E and llvmcache-A8107945C65C2B2BBEE8E61AA604C311D60D58D6 are smaller than foo, so llvmcache-349F039B8EB076D412007D82778442BED3148C4E and llvmcache-A8107945C65C2B2BBEE8E61AA604C311D60D58D6 are deleted first. Since the files size is still above the file size threshold after deleting the 2 files, the foo file is also deleted. And then the test case fails, because it expect only one file should be deleted instead of 3.

    The fix is to change the timestamp of llvmcache-foo file to meet the thinLTO prune policy.
    The same fix is applied to llvm code at https://reviews.llvm.org/D52452.

    Patch by Luo Yuanke.

Reviewers: ruiu, craig.topper, smaslov, Jianping, espindola, LuoYuanke, tejohnson

Reviewed By: tejohnson

Subscribers: sbc100, krytarowski, aheejin, llvm-commits, dexonsmith, steven_wu, arichardson, inglorion, emaste, bjope, rupprecht

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

llvm-svn: 346006
2018-11-02 17:44:30 +00:00
Teresa Johnson 366d7285a1 Revert "Change the timestamp of llvmcache-foo file to meet the thinLTO prune policy"
This reverts commit r345977.

A few bots failing because the invocation of "touch" is not accepted on
a couple other OSes. Specifically the -d argument is not accepted or requires
a different format.

http://lab.llvm.org:8011/builders/lld-x86_64-darwin13/builds/27103/steps/test_lld/logs/stdio
http://lab.llvm.org:8011/builders/lld-x86_64-freebsd/builds/24974/steps/test_lld/logs/stdio

llvm-svn: 345980
2018-11-02 15:22:19 +00:00
Teresa Johnson c8325b4b59 Change the timestamp of llvmcache-foo file to meet the thinLTO prune policy
Summary:

The case may randomly fail if we test it with command "
 while llvm-lit tools/lld/test/ELF/lto/cache.ll; do true; done". It is because the llvmcache-foo file is younger than llvmcache-349F039B8EB076D412007D82778442BED3148C4E and llvmcache-A8107945C65C2B2BBEE8E61AA604C311D60D58D6. But due to timestamp precision reason their timestamp is the same. Given the same timestamp, the file prune policy is to remove bigger size file first, so mostly foo file is removed for its bigger size. And the files size is under threshold after deleting foo file. That's what test case expect.

However sometimes, the precision is enough to measure that timestamp of llvmcache-349F039B8EB076D412007D82778442BED3148C4E and llvmcache-A8107945C65C2B2BBEE8E61AA604C311D60D58D6 are smaller than foo, so llvmcache-349F039B8EB076D412007D82778442BED3148C4E and llvmcache-A8107945C65C2B2BBEE8E61AA604C311D60D58D6 are deleted first. Since the files size is still above the file size threshold after deleting the 2 files, the foo file is also deleted. And then the test case fails, because it expect only one file should be deleted instead of 3.

The fix is to change the timestamp of llvmcache-foo file to meet the thinLTO prune policy.
The same fix is applied to llvm code at https://reviews.llvm.org/D52452.

Patch by Luo Yuanke.

Reviewers: ruiu, craig.topper, smaslov, Jianping, espindola, LuoYuanke

Subscribers: rupprecht, bjope, emaste, inglorion, arichardson, steven_wu, dexonsmith, llvm-commits

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

llvm-svn: 345977
2018-11-02 14:52:10 +00:00
Fangrui Song 27d036e995 [ELF] Change sh_link of .rel{,a}.plt to make GNU strip happy
Summary:
D52830 sets sh_link to .symtab in static link, which breaks executable stripped by GNU strip.
It may also be odd that .rela.plt (SHF_ALLOC) points to .symtab (non-SHF_ALLOC).

Change the logic on pcc's suggestion.

Before:

% clang -fuse-ld=lld -static -xc =(printf 'int main(){}') # or gcc
% strip a.out; ./a.out
unexpected reloc type in static binary[1]    61634 segmentation fault  ./a.out

Reviewers: ruiu, grimar, emaste, espindola

Reviewed By: ruiu

Subscribers: pcc, arichardson, llvm-commits

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

llvm-svn: 345899
2018-11-01 22:28:58 +00:00
Rui Ueyama 4d0059c09b Omit "virtual" if overridden.
llvm-svn: 345886
2018-11-01 20:08:39 +00:00
Rui Ueyama cace50c628 Use llvm_unreachable for unreachable code.
llvm-svn: 345885
2018-11-01 20:08:29 +00:00
Fangrui Song ccfc8415c2 Set MAttrs in LTO mode
Summary: Without this patch, MAttrs are not set.

Patch by Yin Ma

Reviewers: espindola, MaskRay, ruiu, pcc

Reviewed By: MaskRay, pcc

Subscribers: pcc, emaste, sbc100, inglorion, arichardson, aheejin, steven_wu, llvm-commits

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

llvm-svn: 345884
2018-11-01 20:02:49 +00:00
Reid Kleckner 4dc0b1ac60 Fix clang -Wimplicit-fallthrough warnings across llvm, NFC
This patch should not introduce any behavior changes. It consists of
mostly one of two changes:
1. Replacing fall through comments with the LLVM_FALLTHROUGH macro
2. Inserting 'break' before falling through into a case block consisting
   of only 'break'.

We were already using this warning with GCC, but its warning behaves
slightly differently. In this patch, the following differences are
relevant:
1. GCC recognizes comments that say "fall through" as annotations, clang
   doesn't
2. GCC doesn't warn on "case N: foo(); default: break;", clang does
3. GCC doesn't warn when the case contains a switch, but falls through
   the outer case.

I will enable the warning separately in a follow-up patch so that it can
be cleanly reverted if necessary.

Reviewers: alexfh, rsmith, lattner, rtrieu, EricWF, bollu

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

llvm-svn: 345882
2018-11-01 19:54:45 +00:00
George Rimar 3608decaa5 [ELF] - Do not crash when -r output uses linker script with `/DISCARD/`
This is https://bugs.llvm.org/show_bug.cgi?id=39493.

We crashed previously because did not handle /DISCARD/ properly
when -r was used. I think it is uncommon to use scripts with -r, though I see
nothing wrong to handle the /DISCARD/ so that we will not crash at least.

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

llvm-svn: 345819
2018-11-01 09:20:06 +00:00
Sam Clegg c1a3b9d51a [WebAssembly] Remove duplicate function. NFC.
Differential Revision: https://reviews.llvm.org/D53963

llvm-svn: 345806
2018-11-01 01:08:37 +00:00
Ryan Prichard e7cb0225a0 [ELF] Refactor per-target TLS layout configuration. NFC.
Summary:
There are really three different kinds of TLS layouts:

 * A fixed TLS-to-TP offset. On architectures like PowerPC, MIPS, and
   RISC-V, the thread pointer points to a fixed offset from the start
   of the executable's TLS segment. The offset is 0x7000 for PowerPC
   and MIPS, which allows a signed 16-bit offset to reach 0x1000 of
   per-thread implementation data and 0xf000 of the application's TLS
   segment. The size and layout of the TCB isn't relevant to the static
   linker and might not be known.

 * A fixed TCB size. This is the format documented as "variant 1" in
   Ulrich Drepper's TLS spec. The thread pointer points to a 2-word TCB
   followed by the executable's TLS segment. The first word is always
   the DTV pointer. Used on ARM. The thread pointer must be aligned to
   the TLS segment's alignment, possibly creating alignment padding.

 * Variant 2. This format predates variant 1 and is also documented in
   Drepper's TLS spec. It allocates the executable's TLS segment before
   the thread pointer, apparently for backwards-compatibility. It's
   used on x86 and SPARC.

Factor out an lld:🧝:getTlsTpOffset() function for use in a
follow-up patch for Android. The TcbSize/TlsTpOffset fields are only used
in getTlsTpOffset, so replace them with a switch on Config->EMachine.

Reviewers: espindola, ruiu, PkmX, jrtc27

Reviewed By: ruiu, PkmX, jrtc27

Subscribers: jyknight, emaste, sdardis, nemanjai, javed.absar, arichardson, kristof.beyls, kbarton, fedor.sergeev, atanasyan, PkmX, jsji, llvm-commits

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

llvm-svn: 345775
2018-10-31 20:53:17 +00:00
Sam Clegg 59d52f8040 [WedAssembly] Add -s and -S alias for --strip-all and --strip-debug
llvm-svn: 345767
2018-10-31 19:30:43 +00:00
Fangrui Song e0799a7268 [ELF] Fallback to sh_link=0 if neither .dynsym nor .symtab exists
Summary: .rela.plt may only contain R_*_{,I}RELATIVE relocations and not need a symbol table link. bfd/gold fallbacks to sh_link=0 in this case. Without this patch, ld.lld --strip-all caused lld to dereference a null pointer.

Reviewers: ruiu, grimar, espindola

Reviewed By: ruiu

Subscribers: emaste, arichardson, llvm-commits

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

llvm-svn: 345648
2018-10-30 20:54:54 +00:00
Sean Fertile 92964e74a5 [PPC64] Handle powerpc64 in OUTPUT_FORMAT.
Differential Revision: https://reviews.llvm.org/D53652

llvm-svn: 345604
2018-10-30 14:37:17 +00:00
Diogo N. Sampaio 0c4a184577 [FIX][AArch64] lld test change
Adding AArch64 UDF, in rL345581,
breaks some lld test files, due
different decoding

llvm-svn: 345592
2018-10-30 12:19:55 +00:00
James Henderson 1e74ecab17 [ELF][PPC64]Workaround bogus Visual Studio build warning
Visual Studio has a bug where it converts the integer literal 2147483648
into an unsigned int instead of a long long (i.e. it follows C89 rules).
The bug has been reported as:
https://developercommunity.visualstudio.com/content/problem/141813/-2147483648-c4146-error.html.

Because of this bug, we were getting a signed/unsigned comparison
warning in VS2015 from the old code (the subsequent unary negation had
no effect on the type).

Reviewed by: sfertile

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

llvm-svn: 345579
2018-10-30 10:55:14 +00:00
Konstantin Zhuravlyov f472afd684 AMDGPU: Switch some lld tests to v2
Differential Revision: https://reviews.llvm.org/D53526

llvm-svn: 345530
2018-10-29 19:59:12 +00:00
Rui Ueyama 4c06a6cc90 Rename warnUnorderableSymbol maybeWarnUnorderableSymbol because the function doesn't always emit a warning.
llvm-svn: 345393
2018-10-26 15:07:12 +00:00
Rui Ueyama 2b39ea477c Refactor readCallGraph() and readCallGraphFromObjectFiles(). NFC.
llvm-svn: 345392
2018-10-26 15:07:02 +00:00
Fangrui Song cc18f8aa0f [ELF] Add --{,no-}call-graph-profile-sort (enabled by default)
Summary: Add an option to disable sorting sections with call graph profile

Reviewers: ruiu, Bigcheese, espindola

Reviewed By: Bigcheese

Subscribers: grimar, emaste, arichardson, llvm-commits

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

llvm-svn: 345332
2018-10-25 23:15:23 +00:00
Fangrui Song 018b0634b4 [ELF] Don't check if symbols in .llvm.call-graph-profile are unorderable
Summary: There are too many reasonable cases that would be considered unorderable.

Reviewers: ruiu, espindola, Bigcheese

Reviewed By: ruiu

Subscribers: grimar, emaste, arichardson, llvm-commits

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

llvm-svn: 345322
2018-10-25 22:15:42 +00:00
Rui Ueyama 9db06423d1 Remove full stops from error messages for consistency.
llvm-svn: 345294
2018-10-25 18:07:55 +00:00
Rui Ueyama 444363e931 Do not call computeIsPreemptible() if its result is discarded. NFC.
llvm-svn: 345208
2018-10-24 22:15:32 +00:00
Rui Ueyama 4664280a9c Add more blank lines so that code doesn't look too dense. NFC.
llvm-svn: 345205
2018-10-24 21:59:58 +00:00
Rui Ueyama 6e33f7ac66 Add a comment for PPC64 .toc and GNU relro.
llvm-svn: 345204
2018-10-24 21:59:48 +00:00
Rui Ueyama aa2948776a Include input section name and output section name in an error message.
Differential Revision: https://reviews.llvm.org/D53645

llvm-svn: 345172
2018-10-24 18:15:40 +00:00
Rui Ueyama 8d76024916 Refactor assignFileOffsets. NFC.
llvm-svn: 345154
2018-10-24 15:47:46 +00:00
Rui Ueyama 6d26ed92cc Split a function. NFC.
llvm-svn: 345143
2018-10-24 14:24:01 +00:00
Rui Ueyama 6e763a36c6 Add parenthese around a bitand.
llvm-svn: 345139
2018-10-24 13:44:51 +00:00
Rui Ueyama ee2b61cfd3 Make a local variable scope narrower. NFC.
llvm-svn: 345138
2018-10-24 13:42:33 +00:00
Martin Storsjo 865cb5604c [MinGW] Support for multiarch runtimes layout
Patch by Peiyuan Song!

llvm-svn: 345117
2018-10-24 07:42:10 +00:00
Martin Storsjo c3095ace86 [ELF] Remove a superfluous semicolon, fixing warnings. NFC.
llvm-svn: 345116
2018-10-24 07:42:06 +00:00
Rui Ueyama 02c7fae348 Move forward declarations to the top of the file and sort.
llvm-svn: 345094
2018-10-23 22:37:14 +00:00
Rui Ueyama b0de6c742a Move a function out of a class because it doesn't depend on any class member. NFC.
llvm-svn: 345093
2018-10-23 22:31:08 +00:00
Rui Ueyama 5a30177ef6 Factor out code to a new function. NFC.
llvm-svn: 345088
2018-10-23 22:03:33 +00:00
Rui Ueyama edc3201b11 Simplify. NFC.
A higher order function `applySyntehtic` can be replaced with a simpler function.

llvm-svn: 345081
2018-10-23 21:17:00 +00:00
Rui Ueyama 660e8721a9 Remove a global variable that is set but not used.
llvm-svn: 345080
2018-10-23 21:00:28 +00:00
Rui Ueyama 29d8639732 Add a comment.
llvm-svn: 345062
2018-10-23 17:52:44 +00:00
Rui Ueyama 42ab6c53f8 Remove a global variable that we can live without.
Out::DebugInfo was used only by GdbIndex class to determine if
we need to create a .gdb_index section, but we can do the same
check without it.

Added a test that this patch doesn't change the existing behavior.

llvm-svn: 345058
2018-10-23 17:39:43 +00:00
Dimitry Andric ad5aab8ddb Don't mess up RelIplt symbols during relocatable processing
Summary:
During upgrading of the FreeBSD source tree with lld 7.0.0, I noticed
that it started complaining about `crt1.o` having an "index past the
end of the symbol table".

Such a symbol table looks approximately like this, viewed with `readelf
-s` (note the `Ndx` field being messed up):

```
Symbol table '.symtab' contains 4 entries:
   Num:    Value  Size Type    Bind   Vis      Ndx Name
     0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 00000000     0 SECTION LOCAL  DEFAULT    1
     2: 00000000     0 NOTYPE  WEAK   HIDDEN  RSV[0xffff] __rel_iplt_end
     3: 00000000     0 NOTYPE  WEAK   HIDDEN  RSV[0xffff] __rel_iplt_start
```

At first, it seemed that recent ifunc relocation work had caused this:
<https://reviews.freebsd.org/rS339351>, but it turned out that it was
due to incorrect processing of the object files by lld, when using `-r`
(a.k.a. --relocatable).

Bisecting showed that rL324421 ("Convert a use of Config->Static") was
the commit where this new behavior began.  Simply reverting it solved
the issue, and the `__rel_iplt` symbols had an index of `UND` again.

Looking at Rafael's commit message, I think he simply missed the
possibility of `--relocatable` being in effect, so I have added an
additional check for it.

I also added a simple regression test case.

Reviewers: grimar, ruiu, emaste, espindola

Reviewed By: ruiu

Subscribers: arichardson, krytarowski, llvm-commits

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

llvm-svn: 345002
2018-10-23 05:53:15 +00:00
Shoaib Meenai 3ac97e2ed4 [ELF] Split up emulation.s per backend
emulation.s is testing multiple architectures, which means it needs all
the corresponding backends enabled, which might not be true for all
developers (for example, I don't have PPC or MIPS enabled). Rather than
marking the entire test as unsupported for such developers, split it up
per backend to get better testing granularity.

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

llvm-svn: 344986
2018-10-23 01:19:18 +00:00
Shoaib Meenai 9d7de097d4 [ELF] Actually fix test from r344976
I don't think the offsets and flags are particularly relevant to the
test; the machine type is what matters.

llvm-svn: 344984
2018-10-23 00:34:34 +00:00
Shoaib Meenai 45f143c316 [ELF] Fix test from r344976
Turns out I wasn't actually running this test locally, since I don't
build the PPC and MIPS backends. Whoops.

Perhaps this test should be split up per-architecture?

llvm-svn: 344980
2018-10-23 00:27:27 +00:00
Shoaib Meenai c8e9d2b525 [ELF] Handle elf32-littlearm in OUTPUT_FORMAT
We need this to support 32-bit ARM. Add test cases for emulation
handling for this architecture as well.

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

llvm-svn: 344976
2018-10-22 23:56:10 +00:00
Fangrui Song efc0fe5a72 [ELF] Don't warn on two legitimate cases when reading .llvm.call-graph-profile
Summary:
Before, superfluous warnings were emitted for the following two cases:

1) When from symbol was in a discarded section.
  The profile should be thought of as affiliated to the section.
  It makes sense to ignore the profile if the section is discarded.

2) When to symbol was in a shared object.
  The object file containing the profile may not know about the to
  symbol, which can reside in another object file (useful profile) or a
  shared object (not useful as symbols in the shared object are fixed
  and unorderable). It makes sense to ignore the profile from the object
  file.

  Note, the warning when to symbol was undefined was suppressed in
  D53044, which is still useful for --symbol-ordering-file=

This patch silences the warnings. The check is actually more relaxed (no
warnings if either From or To is not Defined) for simplicity and I don't
see a compelling reason to warn on more cases.

Reviewers: ruiu, davidxl, espindola, Bigcheese

Reviewed By: ruiu

Subscribers: emaste, arichardson, llvm-commits

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

llvm-svn: 344974
2018-10-22 23:43:53 +00:00
Rui Ueyama ea8cd00a1d Add OUTPUT_FORMAT linker script directive support.
This patch adds a support for OUTPUT_FORMAT linker script directive.
Since I'm not 100% confident with BFD names you can use in the directive
for all architectures, I added only a few in this patch. We can add
other names for other archtiectures later.

We still do not support triple-style OUTPUT_FORMAT directive, namely,
OUTPUT_FORMAT(bfdname, big, little). If you pass -EL (little endian)
or -EB (big endian) to the linker, GNU linkers pick up big or little
as a BFD name, correspondingly, so that you can use a single linker
script for bi-endian processor. I'm not sure if we really need to
support that, so I'll leave it alone for now.

Note that -m takes precedence over OUTPUT_FORAMT, but we always parse
a BFD name given to OUTPUT_FORMAT for error checking. You cannot write
an invalid name in the OUTPUT_FORMAT directive.

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

llvm-svn: 344952
2018-10-22 20:50:01 +00:00
Fangrui Song e94d8823d9 [ELF][PPC64] Fix a split-stack comment in rLLD344622
The blt- instruction (predicted not to be taken) uses cr7, not the default cr0.

llvm-svn: 344948
2018-10-22 20:30:06 +00:00
Fangrui Song 8bafbfea9d [ELF] Format PPC64.cpp, NFC
Mainly two mis-indented places. The changes are local so should not interfere with in-review revisions

llvm-svn: 344932
2018-10-22 18:20:18 +00:00
Rui Ueyama ae8c3176b8 Fix typo in a test file name.
llvm-svn: 344899
2018-10-22 13:15:19 +00:00
Sam Clegg ea65647254 Use llvm::arrayRefFromStringRef
Differential Revision: https://reviews.llvm.org/D53432

llvm-svn: 344888
2018-10-22 08:35:39 +00:00
Sam Clegg ad980a75c4 [WebAssembly] Simplify --help message
Update wasm to match ELF changes made rL333596.

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

llvm-svn: 344887
2018-10-22 08:34:37 +00:00
Alexander Richardson 39adc6df0d Add an addAbsolute static function to Writer.cpp
Summary:
SymbolTable::addAbsolute() was removed in rL344305.
To me this is more readable than the lambda named `Add` and in our
out-of-tree CHERI target we use addAbsolute() in another function.

Reviewers: ruiu, espindola

Reviewed By: ruiu

Subscribers: kristina, emaste, llvm-commits

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

llvm-svn: 344842
2018-10-20 11:12:50 +00:00
Martin Storsjo 28212dfce6 [COFF] Fix error handling on duplicates for import library symbols
Normally one wouldn't run into that case, but it is possible with
a little creative ordering of special libraries.

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

llvm-svn: 344776
2018-10-19 06:39:36 +00:00
Sean Fertile 3acfe400a2 [PPC64] Fix offset checks on rel24 call relocations.
Adjusted the range check on a call instruction from 24 bits signed to
26 bits signed. While the instruction only encodes 24 bits, the target is
assumed to be 4 byte aligned, and the value that is encoded in the instruction
gets shifted left by 2 to form the offset. Also added a check that the offset is
indeed at least 4 byte aligned.

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

llvm-svn: 344747
2018-10-18 15:43:41 +00:00
Fedor Sergeev bd6b2138b9 [NewPM] teach -passes= to emit meaningful error messages
All the PassBuilder::parse interfaces now return descriptive StringError
instead of a plain bool. It allows to make -passes/aa-pipeline parsing
errors context-specific and thus less confusing.

TODO: ideally we should also make suggestions for misspelled pass names,
but that requires some extensions to PassBuilder.

Reviewed By: philip.pfaffe, chandlerc
Differential Revision: https://reviews.llvm.org/D53246

llvm-svn: 344685
2018-10-17 10:36:23 +00:00
Sean Fertile 4b5ec7fb80 Reland "[PPC64] Add split - stack support."
Recommitting https://reviews.llvm.org/rL344544 after fixing undefined behavior
from left-shifting a negative value. Original commit message:

This support is slightly different then the X86_64 implementation in that calls
to __morestack don't need to get rewritten to calls to __moresatck_non_split
when a split-stack caller calls a non-split-stack callee. Instead the size of
the stack frame requested by the caller is adjusted prior to the call to
__morestack. The size the stack-frame will be adjusted by is tune-able through a
new --split-stack-adjust-size option.

llvm-svn: 344622
2018-10-16 17:13:01 +00:00
Sean Fertile 831a1336ff Revert "[PPC64] Add split - stack support."
This reverts commit https://reviews.llvm.org/rL344544, which causes failures on
a undefined behaviour sanitizer bot -->
lld/ELF/Arch/PPC64.cpp:849:35: runtime error: left shift of negative value -1

llvm-svn: 344551
2018-10-15 20:20:28 +00:00
Sean Fertile 795cc9332b [PPC64] Add split - stack support.
This support is slightly different then the X86_64 implementation in that calls
to __morestack don't need to get rewritten to calls to __moresatck_non_split
when a split-stack caller calls a non-split-stack callee. Instead the size of
the stack frame requested by the caller is adjusted prior to the call to
__morestack. The size the stack-frame will be adjusted by is tune-able through a
new --split-stack-adjust-size option.

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

llvm-svn: 344544
2018-10-15 19:05:57 +00:00
Sid Manning 6df0759aad [ELF][HEXAGON] Let input determine e_flag.
Differential Revision: https://reviews.llvm.org/D53204

llvm-svn: 344518
2018-10-15 14:44:52 +00:00
George Rimar ee981860a8 [LLD][ELF] - Check options before processing the -v/-version options.
This is https://bugs.llvm.org/show_bug.cgi?id=39289.

Currently both gold and bfd report errors about invalid options values
even with -v/-versions. But LLD does not.

This makes complicated to check the options available when LLD is used.

Patch makes LLD behavior to be consistent with GNU linkers.

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

llvm-svn: 344514
2018-10-15 14:21:43 +00:00
Rui Ueyama 3d354081e0 Simplify. NFC.
- Removed redundant `llvm::`
 - Typedef a long type name
 - Initialize members by member initializers

llvm-svn: 344427
2018-10-12 22:44:06 +00:00
Rui Ueyama f3fad55787 Remove `Type` parameter from SymbolTable::insert(). NFC.
`Type` parameter was used only to check for TLS attribute mismatch,
but we can do that when we actually replace symbols, so we don't need
to type as an argument. This change should simplify the interface of
the symbol table a bit.

llvm-svn: 344394
2018-10-12 18:29:18 +00:00
Zachary Turner 5bba1cafbe Better support for POSIX paths in PDBs.
This a resubmission of a patch which was previously reverted
due to breaking several lld tests.  The issues causing those
failures have been fixed, so the patch is now resubmitted.

---Original Commit Message---

While it doesn't make a *ton* of sense for POSIX paths to be
in PDBs, it's possible to occur in real scenarios involving
cross compilation.

The tools need to be able to handle this, because certain types
of debugging scenarios are possible without a running process
and so don't necessarily require you to be on a Windows system.
These include post-mortem debugging and binary forensics (e.g.
using a debugger to disassemble functions and examine symbols
without running the process).

There's changes in clang, LLD, and lldb in this patch.  After
this the cross-platform disassembly and source-list tests pass
on Linux.

Furthermore, the behavior of LLD can now be summarized by a much
simpler rule than before: Unless you specify /pdbsourcepath and
/pdbaltpath, the PDB ends up with paths that are valid within
the context of the machine that the link is performed on.

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

llvm-svn: 344377
2018-10-12 17:26:19 +00:00
Rui Ueyama 2e9d40d5f4 [lld] Add more complete support for the INCLUDE command.
Patch by Ian Tessier.

This change adds INCLUDE support to the MEMORY and SECTION commands, and
to output sections, as per:

https://sourceware.org/binutils/docs/ld/File-Commands.html#File-Commands

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

llvm-svn: 344368
2018-10-12 17:07:32 +00:00
Zachary Turner 9f169afab2 Make YAML quote forward slashes.
If you have the string /usr/bin, prior to this patch it would not
be quoted by our YAML serializer.  But a string like C:\src would
be, due to the presence of a backslash.  This makes the quoting
rules of basically every single file path different depending on
the path syntax (posix vs. Windows).

While technically not required by the YAML specification to quote
forward slashes, when the behavior of paths is inconsistent it
makes it difficult to portably write FileCheck lines that will
work with either kind of path.

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

llvm-svn: 344359
2018-10-12 16:31:20 +00:00
Zachary Turner 9c544199cf Revert "Make YAML quote forward slashes."
This reverts commit b86c16ad8c97dadc1f529da72a5bb74e9eaed344.

This is being reverted because I forgot to write a useful
commit message, so I'm going to resubmit it with an actual
commit message.

llvm-svn: 344358
2018-10-12 16:31:08 +00:00
Zachary Turner ec234052a6 Make YAML quote forward slashes.
llvm-svn: 344357
2018-10-12 16:24:09 +00:00
George Rimar 4cb05713de [ELF] - Remove unused variable. NFC.
It triggered a bot failture somehow today:
http://lab.llvm.org:8011/builders/lld-perf-testsuite/builds/7993

llvm-svn: 344344
2018-10-12 13:39:35 +00:00
George Rimar fa91327b51 [ELF] - Remove dead declaration. NFC.
MipsGotSection::FileGot::isOverflow()
was a dead declararion.

llvm-svn: 344342
2018-10-12 13:25:25 +00:00
Rui Ueyama b0d8502049 Remove SymbolTable::addAbsolute().
addAbsolute() could be implemented as a non-member function.

llvm-svn: 344305
2018-10-11 22:15:41 +00:00
Rui Ueyama 936cfa86e5 Remove `else` after `return`.
llvm-svn: 344302
2018-10-11 21:57:52 +00:00
Eli Friedman 8b44cc21c6 [ELF] Fix link failure with Android compressed relocation support.
Android uses a compressed relocation format, which means the size of the
relocation section isn't predictable based on the number of relocations,
and can vary if the layout changes in any way. To deal with this, the
linker normally runs multiple passes until the layout converges.

The layout should converge if the size of the compressed
relocation section increases monotonically: if the size of an encoded
offset increases by one byte, the larget value which can be encoded is
multiplied by 128, so the representable offsets grow much faster than
the size of the section itself.

The problem here is that there is no code to ensure the size of the
section doesn't decrease.  If the size of the relocation section
decreases, the relative offsets can increase due to alignment
restrictions, so that can force the size of the relocation section to
increase again.  The end result is an infinite loop; the loop gets cut
off after 10 iterations with the message "thunk creation not
converged".

To avoid this issue, this patch adds padding to the end of the
relocation section if its size would decrease.  The extra
padding is harmless because of the way the format is defined:
decoding stops after it reaches the number of relocations specified
in the section's header.

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

llvm-svn: 344300
2018-10-11 21:43:06 +00:00
Rui Ueyama 2600e0946b Rename SymbolTable::addRegular -> SymbolTable::addDefined.
We have addAbsolute, addBitcode, addCommon, etc. addRegular looked a
bit inconsistent.

llvm-svn: 344294
2018-10-11 20:43:01 +00:00
Rui Ueyama cf41adaaa0 Remove unused default arguments.
llvm-svn: 344292
2018-10-11 20:38:34 +00:00
Rui Ueyama c7497d3ac5 Remove SymbolTable::addUndefined<ELF32LE>(StringRef).
Because we can implement the function as a non-member function.

llvm-svn: 344290
2018-10-11 20:34:29 +00:00
Zachary Turner e8a6c3eb96 Revert SymbolFileNativePDB plugin.
This was originally causing some test failures on non-Windows
platforms, which required fixes in the compiler and linker.  After
those fixes, however, other tests started failing.  Reverting
temporarily until I can address everything.

llvm-svn: 344279
2018-10-11 18:45:44 +00:00
Zachary Turner e502f8b315 Better support for POSIX paths in PDBs.
While it doesn't make a *ton* of sense for POSIX paths to be
in PDBs, it's possible to occur in real scenarios involving
cross compilation.

The tools need to be able to handle this, because certain types
of debugging scenarios are possible without a running process
and so don't necessarily require you to be on a Windows system.
These include post-mortem debugging and binary forensics (e.g.
using a debugger to disassemble functions and examine symbols
without running the process).

There's changes in clang, LLD, and lldb in this patch.  After
this the cross-platform disassembly and source-list tests pass
on Linux.

Furthermore, the behavior of LLD can now be summarized by a much
simpler rule than before: Unless you specify /pdbsourcepath and
/pdbaltpath, the PDB ends up with paths that are valid within
the context of the machine that the link is performed on.

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

llvm-svn: 344269
2018-10-11 18:01:55 +00:00
Martin Storsjo 8cc0f71261 [COFF] Add and use a Wordsize field in Config. NFCI.
Differential Revision: https://reviews.llvm.org/D53143

llvm-svn: 344265
2018-10-11 17:45:58 +00:00
Martin Storsjo 21eb363302 [COFF] Set proper pointer size alignment for LocalImportChunk
When these are accessed with load/store instructions on ARM64,
it becomes strictly necessary to have them properly aligned.

This fixes PR39228.

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

llvm-svn: 344264
2018-10-11 17:45:51 +00:00
George Rimar 3368643f5e [ELF] - Set sh_info and sh_link for .rela.plt sections.
This is https://bugs.llvm.org/show_bug.cgi?id=37538,

Currently, LLD may set both sh_link and sh_info for
.rela.plt section to zero when we have only .rela.iplt section part used.

ELF spec (https://docs.oracle.com/cd/E19683-01/816-1386/chapter6-94076/index.html)
says that for SHT_REL and SHT_RELA, sh_link references the associated symbol table
and sh_info the "section to which the relocation applies."

When we set the sh_link field, for the regular case we use the .dynsym index.
For .rela.iplt sections, it is unclear what is the associated symbol table,
because R_*_RELATIVE relocations do not use symbol names and we might have no
.dynsym section at all so this patch uses .symtab section index.

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

llvm-svn: 344226
2018-10-11 08:25:35 +00:00
Fangrui Song a535e0543f Eliminate dependency to formatv(). NFC.
llvm-svn: 344212
2018-10-11 00:58:00 +00:00
Rui Ueyama 3c4344810a Make a member function private and rename it to avoid function overloading.
llvm-svn: 344196
2018-10-10 22:49:29 +00:00
Fangrui Song 11ca54f49c [ELF] Don't warn on undefined symbols if UnresolvedPolicy::Ignore is used
Summary:
Add a condition UnresolvedPolicy::Ignore to elf::warnUnorderedSymbol to suppress Sym->isUndefined() warnings from both

1) --symbol-ordering-file=
2) .llvm.call-graph-profile

If --unresolved-symbols=ignore-all is used,

  no "undefined symbol" error/warning is emitted. It makes sense to not warn unorderable symbols.

Otherwise,

  If an executable is linked, the default policy UnresolvedPolicy::ErrorOrWarn will issue a "undefined symbol" error. The unorderable symbol warning is redundant.

  If a shared object is linked, it is possible that only part of object files are used and some symbols are left undefined. The warning is not very necessary.
    In particular for .llvm.call-graph-profile, when linking a shared object, a call graph profile may contain undefined symbols. This case generated a warning before but it will be suppressed by this patch.

Reviewers: ruiu, davidxl, espindola

Reviewed By: ruiu

Subscribers: grimar, emaste, arichardson, llvm-commits

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

llvm-svn: 344195
2018-10-10 22:48:57 +00:00
Rui Ueyama 9ae88c64fa Eliminate dependency to formatv(). NFC.
llvm-svn: 344177
2018-10-10 20:29:29 +00:00
Martin Storsjo 33d43ff851 [COFF] Look for libfoo.a if foo.lib is specified, for MinGW
This allows using #pragma comment(lib, "foo") in MinGW built code,
if built with -fms-extensions. (This works for system libraries and
static libraries only, as it doesn't try to look for .dll.a. As
ld.bfd doesn't support embedded defaultlib directives, this isn't
in widespread use among mingw users.)

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

llvm-svn: 344124
2018-10-10 09:00:10 +00:00
Martin Storsjo 803b37ad5d [MinGW] Pass libpath to the COFF linker
This is necessary for handling defaultlib directives embedded in
object files, unless they use an absolute path.

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

llvm-svn: 344123
2018-10-10 09:00:03 +00:00
Fangrui Song 2043a58abe Adapt OptTable::PrintHelp change in D51009
Summary: Before, OptTable::PrintHelp append "[options] <inputs>" to its parameter `Help`. It is more flexible to change its semantic to `Usage` and let user customize the usage line.

Reviewers: rupprecht, ruiu, espindola

Reviewed By: rupprecht

Subscribers: emaste, sbc100, arichardson, aheejin, llvm-commits

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

llvm-svn: 344099
2018-10-10 00:15:36 +00:00
Rui Ueyama 71cdbb7fe6 Merge two overloaded functions into one function. NFC.
llvm-svn: 344089
2018-10-09 22:44:53 +00:00
Rui Ueyama e65cb4889e Return early. NFC.
llvm-svn: 344088
2018-10-09 22:44:42 +00:00
Rui Ueyama 2b53b4bea6 Attempt to fix ubsan.
Previously, we cast a pointer to Elf{32,64}_Chdr like this

  auto *Hdr = reinterpret_cast<const ELF64_Chdr>(Ptr);

and read from its members like this

  read32(&Hdr->ch_size);

I was thinking that this does not violate alignment requirement,
since &Hdr->ch_size doesn't really access memory, but seems like
it is a violation in terms of C++ spec (?)

In this patch, I use a different struct that allows unaligned access.

llvm-svn: 344083
2018-10-09 21:41:53 +00:00
Rui Ueyama e3ccd28660 Calculate an offset only once. NFC.
llvm-svn: 344076
2018-10-09 20:29:24 +00:00
Rui Ueyama fbc629702d Remove redundant `Symtab->`.
`SymbolTable` is a singleton class and is a global variable for the
unique instance, so we can always refer the symtab by `Symtab->`.
However, we don't need to use the global varaible from member functions
of SymbolTable class.

llvm-svn: 344075
2018-10-09 20:22:18 +00:00
Rui Ueyama 714abece2b Simplify. NFC.
llvm-svn: 344074
2018-10-09 20:16:16 +00:00
Rui Ueyama 659cff37d6 Remove a use of template to make code less abstracted.
Sometimes, code that is a bit longer but doesn't use template is
easier to understand than code that uses template.

llvm-svn: 344072
2018-10-09 19:54:32 +00:00
Zachary Turner 2be11c0260 Fix lld test.
I removed this output from the dumper because it was non-portable,
but I didn't update the test which checked the output.

llvm-svn: 344066
2018-10-09 18:35:06 +00:00
Nico Weber e1b9331d29 lld-link: attempt to fix tests on Windows after r344061.
llvm-svn: 344065
2018-10-09 18:19:09 +00:00
Rui Ueyama c2921f002e Add an entry to the release notes.
llvm-svn: 344062
2018-10-09 17:53:33 +00:00
Nico Weber 4764bb2cb1 lld-link: Use /pdbsourcepath: for more places when present.
/pdbsourcepath: was added in https://reviews.llvm.org/D48882 to make it
possible to have relative paths in the debug info that clang-cl writes.
lld-link then makes the paths absolute at link time, which debuggers require.
This way, clang-cl's output is independent of the absolute path of the build
directory, which is useful for cacheability in distcc-like systems.

This patch extends /pdbsourcepath: (if passed) to also be used for:

1. The "cwd" stored in the env block in the pdb is /pdbsourcepath: if present
2. The "exe" stored in the env block in the pdb is made absolute relative
   to /pdbsourcepath: instead of the cwd
3. The "pdb" stored in the env block in the pdb is made absolute relative
   to /pdbsourcepath: instead of the cwd
4. For making absolute paths to .obj files referenced from the pdb

/pdbsourcepath: is now useful in three scenarios (the first one already working
before this change):

1. When building with full debug info, passing the real build dir to
   /pdbsourcepath: allows having clang-cl's output to be independent
   of the build directory path. This patch effectively doesn't change
   behavior for this use case (assuming the cwd is the build dir).

2. When building without compile-time debug info but linking with /debug,
   a fake fixed /pdbsourcepath: can be passed to get symbolized stacks
   while making the pdb and exe independent of the current build dir.
   For this two work, lld-link needs to be invoked with relative paths for
   the lld-link invocation itself (for "exe"), for the pdb output name, the exe
   output name (for "pdb"), and the obj input files, and no absolute path
   must appear on the link command (for "cmd" in the pdb's env block).
   Since no full debug info is present, it doesn't matter that the absolute
   path doesn't exist on disk -- we only get symbols in stacks.

3. When building production builds with full debug info that don't have
   local changes, and that get source indexed and their pdbs get uploaded
   to a symbol server. /pdbsourcepath: again makes the build output independent
   of the current directory, and the fixed path passed to /pdbsourcepath: can
   be given the source indexing transform so that it gets mapped to a
   repository path. This has the same requirements as 2.

This patch also makes it possible to create PDB files containing Windows-style
absolute paths when cross-compiling on a POSIX system.

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

llvm-svn: 344061
2018-10-09 17:52:25 +00:00
Nico Weber 9d7524160a lld-link: Implement support for %_PDB% and %_EXT% for /pdbaltpath:.
Warn that references to regular env vars are ignored.

Fixes PR38940.

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

llvm-svn: 344003
2018-10-08 23:06:05 +00:00
Rui Ueyama 9b5a495d48 Fix a broken buildbot.
llvm-svn: 343983
2018-10-08 17:24:29 +00:00
Rui Ueyama e28c146423 Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.

In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.

This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.

If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.

This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.

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

llvm-svn: 343979
2018-10-08 16:58:59 +00:00
Sid Manning 307c7901d0 [ELF][HEXAGON] Add R_HEX_GOT_16_X support
Differential Revision: https://reviews.llvm.org/D52909

llvm-svn: 343972
2018-10-08 15:32:46 +00:00
Petr Hosek 62f6462bf9 [llvm-nm] Write "no symbol" output to stderr
This matches the output of binutils' nm and ensures that any scripts
or tools that use nm and expect empty output in case there no symbols
don't break.

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

llvm-svn: 343887
2018-10-05 21:10:03 +00:00
Martin Storsjo 08ab568aaa [COFF] Do MinGW specific entry/subsystem inference
ld.bfd doesn't do any inference of subsystem; unless the windows
subsystem is specified, the console subsystem is used.

For the console subsystem, the entry point is called mainCRTStartup,
regardless of whether the the user code entry point is main or wmain.
The same goes for the windows subsystem, where the entry point always
is WinMainCRTStartup, for both WinMain and wWinMain in user code.

One detail that we don't emulate, is that if the inferred entry point
is undefined, ld.bfd silently just sets the entry point to the start
of the image. And if an explicit entry point is set, but it is
undefined, the link still succeeds but the linker warns about the
entry point not being found.

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

llvm-svn: 343879
2018-10-05 19:43:24 +00:00
Martin Storsjo db9148d21d [docs] Mention some notable feature in the release notes
Differential Revision: https://reviews.llvm.org/D52908

llvm-svn: 343878
2018-10-05 19:43:20 +00:00
Martin Storsjo cab6dafc04 [COFF] Cope with GCC produced weak aliases referring to comdat functions
For certain cases of inline functions written to comdat sections,
GCC 5.x produces a weak symbol in addition, which would end up
undefined in some cases.

This no longer seems to happen with GCC 6.x or newer though.

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

llvm-svn: 343877
2018-10-05 19:43:16 +00:00
Alexandre Ganea 149de8de19 [LLD][COFF] Fix ordering of CRT global initializers in COMDAT sections
(patch by Benoit Rousseau)

This patch fixes a bug where the global variable initializers were sometimes not invoked in the correct order when it involved a C++ template instantiation.

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

llvm-svn: 343847
2018-10-05 12:56:46 +00:00
Ed Maste bf378b808a ReleaseNotes: remove superfluous 's' in 'can promotes'
llvm-svn: 343820
2018-10-04 21:37:32 +00:00
Sid Manning 261eec5fa5 [ELF][HEXAGON] Add support for GOT relocations.
The GOT is referenced through the symbol _GLOBAL_OFFSET_TABLE_ .

The relocation added calculates the offset into the global offset table for
the entry of a symbol.  In order to get the correct TargetVA I needed to
create an new relocation expression, HEXAGON_GOT.  It does
Sym.getGotVA() - In.GotPlt->getVA().

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

llvm-svn: 343784
2018-10-04 14:54:17 +00:00
George Rimar f76dffb90f [ELF] - Simplify. NFCI.
Assign the `Link` to parent directly.

llvm-svn: 343762
2018-10-04 09:31:15 +00:00
Matt Morehouse 8d53ab5e7a Use std::make_pair rather than brace initialization.
r343732 broke the Windows bot.  Seems like the compiler on that bot
doesn't like brace initialization.

llvm-svn: 343749
2018-10-04 00:35:24 +00:00
Shoaib Meenai 509230f68d [ELF] Fix typo. NFC
llvm-svn: 343748
2018-10-04 00:07:59 +00:00
Shoaib Meenai 6883d882f1 [ELF] Fix crash on invalid undefined local symbols
r320770 made LLD handle invalid DSOs where local symbols were found in
the global part of the symbol table. Unfortunately, it didn't handle the
case where those local symbols were also undefined, and r326242 exposed
an assertion failure in that case. Just warn on that case instead of
crashing, by moving the local binding check before the undefined symbol
addition.

The input file for the test is crafted by hand, since I don't know of
any tool that would produce such a broken DSO. I also don't understand
what it even means for a symbol to be undefined but have STB_LOCAL
binding - I don't think that combination makes any sense - but we have
found broken DSOs of this nature that we were linking against. I've
included detailed instructions on how to produce the DSO in the test.

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

llvm-svn: 343745
2018-10-03 23:53:11 +00:00
Jordan Rupprecht dd0f98972d [llvm-nm] Update lld tests based on changed llvm-nm output
llvm-svn: 343743
2018-10-03 23:39:59 +00:00
Derek Schuff 371842b853 [WebAssembly] Refactor use of signatures
Update use of WebAssemblySignature to go along with D52580

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

llvm-svn: 343734
2018-10-03 22:25:32 +00:00
Rui Ueyama 3ed33eade0 Minor refacotring of Relocations.cpp. NFC.
This patch splits ThunkCreator::mergeThunks into two smaller functions.
Also adds blank lines to various places so that the code doesn't look
too dense.

llvm-svn: 343732
2018-10-03 22:20:26 +00:00
Sean Fertile 98da20f0fb [PPC64] Test documenting toc-restore after interposable recursive call. [NFC]
A test verifying that toc restores are properly inserted following recursive
calls, as well as briefly describing why they are needed.

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

llvm-svn: 343729
2018-10-03 21:58:44 +00:00
Sean Fertile 7cbe41f2c7 [PPC64] Add test documenting toc-restore when linking with -Bsymbolic. [NFC]
Differential Revision: https://reviews.llvm.org/D52523

llvm-svn: 343728
2018-10-03 21:58:42 +00:00
Martin Storsjo 2657200274 [COFF] Cope with weak aliases produced by GNU tools
When GNU tools create a weak alias, they produce a strong symbol
named .weak.<weaksymbol>.<relatedstrongsymbol>.

GNU ld allows many such weak alternatives for the same weak symbol, and
the linker picks the first one encountered.

This can't be reproduced by assembling from .s files, since llvm-mc
produces symbols named .weak.<weaksymbol>.default in these cases.

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

llvm-svn: 343704
2018-10-03 18:31:53 +00:00
Nico Weber d377826277 lld-link: Several tweaks to default entry point selection.
Three related changes:

1. link.exe uses the presence of main and wmain to decide if it should call
   mainCRTStartup or wmainCRTStartup, even if /nodefaultlib is passed. For
   compatibility, remove FindMain logic.

2. Default to the non-wide entrypoint if main is not found. This has two effects:

2a. In normal links, lld-link now prints

        lld-link: error: undefined symbol: _main
        >>> referenced by f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl:78
        >>>               libcmt.lib(exe_main.obj):("int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ))
        >>> referenced by f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl:283
        >>>               libcmt.lib(exe_main.obj):("int __cdecl __scrt_common_main_seh(void)" (?__scrt_common_main_seh@@YAHXZ))

    instead of

        lld-link: error: entry point must be defined

    This is arguably a better error message, since it now mentions that _main is
    missing. (This matches link.exe's diagnostic in this case.)

2b. With /nodefautlib, we now default to mainCRTStartup if no main() is
    present, again matching link.exe. This makes r337407 obsolete.

This means if you have a cc file containing both mainCRTStartup and
wmainCRTStartup and you pass /nodefaultlib /subsystem:console, lld-link will
now call mainCRTStartup, matching link.exe

3. Print a warning if both main and wmain are present, similar to link.exe's
   LNK4067.

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

llvm-svn: 343698
2018-10-03 17:01:39 +00:00
George Rimar db2e641fd7 [ELF] - Fix BB after r343668
This stops testing the value of .rela.plt section offset.

Also makes _start global to eliminate
'cannot find entry symbol _start' warning.

llvm-svn: 343669
2018-10-03 09:59:08 +00:00
George Rimar f79a8ef2ae [ELF] - Do not forget to include to .dymsym symbols that were converted to Defined.
This is the fix for
"Bug 39104 - LLD links incorrect ELF executable if version script contains "local: *;"
(https://bugs.llvm.org/show_bug.cgi?id=39104).

The issue happens when we have non-PIC program call to function in a shared library.
(for example, the PR above has R_X86_64_PC32 relocation against __libc_start_main)

LLD converts symbol to Defined in that case with the use of replaceWithDefined()

The issue is that after above we create a broken relocation because do not
include the symbol into .dynsym.

That happens when the version script is used because we treat the symbol as
STB_LOCAL if the following condition match:
VersionId == VER_NDX_LOCAL && isDefined() and do not include it to
.dynsym because of that. Patch fixes the issue.

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

llvm-svn: 343668
2018-10-03 09:33:00 +00:00
George Rimar 3832d7c25d [LLD] - Do not crash when reporting errors when .debug_line_str (DWARF5) is used
Imagine we have the following code:

int foo();
int main() { return foo(); }

It will crash if you try to compile it with
`clang -O0 -gdwarf-5 test.cpp -o test -g -fuse-ld=lld`

The crash happens inside the LLVM DWARF parser because LLD does not provide
the .debug_line_str section. At the same time for correct parsing and reporting,
we anyways need to provide this section from our side.

The patch fixes the issue.

llvm-svn: 343667
2018-10-03 08:59:46 +00:00
Ali Tamur 63830b2794 Introduce a flag to warn when ifunc symbols are used with text relocations.
Summary:
This patch adds a new flag, --warn-ifunc-textrel, to work around a glibc bug. When a code with ifunc symbols is used to produce an object file with text relocations, lld always succeeds. However, if that object file is linked using an old version of glibc, the resultant binary just crashes with segmentation fault when it is run (The bug is going to be corrected as of glibc 2.19).

Since there is no way to tell beforehand what library the object file will be linked against in the future, there does not seem to be a fool-proof way for lld to give an error only in cases where the binary will crash. So, with this change (dated 2018-09-25), lld starts to give a warning, contingent on a new command line flag that does not have a gnu counter part. The default value for --warn-ifunc-textrel is false, so lld behaviour will not change unless the user explicitly asks lld to give a warning. Users that link with a glibc library with version 2.19 or newer, or does not use ifunc symbols, or does not generate object files with text relocations do not need to take any action. Other users may consider to start passing warn-ifunc-textrel to lld to get early warnings.

Reviewers: ruiu, espindola

Reviewed By: ruiu

Subscribers: grimar, MaskRay, markj, emaste, arichardson, llvm-commits

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

llvm-svn: 343628
2018-10-02 20:30:22 +00:00
Diogo N. Sampaio a35e55eff9 [NFC][BUG-FIX][ARM] Add missing data symbol
llvm rL343594: [ARM] Emmit data symbol
for constant pool data fixed a bug that ommited
required data symbols.
Such change breaked a test case in lld:
test/ELF/arm-thunk-largesection.s

llvm-svn: 343604
2018-10-02 16:53:12 +00:00
Michael J. Spencer 8222f902ca [ELF] Read the call graph profile from object files.
This uses the call graph profile embedded in the object files to construct the call graph.

This is read from a SHT_LLVM_CALL_GRAPH_PROFILE (0x6fff4c02) section as (uint32_t, uint32_t, uint64_t) tuples as (from symbol index, to symbol index, weight).

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

llvm-svn: 343552
2018-10-02 00:17:15 +00:00
Sid Manning 490429d069 [ELF] [HEXAGON] Add support for PLT_B22_PCREL and HEX_32_PCREL
Update testcase.

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

llvm-svn: 343515
2018-10-01 18:27:26 +00:00
Rui Ueyama 1e591d807b Add an explanation about cross linking.
Differential Revision: https://reviews.llvm.org/D52567

llvm-svn: 343495
2018-10-01 16:41:58 +00:00
James Henderson 194a4eb8f7 Fix typo in LLD man page
llvm-svn: 343449
2018-10-01 09:57:20 +00:00
Martin Storsjo 0f8f0d6d1d [COFF] In MinGW mode, ignore relocations against a discarded section
When GCC produces a jump table as part of a comdat function, the
jump table itself is produced as plain non-comdat rdata section. When
linked with ld.bfd, all of those rdata sections are kept, with
relocations unchanged in the sections that refer to discarded comdat
sections.

This has been observed with at least GCC 5.x and 7.x.

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

llvm-svn: 343422
2018-09-30 18:31:03 +00:00
Alexandre Ganea 91def5cc6a [LLD][COFF] Fix pdb loading when the path points to a removable device
Differential Revision: https://reviews.llvm.org/D52666

llvm-svn: 343366
2018-09-28 21:53:40 +00:00
Sam Clegg 8b0b48f343 [WebAssembly] Preserve function signatures during LTO
With LTO when and undefined function (with a known signature)
in replaced by a defined bitcode function we were loosing the
signature information (since bitcode functions don't have
signatures).

With this change we preserve the original signature from the
undefined function and verify that the post LTO compiled
function has the correct signature.

This change improves the error handling in the case where
there is a signature mismatch with a function defined in
a bitcode file.

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

llvm-svn: 343340
2018-09-28 16:50:14 +00:00
Sid Manning fc50e63a2a [ELF][HEXAGON] Add support for dynamic libraries
Write out the PLT header and stub.
Hexagon uses RELA relocations.

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

llvm-svn: 343320
2018-09-28 14:09:16 +00:00
Sam Clegg 305b0343ce [WebAssembly] Add --[no]-export-dynamic to replace --export-default
In a very recent change I introduced a --no-export-default flag
but after conferring with others it seems that this feature already
exists in gnu GNU ld and lld in the form the --export-dynamic flag
which is off by default.

This change replaces export-default with export-dynamic and also
changes the default to match the traditional linker behaviour.

Now, by default, only the entry point is exported.  If other symbols
are required by the embedder then --export-dynamic or --export can
be used to export all visibility hidden symbols or individual
symbols respectively.

This change touches a lot of tests that were relying on symbols
being exported by default.  I imagine it will also effect many
users but do think the change is worth it match of the traditional
behaviour and flag names.

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

llvm-svn: 343265
2018-09-27 21:06:25 +00:00
Peter Smith fb5a5d09fe [COFF] Add missing Requires x86 to fix buildbot
Add REQUIRES: x86 to pdb-debug-f.s as this is causing the Arm and
AArch64 buildbots to fail as they do not have the x86 backend.

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

llvm-svn: 343196
2018-09-27 12:07:47 +00:00
Sam Clegg 4aad12ce0b [WebAssembly] Update Config member to match command line option
Subscribers: dschuff, jgravelle-google, aheejin, sunfish, llvm-commits

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

llvm-svn: 343157
2018-09-27 00:46:54 +00:00
Sam Clegg bd37e09355 Revert "[WebAssembly] Update docs"
This change reverts rL343155.  It broke the builtbot, even though
it works for me locally:
http://lab.llvm.org:8011/builders/lld-sphinx-docs/builds/26336

Must be a sphinx version issue I guess.

llvm-svn: 343156
2018-09-27 00:42:49 +00:00
Sam Clegg 77ab7c1b30 [WebAssembly] Update docs
Differential Revision: https://reviews.llvm.org/D52048

llvm-svn: 343155
2018-09-27 00:22:24 +00:00
Fangrui Song dbaeec6892 [ELF] llvm::sort(C.begin(), C.end(), ...) -> llvm::sort(C, ...)
Summary: The convenience wrapper in STLExtras is available since rL342102.

Reviewers: ruiu, espindola

Subscribers: emaste, arichardson, mgrang, llvm-commits

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

llvm-svn: 343146
2018-09-26 20:54:42 +00:00
Ryan Prichard ed5bb932a2 Allow later -z name=<int> args to override earlier args
Summary:
lld already gives later -z options precedence in getZFlag().

This matches the behavior of ld.bfd and ld.gold, where later options
override earlier ones. (I tested with -z max-page-size and -z stack-size.)

Reviewers: ruiu, espindola, grimar

Reviewed By: ruiu, grimar

Subscribers: grimar, emaste, arichardson, llvm-commits

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

llvm-svn: 343145
2018-09-26 20:50:39 +00:00
Ryan Prichard b7c8d2d405 [AArch64] Fix range check of R_AARCH64_TLSLE_ADD_TPREL_HI12
Summary:
An AArch64 LE relocation is a positive ("variant 1") offset. This
relocation is used to write the upper 12 bits of a 24-bit offset into an
add instruction:

    add x0, x0, :tprel_hi12:v1

The comment in the ARM docs for R_AARCH64_TLSLE_ADD_TPREL_HI12 is:

"Set an ADD immediate field to bits [23:12] of X; check 0 <= X < 2^24."

Reviewers: javed.absar, espindola, ruiu, peter.smith, zatrazz

Reviewed By: ruiu

Subscribers: emaste, arichardson, kristof.beyls, llvm-commits

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

llvm-svn: 343144
2018-09-26 20:50:38 +00:00
Fangrui Song 5dffc95b26 [ELF][test] Use llvm-readelf's short option -r instead of -relocations and remove ignored --wide
Reviewers: ruiu, sfertile, espindola

Reviewed By: ruiu

Subscribers: jsji, emaste, nemanjai, arichardson, kbarton, llvm-commits

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

llvm-svn: 343135
2018-09-26 19:48:07 +00:00
George Rimar 95aae4c59d [ELF] - Do not fail on R_*_NONE relocations when parsing the debug info.
This is https://bugs.llvm.org//show_bug.cgi?id=38919.

Currently, LLD may report "unsupported relocation target while parsing debug info"
when parsing the debug information.

At the same time LLD does that for zeroed R_X86_64_NONE relocations,
which obviously has "invalid" targets.

The nature of R_*_NONE relocation assumes them should be ignored.
This patch teaches LLD to stop reporting the debug information parsing errors for them.

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

llvm-svn: 343078
2018-09-26 08:11:34 +00:00
Martin Storsjo 32e651e169 [COFF] Don't do autoexport of symbols from GNU import libraries
This involves adding more generic list of symbol suffixes/prefixes
to ignore for autoexport; adding a few other entries to these lists
as well from the corresponding lists in binutils.

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

llvm-svn: 343070
2018-09-26 06:13:47 +00:00
Martin Storsjo 2bfa125fd6 [COFF] Allow automatic dllimport from gnu import libraries
Don't assume that the IAT chunk will be a DefinedImportData, it can
just as well be a DefinedRegular for gnu import libraries.

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

llvm-svn: 343069
2018-09-26 06:13:39 +00:00
Sam Clegg 4e62418b25 [WebAssembly] Add --export-default/--no-export-default options
These option control weather or not symbols marked as visibility
default are exported in the output binary.

By default this is true, but emscripten prefers to control the
exported symbol list explicitly at link time and ignore the
symbol attributes.

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

llvm-svn: 343034
2018-09-25 21:50:15 +00:00
Rui Ueyama 378a028d91 Update a --help message and add -execute-only to the man page.
llvm-svn: 343032
2018-09-25 21:39:08 +00:00
Rui Ueyama 12ef7a9575 De-template VersionDefinitionSection. NFC.
When we write a struct to a mmap'ed buffer, we usually use
write16/32/64, but we didn't for VersionDefinitionSection, so
we needed to template that class.

llvm-svn: 343024
2018-09-25 20:37:51 +00:00
Rui Ueyama 4e247522ac Reset input section pointers to null on each linker invocation.
Previously, if you invoke lld's `main` more than once in the same process,
the second invocation could fail or produce a wrong result due to a stale
pointer values of the previous run.

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

llvm-svn: 343009
2018-09-25 19:26:58 +00:00
Rui Ueyama ae34348354 Fix an error message. It must start with a lowercase letter.
llvm-svn: 342992
2018-09-25 17:12:50 +00:00
Rui Ueyama 7d053709d4 Parallelize .gdb_index string table writes.
When we are creating a large .gdb_index, this change makes a difference.

llvm-svn: 342978
2018-09-25 14:34:56 +00:00
Martin Storsjo 57ddec0dd1 [COFF] Add support for creating range extension thunks for ARM
This is a feature that MS link.exe lacks; it currently errors out on
such relocations, just like lld did before.

This allows linking clang.exe for ARM - practically, any image over
16 MB will likely run into the issue.

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

llvm-svn: 342962
2018-09-25 10:59:29 +00:00
George Rimar b5a6538b07 [ELF] - Add -z global option to manual.
This was requested during review of D49374,
but for some unknown reason was not in the final commit.

llvm-svn: 342954
2018-09-25 09:32:31 +00:00
Yury Delendik d66aabc6d7 [WebAssembly] Move .debug_line section address of dead function outside section range
Summary:
Currently we are pointing all debug information that refer removed function code
to the beginning of the code section (offset = 0). A debugger may want to
resolve code offset to the debug information, which will collide with offsets
of the live functions.

Moving offsets of dead functions outside code section range.

Reviewers: sbc100

Reviewed By: sbc100

Subscribers: dblaikie, ruiu, alexcrichton, dschuff, aprantl, jgravelle-google, aheejin, sunfish, JDevlieghere, llvm-commits

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

llvm-svn: 342930
2018-09-24 23:50:55 +00:00
Will Wilson 3cb18346d7 [lld-link] Generalize handling of /debug and /debug:{none,full,fastlink,ghash,symtab}
Implement final argument precedence if multiple /debug arguments are passed on the command-line to match expected link.exe behavior.
Support /debug:none and emit warning for /debug:fastlink with automatic fallback to /debug:full.
Emit error if last /debug:option is unknown.
Emit warning if last /debugtype:option is unknown.

https://reviews.llvm.org/D50404

llvm-svn: 342894
2018-09-24 15:28:03 +00:00
Ed Maste c8ec7a9e1e Correct RISC-V link in release notes
llvm-svn: 342887
2018-09-24 14:47:56 +00:00
Martin Storsjo 5f6d527f09 [COFF] Support linking to import libraries from GNU binutils
GNU binutils import libraries aren't the same kind of short import
libraries as link.exe and LLD produce, but are a plain static library
containing .idata section chunks. MSVC link.exe can successfully link
to them.

In order for imports from GNU import libraries to mix properly with the
normal import chunks, the chunks from the existing mechanism needs to
be added into named sections like .idata$2.

These GNU import libraries consist of one header object, a number of
object files, one for each imported function/variable, and one trailer.
Within the import libraries, the object files are ordered alphabetically
in this order. The chunks stemming from these libraries have to be
grouped by what library they originate from and sorted, to make sure
the section chunks for headers and trailers for the lists are ordered
as intended. This is done on all sections named .idata$*, before adding
the synthesized chunks to them.

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

llvm-svn: 342777
2018-09-21 22:01:06 +00:00
Rui Ueyama c41985a50e Update release notes.
llvm-svn: 342754
2018-09-21 17:48:45 +00:00
Rui Ueyama a3d0f40964 Update release notes.
llvm-svn: 342748
2018-09-21 17:13:55 +00:00
Dimitry Andric c9de3b4d26 Align AArch64 and i386 image base to superpage
Summary:

As for x86_64, the default image base for AArch64 and i386 should be
aligned to a superpage appropriate for the architecture.

On AArch64, this is 2 MiB, on i386 it is 4 MiB.

Reviewers: emaste, grimar, javed.absar, espindola, ruiu, peter.smith, srhines, rprichard

Reviewed By: ruiu, peter.smith

Subscribers: jfb, markj, arichardson, krytarowski, kristof.beyls, llvm-commits

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

llvm-svn: 342746
2018-09-21 16:58:13 +00:00
Petr Hosek e717ae2117 [ELF] Use the Repl point to avoid the segfault when using ICF
This addresses PR38918.

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

llvm-svn: 342704
2018-09-21 00:55:42 +00:00
Rui Ueyama 180cd0717f Make a member function non-member. NFC.
Non-member functions are generally preferred over member functions
because it is clear that non-member functions don't depend on an
internal state of an object.

llvm-svn: 342695
2018-09-20 22:58:00 +00:00
Thomas Lively d661e265c7 [WebAssembly] Add v128 value type
Reviewers: sbc100, aheejin, dschuff

Subscribers: jgravelle-google, sunfish, llvm-commits

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

llvm-svn: 342690
2018-09-20 22:07:18 +00:00
Rui Ueyama a071669956 Rename a function. NFC.
llvm-svn: 342687
2018-09-20 21:40:38 +00:00
Rui Ueyama d19bc04869 Make variable names shorter. NFC.
llvm-svn: 342686
2018-09-20 21:29:14 +00:00
Rui Ueyama 9932091d87 Simplify. NFC.
llvm-svn: 342685
2018-09-20 21:18:56 +00:00
Rui Ueyama 11cda0cbae Add paretntheses around a C macro parameter.
llvm-svn: 342673
2018-09-20 18:24:19 +00:00
Sid Manning 6d0e4fce1a [ELF][HEXAGON] Set DefaultMaxPageSize to 64K
Update testcase to reflect the change.

llvm-svn: 342662
2018-09-20 16:32:19 +00:00
Zachary Turner 77bbd7b19d Fix one more test failure.
llvm-svn: 342660
2018-09-20 16:18:15 +00:00
Sean Fertile 69e09116ba [PPC64] Handle ppc64le triple in getBitcodeMachineKind.
Enables lto and thinlto with bitcode targeting ppc64le.

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

llvm-svn: 342604
2018-09-20 00:26:49 +00:00
Sean Fertile e0e586b997 [PPC64] Helper for offset from a function's global entry to local entry. [NFC]
The PPC64 elf V2 abi defines 2 entry points for a function. There are a few
places we need to calculate the offset from the global entry to the local entry
and how this is done is not straight forward. This patch adds a helper function
mostly for documentation purposes, explaining how the 2 entry points differ and
why we choose one over the other, as well as documenting how the offsets are
encoded into a functions st_other field.

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

llvm-svn: 342603
2018-09-20 00:26:47 +00:00
Sean Fertile 7f3f05e0b7 [PPC64] Optimize redundant instructions in global access sequences.
The access sequence for global variables in the medium and large code models use
2 instructions to add an offset to the toc-pointer. If the offset fits whithin
16-bits then the instruction that sets the high 16 bits is redundant.

This patch adds the --toc-optimize option, (on by default) and enables rewriting
of 2 instruction global variable accesses into 1 when the offset from the
TOC-pointer to the variable (or .got entry) fits in 16 signed bits. eg

addis %r3, %r2, 0           -->     nop
addi  %r3, %r3, -0x8000     -->     addi %r3, %r2, -0x8000

This rewriting can be disabled with the --no-toc-optimize flag

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

llvm-svn: 342602
2018-09-20 00:26:44 +00:00
Martin Storsjo 5fefad793c [COFF] Fix the name mangling of a function in the autoexport exclusion list
The __NULL_IMPORT_DESCRIPTOR symbol has two leading underscores on
architectures other than i386 as well; it is not a mangled symbol name.

llvm-svn: 342448
2018-09-18 07:22:05 +00:00
Martin Storsjo 32d21d6a2d [COFF] Add support for delay loading DLLs for ARM64
Differential Revision: https://reviews.llvm.org/D52190

llvm-svn: 342447
2018-09-18 07:22:01 +00:00
Martin Storsjo cb9570eb22 [COFF] Fix a block with incorrect indentation. NFC.
llvm-svn: 342446
2018-09-18 07:21:55 +00:00
Ryan Prichard 1c33d14bcd [ELF] Set Out::TlsPhdr earlier for encoding packed reloc tables
Summary:
For --pack-dyn-relocs=android, finalizeSections calls
LinkerScript::assignAddresses and
AndroidPackedRelocationSection::updateAllocSize in a loop,
where assignAddresses lays out the ELF image, then updateAllocSize
determines the size of the Android packed relocation table by encoding it.
Encoding the table requires knowing the values of relocation addends.

To get the addend of a TLS relocation, updateAllocSize can call getSymVA
on a TLS symbol before setPhdrs has initialized Out::TlsPhdr, producing an
error:

    <file> has an STT_TLS symbol but doesn't have an SHF_TLS section

Fix the problem by initializing Out::TlsPhdr immediately after the program
headers are created. The segment's p_vaddr field isn't initialized until
setPhdrs, so use FirstSec->Addr, which is what setPhdrs would use.
FirstSec will typically refer to the .tdata or .tbss output section, whose
(tentative) address was computed by assignAddresses.

Android currently avoids this problem because it uses emutls and doesn't
support ELF TLS. This problem doesn't apply to --pack-dyn-relocs=relr
because SHR_RELR only handles relative relocations without explicit addends
or info.

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

Reviewers: ruiu, pcc, chh, javed.absar, espindola

Subscribers: emaste, arichardson, llvm-commits, srhines

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

llvm-svn: 342432
2018-09-18 00:24:48 +00:00
Sean Fertile 72435e5701 [PPC64] Support relaxing R_PPC64_TLSGD16 in TlsGdtoLe relaxation.
A General-dynamic tls access can be written using a R_PPC64_TLSGD16 relocation
if the target got entry is within 16 bits of the TOC-base. This patch adds
support for R_PPC64_TLSGD16 by relaxing it the same as a R_PPC64_GOT_TLSGD16_LO.

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

llvm-svn: 342411
2018-09-17 20:27:02 +00:00
Sean Fertile eed3e43e54 [PPC64] Helpers for read/write an instr while relocating a half16 type. [NFC]
There are a growing number of places when we either want to read or write an
instruction when handling a half16 relocation type. On big-endian the buffer
pointer is pointing into the middle of the word we want and on little-endian it
is pointing to the start of the word. These 2 helpers are to simplify reading
and writing in these contexts.

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

llvm-svn: 342410
2018-09-17 20:27:00 +00:00
Nico Weber 5ffd8cedf4 lld-link: Also demangle undefined dllimported symbols.
dllimported symbols go through an import stub that's called __imp_ followed by
the name the stub points to. Make that work.

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

llvm-svn: 342401
2018-09-17 16:31:20 +00:00
Fangrui Song 3e0a54e9da [ELF] Use llvm::toLower instead of libc call tolower
tolower() has some overhead because current locale is considered (though in lld the default "C" locale is used which does not matter too much). llvm::toLower is more efficient as it compiles to a compare and a conditional jump, as opposed to a libc call if tolower is used.

Disregarding locale also matches gdb's behavior (gdb/minsyms.h):

    #define SYMBOL_HASH_NEXT(hash, c)			\
      ((hash) * 67 + TOLOWER ((unsigned char) (c)) - 113)

where TOLOWER (include/safe-ctype.h) is a macro that uses a lookup table under the hood which is similar to llvm::toLower.

Reviewers: ruiu, espindola

Subscribers: emaste, arichardson, llvm-commits

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

llvm-svn: 342342
2018-09-15 23:59:13 +00:00
Nico Weber 0bd2d304e6 lld-link: Set PDB GUID to hash of PDB contents instead of to a random byte sequence.
Previously, lld-link would use a random byte sequence as the PDB GUID. Instead,
use a hash of the PDB file contents.

To not disturb llvm-pdbutil pdb2yaml, the hash generation is an opt-in feature
on InfoStreamBuilder and ldb/COFF/PDB.cpp always sets it.

Since writing the PDB computes this ID which also goes in the exe, the PDB
writing code now must be called before writeBuildId(). writeBuildId() for that
reason is no longer included in the "Code Layout" timer.

Since the PDB GUID is now a function of the PDB contents, the PDB Age is always
set to 1. There was a long comment above loadExistingBuildId (now gone) about
how not changing the GUID and only incrementing the age was important, but
according to the discussion in PR35914 that comment was incorrect.

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

llvm-svn: 342334
2018-09-15 18:37:22 +00:00
Nico Weber da15acbd68 lld-link: print demangled symbol names for "undefined symbol" diagnostics
For this, add a few toString() calls when printing the "undefined symbol"
diagnostics; toString() already does demangling on Windows hosts.

Also make lld::demangleMSVC() (called by toString(Symbol*)) call LLVM's
microsoftDemangle() instead of UnDecorateSymbolName() so that it works on
non-Windows hosts – this makes both updating tests easier and provides a better
user experience for people doing cross-links.

This doesn't yet do the right thing for symbols starting with __imp_, but that
can be improved in a follow-up.

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

llvm-svn: 342332
2018-09-15 18:27:09 +00:00
Rui Ueyama 01d250eed9 Style fix. NFC.
llvm-svn: 342300
2018-09-14 23:51:17 +00:00
Rui Ueyama 970573776a Rename GdbIndex.{cpp,h} -> DWARF.{cpp,h}.
These files used to contain classes and functions for .gdb_index,
but they are moved to SyntheticSections.{cpp,h}, so the name is now
irrelevant.

llvm-svn: 342299
2018-09-14 23:51:05 +00:00
Rui Ueyama e7688e6663 Revert r342297: Discard uncompressed buffer after creating .gdb_index contents.
Looks like it broke some local builds that use -gdb-index.

llvm-svn: 342298
2018-09-14 23:28:13 +00:00
Rui Ueyama 751dfbe39b Discard uncompressed buffer after creating .gdb_index contents.
Once we create .gdb_index contents, .zdebug_gnu_pub{names,types}
are useless, so there's no need to keep their uncompressed data
in memory.

I observed that for a test case in which lld creates a 3GB .gdb_index
section, the maximum resident set size reduced from 43GB to 29GB after
this patch.

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

llvm-svn: 342297
2018-09-14 22:57:39 +00:00
Martin Storsjo 7a41693898 [COFF] Provide __CTOR_LIST__ and __DTOR_LIST__ symbols for MinGW
MinGW uses these kind of list terminator symbols for traversing
the constructor/destructor lists. These list terminators are
actual pointers entries in the lists, with the values 0 and
(uintptr_t)-1 (instead of just symbols pointing to the start/end
of the list).

(This mechanism exists in both the mingw-w64 crt startup code and
in libgcc; normally the mingw-w64 one is used, but a DLL build of
libgcc uses the libgcc one. Therefore it's not trivial to change
the mechanism without lots of cross-project synchronization and
potentially invalidating some combinations of old/new versions
of them.)

When mingw-w64 has been used with lld so far, the CRT startup object
files have so far provided these symbols, ending up with different,
incompatible builds of the CRT startup object files depending on
whether binutils or lld are going to be used.

In order to avoid the need of different configuration of the CRT startup
object files depending on what linker to be used, provide these symbols
in lld instead. (Mingw-w64 checks at build time whether the linker
provides these symbols or not.) This unifies this particular detail
between the two linkers.

This does disallow the use of the very latest lld with older versions
of mingw-w64 (the configure check for the list was added recently;
earlier it simply checked whether the CRT was built with gcc or clang),
and requires rebuilding the mingw-w64 CRT. But the number of users of
lld+mingw still is low enough that such a change should be tolerable,
and unifies this aspect of the toolchains, easing interoperability
between the toolchains for the future.

The actual test for this feature is added in ctors_dtors_priority.s,
but a number of other tests that checked absolute output addresses
are updated.

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

llvm-svn: 342294
2018-09-14 22:26:59 +00:00
Ed Maste c0b474f67a lld: add -z interpose support
-z interpose sets the DF_1_INTERPOSE flag, marking the object as an
interposer.

Via FreeBSD PR 230604, linking Valgrind with lld failed.

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

llvm-svn: 342239
2018-09-14 14:25:37 +00:00
Martin Storsjo 4c1b815d39 [COFF] Allow embedded directives to be separated by null bytes
The PE spec says that they will be separated by spaces, but link.exe
handles it just fine if they are separated by null bytes as well.

This adds tests to the lld repo, with the actual functional change
in LLVM in SVN r342204.

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

llvm-svn: 342206
2018-09-14 06:08:56 +00:00
Martin Storsjo 4c201a8ba5 [COFF] Avoid copying of chunk vectors. NFC.
When declaring the pair variable as "auto Pair : Map", it is
effectively declared as
std::pair<std::pair<StringRef, uint32_t>, std::vector<Chunk *>>.
This effectively does a full, shallow copy of the Chunk vector,
just to be thrown away after each iteration.

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

llvm-svn: 342205
2018-09-14 06:08:51 +00:00
Rui Ueyama 11ca38f421 COFF: Add support for /force:multiple option
Patch by Thomas Roughton.

This patch adds support for linking with multiple definitions to LLD's
COFF driver, in line with link.exe's /force:multiple option.

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

llvm-svn: 342191
2018-09-13 22:05:10 +00:00
Nico Weber f1828e3240 lld-link: For nonexisting inputs, omit follow-on diagnostics
For lld-link missing.obj, lld-link currently prints:

  lld-link: error: could not open foo.obj: No such file or directory
  lld-link: warning: /machine is not specified. x64 is assumed
  lld-link: error: subsystem must be defined

The 2nd and 3rd diagnostics are consequences of the input not existing and are
not interesting. If input files are missing, the best thing we can do is point
that out and then return.

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

llvm-svn: 342158
2018-09-13 18:13:21 +00:00
Peter Smith f31f596152 [ELF] Guard --fix-cortex-a53-843419 against --just-syms
If --just-syms is used the mapping symbols from the ELF file will be
absolute symbols with no section. The code to process mapping symbols in
--fix-cortex-a53-843419 assumes that these symbols have a defining section
so a crash will result when --just-syms is used. The simple fix is to not
process the symbol when it doesn't have a section.

Fixes PR37971

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

llvm-svn: 342146
2018-09-13 15:49:13 +00:00
Azharuddin Mohammed 4dce517c47 Add missing REQUIRES x86 to test/COFF/icf-safe.s
Reviewers: ruiu, pcc

Reviewed By: ruiu

Subscribers: llvm-commits

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

llvm-svn: 342145
2018-09-13 15:40:19 +00:00
Azharuddin Mohammed e41386b446 Fix typo in error message. NFC.
Summary: It should be "wasm-ld", not "wasm-lld".

Reviewers: ruiu

Reviewed By: ruiu

Subscribers: aheejin, sunfish, llvm-commits

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

llvm-svn: 342144
2018-09-13 15:37:13 +00:00
Zachary Turner a1f85f8bdd [PDB] Emit old fpo data to the PDB file.
r342003 added support for emitting FPO data from the
DEBUG_S_FRAMEDATA subsection of the .debug$S section to the PDB
file.  However, that is not the end of the story.  FPO can end
up in two different destinations in a PDB, each corresponding to
a different FPO data source.

The case handled by r342003 involves copying data from the
DEBUG_S_FRAMEDATA subsection of the .debug$S section to the
"New FPO" stream in the PDB, which is then referred to by the
DBI stream.  The case handled by this patch involves copying
records from the .debug$F section of an object file to the "FPO"
stream (or perhaps more aptly, the "Old FPO" stream) in the PDB
file, which is also referred to by the DBI stream.

The formats are largely similar, and the difference is mostly
only visible in masm generated object files, such as some of the
low-level CRT object files like memcpy.  MASM doesn't appear to
support writing the DEBUG_S_FRAMEDATA subsection, and instead
just writes these records to the .debug$F section.

Although clang-cl does not emit a .debug$F section ever, lld still
needs to support it so we have good debugging for CRT functions.

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

llvm-svn: 342080
2018-09-12 21:02:01 +00:00
Chih-Hung Hsieh 73e04847bf [ELF] Revert "Also demote lazy symbols."
This reverts commit https://reviews.llvm.org/rL330869
for a regression to link Android dex2oatds.

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

llvm-svn: 342007
2018-09-11 23:00:36 +00:00
Zachary Turner 42e7cc1b0f [PDB] Write FPO Data to the PDB.
llvm-svn: 342003
2018-09-11 22:35:01 +00:00
Reid Kleckner a6f64265ea [codeview] Decode and dump FP regs from S_FRAMEPROC records
Summary:
There are two registers encoded in the S_FRAMEPROC flags: one for locals
and one for parameters. The encoding is described by the
ExpandEncodedBasePointerReg function in cvinfo.h. Two bits are used to
indicate one of four possible values:

  0: no register - Used when there are no variables.
  1: SP / standard - Variables are stored relative to the standard SP
     for the ISA.
  2: FP - Variables are addressed relative to the ISA frame
     pointer, i.e. EBP on x86. If realignment is required, parameters
     use this. If a dynamic alloca is used, locals will be EBP relative.
  3: Alternative - Variables are stored relative to some alternative
     third callee-saved register. This is required to address highly
     aligned locals when there are dynamic stack adjustments. In this
     case, both the incoming SP saved in the standard FP and the current
     SP are at some dynamic offset from the locals. LLVM uses ESI in
     this case, MSVC uses EBX.

Most of the changes in this patch are to pass around the CPU so that we
can decode these into real, named architectural registers.

Subscribers: hiraditya

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

llvm-svn: 341999
2018-09-11 22:00:50 +00:00
Rui Ueyama 98e4a5ca11 Simplify.
Instead of Map<StringRef, bool>, we can simply use Set<StringRef>.

llvm-svn: 341948
2018-09-11 14:37:27 +00:00
Rui Ueyama 10454c4fd5 Style fix.
llvm-svn: 341937
2018-09-11 13:41:58 +00:00
Petr Hosek c6a233a68a [ELF] Don't emit .relr.dyn section if there are no relocs
This resolves PR38875.

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

llvm-svn: 341870
2018-09-10 21:54:56 +00:00
Martin Storsjo db62913f28 [MinGW] Hook up the --require-defined option to -include:
Differential Revision: https://reviews.llvm.org/D51840

llvm-svn: 341846
2018-09-10 17:41:40 +00:00
Alexandre Ganea 472e9b0ab2 Buildfix for r341825
llvm-svn: 341827
2018-09-10 14:07:11 +00:00
Alexandre Ganea d93b07f0b0 [LLD][COFF] Cleanup error messages / add more coverage tests
- Log the reason for a PDB or precompiled-OBJ load failure
- Properly handle out-of-date PDB or precompiled-OBJ signature by displaying a corresponding error
- Slightly change behavior on PDB failure: any subsequent load attempt from another OBJ would result in the same error message being logged
- Slightly change behavior on PDB failure: retry with filename only if previous error was ENOENT ("no such file or directory")
- Tests: a. for native PDB errors; b. cover all the cases above

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

llvm-svn: 341825
2018-09-10 13:51:21 +00:00
Nico Weber cc08366035 Remove an effectively unused local variable.
llvm-svn: 341823
2018-09-10 13:20:16 +00:00
Hans Wennborg 8ec9b16f27 ReleaseNotes: update links to use https
llvm-svn: 341788
2018-09-10 08:52:31 +00:00
Tom Stellard a6749135df MachO: Change getString16() back to inline function
This was accidentally changed in r341670.

llvm-svn: 341672
2018-09-07 15:51:52 +00:00
Tom Stellard 493f3bad3c MachO: Fix out-of-bounds memory access in getString16
Summary:
This fixes the following tests when gcc is compiled with gcc8:

lld :: mach-o/do-not-emit-unwind-fde-arm64.yaml
lld :: mach-o/eh-frame-relocs-arm64.yaml

llvm.org/PR38096

Reviewers: lhames, kledzik, javed.absar

Subscribers: kristof.beyls, llvm-commits

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

llvm-svn: 341670
2018-09-07 15:42:01 +00:00
Fangrui Song 1fe3e8b26f [ELF] Check if LinkSec is nullptr when initializing SHF_LINK_ORDER sections
Summary: This protects lld from a null pointer dereference when a faulty input file has such invalid sh_link fields.

Reviewers: ruiu, espindola

Reviewed By: ruiu

Subscribers: emaste, arichardson, llvm-commits

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

llvm-svn: 341611
2018-09-07 00:18:07 +00:00
Bob Haarman 2ba4d231d1 [COFF] don't mark lazy symbols as used in regular objects
Summary:
r338767 updated the COFF and wasm linker SymbolTable code to be
strutured more like the ELF linker's. That inadvertedly changed the
behavior of the COFF linker so that lazy symbols would be marked as
used in regular objects. This change adds an overload of the insert()
function, similar to the ELF linker, which does not perform that
marking.

Reviewers: ruiu, rnk, hans

Subscribers: aheejin, sunfish, llvm-commits

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

llvm-svn: 341585
2018-09-06 20:23:56 +00:00
Nico Weber 13b55bbc2f lld-link: Write an empty "repro" debug directory entry if /Brepro is passed
If the coff timestamp is set to a hash, like lld-link does if /Brepro is
passed, the coff spec suggests that a IMAGE_DEBUG_TYPE_REPRO entry is in the
debug directory. This lets lld-link write such a section.
Fixes PR38429, see bug for details.

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

llvm-svn: 341486
2018-09-05 18:02:43 +00:00
Sterling Augustine b55236f522 When a relocation to an undefined symbol is an R_X86_64_PC32, an input
section will not have an input file. Don't crash under those circumstances.

Neither clang nor llvm-mc generates R_X86_64_PC32 relocations due to
https://reviews.llvm.org/D43383, which makes it hard to write a test case.
However, gcc does generate such relocations. I want to get a fix in now,
but will figure out a way to actually exercise this code path as soon
as I can.

llvm-svn: 341408
2018-09-04 21:06:59 +00:00
Martin Storsjo a47957ab13 [COFF] Allow exporting all symbols from system libraries specfied with -wholearchive:
When building a shared libc++.dll, it pulls in libc++abi.a statically
with the --wholearchive flag. If such a build is done with
--export-all-symbols, it's reasonable to assume that everything
from that library also should be exported with the same rules as normal
local object files, even though we normally avoid autoexporting things
from libc++abi.a in other cases when linking a DLL (user code).

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

llvm-svn: 341403
2018-09-04 20:56:56 +00:00
Alexandre Ganea 6a7efef4af [DebugInfo] Common behavior for error types
Following D50807, and heading towards D50664, this intermediary change does the following:

1. Upgrade all custom Error types in llvm/trunk/lib/DebugInfo/ to use the new StringError behavior (D50807).
2. Implement std::is_error_code_enum and make_error_code() for DebugInfo error enumerations.
3. Rename GenericError -> PDBError (the file will be renamed in a subsequent commit)
4. Update custom error messages to follow the same formatting: (\w\s*)+\.
5. Keep generic "file not found" (ENOENT) errors as they are in PDB code. Previously, there used to be a custom enumeration for that purpose.
6. Remove a few extraneous LF in log() implementations. Printing LF is a responsability at a higher level, not at the error level.

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

llvm-svn: 341228
2018-08-31 17:41:58 +00:00
Ben Dunbobbin d2c2c57f52 [LLD] Add test missed from r341206. NFC.
llvm-svn: 341207
2018-08-31 12:09:21 +00:00
Ben Dunbobbin df6f0ad210 [LLD] Check too large offsets into merge sections earlier
This patch moves the checking for too large offsets into merge sections
earlier.

Without this change the large offset generated in the added test-case
will cause an assert (as it happens to be a value reserved as a
"tombstone" in the DenseMap implementation) when OffsetMap is queried in
getSectionPiece().

To simplify the code and avoid future mistakes I have refactored so that
there is only one function that looks up offsets in the OffsetMap.

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

llvm-svn: 341206
2018-08-31 11:51:51 +00:00
Martin Storsjo 24f590fca6 [ELF] [ARM] Don't mix 'ip' and 'r12' as names for the same register in comments. NFC.
llvm-svn: 341179
2018-08-31 08:03:33 +00:00
Martin Storsjo 802fcb4167 [COFF] When doing automatic dll imports, replace whole .refptr.<var> chunks with __imp_<var>
After fixing up the runtime pseudo relocation, the .refptr.<var>
will be a plain pointer with the same value as the IAT entry itself.
To save a little binary size and reduce the number of runtime pseudo
relocations, redirect references to the IAT entry (via the __imp_<var>
symbol) itself and discard the .refptr.<var> chunk (as long as the
same section chunk doesn't contain anything else than the single
pointer).

As there are now cases for both setting the Live variable to true
and false externally, remove the accessors and setters and just make
the variable public instead.

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

llvm-svn: 341175
2018-08-31 07:45:20 +00:00
Fangrui Song f1126325e8 [PPC64] Improve a test ppc64_entry_point.s
Rename to ppc64-entry-point.s
Deduplicate .text dump of little-endian big-endian

llvm-svn: 341153
2018-08-31 00:33:16 +00:00
Fangrui Song 0f0f8aa2e1 Import lit.llvm after rLLD341134
llvm-svn: 341150
2018-08-31 00:23:09 +00:00
Fangrui Song d958df8324 [PPC64] Improve a test ppc64-toc-rel.s
Deduplicate dump of .text .data for little-endian and big-endian
Fix a RELOCS-NE check line typo

llvm-svn: 341148
2018-08-31 00:16:22 +00:00
Fangrui Song c799893787 [PPC64] Remove unused -z notext from a test
llvm-svn: 341139
2018-08-30 23:01:00 +00:00
Nico Weber abb8febb72 Remove LIT_SITE_CFG_IN_FOOTER, lld
It's always replaced with the same (short) static string, so just put that
there directly.

No intended behavior change.
https://reviews.llvm.org/D51357

llvm-svn: 341134
2018-08-30 22:12:16 +00:00
Martin Storsjo fcd552999f [COFF] Skip exporting artificial symbols when exporting all symbols
Differential Revision: https://reviews.llvm.org/D51457

llvm-svn: 341017
2018-08-30 05:44:41 +00:00
Martin Storsjo e5120a3bd4 [test] Adjust a test to use CHECK-NEXT instead of CHECK-NOT. NFC.
Since the order and placement of the non-wanted elements might not
be obvious, it feels more straightforward to hardcode the whole list
with -NEXT elements (and checking for the end of the output with
CHECK-EMPTY) instead of adding CHECK-NOT lines at the right places
where the unwanted elements would appear if they erroneously
were to included.

llvm-svn: 341016
2018-08-30 05:44:36 +00:00
Peter Collingbourne 6556e6b929 ELF: Don't examine values of linker script symbols during ICF.
These symbols are declared early with the same value, so they otherwise
appear identical to ICF.

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

llvm-svn: 340998
2018-08-29 23:43:38 +00:00
Heejin Ahn 4821ebf73f [WebAssembly] clang-format (NFC)
Summary: This patch runs clang-format on all wasm-only files.

Reviewers: sbc100

Subscribers: dschuff, jgravelle-google, sunfish, llvm-commits

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

llvm-svn: 340970
2018-08-29 21:03:16 +00:00
Martin Storsjo bb0a3c911d [CMake] Add an lld-test-depends target
This builds all dependencies of lld-test/check-lld, without running
the tests. This matches llvm-test-depends and clang-test-depends.

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

llvm-svn: 340943
2018-08-29 17:30:45 +00:00
Martin Storsjo cfbbb707f5 [COFF] Merge the .ctors, .dtors and .CRT sections into .rdata for MinGW
There's no point in keeping them as separate sections.

This differs from GNU ld, which places .ctors and .dtors content in
.text (implemented by a built-in linker script). But since the content
only is pointers, there's no need to have it executable.

GNU ld also leaves .CRT separate as its own standalone section.

MSVC merges .CRT into .rdata similarly, with a directive embedded in
an object file in msvcrt.lib or libcmt.lib.

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

llvm-svn: 340940
2018-08-29 17:24:10 +00:00
Rui Ueyama 025bb56a86 Always add a .note.GNU-stack section if -r.
With this patch, lld creates a .note.GNU_stack and adds that to an
output file if it is creating a re-linkable object file (i.e. if -r
is given). If we don't do this, and if you use GNU linkers as a final
linker, they create an executable whose stack area is executable,
which is considered pretty bad these days.

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

llvm-svn: 340902
2018-08-29 07:27:09 +00:00
Fangrui Song f9b789eaa4 [ELF] Change llvm-objdump -D to -d for check lines that only inspect text sections
Reviewers: ruiu, sfertile, syzaara, espindola

Subscribers: emaste, nemanjai, arichardson, kbarton, llvm-commits

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

llvm-svn: 340890
2018-08-29 02:07:58 +00:00
Sean Fertile 4d354e1199 [PPC64] Fix DQ-form instruction handling and emit error for misalignment.
Relanding r340564, original commit message:

Fixes the handling of *_DS relocations used on DQ-form instructions where we
were overwriting some of the extended opcode bits. Also adds an alignment check
so that the user will receive a diagnostic error if the value we are writing
is not properly aligned.

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

llvm-svn: 340832
2018-08-28 15:16:01 +00:00
George Rimar f0eedbce44 [LLD][ELF] - Simplify Call-Chain Clustering implementation a bit.
Looking at the current implementation and algorithm description,
it does not seem we need to keep vector with all edges for
each cluster and can just remember the best one. This is NFC change.

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

llvm-svn: 340806
2018-08-28 08:49:40 +00:00
George Rimar a46d08ebe6 [LLD][ELD] - Do not reject INFO output section type when used with a start address.
This is https://bugs.llvm.org/show_bug.cgi?id=38625

LLD accept this: 

".stack (INFO) : {", 

but not this:

".stack address_expression (INFO) :"

The patch fixes it.

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

llvm-svn: 340804
2018-08-28 08:39:21 +00:00
George Rimar 27bbe7d0b4 [LLF][ELF] - Support -z global.
-z global is a flag used on Android (see D49198).

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

llvm-svn: 340802
2018-08-28 08:24:34 +00:00
Nico Weber c7bad5767b fix comment typo
llvm-svn: 340742
2018-08-27 14:22:25 +00:00
Sid Manning 569a56d6a0 [ELF][HEXAGON] Add R_HEX_11/10/9_X support
Differential Revision: https://reviews.llvm.org/D51225

llvm-svn: 340739
2018-08-27 12:55:28 +00:00
Matt Arsenault 8d6c7a63a1 Handle identifying AMDGPU bitcode files
llvm-svn: 340738
2018-08-27 12:40:00 +00:00
Martin Storsjo eac1b05f1d [COFF] Support MinGW automatic dllimport of data
Normally, in order to reference exported data symbols from a different
DLL, the declarations need to have the dllimport attribute, in order to
use the __imp_<var> symbol (which contains an address to the actual
variable) instead of the variable itself directly. This isn't an issue
in the same way for functions, since any reference to the function without
the dllimport attribute will end up as a reference to a thunk which loads
the actual target function from the import address table (IAT).

GNU ld, in MinGW environments, supports automatically importing data
symbols from DLLs, even if the references didn't have the appropriate
dllimport attribute. Since the PE/COFF format doesn't support the kind
of relocations that this would require, the MinGW's CRT startup code
has an custom framework of their own for manually fixing the missing
relocations once module is loaded and the target addresses in the IAT
are known.

For this to work, the linker (originall in GNU ld) creates a list of
remaining references needing fixup, which the runtime processes on
startup before handing over control to user code.

While this feature is rather controversial, it's one of the main features
allowing unix style libraries to be used on windows without any extra
porting effort.

Some sort of automatic fixing of data imports is also necessary for the
itanium C++ ABI on windows (as clang implements it right now) for importing
vtable pointers in certain cases, see D43184 for some discussion on that.

The runtime pseudo relocation handler supports 8/16/32/64 bit addresses,
either PC relative references (like IMAGE_REL_*_REL32*) or absolute
references (IMAGE_REL_AMD64_ADDR32, IMAGE_REL_AMD64_ADDR32,
IMAGE_REL_I386_DIR32). On linking, the relocation is handled as a
relocation against the corresponding IAT slot. For the absolute references,
a normal base relocation is created, to update the embedded address
in case the image is loaded at a different address.

The list of runtime pseudo relocations contains the RVA of the
imported symbol (the IAT slot), the RVA of the location the relocation
should be applied to, and a size of the memory location. When the
relocations are fixed at runtime, the difference between the actual
IAT slot value and the IAT slot address is added to the reference,
doing the right thing for both absolute and relative references.

With this patch alone, things work fine for i386 binaries, and mostly
for x86_64 binaries, with feature parity with GNU ld. Despite this,
there are a few gotchas:
- References to data from within code works fine on both x86 architectures,
  since their relocations consist of plain 32 or 64 bit absolute/relative
  references. On ARM and AArch64, references to data doesn't consist of
  a plain 32 or 64 bit embedded address or offset in the code. On ARMNT,
  it's usually a MOVW+MOVT instruction pair represented by a
  IMAGE_REL_ARM_MOV32T relocation, each instruction containing 16 bit of
  the target address), on AArch64, it's usually an ADRP+ADD/LDR/STR
  instruction pair with an even more complex encoding, storing a PC
  relative address (with a range of +/- 4 GB). This could theoretically
  be remedied by extending the runtime pseudo relocation handler with new
  relocation types, to support these instruction encodings. This isn't an
  issue for GCC/GNU ld since they don't support windows on ARMNT/AArch64.
- For x86_64, if references in code are encoded as 32 bit PC relative
  offsets, the runtime relocation will fail if the target turns out to be
  out of range for a 32 bit offset.
- Fixing up the relocations at runtime requires making sections writable
  if necessary, with the VirtualProtect function. In Windows Store/UWP apps,
  this function is forbidden.

These limitations are addressed by a few later patches in lld and
llvm.

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

llvm-svn: 340726
2018-08-27 08:43:31 +00:00
Rui Ueyama 41831204c7 Rename a function to follow the LLVM coding style.
llvm-svn: 340716
2018-08-27 06:18:10 +00:00
Martin Storsjo c4b0061c05 [COFF] Check the instructions in ARM MOV32T relocations
For this relocation, which applies to two consecutive instructions,
it's plausible that the second instruction might not actually be
the right one.

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

llvm-svn: 340715
2018-08-27 06:04:36 +00:00
Sid Manning 2c3d4b37b6 [ELF][HEXAGON] Add R_HEX_B13_PCREL relocation support
Differential Revision: https://reviews.llvm.org/D51166

llvm-svn: 340693
2018-08-25 23:16:37 +00:00
Sid Manning 6d0415e098 [ELF][HEXAGON] Add R_HEX_B9_PCREL and R_HEX_B9_PCREL_X relocation support
Differential Revision: https://reviews.llvm.org/D51045

llvm-svn: 340692
2018-08-25 21:25:35 +00:00
Martin Storsjo 0f3d8e2360 [Common] Discard the temp file while keeping the memory mapping open, on errors
Differential Revision: https://reviews.llvm.org/D51095

llvm-svn: 340635
2018-08-24 18:36:42 +00:00
Joel Galenson 0f6e12bcdf Modify tests to show PLT entry labels
llvm-svn: 340616
2018-08-24 16:22:42 +00:00
Nico Weber 0b87d05679 Attempt to fix ELF/lto-plugin-ignore.s on Windows after r340487.
I removed the ".exe" regex in the wrong place. lld-link in front of the
"error:" no longer has the suffix; the --plugin-opt diagnostic still has it.

llvm-svn: 340590
2018-08-24 00:12:58 +00:00
Sean Fertile 1542b0aef5 Revert "[PPC64] Fix DQ-form instruction handling and emit error for misalign..."
This reverts commit 5125b44dbb5d06b715213e4bec75c7346bfcc7d3.
ppc64-dq.s and ppc64-error-missaligned-dq.s fail on several of the build-bots.
Reverting to investigate.

llvm-svn: 340568
2018-08-23 19:10:40 +00:00
Sean Fertile a6b2ab7a91 [PPC64] Fix DQ-form instruction handling and emit error for misalignment.
Fixes the handling of *_DS relocations used on DQ-form instructions where we
were overwriting some of the extended opcode bits. Also adds an alignment check
so that the user will receive a diagnostic error if the value we are writing
is not properly aligned.

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

llvm-svn: 340564
2018-08-23 18:35:09 +00:00
Peter Collingbourne ab038025a5 COFF: Implement safe ICF on rodata using address-significance tables.
Differential Revision: https://reviews.llvm.org/D51050

llvm-svn: 340555
2018-08-23 17:44:42 +00:00
Nico Weber 386bf1216e win: Omit ".exe" from lld warning and error messages.
This is a minor follow-up to https://reviews.llvm.org/D49189. On Windows, lld
used to print "lld-link.exe: error: ...". Now it just prints "lld-link: error:
...". This matches what link.exe does (it prints "LINK : ...") and makes lld's
output less dependent on the host system.

https://reviews.llvm.org/D51133

llvm-svn: 340487
2018-08-22 23:52:13 +00:00
Nico Weber 7830c6f66f lld-link: Separate 'undefined symbol' errors with just one newline, not two.
newline() in ErrorHandler.cpp already tries to insert newlines between messages
that contain embedded newlines, so getSymbolLocations() shouldn't return a
string that ends in a newline -- else we end up with two newlines between error
messages.

Makes lld-link's output look more like ld.lld output.

https://reviews.llvm.org/D51117

llvm-svn: 340482
2018-08-22 23:45:05 +00:00
Nico Weber 613edd1582 Fix two RUN: lines that were unintentionally spelled "RN:".
https://reviews.llvm.org/D51140

llvm-svn: 340481
2018-08-22 23:44:03 +00:00
Sam Clegg 47078f56f2 [WebAssembly] Optimise relocation processing. NFC.
This is a rebased version https://reviews.llvm.org/D42176 which is patch
by Nicolas Wilson.

Addresses issue:
https://github.com/WebAssembly/tool-conventions/issues/32, and
https://bugs.llvm.org/show_bug.cgi?id=38650

Previously, for each function/segment we iterated over every relocation
to find the relevant ones, which is an n^2 operation. Now, we just make
a single pass.

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

llvm-svn: 340428
2018-08-22 17:50:51 +00:00
Nico Weber ebc27c4873 lld-link: Emit warning if one each of {main,wmain} and {WinMain,wWinMain} exist and no /subsystem: flag is passed.
Similar to link.exe's LNK4031.
https://reviews.llvm.org/D51076

llvm-svn: 340420
2018-08-22 16:47:16 +00:00
Sid Manning 564e65a4c0 [ELF][HEXAGON] Add R_HEX_16_X relocation
This relocation has only 6-bits the remaining are in the extender.

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

llvm-svn: 340413
2018-08-22 15:25:15 +00:00
Martin Storsjo c0ee24033c [COFF] Move a comment close to the code it refers to. NFC.
llvm-svn: 340400
2018-08-22 11:35:02 +00:00
Martin Storsjo 6c67e04345 [COFF] Change fatal() into error() when writing chunks to the output
In most of these cases, it's easy to go on despite the error,
printing as many valuable error messages as possible from one run
as possible.

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

llvm-svn: 340399
2018-08-22 11:34:58 +00:00
Rui Ueyama 07b4536bb7 Change how we handle -wrap.
We have an issue with -wrap that the option doesn't work well when
renamed symbols get PLT entries. I'll explain what is the issue and
how this patch solves it.

For one -wrap option, we have three symbols: foo, wrap_foo and real_foo.
Currently, we use memcpy to overwrite wrapped symbols so that they get
the same contents. This works in most cases but doesn't when the relocation
processor sets some flags in the symbol. memcpy'ed symbols are just
aliases, so they always have to have the same contents, but the
relocation processor breaks that assumption.

r336609 is an attempt to fix the issue by memcpy'ing again after
processing relocations, so that symbols that are out of sync get the
same contents again. That works in most cases as well, but it breaks
ASan build in a mysterious way.

We could probably fix the issue by choosing symbol attributes that need
to be copied after they are updated. But it feels too complicated to me.

So, in this patch, I fixed it once and for all. With this patch, we no
longer memcpy symbols. All references to renamed symbols point to new
symbols after wrapSymbols() is done.

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

llvm-svn: 340387
2018-08-22 07:02:26 +00:00
Bob Haarman 481d224b67 [Support][CachePruning] prune least recently accessed files first
Summary:
Before this change, pruning order was based on size. This changes it
to be based on time of last use instead, preferring to keep recently
used files and prune older ones.

Reviewers: pcc, rnk, espindola

Reviewed By: rnk

Subscribers: emaste, arichardson, hiraditya, steven_wu, dexonsmith, llvm-commits

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

llvm-svn: 340374
2018-08-22 00:52:16 +00:00
Fangrui Song 887ec75173 [ELF] -thinlto-object-suffix-replace=: don't error if the path does not end with old suffix
Summary:
For -thinlto-object-suffix-replace=old\;new, in
tools/gold/gold-plugin.cpp, the thinlto object filename is Path minus
optional old suffix.

    static std::string getThinLTOObjectFileName(StringRef Path, StringRef OldSuffix,
                                                StringRef NewSuffix) {
      if (OldSuffix.empty() && NewSuffix.empty())
        return Path;
      StringRef NewPath = Path;
      NewPath.consume_back(OldSuffix);
      std::string NewNewPath = NewPath;
      NewNewPath += NewSuffix;
      return NewNewPath;
    }

Currently lld will error that the path does not end with old suffix.

This patch makes lld accept such paths but only add new suffix if Path
ends with old suffix. This fixes a link error where bitcode members in
an archive are regular LTO objects without old suffix.

Acording to tejohnson, this will "enable supporting mix and match of
minimized ThinLTO bitcode files with normal ThinLTO bitcode files in a
single link (where we want to apply the suffix replacement to the
minimized files, and just ignore it for the normal ThinLTO files)."

Reviewers: ruiu, pcc, tejohnson, espindola

Reviewed By: tejohnson

Subscribers: emaste, inglorion, arichardson, eraman, steven_wu, dexonsmith, llvm-commits

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

llvm-svn: 340364
2018-08-21 23:28:12 +00:00
Zaara Syeda 662d146c5b [PPC64] Add TLS initial exec to local exec relaxation
This patch adds the target call back relaxTlsIeToLe to support TLS relaxation
from initial exec to local exec model.

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

llvm-svn: 340281
2018-08-21 15:13:53 +00:00
George Rimar 3cd1d27b58 [LLD][ELF] - Fix BB after r340257.
Renamed Filecheck->FileCheck.

llvm-svn: 340259
2018-08-21 08:27:23 +00:00
George Rimar e2684662ee [LLD][ELF] - Check the architecture of lazy objects earlier.
Our code in LazyObjFile::parse() has an ELFT switch and
adds a lazy object by its ELFT kind.
Though it might be possible to add a file using a different
architecture and make LLD to silently accept it (if the file
is empty or contains only week symbols). That itself, not a
huge issue perhaps (because the error would be reported later
if the file is fetched), but still does not look clean and correct.

It is possible to report an error earlier and clean up the
code. That is what the patch does.

Ideally, we might want to reuse isCompatible from SymbolTable.cpp,
but it is static and accepts a file as an argument, what is not
convenient. Since such a situation should be rare, I think it
should be OK to go with the way chosen in this patch.

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

llvm-svn: 340257
2018-08-21 08:13:06 +00:00
Fangrui Song 3a84a093f2 [docs] Add --print-icf-sections to ld.lld.1
llvm-svn: 340253
2018-08-21 07:35:23 +00:00
George Rimar 20f994d350 [LLD][ELF] - Fix warning.
This fixes the following warning when compiling with gcc version 8.0.1 20180319 (experimental) (GCC):

/home/umb/LLVM/llvm/tools/lld/ELF/SyntheticSections.cpp:1951:46: warning: enumeral and non-enumeral type in conditional expression [-Wextra]
     return OS->SectionIndex >= SHN_LORESERVE ? SHN_XINDEX : OS->SectionIndex;

llvm-svn: 340164
2018-08-20 10:29:21 +00:00
Peter Smith a8656c62f5 [ELF] Add support for Armv5 and Armv6 compatible Thunks
Older Arm architectures do not support the MOVT and MOVW instructions so we
must use an alternative sequence of instructions to transfer control to the
destination.

Assuming at least Armv5 this patch adds support for Thunks that load or add
to the program counter. Note that there are no Armv5 Thumb Thunks as there
is no Thumb branch instruction in Armv5 that supports Thunks. These thunks
will not work for Armv4t (arm7tdmi) as this architecture cannot change state
from using the LDR or ADD instruction.

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

llvm-svn: 340160
2018-08-20 09:37:50 +00:00
Peter Smith d1be026ede [ELF] Add support for older Arm Architectures with smaller branch range
The Thumb BL and BLX instructions on older Arm Architectures such as v5 and
v6 have a constrained encoding J1 and J2 must equal 1, later Architectures
relaxed this restriction allowing J1 and J2 to be used to calculate a larger
immediate.

This patch adds support for the old encoding, it is used when the build
attributes for the input objects only contain older architectures.

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

llvm-svn: 340159
2018-08-20 09:19:30 +00:00
Rui Ueyama 4c7d6beb37 Remove unnecessary applyMask() application.
applyMask(0xffffffff, x) is an identity function.

llvm-svn: 340152
2018-08-20 06:33:29 +00:00
Sam Clegg 30161dc28d [WebAssembly] Don't compress LEBs by default
LEB compression breaks debug info so we don't want to enable
it by default, even at high optimization levels.

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

llvm-svn: 340073
2018-08-17 19:42:46 +00:00
George Rimar bf6132c139 [LLD][ELF] - Remove dead code. NFC.
These lines were unused.

llvm-svn: 340011
2018-08-17 11:19:55 +00:00
Reid Kleckner e8299ded5b Update LLD tests for CodeView dumper change in r339907
llvm-svn: 339913
2018-08-16 18:03:06 +00:00
Reid Kleckner bd5d71229d [codeview] Use push_macro to avoid conflicts instead of a prefix
Summary:
This prefix was added in r333421, and it changed our dumper output to
say things like "CVRegEAX" instead of just "EAX". That's a functional
change that I'd rather avoid.

I tested GCC, Clang, and MSVC, and all of them support #pragma
push_macro. They don't issue warnings whem the macro is not defined
either.

I don't have a Mac so I can't test the real termios.h header, but I
looked at the termios.h sources online and looked for other conflicts.
I saw only the CR* macros, so those are the ones we work around.

Reviewers: zturner, JDevlieghere

Subscribers: hiraditya, llvm-commits

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

llvm-svn: 339907
2018-08-16 17:34:31 +00:00
Fangrui Song ebf9607d7d [ELF] mergeSections: remove non-alive MergeInputSection
Summary: This makes it conform to what the comment says. Otherwise when getErrPlace() is called afterwards, cast<InputSection>(D) will cause incompatible cast as MergeInputSection is not a subclass of InputSection.

Reviewers: ruiu, grimar, espindola, pcc

Reviewed By: grimar

Subscribers: emaste, arichardson, llvm-commits

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

llvm-svn: 339904
2018-08-16 17:22:02 +00:00
George Rimar e32c31af31 [LLD][ELF] - Add a test case for DT_SONAME entry reading.
This covers a following line with a test:
https://github.com/llvm-mirror/lld/blob/master/ELF/InputFiles.cpp#L899

llvm-svn: 339880
2018-08-16 14:07:29 +00:00
George Rimar ff22b43c7d [LLD][ELF] - Add a test case.
This covers the following line with a test:
https://github.com/llvm-mirror/lld/blob/master/ELF/InputFiles.cpp#L487

llvm-svn: 339876
2018-08-16 13:02:50 +00:00
George Rimar fc1a547be7 [LLD][ELF] - Add test case.
To cover the following error message:
https://github.com/llvm-mirror/lld/blob/master/ELF/InputFiles.cpp#L463

llvm-svn: 339867
2018-08-16 11:48:18 +00:00
George Rimar 21149aa2fa [LLD][ELF] - Stop using binary input in merge-invalid-size.s test. NFCI.
This rewrites the test using yaml.

llvm-svn: 339775
2018-08-15 14:59:38 +00:00
George Rimar eecc92d7d3 [LLD][ELF] - Convert binary input to yaml in sht-group.s test. NFC.
This removes test that used binary input and adds a yaml based test instead.

llvm-svn: 339774
2018-08-15 14:10:15 +00:00
George Rimar 2835606889 [LLD][ELF] - Handle SHT_GROUP more carefully. NFCI.
This patch solves 2 problems:
1) It adds a test to check the line below:
https://github.com/llvm-mirror/lld/blob/master/ELF/InputFiles.cpp#L334
Test case contains SHT_GROUP section with a broken (0xFF) flag.

2) The patch fixes the case when we silently accepted such broken groups
in the case when there were no other objects with the same group signature.

llvm-svn: 339765
2018-08-15 12:20:38 +00:00
George Rimar 48ca274d2f [LLD][ELF] - Eliminate dead code from OffsetGetter::get().
We have a dead piece of code there which is impossible to trigger
using regular objects I believe.
Patch removes it and adds a test case showing how this condition
can be triggered with use of a broken object and crash the linker.

llvm-svn: 339680
2018-08-14 14:20:34 +00:00
Sid Manning 5b0141c49e [ELF][HEXAGON] Add R_HEX_32 support
Differential Revision: https://reviews.llvm.org/D50657

llvm-svn: 339677
2018-08-14 13:52:38 +00:00
George Rimar 152e3c98ac [LLD][ELF] - Remove UnresolvedPolicy::IgnoreAll and relative code. NFC.
The code involved was simply dead. `IgnoreAll` value is used in
`maybeReportUndefined` only which is never called for -r.
And at the same time `IgnoreAll` was set only for -r.

llvm-svn: 339672
2018-08-14 11:55:31 +00:00
Richard Smith 5a778aa79f Revert r339490 to match revert of llvm r339474 in r339630.
llvm-svn: 339635
2018-08-14 01:23:54 +00:00
Sterling Augustine 48b469746c Support shared objects for split stack.
llvm-svn: 339626
2018-08-13 22:29:15 +00:00
George Rimar 4b3a8bd91e [LLD][ELF] - Remove dead code from handleTlsRelocation. NFC.
Code is dead because R_TLSDESC_CALL is already handled in the
following block of the code:
https://github.com/llvm-mirror/lld/blob/master/ELF/Relocations.cpp#L231

llvm-svn: 339566
2018-08-13 13:59:06 +00:00
George Rimar b44789759b [LLD][ELF] - Remove dead code from handleTlsRelocation. NFC.
That piece of code is really very old and "protected"
from TLS relocations against symbol in non-allocatable sections.
It is useless because normally non-alloc sections have relocations
with allocatable targets, but not the reverse.
And so the code was simply dead.

llvm-svn: 339553
2018-08-13 11:20:18 +00:00
George Rimar c1ef96f593 [LLD][ELF] - Update test case to check the error message reported. NFC.
This updates znotext-plt-relocations-protected.s test case
(adds a file symbol) to check how the error message is built.

It allows to cover/test the following line:
https://github.com/llvm-mirror/lld/blob/master/ELF/Relocations.cpp#L79

llvm-svn: 339550
2018-08-13 10:28:00 +00:00
George Rimar 25cd609a64 [LLD][ELF] - Add one more test case for CallGraphSort.
This adds a coverage for the following early continue:
https://github.com/llvm-mirror/lld/blob/master/ELF/CallGraphSort.cpp#L200

llvm-svn: 339514
2018-08-12 09:16:22 +00:00
George Rimar a5cf8da145 [LLF][ELF] - Simplify. NFC.
llvm-svn: 339510
2018-08-12 07:52:24 +00:00
Richard Trieu f1342bb8b8 Fix WebAssembly tests after r339474
Add flags to llc RUN lines to keep tests passing.

llvm-svn: 339490
2018-08-10 23:58:11 +00:00
Sid Manning 2e20df3e9b [ELF][HEXAGON] Put test back in.
The registers are tied.

llvm-svn: 339487
2018-08-10 23:46:11 +00:00
Sid Manning 98b07a615b [ELF][HEXAGON] Remove test that breaks assembler.
Remove testcase the breaks the assembler parser.  Will fix llvm-mc
and put this back afterward.

llvm-svn: 339486
2018-08-10 22:54:54 +00:00
Sid Manning 07e541a892 [ELF][HEXAGON] Add R_HEX_8_X relocation
Differential Revision: https://reviews.llvm.org/D50577

llvm-svn: 339477
2018-08-10 21:48:40 +00:00
George Rimar 88863a5f62 [ELF] - Get rid of SyntheticSection::postThunkContents(). NFCI.
It turns out that postThunkContents() is only used for
sorting symbols in .symtab.

Though we can instead move the logic to SymbolTableBaseSection::finalizeContents(),
postpone calling it and then get rid of postThunkContents completely.

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

llvm-svn: 339413
2018-08-10 07:24:18 +00:00
George Rimar 00d6f4bd6b [LLD][ELF] - Fix crash when using empty --defsym.
We have a crash issue when handling the empty -defsym.

For parsing this option we are using ScriptParser class which is used
generally for reading the linker script. For empty defsym case, we
pass the empty memory buffer and crash in the place removed in https://reviews.llvm.org/rL336436.

But reverting of the above patch would not help here (we would still crash but a bit later). And
even after fixing the crash we would report something like
"lld.exe: error: -defsym:1: unexpected EOF"
It is probably not the appropriate message because mentions EOF.

I think the issue should be handled on a higher level like this patch does.
So we do not want to pass the empty memory buffer first of all I believe.

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

llvm-svn: 339412
2018-08-10 06:32:39 +00:00
Sid Manning c53078a1e3 [ELF][HEXAGON] Add R_HEX_HI16/R_HEX_LO16 relocations
Differential Revision: https://reviews.llvm.org/D50520

llvm-svn: 339371
2018-08-09 20:04:53 +00:00
Rui Ueyama 5cd9c6bcd8 Support RISC-V
Patch by PkmX.

This patch makes lld recognize RISC-V target and implements basic
relocation for RV32/RV64 (and RVC). This should be necessary for static
linking ELF applications.

The ABI documentation for RISC-V can be found at:
https://github.com/riscv/riscv-elf-psabi-doc/blob/master/riscv-elf.md.
Note that the documentation is far from complete so we had to figure out
some details from bfd.

The patch should be pretty straightforward. Some highlights:

 - A new relocation Expr R_RISCV_PC_INDIRECT is added. This is needed as
   the low part of a PC-relative relocation is linked to the corresponding
   high part (auipc), see:
   https://github.com/riscv/riscv-elf-psabi-doc/blob/master/riscv-elf.md#pc-relative-symbol-addresses

 - LLVM's MC support for RISC-V is very incomplete (we are working on
   this), so tests are given in objectyaml format with the original
   assembly included in the comments. Once we have complete support for
   RISC-V in MC, we can switch to llvm-as/llvm-objdump.

 - We don't support linker relaxation for now as it requires greater
   changes to lld that is beyond the scope of this patch. Once this is
   accepted we can start to work on adding relaxation to lld.

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

llvm-svn: 339364
2018-08-09 17:59:56 +00:00
George Rimar dffc1911e2 [LLD][ELF] - Add a test for elf::getPriority(StringRef S)
It covers the following line with a test:
https://github.com/llvm-mirror/lld/blob/master/ELF/OutputSections.cpp#L383

llvm-svn: 339348
2018-08-09 15:05:25 +00:00
Hans Wennborg bdd8493f2b [COFF] Make the relocation scanning for CFG more discriminating
link.exe ignores REL32 relocations on 32-bit x86, as well as relocations
against non-function symbols such as labels. This makes lld do the same.

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

llvm-svn: 339345
2018-08-09 13:43:22 +00:00
George Rimar cc8f8194ba [LLD][ELF] - Add a test for ScriptParser::readPhdrType().
This adds a test for https://github.com/llvm-mirror/lld/blob/master/ELF/ScriptParser.cpp#L1289.

llvm-svn: 339342
2018-08-09 13:19:10 +00:00
George Rimar 5570d7d45b [LLD][ELF] - Add a test for ScriptParser::readOutputSectionDescription.
This covers the following line:
https://github.com/llvm-mirror/lld/blob/master/ELF/ScriptParser.cpp#L415

llvm-svn: 339333
2018-08-09 12:14:00 +00:00
Sid Manning 17858207a0 Add R_HEX_6_X relocation support
This is a larger patch.  This relocation has irregular immediate
masks that require a lookup to find the correct mask.

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

llvm-svn: 339332
2018-08-09 12:00:10 +00:00
George Rimar b89367ac46 [LLD][ELF] - Add a test for ScriptParser::readPhdrs(). NFCI.
This covers the following line with a test:
https://github.com/llvm-mirror/lld/blob/master/ELF/ScriptParser.cpp#L415

llvm-svn: 339327
2018-08-09 11:13:30 +00:00
George Rimar fce824e157 [LLD][ELF] - Add a test case for parsing -defsym.
This adds a coverage for the following line of the code:
https://github.com/llvm-mirror/lld/blob/master/ELF/ScriptParser.cpp#L274

llvm-svn: 339326
2018-08-09 10:40:11 +00:00
Peter Collingbourne 98930115ea ELF: Only add libcall symbols to the link if defined in bitcode.
Adding all libcall symbols to the link can have undesired consequences.
For example, the libgcc implementation of __sync_val_compare_and_swap_8
on 32-bit ARM pulls in an .init_array entry that aborts the program if
the Linux kernel does not support 64-bit atomics, which would prevent
the program from running even if it does not use 64-bit atomics.

This change makes it so that we only add libcall symbols to the
link before LTO if we have to, i.e. if the symbol's definition is in
bitcode. Any other required libcall symbols will be added to the link
after LTO when we add the LTO object file to the link.

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

llvm-svn: 339301
2018-08-08 23:48:12 +00:00
Sam Clegg 57694c50bc [WebAssembly] Group rodata into a single output segment
Differential Revision: https://reviews.llvm.org/D50424

llvm-svn: 339279
2018-08-08 18:02:55 +00:00
Peter Smith 5c57957281 Add missing REQUIRES x86 to tests.
Add REQUIRES to tests that fail when an x86 backend is not present.

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

llvm-svn: 339253
2018-08-08 14:50:33 +00:00
Nico Weber f4f5b7eea3 lld-link: Take /SUBSYSTEM into account for automatic /ENTRY detection.
If /subsystem:windows is passed, link.exe only looks for WinMain and wWinMain,
and if /subsystem:console is passed it only looks for main and wmain. lld-link
used to look for all 4 in both cases. This patch makes lld-link match
link.exe's behavior.

This requires that the subsystem is known by the time findDefaultEntry() gets
called. findDefaultEntry() is called before the main link loop, so that the
loop can mark the entry point as undefined. That means inferSubsystem() has to
be called above the main loop as well. This in turn means /subsystem: from
.drectve sections only has an effect on entry point inference for obj files
passed to lld-link directly (and not in obj files found later in .lib files).
link.exe seems to ignore /subsystem: for obj files from lib files completely
(while in lld it's ignored only for entry point detection but it still
overrides /subsystem: flags passed on the command line for the value that gets
written in the output file).

Also, if the subsytem isn't needed (e.g. when only writing a /def: lib file and
not writing a coff file), link.exe doesn't complain if the subsystem isn't
known, so both subsystem and entry point handling should be below the early
return lld has for that case.

Fixes PR36523.
https://reviews.llvm.org/D50316

llvm-svn: 339165
2018-08-07 19:10:28 +00:00
George Rimar 462b74cad3 [LLD][ELF] - Add test cases for ScriptParser::readDynamicList(). NFCI.
This 2 test cases covers the following code:
https://github.com/llvm-mirror/lld/blob/master/ELF/ScriptParser.cpp#L185
https://github.com/llvm-mirror/lld/blob/master/ELF/ScriptParser.cpp#L189

llvm-svn: 339137
2018-08-07 13:13:19 +00:00
George Rimar 6bcbd1ea83 [LLD][ELF] - Added test case for non-nullterminated wide strings.
This covers the following line:
https://github.com/llvm-mirror/lld/blob/master/ELF/InputSection.cpp#L1032

llvm-svn: 339124
2018-08-07 10:29:35 +00:00
George Rimar 65eceee701 [LLD][ELF] - Add a test case for code in Archive::fetch(). NFCI.
This covers the following piece with a test.
https://github.com/llvm-mirror/lld/blob/master/ELF/InputFiles.cpp#L830

Thanks to Peter Collingbourne for providing the reproducer sample!

llvm-svn: 339114
2018-08-07 08:41:37 +00:00
Martin Storsjo d9fd4a0de6 [COFF] Fix a comment about automatic resolving of dllimports from within a module. NFC.
Differential Revision: https://reviews.llvm.org/D50357

llvm-svn: 339100
2018-08-07 06:42:53 +00:00
Fangrui Song f66d0ce10b [ELF] Use MathExtras.h llvm::SignExtend64
Summary: To be consistent with other files where only SignExtend64 is used.

Reviewers: ruiu, espindola

Subscribers: emaste, arichardson, llvm-commits

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

llvm-svn: 339083
2018-08-06 23:50:26 +00:00