Commit Graph

3811 Commits

Author SHA1 Message Date
Rafael Espindola e3335d8a66 Delete dead code.
llvm-svn: 244040
2015-08-05 13:26:54 +00:00
Rafael Espindola d39c8cd087 Rename asm tests to .s. NFC.
llvm-svn: 244037
2015-08-05 12:14:51 +00:00
Rafael Espindola 3c9cb4b3d3 lld elf2: Diagnose trying to mix incompatible files.
This is also a step in instantiating the writer with the correct template
argument.

llvm-svn: 244035
2015-08-05 12:03:34 +00:00
Rafael Espindola 87ee8dcb64 Use ELFOSABI_NONE. This matches both bfd ld and gold.
llvm-svn: 244033
2015-08-05 11:55:52 +00:00
Rafael Espindola aefd5c1617 Create ObjectFile with the correct endian and word size.
The writer is still hard coded to 64 bits le, but with this we can test for
invalid ELF files.

llvm-svn: 243993
2015-08-04 15:45:54 +00:00
Rafael Espindola e0a5c09a5c Use make_unique. NFC.
llvm-svn: 243988
2015-08-04 15:06:36 +00:00
Rafael Espindola 2ffdd4d0e5 The SymbolTable doesn't need to be a template.
It was already using just code that is common to all object files.

llvm-svn: 243985
2015-08-04 14:29:01 +00:00
Rafael Espindola 602592a048 Remove a silly return.
llvm-svn: 243983
2015-08-04 14:08:11 +00:00
Rafael Espindola 30318518a5 This function never returns null, don't check it.
llvm-svn: 243982
2015-08-04 14:00:56 +00:00
Rafael Espindola b89951457d Remove SymbolTable::getChunks.
When we were using a std::sort over all the chunks we needed to put them in a
single storage.

Now that we just iterate over them and use a map to find the output section,
we can avoid allocating the temporary storage.

llvm-svn: 243980
2015-08-04 13:39:30 +00:00
Simon Atanasyan 7a400d9af4 [Mips] Implement definition of the DT_MIPS_RLD_MAP_REL dynamic tag
llvm-svn: 243835
2015-08-01 14:53:49 +00:00
Rui Ueyama 18edc83b6c COFF: Fix error message. Space was missing.
llvm-svn: 243794
2015-07-31 21:51:25 +00:00
David Majnemer bf1e5c0ee9 COFF: Reenable the lld test disabled in r243758.
llvm-svn: 243762
2015-07-31 17:41:11 +00:00
Reid Kleckner 60dde1fbda Disable objdump disassembly test after r243755
Right now PE image section addresses are RVAs and symbol addresses are
VAs.  We should probably fix this by changing section addresses to match
symbol addresses. Fixing this might take a few hours, so temporarily
disable the objdump part of this test.

llvm-svn: 243758
2015-07-31 16:41:23 +00:00
Reid Kleckner 981576daf7 Add some help strings for /dll and /debug so they show up in /?
llvm-svn: 243757
2015-07-31 16:40:38 +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 7b276e2fb4 COFF: Move code for Identical COMDAT Folding to ICF.cpp.
llvm-svn: 243701
2015-07-30 22:57:21 +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
Rui Ueyama 7ed10b84cc Remove trailing whitespace.
llvm-svn: 243683
2015-07-30 20:47:17 +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
Simon Atanasyan 98a857140b [Mips] Implement definition of DT_MIPS_RLD_MAP dynamic tag, .rld_map section, and __RLD_MAP symbol
llvm-svn: 243626
2015-07-30 07:45:55 +00:00
Rui Ueyama e0d68e381b Remove unused #includes.
llvm-svn: 243588
2015-07-29 22:53:29 +00:00
Rui Ueyama 966acb2e2f COFF: Suppress "Duplicate entry" warning of lib.exe
We create a module-definition file and give that to lib.exe to
create an import library file. A module-definition has to be
syntactically and semantically correct, of course.

There was a case that we created a module-definition file that
lib.exe would complain for duplicate entries. If a user gives
an unmangled and mangled name for the same symbol, we would end
up having two duplicate lines for the mangled name in a module-
definition file.

This patch fixes that issue by uniquefying entries by mangled
symbol name.

