Commit Graph

235 Commits

Author SHA1 Message Date
Rui Ueyama 4e1536c155 COFF: Fix AMD64_SECTION relocation.
llvm-svn: 241658
2015-07-08 01:47:28 +00:00
Rui Ueyama 11863b4ae1 COFF: Support x86 file header and relocations.
llvm-svn: 241657
2015-07-08 01:45:29 +00:00
Rui Ueyama 84936e0b43 COFF: Check for incompatible machine types.
llvm-svn: 241647
2015-07-07 23:39:18 +00:00
Rui Ueyama 661a4e7ab6 COFF: Split writeTo in preparation for supporting 32-bit x86.
llvm-svn: 241638
2015-07-07 22:49:21 +00:00
Peter Collingbourne f5339ec035 COFF: Improve undefined symbol diagnostics.
We now report the names of any files containing undefined symbol references.

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

llvm-svn: 241612
2015-07-07 18:38:39 +00:00
Peter Collingbourne 8e17451d54 COFF: Fix bug involving archives defining a symbol multiple times.
Previously we were unnecessarily loading lazy symbols if they appeared in an
archive multiple times, as can happen with comdat symbols. This change fixes
the bug by only loading symbols from archives at load time if the original
symbol was undefined.

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

llvm-svn: 241538
2015-07-07 02:15:25 +00:00
Rui Ueyama 95dd08e4c9 COFF: Make ArchiveFile::getMember lock-free.
The previous code was not even safe with MSVC 2013 because the compiler
doesn't guarantee that static variables (in this case, a mutex) are
initialized in a thread-safe manner.

llvm-svn: 241481
2015-07-06 18:22:16 +00:00
Rui Ueyama 183f53fd22 COFF: Support isa<> for Symbol::Body, whose type is std::atomic<SymbolBody *>.
llvm-svn: 241477
2015-07-06 17:45:22 +00:00
Rui Ueyama 92a8c82076 COFF: Set TLS table header field.
TLS table header field is supposed to have address and size of TLS table.
The linker doesn't have to understand what TLS table is. TLS table's name
is always "_tls_used", so if there's that symbol, the linker simply sets
that symbol's RVA to the header. The size of the TLS table is always 40 bytes.

llvm-svn: 241426
2015-07-06 01:48:01 +00:00
Rui Ueyama adcde5384e COFF: Make ArchiveFile::getMember thread-safe.
This function is called SymbolTable::readObjects, so in order to
parallelize that function, we have to make this function thread-safe.

llvm-svn: 241420
2015-07-05 22:50:00 +00:00
Rui Ueyama e2eb15577d COFF: Use CAS to update Sym->Body.
Note that the linker is not multi-threaded yet.
This is a preparation for that.

llvm-svn: 241417
2015-07-05 22:05:08 +00:00
Rui Ueyama c80c03da6c COFF: Use atomic pointers in preparation for parallelizing.
In the new design, mutation of Symbol pointers is the name resolution
operation. This patch makes them atomic pointers so that they can
be mutated by multiple threads safely. I'm going to use atomic
compare-exchange on these pointers.

dyn_cast<> doesn't recognize atomic pointers as pointers,
so we need to call load(). This is unfortunate, but in other places
automatic type conversion works fine.

llvm-svn: 241416
2015-07-05 21:54:42 +00:00
Rui Ueyama 2b82d5f8ca COFF: Do not warn on identical /merge options.
llvm-svn: 241397
2015-07-04 23:54:52 +00:00
Rui Ueyama 6600eb18cd COFF: Implement /merge option.
/merge:.foo=.bar makes the linker to merge section .foo with section .bar.

llvm-svn: 241396
2015-07-04 23:37:32 +00:00
Peter Collingbourne 2612a32ce5 COFF: Numerous fixes for interaction between LTO and weak externals.
We were previously hitting assertion failures in the writer in cases where
a regular object file defined a weak external symbol that was defined by
a bitcode file. Because /export and /entry name mangling were implemented
using weak externals, the same problem affected mangled symbol names in
bitcode files.

