Commit Graph

3584 Commits

Author SHA1 Message Date
Zachary Turner c1e93e5fa4 Fix some differences between lld and MSVC generated PDBs.
A couple of things were different about our generated PDBs.

1) We were outputting the wrong Version on the PDB Stream.
   The version we were setting was newer than what MSVC is setting.
   It's not clear what the implications are, but we change LLD
   to use PdbImplVC70, as MSVC does.
2) For the optional debug stream indices in the DBI Stream, we
   were outputting 0 to mean "the stream is not present".  MSVC
   outputs uint16_t(-1), which is the "correct" way to specify
   that a stream is not present.  So we fix that as well.
3) We were setting the PDB Stream signature to 0.  This is supposed
   to be the result of calling time(nullptr).  Although this leads
   to non-deterministic builds, a better way to solve that is by
   having a command line option explicitly for generating a
   reproducible build, and have the default behavior of lld-link
   match the default behavior of link.

To test this, I'm making use of the new and improved `pdb diff`
sub command.  To make it suitable for writing tests against, I had
to modify the diff subcommand slightly to print less verbose output.
Previously it would always print | <column> | <value1> | <value2> |
which is quite verbose, and the values are fragile.  All we really
want to know is "did we produce the same value as link?"  So I added
command line options to print a single character representing the
result status (different, identical, equivalent), and another to
hide the value display.  Note that just inspecting the diff output
used to write the test, you can see some things that are obviously
wrong.  That is just reflective of the fact that this is the state
of affairs today, not that we're asserting that this is "correct".
We can use this as a starting point to discover differences, fix
them, and update the test.

Differential Revision: https://reviews.llvm.org/D35086

llvm-svn: 307422
2017-07-07 18:45:56 +00:00
Davide Italiano 6687318bab [LTO] Add a test for ThinLTO + --defsym.
We also get this right since r307303.

llvm-svn: 307412
2017-07-07 16:40:19 +00:00
Davide Italiano 406d8707f0 [LTO] Add a test for ThinLTO + --wrap.
We should get this right after r307303.

Differential Revision:  https://reviews.llvm.org/D35126

llvm-svn: 307410
2017-07-07 16:33:01 +00:00
George Rimar 1b51b7fc7f [ELF] - Fail the link if something happens on DWARF parsing stage of -gdb-index building
This is relative to PR33173,

Previously if something wrong happened on DWARF parsers side during parsing
object for building gdb index (like was in PR: unsupported relocation) 
then LLD continued and finished the link. DWARF parsers sure showed error
message on their side, but that is all.

Patch changes behavior to fail the link in this case and show more
detailed message.

Differential revision: https://reviews.llvm.org/D34814

llvm-svn: 307370
2017-07-07 09:18:05 +00:00
George Rimar 85e9216e8c [ELF] - Handle symbols with default version early.
This fixes last testcase provided in PR28414.
In short issue is next: when we had X@@Version symbol in object A,
we did not resolve it to X early. Then when in another object B
we had reference to undefined X, symbol X from archive was fetched.
Since both archive and object A contains another symbol Z, duplicate
symbol definition was triggered as a result.

Correct behavior is to use X@@Version from object A instead and do not fetch
any symbols from archive.

Differential revision: https://reviews.llvm.org/D35059

llvm-svn: 307364
2017-07-07 08:29:51 +00:00
Zachary Turner cea99709c4 Fix lld tests after r307356.
llvm-svn: 307360
2017-07-07 05:41:25 +00:00
David L. Jones d4053fb2ec Change remaining references to lit.util.capture to use subprocess.check_output.
Summary:
The capture() function was removed in r306625. This should fix PGO breakages
reported by Michael Zolotukhin.

Reviewers: mzolotukhin

Subscribers: sanjoy, llvm-commits

Differential Revision: https://reviews.llvm.org/D35088

llvm-svn: 307320
2017-07-06 21:46:47 +00:00
Reid Kleckner ffa5180f39 Fix pdb-scopes.test
llvm-svn: 307280
2017-07-06 16:41:15 +00:00
Reid Kleckner 3f85192930 [PDB] Fill in "Parent" and "End" fields of scope-like symbol records
Summary:
There are a variety of records that open scopes: function scopes, block
scopes, and inlined call site scopes. These symbol records contain
Parent and End fields with the offsets of other symbol records. The End
field contains the offset of the matching S_END or S_INLINESITE_END
record. The Parent field contains the offset of the parent record, or 0
if this is a top-level scope (i.e. a function).

With this change, `llvm-pdbutil pretty -all` no longer crashes on PDBs
produced by LLD. I haven't tried a real debugger yet.

Reviewers: zturner, ruiu

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D34898

llvm-svn: 307278
2017-07-06 16:39:32 +00:00
Eric Beckmann f6090b620e Revert "Revert "Revert "Switch external cvtres.exe for llvm's own resource library."""
This reverts commit ae21ee0b6cacbc1efaf4d42502e71da2f0eb45c3.

The initial revert was done in order to prevent ongoing errors on
chromium bots such as CrWinClangLLD.  However, this was done haphazardly
and I didn't realize there were test and compilation failures, so this
revert was reverted.  Now that those have been fixed, we can revert the
revert of the revert.

llvm-svn: 307227
2017-07-05 23:46:06 +00:00
Eric Beckmann 81979b038f Revert "Revert "Revert "Replace trivial use of external rc.exe by writing our own .res file."""
This reverts commit 5fecbbbe5049665d86834cf69d8f75db4f392308.

The initial revert was done in order to prevent ongoing errors on
chromium bots such as CrWinClangLLD.  However, this was done haphazardly
and I didn't realize there were test and compilation failures, so this
revert was reverted.  Now that those have been fixed, we can revert the
revert of the revert.

llvm-svn: 307226
2017-07-05 23:45:50 +00:00
Eric Beckmann 1d50926e71 Revert "Revert "Replace trivial use of external rc.exe by writing our own .res file.""
This reverts commit 8c8dce3b8f15d6ebaefc35ce88f15a85c8cdbd6e.

llvm-svn: 307191
2017-07-05 19:04:48 +00:00
Eric Beckmann 0eafa581a3 Revert "Revert "Switch external cvtres.exe for llvm's own resource library.""
This reverts commit 165e578e47f1cd38191120aad23a9020fb5476dd.

Forgot to run tests on this.

llvm-svn: 307190
2017-07-05 19:04:33 +00:00
Eric Beckmann 36793a0ecf Revert "Switch external cvtres.exe for llvm's own resource library."
This reverts commit 600d52c278e123dd08bee24c1f00932b55add8de.

This patch still seems to break CrWinClangLLD, reverting until I can
find root problem.

llvm-svn: 307189
2017-07-05 18:59:16 +00:00
Eric Beckmann 8cc9fd31e6 Revert "Replace trivial use of external rc.exe by writing our own .res file."
This patch still seems to break CrWinClangLLD, reverting this once more
until I can discover root problem.

This reverts commit 3dbbc8ce43be50ffde2b1c655c6d3a25796fe78b.

llvm-svn: 307188
2017-07-05 18:59:01 +00:00
Sean Eveson d54759b236 [ELF] Fix nullptr dereference when creating an error message for a synthetic section.
Differential Revision: https://reviews.llvm.org/D34752

llvm-svn: 307162
2017-07-05 14:55:43 +00:00
Rafael Espindola cf00d4374e Fix PR33635.
This is a semantic revert of r306036.

We have to change the names, otherwise dynamic relocations will point
to the wrong name.

llvm-svn: 307110
2017-07-05 00:43:18 +00:00
Rafael Espindola 909899cc47 Improve error message.
Before we would sometimes not mention that the relocation was in a ro
area before.

llvm-svn: 307089
2017-07-04 15:53:57 +00:00
Rafael Espindola aec6287f5d Make the test more strict.
We now don't produce "has undefined version" errors when producing
binaries even when there is a linker script.

llvm-svn: 307082
2017-07-04 14:59:28 +00:00
George Rimar 4d2f976208 [ELF] - Resolve references properly when using .symver directive
This is PR28414. 
Previously LLD was unable to link following:
(failed with undefined symbol bar)

Version script:
SOME_VERSION { global: *; };

.global _start
.global bar
.symver _start, bar@@SOME_VERSION
_start:
  jmp bar
Manual has next description:

.symver name, name2@@nodename
In this case, the symbol name must exist and be defined within the file being assembled. It is similar to name2@nodename. 
The difference is name2@@nodename will also be used to resolve references to name2 by the linker
https://sourceware.org/binutils/docs/as/Symver.html

Patch implements that. If we have name@@ver symbol and name is undefined, name@@ver is used to resolve references to name.
If name is defined then multiple definition error is emited, that is consistent with what bfd do.

Differential revision: https://reviews.llvm.org/D33680

llvm-svn: 307077
2017-07-04 13:19:13 +00:00
Rafael Espindola bd9682a1ad Add a test for a recent regression.
llvm-svn: 307041
2017-07-03 16:49:17 +00:00
Petr Hosek 52db9a4fe6 [ELF] Remove unused synthetic sections from script commands
Script commands are processed before unused synthetic sections are
removed. Therefore, if a linker script matches one of these sections
it'll get emitted as an empty output section because the logic for
removing unused synthetic sections ignores script commands which
could have already matched and captured one of these sections. This
patch fixes that by also removing the unused synthetic sections from
the script commands.

Differential Revision: https://reviews.llvm.org/D34800

llvm-svn: 307037
2017-07-03 15:49:25 +00:00
George Rimar bd4943e2ca [ELF] - Remove use of precomipled binary for invalid-relocation-x64.test
Since yaml2obj is now able to parse integer values as relocation types,
there is no need to keep hex edited elf object for this test,
we can produce it on fly, just like this patch do.

Differential revision: https://reviews.llvm.org/D34893

llvm-svn: 307032
2017-07-03 15:24:50 +00:00
Andrew Ng a020d3487e [LLD][LinkerScript] Allow non-alloc sections to be assigned to segments.
This patch makes changes to allow sections without the SHF_ALLOC bit to be
assigned to segments in a linker script.

The assignment of output sections to segments is performed in
LinkerScript::createPhdrs. Previously, this function would bail as soon as it
encountered an output section which did not have the SHF_ALLOC bit set, thus
preventing any output section without SHF_ALLOC from being assigned to a
segment.

This restriction has now been removed from LinkerScript::createPhdrs and instead
a check for SHF_ALLOC has been added to LinkerScript::adjustSectionsAfterSorting
to not propagate program headers to sections without SHF_ALLOC which matches the
behaviour of bfd linker scripts.

Differential Revision: https://reviews.llvm.org/D34204

llvm-svn: 307013
2017-07-03 10:11:25 +00:00
Rui Ueyama 80cbc776cb Revert r306813: "[ELF] - Resolve references properly when using .symver directive"
This reverts commit r306813 because it broke linking of the
FreeBSD base system.

llvm-svn: 306996
2017-07-03 03:26:07 +00:00
Martin Storsjo 94fce50950 [COFF] Add initial support for linking ARM64 binaries
A plain empty entry point function that returns 0 seems to produce
a binary that loads and runs fine in wine.

Differential Revision: https://reviews.llvm.org/D34833

