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/Support/ErrorHandling.h"
20 #include "llvm/Support/raw_ostream.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 MCSymbol
&Sym
,
42 unsigned Type
) const override
;
44 void addTargetSectionFlags(MCContext
&Ctx
, MCSectionELF
&Sec
) override
;
47 } // end anonymous namespace
49 ARMELFObjectWriter::ARMELFObjectWriter(uint8_t OSABI
)
50 : MCELFObjectTargetWriter(/*Is64Bit*/ false, OSABI
,
52 /*HasRelocationAddend*/ false) {}
54 bool ARMELFObjectWriter::needsRelocateWithSymbol(const MCSymbol
&Sym
,
55 unsigned Type
) const {
56 // FIXME: This is extremely conservative. This really needs to use a
57 // whitelist with a clear explanation for why each realocation needs to
58 // point to the symbol, not to the section.
63 case ELF::R_ARM_PREL31
:
64 case ELF::R_ARM_ABS32
:
69 // Need to examine the Fixup when determining whether to
70 // emit the relocation as an explicit symbol or as a section relative
72 unsigned ARMELFObjectWriter::getRelocType(MCContext
&Ctx
, const MCValue
&Target
,
75 return GetRelocTypeInner(Target
, Fixup
, IsPCRel
, Ctx
);
78 unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue
&Target
,
81 MCContext
&Ctx
) const {
82 MCSymbolRefExpr::VariantKind Modifier
= Target
.getAccessVariant();
85 switch ((unsigned)Fixup
.getKind()) {
87 Ctx
.reportFatalError(Fixup
.getLoc(), "unsupported relocation on symbol");
88 return ELF::R_ARM_NONE
;
92 llvm_unreachable("Unsupported Modifier");
93 case MCSymbolRefExpr::VK_None
:
94 return ELF::R_ARM_REL32
;
95 case MCSymbolRefExpr::VK_GOTTPOFF
:
96 return ELF::R_ARM_TLS_IE32
;
97 case MCSymbolRefExpr::VK_ARM_GOT_PREL
:
98 return ELF::R_ARM_GOT_PREL
;
99 case MCSymbolRefExpr::VK_ARM_PREL31
:
100 return ELF::R_ARM_PREL31
;
102 case ARM::fixup_arm_blx
:
103 case ARM::fixup_arm_uncondbl
:
105 case MCSymbolRefExpr::VK_PLT
:
106 return ELF::R_ARM_CALL
;
107 case MCSymbolRefExpr::VK_TLSCALL
:
108 return ELF::R_ARM_TLS_CALL
;
110 return ELF::R_ARM_CALL
;
112 case ARM::fixup_arm_condbl
:
113 case ARM::fixup_arm_condbranch
:
114 case ARM::fixup_arm_uncondbranch
:
115 return ELF::R_ARM_JUMP24
;
116 case ARM::fixup_t2_condbranch
:
117 return ELF::R_ARM_THM_JUMP19
;
118 case ARM::fixup_t2_uncondbranch
:
119 return ELF::R_ARM_THM_JUMP24
;
120 case ARM::fixup_arm_movt_hi16
:
121 return ELF::R_ARM_MOVT_PREL
;
122 case ARM::fixup_arm_movw_lo16
:
123 return ELF::R_ARM_MOVW_PREL_NC
;
124 case ARM::fixup_t2_movt_hi16
:
125 return ELF::R_ARM_THM_MOVT_PREL
;
126 case ARM::fixup_t2_movw_lo16
:
127 return ELF::R_ARM_THM_MOVW_PREL_NC
;
128 case ARM::fixup_arm_thumb_br
:
129 return ELF::R_ARM_THM_JUMP11
;
130 case ARM::fixup_arm_thumb_bcc
:
131 return ELF::R_ARM_THM_JUMP8
;
132 case ARM::fixup_arm_thumb_bl
:
133 case ARM::fixup_arm_thumb_blx
:
135 case MCSymbolRefExpr::VK_TLSCALL
:
136 return ELF::R_ARM_THM_TLS_CALL
;
138 return ELF::R_ARM_THM_CALL
;
142 switch ((unsigned)Fixup
.getKind()) {
144 Ctx
.reportFatalError(Fixup
.getLoc(), "unsupported relocation on symbol");
145 return ELF::R_ARM_NONE
;
149 llvm_unreachable("unsupported Modifier");
150 case MCSymbolRefExpr::VK_None
:
151 return ELF::R_ARM_ABS8
;
156 llvm_unreachable("unsupported modifier");
157 case MCSymbolRefExpr::VK_None
:
158 return ELF::R_ARM_ABS16
;
163 llvm_unreachable("Unsupported Modifier");
164 case MCSymbolRefExpr::VK_ARM_NONE
:
165 return ELF::R_ARM_NONE
;
166 case MCSymbolRefExpr::VK_GOT
:
167 return ELF::R_ARM_GOT_BREL
;
168 case MCSymbolRefExpr::VK_TLSGD
:
169 return ELF::R_ARM_TLS_GD32
;
170 case MCSymbolRefExpr::VK_TPOFF
:
171 return ELF::R_ARM_TLS_LE32
;
172 case MCSymbolRefExpr::VK_GOTTPOFF
:
173 return ELF::R_ARM_TLS_IE32
;
174 case MCSymbolRefExpr::VK_None
:
175 return ELF::R_ARM_ABS32
;
176 case MCSymbolRefExpr::VK_GOTOFF
:
177 return ELF::R_ARM_GOTOFF32
;
178 case MCSymbolRefExpr::VK_ARM_GOT_PREL
:
179 return ELF::R_ARM_GOT_PREL
;
180 case MCSymbolRefExpr::VK_ARM_TARGET1
:
181 return ELF::R_ARM_TARGET1
;
182 case MCSymbolRefExpr::VK_ARM_TARGET2
:
183 return ELF::R_ARM_TARGET2
;
184 case MCSymbolRefExpr::VK_ARM_PREL31
:
185 return ELF::R_ARM_PREL31
;
186 case MCSymbolRefExpr::VK_ARM_SBREL
:
187 return ELF::R_ARM_SBREL32
;
188 case MCSymbolRefExpr::VK_ARM_TLSLDO
:
189 return ELF::R_ARM_TLS_LDO32
;
190 case MCSymbolRefExpr::VK_TLSCALL
:
191 return ELF::R_ARM_TLS_CALL
;
192 case MCSymbolRefExpr::VK_TLSDESC
:
193 return ELF::R_ARM_TLS_GOTDESC
;
194 case MCSymbolRefExpr::VK_TLSLDM
:
195 return ELF::R_ARM_TLS_LDM32
;
196 case MCSymbolRefExpr::VK_ARM_TLSDESCSEQ
:
197 return ELF::R_ARM_TLS_DESCSEQ
;
199 case ARM::fixup_arm_condbranch
:
200 case ARM::fixup_arm_uncondbranch
:
201 return ELF::R_ARM_JUMP24
;
202 case ARM::fixup_arm_movt_hi16
:
205 llvm_unreachable("Unsupported Modifier");
206 case MCSymbolRefExpr::VK_None
:
207 return ELF::R_ARM_MOVT_ABS
;
208 case MCSymbolRefExpr::VK_ARM_SBREL
:
209 return ELF::R_ARM_MOVT_BREL
;
211 case ARM::fixup_arm_movw_lo16
:
214 llvm_unreachable("Unsupported Modifier");
215 case MCSymbolRefExpr::VK_None
:
216 return ELF::R_ARM_MOVW_ABS_NC
;
217 case MCSymbolRefExpr::VK_ARM_SBREL
:
218 return ELF::R_ARM_MOVW_BREL_NC
;
220 case ARM::fixup_t2_movt_hi16
:
223 llvm_unreachable("Unsupported Modifier");
224 case MCSymbolRefExpr::VK_None
:
225 return ELF::R_ARM_THM_MOVT_ABS
;
226 case MCSymbolRefExpr::VK_ARM_SBREL
:
227 return ELF::R_ARM_THM_MOVT_BREL
;
229 case ARM::fixup_t2_movw_lo16
:
232 llvm_unreachable("Unsupported Modifier");
233 case MCSymbolRefExpr::VK_None
:
234 return ELF::R_ARM_THM_MOVW_ABS_NC
;
235 case MCSymbolRefExpr::VK_ARM_SBREL
:
236 return ELF::R_ARM_THM_MOVW_BREL_NC
;
241 void ARMELFObjectWriter::addTargetSectionFlags(MCContext
&Ctx
,
243 // The mix of execute-only and non-execute-only at link time is
244 // non-execute-only. To avoid the empty implicitly created .text
245 // section from making the whole .text section non-execute-only, we
246 // mark it execute-only if it is empty and there is at least one
247 // execute-only section in the object.
248 MCSectionELF
*TextSection
=
249 static_cast<MCSectionELF
*>(Ctx
.getObjectFileInfo()->getTextSection());
250 if (Sec
.getKind().isExecuteOnly() && !TextSection
->hasInstructions() &&
251 !TextSection
->hasData()) {
252 TextSection
->setFlags(TextSection
->getFlags() | ELF::SHF_ARM_PURECODE
);
256 std::unique_ptr
<MCObjectTargetWriter
>
257 llvm::createARMELFObjectWriter(uint8_t OSABI
) {
258 return llvm::make_unique
<ARMELFObjectWriter
>(OSABI
);