Commit Graph

619 Commits

Author SHA1 Message Date
Rui Ueyama 8ecc2ec0e3 Remove dead code.
Thanks to George Rimor for pointing it out.

llvm-svn: 265155
2016-04-01 17:17:14 +00:00
Rui Ueyama a63baf176f Don't do extra work if -r is given.
fixAbsoluteSymbols fixes linker-created symbol addresses. Since we don't
create such symbols for relocatable output, we don't need to call this
function.

llvm-svn: 265154
2016-04-01 17:11:42 +00:00
Rui Ueyama e044e9cf80 Tidy up address and file offset assignments in the Writer.
assignAddressesRelocatable function did not set addresses to sections
despite its name. What it actually did is to set file offsets to sections.
assignAddresses function assigned addresses and file offsets to sections.
So there was a confusion what they were doing, and they had duplicate code.

This patch separates file offset assignments from address assignments.
A new function, assignFileOffsets assign file offsets. assignAddresses
do not care about file offsets anymore.

llvm-svn: 265151
2016-04-01 17:07:17 +00:00
Rafael Espindola 790db9c458 Bring r264761 back with an extra fix.
The extra fix is to note that it still requires copy relocations.

Original message:

Change how we handle R_MIPS_LO16.

Mips aligns PT_LOAD to 16 bits (0x10000). That means that the lower 16
bits are always the same, so we can, effectively, say that the
relocation is relative.

P.S.: Suggestions for a better name for the predicate are welcome :-)

llvm-svn: 265150
2016-04-01 17:00:36 +00:00
Rafael Espindola 31d2ada6d5 Refactor duplicated code.
We had almost identical code to handle creating a plt entry in two
places.

llvm-svn: 265142
2016-04-01 14:14:48 +00:00
Rafael Espindola b97f4beec4 Avoid creating duplicated relocations.
llvm-svn: 265139
2016-04-01 12:54:27 +00:00
George Rimar 5a5c39ca2a Fix linux buildbot after r265136
http://lab.llvm.org:8011/builders/lld-x86_64-darwin13/builds/20138

Fix: Move method above class that uses it.
Works fine with MSVS.

llvm-svn: 265137
2016-04-01 12:24:28 +00:00
George Rimar e2da42a762 Make dummySectionsNum() to be not a member of writer. NFC.
llvm-svn: 265136
2016-04-01 11:59:32 +00:00
George Rimar b345e0d15c Split Writer::assignAddresses(): extract common code to fixFileOff(). NFC.
llvm-svn: 265131
2016-04-01 11:04:47 +00:00
George Rimar 900a260357 Split Writer::assignAddresses(): extract assignPhdrs(). NFC.
llvm-svn: 265130
2016-04-01 10:49:14 +00:00
George Rimar efded31a61 [ELF] - Move calculation of _end to fixAbsoluteSymbols()
That is consistent with other symbols: _edata, _etext
and can help to avoid duplicate code.

Differential revision: http://reviews.llvm.org/D18655

llvm-svn: 265129
2016-04-01 10:23:32 +00:00
Simon Atanasyan 13f6da1d2c [ELF] Implement infrastructure for thunk code creation
Some targets might require creation of thunks. For example, MIPS targets
require stubs to call PIC code from non-PIC one. The patch implements
infrastructure for thunk code creation and provides support for MIPS
LA25 stubs. Any MIPS PIC code function is invoked with its address
in register $t9. So if we have a branch instruction from non-PIC code
to the PIC one we cannot make the jump directly and need to create a small
stub to save the target function address.
See page 3-38 ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf

- In relocation scanning phase we ask target about thunk creation necessity
by calling `TagetInfo::needsThunk` method. The `InputSection` class
maintains list of Symbols requires thunk creation.

- Reassigning offsets performed for each input sections after relocation
scanning complete because position of each section might change due
thunk creation.

- The patch introduces new dedicated value for DefinedSynthetic symbols
DefinedSynthetic::SectionEnd. Synthetic symbol with that value always
points to the end of the corresponding output section. That allows to
escape updating synthetic symbols if output sections sizes changes after
relocation scanning due thunk creation.

- In the `InputSection::writeTo` method we write thunks after corresponding
input section. Each thunk is written by calling `TargetInfo::writeThunk` method.

- The patch supports the only type of thunk code for each target. For now,
it is enough.

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

llvm-svn: 265059
2016-03-31 21:26:23 +00:00
Simon Atanasyan cf8c42f546 [ELF][MIPS] Revert r264761 and add test case to demonstrate the problem
If we make R_MIPS_LO16 a relative relocation, linker:
- never creates R_MIPS_COPY relocation for it
- attempts to create R_MIPS_REL32 dynamic relocation if R_MIPS_LO16's
  target is a preemptible symbol

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

llvm-svn: 264956
2016-03-30 22:43:14 +00:00
Rui Ueyama 4709190376 Split Writer::assignAddresses. NFC.
llvm-svn: 264905
2016-03-30 19:41:51 +00:00
George Rimar f1c0bf5b40 [ELF] - Do not keep undefined locals in .symtab
gold and bfd do not include the undefined locals in symtab.
We have no reasons to support that either.

That fixes PR27016

Differential revision: http://reviews.llvm.org/D18554

llvm-svn: 264843
2016-03-30 08:16:11 +00:00
Rafael Espindola cba3e8b4b5 Change how we handle R_MIPS_LO16.
Mips aligns PT_LOAD to 16 bits (0x10000). That means that the lower 16
bits are always the same, so we can, effectively, say that the
relocation is relative.

