Commit Graph

781 Commits

Author SHA1 Message Date
Igor Kudrin c844524e46 [ELF] Process linker scripts deeper when declaring symbols.
We should process symbols inside output section declarations the same way as top-level ones.

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

llvm-svn: 326305
2018-02-28 05:55:56 +00:00
Rafael Espindola 3f4c673d38 Put undefined symbols from shared libraries in the symbol table.
With the recent fixes these symbols have more in common than not with
regular undefined symbols.

llvm-svn: 326242
2018-02-27 20:31:22 +00:00
Igor Kudrin 3345c9ac18 [ELF] Create and export symbols provided by a linker script if they referenced by DSOs.
It should be possible to resolve undefined symbols in dynamic libraries
using symbols defined in a linker script.

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

llvm-svn: 326176
2018-02-27 07:18:07 +00:00
Rafael Espindola 79c23eec04 Keep flags from phantom synthetic sections.
This fixes pr36475.

I think this code can be simplified a bit, but I would like to check
in the more direct fix if we are in agreement on the direction and
then refactor.

This is not something that bfd does. The issue is not noticed in bfd
because it keeps fewer sections from the linkerscript in the output.

The reasons why it seems reasonable to do this:

- As George noticed, we would still keep the flags if the output
  section had both an empty synthetic section and a regular section
- We need an heuristic to find the flags of output sections. Using the
  flags of a synthetic section that would have been there seems a
  reasonable heuristic.

llvm-svn: 326137
2018-02-26 22:32:15 +00:00
Fangrui Song ffac3ed341 [ELF] Fix IsPreemptible comment and typo. NFC
llvm-svn: 325963
2018-02-23 21:57:49 +00:00
George Rimar db1a062447 [ELF] - Do not remove empty output sections that are explicitly assigned to phdr in script.
This continues direction started in D43069.

We can keep sections that are explicitly assigned to segment in script.
It helps to simplify code.

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

llvm-svn: 325887
2018-02-23 10:53:04 +00:00
George Rimar 3cdf0d969a [ELF] - Report error if removed empty output section declaration used undefined symbols.
This is for fixing PR36297.

Issue itself is that if we have SECTIONS { .bar (a+b) : { *(.stub) } };
script and no section .stub, when LLD will remove .bar, but
produce output with undefined symbols a and b.

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

llvm-svn: 325875
2018-02-23 10:15:54 +00:00
Fangrui Song 9cd5df0e15 [ELF] Add comment for preemptible and fix typo. NFC
Subscribers: emaste, arichardson, llvm-commits

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

llvm-svn: 325855
2018-02-23 02:05:48 +00:00
George Rimar 1c08e9f5ce [ELF] - Support COPY, INFO, OVERLAY output sections attributes.
This is PR36298.

(COPY), (INFO), (OVERLAY) all have the same effect:
section should be marked as non-allocatable.

(https://www.eecs.umich.edu/courses/eecs373/readings/Linker.pdf, 
3.6.8.1 Output Section Type)

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

llvm-svn: 325331
2018-02-16 10:42:58 +00:00
George Rimar 27ae7ae774 [ELF] - Make defsym to work correctly with reserved symbols.
Previously --defsym=foo2=etext+2 would produce incorrect value
for foo2 because expressions did not work correctly with
reserved symbols, section offset was calculated wrong for them.

Fixes PR35744.

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

llvm-svn: 324461
2018-02-07 09:00:34 +00:00
Rafael Espindola 22d533568b Sort orphan section if --symbol-ordering-file is given.
Before this patch orphan sections were not sorted.

llvm-svn: 323779
2018-01-30 16:20:08 +00:00
George Rimar c4ccfb5d93 [ELF] - Define linkerscript symbols early.
Currently symbols assigned or created by linkerscript are not processed early
enough. As a result it is not possible to version them or assign any other flags/properties.

Patch creates Defined symbols for -defsym and linkerscript symbols early,
so that issue from above can be addressed.