The underlying cause of the problem was that weak external symbols were
being resolved before doing LTO, so the symbol table may have contained stale
references to bitcode symbols. The fix here is to defer weak external symbol
resolution until after LTO.

Also implement support for weak external symbols in bitcode files
by modelling them as replaceable DefinedBitcode symbols.

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

llvm-svn: 241391
2015-07-04 05:28:41 +00:00
Rui Ueyama 0569ecfa9b Revert "COFF: Do not use VirtualSize section header field for directive sections."
This reverts commit r241386 because the issue is addressed in LLVM (r241387).

llvm-svn: 241388
2015-07-04 03:27:46 +00:00
Rui Ueyama cffbe7cb55 COFF: Do not use VirtualSize section header field for directive sections.
Looks like clang-cl sets a bogus value to the field, which makes
getSectionContents() to truncate section contents. This patch directly
uses SizeOfRawData field instead of VirtualSize to see if this can
make buildbot green.

llvm-svn: 241386
2015-07-04 02:26:20 +00:00
Rui Ueyama 3126c6c565 Use map::insert instead of checking existence of a key and insert. NFC.
llvm-svn: 241385
2015-07-04 02:00:22 +00:00
Rui Ueyama a3d463df6f COFF: Print directive section contents if /verbose.
llvm-svn: 241384
2015-07-04 01:39:11 +00:00
Rui Ueyama b0398827c2 COFF: Fix bug in garbage collector.
GC root may have non-regular defined symbols, such as DefinedImportThunk,
so this cast<> was a wrong assumption.

llvm-svn: 241382
2015-07-04 01:10:32 +00:00
Rui Ueyama 4b8cdd20fb COFF: Don't print warning message for identical /export options.
llvm-svn: 241379
2015-07-03 23:23:29 +00:00
Peter Collingbourne da2f094bbb COFF: Fix the case where an object defines a weak external and its alias.
This worked before, but only by accident, and only with assertions disabled.
We ended up storing a DefinedRegular symbol in the WeakAlias field,
and never using it as an Undefined.

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

llvm-svn: 241376
2015-07-03 22:03:36 +00:00
Rui Ueyama a51ce71fdf COFF: Call exit(0) on success to not call destructors.
This change cut the link time of chrome.dll from 24 seconds
to 22 seconds (5% gain). When the control reaches end of link(),
all output files have already been written. All in-memory
objects can just vanish. There is no use to call their dtors.

llvm-svn: 241320
2015-07-03 05:31:35 +00:00
Rui Ueyama d8111f2c2e COFF: Fix ordinal-only delay-imported symbols.
DLLs can export symbols only by ordinal, and DLLs are also able to be
delay-loaded. The combination of the two is valid. I didn't expect
that combination. This patch implements that feature.

With this patch, LLD is now able to link a working executable of Chrome
for 64-bit debug build. The browser seemed to be working fine. Chrome is
good for testing because of its variety and size. It contains various
open-source libraries written by various people. The largest file in
Chrome is chrome.dll whose size is 496MB. LLD can link it in 24 seconds.
MSVC linker takes 48 seconds. So it is exactly 2x faster. (I measured
that with debug info and ICF being turned off.)

With this achievement, I think I can say that the new COFF linker is
now mostly feature complete for x86-64 Windows. I believe there are
still many lingering bugs, though.

llvm-svn: 241318
2015-07-03 04:32:49 +00:00
Rui Ueyama 7a247ee242 COFF: Fix a bug that /delayload was case-sensitive.
llvm-svn: 241316
2015-07-03 01:40:14 +00:00
Rui Ueyama 49d6cd35ad COFF: Fix /base option.
Previously, __ImageBase symbol got a different value than the one
specified by /base:<number> because the symbol was created in the
SymbolTable's constructor. When the constructor is called,
no command line options are processed yet, so the symbol was
created always with the initial value. This caused wrong relocations
and thus caused mysterious crashes of some executables linked by LLD.

