Commit Graph

1292 Commits

Author SHA1 Message Date
Rafael Espindola 056190c348 Don't make _gp absolute.
_gp points to a position in the file, so it is not really absolute. It
is also simpler to not force it to be absolute, so if there is no
strong ABI requirement we should not do it.

llvm-svn: 313333
2017-09-15 01:49:01 +00:00
George Rimar cb888a620d [ELF] - Refactor of Writer<ELFT>::forEachRelSec.
There is no need to scan over all input sections for relocatable output.
As we do not process or scan relocations anyways.
Patch moves check for Config->Relocatable out to avoid that and also removes
excessive check for isa<EhInputSection> from first for loop. 
It is excessive because we handle all of them in a second for loop below.
That all allowed to simplify code.

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

llvm-svn: 313127
2017-09-13 07:54:47 +00:00
Rafael Espindola 30ea5cf543 Update comment. NFC.
llvm-svn: 313051
2017-09-12 17:18:03 +00:00
Rafael Espindola 4d5601608d Simplify handling of predefined symbols.
Given our representation of symbols as section + offset, we can
finalize symbols like _end as soon as the section is known.

llvm-svn: 313048
2017-09-12 16:38:01 +00:00
Rui Ueyama 9011320921 Remove a redundant local variable.
llvm-svn: 312823
2017-09-08 19:41:35 +00:00
Rafael Espindola 8016bdfd93 Handle empty dynamic lists.
llvm-svn: 312820
2017-09-08 18:53:43 +00:00
Rafael Espindola ee6a352a8d Handle lazy symbols is computeIsPreemptible.
llvm-svn: 312812
2017-09-08 18:41:49 +00:00
Rafael Espindola 716c57bfb5 Simplify. NFC.
We handled all undefined symbols before this.

llvm-svn: 312808
2017-09-08 18:23:25 +00:00
Rafael Espindola bb468da200 Simplify as this runs before copy relocations are created.
llvm-svn: 312807
2017-09-08 18:21:14 +00:00
Rafael Espindola d72d97b3be If --dynamic-list is given, only those symbols are preemptible.
This allows combining --dynamic-list and version scripts too. The
version script controls which symbols are visible, and
--dynamic-list controls which of those are preemptible.

Unlike previous versions, undefined symbols are still considered
preemptible, which was the issue breaking the cfi tests.

This fixes pr34053.

llvm-svn: 312806
2017-09-08 18:16:59 +00:00
Dmitry Mikulin 1e30f07ce7 Currently lld creates a single section to collect all commons. There is no way
to separate commons based on file name patterns. The following linker script
construct does not work because commons are allocated before section placement
is done and the only synthesized BssSection that holds all commons has no file
associated with it:
SECTIONS { .common_0 : { *file0.o(COMMON) }}

This patch changes the allocation of commons to create a section per common
symbol and let the section logic do the layout.

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

llvm-svn: 312796
2017-09-08 16:22:43 +00:00
George Rimar 9a2da39837 [ELF] - Simplify #2. NFC.
llvm-svn: 312789
2017-09-08 13:40:07 +00:00
George Rimar 7ac3825afb [ELF] - Simplify. NFC.
llvm-svn: 312787
2017-09-08 13:26:45 +00:00
Rafael Espindola 0ff545c018 Revert "Revert "Revert r311468: If --dynamic-list is given, only those symbols are preemptible""
This reverts commit r312757.

Evgenii Stepanov reports that it broke some tests.

llvm-svn: 312771
2017-09-08 01:09:52 +00:00
Rafael Espindola b7d0be5896 Revert "Revert r311468: If --dynamic-list is given, only those symbols are preemptible"
If --dynamic-list is given, only those symbols are preemptible.

This allows combining --dynamic-list and version scripts too. The
version script controls which symbols are visible, and --dynamic-list
controls which of those are preemptible.

This fixes pr34053.

llvm-svn: 312757
2017-09-07 23:19:09 +00:00
George Rimar 6823c5f0c0 [ELF] - Rename PhdrEntry::First/Last to FirstSec/LastSec. NFC.
As was suggested in D34956 thread.

llvm-svn: 312712
2017-09-07 11:01:10 +00:00
George Rimar 582ede8922 [ELF] - Store pointer to PT_LOAD instead of pointer to first section in OutputSection
It is a bit more convinent and helps to simplify logic 
of program headers allocation a little.

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

llvm-svn: 312711
2017-09-07 10:53:07 +00:00
Davide Italiano a0186dd986 [ELF/Writer] Fix english in a comment. NFCI.
llvm-svn: 312669
2017-09-06 21:16:51 +00:00
Peter Smith 3ef89b0fc7 [ELF] Move fixSectionAlignments() before first call to assignAddresses()
The fixSectionAlignments() function may alter the alignment of some
OutputSections, this is likely to alter the addresses calculated earlier
in assignAddresses(). By moving the call to fixSectionAlignments() we
make sure that assignAddresses() is consistent with the early calculation
used for RangeThunks and the final call just before writing the image.

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

llvm-svn: 312636
2017-09-06 14:02:14 +00:00
Rui Ueyama 888da8c232 Do not use invalid iterators to fix Windows build.
std::vector::insert invalidates all iterators, so it was not safe to do

  Script->Opt.Commands.insert(++I, Make(ElfSym::End1));
  Script->Opt.Commands.insert(++I, Make(ElfSym::End2));

because after the first line, `I` is no longer valid.

This patch rewrites fixes the issue. I belive the new code without
higher-order functions is a bit more readable than before.

