Skip to content

Commit

Permalink
Make replaceSymbol a member function of Symbol.
Browse files Browse the repository at this point in the history
This is a mechanical rewrite of replaceSymbol(A, B) to A->replace(B).
I also added a comment to Symbol::replace().

Technically this change is not necessary, but this change makes code a
bit more concise.

Differential Revision: https://reviews.llvm.org/D62117

llvm-svn: 361123
  • Loading branch information
rui314 committed May 20, 2019
1 parent a6720e7 commit faf541e
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 46 deletions.
8 changes: 3 additions & 5 deletions lld/ELF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1338,9 +1338,8 @@ static void replaceCommonSymbols() {
Bss->File = S->File;
Bss->Live = !Config->GcSections;
InputSections.push_back(Bss);
replaceSymbol(S, Defined{S->File, S->getName(), S->Binding, S->StOther,
S->Type,
/*Value=*/0, S->Size, Bss});
S->replace(Defined{S->File, S->getName(), S->Binding, S->StOther, S->Type,
/*Value=*/0, S->Size, Bss});
}
}

Expand All @@ -1355,8 +1354,7 @@ static void demoteSharedSymbols() {
continue;

bool Used = S->Used;
replaceSymbol(
S, Undefined{nullptr, S->getName(), STB_WEAK, S->StOther, S->Type});
S->replace(Undefined{nullptr, S->getName(), STB_WEAK, S->StOther, S->Type});
S->Used = Used;
}
}
Expand Down
4 changes: 2 additions & 2 deletions lld/ELF/LTO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ void BitcodeCompiler::add(BitcodeFile &F) {
!(DR->Section == nullptr && (!Sym->File || Sym->File->isElf()));

if (R.Prevailing)
replaceSymbol(Sym, Undefined{nullptr, Sym->getName(), STB_GLOBAL,
STV_DEFAULT, Sym->Type});
Sym->replace(Undefined{nullptr, Sym->getName(), STB_GLOBAL, STV_DEFAULT,
Sym->Type});

// We tell LTO to not apply interprocedural optimization for wrapped
// (with --wrap) symbols because otherwise LTO would inline them while
Expand Down
4 changes: 2 additions & 2 deletions lld/ELF/LinkerScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ void LinkerScript::addSymbol(SymbolAssignment *Cmd) {

Symbol *Sym = Symtab->insert(Cmd->Name);
mergeSymbolProperties(Sym, New);
replaceSymbol(Sym, New);
Sym->replace(New);
Cmd->Sym = cast<Defined>(Sym);
}

Expand All @@ -205,7 +205,7 @@ static void declareSymbol(SymbolAssignment *Cmd) {
// We can't calculate final value right now.
Symbol *Sym = Symtab->insert(Cmd->Name);
mergeSymbolProperties(Sym, New);
replaceSymbol(Sym, New);
Sym->replace(New);

Cmd->Sym = cast<Defined>(Sym);
Cmd->Provide = false;
Expand Down
4 changes: 2 additions & 2 deletions lld/ELF/Relocations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,8 +530,8 @@ static void replaceWithDefined(Symbol &Sym, SectionBase *Sec, uint64_t Value,
uint64_t Size) {
Symbol Old = Sym;

replaceSymbol(&Sym, Defined{Sym.File, Sym.getName(), Sym.Binding, Sym.StOther,
Sym.Type, Value, Size, Sec});
Sym.replace(Defined{Sym.File, Sym.getName(), Sym.Binding, Sym.StOther,
Sym.Type, Value, Size, Sec});

Sym.PltIndex = Old.PltIndex;
Sym.GotIndex = Old.GotIndex;
Expand Down
12 changes: 6 additions & 6 deletions lld/ELF/SymbolTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ static void addUndefined(Symbol *Old, const Undefined &New) {
// An undefined symbol with non default visibility must be satisfied
// in the same DSO.
if (Old->isShared() && New.Visibility != STV_DEFAULT) {
replaceSymbol(Old, New);
Old->replace(New);
return;
}

Expand Down Expand Up @@ -284,7 +284,7 @@ static void addCommon(Symbol *Old, const CommonSymbol &New) {
return;

if (Cmp > 0) {
replaceSymbol(Old, New);
Old->replace(New);
return;
}

Expand Down Expand Up @@ -335,7 +335,7 @@ static void reportDuplicate(Symbol *Sym, InputFile *NewFile,
static void addDefined(Symbol *Old, const Defined &New) {
int Cmp = compare(Old, &New);
if (Cmp > 0)
replaceSymbol(Old, New);
Old->replace(New);
else if (Cmp == 0)
reportDuplicate(Old, New.File,
dyn_cast_or_null<InputSectionBase>(New.Section), New.Value);
Expand All @@ -346,7 +346,7 @@ static void addShared(Symbol *Old, const SharedSymbol &New) {
// An undefined symbol with non default visibility must be satisfied
// in the same DSO.
uint8_t Binding = Old->Binding;
replaceSymbol(Old, New);
Old->replace(New);
Old->Binding = Binding;
}
}
Expand All @@ -368,7 +368,7 @@ template <class LazyT> static void addLazy(Symbol *Old, const LazyT &New) {
// Symbols.h for the details.
if (Old->isWeak()) {
uint8_t Type = Old->Type;
replaceSymbol(Old, New);
Old->replace(New);
Old->Type = Type;
Old->Binding = STB_WEAK;
return;
Expand Down Expand Up @@ -578,7 +578,7 @@ void elf::resolveSymbol(Symbol *Old, const Symbol &New) {
mergeSymbolProperties(Old, New);

if (Old->isPlaceholder()) {
replaceSymbol(Old, New);
Old->replace(New);
return;
}

Expand Down
64 changes: 35 additions & 29 deletions lld/ELF/Symbols.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class Symbol {
// An index into the .branch_lt section on PPC64.
uint16_t PPC64BranchltIndex = -1;

// Symbol binding. This is not overwritten by replaceSymbol to track
// Symbol binding. This is not overwritten by replace() to track
// changes during resolution. In particular:
// - An undefined weak is still weak when it resolves to a shared library.
// - An undefined weak will not fetch archive members, but we have to
Expand Down Expand Up @@ -120,6 +120,8 @@ class Symbol {
// True if this symbol is specified by --trace-symbol option.
unsigned Traced : 1;

inline void replace(const Symbol &New);

bool includeInDynsym() const;
uint8_t computeBinding() const;
bool isWeak() const { return Binding == llvm::ELF::STB_WEAK; }
Expand Down Expand Up @@ -179,6 +181,8 @@ class Symbol {
return Config->Shared || Config->ExportDynamic;
}

inline size_t getSymbolSize() const;

protected:
Symbol(Kind K, InputFile *File, StringRefZ Name, uint8_t Binding,
uint8_t StOther, uint8_t Type)
Expand Down Expand Up @@ -427,59 +431,61 @@ static inline void assertSymbols() {

void printTraceSymbol(Symbol *Sym);

static size_t getSymbolSize(const Symbol &Sym) {
switch (Sym.kind()) {
case Symbol::CommonKind:
size_t Symbol::getSymbolSize() const {
switch (kind()) {
case CommonKind:
return sizeof(CommonSymbol);
case Symbol::DefinedKind:
case DefinedKind:
return sizeof(Defined);
case Symbol::LazyArchiveKind:
case LazyArchiveKind:
return sizeof(LazyArchive);
case Symbol::LazyObjectKind:
case LazyObjectKind:
return sizeof(LazyObject);
case Symbol::SharedKind:
case SharedKind:
return sizeof(SharedSymbol);
case Symbol::UndefinedKind:
case UndefinedKind:
return sizeof(Undefined);
case Symbol::PlaceholderKind:
case PlaceholderKind:
return sizeof(Symbol);
}
llvm_unreachable("unknown symbol kind");
}

inline void replaceSymbol(Symbol *Sym, const Symbol &New) {
// replace() replaces "this" object with a given symbol by memcpy'ing
// it over to "this". This function is called as a result of name
// resolution, e.g. to replace an undefind symbol with a defined symbol.
void Symbol::replace(const Symbol &New) {
using llvm::ELF::STT_TLS;

// Symbols representing thread-local variables must be referenced by
// TLS-aware relocations, and non-TLS symbols must be reference by
// non-TLS relocations, so there's a clear distinction between TLS
// and non-TLS symbols. It is an error if the same symbol is defined
// as a TLS symbol in one file and as a non-TLS symbol in other file.
if (Sym->SymbolKind != Symbol::PlaceholderKind && !Sym->isLazy() &&
!New.isLazy()) {
bool TlsMismatch = (Sym->Type == STT_TLS && New.Type != STT_TLS) ||
(Sym->Type != STT_TLS && New.Type == STT_TLS);
if (SymbolKind != PlaceholderKind && !isLazy() && !New.isLazy()) {
bool TlsMismatch = (Type == STT_TLS && New.Type != STT_TLS) ||
(Type != STT_TLS && New.Type == STT_TLS);
if (TlsMismatch)
error("TLS attribute mismatch: " + toString(*Sym) + "\n>>> defined in " +
toString(New.File) + "\n>>> defined in " + toString(Sym->File));
error("TLS attribute mismatch: " + toString(*this) + "\n>>> defined in " +
toString(New.File) + "\n>>> defined in " + toString(File));
}

Symbol Old = *Sym;
memcpy(Sym, &New, getSymbolSize(New));
Symbol Old = *this;
memcpy(this, &New, New.getSymbolSize());

Sym->VersionId = Old.VersionId;
Sym->Visibility = Old.Visibility;
Sym->IsUsedInRegularObj = Old.IsUsedInRegularObj;
Sym->ExportDynamic = Old.ExportDynamic;
Sym->CanInline = Old.CanInline;
Sym->Traced = Old.Traced;
Sym->IsPreemptible = Old.IsPreemptible;
Sym->ScriptDefined = Old.ScriptDefined;
VersionId = Old.VersionId;
Visibility = Old.Visibility;
IsUsedInRegularObj = Old.IsUsedInRegularObj;
ExportDynamic = Old.ExportDynamic;
CanInline = Old.CanInline;
Traced = Old.Traced;
IsPreemptible = Old.IsPreemptible;
ScriptDefined = Old.ScriptDefined;

// Print out a log message if --trace-symbol was specified.
// This is for debugging.
if (Sym->Traced)
printTraceSymbol(Sym);
if (Traced)
printTraceSymbol(this);
}

void maybeWarnUnorderableSymbol(const Symbol *Sym);
Expand Down

0 comments on commit faf541e

Please sign in to comment.