Commit Graph

1477 Commits

Author SHA1 Message Date
Reid Kleckner a30920c31f [COFF] Pack Name in Symbol as is done in ELF
Summary:
This assumes all symbols are <4GB long, so we can store them as a 32-bit
integer. This reorders the fields so the length appears first, packing
with the other bitfield data in the base Symbol object.

This saved 70MB / 3.60% of heap allocations when linking
browser_tests.exe with no PDB. It's not much as a percentage, but worth
doing. I didn't do performance measurements, I don't think it will be
measurable in time.

Reviewers: ruiu, inglorion, amccarth, aganea

Subscribers: llvm-commits

Tags: #llvm

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

llvm-svn: 358794
2019-04-19 22:51:49 +00:00
Bob Haarman 8b1ec798b5 [LLD][COFF] use offset in archive to disambiguate archive members
Summary:
Archives can contain multiple members with the same name. This would
cause ThinLTO links to fail ("Expected at most one ThinLTO module per
bitcode file"). This change implements the same strategy we use in
the ELF linker: make the offset in the archive part of the module
name so that names are unique.

Reviewers: pcc, mehdi_amini, ruiu

Reviewed By: ruiu

Subscribers: eraman, steven_wu, dexonsmith, llvm-commits

Tags: #llvm

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

llvm-svn: 358440
2019-04-15 19:48:32 +00:00
Martin Storsjo cdf126ebec [COFF] Link crtend.o as the last object file
When faced with command line options such as "crtbegin.o appmain.o
-lsomelib crtend.o", GNU ld pulls in all necessary object files from
somelib before proceeding to crtend.o.

LLD operates differently, only loading object files from any
referenced static libraries after processing all input object files.

This uses a similar hack as in the ELF linker. Here, it moves crtend.o
to the end of the vector of object files. This makes sure that
terminator chunks for sections such as .eh_frame gets ordered last,
fixing DWARF exception handling for libgcc and gcc's crtend.o.

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

llvm-svn: 358394
2019-04-15 10:57:44 +00:00
Reid Kleckner e10d00419a [codeview] Remove Type member from CVRecord
Summary:
Now CVType and CVSymbol are effectively type-safe wrappers around
ArrayRef<uint8_t>. Make the kind() accessor load it from the
RecordPrefix, which is the same for types and symbols.

Reviewers: zturner, aganea

Subscribers: hiraditya, llvm-commits

Tags: #llvm

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

llvm-svn: 357658
2019-04-04 00:28:48 +00:00
Reid Kleckner cc525c97b7 [COFF] Reduce the size of Chunk and SectionChunk, NFC
Summary:
Reorder the fields in both to use padding more efficiently, and add more
comments on the purpose of the fields.

Replace `std::vector<SectionChunk*> AssociativeChildren` with a
singly-linked list. This avoids the separate vector allocation to list
associative children, and shrinks the 3 pointers used for the typically
empty vector down to 1.

In the end, this reduces the sum of heap allocations used to link
browser_tests.exe with NO PDB by 13.10%, going from 2,248,728 KB to
1,954,071 KB of heap. These numbers exclude memory mapped files, which
are of course a significant factor in LLD's memory usage.

Reviewers: ruiu, mstorsjo, aganea

Subscribers: jdoerfert, llvm-commits

Tags: #llvm

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

llvm-svn: 357535
2019-04-02 22:11:58 +00:00
Alexandre Ganea 19775a4c67 [LLD][COFF] Move type merging structures out of PDB.cpp. NFC
Introduce a new TypeMerger class, out of some type-merge-specific structures from PDB.cpp

No changes intended / this is only moving code around.

This patch is step 3. in "Proposed commit strategy" in D59226

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

llvm-svn: 357525
2019-04-02 20:43:19 +00:00
Matthew Voss 3c023420d1 [NFC][LLD] Specify namespaces explicity to fix build failure on GCC 5 after r357383
llvm-svn: 357421
2019-04-01 19:23:56 +00:00
Alexandre Ganea 30c2f20e55 Fix builder.
http://lab.llvm.org:8011/builders/sanitizer-x86_64-linux-fuzzer/builds/24702/steps/check-fuzzer/logs/stdio

llvm-svn: 357391
2019-04-01 14:37:36 +00:00
Alexandre Ganea bf55c4e3e3 [LLD][COFF] Early dependency detection
We introduce a new class hierarchy for debug types merging (in DebugTypes.h). The end-goal is to parallelize the type merging - please see the plan in D59226.

Previously, dependency discovery was done on the fly, much later, during the type merging loop. Unfortunately, parallelizing the type merging requires the dependencies to be merged in first, before any dependent ObjFile, thus this early discovery.

The overall intention for this path is to discover debug information dependencies at a much earlier stage, when processing input files. Currently, two types of dependency are supported: PDB type servers (when compiling with MSVC /Zi) and precompiled headers OBJs (when compiling with MSVC /Yc and /Yu). Once discovered, an explicit link is added into the dependent ObjFile, through the new debug types class hierarchy introduced in DebugTypes.h.

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

llvm-svn: 357383
2019-04-01 13:36:59 +00:00
Rui Ueyama 68b9f45fee Replace `typedef A B` with `using B = A`. NFC.
I did this using Perl.

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

llvm-svn: 357372
2019-04-01 00:11:24 +00:00
Alexandre Ganea b13f064b5d Fix build following r357308 : Ensure only live thunks are considered when creating import modules
llvm-svn: 357316
2019-03-29 21:24:19 +00:00
Reid Kleckner ba708619ad Don't copy the .drective section with std::string
Both COFF and bitcode input files expose these as stable strings.

llvm-svn: 357314
2019-03-29 21:00:22 +00:00
Alexandre Ganea 09cca5b243 [LLD][COFF] Generate import modules & COFF groups in PDB
Generate import modules for each imported DLL, along with its symbol stream.
Also create COFF groups in the * Linker * module, one for each PartialSection (input, unmerged sections)
Currently COFF groups are disabled for MINGW because it significantly increases PDB sizes. We could enable that later with an option.

The overall objective for this change is to support code hot patching tools. Such tools need to know the import libraries used, from the PDB alone.

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

llvm-svn: 357308
2019-03-29 20:25:34 +00:00
Alexandre Ganea 347a45ccd5 [LLD][COFF] Improve checkFailIfMismatch()
As suggested by ruiu here (https://reviews.llvm.org/D58910#1425484), defer a call to toString(File) until it's really needed (if there's an error)

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

llvm-svn: 357305
2019-03-29 19:58:58 +00:00
Reid Kleckner 1600490af1 [COFF] Optimize range extension thunk insertion memory usage
Summary:
This avoids allocating O(#relocs) of intermediate data for each section
when range extension thunks aren't needed for that section. This also
removes a std::vector from SectionChunk, which further reduces its size.

Instead, this change adds the range extension thunk symbols to the
object files that contain sections that need extension thunks. By adding
them to the symbol table of the parent object, that means they now have
a symbol table index. Then we can then modify the original relocation,
after copying it to read-write memory, to use the new symbol table
index.

This makes linking browser_tests.exe with no PDB 10.46% faster, moving
it from 11.364s to 10.288s averaged over five runs.

Reviewers: mstorsjo, ruiu

Subscribers: aganea, llvm-commits

Tags: #llvm

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

llvm-svn: 357200
2019-03-28 18:30:03 +00:00
Alexandre Ganea 74d5b33222 [LLD][COFF] Separate module descriptors creation from type/symbol merging
Take module DBI creation out of PDBLinker::addObjFile() into its own function.

This is groundwork towards parallelizable type merging, as proposed in D59226.

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

llvm-svn: 356815
2019-03-22 22:07:27 +00:00
Alexandre Ganea 4aeea4cc42 [DebugInfo][PDB] Don't write empty debug streams
Before, empty debug streams were written as 8 bytes (4 bytes signature + 4 bytes for the GlobalRefs count).

With this patch, unused empty streams aren't emitted anymore. Modules now encode 65535 as an 'unused stream' value, by convention.
Also fix the * Linker * contrib section which wasn't correctly emitted previously.

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

llvm-svn: 356395
2019-03-18 19:13:23 +00:00
Fangrui Song 4ac6d7e4b8 [COFF] Delete unused declarations and add a missing forward declaration. NFC
llvm-svn: 356241
2019-03-15 09:40:03 +00:00
Alexandre Ganea 3e60ee9f10 [LLD][COFF] Add /summary to print statistics
/summary prints information about the data (OBJ/LIB/PDB) processed by LLD. The goal is have an estimate about the inputs and outputs, to better understand where the timings go.

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

llvm-svn: 356188
2019-03-14 18:45:08 +00:00
Nico Weber 020d92cb61 lld-link: Only print demangled symbol names by default
This makes lld-link's output a bit more concise. Since most developers can't
read mangled names, this should make the output a bit easier to understand as
well. It also makes lld-link's output consistent with ld.lld's output.

(link.exe prints both demangled and mangled names; lld-link used to match
link.exe output but now no longer does.)

For people working on toolchains, add a `/demangle:no` flag that makes lld-link
print the mangled name instead of the demangled name. (If desired, people could
pipe that through `demumble -b` to get the old behavior of both demangled and
mangled output.)

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

llvm-svn: 355878
2019-03-11 23:02:18 +00:00
Rui Ueyama 7fd99fc475 Fail early if an output file is not writable
Fixes https://bugs.llvm.org/show_bug.cgi?id=36478

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

llvm-svn: 355834
2019-03-11 16:30:55 +00:00
Alexandre Ganea d8ec81059e [LLD][COFF] More detailed information for /failifmismatch
When mismatched #pragma detect_mismatch declarations occur, now print the conflicting OBJs.

  lld-link: error: /failifmismatch: mismatch detected for 'TEST':
  >>> test.obj has value 1
  >>> test2.obj has value 2

Fixes PR38579

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

llvm-svn: 355543
2019-03-06 20:18:38 +00:00
Reid Kleckner 7818144ff3 [COFF] Add address-taken import thunks to the fid table
Summary: Fixes PR39799

Reviewers: dmajor, hans

Subscribers: jdoerfert, llvm-commits

Tags: #llvm

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

llvm-svn: 355141
2019-02-28 21:05:41 +00:00
Alexandre Ganea 97b2b0636b [LLD][COFF] Support /threads[:no] like the ELF driver
Differential review: https://reviews.llvm.org/D58594

llvm-svn: 355029
2019-02-27 20:53:50 +00:00
Alexandre Ganea d307c4c47f [LLD][COFF] Add support for /FUNCTIONPADMIN command-line option
Initial patch by Stefan Reinalter.

Fixes PR36775

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

llvm-svn: 354716
2019-02-23 01:46:18 +00:00
Bob Haarman 61e8735f17 [lld-link] preserve @llvm.used symbols in LTO
Summary:
We translate @llvm.used to COFF by generating /include directives
in the .drectve section. However, in LTO links, this happens after
directives have already been processed, so the new directives do
not take effect. This change marks @llvm.used symbols as GCRoots
so that they are preserved as intended.

Fixes PR40733.

Reviewers: rnk, pcc, ruiu

Reviewed By: ruiu

Subscribers: mehdi_amini, steven_wu, dexonsmith, dang, llvm-commits

Tags: #llvm

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

llvm-svn: 354410
2019-02-20 00:26:01 +00:00
Rui Ueyama 659f2752a0 Move MinGW-specific code out of LinkerDriver::link. NFC.
LinkerDriver::link is getting too long, it's time to simplify it.

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

llvm-svn: 354391
2019-02-19 22:06:44 +00:00
Martin Storsjo 272d8c18e0 [COFF] Add -exclude-all-symbols for MinGW
This is a private undocumented option, intended to be used by
the MinGW driver frontend.

Also restructure the condition to put if (Config->MinGW) first.
This changes the behaviour for the tautological combination of
-export-all-symbols without -lldmingw.

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

llvm-svn: 354386
2019-02-19 21:57:44 +00:00
Nico Weber 04db8cb92b lld/coff: Simplify error message for comdat selection mismatches
Turns out nobody understands what "conflicting comdat type" is supposed to
mean, so just emit a regular "duplicate symbol" error and move the comdat
selection information into /verbose output.

This also fixes a problem where the error output would depend on the order of
.obj files passed. Before this patch:

- If passed `one_only.obj discard.obj`, lld-link would only err "conflicting
  comdat type"

- If passed `discard.obj one_only.obj`, lld-link would err "conflicting comdat
  type" and then "duplicate symbol"

Now lld-link only errs "duplicate symbol" in both cases.

I considered adding a "Detail" parameter to reportDuplicate() that's printed in
parens at the end of the "duplicate symbol" diag if present, and then put the
comdat selection mismatch details there, but since users don't know what it's
supposed to mean decided against it. I also considered special-casing the
Detail message for one_only/discard mismatches, which in practice means
"function defined as inline in TU 1 but as out-of-line in TU 2", but I wasn't
sure how useful it is so I omitted that too.

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

llvm-svn: 354006
2019-02-14 03:16:44 +00:00
Bob Haarman 3edf63c55a [lld-link] better error message when failing to open archive members
Summary:
The message "could not get the buffer for the member defining symbol"
now also contains the name of the archive and the name of the archive
member that we tried to open.

Reviewers: ruiu

Subscribers: llvm-commits

Tags: #llvm

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

llvm-svn: 353572
2019-02-08 21:59:35 +00:00
Zachary Turner c5d68d499a [PDB] Remove dots and normalize slashes with /PDBSOURCEPATH.
In a previous patch, I made changes so that PDBs which were
generated on non-Windows platforms contained sensical paths
for the host.  While this is an esoteric use case, we need
it to be supported for certain cross compilation scenarios
especially with LLDB, which can debug things on non-Windows
platforms.

However, this regressed a case where you specify /PDBSOURCEPATH
and use a windows-style path.  Previously, we would still remove
dots and canonicalize slashes to backslashes, but since my
change intentionally tried to support non-backslash paths, this
was broken.

This patch fixes the situation by trying to guess which path
style the user is specifying when /PDBSOURCEPATH is passed.
It is intentionally conservative, erring on the side of a
Windows path style unless absolutely certain.  All dots are
removed and slashes canonicalized to whatever the deduced
path style is after appending the file path to the /PDBSOURCEPATH
argument.

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

llvm-svn: 353250
2019-02-06 00:50:35 +00:00
Martin Storsjo ccd4e5e016 [COFF] Avoid O(n^2) accesses into PartialSections
For MinGW, unique partial sections are much more common, e.g.
comdat functions get sections named e.g. text$symbol.

A moderate sized example of this contains over 200K Chunks
which create 174K unique PartialSections. Prior to SVN r352928
(D57574), linking this took around 1,5 seconds for me, while
it afterwards takes around 13 minutes. After this patch, the
linking time is back to what it was before.

The std::find_if in findPartialSection will do a linear scan of
the whole container until a match is found. To use something like
binary_search or the std::set container's own methods, we'd need
to already have a PartialSection*.

Reinstate a proper map instead of having a set with a custom sorting
comparator.

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

llvm-svn: 353146
2019-02-05 08:16:10 +00:00
Martin Storsjo c9f4d25f26 [COFF] Create range extension thunks for ARM64
On ARM64, this is normally necessary only after a module exceeds
128 MB in size (while the limit for thumb is 16 MB). For conditional
branches, the range limit is only 1 MB though (the same as for thumb),
and for the tbz instruction, the range is only 32 KB, which allows for
a test much smaller than the full 128 MB.

This fixes PR40467.

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

llvm-svn: 352929
2019-02-01 22:08:09 +00:00
Martin Storsjo b2b0cab0c3 [COFF] Fix crashes when writing a PDB after adding thunks.
When writing a PDB, the OutputSection of all chunks need to be set.
The thunks are added directly to OutputSection after the normal
machinery that sets it for all other chunks.

This fixes part of PR40467.

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

llvm-svn: 352928
2019-02-01 22:08:03 +00:00
Sam Clegg dfbd19033b Fix names of functions in TargetOptionsCommandFlags.h. NFC.
Differential Revision: https://reviews.llvm.org/D57555

llvm-svn: 352825
2019-02-01 02:24:50 +00:00
Nico Weber 9aa55d3c66 lld-link: Allow mixing 'discard' and 'largest' comdat selections
cl.exe and clang-cl.exe put vftables in a 'discard' comdat when building with
RTTI disabled (/GR-) but in a 'largest' comdat when building with RTTI enabled.
To be able to link /GR- code with /GR code, lld-link needs to accept comdats
that have this type of comdat selection conflict.

For example, static libraries in the Visual Studio standard library are built
with /GR, and without this it's impossible to build client code with /GR- and
still link to the standard library.

link.exe also accepts merging 'discard' with 'largest', and it accepts merging
'largest' with any other selection type. lld-link is still a bit stricter since
it only allows merging 'largest' with 'discard' for symmetry.

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

llvm-svn: 352765
2019-01-31 16:14:33 +00:00
Sam Clegg 5cdc91d003 [LTO] Set CGOptLevel in LTO config.
Previously we were never setting this which means it was always being
set to Default (-O2/-Os).

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

llvm-svn: 352667
2019-01-30 20:46:18 +00:00
Nico Weber 48dc110eea lld/coff: Implement some support for the comdat selection field
LLD used to handle comdats as if the selection field was always set to
IMAGE_COMDAT_SELECT_ANY. This means for obj files produced by `cl /Gy`, LLD
would never report a duplicate symbol error.

This change:
- adds validation for the Selection field (should make no difference in
  practice for compiler-generated obj inputs)
- rejects comdats that have different Selection fields in different obj files
  (likewise). This is a bit more strict but also more self-consistent thank
  link.exe (see comment in code)
- implements handling for all the selection kinds

In practice, compilers only generate comdats with
IMAGE_COMDAT_SELECT_NODUPLICATES (LLD now produces duplicate symbol errors for
these), IMAGE_COMDAT_SELECT_ANY (no behavior change), and
IMAGE_COMDAT_SELECT_LARGEST (for RTTI data; here LLD should no longer create
broken executables when linking some TUs with RTTI enabled and some with it
disabled – but see below).

The implementation of `IMAGE_COMDAT_SELECT_LARGEST` is incomplete: If one
SELECT_LARGEST comdat replaces an earlier one, the comdat symbol is replaced
correctly, but the old section stays loaded and if /opt:ref is disabled (via
/opt:noref or /debug) it's still written to the output. That's not ideal, but
better than the current treatment of just picking any one of those comdats. I
hope to fix this better later.

Fixes most of PR40094.

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

llvm-svn: 352590
2019-01-30 02:17:27 +00:00
Nico Weber 5b04e0a3fd lld-link: Allow backward references between associated comdats
References between associated comdats are invalid per COFF spec, but the newest
Windows SDK contains obj files that have these references
(https://bugs.chromium.org/p/chromium/issues/detail?id=925943#c13). So add back
support for them and add tests for them. The old code handled them fine.

This makes lld-link match the behavior of newer link.exe versions as far as I
can tell. (The behavior before this change matched the behavior of older
link.exe versions.)

This mostly reverts r352254.

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

llvm-svn: 352508
2019-01-29 15:50:31 +00:00
Nico Weber 38170e444f lld/coff: Make assoc comdat diag a bit more detailed
Many different sections can have the same name, so include the indices of the
sections mentioned in the diagnostic too.

I'm debugging something I can't repro locally, maybe this will help.

llvm-svn: 352428
2019-01-28 21:16:15 +00:00
Alexandre Ganea 864d2639f1 [LLD][COFF] Partial sections
Persist (input) sections that make up an OutputSection. This is a supporting patch for the upcoming D54802.

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

llvm-svn: 352336
2019-01-28 01:45:35 +00:00
Martin Storsjo acaa78b171 [COFF] Add support for the new relocation IMAGE_REL_ARM{,64}_REL32
Differential Revision: https://reviews.llvm.org/D57292

llvm-svn: 352325
2019-01-27 19:57:50 +00:00
Nico Weber b1a110c961 Follow-up to r352254: Initialize Selection field.
The diagnostic there fired spuriosly due to uninitialized memory.

llvm-svn: 352304
2019-01-27 03:56:37 +00:00
Nico Weber 6bb3a1aa75 lld-link: Store comdat selection in SectionChunk, reject more invalid associated comdats
I need the comdat selection for PR40094. To keep the patch for that smaller,
I'm adding it here, and as a first application I'm using it to reject
associative comdats referring to earlier associative comdats. Depends on
D56929; together with that all associative comdats referring to other
associative comdats are now rejected.

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

llvm-svn: 352254
2019-01-26 00:14:52 +00:00
Rui Ueyama 18972d1ee9 Fix broken export table if .rdata is merged with .text.
Previously, we assumed that .rdata is zero-filled, so when writing
an COFF import table, we didn't write anything if the data is zero.
That assumption was wrong because .rdata can be merged with .text.
If .rdata is merged with .text, they are initialized with 0xcc which
is a trap instruction.

This patch removes that assumption from code.

Should be merged to 8.0 branch as this is a regression.

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

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

llvm-svn: 352082
2019-01-24 19:02:31 +00:00
Nico Weber 0fb18e6e78 lld-link: Use just one code path to process associative comdats, reject some invalid associated comdats
Currently, if an associative comdat appears after the comdat it's associated
with it's processed immediately, else it's deferred until the end of the object
file. I found this confusing to think about while working on PR40094, so this
makes it so that associated comdats are always processed at the end of the
object file.  This seems to be perf-neutral and simpler.

Now there's a natural place to reject the associated comdats referring to later
associated comdats (associated comdats referring to associated comdats is
invalid per COFF spec) that, so reject those. (A later patch will reject
associated comdats referring to earlier comdats.)

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

llvm-svn: 351917
2019-01-23 02:07:10 +00:00
Peter Collingbourne bcd08c16bb COFF, ELF: ICF: Perform 2 rounds of relocation hash propagation.
LLD's performance on PGO instrumented Windows binaries was still not
great even with the fix in D56955; out of the 2m41s linker runtime,
around 2 minutes were still being spent in ICF. I looked into this more
closely and discovered that the vast majority of the runtime was being
spent segregating .pdata sections with the following relocation chain:

.pdata -> identical .text -> unique PGO counter (not eligible for ICF)

This patch causes us to perform 2 rounds of relocation hash
propagation, which allows the hash for the .pdata sections to
incorporate the identifier from the PGO counter. With that, the amount
of time spent in ICF was reduced to about 2 seconds. I also found that
the same change led to a significant ICF performance improvement in a
regular release build of Chromium's chrome_child.dll, where ICF time
was reduced from around 1s to around 700ms.

With the same change applied to the ELF linker, median of 100 runs
for lld-speed-test/chrome reduced from 4.53s to 4.45s on my machine.

I also experimented with increasing the number of propagation rounds
further, but I did not observe any further significant performance
improvements linking Chromium or Firefox.

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

llvm-svn: 351899
2019-01-22 23:54:49 +00:00
Peter Collingbourne 3426111145 COFF, ELF: Adjust ICF hash computation to account for self relocations.
It turns out that sections in PGO instrumented object files on Windows
contain a large number of relocations pointing to themselves. With
r347429 this can cause many sections to receive the same hash (usually
zero) as a result of a section's hash being xor'ed with itself.

This patch causes the COFF and ELF linkers to avoid this problem
by adding the hash of the relocated section instead of xor'ing it.
On my machine this causes the regressing test case
provided by Mozilla to terminate in 2m41s.

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

llvm-svn: 351898
2019-01-22 23:51:35 +00:00
Chandler Carruth 2946cd7010 Update the file headers across all of the LLVM projects in the monorepo
to reflect the new license.

We understand that people may be surprised that we're moving the header
entirely to discuss the new license. We checked this carefully with the
Foundation's lawyer and we believe this is the correct approach.

Essentially, all code in the project is now made available by the LLVM
project under our new license, so you will see that the license headers
include that license only. Some of our contributors have contributed
code under our old license, and accordingly, we have retained a copy of
our old license notice in the top-level files in each project and
repository.

llvm-svn: 351636
2019-01-19 08:50:56 +00:00
Nico Weber 1f3ab98aca lld-link: Spelling fixes in comments and minor style tweaks
Changes a few things I noticed while reading this code.
- fix a few typos in comments
- remove two `auto` uses where the type wasn't clear to me
- add comment saying that two sequential checks for `if (SparseChunks[SectionNumber] == PendingComdat)` are intentional
- name two parameters

No behavior change.

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

llvm-svn: 351101
2019-01-14 19:05:21 +00:00
Alexandre Ganea 7d9fc98db0 Fix unchecked Error introduced in r350956
llvm-svn: 350968
2019-01-11 20:39:38 +00:00
Alexandre Ganea 27ba55914a [LLD][COFF] Support /ignore:4099. Support /ignore with comma-separated arguments.
Differential Revision: https://reviews.llvm.org/D56392

llvm-svn: 350956
2019-01-11 19:10:01 +00:00
Nico Weber 64fb85c907 lld-link: Add help strings for /manifest, /nodefaultlib, /noentry; tweak manifest help strings
My main motivation is that I can never remember /nodefaultlib and
`lld-link /? | grep no` didn't display it due to it not having a help string.

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

llvm-svn: 350750
2019-01-09 19:18:03 +00:00
Alexandre Ganea 90f4b94da3 [CodeView] More appropriate name and type for a Microsoft precompiled headers parameter. NFC
llvm-svn: 350520
2019-01-07 13:53:16 +00:00
Alexandre Ganea 383be892fc [LLD][COFF] PDB: Parallel sort publics
Saves up to 1.3 sec on large PDBs.
Figures below are for the "Globals Stream Layout" pass:

                            Before	This patch
Large EXE (PDB is ~2 GB)	3330 ms	2022 ms
Large EXE (PDB is ~2 GB)	2680 ms	1608 ms
Large DLL (PDB is ~1 GB)	1455 ms	938 ms
Large DLL (PDB is ~800 MB)	1215 ms	800 ms
Small DLL (PDB is ~200 MB)	224 ms	146 ms

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

llvm-svn: 350452
2019-01-05 01:16:24 +00:00
Alexandre Ganea e6ed8540c5 [LLD][COFF] Fix namespace compilation issue with a upcoming patch. NFC
llvm-svn: 350450
2019-01-05 01:08:10 +00:00
Alexandre Ganea 79d4851678 [LLD][COFF] Fix file/line retrieval when a undefined symbol is to be printed
Differential Revision: https://reviews.llvm.org/D55951

llvm-svn: 350438
2019-01-04 21:49:22 +00:00
Reid Kleckner 0aa260d2c9 [COFF] Set the CPU string for LTO like ELF does
Fixes PR40043

llvm-svn: 349436
2018-12-18 01:59:33 +00:00
Reid Kleckner 53ce05960e [codeview] Align symbol records to save 441MB during linking clang.pdb
In PDBs, symbol records must be aligned to four bytes. However, in the
object file, symbol records may not be aligned. MSVC does not pad out
symbol records to make sure they are aligned. That means the linker has
to do extra work to insert the padding. Currently, LLD calculates the
required space with alignment, and copies each record one at a time
while padding them out to the correct size. It has a fast path that
avoids this copy when the records are already aligned.

This change fixes a bug in that codepath so that the copy is actually
saved, and tweaks LLVM's symbol record emission to align symbol records.
Here's how things compare when doing a plain clang Release+PDB build:
- objs are 0.65% bigger (negligible)
- link is 3.3% faster (negligible)
- saves allocating 441MB
- new LLD high water mark is ~1.05GB

llvm-svn: 349431
2018-12-18 01:14:05 +00:00
Zachary Turner a05ae9db01 Correctly handle skewed streams in drop_front() method.
When calling BinaryStreamArray::drop_front(), if the stream
is skewed it means we must never drop the first bytes of the
stream since offsets which occur in records assume the existence
of those bytes.  So if we want to skip the first record in a
stream, then what we really want to do is just set the begin
pointer to the next record.  But we shouldn't actually remove
those bytes from the underlying view of the data.

llvm-svn: 349066
2018-12-13 18:11:33 +00:00
Zachary Turner a93458b050 [PDB] Move some code around. NFC.
llvm-svn: 348505
2018-12-06 17:49:15 +00:00
Zachary Turner 7c6b19f49b [PDB] Emit S_UDT records in LLD.
Previously these were dropped.  We now understand them sufficiently
well to start emitting them.  From the debugger's perspective, this
now enables us to have debug info about typedefs (both global and
function-locally scoped)

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

llvm-svn: 348306
2018-12-04 21:48:46 +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
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
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
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
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
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
Martin Storsjo 49037d2b3c [COFF] Fix a longstanding typo in a variable name. NFC.
llvm-svn: 346846
2018-11-14 10:26:47 +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
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
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
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
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
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
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 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
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
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
Fangrui Song a535e0543f Eliminate dependency to formatv(). NFC.
llvm-svn: 344212
2018-10-11 00:58:00 +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
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
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
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 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
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
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
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
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
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
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
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
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
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
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
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
Zachary Turner 42e7cc1b0f [PDB] Write FPO Data to the PDB.
llvm-svn: 342003
2018-09-11 22:35:01 +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
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
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
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
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 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
Nico Weber c7bad5767b fix comment typo
llvm-svn: 340742
2018-08-27 14:22:25 +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
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 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
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
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
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
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
Martin Storsjo 21858a9b63 [COFF] Treat .xdata/.pdata$<sym> as implicitly associative to <sym> for MinGW
MinGW configurations don't use associative comdats, as GNU ld doesn't
support that. Instead they produce normal comdats named .text$sym,
.xdata$sym and .pdata$sym.

GNU ld doesn't discard any comdats starting with .xdata or .pdata,
even if --gc-sections is used (while it does discard other unreferenced
comdats), regardless of what symbol name is used after the $ separator.

For LLD, treat any such comdat as implicitly associative to the base
symbol. This requires maintaining a map from symbol name to section
number, but that is only maintained when the MinGW flag has been
enabled.

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

llvm-svn: 339058
2018-08-06 21:26:09 +00:00
Martin Storsjo 214d69975c [COFF] Remove a superfluous warning about aligncomm for non-common symbols
It's not an error if a common symbol (uninitialized data, with alignment
specified via the aligncomm directive) is replaced with a regular
one with initialized data (with alignment specified via the section
chunk).

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

llvm-svn: 339049
2018-08-06 19:49:18 +00:00
Nico Weber 397985db51 lld-link: Simplify LinkerDriver::findDefaultEntry()
No intended behavior change. Not repeating the CRTStartup names makes fixing
PR36523 simpler.

https://reviews.llvm.org/D50253

llvm-svn: 338911
2018-08-03 18:32:44 +00:00
Nico Weber d48d5f086f lld-link: Fix subsystem inference for non-console apps on 32-bit, and fix entry point inference on 32-bit with /nodefaultlib
LinkerDriver::inferSubsystem() used to do Symtab->findUnderscore("WinMain"),
but WinMain is stdcall in 32-bit and is hence is called _WinMain@16. Instead,
Symtab->findMangle(mangle("WinMain")) needs to be called.

But since LinkerDriver::inferSubsystem() and LinkerDriver::findDefaultEntry()
both need to call this, introduce a common helper function for this and call it
from both places. (Also call it for "main" for consistency, even though
findUnderscore() is enough for main since that's __cdecl on 32-bit).

This also exposed a bug for /nodefaultlib entrypoint inference: The code here
called findMangle(Sym) instead of findMangle(mangle(Sym)), again doing the
wrong thing on 32-bit. Fix that too.

While here, make Driver::mangle() a static free function.

https://reviews.llvm.org/D50184

llvm-svn: 338877
2018-08-03 12:00:12 +00:00
Sam Clegg 4c2cbfe68d Set IsUsedInRegularObj in a consistent manor between COFF, ELF and wasm. NFC
Differential Revision: https://reviews.llvm.org/D49147

llvm-svn: 338767
2018-08-02 20:39:19 +00:00
Nico Weber 11f14904d3 lld-link: Remove /msvclto option
This was useful for LTO bringup in lld-link while lld couldn't write PDBs. Now
that it can, this should no longer be needed. Hopefully the flag is obscure
enough and recent enough, that nobody uses it – but if somebody should use it,
they should be able to just stop passing it and things should continue to work.

https://reviews.llvm.org/D50139

llvm-svn: 338615
2018-08-01 19:00:49 +00:00
Rui Ueyama 7f97570e79 Make ICF log output order deterministic.
This patch does the same thing as r338153 for COFF.
Note that this patch affects only the order of log messages.
The output file is already deterministic.

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

llvm-svn: 338406
2018-07-31 18:04:58 +00:00
Martin Storsjo 6c8cbf6db0 [COFF] Handle comdat sections without leader symbols
Discard them unless they have been associated by other means (yet
uimplemented).

According to MS link.exe, such sections are illegal, but MinGW setups
use them in their take on associative comdats.

This avoids leaving references to the bogus SectionChunk* PendingComdat,
which cannot be dereferenced.

This fixes PR38183.

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

llvm-svn: 338064
2018-07-26 20:14:50 +00:00
Rui Ueyama 279621fbf0 [COFF] clean up global resources after completion
Patch by Andrew Kelley.

Previously, running lld::coff::link() twice in the same process would
access stale pointers because of these global variables not being reset.
After this patch, lld::coff::link() can be called any number of times,
just like its ELF and MACH-O counterparts.

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

llvm-svn: 338042
2018-07-26 17:11:24 +00:00
Rui Ueyama 7e95d9e362 Fix error messages for bad symbols.
Previously, the error messages didn't contain symbol name because we
didn't read a symbol name for these error messages.

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

llvm-svn: 337863
2018-07-24 22:52:11 +00:00
Nico Weber bbfe0b79e2 Omit path to lld binary from lld's error, warning, and log output.
lld currently prepends the absolute path to itself to every diagnostic it
emits. This path can be longer than the diagnostic, and makes the actual error
message hard to read.

There isn't a good reason for printing this path: if you want to know which lld
you're running, pass -v to clang – chances are that if you're unsure of this,
you're not only unsure when it errors out. Some people want an indication that
the diagnostic is from the linker though, so instead print just the basename of
the linker's path.

Before:

```
$ out/bin/clang -target x86_64-unknown-linux -x c++ /dev/null -fuse-ld=lld 
/Users/thakis/src/llvm-mono/out/bin/ld.lld: error: cannot open crt1.o: No such file or directory
/Users/thakis/src/llvm-mono/out/bin/ld.lld: error: cannot open crti.o: No such file or directory
/Users/thakis/src/llvm-mono/out/bin/ld.lld: error: cannot open crtbegin.o: No such file or directory
/Users/thakis/src/llvm-mono/out/bin/ld.lld: error: unable to find library -lgcc
/Users/thakis/src/llvm-mono/out/bin/ld.lld: error: unable to find library -lgcc_s
/Users/thakis/src/llvm-mono/out/bin/ld.lld: error: unable to find library -lc
/Users/thakis/src/llvm-mono/out/bin/ld.lld: error: unable to find library -lgcc
/Users/thakis/src/llvm-mono/out/bin/ld.lld: error: unable to find library -lgcc_s
/Users/thakis/src/llvm-mono/out/bin/ld.lld: error: cannot open crtend.o: No such file or directory
/Users/thakis/src/llvm-mono/out/bin/ld.lld: error: cannot open crtn.o: No such file or directory
clang: error: linker command failed with exit code 1 (use -v to see invocation)
```

After:

```
$ out/bin/clang -target x86_64-unknown-linux -x c++ /dev/null -fuse-ld=lld 
ld.lld: error: cannot open crt1.o: No such file or directory
ld.lld: error: cannot open crti.o: No such file or directory
ld.lld: error: cannot open crtbegin.o: No such file or directory
ld.lld: error: unable to find library -lgcc
ld.lld: error: unable to find library -lgcc_s
ld.lld: error: unable to find library -lc
ld.lld: error: unable to find library -lgcc
ld.lld: error: unable to find library -lgcc_s
ld.lld: error: cannot open crtend.o: No such file or directory
ld.lld: error: cannot open crtn.o: No such file or directory
clang: error: linker command failed with exit code 1 (use -v to see invocation)
```

https://reviews.llvm.org/D49189

llvm-svn: 337634
2018-07-20 23:09:12 +00:00
Nico Weber f6be416687 Simplify; no behavior change.
Reviewed as part of https://reviews.llvm.org/D49189

llvm-svn: 337633
2018-07-20 23:06:34 +00:00
Reid Kleckner 276d7167d0 [PDB] Write the command line after response file expansion
Summary: Fixes PR38085

Reviewers: ruiu, zturner

Subscribers: llvm-commits

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

llvm-svn: 337628
2018-07-20 22:34:20 +00:00
Martin Storsjo 98ff9f845d [COFF] Sort .reloc before all other discardable sections
If a binary is stripped, which can remove discardable sections (except
for the .reloc section, which also is marked as discardable as it isn't
loaded at runtime, only read by the loader), the .reloc section should
be first of them, in order not to create gaps in the image.

Previously, binaries with relocations were broken if they were stripped
by GNU binutils strip. Trying to execute such binaries produces an error
about "xx is not a valid win32 application".

This fixes GNU binutils bug 23348.

Prior to SVN r329370 (which didn't intend to have functional changes),
the code for moving discardable sections to the end didn't clearly
express how other discardable sections should be ordered compared to
.reloc, but the change retained the exact same end result as before.

After SVN r329370, the code (and comments) more clearly indicate that
it tries to make the .reloc section the absolutely last one; this patch
changes that.

This matches how GNU binutils ld sorts .reloc compared to dwarf debug
info sections.

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

Signed-off-by: Martin Storsjö <martin@martin.st>
llvm-svn: 337598
2018-07-20 18:43:35 +00:00
Martin Storsjo a55fc71614 [COFF] Write the debug directory and build id to a separate section for MinGW
For dwarf debug info, an executable normally either contains the debug
info, or it is stripped out. To reduce the storage needed (slightly)
for the debug info kept separately from the released, stripped binaries,
one can choose to only copy the debug data from the original executable
(essentially the reverse of the strip operation), producing a file with
only debug info.

When copying the debug data from an executable with GNU objcopy,
the build id and debug directory need to reside in a separate section,
as this will be kept while the rest of the .rdata section is removed.

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

llvm-svn: 337526
2018-07-20 05:44:34 +00:00
Takuto Ikuta d855928ec3 [PDB] Add PDBSourcePath flag to support absolutize source file path
This patch changes relative path for source files in obj files to
absolute path in PDB when linking with added flag.

I will make obj file generated by clang-cl independent from build
directory for chromium build. But I don't want to confuse visual studio
debugger or require additional configuration. To attain this goal, I
added flag to convert relative source file path in obj to absolute path
when emitting PDB.

By removing absolute path from obj files, we can share build cache
between chromium developers even when they are doing debug build.
That will make build time faster.

More context:
https://bugs.chromium.org/p/chromium/issues/detail?id=712796
https://groups.google.com/a/chromium.org/forum/#!topic/chromium-dev/5HXSVX-7fPc

llvm-svn: 337439
2018-07-19 04:56:22 +00:00
Martin Storsjo c35e4bf7eb [COFF] Don't produce base relocs for discardable sections
Dwarf debug info contains some data that contains absolute addresses.
Since these sections are discardable and aren't loaded at runtime,
there's no point in adding base relocations for them.

This makes sure that after stripping out dwarf debug info, there are no
base relocations that point to nonexistent sections.

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

llvm-svn: 337438
2018-07-19 04:25:22 +00:00
Rui Ueyama c93530d873 Look for an entry point function if /nodefaultlib is given.
Summary: Fixes https://bugs.llvm.org/show_bug.cgi?id=38018

Reviewers: thakis

Subscribers: llvm-commits

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

llvm-svn: 337407
2018-07-18 17:48:14 +00:00
Nico Weber c421fe5ef4 lld-link: Add /lib to Options.td so that it appears in lld-link's help output.
https://reviews.llvm.org/D49319

llvm-svn: 337086
2018-07-14 04:07:51 +00:00
Zachary Turner e2ce2a5c86 [coff] remove_dots from /PDBPATH but not /PDBALTPATH.
This more closely matches the behavior of link.exe, and also
simplifies the code slightly.

llvm-svn: 336882
2018-07-12 03:22:39 +00:00
Zachary Turner bf9abccacd [coff] Remove dots in path pointing to PDB file.
Some Microsoft tools (e.g. new versions of WPA) fail when the
COFF Debug Directory contains a path to the PDB that contains
dots, such as D:\foo\./bar.pdb.  Remove dots before writing this
path.

This fixes pr38126.

llvm-svn: 336873
2018-07-12 00:44:15 +00:00
Martin Storsjo 474be005db [COFF] Store import symbol pointers as pointers to the base class
Future symbol insertions can potentially change the type of these
symbols - keep pointers to the base class to reflect this, and
use dynamic casts to inspect them before using as the subclass
type.

This fixes crashes that were possible before, by touching these
symbols that now are populated as e.g. a DefinedRegular, via
the old pointers with DefinedImportThunk type.

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

llvm-svn: 336652
2018-07-10 10:40:11 +00:00
Filipe Cabecinhas 17f3f7ae6b [lld] Address post-commit review of r335848
llvm-svn: 335985
2018-06-29 15:34:36 +00:00
Martin Storsjo 3a7905b2aa [COFF] Add an LLD specific option -debug:symbtab
With this set, we retain the symbol table, but skip the actual debug
information.

This is meant to be used by the MinGW frontend.

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

llvm-svn: 335946
2018-06-29 06:08:25 +00:00
Bob Haarman c103156c60 lld-link: align sections to 16 bytes if referenced from the gfids table
Summary:
Control flow guard works best when targets it checks are 16-byte aligned.
Microsoft's link.exe helps ensure this by aligning code from sections
that are referenced from the gfids table to 16 bytes when linking with
-guard:cf, even if the original section specifies a smaller alignment.
This change implements that behavior in lld-link.

See https://crbug.com/857012 for more details.

Reviewers: ruiu, hans, thakis, zturner

Subscribers: llvm-commits

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

llvm-svn: 335864
2018-06-28 15:22:40 +00:00
Filipe Cabecinhas 02c70438f8 Fix warning on MSVC by using size_t arithmetic instead of casting after the fact. NFC
llvm-svn: 335848
2018-06-28 12:38:43 +00:00
Reid Kleckner 3408568392 [COFF] Fix /wholearchive: to do libpath search again
Fixes https://crbug.com/852882

llvm-svn: 334761
2018-06-14 19:56:03 +00:00
Rui Ueyama 4eed6cc433 Fix /WholeArchive bug.
`lld-link foo.lib /wholearchive:foo.lib` should work the same way as
`lld-link /wholearchive:foo.lib foo.lib`. Previously, /wholearchive in
the former case was ignored.

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

llvm-svn: 334552
2018-06-12 21:47:31 +00:00
Shoaib Meenai 02c4344262 [COFF] Fix crash when emitting symbol tables with GC
When running with linker GC (`-opt:ref`), defined imported symbols that
are referenced but then dropped by GC end up with their `Location`
member being nullptr, which means `getChunk()` returns nullptr for them
and attempting to call `getChunk()->getOutputSection()` causes a crash
from the nullptr dereference. Check for `getChunk()` being nullptr and
bail out early to avoid the crash.

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

llvm-svn: 334548
2018-06-12 21:19:33 +00:00
Zachary Turner 08426e1f9f Refactor ExecuteAndWait to take StringRefs.
This simplifies some code which had StringRefs to begin with, and
makes other code more complicated which had const char* to begin
with.

In the end, I think this makes for a more idiomatic and platform
agnostic API.  Not all platforms launch process with null terminated
c-string arrays for the environment pointer and argv, but the api
was designed that way because it allowed easy pass-through for
posix-based platforms.  There's a little additional overhead now
since on posix based platforms we'll be takign StringRefs which
were constructed from null terminated strings and then copying
them to null terminate them again, but from a readability and
usability standpoint of the API user, I think this API signature
is strictly better.

llvm-svn: 334518
2018-06-12 17:43:52 +00:00
Bob Haarman 30913ac39d [COFF] report file containing unsupported relocation
Summary:
When reporting an unsupported relocation type, let's also report the
file we encountered it in to aid diagnosis.

Reviewers: ruiu, rnk

Reviewed By: rnk

Subscribers: llvm-commits

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

llvm-svn: 334154
2018-06-07 00:50:03 +00:00
Nico Weber d657c25649 lld-link: Implement /INTEGRITYCHECK flag
/INTEGRITYCHECK has the effect of setting
IMAGE_DLLCHARACTERISTICS_FORCE_INTEGRITY. Fixes PR31066.
https://reviews.llvm.org/D47472

llvm-svn: 333652
2018-05-31 13:43:02 +00:00
Shoaib Meenai 663518d61a [COFF] Unify output section code. NFC
Peter Collingbourne suggested moving the switch to the top of the
function, so that all the code that cares about the output section for a
symbol is in the same place.

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

llvm-svn: 333472
2018-05-29 22:49:56 +00:00
Shoaib Meenai 4e51833611 [COFF] Simplify symbol table output section computation
Rather than using a loop to compare symbol RVAs to the starting RVAs of
sections to determine which section a symbol belongs to, just get the
output section of a symbol directly via its chunk, and bail if the
symbol doesn't have an output section, which avoids having to hardcode
logic for handling dead symbols, CodeView symbols, etc. This was
suggested by Reid Kleckner; thank you.

This also fixes writing out symbol tables in the presence of RVA table
input sections (e.g. .sxdata and .gfids). Such sections aren't written
to the output file directly, so their RVA is 0, and the loop would thus
fail to find an output section for them, resulting in a segfault. Extend
some existing tests to cover this case.

Fixes PR37584.

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

llvm-svn: 333450
2018-05-29 19:07:47 +00:00
Sam Clegg 3ad27e92bc Code cleanup in preparation for adding LTO for wasm. NFC.
- Move some common code into Common/rrorHandler.cpp and
  Common/Strings.h.
- Don't use `fatal` when incompatible bitcode files are
  encountered.
- Rename NameRef variable to just Name

See D47162

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

llvm-svn: 333021
2018-05-22 20:20:25 +00:00
Zachary Turner c762666e87 Resubmit [pdb] Change /DEBUG:GHASH to emit 8 byte hashes."
This fixes the remaining failing tests, so resubmitting with no
functional change.

llvm-svn: 332676
2018-05-17 22:55:15 +00:00
Zachary Turner 1de9fce151 Revert "[pdb] Change /DEBUG:GHASH to emit 8 byte hashes."
A few tests haven't been properly updated, so reverting while
I have time to investigate proper fixes.

llvm-svn: 332672
2018-05-17 21:49:25 +00:00
Zachary Turner 3c4c8a0937 [pdb] Change /DEBUG:GHASH to emit 8 byte hashes.
Previously we emitted 20-byte SHA1 hashes.  This is overkill
for identifying debug info records, and has the negative side
effect of making object files bigger and links slower.  By
using only the last 8 bytes of a SHA1, we get smaller object
files and ~10% faster links.

This modifies the format of the .debug$H section by adding a new
value for the hash algorithm field, so that the linker will still
work when its object files have an old format.

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

llvm-svn: 332669
2018-05-17 21:22:48 +00:00
Zachary Turner c8dd6ccc8a [COFF] Add /Brepro and /TIMESTAMP options.
Previously we would always write a hash of the binary into the
PE file, for reproducible builds.  This breaks AppCompat, which
is a feature of Windows that relies on the timestamp in the PE
header being set to a real value (or at the very least, a value
that satisfies certain properties).

To address this, we put the old behavior of writing the hash
behind the /Brepro flag, which mimics MSVC linker behavior.  We
also match MSVC default behavior, which is to write an actual
timestamp to the PE header.  Finally, we add the /TIMESTAMP
option (an lld extension) so that the user can specify the exact
value to be used in case he/she manually constructs a value which
is both reproducible and satisfies AppCompat.

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

llvm-svn: 332613
2018-05-17 15:11:01 +00:00
Peter Collingbourne 62f7af712c COFF: Allow ICFing sections with different alignments.
The combined section gets the maximum alignment of all sections.

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

llvm-svn: 332273
2018-05-14 18:36:51 +00:00
Peter Collingbourne 107f55005b COFF: ICF a section and its associated sections as a unit.
This is needed to avoid merging two functions with identical
instructions but different xdata. It also reduces binary size by
deduplicating identical pdata sections.

Fixes PR35337.

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

llvm-svn: 332169
2018-05-12 02:12:40 +00:00
Peter Collingbourne d25dfe9bda COFF: Add a flag for disabling string tail merging.
We discovered (crbug.com/838449#c24) that string tail merging can
negatively affect compressed binary size, so provide a flag to turn
it off for users who care more about compressed size than uncompressed
size.

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

llvm-svn: 332149
2018-05-11 22:21:36 +00:00
Peter Collingbourne b6c5a3045b COFF: Allow ICF on vtable sections.
Differential Revision: https://reviews.llvm.org/D46734

llvm-svn: 332059
2018-05-10 23:31:58 +00:00
Peter Collingbourne e28faed768 COFF: Don't create unnecessary thunks.
A thunk is only needed if a relocation points to the undecorated
import name.

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

llvm-svn: 332019
2018-05-10 19:01:28 +00:00
Nico Weber cac2b3349e lld-link: Add --color-diagnostics(={always,never,auto})?, --no-color-diagnostics flags.
This is most useful when using lld-link on a non-Win host (but it might become
useful on Windows too if lld also grows a fansi-escape-codes flag).

Also make the help for --color-diagnostic mention the valid values in ELF and
wasm, and print the flag name with two dashes in diags, since the one-dash form
is seen as a list of many one-letter flags in some contexts.

https://reviews.llvm.org/D46693

llvm-svn: 332012
2018-05-10 18:19:02 +00:00
Martin Storsjo 5c84d442f5 [COFF] Fix dangling StringRefs from SVN 331900
llvm-svn: 331912
2018-05-09 19:07:10 +00:00
Martin Storsjo 0ca06f7950 [COFF] Allow specifying export forwarding in a def file
Previously this was only supported when specified on the command line
or in directives.

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

llvm-svn: 331900
2018-05-09 18:19:41 +00:00
Martin Storsjo 97379ff6b2 [COFF] Improve correctness of def parsing for GNU features
The operator == used for exporting a function with a different
name in the DLL compared to the name in the import library
(which is useful for adding linker level aliases for function
in the import library) is a feature distinct and different from
the operator = used for exporting a function with a different
name (both in import library and DLL) than in the implementation
producing the DLL.

When creating an import library using dlltool, from a def file that
contains forwards (Func = OtherDll.Func), this shouldn't affect the
produced import library, which should still behave just as if it
was a normal exported function.

This clears a lot of confusion and subtle misunderstandings, and
avoids a parameter that was used to avoid creating weak aliases
when invoked from lld. (This parameter was added previously due to
the existing conflation of the two features.)

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

llvm-svn: 331860
2018-05-09 09:22:03 +00:00
Martin Storsjo cc80776eff [COFF] Implement the remaining ARM64 relocations
Now only IMAGE_REL_ARM64_ABSOLUTE and IMAGE_REL_ARM64_TOKEN
are unhandled.

Also add range checks for the existing BRANCH26 relocation.

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

llvm-svn: 331505
2018-05-04 06:06:27 +00:00
Bob Haarman 947647d0c1 [COFF] more informative "broken object file" diagnostics
Summary:
When a symbol refers to a special section or a section that doesn't
exist, lld would fatal with "broken object file". This change gives a
different message for each scenario, and includes the name of the
file, name of the symbol, and the section being referred to.

Reviewers: pcc, ruiu

Reviewed By: ruiu

Subscribers: llvm-commits

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

llvm-svn: 330883
2018-04-25 23:33:19 +00:00
Hans Wennborg 03ca8f4fd0 [COFF] Don't set the tsaware bit on DLLs
It doesn't apply to DLLs, and link.exe doesn't set it.

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

llvm-svn: 330868
2018-04-25 20:32:00 +00:00
Bob Haarman 8832f88996 [COFF] create MemoryBuffers without requiring NUL terminators
Summary:
In a number of places in the COFF linker, we were calling
MemoryBuffer::getFile() with default parameters. This causes LLVM to
NUL-terminate the buffers, which can prevent them from being memory
mapped. Since we operate on binary and do not use NUL as an indicator
of the end of the file content, this change causes us to not require
the NUL terminator anymore.

Reviewers: ruiu, pcc

Reviewed By: ruiu

Subscribers: llvm-commits

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

llvm-svn: 330786
2018-04-24 23:16:39 +00:00
Zachary Turner d6fce2e9db [COFF] Alias /DEBUG:FULL to /DEBUG
With MSVC linker, /DEBUG is an alias of /DEBUG:FASTLINK, and if
you don't want /DEBUG:FASTLINK you have to explicitly specify
/DEBUG:FULL.

LLD doesn't support /DEBUG:FASTLINK, and so our standard /DEBUG
option is what MSVC calls /DEBUG:FULL.  To provide command line
compatibility with MSVC, we should also support /DEBUG:FULL, and
since it's the same as what LLD already does for /DEBUG, just
alias it.

llvm-svn: 330647
2018-04-23 20:54:08 +00:00
Bob Haarman 8679a7ecea Fix nullptr passed to memcpy in lld/COFF/Chunks.cpp
Summary:
ubsan found that we sometimes pass nullptr to memcpy in
SectionChunk::writeTo(). This change adds a check that avoids that.

Reviewers: ruiu

Reviewed By: ruiu

Subscribers: llvm-commits

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

llvm-svn: 330490
2018-04-20 22:16:09 +00:00
Peter Collingbourne c8f5c05d41 COFF: Document /pdbaltpath.
llvm-svn: 330488
2018-04-20 22:11:28 +00:00
Peter Collingbourne 3d636edc56 COFF: Merge .xdata into .rdata by default.
Differential Revision: https://reviews.llvm.org/D45804

llvm-svn: 330484
2018-04-20 21:32:37 +00:00
Peter Collingbourne 326f419335 COFF: Merge .bss into .data by default.
Differential Revision: https://reviews.llvm.org/D45803

llvm-svn: 330483
2018-04-20 21:30:36 +00:00
Peter Collingbourne 71c7de5b77 COFF: Preserve section type when processing /section flag.
It turns out that we were dropping this before.

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

llvm-svn: 330481
2018-04-20 21:23:16 +00:00
Peter Collingbourne 381b3d8aa3 COFF: Use (name, output characteristics) as a key when grouping input sections into output sections.
This is what link.exe does and lets us avoid needing to worry about
merging output characteristics while adding input sections to output
sections.

With this change we can't process /merge in the same way as before
because sections with different output characteristics can still
be merged into one another. So this change moves the processing of
/merge to just before we assign addresses. In the case where there
are multiple output sections with the same name, link.exe only merges
the first section with the source name into the first section with
the target name, and we do the same.

At the same time I also implemented transitive merging (which means
that /merge:.c=.b /merge:.b=.a merges both .c and .b into .a).

This isn't quite enough though because link.exe has a special case for
.CRT in 32-bit mode: it processes sections whose output characteristics
are DATA | R | W as though the output characteristics were DATA | R
(so that they get merged into things like constructor lists in the
expected way). Chromium has a few such sections, and it turns out
that those sections were causing the problem that resulted in r318699
(merge .xdata into .rdata) being reverted: because of the previous
permission merging semantics, the .CRT sections were causing the entire
.rdata section to become writable, which caused the SEH runtime to
crash because it apparently requires .xdata to be read-only. This
change also implements the same special case.

This should unblock being able to merge .xdata into .rdata by default,
as well as .bss into .data, both of which will be done in followups.

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

llvm-svn: 330479
2018-04-20 21:10:33 +00:00
Zachary Turner 041eb6fef6 [LLD/PDB] Remove improper assert.
It's possible to have an empty object file, for example if you
just compile an empty .c file.  This file won't have any sections
so asserting that a file has chunks is definitely wrong.

llvm-svn: 330461
2018-04-20 18:36:51 +00:00
Zachary Turner 194be871b9 [LLD/PDB] Emit first section contribution for DBI Module Descriptor.
Part of the DBI stream is a list of variable length structures
describing each module that contributes to the final executable.

One member of this structure is a section contribution entry that
describes the first section contribution in the output file for
the given module.

We have been leaving this structure unpopulated until now, so with
this patch it is now filled out correctly.

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

llvm-svn: 330457
2018-04-20 18:00:46 +00:00
Peter Collingbourne be084eca5b COFF: Remove OutputSection::getPermissions() and getCharacteristics().
All callers can just access the header directly.

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

llvm-svn: 330367
2018-04-19 21:48:37 +00:00
Peter Collingbourne fa322abee9 COFF: Rename Chunk::getPermissions to getOutputCharacteristics.
In an upcoming change I will need to make a distinction between section
type (code, data, bss) and permissions. The term that I use for both
of these things is "output characteristics".

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

llvm-svn: 330361
2018-04-19 20:03:24 +00:00
Reid Kleckner 8f1a28f190 [COFF] Mark images with no exception handlers for /safeseh
Summary:
DLLs and executables with no exception handlers need to be marked with
IMAGE_DLL_CHARACTERISTICS_NO_SEH, even if they have a load config.

Discovered here when building Chromium with LLD on Windows:
https://crbug.com/833951

Reviewers: ruiu, mstorsjo

Subscribers: llvm-commits

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

llvm-svn: 330300
2018-04-18 22:37:10 +00:00
Peter Collingbourne 3108802f16 COFF: Friendlier undefined symbol errors.
Summary:
This change does three things:
- Try to find the file and line number of an undefined symbol
  reference by reading codeview debug info.
- Try to find the name of the function or global variable with the
  undefined symbol reference by searching the object file's symbol
  table.
- Prints the information in the same style as the ELF linker.

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

llvm-svn: 330235
2018-04-17 23:32:33 +00:00
Peter Collingbourne 66f1c9a858 Reland r330223, "COFF: Merge .idata, .didat and .edata into .rdata by default.", which was reverted in r330228.
In this reland I removed an unnecessary use of /debug in the test
delayimports32.test and used the /pdbaltpath flag in the test
pdb-publics-import.test, both of which avoid embedding absolute PDB
paths in executables which could affect later RVAs.

Original commit message:
> COFF: Merge .idata, .didat and .edata into .rdata by default.
>
> This saves a little space and matches what link.exe does.
>
> Tested using the chromium Windows trybots:
> https://chromium-review.googlesource.com/c/chromium/src/+/1014784

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

llvm-svn: 330233
2018-04-17 23:28:52 +00:00
Peter Collingbourne 94aa62e48a COFF: Implement /pdbaltpath flag.
I needed to revert r330223 because we were embedding an absolute PDB
path in the .rdata section, which ended up being laid out before the
.idata section and affecting its RVAs. This flag will let us control
the embedded path.

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

llvm-svn: 330232
2018-04-17 23:28:38 +00:00
Peter Collingbourne 1254f3e77c Revert r330223, "COFF: Merge .idata, .didat and .edata into .rdata by default."
Seems to have uncovered some sort of non-determinism on the bots.

llvm-svn: 330228
2018-04-17 22:16:39 +00:00
Peter Collingbourne 09e0e2e656 COFF: Merge .idata, .didat and .edata into .rdata by default.
This saves a little space and matches what link.exe does.

Tested using the chromium Windows trybots:
https://chromium-review.googlesource.com/c/chromium/src/+/1014784

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

llvm-svn: 330223
2018-04-17 21:44:31 +00:00
Peter Collingbourne 2f6d00612d COFF: Make SectionChunk::Relocs field an ArrayRef. NFCI.
Differential Revision: https://reviews.llvm.org/D45714

llvm-svn: 330172
2018-04-17 01:54:34 +00:00
Zachary Turner d8d97de514 [PDB] Correctly use the target machine when writing DBI stream.
Using Config->is64() will treat ARM64 as Amd64, which is incorrect.
Furthermore, there are more esoteric architectures that could
theoretically be encountered.  Just set it directly to the machine
type, which we already know anyway.

llvm-svn: 330157
2018-04-16 20:42:06 +00:00
Zachary Turner e3fe669855 Resubmit "Fix some incorrect fields in our generated PDBs."
This fixes the failing tests.  They simply hadn't been updated
to match the new output resulting from this patch.

llvm-svn: 330145
2018-04-16 18:17:13 +00:00
Zachary Turner 52c80e3860 Revert "Fix some incorrect fields in our generated PDBs."
There are a couple of failing tests which slipped under my radar
so I'm reverting this while I attempt to fix.

llvm-svn: 330133
2018-04-16 16:55:41 +00:00
Zachary Turner 1b06cc7817 Fix some incorrect fields in our generated PDBs.
Most of these are pretty trivial and obvious. Setting the toolchain
version to 14.11 is perhaps a little questionable, but we've been bitten
in the past where one of our version fields sidn't match MSVC's, and I
definitely don't want to go through that diagnosis again as it was
pretty time consuming and hard to track down.

I found all of these by using llvm-pdbutil export to dump the dbi and
pdb streams to a file, then using fc followed by llvm-pdbutil explain to
explain the mismatched bytes.

There are still some more, these are just the low hanging fruit.

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

llvm-svn: 330130
2018-04-16 16:27:49 +00:00
Sam Clegg d11b6edfc8 Remove references to old SymbolBody class
Differential Revision: https://reviews.llvm.org/D45400

llvm-svn: 329846
2018-04-11 19:52:53 +00:00
Nico Weber fb64730005 s/LLVM_ON_WIN32/_WIN32/, lld
LLVM_ON_WIN32 is set exactly with MSVC and MinGW (but not Cygwin) in             
HandleLLVMOptions.cmake, which is where _WIN32 defined too.  Just use the        
default macro instead of a reinvented one.                                       
                                                                                 
See thread "Replacing LLVM_ON_WIN32 with just _WIN32" on llvm-dev and cfe-dev.   
No intended behavior change. 

llvm-svn: 329696
2018-04-10 13:15:21 +00:00
Peter Collingbourne bdfa127c3e COFF: Remove dead code. NFCI.
Differential Revision: https://reviews.llvm.org/D45423

llvm-svn: 329609
2018-04-09 19:46:00 +00:00
Peter Collingbourne 4902508934 COFF: Process /merge flag as we create output sections.
With this we can merge builtin sections.

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

llvm-svn: 329471
2018-04-07 00:46:55 +00:00
Peter Collingbourne f2c0f39b91 COFF: Create output sections early. NFCI.
With this, all output sections are created in one place. This will make
it simpler to implement merging of builtin sections.

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

llvm-svn: 329370
2018-04-06 03:25:49 +00:00
Peter Collingbourne 05f0bae318 COFF: Sort non-discardable sections at the same time as other sections. NFC.
This makes the sort order a little clearer.

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

llvm-svn: 329227
2018-04-04 20:30:37 +00:00
Hans Wennborg 9a9fc78744 COFF: Layout sections in the same order as link.exe
One place where this seems to matter is to make sure the .rsrc section comes
after .text. The Win32 UpdateResource() function can change the contents of
.rsrc. It will move the sections that come after, but if .text gets moved, the
entry point header will not get updated and the executable breaks. This was
found by a test in Chromium.

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

llvm-svn: 329221
2018-04-04 19:15:55 +00:00
Nico Weber a764379458 [lld-link] Add comment explaining that /FIXED behavior is correct despite contradicting MSDN.
Also add a test for /FIXED.
https://reviews.llvm.org/D45087

llvm-svn: 328879
2018-03-30 17:17:04 +00:00
Nico Weber 0945ad6643 [lld-link] Let /PROFILE imply /OPT:REF /OPT:NOICF /INCREMENTAL:NO /FIXED:NO
/FIXED:NO is always the default, so that part needs no work.

Also test the interaction of /ORDER: with /INCREMENTAL.

https://reviews.llvm.org/D45091

llvm-svn: 328877
2018-03-30 17:14:50 +00:00
Bob Haarman 3ddeb33e00 [lld] fix data race in ICF.cpp
Summary: Fixes PR36823.

Reviewers: ruiu, pcc, rnk

Reviewed By: ruiu

Subscribers: llvm-commits

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

llvm-svn: 328610
2018-03-27 06:08:35 +00:00
David Blaikie ddcd856147 Fix for header rename in LLVM
llvm-svn: 328594
2018-03-26 23:43:29 +00:00
Zachary Turner f228276262 [PDB] Resubmit "Support embedding natvis files in PDBs."
This was reverted several times due to what ultimately turned out
to be incompatibilities in our serialized hash table format.

Several changes went in prior to this to fix those issues since
they were more fundamental and independent of supporting injected
sources, so now that those are fixed this change should hopefully
pass.

llvm-svn: 328363
2018-03-23 19:57:25 +00:00
Zachary Turner a6fb536e5b [PDB] Make our PDBs look more like MS PDBs.
When investigating bugs in PDB generation, the first step is
often to do the same link with link.exe and then compare PDBs.

But comparing PDBs is hard because two completely different byte
sequences can both be correct, so it hampers the investigation when
you also have to spend time figuring out not just which bytes are
different, but also if the difference is meaningful.

This patch fixes a couple of cases related to string table emission,
hash table emission, and the order in which we emit strings that
makes more of our bytes the same as the bytes generated by MS PDBs.

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

llvm-svn: 328348
2018-03-23 18:43:39 +00:00
Zachary Turner fced530650 Revert "Resubmit "Support embedding natvis files in PDBs.""
This is still failing on a different bot this time due to some
issue related to hashing absolute paths.  Reverting until I can
figure it out.

llvm-svn: 328014
2018-03-20 18:37:03 +00:00
Zachary Turner 132d7a134f Resubmit "Support embedding natvis files in PDBs."
The issue causing this to fail in certain configurations
should be fixed.

It was due to the fact that DIA apparently expects there to be
a null string at ID 1 in the string table.  I'm not sure why this
is important but it seems to make a difference, so set it.

llvm-svn: 328002
2018-03-20 17:06:39 +00:00
Zachary Turner a21558897b Revert "Support embedding natvis files in PDBs."
This is causing a test failure on a certain bot, so I'm removing
this temporarily until we can figure out the source of the error.

llvm-svn: 327903
2018-03-19 20:41:59 +00:00
Zachary Turner de53aaf132 Support embedding natvis files in PDBs.
Natvis is a debug language supported by Visual Studio for
specifying custom visualizers.  The /NATVIS option is an
undocumented link.exe flag which will take a .natvis file
and "inject" it into the PDB.  This way, you can ship the
debug visualizers for a program along with the PDB, which
is very useful for postmortem debugging.

This is implemented by adding a new "named stream" to the
PDB with a special name of /src/files/<natvis file name>
and simply copying the contents of the xml into this file.

Additionally, we need to emit a single stream named
/src/headerblock which contains a hash table of embedded
files to records describing them.

This patch adds this functionality, including the /NATVIS
option to lld-link.

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

llvm-svn: 327895
2018-03-19 19:53:51 +00:00
Shoaib Meenai 290f26fefd [COFF] Clarify comment. NFC
Reid pointed out the string table for supporting long section names is a
BFD extension and the comments should reflect that. Explicitly spell out
link.exe's and binutil's behavior around section names and the rationale
for LLD's behavior.

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

llvm-svn: 327736
2018-03-16 20:20:01 +00:00
Peter Collingbourne f1a11f87a0 COFF: Implement string tail merging.
In COFF, duplicate string literals are merged by placing them in a
comdat whose leader symbol name contains a specific prefix followed
by the hash and partial contents of the string literal. This gives
us an easy way to identify sections containing string literals in
the linker: check for leader symbol names with the given prefix.

Any sections that are identified in this way as containing string
literals may be tail merged. We do so using the StringTableBuilder
class, which is also used to tail merge string literals in the ELF
linker. Tail merging is enabled only if ICF is enabled, as this
provides a signal as to whether the user cares about binary size.

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

llvm-svn: 327668
2018-03-15 21:14:02 +00:00
Peter Collingbourne 435b099115 COFF: Move assignment of section RVAs to assignAddresses(). NFCI.
This makes the design a little more similar to the ELF linker and
should allow for features such as ARM range extension thunks to be
implemented more easily.

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

llvm-svn: 327667
2018-03-15 21:13:46 +00:00
Reid Kleckner 8364901f24 [COFF] Enable per-function and data sections in LTO
Summary: This allows post-LTO symbol reordering and ICF.

Reviewers: inglorion

Subscribers: mehdi_amini, llvm-commits

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

llvm-svn: 327563
2018-03-14 20:25:41 +00:00
Martin Storsjo 5351891b55 [COFF] Add support for the GNU ld flag --kill-at
GNU ld has got a number of different flags for adjusting how to
behave around stdcall functions. The --kill-at flag strips the
trailing sdcall suffix from exported functions (which otherwise
is included by default in MinGW setups).

This also strips it from the corresponding import library though.
That makes it hard to link to such an import library from code
that calls the functions - but this matches what GNU ld does with
this flag. Therefore, this flag is probably not sensibly used
together with import libraries, but probably mostly when creating
some sort of plugin, or if creating the import library separately
with dlltool.

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

llvm-svn: 327561
2018-03-14 20:17:16 +00:00
Nico Weber f06ae4f3b4 Make lld-link shout at me less.
This makes the output of some flag names in warning messages consistent with
the output of /? and the output of flags in most other diagnostics.

https://reviews.llvm.org/D44307

llvm-svn: 327261
2018-03-12 12:45:40 +00:00
Nico Weber 11a6db3027 [lld-link] For suppressible warnings, print the warning number.
The warning can be suppressed by passing the number to /ignore:.
https://reviews.llvm.org/D44297

llvm-svn: 327257
2018-03-12 12:04:17 +00:00
Nico Weber 0771c604c9 [lld-link] Add support for /ignore:4037.
Fixes PR36657.
https://reviews.llvm.org/D44286

llvm-svn: 327124
2018-03-09 12:41:04 +00:00
Zachary Turner b575f46b6d Resubmit "Write a hash of the executable into the PE timestamp fields."
This fixes the broken tests that were causing failures.  The tests
before were verifying that the time stamp was 0, but now that we
are actually writing a timestamp, I just removed the match against
the timestamp value.

llvm-svn: 327049
2018-03-08 19:33:47 +00:00
Hans Wennborg aee5881a85 [COFF] Make the DOS stub a real DOS program
It only adds a few bytes and is nice for backward compatibility.

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

llvm-svn: 327001
2018-03-08 14:27:28 +00:00
Zachary Turner 0b4af0434b Revert "Write a hash of the executable into the PE timestamp fields."
This is breaking a couple of tests, so I'm reverting temporarily
until I can get everything resolved properly.

llvm-svn: 326943
2018-03-07 21:22:10 +00:00
Zachary Turner 69f3347b56 Write a hash of the executable into the PE timestamp fields.
Windows tools treats the timestamp fields as sort of a build id,
using it to archive executables on a symbol server, as well as
for matching executables to PDBs.  We were writing 0 for these
fields, which would cause symbol servers to break as they are
indexed in the symbol server based on this value.

Although the field is called timestamp, it can really be any
value that is unique per build, so to support reproducible builds
we use a hash of the executable here.

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

llvm-svn: 326920
2018-03-07 18:13:41 +00:00
Rui Ueyama e1847da924 Rename Indent{1,2} -> Indent{8,16}.
llvm-svn: 326912
2018-03-07 17:15:15 +00:00
Rui Ueyama 9e0171fbb6 Do not create temporary strings just to print out spaces. NFC.
llvm-svn: 326841
2018-03-06 22:48:46 +00:00
Rui Ueyama cdd5fb5087 Report an error if you try to link against .dll instead of .lib.
It is a usage error to feed a .dll file instead of a .dll to COFF linker.
Previously, lld failed with a mysterious error message. Now we reject
it at the driver.

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

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

llvm-svn: 326507
2018-03-01 23:11:30 +00:00
Zachary Turner c6a75a69f1 [PDB] Defer writing the build id until the rest of the PDB is written.
For now this is NFC, but this small refactor opens the door to
letting us embed a hash of the PDB in the build id field of the
PDB.

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

llvm-svn: 326453
2018-03-01 18:00:29 +00:00
Rui Ueyama 67d6908f59 Use DenseMap::lookup() instead of find() and a hand-written null check.
llvm-svn: 326382
2018-02-28 23:03:06 +00:00
Zachary Turner 3868cfd503 Fix use after free in PDB linker.
When merging in types from a type server PDB, we would use a
pointer into the type server PDB's mapped file buffer directly
to avoid copying data.  However, we would close the type server
PDB after we finished merging in its types, which would unmap
all of its memory.  This would lead to a use after free.

We fix this by making a strong reference in the PDBLinker class
to all referenced type server PDBs, thereby making it safe to
hold pointers into its memory mapped contents.

This fixes llvm.org/pr36455

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

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

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

llvm-svn: 326339
2018-02-28 17:38:19 +00:00
Sam Clegg f187c4d2e5 Consistent use of header file for ICF and MarkLive
Previously wasm used a separate header to declare markLive
and ELF used to declare ICF.  This change makes each backend
consistently declare these in their own headers.

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

llvm-svn: 325631
2018-02-20 22:09:59 +00:00
Sam Clegg 3141ddc58d Consistent (non) use of empty lines in include blocks
The profailing style in lld seem to be to not include such empty lines.
Clang-tidy/clang-format seem to handle this just fine.

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

llvm-svn: 325629
2018-02-20 21:53:18 +00:00
Teresa Johnson 2c2ed3cf03 [lld/LTO] Remove unused Path parameter to AddBufferFn
Summary:
With D43396, no clients use the Path parameter anymore.
This is the lld side fix with D43400.

Depends on D43396 and D43400.

Reviewers: pcc

Subscribers: emaste, inglorion, arichardson, llvm-commits

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

llvm-svn: 325620
2018-02-20 20:21:59 +00:00
Rui Ueyama c6c66225a3 Simplify.
llvm-svn: 325453
2018-02-17 23:37:26 +00:00
Rui Ueyama b3107476a4 Remove an unused accessor and simplify the logic a bit. NFC.
llvm-svn: 325445
2018-02-17 20:41:38 +00:00
Rui Ueyama 03c7f3a2d0 Factor out common code from applySecRel functions.
Differential Revision: https://reviews.llvm.org/D43412

llvm-svn: 325444
2018-02-17 20:28:15 +00:00
Martin Storsjo ef4f78bbf2 [COFF] Add support for ARM64 secrel relocations for add/load instructions
Differential Revision: https://reviews.llvm.org/D43287

llvm-svn: 325396
2018-02-16 22:02:38 +00:00
Rui Ueyama 38781a59f6 Revert r325158: Convert an assert to a static_assert. NFC.
This reverts commit r325158 because it broke GCC builds.

llvm-svn: 325183
2018-02-14 22:43:43 +00:00
Sam Clegg ab31b7759d Convert an assert to a static_assert. NFC.
Differential Revision: https://reviews.llvm.org/D43305

llvm-svn: 325158
2018-02-14 19:28:46 +00:00
Reid Kleckner fd52096259 [LLD] Implement /guard:[no]longjmp
Summary:
This protects calls to longjmp from transferring control to arbitrary
program points. Instead, longjmp calls are limited to the set of
registered setjmp return addresses.

This also implements /guard:nolongjmp to allow users to link in object
files that call setjmp that weren't compiled with /guard:cf. In this
case, the linker will approximate the set of address taken functions,
but it will leave longjmp unprotected.

I used the following program to test, compiling it with different -guard
flags:
  $ cl -c t.c -guard:cf
  $ lld-link t.obj -guard:cf

  #include <setjmp.h>
  #include <stdio.h>
  jmp_buf buf;
  void g() {
    printf("before longjmp\n");
    fflush(stdout);
    longjmp(buf, 1);
  }
  void f() {
    if (setjmp(buf)) {
      printf("setjmp returned non-zero\n");
      return;
    }
    g();
  }
  int main() {
    f();
    printf("hello world\n");
  }

In particular, the program aborts when the code is compiled *without*
-guard:cf and linked with -guard:cf. That indicates that longjmps are
protected.

Reviewers: ruiu, inglorion, amccarth

Subscribers: llvm-commits

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

llvm-svn: 325047
2018-02-13 20:32:53 +00:00
Rui Ueyama af7242a385 Use reinterpret_cast<> instead of C-style cast. NFC.
It is currently interpreted as reinterpret_cast<>. Make it explicit.

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

llvm-svn: 325033
2018-02-13 18:11:42 +00:00
Sam Clegg 38f52b2eb8 Check that Symbol types are trivially destructible
This adds an extra level of static safety to our use of placement
new to allocate Symbol types.  It prevents the accidental addition
on a non-trivially-destructible member that could allocate and
leak memory.

From the spec: Storage occupied by trivially destructible objects
may be reused without calling the destructor.

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

llvm-svn: 325025
2018-02-13 17:32:31 +00:00
Benjamin Kramer 6400a97418 [COFF] LTO does not require a disassembler.
llvm-svn: 324355
2018-02-06 15:42:41 +00:00
Reid Kleckner af2f7da74c [COFF] Add minimal support for /guard:cf
Summary:
This patch adds some initial support for Windows control flow guard. At
the end of the day, the linker needs to synthesize a table of RVAs very
similar to the structured exception handler table (/safeseh).

Both /safeseh and /guard:cf take sections of symbol table indices
(.sxdata and .gfids$y) and turn them into RVA tables referenced by the
load config struct in the CRT through special symbols.

Reviewers: ruiu, amccarth

Subscribers: llvm-commits

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

llvm-svn: 324306
2018-02-06 01:58:26 +00:00
Bob Haarman 5ec448516d [COFF] make /incremental control overwriting unchanged import libraries
Summary:
r323164 made lld-link not overwrite import libraries when their
contents haven't changed. MSVC's link.exe does this only when
performing incremental linking. This change makes lld-link's import
library overwriting similarly dependent on whether or not incremental
linking is being performed. This is controlled by the /incremental or
/incremental:no options. In addition, /opt:icf, /opt:ref, and /order
turn off /incremental and issue a warning if /incremental was
specified on the command line.

Reviewers: rnk, ruiu, zturner

Reviewed By: ruiu

Subscribers: llvm-commits

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

llvm-svn: 323930
2018-01-31 23:44:00 +00:00
Colden Cullen b9b6ed9ae6 [LLD][PDB] Implement FIXME: Warn on missing TypeServer PDB rather than error
Summary: Instead of fatal-ing out when missing a type server PDB, insead warn and cache the miss.

Reviewers: rnk, zturner

Reviewed By: rnk

Subscribers: llvm-commits

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

llvm-svn: 323893
2018-01-31 17:48:04 +00:00
Martin Storsjo cf47b046f9 [COFF] Remove the temporary file if not updating the import library
Differential Revision: https://reviews.llvm.org/D42621

llvm-svn: 323725
2018-01-30 07:26:01 +00:00
Rui Ueyama b6d3a93594 Warn on nonexistent comdat sections in an /order file.
I didn't implement the feature in the original patch because I didn't
come up with an idea to do that easily and efficiently. Turned out that
that is actually easy to implement.

In this patch, we collect comdat sections before gc is run and warn on
nonexistent symbols in an order file.

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

llvm-svn: 323699
2018-01-29 21:50:53 +00:00
Rui Ueyama d73479ba12 Remove trailing space.
llvm-svn: 323682
2018-01-29 19:55:55 +00:00
Shoaib Meenai 34a1101b06 [COFF] Update comment to reflect link.exe behavior. NFC
In my experimentation with link.exe from both VS 2015 and 2017, it
always produces images with truncated section names. Update the comment
accordingly.

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

llvm-svn: 323598
2018-01-27 18:17:08 +00:00
Rui Ueyama 57175aa1e9 Add the /order option.
With the /order option, you can give an order file. An order file
contains symbol names, one per line, and the linker places comdat
sections in that given order. The option is used often to optimize
an output binary for (in particular, startup) speed by improving
locality.

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

llvm-svn: 323579
2018-01-27 00:34:46 +00:00
Bob Haarman 4ce341ffb6 [COFF] don't replace import library if contents are unchanged
Summary:
This detects when an import library is about to be overwritten with a
newly built one with the same contents, and keeps the old library
instead. The use case for this is to avoid needlessly rebuilding
targets that depend on the import library in build systems that rely
on timestamps to determine whether a target requires rebuilding.

This feature was requested in PR35917.

Reviewers: rnk, ruiu, zturner, pcc

Reviewed By: ruiu

Subscribers: llvm-commits

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

llvm-svn: 323164
2018-01-23 00:36:42 +00:00
Martin Storsjo 3b611fa93f [COFF] Keep the underscore on exported decorated stdcall functions in MSVC mode
This fixes PR35733.

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

llvm-svn: 323036
2018-01-20 11:44:42 +00:00
Zachary Turner 1bc2ce6b9b Speed up iteration of CodeView record streams.
There's some abstraction overhead in the underlying
mechanisms that were being used, and it was leading to an
abundance of small but not-free copies being made.  This
showed up on a profile.  Eliminating this and going back to
a low-level byte-based implementation speeds up lld with
/DEBUG between 10 and 15%.

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

llvm-svn: 322871
2018-01-18 18:35:01 +00:00
Zachary Turner 727f153b6f [coff] Print detailed timing information with /TIME.
The classes used to print and update time information are in
common, so other linkers could use this as well if desired.

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

llvm-svn: 322736
2018-01-17 19:16:26 +00:00
Rui Ueyama 2c95e798a0 [LLD][COFF] Report error when file will exceed Windows maximum image size (4GB)
Patch by Colden Cullen.

Currently, when a large PE (>4 GiB) is to be produced, a crash occurs
because:

1. Calling setOffset with a number greater than UINT32_MAX causes the
   PointerToRawData to overflow

2. When adding the symbol table to the end of the file, the last section's
   offset was used to calculate file size. Because this had overflowed,
   this number was too low, and the file created would not be large enough.
   This lead to the actual crash I saw, which was a buffer overrun.

This change:

1. Adds comment to setOffset, clarifying that overflow can occur, but it's
   somewhat safe because the error will be handled elsewhere

2. Adds file size check after all output data has been created This matches
   the MS link.exe error, which looks prints as: "LINK : fatal error
   LNK1248: image size (10000EFC9) exceeds maximum allowable size
   (FFFFFFFF)"

3. Changes calculate of the symbol table offset to just use the existing
   FileSize. This should match the previous calculations, but doesn't rely
   on the use of a u32 that can overflow.

4. Removes trivial usage of a magic number that bugged me while I was
   debugging the issue

I'm not sure how to add a test for this outside of adding 4GB of object
files to the repo. If there's an easier way, let me know and I'll be
happy to add a test.

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

llvm-svn: 322605
2018-01-17 01:08:02 +00:00
Pavel Labath a006baa620 Attempt to fix FreeBSD build broken by the previous commit
The compiler could not find the conversion from
unique_ptr<WritableMemoryBuffer> to unique_ptr<MemoryBuffer>. This will
hopefully help it along.

llvm-svn: 322365
2018-01-12 10:09:10 +00:00
Pavel Labath 1c1e24ad96 [lld/COFF] Use WritableMemoryBuffer for creating the manifest
This avoids the need for const_casting the memory buffer contents in
order to write to it.

NFCI.

llvm-svn: 322363
2018-01-12 09:48:41 +00:00
Rui Ueyama 5fa0d6e4a2 [COFF] Process /EXPORT option in fastpath
Patch by Takuto Ikuta.

This patch reduces lld link time of chromium's blink_core.dll in
component build.

Total size of input argument in .directives become nearly 300MB in the
build and almost all its content are /EXPORT.

To reduce time of parsing too many /EXPORT option in the build, I
introduce fastpath for /EXPORT in ArgParser::parseDirectives.

On my desktop machine, 4 times stats of the link time are like below.
Improved around 20%.

This patch
TotalSeconds : 8.6217627
TotalSeconds : 8.5402175
TotalSeconds : 8.6855853
TotalSeconds : 8.3624441
Ave : 8.5525024

master
TotalSeconds : 10.9975031
TotalSeconds : 11.3409428
TotalSeconds : 10.6332897
TotalSeconds : 10.7650687
Ave : 10.934201075

llvm-svn: 322117
2018-01-09 20:36:42 +00:00
Shoaib Meenai 7e3e28f968 [COFF] Delete CanExitEarly
It was being set but never used, and its value is only ever needed
locally in lld::coff::link.

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

llvm-svn: 322026
2018-01-08 21:14:51 +00:00
Shoaib Meenai fd3e4b0ea1 [COFF] Initalize ErrorHandler with CanExitEarly value
Previously, the COFF driver would call exit(1) from the
ErrorHandler in the case of a link error, even if
CanExitEarly=false was specified. Now it initializes
the ErrorHandler in the same way that the ELF driver does.

Patch by Andrew Kelley.

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

llvm-svn: 321983
2018-01-08 05:58:36 +00:00
Zachary Turner 9e52e50ac0 Fix unhandled switch values.
llvm-svn: 321885
2018-01-05 19:28:39 +00:00
Zachary Turner 6047858270 [PDB] Correctly link S_FILESTATIC records.
This is not a record type that clang currently generates,
but it is a record that is encountered in object files generated
by cl.  This record is unusual in that it refers directly to
the string table instead of indirectly to the string table via
the FileChecksums table.  Because of this, it was previously
overlooked and we weren't remapping the string indices at all.
This would lead to crashes in MSVC when trying to display a
variable whose debug info involved an S_FILESTATIC.

Original bug report by Alexander Ganea

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

llvm-svn: 321883
2018-01-05 19:12:40 +00:00
Rui Ueyama a63eed0f80 Do not parse the same /export string more than once.
Differential Revision: https://reviews.llvm.org/D41607

llvm-svn: 321513
2017-12-28 07:41:19 +00:00
Bob Haarman e90ac016e7 [COFF] support /ignore:4217
Summary:
lld-link accepts link.exe's /ignore option, but used to ignore
it. This can lead to semantic differences when warnings are treated as
fatal errors. One such case is when we resolve an __imp_ symbol to a
local definition. We emit a warning in that case, which /wx turns into
a fatal. This change makes lld-link accept /ignore:4217 to suppress
that warning, so that code that links with link.exe /wx /ignore:4217
links with lld-link, too.

Fixes PR35762.

Reviewers: rnk, ruiu

Reviewed By: ruiu

Subscribers: llvm-commits

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

llvm-svn: 321512
2017-12-28 07:02:13 +00:00
Rui Ueyama 130eb04689 [COFF] Do not parse args twice if no rsp files exists
Patch by Takuto Ikuta.

This patch reduces link time of chromium's blink_core.dll in component
build. Total size of input argument in .directives become nearly 300MB
in the build and no rsp file is used. Speedup link by skipping duplicate
parsing.

On my desktop machine, 4 times stats are like below. Improved around 15%.

This patch
TotalSeconds : 18.408538
TotalSeconds : 17.2996744
TotalSeconds : 17.1053862
TotalSeconds : 17.809777
avg: 17.6558439

master
TotalSeconds : 20.9290504
TotalSeconds : 19.9158213
TotalSeconds : 21.0643515
TotalSeconds : 20.8775831
avg: 20.696701575

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

llvm-svn: 321470
2017-12-27 06:08:10 +00:00
Martin Storsjo 6528fb8691 [COFF] Don't set the thumb bit in address table entries for data symbols
The thumb bit should only be set for executable code.

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

llvm-svn: 321149
2017-12-20 06:50:45 +00:00
Shoaib Meenai a1f6fba4d1 [COFF] Clean up debug option handling
/debug and /debug:dwarf are orthogonal. An object file can contain both
CodeView and DWARF debug info, so the combination of /debug:dwarf and
/debug should generate both DWARF and a PDB, rather than /debug:dwarf
always suppressing PDB creation.

/nopdb is now redundant and can be removed. /debug /nopdb was previously
used to support DWARF, but specifying /debug:dwarf is entirely
equivalent to that combination now.

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

llvm-svn: 320896
2017-12-16 00:23:24 +00:00
Shoaib Meenai c4fdbca604 [COFF] Update an outdated comment. NFC
This comment dates from when LLD didn't produce actual PDBs, and is very
outdated now.

llvm-svn: 320894
2017-12-15 23:52:46 +00:00
Shoaib Meenai 111db7945d [COFF] Simplify hasArgs calls. NFC
We can just pass multiple options to hasArgs (which will check for any
of those options being present) instead of calling it multiple times.

llvm-svn: 320892
2017-12-15 23:51:14 +00:00
Martin Storsjo a1e9b6e3d2 [COFF] Set the IMAGE_DLL_CHARACTERISTICS_NO_SEH flag automatically
This seems to match how link.exe sets it.

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

llvm-svn: 320860
2017-12-15 20:53:03 +00:00
Shoaib Meenai d0bd40294d [COFF] Warn for locally imported symbols
Locally imported symbols are a very surprising linker feature. link.exe
warns for them, and we should warn too.

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

llvm-svn: 320792
2017-12-15 07:49:21 +00:00
Sam Clegg ea244bf89b Fix -Wreorder warning
Subscribers: aheejin, llvm-commits

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

llvm-svn: 320741
2017-12-14 21:09:31 +00:00
Hans Wennborg dcb2f44424 Fix a -Wreorder warning
llvm-svn: 320738
2017-12-14 19:51:18 +00:00
Zachary Turner 0d07a8e948 [COFF] Teach LLD to use the COFF .debug$H section.
This adds the /DEBUG:GHASH option to LLD which will look for
the existence of .debug$H sections in linker inputs and use them
to accelerate type merging.  The clang-cl side has already been
added, so this completes the work necessary to begin experimenting
with this feature.

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

llvm-svn: 320719
2017-12-14 18:07:04 +00:00
Martin Storsjo 94d72b89d6 [COFF] Error out if 20 bit thumb branches are out of range
This is similar to what was added in SVN r277838 for 24 bit
branch instructions.

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

llvm-svn: 320677
2017-12-14 08:56:36 +00:00
Martin Storsjo 9603b8e3f5 [COFF] Sort .pdata for arm64
This works for linking the output from the MSVC compiler.
The pdata entries for arm64 seem to be 8 bytes in the same
(or at least similar) form to arm.

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

llvm-svn: 320676
2017-12-14 08:56:29 +00:00
Martin Storsjo 6ea167cc43 [COFF] Disallow -dynamicbase:no for arm and arm64
This matches what MSVC link.exe does.

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

llvm-svn: 320517
2017-12-12 19:39:13 +00:00
Martin Storsjo 2b964108a0 [COFF] Don't error out on undefined references to __enclave_config
This is required for linking the CRT from MSVC 2017 15.5.

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

llvm-svn: 320462
2017-12-12 08:22:29 +00:00
Rui Ueyama 6074e6b094 Remove redundant local variables.
llvm-svn: 320436
2017-12-11 23:19:11 +00:00
Rui Ueyama b59ceb1068 Do not read the same .lib file more than once.
In the following command line,

  lld-link foo/bar.lib /defaultlib:bar.lib

"/defaultlib:bar.lib" should be a nop even if a file with the same
name exists in other library search path.

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

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

llvm-svn: 320434
2017-12-11 23:09:18 +00:00
Sam Clegg 0fb6faa0be Prefer `ArrayRef` over `const std::vector&`
Differential Revision: https://reviews.llvm.org/D40993

llvm-svn: 320125
2017-12-08 01:09:21 +00:00
Rui Ueyama c0081639cc Remove checkToString functions and use toString instead.
Differential Revision: https://reviews.llvm.org/D40928

llvm-svn: 320005
2017-12-07 03:24:57 +00:00
Shoaib Meenai 9a5161e056 [COFF] Stop lowercasing paths in messages
It's pretty annoying to have LLD lowercase paths in error messages when
cross-compiling from a case-sensitive filesystem, since e.g. if I want
to examine the problematic object file, I have to perform some manual
case correction instead of just being able to copy the path from the
error message.

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

llvm-svn: 319996
2017-12-07 01:21:27 +00:00
Rui Ueyama bdc5150984 Always evaluate the second argument for CHECK() lazily.
This patch is to rename check CHECK and make it a C macro, so that
we can evaluate the second argument lazily.

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

llvm-svn: 319974
2017-12-06 22:08:17 +00:00
Sam Clegg 7e7566323d toString function take a const refs where possible
Differential Revision: https://reviews.llvm.org/D40824

llvm-svn: 319787
2017-12-05 16:50:46 +00:00
Reid Kleckner 3e89b08303 Revert "Merge .xdata into .rdata by default"
This reverts commit r318699, it is breaking 32-bit SEH handlers in
Chromium.

llvm-svn: 319508
2017-12-01 01:04:31 +00:00
Zachary Turner ca6dbf1440 Split TypeTableBuilder into two classes.
llvm-svn: 319456
2017-11-30 18:39:50 +00:00
Rui Ueyama 215286f2a4 [LLD] [COFF] Support ENTRY and SUBSYSTEM in .drectve sections
Adds support for "/ENTRY" and "/SUBSYSTEM" linker options in .drectve
sections. Some Mozilla binaries were using these directives and MSVC
link.exe appears to allow them. No attempt is made to reconcile these
with the options on the command line.

Patch by David Major!

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

llvm-svn: 319356
2017-11-29 20:46:13 +00:00
Zachary Turner 3e3936da93 Make TypeTableBuilder inherit from TypeCollection.
A couple of places in LLD were passing references to
TypeTableCollections around, which makes it hard to change the
implementation at runtime.  However, these cases only needed to
iterate over the types in the collection, and TypeCollection
already provides a handy abstract interface for this purpose.

By implementing this interface, we can get rid of the need to
pass TypeTableBuilder references around, which should allow us
to swap the implementation at runtime in subsequent patches.

llvm-svn: 319345
2017-11-29 19:35:21 +00:00
Martin Storsjo 0010707e1c [COFF] Don't export symbols that have corresponding __imp_ symbols
GNU ld has got an exception for such symbols, and mingw-w64
occasionally uses that exception to avoid exporting symbols in cases
where they otherwise aren't caught by the other exclusion mechanisms.

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

llvm-svn: 319291
2017-11-29 05:50:49 +00:00
Peter Collingbourne 24ca79c776 COFF: Simplify construction of safe SEH table. NFCI.
Instead of building intermediate sets of exception handlers for each
object file, just create one for the final output file.

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

llvm-svn: 319244
2017-11-28 22:50:53 +00:00
Peter Collingbourne bcf7f290ff COFF: Do not add symbols in discarded sections to SEH handler list.
Differential Revision: https://reviews.llvm.org/D40576

llvm-svn: 319229
2017-11-28 21:30:05 +00:00
Rui Ueyama 2017d52b54 Move Memory.{h,cpp} to Common.
Differential Revision: https://reviews.llvm.org/D40571

llvm-svn: 319221
2017-11-28 20:39:17 +00:00
Martin Storsjo f2508f46ca [COFF] Interpret a period as a separator for section suffix just like '$'
This allows grouping all sections like ".ctors.12345" into ".ctors".

For MinGW, the numerical values for such ctors are all zero-padded,
so a lexical sort is good enough.

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

llvm-svn: 319151
2017-11-28 08:08:37 +00:00
Rui Ueyama 53fe469162 Factor out common code to Common/Strings.cpp.
Differential Revision: https://reviews.llvm.org/D40530

llvm-svn: 319138
2017-11-28 02:15:26 +00:00
Peter Collingbourne 1621c20ffc Reland r319090, "COFF: Do not create SectionChunks for discarded comdat sections." with a fix for debug sections.
If /debug was not specified, readSection will return a null
pointer for debug sections. If the debug section is associative with
another section, we need to make sure that the section returned from
readSection is not a null pointer before adding it as an associative
section.

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

llvm-svn: 319133
2017-11-28 01:30:07 +00:00
Sam Clegg a80d94d52b Rename `Symtab` private memory to avoid confusion with global `Symtab`
This is also consistent with SymVector that exists in COFF port
and soon to be added to the wasm port.

Split off as part of https://reviews.llvm.org/D40371

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

llvm-svn: 319113
2017-11-27 23:16:06 +00:00
Sam Clegg e2ed092e01 Consistent use of <internal> when displaying internal symbol names
Differential Revision: https://reviews.llvm.org/D40510

llvm-svn: 319108
2017-11-27 22:49:16 +00:00
Peter Collingbourne c8477b8234 Revert r319090, "COFF: Do not create SectionChunks for discarded comdat sections."
Caused test failures in check-cfi on Windows.
http://lab.llvm.org:8011/builders/sanitizer-windows/builds/20284

llvm-svn: 319100
2017-11-27 21:37:51 +00:00
Peter Collingbourne 3f2921f5ec COFF: Do not create SectionChunks for discarded comdat sections.
With this change, instead of creating a SectionChunk for each section
in the object file, we only create them when we encounter a prevailing
comdat section.

Also change how symbol resolution occurs between comdat symbols. Now
only the comdat leader participates in comdat resolution, and not any
other external associated symbols. This is more in line with how COFF
semantics are defined, and should allow for a more straightforward
implementation of non-ANY comdat types.

On my machine, this change reduces our runtime linking a release
build of chrome_child.dll with /nopdb from 5.65s to 4.54s (median of
50 runs).

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

llvm-svn: 319090
2017-11-27 20:42:34 +00:00
Rui Ueyama 66384b391e Return early. NFC.
llvm-svn: 318826
2017-11-22 09:06:42 +00:00
Rui Ueyama c121401ca5 Add more blank lines to separate code chunks.
llvm-svn: 318825
2017-11-22 09:06:27 +00:00
Peter Collingbourne f874bd67d8 COFF: Emit a COFF symbol table if /debug:dwarf is specified.
This effectively reverts r318548 and r318635 while keeping the
functionality behind the flag and preserving the bug fix from r318548.

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

llvm-svn: 318721
2017-11-21 01:14:14 +00:00
Reid Kleckner 95cc796cf1 Merge .xdata into .rdata by default
Summary: MSVC does this. The user can override it with their own /merge: flag.

Reviewers: ruiu, pcc

Subscribers: llvm-commits

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

llvm-svn: 318699
2017-11-20 21:49:35 +00:00
Peter Collingbourne 31275d4472 COFF: Correctly handle relocations against early discarded sections.
Don't crash if we encounter a reference to an early discarded section
(such as .drectve). Instead, handle them the same way as sections
discarded by comdat merging, i.e. either print an error message or
(for debug sections) silently ignore the relocation.

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

llvm-svn: 318689
2017-11-20 19:37:07 +00:00
Peter Collingbourne 80f961ab66 COFF: Remove the SymbolBodies vector, and rename SparseSymbolBodies to Symbols.
Differential Revision: https://reviews.llvm.org/D40237

llvm-svn: 318683
2017-11-20 18:52:53 +00:00
Peter Collingbourne d01571353d COFF: Stop requiring comdat sections to have an external leader to participate in ICF.
This requirement was added in r254578 to fix pr25686. However, it
appears to have originated from a misdiagnosis of the problem: link.exe
refused to merge the two sections because they are non-executable,
not because they have internal leaders. If I set up a similar scenario
with functions instead of globals I see that link.exe merges them.

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

llvm-svn: 318682
2017-11-20 18:51:29 +00:00
Peter Collingbourne 5c7b467b6e COFF: Change SparseChunks to be of type std::vector<SectionChunk *>. NFC.
llvm-svn: 318636
2017-11-20 05:31:23 +00:00
Peter Collingbourne 38e3a1ea17 COFF: Remove unused fields. NFC.
llvm-svn: 318635
2017-11-20 05:31:09 +00:00
Peter Collingbourne 5e80bdebd2 COFF: Stop emitting a non-standard COFF symbol table into PEs.
Now that our support for PDB emission is reasonably good, there is
no longer a need to emit a COFF symbol table.

Also fix a bug where we would fail to emit a string table for long
section names if /debug was not specified.

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

llvm-svn: 318548
2017-11-17 19:51:20 +00:00
Reid Kleckner d99ac29a24 All .xdata sections are eligble for ICF
Summary:
Many small functions have identical unwind info because they push the
same sets of CSRs in the same order and have the same stack and prologue
size. The VC linker merges duplicate .xdata, and so should LLD.

This reduces the .xdata section size of clang.exe from 1.8MB to 94KB.

Reviewers: pcc, ruiu

Subscribers: llvm-commits

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

llvm-svn: 318547
2017-11-17 19:50:10 +00:00
Martin Storsjo 46304e03ec [COFF] Don't write long section names for sections that will be mapped at runtime
Sections that will be mapped at runtime will only have the short
section name available, since the string table it points into isn't
mapped. Therefore prefer truncating those names over writing a
long name that is unavailable at runtime.

This allows libunwind to find the .eh_frame section at runtime even
if the module was built with debug info enabled.

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

llvm-svn: 318391
2017-11-16 12:06:42 +00:00
Martin Storsjo fe3eda9137 [COFF] Improve the autoexport check for symbols from import libraries with -opt:noref
If -opt:noref is specified, they can end up with isLive() == 1
when the autoexport check is run.

To reduce the risk of potential issues, only consider exporting
DefinedRegular and DefinedCommon, nothing else.

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

llvm-svn: 318384
2017-11-16 07:22:44 +00:00
Bob Haarman fe059c782f [coff] correctly emit safeseh entries for handlers defined in dlls
Summary:
We previously assumed that all SafeSEH handlers are
DefinedRegular symbols. This is not the case for handlers defined in
DLLs. As a result, we were failing to emit entries in the SafeSEH
table for those handlers. This change fixes that.

Fixes PR35324.

Reviewers: rnk, ruiu

Reviewed By: rnk

Subscribers: llvm-commits

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

llvm-svn: 318364
2017-11-16 01:22:01 +00:00
Martin Storsjo 61716878ae [COFF] Always include the size of the string table size field
Even if we don't actually write any string table contents, the
4 byte size for the string table will always be written. Make
sure we accommodate for this in the file size. Since this size
is aligned up, this would seldom be an issue in practice.

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

llvm-svn: 318284
2017-11-15 08:18:25 +00:00
Reid Kleckner 5883989e51 Remove a std::map and std::set that show up in LLD profiles
For GC roots, add a bit to SymbolBody to ensure that we don't add the
same root twice, and switch to a vector. In addition to being faster,
this may also fix some latent non-determinism. We iterate the GCRoot
list later and it the order should be deterministic.

For fixupExports, we can just use DenseMap. This is a simple string
uniquing task, and we don't iterate the map.

Reviewers: ruiu

Subscribers: llvm-commits

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

llvm-svn: 318072
2017-11-13 18:38:53 +00:00
Reid Kleckner c2dcdd852b Disable GC and ICF when /debug is present
ICF and GC impair debugging, so MSVC disables these optimizations when
/debug is passed. They are still on by default when no PDB is produced.

This change also makes /opt:ref enable ICF, which is consistent with
MSVC: https://msdn.microsoft.com/en-us/library/bxwfs976.aspx

We should consider making /opt:icf fold readonly data in the near
future. LLD used to do this, but we disabled it because it breaks too
many programs. MSVC only does this if the user explicitly passes
/opt:icf.

Reviewers: ruiu, pcc

Subscribers: llvm-commits

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

llvm-svn: 318071
2017-11-13 18:38:25 +00:00
Rafael Espindola 0a7d0230fc Try harder to delete the temporary file.
This changes COFF to use the output buffer that is reset by the error
handler.

llvm-svn: 318062
2017-11-13 18:15:22 +00:00
Rafael Espindola 4ee864b763 Remove dead include.
llvm-svn: 318061
2017-11-13 18:10:37 +00:00
Reid Kleckner 8320a1753f Use DenseMap instead of std::map in fixupExports
Some DLLs have many exports, and this data structure shows up in the
profile of linking content.dll.

llvm-svn: 317910
2017-11-10 19:12:01 +00:00
Martin Storsjo bfe2fed2c4 [COFF] Add support for IMAGE_REL_ARM64_SECREL
I never ran into this until lld-link started enabling debug output
by default for the mingw mode. I haven't been able to verify that
this actually behaves correctly, but this relocation is handled
identically on all other architectures so far.

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

llvm-svn: 317669
2017-11-08 07:31:50 +00:00
Rafael Espindola 5f903f3848 Update for llvm change.
llvm-svn: 317657
2017-11-08 01:50:34 +00:00
Bob Haarman 6c301b6eb1 [coff] use relative instead of absolute __safe_se_handler_base when present
Summary:
__safe_se_handler_base should be either absolute 0 (when no SafeSEH
table is present), or relative to the image base (when the table is
present). An earlier change inadvertedly made the symbol absolute in
both cases, leading to the SafeSEH table not being locatble at run
time. This change fixes that and updates the safeseh test to check for
the presence of the relocation.

Reviewers: rnk, ruiu

Reviewed By: ruiu

Subscribers: ruiu, llvm-commits

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

llvm-svn: 317635
2017-11-07 23:24:10 +00:00
Martin Storsjo 6bde1667ba [MinGW] Don't autoexport anything from libmsvcrt or libucrtbase
These libraries contain a number of object files with compat wrappers,
in addition to the normal import library entries.

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

llvm-svn: 317505
2017-11-06 20:33:13 +00:00
Martin Storsjo ee8c973884 [COFF] Handle ARM64 in getDefaultType
Differential Revision: https://reviews.llvm.org/D39634

llvm-svn: 317455
2017-11-06 07:02:33 +00:00
Rui Ueyama 7782abe5e6 [COFF] Avoid "Body" as a local variable name.
Since SymbolBody is gone, "Body" is not a good variable name.
This patch renames or eliminates them.

llvm-svn: 317384
2017-11-03 22:49:02 +00:00
Rui Ueyama f483da0038 Rename replaceBody -> replaceSymbol.
llvm-svn: 317383
2017-11-03 22:48:47 +00:00
Martin Storsjo 068512dfb9 [MinGW] Output debug info by default, unless the -s parameter is passed
Differential Revision: https://reviews.llvm.org/D39541

llvm-svn: 317376
2017-11-03 22:10:37 +00:00
Rui Ueyama f52496e1e0 Rename SymbolBody -> Symbol
Now that we have only SymbolBody as the symbol class. So, "SymbolBody"
is a bit strange name now. This is a mechanical change generated by

  perl -i -pe s/SymbolBody/Symbol/g $(git grep -l SymbolBody lld/ELF lld/COFF)

nd clang-format-diff.

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

llvm-svn: 317370
2017-11-03 21:21:47 +00:00
Martin Storsjo dc95dbfcab [COFF] Autoexport symbols as data if they don't point to an executable section
This was already taken care of for the output def file.

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

llvm-svn: 317367
2017-11-03 20:58:20 +00:00