Skip to content

Commit 6bdde34

Browse files
committed
[ARM][MC] Add Range Checks for MOVT and MOVW Instructions
As per the ARM ABI, the MOVT and MOVW instructions should have addends that fall within a 16bit signed range. LLVM does not check this so it is possible to use addends that are beyond the accepted range. These addends are silently truncated. A new check is added to ensure the addend falls within the expected range, rejecting an addend that falls outside with an error. Information relating to the ABI requirements can be found here: https://github.com/ARM-software/abi-aa/blob/main/aaelf32/aaelf32.rst#addends-and-pc-bias-compensation
1 parent a440203 commit 6bdde34

File tree

4 files changed

+43
-8
lines changed

4 files changed

+43
-8
lines changed

llvm/lib/Target/ARM/MCTargetDesc/ARMAsmBackend.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "llvm/Support/EndianStream.h"
3535
#include "llvm/Support/ErrorHandling.h"
3636
#include "llvm/Support/Format.h"
37+
#include "llvm/Support/MathExtras.h"
3738
#include "llvm/Support/raw_ostream.h"
3839
using namespace llvm;
3940

@@ -472,11 +473,19 @@ unsigned ARMAsmBackend::adjustFixupValue(const MCAssembler &Asm,
472473
case FK_SecRel_4:
473474
return Value;
474475
case ARM::fixup_arm_movt_hi16:
476+
if(!(minIntN(16) <= static_cast<int64_t>(Value) && static_cast<int64_t>(Value) <= maxIntN(16))) {
477+
Ctx.reportError(Fixup.getLoc(), "Relocation Not In Range");
478+
return 0;
479+
}
475480
assert(STI != nullptr);
476481
if (IsResolved || !STI->getTargetTriple().isOSBinFormatELF())
477482
Value >>= 16;
478483
[[fallthrough]];
479484
case ARM::fixup_arm_movw_lo16: {
485+
if(!(minIntN(16) <= static_cast<int64_t>(Value) && static_cast<int64_t>(Value) <= maxIntN(16))) {
486+
Ctx.reportError(Fixup.getLoc(), "Relocation Not In Range");
487+
return 0;
488+
}
480489
unsigned Hi4 = (Value & 0xF000) >> 12;
481490
unsigned Lo12 = Value & 0x0FFF;
482491
// inst{19-16} = Hi4;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@RUN: not llvm-mc -triple armv7-eabi -filetype obj -o - %s 2>&1 | FileCheck %s
2+
3+
.global v
4+
.text
5+
movw r1, #:lower16:v + -65536
6+
movt r1, #:upper16:v + 65536
7+
8+
@CHECK: error: Relocation Not In Range
9+
@CHECK: movw r1, #:lower16:v + -65536
10+
@CHECK: ^
11+
@CHECK: error: Relocation Not In Range
12+
@CHECK: movt r1, #:upper16:v + 65536
13+
@CHECK: ^
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@RUN: llvm-mc -triple armv7-eabi -filetype obj -o - %s 2>&1 | FileCheck %s
2+
3+
.global v
4+
.text
5+
movw r1, #:lower16:v + -20000
6+
movt r1, #:upper16:v + 20000
7+
8+
@CHECK-NOT: error: Relocation Not In Range
9+
@CHECK-NOT: movw r1, #:lower16:v + -20000
10+
@CHECK-NOT: ^
11+
@CHECK-NOT: error: Relocation Not In Range
12+
@CHECK-NOT: movt r1, #:upper16:v + 20000
13+
@CHECK-NOT: ^

llvm/test/MC/ARM/macho-movwt.s

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
movw r0, :lower16:_x+4
99
movt r0, :upper16:_x+4
1010

11-
movw r0, :lower16:_x+0x10000
12-
movt r0, :upper16:_x+0x10000
11+
movw r0, :lower16:_x+0x1000
12+
movt r0, :upper16:_x+0x1000
1313

1414
.arm
1515
movw r0, :lower16:_x
@@ -18,8 +18,8 @@
1818
movw r0, :lower16:_x+4
1919
movt r0, :upper16:_x+4
2020

21-
movw r0, :lower16:_x+0x10000
22-
movt r0, :upper16:_x+0x10000
21+
movw r0, :lower16:_x+0x1000
22+
movt r0, :upper16:_x+0x1000
2323

2424
@ Enter the bizarre world of MachO relocations. First, they're in reverse order
2525
@ to the actual instructions
@@ -30,10 +30,10 @@
3030
@ Third column identifies ARM/Thumb & HI/LO.
3131

3232
@ CHECK: 0x2C 0 1 1 ARM_RELOC_HALF 0 _x
33-
@ CHECK: 0x0 0 1 0 ARM_RELOC_PAIR 0 -
33+
@ CHECK: 0x1000 0 1 0 ARM_RELOC_PAIR 0 -
3434

3535
@ CHECK: 0x28 0 0 1 ARM_RELOC_HALF 0 _x
36-
@ CHECK: 0x1 0 0 0 ARM_RELOC_PAIR 0 -
36+
@ CHECK: 0x0 0 0 0 ARM_RELOC_PAIR 0 -
3737

3838
@ CHECK: 0x24 0 1 1 ARM_RELOC_HALF 0 _x
3939
@ CHECK: 0x4 0 1 0 ARM_RELOC_PAIR 0 -
@@ -48,10 +48,10 @@
4848
@ CHECK: 0x0 0 0 0 ARM_RELOC_PAIR 0 -
4949

5050
@ CHECK: 0x14 0 3 1 ARM_RELOC_HALF 0 _x
51-
@ CHECK: 0x0 0 3 0 ARM_RELOC_PAIR 0 -
51+
@ CHECK: 0x1000 0 3 0 ARM_RELOC_PAIR 0 -
5252

5353
@ CHECK: 0x10 0 2 1 ARM_RELOC_HALF 0 _x
54-
@ CHECK: 0x1 0 2 0 ARM_RELOC_PAIR 0 -
54+
@ CHECK: 0x0 0 2 0 ARM_RELOC_PAIR 0 -
5555

5656
@ CHECK: 0xC 0 3 1 ARM_RELOC_HALF 0 _x
5757
@ CHECK: 0x4 0 3 0 ARM_RELOC_PAIR 0 -

0 commit comments

Comments
 (0)