llvm-svn: 312570
2017-09-05 20:17:37 +00:00
Petr Hosek 7ab9f7be0c [ELF] Set p_memsz to p_filesz when aligning the last segment to page boundary
Having p_filesz different from p_memsz is confusing some tools.

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

llvm-svn: 312384
2017-09-01 21:48:20 +00:00
Petr Hosek 18821b60b0 [ELF] Generate symbol assignments for predefined symbols
The problem with symbol assignments in implicit linker scripts is that
they can refer synthetic symbols such as _end, _etext or _edata. The
value of these symbols is currently fixed only after all linker script
commands are processed, so these assignments will be using non-final and
hence invalid value.

Rather than fixing the symbol values after all command processing have
finished, we instead change the logic to generate symbol assignment
commands that set the value of these symbols while processing the
commands, this ensures that the value is going to be correct by the time
any reference to these symbol is processed and is equivalent to defining
these symbols explicitly in linker script as BFD ld does.

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

llvm-svn: 312305
2017-09-01 02:23:31 +00:00
George Rimar 60608a8ae5 [ELF] - Simplify (use llvm::erase_if). NFC.
llvm-svn: 311878
2017-08-28 09:28:15 +00:00
Sam Clegg 7dbd1fd73b Update comments: parallel_for_each -> parallelForEach
Also remove unused include of raw_ostream.h

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

llvm-svn: 311587
2017-08-23 19:03:20 +00:00
Rui Ueyama e158f7c329 Revert r311468: If --dynamic-list is given, only those symbols are preemptible
This reverts commit r311468 because it broke some CFI bots.

llvm-svn: 311497
2017-08-22 21:54:58 +00:00
Rui Ueyama 9cbbacb910 If --dynamic-list is given, only those symbols are preemptible
Patch by Rafael Espíndola.

This is PR34053.

The implementation is a bit of a hack, given the precise location where
IsPreemtible is set, it cannot be used from
SymbolTable::handleAnonymousVersion.

I could add another method to SymbolTable if you think that would be
better.

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

llvm-svn: 311468
2017-08-22 16:31:47 +00:00
George Rimar f7ef2a13f6 [ELF] - Recommit "[ELF] - Do not forget to fill last bytes of PT_LOADs with trap instructions."
With fix: explicitly specify ouput format for hexdump tool call.

Original commit message:

[ELF] - Do not forget to fill last bytes of PT_LOADs with trap instructions.

Previously last 4 bytes of executable loads
were not filled with trap instructions,
patch fixes this bug.

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

llvm-svn: 311315
2017-08-21 08:31:14 +00:00
George Rimar 09a6945b48 [ELF] - Revert r311310 "[ELF] - Do not forget to fill last bytes of PT_LOADs with trap instructions."
It broke BB:
http://lab.llvm.org:8011/builders/lld-x86_64-darwin13/builds/11792/steps/test_lld/logs/stdio

llvm-svn: 311314
2017-08-21 08:13:45 +00:00
George Rimar c7392cbe9a [ELF] - Do not forget to fill last bytes of PT_LOADs with trap instructions.
Previously last 4 bytes of executable loads
were not filled with trap instructions,
patch fixes this bug.

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

llvm-svn: 311310
2017-08-21 07:51:21 +00:00
Ben Dunbobbin 95637558fa Test commit.
Removed extraneous comment markers

llvm-svn: 311169
2017-08-18 16:15:36 +00:00
Rui Ueyama 2b6631bb36 Remove GdbIndexSection::finalizeContents.
GdbIndexSection doesn't need lazy finalization because when an instance
of the class is created, we already know all debug info sections.
We can initialize the instnace in the ctor.

llvm-svn: 310931
2017-08-15 17:01:39 +00:00
Rui Ueyama 6238ed2457 Update comments as the function does not write to the first page anymore.
Also refactored the code a bit.

llvm-svn: 310886
2017-08-14 21:18:12 +00:00
Rafael Espindola 0158e2a50a Delete what looks like dead code.
The start of a segment should not have a gap that needs trap
instructions.

llvm-svn: 310696
2017-08-11 06:07:02 +00:00
Rui Ueyama 9c77d27004 Garbage-collect common symbols.
Liveness is usually a notion of input sections, but this patch adds
"liveness" bit to common symbols because they don't belong to any
input section.

This patch is based on https://reviews.llvm.org/D36520

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

llvm-svn: 310617
2017-08-10 15:54:27 +00:00
Rafael Espindola 35c908f20c Compute isPreemtible only once.
This is probably a small optimization, but the main motivation is
having a way of fixing pr34053 that doesn't require a hash lookup in
isPreempitible.

llvm-svn: 310602
2017-08-10 15:05:37 +00:00
Shoaib Meenai 335fad1c24 [lld] Allow rel iplt symbols with dynamic symbol table
Emit these symbols as long as we're building in a static configuration,
even if we're emitting a dynamic symbol table. This is consistent with
both bfd and gold.

Ordinarily, the combination of -static and -export-dynamic wouldn't make
much sense. Unfortunately, cmake versions prior to 3.4 forcefully
injected -rdynamic [1], so it seems worthwhile to support.

[1] https://cmake.org/cmake/help/v3.4/policy/CMP0065.html

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

llvm-svn: 310168
2017-08-05 05:01:07 +00:00
Rafael Espindola 6e93d0546a Move File from SymbolBody to Symbol.
With this Symbol has the same size as before, but DefinedRegular goes
from 72 to 64 bytes.

I also find this a bit easier to read. There are fewer places
initializing File for example.

This has a small but measurable speed improvement on all tests (1%
max).