It is based on Rafael Espindola's version of D38239 patch.

Fixes PR34121.

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

llvm-svn: 323729
2018-01-30 09:04:27 +00:00
Rafael Espindola 4879864dd7 Move LMAOffset from the OutputSection to the PhdrEntry. NFC.
If two sections are in the same PT_LOAD, their relatives offsets,
virtual address and physical addresses are all the same.

I initially wanted to have a single global LMAOffset, on the
assumption that every ELF file was in practiced loaded contiguously in
both physical and virtual memory.

Unfortunately that is not the case. The linux kernel has:

  LOAD           0x200000 0xffffffff81000000 0x0000000001000000 0xced000 0xced000 R E 0x200000
  LOAD           0x1000000 0xffffffff81e00000 0x0000000001e00000 0x15f000 0x15f000 RW  0x200000
  LOAD           0x1200000 0x0000000000000000 0x0000000001f5f000 0x01b198 0x01b198 RW  0x200000
  LOAD           0x137b000 0xffffffff81f7b000 0x0000000001f7b000 0x116000 0x1ec000 RWE 0x200000

The delta for all but the third PT_LOAD is the same:
0xffffffff80000000. I think the 3rd one is a hack for implementing per
cpu data, but we can't break that.

llvm-svn: 323456
2018-01-25 19:02:08 +00:00
Rafael Espindola db9dd5b43e Improve LMARegion handling.
This fixes the crash reported at PR36083.

The issue is that we were trying to put all the sections in the same
PT_LOAD and crashing trying to write past the end of the file.

This also adds accounting for used space in LMARegion, without it all
3 PT_LOADs would have the same physical address.

llvm-svn: 323449
2018-01-25 17:42:03 +00:00
Rafael Espindola 667ffcf153 Simplify. NFC.
llvm-svn: 323440
2018-01-25 16:43:49 +00:00
Rafael Espindola 490f0a4da9 Remove MemRegionOffset. NFC.
We can just use a member variable in MemoryRegion.

llvm-svn: 323399
2018-01-25 02:18:00 +00:00
Rafael Espindola 567175f3c1 Only lookup LMARegion once. NFC.
This is similar to how we handle MemRegion.

llvm-svn: 323396
2018-01-25 01:36:36 +00:00
Rafael Espindola 9e333e976e Use lookup instead of find. NFC, just simpler.
llvm-svn: 323395
2018-01-25 01:29:15 +00:00
Rafael Espindola 75702389bd Fix incorrect physical address on self-referencing AT command.
When a section placement (AT) command references the section itself,
the physical address of the section in the ELF header was calculated
incorrectly due to alignment happening right after the location
pointer's value was captured.

The problem was diagnosed and the first version of the patch written
by Erick Reyes.

llvm-svn: 322421
2018-01-12 23:26:25 +00:00
George Rimar 5d01a8be96 [ELF] - Fix for ld.lld does not accept "AT" syntax for declaring LMA region
AT> lma_region expression allows to specify the memory region
for section load address.

Should fix PR35684.

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

llvm-svn: 322359
2018-01-12 09:07:35 +00:00
James Henderson e1689689d8 [ELF] Compress debug sections after assignAddresses and support custom layout
Previously, in r320472, I moved the calculation of section offsets and sizes
for compressed debug sections into maybeCompress, which happens before
assignAddresses, so that the compression had the required information. However,
I failed to take account of relocations that patch such sections. This had two
effects:

1. A race condition existed when a debug section referred to a different debug
section (see PR35788).
2. References to symbols in non-debug sections would be patched incorrectly.
This is because the addresses of such symbols are not calculated until after
assignAddresses (this was a partial regression caused by r320472, but they
could still have been broken before, in the event that a custom layout was used
in a linker script).

