Commit Graph

944 Commits

Author SHA1 Message Date
Rui Ueyama 104e2357de Do not store a computable attributes to Config.
llvm-svn: 295031
2017-02-14 05:45:47 +00:00
Rafael Espindola 08d6a3f133 Create only one section symbol per section.
Unfortunately some consumers of our .o files produced with -r expect
only one section symbol per section. That is true of at least of go's
own linker.

Combining them is a somewhat convoluted process. We have to create a
symbol for every section since we don't know which ones will be
needed. The relocation sections also have to be written first to
handle the Elf_Rel addend.

I did consider a completely different approach:

We could remove the -r special case of relocation sections when
reading. We would instead have a copyRelocs function that is used
instead of scanRelocs. It would create a DynamicReloc for each
relocation and a RelocationSection for each input relocation section.

A complication of such change is that DynamicReloc would have to take
a section index and a input section instead of a symbol since with
-emit-relocs some DynamicReloc would hold relocations referring to the
dynamic symbol table and other to the static symbol table.

That would be a pretty big change, and if we do it it is probably
better to do it as a refactoring.

llvm-svn: 294816
2017-02-11 01:40:49 +00:00
Peter Smith f09245a689 [ELF] Refactor PltSection and IPltSection into PltSection [NFC]
Much of the code in PltSection and IPltSection is similar, we identify
the IPlt by a HeaderSize of 0 and alter our behaviour in the member
functions appropriately:
-Iplt does not have a header
-Iplt always follows after the Plt
    
Differential Revision: https://reviews.llvm.org/D29664

llvm-svn: 294579
2017-02-09 10:56:15 +00:00
George Rimar 82bd8be6d8 Recommit r294464 "[ELF] - Added partial support for --emit-relocs (no --gc-section case, no /DISCARD/ support) #3"
with temporarily file name fix in testcase.

Original commit message:

-q, --emit-relocs - Generate relocations in output

Simplest implementation: 
* no GC case, 
* no "/DISCARD/" linkerscript command support.

This patch is extracted from D28612 / D29636,

Relative to PR31579.

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

llvm-svn: 294469
2017-02-08 16:18:10 +00:00
George Rimar 0b2cc8190d Reverted r294464 "[ELF] - Added partial support for --emit-relocs (no --gc-section case, no /DISCARD/ support) #3"
Broked build bot:
http://lab.llvm.org:8011/builders/llvm-clang-lld-x86_64-scei-ps4-ubuntu-fast/builds/5835/steps/test/logs/stdio

llvm-svn: 294466
2017-02-08 16:10:14 +00:00
George Rimar d6ae624552 [ELF] - Added partial support for --emit-relocs (no --gc-section case, no /DISCARD/ support) #3
-q, --emit-relocs - Generate relocations in output

Simplest implementation: 
* no GC case, 
* no "/DISCARD/" linkerscript command support.

This patch is extracted from D28612 / D29636,

Relative to PR31579.

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

llvm-svn: 294464
2017-02-08 15:53:33 +00:00
Rui Ueyama 7ddd7a8b76 Use a utility function to reduce repetition. NFC.
llvm-svn: 294117
2017-02-05 05:18:58 +00:00
Rafael Espindola 9e9754b520 Replace MergeOutputSection with a synthetic section.
With a synthetic merge section we can have, for example, a single
.rodata section with stings, fixed sized constants and non merge
constants.

I can be simplified further by not setting Entsize, but that is
probably better done is a followup patch.

This should allow some cleanup in the linker script code now that
every output section command maps to just one output section.

llvm-svn: 294005
2017-02-03 13:06:18 +00:00
Rui Ueyama 9255b91a2b Make `-z stack-size` compatible with ld.bfd.
ld.bfd doesn't handle `-z stack-size=0` as a special case.
We shouldn't do that too.

llvm-svn: 293849
2017-02-02 01:12:45 +00:00
Rui Ueyama db00b61860 Simplify createPhdrs. NFC.
Instead of creating multiple PHDRs in a single loop, this patch
runs one for loop for each PHDR type. I think this improves code
readability.

llvm-svn: 293832
2017-02-01 22:42:17 +00:00
Petr Hosek 4d65ef3be1 [ELF] Handle multiple discontiguous .note sections
There could be multiple discontiguous output .note sections in which
case we need to put these into separate PT_NOTE segments rather then
placing them into a single segment. Where possible, we could reorder
the input sections to make sure that all .note are layed out next to
each other to avoid creation multiple PT_NOTE segments, but even in
that case, it's still possible to construct a discontiguous case e.g.
by using a linker script.

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

