LLVM 24.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/MC/MCContext.h"
14#include "llvm/MC/MCExpr.h"
17#include "llvm/MC/MCValue.h"
19
20using namespace llvm;
21
22namespace {
23 class SparcELFObjectWriter : public MCELFObjectTargetWriter {
24 public:
25 SparcELFObjectWriter(bool Is64Bit, bool IsV8Plus, uint8_t OSABI)
26 : MCELFObjectTargetWriter(
27 Is64Bit, OSABI,
28 Is64Bit ? ELF::EM_SPARCV9
29 : (IsV8Plus ? ELF::EM_SPARC32PLUS : ELF::EM_SPARC),
30 /*HasRelocationAddend*/ true) {}
31
32 ~SparcELFObjectWriter() override = default;
33
34 protected:
35 unsigned getRelocType(const MCFixup &Fixup, const MCValue &Target,
36 bool IsPCRel) const override;
37
38 bool needsRelocateWithSymbol(const MCValue &, unsigned Type) const override;
39 };
40}
41
42unsigned SparcELFObjectWriter::getRelocType(const MCFixup &Fixup,
43 const MCValue &Target,
44 bool IsPCRel) const {
45 auto Kind = Fixup.getKind();
46 auto Spec = Target.getSpecifier();
47 switch (Spec) {
48 case ELF::R_SPARC_TLS_GD_HI22:
49 case ELF::R_SPARC_TLS_GD_LO10:
50 case ELF::R_SPARC_TLS_GD_ADD:
51 case ELF::R_SPARC_TLS_LDM_HI22:
52 case ELF::R_SPARC_TLS_LDM_LO10:
53 case ELF::R_SPARC_TLS_LDM_ADD:
54 case ELF::R_SPARC_TLS_LDO_HIX22:
55 case ELF::R_SPARC_TLS_LDO_LOX10:
56 case ELF::R_SPARC_TLS_LDO_ADD:
57 case ELF::R_SPARC_TLS_IE_HI22:
58 case ELF::R_SPARC_TLS_IE_LO10:
59 case ELF::R_SPARC_TLS_IE_LD:
60 case ELF::R_SPARC_TLS_IE_LDX:
61 case ELF::R_SPARC_TLS_IE_ADD:
62 case ELF::R_SPARC_TLS_LE_HIX22:
63 case ELF::R_SPARC_TLS_LE_LOX10:
64 if (auto *SA = const_cast<MCSymbol *>(Target.getAddSym()))
65 static_cast<MCSymbolELF *>(SA)->setType(ELF::STT_TLS);
66 break;
67 case ELF::R_SPARC_DISP8:
68 case ELF::R_SPARC_DISP16:
69 case ELF::R_SPARC_DISP32:
70 case ELF::R_SPARC_DISP64:
71 case ELF::R_SPARC_PLT32:
72 case ELF::R_SPARC_PLT64:
73 case ELF::R_SPARC_TLS_DTPOFF32:
74 case ELF::R_SPARC_TLS_DTPOFF64: {
75 // Data relocation specifiers name the size of the data directive.
76 MCFixupKind DataKind;
77 StringRef Directive;
78 switch (Spec) {
79 case ELF::R_SPARC_DISP8:
80 DataKind = FK_Data_1;
81 Directive = ".byte";
82 break;
83 case ELF::R_SPARC_DISP16:
84 DataKind = FK_Data_2;
85 Directive = ".half";
86 break;
87 case ELF::R_SPARC_DISP32:
88 case ELF::R_SPARC_PLT32:
89 case ELF::R_SPARC_TLS_DTPOFF32:
90 DataKind = FK_Data_4;
91 Directive = ".word";
92 break;
93 case ELF::R_SPARC_DISP64:
94 case ELF::R_SPARC_PLT64:
95 case ELF::R_SPARC_TLS_DTPOFF64:
96 DataKind = FK_Data_8;
97 Directive = ".xword";
98 break;
99 default:
100 llvm_unreachable("unexpected data relocation specifier");
101 }
102 if (Kind != DataKind) {
103 reportError(Fixup.getLoc(), "%" + Sparc::getSpecifierName(Spec) +
104 " can only be used in a " + Directive +
105 " directive");
106 return ELF::R_SPARC_NONE;
107 }
108 if (Spec == ELF::R_SPARC_TLS_DTPOFF32 || Spec == ELF::R_SPARC_TLS_DTPOFF64)
109 if (auto *SA = const_cast<MCSymbol *>(Target.getAddSym()))
110 static_cast<MCSymbolELF *>(SA)->setType(ELF::STT_TLS);
111 return Spec;
112 }
113 default:
114 break;
115 }
116
117 // Extract the relocation type from the fixup kind, after applying STT_TLS as
118 // needed.
119 if (mc::isRelocation(Fixup.getKind()))
120 return Kind;
121
122 if (IsPCRel) {
123 switch (Kind) {
124 default:
125 llvm_unreachable("Unimplemented fixup -> relocation");
126 case FK_Data_1: return ELF::R_SPARC_DISP8;
127 case FK_Data_2: return ELF::R_SPARC_DISP16;
128 case FK_Data_4: return ELF::R_SPARC_DISP32;
129 case FK_Data_8: return ELF::R_SPARC_DISP64;
131 if (getContext().getObjectFileInfo()->isPositionIndependent())
132 return ELF::R_SPARC_WPLT30;
133 return ELF::R_SPARC_WDISP30;
134 }
135 }
136
137 // clang-format off
138 switch(Fixup.getKind()) {
139 default:
140 llvm_unreachable("Unimplemented fixup -> relocation");
141 case FK_NONE: return ELF::R_SPARC_NONE;
142 case FK_Data_1: return ELF::R_SPARC_8;
143 case FK_Data_2: return ((Fixup.getOffset() % 2)
144 ? ELF::R_SPARC_UA16
145 : ELF::R_SPARC_16);
146 case FK_Data_4: return ((Fixup.getOffset() % 4)
147 ? ELF::R_SPARC_UA32
148 : ELF::R_SPARC_32);
149 case FK_Data_8: return ((Fixup.getOffset() % 8)
150 ? ELF::R_SPARC_UA64
151 : ELF::R_SPARC_64);
153 if (getContext().getObjectFileInfo()->isPositionIndependent())
154 return ELF::R_SPARC_GOT13;
155 return ELF::R_SPARC_13;
156 }
157 // clang-format on
158
159 return ELF::R_SPARC_NONE;
160}
161
162bool SparcELFObjectWriter::needsRelocateWithSymbol(const MCValue &,
163 unsigned Type) const {
164 switch (Type) {
165 default:
166 return false;
167
168 // All relocations that use a GOT need a symbol, not an offset, as
169 // the offset of the symbol within the section is irrelevant to
170 // where the GOT entry is. Don't need to list all the TLS entries,
171 // as they're all marked as requiring a symbol anyways.
172 case ELF::R_SPARC_GOT10:
173 case ELF::R_SPARC_GOT13:
174 case ELF::R_SPARC_GOT22:
175 case ELF::R_SPARC_GOTDATA_OP_HIX22:
176 case ELF::R_SPARC_GOTDATA_OP_LOX10:
177 case ELF::R_SPARC_GOTDATA_OP:
178 return true;
179 }
180}
181
182std::unique_ptr<MCObjectTargetWriter>
183llvm::createSparcELFObjectWriter(bool Is64Bit, bool IsV8Plus, uint8_t OSABI) {
184 return std::make_unique<SparcELFObjectWriter>(Is64Bit, IsV8Plus, OSABI);
185}
basic Basic Alias true
static Error reportError(StringRef Message)
Func getContext().diagnose(DiagnosticInfoUnsupported(Func
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition MCFixup.h:65
Target - Wrapper for Target specific information.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ STT_TLS
Definition ELF.h:1278
@ fixup_sparc_13
fixup_sparc_13 - 13-bit fixup
StringRef getSpecifierName(uint16_t S)
bool isRelocation(MCFixupKind FixupKind)
Definition MCFixup.h:134
This is an optimization pass for GlobalISel generic memory operations.
uint16_t MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition MCFixup.h:22
std::unique_ptr< MCObjectTargetWriter > createSparcELFObjectWriter(bool Is64Bit, bool IsV8Plus, uint8_t OSABI)
@ FK_Data_8
A eight-byte fixup.
Definition MCFixup.h:41
@ FK_Data_1
A one-byte fixup.
Definition MCFixup.h:38
@ FK_Data_4
A four-byte fixup.
Definition MCFixup.h:40
@ FK_NONE
A no-op fixup.
Definition MCFixup.h:37
@ FK_Data_2
A two-byte fixup.
Definition MCFixup.h:39