Commit Graph

4976 Commits

Author SHA1 Message Date
Rafael Espindola 1eeb26293d Pack symbols a bit more.
Before this patch:

Symbol 56
Defined 80
Undefined 56
SharedSymbol 88
LazyArchive 72
LazyObject 56

With this patch

Symbol 48
Defined 72
Undefined 48
SharedSymbol 80
LazyArchive 64
LazyObject 48

The result is that peak allocation when linking chromium (according to
heaptrack) goes from 578 to 568 MB.

llvm-svn: 330874
2018-04-25 21:44:37 +00:00
Rafael Espindola 047f857642 Also demote lazy symbols.
This is not a big simplification right now, but the special cases for
lazy symbols have been a common source of bugs in the past.

llvm-svn: 330869
2018-04-25 20:46:08 +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
Rafael Espindola 61376d9bed Bring r329960 back.
The fix is to copy Used when replacing the symbol.

Original message:

Do not keep shared symbols created from garbage-collected eliminated DSOs.

If all references to a DSO happen to be weak, and if the DSO is
specified with --as-needed, the DSO is not added to DT_NEEDED.
If that happens, we also need to eliminate shared symbols created
from the DSO. Otherwise, they become dangling references that point
to non-exsitent DSO.

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

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

llvm-svn: 330788
2018-04-25 00:29:13 +00:00
George Rimar de83cbf37e [ELF] - Never use std::sort.
It turns out we should not use the std::sort anymore.
r327219 added a new wrapper llvm::sort (D39245).
When EXPENSIVE_CHECKS is defined, it shuffles the
input container and that helps to find non-deterministic
ordering.

Patch changes code to use llvm::sort and std::stable_sort
instead of std::sort

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

llvm-svn: 330702
2018-04-24 09:55:39 +00:00
George Rimar 19f9b814dd [ELF] - Refactor lazy symbol duplicated code.
Our code for LazyObject and LazyArchive duplicates.

This patch extracts the common part to remove
the duplication.

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

llvm-svn: 330701
2018-04-24 09:41:56 +00:00
Rui Ueyama d134d2e509 Remove duplicate "error:" from an error message.
This patch also simplifies the code a bit which wasn't committed in
https://reviews.llvm.org/r330600.

llvm-svn: 330644
2018-04-23 20:34:35 +00:00
Zaara Syeda 25b488b0ea [PPC64] Fix toc restore nops offset for V2 ABI
The PPC64 V2 ABI restores the toc base by loading from an offset of 24 from r1.
This patch fixes the offset and updates the testcases from V1 to V2. It also
issues an error when a nop is missing after a call to an external function.

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

llvm-svn: 330600
2018-04-23 15:01:24 +00:00
George Rimar 97df22f110 [ELF] - Simplify. NFC.
llvm-svn: 330597
2018-04-23 14:41:49 +00:00
Fangrui Song c8ac0a6f97 [ELF] Swap argument names: use Old to refer to original symbol and New for incoming one
Reviewers: ruiu, espindola

Subscribers: emaste, arichardson, llvm-commits

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

llvm-svn: 330491
2018-04-20 22:50:15 +00:00
Rui Ueyama 88fe5c9557 Add -z {combreloc,copyreloc,noexecstack,lazy,relro,text}.
Differential Revision: https://reviews.llvm.org/D45902

llvm-svn: 330482
2018-04-20 21:24:08 +00:00
Fangrui Song 2416d7fcb2 [ELF] --warn-backrefs: use the same GroupId for object files in the same --{start,end}-lib
Reviewers: ruiu, espindola

Subscribers: emaste, arichardson, llvm-commits

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

llvm-svn: 330443
2018-04-20 16:33:01 +00:00
Rafael Espindola a59aacf52b Fix trap instruction on pp64.
The test was passing on a big endian host, but just because od with x4
was compensating for it.

llvm-svn: 330386
2018-04-20 01:21:24 +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
Rafael Espindola 4809e2c11d Define InputSection::getOffset inline.
This is much simpler than the other section types and there are many
places where the section type is statically know.

llvm-svn: 330350
2018-04-19 18:00:46 +00:00
Rafael Espindola f4d6e8caea Simplify Repl handling.
Now that we don't ICF synthetic sections, we can go back to the old
logic on whose responsibility it is to check Repl.

The idea is that Sec->something() will not check Repl. It is the
responsibility of the caller to find the correct Sec.

llvm-svn: 330346
2018-04-19 17:26:50 +00:00
Rafael Espindola aded409325 Simplify getOffset for synthetic sections.
We had a single symbol using -1 with a synthetic section. It is
simpler to just update its value.

This is not a big will by itself, but will allow having a simple
getOffset for InputSeciton.

