Commit Graph

186 Commits

Author SHA1 Message Date
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
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
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
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
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
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
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
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
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
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
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
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 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 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 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
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
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
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
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
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
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
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
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
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
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
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 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
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 5c7b467b6e COFF: Change SparseChunks to be of type std::vector<SectionChunk *>. NFC.
llvm-svn: 318636
2017-11-20 05:31:23 +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 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
Rui Ueyama 616cd99194 [COFF] Merge Symbol and SymbolBody.
llvm-svn: 317007
2017-10-31 16:10:24 +00:00
Bob Haarman b8a59c8aa5 [lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.

Reviewers: ruiu

Reviewed By: ruiu

Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits

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

llvm-svn: 316624
2017-10-25 22:28:38 +00:00
Rafael Espindola 329886556a Update for llvm change.
llvm-svn: 315478
2017-10-11 17:23:29 +00:00
Saleem Abdulrasool 353c57a3f6 COFF: simplify thunk handling (NFC)
Apply the simplification suggestions that Peter Collingbourne made
during the review at D37368.  The returned thunk is cast to the
appropriate type in the SymbolTable, and the constant symbol's body is
not needed directly, so avoid the assignment.  NFC

llvm-svn: 312391
2017-09-01 23:35:43 +00:00
Saleem Abdulrasool 3170ad7431 COFF: handle multiply defined symbols with different storage
If a symbol is locally defined and is DLL imported in another
translation unit, and the object with the locally defined version is
loaded prior to the imported version, then the linker will fail to
resolve the definition of the thunk and return the locally defined
symbol.  This will then be attempted to be cast to an import thunk,
which will clearly fail.

Only return the thunk if the symbol is inserted or a thunk is created.
Otherwise, report a duplication error.

llvm-svn: 312386
2017-09-01 22:12:10 +00:00
Rui Ueyama c1e101f356 Revert r312171: Support nested static libraries.
This reverts commit r312171 because it is pointed out that that's not a
correct fix (see https://bugs.llvm.org/show_bug.cgi?id=32674#c14) and
also because it broke buildbots.

llvm-svn: 312174
2017-08-30 21:22:59 +00:00
Rui Ueyama a68748659a Support nested static libraries.
MSVC link.exe supports nested static libraries. That is, an .a file can
contain other .a file as its member. It is reported that MySQL actually
depends on this feature.

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

llvm-svn: 312171
2017-08-30 20:55:41 +00:00
Rui Ueyama f59b709ae4 Move a function from Driver.cpp to InputFile.cpp.
This patch doesn't improve code per se, but it should make the following
patch's diff easier to read.

llvm-svn: 312170
2017-08-30 20:55:18 +00:00