llvm-svn: 264761
2016-03-29 18:18:19 +00:00
Rafael Espindola 5432287bad Make needsPlt a plain function instead of a template.
llvm-svn: 264267
2016-03-24 12:55:27 +00:00
Rafael Espindola 26d239c293 Skip some relocations in scanRelocs.
When a tls access is optimized, a group of relocations is converted at a
time.

We were already skipping relocations that were optimized out in
relocate, but not in scanRelocs.

This is a small optimization. I got here while working on a patch that
will always keep scanRelocs and relocate in sync.

llvm-svn: 264048
2016-03-22 13:24:29 +00:00
Simon Atanasyan f3ec3be178 [ELF][MIPS] Delete GotSection::addMipsLocalEntry method
Now local symbols have SymbolBody so we can handle all kind of symbols
in the GotSection::addEntry method. The patch moves the code from
addMipsLocalEntry to addEntry. NFC.

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

llvm-svn: 264032
2016-03-22 08:36:48 +00:00
Rafael Espindola 69082f051d Revert "bar"
This reverts commit r263799.
It was a mistake. Sorry about that.

llvm-svn: 263801
2016-03-18 18:11:26 +00:00
Rafael Espindola c2cfd9fa34 bar
llvm-svn: 263799
2016-03-18 18:09:32 +00:00
George Rimar 786e866fea [ELF] - -pie/--pic-executable option implemented
-pie
--pic-executable

Create a position independent executable.  This is currently only
 supported on ELF platforms.  Position independent executables are
 similar to shared libraries in that they are relocated by the
 dynamic linker to the virtual address the OS chooses for them
 (which can vary between invocations).  Like normal dynamically
 linked executables they can be executed and symbols defined in the
 executable cannot be overridden by shared libraries.

Differential revision: http://reviews.llvm.org/D18183

llvm-svn: 263693
2016-03-17 05:57:33 +00:00
Rui Ueyama 9328b2cdde Use ELFT instead of ELFFile<ELFT>.
llvm-svn: 263510
2016-03-14 23:16:09 +00:00
Rui Ueyama f50e1d8358 Make getAlignment a non-member function. NFC.
This function did not rely on Writer class.

llvm-svn: 263502
2016-03-14 22:41:08 +00:00
Rui Ueyama cc232d1f90 Simplify. NFC.
llvm-svn: 263398
2016-03-13 22:08:11 +00:00
Rui Ueyama 98a4b8b05c Remove a local variable. NFC.
llvm-svn: 263389
2016-03-13 20:18:12 +00:00
Simon Atanasyan 0295bb42c0 [ELF][MIPS] Use TargetInfo::needsGot call to check necessity of GOT for local symbols. NFC.
llvm-svn: 263388
2016-03-13 20:13:24 +00:00
Rui Ueyama 6c5638b01a ELF: Add `Rela` member variable to Config.
The member is true if we want to create relocatin sections with RELA
instead of REL.

llvm-svn: 263387
2016-03-13 20:10:20 +00:00
Rui Ueyama c4466605d8 ELF: Redefine canBeDefined as a member function of SymbolBody.
We want to make SymbolBody the central place to query symbol information.
This patch also renames canBePreempted to isPreemptible because I feel that
the latter is slightly better (the former is three words and the latter
is two words.)

llvm-svn: 263386
2016-03-13 19:48:18 +00:00
Rui Ueyama 22b5d1f901 ELF: Set e_flags header only when MIPS.
The field is zero by default, so this is NFC.

llvm-svn: 263385
2016-03-13 19:29:17 +00:00
Rui Ueyama fc467e77b8 Use RelTy instead of Elf_Rel_Impl<ELFT, isRela> for readability.
llvm-svn: 263368
2016-03-13 05:06:50 +00:00
Rui Ueyama 7ede54310a Redefine isGnuIfunc as a member function of SymbolBody.
llvm-svn: 263365
2016-03-13 04:40:14 +00:00
Rui Ueyama cb8fd3ac68 Body can never be null in this context.
Because of the recent commit to allocate SymbolBodies for local symbols.

llvm-svn: 263364
2016-03-13 04:40:12 +00:00
Rui Ueyama 6eafa7fb23 Do not return a bool value from error().
error returned true if there was an error. This allows us to replace
the code like this

  if (EC) {
    error(EC, "something failed");
    return;
  }

with

  if (error(EC, "something failed"))
    return;

I thought that that was a good idea, but it turned out that we only
have two places to use this pattern. So this patch removes that feature.

llvm-svn: 263362
2016-03-13 04:25:41 +00:00
Rui Ueyama b30f73568f The difference of getBss() and Out<ELFT>::Bss is subtle, so remove that function.
llvm-svn: 263361
2016-03-13 04:11:53 +00:00
Rui Ueyama a969218f91 Split addCopyRelSymbols. NFC.
llvm-svn: 263360
2016-03-13 04:05:42 +00:00
Rui Ueyama 28286cdfc7 ELF: Include the build ID section in the first page.
At least Linux has the kernel configuration to include the first page
of the executable into core files. We want build ID section to be
included in core files to identify them.

Here is the link to the description about the kernel configuration.

097f70b3c4/fs/Kconfig.binfmt (L46)

llvm-svn: 263351
2016-03-13 01:54:48 +00:00
Rui Ueyama 634ddf0bec ELF: Implement --build-id.
This patch implements --build-id. After the linker creates an output file
in the memory buffer, it computes the FNV1 hash of the resulting file
and set the hash to the .note section as a build-id.

