Commit Graph

7698 Commits

Author SHA1 Message Date
Rui Ueyama b458841745 Simplify. NFC.
llvm-svn: 290321
2016-12-22 05:22:29 +00:00
Rui Ueyama e50e8071c2 Define a function to avoid a magic variable 0x3.
llvm-svn: 290320
2016-12-22 05:11:12 +00:00
Rui Ueyama 7323b4b788 Remove a typedef that is used only once.
llvm-svn: 290318
2016-12-22 04:40:56 +00:00
Davide Italiano 7116dc908c [ELF/tests] Use cpio -it instead of cpio -t.
OpenBSD's cpio does not accept the -t option without -i.
Apparently some systems implement cpio -t as a shortcut
for cpio -it, the latter is the only thing that's documented.
This change avoids test failures on OpenBSD.

Patch by Mark Kettenis!

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

llvm-svn: 290252
2016-12-21 12:22:19 +00:00
George Rimar d450065308 [ELF] - Linkerscript: Fall back to search paths when INCLUDE not found
From https://sourceware.org/binutils/docs/ld/File-Commands.html:
The file will be searched for in the current directory, and in any 
directory specified with the -L option.

Patch done by Alexander Richardson.

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

llvm-svn: 290247
2016-12-21 09:42:25 +00:00
George Rimar cc9302d0b7 [ELF] - Removed trailing whitespaces. NFC.
llvm-svn: 290243
2016-12-21 08:58:36 +00:00
Rui Ueyama 4f2f50dc64 De-template DefinedSynthetic.
DefinedSynthetic is not created for a real ELF object, so it doesn't
have to be a template function. It has a virtual st_value, which is
either 32 bit or 64 bit, but we can simply use 64 bit.

llvm-svn: 290241
2016-12-21 08:40:09 +00:00
George Rimar dcf5b72e20 [ELF] - Do not call fatal() in Target.cpp, call error() instead.
We probably would want to avoid fatal() if we can in context of librarification,
but for me reason of that patch is to help D27900 go.

D27900 changes errors reporting to something like
error: text1
note: text2
note: text3

where hint used to provide additional information about location. In that case
I can't just call fatal() because user will not see notes after that what adds additional complication to handle.
So It is good to switch fatal() to error() where it is possible.

Also it adds testcase with broken relocation number. 
Previously we did not have any, It checks that error() instead of fatal() works fine.

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

llvm-svn: 290239
2016-12-21 08:21:34 +00:00
George Rimar 4fb6e79c65 [ELF] - Fix use of freed memory.
It was revealed by D27831.

If we have linkerscript that includes another one that sets OUTPUT for example:

RUN: echo "INCLUDE \"foo.script\"" > %t.script
RUN: echo "OUTPUT(\"%t.out\")" > %T/foo.script
then we do:

void ScriptParser::readInclude() {
...
  std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
  tokenize(MB->getMemBufferRef());
  OwningMBs.push_back(std::move(MB));
}

void ScriptParser::readOutput() {
...
    Config->OutputFile = unquote(Tok);
...
}
Problem is that OwningMBs are destroyed after script parser do its job.
So all Toks are dead and Config->OutputFile points to destroyed data.

Patch suggests to save all included scripts into using string Saver.

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

llvm-svn: 290238
2016-12-21 08:11:49 +00:00
Simon Atanasyan 86dc60d8d4 [ELF][MIPS] Allow .MIPS.abiflags larger than one Elf_Mips_ABIFlags struct
Older versions of BFD generate libraries with .MIPS.abiflags that only
concatenate the individual .MIPS.abiflags sections instead of merging.

Patch by Alexander Richardson.

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

llvm-svn: 290237
2016-12-21 05:31:57 +00:00
Vitaly Buka 0b7de06a23 Fix build broken by changes in StringMatcher interface r290213
llvm-svn: 290231
2016-12-21 02:27:14 +00:00
Rui Ueyama 6e3595d6c5 Move a function defintion to make it static.
llvm-svn: 290215
2016-12-21 00:05:39 +00:00
Rui Ueyama e52614b5c7 Use ArrayRef instead of `const std::vector`.
llvm-svn: 290213
2016-12-20 23:17:00 +00:00
Rui Ueyama f7a7ab59af Move GlobPattern class from LLD to llvm/Support.
GlobPattern is a class to handle glob pattern matching. Currently
only LLD is using that, but technically that feature is not specific
to linkers, so in this patch I move that file to LLVM.

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

llvm-svn: 290212
2016-12-20 23:09:09 +00:00
Simon Atanasyan 8f7d28122c [ELF] Do not warn about missed entry symbol in case of relocatable output
Relocatable file does not need to have an entry symbol so the warning is
useless in that case.

llvm-svn: 290206
2016-12-20 22:24:45 +00:00
George Rimar d003c7f394 [ELF] - Treat .openbsd.randomdata as relro section
That was requested by Mark Kettenis in llvm-dev:

"It is the intention that .openbsd.randomdata sections are made
read-only after initialization. The native (ld.bfd based) OpenBSD
toolchain accomplishes this by including .openbsd.randomdata into the
PT_GNU_RELRO segment."

He suggested code change, I added testcase.

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

llvm-svn: 290174
2016-12-20 11:28:54 +00:00
Rui Ueyama c207a89c91 Remove `Compressed` member from InputSectionData.
This value is used only once, and we can compute a value.
So we don't need to save it.

llvm-svn: 290164
2016-12-20 05:47:55 +00:00
Rui Ueyama 0be9720875 Fix a bug that the glob pattern parser falls in an infinite loop for invalid patterns.
llvm-svn: 290160
2016-12-20 03:12:28 +00:00
Rui Ueyama 398f55f6f6 Set Config->ColorDiagnostics early so that all error messages are colored.
Previously, some errors that were checked before we set to
Config->ColorDiagnostics weren't colored. This patch moves the code
to set the variable so that such error messages are colored just like
other error messages.

llvm-svn: 290157
2016-12-20 02:17:24 +00:00
Rui Ueyama 312703116f Simplify type of Config->SymbolOrderingFile.
That variable was of type DenseMap<StringRef, unsigned>, but the
unsigned numbers needed to be monotonicly increasing numbers because
the implementation that used the variable depended on that fact.
That was an implementation detail and shouldn't have leaked into Config.

This patch simplifies its type to std::vector<StringRef>.

llvm-svn: 290151
2016-12-20 01:51:08 +00:00
Rui Ueyama a8529e2cb7 Inline a small function.
llvm-svn: 290150
2016-12-20 01:34:31 +00:00
Rafael Espindola 5967c97323 Fix corner cases of setting the section address.
This handles all the corner cases if setting a section address:

- If the address is too low, we cannot allocate the program headers.
- If the load address is lowered, we have to do that before finalize

This also shares some code with the linker script since it was already
hitting similar cases.

This is used by the freebsd boot loader. It is not clear if we need to
support this with a non binary output, but it is not as bad as I was
expecting.

llvm-svn: 290136
2016-12-19 21:21:07 +00:00
George Rimar 2bb88ab5e0 [ELF] - Implemented --retain-symbols-file option
--retain-symbols-file=filename
Retain only the symbols listed in the file filename, discarding all others. 
filename is simply a flat file, with one symbol name per line. This option 
is especially useful in environments (such as VxWorks) where a large global 
symbol table is accumulated gradually, to conserve run-time memory.

Note: though documentation says "--retain-symbols-file does not discard 
undefined symbols, or symbols needed for relocations.", both bfd and gold 
do that, and this patch too, like testcase show.

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

llvm-svn: 290122
2016-12-19 18:00:52 +00:00
Rafael Espindola 17cb7c0a2a Detemplate PhdrEntry. NFC.
llvm-svn: 290115
2016-12-19 17:01:01 +00:00
Rafael Espindola 29982b0f6b Use existing variable. NFC.
llvm-svn: 290112
2016-12-19 16:50:20 +00:00
Adhemerval Zanella 86513f06be ELF/AArch64: Fix dynamic relocation against local symbol in shared objects
AArch64 TLSDESC for local symbol in shared objects are implemented in a
arch specific manner where the TLSDESC dynamic relocation addend is the
symbol VM inside the TLS block. For instance, with a shared library
created from the code:

--
static __thread int32_t x1;
static __thread int64_t x2;

int32_t foo1 (int32_t x)
{
  x1 += x;
  return x;
}

int64_t foo2 (int64_t x)
{
  x2 += x;
  return x;
}
--

The dynamic relocation should be create as:

Relocations [
  Section (N) .rela.dyn {
    <Address1> R_AARCH64_TLSDESC - 0x0
    <Address2> R_AARCH64_TLSDESC - 0x8
  }
]

Where 0x0 addend in first dynamic relocation is the address of 'x1'
in TLS block and '0x8' is the address of 'x2'.

Checked against test-suite on aarch64-linux-gnu.

llvm-svn: 290099
2016-12-19 11:58:01 +00:00
Rui Ueyama 8f687f71fb Remove inappropriate use of CachedHashStringRef.
Use of CachedHashStringRef makes sense only when we reuse hash values.
Sprinkling it to all DenseMap has no benefits and just complicates data types.
Basically we shouldn't use CachedHashStringRef unless there is a strong
reason to to do so.

llvm-svn: 290076
2016-12-19 03:14:16 +00:00
Rui Ueyama 9381eb1045 Remove lld/Support/Memory.h.
I thought for a while about how to remove it, but it looks like we
can just copy the file for now. Of course I'm not happy about that,
but it's just less than 50 lines of code, and we already have
duplicate code in Error.h and some other places. I want to solve
them all at once later.

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

llvm-svn: 290062
2016-12-18 14:06:06 +00:00
George Rimar 607fa711b2 [ELF] - Use DWARFDebugPubTable parser class intead of hand-written parsing.
DWARFDebugPubTable was introduced recently and allows us to get rid of
code duplication in LLD.

llvm-svn: 290042
2016-12-17 10:18:05 +00:00
Zachary Turner b5381eef6f Fix compiler warning.
Differential Revision: https://reviews.llvm.org/D27860

llvm-svn: 290002
2016-12-16 23:12:58 +00:00
Rui Ueyama 5d804dc8f7 [ELF] - Linkerscript: Implement two argument version of ALIGN()
Fixes http://llvm.org/PR31129

Patch by Alexander Richardson!

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

llvm-svn: 289968
2016-12-16 18:19:35 +00:00
George Rimar b86448c669 [ELF] - Accept --sort-section=xxx command form.
--sort-section=xxx is the same as --sort-section xxx,
was found in one of FreeBSD ports.

llvm-svn: 289938
2016-12-16 11:59:52 +00:00
Peter Collingbourne c1ded7dcef COFF: Cache the result of library searches.
File system operations were still dominating the profile on Windows. In this
case we were spending a significant amount of our time repeatedly searching
for libraries as a result of processing linker directives. Address this
by caching whether we have already found a library with a given name. For
chrome_child.dll:

Before: 10.53s
After: 6.88s

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

llvm-svn: 289915
2016-12-16 03:45:59 +00:00
George Rimar 6cbfce7785 [ELF] - Make LLD accept Ttext-segment X/Ttext-segment=X aliases for -Ttext.
It os used in work/emulators/qemu-user-static port.
Which tries to use -Ttext-segment and then:

# In case ld does not support -Ttext-segment, edit the default linker
# script via sed to set the .text start addr.  This is needed on FreeBSD
# at least.
<here it calls -verbose to extract and edit default bfd linker script.>

Actually now we are do not fully support -Ttext properly (see D27613),
but we also seems never will provide anything close to default script, like bfd do,
so at least this patch introduces proper alias handling.

llvm-svn: 289827
2016-12-15 16:12:34 +00:00
George Rimar 879a657680 [ELF] - Apply format (2). NFC.
llvm-svn: 289824
2016-12-15 15:38:58 +00:00
George Rimar 93c64025fc [ELF] - Apply format. NFC.
llvm-svn: 289823
2016-12-15 15:38:09 +00:00
George Rimar ec02b8d4c0 [ELF] - Partial support of --gdb-index command line option (Part 3).
Patch continues work started in D24706 and D25821.

in this patch symbol table and constant pool areas were
added to .gdb_index section output.

This one finishes the implementation of --gdb-index functionality in LLD.

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