llvm-svn: 306963
2017-07-01 20:29:27 +00:00
Eric Beckmann 602afcf7e9 Revert "Revert "Replace trivial use of external rc.exe by writing our own .res file.""
Summary:
This reverts commit 51931072a7c9a52540baf76fc30ef391d2529a2f.

This revert was originally done because the integrations of the new
WindowsResource library into LLD was causing error in chromium, due to
bugs in how resource sections were handled.  These bugs were fixed,
meaning that the features may be reintegrated.

Subscribers: hiraditya, llvm-commits

Differential Revision: https://reviews.llvm.org/D34922

llvm-svn: 306941
2017-07-01 03:59:54 +00:00
Zachary Turner af8c75a8c0 [llvm-pdbutil] Output the symbol offset when dumping.
Type records have a unique type index, but symbol records do
not.  Instead, symbol records refer to other symbol records
by referencing their offset in the symbol stream.  In a sense
this is the analogue of the TypeIndex, but we are not printing
it in the dumper.  Printing it not only gives us more useful
information when manually investigating the contents of a PDB,
but also allows us to write better tests by enabling us to
verify that fields that reference other symbol records do
so correctly.

Differential Revision: https://reviews.llvm.org/D34906

llvm-svn: 306890
2017-06-30 21:35:00 +00:00
Eric Beckmann 5de7361097 Tighten up tests for .rsrc section emission.
Summary:
There have been bugs with the WindowsResource library, such as incorrect
symbols for addresses.  Directly checking the .rsrc in the final PE will
help ensure this doesn't happen again.

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D34900

llvm-svn: 306854
2017-06-30 18:22:11 +00:00
George Rimar aad84e2ee2 [ELF] - Resolve references properly when using .symver directive
This is PR28414. 
Previously LLD was unable to link following:
(failed with undefined symbol bar)

```
Version script:
SOME_VERSION { global: *; };

.global _start
.global bar
.symver _start, bar@@SOME_VERSION
_start:
  jmp bar
```

Manual has next description:
//
.symver name, name2@@nodename
In this case, the symbol name must exist and be defined within the file being assembled. It is similar to name2@nodename. 
**The difference is name2@@nodename will also be used to resolve references to name2 by the linker**
https://sourceware.org/binutils/docs/as/Symver.html
//

Patch implements that. If we have name@@ver symbol and name is undefined, 
name@@ver is used to resolve references to name.

Differential revision: https://reviews.llvm.org/D33680

llvm-svn: 306813
2017-06-30 10:19:54 +00:00
Martin Storsjo e24f434eb2 Update a test case after LLVM r306794
llvm-svn: 306796
2017-06-30 07:02:59 +00:00
Eric Beckmann d40dd64ff0 Revert "Replace trivial use of external rc.exe by writing our own .res file."
This reverts commit d4c7e9fc63c10dbab0c30186ef8575474a704496.

This is done in order to address the failure of CrWinClangLLD etc. bots.
These throw an error of "side-by-side configuration is incorrect" during
compilation, which sounds suspiciously related to these manifest
changes.

Revert "Switch external cvtres.exe for llvm's own resource library."

This reverts commit 71fe8ef283a9dab9a3f21432c98466cbc23990d1.

llvm-svn: 306618
2017-06-29 00:17:26 +00:00
Reid Kleckner a1001b8f38 [COFF] Allow debug info to relocate against discarded symbols
Summary:
In order to do this without switching on the symbol kind multiple times,
I created Defined::getChunkAndOffset and use that instead of
SymbolBody::getRVA in the inner relocation loop.

Now we get the symbol's chunk before switching over relocation types, so
we can test if it has been discarded outside the inner relocation type
switch. This also simplifies application of section relative
relocations. Previously we would switch on symbol kind to compute the
RVA, then the relocation type, and then the symbol kind again to get the
output section so we could subtract that from the symbol RVA. Now we
*always* have an OutputSection, so applying SECREL and SECTION
relocations isn't as much of a special case.

I'm still not quite happy with the cleanliness of this code. I'm not
sure what offsets and bases we should be using during the relocation
processing loop: VA, RVA, or OutputSectionOffset.

Reviewers: ruiu, pcc

Reviewed By: ruiu

Subscribers: majnemer, inglorion, llvm-commits, aprantl

Differential Revision: https://reviews.llvm.org/D34650

llvm-svn: 306566
2017-06-28 17:06:35 +00:00
Rui Ueyama 0cc1483562 Add basic 64-bit SPARC support
Add support for the most common SPARC relocations.
Make DT_PLTGOT point to the PLT on SPARC.
Mark the PLT as executable on SPARC.

This adds a basic test that creates a SPARV9 executable
that invokes the exit system call on OpenBSD.

Patch by Mark Kettenis.

Differential Revision: https://reviews.llvm.org/D34618

llvm-svn: 306565
2017-06-28 17:05:39 +00:00
George Rimar e0b43df32e [ELF] - Do not crash when LLD synthesizes output sections with BYTE commands and -r
This is PR33596. Previously LLD would crash
because BYTE command synthesized output section,
but it was not assigned to Sec member of OutputSectionCommand.

Behaviour of -script and -r combination is not well defined,
but it seems after this change LLD naturally inherits behavior of
GNU linkers - creates output section requested in script and does not
crash anymore.

Differential revision: https://reviews.llvm.org/D34676

llvm-svn: 306527
2017-06-28 09:59:34 +00:00
George Rimar dbe843d602 [ELF] - Do not set st_size field of SHT_UNDEF symbols.
This fixes PR33598.

Size field for undefined symbols is not significant.
Setting it to fixed value, like zero, may be useful though.

