Commit Graph

870 Commits

Author SHA1 Message Date
Simon Atanasyan 8469b8841c [ELF][MIPS] Fix handling of _gp/_gp_disp/__gnu_local_gp symbols
Offset between beginning of a .got section and _gp symbols used in MIPS
GOT relocations calculations. Usually the expression looks like
VA + Offset - GP, where VA is the .got section address, Offset - offset
of the GOT entry, GP - offset between .got and _gp. Also there two "magic"
symbols _gp_disp and __gnu_local_gp which hold the offset mentioned above.
These symbols might be referenced by MIPS relocations.

Now the linker always defines _gp symbol and uses hardcoded value for
its initialization. So offset between .got and _gp is 0x7ff0. The _gp_disp
and __gnu_local_gp defined if required and initialized by 0x7ff0.
In fact that is not correct because _gp symbol might be defined by a linker
script and holds arbitrary value. In that case we need to use this value
in relocation calculation and initialize _gp_disp and __gnu_local_gp
properly.

The patch fixes the problem and completes fixing the bug #30311.
https://llvm.org/bugs/show_bug.cgi?id=30311

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

llvm-svn: 287832
2016-11-23 22:22:16 +00:00
Rui Ueyama 3fc0f7e54f Define toString() as a generic function to get a string for error message.
We have different functions to stringize objects to construct
error messages. For InputFile, we have getFilename, and for
InputSection, we have getName. You had to memorize them.

I think this is the case where the function overloading comes in handy.

This patch defines toString() functions that are overloaded for all these
types, so that you just call it in error().

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

llvm-svn: 287787
2016-11-23 18:07:33 +00:00
Rui Ueyama 0cbf749397 Remove one of SymbolTable::addRegular function that forwards other addRegular.
So that we have less number of overloaded functions.

llvm-svn: 287745
2016-11-23 06:59:47 +00:00
Rui Ueyama c72ba3a4d7 Allow calling getName() on local symbols.
Previously, we stored offsets in string tables to symbols, so
you needed to pass a string table to get a symbol name. This patch
stores const char pointers instead to eliminate the need to pass
a string table.

llvm-svn: 287737
2016-11-23 04:57:25 +00:00
Rafael Espindola 3c97dc7055 move VerDef finalization before DynStrTab
llvm-svn: 287701
2016-11-22 21:12:20 +00:00
Rui Ueyama bdfa155fa2 Fix build breakage.
We cannot have MipsRldMap class and In<ELFT>::MipsRldMap.
Renamed the class.

llvm-svn: 287683
2016-11-22 19:24:52 +00:00
Eugene Leviant 17b7a57533 [ELF] Convert .rld_map to input section
Differential revision: https://reviews.llvm.org/D26958

llvm-svn: 287675
2016-11-22 17:49:14 +00:00
Rui Ueyama 1d75de00e8 Do not save unused pointers to In<ELFT>.
llvm-svn: 287617
2016-11-22 04:28:39 +00:00
Rui Ueyama 9cfac8a849 Convert MipsOptionsSection to SyntheticSection.
llvm-svn: 287615
2016-11-22 04:13:09 +00:00
Rui Ueyama b71cae90de Convert MipsReginfoSection to SyntheticSection.
llvm-svn: 287614
2016-11-22 03:57:08 +00:00
Rui Ueyama 12f2da870e Convert MipsAbiFlagsSection to SyntheticSection.
llvm-svn: 287613
2016-11-22 03:57:06 +00:00
Rui Ueyama c4030a1937 Merge BuildId subclasses.
We had five different BuildId subclasses for five different types
of build-ids. They can simply be merged to a single class.

llvm-svn: 287603
2016-11-22 00:54:15 +00:00
Rafael Espindola 1c57007ec8 Fix address computation for headers.
If the linker script has SECTIONS, the address computation is now
always done in LinkerScript::assignAddresses, like for any other
section.

Before fixHeaders would do a tentative computation that
assignAddresses would sometimes override.

This patch also splits the cases where assignAddresses needs to add
the headers to the first PT_LOAD and the address computation. The net
effect is that we no longer create an empty page for no reason in the
included test case, which matches bfd behavior.

llvm-svn: 287565
2016-11-21 19:59:33 +00:00
Eugene Leviant e9bab5d857 [ELF] Convert Version*** sections to input sections
Differential revision: https://reviews.llvm.org/D26918

