Commit Graph

779 Commits

Author SHA1 Message Date
Fangrui Song 3deff86657 [ELF] Respect NonAlloc when copying flags from the previous sections
Summary:
If the output section contains only symbol assignments, we copy flags
from the previous sections. Don't set SHF_ALLOC if NonAlloc is true.

We also have to change the type from SHT_NOBITS to SHT_PROGBITS.
In ld.bfd, bfd_elf_get_default_section_type maps non-alloctable sections to SHT_PROGBITS.
Non-alloctable SHT_NOBITS sections do not make sense.

Fixes PR38626

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

llvm-svn: 358650
2019-04-18 09:22:05 +00:00
Rui Ueyama 14ef9b30b6 lld: elf: Fix sections with explict addresses in regions
Patch by Gabriel Smith.

The address for a section would be evaluated before the region was
switched to. Because of this, the position within the region would not
be updated. After the region is swapped to the dot would be set to the
out of date position within the region, undoing the section address
evaluation.

To fix this, the region is swapped to before the section's address is
evaluated. As part of the fallout of this, expandMemoryRegions needed
to be gated in setDot on the condition that the evaluated address is
less than the dot. This is for the case where sections are not listed
from lowest address to highest address.

Finally, a test for the case where sections are listed "out of order"
was added.

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

llvm-svn: 358638
2019-04-18 02:32:12 +00:00
Peter Collingbourne 2da7b32684 ELF: Simplify. NFCI.
We don't need to take a slice of SectionCommands in addOrphanSections()
because it is not modified until the end of the function.

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

llvm-svn: 355954
2019-03-12 19:19:23 +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 1f958ed269 [LLD][ELF] - Support discarding the .dynamic section.
This is a part of https://bugs.llvm.org/show_bug.cgi?id=39810.

Seems it turns out that supporting /DISCARD/ for the .dynamic section with the
linker script is something we can do easily. The patch does this.

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

llvm-svn: 348749
2018-12-10 09:24:49 +00:00
George Rimar ad667661c4 [ELF] - Allow discarding .dynsym from the linker script.
This is a part of https://bugs.llvm.org/show_bug.cgi?id=39810.
The patch allows discarding the .dynsym section using linker script.

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

llvm-svn: 348748
2018-12-10 09:13:36 +00:00
George Rimar 4af28e46ca [LLD][ELF] - Support discarding .dynstr section.
This is a part of https://bugs.llvm.org/show_bug.cgi?id=39810.
The patch allows discarding the .dynstr section using linker script.

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

llvm-svn: 348746
2018-12-10 09:07:30 +00:00
Martell Malone 15b6c453b8 [ELF] Allow discarding of .rela.plt
When linking the linux kernel on ppc64le

ld.lld -EL -m elf64lppc -Bstatic --orphan-handling=warn --build-id -o
.tmp_vmlinux1 -T ./arch/powerpc/kernel/vmlinux.lds --whole-archive
built-in.a --no-whole-archive --start-group lib/lib.a --end-group
ld.lld: error: discarding .rela.plt section is not allowed

The linker script discards with the following matches
*(.glink .iplt .plt .rela* .comment)

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

llvm-svn: 348258
2018-12-04 12:37:56 +00:00
Rui Ueyama f3fad55787 Remove `Type` parameter from SymbolTable::insert(). NFC.
`Type` parameter was used only to check for TLS attribute mismatch,
but we can do that when we actually replace symbols, so we don't need
to type as an argument. This change should simplify the interface of
the symbol table a bit.

llvm-svn: 344394
2018-10-12 18:29:18 +00:00
Rui Ueyama 4e247522ac Reset input section pointers to null on each linker invocation.
Previously, if you invoke lld's `main` more than once in the same process,
the second invocation could fail or produce a wrong result due to a stale
pointer values of the previous run.

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

llvm-svn: 343009
2018-09-25 19:26:58 +00:00
Peter Collingbourne 6556e6b929 ELF: Don't examine values of linker script symbols during ICF.
These symbols are declared early with the same value, so they otherwise
appear identical to ICF.

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

