Commit Graph

12127 Commits

Author SHA1 Message Date
Alexandre Ganea 9c78db6005 Re-land [LLD][COFF] Early load PDB type server files
We need to have all input files ready before doing debuginfo type merging.
This patch is moving the late PDB type server discovery much earlier in the process, when the explicit inputs (OBJs, LIBs) are loaded.
The short term goal is to parallelize type merging.

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

llvm-svn: 362393
2019-06-03 12:39:47 +00:00
George Rimar 3b20ae6c54 [LLD][ELF] - Remove dead code. NFC.
I believe this line was dead after r362356.

llvm-svn: 362367
2019-06-03 09:23:01 +00:00
Fangrui Song 8522d579b8 [ELF][PPC64] Rename some PPC64 ELFv2 specific RelExpr from R_PPC_* to R_PPC64_*
The following abstract relocation types (RelExpr) are PPC64 ELFv2 ABI specific,
not used by PPC32. So rename them to prevent confusion when the PPC32 port is improved.

* R_PPC_CALL R_PPC_CALL_PLT:
  R_PPC_CALL_PLT represents R_PPC64_REL14 and R_PPC64_REL24.
  If the function is not preemptable, R_PPC_CALL_PLT can be optimized to R_PPC_CALL:
  the formula adjusts the symbol VA from the global entry point to the local entry point.
* R_PPC_TOC: represents R_PPC64_TOC.  We don't have a test. Add one to ppc64-relocs.s
  Rename it to R_PPC64_TOCBASE because `@tocbase` is the assembly form.

Reviewed By: ruiu

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

llvm-svn: 362359
2019-06-03 06:21:33 +00:00
Fangrui Song ce1534b405 [ELF][PPC64] Don't apply LD->LE relaxation on R_PPC64_GOT_DTPREL16*
In ELF v2 ABI, R_PPC64_GOT_DTPREL16* are not relaxed.

This family of relocation types are used for variables outside of 2GiB
of the TLS block. 2 instructions cannot materialize a DTPREL offset that
is not 32-bit.

Reviewed By: ruiu

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

llvm-svn: 362357
2019-06-03 05:41:31 +00:00
Fangrui Song abb7484c31 [ELF] Don't create an output section named `/DISCARD/` if it is assigned to the special phdr `NONE`
Fixes the remaining issue of PR41673 after D61186: with `/DISCARD/ { ... } :NONE`,
we may create an output section named `/DISCARD/`.

Note, if an input section is named `/DISCARD/`, ld.bfd discards it but
lld keeps it. It is probably not worth copying this behavior as it is unrealistic.

Reviewed By: ruiu

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

llvm-svn: 362356
2019-06-03 05:34:25 +00:00
Fangrui Song c78c999a9c [ELF] Simplify the condition to create .interp
(1) {gcc,clang} -fuse-ld=bfd -pie -fPIE -nostdlib a.c => .interp created
(2) {gcc,clang} -fuse-ld=lld -pie -fPIE -nostdlib a.c => .interp not created
(3) {gcc,clang} -fuse-ld=lld -pie -fPIE -nostdlib a.c a.so => .interp created

The inconsistency of (2) is due to the condition `!Config->SharedFiles.empty()`.
To make lld behave more like ld.bfd, we could change the condition to:

    Config->HasDynSymTab && !Config->DynamicLinker.empty() && Script->needsInterpSection();

However, that would bring another inconsistency as can be observed with:

(4) {gcc,clang} -fuse-ld=bfd -no-pie -nostdlib a.c => .interp not created

So instead, use `!Config->DynamicLinker.empty() && Script->needsInterpSection()`,
which is both simple and consistent in these cases.

The inconsistency of (4) likely originated from ld.bfd and gold's choice to have a default --dynamic-linker.
Their condition to create .interp is ANDed with (not -shared).
Since lld doesn't have a default --dynamic-linker,
compiler drivers (gcc/clang) don't pass --dynamic-linker for -shared,
and direct ld users are not supposed to specify --dynamic-linker for -shared,
we do not need the condition !Config->Shared.

Reviewed By: ruiu

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

llvm-svn: 362355
2019-06-03 05:25:03 +00:00
Rui Ueyama 471f11805f Add --sort-common to the man page.
Differential Revision: https://reviews.llvm.org/D62799

llvm-svn: 362354
2019-06-03 05:11:44 +00:00
Fangrui Song 0a6bababa8 [ELF][MIPS] Delete dead !Sym->isDefined() check in addAbsolute()
llvm-svn: 362314
2019-06-02 02:43:38 +00:00
Fangrui Song bcc0bd7e2a [ELF][test] Reorganize some AArch64 tests
Delete aarch64-got.s because it is covered by aarch64-tls-iele.s
Merge got-aarch64.s into aarch64-fpic-got.s by adding disassembly to the latter
Create aarch64-gnu-ifunc-nonpreemptable to unify aarch64-gnu-ifunc3.s (position-dependent executable) and aarch64-gnu-ifunc-address-pie.s (PIE)
Rename aarch64-got-reloc.s to aarch64-got-weak-undef.s
Add --no-show-raw-insn to llvm-objdump -d RUN lines
Add -pie test to arch64-tls-iele.s
Delete aarch64-tls-pie.s: it is covered by arch64-tls-iele.s and aarch64-tls-le.s
Rename aarch64-copy2.s to aarch64-nopic-plt.s: "copy2" gives false impression that the test is related to copy relocation

