Commit Graph

235 Commits

Author SHA1 Message Date
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
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
Rui Ueyama f00df0af2d COFF: Fix precedence between LIB and /libpath.
/libpath should take precedence over LIB.
Previously, LIB took precedence over /libpath.

llvm-svn: 240182
2015-06-19 22:39:48 +00:00
Rui Ueyama 165b254e06 COFF: Add search paths in the correct order.
Previously, we added search paths in reverse order.

llvm-svn: 240180
2015-06-19 21:44:32 +00:00
Rui Ueyama 29792a82a9 COFF: Cache Archive::Symbol::getName(). NFC.
getName() does strlen() on the symbol table, so it's not very fast.
It's not as bad as r239332 because the number of symbols exported
from archive files are fewer than object files, and they are usually
shorter, though.

llvm-svn: 240178
2015-06-19 21:25:44 +00:00
Rui Ueyama 573bf7de9c COFF: Continue reading object files until converge.
In this linker model, adding an undefined symbol may trigger chain
reactions. It may trigger a Lazy symbol to read a new file.
A new file may contain a directive section, which may contain various
command line options.

Previously, we didn't handle chain reactions well. We visited /include'd
symbols only once, so newly-added /include symbols were ignored.
This patch fixes that bug.

Now, the symbol table is versioned; every time the symbol table is
updated, the version number is incremented. We repeat adding undefined
symbols until the version number does not change. It is guaranteed to
converge -- the number of undefined symbol in the system is finite,
and adding the same undefined symbol more than once is basically no-op.

llvm-svn: 240177
2015-06-19 21:12:48 +00:00
Rui Ueyama 4d2834bd7b COFF: Don't add new undefined symbols for /alternatename.
Alternatename option is in the form of /alternatename:<from>=<to>.
It's effect is to resolve <from> as <to> if <from> is still undefined
at end of name resolution.

If <from> is not undefined but completely a new symbol, alternatename
shouldn't do anything. Previously, it introduced a new undefined
symbol for <from>, which resulted in undefined symbol error.

llvm-svn: 240161
2015-06-19 19:23:43 +00:00
Rui Ueyama ce86c9962c COFF: Add /nodefaultlib and /merge for .drectve.
llvm-svn: 240077
2015-06-18 23:22:39 +00:00
Rui Ueyama 08d5e1875f COFF: Handle /include in .drectve.
We don't want to insert a new symbol to the symbol table while reading
a .drectve section because it's going to be too complicated.
That we are reading a directive section means that we are currently
reading some object file. Adding a new undefined symbol to the symbol
table can trigger a library file to read a new file, so it would make
the call stack too deep.

In this patch, I add new symbol names to a list to resolve them later.

llvm-svn: 240076
2015-06-18 23:20:11 +00:00
Rui Ueyama e8d56b5258 COFF: Allow identical alternatename options.
Alternatename option is in the form of /alternatename:<from>=<to>.
It is an error if there are two options having the same <from> but
different <to>. It is *not* an error if both are the same.

llvm-svn: 240075
2015-06-18 23:04:26 +00:00
Rui Ueyama 562daa8148 COFF: Unknown options in .drectve section is an error.
We skip unknown options in the command line with a warning message
being printed out, but we shouldn't do that for .drectve section.
The section is not visible to the user. We should handle unknown
options as an error.

llvm-svn: 240067
2015-06-18 21:50:38 +00:00
Rui Ueyama 75b098b29d COFF: Handle /failifmismatch in the same manner as other options.
No functionality change intended.

llvm-svn: 240061
2015-06-18 21:23:34 +00:00
Rui Ueyama 223fe1b9e7 COFF: Fix unsafe memory access.
llvm-svn: 240046
2015-06-18 20:29:41 +00:00
Rui Ueyama b95188cb2c COFF: Add /implib option.
llvm-svn: 240045
2015-06-18 20:27:09 +00:00
Rui Ueyama ea63a28364 COFF: Handle both / and \ as path separator.
llvm-svn: 240042
2015-06-18 20:16:26 +00:00
Rui Ueyama 2edb35a264 COFF: Handle /alternatename in .drectve section.
llvm-svn: 240037
2015-06-18 19:09:30 +00:00
Rui Ueyama 23ed96d95f COFF: Rename a function. NFC.
llvm-svn: 240031
2015-06-18 17:29:50 +00:00
Peter Collingbourne 8b2492f2a0 COFF: Implement DLL symbol exports for bitcode files.
Differential Revision: http://reviews.llvm.org/D10530

