Write section and section table entries in the same order.
authorRafael Espindola <rafael.espindola@gmail.com>
Wed, 15 Apr 2015 13:07:47 +0000 (13:07 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Wed, 15 Apr 2015 13:07:47 +0000 (13:07 +0000)
We had two different orders, which has no value.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@235004 91177308-0d34-0410-b5e6-96231b3b80d8

lib/MC/ELFObjectWriter.cpp
test/MC/ARM/eh-link.s
test/MC/ELF/debug-line.s

index 583f2fd15a5b90b10d88e891d9cbf649ceb464ac..8cb01c43edd61e82c991a72b29167fc06927f092 100644 (file)
@@ -257,14 +257,12 @@ class ELFObjectWriter : public MCObjectWriter {
     void ExecutePostLayoutBinding(MCAssembler &Asm,
                                   const MCAsmLayout &Layout) override;
 
     void ExecutePostLayoutBinding(MCAssembler &Asm,
                                   const MCAsmLayout &Layout) override;
 
-    void writeSectionHeader(MCAssembler &Asm, const GroupMapTy &GroupMap,
+    void writeSectionHeader(ArrayRef<const MCSectionELF *> Sections,
+                            MCAssembler &Asm, const GroupMapTy &GroupMap,
                             const MCAsmLayout &Layout,
                             const SectionIndexMapTy &SectionIndexMap,
                             const SectionOffsetMapTy &SectionOffsetMap);
 
                             const MCAsmLayout &Layout,
                             const SectionIndexMapTy &SectionIndexMap,
                             const SectionOffsetMapTy &SectionOffsetMap);
 
-    void ComputeSectionOrder(MCAssembler &Asm,
-                             std::vector<const MCSectionELF*> &Sections);
-
     void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
                           uint64_t Address, uint64_t Offset,
                           uint64_t Size, uint32_t Link, uint32_t Info,
     void WriteSecHdrEntry(uint32_t Name, uint32_t Type, uint64_t Flags,
                           uint64_t Address, uint64_t Offset,
                           uint64_t Size, uint32_t Link, uint32_t Info,
@@ -1550,28 +1548,20 @@ void ELFObjectWriter::writeDataSectionData(MCAssembler &Asm,
 }
 
 void ELFObjectWriter::writeSectionHeader(
 }
 
 void ELFObjectWriter::writeSectionHeader(
-    MCAssembler &Asm, const GroupMapTy &GroupMap, const MCAsmLayout &Layout,
+    ArrayRef<const MCSectionELF *> Sections, MCAssembler &Asm,
+    const GroupMapTy &GroupMap, const MCAsmLayout &Layout,
     const SectionIndexMapTy &SectionIndexMap,
     const SectionOffsetMapTy &SectionOffsetMap) {
     const SectionIndexMapTy &SectionIndexMap,
     const SectionOffsetMapTy &SectionOffsetMap) {
-  const unsigned NumSections = Asm.size() + 1;
-
-  std::vector<const MCSectionELF*> Sections;
-  Sections.resize(NumSections - 1);
-
-  for (SectionIndexMapTy::const_iterator i=
-         SectionIndexMap.begin(), e = SectionIndexMap.end(); i != e; ++i) {
-    const std::pair<const MCSectionELF*, uint32_t> &p = *i;
-    Sections[p.second - 1] = p.first;
-  }
+  const unsigned NumSections = Asm.size();
 
   // Null section first.
   uint64_t FirstSectionSize =
 
   // Null section first.
   uint64_t FirstSectionSize =
-    NumSections >= ELF::SHN_LORESERVE ? NumSections : 0;
+      (NumSections + 1) >= ELF::SHN_LORESERVE ? NumSections + 1 : 0;
   uint32_t FirstSectionLink =
     ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
   WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
 
   uint32_t FirstSectionLink =
     ShstrtabIndex >= ELF::SHN_LORESERVE ? ShstrtabIndex : 0;
   WriteSecHdrEntry(0, 0, 0, 0, 0, FirstSectionSize, FirstSectionLink, 0, 0, 0);
 
-  for (unsigned i = 0; i < NumSections - 1; ++i) {
+  for (unsigned i = 0; i < NumSections; ++i) {
     const MCSectionELF &Section = *Sections[i];
     const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
     uint32_t GroupSymbolIndex;
     const MCSectionELF &Section = *Sections[i];
     const MCSectionData &SD = Asm.getOrCreateSectionData(Section);
     uint32_t GroupSymbolIndex;
@@ -1589,36 +1579,6 @@ void ELFObjectWriter::writeSectionHeader(
   }
 }
 
   }
 }
 
-void ELFObjectWriter::ComputeSectionOrder(MCAssembler &Asm,
-                                  std::vector<const MCSectionELF*> &Sections) {
-  for (MCAssembler::iterator it = Asm.begin(),
-         ie = Asm.end(); it != ie; ++it) {
-    const MCSectionELF &Section =
-      static_cast<const MCSectionELF &>(it->getSection());
-    if (Section.getType() == ELF::SHT_GROUP)
-      Sections.push_back(&Section);
-  }
-
-  for (MCAssembler::iterator it = Asm.begin(),
-         ie = Asm.end(); it != ie; ++it) {
-    const MCSectionELF &Section =
-      static_cast<const MCSectionELF &>(it->getSection());
-    if (Section.getType() != ELF::SHT_GROUP &&
-        Section.getType() != ELF::SHT_REL &&
-        Section.getType() != ELF::SHT_RELA)
-      Sections.push_back(&Section);
-  }
-
-  for (MCAssembler::iterator it = Asm.begin(),
-         ie = Asm.end(); it != ie; ++it) {
-    const MCSectionELF &Section =
-      static_cast<const MCSectionELF &>(it->getSection());
-    if (Section.getType() == ELF::SHT_REL ||
-        Section.getType() == ELF::SHT_RELA)
-      Sections.push_back(&Section);
-  }
-}
-
 void ELFObjectWriter::WriteObject(MCAssembler &Asm,
                                   const MCAsmLayout &Layout) {
   GroupMapTy GroupMap;
 void ELFObjectWriter::WriteObject(MCAssembler &Asm,
                                   const MCAsmLayout &Layout) {
   GroupMapTy GroupMap;
@@ -1638,9 +1598,13 @@ void ELFObjectWriter::WriteObject(MCAssembler &Asm,
                          const_cast<MCAsmLayout&>(Layout),
                          SectionIndexMap);
 
                          const_cast<MCAsmLayout&>(Layout),
                          SectionIndexMap);
 
+  unsigned NumSections = Asm.size();
   std::vector<const MCSectionELF*> Sections;
   std::vector<const MCSectionELF*> Sections;
-  ComputeSectionOrder(Asm, Sections);
-  unsigned NumSections = Sections.size();
+  Sections.resize(NumSections);
+
+  for (auto &Pair : SectionIndexMap)
+    Sections[Pair.second - 1] = Pair.first;
+
   SectionOffsetMapTy SectionOffsetMap;
 
   // Write out the ELF header ...
   SectionOffsetMapTy SectionOffsetMap;
 
   // Write out the ELF header ...
@@ -1666,7 +1630,8 @@ void ELFObjectWriter::WriteObject(MCAssembler &Asm,
   const unsigned SectionHeaderOffset = OS.tell();
 
   // ... then the section header table ...
   const unsigned SectionHeaderOffset = OS.tell();
 
   // ... then the section header table ...
-  writeSectionHeader(Asm, GroupMap, Layout, SectionIndexMap, SectionOffsetMap);
+  writeSectionHeader(Sections, Asm, GroupMap, Layout, SectionIndexMap,
+                     SectionOffsetMap);
 
   if (is64Bit()) {
     uint64_t Val = SectionHeaderOffset;
 
   if (is64Bit()) {
     uint64_t Val = SectionHeaderOffset;
index b7bed61bd44f2195c82f0832c6c6986ae02c7d78..0c44c0e2c06d911822572aabe6086a9f9b362b3f 100644 (file)
@@ -52,7 +52,7 @@
 @ CHECK-NEXT:     SHF_GROUP
 @ CHECK-NEXT:   ]
 @ CHECK-NEXT:   Address: 0x0
 @ CHECK-NEXT:     SHF_GROUP
 @ CHECK-NEXT:   ]
 @ CHECK-NEXT:   Address: 0x0
-@ CHECK-NEXT:   Offset: 0x60
+@ CHECK-NEXT:   Offset:
 @ CHECK-NEXT:   Size: 4
 @ CHECK-NEXT:   Link: 0
 @ CHECK-NEXT:   Info: 0
 @ CHECK-NEXT:   Size: 4
 @ CHECK-NEXT:   Link: 0
 @ CHECK-NEXT:   Info: 0
@@ -69,7 +69,7 @@
 @ CHECK-NEXT:     SHF_LINK_ORDER
 @ CHECK-NEXT:   ]
 @ CHECK-NEXT:   Address: 0x0
 @ CHECK-NEXT:     SHF_LINK_ORDER
 @ CHECK-NEXT:   ]
 @ CHECK-NEXT:   Address: 0x0
-@ CHECK-NEXT:   Offset: 0x64
+@ CHECK-NEXT:   Offset:
 @ CHECK-NEXT:   Size: 8
 @ CHECK-NEXT:   Link: 9
 @ CHECK-NEXT:   Info: 0
 @ CHECK-NEXT:   Size: 8
 @ CHECK-NEXT:   Link: 9
 @ CHECK-NEXT:   Info: 0
index 38ef8284a0dfa162bc431ba968d2525e4299cedd..072265c5c701f1869e9e398b234a2a5097bbfc02 100644 (file)
@@ -17,7 +17,7 @@
 // CHECK-NEXT:     Flags [
 // CHECK-NEXT:     ]
 // CHECK-NEXT:     Address: 0x0
 // CHECK-NEXT:     Flags [
 // CHECK-NEXT:     ]
 // CHECK-NEXT:     Address: 0x0
-// CHECK-NEXT:     Offset: 0x50
+// CHECK-NEXT:     Offset:
 // CHECK-NEXT:     Size: 57
 // CHECK-NEXT:     Link: 0
 // CHECK-NEXT:     Info: 0
 // CHECK-NEXT:     Size: 57
 // CHECK-NEXT:     Link: 0
 // CHECK-NEXT:     Info: 0