Commit Graph

3426 Commits

Author SHA1 Message Date
Michael J. Spencer ded14f8a33 llvm api change.
llvm-svn: 240732
2015-06-25 23:41:55 +00:00
Rui Ueyama f799edef28 COFF: Rename /opt:icf -> /opt:lldicf.
ICF implemented in LLD is so experimental that we don't want to
enable that even if /opt:icf option is passed. I'll rename it back
once the feature is complete.

llvm-svn: 240721
2015-06-25 23:26:58 +00:00
Rui Ueyama 68633f1719 COFF: Better error message for duplicate symbols.
Now the symbol table prints out not only symbol names but
also file names for duplicate symbols.

llvm-svn: 240719
2015-06-25 23:22:00 +00:00
Rui Ueyama 9b921e5dc9 COFF: Merge DefinedRegular and DefinedCOMDAT.
I split them in r240319 because I thought they are different enough
that we should treat them as different types. It turned out that
that was not a good idea. They are so similar that we ended up having
many duplicate code.

llvm-svn: 240706
2015-06-25 22:00:42 +00:00
Rui Ueyama 5817ebb0c8 COFF: Fix lexer for the module-definition file.
Previously it would hang if there's a stray punctuation (e.g. ?).

llvm-svn: 240697
2015-06-25 21:06:00 +00:00
Rui Ueyama b0c001c055 COFF: Remove dead code.
llvm-svn: 240682
2015-06-25 20:12:15 +00:00
Rui Ueyama fc510f4cf8 COFF: Devirtualize mark(), markLive() and isCOMDAT().
Only SectionChunk can be dead-stripped. Previously,
all types of chunks implemented these functions,
but their functions were blank.

Likewise, only DefinedRegular and DefinedCOMDAT symbols
can be dead-stripped. markLive() function was implemented
for other symbol types, but they were blank.

I started thinking that the change I made in r240319 was
a mistake. I separated DefinedCOMDAT from DefinedRegular
because I thought that would make the code cleaner, but now
we want to handle them as the same type here. Maybe we
should roll it back.

This change should improve readability a bit as this removes
some dubious uses of reinterpret_cast. Previously, we
assumed that all COMDAT chunks are actually SectionChunks,
which was not very obvious.

llvm-svn: 240675
2015-06-25 19:10:58 +00:00
Rui Ueyama f34c088515 COFF: Simplify. NFC.
llvm-svn: 240666
2015-06-25 17:56:36 +00:00
Rui Ueyama c6fcfbc98a COFF: Use std::equal to compare two lists of relocations.
llvm-svn: 240665
2015-06-25 17:51:07 +00:00
Rui Ueyama 02c302790f COFF: Don't use COFFHeader->NumberOfRelocations.
The size of the field is 16 bit, so it's inaccurate if the
number of relocations in a section is more than 65535.

llvm-svn: 240661
2015-06-25 17:43:37 +00:00
Rafael Espindola 69e942aefe Update for llvm change.
llvm-svn: 240658
2015-06-25 17:04:12 +00:00
Rui Ueyama 88e0f9206b COFF: Fix a bug of __imp_ symbol.
The change I made in r240620 was not correct. If a symbol foo is
defined, and if you use __imp_foo, __imp_foo symbol is automatically
defined as a pointer (not just an alias) to foo.

Now that we need to create a chunk for automatically-created symbols.
I defined LocalImportChunk class for them.

llvm-svn: 240622
2015-06-25 03:31:47 +00:00
Rui Ueyama d766653534 COFF: Handle undefined symbols starting with __imp_ in a special way.
MSVC linker is able to link an object file created from the following code.
Note that __imp_hello is not defined anywhere.

  void hello() { printf("Hello\n"); }
  extern void (*__imp_hello)();
  int main() { __imp_hello(); }

