Commit Graph

105 Commits

Author SHA1 Message Date
Rui Ueyama d7e4a281c4 ELF: Make some functions constant. NFC.
This is a preparation for ICF.

llvm-svn: 261710
2016-02-24 00:23:13 +00:00
Rafael Espindola f7ae359d2c Simplify. NFC.
llvm-svn: 261668
2016-02-23 18:53:29 +00:00
Rafael Espindola 5e8b54afdb Remove a trivial getter.
llvm-svn: 261590
2016-02-22 23:16:05 +00:00
George Rimar e9e1d323c6 Fixed comments formatting. NFC.
llvm-svn: 261214
2016-02-18 15:17:01 +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
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
Rui Ueyama 512c61df1c Define SymbolBody::getSize instead of getSymSize(SymbolBody&). NFC.
llvm-svn: 259613
2016-02-03 00:12:24 +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 c516ae1719 ELF: Make Target's member function names shorter.
llvm-svn: 259147
2016-01-29 02:33:45 +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
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 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 25472ee9c2 Remove unnecessary object:: prefix.
llvm-svn: 258563
2016-01-22 21:49:07 +00:00
George Rimar 237b218770 [ELF] - fix possible UB when dereferencing null
In InputSection.cpp it was possible to dereference null.
Had to change signature of relocateTlsOptimize to accept pointer instead of reference.

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

llvm-svn: 258508
2016-01-22 18:02:28 +00:00
Rui Ueyama 88d79a06b7 Style change. NFC.
llvm-svn: 258427
2016-01-21 20:18:36 +00:00
Simon Atanasyan 56ab5f0289 [ELF][MIPS] Initial support of MIPS local GOT entries
Some MIPS relocation (for now R_MIPS_GOT16) requires creation of GOT
entries for symbol not included in the dynamic symbol table. They are
local symbols and non-local symbols with 'local' visibility. Local GOT
entries occupy continuous block between GOT header and regular GOT
entries.

The patch adds initial support for handling local GOT entries. The main
problem is allocating local GOT entries for local symbols. Such entries
should be initialized by high 16-bit of the symbol value. In ideal world
there should be no duplicated entries with the same values. But at the
moment of the `Writer::scanRelocs` call we do not know a value of the
symbol. In this patch we create new local GOT entry for each relocation
against local symbol, though we can exhaust GOT quickly. That needs to
be optimized later. When we calculate relocation we know a final symbol
value and request local GOT entry index. To do that we maintain map
between addresses and local GOT entry indexes. If we start to calculate
relocations in parallel we will have to serialize access to this map.

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

llvm-svn: 258388
2016-01-21 05:33:23 +00:00
Rui Ueyama b705b10012 ELF: Remove dead code.
R_X86_64_PLT32 is handled in the same way as R_X86_64_PC32 by
relocateOne(), so this function does not seems to be needed.
Without this code, all tests still pass.

http://reviews.llvm.org/D15971

llvm-svn: 257203
2016-01-08 20:11:47 +00:00
Rui Ueyama 3a7c2f6f44 ELF: Simplify Target::isSizeReloc and add comments.
All non-trivial relocation decisions need explanations like this
to help readers understand not only how relocations are handled but
why they are handled these ways. This is a start.

llvm-svn: 257119
2016-01-08 00:13:23 +00:00
Rui Ueyama 70eed364fc Simplify MipsReginfoInputSection.
MipsReginfoInputSection is basically just a container of Elf_Mips_Reginfo
struct. This patch makes that struct directly accessible from others.

llvm-svn: 256984
2016-01-06 22:42:43 +00:00
Rui Ueyama 58a636ac06 Add llvm_unreachable to unreachable code.
The original code was an infinite recursion when the control reaches there.

llvm-svn: 256981
2016-01-06 22:01:25 +00:00
Simon Atanasyan 57830b60dc [ELF][MIPS] Implement R_MIPS_GPREL16/R_MIPS_GPREL32 relocations
The R_MIPS_GPREL16 / R_MIPS_GPREL32 relocations use the following
expressions for calculations:
```
local symbol:  S + A + GP0 - GP
global symbol: S + A - GP

GP  - Represents the final gp value, i.e. _gp symbol
GP0 - Represents the gp value used to create the relocatable object
```
The GP0 value is taken from the .reginfo data section defined by an object
file. To implement that I keep a reference to `MipsReginfoInputSection`
in the `ObjectFile` class. This reference is used by the
`ObjectFile::getMipsGp0` method to return the GP0 value.

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

llvm-svn: 256416
2015-12-25 13:02:13 +00:00
George Rimar 6ab275c0c8 [ELF] - Fixed handling relocations against zero sized .eh_frame section.
The file crtbeginT.o has relocations pointing to the start of an empty
.eh_frame that is known to be the first in the link. It does that to
identify the start of the output .eh_frame. Handle this special case.

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

