Index: ELF/Target.cpp =================================================================== --- ELF/Target.cpp +++ ELF/Target.cpp @@ -703,18 +703,22 @@ return Expr & (~static_cast(0xFFF)); } +template static void checkOutOfRange(int64_t X, uint32_t Type) { + if (!isInt(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"); + checkOutOfRange<16>(SA, Type); write16le(Loc, SA); break; case R_AARCH64_ABS32: - if (!isInt<32>(SA)) - error("Relocation R_AARCH64_ABS32 out of range"); + checkOutOfRange<32>(SA, Type); write32le(Loc, SA); break; case R_AARCH64_ABS64: @@ -731,30 +735,21 @@ 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"); + checkOutOfRange<21>(X, Type); updateAArch64Adr(Loc, X & 0x1FFFFF); break; } case R_AARCH64_ADR_GOT_PAGE: case R_AARCH64_ADR_PREL_PG_HI21: { uint64_t X = getAArch64Page(SA) - getAArch64Page(P); - if (!isInt<33>(X)) { - if (Type == R_AARCH64_ADR_GOT_PAGE) - error("Relocation R_AARCH64_ADR_GOT_PAGE out of range"); - error("Relocation R_AARCH64_ADR_PREL_PG_HI21 out of range"); - } + checkOutOfRange<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"); - } + checkOutOfRange<28>(X, Type); or32le(Loc, (X & 0x0FFFFFFC) >> 2); break; } @@ -777,13 +772,11 @@ or32le(Loc, (SA & 0xFFF) << 10); break; case R_AARCH64_PREL16: - if (!isInt<16>(SA)) - error("Relocation R_AARCH64_PREL16 out of range"); + checkOutOfRange<16>(SA - P, Type); write16le(Loc, SA - P); break; case R_AARCH64_PREL32: - if (!isInt<32>(SA)) - error("Relocation R_AARCH64_PREL32 out of range"); + checkOutOfRange<32>(SA - P, Type); write32le(Loc, SA - P); break; case R_AARCH64_PREL64: