Commit Graph

781 Commits

Author SHA1 Message Date
Rui Ueyama 2f4c121d89 Inline LinkerScript::process.
"process" was not a good name because everything can be named it.

llvm-svn: 315430
2017-10-11 04:21:55 +00:00
Rui Ueyama 355a8dd663 Split a loop into two to make it clear that it did two different things.
llvm-svn: 315427
2017-10-11 04:01:24 +00:00
Rui Ueyama 0543343170 Use more precise type.
llvm-svn: 315426
2017-10-11 04:01:13 +00:00
Rui Ueyama 187b67a533 Remove a static local varaible.
We should generally avoid static local variables.

llvm-svn: 315425
2017-10-11 03:34:09 +00:00
Rui Ueyama 1f4d7b56f4 Avoid a pointer to a pointer to an input section.
llvm-svn: 315424
2017-10-11 03:23:29 +00:00
Rui Ueyama ec5c4adbf2 Add comment.
llvm-svn: 315423
2017-10-11 03:23:17 +00:00
Rui Ueyama b801441ed3 Remove useless cast.
llvm-svn: 315421
2017-10-11 02:55:05 +00:00
Rui Ueyama 04c9ca7408 Rename filename -> getFilename.
llvm-svn: 315420
2017-10-11 02:54:52 +00:00
Rui Ueyama 71f840672d Rename Align -> Alignment.
llvm-svn: 315419
2017-10-11 02:46:09 +00:00
Rui Ueyama 29b240c671 Rename CurAddressState -> Ctx.
We used CurAddressState to capture a dynamic context just like
we use lambdas to capture static contexts. So, CurAddressState
is used everywhere in LinkerScript.cpp. It is worth a shorter
name.

llvm-svn: 315418
2017-10-11 02:45:54 +00:00
Rui Ueyama 5908c2f877 Rename processCommands -> processSectionCommands.
llvm-svn: 315415
2017-10-11 02:28:28 +00:00
Rui Ueyama a80633ca14 Inline small functions.
llvm-svn: 315414
2017-10-11 02:20:00 +00:00
Rui Ueyama 6b394caaf1 Rename Commands -> SectionCommands.
"Commands" was ambiguous because in the linker script, everything is
a command. We used to handle only SECTIONS commands, and at the time,
it might make sense to call them the commands, but it is no longer
the case. We handle not only SECTIONS but also MEMORY, PHDRS, VERSION,
etc., and they are all commands.

llvm-svn: 315409
2017-10-11 01:50:56 +00:00
Rui Ueyama a323e2a722 Rename HasSections -> HasSectionsComand.
HasSections is true if there is at least one SECTIONS linker
script command, and it is not directly related to whether we have
section objects or not. So I think the new name is better.

llvm-svn: 315405
2017-10-11 01:34:51 +00:00
Rui Ueyama 7f1c266e43 Move a loop invariant outside the loop.
llvm-svn: 315404
2017-10-11 01:26:22 +00:00
Rui Ueyama ac27de9dc7 Remove ScriptConfiguration class and move the members to LinkerScript class.
ScriptConfiguration was a class to contain parsed results of
linker scripts. LinkerScript is a class to interpret it.

That ditinction was needed because we haven't instantiated
LinkerScript early (because, IIRC, LinkerScript class was a
ELFT template function). So, when we parse linker scripts,
we couldn't directly store the result to a LinkerScript instance.

Now, that limitation is gone. We instantiate LinkerScript
at the very beginning of our main function. We can directly
store parse results to a LinkerScript instance.

llvm-svn: 315403
2017-10-11 01:19:33 +00:00
Rui Ueyama 18d19687c8 Inline addRegular into addSymbol.
Because addRegular's functionality is tightly coupled with
addSymbol, and the former is called only once, it makes sense
to merge the two functions. This patch also adds comments.

llvm-svn: 315401
2017-10-11 01:03:37 +00:00
Rui Ueyama 4fbe351817 Remove a constructor from ExprValue. NFC.
I think three ctors are too many for this simple class.

llvm-svn: 315394
2017-10-11 00:06:27 +00:00
James Henderson b5ca92ef73 [ELF] Set Dot initially to --image-base value when using linker scripts
When parsing linker scripts, LLD previously started with a '.' value of 0,
regardless of the internal default image base for the target, and regardless of
switches such as --image-base. It seems reasonable to use a different image base
value when using linker scripts and --image-base is specified, since otherwise the
switch has no effect. This change does this, as well as removing unnecessary
initialisation of Dot where it is not used.