Function symbols exported from DLLs are automatically mangled by appending
__imp_ prefix, so they have two names (original one and with the prefix).
This "feature" seems to simulate that behavior even for non-DLL symbols.

This is in my opnion very odd feature. Even MSVC linker warns if you use this.
I'm adding that anyway for the sake of compatibiltiy.

llvm-svn: 240620
2015-06-25 02:21:44 +00:00
Rui Ueyama 42aa00b34b COFF: Use COFFObjectFile::getRelocations(). NFC.
llvm-svn: 240614
2015-06-25 00:33:38 +00:00
Rui Ueyama cde92423d7 COFF: Cache raw pointers to relocation tables.
Getting an iterator to the relocation table is very hot operation
in the linker. We do that not only to apply relocations but also
to mark live sections and to do ICF.

libObject's interface is slow. By caching pointers to the first
relocation table entries makes the linker 6% faster to self-link.

We probably need to fix libObject as well.

llvm-svn: 240603
2015-06-24 23:03:17 +00:00
Rui Ueyama 49560c7a10 COFF: Move code for ICF from Writer.cpp to ICF.cpp.
llvm-svn: 240590
2015-06-24 20:40:03 +00:00
Adhemerval Zanella 3ebea27d66 [ELF] Fix .init_array initialization
Some compilers may not add the section symbol in '.symtab' for the
.init_array and 'ldd' just ignore it.  It results in global constructor
not being called in final executable.

This patch add both '.init_array' and '.fini_array' to be added in
Atom graph generation even when the section contains no symbol.  An
already existing testcase is modified to check for such scenario.

The issue fixes the llvm test-suite regressions for both Single
and MultiSource files.

llvm-svn: 240570
2015-06-24 19:26:00 +00:00
Rui Ueyama ddf71fc370 COFF: Initial implementation of Identical COMDAT Folding.
Identical COMDAT Folding (ICF) is an optimization to reduce binary
size by merging COMDAT sections that contain the same metadata,
actual data and relocations. MSVC link.exe and many other linkers
have this feature. LLD achieves on per with MSVC in terms produced
binary size with this patch.

This technique is pretty effective. For example, LLD's size is
reduced from 64MB to 54MB by enaling this optimization.

The algorithm implemented in this patch is extremely inefficient.
It puts all COMDAT sections into a set to identify duplicates.
Time to self-link with/without ICF are 3.3 and 320 seconds,
respectively. So this option roughly makes LLD 100x slower.
But it's okay as I wanted to achieve correctness first.
LLD is still able to link itself with this optimization.
I'm going to make it more efficient in followup patches.

Note that this optimization is *not* entirely safe. C/C++ require
different functions have different addresses. If your program
relies on that property, your program wouldn't work with ICF.
However, it's not going to be an issue on Windows because MSVC
link.exe turns ICF on by default. As long as your program works
with default settings (or not passing /opt:noicf), your program
would work with LLD too.

llvm-svn: 240519
2015-06-24 04:36:52 +00:00
Peter Collingbourne bd3a29d063 COFF: Remove unused field SectionChunk::SectionIndex.
llvm-svn: 240512
2015-06-24 00:12:36 +00:00
Peter Collingbourne 2ed4c8f55d COFF: Add some error checking to SymbolTable::addCombinedLTOObject().
llvm-svn: 240511
2015-06-24 00:12:34 +00:00
Peter Collingbourne c7b685d997 COFF: Ignore debug symbols.
Differential Revision: http://reviews.llvm.org/D10675