llvm-svn: 241313
2015-07-03 00:02:19 +00:00
Rui Ueyama 6be9099140 COFF: Define SymbolTable::insert to simplify. NFC.
llvm-svn: 241311
2015-07-02 22:52:33 +00:00
Rui Ueyama 7a333c66be COFF: Fix locally-imported symbols.
Previously, pointers pointed by locally-imported symbols were broken.
It has only 4 bytes although the correct size is 8 byte. This patch
fixes that bug.

llvm-svn: 241295
2015-07-02 20:33:50 +00:00
Rui Ueyama 65813edfe2 COFF: Make symbols satisfy weak ordering.
Previously, SymbolBody::compare(A, B) didn't satisfy weak ordering.
There was a case that A < B and B < A could have been true.
This is because we just pick LHS if A and B are consisdered equivalent.

This patch is to make symbols being weakly ordered. If A and B are
not tie, one of A < B && B > A or A > B && B < A is true.
This is not an improtant property for a single-threaded environment
because everything is deterministic anyways. However, in a multi-
threaded environment, this property becomes important.

If a symbol is defined or lazy, ties are resolved by its file index.
For simple types that we don't really care about their identities,
symbols are compared by their addresses.

llvm-svn: 241294
2015-07-02 20:33:48 +00:00
Rui Ueyama 458d74421b COFF: Merge SymbolTable::find{,Symbol}. NFC
llvm-svn: 241238
2015-07-02 03:59:04 +00:00
Rui Ueyama 85225b0a36 COFF: Infer entry point as early as possible, but not too early.
On Windows, we have four different main functions, {w,}{main,WinMain}.
The linker has to choose a corresponding entry point function among
{w,}{main,WinMain}CRTStartup. These entry point functions are defined
in the standard library. The linker resolves one of them by looking at
which main function is defined and adding a corresponding undefined
symbol to the symbol table.

Object files containing entry point functions conflicts each other.
For example, we cannot resolve both mainCRTStartup and WinMainCRTStartup
because other symbols defined in the files conflict.

Previously, we inferred CRT function name at the very end of name
resolution. I found that that is sometimes too late. If the linker
already linked one of these four archive member objects, it's too late
to change the decision.

The right thing to do here is to infer entry point name after adding
all symbols from command line files and before adding any other files
(which are specified by directive sections). This patch does that.

llvm-svn: 241236
2015-07-02 03:15:15 +00:00
Rui Ueyama 3d4c69c04d COFF: Resolve AlternateNames using weak aliases.
Previously, we use SymbolTable::rename to resolve AlternateName symbols.
This patch is to merge that mechanism with weak aliases, so that we
remove that function.

llvm-svn: 241230
2015-07-02 02:38:59 +00:00
Rui Ueyama 0744e87fad COFF: Rename getReplacement -> repl.
The previous name was too long to my taste.

llvm-svn: 241215
2015-07-02 00:21:11 +00:00
Rui Ueyama 18f8d2c5c0 COFF: Change GCRoot member type from StringRef to Undefined. NFC.
I think Undefined symbols are a bit more convenient than StringRefs
since SymbolBodies are handles for symbols. You can get resolved
symbols for undefined symbols just by calling getReplacmenet without
looking up the symbol table.

llvm-svn: 241214
2015-07-02 00:21:08 +00:00
Rui Ueyama 6bf638e688 COFF: Simplify and rename findMangle. NFC.
Occasionally we have to resolve an undefined symbol to its
mangled symbol. Previously, we did that on calling side of
findMangle by explicitly updating SymbolBody.
In this patch, mangled symbols are handled as weak aliases
for undefined symbols.

llvm-svn: 241213
2015-07-02 00:04:14 +00:00
Rui Ueyama 4897596728 COFF: Chagne weak alias' type from SymbolBody** to SymbolBody*. NFC.
llvm-svn: 241198
2015-07-01 22:32:23 +00:00
Rui Ueyama 4b6698917d COFF: Simplify SymbolTable::findLazy. NFC.
llvm-svn: 241128
2015-06-30 23:46:52 +00:00
Rui Ueyama 8d3010a1a6 COFF: Change the order of adding symbols to the symbol table.
Previously, the order of adding symbols to the symbol table was simple.
We have a list of all input files. We read each file from beginning of
the list and add all symbols in it to the symbol table.

