2020-04-03 02:54:05 +08:00
|
|
|
//===- SymbolTable.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_SYMBOL_TABLE_H
|
|
|
|
#define LLD_MACHO_SYMBOL_TABLE_H
|
|
|
|
|
2021-03-19 06:49:45 +08:00
|
|
|
#include "Symbols.h"
|
|
|
|
|
2020-04-03 02:54:05 +08:00
|
|
|
#include "lld/Common/LLVM.h"
|
|
|
|
#include "llvm/ADT/CachedHashString.h"
|
2020-04-27 21:05:29 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2020-04-03 02:54:05 +08:00
|
|
|
#include "llvm/Object/Archive.h"
|
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace macho {
|
|
|
|
|
|
|
|
class ArchiveFile;
|
2020-04-22 04:37:57 +08:00
|
|
|
class DylibFile;
|
2020-09-25 05:44:14 +08:00
|
|
|
class InputFile;
|
2021-02-04 02:31:40 +08:00
|
|
|
class ObjFile;
|
2020-04-22 04:37:57 +08:00
|
|
|
class InputSection;
|
2020-07-31 05:28:41 +08:00
|
|
|
class MachHeaderSection;
|
2020-04-03 02:54:05 +08:00
|
|
|
class Symbol;
|
2021-03-13 06:26:12 +08:00
|
|
|
class Defined;
|
2021-02-04 02:31:40 +08:00
|
|
|
class Undefined;
|
2020-04-03 02:54:05 +08:00
|
|
|
|
2020-07-25 06:55:25 +08:00
|
|
|
/*
|
|
|
|
* Note that the SymbolTable handles name collisions by calling
|
|
|
|
* replaceSymbol(), which does an in-place update of the Symbol via `placement
|
|
|
|
* new`. Therefore, there is no need to update any relocations that hold
|
|
|
|
* pointers the "old" Symbol -- they will automatically point to the new one.
|
|
|
|
*/
|
2020-04-03 02:54:05 +08:00
|
|
|
class SymbolTable {
|
|
|
|
public:
|
2021-03-13 06:26:12 +08:00
|
|
|
Defined *addDefined(StringRef name, InputFile *, InputSection *,
|
2021-04-02 08:48:09 +08:00
|
|
|
uint64_t value, uint64_t size, bool isWeakDef,
|
2021-05-01 04:17:26 +08:00
|
|
|
bool isPrivateExtern, bool isThumb);
|
2020-04-03 02:54:05 +08:00
|
|
|
|
2021-02-04 02:31:40 +08:00
|
|
|
Symbol *addUndefined(StringRef name, InputFile *, bool isWeakRef);
|
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
|
|
|
Symbol *addCommon(StringRef name, InputFile *, uint64_t size, uint32_t align,
|
|
|
|
bool isPrivateExtern);
|
2020-09-25 05:44:14 +08:00
|
|
|
|
2020-08-13 10:50:09 +08:00
|
|
|
Symbol *addDylib(StringRef name, DylibFile *file, bool isWeakDef, bool isTlv);
|
2021-02-26 08:56:31 +08:00
|
|
|
Symbol *addDynamicLookup(StringRef name);
|
2020-04-22 04:37:57 +08:00
|
|
|
|
2020-05-15 03:43:51 +08:00
|
|
|
Symbol *addLazy(StringRef name, ArchiveFile *file,
|
|
|
|
const llvm::object::Archive::Symbol &sym);
|
|
|
|
|
2021-04-07 05:52:30 +08:00
|
|
|
Defined *addSynthetic(StringRef name, InputSection *, uint64_t value,
|
2021-03-19 06:49:45 +08:00
|
|
|
bool isPrivateExtern, bool includeInSymtab);
|
2020-07-31 05:28:41 +08:00
|
|
|
|
2020-04-03 02:54:05 +08:00
|
|
|
ArrayRef<Symbol *> getSymbols() const { return symVector; }
|
2021-03-11 08:45:18 +08:00
|
|
|
Symbol *find(llvm::CachedHashStringRef name);
|
|
|
|
Symbol *find(StringRef name) { return find(llvm::CachedHashStringRef(name)); }
|
2020-04-03 02:54:05 +08:00
|
|
|
|
|
|
|
private:
|
2021-04-16 09:14:29 +08:00
|
|
|
std::pair<Symbol *, bool> insert(StringRef name, const InputFile *);
|
2020-04-03 02:54:05 +08:00
|
|
|
llvm::DenseMap<llvm::CachedHashStringRef, int> symMap;
|
|
|
|
std::vector<Symbol *> symVector;
|
|
|
|
};
|
|
|
|
|
2021-05-11 03:45:18 +08:00
|
|
|
void treatUndefinedSymbol(const Undefined &, StringRef source = "");
|
2020-12-14 11:31:33 +08:00
|
|
|
|
2020-04-03 02:54:05 +08:00
|
|
|
extern SymbolTable *symtab;
|
|
|
|
|
|
|
|
} // namespace macho
|
|
|
|
} // namespace lld
|
|
|
|
|
|
|
|
#endif
|