The default image base should not be used when processing linker
scripts, because this will change the behaviour for existing linker script users,
and potentially result in invalid output being produced, as a subsequent assignment
to Dot could move the location counter backwards. Instead, we maintain the existing
behaviour of starting from 0 if --image-base is not specified.

Reviewers: ruiu

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

llvm-svn: 315293
2017-10-10 10:09:35 +00:00
Rui Ueyama e03ba02348 Rename ignoreInterpSection -> needsInterpSection.
This should improve consistency. Also added comment.

llvm-svn: 315169
2017-10-08 03:52:15 +00:00
Rui Ueyama 0ae2c24c5d Use llvm::Optional instead of UINT_MAX to represent a null value.
llvm-svn: 315168
2017-10-08 03:45:49 +00:00
Rui Ueyama 656be31174 Make a helper function a non-member function. NFC.
llvm-svn: 315167
2017-10-08 02:54:42 +00:00
Rui Ueyama 872235743d Use llvm::Optional instead of a magic number -1 to represent "no result".
llvm-svn: 315166
2017-10-08 02:44:08 +00:00
Rui Ueyama 9b18f50f79 Remove a trivial function.
llvm-svn: 315164
2017-10-08 02:06:03 +00:00
Rui Ueyama 8befefb2ea Remove OutputSection::updateAlignment.
I feel it is easier to understand without this function.

llvm-svn: 315140
2017-10-07 00:58:34 +00:00
Rui Ueyama 0e2bfb1e3b Merge addInputSec with OutputSection::addSection.
Previously, when we added an input section to an output section, we
called `OutputSectionFactory::addInputSec`. This isn't a good design
because, a factory class is intended to create a new object and
return it, but in this use case, it will never create a new object.
This patch fixes the design flaw.

llvm-svn: 315138
2017-10-07 00:43:31 +00:00
Rui Ueyama b4175edf16 Remove unused parameters.
llvm-svn: 315133
2017-10-07 00:08:30 +00:00
Rui Ueyama c54d5b16d5 Do not mutate Script->Opt.Commands from a leaf helper function.
Factory::addInputSec added an output section to Script->Opt.Commands,
but that is too subtle. This patch makes it explicit so that it is easy
to see when a new element is added to Script->Opt.Commands.

llvm-svn: 315129
2017-10-06 23:34:43 +00:00
Rui Ueyama d2f225fdef Simplify LinkerScript::addOrphanSections. NFCI.
This patch moves a std::find to a new function. It also removes
the following piece of code. I believe it should be fine because all
tests still pass.

  unsigned Index = std::distance(Opt.Commands.begin(), I);
  assert(Sec->SectionIndex == INT_MAX || Sec->SectionIndex == Index);
  Sec->SectionIndex = Index;

llvm-svn: 315125
2017-10-06 23:06:55 +00:00
Rui Ueyama edafba200f Split addSection into two small functions. NFCI.
addSection function was hard to read because it behaves differently
depending on its arguments but what exactly it does is not clear.
Now it should be better. Still, it is not clear (not what but) why
it does what it does, but I'll take a look at it later.

llvm-svn: 315124
2017-10-06 23:06:40 +00:00
George Rimar ba45584c4b [ELF] - Stop removing sections in removeUnusedSyntheticSections().
That makes code a bit more consistent. Instead of removing sections there
we can just mark them as dead. So that removeEmptyCommands() will
handle the rest.

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

llvm-svn: 314654
2017-10-02 09:11:13 +00:00
Rafael Espindola 1f0fe88a1b Fix header location with PHDR.
We were not subtracting its size, causing it to overlap with section
data.

Fixes PR34750.

llvm-svn: 314440
2017-09-28 18:12:13 +00:00
Rui Ueyama 761f0b660d Use UINT64_MAX instead of implicitly-type-converted -1.
llvm-svn: 314126
2017-09-25 17:40:21 +00:00
Rui Ueyama f5db0b36ff Use a temporary varaible to improve readability. NFC.
llvm-svn: 314120
2017-09-25 17:19:17 +00:00
George Rimar 347c70d782 [ELF] - Report orphan sections if -verbose given.
When -verbose is specified, patch outputs names of each input orphan section
assigned to output.

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

