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
9 #include "MCTargetDesc/SparcFixupKinds.h"
10 #include "MCTargetDesc/SparcMCExpr.h"
11 #include "MCTargetDesc/SparcMCTargetDesc.h"
12 #include "llvm/ADT/STLExtras.h"
13 #include "llvm/MC/MCELFObjectWriter.h"
14 #include "llvm/MC/MCExpr.h"
15 #include "llvm/MC/MCObjectWriter.h"
16 #include "llvm/MC/MCValue.h"
17 #include "llvm/Support/ErrorHandling.h"
18
19 using namespace llvm;
20
21 namespace {
22 class SparcELFObjectWriter : public MCELFObjectTargetWriter {
23 public:
SparcELFObjectWriter(bool Is64Bit,uint8_t OSABI)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 MCValue &Val, const MCSymbol &Sym,
36 unsigned Type) const override;
37 };
38 }
39
getRelocType(MCContext & Ctx,const MCValue & Target,const MCFixup & Fixup,bool IsPCRel) const40 unsigned SparcELFObjectWriter::getRelocType(MCContext &Ctx,
41 const MCValue &Target,
42 const MCFixup &Fixup,
43 bool IsPCRel) const {
44 MCFixupKind Kind = Fixup.getKind();
45 if (Kind >= FirstLiteralRelocationKind)
46 return Kind - FirstLiteralRelocationKind;
47
48 if (const SparcMCExpr *SExpr = dyn_cast<SparcMCExpr>(Fixup.getValue())) {
49 if (SExpr->getKind() == SparcMCExpr::VK_Sparc_R_DISP32)
50 return ELF::R_SPARC_DISP32;
51 }
52
53 if (IsPCRel) {
54 switch(Fixup.getTargetKind()) {
55 default:
56 llvm_unreachable("Unimplemented fixup -> relocation");
57 case FK_Data_1: return ELF::R_SPARC_DISP8;
58 case FK_Data_2: return ELF::R_SPARC_DISP16;
59 case FK_Data_4: return ELF::R_SPARC_DISP32;
60 case FK_Data_8: return ELF::R_SPARC_DISP64;
61 case Sparc::fixup_sparc_call30: return ELF::R_SPARC_WDISP30;
62 case Sparc::fixup_sparc_br22: return ELF::R_SPARC_WDISP22;
63 case Sparc::fixup_sparc_br19: return ELF::R_SPARC_WDISP19;
64 case Sparc::fixup_sparc_br16:
65 return ELF::R_SPARC_WDISP16;
66 case Sparc::fixup_sparc_pc22: return ELF::R_SPARC_PC22;
67 case Sparc::fixup_sparc_pc10: return ELF::R_SPARC_PC10;
68 case Sparc::fixup_sparc_wplt30: return ELF::R_SPARC_WPLT30;
69 }
70 }
71
72 switch(Fixup.getTargetKind()) {
73 default:
74 llvm_unreachable("Unimplemented fixup -> relocation");
75 case FK_NONE: return ELF::R_SPARC_NONE;
76 case FK_Data_1: return ELF::R_SPARC_8;
77 case FK_Data_2: return ((Fixup.getOffset() % 2)
78 ? ELF::R_SPARC_UA16
79 : ELF::R_SPARC_16);
80 case FK_Data_4: return ((Fixup.getOffset() % 4)
81 ? ELF::R_SPARC_UA32
82 : ELF::R_SPARC_32);
83 case FK_Data_8: return ((Fixup.getOffset() % 8)
84 ? ELF::R_SPARC_UA64
85 : ELF::R_SPARC_64);
86 case Sparc::fixup_sparc_13: return ELF::R_SPARC_13;
87 case Sparc::fixup_sparc_hi22: return ELF::R_SPARC_HI22;
88 case Sparc::fixup_sparc_lo10: return ELF::R_SPARC_LO10;
89 case Sparc::fixup_sparc_h44: return ELF::R_SPARC_H44;
90 case Sparc::fixup_sparc_m44: return ELF::R_SPARC_M44;
91 case Sparc::fixup_sparc_l44: return ELF::R_SPARC_L44;
92 case Sparc::fixup_sparc_hh: return ELF::R_SPARC_HH22;
93 case Sparc::fixup_sparc_hm: return ELF::R_SPARC_HM10;
94 case Sparc::fixup_sparc_lm: return ELF::R_SPARC_LM22;
95 case Sparc::fixup_sparc_got22: return ELF::R_SPARC_GOT22;
96 case Sparc::fixup_sparc_got10: return ELF::R_SPARC_GOT10;
97 case Sparc::fixup_sparc_got13: return ELF::R_SPARC_GOT13;
98 case Sparc::fixup_sparc_tls_gd_hi22: return ELF::R_SPARC_TLS_GD_HI22;
99 case Sparc::fixup_sparc_tls_gd_lo10: return ELF::R_SPARC_TLS_GD_LO10;
100 case Sparc::fixup_sparc_tls_gd_add: return ELF::R_SPARC_TLS_GD_ADD;
101 case Sparc::fixup_sparc_tls_gd_call: return ELF::R_SPARC_TLS_GD_CALL;
102 case Sparc::fixup_sparc_tls_ldm_hi22: return ELF::R_SPARC_TLS_LDM_HI22;
103 case Sparc::fixup_sparc_tls_ldm_lo10: return ELF::R_SPARC_TLS_LDM_LO10;
104 case Sparc::fixup_sparc_tls_ldm_add: return ELF::R_SPARC_TLS_LDM_ADD;
105 case Sparc::fixup_sparc_tls_ldm_call: return ELF::R_SPARC_TLS_LDM_CALL;
106 case Sparc::fixup_sparc_tls_ldo_hix22: return ELF::R_SPARC_TLS_LDO_HIX22;
107 case Sparc::fixup_sparc_tls_ldo_lox10: return ELF::R_SPARC_TLS_LDO_LOX10;
108 case Sparc::fixup_sparc_tls_ldo_add: return ELF::R_SPARC_TLS_LDO_ADD;
109 case Sparc::fixup_sparc_tls_ie_hi22: return ELF::R_SPARC_TLS_IE_HI22;
110 case Sparc::fixup_sparc_tls_ie_lo10: return ELF::R_SPARC_TLS_IE_LO10;
111 case Sparc::fixup_sparc_tls_ie_ld: return ELF::R_SPARC_TLS_IE_LD;
112 case Sparc::fixup_sparc_tls_ie_ldx: return ELF::R_SPARC_TLS_IE_LDX;
113 case Sparc::fixup_sparc_tls_ie_add: return ELF::R_SPARC_TLS_IE_ADD;
114 case Sparc::fixup_sparc_tls_le_hix22: return ELF::R_SPARC_TLS_LE_HIX22;
115 case Sparc::fixup_sparc_tls_le_lox10: return ELF::R_SPARC_TLS_LE_LOX10;
116 case Sparc::fixup_sparc_hix22: return ELF::R_SPARC_HIX22;
117 case Sparc::fixup_sparc_lox10: return ELF::R_SPARC_LOX10;
118 case Sparc::fixup_sparc_gotdata_hix22: return ELF::R_SPARC_GOTDATA_HIX22;
119 case Sparc::fixup_sparc_gotdata_lox10: return ELF::R_SPARC_GOTDATA_LOX10;
120 case Sparc::fixup_sparc_gotdata_op: return ELF::R_SPARC_GOTDATA_OP;
121 }
122
123 return ELF::R_SPARC_NONE;
124 }
125
needsRelocateWithSymbol(const MCValue &,const MCSymbol &,unsigned Type) const126 bool SparcELFObjectWriter::needsRelocateWithSymbol(const MCValue &,
127 const MCSymbol &,
128 unsigned Type) const {
129 switch (Type) {
130 default:
131 return false;
132
133 // All relocations that use a GOT need a symbol, not an offset, as
134 // the offset of the symbol within the section is irrelevant to
135 // where the GOT entry is. Don't need to list all the TLS entries,
136 // as they're all marked as requiring a symbol anyways.
137 case ELF::R_SPARC_GOT10:
138 case ELF::R_SPARC_GOT13:
139 case ELF::R_SPARC_GOT22:
140 case ELF::R_SPARC_GOTDATA_HIX22:
141 case ELF::R_SPARC_GOTDATA_LOX10:
142 case ELF::R_SPARC_GOTDATA_OP_HIX22:
143 case ELF::R_SPARC_GOTDATA_OP_LOX10:
144 return true;
145 }
146 }
147
148 std::unique_ptr<MCObjectTargetWriter>
createSparcELFObjectWriter(bool Is64Bit,uint8_t OSABI)149 llvm::createSparcELFObjectWriter(bool Is64Bit, uint8_t OSABI) {
150 return std::make_unique<SparcELFObjectWriter>(Is64Bit, OSABI);
151 }
152