Commit Graph

1432 Commits

Author SHA1 Message Date
Rafael Espindola 832b93f219 Start recording the section of symbols in the symbol table.
Support for more than 64 K sections to follow shortly.

llvm-svn: 245868
2015-08-24 20:06:32 +00:00
Rui Ueyama 473aa4088d COFF: Add a test for ICF and circular references.
ICF is a feature to merge sections not by name (which is the regular
COMDAT merging) but by contents. If two or more sections have the
identical contents and relocations, ICF merges them to save space.
Accessors or templated functions tend to have the same contents, and
ICF can hold them.

If we consider sections as vertices and relocations as edges, the
problem is to find as many isomorphic graphs as possile from input
graphs. MSVC linker is smart enough to identify isomorphic graphs
even if they contain circles (GNU gold cannot handle circles
according to http://research.google.com/pubs/pub36912.html, so this
is impressive).

Circular references are not uncommon in COFF object files.
One example is .pdata. .pdata sections contain exception handler info
for functions, so they naturally have relocations for the functions.
The functions in turn have references to the .pdata sections so that
the functions and their .pdata are linked together. As a result, they
form circles.

This is a test case for circular graphs. LLD is not able to handle
this test case yet. I'll add code soon.

llvm-svn: 245827
2015-08-24 08:11:53 +00:00
Rui Ueyama 28103266c6 COFF: Update a test for ICF.
The old test files were just compiler outputs, so it was hard to
debug if something goes wrong. The new test file is carefully
hand-crafted to trigger ICF to avoid that.

llvm-svn: 245826
2015-08-24 07:52:08 +00:00
Davide Italiano b3c7fb7f24 [LinkerScript] Fix a crash when matching wildcards.
Submitted by:	  zan jyu via llvm-dev

llvm-svn: 245792
2015-08-22 20:36:19 +00:00
Simon Atanasyan 814b705984 [Mips] Use 'or' for move instead of [d]addu in PLT entries
Patch by Simon Dardis.

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

llvm-svn: 245491
2015-08-19 20:20:10 +00:00
Rui Ueyama 7171c82194 COFF: Allow forward reference for weak externals
Previously, weak external symbols could reference only symbols that
appeared before them. Although that covers almost all use cases
of weak externals, there are object files out there which contains
weak externals that have forward references.

This patch supports such weak externals.

llvm-svn: 245258
2015-08-17 23:35:43 +00:00
Rui Ueyama 27716bb9d9 COFF: Fix a broken test.
This test couldn't be run more than once because lib.exe does not work
if the files already exist.

llvm-svn: 245214
2015-08-17 11:06:21 +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
Rafael Espindola 1bd885aba4 ELF: Also record the type of undefined symbols.
Tested with a weak undefined. Testing with a plain undefined will have to wait
for support for -shared.

llvm-svn: 245069
2015-08-14 16:46:28 +00:00
Rafael Espindola c44d17ad45 Add the type of the symbols to the symbol table.
For now only defined symbols are covered. I will add undefined ones in the
next patch.

llvm-svn: 245057
2015-08-14 15:10:49 +00:00
Rafael Espindola 383c323d4d ELF: Set the correct symbol binding.
llvm-svn: 245049
2015-08-14 13:52:36 +00:00
Rafael Espindola 62b81b875a Start populating the symbol table.
With this patch only the name is set. I will set the other fields shortly.

For now the table doesn't include local symbols. This is equivalent to using
--discard-all with gnu ld. This is OK for now since the symbols are not
needed for execution and for testing symbol resolution we only need the
global symbols.

llvm-svn: 245044
2015-08-14 13:07:05 +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
Rafael Espindola f763ca3c9c ELF: Create a symbol table.
For now it is empty. I will add the symbols in a followup patch.

llvm-svn: 245008
2015-08-14 02:42:20 +00:00
Rafael Espindola 2db634d8f1 Correctly align output sections.
They don't need to be aligned to page boundaries. Only segments need that.

llvm-svn: 244942
2015-08-13 20:24:18 +00:00
Rafael Espindola 0160a281c2 Record the alignment of the output sections.
llvm-svn: 244941
2015-08-13 20:13:39 +00:00
Rafael Espindola 64e888ba5f Set the file offset of zero sized sections.
There is no reason not to do it and this matches what gold and bfd ld do.

