Add a ObjectFile<ELFT>::getSection helper and simplify. NFC.

llvm-svn: 250519
This commit is contained in:
Rafael Espindola 2015-10-16 15:29:48 +00:00
parent e71893d580
commit 4cda58168a
3 changed files with 23 additions and 34 deletions

View File

@ -172,6 +172,21 @@ template <class ELFT> void elf2::ObjectFile<ELFT>::initializeSymbols() {
this->SymbolBodies.push_back(createSymbolBody(this->StringTable, &Sym));
}
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) {
@ -179,30 +194,22 @@ SymbolBody *elf2::ObjectFile<ELFT>::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<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);

View File

@ -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;

View File

@ -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;
}