Commit Graph

142 Commits

Author SHA1 Message Date
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
Nico Weber 5660de7877 lld-link: Fix default output name with /dll flag.
If /dll is passed, the default output filename should be foo.dll,
not foo.exe.
http://reviews.llvm.org/D19321

llvm-svn: 266929
2016-04-20 22:34:15 +00:00
Rui Ueyama afb1901e42 COFF: Support /manifestinput command line option.
Manifest file is a separate or embedded XML file having metadata
of an executable. As it is XML, it can contain various types of
information. Probably the most popular one is to request escalated
priviledges.

Usually the linker creates an XML file and embed that file into
an executable. However, there's a way to supply an XML file from
command line. /manifestniput is it.

Apparently it is over-designed here, but if you supply two or more
manifest files, then the linker needs to merge the files into a
single XML file. A good news is that we don't need to do that ourselves.
MT.exe command can do that, so we call the command from the linker
in this patch.

llvm-svn: 266704
2016-04-19 01:21:58 +00:00
Reid Kleckner 9cd77ce169 [coff] Accept and ignore another link.exe flag for compatibility
This flag disables link.exe's crash handler so that normal windows error
reporting and crash dumping occurs. For now it is reasonable for LLD to
ignore the flag.

Chromium is currently using this flag to collect minidumps of link.exe
crashing, and it breaks the LLD build.

llvm-svn: 264439
2016-03-25 18:09:29 +00:00
Rui Ueyama a453c0a5ad Merge DarwinLdDriver and Driver.
Now that DarwinLdDriver is the only derived class of Driver.
This patch merges them and actually removed the class because
they can now just be non-member functions. This change simplifies
a common header, Driver.h.

http://reviews.llvm.org/D17788

llvm-svn: 262502
2016-03-02 19:08:05 +00:00
Rui Ueyama 417553d393 Make the entry point function calls consistent. NFC.
llvm-svn: 262191
2016-02-28 19:54:51 +00:00
Rui Ueyama 84425d7289 COFF: Implement DLL symbol forwarding.
DLL export tables usually contain dllexport'ed symbol RVAs so that
applications which use the DLLs can find symbols from the DLLs.
However, there's a minor feature to "forward" DLL symbols to other
DLLs.

If you set an RVA to a string whose form is "<dllname>.<symbolname>"
(e.g. "KERNEL32.ExitProcess") instead of symbol RVA to the export
table, the loader interprets that as a forwarder symbol, and resolve
that symbol from the specified DLL.

This patch implements that feature.

llvm-svn: 257243
2016-01-09 01:22:00 +00:00
Rui Ueyama e737824d8a COFF: Create a PDB file with the correct file signature.
Before this patch, we created an empty PDB file if /debug option is
specified. For MSVC linker, such PDB file is completely broken, and
linker exits without doing anything as soon as it finds an empty PDB
file.

A PDB file created in this patch has the correct file signature.
MSVC linker still thinks that the file is broken, but it then removes
and replaces with its output.

This is an initial patch to support PDB in LLD. We aim to support
PDB in order to make it 100% compatible with MSVC linker. PDB support
is the last missing piece.

llvm-svn: 254796
2015-12-04 23:11:05 +00:00
Rui Ueyama 75656eebb8 COFF: /OPT should accept comma-separated multiple arguments.
/OPT:foo,bar is equivalent to /OPT:foo /OPT:bar.

Reported by alexchandel. https://llvm.org/bugs/show_bug.cgi?id=25228

llvm-svn: 250728
2015-10-19 19:40:43 +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 3b153e6541 COFF: Fix bug that /opt:noicf was ignored.
llvm-svn: 247854
2015-09-16 21:30:55 +00:00
Rui Ueyama b02a320f5e COFF: Enable ICF by default.
MSVC linker enables ICF as long as /opt:ref is eanbled, so do we.

llvm-svn: 247817
2015-09-16 16:41:38 +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 13563d8645 Fix style.
llvm-svn: 247644
2015-09-15 00:33:11 +00:00
Rui Ueyama 31e66e32b4 COFF: Ignore /GUARDSYM option.
The option is added in MSVC 2015, and there's no documentation about
what the option is. This patch is to ignore the option for now, so that
at least LLD is usable with MSVC 2015.

llvm-svn: 246780
2015-09-03 16:20:47 +00:00
Rui Ueyama 100ffacf2c COFF: .exe files should be able to export functions.
In r246424, I made a change that disables non-DLL to export
symbols. It turned out that the change was not correct. Both
DLLs and executables are able to export symbols (although the
latter is relatively rare). This change restores the feature.

