[lld] r244911 - Take name, type and flags in consideration when concatenating sections.

Sean Silva via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 13 16:19:35 PDT 2015


On Thu, Aug 13, 2015 at 10:04 AM, Rafael Espindola via llvm-commits <
llvm-commits at lists.llvm.org> wrote:

> Author: rafael
> Date: Thu Aug 13 12:04:50 2015
> New Revision: 244911
>
> URL: http://llvm.org/viewvc/llvm-project?rev=244911&view=rev
> Log:
> Take name, type and flags in consideration when concatenating sections.
>
> This is mandated by the ELF spec.
>
> Modified:
>     lld/trunk/ELF/Writer.cpp
>     lld/trunk/test/elf2/string-table.s
>
> Modified: lld/trunk/ELF/Writer.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=244911&r1=244910&r2=244911&view=diff
>
> ==============================================================================
> --- lld/trunk/ELF/Writer.cpp (original)
> +++ lld/trunk/ELF/Writer.cpp Thu Aug 13 12:04:50 2015
> @@ -63,6 +63,7 @@ private:
>  template <class ELFT> class Writer {
>  public:
>    typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
> +  typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
>    Writer(SymbolTable *T) : Symtab(T) {}
>    void run();
>
> @@ -140,13 +141,44 @@ template <class ELFT> void OutputSection
>    *SHdr = Header;
>  }
>
> +namespace {
> +template <bool Is64Bits> struct SectionKey {
> +  typedef typename std::conditional<Is64Bits, uint64_t, uint32_t>::type
> uintX_t;
> +  StringRef Name;
> +  uint32_t sh_type;
> +  uintX_t sh_flags;
> +};
> +}
> +namespace llvm {
> +template <bool Is64Bits> struct DenseMapInfo<SectionKey<Is64Bits>> {
> +  static SectionKey<Is64Bits> getEmptyKey() {
> +    return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getEmptyKey(),
> 0, 0};
> +  }
> +  static SectionKey<Is64Bits> getTombstoneKey() {
> +    return
> SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getTombstoneKey(), 0,
> +                                0};
> +  }
> +  static unsigned getHashValue(const SectionKey<Is64Bits> &Val) {
> +    return hash_combine(Val.Name, Val.sh_type, Val.sh_flags);
> +  }
> +  static bool isEqual(const SectionKey<Is64Bits> &LHS,
> +                      const SectionKey<Is64Bits> &RHS) {
> +    return DenseMapInfo<StringRef>::isEqual(LHS.Name, RHS.Name) &&
> +           LHS.sh_type == RHS.sh_type && LHS.sh_flags == RHS.sh_flags;
> +  }
> +};
> +}
> +
>  // Create output section objects and add them to OutputSections.
>  template <class ELFT> void Writer<ELFT>::createSections() {
> -  SmallDenseMap<StringRef, OutputSection<ELFT> *> Map;
> +  SmallDenseMap<SectionKey<ELFT::Is64Bits>, OutputSection<ELFT> *> Map;
>    for (std::unique_ptr<ObjectFileBase> &FileB : Symtab->ObjectFiles) {
>      auto &File = cast<ObjectFile<ELFT>>(*FileB);
>      for (SectionChunk<ELFT> *C : File.getChunks()) {
> -      OutputSection<ELFT> *&Sec = Map[C->getSectionName()];
> +      const Elf_Shdr *H = C->getSectionHdr();
> +      SectionKey<ELFT::Is64Bits> Key{C->getSectionName(), H->sh_type,
> +                                     H->sh_flags};
> +      OutputSection<ELFT> *&Sec = Map[Key];
>        if (!Sec) {
>          Sec = new (CAlloc.Allocate())
> OutputSection<ELFT>(C->getSectionName());
>          OutputSections.push_back(Sec);
>
> Modified: lld/trunk/test/elf2/string-table.s
> URL:
> http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/string-table.s?rev=244911&r1=244910&r2=244911&view=diff
>
> ==============================================================================
> --- lld/trunk/test/elf2/string-table.s (original)
> +++ lld/trunk/test/elf2/string-table.s Thu Aug 13 12:04:50 2015
> @@ -6,28 +6,47 @@
>  .global _start
>  _start:
>
> -.section foobar
> +.section        foobar,"", at progbits,unique,1
> +.section        foobar,"T", at progbits,unique,2
> +.section        foobar,"", at nobits,unique,3
> +.section        foobar,"", at nobits,unique,4
>

Is there a way to write this that is less arcane? Where is `unique`
documented? What is the number at the end?

-- Sean Silva



> +
>  .section bar, "a"
>
>  // Both sections are in the output and that the alloc section is first:
>  // CHECK:      Name: bar
> -// CHECK-NEXT:   Type: SHT_PROGBITS
> -// CHECK-NEXT:   Flags [
> -// CHECK-NEXT:     SHF_ALLOC
> -// CHECK-NEXT:   ]
> -// CHECK-NEXT:   Address: 0x1000
> +// CHECK-NEXT: Type: SHT_PROGBITS
> +// CHECK-NEXT: Flags [
> +// CHECK-NEXT:  SHF_ALLOC
> +// CHECK-NEXT: ]
> +// CHECK-NEXT: Address: 0x1000
>
>  // CHECK:      Name: foobar
> -// CHECK-NEXT:   Type: SHT_PROGBITS
> -// CHECK-NEXT:   Flags [
> -// CHECK-NEXT:   ]
> -// CHECK-NEXT:   Address: 0x0
> +// CHECK-NEXT: Type: SHT_PROGBITS
> +// CHECK-NEXT: Flags [
> +// CHECK-NEXT: ]
> +// CHECK-NEXT: Address: 0x0
> +
> +// CHECK:      Name: foobar
> +// CHECK-NEXT: Type: SHT_PROGBITS
> +// CHECK-NEXT: Flags [
> +// CHECK-NEXT:   SHF_TLS
> +// CHECK-NEXT: ]
> +// CHECK-NEXT: Address: 0x0
> +
> +// CHECK:      Name: foobar
> +// CHECK-NEXT: Type: SHT_NOBITS
> +// CHECK-NEXT: Flags [
> +// CHECK-NEXT: ]
> +// CHECK-NEXT: Address: 0x0
> +
> +// CHECK-NOT:  Name: foobar
>
>  // Test that the sting "bar" is merged into "foobar"
>
>  // CHECK:      Section {
> -// CHECK:        Index: 6
> -// CHECK-NEXT:   Name: .strtab
> +// CHECK:        Index:
> +// CHECK:        Name: .strtab
>  // CHECK-NEXT:   Type: SHT_STRTAB (0x3)
>  // CHECK-NEXT:   Flags [ (0x0)
>  // CHECK-NEXT:   ]
>
>
> _______________________________________________
> 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/20150813/5b68de69/attachment.html>


More information about the llvm-commits mailing list