llvm-svn: 239994
2015-06-18 05:22:15 +00:00
Rui Ueyama ae36985af7 COFF: Fix entry point inference bug.
Previously, LLD couldn't find a default entry point if it's
defined by a library.

llvm-svn: 239982
2015-06-18 00:40:33 +00:00
Rui Ueyama 24c5fd0419 COFF: Support /manifest{,uac,dependency,file} options.
The linker has to create an XML file for each executable.
This patch supports that feature.

You can optionally embed an XML file to an executable as .rsrc
section. If you choose to do that (by passing /manifest:embed
option), the linker has to create a textual resource file
containing an XML file, compile that using rc.exe to a binary
resource file, conver that resource file to a COFF file using
cvtres.exe, and then link that COFF file. This patch implements
that feature too.

llvm-svn: 239978
2015-06-18 00:12:42 +00:00
Rui Ueyama a9c8838f69 COFF: Simplify. NFC.
Executor is a convenience class to run an external command.

llvm-svn: 239945
2015-06-17 21:01:56 +00:00
Rui Ueyama 151d862d97 COFF: Create import library files.
On Windows, we have to create a .lib file for each .dll.
When linking against DLLs, the linker doesn't use the DLL files,
but instead read a list of dllexported symbols from corresponding
lib files.

A library file containing descriptors of a DLL is called an
import library file.

lib.exe has a feature to create an import library file from a
module-definition file. In this patch, we create a module-definition
file and pass that to lib.exe.

We eventually want to create an import library file by ourselves
to eliminate dependency to lib.exe. For now, we just use the MSVC
tool.

llvm-svn: 239937
2015-06-17 20:40:43 +00:00
Rui Ueyama 1f373704e3 COFF: Support module-definition files.
Module-definition files (.def files) are yet another way to
specify parameters to the linker. You can write a list of dllexported
symbols in module-definition files instead of using /export command
line option. It also supports a few more directives.

The parser code is taken from lib/Driver/WinLinkModuleDef.cpp
with the following modifications.

 - variable names are updated to comply with the LLVM coding style.
 - Instead of returning parsing results as "directive" objects,
   it updates Config object directly.

llvm-svn: 239929
2015-06-17 19:19:25 +00:00
Rui Ueyama 97dff9ee3a COFF: Support creating DLLs.
DLL files are in the same format as executables but they have export tables.
The format of the export table is described in PE/COFF spec section 5.3.

A new class, EdataContents, takes care of creating chunks for export tables.
What we need to do is to parse command line flags for dllexports, and then
instantiate the class to create chunks. For the writer, export table chunks
are opaque data -- it just add chunks to .edata section.

llvm-svn: 239869
2015-06-17 00:16:33 +00:00
Rui Ueyama 6592ff8c93 COFF: Add miscellaneous boolean flags.
llvm-svn: 239864
2015-06-16 23:13:00 +00:00
Rui Ueyama e25147626c COFF: Simplify SymbolBody::compare(SymbolBody *Other).
We are currently handling all combinations of SymbolBody types directly.
This patch is to flip this and Other if Other->kind() < this->kind()
to reduce number of combinations. No functionality change intended.

llvm-svn: 239745
2015-06-15 19:06:53 +00:00
Rui Ueyama bc2cc7d0b8 COFF: Fix .reloc section attributes.
llvm-svn: 239738
2015-06-15 18:03:47 +00:00
Rui Ueyama 6200b6d593 COFF: Update README.
llvm-svn: 239734
2015-06-15 16:25:11 +00:00
Rui Ueyama f3770d3edb COFF: Use ulittle32_t::operator|=. NFC.
llvm-svn: 239717
2015-06-15 03:03:23 +00:00
Rui Ueyama 095409e9e8 COFF: Add a brief description about LTO.
llvm-svn: 239714
2015-06-15 02:46:18 +00:00
Rui Ueyama 59e9578f20 COFF: Fix resource table size.
The size field shouldn't include trailing padding.

