aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdhemerval Zanella <azanella@linux.vnet.ibm.com>2015-04-13 11:42:06 +0000
committerAdhemerval Zanella <azanella@linux.vnet.ibm.com>2015-04-13 11:42:06 +0000
commit91ad3847be68c52eec88804d33546d320c26665b (patch)
tree4203a56cedc2ef6a10c55bf739fbbf7a246377ae
parentd33e8e7f830ecdce6526c534f6fcf89b0971ca5e (diff)
ELF/AArch64: Check ADR_PREL_PG_HI21 for overflow
Add support for overflow checking when processing R_AARCH64_ADR_PREL_PG_HI21 relocations and add test. Patch Will Newton. git-svn-id: https://llvm.org/svn/llvm-project/lld/trunk@234743 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--lib/ReaderWriter/ELF/AArch64/AArch64RelocationHandler.cpp13
-rw-r--r--test/elf/AArch64/rel-adr_prel_pg_hi21-overflow.test45
-rw-r--r--test/elf/AArch64/rel-adr_prel_pg_hi21.test50
3 files changed, 103 insertions, 5 deletions
diff --git a/lib/ReaderWriter/ELF/AArch64/AArch64RelocationHandler.cpp b/lib/ReaderWriter/ELF/AArch64/AArch64RelocationHandler.cpp
index 2d305224..ceed0703 100644
--- a/lib/ReaderWriter/ELF/AArch64/AArch64RelocationHandler.cpp
+++ b/lib/ReaderWriter/ELF/AArch64/AArch64RelocationHandler.cpp
@@ -114,9 +114,12 @@ static std::error_code relocR_AARCH64_PREL16(uint8_t *location, uint64_t P,
}
/// \brief R_AARCH64_ADR_PREL_PG_HI21 - Page(S+A) - Page(P)
-static void relocR_AARCH64_ADR_PREL_PG_HI21(uint8_t *location, uint64_t P,
- uint64_t S, int64_t A) {
- uint64_t result = (page(S + A) - page(P));
+static std::error_code relocR_AARCH64_ADR_PREL_PG_HI21(uint8_t *location,
+ uint64_t P, uint64_t S,
+ int64_t A) {
+ int64_t result = page(S + A) - page(P);
+ if (!isInt<32>(result))
+ return make_out_of_range_reloc_error();
result = result >> 12;
uint32_t immlo = result & 0x3;
uint32_t immhi = result & 0x1FFFFC;
@@ -130,6 +133,7 @@ static void relocR_AARCH64_ADR_PREL_PG_HI21(uint8_t *location, uint64_t P,
llvm::dbgs() << " immlo: " << Twine::utohexstr(immlo);
llvm::dbgs() << " result: " << Twine::utohexstr(result) << "\n");
write32le(location, immlo | immhi | read32le(location));
+ return std::error_code();
}
/// \brief R_AARCH64_ADR_PREL_LO21 - S + A - P
@@ -405,8 +409,7 @@ std::error_code AArch64TargetRelocationHandler::applyRelocation(
case R_AARCH64_GLOB_DAT:
break;
case R_AARCH64_ADR_PREL_PG_HI21:
- relocR_AARCH64_ADR_PREL_PG_HI21(loc, reloc, target, addend);
- break;
+ return relocR_AARCH64_ADR_PREL_PG_HI21(loc, reloc, target, addend);
case R_AARCH64_ADR_PREL_LO21:
relocR_AARCH64_ADR_PREL_LO21(loc, reloc, target, addend);
break;
diff --git a/test/elf/AArch64/rel-adr_prel_pg_hi21-overflow.test b/test/elf/AArch64/rel-adr_prel_pg_hi21-overflow.test
new file mode 100644
index 00000000..674c38ba
--- /dev/null
+++ b/test/elf/AArch64/rel-adr_prel_pg_hi21-overflow.test
@@ -0,0 +1,45 @@
+# Check handling of R_AARCH64_ADR_PREL_PG_HI21 relocation overflow.
+# RUN: yaml2obj -format=elf %s > %t-obj
+# RUN: not lld -flavor gnu -target arm64 -o %t-exe %t-obj
+
+# CHECK-DAG: Relocation out of range in file {{.*}}: reference from _start+0 to data1+2147483649 of type 275 (R_AARCH64_ADR_PREL_PG_HI21)
+
+!ELF
+FileHeader: !FileHeader
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_REL
+ Machine: EM_AARCH64
+
+Sections:
+- Name: .text
+ Type: SHT_PROGBITS
+ Content: "00000090"
+ AddressAlign: 16
+ Flags: [SHF_ALLOC, SHF_EXECINSTR]
+
+- Name: .data
+ Type: SHT_PROGBITS
+ Content: "00000000"
+ AddressAlign: 4096
+ Flags: [SHF_ALLOC, SHF_WRITE]
+
+- Name: .rela.text
+ Type: SHT_RELA
+ Info: .text
+ AddressAlign: 8
+ Relocations:
+ - Offset: 0x0
+ Symbol: data1
+ Type: R_AARCH64_ADR_PREL_PG_HI21
+ Addend: 0x80000001
+
+Symbols:
+ Global:
+ - Name: _start
+ Section: .text
+ Value: 0x0
+ Size: 4
+ - Name: data1
+ Section: .data
+ Size: 8
diff --git a/test/elf/AArch64/rel-adr_prel_pg_hi21.test b/test/elf/AArch64/rel-adr_prel_pg_hi21.test
new file mode 100644
index 00000000..3cbbd366
--- /dev/null
+++ b/test/elf/AArch64/rel-adr_prel_pg_hi21.test
@@ -0,0 +1,50 @@
+# Check handling of R_AARCH64_ADR_PREL_PG_HI21 relocation.
+# RUN: yaml2obj -format=elf %s > %t-obj
+# RUN: lld -flavor gnu -target arm64 -o %t-exe %t-obj
+# RUN: llvm-objdump -d -t %t-exe | FileCheck %s
+
+# CHECK: Disassembly of section .text:
+# CHECK-NEXT: _start:
+# CHECK-NEXT: 4001b0: 00 00 00 d0 adrp x0, #8192
+# CHECK: SYMBOL TABLE:
+# CHECK: 00402000 g .data 00000004 data1
+
+!ELF
+FileHeader: !FileHeader
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_REL
+ Machine: EM_AARCH64
+
+Sections:
+- Name: .text
+ Type: SHT_PROGBITS
+ Content: "00000090"
+ AddressAlign: 16
+ Flags: [SHF_ALLOC, SHF_EXECINSTR]
+
+- Name: .data
+ Type: SHT_PROGBITS
+ Content: "00000000"
+ AddressAlign: 4096
+ Flags: [SHF_ALLOC, SHF_WRITE]
+
+- Name: .rela.text
+ Type: SHT_RELA
+ Info: .text
+ AddressAlign: 8
+ Relocations:
+ - Offset: 0x0
+ Symbol: data1
+ Type: R_AARCH64_ADR_PREL_PG_HI21
+ Addend: 0
+
+Symbols:
+ Global:
+ - Name: _start
+ Section: .text
+ Value: 0x0
+ Size: 4
+ - Name: data1
+ Section: .data
+ Size: 8