llvm-svn: 293811
2017-02-01 20:58:41 +00:00
Peter Smith 3a52eb0054 [ELF] Use SyntheticSections for Thunks
Thunks are now implemented by redirecting the relocation to the
symbol S, to a symbol TS in a Thunk. The Thunk will transfer control
to S. This has the following implications:
- All the side-effects of Thunks happen within createThunks()
- Thunks are no longer stored in InputSections and Symbols no longer
  need to hold a pointer to a Thunk
- The synthetic Thunk sections need to be merged into OutputSections
    
This implementation is almost a direct conversion of the existing
Thunks with the following exceptions:
- Mips LA25 Thunks are placed before the InputSection that defines
  the symbol that needs a Thunk.
- All ARM Thunks are placed at the end of the OutputSection of the
  first caller to the Thunk.
    
Range extension Thunks are not supported yet so it is optimistically
assumed that all Thunks can be reused.

This is a recommit of r293283 with a fixed comparison predicate as
std::merge requires a strict weak ordering.

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

llvm-svn: 293757
2017-02-01 10:26:03 +00:00
Peter Collingbourne 4a72c38702 ELF: Align RELRO to the target page size rather than the max page size.
If no bss sections appear after the relro segment, the loader will round
the r/w segment size to the target's page size. Align the relro size in the
same way to ensure that it does not extend past the end of the program's
own memory region.

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

llvm-svn: 293519
2017-01-30 18:20:07 +00:00
Rafael Espindola 64e5556e9f Fix -Werror build.
llvm-svn: 293390
2017-01-28 19:07:33 +00:00
Rafael Espindola 78493a259d Fix a few symbols that are not actually ABS.
The symbols _end, end, _etext, etext, _edata, edata and __ehdr_start
refer to positions in the file and are therefore not absolute. Making
them absolute was on unfortunate cargo cult of what bfd was doing.

Changing the symbols allows for pc relocations to them to be resolved,
which should fix the wine build.

llvm-svn: 293385
2017-01-28 17:48:21 +00:00
Rui Ueyama f20ee9f11a Revert "[ELF][ARM] Use SyntheticSections for Thunks"
This reverts commit r293283 because it broke MSVC build.

llvm-svn: 293352
2017-01-28 00:48:06 +00:00
Rafael Espindola fe12450e8e Revert commits r293276 and r293278.
[ELF] Fixed formatting. NFC

and

    [ELF] Bypass section type check

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

They do the opposite of what was asked for in the code review.

llvm-svn: 293320
2017-01-27 18:39:30 +00:00
Peter Smith 5191c6f945 [ELF][ARM] Use SyntheticSections for Thunks
Thunks are now implemented by redirecting the relocation to the
symbol S, to a symbol TS in a Thunk. The Thunk will transfer control
to S. This has the following implications:
- All the side-effects of Thunks happen within createThunks()
- Thunks are no longer stored in InputSections and Symbols no longer
  need to hold a pointer to a Thunk
- The synthetic Thunk sections need to be merged into OutputSections
    
This implementation is almost a direct conversion of the existing
Thunks with the following exceptions:
- Mips LA25 Thunks are placed before the InputSection that defines
  the symbol that needs a Thunk.
- All ARM Thunks are placed at the end of the OutputSection of the
  first caller to the Thunk.
    
Range extension Thunks are not supported yet so it is optimistically
assumed that all Thunks can be reused.

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

llvm-svn: 293283
2017-01-27 13:10:16 +00:00
Eugene Leviant 8b7cadcf96 [ELF] Bypass section type check
Differential revision: https://reviews.llvm.org/D28761

llvm-svn: 293276
2017-01-27 11:01:43 +00:00
Rafael Espindola c0fc253071 Change the --retain-symbols-file implementation.
It now uses the same infrastructure as symbol versions. This fixes us
creating a dynamic relocation without a corresponding dynamic symbol.

This also means that unlike gold and bfd we keep a STB_LOCAL in the
static symbol table. It seems an odd feature to offer precise control
over what is in a symbol table that is not used by the dynamic
linker. We can bring this back if needed, but it would probably be
better to just have --discard option that tells the linker to keep in
the static symbol table only what is in the dynamic one.

Should fix the eog build.

llvm-svn: 293093
2017-01-25 21:23:06 +00:00
Peter Smith 9694376a93 [ELF] Add local mapping symbols to ARM PLT entries
Mapping symbols allow a mapping symbol aware disassembler to
correctly disassemble the PLT when the code immediately prior to the
PLT is Thumb.

To implement this we add a function to add symbols with local
binding to be defined in SyntheticSymbols.

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

llvm-svn: 293044
2017-01-25 10:31:16 +00:00
George Rimar 190bac5d51 [ELF] - Stop handling local symbols in a special way.
Previously we stored kept locals in a KeptLocalSyms arrays,
belonged to files.

Patch makes SymbolTableSection to store locals in Symbols member,
that already present and was used for globals.
SymbolTableSection already had NumLocals counter member, so change
itself is trivial.

