[lld] r256119 - [ELF][MIPS] MIPS .reginfo sections handling

Simon Atanasyan via llvm-commits llvm-commits at lists.llvm.org
Sun Dec 20 02:57:36 PST 2015


Author: atanasyan
Date: Sun Dec 20 04:57:34 2015
New Revision: 256119

URL: http://llvm.org/viewvc/llvm-project?rev=256119&view=rev
Log:
[ELF][MIPS] MIPS .reginfo sections handling

MIPS .reginfo section provides information on the registers used by
the code in the object file. Linker should collect this information and
write .reginfo section in the output file. This section contains a union
of used registers masks taken from input .reginfo sections and final
value of the `_gp` symbol.

For details see the "Register Information" section in Chapter 4 in the
following document:
ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf

The patch implements .reginfo sections handling with a couple missed
features: a) it does not put output .reginfo section into the separate
REGINFO segment; b) it does not merge `ri_cprmask` masks from input
section. These features will be implemented later.

Differential Revision: http://reviews.llvm.org/D15669

Added:
    lld/trunk/test/ELF/mips-reginfo.s
Modified:
    lld/trunk/ELF/InputFiles.cpp
    lld/trunk/ELF/InputSection.cpp
    lld/trunk/ELF/InputSection.h
    lld/trunk/ELF/OutputSections.cpp
    lld/trunk/ELF/OutputSections.h
    lld/trunk/ELF/Writer.cpp
    lld/trunk/test/ELF/basic-mips.s

Modified: lld/trunk/ELF/InputFiles.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.cpp?rev=256119&r1=256118&r2=256119&view=diff
==============================================================================
--- lld/trunk/ELF/InputFiles.cpp (original)
+++ lld/trunk/ELF/InputFiles.cpp Sun Dec 20 04:57:34 2015
@@ -232,6 +232,9 @@ void elf2::ObjectFile<ELFT>::initializeS
         Sections[I] = &InputSection<ELFT>::Discarded;
       else if (Name == ".eh_frame")
         Sections[I] = new (this->Alloc) EHInputSection<ELFT>(this, &Sec);
+      else if (Name == ".reginfo")
+        Sections[I] =
+            new (this->Alloc) MipsReginfoInputSection<ELFT>(this, &Sec);
       else if (shouldMerge<ELFT>(Sec))
         Sections[I] = new (this->Alloc) MergeInputSection<ELFT>(this, &Sec);
       else

Modified: lld/trunk/ELF/InputSection.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputSection.cpp?rev=256119&r1=256118&r2=256119&view=diff
==============================================================================
--- lld/trunk/ELF/InputSection.cpp (original)
+++ lld/trunk/ELF/InputSection.cpp Sun Dec 20 04:57:34 2015
@@ -51,6 +51,8 @@ InputSectionBase<ELFT>::getOffset(uintX_
     return cast<EHInputSection<ELFT>>(this)->getOffset(Offset);
   case Merge:
     return cast<MergeInputSection<ELFT>>(this)->getOffset(Offset);
+  case MipsReginfo:
+    return cast<MipsReginfoInputSection<ELFT>>(this)->getOffset(Offset);
   }
   llvm_unreachable("Invalid section kind");
 }
@@ -320,6 +322,24 @@ MergeInputSection<ELFT>::getOffset(uintX
   return Base + Addend;
 }
 
+template <class ELFT>
+MipsReginfoInputSection<ELFT>::MipsReginfoInputSection(ObjectFile<ELFT> *F,
+                                                       const Elf_Shdr *Header)
+    : InputSectionBase<ELFT>(F, Header, InputSectionBase<ELFT>::MipsReginfo) {}
+
+template <class ELFT>
+uint32_t MipsReginfoInputSection<ELFT>::getGeneralMask() const {
+  ArrayRef<uint8_t> D = this->getSectionData();
+  if (D.size() != sizeof(Elf_Mips_RegInfo))
+    error("Invalid size of .reginfo section");
+  return reinterpret_cast<const Elf_Mips_RegInfo *>(D.data())->ri_gprmask;
+}
+
+template <class ELFT>
+bool MipsReginfoInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
+  return S->SectionKind == InputSectionBase<ELFT>::MipsReginfo;
+}
+
 namespace lld {
 namespace elf2 {
 template class InputSectionBase<object::ELF32LE>;
@@ -341,5 +361,10 @@ template class MergeInputSection<object:
 template class MergeInputSection<object::ELF32BE>;
 template class MergeInputSection<object::ELF64LE>;
 template class MergeInputSection<object::ELF64BE>;
+
+template class MipsReginfoInputSection<object::ELF32LE>;
+template class MipsReginfoInputSection<object::ELF32BE>;
+template class MipsReginfoInputSection<object::ELF64LE>;
+template class MipsReginfoInputSection<object::ELF64BE>;
 }
 }

