[llvm] [bolt][aarch64] Adding test with unsupported indirect branches (PR #127655)

Alexey Moksyakov via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 18 07:54:30 PST 2025


https://github.com/yavtuk updated https://github.com/llvm/llvm-project/pull/127655

>From ad3e3ece482b10bb7c87178c38b1583e9c7287ae Mon Sep 17 00:00:00 2001
From: Alexey Moksyakov <alexey.moksyakov at huawei.com>
Date: Tue, 18 Feb 2025 18:40:21 +0300
Subject: [PATCH 1/2] [bolt][aarch64] Adding test with unsupported indirect
 branches

---
 bolt/test/AArch64/jmp-table-unsupported.s | 315 ++++++++++++++++++++++
 1 file changed, 315 insertions(+)
 create mode 100644 bolt/test/AArch64/jmp-table-unsupported.s

diff --git a/bolt/test/AArch64/jmp-table-unsupported.s b/bolt/test/AArch64/jmp-table-unsupported.s
new file mode 100644
index 0000000000000..dfdc78d48dbec
--- /dev/null
+++ b/bolt/test/AArch64/jmp-table-unsupported.s
@@ -0,0 +1,315 @@
+## This test checks that disassemble stage works properly
+## JT with indirect branch
+## 1) nop + adr pair instructions
+## 2) sub + ldr pair instructions
+## 3) adrp + ldr pair instructions
+## 4) pic jt with relive offsets packed to 1-byte entry size
+## 5) fixed indirect branch
+## 6) normal jt
+
+# REQUIRES: system-linux
+
+# RUN: rm -rf %t && split-file %s %t
+
+## Prepare binary (1)
+# RUN: llvm-mc -filetype=obj -triple aarch64-unknown-unknown %t/jt_nop_adr.s \
+# RUN:    -o %t/jt_nop_adr.o
+# RUN: %clang %cflags --target=aarch64-unknown-linux %t/jt_nop_adr.o \
+# RUN:  -Wl,-q -Wl,-z,now, -Wl,-T,%t/within-adr-range.t -o %t/jt_nop_adr.exe
+# RUN: llvm-objdump --no-show-raw-insn -d %t/jt_nop_adr.exe | FileCheck \
+# RUN:    --check-prefix=JT-RELAXED %s
+
+# JT-RELAXED: <_start>:
+# JT-RELAXED-NEXT:  nop
+# JT-RELAXED-NEXT:  adr {{.*}}x3
+
+# RUN: llvm-bolt %t/jt_nop_adr.exe -o %t/jt_nop_adr.bolt -v 3 2>&1 | FileCheck \
+# RUN:    --check-prefix=JT-BOLT-RELAXED %s
+
+# JT-BOLT-RELAXED: failed to match indirect branch
+
+## This linker script ensures that .rodata and .text are sufficiently (<1M)
+## close to each other so that the adrp + ldr pair can be relaxed to nop + adr.
+#--- within-adr-range.t
+SECTIONS {
+ .rodata 0x1000: { *(.rodata) }
+ .text   0x2000: { *(.text) }
+ .rela.rodata :    { *(.rela.rodata) }
+}
+
+## Prepare binary (2)
+# RUN: llvm-mc -filetype=obj -triple aarch64-unknown-unknown %t/jt_sub_ldr.s \
+# RUN:   -o %t/jt_sub_ldr.o
+# RUN: %clang %cflags --target=aarch64-unknown-linux %t/jt_sub_ldr.o \
+# RUN:  -Wl,-q -Wl,-z,now -o %t/jt_sub_ldr.exe
+# RUN: llvm-objdump --no-show-raw-insn -d %t/jt_sub_ldr.exe | FileCheck \
+# RUN:    --check-prefix=JT-SUB-LDR %s
+
+# JT-SUB-LDR: <_start>:
+# JT-SUB-LDR-NEXT:  sub
+# JT-SUB-LDR-NEXT:  ldr
+
+# RUN: llvm-bolt %t/jt_sub_ldr.exe -o %t/jt_sub_ldr.bolt -v 3 2>&1 | FileCheck \
+# RUN:    --check-prefix=JT-BOLT-SUBLDR %s
+# JT-BOLT-SUBLDR: failed to match indirect branch
+
+## Prepare binary (3)
+# RUN: llvm-mc -filetype=obj -triple aarch64-unknown-unknown %t/jt_adrp_ldr.s \
+# RUN:    -o %t/jt_adrp_ldr.o
+# RUN: %clang %cflags --target=aarch64-unknown-linux %t/jt_adrp_ldr.o \
+# RUN:  -Wl,-q -Wl,-z,now  -Wl,--no-relax -o %t/jt_adrp_ldr.exe
+# RUN: llvm-objdump --no-show-raw-insn -d %t/jt_adrp_ldr.exe | FileCheck \
+# RUN:   --check-prefix=JT-ADRP-LDR %s
+
+# JT-ADRP-LDR: <_start>:
+# JT-ADRP-LDR-NEXT:  adrp
+# JT-ADRP-LDR-NEXT:  ldr
+
+# RUN: llvm-bolt %t/jt_adrp_ldr.exe -o %t/jt_adrp_ldr.bolt -v 3 2>&1 | FileCheck \
+# RUN:   --check-prefix=JT-BOLT-ADRP-LDR %s
+# JT-BOLT-ADRP-LDR: failed to match indirect branch
+
+## Prepare binary (4)
+# RUN: llvm-mc -filetype=obj -triple aarch64-unknown-unknown \
+# RUN:    --position-independent %t/jt_pic_with_relative_offset.s \
+# RUN:    -o %t/jt_pic_with_relative_offset.o
+# RUN: %clang %cflags -fPIC -O0  %t/jt_pic_with_relative_offset.o \
+# RUN:    -o %t/jt_pic_with_relative_offset.exe -Wl,-q -Wl,--no-relax
+# RUN: llvm-bolt %t/jt_pic_with_relative_offset.exe \
+# RUN:    -o %t/jt_pic_with_relative_offset.bolt -v 3 2>&1 | FileCheck \
+# RUN:   --check-prefix=JT-BOLT-JT-PIC-OFFSETS %s
+
+# JT-BOLT-JT-PIC-OFFSETS: failed to match indirect branch
+
+## Prepare binary (5)
+# RUN: %clang %cflags %t/jt_fixed_branch.s -Wl,-q -Wl,--no-relax \
+# RUN:     -o %t/jt_fixed_branch.exe
+
+# RUN: llvm-bolt %t/jt_fixed_branch.exe \
+# RUN:    -o %t/jt_fixed_branch.bolt -v 3 2>&1 | FileCheck \
+# RUN:   --check-prefix=JT-BOLT-FIXED-BR %s
+
+# JT-BOLT-FIXED-BR: failed to match indirect branch
+
+## Prepare binary (6)
+# RUN: %clang %t/jt_type_normal.c \
+# RUN:   -no-pie \
+# RUN:   -Wl,-q -Wl,-z,now -o %t/jt_type_normal.exe
+# RUN: llvm-objdump --no-show-raw-insn -d %t/jt_type_normal.exe | FileCheck \
+# RUN:   --check-prefix=JT-OBJDUMP-NORMAL %s
+
+# JT-OBJDUMP-NORMAL: <handleOptionJumpTable>:
+# JT-OBJDUMP-NORMAL:  adrp
+# JT-OBJDUMP-NORMAL-NEXT:  (ldr|add)
+# JT-OBJDUMP-NORMAL-NEXT:  (ldr|add)
+# JT-OBJDUMP-NORMAL-NEXT:  br
+
+# RUN: llvm-bolt %t/jt_type_normal.exe \
+# RUN:    -o %t/jt_type_normal.bolt -v 3 2>&1 | FileCheck \
+# RUN:   --check-prefix=JT-BOLT-NORMAL %s
+
+# JT-BOLT-NORMAL: failed to match indirect branch
+
+
+#--- jt_nop_adr.s
+  .globl _start
+  .type  _start, %function
+_start:
+  adrp    x3, :got:jump_table
+  ldr     x3, [x3, #:got_lo12:jump_table]
+  ldrh    w3, [x3, x1, lsl #1]
+  adr     x1, test2_0
+  add     x3, x1, w3, sxth #2
+  br      x3
+test2_0:
+  ret
+test2_1:
+  ret
+
+  .section .rodata,"a", at progbits
+jump_table:
+  .hword  (test2_0-test2_0)>>2
+  .hword  (test2_1-test2_0)>>2
+
+
+#--- jt_sub_ldr.s
+  .globl _start
+  .type  _start, %function
+_start:
+  sub     x1, x29, #0x4, lsl #12
+  ldr     x1, [x1, #14352]
+  ldrh    w1, [x1, w3, uxtw #1]
+  adr     x3, test2_0
+  add     x1, x3, w1, sxth #2
+  br      x1
+test2_0:
+  ret
+test2_1:
+  ret
+
+  .section .rodata,"a", at progbits
+jump_table:
+  .hword  (test2_0-test2_0)>>2
+  .hword  (test2_1-test2_0)>>2
+
+
+#--- jt_adrp_ldr.s
+  .globl _start
+  .type  _start, %function
+_start:
+  adrp    x3, :got:jump_table
+  ldr     x3, [x3, #:got_lo12:jump_table]
+  ldrh    w3, [x3, x1, lsl #1]
+  adr     x1, test2_0
+  add     x3, x1, w3, sxth #2
+  br      x3
+test2_0:
+  ret
+test2_1:
+  ret
+
+  .section .rodata,"a", at progbits
+jump_table:
+  .hword  (test2_0-test2_0)>>2
+  .hword  (test2_1-test2_0)>>2
+
+
+#--- jt_pic_with_relative_offset.s
+.text
+.global _start
+_start:
+  mov     x4, 3  // index in jmp table where offset related to adr instr
+  adrp    x0, funcTableSym
+  add     x0, x0, #:lo12:funcTableSym
+  ldrb    w0, [x0, w4, uxtw #0]
+  adr     x2, .LBB1
+  add     x0, x2, w0, sxth #2
+  br      x0
+
+.LBB1:
+  bl      funcA
+  b       .test_exit
+
+.LBB2:
+  bl      funcB
+  b       .test_exit
+
+.LBB3:
+  bl      funcC
+  b       .test_exit
+
+.LBB4:
+  bl      funcD
+  b       .test_exit
+
+.test_exit:
+  mov x8, #93
+  mov x0, #0
+  svc #0
+
+.global funcA
+funcA:
+  mov     x1, 0   // Message pointer
+  mov     x2, #0  // Message length
+  mov x0, #1      // File descriptor: stdout
+  mov x8, #64     // sys_write system call number
+  svc 0
+  ret
+
+.global funcB
+funcB:
+  mov     x1, 0   // Message pointer
+  mov     x2, #0  // Message length
+  mov x0, #1      // File descriptor: stdout
+  mov x8, #64     // sys_write system call number
+  svc 0
+  ret
+
+.global funcC
+funcC:
+  mov     x1, 0   // Message pointer
+  mov     x2, #0  // Message length
+  mov x0, #1      // File descriptor: stdout
+  mov x8, #64     // sys_write system call number
+  svc 0
+  ret
+
+.global funcD
+funcD:
+  mov     x1, 0   // Message pointer
+  mov     x2, #0  // Message length
+  mov x0, #1      // File descriptor: stdout
+  mov x8, #64     // sys_write system call number
+  svc 0
+  ret
+
+.section .rodata,"a", at progbits
+.align 2
+funcTableSym:
+  .byte 0x00,0x02,0x04,0x06  // 1 - .LBB1, 3 - .LBB2
+
+#--- jt_fixed_branch.s
+
+.text
+.global _start
+_start:
+  mov	x0, x13
+  mov	x1, x4
+  mov	x0, x2
+  movk	x1, #0x0, lsl #48
+  movk	x1, #0x0, lsl #32
+  movk	x1, #0x0, lsl #16
+  movk	x1, #0x12
+  stp	x0, x1, [sp, #-16]!
+  adrp    x0, foo
+  add     x0, x0, #:lo12:foo
+  br     x0
+  mov x8, #93
+  mov x0, #0
+  svc #0
+
+.global foo
+.type foo,%function
+foo:
+  mov x8, #9
+  ret
+.size foo,.-foo
+
+#--- jt_type_normal.c
+
+#include <stdio.h>
+
+void __attribute__ ((noinline)) option0() {
+    printf("Option 0 selected\n");
+}
+
+void __attribute__ ((noinline)) option1() {
+    printf("Option 1 selected\n");
+}
+
+void __attribute__ ((noinline)) option2() {
+    printf("Option 2 selected\n");
+}
+
+void __attribute__ ((noinline)) option3() {
+    printf("Option 3 selected\n");
+}
+
+void __attribute__ ((noinline)) option4() {
+    printf("Option 4 selected\n");
+}
+
+void __attribute__ ((noinline)) option5() {
+    printf("Option 5 selected\n");
+}
+
+void (*jumpTable[6])() = { option0, option1, option2, option3, option4, option5 };
+
+void __attribute__ ((noinline)) handleOptionJumpTable(int option) {
+    jumpTable[option]();
+}
+
+int main(int argc, char *argv[]) {
+    handleOptionJumpTable(argc);
+    return 0;
+}

>From da1f1d4e365b83c1ef2ae72f2cbf16016ace5260 Mon Sep 17 00:00:00 2001
From: Alexey Moksyakov <yavtuk at yandex.ru>
Date: Tue, 18 Feb 2025 18:54:09 +0300
Subject: [PATCH 2/2] [bolt][aarch64] Adding extra log for indir branch
 matching

---
 bolt/lib/Core/BinaryFunction.cpp | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/bolt/lib/Core/BinaryFunction.cpp b/bolt/lib/Core/BinaryFunction.cpp
index bc45caf3ec8b7..36286eff91c08 100644
--- a/bolt/lib/Core/BinaryFunction.cpp
+++ b/bolt/lib/Core/BinaryFunction.cpp
@@ -833,6 +833,14 @@ BinaryFunction::processIndirectBranch(MCInst &Instruction, unsigned Size,
       Instruction, Begin, Instructions.end(), PtrSize, MemLocInstr, BaseRegNum,
       IndexRegNum, DispValue, DispExpr, PCRelBaseInstr, FixedEntryLoadInstr);
 
+  if (BranchType == IndirectBranchType::UNKNOWN) {
+    if (opts::Verbosity > 2)
+      outs() << "BOLT-WARNING: failed to match indirect branch, "
+             << getPrintName() << " at 0x" << Twine::utohexstr(Offset)
+             << " offset\n";
+    return IndirectBranchType::UNKNOWN;
+  }
+
   if (BranchType == IndirectBranchType::UNKNOWN && !MemLocInstr)
     return BranchType;
 



More information about the llvm-commits mailing list