Commit Graph

4820 Commits

Author SHA1 Message Date
Peter Smith 732cd8cbef [ELF] Implement scanner for Cortex-A53 Erratum 843419
Add a new file AArch64ErrataFix.cpp that implements the logic to scan for
the Cortex-A53 Erratum 843419. This involves finding all the executable
code, disassembling the instructions that might trigger the erratum and
reporting a message if the sequence is detected.

At this stage we do not attempt to fix the erratum, this functionality
will be added in a later patch. See D36749 for proposal.

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

llvm-svn: 319780
2017-12-05 15:59:05 +00:00
George Rimar 31cd5da582 Recommit r319747 "[ELF] - Show copy relocated symbols in a --Map file output."
With fix:
Specify -soname for input dso to fix up the .dynstr section
size in different environments.

Original commit message:

As mentioned in PR35471, copied symbols did not show
in --Map output. Patch fixes that.

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

llvm-svn: 319769
2017-12-05 14:23:18 +00:00
Peter Smith 7c40f93f38 [ELF] InX::BssRelRo should check section contents before marking relro
When a linker script is used with a pattern like { *(.bss .bss.*) } the
InX::BssRelRo section will match against .bss.*. By matching on the name
only, in the same way that .data.rel.ro works we prevent this
from happening, but permit scripts that want to explicitly provide
a .bss.rel.ro OutputSection.
    
Differential Revision: https://reviews.llvm.org/D40735

llvm-svn: 319755
2017-12-05 11:15:58 +00:00
George Rimar fe52e0064b [ELF] - Revert r319747 "[ELF] - Show copy relocated symbols in a --Map file output.".
It broke bots:
http://lab.llvm.org:8011/builders/lld-x86_64-darwin13/builds/15570/steps/test_lld/logs/stdio
http://lab.llvm.org:8011/builders/sanitizer-x86_64-linux-fast/builds/10930/steps/check-lld%20asan/logs/stdio
http://lab.llvm.org:8011/builders/lld-x86_64-win7/builds/16336/steps/test%20lld/logs/stdio

According to outputs,
.dynstr section has different sizes for some reason.

llvm-svn: 319749
2017-12-05 09:38:05 +00:00
George Rimar d9ae97cdb6 [ELF] - Show copy relocated symbols in a --Map file output.
As mentioned in PR35471, copied symbols did not show
in --Map output. Patch fixes that.

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

llvm-svn: 319747
2017-12-05 09:13:45 +00:00
Rui Ueyama be61cc90c4 Make the behavior of the -v option more closer to GNU linkers.
Previously, lld exited with an error status if the only option given to
the command was -v. GNU linkers gracefully exit in that case. This patch
makes lld behave like GNU.

Note that even with this patch, lld's -v and --version options behave
slightly differently than GNU linkers' counterparts. For example,
if you run `ld.bfd -v -v`, the version string is printed out twice.
But that is an edge case that I don't think we need to take care of.

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

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

llvm-svn: 319717
2017-12-05 00:03:41 +00:00
Rui Ueyama 2278826c60 Cache modulo values for the .gnu.hash section.
This change actually makes the linker slightly faster. My observation
is that, with this patch, link time of clang without debug is about 1%
faster.

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

llvm-svn: 319600
2017-12-02 00:37:13 +00:00
Rafael Espindola b70824c1b8 Revert r318924 Skip over empty sections when checking for contiguous relro
PR35478 https://bugs.llvm.org/show_bug.cgi?id=35478 points out a flaw
in the implementation of r318924 from D40364. The implementation
depends on the Size field being set or the SyntheticSection::empty()
being accurate. These functions are not reliable as some linker script
commands that have yet to be processed may affect the results, causing
some non-zero size sections to be reported as zero size.

I think the first step is to revert r318924 and come up with a better
solution for the underlying problem rather than trying to layer more
heuristics onto the zero sized output section.

Chances are I'll be out of office by the time anyone sees this so feel
free to commit the revert if you agree with me.

Fixes PR35478

Current thoughts on the underlying problem:

Revisiting the motivation for adding the zero size check in the first
place; it was to prevent 0 sized SyntheticSections that a user does
not have full control over from needlessly breaking the PT_GNU_RELRO,
rather than trying to accommodate arbitrarily complex linker
scripts. Looking at the code, it looks like
removeUnusedSyntheticSections() should remove zero sized synthetic
sections. It does, but it doesn't set the Parent to nullptr, this has
the side effect that Sec == InX::BssRelRo->getParent() will make the
parent OutputSection of InX::BssRelRo RelRo even if there is no
InX::BssRelRo.

I tried a quick experiment with setting the Parent to nullptr and this
flushed out a few interesting test failures, it feels like playing
Jenga with every change:

    In the isRelroSection() we have to consider the case where there
    is no .plt and .plt.got but there is a ifunc plt with accompanying
    (ifunc .got or .plt.got)

    The PPC64 has PltHeaderSize == 0. Unfortunately HeaderSize == 0 is
    used to choose between the ifunc plt or normal plt. We seem to get
    away with this at the moment, but tests start to fail when Parent
    is set to nullptr for the .got.plt.

    The InX::BssRelRo and InX::Bss never get their sizes set and they
    are always removed by removeUnusedSyntheticSections(), their
    purpose seems to be as some kind of proxy for add .bss or
    .bss.relro InputSections into their parent OutputSections, they
    therefore don't behave like other SyntheticSections anyway.

My thinking is that some work is needed to make sure that the Sec ==
SyntheticSection->getParent() does a bit more checking before
returning true, particularly for InX::BssRelRo as that has special
behaviour. I'll hope to post something for review as soon as possible.

Patch by Peter Smith!

llvm-svn: 319563
2017-12-01 18:14:14 +00:00
George Rimar 196be4a18e [ELF] - Rename excessive variable. NFC.
It is a follow up for 
https://reviews.llvm.org/D40652 requested during review.

llvm-svn: 319527
2017-12-01 09:14:56 +00:00
George Rimar 78e27e830d [ELF] - Produce relocation section name consistent with output section name when --emit-reloc used with linker script.
This is for "Bug 35474 - --emit-relocs produces wrongly-named reloc sections".

LLD currently for scripts like:

.text.boot : { *(.text.boot) }
emits relocation section with name .rela.text because does not take
redefined name of output section into account and builds section name
using rules for non-scripted case. Patch fixes this oddness.

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

llvm-svn: 319526
2017-12-01 09:04:52 +00:00
Rui Ueyama 1cf7f9cc80 Make .gnu.hash section smaller.
Our on-disk hash table was unnecessarily large. The cost of collision is
not high in the .gnu.hash table because each symbol in the .gnu.hash
table has a hash value with it. So, for each collided symbol, the
dynamic linker just compares an integer, which is pretty cheap.

This patch increases the load factor by about 8. Here's a comparison.

  $ readelf --histogram libclangSema.so.6.0.0svn-new-lld
  Histogram for `.gnu.hash' bucket list length (total of 582 buckets):
   Length  Number     % of total  Coverage
        0  11         (  1.9%)
        1  35         (  6.0%)      1.5%
        2  93         ( 16.0%)      9.5%
        3  108        ( 18.6%)     23.4%
        4  121        ( 20.8%)     44.1%
        5  86         ( 14.8%)     62.6%
        6  63         ( 10.8%)     78.8%
        7  38         (  6.5%)     90.2%
        8  18         (  3.1%)     96.4%
        9  6          (  1.0%)     98.7%
       10  3          (  0.5%)    100.0%

  $ readelf --histogram libclangSema.so.6.0.0svn-old-lld
  Histogram for `.gnu.hash' bucket list length (total of 4093 buckets):
   Length  Number     % of total  Coverage
        0  1498       ( 36.6%)
        1  1545       ( 37.7%)     37.7%
        2  712        ( 17.4%)     72.5%
        3  251        (  6.1%)     90.9%
        4  66         (  1.6%)     97.3%
        5  16         (  0.4%)     99.3%
        6  5          (  0.1%)    100.0%

  $ readelf --histogram libclangSema.so.6.0.0svn-bfd
  Histogram for `.gnu.hash' bucket list length (total of 1004 buckets):
   Length  Number     % of total  Coverage
      0  92         (  9.2%)
        1  227        ( 22.6%)      9.8%
        2  266        ( 26.5%)     32.6%
        3  222        ( 22.1%)     61.2%
        4  115        ( 11.5%)     81.0%
        5  55         (  5.5%)     92.8%
        6  21         (  2.1%)     98.2%
        7  6          (  0.6%)    100.0%

  $ readelf --histogram libclangSema.so.6.0.0svn-gold
  Histogram for `.gnu.hash' bucket list length (total of 2053 buckets):
   Length  Number     % of total  Coverage
        0  671        ( 32.7%)
        1  709        ( 34.5%)     30.4%
        2  470        ( 22.9%)     70.7%
        3  141        (  6.9%)     88.9%
        4  54         (  2.6%)     98.2%
        5  5          (  0.2%)     99.2%
        6  3          (  0.1%)    100.0%

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

llvm-svn: 319503
2017-11-30 23:59:40 +00:00
Rafael Espindola 477ff12411 Error on -no-define-common if the output is not relocatable.
The ELF spec says

Symbols with section index SHN_COMMON may appear only in relocatable
objects.

Currently lld can produce file that break that requirement.

llvm-svn: 319473
2017-11-30 20:46:33 +00:00
Rafael Espindola b2ee25ac8a Simplify. NFC.
This particular reportDuplicate is only used with Defined.

llvm-svn: 319451
2017-11-30 18:02:04 +00:00
Rafael Espindola de38b3d22f Handle copy relocations in symbol assignments.
When a linker script has "foo = bar" and bar is the result of a copy
relocation foo should point to the same location in .bss.

This is part of a growing evidence that copy relocations should be
implemented by using replaceSymbol to replace the SharedSymbol with a
Defined.

llvm-svn: 319449
2017-11-30 17:51:10 +00:00
George Rimar 2e610cd470 [ELF] - Handle EhInputSection Live bit in MarkLive.cpp
Since MarkLive.cpp is the place where we set Live flags for
other sections, it looks correct to do that there.
Benefit is that we stop spreading GC logic outsize of MarkLive.cpp.

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

llvm-svn: 319435
2017-11-30 14:01:06 +00:00
Rafael Espindola 568ee2c089 Don't crash on invalid.
llvm-svn: 319404
2017-11-30 06:18:31 +00:00
Rafael Espindola bdcfb178b5 Delete dead code.
llvm-svn: 319403
2017-11-30 05:52:42 +00:00
Rafael Espindola 8ce6db0c4b Error instead of ignoring broken debug info.
Thanks to Davide for noticing.

llvm-svn: 319401
2017-11-30 05:37:35 +00:00
Rui Ueyama 1bf618a9da Remove unused condition.
Looks like all tests pass without this `isLocal()`.

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

llvm-svn: 319395
2017-11-30 01:08:09 +00:00
Rui Ueyama 8ed548a481 Fix typo.
llvm-svn: 319394
2017-11-30 01:04:26 +00:00
Rafael Espindola dfebd3601d Use Symbol::File directly.
We are already paying the cost of storing a InputFile in every
Symbol, so use it uniformly.

llvm-svn: 319378
2017-11-29 22:47:35 +00:00
Rafael Espindola 07b7ee4394 Simplify. NFC.
llvm-svn: 319370
2017-11-29 22:20:03 +00:00
Rafael Espindola f893396d1b Don't crash on broken debug info.
llvm-svn: 319367
2017-11-29 22:09:16 +00:00
Rafael Espindola cf5dc9f24c Replace a dyn_cast with a cast. NFC.
llvm-svn: 319361
2017-11-29 21:29:52 +00:00
Rafael Espindola ea5610648c Replace another dyn_cast with a cast.
llvm-svn: 319335
2017-11-29 18:43:34 +00:00
Rafael Espindola d42f7e5cae Replace a dyn_cast with a cast.
It is always accessed, so there is no value in a dyn_cast.

llvm-svn: 319334
2017-11-29 18:32:57 +00:00
Peter Smith 31dddc97ae [ELF][AArch64] Add support for AArch64 range thunks.
The AArch64 unconditional branch and branch and link instructions have a
maximum range of 128 Mib. This is usually enough for most programs but
there are cases when it isn't enough. This change adds support for range
extension thunks to AArch64. For pc-relative thunks we follow the small
code model and use ADRP, ADD, BR. This has a limit of 4 gigabytes.

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

llvm-svn: 319307
2017-11-29 11:15:12 +00:00
Peter Smith 8fca2e12ab [ELF] Make sure SHT_ARM_ATTRIBUTES is only recognized by Arm Targets
The SHT_ARM_ATTRIBUTES section type is in the processor specific space
adding guard to make sure that it is only recognized when EMachine is
EM_ARM.

llvm-svn: 319304
2017-11-29 10:20:46 +00:00
Rafael Espindola 09b53f6fd8 Delete dead code. NFC.
llvm-svn: 319274
2017-11-29 01:55:03 +00:00
Rafael Espindola b262cbe6b7 Replace copyFrom with memcpy.
It was only used for --wrap and I don't think the fields with special
treatment had a meaningful impact on that feature.