llvm-svn: 310142
2017-08-04 22:31:42 +00:00
George Rimar d6bcde389a [ELF] - Fix "--symbol-ordering-file doesn't work with linker scripts"
This is PR33889,

Patch adds support of combination of linkerscript and
-symbol-ordering-file option.

If no sorting commands are present in script inside section declaration
and no --sort-section option specified, code uses sorting from ordering 
file if any exist.

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

llvm-svn: 310045
2017-08-04 10:25:29 +00:00
George Rimar a9b071482d [ELF] - Replace parallelForEach with ranged form.
Makes code a bit more convinent to write/read.

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

llvm-svn: 310040
2017-08-04 08:30:16 +00:00
Petr Hosek edd6c3587c [ELF] When the code segment is the last, align it to the page boundary
When the data segment is the last segment, it is correct to leave
it unaligned. However, when the code segment is the last segment,
it should be aligned to the page boundary to avoid loading the
non-segment parts of the ELF file at the end of the file.

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

llvm-svn: 309829
2017-08-02 16:35:00 +00:00
Rafael Espindola 8c022ca783 Merge OutputSectionCommand and OutputSection.
This is a bit of a hack, but it is *so* convenient.

Now that we create synthetic linker scripts when none is provided, we
always have to handle paired OutputSection and OutputsectionCommand and
keep a mapping from one to the other.

This patch simplifies things by merging them and creating what used to
be OutputSectionCommands really early.

llvm-svn: 309311
2017-07-27 19:22:43 +00:00
George Rimar aa3541875e [ELF] - Store PhdrEntry values by pointers instead of storing by value.
That is slightly more convinent as allows to store pointer on
program header entry in a more safe way.
 
Change was used in 2 patches currently on review.

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

llvm-svn: 309253
2017-07-27 07:46:50 +00:00
Rui Ueyama 709fb2bb10 Rename ObjectFile -> ObjFile.
Rename it because it was too easy to conflict with llvm::object::ObjectFile
which broke buildbots several times.

llvm-svn: 309199
2017-07-26 22:13:32 +00:00
Rafael Espindola aaa8e433ad Try to fix the windows build.
llvm-svn: 309162
2017-07-26 19:57:40 +00:00
Rafael Espindola bf991405de Try to fix the windows build.
llvm-svn: 309160
2017-07-26 19:37:58 +00:00
Rafael Espindola 244ef98161 Detemplate SymbolTable.
NFC, just makes it easier to access from non templated code.

llvm-svn: 309152
2017-07-26 18:42:48 +00:00
Rafael Espindola dc49af9d32 Make a function static. NFC.
llvm-svn: 308939
2017-07-24 23:55:33 +00:00
Dmitry Mikulin 97d6a80895 If user requested section alignment is greater than MaxPageSize, propagate it to segment headers correctly.
Differential Revision: https://reviews.llvm.org/D35813

llvm-svn: 308930
2017-07-24 21:27:02 +00:00
Rafael Espindola fae04ae44c Handle a section being more aligned than a page size.
llvm-svn: 308812
2017-07-22 00:17:57 +00:00
Rafael Espindola afbb7be6d5 Fix a crash.
This is PR33821.

What we really want to check in here is if the output section was
created, not if the command was empty.

llvm-svn: 308382
2017-07-18 21:46:27 +00:00
George Rimar 67c60727ce [ELF] - Apply clang-format. NFC.
llvm-svn: 308297
2017-07-18 11:55:35 +00:00
Rafael Espindola 300b38678c Refactor gdb index creation.
I have a patch to let DwarfContext defer to lld for getting section
contents and relocations.

That is a pretty big performance improvement.

This is just a refactoring to make that easier to do.

This change makes the *creation* of gdb index a dedicated step and
makes that templated. That is so that we can uses Elf_Rel in the code.

llvm-svn: 307867
2017-07-12 23:56:53 +00:00
Peter Smith 96f813d1a0 [ELF] Add call to assignAddresses() before createThunks() [NFC]
In preparation for the addition of rangeThunks() calculate the addresses
of all the inputSections so that ThunkSections can be inserted at the right
place.

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

llvm-svn: 307373
2017-07-07 10:03:37 +00:00
Rafael Espindola 055312465b Remove redundant argument. NFC.
llvm-svn: 307279
2017-07-06 16:40:44 +00:00
Rafael Espindola 2126334e08 Move fabricateDefaultCommands earlier.
It is now just after the OutputSections are created, which is as early
as it can possibly go.

llvm-svn: 307225
2017-07-05 23:36:24 +00:00
Rafael Espindola a4c15c1339 Remove dummy cast.
llvm-svn: 307220
2017-07-05 23:16:38 +00:00
Rafael Espindola 4cc0cd6556 Use Entry::SecSize in a couple of cases.
This avoids having to compute relocation section sizes early, removing
the last use of assignOffsets.

llvm-svn: 307219
2017-07-05 23:06:59 +00:00
Rafael Espindola cf7347379f Remove a function that is now trivial.
llvm-svn: 307214
2017-07-05 22:25:38 +00:00
Rafael Espindola cfeb9ae073 Remove two dummy casts. NFC.
llvm-svn: 307209
2017-07-05 22:07:20 +00:00
Rafael Espindola d7faa9165f Merge createOrphanCommands into addOrphanSections.
This is a bit simpler and avoids a walk over OutputSections.

