Commit Graph

117 Commits

Author SHA1 Message Date
Saleem Abdulrasool 9f8bc029bd COFF: hoist a local variable
Create a local variable for the rdata section.  NFC.

llvm-svn: 279407
2016-08-21 23:05:43 +00:00
Saleem Abdulrasool 15f6f6cb97 COFF: reorder the table construction
Reorder the table setup to mirror the indices corresponding to them.  This means
that the table values are filled out as per the enumeration ordering.  Doing so
makes it easier to identify a particular table.  NFC.

llvm-svn: 278199
2016-08-10 04:37:56 +00:00
Rui Ueyama bb57954241 COFF: Update error messages so that they start with lowercase letters.
llvm-svn: 275513
2016-07-15 01:12:24 +00:00
Rui Ueyama 659a4f2f38 Make check() always return a value.
Previously, one of two check functions didn't return a value.
It was confusing. This patch makes both functions return values.

llvm-svn: 275511
2016-07-15 01:06:38 +00:00
Rui Ueyama 0d09a865c6 COFF: Remove `void error()` functions and use fatal instead.
This change makes the control flow more explicit.

llvm-svn: 275504
2016-07-15 00:40:46 +00:00
Rui Ueyama 1a3fd13776 COFF: Remove unnecessary explicit calls of Twine ctor.
llvm-svn: 275501
2016-07-14 23:43:36 +00:00
Rui Ueyama 606047939c COFF: Rename noreturn error -> fatal.
This new name is also consistent with ELF.

llvm-svn: 275500
2016-07-14 23:37:14 +00:00
Rui Ueyama 7e75866c51 COFF: Rename non-noreturn error -> check.
The new name is consistent with ELF.

