Commit Graph

142 Commits

Author SHA1 Message Date
Rafael Espindola 6f92e14ce2 Use DefinedSynthetic for _gp* symbols.
The test changes to put _gp* in the .got section matches what both bfd
and gold do.

llvm-svn: 266067
2016-04-12 13:26:51 +00:00
Rafael Espindola 8396f72f7b Simplify handling of mips gp* symbols.
Give them values instead of computing it during relocation.

llvm-svn: 265986
2016-04-11 20:34:27 +00:00
Rafael Espindola 03ef404e97 Simplify the creation of __rel[a]_iplt_{start,end}.
They can be regular DefinedSynthetic.

llvm-svn: 265981
2016-04-11 19:14:59 +00:00
Rui Ueyama f8baa66056 ELF: Implement --start-lib and --end-lib
start-lib and end-lib are options to link object files in the same
semantics as archive files. If an object is in start-lib and end-lib,
the object is linked only when the file is needed to resolve
undefined symbols. That means, if an object is in start-lib and end-lib,
it behaves as if it were in an archive file.

In this patch, I introduced a new notion, LazyObjectFile. That is
analogous to Archive file type, but that works for a single object
file instead of for an archive file.

http://reviews.llvm.org/D18814

llvm-svn: 265710
2016-04-07 19:24:51 +00:00
Rafael Espindola 74031ba1e9 Simplify dynamic relocation creation.
The position of a relocation can always be expressed as an offset in an
output section.

llvm-svn: 265682
2016-04-07 15:20:56 +00:00
Rafael Espindola 5e34568f79 Use a bit in SymbolBody to store CanKeepUndefined.
UndefinedElf for 64 bits goes from 72 to 64 bytes.

llvm-svn: 265543
2016-04-06 14:31:03 +00:00
Rafael Espindola f47657301b Change the type hierarchy for undefined symbols.
We have to differentiate undefined symbols from bitcode and undefined
symbols from other sources.

Undefined symbols from bitcode should not inhibit the symbol being
internalized. Undefined symbols from other sources should.

llvm-svn: 265536
2016-04-06 13:22:41 +00:00
Rafael Espindola d9a1717efc Remove redundant argument. NFC.
llvm-svn: 265386
2016-04-05 11:47:46 +00:00
Peter Collingbourne d0856a6bb2 ELF: Make SymbolBody::compare a non-template function.
Differential Revision: http://reviews.llvm.org/D18781

llvm-svn: 265372
2016-04-05 00:47:58 +00:00
Rui Ueyama b5792b231b Rename Other -> StOther.
"Other" as a name is too generic, so name it StOther.

llvm-svn: 265332
2016-04-04 19:09:08 +00:00
Rui Ueyama dd41807392 Add comments.
llvm-svn: 265320
2016-04-04 18:15:38 +00:00
Rafael Espindola 193b99c53c Try to fix the windows build.
MSVC doesn't want StringRef in an union.

llvm-svn: 265297
2016-04-04 14:31:20 +00:00
Rafael Espindola ccfe3cb3d6 Don't store an Elf_Sym for most symbols.
Our symbol representation was redundant, and some times would get out of
sync. It had an Elf_Sym, but some fields were copied to SymbolBody.

Different parts of the code were checking the bits in SymbolBody and
others were checking Elf_Sym.

There are two general approaches to fix this:
* Copy the required information and don't store and Elf_Sym.
* Don't copy the information and always use the Elf_Smy.

The second way sounds tempting, but has a big problem: we would have to
template SymbolBody. I started doing it, but it requires templeting
*everything* and creates a bit chicken and egg problem at the driver
where we have to find ELFT before we can create an ArchiveFile for
example.

As much as possible I compared the test differences with what gold and
bfd produce to make sure they are still valid. In most cases we are just
adding hidden visibility to a local symbol, which is harmless.

In most tests this is a small speedup. The only slowdown was scylla
(1.006X). The largest speedup was clang with no --build-id, -O3 or
--gc-sections (i.e.: focus on the relocations): 1.019X.

llvm-svn: 265293
2016-04-04 14:04:16 +00:00
Rui Ueyama bfc1d9d976 Remove DefinedElf class.
DefinedElf was a superclass of DefinedRegular and SharedSymbol classes
and represented the notion of defined symbols created for ELF symbols.

It turned out that we didn't use that class often. We had only two
occurrences of dyn_cast'ing to DefinedElf, and both were easily
rewritten without it.

The class was also a bit confusing. The concept of "created for ELF
symbol" is orthogonal to defined/undefined types. However, we had
two distinct classes, DefinedElf and UndefinedElf.

This patch simply removes the class. Now the class hierarchy is one
level shallower.

llvm-svn: 265234
2016-04-02 18:06:18 +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
Davide Italiano 04d6aa2b38 [LTO] Include in .symtab/.dynsym symbols introduced by optimizations.
Some optimizations, e.g. SimplifyLibCalls, can replace functions with
others as part of the lowering, e.g. printf => puts.
The new symbols don't have the isUsedInRegularObj flag set so they
don't get included in the final symbol table (and dynamic symbol
table), and the dynamic linker gets confused. Include  them as a fix.

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

