xref: /llvm-project/llvm/lib/Target/ARM/MCTargetDesc/ARMELFObjectWriter.cpp (revision 5be43db9b17e7cfc9e987f257221b0926551eb6e)
1 //===-- ARMELFObjectWriter.cpp - ARM 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 
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"
21 #include <cstdint>
22 
23 using namespace llvm;
24 
25 namespace {
26 
27   class ARMELFObjectWriter : public MCELFObjectTargetWriter {
28     enum { DefaultEABIVersion = 0x05000000U };
29 
30     unsigned GetRelocTypeInner(const MCValue &Target, const MCFixup &Fixup,
31                                bool IsPCRel, MCContext &Ctx) const;
32 
33   public:
34     ARMELFObjectWriter(uint8_t OSABI);
35 
36     ~ARMELFObjectWriter() override = default;
37 
38     unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
39                           const MCFixup &Fixup, bool IsPCRel) const override;
40 
41     bool needsRelocateWithSymbol(const MCValue &Val, const MCSymbol &Sym,
42                                  unsigned Type) const override;
43   };
44 
45 } // end anonymous namespace
46 
47 ARMELFObjectWriter::ARMELFObjectWriter(uint8_t OSABI)
48   : MCELFObjectTargetWriter(/*Is64Bit*/ false, OSABI,
49                             ELF::EM_ARM,
50                             /*HasRelocationAddend*/ false) {}
51 
52 bool ARMELFObjectWriter::needsRelocateWithSymbol(const MCValue &,
53                                                  const MCSymbol &,
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.
58   switch (Type) {
59   default:
60     return true;
61 
62   case ELF::R_ARM_PREL31:
63   case ELF::R_ARM_ABS32:
64     return false;
65   }
66 }
67 
68 // Need to examine the Fixup when determining whether to
69 // emit the relocation as an explicit symbol or as a section relative
70 // offset
71 unsigned ARMELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
72                                           const MCFixup &Fixup,
73                                           bool IsPCRel) const {
74   return GetRelocTypeInner(Target, Fixup, IsPCRel, Ctx);
75 }
76 
77 unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
78                                                const MCFixup &Fixup,
79                                                bool IsPCRel,
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(),
88                       "relocation " +
89                           object::getELFRelocationTypeName(ELF::EM_ARM, Type) +
90                           " only supported in FDPIC mode");
91     return Type;
92   };
93 
94   if (IsPCRel) {
95     switch (Fixup.getTargetKind()) {
96     default:
97       Ctx.reportError(Fixup.getLoc(), "unsupported relocation type");
98       return ELF::R_ARM_NONE;
99     case FK_Data_4:
100       switch (Modifier) {
101       default:
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;
111         }
112         return ELF::R_ARM_REL32;
113       }
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;
120       }
121     case ARM::fixup_arm_blx:
122     case ARM::fixup_arm_uncondbl:
123       switch (Modifier) {
124       case MCSymbolRefExpr::VK_PLT:
125         return ELF::R_ARM_CALL;
126       case MCSymbolRefExpr::VK_TLSCALL:
127         return ELF::R_ARM_TLS_CALL;
128       default:
129         return ELF::R_ARM_CALL;
130       }
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:
161       switch (Modifier) {
162       case MCSymbolRefExpr::VK_TLSCALL:
163         return ELF::R_ARM_THM_TLS_CALL;
164       default:
165         return ELF::R_ARM_THM_CALL;
166       }
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;
185     }
186   }
187   switch (Kind) {
188   default:
189     Ctx.reportError(Fixup.getLoc(), "unsupported relocation type");
190     return ELF::R_ARM_NONE;
191   case FK_Data_1:
192     switch (Modifier) {
193     default:
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;
199     }
200   case FK_Data_2:
201     switch (Modifier) {
202     default:
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;
208     }
209   case FK_Data_4:
210     switch (Modifier) {
211     default:
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);
261     }
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:
266     switch (Modifier) {
267     default:
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;
274     }
275   case ARM::fixup_arm_movw_lo16:
276     switch (Modifier) {
277     default:
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;
284     }
285   case ARM::fixup_t2_movt_hi16:
286     switch (Modifier) {
287     default:
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;
295     }
296   case ARM::fixup_t2_movw_lo16:
297     switch (Modifier) {
298     default:
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;
306     }
307 
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;
316   }
317 }
318 
319 std::unique_ptr<MCObjectTargetWriter>
320 llvm::createARMELFObjectWriter(uint8_t OSABI) {
321   return std::make_unique<ARMELFObjectWriter>(OSABI);
322 }
323