llvm-svn: 307178
2017-07-05 17:50:43 +00:00
Peter Smith 5aedebff38 [ELF] Extract allocateHeaders() from assignAddresses()
The allocateHeaders() function is called at the end of assignAddresses(), it
decides whether the ELF header and program header table can be allocated to
a PT_LOAD program header. As the function alters state, it prevents
assignAddresses() from being called multiple times.

This change splits out the call to allocateHeaders() from assignAddresses()
this will permit assignAddresses() to be called while processing range
extension thunks without trying to allocateHeaders().

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

llvm-svn: 307131
2017-07-05 09:12:54 +00:00
Rafael Espindola 9b3f657d21 Move clearOutputSections earlier. NFC.
This is finally getting to the point where output sections are
constructed.

createOrphanCommands and fabricateDefaultCommands are moved out so
they can be merged with existing code in followup commits.

llvm-svn: 307107
2017-07-04 21:05:26 +00:00
Rafael Espindola d48b208833 Move clearOutputSections earlier. NFC.
Now addSectionSymbols operates on the linker script.

llvm-svn: 307102
2017-07-04 19:08:40 +00:00
Rafael Espindola b691545721 Move clearOutputSections earlier. NFC.
Now all of finalizeSections works on the linker script.

llvm-svn: 307101
2017-07-04 19:05:03 +00:00
Rafael Espindola 1eb3a0fa83 Move clearOutputSections earlier. NFC.
Now addPredefinedSections operates only on the linker script.

llvm-svn: 307100
2017-07-04 18:26:21 +00:00
George Rimar a951d5c40b [ELF] - Use llvm::find_if instead std::find_if where possible. NFC.
Two more places.

llvm-svn: 307076
2017-07-04 13:10:37 +00:00
Rafael Espindola 43ee36040d fix msvc build
llvm-svn: 307044
2017-07-03 17:32:09 +00:00
Rafael Espindola c080ff64dc Move clearOutputSections earlier.
Now removeUnusedSyntheticSections operates entirely on the linker
script.

llvm-svn: 307043
2017-07-03 16:54:39 +00:00
Petr Hosek 52db9a4fe6 [ELF] Remove unused synthetic sections from script commands
Script commands are processed before unused synthetic sections are
removed. Therefore, if a linker script matches one of these sections
it'll get emitted as an empty output section because the logic for
removing unused synthetic sections ignores script commands which
could have already matched and captured one of these sections. This
patch fixes that by also removing the unused synthetic sections from
the script commands.

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

llvm-svn: 307037
2017-07-03 15:49:25 +00:00
Rafael Espindola 85de6786ab Fix indentation.
llvm-svn: 306609
2017-06-28 22:44:11 +00:00
George Rimar e0b43df32e [ELF] - Do not crash when LLD synthesizes output sections with BYTE commands and -r
This is PR33596. Previously LLD would crash
because BYTE command synthesized output section,
but it was not assigned to Sec member of OutputSectionCommand.

Behaviour of -script and -r combination is not well defined,
but it seems after this change LLD naturally inherits behavior of
GNU linkers - creates output section requested in script and does not
crash anymore.

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

llvm-svn: 306527
2017-06-28 09:59:34 +00:00
Peter Smith 691ff76662 [ELF] Consolidate .ARM.extab.* sections into .ARM.extab
When -ffunction-sections and ARM C++ exceptions are used each .text.suffix
section will have at least one .ARM.exidx.suffix section and may have an
additional .ARM.extab.suffix section if the unwinding instructions are too
large to inline into the .ARM.exidx table entry. For a large program without
a linker script this can lead to a large number of section header table
entries that can increase the size of the ELF file.

This change introduces a default rule for .ARM.extab.* to be placed in
a single output section called .ARM.extab . This follows the behavior of
ld.gold and ld.bfd.

fixes pr33407

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

llvm-svn: 306522
2017-06-28 09:12:38 +00:00
Rui Ueyama 92c3781959 Add GlobalOffsetTable to ElfSym. NFC.
Most "reserved" symbols are in ElfSym and it looks like there's no
reason to not do the same thing for _GLOBAL_OFFSET_TABLE_. This should
help https://reviews.llvm.org/D34618 too.

llvm-svn: 306292
2017-06-26 15:11:24 +00:00
Peter Smith 113a59e7db [ELF] Define _GLOBAL_OFFSET_TABLE_ symbol relative to .got
On many architectures gcc and clang will recognize _GLOBAL_OFFSET_TABLE_ - .
and produce a relocation that can be processed without needing to know the
value of _GLOBAL_OFFSET_TABLE_. This is not always the case; for example ARM
gcc produces R_ARM_BASE_PREL but clang produces the more general
R_ARM_REL32 to _GLOBAL_OFFSET_TABLE_. To evaluate this relocation
correctly _GLOBAL_OFFSET_TABLE_ must be defined to be the either the base of
the GOT or end of the GOT dependent on architecture..

If/when llvm-mc is changed to recognize _GLOBAL_OFFSET_TABLE_ - . this
change will not be necessary for new objects. However there may still be
old objects and versions of clang.

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

llvm-svn: 306282
2017-06-26 10:22:17 +00:00
Rafael Espindola 9c0395e39e Prefer -Ttext over linker script values.
I found this while trying to build u-boot. It uses -Ttext in
combination with linker scripts.

My first reaction was to change the linker scripts to have the correct
value, but I found that it is actually quite convenient to have -Ttext
take precedence.

By having just

.text : { *(.text) }

In the script, they can define the text address in a single Makefile
and pass it to ld with -Ttext and for the C code with
-DFoo=value. Doing the same with linker scripts would require them to
be generated during the build.

