Commit Graph

293 Commits

Author SHA1 Message Date
Rui Ueyama 56965f4b32 COFF: ARM: Fix DLL import table.
The previous test was testing -flavor link. This patch correctly
tests link2 and fixes a bug that we didn't emit import thunks.

llvm-svn: 243559
2015-07-29 19:24:58 +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 ca577fd536 COFF: Remove unused command line option.
llvm-svn: 243533
2015-07-29 16:30:34 +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 9420dee328 COFF: Fix export symbol names for x86.
I don't fully understand the rationale behind the name mangling
scheme used for the DLL export table and the import library.
Why only leading "_" is dropped for the import library while
both "_" and "@" are dropped from DLL symbol table? But this seems
to be what MSVC linker does.

llvm-svn: 243490
2015-07-28 22:34:24 +00:00
Rui Ueyama 635e85a647 COFF: Update README to mention that it now supports 32-bit x86.
The linker is now able to link not only LLVM/Clang/LLD for x86 but
even larger programs. I confirmed that it successsfully linked Chrome
for x86. Because the browser is a pretty large program, I think I can
say that the linker is now mostly feature complete. (I'm pretty sure
that there are hidden bugs somewhere, but they shouldn't be significant.)

llvm-svn: 243377
2015-07-28 03:40:58 +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 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 7e387a68de COFF: Fix 32-bit delay-import address table.
The address table entry is 32-bit wide on 32-bit and 64-bit on 64-bit.
Previously, it was 64-bit even on 32-bit.

llvm-svn: 243372
2015-07-28 02:54:18 +00:00
Rui Ueyama 2832ce95a5 COFF: Skip non-DWARF debug info sections.
Leaving them in an executable is basically harmless but wastes disk space.
Because no one is using non-DWARF debug info linked by LLD, we can just
remove them.

llvm-svn: 243364
2015-07-28 01:06:58 +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 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 3dd9372d2b COFF: ARM: Support import functions.
llvm-svn: 243205
2015-07-25 03:39:29 +00:00
Rui Ueyama cde7a7907e COFF: ARM: Implement BLX23T relocation.
llvm-svn: 243204
2015-07-25 03:25:28 +00:00
Rui Ueyama 3d9c8639c3 COFF: ARM: Implement BRANCH24T relocation.
llvm-svn: 243202
2015-07-25 03:19:34 +00:00
Rui Ueyama 237fca1451 COFF: ARM: Implement MOV32T relocation.
llvm-svn: 243201
2015-07-25 03:03:46 +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 28df04211c COFF: Split ImportThunkChunk into x86 and x64. NFC.
This change should make it easy to port this code to ARM.

llvm-svn: 243195
2015-07-25 01:16:06 +00:00
Rui Ueyama 1c341a54de COFF: Do not align import thunks on 16-byte boundaries on x86.
Looks like MSVC linker aligns them only on x64.

llvm-svn: 243194
2015-07-25 01:16:04 +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 2296dc137c COFF: Fix base relocation type for x86.
llvm-svn: 243178
2015-07-24 23:24:45 +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 afad42f9ea COFF: Set Load Configuration entry in Data Directory.
Load Configuration field points to a structure containing information
for SEH. That data strucutre is not created by the linker but provided
by an external file. What we have to do is just to set __load_config_used
address to the header.

llvm-svn: 242427
2015-07-16 18:30:35 +00:00
Rui Ueyama 759c8aa9a0 COFF: Fix offset in x86 delay-load thunks.
llvm-svn: 242353
2015-07-15 23:01:36 +00:00
Rui Ueyama ef0e647581 COFF: Implement x86 delay-load thunks.
llvm-svn: 242343
2015-07-15 22:26:57 +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 33fb2cb11b COFF: Fix base relocations for __imp_ symbols on x86.
Because thunks for dllimported symbols contain absolute addresses on x86,
they need to be relocated at load-time. This bug was a cause of crashes
in DLL initialization routines.

llvm-svn: 242259
2015-07-15 00:25:38 +00:00
Rafael Espindola 9f141efc57 Use getChildOffset instead of getBuffer for identifying a member.
I am adding support for thin archives. On those, getting the buffer
involves reading another file.

Since we only need an id in here, use the member offset in the archive.

llvm-svn: 242205
2015-07-14 21:28:07 +00:00
Rui Ueyama 159fb4cd84 Revert "Make COFF linker work when it's built by clang again."
This reverts commit r242006. The original issue in Clang was fixed in r242009,
so we can now safely use std::atomic_flag.

llvm-svn: 242112
2015-07-14 03:09:59 +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 cb71c72ccc COFF: Inline Defined::getRVA because it's very hot.
llvm-svn: 242075
2015-07-13 22:01:27 +00:00
Rui Ueyama e59a530a6c COFF: Split createSymbolAndSymbolTable to small functions. NFC.
llvm-svn: 242066
2015-07-13 20:56:31 +00:00
Nico Weber 9262da26d0 Make COFF linker work when it's built by clang again.
clang-cl doesn't compile std::atomic_flag correctly (PR24101).  Since the COFF
linker doesn't use threads yet, just revert r241420 and r241481 for now to
work around this clang-cl bug.

llvm-svn: 242006
2015-07-13 00:55:26 +00:00
Rui Ueyama c851ccc3bd COFF: Fix locally-imported symbol's base relocations.
Base relocations are RVA and not VA, so we shouldn't add ImageBase.

llvm-svn: 241883
2015-07-10 04:30:54 +00:00
Rui Ueyama 270960f5cd COFF: Find C++ mangled name for symbols starting with underscore.
Symbol foo is mangled as _foo in C and ?foo@@... in C++ on x86.
findMangle has to remove prefix underscore before mangle a given name
as a C++ symbol.

llvm-svn: 241874
2015-07-09 23:03:51 +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 d4b351f0de COFF: Fix locally-imported symbol's size for x86.
llvm-svn: 241860
2015-07-09 21:15:58 +00:00
Rui Ueyama 93b4571187 COFF: Implement base relocations for x86.
With this patch, LLD is now able to self-link an .exe file for x86
that runs correctly, although I don't think some headers (particularly
SEH) are not correct. DLL support is coming soon.

llvm-svn: 241857
2015-07-09 20:36:59 +00:00
Rui Ueyama a841bb0f5d COFF: Fix import symbol name mangling.
For IMPORT_NAME_NOPREFIX symbols, we should remove only one prefix character.

llvm-svn: 241854
2015-07-09 20:22:41 +00:00
Rui Ueyama 39d9efb772 COFF: Fix command line options for external commands.
llvm-svn: 241853
2015-07-09 20:22:39 +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
Rui Ueyama 57aa69ee97 COFF: Make /machine:{i386,amd64} aliases to {x86,x64}.
MSVC linker accepts these aliases.

llvm-svn: 241840
2015-07-09 19:43:49 +00:00
David Majnemer 3a62d3d456 COFF: Fill in the type and storage class in the symbol table
We can use the type and storage class from the symbol's original object
file to fill in the linked executable's symbol table.

llvm-svn: 241828
2015-07-09 17:43:50 +00:00