llvm-svn: 243587
2015-07-29 22:38:27 +00:00
Michael J. Spencer 44fc1c0a73 [ELF2] Templatize addChunk and handle section flags.
llvm-svn: 243579
2015-07-29 22:14:50 +00:00
Rui Ueyama 4323831732 COFF: Fix command line option spelling.
llvm-svn: 243573
2015-07-29 21:01:15 +00:00
Rui Ueyama 1e4dd36824 Remove astyle-options.
We have our own code formatting tool, clang-format.
I believe no one is using "astyle" to format the code.

llvm-svn: 243570
2015-07-29 20:48:02 +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 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 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 308915ade2 COFF: Fix seh.test.
seh.test was testing -flavor link. This should have been testing
-flavor link2.

llvm-svn: 243557
2015-07-29 19:17:15 +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 b4731a5847 ELF2: Use DenseMap instead of std::map for a symbol table.
We use DenseMap for COFF too.

llvm-svn: 243534
2015-07-29 16:30:40 +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
Michael J. Spencer 5a6f5f178e [ELF2] Emit correct section types.
llvm-svn: 243509
2015-07-29 00:52:17 +00:00
Michael J. Spencer 8039dae26d [ELF2] Emit section table.
llvm-svn: 243506
2015-07-29 00:30:10 +00:00
Michael J. Spencer cdae0a4e2d [ELF2] Devirtualize SymbolBody.
llvm-svn: 243496
2015-07-28 22:58:25 +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 83175276ef COFF: Add REQUIRES arm to tests which use the ARM disassembler.
llvm-svn: 243346
2015-07-27 23:27:03 +00:00
Rui Ueyama fdbbcdad87 ELF2: Inline very small functions.
llvm-svn: 243323
2015-07-27 20:39:04 +00:00
Rui Ueyama a7ccb2926f ELF2: Devirtualize SymbolBody::compare. NFC.
This is to make it consistent with COFF.

llvm-svn: 243321
2015-07-27 20:39:01 +00:00
Rui Ueyama 06d7bd2e42 ELF2: Avoid calling std::sort to make output deterministic.
llvm-svn: 243232
2015-07-26 00:50:15 +00:00
Rui Ueyama 179cd7b82a ELF2: Remove dead code.
llvm-svn: 243231
2015-07-26 00:50:13 +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
Simon Atanasyan 9191a84392 [Mips] Define _DYNAMIC_LINKING symbol if output is a is dynamically linked executable file
llvm-svn: 243211
2015-07-25 10:18:52 +00:00
Simon Atanasyan 8e24577100 [Mips] Remove redundant separate functions to calculate relocations. Do
that inline.

No functional changes.

llvm-svn: 243210
2015-07-25 10:18:44 +00:00
Simon Atanasyan 4504791890 [Mips] Remove duplicated code by joining the same functions
llvm-svn: 243209
2015-07-25 10:18:39 +00:00
Simon Atanasyan 3b6ea7ab4a [Mips] Do not sign-extend addends read from RELA relocation records
llvm-svn: 243208
2015-07-25 10:18: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
Nick Kledzik a79d7bb344 Add initial CODE_OWNERS.TXT file
llvm-svn: 243190
2015-07-25 00:44:37 +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
Michael J. Spencer c34eed3045 [ELF2] Remove broken test.
The exact error the system returns for this case is varies between OS's and file systems.

llvm-svn: 243184
2015-07-25 00:09:22 +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
Michael J. Spencer 84487f1174 [ELF2] Add a new ELF linker based on the new PE/COFF linker.
Differential Revision: http://reviews.llvm.org/D11188

llvm-svn: 243161
2015-07-24 21:03:07 +00:00
Rui Ueyama 8abca7e729 Fix -Wextra-semi.
Patch from Eugene.Zelenko!

llvm-svn: 243060
2015-07-23 23:03:55 +00:00
Rafael Espindola 5871bf30ac Use the getSymbol with an explicit symbol table. NFC.
llvm-svn: 243014
2015-07-23 13:41:25 +00:00
Denis Protivensky f84acb959b [LinkerScript] Fix case when setting custom NONE segment
llvm-svn: 243006
2015-07-23 11:46:59 +00:00
Denis Protivensky cdc1246750 [ELF] Apply segments from linker scripts
Put sections to segments according to linker scripts if available.
Rework the code of TargetLayout::assignSectionsToSegments so it operates
on the given list of segments, which can be either read from linker scripts
or constructed as before.
Handle NONE segments defined in linker scripts by putting corresponding sections
to PT_NULL segment.
Consider flags set for segments through linker scripts.

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

