[lld] r295278 - Add CopyRelSection instances to BSS in the regular way.

Rafael Avila de Espindola via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 16 07:55:09 PST 2017


Nice!

Rui Ueyama via llvm-commits <llvm-commits at lists.llvm.org> writes:

> Author: ruiu
> Date: Wed Feb 15 22:12:19 2017
> New Revision: 295278
>
> URL: http://llvm.org/viewvc/llvm-project?rev=295278&view=rev
> Log:
> Add CopyRelSection instances to BSS in the regular way.
>
> Previously, space in a BSS section for copy relocations are reserved
> in a special way. We directly manipulated size of the BSS section.
> r294577 changed the way of doing it. Now, we create an instance of
> CopyRelSection (which is a synthetic input section) for each copy
> relocation.
>
> This patch removes the remains of the old way and add CopyRelSections
> to BSS sections using `addSections` function, which is the usual
> way to add an input section to an output section.
>
> Modified:
>     lld/trunk/ELF/Relocations.cpp
>     lld/trunk/ELF/SyntheticSections.h
>     lld/trunk/ELF/Writer.cpp
>
> Modified: lld/trunk/ELF/Relocations.cpp
> URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Relocations.cpp?rev=295278&r1=295277&r2=295278&view=diff
> ==============================================================================
> --- lld/trunk/ELF/Relocations.cpp (original)
> +++ lld/trunk/ELF/Relocations.cpp Wed Feb 15 22:12:19 2017
> @@ -426,37 +426,28 @@ template <class ELFT> static void addCop
>    // See if this symbol is in a read-only segment. If so, preserve the symbol's
>    // memory protection by reserving space in the .bss.rel.ro section.
>    bool IsReadOnly = isReadOnly(SS);
> -  OutputSection<ELFT> *CopySec =
> -      IsReadOnly ? Out<ELFT>::BssRelRo : Out<ELFT>::Bss;
> +  OutputSection<ELFT> *OSec = IsReadOnly ? Out<ELFT>::BssRelRo : Out<ELFT>::Bss;
>  
> -  uintX_t Alignment = getAlignment(SS);
> -  uintX_t Off = alignTo(CopySec->Size, Alignment);
> -  CopySec->Size = Off + SymSize;
> -  CopySec->updateAlignment(Alignment);
> -  uintX_t Shndx = SS->Sym.st_shndx;
> -  uintX_t Value = SS->Sym.st_value;
> -
> -  // Create a SyntheticSection in CopySec to hold the .bss and the Copy Reloc
> -  auto *CopyISec = make<CopyRelSection<ELFT>>(IsReadOnly, Alignment, SymSize);
> -  CopyISec->OutSecOff = Off;
> -  CopyISec->OutSec = CopySec;
> -  CopySec->Sections.push_back(CopyISec);
> +  // Create a SyntheticSection in Out to hold the .bss and the Copy Reloc.
> +  auto *ISec =
> +      make<CopyRelSection<ELFT>>(IsReadOnly, getAlignment(SS), SymSize);
> +  OSec->addSection(ISec);
>  
>    // Look through the DSO's dynamic symbol table for aliases and create a
>    // dynamic symbol for each one. This causes the copy relocation to correctly
>    // interpose any aliases.
>    for (const Elf_Sym &S : SS->file()->getGlobalSymbols()) {
> -    if (S.st_shndx != Shndx || S.st_value != Value)
> +    if (S.st_shndx != SS->Sym.st_shndx || S.st_value != SS->Sym.st_value)
>        continue;
>      auto *Alias = dyn_cast_or_null<SharedSymbol<ELFT>>(
>          Symtab<ELFT>::X->find(check(S.getName(SS->file()->getStringTable()))));
>      if (!Alias)
>        continue;
> -    Alias->CopySection = CopyISec;
> +    Alias->CopySection = ISec;
>      Alias->NeedsCopyOrPltAddr = true;
>      Alias->symbol()->IsUsedInRegularObj = true;
>    }
> -  In<ELFT>::RelaDyn->addReloc({Target->CopyRel, CopyISec, 0, false, SS, 0});
> +  In<ELFT>::RelaDyn->addReloc({Target->CopyRel, ISec, 0, false, SS, 0});
>  }
>  
>  template <class ELFT>
>
> Modified: lld/trunk/ELF/SyntheticSections.h
> URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SyntheticSections.h?rev=295278&r1=295277&r2=295278&view=diff
> ==============================================================================
> --- lld/trunk/ELF/SyntheticSections.h (original)
> +++ lld/trunk/ELF/SyntheticSections.h Wed Feb 15 22:12:19 2017
> @@ -107,7 +107,8 @@ private:
>    uint8_t *HashBuf;
>  };
>  
> -// SHT_NOBITS section created for a copyReloc
> +// For each copy relocation, we create an instance of this class to
> +// reserve space in .bss or .bss.rel.ro.
>  template <class ELFT>
>  class CopyRelSection final : public SyntheticSection<ELFT> {
>    typedef typename ELFT::uint uintX_t;
>
> Modified: lld/trunk/ELF/Writer.cpp
> URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=295278&r1=295277&r2=295278&view=diff
> ==============================================================================
> --- lld/trunk/ELF/Writer.cpp (original)
> +++ lld/trunk/ELF/Writer.cpp Wed Feb 15 22:12:19 2017
> @@ -1184,12 +1184,16 @@ template <class ELFT> void Writer<ELFT>:
>  }
>  
>  template <class ELFT> void Writer<ELFT>::addPredefinedSections() {
> -  if (Out<ELFT>::Bss->Size > 0)
> -    OutputSections.push_back(Out<ELFT>::Bss);
> -  if (Out<ELFT>::BssRelRo->Size > 0)
> -    OutputSections.push_back(Out<ELFT>::BssRelRo);
> +  auto Add = [=](OutputSection<ELFT> *Sec) {
> +    if (!Sec->Sections.empty()) {
> +      Sec->assignOffsets();
> +      OutputSections.push_back(Sec);
> +    }
> +  };
> +  Add(Out<ELFT>::Bss);
> +  Add(Out<ELFT>::BssRelRo);
>  
> -  auto OS = dyn_cast_or_null<OutputSection<ELFT>>(findSection(".ARM.exidx"));
> +  auto *OS = dyn_cast_or_null<OutputSection<ELFT>>(findSection(".ARM.exidx"));
>    if (OS && !OS->Sections.empty() && !Config->Relocatable)
>      OS->addSection(make<ARMExidxSentinelSection<ELFT>>());
>  }
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits


More information about the llvm-commits mailing list