Modified: lld/trunk/ELF/InputSection.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputSection.h?rev=256119&r1=256118&r2=256119&view=diff
==============================================================================
--- lld/trunk/ELF/InputSection.h (original)
+++ lld/trunk/ELF/InputSection.h Sun Dec 20 04:57:34 2015
@@ -35,7 +35,7 @@ protected:
   ObjectFile<ELFT> *File;
 
 public:
-  enum Kind { Regular, EHFrame, Merge };
+  enum Kind { Regular, EHFrame, Merge, MipsReginfo };
   Kind SectionKind;
 
   InputSectionBase(ObjectFile<ELFT> *File, const Elf_Shdr *Header,
@@ -160,6 +160,25 @@ public:
 
   static bool classof(const InputSectionBase<ELFT> *S);
 };
+
+// MIPS .reginfo section provides information on the registers used by the code
+// in the object file. Linker should collect this information and write a single
+// .reginfo section in the output file. The output section contains a union of
+// used registers masks taken from input .reginfo sections and final value
+// of the `_gp` symbol.  For details: Chapter 4 / "Register Information" at
+// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
+template <class ELFT>
+class MipsReginfoInputSection : public InputSectionBase<ELFT> {
+  typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
+  typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
+
+public:
+  MipsReginfoInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
+
+  uint32_t getGeneralMask() const;
+
+  static bool classof(const InputSectionBase<ELFT> *S);
+};
 
 } // namespace elf2
 } // namespace lld

Modified: lld/trunk/ELF/OutputSections.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.cpp?rev=256119&r1=256118&r2=256119&view=diff
==============================================================================
--- lld/trunk/ELF/OutputSections.cpp (original)
+++ lld/trunk/ELF/OutputSections.cpp Sun Dec 20 04:57:34 2015
@@ -1389,6 +1389,27 @@ uint8_t SymbolTableSection<ELFT>::getSym
   return Body->isWeak() ? STB_WEAK : STB_GLOBAL;
 }
 
+template <class ELFT>
+MipsReginfoOutputSection<ELFT>::MipsReginfoOutputSection()
+    : OutputSectionBase<ELFT>(".reginfo", SHT_MIPS_REGINFO, SHF_ALLOC) {
+  this->Header.sh_addralign = 4;
+  this->Header.sh_entsize = sizeof(Elf_Mips_RegInfo);
+  this->Header.sh_size = sizeof(Elf_Mips_RegInfo);
+}
+
+template <class ELFT>
+void MipsReginfoOutputSection<ELFT>::writeTo(uint8_t *Buf) {
+  auto *R = reinterpret_cast<Elf_Mips_RegInfo *>(Buf);
+  R->ri_gp_value = getMipsGpAddr<ELFT>();
+  R->ri_gprmask = GeneralMask;
+}
+
+template <class ELFT>
+void MipsReginfoOutputSection<ELFT>::addSection(
+    MipsReginfoInputSection<ELFT> *S) {
+  GeneralMask |= S->getGeneralMask();
+}
+
 namespace lld {
 namespace elf2 {
 template class OutputSectionBase<ELF32LE>;
@@ -1446,6 +1467,11 @@ template class EHOutputSection<ELF32BE>;
 template class EHOutputSection<ELF64LE>;
 template class EHOutputSection<ELF64BE>;
 
+template class MipsReginfoOutputSection<ELF32LE>;
+template class MipsReginfoOutputSection<ELF32BE>;
+template class MipsReginfoOutputSection<ELF64LE>;
+template class MipsReginfoOutputSection<ELF64BE>;
+
 template class MergeOutputSection<ELF32LE>;
 template class MergeOutputSection<ELF32BE>;
 template class MergeOutputSection<ELF64LE>;

Modified: lld/trunk/ELF/OutputSections.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.h?rev=256119&r1=256118&r2=256119&view=diff
==============================================================================
--- lld/trunk/ELF/OutputSections.h (original)
+++ lld/trunk/ELF/OutputSections.h Sun Dec 20 04:57:34 2015
@@ -31,6 +31,7 @@ template <class ELFT> class EHInputSecti
 template <class ELFT> class InputSection;
 template <class ELFT> class InputSectionBase;
 template <class ELFT> class MergeInputSection;
+template <class ELFT> class MipsReginfoInputSection;
 template <class ELFT> class OutputSection;
 template <class ELFT> class ObjectFile;
 template <class ELFT> class DefinedRegular;
@@ -412,6 +413,20 @@ private:
   uint32_t DtFlags1 = 0;
 };
 
+template <class ELFT>
+class MipsReginfoOutputSection final : public OutputSectionBase<ELFT> {
+  typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
+
+public:
+  MipsReginfoOutputSection();
+  void writeTo(uint8_t *Buf) override;
+
+  void addSection(MipsReginfoInputSection<ELFT> *S);
+
+private:
+  uint32_t GeneralMask = 0;
+};
+
 // All output sections that are hadnled by the linker specially are
 // globally accessible. Writer initializes them, so don't use them
 // until Writer is initialized.

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=256119&r1=256118&r2=256119&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Sun Dec 20 04:57:34 2015
@@ -75,6 +75,7 @@ private:
   SpecificBumpPtrAllocator<OutputSection<ELFT>> SecAlloc;
   SpecificBumpPtrAllocator<MergeOutputSection<ELFT>> MSecAlloc;
   SpecificBumpPtrAllocator<EHOutputSection<ELFT>> EHSecAlloc;
+  SpecificBumpPtrAllocator<MipsReginfoOutputSection<ELFT>> MReginfoSecAlloc;
   BumpPtrAllocator Alloc;
   std::vector<OutputSectionBase<ELFT> *> OutputSections;
   unsigned getNumSections() const { return OutputSections.size() + 1; }
@@ -618,6 +619,10 @@ template <class ELFT> void Writer<ELFT>:
           Sec = new (MSecAlloc.Allocate())
               MergeOutputSection<ELFT>(Key.Name, Key.Type, Key.Flags);
           break;
+        case InputSectionBase<ELFT>::MipsReginfo:
+          Sec = new (MReginfoSecAlloc.Allocate())
+              MipsReginfoOutputSection<ELFT>();
+          break;
         }
         OutputSections.push_back(Sec);
         RegularSections.push_back(Sec);
