184 changes: 91 additions & 93 deletions lld/COFF/Symbols.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,33 @@ struct Symbol {
class SymbolBody {
public:
enum Kind {
DefinedFirst,
DefinedBitcodeKind,
DefinedAbsoluteKind,
DefinedImportDataKind,
DefinedImportThunkKind,
DefinedLocalImportKind,
// The order of these is significant. We start with the regular defined
// symbols as those are the most prevelant and the zero tag is the cheapest
// to set. Among the defined kinds, the lower the kind is preferred over
// the higher kind when testing wether one symbol should take precedence
// over another.
DefinedRegularKind = 0,
DefinedCommonKind,
DefinedRegularKind,
DefinedLast,
DefinedLocalImportKind,
DefinedImportThunkKind,
DefinedImportDataKind,
DefinedAbsoluteKind,
DefinedBitcodeKind,

LazyKind,
UndefinedKind,

LastDefinedCOFFKind = DefinedCommonKind,
LastDefinedKind = DefinedBitcodeKind,
};

Kind kind() const { return SymbolKind; }
virtual ~SymbolBody() {}
Kind kind() const { return static_cast<Kind>(SymbolKind); }

// Returns true if this is an external symbol.
virtual bool isExternal() { return true; }
bool isExternal() { return IsExternal; }

// Returns the symbol name.
virtual StringRef getName() = 0;
StringRef getName();

// A SymbolBody has a backreference to a Symbol. Originally they are
// doubly-linked. A backreference will never change. But the pointer
Expand All @@ -77,124 +83,134 @@ class SymbolBody {
// Decides which symbol should "win" in the symbol table, this or
// the Other. Returns 1 if this wins, -1 if the Other wins, or 0 if
// they are duplicate (conflicting) symbols.
virtual int compare(SymbolBody *Other) = 0;
int compare(SymbolBody *Other);

// Returns a name of this symbol including source file name.
// Used only for debugging and logging.
virtual std::string getDebugName() { return getName(); }
std::string getDebugName();

protected:
SymbolBody(Kind K) : SymbolKind(K) {}
explicit SymbolBody(Kind K, StringRef N = "")
: SymbolKind(K), IsExternal(true), IsCOMDAT(false),
IsReplaceable(false), Name(N) {}

private:
const Kind SymbolKind;
const unsigned SymbolKind : 8;
unsigned IsExternal : 1;

// This bit is used by the \c DefinedRegular subclass.
unsigned IsCOMDAT : 1;

// This bit is used by the \c DefinedBitcode subclass.
unsigned IsReplaceable : 1;

StringRef Name;
Symbol *Backref = nullptr;
};

// The base class for any defined symbols, including absolute symbols,
// etc.
class Defined : public SymbolBody {
public:
Defined(Kind K) : SymbolBody(K) {}
Defined(Kind K, StringRef N = "") : SymbolBody(K, N) {}

static bool classof(const SymbolBody *S) {
Kind K = S->kind();
return DefinedFirst <= K && K <= DefinedLast;
return S->kind() <= LastDefinedKind;
}

// Returns the RVA (relative virtual address) of this symbol. The
// writer sets and uses RVAs.
virtual uint64_t getRVA() = 0;
uint64_t getRVA();

// Returns the file offset of this symbol in the final executable.
// The writer uses this information to apply relocations.
virtual uint64_t getFileOff() = 0;
uint64_t getFileOff();
};

int compare(SymbolBody *Other) override;
// Symbols defined via a COFF object file.
class DefinedCOFF : public Defined {
friend SymbolBody;
public:
DefinedCOFF(Kind K, ObjectFile *F, COFFSymbolRef S)
: Defined(K), File(F), Sym(S) {}

static bool classof(const SymbolBody *S) {
return S->kind() <= LastDefinedCOFFKind;
}

protected:
ObjectFile *File;
COFFSymbolRef Sym;
};

// Regular defined symbols read from object file symbol tables.
class DefinedRegular : public Defined {
class DefinedRegular : public DefinedCOFF {
public:
DefinedRegular(ObjectFile *F, COFFSymbolRef S, SectionChunk *C)
: Defined(DefinedRegularKind), File(F), Sym(S), Data(&C->Ptr),
IsCOMDAT(C->isCOMDAT()) {}
: DefinedCOFF(DefinedRegularKind, F, S), Data(&C->Ptr) {
IsExternal = Sym.isExternal();
IsCOMDAT = C->isCOMDAT();
}

static bool classof(const SymbolBody *S) {
return S->kind() == DefinedRegularKind;
}

uint64_t getFileOff() override {
uint64_t getFileOff() {
return (*Data)->getFileOff() + Sym.getValue();
}

StringRef getName() override;
uint64_t getRVA() override { return (*Data)->getRVA() + Sym.getValue(); }
bool isExternal() override { return Sym.isExternal(); }
int compare(SymbolBody *Other) override;
std::string getDebugName() override;
uint64_t getRVA() { return (*Data)->getRVA() + Sym.getValue(); }
bool isCOMDAT() { return IsCOMDAT; }
bool isLive() const { return (*Data)->isLive(); }
void markLive() { (*Data)->markLive(); }
SectionChunk *getChunk() { return *Data; }
uint64_t getValue() { return Sym.getValue(); }

private:
StringRef Name;
ObjectFile *File;
COFFSymbolRef Sym;
SectionChunk **Data;
bool IsCOMDAT;
};

class DefinedCommon : public Defined {
class DefinedCommon : public DefinedCOFF {
public:
DefinedCommon(ObjectFile *F, COFFSymbolRef S, CommonChunk *C)
: Defined(DefinedCommonKind), File(F), Sym(S), Data(C) {}
: DefinedCOFF(DefinedCommonKind, F, S), Data(C) {
IsExternal = Sym.isExternal();
}

static bool classof(const SymbolBody *S) {
return S->kind() == DefinedCommonKind;
}

StringRef getName() override;
uint64_t getRVA() override { return Data->getRVA(); }
bool isExternal() override { return Sym.isExternal(); }
uint64_t getFileOff() override { return Data->getFileOff(); }
int compare(SymbolBody *Other) override;
std::string getDebugName() override;
uint64_t getRVA() { return Data->getRVA(); }
uint64_t getFileOff() { return Data->getFileOff(); }

private:
friend SymbolBody;

uint64_t getSize() { return Sym.getValue(); }

StringRef Name;
ObjectFile *File;
COFFSymbolRef Sym;
CommonChunk *Data;
};

// Absolute symbols.
class DefinedAbsolute : public Defined {
public:
DefinedAbsolute(StringRef N, COFFSymbolRef S)
: Defined(DefinedAbsoluteKind), Name(N), VA(S.getValue()),
External(S.isExternal()) {}
: Defined(DefinedAbsoluteKind, N), VA(S.getValue()) {
IsExternal = S.isExternal();
}

DefinedAbsolute(StringRef N, uint64_t V)
: Defined(DefinedAbsoluteKind), Name(N), VA(V) {}
: Defined(DefinedAbsoluteKind, N), VA(V) {}

static bool classof(const SymbolBody *S) {
return S->kind() == DefinedAbsoluteKind;
}

StringRef getName() override { return Name; }
uint64_t getRVA() override { return VA - Config->ImageBase; }
uint64_t getFileOff() override { llvm_unreachable("internal error"); }
bool isExternal() override { return External; }
uint64_t getRVA() { return VA - Config->ImageBase; }

private:
StringRef Name;
uint64_t VA;
bool External = true;
};

// This class represents a symbol defined in an archive file. It is
Expand All @@ -205,19 +221,15 @@ class DefinedAbsolute : public Defined {
class Lazy : public SymbolBody {
public:
Lazy(ArchiveFile *F, const Archive::Symbol S)
: SymbolBody(LazyKind), Name(S.getName()), File(F), Sym(S) {}
: SymbolBody(LazyKind, S.getName()), File(F), Sym(S) {}

static bool classof(const SymbolBody *S) { return S->kind() == LazyKind; }
StringRef getName() override { return Name; }

// Returns an object file for this symbol, or a nullptr if the file
// was already returned.
ErrorOr<std::unique_ptr<InputFile>> getMember();

int compare(SymbolBody *Other) override;

private:
StringRef Name;
ArchiveFile *File;
const Archive::Symbol Sym;
};
Expand All @@ -226,23 +238,19 @@ class Lazy : public SymbolBody {
class Undefined : public SymbolBody {
public:
explicit Undefined(StringRef N, SymbolBody **S = nullptr)
: SymbolBody(UndefinedKind), Name(N), Alias(S) {}
: SymbolBody(UndefinedKind, N), Alias(S) {}

static bool classof(const SymbolBody *S) {
return S->kind() == UndefinedKind;
}
StringRef getName() override { return Name; }

// An undefined symbol can have a fallback symbol which gives an
// undefined symbol a second chance if it would remain undefined.
// If it remains undefined, it'll be replaced with whatever the
// Alias pointer points to.
SymbolBody *getWeakAlias() { return Alias ? *Alias : nullptr; }

int compare(SymbolBody *Other) override;

private:
StringRef Name;
SymbolBody **Alias;
};

Expand All @@ -256,23 +264,22 @@ class DefinedImportData : public Defined {
public:
DefinedImportData(StringRef D, StringRef N, StringRef E,
const coff_import_header *H)
: Defined(DefinedImportDataKind), Name(N), DLLName(D), ExternalName(E),
Hdr(H) {}
: Defined(DefinedImportDataKind, N), DLLName(D), ExternalName(E), Hdr(H) {
}

static bool classof(const SymbolBody *S) {
return S->kind() == DefinedImportDataKind;
}

StringRef getName() override { return Name; }
uint64_t getRVA() override { return Location->getRVA(); }
uint64_t getFileOff() override { return Location->getFileOff(); }
uint64_t getRVA() { return Location->getRVA(); }
uint64_t getFileOff() { return Location->getFileOff(); }

StringRef getDLLName() { return DLLName; }
StringRef getExternalName() { return ExternalName; }
void setLocation(Chunk *AddressTable) { Location = AddressTable; }
uint16_t getOrdinal() { return Hdr->OrdinalHint; }

private:
StringRef Name;
StringRef DLLName;
StringRef ExternalName;
const coff_import_header *Hdr;
Expand All @@ -287,19 +294,18 @@ class DefinedImportData : public Defined {
class DefinedImportThunk : public Defined {
public:
DefinedImportThunk(StringRef N, DefinedImportData *S)
: Defined(DefinedImportThunkKind), Name(N), Data(S) {}
: Defined(DefinedImportThunkKind, N), Data(S) {}

static bool classof(const SymbolBody *S) {
return S->kind() == DefinedImportThunkKind;
}

StringRef getName() override { return Name; }
uint64_t getRVA() override { return Data.getRVA(); }
uint64_t getFileOff() override { return Data.getFileOff(); }
uint64_t getRVA() { return Data.getRVA(); }
uint64_t getFileOff() { return Data.getFileOff(); }

Chunk *getChunk() { return &Data; }

private:
StringRef Name;
ImportThunkChunk Data;
};

Expand All @@ -311,39 +317,31 @@ class DefinedImportThunk : public Defined {
class DefinedLocalImport : public Defined {
public:
DefinedLocalImport(StringRef N, Defined *S)
: Defined(DefinedLocalImportKind), Name(N), Data(S) {}
: Defined(DefinedLocalImportKind, N), Data(S) {}

static bool classof(const SymbolBody *S) {
return S->kind() == DefinedLocalImportKind;
}

StringRef getName() override { return Name; }
uint64_t getRVA() override { return Data.getRVA(); }
uint64_t getFileOff() override { return Data.getFileOff(); }
uint64_t getRVA() { return Data.getRVA(); }
uint64_t getFileOff() { return Data.getFileOff(); }

Chunk *getChunk() { return &Data; }

private:
StringRef Name;
LocalImportChunk Data;
};

class DefinedBitcode : public Defined {
public:
DefinedBitcode(StringRef N, bool R)
: Defined(DefinedBitcodeKind), Name(N), Replaceable(R) {}
DefinedBitcode(StringRef N, bool IsReplaceable)
: Defined(DefinedBitcodeKind, N) {
this->IsReplaceable = IsReplaceable;
}

static bool classof(const SymbolBody *S) {
return S->kind() == DefinedBitcodeKind;
}

StringRef getName() override { return Name; }
uint64_t getRVA() override { llvm_unreachable("bitcode reached writer"); }
uint64_t getFileOff() override { llvm_unreachable("bitcode reached writer"); }
int compare(SymbolBody *Other) override;

private:
StringRef Name;
bool Replaceable;
};

} // namespace coff
Expand Down