llvm-svn: 256414
2015-12-25 09:51:42 +00:00
Rafael Espindola 02ce26a1b4 Delete DefinedAbsolute.
There are 3 symbol types that a .bc can provide during lto: defined,
undefined, common.

Defined and undefined symbols have already been refactored. I was
working on common and noticed that absolute symbols would become an
oddity: They would be the only symbol type present in a .o but not in
a.bc.

Looking a bit more, other than the special section number they were only
used for special rules for computing values. In that way they are
similar to TLS, and we don't have a DefinedTLS.

This patch deletes it. With it we have a reasonable rule of the thumb
for having a symbol kind: It exists if it has special resolution
semantics.

llvm-svn: 256383
2015-12-24 14:22:24 +00:00
Rui Ueyama da735325fa Mark .eh_frame sections as live by default. NFC.
This change eliminates a string comparison from the
garbage collector.

llvm-svn: 256378
2015-12-24 10:08:54 +00:00
Rafael Espindola 4d4b06a0f8 Split Defined and DefinedElf.
This is similar to what was done for Undefined and opens the way for
having a symbol defined in bitcode.

llvm-svn: 256354
2015-12-24 00:47:42 +00:00
George Rimar 0b8ed1d162 [ELF] - fixed not properly handled @GOTTPOFF relocation against local symbols
This patch changes sequence of applying relocations, moving tls optimized relocation handling code before code for other locals.
Without that change relocation @GOTTPOFF against local symbol caused runtime error ("unrecognized reloc ...").
That change also should fix other tls optimized relocations, but I did not check them, that's a field for another patch.

R_X86_64_GOTTPOFF relocations against locals can be found when linking against libc.a(malloc.o):
000000000036 000600000016 R_X86_64_GOTTPOFF 0000000000000000 libc_tsd_MALLOC - 4
000000000131 000600000016 R_X86_64_GOTTPOFF 0000000000000000 libc_tsd_MALLOC - 4

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

llvm-svn: 256145
2015-12-21 10:37:33 +00:00
Simon Atanasyan 1d7df40711 [ELF][MIPS] MIPS .reginfo sections handling
MIPS .reginfo section provides information on the registers used by
the code in the object file. Linker should collect this information and
write .reginfo section in the output file. This section contains a union
of used registers masks taken from input .reginfo sections and final
value of the `_gp` symbol.

For details see the "Register Information" section in Chapter 4 in the
following document:
ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf

The patch implements .reginfo sections handling with a couple missed
features: a) it does not put output .reginfo section into the separate
REGINFO segment; b) it does not merge `ri_cprmask` masks from input
section. These features will be implemented later.

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

llvm-svn: 256119
2015-12-20 10:57:34 +00:00
George Rimar 6f17e09307 [ELF] - implemented @indntpoff (x86) relocation and its optimization.
@indntpoff is similar to @gotntpoff, but for use in position dependent code. While @gotntpoff resolves to GOT slot address relative to the
start of the GOT in the movl or addl instructions, @indntpoff resolves to the
absolute GOT slot address. ("ELF Handling For Thread-Local Storage", Ulrich Drepper).

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

llvm-svn: 255884
2015-12-17 09:32:21 +00:00
Rui Ueyama 02dfd496b0 ELF: Rename relocNeedsCopy -> needsCopyRel
Just "copy" was a bit too ambiguous to say about copy relocations.

llvm-svn: 255866
2015-12-17 01:18:40 +00:00
Rui Ueyama 62d0e3297b ELF: Rename isTLS -> isTls for consistency.
llvm-svn: 255855
2015-12-17 00:04:18 +00:00
Simon Atanasyan 09dae7c3e7 [ELF][MIPS] Handle R_MIPS_HI16/LO16 relocations against _gp_disp symbol
The `_gp_disp` is a magic symbol designates offset between start of
function and gp pointer into GOT. Only `R_MIPS_HI16` and `R_MIPS_LO16`
relocations are permitted with `_gp_disp`. The patch adds the `_gp_disp`
as an ignored symbol and adjusts symbol value before call the `relocateOne`
for `R_MIPS_HI16/LO16` relocations.

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

llvm-svn: 255768
2015-12-16 14:45:09 +00:00
Simon Atanasyan dddbeb7a46 [ELF][MIPS] Match paired relocation using relocation type and symbol index
If we have R_MIPS_HI16 relocation, the paired relocation is the next
R_MIPS_LO16 relocation with the same symbol as a target.