This patch changes that order. Now all archive files are added to the
symbol table first, and then all the other object files are added.
This shouldn't change the behavior in single-threading, and make room
to parallelize in multi-threading.

In the first step, only lazy symbols are added to the symbol table
because archives contain only Lazy symbols. Member object files
found to be necessary are queued. In the second step, defined and
undefined symbols are added from object files. Adding an undefined
symbol to the symbol table may cause more member files to be added
to the queue. We simply continue reading all object files until the
queue is empty.

Finally, new archive or object files may be added to the queues by
object files' directive sections (which contain new command line
options).

The above process is repeated until we get no new files.

Symbols defined both in object files and in archives can make results
undeterministic. If an archive is read before an object, a new member
file gets linked, while in the other way, no new file would be added.
That is the most popular cause of an undeterministic result or linking
failure as I observed. Separating phases of adding lazy symbols and
undefined symbols makes that deterministic. Adding symbols in each
phase should be parallelizable.

llvm-svn: 241107
2015-06-30 19:35:21 +00:00
Peter Collingbourne f7b27d15f2 COFF: Implement SymbolBody::getDebugName() for DefinedBitcode symbols.
Differential Revision: http://reviews.llvm.org/D10827

llvm-svn: 241029
2015-06-30 00:47:52 +00:00
Rui Ueyama c15139bb6d COFF: Make DefinedCOFF one pointer smaller.
The size of this class actually matters because this is the
most popular class among all classes. We create a Defined symbol
for each defined symbol in a symbol table. That can be millions
for a large program. For example, linking LLD instantiates this
class millions times.

llvm-svn: 241025
2015-06-30 00:10:54 +00:00
Peter Collingbourne 79cfd437b5 COFF: Use LTOModule::getLinkerOpts() instead of reading the linker directives ourselves.
llvm-svn: 241020
2015-06-29 23:26:28 +00:00
Rui Ueyama dae1661436 COFF: Split ObjectFile::createSymbolBody into small functions. NFC.
llvm-svn: 241011
2015-06-29 22:16:21 +00:00
Rui Ueyama 579c21537a Move llvm_unreachable out of switch to avoid -Wswitch-covered-defualt.
llvm-svn: 241008
2015-06-29 21:59:34 +00:00
Rui Ueyama 81dd16a1e0 Silence MSVC "not all control paths return a value" warning.
llvm-svn: 241004
2015-06-29 21:46:46 +00:00
Chandler Carruth 64c17c7d67 [opt] Devirtualize the SymbolBody type hierarchy and start compacting
its members into the base class.

First, to help motivate this kind of change, understand that in
a self-link, LLD creates 5.5 million defined regular symbol bodies (and
6 million symbol bodies total). A significant portion of its time is
spent allocating the memory for these symbols, and befor ethis patch
the defined regular symbol body objects alone consumed some 420mb of
memory during the self link.

As a consequence, I think it is worth expending considerable effort to
make these objects as memory efficient as possible. This is the first of
several components of that. This change starts with the goal of removing
the virtual functins from SymbolBody so that it can avoid having a vptr
embedded in it when it already contains a "kind" member, and that member
can be much more compact than a vptr.

The primary way of doing this is to sink as much of the logic that we
would have to dispatch for into data in the base class. As part of this,
I made the various flags bits that will pack into a bitfield with the
kind tag. I also sank the Name down to eliminate the dispatch for that,
and used LLVM's RTTI-style dispatch for everything else (most of which
is cold and so doesn't matter terribly if we get minutely worse lowering
than a vtable dispatch).

As I was doing this, I wanted to make the RTTI-dispatch (which would
become much hotter than before) as efficient as possible, so I've
re-organized the tags somewhat. Notably, the common case (regular
defined symbols) is now zero which we can test for faster.

