Commit Graph

130 Commits

Author SHA1 Message Date
Rui Ueyama e28c146423 Avoid unnecessary buffer allocation and memcpy for compressed sections.
Previously, we uncompress all compressed sections before doing anything.
That works, and that is conceptually simple, but that could results in
a waste of CPU time and memory if uncompressed sections are then
discarded or just copied to the output buffer.

In particular, if .debug_gnu_pub{names,types} are compressed and if no
-gdb-index option is given, we wasted CPU and memory because we
uncompress them into newly allocated bufers and then memcpy the buffers
to the output buffer. That temporary buffer was redundant.

This patch changes how to uncompress sections. Now, compressed sections
are uncompressed lazily. To do that, `Data` member of `InputSectionBase`
is now hidden from outside, and `data()` accessor automatically expands
an compressed buffer if necessary.

If no one calls `data()`, then `writeTo()` directly uncompresses
compressed data into the output buffer. That eliminates the redundant
memory allocation and redundant memcpy.

This patch significantly reduces memory consumption (20 GiB max RSS to
15 Gib) for an executable whose .debug_gnu_pub{names,types} are in total
5 GiB in an uncompressed form.

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

llvm-svn: 343979
2018-10-08 16:58:59 +00:00
Benjamin Kramer 88e7be2e6b [ELF] Pass callables by function_ref
No need to create a heavyweight std::function if it's not stored. No
functionality change intended.

llvm-svn: 334885
2018-06-16 12:11:34 +00:00
George Rimar 848405b164 [ELF] - Do not crash when do --gc-sections for non-allocatable metadata sections.
Currently, LLD marks all non-allocatable sections except SHF_REL[A] as Live
when doing GC.

This can be a reason of the crash when SHF_LINK_ORDER sections
are involved, because their parents can be dead.

We should do GC for them correctly. The patch implements it.

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

llvm-svn: 332589
2018-05-17 10:00:34 +00:00
Rafael Espindola f1652d4c60 Split .eh_frame sections in parellel.
We can now split them in the same spot we split merge sections.

llvm-svn: 331064
2018-04-27 18:17:36 +00:00
Rafael Espindola 9bf1006278 Split merge sections early.
Now that getSectionPiece is fast (uses a hash) it is probably OK to
split merge sections early.

The reason I want to do this is to split eh_frame sections in the same
place.

This does mean that we have to decompress early. Given that the only
compressed sections are debug info, I don't think we are missing much.

It is a small improvement: 0.5% on the geometric mean.

llvm-svn: 331058
2018-04-27 16:29:57 +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
Sam Clegg f187c4d2e5 Consistent use of header file for ICF and MarkLive
Previously wasm used a separate header to declare markLive
and ELF used to declare ICF.  This change makes each backend
consistently declare these in their own headers.

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

llvm-svn: 325631
2018-02-20 22:09:59 +00:00
Rui Ueyama ff59a899d6 Use toString to print out garbage-collected sections.
Currently, archive file name is missing in this message. In general,
we should avoid constructing strings in an ad-hoc manner and instead
use toString() to get consistent output strings.

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

llvm-svn: 325416
2018-02-17 00:09:49 +00:00
Rafael Espindola bb3198949f Use dyn_cast instead of dyn_cast_or_null.
There should be no null sections in InputSections.

llvm-svn: 321219
2017-12-20 20:46:08 +00:00
Rafael Espindola a32ddc4639 Use a reference for the shared symbol file.
Every shared symbol has a file, so we can use a reference.

llvm-svn: 321187
2017-12-20 16:28:19 +00:00
Peter Collingbourne 17f71de70b ELF: Do not follow relocation edges to output sections during GC.
This fixes an assertion error introduced by r320390.

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

llvm-svn: 320431
2017-12-11 22:47:43 +00:00
Rafael Espindola af57d48ed4 Handle symbols pointing to output sections.
Now that gc sections runs after linker defined symbols are added it
can see symbols that point to an OutputSection.

Should fix a bot failure.

llvm-svn: 320412
2017-12-11 19:45:36 +00:00
George Rimar 2e610cd470 [ELF] - Handle EhInputSection Live bit in MarkLive.cpp
Since MarkLive.cpp is the place where we set Live flags for
other sections, it looks correct to do that there.
Benefit is that we stop spreading GC logic outsize of MarkLive.cpp.

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

llvm-svn: 319435
2017-11-30 14:01:06 +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
Rafael Espindola 1d4b3023dc Bring back r319008.
This includes a fix to mark copy reloc aliases as used.

Original message:

[ELF] Do not keep symbols if they referenced only from discarded sections.

This patch also ensures that in case of "--as-needed" is used,
DT_NEEDED entries are not created if they are required only by
these eliminated symbols.

llvm-svn: 319215
2017-11-28 20:17:58 +00:00
Igor Kudrin a46522f369 Revert r319008, "[ELF] Do not keep symbols if they referenced only from discarded sections."
and r319051, "Add a missing test."

r319008 broke the LTO bots;
r319051 depends on changes in r319008.

