[lld] r250519 - Add a ObjectFile<ELFT>::getSection helper and simplify. NFC.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 16 14:24:59 PDT 2015


This is a good change. Letting the function caling side to handle SHN_XINDEX
was error prone. Thank you for doing this!

On Fri, Oct 16, 2015 at 8:29 AM, Rafael Espindola via llvm-commits <
llvm-commits at lists.llvm.org> wrote:

> Author: rafael
> Date: Fri Oct 16 10:29:48 2015
> New Revision: 250519
>
> URL: http://llvm.org/viewvc/llvm-project?rev=250519&view=rev
> Log:
> Add a ObjectFile<ELFT>::getSection helper and simplify. NFC.
>
> Modified:
>     lld/trunk/ELF/InputFiles.cpp
>     lld/trunk/ELF/InputFiles.h
>     lld/trunk/ELF/OutputSections.cpp
>
> Modified: lld/trunk/ELF/InputFiles.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.cpp?rev=250519&r1=250518&r2=250519&view=diff
>
> ==============================================================================
> --- lld/trunk/ELF/InputFiles.cpp (original)
> +++ lld/trunk/ELF/InputFiles.cpp Fri Oct 16 10:29:48 2015
> @@ -173,36 +173,43 @@ template <class ELFT> void elf2::ObjectF
>  }
>
>  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);
>
> Modified: lld/trunk/ELF/InputFiles.h
> URL:
> http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.h?rev=250519&r1=250518&r2=250519&view=diff
>
> ==============================================================================
> --- lld/trunk/ELF/InputFiles.h (original)
> +++ lld/trunk/ELF/InputFiles.h Fri Oct 16 10:29:48 2015
> @@ -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;
>
> Modified: lld/trunk/ELF/OutputSections.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.cpp?rev=250519&r1=250518&r2=250519&view=diff
>
> ==============================================================================
> --- lld/trunk/ELF/OutputSections.cpp (original)
> +++ lld/trunk/ELF/OutputSections.cpp Fri Oct 16 10:29:48 2015
> @@ -434,17 +434,11 @@ lld::elf2::getLocalRelTarget(const Objec
>    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
>      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>::writeLoca
>        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;
>        }
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151016/77128db1/attachment-0001.html>


More information about the llvm-commits mailing list