llvm-svn: 243002
2015-07-23 10:34:30 +00:00
Simon Atanasyan b6d60a69c0 [ELF] Protect write access to the ELFLinkingContext::_cidentSections by a mutex
The _cidentSections container is filled during files parsing so we need to
serialize a concurrent access to it.

llvm-svn: 242885
2015-07-22 10:32:19 +00:00
Rafael Espindola 1cb8a19bcb Don't assume ELFFile iterates over the program headers.
It will stop doing so shortly.

llvm-svn: 242832
2015-07-21 20:46:21 +00:00
Rafael Espindola 5f95fa31b3 Drop last use of getDynamicSymbolName.
llvm-svn: 242820
2015-07-21 18:15:32 +00:00
Rafael Espindola 16154afb06 Keep track of which string table is associated with a symbol table.
This removes the last uses of getStaticSymbolName in lld.

llvm-svn: 242816
2015-07-21 18:00:39 +00:00
Simon Atanasyan b1600f2eee [Mips] Fix addend writing for R_MIPS_REL32 relocation
llvm-svn: 242760
2015-07-21 05:54:30 +00:00
Simon Atanasyan f9db71eaec [Mips] Handle R_MIPS_JALR relocation to omptimize jalr/jr instructions
llvm-svn: 242759
2015-07-21 05:54:22 +00:00
Rafael Espindola d285d3fbb7 Update for llvm api change.
llvm-svn: 242701
2015-07-20 20:08:04 +00:00
Sean Silva 1bc87776ec Add missing REQUIRES to this test.
An internal builder was failing.

llvm-svn: 242452
2015-07-16 22:12: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
Nico Weber f26ff282df Fix test/COFF/dll.test after r242342.
r242342 removed the RUN line that wrote t2.obj, but another RUN line further
down still reads t2.obj, so add it back.

llvm-svn: 242415
2015-07-16 16:45:01 +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 9170a17377 Update for llvm api change.
llvm-svn: 242216
2015-07-14 22:19:09 +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
Adhemerval Zanella 4d0d1babfc [ELF/AArch64] Fix export TLS dynamic symbol
This patch fixes the TLS dynamic variable exportation from .got.plt segments,
created by General-dynamic relocations (TLSDESC).  Current code only export
symbols in dynamic table from .got sections.

llvm-svn: 242142
2015-07-14 13:25:32 +00:00
Michael J. Spencer bae540e945 Revert ELF port. Posting to mailing list.
llvm-svn: 242118
2015-07-14 04:49:48 +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
Michael J. Spencer 50ed5308d1 Fix warnings.
llvm-svn: 242098
2015-07-14 01:02:37 +00:00
Rafael Auler 2c4af3ead7 [LinkerScript] Don't create unnecessarily large segments
When using a linker script expression to change the address of a section, even
if the new address is more than a page of distance from the old address, lld
may put everything in the same segment, forcing it to be unnecessarily large.
This patch changes the logic in Segment::assignVirtualAddress() and
Segment::assignFileOffsets() to allow the segment to be sliced into two or more
if it detects a linker script expression that changes a section address.

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

llvm-svn: 242096
2015-07-14 00:34:43 +00:00
Michael J. Spencer f754b1fe12 Put explicit template instantiations in the correct namespace.
llvm-svn: 242093
2015-07-14 00:22:00 +00:00
Michael J. Spencer 64661cdb4c Remove commented out code.
llvm-svn: 242092
2015-07-14 00:11:38 +00:00
Rafael Auler a4917f74eb [LinkerScript] Fix bug in Segment::assignVirtualAddress()
When calculating the start address and size of a segment, lld mistakenly
attributed the start address of the last segment slice to the whole segment
when it should consider the start address of the first slice. In this case, in a
multi-slice segment, Segment::assignVirtualAddress() will return a wrong
segment start address to TargetLayout::assignVirtualAddress(). The effect of
this miscalculation is to allocate some program headers in unnecessarily far
away addresses. This commit fixes this.

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

llvm-svn: 242089
2015-07-13 23:52:58 +00:00
Michael J. Spencer 8a4145411f Initial ELF port.
This is a direct port of the new PE/COFF linker to ELF.

It can take a single object file and generate a valid executable that executes at the first byte in the text section.

