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 virtual ~ARMELFObjectWriter();
37 unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
38 bool IsPCRel) const override;
40 bool needsRelocateWithSymbol(const MCSymbolData &SD,
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 MCSymbolData &SD,
53 unsigned Type) const {
54 // FIXME: This is extremelly 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()) {
84 default: llvm_unreachable("Unimplemented");
87 default: llvm_unreachable("Unsupported Modifier");
88 case MCSymbolRefExpr::VK_None:
89 Type = ELF::R_ARM_REL32;
91 case MCSymbolRefExpr::VK_TLSGD:
92 llvm_unreachable("unimplemented");
93 case MCSymbolRefExpr::VK_GOTTPOFF:
94 Type = ELF::R_ARM_TLS_IE32;
96 case MCSymbolRefExpr::VK_GOTPCREL:
97 Type = ELF::R_ARM_GOT_PREL;
101 case ARM::fixup_arm_blx:
102 case ARM::fixup_arm_uncondbl:
104 case MCSymbolRefExpr::VK_PLT:
105 Type = ELF::R_ARM_PLT32;
107 case MCSymbolRefExpr::VK_ARM_TLSCALL:
108 Type = ELF::R_ARM_TLS_CALL;
111 Type = ELF::R_ARM_CALL;
115 case ARM::fixup_arm_condbl:
116 case ARM::fixup_arm_condbranch:
117 case ARM::fixup_arm_uncondbranch:
118 Type = ELF::R_ARM_JUMP24;
120 case ARM::fixup_t2_condbranch:
121 case ARM::fixup_t2_uncondbranch:
122 Type = ELF::R_ARM_THM_JUMP24;
124 case ARM::fixup_arm_movt_hi16:
125 Type = ELF::R_ARM_MOVT_PREL;
127 case ARM::fixup_arm_movw_lo16:
128 Type = ELF::R_ARM_MOVW_PREL_NC;
130 case ARM::fixup_t2_movt_hi16:
131 Type = ELF::R_ARM_THM_MOVT_PREL;
133 case ARM::fixup_t2_movw_lo16:
134 Type = ELF::R_ARM_THM_MOVW_PREL_NC;
136 case ARM::fixup_arm_thumb_bl:
137 case ARM::fixup_arm_thumb_blx:
139 case MCSymbolRefExpr::VK_ARM_TLSCALL:
140 Type = ELF::R_ARM_THM_TLS_CALL;
143 Type = ELF::R_ARM_THM_CALL;
149 switch ((unsigned)Fixup.getKind()) {
150 default: llvm_unreachable("invalid fixup kind!");
153 default: llvm_unreachable("Unsupported Modifier");
154 case MCSymbolRefExpr::VK_ARM_NONE:
155 Type = ELF::R_ARM_NONE;
157 case MCSymbolRefExpr::VK_GOT:
158 Type = ELF::R_ARM_GOT_BREL;
160 case MCSymbolRefExpr::VK_TLSGD:
161 Type = ELF::R_ARM_TLS_GD32;
163 case MCSymbolRefExpr::VK_TPOFF:
164 Type = ELF::R_ARM_TLS_LE32;
166 case MCSymbolRefExpr::VK_GOTTPOFF:
167 Type = ELF::R_ARM_TLS_IE32;
169 case MCSymbolRefExpr::VK_None:
170 Type = ELF::R_ARM_ABS32;
172 case MCSymbolRefExpr::VK_GOTOFF:
173 Type = ELF::R_ARM_GOTOFF32;
175 case MCSymbolRefExpr::VK_GOTPCREL:
176 Type = ELF::R_ARM_GOT_PREL;
178 case MCSymbolRefExpr::VK_ARM_TARGET1:
179 Type = ELF::R_ARM_TARGET1;
181 case MCSymbolRefExpr::VK_ARM_TARGET2:
182 Type = ELF::R_ARM_TARGET2;
184 case MCSymbolRefExpr::VK_ARM_PREL31:
185 Type = ELF::R_ARM_PREL31;
187 case MCSymbolRefExpr::VK_ARM_TLSLDO:
188 Type = ELF::R_ARM_TLS_LDO32;
190 case MCSymbolRefExpr::VK_ARM_TLSCALL:
191 Type = ELF::R_ARM_TLS_CALL;
193 case MCSymbolRefExpr::VK_ARM_TLSDESC:
194 Type = ELF::R_ARM_TLS_GOTDESC;
196 case MCSymbolRefExpr::VK_ARM_TLSDESCSEQ:
197 Type = ELF::R_ARM_TLS_DESCSEQ;
201 case ARM::fixup_arm_ldst_pcrel_12:
202 case ARM::fixup_arm_pcrel_10:
203 case ARM::fixup_arm_adr_pcrel_12:
204 case ARM::fixup_arm_thumb_bl:
205 case ARM::fixup_arm_thumb_cb:
206 case ARM::fixup_arm_thumb_cp:
207 case ARM::fixup_arm_thumb_br:
208 llvm_unreachable("Unimplemented");
209 case ARM::fixup_arm_condbranch:
210 case ARM::fixup_arm_uncondbranch:
211 Type = ELF::R_ARM_JUMP24;
213 case ARM::fixup_arm_movt_hi16:
214 Type = ELF::R_ARM_MOVT_ABS;
216 case ARM::fixup_arm_movw_lo16:
217 Type = ELF::R_ARM_MOVW_ABS_NC;
219 case ARM::fixup_t2_movt_hi16:
220 Type = ELF::R_ARM_THM_MOVT_ABS;
222 case ARM::fixup_t2_movw_lo16:
223 Type = ELF::R_ARM_THM_MOVW_ABS_NC;
231 MCObjectWriter *llvm::createARMELFObjectWriter(raw_ostream &OS,
233 bool IsLittleEndian) {
234 MCELFObjectTargetWriter *MOTW = new ARMELFObjectWriter(OSABI);
235 return createELFObjectWriter(MOTW, OS, IsLittleEndian);