1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
//===- 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
#include "InputSection.h"
#include "Target.h"
#include "lld/Common/ErrorHandler.h"
#include "lld/Common/Strings.h"
#include "llvm/Object/Archive.h"
namespace lld {
namespace macho {
class InputSection;
class DylibFile;
class ArchiveFile;
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,
DylibKind,
LazyKind,
};
Kind kind() const { return static_cast<Kind>(symbolKind); }
StringRef getName() const { return {name.data, name.size}; }
uint64_t getVA() const;
uint64_t getFileOffset() const;
uint32_t gotIndex = UINT32_MAX;
protected:
Symbol(Kind k, StringRefZ name) : symbolKind(k), name(name) {}
Kind symbolKind;
StringRefZ name;
};
class Defined : public Symbol {
public:
Defined(StringRefZ name, InputSection *isec, uint32_t value)
: Symbol(DefinedKind, name), isec(isec), value(value) {}
InputSection *isec;
uint32_t value;
static bool classof(const Symbol *s) { return s->kind() == DefinedKind; }
};
class Undefined : public Symbol {
public:
Undefined(StringRefZ name) : Symbol(UndefinedKind, name) {}
static bool classof(const Symbol *s) { return s->kind() == UndefinedKind; }
};
class DylibSymbol : public Symbol {
public:
DylibSymbol(DylibFile *file, StringRefZ name)
: Symbol(DylibKind, name), file(file) {}
static bool classof(const Symbol *s) { return s->kind() == DylibKind; }
DylibFile *file;
uint32_t stubsIndex = UINT32_MAX;
uint32_t lazyBindOffset = UINT32_MAX;
};
class LazySymbol : public Symbol {
public:
LazySymbol(ArchiveFile *file, const llvm::object::Archive::Symbol &sym)
: Symbol(LazyKind, sym.getName()), file(file), sym(sym) {}
static bool classof(const Symbol *s) { return s->kind() == LazyKind; }
void fetchArchiveMember();
private:
ArchiveFile *file;
const llvm::object::Archive::Symbol sym;
};
inline uint64_t Symbol::getVA() const {
if (auto *d = dyn_cast<Defined>(this))
return d->isec->getVA() + d->value;
return 0;
}
inline uint64_t Symbol::getFileOffset() const {
if (auto *d = dyn_cast<Defined>(this))
return d->isec->getFileOffset() + d->value;
llvm_unreachable("attempt to get an offset from an undefined symbol");
}
union SymbolUnion {
alignas(Defined) char a[sizeof(Defined)];
alignas(Undefined) char b[sizeof(Undefined)];
alignas(DylibSymbol) char c[sizeof(DylibSymbol)];
alignas(LazySymbol) char d[sizeof(LazySymbol)];
};
template <typename T, typename... ArgT>
void replaceSymbol(Symbol *s, ArgT &&... arg) {
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");
new (s) T(std::forward<ArgT>(arg)...);
}
} // namespace macho
std::string toString(const macho::Symbol &);
} // namespace lld
#endif
|