diff options
author | Rafael Espindola <rafael.espindola@gmail.com> | 2015-10-16 15:29:48 +0000 |
---|---|---|
committer | Rafael Espindola <rafael.espindola@gmail.com> | 2015-10-16 15:29:48 +0000 |
commit | 4cda58168a97778ebd1cf57d3bc66804b45c1966 (patch) | |
tree | 149dc2579d8d87152794a30b3767523843e546b9 | |
parent | e71893d58070b08f4dcc102c1c3af52f31fe4012 (diff) | |
download | bcm5719-llvm-4cda58168a97778ebd1cf57d3bc66804b45c1966.tar.gz bcm5719-llvm-4cda58168a97778ebd1cf57d3bc66804b45c1966.zip |
Add a ObjectFile<ELFT>::getSection helper and simplify. NFC.
llvm-svn: 250519
-rw-r--r-- | lld/ELF/InputFiles.cpp | 27 | ||||
-rw-r--r-- | lld/ELF/InputFiles.h | 1 | ||||
-rw-r--r-- | 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 b154ac63c2a..661ea64c176 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -173,36 +173,43 @@ template <class ELFT> void elf2::ObjectFile<ELFT>::initializeSymbols() { } template <class ELFT> +InputSection<ELFT> * +elf2::ObjectFile<ELFT>::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 <class ELFT> SymbolBody *elf2::ObjectFile<ELFT>::createSymbolBody(StringRef StringTable, const Elf_Sym *Sym) { ErrorOr<StringRef> NameOrErr = Sym->getName(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<ELFT>(Name, *Sym); case SHN_UNDEF: return new (this->Alloc) Undefined<ELFT>(Name, *Sym); case SHN_COMMON: return new (this->Alloc) DefinedCommon<ELFT>(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<ELFT> *Sec = Sections[SecIndex]; + InputSection<ELFT> *Sec = getSection(*Sym); if (Sec == &InputSection<ELFT>::Discarded) return new (this->Alloc) Undefined<ELFT>(Name, *Sym); return new (this->Alloc) DefinedRegular<ELFT>(Name, *Sym, *Sec); diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h index 00ab848eddf..00b44201d9d 100644 --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -111,6 +111,7 @@ public: void parse(llvm::DenseSet<StringRef> &Comdats); ArrayRef<InputSection<ELFT> *> getSections() const { return Sections; } + InputSection<ELFT> *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 6d27e421d1a..7f7b970a72f 100644 --- a/lld/ELF/OutputSections.cpp +++ b/lld/ELF/OutputSections.cpp @@ -434,17 +434,11 @@ lld::elf2::getLocalRelTarget(const ObjectFile<ELFT> &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<InputSection<ELFT> *> Sections = File.getSections(); - InputSection<ELFT> *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<ELFT> *Section = File.getSection(*Sym); if (Section == &InputSection<ELFT>::Discarded) return 0; @@ -535,16 +529,8 @@ bool lld::elf2::shouldKeepInSymtab(const ObjectFile<ELFT> &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<InputSection<ELFT> *> Sections = File.getSections(); - const InputSection<ELFT> *Section = Sections[SecIndex]; - if (Section == &InputSection<ELFT>::Discarded) - return false; - } + if (File.getSection(Sym) == &InputSection<ELFT>::Discarded) + return false; if (Config->DiscardNone) return true; @@ -611,16 +597,11 @@ void SymbolTableSection<ELFT>::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<InputSection<ELFT> *> Sections = File->getSections(); - const InputSection<ELFT> *Sec = Sections[SecIndex]; + const InputSection<ELFT> *Sec = File->getSection(Sym); ESym->st_shndx = Sec->OutSec->SectionIndex; VA += Sec->OutSec->getVA() + Sec->OutSecOff; } |