llvm-svn: 305766
2017-06-20 01:51:50 +00:00
Peter Smith 3298027951 [ELF] Enable createThunks to be called more than once.
In preparation for supporting range extension thunks we now continually
call createThunks() until no more thunks are added. This requires us to
record the thunks we add on each pass and only merge the new ones into the
OutputSection. We also need to check if a Relocation is targeting a thunk
to prevent us from infinitely creating more thunks.
    
Differential Revision: https://reviews.llvm.org/D34034

llvm-svn: 305555
2017-06-16 13:10:08 +00:00
Rafael Espindola c54b1c8b4d Try to fix MSVC build.
llvm-svn: 305514
2017-06-15 22:03:06 +00:00
Rafael Espindola 383971d2a7 Move clearOutputSections before sortSections.
This is probably the main patch left in unifying our intermediary
representation.

It moves the creation of default commands before section sorting. This
has the nice effect that we now have one location where we decide
where an orphan section should be placed.

Before this patch sortSections would decide the relative location of
orphan sections to other sections, but it was up to placeOrphanSection
to decide on the exact location.

We now only sort sections we created since the linker script is
already in the correct order.

llvm-svn: 305512
2017-06-15 21:51:01 +00:00
Rafael Espindola f51c80559c Make OutputSections and OutputSectionCommands globals.
This is similar to what we do for InputSections and makes them easier
to access.

llvm-svn: 305337
2017-06-13 23:26:31 +00:00
Rafael Espindola cdf813bbd1 Move clearOutputSections earlier. NFC.
llvm-svn: 305333
2017-06-13 22:36:20 +00:00
Peter Collingbourne dc7936eced ELF: Move section merging before ICF. NFCI.
Differential Revision: https://reviews.llvm.org/D34093

llvm-svn: 305176
2017-06-12 00:00:51 +00:00
Rafael Espindola 7ff9329b7c Move clearOutputSections before createPhdrs. NFC.
llvm-svn: 305156
2017-06-10 22:12:32 +00:00
Rafael Espindola e76231b647 Move fabricateDefaultCommands earlier.
This then requires delaying a call to getHeaderSize.

llvm-svn: 304961
2017-06-08 04:17:04 +00:00
Rafael Espindola 7204620d32 Use OutputSectionCommands in assignOffsets.
This allows moving clearOutputSections earlier.

llvm-svn: 304952
2017-06-07 23:08:55 +00:00
Rafael Espindola f2c41271ca Delete dead code.
The real offsets are computed in assignAddresses and we compute the
special cases we need earlier after this.

llvm-svn: 304950
2017-06-07 22:57:52 +00:00
Rafael Espindola fa6fcff2e3 Move clearOutputSections earlier. NFC.
llvm-svn: 304948
2017-06-07 22:27:51 +00:00
Rafael Espindola 2356c9ffae Convert an use of OutputSections to OutputSectionCommands.
This allows moving clearOutputSections a bit earlier.

llvm-svn: 304947
2017-06-07 22:23:01 +00:00
Peter Smith 8e791463ef [ELF] Convert Thunks to use InputSectionDescriptions
Thunks are now generated per InputSectionDescription instead of per
OutputSection. This allows created ThunkSections to be inserted directly
into InputSectionDescription.

Changes in this patch:
- Loop over InputSectionDescriptions to find relocations to Thunks
- Generate a ThunkSection per InputSectionDescription
- Remove synchronize() as we no longer need it
- Move fabricateDefaultCommands() before createThunks

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

llvm-svn: 304887
2017-06-07 09:35:14 +00:00
George Rimar 990c9cb2bf [ELF] - Do not merge relocation sections by name when using --emit-relocs.
Previously we would merge relocation sections by name.
That did not work in some cases, like testcase shows.

Patch implements logic to merge relocation sections if their target
sections were merged into the same output section.

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

llvm-svn: 304886
2017-06-07 09:20:35 +00:00
Rafael Espindola d57c58d75d Move clearOutputSections earlier. NFC.
This now just requires not calling assignOffsets after it.

llvm-svn: 304861
2017-06-07 02:31:19 +00:00
Rafael Espindola 189860c317 Use assignAddresses with -r.
Before this patch in -r we compute the OutputSection sizes early in
the various calls to assignOffsets. With this change we can remove
most of those calls.

llvm-svn: 304860
2017-06-07 02:24:08 +00:00
Rafael Espindola 8c284acf14 Move finalize to OutputSectionCommands. NFC.
This removes a mapping from OutputSection to OutputSectionCommand and
is another step in moving clearOutputSections earlier.

llvm-svn: 304821
2017-06-06 19:40:01 +00:00
Rafael Espindola 13f412f6a2 Convert a use of OutputSections. NFC.
llvm-svn: 304820
2017-06-06 19:29:36 +00:00
Rafael Espindola 0e454a9837 Define __executable_start.
This is defined by both bfd and gold and used by the android libc.

llvm-svn: 304803
2017-06-06 16:18:48 +00:00
Rafael Espindola a6fef0e5db Move clearOutputSections earlier.
This moves clearOutputSections and OutputSectionCommands creation as
early as possible without changing other code.

llvm-svn: 304751
2017-06-05 23:43:49 +00:00
Petr Hosek 7953e62f2e [ELF] Define __dso_handle symbol if needed
Traditionally, it has been defined in crtbegin.o, which is typically
provided by libgcc or as part of the C library on some systems. However,
but there's no principled reason for it to be there. We optionaly
define this symbol, which can be used on platforms that don't provide
__dso_handle in crtbegin.o or which don't use crtbegin.o at all.

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