llvm-svn: 319154
2017-11-28 08:39:40 +00:00
Igor Kudrin 40007586fc [ELF] Do not keep symbols if they referenced only from discarded sections.
This patch also ensures that in case of "--as-needed" is used,
DT_NEEDED entries are not created if they are required only by
these eliminated symbols.

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

llvm-svn: 319008
2017-11-27 05:51:10 +00:00
Peter Collingbourne b472aa0268 ELF: Remove function Symbol::isInCurrentOutput().
This function is now equivalent to isDefined().

llvm-svn: 317449
2017-11-06 04:39:07 +00:00
Peter Collingbourne e9a9e0a1e7 ELF: Merge DefinedRegular and Defined.
Now that DefinedRegular is the only remaining derived class of
Defined, we can merge the two classes.

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

llvm-svn: 317448
2017-11-06 04:35:31 +00:00
Rui Ueyama f52496e1e0 Rename SymbolBody -> Symbol
Now that we have only SymbolBody as the symbol class. So, "SymbolBody"
is a bit strange name now. This is a mechanical change generated by

  perl -i -pe s/SymbolBody/Symbol/g $(git grep -l SymbolBody lld/ELF lld/COFF)

nd clang-format-diff.

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

llvm-svn: 317370
2017-11-03 21:21:47 +00:00
Rui Ueyama f1f00841d9 Merge SymbolBody and Symbol into one class, SymbolBody.
SymbolBody and Symbol were separated classes due to a historical reason.
Symbol used to be a pointer to a SymbolBody, and the relationship
between Symbol and SymbolBody was n:1.

r2681780 changed that. Since that patch, SymbolBody and Symbol are
allocated next to each other to improve memory locality, and they have
1:1 relationship now. So, the separation of Symbol and SymbolBody no
longer makes sense.

This patch merges them into one class. In order to avoid updating too
many places, I chose SymbolBody as a unified name. I'll rename it Symbol
in a follow-up patch.

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

llvm-svn: 317006
2017-10-31 16:07:41 +00:00
Rui Ueyama bda337a8d8 Rename isInCurrentDSO -> isInCurrentOutput.
DSO is short for dynamic shared object, so the function name was a
little confusing because it sounded like it didn't work when we were
a creating statically-linked executable or something.

What we mean by "DSO" here is the current output file that we are
creating. Thus the new name. Alternatively, we could call it the current
ELF module, but "module" is a overloaded word, so I avoided that.

llvm-svn: 316809
2017-10-27 22:54:16 +00:00
George Rimar 96b1157814 [ELF] - Simplify reporting of garbage collected sections.
This moves reporting of garbage collected sections right after
we do GC. That simplifies things.

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

llvm-svn: 316759
2017-10-27 11:32:22 +00:00
George Rimar f4ca4a6fa1 [ELF] - Do not collect SHT_REL[A] sections unconditionally when --gc-sections and --emit-relocs used together.
This is "Bug 34836 - --gc-sections remove relocations from --emit-relocs",

When --emit-relocs is used, LLD currently always drops SHT_REL[A] sections from
output if --gc-sections is present. Patch fixes the issue.

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

llvm-svn: 316418
2017-10-24 08:26:32 +00:00
Rafael Espindola a7e3132d03 Handle shared symbols in --gc-sections.
We don't want to gc a section if it is used via __start_*, but we were
doing that if the same symbol was visible in a shared library.

llvm-svn: 315727
2017-10-13 18:32:16 +00:00
Rafael Espindola bfc20343a3 Simplify. NFC.
llvm-svn: 315724
2017-10-13 18:18:36 +00:00
Rui Ueyama ac27de9dc7 Remove ScriptConfiguration class and move the members to LinkerScript class.
ScriptConfiguration was a class to contain parsed results of
linker scripts. LinkerScript is a class to interpret it.

That ditinction was needed because we haven't instantiated
LinkerScript early (because, IIRC, LinkerScript class was a
ELFT template function). So, when we parse linker scripts,
we couldn't directly store the result to a LinkerScript instance.

Now, that limitation is gone. We instantiate LinkerScript
at the very beginning of our main function. We can directly
store parse results to a LinkerScript instance.

llvm-svn: 315403
2017-10-11 01:19:33 +00:00
Rui Ueyama a1b79dff2a Handle input section liveness only in MarkLive.cpp.
The condition whether a section is alive or not by default
is becoming increasingly complex, so the decision of garbage
collection is spreading over InputSection.h and MarkLive.cpp,
which is not a good state.

This moves the code to MarkLive.cpp, to keep the file the central
place to make decisions about garbage collection.

llvm-svn: 315384
2017-10-10 22:59:32 +00:00
Ben Dunbobbin 73eabf23a4 [ELF] Simpler scheme for handling common symbols
Convert all common symbols to regular symbols after scan.
This means that the downstream code does not to handle common symbols as a special case.

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

llvm-svn: 314495
2017-09-29 09:08:26 +00:00
NAKAMURA Takumi 169dbde262 Revert rL313697, "Compact EhSectionPiece from 32 bytes to 16 bytes."
It broke selfhosting.
http://lab.llvm.org:8011/builders/clang-with-lto-ubuntu/builds/4896