llvm-svn: 242088
2015-07-13 23:48:06 +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
Simon Atanasyan b34a080701 [Mips] Support MIPS big-endian 32/64-bits targets
llvm-svn: 242014
2015-07-13 09:11:35 +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
Nico Weber 0e80816fe8 Fix lld tests with LLVM_ENABLE_THREADS disabled.
With LLVM_ENABLE_THREADS disabled, all the llvm code assumes that it runs on
a single thread and doesn't use any mutexes.  lld still spawned lots of threads
in that case and called into llvm, assuming that llvm is thread-safe.

As fix, let lld use only a single thread if LLVM_ENABLE_THREADS is disabled.
I left in all the mutexes in lld. That means lld is a bit slower than
necessary in single-thread mode, but that's probably worth the simpler code.

llvm-svn: 242004
2015-07-13 00:51:43 +00:00
Nico Weber d08eca0181 elf: Make error output from AtomSection<ELFT>::write() deterministic.
The function uses parallel_for() and then writes error messages from the
parallel loop's body.  This produces nondetermistic error messages.  Instead,
copy error messages to a vector and sort it by the atom's file offsets before
printing all error messages after the parallel_for().  This results in a few
string copies, but only in the error case.  (And passing tests seem more
important than performance.)

This makes tests elf/AArch64/rel-prel16-overflow.test and
elf/AArch64/rel-prel32-overflow.test pass on Windows: Both tests check that
atom error messages are emitted in a certain order, and on Windows they
happened to be emitted in a different order before this patch.

llvm-svn: 241988
2015-07-12 04:45:35 +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
Rui Ueyama 1b53ec796a COFF: Remove Writer::Is64 and use Config::is64 instead. NFC.
llvm-svn: 241819
2015-07-09 16:40:39 +00:00
Rui Ueyama 7c3e23fffd COFF: Fix import thunks and name mangling for x86.
With this patch, LLD is now able to correctly link a "hello world"
program written in assembly for 32-bit x86.

llvm-svn: 241771
2015-07-09 01:25:49 +00:00
Rui Ueyama 25522f5d4a COFF: Support 32-bit x86 DLL import table.
llvm-svn: 241767
2015-07-09 00:45:50 +00:00
Rafael Espindola dbb40ab8b7 Update for upcoming llvm change.
llvm-svn: 241746
2015-07-08 22:14:36 +00:00
Rui Ueyama dcb46d6a74 COFF: Remove dead code.
r241647 made Driver to infer machine type, so this code is not actually in use.

llvm-svn: 241720
2015-07-08 20:35:29 +00:00
Rui Ueyama 1c79ce9a4c COFF: Implement dllimported symbol name mangling.
Symbols exported by DLLs are listed in import library files.
Exported names may be mangled by "Import Name Type" field as
described in PE/COFF spec 7.3. This patch implements that
mangling scheme.

llvm-svn: 241719
2015-07-08 20:22:50 +00:00
Peter Collingbourne 04a4711565 COFF: Set parent name for bitcode files.
Differential Revision: http://reviews.llvm.org/D10983

llvm-svn: 241713
2015-07-08 19:14:33 +00:00
Rui Ueyama e16a75d5a1 COFF: Handle /machine option in a similar manner for other options. NFC.
llvm-svn: 241701
2015-07-08 18:14:51 +00:00
Peter Collingbourne e008391ea2 Create lld-link2 symlink/copy for new COFF linker.
This situation will only be temporary; I imagine we will eventually want to
have the new linker take the lld-link alias after we remove the old one.
However, I would like to port the CFI test suite in the compiler-rt
repository to Windows using the new COFF linker's LTO support; these tests
currently use gcc-style command lines and invoke the linker via the
compiler driver.