I also needed to rewrite the comparison routine used during resolving
symbols. This proved to be quite complex as the semantics of the
existing one were very subtle due to the back-and-forth virtual dispatch
caused by re-dispatching with reversed operands. I've consolidated it to
a single function and tried to comment it quite a bit more to help
explain what is going on. However, this may need more comments or other
explanations. It at least passes all the regression tests. I'm not
working on Windows, so I can't fully test it.

With all of these changes, the size of a DefinedRegular symbol on
a 64-bit build goes from 80 bytes to 64 bytes, and we save approximately
84mb or 20% of the memory consumed by these symbol bodies during the
link.

The link time appears marginally faster as well, and the profile hotness
of the memory allocation subsystem got a bit better, but there is still
a lot of allocation traffic.

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

llvm-svn: 241001
2015-06-29 21:35:48 +00:00
Chandler Carruth ee5bf526eb [cleanup] Clean up the flow of creating a symbol body for regular symbols.
This uses a single cast and test to get the section for the symbol, and
uses the cast_or_null<> pattern throughout to handle the known type but
unknown non-null-ness.

No functionality changed.

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

llvm-svn: 241000
2015-06-29 21:32:37 +00:00
Chandler Carruth 59013c387e [opt] Replace the recursive walk for GC with a worklist algorithm.
This flattens the entire liveness walk from a recursive mark approach to
a worklist approach. It also sinks the worklist management completely
out of the SectionChunk and into the Writer by exposing the ability to
iterato over children of a chunk and over the symbol bodies of relocated
symbols. I'm not 100% happy with the API names, so suggestions welcome
there.

This allows us to use a single worklist for the entire recursive walk
and would also be a natural place to take advantage of parallelism at
some future point.

With this, we completely inline away the GC walk into the
Writer::markLive function and it makes it very easy to profile what is
slow. Currently, time is being wasted checking whether a Chunk isa
SectionChunk (it essentially always is), finding (or skipping)
a replacement for a symbol, and chasing pointers between symbols and
their chunks. There are a bunch of things we can do to fix this, and its
easier to do them after this change IMO.

This change alone saves 1-2% of the time for my self-link of lld.exe
(which I'm running and benchmarking on Linux ironically).

Perhaps more notably, we'll no longer blow out the stack for large
links. =]

Just as an FYI, at this point, I/O is starting to really dominate the
profile. Well over 10% of the time appears to be inside the kernel doing
page table silliness. I think a decent chunk of this can be nuked as
well, but it's a little odd as cross-linking in this way isn't really
the primary goal here.

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

llvm-svn: 240995
2015-06-29 21:12:49 +00:00
Chandler Carruth be6e80b012 [opt] Hoist the call throuh SymbolBody::getReplacement out of the inline
method to get a SymbolBody and into the callers, and kill now dead
includes.

This removes the need to have the SymbolBody definition when we're
defining the inline method and makes it a better inline method. That was
the only reason for a lot of header includes here. Removing these and
using forward declarations actually uncovers a bunch of cross-header
dependencies that I've fixed while I'm here, and will allow me to
introduce some *important* inline code into Chunks.h that requires the
definition of ObjectFile.

No functionality changed at this point.

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

llvm-svn: 240982
2015-06-29 18:50:11 +00:00
Rui Ueyama 2d5e917bce COFF: Handle mangled entry symbol name.
Compilers recognize "main" function and don't mangle its name.
But if you use a different function as a user-defined entry name,
and if you didn't define that function with extern C, your entry
point function name is mangled. And the linker has to be able to
find that. This is relatively rare but can happen.

llvm-svn: 240953
2015-06-29 14:43:07 +00:00
Rui Ueyama 0fc26d21bd COFF: Create an empty file for /pdb.
Most build system depends on existence or time stamp of a file.
This patch is to create an empty file for /pdb:<filename> option
just to satisfy some build rules.

llvm-svn: 240948
2015-06-29 14:27:12 +00:00