llvm-svn: 314098
2017-09-25 09:41:32 +00:00
Rafael Espindola 9be24cf516 Fix assigning to _gp in linker scripts.
The previous logic was to try to detect if a linker script defined _gp
by checking !ElfSym::MipsGp->Value. That doesn't work in all cases as
the assigned value can be 0.

We now just always defined it Writer.cpp and always overwrite it
afterwards if needed.

llvm-svn: 313788
2017-09-20 18:30:57 +00:00
Rafael Espindola 8b250344e9 Add a special case for trivial alignment.
Normally to find the offset of a value in a section, we have to
compute the value since the alignment is defined on the final address.

If the alignment is trivial, we can skip the value computation. This
allows us to know the offset even in cases where we cannot yet know
the value.

llvm-svn: 313777
2017-09-20 17:43:44 +00:00
Rafael Espindola e4bad83edb Don't try to compute a value that is known to fail.
We try to evaluate expressions early when possible, but it is not
possible to evaluate them early if they are based on a section.

Before we would get this wrong on ABSOLUTE expressions.

llvm-svn: 313764
2017-09-20 16:42:56 +00:00
George Rimar 696a7f9ac6 [ELF] - Introduce std::vector<InputFile *> global arrays.
This patch removes lot of static Instances arrays from different input file 
classes and introduces global arrays for access instead. Similar to arrays we
have for InputSections/OutputSectionCommands.

It allows to iterate over input files in a non-templated code.

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

llvm-svn: 313619
2017-09-19 09:20:54 +00:00
George Rimar 8962db9111 [ELF] - Simplify adjustSectionsBeforeSorting().
Does not seem we need to set SectionIndex here.
It is set in finalizeSections() later.

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

llvm-svn: 313522
2017-09-18 08:43:44 +00:00
Rafael Espindola 67df57a242 Remove Offset from Common.
It is not needed since it is always 0.

llvm-svn: 313076
2017-09-12 21:19:09 +00:00
Rafael Espindola a6acd23c53 Align addresses, not offsets.
This fixes two more cases where we were aligning the offset in a
section, instead of the final address.

llvm-svn: 312983
2017-09-12 00:06:00 +00:00
Rafael Espindola b7147ad3dd Correct ALIGN expression when inside a section.
When given

foobar = ALIGN(., 0x100);

my expectation from what the manual says is that the final address of
foobar will be aligned. It seems that bfd aligns the offset in the
section, which causes some odd results if the section is not 0x100
aligned. Gold aligns the address.

This changes lld to align the final address.

llvm-svn: 312979
2017-09-11 23:44:53 +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 5f37541c73 [ELF] - Linkerscript: implement REGION_ALIAS.
REGION_ALIAS(alias, region)

Alias names can be added to existing memory regions created with
the MEMORY command. Each name corresponds to at most one
memory region.

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

llvm-svn: 312777
2017-09-08 08:23:15 +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
George Rimar c2dffe3aa0 [ELF] - Linkerscript: set load address correctly if MEMORY command used.
Previously LLD did not calculate LMAOffset correctly when
AT and MEMORY were used together.

Patch fixes PR34407.

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

llvm-svn: 312625
2017-09-06 09:35:09 +00:00
George Rimar 60608a8ae5 [ELF] - Simplify (use llvm::erase_if). NFC.
llvm-svn: 311878
2017-08-28 09:28:15 +00:00
Dmitry Mikulin f300ca211f Currently lld uses base names of files to match against file patterns in
linker script SECTION rules. This patch extends it to use a fully specified
file name as it appears in --trace output to match agains, i.e,
"<path>/<objname>.o" or "<path>/<libname>.a(<objname>.o)".

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

llvm-svn: 311713
2017-08-24 22:01:40 +00:00
Petr Hosek b93c5b9f7e [ELF] Don't output headers into a segment if there's no space for them
Currently, LLD checks whether there's enough space for headers by
checking if headers fit below the address of the first allocated
section. However, that's always thue if the binary doesn't start
at zero which means that LLD always emits a segment for headers,
even if no other sections belong to that segment.