llvm-svn: 362294
2019-06-01 11:01:26 +00:00
Sam Clegg 7d4ec5af6c [WebAssembly] Don't export __data_end and __heap_base by default.
These can still be exported via --export if needed.

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

llvm-svn: 362276
2019-05-31 22:51:59 +00:00
Fangrui Song 7477fcd93a [PPC64][test] Delete redundant labels from ppc64-relocs.s
llvm-svn: 362235
2019-05-31 15:41:19 +00:00
Fangrui Song e98baf8631 [ELF] Delete GotEntrySize and GotPltEntrySize
GotEntrySize and GotPltEntrySize were added in D22288. Later, with
the introduction of wordsize() (then Config->Wordsize), they become
redundant, because there is no target that sets GotEntrySize or
GotPltEntrySize to a number different from Config->Wordsize.

Reviewed By: grimar, ruiu

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

llvm-svn: 362220
2019-05-31 10:35:45 +00:00
Fangrui Song 3f29cfd915 [ELF] Replace a dead test in getSymVA() with assert()
Symbols relative to discarded comdat sections are Undefined instead of
Defined now (after D59649 and D61583). The `== &InputSection::Discarded`
test becomes dead. I cannot find a test related to this behavior.

Reviewed By: ruiu

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

llvm-svn: 362218
2019-05-31 10:12:22 +00:00
Fangrui Song 750d148e8f [ELF][test] Restore linkerscript/symbol-location.s to test getLinkerScriptLocation()
The test (the only test that checks getLinkerScriptLocation()) deleted
by r358652 can be restored by replacing R_X86_64_PLT32 with
R_X86_64_PC32, and changing -pie to -shared (preemptable). Then, the
symbol will not be a link-time constant and a -fPIC error will be
issued.

llvm-svn: 362207
2019-05-31 08:20:43 +00:00
Thomas Lively 86e73f51d7 [WebAssembly] Improve feature validation error messages
Summary:
Add the names of the input files responsible for each error to the
messages.

Reviewers: sbc100, azakai

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

Tags: #llvm

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

llvm-svn: 362162
2019-05-30 21:57:23 +00:00
J. Ryan Stinnett d45eaf9405 [Docs] Modernize references to macOS
Summary:
This updates all places in documentation that refer to "Mac OS X", "OS X", etc.
to instead use the modern name "macOS" when no specific version number is
mentioned.

If a specific version is mentioned, this attempts to use the OS name at the time
of that version:

* Mac OS X for 10.0 - 10.7
* OS X for 10.8 - 10.11
* macOS for 10.12 - present

Reviewers: JDevlieghere

Subscribers: mgorny, christof, arphaman, cfe-commits, lldb-commits, libcxx-commits, llvm-commits

Tags: #clang, #lldb, #libc, #llvm

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

llvm-svn: 362113
2019-05-30 16:46:22 +00:00
Fangrui Song 0317e46a63 [ELF] Delete dead SHT_NOBITS->SHT_PROGBITS code after r358981
After D60131/r358981, we no longer create SHT_NOBITS sections that may
contain ByteCommand (BYTE, SHORT, LONG, QUAD).

llvm-svn: 362108
2019-05-30 15:52:11 +00:00
Fangrui Song bdaa39ea6c [ELF] De-template addUndefined() and addWrappedSymbols(). NFC
llvm-svn: 362099
2019-05-30 14:50:10 +00:00
Fangrui Song 064ae08e86 [ELF][test] Update znotext-plt-relocations.s to emit R_X86_64_PC32
This is a leftover from r325379.

The intention of this test was to check in a non-pic link, R_X86_64_PC32
to a STT_FUNC created a PLT. However, after the llvm-mc change in
r325569, this code path is no longer exercised. Use the r325379 trick to
keep testing R_X86_64_PC32.

llvm-svn: 362095
2019-05-30 14:16:29 +00:00
Fangrui Song 0526c0cd8e [ELF] Implement Local Dynamic style TLSDESC for x86-64
For the Local Dynamic case of TLSDESC, _TLS_MODULE_BASE_ is defined as a
special TLS symbol that makes:

1) Without relaxation: it produces a dynamic TLSDESC relocation that
computes 0. Adding @dtpoff to access a TLS symbol.
2) With LD->LE relaxation: _TLS_MODULE_BASE_@tpoff = 0 (lowest address in
the TLS block). Adding @tpoff to access a TLS symbol.

For 1), this saves dynamic relocations and GOT slots as otherwise
(General Dynamic) we would create an R_X86_64_TLSDESC and reserve two
GOT slots for each symbol.

Add ElfSym::TlsModuleBase and change the signature of getTlsTpOffset()
to special case _TLS_MODULE_BASE_.

Reviewed By: ruiu

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

llvm-svn: 362078
2019-05-30 10:00:20 +00:00
Sam Clegg 56e970d45d [WebAssembly] Move direct call tracking from member to local. NFC.
This data structure is only needed temporarily while symbols are being
created.

This is a followup on rL361678.

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

llvm-svn: 361977
2019-05-29 15:41:08 +00:00
Sam Clegg d506b0a484 [WebAssembly] Fix signatures of undefined function in LTO object which are not called directly.
We recently added special handling for function that are not called
directly but failed to add testing for the LTO case.

See https://reviews.llvm.org/D62153

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

llvm-svn: 361975
2019-05-29 15:36:42 +00:00
Peter Collingbourne 87575f6501 ELF: Don't reuse a thunk in a different loadable partition.
There's no guarantee that the other partition will be loaded, so it
can't be reused.

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

