LLVM  16.0.0git
SparcELFObjectWriter.cpp
Go to the documentation of this file.
1 //===-- SparcELFObjectWriter.cpp - Sparc ELF Writer -----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
12 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/MC/MCExpr.h"
15 #include "llvm/MC/MCObjectWriter.h"
16 #include "llvm/MC/MCValue.h"
18 
19 using namespace llvm;
20 
21 namespace {
22  class SparcELFObjectWriter : public MCELFObjectTargetWriter {
23  public:
24  SparcELFObjectWriter(bool Is64Bit, uint8_t OSABI)
25  : MCELFObjectTargetWriter(Is64Bit, OSABI,
26  Is64Bit ? ELF::EM_SPARCV9 : ELF::EM_SPARC,
27  /*HasRelocationAddend*/ true) {}
28 
29  ~SparcELFObjectWriter() override = default;
30 
31  protected:
32  unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
33  const MCFixup &Fixup, bool IsPCRel) const override;
34 
35  bool needsRelocateWithSymbol(const MCSymbol &Sym,
36  unsigned Type) const override;
37 
38  };
39 }
40 
41 unsigned SparcELFObjectWriter::getRelocType(MCContext &Ctx,
42  const MCValue &Target,
43  const MCFixup &Fixup,
44  bool IsPCRel) const {
45  MCFixupKind Kind = Fixup.getKind();
48 
49  if (const SparcMCExpr *SExpr = dyn_cast<SparcMCExpr>(Fixup.getValue())) {
50  if (SExpr->getKind() == SparcMCExpr::VK_Sparc_R_DISP32)
51  return ELF::R_SPARC_DISP32;
52  }
53 
54  if (IsPCRel) {
55  switch(Fixup.getTargetKind()) {
56  default:
57  llvm_unreachable("Unimplemented fixup -> relocation");
58  case FK_Data_1: return ELF::R_SPARC_DISP8;
59  case FK_Data_2: return ELF::R_SPARC_DISP16;
60  case FK_Data_4: return ELF::R_SPARC_DISP32;
61  case FK_Data_8: return ELF::R_SPARC_DISP64;
62  case Sparc::fixup_sparc_call30: return ELF::R_SPARC_WDISP30;
63  case Sparc::fixup_sparc_br22: return ELF::R_SPARC_WDISP22;
64  case Sparc::fixup_sparc_br19: return ELF::R_SPARC_WDISP19;
65  case Sparc::fixup_sparc_pc22: return ELF::R_SPARC_PC22;
66  case Sparc::fixup_sparc_pc10: return ELF::R_SPARC_PC10;
67  case Sparc::fixup_sparc_wplt30: return ELF::R_SPARC_WPLT30;
68  }
69  }
70 
71  switch(Fixup.getTargetKind()) {
72  default:
73  llvm_unreachable("Unimplemented fixup -> relocation");
74  case FK_NONE: return ELF::R_SPARC_NONE;
75  case FK_Data_1: return ELF::R_SPARC_8;
76  case FK_Data_2: return ((Fixup.getOffset() % 2)
77  ? ELF::R_SPARC_UA16
78  : ELF::R_SPARC_16);
79  case FK_Data_4: return ((Fixup.getOffset() % 4)
80  ? ELF::R_SPARC_UA32
81  : ELF::R_SPARC_32);
82  case FK_Data_8: return ((Fixup.getOffset() % 8)
83  ? ELF::R_SPARC_UA64
84  : ELF::R_SPARC_64);
85  case Sparc::fixup_sparc_13: return ELF::R_SPARC_13;
86  case Sparc::fixup_sparc_hi22: return ELF::R_SPARC_HI22;
87  case Sparc::fixup_sparc_lo10: return ELF::R_SPARC_LO10;
88  case Sparc::fixup_sparc_h44: return ELF::R_SPARC_H44;
89  case Sparc::fixup_sparc_m44: return ELF::R_SPARC_M44;
90  case Sparc::fixup_sparc_l44: return ELF::R_SPARC_L44;
91  case Sparc::fixup_sparc_hh: return ELF::R_SPARC_HH22;
92  case Sparc::fixup_sparc_hm: return ELF::R_SPARC_HM10;
93  case Sparc::fixup_sparc_lm: return ELF::R_SPARC_LM22;
94  case Sparc::fixup_sparc_got22: return ELF::R_SPARC_GOT22;
95  case Sparc::fixup_sparc_got10: return ELF::R_SPARC_GOT10;
96  case Sparc::fixup_sparc_got13: return ELF::R_SPARC_GOT13;
97  case Sparc::fixup_sparc_tls_gd_hi22: return ELF::R_SPARC_TLS_GD_HI22;
98  case Sparc::fixup_sparc_tls_gd_lo10: return ELF::R_SPARC_TLS_GD_LO10;
99  case Sparc::fixup_sparc_tls_gd_add: return ELF::R_SPARC_TLS_GD_ADD;
100  case Sparc::fixup_sparc_tls_gd_call: return ELF::R_SPARC_TLS_GD_CALL;
101  case Sparc::fixup_sparc_tls_ldm_hi22: return ELF::R_SPARC_TLS_LDM_HI22;
102  case Sparc::fixup_sparc_tls_ldm_lo10: return ELF::R_SPARC_TLS_LDM_LO10;
103  case Sparc::fixup_sparc_tls_ldm_add: return ELF::R_SPARC_TLS_LDM_ADD;
104  case Sparc::fixup_sparc_tls_ldm_call: return ELF::R_SPARC_TLS_LDM_CALL;
105  case Sparc::fixup_sparc_tls_ldo_hix22: return ELF::R_SPARC_TLS_LDO_HIX22;
106  case Sparc::fixup_sparc_tls_ldo_lox10: return ELF::R_SPARC_TLS_LDO_LOX10;
107  case Sparc::fixup_sparc_tls_ldo_add: return ELF::R_SPARC_TLS_LDO_ADD;
108  case Sparc::fixup_sparc_tls_ie_hi22: return ELF::R_SPARC_TLS_IE_HI22;
109  case Sparc::fixup_sparc_tls_ie_lo10: return ELF::R_SPARC_TLS_IE_LO10;
110  case Sparc::fixup_sparc_tls_ie_ld: return ELF::R_SPARC_TLS_IE_LD;
111  case Sparc::fixup_sparc_tls_ie_ldx: return ELF::R_SPARC_TLS_IE_LDX;
112  case Sparc::fixup_sparc_tls_ie_add: return ELF::R_SPARC_TLS_IE_ADD;
113  case Sparc::fixup_sparc_tls_le_hix22: return ELF::R_SPARC_TLS_LE_HIX22;
114  case Sparc::fixup_sparc_tls_le_lox10: return ELF::R_SPARC_TLS_LE_LOX10;
115  case Sparc::fixup_sparc_hix22: return ELF::R_SPARC_HIX22;
116  case Sparc::fixup_sparc_lox10: return ELF::R_SPARC_LOX10;
117  case Sparc::fixup_sparc_gotdata_hix22: return ELF::R_SPARC_GOTDATA_HIX22;
118  case Sparc::fixup_sparc_gotdata_lox10: return ELF::R_SPARC_GOTDATA_LOX10;
119  case Sparc::fixup_sparc_gotdata_op: return ELF::R_SPARC_GOTDATA_OP;
120  }
121 
122  return ELF::R_SPARC_NONE;
123 }
124 
125 bool SparcELFObjectWriter::needsRelocateWithSymbol(const MCSymbol &Sym,
126  unsigned Type) const {
127  switch (Type) {
128  default:
129  return false;
130 
131  // All relocations that use a GOT need a symbol, not an offset, as
132  // the offset of the symbol within the section is irrelevant to
133  // where the GOT entry is. Don't need to list all the TLS entries,
134  // as they're all marked as requiring a symbol anyways.
135  case ELF::R_SPARC_GOT10:
136  case ELF::R_SPARC_GOT13:
137  case ELF::R_SPARC_GOT22:
138  case ELF::R_SPARC_GOTDATA_HIX22:
139  case ELF::R_SPARC_GOTDATA_LOX10:
140  case ELF::R_SPARC_GOTDATA_OP_HIX22:
141  case ELF::R_SPARC_GOTDATA_OP_LOX10:
142  return true;
143  }
144 }
145 
146 std::unique_ptr<MCObjectTargetWriter>
147 llvm::createSparcELFObjectWriter(bool Is64Bit, uint8_t OSABI) {
148  return std::make_unique<SparcELFObjectWriter>(Is64Bit, OSABI);
149 }
llvm::Sparc::fixup_sparc_br22
@ fixup_sparc_br22
fixup_sparc_br22 - 22-bit PC relative relocation for branches
Definition: SparcFixupKinds.h:22
llvm::Sparc::fixup_sparc_br19
@ fixup_sparc_br19
fixup_sparc_br19 - 19-bit PC relative relocation for branches on icc/xcc
Definition: SparcFixupKinds.h:26
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::MCSymbol
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:76
llvm::createSparcELFObjectWriter
std::unique_ptr< MCObjectTargetWriter > createSparcELFObjectWriter(bool Is64Bit, uint8_t OSABI)
Definition: SparcELFObjectWriter.cpp:147
SparcMCTargetDesc.h
llvm::Sparc::fixup_sparc_wplt30
@ fixup_sparc_wplt30
fixup_sparc_wplt30
Definition: SparcFixupKinds.h:76
llvm::Target
Target - Wrapper for Target specific information.
Definition: TargetRegistry.h:145
ErrorHandling.h
llvm::Sparc::fixup_sparc_tls_ie_add
@ fixup_sparc_tls_ie_add
Definition: SparcFixupKinds.h:94
llvm::Sparc::fixup_sparc_tls_gd_add
@ fixup_sparc_tls_gd_add
Definition: SparcFixupKinds.h:81
true
basic Basic Alias true
Definition: BasicAliasAnalysis.cpp:1875
llvm::Sparc::fixup_sparc_tls_gd_hi22
@ fixup_sparc_tls_gd_hi22
fixups for Thread Local Storage
Definition: SparcFixupKinds.h:79
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::ELF::EM_SPARCV9
@ EM_SPARCV9
Definition: ELF.h:159
llvm::SparcMCExpr
Definition: SparcMCExpr.h:23
STLExtras.h
llvm::Sparc::fixup_sparc_tls_ie_hi22
@ fixup_sparc_tls_ie_hi22
Definition: SparcFixupKinds.h:90
llvm::Sparc::fixup_sparc_hi22
@ fixup_sparc_hi22
fixup_sparc_hi22 - 22-bit fixup corresponding to hi(foo) for sethi
Definition: SparcFixupKinds.h:37
llvm::FirstLiteralRelocationKind
@ FirstLiteralRelocationKind
The range [FirstLiteralRelocationKind, MaxTargetFixupKind) is used for relocations coming from ....
Definition: MCFixup.h:50
llvm::Sparc::fixup_sparc_lox10
@ fixup_sparc_lox10
13-bit fixup corresponding to lox(foo)
Definition: SparcFixupKinds.h:101
llvm::FK_Data_4
@ FK_Data_4
A four-byte fixup.
Definition: MCFixup.h:25
llvm::SparcMCExpr::VK_Sparc_R_DISP32
@ VK_Sparc_R_DISP32
Definition: SparcMCExpr.h:43
llvm::objcopy::FileFormat::ELF
@ ELF
SparcFixupKinds.h
llvm::Sparc::fixup_sparc_h44
@ fixup_sparc_h44
fixup_sparc_h44 - 22-bit fixup corresponding to h44(foo)
Definition: SparcFixupKinds.h:43
SparcMCExpr.h
llvm::Sparc::fixup_sparc_pc22
@ fixup_sparc_pc22
fixup_sparc_pc22 - 22-bit fixup corresponding to pc22(foo)
Definition: SparcFixupKinds.h:61
llvm::MCELFObjectTargetWriter
Definition: MCELFObjectWriter.h:53
llvm::Sparc::fixup_sparc_tls_ldo_lox10
@ fixup_sparc_tls_ldo_lox10
Definition: SparcFixupKinds.h:88
llvm::Sparc::fixup_sparc_tls_ie_ldx
@ fixup_sparc_tls_ie_ldx
Definition: SparcFixupKinds.h:93
llvm::Sparc::fixup_sparc_gotdata_op
@ fixup_sparc_gotdata_op
32-bit fixup corresponding to gdop(foo)
Definition: SparcFixupKinds.h:108
llvm::Sparc::fixup_sparc_tls_ie_ld
@ fixup_sparc_tls_ie_ld
Definition: SparcFixupKinds.h:92
llvm::Sparc::fixup_sparc_tls_ie_lo10
@ fixup_sparc_tls_ie_lo10
Definition: SparcFixupKinds.h:91
llvm::Sparc::fixup_sparc_13
@ fixup_sparc_13
fixup_sparc_13 - 13-bit fixup
Definition: SparcFixupKinds.h:33
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
llvm::Sparc::fixup_sparc_tls_ldm_call
@ fixup_sparc_tls_ldm_call
Definition: SparcFixupKinds.h:86
MCELFObjectWriter.h
llvm::Sparc::fixup_sparc_tls_ldo_add
@ fixup_sparc_tls_ldo_add
Definition: SparcFixupKinds.h:89
llvm::Sparc::fixup_sparc_tls_gd_call
@ fixup_sparc_tls_gd_call
Definition: SparcFixupKinds.h:82
llvm::Sparc::fixup_sparc_gotdata_hix22
@ fixup_sparc_gotdata_hix22
22-bit fixup corresponding to gdop_hix22(foo)
Definition: SparcFixupKinds.h:104
llvm::Sparc::fixup_sparc_tls_ldm_lo10
@ fixup_sparc_tls_ldm_lo10
Definition: SparcFixupKinds.h:84
llvm::FK_Data_1
@ FK_Data_1
A one-byte fixup.
Definition: MCFixup.h:23
llvm::FK_NONE
@ FK_NONE
A no-op fixup.
Definition: MCFixup.h:22
llvm::Sparc::fixup_sparc_tls_le_lox10
@ fixup_sparc_tls_le_lox10
Definition: SparcFixupKinds.h:96
llvm::Sparc::fixup_sparc_got13
@ fixup_sparc_got13
fixup_sparc_got13 - 13-bit fixup corresponding to got13(foo)
Definition: SparcFixupKinds.h:73
Fixup
PowerPC TLS Dynamic Call Fixup
Definition: PPCTLSDynamicCall.cpp:215
llvm::Sparc::fixup_sparc_pc10
@ fixup_sparc_pc10
fixup_sparc_pc10 - 10-bit fixup corresponding to pc10(foo)
Definition: SparcFixupKinds.h:64
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:143
llvm::Sparc::fixup_sparc_tls_gd_lo10
@ fixup_sparc_tls_gd_lo10
Definition: SparcFixupKinds.h:80
llvm::Sparc::fixup_sparc_l44
@ fixup_sparc_l44
fixup_sparc_l44 - 12-bit fixup corresponding to l44(foo)
Definition: SparcFixupKinds.h:49
llvm::Sparc::fixup_sparc_tls_le_hix22
@ fixup_sparc_tls_le_hix22
Definition: SparcFixupKinds.h:95
MCObjectWriter.h
llvm::Sparc::fixup_sparc_m44
@ fixup_sparc_m44
fixup_sparc_m44 - 10-bit fixup corresponding to m44(foo)
Definition: SparcFixupKinds.h:46
llvm::Sparc::fixup_sparc_tls_ldo_hix22
@ fixup_sparc_tls_ldo_hix22
Definition: SparcFixupKinds.h:87
llvm::Sparc::fixup_sparc_call30
@ fixup_sparc_call30
Definition: SparcFixupKinds.h:18
llvm::Sparc::fixup_sparc_lo10
@ fixup_sparc_lo10
fixup_sparc_lo10 - 10-bit fixup corresponding to lo(foo)
Definition: SparcFixupKinds.h:40
llvm::Sparc::fixup_sparc_hh
@ fixup_sparc_hh
fixup_sparc_hh - 22-bit fixup corresponding to hh(foo)
Definition: SparcFixupKinds.h:52
MCValue.h
llvm::ELF::EM_SPARC
@ EM_SPARC
Definition: ELF.h:135
llvm::Sparc::fixup_sparc_got22
@ fixup_sparc_got22
fixup_sparc_got22 - 22-bit fixup corresponding to got22(foo)
Definition: SparcFixupKinds.h:67
llvm::MCFixupKind
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:21
llvm::Sparc::fixup_sparc_hix22
@ fixup_sparc_hix22
22-bit fixup corresponding to hix(foo)
Definition: SparcFixupKinds.h:99
llvm::FK_Data_8
@ FK_Data_8
A eight-byte fixup.
Definition: MCFixup.h:26
llvm::Sparc::fixup_sparc_gotdata_lox10
@ fixup_sparc_gotdata_lox10
13-bit fixup corresponding to gdop_lox10(foo)
Definition: SparcFixupKinds.h:106
llvm::MCValue
This represents an "assembler immediate".
Definition: MCValue.h:36
llvm::Sparc::fixup_sparc_tls_ldm_hi22
@ fixup_sparc_tls_ldm_hi22
Definition: SparcFixupKinds.h:83
llvm::FK_Data_2
@ FK_Data_2
A two-byte fixup.
Definition: MCFixup.h:24
llvm::Sparc::fixup_sparc_got10
@ fixup_sparc_got10
fixup_sparc_got10 - 10-bit fixup corresponding to got10(foo)
Definition: SparcFixupKinds.h:70
MCExpr.h
llvm::MCFixup
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
llvm::Sparc::fixup_sparc_lm
@ fixup_sparc_lm
fixup_sparc_lm - 22-bit fixup corresponding to lm(foo)
Definition: SparcFixupKinds.h:58
llvm::Sparc::fixup_sparc_tls_ldm_add
@ fixup_sparc_tls_ldm_add
Definition: SparcFixupKinds.h:85
llvm::Sparc::fixup_sparc_hm
@ fixup_sparc_hm
fixup_sparc_hm - 10-bit fixup corresponding to hm(foo)
Definition: SparcFixupKinds.h:55