Commit Graph

193 Commits

Author SHA1 Message Date
George Rimar 3b189d1643 [ELF] - Do not allow -r to eat comdats.
This is PR33052, "Bug 33052 - -r eats comdats ".

To fix it I stop removing group section from out when -r is given
and fixing SHT_GROUP content when writing it just like we do some
other fixup, e.g. for Rel[a]. (it needs fix for section indices that
are in group).

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

llvm-svn: 304140
2017-05-29 08:37:50 +00:00
Kamil Rytarowski e739e49c0f Replace std::call_once with llvm:call_once
Summary:
This is required on some platforms, as GNU libstdc++ std::call_once is known to be buggy.

This fixes operation of LLD on at least NetBSD and perhaps OpenBSD and Linux PowerPC.

The same change has been introduced to LLVM and LLDB.

Reviewers: ruiu

Reviewed By: ruiu

Subscribers: emaste, #lld

Tags: #lld

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

llvm-svn: 303788
2017-05-24 18:31:48 +00:00
Rafael Espindola a6465bbb72 Alternative way to detemplate GotSection.
GetSection is a template because write calls relocate.

relocate has two parts. The non alloc code really has to be a
template, as it is looking a raw input file data.

The alloc part is only a template because of getSize.

This patch folds the value of getSize early, detemplates
getRelocTargetVA and splits relocate into a templated non alloc case
and a regular function for the alloc case. This has the nice advantage
of making sure we collect all the information we need for relocations
before getting to InputSection::relocateNonAlloc.

Since we know got is alloc, it can just call the function directly and
avoid the template.

llvm-svn: 303355
2017-05-18 16:45:36 +00:00
Rui Ueyama b87602032a Change the error message format for undefined symbols.
Previously, undefined symbol errors are one line like this
and wasn't easy to read.

  /ssd/clang/bin/ld.lld: error: /ssd/llvm-project/lld/ELF/Writer.cpp:207: undefined symbol 'lld:🧝:EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)0, true> >::addSection(lld:🧝:InputSectionBase*)'

This patch make it more structured like this.

  bin/ld.lld: error: undefined symbol: lld:🧝:EhFrameSection<llvm::object::ELFType<(llvm::support::endianness)0, true>
  >>> Referenced by Writer.cpp:207 (/ssd/llvm-project/lld/ELF/Writer.cpp:207)
  >>>               Writer.cpp.o in archive lib/liblldELF.a

Discussion thread:
http://lists.llvm.org/pipermail/llvm-dev/2017-March/111459.html

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

llvm-svn: 299097
2017-03-30 19:13:47 +00:00
George Rimar 1ec03e46a7 [ELF] - Detemplate InputSection::getRelocatedSection(). NFC.
llvm-svn: 298353
2017-03-21 09:13:27 +00:00
George Rimar 76e562ab1b [ELF] - Detemplate InputSectionBase::uncompress(). NFC
llvm-svn: 298351
2017-03-21 09:08:58 +00:00
George Rimar 9353e2dbfb [ELF] - Detemplate InputSectionBase::getLinkOrderDep(). NFC.
llvm-svn: 298346
2017-03-21 08:29:48 +00:00
Rafael Espindola 5616adf655 Remove DefinedSynthetic.
With this we have a single section hierarchy. It is a bit less code,
but the main advantage will be in a future patch being able to handle

foo = symbol_in_obj;

in a linker script. Currently that fails since we try to find the
output section of symbol_in_obj.  With this we should be able to just
return an InputSection from the expression.

llvm-svn: 297313
2017-03-08 22:36:28 +00:00
Rafael Espindola fcd208fdb3 Use uint32_t for alignment in more places, NFC.
llvm-svn: 297305
2017-03-08 19:35:29 +00:00
Rafael Espindola 5e434b3f11 Remove unnecessary template.
llvm-svn: 297293
2017-03-08 16:08:36 +00:00
Rafael Espindola e1294091d3 Remove unnecessary template. NFC.
llvm-svn: 297292
2017-03-08 16:03:41 +00:00
Rafael Espindola 35ae65ee2b Rename one of the getOffset methods.
It is sufficiently different in that it returns an offset in the input
file, not the output section.