GNU ld and gold have the same feature, but their default choice of the
hash function is different. Their default is SHA1.

We made a deliberate choice to not use a secure hash function for the
sake of performance. Computing a secure hash is slow -- for example,
MD5 throughput is usually 400 MB/s or so. SHA1 is slower than that.

As a result, if you pass --build-id to gold, then the linker becomes about
10% slower than that without the option. We observed a similar degradation
in an experimental implementation of build-id for LLD. On the other hand,
we observed only 1-2% performance degradation with the FNV hash.

Since build-id is not for digital certificate or anything, we think that
a very small probability of collision is acceptable.

We considered using other signals such as using input file timestamps as
inputs to a secure hash function. But such signals would have an issue
with build reproducibility (if you build a binary from the same source
tree using the same toolchain, the build id should become the same.)

GNU linkers accepts --build-id=<style> option where style is one of
"MD5", "SHA1", or an arbitrary hex string. That option is out of scope
of this patch.

http://reviews.llvm.org/D18091

llvm-svn: 263292
2016-03-11 20:51:53 +00:00
Rui Ueyama 6b074f5c9f Fix MSVC build.
llvm-svn: 263277
2016-03-11 18:56:05 +00:00
Rafael Espindola 2230483812 Now that it is trivial, fix pr26878.
llvm-svn: 263271
2016-03-11 18:33:48 +00:00
Rafael Espindola 7862097a45 Yet more MSVC fixes.
llvm-svn: 263253
2016-03-11 16:41:23 +00:00
Rafael Espindola 36a73d2deb More MSVC fixes.
llvm-svn: 263251
2016-03-11 16:32:46 +00:00
Rafael Espindola 5d57fb823b Don't invent names for STT_SECTION symbols.
We should just copy the symbol names with -r.

llvm-svn: 263244
2016-03-11 14:57:33 +00:00
George Rimar 5761042db7 This reverts the r263125
It was discussed to make all messages be 
lowercase to be consistent with clang.
(also reverts the r263128 which fixed 
build bot fail after r263125)

Original commit message:
[ELF] - Consistent spelling for error/warning messages

Previously error and warnings were not consistent in lld.
Some of them started from lowercase letter, others from
uppercase. Also there was one or two which had a dot at the end.
This patch changes all messages to start from uppercase letter if
they were not before.

Differential revision: http://reviews.llvm.org/D18045

llvm-svn: 263240
2016-03-11 14:43:02 +00:00
Rafael Espindola 1f5b70f64f Represent local symbols with DefinedRegular.
llvm-svn: 263237
2016-03-11 14:21:37 +00:00
Rafael Espindola 4e90611ed2 Remove a redundant cast.
llvm-svn: 263230
2016-03-11 13:17:15 +00:00
Rafael Espindola ccb8b4d4fe Remember the input section of locals.
This is already a simplification, but will allow much more.

llvm-svn: 263224
2016-03-11 12:14:02 +00:00
Rafael Espindola 67d72c02bc Create a SymbolBody for locals.
pr26878 shows a case where locals have to be in the got.

llvm-svn: 263222
2016-03-11 12:06:30 +00:00
Rui Ueyama 17d6983a4e Rename MaxAlignment -> Alignment.
We can argue about a maximum alignment of a group of symbols,
but for each symbol, there is only one alignment.
So it is a bit weird that each symbol has a "maximum alignment".

llvm-svn: 263151
2016-03-10 18:58:53 +00:00
George Rimar e094388861 [ELF] - Consistent spelling for error/warning messages
Previously error and warnings were not consistent in lld.
Some of them started from lowercase letter, others from
uppercase. Also there was one or two which had a dot at the end.
This patch changes all messages to start from uppercase letter if
they were not before.

Differential revision: http://reviews.llvm.org/D18045

llvm-svn: 263125
2016-03-10 16:58:34 +00:00
Rafael Espindola e090fb2891 ELF: Remove non-standard ELF features from AMDGPU target.
Patch by Tom Stellard!

llvm-svn: 263063
2016-03-09 21:37:22 +00:00
Rafael Espindola e02f4dfb9e Remove an unnecessary hack.
It doesn't look like anything is depending on using local dynamic tls
relocations with preemptable  symbols.

llvm-svn: 262957
2016-03-08 21:17:31 +00:00
Rafael Espindola 1f04c44885 Delete isTlsDynRel.
It was a badly specified hack for when a tls relocation should be
propagated to the dynamic relocation table.

This replaces it with a not as bad hack of saying that a local dynamic
tls relocation is never preempted.

I will try to remove even that second hack in the next patch.

llvm-svn: 262955
2016-03-08 20:24:36 +00:00
Rui Ueyama 3f9f09265b ELF: Rename NoInhibitExec -> NoinhibitExec.
The variables corresponding to command line options are named mechanically.
Because the option for the variable is -noinhibit-exec and not -no-inhibit-exec,
it should be name this way.

llvm-svn: 262911
2016-03-08 04:06:29 +00:00
Rafael Espindola e711fc3767 Adjust the .bss alignment to include common symbols.
llvm-svn: 262840
2016-03-07 17:43:39 +00:00
George Rimar 2f0fab53e4 [ELF] - Simplify a SymbolBody class interface a bit.
Get rid of few accessors in that class, and replace
them with direct fields access.

Differential revision: http://reviews.llvm.org/D17879