llvm-svn: 275499
2016-07-14 23:37:10 +00:00
Rui Ueyama 440138c4c8 [COFF] Add /section command line flag.
llvm-svn: 273134
2016-06-20 03:39:39 +00:00
David Majnemer 08af4e56e1 [COFF] Don't call memcpy with a NULL argument
Some declarations of memcpy (like glibc's for example) are attributed
with notnull which makes it UB for NULL to get passed in, even if the
memcpy count is zero.

To account for this, guard the memcpy with an appropriate precondition.

This should fix the last UBSan bug, exposed by the test suite, in the
COFF linker.

llvm-svn: 263919
2016-03-20 23:10:12 +00:00
David Majnemer 22dff0aafc [COFF] Don't hard-code the load configuration size
The load configuration directory is a structure whose size varies as the
OS gains additional functionality.  To account for this, the structure's
layout begins with a size field; this allows loaders to know which
fields are available.

However, LLD hard-coded the sizes (112 bytes for 64-bit and 64 for
32-bit).  This means that we might not inform the loader of all the
pertinent fields or we might claim that there are more fields than are
actually present.

To correctly account for this, the size field must be loaded from the
_load_config_used symbol.

N.B.  The COFF spec is either wrong or out of date, the load
configuration directory is not correctly documented in the
specification: it omits the size field.

llvm-svn: 263543
2016-03-15 09:48:27 +00:00
David Majnemer 22a19a9ace [COFF] Use the correct size of the TLS directory
The TLS directory has a different layout depending on the bitness of the
machine the image will run on.  LLD would always use the 64-bit TLS
directory for the data directory entry instead of an appropriately sized
TLS directory.

llvm-svn: 263539
2016-03-15 06:41:02 +00:00
Rui Ueyama 489a806965 Update for LLVM function name change.
llvm-svn: 257801
2016-01-14 20:53:50 +00:00
Rui Ueyama dba6b576cf COFF: Rename RoundUpToAlignment -> align.
llvm-svn: 257220
2016-01-08 22:24:26 +00:00
Rui Ueyama 9640173e16 COFF: Add /nosymtab command line option.
This is an LLD extension to MSVC link.exe command line. MSVC linker
does not write symbol tables for executables. We do unless no /debug
option is given.

There's a situation that we want to enable debug info but don't want
to emit the symbol table. One example is when we are comparing output
file size. With this patch, you can tell the linker to not create
a symbol table by just specifying /nosymtab.

llvm-svn: 248225
2015-09-21 23:43:31 +00:00
Rui Ueyama a5f0f758d3 COFF: Move markLive() from Writer.cpp to its own file.
Conceptually, garbage collection is not part of Writer,
so move the function out of the file.

llvm-svn: 248099
2015-09-19 21:36:28 +00:00
Rui Ueyama 27e9e6540c Remove unused #includes.
llvm-svn: 248081
2015-09-19 02:28:32 +00:00
Rui Ueyama 8197a4e0bf COFF: Use parallel_sort in Writer::sortExceptionTable().
This patch saves 4 ms out of 5 ms. Very small improvement,
but maybe better than nothing.

llvm-svn: 248063
2015-09-18 23:17:34 +00:00
Rui Ueyama e0e0796d83 COFF: Parallelize Writer::writeSections().
Self-hosting took 801 ms on my machine. Of which this function took
69 ms. Now it takes 37 ms. That is about 4% overall performance
improvement.

llvm-svn: 248052
2015-09-18 22:07:10 +00:00
Rui Ueyama 63dd8766ab COFF: Remove DefinedSymbol::isLive() and markLive(). NFC.
Basically the concept of "liveness" is for sections (or chunks in LLD
terminology) and not for symbols. Symbols are always available or live,
or otherwise it indicates a link failure.

Previously, we had isLive() and markLive() methods for DefinedSymbol.
They are confusing methods. What they actually did is to act as a proxy
to backing section chunks. We can simplify eliminate these methods
and call section chunk's methods directly.

llvm-svn: 247869
2015-09-16 23:55:52 +00:00
Rui Ueyama 4dbff20c91 COFF: Fix bug that not all symbols were written to symtab if /opt:noref.
Only live symbols are written to the symbol table. Because isLive()
returned false if dead-stripping was disabled entirely, only
non-COMDAT sections were written to the symbol table. This patch fixes
the issue.

llvm-svn: 247856
2015-09-16 21:40:47 +00:00
Rui Ueyama c04d5dbf20 COFF: Rename /opt:lldicf -> /opt:icf.
Now that ICF is complete, we can rename this option so that
the driver accepts the MSVC-compatible command line option.

llvm-svn: 247816
2015-09-16 16:33:57 +00:00
Rui Ueyama 6295b27184 COFF: /delayload:<DLLNAME> is case-insensitive.
llvm-svn: 246770
2015-09-03 14:49:47 +00:00
Rui Ueyama bfbd277a1c COFF: Preserve original spelling of DLL file name.
This patch fixes a subtle incompatibility with MSVC linker.
MSVC linker preserves the original spelling of a DLL in the
import descriptor table. LLD previously converted all
characters to lowercase. Usually this difference is benign,
but if a program explicitly checks for DLL file names, the
program could fail.

llvm-svn: 246620
2015-09-02 07:27:31 +00:00
Rui Ueyama d157088adb COFF: Fix the order of the DLL import entry.
There are some DLLs whose initializers depends on other DLLs'
initializers. The initialization order matters for them.

MSVC linker uses the order of the libraries from the command line.
LLD used ASCII-betical order. So they were incompatible.
This patch makes LLD compatible with MSVC.

llvm-svn: 245201
2015-08-17 08:30:31 +00:00
Rui Ueyama e73e418bb4 COFF: Simplify Writer::createImportTables.
A short import library has up to two symbols, so we don't have
to do a for-loop and type dispatch in createImportTables.

llvm-svn: 245200
2015-08-17 07:27:45 +00:00
Rafael Espindola 5c546a1437 COFF: In chunks, store the offset from the start of the output section. NFC.
This is more convenient than the offset from the start of the file as we
don't have to worry about it changing when we move the output section.

This is a port of r245008 from ELF.

llvm-svn: 245018
2015-08-14 03:30:59 +00:00
Rafael Espindola bdc8f2fb83 Update for llvm api change.
llvm-svn: 244849
2015-08-13 00:31:46 +00:00
Rui Ueyama fa071e13aa COFF: Align sections to 512-byte boundaries on disk.
Sections must start at page boundaries in memory, but they
can be aligned to sector boundaries (512-bytes) on disk.
We aligned them to 4096-byte boundaries even on disk, so we
wasted disk space a bit.

llvm-svn: 244691
2015-08-11 23:09:00 +00:00
Rui Ueyama 8ebdc8cedc COFF: Handle _load_config_used in the same way as other special symbols.
Handling the symbol this way is consistent with other symbols, such as
_tls_used. NFC.

llvm-svn: 244367
2015-08-07 22:43:53 +00:00
Rui Ueyama 237c8ff796 Remove unused variable.
llvm-svn: 244365
2015-08-07 22:40:13 +00:00
Rafael Espindola b835ae8e4a Port the error functions from ELF to COFF.
This has a few advantages

* Less C++ code (about 300 lines less).
* Less machine code (about 14 KB of text on a linux x86_64 build).
* It is more debugger friendly. Just set a breakpoint on the exit function and
  you get the complete lld stack trace of when the error was found.
* It is a more robust API. The errors are handled early and we don't get a
  std::error_code hot potato being passed around.
* In most cases the error function in a better position to print diagnostics
  (it has more context).

llvm-svn: 244215
2015-08-06 14:58:50 +00:00
Rui Ueyama 9b000812a4 COFF: ARM: Sort .pdata section correctly.
On ARM, exception handler entries in .pdata section are 8 byte long.

llvm-svn: 244191
2015-08-06 03:45:27 +00:00
Rui Ueyama cb8474edae COFF, ELF2: Pass output file path implicitly using Config global variable.
Various parameters are passed implicitly using Config global variable
already. Output file path is no different from others, so there was no
special reason to handle that differnetly.

This patch changes the signature of writeResult(SymbolTable *, StringRef)
to writeResult(SymbolTable *).

llvm-svn: 244180
2015-08-05 23:51:50 +00:00
Rui Ueyama 685c41cd39 COFF: Simplify Writer interface by hiding Writer class.
llvm-svn: 244175
2015-08-05 23:43:53 +00:00
Rui Ueyama f69ecc1212 COFF: Handle all COMDAT sections as non-GC root.
I don't remember why I thought that only functions are subject
of garbage collection, but the comment here said so, which is
not correct. Moreover, the code just below the comment does not
do what the comment says -- it handles non-COMDAT, non-function
sections as GC root. As a result, it just handles non-COMDAT
sections as GC root.

This patch cleans that up by removing SectionChunk::isRoot and
use isCOMDAT instead.

llvm-svn: 243700
2015-07-30 22:48:45 +00:00
David Majnemer 13ac40ea6e COFF: Sort output sections which start with .debug to the end of the file
We want to convince the NT loader not to map these sections into memory.
A good first step is to move them to the end of the executable.

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

llvm-svn: 243680
2015-07-30 20:26:55 +00:00
Rui Ueyama e0d68e381b Remove unused #includes.
llvm-svn: 243588
2015-07-29 22:53:29 +00:00
Rui Ueyama 8bc43a142b COFF: ARM: Fix relocations to thumb code.
Windows ARM is the thumb ARM environment, and pointers to thumb code
needs to have its LSB set. When we apply relocations, we need to
adjust the LSB if it points to an executable section.

llvm-svn: 243560
2015-07-29 19:25:00 +00:00
Rui Ueyama eb26e1d03c COFF: Fix SECREL and SECTION relocations.
SECREL should sets the 32-bit offset of the target from the beginning
of *target's* output section. Previously, the offset from the beginning
of source's output section was used instead.

SECTION means the target section's index, and not the source section's
index. This patch fixes that issue too.

llvm-svn: 243535
2015-07-29 16:30:45 +00:00
Rui Ueyama 29f74c312a COFF: Set load config table entry on non-x86.
llvm-svn: 243532
2015-07-29 16:30:31 +00:00
Rui Ueyama 506f6d1ae1 COFF: _tls_used is __tls_used on x86.
llvm-svn: 243495
2015-07-28 22:56:02 +00:00
Rui Ueyama cfb874a3d4 COFF: Do not ignore /merge if /debug is specified.
Previously, we ignore /merge option if /debug is specified
because I thought that was MSVC linker did. This was wrong.
/merge shouldn't be ignored even in debug mode.

llvm-svn: 243375
2015-07-28 03:24:23 +00:00
Rui Ueyama 4d54534627 COFF: Add /LargeAddressAware command line option.
llvm-svn: 243373
2015-07-28 03:12:00 +00:00
Rui Ueyama e44524de65 Use SmallDenseMap instead of std::map where we don't care about order of keys.
llvm-svn: 243358
2015-07-28 00:17:25 +00:00
Rui Ueyama a8eed749a2 COFF: Write import library symbols to a symbol table.
Previously no __imp_ symbols nor dllimport thunk functions were
written to a symbol table.

llvm-svn: 243350
2015-07-27 23:40:20 +00:00
Rui Ueyama 5e706b3ee3 COFF: Use short identifiers. NFC.
llvm-svn: 243229
2015-07-25 21:54:50 +00:00
Rui Ueyama a265b01353 COFF: ARM: Set correct entry point address.
llvm-svn: 243199
2015-07-25 02:25:14 +00:00
Rui Ueyama 3afd5bfd7b COFF: Handle base relocation as a tuple of relocation type and RVA. NFC.
On x64 and x86, we use only one base relocation type, so we handled
base relocations just as a list of RVAs. That doesn't work well for
ARM becuase we have to handle two types of base relocations on ARM.
This patch changes the type of base relocation from uint32_t to
{reltype, uint32_t} to make it easy to port this code to ARM.

llvm-svn: 243197
2015-07-25 01:44:32 +00:00
Rui Ueyama cd3f99b6c5 COFF: Implement Safe SEH support for x86.
An object file compatible with Safe SEH contains a .sxdata section.
The section contains a list of symbol table indices, each of which
is an exception handler function. A safe SEH-enabled executable
contains a list of exception handler RVAs. So, what the linker has
to do to support Safe SEH is basically to read the .sxdata section,
interpret the contents as a list of symbol indices, unique-fy and
sort their RVAs, and then emit that list to .rdata. This patch
implements that feature.

llvm-svn: 243182
2015-07-24 23:51:14 +00:00