That allows to simplify handling of -r,
Body::DynsymIndex is no more used as "symbol table index" for relocatable
output.

Change was suggested during review of D28773 and opens road for D28612.

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

llvm-svn: 292789
2017-01-23 14:07:23 +00:00
Rafael Espindola 0347c0b874 Don't create a bogus PT_PHDR if we don't allocate the headers.
llvm-svn: 292644
2017-01-20 20:46:15 +00:00
Rafael Espindola 8c495e20bd Reduce code duplication when allocating program headers.
This will simplify a bug fix.

llvm-svn: 292642
2017-01-20 20:41:18 +00:00
Rafael Espindola 338def1506 Simplify. NFC.
addIgnored already creates the symbol only if there is a reference to
it.

llvm-svn: 292628
2017-01-20 18:20:37 +00:00
Rafael Espindola 2c87688c70 Also define 'end' if it is present in a .so.
I don't know of anything using it, but we should handle it like _end.

llvm-svn: 292513
2017-01-19 19:51:02 +00:00
Peter Smith ee6d7186c3 [ELF] Move createThunks() after scanRelocations()
A necessary first step towards range extension thunks is to delay
the creation of thunks until the layout of InputSections within
OutputSections has been done.
    
The change scans the relocations directly from InputSections rather
than looking in the ELF File the InputSection came from. This will
allow a future change to redirect the relocations to symbols defined
by Thunks rather than indirect when resolving relocations.
    
A side-effect of moving ThunkCreation is that the OutSecOff of
InputSections may change in an OutputSection that contains Thunks.
In well behaved programs thunks are not in OutputSections with
dynamic relocations.
    
Differential Revision: https://reviews.llvm.org/D28811

llvm-svn: 292359
2017-01-18 09:57:14 +00:00
Rui Ueyama 40eaa9968d Return early if writeMapFile failed.
This patch adds a test for an invalid output path for -Map option,
though that test is not for verifying that we are using error()
instead of fatal() in writeMapFile.

llvm-svn: 292336
2017-01-18 03:34:38 +00:00
Rafael Espindola 1d6d1b44cc Add a isInCurrentDSO helper. NFC.
llvm-svn: 292228
2017-01-17 16:08:06 +00:00
George Rimar 2ddab6d186 [ELF] - Do not create huge garbage files on section offset overflow.
Patch changes behavior to not try open the output 
file if we already know about error.

That is not just cleaner, but also fixes nasty behavior of linker that
could create huge temporarily files under certain conditions.

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

llvm-svn: 292219
2017-01-17 13:50:34 +00:00
Simon Atanasyan 2bd98af563 [ELF][MIPS] Exclude mips .got from PT_GNU_RELRO segment
On MIPS .got section cannot be included into the PT_GNU_RELRO segment.
Sometimes it might work, but in general it is unsupported. One of the
problem is that all sections marked by SHF_MIPS_GPREL should be grouped
together because data in these sections is addressable with a gp
relative address, but such sections might be writable.

This patch exclude mips .got from PT_GNU_RELRO segment and group
SHF_MIPS_GPREL sections.

llvm-svn: 292161
2017-01-16 21:17:23 +00:00
Rafael Espindola 1ebfc59c89 Implement -Map.
The format is not exactly the same as the one in bfd since bfd always
follows a linker script and prints it along.

llvm-svn: 291958
2017-01-13 21:05:46 +00:00
George Rimar 1e799942b3 [ELF] - Move the addition of synthetics from addPredefinedSections()
These were 3 last synthetics that were added in addPredefinedSections() instead
of createSyntheticSections(). Now it is possible to move addition to correct common place.

Also patch fixes testcase which discards .shstrtab, by restricting doing that.

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

llvm-svn: 291908
2017-01-13 16:18:15 +00:00
Rui Ueyama c8d3a83fff Remove error(error_code, const Twine &).
Now we have the consistent interface for all log/warn/error/fatal functions.

llvm-svn: 291847
2017-01-12 22:18:04 +00:00
Peter Collingbourne feb6629d6d ELF: Reserve space for copy relocations of read-only symbols in relro.
When reserving copy relocation space for a shared symbol, scan the DSO's
program headers to see if the symbol is in a read-only segment. If so,
reserve space for that symbol in a new synthetic section named .bss.rel.ro
which will be covered by the relro program header.

This fixes the security issue disclosed on the binutils mailing list at:
https://sourceware.org/ml/libc-alpha/2016-12/msg00914.html

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

llvm-svn: 291524
2017-01-10 01:21:50 +00:00
Peter Collingbourne 628ec9f193 ELF: Place relro sections after non-relro sections in r/w segment.
This is in preparation for my next change, which will introduce a relro
nobits section. That requires that relro sections appear at the end of the
progbits part of the r/w segment so that the relro nobits section can appear
contiguously.