@@ -635,6 +640,10 @@ template <class ELFT> void Writer<ELFT>:
         static_cast<MergeOutputSection<ELFT> *>(Sec)
             ->addSection(cast<MergeInputSection<ELFT>>(C));
         break;
+      case InputSectionBase<ELFT>::MipsReginfo:
+        static_cast<MipsReginfoOutputSection<ELFT> *>(Sec)
+            ->addSection(cast<MipsReginfoInputSection<ELFT>>(C));
+        break;
       }
     }
   }

Modified: lld/trunk/test/ELF/basic-mips.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/basic-mips.s?rev=256119&r1=256118&r2=256119&view=diff
==============================================================================
--- lld/trunk/test/ELF/basic-mips.s (original)
+++ lld/trunk/test/ELF/basic-mips.s Sun Dec 20 04:57:34 2015
@@ -68,7 +68,7 @@ __start:
 # CHECK-NEXT:     Link: 0
 # CHECK-NEXT:     Info: 0
 # CHECK-NEXT:     AddressAlignment: 4
-# CHECK-NEXT:     EntrySize: 0
+# CHECK-NEXT:     EntrySize: 24
 # CHECK-NEXT:   }
 # CHECK-NEXT:   Section {
 # CHECK-NEXT:     Index: 2

Added: lld/trunk/test/ELF/mips-reginfo.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/mips-reginfo.s?rev=256119&view=auto
==============================================================================
--- lld/trunk/test/ELF/mips-reginfo.s (added)
+++ lld/trunk/test/ELF/mips-reginfo.s Sun Dec 20 04:57:34 2015
@@ -0,0 +1,26 @@
+# Check MIPS .reginfo section generation.
+
+# RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux %s -o %t1.o
+# RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux \
+# RUN:         %S/Inputs/mips-dynamic.s -o %t2.o
+# RUN: ld.lld %t1.o %t2.o -shared -o %t.so
+# RUN: llvm-readobj -symbols -mips-reginfo %t.so | FileCheck %s
+
+# REQUIRES: mips
+
+  .text
+  .globl  __start
+__start:
+    lw   $t0,%call16(g1)($gp)
+
+# CHECK:      Name: _gp
+# CHECK-NEXT: Value: 0x[[GP:[0-9A-F]+]]
+
+# CHECK:      MIPS RegInfo {
+# CHECK-NEXT:   GP: 0x[[GP]]
+# CHECK-NEXT:   General Mask: 0x10000101
+# CHECK-NEXT:   Co-Proc Mask0: 0x0
+# CHECK-NEXT:   Co-Proc Mask1: 0x0
+# CHECK-NEXT:   Co-Proc Mask2: 0x0
+# CHECK-NEXT:   Co-Proc Mask3: 0x0
+# CHECK-NEXT: }




More information about the llvm-commits mailing list