1 //===-- ARMELFObjectWriter.cpp - ARM ELF Writer ---------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "MCTargetDesc/ARMMCTargetDesc.h"
11 #include "MCTargetDesc/ARMFixupKinds.h"
12 #include "llvm/ADT/Statistic.h"
13 #include "llvm/ADT/StringSwitch.h"
14 #include "llvm/MC/MCELFObjectWriter.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCSectionELF.h"
17 #include "llvm/MC/MCValue.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/raw_ostream.h"
25 class ARMELFObjectWriter : public MCELFObjectTargetWriter {
26 enum { DefaultEABIVersion = 0x05000000U };
27 unsigned GetRelocTypeInner(const MCValue &Target,
33 ARMELFObjectWriter(uint8_t OSABI);
35 ~ARMELFObjectWriter() override;
37 unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
38 bool IsPCRel) const override;
40 bool needsRelocateWithSymbol(const MCSymbol &Sym,
41 unsigned Type) const override;
45 ARMELFObjectWriter::ARMELFObjectWriter(uint8_t OSABI)
46 : MCELFObjectTargetWriter(/*Is64Bit*/ false, OSABI,
48 /*HasRelocationAddend*/ false) {}
50 ARMELFObjectWriter::~ARMELFObjectWriter() {}
52 bool ARMELFObjectWriter::needsRelocateWithSymbol(const MCSymbol &Sym,
53 unsigned Type) const {
54 // FIXME: This is extremely conservative. This really needs to use a
55 // whitelist with a clear explanation for why each realocation needs to
56 // point to the symbol, not to the section.
61 case ELF::R_ARM_PREL31:
62 case ELF::R_ARM_ABS32:
67 // Need to examine the Fixup when determining whether to
68 // emit the relocation as an explicit symbol or as a section relative
70 unsigned ARMELFObjectWriter::GetRelocType(const MCValue &Target,
73 return GetRelocTypeInner(Target, Fixup, IsPCRel);
76 unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
79 MCSymbolRefExpr::VariantKind Modifier = Target.getAccessVariant();
83 switch ((unsigned)Fixup.getKind()) {
85 report_fatal_error("unsupported relocation on symbol");
86 return ELF::R_ARM_NONE;
89 default: llvm_unreachable("Unsupported Modifier");
90 case MCSymbolRefExpr::VK_None:
91 Type = ELF::R_ARM_REL32;
93 case MCSymbolRefExpr::VK_TLSGD:
94 llvm_unreachable("unimplemented");
95 case MCSymbolRefExpr::VK_GOTTPOFF:
96 Type = ELF::R_ARM_TLS_IE32;
98 case MCSymbolRefExpr::VK_GOTPCREL:
99 Type = ELF::R_ARM_GOT_PREL;
103 case ARM::fixup_arm_blx:
104 case ARM::fixup_arm_uncondbl:
106 case MCSymbolRefExpr::VK_PLT:
107 Type = ELF::R_ARM_CALL;
109 case MCSymbolRefExpr::VK_ARM_TLSCALL:
110 Type = ELF::R_ARM_TLS_CALL;
113 Type = ELF::R_ARM_CALL;
117 case ARM::fixup_arm_condbl:
118 case ARM::fixup_arm_condbranch:
119 case ARM::fixup_arm_uncondbranch:
120 Type = ELF::R_ARM_JUMP24;
122 case ARM::fixup_t2_condbranch:
123 case ARM::fixup_t2_uncondbranch:
124 Type = ELF::R_ARM_THM_JUMP24;
126 case ARM::fixup_arm_movt_hi16:
127 Type = ELF::R_ARM_MOVT_PREL;
129 case ARM::fixup_arm_movw_lo16:
130 Type = ELF::R_ARM_MOVW_PREL_NC;
132 case ARM::fixup_t2_movt_hi16:
133 Type = ELF::R_ARM_THM_MOVT_PREL;
135 case ARM::fixup_t2_movw_lo16:
136 Type = ELF::R_ARM_THM_MOVW_PREL_NC;
138 case ARM::fixup_arm_thumb_bl:
139 case ARM::fixup_arm_thumb_blx:
141 case MCSymbolRefExpr::VK_ARM_TLSCALL:
142 Type = ELF::R_ARM_THM_TLS_CALL;
145 Type = ELF::R_ARM_THM_CALL;
151 switch ((unsigned)Fixup.getKind()) {
153 report_fatal_error("unsupported relocation on symbol");
154 return ELF::R_ARM_NONE;
157 default: llvm_unreachable("unsupported Modifier");
158 case MCSymbolRefExpr::VK_None:
159 Type = ELF::R_ARM_ABS8;
165 default: llvm_unreachable("unsupported modifier");
166 case MCSymbolRefExpr::VK_None:
167 Type = ELF::R_ARM_ABS16;
173 default: llvm_unreachable("Unsupported Modifier");
174 case MCSymbolRefExpr::VK_ARM_NONE:
175 Type = ELF::R_ARM_NONE;
177 case MCSymbolRefExpr::VK_GOT:
178 Type = ELF::R_ARM_GOT_BREL;
180 case MCSymbolRefExpr::VK_TLSGD:
181 Type = ELF::R_ARM_TLS_GD32;
183 case MCSymbolRefExpr::VK_TPOFF:
184 Type = ELF::R_ARM_TLS_LE32;
186 case MCSymbolRefExpr::VK_GOTTPOFF:
187 Type = ELF::R_ARM_TLS_IE32;
189 case MCSymbolRefExpr::VK_None:
190 Type = ELF::R_ARM_ABS32;
192 case MCSymbolRefExpr::VK_GOTOFF:
193 Type = ELF::R_ARM_GOTOFF32;
195 case MCSymbolRefExpr::VK_GOTPCREL:
196 Type = ELF::R_ARM_GOT_PREL;
198 case MCSymbolRefExpr::VK_ARM_TARGET1:
199 Type = ELF::R_ARM_TARGET1;
201 case MCSymbolRefExpr::VK_ARM_TARGET2:
202 Type = ELF::R_ARM_TARGET2;
204 case MCSymbolRefExpr::VK_ARM_PREL31:
205 Type = ELF::R_ARM_PREL31;
207 case MCSymbolRefExpr::VK_ARM_SBREL:
208 Type = ELF::R_ARM_SBREL32;
210 case MCSymbolRefExpr::VK_ARM_TLSLDO:
211 Type = ELF::R_ARM_TLS_LDO32;
213 case MCSymbolRefExpr::VK_ARM_TLSCALL:
214 Type = ELF::R_ARM_TLS_CALL;
216 case MCSymbolRefExpr::VK_ARM_TLSDESC:
217 Type = ELF::R_ARM_TLS_GOTDESC;
219 case MCSymbolRefExpr::VK_ARM_TLSDESCSEQ:
220 Type = ELF::R_ARM_TLS_DESCSEQ;
224 case ARM::fixup_arm_ldst_pcrel_12:
225 case ARM::fixup_arm_pcrel_10:
226 case ARM::fixup_arm_adr_pcrel_12:
227 case ARM::fixup_arm_thumb_bl:
228 case ARM::fixup_arm_thumb_cb:
229 case ARM::fixup_arm_thumb_cp:
230 case ARM::fixup_arm_thumb_br:
231 llvm_unreachable("Unimplemented");
232 case ARM::fixup_arm_condbranch:
233 case ARM::fixup_arm_uncondbranch:
234 Type = ELF::R_ARM_JUMP24;
236 case ARM::fixup_arm_movt_hi16:
237 Type = ELF::R_ARM_MOVT_ABS;
239 case ARM::fixup_arm_movw_lo16:
240 Type = ELF::R_ARM_MOVW_ABS_NC;
242 case ARM::fixup_t2_movt_hi16:
243 Type = ELF::R_ARM_THM_MOVT_ABS;
245 case ARM::fixup_t2_movw_lo16:
246 Type = ELF::R_ARM_THM_MOVW_ABS_NC;
254 MCObjectWriter *llvm::createARMELFObjectWriter(raw_pwrite_stream &OS,
256 bool IsLittleEndian) {
257 MCELFObjectTargetWriter *MOTW = new ARMELFObjectWriter(OSABI);
258 return createELFObjectWriter(MOTW, OS, IsLittleEndian);