Because of the amount of churn required in the test suite, I'm making this
change separately.

llvm-svn: 291523
2017-01-10 01:21:30 +00:00
Rui Ueyama 817c9d66f0 Support non-regular output files.
This patch enables something like "-o /dev/null".
Previouly, it failed because such files cannot be renamed.

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

llvm-svn: 291496
2017-01-09 23:07:05 +00:00
Peter Collingbourne 7b5088b3b2 ELF: Round p_memsz of the PT_GNU_RELRO program header up to the page size.
The glibc dynamic loader rounds the size down, so without this the loader
will fail to change the memory protection for the last page.

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

llvm-svn: 290986
2017-01-04 18:56:15 +00:00
Peter Collingbourne 578af61b09 ELF: Simplify; addOptionalSynthetic() does not need to return a value. NFC.
llvm-svn: 290932
2017-01-04 02:43:01 +00:00
George Rimar 31a46b4835 [ELF] - Fix mistype in comment. NFC.
llvm-svn: 290510
2016-12-25 06:49:17 +00:00
Rui Ueyama 4f2f50dc64 De-template DefinedSynthetic.
DefinedSynthetic is not created for a real ELF object, so it doesn't
have to be a template function. It has a virtual st_value, which is
either 32 bit or 64 bit, but we can simply use 64 bit.

llvm-svn: 290241
2016-12-21 08:40:09 +00:00
Rui Ueyama 6e3595d6c5 Move a function defintion to make it static.
llvm-svn: 290215
2016-12-21 00:05:39 +00:00
George Rimar d003c7f394 [ELF] - Treat .openbsd.randomdata as relro section
That was requested by Mark Kettenis in llvm-dev:

"It is the intention that .openbsd.randomdata sections are made
read-only after initialization. The native (ld.bfd based) OpenBSD
toolchain accomplishes this by including .openbsd.randomdata into the
PT_GNU_RELRO segment."

He suggested code change, I added testcase.

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

llvm-svn: 290174
2016-12-20 11:28:54 +00:00
Rui Ueyama 312703116f Simplify type of Config->SymbolOrderingFile.
That variable was of type DenseMap<StringRef, unsigned>, but the
unsigned numbers needed to be monotonicly increasing numbers because
the implementation that used the variable depended on that fact.
That was an implementation detail and shouldn't have leaked into Config.

This patch simplifies its type to std::vector<StringRef>.

llvm-svn: 290151
2016-12-20 01:51:08 +00:00
Rafael Espindola 5967c97323 Fix corner cases of setting the section address.
This handles all the corner cases if setting a section address:

- If the address is too low, we cannot allocate the program headers.
- If the load address is lowered, we have to do that before finalize

This also shares some code with the linker script since it was already
hitting similar cases.

This is used by the freebsd boot loader. It is not clear if we need to
support this with a non binary output, but it is not as bad as I was
expecting.

llvm-svn: 290136
2016-12-19 21:21:07 +00:00
George Rimar 2bb88ab5e0 [ELF] - Implemented --retain-symbols-file option
--retain-symbols-file=filename
Retain only the symbols listed in the file filename, discarding all others. 
filename is simply a flat file, with one symbol name per line. This option 
is especially useful in environments (such as VxWorks) where a large global 
symbol table is accumulated gradually, to conserve run-time memory.

Note: though documentation says "--retain-symbols-file does not discard 
undefined symbols, or symbols needed for relocations.", both bfd and gold 
do that, and this patch too, like testcase show.

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

llvm-svn: 290122
2016-12-19 18:00:52 +00:00
Rafael Espindola 17cb7c0a2a Detemplate PhdrEntry. NFC.
llvm-svn: 290115
2016-12-19 17:01:01 +00:00
Rui Ueyama 8f687f71fb Remove inappropriate use of CachedHashStringRef.
Use of CachedHashStringRef makes sense only when we reuse hash values.
Sprinkling it to all DenseMap has no benefits and just complicates data types.
Basically we shouldn't use CachedHashStringRef unless there is a strong
reason to to do so.

llvm-svn: 290076
2016-12-19 03:14:16 +00:00
Rui Ueyama 9381eb1045 Remove lld/Support/Memory.h.
I thought for a while about how to remove it, but it looks like we
can just copy the file for now. Of course I'm not happy about that,
but it's just less than 50 lines of code, and we already have
duplicate code in Error.h and some other places. I want to solve
them all at once later.

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

llvm-svn: 290062
2016-12-18 14:06:06 +00:00
Simon Atanasyan 2903a8a11f [ELF][MIPS] Calculate default _gp value relative to the GPREL section with the lowest address
llvm-svn: 289471
2016-12-12 21:34:11 +00:00