We can select the linker using the -fuse-ld flag, which Clang supports on
Windows, but it takes an executable name, rather than a list of arguments
(so "-fuse-ld=lld -flavor link2" wouldn't work), and it doesn't seem worth
the effort to extend the Clang driver to support arguments.

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

llvm-svn: 241696
2015-07-08 18:05:22 +00:00
David Majnemer 2c345a337c COFF: Emit a symbol table if /debug is specified
Providing a symbol table in the executable is quite useful when
debugging a fully-linked executable without having to reconstruct one
from DWARF.

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

llvm-svn: 241689
2015-07-08 16:37:50 +00:00
Simon Atanasyan fb1662f563 The LLD tests require 'llvm-lib', ensure that this is built.
llvm-svn: 241676
2015-07-08 11:40:43 +00:00
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
Davide Italiano 5a2acd1e1e Fix undefined behaviour exposed by the new -Wshift-negative-value warning.
llvm-svn: 241530
2015-07-07 00:02:59 +00:00
Rafael Espindola 9c7a623015 Update for llvm change.
llvm-svn: 241489
2015-07-06 19:21:08 +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
Simon Atanasyan 1e7b08b68d [ELF] Fix ELF test cases. Do not provide content for bss sections.
llvm-svn: 241378
2015-07-03 23:06:24 +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
Adhemerval Zanella c0ad64543b [ELF/AArch64] Set correct loader name in linking context
This patch reimplements ELFLinkingContext::getDefaultInterpreter for aarch64
with correct loader name. It is required to exclude the loader from DT_NEEDED
in shared library creation.

llvm-svn: 241371
2015-07-03 21:24:36 +00:00
Adhemerval Zanella fdb4d6346a [ELF/AArch64] Set correct loader name in linking context
This patch reimplements ELFLinkingContext::getDefaultInterpreter for aarch64
with correct loader name.  It is required to exclude the loader from DT_NEEDED
in shared library creation.

llvm-svn: 241370
2015-07-03 21:21:39 +00:00
Rafael Espindola ec57b8b9f8 Use getDynamicSymbolName/getStaticSymbolName instead of a constant argument.
llvm-svn: 241346
2015-07-03 12:50:54 +00:00
Simon Atanasyan 47a2eafd5c [Mips] Factor out symbol type checking (PIC/non-PIC) into the separate function
No functional changes.

llvm-svn: 241342
2015-07-03 11:36:55 +00:00
Simon Atanasyan a87c17a201 [ELF] Define __start_XXX/__stop_XXX symbols where XXX is a section name
This is GNU ELF linker extension used particularly by LibC code.
If input object files contain section named XXX, and the XXX is a valid C
identifier, and there are undefined or weak symbols __start_XXX/__stop_XXX,
linker should define __start_XXX/__stop_XXX symbols point to the begin/end
of the XXX section correspondingly.

For example, without support of this extension statically linked executables
for X86_64 and Mips (maybe other) targets do not flush IO buffers at the end
of executing.

llvm-svn: 241341
2015-07-03 11:25:37 +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
Rafael Espindola cd55286893 Update for llvm changes.
llvm-svn: 241298
2015-07-02 20:55:28 +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
Simon Atanasyan b28f504226 [ELF] Remove dead code. NFC
llvm-svn: 241274
2015-07-02 15:04:08 +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
Simon Atanasyan 0d38933235 [ELF] Make OutputSection::memSize `const` member function. NFC
llvm-svn: 241194
2015-07-01 21:44:55 +00:00
Adhemerval Zanella dbd75b7fa0 [ELF/AArch64] Initial General-dynamic TLS support
This patch adds initial general-dynamic TLS support for AArch64.  Currently
no optimization is done to realx for more performance-wise models (initial-exec
or local-exec).  This patch also only currently handles correctly executable
generation, although priliminary DSO support through PLT specific creation
is also added.

With this change clang/llvm bootstrap with lld is possible in static configuration
(some DSO creation fails due missing Linker script support, not AArch64 specific),
although make check also shows some issues.

llvm-svn: 241192
2015-07-01 21:35:39 +00:00
Rafael Espindola a5ee9fdea2 Update for llvm api change.
llvm-svn: 241157
2015-07-01 12:56:33 +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
Rafael Espindola 1acc30e970 Use copy init instead of direct init.
llvm-svn: 241089
2015-06-30 18:04:46 +00:00
Rafael Espindola 29efdd4a21 Update for llvm change.
llvm-svn: 241075
2015-06-30 15:34:00 +00:00
Peter Collingbourne a5cb389337 Add layout/triple to fix test on platforms where names are mangled.
llvm-svn: 241031
2015-06-30 01:15:54 +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
Rafael Espindola bff864a596 Update for llvm api change.
llvm-svn: 240997
2015-06-29 21:26:07 +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
Rafael Espindola fdeb19ff33 Update for llvm api change.
llvm-svn: 240951
2015-06-29 14:39:30 +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