2020-04-03 02:54:05 +08:00
|
|
|
//===- Symbols.cpp --------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Symbols.h"
|
|
|
|
#include "InputFiles.h"
|
2020-07-31 05:28:41 +08:00
|
|
|
#include "SyntheticSections.h"
|
2020-04-03 02:54:05 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::macho;
|
|
|
|
|
2020-11-20 23:14:57 +08:00
|
|
|
// Returns a symbol for an error message.
|
|
|
|
static std::string demangle(StringRef symName) {
|
|
|
|
if (config->demangle)
|
|
|
|
return demangleItanium(symName);
|
|
|
|
return std::string(symName);
|
|
|
|
}
|
|
|
|
|
2021-04-07 02:05:15 +08:00
|
|
|
std::string lld::toString(const Symbol &sym) { return demangle(sym.getName()); }
|
2020-11-20 23:14:57 +08:00
|
|
|
|
|
|
|
std::string lld::toMachOString(const object::Archive::Symbol &b) {
|
|
|
|
return demangle(b.getName());
|
|
|
|
}
|
|
|
|
|
2021-03-30 08:33:48 +08:00
|
|
|
uint64_t Symbol::getStubVA() const { return in.stubs->getVA(stubsIndex); }
|
|
|
|
uint64_t Symbol::getGotVA() const { return in.got->getVA(gotIndex); }
|
|
|
|
uint64_t Symbol::getTlvVA() const { return in.tlvPointers->getVA(gotIndex); }
|
|
|
|
|
[lld/mac] Implement -dead_strip
Also adds support for live_support sections, no_dead_strip sections,
.no_dead_strip symbols.
Chromium Framework 345MB unstripped -> 250MB stripped
(vs 290MB unstripped -> 236M stripped with ld64).
Doing dead stripping is a bit faster than not, because so much less
data needs to be processed:
% ministat lld_*
x lld_nostrip.txt
+ lld_strip.txt
N Min Max Median Avg Stddev
x 10 3.929414 4.07692 4.0269079 4.0089678 0.044214794
+ 10 3.8129408 3.9025559 3.8670411 3.8642573 0.024779651
Difference at 95.0% confidence
-0.144711 +/- 0.0336749
-3.60967% +/- 0.839989%
(Student's t, pooled s = 0.0358398)
This interacts with many parts of the linker. I tried to add test coverage
for all added `isLive()` checks, so that some test will fail if any of them
is removed. I checked that the test expectations for the most part match
ld64's behavior (except for live-support-iterations.s, see the comment
in the test). Interacts with:
- debug info
- export tries
- import opcodes
- flags like -exported_symbol(s_list)
- -U / dynamic_lookup
- mod_init_funcs, mod_term_funcs
- weak symbol handling
- unwind info
- stubs
- map files
- -sectcreate
- undefined, dylib, common, defined (both absolute and normal) symbols
It's possible it interacts with more features I didn't think of,
of course.
I also did some manual testing:
- check-llvm check-clang check-lld work with lld with this patch
as host linker and -dead_strip enabled
- Chromium still starts
- Chromium's base_unittests still pass, including unwind tests
Implemenation-wise, this is InputSection-based, so it'll work for
object files with .subsections_via_symbols (which includes all
object files generated by clang). I first based this on the COFF
implementation, but later realized that things are more similar to ELF.
I think it'd be good to refactor MarkLive.cpp to look more like the ELF
part at some point, but I'd like to get a working state checked in first.
Mechanical parts:
- Rename canOmitFromOutput to wasCoalesced (no behavior change)
since it really is for weak coalesced symbols
- Add noDeadStrip to Defined, corresponding to N_NO_DEAD_STRIP
(`.no_dead_strip` in asm)
Fixes PR49276.
Differential Revision: https://reviews.llvm.org/D103324
2021-05-08 05:10:05 +08:00
|
|
|
bool Symbol::isLive() const {
|
|
|
|
if (isa<DylibSymbol>(this) || isa<Undefined>(this))
|
|
|
|
return used;
|
|
|
|
|
|
|
|
if (auto *d = dyn_cast<Defined>(this)) {
|
|
|
|
// Non-absolute symbols might be alive because their section is
|
|
|
|
// no_dead_strip or live_support. In that case, the section will know
|
|
|
|
// that it's live but `used` might be false. Non-absolute symbols always
|
|
|
|
// have to use the section's `live` bit as source of truth.
|
2021-05-20 00:58:17 +08:00
|
|
|
if (d->isAbsolute())
|
|
|
|
return used;
|
|
|
|
return d->isec->canonical()->isLive(d->value);
|
[lld/mac] Implement -dead_strip
Also adds support for live_support sections, no_dead_strip sections,
.no_dead_strip symbols.
Chromium Framework 345MB unstripped -> 250MB stripped
(vs 290MB unstripped -> 236M stripped with ld64).
Doing dead stripping is a bit faster than not, because so much less
data needs to be processed:
% ministat lld_*
x lld_nostrip.txt
+ lld_strip.txt
N Min Max Median Avg Stddev
x 10 3.929414 4.07692 4.0269079 4.0089678 0.044214794
+ 10 3.8129408 3.9025559 3.8670411 3.8642573 0.024779651
Difference at 95.0% confidence
-0.144711 +/- 0.0336749
-3.60967% +/- 0.839989%
(Student's t, pooled s = 0.0358398)
This interacts with many parts of the linker. I tried to add test coverage
for all added `isLive()` checks, so that some test will fail if any of them
is removed. I checked that the test expectations for the most part match
ld64's behavior (except for live-support-iterations.s, see the comment
in the test). Interacts with:
- debug info
- export tries
- import opcodes
- flags like -exported_symbol(s_list)
- -U / dynamic_lookup
- mod_init_funcs, mod_term_funcs
- weak symbol handling
- unwind info
- stubs
- map files
- -sectcreate
- undefined, dylib, common, defined (both absolute and normal) symbols
It's possible it interacts with more features I didn't think of,
of course.
I also did some manual testing:
- check-llvm check-clang check-lld work with lld with this patch
as host linker and -dead_strip enabled
- Chromium still starts
- Chromium's base_unittests still pass, including unwind tests
Implemenation-wise, this is InputSection-based, so it'll work for
object files with .subsections_via_symbols (which includes all
object files generated by clang). I first based this on the COFF
implementation, but later realized that things are more similar to ELF.
I think it'd be good to refactor MarkLive.cpp to look more like the ELF
part at some point, but I'd like to get a working state checked in first.
Mechanical parts:
- Rename canOmitFromOutput to wasCoalesced (no behavior change)
since it really is for weak coalesced symbols
- Add noDeadStrip to Defined, corresponding to N_NO_DEAD_STRIP
(`.no_dead_strip` in asm)
Fixes PR49276.
Differential Revision: https://reviews.llvm.org/D103324
2021-05-08 05:10:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(!isa<CommonSymbol>(this) &&
|
|
|
|
"replaceCommonSymbols() runs before dead code stripping, and isLive() "
|
|
|
|
"should only be called after dead code stripping");
|
|
|
|
|
|
|
|
// Assume any other kind of symbol is live.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-09-18 23:40:46 +08:00
|
|
|
uint64_t Defined::getVA() const {
|
[lld/mac] Implement -dead_strip
Also adds support for live_support sections, no_dead_strip sections,
.no_dead_strip symbols.
Chromium Framework 345MB unstripped -> 250MB stripped
(vs 290MB unstripped -> 236M stripped with ld64).
Doing dead stripping is a bit faster than not, because so much less
data needs to be processed:
% ministat lld_*
x lld_nostrip.txt
+ lld_strip.txt
N Min Max Median Avg Stddev
x 10 3.929414 4.07692 4.0269079 4.0089678 0.044214794
+ 10 3.8129408 3.9025559 3.8670411 3.8642573 0.024779651
Difference at 95.0% confidence
-0.144711 +/- 0.0336749
-3.60967% +/- 0.839989%
(Student's t, pooled s = 0.0358398)
This interacts with many parts of the linker. I tried to add test coverage
for all added `isLive()` checks, so that some test will fail if any of them
is removed. I checked that the test expectations for the most part match
ld64's behavior (except for live-support-iterations.s, see the comment
in the test). Interacts with:
- debug info
- export tries
- import opcodes
- flags like -exported_symbol(s_list)
- -U / dynamic_lookup
- mod_init_funcs, mod_term_funcs
- weak symbol handling
- unwind info
- stubs
- map files
- -sectcreate
- undefined, dylib, common, defined (both absolute and normal) symbols
It's possible it interacts with more features I didn't think of,
of course.
I also did some manual testing:
- check-llvm check-clang check-lld work with lld with this patch
as host linker and -dead_strip enabled
- Chromium still starts
- Chromium's base_unittests still pass, including unwind tests
Implemenation-wise, this is InputSection-based, so it'll work for
object files with .subsections_via_symbols (which includes all
object files generated by clang). I first based this on the COFF
implementation, but later realized that things are more similar to ELF.
I think it'd be good to refactor MarkLive.cpp to look more like the ELF
part at some point, but I'd like to get a working state checked in first.
Mechanical parts:
- Rename canOmitFromOutput to wasCoalesced (no behavior change)
since it really is for weak coalesced symbols
- Add noDeadStrip to Defined, corresponding to N_NO_DEAD_STRIP
(`.no_dead_strip` in asm)
Fixes PR49276.
Differential Revision: https://reviews.llvm.org/D103324
2021-05-08 05:10:05 +08:00
|
|
|
assert(isLive() && "this should only be called for live symbols");
|
|
|
|
|
2020-09-18 23:40:46 +08:00
|
|
|
if (isAbsolute())
|
|
|
|
return value;
|
2021-03-30 08:33:48 +08:00
|
|
|
|
2021-05-20 00:58:17 +08:00
|
|
|
if (!isec->canonical()->isFinal) {
|
2021-03-30 08:33:48 +08:00
|
|
|
// A target arch that does not use thunks ought never ask for
|
|
|
|
// the address of a function that has not yet been finalized.
|
|
|
|
assert(target->usesThunks());
|
|
|
|
|
2021-05-26 02:57:16 +08:00
|
|
|
// ConcatOutputSection::finalize() can seek the address of a
|
2021-03-30 08:33:48 +08:00
|
|
|
// function before its address is assigned. The thunking algorithm
|
|
|
|
// knows that unfinalized functions will be out of range, so it is
|
|
|
|
// expedient to return a contrived out-of-range address.
|
|
|
|
return TargetInfo::outOfRangeVA;
|
|
|
|
}
|
2021-05-20 00:58:17 +08:00
|
|
|
return isec->canonical()->getVA(value);
|
2020-09-18 23:40:46 +08:00
|
|
|
}
|
|
|
|
|
2021-03-30 08:33:48 +08:00
|
|
|
uint64_t DylibSymbol::getVA() const {
|
|
|
|
return isInStubs() ? getStubVA() : Symbol::getVA();
|
|
|
|
}
|
|
|
|
|
2021-02-04 02:31:40 +08:00
|
|
|
void LazySymbol::fetchArchiveMember() { getFile()->fetch(sym); }
|