llvm-svn: 361926
2019-05-29 04:06:01 +00:00
Peter Collingbourne ba2816be82 ELF: Add basic partition data structures and behaviours.
This change causes us to read partition specifications from partition
specification sections and split output sections into partitions according
to their reachability from partition entry points.

This is only the first step towards a full implementation of partitions. Later
changes will add additional synthetic sections to each partition so that
they can be loaded independently.

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

llvm-svn: 361925
2019-05-29 03:55:20 +00:00
Peter Collingbourne 31fda09b2d Add IR support, ELF section and user documentation for partitioning feature.
The partitioning feature was proposed here:
http://lists.llvm.org/pipermail/llvm-dev/2019-February/130583.html

This is mostly just documentation. The feature itself will be contributed
in subsequent patches.

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

llvm-svn: 361923
2019-05-29 03:29:01 +00:00
Fangrui Song 719322411c [ELF] Implement General Dynamic style TLSDESC for x86-64
This handles two initial relocation types R_X86_64_GOTPC32_TLSDESC and
R_X86_64_TLSDESC_CALL, as well as the GD->LE and GD->IE relaxations.

Reviewed By: ruiu

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

llvm-svn: 361911
2019-05-29 02:03:56 +00:00
Alexandre Ganea ccc1fa5e1d Revert r361842 as it breaks LLDB :: tools/lldb-mi/exec/exec-finish.test
llvm-svn: 361876
2019-05-28 20:57:56 +00:00
Reid Kleckner f612b18720 [COFF] Add ImportChunkThunk, simplify, deduplicate
Removes the isHotPatchable faux-virtual and virtual methods.  Follow-up to
D62362.

Reviewers: aganea

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

llvm-svn: 361851
2019-05-28 17:38:04 +00:00
Alexandre Ganea ebe22a1774 [LLD][COFF] Early load PDB type server files
We need to have all input files ready before doing debuginfo type merging.
This patch is moving the late PDB type server discovery much earlier in the process, when the explicit inputs (OBJs, LIBs) are loaded.
The short term goal is to parallelize type merging.

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

