[llvm] [RISC-V][Mach-O] Implement and select the RISCVMachOTargetObjectFile. (PR #174677)

Francesco Petrogalli via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 6 17:10:00 PST 2026


https://github.com/fpetrogalli created https://github.com/llvm/llvm-project/pull/174677

The RISCVTargetMachine was still selecting RISCVELFTargetObjectFile, which was making llc crash when running the test at llvm/test/CodeGen/RISCV/riscv-macho.ll

>From ca81fd6ea73cdb891a03cdf540fa55e3e6ae4d06 Mon Sep 17 00:00:00 2001
From: Francesco Petrogalli <francesco.petrogalli at apple.com>
Date: Tue, 6 Jan 2026 17:05:44 -0800
Subject: [PATCH] [RISC-V][Mach-O] Implement and select the
 RISCVMachOTargetObjectFile.

The RISCVTargetMachine was still selecting RISCVELFTargetObjectFile,
which was making llc crash when running the test at
llvm/test/CodeGen/RISCV/riscv-macho.ll
---
 llvm/lib/Target/RISCV/RISCVTargetMachine.cpp    | 8 +++++++-
 llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp | 4 ++++
 llvm/lib/Target/RISCV/RISCVTargetObjectFile.h   | 5 +++++
 3 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp b/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
index 52dc38564059c..8a23da070adbf 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
+++ b/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
@@ -152,6 +152,12 @@ static Reloc::Model getEffectiveRelocModel(std::optional<Reloc::Model> RM) {
   return RM.value_or(Reloc::Static);
 }
 
+static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
+  if (TT.isOSBinFormatMachO())
+    return std::make_unique<RISCVMachOTargetObjectFile>();
+  return std::make_unique<RISCVELFTargetObjectFile>();
+}
+
 RISCVTargetMachine::RISCVTargetMachine(const Target &T, const Triple &TT,
                                        StringRef CPU, StringRef FS,
                                        const TargetOptions &Options,
@@ -162,7 +168,7 @@ RISCVTargetMachine::RISCVTargetMachine(const Target &T, const Triple &TT,
           T, TT.computeDataLayout(Options.MCOptions.getABIName()), TT, CPU, FS,
           Options, getEffectiveRelocModel(RM),
           getEffectiveCodeModel(CM, CodeModel::Small), OL),
-      TLOF(std::make_unique<RISCVELFTargetObjectFile>()) {
+      TLOF(createTLOF(TT)) {
   initAsmInfo();
 
   // RISC-V supports the MachineOutliner.
diff --git a/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp b/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp
index bc90cf8f53aba..e1d02c902537f 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp
+++ b/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp
@@ -179,3 +179,7 @@ MCSection *RISCVELFTargetObjectFile::getSectionForConstant(
   return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C,
                                                             Alignment);
 }
+
+RISCVMachOTargetObjectFile::RISCVMachOTargetObjectFile() {
+  SupportGOTPCRelWithOffset = false;
+}
diff --git a/llvm/lib/Target/RISCV/RISCVTargetObjectFile.h b/llvm/lib/Target/RISCV/RISCVTargetObjectFile.h
index ff7e3e4c752c3..c23314add88b5 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetObjectFile.h
+++ b/llvm/lib/Target/RISCV/RISCVTargetObjectFile.h
@@ -55,6 +55,11 @@ class RISCVELFTargetObjectFile : public TargetLoweringObjectFileELF {
                                           MCStreamer &Streamer) const override;
 };
 
+class RISCVMachOTargetObjectFile : public TargetLoweringObjectFileMachO {
+public:
+  RISCVMachOTargetObjectFile();
+};
+
 } // end namespace llvm
 
 #endif



More information about the llvm-commits mailing list