llvm-svn: 297290
2017-03-08 15:57:17 +00:00
Rafael Espindola 76b6bd355d Remove unnecessary template. NFC.
llvm-svn: 297287
2017-03-08 15:44:30 +00:00
Rafael Espindola bdd2e3e36c Remove redundant member of InputSectionBase. NFC.
With this InputSectionBase is now 144 bytes.

llvm-svn: 297278
2017-03-08 14:12:52 +00:00
Rafael Espindola c8de15bdda Rename remaining uses of Addralign to Alignment.
llvm-svn: 297146
2017-03-07 15:11:21 +00:00
Rafael Espindola 2a80e1180e Make Discarded a InputSectionBase.
NFC, just a bit simpler.

llvm-svn: 297087
2017-03-06 22:36:19 +00:00
Rafael Espindola 5c02b741eb Detemplate EhInputSection. NFC.
llvm-svn: 297077
2017-03-06 21:17:18 +00:00
Rafael Espindola 6119b865ec Detemplate merge (input and synthetic) sections. NFC.
llvm-svn: 297061
2017-03-06 20:23:56 +00:00
Rui Ueyama 80474a26b9 De-template DefinedRegular.
Differential Revision: https://reviews.llvm.org/D30348

llvm-svn: 296508
2017-02-28 19:29:55 +00:00
Rui Ueyama 536a26706f Move SymbolTable<ELFT>::Sections out of the class.
The list of all input sections was defined in SymbolTable class for a
historical reason. The list itself is not a template. However, because
SymbolTable class is a template, we needed to pass around ELFT to access
the list. This patch moves the list out of the class so that it doesn't
need ELFT.

llvm-svn: 296309
2017-02-27 02:32:08 +00:00
Rafael Espindola 24e6f363c5 Merge OutputSectionBase and OutputSection. NFC.
Now that all special sections are SyntheticSections, we only need one
OutputSection class.

llvm-svn: 296127
2017-02-24 15:07:30 +00:00
Rafael Espindola 798ad9a1e8 Expand a comment. NFC.
llvm-svn: 296114
2017-02-24 13:06:59 +00:00
Rafael Espindola 66b4e21534 Convert EhOutputSection to be a synthetic section.
With this we complete the transition out of special output sections,
and with the previous patches it should be possible to merge
OutputSectionBase and OuputSection.

llvm-svn: 296023
2017-02-23 22:06:28 +00:00
Rafael Espindola 774ea7d0a9 Make InputSection a class. NFC.
With the current design an InputSection is basically anything that
goes directly in a OutputSection. That includes plain input section
but also synthetic sections, so this should probably not be a
template.

llvm-svn: 295993
2017-02-23 16:49:07 +00:00
Rafael Espindola c404d50d7c Merge InputSectionData and InputSectionBase.
Now that InputSectionBase is not a template there is no reason to have
the two.

llvm-svn: 295924
2017-02-23 02:32:18 +00:00
Rafael Espindola b4c9b81aad Convert InputSectionBase to a class.
Removing this template is not a big win by itself, but opens the way
for removing more templates.

llvm-svn: 295923
2017-02-23 02:28:28 +00:00
George Rimar 647c1685b6 [ELF] - Move DependentSections vector from InputSection to InputSectionBase
I splitted it from D29273.
Since we plan to make relocatable sections as dependent for target ones for
--emit-relocs implementation, this change is required to support .eh_frame case.

EhInputSection inherets from InputSectionBase and not from InputSection.
So for case when it has relocation section, it should be able to access DependentSections
vector.

This case is real for Linux kernel.

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

llvm-svn: 295483
2017-02-17 19:34:05 +00:00
George Rimar 09015fee3c [ELF] - Allow section to have multiple dependent sections.
That fixes a case when section has more than one metadata 
section. Previously GC would collect one of such sections 
because we had implementation that stored only last one as
dependent.

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