llvm-svn: 313731
2017-09-20 08:03:18 +00:00
Rui Ueyama 014b0f24ae Compact EhSectionPiece from 32 bytes to 16 bytes.
EhSectionPiece used to have a pointer to a section, but that pointer was
mostly redundant because we almost always know what the section is without
using that pointer. This patch removes the pointer from the struct.

This patch also use uint32_t/int32_t instead of size_t to represent
offsets that are hardly be larger than 4 GiB. At the moment, I think it is
OK even if we cannot handle .eh_frame sections larger than 4 GiB.

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

llvm-svn: 313697
2017-09-19 23:36:48 +00:00
Rui Ueyama a6ff617967 Remove useless accessor.
llvm-svn: 313586
2017-09-18 23:07:09 +00:00
Rui Ueyama 27fbd1e14a Remove ResolvedReloc.
ResolvedReloc struct is always passed to a callback function and not
stored anywhere. That is, in effect, we use the struct to pack multiple
arguments to one argument, which doesn't make much sense. This patch
removes the struct and passes the members to the callback directly.

llvm-svn: 310620
2017-08-10 16:21:04 +00:00
Rui Ueyama 9c77d27004 Garbage-collect common symbols.
Liveness is usually a notion of input sections, but this patch adds
"liveness" bit to common symbols because they don't belong to any
input section.

This patch is based on https://reviews.llvm.org/D36520

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

llvm-svn: 310617
2017-08-10 15:54:27 +00:00
Rafael Espindola 6c4f5b41a3 Fix the name of the section end symbol.
It is __stop_<sec>, not __end_<sec>.

llvm-svn: 309225
2017-07-26 22:52:53 +00:00
Rafael Espindola 244ef98161 Detemplate SymbolTable.
NFC, just makes it easier to access from non templated code.

llvm-svn: 309152
2017-07-26 18:42:48 +00:00
Peter Collingbourne bac3570d53 ELF: Don't dereference Repl in MarkLive. NFCI.
This is unnecessary because --gc-sections runs before ICF.

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

llvm-svn: 305954
2017-06-21 21:29:51 +00:00
Petr Hosek 40f2866a67 [ELF] Mark symbols referenced from linker script as live
This is necessary to ensure that sections containing symbols referenced
from linker scripts (e.g. in data commands) don't get GC'd.

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

llvm-svn: 305452
2017-06-15 05:34:31 +00:00
George Rimar b940f65cd2 [ELF] - Do not crash when proccessing absolute symbols during GC.
This is PR33239, previously we segfault on given testcase.

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

llvm-svn: 304392
2017-06-01 07:47:56 +00:00
George Rimar a8dba48762 [ELF] - Combine LinkerScriptBase and LinkerScript<ELFT>
Patch removes templated linkerscript class.

Unfortunately that required 2 additional static methods
findSymbol() and addRegularSymbol() because code
depends on Symtab<ELFT>::X

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

llvm-svn: 298241
2017-03-20 10:09:58 +00:00
Rui Ueyama d57e74b7d3 Compute Config member function return values only once.
We had a few Config member functions that returns configuration values.
For example, we had is64() which returns true if the target is 64-bit.
The return values of these functions are constant and never change.

This patch is to compute them only once to make it clear that they'll
never change.

llvm-svn: 298168
2017-03-17 23:29:01 +00:00
Evgeniy Stepanov 4b78036ba8 [ELF] Restore GC handling of LINK_ORDER, C-named sections.
__start_xxx symbol keeps section xxx alive only if it is not
SHF_LINK_ORDER. Such sections can be used for user metadata, when
__start_xxx is used to iterate over section contents at runtime, and
the liveness is determined solely by the linked (associated) section.

This was earlier implemented in r294592, and broken in r296723.

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

llvm-svn: 298154
2017-03-17 22:04:52 +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
Rui Ueyama df8eb17d21 Remove Config->Mips64EL and define Config->isMips64EL() instead.
llvm-svn: 297107
2017-03-07 00:43:33 +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
Rafael Espindola 692b2f88d3 Fully precise gc handling of __start and __stop symbols.
This puts us at parity with bfd, which could already gc this case.

I noticed the sections not being gced when linking a modified freebsd
kernel. A section that was not gced and not mentioned in the linker
script would end up breaking the expected layout. Since fixing the gc
is relatively simple and an improvement, that seems better than trying
to hack the orphan placement code.

There are 173 input section in the entire link whose names are valid C
identifiers, so this is probably not too performance critical.

llvm-svn: 297049
2017-03-06 18:48:18 +00:00
Rafael Espindola 066d3d2141 Pass a callback to resolveReloc. NFC.
This is consistent with rest of the file and opens the way for a
relocation keeping multiple sections alive.

llvm-svn: 296788
2017-03-02 19:29:28 +00:00
Rafael Espindola 4368bdb270 Make gc a bit more aggressive.
We were not gcing any section whose name was a C identifier. Both gold
and bfd only keep those if they are used.

To avoid having to create the __start/__stop symbols early or doing
string lookups in resolvedReloc, this patch just looks for undefined
symbols __start/__stop to decide if a section is needed or not.

llvm-svn: 296723
2017-03-02 01:50:34 +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