2020-04-03 02:54:05 +08:00
|
|
|
//===- Symbols.h ------------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLD_MACHO_SYMBOLS_H
|
|
|
|
#define LLD_MACHO_SYMBOLS_H
|
|
|
|
|
[lld-macho] Associate compact unwind entries with function symbols
Compact unwind entries (CUEs) contain pointers to their respective
function symbols. However, during the link process, it's far more useful
to have pointers from the function symbol to the CUE than vice versa.
This diff adds that pointer in the form of `Defined::compactUnwind`.
In particular, when doing dead-stripping, we want to mark CUEs live when
their function symbol is live; and when doing ICF, we want to dedup
sections iff the symbols in that section have identical CUEs. In both
cases, we want to be able to locate the symbols within a given section,
as well as locate the CUEs belonging to those symbols. So this diff also
adds `InputSection::symbols`.
The ultimate goal of this refactor is to have ICF support dedup'ing
functions with unwind info, but that will be handled in subsequent
diffs. This diff focuses on simplifying `-dead_strip` --
`findFunctionsWithUnwindInfo` is no longer necessary, and
`Defined::isLive()` is now a lot simpler. Moreover, UnwindInfoSection no
longer has to check for dead CUEs -- we simply avoid adding them in the
first place.
Additionally, we now support stripping of dead LSDAs, which follows
quite naturally since `markLive()` can now reach them via the CUEs.
Reviewed By: #lld-macho, gkm
Differential Revision: https://reviews.llvm.org/D109944
2021-10-27 04:04:04 +08:00
|
|
|
#include "Config.h"
|
2021-02-04 02:31:40 +08:00
|
|
|
#include "InputFiles.h"
|
2020-04-03 02:54:05 +08:00
|
|
|
#include "Target.h"
|
2020-05-19 11:28:50 +08:00
|
|
|
#include "lld/Common/ErrorHandler.h"
|
2020-04-03 02:54:05 +08:00
|
|
|
#include "lld/Common/Strings.h"
|
|
|
|
#include "llvm/Object/Archive.h"
|
2020-09-25 05:44:14 +08:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2020-04-03 02:54:05 +08:00
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace macho {
|
|
|
|
|
2020-07-31 05:28:41 +08:00
|
|
|
class MachHeaderSection;
|
2020-04-03 02:54:05 +08:00
|
|
|
|
|
|
|
struct StringRefZ {
|
|
|
|
StringRefZ(const char *s) : data(s), size(-1) {}
|
|
|
|
StringRefZ(StringRef s) : data(s.data()), size(s.size()) {}
|
|
|
|
|
|
|
|
const char *data;
|
|
|
|
const uint32_t size;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Symbol {
|
|
|
|
public:
|
|
|
|
enum Kind {
|
|
|
|
DefinedKind,
|
|
|
|
UndefinedKind,
|
2020-09-25 05:44:14 +08:00
|
|
|
CommonKind,
|
2020-04-22 04:37:57 +08:00
|
|
|
DylibKind,
|
2022-01-12 08:49:06 +08:00
|
|
|
LazyArchiveKind,
|
2022-01-20 02:14:49 +08:00
|
|
|
LazyObjectKind,
|
2020-04-03 02:54:05 +08:00
|
|
|
};
|
|
|
|
|
2020-07-25 06:55:25 +08:00
|
|
|
virtual ~Symbol() {}
|
|
|
|
|
2021-03-26 02:59:54 +08:00
|
|
|
Kind kind() const { return symbolKind; }
|
2020-04-03 02:54:05 +08:00
|
|
|
|
[lld/mac] Implement support for private extern symbols
Private extern symbols are used for things scoped to the linkage unit.
They cause duplicate symbol errors (so they're in the symbol table,
unlike TU-scoped truly local symbols), but they don't make it into the
export trie. They are created e.g. by compiling with
-fvisibility=hidden.
If two weak symbols have differing privateness, the combined symbol is
non-private external. (Example: inline functions and some TUs that
include the header defining it were built with
-fvisibility-inlines-hidden and some weren't).
A weak private external symbol implicitly has its "weak" dropped and
behaves like a regular strong private external symbol: Weak is an export
trie concept, and private symbols are not in the export trie.
If a weak and a strong symbol have different privateness, the strong
symbol wins.
If two common symbols have differing privateness, the larger symbol
wins. If they have the same size, the privateness of the symbol seen
later during the link wins (!) -- this is a bit lame, but it matches
ld64 and this behavior takes 2 lines less to implement than the less
surprising "result is non-private external), so match ld64.
(Example: `int a` in two .c files, both built with -fcommon,
one built with -fvisibility=hidden and one without.)
This also makes `__dyld_private` a true TU-local symbol, matching ld64.
To make this work, make the `const char*` StringRefZ ctor to correctly
set `size` (without this, writing the string table crashed when calling
getName() on the __dyld_private symbol).
Mention in CommonSymbol's comment that common symbols are now disabled
by default in clang.
Mention in -keep_private_externs's HelpText that the flag only has an
effect with `-r` (which we don't implement yet -- so this patch here
doesn't regress any behavior around -r + -keep_private_externs)). ld64
doesn't explicitly document it, but the commit text of
http://reviews.llvm.org/rL216146 does, and ld64's
OutputFile::buildSymbolTable() checks `_options.outputKind() ==
Options::kObjectFile` before calling `_options.keepPrivateExterns()`
(the only reference to that function).
Fixes PR48536.
Differential Revision: https://reviews.llvm.org/D93609
2020-12-18 02:30:18 +08:00
|
|
|
StringRef getName() const {
|
|
|
|
if (nameSize == (uint32_t)-1)
|
|
|
|
nameSize = strlen(nameData);
|
|
|
|
return {nameData, nameSize};
|
|
|
|
}
|
2020-04-03 02:54:05 +08:00
|
|
|
|
[lld-macho] Associate compact unwind entries with function symbols
Compact unwind entries (CUEs) contain pointers to their respective
function symbols. However, during the link process, it's far more useful
to have pointers from the function symbol to the CUE than vice versa.
This diff adds that pointer in the form of `Defined::compactUnwind`.
In particular, when doing dead-stripping, we want to mark CUEs live when
their function symbol is live; and when doing ICF, we want to dedup
sections iff the symbols in that section have identical CUEs. In both
cases, we want to be able to locate the symbols within a given section,
as well as locate the CUEs belonging to those symbols. So this diff also
adds `InputSection::symbols`.
The ultimate goal of this refactor is to have ICF support dedup'ing
functions with unwind info, but that will be handled in subsequent
diffs. This diff focuses on simplifying `-dead_strip` --
`findFunctionsWithUnwindInfo` is no longer necessary, and
`Defined::isLive()` is now a lot simpler. Moreover, UnwindInfoSection no
longer has to check for dead CUEs -- we simply avoid adding them in the
first place.
Additionally, we now support stripping of dead LSDAs, which follows
quite naturally since `markLive()` can now reach them via the CUEs.
Reviewed By: #lld-macho, gkm
Differential Revision: https://reviews.llvm.org/D109944
2021-10-27 04:04:04 +08:00
|
|
|
bool isLive() const { return used; }
|
2022-01-20 02:14:49 +08:00
|
|
|
bool isLazy() const {
|
|
|
|
return symbolKind == LazyArchiveKind || symbolKind == LazyObjectKind;
|
|
|
|
}
|
[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
|
|
|
|
2020-07-31 05:28:41 +08:00
|
|
|
virtual uint64_t getVA() const { return 0; }
|
2020-04-03 02:54:05 +08:00
|
|
|
|
2020-12-16 10:05:06 +08:00
|
|
|
virtual bool isWeakDef() const { llvm_unreachable("cannot be weak def"); }
|
|
|
|
|
|
|
|
// Only undefined or dylib symbols can be weak references. A weak reference
|
|
|
|
// need not be satisfied at runtime, e.g. due to the symbol not being
|
|
|
|
// available on a given target platform.
|
|
|
|
virtual bool isWeakRef() const { llvm_unreachable("cannot be a weak ref"); }
|
2020-07-25 06:55:25 +08:00
|
|
|
|
2020-08-13 10:50:09 +08:00
|
|
|
virtual bool isTlv() const { llvm_unreachable("cannot be TLV"); }
|
|
|
|
|
2020-08-25 12:57:59 +08:00
|
|
|
// Whether this symbol is in the GOT or TLVPointer sections.
|
|
|
|
bool isInGot() const { return gotIndex != UINT32_MAX; }
|
|
|
|
|
2020-08-28 06:54:42 +08:00
|
|
|
// Whether this symbol is in the StubsSection.
|
|
|
|
bool isInStubs() const { return stubsIndex != UINT32_MAX; }
|
|
|
|
|
2021-03-30 08:33:48 +08:00
|
|
|
uint64_t getStubVA() const;
|
|
|
|
uint64_t getGotVA() const;
|
|
|
|
uint64_t getTlvVA() const;
|
|
|
|
uint64_t resolveBranchVA() const {
|
|
|
|
assert(isa<Defined>(this) || isa<DylibSymbol>(this));
|
|
|
|
return isInStubs() ? getStubVA() : getVA();
|
|
|
|
}
|
|
|
|
uint64_t resolveGotVA() const { return isInGot() ? getGotVA() : getVA(); }
|
|
|
|
uint64_t resolveTlvVA() const { return isInGot() ? getTlvVA() : getVA(); }
|
|
|
|
|
2020-08-13 10:50:09 +08:00
|
|
|
// The index of this symbol in the GOT or the TLVPointer section, depending
|
|
|
|
// on whether it is a thread-local. A given symbol cannot be referenced by
|
|
|
|
// both these sections at once.
|
2020-06-14 11:00:06 +08:00
|
|
|
uint32_t gotIndex = UINT32_MAX;
|
|
|
|
|
2020-08-28 06:54:42 +08:00
|
|
|
uint32_t stubsIndex = UINT32_MAX;
|
|
|
|
|
2020-09-05 09:02:07 +08:00
|
|
|
uint32_t symtabIndex = UINT32_MAX;
|
|
|
|
|
2021-02-04 02:31:40 +08:00
|
|
|
InputFile *getFile() const { return file; }
|
|
|
|
|
2020-04-03 02:54:05 +08:00
|
|
|
protected:
|
2021-02-04 02:31:40 +08:00
|
|
|
Symbol(Kind k, StringRefZ name, InputFile *file)
|
2021-11-13 07:33:21 +08:00
|
|
|
: symbolKind(k), nameData(name.data), file(file), nameSize(name.size),
|
[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
|
|
|
isUsedInRegularObj(!file || isa<ObjFile>(file)),
|
|
|
|
used(!config->deadStrip) {}
|
2020-04-03 02:54:05 +08:00
|
|
|
|
|
|
|
Kind symbolKind;
|
[lld/mac] Implement support for private extern symbols
Private extern symbols are used for things scoped to the linkage unit.
They cause duplicate symbol errors (so they're in the symbol table,
unlike TU-scoped truly local symbols), but they don't make it into the
export trie. They are created e.g. by compiling with
-fvisibility=hidden.
If two weak symbols have differing privateness, the combined symbol is
non-private external. (Example: inline functions and some TUs that
include the header defining it were built with
-fvisibility-inlines-hidden and some weren't).
A weak private external symbol implicitly has its "weak" dropped and
behaves like a regular strong private external symbol: Weak is an export
trie concept, and private symbols are not in the export trie.
If a weak and a strong symbol have different privateness, the strong
symbol wins.
If two common symbols have differing privateness, the larger symbol
wins. If they have the same size, the privateness of the symbol seen
later during the link wins (!) -- this is a bit lame, but it matches
ld64 and this behavior takes 2 lines less to implement than the less
surprising "result is non-private external), so match ld64.
(Example: `int a` in two .c files, both built with -fcommon,
one built with -fvisibility=hidden and one without.)
This also makes `__dyld_private` a true TU-local symbol, matching ld64.
To make this work, make the `const char*` StringRefZ ctor to correctly
set `size` (without this, writing the string table crashed when calling
getName() on the __dyld_private symbol).
Mention in CommonSymbol's comment that common symbols are now disabled
by default in clang.
Mention in -keep_private_externs's HelpText that the flag only has an
effect with `-r` (which we don't implement yet -- so this patch here
doesn't regress any behavior around -r + -keep_private_externs)). ld64
doesn't explicitly document it, but the commit text of
http://reviews.llvm.org/rL216146 does, and ld64's
OutputFile::buildSymbolTable() checks `_options.outputKind() ==
Options::kObjectFile` before calling `_options.keepPrivateExterns()`
(the only reference to that function).
Fixes PR48536.
Differential Revision: https://reviews.llvm.org/D93609
2020-12-18 02:30:18 +08:00
|
|
|
const char *nameData;
|
2021-02-04 02:31:40 +08:00
|
|
|
InputFile *file;
|
2021-11-13 07:33:21 +08:00
|
|
|
mutable uint32_t nameSize;
|
2021-04-16 09:14:29 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
// True if this symbol was referenced by a regular (non-bitcode) object.
|
2021-11-03 04:57:51 +08:00
|
|
|
bool isUsedInRegularObj : 1;
|
[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
|
|
|
|
|
|
|
// True if an undefined or dylib symbol is used from a live section.
|
2021-11-03 04:57:51 +08:00
|
|
|
bool used : 1;
|
2020-04-03 02:54:05 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class Defined : public Symbol {
|
|
|
|
public:
|
2021-04-02 08:48:09 +08:00
|
|
|
Defined(StringRefZ name, InputFile *file, InputSection *isec, uint64_t value,
|
2021-05-01 04:17:26 +08:00
|
|
|
uint64_t size, bool isWeakDef, bool isExternal, bool isPrivateExtern,
|
2021-11-09 08:50:34 +08:00
|
|
|
bool isThumb, bool isReferencedDynamically, bool noDeadStrip,
|
|
|
|
bool canOverrideWeakDef = false, bool isWeakDefCanBeHidden = false);
|
2020-07-25 06:55:25 +08:00
|
|
|
|
|
|
|
bool isWeakDef() const override { return weakDef; }
|
[lld/mac] Implement support for private extern symbols
Private extern symbols are used for things scoped to the linkage unit.
They cause duplicate symbol errors (so they're in the symbol table,
unlike TU-scoped truly local symbols), but they don't make it into the
export trie. They are created e.g. by compiling with
-fvisibility=hidden.
If two weak symbols have differing privateness, the combined symbol is
non-private external. (Example: inline functions and some TUs that
include the header defining it were built with
-fvisibility-inlines-hidden and some weren't).
A weak private external symbol implicitly has its "weak" dropped and
behaves like a regular strong private external symbol: Weak is an export
trie concept, and private symbols are not in the export trie.
If a weak and a strong symbol have different privateness, the strong
symbol wins.
If two common symbols have differing privateness, the larger symbol
wins. If they have the same size, the privateness of the symbol seen
later during the link wins (!) -- this is a bit lame, but it matches
ld64 and this behavior takes 2 lines less to implement than the less
surprising "result is non-private external), so match ld64.
(Example: `int a` in two .c files, both built with -fcommon,
one built with -fvisibility=hidden and one without.)
This also makes `__dyld_private` a true TU-local symbol, matching ld64.
To make this work, make the `const char*` StringRefZ ctor to correctly
set `size` (without this, writing the string table crashed when calling
getName() on the __dyld_private symbol).
Mention in CommonSymbol's comment that common symbols are now disabled
by default in clang.
Mention in -keep_private_externs's HelpText that the flag only has an
effect with `-r` (which we don't implement yet -- so this patch here
doesn't regress any behavior around -r + -keep_private_externs)). ld64
doesn't explicitly document it, but the commit text of
http://reviews.llvm.org/rL216146 does, and ld64's
OutputFile::buildSymbolTable() checks `_options.outputKind() ==
Options::kObjectFile` before calling `_options.keepPrivateExterns()`
(the only reference to that function).
Fixes PR48536.
Differential Revision: https://reviews.llvm.org/D93609
2020-12-18 02:30:18 +08:00
|
|
|
bool isExternalWeakDef() const {
|
|
|
|
return isWeakDef() && isExternal() && !privateExtern;
|
|
|
|
}
|
[lld-macho] Associate compact unwind entries with function symbols
Compact unwind entries (CUEs) contain pointers to their respective
function symbols. However, during the link process, it's far more useful
to have pointers from the function symbol to the CUE than vice versa.
This diff adds that pointer in the form of `Defined::compactUnwind`.
In particular, when doing dead-stripping, we want to mark CUEs live when
their function symbol is live; and when doing ICF, we want to dedup
sections iff the symbols in that section have identical CUEs. In both
cases, we want to be able to locate the symbols within a given section,
as well as locate the CUEs belonging to those symbols. So this diff also
adds `InputSection::symbols`.
The ultimate goal of this refactor is to have ICF support dedup'ing
functions with unwind info, but that will be handled in subsequent
diffs. This diff focuses on simplifying `-dead_strip` --
`findFunctionsWithUnwindInfo` is no longer necessary, and
`Defined::isLive()` is now a lot simpler. Moreover, UnwindInfoSection no
longer has to check for dead CUEs -- we simply avoid adding them in the
first place.
Additionally, we now support stripping of dead LSDAs, which follows
quite naturally since `markLive()` can now reach them via the CUEs.
Reviewed By: #lld-macho, gkm
Differential Revision: https://reviews.llvm.org/D109944
2021-10-27 04:04:04 +08:00
|
|
|
bool isTlv() const override;
|
2020-08-13 10:50:09 +08:00
|
|
|
|
2020-08-25 12:57:59 +08:00
|
|
|
bool isExternal() const { return external; }
|
2020-09-18 23:40:46 +08:00
|
|
|
bool isAbsolute() const { return isec == nullptr; }
|
2020-08-25 12:57:59 +08:00
|
|
|
|
2020-09-18 23:40:46 +08:00
|
|
|
uint64_t getVA() const override;
|
2020-07-31 05:28:41 +08:00
|
|
|
|
2021-10-29 23:00:13 +08:00
|
|
|
// Ensure this symbol's pointers to InputSections point to their canonical
|
|
|
|
// copies.
|
|
|
|
void canonicalize();
|
|
|
|
|
2020-09-18 23:40:46 +08:00
|
|
|
static bool classof(const Symbol *s) { return s->kind() == DefinedKind; }
|
2020-07-31 05:28:41 +08:00
|
|
|
|
2021-11-13 07:33:21 +08:00
|
|
|
// Place the bitfields first so that they can get placed in the tail padding
|
|
|
|
// of the parent class, on platforms which support it.
|
2020-08-28 06:59:30 +08:00
|
|
|
bool overridesWeakDef : 1;
|
2021-03-13 06:26:12 +08:00
|
|
|
// Whether this symbol should appear in the output binary's export trie.
|
[lld/mac] Implement support for private extern symbols
Private extern symbols are used for things scoped to the linkage unit.
They cause duplicate symbol errors (so they're in the symbol table,
unlike TU-scoped truly local symbols), but they don't make it into the
export trie. They are created e.g. by compiling with
-fvisibility=hidden.
If two weak symbols have differing privateness, the combined symbol is
non-private external. (Example: inline functions and some TUs that
include the header defining it were built with
-fvisibility-inlines-hidden and some weren't).
A weak private external symbol implicitly has its "weak" dropped and
behaves like a regular strong private external symbol: Weak is an export
trie concept, and private symbols are not in the export trie.
If a weak and a strong symbol have different privateness, the strong
symbol wins.
If two common symbols have differing privateness, the larger symbol
wins. If they have the same size, the privateness of the symbol seen
later during the link wins (!) -- this is a bit lame, but it matches
ld64 and this behavior takes 2 lines less to implement than the less
surprising "result is non-private external), so match ld64.
(Example: `int a` in two .c files, both built with -fcommon,
one built with -fvisibility=hidden and one without.)
This also makes `__dyld_private` a true TU-local symbol, matching ld64.
To make this work, make the `const char*` StringRefZ ctor to correctly
set `size` (without this, writing the string table crashed when calling
getName() on the __dyld_private symbol).
Mention in CommonSymbol's comment that common symbols are now disabled
by default in clang.
Mention in -keep_private_externs's HelpText that the flag only has an
effect with `-r` (which we don't implement yet -- so this patch here
doesn't regress any behavior around -r + -keep_private_externs)). ld64
doesn't explicitly document it, but the commit text of
http://reviews.llvm.org/rL216146 does, and ld64's
OutputFile::buildSymbolTable() checks `_options.outputKind() ==
Options::kObjectFile` before calling `_options.keepPrivateExterns()`
(the only reference to that function).
Fixes PR48536.
Differential Revision: https://reviews.llvm.org/D93609
2020-12-18 02:30:18 +08:00
|
|
|
bool privateExtern : 1;
|
2021-03-19 06:49:45 +08:00
|
|
|
// Whether this symbol should appear in the output symbol table.
|
|
|
|
bool includeInSymtab : 1;
|
2021-05-01 04:17:26 +08:00
|
|
|
// Only relevant when compiling for Thumb-supporting arm32 archs.
|
|
|
|
bool thumb : 1;
|
2021-05-17 21:15:39 +08:00
|
|
|
// Symbols marked referencedDynamically won't be removed from the output's
|
|
|
|
// symbol table by tools like strip. In theory, this could be set on arbitrary
|
|
|
|
// symbols in input object files. In practice, it's used solely for the
|
|
|
|
// synthetic __mh_execute_header symbol.
|
[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
|
|
|
// This is information for the static linker, and it's also written to the
|
|
|
|
// output file's symbol table for tools running later (such as `strip`).
|
2021-05-17 21:15:39 +08:00
|
|
|
bool referencedDynamically : 1;
|
[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
|
|
|
// Set on symbols that should not be removed by dead code stripping.
|
|
|
|
// Set for example on `__attribute__((used))` globals, or on some Objective-C
|
|
|
|
// metadata. This is information only for the static linker and not written
|
|
|
|
// to the output.
|
|
|
|
bool noDeadStrip : 1;
|
2020-08-28 06:59:30 +08:00
|
|
|
|
2021-11-09 08:50:34 +08:00
|
|
|
bool weakDefCanBeHidden : 1;
|
|
|
|
|
2020-07-25 06:55:25 +08:00
|
|
|
private:
|
2020-08-28 06:59:30 +08:00
|
|
|
const bool weakDef : 1;
|
|
|
|
const bool external : 1;
|
2021-11-13 07:33:21 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
InputSection *isec;
|
|
|
|
// Contains the offset from the containing subsection. Note that this is
|
|
|
|
// different from nlist::n_value, which is the absolute address of the symbol.
|
|
|
|
uint64_t value;
|
|
|
|
// size is only calculated for regular (non-bitcode) symbols.
|
|
|
|
uint64_t size;
|
2021-11-16 02:46:59 +08:00
|
|
|
ConcatInputSection *unwindEntry = nullptr;
|
2020-04-03 02:54:05 +08:00
|
|
|
};
|
|
|
|
|
2020-12-17 08:14:57 +08:00
|
|
|
// This enum does double-duty: as a symbol property, it indicates whether & how
|
|
|
|
// a dylib symbol is referenced. As a DylibFile property, it indicates the kind
|
|
|
|
// of referenced symbols contained within the file. If there are both weak
|
|
|
|
// and strong references to the same file, we will count the file as
|
|
|
|
// strongly-referenced.
|
2020-12-16 10:05:06 +08:00
|
|
|
enum class RefState : uint8_t { Unreferenced = 0, Weak = 1, Strong = 2 };
|
|
|
|
|
2020-04-03 02:54:05 +08:00
|
|
|
class Undefined : public Symbol {
|
|
|
|
public:
|
2021-02-04 02:31:40 +08:00
|
|
|
Undefined(StringRefZ name, InputFile *file, RefState refState)
|
|
|
|
: Symbol(UndefinedKind, name, file), refState(refState) {
|
2020-12-16 10:05:06 +08:00
|
|
|
assert(refState != RefState::Unreferenced);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isWeakRef() const override { return refState == RefState::Weak; }
|
2020-04-03 02:54:05 +08:00
|
|
|
|
|
|
|
static bool classof(const Symbol *s) { return s->kind() == UndefinedKind; }
|
2020-12-16 10:05:06 +08:00
|
|
|
|
|
|
|
RefState refState : 2;
|
2020-04-03 02:54:05 +08:00
|
|
|
};
|
|
|
|
|
2020-09-25 05:44:14 +08:00
|
|
|
// On Unix, it is traditionally allowed to write variable definitions without
|
|
|
|
// initialization expressions (such as "int foo;") to header files. These are
|
|
|
|
// called tentative definitions.
|
|
|
|
//
|
|
|
|
// Using tentative definitions is usually considered a bad practice; you should
|
|
|
|
// write only declarations (such as "extern int foo;") to header files.
|
|
|
|
// Nevertheless, the linker and the compiler have to do something to support
|
|
|
|
// bad code by allowing duplicate definitions for this particular case.
|
|
|
|
//
|
|
|
|
// The compiler creates common symbols when it sees tentative definitions.
|
|
|
|
// (You can suppress this behavior and let the compiler create a regular
|
[lld/mac] Implement support for private extern symbols
Private extern symbols are used for things scoped to the linkage unit.
They cause duplicate symbol errors (so they're in the symbol table,
unlike TU-scoped truly local symbols), but they don't make it into the
export trie. They are created e.g. by compiling with
-fvisibility=hidden.
If two weak symbols have differing privateness, the combined symbol is
non-private external. (Example: inline functions and some TUs that
include the header defining it were built with
-fvisibility-inlines-hidden and some weren't).
A weak private external symbol implicitly has its "weak" dropped and
behaves like a regular strong private external symbol: Weak is an export
trie concept, and private symbols are not in the export trie.
If a weak and a strong symbol have different privateness, the strong
symbol wins.
If two common symbols have differing privateness, the larger symbol
wins. If they have the same size, the privateness of the symbol seen
later during the link wins (!) -- this is a bit lame, but it matches
ld64 and this behavior takes 2 lines less to implement than the less
surprising "result is non-private external), so match ld64.
(Example: `int a` in two .c files, both built with -fcommon,
one built with -fvisibility=hidden and one without.)
This also makes `__dyld_private` a true TU-local symbol, matching ld64.
To make this work, make the `const char*` StringRefZ ctor to correctly
set `size` (without this, writing the string table crashed when calling
getName() on the __dyld_private symbol).
Mention in CommonSymbol's comment that common symbols are now disabled
by default in clang.
Mention in -keep_private_externs's HelpText that the flag only has an
effect with `-r` (which we don't implement yet -- so this patch here
doesn't regress any behavior around -r + -keep_private_externs)). ld64
doesn't explicitly document it, but the commit text of
http://reviews.llvm.org/rL216146 does, and ld64's
OutputFile::buildSymbolTable() checks `_options.outputKind() ==
Options::kObjectFile` before calling `_options.keepPrivateExterns()`
(the only reference to that function).
Fixes PR48536.
Differential Revision: https://reviews.llvm.org/D93609
2020-12-18 02:30:18 +08:00
|
|
|
// defined symbol by passing -fno-common. -fno-common is the default in clang
|
|
|
|
// as of LLVM 11.0.) When linking the final binary, if there are remaining
|
|
|
|
// common symbols after name resolution is complete, the linker converts them
|
|
|
|
// to regular defined symbols in a __common section.
|
2020-09-25 05:44:14 +08:00
|
|
|
class CommonSymbol : public Symbol {
|
|
|
|
public:
|
[lld/mac] Implement support for private extern symbols
Private extern symbols are used for things scoped to the linkage unit.
They cause duplicate symbol errors (so they're in the symbol table,
unlike TU-scoped truly local symbols), but they don't make it into the
export trie. They are created e.g. by compiling with
-fvisibility=hidden.
If two weak symbols have differing privateness, the combined symbol is
non-private external. (Example: inline functions and some TUs that
include the header defining it were built with
-fvisibility-inlines-hidden and some weren't).
A weak private external symbol implicitly has its "weak" dropped and
behaves like a regular strong private external symbol: Weak is an export
trie concept, and private symbols are not in the export trie.
If a weak and a strong symbol have different privateness, the strong
symbol wins.
If two common symbols have differing privateness, the larger symbol
wins. If they have the same size, the privateness of the symbol seen
later during the link wins (!) -- this is a bit lame, but it matches
ld64 and this behavior takes 2 lines less to implement than the less
surprising "result is non-private external), so match ld64.
(Example: `int a` in two .c files, both built with -fcommon,
one built with -fvisibility=hidden and one without.)
This also makes `__dyld_private` a true TU-local symbol, matching ld64.
To make this work, make the `const char*` StringRefZ ctor to correctly
set `size` (without this, writing the string table crashed when calling
getName() on the __dyld_private symbol).
Mention in CommonSymbol's comment that common symbols are now disabled
by default in clang.
Mention in -keep_private_externs's HelpText that the flag only has an
effect with `-r` (which we don't implement yet -- so this patch here
doesn't regress any behavior around -r + -keep_private_externs)). ld64
doesn't explicitly document it, but the commit text of
http://reviews.llvm.org/rL216146 does, and ld64's
OutputFile::buildSymbolTable() checks `_options.outputKind() ==
Options::kObjectFile` before calling `_options.keepPrivateExterns()`
(the only reference to that function).
Fixes PR48536.
Differential Revision: https://reviews.llvm.org/D93609
2020-12-18 02:30:18 +08:00
|
|
|
CommonSymbol(StringRefZ name, InputFile *file, uint64_t size, uint32_t align,
|
|
|
|
bool isPrivateExtern)
|
2021-02-04 02:31:40 +08:00
|
|
|
: Symbol(CommonKind, name, file), size(size),
|
[lld/mac] Implement support for private extern symbols
Private extern symbols are used for things scoped to the linkage unit.
They cause duplicate symbol errors (so they're in the symbol table,
unlike TU-scoped truly local symbols), but they don't make it into the
export trie. They are created e.g. by compiling with
-fvisibility=hidden.
If two weak symbols have differing privateness, the combined symbol is
non-private external. (Example: inline functions and some TUs that
include the header defining it were built with
-fvisibility-inlines-hidden and some weren't).
A weak private external symbol implicitly has its "weak" dropped and
behaves like a regular strong private external symbol: Weak is an export
trie concept, and private symbols are not in the export trie.
If a weak and a strong symbol have different privateness, the strong
symbol wins.
If two common symbols have differing privateness, the larger symbol
wins. If they have the same size, the privateness of the symbol seen
later during the link wins (!) -- this is a bit lame, but it matches
ld64 and this behavior takes 2 lines less to implement than the less
surprising "result is non-private external), so match ld64.
(Example: `int a` in two .c files, both built with -fcommon,
one built with -fvisibility=hidden and one without.)
This also makes `__dyld_private` a true TU-local symbol, matching ld64.
To make this work, make the `const char*` StringRefZ ctor to correctly
set `size` (without this, writing the string table crashed when calling
getName() on the __dyld_private symbol).
Mention in CommonSymbol's comment that common symbols are now disabled
by default in clang.
Mention in -keep_private_externs's HelpText that the flag only has an
effect with `-r` (which we don't implement yet -- so this patch here
doesn't regress any behavior around -r + -keep_private_externs)). ld64
doesn't explicitly document it, but the commit text of
http://reviews.llvm.org/rL216146 does, and ld64's
OutputFile::buildSymbolTable() checks `_options.outputKind() ==
Options::kObjectFile` before calling `_options.keepPrivateExterns()`
(the only reference to that function).
Fixes PR48536.
Differential Revision: https://reviews.llvm.org/D93609
2020-12-18 02:30:18 +08:00
|
|
|
align(align != 1 ? align : llvm::PowerOf2Ceil(size)),
|
|
|
|
privateExtern(isPrivateExtern) {
|
2020-09-25 05:44:14 +08:00
|
|
|
// TODO: cap maximum alignment
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool classof(const Symbol *s) { return s->kind() == CommonKind; }
|
|
|
|
|
|
|
|
const uint64_t size;
|
|
|
|
const uint32_t align;
|
[lld/mac] Implement support for private extern symbols
Private extern symbols are used for things scoped to the linkage unit.
They cause duplicate symbol errors (so they're in the symbol table,
unlike TU-scoped truly local symbols), but they don't make it into the
export trie. They are created e.g. by compiling with
-fvisibility=hidden.
If two weak symbols have differing privateness, the combined symbol is
non-private external. (Example: inline functions and some TUs that
include the header defining it were built with
-fvisibility-inlines-hidden and some weren't).
A weak private external symbol implicitly has its "weak" dropped and
behaves like a regular strong private external symbol: Weak is an export
trie concept, and private symbols are not in the export trie.
If a weak and a strong symbol have different privateness, the strong
symbol wins.
If two common symbols have differing privateness, the larger symbol
wins. If they have the same size, the privateness of the symbol seen
later during the link wins (!) -- this is a bit lame, but it matches
ld64 and this behavior takes 2 lines less to implement than the less
surprising "result is non-private external), so match ld64.
(Example: `int a` in two .c files, both built with -fcommon,
one built with -fvisibility=hidden and one without.)
This also makes `__dyld_private` a true TU-local symbol, matching ld64.
To make this work, make the `const char*` StringRefZ ctor to correctly
set `size` (without this, writing the string table crashed when calling
getName() on the __dyld_private symbol).
Mention in CommonSymbol's comment that common symbols are now disabled
by default in clang.
Mention in -keep_private_externs's HelpText that the flag only has an
effect with `-r` (which we don't implement yet -- so this patch here
doesn't regress any behavior around -r + -keep_private_externs)). ld64
doesn't explicitly document it, but the commit text of
http://reviews.llvm.org/rL216146 does, and ld64's
OutputFile::buildSymbolTable() checks `_options.outputKind() ==
Options::kObjectFile` before calling `_options.keepPrivateExterns()`
(the only reference to that function).
Fixes PR48536.
Differential Revision: https://reviews.llvm.org/D93609
2020-12-18 02:30:18 +08:00
|
|
|
const bool privateExtern;
|
2020-09-25 05:44:14 +08:00
|
|
|
};
|
|
|
|
|
2020-04-22 04:37:57 +08:00
|
|
|
class DylibSymbol : public Symbol {
|
|
|
|
public:
|
2020-12-16 10:05:06 +08:00
|
|
|
DylibSymbol(DylibFile *file, StringRefZ name, bool isWeakDef,
|
|
|
|
RefState refState, bool isTlv)
|
2021-02-04 02:31:40 +08:00
|
|
|
: Symbol(DylibKind, name, file), refState(refState), weakDef(isWeakDef),
|
2021-06-01 10:12:35 +08:00
|
|
|
tlv(isTlv) {
|
|
|
|
if (file && refState > RefState::Unreferenced)
|
|
|
|
file->numReferencedSymbols++;
|
|
|
|
}
|
2020-07-25 06:55:25 +08:00
|
|
|
|
2021-03-30 08:33:48 +08:00
|
|
|
uint64_t getVA() const override;
|
2020-07-25 06:55:25 +08:00
|
|
|
bool isWeakDef() const override { return weakDef; }
|
2021-11-23 06:14:20 +08:00
|
|
|
|
|
|
|
// Symbols from weak libraries/frameworks are also weakly-referenced.
|
|
|
|
bool isWeakRef() const override {
|
|
|
|
return refState == RefState::Weak ||
|
|
|
|
(file && getFile()->umbrella->forceWeakImport);
|
|
|
|
}
|
2020-12-16 10:05:06 +08:00
|
|
|
bool isReferenced() const { return refState != RefState::Unreferenced; }
|
2020-08-13 10:50:09 +08:00
|
|
|
bool isTlv() const override { return tlv; }
|
2021-02-26 08:56:31 +08:00
|
|
|
bool isDynamicLookup() const { return file == nullptr; }
|
2020-08-28 06:54:42 +08:00
|
|
|
bool hasStubsHelper() const { return stubsHelperIndex != UINT32_MAX; }
|
2021-02-26 08:56:31 +08:00
|
|
|
|
|
|
|
DylibFile *getFile() const {
|
|
|
|
assert(!isDynamicLookup());
|
|
|
|
return cast<DylibFile>(file);
|
|
|
|
}
|
2020-08-13 10:50:09 +08:00
|
|
|
|
2020-04-22 04:37:57 +08:00
|
|
|
static bool classof(const Symbol *s) { return s->kind() == DylibKind; }
|
|
|
|
|
2020-08-28 06:54:42 +08:00
|
|
|
uint32_t stubsHelperIndex = UINT32_MAX;
|
[lld-macho] Support calls to functions in dylibs
Summary:
This diff implements lazy symbol binding -- very similar to the PLT
mechanism in ELF.
ELF's .plt section is broken up into two sections in Mach-O:
StubsSection and StubHelperSection. Calls to functions in dylibs will
end up calling into StubsSection, which contains indirect jumps to
addresses stored in the LazyPointerSection (the counterpart to ELF's
.plt.got).
Initially, the LazyPointerSection contains addresses that point into one
of the entry points in the middle of the StubHelperSection. The code in
StubHelperSection will push on the stack an offset into the
LazyBindingSection. The push is followed by a jump to the beginning of
the StubHelperSection (similar to PLT0), which then calls into
dyld_stub_binder. dyld_stub_binder is a non-lazily bound symbol, so this
call looks it up in the GOT.
The stub binder will look up the bind opcodes in the LazyBindingSection
at the given offset. The bind opcodes will tell the binder to update the
address in the LazyPointerSection to point to the symbol, so that
subsequent calls don't have to redo the symbol resolution. The binder
will then jump to the resolved symbol.
Depends on D78269.
Reviewers: ruiu, pcc, MaskRay, smeenai, alexshap, gkm, Ktwu, christylee
Subscribers: llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D78270
2020-05-06 08:38:10 +08:00
|
|
|
uint32_t lazyBindOffset = UINT32_MAX;
|
2020-07-25 06:55:25 +08:00
|
|
|
|
2021-06-01 10:12:35 +08:00
|
|
|
RefState getRefState() const { return refState; }
|
|
|
|
|
|
|
|
void reference(RefState newState) {
|
|
|
|
assert(newState > RefState::Unreferenced);
|
|
|
|
if (refState == RefState::Unreferenced && file)
|
|
|
|
getFile()->numReferencedSymbols++;
|
|
|
|
refState = std::max(refState, newState);
|
|
|
|
}
|
|
|
|
|
|
|
|
void unreference() {
|
|
|
|
// dynamic_lookup symbols have no file.
|
|
|
|
if (refState > RefState::Unreferenced && file) {
|
|
|
|
assert(getFile()->numReferencedSymbols > 0);
|
|
|
|
getFile()->numReferencedSymbols--;
|
|
|
|
}
|
|
|
|
}
|
2020-12-16 10:05:06 +08:00
|
|
|
|
2020-07-25 06:55:25 +08:00
|
|
|
private:
|
2021-06-01 10:12:35 +08:00
|
|
|
RefState refState : 2;
|
2020-12-16 10:05:06 +08:00
|
|
|
const bool weakDef : 1;
|
|
|
|
const bool tlv : 1;
|
2020-04-22 04:37:57 +08:00
|
|
|
};
|
|
|
|
|
2022-01-12 08:49:06 +08:00
|
|
|
class LazyArchive : public Symbol {
|
2020-05-15 03:43:51 +08:00
|
|
|
public:
|
2022-01-12 08:49:06 +08:00
|
|
|
LazyArchive(ArchiveFile *file, const llvm::object::Archive::Symbol &sym)
|
|
|
|
: Symbol(LazyArchiveKind, sym.getName(), file), sym(sym) {}
|
2020-05-15 03:43:51 +08:00
|
|
|
|
2021-02-04 02:31:40 +08:00
|
|
|
ArchiveFile *getFile() const { return cast<ArchiveFile>(file); }
|
2020-05-15 03:43:51 +08:00
|
|
|
void fetchArchiveMember();
|
|
|
|
|
2022-01-12 08:49:06 +08:00
|
|
|
static bool classof(const Symbol *s) { return s->kind() == LazyArchiveKind; }
|
2021-02-04 02:31:40 +08:00
|
|
|
|
2020-05-15 03:43:51 +08:00
|
|
|
private:
|
|
|
|
const llvm::object::Archive::Symbol sym;
|
|
|
|
};
|
|
|
|
|
2022-01-20 02:14:49 +08:00
|
|
|
// A defined symbol in an ObjFile/BitcodeFile surrounded by --start-lib and
|
|
|
|
// --end-lib.
|
|
|
|
class LazyObject : public Symbol {
|
|
|
|
public:
|
|
|
|
LazyObject(InputFile &file, StringRef name)
|
|
|
|
: Symbol(LazyObjectKind, name, &file) {
|
|
|
|
isUsedInRegularObj = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool classof(const Symbol *s) { return s->kind() == LazyObjectKind; }
|
|
|
|
};
|
|
|
|
|
2020-04-03 02:54:05 +08:00
|
|
|
union SymbolUnion {
|
|
|
|
alignas(Defined) char a[sizeof(Defined)];
|
|
|
|
alignas(Undefined) char b[sizeof(Undefined)];
|
2020-09-25 05:44:14 +08:00
|
|
|
alignas(CommonSymbol) char c[sizeof(CommonSymbol)];
|
|
|
|
alignas(DylibSymbol) char d[sizeof(DylibSymbol)];
|
2022-01-12 08:49:06 +08:00
|
|
|
alignas(LazyArchive) char e[sizeof(LazyArchive)];
|
2022-01-20 02:14:49 +08:00
|
|
|
alignas(LazyObject) char f[sizeof(LazyObject)];
|
2020-04-03 02:54:05 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, typename... ArgT>
|
2021-04-07 02:05:15 +08:00
|
|
|
T *replaceSymbol(Symbol *s, ArgT &&...arg) {
|
2020-04-03 02:54:05 +08:00
|
|
|
static_assert(sizeof(T) <= sizeof(SymbolUnion), "SymbolUnion too small");
|
|
|
|
static_assert(alignof(T) <= alignof(SymbolUnion),
|
|
|
|
"SymbolUnion not aligned enough");
|
|
|
|
assert(static_cast<Symbol *>(static_cast<T *>(nullptr)) == nullptr &&
|
|
|
|
"Not a Symbol");
|
|
|
|
|
2021-04-16 09:14:29 +08:00
|
|
|
bool isUsedInRegularObj = s->isUsedInRegularObj;
|
2021-07-22 23:37:29 +08:00
|
|
|
bool used = s->used;
|
2021-04-16 09:14:29 +08:00
|
|
|
T *sym = new (s) T(std::forward<ArgT>(arg)...);
|
|
|
|
sym->isUsedInRegularObj |= isUsedInRegularObj;
|
2021-07-22 23:37:29 +08:00
|
|
|
sym->used |= used;
|
2021-04-16 09:14:29 +08:00
|
|
|
return sym;
|
2020-04-03 02:54:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace macho
|
|
|
|
|
|
|
|
std::string toString(const macho::Symbol &);
|
2020-11-20 23:14:57 +08:00
|
|
|
std::string toMachOString(const llvm::object::Archive::Symbol &);
|
|
|
|
|
2020-04-03 02:54:05 +08:00
|
|
|
} // namespace lld
|
|
|
|
|
|
|
|
#endif
|