llvm-svn: 361842
2019-05-28 15:35:23 +00:00
Alexandre Ganea 756565d470 Fix 'warning: comparison is always true due to limited range of data type [-Wtype-limits]' with GCC 7.3
llvm-svn: 361840
2019-05-28 15:32:11 +00:00
Fangrui Song 5d3b3188f7 Reland D61583 [ELF] Error on relocations to STT_SECTION symbols if the sections were discarded
This is implemented by creating Undefined (instead of Defined) for such
local STT_SECTION symbols. It allows us to catch errors when there are
relocations to such discarded sections (e.g. in PR41693, ld.bfd and gold
error but we don't). Updated comdat-discarded-error.s checks we emit
friendly error message.

For relocatable-eh-frame.s, ld.lld -r a.o a.o will now error
"STT_SECTION symbol should be defined" because the section .eh_frame
refers to is now an Undefined instead of a Defined.
So I have to change `error()` to `warn()` to retain the output.

rLLD361144 inadvertently enabled the error for --gdb-index
(in LLDDwarfObj<ELFT>::findAux()).

Relocations from .debug_info (not in comdat) to .text.* (in comdat) for
DW_AT_low_pc are common. If an .text.* was discarded, rLLD361144 would error,
which was unexpected. (Note, if we don't error as this patch does,
InputSection::relocateNonAlloc() will resolve such relocations).

llvm-svn: 361830
2019-05-28 14:34:28 +00:00
Nico Weber 833c5abbce Add release note entries for recent typo correction changes
Differential Revision: https://reviews.llvm.org/D62523

llvm-svn: 361824
2019-05-28 14:04:48 +00:00
Haojian Wu 241dcb386e Revert [ELF] Error on relocations to STT_SECTION symbols if the sections were discarded
This reverts r361792 (git commit cfca5095df), the
revision causes link errors internally, will share more details with the
author.

llvm-svn: 361806
2019-05-28 11:21:59 +00:00
Fangrui Song 173a68f1fb [ELF] Replace two addSymbol() call sites with Symbol::resolve(). NFC
If we have a handle of the symbol, insert() called by addSymbol() is
redundant. Just call resolve().

llvm-svn: 361802
2019-05-28 10:12:06 +00:00
Fangrui Song cfca5095df [ELF] Error on relocations to STT_SECTION symbols if the sections were discarded
This is implemented by creating Undefined (instead of Defined) for such
local STT_SECTION symbols. It allows us to catch errors when there are
relocations to such discarded sections (e.g. in PR41693, ld.bfd and gold
error but we don't). Updated comdat-discarded-error.s checks we emit
friendly error message.

For relocatable-eh-frame.s, ld.lld -r a.o a.o will now error
"STT_SECTION symbol should be defined" because the section .eh_frame
refers to is now an Undefined instead of a Defined.
So I have to change `error()` to `warn()` to retain the output.

Reviewed By: ruiu

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

llvm-svn: 361792
2019-05-28 06:34:52 +00:00
Rui Ueyama d8f8abbd4a Use SymbolTable::insert() to implement --trace.
Differential Revision: https://reviews.llvm.org/D62381

llvm-svn: 361791
2019-05-28 06:33:06 +00:00
Rui Ueyama 92069605bf Merge ELFFileBase::{initSymtab,parseHeader} as ELFFileBase:init. NFC.
This patch simplifies ELFFile instance initialization by merging
two similar functions into a single function and call it from the
ctor.

llvm-svn: 361789
2019-05-28 05:17:21 +00:00
Rui Ueyama 76737f4d19 Remove elf::createSharedFile and move its code to SharedFile's ctor. NFC.
llvm-svn: 361747
2019-05-27 07:26:13 +00:00
Sam Clegg 59f959ff33 [WebAssembly] Relax signature checking for undefined functions that are not called directly
When function signatures don't match and the undefined function is not
called directly (i.e. only has its address taken) we don't issue a
warning or create a runtime thunk for the undefined function.

Instead in this case we simply use the defined version of the function.
This is possible since checking signatures of dynamic calls happens
at runtime so any invalid usage will still result in a runtime error.

This is needed to allow C++ programs to link without generating
warnings.  Its not uncommon in C++ for vtables to be populated by
function address whee the signature of the function is not known in the
compilation unit.  In this case clang declares the method as void(void)
and relies on the vtable caller casting the data back to the correct
signature.

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

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

llvm-svn: 361678
2019-05-24 22:45:08 +00:00
Reid Kleckner a431dd7ae7 [COFF] De-virtualize Chunk and SectionChunk
Shaves another pointer off of SectionChunk, reducing the size from 96 to
88 bytes, down from 144 before I started working on this. Combined with
D62356, this reduced peak memory usage when linking chrome_child.dll
from 713MB to 675MB, or 5%.

Create NonSectionChunk to provide virtual dispatch to the rest of the
chunk types.

Reviewers: ruiu, aganea

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

llvm-svn: 361667
2019-05-24 20:25:40 +00:00
Reid Kleckner 56bee1a90a [COFF] Replace OutputSection* with uint16_t index in Chunk
Shaves another 8 bytes off of SectionChunk, the most commonly allocated
type in LLD.

These indices are only valid after we've assigned chunks to output
sections and removed empty sections, so do that in a new pass.

Reviewers: ruiu, aganea

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

llvm-svn: 361657
2019-05-24 18:25:49 +00:00
Sam Clegg a5ca34e6b3 [WebAssebmly] Add support for --wrap
The code for implementing this features is taken almost verbatim
from the ELF backend.

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

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

llvm-svn: 361639
2019-05-24 14:14:25 +00:00
Sam Clegg 7991b68284 [lld] Trace all references with lld --trace-symbol
Previously undefined symbol references were only traced if they were
seen before that definition.

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

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

llvm-svn: 361636
2019-05-24 13:29:17 +00:00
Sam Clegg 35be7ff80c [WebAssembly] Add support for -emit-relocs
This can be useful for post-link tools and for testing.  Sometimes
it can be useful to produces a regular executable but with relocations
preserved.

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

llvm-svn: 361635
2019-05-24 13:28:27 +00:00
Rui Ueyama 74de6203ef [LLD][COFF] Implement /filealign parameter
Patch by Stefan Schmidt.

This adds the /filealign parameter to lld, which allows to specify the
section alignment in the output file (as it does on Microsoft's
link.exe).

This is required to be able to load dynamically linked libraries on the
original Xbox, where the debugger monitor expects the section alignment
in the file to be the same as in memory.

llvm-svn: 361634
2019-05-24 12:42:36 +00:00
Simon Atanasyan 8362cbe13b [llvm-readobj] Implement GNU-style output for dynamic table
GNU readelf tool prints slightly different dynamic table "header" and
surrounds dynamic tag names by brackets. This patch implements the same
formatting for GNU-style output of the `llvm-readobj`.

LLVM
```
DynamicSection [ (13 entries)
  Tag        Type                 Name/Value
  0x00000006 SYMTAB               0x168
  ...
]
```

GNU
```
Dynamic section at offset 0x1d0 contains 13 entries:
  Tag        Type                 Name/Value
  0x00000006 (SYMTAB)             0x168
  ...
```

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

llvm-svn: 361633
2019-05-24 12:22:53 +00:00
Fangrui Song 7d4a67852d [ELF] Fix a doc typo. NFC
llvm-svn: 361617
2019-05-24 09:53:25 +00:00
Fangrui Song 7f1ff68a16 [ELF] Deleted unused forward declarations. NFC
llvm-svn: 361614
2019-05-24 09:25:47 +00:00
Reid Kleckner 11c141eb68 [COFF] Remove finalizeContents virtual method from Chunk, NFC
This only needs to be done for MergeChunks, so just do that in a
separate pass in the Writer.

This is one small step towards eliminating the vtable in Chunk.

llvm-svn: 361573
2019-05-24 00:02:00 +00:00
Peter Collingbourne ca6a8ae0bf ELF: Remove a comparison against In.EhFrame. NFCI.
This won't work once we have multiple .eh_frame sections.

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

llvm-svn: 361556
2019-05-23 21:30:30 +00:00
Reid Kleckner 14f4ff6e89 [COFF] Move KeepUnique bit from Chunk to SectionChunk, NFC
The KeepUnique bit is used during ICF, which only operates on
SectionChunks, so only SectionChunks need it. This frees up a byte in
Chunk, which I plan to use in a follow-up change.

llvm-svn: 361549
2019-05-23 20:26:41 +00:00
Nico Weber 9b2830b46e lld-link, clang: Treat non-existent input files as possible spellos for option flags
OptTable treats arguments starting with / that aren't a known option
as filenames. This means lld-link's and clang-cl's typo correction for
unknown flags didn't do spell checking for misspelled options that start
with /.

I first tried changing OptTable, but that got pretty messy, see PR41787
comments 2 and 3.

Instead, let lld-link's and clang's (including clang-cl's) "file not
found" diagnostic check if a non-existent file looks like it could be a
mis-spelled option, and if so add a "did you mean" suggestion to the
"file not found" diagnostic.

While here, make formatting of a few diagnostics a bit more
self-consistent.

Fixes PR41787.

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

llvm-svn: 361518
2019-05-23 17:58:33 +00:00
Rui Ueyama f5d9d23905 Simplify InputFile::fetch().
We don't have to return a value from the function. Instead, we can
directly call parseFile from the functions.

llvm-svn: 361478
2019-05-23 10:15:12 +00:00
Rui Ueyama 821a1ac050 Remove LazyObjFile::AddedToLink.
Instead we can just clear a MemoryBuffer so that we cannot get the
same buffer more than once.

llvm-svn: 361477
2019-05-23 10:08:56 +00:00
Sam Clegg 4bce63a0e7 Reland: [WebAssembly] Add __start_/_stop_ symbols for data sections
This is a reland of rL361235.

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

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

llvm-svn: 361476
2019-05-23 10:06:03 +00:00
Rui Ueyama 7f7d2b2e62 Move code for symbol resolution from SymbolTable.cpp to Symbols.cpp.
My recent commits separated symbol resolution from the symbol table,
so the functions to resolve symbols are now in a somewhat wrong file.
This patch moves it to Symbols.cpp.

The functions are now member functions of the symbol.

This is code move change. I modified function names so that they are
appropriate as member functions, though. No functionality change
intended.

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

llvm-svn: 361474
2019-05-23 09:58:08 +00:00
Rui Ueyama 4254840313 Speed up --start-lib and --end-lib.
--{start,end}-lib give files grouped by the options the archive file
semantics. That is, each object file between them acts as if it were
in an archive file whose sole member is the file.

Therefore, files between --{start,end}-lib are linked to the final
output only if they are needed to resolve some undefined symbols.

Previously, the feature was implemented this way:

 1. We read a symbol table and insert defined symbols to the symbol
    table as lazy symbols.

 2. If an undefind symbol is resolved to a lazy symbol, that lazy
    symbol instantiate ObjFile class for that symbol, which re-insert
    all defined symbols to the symbol table.

So, if an ObjFile is instantiated, defined symbols are inserted to the
symbol table twice. Since inserting long symbol names is not cheap,
there's a room to optimize here.

This patch optimzies it. Now, LazyObjFile remembers symbol handles and
passed them over to a new ObjFile instance, so that the ObjFile
doesn't insert the same strings.

Here is a quick benchmark to link clang. "Original" is the original
lld with unmodified command line options. For "Case 1" and "Case 2", I
extracted all files from archive files and replace .a's in a command
line with .o's wrapped with --{start,end}-lib. I used the original lld
for Case 1" and use this patch for Case 2.

  Original: 5.892
    Case 1: 6.001 (+1.8%)
    Case 2: 5.701 (-3.2%)

So, interestingly, --{start,end}-lib are now faster than the regular
linking scheme with archive files. That's perhaps not too surprising,
though, because for regular archive files, we look up the symbol table
with the same string twice.

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

llvm-svn: 361473
2019-05-23 09:53:30 +00:00
George Rimar 77b4f0abb8 [LLD][ELF] - Improve diagnostic about unrecognized relocations.
This is a minor improvement inspired by https://bugs.llvm.org/show_bug.cgi?id=38303.

A person reported that he observed message complaining about unsupported R_ARM_V4BX:
error: can't create dynamic relocation R_ARM_V4BX against local symbol in readonly segment; recompile object files with -fPIC

But with -z notext he only saw a relocation number, what is not convenient:
error: ../../gfx/cairo/libpixman/src/pixman-arm-neon-asm-bilinear.o:(.text+0x4F0): unrecognized reloc 40

Also, in the error messages we use relocation but not reloc.

With this patch we start to print one of the following messages:
error: file.o: unrecognized relocation Unknown(999)
error: file.o: unrecognized relocation R_X_KNOWN_BY_LLVM_BUT_UNSUPPORTED_BY_LLD_NAME

There is no way to write a test for that I believe.

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

llvm-svn: 361472
2019-05-23 09:50:18 +00:00
Sam Clegg b9889bbacb [WebAssembly] Seal imports section before counting imports
Summary:
Before we can assign entries in the function of global index space
we need to know the total number of function and global imports
respectively.

To avoid programmer error this change seals that imports section before
assigned function and global index space.  Any attempt to add an import
after the section is sealed will assert.

The lack this such as check caused  https://reviews.llvm.org/D61876
to be reverted.  I'm also trying to craft a test case the this
failure.

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

Tags: #llvm

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

llvm-svn: 361470
2019-05-23 09:41:03 +00:00
Rui Ueyama 0baaf45be7 Move SymbolTable::addCombinedLTOObject() to LinkerDriver.
Also renames it LinkerDriver::compileBitcodeFiles.

The function doesn't logically belong to SymbolTable. We added this
function to the symbol table because symbol table used to be a
container of input files. This is no longer the case.

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

llvm-svn: 361469
2019-05-23 09:26:27 +00:00
Reid Kleckner ee4e0a2942 Re-land r361206 "[COFF] Store alignment in log2 form, NFC"
The previous patch lost the call to PowerOf2Ceil, which causes LLD to
crash when handling common symbols with a non-power-of-2 size. I tweaked
the existing common.test to make the bsspad16 common symbol be 15 bytes
to add coverage for this case.

llvm-svn: 361426
2019-05-22 20:21:52 +00:00
Simon Atanasyan 74b791b4f7 [mips] Use GNU-style output to make tests smaller. NFC
llvm-svn: 361394
2019-05-22 15:03:52 +00:00
Simon Atanasyan 84f929d00c [mips] Replace multiple `-check-prefix` by single `-check-prefixes`. NFC
llvm-svn: 361393
2019-05-22 15:03:42 +00:00
Rui Ueyama ecf6eb515f Copy symbol length when we replace a symbol.
Symbol's NameSize is computed lazily. Currently, when we replace a symbol,
a cached length value can be discarded. This patch propagates that value.

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

llvm-svn: 361364
2019-05-22 09:19:30 +00:00
Fangrui Song b72b091389 [ELF] Improve error message for relocations to symbols defined in discarded sections
Rather than report "undefined symbol: ", give more informative message
about the object file that defines the discarded section.

In particular, PR41133, if the section is a discarded COMDAT, print the
section group signature and the object file with the prevailing
definition. This is useful to track down some ODR issues.

We need to
* add `uint32_t DiscardedSecIdx` to Undefined for this feature.
* make ComdatGroups public and change its type to DenseMap<CachedHashStringRef, const InputFile *>

Reviewed By: ruiu

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

llvm-svn: 361359
2019-05-22 09:06:42 +00:00
Rui Ueyama 33e74d9f62 Simplify the logic to instantiate Symbols. Should be NFC.
llvm-svn: 361350
2019-05-22 04:56:25 +00:00
Fangrui Song b372259ace [docs] Fix troff macro (.F1 -> .Fl) in ld.lld.1
llvm-svn: 361345
2019-05-22 02:15:25 +00:00
Richard Smith 32591ca4e2 Fix test to put its outputs into the temp directory.
llvm-svn: 361297
2019-05-21 19:41:19 +00:00
Sam Clegg c0a4f45bcb Revert "[WebAssembly] Add __start_/_stop_ symbols for data sections"
This reverts commit 7804dbddcc.

This change broke a bunch of tests of the WebAssembly waterfall.
Will hopefully reland with increased test coverage.

llvm-svn: 361273
2019-05-21 17:16:33 +00:00
Fangrui Song 5ea0d06e81 [ELF] Deleted unused ComdatGroups member variable left by D61854
llvm-svn: 361266
2019-05-21 14:40:38 +00:00
Rui Ueyama d3f27f2bf4 Fix test failure.
I forgot to submit a last-minute change to the last patch.

llvm-svn: 361249
2019-05-21 12:01:16 +00:00
Rui Ueyama 35150bb534 [WebAssembly] Add --reproduce.
--reproduce is a convenient option for debugging. If you invoke lld
with `--reproduce=repro.tar`, it creates `repro.tar` with all input
files and the command line options given to the linker, so that it is
very easy to run lld with the exact same inputs.

ELF and Windows lld have this option.

This patch add that option to lld/wasm.

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

llvm-svn: 361244
2019-05-21 11:52:14 +00:00
Sam Clegg 7804dbddcc [WebAssembly] Add __start_/_stop_ symbols for data sections
Fixes https://bugs.llvm.org/show_bug.cgi?id=41565

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

llvm-svn: 361236
2019-05-21 10:07:30 +00:00
Sam Clegg 8fcf012693 [WebAssembly] Refactor synthetic sections and relocation processing. NFC.
Major refactor to better match the structure of the ELF linker.

- Split out relocation processing into scanRelocations
- Split out synthetic sections into their own classes.

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

llvm-svn: 361233
2019-05-21 09:13:09 +00:00
Fangrui Song ecf4c9e13c [ELF] Don't advance position in a memory region when assigning to the Dot
For memory5.test, ld.bfd appears to ignore `. += 0x2000;`, so the test was testing
a wrong behavior. After deleting the code added in rLLD336335, we match ld.bfd and thus fix PR41357.

PR37836 (memory4.test) seems to have been fixed by another change.

Reviewed By: ruiu

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

llvm-svn: 361228
2019-05-21 08:21:44 +00:00
Fangrui Song 547e3e930c [ELF] Error on relocations to local undefined symbols
For a reference to a local symbol, ld.bfd and gold error if the symbol
is defined in a discarded section but accept it if the symbol is
undefined. This inconsistent behavior seems unnecessary for us (it
probably makes sense for them as they differentiate local/global
symbols, the error would mean more code).

Catch such errors. Symbol index 0 may be used by marker relocations,
e.g. R_*_NONE R_ARM_V4BX. Don't error on them.

The difference from D61563 (which caused msan failure) is we don't call
Sym.computeBinding() on local symbols - VersionId is uninitialized.

llvm-svn: 361213
2019-05-21 02:38:11 +00:00
Nico Weber 67510fac36 Revert r361206 "[COFF] Store alignment in log2 form, NFC"
Makes the linker crash when linking nasm.exe.

llvm-svn: 361212
2019-05-21 02:06:59 +00:00
Reid Kleckner 1a5cc629de [COFF] Store alignment in log2 form, NFC
Summary:
Valid section or chunk alignments are powers of 2 in the range [1,
8192]. These can be stored more canonically in log2 form to free up some
bits in Chunk. Combined with D61696, SectionChunk gets 8 bytes smaller.

Reviewers: ruiu, aganea

Subscribers: llvm-commits

Tags: #llvm

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

llvm-svn: 361206
2019-05-20 22:57:52 +00:00
Tiancong Wang a5d8d01d6f [ELF][Driver] Fix precedence of symbol ordering file and CGProfile
This patch is a fix for https://bugs.llvm.org/show_bug.cgi?id=41804.
We try to solve the precedence of user-specified symbol ordering file and C3 ordering provided as call graph. It deals with two case:
(1) When both --symbol-ordering-file=<file> and --call-graph-order-file=<file> are present, whichever flag comes later will take precedence.
(2) When only --symbol-ordering-file=<file> is present, it takes precedence over implicit call graph (CGProfile) generated by CGProfilePass enabled in new pass manager.

llvm-svn: 361190
2019-05-20 19:13:34 +00:00
Tiancong Wang af4219adf5 Test commit, add an empty line.
llvm-svn: 361186
2019-05-20 18:46:25 +00:00
Fangrui Song 055906e1e5 [ELF] -z combreloc: sort dynamic relocations by (!is_relative,symbol_index,r_offset)
We currently sort dynamic relocations by (!is_relative,symbol_index).
Add r_offset as the third key. This makes `readelf -r` debugging easier
(relocations to the same symbol are ordered by r_offset).

Refactor the test combreloc.s (renamed from combrelocs.s) to check
R_X86_64_RELATIVE, and delete --expand-relocs.

The difference from the reverted D61477 is that we keep !is_relative as
the first key. In local dynamic TLS model, DTPMOD (e.g.
R_ARM_TLS_DTPMOD32 R_X86_64_DTPMOD and R_PPC{,64}_DTPMOD) may use 0 as
the symbol index.

Reviewed By: grimar

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

llvm-svn: 361164
2019-05-20 15:25:01 +00:00
Dmitri Gribenko 0429fddc9d Revert "[ELF] Error on relocations to local undefined symbols"
This reverts commit r361144.  It causes a use-of-uninitialized-value in
maybeReportUndefined at llvm/tools/lld/ELF/Relocations.cpp:682, as
detected by MemorySanitizer when local-undefined-symbol.s test is run.

llvm-svn: 361162
2019-05-20 15:04:08 +00:00
Dmitri Gribenko a2fbe2bcda Revert "[ELF] -z combreloc: sort dynamic relocations by (symbol_index,r_offset)"
This reverts commit r361125.  This linker change breaks shared libraries
in some subtle way on x86_64.  (Specifically, gold segfaults when
loading the LLVMgold.so plugin linked with lldb with this patch.)

llvm-svn: 361150
2019-05-20 13:05:55 +00:00
Fangrui Song 2109572464 [ELF] Fix getRelocTargetVA formulae of R_TLS and R_NEG_TLS
For R_TLS:
1) Delete Sym.isTls() . The assembler ensures the symbol is STT_TLS.
   If not (the input is broken), we would crash (dereferencing null Out::TlsPhdr).
2) Change Sym.isUndefWeak() to Sym.isUndefined(), otherwise with --noinhibit-exec
   we would still evaluate the symbol and crash.
3) Return A if the symbol is undefined. This is PR40570.
   The case is probably unrealistic but returning A matches R_ABS and the
   behavior of several dynamic loaders.