llvm-svn: 262796
2016-03-06 06:26:18 +00:00
George Rimar 98b060d228 [ELF] - Use the uint32_t instead of unsigned in Target class for relocations types
Patch changes all relocations types to be uint32_t and also 
fixes some dependent inconsistency in callers code.

Differential revision: http://reviews.llvm.org/D17882

llvm-svn: 262793
2016-03-06 06:01:07 +00:00
Rafael Espindola d405f4721c Refactor target independent code.
The rules for when we can relax tls relocations are target independent.
The only things that are target dependent are the relocation values.

llvm-svn: 262748
2016-03-04 21:37:09 +00:00
Rui Ueyama d4530c6eb8 Add a comment about _DYNAMIC.
llvm-svn: 262735
2016-03-04 18:34:14 +00:00
Davide Italiano 255730cdc5 [ELF] Generalize symbol type handling.
SymbolBody constructor and friends take isFunc and isTLS boolean arguments.
ELF symbols have already a type so than be easily passed as argument.
If we want to support another type, this scheme is not good enough, that is,
the current code logic would require passing another `bool isObject` around.
Up to two argument, this stretching exercise was a little bit goofy but
still acceptable, but with more types to support, is just too much, IMHO.

Change the code so that the type is passed instead.

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

llvm-svn: 262684
2016-03-04 01:55:28 +00:00
Rafael Espindola 75714f618c Rename 'fatal' to 'check' when it doesn't always fail.
llvm-svn: 262666
2016-03-03 22:24:39 +00:00
George Rimar e5960ceb6b [ELF] - Do not allow .bss to occupy the file space when producing relocatable output
When generating relocatable output SHT_NOBITS sections
were still occupy the file space.

Differential revision: http://reviews.llvm.org/D17857

llvm-svn: 262650
2016-03-03 20:24:14 +00:00
Rafael Espindola 1130935c4a Simplify error handling.
This makes fatal return T when there is no error. This avoids the need
for quite a few temporaries.

llvm-svn: 262626
2016-03-03 16:21:44 +00:00
George Rimar 4cfe572932 [ELF] - add support for relocations against local symbols when producing relocatable output.
There was a known limitation for -r option:
relocations against local symbols were not supported. 
For example rel[a].eh_frame sections contained relocations against sections
and that was not supported for -r before. Patch fixes that.

Differential review: http://reviews.llvm.org/D17813

llvm-svn: 262590
2016-03-03 07:49:35 +00:00
George Rimar 2abc587c1e [ELF] - More direct implementation of edata/etext
As was suggested in mails, this patch implements edata/etext
symbols in a more direct way.
It iterates through PT_LOADs.

Result seems to be the same and equal to gold output.

Differential revision: http://reviews.llvm.org/D17755

llvm-svn: 262369
2016-03-01 19:18:07 +00:00
George Rimar c1034a85d0 [ELF] - do not create special symbols when creating relocatable output
__start_/__end_ <section-name> symbols and other specials like:
preinit_array_start/end
init_array_start/end
fini_array_start/end

should not be created by linker when creating relocatable files.

Differential revision: http://reviews.llvm.org/D17774

llvm-svn: 262366
2016-03-01 19:12:35 +00:00
George Rimar aa4dc20f09 [ELF] - Create _DYNAMIC symbol for dynamic output
lld needs to provide _DYNAMIC symbol when creating a shared library
both bfd and gold do that.

This should fix the https://llvm.org/bugs/show_bug.cgi?id=26732

Differential revision: http://reviews.llvm.org/D17607

llvm-svn: 262348
2016-03-01 16:23:13 +00:00
Rafael Espindola 9907eb0b0a Produce PT_NOTE program headers.
llvm-svn: 262331
2016-03-01 13:23:29 +00:00
George Rimar 6de3f63bb0 [ELF] use of already declared reference instead of indirect access to fields. NFC.
llvm-svn: 262315
2016-03-01 08:46:03 +00:00
Rafael Espindola e0df00b91f Rename elf2 to elf.
llvm-svn: 262159
2016-02-28 00:25:54 +00:00
Rafael Espindola 18f0950783 Report duplicated symbols in bitcode.
llvm-svn: 262076
2016-02-26 21:49:38 +00:00
Rui Ueyama 68e15559b8 Fix broken buildbots.
llvm-svn: 262034
2016-02-26 16:49:54 +00:00
Rui Ueyama 4d169bdca2 Simplify. NFC.
Regarding the comment, it is out of context because it describes
what it does not do there. It got too long because it was originally
two different comments that were simply merged together.
The semantics is described in fixAbsoluteSymbols, so we don't need it.

llvm-svn: 262031
2016-02-26 16:38:39 +00:00
George Rimar 9e8593949d Description of symbols is avalable here:
https://docs.oracle.com/cd/E53394_01/html/E54766/u-etext-3c.html

It is said that:
_etext - The address of _etext is the first 
location after the last read-only loadable segment.

_edata - The address of _edata is the first 
location after the last read-write loadable segment.

_end - If the address of _edata is greater than the address 
of _etext, the address of _end is same as the address of _edata.

In real life _end and _edata has different values for that case.
Both gold/bfd set _edata to the end of the last non SHT_NOBITS section.
This patch do the same for consistency.

It should fix the https://llvm.org/bugs/show_bug.cgi?id=26729.

Differential revision: http://reviews.llvm.org/D17601