This is a problem in cases when linker script is being used with a
non-zero start address when we don't want to make the headers visible
by not leaving enough space for them. This pattern is common in
embedded programming but doesn't work in LLD.

This patch changes the behavior of LLD in case when linker script
is being to match the behavior of BFD ld and gold, which is to only
place headers into a segment when they're covered by some output
section.

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

llvm-svn: 311586
2017-08-23 18:44:34 +00:00
George Rimar 5d0ea70ad5 [ELF] - Do not segfault when doing logical and/or operations on symbols that have no output sections.
Previously we would crash on samples from testcase,
because were trying to access zero pointer to output section.

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

llvm-svn: 311311
2017-08-21 07:57:12 +00:00
George Rimar 7e5b0a5978 [ELF] - Don't segfault when accessing location counter inside MEMORY command.
We would previously crash on next script:
MEMORY { name : ORIGIN = .; }

Patch fixes that.

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

llvm-svn: 311073
2017-08-17 08:47:21 +00:00
George Rimar 945cd31c4b [ELF] - Linkerscript: disallow discarding COMMON.
This patch restricts following construction:

/DISCARD/ : { *(COMMON) }

Previously LLD would crash.

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

llvm-svn: 310554
2017-08-10 08:07:05 +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
Rafael Espindola a2df2f09f4 Avoid warnings when asserts are disabled.
llvm-svn: 309326
2017-07-27 21:42:42 +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 a5e8dd35b0 Simplify. NFC.
llvm-svn: 309153
2017-07-26 18:47:49 +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
George Rimar f694d33f4c [ELF] - Fix calculation of memory region offset.
This is PR33714.

Previously for each input section offset of memory region
was incremented on a size of output section.

That resulted in a wrong error message saying about
overflow. Patch fixes that.

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

llvm-svn: 308955
2017-07-25 08:29:29 +00:00
Rafael Espindola ca5740d95a Don't crash on an empty section with an ALIGN.
llvm-svn: 308809
2017-07-22 00:00:51 +00:00
Petr Hosek 8b591f592b [ELF] Remove processNonSectionCommands
This method is no longer being used anywhere.

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

llvm-svn: 308706
2017-07-21 01:02:30 +00:00
George Rimar 67c60727ce [ELF] - Apply clang-format. NFC.
llvm-svn: 308297
2017-07-18 11:55:35 +00:00
Shoaib Meenai e1d7776c53 [ELF] Use stable_sort for compareByFilePosition
The comment at the top of compareByFilePosition indicates that it relies
on stable_sort to preserve the order of synthetic sections. We were
using sort instead of stable_sort, however, leading to incorrect
synthetic section ordering.

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

llvm-svn: 308207
2017-07-17 19:42:19 +00:00
Rui Ueyama 1e77ad1456 Move feature-specific functions out of Strings.cpp.
Functions declared in Strings.h should provide generic string operations
for the linker, but some of them are too specific to some features. This
patch moves them to the location where they are used.

llvm-svn: 307949
2017-07-13 20:30:35 +00:00
George Rimar 8c804d9746 [ELF] - Allow moving location counter backward in some cases.
Patch removes restriction about moving location counter
backwards outside of output sections declarations.

That may be useful for some apps relying on such scripts, 
known example is linux kernel.

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

llvm-svn: 307794
2017-07-12 14:50:25 +00:00
Peter Smith c1ace40bf9 [ELF] Add comment to explain LinkerScript::CurAddressState [NFC]
r307367 via D34345 split out the temporary address state used within
processCommands() and assignAddresses(). Due to the way that getSymbolValue
is used by the ScriptParser there is no way of giving the current
OutputSection to getSymbolValue() without somehow accessing the created
addressState. The suggestion was that by making a pointer that would go out
of scope we would find out by ASAN/MSAN or a crash if someone had misused
currentAddressState.

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

llvm-svn: 307637
2017-07-11 09:28:27 +00:00
Peter Smith 906e9a1898 [ELF] Extract temporary state used in assignAddresses()
The assignAddresses() function accumulates state in the LinkerScript that
prevents it from being called multiple times. This change moves the state
into a separate structure AddressState that is created at the start of the
function and disposed of at the end.

CurAddressState is used rather than passing a reference to the state as a
parameter to the functions used by assignAddresses(). This is because the
getSymbolValue function needs to be executed in the context of AddressState
but it is stored in ScriptParser when AddressState is not available.

