[llvm] a584b1a - [Sparc] Implement BFD_RELOC_NONE

Rainer Orth via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 28 01:45:28 PST 2022


Author: Rainer Orth
Date: 2022-01-28T10:44:22+01:00
New Revision: a584b1a4d17a834490d40ea12281d36c3cfb89c1

URL: https://github.com/llvm/llvm-project/commit/a584b1a4d17a834490d40ea12281d36c3cfb89c1
DIFF: https://github.com/llvm/llvm-project/commit/a584b1a4d17a834490d40ea12281d36c3cfb89c1.diff

LOG: [Sparc] Implement BFD_RELOC_NONE

`instrprof-icall-promo.test` `FAIL`s on Solaris/sparcv9:

  Profile-sparc :: instrprof-icall-promo.test
  Profile-sparcv9 :: instrprof-icall-promo.test

when compiling `compiler-rt/test/profile/Inputs/instrprof-icall-promo_2.cpp` with

  fatal error: error in backend: Relocation for CG Profile could not be created: unknown relocation name

This happens because the Sparc backend doesn't implement `BFD_RELOC_NONE`.
This patch fixes that, following what X86 does.

Tested on `sparcv9-sun-solaris2.11`.

Differential Revision: https://reviews.llvm.org/D118136

Added: 
    llvm/test/MC/Sparc/reloc-directive.s

Modified: 
    llvm/lib/Target/Sparc/MCTargetDesc/SparcAsmBackend.cpp
    llvm/lib/Target/Sparc/MCTargetDesc/SparcELFObjectWriter.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/Sparc/MCTargetDesc/SparcAsmBackend.cpp b/llvm/lib/Target/Sparc/MCTargetDesc/SparcAsmBackend.cpp
index e950f9582f099..4d69040a45081 100644
--- a/llvm/lib/Target/Sparc/MCTargetDesc/SparcAsmBackend.cpp
+++ b/llvm/lib/Target/Sparc/MCTargetDesc/SparcAsmBackend.cpp
@@ -8,6 +8,7 @@
 
 #include "MCTargetDesc/SparcFixupKinds.h"
 #include "MCTargetDesc/SparcMCTargetDesc.h"
+#include "llvm/ADT/StringSwitch.h"
 #include "llvm/MC/MCAsmBackend.h"
 #include "llvm/MC/MCELFObjectWriter.h"
 #include "llvm/MC/MCExpr.h"
@@ -131,6 +132,23 @@ namespace {
       return Sparc::NumTargetFixupKinds;
     }
 