llvm-svn: 255452
2015-12-13 06:49:08 +00:00
George Rimar 48651489b3 [ELF] - R_X86_64_SIZE64/R_X86_64_SIZE32 relocations implemented.
R_X86_64_SIZE64/R_X86_64_SIZE32 relocations were introduced in 0.98v of "System V Application Binary Interface x86-64" (http://www.x86-64.org/documentation/abi.pdf).

Calculation for them is Z + A, where:
Z - Represents the size of the symbol whose index resides in the relocation entry.
A - Represents the addend used to compute the value of the relocatable field.

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

llvm-svn: 255332
2015-12-11 08:59:37 +00:00
George Rimar 25411f2558 [ELF] - Implemented @tlsgd optimization (GD->IE case, x64).
"Ulrich Drepper, ELF Handling For Thread-Local Storage" (5.5 x86-x64 linker optimizations, http://www.akkadia.org/drepper/tls.pdf) shows how GD can be optimized to IE.
This patch implements the optimization.

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

llvm-svn: 254713
2015-12-04 11:20:13 +00:00
Simon Atanasyan 09b3e3685f [ELF] MIPS paired R_MIPS_HI16/LO16 relocations support
Some MIPS relocations including `R_MIPS_HI16/R_MIPS_LO16` use combined
addends. Such addend is calculated using addends of both paired relocations.
Each `R_MIPS_HI16` relocation is paired with the next `R_MIPS_LO16`
relocation. ABI requires to compute such combined addend in case of REL
relocation record format only.

For details see p. 4-17 at
ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf

This patch implements lookup of the next paired relocation suing new
`InputSectionBase::findPairedRelocLocation` method. The primary
disadvantage of this approach is that we put MIPS specific logic into
the common code. The next disadvantage is that we lookup `R_MIPS_LO16`
for each `R_MIPS_HI16` relocation, while in fact multiple `R_MIPS_HI16`
might be paired with the single `R_MIPS_LO16`. From the other side
this way allows us to keep `MipsTargetInfo` class stateless and implement
later relocation handling in parallel.

This patch does not support `R_MIPS_HI16/R_MIPS_LO16` relocations against
`_gp_disp` symbol. In that case the relocations use a special formula for
the calculation. That will be implemented later.

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

llvm-svn: 254461
2015-12-01 21:24:45 +00:00
George Rimar 90cd0a8234 [ELF] - Fixed bug leading to miss of tls relocation when @tlsgd and @gottpoff relocations were used at the same time.
Combination of @tlsgd and @gottpoff at the same time leads to miss of R_X86_64_TPOFF64 dynamic relocation. Patch fixes that.

@tlsgd(%rip) - Allocate two contiguous entries in the GOT to hold a tls index
structure (for passing to tls get addr).
@gottpoff(%rip) - Allocate one GOT entry to hold a variable offset in initial TLS
block (relative to TLS block end, %fs:0).

The same situation can be observed for x86 (probably others too, not sure) with corresponding for that target relocations: @tlsgd, @gotntpoff.

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

llvm-svn: 254443
2015-12-01 19:20:26 +00:00
George Rimar b17f739808 Reapply r254428.
Fix was:
uint32_t getLocalTlsIndexVA() { return getVA() + LocalTlsIndexOff; }
=>
uint32_t getLocalTlsIndexVA() { return Base::getVA() + LocalTlsIndexOff; }
Both works for my MSVS.

Original commit message:
[ELF] - Refactor of tls_index implementation for tls local dynamic model.

Patch contains the next 2 changes:
1) static variable Out<ELFT>::LocalModuleTlsIndexOffset moved to Out<ELFT>::Got. At fact there is no meaning for it to be separated from GOT class because at each place of using it anyways needs to call GOT`s getVA(). Also it is impossible to have that offset and not have GOT.
2) addLocalModuleTlsIndex -> addLocalModelTlsIndex (word "Module" changed to "Model"). Not sure was it a mistype or not but I think that update is closer to Urlich terminology.

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

llvm-svn: 254433
2015-12-01 18:24:07 +00:00
George Rimar 60849f2913 revert r254428 [ELF] - Refactor of tls_index implementation for tls local dynamic model.
It failed buildbot:
http://lab.llvm.org:8011/builders/llvm-clang-lld-x86_64-scei-ps4-ubuntu-fast/builds/3782/steps/build/logs/stdio

Target.cpp
In file included from /home/buildbot/Buildbot/Slave/llvm-clang-lld-x86_64-scei-ps4-ubuntu-fast/llvm.src/tools/lld/ELF/Target.cpp:20:
/home/buildbot/Buildbot/Slave/llvm-clang-lld-x86_64-scei-ps4-ubuntu-fast/llvm.src/tools/lld/ELF/OutputSections.h:136:42: error: use of undeclared identifier 'getVA'
  uint32_t getLocalTlsIndexVA() { return getVA() + LocalTlsIndexOff; }

llvm-svn: 254432
2015-12-01 18:11:16 +00:00
George Rimar b8bfd25239 [ELF] - Target interface simplification, getGotRefReloc() removed.
Removes Target::getGotRefReloc() method to simplify Target class a little.

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

llvm-svn: 254429
2015-12-01 17:52:40 +00:00
George Rimar 0ec3f306d4 [ELF] - Refactor of tls_index implementation for tls local dynamic model.
Patch contains the next 2 changes:
1) static variable Out<ELFT>::LocalModuleTlsIndexOffset moved to Out<ELFT>::Got. At fact there is no meaning for it to be separated from GOT class because at each place of using it anyways needs to call GOT`s getVA(). Also it is impossible to have that offset and not have GOT.
2) addLocalModuleTlsIndex -> addLocalModelTlsIndex (word "Module" changed to "Model"). Not sure was it a mistype or not but I think that update is closer to Urlich terminology.

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