llvm-svn: 264688
2016-03-29 00:15:52 +00:00
Davide Italiano 828ac541ef [LTO] Internalize symbols.
IPO doesn't work very well across symbols referenced
by others TUs. The linker here tries to evaluate
which symbols are safe to internalize and switches
their linkage.

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

llvm-svn: 264585
2016-03-28 15:44:21 +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
Sean Silva e32368fc6e Mark SymbolBody::getSymbol as `const`.
llvm-svn: 264094
2016-03-22 21:04:03 +00:00
Rui Ueyama 9328b2cdde Use ELFT instead of ELFFile<ELFT>.
llvm-svn: 263510
2016-03-14 23:16:09 +00:00
Rui Ueyama 71a8686244 Revert r263366: Cosmetic change to reduce repetitions. NFC.
This reverts commit r263366 because it caused link errors in some linkers.

llvm-svn: 263471
2016-03-14 19:37:58 +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 a777e01f02 Cosmetic change to reduce repetitions. NFC.
llvm-svn: 263366
2016-03-13 04:40:17 +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 ebce18b6fc Remove obsolete comment.
llvm-svn: 263280
2016-03-11 19:04:56 +00:00
Rafael Espindola 1f5b70f64f Represent local symbols with DefinedRegular.
llvm-svn: 263237
2016-03-11 14:21:37 +00:00
Rafael Espindola 87d9f10733 Compute value of local symbol with getVA.
llvm-svn: 263225
2016-03-11 12:19:05 +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
George Rimar 56e0d53e92 [ELF] - Move initSymbols() to Driver.cpp. NFC.
That is followup for http://reviews.llvm.org/D18047
patch. initSymbols() moved to Driver.cpp and made static.

llvm-svn: 263214
2016-03-11 10:07:18 +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 5a3dcf4edf [ELF] - Do not call doInitSymbols for all ELFTs
It looks a bit wierd that we have to initialize symbols for all ELFT 
types when we use only one ELFT for link. We can only init those
that we need. Patch fixes it.

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

llvm-svn: 263133
2016-03-10 17:38:49 +00:00
Rafael Espindola 4f29c1a337 lto: Record visibility in defined symbols.
llvm-svn: 262835
2016-03-07 17:14:36 +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
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
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 e0df00b91f Rename elf2 to elf.
llvm-svn: 262159
2016-02-28 00:25:54 +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
Rui Ueyama 0b28952993 ELF: Implement ICF.
This patch implements the same algorithm as LLD/COFF's ICF. I'm
not going to repeat the same description about how it works, so you
want to read the comment in ICF.cpp in this patch if you want to know
the details. This algorithm should be more powerful than the ICF
algorithm implemented in GNU gold. It can even merge mutually-recursive
functions (which is harder than one might think).

ICF is a fairly effective size optimization. Here are some examples.

 LLD:   37.14 MB -> 35.80 MB (-3.6%)
 Clang: 59.41 MB -> 57.80 MB (-2.7%)

The lacking feature is "safe" version of ICF. This merges all
identical sections. That is not compatible with a C/C++ language
requirement that two distinct functions must have distinct addresses.

But as long as your program do not rely on the pointer equality
(which is in many cases true), your program should work with the
feature. LLD works fine for example.

GNU gold implements so-called "safe ICF" that identifies functions
that are safe to merge by heuristics -- for example, gold thinks
that constructors are safe to merge because there is no way to
take an address of a constructor in C++. We have a different idea
which David Majnemer suggested that we add NOPs at beginning of
merged functions so that two or more pointers can have distinct
values. We can do whichever we want, but this patch does not
include neither.

http://reviews.llvm.org/D17529

llvm-svn: 261912
2016-02-25 18:43:51 +00:00
Rafael Espindola 148445ef98 Add support for weak symbols in LTO.
llvm-svn: 261881
2016-02-25 16:25:41 +00:00
Rafael Espindola 5e8b54afdb Remove a trivial getter.
llvm-svn: 261590
2016-02-22 23:16:05 +00:00
Rafael Espindola 9f77ef0c08 Add initial LTO support.
llvm-svn: 260726
2016-02-12 20:54:57 +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 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
Rui Ueyama 512c61df1c Define SymbolBody::getSize instead of getSymSize(SymbolBody&). NFC.
llvm-svn: 259613
2016-02-03 00:12:24 +00:00
George Rimar 5c36e5938d [ELF] Implemented -Bsymbolic-functions command line option
-Bsymbolic-functions: 
When creating a shared library, bind references to global 
function symbols to the definition within the shared library, if any.

This patch also fixed behavior of already existent -Bsymbolic:
previously PLT entries were created even if -Bsymbolic was specified.

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

llvm-svn: 259481
2016-02-02 09:28:53 +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 572a6f74a7 Rename DynamicSymbolTableIndex -> DynsymIndex.
This is the index in .dynsym, so the new name should make sense.

llvm-svn: 259142
2016-01-29 01:49:33 +00:00
Rui Ueyama ac2b0a168d Remove dead code.
llvm-svn: 258539
2016-01-22 19:55:01 +00:00
Rafael Espindola 65e80b963a Rename IgnoredWeak to Ignored.
Thanks to Rui for the suggestion.

llvm-svn: 258189
2016-01-19 21:19:52 +00:00