[llvm] r340736 - [Sparc] Avoid writing outside array in applyFixup

Daniel Cederman via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 27 04:43:59 PDT 2018


Author: dcederman
Date: Mon Aug 27 04:43:59 2018
New Revision: 340736

URL: http://llvm.org/viewvc/llvm-project?rev=340736&view=rev
Log:
[Sparc] Avoid writing outside array in applyFixup

Summary: If an object file ends with a relocation that is smaller
than 4 bytes we will write outside the Data array and trigger an
"Invalid index" assertion.

Reviewers: jyknight, venkatra

Reviewed By: jyknight

Subscribers: fedor.sergeev, jrtc27, llvm-commits

Differential Revision: https://reviews.llvm.org/D50971

Modified:
    llvm/trunk/lib/Target/Sparc/MCTargetDesc/SparcAsmBackend.cpp
    llvm/trunk/test/MC/Sparc/sparc-relocations.s

Modified: llvm/trunk/lib/Target/Sparc/MCTargetDesc/SparcAsmBackend.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/Sparc/MCTargetDesc/SparcAsmBackend.cpp?rev=340736&r1=340735&r2=340736&view=diff
==============================================================================
--- llvm/trunk/lib/Target/Sparc/MCTargetDesc/SparcAsmBackend.cpp (original)
+++ llvm/trunk/lib/Target/Sparc/MCTargetDesc/SparcAsmBackend.cpp Mon Aug 27 04:43:59 2018
@@ -100,6 +100,20 @@ static unsigned adjustFixupValue(unsigne
   }
 }
 
+/// getFixupKindNumBytes - The number of bytes the fixup may change.
+static unsigned getFixupKindNumBytes(unsigned Kind) {
+    switch (Kind) {
+  default:
+    return 4;
+  case FK_Data_1:
+    return 1;
+  case FK_Data_2:
+    return 2;
+  case FK_Data_8:
+    return 8;
+  }
+}
+
 namespace {
   class SparcAsmBackend : public MCAsmBackend {
   protected:
@@ -290,13 +304,13 @@ namespace {
       Value = adjustFixupValue(Fixup.getKind(), Value);
       if (!Value) return;           // Doesn't change encoding.
 
+      unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
       unsigned Offset = Fixup.getOffset();
-
       // For each byte of the fragment that the fixup touches, mask in the bits
       // from the fixup value. The Value has been "split up" into the
       // appropriate bitfields above.
-      for (unsigned i = 0; i != 4; ++i) {
-        unsigned Idx = Endian == support::little ? i : 3 - i;
+      for (unsigned i = 0; i != NumBytes; ++i) {
+        unsigned Idx = Endian == support::little ? i : (NumBytes - 1) - i;
         Data[Offset + Idx] |= uint8_t((Value >> (i * 8)) & 0xff);
       }
     }

Modified: llvm/trunk/test/MC/Sparc/sparc-relocations.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/Sparc/sparc-relocations.s?rev=340736&r1=340735&r2=340736&view=diff
==============================================================================
--- llvm/trunk/test/MC/Sparc/sparc-relocations.s (original)
+++ llvm/trunk/test/MC/Sparc/sparc-relocations.s Mon Aug 27 04:43:59 2018
@@ -49,3 +49,9 @@
         ! CHECK: or %g1, sym, %g3 ! encoding: [0x86,0x10,0b011AAAAA,A]
         ! CHECK-NEXT:                  !   fixup A - offset: 0, value: sym, kind: fixup_sparc_13
         or %g1, sym, %g3
+
+        ! This test needs to placed last in the file
+        ! CHECK: .half	a-.Ltmp0
+        .half a - .
+        .byte a - .
+a:




More information about the llvm-commits mailing list