llvm-svn: 287554
2016-11-21 16:59:33 +00:00
Eugene Leviant 952eb4d348 [ELF] Convert EhFrameHeader to input section
Differential revision: https://reviews.llvm.org/D26906

llvm-svn: 287549
2016-11-21 15:52:10 +00:00
Eugene Leviant a113a4194c [ELF] Convert GdbIndexSection to input section
Differential revision: https://reviews.llvm.org/D26854

llvm-svn: 287526
2016-11-21 09:24:43 +00:00
Rui Ueyama 0b1b695a9e Add comments.
This patch rearranges code a bit to make it easy to explain.

llvm-svn: 287515
2016-11-21 02:11:05 +00:00
Rafael Espindola 4d2d9c07a5 Simplify handling of SHF_LINK_ORDER.
It seems a lot simpler to just sort the sections and let the
relocations do the rest.

llvm-svn: 287365
2016-11-18 19:02:15 +00:00
Eugene Leviant ff23d3e741 [ELF] Convert PltSection to input section
Differential revision: https://reviews.llvm.org/D26842

llvm-svn: 287346
2016-11-18 14:35:03 +00:00
Eugene Leviant b96e809c7f [ELF] Convert HashTableSection to input section
Differential revision: https://reviews.llvm.org/D26834

llvm-svn: 287326
2016-11-18 09:06:47 +00:00
Eugene Leviant be809a7125 [ELF] Convert GnuHashTableSection to input section
Differential revision: https://reviews.llvm.org/D26792

llvm-svn: 287322
2016-11-18 06:44:18 +00:00
Rui Ueyama 77f2a87575 Simplify MergeOutputSection.
MergeOutputSection class was a bit hard to use because it provdes
a series of finalize functions that have to be called in a right way
at a right time. It also intereacted with MergeInputSection, and the
logic was somewhat entangled between the two classes.

This patch simplifies it by providing only one finalize function.
Now, all you have to do is to call MergeOutputSection::finalize
when you have added all sections to the output section. Then, it
internally merges strings and initliazes StringPiece objects.
I think this is much easier to understand.

This patch also adds comments.

llvm-svn: 287314
2016-11-18 05:05:43 +00:00
Rafael Espindola dab02d4b68 Allow use define symbols to override linker defined ones.
I hit an internal linker script that was defining _DYNAMIC instead of
letting the linker do it. It turns out that both bfd and gold allow
that.

This is pretty easy to implement, just make the linker defined symbol
weak. This should have no impact in the case where there is no user
defined symbol: The visibility is hidden, which causes the output to
still be local.

llvm-svn: 287260
2016-11-17 21:20:16 +00:00
Rafael Espindola 74fa2822f6 Simplify. NFC.
llvm-svn: 287231
2016-11-17 15:29:11 +00:00
Eugene Leviant 9230db94b7 [ELF] Convert SymbolTableSection to input section
Differential revision: https://reviews.llvm.org/D26740

llvm-svn: 287216
2016-11-17 09:16:34 +00:00
Simon Atanasyan 725dc14bb2 [ELF][MIPS] Add MipsGotSection to handle MIPS GOT
MIPS GOT handling is very different from other targets so it is better
to keep the code in the separatre section class MipsGotSection. This
patch introduces the new section and moves all MIPS specific code from
GotSection to the new class. I did not rename fields and methods in the
MipsGotSection class to reduce the diff and plan to do that by the
separate commit.

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

llvm-svn: 287150
2016-11-16 21:01:02 +00:00
Eugene Leviant a96d9027a3 [ELF] Convert RelocationSection to input section
Differential revision: https://reviews.llvm.org/D26669

llvm-svn: 287092
2016-11-16 10:02:27 +00:00
Eugene Leviant 65262bdc0c [ELF] Don't check Out<ELFT>::RelaPlt as it's always non-zero. NFC
llvm-svn: 286955
2016-11-15 13:19:21 +00:00
Eugene Leviant 6380ce2212 [ELF] Convert DynamicSection to input section.
This patch introduces the following changes:
- DynamicSection now inherits InputSection<ELFT> and was moved
  to SyntheticSections.h/.cpp.
- Link and Entsize fields of DynamicSection are propagated to 
  its output section
- In<ELFT>::SyntheticSections was removed.
- Finalization of synthetic sections was removed from 
  OutputSection<ELFT>::finalize. Now finalizeSyntheticSections is
  used instead.

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