The AddressState is also used in a limited context by processCommands()

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

llvm-svn: 307367
2017-07-07 09:11:27 +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 9f9e99be20 Simplify. NFC.
llvm-svn: 307217
2017-07-05 22:30:04 +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 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
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
George Rimar d971e7030e [ELF] - Simplify allocateHeaders(). NFC.
* Return type changed to void, because it was unused.
* std::find_if -> llvm::find_if

llvm-svn: 307039
2017-07-03 16:05:51 +00:00
Andrew Ng a020d3487e [LLD][LinkerScript] Allow non-alloc sections to be assigned to segments.
This patch makes changes to allow sections without the SHF_ALLOC bit to be
assigned to segments in a linker script.

The assignment of output sections to segments is performed in
LinkerScript::createPhdrs. Previously, this function would bail as soon as it
encountered an output section which did not have the SHF_ALLOC bit set, thus
preventing any output section without SHF_ALLOC from being assigned to a
segment.

This restriction has now been removed from LinkerScript::createPhdrs and instead
a check for SHF_ALLOC has been added to LinkerScript::adjustSectionsAfterSorting
to not propagate program headers to sections without SHF_ALLOC which matches the
behaviour of bfd linker scripts.

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

llvm-svn: 307013
2017-07-03 10:11:25 +00:00
Rui Ueyama 82143d3fbb Move `assert` upwards so that it fails early if it fails.
llvm-svn: 306308
2017-06-26 17:11:36 +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
Andrew Ng 6e9f98c198 [LLD][LinkerScript] Add support for segment NONE.
This patch adds support for segment NONE in linker scripts which enables the
specification that a section should not be assigned to any segment.

Note that GNU ld does not disallow the definition of a segment named NONE, which
if defined, effectively overrides the behaviour described above. This feature
has been copied.

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

llvm-svn: 305700
2017-06-19 15:28:58 +00:00
Rafael Espindola 4f1fca270a Error when discarding .dynstr.
We would crash before.

llvm-svn: 305615
2017-06-16 23:53:36 +00:00
Rafael Espindola 656cc20f5b Error when discarding .dynsym.
We would crash instead before.

llvm-svn: 305614
2017-06-16 23:50:09 +00:00
Rafael Espindola 2af64b0bf8 Error on trying to discard .dynamic.
We would crash instead before.

llvm-svn: 305613
2017-06-16 23:45:35 +00:00
Rui Ueyama e6d8c1cb38 Update a comment.
llvm-svn: 305601
2017-06-16 22:45:13 +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 e86fddd651 Simplify. NFC.
llvm-svn: 305341
2017-06-13 23:54:34 +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 dece28087e Set non alloc section address to 0 earlier.
Currently we do layout as if non alloc sections had an actual address
and then set it to zero. This produces a few odd results where a
symbol has an address that is inconsistent with the section address.

The simplest way to fix it is probably to just set the address earlier.

The behavior of bfd seems to be similar, but it only sets the non
alloc section address is missing from the linker script or if the
script has an explicit " : 0" setting the address of the output
section (which the default script does).

llvm-svn: 305323
2017-06-13 20:57:43 +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
George Rimar fbb0463f39 [ELF] - Linkerscript: implement NOLOAD section type.
This is PR32351

