Commit Graph

486 Commits

Author SHA1 Message Date
George Rimar 0c1c8085bc [ELF] - Move ThreadBssOffset and Dot to LinkerScriptBase. NFC.
One more step to combine LinkerScript and LinkerScriptBase.

llvm-svn: 297722
2017-03-14 10:00:19 +00:00
George Rimar e21c3af7e9 [ELF] - Remove unnecessary template #4. NFC.
OutputSectionFactory has no ELFT templates anymore.

llvm-svn: 297720
2017-03-14 09:30:25 +00:00
George Rimar 2d2621090d [ELF] - Step to combine LinkerScript and LinkerScriptBase
We can move all not templated functionality to LinkerScriptBase.
Patch do that for hasPhdrsCommands() and shows how it helps to detemplate
things in other places.

Probably we should be able to merge these 2 classes into single one after such steps.
Even if not, it still looks as reasonable cleanup for me.

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

llvm-svn: 297714
2017-03-14 09:03:53 +00:00
Eugene Leviant 5784e96f5c [ELF] Fix LMA offset calculation
Differential revision: https://reviews.llvm.org/D30832

llvm-svn: 297713
2017-03-14 08:57:09 +00:00
George Rimar 78aa270041 [ELF] - Remove unnecessary template. NFC.
llvm-svn: 297622
2017-03-13 14:40:58 +00:00
Rafael Espindola 4595df94bb Don't pass Dot to every callback.
It is available from ScriptBase.

llvm-svn: 297472
2017-03-10 16:04:26 +00:00
Rafael Espindola 9bd4566dac Use SectionBase for linker script expressions.
This is a small step for fixing pr32031, which needs expressions that
point to input sections.

llvm-svn: 297431
2017-03-10 00:47:33 +00:00
Rui Ueyama f5fce48679 Handle ":" as a regular token character in linker scripts.
This is an alternative to https://reviews.llvm.org/D30500 to simplify the
version definition parser and allow ":" in symbol names.

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

llvm-svn: 297402
2017-03-09 19:23:00 +00:00
Rafael Espindola 5616adf655 Remove DefinedSynthetic.
With this we have a single section hierarchy. It is a bit less code,
but the main advantage will be in a future patch being able to handle

foo = symbol_in_obj;

in a linker script. Currently that fails since we try to find the
output section of symbol_in_obj.  With this we should be able to just
return an InputSection from the expression.

llvm-svn: 297313
2017-03-08 22:36:28 +00:00
Rafael Espindola 76b6bd355d Remove unnecessary template. NFC.
llvm-svn: 297287
2017-03-08 15:44:30 +00:00
Rafael Espindola 3770763cda Rename Addralign to Alignment.
It now matches the name used in InputSectionBase.

llvm-svn: 297144
2017-03-07 14:55:52 +00:00
Rafael Espindola bedccb5e78 Simplify. NFC.
llvm-svn: 296620
2017-03-01 14:21:31 +00:00
Rafael Espindola bd12e2a0ce Simplify. NFC.
llvm-svn: 296619
2017-03-01 14:12:21 +00:00
Rui Ueyama 80474a26b9 De-template DefinedRegular.
Differential Revision: https://reviews.llvm.org/D30348

llvm-svn: 296508
2017-02-28 19:29:55 +00:00
Rui Ueyama 968db48cee Move SymbolTableSection::getOutputSection to SymbolBody::getOutputSection.
That function doesn't use any member of SymbolTableSection, so I
couldn't see a reason to make it a member of that class. The function
takes a SymbolBody, so it is more natural to make it a member of
SymbolBody.

llvm-svn: 296433
2017-02-28 04:02:42 +00:00
Rui Ueyama 9320cb0719 De-template SyntheticSection.
This class didn't use ELFT.

llvm-svn: 296313
2017-02-27 02:56:02 +00:00
Rui Ueyama 536a26706f Move SymbolTable<ELFT>::Sections out of the class.
The list of all input sections was defined in SymbolTable class for a
historical reason. The list itself is not a template. However, because
SymbolTable class is a template, we needed to pass around ELFT to access
the list. This patch moves the list out of the class so that it doesn't
need ELFT.

llvm-svn: 296309
2017-02-27 02:32:08 +00:00
Rui Ueyama 02a036f2e6 De-template OutputSectionFactory.
Since OutputSection is no longer a template, it doesn't make much
sense to tempalte its factory class.

llvm-svn: 296308
2017-02-27 02:31:48 +00:00
Rui Ueyama 9d1bacb1b4 Remove useless template so that Out<ELFT> becomes just Out.
llvm-svn: 296307
2017-02-27 02:31:26 +00:00
Rafael Espindola 24e6f363c5 Merge OutputSectionBase and OutputSection. NFC.
Now that all special sections are SyntheticSections, we only need one
OutputSection class.

llvm-svn: 296127
2017-02-24 15:07:30 +00:00
Rafael Espindola 29c1afb880 Delete trivial setter.
llvm-svn: 296124
2017-02-24 14:34:12 +00:00
Rafael Espindola 40849419e2 Delete trivial getter.
llvm-svn: 296123
2017-02-24 14:28:00 +00:00
Rafael Espindola 774ea7d0a9 Make InputSection a class. NFC.
With the current design an InputSection is basically anything that
goes directly in a OutputSection. That includes plain input section
but also synthetic sections, so this should probably not be a
template.

