[llvm] [BOLT] Err when linking objects of different architectures (PR #66770)

Job Noorman via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 25 00:45:04 PDT 2023


https://github.com/mtvec updated https://github.com/llvm/llvm-project/pull/66770

>From f8b4bdb2c229f56a49e44904c430ac963977e6ea Mon Sep 17 00:00:00 2001
From: Job Noorman <jnoorman at igalia.com>
Date: Tue, 19 Sep 2023 14:39:38 +0200
Subject: [PATCH 1/3] [BOLT] Err when linking objects of different
 architectures

This could happen, for example, when instrumenting an AArch64 binary on
an x86 host because the instrumentation library is always built for the
host.

Note that this check will probably need to be refined in the future as
merely having the same architecture does not guarantee objects can be
linked. For example, on RISC-V, the float ABI of all objects should
match.
---
 bolt/lib/Rewrite/JITLinkLinker.cpp | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/bolt/lib/Rewrite/JITLinkLinker.cpp b/bolt/lib/Rewrite/JITLinkLinker.cpp
index ebbe2edd915fb6d..74f7967d6909ef2 100644
--- a/bolt/lib/Rewrite/JITLinkLinker.cpp
+++ b/bolt/lib/Rewrite/JITLinkLinker.cpp
@@ -179,6 +179,13 @@ void JITLinkLinker::loadObject(MemoryBufferRef Obj,
     exit(1);
   }
 
+  if ((*LG)->getTargetTriple().getArch() != BC.TheTriple->getArch()) {
+    errs() << "BOLT-ERROR: linking object withg arch "
+           << (*LG)->getTargetTriple().getArchName()
+           << " into context with arch " << BC.TheTriple->getArchName() << "\n";
+    exit(1);
+  }
+
   auto Ctx = std::make_unique<Context>(*this, MapSections);
   jitlink::link(std::move(*LG), std::move(Ctx));
 }

>From 67da4e5b8f9ac5a035658bb6649fe380377b21a3 Mon Sep 17 00:00:00 2001
From: Job Noorman <jnoorman at igalia.com>
Date: Tue, 19 Sep 2023 15:02:54 +0200
Subject: [PATCH 2/3] Fix typo

---
 bolt/lib/Rewrite/JITLinkLinker.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/bolt/lib/Rewrite/JITLinkLinker.cpp b/bolt/lib/Rewrite/JITLinkLinker.cpp
index 74f7967d6909ef2..5c7f3101a53eaa5 100644
--- a/bolt/lib/Rewrite/JITLinkLinker.cpp
+++ b/bolt/lib/Rewrite/JITLinkLinker.cpp
@@ -180,7 +180,7 @@ void JITLinkLinker::loadObject(MemoryBufferRef Obj,
   }
 
   if ((*LG)->getTargetTriple().getArch() != BC.TheTriple->getArch()) {
-    errs() << "BOLT-ERROR: linking object withg arch "
+    errs() << "BOLT-ERROR: linking object with arch "
            << (*LG)->getTargetTriple().getArchName()
            << " into context with arch " << BC.TheTriple->getArchName() << "\n";
     exit(1);

>From cfbb0e4294bbbe51871fed09d38a308b0a857f26 Mon Sep 17 00:00:00 2001
From: Job Noorman <jnoorman at igalia.com>
Date: Mon, 25 Sep 2023 09:44:42 +0200
Subject: [PATCH 3/3] Add test

---
 bolt/test/runtime/instrument-wrong-target.s | 26 +++++++++++++++++++++
 1 file changed, 26 insertions(+)
 create mode 100644 bolt/test/runtime/instrument-wrong-target.s

diff --git a/bolt/test/runtime/instrument-wrong-target.s b/bolt/test/runtime/instrument-wrong-target.s
new file mode 100644
index 000000000000000..31cae734a0bffd0
--- /dev/null
+++ b/bolt/test/runtime/instrument-wrong-target.s
@@ -0,0 +1,26 @@
+# Test that BOLT errs when trying to instrument a binary with a different
+# architecture than the one BOLT is built for.
+
+# REQUIRES: x86_64-linux,bolt-runtime,target=x86_64{{.*}}
+
+# RUN: llvm-mc -triple aarch64 -filetype=obj %s -o %t.o
+# RUN: ld.lld -q -pie -o %t.exe %t.o
+# RUN: not llvm-bolt --instrument -o %t.out %t.exe 2>&1 | FileCheck %s
+
+# CHECK: BOLT-ERROR: linking object with arch x86_64 into context with arch aarch64
+
+    .text
+    .globl _start
+    .type _start, %function
+_start:
+    # BOLT errs when instrumenting without relocations; create a dummy one.
+    .reloc 0, R_AARCH64_NONE
+    ret
+    .size _start, .-_start
+
+    .globl _fini
+    .type _fini, %function
+    # Force DT_FINI to be created (needed for instrumentation).
+_fini:
+    ret
+    .size _fini, .-_fini



More information about the llvm-commits mailing list