llvm-svn: 286950
2016-11-15 12:26:55 +00:00
George Rimar 7beff42f8f [ELF] - Do not create reserved symbols in case of relocatable output.
This patch stops creating symbols like __ehdr_start, 
_end/_etext_edata,__tls_get_addr when using -r.

This fixes PR30984.

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

llvm-svn: 286941
2016-11-15 08:19:02 +00:00
George Rimar 463984d4bf [ELF] - Better diagnostic for relative relocation to an absolute value error.
Patch adds a filename to that error message.

I faced next error when debugged one of FreeBSD port:
error: relocation R_X86_64_PLT32 cannot refer to absolute symbol __tls_get_addr

error message was poor and this patch improves it to show the locations 
of symbol declaration and using.

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

llvm-svn: 286940
2016-11-15 08:07:14 +00:00
Rafael Espindola f7a1744803 Fix program header propagation.
Propagate program headers by walking the commands, not the
sections. This allows us to propagate program headers even from
sections that don't end up in the output.

Fixes pr30997.

llvm-svn: 286837
2016-11-14 15:39:38 +00:00
George Rimar d8b27769c8 [ELF] - format. NFC.
llvm-svn: 286805
2016-11-14 10:14:18 +00:00
Eugene Leviant 22eb026886 [ELF] Convert StringTableSection to input section
Differential revision: https://reviews.llvm.org/D26549

llvm-svn: 286799
2016-11-14 09:16:00 +00:00
Rafael Espindola 1960bcd710 Don't sort sections in -r links.
Unlike gold, bfd, gas or MC we were putting exidx sections first since
they are ro.

The spec doesn't explicitly say that they must come after, but it is
definitely more convenient for the consumer, matches other producers
and matches other areas in ELF (like SHT_GROUP) where sections are
ordered in a natural way.

llvm-svn: 286659
2016-11-11 22:43:27 +00:00
Rafael Espindola d4372f783b Avoid a crash with -r and .comment.
We would create a MergeInputSection for the synthetic .comment and
crash trying to add it to a regular output section.

With this we just don't add the synthetic section with -r. That is
consistent with gold that doesn't create .note.gnu.gold-version with
-r.

llvm-svn: 286635
2016-11-11 21:36:25 +00:00
Eugene Leviant ad4439e802 [ELF] Convert .got section to input section
Differential revision: https://reviews.llvm.org/D26498

llvm-svn: 286580
2016-11-11 11:33:32 +00:00
Rui Ueyama 3da3f06dd3 Include version string into ".comment" section.
Summary:
This patch adds a ".comment" section to an output. The comment
section contains the linker's version string. You can now
find out whether a binary is created by LLD or not using objdump
command like this.

  $ objdump -s -j .comment foo

  foo:     file format elf64-x86-64

  Contents of section .comment:
   0000 00474343 3a202855 62756e74 7520342e  .GCC: (Ubuntu 4.
   0010 382e342d 32756275 6e747531 7e31342e  8.4-2ubuntu1~14.
   ...
   00c0 766d2f74 72756e6b 20323835 38343629  vm/trunk 285846)
   00d0 004c696e 6b65723a 204c4c44 20342e30  .Linker: LLD 4.0
   00e0 2e302028 7472756e 6b203238 36343036  .0 (trunk 286406
   00f0 2900                                 ).

Compilers emits .comment section as well, so the output contains
both compiler and linker information.

Alternative considered:

I first tried to add a SHT_NOTE section because GNU gold does that.
A NOTE section starts with a header which contains content type.
It turned out that ld.gold sets type NT_GNU_GOLD_VERSION to their
NOTE section. So the NOTE type is only for GNU gold (surprise!)

Next, I tried to create ".linker-version" section. However, it seems
that reusing the existing ".comment" section is better because 1)
other tools already know about .comment section and is able to strip
it and 2) the result contans not only linker info but also compiler
info.

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

llvm-svn: 286496
2016-11-10 20:20:37 +00:00
Rafael Espindola 9f0c4bb795 Parse relocations only once.
Relocations are the last thing that we wore storing a raw section
pointer to and parsing on demand.

With this patch we parse it only once and store a pointer to the
actual data.