llvm-svn: 295298
2017-02-16 08:41:19 +00:00
Rafael Espindola 9e9754b520 Replace MergeOutputSection with a synthetic section.
With a synthetic merge section we can have, for example, a single
.rodata section with stings, fixed sized constants and non merge
constants.

I can be simplified further by not setting Entsize, but that is
probably better done is a followup patch.

This should allow some cleanup in the linker script code now that
every output section command maps to just one output section.

llvm-svn: 294005
2017-02-03 13:06:18 +00:00
Peter Smith 3a52eb0054 [ELF] Use SyntheticSections for Thunks
Thunks are now implemented by redirecting the relocation to the
symbol S, to a symbol TS in a Thunk. The Thunk will transfer control
to S. This has the following implications:
- All the side-effects of Thunks happen within createThunks()
- Thunks are no longer stored in InputSections and Symbols no longer
  need to hold a pointer to a Thunk
- The synthetic Thunk sections need to be merged into OutputSections
    
This implementation is almost a direct conversion of the existing
Thunks with the following exceptions:
- Mips LA25 Thunks are placed before the InputSection that defines
  the symbol that needs a Thunk.
- All ARM Thunks are placed at the end of the OutputSection of the
  first caller to the Thunk.
    
Range extension Thunks are not supported yet so it is optimistically
assumed that all Thunks can be reused.

This is a recommit of r293283 with a fixed comparison predicate as
std::merge requires a strict weak ordering.

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

llvm-svn: 293757
2017-02-01 10:26:03 +00:00
Rui Ueyama f20ee9f11a Revert "[ELF][ARM] Use SyntheticSections for Thunks"
This reverts commit r293283 because it broke MSVC build.

llvm-svn: 293352
2017-01-28 00:48:06 +00:00
Peter Smith 5191c6f945 [ELF][ARM] Use SyntheticSections for Thunks
Thunks are now implemented by redirecting the relocation to the
symbol S, to a symbol TS in a Thunk. The Thunk will transfer control
to S. This has the following implications:
- All the side-effects of Thunks happen within createThunks()
- Thunks are no longer stored in InputSections and Symbols no longer
  need to hold a pointer to a Thunk
- The synthetic Thunk sections need to be merged into OutputSections
    
This implementation is almost a direct conversion of the existing
Thunks with the following exceptions:
- Mips LA25 Thunks are placed before the InputSection that defines
  the symbol that needs a Thunk.
- All ARM Thunks are placed at the end of the OutputSection of the
  first caller to the Thunk.
    
Range extension Thunks are not supported yet so it is optimistically
assumed that all Thunks can be reused.

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

llvm-svn: 293283
2017-01-27 13:10:16 +00:00
George Rimar 0d8af3697a [ELF] - Reuse Decompressor class.
Intention of change is to get rid of code duplication.
Decompressor was introduced in D28105.

Change allows to get rid of few methods relative to decompression.

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

llvm-svn: 291758
2017-01-12 10:53:31 +00:00
Rui Ueyama ce039266c1 Merge elf::toString and coff::toString.
The two overloaded functions hid each other. This patch merges them.

llvm-svn: 291222
2017-01-06 10:04:08 +00:00
Rui Ueyama c207a89c91 Remove `Compressed` member from InputSectionData.
This value is used only once, and we can compute a value.
So we don't need to save it.

llvm-svn: 290164
2016-12-20 05:47:55 +00:00
Rui Ueyama 8f687f71fb Remove inappropriate use of CachedHashStringRef.
Use of CachedHashStringRef makes sense only when we reuse hash values.
Sprinkling it to all DenseMap has no benefits and just complicates data types.
Basically we shouldn't use CachedHashStringRef unless there is a strong
reason to to do so.