llvm-svn: 244929
2015-08-13 18:23:43 +00:00
Rafael Espindola a7471795ae Take name, type and flags in consideration when concatenating sections.
This is mandated by the ELF spec.

llvm-svn: 244911
2015-08-13 17:04:50 +00:00
Rafael Espindola ef1ac01c2e Don't give an address to sections that are not allocated.
llvm-svn: 244900
2015-08-13 15:31:17 +00:00
Rafael Espindola abad6186c3 Place SHF_ALLOC sections first in the output.
Having them in the middle of the file complicates the creation of segments.

llvm-svn: 244898
2015-08-13 15:23:46 +00:00
Rafael Espindola cde251370a Include non-alloca sections in the link.
llvm-svn: 244895
2015-08-13 14:45:44 +00:00
Rafael Espindola 40dfae36fc This test needs lib.exe.
llvm-svn: 244844
2015-08-13 00:03:47 +00:00
Denis Protivensky 0c8beb1c95 [LinkerScript] Process program header in PHDRS command
Add PT_PHDR segment depending on its availability in linker script's
PHDRS command, fallback if no linker script is given.
Handle FILEHDR, PHDRS and FLAGS attributes of program header.

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

llvm-svn: 244743
2015-08-12 12:31:35 +00:00
Rafael Espindola 91009b3856 Fix the alignment of the section headers.
Thanks a lot to Rui for noticing it.

llvm-svn: 244711
2015-08-12 01:45:28 +00:00
Rafael Espindola 6b83b90b6a ELF: Create a string table.
For now only the sections are in it, but it already makes the output easier
to read and test.

llvm-svn: 244702
2015-08-12 00:00:24 +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
Rafael Espindola 76e24ea955 Add support for weak undefined symbols.
llvm-svn: 244640
2015-08-11 17:57:05 +00:00
Rafael Espindola b13df6582a Add support for weak symbols.
llvm-svn: 244636
2015-08-11 17:33:02 +00:00
Rafael Espindola 7c388187c9 Don't report a conflict between two local symbols.
llvm-svn: 244624
2015-08-11 16:30:34 +00:00
Rafael Espindola 904c81dc9e Add a test for our handling of shndx.
It was already working, but missing a test.

llvm-svn: 244477
2015-08-10 18:28:24 +00:00
Tom Stellard 01da42d8db ELF: Correctly identify SHT_NOTE sections with !SHF_ALLOC as type*Note
Subscribers: llvm-commits

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

llvm-svn: 244317
2015-08-07 14:36:04 +00:00
Rafael Espindola 525d0bf0b9 On freebsd it is possible to open a directory. Test with another error.
llvm-svn: 244249
2015-08-06 19:12:03 +00:00
Rui Ueyama 6872455c6d COFF: Make test commands shorter. NFC.
llvm-svn: 244227
2015-08-06 16:47:46 +00:00
Rui Ueyama 251b0e268b COFF: Remove the old COFF linker and make link an alias to link2.
It's time to remove old COFF linker because the new one is now complete.

llvm-svn: 244226
2015-08-06 16:19:35 +00:00
Rafael Espindola 08c1df6a45 Bring test back but don't check the filesytem dependent error.
llvm-svn: 244218
2015-08-06 15:18:59 +00:00
Rafael Espindola 3b8018069e For completeness, add a 64 bit big endian test.
llvm-svn: 244183
2015-08-06 00:00:50 +00:00
Rafael Espindola 1c2f64d1a6 Add a big endian testcase.
llvm-svn: 244173
2015-08-05 23:40:20 +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
Rafael Espindola ccd3adc60b lit fixes for lld.
It was not using LLVM_LIT_TOOLS_DIR and at least on my VM never finding lib.exe.

With this all the COFF tests show up as supported and pass.

llvm-svn: 244107
2015-08-05 19:55:17 +00:00
Rui Ueyama ba7c041f21 COFF: ARM: Implepment BLX23T relocation and fix Branch20T.
I fed the same test to MSVC linker and got the same output,
so I believe this implementation is correct.