llvm-svn: 330340
2018-04-19 16:54:30 +00:00
Rafael Espindola 6275a7aa39 Rename MergeInputSection::getOffset.
Unlike the getOffset in the base class, this one computes the offset
in the parent synthetic section, not the final output section.

llvm-svn: 330339
2018-04-19 16:05:07 +00:00
Rafael Espindola 9c680301b0 Simplify. NFC.
Using getOffset is here was a bit of an overkill. This is being
written and has relocations. This implies it is a .eh_frame or regular
section.

llvm-svn: 330307
2018-04-19 03:51:26 +00:00
Rafael Espindola 719fcd08c6 Don't call getOffset twice. NFC.
Just a bit faster.

llvm-svn: 330306
2018-04-19 02:24:28 +00:00
Michael J. Spencer b842725c1d [ELF] Add profile guided section layout
This adds profile guided layout using the Call-Chain Clustering (C³) heuristic
from https://research.fb.com/wp-content/uploads/2017/01/cgo2017-hfsort-final1.pdf .

RFC: [llvm-dev] [RFC] Profile guided section layout
     http://lists.llvm.org/pipermail/llvm-dev/2017-June/114178.html

Pass `--call-graph-ordering-file <file>` to read a call graph profile where each
line has the format:

    <from symbol> <to symbol> <call count>

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

llvm-svn: 330234
2018-04-17 23:30:05 +00:00
Reid Kleckner 9c86a31307 Revert r329960 "Do not keep shared symbols created from garbage-collected eliminated DSOs."
This is causing large numbers of Chromium test executables to crash on
shutdown. The relevant symbol seems to be __cxa_finalize, which gets
removed from the dynamic symbol table for some of the support libraries.

llvm-svn: 330164
2018-04-16 22:45:23 +00:00
Rafael Espindola ea2c78369c Reduce code duplication.
getVA was already implemented in the base class.

llvm-svn: 330036
2018-04-13 16:07:27 +00:00
Simon Atanasyan 1ba194212a [ELF][MIPS] Support linking of PIE for MIPS
MIPS ABI requires creation of the MIPS_RLD_MAP dynamic tag for non-PIE
executables only and MIPS_RLD_MAP_REL tag for both PIE and non-PIE
executables. The patch skips definition of the MIPS_RLD_MAP for PIE
files and defines MIPS_RLD_MAP_REL.

The MIPS_RLD_MAP_REL tag stores the offset to the .rld_map section
relative to the address of the tag itself.

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

llvm-svn: 329996
2018-04-13 08:15:01 +00:00
Rui Ueyama 039d248778 Do not keep shared symbols created from garbage-collected eliminated DSOs.
If all references to a DSO happen to be weak, and if the DSO is
specified with --as-needed, the DSO is not added to DT_NEEDED.
If that happens, we also need to eliminate shared symbols created
from the DSO. Otherwise, they become dangling references that point
to non-exsitent DSO.

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

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

llvm-svn: 329960
2018-04-12 21:57:04 +00:00
Sam Clegg d11b6edfc8 Remove references to old SymbolBody class
Differential Revision: https://reviews.llvm.org/D45400