llvm-svn: 319265
2017-11-29 00:31:39 +00:00
Rafael Espindola 97d05f95b9 Copy the visibility in copyFrom.
This is simpler and matches bfd's behavior on the changed test.

llvm-svn: 319248
2017-11-28 23:06:09 +00:00
Rui Ueyama 2017d52b54 Move Memory.{h,cpp} to Common.
Differential Revision: https://reviews.llvm.org/D40571

llvm-svn: 319221
2017-11-28 20:39:17 +00:00
Rafael Espindola 1d4b3023dc Bring back r319008.
This includes a fix to mark copy reloc aliases as used.

Original message:

[ELF] Do not keep symbols if they referenced only from discarded sections.

This patch also ensures that in case of "--as-needed" is used,
DT_NEEDED entries are not created if they are required only by
these eliminated symbols.

llvm-svn: 319215
2017-11-28 20:17:58 +00:00
Rui Ueyama 3e03944f02 Factor out more code to Common/Args.cpp.
Differential Revision: https://reviews.llvm.org/D40540

llvm-svn: 319211
2017-11-28 19:58:45 +00:00
Rafael Espindola b45c164fc2 Handle binding in --wrap.
Copying the binding seems reasonable and matches the bfd behavior.

llvm-svn: 319196
2017-11-28 18:02:22 +00:00
Peter Smith 57eb046984 [ELF] Read ARM BuildAttributes section to determine supported features.
lld assumes some ARM features that are not available in all Arm
processors. In particular:
- The blx instruction present for interworking.
- The movt/movw instructions are used in Thunks.
- The J1=1 J2=1 encoding of branch immediates to improve Thumb wide
  branch range are assumed to be present.

This patch reads the ARM Attributes section to check for the
architecture the object file was compiled with. If none of the objects
have an architecture that supports either of these features a warning
will be given. This is most likely to affect armv6 as used in the first
Raspberry Pi.

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

llvm-svn: 319169
2017-11-28 13:51:48 +00:00
Igor Kudrin a46522f369 Revert r319008, "[ELF] Do not keep symbols if they referenced only from discarded sections."
and r319051, "Add a missing test."

r319008 broke the LTO bots;
r319051 depends on changes in r319008.

llvm-svn: 319154
2017-11-28 08:39:40 +00:00
Martin Storsjo 2d614ced55 Fix shared library builds of lld, broken in SVN r319138
llvm-svn: 319152
2017-11-28 08:12:03 +00:00
Rui Ueyama 53fe469162 Factor out common code to Common/Strings.cpp.
Differential Revision: https://reviews.llvm.org/D40530

llvm-svn: 319138
2017-11-28 02:15:26 +00:00
Rafael Espindola 9e3381e8dc Store the real binding of shared symbols.
Currently we mark every shared symbol as STB_WEAK.

That is a hack to make it easy to decide when a .so is needed or not
because of a reference to a given symbol.

That hack leaks when we create copy relocations as shown by the update
to relocation-copy-alias.s.

This patch stores the original binding when we first read a shared
symbol. We still have to update the binding to weak if we see a weak
undef, but I find the logic easier to read where it is now.

llvm-svn: 319127
2017-11-28 01:04:51 +00:00
Sam Clegg a80d94d52b Rename `Symtab` private memory to avoid confusion with global `Symtab`
This is also consistent with SymVector that exists in COFF port
and soon to be added to the wasm port.

Split off as part of https://reviews.llvm.org/D40371

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

llvm-svn: 319113
2017-11-27 23:16:06 +00:00
Sam Clegg e2ed092e01 Consistent use of <internal> when displaying internal symbol names
Differential Revision: https://reviews.llvm.org/D40510

llvm-svn: 319108
2017-11-27 22:49:16 +00:00
Peter Smith 2809926c4d [ELF][ARM] Refine check for when undefined weak needs a Thunk
When an undefined weak reference has a PLT entry we must generate a range
extension thunk for any B or BL that can't reach the PLT entry.

This change explicitly looks for whether a PLT entry exists rather than
assuming that weak references never need PLT entries unless Config->Shared
is in operation. This covers the case where we are linking an executable
with dynamic linking, hence a PLT entry will be needed for undefined weak
references. This case comes up in real programs over 32 Mb in size as there
is a B to a weak reference __gmon__start__ in the Arm crti.o for glibc.

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

llvm-svn: 319020
2017-11-27 11:49:18 +00:00
Igor Kudrin 40007586fc [ELF] Do not keep symbols if they referenced only from discarded sections.
This patch also ensures that in case of "--as-needed" is used,
DT_NEEDED entries are not created if they are required only by
these eliminated symbols.

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

llvm-svn: 319008
2017-11-27 05:51:10 +00:00
Rafael Espindola f8e405db46 Delete dead code.
The parent constructor is already setting the binding.

llvm-svn: 318962
2017-11-24 19:06:14 +00:00
Peter Smith 80e4be7eae [ELF] Rename .bss.rel.ro to .data.rel.ro.bss for some Linker Scripts.
LLD uses .bss.rel.ro for read-only copy relocations whereas the ld.bfd and
gold linkers use .data.rel.ro. In some linker scripts including ld.bfd's
internal linker script, the relro sections are placed sequentially assuming
.data.rel.ro is used. LLD's use of .bss.rel.ro means that the copy
relocations get matched into the .bss section causing the relro sections to
be non-contiguous.

This change checks for a .data.rel.ro OutputSection when a linker script
with the SECTIONS command is used. The section will match in the
.data.rel.ro output section and will maintain contiguous relro.

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

Fixes PR35265

llvm-svn: 318940
2017-11-24 08:48:29 +00:00
Rui Ueyama 15475e92c6 Use lambdas to store lazy-evaluated .dynamic entries.
Differential Revision: https://reviews.llvm.org/D40338

llvm-svn: 318938
2017-11-24 02:15:51 +00:00
Peter Smith c076b280df [ELF] Skip over empty sections when checking for contiguous relro
When checking for contiguous relro sections we can skip over empty sections.
If there is an empty non-relro section in the middle of a contiguous block
of relro sections then it cannot be written to so it is safe to include in
PT_GNU_RELRO header. If there is a contiguous block of empty relro sections
then no PT_GNU_RELRO header is required for them.

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

llvm-svn: 318924
2017-11-23 15:10:00 +00:00
Peter Smith dc3bd88b22 [ELF] Give error message when relro sections are not contiguous.
If a linker script is used that names linker generated synthetic sections
it is possible that the OutputSections for which isRelroSection() is true
are not contiguous. When the relro sections are not contiguous we cannot
describe them with a single PT_GNU_RELRO PHDR. Unfortunately at least one
contemporary dynamic loader only supports one PT_GNU_RELRO PHDR so we
cannot output more than one of these PHDRs. As not including relro
sections in the PHDR will lead to security sensitive sections being
writeable we choose to give an error message instead.

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

[ELF] Skip over empty sections when checking for contiguous relro

llvm-svn: 318920
2017-11-23 14:53:10 +00:00
Rafael Espindola de56343cf0 Simplify as-needed handling.
This is a reduction of a patch by Rui Ueyama.

llvm-svn: 318852
2017-11-22 17:50:42 +00:00
Simon Atanasyan 167306a56c [MIPS] Write PLT0 entry in case of linking N64 ABI code
llvm-svn: 318831
2017-11-22 12:34:29 +00:00
James Henderson f70c5beb22 [ELF] Fix DT_MIPS_LOCAL_GOTNO value for thunks and linker scripts
The MIPS GOT section has a number of local entries based on the number of pages
needed for output sections referenced by GOT page relocations. The number is
recorded in the DT_MIPS_LOCAL_GOTNO dynamic section tag. However, the dynamic tag
is added before assignAddresses has been called, meaning that any section size used
to calculate the value will not include size modifications caused by, for example,
linker scripts and thunks.

This change moves the calculation of DT_MIPS_LOCAL_GOTNO until writeTo, by which
time the output section sizes have been finalized.

Reviewers: ruiu, rafael

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

llvm-svn: 318828
2017-11-22 12:04:21 +00:00
Alexander Richardson d642494828 [ELF] Fix rewriting of --sysroot in reproducer tars
Summary:
I noticed that the reproducers files I was getting from building CheriBSD
didn't work because the --sysroot option was not being rewritten. I've
updated the test to also verify that the rewritten path matches uses a
FileCheck capature instead of a {{.+}} regex

Reviewers: ruiu

Reviewed By: ruiu

Subscribers: llvm-commits, emaste

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

llvm-svn: 318656
2017-11-20 15:49:48 +00:00
Alexander Richardson 1de78471f5 [ELF] Fall back to search dirs for linker scripts specified with -T
Summary:
This matches the behaviour of ld.bfd:
https://sourceware.org/binutils/docs/ld/Options.html#Options

If scriptfile does not exist in the current directory, ld looks for it in
the directories specified by any preceding '-L' options. Multiple '-T'
options accumulate.

Reviewers: ruiu, grimar

Reviewed By: ruiu, grimar

Subscribers: emaste, llvm-commits

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

llvm-svn: 318655
2017-11-20 15:43:20 +00:00
Alexander Richardson f463042312 [ELF][MIPS] Fix crash in LLD when linking code that needs PIC thunks
Summary:
The bug triggers when the following conditions are met:
    - A thunk is created in a given input section S
    - A linker script is specified
    - There is at least one matcher in the linker script .text section output
      that does not match any of the sections in the input files, before the matcher
      that matches section S.

The issue was found when linking the FreeBSD kernel for MIPS when built
with -fPIC. Patch by Alfredo Mazzinghi.

Reviewers: ruiu, psmith, atanasyan

Reviewed By: ruiu

Subscribers: peter.smith, emaste, sdardis, krytarowski, llvm-commits

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

llvm-svn: 318653
2017-11-20 15:37:19 +00:00
Rafael Espindola d0929caa21 Don't use llvm/Config/config.h.
It is private to llvm. Instead use llvm/Config/llvm-config.h and check
LLVM_ON_UNIX. That is the same guard that clang uses before including
unistd.h.

llvm-svn: 318558
2017-11-17 21:40:38 +00:00
George Rimar 5f928ff133 [ELF] - Teach LLD to use information from .debug_str for error reporting.
Recently we teached LLD to report line numbers for duplicate variables
definitions, though currently LLD is unable to do that for case when
strings are not built in .debug_info, but stored in .debug_str instead.
That is because out LLDDwarfObj does not handle .debug_str yet. 
Patch fixes that.

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

llvm-svn: 318519
2017-11-17 11:57:47 +00:00
George Rimar 80355234f7 [ELF] - Allow applying SHF_MERGE optimization for relocatable output.
This fixes PR35223.

Here I enabled SHF_MERGE section content merging for -r like
we do for regular linking.

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

llvm-svn: 318516
2017-11-17 11:27:57 +00:00
Rui Ueyama 11adb536e1 Ignore -no-ctors-in-init-array.
Patch by dxf@google.com.

llvm-svn: 318513
2017-11-17 08:17:36 +00:00
Rui Ueyama 7212f0ff11 Add a missing #include.
llvm-svn: 318512
2017-11-17 08:17:21 +00:00
Rafael Espindola bec3765bea Remove IsLocal.
Since we always have Binding in the current symbol design IsLocal is
redundant.

llvm-svn: 318497
2017-11-17 01:37:50 +00:00
George Rimar 661a2c1970 [ELF] - Don't emit broken relocations for SHF_MERGE sections when --emit-relocs is used.
Previously our relocations we rewrote were broken for that case.
We emited incorrect addend and broken relocation info field
because did not produce section symbol for mergeable synthetic sections.

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

llvm-svn: 318394
2017-11-16 12:33:36 +00:00
Rafael Espindola 8bc2a19ef8 Drop conflicting sh_entsize values.
An output section can include elements from two input sections with
different sh_entsize. When that happens the output section itself
should not have a sh_entsize.

llvm-svn: 318311
2017-11-15 17:35:22 +00:00
Rafael Espindola 3f0b575363 Remove an unnecessary constraint.
Our current implementation of SHF_MERGE can already handle over
aligned elements.

llvm-svn: 318310
2017-11-15 17:31:27 +00:00
Rafael Espindola a5d43d004a Propagate sh_entsize out.
No difference in practice other than having sh_entsize in the output.

This should simplify the patch for handling SHF_MERGE in -r.

Based on a patch by George Rimar.

llvm-svn: 318306
2017-11-15 16:56:20 +00:00
Simon Atanasyan cfa8aa7edb [MIPS] Set STO_MIPS_MICROMIPS flag and less-significant bit for microMIPS symbols
microMIPS symbols including microMIPS PLT records created for regular
symbols needs to be marked by STO_MIPS_MICROMIPS flag in a symbol table.
Additionally microMIPS entries in a dynamic symbol table should have
configured less-significant bit. That allows to escape teaching a
dynamic linker about microMIPS symbols.

llvm-svn: 318097
2017-11-13 22:40:36 +00:00
Rafael Espindola a4884cc38f Try harder to delete the temporary file.
It is really hard to cover restarts in a debugger, SIGKILL or power
failures. I will try to handle them in a followup patch, but it will
not support all the systems lld has to run on.

RemoveFileOnSignal takes care of crashes.