llvm-svn: 244102
2015-08-05 19:40:07 +00:00
Rafael Espindola 4b7c2fc64e Add bare minimum to get a working i386 linux program.
llvm-svn: 244050
2015-08-05 15:08:40 +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
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
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
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 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
Michael J. Spencer 44fc1c0a73 [ELF2] Templatize addChunk and handle section flags.
llvm-svn: 243579
2015-07-29 22:14:50 +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 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
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 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 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 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 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 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
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
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 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
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
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 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
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 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
Simon Atanasyan b34a080701 [Mips] Support MIPS big-endian 32/64-bits targets
llvm-svn: 242014
2015-07-13 09:11: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 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
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 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
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
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
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
Rafael Espindola 9c7a623015 Update for llvm change.
llvm-svn: 241489
2015-07-06 19:21:08 +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 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 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
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 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 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 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
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
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
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 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
Rui Ueyama 6b79ed128a COFF: Fix /export.
Mangled dllexported symbols may be defined in a library.
If that's the case, we have to read a member file from the library.

llvm-svn: 240947
2015-06-29 14:27:10 +00:00
Rui Ueyama 45044f47d3 COFF: Fix logic to find default entry name or subsystem.
The previous logic to find default entry name or subsystem does not
seem correct (i.e. was not compatible with MSVC linker). Previously,
default entry name was inferred from CRT functions and user-defined
entry functions. Subsystem was inferred from CRT functions.

Default entry name and subsystem are now inferred based on the
following table. Note that we no longer use CRT functions to infer
them.

               Entry name           Subsystem
  main         mainCRTStartup       console
  wmain        wmainCRTStartup      console
  WinMain      WinMainCRTStartup    windows
  wWinMain     wWinMainCRTStartup   windows

llvm-svn: 240922
2015-06-29 01:03:53 +00:00
Rui Ueyama f5313b3498 COFF: Allow mangled symbols as arguments for /export.
Usually dllexported symbols are defined with 'extern "C"',
so identifying them is easy. We can just do hash table lookup
to look up exported symbols.

However, C++ non-member functions are also allowed to be exported,
and they can be specified with unmangled name. So, if /export:foo
is given, we need to look up not only "foo" but also its all
mangled names. In MSVC mangling scheme, that means that we need to
look up any symbol which starts with "?foo@@Y".

In this patch, we scan the entire symbol table to search for
a mangled symbol. The symbol table is a DenseMap, and that doesn't
support table lookup by string prefix. This is of course very
inefficient. But that should be probably OK because the user
should always add 'extern "C"' to dllexported symbols.

llvm-svn: 240919
2015-06-28 22:16:41 +00:00
Rui Ueyama 091b1a6585 COFF: Fix flaky test.
This test was flaky because stdout and stderr can be mixed.

llvm-svn: 240918
2015-06-28 22:06:53 +00:00
Rui Ueyama a8b60458ea COFF: Add /noentry flag.
This option is sometimes used to create a resource-only DLL that
doesn't need any initialization.

llvm-svn: 240915
2015-06-28 19:56:30 +00:00
Rui Ueyama fe6d11c0db Fix broken test.
llvm-svn: 240914
2015-06-28 19:38:10 +00:00
Rui Ueyama 95925fd1ab COFF: Support /force flag.
This option is to ignore remaining undefined symbols and force
the linker to create an output file anyways.

The existing code assumes that there's no undefined symbol after
reportRemainingUndefines(). That assumption is legitimate.
I also don't want to mess up the existing code for this minor feature.
In order to keep it as is, remaining undefined symbols are replaced
with dummy defined symbols.

llvm-svn: 240913
2015-06-28 19:35:15 +00:00
Rui Ueyama 06cf3df2d5 COFF: Handle LINK environment variable.
If LINK is defined and not empty, it's supposed to contain
command line options.

llvm-svn: 240900
2015-06-28 02:35:31 +00:00
Rui Ueyama 871847e32d COFF: Fix ICF correctness bug.
When comparing two COMDAT sections, we need to take section values
and associative sections into account. This patch fixes that bug.
It fixes a crash bug of llvm-tblgen when linked with /opt:lldicf.

One thing I don't understand yet is that this logic seems to be
too strict. MSVC linker is able to create more compact executables
(which of course work correctly). With this ICF algorithm, LLD is
able to make executable smaller, but the outputs are larger than
MSVC's. There must be something I'm missing here.

llvm-svn: 240897
2015-06-28 01:30:54 +00:00
Rui Ueyama 810551a694 COFF: Add base relocation for delay-import table.
Because the address table of the delay-import table contains
absolute address, it needs to be added to the base relocation
table.

