[llvm] [SPARC][IAS] Emit the correct 32-bit ELF machine type (PR #96583)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 24 18:57:12 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-sparc
Author: Koakuma (koachan)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/96583.diff
3 Files Affected:
- (modified) llvm/lib/Target/Sparc/MCTargetDesc/SparcAsmBackend.cpp (+11-6)
- (modified) llvm/lib/Target/Sparc/MCTargetDesc/SparcELFObjectWriter.cpp (+8-6)
- (modified) llvm/lib/Target/Sparc/MCTargetDesc/SparcMCTargetDesc.h (+2-2)
``````````diff
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 {
``````````
</details>
https://github.com/llvm/llvm-project/pull/96583
More information about the llvm-commits
mailing list