Commit Graph

7124 Commits

Author SHA1 Message Date
Rafael Espindola 88eb6482ca Add a faster binary search.
Even with the hash table cache, binary search was still pretty
hot. This can be made even faster with prefetching.

Idea from http://cglab.ca/~morin/misc/arraylayout-v2/

I will suggest moving this to llvm.

llvm-svn: 284594
2016-10-19 14:17:36 +00:00
Eugene Leviant 74c1b6c548 [ELF] Add "error" and "warning" prefixes to linker messages
Differential revision: https://reviews.llvm.org/D25729

llvm-svn: 284575
2016-10-19 09:35:19 +00:00
Justin Lebar 3c11e93831 Use CachedHashStringRef instead of CachedHash<StringRef>.
Summary:
This uses one less word on 64-bit platforms, so should be a strict
improvement.  This change also lets us get rid of llvm::CachedHash.

Reviewers: rafael, timshen

Subscribers: llvm-commits

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

llvm-svn: 284502
2016-10-18 17:50:36 +00:00
Rafael Espindola ad74c1cfdf Use reserve instead of grow.
The table was still being resized as grow doesn't account for the fact
that the table needs to remain 3/4 full.

llvm-svn: 284487
2016-10-18 15:31:23 +00:00
George Rimar 9f578ceed7 [ELF] - Update testcases after llvm change.
llvm-svn: 284474
2016-10-18 11:11:03 +00:00
George Rimar 95dd718c98 [ELF] - Linkerscript: accept integer values for PHDRS types.
Both gold and ld accepts integers instead of named constants
for PHDRS.
Patch adds support for that.

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

llvm-svn: 284470
2016-10-18 10:49:50 +00:00
Chris Bieneman 62113e854a [CMake] Add missing dependency on intrinsics_gen
COFF/InputFiles.h includes LTOModule.h which transitively relies on Attributes.inc.

llvm-svn: 284444
2016-10-18 00:50:39 +00:00
Justin Lebar ee34a7343d [ADT] Move CachedHashString to its own header in ADT, and rename to CachedHashStringRef.
Summary:
Reclaiming the name 'CachedHashString' will let us add a type with that
name that owns its value.

Reviewers: timshen

Subscribers: llvm-commits

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

llvm-svn: 284434
2016-10-17 22:24:36 +00:00
Rafael Espindola 00ee5edc0d Avoid using getComdatSymbolTable.
This is not particularly efficient, but does avoid exposing Comdat*
out of LTO.h.

I will send a patch for review with a more efficient interface that
should map nicely to a bitcode symbol table.

llvm-svn: 284413
2016-10-17 19:25:08 +00:00
Rafael Espindola b18783e441 Update for llvm change.
llvm-svn: 284409
2016-10-17 18:51:27 +00:00
Justin Bogner c197ae0d74 ELF: Implement skip() as (void)next()
Thanks to Rafael for pointing out the simplification.

llvm-svn: 284407
2016-10-17 18:40:35 +00:00
Peter Smith 9bbd4e27a9 [ELF] Support for R_ARM_TARGET2 relocation
The R_ARM_TARGET2 relocation is used in ARM exception tables to encode
a data dependency that will only be dereferenced by code in the
run-time support library. In a similar way to R_ARM_TARGET1 the
handling of the relocation is target specific, it maps to one of
R_ARM_ABS32, R_ARM_REL32 or R_ARM_GOT_PREL. The choice depends on the 
run-time library. R_ARM_GOT_PREL is used for linux and BSD,
R_ARM_ABS32 and R_ARM_REL32 are used for bare-metal.

The command line option --target2=<target> can be used to select the
relocation used for R_ARM_TARGET2. The default is R_ARM_GOT_PREL.

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

llvm-svn: 284404
2016-10-17 18:12:24 +00:00
Rui Ueyama 83043f237c Rename skip(StringRef) -> consume(StringRef).
skip() and skip(StringRef) were overloaded functions that
have different semantics. This patch rename one of the functions
to avoid function overloading.

llvm-svn: 284396
2016-10-17 16:01:53 +00:00
George Rimar c3c4f46d07 [ELF] - Add support for -nopie
This is https://llvm.org/bugs/show_bug.cgi?id=30696,

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

llvm-svn: 284388
2016-10-17 14:42:11 +00:00
George Rimar 17d566daaa [ELF] - Don't crash on multiple SHT_MIPS_REGINFO/SHT_MIPS_ABIFLAGS sections.
In continue of D25555, this patch fixes possible crash when
we have multiple SHT_MIPS_REGINFO or SHT_MIPS_ABIFLAGS sections.
yaml2obj was used to produce such objects.

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

llvm-svn: 284376
2016-10-17 11:31:46 +00:00
George Rimar 21cd1a6fa9 [ELF] - Added testcase relative to D25090.
It was requested on review for https://reviews.llvm.org/D25090 to add testcase in lld.