assignAddresses does not need to know about the output section size of
non-allocatable sections, because they do not affect the value of Dot. This
means that there is no longer a reason not to support custom layout of
compressed debug sections, as far as I'm aware. These two points allow for
delaying when maybeCompress can be called, removing the need for the loop I
previously added to calculate the section size, and therefore the race
condition. Furthermore, by delaying, we fix the issues of relocations getting
incorrect symbol values, because they have now all been finalized.

llvm-svn: 321986
2018-01-08 10:17:03 +00:00
Rafael Espindola 6a97f80755 Fix output section offset and contents when linker script uses memory region and data commands.
Advance the memory region offset when handling a linker script data
command such as BYTE or LONG.  Failure to advance the offset results
in corrupted output with overlapping sections.

Update tests to check for this combination of both a) memory regions
and b) data commands.

Fixes https://bugs.llvm.org/show_bug.cgi?id=35565

Patch by Owen Shaw!

llvm-svn: 321418
2017-12-24 03:46:35 +00:00
James Henderson 8d0efdd5db [ELF] Reset OutputSection size prior to processing linker script commands
The size of an OutputSection is calculated early, to aid handling of compressed
debug sections. However, subsequent to this point, unused synthetic sections are
removed. In the event that an OutputSection, from which such an InputSection is
removed, is still required (e.g. because it has a symbol assignment), and no longer
has any InputSections, dot assignments, or BYTE()-family directives, the size
member is never updated when processing the commands. If the removed InputSection
had a non-zero size (such as a .got.plt section), the section ends up with the
wrong size in the output.

The fix is to reset the OutputSection size prior to processing the linker script
commands relating to that OutputSection. This ensures that the size is correct even
in the above situation.

Additionally, to reduce the risk of developers misusing OutputSection Size and
InputSection OutSecOff, they are set to simply the number of InputSections in an
OutputSection, and the corresponding index respectively. We cannot completely
stop using them, due to SHF_LINK_ORDER sections requiring them.

Compressed debug sections also require the full size. This is now calculated in
maybeCompress for these kinds of sections.

Reviewers: ruiu, rafael

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

llvm-svn: 320472
2017-12-12 11:51:13 +00:00
Jake Ehrlich 0ca350a92d [ELF] Change default output section type to SHT_NOBITS
When an output section has no byte commands and has no input sections then it
would be ideal if the type of the section is SHT_NOBITS so that the file can
take up less space. This change sets the default type of of output sections to
SHT_NOBITS instead of SHT_PROGBITS to allow this. This required some minor test
changes (which double as tests for this new behavior) but extend-pt-load.s had
be changed in a non-trivial way. Since it seems to me that the point of the
test is to point out the consequences of how flags are assigned to output
sections that don't have input sections I changed the test to work and still
show how the memsize of the executable segment was changed.

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

llvm-svn: 320437
2017-12-11 23:25:27 +00:00
Rafael Espindola 0ab9d8b6ed Add an early return.
Total memory allocation when linking clang goes from 281.80MB to
270.96MB.

llvm-svn: 319930
2017-12-06 19:13:23 +00:00
George Rimar 78e27e830d [ELF] - Produce relocation section name consistent with output section name when --emit-reloc used with linker script.
This is for "Bug 35474 - --emit-relocs produces wrongly-named reloc sections".

LLD currently for scripts like:

.text.boot : { *(.text.boot) }
emits relocation section with name .rela.text because does not take
redefined name of output section into account and builds section name
using rules for non-scripted case. Patch fixes this oddness.

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

llvm-svn: 319526
2017-12-01 09:04:52 +00:00
Rafael Espindola de38b3d22f Handle copy relocations in symbol assignments.
When a linker script has "foo = bar" and bar is the result of a copy
relocation foo should point to the same location in .bss.

This is part of a growing evidence that copy relocations should be
implemented by using replaceSymbol to replace the SharedSymbol with a
Defined.

llvm-svn: 319449
2017-11-30 17:51:10 +00:00
Rui Ueyama 2017d52b54 Move Memory.{h,cpp} to Common.
Differential Revision: https://reviews.llvm.org/D40571