For example when we have 2 DSO's, like in this PR, if lower level DSO may
change slightly (in part of some symbol's st_size)  and higher-level DSO is
rebuilt, then tools that monitoring checksum of high level DSO file can notice
it and trigger cascade of some other unnecessary actions. 
If we set st_size to zero, that can be avoided.

Differential revision: https://reviews.llvm.org/D34673

llvm-svn: 306526
2017-06-28 09:51:33 +00:00
Peter Smith 691ff76662 [ELF] Consolidate .ARM.extab.* sections into .ARM.extab
When -ffunction-sections and ARM C++ exceptions are used each .text.suffix
section will have at least one .ARM.exidx.suffix section and may have an
additional .ARM.extab.suffix section if the unwinding instructions are too
large to inline into the .ARM.exidx table entry. For a large program without
a linker script this can lead to a large number of section header table
entries that can increase the size of the ELF file.

This change introduces a default rule for .ARM.extab.* to be placed in
a single output section called .ARM.extab . This follows the behavior of
ld.gold and ld.bfd.

fixes pr33407

Differential Revision: https://reviews.llvm.org/D34678

llvm-svn: 306522
2017-06-28 09:12:38 +00:00
Rafael Espindola 36f2edb6fd Check the produced file instead of stderr.
It is somewhat pointless to check that a specific error is not
produced. That is already checked by the ld.lld exit value.

Instead make the test a bit stronger by checking that the output file
has the expected symbol and section.

llvm-svn: 306496
2017-06-28 01:46:31 +00:00
Eric Beckmann 01822fa1c1 Add more stringent tests for the resource section of executables.
Summary: The testing on the resource section of executables produced by lld has been very lax, and allowed a major bug to go unnoticed when we switched from shelling out to cvtres.exe to using llvm's own library.  These additional tests should cover all the major failure points.

Reviewers: zturner, ruiu

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D34664

llvm-svn: 306465
2017-06-27 21:28:55 +00:00
Ed Maste 87b0ab1f99 Sort lit.cfg architectures
From Mark Kettenis in review D34618.

llvm-svn: 306403
2017-06-27 12:21:50 +00:00
Rui Ueyama 921d43fbb2 Add trap instructions for ARM and MIPS.
This patch fills holes in executable sections with 0xd4 (ARM) or
0xef (MIPS). These trap instructions were suggested by Theo de Raadt.

llvm-svn: 306322
2017-06-26 19:45:53 +00:00
Eric Beckmann 2a81089116 Replace trivial use of external rc.exe by writing our own .res file.
This patch removes the dependency on the external rc.exe tool by writing
a simple .res file using our own library. In this patch I also added an
explicit definition for the .res file magic.  Furthermore, I added a
unittest for embeded manifests and fixed a bug exposed by the test.

llvm-svn: 306311
2017-06-26 17:43:30 +00:00
Reid Kleckner eb8c0f9d51 [COFF] Fix SECREL and SECTION relocations against common symbols
Summary:
They do the obvious thing: provide the section index of .bss and the
offset of the symbol in .bss.

Reviewers: ruiu

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D34628

llvm-svn: 306304
2017-06-26 16:45:36 +00:00
Reid Kleckner 892d2a5767 Add .yaml as an lld test suffix
Over time we've started to add inputs and test cases using the .yaml
extension, which seems to be preferred over the .objtxt extension that
we were using initially. One nice thing about using .yaml is that it
triggers existing editor highlighting and formatting support.

Fix two pdb*.yaml test cases that I added that weren't being run as part
of check-lld.

llvm-svn: 306303
2017-06-26 16:42:44 +00:00
Rui Ueyama fb02869b74 Remove a stale comment.
llvm-svn: 306295
2017-06-26 15:51:28 +00:00
Reid Kleckner 502d4ce2e4 [COFF] Improve synthetic symbol handling
Summary:
The main change is that we can have SECREL and SECTION relocations
against ___safe_se_handler_table, which is important for handling the
debug info in the MSVCRT.

Previously we were using DefinedRelative for __safe_se_handler_table and
__ImageBase, and after we implement CFGuard, we plan to extend it to
handle __guard_fids_table, __guard_longjmp_table, and more.  However,
DefinedRelative is really only suitable for implementing __ImageBase,
because it lacks a Chunk, which you need in order to figure out the
output section index and output section offset when resolving SECREl and
SECTION relocations.

This change renames DefinedRelative to DefinedSynthetic and gives it a
Chunk. One wart is that __ImageBase doesn't have a chunk. It points to
the PE header, effectively. We could split DefinedRelative and
DefinedSynthetic if we think that's cleaner and creates fewer special
cases.

I also added safeseh.s, which checks that we don't emit a safe seh table
entries pointing to garbage collected handlers and that we don't emit a
table at all when there are no handlers.

Reviewers: ruiu

Reviewed By: ruiu

Subscribers: inglorion, pcc, llvm-commits, aprantl

Differential Revision: https://reviews.llvm.org/D34577

llvm-svn: 306293
2017-06-26 15:39:52 +00:00
Peter Smith 113a59e7db [ELF] Define _GLOBAL_OFFSET_TABLE_ symbol relative to .got
On many architectures gcc and clang will recognize _GLOBAL_OFFSET_TABLE_ - .
and produce a relocation that can be processed without needing to know the
value of _GLOBAL_OFFSET_TABLE_. This is not always the case; for example ARM
gcc produces R_ARM_BASE_PREL but clang produces the more general
R_ARM_REL32 to _GLOBAL_OFFSET_TABLE_. To evaluate this relocation
correctly _GLOBAL_OFFSET_TABLE_ must be defined to be the either the base of
the GOT or end of the GOT dependent on architecture..

If/when llvm-mc is changed to recognize _GLOBAL_OFFSET_TABLE_ - . this
change will not be necessary for new objects. However there may still be
old objects and versions of clang.

Differential Revision: https://reviews.llvm.org/D34355

llvm-svn: 306282
2017-06-26 10:22:17 +00:00
Petar Jovanovic 661102a339 [mips] update expected output in test/ELF/mips-plt-r6.s
r306095 in LLVM has changed register positions for instruction aui.
This change updates the test that checks for aui.

llvm-svn: 306173
2017-06-23 22:30:28 +00:00
Peter Collingbourne 36d43dd958 Add parens for python3.
llvm-svn: 306128
2017-06-23 17:11:50 +00:00
Peter Collingbourne 8d29223386 Add a ThinLTO cache policy for controlling the maximum cache size in bytes.
This is useful when an upper limit on the cache size needs to be
controlled independently of the amount of the amount of free space.

One use case is a machine with a large number of cache directories
(e.g. a buildbot slave hosting a large number of independent build
jobs). By imposing an upper size limit on each cache directory,
users can more easily estimate the server's capacity.

Differential Revision: https://reviews.llvm.org/D34547

llvm-svn: 306126
2017-06-23 17:05:03 +00:00
Reid Kleckner 8456411e3b [COFF] Fix SECTION and SECREL relocation handling for absolute symbols
Summary:
For SECTION relocations against absolute symbols, MSVC emits the largest
output section index plus one. I've implemented that by threading a
global variable through DefinedAbsolute that is filled in by the Writer.
A more library-oriented approach would be to thread the Writer through
Chunk::writeTo and SectionChunk::applyRel*, but Rui seems to prefer
doing it this way.

MSVC rejects SECREL relocations against absolute symbols, but only when
the relocation is in a real output section. When the relocation is in a
CodeView debug info section destined for the PDB, it seems that this
relocation error is suppressed, and absolute symbols become zeros in the
object file. This is easily implemented by checking the input section
from which we're applying relocations.

This should fix errors about __safe_se_handler_table and
__guard_fids_table when linking the CRT and generating a PDB.

Reviewers: ruiu

Subscribers: aprantl, llvm-commits

Differential Revision: https://reviews.llvm.org/D34541

llvm-svn: 306071
2017-06-22 23:33:04 +00:00
Zachary Turner 7df69958f8 [llvm-pdbutil] Rename "raw" to "dump".
Now you run llvm-pdbutil dump <options>.  This is a followup
after having renamed the tool, whereas before raw was obviously
just the style of dumping, whereas now "dump" is the action to
perform with the "util".

llvm-svn: 306055
2017-06-22 20:57:39 +00:00
Rui Ueyama 4402a39981 Keep the original symbol name when renamed.
Previously, when symbol A is renamed B, both A and B end up having
the same name. This is because name is a symbol's attribute, and
we memcpy symbols for symbol renaming.

This pathc saves the original symbol name and restore it after memcpy
to keep the original name.

This patch shouldn't change program's meaning, but names in symbol
tables make more sense than before.

llvm-svn: 306036
2017-06-22 17:30:19 +00:00
Reid Kleckner d0e6e24a53 [PDB] Add symbols to the PDB
Summary:
The main complexity in adding symbol records is that we need to
"relocate" all the type indices. Type indices do not have anything like
relocations, an opaque data structure describing where to find existing
type indices for fixups. The linker just has to "know" where the type
references are in the symbol records. I added an overload of
`discoverTypeIndices` that works on symbol records, and it seems to be
able to link the standard library.

Reviewers: zturner, ruiu

Subscribers: llvm-commits, hiraditya

Differential Revision: https://reviews.llvm.org/D34432

llvm-svn: 305933
2017-06-21 17:25:56 +00:00
Rui Ueyama 28ea8c7ad7 [COFF] Set MajorLinkerVersion to 14 instead of 0.
This works around a strange interaction with Authenticode signatures,
in which a signed PE executable with {Major,Minor}LinkerVersion = 0.0
fails to validate on Windows 7 (but is OK on Windows 10). Setting the
linker version to 14.0 (which is what VS2015 outputs) makes it work
again.

Patch by Simon Tatham <simon.tatham@arm.com>.

llvm-svn: 305929
2017-06-21 16:42:08 +00:00
Rui Ueyama d1f8b8162b Implement the --exclude-libs option.
The --exclude-libs option is not a popular option, but at least some
programs in Android depend on it, so it's worth to support it.

Differential Revision: https://reviews.llvm.org/D34422

llvm-svn: 305920
2017-06-21 15:36:24 +00:00
Rui Ueyama 2f740f74bb Define __guard_{iat,longjmp}_{count,table} symbols.
VC2017 contains these new symbols as undefined symobls. They are used
for /guard:cf. Since we do not support the control flow guard, but we
want to at least ignore these symbols so that we can link against VS2017
libraries.

Fixes https://bugs.chromium.org/p/chromium/issues/detail?id=727193.

llvm-svn: 305876
2017-06-21 02:26:19 +00:00
Reid Kleckner 91ef9de643 [codeview] YAMLize all section offsets and indices in symbol records
We forgot to serialize these because llvm-readobj didn't dump them. They
are typically all zeros in an object file. The linker fills them in with
relocations before adding them to the PDB. Now we can properly round
trip these symbols through pdb2yaml -> yaml2pdb.

I made these fields optional with a zero default so that we can elide
them from our test cases.

llvm-svn: 305857
2017-06-20 21:19:22 +00:00
Reid Kleckner f5bb738f75 [PDB] Don't emit debug info associated with dead chunks
Summary:
Previously we didn't add debug info chunks to the SparseChunks array, so
they didn't participate in section GC. Now we do.

Reviewers: ruiu

Subscribers: aprantl, llvm-commits

Differential Revision: https://reviews.llvm.org/D34356

llvm-svn: 305811
2017-06-20 17:14:09 +00:00
Rafael Espindola 9c0395e39e Prefer -Ttext over linker script values.
I found this while trying to build u-boot. It uses -Ttext in
combination with linker scripts.

My first reaction was to change the linker scripts to have the correct
value, but I found that it is actually quite convenient to have -Ttext
take precedence.

By having just

.text : { *(.text) }

In the script, they can define the text address in a single Makefile
and pass it to ld with -Ttext and for the C code with
-DFoo=value. Doing the same with linker scripts would require them to
be generated during the build.

llvm-svn: 305766
2017-06-20 01:51:50 +00:00
Kevin Enderby 2a833e0123 Fix lld test that was causing llvm-clang-lld-x86_64-scei-ps4-ubuntu-fast
to fail due to change in llvm trunk r305744.

llvm-svn: 305747
2017-06-19 21:48:29 +00:00
Reid Kleckner adea0ce7ca Fix build and PDB test on Linux
llvm-svn: 305716
2017-06-19 17:27:31 +00:00
Rui Ueyama 86360d6902 Replace CRLF with LF.
llvm-svn: 305714
2017-06-19 17:23:04 +00:00
Reid Kleckner 44cdb10964 [PDB] Start emitting source file and line information
Summary:
This is a first step towards getting line info to show up in VS and
windbg. So far, only llvm-pdbutil can parse the PDBs that we produce.
cvdump doesn't like something about our file checksum tables. I'll have
to dig into that next.

This patch adds a new DebugSubsectionRecordBuilder which takes bytes
directly from some other producer, such as a linker, and sticks it into
the PDB. Line tables only need to be relocated. No data needs to be
rewritten.

File checksums and string tables, on the other hand, need to be re-done.

Reviewers: zturner, ruiu

Subscribers: llvm-commits, hiraditya

Differential Revision: https://reviews.llvm.org/D34257

llvm-svn: 305713
2017-06-19 17:21:45 +00:00
Andrew Ng 6e9f98c198 [LLD][LinkerScript] Add support for segment NONE.
This patch adds support for segment NONE in linker scripts which enables the
specification that a section should not be assigned to any segment.

Note that GNU ld does not disallow the definition of a segment named NONE, which
if defined, effectively overrides the behaviour described above. This feature
has been copied.

Differential Revision: https://reviews.llvm.org/D34203

llvm-svn: 305700
2017-06-19 15:28:58 +00:00
James Henderson 38e67e8212 [ELF] Emit only one error if -z max-page-size without value
In r305364, Rui changed the mechanism that parses -z option values slightly.
This caused a bug, as demonstrated by this test, which now fails:

---
# REQUIRES: x86
# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
# RUN: ld.lld %t.o -o %t -z max-page-size

.global _start
_start:
  nop
---

Before, the link succeeded and set the max-page-size to the target default.

After we get the following two error messages:
"invalid max-page-size: "
"max-page-size: value isn't a power of 2"

The latter error is because an uninitialised variable ends up being passed back
to getMaxPageSize).

This change ensures we only get the first error.

Reviewers: ruiu

Differential Revision: https://reviews.llvm.org/D34234

llvm-svn: 305679
2017-06-19 11:05:15 +00:00
Rafael Espindola 4f1fca270a Error when discarding .dynstr.
We would crash before.

llvm-svn: 305615
2017-06-16 23:53:36 +00:00
Rafael Espindola 656cc20f5b Error when discarding .dynsym.
We would crash instead before.

llvm-svn: 305614
2017-06-16 23:50:09 +00:00
Rafael Espindola 2af64b0bf8 Error on trying to discard .dynamic.
We would crash instead before.

llvm-svn: 305613
2017-06-16 23:45:35 +00:00
Eric Beckmann d135e8c039 Switch external cvtres.exe for llvm's own resource library.
In this patch, I flip the switch in DriverUtils from using the external
cvtres.exe tool to using the Windows Resource library in llvm.

I also fixed a bug where .rsrc sections were marked as discardable
memory and therefore were placed in the wrong order in the final PE.

Furthermore, I modified WindowsResource to write the coff directly to a
memory buffer instead of to file, also had it use the machine types
already declared in COFF.h instead creating my own enum.

Finally, I flipped the switch to allow all unit tests that had
previously run only on windows due to a winres dependency to run
cross-platform.

Reviewers: zturner, ruiu

Subscribers: llvm-commits, hiraditya

Differential Revision: https://reviews.llvm.org/D34265

llvm-svn: 305592
2017-06-16 21:13:24 +00:00
Reid Kleckner f8b992dd08 Give associative-comdat lld test a triple
llvm-svn: 305585
2017-06-16 20:51:42 +00:00
Reid Kleckner 79ac99b3e8 [COFF] Drop unused comdat sections when GC is turned off
Summary:
Adds a "Discarded" bool to SectionChunk to indicate if the section was
discarded by COMDAT deduplication. The Writer still just checks
`isLive()`.

Fixes PR33446

Reviewers: ruiu

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D34288

llvm-svn: 305582
2017-06-16 20:47:19 +00:00
Zachary Turner 56ed9deafc Remove some non-determinstic numbers from pdb test.
llvm-svn: 305520
2017-06-15 22:41:45 +00:00
Zachary Turner 93f986a7dc Fix pdb embedded paths.
Obviously the paths are going to be machine dependent, so
don't try to match them in the test.

llvm-svn: 305518
2017-06-15 22:30:00 +00:00
Zachary Turner 6305545527 Resubmit "[llvm-pdbutil] rewrite the "raw" output style."
This resubmits commit c0c249e9f2ef83e1d1e5f166b50673d92f3579d7.

It was broken due to some weird template issues, which have
since been fixed.