llvm-svn: 304732
2017-06-05 20:42:34 +00:00
George Rimar 60a0ea161e [ELF] - Make LLD remove gnu-lib compression prefix (".z") after decompression when using -r
This is PR33289.

Previously LLD leaved section naming as is and that lead to wrong result,
because we decompress sections when using -r,
and hence should remove ".z" prefix.

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

llvm-svn: 304711
2017-06-05 12:49:21 +00:00
Peter Smith 43e852fb79 [ELF] SHF_LINK_ORDER should sort based on InputSectionDescriptions
This change alters the sorting for OutputSections with the SHF_LINK_ORDER
flag in OutputSection::finalize() to use the InputSectionDescription
representation and not the OutputSection::Sections representation.

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

llvm-svn: 304700
2017-06-05 08:51:15 +00:00
Rafael Espindola c7c42f6ae0 Convert another OutputSections use.
This allows moving clearOutputSections a bit earlier.

llvm-svn: 304654
2017-06-03 06:51:22 +00:00
Rafael Espindola 0f7dc0e2d0 Run fabricateDefaultCommands before fixSectionAlignments.
This allows us to remove the PageAlign field. It will also allow
moving fabricateDefaultCommands earlier.

llvm-svn: 304513
2017-06-02 01:37:58 +00:00
Rafael Espindola 6888072808 Move maybeCompress to OutputSectionCommand.
This removes a call to getCmd and allows us to move
clearOutputSections earlier.

llvm-svn: 304439
2017-06-01 16:57:48 +00:00
Rafael Espindola cca62ef861 Move synchronize earlier.
This is probably the correct location for it: next to
fabricateDefaultCommands. If we don't have a linker script, we
fabricate one. If we have one, we patch it.

llvm-svn: 304436
2017-06-01 16:42:16 +00:00
Rafael Espindola 3f235c71cc Move clearOutputSections earlier.
Now it is as early as it can go: just before synchronize. We now have
to move synchronize earlier too.

llvm-svn: 304434
2017-06-01 16:32:58 +00:00
Rafael Espindola 805f5152c5 Synchronize the linker script with -r.
I don't have a testcase in mind, but there is no reason to have the
linker script out of sync during a -r link.

llvm-svn: 304433
2017-06-01 16:30:12 +00:00
Rafael Espindola faf25a7216 Convert a few more uses of OutputSections. NFC.
Also needed to move clearOutputSections earlier.

llvm-svn: 304420
2017-06-01 16:26:28 +00:00
Rafael Espindola 8a0966ea03 Convert a few user of OutputSections NFC.
This is needed to move clearOutputSections earlier.

llvm-svn: 304419
2017-06-01 16:21:33 +00:00
Rafael Espindola 47c9f84d50 Simplify. NFC.
The sections are ordered, so we can just use the first one when
looking for the lowest address.

llvm-svn: 304369
2017-05-31 22:49:50 +00:00
Rafael Espindola cccd2c604e Merge two nested ifs. NFC.
llvm-svn: 304368
2017-05-31 22:46:19 +00:00
Rafael Espindola 969c6512c2 Move clearOutputSections earlier.
Another step into merging the linker script and non linker script code
paths.

llvm-svn: 304339
2017-05-31 20:22:27 +00:00
Rafael Espindola db5e56f7b2 Store a single Parent pointer for InputSectionBase.
Before InputSectionBase had an OutputSection pointer, but that was not
always valid. For example, if it was a merge section one actually had
to look at MergeSec->OutSec.

This was brittle and caused bugs like the one fixed by r304260.

We now have a single Parent pointer that points to an OutputSection
for InputSection, but to a SyntheticSection for merge sections and
.eh_frame. This makes it impossible to accidentally access an invalid
OutSec.

llvm-svn: 304338
2017-05-31 20:17:44 +00:00
Peter Smith 626c997aff [ELF] Add .ARM.exidx sentinel section to an OutputSectionDescription
When there is a linker script with .ARM.exidx in the SECTIONS
command we must add the .ARM.exidx sentinel section to the
InputSectionDescriptions as well as to OutputSection::Sections.

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

llvm-svn: 304206
2017-05-30 11:51:02 +00:00
Rafael Espindola 1da971706e Replace a few more uses of OutputSections.
llvm-svn: 304182
2017-05-30 01:36:48 +00:00
Rafael Espindola 2d9a3be2ab Keep a list of all OutputSectionCommands.
Now that we are trying to use the linker script representation as the
canonycal one, there are a few loops looking for just OutputSectionCommands.

Create a vector with just the OutputSectionCommands once that is
stable to simplify the rest of the code.

llvm-svn: 304181
2017-05-30 01:30:14 +00:00
Rafael Espindola 43e76cd489 Avoid a couple uses of OutputSections.
After fabricateDefaultCommands we can look at the script commands.

llvm-svn: 304014
2017-05-26 17:48:27 +00:00
Rafael Espindola 881cc1643a Avoid using EhFrameHdr->Sections.
This is a necessary step for moving clearOutputSections earlier.

llvm-svn: 304009
2017-05-26 17:28:17 +00:00
Rafael Espindola d23e9267a6 Order writable executable sections before writable ones.
On SPARC, .plt is both writeable and executable. The current way
sections are sorted means that lld puts it after .data/.bss. but it
really needs to be close to .test to make sure branches into .plt
don't overflow. I'd argue that because .bss is supposed to come last
on all architectures, we should change the default sort order such
that writable and executable sections come before sections that are
just writeable. read-only executable sections should still come after
sections that are just read-only of course. This diff makes this
change.

