Commit Graph

487 Commits

Author SHA1 Message Date
Rafael Espindola 093abab817 Don't create a dummy ELF to process a binary file.
Now that it is easy to create input section and symbols, this is
simple.

llvm-svn: 285322
2016-10-27 17:45:40 +00:00
Rafael Espindola 1854a8ebb8 Delete trivial getters. NFC.
llvm-svn: 285190
2016-10-26 12:36:56 +00:00
Rafael Espindola 0e090522c8 Read section headers upfront.
Instead of storing a pointer, store the members we need.

The reason for doing this is that it makes it far easier to create
synthetic sections. It also avoids reading data from files multiple
times., which might help with cross endian linking and host
architectures with slow unaligned access.

There are obvious compacting opportunities, but this already has mixed
results even on native x86_64 linking.

There is also the possibility of better refactoring the code for
handling common symbols, but this already shows that a custom class is
not necessary.

llvm-svn: 285148
2016-10-26 00:54:03 +00:00
Rafael Espindola 58139d1758 Delete getSectionHdr.
We were fairly inconsistent as to what information should be accessed
with getSectionHdr and what information (like alignment) was stored
elsewhere.

Now all section info has a dedicated getter. The code is also a bit
more compact.

llvm-svn: 285079
2016-10-25 16:14:25 +00:00
Eugene Leviant db35fdf70f Don't include PHDRs if linker script doesn't want them
This script below shouldn't include file and program headers
to PT_LOAD segment, because it doesn't have PHDRS and FILEHDR
attributes:

PHDRS { all PT_LOAD; }
SECTIONS { /* list of sections here */ }

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

llvm-svn: 284709
2016-10-20 09:39:09 +00:00
George Rimar a4c7e74d4b [ELF] - Applied clang format. NFC.
llvm-svn: 284705
2016-10-20 08:36:42 +00:00
Rui Ueyama 7c1381a099 Read an expression in the form of "ABSOLUTE(<expr>) op <expr> ...".
Fixes bug 30741.

llvm-svn: 284662
2016-10-19 23:11:21 +00:00
Eugene Leviant ce30b1c78e [ELF] Choose default segment when it is not specified
Linker scripts may specify PHDRS, but not specify section to
segment assignments, i.e:

PHDRS { seg PT_LOAD; }
SECTIONS { 
  .sec1 {} : seg
  .sec2 {} 
}

In such case linker should still choose some segment for .sec2 section.
This patch will add .sec2 to previously opened segments (seg) or to the 
very first PT_LOAD segment, if no section-to-segment assignments has been
made

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

llvm-svn: 284600
2016-10-19 15:04:49 +00:00
George Rimar 95dd718c98 [ELF] - Linkerscript: accept integer values for PHDRS types.
Both gold and ld accepts integers instead of named constants
for PHDRS.
Patch adds support for that.

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

llvm-svn: 284470
2016-10-18 10:49:50 +00:00
Rui Ueyama 83043f237c Rename skip(StringRef) -> consume(StringRef).
skip() and skip(StringRef) were overloaded functions that
have different semantics. This patch rename one of the functions
to avoid function overloading.

llvm-svn: 284396
2016-10-17 16:01:53 +00:00
Justin Bogner 5424e7c7dc ELF: Add a skip() overload to ignore any token
Most functions that return StringRef should check their return values,
so I'm planning on marking StringRef [[nodiscard]]. This requires
splitting up functions like next() that are sometimes just used for
side effects.

llvm-svn: 284363
2016-10-17 06:21:13 +00:00
Justin Bogner 5af1687fd2 ELF: Fix a misuse of Twine::toStringRef
While the toStringRef API almost certainly ends up populating the
SmallString here, the correct way to use this API is to use the return
value.

llvm-svn: 284361
2016-10-17 06:08:48 +00:00
George Rimar 270173f2db [ELF] - Added support of PT_OPENBSD_RANDOMIZE
This is 30646.

PT_OPENBSD_RANDOMIZE
The array element specifies the location and size of a part of the memory image of the program that must be filled with random data before any code in the object is executed. The memory region specified by a segment of this type may overlap the region specified by a PT_GNU_RELRO segment, in which case the intersection will be filled with random data before being marked read-only.

Reference links:
http://man.openbsd.org/OpenBSD-current/man5/elf.5
c494713c45

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

llvm-svn: 284234
2016-10-14 13:02:22 +00:00
George Rimar cc6e567ca3 [ELF] - Implemented -z wxneeded.
-z wxneeded creates a PHDR PT_OPENBSD_WXNEEDED.

PT_OPENBSD_WXNEEDED
The array element specifies that a process executing this file may need to be able to map or protect memory regions as simultaneously executable and writable. If the system is unable or unwilling to permit that for this executable then it may fail immediately. This segment type is meaningful only for executable files and is ignored in other objects.

http://man.openbsd.org/OpenBSD-current/man5/elf.5

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

llvm-svn: 284226
2016-10-14 10:34:36 +00:00
Rui Ueyama 05384080df Support GNU-style ZLIB-compressed input sections.
Previously, we supported only SHF_COMPRESSED sections because it's
new and it's the ELF standard. But there are object files compressed
in the GNU style out there, so we had to support it.

