[llvm] [BOLT] Refactor AArch64 Relocation Print (PR #101895)

via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 24 04:34:35 PDT 2024


https://github.com/linsinan1995 updated https://github.com/llvm/llvm-project/pull/101895

>From ee11fee27fe23725aa5da7b0671978e652e01b31 Mon Sep 17 00:00:00 2001
From: Sinan Lin <sinan.lin at linux.alibaba.com>
Date: Sun, 4 Aug 2024 23:37:02 +0800
Subject: [PATCH] [BOLT] Fix -print-relocations option

1. AArch64/X86 relocations are not sequentially numbered (more details on
llvm/include/llvm/BinaryFormat/ELFRelocs/AArch64.def), so directly
mapping the relocation type to its name can trigger a debug
assertion `assert(Type < ArrayRef(AArch64RelocNames).size())`.

2. Do not clear relocation lists in BF during disassembling, so we could
print out relocation infos with -print-disasm for debugging.
---
 bolt/lib/Core/BinaryFunction.cpp  |  3 +--
 bolt/lib/Core/Relocation.cpp      | 29 +++++++++++++++++------------
 bolt/test/X86/print-relocations.s | 23 +++++++++++++++++++++++
 3 files changed, 41 insertions(+), 14 deletions(-)
 create mode 100644 bolt/test/X86/print-relocations.s

diff --git a/bolt/lib/Core/BinaryFunction.cpp b/bolt/lib/Core/BinaryFunction.cpp
index 46bdf208be6ad3..a87ad9681005d4 100644
--- a/bolt/lib/Core/BinaryFunction.cpp
+++ b/bolt/lib/Core/BinaryFunction.cpp
@@ -1485,8 +1485,6 @@ Error BinaryFunction::disassemble() {
   if (uint64_t Offset = getFirstInstructionOffset())
     Labels[Offset] = BC.Ctx->createNamedTempSymbol();
 
-  clearList(Relocations);
-
   if (!IsSimple) {
     clearList(Instructions);
     return createNonFatalBOLTError("");
@@ -2277,6 +2275,7 @@ Error BinaryFunction::buildCFG(MCPlusBuilder::AllocatorIdTy AllocatorId) {
   clearList(Instructions);
   clearList(OffsetToCFI);
   clearList(TakenBranches);
+  clearList(Relocations);
 
   // Update the state.
   CurrentState = State::CFG;
diff --git a/bolt/lib/Core/Relocation.cpp b/bolt/lib/Core/Relocation.cpp
index 4e888a5b147aca..9c5b7ef923d4e6 100644
--- a/bolt/lib/Core/Relocation.cpp
+++ b/bolt/lib/Core/Relocation.cpp
@@ -1065,24 +1065,26 @@ MCBinaryExpr::Opcode Relocation::getComposeOpcodeFor(uint64_t Type) {
 }
 
 void Relocation::print(raw_ostream &OS) const {
+  // Relocations are not sequentially numbered so we cannot use an array
   switch (Arch) {
   default:
     OS << "RType:" << Twine::utohexstr(Type);
     break;
 
   case Triple::aarch64:
-    static const char *const AArch64RelocNames[] = {
-#define ELF_RELOC(name, value) #name,
+    switch (Type) {
+    default:
+      llvm_unreachable("illegal AArch64 relocation");
+#define ELF_RELOC(name, value)                                                 \
+  case value:                                                                  \
+    OS << #name;                                                               \
+    break;
 #include "llvm/BinaryFormat/ELFRelocs/AArch64.def"
 #undef ELF_RELOC
-    };
-    assert(Type < ArrayRef(AArch64RelocNames).size());
-    OS << AArch64RelocNames[Type];
+    }
     break;
 
   case Triple::riscv64:
-    // RISC-V relocations are not sequentially numbered so we cannot use an
-    // array
     switch (Type) {
     default:
       llvm_unreachable("illegal RISC-V relocation");
@@ -1096,13 +1098,16 @@ void Relocation::print(raw_ostream &OS) const {
     break;
 
   case Triple::x86_64:
-    static const char *const X86RelocNames[] = {
-#define ELF_RELOC(name, value) #name,
+    switch (Type) {
+    default:
+      llvm_unreachable("illegal X86-64 relocation");
+#define ELF_RELOC(name, value)                                                 \
+  case value:                                                                  \
+    OS << #name;                                                               \
+    break;
 #include "llvm/BinaryFormat/ELFRelocs/x86_64.def"
 #undef ELF_RELOC
-    };
-    assert(Type < ArrayRef(X86RelocNames).size());
-    OS << X86RelocNames[Type];
+    }
     break;
   }
   OS << ", 0x" << Twine::utohexstr(Offset);
diff --git a/bolt/test/X86/print-relocations.s b/bolt/test/X86/print-relocations.s
new file mode 100644
index 00000000000000..cd964c4f6387d0
--- /dev/null
+++ b/bolt/test/X86/print-relocations.s
@@ -0,0 +1,23 @@
+
+# This test is to check if -print-relocations option works correctly.
+
+# REQUIRES: system-linux
+
+# RUN: llvm-mc -filetype=obj -triple x86_64-unknown-unknown \
+# RUN:   %s -o %t.o
+# RUN: ld.lld -q %t.o -o %t.exe -q
+# RUN: llvm-bolt %t.exe -o %t.bolt.exe -print-only=_start \
+# RUN:   -print-disasm -print-relocations | FileCheck %s
+
+# CHECK: leaq    foo(%rip), %rax # Relocs: (R: R_X86_64_REX_GOTPCRELX
+  .globl _start
+  .type _start, %function
+_start:
+  movq 0(%rip), %rax
+  .reloc .-4, R_X86_64_REX_GOTPCRELX, foo-4
+  ret
+
+  .globl foo
+  .type foo, %function
+foo:
+  nop



More information about the llvm-commits mailing list