llvm-svn: 295993
2017-02-23 16:49:07 +00:00
George Rimar 2146787609 [ELF] - Refactoring of LMA offset handling code. NFC.
Thanks to Rui Ueyama for suggestion.

llvm-svn: 295943
2017-02-23 07:57:55 +00:00
Rafael Espindola c404d50d7c Merge InputSectionData and InputSectionBase.
Now that InputSectionBase is not a template there is no reason to have
the two.

llvm-svn: 295924
2017-02-23 02:32:18 +00:00
Rafael Espindola b4c9b81aad Convert InputSectionBase to a class.
Removing this template is not a big win by itself, but opens the way
for removing more templates.

llvm-svn: 295923
2017-02-23 02:28:28 +00:00
Petr Hosek 5e51f7d24e [ELF] Insert linkerscript symbols directly into symbol table
This change exposes the symbol table insert method and uses it to
insert the linkerscript defined symbols directly into the symbol
table to avoid unnecessarily pulling the object out of an archive.

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

llvm-svn: 295780
2017-02-21 22:32:51 +00:00
George Rimar 78ef645f94 [ELF] - Do not segfault when using --gc-sections with linker script
Patch fixes PR32024.

Sections that were not marked as Live has null output section.
Previously we tried to access that field and segfaulted.

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

llvm-svn: 295727
2017-02-21 15:46:43 +00:00
George Rimar ae4761c186 [ELF] - Postpone evaluation of LMA offset.
Previously we evaluated the values of LMA incorrectly for next cases:

.text : AT(ADDR(.text) - 0xffffffff80000000) { ... }
.data : AT(ADDR(.data) - 0xffffffff80000000) { ... }
.init.begin : AT(ADDR(.init.begin) - 0xffffffff80000000) { ... }

Reason was that we evaluated offset when VA was not assigned. For case above
we ended up with 3 loads that has similar LMA and it was incorrect.
That is critical for linux kernel.

Patch updates the offset after VA calculation. That fixes the issue.

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

llvm-svn: 295722
2017-02-21 15:08:18 +00:00
George Rimar 2ee2d2dcb5 [ELF] - Improve diagnostic messages for move location counter errors.
Previously LLD would error out just "ld.lld: error: unable to move location counter backward"
What does not really reveal the place of issue,
Patch adds location to the output.

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

llvm-svn: 295720
2017-02-21 14:50:38 +00:00
George Rimar 60f1fe8438 [ELF] - Make ASSERT() return Dot instead of evaluated value.
Previously ASSERT we implemented returned expression value.
Ex:
. = ASSERT(0x100);
would set Dot value to 0x100

Form of assert when it is assigned to Dot was implemented for 
compatibility with very old GNU ld which required it.
Some scripts in the wild, including linux kernel scripts 
use such ASSERTs at the end for doing different checks.

Currently we fail with "unable to move location counter backward"
for such scripts. Patch changes ASSERT to return location counter 
value to fix that.

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

llvm-svn: 295703
2017-02-21 07:33:38 +00:00
Rafael Espindola 3773bcac55 Fix --print-gc-sections with linker scripts.
Before it would never print anything.

Thanks to George Rimar for pointing it out.

llvm-svn: 295485
2017-02-17 19:37:30 +00:00
George Rimar 647c1685b6 [ELF] - Move DependentSections vector from InputSection to InputSectionBase
I splitted it from D29273.
Since we plan to make relocatable sections as dependent for target ones for
--emit-relocs implementation, this change is required to support .eh_frame case.

EhInputSection inherets from InputSectionBase and not from InputSection.
So for case when it has relocation section, it should be able to access DependentSections
vector.

This case is real for Linux kernel.

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

llvm-svn: 295483
2017-02-17 19:34:05 +00:00
Rafael Espindola ecbfd871f9 Don't print DISCARD sections as gced.
This is a small difference I noticed to gold and bfd. When given
--print-gc-sections, we print sections a linkerscript marks
DISCARD. The other linkers don't.

llvm-svn: 295467
2017-02-17 17:35:07 +00:00
Rafael Espindola 679828ff92 Diagnose another case of the location counter moving backwards.
This case should be possible to handle, but it is hard:

* In order to create program headers correctly, we have to scan the
  sections in the order they are in the file.

* To find that order, we have to "execute" the linker script.

* The linker script can contain SIZEOF_HEADERS.

So to support this we have to start with a guess of how many headers
we need (3), run the linker script and try to create the program
headers. If it turns out we need more headers, we run the script again
with a larger SIZEOF_HEADERS.

Also, running the linker script depends on knowing the size of the
sections, so we have to finalize them. But creating the program
headers can change the value stored in some sections, so we have to
split size finalization and content finalization.

Looks like the last part is also needed for range extension thunks, so
we might support this at some point. For now just report an error
instead of producing broken files.