The patch also changes where we store it. It is now in
InputSectionBase. Not all sections have relocations, but most do and
this simplifies the logic. It also means that we now only support one
relocation section per section. Given that that constraint is
maintained even with -r with gold bfd and lld, I think it is OK.

llvm-svn: 286459
2016-11-10 14:53:24 +00:00
Eugene Leviant 41ca327b5e [ELF] Convert .got.plt section to input section
Differential revision: https://reviews.llvm.org/D26349

llvm-svn: 286443
2016-11-10 09:48:29 +00:00
George Rimar 1a33c0f242 [ELF] - Implemented --symbol-ordering-file option.
Patch allows to pass a symbols file to linker.
LLD will map symbols to sections and sort sections
in output according to symbol ordering file.

That can help to reduce the startup time and/or
amount of pagefaults during startup.

Also, interesting benchmark result was produced by Rafael Espíndola. 
After applying the symbols file for clang he timed compiling 
X86MCTargetDesc.ii to an object file.  

The page faults went from just
56,988 to 56,946 since most faults are not in the binary.
Running time went from 4.403053515 to 4.178112244. 
The speedup seems to be because of better cache
locality.

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

llvm-svn: 286440
2016-11-10 09:05:20 +00:00
Rafael Espindola e08e78df6d Make OutputSectionBase a class instead of class template.
The disadvantage is that we use uint64_t instad of uint32_t for some
value in 32 bit files. The advantage is a substantially simpler code,
faster builds and less code duplication.

llvm-svn: 286414
2016-11-09 23:23:45 +00:00
Simon Atanasyan fa03b0fafa [ELF][MIPS] Convert .MIPS.abiflags section to synthetic input section
Previously, we have both input and output section for .MIPS.abiflags.
Now we have only one class for .MIPS.abiflags, which is MipsAbiFlagsSection.
This class is a synthetic input section.

.MIPS.abiflags sections are handled as regular sections until
the control reaches Writer. Writer then aggregates all sections
whose type is SHT_MIPS_ABIFLAGS to create a single synthesized
input section. The synthesized section is then processed normally
as if it came from an input file.

llvm-svn: 286398
2016-11-09 21:37:06 +00:00
Simon Atanasyan ce02cf0099 [ELF][MIPS] Convert .reginfo and .MIPS.options sections to synthetic input sections
Previously, we have both input and output sections for .reginfo and
.MIPS.options. Now for each such sections we have one synthetic input
sections: MipsReginfoSection and MipsOptionsSection respectively.

Both sections are handled as regular sections until the control reaches
Writer. Writer then aggregates all sections whose type is SHT_MIPS_REGINFO
or SHT_MIPS_OPTIONS to create a single synthesized input section. In that
moment Writer also save GP0 value to the MipsGp0 field of the corresponding
ObjectFile. This value required for R_MIPS_GPREL16 and R_MIPS_GPREL32
relocations calculation.

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

llvm-svn: 286397
2016-11-09 21:36:56 +00:00
Rafael Espindola 04a2e348bb Split Header into individual fields.
This is similar to what was done for InputSection.

With this the various fields are stored in host order and only
converted to target order when writing.

llvm-svn: 286327
2016-11-09 01:42:41 +00:00
Rafael Espindola 8f9026baff Don't add null and discarded sections to the global list.
Avoids having to skip them multiple times.

llvm-svn: 286261
2016-11-08 18:23:02 +00:00
Rafael Espindola c0e47fbfb2 Delete the InterpSection class.
We can just use a regular InputSection.

llvm-svn: 286237
2016-11-08 14:56:27 +00:00
Rafael Espindola 682a5bc2c1 Delete the CommonSection class.
With the current infrastructure it can be just an ordinary
InputSection like the real .bss sections.

llvm-svn: 286234
2016-11-08 14:42:34 +00:00
Eugene Leviant bae1c656bb [ELF] Heuristic for placing orphan section
Differential revision: https://reviews.llvm.org/D25325

llvm-svn: 286225
2016-11-08 10:44:48 +00:00
Rui Ueyama e8a6102fa9 Rewrite CommonInputSection as a synthetic input section.
A CommonInputSection is a section containing all common symbols.
That was an input section but was abstracted in a different way
than the synthetic input sections because it was written before
the synthetic input section was invented.

This patch rewrites CommonInputSection as a synthetic input section
so that it behaves better with other sections.

llvm-svn: 286053
2016-11-05 23:05:47 +00:00