llvm-svn: 329846
2018-04-11 19:52:53 +00:00
George Rimar c552619fc1 [ELF] - Reorder local symbols.
This fixes PR36716 (https://bugs.llvm.org/show_bug.cgi?id=36716),

Patch sorts local symbols to match the
following order: file1, local1, hidden1, file2, local2, hidden2 ...

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

llvm-svn: 329787
2018-04-11 09:24:27 +00:00
George Rimar 9f0b8e8025 [ELF] - Simplify. NFC.
llvm-svn: 329785
2018-04-11 09:03:02 +00:00
Rafael Espindola 262589db70 Don't warn on ICFed symbols, warn on synthetic ones.
Based on a patch for the ICF warning by Rui.

llvm-svn: 329757
2018-04-10 21:13:52 +00:00
Nico Weber fb64730005 s/LLVM_ON_WIN32/_WIN32/, lld
LLVM_ON_WIN32 is set exactly with MSVC and MinGW (but not Cygwin) in             
HandleLLVMOptions.cmake, which is where _WIN32 defined too.  Just use the        
default macro instead of a reinvented one.                                       
                                                                                 
See thread "Replacing LLVM_ON_WIN32 with just _WIN32" on llvm-dev and cfe-dev.   
No intended behavior change. 

llvm-svn: 329696
2018-04-10 13:15:21 +00:00
George Rimar e160473823 [ELF] - Do not crash when trying to order --defsym/linker script symbols.
Currently, we crash because File is null for
such symbols.

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

llvm-svn: 329678
2018-04-10 09:44:44 +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
Rumeet Dhindsa 682a417e51 Added support for LTO options: sample_profile, new_pass_manager and debug_pass_manager
Differential Revision: https://reviews.llvm.org/D45275

llvm-svn: 329598
2018-04-09 17:56:07 +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 7bf92be676 [ELF] - Allow LLD to produce file symbols.
This is for PR36716 and
this enables emitting STT_FILE symbols.

Output size affect is minor:
lld binary size changes from 52,883,408 to 52,949,400
clang binary size changes from 83,136,456 to 83,219,600

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

llvm-svn: 329557
2018-04-09 11:43:52 +00:00
Rafael Espindola b4d3dfefb7 Avoid some temporary allocations.
Some system libraries have a lot of versioned symbols. When linking
scylla this brings the number of malloc calls from 49154 to 37944.

llvm-svn: 329453
2018-04-06 20:53:06 +00:00
Rafael Espindola 3bd98b1031 Change which file we print when a symbol cannot be ordered.
Currently there are a few odd things about the warning about symbols
that cannot be ordered. This patch fixes:

* When there is an undefined symbol that resolves to a shared file, we
  were printing the location of the undefined reference.

* If there are multiple comdats, we were reporting them all.

llvm-svn: 329371
2018-04-06 03:36:19 +00:00
Rafael Espindola 9b6a65b144 Don't ignore addend when a SHF_MERGE section is dead.
This is similar to r329219, but for the entire section. Like r329219 I
don't expect this to have any real impact, it is just more consistent
and simpler.

llvm-svn: 329367
2018-04-06 01:10:33 +00:00
Rui Ueyama 4e150c4c88 Make "Size" column in the map file one characters shorter.
Previously, "size" column is 9 characters long which is too long
at least for 32-bit (because at maximum it needs 8 columns). This
patch make it one column shorter than before. That's also a reasonable
default for 64-bit.

llvm-svn: 329317
2018-04-05 17:20:18 +00:00
Rui Ueyama 16a130bc78 Fix column size in the map file.
Size can be narrow, but LMA should be the same width as VMA.

llvm-svn: 329312
2018-04-05 16:45:37 +00:00
Rafael Espindola 5f8e77afb5 Initialize OutputOff to zero.
We have a dedicated Live bit, so we don't need a special value and we
were not accounting for in at least one place.

llvm-svn: 329307
2018-04-05 15:56:04 +00:00
Eugene Leviant 57225ac179 [ELF] Don't add NOLOAD sections to segment
Differential revision: https://reviews.llvm.org/D45264

llvm-svn: 329281
2018-04-05 13:23:59 +00:00
George Rimar f9936e1fc9 [ELF] - Eliminate Target::isPicRel method.
As was mentioned in comments for D45158,
isPicRel's name does not make much sense,
because what this method does is checks if
we need to create the dynamic relocation or not.

Instead of renaming it to something different,
we can 'isPicRel' completely.

We can reuse the getDynRel method.
They are logically very close, getDynRel can just return
R_*_NONE in case no dynamic relocation should be produced
and that would simplify things and avoid functionality
correlation/duplication with 'isPicRel'.

The patch does this change.

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

llvm-svn: 329275
2018-04-05 12:07:20 +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 ee01b1d390 [ELF] - Print LMA in a -Map file.
Currently, LLD prints VA, but not LMA in a map file.
It seems can be useful to print both to reveal layout
details and patch implements it.

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

llvm-svn: 329271
2018-04-05 10:51:06 +00:00
Rafael Espindola 7bd45502fe Initialize OffsetMap earlier.
Now that getSectionPiece uses OffsetMap, it is advantageous to
initialize it earlier.

llvm-svn: 329242
2018-04-05 00:01:57 +00:00
Rui Ueyama 5a9529f4a2 Do not show alignment 0 because that is equivalent to 1.
Differential Revision: https://reviews.llvm.org/D44991

llvm-svn: 329233
2018-04-04 21:25:37 +00:00
Rafael Espindola f7c5a10e55 Don't ignore addend in getOffset.
We were ignoring the addend if the piece was dead. I don't expect this
to make a difference in any real world situations, but it is simpler
anyway.

llvm-svn: 329219
2018-04-04 19:13:30 +00:00
George Rimar 785a79133b [ELF] - X86_64: Use white list for relocations checked by isPicRel.
isPicRel is used to check if we want to create the dynamic relocations.
Not all of the dynamic relocations we create are passing through this
check, but those that are, probably better be whitelisted.

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

llvm-svn: 329203
2018-04-04 15:21:21 +00:00
George Rimar 55d717805b [ELF] - Use early return. NFC.
llvm-svn: 329180
2018-04-04 12:36:21 +00:00