llvm-svn: 246537
2015-09-01 09:15:58 +00:00
Rui Ueyama f10a32014d COFF: Improve dllexported name mangling compatibility.
The rules for dllexported symbols are overly complicated due to
x86 name decoration, fuzzy symbol resolution, and the fact that
one symbol can be resolved by so many different names. The rules
are probably intended to be "intuitive", so that users don't have
to understand the name mangling schemes, but it seems that it can
lead to unintended symbol exports.

To make it clear what I'm trying to do with this patch, let me
write how the export rules are subtle and complicated.

 - x86 name decoration: If machine type is i386 and export name
   is given by a command line option, like /export:foo, the
   real symbol name the linker has to search for is _foo because
   all symbols are decorated with "_" prefixes. This doesn't happen
   on non-x86 machines. This automatic name decoration happens only
   when the name is not C++ mangled.

   However, the symbol name exported from DLLs are ones without "_"
   on all platforms.

   Moreover, if the option is given via .drectve section, no
   symbol decoration is done (the reason being that the .drectve
   section is created by a compiler and the compiler should always
   know the exact name of the symbol, I guess).

 - Fuzzy symbol resolution: In addition to x86 name decoration,
   the linker has to look for cdecl or C++ mangled symbols
   for a given /export. For example, it searches for not only
   _foo but also _foo@<number> or ??foo@... for /export:foo.

Previous implementation didn't get it right. I'm trying to make
it as compatible with MSVC linker as possible with this patch
however the rules are. The new code looks a bit messy to me, but
I don't think it can be simpler due to the ad-hoc-ness of the rules.

llvm-svn: 246424
2015-08-31 08:43:21 +00:00
Peter Collingbourne df5783b7a5 COFF: Implement parallel LTO code generation.
This is exposed via a new flag /opt:lldltojobs=N, where N is the number of
code generation threads.

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

llvm-svn: 246342
2015-08-28 22:16:09 +00:00
Rui Ueyama 570752c7ac Do not use unique pointers. NFC.
These unique pointers have the exact same lifetime as automatic
variables, so use automatic variables instead.

llvm-svn: 245281
2015-08-18 09:13:25 +00:00
Peter Collingbourne 526ff15546 COFF: Introduce flag /opt:lldlto=N for controlling LTO optimization level.
Differential Revision: http://reviews.llvm.org/D12024

llvm-svn: 245027
2015-08-14 04:47:07 +00:00
Rui Ueyama 3c4737db54 COFF: Ignore /editandcontinue option.
llvm-svn: 244626
2015-08-11 16:46:08 +00:00
Rui Ueyama 107db55ac4 COFF: Define symbols for MSVC 2015 Control Flow Protection.
MSVC 2015's load configuration object (__load_config_used) contains
references to these symbols. I don't fully understand how it works,
but looks like these symbols are linker-defined ones. So I define them
here in the Driver. With this patch, LLD can self-host with MSVC 2015.

This patch is to link MSVC 2015-produced object files. It does not
implement Control Flow Protection. If I understand correctly, the
linker has to create a bitmap of function entry point addresses for
the CFG runtime. We don't do that yet. Produced executables will not
be protected by CFG.

llvm-svn: 244425
2015-08-09 21:01:06 +00:00
Rui Ueyama 27e470abae COFF: Do not fall through if /lib is processed.
llvm-svn: 244424
2015-08-09 20:45:17 +00:00
Rui Ueyama 611add25e3 COFF: Simplify.
SymbolTable::find(mangle(X)) is equivalent to SymbolTable::findUnderscore(X)
except that the latter is slightly efficient as that doesn't allocate a new
string.

llvm-svn: 244377
2015-08-08 00:23:37 +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
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 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
Rafael Espindola 4280a96468 Handle writeImportLibrary failing.
We were printing an error but exiting with 0.

Not sure how to test this. We could add a no-winlib feature,
but that is probably not worth it.

llvm-svn: 244109
2015-08-05 20:03:57 +00:00
Peter Collingbourne e7107ec03b COFF: When resolving _load_config_used, add it as a GC root.
This fixes the cases where the symbol is defined in a comdat or by bitcode.

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

llvm-svn: 243735
2015-07-31 05:33:34 +00:00
Rui Ueyama 4323831732 COFF: Fix command line option spelling.
llvm-svn: 243573
2015-07-29 21:01:15 +00:00
Rui Ueyama 46682630f4 COFF: Ignore /ThrowNew command line option.
This command line option is added since MSVC 2015. Our wild guess is
that the flag is for LTCG and we can safely ignore that.