+    Optional<MCFixupKind> getFixupKind(StringRef Name) const override {
+      unsigned Type;
+      Type = llvm::StringSwitch<unsigned>(Name)
+#define ELF_RELOC(X, Y) .Case(#X, Y)
+#include "llvm/BinaryFormat/ELFRelocs/Sparc.def"
+#undef ELF_RELOC
+                 .Case("BFD_RELOC_NONE", ELF::R_SPARC_NONE)
+                 .Case("BFD_RELOC_8", ELF::R_SPARC_8)
+                 .Case("BFD_RELOC_16", ELF::R_SPARC_16)
+                 .Case("BFD_RELOC_32", ELF::R_SPARC_32)
+                 .Case("BFD_RELOC_64", ELF::R_SPARC_64)
+                 .Default(-1u);
+      if (Type == -1u)
+        return None;
+      return static_cast<MCFixupKind>(FirstLiteralRelocationKind + Type);
+    }
+
     const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override {
       const static MCFixupKindInfo InfosBE[Sparc::NumTargetFixupKinds] = {
         // name                    offset bits  flags
@@ -216,6 +234,11 @@ namespace {
         { "fixup_sparc_tls_le_lox10",   0,  0,  0 }
       };
 
+      // Fixup kinds from .reloc directive are like R_SPARC_NONE. They do
+      // not require any extra processing.
+      if (Kind >= FirstLiteralRelocationKind)
+        return MCAsmBackend::getFixupKindInfo(FK_NONE);
+
       if (Kind < FirstTargetFixupKind)
         return MCAsmBackend::getFixupKindInfo(Kind);
 
@@ -229,6 +252,8 @@ namespace {
 
     bool shouldForceRelocation(const MCAssembler &Asm, const MCFixup &Fixup,
                                const MCValue &Target) override {
+      if (Fixup.getKind() >= FirstLiteralRelocationKind)
+        return true;
       switch ((Sparc::Fixups)Fixup.getKind()) {
       default:
         return false;
@@ -299,6 +324,8 @@ namespace {
                     uint64_t Value, bool IsResolved,
                     const MCSubtargetInfo *STI) const override {
 
+      if (Fixup.getKind() >= FirstLiteralRelocationKind)
+        return;
       Value = adjustFixupValue(Fixup.getKind(), Value);
       if (!Value) return;           // Doesn't change encoding.
 

diff  --git a/llvm/lib/Target/Sparc/MCTargetDesc/SparcELFObjectWriter.cpp b/llvm/lib/Target/Sparc/MCTargetDesc/SparcELFObjectWriter.cpp
index bc508b45c3bd7..02261dc5c4cde 100644
--- a/llvm/lib/Target/Sparc/MCTargetDesc/SparcELFObjectWriter.cpp
+++ b/llvm/lib/Target/Sparc/MCTargetDesc/SparcELFObjectWriter.cpp
@@ -42,6 +42,9 @@ unsigned SparcELFObjectWriter::getRelocType(MCContext &Ctx,
                                             const MCValue &Target,
                                             const MCFixup &Fixup,
                                             bool IsPCRel) const {
+  MCFixupKind Kind = Fixup.getKind();
+  if (Kind >= FirstLiteralRelocationKind)
+    return Kind - FirstLiteralRelocationKind;
 
   if (const SparcMCExpr *SExpr = dyn_cast<SparcMCExpr>(Fixup.getValue())) {
     if (SExpr->getKind() == SparcMCExpr::VK_Sparc_R_DISP32)
@@ -68,6 +71,7 @@ unsigned SparcELFObjectWriter::getRelocType(MCContext &Ctx,
   switch(Fixup.getTargetKind()) {
   default:
     llvm_unreachable("Unimplemented fixup -> relocation");
+  case FK_NONE:                  return ELF::R_SPARC_NONE;
   case FK_Data_1:                return ELF::R_SPARC_8;
   case FK_Data_2:                return ((Fixup.getOffset() % 2)
                                          ? ELF::R_SPARC_UA16

diff  --git a/llvm/test/MC/Sparc/reloc-directive.s b/llvm/test/MC/Sparc/reloc-directive.s
new file mode 100644
index 0000000000000..8899408ee428d
--- /dev/null
+++ b/llvm/test/MC/Sparc/reloc-directive.s
@@ -0,0 +1,46 @@
+# RUN: llvm-mc -triple=sparc %s | FileCheck --check-prefix=PRINT %s
+# RUN: llvm-mc -triple=sparcv9 %s | FileCheck --check-prefix=PRINT %s
+# RUN: llvm-mc -filetype=obj -triple=sparc %s | llvm-readobj -r - | FileCheck %s
+# RUN: llvm-mc -filetype=obj -triple=sparcv9 %s | llvm-readobj -r - | FileCheck %s
+
+# PRINT: .reloc 8, R_SPARC_NONE, .data
+# PRINT: .reloc 4, R_SPARC_NONE, foo+4
+# PRINT: .reloc 0, R_SPARC_NONE, 8
+# PRINT: .reloc 0, R_SPARC_32, .data+2
+# PRINT: .reloc 0, R_SPARC_UA16, foo+3
+# PRINT: .reloc 0, R_SPARC_DISP32, foo+5
+# PRINT:      .reloc 0, BFD_RELOC_NONE, 9
+# PRINT-NEXT: .reloc 0, BFD_RELOC_32, foo+2
+# PRINT-NEXT: .reloc 0, BFD_RELOC_64, foo+3
+
+# CHECK:      0x8 R_SPARC_NONE .data 0x0
+# CHECK-NEXT: 0x4 R_SPARC_NONE foo 0x4
+# CHECK-NEXT: 0x0 R_SPARC_NONE - 0x8
+# CHECK-NEXT: 0x0 R_SPARC_32 .data 0x2
+# CHECK-NEXT: 0x0 R_SPARC_UA16 foo 0x3
+# CHECK-NEXT: 0x0 R_SPARC_DISP32 foo 0x5
+# CHECK-NEXT: 0x0 R_SPARC_NONE - 0x9
+# CHECK-NEXT: 0x0 R_SPARC_32 foo 0x2
+# CHECK-NEXT: 0x0 R_SPARC_64 foo 0x3
+.text
+  ret
+  nop
+  nop
+  .reloc 8, R_SPARC_NONE, .data
+  .reloc 4, R_SPARC_NONE, foo+4
+  .reloc 0, R_SPARC_NONE, 8
+
+  .reloc 0, R_SPARC_32, .data+2
+  .reloc 0, R_SPARC_UA16, foo+3
+  .reloc 0, R_SPARC_DISP32, foo+5
+
+  .reloc 0, BFD_RELOC_NONE, 9
+  .reloc 0, BFD_RELOC_32, foo+2
+  .reloc 0, BFD_RELOC_64, foo+3
+
+.data
+.globl foo
+foo:
+  .word 0
+  .word 0
+  .word 0


        


More information about the llvm-commits mailing list