llvm-svn: 319221
2017-11-28 20:39:17 +00:00
George Rimar 80355234f7 [ELF] - Allow applying SHF_MERGE optimization for relocatable output.
This fixes PR35223.

Here I enabled SHF_MERGE section content merging for -r like
we do for regular linking.

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

llvm-svn: 318516
2017-11-17 11:27:57 +00:00
Rafael Espindola bec3765bea Remove IsLocal.
Since we always have Binding in the current symbol design IsLocal is
redundant.

llvm-svn: 318497
2017-11-17 01:37:50 +00:00
Peter Collingbourne e9a9e0a1e7 ELF: Merge DefinedRegular and Defined.
Now that DefinedRegular is the only remaining derived class of
Defined, we can merge the two classes.

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

llvm-svn: 317448
2017-11-06 04:35:31 +00:00
Rui Ueyama 5c4cb8a9e3 Inline a small function.
llvm-svn: 317428
2017-11-04 23:57:51 +00:00
Rui Ueyama aa8523e4b6 Move OutputSectionFactory to LinkerScript.cpp. NFC.
That class is used only by LinkerScript.cpp, so we should move it to
that file. Also, it no longer has to be a "factory" class. It can just
be a non-member function.

llvm-svn: 317427
2017-11-04 23:54:25 +00:00
Rui Ueyama f483da0038 Rename replaceBody -> replaceSymbol.
llvm-svn: 317383
2017-11-03 22:48:47 +00:00
Rui Ueyama f52496e1e0 Rename SymbolBody -> Symbol
Now that we have only SymbolBody as the symbol class. So, "SymbolBody"
is a bit strange name now. This is a mechanical change generated by

  perl -i -pe s/SymbolBody/Symbol/g $(git grep -l SymbolBody lld/ELF lld/COFF)

nd clang-format-diff.

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

llvm-svn: 317370
2017-11-03 21:21:47 +00:00
George Rimar 1b45d377e3 [ELF] - Cleanup of processSectionCommands().
The way we handle ONLY_IF_RO/ONLY_IF_RW constraints in
processSectionCommands is a bit tricky. If input sections
does no satisfy given constraint we remove command from
commands list. It seems too complex, what we can do instead
is to make the OutputCommand empty. So that at later steps
LLD will remove it just like it deal with other empty output commands.
That allows to simplify the loop a bit.

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

llvm-svn: 317082
2017-11-01 08:40:28 +00:00
Rui Ueyama f1f00841d9 Merge SymbolBody and Symbol into one class, SymbolBody.
SymbolBody and Symbol were separated classes due to a historical reason.
Symbol used to be a pointer to a SymbolBody, and the relationship
between Symbol and SymbolBody was n:1.

r2681780 changed that. Since that patch, SymbolBody and Symbol are
allocated next to each other to improve memory locality, and they have
1:1 relationship now. So, the separation of Symbol and SymbolBody no
longer makes sense.

This patch merges them into one class. In order to avoid updating too
many places, I chose SymbolBody as a unified name. I'll rename it Symbol
in a follow-up patch.

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

llvm-svn: 317006
2017-10-31 16:07:41 +00:00
George Rimar f9b04fd91f [ELF] - Simplify output section creation.
When there is no SECTION commands given, all sections are
technically orphans, but now we handle script orphans sections
and regular "orphans" sections for non-scripted case differently,
though we can handle them at one place.

Patch do that change.

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

llvm-svn: 316984
2017-10-31 10:31:58 +00:00
George Rimar 96b1157814 [ELF] - Simplify reporting of garbage collected sections.
This moves reporting of garbage collected sections right after
we do GC. That simplifies things.

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

