2020-04-03 02:54:05 +08:00
|
|
|
//===- SymbolTable.cpp ----------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "SymbolTable.h"
|
2020-12-14 11:31:33 +08:00
|
|
|
#include "Config.h"
|
2020-04-03 02:54:05 +08:00
|
|
|
#include "InputFiles.h"
|
|
|
|
#include "Symbols.h"
|
|
|
|
#include "lld/Common/ErrorHandler.h"
|
|
|
|
#include "lld/Common/Memory.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::macho;
|
|
|
|
|
|
|
|
Symbol *SymbolTable::find(StringRef name) {
|
|
|
|
auto it = symMap.find(llvm::CachedHashStringRef(name));
|
|
|
|
if (it == symMap.end())
|
|
|
|
return nullptr;
|
|
|
|
return symVector[it->second];
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<Symbol *, bool> SymbolTable::insert(StringRef name) {
|
|
|
|
auto p = symMap.insert({CachedHashStringRef(name), (int)symVector.size()});
|
|
|
|
|
|
|
|
// Name already present in the symbol table.
|
|
|
|
if (!p.second)
|
|
|
|
return {symVector[p.first->second], false};
|
|
|
|
|
|
|
|
// Name is a new symbol.
|
|
|
|
Symbol *sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
|
|
|
|
symVector.push_back(sym);
|
|
|
|
return {sym, true};
|
|
|
|
}
|
|
|
|
|
|
|
|
Symbol *SymbolTable::addDefined(StringRef name, InputSection *isec,
|
[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
|
|
|
uint32_t value, bool isWeakDef,
|
|
|
|
bool isPrivateExtern) {
|
2020-04-03 02:54:05 +08:00
|
|
|
Symbol *s;
|
|
|
|
bool wasInserted;
|
2020-08-28 06:59:30 +08:00
|
|
|
bool overridesWeakDef = false;
|
2020-04-03 02:54:05 +08:00
|
|
|
std::tie(s, wasInserted) = insert(name);
|
|
|
|
|
2020-07-25 06:55:25 +08:00
|
|
|
if (!wasInserted) {
|
|
|
|
if (auto *defined = dyn_cast<Defined>(s)) {
|
[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
|
|
|
if (isWeakDef) {
|
|
|
|
// Both old and new symbol weak (e.g. inline function in two TUs):
|
|
|
|
// If one of them isn't private extern, the merged symbol isn't.
|
|
|
|
if (defined->isWeakDef())
|
|
|
|
defined->privateExtern &= isPrivateExtern;
|
2020-07-25 06:55:25 +08:00
|
|
|
return s;
|
[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
|
|
|
}
|
2020-07-25 06:55:25 +08:00
|
|
|
if (!defined->isWeakDef())
|
|
|
|
error("duplicate symbol: " + name);
|
2020-08-28 06:59:30 +08:00
|
|
|
} else if (auto *dysym = dyn_cast<DylibSymbol>(s)) {
|
|
|
|
overridesWeakDef = !isWeakDef && dysym->isWeakDef();
|
2020-07-25 06:55:25 +08:00
|
|
|
}
|
|
|
|
// Defined symbols take priority over other types of symbols, so in case
|
|
|
|
// of a name conflict, we fall through to the replaceSymbol() call below.
|
|
|
|
}
|
|
|
|
|
[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 *defined =
|
|
|
|
replaceSymbol<Defined>(s, name, isec, value, isWeakDef,
|
|
|
|
/*isExternal=*/true, isPrivateExtern);
|
2020-08-28 06:59:30 +08:00
|
|
|
defined->overridesWeakDef = overridesWeakDef;
|
2020-04-03 02:54:05 +08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2020-12-16 10:05:06 +08:00
|
|
|
Symbol *SymbolTable::addUndefined(StringRef name, bool isWeakRef) {
|
2020-04-03 02:54:05 +08:00
|
|
|
Symbol *s;
|
|
|
|
bool wasInserted;
|
|
|
|
std::tie(s, wasInserted) = insert(name);
|
|
|
|
|
2020-12-16 10:05:06 +08:00
|
|
|
auto refState = isWeakRef ? RefState::Weak : RefState::Strong;
|
|
|
|
|
2020-04-03 02:54:05 +08:00
|
|
|
if (wasInserted)
|
2020-12-16 10:05:06 +08:00
|
|
|
replaceSymbol<Undefined>(s, name, refState);
|
|
|
|
else if (auto *lazy = dyn_cast<LazySymbol>(s))
|
2020-05-15 03:43:51 +08:00
|
|
|
lazy->fetchArchiveMember();
|
2020-12-16 10:05:06 +08:00
|
|
|
else if (auto *dynsym = dyn_cast<DylibSymbol>(s))
|
|
|
|
dynsym->refState = std::max(dynsym->refState, refState);
|
|
|
|
else if (auto *undefined = dyn_cast<Undefined>(s))
|
|
|
|
undefined->refState = std::max(undefined->refState, refState);
|
2020-04-03 02:54:05 +08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2020-09-25 05:44:14 +08:00
|
|
|
Symbol *SymbolTable::addCommon(StringRef name, InputFile *file, uint64_t 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
|
|
|
uint32_t align, bool isPrivateExtern) {
|
2020-09-25 05:44:14 +08:00
|
|
|
Symbol *s;
|
|
|
|
bool wasInserted;
|
|
|
|
std::tie(s, wasInserted) = insert(name);
|
|
|
|
|
|
|
|
if (!wasInserted) {
|
|
|
|
if (auto *common = dyn_cast<CommonSymbol>(s)) {
|
|
|
|
if (size < common->size)
|
|
|
|
return s;
|
2020-09-25 06:00:56 +08:00
|
|
|
} else if (isa<Defined>(s)) {
|
2020-09-25 05:44:14 +08:00
|
|
|
return s;
|
|
|
|
}
|
2020-09-25 06:00:56 +08:00
|
|
|
// Common symbols take priority over all non-Defined symbols, so in case of
|
|
|
|
// a name conflict, we fall through to the replaceSymbol() call below.
|
2020-09-25 05:44:14 +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
|
|
|
replaceSymbol<CommonSymbol>(s, name, file, size, align, isPrivateExtern);
|
2020-09-25 05:44:14 +08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2020-08-13 10:50:09 +08:00
|
|
|
Symbol *SymbolTable::addDylib(StringRef name, DylibFile *file, bool isWeakDef,
|
|
|
|
bool isTlv) {
|
2020-04-22 04:37:57 +08:00
|
|
|
Symbol *s;
|
|
|
|
bool wasInserted;
|
|
|
|
std::tie(s, wasInserted) = insert(name);
|
|
|
|
|
2020-12-16 10:05:06 +08:00
|
|
|
auto refState = RefState::Unreferenced;
|
|
|
|
if (!wasInserted) {
|
|
|
|
if (auto *defined = dyn_cast<Defined>(s)) {
|
|
|
|
if (isWeakDef && !defined->isWeakDef())
|
2020-08-28 06:59:30 +08:00
|
|
|
defined->overridesWeakDef = true;
|
2020-12-16 10:05:06 +08:00
|
|
|
} else if (auto *undefined = dyn_cast<Undefined>(s)) {
|
|
|
|
refState = undefined->refState;
|
|
|
|
} else if (auto *dysym = dyn_cast<DylibSymbol>(s)) {
|
|
|
|
refState = dysym->refState;
|
|
|
|
}
|
|
|
|
}
|
2020-08-28 06:59:30 +08:00
|
|
|
|
2020-07-25 06:55:25 +08:00
|
|
|
if (wasInserted || isa<Undefined>(s) ||
|
|
|
|
(isa<DylibSymbol>(s) && !isWeakDef && s->isWeakDef()))
|
2020-12-16 10:05:06 +08:00
|
|
|
replaceSymbol<DylibSymbol>(s, file, name, isWeakDef, refState, isTlv);
|
2020-07-25 06:55:25 +08:00
|
|
|
|
2020-04-22 04:37:57 +08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2020-05-15 03:43:51 +08:00
|
|
|
Symbol *SymbolTable::addLazy(StringRef name, ArchiveFile *file,
|
|
|
|
const llvm::object::Archive::Symbol &sym) {
|
|
|
|
Symbol *s;
|
|
|
|
bool wasInserted;
|
|
|
|
std::tie(s, wasInserted) = insert(name);
|
|
|
|
|
|
|
|
if (wasInserted)
|
|
|
|
replaceSymbol<LazySymbol>(s, file, sym);
|
2020-07-25 06:55:25 +08:00
|
|
|
else if (isa<Undefined>(s) || (isa<DylibSymbol>(s) && s->isWeakDef()))
|
2020-05-15 03:43:51 +08:00
|
|
|
file->fetch(sym);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2020-07-31 05:28:41 +08:00
|
|
|
Symbol *SymbolTable::addDSOHandle(const MachHeaderSection *header) {
|
|
|
|
Symbol *s;
|
|
|
|
bool wasInserted;
|
|
|
|
std::tie(s, wasInserted) = insert(DSOHandle::name);
|
|
|
|
if (!wasInserted) {
|
2020-09-18 23:40:46 +08:00
|
|
|
// FIXME: Make every symbol (including absolute symbols) contain a
|
|
|
|
// reference to their originating file, then add that file name to this
|
|
|
|
// error message.
|
2020-11-19 17:47:47 +08:00
|
|
|
if (isa<Defined>(s))
|
2020-09-18 23:40:46 +08:00
|
|
|
error("found defined symbol with illegal name " + DSOHandle::name);
|
2020-07-31 05:28:41 +08:00
|
|
|
}
|
|
|
|
replaceSymbol<DSOHandle>(s, header);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2020-12-14 11:31:33 +08:00
|
|
|
void lld::macho::treatUndefinedSymbol(StringRef symbolName,
|
|
|
|
StringRef fileName) {
|
|
|
|
std::string message = ("undefined symbol: " + symbolName).str();
|
|
|
|
if (!fileName.empty())
|
|
|
|
message += ("\n>>> referenced by " + fileName).str();
|
|
|
|
switch (config->undefinedSymbolTreatment) {
|
|
|
|
case UndefinedSymbolTreatment::suppress:
|
|
|
|
break;
|
|
|
|
case UndefinedSymbolTreatment::error:
|
|
|
|
error(message);
|
|
|
|
break;
|
|
|
|
case UndefinedSymbolTreatment::warning:
|
|
|
|
warn(message);
|
|
|
|
break;
|
|
|
|
case UndefinedSymbolTreatment::dynamic_lookup:
|
|
|
|
error("dynamic_lookup unimplemented for " + message);
|
|
|
|
break;
|
|
|
|
case UndefinedSymbolTreatment::unknown:
|
|
|
|
llvm_unreachable("unknown -undefined TREATMENT");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 02:54:05 +08:00
|
|
|
SymbolTable *macho::symtab;
|