llvm-svn: 240487
2015-06-24 00:05:50 +00:00
Rui Ueyama 6a60be7749 COFF: Add names for logging/debugging to COMDAT chunks.
Chunks are basically unnamed chunks of bytes, and we don't like
to give them names. However, for logging or debugging, we want to
know symbols names of functions for COMDAT chunks. (For example,
we want to print out "we have removed unreferenced COMDAT section
which contains a function FOOBAR.")

This patch is to do that.

llvm-svn: 240484
2015-06-24 00:00:52 +00:00
Rui Ueyama 0d2e999050 COFF: Make link order compatible with MSVC link.exe.
Previously, we added files in directive sections to the symbol
table as we read the sections, so the link order was depth-first.
That's not compatible with MSVC link.exe nor the old LLD.

This patch is to queue files so that new files are added to the
end of the queue and processed last. Now addFile() doesn't parse
files nor resolve symbols. You need to call run() to process
queued files.

llvm-svn: 240483
2015-06-23 23:56:39 +00:00
Lang Hames 49047039b0 [lld] Add MachO thread-local storage support.
This allows LLD to correctly link MachO objects that use thread-local storage.

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

llvm-svn: 240454
2015-06-23 20:35:31 +00:00
Peter Collingbourne bf0aa08bba COFF: Fix null pointer dereference.
llvm-svn: 240447
2015-06-23 20:02:31 +00:00
Benjamin Kramer cfacc9d3e6 [MachO] Initialize all fields of NormalizedFile.
The ObjectFileYAML.roundTrip serializes a default-constructed
NormalizedFile to YAML, triggering uninitialized memory reads.

While there use in-class member initializers.

llvm-svn: 240446
2015-06-23 19:55:04 +00:00
Benjamin Kramer 44b0723069 Add missing dependencies for the CMake shared lld build.
llvm-svn: 240445
2015-06-23 19:54:57 +00:00
David Blaikie 6521ed964b Update for LLVM API change to return by InputArgList directly (rather than by pointer) from ParseArgs
llvm-svn: 240347
2015-06-22 22:06:52 +00:00
David Blaikie 008181933d Fix missed formatting in prior commit (mostly 80 cols violation and some whitespace around *)
llvm-svn: 240346
2015-06-22 22:06:48 +00:00
Rui Ueyama 617f5ccb5c COFF: Separate DefinedCOMDAT from DefinedRegular symbol type. NFC.
Before this change, you got to cast a symbol to DefinedRegular and then
call isCOMDAT() to determine if a given symbol is a COMDAT symbol.
Now you can just use isa<DefinedCOMDAT>().

As to the class definition of DefinedCOMDAT, I could remove duplicate
code from DefinedRegular and DefinedCOMDAT by introducing another base
class for them, but I chose to not do that to keep the class hierarchy
shallow. This amount of code duplication doesn't worth to define a new
class.

llvm-svn: 240319
2015-06-22 19:56:01 +00:00
Rui Ueyama 610962061f Fix typo.
llvm-svn: 240298
2015-06-22 17:26:27 +00:00
Simon Atanasyan 356d7c52b6 [Mips] Support R_MICROMIPS_HI0_LO16 relocation handling
llvm-svn: 240268
2015-06-22 09:27:05 +00:00
Simon Atanasyan f44f854af3 [Mips] Support R_MICROMIPS_LITERAL relocation handling
llvm-svn: 240267
2015-06-22 09:26:57 +00:00
Simon Atanasyan 692e792575 [Mips] Support R_MIPS_LITERAL relocation handling
llvm-svn: 240266
2015-06-22 09:26:48 +00:00
Simon Atanasyan ca0fe2f4a4 [Mips] Support R_MICROMIPS_SUB relocation handling
llvm-svn: 240265
2015-06-22 09:26:41 +00:00
Simon Atanasyan 1af72b898c [Mips] Reject R_MIPS_GPREL32 against external symbols
llvm-svn: 240264
2015-06-22 09:26:33 +00:00
Simon Atanasyan 74a07c5457 [Mips] Fix test case - do not use R_MIPS_GPREL32 against external symbols
No functional changes.

llvm-svn: 240263
2015-06-22 09:26:25 +00:00
Simon Atanasyan b5d26b1433 [Mips] Reject position-dependent relocations in case of shared library linking
llvm-svn: 240262
2015-06-22 09:26:20 +00:00
Simon Atanasyan 7851f3b111 [Mips] Fix test case - do not use R_MIPS_HI16 for shared library linking
No functional changes.

llvm-svn: 240261
2015-06-22 09:26:12 +00:00
Simon Atanasyan 46d97f246b [Mips] Support R_MICROMIPS_HIGHER / R_MICROMIPS_HIGHEST relocations handling
llvm-svn: 240260
2015-06-22 09:26:05 +00:00
Simon Atanasyan e55110454d [Mips] Support R_MIPS_HIGHER / R_MIPS_HIGHEST relocations handling
llvm-svn: 240259
2015-06-22 09:25:57 +00:00
Rui Ueyama a77336bd5d COFF: Support delay-load import tables.
DLLs are usually resolved at process startup, but you can
delay-load them by passing /delayload option to the linker.

If a /delayload is specified, the linker has to create data
which is similar to regular import table.
One notable difference is that the pointers in a delay-load
import table are originally pointing to thunks that resolves
themselves. Each thunk loads a DLL, resolve its name, and then
overwrites the pointer with the result so that subsequent
function calls directly call a desired function. The linker
has to emit thunks.

llvm-svn: 240250
2015-06-21 22:31:52 +00:00
David Blaikie b2b1c7c3e1 ArrayRef-ify Driver::parse and related functions.
llvm-svn: 240236
2015-06-21 06:32:10 +00:00
David Blaikie 8da889f1a5 ArrayRef-ify ParseArgs
llvm-svn: 240235
2015-06-21 06:32:04 +00:00
Rui Ueyama 1a109285c2 COFF: Use short varaible name. NFC.
llvm-svn: 240232
2015-06-21 04:10:54 +00:00
Rui Ueyama 4d769c3a57 COFF: Support exception table.
.pdata section contains a list of triplets of function start address,
function end address and its unwind information. Linkers have to
sort section contents by function start address and set the section
address to the file header (so that runtime is able to find it and
do binary search.)

This change seems to resolve all but one remaining test failures in
check{,-clang,-lld} when building the entire stuff with clang-cl and
lld-link.

llvm-svn: 240231
2015-06-21 04:00:54 +00:00
Rui Ueyama e3a335076a COFF: Combine add{Object,Archive,Bitcode,Import} functions. NFC.
llvm-svn: 240229
2015-06-20 23:10:05 +00:00
Rui Ueyama 5e31d0b2e9 COFF: Fix common symbol alignment.
llvm-svn: 240217
2015-06-20 07:25:45 +00:00
Rui Ueyama efb7e1aa29 COFF: Fix a common symbol bug.
This is a case that one mistake caused a very mysterious bug.
I made a mistake to calculate addresses of common symbols, so
each common symbol pointed not to the beginning of its location
but to the end of its location. (Ouch!)

Common symbols are aligned on 16 byte boundaries. If a common
symbol is small enough to fit between the end of its real
location and whatever comes next, this bug didn't cause any harm.

However, if a common symbol is larger than that, its memory
naturally overlapped with other symbols. That means some
uninitialized variables accidentally shared memory. Because
totally unrelated memory writes mutated other varaibles, it was
hard to debug.

It's surprising that LLD was able to link itself and all LLD
tests except gunit tests passed with this nasty bug.

With this fix, the new COFF linker is able to pass all tests
for LLVM, Clang and LLD if I use MSVC cl.exe as a compiler.
Only three tests are failing when used with clang-cl.

llvm-svn: 240216
2015-06-20 07:21:57 +00:00
Peter Collingbourne 74ecc89c46 COFF: Take reference to argument vector using std::vector::data() instead of operator[](0).
This avoids undefined behaviour caused by an out-of-range access if the
vector is empty, which can happen if an object file's directive section
contains only whitespace.

llvm-svn: 240183
2015-06-19 22:40:05 +00:00