llvm-svn: 316759
2017-10-27 11:32:22 +00:00
Rui Ueyama 8e38ea8b9e Unassign sections if they are "assigned" to /DISCARD/.
"/DISCARD/" is a special section name in the linker script to discard
input sections. Previously, we handled it as if it were a real section,
so an input section can be assigned but dead. However, allowing sections
to be

 - assigned and alive (will be emitted),
 - not assigned and dead (removed), or
 - assigned but dead (???)

feels logically wrong and practically error-prone. This patch removes
the last combination of the states.

llvm-svn: 316622
2017-10-25 22:03:21 +00:00
George Rimar 9814d15136 [ELF] - Implement --orphan-handling option.
It is PR34946.

Spec (http://man7.org/linux/man-pages/man1/ld.1.html) tells about
--orphan-handling=MODE, option where MODE can be one of four:
"place", "discard", "warn", "error".
Currently we already report orphans when -verbose given,
what becomes excessive with option implemented.

Patch stops reporting orphans when -versbose is given,
and support "place", "warn" and "error" modes.
It is not yet clear that "discard" mode is useful so it is not supported.

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

llvm-svn: 316583
2017-10-25 15:20:30 +00:00
Rafael Espindola 089dac7be1 Make Ctx a plain pointer again.
If a struct has a std::unique_ptr member, the logical interpretation
is that that member will be destroyed with the struct.

That is not the case for Ctx. It is has to be deleted earlier and its
lifetime is defined by the functions where the AddressState is
created.

llvm-svn: 316378
2017-10-23 21:12:19 +00:00
Rafael Espindola 849d499e6d Don't call buildSectionOrder multiple times.
This takes linking the linux kernel from 1.52s to 0.58s.

llvm-svn: 316251
2017-10-21 00:05:01 +00:00
Rafael Espindola 3558e24ae8 Remove unused argument.
llvm-svn: 316248
2017-10-20 23:28:19 +00:00
Bob Haarman 4f5c8c29ac [lld] Move Threads to Common
Summary:
This will allow using the functionality from other linkers. It is also
a prerequisite for sharing the error logging code.

Reviewers: ruiu

Reviewed By: ruiu

Subscribers: emaste, mgorny, llvm-commits

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

llvm-svn: 315725
2017-10-13 18:22:55 +00:00
George Rimar 26fa916deb [ELF] - Do not set output section flags except SHF_{ALLOC,WRITE,EXECINSTR}.
This is PR34546.

Currently LLD creates output sections even if it has no input sections,
but its command contains an assignment.
Committed code just assigns the same flag that was used in previous
live section.
That does not work sometimes. For example if we have following script:

.ARM.exidx : { *(.ARM.exidx*) }
.foo : { _foo = 0; } }
Then first section has SHF_LINK_ORDER flag. But section foo should not.
That was a reason of crash in OutputSection::finalize(). LLD tried to calculate
Link value, calling front() on empty input sections list.
We should only keep access flags and omit all others when creating such sections.

Patch fixes the crash observed.

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

llvm-svn: 315441
2017-10-11 08:13:40 +00:00
Rui Ueyama 7ad1e3102a Split LinkerScript::computeInputSections into two functions.
llvm-svn: 315434
2017-10-11 04:50:30 +00:00
Rui Ueyama 722221f5a7 Swap parameters of getSymbolValue.
Usually, a function that does symbol lookup takes symbol name as
its first argument. Also, if a function takes a source location hint,
it is usually the last parameter. So the previous parameter order
was counter-intuitive.

llvm-svn: 315433
2017-10-11 04:34:34 +00:00
Rui Ueyama f5733500c9 Do not handle DefinedCommon in linker scripts.
Because of r314495, DefinedCommon symbols cannot reach to
getSymbolValue function. When they reach the fucntion, they have
already been converted to DefinedRegular symbols.

llvm-svn: 315432
2017-10-11 04:31:13 +00:00
Rui Ueyama f0403c601a Rename BytesDataCommand -> ByteCommand.
llvm-svn: 315431
2017-10-11 04:22:09 +00:00
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