R_NEG_TLS is obsoleted Sun TLS we don't fully support, but
R_RELAX_TLS_GD_TO_LE_NEG is still used by GD->LE relaxation (subl $var@tpoff,%eax).

They should add the addend. Unfortunately I can't test it as compilers don't seem to generate non-zero implicit addends.

Reviewed By: ruiu

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

llvm-svn: 361146
2019-05-20 11:47:31 +00:00
Fangrui Song 7c7425483a [ELF] Error on relocations to local undefined symbols
For a reference to a local symbol, ld.bfd and gold error if the symbol
is defined in a discarded section but accept it if the symbol is
undefined. This inconsistent behavior seems unnecessary for us (it
probably makes sense for them as they differentiate local/global
symbols, the error would mean more code).

Weaken the condition to getSymbol(Config->IsMips64EL) == 0 to catch such
errors. The symbol index can be 0 (e.g. R_*_NONE R_ARM_V4BX) and we shouldn't error on them.

Reviewed By: ruiu

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

llvm-svn: 361144
2019-05-20 11:25:55 +00:00
Fangrui Song 63514556c4 [MIPS] Update mips-abs-got.s to not use .reloc
As Simon Atanasyan suggested in D61563

llvm-svn: 361142
2019-05-20 11:17:02 +00:00
Yvan Roux bc07278e02 [ELF] Fix tests from r361013 on ARM/AARch64 bots.
llvm-svn: 361128
2019-05-20 08:51:39 +00:00
Fangrui Song 9f1a6de631 [ELF] -z combreloc: sort dynamic relocations by (symbol_index,r_offset)
Fixes PR41692.