llvm-svn: 254428
2015-12-01 17:45:31 +00:00
Hal Finkel f950595ea1 Fix a comment typo (cashe -> cache)
llvm-svn: 254111
2015-11-25 23:54:53 +00:00
George Rimar 6713cf8a52 [ELF] - Implemented optimizations for @tlsld and @tlsgd
Implements @tlsld (LD to LE) and @tlsgd (GD to LE) optimizations.
Patch does not implement the GD->IE case for @tlsgd.

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

llvm-svn: 254101
2015-11-25 21:46:05 +00:00
George Rimar d23970f778 [ELF/x86] Implemented R_386_TLS_LE_32, R_386_TLS_LE relocations.
This patch implements next relocations:
R_386_TLS_LE - Negative offset relative to static TLS (GNU version).
R_386_TLS_LE_32 - Offset relative to static TLS block.

These ones are created when using next code sequences:
* @tpoff - The operator must be used to compute an immediate value. The linker will report
an error if the referenced variable is not defined or it is not code for the executable
itself. No GOT entry is created in this case.
* @ntpoff Calculate the negative offset of the variable it is added to relative to the static TLS block.
The operator must be used to compute an immediate value. The linker will report
an error if the referenced variable is not defined or it is not code for the executable
itself. No GOT entry is created in this case.

Information was found in Ulrich Drepper, ELF Handling For Thread-Local Storage, http://www.akkadia.org/drepper/tls.pdf, (6.2, p76)

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

llvm-svn: 254090
2015-11-25 20:41:53 +00:00
George Rimar 1393477fd7 [ELF] - simplify Target interface, relocPointsToGot() removed.
https://docs.oracle.com/cd/E19683-01/817-3677/chapter6-26/index.html says:
R_386_GOTPC
Resembles R_386_PC32, except that it uses the address of the global offset table in its calculation. The symbol referenced in this relocation normally is _GLOBAL_OFFSET_TABLE_, which also instructs the link-editor to create the global offset table.

Currently _GLOBAL_OFFSET_TABLE_ has value == zero. And we use GOT address to calculate the relocation. This patch does not changes that. It just removes the method which is used only for x86. So it is close to non functional change.

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

llvm-svn: 254088
2015-11-25 20:20:31 +00:00
George Rimar 77d1cb1ddf [ELF2] - Optimization for R_X86_64_GOTTPOFF relocation.
R_X86_64_GOTTPOFF is not always requires GOT entries. Some relocations can be converted to local ones.

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

llvm-svn: 253966
2015-11-24 09:00:06 +00:00
Igor Kudrin 5d2bffdd57 [ELF/AArch64] Add support for R_AARCH64_ADR_GOT_PAGE and R_AARCH64_LD64_GOT_LO12_NC.
With these relocations, it is now possible to build a simple "hello world"
program for AArch64 Debian.

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

llvm-svn: 253957
2015-11-24 06:48:31 +00:00
Igor Kudrin e7ad0937bb [ELF2] getPLTRefReloc() -> getPltRefReloc().
llvm-svn: 253351
2015-11-17 17:47:53 +00:00
George Rimar 687138c7d1 [ELF2] - Implemented R_X86_64_GOTTPOFF relocation
Generates single GOT entry, R_X86_64_TPOFF64 is added to RelaDyn.

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

llvm-svn: 253049
2015-11-13 16:28:53 +00:00
George Rimar 4b40ebce66 [ELF2] - fix of eh-frame-merge.s and eh-frame-merge.s tests fails for win32 configuration.
llvm-svn: 253043
2015-11-13 13:44:59 +00:00