Commit Graph

788 Commits

Author SHA1 Message Date
Rui Ueyama bbf154cf9c Move symbol resolution code out of SymbolTable class.
This is the last patch of the series of patches to make it possible to
resolve symbols without asking SymbolTable to do so.

The main point of this patch is the introduction of
`elf::resolveSymbol(Symbol *Old, Symbol *New)`. That function resolves
or merges given symbols by examining symbol types and call
replaceSymbol (which memcpy's New to Old) if necessary.

With the new function, we have now separated symbol resolution from
symbol lookup. If you already have a Symbol pointer, you can directly
resolve the symbol without asking SymbolTable to do that.

Now that the nice abstraction become available, I can start working on
performance improvement of the linker. As a starter, I'm thinking of
making --{start,end}-lib faster.

--{start,end}-lib is currently unnecessarily slow because it looks up
the symbol table twice for each symbol.

 - The first hash table lookup/insertion occurs when we instantiate a
   LazyObject file to insert LazyObject symbols.

 - The second hash table lookup/insertion occurs when we create an
   ObjFile from LazyObject file. That overwrites LazyObject symbols
   with Defined symbols.

I think it is not too hard to see how we can now eliminate the second
hash table lookup. We can keep LazyObject symbols in Step 1, and then
call elf::resolveSymbol() to do Step 2.

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

llvm-svn: 360975
2019-05-17 01:55:20 +00:00
Rui Ueyama 5c073a94f9 Introduce CommonSymbol.
Previously, we handled common symbols as a kind of Defined symbol,
but what we were doing for common symbols is pretty different from
regular defined symbols.

Common symbol and defined symbol are probably as different as shared
symbol and defined symbols are different.

This patch introduces CommonSymbol to represent common symbols.
After symbols are resolved, they are converted to Defined symbols
residing in a .bss section.

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

llvm-svn: 360841
2019-05-16 03:29:03 +00:00
Rui Ueyama 7d4761928e Simplify SymbolTable::add{Defined,Undefined,...} functions.
SymbolTable's add-family functions have lots of parameters because
when they have to create a new symbol, they forward given arguments
to Symbol's constructors. Therefore, the functions take at least as
many arguments as their corresponding constructors.

This patch simplifies the add-family functions. Now, the functions
take a symbol instead of arguments to construct a symbol. If there's
no existing symbol, a given symbol is memcpy'ed to the symbol table.
Otherwise, the functions attempt to merge the existing and a given
new symbol.

I also eliminated `CanOmitFromDynSym` parameter, so that the functions
take really one argument.

Symbol classes are trivially constructible, so looks like constructing
them to pass to add-family functions is as cheap as passing a lot of
arguments to the functions. A quick benchmark showed that this patch
seems performance-neutral.

This is a preparation for
http://lists.llvm.org/pipermail/llvm-dev/2019-April/131902.html

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

llvm-svn: 360838
2019-05-16 02:14:00 +00:00
Peter Smith 4e21c770ec [ELF] Full support for -n (--nmagic) and -N (--omagic) via common page
The -n (--nmagic) disables page alignment, and acts as a -Bstatic
The -N (--omagic) does what -n does but also marks the executable segment as
writeable. As page alignment is disabled headers are not allocated unless
explicit in the linker script.

To disable page alignment in LLD we choose to set the page sizes to 1 so
that any alignment based on the page size does nothing. To set the
Target->PageSize to 1 we implement -z common-page-size, which has the side
effect of allowing the user to set the value as well.

Setting the page alignments to 1 does mean that any use of
CONSTANT(MAXPAGESIZE) or CONSTANT(COMMONPAGESIZE) in a linker script will
return 1, unlike in ld.bfd. However given that -n and -N disable paging
these probably shouldn't be used in a linker script where -n or -N is in
use.

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

llvm-svn: 360593
2019-05-13 16:01:26 +00:00
Andrew Ng 24896d304d [LLD][ELF] /DISCARD/ output sections should not be orphans
/DISCARD/ output sections were being treated as orphans. As a result, if
a /DISCARD/ output section has been assigned a PHDR, it could cause
incorrect assignment of sections to segments.

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

llvm-svn: 359565
2019-04-30 14:31:22 +00:00
George Rimar dee900ae59 [LLD][ELF] - Do not remove empty sections referenced in LOADADDR/ADDR commands.
This is https://bugs.llvm.org//show_bug.cgi?id=38750.

If script references empty sections in LOADADDR/ADDR commands

.empty  : { *(.empty ) }
.text   : AT(LOADADDR (.empty) + SIZEOF (.empty)) { *(.text) }
then an empty section will be removed and LOADADDR/ADDR will evaluate to null.
It is not that user may expect from using of the generic script, what is a common case.

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

llvm-svn: 359279
2019-04-26 06:59:30 +00:00
Fangrui Song 74780852dc [ELF] Fix a gcc -Wextra warning
Extracted from D61046.

warning: enumeral and non-enumeral type in conditional expression [-Wextra]

Cast SHF_ALLOC to avoid that.

llvm-svn: 359070
2019-04-24 05:33:33 +00:00
Andrew Ng ccba42c7eb [ELF] Change default output section type to SHT_PROGBITS
This fixes an issue where a symbol only section at the start of a
PT_LOAD segment, causes incorrect alignment of the file offset for the
start of the segment which results in the output of an invalid ELF.

SHT_PROGBITS was the default output section type in the past.

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

llvm-svn: 358981
2019-04-23 12:38:52 +00:00
Fangrui Song 32c0ebe615 Use llvm::stable_sort
Make some small adjustment while touching the code: make parameters
const, use less_first(), etc.

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

llvm-svn: 358943
2019-04-23 02:42:06 +00:00
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