[lld] r253884 - [ELF/AArch64] Factor out overflow checks into a separate function. NFC.

Igor Kudrin via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 23 09:16:09 PST 2015


Author: ikudrin
Date: Mon Nov 23 11:16:09 2015
New Revision: 253884

URL: http://llvm.org/viewvc/llvm-project?rev=253884&view=rev
Log:
[ELF/AArch64] Factor out overflow checks into a separate function. NFC.

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

Modified:
    lld/trunk/ELF/Target.cpp

Modified: lld/trunk/ELF/Target.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Target.cpp?rev=253884&r1=253883&r2=253884&view=diff
==============================================================================
--- lld/trunk/ELF/Target.cpp (original)
+++ lld/trunk/ELF/Target.cpp Mon Nov 23 11:16:09 2015
@@ -691,18 +691,23 @@ static uint64_t getAArch64Page(uint64_t
   return Expr & (~static_cast<uint64_t>(0xFFF));
 }
 
+template <unsigned N>
+static void checkAArch64OutOfRange(int64_t X, uint32_t Type) {
+  if (!isInt<N>(X))
+    error("Relocation " + getELFRelocationTypeName(EM_AARCH64, Type) +
+          " out of range");
+}
+
 void AArch64TargetInfo::relocateOne(uint8_t *Loc, uint8_t *BufEnd,
                                     uint32_t Type, uint64_t P,
                                     uint64_t SA) const {
   switch (Type) {
   case R_AARCH64_ABS16:
-    if (!isInt<16>(SA))
-      error("Relocation R_AARCH64_ABS16 out of range");
+    checkAArch64OutOfRange<16>(SA, Type);
     write16le(Loc, SA);
     break;
   case R_AARCH64_ABS32:
-    if (!isInt<32>(SA))
-      error("Relocation R_AARCH64_ABS32 out of range");
+    checkAArch64OutOfRange<32>(SA, Type);
     write32le(Loc, SA);
     break;
   case R_AARCH64_ABS64:
@@ -719,26 +724,20 @@ void AArch64TargetInfo::relocateOne(uint
     break;
   case R_AARCH64_ADR_PREL_LO21: {
     uint64_t X = SA - P;
-    if (!isInt<21>(X))
-      error("Relocation R_AARCH64_ADR_PREL_LO21 out of range");
+    checkAArch64OutOfRange<21>(X, Type);
     updateAArch64Adr(Loc, X & 0x1FFFFF);
     break;
   }
   case R_AARCH64_ADR_PREL_PG_HI21: {
     uint64_t X = getAArch64Page(SA) - getAArch64Page(P);
-    if (!isInt<33>(X))
-      error("Relocation R_AARCH64_ADR_PREL_PG_HI21 out of range");
+    checkAArch64OutOfRange<33>(X, Type);
     updateAArch64Adr(Loc, (X >> 12) & 0x1FFFFF); // X[32:12]
     break;
   }
   case R_AARCH64_JUMP26:
   case R_AARCH64_CALL26: {
     uint64_t X = SA - P;
-    if (!isInt<28>(X)) {
-      if (Type == R_AARCH64_JUMP26)
-        error("Relocation R_AARCH64_JUMP26 out of range");
-      error("Relocation R_AARCH64_CALL26 out of range");
-    }
+    checkAArch64OutOfRange<28>(X, Type);
     or32le(Loc, (X & 0x0FFFFFFC) >> 2);
     break;
   }
@@ -755,13 +754,11 @@ void AArch64TargetInfo::relocateOne(uint
     or32le(Loc, (SA & 0xFFF) << 10);
     break;
   case R_AARCH64_PREL16:
-    if (!isInt<16>(SA - P))
-      error("Relocation R_AARCH64_PREL16 out of range");
+    checkAArch64OutOfRange<16>(SA - P, Type);
     write16le(Loc, SA - P);
     break;
   case R_AARCH64_PREL32:
-    if (!isInt<32>(SA - P))
-      error("Relocation R_AARCH64_PREL32 out of range");
+    checkAArch64OutOfRange<32>(SA - P, Type);
     write32le(Loc, SA - P);
     break;
   case R_AARCH64_PREL64:




More information about the llvm-commits mailing list