Skip to content

Commit

Permalink
[ELF] Symbol::replace: use the old nameData/nameSize. NFC
Browse files Browse the repository at this point in the history
Currently `this->getName() == newSym.getName()`.
By keeping the old nameData/nameSize, newSym's nameData/nameSize will be
ignored. The call sites can avoid calling getName().

printTraceSymbol needs to take the symbol name since `other`'s name is empty.
  • Loading branch information
MaskRay committed Feb 6, 2022
1 parent c5699c6 commit 977a1a5
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 44 deletions.
4 changes: 2 additions & 2 deletions lld/ELF/Arch/PPC64.cpp
Expand Up @@ -196,8 +196,8 @@ static bool addOptional(StringRef name, uint64_t value,
Symbol *sym = symtab->find(name);
if (!sym || sym->isDefined())
return false;
sym->resolve(Defined{/*file=*/nullptr, saver().save(name), STB_GLOBAL,
STV_HIDDEN, STT_FUNC, value,
sym->resolve(Defined{/*file=*/nullptr, StringRef(), STB_GLOBAL, STV_HIDDEN,
STT_FUNC, value,
/*size=*/0, /*section=*/nullptr});
defined.push_back(cast<Defined>(sym));
return true;
Expand Down
2 changes: 1 addition & 1 deletion lld/ELF/Driver.cpp
Expand Up @@ -1814,7 +1814,7 @@ static void replaceCommonSymbols() {
auto *bss = make<BssSection>("COMMON", s->size, s->alignment);
bss->file = s->file;
inputSections.push_back(bss);
s->replace(Defined{s->file, s->getName(), s->binding, s->stOther, s->type,
s->replace(Defined{s->file, StringRef(), s->binding, s->stOther, s->type,
/*value=*/0, s->size, bss});
}
}
Expand Down
38 changes: 17 additions & 21 deletions lld/ELF/InputFiles.cpp
Expand Up @@ -1101,14 +1101,13 @@ void ObjFile<ELFT>::initializeSymbols(const object::ELFFile<ELFT> &obj) {
uint64_t size = eSym.st_size;

Symbol *sym = symbols[i];
const StringRef name = sym->getName();
if (LLVM_UNLIKELY(eSym.st_shndx == SHN_COMMON)) {
if (value == 0 || value >= UINT32_MAX)
fatal(toString(this) + ": common symbol '" + name +
fatal(toString(this) + ": common symbol '" + sym->getName() +
"' has invalid alignment: " + Twine(value));
hasCommonSyms = true;
sym->resolve(
CommonSymbol{this, name, binding, stOther, type, value, size});
CommonSymbol{this, StringRef(), binding, stOther, type, value, size});
continue;
}

Expand All @@ -1118,7 +1117,7 @@ void ObjFile<ELFT>::initializeSymbols(const object::ELFFile<ELFT> &obj) {
// COMDAT member sections, and if a comdat group is discarded, some
// defined symbol in a .eh_frame becomes dangling symbols.
if (sec == &InputSection::discarded) {
Undefined und{this, name, binding, stOther, type, secIdx};
Undefined und{this, StringRef(), binding, stOther, type, secIdx};
// !ArchiveFile::parsed or !LazyObjFile::lazy means that the file
// containing this object has not finished processing, i.e. this symbol is
// a result of a lazy symbol extract. We should demote the lazy symbol to
Expand All @@ -1140,7 +1139,7 @@ void ObjFile<ELFT>::initializeSymbols(const object::ELFFile<ELFT> &obj) {
if (binding == STB_GLOBAL || binding == STB_WEAK ||
binding == STB_GNU_UNIQUE) {
sym->resolve(
Defined{this, name, binding, stOther, type, value, size, sec});
Defined{this, StringRef(), binding, stOther, type, value, size, sec});
continue;
}

Expand All @@ -1156,8 +1155,8 @@ void ObjFile<ELFT>::initializeSymbols(const object::ELFFile<ELFT> &obj) {
for (unsigned i : undefineds) {
const Elf_Sym &eSym = eSyms[i];
Symbol *sym = symbols[i];
sym->resolve(Undefined{this, sym->getName(), eSym.getBinding(),
eSym.st_other, eSym.getType()});
sym->resolve(Undefined{this, StringRef(), eSym.getBinding(), eSym.st_other,
eSym.getType()});
sym->referenced = true;
}
}
Expand Down Expand Up @@ -1670,28 +1669,23 @@ createBitcodeSymbol(Symbol *&sym, const std::vector<bool> &keptComdats,
uint8_t type = objSym.isTLS() ? STT_TLS : STT_NOTYPE;
uint8_t visibility = mapVisibility(objSym.getVisibility());

StringRef name;
if (sym) {
name = sym->getName();
} else {
name = saver().save(objSym.getName());
sym = symtab->insert(name);
}
if (!sym)
sym = symtab->insert(saver().save(objSym.getName()));

int c = objSym.getComdatIndex();
if (objSym.isUndefined() || (c != -1 && !keptComdats[c])) {
Undefined newSym(&f, name, binding, visibility, type);
Undefined newSym(&f, StringRef(), binding, visibility, type);
sym->resolve(newSym);
sym->referenced = true;
return;
}

if (objSym.isCommon()) {
sym->resolve(CommonSymbol{&f, name, binding, visibility, STT_OBJECT,
sym->resolve(CommonSymbol{&f, StringRef(), binding, visibility, STT_OBJECT,
objSym.getCommonAlignment(),
objSym.getCommonSize()});
} else {
Defined newSym(&f, name, binding, visibility, type, 0, 0, nullptr);
Defined newSym(&f, StringRef(), binding, visibility, type, 0, 0, nullptr);
if (objSym.canBeOmittedFromSymbolTable())
newSym.exportDynamic = false;
sym->resolve(newSym);
Expand Down Expand Up @@ -1721,9 +1715,11 @@ void BitcodeFile::parseLazy() {
SymbolTable &symtab = *elf::symtab;
symbols.resize(obj->symbols().size());
for (auto it : llvm::enumerate(obj->symbols()))
if (!it.value().isUndefined())
symbols[it.index()] = symtab.addSymbol(
LazyObject{*this, saver().save(it.value().getName())});
if (!it.value().isUndefined()) {
auto *sym = symtab.insert(saver().save(it.value().getName()));
sym->resolve(LazyObject{*this});
symbols[it.index()] = sym;
}
}

void BinaryFile::parse() {
Expand Down Expand Up @@ -1797,7 +1793,7 @@ template <class ELFT> void ObjFile<ELFT>::parseLazy() {
// exit from the loop early.
for (Symbol *sym : makeArrayRef(symbols).slice(firstGlobal))
if (sym) {
sym->resolve(LazyObject{*this, sym->getName()});
sym->resolve(LazyObject{*this});
if (!lazy)
return;
}
Expand Down
4 changes: 2 additions & 2 deletions lld/ELF/LTO.cpp
Expand Up @@ -267,8 +267,8 @@ void BitcodeCompiler::add(BitcodeFile &f) {
!(dr->section == nullptr && (!sym->file || sym->file->isElf()));

if (r.Prevailing)
sym->replace(Undefined{nullptr, sym->getName(), STB_GLOBAL, STV_DEFAULT,
sym->type});
sym->replace(
Undefined{nullptr, StringRef(), 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
2 changes: 1 addition & 1 deletion lld/ELF/Relocations.cpp
Expand Up @@ -299,7 +299,7 @@ static void replaceWithDefined(Symbol &sym, SectionBase &sec, uint64_t value,
uint64_t size) {
Symbol old = sym;

sym.replace(Defined{sym.file, sym.getName(), sym.binding, sym.stOther,
sym.replace(Defined{sym.file, StringRef(), sym.binding, sym.stOther,
sym.type, value, size, &sec});

sym.auxIdx = old.auxIdx;
Expand Down
18 changes: 9 additions & 9 deletions lld/ELF/Symbols.cpp
Expand Up @@ -290,20 +290,20 @@ bool Symbol::includeInDynsym() const {
}

// Print out a log message for --trace-symbol.
void elf::printTraceSymbol(const Symbol *sym) {
void elf::printTraceSymbol(const Symbol &sym, StringRef name) {
std::string s;
if (sym->isUndefined())
if (sym.isUndefined())
s = ": reference to ";
else if (sym->isLazy())
else if (sym.isLazy())
s = ": lazy definition of ";
else if (sym->isShared())
else if (sym.isShared())
s = ": shared definition of ";
else if (sym->isCommon())
else if (sym.isCommon())
s = ": common definition of ";
else
s = ": definition of ";

message(toString(sym->file) + s + sym->getName());
message(toString(sym.file) + s + name);
}

static void recordWhyExtract(const InputFile *reference,
Expand Down Expand Up @@ -458,7 +458,7 @@ void Symbol::resolveUndefined(const Undefined &other) {
}

if (traced)
printTraceSymbol(&other);
printTraceSymbol(other, getName());

if (isLazy()) {
// An undefined weak will not extract archive members. See comment on Lazy
Expand Down Expand Up @@ -702,7 +702,7 @@ template <class LazyT> void Symbol::resolveLazy(const LazyT &other) {
return;
}
} else if (auto *loSym = dyn_cast<LazyObject>(&other)) {
if (loSym->file->shouldExtractForCommon(loSym->getName())) {
if (loSym->file->shouldExtractForCommon(getName())) {
replaceCommon(*this, other);
return;
}
Expand Down Expand Up @@ -746,5 +746,5 @@ void Symbol::resolveShared(const SharedSymbol &other) {
replace(other);
binding = bind;
} else if (traced)
printTraceSymbol(&other);
printTraceSymbol(other, getName());
}
10 changes: 6 additions & 4 deletions lld/ELF/Symbols.h
Expand Up @@ -452,8 +452,8 @@ class LazyArchive : public Symbol {
// --start-lib and --end-lib options.
class LazyObject : public Symbol {
public:
LazyObject(InputFile &file, StringRef name)
: Symbol(LazyObjectKind, &file, name, llvm::ELF::STB_GLOBAL,
LazyObject(InputFile &file)
: Symbol(LazyObjectKind, &file, {}, llvm::ELF::STB_GLOBAL,
llvm::ELF::STV_DEFAULT, llvm::ELF::STT_NOTYPE) {
isUsedInRegularObj = false;
}
Expand Down Expand Up @@ -534,7 +534,7 @@ static inline void assertSymbols() {
AssertSymbol<LazyObject>();
}

void printTraceSymbol(const Symbol *sym);
void printTraceSymbol(const Symbol &sym, StringRef name);

size_t Symbol::getSymbolSize() const {
switch (kind()) {
Expand Down Expand Up @@ -580,6 +580,8 @@ void Symbol::replace(const Symbol &newSym) {

// old may be a placeholder. The referenced fields must be initialized in
// SymbolTable::insert.
nameData = old.nameData;
nameSize = old.nameSize;
partition = old.partition;
visibility = old.visibility;
isPreemptible = old.isPreemptible;
Expand All @@ -595,7 +597,7 @@ void Symbol::replace(const Symbol &newSym) {
// Print out a log message if --trace-symbol was specified.
// This is for debugging.
if (traced)
printTraceSymbol(this);
printTraceSymbol(*this, getName());
}

template <typename... T> Defined *makeDefined(T &&...args) {
Expand Down
7 changes: 3 additions & 4 deletions lld/ELF/Writer.cpp
Expand Up @@ -168,8 +168,7 @@ static Defined *addOptionalRegular(StringRef name, SectionBase *sec,
if (!s || s->isDefined())
return nullptr;

s->resolve(Defined{/*file=*/nullptr, name, STB_GLOBAL, stOther, STT_NOTYPE,
val,
s->resolve(Defined{nullptr, StringRef(), STB_GLOBAL, stOther, STT_NOTYPE, val,
/*size=*/0, sec});
return cast<Defined>(s);
}
Expand Down Expand Up @@ -232,7 +231,7 @@ void elf::addReservedSymbols() {
if (config->emachine == EM_PPC64)
gotOff = 0x8000;

s->resolve(Defined{/*file=*/nullptr, gotSymName, STB_GLOBAL, STV_HIDDEN,
s->resolve(Defined{/*file=*/nullptr, StringRef(), STB_GLOBAL, STV_HIDDEN,
STT_NOTYPE, gotOff, /*size=*/0, Out::elfHeader});
ElfSym::globalOffsetTable = cast<Defined>(s);
}
Expand Down Expand Up @@ -1862,7 +1861,7 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
// define _TLS_MODULE_BASE_ relative to the first TLS section.
Symbol *s = symtab->find("_TLS_MODULE_BASE_");
if (s && s->isUndefined()) {
s->resolve(Defined{/*file=*/nullptr, s->getName(), STB_GLOBAL, STV_HIDDEN,
s->resolve(Defined{/*file=*/nullptr, StringRef(), STB_GLOBAL, STV_HIDDEN,
STT_TLS, /*value=*/0, 0,
/*section=*/nullptr});
ElfSym::tlsModuleBase = cast<Defined>(s);
Expand Down

0 comments on commit 977a1a5

Please sign in to comment.