Commit Graph

353 Commits

Author SHA1 Message Date
Rui Ueyama f34d0e0875 Allocate LayoutInputSections using SpecificBumpPtrAllocator.
llvm-svn: 278453
2016-08-12 01:24:53 +00:00
Rui Ueyama 2de509c370 Remove OutputSectionBuilder::finalize.
The reason why we had to assign offsets only to sections that
don't contain layout sections were unclear. It turned out that
we can live without it.

llvm-svn: 278449
2016-08-12 00:55:08 +00:00
Rui Ueyama e5cc668eb8 Remove useless local variable.
llvm-svn: 278446
2016-08-12 00:36:56 +00:00
Rui Ueyama 48c3f1cebc Split LinkerScript<ELFT>::createSections.
Also avoid to use a lambda that is called only once.

llvm-svn: 278445
2016-08-12 00:27:23 +00:00
Rui Ueyama 1602421c85 Replace a variadic forwarding template function with less complex ones.
llvm-svn: 278439
2016-08-11 23:22:52 +00:00
Rui Ueyama 2c3f501052 Make class hierarchy more shallow.
LayoutInputSection is a dummy class and does not need any features
that the actual implementation (InputSection class) provides.

llvm-svn: 278430
2016-08-11 22:06:55 +00:00
Rui Ueyama f4a30a5d89 Remove trailing whitespace.
llvm-svn: 278420
2016-08-11 21:30:42 +00:00
Rui Ueyama dd81fe3156 Add comments.
llvm-svn: 278408
2016-08-11 21:00:02 +00:00
Eugene Leviant ceabe80e97 [ELF] Symbol assignment within output section description
llvm-svn: 278322
2016-08-11 07:56:43 +00:00
George Rimar e32a3598bc [ELF] - Linkerscript: implement SIZEOF_HEADERS.
SIZEOF_HEADERS - Return the size in bytes of the output file’s headers.

It is is a feature used in FreeBsd script, for example.
There is a discussion on PR28688 page about it.

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

llvm-svn: 278204
2016-08-10 07:59:34 +00:00
Rui Ueyama edc521f192 Remove unreachable `return`. NFC.
llvm-svn: 278096
2016-08-09 04:50:26 +00:00
Rui Ueyama 1034c9e342 Remove isOutputDynamic and use Out<ELFT>::DynSymTab instead.
This patch is to not instantiate DynSymTab and DynStrTab if the
output is not a dynamic output.

llvm-svn: 278095
2016-08-09 04:42:01 +00:00
Rui Ueyama a2acc93140 Simplify. NFC.
llvm-svn: 277794
2016-08-05 01:25:45 +00:00
Rui Ueyama 808d13ea49 Move invariants outside of a lambda. NFC.
llvm-svn: 277791
2016-08-05 01:05:01 +00:00
Rui Ueyama 36c1cd235a Make combine() non-member function.
Because this function depends only on its arguments.

llvm-svn: 277790
2016-08-05 01:04:59 +00:00
Rui Ueyama 742c38361b Split InputSectionDescription::Sort into SortInner and SortOuter.
Summary:
The comparator function to compare input sections as instructed by
SORT command was a bit too complicated because it needed to handle
four different cases. This patch split it into two function calls.

This patch also simplifies the parser.

Reviewers: grimar

Subscribers: llvm-commits

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

llvm-svn: 277780
2016-08-04 22:27:00 +00:00
Rafael Espindola a4b41dca31 Remove redundant argument.
But always set Script<ELFT>::X->OutputSections.

llvm-svn: 277720
2016-08-04 12:13:05 +00:00
George Rimar eefa758ee2 [ELF] - Linkerscript: implemented ASSERT() keyword.
ASSERT(exp, message)
Ensure that exp is non-zero. If it is zero, then exit the linker with an error
code, and print message.

ASSERT is useful and was seen in few projects in the wild.

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

llvm-svn: 277710
2016-08-04 09:29:31 +00:00
George Rimar 9e5386ceae [ELF] - Linkerscript: Fixed SORT_BY_ALIGNMENT sorting order.
According to spec:
"SORT_BY_ALIGNMENT will sort sections into descending order by 
alignment before placing them in the output file"

Previously they were sorted into ascending order.

llvm-svn: 277706
2016-08-04 08:56:17 +00:00
Eugene Leviant c7611fc567 [ELF] Linkerscript: remove repeated sections in filter()
llvm-svn: 277703
2016-08-04 08:20:23 +00:00
Rui Ueyama c163318b21 Remove buggy PROVIDE-in-output-description command.
With the previous change, it is now obvious that readProvide in
this context appended new commands to a wrong command list.
It was mistakenly adding new commands to the top level.
Thus, all commands inside output section descriptions were
interpreted as they were written on top level.

PROVIDE command naturally requires symbol assignment support
in the output section description. We don't have that one yet.
I removed the implementation because there's no way to fix it now.
We can resurrect the test once we support the symbol assignment
(with a modification to detect errors that we failed to find as
described.)

llvm-svn: 277687
2016-08-04 02:03:29 +00:00
Rui Ueyama 104165643e Make ScriptParser::read* functions more functional style.
Previously, many read* functions created new command objects and
add them directly to the top-level data structure. This is not
work for some commands because some commands, such as the assignment,
can appear inside and outside of the output section description.

This patch is to not append objects to the top-level data structure.
Callers are now responsible to do that.

llvm-svn: 277686
2016-08-04 02:03:27 +00:00
Rui Ueyama 965827d674 Make filler expression compatible with gold.
Previously, a decimal filler expression is interpreted as a byte value.
Gold on the other hand use it as a 32-bit big-endian value.
This patch fixes the compatibility issue.

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

llvm-svn: 277680
2016-08-03 23:25:15 +00:00
Rui Ueyama e7f912cd8f Create only one vector instead of two.
In this for-loop, we append elements from one vector to another,
which is a bit inefficient.

llvm-svn: 277653
2016-08-03 21:12:09 +00:00
Nico Weber 2e36772caf Revert 277594, it caused PR28827
llvm-svn: 277599
2016-08-03 14:37:57 +00:00
Rafael Espindola a348532ec3 Remove redundant argument.
llvm-svn: 277594
2016-08-03 13:21:16 +00:00
George Rimar 350ece4efb [ELF] - Linkerscript: support all kinds of sorting (including nested).
Previously we supported only sorting by name.

When there are nested section sorting commands in linker script, there can be at most 1
level of nesting for section sorting commands.

SORT_BY_NAME (SORT_BY_ALIGNMENT (wildcard section pattern)). It will sort the input
sections by name first, then by alignment if 2 sections have the same name.

SORT_BY_ALIGNMENT (SORT_BY_NAME (wildcard section pattern)). It will sort the input
sections by alignment first, then by name if 2 sections have the same alignment.

SORT_BY_NAME (SORT_BY_NAME (wildcard section pattern)) is treated the same as SORT_
BY_NAME (wildcard section pattern).

SORT_BY_ALIGNMENT (SORT_BY_ALIGNMENT (wildcard section pattern)) is treated the
same as SORT_BY_ALIGNMENT (wildcard section pattern).

All other nested section sorting commands are invalid.

Patch implements that all above.

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

llvm-svn: 277583
2016-08-03 08:35:59 +00:00
Rafael Espindola eb685cd798 Support expressions inside FLAGS.
This is an undocumented bfd feature. It is reasonable for making the
scripts a bit more readable.