We currently sort dynamic relocations by (!is_relative,symbol_index).
Change it to (symbol_index,r_offset). We still place relative
relocations first because R_*_RELATIVE are the only dynamic relocations
with 0 symbol index (except on MIPS, which doesn't use DT_REL[A]COUNT
anyway).

This makes `readelf -r` debugging easier (relocations to the same symbol
are ordered by r_offset).

Refactor the test combreloc.s (renamed from combrelocs.s) to check
R_X86_64_RELATIVE, and delete --expand-relocs.

Reviewed By: ruiu

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

llvm-svn: 361125
2019-05-20 07:22:55 +00:00
Rui Ueyama faf541e1e1 Make replaceSymbol a member function of Symbol.
This is a mechanical rewrite of replaceSymbol(A, B) to A->replace(B).
I also added a comment to Symbol::replace().

Technically this change is not necessary, but this change makes code a
bit more concise.

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

llvm-svn: 361123
2019-05-20 03:36:33 +00:00
Fangrui Song a6720e7407 [ELF] Copy IsPreemptible in replaceSymbol()
Otherwise, we may set IsPreemptible (e.g. --dynamic-list) then clear it
(in replaceCommonSymbols()).

Reviewed By: ruiu

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

llvm-svn: 361122
2019-05-20 02:07:03 +00:00
Michael Liao 63621832da Suppress false-positive GCC -Wreturn-type warning.
llvm-svn: 361094
2019-05-18 06:35:47 +00:00
Martin Storsjo 5fab538e2a [MinGW] Set the right updated option help for the updated pdb option. NFC
I forgot to amend this change before committing it in SVN r361014, this
is what was agreed upon in the review.

