Commit Graph

656 Commits

Author SHA1 Message Date
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