llvm-svn: 262019
2016-02-26 14:36:36 +00:00
Rafael Espindola a350e266aa Refactor multiple calls to canBePreempted.
llvm-svn: 262018
2016-02-26 14:33:23 +00:00
Rafael Espindola 993f0273e3 Fix some confusion about what can be preempted.
For shared libraries we allow any weak undefined symbol to eventually be
resolved, even if we never see a definition in another .so. This matches
the behavior when handling other undefined symbols in a shared library.

For executables, we require seeing a definition in a .so or resolve it
to zero. This is also similar to how non weak symbols are handled.

llvm-svn: 262017
2016-02-26 14:27:47 +00:00
Rui Ueyama ad59b65f18 ELF: Add '*' to auto.
llvm-svn: 261941
2016-02-25 23:58:21 +00:00
Rui Ueyama a354c5c433 ELF: Create MIPS .rld_map section earlier.
This is the usual way of instantiating a globally-visible section.

llvm-svn: 261938
2016-02-25 23:54:49 +00:00
Rui Ueyama 3095148015 ELF: Split Writer::addPredefiendSections. NFC.
llvm-svn: 261924
2016-02-25 19:34:37 +00:00
Rui Ueyama 4cea4e81d4 ELF: Split Writer::writeHeader. NFC.
llvm-svn: 261923
2016-02-25 19:28:37 +00:00
George Rimar 12737b7f72 [ELF] - Referencing __start or __stop should keep the section from GC.
This fixes the https://llvm.org/bugs/show_bug.cgi?id=22906 bug.

In GNU Binutils, a reference to start or stop is sufficient to 
prevent the section from being garbage collected.
Patch implements the same behavior for lld.

Differential revision: http://reviews.llvm.org/D17502

llvm-svn: 261840
2016-02-25 08:40:26 +00:00
George Rimar 58941ee12a [ELF2] - Basic implementation of -r/--relocatable
-r, -relocatable - Generate relocatable output

Currently does not have support for files containing 
relocation sections with entries that refer to local 
symbols (like rel[a].eh_frame which refer to sections
and not to symbols)

Differential revision: http://reviews.llvm.org/D14382

llvm-svn: 261838
2016-02-25 08:23:37 +00:00
Rui Ueyama 733153de3c ELF: Do not instantiate InputSectionBase::Discarded.
"Discarded" section is a marker for discarded sections, and we do not
use the instance except for checking its identity. In that sense, it
is just another type of a "null" pointer for InputSectionBase. So,
it doesn't have to be a real instance of InputSectionBase class.

In this patch, we no longer instantiate Discarded section but instead
use -1 as a pointer value. This eliminates a global variable which
needed initialization at startup.

llvm-svn: 261761
2016-02-24 18:33:35 +00:00
Rafael Espindola 795dc5a0fb Move target independent code out of x86_64 only path.
The logic for deciding if an undefined symbol should have the value of a
got entry is not target specific.

llvm-svn: 261760
2016-02-24 18:24:23 +00:00
Rui Ueyama 8fc070d64d ELF: Remove InputSectionBase::isLive and use Live member instead. NFC.
This is also a preparation for ICF.

llvm-svn: 261711
2016-02-24 00:23:15 +00:00
Rafael Espindola 435c00f39c Fix the aarch64 logic for dynamic relocations.
There is nothing aarch64 specific in here. If a symbol can be preempted,
we need to copy the full relocation to the dynamic linker.

If a symbol cannot be preempted, we can make the dynamic linker life
easier and produce a relative relocation.

This is directly equivalent to R_X86_64_64 to R_x86_64_RELATIVE
conversion.

llvm-svn: 261678
2016-02-23 20:19:44 +00:00
Rafael Espindola f7ae359d2c Simplify. NFC.
llvm-svn: 261668
2016-02-23 18:53:29 +00:00
Rafael Espindola 75baf09be5 Don't include tbss's alignment in offset.
The .tbss section is in the middle of a PT_LOAD. Whatever treatment we
give to its address we must also give to the offset.

We were ignoring it for address computations, but not for offset.

Fixes pr26712.

llvm-svn: 261667
2016-02-23 18:39:55 +00:00
Adhemerval Zanella 668ad0ffcb [lld] [ELF/AArch64] Fix R_AARCH64_ABS64 in Shared mode
This patch fixes the R_AARCH64_ABS64 relocation when used in shared mode,
where it requires a dynamic R_AARCH64_RELATIVE relocation. To correct set
the addend on the dynamic relocation (since it will be used by the dynamic
linker), a new TargetInfo specific hook was created (getDynRelativeAddend)
to get the correct addend based on relocation type.

The patch fixes the issues when creating shared library code against
{init,fini}_array, where it issues R_AARCH64_ABS64 relocation against
local symbols.

llvm-svn: 261651
2016-02-23 16:54:40 +00:00
Rafael Espindola 5e8b54afdb Remove a trivial getter.
llvm-svn: 261590
2016-02-22 23:16:05 +00:00
Rafael Espindola 56da313e86 Correctly handle two gd relocations to the same symbol.
Fixes PR26676.

llvm-svn: 261561
2016-02-22 19:57:55 +00:00
Rafael Espindola 7efa5be205 Add support for merging strings with alignment larger than one char.
This reduces the .rodata of scyladb from 4501932 to 4334639 bytes (1.038
times smaller).

I don't think it is critical to support tail merging, just exact
duplicates, but given the code organization it was actually a bit easier
to support both.

llvm-svn: 261327
2016-02-19 14:17:40 +00:00
Rui Ueyama 874e7aee29 Split SymbolTableSection::writeGlobalSymbols.
Previously, we added garbage-collected symbols to the symbol table
and filter them out when we were writing symbols to the file. In
this patch, garbage-collected symbols are filtered out from beginning.