llvm-svn: 361093
2019-05-18 04:20:31 +00:00
Fangrui Song ed2ad77ccb [ARM][AArch64] Revert Android Bionic PT_TLS overaligning hack
This reverts D53906.

D53906 increased p_align of PT_TLS on ARM/AArch64 to 32/64 to make the
static TLS layout compatible with Android Bionic's ELF TLS. However,
this may cause glibc ARM/AArch64 programs to crash (see PR41527).

The faulty PT_TLS in the executable satisfies p_vaddr%p_align != 0. The
remainder is normally 0 but may be non-zero with the hack in place. The
problem is that we increase PT_TLS's p_align after OutputSections'
addresses are fixed (assignAddress()). It is possible that
p_vaddr%old_p_align = 0 while p_vaddr%new_p_align != 0.

For a thread local variable defined in the executable, lld computed TLS
offset (local exec) is different from glibc computed TLS offset from
another module (initial exec/generic dynamic). Note: PR41527 said the
bug affects initial exec but actually generic dynamic is affected as
well.

(glibc is correct in that it compute offsets that satisfy
`offset%p_align == p_vaddr%p_align`, which is a basic ELF requirement.
This hack appears to work on FreeBSD rtld, musl<=1.1.22, and Bionic, but
that is just because they (and lld) incorrectly compute offsets that
satisfy `offset%p_align = 0` instead.)