llvm-svn: 290076
2016-12-19 03:14:16 +00:00
Rui Ueyama c8e6884871 Inline MergeInputSection::getData().
This change seems to make LLD 0.6% faster when linking Clang with
debug info. I don't want us to have lots of local optimizations,
but this function is very hot, and the improvement is small but
not negligible, so I think it's worth doing.

llvm-svn: 288757
2016-12-06 02:19:30 +00:00
Rui Ueyama fcd3fa83ea Use "equivalence class" instead of "color" to describe the concept in ICF.
Also add a citation to GNU gold safe ICF paper.

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

llvm-svn: 288684
2016-12-05 18:11:35 +00:00
Rui Ueyama 91ae861af5 Updates file comments and variable names.
Use "color" instead of "group id" to describe the ICF algorithm.

llvm-svn: 288409
2016-12-01 19:45:22 +00:00
Rui Ueyama c1835319c9 Parallelize ICF to make LLD's ICF really fast.
ICF is short for Identical Code Folding. It is a size optimization to
identify two or more functions that happened to have the same contents
to merges them. It usually reduces output size by a few percent.

ICF is slow because it is computationally intensive process. I tried
to paralellize it before but failed because I couldn't make a
parallelized version produce consistent outputs. Although it didn't
create broken executables, every invocation of the linker generated
slightly different output, and I couldn't figure out why.

I think I now understand what was going on, and also came up with a
simple algorithm to fix it. So is this patch.

The result is very exciting. Chromium for example has 780,662 input
sections in which 20,774 are reducible by ICF. LLD previously took
7.980 seconds for ICF. Now it finishes in 1.065 seconds.

As a result, LLD can now link a Chromium binary (output size 1.59 GB)
in 10.28 seconds on my machine with ICF enabled. Compared to gold
which takes 40.94 seconds to do the same thing, this is an amazing
number.

From here, I'll describe what we are doing for ICF, what was the
previous problem, and what I did in this patch.

In ICF, two sections are considered identical if they have the same
section flags, section data, and relocations. Relocations are tricky,
becuase two relocations are considered the same if they have the same
relocation type, values, and if they point to the same section _in
terms of ICF_.

Here is an example. If foo and bar defined below are compiled to the
same machine instructions, ICF can (and should) merge the two,
although their relocations point to each other.

  void foo() { bar(); }
  void bar() { foo(); }

This is not an easy problem to solve.

What we are doing in LLD is some sort of coloring algorithm. We color
non-identical sections using different colors repeatedly, and sections
in the same color when the algorithm terminates are considered
identical. Here is the details:

  1. First, we color all sections using their hash values of section
  types, section contents, and numbers of relocations. At this moment,
  relocation targets are not taken into account. We just color
  sections that apparently differ in different colors.

  2. Next, for each color C, we visit sections having color C to see
  if their relocations are the same. Relocations are considered equal
  if their targets have the same color. We then recolor sections that
  have different relocation targets in new colors.

  3. If we recolor some section in step 2, relocations that were
  previously pointing to the same color targets may now be pointing to
  different colors. Therefore, repeat 2 until a convergence is
  obtained.

Step 2 is a heavy operation. For Chromium, the first iteration of step
2 takes 2.882 seconds, and the second iteration takes 1.038 seconds,
and in total it needs 23 iterations.

Parallelizing step 1 is easy because we can color each section
independently. This patch does that.

Parallelizing step 2 is tricky. We could work on each color
independently, but we cannot recolor sections in place, because it
will break the invariance that two possibly-identical sections must
have the same color at any moment.

Consider sections S1, S2, S3, S4 in the same color C, where S1 and S2
are identical, S3 and S4 are identical, but S2 and S3 are not. Thread
A is about to recolor S1 and S2 in C'. After thread A recolor S1 in
C', but before recolor S2 in C', other thread B might observe S1 and
S2. Then thread B will conclude that S1 and S2 are different, and it
will split thread B's sections into smaller groups wrongly. Over-
splitting doesn't produce broken results, but it loses a chance to
merge some identical sections. That was the cause of indeterminism.