llvm-svn: 239712
2015-06-15 01:35:56 +00:00
Rui Ueyama 588e832d0a COFF: Support base relocations.
PE/COFF executables/DLLs usually contain data which is called
base relocations. Base relocations are a list of addresses that
need to be fixed by the loader if load-time relocation is needed.

Base relocations are in .reloc section.

We emit one base relocation entry for each IMAGE_REL_AMD64_ADDR64
relocation.

In order to save disk space, base relocations are grouped by page.
Each group is called a block. A block starts with a 32-bit page
address followed by 16-bit offsets in the page. That is more
efficient representation of addresses than just an array of 32-bit
addresses.

llvm-svn: 239710
2015-06-15 01:23:58 +00:00
Rui Ueyama 9a03362a08 COFF: Change const name. NFC.
llvm-svn: 239707
2015-06-14 22:21:29 +00:00
Rui Ueyama 669236fef3 COFF: Set Chunk to OutputSection backreference in addChunk().
When we add a chunk to an OutputSection, we always want to create
a backreference from an OutputSection to a Chunk. To make sure
we always do, do that in addChunk(). NFC.

llvm-svn: 239706
2015-06-14 22:16:47 +00:00
Rui Ueyama 4108f3f393 COFF: Add an assertion. NFC.
r239458 changed callee side of this function, so Live can never be
true when this function is called.

llvm-svn: 239705
2015-06-14 22:01:39 +00:00
Rui Ueyama 2bf6a12238 COFF: Support Windows resource files.
Resource files are data files containing i18n messages, icon images, etc.
MSVC has a tool to convert a resource file to a regular COFF file so that
you can just link that file to embed resources to an executable.

However, you can directly pass resource files to the linker. If you do that,
the linker invokes the tool automatically. This patch implements that feature.

llvm-svn: 239704
2015-06-14 21:50:50 +00:00
Rafael Espindola 9bd82e9952 Update for llvm api change.
llvm-svn: 239671
2015-06-13 12:50:13 +00:00
Davide Italiano d106ab263a [COFF] Spell the namespace correctly.
llvm-svn: 239641
2015-06-12 21:37:55 +00:00
Peter Collingbourne 1b6fd1f5fd COFF: Symbol resolution for common and comdat symbols defined in bitcode.
In the case where either a bitcode file and a regular file or two bitcode
files export a common or comdat symbol with the same name, the linker needs
to pick one of them following COFF semantics. This patch implements a design
for resolving such symbols that pushes most of the work onto either LLD's
regular mechanism for resolving common or comdat symbols or the IR linker's
mechanism for doing the same.

We modify SymbolBody::compare to always prefer non-bitcode symbols, so that
during the initial phase of symbol resolution, the symbol table always contains
a regular symbol in any case where we need to choose between a regular and
a bitcode symbol. In SymbolTable::addCombinedLTOObject, we force export
any bitcode symbols that were initially pre-empted by a regular symbol,
and later use SymbolBody::compare to choose between the regular symbol in
the symbol table and the regular symbol from the combined LTO object file.

This design seems to be sound, so long as the resolution mechanism is defined
to be commutative and associative modulo arbitrary choices between symbols
(which seems to be the case for COFF).

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

llvm-svn: 239563
2015-06-11 21:49:54 +00:00
Rui Ueyama 8b33f59bfd COFF: De-virtualize and inline garbage collector functions.
isRoot, isLive and markLive functions are called very frequently.
Previously, they were virtual functions. This patch make them
non-virtual.

Also this patch checks chunk liveness before calling its mark().
Previously, we did that at beginning of markLive(), so the virtual
function would return immediately if it's live. That was inefficient.

llvm-svn: 239458
2015-06-10 04:21:47 +00:00
Peter Collingbourne bd1cb792d3 COFF: Implement /lib using LibDriver.
Differential Revision: http://reviews.llvm.org/D10347

