[llvm] [SPARC][IAS] Emit the correct 32-bit ELF machine type (PR #96583)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Jul 1 20:46:44 PDT 2024
https://github.com/koachan updated https://github.com/llvm/llvm-project/pull/96583
>From 607ed882cb1e0339606e5f31ef06d7bd6f65344b Mon Sep 17 00:00:00 2001
From: Koakuma <koachan at protonmail.com>
Date: Mon, 24 Jun 2024 21:07:14 +0700
Subject: [PATCH 1/4] [SPARC][IAS] Emit the correct 32-bit ELF machine type
Emit the correct machine type when writing out 32-bit ELF objects.
This patch is modeled on GCC's behavior:
- `-m32` emits an object of type EM_SPARC;
- `-m32 -mcpu=v9` emits EM_SPARC32PLUS (however, see below); and
- `-m64` emits EM_SPARCV9.
Note:
GNU as doesn't actually support user control of emitted machine type.
It will always autodetect the type based on the instruction mix:
- If there's a V9 instruction inside, then use EM_SPARC32PLUS; and
- Emit EM_SPARC otherwise.
GCC simply happens to try to emit V9 instructions whenever possible
with `-m32 -mcpu=v9`, so there's a high chance that the resulting
object file will be of a EM_SPARC32PLUS type, however it is not
a guaranteed behavior.
For this patch, I opt for deterministic behavior for simplicity.
---
.../Sparc/MCTargetDesc/SparcAsmBackend.cpp | 17 +++++++++++------
.../Sparc/MCTargetDesc/SparcELFObjectWriter.cpp | 14 ++++++++------
.../Sparc/MCTargetDesc/SparcMCTargetDesc.h | 4 ++--
3 files changed, 21 insertions(+), 14 deletions(-)
diff --git a/llvm/lib/Target/Sparc/MCTargetDesc/SparcAsmBackend.cpp b/llvm/lib/Target/Sparc/MCTargetDesc/SparcAsmBackend.cpp
index cb7414fddd29f..046f81363d294 100644
--- a/llvm/lib/Target/Sparc/MCTargetDesc/SparcAsmBackend.cpp
+++ b/llvm/lib/Target/Sparc/MCTargetDesc/SparcAsmBackend.cpp
@@ -132,14 +132,18 @@ namespace {
class SparcAsmBackend : public MCAsmBackend {
protected:
const Target &TheTarget;
+ const MCSubtargetInfo &TheSTI;
bool Is64Bit;
+ bool HasV9;
public:
- SparcAsmBackend(const Target &T)
+ SparcAsmBackend(const Target &T, const MCSubtargetInfo &STI)
: MCAsmBackend(StringRef(T.getName()) == "sparcel"
? llvm::endianness::little
: llvm::endianness::big),
- TheTarget(T), Is64Bit(StringRef(TheTarget.getName()) == "sparcv9") {}
+ TheTarget(T), TheSTI(STI),
+ Is64Bit(StringRef(TheTarget.getName()) == "sparcv9"),
+ HasV9(TheSTI.hasFeature(Sparc::FeatureV9)) {}
unsigned getNumFixupKinds() const override {
return Sparc::NumTargetFixupKinds;
@@ -340,8 +344,9 @@ namespace {
class ELFSparcAsmBackend : public SparcAsmBackend {
Triple::OSType OSType;
public:
- ELFSparcAsmBackend(const Target &T, Triple::OSType OSType) :
- SparcAsmBackend(T), OSType(OSType) { }
+ ELFSparcAsmBackend(const Target &T, const MCSubtargetInfo &STI,
+ Triple::OSType OSType)
+ : SparcAsmBackend(T, STI), OSType(OSType) {}
void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
const MCValue &Target, MutableArrayRef<char> Data,
@@ -368,7 +373,7 @@ namespace {
std::unique_ptr<MCObjectTargetWriter>
createObjectTargetWriter() const override {
uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(OSType);
- return createSparcELFObjectWriter(Is64Bit, OSABI);
+ return createSparcELFObjectWriter(Is64Bit, HasV9, OSABI);
}
};
@@ -378,5 +383,5 @@ MCAsmBackend *llvm::createSparcAsmBackend(const Target &T,
const MCSubtargetInfo &STI,
const MCRegisterInfo &MRI,
const MCTargetOptions &Options) {
- return new ELFSparcAsmBackend(T, STI.getTargetTriple().getOS());
+ return new ELFSparcAsmBackend(T, STI, STI.getTargetTriple().getOS());
}
diff --git a/llvm/lib/Target/Sparc/MCTargetDesc/SparcELFObjectWriter.cpp b/llvm/lib/Target/Sparc/MCTargetDesc/SparcELFObjectWriter.cpp
index f17d3e997452d..bfd71af736231 100644
--- a/llvm/lib/Target/Sparc/MCTargetDesc/SparcELFObjectWriter.cpp
+++ b/llvm/lib/Target/Sparc/MCTargetDesc/SparcELFObjectWriter.cpp
@@ -21,10 +21,12 @@ using namespace llvm;
namespace {
class SparcELFObjectWriter : public MCELFObjectTargetWriter {
public:
- SparcELFObjectWriter(bool Is64Bit, uint8_t OSABI)
- : MCELFObjectTargetWriter(Is64Bit, OSABI,
- Is64Bit ? ELF::EM_SPARCV9 : ELF::EM_SPARC,
- /*HasRelocationAddend*/ true) {}
+ SparcELFObjectWriter(bool Is64Bit, bool HasV9, uint8_t OSABI)
+ : MCELFObjectTargetWriter(
+ Is64Bit, OSABI,
+ Is64Bit ? ELF::EM_SPARCV9
+ : (HasV9 ? ELF::EM_SPARC32PLUS : ELF::EM_SPARC),
+ /*HasRelocationAddend*/ true) {}
~SparcELFObjectWriter() override = default;
@@ -146,6 +148,6 @@ bool SparcELFObjectWriter::needsRelocateWithSymbol(const MCValue &,
}
std::unique_ptr<MCObjectTargetWriter>
-llvm::createSparcELFObjectWriter(bool Is64Bit, uint8_t OSABI) {
- return std::make_unique<SparcELFObjectWriter>(Is64Bit, OSABI);
+llvm::createSparcELFObjectWriter(bool Is64Bit, bool HasV9, uint8_t OSABI) {
+ return std::make_unique<SparcELFObjectWriter>(Is64Bit, HasV9, OSABI);
}
diff --git a/llvm/lib/Target/Sparc/MCTargetDesc/SparcMCTargetDesc.h b/llvm/lib/Target/Sparc/MCTargetDesc/SparcMCTargetDesc.h
index a2a9f7474c3f9..63419663b722c 100644
--- a/llvm/lib/Target/Sparc/MCTargetDesc/SparcMCTargetDesc.h
+++ b/llvm/lib/Target/Sparc/MCTargetDesc/SparcMCTargetDesc.h
@@ -34,8 +34,8 @@ MCCodeEmitter *createSparcMCCodeEmitter(const MCInstrInfo &MCII,
MCAsmBackend *createSparcAsmBackend(const Target &T, const MCSubtargetInfo &STI,
const MCRegisterInfo &MRI,
const MCTargetOptions &Options);
-std::unique_ptr<MCObjectTargetWriter> createSparcELFObjectWriter(bool Is64Bit,
- uint8_t OSABI);
+std::unique_ptr<MCObjectTargetWriter>
+createSparcELFObjectWriter(bool Is64Bit, bool HasV9, uint8_t OSABI);
// Defines symbolic names for Sparc v9 ASI tag names.
namespace SparcASITag {
>From d2a1878338e39f443f6f39bdf3ffd986e5e74f0e Mon Sep 17 00:00:00 2001
From: Koakuma <koachan at protonmail.com>
Date: Tue, 2 Jul 2024 01:55:33 +0700
Subject: [PATCH 2/4] Add unit test
---
llvm/test/MC/Sparc/elf-sparc-machine-type.s | 12 ++++++++++++
1 file changed, 12 insertions(+)
create mode 100644 llvm/test/MC/Sparc/elf-sparc-machine-type.s
diff --git a/llvm/test/MC/Sparc/elf-sparc-machine-type.s b/llvm/test/MC/Sparc/elf-sparc-machine-type.s
new file mode 100644
index 0000000000000..fcba124ac0193
--- /dev/null
+++ b/llvm/test/MC/Sparc/elf-sparc-machine-type.s
@@ -0,0 +1,12 @@
+## Emit correct machine type depending on triple and cpu options.
+## - `-triple sparc` emits an object of type EM_SPARC;
+## - `-triple sparc -mcpu=v9` emits EM_SPARC32PLUS; and
+## - `-triple sparcv9` emits EM_SPARCV9.
+
+# RUN: llvm-mc -filetype=obj -triple sparc %s -o - | llvm-readobj -h - | FileCheck --check-prefixes=SPARC %s
+# RUN: llvm-mc -filetype=obj -triple sparc -mcpu=v9 %s -o - | llvm-readobj -h - | FileCheck --check-prefixes=SPARC32PLUS %s
+# RUN: llvm-mc -filetype=obj -triple sparcv9 %s -o - | llvm-readobj -h - | FileCheck --check-prefixes=SPARCV9 %s
+
+# SPARC: Machine: EM_SPARC (0x2)
+# SPARC32PLUS: Machine: EM_SPARC32PLUS(0x12)
+# SPARCV9: Machine: EM_SPARCV9 (0x2B)
>From f3c33c7236591d9784a541c80ffbc1c547a3a8d3 Mon Sep 17 00:00:00 2001
From: Koakuma <koachan at protonmail.com>
Date: Tue, 2 Jul 2024 02:01:14 +0700
Subject: [PATCH 3/4] Fix typo in test
---
llvm/test/MC/Sparc/elf-sparc-machine-type.s | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/test/MC/Sparc/elf-sparc-machine-type.s b/llvm/test/MC/Sparc/elf-sparc-machine-type.s
index fcba124ac0193..630812394560c 100644
--- a/llvm/test/MC/Sparc/elf-sparc-machine-type.s
+++ b/llvm/test/MC/Sparc/elf-sparc-machine-type.s
@@ -8,5 +8,5 @@
# RUN: llvm-mc -filetype=obj -triple sparcv9 %s -o - | llvm-readobj -h - | FileCheck --check-prefixes=SPARCV9 %s
# SPARC: Machine: EM_SPARC (0x2)
-# SPARC32PLUS: Machine: EM_SPARC32PLUS(0x12)
+# SPARC32PLUS: Machine: EM_SPARC32PLUS (0x12)
# SPARCV9: Machine: EM_SPARCV9 (0x2B)
>From 40930754c94363d8d725688026797dfad565957e Mon Sep 17 00:00:00 2001
From: Koakuma <koachan at protonmail.com>
Date: Tue, 2 Jul 2024 10:45:42 +0700
Subject: [PATCH 4/4] Remove unused members from SparcAsmBackend
---
llvm/lib/Target/Sparc/MCTargetDesc/SparcAsmBackend.cpp | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/llvm/lib/Target/Sparc/MCTargetDesc/SparcAsmBackend.cpp b/llvm/lib/Target/Sparc/MCTargetDesc/SparcAsmBackend.cpp
index 046f81363d294..0ddf354f8686c 100644
--- a/llvm/lib/Target/Sparc/MCTargetDesc/SparcAsmBackend.cpp
+++ b/llvm/lib/Target/Sparc/MCTargetDesc/SparcAsmBackend.cpp
@@ -131,8 +131,6 @@ static unsigned getFixupKindNumBytes(unsigned Kind) {
namespace {
class SparcAsmBackend : public MCAsmBackend {
protected:
- const Target &TheTarget;
- const MCSubtargetInfo &TheSTI;
bool Is64Bit;
bool HasV9;
@@ -141,9 +139,8 @@ namespace {
: MCAsmBackend(StringRef(T.getName()) == "sparcel"
? llvm::endianness::little
: llvm::endianness::big),
- TheTarget(T), TheSTI(STI),
- Is64Bit(StringRef(TheTarget.getName()) == "sparcv9"),
- HasV9(TheSTI.hasFeature(Sparc::FeatureV9)) {}
+ Is64Bit(StringRef(T.getName()) == "sparcv9"),
+ HasV9(STI.hasFeature(Sparc::FeatureV9)) {}
unsigned getNumFixupKinds() const override {
return Sparc::NumTargetFixupKinds;
More information about the llvm-commits
mailing list