llvm-svn: 340998
2018-08-29 23:43:38 +00:00
George Rimar e0ff432a48 [ELF] - Remove dead code from LinkerScript::assignOffsets(). NFC-ihope.
Some parts of the code changed are a bit old. I found traces in 2016.

Initiall commits has test cases and perhaps reasonable comments.
For example, we had segfaults earlier and had the code to fix them.

Now, in 2018, I think it is excessive to have these parts, because
we do not have segfaults and our code was changed a lot (softly saying).

I reviewed the current sources and I think that at this point of the
execution flow, we should never face with
the conditions checked and so I removing them in this patch.

This helps to cleanup the code.

llvm-svn: 339003
2018-08-06 10:44:17 +00:00
George Rimar 6fe30771aa [LLD][ELF] - Simplify. NFC.
isHeaderSection can be useful I believe,
but probably not right now and not
for this case.

llvm-svn: 338699
2018-08-02 10:59:28 +00:00
George Rimar acf8cef80f [LLD] Do not overwrite LMAOffset of PT_LOAD header
Patch by Konstantin Schwarz!

If more than a single output section is added to a PT_LOAD header,
only the first section should set the LMAOffset of the segment.
Otherwise, we get a load-address overlap error

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

llvm-svn: 338697
2018-08-02 10:45:46 +00:00
George Rimar add69e9c19 [LLD] Only increase LMARegion if different from MemRegion
Patch by Konstantin Schwarz!

If both the MemRegion and LMARegion are set for an output section in
a linker script, we should only increase the LMARegion if it is
different from the MemRegion. Otherwise, we reserve the memory twice.

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

llvm-svn: 338684
2018-08-02 08:13:56 +00:00
Rui Ueyama 11479daf2f lld: add experimental support for SHT_RELR sections.
Patch by Rahul Chaudhry!

This change adds experimental support for SHT_RELR sections, proposed
here: https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg

Pass '--pack-dyn-relocs=relr' to enable generation of SHT_RELR section
and DT_RELR, DT_RELRSZ, and DT_RELRENT dynamic tags.

Definitions for the new ELF section type and dynamic array tags, as well
as the encoding used in the new section are all under discussion and are
subject to change. Use with caution!

Pass '--use-android-relr-tags' with '--pack-dyn-relocs=relr' to use
SHT_ANDROID_RELR section type instead of SHT_RELR, as well as
DT_ANDROID_RELR* dynamic tags instead of DT_RELR*. The generated
section contents are identical.

'--pack-dyn-relocs=android+relr --use-android-relr-tags' enables both
'--pack-dyn-relocs=android' and '--pack-dyn-relocs=relr': lld will
encode the relative relocations in a SHT_ANDROID_RELR section, and pack
the rest of the dynamic relocations in a SHT_ANDROID_REL(A) section.

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

llvm-svn: 336594
2018-07-09 20:08:55 +00:00
George Rimar 732a1e52b9 [ELF] - Eliminate dead "if". NFC.
We call switchTo() from assignAddresses() for switching to Aether,
and from assignOffsets().