llvm-svn: 261064
2016-02-17 04:56:44 +00:00
Rafael Espindola f8b8b7b5d0 Don't create multiple .got.plt entries for the same symbol.
We were doing it for ifunc symbols.

llvm-svn: 260980
2016-02-16 16:46:31 +00:00
Rafael Espindola 8ae1290058 Merge multiple tdata.* into a single section.
llvm-svn: 260977
2016-02-16 16:12:06 +00:00
Rafael Espindola 1492820aaa Merge multiple .gcc_except_table.* into a single section.
llvm-svn: 260976
2016-02-16 16:05:27 +00:00
Rafael Espindola b032aa1b2b Merge multiple .tbss.* sections into one output section.
llvm-svn: 260974
2016-02-16 15:57:07 +00:00
Rui Ueyama 1ebc8ed78a ELF: Add wildcard pattern matching to SECTIONS linker script command.
Each rule in SECTIONS commands is something like ".foo *(.baz.*)",
which instructs the linker to collect all sections whose name matches
".baz.*" from all files and put them into .foo section.

Previously, we didn't recognize the wildcard character. This patch
adds that feature.

Performance impact is a bit concerning because a linker script can
contain hundreds of SECTIONS rules, and doing pattern matching against
each rule would be too expensive. We could merge all patterns into
single DFA so that it takes O(n) to the input size. However, it is
probably too much at this moment -- we don't know whether the
performance of pattern matching matters or not. So I chose to
implement the simplest algorithm in this patch. I hope this simple
pattern matcher is sufficient.

llvm-svn: 260745
2016-02-12 21:47:28 +00:00
Rafael Espindola 852860e920 make needsPlt a pure predicate.
llvm-svn: 260685
2016-02-12 15:47:37 +00:00
Rui Ueyama 5af8368f8b ELF: Implement the correct semantics of .[cd]tors.
As I noted in the comment, the sorting order of .[cd]tors are
different from .{init,fini}_array's.

http://reviews.llvm.org/D17120

llvm-svn: 260620
2016-02-11 23:41:38 +00:00
Rui Ueyama 717677af35 ELF: Create LinkerScript class to move code out of Writer.
Previously, we had code for linker scripts in Writer. This patch
separates that as LinkerScript class. The class provides a few
functions to query linker scripts and is also a container of some
linker-script-specific information.

Hopefully, Writer will only implement the default behavior and let
the new class handle gotchas regarding linker scripts.

llvm-svn: 260591
2016-02-11 21:17:59 +00:00
George Rimar 2960c9867a [ELF] - Remove R_X86_64_GOTTPOFF from static relocation processing
R_X86_64_TPOFF64 is a dynamic relocation,
it should not appear in static relocation processing.
Patch fixes it.

Differential revision: http://reviews.llvm.org/D16880

llvm-svn: 260508
2016-02-11 11:14:46 +00:00
Rui Ueyama 6f46de21e1 ELF: Sort .[cd]tors by priority as we do for .{init,fini}_array.
llvm-svn: 260477
2016-02-11 01:07:19 +00:00
Rui Ueyama c2dca5d76e Reduce code repetition. NFC.
llvm-svn: 260476
2016-02-11 01:07:18 +00:00
Rafael Espindola ef762f2639 Don't include NOBITS TLS in PT_LOAD.
They don't count for the memory or file size, so this is mostly just a
simplification.

The only noticeable difference should be fewer empty program headers.