llvm-svn: 239436
2015-06-09 21:52:48 +00:00
Rui Ueyama efba7812cc COFF: Split SymbolTable::addCombinedLTOObject. NFC.
llvm-svn: 239418
2015-06-09 17:52:17 +00:00
Rui Ueyama 0e77d227f8 COFF: Update comment.
llvm-svn: 239413
2015-06-09 16:52:56 +00:00
Peter Collingbourne 73b75e3d0c COFF: Handle references from LTO object to lazy symbols correctly.
The code generator may create references to runtime library symbols such as
__chkstk which were not visible via LTOModule. Handle these cases by loading
the object file from the library, but abort if we end up having loaded any
bitcode objects.

Because loading the object file may have introduced new undefined references,
call reportRemainingUndefines again to detect and report them.

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

llvm-svn: 239386
2015-06-09 04:29:54 +00:00
Peter Collingbourne d9e4e98cce COFF: Allow the combined LTO object to define new symbols.
The LLVM code generator can sometimes synthesize symbols, such as SSE
constants, that are not visible via the LTOModule interface. Allow such
symbols so long as they have definitions.

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

llvm-svn: 239385
2015-06-09 02:53:09 +00:00
Peter Collingbourne df637ea289 COFF: Skip internal symbols in bitcode files.
Differential Revision: http://reviews.llvm.org/D10319

llvm-svn: 239338
2015-06-08 20:21:28 +00:00
Rui Ueyama 57fe78d339 COFF: Read symbol names lazily.
This change seems to make the linker about 10% faster.
Reading symbol name is not very cheap because it needs strlen()
on the string table. We were wasting time on reading non-external
symbol names that would never be used by the linker.

llvm-svn: 239332
2015-06-08 19:43:59 +00:00
Rui Ueyama f533d3e09d COFF: Avoid callign stable_sort.
MSVC profiler reported that this stable_sort takes 7% time
when self-linking. As a result, createSection was taking 10% time.
Now createSection takes 3%. This small change actually makes
the linker a bit but perceptibly faster.

llvm-svn: 239292
2015-06-08 08:26:28 +00:00
Rui Ueyama 7d80640f25 COFF: Use the empty string as the current directory instead of ".".
This is NFC but makes log message a bit nicer because it doesn't
append .\ (or ./ on Unix) to files in the current directory.

llvm-svn: 239290
2015-06-08 06:13:12 +00:00
Rui Ueyama eeae5ddbe2 COFF: Add more log messages.
llvm-svn: 239289
2015-06-08 06:00:10 +00:00
Rui Ueyama 5b2588ae8c COFF: Print out log messages to stdout.
llvm-svn: 239288
2015-06-08 05:43:50 +00:00
Rui Ueyama 80141a4bcd COFF: Check for auxiliary symbol's type.
We forgot to check for auxiliary symbol's type. So we sometimes read
garbage as associative section definitions.

Associative sections are considered as not live themselves by the
garbage collector because they are live only when associaited sections
are live.