llvm-svn: 305517
2017-06-15 22:24:24 +00:00
Zachary Turner da504b794c Revert "[llvm-pdbutil] rewrite the "raw" output style."
This reverts commit 83ea17ebf2106859a51fbc2a86031b44d33696ad.

This is failing due to some strange template problems, so reverting
until it can be straightened out.

llvm-svn: 305505
2017-06-15 20:55:51 +00:00
Saleem Abdulrasool bc7ff7027e COFF: add support for lib mode usage
When link is invoked with `/def:` and no input files, it behaves as if
`lib.exe` was invoked.  Emulate this behaviour, generating the import
library from the def file that was passed.  Because there is no input to
actually generate the dll, we simply process the def file early and exit
once we have created the import library.

llvm-svn: 305502
2017-06-15 20:39:58 +00:00
Zachary Turner b560fdf3b8 [llvm-pdbutil] rewrite the "raw" output style.
After some internal discussions, we agreed that the raw output style had
outlived its usefulness. It was originally created before we had even
thought of dumping to YAML, and it was intended to give us some insight
into the internals of a PDB file. Now we have YAML mode which does
almost exactly this but is more powerful in that it can round-trip back
to a PDB, which the raw mode could not do. So the raw mode had become
purely a maintenance burden.

One option was to just delete it. However, its original goal was to be
as readable as possible while staying close to the "metal" - i.e.
presenting the output in a way that maps directly to the underlying file
format. We don't actually need that last requirement anymore since it's
covered by the yaml mode, so we could repurpose "raw" mode to actually
just be as readable as possible.

This patch implements about 80% of the functionality previously in raw
mode, but in a completely different style that is more akin to what
cvdump outputs. Records are very compressed, often times appearing on
just one line. One nice thing about this is that it makes full record
matching easier, because you can grep for indices, names, and leaf types
on a single line often.

See the tests for some examples of what the new output looks like.

Note that this patch actually regresses the functionality of raw mode in
a few areas, but only because the patch was already unreasonably large
and going 100% would have been even worse. Specifically, this patch is
missing:

The ability to dump module debug subsections (checksums, lines, etc)
The ability to dump section headers
Aside from that everything is here. While goign through the tests fixing
them all up, I found many duplicate tests. They've been deleted. In
subsequent patches I will go through and re-add the missing
functionality.

Differential Revision: https://reviews.llvm.org/D34191

llvm-svn: 305495
2017-06-15 19:34:41 +00:00
Petr Hosek 40f2866a67 [ELF] Mark symbols referenced from linker script as live
This is necessary to ensure that sections containing symbols referenced
from linker scripts (e.g. in data commands) don't get GC'd.

Differential Revision: https://reviews.llvm.org/D34195

llvm-svn: 305452
2017-06-15 05:34:31 +00:00
Leslie Zhai 49ba795d15 [ELF] Initial migration of AVR target
Reviewers: ruiu, rafael, grimar, atanasyan, psmith, dylanmckay

Reviewed By: ruiu, rafael, grimar, dylanmckay

Differential Revision: https://reviews.llvm.org/D32991

llvm-svn: 305444
2017-06-15 02:27:50 +00:00
Zachary Turner a8cfc29c9a Resubmit "[codeview] Make obj2yaml/yaml2obj support .debug$S..."
This was originally reverted because of some non-deterministic
failures on certain buildbots.  Luckily ASAN eventually caught
this as a stack-use-after-scope, so the fix is included in
this patch.

llvm-svn: 305393
2017-06-14 15:59:27 +00:00
Zachary Turner 0085dce221 Revert "[codeview] Make obj2yaml/yaml2obj support .debug$S..."
This is causing failures on linux bots with an invalid stream
read.  It doesn't repro in any configuration on Windows, so
reverting until I have a chance to investigate on Linux.

llvm-svn: 305371
2017-06-14 06:24:24 +00:00
Zachary Turner a3da4467fa [codeview] Make obj2yaml/yaml2obj support .debug$S/T sections.
This allows us to use yaml2obj and obj2yaml to round-trip CodeView
symbol and type information without having to manually specify the bytes
of the section. This makes for much easier to maintain tests. See the
tests under lld/COFF in this patch for example. Before they just said
SectionData: <blob> whereas now we can use meaningful record
descriptions. Note that it still supports the SectionData yaml field,
which could be useful for initializing a section to invalid bytes for
testing, for example.

Differential Revision: https://reviews.llvm.org/D34127

llvm-svn: 305366
2017-06-14 05:31:00 +00:00
Rafael Espindola dece28087e Set non alloc section address to 0 earlier.
Currently we do layout as if non alloc sections had an actual address
and then set it to zero. This produces a few odd results where a
symbol has an address that is inconsistent with the section address.

The simplest way to fix it is probably to just set the address earlier.

The behavior of bfd seems to be similar, but it only sets the non
alloc section address is missing from the linker script or if the
script has an explicit " : 0" setting the address of the output
section (which the default script does).

llvm-svn: 305323
2017-06-13 20:57:43 +00:00
Sam Clegg be373670ec Add llvm-pdbutil tool_patterns in lit.cfg
This means that 'llvm-pdbutil' in test commands will resolve
to the absolute path to the tool, in line with what happens
already for other tools.

This works either way because the bin directory is also
prepended to the PATH.  I'm not sure why both methods are
used.

Differential Revision: https://reviews.llvm.org/D34128

llvm-svn: 305297
2017-06-13 15:53:53 +00:00
Reid Kleckner 8cbdd0c0f2 [PDB] Add a module descriptor for every object file
Summary:
Expose the module descriptor index and fill it in for section
contributions.

Reviewers: zturner

Subscribers: llvm-commits, ruiu, hiraditya

Differential Revision: https://reviews.llvm.org/D34126

llvm-svn: 305296
2017-06-13 15:49:13 +00:00
Reid Kleckner 41ff570657 [lld] Add .s to the test suffix list for all lld ports
We have two .s test files in lld/test/COFF that weren't being run as
part of check-lld. They both pass locally for me.

llvm-svn: 305295
2017-06-13 15:39:49 +00:00
Zachary Turner fa18b2f68c Fix broken LLD test.
llvm-svn: 305260
2017-06-13 00:30:09 +00:00
Rafael Espindola 4c4becf83c Also check section address in test.
This shows an oddity of this output. While the section address is 0,
the the symbol address is computed as if the section was allocatable.

llvm-svn: 305250
2017-06-12 23:22:00 +00:00
Zachary Turner 68ea80d0a7 Slightly better fix for dealing with no-id-stream PDBs.
The last fix required the user to manually add the required
feature.  This caused an LLD test to fail because I failed to
update LLD.  In practice we can hide this logic so it can just
be transparently added when we write the PDB.

llvm-svn: 305236
2017-06-12 21:46:51 +00:00
Peter Collingbourne 89061b2224 IR: Replace the "Linker Options" module flag with "llvm.linker.options" named metadata.
The new metadata is easier to manipulate than module flags.

Differential Revision: https://reviews.llvm.org/D31349

llvm-svn: 305227
2017-06-12 20:10:48 +00:00
Rui Ueyama f9f69548a9 Allow the GNU gold-style nonstandard SHT_GROUP section.
The ELF standard defines that the SHT_GROUP section as follows:

 - its sh_link has the symbol index, and
 - the symbol name is used to uniquify section groups.

Object files created by GNU gold does not seem to comply with the
standard. They have this additional rule:

 - if the symbol has no name and a STT_SECTION symbol, a section
   name is used instead of a symbol name.

If we don't do anything for this, the linker fails with a mysterious
error message if input files are generated by gas. It is unfortunate
but I think we need to support it.

Differential Revision: https://reviews.llvm.org/D34064

llvm-svn: 305218
2017-06-12 18:46:33 +00:00
Rafael Espindola f846ce259a Fix weak symbols on arm and aarch64.
Given

.weak target
 .global _start
_start:
 b target

The intention is that the branch goes to the instruction after the
branch, effectively turning it on a nop.  The branch adds the runtime
PC, but we were adding it statically too.

I noticed the oddity by inspection, but llvm-objdump seems to agree,
since it now prints things like:

b       #-4 <_start+0x4>

llvm-svn: 305212
2017-06-12 18:05:01 +00:00
Peter Collingbourne bfd5113ca1 ELF: Teach ICF about relocations referring to merge input sections.
Relocations referring to merge sections are considered equal if they
resolve to the same offset in the same output section.

Differential Revision: https://reviews.llvm.org/D34094

llvm-svn: 305177
2017-06-12 00:05:54 +00:00
Rafael Espindola 07c8741644 Relax the overflow checking of R_386_PC16.
Currently the freebsd early boot code fails to link. The issue reduces
to 16 bit code at position 0x7000 wanting to jump to position
0x9000. That is represented in the .o file as a relocation with no
symbol and an addend of 0x9000 - 2 (The -2 is because i386 uses the ip
after the current instruction for jumps).

If the addend is interpreted as signed (it should), it is -28674. In a
32 bit architecture, that is the address 0xffff8ffe. To get there from
0x7000 we have to add 4294909950 (too big) or subtract 57346 (too
small). We then produce an error.

What lld is missing is the fact that at runtime this will actually be
a 16 bit architecture and adding 0x1ffe produces 0x8ffe which is the
correct result in 16 bits (-28674).

Since we have a 16 bit addend and a 16 bit PC, the relocation can move
the PC to any 16 bit address and that is the only thing we really need
to check: if the address we are pointing to fits in 16 bits. This is
unfortunately hard to do since we have to delay subtracting the PC and
if we want to do that outside of Target.cpp, we have to move the
overflow check out too.  An incomplete patch that tries to do that is
at https://reviews.llvm.org/D34070

This patch instead just relaxes the check. Since the value we have is
the destination minus the PC and the PC is 16 bits, it should fit in
17 bits if the destination fits in 16 too.

bfd had a similar issue for some time and got a similar fix:
https://sourceware.org/ml/binutils/2005-08/msg00001.html

llvm-svn: 305135
2017-06-10 01:56:58 +00:00
Rui Ueyama 1c837b5fb1 [ICF] Ignore SHF_GROUP flag when comparing two sections.
SHF_GROUP bit doesn't make sense in executables or DSOs, so linkers are
expected to remove that bit from section flags. We did that when we create
output sections.

This patch is to do that earlier than before. Now the flag is dropped when
we instantiate input section objects.

This change improves ICF. Previously, two sections that differ only in
SHF_GROUP flag were not merged, because when the control reached ICF,
the flag was still there. Now the flag is dropped before reaching to ICF,
so the difference is ignored naturally.

This issue was found by pcc.

Differential Revision: https://reviews.llvm.org/D34074

llvm-svn: 305134
2017-06-10 00:38:55 +00:00
Zachary Turner bd336e44d8 Rename llvm-pdbdump -> llvm-pdbutil.
This is to reflect the evolving nature of the tool as being
useful for more than just dumping PDBs, as it can do many other
things.

Differential Revision: https://reviews.llvm.org/D34062

llvm-svn: 305106
2017-06-09 20:46:17 +00:00
Rafael Espindola d2a1b944bc Don't check the raw bits in tests.
It is not needed since we have the disassemble.

llvm-svn: 305096
2017-06-09 18:07:34 +00:00
George Rimar adaba7aeaa [ELF] - Simplify gdb-index-empty.s
Just simplifies existent test.

