[ELF] - Fixed padding for CIE/FDE entries of .eh_frame section
authorGeorge Rimar <grimar@accesssoftek.com>
Mon, 21 Dec 2015 09:38:59 +0000 (09:38 +0000)
committerGeorge Rimar <grimar@accesssoftek.com>
Mon, 21 Dec 2015 09:38:59 +0000 (09:38 +0000)
Spec says both CIE/FDE has "Padding
Extra bytes to align the CIE structure to an addressing unit size boundary."
https://refspecs.linuxfoundation.org/LSB_3.0.0/LSB-PDA/LSB-PDA/ehframechpt.html

Patch aligns CIE/FDE entries to the size of platform pointer.

Differential revision: http://reviews.llvm.org/D15637

llvm-svn: 256141

lld/ELF/OutputSections.cpp
lld/test/ELF/eh-frame-merge.s

index 7348f35..9b5fad1 100644 (file)
@@ -971,7 +971,7 @@ void EHOutputSection<ELFT>::addSectionAux(
       auto P = CieMap.insert(std::make_pair(CieInfo, Cies.size()));
       if (P.second) {
         Cies.push_back(C);
-        this->Header.sh_size += Length;
+        this->Header.sh_size += RoundUpToAlignment(Length, sizeof(uintX_t));
       }
       OffsetToIndex[Offset] = P.first->second;
     } else {
@@ -984,7 +984,7 @@ void EHOutputSection<ELFT>::addSectionAux(
         if (I == OffsetToIndex.end())
           error("Invalid CIE reference");
         Cies[I->second].Fdes.push_back(EHRegion<ELFT>(S, Index));
-        this->Header.sh_size += Length;
+        this->Header.sh_size += RoundUpToAlignment(Length, sizeof(uintX_t));
       }
     }
 
@@ -1040,15 +1040,16 @@ template <class ELFT> void EHOutputSection<ELFT>::writeTo(uint8_t *Buf) {
     StringRef CieData = C.data();
     memcpy(Buf + Offset, CieData.data(), CieData.size());
     C.S->Offsets[C.Index].second = Offset;
-    Offset += CieData.size();
+    Offset += RoundUpToAlignment(CieData.size(), sizeof(uintX_t));
 
     for (const EHRegion<ELFT> &F : C.Fdes) {
       StringRef FdeData = F.data();
-      memcpy(Buf + Offset, FdeData.data(), 4);              // Length
+      uintX_t Len = RoundUpToAlignment(FdeData.size(), sizeof(uintX_t));
+      write32<E>(Buf + Offset, Len - 4);                    // Length
       write32<E>(Buf + Offset + 4, Offset + 4 - CieOffset); // Pointer
       memcpy(Buf + Offset + 8, FdeData.data() + 8, FdeData.size() - 8);
       F.S->Offsets[F.Index].second = Offset;
-      Offset += FdeData.size();
+      Offset += Len;
     }
   }
 
index 0a60ef5..2b0e4a5 100644 (file)
 // CHECK-NEXT: ]
 // CHECK-NEXT: Address:
 // CHECK-NEXT: Offset:
-// CHECK-NEXT: Size:
+// CHECK-NEXT: Size: 96
 // CHECK-NEXT: Link: 0
 // CHECK-NEXT: Info: 0
 // CHECK-NEXT: AddressAlignment: 8
 // CHECK-NEXT: EntrySize: 0
 // CHECK-NEXT: SectionData (
-// CHECK-NEXT:   0000: 14000000 00000000 017A5200 01781001  |
-// CHECK-NEXT:   0010: 1B0C0708 90010000 10000000 1C000000  |
-// CHECK-NEXT:   0020: 180E0000 01000000 00000000 10000000  |
-// CHECK-NEXT:   0030: 30000000 060E0000 02000000 00000000  |
-// CHECK-NEXT:   0040: 10000000 44000000 F10D0000 01000000  |
-// CHECK-NEXT:   0050: 00000000                             |
+// CHECK-NEXT: 0000: 14000000 00000000 017A5200 01781001  |
+// CHECK-NEXT: 0010: 1B0C0708 90010000 14000000 1C000000  |
+// CHECK-NEXT: 0020: 180E0000 01000000 00000000 00000000  |
+// CHECK-NEXT: 0030: 14000000 34000000 020E0000 02000000  |
+// CHECK-NEXT: 0040: 00000000 00000000 14000000 4C000000  |
+// CHECK-NEXT: 0050: E90D0000 01000000 00000000 00000000  |
 // CHECK-NEXT: )
 
 // CHECK:      Name: foo