Each output section may have a type. The type is a keyword in parentheses.
(https://sourceware.org/binutils/docs/ld/Output-Section-Type.html#Output-Section-Type)
This patch support only one type, it is NOLOAD.
If output section has such type, we force it to be SHT_NOBITS. 

More details are available on a review page.

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

llvm-svn: 304925
2017-06-07 16:31:08 +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 41c7ab4a3d [ELF] - Linkerscript: improved error reporting.
When linking linux kernel LLD currently reports next errors:

ld: error: unable to evaluate expression: input section .head.text has no output section assigned
ld: error: At least one side of the expression must be absolute
ld: error: At least one side of the expression must be absolute

That does not provide file/line information and overall looks unclear. 
Patch adds location information to ExprValue and that allows
to provide more clear error messages.

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

llvm-svn: 304881
2017-06-07 08:54:43 +00:00
Zachary Turner 264b5d9e88 Move Object format code to lib/BinaryFormat.
This creates a new library called BinaryFormat that has all of
the headers from llvm/Support containing structure and layout
definitions for various types of binary formats like dwarf, coff,
elf, etc as well as the code for identifying a file from its
magic.

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

llvm-svn: 304864
2017-06-07 03:48:56 +00:00
Rafael Espindola 658a0c74ec Avoid using OutputSection::Sections. NFC.
We now used the InputSectionDescriptions in
OutputSectionCommand::finalize. This will allow moving
clearOutputSections earlier.

llvm-svn: 304827
2017-06-06 20:13:19 +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
George Rimar 1124001c68 [ELF] - Early return from writeTo() if section is SHT_NOBITS. NFCi.
That addresses port commit comments for https://reviews.llvm.org/D33646

llvm-svn: 304777
2017-06-06 07:46:28 +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 bb7bd3eefe Simplify. NFC.
llvm-svn: 304511
2017-06-02 01:24:37 +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 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 05c4f67cf3 Move name lookup to script parsing time.
We were looking up sections by name during expression evaluation. By
keeping track of forward declarations we can do the lookup during
script parsing.

Doing the lookup earlier will be more efficient when assignAddresses
is run twice and removes two uses of OutputSections.

llvm-svn: 304381
2017-06-01 01:16:50 +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
Rafael Espindola d54c566510 Simplify. NFC.
llvm-svn: 304334
2017-05-31 19:53:40 +00:00
Rafael Espindola e39709b20d Check Live instead of the section type.
By the time we get here all live sections should have been combined
into InputSections.

llvm-svn: 304243
2017-05-30 20:40:03 +00:00
Rafael Espindola a85e8dda54 Remove unnecessary cast.
llvm-svn: 304240
2017-05-30 20:24:52 +00:00
George Rimar d4096140e3 [ELF] - Do not crash when linkerscript applies fill to .bss.
I found that during visual inspection of code while wrote different patch.
Script in testcase probably have nothing common with real life, but
we segfault currently using it.

If output section is known NOBITS, there is no need to create
writers threads for doing nothing or proccess any filler logic that 
is useless here. We can just early return, that is what this patch do.

DIfferential revision: https://reviews.llvm.org/D33646

llvm-svn: 304192
2017-05-30 05:48:09 +00:00
Petr Hosek 08dfd53269 [ELF] Filter out non InputSection members from InputSections
InputSections may contain MergeInputSection members which trigger
a segmentation fault when trying to cast them to InputSection.

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

llvm-svn: 304189
2017-05-30 05:17:58 +00:00
Petr Hosek 3c6de1a66c [ELF] Use late evaluation for ALIGN in expression
While the following expression is handled fine:

  PROVIDE_HIDDEN(newsym = oldsym + address);

The following expression triggers an error because the expression
is evaluated as absolute:

  PROVIDE_HIDDEN(newsym = ALIGN(oldsym, CONSTANT(MAXPAGESIZE)) + address);

To avoid this error, we use late evaluation for ALIGN by making the
alignment an attribute of the expression itself.

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

llvm-svn: 304185
2017-05-30 03:18:28 +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
Dmitry Mikulin fd0c844fbb Do not track section types of previous sections, always use PROGBITS for dummy sections.
Fix for PR33029.

llvm-svn: 303770
2017-05-24 16:48:31 +00:00
Rafael Espindola 6a1aa8d9b4 Use more strict types. NFC.
By the time we get to linker scripts, all special InputSectionBase
should have been combined into synthetic sections, which are a type of
InputSection. The net result is that we can use InputSection in a few
places that were using InputSectionBase.

llvm-svn: 303702
2017-05-23 22:47:31 +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 d7dc225888 Use a DenseMap in LinkerScript::getCmd.
This improves many-sections.s with a linker script from 22s to 0.9s.

llvm-svn: 302708
2017-05-10 19:13:38 +00:00
Rafael Espindola fa948c724a Refactor OutputSection to OutputSectionCommand mapping.
We now always use getCmd. I will optimize it in a followup commit.

llvm-svn: 302706
2017-05-10 19:00:23 +00:00
Rafael Espindola 3f38e818ac Remove one more use of section names.
llvm-svn: 302672
2017-05-10 14:35:20 +00:00
Rafael Espindola 2c923c2c37 Remove another use of section names. NFC.
llvm-svn: 302671
2017-05-10 14:28:31 +00:00
George Rimar 608cf67084 [ELF] - Don't segfault when assigning non-calculatable absolute symbol value.
This is PR32664.

Issue was revealed by linux kernel script which was:

SECTIONS {
 . = (0xffffffff80000000 + ALIGN(0x1000000, 0x200000));
 phys_startup_64 = ABSOLUTE(startup_64 - 0xffffffff80000000);

 .text : AT(ADDR(.text) - 0xffffffff80000000) {
.....
  *(.head.text)
Where startup_64 is in .head.text.

At the place of assignment to phys_startup_64 we can not calculate absolute value for startup_64
because .text section has no VA assigned. Two patches were prepared earlier to address this: D32173 and D32174.

And in comments for D32173 was suggested not try to support this case, but error out.

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

llvm-svn: 302668
2017-05-10 14:23: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 c5b612b8b4 Don't use section names in getFiller. NFC.
This is just faster and avoids using names.

llvm-svn: 302661
2017-05-10 14:01:13 +00:00
George Rimar d86a4e505b [ELF] - Linkerscript: support combination of linkerscript and --compress-debug-sections.
Previously it was impossible to use linkerscript with --compress-debug-sections 
because of assert failture:
Assertion failed: isFinalized(), file C:\llvm\lib\MC\StringTableBuilder.cpp, line 64

Patch fixes the issue

llvm-svn: 302413
2017-05-08 10:18:12 +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
Rafael Espindola 7c4eafa3ee Fix accounting of tbss.
We were correctly computing the size contribution of a .tbss input
section (it is none), but we were incorrectly considering the
alignment of the output section: it was advancing Dot instead of
ThreadBssOffset.

As far as I can tell this was always wrong in our linkerscript
implementation, but that became more visible now that the code is
shared with the non linker script case.

llvm-svn: 302107
2017-05-04 03:00:27 +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 4aa2ef5b0e Fix pr32816.
When using linkerscripts we were trying to sort SHF_LINK_ORDER
sections too early. Instead of always doing two runs of
assignAddresses, record the section order in processCommands.

llvm-svn: 301830
2017-05-01 20:32:39 +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 a7a945a13b Rename one of the variables to avoid confusion.
llvm-svn: 301691
2017-04-28 21:23:43 +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
Rafael Espindola 4f013bb3b2 Create an OutputSection for each non-empty OutputSectionCommand.
We were already pretty close, the one exception was when a name was
reused in another SECTIONS directive:

SECTIONS {
  .text : { *(.text) }
  .data : { *(.data) }
}
SECTIONS {
  .data : { *(other) }
}

In this case we would create a single .data and magically output
"other" while looking at the first OutputSectionCommand.

We now create two .data sections. This matches what gold does. If we
really want to create a single one, we should change the parser so that
the above is parsed as if the user had written

SECTIONS {
  .text : { *(.text) }
  .data : { *(.data) *(other)}
}

That is, there should be only one OutputSectionCommand for .data and
it would have two InputSectionDescriptions.

By itself this patch makes the code a bit more complicated, but is an
important step in allowing assignAddresses to operate just on the
linker script.

llvm-svn: 301484
2017-04-26 22:30:15 +00:00
Peter Smith cbfe9e946f [ELF] Always use Script::assignAddresses()
This change fabricates linker script commands for the case where there is
no linker script SECTIONS to control address assignment. This permits us
to have a single Script->assignAddresses() function.
    
There is a small change in user-visible-behavior with respect to the
handling of .tbss SHT_NOBITS, SHF_TLS as the Script->assignAddresses()
requires setDot() to be called with monotically increasing addresses.
The tls-offset.s test has been updated so that the script and non-script
results match.

This change should make the non-script behavior of lld closer to an
equivalent linker script.

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

llvm-svn: 300687
2017-04-19 12:46:32 +00:00
George Rimar 1022112d77 [ELF] - Linkerscript: make section with no content to be SHT_PROGBITS by default.
Imagine next script:

SECTIONS { BYTE(0x11); }

Section content written to disk will be 0x11. Previous LLD behavior was to make this
section SHT_NOBITS. What is not correct because section has content.
ld.bfd makes such sections SHT_PROGBITS, this patch do the same.

This fixes PR32537

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

llvm-svn: 300317
2017-04-14 09:37:00 +00:00
George Rimar 01aa795f82 [ELF] LinkerScript: Don't assign zero to all regular symbols
This fixes an assertion `Align != 0u && "Align can't be 0."'
in llvm::alignTo() when a linker script references a globally
defined variable in an ALIGN() context.

Patch by Alexander Richardson !

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

llvm-svn: 300315
2017-04-14 09:23:26 +00:00
Rafael Espindola e4c8b9b78c Simplify this further.
Thanks to Rui for noticing it.

llvm-svn: 299777
2017-04-07 16:10:46 +00:00
James Henderson 9d9a663731 [ELF] Recommit r299635 to pad x86 executable sections with 0xcc
This follows r299748 which fixed a latent bug the original commit exposed.

llvm-svn: 299755
2017-04-07 10:36:42 +00:00
Rafael Espindola 7f91d44fba Remove unnecessary cast.
llvm-svn: 299740
2017-04-07 01:25:01 +00:00
Rafael Espindola d1960dc097 Call updateAlignment before assignAddresses.
The alignment expression cannot depend on '.', so we can compute it
early.

llvm-svn: 299717
2017-04-06 21:40:22 +00:00
Rafael Espindola feed750676 Move call to findMemoryRegion before assignAddresses.
This removes a bit more work from assignAddresses.

llvm-svn: 299716
2017-04-06 21:31:24 +00:00
Rafael Espindola 1902b337e9 Remove redundant argument. NFC.
llvm-svn: 299713
2017-04-06 21:26:03 +00:00
Rafael Espindola 9b9800951d Cache the result of findSection.
This avoids calling it multiple times. In particular, we don't have to
call in in assignAddresses any more.

llvm-svn: 299709
2017-04-06 21:05:39 +00:00
James Henderson d983180778 Revert r299635 because it exposed a latent bug.
llvm-svn: 299655
2017-04-06 15:22:58 +00:00
James Henderson 8dd4c06a77 [ELF] Pad x86 executable sections with 0xcc int3 instructions
Executable sections should not be padded with zero by default. On some
architectures, 0x00 is the start of a valid instruction sequence, so can confuse
disassembly between InputSections (and indeed the start of the next InputSection
in some situations). Further, in the case of misjumps into padding, padding may
start to be executed silently.

On x86, the "0xcc" byte represents the int3 trap instruction. It is a single
byte long so can serve well as padding. This change switches x86 (and x86_64) to
use this value for padding in executable sections, if no linker script directive
overrides it. It also puts the behaviour into place making it easy to change the
behaviour of other targets when desired. I do not know the relevant instruction
sequences for trap instructions on other targets however, so somebody should add
this separately.

Because the old behaviour simply wrote padding in the whole section before
overwriting most of it, this change also modifies the padding algorithm to write
padding only where needed. This in turn has caused a small behaviour change with
regards to what values are written via Fill commands in linker scripts, bringing
it into line with ld.bfd. The fill value is now written starting from the end of
the previous block, which means that it always starts from the first byte of the
fill, whereas the old behaviour meant that the padding sometimes started mid-way
through the fill value. See the test changes for more details.

Reviewed by: ruiu

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

Bugzilla: http://bugs.llvm.org/show_bug.cgi?id=32227
llvm-svn: 299635
2017-04-06 09:29:08 +00:00
Rui Ueyama 92a5ba6da5 Remove unnecessary local variable.
This patch does what r299506 was trying to do in a different way.

llvm-svn: 299554
2017-04-05 16:07:44 +00:00
George Rimar 07d7c42c5d Revert r299506 "Simplify. NFC."
It was not NFC unfortunaly,
one of changes decrements begin() iterator and
that is not allowed by MSVS.

llvm-svn: 299525
2017-04-05 09:19:29 +00:00
Rui Ueyama 2ec34544aa Move the parser for the linker script to a separate file.
LinkerScript.cpp contains both the linker script processor and the
linker script parser. I put both into a single file, but the file grown
too large, so it's time to put them into two different files.

llvm-svn: 299515
2017-04-05 05:07:39 +00:00
Rui Ueyama fd9dafdc65 Remove unused typedef.
llvm-svn: 299514
2017-04-05 05:07:18 +00:00