Differential revision: https://reviews.llvm.org/D33986

llvm-svn: 305073
2017-06-09 13:30:36 +00:00
George Rimar af845f26e6 [ELF] - Fix build bot.
llvm-svn: 305071
2017-06-09 12:58:13 +00:00
George Rimar 1840901a2f [ELF] - Allow producing -r output if only empty archive is given.
This is used by linux kernel build system.

(https://www.kernel.org/doc/Documentation/kbuild/makefiles.txt "3.2 Built-in object goals")

It has for example next configuration for linking built-in.o files:
drivers-y	:= $(patsubst %/, %/built-in.o, $(drivers-y))
drivers-$(CONFIG_PCI)		+= arch/ia64/pci/
...
drivers-$(CONFIG_OPROFILE)	+= arch/ia64/oprofile/

Im most simple case all CONFIG_* options are off. That means linker is called with empty input archive, 
emulation option and no inputs and expected to generate some relocatable output. 
ld.bfd is able to do that, we dont.

Patch allows to support this case.

Differential revision: https://reviews.llvm.org/D33937

llvm-svn: 305069
2017-06-09 12:26:57 +00:00
Peter Smith 28285576cb [ELF] Be more precise about Thumb state bit in ARM thunks
The symbols generated for Thunks have type STT_FUNC, to permit a thunk to
be reused via a blx instruction the Thumb bit (0) needs to be set properly.

Differential Revision: https://reviews.llvm.org/D34036

llvm-svn: 305065
2017-06-09 09:51:51 +00:00
Rafael Espindola 6b901217dc Fix filename.
It was passing locally because of a leftover file.

llvm-svn: 305042
2017-06-09 00:56:09 +00:00
Rafael Espindola 85c607ea56 Add a test for R_386_PC16 range checking.
llvm-svn: 305040
2017-06-09 00:48:55 +00:00
Rui Ueyama 3271d3704a Fix a bug in output section directive.
Previously, it couldn't parse

  SECTIONS .text (0x1000) : { *(.text) }

because "(" was interpreted as the begining of the "(NOLOAD)" directive.

llvm-svn: 305006
2017-06-08 19:47:16 +00:00
George Rimar fbb0463f39 [ELF] - Linkerscript: implement NOLOAD section type.
This is PR32351

Each output section may have a type. The type is a keyword in parentheses.
(https://sourceware.org/binutils/docs/ld/Output-Section-Type.html#Output-Section-Type)
This patch support only one type, it is NOLOAD.
If output section has such type, we force it to be SHT_NOBITS. 

More details are available on a review page.

Differential revision: https://reviews.llvm.org/D33647

llvm-svn: 304925
2017-06-07 16:31:08 +00:00
Peter Smith 8e791463ef [ELF] Convert Thunks to use InputSectionDescriptions
Thunks are now generated per InputSectionDescription instead of per
OutputSection. This allows created ThunkSections to be inserted directly
into InputSectionDescription.

Changes in this patch:
- Loop over InputSectionDescriptions to find relocations to Thunks
- Generate a ThunkSection per InputSectionDescription
- Remove synchronize() as we no longer need it
- Move fabricateDefaultCommands() before createThunks

Differential Revision: https://reviews.llvm.org/D33835

llvm-svn: 304887
2017-06-07 09:35:14 +00:00
George Rimar 990c9cb2bf [ELF] - Do not merge relocation sections by name when using --emit-relocs.
Previously we would merge relocation sections by name.
That did not work in some cases, like testcase shows.

Patch implements logic to merge relocation sections if their target
sections were merged into the same output section.

Differential revision: https://reviews.llvm.org/D33824

llvm-svn: 304886
2017-06-07 09:20:35 +00:00
George Rimar 41c7ab4a3d [ELF] - Linkerscript: improved error reporting.
When linking linux kernel LLD currently reports next errors:

ld: error: unable to evaluate expression: input section .head.text has no output section assigned
ld: error: At least one side of the expression must be absolute
ld: error: At least one side of the expression must be absolute

That does not provide file/line information and overall looks unclear. 
Patch adds location information to ExprValue and that allows
to provide more clear error messages.

Differential revision: https://reviews.llvm.org/D33943

llvm-svn: 304881
2017-06-07 08:54:43 +00:00
Rafael Espindola 0e454a9837 Define __executable_start.
This is defined by both bfd and gold and used by the android libc.

llvm-svn: 304803
2017-06-06 16:18:48 +00:00
George Rimar 8cde9a7e77 [ELF] - Allow multiple comdats when producing relocatable output.
Previously LLD would fail for case when there are multiple comdats and -r.

That happened because it merged all ".group" (SHT_GROUP) sections into single
output, producing broken result. Such sections may have similar name, alignment and flags
and other properties. We need to produce separate output section for each such input one.

Differential revision: https://reviews.llvm.org/D33643

llvm-svn: 304769
2017-06-06 06:38:32 +00:00
Konstantin Zhuravlyov 4c882d90aa AMDGPU/LLD: Remove deprecated and unused elf definitions from tests
Differential Revision: https://reviews.llvm.org/D33691

llvm-svn: 304734
2017-06-05 20:57:17 +00:00
Petr Hosek 7953e62f2e [ELF] Define __dso_handle symbol if needed
Traditionally, it has been defined in crtbegin.o, which is typically
provided by libgcc or as part of the C library on some systems. However,
but there's no principled reason for it to be there. We optionaly
define this symbol, which can be used on platforms that don't provide
__dso_handle in crtbegin.o or which don't use crtbegin.o at all.

Differential Revision: https://reviews.llvm.org/D33856

llvm-svn: 304732
2017-06-05 20:42:34 +00:00
Dmitry Mikulin db3b87b2c0 Symbols re-defined with -wrap and -defsym need to be excluded from inter-
procedural optimizations to prevent dropping symbols and allow the linker
to process re-directs.

PR33145: --wrap doesn't work with lto.
Differential Revision: https://reviews.llvm.org/D33621

llvm-svn: 304719
2017-06-05 16:24:25 +00:00
George Rimar 60a0ea161e [ELF] - Make LLD remove gnu-lib compression prefix (".z") after decompression when using -r
This is PR33289.

Previously LLD leaved section naming as is and that lead to wrong result,
because we decompress sections when using -r,
and hence should remove ".z" prefix.

Differential revision: https://reviews.llvm.org/D33885

llvm-svn: 304711
2017-06-05 12:49:21 +00:00
Reid Kleckner c2eaccb0af Revert "Merge IAT and ILT."
This reverts r303374. It breaks Chrome's IAT patching code:
http://crbug.com/729077

llvm-svn: 304584
2017-06-02 18:49:38 +00:00
Reid Kleckner 5a1ed789e1 Add test case for LLD/COFF for stdcall exports in .def files
The .def file parser changes I reverted broke this test case, and
exported "__imp__foo" instead of "__imp__foo@8". This was
http://crbug.com/728726.

llvm-svn: 304572
2017-06-02 17:48:06 +00:00
George Rimar 6a0644b24b [ELF] - Following commit for r304531 to fix BB.
Not sure why I did not faced that locally earlier, 
llvm-readobj did not have -s flag for 2/3 tests.

llvm-svn: 304539
2017-06-02 10:38:45 +00:00
George Rimar ef84219d21 [ELF] - Assign SHF_INFO_LINK flag to REL[A] sections.
Spec says: (http://www.sco.com/developers/gabi/latest/ch4.sheader.html)

sh_info
This member holds extra information, whose interpretation depends on the section type.
If the sh_flags field for this section header includes the attribute SHF_INFO_LINK, 
then this member represents a section header table index.

SHF_INFO_LINK
The sh_info field of this section header holds a section header table index.

Since sh_info for SHT_REL[A] sections should contain the section header index of the
section to which the relocation applies, this is
consistent with spec to put this flag. Behavior matches both bfd and gold as well.

Differential revision: https://reviews.llvm.org/D33763

llvm-svn: 304531
2017-06-02 09:17:45 +00:00
George Rimar d602987271 [ELF] - Properly handle R_386_GOTPC relocation.
This is PR33243. R_GOTONLY_PC_FROM_END was not in a list of link time constant
expressions and that was a result of confusiing messages like PR shows:

/usr/bin/ld.lld: error: /usr/lib/go/src/runtime/alg.go:47: 
can't create dynamic relocation R_386_GOTPC against local symbol in readonly segment defined in /tmp/nice/go-link-597453838/go.o

Though in reality we just should not have try to create a dynamic relocation for this case at all.

Patch fixes the issue.

Differential revision: https://reviews.llvm.org/D33717

llvm-svn: 304393
2017-06-01 07:53:58 +00:00
George Rimar b940f65cd2 [ELF] - Do not crash when proccessing absolute symbols during GC.
This is PR33239, previously we segfault on given testcase.

Differential revision: https://reviews.llvm.org/D33713

llvm-svn: 304392
2017-06-01 07:47:56 +00:00
Rafael Espindola b47c6e5cbd Fix a crash.
We would crash if a SHF_LINK_ORDER section pointed to a non
InputSection section. Since those sections are not merged in order,
SHF_LINK_ORDER is pretty meaningless and we can error on that case.

llvm-svn: 304327
2017-05-31 19:09:52 +00:00
Rafael Espindola 180de970c8 Fix crash when processing relocations in .eh_frame.
This happens when attempting to link shared libraries using exceptions on
MIPS. It requires -z notext because clang generates R_MIPS_64 relocations
inside .eh_frame.
The crash happened because for EhInputSection the OutSec member is null.

Patch by Alexander Richardson!

llvm-svn: 304260
2017-05-31 00:23:23 +00:00
George Rimar d4096140e3 [ELF] - Do not crash when linkerscript applies fill to .bss.
I found that during visual inspection of code while wrote different patch.
Script in testcase probably have nothing common with real life, but
we segfault currently using it.

If output section is known NOBITS, there is no need to create
writers threads for doing nothing or proccess any filler logic that 
is useless here. We can just early return, that is what this patch do.

DIfferential revision: https://reviews.llvm.org/D33646

llvm-svn: 304192
2017-05-30 05:48:09 +00:00
Petr Hosek 08dfd53269 [ELF] Filter out non InputSection members from InputSections
InputSections may contain MergeInputSection members which trigger
a segmentation fault when trying to cast them to InputSection.

Differential Revision: https://reviews.llvm.org/D33628

llvm-svn: 304189
2017-05-30 05:17:58 +00:00
Petr Hosek 3c6de1a66c [ELF] Use late evaluation for ALIGN in expression
While the following expression is handled fine:

  PROVIDE_HIDDEN(newsym = oldsym + address);

The following expression triggers an error because the expression
is evaluated as absolute:

  PROVIDE_HIDDEN(newsym = ALIGN(oldsym, CONSTANT(MAXPAGESIZE)) + address);

To avoid this error, we use late evaluation for ALIGN by making the
alignment an attribute of the expression itself.

Differential Revision: https://reviews.llvm.org/D33629

llvm-svn: 304185
2017-05-30 03:18:28 +00:00
George Rimar 3b189d1643 [ELF] - Do not allow -r to eat comdats.
This is PR33052, "Bug 33052 - -r eats comdats ".

To fix it I stop removing group section from out when -r is given
and fixing SHT_GROUP content when writing it just like we do some
other fixup, e.g. for Rel[a]. (it needs fix for section indices that
are in group).

Differential revision: https://reviews.llvm.org/D33485

llvm-svn: 304140
2017-05-29 08:37:50 +00:00
Petr Hosek ffa786f465 [lld][ELF]Add option to make .dynamic read only
The .dynamic section of an ELF almost doesn't need to be written to with
the exception of the DT_DEBUG entry. For several reasons having a read
only .dynamic section would be useful. This change adds the -z keyword
"rodynamic" which forces .dynamic to be read-only. In this case DT_DEBUG
will not be emited.

Patch by Jake Ehrlich

Differential Revision: https://reviews.llvm.org/D33251

llvm-svn: 304024
2017-05-26 19:12:38 +00:00
Rafael Espindola d23e9267a6 Order writable executable sections before writable ones.
On SPARC, .plt is both writeable and executable. The current way
sections are sorted means that lld puts it after .data/.bss. but it
really needs to be close to .test to make sure branches into .plt
don't overflow. I'd argue that because .bss is supposed to come last
on all architectures, we should change the default sort order such
that writable and executable sections come before sections that are
just writeable. read-only executable sections should still come after
sections that are just read-only of course. This diff makes this
change.

llvm-svn: 304008
2017-05-26 17:23:25 +00:00
George Rimar c1a0364cd6 [ELF] - Do not produce duplicate values in .gdb_index's constant pool area.
I found this when builded llc binary using gcc 5.4.1 + LLD.
gcc produces duplicate entries in .debug_gnu_pubtypes section, ex:

UnifyFunctionExitNodes.cpp.o has:
0x0000ac07 EXTERNAL TYPE "std::success_type<void*>"
0x0000ac07 EXTERNAL TYPE "std::success_type<void*>"

clang produces single entry here:
0x0000d291 EXTERNAL TYPE "std::__success_type<void *>"

If we link output from gcc with LLD, that would produce excessive duplicate
entries in .gdb_index constant pool area. That does not seem affect gdb work,
but makes .gdb_index larger than it can be.

I also checked that gold filters out such duplicates too. Patch fixes it.

Differential revision: https://reviews.llvm.org/D32647

llvm-svn: 303975
2017-05-26 12:09:26 +00:00
Rui Ueyama 5012858e26 Accept not only --reproduce <foo> but also --reproduce=<foo>.
llvm-svn: 303905
2017-05-25 19:49:54 +00:00
Rui Ueyama 69ae29b1d1 Do not allow delay-importing data symbols.
If you pass /delayload:<dllname> to the COFF linker, it creates thunks
so that DLLs are loaded when they are used for the first time instead of
load-time.

This mechanism do not work for data symbols as there's no way to trap
acccesses to data imported from DLLs. (Technically, I think if we do not
initially map dllimport tables in memory, we could actually trap accesses
and delay-load data symbols, but that's not what Windows do.)

This patch is to report an error when you try to delay-load data symbols.

Fixes https://bugs.llvm.org/show_bug.cgi?id=33106

Differential Revision: https://reviews.llvm.org/D33557

llvm-svn: 303890
2017-05-25 18:03:34 +00:00
Rui Ueyama 9aa82f76ac Garbage collect dllimported symbols.
This is a different implementation than r303225 (which was reverted
in r303270, re-submitted in r303304 and then re-reverted in r303527).

In the previous patch, I tried to add Live bit to each dllimported
symbol. It turned out that it didn't work with "oldnames.lib" which
contains a lot of weak aliases to dllimported symbols.

The way we handle weak aliases is to check if undefined symbols
can be resolved using weak aliases, and if so, memcpy the Defined
symbols to weak Undefined symbols, so that any references to weak
aliases automatically see defined symbols instead of undefined ones.

This memcpy happens before MarkLive kicks in.

That means we may have multiple copies of dllimported symbols. So
turning on one instance's Live bit is not enough.

This patch moves the Live bit to dllimport file. Since multiple
copies of dllsymbols still point to the same file, we can use it as the
central repository to keep track of liveness.

Differential Revision: https://reviews.llvm.org/D33520

llvm-svn: 303814
2017-05-24 22:30:06 +00:00
Dmitry Mikulin fd0c844fbb Do not track section types of previous sections, always use PROGBITS for dummy sections.
Fix for PR33029.

llvm-svn: 303770
2017-05-24 16:48:31 +00:00
Peter Collingbourne 968fe93803 ELF: The later of --build-id and --build-id= wins.
If the compiler driver passes --build-id and the user uses -Wl to
pass --build-id= then the user's flag should take precedence.

Differential Revision: https://reviews.llvm.org/D33461

llvm-svn: 303689
2017-05-23 21:16:48 +00:00
Evgeniy Stepanov f12ac5b776 [lld] Infer relocation model from module flags in relocatable LTO link.
Fix for PR33096.

llvm-svn: 303579
2017-05-22 21:11:44 +00:00
Rui Ueyama b6632d9cd1 Revert r303304: Re-submit r303225: Garbage collect dllimported symbols.
This reverts commit r303304 because it looks like the change
introduced a crash bug. At least after that change, LLD with thinlto
crashes when linking Chromium.

llvm-svn: 303527
2017-05-22 06:01:37 +00:00
Rui Ueyama a674943211 Set IMAGE_DLL_CHARACTERISTICS_NO_BIND.
Our output is not compatible with the Binding feature, so make it
explicit that.

Differential Revision: https://reviews.llvm.org/D33336

llvm-svn: 303378
2017-05-18 20:26:58 +00:00
Rui Ueyama 9fbfd76fe3 Merge IAT and ILT.
Previously, LLD-produced executables had IAT (Import Address Table) and
ILT (Import Lookup Table) as separate chunks of data, although their
contents are identical. My interpretation of the COFF spec when I wrote
the COFF linker is that they need to be separate tables even though they
are the same.

But Peter found that the Windows loader is fine with executables in
which IAT and ILT are merged. This is a patch to merge IAT and ILT.
I confirmed that an lld-link self-hosted with this patch works fine.

Fixes https://bugs.llvm.org/show_bug.cgi?id=33064

Differential Revision: https://reviews.llvm.org/D33326

llvm-svn: 303374
2017-05-18 19:59:22 +00:00
Zachary Turner 8a7508970a [COFF] Fix interaction between /DEBUG and /PDB
When /DEBUG is not specified, /PDB should be ignored.  When
/DEBUG is specified, a PDB should be output regardless of
whether or not /PDB is specified.  /PDB just overrides the
default name.

This patch implements this behavior, and adds some tests, while
also removing a dead option /DEBUGPDB which was unused in any
code.

Differential Revision: https://reviews.llvm.org/D33302

llvm-svn: 303352
2017-05-18 15:15:10 +00:00
Peter Smith d54f368e6a [ELF] Support R_ARM_SBREL32 Relocation
This change adds support for the R_ARM_SBREL32 relocation. The relocation
is a base relative relocation that is produced by clang/llvm when -frwpi
is used. The use case for the -frwpi option is position independent data
for embedded systems that do not have a GOT. With -frwpi all data is
accessed via an offset from a base register (usually r9), where r9 is set
at run time to where the data has been loaded. The base of the data is
known as the static base.

The ARM ABI defines the static base as:
B(S) is the addressing origin of the output segment defining the symbol S.
The origin is not required to be the base address of the segment. For
simplicity we choose to use the base address of the segment.

The ARM procedure call standard only defines a read write variant using
R_ARM_SBREL32 relocations. The read-only data is accessed via pc-relative
offsets from the code, this is implemented in clang as -fropi.

Fixes PR32924

Differential Revision: https://reviews.llvm.org/D33280

llvm-svn: 303337
2017-05-18 09:12:21 +00:00
Rui Ueyama cd41bc8dec Re-submit r303225: Garbage collect dllimported symbols.
This reverts re-submits r303225 which was reverted in r303270 because it
broke the sanitizer-windows bot.

The reason of the failure is that we were writing dead symbols to the
symbol table. I fixed the issue.

llvm-svn: 303304
2017-05-17 21:36:08 +00:00
Hans Wennborg e67c5f6b52 Revert r303225 "Garbage collect dllimported symbols."
and follow-up r303226 "Fix Windows buildbots."

This broke the sanitizer-windows buildbot.

> Previously, the garbage collector (enabled by default or by explicitly
> passing /opt:ref) did not kill dllimported symbols. As a result,
> dllimported symbols could be added to resulting executables' dllimport
> list even if no one was actually using them.
>
> This patch implements dllexported symbol garbage collection. Just like
> COMDAT sections, dllimported symbols now have Live bits to manage their
> liveness, and MarkLive marks reachable dllimported symbols.
>
> Fixes https://bugs.llvm.org/show_bug.cgi?id=32950
>
> Reviewers: pcc
>
> Subscribers: llvm-commits
>
> Differential Revision: https://reviews.llvm.org/D33264

llvm-svn: 303270
2017-05-17 16:22:03 +00:00
Rui Ueyama de83fec029 Fix Windows buildbots.
llvm-svn: 303226
2017-05-17 01:05:56 +00:00
Rui Ueyama 02df7a6cf1 Garbage collect dllimported symbols.
Summary:
Previously, the garbage collector (enabled by default or by explicitly
passing /opt:ref) did not kill dllimported symbols. As a result,
dllimported symbols could be added to resulting executables' dllimport
list even if no one was actually using them.

This patch implements dllexported symbol garbage collection. Just like
COMDAT sections, dllimported symbols now have Live bits to manage their
liveness, and MarkLive marks reachable dllimported symbols.

Fixes https://bugs.llvm.org/show_bug.cgi?id=32950

Reviewers: pcc

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D33264

llvm-svn: 303225
2017-05-17 00:35:50 +00:00
Rui Ueyama 35e828775f Merge a test YAML file and a test file.
llvm-svn: 303135
2017-05-16 01:16:20 +00:00
Rafael Espindola d51400b60a Disable threads in a few tests.
They are too slow otherwise. We track the issue in pr32942.

llvm-svn: 303097
2017-05-15 18:29:14 +00:00
Peter Collingbourne 0a2678e9aa ELF: --gdb-index: Change findSection to return an InputSection.
We should only ever expect this function to return a regular
InputSection; I would not expect a function definition to be in a
MergeInputSection or EhInputSection. We were previously crashing
in writeTo if this function returned a section that was not an
InputSection because we do not set OutSec for such sections.

This can happen in practice if a function is defined in an empty
section which shares its offset-in-file with a MergeInputSection,
as in the provided test case.

A better fix for this bug would be to fix the
DWARFUnit::collectAddressRanges() interface to provide section
information (see D33183), but this at least fixes the crash.

Differential Revision: https://reviews.llvm.org/D33176

llvm-svn: 303089
2017-05-15 17:59:21 +00:00
Peter Collingbourne c5bee3212b ELF: --gdb-index: Do not add dead sections to the address area.
Fixes PR33032.

Differential Revision: https://reviews.llvm.org/D33175

llvm-svn: 303088
2017-05-15 17:53:26 +00:00
Rafael Espindola 5210141b07 Optimize orphan placement in a general way.
We used to place orphans by just using compareSectionsNonScript.

Then we noticed that since linker scripts can use another order, we
should first try match the section to a given PT_LOAD. But there is
nothing special about PT_LOAD. The same issue can show up for
PT_GNU_RELRO for example.

In general, we have to search for the most similar section and put the
orphan next to it. Most similar being defined as how long they follow
the same code path in compareSecitonsNonScript.

That is what this patch does. We now compute a rank for each output
section, with a bit for each branch in what was
compareSectionsNonScript.

With this findOrphanPos is now fully general and orphan placement can
be optimized by placing every section with the same rank at once.

The included testcase is a variation of many-sections.s that uses
allocatable sections to avoid the fast path in the existing
code. Without threads it goes form 46 seconds to 0.9 seconds.

llvm-svn: 302903
2017-05-12 14:52:22 +00:00
George Rimar b4081bb0ab [ELF] - Stop support of DF_STATIC_TLS flag.
This reverts changes introduced in r302414 "[ELF] - Set DF_STATIC_TLS flag for i386 target."

Because DF_STATIC_TLS does not look to be used by glibc or anything else.

llvm-svn: 302884
2017-05-12 08:04:58 +00:00
George Rimar 6f586731c5 [ELF] - Don't allow R_X86_64_TPOFF32 dynamic relocation when linking PIC
Both gold and bfd restrict that one:

ld.bfd: test.o: relocation R_X86_64_TPOFF32 against `var' can not be 
used when making a shared object; recompile with -fPIC
ld.gold: error: test.o: unsupported reloc 23 against global symbol var

What looks reasonable because it is 32 bit one. Patch do the same.

Differential revision: https://reviews.llvm.org/D33100

llvm-svn: 302881
2017-05-12 07:19:11 +00:00
George Rimar f2cd0f9d05 [ELF] - Make text section location explicit in early-assign-symbol.s test.
Testcase itself depends on .text section location, which was orphan earlier.

Suggested by Rafael Espíndola

llvm-svn: 302792
2017-05-11 11:53:49 +00:00
Rafael Espindola d7dc225888 Use a DenseMap in LinkerScript::getCmd.
This improves many-sections.s with a linker script from 22s to 0.9s.

llvm-svn: 302708
2017-05-10 19:13:38 +00:00
Rui Ueyama 896cbc40ad [ELF] Improve error message for incompatible section types
Previously we were not printing out the type of the incompatible section
which made it difficult to determine what the problem was.

The error message format has been change to the following:

  error: section type mismatch for .shstrtab
  >>> <internal>:(.shstrtab): SHT_STRTAB
  >>> output section .shstrtab: Unknown

Patch by Alexander Richardson.

Differential Revision: https://reviews.llvm.org/D32488

llvm-svn: 302694
2017-05-10 16:57:50 +00:00
Petr Hosek 6b936bf6c7 [ELF] Define __ehdr_start unconditionally even when using linker script
This behavior differs from the semantics implemented by GNU linkers
which only define this symbol iff ELF headers are in the memory
mapped segment.

Differential Revision: https://reviews.llvm.org/D33019

llvm-svn: 302687
2017-05-10 16:20:33 +00:00
George Rimar 608cf67084 [ELF] - Don't segfault when assigning non-calculatable absolute symbol value.
This is PR32664.

Issue was revealed by linux kernel script which was:

SECTIONS {
 . = (0xffffffff80000000 + ALIGN(0x1000000, 0x200000));
 phys_startup_64 = ABSOLUTE(startup_64 - 0xffffffff80000000);

 .text : AT(ADDR(.text) - 0xffffffff80000000) {
.....
  *(.head.text)
Where startup_64 is in .head.text.

At the place of assignment to phys_startup_64 we can not calculate absolute value for startup_64
because .text section has no VA assigned. Two patches were prepared earlier to address this: D32173 and D32174.

And in comments for D32173 was suggested not try to support this case, but error out.

Differential revision: https://reviews.llvm.org/D32793

llvm-svn: 302668
2017-05-10 14:23:33 +00:00
Rui Ueyama 91b95b61f8 Add memory ORIGIN and LENGTH expression support
Adds support for the ORIGIN and LENGTH linker script built in functions.

  ORIGIN(memory) Return the origin of the memory region
  LENGTH(memory) Return the length of the memory region

Redo of D29775 for refactored linker script parsing.

Patch by Robert Clarke

Differential Revision: https://reviews.llvm.org/D32934

llvm-svn: 302564
2017-05-09 18:24:38 +00:00
George Rimar 1564122b05 [ELF] - Set DF_STATIC_TLS flag for i386 target.
This is PR32437.

DF_STATIC_TLS
If set in a shared object or executable, this flag instructs the 
dynamic linker to reject attempts to load this file dynamically. 
It indicates that the shared object or executable contains code 
using a static thread-local storage scheme. Implementations need 
not support any form of thread-local storage.

Patch checks if IE/LE relocations were used to check if code uses
static model. If so it sets the DF_STATIC_TLS flag.

Differential revision: https://reviews.llvm.org/D32354

llvm-svn: 302414
2017-05-08 10:24:38 +00:00
George Rimar d86a4e505b [ELF] - Linkerscript: support combination of linkerscript and --compress-debug-sections.
Previously it was impossible to use linkerscript with --compress-debug-sections 
because of assert failture:
Assertion failed: isFinalized(), file C:\llvm\lib\MC\StringTableBuilder.cpp, line 64

Patch fixes the issue

llvm-svn: 302413
2017-05-08 10:18:12 +00:00
Rafael Espindola 0f6cc65fe9 Remember OffsetInArchive in LazyObjectFile.
LazyObjectFile might turn out to be a BitcodeFile, so we need
OffsetInArchive.

llvm-svn: 302242
2017-05-05 15:17:07 +00:00
Rafael Espindola 0b1413a881 Use the archive offset with --whole-archive.
The test ELF/lto/thin-archivecollision.ll was not testing what it
wanted to test. It needs two archive members with the same name, but
different offsets.

Without this we could remove all references of OffsetInArchive and all
tests would still pass.

Fixing the test showed that the --whole-archive case was broken, which
this patch fixes.

llvm-svn: 302241
2017-05-05 15:08:06 +00:00
Rafael Espindola 80ae8ae186 Remember archive name when creating LazyObjectFile.
It is needed for creating an unique identifier for ThinLTO.

This fixes pr32931.

llvm-svn: 302235
2017-05-05 13:55:51 +00:00
George Rimar bb451903f5 [ELF] - Accept --defsym foo=value form.
Previously we accepted --defsym=foo=value only.

Reported by Sean Silva.

llvm-svn: 302227
2017-05-05 11:59:15 +00:00
Rafael Espindola 7c4eafa3ee Fix accounting of tbss.
We were correctly computing the size contribution of a .tbss input
section (it is none), but we were incorrectly considering the
alignment of the output section: it was advancing Dot instead of
ThreadBssOffset.

As far as I can tell this was always wrong in our linkerscript
implementation, but that became more visible now that the code is
shared with the non linker script case.

llvm-svn: 302107
2017-05-04 03:00:27 +00:00
Rui Ueyama fd7deda57a Accept archive files with no symbol table instad of warning on them.
It seems virtually everyone who tries to do LTO build with Clang and
LLD was hit by a mistake to forget using llvm-ar command to create
archive files. I wasn't an exception. Since this is an annoying common
issue, it is probably better to handle that gracefully rather than
reporting an error and tell the user to redo build with different
configuration.

Differential Revision: https://reviews.llvm.org/D32721

llvm-svn: 302083
2017-05-03 21:03:08 +00:00
Rafael Espindola 5e20c75e3a Handle mixed strong and weak undefined symbols.
We were ignoring strong undefined symbols if they followed weak ones.

Fixes pr32899.

llvm-svn: 302065
2017-05-03 18:40:27 +00:00
Peter Smith c60b4510ea [ELF] Fix problems with fabricateDefaultCommands() and --section-start
The --section-start <name>=<address> needs to be translated into equivalent
linker script commands. There are a couple of problems with the existing
implementation:
- The --section-start with the lowest address is assumed to be at the start
of the map. This assumption is incorrect, we have to iterate through the
SectionStartMap to find the lowest address.
- The addresses in --section-start were being over-aligned when the
sections were marked as PageAlign. This is inconsistent with the use of
SectionStartMap in fixHeaders(), and can cause problems when the PageAlign
causes an "unable to move location counter backward" error when the
--section-start with PageAlign is aligned to an address higher than the next
--section-start. The ld.bfd and ld.gold seem to be more consistent with this
approach but this is not a well specified area.
    
This change fixes the problems above and also corrects a typo in which
fabricateDefaultCommands() is called with the wrong parameter, it should be
called with AllocateHeader not Config->MaxPageSize.

Differential Revision: https://reviews.llvm.org/D32749

llvm-svn: 302007
2017-05-03 08:44:50 +00:00
George Rimar 99e1890e71 [ELF] - Added testcase gdb-index-ranges.s (https://reviews.llvm.org/D32750)
Before rL301170 was landed, LLD did not produce correct entries in .gdb_index address area.
Issue was fixed on LLVM DWARF parsers side and was relative to how .debug_ranges
section was scanned. It was main problem of PR32319.

It makes sense to have testcase on LLD size too. This checks that we generate proper values 
now, because we do not have any tests for .gdb_index which works with .debug_ranges atm.

Differential revision: https://reviews.llvm.org/D32750

llvm-svn: 302006
2017-05-03 08:21:42 +00:00
Rafael Espindola a8a1a4fc30 Avoid empty .eh_frame sections.
Strip on OpenBSD does not correctly handle an empty .eh_frame section
and produces broken binaries in that case. Currently lld creates such
an empty .eh_frame section, despite the fact that the OpenBSD crtend.o
explicitly inserts a terminator. The Linux LSB "standard":

https://refspecs.linuxfoundation.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html#EHFRAME

explicitly says that

    The .eh_frame section shall contain 1 or more Call Frame Information (CFI) records.

This diff includes a test that specifically tests the issue I'm seeing
on OpenBSD.

Patch by Mark Kettenis!

llvm-svn: 301931
2017-05-02 15:45:31 +00:00
Rafael Espindola 4aa2ef5b0e Fix pr32816.
When using linkerscripts we were trying to sort SHF_LINK_ORDER
sections too early. Instead of always doing two runs of
assignAddresses, record the section order in processCommands.

llvm-svn: 301830
2017-05-01 20:32:39 +00:00
Rui Ueyama cfe4b5ca15 Change an unusual directory permission back to a sane value after test.
Leaving a directory with a permission 0o100 is inconvenient.
For example, macOS's rm can't remove such directory.

llvm-svn: 301821
2017-05-01 18:38:37 +00:00
Rui Ueyama 3969acf8a4 Port recent changes to ELF/MapFile.cpp to COFF.
llvm-svn: 301754
2017-04-29 18:50:39 +00:00
Davide Italiano d765638ad1 [ELF] When a rel points to a SHF_MERGE section, don't merge the sec.
Fixes PR32785. Thanks to Piotr Padlewski for the report and
Rafael for the fix.

llvm-svn: 301729
2017-04-29 01:24:34 +00:00
Rui Ueyama e2f11690fe Eliminate .debug_gnu_pub{names,types} if -gdb-index is given.
This patch is to ignore .debug_gnu_pub{names,types} sections if the
-gdb-index option was given.

Differential Revision: https://reviews.llvm.org/D32662

llvm-svn: 301710
2017-04-28 22:46:55 +00:00
Rui Ueyama 3012b371fd Change the format of the map file.
Previously, we printed out input sections and input files in
separate columns as shown below.

  Address          Size             Align Out     In      File    Symbol
  0000000000201000 0000000000000015     4 .text
  0000000000201000 000000000000000e     4         .text
  0000000000201000 000000000000000e     4                 foo.o
  0000000000201000 0000000000000000     0                         _start
  0000000000201005 0000000000000000     0                         f(int)
  000000000020100e 0000000000000000     0                         local
  0000000000201010 0000000000000002     4                 bar.o
  0000000000201010 0000000000000000     0                         foo
  0000000000201011 0000000000000000     0                         bar

This format doesn't make much sense because for each input section,
there's always exactly one input file. This patch changes the format
to this.

  Address          Size             Align Out     In      Symbol
  0000000000201000 0000000000000015     4 .text
  0000000000201000 000000000000000e     4         foo.o:(.text)
  0000000000201000 0000000000000000     0                 _start
  0000000000201005 0000000000000000     0                 f(int)
  000000000020100e 0000000000000000     0                 local
  0000000000201010 0000000000000002     4         bar.o:(.text)
  0000000000201010 0000000000000000     0                 foo
  0000000000201011 0000000000000000     0                 bar

Differential Revision: https://reviews.llvm.org/D32657

llvm-svn: 301683
2017-04-28 20:38:27 +00:00
Rui Ueyama 8a3ef95fc9 Use toString(Inputfile*) to format a file object.
llvm-svn: 301674
2017-04-28 20:00:09 +00:00
Rui Ueyama b882e5910d Speed up the -Map option.
We found that some part of code for the -Map option takes O(m*n)
where m is the number of input sections in some file and n is
the number of symbols in the same file. If you do LTO, we usually
have only a few object files as inputs for the -Map option
feature, so this performance characteristic was worse than I
expected.

This patch rewrites the -Map option feature to speed it up.
I eliminated the O(m*n) bottleneck and also used multi-threading.

As a result, clang link time with the -Map option improved from
18.7 seconds to 11.2 seconds. Without -Map, it takes 7.7 seconds,
so the -Map option is now about 3x faster than before for this
test case (from 11.0 seconds to 3.5 seconds.) The generated output
file size was 223 MiB, and the file contains 1.2M lines.

Differential Revision: https://reviews.llvm.org/D32631

llvm-svn: 301659
2017-04-28 17:19:13 +00:00
Saleem Abdulrasool 5c98b74536 COFF: actually synthesize CONST imports properly
CONSTANT imports expect both the `_imp_` prefixed and non-prefixed
symbols should be added to the symbol table.  This allows for linking
symbols like _NSConcreteGlobalBlock in WinObjC.  The previous change
would generate the import library properly by handling the option but
would not consume the generated entry properly.

llvm-svn: 301657
2017-04-28 17:06:40 +00:00
Rui Ueyama a85572ebf0 COFF ICF: Merge only functions. Do not merge read-only data.
This seems to be the behavior of the MSVC linker. Previously, this
incompatibility caused nasty issues in chromium build a few times.

Differential Revision: https://reviews.llvm.org/D30363

llvm-svn: 301598
2017-04-27 23:03:22 +00:00
George Rimar 79d271b40b [ELF] - Modify testcase because of llvm-dwarfdump dump format change.
llvm-dwarfdump format changed in r301527  

llvm-svn: 301528
2017-04-27 10:00:39 +00:00
Rafael Espindola 4f013bb3b2 Create an OutputSection for each non-empty OutputSectionCommand.
We were already pretty close, the one exception was when a name was
reused in another SECTIONS directive:

SECTIONS {
  .text : { *(.text) }
  .data : { *(.data) }
}
SECTIONS {
  .data : { *(other) }
}

In this case we would create a single .data and magically output
"other" while looking at the first OutputSectionCommand.

We now create two .data sections. This matches what gold does. If we
really want to create a single one, we should change the parser so that
the above is parsed as if the user had written

SECTIONS {
  .text : { *(.text) }
  .data : { *(.data) *(other)}
}

That is, there should be only one OutputSectionCommand for .data and
it would have two InputSectionDescriptions.

By itself this patch makes the code a bit more complicated, but is an
important step in allowing assignAddresses to operate just on the
linker script.

llvm-svn: 301484
2017-04-26 22:30:15 +00:00
Peter Collingbourne fa58f7528e LTO: Mark undefined module asm symbols as used.
Marking them as used causes them to be considered visible outside of LTO. This
prevents the symbols from being internalized or discarded, either by GlobalDCE
or by summary-based dead stripping in ThinLTO.

This change makes it unnecessary to add these symbols to llvm.compiler.used
in the backend, as the symbols are kept alive by virtue of being external,
so remove the backend code that handles that.

Fixes PR32798.

Differential Revision: https://reviews.llvm.org/D32544

llvm-svn: 301438
2017-04-26 17:53:39 +00:00
Rui Ueyama d283eb7091 Make LLD work even if the current directory is not writable.
llvm-svn: 301423
2017-04-26 16:15:07 +00:00
Rafael Espindola 40d406534e Use CHECK-NEXT in a test.
This will simplify a future patch.

llvm-svn: 301415
2017-04-26 15:05:10 +00:00
George Rimar 9703ad2221 [ELF] - Implemented --defsym option.
gnu ld description of option is:

--defsym=symbol=expression
Create a global symbol in the output file, containing the absolute address given 
by expression. You may use this option as many times as necessary to define multiple
symbols in the command line. A limited form of arithmetic is supported for the 
expression in this context: you may give a hexadecimal constant or the name of an
existing symbol, or use "+" and "-" to add or subtract hexadecimal constants or 
symbols. If you need more elaborate expressions, consider using the linker command
language from a script. Note: there should be no white space between symbol, 
the equals sign ("="), and expression.

In compare with D32082, this patch does not support math expressions and absolute
symbols. It implemented via code similar to --wrap. That covers 1 of 3 possible
--defsym cases.

Differential revision: https://reviews.llvm.org/D32171

llvm-svn: 301391
2017-04-26 10:40:02 +00:00
Rui Ueyama 9b52d67e17 Attempt to fix a Windows bot.
Looks like `echo "{_start;};"` on lld-x86_64-win7 bot echoes `_start;;`
for some reason. I cannot reproduce the issue locally. I suspect that
it might be interpreting {<word>} in a special way. This patch is to
attempt to fix it by appending space characters.

llvm-svn: 301353
2017-04-25 19:55:28 +00:00
Rui Ueyama b04562a990 Add a test for ICF and REL relocations.
llvm-svn: 301332
2017-04-25 17:19:15 +00:00
Rui Ueyama 577168ecde [ELF] Improve error message for incompatible section flags
Previously we were not printing out the flags of the incompatible
section which made it difficult to determine what the problem was.

The error message format has been change to the following:

  error: incompatible section flags for .bar
  >>> /foo/bar/incompatible-section-flags.s.tmp.o:(.bar): 0x403
  >>> output section .bar: 0x3

Patch by Alexander Richardson.

Differential Revision: https://reviews.llvm.org/D32484

llvm-svn: 301319
2017-04-25 16:00:44 +00:00
Rui Ueyama 303a5cafbb Do not create temporary files in the main source tree.
llvm-svn: 301286
2017-04-25 03:30:50 +00:00
Rui Ueyama 321b9cd072 Export __progname even if a -dynamic-list is given.
BSD's __progname symbol is defined in crt1.o and linked against main
executables. The libc expects that main executables export __progname
symbol via .dynsym sections. In order to handle this case, we scan
undefined symbols in DSOs and exported them by setting Sym->ExportDynamic
to true.

But it turned out that setting that variable is not enough to make sure
that symbols are exported in all use cases. If a -dynamic-list option is
given, all symbols not explicitly mentioned in a version script are
hidden by default. That hides __progname symbol. This patch fixes the issue.

Fixes https://bugs.llvm.org/show_bug.cgi?id=32703

llvm-svn: 301282
2017-04-25 00:15:48 +00:00
Rui Ueyama c1d043c40a Replace a binary .so with a text .s.
Since LLD is now able to take version scripts, we no longer
have to keep a pre-generated binary file for a test.

llvm-svn: 301279
2017-04-24 23:51:18 +00:00
Rui Ueyama 95fe854332 Handle _LINK_ env string as command line parameters.
"_LINK_" environment varaible should be appended to the command line.
https://msdn.microsoft.com/en-us/library/6y6t9esh.aspx

Fixes https://bugs.llvm.org/show_bug.cgi?id=32756

llvm-svn: 301264
2017-04-24 22:20:03 +00:00
Shoaib Meenai b573b4b04d [ELF] Account for R_386_TLS_LDO_32 addend
This relocation type has an implicit addend. Account for it when
processing the relocation. Add an offset to an existing test to ensure
it gets processed correctly.

Fixes PR32634.

Differential Revision: https://reviews.llvm.org/D32336

llvm-svn: 301207
2017-04-24 18:02:11 +00:00
Bob Haarman 4110816253 [coff] for /msvclto, pass archive members with prevailing symbols first
Summary: When using /msvclto, lld and MSVC's linker both do their own symbol resolution. This can cause them to select different archive members, which can result in undefined references. This change avoids that situation by extracting archive members that are selected by lld and passing those to link.exe before any archives, so that MSVC's uses those objects for symbol resolution instead of different archive members.

Reviewers: pcc, rnk, ruiu

Reviewed By: pcc

Subscribers: llvm-commits, mehdi_amini

Differential Revision: https://reviews.llvm.org/D32317

llvm-svn: 301045
2017-04-21 21:38:01 +00:00
Saleem Abdulrasool 72fbd346c6 COFF: add support for CONSTANT exports
The CONSTANT export type is marked as obsolete, but link still supports
this.  Furthermore, WinObjC uses this for certain exports.  Add support
for this export type.

llvm-svn: 301013
2017-04-21 18:05:46 +00:00
Peter Smith cbfe9e946f [ELF] Always use Script::assignAddresses()
This change fabricates linker script commands for the case where there is
no linker script SECTIONS to control address assignment. This permits us
to have a single Script->assignAddresses() function.
    
There is a small change in user-visible-behavior with respect to the
handling of .tbss SHT_NOBITS, SHF_TLS as the Script->assignAddresses()
requires setDot() to be called with monotically increasing addresses.
The tls-offset.s test has been updated so that the script and non-script
results match.

This change should make the non-script behavior of lld closer to an
equivalent linker script.

Differential Revision: https://reviews.llvm.org/D31888

llvm-svn: 300687
2017-04-19 12:46:32 +00:00
Rui Ueyama f3f923c9d2 Write multiple REQUIRES in one line.
llvm-svn: 300685
2017-04-19 12:21:46 +00:00
Rui Ueyama ac377f2f11 Rename a test file so that it matches with the option name it is testing.
llvm-svn: 300679
2017-04-19 11:42:12 +00:00
Rui Ueyama 151cc0a15b Remove a redundant test.
We already have compress-debug-output.s, and that test should suffice
for the -compressed-debug-section option.

llvm-svn: 300677
2017-04-19 11:34:55 +00:00