Android developers are fine to revert this patch, carry this patch in
their tree before figuring out a long-term solution (e.g. a dummy .tdata
with sh_addralign=64 sh_size={0,1} in crtbegin*.o files. The overhead is
now insignificant after D62059).

Reviewed By: rprichard, srhines

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

llvm-svn: 361090
2019-05-18 03:16:00 +00:00
Fangrui Song 898896836d [ELF][X86] Fix R_RELAX_TLS_GD_TO_LE_NEG and R_NEG_TLS after D62059
After D62059, we don't align p_memsz of PT_TLS to p_align. The
getRelocTargetVA formula should align it instead.

It becomes clear that R_NEG_TLS and R_TLS are opposite from each other.

In i386-tls-le-align.s, I put ret after call ___tls_get_addr@plt as
otherwise ld.bfd would reject the relaxation:
TLS transition from R_386_TLS_GD to R_386_TLS_LE_32 against `a' at 0x3 in section `.text' failed

llvm-svn: 361088
2019-05-18 01:58:40 +00:00
Fangrui Song db5cb0fce7 [ELF] Add {i386,x86-64}-tls-le-align.s to test TP offsets for TLS Variant 2
I forgot to add them in rLLD361084. I made another mistake, rLLD361084 fixed Variant 2, not Variant 1.

llvm-svn: 361085
2019-05-18 00:57:30 +00:00
Fangrui Song 348731aeed [ELF] Fix TP offset of TLS Variant I after D62059
As Ryan Prichard pointed out, after D62059, the TP offset is incorrect.

Add x86-64-tls-le-align.s to check this.  Better formulae for both
variants should take p_vaddr%p_align into account (offset%p_align =
p_vaddr%p_align is a basic ELF requirement), but I can't find a way to
test the behavior.

llvm-svn: 361084
2019-05-18 00:43:10 +00:00
Russell Gallop 3a02b12439 Fix tests from r361013 on X86 bots.
llvm-svn: 361041
2019-05-17 15:38:01 +00:00
Fangrui Song f3a3b93f54 [ELF] -r: fix R_*_NONE to section symbols on Elf*_Rel targets
On Elf*_Rel targets, for a relocation to a section symbol, an R_ABS is
added which will be used by relocateOne() to compute the implicit
addend.

Addends of R_*_NONE should be ignored, so don't emit an R_ABS.

This fixes crashes on X86 and ARM because their relocateOne() do not
handle R_*_NONE.

Reviewed By: peter.smith

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

llvm-svn: 361036
2019-05-17 14:11:03 +00:00
Fangrui Song f3dccc64af [ELF] Don't align PT_TLS's p_memsz
The code was added in r252352, probably to address some layout issues.
Actually PT_TLS's p_memsz doesn't need to be aligned on either variant.
ld.bfd doesn't do that.

In case of larger alignment (e.g. 64 for Android Bionic on AArch64, see
D62055), this may make the overhead smaller.

Reviewed By: ruiu

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

llvm-svn: 361029
2019-05-17 12:48:53 +00:00
Martin Storsjo 6ff6a8f656 [MinGW] Include --enable-auto-import in the help listing
Libtool concludes that the linker doesn't support shared libraries,
unless this flag is listed in the output of --help.

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

llvm-svn: 361017
2019-05-17 11:07:46 +00:00