1 //===-- ARMELFObjectWriter.cpp - ARM ELF Writer ---------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "MCTargetDesc/ARMFixupKinds.h"
10 #include "MCTargetDesc/ARMMCTargetDesc.h"
11 #include "llvm/BinaryFormat/ELF.h"
12 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCELFObjectWriter.h"
14 #include "llvm/MC/MCExpr.h"
15 #include "llvm/MC/MCFixup.h"
16 #include "llvm/MC/MCObjectFileInfo.h"
17 #include "llvm/MC/MCObjectWriter.h"
18 #include "llvm/MC/MCValue.h"
19 #include "llvm/Object/ELF.h"
20 #include "llvm/Support/ErrorHandling.h"
27 class ARMELFObjectWriter
: public MCELFObjectTargetWriter
{
28 enum { DefaultEABIVersion
= 0x05000000U
};
30 unsigned GetRelocTypeInner(const MCValue
&Target
, const MCFixup
&Fixup
,
31 bool IsPCRel
, MCContext
&Ctx
) const;
34 ARMELFObjectWriter(uint8_t OSABI
);
36 ~ARMELFObjectWriter() override
= default;
38 unsigned getRelocType(MCContext
&Ctx
, const MCValue
&Target
,
39 const MCFixup
&Fixup
, bool IsPCRel
) const override
;
41 bool needsRelocateWithSymbol(const MCValue
&Val
, const MCSymbol
&Sym
,
42 unsigned Type
) const override
;
45 } // end anonymous namespace
47 ARMELFObjectWriter::ARMELFObjectWriter(uint8_t OSABI
)
48 : MCELFObjectTargetWriter(/*Is64Bit*/ false, OSABI
,
50 /*HasRelocationAddend*/ false) {}
52 bool ARMELFObjectWriter::needsRelocateWithSymbol(const MCValue
&,
54 unsigned Type
) const {
55 // FIXME: This is extremely conservative. This really needs to use an
56 // explicit list with a clear explanation for why each realocation needs to
57 // point to the symbol, not to the section.
62 case ELF::R_ARM_PREL31
:
63 case ELF::R_ARM_ABS32
:
68 // Need to examine the Fixup when determining whether to
69 // emit the relocation as an explicit symbol or as a section relative
71 unsigned ARMELFObjectWriter::getRelocType(MCContext
&Ctx
, const MCValue
&Target
,
74 return GetRelocTypeInner(Target
, Fixup
, IsPCRel
, Ctx
);
77 unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue
&Target
,
80 MCContext
&Ctx
) const {
81 unsigned Kind
= Fixup
.getTargetKind();
82 if (Kind
>= FirstLiteralRelocationKind
)
83 return Kind
- FirstLiteralRelocationKind
;
84 MCSymbolRefExpr::VariantKind Modifier
= Target
.getAccessVariant();
85 auto CheckFDPIC
= [&](uint32_t Type
) {
86 if (getOSABI() != ELF::ELFOSABI_ARM_FDPIC
)
87 Ctx
.reportError(Fixup
.getLoc(),
89 object::getELFRelocationTypeName(ELF::EM_ARM
, Type
) +
90 " only supported in FDPIC mode");
95 switch (Fixup
.getTargetKind()) {
97 Ctx
.reportError(Fixup
.getLoc(), "unsupported relocation type");
98 return ELF::R_ARM_NONE
;
102 Ctx
.reportError(Fixup
.getLoc(),
103 "invalid fixup for 4-byte pc-relative data relocation");
104 return ELF::R_ARM_NONE
;
105 case MCSymbolRefExpr::VK_None
: {
106 if (const MCSymbolRefExpr
*SymRef
= Target
.getSymA()) {
107 // For GNU AS compatibility expressions such as
108 // _GLOBAL_OFFSET_TABLE_ - label emit a R_ARM_BASE_PREL relocation.
109 if (SymRef
->getSymbol().getName() == "_GLOBAL_OFFSET_TABLE_")
110 return ELF::R_ARM_BASE_PREL
;
112 return ELF::R_ARM_REL32
;
114 case MCSymbolRefExpr::VK_GOTTPOFF
:
115 return ELF::R_ARM_TLS_IE32
;
116 case MCSymbolRefExpr::VK_ARM_GOT_PREL
:
117 return ELF::R_ARM_GOT_PREL
;
118 case MCSymbolRefExpr::VK_ARM_PREL31
:
119 return ELF::R_ARM_PREL31
;
121 case ARM::fixup_arm_blx
:
122 case ARM::fixup_arm_uncondbl
:
124 case MCSymbolRefExpr::VK_PLT
:
125 return ELF::R_ARM_CALL
;
126 case MCSymbolRefExpr::VK_TLSCALL
:
127 return ELF::R_ARM_TLS_CALL
;
129 return ELF::R_ARM_CALL
;
131 case ARM::fixup_arm_condbl
:
132 case ARM::fixup_arm_condbranch
:
133 case ARM::fixup_arm_uncondbranch
:
134 return ELF::R_ARM_JUMP24
;
135 case ARM::fixup_t2_condbranch
:
136 return ELF::R_ARM_THM_JUMP19
;
137 case ARM::fixup_t2_uncondbranch
:
138 return ELF::R_ARM_THM_JUMP24
;
139 case ARM::fixup_arm_movt_hi16
:
140 return ELF::R_ARM_MOVT_PREL
;
141 case ARM::fixup_arm_movw_lo16
:
142 return ELF::R_ARM_MOVW_PREL_NC
;
143 case ARM::fixup_t2_movt_hi16
:
144 return ELF::R_ARM_THM_MOVT_PREL
;
145 case ARM::fixup_t2_movw_lo16
:
146 return ELF::R_ARM_THM_MOVW_PREL_NC
;
147 case ARM::fixup_arm_thumb_upper_8_15
:
148 return ELF::R_ARM_THM_ALU_ABS_G3
;
149 case ARM::fixup_arm_thumb_upper_0_7
:
150 return ELF::R_ARM_THM_ALU_ABS_G2_NC
;
151 case ARM::fixup_arm_thumb_lower_8_15
:
152 return ELF::R_ARM_THM_ALU_ABS_G1_NC
;
153 case ARM::fixup_arm_thumb_lower_0_7
:
154 return ELF::R_ARM_THM_ALU_ABS_G0_NC
;
155 case ARM::fixup_arm_thumb_br
:
156 return ELF::R_ARM_THM_JUMP11
;
157 case ARM::fixup_arm_thumb_bcc
:
158 return ELF::R_ARM_THM_JUMP8
;
159 case ARM::fixup_arm_thumb_bl
:
160 case ARM::fixup_arm_thumb_blx
:
162 case MCSymbolRefExpr::VK_TLSCALL
:
163 return ELF::R_ARM_THM_TLS_CALL
;
165 return ELF::R_ARM_THM_CALL
;
167 case ARM::fixup_arm_ldst_pcrel_12
:
168 return ELF::R_ARM_LDR_PC_G0
;
169 case ARM::fixup_arm_pcrel_10_unscaled
:
170 return ELF::R_ARM_LDRS_PC_G0
;
171 case ARM::fixup_t2_ldst_pcrel_12
:
172 return ELF::R_ARM_THM_PC12
;
173 case ARM::fixup_arm_adr_pcrel_12
:
174 return ELF::R_ARM_ALU_PC_G0
;
175 case ARM::fixup_thumb_adr_pcrel_10
:
176 return ELF::R_ARM_THM_PC8
;
177 case ARM::fixup_t2_adr_pcrel_12
:
178 return ELF::R_ARM_THM_ALU_PREL_11_0
;
179 case ARM::fixup_bf_target
:
180 return ELF::R_ARM_THM_BF16
;
181 case ARM::fixup_bfc_target
:
182 return ELF::R_ARM_THM_BF12
;
183 case ARM::fixup_bfl_target
:
184 return ELF::R_ARM_THM_BF18
;
189 Ctx
.reportError(Fixup
.getLoc(), "unsupported relocation type");
190 return ELF::R_ARM_NONE
;
194 Ctx
.reportError(Fixup
.getLoc(),
195 "invalid fixup for 1-byte data relocation");
196 return ELF::R_ARM_NONE
;
197 case MCSymbolRefExpr::VK_None
:
198 return ELF::R_ARM_ABS8
;
203 Ctx
.reportError(Fixup
.getLoc(),
204 "invalid fixup for 2-byte data relocation");
205 return ELF::R_ARM_NONE
;
206 case MCSymbolRefExpr::VK_None
:
207 return ELF::R_ARM_ABS16
;
212 Ctx
.reportError(Fixup
.getLoc(),
213 "invalid fixup for 4-byte data relocation");
214 return ELF::R_ARM_NONE
;
215 case MCSymbolRefExpr::VK_ARM_NONE
:
216 return ELF::R_ARM_NONE
;
217 case MCSymbolRefExpr::VK_GOT
:
218 return ELF::R_ARM_GOT_BREL
;
219 case MCSymbolRefExpr::VK_TLSGD
:
220 return ELF::R_ARM_TLS_GD32
;
221 case MCSymbolRefExpr::VK_TPOFF
:
222 return ELF::R_ARM_TLS_LE32
;
223 case MCSymbolRefExpr::VK_GOTTPOFF
:
224 return ELF::R_ARM_TLS_IE32
;
225 case MCSymbolRefExpr::VK_None
:
226 return ELF::R_ARM_ABS32
;
227 case MCSymbolRefExpr::VK_GOTOFF
:
228 return ELF::R_ARM_GOTOFF32
;
229 case MCSymbolRefExpr::VK_ARM_GOT_PREL
:
230 return ELF::R_ARM_GOT_PREL
;
231 case MCSymbolRefExpr::VK_ARM_TARGET1
:
232 return ELF::R_ARM_TARGET1
;
233 case MCSymbolRefExpr::VK_ARM_TARGET2
:
234 return ELF::R_ARM_TARGET2
;
235 case MCSymbolRefExpr::VK_ARM_PREL31
:
236 return ELF::R_ARM_PREL31
;
237 case MCSymbolRefExpr::VK_ARM_SBREL
:
238 return ELF::R_ARM_SBREL32
;
239 case MCSymbolRefExpr::VK_ARM_TLSLDO
:
240 return ELF::R_ARM_TLS_LDO32
;
241 case MCSymbolRefExpr::VK_TLSCALL
:
242 return ELF::R_ARM_TLS_CALL
;
243 case MCSymbolRefExpr::VK_TLSDESC
:
244 return ELF::R_ARM_TLS_GOTDESC
;
245 case MCSymbolRefExpr::VK_TLSLDM
:
246 return ELF::R_ARM_TLS_LDM32
;
247 case MCSymbolRefExpr::VK_ARM_TLSDESCSEQ
:
248 return ELF::R_ARM_TLS_DESCSEQ
;
249 case MCSymbolRefExpr::VK_FUNCDESC
:
250 return CheckFDPIC(ELF::R_ARM_FUNCDESC
);
251 case MCSymbolRefExpr::VK_GOTFUNCDESC
:
252 return CheckFDPIC(ELF::R_ARM_GOTFUNCDESC
);
253 case MCSymbolRefExpr::VK_GOTOFFFUNCDESC
:
254 return CheckFDPIC(ELF::R_ARM_GOTOFFFUNCDESC
);
255 case MCSymbolRefExpr::VK_TLSGD_FDPIC
:
256 return CheckFDPIC(ELF::R_ARM_TLS_GD32_FDPIC
);
257 case MCSymbolRefExpr::VK_TLSLDM_FDPIC
:
258 return CheckFDPIC(ELF::R_ARM_TLS_LDM32_FDPIC
);
259 case MCSymbolRefExpr::VK_GOTTPOFF_FDPIC
:
260 return CheckFDPIC(ELF::R_ARM_TLS_IE32_FDPIC
);
262 case ARM::fixup_arm_condbranch
:
263 case ARM::fixup_arm_uncondbranch
:
264 return ELF::R_ARM_JUMP24
;
265 case ARM::fixup_arm_movt_hi16
:
268 Ctx
.reportError(Fixup
.getLoc(), "invalid fixup for ARM MOVT instruction");
269 return ELF::R_ARM_NONE
;
270 case MCSymbolRefExpr::VK_None
:
271 return ELF::R_ARM_MOVT_ABS
;
272 case MCSymbolRefExpr::VK_ARM_SBREL
:
273 return ELF::R_ARM_MOVT_BREL
;
275 case ARM::fixup_arm_movw_lo16
:
278 Ctx
.reportError(Fixup
.getLoc(), "invalid fixup for ARM MOVW instruction");
279 return ELF::R_ARM_NONE
;
280 case MCSymbolRefExpr::VK_None
:
281 return ELF::R_ARM_MOVW_ABS_NC
;
282 case MCSymbolRefExpr::VK_ARM_SBREL
:
283 return ELF::R_ARM_MOVW_BREL_NC
;
285 case ARM::fixup_t2_movt_hi16
:
288 Ctx
.reportError(Fixup
.getLoc(),
289 "invalid fixup for Thumb MOVT instruction");
290 return ELF::R_ARM_NONE
;
291 case MCSymbolRefExpr::VK_None
:
292 return ELF::R_ARM_THM_MOVT_ABS
;
293 case MCSymbolRefExpr::VK_ARM_SBREL
:
294 return ELF::R_ARM_THM_MOVT_BREL
;
296 case ARM::fixup_t2_movw_lo16
:
299 Ctx
.reportError(Fixup
.getLoc(),
300 "invalid fixup for Thumb MOVW instruction");
301 return ELF::R_ARM_NONE
;
302 case MCSymbolRefExpr::VK_None
:
303 return ELF::R_ARM_THM_MOVW_ABS_NC
;
304 case MCSymbolRefExpr::VK_ARM_SBREL
:
305 return ELF::R_ARM_THM_MOVW_BREL_NC
;
308 case ARM::fixup_arm_thumb_upper_8_15
:
309 return ELF::R_ARM_THM_ALU_ABS_G3
;
310 case ARM::fixup_arm_thumb_upper_0_7
:
311 return ELF::R_ARM_THM_ALU_ABS_G2_NC
;
312 case ARM::fixup_arm_thumb_lower_8_15
:
313 return ELF::R_ARM_THM_ALU_ABS_G1_NC
;
314 case ARM::fixup_arm_thumb_lower_0_7
:
315 return ELF::R_ARM_THM_ALU_ABS_G0_NC
;
319 std::unique_ptr
<MCObjectTargetWriter
>
320 llvm::createARMELFObjectWriter(uint8_t OSABI
) {
321 return std::make_unique
<ARMELFObjectWriter
>(OSABI
);