llvm-svn: 289810
2016-12-15 12:07:53 +00:00
George Rimar 232d11cb54 [ELF] - Attempt to fix ubuntu 64x buildbot (2).
Fixed inaccurate member type: uint32_t -> size_t
(http://lab.llvm.org:8011/builders/llvm-clang-lld-x86_64-scei-ps4-ubuntu-fast/builds/2984/steps/build/logs/stdio).

llvm-svn: 289796
2016-12-15 09:59:18 +00:00
George Rimar aff2530cf8 [ELF] - Attempt to fix ubuntu bot.
(http://lab.llvm.org:8011/builders/llvm-clang-lld-x86_64-scei-ps4-ubuntu-fast/builds/2982)

llvm-svn: 289792
2016-12-15 09:30:07 +00:00
George Rimar 8b54739328 [ELF] - Partial support of --gdb-index command line option (Part 2).
Patch continues work started in D24706,

in this patch address area was added to .gdb_index section output.

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

llvm-svn: 289790
2016-12-15 09:08:13 +00:00
George Rimar 14460e0216 [ELF] - Do not crash when move location counter backward.
PR31335 shows that we do that in next case:
SECTIONS { .text 0x2000 : {. = 0x100 ; *(.text) } }

though documentations says that "If . is used inside a section 
description however, it refers to the byte offset from the start
of that section, not an absolute address. " looks does not work 
as documented in bfd (as mentioned in comments for PR31335).

Until we find out the expected behavior was suggested at least not
to 'crash', what we do after trying to generate huge file.

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

llvm-svn: 289782
2016-12-15 07:27:28 +00:00
Peter Collingbourne 6ee0b4e9f5 COFF: Open and map input files asynchronously on Windows.
Profiling revealed that the majority of lld's execution time on Windows was
spent opening and mapping input files. We can reduce this cost significantly
by performing these operations asynchronously.

This change introduces a queue for all operations on input file data. When
we discover that we need to load a file (for example, when we find a lazy
archive for an undefined symbol, or when we read a linker directive to
load a file from disk), the file operation is launched using a future and
the symbol resolution operation is enqueued.  This implies another change
to symbol resolution semantics, but it seems to be harmless ("ninja All"
in Chromium still succeeds).

To measure the perf impact of this change I linked Chromium's chrome_child.dll
with both thin and fat archives.

Thin archives:

Before (median of 5 runs): 19.50s
After: 10.93s

Fat archives:

Before: 12.00s
After: 9.90s

On Linux I found that doing this asynchronously had a negative effect on
performance, probably because the cost of mapping a file is small enough that
it becomes outweighed by the cost of managing the futures. So on non-Windows
platforms I use the deferred execution strategy.

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

llvm-svn: 289760
2016-12-15 04:02:23 +00:00
Rui Ueyama fd7ed23ee7 Rename functions as per post commit review for r289072.
llvm-svn: 289758
2016-12-15 03:31:53 +00:00
Sean Silva 902ae3cb3f Rename this variable.
`SC` didn't make much sense. We don't seem to have a clear convention,
but `IS` sounds good here because it emphasizes that it is an input
section (this is one place in the code where we are dealing with both
input sections and output sections at the same time so that extra
emphasis makes it a bit clearer).

llvm-svn: 289748
2016-12-15 00:57:53 +00:00
Peter Collingbourne c4e9482164 COFF: We no longer require lib.exe to test DLL exports.
llvm-svn: 289745
2016-12-15 00:11:17 +00:00
Sean Silva 7608626631 Rename InputSection.cpp:getSymVA to getRelocTargetVA.
This name was really confusing because there is also another static
helper Symbols.cpp:getSymVA which has the same name.

llvm-svn: 289733
2016-12-14 22:45:52 +00:00
Davide Italiano 19d647b667 [LTO] Catch up with llvm. Modules without DL are now considered invalid.
llvm-svn: 289722
2016-12-14 21:58:26 +00:00
Michal Gorny 906534fe4e Remove unnecessary llvm/Config/config.h includes
Remove the includes of <llvm/Config/config.h> private LLVM header.
The relevant files seem not to use any definitions from that file,
and it is not available when building against installed LLVM.

The use in lib/ReaderWriter/MachO/MachOLinkingContext.cpp originates
from rL218718, and the use in ELF/Strings.cpp from rL274804 (where it
was moved from Symbols.cpp). In both cases, they were added as a part of
demangling support, and they provided HAVE_CXXABI_H.

Since we are now using the LLVM demangler library instead, the code was
removed and the includes and no longer necessary.

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

llvm-svn: 289707
2016-12-14 20:53:57 +00:00
Peter Smith 532bc984f5 [ELF] Accept first SHT_ARM_ATTRIBUTES section
The eglibc library, as used by Ubuntu 14.04 requires the presence of an
SHT_ARM_ATTRIBUTES section in for the purposes of checking hard/soft float
compatibility when dlopen() is used. Unfortunately when the section is not
present dlopen() fails with a generic could not find file message.
    
This change makes lld keep the first .ARM.attributes section that it
encounters and propagates it to the output. This is not a complete
SHT_ARM_ATTRIBUTES implementation, that would involve reading the contents
of the section and joining each individual attribute. It should suffice
for a homogenous build all libraries and executables on the same system
with a compatible set of command line options.

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

llvm-svn: 289642
2016-12-14 10:36:12 +00:00
Rafael Espindola 09d5daab49 Refactor duplicated expression. NFC.
llvm-svn: 289550
2016-12-13 16:59:19 +00:00
Peter Smith d92099991b [ELF] Add R_ARM_RELATIVE to relocations that can be applied to GotSection
When compiling -fpie and linking with the --pie option the R_ARM_GOTBREL
relocation to D is resolved by writing the value of D into the .got slot
and emitting an R_ARM_RELATIVE relocation for it.
    
This changes adds the R_ARM_RELATIVE relocation to the switch in
relocateOne() so we can process the GotSection relocation to write the
value of the variable as well as emitting the dynamic relocation.

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

llvm-svn: 289527
2016-12-13 10:42:05 +00:00
Rafael Espindola 5f51ed1ca7 Simplify the test. NFC.
llvm-svn: 289499
2016-12-12 23:52:21 +00:00
Simon Atanasyan 2903a8a11f [ELF][MIPS] Calculate default _gp value relative to the GPREL section with the lowest address
llvm-svn: 289471
2016-12-12 21:34:11 +00:00
Peter Collingbourne e50f4854c2 COFF: Fix memory leaks reported by lsan.
llvm-svn: 289451
2016-12-12 18:42:09 +00:00
Simon Atanasyan b9666655c2 [ELF][MIPS] Fix .MIPS.options ri_gp_value on MIPS64
The VA of _gp was being truncated to 32 bits when calling getVa(), but
for 64bit MIPS we need to write a 64 bit value to .MIPS.options.

Patch by Alexander Richardson.

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

llvm-svn: 289432
2016-12-12 14:30:18 +00:00
Saleem Abdulrasool f1af26ddf2 build: add support for standalone lld build
Enable building lld as a standalone project. This is motivated by the desire to
package lld for inclusion in a linux distribution. This allows building lld
against an existing paired llvm installation. Now that lld is usable on x86_64,
it makes sense to revive this configuration to allow distributions to package
it.

llvm-svn: 289421
2016-12-12 05:47:40 +00:00
Peter Collingbourne 4b5a7ac831 COFF: Remove an unused mutex declaration.
llvm-svn: 289415
2016-12-12 03:16:18 +00:00
Peter Collingbourne 8155dfa8f3 COFF: Use a DenseSet instead of a map to atomic_flag to track which archive members have been read.
Differential Revision: https://reviews.llvm.org/D27667

llvm-svn: 289414
2016-12-12 03:16:14 +00:00
Peter Collingbourne 3f530938f6 COFF: Use CachedHashStringRef in the symbol table.
This resulted in about a 1% perf improvement linking chrome_child.dll.

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

llvm-svn: 289410
2016-12-11 22:15:30 +00:00
Peter Collingbourne 8b65e51b73 COFF: Load inputs immediately instead of adding them to a queue.
This patch replaces the symbol table's object and archive queues, as well as
the convergent loop in the linker driver, with a design more similar to the
ELF linker where symbol resolution directly causes input files to be added to
the link, including input files arising from linker directives. Effectively
this removes the last vestiges of the old parallel input file loader.

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

llvm-svn: 289409
2016-12-11 22:15:25 +00:00
Peter Collingbourne 99111287fc COFF: Use a bit in SymbolBody to track which symbols are written to the symbol table.
Using a set here caused us to take about 1 second longer to write the symbol
table when linking chrome_child.dll. With this I consistently get better
performance on Windows with the new symbol table.

Before r289280 and with r289183 reverted (median of 5 runs): 17.65s
After this change: 17.33s

On Linux things look even better:

Before: 10.700480444s
After: 5.735681610s

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

llvm-svn: 289408
2016-12-11 22:15:20 +00:00
Sanjoy Das fd4016b7c7 Fix TBAA metadata
The existing tbaa metadata in the test is ill-formed, and fails the
verifier after r289402.

llvm-svn: 289405
2016-12-11 21:10:11 +00:00
Rui Ueyama 327705d17d Create a TPI stream only when /debugpdb is given.
Adding type records to TPI stream is too time consuming.
It is reported that linking chrome_child.dll took 5 minutes.

llvm-svn: 289330
2016-12-10 17:23:23 +00:00
Ismail Donmez 85588fa2b0 Fix shared library build
llvm-svn: 289320
2016-12-10 09:29:47 +00:00
Rui Ueyama 86581e496b Rename find(SymbolVersion) to avoid overloading with find(StringRef).
llvm-svn: 289293
2016-12-10 00:34:06 +00:00
Peter Collingbourne 6baa5d276d COFF: Fix test failure on Windows.
llvm-svn: 289289
2016-12-09 23:34:49 +00:00
Rafael Espindola dd9dd488fc Fix a bogus warning.
We first decide that the symbol is global, than that it should have
version foo. Since it was already not the default version, we were
producing a bogus warning.

llvm-svn: 289284
2016-12-09 22:40:49 +00:00
Peter Collingbourne 79a5e6b1b7 COFF: New symbol table design.
This ports the ELF linker's symbol table design, introduced in r268178,
to the COFF linker.

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

llvm-svn: 289280
2016-12-09 21:55:24 +00:00
Meador Inge b06147db0c [ELF] Fix test case thinko from r289152
It was pointed out in a post-commit review that the tests
were structured oddly.  Fixed thusly.

llvm-svn: 289278
2016-12-09 21:51:37 +00:00
Rafael Espindola 157c51dce1 Add support for R_X86_64_GOT64.
llvm-svn: 289277
2016-12-09 21:46:39 +00:00
Hans Wennborg 1818e65aad COFF: Support both /lldmap and /lldmap:<file>
The former option bases the filename on the output name, e.g. if the
link output is a.exe, the map will be written to a.map. This matches the
behaviour of link.exe's /MAP option and is useful for creating a map
file of each executable when building a large project.

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

llvm-svn: 289271
2016-12-09 20:54:44 +00:00
Peter Collingbourne fa7f393714 COFF: Move a call to toString() out of the hot path.
Profiling revealed that we were spending 5% of our time linking
chrome_child.dll just in this call to toString().

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

llvm-svn: 289270
2016-12-09 20:51:33 +00:00
Rui Ueyama df41b13b09 Remove `REQUIRES: shell` hack to workaround an echo issue.
These tests are disabled on Windows, but they seem to work just fine now,
so I'll enable them.

llvm-svn: 289251
2016-12-09 18:49:37 +00:00
Rafael Espindola 1ef90d2f25 Fix parsing when one extern follows another.
llvm-svn: 289224
2016-12-09 16:44:05 +00:00
Rafael Espindola 49cd09337c Don't crash on an extra symbol in a version script.
llvm-svn: 289217
2016-12-09 15:08:40 +00:00
Rafael Espindola 6239ce6889 Give preempting symbols precedence over linker script.
llvm-svn: 289212
2016-12-09 14:12:02 +00:00
Peter Smith 4b360292ed [ELF][I386] .got.plt entries for i386 should use VA of ifunc resolver
The i386 glibc ld.so expects the .got.slot entry that is relocated by a 
R_386_IRELATIVE relocation to point directly at the ifunc resolver and
not the address of the PLT entry + 6 (thus entering the lazy resolver).
This is also the case for ARM and I suspect it is because these use REL
relocations and can't use the addend field to store the address of the
ifunc resolver. If the lazy resolver is used we get an error message
stating that only R_386_JUMP_SLOT is supported.

As ARM and i386 share the same code, I've removed the ARM specific test
and added a writeIgotPlt() function that by default calls writeGotPlt().
ARM and i386 override this to write the address of the ifunc resolver.

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

llvm-svn: 289198
2016-12-09 09:59:54 +00:00
Rui Ueyama 26186c7e0f Add type records to TPI stream.
I don't think the data I add to a TPI stream in this patch is correct,
but at least it can be displayed using llvm-pdbdump. Until I add more
streams to a PDB file, I'm not able to know whether the data will be
accepted by MSVC tools or not.

llvm-svn: 289183
2016-12-09 04:46:54 +00:00
Rui Ueyama 7a0a076e40 COFF: Use make() in SymbolTable and Writer.
llvm-svn: 289170
2016-12-09 02:13:12 +00:00
Meador Inge 95c7d8d2a7 [ELF] Allow output section data commands to take expressions
The current implementation of the output section data store commands
can only handle integer literals, but it should really handle arbitrary
expressions [1].  This commit fixes that.

[1] https://sourceware.org/binutils/docs-2.27/ld/Output-Section-Data.html#Output-Section-Data

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

llvm-svn: 289152
2016-12-08 23:21:30 +00:00
Rui Ueyama 89ccd0f31c Split linkerscript.s into small test files.
linkerscript.s is the first test file for linker script, and at the moment
it contains all tests for linker scripts. Now that test file doesn't make
sense.

linkerscript2.s was just badly named. Renamed searchdir.s.

llvm-svn: 289148
2016-12-08 22:36:12 +00:00
Rui Ueyama 3c04f8d790 Print a warning message if ENTRY() symbol is not found.
llvm-svn: 289146
2016-12-08 22:26:31 +00:00
Rui Ueyama 968468033c Fix -Wunused-private-field.
llvm-svn: 289112
2016-12-08 21:49:18 +00:00
Rui Ueyama 8d433d71f5 COFF: Use a string saver in Memory.h in Driver.
llvm-svn: 289104
2016-12-08 21:27:09 +00:00
Rui Ueyama 8fe1767cdd Remove unnecessary `llvm::`.
llvm-svn: 289102
2016-12-08 20:50:47 +00:00
Rui Ueyama bf4ad1d63d COFF: Use make() to create a new file object in createFile.
llvm-svn: 289097
2016-12-08 20:20:22 +00:00
Rui Ueyama 7fed58cd9f Start using make() in COFF (with a smaller change.)
llvm-svn: 289089
2016-12-08 19:10:28 +00:00
Rui Ueyama aa4f4450af Revert r289084: Start using make() in COFF.
This reverts commit r289084 to appease buildbots.

llvm-svn: 289086
2016-12-08 18:49:04 +00:00
Rui Ueyama 843233b920 Start using make() in COFF.
We don't want ELF and COFF to diverge too much.

llvm-svn: 289085
2016-12-08 18:31:18 +00:00
Rui Ueyama 520d9169e6 Move Memory.{h,cpp} to lld/Support so that we can use them from COFF.
llvm-svn: 289084
2016-12-08 18:31:13 +00:00
Rafael Espindola d0ebd84c42 Change the implementation of --dynamic-list to use linker script parsing.
The feature is documented as
-----------------------------
The format of the dynamic list is the same as the version node
without scope and node name.  See *note VERSION:: for more
information.
--------------------------------

And indeed qt uses a dynamic list with an 'extern "C++"' in it. With
this patch we support that

The change to gc-sections-shared makes us match bfd. Just because we
kept bar doesn't mean it has to be in the dynamic symbol table.

The changes to invalid-dynamic-list.test and reproduce.s are because
of the new parser.

The changes to version-script.s are the only case where we change
behavior with regards to bfd, but I would like to see a mix of
--version-script and --dynamic-list used in the wild before
complicating the code.

llvm-svn: 289082
2016-12-08 17:54:26 +00:00
Rui Ueyama 6f6d46d497 Use `make` to simplify. NFC.
llvm-svn: 289081
2016-12-08 17:48:52 +00:00
Rui Ueyama 6dbf7ff747 Use make to instantiate Target and LinkerScript. NFC.
llvm-svn: 289079
2016-12-08 17:44:39 +00:00
Rui Ueyama e4eadb6a24 Split LinkerDriver::link. NFC.
llvm-svn: 289078
2016-12-08 17:44:37 +00:00
Rui Ueyama 34bf8677a4 Remove a special handling of AMDGPU entry points.
This is the last peculiar semantics left in the linker. If you want to
always set an entry point to 0, you can pass `-e 0` to the linker.

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

llvm-svn: 289077
2016-12-08 17:32:58 +00:00
Rafael Espindola 7e71415cb3 Add support for 'extern "C"'.
It is used by Qt.

llvm-svn: 289074
2016-12-08 17:26:53 +00:00
Rui Ueyama 8cb6283e74 Make function names shorter. NFC.
llvm-svn: 289072
2016-12-08 17:18:09 +00:00
Rui Ueyama 248e4a344c Do not use template where template is not needed.
Compilers can inline and optimize this code in the same way as template.

llvm-svn: 289071
2016-12-08 17:04:18 +00:00
Rafael Espindola 4737f94132 Make this test more strict. NFC.
llvm-svn: 289069
2016-12-08 16:51:56 +00:00
Rafael Espindola 191390a851 Inline function called only once.
llvm-svn: 289067
2016-12-08 16:26:20 +00:00
Rafael Espindola 361da4cef7 Handle C++ names in anon scripts.
llvm-svn: 289066
2016-12-08 16:20:29 +00:00
Rafael Espindola defdfa86c1 Inline two functions called only once. NFC.
llvm-svn: 289065
2016-12-08 16:02:48 +00:00
Rafael Espindola c65aee64ec Add two helper functions. NFC.
llvm-svn: 289064
2016-12-08 15:56:33 +00:00
Rafael Espindola 39c16dfbce Simplify. NFC.
llvm-svn: 289062
2016-12-08 15:36:58 +00:00
Simon Atanasyan 872764f6fe [ELF] Correct addAbsolute function argument name
Follow-up to r289025.

llvm-svn: 289061
2016-12-08 15:29:17 +00:00
George Rimar c49fd8c477 [ELF] - Read 16 bits for R_386_16/R_386_PC16 relocations instead of 32.
Looks it was theoretically incorrect if the section is at the very end of the file as
reading 32 bits would pass the end of file

llvm-svn: 289046
2016-12-08 13:50:28 +00:00
Peter Smith baffdb8bc2 [ELF] ifunc implementation using synthetic sections
This change introduces new synthetic sections IpltSection, IgotPltSection
that represent the ifunc entries that would previously have been put in
the PltSection and the GotPltSection. The separation makes sure that
the R_*_IRELATIVE relocations are placed after the non R_*_IRELATIVE
relocations, which permits ifunc resolvers to know that the .got.plt
slots will be initialized prior to the resolver being called.

A secondary benefit is that for ARM we can move the IgotPltSection and its
dynamic relocations to the .got and .rel.dyn as the ARM glibc expects all
the R_*_IRELATIVE relocations to be in the .rel.dyn

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

llvm-svn: 289045
2016-12-08 12:58:55 +00:00
Simon Atanasyan 6a4eb75c46 [ELF][MIPS] Make _gp, _gp_disp, __gnu_local_gp global symbols
These MIPS specific symbols should be global because in general they can
have an arbitrary value. By default this value is a fixed offset from .got
section.

This patch adds more checks to the mips-gp-local.s test case but marks
it as XFAIL because LLD does not allow redefinition of absolute symbols
value by a linker script. This should be fixed by D27276.

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

llvm-svn: 289025
2016-12-08 06:19:47 +00:00
Rafael Espindola 41217616a8 Delete dead code.
Thanks to George Rimar for pointing it out.

llvm-svn: 289020
2016-12-08 03:17:05 +00:00
Rui Ueyama 9a97acc097 Remove redundant call of std::unique_ptr::get.
Obj is an instance of std::unique_ptr, so *Obj.get() is the same as *Obj.

llvm-svn: 288996
2016-12-07 23:26:39 +00:00
Rui Ueyama 332e02a164 Fix Windows buildbots.
clang-format-diff sorted these #include's in the asciibetical order,
but they need to be in this order.

llvm-svn: 288995
2016-12-07 23:24:32 +00:00
Rui Ueyama 4c5b8cea02 Make demangle() return None instead of "" if a given string is not a mangled symbol.
llvm-svn: 288993
2016-12-07 23:17:05 +00:00
Rui Ueyama a45d45e231 COFF: Define overloaded toString functions.
Previously, we had different way to stringize SymbolBody and InputFile
to construct error messages. This patch defines overloaded function
toString() so that we don't need to memorize all these different
function names.

With that change, it is now easy to include demangled names in error
messages. Now, if there is a symbol name conflict, we'll print out
both mangled and demangled names.

llvm-svn: 288992
2016-12-07 23:17:02 +00:00
Rafael Espindola d4db0b3748 Rename MaxPageSize to DefaultMaxPageSize to avoid confusion.
Thanks to Rui for the suggestion.

llvm-svn: 288982
2016-12-07 21:13:27 +00:00
Rui Ueyama 007f9f3e3d Fix Windows buildbots.
llvm-svn: 288975
2016-12-07 20:31:46 +00:00
Rafael Espindola 476d20764a Use the correct MaxPageSize.
Now Target->MaxPageSize is only used as the default value of
Config->MaxPageSize.

llvm-svn: 288974
2016-12-07 20:29:46 +00:00
Rui Ueyama f966fe6c38 Do not pass line number to convertToUnixPathSeparator.
Line number can never contain '/' or '\', so the previous code
was pointless at that point.

llvm-svn: 288973
2016-12-07 20:25:45 +00:00
Rui Ueyama 63d064e9d1 Make convertToUnixPathSeparator return a new string instead of mutating argument.
llvm-svn: 288972
2016-12-07 20:22:27 +00:00
Rafael Espindola 8b8f74f6b7 Simplify. NFC.
llvm-svn: 288971
2016-12-07 20:20:39 +00:00
Rafael Espindola a86a9c6fad Use the correct MaxPageSize.
Found by inspection.

llvm-svn: 288970
2016-12-07 20:10:43 +00:00
George Rimar 49326e4d84 Format. NFC.
llvm-svn: 288967
2016-12-07 19:44:27 +00:00
George Rimar 92c88a4f76 [ELF] - Print absolute file name in errors when possible.
Currently LLD prints basename of source file name in error messages,
for example:
$ mkdir foo
$ echo 'void _start(void) { foobar(); }' > foo/bar.c
$ gcc -g -c foo/bar.c
$ bin/ld.lld -o out bar.o 
bin/ld.lld: error: bar.c:1: undefined symbol 'foobar'
$
This should say:
bin/ld.lld: error: foo/bar.c:1: undefined symbol 'foobar'

This is PR31299

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

llvm-svn: 288966
2016-12-07 19:42:25 +00:00
Adhemerval Zanella d719d37156 ELF/AArch64: Refactor R_AARCH64_LDST{8,15,32,64,128}_ABS_LO12_NC Relocations
This patch refactor how to apply the R_AARCH64_LDST{8,16,32,64,128}_ABS_NC
relocations by adding a new function to correct extract the bits expected
by each relocation.  This make is explicit which are the bits range expected
and simplify the code to mask and shift the deriable values.

It also fixes the R_AARCH64_LDST128_ABS_LO12_NC mask, although in pratice
the mask/shift always returns a 16 bytes aligned value.

Checked on AArch64 and with test-suite.

llvm-svn: 288921
2016-12-07 17:31:48 +00:00
Rui Ueyama 4de746f326 Add comments and reorder code a bit to clarify the intention. NFC.
llvm-svn: 288885
2016-12-07 04:45:34 +00:00
Rui Ueyama 9e5f5effe5 Make a decision about whether we should warn on missing entry or not early.
Config->WarnMissingEntry is a single-purpose boolean variable, and
I think it's easier to understand than Config->HasEntry.

llvm-svn: 288883
2016-12-07 04:06:21 +00:00
Rui Ueyama a1407c4fdf Simplify -e <number> option handling.
This patch is to parse the entry symbol name lazily.

llvm-svn: 288882
2016-12-07 03:23:06 +00:00
Petr Hosek 7173340e1f [ELF] Fix the broken PPC test
This is related to the change in handling of entry point symbols.

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

llvm-svn: 288880
2016-12-07 03:04:02 +00:00
Petr Hosek 2f50fef095 [ELF] Shared libraries should have entry point
Shared libraries should have entry set following the same rules as for
regular binaries. The only difference is that in case the default entry
point (_start or __start) isn't found (unless it was set explicitly), we
shouldn't give a warning as in case of regular binaries.

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

llvm-svn: 288878
2016-12-07 02:26:16 +00:00
Petr Hosek 668bebed6d [ELF] Only binaries should have DT_DEBUG entry
The presence of DT_DEBUG entry is unrelated to the existence of entry point.

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

llvm-svn: 288877
2016-12-07 02:05:42 +00:00
George Rimar a2a32c2cc8 [ELF] - Teach LLD to recognize PT_OPENBSD_BOOTDATA
Minor patch to fix PR31288

OpenBSD commit:
d39116912b

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

llvm-svn: 288832
2016-12-06 17:57:42 +00:00
Rafael Espindola 074ba93ceb Don't print empty PT_LOAD.
If we do, the freebsd dynamic linker tries to call mmap with a size 0,
which fails.

It is hard to avoid creating them when linker scripts are used, so we
just delete empty PT_LOADs at the end.

llvm-svn: 288808
2016-12-06 13:43:34 +00:00
Rafael Espindola e004d4bfc2 Don't crash trying to write an 0 addend.
For preemptable symbols the dynamic linker does all the work. Trying
to compute the addend is at best wasteful and can also lead to crashes
in cases of programs that uses tls but doesn't define any tls
variables.

llvm-svn: 288803
2016-12-06 12:19:24 +00:00
Rafael Espindola 43c3cb7786 Make the test a bit more strict. NFC.
llvm-svn: 288802
2016-12-06 12:15:12 +00:00
Rui Ueyama c8e6884871 Inline MergeInputSection::getData().
This change seems to make LLD 0.6% faster when linking Clang with
debug info. I don't want us to have lots of local optimizations,
but this function is very hot, and the improvement is small but
not negligible, so I think it's worth doing.

llvm-svn: 288757
2016-12-06 02:19:30 +00:00
Rafael Espindola d207c37806 Test only the relevant bits.
This test only needs to test the Type (SharedObject), the address of
the first PT_LOAD and the presence of PT_DYNAMIC.

llvm-svn: 288719
2016-12-05 22:27:21 +00:00
Rafael Espindola d6b42ee404 Don't check the symbol values is this test.
It only needs to find how many are local.

llvm-svn: 288716
2016-12-05 22:16:32 +00:00
Rui Ueyama c38860b0d2 Revert r288707: Split removeUnusedSyntheticSections into two functions.
That patch broke build.

llvm-svn: 288708
2016-12-05 21:39:35 +00:00
Rui Ueyama 29e7a19e17 Split removeUnusedSyntheticSections into two functions.
llvm-svn: 288707
2016-12-05 21:37:16 +00:00
Rafael Espindola 7b4dd875df Don't check symbol value in this test.
llvm-svn: 288704
2016-12-05 21:02:45 +00:00
Rafael Espindola 35ac8bab7b Don't check the symbol value in this test.
llvm-svn: 288702
2016-12-05 20:56:40 +00:00
Rafael Espindola c5b3334124 Don't check the symbol values.
This test is just about which symbols are in which table.

llvm-svn: 288701
2016-12-05 20:53:11 +00:00
Rafael Espindola fc4ddbbe8f Simplify test. NFC.
llvm-svn: 288700
2016-12-05 20:49:16 +00:00
Rafael Espindola b4e61e9dd0 Make test test what it should be testing.
Looks like the second section in this test was lost along the way.

llvm-svn: 288699
2016-12-05 20:42:58 +00:00
Rui Ueyama 44da9decb5 Include object file name to an error message.
llvm-svn: 288686
2016-12-05 18:40:14 +00:00
Rui Ueyama fcd3fa83ea Use "equivalence class" instead of "color" to describe the concept in ICF.
Also add a citation to GNU gold safe ICF paper.

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

llvm-svn: 288684
2016-12-05 18:11:35 +00:00
Rui Ueyama 6d12eaee8b Remove existing file in a separate thread asynchronously.
On Linux (and probably on other Unix-like systems), unlink(2) is
noticeably slow. It takes 250 milliseconds to remove a 1 GB file
on ext4 filesystem on my machine, whether the file is on SSD or
on a spinning disk.

To create a new result file, we remove existing file first. So, if
you repeatedly link a 1 GB program in a regular compile-link-debug
cycle, every cycle wastes 250 milliseconds only to remove a file.

Since LLD can link a 1 GB in about 5 seconds, that waste actually
matters.

This patch defines `unlinkAsync` function. The function spawns a
background thread to call unlink. The calling thread returns
almost immediately.

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

llvm-svn: 288680
2016-12-05 17:40:37 +00:00
Eugene Leviant 2a942c4b45 [ELF] Print file:line for unknown PHDR error
Differential revision: https://reviews.llvm.org/D27335

llvm-svn: 288678
2016-12-05 16:38:32 +00:00
Adhemerval Zanella a47ba192dc ELF/AArch64: Fix R_AARCH64_LDST16_ABS_LO12_NC mask
The relocation R_AARCH64_LDST16_ABS_LO12_NC should set a ld/st
immediate value to bits [11:1] not [11:2].  This patches fixes it
and adds a testcase for regression.

With this fix all the faulty tests on test-suite (clavm, lencod,
and trimaran) pass.

llvm-svn: 288670
2016-12-05 14:15:44 +00:00
Adhemerval Zanella df310646d8 ELF/AArch64: Simplify R_AARCH64_ADD_ABS_LO12_NC relocation
This patch uses the updateAArch64Add on relocation apply and remove
the comment.

llvm-svn: 288669
2016-12-05 14:15:03 +00:00
Adhemerval Zanella 6afe128ae5 ELF/AArch64: consolidate getAArch64Page implementation
This patch avoid getAArch64Page code duplication by removing the
implementation at InputSection.

llvm-svn: 288668
2016-12-05 14:14:26 +00:00
Rui Ueyama ca17841fc4 Run the last iteration of parallel_for_loop using a threadpool.
Remainders of tasks were ran in the main thread, so parallel_for_each
could theoretically take 2x time than the ideal.

llvm-svn: 288631
2016-12-05 02:07:29 +00:00
Rui Ueyama 5cb712ed3c Simplify ICF alignment handling.
llvm-svn: 288630
2016-12-05 01:31:39 +00:00
Rui Ueyama 045d828158 Re-implement the optimization that I removed in r288527.
I removed a wrong optimization for ICF in r288527. Sean Silva suggested
in a post commit review that the correct algorithm can be implemented
easily. So is this patch.

llvm-svn: 288620
2016-12-04 16:33:13 +00:00
Rafael Espindola 61d052d725 Don't discard .L symbol with -r.
They might be used by relocations.

Fixes pr31252.

llvm-svn: 288617
2016-12-04 08:34:17 +00:00
Rui Ueyama 28f22ae15e Update comment to clarify the machine spec.
llvm-svn: 288609
2016-12-04 02:34:29 +00:00
Rui Ueyama c3aacfd91b Add comments about the use of threads in LLD.
llvm-svn: 288606
2016-12-03 23:35:22 +00:00
Rui Ueyama 244a435ae3 Factor out common code to a header.
llvm-svn: 288599
2016-12-03 21:24:51 +00:00
Rafael Espindola 27004d336f Ignone SHF_INFO_LINK.
Some elf producers (dtrace) put this flag in relocation sections and
some (MC) don't. If we don't ignore the flag we end up with multiple
relocation sections poiting to the same section, which we don't
support.

llvm-svn: 288585
2016-12-03 15:26:18 +00:00
George Rimar 1b3d34a298 [ELF] - Implemented R_386_16 and R_386PC16 relocations
A program or object file using R_386_8, R_386_16, R_386_PC16 or R_386_PC8
relocations is not conformant to latest ABI. The R_386_16, and R_386_8
relocations truncate the computed value to 16 - bits and 8 - bits
respectively. R_386_PC16 and R_386_16 are used by some
applications, for example by FreeBSD loaders.

Previously we did not take addend in account for these relocation,
counting it as 0, what is wrong and was a reason of hangs.

This patch needed for example for FreeBSD pmbr (protective mbr).

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

llvm-svn: 288581
2016-12-03 07:30:30 +00:00
George Rimar 40c28c7f9a [ELF] - Change the way how we compute offsets for binary output.
Binary output feature is a bit confuzing. bfd and gold output differs a lot sometimes,
though it is important for FreeBSD mbr loaders.

Patch change the way how we compute file offsets for binary output.
This fixes PR31196.

Previously offsets were calculated basing on offsets and addresses of sections
from the same loads:
if (Sec == First)
  return alignTo(Off, Target->MaxPageSize, Sec->Addr);
return First->Offset + Sec->Addr - First->Addr;

bfd assigns offsets for each section to VA - MinVA:
https://github.com/redox-os/binutils-gdb/blob/master/bfd/binary.c#L27
https://github.com/redox-os/binutils-gdb/blob/master/bfd/binary.c#L255 
(LMA == VA usually)

This patch for now just stops creating phdrs for binary output. 
An effect from this that no any additional calculation for offset is performed:

uintX_t getFileAlignment(uintX_t Off, OutputSectionBase *Sec) {
OutputSectionBase *First = Sec->FirstInPtLoad;
// If the section is not in a PT_LOAD, we have no other constraint.
if (!First)
  return Off; //**First is always null, condition always happens**

That is enough now with combination of another patch to generate output
that is similar to what bfd produce for mbr loader.

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

llvm-svn: 288580
2016-12-03 07:23:30 +00:00
George Rimar d250618c3e [ELF] - Disable relro when -omagic specified.
--omagic is an option to create old-fashioned executables in which
.text segments are writable. Today, the option is still in use to
create special-purpose programs such as boot loaders. It doesn't
make sense to create PT_GNU_RELRO for such executables.

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

llvm-svn: 288579
2016-12-03 07:09:28 +00:00
Rui Ueyama 5419861a52 Remove a wrong performance optimization.
This is a hack for single thread execution. We are using Color[0] and
Color[1] alternately on each iteration. This optimization is to look
at the next slot as opposted to the current slot to get recent results
early. Turns out that the assumption is wrong, because the other slots
are not always have the most recent values, but instead it may have
stale values of the previous iteration. This patch removes that
performance hack.

llvm-svn: 288527
2016-12-02 18:40:43 +00:00
Rui Ueyama 83ec681a5c Removed a wrong assertion about non-colorable sections.
The assertion asserted that colorable sections can never have
a reference to non-colorable sections, but that was simply wrong.
They can have references to non-colorable sections. If that's the
case, referenced sections must be the same in terms of pointer
comparison.

llvm-svn: 288511
2016-12-02 17:23:58 +00:00
Rui Ueyama 3a618e5606 Port parallel ICF to COFF.
LLD used to take 11.73 seconds to link Clang. Now it is 6.94 seconds.
MSVC link takes 83.02 seconds. Note that ICF is enabled by default on
Windows, so a low latency ICF is more important than in ELF.

llvm-svn: 288487
2016-12-02 08:03:58 +00:00
Rafael Espindola 5708b2f8a6 Ignore R_X86_64_NONE.
It looks like the way dtrace works is

* The user creates .o files that reference magical symbol names.
* dtrace reads those files, collecs the info it needs and changes the
  relocation to R_X86_64_NONE expecting the linker to ignore them.

llvm-svn: 288485
2016-12-02 08:00:09 +00:00
Rui Ueyama 27498b5dd5 Fix a bug in ICF involving COFF associative sections.
Associative sections are sections that need to be linked if their associated
sections are linked. Associative sections are used to append auxiliary data
such as debug info.

Previously, we compared all associative sections when comparing two comdat
sections. Because usually assocative sections are not mergeable sections,
we missed a lot of mergeable sections. MSVC linker doesn't seem to check
the identity of associative sections.

This patch makes LLD to ignore associative sections when doing ICF.

llvm-svn: 288483
2016-12-02 07:46:12 +00:00
Rui Ueyama 1b6bab011c Fix the worse case performance of ICF.
r288228 seems to have regressed ICF performance in some cases in which
a lot of sections are actually mergeable. In r288228, I made a change
to create a Range object for each new color group. So every time we
split a group, we allocated and added a new group to a list of groups.

This patch essentially reverted r288228 with an improvement to
parallelize the original algorithm.

Now the ICF main loop is entirely allocation-free and lock-free.

Just like pre-r288228, we search for group boundaries by linear scan
instead of managing the information using Range class. r288228 was
neutral in performance-wise, and so is this patch.

I confirmed that this produces the exact same result as before
using chromium and clang as tests.

llvm-svn: 288480
2016-12-02 05:35:46 +00:00
Rafael Espindola 103fc28961 Add a test documenting how we handle addends on Elf_Rela.
llvm-svn: 288477
2016-12-02 04:20:47 +00:00
Rafael Espindola 858c092daa Allow duplicated abs symbols with the same value.
This is a fairly reasonable bfd extension since there is one obvious value.

dtrace depends on this feature as it creates multiple absolute
symbols with the same value.

llvm-svn: 288461
2016-12-02 02:58:21 +00:00
Rafael Espindola f4ff80c128 Write the addent to got entries when using Elf_Rel.
llvm-svn: 288451
2016-12-02 01:57:24 +00:00
Rui Ueyama 395859bdb7 Fix undefined behavior.
New items can be added to Ranges here, and that invalidates
an iterater that previously pointed the end of the vector.

llvm-svn: 288443
2016-12-02 00:38:15 +00:00
Rui Ueyama a6cd5fe415 Add an assert instead of ignoring an impossible condition.
llvm-svn: 288419
2016-12-01 21:41:06 +00:00
Rui Ueyama 91ae861af5 Updates file comments and variable names.
Use "color" instead of "group id" to describe the ICF algorithm.

llvm-svn: 288409
2016-12-01 19:45:22 +00:00
Rui Ueyama c1835319c9 Parallelize ICF to make LLD's ICF really fast.
ICF is short for Identical Code Folding. It is a size optimization to
identify two or more functions that happened to have the same contents
to merges them. It usually reduces output size by a few percent.

ICF is slow because it is computationally intensive process. I tried
to paralellize it before but failed because I couldn't make a
parallelized version produce consistent outputs. Although it didn't
create broken executables, every invocation of the linker generated
slightly different output, and I couldn't figure out why.

I think I now understand what was going on, and also came up with a
simple algorithm to fix it. So is this patch.

The result is very exciting. Chromium for example has 780,662 input
sections in which 20,774 are reducible by ICF. LLD previously took
7.980 seconds for ICF. Now it finishes in 1.065 seconds.

As a result, LLD can now link a Chromium binary (output size 1.59 GB)
in 10.28 seconds on my machine with ICF enabled. Compared to gold
which takes 40.94 seconds to do the same thing, this is an amazing
number.

From here, I'll describe what we are doing for ICF, what was the
previous problem, and what I did in this patch.

In ICF, two sections are considered identical if they have the same
section flags, section data, and relocations. Relocations are tricky,
becuase two relocations are considered the same if they have the same
relocation type, values, and if they point to the same section _in
terms of ICF_.

Here is an example. If foo and bar defined below are compiled to the
same machine instructions, ICF can (and should) merge the two,
although their relocations point to each other.

  void foo() { bar(); }
  void bar() { foo(); }

This is not an easy problem to solve.

What we are doing in LLD is some sort of coloring algorithm. We color
non-identical sections using different colors repeatedly, and sections
in the same color when the algorithm terminates are considered
identical. Here is the details:

  1. First, we color all sections using their hash values of section
  types, section contents, and numbers of relocations. At this moment,
  relocation targets are not taken into account. We just color
  sections that apparently differ in different colors.

  2. Next, for each color C, we visit sections having color C to see
  if their relocations are the same. Relocations are considered equal
  if their targets have the same color. We then recolor sections that
  have different relocation targets in new colors.

  3. If we recolor some section in step 2, relocations that were
  previously pointing to the same color targets may now be pointing to
  different colors. Therefore, repeat 2 until a convergence is
  obtained.

Step 2 is a heavy operation. For Chromium, the first iteration of step
2 takes 2.882 seconds, and the second iteration takes 1.038 seconds,
and in total it needs 23 iterations.

Parallelizing step 1 is easy because we can color each section
independently. This patch does that.

Parallelizing step 2 is tricky. We could work on each color
independently, but we cannot recolor sections in place, because it
will break the invariance that two possibly-identical sections must
have the same color at any moment.

Consider sections S1, S2, S3, S4 in the same color C, where S1 and S2
are identical, S3 and S4 are identical, but S2 and S3 are not. Thread
A is about to recolor S1 and S2 in C'. After thread A recolor S1 in
C', but before recolor S2 in C', other thread B might observe S1 and
S2. Then thread B will conclude that S1 and S2 are different, and it
will split thread B's sections into smaller groups wrongly. Over-
splitting doesn't produce broken results, but it loses a chance to
merge some identical sections. That was the cause of indeterminism.

To fix the problem, I made sections have two colors, namely current
color and next color. At the beginning of each iteration, both colors
are the same. Each thread reads from current color and writes to next
color. In this way, we can avoid threads from reading partial
results. After each iteration, we flip current and next.

This is a very simple solution and is implemented in less than 50
lines of code.

I tested this patch with Chromium and confirmed that this parallelized
ICF produces the identical output as the non-parallelized one.

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

llvm-svn: 288373
2016-12-01 17:09:04 +00:00
Sean Silva 2eed75926c Add `isRelExprOneOf` helper
In various places in LLD's hot loops, we have expressions of the form
"E == R_FOO || E == R_BAR || ..." (E is a RelExpr).

Some of these expressions are quite long, and even though they usually go just
a very small number of ways and so should be well predicted, they can still
occupy branch predictor resources harming other parts of the code, or they
won't be predicted well if they overflow branch predictor resources or if the
branches are too dense and the branch predictor can't track them all (the
compiler can in theory avoid this, at a cost in text size). And some of these
expressions are so large and executed so frequently that even when
well-predicted they probably still have a nontrivial cost.

This speedup should be pretty portable. The cost of these simple bit tests is
independent of:

- the target we are linking for
- the distribution of RelExpr's for a given link (which can depend on how the
  input files were compiled)
- what compiler was used to compile LLD (it is just a simple bit test;
  hopefully the compiler gets it right!)
- adding new target-dependent relocations (e.g. needsPlt doesn't pay any extra
  cost checking R_PPC_PLT_OPD on x86-64 builds)

I did some rough measurements on clang-fsds and this patch gives over about 4%
speedup for a regular -O1 link, about 2.5% for -O3 --gc-sections and over 5%
for -O0. Sorry, I don't have my current machine set up for doing really
accurate measurements right now.

This also is just a bit cleaner. Thanks for Joerg for suggesting for
this approach.

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

llvm-svn: 288314
2016-12-01 05:43:48 +00:00
Rui Ueyama 10091b0ac2 Simplify ScriptParser.
- Rename currentBuffer -> getCurrentMB to start it with verb.
 - Simplify containsString.
 - Add llvm_unreachable at end of getCurrentMB.

llvm-svn: 288310
2016-12-01 04:36:54 +00:00
Rui Ueyama 3cd22d3104 Do not name a variable Ret which is not a return value.
llvm-svn: 288309
2016-12-01 04:36:51 +00:00
Rui Ueyama b5f1c3ec0c Make get{Line,Column}Number members of StringParser.
This patch also renames currentLocation getCurrentLocation.

llvm-svn: 288308
2016-12-01 04:36:49 +00:00
Rui Ueyama 50fb82743e Split getPos into getLineNumber and getColumnNumber.
llvm-svn: 288306
2016-12-01 03:56:27 +00:00
Rui Ueyama c5cb737584 Dump Codeview type information correctly.
llvm-svn: 288298
2016-12-01 01:22:48 +00:00
Rui Ueyama 9dedfb1fa8 Change how we manage groups in ICF.
Previously, on each iteration in ICF, we scan the entire vector of
input sections to find boundaries of groups having the same ID.

This patch changes the algorithm so that we now have a vector of ranges.
Each range contains a starting index and an ending index of the group.
So we no longer have to search boundaries on each iteration.

Performance-wise, this seems neutral. Instead of searching boundaries,
we now have to maintain ranges. But I think this is more readable
than the previous implementation.

Moreover, this makes easy to parallelize the main loop of ICF,
which I'll do in a follow-up patch.

llvm-svn: 288228
2016-11-30 01:50:03 +00:00
Rui Ueyama 84e65a7ca1 Use StringRefZ explicitly instead of const char *.
This patch is to avoid an implicit conversion from const char * to
StringRefZ, to make it apparent where we are using StringRefZ.

llvm-svn: 288182
2016-11-29 19:11:39 +00:00
Rui Ueyama a13efc2a73 Introduce StringRefZ class to represent null-terminated strings.
StringRefZ is a class to represent a null-terminated string. String
length is computed lazily, so it's more efficient than StringRef to
represent strings in string table.

The motivation of defining this new class is to merge functions
that only differ in string types; we have many constructors that takes
`const char *` or `StringRef`. With StringRefZ, we can merge them.

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

llvm-svn: 288172
2016-11-29 18:05:04 +00:00
Peter Smith de3e73880e [ELF] Add support for static TLS to ARM
The module index dynamic relocation R_ARM_DTPMOD32 is always 1 for an
executable. When static linking and when we know that we are not a shared
object we can resolve the module index relocation statically.
    
The logic in handleNoRelaxTlsRelocation remains the same for Mips as it
has its own custom GOT writing code. For ARM we add the module index
relocation to the GOT when it can be resolved statically.
    
In addition the type of the RelExpr for the static resolution of TlsGotRel
should be R_TLS and not R_ABS as we need to include the size of
the thread control block in the calculation.
    
Addresses the TLS part of PR30218.

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

llvm-svn: 288153
2016-11-29 16:23:50 +00:00
George Rimar 9b3ae73fc8 [ELF] - Disable emiting multiple output sections when merging is disabled.
When -O0 is specified, we do not do section merging.
Though before this patch several sections were generated instead
of single, what is useless.

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

llvm-svn: 288151
2016-11-29 16:11:09 +00:00
George Rimar 3fb5a6dc9e [ELF] - Add support of proccessing of the rest allocatable synthetic sections from linkerscript.
This change continues what was started by D27040
Now all allocatable synthetics should be available from script side.

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

llvm-svn: 288150
2016-11-29 16:05:27 +00:00
Simon Atanasyan 160bf723f5 [ELF][MIPS] Make stable an order of GOT page address entries
llvm-svn: 288137
2016-11-29 13:26:04 +00:00
Simon Atanasyan 198dd205e6 [ELF][MIPS] Add new check to the test case in attempt to investigate Windows build-bot failure
llvm-svn: 288132
2016-11-29 11:19:47 +00:00
Simon Atanasyan 9705ff74ed [ELF][MIPS] Restore Config->Threads for MIPS targets
llvm-svn: 288130
2016-11-29 10:24:00 +00:00
Simon Atanasyan 9fae3b8a2c [ELF][MIPS] Do not change MipsGotSection state in the getPageEntryOffset method
The MipsGotSection::getPageEntryOffset calculates index of GOT entry
with a "page" address. Previously this method changes the state
of MipsGotSection because it modifies PageIndexMap field. That leads
to the unpredictable results if getPageEntryOffset called from multiple threads.

The patch makes getPageEntryOffset constant. To do so it calculates GOT
entry index but does not update PageIndexMap field. Later in the
MipsGotSection::writeTo method linker calculates "page" addresses and
writes them to the output.

llvm-svn: 288129
2016-11-29 10:23:56 +00:00
Simon Atanasyan a0efc4268c [ELF][MIPS] Replace the magic number of GOT header entries by constant. NFC
llvm-svn: 288128
2016-11-29 10:23:50 +00:00
Simon Atanasyan 80f3d9ce93 [ELF][MIPS] Fix calculation of GOT "page address" entries number
If output section which referenced by R_MIPS_GOT_PAGE or R_MIPS_GOT16
relocations is small (less that 0x10000 bytes) and occupies two adjacent
0xffff-bytes pages, current formula gives incorrect number of required "page"
GOT entries. The problem is that in time of calculation we do not know
the section address and so we cannot calculate number of 0xffff-bytes
pages exactly.

This patch fix the formula. Now it gives a correct number of pages in
the worst case when "small" section intersects 0xffff-bytes page
boundary. From the other side, sometimes it adds one more redundant GOT
entry for each output section. But usually number of output sections
referenced by GOT relocations is small.

llvm-svn: 288127
2016-11-29 10:23:46 +00:00
George Rimar 595a763f38 [ELF] - Implemented -N (-omagic) command line option.
-N (-omagic)
  Set the text and data sections to be readable and writable. 
  Also, do not page-align the data segment.

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

llvm-svn: 288123
2016-11-29 09:43:51 +00:00
Eugene Leviant 84569e6caa [ELF] Refactor target error messages
Differential revision: https://reviews.llvm.org/D27097

llvm-svn: 288114
2016-11-29 08:05:44 +00:00
Rui Ueyama bf4ddeb97c Style fix.
llvm-svn: 288113
2016-11-29 04:22:57 +00:00
Rui Ueyama c910bc7e19 Simplify "missing argument" error message.
llvm-svn: 288112
2016-11-29 04:17:31 +00:00
Rui Ueyama e5e407beb4 Add comments.
llvm-svn: 288111
2016-11-29 04:17:30 +00:00
Rui Ueyama bd2a812ff0 Print error message header in red.
llvm-svn: 288110
2016-11-29 04:09:08 +00:00
Rafael Espindola f1e245315b Use relocations to fill statically known got entries.
Right now we just remember a SymbolBody for each got entry and
duplicate a bit of logic to decide what value, if any, should be
written for that SymbolBody.

With ARM there will be more complicated values, and it seems better to
just use the relocation code to fill the got entries. This makes it
clear that each entry is filled by the dynamic linker or by the static
linker.

llvm-svn: 288107
2016-11-29 03:45:36 +00:00
Rafael Espindola d3b32df3de Sort. NFC.
llvm-svn: 288102
2016-11-29 03:36:30 +00:00