Spec says (http://www.sco.com/developers/gabi/1998-04-29/ch4.eheader.html) :
e_shnum
This member holds the number of entries in the section header table. 
Thus the product of e_shentsize and e_shnum gives the section header table's size in bytes.
If a file has no section header table, e_shnum holds the value zero.

In case revealed, broken input did not contain zero in this field.
 LLD then could crash when proccessed sections (returned array has incorrect size):

template <class ELFT> void SharedFile<ELFT>::parseSoName() {
...
  for (const Elf_Shdr &Sec : Obj.sections()) {
...

llvm-svn: 284375
2016-10-17 11:15:12 +00:00
Justin Bogner 5424e7c7dc ELF: Add a skip() overload to ignore any token
Most functions that return StringRef should check their return values,
so I'm planning on marking StringRef [[nodiscard]]. This requires
splitting up functions like next() that are sometimes just used for
side effects.

llvm-svn: 284363
2016-10-17 06:21:13 +00:00
Justin Bogner 5af1687fd2 ELF: Fix a misuse of Twine::toStringRef
While the toStringRef API almost certainly ends up populating the
SmallString here, the correct way to use this API is to use the return
value.

llvm-svn: 284361
2016-10-17 06:08:48 +00:00
George Rimar 270173f2db [ELF] - Added support of PT_OPENBSD_RANDOMIZE
This is 30646.

PT_OPENBSD_RANDOMIZE
The array element specifies the location and size of a part of the memory image of the program that must be filled with random data before any code in the object is executed. The memory region specified by a segment of this type may overlap the region specified by a PT_GNU_RELRO segment, in which case the intersection will be filled with random data before being marked read-only.

Reference links:
http://man.openbsd.org/OpenBSD-current/man5/elf.5
c494713c45

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

llvm-svn: 284234
2016-10-14 13:02:22 +00:00
George Rimar 85e6106d76 [ELF] - Do not crash when object has multiple SHT_MIPS_OPTIONS
Issue was revealed by AFl and I was able to generate such object using yaml2obj.

When object has more than one SHT_MIPS_OPTIONS, 
each except the last one is destroyed after placing into Sections array.
Sections array contains dead pointers finally. LLD may crash then.

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

llvm-svn: 284227
2016-10-14 11:10:36 +00:00
George Rimar cc6e567ca3 [ELF] - Implemented -z wxneeded.
-z wxneeded creates a PHDR PT_OPENBSD_WXNEEDED.

PT_OPENBSD_WXNEEDED
The array element specifies that a process executing this file may need to be able to map or protect memory regions as simultaneously executable and writable. If the system is unable or unwilling to permit that for this executable then it may fail immediately. This segment type is meaningful only for executable files and is ignored in other objects.

http://man.openbsd.org/OpenBSD-current/man5/elf.5

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

llvm-svn: 284226
2016-10-14 10:34:36 +00:00
Konstantin Zhuravlyov d4327e95dd [ELF/AMDGPU]: Add support for new relocations
Differential Revision: https://reviews.llvm.org/D25563

llvm-svn: 284197
2016-10-14 04:51:43 +00:00
Rui Ueyama b04af133f5 Improve comments.
llvm-svn: 284177
2016-10-13 23:08:33 +00:00
Peter Collingbourne 24a01c341e ELF: Override DSO definitions when creating __start_* and __stop_* symbols.
Previously we would fail to synthesise a __start_ or __stop_ symbol if
there existed a definition in a DSO. Instead, we would try to link against
the DSO definition. This became possible after D23552 when linking against
lld-produced DSOs but could in principle also occur when linking against
DSOs produced by other linkers.

Not only does it seem more likely that a user would expect the resolved
definition to be local to the executable, but if a __start_ or __stop_
symbol was synthesised by the linker, it is effectively impossible to link
against correctly from a non-PIC executable in a read-only section. Neither
a PLT nor a copy relocation would give us the right semantics here. The only
way the link could succeed is if the executable provided its own synthetic
definition of the symbol.

The fix is to also synthesise the definition if the only definition comes
from a DSO. Since this is what the addOptionalSynthetic function does,
switch to using that function.

Fixes PR30680.

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

llvm-svn: 284168
2016-10-13 22:20:18 +00:00
Peter Collingbourne 3aa0a2510e Add "opt" to the list of test dependencies.
We've started using it in the ThinLTO tests.

Also remove a repeated dependency on llvm-nm.

llvm-svn: 284095
2016-10-13 00:49:21 +00:00
Rui Ueyama acb67bcb98 Move a utility function to Strings.cpp.
So that we can use the function from anywhere.

llvm-svn: 284092
2016-10-13 00:13:15 +00:00
Rui Ueyama 5665af836f Fix variable name. NFC.
llvm-svn: 284079
2016-10-12 23:22:59 +00:00
Rui Ueyama 05384080df Support GNU-style ZLIB-compressed input sections.
Previously, we supported only SHF_COMPRESSED sections because it's
new and it's the ELF standard. But there are object files compressed
in the GNU style out there, so we had to support it.

Sections compressed in the GNU style start with ".zdebug_" and
contain different headers than the ELF standard's one. In this
patch, getRawCompressedData is responsible to handle it.

A tricky thing about GNU-style compressed sections is that we have
to rename them when creating output sections. ".zdebug_" prefix
implies the section is compressed. We need to rename ".zdebug_"
".debug" because our output sections are not compressed.
We do that in this patch.

llvm-svn: 284068
2016-10-12 22:36:31 +00:00
Davide Italiano 726f96e63f [ThinLTO] Test we handle collisions correctly in presence of --whole-archive.
llvm-svn: 284050
2016-10-12 20:52:52 +00:00
Davide Italiano bcdd6c60a0 [ThinLTO] Avoid archive member collisions.
This fixes PR30665.

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

llvm-svn: 284034
2016-10-12 19:35:54 +00:00
Eugene Leviant cc1ba8c7d0 Alternative fix for reloc tareting discarded section
r283984 introduced a problem of too many warning messages being shown
when -ffunction-sections and -fdata-sections were used in conjunction 
with --gc-sections linker flag and debugging information present. This
happens because lot of relocations from .debug_line section may become
invalid in such case. The newer fix doesn't show any warning message but
zeroes OutSec pointer in createInputSectionList() to avoid crash, when
relocations are written

llvm-svn: 284010
2016-10-12 12:31:34 +00:00
Hans Wennborg cb8f2ef644 LTO: Don't follow lazy references when seeing new definitions in the combined LTO object
Following the lazy reference might bring in an object file that depends
on bitcode files that weren't part of the LTO step.

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

llvm-svn: 283989
2016-10-12 09:00:06 +00:00
Eugene Leviant c958d8d621 Don't crash if reloc targets discarded section
Differential revision: https://reviews.llvm.org/D25433

llvm-svn: 283984
2016-10-12 08:19:30 +00:00
Rui Ueyama e05e9502c5 Fix a test to pacify llvm-clang-lld-x86_64-scei-ps4-windows10pro bot.
I don't really understand why we get a larger .rodata section only
on this bot. I guess it may be picking up a library which contains
a .rodata. I removed the specific values since their values are not
important for this test case.

llvm-svn: 283931
2016-10-11 20:15:36 +00:00
Rui Ueyama 9f66f8277d Re-submit r283825: Add section header stream to PDB.
It was reverted because the change that depends on was reverted.
Now it was submitted as r283925, so we can submit this as well.

llvm-svn: 283926
2016-10-11 19:45:07 +00:00
Rui Ueyama 110dda7915 Make variable name consistent with other -z options.
llvm-svn: 283910
2016-10-11 17:46:48 +00:00
Davide Italiano b67948f497 [ThinLTO/tests] Check we don't fail if --thinlto-jobs is not passed.
While here, fix the CHECK lines of an existing test.

llvm-svn: 283889
2016-10-11 15:32:34 +00:00
George Rimar 78fe56e5ef [ELF] - Fixed assertion fail when symbol table has invalid sh_info value.
This part was splitted from D25016.

When sh_info value was set in the way that non-local symbol was treated as local, lld
was asserting, patch fixes that.

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

llvm-svn: 283859
2016-10-11 09:07:14 +00:00
Rui Ueyama 9aa4ab6f9b Revert "Add section header stream to PDB." because it depends on r283823.
The change this patch depends on was reverted.

llvm-svn: 283837
2016-10-11 01:01:40 +00:00
Davide Italiano 4683f9d7dc [ThinLTO] Attempt to fix the test (and the bots).
llvm-svn: 283826
2016-10-10 23:56:13 +00:00
Rui Ueyama 55505954fe Add section header stream to PDB.
Differential Revision: https://reviews.llvm.org/D25357

llvm-svn: 283825
2016-10-10 23:44:10 +00:00
Davide Italiano b6e6e4a074 [LTO] Split the options for ThinLTO jobs and Regular LTO partitions
Differential Revision:   https://reviews.llvm.org/D25452

llvm-svn: 283817
2016-10-10 23:12:14 +00:00
Davide Italiano 7a7b35ae3b [LTO/Thin] Make the number of threads to run in the BE configurable.
Before the default was whatever number hardware_concurrency() returned.
Users can specify the number of threads via --lto-jobs=X option.

llvm-svn: 283787
2016-10-10 18:12:53 +00:00
Rui Ueyama b224c048b4 Remove trailing whitespace.
llvm-svn: 283786
2016-10-10 18:10:01 +00:00
Eugene Leviant cd8eaf891a [ELF] Don't emit empty PT_LOAD segment
Sometimes the very first PT_LOAD segment, created by lld, can be empty. 
This happens when (all conditions met):

- Linker script is used
- First section in ELF image is not RO
- Not enough space for program headers.

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

llvm-svn: 283760
2016-10-10 15:09:44 +00:00
Eugene Leviant 9257764d61 [ELF] Change argument type for findSections. NFC
llvm-svn: 283742
2016-10-10 11:23:12 +00:00
George Rimar 27e651d4f6 Recommit r283733 "[ELF] - Do not crash if common symbol alignment set to value greater than UINT32_MAX.
With fix: commit changes from InputFiles.cpp too.

Original commit message:
We have following code in lld, that truncates the alignment value to 32 bit. Big alignment in this case
may give result 0 and crash later.

template <class ELFT>
CommonInputSection<ELFT>::CommonInputSection(std::vector<DefinedCommon *> Syms)
    : InputSection<ELFT>(nullptr, &Hdr, "") {
....
  for (DefinedCommon *Sym : Syms) {
    this->Alignment = std::max<uintX_t>(this->Alignment, Sym->Alignment);
...
  }
}

Patch fixes the issue.

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

llvm-svn: 283738
2016-10-10 10:31:03 +00:00
George Rimar a1b8245654 Revert r283733 "[ELF] - Do not crash if common symbol alignment set to value greater than UINT32_MAX."
Broke BB because forgot to commit source file.

llvm-svn: 283737
2016-10-10 10:27:38 +00:00
Peter Smith bafd3c40e2 [ELF][ARM] Fix formatting mistake, removed extra space before ) NFC.
llvm-svn: 283735
2016-10-10 10:18:58 +00:00
Peter Smith 0760605ac5 [ELF][ARM] Garbage collection support for .ARM.exidx sections
.ARM.exidx sections have a reverse dependency on the section they have
a SHF_LINK_ORDER dependency on. In other words a .ARM.exidx section is
live only if the executable section it describes is live. We implement
this with a reverse dependency field in InputSection.

Adding the dependency to InputSection is the simplest implementation
but it could be moved out to a separate map if it were found to decrease
performance for non ARM targets.

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

llvm-svn: 283734
2016-10-10 10:10:27 +00:00
George Rimar 50188b2771 [ELF] - Do not crash if common symbol alignment set to value greater than UINT32_MAX.
We have following code in lld, that truncates the alignment value to 32 bit. Big alignment in this case
may give result 0 and crash later.

template <class ELFT>
CommonInputSection<ELFT>::CommonInputSection(std::vector<DefinedCommon *> Syms)
    : InputSection<ELFT>(nullptr, &Hdr, "") {
....
  for (DefinedCommon *Sym : Syms) {
    this->Alignment = std::max<uintX_t>(this->Alignment, Sym->Alignment);
...
  }
}

Patch fixes the issue.

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

llvm-svn: 283733
2016-10-10 10:10:09 +00:00
George Rimar af58b07acc [ELF] - Do not crash on absolute local symbol starting from ".L".
Absolute local symbols with name staring from ".L" were reason of crash.
The same could happen when using some broken inputs found by AFL.

Patch fixes that.

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

llvm-svn: 283731
2016-10-10 09:59:34 +00:00
Peter Smith 0a259f3b9c [ELF][ARM] Initial implentation of ARM exceptions support
The .ARM.exidx sections contain a table. Each entry has two fields:
- PREL31 offset to the function the table entry describes
- Action to take, either cantunwind, inline unwind, or PREL31 offset to
  .ARM.extab section

The table entries must be sorted in order of the virtual addresses the
first entry of the table describes. Traditionally this is implemented by
the SHF_LINK_ORDER dependency. Instead of implementing this directly we
sort the table entries post relocation. 

The .ARM.exidx OutputSection is described by the PT_ARM_EXIDX program
header

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

llvm-svn: 283730
2016-10-10 09:39:26 +00:00
Rui Ueyama 8c8ff00b28 Improve comments.
Also use uint64_t instead of uintX_t so that you don't have to
think about two different cases to verify that the code is correct.

llvm-svn: 283585
2016-10-07 19:54:57 +00:00
George Rimar 9397cf953c [ELF] - Fixed assert fail when symbol table has zero sh_info value.
When sh_info of sumbol table value was set to zero, lld was asserting.

Patch fixes the issue.

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

llvm-svn: 283562
2016-10-07 15:16:27 +00:00
Rafael Espindola 88b9d66d4a Add () for clarity. NFC.
llvm-svn: 283556
2016-10-07 14:23:28 +00:00
George Rimar 7c213fd17e [ELF] - Check that section alignment is a power of 2.
I found that this check still may be useful in some cases.
At fact since we use uint32_t alignment, then maximum value
that is valid for us is 0x80000000. But some broken files,
for example file from testcase may have greater value.
Because of that offset calculation overflow and crash happens.

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

llvm-svn: 283544
2016-10-07 12:27:45 +00:00
George Rimar 53cf2a8112 [ELF] - Do not crash on invalid size of dynamic section.
Previously if sh_size of dynamic section was broken,
lld may crash. Or even may not crash if used 32 bits host.
(then value may be truncated to 32 bits when doing pointer arithmetic 
and could be just zero).
Patch fixes the issue.

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

llvm-svn: 283533
2016-10-07 09:01:04 +00:00
George Rimar b7aec33125 [ELF] - Do not crash when unable to parse ELF object file.
createELFObj() may call error(...), for example when file is too short.
In that case header is not set and following line lead to crash:

EMachine = ELFObj.getHeader()->e_machine;

Patch fixes the issue.

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

llvm-svn: 283532
2016-10-07 08:51:57 +00:00
Rui Ueyama 1343fac3ca Add an empty DBI stream.
llvm-svn: 283504
2016-10-06 22:52:01 +00:00
Rui Ueyama f65f56e002 Remove useless function.
llvm-svn: 283488
2016-10-06 20:04:54 +00:00
Eugene Leviant 898375994b [ELF] Don't fail if undefined symbol is not used
Differential revision: https://reviews.llvm.org/D25240

llvm-svn: 283431
2016-10-06 09:45:04 +00:00
Eugene Leviant b71d6f7a72 [ELF] Linker script: implement LOADADDR
Differential revision: https://reviews.llvm.org/D24298

llvm-svn: 283429
2016-10-06 09:39:28 +00:00
George Rimar 24adce95b2 [ELF] - Make checks in ObjectFile<ELFT>::getSection() stricter.
This patch makes the check for null section stricter, 
so it is only allowed for STT_SECTION symbols now.

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

llvm-svn: 283426
2016-10-06 09:17:55 +00:00
Rui Ueyama 8d3fb5da7b Add exact number of streams for reserved stream #s.
llvm-svn: 283397
2016-10-05 22:08:58 +00:00
Rui Ueyama d381c9842b Add an empty IPI stream.
With this, "llvm-pdbdump yaml -ipi-stream" prints out an IPI stream.
Previously it crashed because it can't handle the case where IPI
stream doesn't exist.

llvm-svn: 283392
2016-10-05 21:37:25 +00:00
Rui Ueyama a03380808c Early continue. NFC.
llvm-svn: 283382
2016-10-05 21:06:32 +00:00
Rui Ueyama b66260ac17 Remove trailing whitespace.
llvm-svn: 283372
2016-10-05 20:09:50 +00:00
Rafael Espindola 5fc2b1d2fe Store the hash in SectionPiece.
This spreads out computing the hash and using it in a hash table. The
speedups are:

firefox
  master 6.811232891
  patch  6.559280249 1.03841162939x faster
chromium
  master 4.369323666
  patch  4.33171853 1.00868134338x faster
chromium fast
  master 1.856679971
  patch  1.850617741 1.00327578725x faster
the gold plugin
  master 0.32917962
  patch  0.325711944 1.01064645023x faster
clang
  master 0.558015452
  patch  0.550284165 1.01404962652x faster
llvm-as
  master 0.032563515
  patch  0.032152077 1.01279662275x faster
the gold plugin fsds
  master 0.356221362
  patch  0.352772162 1.00977741549x faster
clang fsds
  master 0.635096494
  patch  0.627249229 1.01251060127x faster
llvm-as fsds
  master 0.030183188
  patch  0.029889544 1.00982430511x faster
scylla
  master 3.071448906
  patch  2.938484138 1.04524944215x faster

This seems to be because we don't stall as much. When linking firefox
stalled-cycles-frontend goes from 57.56% to 55.55%.

With -O2 the difference is even more significant since we avoid
recomputing the hash. For firefox we go from 9.990295265 to
 9.149627521 seconds (1.09x faster).

llvm-svn: 283367
2016-10-05 19:36:02 +00:00
Rafael Espindola 32aca87bf8 Compact SectionPiece.
It is pretty easy to get the data from the InputSection, so we don't
have to store it.

This opens the way for storing the hash instead.

llvm-svn: 283357
2016-10-05 18:40:00 +00:00
Rafael Espindola 939e9493bf Simplify setting the Live bit in SectionPiece. NFC.
llvm-svn: 283340
2016-10-05 17:02:09 +00:00
Rafael Espindola 7b8bb53e94 Remove redundant check. NFC.
llvm-svn: 283328
2016-10-05 15:35:18 +00:00
Eugene Leviant a8d12ef853 Do not join sections for relocatable object files
Differential revision: https://reviews.llvm.org/D25232

llvm-svn: 283307
2016-10-05 10:10:45 +00:00
Eugene Leviant cf43f179b1 [ELF] make KEEP command recognize file patterns
Differential revision: https://reviews.llvm.org/D25242

llvm-svn: 283305
2016-10-05 09:36:59 +00:00
Simon Atanasyan 02b9c3f8c3 [ELF] Do not merge sections in case of relocatable object generation
Do not merge sections if generating a relocatable object. It makes
the code simpler because we do not need to update relocations addends
to reflect changes introduced by merging. Instead of that we write
such "merge" sections into separate OutputSections and keep SHF_MERGE
/ SHF_STRINGS flags and sh_entsize value to be able to perform merging
later during a final linking.

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

llvm-svn: 283300
2016-10-05 07:49:18 +00:00
Mehdi Amini ec4fb5ba97 Use StringRef in StringSaver API (NFC)
llvm-svn: 283290
2016-10-05 01:32:41 +00:00
Rafael Espindola 259d645147 Update for llvm change.
llvm-svn: 283267
2016-10-04 22:43:38 +00:00
Rui Ueyama af9793d1c1 Add a comment.
llvm-svn: 283229
2016-10-04 16:47:49 +00:00
George Rimar e252538107 [ELF] - Do not crash on MIPS if there is no object files in input.
If we have input without object files, for example if we have only .so
code crashes in checkFlags(), getPicFlags(), getArchFlags() functions.

Patch fixes the issue.

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

llvm-svn: 283226
2016-10-04 15:19:20 +00:00
Eugene Leviant b90f3cf15b [ELF] Don't discard excluded section if -r is used
This conforms to GNU ld/gold behavior and fixes crash in case
excluded section also has associated relocation section

llvm-svn: 283214
2016-10-04 12:05:42 +00:00
George Rimar 40be15cfb0 [ELF] - Removed "invalid-" prefix from testcase. NFC.
llvm-svn: 283213
2016-10-04 11:51:38 +00:00
George Rimar 422e47a9f5 [ELF] - Do not hang if broken object has option descriptor in .MIPS.options with size of zero.
Previously lld would hang in infinite loop in this case,
patch fixes the issue. Object was found during AFL run.

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

llvm-svn: 283208
2016-10-04 10:23:07 +00:00
George Rimar b1f78d1202 [ELF] - Do not crash on invalid symbol index.
Relative to PR30540.

If .symtab has invalid type in elf, no bodies are created and any relocation
that tries to access them will fail.
The same can happen if symbol index is just incorrect.

This was revealed by "id_000005,sig_11,src_000000,op_flip2,pos_420"

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

llvm-svn: 283201
2016-10-04 09:02:42 +00:00
Eugene Leviant ee8dcfbdf7 [ELF] Set max page size to 64K for AArch64
Differential revision: https://reviews.llvm.org/D25079

llvm-svn: 283200
2016-10-04 08:58:55 +00:00
George Rimar 6a3b154aab [ELF] - Do not crash if symbol type set to TLS when there is no tls sections.
id_000021,sig_11,src_000002,op_flip1,pos_92 from PR30540

does not have TLS sections, but type
of one of the symbol is broken and set to STT_TLS,
what resulted in a crash. Patch fixes crash.

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

llvm-svn: 283198
2016-10-04 08:52:51 +00:00
George Rimar 0c82561c4c [ELF] - Do not crash if common symbol has alignment 0.
Testcase contains a common symbol with zero alignment,
previously lld would crash, patch fixes that.

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

llvm-svn: 283197
2016-10-04 08:49:52 +00:00
Simon Atanasyan 3a61cabbb2 [ELF][MIPS] Do not accept non-zero GP0 value for relocatable object only
Follow-up to r282716. Reject input files with non-zero GP0 value only in
case of relocatable object generation. In other case we can handle
arbitrary GP0 value so it does not have a sense to make the restriction
so wide.

llvm-svn: 283194
2016-10-04 08:24:25 +00:00
Rui Ueyama ab8e88b686 Remove unused parameter.
I do not fully understand how to use these classes yet, but
seems like these arguments are not used, since without them
all tests still pass. In order to simplify the situation,
I'll remove them now.

llvm-svn: 283174
2016-10-04 01:03:03 +00:00
Rui Ueyama ad9268a7b4 Early return. NFC.
llvm-svn: 283173
2016-10-04 00:46:36 +00:00
George Rimar 10874f70fa [ELF] - Apply clang-format. NFC.
llvm-svn: 283101
2016-10-03 11:13:55 +00:00
George Rimar 2c0a7f081a [ELF] - Do not crash on invalid section alignment.
Case was revealed by id_000010,sig_08,src_000000,op_havoc,rep_4 from PR30540.

Out implementation uses uint32 for storing section alignment value,
what seems reasonable, though if value exceeds 32 bits bounds we have
truncation and final value of 0.

Patch fixes the issue.

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

llvm-svn: 283097
2016-10-03 10:04:38 +00:00
Rui Ueyama c79ecdd876 Use StringSwtich::Cases for multiple choices.
llvm-svn: 282964
2016-09-30 22:01:25 +00:00
Rui Ueyama 129795497d Change for LLVM upstream change r282944.
llvm-svn: 282945
2016-09-30 20:53:45 +00:00
Rui Ueyama 5c82eeaf23 Change for LLVM change r282940.
llvm-svn: 282942
2016-09-30 20:39:04 +00:00
Rui Ueyama 062d7e4911 Removed a test that should've been removed with r282916.
llvm-svn: 282926
2016-09-30 20:17:26 +00:00
Rui Ueyama eb0007aa0c Remove an warning message that can be spammy.
This was intended to warn on a usage error of an ar command.
I was thinking that false positive would be rare because it
has two guards: it warns only when an archive file has no symbols
and contains at least one file.

Turned out that false positive would probably be not that rare.
You wouldn't intentionally create an object file that contains no
exported symbols, but with conditional compile guards, you could
create such file. If it is for a LTO build, you could create an
archive file containing such file. That means there's no way to
detect the usage error in a reliable manner.

This patch simply removes the warning.

llvm-svn: 282916
2016-09-30 19:59:25 +00:00
Rui Ueyama 3d0f77b281 Improve error check for an empty archive.
Previously, it warned on any archive file that has no symbol.
It turned out that that is too noisy.

With this patch, it warns on such archive file that contains no file.

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

llvm-svn: 282885
2016-09-30 17:56:20 +00:00
George Rimar f50bafc708 [ELF] - Inlined template argument variable. NFC.
llvm-svn: 282851
2016-09-30 13:46:34 +00:00
George Rimar 3da3ffa67f [ELF] - Removed excessive variables. NFC.
llvm-svn: 282850
2016-09-30 13:44:02 +00:00
Rafael Espindola 2629602f28 Fix another --gc-sections crash.
The missing case was when a merge section was only referenced from
non-alloca sections.

llvm-svn: 282847
2016-09-30 06:48:09 +00:00
Rafael Espindola c1eff79f61 Fix --gc-sections crash.
We would crash when a non-alloca section pointed to a gced part of a
merge section.

That can happen when a C/c++ constant in put in a merge section and
debug info is present.

llvm-svn: 282845
2016-09-30 06:37:29 +00:00
Rafael Espindola be6073345e Start linker scripts at 0.
We were implicitly creating space for the headers. That is not the
behaviour of bfd, which requires the script to use SIZEOF_HEADERS. The
difference is important for scripts that don't use SIZEOF_HEADERS and
expect the first section to be at 0.

llvm-svn: 282818
2016-09-30 00:16:11 +00:00
Rafael Espindola 317c339f61 Don't depend on lld creating space for the headers.
Currently lld will implicitly reserve space for the headers. This is
not the case is bfd, where it is the script responsibility to use
SIZEOF_HEADERS. This means that a script not using SIZEOF_HEADERS and
expecting the address of the first section to be 0 would fail with lld.

I am fixing that is the next commit. This one just makes the tests
explicitly use SIZEOF_HEADERS to avoid the dependency on the current
behaviour.

llvm-svn: 282814
2016-09-30 00:06:24 +00:00
Rui Ueyama e364d1f431 Add comments.
llvm-svn: 282807
2016-09-29 23:04:50 +00:00
Rafael Espindola 2d68f8ade4 Break long line. NFC.
llvm-svn: 282806
2016-09-29 23:02:57 +00:00
Rui Ueyama 541f48d0fc Use StringSwitch::Cases. NFC.
llvm-svn: 282802
2016-09-29 22:54:31 +00:00
Rui Ueyama 5b8a3b3262 Remove HasError check after lto::LTO::run.
Because LTO::run doesn't change HasError, it doesn't make
sense to check that value.

llvm-svn: 282801
2016-09-29 22:50:37 +00:00
Rafael Espindola e979fd1bf0 Don't put ro before rx when using linker scripts.
Since they end up going on the same PT_LOAD, there is no reason to
sort them. This matches bfd's behaviour and is user visible in the
placement of orphan sections.

llvm-svn: 282799
2016-09-29 22:48:55 +00:00
Rui Ueyama 1cc007aeae Keep a private member private.
llvm-svn: 282798
2016-09-29 22:37:45 +00:00
Rafael Espindola 475f2ccf98 List more sections in linker script.
This makes the test less brittle with regards to orphan section placement.

llvm-svn: 282797
2016-09-29 22:27:48 +00:00
Rui Ueyama 1375a9bdc6 Remove a return type that can be trivially easily.
llvm-svn: 282795
2016-09-29 22:14:20 +00:00
Rui Ueyama 6c5cbff97e Rename "void check(Error)".
We have a few "check" functions in Error.h. All of them are to
check for an error and strip an error object if there was no error,
except "void check(Error E)", which doesn't return anything.
This patch renames it and moves it to the .cpp file where it is used.

llvm-svn: 282764
2016-09-29 21:00:26 +00:00
Rui Ueyama d31e13f287 Rename warning -> warn.
It's better because it's a verb.

llvm-svn: 282763
2016-09-29 21:00:23 +00:00
Rafael Espindola f084edf40e Use > and not >> to create the script.
llvm-svn: 282760
2016-09-29 20:23:07 +00:00
Rafael Espindola 6d91fce526 Don't error if we can't put the header in a PT_LOAD.
If there is not sufficient address space, just give up and don't put
the header in the PT_LOAD.

This matches bfd behaviour and I found at least one script that
depends on having a section at address 0.

llvm-svn: 282750
2016-09-29 18:50:34 +00:00
Rafael Espindola c94678bf72 Simplify. NFC.
llvm-svn: 282725
2016-09-29 16:29:55 +00:00
Rafael Espindola 4e96895385 Don't handle binary output differently.
It is not clear if we need a different layout, so for now handle them
like regular ELF output.

llvm-svn: 282724
2016-09-29 16:24:17 +00:00
Simon Atanasyan 353fc1e947 [ELF][MIPS] Set GP0 value to zero in case of relocatable object generation
LLD does not update relocations addends when generate a relocatable
object. That is why we should not write a non-zero GP0 value into
the .reginfo and .MIPS.options sections. And we should not accept input
object files with non-zero GP0 value because we cannot handle them
properly.

llvm-svn: 282716
2016-09-29 12:58:48 +00:00
Simon Atanasyan f4a1d8d0e0 [ELF][MIPS] Do not use a binary input file in the test case. NFC
llvm-svn: 282715
2016-09-29 12:58:42 +00:00
Simon Atanasyan f967f090b8 [ELF][MIPS] Setup STO_MIPS_PIC flag for PIC symbols when generate a relocatable object
In case of linking PIC and non-PIC code together and generation of a
relocatable object, all PIC symbols should have STO_MIPS_PIC flag in the
symbol table of the ouput file.

llvm-svn: 282714
2016-09-29 12:58:36 +00:00
George Rimar 678bd05740 [ELF] - Fixed crash on invalid input.
I took the input from https://llvm.org/bugs/show_bug.cgi?id=30540, it was
"id_000000,sig_11,src_000000,op_flip1,pos_98"

File contains invalid symbol name offset (too large) and lld just crashes,
patch fixes the issue.

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

llvm-svn: 282709
2016-09-29 11:10:40 +00:00
Eugene Leviant d2778848ef Don't GC non-alloc mergeable section pieces
Differential revision: https://reviews.llvm.org/D25033

llvm-svn: 282708
2016-09-29 10:27:10 +00:00
George Rimar 1abe30a762 [ELF] - Testcases: remove "invalid-" prefix from inputs located in "invalid" subfolder. NFC.
llvm-svn: 282707
2016-09-29 09:39:06 +00:00
George Rimar c017bb3f11 [ELF] - Rename testcase folder from "Invalid" to "invalid". NFC.
llvm-svn: 282706
2016-09-29 09:30:01 +00:00
Eugene Leviant 3d9abec091 [ELF] Change the way we compute file offsets
If we two sections reside in the same PT_LOAD segment,
we compute second section using the following formula:
Off2 = Off1 + VA2 - VA1. This allows OS kernel allocating
sections correctly when loading an image.

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

llvm-svn: 282705
2016-09-29 09:20:33 +00:00
Lei Liu 6fa4fe83f7 [AArch64] Fix test case ELF/aarch64-tls-le.s
The add instruction should be generated with shift bit (lsl #12).
Update the test case.

llvm-svn: 282671
2016-09-29 02:01:27 +00:00
Rui Ueyama 0f327380bf Attempt to fix Windows buildbots.
llvm-svn: 282670
2016-09-29 01:59:03 +00:00
Rui Ueyama e8b2df47a3 Remove unused #includes.
llvm-svn: 282668
2016-09-29 01:45:22 +00:00
Rui Ueyama ed1414b83c Add comments.
llvm-svn: 282664
2016-09-29 01:26:48 +00:00
Rafael Espindola 615c5ae7ae Read addend from the correct location.
llvm-svn: 282663
2016-09-29 01:20:40 +00:00
Davide Italiano c985e8b341 [LTO] Fully qualify make_unique to make MSVC happy.
llvm-svn: 282662
2016-09-29 01:13:32 +00:00
Rui Ueyama d72dd1fc4e Use InputFile::Symbol instead of auto for consistency.
llvm-svn: 282658
2016-09-29 00:58:10 +00:00
Davide Italiano 786d8e33db [ELF/LTO] Switch to the new resolution-based API.
Differential Revision:  https://reviews.llvm.org/D24492

llvm-svn: 282656
2016-09-29 00:40:08 +00:00
Rui Ueyama cb85d7f840 Warn on empty archive files.
Differential Revision: https://reviews.llvm.org/D25044

llvm-svn: 282633
2016-09-28 21:10:54 +00:00
Rafael Espindola 5217f8452c Sort headers. NFC.
llvm-svn: 282603
2016-09-28 16:31:05 +00:00
Petr Hosek 997f8838e5 [ELF] Support -z max-page-size option
This options issupported by both BFD ld and gold and allows
overriding the max page size whose default values are defined by
the target.

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

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

llvm-svn: 282596
2016-09-28 15:20:47 +00:00
George Rimar dd381c39e1 [ELF] - Created new "Invalid" subfolder for testcases.
This subfolder just like "linkerscript" subfolder keeps
testcases with invalid input. According to PR30540 it seems
we might have many new ones soon, so it is seems reasonable to 
separate them from regular testcases.

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

llvm-svn: 282595
2016-09-28 15:11:58 +00:00
Eugene Leviant 27be542cdf [ELF] Ignore sections flagged with SHF_EXCLUDE
Differential revision: https://reviews.llvm.org/D24966

llvm-svn: 282568
2016-09-28 08:42:02 +00:00
Petr Hosek 5d98fef75d [ELF] Use MaxPageSize for aligning PT_LOAD
This matches the behavior of Binutils linkers. We also change the
default MaxPageSize on x86-64 to 0x1000 to preserver the current
behavior, which is the same as the behavior implemented by gold.

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

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

llvm-svn: 282560
2016-09-28 00:09:20 +00:00
Martin Storsjo 63762b58c0 [COFF] Add support for IMAGE_REL_ARM_SECREL
Handle this in the exact same way as IMAGE_REL_AMD64_SECREL
and IMAGE_REL_I386_SECREL.

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

llvm-svn: 282531
2016-09-27 19:27:17 +00:00
Rafael Espindola a42b3bcae4 Use xxhash for fast --build-id.
The speed improvements I got were:

firefox
  master 7.050784981
  patch  6.842361079 0.970439617353
chromium
  master 4.260626249
  patch  4.183148025 0.981815296749
chromium fast
  master 1.829028591
  patch  1.806439277 0.987649556649
the gold plugin
  master 0.336154128
  patch  0.331893374 0.987324998728
clang
  master 0.561869781
  patch  0.558640828 0.994253200458
llvm-as
  master 0.034025959
  patch  0.033984389 0.99877828572
the gold plugin fsds
  master 0.360710529
  patch  0.356483564 0.988281559145
clang fsds
  master 0.640518422
  patch  0.632329874 0.987215749432
llvm-as fsds
  master 0.031569416
  patch  0.030822055 0.976326423017
scylla
  master 3.154770529
  patch  3.11982016 0.988921422754

llvm-svn: 282505
2016-09-27 16:43:49 +00:00
George Rimar 3150799859 [ELF] - Fixed string encoding in test. NFC.
llvm-svn: 282497
2016-09-27 15:58:31 +00:00
George Rimar 74661eb040 [ELF] - Fixed linkage error when using -g --gc-sections together.
r282444 introduced new issue, sample program below
fails to link on

assert(Piece.Live);
int main() { return 0; }
clang test.cpp -c -o out.o -g
ld.lld -flavor gnu --gc-sections out.o -o out

Problem is that .debug_info contains relocations to .debug_str:
Section (7) .rela.debug_info {
..

0xC R_X86_64_32 .debug_str 0x0
0x12 R_X86_64_32 .debug_str 0x37
..
But we do not preserve .debug_str in a right way now.

To fix this we should ignore relocations from non-allocatable sections to allocatable
to allow GC work at full power, but still should proccess relocations from non-allocatable to non-allocatable sections
as usual to mark some parts of debug sections alive to keep them so we do not end 
up with such assert when trying to access dead pieces. That looks like what gold/ld do, they do 
not strip .debug_str section from what I saw using sample provided.

Thanks to Evgeny Leviant for suggestions about how to fix this.

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

llvm-svn: 282495
2016-09-27 15:55:29 +00:00
Rui Ueyama 3e9d6bbad6 Create FileOutputBuffer lazily.
So that it is clear that FileOutputBuffer does not depend on
PDB file builder. Eventually we will have to to get the file size
info from the file builder to create a file with the exact size.
NFC.

llvm-svn: 282454
2016-09-26 23:53:55 +00:00
Rafael Espindola 2b9008e5e5 Non alloca sections should not keep other sections live.
This matches the gold behaviour and is important to prevent debug info
from effectively disabling gc.

llvm-svn: 282444
2016-09-26 21:34:34 +00:00
George Rimar e38cbab5a4 [ELF] - Linkerscript: implemented BYTE/SHORT/LONG/QUAD commands.
The BYTE, SHORT, LONG, and QUAD commands store one, two, four, and eight bytes (respectively). 
After storing the bytes, the location counter is incremented by the number of bytes
stored.

Previously our scripts handles these commands incorrectly. For example:
SECTIONS  {
  .foo : {
 *(.foo.1)
 BYTE(0x11)
...
We accepted the script above treating BYTE as input section description. 
These commands are used in the wild though.

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

llvm-svn: 282429
2016-09-26 19:22:50 +00:00
George Rimar a213e6b6f5 [ELF] - Fix for: Bug 30521 - lld exits non-zero return value linking a library with no entry symbol
PR30521 was about linking shared library. After r282295 code when linking -shared produced
"entry symbol not found" warning, what in combination with --fatal-errors failed linkage.

Patch fixes logic (and adds testcases) to follow next rules:

1) If entry was specified and not found report warning.
2) If entry was not specified then:
 a) Emit warning if not -shared.
 b) Do not emit warning if -shared.

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

llvm-svn: 282427
2016-09-26 19:04:42 +00:00
George Rimar f28210285b [ELF] - Format. NFC.
llvm-svn: 282395
2016-09-26 11:00:48 +00:00
Eugene Leviant 527a84ec12 Linkerscript: don't crash when GC .debug_line
Differential revision: https://reviews.llvm.org/D24870

llvm-svn: 282393
2016-09-26 09:04:16 +00:00
Eugene Leviant 8287b1bce1 Linkerscript: do not GC non-allocated sections
Differential revision: https://reviews.llvm.org/D24733

llvm-svn: 282391
2016-09-26 08:32:41 +00:00
Davide Italiano c79d196c8e [LTO] Add a test for invalid alias analysis pipelines.
I found out this wasn't tested when looking at Vedant's coverage bot
numbers, so, thanks to him. While I'm here, switch the error message
to be lld-compliant (first letter lowercase).

llvm-svn: 282335
2016-09-24 20:05:52 +00:00
Rui Ueyama 9c4ac5f26c Combine two StringSwitch::Cases because Cases now can take 6 parameters.
llvm-svn: 282307
2016-09-23 22:22:34 +00:00
Rafael Espindola 251e240adc Warn if we can't find the entry symbol.
Fixes pr30465.

llvm-svn: 282295
2016-09-23 21:04:56 +00:00
Rafael Espindola 65499b9040 Avoid counting sections twice.
We were counting the size of the bss section holding common symbols twice:

    Dot += CurOutSec->getSize();
    flush();

The new code is also simpler as now flush is the only function that
inserts in AlreadyOutputOS, which makes sense since the set hold fully
output sections.

llvm-svn: 282285
2016-09-23 20:10:47 +00:00
Davide Italiano 03f7005375 [ELF] Resolve weak undefined TLS symbols when no phdr is available.
If we pass --gc-sections to lld and .tbss is not referenced,
the section is reclaimed and lld doesn't create a TLS program header.
R_TLS tries to access the program header -> lld crashes.
Mimic what bfd/gold do in this case and resolve a weak undefined
TLS symbol to the base of the TLS block, i.e. give it a value of zero.

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

llvm-svn: 282279
2016-09-23 18:47:50 +00:00
Rui Ueyama 0120e3f278 Simplify. NFC.
llvm-svn: 282268
2016-09-23 18:06:51 +00:00
Rafael Espindola cfdd1fdfb6 Fix test when run in unusual test directories.
It would fail for example if the directory contained an @. Both gold
and bfd require quotes for @, so just change the test to quote the
directory name.

Thanks to Vedant Kumar for the bug report.

llvm-svn: 282256
2016-09-23 15:34:53 +00:00
Peter Smith fde6213fe8 [ARM] ARM TLS shouldn't use relaxations
The ARM TLS relocations are placed on literal data and not the 
code-sequence, it is therefore not possible to implement the relaxTls* 
functions. This change updates handleMipsTlsRelocation() to
handleNoRelaxTlsRelocation() and incorporates ARM as well as Mips.

The ARM support in handleNoRelaxTlsRelocation() currently needs to ouput
the module index dynamic relocation in all cases as it is relying on the 
dynamic linker to set the module index in the got.

Should address PR30218

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

llvm-svn: 282250
2016-09-23 13:54:48 +00:00
George Rimar 4ebc562052 [ELF] - Linkerscript: accept space between '=' and expression in section filler.
Previously we failed to parse next scripts because disallowed
a space between filler value and '=':

  .text : {
...
  } :text = 0x9090

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

llvm-svn: 282248
2016-09-23 13:29:20 +00:00
George Rimar f34f45fd53 [ELF] - Linkerscript: implement DEFINED() command.
DEFINED(symbol)
Return 1 if symbol is in the linker global symbol table and is defined before
the statement using DEFINED in the script, otherwise return 0.

Can be used to define default values for symbols. Found it in the wild.

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

llvm-svn: 282245
2016-09-23 13:17:23 +00:00
Eugene Leviant d4dea164dd Linker script: fix crash when discarding section
If section contains local symbols ldd crashes, because local
symbols are added to symbol table before section is discarded
by linker script processor. This patch calls copyLocalSymbols()
after createSections, so discarded section symbols are not copied

llvm-svn: 282244
2016-09-23 13:17:16 +00:00
George Rimar c8ccd1f1c5 [ELF] - Linkerscript: Implemented >> and <<
Found this operators used in the wild scripts, for example:

__got2_entries = (_FIXUP_TABLE_ - _GOT2_TABLE_) >>2;
__fixup_entries = (. - _FIXUP_TABLE_)>>2;

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

llvm-svn: 282243
2016-09-23 13:13:55 +00:00
Davide Italiano 49ab2517f5 [ELF/GC] Don't crash while processing Discarded sections.
The ELF spec doesn't allow relocations to point directly to
a deduplicated COMDAT section but this unfortunately happens in
practice. Bail out early instead of crashing.

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

llvm-svn: 282197
2016-09-22 21:08:51 +00:00
Vedant Kumar 387eb83a50 [lld] Add llvm-pdbdump as a test dependency
Reviewed-by: rafael
llvm-svn: 282187
2016-09-22 20:12:23 +00:00
Rafael Espindola 29c59cc15d Remove extra \
Should fix the windows bots.

llvm-svn: 282181
2016-09-22 18:36:46 +00:00
Rafael Espindola 15c579518d Don't move orphan sections past assignments.
This fixes a case where we would produce an unaligned PT_LOAD.

llvm-svn: 282180
2016-09-22 18:05:49 +00:00
Rafael Espindola 1998ee53f0 Simplify. NFC.
With the recent changes there should always be a 1:1 correspondence in
the correct order between OutputSections and OutputSectionCommands.

llvm-svn: 282176
2016-09-22 17:23:53 +00:00
Rafael Espindola 0d4b6d5c98 Avoid duplicated code.
This also fixes the linker script accounting for the ELF header in
some places but not in others.

llvm-svn: 282173
2016-09-22 16:47:21 +00:00
Rafael Espindola 2644208381 Simplify. NFC.
It doesn't matter which direction we rotate and we haven't really
started optimizing the linker script code, so keep this simple.

llvm-svn: 282166
2016-09-22 15:25:21 +00:00
Rafael Espindola 9546fffbfe Handle empty sections with symbol assignments.
Before the symbols were becoming undefined.

llvm-svn: 282159
2016-09-22 14:40:50 +00:00
Rafael Espindola a940e5396b Fix VA computation for tbss.
llvm-svn: 282149
2016-09-22 12:35:44 +00:00
Rafael Espindola 7252ae52cf Handle multiple .tbss sections.
llvm-svn: 282147
2016-09-22 12:00:08 +00:00
Rafael Espindola 24c073d1bb Once more unto the strict weak ordering, once more.
This should finally give a stable sorting over all implementations.

llvm-svn: 282118
2016-09-21 22:36:19 +00:00
Rafael Espindola e746e52c7b Implement ONLY_IF_RO/ONLY_IF_RW like bfd.
The actual logic is to keep the output section if the output section
would have been ro/rw.

This is both simpler and more practical, as the intention is linker
scripts is to always keep of of a pair of ONLY_IF_RO/ONLY_IF_RW.

llvm-svn: 282099
2016-09-21 18:33:44 +00:00
George Rimar 07171f21d1 [ELF] - Linkerscript: support complex section pattern grammar.
This is PR30442.
Previously we were failed to parce complex expressions like:
foo : { *(SORT_BY_NAME(bar) zed) }

Main idea of patch that globs and excludes can be wrapped in a SORT.
There is a difference in semanics of ld/gold:
ld likes:
*(SORT(EXCLUDE_FILE (*file1.o) .foo.1))

gold likes:
*(EXCLUDE_FILE (*file1.o) SORT(.foo.1))

Patch implements ld grammar, complex expressions like 
next is not a problem anymore:
.abc : { *(SORT(.foo.* EXCLUDE_FILE (*file1.o) .bar.*) .bar.*) }


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

llvm-svn: 282078
2016-09-21 15:56:44 +00:00
Eugene Leviant 2506cb4ddb Linker script: Fix bug with several .bss
When final image has several .bss sections, lld fails
because second .bss always has zero VA. This causes 
link error "Not enough space for ELF and program headers"

llvm-svn: 282067
2016-09-21 11:29:28 +00:00
George Rimar 601e989879 [ELF] - Linkerscript: reimplement readSectionExcludes()
It is not only a bit more straightforward now, but also next 2 issues are solved:

* It just crashed on ".foo : { *(EXCLUDE_FILE (*file1.o)) }" before.
* It accepted multiple EXCLUDE_FILEs in a row.

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

llvm-svn: 282060
2016-09-21 08:53:21 +00:00
Rui Ueyama c75ef85e84 Accept sh_entsize = 0.
This surfaced again with Rust. As per bug 30435, rustc creates a
mergeable section with a sh_entsize zero. It bit us before, too.
I think we should relax the input check rather than being too picky.

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

llvm-svn: 282049
2016-09-21 03:22:18 +00:00
Rafael Espindola b6b8f6c308 Revert "Revert "Only restrict order if both sections are in the script.""
This reverts commit r282021, bringing back r282015.

The problem was that the comparison function was not a strict weak
ordering anymore, which this patch fixes.

Original message:

Only restrict order if both sections are in the script.

This matches gold and bfd behavior and is required to handle some scripts.

The script has to assume where PT_LOADs start in order to align that
spot. If we don't allow section it doesn't know about to move to the
middle, we can need more PT_LOADs and those will not be aligned.

llvm-svn: 282035
2016-09-20 22:43:15 +00:00
Rafael Espindola bf04708e11 Revert "Only restrict order if both sections are in the script."
This reverts commit r282015. It broke some bots.

llvm-svn: 282021
2016-09-20 21:28:19 +00:00
Rafael Espindola 145569df64 Only restrict order if both sections are in the script.
This matches gold and bfd behavior and is required to handle some scripts.

The script has to assume where PT_LOADs start in order to align that
spot. If we don't allow section it doesn't know about to move to the
middle, we can need more PT_LOADs and those will not be aligned.

llvm-svn: 282015
2016-09-20 20:54:39 +00:00
Rafael Espindola e5bfa690d0 Make test less dependent on the section order.
This means either relaxing CHECKs or listing more sections and
addresses in linker scripts.

llvm-svn: 282014
2016-09-20 20:48:54 +00:00
Rui Ueyama ee92470969 Simplify SORT and --sort-section command line option handling.
Differential Revision: https://reviews.llvm.org/D24685

llvm-svn: 282006
2016-09-20 19:42:41 +00:00
Rafael Espindola db62cbb97f Don't produce an error for undefined entry symbol.
This is particularly important when the symbol comes from a linker
script. It is common to use the same linker script for shared
libraries and executables. Without this we would always fail to link
shared libraries with -z,defs and a linker script with an ENTRY
directive.

llvm-svn: 281989
2016-09-20 17:14:16 +00:00
Rafael Espindola c8e7c98b66 Redirect stderr to /dev/null. NFC.
Makes the error output less confusing when this test fails.

llvm-svn: 281985
2016-09-20 17:02:38 +00:00
Davide Italiano 8f41f33f45 [Writer] Fix a typo. NFC.
llvm-svn: 281984
2016-09-20 16:57:02 +00:00
Rafael Espindola b45fd70ef1 Disable --rosegment when we have linker scripts.
Linker scripts are responsible for aliging '.'. Since they are
designed for bfd which has no --rosegment, they don't align the RO to
RX transition.

llvm-svn: 281978
2016-09-20 15:22:27 +00:00
Rafael Espindola 1b87995f31 Make tests less dependent on the exact layout.
In most cases that means just not checking the address when we don't
need it.

For some tests it is easier to just set . to a known value.

llvm-svn: 281976
2016-09-20 15:08:24 +00:00
Rafael Espindola 49913bc63d Move test to the correct directory. NFC.
llvm-svn: 281974
2016-09-20 14:49:18 +00:00
Rafael Espindola 6d38e4dbe1 Remove empty section commands.
We were already not creating them, and with this other parts of the
code don't have to worry about them.

llvm-svn: 281968
2016-09-20 13:12:07 +00:00
Rui Ueyama d1d7cfcd52 Remove a use of std::list.
llvm-svn: 281951
2016-09-20 00:02:06 +00:00
Davide Italiano 1c3c3953c6 [GC] Don't crash when printing the special Discarded GC section.
InputSection<ELFT>::Discarded has no name and it's not backed by
a file. Trying to report it as discared will cause a nullptr
dereference, therefore a crash. Skip it.

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

llvm-svn: 281946
2016-09-19 23:15:51 +00:00
Rafael Espindola caa6024303 Use FileCheck variables. NFC.
llvm-svn: 281932
2016-09-19 20:23:19 +00:00
Rafael Espindola 7d38273636 Map .data.rel.ro correctly.
An input section named .data.rel.ro now maps to an output section
named .data.rel.ro. Before we were mapping it to .data.

llvm-svn: 281931
2016-09-19 19:59:21 +00:00
Rafael Espindola 3d90318aff Make this test a bit stricter.
By making every section on byte and checking the size it now shows how
many sections were concatenated into each output section.

llvm-svn: 281930
2016-09-19 19:56:51 +00:00
Rafael Espindola 881f12ee0b Don't CHECK the addresses. NFC
That is not what this test is about. This reduces changes in another
patch that changes layout.

llvm-svn: 281929
2016-09-19 18:58:30 +00:00
Davide Italiano 6f862b1f07 [Writer] Rename variables to reflect reality. NFCI.
The InputSection variables in the Writer were named `C`. This
was because when the ELF linker was ported (from COFF)
the name `Chunks` for input sections was retained.
Luckily we switched to a more ELF-compliant jargon, but these
variables weren't reanamed accordingly during the transition.

llvm-svn: 281917
2016-09-19 17:38:39 +00:00
Rafael Espindola f135f0ec1d Remove unnecessary const_canst. NFC.
llvm-svn: 281901
2016-09-19 13:33:38 +00:00
George Rimar b31dd37005 [ELF] - LinkerScript: Add workaround for gcc 6.2.0 failure w/auto
Will Dietz found and reported that lld does not compile with gcc 6.2.0,
more details https://llvm.org/bugs/show_bug.cgi?id=30438

And confirmed this change fixes the issue.

llvm-svn: 281900
2016-09-19 13:27:31 +00:00
George Rimar 194470cd11 [ELF] - Fix comment. NFC.
llvm-svn: 281836
2016-09-17 19:21:05 +00:00
George Rimar af03be19f9 [ELF] - Added comments. NFC.
llvm-svn: 281835
2016-09-17 19:17:25 +00:00
George Rimar 331b9ae196 [ELF] - Linkerscript: change locationcounter.s to use llvm-objdump
Previously it used llvm-readobj -s, now changed to llvm-objdump -section-headers,
that reduced the output significantly and helps to read/support it.

llvm-svn: 281832
2016-09-17 18:35:24 +00:00
George Rimar 8c658bf824 [ELF] - SEGMENT_START's default argument can be an expression
Our implementation supported integer value previously.
ld can use expression,
for example, it is OK to write
 . = SEGMENT_START("foobar", .);

Patch implements that.

llvm-svn: 281831
2016-09-17 18:14:56 +00:00
George Rimar dfbbbc86a1 [ELF] Linkerscript: fixed bug about commands processing.
It was possible situation about some commands just were not processed
(were skipped) because of a bug appeared when constraint checking used.

Testcase is attached.

llvm-svn: 281818
2016-09-17 09:50:10 +00:00
George Rimar 8034d49db5 [ELF] - Fixed mistypes in comments. NFC.
llvm-svn: 281817
2016-09-17 07:31:49 +00:00
Rui Ueyama 70efa2f251 Attempt to fix buildbot.
llvm-svn: 281805
2016-09-17 02:34:50 +00:00
Rui Ueyama 4dc07becd0 Use named struct instead of unnamed std::pair.
It is important to give members names for readability.

llvm-svn: 281803
2016-09-17 02:23:40 +00:00
Rui Ueyama 3ff27f49cd Define a versatile utility function and use it instead of a single purpose one.
llvm-svn: 281802
2016-09-17 02:15:28 +00:00
Rui Ueyama 027a9e8787 Remove unnecessary namespace specifiers.
llvm-svn: 281801
2016-09-17 02:10:15 +00:00
Rui Ueyama bb542b3f0c Partially fill Info stream with proper values.
llvm-svn: 281795
2016-09-16 22:51:17 +00:00
Rafael Espindola 373343bd5b Try to fix a few bots.
llvm-svn: 281794
2016-09-16 22:47:34 +00:00
Rafael Espindola aab6d5c52a Put SHF_ALLOC sections first, even with linker scripts.
This matches gold and bfd, and is pretty much required by some linker
scripts. They end with commands like

foo   0 : { *(bar) }

if we put any SHF_ALLOC sections after they can have an address that
is too low.

llvm-svn: 281778
2016-09-16 21:29:07 +00:00
Rui Ueyama b2a0abdf0e Rename SortSectionPolicy::IgnoreConfig to None.
Because it corresponds to SORT_NONE. None was renamed Default.

llvm-svn: 281776
2016-09-16 21:14:55 +00:00
Rafael Espindola 7c3ff2eb58 Only process commands in a ONLY_IF_RO if it matches.
This matches bfd behavior. It also makes future changes simpler as we
don't have to worry about ignoring these commands in multiple places

llvm-svn: 281775
2016-09-16 21:05:36 +00:00
Rafael Espindola e71a3f8ae1 Fix constraint checking in ONLY_IF_RO.
We have to look at all the relevant input sections at once.

llvm-svn: 281772
2016-09-16 20:34:02 +00:00
George Rimar be394db376 [ELF] - Implemented --sort-section cmd line option and SORT_NONE script command.
This fixes Bug 30385 - SORT_NONE not implemented,

`SORT_NONE' disables section sorting by ignoring the command line
section sorting option.

That is why this patch also implements --sort-section option.

Description of sorting rules
available at https://sourceware.org/binutils/docs/ld/Input-Section-Wildcards.html 

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

llvm-svn: 281771
2016-09-16 20:21:55 +00:00
George Rimar 395281cfc3 Recommit r281721 "[ELF] - Linkerscript: implement EXCLUDE_FILE in the middle of a input section description."
With fix for 2 bots. Details about the fix performed is on a review page.

Initial commit message:
This is PR30387:

From PR description:
We fail to parse

SECTIONS
{
  foo :
  {
    *(sec0 EXCLUDE_FILE (zed1.o) sec1  EXCLUDE_FILE (zed2.o) sec2 )
  }
}
The semantics according to bfd are:

Include sec1 from every file but zed1.o
Include sec2 from every file but zed2.o
Include sec0 from every file

Patch implements the support.

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

llvm-svn: 281754
2016-09-16 17:42:10 +00:00
Eugene Leviant 20d031948e Improve handling ASSERT outside SECTIONS block
Differential revision: https://reviews.llvm.org/D24450

llvm-svn: 281740
2016-09-16 15:30:47 +00:00
Rafael Espindola d31907957a Change how we compute offsets with linker scripts.
This fixes pr30367, but more importantly, it changes how we compute offsets.

Now offset computation in a walk over linker script commands, like the
rest of assignAddresses. IMHO this is simpler to understand and if we
ever have to create multiple outputsections or chunks to change how we
handle test/ELF/linkerscript/alternate-sections.s it should be easier
to do it.

llvm-svn: 281736
2016-09-16 15:10:23 +00:00
Ismail Donmez e7174ffcd8 Fix shared library build
llvm-svn: 281728
2016-09-16 14:10:23 +00:00
George Rimar ceae630c9b Reverted r281721 ("[ELF] - Linkerscript: implement EXCLUDE_FILE in the middle of a input section description.").
It broke build bot:
http://lab.llvm.org:8011/builders/lld-x86_64-darwin13/builds/27508

llvm-svn: 281723
2016-09-16 13:30:18 +00:00
George Rimar 4906c7f5c4 [ELF] - Linkerscript: implement EXCLUDE_FILE in the middle of a input section description.
This is PR30387:

From PR description:
We fail to parse

SECTIONS
{
  foo :
  {
    *(sec0 EXCLUDE_FILE (zed1.o) sec1  EXCLUDE_FILE (zed2.o) sec2 )
  }
}
The semantics according to bfd are:

Include sec1 from every file but zed1.o
Include sec2 from every file but zed2.o
Include sec0 from every file

Patch implements the support.

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

llvm-svn: 281721
2016-09-16 13:07:02 +00:00
Rui Ueyama b28c6d495d Use functions in DebugInfoPDB to create dummy PDB file.
I don't think we are creating valid PDB file here,
but it is okay because we have never created valid PDBs before.

llvm-svn: 281704
2016-09-16 04:32:33 +00:00
Rui Ueyama 2644cd1d88 Demangle symbols in "undefined symbol" error message.
llvm-svn: 281698
2016-09-16 03:52:45 +00:00
Rui Ueyama 783afd667d Check /pdb output using llvm-pdbdump.
test/COFF/rsds.test checks only RSDS directory in a DLL and
didn't check the validity of the PDF file produced.
(Technically the produced PDB is not valid because it is really
a stub, but this test is still good to have.)

llvm-svn: 281678
2016-09-15 23:14:40 +00:00
Rui Ueyama 8e8296fad8 Fix old copy-n-paste errors.
llvm-svn: 281671
2016-09-15 22:26:07 +00:00
Rui Ueyama 1d99ab3925 Create PDB.h and move code to remove unnecessary #includes.
llvm-svn: 281670
2016-09-15 22:24:51 +00:00
Rafael Espindola 4ec013ac83 Error out instead of producing a corrupt PT_LOAD.
What bfd and gold do is give up in putting the headers in the PT_LOAD
and just start the PT_LOAD in the second page.

llvm-svn: 281660
2016-09-15 21:22:11 +00:00
Rafael Espindola 1d98d6292f Make sure we create all necessary PT_LOADs.
We were some times stopping early when using linker scripts.

llvm-svn: 281647
2016-09-15 19:36:01 +00:00
George Rimar 575208cabd [ELF] - Linkerscript: implemented SORT_BY_INIT_PRIORITY.
This is PR30386,

SORT_BY_INIT_PRIORITY is a keyword can be used to sort sections by numerical value of the
GCC init_priority attribute encoded in the section name.

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

llvm-svn: 281646
2016-09-15 19:15:12 +00:00
Rui Ueyama 7f38299919 Use SuperBlock struct defined in MSFCommon.h.
llvm-svn: 281643
2016-09-15 18:55:18 +00:00
Rafael Espindola 3adbbc3891 Check the return of getInteger.
llvm-svn: 281608
2016-09-15 13:36:44 +00:00
George Rimar 31c25ae90a Recommit r281318 "[ELF] - Versionscript: support mangled symbols with the same name."
Previouly bot was failing:
http://lab.llvm.org:8011/builders/clang-with-lto-ubuntu/builds/413/steps/test-stage1-compiler/logs/stdio
Fixed possible segfault, so commit should bix the buildbot.

Initial commit message:

This is PR30312. Info from bug page:

Both of these symbols demangle to abc::abc():
_ZN3abcC1Ev
_ZN3abcC2Ev
(These would be abc's complete object constructor and base object constructor, respectively.)
however with "abc::abc()" in the version script only one of the two receives the symbol version.

Patch fixes that.
It uses testcase created by Ed Maste (D24306).

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

llvm-svn: 281605
2016-09-15 12:44:38 +00:00
Rafael Espindola 97bdc7220b Handle arbitrary expressions in DATA_SEGMENT_RELRO_END.
llvm-svn: 281521
2016-09-14 19:14:01 +00:00
Rafael Espindola 0289c5bbcd Add an interesting test.
llvm-svn: 281515
2016-09-14 18:06:02 +00:00
Rafael Espindola 77af7e48b7 Add a test showing we handle .tbss sections in linker scripts.
llvm-svn: 281496
2016-09-14 16:40:33 +00:00
Simon Atanasyan d10a5ea19e [ELF] Do not adjust TLS symbol value when produce relocatable object
When the linker generates a relocatable object there is no TLS program
header and we should not adjust TLS symbols value.

llvm-svn: 281494
2016-09-14 16:26:19 +00:00
Rafael Espindola be94e1b630 Move helper function higher in the file. NFC
This just makes a followup patch easier to read.

llvm-svn: 281482
2016-09-14 14:32:08 +00:00
George Rimar d73ef1738b [ELF] - Implemented --section-start, -Ttext, -Tdata, -Tbss options.
--section-start=sectionname=org
Locate a section in the output file at the absolute address given by org. 
You may use this option as many times as necessary to locate multiple sections in the command line.
 org must be a single hexadecimal integer; for compatibility with other linkers, 
you may omit the leading `0x' usually associated with hexadecimal values. 
Note: there should be no white space between sectionname, the equals sign (“<=>”), and org.

-Tbss=org
-Tdata=org
-Ttext=org
Same as --section-start, with .bss, .data or .text as the sectionname.

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

llvm-svn: 281458
2016-09-14 13:07:13 +00:00
Rafael Espindola d88d7166a8 Use murmurhash2 instead of fnv.
It is substantially faster by processing 8 bytes at a time.

llvm-svn: 281454
2016-09-14 11:32:57 +00:00
Eugene Leviant e05336ffa1 [ELF] Replace HasContents with HasSections. NFC
llvm-svn: 281449
2016-09-14 08:32:36 +00:00
Rui Ueyama 2d7fe598f2 Turn a no-op assignment into an assertion.
r279456 guarantees that this condition is always satisfied.

llvm-svn: 281426
2016-09-14 00:09:50 +00:00
Rui Ueyama 38dbd3eea9 Simplify InputFile ownership management.
Previously, all input files were owned by the symbol table.
Files were created at various places, such as the Driver, the lazy
symbols, or the bitcode compiler, and the ownership of new files
was transferred to the symbol table using std::unique_ptr.
All input files were then free'd when the symbol table is freed
which is on program exit.

I think we don't have to transfer ownership just to free all
instance at once on exit.

In this patch, all instances are automatically collected to a
vector and freed on exit. In this way, we no longer have to
use std::unique_ptr.

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

llvm-svn: 281425
2016-09-14 00:05:51 +00:00
George Rimar 84ba4ae11d revert: [ELF] - Versionscript: support mangled symbols with the same name.
Something broked BBots:
281318 failed on step 9:
http://lab.llvm.org:8011/builders/clang-with-lto-ubuntu/builds/413

r281317 built step 9 green:
http://lab.llvm.org:8011/builders/clang-with-lto-ubuntu/builds/415

Initial revision commits were:
This is PR30312. Info from bug page:

Both of these symbols demangle to abc::abc():
_ZN3abcC1Ev
_ZN3abcC2Ev
(These would be abc's complete object constructor and base object constructor, respectively.)
however with "abc::abc()" in the version script only one of the two receives the symbol version.

Patch fixes that.
It uses testcase created by Ed Maste (D24306).

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

llvm-svn: 281411
2016-09-13 22:26:12 +00:00
Rui Ueyama 1e2e8eae5d [ELF] Set EF_ARM_EABI_VER5 for ARM
Without this flag set, an AArch64 Linux kernel won't try to load the executable
(even if a 32 bit arm kernel will run the binary just fine).

Patch by Martin Storsjö!

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

llvm-svn: 281394
2016-09-13 20:51:32 +00:00
Rui Ueyama ea26504c19 Split scanVersionScript. NFC.
llvm-svn: 281393
2016-09-13 20:51:30 +00:00
Rui Ueyama fbde710497 Add comments.
llvm-svn: 281388
2016-09-13 20:41:06 +00:00
Rui Ueyama 3e9037d419 Inline small funciton.
llvm-svn: 281381
2016-09-13 19:56:27 +00:00
Rui Ueyama a75b7a48f1 Fix comments.
llvm-svn: 281380
2016-09-13 19:56:25 +00:00
Rafael Espindola 10897f1807 Enable merging of SHF_MERGE sections with linker scripts.
This also fixes the related problem of non SHF_MERGE sections with
different flags not being merged.

Fixes pr30355.

llvm-svn: 281338
2016-09-13 14:23:14 +00:00