First calls assignOffsets() one by one for each output section.
(https://github.com/llvm-mirror/lld/blob/master/ELF/LinkerScript.cpp#L1045)

That I believe means the condition removed in this patch is dead.

llvm-svn: 336356
2018-07-05 14:27:36 +00:00
George Rimar ccf0db9974 [ELF] - Convert excessive dyn_cast -> cast. NFC.
Currently, there are only OutputSection and SymbolAssignment
commands possible at the first level under SECTIONS tag.

Hence, dyn_cast was excessive.

llvm-svn: 336354
2018-07-05 14:09:47 +00:00
George Rimar 9b99abcf99 [ELF] - Advance position in a memory region when change the Dot.
This is https://bugs.llvm.org//show_bug.cgi?id=37836

Previously LLD could assign to Dot or set the address
for the section with address expression but did not advance
the position in a memory region.

Patch fixes the issue.

llvm-svn: 336335
2018-07-05 10:44:17 +00:00
James Henderson b427d4eced [ELF] Don't create empty output section for unreferenced PROVIDEs
LLD removes empty output sections otherwise specified in the linker
script. Prior to this change however, if section descriptions included
ANY kind of symbol assignment, then the consequent output section would
not be removed, even if the assignment was marked with PROVIDE and not
actually triggered (i.e. the symbol was never referenced). This change
modifies the isDiscarable function to ignore such directives when
determining whether a section should be discarded, in keeping with
bfd's behaviour. Symbol assignments that do result in a symbol
definition will continue to result in a kept section (this is not
actually the same as bfd's behaviour, but it is simpler, and probably
makes more sense).

Reviewed By: grimar

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

llvm-svn: 336184
2018-07-03 09:23:25 +00:00
Rui Ueyama b75d19c36c Make ALIGN work with -r in linker scripts
Patch by Mark Kettenis.

Make ALIGN work in linker scripts used with the -r option. This works in
GNU ld (ld.bfd) and is used to generate the "random gap" object for
linking the OpenBSD kernel.

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

llvm-svn: 332656
2018-05-17 20:22:39 +00:00
Rafael Espindola 40f46bc3e6 Delete unused variable.
llvm-svn: 330978
2018-04-26 19:21:07 +00:00
Rafael Espindola ab0cce5f1f Replace SharedSymbols with Defined when creating copy relocations.
This is slightly simpler to read IMHO. Now if a symbol has a position
in the file, it is Defined.

The main motivation is that with this a SharedSymbol doesn't need a
section, which reduces the size of SymbolUnion.

With this the peak allocation when linking chromium goes from 568.1 to
564.2 MB.

llvm-svn: 330966
2018-04-26 17:58:58 +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
George Rimar 0029f21482 [ELF] - Stop setting OutSecOff too early.
Currently LLD sets OutSecOff in addSection for input sections.
That is a fake offset (just a rude approximation to remember the order), 
used for sorting SHF_LINK_ORDER sections
(see resolveShfLinkOrder, compareByFilePosition).

There are 2 problems with such approach:

1. We currently change and reuse Size field as a value assigned. Changing size is
not good because leads to bugs. Currently, SIZEOF(.bss) for empty .bss returns 2
because we add two empty synthetic sections and increase size twice by 1. 
(See PR37011: https://bugs.llvm.org/show_bug.cgi?id=37011)

2. Such approach simply does not work when --symbol-ordering-file is involved,
because processing of the ordering file might break the initial section order.

This fixes PR37011.

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

llvm-svn: 329560
2018-04-09 13:01:50 +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 4d2740c6ed [ELF] - Cleanup. NFCI.
Rename field, added comments.

This is splitted from the D44894. 
Requested to be committed as independent cleanup.

llvm-svn: 329162
2018-04-04 09:39:05 +00:00
Rafael Espindola 9c2c938521 Reduce code duplication a bit.
Thanks to George Rimar for pointing it out.

llvm-svn: 328571
2018-03-26 18:55:33 +00:00
Rafael Espindola f065390f6c Reduce code duplication a bit. NFC
llvm-svn: 328569
2018-03-26 18:49:31 +00:00
George Rimar a6ce78ece1 This is PR36799.
Currently, we might have a bug with scripts like below:

.foo : ALIGN(8) 
{
  *(.foo)
} > ram
because do not expand the memory region when doing ALIGN.

This might result in file range overlaps. The patch fixes the issue.

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

llvm-svn: 328479
2018-03-26 08:58:16 +00:00
George Rimar d8281379f9 [ELF] - Do not ignore discarding of .rela.plt/.rela.dyn, allow doing custom layout for them.
Currently when we build input sections list in linker script
we ignore all rel[a] sections. That was done to support
scripts like .rela.dyn : { *(.rela.data) } for emit relocs.

Though as a result following scripts were also silently ignored:

/DISCARD/ : { *(.rela.plt)
/DISCARD/ : { *(.rela.dyn)

and we produced output with this sections. That is not ideal.
The solution this patch suggests is simple: do not ignore synthetic
rel[a] sections. That way we can enable common discarding logic
for them and report a proper error.

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

llvm-svn: 328419
2018-03-24 13:10:19 +00:00
George Rimar 54634f1990 [ELF] - Another fix for "LLD crashes with --emit-relocs when trying to proccess .eh_frame"
This fixes PR36367 which is about segfault when --emit-relocs is
used together with .eh_frame sections which happens because
of reordering of regular and .rel[a] sections.

Path changes loop that iterates over input sections to create
relocation target sections first.

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

llvm-svn: 328299
2018-03-23 09:18:31 +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 afbf90aef9 [ELF] - Drop special flags for empty output sections.
This fixes PR36598.

LLD currently crashes when we have empty output section
with SHF_LINK_ORDER flag. This might happen if we place an 
empty synthetic section in the linker script, but keep output
section alive with the use of additional symbol, for example.

The patch fixes the issue by dropping all special flags
for empty sections.

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

llvm-svn: 327374
2018-03-13 08:32:56 +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 301305fd3d Use exact uint32_t for uint32_t ELF field. NFC.
llvm-svn: 326934
2018-03-07 19:25:36 +00:00
George Rimar 527bfd7a48 [ELF] - Recommit r326892,r326893 "[ELF] - Report LMA region overflows."
With fix: add missing "RUN:" prefix to test case.

Original commit message:
We do not report LMA region overflows currently.
Both GNU linkers do that. The patch implements it.

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

llvm-svn: 326895
2018-03-07 12:44:18 +00:00
George Rimar 06846c2251 [ELF] - Revert r326892, r326893.
Bots are still unhappy:
http://lab.llvm.org:8011/builders/llvm-clang-lld-x86_64-scei-ps4-ubuntu-fast/builds/26259

llvm-svn: 326894
2018-03-07 12:33:00 +00:00
George Rimar 97e054e00d [ELF] - Report LMA region overflows.
We do not report LMA region overflows currently.
Both GNU linkers do that. The patch implements it.

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

llvm-svn: 326892
2018-03-07 11:54:30 +00:00
George Rimar 54baa5f45f [ELF] - Allow discarding .hash and .gnu.hash from linker script.
Currently, LLD segfaults when linker script attempts to discard
one of the hash sections. This patch fixes that.

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

llvm-svn: 326891
2018-03-07 11:47:15 +00:00
George Rimar 162d436c8e [ELF] - Support moving location counter when MEMORY is used.
We do not expand memory region correctly for following scripts:

.foo.1 : 
 {
   *(.foo.1)
   . += 0x1000;
 } > ram
Patch generalizes expanding of output sections and memory
regions in one place and fixes the issue.

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

llvm-svn: 326688
2018-03-05 10:54:03 +00:00
George Rimar 97785af464 [ELF] - Report error when memory region is overflowed by data commands.
LLD can not catch a memory area overflow when using a data command.
If we have the script below:

.foo : 
{
  *(.foo)
  BYTE(0x1)
} > ram

where BYTE overflows the ram region, we do not report it currently.
Patch fixes that.

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

llvm-svn: 326545
2018-03-02 08:11:58 +00:00
Rafael Espindola e75b42ee4e Don't allocate a header bellow address 0.
With the current code if the script has a PHDRS we always obey and try
to allocate a header. This can cause Min - HeaderSize to underflow.

It looks like bfd actually prints an error for this case. With this
patch we do the same.

Found while looking at pr36515.

llvm-svn: 326441
2018-03-01 15:25:46 +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
Rafael Espindola 852bd5c062 Simplify removing empty output sections.
With this the meaning of the Live bit in output sections is clear: we
have at some point added a input section into it.

llvm-svn: 326401
2018-03-01 01:08:00 +00:00
Rafael Espindola 367bfce611 Simplify. NFC.
llvm-svn: 326344
2018-02-28 18:05:37 +00:00
Rui Ueyama ee17371897 Merge {COFF,ELF}/Strings.cpp to Common/Strings.cpp.
This should resolve the issue that lld build fails in some hosts
that uses case-insensitive file system.

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

llvm-svn: 326339
2018-02-28 17:38:19 +00:00
Igor Kudrin c844524e46 [ELF] Process linker scripts deeper when declaring symbols.
We should process symbols inside output section declarations the same way as top-level ones.

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

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

llvm-svn: 326242
2018-02-27 20:31:22 +00:00