Sections compressed in the GNU style start with ".zdebug_" and
contain different headers than the ELF standard's one. In this
patch, getRawCompressedData is responsible to handle it.

A tricky thing about GNU-style compressed sections is that we have
to rename them when creating output sections. ".zdebug_" prefix
implies the section is compressed. We need to rename ".zdebug_"
".debug" because our output sections are not compressed.
We do that in this patch.

llvm-svn: 284068
2016-10-12 22:36:31 +00:00
Eugene Leviant cc1ba8c7d0 Alternative fix for reloc tareting discarded section
r283984 introduced a problem of too many warning messages being shown
when -ffunction-sections and -fdata-sections were used in conjunction 
with --gc-sections linker flag and debugging information present. This
happens because lot of relocations from .debug_line section may become
invalid in such case. The newer fix doesn't show any warning message but
zeroes OutSec pointer in createInputSectionList() to avoid crash, when
relocations are written

llvm-svn: 284010
2016-10-12 12:31:34 +00:00
Rui Ueyama b224c048b4 Remove trailing whitespace.
llvm-svn: 283786
2016-10-10 18:10:01 +00:00
Eugene Leviant cd8eaf891a [ELF] Don't emit empty PT_LOAD segment
Sometimes the very first PT_LOAD segment, created by lld, can be empty. 
This happens when (all conditions met):

- Linker script is used
- First section in ELF image is not RO
- Not enough space for program headers.

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

llvm-svn: 283760
2016-10-10 15:09:44 +00:00
Eugene Leviant 9257764d61 [ELF] Change argument type for findSections. NFC
llvm-svn: 283742
2016-10-10 11:23:12 +00:00
Eugene Leviant b71d6f7a72 [ELF] Linker script: implement LOADADDR
Differential revision: https://reviews.llvm.org/D24298

llvm-svn: 283429
2016-10-06 09:39:28 +00:00
Rui Ueyama b66260ac17 Remove trailing whitespace.
llvm-svn: 283372
2016-10-05 20:09:50 +00:00
Eugene Leviant a8d12ef853 Do not join sections for relocatable object files
Differential revision: https://reviews.llvm.org/D25232

llvm-svn: 283307
2016-10-05 10:10:45 +00:00
Eugene Leviant cf43f179b1 [ELF] make KEEP command recognize file patterns
Differential revision: https://reviews.llvm.org/D25242

llvm-svn: 283305
2016-10-05 09:36:59 +00:00
Rafael Espindola be6073345e Start linker scripts at 0.
We were implicitly creating space for the headers. That is not the
behaviour of bfd, which requires the script to use SIZEOF_HEADERS. The
difference is important for scripts that don't use SIZEOF_HEADERS and
expect the first section to be at 0.

llvm-svn: 282818
2016-09-30 00:16:11 +00:00
Rafael Espindola 6d91fce526 Don't error if we can't put the header in a PT_LOAD.
If there is not sufficient address space, just give up and don't put
the header in the PT_LOAD.

This matches bfd behaviour and I found at least one script that
depends on having a section at address 0.

llvm-svn: 282750
2016-09-29 18:50:34 +00:00
Petr Hosek 997f8838e5 [ELF] Support -z max-page-size option
This options issupported by both BFD ld and gold and allows
overriding the max page size whose default values are defined by
the target.

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

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

llvm-svn: 282596
2016-09-28 15:20:47 +00:00
George Rimar e38cbab5a4 [ELF] - Linkerscript: implemented BYTE/SHORT/LONG/QUAD commands.
The BYTE, SHORT, LONG, and QUAD commands store one, two, four, and eight bytes (respectively). 
After storing the bytes, the location counter is incremented by the number of bytes
stored.