llvm-svn: 277532
2016-08-02 22:14:57 +00:00
Davide Italiano 5ac0d7c5ad [LinkerScript] Filler can have a decimal value.
llvm-svn: 277222
2016-07-29 22:21:28 +00:00
George Rimar 9e69450ec7 [ELF] - Linkerscript: implemented SIZEOF(section)
SIZEOF(section)
Return the size in bytes of the named section, if that section has been allocated.

SIZEOF(section) often used in scripts. Few examples from the wild:
https://github.com/chipKIT32/pic32-Arduino-USB-Bootloader-original/blob/master/boot-linkerscript.ld
https://github.com/devkitPro/buildscripts/blob/master/dkarm-eabi/crtls/gba_cart.ld

Patch implements it.

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

llvm-svn: 277165
2016-07-29 16:18:47 +00:00
George Rimar 0702c4e86e [ELF] - Linkerscript: Implemented SORT command.
When the SORT keyword is used, the linker will sort the files or sections into ascending order by name before placing them in the output file.
It is used in FreeBSD script:
https://svnweb.freebsd.org/base/head/sys/conf/ldscript.amd64?revision=284870&view=markup#l139

This is PR28689.

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

llvm-svn: 277153
2016-07-29 15:32:46 +00:00
George Rimar c3cb884c8e [ELF] - Linkerscript: make addSection() global function instead lambda. NFC.
llvm-svn: 277150
2016-07-29 15:12:48 +00:00
George Rimar eaee2af51e [ELF] - Update comment. NFC.
llvm-svn: 277147
2016-07-29 15:07:11 +00:00
Rui Ueyama ed94271350 Remove `continue` at end of a for-loop.
llvm-svn: 277122
2016-07-29 06:21:06 +00:00
Rui Ueyama f71caa2b49 Split readOutputSectionDescription.
llvm-svn: 277121
2016-07-29 06:14:07 +00:00
Rui Ueyama 8d083e6a0a Remove `Ignore` flag from SymbolAssignment class.
Previously, Ignore flag is set if we don't want to assign
a value to symbols. It happens if a symbol assingment is in
PROVIDE() and there's already a symbol with the same name.

The previous code had a subtle but that we assume that the
existing symbol is an absolute symbol even if it is not.
This patch fixes the issue by always overwriting an absolute
symbol.

llvm-svn: 277115
2016-07-29 05:48:39 +00:00
Rui Ueyama 3de0a3308b Rename DoLayout -> HasContents. NFC.
llvm-svn: 277102
2016-07-29 03:31:09 +00:00
Rui Ueyama 174e0a16f9 Simplify. readAssignment never returns a nullptr.
llvm-svn: 277090
2016-07-29 00:29:25 +00:00
George Rimar f586ff7ec9 [ELF] - Removed excessive check. NFC.
Thanks to Rui Ueyama who noticed that.

llvm-svn: 277062
2016-07-28 22:15:44 +00:00
George Rimar 352eac37a1 [ELF] - Attempt to fix BB after 277042.
http://lab.llvm.org:8011/builders/llvm-clang-lld-x86_64-scei-ps4-ubuntu-fast/builds/17294
Change:
std::make_unique -> llvm::make_unique

llvm-svn: 277059
2016-07-28 22:10:50 +00:00
George Rimar 8c16d52893 [ELF] - Cosmetic change. NFC.
peek()[0] == '*' changed to peek().startswith("*")

llvm-svn: 277043
2016-07-28 22:01:56 +00:00
George Rimar 0659800ef0 [ELF] - Linkerscript: implemented filename specification.
Scripts can contain something like:
KEEP (*crtbegin.o(.ctors))

What means that "*crtbegin.o" is a wildcard of file to take the sections from.
This is some kind of opposite to EXCLUDE_FILE and used in FreeBSD script:
https://svnweb.freebsd.org/base/head/sys/conf/ldscript.amd64?revision=284870&view=markup#l122

Patch implements this.

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

llvm-svn: 277042
2016-07-28 21:51:30 +00:00
George Rimar 30835ea47a [ELF] - Linkerscript: implemented += operator.
Sometimes += is used to move the location counter.
Example from the wild is:

.dbg_excpt _DBG_EXCPT_ADDR (NOLOAD) :
{
  . += (DEFINED (_DEBUGGER) ? 0x8 : 0x0);
https://github.com/chipKIT32/pic32-Arduino-USB-Bootloader-original/blob/master/boot-linkerscript.ld

Patch implements it and opens way for others type of assignments (-= *= etc), though I think only += is
actual to support.

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

llvm-svn: 277035
2016-07-28 21:08:56 +00:00
Rui Ueyama ad10c3d8d4 Make CommonInputSection singleton class.
All other singleton instances are accessible globally.
CommonInputSection shouldn't be an exception.

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

llvm-svn: 277034
2016-07-28 21:05:04 +00:00
Eugene Leviant 3e6b027705 [ELF] Allows setting section for common symbols in linker script
llvm-svn: 277023
2016-07-28 19:24:13 +00:00
Rafael Espindola 54c145ce0e Add support for SEGMENT_START.
This is a bit of an odd feature. It is normally used in

. = SEGMENT_START(seg, val);

In bfd it evaluates to val or to the value of the corresponding
-T<seg>-segment. Note that the -T<seg>-segment in bfd doesn't actually
change the segment address, just the value this evaluates too,
including in the default linker script.

In gold the -T<seg>-segment options do change the segment address and
seeing this expressions in linker scripts disables the options.

For new this just always evaluates the expression to val.

llvm-svn: 277014
2016-07-28 18:16:24 +00:00
George Rimar 03fc010e10 [ELF] - Linkerscript: ignore SORT(CONSTRUCTORS)
Some scripts can contain SORT(CONSTRUCTORS) expression:
https://svnweb.freebsd.org/base/head/sys/conf/ldscript.amd64?revision=284870&view=markup#l152

for ELF it just a nop:
"When linking object file formats which do not support arbitrary sections, such as ECOFF and XCOFF, the linker will automatically recognize C++ global constructors and destructors by name. For these object file formats, the CONSTRUCTORS command tells the linker to place constructor information in the output section where the CONSTRUCTORS command appears. The CONSTRUCTORS command is ignored for other object file formats."
(http://www.sourceware.org/binutils/docs-2.10/ld_3.html)

So patch implements ignoring.

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

llvm-svn: 276965
2016-07-28 07:18:23 +00:00
Rafael Espindola 0b113671c5 Make toPhdrFlags a member function. NFC.
llvm-svn: 276868
2016-07-27 14:10:56 +00:00
Davide Italiano e7282797aa [ELF/LinkerScript] Support EXCLUDE_FILE inside KEEP.
Differential Revision:	https://reviews.llvm.org/D22795

llvm-svn: 276825
2016-07-27 01:44:01 +00:00
Rui Ueyama f7791bb9c6 Remove return type that can trivially be inferred.
llvm-svn: 276794
2016-07-26 19:34:10 +00:00
George Rimar b567b628b7 [ELF] - replace error() with llvm_unreachable.
llvm-svn: 276790
2016-07-26 18:46:13 +00:00
George Rimar 6930a6dc82 Attemp to fix build bot:
http://lab.llvm.org:8011/builders/lld-x86_64-darwin13/builds/25329/steps/build_Lld

llvm-svn: 276789
2016-07-26 18:41:06 +00:00
George Rimar a9c5a52846 [ELF] Linkerscript: symbol assignments with indentifiers on the right side of expression.
In symbol assignments symbol may appear on the right-hand side of the expression:
(https://svnweb.freebsd.org/base/head/sys/conf/ldscript.amd64?revision=284870&view=markup#l8)

kernphys = CONSTANT (MAXPAGESIZE);
 . = kernbase + kernphys + SIZEOF_HEADERS;

Patch implements that.

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

llvm-svn: 276784
2016-07-26 18:18:58 +00:00
George Rimar 630c617912 [ELF] - Linkerscript: implemented ALIGN modificatior of output sections.
Output section description can contain ALIGN modificator:
https://sourceware.org/binutils/docs/ld/Output-Section-Description.html#Output-Section-Description

Patch implements it.

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

llvm-svn: 276780
2016-07-26 18:06:29 +00:00
George Rimar 276b4e6428 [ELF] Linkerscript: implement DATA_SEGMENT_RELRO_END.
In compare with what GNU linkers do (https://sourceware.org/binutils/docs/ld/Builtin-Functions.html),
this implementation simple:

Do not touch DATA_SEGMENT_ALIGN, it do what it do now - just aligns to the page boundary.
Parameters of DATA_SEGMENT_RELRO_END is ignored. That should be correct as it is usually just a 24 bytes
shift that allows to protect first 3 entries of got.plt with relro. 
(https://svnweb.freebsd.org/base/head/sys/conf/ldscript.amd64?revision=284870&view=markup#l146). 

DATA_SEGMENT_RELRO_END just aligns to the page boundary. 
That is what expected because all sections that are not affected by relro should be on another memory page.
So at fact the difference with documented behavior is that we do not pad DATA_SEGMENT_ALIGN. 
3 entries of got.plt are uncovered by relro, but functionality is simple and equal to lld behavior 
for case when script is not given.

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

llvm-svn: 276778
2016-07-26 17:58:44 +00:00
George Rimar 4509a4f52a [ELF] - Merged 2 lines. NFC.
llvm-svn: 276768
2016-07-26 17:01:18 +00:00
George Rimar 528e0bfa12 [ELF] Linkerscript: simplify DATA_SEGMENT_ALIGN evaluation
We can simplify the evaluation of DATA_SEGMENT_ALIGN
just to simple align(). That way it will work exactly like we have in non-script case.

Change was suggested by Rafael Ávila de Espíndola

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

llvm-svn: 276745
2016-07-26 14:00:10 +00:00
George Rimar bfc4a4b7a1 [ELF] - Fixed possible iterator overflow.
We can have Opt.Commands size greater then Sections.size().
For example if we have next script:

SECTIONS { 
.aaa : { *(.aaa) }           
.bbb : { *(.bbb) }   
.ccc : { *(.ccc) }   
}

and next code:

.global _start
_start:
 nop

.section .aaa,"a"
 .quad 0

Then amount of sections is less than amound of Opt.Commands
and if we for example have all commands NoConstraint,
that overflowed the iterator used.

llvm-svn: 276741
2016-07-26 10:47:09 +00:00
Eugene Leviant f9d897c7a7 [ELF/Linkerscript] Remove special handling of TLS/NOTE/RELRO sections (patch from ruiu)
llvm-svn: 276731
2016-07-26 07:20:40 +00:00
Rui Ueyama 29c5a2a9b7 Split getPhdrsIndices. NFC.
llvm-svn: 276717
2016-07-26 00:27:36 +00:00
Rui Ueyama f510fa6b68 Replace std::find_if with plain for loop. NFC.
llvm-svn: 276715
2016-07-26 00:21:15 +00:00
Rui Ueyama 6b2748104b Split LinkerScript::createSections into small functions.
createSections function is getting longer, so it is time to split it
into small functions. The reason why the function is long is because
it has deeply nested for-loops. This patch constructs temporary data
to reduce nesting level.

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

llvm-svn: 276706
2016-07-25 22:51:07 +00:00
Rui Ueyama 63dc650938 Fix parameter names.
match() returns true of the first argument, a target string, matches
one of the second argument, a list of glob patterns. Calling the
target string, which is not a glob pattern, "Pattern" was very confusing.

llvm-svn: 276705
2016-07-25 22:41:42 +00:00
Rui Ueyama 96bdd5bc81 Do not pass InputFile to reportDiscarded().
Because the file from which an input section is created can be
obtained using getFile().

llvm-svn: 276702
2016-07-25 22:26:28 +00:00
Rui Ueyama efc4066b47 Make ConstraintKind an enum class.
llvm-svn: 276697
2016-07-25 22:00:10 +00:00
Davide Italiano 0ed42b0ca0 [LinkerScript] Refactor KEEP handling in a separate function
This will grow because I have a patch to support more complex
constructs, e.g.:

KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors))

Let's make this a separate function.

llvm-svn: 276695
2016-07-25 21:47:13 +00:00
Rui Ueyama 3c291e1aa2 Re-commit "Split LinkerScript::createSections".
Re-commit r276543 with a fix for buildbots.

llvm-svn: 276693
2016-07-25 21:30:00 +00:00
George Rimar 58e5c4dcfe [ELF] - Linkerscript: implemented output section [address] attribute.
Output section description in SECTIONS looks like that:

section [address] [(type)] :
...
{
...
}

Patch implements support of address atribute.

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

llvm-svn: 276619
2016-07-25 08:29:46 +00:00
Davide Italiano 373a533a0a [ELF] Fix the semantic of PROVIDE in linker scripts.
PROVIDE request us to define a symbol only if it is referenced and is
not defined by any object included in the link. We created the
symbol in the symbol table no matter what.

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

llvm-svn: 276592
2016-07-25 00:25:18 +00:00
Rui Ueyama edebbdf12a Simplify. NFC.
llvm-svn: 276586
2016-07-24 23:47:31 +00:00
Davide Italiano 054a679634 [ELF] Support PROVIDE/PROVIDE_HIDDEN inside output sections description.
Differential Revision:  https://reviews.llvm.org/D22738

llvm-svn: 276579
2016-07-24 23:13:48 +00:00
Rui Ueyama 113cdec9ec Merge readSymbolAssignment with readAssignment. NFC.
llvm-svn: 276575
2016-07-24 23:05:57 +00:00
Rui Ueyama 708019c41b Make readExpr return an Expr object instead of a vector of tokens.
Previously, we handled an expression as a vector of tokens. In other
words, an expression was a vector of uncooked raw StringRefs.
When we need a value of an expression, we used ExprParser to run
the expression.

The separation was needed essentially because parse time is too
early to evaluate an expression. In order to evaluate an expression,
we need to finalize section sizes. Because linker script parsing
is done at very early stage of the linking process, we can't
evaluate expressions while parsing.

The above mechanism worked fairly well, but there were a few
drawbacks.

One thing is that we sometimes have to parse the same expression
more than once in order to find the end of the expression.
In some contexts, linker script expressions have no clear end marker.
So, we needed to recognize balanced expressions and ternary operators.

The other is poor error reporting. Since expressions are parsed
basically twice, and some information that is available at the first
stage is lost in the second stage, it was hard to print out
apprpriate error messages.

This patch fixes the issues with a new approach.

Now the expression parsing is integrated into ScriptParser.
ExprParser class is removed. Expressions are represented as lambdas
instead of vectors of tokens. Lambdas captures information they
need to run themselves when they are created.

In this way, ends of expressions are naturally detected, and
errors are handled in the usual way. This patch also reduces
the amount of code.

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

llvm-svn: 276574
2016-07-24 18:19:40 +00:00
Rui Ueyama 8a9bb7baeb Rollback r276538 and r276540 to unbreak asan bot.
llvm-svn: 276543
2016-07-24 02:05:09 +00:00
Rui Ueyama d227620582 Simplify. NFC.
llvm-svn: 276540
2016-07-24 01:18:18 +00:00
Rui Ueyama 01151e9c24 Split LinkerScript::createSections.
createSections is getting longer, so it is probably time to split.

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

llvm-svn: 276538
2016-07-24 01:06:18 +00:00
Davide Italiano 8ab4108df2 [ELF/Linkerscript] Define an absolute if we find an undefined.
Otherwhise undefined references to symbols defined in linker scripts
are never resolved.

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

llvm-svn: 276536
2016-07-23 22:09:04 +00:00
Rui Ueyama adca245f79 Remove Phdr typedef.
I don't think this typedef contributes to readability.

llvm-svn: 276525
2016-07-23 14:18:48 +00:00
Rui Ueyama 36a153cd83 Make a pure function a non-member file-scoped function.
llvm-svn: 276524
2016-07-23 14:09:58 +00:00
Eugene Leviant a31c91b150 [ELF] Support PROVIDE and PROVIDE_HIDDEN inside SECTIONS
llvm-svn: 276398
2016-07-22 07:38:40 +00:00
Davide Italiano 246f681e0b [ELF/LinkerScript] Support ONLY_IF_{RO, RW} directive.
Differential Revision:   https://reviews.llvm.org/D22660

llvm-svn: 276384
2016-07-22 03:36:24 +00:00
George Rimar 92e93fb4ba [ELF] - Basic support of linkerscript commands: DATA_SEGMENT_ALIGN, DATA_SEGMENT_END, CONSTANT
It is called basic because:

CONSTANT expression can refer to COMMONPAGESIZE and MAXPAGESIZE.
This sizes are usually different and used for possible optimization of
memory consumption. 
More details are here: https://sourceware.org/ml/binutils/2002-02/msg00265.html
We currently do not support this optimization, so both CONSTANT(MAXPAGESIZE)
and CONSTANT(COMMONPAGESIZE) just return Target->PageSize value.

DATA_SEGMENT_ALIGN and DATA_SEGMENT_END are used as a part of opt.
The latter one is just ignored now.
According to documentation DATA_SEGMENT_ALIGN has 2 possible
calculation, but since we do not support mentioned opt - it 
is always calculated now as (ALIGN(MAXPAGESIZE) + (. & (MAXPAGESIZE - 1))).

In general this should work for now until we deside to support this opt.

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

llvm-svn: 276323
2016-07-21 19:48:00 +00:00
Rui Ueyama 629e0aa5be Update comment.
llvm-svn: 276322
2016-07-21 19:45:22 +00:00
Reid Kleckner 3c944ec81a Fix MSVC 2015 compilation failure around range-for without curly braces
It doesn't appear to like this pattern:
  for (auto X : Xs)
    if (...) { ... }
    else ...;

We have heard anecdotes that range based for loops are implemented as a
token rewrite in MSVC's lexer, and that the most challenging part of the
rewrite is finding the end of the for loop. That makes sense, given that
it's a lexer.

llvm-svn: 276315
2016-07-21 18:39:28 +00:00
Eugene Leviant 8a8a822a96 [ELF] Fix bug in program header FLAGS processing + test case update (found by grimar)
llvm-svn: 276301
2016-07-21 16:13:54 +00:00
George Rimar 10e576e109 [ELF] - Cleanup of LinkerScript<ELFT>::assignAddresses()
LinkerScript<ELFT>::assignAddresses is becoming larger and looks 
it can be good time for splitting. I expect to can more SectionsCommand's there, 
and dispatching some of them separatelly can help to keep method smaller either.

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

llvm-svn: 276300
2016-07-21 16:07:40 +00:00
George Rimar eea3114ff9 [ELF] - Linkerscript: add InputSectionDescription command to LS parser.
This adds InputSectionDescription command to represent
the input section declaration.

This leads to next cleanup:
SectionRule removed.
ScriptConfiguration::Sections mamber removed.
LinkerScript<ELFT>::getOutputSection() removed.

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

llvm-svn: 276283
2016-07-21 14:26:59 +00:00
Rui Ueyama fd03cfd27a Instantiate Interp output section only when needed.
This change simplifies interaction between Writer and the linker script
because we can make needsInterpSection() a file-scope function.

llvm-svn: 276261
2016-07-21 11:01:23 +00:00
Eugene Leviant 865bf8637b [ELF] Support FLAGS attribute in program header definition
llvm-svn: 276260
2016-07-21 10:43:25 +00:00
George Rimar f6c3ccef5e [ELF] - Linkerscript: remove excessive ScriptConfiguration::Filler field
Previously OutputSectionCommand::Filler was introduced, but unused.
Patch fixes that.

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

llvm-svn: 276253
2016-07-21 07:48:54 +00:00
George Rimar 076fe15757 [ELF] - Initial support of tree-style linker script implemented.
Approach uses LLVM-style RTTI for representing the linker script
commands in a form of tree for future simplification of parsing.

Core idea and code sample belongs to Rui Ueyama.

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

llvm-svn: 276243
2016-07-21 06:43:01 +00:00
Rui Ueyama 18f084ff62 Function names should start with lowercase letters.
llvm-svn: 276165
2016-07-20 19:36:41 +00:00
Rui Ueyama b0f6c59023 Replace parallel arrays with a StringSwitch.
llvm-svn: 276163
2016-07-20 19:36:38 +00:00
Rui Ueyama 047404f711 Remove `else` after `break`.
llvm-svn: 276162
2016-07-20 19:36:36 +00:00
Rui Ueyama a7f7884df8 Simplify output section ownership.
This patch simplifies output section management by making
Factory class have ownership of sections that creates.

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

llvm-svn: 276141
2016-07-20 17:19:03 +00:00
George Rimar 31d842f55f [ELF] - Refactor of LinkerScript<ELFT>::getPhdrIndicesForSection
Previously it was harder to read and also has a error:
command kind was not checked.

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

llvm-svn: 276137
2016-07-20 16:43:03 +00:00
Eugene Leviant e63d81bd05 [ELF] Create output sections in LinkerScript class
llvm-svn: 276121
2016-07-20 14:43:20 +00:00
Rafael Espindola 74df5c7eeb Fix build with gcc 6.
llvm-svn: 275972
2016-07-19 12:33:46 +00:00
Eugene Leviant bbe3860244 [ELF] Minimal PHDRS parser and section to segment assignment support
llvm-svn: 275965
2016-07-19 09:25:43 +00:00
George Rimar b91e7118d4 Reformat comment from 3 to 2 lines. NFC.
llvm-svn: 275961
2016-07-19 07:42:07 +00:00
George Rimar 6ad330acaa Fixed comment. NFC.
llvm-svn: 275959
2016-07-19 07:39:07 +00:00
Eugene Leviant 0e36f42846 [ELF] Overriding reserved symbols in linker script
llvm-svn: 275549
2016-07-15 11:20:04 +00:00
Rui Ueyama 177746a739 Remove redundant `return`.
llvm-svn: 275528
2016-07-15 04:32:11 +00:00
Rui Ueyama 05ef4cff44 Merge SymbolAssignmentKind and ExprKind.
In a linker script, `.` is a special symbol indicating a counter.
Previously, we had two expression types, ExprKind and SymbolAssignmentKind
for `.` and all the other symbol names, respectively. But we could merge
them because the former is a special case of the latter.

llvm-svn: 275527
2016-07-15 04:19:37 +00:00
Eugene Leviant b030411414 [ELF] r275383 reverted due to buildbot failure
llvm-svn: 275385
2016-07-14 09:21:24 +00:00
Eugene Leviant 219d9b2b18 [ELF] Allow overriding reserved symbols in linker scripts
llvm-svn: 275383
2016-07-14 08:26:41 +00:00
Eugene Leviant eda81a1b86 [ELF] Support for symbol assignment in linker scripts within SECTIONS {} block
llvm-svn: 275158
2016-07-12 06:39:48 +00:00
Rafael Espindola 64c32d6f02 Fix formating. NFC.
llvm-svn: 274757
2016-07-07 14:28:47 +00:00
Rui Ueyama 52c4e17f8a Remove trailing whitespaces.
llvm-svn: 274343
2016-07-01 10:42:25 +00:00
Eugene Leviant 467c4d5538 [ELF] Fix first PT_LOAD segment VA calculation, when linker script is used
llvm-svn: 274342
2016-07-01 10:27:36 +00:00
Rui Ueyama 93c9af425e Create Strings.cpp and move string manipulation functions to that file.
llvm-svn: 274109
2016-06-29 08:01:32 +00:00
Rui Ueyama 722830a51b Rename matchStr -> globMatch.
llvm-svn: 274103
2016-06-29 05:32:09 +00:00
Davide Italiano 8e1131dc46 [ELF] Support for wildcard in version scripts.
Example:

VERSION_1.0 {
  global: foo*;
  local: *; }

now correctly matches all the symbols which name starts with
`foo`.

Differential Revision:  http://reviews.llvm.org/D21732

llvm-svn: 274091
2016-06-29 02:46:51 +00:00
Davide Italiano 47ae3c3e22 [LinkerScript] Spell 'character' correctly. NFC.
llvm-svn: 273817
2016-06-26 19:02:43 +00:00
Rui Ueyama 424b408165 Rename Align -> Alignment.
I think it is me who named these variables, but I always find that
they are slightly confusing because align is a verb.
Adding four letters is worth it.

llvm-svn: 272984
2016-06-17 01:18:46 +00:00
Dima Stepanov fb8978fc6a Fix the function to set the section VMA/LMA fields in case of using
the linker script. The cycle in the ELF/LinkerScript.cpp:assignAddresses()
routine will be used to go through all the sections and set all the
addresses correctly.

Add new test to check this case.

llvm-svn: 270090
2016-05-19 18:15:54 +00:00
George Rimar fbfe10f221 Removed dead code. NFC.
llvm-svn: 268497
2016-05-04 13:44:49 +00:00
George Rimar ab9390664f [ELF] - Implemented comparsion operators for linkerscript.
Patch adds support of <,>,!=,==,>=,<= operators.

Differential revision: http://reviews.llvm.org/D19419

llvm-svn: 267382
2016-04-25 08:14:41 +00:00
Rui Ueyama 9c1112d09a Use ScriptParserBase features to parse linker script expressions.
Previously, we have re-implemented utility functions such as `expect`
or `next` in LinkerScript.cpp. This patch reuses the existing
implementation that is in ScriptParser.cpp.

llvm-svn: 267255
2016-04-23 00:04:03 +00:00
Rui Ueyama 4a46539c24 Devirtualize ScriptParserBase. NFC.
ScriptParserBase class is a container of collection of various methods
to parse linker script-ish text. It had a virtual method `run` to run
the parser. But we don't have to enforce its descendents to implement
that. It's up to them.

This patch removes pure virtual function `run`.

llvm-svn: 267246
2016-04-22 22:59:24 +00:00
Rui Ueyama 5fa60985cf Inline getInteger as it's too short to be a function. NFC.
llvm-svn: 267219
2016-04-22 21:05:04 +00:00
Rui Ueyama e29a975d23 Update a comment for r267145.
llvm-svn: 267218
2016-04-22 21:02:27 +00:00
Rui Ueyama 0b3868ec6b Move uintX_t typedef to the class definition. NFC.
Now it is doable because LinkerScript is a template class.

llvm-svn: 267212
2016-04-22 20:41:07 +00:00
George Rimar dffc1410c5 [ELF] - Implemented linkerscript ALIGN command
ALIGN(exp)
Return the location counter (.) aligned to the next exp boundary. (https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/4/html/Using_ld_the_GNU_Linker/expressions.html)

Patch implements this command.
This fixes PR27406.

Differential revision: http://reviews.llvm.org/D19364

llvm-svn: 267145
2016-04-22 11:40:53 +00:00
George Rimar fba45c41df Recommitted r267132 "[ELF] - implemented ternary operator for linkerscript expressions"
With fix: removed redundant Dot parameter.

Original commit message:
[ELF] - implemented ternary operator for linkerscript expressions

Patch implements ternary operator for linkerscript expressions.
Like:

SECTIONS {
 . = 0x1 ? 0x2 : 0x3;
...
}

Differential revision: http://reviews.llvm.org/D19332

llvm-svn: 267140
2016-04-22 11:28:54 +00:00
George Rimar f8e230b277 Revert r267132 as it broke buildbot.
llvm-svn: 267134
2016-04-22 10:51:34 +00:00
George Rimar 8c4acddebc [ELF] - implemented ternary operator for linkerscript expressions
Patch implements ternary operator for linkerscript expressions.
Like:

SECTIONS {
 . = 0x1 ? 0x2 : 0x3;
...
}

Differential revision: http://reviews.llvm.org/D19332

llvm-svn: 267132
2016-04-22 10:35:34 +00:00
Rui Ueyama c9f402eadc Inline SectionRule::match.
This short function was used only once and didn't provide much value.

llvm-svn: 267086
2016-04-22 00:23:52 +00:00
Rui Ueyama c998a8c044 ELF: Make the special variable "." as a LinkerScript class member.
I will eventually make `evaluate` function a usual parse function
rather than a function that works on a separate token list.
This is the first step toward that.

llvm-svn: 267083
2016-04-22 00:03:13 +00:00
Rui Ueyama 8ec77e64fc ELF: Change how to handle KEEP linker script command.
You can instruct the linker to not discard sections even if they
are unused and --gc-sections option is given. The linker script
command for doing that is KEEP. The syntax is KEEP(foo) where foo
is a section name. KEEP commands are written in SECTIONS command,
so you can specify the order of sections *and* which sections
will be kept.

Each sub-command in SECTIONS command are translated into SectionRule
object. Previously, each SectionRule has `Keep` bit. However,
if you think about it, this hid information in too deep in elements
of a list. Semantically, KEEP commands aren't really related to
SECTIONS subcommands. We can keep the section list for KEEP in a
separate list. This patch does that.

llvm-svn: 267065
2016-04-21 22:00:51 +00:00
Rui Ueyama c3e2a4b006 ELF: Change the return type of getSectionOrder.
Also changed the function name and added comments.

llvm-svn: 267044
2016-04-21 20:30:00 +00:00
George Rimar dbbd8b15bf [ELF] - Use ArrayRef instead of std::vector& for LinkerScript module. NFC.
llvm-svn: 266978
2016-04-21 11:21:48 +00:00
George Rimar 71b26e94fd [ELF] - Get rid of SectionOrder array.
SectionOrder vector was a part of LinkerScript class.

It can be removed because Commands vector contains the 
same information and SectiorOrder is just a subset.

Differential revision: http://reviews.llvm.org/D19171

llvm-svn: 266974
2016-04-21 10:22:02 +00:00
Rui Ueyama 6011811beb Define and use a utility function. NFC.
llvm-svn: 266914
2016-04-20 20:54:13 +00:00
Rui Ueyama 99e519cdeb ELF: Redefine parseExpr to parse an expression. NFC.
Previously the function reads an operator and the rest of
the expressions. This patch makes it to actually parse an expression
which starts with a primary pexression followed by other expressions
concatenated with operators.

llvm-svn: 266912
2016-04-20 20:48:25 +00:00
Rui Ueyama 07320e4030 ELF: Template LinkerScript class.
Originally, linker scripts were basically an alternative way to specify
options to the command line options. But as we add more features to hanlde
symbols and sections, many member functions needed to be templated.
Now most the members are templated. It is probably time to template the
entire class.

Previously, LinkerScript is an executor of the linker script as well as
a storage of linker script configurations. This is not suitable to template
the class because when we are reading linker script files, we don't know
the ELF type yet, so we can't instantiate ELF-templated classes.

In this patch, I defined a new class, ScriptConfiguration, to store
linker script configurations. ScriptParser writes parse results to it,
and LinkerScript uses them.

Differential Revision: http://reviews.llvm.org/D19302

llvm-svn: 266908
2016-04-20 20:13:41 +00:00
Rui Ueyama 0536ec0242 Make a function file-scoped.
llvm-svn: 266816
2016-04-19 20:50:25 +00:00
Rui Ueyama 7a81d674f6 Attempt to fix buildbot.
llvm-svn: 266799
2016-04-19 19:04:03 +00:00
Rui Ueyama 960504b91d ELF: Add +, -, *, / and & to SECTIONS linker script command.
This patch is based heavily on George Rimor's patch
http://reviews.llvm.org/D19221.

In the linker script, you can write expressions to compute addresses.
Currently we only support "+" operator. This adds a few more operators.

Since this patch adds * and /, we need to handle operator precedences
properly. I implemented that using the operator-precedence grammar.

Differential Revision: http://reviews.llvm.org/D19237

llvm-svn: 266798
2016-04-19 18:58:11 +00:00
Rui Ueyama 9e957a0ac6 Rename LocationNode -> SectionsCommand.
They are called sections-command in the doc, so it is nice to keep
it consistent with it.
https://sourceware.org/binutils/docs/ld/SECTIONS.html#SECTIONS

llvm-svn: 266668
2016-04-18 21:00:45 +00:00
Rui Ueyama 54115af45e Remove redundant curly braces.
llvm-svn: 266667
2016-04-18 21:00:43 +00:00
Rui Ueyama 7c18c28c97 Refactor LinkerScript::assignAddresses. NFC.
llvm-svn: 266666
2016-04-18 21:00:40 +00:00
George Rimar 652852c5c0 Recommitted 266457 with fix:
* Do script driven layout only if SECTIONS section exist.

Initial commit message:

[ELF] - Implemented basic location counter support.

This patch implements location counter support. 
It also separates assign addresses for sections to assignAddressesScript() if it scipt exists.

Main testcase is test/ELF/linkerscript-locationcounter.s, It contains some work with location counter. It is basic now.
Implemented location counter assignment and '+' operations.

Patch by myself with LOTS of comments and design suggestions from Rui Ueyama.

Differential revision: http://reviews.llvm.org/D18499

llvm-svn: 266526
2016-04-16 10:10:32 +00:00
Rui Ueyama faac567e68 Revert r266457: "[ELF] - Implemented basic location counter support."
This reverts commit r266457 as it breaks "hello world" both on
Linux and FreeBSD.

llvm-svn: 266485
2016-04-15 22:39:27 +00:00
George Rimar ea25877d4a [ELF] - Implemented basic location counter support.
This patch implements location counter support. 
It also separates assign addresses for sections to assignAddressesScript() if it scipt exists.

Main testcase is test/ELF/linkerscript-locationcounter.s, It contains some work with location counter. It is basic now.
Implemented location counter assignment and '+' operations.

Patch by myself with LOTS of comments and design suggestions from Rui Ueyama.

Differential revision: http://reviews.llvm.org/D18499

llvm-svn: 266457
2016-04-15 17:44:19 +00:00
Adhemerval Zanella e77b5bf69f [lld] [ELF] Add ScriptParserBase class
This patch add a base script tokenizer class to decouple parsing from
linker script handling.  The idea is to use this base class on dynamic
list parsing (--dynamic-list option). No functionality added.

llvm-svn: 265600
2016-04-06 20:59:11 +00:00
George Rimar 2348320d4e [ELF] - Teach linkerscript error handler to show full script line and column marker on error.
When error, this adds the text line of script to the output
and a marks exact incorrect token under it:

line 1: <error text here>
UNKNOWN_TAG {
      ^

Differential revision: http://reviews.llvm.org/D18699

llvm-svn: 265523
2016-04-06 08:08:40 +00:00
Rui Ueyama 38dc83417b Include line number in error message for linker scripts.
This patch is based on http://reviews.llvm.org/D18545 written
by George Rimar.

llvm-svn: 264878
2016-03-30 16:51:57 +00:00
Rui Ueyama 3ed2f06913 Fix spelling.
llvm-svn: 263358
2016-03-13 03:17:44 +00:00
George Rimar 777f96304e Recommit of r263252, [ELF] - Change all messages to lowercase to be consistent.
which was reverted because included
unrelative changes by mistake.

Original commit message:

[ELF] - Change all messages to lowercase to be consistent.

That is directly opposite to http://reviews.llvm.org/D18045,
which was reverted.

This patch changes all messages to start from lowercase letter if
they were not before.

That is done to be consistent with clang.

Differential revision: http://reviews.llvm.org/D18085

llvm-svn: 263337
2016-03-12 08:31:34 +00:00
Rui Ueyama f714955402 Revert r263252: "[ELF] - Change all messages to lowercase to be consistent."
This reverts commit r263252 because the change contained unrelated changes.

llvm-svn: 263272
2016-03-11 18:46:51 +00:00
George Rimar 96bcdae1a5 [ELF] - Change all messages to lowercase to be consistent.
That is directly opposite to http://reviews.llvm.org/D18045,
which was reverted.

This patch changes all messages to start from lowercase letter if
they were not before.

That is done to be consistent with clang.

Differential revision: http://reviews.llvm.org/D18085

llvm-svn: 263252
2016-03-11 16:40:55 +00:00
George Rimar 5761042db7 This reverts the r263125
It was discussed to make all messages be 
lowercase to be consistent with clang.
(also reverts the r263128 which fixed 
build bot fail after r263125)

Original commit message:
[ELF] - Consistent spelling for error/warning messages

Previously error and warnings were not consistent in lld.
Some of them started from lowercase letter, others from
uppercase. Also there was one or two which had a dot at the end.
This patch changes all messages to start from uppercase letter if
they were not before.

Differential revision: http://reviews.llvm.org/D18045

llvm-svn: 263240
2016-03-11 14:43:02 +00:00
George Rimar e094388861 [ELF] - Consistent spelling for error/warning messages
Previously error and warnings were not consistent in lld.
Some of them started from lowercase letter, others from
uppercase. Also there was one or two which had a dot at the end.
This patch changes all messages to start from uppercase letter if
they were not before.

Differential revision: http://reviews.llvm.org/D18045

llvm-svn: 263125
2016-03-10 16:58:34 +00:00
Rafael Espindola 0650192094 Revert "ELF: Add /lib and /usr/lib as default search paths."
It was causing errors like

/lib/libc.so.6 is incompatible with elf_x86_64

when linking on Fedora.

Every system has different default paths. It seems better to just trust
the driver to pass the correct -L options.

This reverts commit 262910.

llvm-svn: 262941
2016-03-08 17:13:12 +00:00
Rui Ueyama b9883d55d8 ELF: Add /lib and /usr/lib as default search paths.
GNU ld and gold have these paths as default search paths.
If you don't want these directories, pass -nostdlib.

llvm-svn: 262910
2016-03-08 04:06:27 +00:00
Rui Ueyama 3e80897627 ELF: Remove OutSection class and use a map instead.
It is easier to handle section filler data separately rather than
merging with section names.

llvm-svn: 262175
2016-02-28 05:09:11 +00:00
Rui Ueyama 7a6c9aed7a Simplify. NFC.
llvm-svn: 262174
2016-02-28 04:48:54 +00:00
Rafael Espindola e0df00b91f Rename elf2 to elf.
llvm-svn: 262159
2016-02-28 00:25:54 +00:00
George Rimar e2ee72b509 [ELF] - Implemented linkerscript sections padding.
BSD linker scripts contain special cases to add NOP
padding to code sections. Syntax is next:

.init:
 {
   KEEP (*(.init))
 } =0x90909090
(0x90 is NOP)

This patch implements that functionality.

llvm-svn: 262020
2016-02-26 14:48:31 +00:00
Rui Ueyama dc9cc6a574 Remove redundant template instantiations.
This class is used only in this translation unit, so no need
to instantiate them explicitly.

llvm-svn: 261951
2016-02-26 01:30:35 +00:00
George Rimar c3794e5834 [ELF] - replaced std::function with raw pointers in LinkerScript.cpp
Change as was proposed by David Blaikie.

Differential revision: http://reviews.llvm.org/D17499

llvm-svn: 261729
2016-02-24 09:21:47 +00:00
George Rimar cb2aeb66e4 [ELF] - Teach input section wildcard patterns to recognize '?' meta character.
`?' - matches any single character
https://sourceware.org/binutils/docs/ld/Input-Section-Wildcards.html

This is used in linker scripts.

Differential revision: http://reviews.llvm.org/D17290

llvm-svn: 261726
2016-02-24 08:49:50 +00:00
George Rimar 481c2ce64b [ELF] - Linkerscript KEEP command.
When link-time garbage collection is in use (-gc-sections), it is 
often useful to mark sections that should not be eliminated. 
This is accomplished by surrounding an input section's wildcard 
entry with KEEP(). Patch implements that command.

Differential revision: http://reviews.llvm.org/D17242

llvm-svn: 261616
2016-02-23 07:47:54 +00:00
George Rimar d2389bfd9d Attemp to heal windows buildbot
http://lab.llvm.org:8011/builders/sanitizer-windows/builds/17414

llvm-svn: 261322
2016-02-19 11:56:49 +00:00
George Rimar f23b23200d [ELF] - Minor refactor of LinkerScript file
* Else-ifs in ScriptParser::run() replaced with std::function + map
* Reordered members of ScriptParser

Differential revision: http://reviews.llvm.org/D17256

llvm-svn: 261317
2016-02-19 10:45:45 +00:00
Rui Ueyama 1ebc8ed78a ELF: Add wildcard pattern matching to SECTIONS linker script command.
Each rule in SECTIONS commands is something like ".foo *(.baz.*)",
which instructs the linker to collect all sections whose name matches
".baz.*" from all files and put them into .foo section.

Previously, we didn't recognize the wildcard character. This patch
adds that feature.

Performance impact is a bit concerning because a linker script can
contain hundreds of SECTIONS rules, and doing pattern matching against
each rule would be too expensive. We could merge all patterns into
single DFA so that it takes O(n) to the input size. However, it is
probably too much at this moment -- we don't know whether the
performance of pattern matching matters or not. So I chose to
implement the simplest algorithm in this patch. I hope this simple
pattern matcher is sufficient.

llvm-svn: 260745
2016-02-12 21:47:28 +00:00
Rui Ueyama e9c5806593 ELF: Remove use of MapVector from LinkerScript.
We don't have to use a MapVector here. Instead, just std::vector suffices.

llvm-svn: 260724
2016-02-12 20:41:43 +00:00
Rui Ueyama f9de0d6904 ELF: Remove readLinkerScript and define LinkerScript::read instead.
llvm-svn: 260598
2016-02-11 21:38:55 +00:00
Rui Ueyama 717677af35 ELF: Create LinkerScript class to move code out of Writer.
Previously, we had code for linker scripts in Writer. This patch
separates that as LinkerScript class. The class provides a few
functions to query linker scripts and is also a container of some
linker-script-specific information.

Hopefully, Writer will only implement the default behavior and let
the new class handle gotchas regarding linker scripts.

llvm-svn: 260591
2016-02-11 21:17:59 +00:00
Rui Ueyama 21eecb4f14 Re-submit ELF: Report multiple errors from the driver.
This reverts r259395 which reverted r259143.

llvm-svn: 259572
2016-02-02 21:13:09 +00:00
Rui Ueyama 025d59b16a ELF: Do not use fatal in LinkerScript.cpp.
This patch adds "Error" field to LinkerScript class. That field
is false by default, and set to true if there is a syntax error
in an input file. The linker script parser is a recursive-descedent
parser. Each function returns if Error is true -- so that
eventually the whole parser returns to a caller.

http://reviews.llvm.org/D16667

llvm-svn: 259557
2016-02-02 20:27:59 +00:00
Nico Weber f07bd3b72d Revert r259143, it broke check-lld on Windows (see PR26388).
llvm-svn: 259395
2016-02-01 20:03:53 +00:00
Rui Ueyama 1940424632 ELF: Report multiple errors from the driver.
This patch let the driver keep going until it parses all
command line options.

http://reviews.llvm.org/D16645

llvm-svn: 259143
2016-01-29 01:54:52 +00:00
Rui Ueyama 64cfffd333 ELF: Rename error -> fatal and redefine error as a non-noreturn function.
In many situations, we don't want to exit at the first error even in the
process model. For example, it is better to report all undefined symbols
rather than reporting the first one that the linker picked up randomly.

In order to handle such errors, we don't need to wrap everything with
ErrorOr (thanks for David Blaikie for pointing this out!) Instead, we
can set a flag to record the fact that we found an error and keep it
going until it reaches a reasonable checkpoint.

This idea should be applicable to other places. For example, we can
ignore broken relocations and check for errors after visiting all relocs.

In this patch, I rename error to fatal, and introduce another version of
error which doesn't call exit. That function instead sets HasError to true.
Once HasError becomes true, it stays true, so that we know that there
was an error if it is true.

I think introducing a non-noreturn error reporting function is by itself
a good idea, and it looks to me that this also provides a gradual path
towards lld-as-a-library (or at least embed-lld-to-your-program) without
sacrificing code readability with lots of ErrorOr's.

http://reviews.llvm.org/D16641

llvm-svn: 259069
2016-01-28 18:40:06 +00:00
Pete Cooper 6025933789 Fix bug in handling of quoted linker script tokens and update tests to use it.
There was an off by one error because the StringRef.substr functions
second argument is a length, not the end index.

Also updated a few ELF files which failed when run on Jenkins with paths
including the @ character.  This is often used in Jenkins for shared
workspace plugin.

Reviewed by Rui Ueyama

llvm-svn: 258583
2016-01-22 23:46:37 +00:00
Rui Ueyama 83cd6e00e9 Remove unnecessary `lld::`.
llvm-svn: 256970
2016-01-06 20:11:55 +00:00
Simon Atanasyan a1b8fc3bb4 [ELF] Lookup INPUT argument in the current directory
If an argument of the INPUT directive is a regular path, linker should
lookup it in the current folder first.

The fix does not contain any test cases because I think it is not a good
idea to pollute a current folder (which in general might be arbitrary)
by test files.

Differential Revision: http://reviews.llvm.org/D15027

llvm-svn: 254178
2015-11-26 20:23:46 +00:00
Simon Atanasyan 16b0cc9ee6 [ELF] Reapply r254031 - LinkerScript: lookup absolute paths under sysroot
In case a sysroot prefix is configured, and the filename starts with
the '/' character, and the script being processed was located inside
the sysroot prefix, the file's name will be looked for in the sysroot
prefix. Otherwise, the linker falls to the common lookup scheme.

It is slightly modified version of the commit r254031. The problem of
the initial commit was in the `is_absolute` call. On Windows 'C:\' is
absolute path but we do not need to find it under sysroot. In this patch
linker looks up a path under sysroot only if the paths starts with '/'
character.

llvm-svn: 254135
2015-11-26 05:53:00 +00:00
Hans Wennborg 17436ce174 Follow-up to r254049; remove function I missed in the revert.
llvm-svn: 254052
2015-11-25 01:24:15 +00:00
Hans Wennborg 82d2d07ced Revert r254031: "ELF2: LinkerScript: lookup absolute paths under sysroot"
The test fails on Windows:

Command 34: "ld.lld" "-o" "D:\src\llvm\build.release\tools\lld\test\ELF\Output\l
inkerscript.s.tmp2" "D:\src\llvm\build.release\tools\lld\test\ELF\Output\linkers
cript.s.tmp.dir/xyz.script" "--sysroot=D:\src\llvm\build.release\tools\lld\test\
ELF\Output\linkerscript.s.tmp.dir"
Command 34 Result: 1
Command 34 Output:

Command 34 Stderr:
Unable to find /libxyz.a

llvm-svn: 254049
2015-11-25 00:58:31 +00:00
Rui Ueyama a7e09c6221 ELF2: Factor out isUnderSysroot from readLinkerScript. NFC.
llvm-svn: 254032
2015-11-24 22:26:33 +00:00
Simon Atanasyan 44136b675d ELF2: LinkerScript: lookup absolute paths under sysroot
In case a sysroot prefix is configured, and the filename starts with the
'/' character, and the script being processed was located inside the
sysroot prefix, the file's name will be looked for in the sysroot
prefix. Otherwise, the linker falls to the common lookup scheme.

https://www.sourceware.org/binutils/docs-2.24/ld/File-Commands.html

Differential Revision: http://reviews.llvm.org/D14916

llvm-svn: 254031
2015-11-24 22:16:25 +00:00
Denis Protivensky 8e3b38aba1 [ELF2] SECTIONS command basic support
* determine output section by input section name
* discard input sections
* order output sections accordingly

Differential Revision: http://reviews.llvm.org/D14140

llvm-svn: 252868
2015-11-12 09:52:08 +00:00
George Rimar 83f406cff5 [ELF2] - Linker script EXTERN command implemented.
The reason of collecting all undefines in vector is that during reading files we already need to have Symtab created. Or like was done in that patch - to put undefines from scripts somewhere to delay Symtab.addUndefinedOpt() call.

Differential Revision: http://reviews.llvm.org/D13870

llvm-svn: 250711
2015-10-19 17:35:12 +00:00
Davide Italiano 10d268d62c [ELF2/Linkerscript] Skip semicolon in linker directives parsing.
The documentation says: "You may separate commands using semicolons",
so they seem to be optional.

Differential Revision:	http://reviews.llvm.org/D13703

llvm-svn: 250223
2015-10-13 20:48:56 +00:00
Rui Ueyama 34f2924675 ELF2: Add comments.
llvm-svn: 250215
2015-10-13 19:51:57 +00:00
Rafael Espindola 7f10746be0 Remove unnecessary include.
llvm-svn: 250161
2015-10-13 14:54:49 +00:00
Rui Ueyama f03f3cc18c Use llvm::sys::path::is_absolute instead of my own.
llvm-svn: 250133
2015-10-13 00:09:21 +00:00
Rui Ueyama 63d8880b38 Fix test breakage on Windows.
llvm-svn: 250127
2015-10-12 23:49:04 +00:00
Davide Italiano 9159ce9327 [ELF2/LinkerScript] Implement parsing of OUTPUT_ARCH() command.
Differential Revision:	 http://reviews.llvm.org/D13675

llvm-svn: 250115
2015-10-12 21:50:08 +00:00
Davide Italiano 6836c6182d [ELF2/LinkerScript] Fix OUTPUT_FORMAT directive parsing
Differential Revision:	 http://reviews.llvm.org/D13668

llvm-svn: 250106
2015-10-12 21:08:41 +00:00
Rui Ueyama 1c42afcbdc Remove explicit Twine instantiation if possible.
llvm-svn: 250055
2015-10-12 15:49:06 +00:00
Rui Ueyama 35da9b6e1c ELF2: Implement --as-needed.
This patch adds AsNeeded and IsUsed bool fields to SharedFile. AsNeeded bit
is set if the DSO is enclosed with --as-needed and --no-as-needed. IsUsed
bit is off by default. When we adds a symbol to the symbol table for dynamic
linking, we set its SharedFile's IsUsed bit.

If AsNeeded is set but IsUsed is not set, we don't want to write that
file's SO name to DT_NEEDED field.

http://reviews.llvm.org/D13579

llvm-svn: 249998
2015-10-11 20:59:12 +00:00
Rui Ueyama 52a1509ea7 ELF2: LinkerScript: Interpret -l and = file name prefixes.
In the linker script, -l and = have the same meaning as in the command line.
In addition to that, if a path is not absolute, the path needs to be searched
from the search paths. This patch implements them.

llvm-svn: 249967
2015-10-11 03:28:42 +00:00
Rui Ueyama a47ee68d8e ELF2: Do not leak MemoryBuffers.
llvm-svn: 249962
2015-10-11 01:53:04 +00:00
Rui Ueyama 00f972728b ELF2: LinkerScript: Handle INPUT as a synonym for GROUP.
In our name resolution algorithm, --start-group and --end-group have
no special meaning.

llvm-svn: 249961
2015-10-11 01:31:57 +00:00
Rui Ueyama 31aa1f83de ELF2: LinkerScript: Implement INCLUDE directive.
llvm-svn: 249960
2015-10-11 01:31:55 +00:00
Davide Italiano 68a39a6565 [ELF2] Implement linker script SEARCH_DIR() directive.
llvm-svn: 249707
2015-10-08 17:51:41 +00:00
Denis Protivensky 90c5099e8a [ELF2] Add ENTRY command to the linker script
Set ENTRY as an entry point if -e is not specified.

Differential Revision: http://reviews.llvm.org/D13509

llvm-svn: 249661
2015-10-08 06:48:38 +00:00