Commit Graph

1031 Commits

Author SHA1 Message Date
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