Previously our scripts handles these commands incorrectly. For example:
SECTIONS  {
  .foo : {
 *(.foo.1)
 BYTE(0x11)
...
We accepted the script above treating BYTE as input section description. 
These commands are used in the wild though.

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

llvm-svn: 282429
2016-09-26 19:22:50 +00:00
George Rimar f28210285b [ELF] - Format. NFC.
llvm-svn: 282395
2016-09-26 11:00:48 +00:00
Rui Ueyama 9c4ac5f26c Combine two StringSwitch::Cases because Cases now can take 6 parameters.
llvm-svn: 282307
2016-09-23 22:22:34 +00:00
Rafael Espindola 65499b9040 Avoid counting sections twice.
We were counting the size of the bss section holding common symbols twice:

    Dot += CurOutSec->getSize();
    flush();

The new code is also simpler as now flush is the only function that
inserts in AlreadyOutputOS, which makes sense since the set hold fully
output sections.

llvm-svn: 282285
2016-09-23 20:10:47 +00:00
Rui Ueyama 0120e3f278 Simplify. NFC.
llvm-svn: 282268
2016-09-23 18:06:51 +00:00
George Rimar 4ebc562052 [ELF] - Linkerscript: accept space between '=' and expression in section filler.
Previously we failed to parse next scripts because disallowed
a space between filler value and '=':

  .text : {
...
  } :text = 0x9090

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

llvm-svn: 282248
2016-09-23 13:29:20 +00:00
George Rimar f34f45fd53 [ELF] - Linkerscript: implement DEFINED() command.
DEFINED(symbol)
Return 1 if symbol is in the linker global symbol table and is defined before
the statement using DEFINED in the script, otherwise return 0.

Can be used to define default values for symbols. Found it in the wild.

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

llvm-svn: 282245
2016-09-23 13:17:23 +00:00
George Rimar c8ccd1f1c5 [ELF] - Linkerscript: Implemented >> and <<
Found this operators used in the wild scripts, for example:

__got2_entries = (_FIXUP_TABLE_ - _GOT2_TABLE_) >>2;
__fixup_entries = (. - _FIXUP_TABLE_)>>2;

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

llvm-svn: 282243
2016-09-23 13:13:55 +00:00
Rafael Espindola 15c579518d Don't move orphan sections past assignments.
This fixes a case where we would produce an unaligned PT_LOAD.

llvm-svn: 282180
2016-09-22 18:05:49 +00:00
Rafael Espindola 1998ee53f0 Simplify. NFC.
With the recent changes there should always be a 1:1 correspondence in
the correct order between OutputSections and OutputSectionCommands.

llvm-svn: 282176
2016-09-22 17:23:53 +00:00
Rafael Espindola 0d4b6d5c98 Avoid duplicated code.
This also fixes the linker script accounting for the ELF header in
some places but not in others.

llvm-svn: 282173
2016-09-22 16:47:21 +00:00
Rafael Espindola 2644208381 Simplify. NFC.
It doesn't matter which direction we rotate and we haven't really
started optimizing the linker script code, so keep this simple.

llvm-svn: 282166
2016-09-22 15:25:21 +00:00
Rafael Espindola 9546fffbfe Handle empty sections with symbol assignments.
Before the symbols were becoming undefined.

llvm-svn: 282159
2016-09-22 14:40:50 +00:00
Rafael Espindola a940e5396b Fix VA computation for tbss.
llvm-svn: 282149
2016-09-22 12:35:44 +00:00
Rafael Espindola 7252ae52cf Handle multiple .tbss sections.
llvm-svn: 282147
2016-09-22 12:00:08 +00:00
Rafael Espindola e746e52c7b Implement ONLY_IF_RO/ONLY_IF_RW like bfd.
The actual logic is to keep the output section if the output section
would have been ro/rw.

This is both simpler and more practical, as the intention is linker
scripts is to always keep of of a pair of ONLY_IF_RO/ONLY_IF_RW.

llvm-svn: 282099
2016-09-21 18:33:44 +00:00
George Rimar 07171f21d1 [ELF] - Linkerscript: support complex section pattern grammar.
This is PR30442.
Previously we were failed to parce complex expressions like:
foo : { *(SORT_BY_NAME(bar) zed) }

Main idea of patch that globs and excludes can be wrapped in a SORT.
There is a difference in semanics of ld/gold:
ld likes:
*(SORT(EXCLUDE_FILE (*file1.o) .foo.1))

gold likes:
*(EXCLUDE_FILE (*file1.o) SORT(.foo.1))

Patch implements ld grammar, complex expressions like 
next is not a problem anymore:
.abc : { *(SORT(.foo.* EXCLUDE_FILE (*file1.o) .bar.*) .bar.*) }


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

llvm-svn: 282078
2016-09-21 15:56:44 +00:00
Eugene Leviant 2506cb4ddb Linker script: Fix bug with several .bss
When final image has several .bss sections, lld fails
because second .bss always has zero VA. This causes 
link error "Not enough space for ELF and program headers"

llvm-svn: 282067
2016-09-21 11:29:28 +00:00
George Rimar 601e989879 [ELF] - Linkerscript: reimplement readSectionExcludes()
It is not only a bit more straightforward now, but also next 2 issues are solved:

* It just crashed on ".foo : { *(EXCLUDE_FILE (*file1.o)) }" before.
* It accepted multiple EXCLUDE_FILEs in a row.

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

llvm-svn: 282060
2016-09-21 08:53:21 +00:00
Rafael Espindola b6b8f6c308 Revert "Revert "Only restrict order if both sections are in the script.""
This reverts commit r282021, bringing back r282015.

The problem was that the comparison function was not a strict weak
ordering anymore, which this patch fixes.

Original message:

Only restrict order if both sections are in the script.

This matches gold and bfd behavior and is required to handle some scripts.

The script has to assume where PT_LOADs start in order to align that
spot. If we don't allow section it doesn't know about to move to the
middle, we can need more PT_LOADs and those will not be aligned.

llvm-svn: 282035
2016-09-20 22:43:15 +00:00
Rafael Espindola bf04708e11 Revert "Only restrict order if both sections are in the script."
This reverts commit r282015. It broke some bots.

llvm-svn: 282021
2016-09-20 21:28:19 +00:00
Rafael Espindola 145569df64 Only restrict order if both sections are in the script.
This matches gold and bfd behavior and is required to handle some scripts.

The script has to assume where PT_LOADs start in order to align that
spot. If we don't allow section it doesn't know about to move to the
middle, we can need more PT_LOADs and those will not be aligned.

llvm-svn: 282015
2016-09-20 20:54:39 +00:00
Rui Ueyama ee92470969 Simplify SORT and --sort-section command line option handling.
Differential Revision: https://reviews.llvm.org/D24685

llvm-svn: 282006
2016-09-20 19:42:41 +00:00
Rafael Espindola 6d38e4dbe1 Remove empty section commands.
We were already not creating them, and with this other parts of the
code don't have to worry about them.

llvm-svn: 281968
2016-09-20 13:12:07 +00:00
Rafael Espindola f135f0ec1d Remove unnecessary const_canst. NFC.
llvm-svn: 281901
2016-09-19 13:33:38 +00:00
George Rimar b31dd37005 [ELF] - LinkerScript: Add workaround for gcc 6.2.0 failure w/auto
Will Dietz found and reported that lld does not compile with gcc 6.2.0,
more details https://llvm.org/bugs/show_bug.cgi?id=30438

And confirmed this change fixes the issue.

llvm-svn: 281900
2016-09-19 13:27:31 +00:00
George Rimar 194470cd11 [ELF] - Fix comment. NFC.
llvm-svn: 281836
2016-09-17 19:21:05 +00:00
George Rimar af03be19f9 [ELF] - Added comments. NFC.
llvm-svn: 281835
2016-09-17 19:17:25 +00:00
George Rimar 8c658bf824 [ELF] - SEGMENT_START's default argument can be an expression
Our implementation supported integer value previously.
ld can use expression,
for example, it is OK to write
 . = SEGMENT_START("foobar", .);

Patch implements that.

llvm-svn: 281831
2016-09-17 18:14:56 +00:00
George Rimar dfbbbc86a1 [ELF] Linkerscript: fixed bug about commands processing.
It was possible situation about some commands just were not processed
(were skipped) because of a bug appeared when constraint checking used.

Testcase is attached.

llvm-svn: 281818
2016-09-17 09:50:10 +00:00
Rui Ueyama 4dc07becd0 Use named struct instead of unnamed std::pair.
It is important to give members names for readability.

llvm-svn: 281803
2016-09-17 02:23:40 +00:00
Rui Ueyama 3ff27f49cd Define a versatile utility function and use it instead of a single purpose one.
llvm-svn: 281802
2016-09-17 02:15:28 +00:00
Rui Ueyama 027a9e8787 Remove unnecessary namespace specifiers.
llvm-svn: 281801
2016-09-17 02:10:15 +00:00
Rafael Espindola 373343bd5b Try to fix a few bots.
llvm-svn: 281794
2016-09-16 22:47:34 +00:00
Rafael Espindola aab6d5c52a Put SHF_ALLOC sections first, even with linker scripts.
This matches gold and bfd, and is pretty much required by some linker
scripts. They end with commands like

foo   0 : { *(bar) }

if we put any SHF_ALLOC sections after they can have an address that
is too low.

llvm-svn: 281778
2016-09-16 21:29:07 +00:00
Rui Ueyama b2a0abdf0e Rename SortSectionPolicy::IgnoreConfig to None.
Because it corresponds to SORT_NONE. None was renamed Default.

llvm-svn: 281776
2016-09-16 21:14:55 +00:00
Rafael Espindola 7c3ff2eb58 Only process commands in a ONLY_IF_RO if it matches.
This matches bfd behavior. It also makes future changes simpler as we
don't have to worry about ignoring these commands in multiple places

llvm-svn: 281775
2016-09-16 21:05:36 +00:00
Rafael Espindola e71a3f8ae1 Fix constraint checking in ONLY_IF_RO.
We have to look at all the relevant input sections at once.

llvm-svn: 281772
2016-09-16 20:34:02 +00:00
George Rimar be394db376 [ELF] - Implemented --sort-section cmd line option and SORT_NONE script command.
This fixes Bug 30385 - SORT_NONE not implemented,

`SORT_NONE' disables section sorting by ignoring the command line
section sorting option.

That is why this patch also implements --sort-section option.

Description of sorting rules
available at https://sourceware.org/binutils/docs/ld/Input-Section-Wildcards.html 

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

llvm-svn: 281771
2016-09-16 20:21:55 +00:00
George Rimar 395281cfc3 Recommit r281721 "[ELF] - Linkerscript: implement EXCLUDE_FILE in the middle of a input section description."
With fix for 2 bots. Details about the fix performed is on a review page.

Initial commit message:
This is PR30387:

From PR description:
We fail to parse

SECTIONS
{
  foo :
  {
    *(sec0 EXCLUDE_FILE (zed1.o) sec1  EXCLUDE_FILE (zed2.o) sec2 )
  }
}
The semantics according to bfd are:

Include sec1 from every file but zed1.o
Include sec2 from every file but zed2.o
Include sec0 from every file

Patch implements the support.

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

llvm-svn: 281754
2016-09-16 17:42:10 +00:00
Eugene Leviant 20d031948e Improve handling ASSERT outside SECTIONS block
Differential revision: https://reviews.llvm.org/D24450

llvm-svn: 281740
2016-09-16 15:30:47 +00:00
Rafael Espindola d31907957a Change how we compute offsets with linker scripts.
This fixes pr30367, but more importantly, it changes how we compute offsets.

Now offset computation in a walk over linker script commands, like the
rest of assignAddresses. IMHO this is simpler to understand and if we
ever have to create multiple outputsections or chunks to change how we
handle test/ELF/linkerscript/alternate-sections.s it should be easier
to do it.

llvm-svn: 281736
2016-09-16 15:10:23 +00:00
George Rimar ceae630c9b Reverted r281721 ("[ELF] - Linkerscript: implement EXCLUDE_FILE in the middle of a input section description.").
It broke build bot:
http://lab.llvm.org:8011/builders/lld-x86_64-darwin13/builds/27508

llvm-svn: 281723
2016-09-16 13:30:18 +00:00
George Rimar 4906c7f5c4 [ELF] - Linkerscript: implement EXCLUDE_FILE in the middle of a input section description.
This is PR30387:

From PR description:
We fail to parse

SECTIONS
{
  foo :
  {
    *(sec0 EXCLUDE_FILE (zed1.o) sec1  EXCLUDE_FILE (zed2.o) sec2 )
  }
}
The semantics according to bfd are:

Include sec1 from every file but zed1.o
Include sec2 from every file but zed2.o
Include sec0 from every file

Patch implements the support.

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

llvm-svn: 281721
2016-09-16 13:07:02 +00:00
Rafael Espindola 4ec013ac83 Error out instead of producing a corrupt PT_LOAD.
What bfd and gold do is give up in putting the headers in the PT_LOAD
and just start the PT_LOAD in the second page.

llvm-svn: 281660
2016-09-15 21:22:11 +00:00
George Rimar 575208cabd [ELF] - Linkerscript: implemented SORT_BY_INIT_PRIORITY.
This is PR30386,

SORT_BY_INIT_PRIORITY is a keyword can be used to sort sections by numerical value of the
GCC init_priority attribute encoded in the section name.

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

llvm-svn: 281646
2016-09-15 19:15:12 +00:00
Rafael Espindola 3adbbc3891 Check the return of getInteger.
llvm-svn: 281608
2016-09-15 13:36:44 +00:00
Rafael Espindola 97bdc7220b Handle arbitrary expressions in DATA_SEGMENT_RELRO_END.
llvm-svn: 281521
2016-09-14 19:14:01 +00:00
Rafael Espindola be94e1b630 Move helper function higher in the file. NFC
This just makes a followup patch easier to read.

llvm-svn: 281482
2016-09-14 14:32:08 +00:00
Eugene Leviant e05336ffa1 [ELF] Replace HasContents with HasSections. NFC
llvm-svn: 281449
2016-09-14 08:32:36 +00:00
Rui Ueyama 38dbd3eea9 Simplify InputFile ownership management.
Previously, all input files were owned by the symbol table.
Files were created at various places, such as the Driver, the lazy
symbols, or the bitcode compiler, and the ownership of new files
was transferred to the symbol table using std::unique_ptr.
All input files were then free'd when the symbol table is freed
which is on program exit.

I think we don't have to transfer ownership just to free all
instance at once on exit.

In this patch, all instances are automatically collected to a
vector and freed on exit. In this way, we no longer have to
use std::unique_ptr.

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

llvm-svn: 281425
2016-09-14 00:05:51 +00:00
Rafael Espindola 10897f1807 Enable merging of SHF_MERGE sections with linker scripts.
This also fixes the related problem of non SHF_MERGE sections with
different flags not being merged.

Fixes pr30355.

llvm-svn: 281338
2016-09-13 14:23:14 +00:00
Rafael Espindola 28c1597ad9 Refactor duplicated code. NFC.
llvm-svn: 281329
2016-09-13 13:00:06 +00:00
Rafael Espindola 540893a2e2 Delete dead code.
We already handle this is createSections.

llvm-svn: 281320
2016-09-13 11:28:22 +00:00
Rafael Espindola 7bd37870bc Simplify handling of /DISCARD/. NFC.
llvm-svn: 281222
2016-09-12 16:05:16 +00:00
George Rimar cd574a5e46 Recommit [ELF] - Versionscript: do not treat non-wildcarded names as wildcards.
Fixed code that was not checked before on windows for me, because of testcases that are
disabled on that platform atm.

Inital commit message:
"[ELF] - Versionscript: do not treat non-wildcarded names as wildcards."
Previously we incorrectly handled cases when symbol name in extern c++ tag
was enclosed in quotes. Next case was treated as wildcard:

GLIBCXX_3.4 {                                                                   
    extern "C++" {  
    "aaa*"
   }
But it should have not. Quotes around aaa here means that we should have do exact
name matching.
That is PR30268 which has name with pointer is interpreted as wildcard by lld:

extern "C++" {                                                              
    "operator delete[](void*)";

Patch fixes the issue.

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

llvm-svn: 281049
2016-09-09 14:35:36 +00:00
George Rimar 6368525eea Revert r281045, it broke BB.
Broken BB:
http://lab.llvm.org:8011/builders/lld-x86_64-darwin13/builds/27211

llvm-svn: 281046
2016-09-09 14:16:00 +00:00
George Rimar b5e77c47fb Recommit [ELF] - Versionscript: do not treat non-wildcarded names as wildcards.
Fixed code that was not checked by testcases that are disabled on windows.

Inital commit message:
"[ELF] - Versionscript: do not treat non-wildcarded names as wildcards."
Previously we incorrectly handled cases when symbol name in extern c++ tag
was enclosed in quotes. Next case was treated as wildcard:

GLIBCXX_3.4 {                                                                   
    extern "C++" {  
    "aaa*"
   }
But it should have not. Quotes around aaa here means that we should have do exact
name matching.
That is PR30268 which has name with pointer is interpreted as wildcard by lld:

extern "C++" {                                                              
    "operator delete[](void*)";

Patch fixes the issue.

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

llvm-svn: 281045
2016-09-09 13:58:07 +00:00
George Rimar 08b1862caf [ELF] - Revert r281038 to see if that unbreaks build bot.
http://lab.llvm.org:8011/builders/llvm-clang-lld-x86_64-scei-ps4-ubuntu-fast/builds/19703

llvm-svn: 281041
2016-09-09 12:56:55 +00:00
George Rimar d220384376 [ELF] - Versionscript: do not treat non-wildcarded names as wildcards.
Previously we incorrectly handled cases when symbol name in extern c++ tag
was enclosed in quotes. Next case was treated as wildcard:

GLIBCXX_3.4 {                                                                   
    extern "C++" {  
    "aaa*"
   }
But it should have not. Quotes around aaa here means that we should have do exact
name matching.
That is PR30268 which has name with pointer is interpreted as wildcard by lld:

extern "C++" {                                                              
    "operator delete[](void*)";

Patch fixes the issue.

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

llvm-svn: 281038
2016-09-09 12:22:28 +00:00
Eugene Leviant 56b21c869e Linker script: implement AT [ (address) ] for PHDR
Differential revision: https://reviews.llvm.org/D24340

llvm-svn: 281024
2016-09-09 09:46:16 +00:00
Rui Ueyama 86c5fb8296 Fix bug in -nostdlib.
We still have to skip a token inside SEARCH_DIR() when -nostdlib is
specified. Previuosly, we didn't skip it, so it caused a parse error.

llvm-svn: 281001
2016-09-08 23:26:54 +00:00
Rafael Espindola c0028d3d3b Reduce templating. NFC.
llvm-svn: 280986
2016-09-08 20:47:52 +00:00
Rafael Espindola 042a3f209b Compute section names only once.
This simplifies error handling as there is now only one place in the
code that needs to consider the possibility that the name is
corrupted. Before we would do it in every access.

llvm-svn: 280937
2016-09-08 14:06:08 +00:00
Eugene Leviant 36fac7f0d0 Linker script: implement ALIGNOF
Differential revision: https://reviews.llvm.org/D24141

llvm-svn: 280915
2016-09-08 09:08:30 +00:00
George Rimar 6c55f0e35e [ELF] - Apply clang-format to LinkerScript.cpp, NFC.
llvm-svn: 280911
2016-09-08 08:20:30 +00:00
George Rimar 884e786d38 [ELF] - Linkerscript: simplify access to templated methods from parser.
Previous way of accessing templated methods was a bit bulky,
Patch introduces small interface based solution.

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

llvm-svn: 280910
2016-09-08 08:19:13 +00:00
George Rimar a14b13d867 [ELF] - Linkerscript: create multiple output sections for inputs with different attributes.
Previously we combined sections by name if linkerscript was used.
For that we had to disable SHF_MERGE handling temporarily, but then
found that implementing it properly will require additional complexity layers like
subsections or something.
At the same time looks we can live with multiple output sections approach for now.
That patch do this change.

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

llvm-svn: 280801
2016-09-07 10:46:07 +00:00
Eugene Leviant db741e7203 Support ABSOLUE keyword in symbol assignments
This patch allows making section defined symbols absolute:
.foo : {
  begin_foo = ABSOLUTE(.);
  *(.foo)
}

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

llvm-svn: 280788
2016-09-07 07:08:43 +00:00
Rui Ueyama adcdb664cb Simplify a boolean expression by using the De Morgan's law.
llvm-svn: 280766
2016-09-06 22:50:48 +00:00
Rui Ueyama 25150e8b0d Add a comment.
llvm-svn: 280724
2016-09-06 17:46:43 +00:00
George Rimar ff1f29e0f6 [ELF] - Linkerscript: implemented FILL command as alias for =fillexpr
Patch implements FILL just as alias for =fillexpr. 
This allows to make implementation much shorted and simpler than D24186.

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

llvm-svn: 280708
2016-09-06 13:51:57 +00:00
Simon Atanasyan eaeafb2b4f [ELF] PR30221 - linker script expression parser does not accept '~'
The patch adds support for both '-' and '~' unary expressions. Also it
brings support for signed numbers is expressions.

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

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

llvm-svn: 280546
2016-09-02 21:54:35 +00:00
George Rimar c91930a17f [ELF] - Use std::regex instead of hand written logic in elf::globMatch()
Use std::regex instead of hand written matcher.

Patch based on code and ideas of Rui Ueyama.

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

llvm-svn: 280544
2016-09-02 21:17:20 +00:00
Rui Ueyama 6c7ad13f89 Add -nostdlib.
llvm-svn: 280528
2016-09-02 19:20:33 +00:00
Rui Ueyama a27eeccade Dispatch without hash table lookup.
Cmd used to be the single central place to dispatch. It is not longer
the case because we have a logic for readProvideOrAssignment().
This patch removes the hash table so that evrything is in a single
function. This is slightly verbose but should improve readability.

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

llvm-svn: 280524
2016-09-02 18:52:41 +00:00
Rui Ueyama 626e0b08ac Add comments.
llvm-svn: 280515
2016-09-02 18:19:00 +00:00
George Rimar 9f2f7ad98b [ELF] - Linkerscript: add support for suffixes in numbers.
Both bfd and gold accept:
foo = 1K;
bar = 1M;
zed = 1H;

And lowercase forms: k, m, h.
Patch adds support for that.

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

llvm-svn: 280494
2016-09-02 16:01:42 +00:00
Eugene Leviant 97403d15ee Eliminate LayoutInputSection class
Previously we used LayoutInputSection class to correctly assign
symbols defined in linker script. This patch removes it and uses
pointer to preceding input section in SymbolAssignment class instead.

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

llvm-svn: 280348
2016-09-01 09:55:57 +00:00
Rui Ueyama 95769b4a29 Linker script: support VERSION command.
Summary:
VERSION commands define symbol versions. The grammar of the
commnad is as follows

  VERSION { version-script-commands }

where version-script-commands is

  [ name ] { version-definitions }.

Note that we already support version-script-commands because
it is being used for version script command.

This patch is based on George's patch https://reviews.llvm.org/D23609

Reviewers: grimar

Subscribers: llvm-commits

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

llvm-svn: 280284
2016-08-31 20:03:54 +00:00
Petr Hosek e5d3ca5031 [ELF] Linkerscript: define symbols outside SECTIONS
Symbol assignments outside of SECTIONS command need to be created
even when SECTIONS command is not used.

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

llvm-svn: 280252
2016-08-31 15:31:17 +00:00
George Rimar 20b6598c10 [ELF] - Remove VersionScriptParser class and move the members to ScriptParser
Patch removes VersionScriptParser class and moves the members to ScriptParser
It opens road for implementation of VERSION linkerscript command.

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

llvm-svn: 280212
2016-08-31 09:08:26 +00:00
Eugene Leviant 20889c51b7 Allow adding start/end symbols to any section
Allows adding start and/or end symbols to special output sections,
like .eh_frame_hdr, which aren't lists of regular input sections. 

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

llvm-svn: 280205
2016-08-31 08:13:33 +00:00
George Rimar 96659df09c [ELF] - Linkerscript: implemented ADDR command.
ADDR(section)
Return the absolute address (the VMA) of the named section.

Used in the wild, eg.: https://searchcode.com/file/53617342/arch/x86/kernel/vmlinux.lds.S

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

llvm-svn: 280070
2016-08-30 09:54:01 +00:00
George Rimar a2496cbed4 [ELF] - Fix for: bug 29115 - linkerscript does not support non-wildcard filename spec.
FreeBSD/mips script has non-wildcard filename specifications:
.text :
{
 start.o(.text*)

Patch adds support for that, this is PR29115.

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

llvm-svn: 280069
2016-08-30 09:46:59 +00:00
Rui Ueyama 2c8f1f048c Make lld actually compatible with gold in terms of filler handling.
GNU gold handles output section fillers as 32-bit values.
This patch makes LLD compatible with that behavior.

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

llvm-svn: 280018
2016-08-29 22:01:21 +00:00
Rafael Espindola cc3dd629ee Add support for '|' in expressions.
llvm-svn: 279480
2016-08-22 21:33:35 +00:00
Rui Ueyama 464daadc3d Do not add .interp, .dynamic nor .eh_frame_hdr to segments just by type.
Summary:
We previously added these output sections to segments just by type.
Therefore, if there's a PHDRS command like this

  PHDRS {
    headers PT_PHDR PHDRS;
    interp PT_INTERP;
  }

  SECTIONS {
    . = SIZEOF_HEADERS;
    .interp : { *(.interp) } :text
  }

then .interp was added to "interp" segment even though the linker
is not instructed to do so by SECTIONS command. This patch removes
the default behavior to simplify.

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

llvm-svn: 279414
2016-08-22 04:55:20 +00:00
George Rimar e1937bb524 [ELF] - Give automatically generated __start_* and __stop_* symbols default visibility.
This patch is opposite to D19024, which made this symbols to be hidden by default.

Unfortunately FreeBSD loader wants to see
start_set_modmetadata_set/stop_set_modmetadata_set in the dynamic symbol table. 
They were not placed there because had hidden visibility.

Patch makes them to have default visibility again.

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

llvm-svn: 279262
2016-08-19 15:36:32 +00:00
George Rimar db24d9c3e8 [ELF] - Linkerscript: implemented SUBALIGN() command.
You can force input section alignment within an output section by using SUBALIGN. The
value specified overrides any alignment given by input sections, whether larger or smaller.

SUBALIGN is used in many projects in the wild.

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

llvm-svn: 279256
2016-08-19 15:18:23 +00:00
Eugene Leviant 3f675e38bd [ELF] Don't add input sections twice when linker script is used
llvm-svn: 279036
2016-08-18 07:27:37 +00:00
Petr Hosek 0df80bef96 [ELF] Linkerscript: support assignment outside SECTIONS
We only support assignments inside SECTIONS, but this does not match
the behavior of GNU linker which also allows them outside SECTIONS.
The only restriction on assignments outside SECTIONS is that they
cannot reference . (they have to be absolute expressions).

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

llvm-svn: 279033
2016-08-18 04:34:27 +00:00
Rui Ueyama 6ad7dfcc1e Merge readAt and readAlign.
Now that they are identical.

llvm-svn: 278953
2016-08-17 18:59:16 +00:00
George Rimar 545afafb70 [ELF] - Linkerscript: make readAt() signature to be consistent with others read*(). NFC.
llvm-svn: 278931
2016-08-17 14:48:17 +00:00
George Rimar 206fffa192 Attemp to fix linux build bot after r278911 ("[ELF] - linkerscript AT keyword (in output section description) implemented.")
http://lab.llvm.org:8011/builders/lld-x86_64-darwin13/builds/26289/steps/build_Lld/logs/stdio

llvm-svn: 278914
2016-08-17 08:16:57 +00:00
George Rimar 8ceadb38a8 [ELF] - linkerscript AT keyword (in output section description) implemented.
The linker will normally set the LMA equal to the VMA. 
You can change that by using the AT keyword.
The expression lma that follows the AT keyword specifies 
the load address of the section.

Patch implements this keyword.

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

llvm-svn: 278911
2016-08-17 07:44:19 +00:00
Michael J. Spencer e2cc07bc0b [ELF] Set MAXPAGESIZE to 2MiB on x86-64 to match bfd and gold.
The FreeBSD kernel relies on this behavior to not overwrite the boot loader.

llvm-svn: 278889
2016-08-17 02:10:51 +00:00
Eugene Leviant f9bc3bd2cf [ELF] Ignore .interp section in case linker script specifies PHDRS without PT_INTERP
llvm-svn: 278781
2016-08-16 06:40:58 +00:00
Petr Hosek a35e39ca2e [ELF] Linkerscript: support hidden symbols assignments
This add support for HIDDEN command which can be used to define
a symbol that will be hidden and won't be exported.

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

llvm-svn: 278770
2016-08-16 01:11:16 +00:00
Eugene Leviant b6f1bb13ae [ELF] Linkerscript: fix bug in assignOffsets (check Sym for non-null)
llvm-svn: 278663
2016-08-15 09:19:51 +00:00
George Rimar 8f66df92ba [ELF] - Linkerscript: fix VA value assigned to sections when using constraints.
Previously we searched output section by name to assign VA. That did not
work in the case when multiple output sections with different constraints were defined in script.
Testcase shows the possible issue scenario, patch fixes the issue.

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

llvm-svn: 278561
2016-08-12 20:38:20 +00:00
George Rimar b6c52e8dfa [ELF] - Remove excessive loop in LinkerScript<ELFT>::assignAddresses()
After 278461 "Create only one section for a name in LinkerScript."
this loop is excessive. 
Patch also reorders code slightly to use early return.

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

llvm-svn: 278554
2016-08-12 19:32:45 +00:00
George Rimar 06ae683620 [ELF] - Linkerscript: reimplemented output sections constrains matching functionality.
Previously filtering that was used worked incorrectly. 
For example for next script it would just remove both sections completely:

SECTIONS { 
. = 0x1000;
.aaa : ONLY_IF_RW { *(.aaa.*) } 
. = 0x2000;
.aaa : ONLY_IF_RO { *(.aaa.*) } 
}

Patch fixes above issues and adds testcase showing the issue. Testcase is a subset of
FreeBSD script which has:

.eh_frame       : ONLY_IF_RO { KEEP (*(.eh_frame)) }
...
.eh_frame       : ONLY_IF_RW { KEEP (*(.eh_frame)) }

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

llvm-svn: 278486
2016-08-12 09:07:57 +00:00
Rui Ueyama 4f7500bfc2 Rename getSizeOfHeaders -> getHeaderSize.
We have getSectionSize for SIZEOF command. So, I think
getHeaderSize is a better name for SIZEOF_HEADERS.

llvm-svn: 278470
2016-08-12 04:00:22 +00:00
Rui Ueyama 2ab5f73d5a Early continue. NFC.
llvm-svn: 278465
2016-08-12 03:33:04 +00:00
Rui Ueyama 0c70d3ccb7 Simplify LinkerScript<ELFT>::createSections.
Previously, we were setting LayoutInputSection's OutputSection member
in createSections. Because when we create LayoutInputSectinos, we
don't know the output section for them, so we backfilled the member
in the function. This patch moves the code to backfill it to assignOffsets.

llvm-svn: 278464
2016-08-12 03:31:09 +00:00
Rui Ueyama 7ad9d6d2f0 Remove excessive parentheses.
llvm-svn: 278462
2016-08-12 03:25:25 +00:00
Rui Ueyama 0b9ce6a48a Create only one section for a name in LinkerScript.
Previously, we created two or more output sections if there are
input sections with the same name but with different attributes.
That is a wrong behavior. This patch fixes the issue.

One thing we need to do is to merge output section attributes.
Currently, we create an output section based on the first input
section's attributes. This may make a wrong output section
attributes. What we need to do is to bitwise-OR attributes.
We'll do it in a follow-up patch.

llvm-svn: 278461
2016-08-12 03:16:56 +00:00
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