llvm-svn: 260465
2016-02-10 23:29:38 +00:00
Rui Ueyama c418570db5 ELF: Implement __attribute__((init_priority(N)) support.
llvm-svn: 260460
2016-02-10 23:20:42 +00:00
Rafael Espindola 4fc6044a5e Split the creation of program headers in a few steps.
IMHO this makes the code easier to read and should help with linker
scripts.

This is strongly based on D16575. The main differences are:

We record a range of sections, not every section in a program header.

scanHeaders takes case of deciding what goes in every program header,
including PT_GNU_RELRO

We create dummy sections for the start of the file

With this, program header creation has 3 isolated stages:

Map sections to program headers.
Assign addresses to *sections*
Looking at sections find the address and size of each program header.

Thanks to George Rimar for the initial version.

llvm-svn: 260453
2016-02-10 22:43:13 +00:00
Rafael Espindola a0a65f973a Use the plt entry as the address of some symbols.
This is the function equivalent of a copy relocation.

Since functions are expected to change sizes, we cannot use copy
relocations. In situations where one would be needed, what is done
instead is:
* Create a plt entry
* Output an undefined symbol whose addr is the plt entry.

The dynamic linker makes sure any shared library uses the plt entry as
the function address.

llvm-svn: 260224
2016-02-09 15:11:01 +00:00
Rafael Espindola a64f7d1148 Fix the alignment of PT_PHDR.
We were claiming it was aligned to 8 bytes even on 32 bit files, which
is not the case.

llvm-svn: 260102
2016-02-08 15:33:53 +00:00
Simon Atanasyan b76108a63a [ELF][MIPS] Add comments to describe __gnu_local_gp magic symbol
NFC. Follow-up to r259781.

llvm-svn: 260027
2016-02-07 12:09:40 +00:00
Rafael Espindola d0078b2dba Sort headers. NFC.
llvm-svn: 259959
2016-02-06 00:06:26 +00:00
Rafael Espindola 95708931cf Fix PT_GNU_RELRO computation.
We were not including TLS sections.

llvm-svn: 259945
2016-02-05 22:55:06 +00:00
Rui Ueyama 4197a6ab1b ELF: Make Out<ELFT> initialization less error-prone.
Previously, it was easy to leave some Out<ELFT> fields uninitialized
because assignments to the fields are mixed with output section
instantiations. In this patch, I separate initializations from assignments
to improve readability.

http://reviews.llvm.org/D16864

llvm-svn: 259899
2016-02-05 18:41:40 +00:00
Rafael Espindola abebed982a Rename IsUsedInDynamicReloc to MustBeInDynSym.
The variable was marking various cases where a symbol must be included
in the dynamic symbol table. Being used by a dynamic relocation was only
one of them.

llvm-svn: 259889
2016-02-05 15:27:15 +00:00
Rafael Espindola d30eb7d77d Centralize most calls to setUsedInDynamicReloc.
llvm-svn: 259887
2016-02-05 15:03:10 +00:00
Rui Ueyama 0e53c7dd2c ELF: Make names for TLS module indices shorter.
The previous names contained "Local" and "Current", but what we
are handling is always local and current, so they were redundant.

TlsIndex comes from "tls_index" struct that Ulrich Drepper is using
in this document to describe this data structure in GOT.

llvm-svn: 259852
2016-02-05 00:10:02 +00:00
Rui Ueyama f9cec37880 Merge conditions of two nested `if`s.
llvm-svn: 259843
2016-02-04 23:18:22 +00:00
Rafael Espindola de9857e3c1 Avoid code duplication when creating dynamic relocations.
Another case where we currently have almost duplicated code is the
creation of dynamic relocations. First to decide if we need one, then to
decide what to write.

This patch fixes it by passing more information from the relocation scan
to the section writing code. This is the same idea used for r258723.

I actually think it should be possible to simplify this further by
reordering things a bit in the writer. For example, we should be able to
represent almost every position in the file with an OutputSeciton and
offset. When writing it out we then just need to add the offset to the
OutputSection VA.

llvm-svn: 259829
2016-02-04 21:33:05 +00:00
Simon Atanasyan 597df21eb2 [ELF][MIPS] Add handling for __gnu_local_gp symbol
This symbol is a "fake" symbol like "_gp_disp" and denotes
the GOT + 0x7FF0 value.

llvm-svn: 259781
2016-02-04 12:09:49 +00:00
Simon Atanasyan 4b03451cac [ELF][MIPS] Replace needsMipsLocalGot function by canBePreempted
Symbol does not need an entry i the 'global' part of GOT if it cannot be
preempted. So canBePreempted fully satisfies us at least for now.

llvm-svn: 259779
2016-02-04 11:51:45 +00:00
Simon Atanasyan 170356ba32 [ELF][MIPS] Always create global GOT entry for symbols defined in DSO
If relocation against symbol requires GOT entry creation and this symbol
is defined in DSO, the GOT entry should be created in the 'global' part
of the GOT even if we link executable file. Also we do not need to create
a dynamic symbol table entry for global symbol corresponding to the
local GOT entry.

llvm-svn: 259778
2016-02-04 11:51:39 +00:00
Rui Ueyama cbe392629b ELF: Do not exit if it cannot open an output file.
It can fail to open an output file for various reasons, including
lack of permission, too long filename, or the output file is not
a mmap'able file.

llvm-svn: 259596
2016-02-02 22:48:04 +00:00
Rafael Espindola 0df1b0bf42 Expand comment a bit.
I have spent some time prototyping this idea. While it seems to work, I
now think it is probably not worth it.

llvm-svn: 259516
2016-02-02 15:45:37 +00:00
Simon Atanasyan 40d25f3358 [ELF] Finalize .dynamic section at the end
Some dynamic table tags like RELSZ and PLTRELSZ depens on result of
finalizing corresponding relocation sections. Therefore we have to
finalize .dynamic section at the end.

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

llvm-svn: 259478
2016-02-02 09:07:47 +00:00
Rui Ueyama 82b4288b1a ELF: Simplify and add comments.
llvm-svn: 259474
2016-02-02 07:50:18 +00:00
Rui Ueyama 05aecba118 Do not use "No" prefix for boolean variables to make it simpler. NFC.
llvm-svn: 259472
2016-02-02 07:18:15 +00:00
Rui Ueyama f263c4b0f1 ELF: Move GOT relocation handler code to one place. NFC.
llvm-svn: 259471
2016-02-02 07:07:35 +00:00
Rui Ueyama 554f273b9d ELF: Move PLT relocation handler to one place. NFC.
llvm-svn: 259470
2016-02-02 07:07:34 +00:00
Rui Ueyama 343f7e5d8d ELF: Move GNU_IFUNC relocation handler to one place. NFC.
llvm-svn: 259468
2016-02-02 06:29:10 +00:00
Rui Ueyama 6d3874b995 ELF: Move copy relocation handler to one place. NFC.
llvm-svn: 259467
2016-02-02 06:08:08 +00:00
Rui Ueyama 1ac1338ac8 ELF: Move code for MIPS local GOT entries to one place. NFC.
llvm-svn: 259466
2016-02-02 05:55:28 +00:00
Rui Ueyama cdb7882cb2 Always initialize Out<ELFT> members.
Instead of leave unused fields as is, set them to nullptr.
Currnetly this is NFC, but if you call writeResults more than
once, you should be able to see the difference.

llvm-svn: 259444
2016-02-02 00:35:49 +00:00
Rui Ueyama 21923996f8 ELF: Do not call fatal() if relocation contraints are not satisfied.
http://reviews.llvm.org/D16648

llvm-svn: 259435
2016-02-01 23:28:21 +00:00
Rafael Espindola 4d91f7fc25 Simplify.
Now that we remember the StringRefs when they are first added, we can
write the table in any order.

llvm-svn: 259417
2016-02-01 21:52:00 +00:00
Rui Ueyama b5a6970ace ELF: Teach SymbolBody about how to get its addresses.
Previously, the methods to get symbol addresses were somewhat scattered
in many places. You can use getEntryAddr returns the address of the symbol,
but if you want to get the GOT address for the symbol, you needed to call
Out<ELFT>::Got->getEntryAddr(Sym). This change adds new functions, getVA,
getGotVA, getGotPltVA, and getPltVA to SymbolBody, so that you can use
SymbolBody as the central place to ask about symbols.

http://reviews.llvm.org/D16710

llvm-svn: 259404
2016-02-01 21:00:35 +00:00
Rui Ueyama 5e378ddc1e Consistenly use sizeof(uintX_t) instead of ELFT::Is64Bits ? 8 : 4.
llvm-svn: 259250
2016-01-29 22:18:57 +00:00
Rui Ueyama c516ae1719 ELF: Make Target's member function names shorter.
llvm-svn: 259147
2016-01-29 02:33:45 +00:00
Rui Ueyama c112c1be69 Rename includeInDynamicSymtab -> includeInDynsym.
llvm-svn: 259144
2016-01-29 02:17:01 +00:00
Rui Ueyama 724d625c7a ELF: Remove accessors from Target.
These accessors do not provide values. We can simply make the variables public.

llvm-svn: 259141
2016-01-29 01:49:32 +00:00
Rafael Espindola e2c2461a6b Merge identical strings.
This avoids the need to have reserve and addString in sync.

We avoid hashing the global symbols again. This means that we don't
merge a global symbol that has the same name as some other string, but
that doesn't seem very common. The string table size is the same in
clang an scylladb with or without hashing global symbols again.

llvm-svn: 259136
2016-01-29 01:24:25 +00:00
Rui Ueyama baf16512ea Rename isTlsOptimized -> canRelaxTls.
This function is a predicate that a given relocation can be relaxed.
The previous name implied that it returns true if a given relocation
has already been optimized away.

llvm-svn: 259128
2016-01-29 00:20:12 +00:00
Rui Ueyama 0e36e09b9c Update comments.
llvm-svn: 259127
2016-01-29 00:20:09 +00:00
Rui Ueyama c2a0d7e351 ELF: Report more than one undefined symbols if exist.
http://reviews.llvm.org/D16643

llvm-svn: 259107
2016-01-28 22:56:29 +00:00
Rui Ueyama 64cfffd333 ELF: Rename error -> fatal and redefine error as a non-noreturn function.
In many situations, we don't want to exit at the first error even in the
process model. For example, it is better to report all undefined symbols
rather than reporting the first one that the linker picked up randomly.

In order to handle such errors, we don't need to wrap everything with
ErrorOr (thanks for David Blaikie for pointing this out!) Instead, we
can set a flag to record the fact that we found an error and keep it
going until it reaches a reasonable checkpoint.

This idea should be applicable to other places. For example, we can
ignore broken relocations and check for errors after visiting all relocs.

In this patch, I rename error to fatal, and introduce another version of
error which doesn't call exit. That function instead sets HasError to true.
Once HasError becomes true, it stays true, so that we know that there
was an error if it is true.

I think introducing a non-noreturn error reporting function is by itself
a good idea, and it looks to me that this also provides a gradual path
towards lld-as-a-library (or at least embed-lld-to-your-program) without
sacrificing code readability with lots of ErrorOr's.

http://reviews.llvm.org/D16641

llvm-svn: 259069
2016-01-28 18:40:06 +00:00
Rafael Espindola 10d71ffc65 Remove another case of almost duplicated code.
Were had very similar code for deciding to keep a local symbol and for
actually writing it.

llvm-svn: 258958
2016-01-27 18:04:26 +00:00
Rafael Espindola 81e05525e3 Handle local symbols in discarded sections.
We were reserving space for them but never writing them out.

llvm-svn: 258948
2016-01-27 17:09:37 +00:00
Rui Ueyama 231b5e23c5 Simplify. NFC.
llvm-svn: 258796
2016-01-26 07:17:29 +00:00
Rafael Espindola de06936f28 Avoid almost duplication in .dynamic finalize and write.
There are a few cases where we have almost duplicated code.

This patches fixes the simplest: the finalize and write of dynamic
section. Right now they have to have exactly the same structure to
decide if a DT_* entry is needed and then to actually write it.

We cannot just write it to a std::vector in the first pass since
addresses have not been computed yet.

llvm-svn: 258723
2016-01-25 21:32:04 +00:00
Sean Silva f1c5a0f09c [ELF] Avoid unnecessary global variable.
Summary: It looks like this snuck through in r256143/D15383.

Reviewers: ruiu, grimar

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

llvm-svn: 258599
2016-01-23 01:49:37 +00:00
George Rimar bfd29a1567 [ELF] - Refactoring of Writer<ELFT>::scanRelocs()
Code for handling TLS relocations was moved out scanRelocs() to new function handleTlsRelocations().
That is because scanRelocs already too large to put more TLS code into it.

Differential revision: http://reviews.llvm.org/D16354

llvm-svn: 258392
2016-01-21 09:14:22 +00:00