So what is left is making sure all regular exits delete the file. This
patch does that by moving the buffer to error handling. That is a bit
of a hack, but seemed better than to generalize it to take a callback on
construction.

I will implement this on COFF on the next patch.

llvm-svn: 318060
2017-11-13 18:06:43 +00:00
Rafael Espindola 99f9e1373b Simplify. NFC.
copyFrom doesn't copy the Binding, so this was a nop.

llvm-svn: 317965
2017-11-11 01:59:47 +00:00
Rafael Espindola 5c22661255 Don't produce a duplicated __wrap_foo if there is no __real_foo.
This fixes a regression from r317426.

llvm-svn: 317956
2017-11-11 00:53:52 +00:00
Simon Atanasyan 509834d7d3 [MIPS] Fix calculation of the R_MICROMIPS_LO16 / HI16 relocations
llvm-svn: 317781
2017-11-09 12:10:14 +00:00
Simon Atanasyan 3a7044ef78 [MIPS] Setup less-significant bit in a symbol value in microMIPS thunks
The less-significant bit signals about microMIPS code for jump/branch instructions.

llvm-svn: 317778
2017-11-09 10:42:22 +00:00
Simon Atanasyan 5a4e21364c [MIPS] Setup less-significant bit in the .got and .got.plt entries in case of microMIPS code
The less-significant bit signals about microMIPS code for jump/branch
instructions.

llvm-svn: 317741
2017-11-08 23:34:34 +00:00
Rafael Espindola 85b8d0ca7c Handle "-" in tryCreateFile.
Otherwise we would fail with -M if the we didn't have write
permissions to the current directory.

llvm-svn: 317740
2017-11-08 23:07:32 +00:00
Rui Ueyama 1e8813c6b4 Remove a given file even if openFileForRead fails.
I think the constract of this function is to remove a file in some way,
whether in background or in foreground. This patch makes sure that it
removes a given file.

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

llvm-svn: 317663
2017-11-08 04:22:40 +00:00
Rafael Espindola 5f903f3848 Update for llvm change.
llvm-svn: 317657
2017-11-08 01:50:34 +00:00
Rafael Espindola f7a57294d7 Update for llvm change.
llvm-svn: 317650
2017-11-08 01:05:52 +00:00
Rafael Espindola 367ca01478 Fix unused variable error.
llvm-svn: 317637
2017-11-07 23:43:01 +00:00
Rafael Espindola 37a575f8b6 Delete dead code.
llvm-svn: 317633
2017-11-07 23:12:41 +00:00
Rafael Espindola c78890640e Use std::thread directly.
Now that this code is posix only we don't need to use runBackground.

llvm-svn: 317632
2017-11-07 23:09:54 +00:00
Rafael Espindola 1501a26b39 Simplify unlinkAsync.
It used to rename the file before removing it in a background thread.

A simpler way to delay the removal is simply to open the file.

Since this is a posix specific issue, simplify the windows
implementation.

llvm-svn: 317631
2017-11-07 23:01:37 +00:00
Rafael Espindola 03e4ae42d5 Delete unused constructor.
llvm-svn: 317625
2017-11-07 22:30:53 +00:00
Paul Robinson e5400f8a6e [DWARFv5] Support DW_FORM_strp in the .debug_line header.
Supporting this form in .debug_line.dwo will be done as a follow-up.

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

llvm-svn: 317607
2017-11-07 19:57:12 +00:00
Rafael Espindola fcd7494832 Use Path instead of Config->OutputFile. NFC.
This function is always called with Config->OutputFile. That is not
obvious from reading the function. It should always use Path or take
no argument and always use Config->OutputFile.

llvm-svn: 317536
2017-11-07 02:00:51 +00:00
Rui Ueyama 989b1fcb01 Remove dead code.
llvm-svn: 317530
2017-11-07 00:26:04 +00:00
Rui Ueyama d5e2e8393b Report an error if an inferred alignment for a shared symbol is too large.
Differential Revision: https://reviews.llvm.org/D39697

llvm-svn: 317528
2017-11-07 00:12:05 +00:00
Rui Ueyama 7957b08e87 Move MIPS-specific code from Symbols.cpp to MIPS.cpp.
We have a lot of "if (MIPS)" conditions in lld because the MIPS' ABI
is different at various places than other arch's ABIs at where it
don't have to be different, but we at least want to reduce MIPS-ness
from the regular classes.

llvm-svn: 317525
2017-11-07 00:04:22 +00:00
Peter Smith a2db4349d5 [ELF] Fix typo in comment for getISDThunkSec [NFC]
The ISR in the comment should read ISD for InputSectionDescription. The use
of ISR (InputSectionRange) was from the original implementation that did not
use the sections from InputSectionDescription directly.

llvm-svn: 317469
2017-11-06 10:36:18 +00:00
Peter Collingbourne 38082ac62d ELF: Remove SymbolTable::SymIndex class.
The Traced flag is unnecessary because we only need to set the index
to -1 to mark a symbol for tracing.

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

llvm-svn: 317450
2017-11-06 04:58:04 +00:00
Peter Collingbourne b472aa0268 ELF: Remove function Symbol::isInCurrentOutput().
This function is now equivalent to isDefined().

llvm-svn: 317449
2017-11-06 04:39:07 +00:00
Peter Collingbourne e9a9e0a1e7 ELF: Merge DefinedRegular and Defined.
Now that DefinedRegular is the only remaining derived class of
Defined, we can merge the two classes.

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

llvm-svn: 317448
2017-11-06 04:35:31 +00:00
Peter Collingbourne 6c55a70838 ELF: Remove DefinedCommon.
Common symbols are now represented with a DefinedRegular that points
to a BssSection, even during symbol resolution.

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

llvm-svn: 317447
2017-11-06 04:33:58 +00:00
Rui Ueyama 32665f702e Do not consider Shared symbols as defined symbols.
I don't remember why I made shared symbols one type of defined symbols.
Shared symbols aren't undefined, so it could be considered defined, but
categorizing three symbols as:

 - defined
   - really defined
   - shared
 - undefined

is not as intuitive as

 - defined
 - shared
 - undefined

to me. So, in this patch, I made a change to stop handling shared
symbols as defined symbols.

Surprisingly, I didn't have to update any tests for this change.

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

llvm-svn: 317446
2017-11-06 04:13:24 +00:00
Rui Ueyama 5c4cb8a9e3 Inline a small function.
llvm-svn: 317428
2017-11-04 23:57:51 +00:00
Rui Ueyama aa8523e4b6 Move OutputSectionFactory to LinkerScript.cpp. NFC.
That class is used only by LinkerScript.cpp, so we should move it to
that file. Also, it no longer has to be a "factory" class. It can just
be a non-member function.

llvm-svn: 317427
2017-11-04 23:54:25 +00:00
Rui Ueyama dc0b0b0df3 Rewrite addSymbolWrap and applySymbolWrap. NFCI.
r317396 changed the way how we handle the -defsym option. The option is
now handled using the infrastructure for the linker script.

We used to handle both -defsym and -wrap using the same set of functions
in the symbol table. Now, we don't need to do that.

This patch rewrites the functions so that they become more straightforward.
The new functions directly handle -wrap rather than abstract it.

llvm-svn: 317426
2017-11-04 23:09:43 +00:00
Rui Ueyama 45b81402e3 Remove dead code and update comment.
llvm-svn: 317425
2017-11-04 22:32:56 +00:00
George Rimar 343e8227b7 [ELF] - Stop using SectionKey for creating output sections.
Stop using SectionKey for creating output sections.

Initially SectionKey was designed because we merged section with
use of Flags and Alignment fields. Currently LLD merges them by name only,
except the case when -relocatable output is produced. In that case
we still merge sections only with the same flags and alignment.
There is probably no issue at all to stop using Flags and Alignment for -r and
just disable the merging in that case.

After doing that change we can get rid of using SectionKey. That is not only
simplifies the code, but also gives some perfomance boost.

I tried to link chrome and mozilla, results are next:
* chrome link time goes from 1,666750355s to 1,551585364s, that is about 7%.
* mozilla time changes from 3,210261947 to 3,153782940, or about 2%.

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

llvm-svn: 317406
2017-11-04 09:11:27 +00:00
George Rimar ddd2424929 [ELF] - Fix error reporting with --strip-debug/--strip-all.
Currently LLD tries to use information about functions and variables location
taking it from debug sections. When --strip-* is given we discard such sections
and that breaks error reporting.
Patch stops discarding such sections and just removes them from InputSections list.

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

llvm-svn: 317405
2017-11-04 08:20:30 +00:00
Petr Hosek 8c7e8cce99 [ELF] Support expressions with -defsym option
Fixes PR34948.

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