llvm-svn: 243568
2015-07-29 20:29:15 +00:00
Rui Ueyama ff88d5a26f COFF: Add /safeseh command line option.
If /safeseh is specified, all input files must be compatible with Safe SEH.

llvm-svn: 243565
2015-07-29 20:25:40 +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 d68e211be5 COFF: /HighEntropyVA is on by default only on 64-bit.
llvm-svn: 243374
2015-07-28 03:15:57 +00:00
Rui Ueyama 4d54534627 COFF: Add /LargeAddressAware command line option.
llvm-svn: 243373
2015-07-28 03:12:00 +00:00
Rui Ueyama 5e706b3ee3 COFF: Use short identifiers. NFC.
llvm-svn: 243229
2015-07-25 21:54:50 +00:00
Rui Ueyama 5c437cd1e9 COFF: Fix image base address for 32-bit.
0x140000000 or 0x180000000 are not correct image base addresses
for 32-bit. They are actually much smaller.

llvm-svn: 243228
2015-07-25 21:42:33 +00:00
Rui Ueyama 35ccb0f7d4 COFF: Don't assume !is64() means i386.
In many places we assumed that is64() means AMD64 and i386 otherwise.
This assumption is not sound because Windows also supports ARM.
The linker doesn't support ARM yet, but this is a first step.

llvm-svn: 243188
2015-07-25 00:20:06 +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
Rui Ueyama 3cb895c930 COFF: Fix __ImageBase symbol relocation.
__ImageBase is a special symbol whose value is the image base address.
Previously, we handled __ImageBase symbol as an absolute symbol.

Absolute symbols point to specific locations in memory and the locations
never change even if an image is base-relocated. That means that we
don't have base relocation entries for absolute symbols.

This is not a case for __ImageBase. If an image is base-relocated, its
base address changes, and __ImageBase needs to be shifted as well.
So we have to have base relocations for __ImageBase. That means that
__ImageBase is not really an absolute symbol but a different kind of
symbol.

In this patch, I introduced a new type of symbol -- DefinedRelative.
DefinedRelative is similar to DefinedAbsolute, but it has not a VA but RVA
and is a subject of base relocation. Currently only __ImageBase is of
the new symbol type.

llvm-svn: 243176
2015-07-24 22:58:44 +00:00
Rui Ueyama 8765fbae15 COFF: Fix mangled dllexported names.
If a symbol is exported as /export:foo, and foo is resolved as a
mangled name (_foo@<number> or ?foo@@Y...), that mangled name should
be written to the export table. Previously, we wrote the original
name to the export table.

llvm-svn: 242342
2015-07-15 22:21:08 +00:00
Rui Ueyama a50387f1b3 COFF: Fix entry name inference for x86.
Entry name selection rule is already complicated on x64, but it's more
complicated on x86 because of the underscore name mangling scheme.

If one of _main, _main@<number> (a C function) or ?main@@... (a C++ function)
is defined, entry name is _mainCRTStartup. If _wmain, _wmain@<number or
?wmain@@... is defined, entry name is _wmainCRTStartup. And so on.

llvm-svn: 242110
2015-07-14 02:58:13 +00:00
Rui Ueyama 6d24908fe7 COFF: Fix x86 delay-load helper function name.
If /delayload option is given, we have to resolve __delayLoadHelper2
since the function is the dynamic loader to delay-load DLLs.
The function name is mangled in x86 as ___delayLoadHelper2@8.

llvm-svn: 242078
2015-07-13 22:31:45 +00:00
Rui Ueyama bbdec4fc82 COFF: Fix dllexported symbol names on x86.
Symbol names are usually mangled by appending "_" prefix on x86.
But the mangled name is not used in DLL export table. The export
table contains unmangled names.

llvm-svn: 241872
2015-07-09 22:51:41 +00:00
Rui Ueyama ea533cde30 COFF: Infer machine type earlier than before.
Previously, we infer machine type at the very end of linking after
all symbols are resolved. That's actually too late because machine
type affects how we mangle symbols (whether or not we need to
add "_").

For example, /entry:foo adds "_foo" to the symbol table if x86 but
"foo" if x64.

This patch moves the code to infer machine type, so that machine
type is inferred based on input files given via the command line
(but not based on .directives files).

llvm-svn: 241843
2015-07-09 19:54:13 +00:00