Skip to content

Commit 05a49da

Browse files
authored
[llvm-jitlink] Use MachOObjectFile::getArchTriple for triple identifi… (#161799)
…cation. Replaces a call to ObjectFile::makeTriple (still used for ELF and COFF) with a call to MachOObjectFile::getArchTriple. The latter knows how to build correct triples for different MachO CPU subtypes, e.g. arm64 vs arm64e, which is important for selecting the right slice from universal archives.
1 parent e9972de commit 05a49da

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.section __DATA,__data
2+
.globl x
3+
.p2align 2, 0x0
4+
x:
5+
.long 0
6+
7+
.subsections_via_symbols
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.section __DATA,__data
2+
.globl x
3+
.p2align 2, 0x0
4+
x:
5+
.long 1
6+
7+
.subsections_via_symbols
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# RUN: rm -rf %t && mkdir -p %t
2+
# RUN: llvm-mc -triple=arm64e-apple-darwin -filetype=obj -o %t/main.o %s
3+
# RUN: llvm-mc -triple=arm64-apple-darwin -filetype=obj -o %t/x.arm64.o \
4+
# RUN: %S/Inputs/x-1.s
5+
# RUN: llvm-ar crs %t/libX.arm64.a %t/x.arm64.o
6+
# RUN: llvm-mc -triple=arm64e-apple-darwin -filetype=obj -o %t/x.arm64e.o \
7+
# RUN: %S/Inputs/x-0.s
8+
# RUN: llvm-ar crs %t/libX.arm64e.a %t/x.arm64e.o
9+
# RUN: llvm-lipo --create --output %t/libX.a %t/libX.arm64.a %t/libX.arm64e.a
10+
# RUN: llvm-jitlink -noexec -check=%s %t/main.o -L%t -lX
11+
#
12+
# Create a universal archive with two slices (arm64e, arm64) each containing
13+
# a definition of X: in arm64e X = 0, in arm64 X = 1.
14+
# Check that if we load an arm64e object file then we link the arm64e slice
15+
# of the archive by verifying that X = 0.
16+
#
17+
18+
# jitlink-check: *{4}x = 0
19+
20+
.section __TEXT,__text,regular,pure_instructions
21+
.globl _main
22+
.p2align 2
23+
_main:
24+
mov w0, #0
25+
ret
26+
27+
.section __DATA,__data
28+
.globl p
29+
p:
30+
.quad x
31+
32+
.subsections_via_symbols

llvm/tools/llvm-jitlink/llvm-jitlink.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1636,7 +1636,11 @@ static std::pair<Triple, SubtargetFeatures> getFirstFileTripleAndFeatures() {
16361636
case file_magic::macho_object: {
16371637
auto Obj = ExitOnErr(
16381638
object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef()));
1639-
Triple TT = Obj->makeTriple();
1639+
Triple TT;
1640+
if (auto *MachOObj = dyn_cast<object::MachOObjectFile>(Obj.get()))
1641+
TT = MachOObj->getArchTriple();
1642+
else
1643+
TT = Obj->makeTriple();
16401644
if (Magic == file_magic::coff_object) {
16411645
// TODO: Move this to makeTriple() if possible.
16421646
TT.setObjectFormat(Triple::COFF);

0 commit comments

Comments
 (0)