From 4cda58168a97778ebd1cf57d3bc66804b45c1966 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Fri, 16 Oct 2015 15:29:48 +0000 Subject: [PATCH] Add a ObjectFile::getSection helper and simplify. NFC. llvm-svn: 250519 --- lld/ELF/InputFiles.cpp | 27 +++++++++++++++++---------- lld/ELF/InputFiles.h | 1 + lld/ELF/OutputSections.cpp | 29 +++++------------------------ 3 files changed, 23 insertions(+), 34 deletions(-) diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index b154ac63c2ad..661ea64c1768 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -172,6 +172,21 @@ template void elf2::ObjectFile::initializeSymbols() { this->SymbolBodies.push_back(createSymbolBody(this->StringTable, &Sym)); } +template +InputSection * +elf2::ObjectFile::getSection(const Elf_Sym &Sym) const { + uint32_t Index = Sym.st_shndx; + if (Index == ELF::SHN_XINDEX) + Index = this->ELFObj.getExtendedSymbolTableIndex(&Sym, this->Symtab, + SymtabSHNDX); + else if (Index == ELF::SHN_UNDEF || Index >= ELF::SHN_LORESERVE) + return nullptr; + + if (Index >= Sections.size() || !Index || !Sections[Index]) + error("Invalid section index"); + return Sections[Index]; +} + template SymbolBody *elf2::ObjectFile::createSymbolBody(StringRef StringTable, const Elf_Sym *Sym) { @@ -179,30 +194,22 @@ SymbolBody *elf2::ObjectFile::createSymbolBody(StringRef StringTable, error(NameOrErr.getError()); StringRef Name = *NameOrErr; - uint32_t SecIndex = Sym->st_shndx; - switch (SecIndex) { + switch (Sym->st_shndx) { case SHN_ABS: return new (this->Alloc) DefinedAbsolute(Name, *Sym); case SHN_UNDEF: return new (this->Alloc) Undefined(Name, *Sym); case SHN_COMMON: return new (this->Alloc) DefinedCommon(Name, *Sym); - case SHN_XINDEX: - SecIndex = this->ELFObj.getExtendedSymbolTableIndex(Sym, this->Symtab, - SymtabSHNDX); - break; } - if (SecIndex >= Sections.size() || !SecIndex || !Sections[SecIndex]) - error("Invalid section index"); - switch (Sym->getBinding()) { default: error("unexpected binding"); case STB_GLOBAL: case STB_WEAK: case STB_GNU_UNIQUE: { - InputSection *Sec = Sections[SecIndex]; + InputSection *Sec = getSection(*Sym); if (Sec == &InputSection::Discarded) return new (this->Alloc) Undefined(Name, *Sym); return new (this->Alloc) DefinedRegular(Name, *Sym, *Sec); diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h index 00ab848eddf5..00b44201d9d5 100644 --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -111,6 +111,7 @@ public: void parse(llvm::DenseSet &Comdats); ArrayRef *> getSections() const { return Sections; } + InputSection *getSection(const Elf_Sym &Sym) const; SymbolBody *getSymbolBody(uint32_t SymbolIndex) const { uint32_t FirstNonLocal = this->Symtab->sh_info; diff --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp index 6d27e421d1a2..7f7b970a72f8 100644 --- a/lld/ELF/OutputSections.cpp +++ b/lld/ELF/OutputSections.cpp @@ -434,17 +434,11 @@ lld::elf2::getLocalRelTarget(const ObjectFile &File, if (!Sym) return 0; - uint32_t SecIndex = Sym->st_shndx; - if (SecIndex == SHN_XINDEX) - SecIndex = File.getObj().getExtendedSymbolTableIndex( - Sym, File.getSymbolTable(), File.getSymbolTableShndx()); - ArrayRef *> Sections = File.getSections(); - InputSection *Section = Sections[SecIndex]; - // According to the ELF spec reference to a local symbol from outside // the group are not allowed. Unfortunately .eh_frame breaks that rule // and must be treated specially. For now we just replace the symbol with // 0. + InputSection *Section = File.getSection(*Sym); if (Section == &InputSection::Discarded) return 0; @@ -535,16 +529,8 @@ bool lld::elf2::shouldKeepInSymtab(const ObjectFile &File, return false; // If sym references a section in a discarded group, don't keep it. - uint32_t SecIndex = Sym.st_shndx; - if (SecIndex != SHN_ABS) { - if (SecIndex == SHN_XINDEX) - SecIndex = File.getObj().getExtendedSymbolTableIndex( - &Sym, File.getSymbolTable(), File.getSymbolTableShndx()); - ArrayRef *> Sections = File.getSections(); - const InputSection *Section = Sections[SecIndex]; - if (Section == &InputSection::Discarded) - return false; - } + if (File.getSection(Sym) == &InputSection::Discarded) + return false; if (Config->DiscardNone) return true; @@ -611,16 +597,11 @@ void SymbolTableSection::writeLocalSymbols(uint8_t *&Buf) { ESym->st_name = StrTabSec.getFileOff(SymName); ESym->st_size = Sym.st_size; ESym->setBindingAndType(Sym.getBinding(), Sym.getType()); - uint32_t SecIndex = Sym.st_shndx; uintX_t VA = Sym.st_value; - if (SecIndex == SHN_ABS) { + if (Sym.st_shndx == SHN_ABS) { ESym->st_shndx = SHN_ABS; } else { - if (SecIndex == SHN_XINDEX) - SecIndex = File->getObj().getExtendedSymbolTableIndex( - &Sym, File->getSymbolTable(), File->getSymbolTableShndx()); - ArrayRef *> Sections = File->getSections(); - const InputSection *Sec = Sections[SecIndex]; + const InputSection *Sec = File->getSection(Sym); ESym->st_shndx = Sec->OutSec->SectionIndex; VA += Sec->OutSec->getVA() + Sec->OutSecOff; }