Commit Graph

136 Commits

Author SHA1 Message Date
Rui Ueyama 4f8c82281d Refactoring. NFC.
llvm-svn: 352407
2019-01-28 19:11:52 +00:00
Chandler Carruth 2946cd7010 Update the file headers across all of the LLVM projects in the monorepo
to reflect the new license.

We understand that people may be surprised that we're moving the header
entirely to discuss the new license. We checked this carefully with the
Foundation's lawyer and we believe this is the correct approach.

Essentially, all code in the project is now made available by the LLVM
project under our new license, so you will see that the license headers
include that license only. Some of our contributors have contributed
code under our old license, and accordingly, we have retained a copy of
our old license notice in the top-level files in each project and
repository.

llvm-svn: 351636
2019-01-19 08:50:56 +00:00
George Rimar e1fd3f940b [LLD][ELF] - Use auto. NFC.
This addresses the missed review comment.

llvm-svn: 348480
2018-12-06 10:56:11 +00:00
George Rimar f49fe218c2 [LLD][ELF] - Linker script: accept using a file name without a list of sections.
This is a part of
https://bugs.llvm.org/show_bug.cgi?id=39885

Linker script specification says:
"You can specify a file name to include sections from a particular file. You would
do this if one or more of your files contain special data that needs to be at a
particular location in memory."

LLD did not accept this syntax. The patch implements it.

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

llvm-svn: 348463
2018-12-06 08:34:52 +00:00
Haojian Wu ac6e1fb89d Fix a typo.
llvm-svn: 347751
2018-11-28 13:20:39 +00:00
Haojian Wu 00e82a6f66 Fix build error.
llvm-svn: 347750
2018-11-28 13:15:06 +00:00
Simon Atanasyan 69c61200a9 [ELF][MIPS] Handle mips in the OUTPUT_FORMAT directive
Differential Revision: http://reviews.llvm.org/D54920

llvm-svn: 347742
2018-11-28 11:41:20 +00:00
George Rimar c152281632 [LLD][ELF] - Do not crash when parsing the -defsym option from a error state.
When we are in a error state, script parser will not parse the -defsym
expression and hence will not tokenize it. Then ScriptLexer::Pos will be 0
and LLD will assert and crash here:

MemoryBufferRef ScriptLexer::getCurrentMB() {
  assert(!MBs.empty() && Pos > 0); // Bang !

Solution - stop parsing the defsym in a error state. That is consistent
with the regular case (when we parse the linker script).

llvm-svn: 347549
2018-11-26 12:29:56 +00:00
Simon Atanasyan b0486051d2 [ELF] Make TrapInstr and Filler byte arrays. NFC.
The uint32_t type does not clearly convey that these fields are interpreted
in the target endianness. Converting them to byte arrays should make this
more obvious and less error-prone.

Patch by James Clarke

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

llvm-svn: 346893
2018-11-14 21:05:20 +00:00
Galina Kistanova 4ec5d67e49 Handle OUTPUT_FORMAT arguments in quotation marks
llvm-svn: 346068
2018-11-03 05:25:49 +00:00
Sean Fertile 92964e74a5 [PPC64] Handle powerpc64 in OUTPUT_FORMAT.
Differential Revision: https://reviews.llvm.org/D53652

llvm-svn: 345604
2018-10-30 14:37:17 +00:00
Shoaib Meenai c8e9d2b525 [ELF] Handle elf32-littlearm in OUTPUT_FORMAT
We need this to support 32-bit ARM. Add test cases for emulation
handling for this architecture as well.

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

llvm-svn: 344976
2018-10-22 23:56:10 +00:00
Rui Ueyama ea8cd00a1d Add OUTPUT_FORMAT linker script directive support.
This patch adds a support for OUTPUT_FORMAT linker script directive.
Since I'm not 100% confident with BFD names you can use in the directive
for all architectures, I added only a few in this patch. We can add
other names for other archtiectures later.

We still do not support triple-style OUTPUT_FORMAT directive, namely,
OUTPUT_FORMAT(bfdname, big, little). If you pass -EL (little endian)
or -EB (big endian) to the linker, GNU linkers pick up big or little
as a BFD name, correspondingly, so that you can use a single linker
script for bi-endian processor. I'm not sure if we really need to
support that, so I'll leave it alone for now.

Note that -m takes precedence over OUTPUT_FORAMT, but we always parse
a BFD name given to OUTPUT_FORMAT for error checking. You cannot write
an invalid name in the OUTPUT_FORMAT directive.

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

llvm-svn: 344952
2018-10-22 20:50:01 +00:00
Rui Ueyama 2e9d40d5f4 [lld] Add more complete support for the INCLUDE command.
Patch by Ian Tessier.

This change adds INCLUDE support to the MEMORY and SECTION commands, and
to output sections, as per:

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

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

llvm-svn: 344368
2018-10-12 17:07:32 +00:00
George Rimar a46d08ebe6 [LLD][ELD] - Do not reject INFO output section type when used with a start address.
This is https://bugs.llvm.org/show_bug.cgi?id=38625

LLD accept this: 

".stack (INFO) : {", 

but not this:

".stack address_expression (INFO) :"

The patch fixes it.

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

llvm-svn: 340804
2018-08-28 08:39:21 +00:00
Rui Ueyama e262bb1afb Add TARGET(foo) linker script directive.
GNU ld's manual says that TARGET(foo) is basically an alias for
`--format foo` where foo is a BFD target name such as elf64-x86-64.

Unlike GNU linkers, lld doesn't allow arbitrary BFD target name for
--format. We accept only "default", "elf" or "binary". This makes
situation a bit tricky because we can't simply make TARGET an alias for
--target.

A quick code search revealed that the usage number of TARGET is very
small, and the only meaningful usage is to switch to the binary mode.
Thus, in this patch, we handle only TARGET(elf.*) and TARGET(binary).

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

llvm-svn: 339060
2018-08-06 21:29:41 +00:00
Fangrui Song c60f85d073 [ELF] Use search paths for --version-script=
Summary: This behavior matches ld.bfd -Ld --version-script=t.script a.o

Reviewers: ruiu, espindola

Subscribers: emaste, arichardson, llvm-commits

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

llvm-svn: 337969
2018-07-25 21:53:18 +00:00
George Rimar a50054829d [ELF] - Add support for '||' and '&&' in linker scripts.
This is https://bugs.llvm.org//show_bug.cgi?id=37976,
we had no support, but seems someone faced it.

llvm-svn: 336197
2018-07-03 14:02:52 +00:00
George Rimar a582419ac7 [ELF] - Implement linker script OVERLAYs.
This is PR36768.

Linker script OVERLAYs are described in 4.6.9. Overlay Description of the spec:
https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/4/html/Using_ld_the_GNU_Linker/sections.html

They are used to allow output sections which have different LMAs but the same VAs
and used for embedded programming.

Currently, LLD restricts overlapping of sections and that seems to be the most desired
behaviour for defaults. My thoughts about possible approaches for PR36768 are on the bug page,
this patch implements OVERLAY keyword and allows VAs overlapping for sections that within the overlay.

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

llvm-svn: 335714
2018-06-27 08:08:12 +00:00
George Rimar d30a78b3fe [ELF] - Eliminate the AssertCommand.
Currently, LLD supports ASSERT as a separate command.

We support two forms now.

Assign expression-form: . = ASSERT(0x100)
(old GNU ld required it and some scripts in the wild are still using
something like . = ASSERT((_end - _text <= (512 * 1024 * 1024)), "kernel image bigger than KERNEL_IMAGE_SIZE");

Nowadays above is not a mandatory form and command-like form is commonly used:
ASSERT(<expr>, "text);

The return value of the ASSERT is Dot. That was implemented in D30171.
It looks like (2) is just a short version of (1) then.

GNU ld does *not* list ASSERT as a SECTIONS command:
https://sourceware.org/binutils/docs/ld/SECTIONS.html#SECTIONS

Given above we probably can change ASSERT to be an assignment to Dot. 
That makes the rest of the code much simpler. Patch do that.

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

llvm-svn: 330814
2018-04-25 11:16:31 +00:00
Fangrui Song b72daf00f4 [ELF] Increase NextGroupId with --end-group
Reviewers: ruiu, espindola

Subscribers: emaste, arichardson, llvm-commits

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

llvm-svn: 330379
2018-04-19 23:23:23 +00:00
Rui Ueyama 1d92aa7380 Add --warn-backrefs to maintain compatibility with other linkers
I'm proposing a new command line flag, --warn-backrefs in this patch.
The flag and the feature proposed below don't exist in GNU linkers
nor the current lld.

--warn-backrefs is an option to detect reverse or cyclic dependencies
between static archives, and it can be used to keep your program
compatible with GNU linkers after you switch to lld. I'll explain the
feature and why you may find it useful below.

lld's symbol resolution semantics is more relaxed than traditional
Unix linkers. Therefore,

  ld.lld foo.a bar.o

succeeds even if bar.o contains an undefined symbol that have to be
resolved by some object file in foo.a. Traditional Unix linkers
don't allow this kind of backward reference, as they visit each
file only once from left to right in the command line while
resolving all undefined symbol at the moment of visiting.

In the above case, since there's no undefined symbol when a linker
visits foo.a, no files are pulled out from foo.a, and because the
linker forgets about foo.a after visiting, it can't resolve
undefined symbols that could have been resolved otherwise.

That lld accepts more relaxed form means (besides it makes more
sense) that you can accidentally write a command line or a build
file that works only with lld, even if you have a plan to
distribute it to wider users who may be using GNU linkers.  With
--check-library-dependency, you can detect a library order that
doesn't work with other Unix linkers.

The option is also useful to detect cyclic dependencies between
static archives. Again, lld accepts

  ld.lld foo.a bar.a

even if foo.a and bar.a depend on each other. With --warn-backrefs
it is handled as an error.

Here is how the option works. We assign a group ID to each file. A
file with a smaller group ID can pull out object files from an
archive file with an equal or greater group ID. Otherwise, it is a
reverse dependency and an error.

A file outside --{start,end}-group gets a fresh ID when
instantiated. All files within the same --{start,end}-group get the
same group ID. E.g.

  ld.lld A B --start-group C D --end-group E

A and B form group 0, C, D and their member object files form group
1, and E forms group 2. I think that you can see how this group
assignment rule simulates the traditional linker's semantics.

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

llvm-svn: 329636
2018-04-09 23:05:48 +00:00
George Rimar e88b76a989 [ELF] - Reveal more information in -Map file about assignments.
Currently, LLD print symbol assignment commands to the map file,
but it does not do that for assignments that are outside of the section
descriptions. Such assignments can affect the layout though.

The patch implements the following:

* Teaches LLD to print symbol assignments outside of section declaration.
* Teaches LLD to print PROVIDE/HIDDEN/PROVIDE hidden commands.

In case when symbol is not provided, nothing will be printed.

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

llvm-svn: 329272
2018-04-05 11:25:58 +00:00
George Rimar fd11560f6e [ELF] - Linkerscript: support MIN and MAX.
Sample for the OVERLAY command from the spec 
(https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/4/html/Using_ld_the_GNU_Linker/sections.html)
uses MAX command that we do not support currently:

. = 0x1000 + MAX (SIZEOF (.text0), SIZEOF (.text1));

This patch implements support for MIN and MAX.

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

llvm-svn: 328696
2018-03-28 11:33:00 +00:00
Rui Ueyama aa92fca83c Fix linker script operator precedence.
"&" should have higher priority than "|" [1]. Previously, they had
the same priority.

[1] https://sourceware.org/binutils/docs/ld/Operators.html

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

llvm-svn: 327684
2018-03-15 23:12:33 +00:00
George Rimar 84bcabcb86 [ELF] - Show data and assignment commands in the map file.
Patch teaches LLD to print BYTE/SHORT/LONG/QUAD and
location move commands to the map file.

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

llvm-svn: 327612
2018-03-15 09:16:40 +00:00
George Rimar 796684b451 [ELF] - Implement INSERT BEFORE.
This finishes PR35877.

INSERT BEFORE used similar to INSERT AFTER,
it inserts sections before the given target section.

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

llvm-svn: 327378
2018-03-13 09:18:11 +00:00
George Rimar 0984cfa9ab [ELF] - Removed excessive lookup. NFC.
llvm-svn: 327272
2018-03-12 14:15:38 +00:00
George Rimar e3f198d58a [ELF] - Change consume()->expect() in INSERT AFTER parsing.
AFTER keyword is mandatory and consume() was 
used by mistake here. We accepted broken script before
this patch, testcase shows the issue.

llvm-svn: 327260
2018-03-12 12:34:43 +00:00
George Rimar 9e2c8a9db1 [ELF] - Support "INSERT AFTER" statement.
This implements INSERT AFTER in a following way:

During reading scripts it collects all insert statements.
After we done and read all files it inserts statements into script commands list.

With that:
* Rest of code does know nothing about INSERT.
* Approach is straightforward and have no visible limitations.
* It is also easy to support INSERT BEFORE (was seen in clang code once).
* Should work for PR35877 and similar cases.

Cons:
* It assumes we have "main" scripts that describes sections.

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

llvm-svn: 327003
2018-03-08 14:54:38 +00:00
Rui Ueyama 067617f9ba Remove redundant casts.
llvm-svn: 326753
2018-03-05 23:50:45 +00:00
George Rimar 7b91e2133e [ELF] - Report location for div/mod by zero.
"division by zero" or "modulo by zero" are not
very informative errors and even probably confusing
as does not let to know that error is coming from linker script.
Patch adds location reporting.

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

llvm-svn: 326686
2018-03-05 10:02:44 +00:00
George Rimar b068b03793 [ELF] - Don't crash on broken MEMORY declaration.
LLD crashes with broken scripts shown in testcase,
because fails to read memory regon name and accesses
MemoryRegions's element which is nullptr.
Patch fixes it.

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

llvm-svn: 326431
2018-03-01 12:36:01 +00:00
George Rimar c4df670dea [ELF] - Do not remove empty sections that use symbols in expressions.
This is PR36515.

Currenly if we have a script like .debug_info 0 : { *(.debug_info) },
we would not remove this section and keep it in the output.
That does not work, because it is common case for
debug sections to have a zero address expression.
Patch changes behavior so that we remove only sections
that do not use symbols in its expressions.

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

llvm-svn: 326430
2018-03-01 12:27:04 +00:00
Rui Ueyama 39ba31ff50 Add "%" operator to the linker script.
This patch improves compatibility with GNU linkers.

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

llvm-svn: 326348
2018-02-28 18:38:13 +00:00
Rui Ueyama 1d20222a48 Inline a trivial function. NFC.
llvm-svn: 326347
2018-02-28 18:25:00 +00:00
George Rimar 97f4d158bd [ELF] - Fix comment. NFC.
Addresses forgotten comment for D43071.

llvm-svn: 325332
2018-02-16 10:46:50 +00:00
George Rimar 1c08e9f5ce [ELF] - Support COPY, INFO, OVERLAY output sections attributes.
This is PR36298.

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

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

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

llvm-svn: 325331
2018-02-16 10:42:58 +00:00
Rui Ueyama 17324d8b34 Relax the grammar of the version script.
In GNU linkers, the last semicolon is optional. We can't link libstdc++
with lld because of that difference.

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

llvm-svn: 324036
2018-02-01 23:46:17 +00:00
Rafael Espindola 490f0a4da9 Remove MemRegionOffset. NFC.
We can just use a member variable in MemoryRegion.

llvm-svn: 323399
2018-01-25 02:18:00 +00:00
Rafael Espindola 5e9c77624c Handle parsing AT(ADDR(.foo-bar)).
The problem we had with it is that anything inside an AT is an
expression, so we failed to parse the section name because of the - in
it.

llvm-svn: 322801
2018-01-18 01:14:57 +00:00
Rui Ueyama fe148c88da Remove dead code.
parseInt assumed that it could take a negative number literal (e.g.
"-123"). However, such number is in reality already handled as a
unary operator '-' followed by a number literal, so the number
literal is always non-negative. Thus, this code is dead.

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

Should fix PR35684.

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

llvm-svn: 322359
2018-01-12 09:07:35 +00:00
Rafael Espindola 63a4a98e4c Add a comment about subtracting two non abs symbols. NFC.
llvm-svn: 321458
2017-12-26 18:11:14 +00:00
Rui Ueyama c67d6b2da0 Simplify script lexer.
Differential Revision: https://reviews.llvm.org/D41577

llvm-svn: 321453
2017-12-26 10:13:10 +00:00
Rafael Espindola 9cbb6dd1fc Result of subtracting two symbols should be absolute.
When two linker script symbols are subtracted, the result should be absolute.

This is the behavior of binutils' ld.

Patch by Erick Reyes!

llvm-svn: 321390
2017-12-22 21:55:28 +00:00
Rui Ueyama 2017d52b54 Move Memory.{h,cpp} to Common.
Differential Revision: https://reviews.llvm.org/D40571

llvm-svn: 319221
2017-11-28 20:39:17 +00:00
Alexander Richardson 1de78471f5 [ELF] Fall back to search dirs for linker scripts specified with -T
Summary:
This matches the behaviour of ld.bfd:
https://sourceware.org/binutils/docs/ld/Options.html#Options

If scriptfile does not exist in the current directory, ld looks for it in
the directories specified by any preceding '-L' options. Multiple '-T'
options accumulate.

Reviewers: ruiu, grimar

Reviewed By: ruiu, grimar

Subscribers: emaste, llvm-commits

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

llvm-svn: 318655
2017-11-20 15:43:20 +00:00
Petr Hosek 8c7e8cce99 [ELF] Support expressions with -defsym option
Fixes PR34948.

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

llvm-svn: 317396
2017-11-04 02:03:58 +00:00
George Rimar 8c825db25e [ELF] - Linkerscript: fixed non-determinism when handling MEMORY.
When findMemoryRegion do search to find a region for output section it
iterates over MemoryRegions which is DenseMap and so does not
guarantee iteration in insertion order. As a result selected region depends
on its name and not on its definition position
Testcase shows the issue, patch fixes it. Behavior after applying the patch
seems consistent with bfd.

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

llvm-svn: 317307
2017-11-03 08:21:51 +00:00