llvm-svn: 304008
2017-05-26 17:23:25 +00:00
Rui Ueyama e962e37792 Rearrange code for the sake of readability. NFC.
llvm-svn: 303961
2017-05-26 02:27:19 +00:00
Rui Ueyama 9e6f268054 Inline a trivial constructor.
llvm-svn: 303958
2017-05-26 02:17:13 +00:00
Rui Ueyama 2f106b4690 When uncompressing sections, remove SHF_COMPRESSED bits. NFC.
In this way, the content and the flag is always consistent, which I
think better than removing the bit when input sections reaches the Writer.

llvm-svn: 303926
2017-05-25 22:00:36 +00:00
Rafael Espindola 55b169bf5d Move writeTo to OutputSectionCommand.
This reduces how many times we have to map from OutputSection to
OutputSectionCommand. It is a required step to moving
clearOutputSections earlier.

In order to always use writeTo in OutputSectionCommand we have to call
fabricateDefaultCommands for -r links and move section compression
after it.

llvm-svn: 303784
2017-05-24 18:08:04 +00:00
Rafael Espindola 27dd659be1 Move clearOutputSections earlier. NFC.
llvm-svn: 303781
2017-05-24 17:54:28 +00:00
Rafael Espindola aeb4b7d217 Clear OutSec->Sections.
Once the dummy linker script is created, we want it to be used for
everything to avoid having two redundant representations that can get
out of sync.

We were already clearing OutputSections. With this patch we clear the
Sections vector of every OutputSection.

llvm-svn: 303703
2017-05-23 22:54:06 +00:00
Rafael Espindola a46f688e8f Use linker script commands in writeMapFile.
This converts the last (chronologically) user of OutputSections to use
the linker script commands instead.

The idea is to convert all uses after fabricateDefaultCommands, so
that we have a single representation.

llvm-svn: 303384
2017-05-18 21:30:14 +00:00
Rafael Espindola a6465bbb72 Alternative way to detemplate GotSection.
GetSection is a template because write calls relocate.

relocate has two parts. The non alloc code really has to be a
template, as it is looking a raw input file data.

The alloc part is only a template because of getSize.

This patch folds the value of getSize early, detemplates
getRelocTargetVA and splits relocate into a templated non alloc case
and a regular function for the alloc case. This has the nice advantage
of making sure we collect all the information we need for relocations
before getting to InputSection::relocateNonAlloc.

Since we know got is alloc, it can just call the function directly and
avoid the template.

llvm-svn: 303355
2017-05-18 16:45:36 +00:00
Rafael Espindola 246c1c47ea Fix flag to start with 1 << 0. NFC.
Thanks to Andrew Ng for noticing it.

llvm-svn: 303354
2017-05-18 16:20:12 +00:00
George Rimar ec84ffc529 [ELF] - Detemplate Thunk creation.
Nothing special here, just detemplates code that became possible 
to detemplate after recent commits in a straghtforward way.

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

llvm-svn: 303237
2017-05-17 07:10:59 +00:00
George Rimar 69b17c35d9 [ELF] - Detemplate access to SymTab, DynSymTab, GnuHashTab. NFC.
Follow up for r303150.

llvm-svn: 303153
2017-05-16 10:04:42 +00:00
George Rimar f45f681609 [ELF] - Detemplate GnuHashTableSection and SymbolTableSection sections.
SymbolTableBaseSection was introduced.

Detemplation of SymbolTableSection should allow to detemplate more things.

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

llvm-svn: 303150
2017-05-16 08:53:30 +00:00
George Rimar ab94768cc1 [ELF] - Use llvm::to_integer() instead of StringRef::getAsInteger().
Switch to llvm::to_integer() everywhere in LLD instead of 
StringRef::getAsInteger() because API of latter is confusing. 
It returns true on error and false otherwise what makes reading 
the code incomfortable.

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

llvm-svn: 303149
2017-05-16 08:19:25 +00:00
George Rimar 09328b3ef8 [ELF] - Apply clang-format. NFC.
llvm-svn: 302921
2017-05-12 17:00:07 +00:00
Rafael Espindola 5210141b07 Optimize orphan placement in a general way.
We used to place orphans by just using compareSectionsNonScript.

Then we noticed that since linker scripts can use another order, we
should first try match the section to a given PT_LOAD. But there is
nothing special about PT_LOAD. The same issue can show up for
PT_GNU_RELRO for example.

In general, we have to search for the most similar section and put the
orphan next to it. Most similar being defined as how long they follow
the same code path in compareSecitonsNonScript.

That is what this patch does. We now compute a rank for each output
section, with a bit for each branch in what was
compareSectionsNonScript.

With this findOrphanPos is now fully general and orphan placement can
be optimized by placing every section with the same rank at once.

The included testcase is a variation of many-sections.s that uses
allocatable sections to avoid the fast path in the existing
code. Without threads it goes form 46 seconds to 0.9 seconds.

llvm-svn: 302903
2017-05-12 14:52:22 +00:00
Rafael Espindola 8ff5c41192 Detemplate compareSections. NFC.
llvm-svn: 302849
2017-05-11 23:37:55 +00:00
Rafael Espindola 24a5a9058e Detemplate compareSectionsNonScript. NFC.
llvm-svn: 302848
2017-05-11 23:33:19 +00:00
Rafael Espindola 9e889eb48c Detemplate isRelroSection. NFC.
llvm-svn: 302847
2017-05-11 23:31:06 +00:00
Rafael Espindola 88ab9fb163 Detemplate the got.
This is a bit hackish, but allows for a lot of followup cleanups.