To fix the problem, I made sections have two colors, namely current
color and next color. At the beginning of each iteration, both colors
are the same. Each thread reads from current color and writes to next
color. In this way, we can avoid threads from reading partial
results. After each iteration, we flip current and next.

This is a very simple solution and is implemented in less than 50
lines of code.

I tested this patch with Chromium and confirmed that this parallelized
ICF produces the identical output as the non-parallelized one.

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

llvm-svn: 288373
2016-12-01 17:09:04 +00:00
Rui Ueyama e8a077badf Change return types of split{Non,}Strings.
They return new vectors, but at the same time they mutate other vectors,
so returning values doesn't make much sense. We should just mutate two
vectors.

llvm-svn: 287979
2016-11-26 15:15:11 +00:00
Rui Ueyama da06bfb794 Move getLocation from Relocations.cpp to InputSection.cpp.
The function was used only within Relocations.cpp, but now we are
using it in many places, so this patch moves it to a file that fits
to the functionality.

llvm-svn: 287943
2016-11-25 18:51:53 +00:00
Rui Ueyama 3fc0f7e54f Define toString() as a generic function to get a string for error message.
We have different functions to stringize objects to construct
error messages. For InputFile, we have getFilename, and for
InputSection, we have getName. You had to memorize them.

I think this is the case where the function overloading comes in handy.

This patch defines toString() functions that are overloaded for all these
types, so that you just call it in error().

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

llvm-svn: 287787
2016-11-23 18:07:33 +00:00
Eugene Leviant 531df4fcef [ELF] Print error location in .eh_frame parser
Differential revision: https://reviews.llvm.org/D26914

llvm-svn: 287750
2016-11-23 09:45:17 +00:00
Rui Ueyama f94efdddc0 Add a flag to InputSectionBase for linker script.
Previously, we set (uintptr_t)-1 to InputSectionBase::OutSec to record
that a section has already been set to be assigned to some output section
by linker scripts. Later, we restored nullptr to the pointer to use
the field for the original purpose. That overloading is not very easy to
understand.

This patch adds a bit flag for that purpose, so that we don't need
to piggyback the flag on an unrelated pointer.

llvm-svn: 287508
2016-11-20 23:15:52 +00:00
Rui Ueyama bd1f0630a8 Do not expose ICF class from the file.
Also this patch uses file-scope functions instead of class member function.

Now that ICF class is not visible from outside, InputSection class
can no longer be "friend" of it. So I removed the friend relation
and just make it expose the features to public.

llvm-svn: 287480
2016-11-20 02:39:59 +00:00
Rui Ueyama 77f2a87575 Simplify MergeOutputSection.
MergeOutputSection class was a bit hard to use because it provdes
a series of finalize functions that have to be called in a right way
at a right time. It also intereacted with MergeInputSection, and the
logic was somewhat entangled between the two classes.

This patch simplifies it by providing only one finalize function.
Now, all you have to do is to call MergeOutputSection::finalize
when you have added all sections to the output section. Then, it
internally merges strings and initliazes StringPiece objects.
I think this is much easier to understand.

This patch also adds comments.

llvm-svn: 287314
2016-11-18 05:05:43 +00:00
George Rimar d8b27769c8 [ELF] - format. NFC.
llvm-svn: 286805
2016-11-14 10:14:18 +00:00
Rui Ueyama 82664d9d4c Remove a member from InputSectionData and use the pool instead.
llvm-svn: 286557
2016-11-11 03:54:59 +00:00
Rafael Espindola 9f0c4bb795 Parse relocations only once.
Relocations are the last thing that we wore storing a raw section
pointer to and parsing on demand.

With this patch we parse it only once and store a pointer to the
actual data.

The patch also changes where we store it. It is now in
InputSectionBase. Not all sections have relocations, but most do and
this simplifies the logic. It also means that we now only support one
relocation section per section. Given that that constraint is
maintained even with -r with gold bfd and lld, I think it is OK.

llvm-svn: 286459
2016-11-10 14:53:24 +00:00