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
|
|
|
|
|
2021-02-04 02:31:40 +08:00
|
|
|
#include "InputFiles.h"
|
2020-04-03 02:54:05 +08:00
|
|
|
#include "InputSection.h"
|
|
|
|
#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 {
|
|
|
|
|
|
|
|
class InputSection;
|
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,
|
2020-05-15 03:43:51 +08:00
|
|
|
LazyKind,
|
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/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 isLive() const;
|
|
|
|
|
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-04-16 09:14:29 +08:00
|
|
|
: symbolKind(k), nameData(name.data), nameSize(name.size), file(file),
|
[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;
|
|
|
|
mutable uint32_t nameSize;
|
2021-02-04 02:31:40 +08:00
|
|
|
InputFile *file;
|
2021-04-16 09:14:29 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
// True if this symbol was referenced by a regular (non-bitcode) object.
|
[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 isUsedInRegularObj : 1;
|
|
|
|
|
|
|
|
// True if an undefined or dylib symbol is used from a live section.
|
|
|
|
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,
|
[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 isThumb, bool isReferencedDynamically, bool noDeadStrip)
|
2021-04-02 08:48:09 +08:00
|
|
|
: Symbol(DefinedKind, name, file), isec(isec), value(value), 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
|
|
|
overridesWeakDef(false), privateExtern(isPrivateExtern),
|
2021-05-17 21:15:39 +08:00
|
|
|
includeInSymtab(true), thumb(isThumb),
|
[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
|
|
|
referencedDynamically(isReferencedDynamically),
|
|
|
|
noDeadStrip(noDeadStrip), weakDef(isWeakDef), external(isExternal) {
|
2021-06-12 07:49:52 +08:00
|
|
|
if (auto concatIsec = dyn_cast_or_null<ConcatInputSection>(isec))
|
|
|
|
concatIsec->numRefs++;
|
[lld/mac] Write every weak symbol only once in the output
Before this, if an inline function was defined in several input files,
lld would write each copy of the inline function the output. With this
patch, it only writes one copy.
Reduces the size of Chromium Framework from 378MB to 345MB (compared
to 290MB linked with ld64, which also does dead-stripping, which we
don't do yet), and makes linking it faster:
N Min Max Median Avg Stddev
x 10 3.9957051 4.3496981 4.1411121 4.156837 0.10092097
+ 10 3.908154 4.169318 3.9712729 3.9846753 0.075773012
Difference at 95.0% confidence
-0.172162 +/- 0.083847
-4.14165% +/- 2.01709%
(Student's t, pooled s = 0.0892373)
Implementation-wise, when merging two weak symbols, this sets a
"canOmitFromOutput" on the InputSection belonging to the weak symbol not put in
the symbol table. We then don't write InputSections that have this set, as long
as they are not referenced from other symbols. (This happens e.g. for object
files that don't set .subsections_via_symbols or that use .alt_entry.)
Some restrictions:
- not yet done for bitcode inputs
- no "comdat" handling (`kindNoneGroupSubordinate*` in ld64) --
Frame Descriptor Entries (FDEs), Language Specific Data Areas (LSDAs)
(that is, catch block unwind information) and Personality Routines
associated with weak functions still not stripped. This is wasteful,
but harmless.
- However, this does strip weaks from __unwind_info (which is needed for
correctness and not just for size)
- This nopes out on InputSections that are referenced form more than
one symbol (eg from .alt_entry) for now
Things that work based on symbols Just Work:
- map files (change in MapFile.cpp is no-op and not needed; I just
found it a bit more explicit)
- exports
Things that work with inputSections need to explicitly check if
an inputSection is written (e.g. unwind info).
This patch is useful in itself, but it's also likely also a useful foundation
for dead_strip.
I used to have a "canoncialRepresentative" pointer on InputSection instead of
just the bool, which would be handy for ICF too. But I ended up not needing it
for this patch, so I removed that again for now.
Differential Revision: https://reviews.llvm.org/D102076
2021-05-07 02:47:57 +08:00
|
|
|
}
|
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;
|
|
|
|
}
|
2020-09-18 23:40:46 +08:00
|
|
|
bool isTlv() const override {
|
|
|
|
return !isAbsolute() && isThreadLocalVariables(isec->flags);
|
|
|
|
}
|
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
|
|
|
|
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
|
|
|
|
2020-04-03 02:54:05 +08:00
|
|
|
InputSection *isec;
|
2021-04-07 03:09:14 +08:00
|
|
|
// Contains the offset from the containing subsection. Note that this is
|
|
|
|
// different from nlist::n_value, which is the absolute address of the symbol.
|
2021-04-02 08:48:09 +08:00
|
|
|
uint64_t value;
|
2021-04-07 03:09:14 +08:00
|
|
|
// size is only calculated for regular (non-bitcode) symbols.
|
2021-04-02 08:48:09 +08:00
|
|
|
uint64_t size;
|
2020-04-03 02:54:05 +08:00
|
|
|
|
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
|
|
|
|
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;
|
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; }
|
2020-12-16 10:05:06 +08:00
|
|
|
bool isWeakRef() const override { return refState == RefState::Weak; }
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2020-05-15 03:43:51 +08:00
|
|
|
class LazySymbol : public Symbol {
|
|
|
|
public:
|
|
|
|
LazySymbol(ArchiveFile *file, const llvm::object::Archive::Symbol &sym)
|
2021-02-04 02:31:40 +08:00
|
|
|
: Symbol(LazyKind, 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();
|
|
|
|
|
2021-02-04 02:31:40 +08:00
|
|
|
static bool classof(const Symbol *s) { return s->kind() == LazyKind; }
|
|
|
|
|
2020-05-15 03:43:51 +08:00
|
|
|
private:
|
|
|
|
const llvm::object::Archive::Symbol sym;
|
|
|
|
};
|
|
|
|
|
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)];
|
|
|
|
alignas(LazySymbol) char e[sizeof(LazySymbol)];
|
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;
|
|
|
|
T *sym = new (s) T(std::forward<ArgT>(arg)...);
|
|
|
|
sym->isUsedInRegularObj |= isUsedInRegularObj;
|
|
|
|
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
|