llvm-svn: 317396
2017-11-04 02:03:58 +00:00
Rui Ueyama 48882249dd Rename Body -> Sym.
This patch is mechanically created by

  sed -i -e s/Body/Sym/g lld/ELF/*.{cpp,h}

and clang-format-diff.

llvm-svn: 317392
2017-11-04 00:31:04 +00:00
Rui Ueyama f483da0038 Rename replaceBody -> replaceSymbol.
llvm-svn: 317383
2017-11-03 22:48:47 +00:00
Zachary Turner a104d558d0 Fix variable name.
A mechanical rename caused an illegal name conflict between
a variable and a type, which was picked up by GCC.

llvm-svn: 317380
2017-11-03 22:33:49 +00:00
Bob Haarman 3fae5a6179 Revert "[ELF] - Teach LLD to use information from .debug_str for error reporting."
This reverts commit 00b7acb8f6c8a4663bb7c8396d217c210209b562.

It was causing some links to execute llvm_unreachable.

llvm-svn: 317378
2017-11-03 22:23:19 +00:00
Rui Ueyama f52496e1e0 Rename SymbolBody -> Symbol
Now that we have only SymbolBody as the symbol class. So, "SymbolBody"
is a bit strange name now. This is a mechanical change generated by

  perl -i -pe s/SymbolBody/Symbol/g $(git grep -l SymbolBody lld/ELF lld/COFF)

nd clang-format-diff.

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

llvm-svn: 317370
2017-11-03 21:21:47 +00:00
George Rimar 1d415c335e [ELF] - Remove excessive field initialization. NFC.
It is already initialized in header file to same value.

llvm-svn: 317320
2017-11-03 13:19:41 +00:00
George Rimar 31b6b0a820 [ELf] - Fix compilation after r317307.
Not sure why that seems did not break any llvm bots or
my windows local build, but is was required to fix compilation
breakage of my ubuntu build when using
gcc version 8.0.0 20171019 (experimental)

llvm-svn: 317317
2017-11-03 11:57:01 +00:00
George Rimar 8c0cdca99e [ELF] - Removed unused variable. NFC.
llvm-svn: 317316
2017-11-03 11:51:58 +00:00
George Rimar 8c825db25e [ELF] - Linkerscript: fixed non-determinism when handling MEMORY.
When findMemoryRegion do search to find a region for output section it
iterates over MemoryRegions which is DenseMap and so does not
guarantee iteration in insertion order. As a result selected region depends
on its name and not on its definition position
Testcase shows the issue, patch fixes it. Behavior after applying the patch
seems consistent with bfd.

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

llvm-svn: 317307
2017-11-03 08:21:51 +00:00
George Rimar cbed0e615b [ELF] - Teach LLD to strip .zdebug_* sections when --strip-debug/--strip-all is specified.
Currently we do not strip .zdebug_*, what looks wrong.
Also this simplifies the testcase we have for this options.

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

llvm-svn: 317306
2017-11-03 08:13:16 +00:00
George Rimar 5b262363b4 [ELF] - Teach LLD to use information from .debug_str for error reporting.
Recently we teached LLD to report line numbers for duplicate variables
definitions, though currently LLD is unable to do that for case when
strings are not built in .debug_info, but stored in .debug_str instead.
That is because out LLDDwarfObj does not handle .debug_str yet. 
Patch fixes that.

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

llvm-svn: 317305
2017-11-03 08:04:27 +00:00
George Rimar 1b45d377e3 [ELF] - Cleanup of processSectionCommands().
The way we handle ONLY_IF_RO/ONLY_IF_RW constraints in
processSectionCommands is a bit tricky. If input sections
does no satisfy given constraint we remove command from
commands list. It seems too complex, what we can do instead
is to make the OutputCommand empty. So that at later steps
LLD will remove it just like it deal with other empty output commands.
That allows to simplify the loop a bit.

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

llvm-svn: 317082
2017-11-01 08:40:28 +00:00
George Rimar 82f0c42dad [ELF] - Teach LLD to report line numbers for data symbols.
This is PR34826.

Currently LLD is unable to report line number when reporting
duplicate declaration of some variable.

That happens because for extracting line information we always use
.debug_line section content which describes mapping from machine
instructions to source file locations, what does not help for
variables as does not describe them.

In this patch I am taking the approproate information about
variables locations from the .debug_info section.

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

llvm-svn: 317080
2017-11-01 07:42:38 +00:00
Rui Ueyama 700b1f8a56 Add --no-omagic and --no-print-gc-sections.
llvm-svn: 317068
2017-11-01 02:04:43 +00:00
Rui Ueyama 95c142e208 Revert r316305: Remove a fast lookup table from MergeInputSection.
This reverts commit r316305 because performance regression was observed.

llvm-svn: 317026
2017-10-31 19:14:06 +00:00
James Henderson 50baa593f1 [ELF] Remove unnecessary function call. NFC.
StringTableSection does not implement postThunkContents, so calling it on these 
sections is pointless (it calls an empty virtual function), and we can remove it.

Reviewers: ruiu

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

llvm-svn: 317014
2017-10-31 17:28:44 +00:00
Rui Ueyama f1f00841d9 Merge SymbolBody and Symbol into one class, SymbolBody.
SymbolBody and Symbol were separated classes due to a historical reason.
Symbol used to be a pointer to a SymbolBody, and the relationship
between Symbol and SymbolBody was n:1.

r2681780 changed that. Since that patch, SymbolBody and Symbol are
allocated next to each other to improve memory locality, and they have
1:1 relationship now. So, the separation of Symbol and SymbolBody no
longer makes sense.

This patch merges them into one class. In order to avoid updating too
many places, I chose SymbolBody as a unified name. I'll rename it Symbol
in a follow-up patch.

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

llvm-svn: 317006
2017-10-31 16:07:41 +00:00
George Rimar f173a6309f [ELF] - Remove useless code. NFC.
We set Type and Flags inside OutputSection::addSection,
so this lines looks to be excessive.

llvm-svn: 317002
2017-10-31 14:21:30 +00:00
Oleg Ranevskyy 0cf24ed9aa [lld] Fix --exclude-libs broken when --whole-archive is used
Summary:
**Problem**
`--exclude-libs` does not work for static libraries affected by the `--whole-archive` option.

**Description**
`--exclude-libs` creates a list of static library paths and does library lookups in this list.
`--whole-archive` splits the static libraries that follow it into separate objects. As a result, lld no longer sees static libraries among linked files and does no `--exclude-libs` lookups.

**Solution**
The proposed solution is to make `--exclude-libs` consider object files too. When lld finds an object file it checks whether this file originates from an archive and, if so, looks the archive up in the `--exclude-libs` list.

Reviewers: ruiu, rafael

Reviewed By: ruiu

Subscribers: asl, ikudrin, llvm-commits, emaste

Tags: #lld

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

llvm-svn: 316998
2017-10-31 13:51:06 +00:00
George Rimar f9b04fd91f [ELF] - Simplify output section creation.
When there is no SECTION commands given, all sections are
technically orphans, but now we handle script orphans sections
and regular "orphans" sections for non-scripted case differently,
though we can handle them at one place.

Patch do that change.

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

llvm-svn: 316984
2017-10-31 10:31:58 +00:00
Jake Ehrlich 1128dc5e30 Give .note.gnu.build-id section alignment 4
All SHT_NOTE sections should have minimum alignment 4.

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

llvm-svn: 316961
2017-10-30 22:08:11 +00:00
Rafael Espindola 458173e802 Use 64 bits for the shared symbol size again.
We might want to drop support for such large symbols, but that should
be an explicit decision with a testcase.

llvm-svn: 316931
2017-10-30 17:43:16 +00:00
Rafael Espindola 566dbdc2fd Skip abs symbols when handling copy reloc aliases.
Since we now only check st_value, we have to consider the case where
the section index is special.

llvm-svn: 316928
2017-10-30 17:26:12 +00:00
George Rimar a6b1fbece1 [ELF] - Stop sorting input sections in createSections().
It does not seem that createSections() is a good place for
applying sorting. Patch changes code to do that inside
sortSections(), which looks more appropriate place.

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

llvm-svn: 316893
2017-10-30 10:12:49 +00:00
Rui Ueyama 27f95a9ac3 Remove unnecessary code.
Discarded section's Repl always points to itself, so returning
Sec is not different from returning Sec->Repl.

llvm-svn: 316880
2017-10-29 23:41:16 +00:00
Rui Ueyama ea1398eeb3 Move "Assigned" bit from SectionBase to InputSectionBase.
This bit is to manage whether an input section has already been assigned
to some output section by linker scripts or not. So it logically belongs
to InputSectionBase. SectionBase is a common base class for input and
output sections, so that wasn't the right place to define the bit.

llvm-svn: 316879
2017-10-29 23:32:23 +00:00
Rui Ueyama 63edd9f831 Remove an obscure comment.
llvm-svn: 316878
2017-10-29 22:38:00 +00:00
Peter Collingbourne e662436114 ELF: Correctly set edata if there are no .bss sections.
edata needs to be set to the end of the last mapped initialized
section. We were previously mishandling the case where there were no
non-mapped sections by setting it to the end of the last section in
the output file.

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

llvm-svn: 316877
2017-10-29 22:31:48 +00:00
Rui Ueyama da4d26dfa3 Initialize members not by assignment but by the member initializer list.
llvm-svn: 316876
2017-10-29 22:26:52 +00:00
Rui Ueyama b6f4e722b1 Fix ubsan error that shift amount 64 is too large.
llvm-svn: 316863
2017-10-29 16:49:42 +00:00
Rui Ueyama ac6472535b Remove a redundant temporary variable.
llvm-svn: 316862
2017-10-29 16:46:39 +00:00
Rui Ueyama da42135cef Fix ubsan error.
This patch fixes ubsan's "shift exponent 64 is too large for 32-bit type
'int'" error.

llvm-svn: 316849
2017-10-28 22:18:29 +00:00
Rui Ueyama bd730e3ca7 Reduce sizeof(Symbol) from 104 bytes to 88 bytes.
Finding aliases for shared symbols doesn't need st_shndx because
we can just compare st_value.

llvm-svn: 316848
2017-10-28 22:18:17 +00:00
Rui Ueyama 4a22edabec Do not handle DefinedCommon symbols in the MapFile writer.
Because of r314495 which converts DefinedCommon symbols to DefinedRegular
symbols, common symbols are no longer reachable to the MapFile writer.
So the code to handle common symbols is now dead.

llvm-svn: 316846
2017-10-28 21:11:38 +00:00
Rui Ueyama 1392e84caf Rename V -> Ver for readability.
llvm-svn: 316842
2017-10-28 20:16:11 +00:00
Rui Ueyama 7f9694a42d Pass symbol attributes instead of ElfSym to Shared symbol ctor.
This change allows us to use less templates for Shared symbol and
the functions that deals with shared symbols.

llvm-svn: 316841
2017-10-28 20:15:56 +00:00
Rui Ueyama f405b11224 Simplify error handling.
I don't think we have to aim for precise bug compatibility.
We can return a nullptr if a section is consumed by the linker, and
the rest should naturally work.

llvm-svn: 316817
2017-10-28 00:05:12 +00:00
Rui Ueyama da524d021a Remove unnecessary namespace specifiers.
llvm-svn: 316812
2017-10-27 23:29:58 +00:00
Rui Ueyama 7833afdd6b Inline trivial symbol constructors.
llvm-svn: 316811
2017-10-27 23:26:46 +00:00
Rui Ueyama bda337a8d8 Rename isInCurrentDSO -> isInCurrentOutput.
DSO is short for dynamic shared object, so the function name was a
little confusing because it sounded like it didn't work when we were
a creating statically-linked executable or something.

What we mean by "DSO" here is the current output file that we are
creating. Thus the new name. Alternatively, we could call it the current
ELF module, but "module" is a overloaded word, so I avoided that.

llvm-svn: 316809
2017-10-27 22:54:16 +00:00
Rafael Espindola bc2b1650d7 Handle all symbol types in --trace.
One case I was not sure was lazy symbols. bfd skips them completely,
but that is probably an artifact of how they implement archives.

llvm-svn: 316782
2017-10-27 18:30:11 +00:00
Peter Collingbourne 5c54f15c55 ELF: Add support for emitting dynamic relocations in the Android relocation packing format.
The Android relocation packing format is a more compact
format for dynamic relocations in executables and DSOs
that is based on delta encoding and SLEBs. An overview
of the format can be found in the Android source code:
https://android.googlesource.com/platform/bionic/+/refs/heads/master/tools/relocation_packer/src/delta_encoder.h

This patch implements relocation packing using that format.

This implementation uses a more intelligent algorithm for compressing
relative relocations than Android's own relocation packer. As a
result it can generally create smaller relocation sections than
that packer. If I link Chromium for Android targeting ARM32 I get a
.rel.dyn of size 174693 bytes, as compared to 371832 bytes with gold
and the Android packer.

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

llvm-svn: 316775
2017-10-27 17:49:40 +00:00
George Rimar 90dacc256f [ELF] - Ignore non-absolute R_386_GOTPC relocation in debug sections.
This is for PR34852.

GCC 8.0 or earlier have a bug that it emits R_386_GOTPC relocations
against _GLOBAL_OFFSET_TABLE for .debug_info. The bug seems to have
been fixed in 2017: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82630,
but we do not want LLD to report errors for such inputs.
In this patch we ignore such relocations.

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

llvm-svn: 316761
2017-10-27 11:49:24 +00:00
George Rimar 3396bf07a2 [ELF] - Stop naming relocation sections with first input section name.
It was reported (https://reviews.llvm.org/D38724#902841) that when we use 
-ffunction-sections --emit-relocs build, REL[A] output section receives the name of first
input section, like .rela.text.first_function_in_text rather than .rela.text.

It is probably not really an issue as sh_info still points to correct target section, but
it does not look clean in output and allows internal section name to leak there,
what at least looks confusing and is not consistent with ld.bfd.

Patch changes this behavior so that target output section name is used as a base.

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

llvm-svn: 316760
2017-10-27 11:38:31 +00:00
George Rimar 96b1157814 [ELF] - Simplify reporting of garbage collected sections.
This moves reporting of garbage collected sections right after
we do GC. That simplifies things.

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

llvm-svn: 316759
2017-10-27 11:32:22 +00:00
Peter Smith 6c9df3fce5 [ELF] Add support for multiple passes to createThunks()
This change allows Thunks to be added on multiple passes. To do this we must
merge only the thunks added in each pass, and deal with thunks that have
drifted out of range of their callers.

A thunk may end out of range of its caller if enough thunks are added in
between the caller and the thunk. To handle this we create another thunk.

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

llvm-svn: 316754
2017-10-27 09:07:10 +00:00
Peter Smith 75030b6d56 [ELF] Introduce range extension thunks for ARM
This change adds initial support for range extension thunks. All thunks must
be created within the first pass so some corner cases are not supported. A
follow up patch will add support for multiple passes.

With this change the existing tests arm-branch-error.s and
arm-thumb-branch-error.s now no longer fail with an out of range branch.
These have been renamed and tests added for the range extension thunk.

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

llvm-svn: 316752
2017-10-27 09:04:11 +00:00
Peter Smith f0c70f8d34 [ELF] Pre-create ThunkSections at Target specific intervals
When an OutputSection is larger than the branch range for a Target we
need to place thunks such that they are always in range of their caller,
and sufficiently spaced to maximise the number of callers that can use
the thunk. We use the simple heuristic of placing the
ThunkSection at intervals corresponding to a target specific branch range.
If the OutputSection is small we put the thunks at the end of the executable
sections.

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

llvm-svn: 316751
2017-10-27 08:58:28 +00:00
Peter Smith 4a8e11595c [ELF] Record created ThunkSections in InputSectionDescription [NFC].
Instead of maintaining a map of the std::vector to ThunkSections, record the
ThunkSections directly in InputSectionDescription.

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

llvm-svn: 316750
2017-10-27 08:56:20 +00:00
Rui Ueyama 0f46e45b03 Simplify logic to find the last executable segment. NFC.
llvm-svn: 316742
2017-10-27 05:08:39 +00:00
Rui Ueyama df15559c80 Fix typo.
llvm-svn: 316741
2017-10-27 04:59:33 +00:00
Rui Ueyama f9c66e4b80 Remove a boolean flag `Create`. NFC.
llvm-svn: 316738
2017-10-27 04:15:28 +00:00
Rui Ueyama 79048e4c5c Define a file-local write32 as a wrapper for endian::write32.
llvm-svn: 316736
2017-10-27 03:59:34 +00:00
Rui Ueyama 25f30880f5 Do not use InputSection::getFile<ELFT>() if InputSection::File suffices. NFC.
llvm-svn: 316735
2017-10-27 03:25:04 +00:00
Rui Ueyama 02551abdca De-template EhFrameHdr. NFC.
llvm-svn: 316734
2017-10-27 03:14:24 +00:00
Rui Ueyama 8629752c04 De-template EhReader. NFC.
llvm-svn: 316733
2017-10-27 03:14:09 +00:00
Rui Ueyama d6b7a390d8 De-template elf::getObjMsg. NFC.
llvm-svn: 316732
2017-10-27 03:13:54 +00:00
Rui Ueyama 572247f22e De-template EhFrameSection. NFC.
llvm-svn: 316731
2017-10-27 03:13:39 +00:00
Rui Ueyama c055225c6b Make EhFrameHdr pull data from EhFrameSection. NFC.
Previously, EhFrameSection pushes data to EhFrameHdr by calling addFde
function. By making EhFrameHdr pull data from EhFrameSection, we can
eliminate a member from EhFrameHdr.

llvm-svn: 316730
2017-10-27 03:13:24 +00:00
Rui Ueyama 076e2bb10e De-template a few functions. NFC.
llvm-svn: 316729
2017-10-27 03:13:09 +00:00
Rui Ueyama 88f0568ce5 Include an error code when getRandomBytes failed.
In a rare situation, the function can fail (e.g. it fails if it cannot
open /dev/urandom.)

llvm-svn: 316726
2017-10-27 01:25:29 +00:00
Rui Ueyama ed55e6c64f Remove an unnecessary inheritance.
llvm-svn: 316716
2017-10-26 23:54:00 +00:00
Rui Ueyama bfa84325ee Simplify.
Because of the same reason as r316600, I don't think we need a guard
against empty relocations.

llvm-svn: 316710
2017-10-26 22:30:25 +00:00
Rui Ueyama 6e5fda9db8 Move bit operations to a new file, ELF/Bits.h.
llvm-svn: 316707
2017-10-26 21:37:17 +00:00
George Rimar d605f414db [ELF] - Dedupliсate FDEs when sections are ICFed.
When LLD do ICF for 2 identical sections it leaves 2 duplicate entries in .eh_frame
pointing to the same address. After that it fixes .eh_frame_header's header,
so that it says it contains single FDE, though section itself contains 2 
(it contains garbage data at tail).

As a result excessive entries in .eh_frame and excessive dummy data in .eh_frame_header
emited to output. Patch fixes that. This is PR34518.

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

llvm-svn: 316648
2017-10-26 09:13:19 +00:00
Bob Haarman b8a59c8aa5 [lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.

Reviewers: ruiu

Reviewed By: ruiu

Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits

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

llvm-svn: 316624
2017-10-25 22:28:38 +00:00
Rui Ueyama 8e38ea8b9e Unassign sections if they are "assigned" to /DISCARD/.
"/DISCARD/" is a special section name in the linker script to discard
input sections. Previously, we handled it as if it were a real section,
so an input section can be assigned but dead. However, allowing sections
to be

 - assigned and alive (will be emitted),
 - not assigned and dead (removed), or
 - assigned but dead (???)

feels logically wrong and practically error-prone. This patch removes
the last combination of the states.

llvm-svn: 316622
2017-10-25 22:03:21 +00:00
Rui Ueyama cfa171d68c Simplify.
ArrayRef<T>() equals to ArrayRef<T>(nullptr, 0), so it looks like
we don't need to handle size 0 as a special case.

llvm-svn: 316600
2017-10-25 18:09:54 +00:00
George Rimar 9814d15136 [ELF] - Implement --orphan-handling option.
It is PR34946.

Spec (http://man7.org/linux/man-pages/man1/ld.1.html) tells about
--orphan-handling=MODE, option where MODE can be one of four:
"place", "discard", "warn", "error".
Currently we already report orphans when -verbose given,
what becomes excessive with option implemented.

Patch stops reporting orphans when -versbose is given,
and support "place", "warn" and "error" modes.
It is not yet clear that "discard" mode is useful so it is not supported.

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

llvm-svn: 316583
2017-10-25 15:20:30 +00:00
George Rimar f22ec9ddf6 [ELF] - Linkerscript: fix issue with SUBALIGN.
This is PR34886.

SUBALIGN command currently triggers failture if result expression
is zero. Patch fixes the issue, treating zero as 1, what is consistent with
other places and ELF spec it seems.

Patch also adds "is power of 2" check for this and other expressions
returning alignment.

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

llvm-svn: 316580
2017-10-25 14:50:51 +00:00
Rui Ueyama d5135b7e99 Remove obsolete comment.
llvm-svn: 316520
2017-10-24 21:44:43 +00:00
Rui Ueyama ac7aaeb770 Use ArgList::hasFlag().
I just didn't know that ArgList had that function.

llvm-svn: 316499
2017-10-24 20:59:55 +00:00
Rui Ueyama f8f4620dc6 Move comment to the place where it makes more sense.
llvm-svn: 316491
2017-10-24 20:11:07 +00:00
Rui Ueyama 7b4eddf0ea Improve comment.
llvm-svn: 316489
2017-10-24 19:53:51 +00:00
Rui Ueyama 51f604ce66 Simplify.
llvm-svn: 316486
2017-10-24 19:40:03 +00:00
Konstantin Zhuravlyov b71d1b8a54 LLD/ELF/AMDGPU: Process AMDGPU-specific e_flags
Differential Revision: https://reviews.llvm.org/D39140

llvm-svn: 316483
2017-10-24 19:05:32 +00:00
Konstantin Zhuravlyov e7f1734f1a LLD/ELF: Allow targets to set e_flags
Differential Revision: https://reviews.llvm.org/D39139

llvm-svn: 316460
2017-10-24 17:01:40 +00:00
Rafael Espindola 9c8f853ca9 Refactor duplicated comment. NFC.
llvm-svn: 316455
2017-10-24 16:27:31 +00:00
George Rimar f4ca4a6fa1 [ELF] - Do not collect SHT_REL[A] sections unconditionally when --gc-sections and --emit-relocs used together.
This is "Bug 34836 - --gc-sections remove relocations from --emit-relocs",

When --emit-relocs is used, LLD currently always drops SHT_REL[A] sections from
output if --gc-sections is present. Patch fixes the issue.

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

llvm-svn: 316418
2017-10-24 08:26:32 +00:00
Rafael Espindola 089dac7be1 Make Ctx a plain pointer again.
If a struct has a std::unique_ptr member, the logical interpretation
is that that member will be destroyed with the struct.

That is not the case for Ctx. It is has to be deleted earlier and its
lifetime is defined by the functions where the AddressState is
created.

llvm-svn: 316378
2017-10-23 21:12:19 +00:00
Konstantin Zhuravlyov 70abb6e204 LLD: Fix large integer implicitly truncated to unsigned type gcc warning
This fixes gcc warning.

Change by Brian Sumner

llvm-svn: 316365
2017-10-23 19:31:31 +00:00
Petr Hosek 2fd533db9f [ELF] When placing orphans, handle case when last section is dead
r315292 introduced a change that's supposed to consistently ignore
"dead" output sections when placing orphans. Unfortunately, that
change doesn't handle the special case when the orphan section is
second to last section and the last section is dead (e.g. because
it's being discarded) introducing a regression in some cases.

This change handles this case by using the same predicate when
checking the last section.

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

llvm-svn: 316307
2017-10-23 00:51:08 +00:00
Rui Ueyama 8faafa4fb1 Add R_PPC_ADDR16_HI relocation support
The support of R_PPC_ADDR16_HI improves ld compatibility and makes
things on par with RuntimeDyldELF that already implements this
relocation.

Patch by vit9696.

llvm-svn: 316306
2017-10-22 23:33:49 +00:00
Rui Ueyama d96724db42 Remove a fast lookup table from MergeInputSection.
We used to have a map from section piece offsets to section pieces
as a cache for binary search. But I found that the map took quite a
large amount of memory and didn't make linking faster. So, in this
patch, I removed the map.

This patch saves 566 MiB of RAM (2.019 GiB -> 1.453 GiB) when linking
clang with debug info, and the link time is 4% faster in that test case.

Thanks for Sean Silva for pointing this out.

llvm-svn: 316305
2017-10-22 23:02:07 +00:00
Rui Ueyama 53a9aff93e Simplify.
llvm-svn: 316281
2017-10-22 01:58:30 +00:00
Rui Ueyama 95bf509873 Assume that mergeable input sections are smaller than 4 GiB.
By assuming that mergeable input sections are smaller than 4 GiB,
lld's memory usage when linking clang with debug info drops from
2.788 GiB to 2.019 GiB (measured by valgrind, and that does not include
memory space for mmap'ed files). I think that's a reasonable assumption
given such a large RAM savings, so this patch.

According to valgrind, gold needs 3.54 GiB of RAM to do the same thing.

NB: This patch does not introduce a limitation on the size of
output sections. You can still create sections larger than 4 GiB.

llvm-svn: 316280
2017-10-21 23:20:13 +00:00
Rafael Espindola 849d499e6d Don't call buildSectionOrder multiple times.
This takes linking the linux kernel from 1.52s to 0.58s.

llvm-svn: 316251
2017-10-21 00:05:01 +00:00
Rafael Espindola 3558e24ae8 Remove unused argument.
llvm-svn: 316248
2017-10-20 23:28:19 +00:00
George Rimar 1c09dfe2bc [ELF] - Simplify. NFC.
llvm-svn: 316163
2017-10-19 15:03:59 +00:00
George Rimar da8808bf91 [ELF] - Removed outdated comment. NFC.
llvm-svn: 316078
2017-10-18 13:22:21 +00:00
George Rimar 0d6f30e80d [ELF] - Moved, fixed outdated comment. NFC.
It was at wrong place.

llvm-svn: 316077
2017-10-18 13:06:18 +00:00
George Rimar ee7c99b63a [ELF] - Make LinkerScript::assignOffsets private. NFC.
llvm-svn: 316073
2017-10-18 12:09:41 +00:00
George Rimar a4ca931336 [ELF] - Remove excessive helper. NFC.
Removes addRegular() helper that looks excessive from Writer.cpp.

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

llvm-svn: 316068
2017-10-18 08:36:47 +00:00
Rafael Espindola 06ea0ce1c9 Handle shared symbols in the gnu hash construction.
They are special in that they they need to be in the hash table iff we
are linking an executable that preempts them.

llvm-svn: 316065
2017-10-18 06:49:59 +00:00
Rafael Espindola 3038c6beb5 Revert "Bring back the rest of r315721."
This reverts commit r316051.

It fails on a bot.

llvm-svn: 316058
2017-10-18 03:06:45 +00:00
Rafael Espindola 07412df152 Bring back the rest of r315721.
The original patch was reported to break a libcxx bot. Unfortunately I
could not reproduce the issue and the bot log is not available anymore.

This patch changes the handling of shared symbols. I will watch the
bot and see it breaks.

Original message:
--------------------------------------------------------------
Handle shared and lazy symbol in the gnu hash construction.

We were not considering those symbols undefined and they could end up
in the end of the dynamic symbol table.
--------------------------------------------------------------

llvm-svn: 316051
2017-10-18 01:10:05 +00:00
Rafael Espindola 14381cf5a6 Bring back part of r315721.
The original patch was reported to break a libcxx bot. Unfortunately
I could not reproduce the issue and the bot log is not available anymore.

This patch changes the handling of just lazy symbols. I will watch the
bot and see it breaks.

Original message:
--------------------------------------------------------------
Handle shared and lazy symbol in the gnu hash construction.

We were not considering those symbols undefined and they could end up
in the end of the dynamic symbol table.
--------------------------------------------------------------

llvm-svn: 316004
2017-10-17 15:29:11 +00:00
Simon Atanasyan 38f7d433ef [mips] Provide more detailed comment. NFC
llvm-svn: 316003
2017-10-17 14:39:03 +00:00
George Rimar d30bd1321f [ELF] - Simplify. NFC.
llvm-svn: 316002
2017-10-17 14:31:29 +00:00
George Rimar 228340315d [ELF] - Removed unused class from Options.td. NFC.
llvm-svn: 315993
2017-10-17 12:23:51 +00:00
Tony Tye 394e1dde6e Add base relative relocation record that can be used for the following case (OpenCL example):
static __global int Var = 0; 
__global int* Ptr[] = {&Var};
...

In this case Var is a non premptable symbol and so its address can be used as the value of Ptr, with a base relative relocation that will add the delta between the ELF address and the actual load address. Such relocations do not require a symbol.

This also fixes LLD which was incorrectly generating a PCREL64 for this case.

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

llvm-svn: 315936
2017-10-16 20:46:53 +00:00
Konstantin Zhuravlyov 3f9b4b7c91 Revert r315877: Simplify.
It causes MSVC 2015 to emit "C4307: '-': integral constant overflow" warning

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

llvm-svn: 315926
2017-10-16 18:49:28 +00:00
Rui Ueyama 227cb6baef Remove Config::FirstElf.
llvm-svn: 315881
2017-10-15 21:43:09 +00:00
Rui Ueyama 7be790b1e0 Simplify.
llvm-svn: 315877
2017-10-15 21:07:25 +00:00
NAKAMURA Takumi 09c234f857 Revert rL315721, "Handle shared and lazy symbol in the gnu hash construction."
It broke check-libcxx with stage1-clang and stage1-lld.

llvm-svn: 315803
2017-10-14 12:21:49 +00:00
Rafael Espindola ea413cbacf Give shared symbols priority over lazy ones.
This matches the behavior of gold and bfd. It also matches lld's own
behavior when the shared symbol is seen first

llvm-svn: 315766
2017-10-13 21:52:33 +00:00
Rafael Espindola cb5623152d Initialize CopyRelSec.
It could be used uninitialized since r310079.

llvm-svn: 315764
2017-10-13 21:44:10 +00:00
Petr Hosek ac40140e24 [ELF] Recognize additional relocation types
These are generated by the linker itself and it shouldn't treat
them as unrecognized. This was introduced in r315552 and is triggering
an error when building UBSan shared library for i386.

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

llvm-svn: 315737
2017-10-13 19:30:00 +00:00
Rafael Espindola fd874c20ec Simplify. NFC.
llvm-svn: 315734
2017-10-13 19:02:08 +00:00
Rafael Espindola a7e3132d03 Handle shared symbols in --gc-sections.
We don't want to gc a section if it is used via __start_*, but we were
doing that if the same symbol was visible in a shared library.

llvm-svn: 315727
2017-10-13 18:32:16 +00:00
Bob Haarman 4f5c8c29ac [lld] Move Threads to Common
Summary:
This will allow using the functionality from other linkers. It is also
a prerequisite for sharing the error logging code.

Reviewers: ruiu

Reviewed By: ruiu

Subscribers: emaste, mgorny, llvm-commits

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

llvm-svn: 315725
2017-10-13 18:22:55 +00:00
Rafael Espindola bfc20343a3 Simplify. NFC.
llvm-svn: 315724
2017-10-13 18:18:36 +00:00
Rafael Espindola 8bf95e4678 Handle shared and lazy symbol in the gnu hash construction.
We were not considering those symbols undefined and they could end up
in the end of the dynamic symbol table.

llvm-svn: 315721
2017-10-13 18:07:11 +00:00
Shoaib Meenai ba23183ba7 [LLD] Fix typo. NFC
llvm-svn: 315675
2017-10-13 06:11:20 +00:00
Rui Ueyama fce36d1b87 Make it explicit that we are writing addends to target sections if REL.
Relocations.cpp is still head-scratching. Even though relocations are
processed by multiple functions, the functions are effectively one
gigantic function with lots of local and global shared states, because
they are really tightly coupled. It is really hard to predict whether
a change to a function will or will not affect other functions behaviors.

What I'm trying to do is to rewrite the code without breaking the
existing tests so that the code can tolerate a more aggressive
refactoring (i.e. splitting it to logically separated steps).

llvm-svn: 315673
2017-10-13 05:58:54 +00:00
Rui Ueyama 06b7299dc4 Destructure a boolean expression and add comment.
"IsWrite" variable didn't make sense and was misleading because
it became true even if a section is not writable.

llvm-svn: 315669
2017-10-13 04:04:10 +00:00
Rui Ueyama 662bb00028 Hide SymbolBody::IsLocal.
IsLocal member is initialized by the constructor and will never change.
So we don't want to make it directly accessible.

llvm-svn: 315667
2017-10-13 03:37:26 +00:00
Rui Ueyama c0f564815c Make a condition more explicit. NFC.
llvm-svn: 315666
2017-10-13 03:37:11 +00:00
Rui Ueyama ce2f5fdbb8 Add comment.
llvm-svn: 315664
2017-10-13 02:57:59 +00:00
Rui Ueyama 5a2b02c3df Simplify. NFCI.
llvm-svn: 315659
2017-10-13 01:42:27 +00:00
Rui Ueyama 17819c8bbb Slightly simplify code and add comment.
This is not a mechanical transformation. Even though I believe this
patch is correct, I'm not 100% sure if lld with this patch behaves
exactly the same way as before on all edge cases. At least all tests
still pass.

I'm submitting this patch because it took almost a day to understand
this function, and I don't want to lose it.

llvm-svn: 315658
2017-10-13 01:19:10 +00:00
Rafael Espindola 4516caef8e Use existing helper. NFC.
llvm-svn: 315654
2017-10-13 00:44:33 +00:00
Rui Ueyama 1861aa281d Simplify a boolean expression by the De Morgan's laws.
I don't really understand what exactly this expression means,
but at least I can mechanically transform it.

llvm-svn: 315653
2017-10-13 00:35:44 +00:00
Rafael Espindola 6f7ef54a86 Convert a few cases of isUndefined to isInCurrentDSO.
I don't think there is an observable difference. We now just avoid
doing silly things like setting versions in lazy symbols.

llvm-svn: 315650
2017-10-13 00:23:35 +00:00
Rafael Espindola af6c9edc4b Correctly handle shared and lazy symbols.
I will try to make this API a bit less error prone, but for now just
get another test passing.

llvm-svn: 315645
2017-10-13 00:06:38 +00:00
Shoaib Meenai 14cf51410b [lld] Fix typo. NFC
llvm-svn: 315626
2017-10-12 21:52:14 +00:00
Rui Ueyama d32f06c379 Add comment.
llvm-svn: 315625
2017-10-12 21:45:53 +00:00
Rui Ueyama ab295741dd Handle MIPS-specific addend rules in computeAddend().
This patch merges computeAddend and computeMipsAddend.

Getting an addend for a relocation is usually pretty easy:
it is either in the r_addend field (if RELA) or in a target
section (if REL).

However, MIPS has many special rules that are different from
other ELF ABIs. I don't think there were technical reasons to
be different, but the reality is that they are different.
It is unfortunate that we had to pass many parameters to
computeAddend, but it seems unavoidable because of MIPS.

llvm-svn: 315617
2017-10-12 20:42:15 +00:00
Rui Ueyama 3e8cc2bda6 Early-continue for a MIPS-specific rule.
This is an attempt to make lld's relocation handler code understandable.
Since I don't fully understand what exactly this function does for all
possible cases (I believe no one can), I'm not really sure if this patch
is NFC, but at least no functionality change intended. All tests still pass.

llvm-svn: 315612
2017-10-12 20:03:57 +00:00
Rui Ueyama ca05b6fb79 Remove Symbol::isPreemptible().
Because it was a redundant accessor to Symbol's public member.

llvm-svn: 315609
2017-10-12 19:10:41 +00:00
Rafael Espindola 49419bb303 Use 0 as sh_link when we don't have a dynamic symbol table.
It doesn't make sense to link from an alloc section to a non alloc
one as we were doing.

This matches the behavior of both gold and bfd.

llvm-svn: 315584
2017-10-12 15:05:04 +00:00
George Rimar 81eca18df3 [ELF] - Linkerscript: Add `~` as separate math token.
Previously we did not support following:
foo = ~0xFF;
and had to add space before numeric value:
foo = ~ 0xFF

That was constistent with ld.bfd < 2.30, which shows:
script.txt:3: undefined symbol `~2' referenced in expression,
but inconsistent with gold.

It was fixed for ld.bfd 2.30 as well:
https://sourceware.org/bugzilla/show_bug.cgi?id=22267

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

llvm-svn: 315569
2017-10-12 08:40:12 +00:00
Rui Ueyama cd063a9c7a Start destructuring adjustExpr function.
adjustExpr became a mysterious function as it does too many things.
This patch is an attempt to destructure that function.

llvm-svn: 315560
2017-10-12 05:32:52 +00:00
Rui Ueyama 3427c696f1 Fix comment.
llvm-svn: 315559
2017-10-12 04:52:54 +00:00
Rui Ueyama b518f655c3 Move more code out of scanRelocs().
llvm-svn: 315558
2017-10-12 04:52:39 +00:00
Rui Ueyama bf26e4e2a2 Make a function shorter.
llvm-svn: 315555
2017-10-12 04:02:02 +00:00
Rui Ueyama be85529d2b Remove one parameter from Target::getRelExpr.
A section was passed to getRelExpr just to create an error message.
But if there's an invalid relocation, we would eventually report it
in relocateOne. So we don't have to pass a section to getRelExpr.

llvm-svn: 315552
2017-10-12 03:14:06 +00:00
Rui Ueyama 63e49d489b Rewrite comment.
llvm-svn: 315548
2017-10-12 02:09:11 +00:00
Rui Ueyama 36d8db42d8 Rename P -> Pieces.
Conventionally, an array of SectionPieces is named Pieces.
It is better to follow the convention.

llvm-svn: 315543
2017-10-12 00:43:22 +00:00
NAKAMURA Takumi b6d3452243 lld: Prune unused libdeps.
llvm-svn: 315537
2017-10-12 00:04:24 +00:00
NAKAMURA Takumi d791eaa559 lld: Reorder libdeps.
Differential Revision: https://reviews.llvm.org/D38828

llvm-svn: 315529
2017-10-11 23:18:43 +00:00
Rui Ueyama 67533a2cb3 Define RelType to represent relocation types.
We were using uint32_t as the type of relocation kind. It has a
readability issue because what Type really means in `uint32_t Type`
is not obvious. It could be a section type, a symbol type or a
relocation type.

Since we do not do any arithemetic operations on relocation types
(e.g. adding one to R_X86_64_PC32 doesn't make sense), it would be
more natural if they are represented as enums. Unfortunately, that
is not doable because relocation type definitions are spread into
multiple header files.

So I decided to use typedef. This still should be better than the
plain uint32_t because the intended type is now obvious.

llvm-svn: 315525
2017-10-11 22:49:24 +00:00
Rui Ueyama 4092016b7b Return early if it fails to parse a hex string.
This patch doesn't change the behavior of the program because it
would eventually return None at end of the function. But it is
better to return None early if we know it will eventually happen.

llvm-svn: 315495
2017-10-11 19:30:39 +00:00
Davide Italiano db710f28e6 [ELF] Try to not emit weird diagnostics on undefined symbols.
Fixes PR34872.

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

llvm-svn: 315487
2017-10-11 18:37:24 +00:00
George Rimar 970e783bdd [ELF] - Fix out of sync comment. NFC.
llvm-svn: 315442
2017-10-11 08:18:53 +00:00
George Rimar 26fa916deb [ELF] - Do not set output section flags except SHF_{ALLOC,WRITE,EXECINSTR}.
This is PR34546.

Currently LLD creates output sections even if it has no input sections,
but its command contains an assignment.
Committed code just assigns the same flag that was used in previous
live section.
That does not work sometimes. For example if we have following script:

.ARM.exidx : { *(.ARM.exidx*) }
.foo : { _foo = 0; } }
Then first section has SHF_LINK_ORDER flag. But section foo should not.
That was a reason of crash in OutputSection::finalize(). LLD tried to calculate
Link value, calling front() on empty input sections list.
We should only keep access flags and omit all others when creating such sections.

Patch fixes the crash observed.

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

llvm-svn: 315441
2017-10-11 08:13:40 +00:00
Rui Ueyama a2212f846b Do not copy Sections vector.
llvm-svn: 315436
2017-10-11 05:13:16 +00:00
Rui Ueyama 7ad1e3102a Split LinkerScript::computeInputSections into two functions.
llvm-svn: 315434
2017-10-11 04:50:30 +00:00
Rui Ueyama 722221f5a7 Swap parameters of getSymbolValue.
Usually, a function that does symbol lookup takes symbol name as
its first argument. Also, if a function takes a source location hint,
it is usually the last parameter. So the previous parameter order
was counter-intuitive.

llvm-svn: 315433
2017-10-11 04:34:34 +00:00
Rui Ueyama f5733500c9 Do not handle DefinedCommon in linker scripts.
Because of r314495, DefinedCommon symbols cannot reach to
getSymbolValue function. When they reach the fucntion, they have
already been converted to DefinedRegular symbols.

llvm-svn: 315432
2017-10-11 04:31:13 +00:00
Rui Ueyama f0403c601a Rename BytesDataCommand -> ByteCommand.
llvm-svn: 315431
2017-10-11 04:22:09 +00:00
Rui Ueyama 2f4c121d89 Inline LinkerScript::process.
"process" was not a good name because everything can be named it.

llvm-svn: 315430
2017-10-11 04:21:55 +00:00
Rui Ueyama 355a8dd663 Split a loop into two to make it clear that it did two different things.
llvm-svn: 315427
2017-10-11 04:01:24 +00:00
Rui Ueyama 0543343170 Use more precise type.
llvm-svn: 315426
2017-10-11 04:01:13 +00:00
Rui Ueyama 187b67a533 Remove a static local varaible.
We should generally avoid static local variables.

llvm-svn: 315425
2017-10-11 03:34:09 +00:00
Rui Ueyama 1f4d7b56f4 Avoid a pointer to a pointer to an input section.
llvm-svn: 315424
2017-10-11 03:23:29 +00:00
Rui Ueyama ec5c4adbf2 Add comment.
llvm-svn: 315423
2017-10-11 03:23:17 +00:00
Rui Ueyama 2b714b56a9 Split decompressAndMergeSection into two separate functions.
Even though they are called sequentially, they are separate
operations, so it is better to split it.

llvm-svn: 315422
2017-10-11 03:12:53 +00:00
Rui Ueyama b801441ed3 Remove useless cast.
llvm-svn: 315421
2017-10-11 02:55:05 +00:00
Rui Ueyama 04c9ca7408 Rename filename -> getFilename.
llvm-svn: 315420
2017-10-11 02:54:52 +00:00
Rui Ueyama 71f840672d Rename Align -> Alignment.
llvm-svn: 315419
2017-10-11 02:46:09 +00:00
Rui Ueyama 29b240c671 Rename CurAddressState -> Ctx.
We used CurAddressState to capture a dynamic context just like
we use lambdas to capture static contexts. So, CurAddressState
is used everywhere in LinkerScript.cpp. It is worth a shorter
name.

llvm-svn: 315418
2017-10-11 02:45:54 +00:00
Rui Ueyama 183aa2731e Make LinkerScript::addSymbol a private member function.
llvm-svn: 315416
2017-10-11 02:28:39 +00:00
Rui Ueyama 5908c2f877 Rename processCommands -> processSectionCommands.
llvm-svn: 315415
2017-10-11 02:28:28 +00:00
Rui Ueyama a80633ca14 Inline small functions.
llvm-svn: 315414
2017-10-11 02:20:00 +00:00
Rui Ueyama 6b394caaf1 Rename Commands -> SectionCommands.
"Commands" was ambiguous because in the linker script, everything is
a command. We used to handle only SECTIONS commands, and at the time,
it might make sense to call them the commands, but it is no longer
the case. We handle not only SECTIONS but also MEMORY, PHDRS, VERSION,
etc., and they are all commands.

llvm-svn: 315409
2017-10-11 01:50:56 +00:00
Rui Ueyama a323e2a722 Rename HasSections -> HasSectionsComand.
HasSections is true if there is at least one SECTIONS linker
script command, and it is not directly related to whether we have
section objects or not. So I think the new name is better.

llvm-svn: 315405
2017-10-11 01:34:51 +00:00
Rui Ueyama 7f1c266e43 Move a loop invariant outside the loop.
llvm-svn: 315404
2017-10-11 01:26:22 +00:00
Rui Ueyama ac27de9dc7 Remove ScriptConfiguration class and move the members to LinkerScript class.
ScriptConfiguration was a class to contain parsed results of
linker scripts. LinkerScript is a class to interpret it.

That ditinction was needed because we haven't instantiated
LinkerScript early (because, IIRC, LinkerScript class was a
ELFT template function). So, when we parse linker scripts,
we couldn't directly store the result to a LinkerScript instance.

Now, that limitation is gone. We instantiate LinkerScript
at the very beginning of our main function. We can directly
store parse results to a LinkerScript instance.

llvm-svn: 315403
2017-10-11 01:19:33 +00:00
Rui Ueyama 18d19687c8 Inline addRegular into addSymbol.
Because addRegular's functionality is tightly coupled with
addSymbol, and the former is called only once, it makes sense
to merge the two functions. This patch also adds comments.

llvm-svn: 315401
2017-10-11 01:03:37 +00:00
Rui Ueyama 4fbe351817 Remove a constructor from ExprValue. NFC.
I think three ctors are too many for this simple class.

llvm-svn: 315394
2017-10-11 00:06:27 +00:00
Rui Ueyama 41ef17bb3c Add comments.
llvm-svn: 315393
2017-10-11 00:01:49 +00:00
Rui Ueyama ed94f36154 Remove an unused default argument.
llvm-svn: 315386
2017-10-10 23:09:23 +00:00
Rui Ueyama a1b79dff2a Handle input section liveness only in MarkLive.cpp.
The condition whether a section is alive or not by default
is becoming increasingly complex, so the decision of garbage
collection is spreading over InputSection.h and MarkLive.cpp,
which is not a good state.

This moves the code to MarkLive.cpp, to keep the file the central
place to make decisions about garbage collection.

llvm-svn: 315384
2017-10-10 22:59:32 +00:00
Rafael Espindola ae5e9ed422 Update for llvm change.
llvm-svn: 315377
2017-10-10 22:18:16 +00:00
Rafael Espindola 97c57b9e7b Don't create a dummy __tls_get_addr.
We just don't need one with the current setup.

We only error on undefined references that are used by some
relocation.

If we managed to relax all uses of __tls_get_addr, no relocation uses
it and we don't produce an error.

This is less code and fixes the case were we fail to relax. Before we
would produce a broken output, but now we produce an error.

llvm-svn: 315334
2017-10-10 17:27:34 +00:00
James Henderson b5ca92ef73 [ELF] Set Dot initially to --image-base value when using linker scripts
When parsing linker scripts, LLD previously started with a '.' value of 0,
regardless of the internal default image base for the target, and regardless of
switches such as --image-base. It seems reasonable to use a different image base
value when using linker scripts and --image-base is specified, since otherwise the
switch has no effect. This change does this, as well as removing unnecessary
initialisation of Dot where it is not used.

The default image base should not be used when processing linker
scripts, because this will change the behaviour for existing linker script users,
and potentially result in invalid output being produced, as a subsequent assignment
to Dot could move the location counter backwards. Instead, we maintain the existing
behaviour of starting from 0 if --image-base is not specified.

Reviewers: ruiu

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

llvm-svn: 315293
2017-10-10 10:09:35 +00:00
Andrew Ng 4d54a4b4f7 [LLD] Fix findOrphanPos to consistently ignore "dead" OutputSection's
When findOrphanPos does the reverse search to find the OutputSection
preceding the orphan's insertion point, look for a live OutputSection
and ignore "dead" OutputSection's. This matches the behaviour of the
forward search performed earlier in this function.

Added test which without the above fix fails as a result of an orphan
executable section being incorrectly placed in a non-executable segment.

Differential Review: https://reviews.llvm.org/D38690

llvm-svn: 315292
2017-10-10 10:05:52 +00:00
George Rimar 7e9c5610c9 [ELF] - Improve "has non-ABS reloc" error.
It did not contain information about relocation type and symbol.

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

llvm-svn: 315280
2017-10-10 07:55:07 +00:00
Rui Ueyama c04a91aa39 Make a local variable name shorter. NFC.
llvm-svn: 315271
2017-10-10 04:53:14 +00:00
Rui Ueyama ce135ff6f3 Simplify.
llvm-svn: 315270
2017-10-10 04:45:48 +00:00
Rui Ueyama 6b7f196909 Remove dead function.
llvm-svn: 315269
2017-10-10 03:58:32 +00:00
Rui Ueyama c9a4d1c735 Add comments.
llvm-svn: 315268
2017-10-10 03:58:18 +00:00
Rui Ueyama f987fe7869 Add more comments.
llvm-svn: 315267
2017-10-10 03:40:57 +00:00
Rui Ueyama 124bedbd50 Add comment to InputSectionBase::File.
This patch also moves declarations so that related declarations next
to each other.

llvm-svn: 315266
2017-10-10 03:22:29 +00:00
Rui Ueyama 339ddd1685 Remove dead function.
llvm-svn: 315265
2017-10-10 03:01:41 +00:00
Rui Ueyama e03ba02348 Rename ignoreInterpSection -> needsInterpSection.
This should improve consistency. Also added comment.

llvm-svn: 315169
2017-10-08 03:52:15 +00:00
Rui Ueyama 0ae2c24c5d Use llvm::Optional instead of UINT_MAX to represent a null value.
llvm-svn: 315168
2017-10-08 03:45:49 +00:00
Rui Ueyama 656be31174 Make a helper function a non-member function. NFC.
llvm-svn: 315167
2017-10-08 02:54:42 +00:00
Rui Ueyama 872235743d Use llvm::Optional instead of a magic number -1 to represent "no result".
llvm-svn: 315166
2017-10-08 02:44:08 +00:00
Rui Ueyama 617e2f98a0 Make ScriptParser::checkSection a non-member function.
This patch also make its return type to `void` because
it always returns a given value as-is.

llvm-svn: 315165
2017-10-08 02:27:02 +00:00
Rui Ueyama 9b18f50f79 Remove a trivial function.
llvm-svn: 315164
2017-10-08 02:06:03 +00:00
Rui Ueyama e8936bca72 Add comment.
llvm-svn: 315163
2017-10-08 01:55:29 +00:00
Rui Ueyama 10f5867135 Remove redundant initialization code.
llvm-svn: 315158
2017-10-08 00:45:34 +00:00
Rafael Espindola 8bd0c45c7a Inline small function. NFC.
llvm-svn: 315141
2017-10-07 01:08:28 +00:00
Rui Ueyama 8befefb2ea Remove OutputSection::updateAlignment.
I feel it is easier to understand without this function.

llvm-svn: 315140
2017-10-07 00:58:34 +00:00
Rui Ueyama c8e0f79b90 Clarify code by adding a comment.
llvm-svn: 315139
2017-10-07 00:58:19 +00:00
Rui Ueyama 0e2bfb1e3b Merge addInputSec with OutputSection::addSection.
Previously, when we added an input section to an output section, we
called `OutputSectionFactory::addInputSec`. This isn't a good design
because, a factory class is intended to create a new object and
return it, but in this use case, it will never create a new object.
This patch fixes the design flaw.

llvm-svn: 315138
2017-10-07 00:43:31 +00:00
Rui Ueyama 850b554399 Return early. NFC.
llvm-svn: 315137
2017-10-07 00:22:59 +00:00
Rui Ueyama b4175edf16 Remove unused parameters.
llvm-svn: 315133
2017-10-07 00:08:30 +00:00
Rui Ueyama c54d5b16d5 Do not mutate Script->Opt.Commands from a leaf helper function.
Factory::addInputSec added an output section to Script->Opt.Commands,
but that is too subtle. This patch makes it explicit so that it is easy
to see when a new element is added to Script->Opt.Commands.

llvm-svn: 315129
2017-10-06 23:34:43 +00:00
Rui Ueyama d2f225fdef Simplify LinkerScript::addOrphanSections. NFCI.
This patch moves a std::find to a new function. It also removes
the following piece of code. I believe it should be fine because all
tests still pass.

  unsigned Index = std::distance(Opt.Commands.begin(), I);
  assert(Sec->SectionIndex == INT_MAX || Sec->SectionIndex == Index);
  Sec->SectionIndex = Index;

llvm-svn: 315125
2017-10-06 23:06:55 +00:00
Rui Ueyama edafba200f Split addSection into two small functions. NFCI.
addSection function was hard to read because it behaves differently
depending on its arguments but what exactly it does is not clear.
Now it should be better. Still, it is not clear (not what but) why
it does what it does, but I'll take a look at it later.

llvm-svn: 315124
2017-10-06 23:06:40 +00:00
Peter Collingbourne d6924d7474 Reland r315114, "ELF: Export preempted symbols even if there is a dynamic list." which was reverted in r315116.
I hadn't synced past the change that changed the default hash style
to --hash-style=both, so my test had the symbols in the wrong order.

llvm-svn: 315119
2017-10-06 22:09:03 +00:00
Peter Collingbourne accab5908d Revert r315114, "ELF: Export preempted symbols even if there is a dynamic list."
For some reason the symbols get emitted in the wrong order on one of the
buildbots: http://bb9.pgr.jp/#builders/15/builds/180

llvm-svn: 315116
2017-10-06 21:48:39 +00:00
Rui Ueyama 40bd97af34 Make a few member varaibles/functions private.
llvm-svn: 315115
2017-10-06 21:42:37 +00:00
Peter Collingbourne 5a8928c605 ELF: Export preempted symbols even if there is a dynamic list.
Dynamic lists in an executable are additive, not restrictive, so we
must continue to export preempted symbols even with a dynamic list.

This fixes sanitizer interception of libc symbols (and should also fix
symbol preemption by users of sanitizers).

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

llvm-svn: 315114
2017-10-06 21:33:59 +00:00
Rui Ueyama 56c5c63e6d Inline a small function.
llvm-svn: 315113
2017-10-06 21:32:23 +00:00
Rui Ueyama 8ad23dcd8b Revert r314616: Run writeTo() concurrently.
This reverts commit r314616 because nested parallel-for loops can hang.
Our ThreadPoolExecutor is not necessarily able to handle nested calls.

llvm-svn: 315098
2017-10-06 20:12:43 +00:00
Rafael Espindola 469350810a Refine our --wrap implementation.
Before this patch we would copy foo into real_foo and wrap_foo into
foo. The net result is that __wrap_foo shows up twice in the symbol
table.

With this patch we:

* save a copy of __real_foo before copying foo.
* drop one of the __wrap_foo from the symbol table.
* if __real_foo was not undefined, add a *new* symbol with that content to
  the symbol table.

The net result is that

Anything using foo now uses __wrap_foo
Anything using __real_foo now uses foo.
Anything using __wrap_foo still does.

And the symbol table has foo, __wrap_foo and __real_foo (if defined).

Which I think is the desired behavior.

llvm-svn: 315097
2017-10-06 20:09:34 +00:00
Rui Ueyama e41f9e0651 Inline a small function. NFC.
llvm-svn: 315096
2017-10-06 20:08:51 +00:00
Alexander Richardson f52a3b94e1 [ELF][MIPS] Check for overflow when writing R_MIPS_CALL16 and R_MIPS_TLS_GOTTPREL
Summary:
These are 16 bit relocations and not part of a HI/LO pair so we need to
check that they don't overflow.

Reviewers: atanasyan

Reviewed By: atanasyan

Subscribers: ruiu, llvm-commits, emaste, sdardis

Tags: #lld

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

llvm-svn: 315073
2017-10-06 16:15:59 +00:00
George Rimar 8f3a6c8143 [ELF] - Do not produce broken .dynamic section with --no-rosegment
LLD produces broken .dynamic section when --no-rosegment and at least
one of following options is present:
  1) -z rodynamic is given.
  2) MIPS target.

That happens because code that writes .dynamic assumes target buffer
is zero-filled, what can be not true after LLD fills it with trap
instructions. With one of two options above, .dynamic becomes
SHF_ALLOC section, so can be affected.

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

llvm-svn: 315054
2017-10-06 10:06:13 +00:00
George Rimar 2727ce2c1f [ELF] - Do not produce broken .dynsym with --no-rosegment.
We produce broken output currently.
Code that writes .dynsym assumes output buffer is zero-filled,
though that is not always true. When --no-rosegment is given,
buffer can be filled with trap instructions. Patch fixes the issue.

It is relative with PR34705.

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

llvm-svn: 315053
2017-10-06 09:56:24 +00:00
George Rimar d46753e421 [ELF] - Do --hash-style=both by default.
Its PR34712,

GNU linkers recently changed default values to "both" of "sysv".
Patch do the same for all targets except MIPS, where .gnu.hash
section is not yet supported.

Code suggested by Rui Ueyama.

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

llvm-svn: 315051
2017-10-06 09:37:44 +00:00
Rui Ueyama 0fda1d70bb Fix typo.
llvm-svn: 315042
2017-10-06 04:32:08 +00:00
Alexander Richardson b9aa9a5500 [ELF] Don't crash when parsing a file with out-of-bounds version references
Summary:
We were crashing when linking telnetd in FreeBSD because lld was emitting
corrupted output files for --norosegment. In this file the version index of some symbols
was set to 9 but lld only found 8 version definitions.

I am not sure how to create a minimal .so file that also exposes this behaviour so I just added the one that initially caused the error to Inputs/

This partially addresses https://bugs.llvm.org/show_bug.cgi?id=34705

Reviewers: ruiu, rafael, pcc, grimar

Reviewed By: ruiu

Subscribers: emaste, krytarowski

Tags: #lld

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

llvm-svn: 315036
2017-10-05 23:28:34 +00:00
Alexander Richardson 57f089775b [ELF] Don't crash when parsing a file with external version definition references
Summary:
We were crashing when linking telnetd in FreeBSD because lld was emitting
corrupted output files for --norosegment. In this file the version index of some symbols
was set to 9 but lld only found 8 version definitions.

I am not sure how to create a minimal .so file that also exposes this behaviour so I just added the one that initially caused the error to Inputs/

This partially addresses https://bugs.llvm.org/show_bug.cgi?id=34705

Reviewers: ruiu, rafael, pcc, grimar

Reviewed By: ruiu

Subscribers: emaste, krytarowski

Tags: #lld

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

llvm-svn: 315035
2017-10-05 23:28:29 +00:00
Rui Ueyama 945cd64471 Wait for all threads to terminate before exitting.
I think it is not defined what would happen to detached threads
when the main thread tries to exit. That means it was not guaranteed
that unlinkAsync correctly removes a temporary file. It was also
reported that this unlinkAsync caused a crash on Windows.

This patch adds a few new functions so that the main thread always
waits for non-main threads before exitting.

I don't actually like the new two functions, runBackground and
waitForBackgroundThreads, because it looks like it is a bit
overdesigned. After all, what we are doing with these functions
is to just remove a file.

An alternative would be to do fork(2) and make the child process
remove a file asynchronously. However, it has its own problems.
Correctly forking and reclaiming a resource using waitpid(2) is not
doable unless we know our process-wide settings (such as signal mask),
but we can't make any assumption on it when lld is embedded to other
process. So I chose to stick with threads instead of multi-processes.

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

llvm-svn: 315033
2017-10-05 23:01:11 +00:00
Rafael Espindola 4b851198fc Remove dead code.
llvm-svn: 314966
2017-10-05 03:01:05 +00:00
Rafael Espindola c29b24d524 Refactor duplicated code.
Since VisibleToRegularObj is weaker than LinkerRedefined, this should
have no functionality change.

llvm-svn: 314954
2017-10-05 00:35:47 +00:00
Rafael Espindola 0038dfa19d Revert "Revert r314810: Use sched_getaffinity instead of std:🧵:hardware_concurrency."
This reverts commit r314924.

The required llvm patch was recommitted.

llvm-svn: 314933
2017-10-04 20:35:05 +00:00
Rui Ueyama 61b9ce217a Revert r314810: Use sched_getaffinity instead of std:🧵:hardware_concurrency.
This reverts commit r314810 because r314809 was reverted.

llvm-svn: 314924
2017-10-04 18:39:51 +00:00
George Rimar ed906d8e63 [ELF] - Update after LLVM r314883 change. NFC.
llvm-svn: 314884
2017-10-04 08:50:34 +00:00
Rui Ueyama 7d1f5fd840 Call exitLld() from elf::link.
We used to call exitLld() from a leaf function, Writer::run(), because
we had objects on the stack whose dtors are expensive. Now we no longer
have such objects on the stack, so there's no reason to exist from the
leaf function.

llvm-svn: 314869
2017-10-04 00:50:11 +00:00
Rui Ueyama 732f4e2778 Remove BssSection::reserveSpace().
We no longer call reserveSpace more than once, so it can be merged with
its constructor.

llvm-svn: 314867
2017-10-04 00:21:17 +00:00
Shoaib Meenai 50d7b36f5e [ELF] Decompress debug info sections early
When reporting a symbol conflict, LLD parses the debug info to report
source location information. Sections have not been decompressed at this
point, so if an object file contains zlib compressed debug info, LLD
ends up passing this compressed debug info to the DWARF parser, which
causes debug info parsing failures and can trigger assertions in the
parser (as the test case demonstrates).

Decompress debug sections when constructing the LLDDwarfObj to avoid
this issue. This doesn't handle GNU-style compressed debug info sections
(.zdebug_*), which at present are simply ignored by LLDDwarfObj; those
can be done in a follow-up.

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

llvm-svn: 314866
2017-10-04 00:19:41 +00:00
Rui Ueyama a215e7cbe8 Move fetchIfLazy up so that the following comment makes sense.
We have this comment in LinkerDriver::link

  After this, no new names except a few linker-synthesized ones
  will be added to the symbol table.

but that was not true because new symbols could be added by processing
the -u option.

llvm-svn: 314842
2017-10-03 20:45:09 +00:00
Rui Ueyama fa105f8deb Inline a small function.
llvm-svn: 314841
2017-10-03 20:44:54 +00:00
Rafael Espindola c804bf9397 Use sched_getaffinity instead of std:🧵:hardware_concurrency.
The issue with std:🧵:hardware_concurrency is that it forwards
to libc and some implementations (like glibc) don't take thread
affinity into consideration.

With this change a llvm program that can execute in only 2 cores will
use 2 threads, even if the machine has 32 cores.

This makes benchmarking a lot easier, but should also help if someone
doesn't want to use all cores for compilation for example.

llvm-svn: 314810
2017-10-03 16:25:48 +00:00
Simon Atanasyan f8db45361e [MIPS] Generate thunks for microMIPS code
If symbol has the STO_MIPS_MICROMIPS flag and requires a thunk to perform
call PIC from non-PIC functions, we need to generate a thunk with microMIPS
code.

llvm-svn: 314797
2017-10-03 13:30:02 +00:00
Igor Kudrin fb7f8befec [ELF] Avoid promoting an undefined weak entry symbol to global.
Without this patch, lld emits "error: undefined symbol: _start"
if it encountered only weak references to that symbol.

llvm-svn: 314790
2017-10-03 12:23:46 +00:00
Igor Kudrin 66b0ad91f6 [ELF] Keep symbols specified by '-u' over LTO.
Differential Revision: https://reviews.llvm.org/D38348

llvm-svn: 314789
2017-10-03 12:17:59 +00:00
Rui Ueyama f3f9bae842 Add a comment.
llvm-svn: 314746
2017-10-03 00:45:24 +00:00
Hans Wennborg f69216a575 Build fix: don't try to link in lldConfig
Config was removed in r314719.

llvm-svn: 314736
2017-10-02 23:09:37 +00:00
Rui Ueyama 3f851704c1 Move new lld's code to Common subdirectory.
New lld's files are spread under lib subdirectory, and it isn't easy
to find which files are actually maintained. This patch moves maintained
files to Common subdirectory.

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

llvm-svn: 314719
2017-10-02 21:00:41 +00:00
Rui Ueyama abf908e632 Fix a data race found by tsan.
Reads from `Live` and writes to `OutputOff` in the following code race
even though they are logically independent because they are bitfields
sharing the same word.

  for (size_t I = 0, E = Sec->Pieces.size(); I != E; ++I) {
    if (!Sec->Pieces[I].Live)
      continue;
    CachedHashStringRef Str = Sec->getData(I);
    size_t ShardId = getShardId(Str.hash());
    if ((ShardId & (Concurrency - 1)) == ThreadId)
      Sec->Pieces[I].OutputOff = Shards[ShardId].add(Str);
  }

llvm-svn: 314711
2017-10-02 20:16:13 +00:00
Rui Ueyama 6aeea183ef Rewrite a comment.
llvm-svn: 314707
2017-10-02 18:54:59 +00:00
Simon Atanasyan 649e4d328f [MIPS] Fix PLT entries generation in case of linking regular and microMIPS code
Currently LLD calls the `isMicroMips` routine to determine type of PLT entries
needs to be generated: regular or microMIPS. This routine checks ELF
header flags in the `FirstObj` to retrieve type of linked object files.
So if the first file does not contain microMIPS code, LLD will generate
PLT entries with regular (non-microMIPS) code only.

Ideally, if a PLT entry is referenced by microMIPS code only this entry
should contain microMIPS code, if a PLT entry is referenced by regular
code this entry should contain regular code. In a "mixed" case the PLT
entry can be either microMIPS or regular, but each "cross-mode-call" has
additional cost.

It's rather difficult to implement this ideal solution. But we can
assume that if there is an input object file with microMIPS code, the
most part of the code is microMIPS too. So we need to deduce type of PLT
entries based on finally calculated ELF header flags and do not check
only the first input object file.

This change implements this.
  - The `getMipsEFlags` renamed to the `calcMipsEFlags`. The function
    called from the `LinkerDriver::link`. Result is stored in
    the Configuration::MipsEFlags field.
  - The `isMicroMips` and `isMipsR6` routines access the `MipsEFlags`
    field to get and check calculated ELF flags.
  - New types of PLT records created when necessary.

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

llvm-svn: 314675
2017-10-02 14:56:41 +00:00
George Rimar ba45584c4b [ELF] - Stop removing sections in removeUnusedSyntheticSections().
That makes code a bit more consistent. Instead of removing sections there
we can just mark them as dead. So that removeEmptyCommands() will
handle the rest.

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

llvm-svn: 314654
2017-10-02 09:11:13 +00:00
Rui Ueyama 274aa2fb88 [ICF] Include section contents in section hash values.
Computing section content hashes early seems like a win in terms of
performance. It increases a chance that two different sections will get
different class IDs from the beginning.

Without threads, this patch improves Chromium link time by about 0.3
seconds. With threads, by 0.1 seconds. That's less than 1% time saving
but not bad for a small patch.

llvm-svn: 314644
2017-10-02 01:21:07 +00:00
Rui Ueyama 43ca7167ad Inline hot functions.
llvm-svn: 314637
2017-10-01 23:46:31 +00:00
Rui Ueyama cb222035c2 Run writeTo() concurrently.
I don't know why we didn't use parallelForEach to call writeTo,
but there should be no reason to not do that, as most writeTo
functions are safe to run concurrently.

llvm-svn: 314616
2017-10-01 02:25:34 +00:00
Rui Ueyama 46557f94d0 Fix 32-bit buildbots.
The result of hash_value(StringRef) depends on sizeof(size_t).
That causes lld to create different mergeable table contents on
32-bit machines.

This patch is to use xxHash64 so that we get the same hash values
on 32-bit machines.

llvm-svn: 314603
2017-09-30 21:28:49 +00:00
NAKAMURA Takumi 70947e2224 SyntheticSections.cpp: Appease g++-4.8, s/const/constexpr/
llvm-svn: 314592
2017-09-30 13:40:22 +00:00
Rui Ueyama de3d0cc894 Make parameter lists of SymbolTable::add* functions more consistent. NFC.
llvm-svn: 314591
2017-09-30 12:41:34 +00:00
Rui Ueyama fbc622d1e4 Fix buildbots.
llvm-svn: 314590
2017-09-30 12:19:08 +00:00
Rui Ueyama c97a70c6f5 Parallelize string merging.
String merging is one of the most time-consuming functions in lld.
This patch parallelize it to speed it up. On my 2-socket 20-core
40-threads Xeon E5-2680 @ 2.8 GHz machine, this patch shorten the
clang debug build link time from 7.11s to 5.16s. It's a 27%
improvement and actually pretty noticeable. In this test condition,
lld is now 4x faster than gold.

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

llvm-svn: 314588
2017-09-30 11:46:26 +00:00
Ben Dunbobbin 97c327a6cc [NFC] Removed accidenatally added file
llvm-svn: 314496
2017-09-29 09:15:55 +00:00
Ben Dunbobbin 73eabf23a4 [ELF] Simpler scheme for handling common symbols
Convert all common symbols to regular symbols after scan.
This means that the downstream code does not to handle common symbols as a special case.

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

llvm-svn: 314495
2017-09-29 09:08:26 +00:00
Rafael Espindola 1f0fe88a1b Fix header location with PHDR.
We were not subtracting its size, causing it to overlap with section
data.

Fixes PR34750.

llvm-svn: 314440
2017-09-28 18:12:13 +00:00