llvm-svn: 302845
2017-05-11 23:26:03 +00:00
Rafael Espindola 5ab1989584 Reduce templating. NFC.
llvm-svn: 302843
2017-05-11 23:16:43 +00:00
Rafael Espindola 895aea6d15 Reduce template usage. NFC.
llvm-svn: 302832
2017-05-11 22:02:41 +00:00
Rafael Espindola b3aa2c9b9e Reduce template usage. NFC.
llvm-svn: 302828
2017-05-11 21:33:30 +00:00
Rafael Espindola 4b1c3696e3 Reduce template usage. NFC.
llvm-svn: 302826
2017-05-11 21:23:38 +00:00
Petr Hosek 6b936bf6c7 [ELF] Define __ehdr_start unconditionally even when using linker script
This behavior differs from the semantics implemented by GNU linkers
which only define this symbol iff ELF headers are in the memory
mapped segment.

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

llvm-svn: 302687
2017-05-10 16:20:33 +00:00
Rafael Espindola dc1ed12015 Remove another use of section names. NFC.
llvm-svn: 302662
2017-05-10 14:12:02 +00:00
Rafael Espindola 0ca3712796 Use a simpler heuristic for placing orphans.
This is a bit easier to read and a lot faster in some cases. A version
of many-sections.s with linker scripts goes from 0m41.232s to
0m19.575s.

llvm-svn: 302528
2017-05-09 13:58:46 +00:00
Rafael Espindola b5de5b9354 Simplify orphan section positioning.
The code following this one already considers every possible insertion
point for orphan sections, there is no point in sorting them before.

llvm-svn: 302441
2017-05-08 16:49:20 +00:00
Rafael Espindola 660c9ab929 Delete LinkerScript::getSectionIndex.
We can set SectionIndex tentatively as we process the linker script
instead of looking it repeatedly.

In general we should try to have as few name lookups as possible.

llvm-svn: 302299
2017-05-05 21:34:26 +00:00
Rafael Espindola 02ed7575e7 Simplify the header allocation.
In the non linker script case we would try very early to find out if
we could allocate the headers. Failing to do that would add extra
alignment to the first ro section, since we would set PageAlign
thinking it was the first section in the PT_LOAD.

In the linker script case the header allocation must be done in the
end, causing some duplication.

We now tentatively add the headers to the first PT_LOAD and if it
turns out they don't fit, remove them. With this we only need to
allocate the headers in one place in the code.

llvm-svn: 302186
2017-05-04 19:34:17 +00:00
Peter Smith c60b4510ea [ELF] Fix problems with fabricateDefaultCommands() and --section-start
The --section-start <name>=<address> needs to be translated into equivalent
linker script commands. There are a couple of problems with the existing
implementation:
- The --section-start with the lowest address is assumed to be at the start
of the map. This assumption is incorrect, we have to iterate through the
SectionStartMap to find the lowest address.
- The addresses in --section-start were being over-aligned when the
sections were marked as PageAlign. This is inconsistent with the use of
SectionStartMap in fixHeaders(), and can cause problems when the PageAlign
causes an "unable to move location counter backward" error when the
--section-start with PageAlign is aligned to an address higher than the next
--section-start. The ld.bfd and ld.gold seem to be more consistent with this
approach but this is not a well specified area.
    
This change fixes the problems above and also corrects a typo in which
fabricateDefaultCommands() is called with the wrong parameter, it should be
called with AllocateHeader not Config->MaxPageSize.

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

llvm-svn: 302007
2017-05-03 08:44:50 +00:00
Rafael Espindola de8d9897d9 Bring back r301678.
This version uses a set to speed up the synchronize method.

Original message:

    Remove LinkerScript::flush.

    This patch replaces flush with a last ditch attempt at synchronizing
    the section list with the linker script "AST".

    The synchronization is a bit of a hack and should in time be avoided
    by creating the AST earlier so that modifications can be made directly
    to it instead of modifying the section list and synchronizing it back.

    This is the main step for fixing
    https://bugs.llvm.org/show_bug.cgi?id=32816. With this in place I
    think the only missing thing would be to have processCommands assign
    section indexes as dummy offsets so that the sort in
    OutputSection::finalize works.

    With this LinkerScript::assignAddresses becomes much simpler, which
    should help with the thunk work.

llvm-svn: 301745
2017-04-29 15:44:03 +00:00
Rui Ueyama 216aa11cc5 Revert r301678: Remove LinkerScript::flush.
This reverts commit r301678 since that change significantly slowed
down the linker. Before this patch, LLD could link clang in 8 seconds,
but with this patch it took 40 seconds.

llvm-svn: 301709
2017-04-28 22:40:58 +00:00
Rafael Espindola b3bc1ed0d4 Remove LinkerScript::flush.
This patch replaces flush with a last ditch attempt at synchronizing
the section list with the linker script "AST".

The synchronization is a bit of a hack and should in time be avoided
by creating the AST earlier so that modifications can be made directly
to it instead of modifying the section list and synchronizing it back.

This is the main step for fixing
https://bugs.llvm.org/show_bug.cgi?id=32816. With this in place I
think the only missing thing would be to have processCommands assign
section indexes as dummy offsets so that the sort in
OutputSection::finalize works.

With this LinkerScript::assignAddresses becomes much simpler, which
should help with the thunk work.

llvm-svn: 301678
2017-04-28 20:22:47 +00:00
Rui Ueyama 63d48e5956 Remove needless type conversions.
llvm-svn: 301513
2017-04-27 04:01:14 +00:00