llvm-svn: 240844
2015-06-26 22:05:32 +00:00
Rui Ueyama 382dc96e29 COFF: Fix delay-import tables.
There were a few issues with the previous delay-import tables.

 - "Attribute" field should have been 1 instead of 0.
   (I don't know the meaning of this field, though.)
 - LEA and CALL operands had wrong addresses.
 - Address tables are in .didat (which is read-only).
   They should have been in .data.

llvm-svn: 240837
2015-06-26 21:40:15 +00:00
Peter Collingbourne be54955bba COFF: Implement /lldmap flag.
This flag can be used to produce a map file, which is essentially a list
of objects linked into the final output file together with the RVAs of
their symbols. Because our format differs from MSVC's we expose it as a
separate flag.

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

llvm-svn: 240812
2015-06-26 18:58:24 +00:00
Rui Ueyama 7383562bc9 COFF: Align DLL import thunks on 16-byte boundaries.
llvm-svn: 240806
2015-06-26 18:28:56 +00:00
Simon Atanasyan 253125af03 [Mips] Reject R_MIPS_CALL16 against local symbols
llvm-svn: 240765
2015-06-26 07:25:20 +00:00
Simon Atanasyan b7bcff8796 [Mips] Create LA25 stubs for all branch relocations
llvm-svn: 240763
2015-06-26 07:25:06 +00:00
Rui Ueyama 923cb64801 COFF: Add a test for r240719.
llvm-svn: 240758
2015-06-26 03:50:27 +00:00
Rui Ueyama 32f8e1cb4e COFF: Change symbol resolution order for entry and /include.
We were resolving entry symbols and /include'd symbols after all other
symbols are resolved. But looks like it's too late. I found that it
causes some program to fail to link.

Let's say we have an object file A which defines symbols X and Y in an
archive. We also have another file B after A which defines X, Y and
_DLLMainCRTStartup in another archive. They conflict each other, so
either A or B can be linked.

If we have _DLLMainCRTStartup as an undefined symbol, file B is always
chosen. If not, there's a chance that A is chosen. If the linker
find it needs _DllMainCRTStartup after that, it's too late.

This patch adds undefined symbols to the symbol table as soon as
possible to fix the issue.

llvm-svn: 240757
2015-06-26 03:44:00 +00:00
Rui Ueyama ccde19d77e COFF: Fix local absolute symbols.
Absolute symbols were always handled as external symbols, so if two
or more object files define the same absolute symbol, they would
conflict even if the symbol is private to each file.
This patch fixes that bug.

llvm-svn: 240756
2015-06-26 03:09:23 +00:00
Rui Ueyama a29948873f COFF: Don't read non-x64 object files.
Currently the new LLD supports only x86-64.

llvm-svn: 240749
2015-06-26 00:42:21 +00:00
Rui Ueyama f799edef28 COFF: Rename /opt:icf -> /opt:lldicf.
ICF implemented in LLD is so experimental that we don't want to
enable that even if /opt:icf option is passed. I'll rename it back
once the feature is complete.

llvm-svn: 240721
2015-06-25 23:26:58 +00:00
Rui Ueyama 5817ebb0c8 COFF: Fix lexer for the module-definition file.
Previously it would hang if there's a stray punctuation (e.g. ?).

llvm-svn: 240697
2015-06-25 21:06:00 +00:00
Rafael Espindola 69e942aefe Update for llvm change.
llvm-svn: 240658
2015-06-25 17:04:12 +00:00
Rui Ueyama 88e0f9206b COFF: Fix a bug of __imp_ symbol.
The change I made in r240620 was not correct. If a symbol foo is
defined, and if you use __imp_foo, __imp_foo symbol is automatically
defined as a pointer (not just an alias) to foo.

Now that we need to create a chunk for automatically-created symbols.
I defined LocalImportChunk class for them.

llvm-svn: 240622
2015-06-25 03:31:47 +00:00
Rui Ueyama d766653534 COFF: Handle undefined symbols starting with __imp_ in a special way.
MSVC linker is able to link an object file created from the following code.
Note that __imp_hello is not defined anywhere.

  void hello() { printf("Hello\n"); }
  extern void (*__imp_hello)();
  int main() { __imp_hello(); }

Function symbols exported from DLLs are automatically mangled by appending
__imp_ prefix, so they have two names (original one and with the prefix).
This "feature" seems to simulate that behavior even for non-DLL symbols.

This is in my opnion very odd feature. Even MSVC linker warns if you use this.
I'm adding that anyway for the sake of compatibiltiy.

llvm-svn: 240620
2015-06-25 02:21:44 +00:00
Adhemerval Zanella 3ebea27d66 [ELF] Fix .init_array initialization
Some compilers may not add the section symbol in '.symtab' for the
.init_array and 'ldd' just ignore it.  It results in global constructor
not being called in final executable.

This patch add both '.init_array' and '.fini_array' to be added in
Atom graph generation even when the section contains no symbol.  An
already existing testcase is modified to check for such scenario.

The issue fixes the llvm test-suite regressions for both Single
and MultiSource files.

llvm-svn: 240570
2015-06-24 19:26:00 +00:00
Rui Ueyama ddf71fc370 COFF: Initial implementation of Identical COMDAT Folding.
Identical COMDAT Folding (ICF) is an optimization to reduce binary
size by merging COMDAT sections that contain the same metadata,
actual data and relocations. MSVC link.exe and many other linkers
have this feature. LLD achieves on per with MSVC in terms produced
binary size with this patch.

This technique is pretty effective. For example, LLD's size is
reduced from 64MB to 54MB by enaling this optimization.

The algorithm implemented in this patch is extremely inefficient.
It puts all COMDAT sections into a set to identify duplicates.
Time to self-link with/without ICF are 3.3 and 320 seconds,
respectively. So this option roughly makes LLD 100x slower.
But it's okay as I wanted to achieve correctness first.
LLD is still able to link itself with this optimization.
I'm going to make it more efficient in followup patches.

Note that this optimization is *not* entirely safe. C/C++ require
different functions have different addresses. If your program
relies on that property, your program wouldn't work with ICF.
However, it's not going to be an issue on Windows because MSVC
link.exe turns ICF on by default. As long as your program works
with default settings (or not passing /opt:noicf), your program
would work with LLD too.

llvm-svn: 240519
2015-06-24 04:36:52 +00:00
Peter Collingbourne c7b685d997 COFF: Ignore debug symbols.
Differential Revision: http://reviews.llvm.org/D10675

llvm-svn: 240487
2015-06-24 00:05:50 +00:00
Rui Ueyama 0d2e999050 COFF: Make link order compatible with MSVC link.exe.
Previously, we added files in directive sections to the symbol
table as we read the sections, so the link order was depth-first.
That's not compatible with MSVC link.exe nor the old LLD.

This patch is to queue files so that new files are added to the
end of the queue and processed last. Now addFile() doesn't parse
files nor resolve symbols. You need to call run() to process
queued files.

llvm-svn: 240483
2015-06-23 23:56:39 +00:00
Lang Hames 49047039b0 [lld] Add MachO thread-local storage support.
This allows LLD to correctly link MachO objects that use thread-local storage.

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

llvm-svn: 240454
2015-06-23 20:35:31 +00:00
Simon Atanasyan 356d7c52b6 [Mips] Support R_MICROMIPS_HI0_LO16 relocation handling
llvm-svn: 240268
2015-06-22 09:27:05 +00:00
Simon Atanasyan f44f854af3 [Mips] Support R_MICROMIPS_LITERAL relocation handling
llvm-svn: 240267
2015-06-22 09:26:57 +00:00
Simon Atanasyan 692e792575 [Mips] Support R_MIPS_LITERAL relocation handling
llvm-svn: 240266
2015-06-22 09:26:48 +00:00
Simon Atanasyan ca0fe2f4a4 [Mips] Support R_MICROMIPS_SUB relocation handling
llvm-svn: 240265
2015-06-22 09:26:41 +00:00
Simon Atanasyan 1af72b898c [Mips] Reject R_MIPS_GPREL32 against external symbols
llvm-svn: 240264
2015-06-22 09:26:33 +00:00
Simon Atanasyan 74a07c5457 [Mips] Fix test case - do not use R_MIPS_GPREL32 against external symbols
No functional changes.

llvm-svn: 240263
2015-06-22 09:26:25 +00:00
Simon Atanasyan b5d26b1433 [Mips] Reject position-dependent relocations in case of shared library linking
llvm-svn: 240262
2015-06-22 09:26:20 +00:00
Simon Atanasyan 7851f3b111 [Mips] Fix test case - do not use R_MIPS_HI16 for shared library linking
No functional changes.

llvm-svn: 240261
2015-06-22 09:26:12 +00:00
Simon Atanasyan 46d97f246b [Mips] Support R_MICROMIPS_HIGHER / R_MICROMIPS_HIGHEST relocations handling
llvm-svn: 240260
2015-06-22 09:26:05 +00:00
Simon Atanasyan e55110454d [Mips] Support R_MIPS_HIGHER / R_MIPS_HIGHEST relocations handling
llvm-svn: 240259
2015-06-22 09:25:57 +00:00
Rui Ueyama a77336bd5d COFF: Support delay-load import tables.
DLLs are usually resolved at process startup, but you can
delay-load them by passing /delayload option to the linker.

If a /delayload is specified, the linker has to create data
which is similar to regular import table.
One notable difference is that the pointers in a delay-load
import table are originally pointing to thunks that resolves
themselves. Each thunk loads a DLL, resolve its name, and then
overwrites the pointer with the result so that subsequent
function calls directly call a desired function. The linker
has to emit thunks.

llvm-svn: 240250
2015-06-21 22:31:52 +00:00
Rui Ueyama 4d769c3a57 COFF: Support exception table.
.pdata section contains a list of triplets of function start address,
function end address and its unwind information. Linkers have to
sort section contents by function start address and set the section
address to the file header (so that runtime is able to find it and
do binary search.)

This change seems to resolve all but one remaining test failures in
check{,-clang,-lld} when building the entire stuff with clang-cl and
lld-link.

llvm-svn: 240231
2015-06-21 04:00:54 +00:00
Rui Ueyama 5e31d0b2e9 COFF: Fix common symbol alignment.
llvm-svn: 240217
2015-06-20 07:25:45 +00:00
Rui Ueyama efb7e1aa29 COFF: Fix a common symbol bug.
This is a case that one mistake caused a very mysterious bug.
I made a mistake to calculate addresses of common symbols, so
each common symbol pointed not to the beginning of its location
but to the end of its location. (Ouch!)

Common symbols are aligned on 16 byte boundaries. If a common
symbol is small enough to fit between the end of its real
location and whatever comes next, this bug didn't cause any harm.

However, if a common symbol is larger than that, its memory
naturally overlapped with other symbols. That means some
uninitialized variables accidentally shared memory. Because
totally unrelated memory writes mutated other varaibles, it was
hard to debug.

It's surprising that LLD was able to link itself and all LLD
tests except gunit tests passed with this nasty bug.

With this fix, the new COFF linker is able to pass all tests
for LLVM, Clang and LLD if I use MSVC cl.exe as a compiler.
Only three tests are failing when used with clang-cl.

llvm-svn: 240216
2015-06-20 07:21:57 +00:00
Rui Ueyama f00df0af2d COFF: Fix precedence between LIB and /libpath.
/libpath should take precedence over LIB.
Previously, LIB took precedence over /libpath.

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

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

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

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

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

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

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

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

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

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

llvm-svn: 240067
2015-06-18 21:50:38 +00:00
Rui Ueyama b95188cb2c COFF: Add /implib option.
llvm-svn: 240045
2015-06-18 20:27:09 +00:00
Rui Ueyama 2edb35a264 COFF: Handle /alternatename in .drectve section.
llvm-svn: 240037
2015-06-18 19:09:30 +00:00
Peter Collingbourne 8b2492f2a0 COFF: Implement DLL symbol exports for bitcode files.
Differential Revision: http://reviews.llvm.org/D10530

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

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

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

llvm-svn: 239978
2015-06-18 00:12:42 +00:00
Simon Atanasyan faf558224d [Mips] Use new llvm-readobj -mips-reginfo flag to check .reginfo content
No functional changes.

llvm-svn: 239962
2015-06-17 22:28:38 +00:00
Simon Atanasyan f8b082a6aa [Mips] Support R_MICROMIPS_GPREL16 / R_MICROMIPS_GPREL7_S2 relocations handling
llvm-svn: 239960
2015-06-17 22:27:54 +00:00
Simon Atanasyan 17478c5804 [Mips] Support R_MIPS_16 relocation handling
llvm-svn: 239959
2015-06-17 22:27:39 +00:00
Rui Ueyama 151d862d97 COFF: Create import library files.
On Windows, we have to create a .lib file for each .dll.
When linking against DLLs, the linker doesn't use the DLL files,
but instead read a list of dllexported symbols from corresponding
lib files.

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

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

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

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

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

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

llvm-svn: 239929
2015-06-17 19:19:25 +00:00