By reading more data (or garbage) as associative section definitions,
we treated more sections as non-GC-roots, that caused the linker to
discard too many sections by mistake. That caused another mysterious
bug (such as some global constructors don't run at all for some reason.)

llvm-svn: 239287
2015-06-08 05:00:42 +00:00
Rui Ueyama f811472b4c COFF: Simplify InputFile class.
Now that all InputFile subclasses have MemoryBufferRefs and
provides the same set of functions. Implement that in the base class.

llvm-svn: 239281
2015-06-08 03:27:57 +00:00
Rui Ueyama 9cf1abb8d4 COFF: Set non-1 alignment to common chunks.
I don't know what the right thing to do here, but at least 1 does
not seem like a correct value. If we do not align common chunks at
all, a small program which calls puts() from global dtors crashes
mysteriously in a kernel32's function.

I believe the crash was caused by symbols overlapping each other,
and my guess is that alignment has something to do with that, but
I am not 100% sure. Needs investigating.

llvm-svn: 239280
2015-06-08 03:17:07 +00:00
Rui Ueyama b4f791b510 COFF: Fix memory leak.
llvm-svn: 239272
2015-06-08 00:09:25 +00:00
Rui Ueyama a6cd6c0cd8 COFF: Fix typo.
This change doesn't change its functionality since the value
passed here is converted to uint16_t immediately.

llvm-svn: 239271
2015-06-07 23:10:50 +00:00
Rui Ueyama aace577e3e COFF: Simplify. NFC.
llvm-svn: 239270
2015-06-07 23:02:50 +00:00
Rui Ueyama b51f67a175 COFF: Use llvm:🆑:ExpandReponseFiles.
llvm-svn: 239269
2015-06-07 23:00:29 +00:00
Rui Ueyama 0c35b38fd2 COFF: Add a glossary to README.
llvm-svn: 239268
2015-06-07 22:42:52 +00:00
Rui Ueyama c6b87363a1 COFF: Use named constants instead of sizeof().
llvm-svn: 239267
2015-06-07 22:00:28 +00:00
Rui Ueyama b3aa5e71a0 COFF: Remove dead code.
/include'ed symbols are already added to Config->GCRoots.
Marking symbols twice doesn't have any effect.

llvm-svn: 239266
2015-06-07 21:58:34 +00:00
Rui Ueyama 94df713199 COFF: Make local variables local.
llvm-svn: 239244
2015-06-07 03:55:28 +00:00
Rui Ueyama e2cbfeae5c COFF: Add /opt:noref option.
This option disables dead-stripping.

llvm-svn: 239243
2015-06-07 03:17:42 +00:00
Rui Ueyama 115d7c1036 COFF: Support resonpse files.
llvm-svn: 239242
2015-06-07 02:55:19 +00:00
Rui Ueyama 4b22fa7437 COFF: Move Windows-specific code from Chunk.{cpp,h} to DLL.{cpp,h}.
llvm-svn: 239239
2015-06-07 01:15:04 +00:00
Rui Ueyama ad66098c20 COFF: Fix default output file path.
Default output filename is the same as the first object file's
name with its extension replaced with ".exe".

llvm-svn: 239238
2015-06-07 00:20:32 +00:00
Rui Ueyama 4a9fbbca9f COFF: Add comments and move main function to the top. NFC.
llvm-svn: 239237
2015-06-06 23:32:08 +00:00
Rui Ueyama cc608e4f35 COFF: Rename writeHeader -> writeHeaderTo.
Chunk has writeTo function which takes uint8_t *Buf.
writeHeaderTo feels more consistent with that because this member
function also takes uint8_t *Buf.

llvm-svn: 239236
2015-06-06 23:19:38 +00:00
Rui Ueyama 929d8c52b1 COFF: Inline a constant that is used only once.
llvm-svn: 239235
2015-06-06 23:19:36 +00:00
Rui Ueyama e56f9c0883 Remove redundant `using namespace`.
llvm-svn: 239234
2015-06-06 23:11:39 +00:00
Rui Ueyama 55168c9f70 COFF: Add .didat section.
llvm-svn: 239233
2015-06-06 23:07:01 +00:00
Rui Ueyama 458df98869 COFF: Update comments.
llvm-svn: 239232
2015-06-06 22:56:55 +00:00
Rui Ueyama c6ea057d7f COFF: Move .idata constructor from Writer to Chunk.
Previously, half of the constructor for .idata contents was in Chunks.cpp
and the rest was in Writer.cpp. This patch moves the latter to Chunks.cpp.
Now IdataContents class manages everything for .idata section.

llvm-svn: 239230
2015-06-06 22:46:15 +00:00
Rui Ueyama 743afa0736 COFF: Merge Chunk::applyRelocations with Chunk::writeTo.
In this design, Chunk is the only thing that knows how to write
its contents to output file as well as how to apply relocations
there. The writer shouldn't know about the details.

llvm-svn: 239216
2015-06-06 04:07:39 +00:00
Peter Collingbourne ace2f091fd COFF: Read linker directives from bitcode files.
Differential Revision: http://reviews.llvm.org/D10285

llvm-svn: 239212
2015-06-06 02:00:45 +00:00
Rui Ueyama 8854d8a6f1 COFF: Add /failifmismatch option.
llvm-svn: 239073
2015-06-04 19:21:24 +00:00
Rui Ueyama 2ba7908cf9 Add comments.
llvm-svn: 239072
2015-06-04 19:21:22 +00:00
Rui Ueyama eb262ce4b6 COFF: /include'd symbols must be preserved.
Not only entry point symbol but also symbols specified by /include
option must be preserved, as they will never be dead-stripped.

http://reviews.llvm.org/D10220

llvm-svn: 239005
2015-06-04 02:12:16 +00:00
Rui Ueyama 2d7627198f Fix typo.
llvm-svn: 238937
2015-06-03 16:50:41 +00:00
Rui Ueyama bda72a4af4 COFF: Change OutputSections' type from vector<unique_ptr<T>> to vector<T*>.
This is mainly for readability. OutputSection objects are still owned
by the writer using SpecificBumpPtrAllocator.

llvm-svn: 238936
2015-06-03 16:44:00 +00:00
Rui Ueyama 652052b82c COFF: Update README.
Avoid saying this is based on sections because it's not very accurate.
That we don't split section into smaller chunks of data does not mean
that the linker is built on top of that.
In reality, most part of the code do not care about underlying data,
so they are neither based on "atoms" nor sections.
The symbol table only cares about symbol names and their types.
The writer handles list of chunks, which look like just blobs,
and the writer doesn't care what those chunks are backed by.
The only thing that interact with sections is SectionChunk, which is
abstracted away as one type of Chunk.

llvm-svn: 238902
2015-06-03 05:39:13 +00:00
Rui Ueyama 07e661f8cd COFF: SymbolTable to manage symbols using BumpPtrAllocator.
llvm-svn: 238901
2015-06-03 05:39:12 +00:00
Rui Ueyama 1db1ef9ab4 Use reinterpret_cast instead of const_cast and C-style cast.
llvm-svn: 238786
2015-06-01 21:49:21 +00:00
Rui Ueyama 81b030cbf6 COFF: Remove BitcodeFile::BitcodeFile(StringRef Filename).
In r238690, I made all files have only MemoryBufferRefs. This change
is to do the same thing for the bitcode file reader. Also updated
a few variable names to match with other code.

llvm-svn: 238782
2015-06-01 21:19:43 +00:00
Rui Ueyama fd99e01b91 COFF: Support import-by-ordinal DLL imports.
Symbols exported by DLLs can be imported not by name but by
small number or ordinal. Usually, symbols have both ordinals
and names, and in that case ordinals are called "hints" and
used by the loader as hints.

However, symbols can have only ordinals. They are called
import-by-ordinal symbols. You need to manage ordinals by hand
so that they will never change if you choose to use the feature.
But it's supposed to make dynamic linking faster because
it needs no string comparison. Not sure if that claim still
stands in year 2015, though. Anyways, the feature exists,
and this patch implements that.

llvm-svn: 238780
2015-06-01 21:05:27 +00:00
Rui Ueyama c2abdd9152 COFF: Use Chunk instead of its derived classes.
I'm adding ordinal-only (nameless) imports to the import table.
The chunk for that type is going to be different from LookupChunk.
Without this change, we cannot add objects of the new type to the
vectors.

llvm-svn: 238779
2015-06-01 21:05:24 +00:00
Peter Collingbourne 60c1616613 COFF: Initial implementation of link-time optimization.
This implementation is known to work in very simple cases (see new test case).

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

llvm-svn: 238777
2015-06-01 20:10:10 +00:00
Denis Protivensky 6833690402 COFF: Fix warnings found by gcc
llvm-svn: 238734
2015-06-01 09:26:32 +00:00
Denis Protivensky 1c43df7481 COFF: Better noexcept specification with LLVM_NOEXCEPT
This is a follow-on to r238732

llvm-svn: 238733
2015-06-01 09:08:11 +00:00
Denis Protivensky c44223ed96 COFF: Add noexcept to std::error_category::name
This fixes build error with gcc.

llvm-svn: 238732
2015-06-01 08:12:44 +00:00
Rui Ueyama 5b25edddfe COFF: Fix the import table Hint/Name field.
llvm-svn: 238719
2015-06-01 03:55:04 +00:00
Rui Ueyama 68216c680d Fix comments.
llvm-svn: 238718
2015-06-01 03:55:02 +00:00
Rui Ueyama 78aefcb238 COFF: Fix /include. Included symbols are GC-roots.
llvm-svn: 238717
2015-06-01 03:42:54 +00:00
Rui Ueyama 8fd9fb9857 COFF: Define an error category for the linker.
Instead of returning non-categorized errors, return categorized errors.
All uses of make_dynamic_error_code are removed.

Because we don't have error reporting mechanism, I just chose to print out
error messages to stderr, and then return an error object. Not sure if
that's the right thing to do, but at least it seems practical.

http://reviews.llvm.org/D10129

llvm-svn: 238714
2015-06-01 02:58:15 +00:00
Rui Ueyama 360bace8eb COFF: Add /alternatename option.
Previously, this feature was implemented using a special type of
undefined symbol, in addition to an intricate way to make the resolver
read a virtual file containing that renaming symbols.

Now the feature is directly handled by the symbol table.
The symbol table has a function, rename(), to rename symbols, whose
definition is 4 lines long. Symbol renaming is naturally modeled using
Symbol and SymbolBody.

llvm-svn: 238696
2015-05-31 22:31:31 +00:00
Rui Ueyama 711cd2d7c8 COFF: Detect file type by file magic.
llvm-svn: 238691
2015-05-31 21:17:10 +00:00
Rui Ueyama d7c2f5847a COFF: Make the Driver own all MemoryBuffers. NFC.
Previously, a MemoryBuffer of a file was owned by each InputFile object.
This patch makes the Driver own all of them. InputFiles now have only
MemoryBufferRefs. This change simplifies ownership managment
(particularly for ObjectFile -- the object owned a MemoryBuffer only when
it's not created from an archive file, because in that case a parent
archive file owned the entire buffer. Now it owns nothing unconditionally.)

llvm-svn: 238690
2015-05-31 21:04:56 +00:00
Rui Ueyama f4784cce54 COFF: /libpath should not take precedence over the current directory.
llvm-svn: 238683
2015-05-31 20:20:37 +00:00
Rui Ueyama 0613747e1a COFF: Add /libpath option.
llvm-svn: 238682
2015-05-31 20:10:11 +00:00
Rui Ueyama e042fa9aa5 COFF: Add /include option.
It does not involve notions of virtual archives or virtual files,
nor store a list of undefined symbols somewhere else to consume them later.
We did that before. In this patch, undefined symbols are just added to
the symbol table, which now can be done in very few lines of code.

llvm-svn: 238681
2015-05-31 19:55:40 +00:00
Rui Ueyama d21b00bd7c COFF: Add /nodefaultlib option.
llvm-svn: 238679
2015-05-31 19:17:14 +00:00
Rui Ueyama 54b71daec4 COFF: Refactor functions to find files from search paths.
llvm-svn: 238678
2015-05-31 19:17:12 +00:00
Rui Ueyama a9cbbf885f COFF: Create LinkerDriver class.
Previously the main linker routine is just a non-member function.
We store some context information to the Config object.
This patch makes it belong to Driver.

llvm-svn: 238677
2015-05-31 19:17:09 +00:00
Rui Ueyama 80b5689d91 COFF: Use range-based for loop.
llvm-svn: 238675
2015-05-31 16:10:50 +00:00
Rui Ueyama d68ff34ad2 Fix unsafe memory access.
llvm-svn: 238669
2015-05-31 03:57:30 +00:00
Rui Ueyama 3ee0fe4c2c COFF: Implement subsystem inference.
llvm-svn: 238668
2015-05-31 03:55:46 +00:00
Rui Ueyama 5cff68599d COFF: Infer entry symbol name if /entry is not given.
`main` is not the only main function in Windows. You can choose one
from these four -- {w,}{WinMain,main}. There are four different entry
point functions for them, {w,}{WinMain,main}CRTStartup, respectively.
The linker needs to choose the right one depending on which `main`
function is defined.

llvm-svn: 238667
2015-05-31 03:34:08 +00:00
Rui Ueyama e00d651071 Use initializer instead of memset to zero out.
llvm-svn: 238662
2015-05-30 19:28:58 +00:00
Rui Ueyama bfb4aa1791 COFF: Support long section name.
Section names were truncated to 8 bytes because the section table's
name field is 8 byte long. This patch creates the string table to
store long names.

llvm-svn: 238661
2015-05-30 19:09:50 +00:00
Peter Collingbourne 246ccc5f51 COFF: Move machine type auto-detection to SymbolTable.
The new mechanism is less code, and fixes the case where all inputs
are archives.

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

llvm-svn: 238618
2015-05-29 21:47:36 +00:00
Rui Ueyama 15cc47ee81 COFF: Add /subsystem option.
llvm-svn: 238571
2015-05-29 16:34:31 +00:00
Rui Ueyama b9dcdb5fc9 COFF: Add /version option.
llvm-svn: 238570
2015-05-29 16:28:29 +00:00
Rui Ueyama c377e9aefe COFF: Add /heap option.
llvm-svn: 238569
2015-05-29 16:23:40 +00:00
Rui Ueyama b41b7e5a69 Add /stack option.
llvm-svn: 238568
2015-05-29 16:21:11 +00:00
Rui Ueyama 804a8b6361 COFF: Add /base option.
llvm-svn: 238567
2015-05-29 16:18:15 +00:00
Rui Ueyama 5c726433d2 COFF: Add /help option.
llvm-svn: 238565
2015-05-29 16:11:52 +00:00
Rui Ueyama 3d3e6fba6e COFF: Add /machine option.
llvm-svn: 238564
2015-05-29 16:06:00 +00:00
Rui Ueyama 7c4fcdd559 COFF: Move Windows-specific function under Windows-specific marker.
llvm-svn: 238563
2015-05-29 15:49:09 +00:00
Rui Ueyama c9bfe32010 COFF: Fill imort table HintName field.
Currently we set the field to zero, but as per the spec, we should
set numbers we read from import library files. The loader uses the
values as starting offsets for binary search when looking up imported
symbols from DLL.

llvm-svn: 238562
2015-05-29 15:45:35 +00:00
Rui Ueyama 322b2c413d COFF: Return an error_code directly.
llvm-svn: 238486
2015-05-28 20:39:29 +00:00
Rui Ueyama 3500f6667a COFF: Split Driver.cpp to Driver.cpp and DriverUtils.cpp. NFC.
The previous implementation's driver file is cluttered by lots of
small functions, and it was hard to find important functions.
Make a separate file to prevent that issue.

llvm-svn: 238482
2015-05-28 20:30:06 +00:00
Rui Ueyama d52824d361 Rename InputFile::Name -> InputFile::Filename.
Other local variables shadowed the member variable.
Rename to make that a bit longer.

llvm-svn: 238478
2015-05-28 20:16:25 +00:00
Rui Ueyama 9aefa0c6b9 Fix non-debug build.
llvm-svn: 238474
2015-05-28 20:04:51 +00:00
Rui Ueyama d6fefba447 COFF: Teach Chunk to write to a mmap'ed output file.
Previously Writer directly handles writes to a file.
Chunks needed to give Writer a continuous chunk of memory.
That was inefficent if you construct data in chunks because
it would require two memory copies (one to construct a chunk
and the other is to write that to a file).

This patch teaches chunk to write directly to a file.
From readability point of view, this is also good because
you no longer have to call hasData() before calling getData().

llvm-svn: 238464
2015-05-28 19:45:43 +00:00
Rui Ueyama 411c636081 COFF: Add a new PE/COFF port.
This is an initial patch for a section-based COFF linker.

The patch has 2300 lines of code including comments and blank lines.
Before diving into details, you want to start from reading README
because it should give you an overview of the design.

All important things are written in the README file, so I write
summary here.

- The linker is already able to self-link on Windows.

- It's significantly faster than the existing implementation.
  The existing one takes 5 seconds to link LLD on my machine,
  while the new one only takes 1.2 seconds, even though the new
  one is not multi-threaded yet. (And a proof-of-concept multi-
  threaded version was able to link it in 0.5 seconds.)

- It uses much less memory (250MB vs. 2GB virtual memory space
  to self-host).

- IMHO the new code is much simpler and easier to read than
  the existing PE/COFF port.

http://reviews.llvm.org/D10036

llvm-svn: 238458
2015-05-28 19:09:30 +00:00