llvm-svn: 295458
2017-02-17 16:26:13 +00:00
Rafael Espindola 4cd7352c4f Reject moving the location counter backwards.
We were only checking when the assignment was inside a section.

llvm-svn: 295454
2017-02-17 16:01:51 +00:00
Rafael Espindola 8290274c13 Share more output section creation code.
We can do this now that the linker script and the writer agree on
which sections should be combined.

llvm-svn: 295341
2017-02-16 17:32:26 +00:00
George Rimar 505ac8dc41 [ELF] - Do not crash when discarding sections that are referenced by others.
SHF_LINK_ORDER sections adds special ordering requirements.
Such sections references other sections. Previously we would crash
if section that other were referenced to was discarded by script.

Patch fixes that by discarding all dependent sections in that case.
It supports chained dependencies, testcase is provided.

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

llvm-svn: 295332
2017-02-16 16:06:13 +00:00
Rafael Espindola 908a3d3420 Ignore relocation sections in linker scripts.
Unfortunately, the common way of writing linker scripts seems to be
to get the output of ld.bfd --verbose and edit it a bit.

Also unfortunately, the bfd default script contains things like

.rela.dyn : { *(... .rela.data ...) }

but bfd actually ignores that for -emit-relocs, so we have to do the
same.

llvm-svn: 295324
2017-02-16 14:36:09 +00:00
Rui Ueyama 731a66ae98 Apply different tokenization rules to linker script expressions.
The linker script lexer is context-sensitive. In the regular context,
arithmetic operator characters are regular characters, but in the
expression context, they are independent tokens. This afects how the
lexer tokenizes "3*4", for example. (This kind of expression is real;
the Linux kernel uses it.)

This patch defines function `maybeSplitExpr`. This function splits the
current token into multiple expression tokens if the lexer is in the
expression context.

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

llvm-svn: 295225
2017-02-15 19:58:17 +00:00
Rui Ueyama 794366a237 Rename ScriptParser.{cpp,h} -> ScriptLexer.{cpp,h}.
These files contain a lexer, so the new names are better.
The parser is in LinkerScript.{cpp,h}.

llvm-svn: 295022
2017-02-14 04:47:05 +00:00
Peter Collingbourne 904c5ed558 ELF: Read all dynamic lists specified on the command line.
We were previously only reading the last one.

Fixes PR31939 (which was likely the issue underlying
https://github.com/google/oss-fuzz/issues/295).

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

llvm-svn: 294977
2017-02-13 18:31:12 +00:00
Rafael Espindola ea590d91a0 Revert "Simplify symbol computation for non alloc sections."
This reverts commit r294346. Looks like it regressed the build of
magenta.

llvm-svn: 294460
2017-02-08 15:19:03 +00:00
George Rimar 4e01c3e8cd [ELF] - Linkerscript - fix handling of OUTPUT_ARCH command.
OUTPUT_ARCH command can contain architecture values separated with ":", like:
OUTPUT_ARCH(i386:x86-64)

We did not support that, because got 3 lexer tokens here after recent changes.

This trivial patch fixes the issue, now whole expression inside 
OUTPUT_ARCH is just ignored.

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

llvm-svn: 294432
2017-02-08 09:59:06 +00:00
Petr Hosek 165088aa5c [ELF] Handle output section alignment in linker scripts
LLD already parses ALIGN expression to specifiy alignment for output
sections in linker scripts but it never applies the alignment to the
output section. This change handles that.

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

llvm-svn: 294374
2017-02-07 23:42:31 +00:00
Rafael Espindola 193b158b39 Simplify symbol computation for non alloc sections.
We now just keep the address the section would have if it was
allocatable. Only the writer ignores it at the very end.

llvm-svn: 294346
2017-02-07 20:22:04 +00:00
Dmitry Mikulin f3965c0246 Handle the case where 'local' is the name of a global in a version script:
{ global : local; local: *; };

llvm-svn: 294343
2017-02-07 19:50:47 +00:00
George Rimar c6cf1f1f02 [ELF] - Assign proper values for DefinedSynthetic symbols attached to non-allocatable sections.
DefinedSynthetic symbols are attached to sections,
for the case when such symbol was attached to non-allocated section,
we calculated its value incorrectly.

We subtracted Body->Section->Addr, but non-allocatable sections
should have zero VA in output and therefore result value was wrong.

And at the same time we have Body->Section->Addr != 0 for them 
internally because  use it for calculation of section size.

Patch fixes calculation of such symbols values.

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

llvm-svn: 294322
2017-02-07 17:51:35 +00:00
George Rimar b2b70975e0 [ELF] - Refactoring: reuse similar method.
We had assignSymbol and assignSectionSymbol methods which has similar functionality.
Patch removes one of copy and reuses another in code.

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

llvm-svn: 294290
2017-02-07 10:23:28 +00:00
Rafael Espindola 06f4743a48 Handle symbol assignments before the first section switch.
We now create a dummy section with index 1 before processing the
linker script.

Thanks to George Rimar for finding the bug and providing the initial
testcase.

llvm-svn: 294252
2017-02-06 22:21:46 +00:00