xref: /llvm-project/llvm/lib/Target/ARM/MCTargetDesc/ARMELFObjectWriter.cpp (revision bbc5d9fe427362f3fec8d147d5f7bc2d39b08e44)
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/Support/ErrorHandling.h"
20 #include "llvm/Support/raw_ostream.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     void addTargetSectionFlags(MCContext &Ctx, MCSectionELF &Sec) override;
45   };
46 
47 } // end anonymous namespace
48 
49 ARMELFObjectWriter::ARMELFObjectWriter(uint8_t OSABI)
50   : MCELFObjectTargetWriter(/*Is64Bit*/ false, OSABI,
51                             ELF::EM_ARM,
52                             /*HasRelocationAddend*/ false) {}
53 
54 bool ARMELFObjectWriter::needsRelocateWithSymbol(const MCValue &,
55                                                  const MCSymbol &,
56                                                  unsigned Type) const {
57   // FIXME: This is extremely conservative. This really needs to use an
58   // explicit list with a clear explanation for why each realocation needs to
59   // point to the symbol, not to the section.
60   switch (Type) {
61   default:
62     return true;
63 
64   case ELF::R_ARM_PREL31:
65   case ELF::R_ARM_ABS32:
66     return false;
67   }
68 }
69 
70 // Need to examine the Fixup when determining whether to
71 // emit the relocation as an explicit symbol or as a section relative
72 // offset
73 unsigned ARMELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
74                                           const MCFixup &Fixup,
75                                           bool IsPCRel) const {
76   return GetRelocTypeInner(Target, Fixup, IsPCRel, Ctx);
77 }
78 
79 unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
80                                                const MCFixup &Fixup,
81                                                bool IsPCRel,
82                                                MCContext &Ctx) const {
83   unsigned Kind = Fixup.getTargetKind();
84   if (Kind >= FirstLiteralRelocationKind)
85     return Kind - FirstLiteralRelocationKind;
86   MCSymbolRefExpr::VariantKind Modifier = Target.getAccessVariant();
87 
88   if (IsPCRel) {
89     switch (Fixup.getTargetKind()) {
90     default:
91       Ctx.reportError(Fixup.getLoc(), "unsupported relocation type");
92       return ELF::R_ARM_NONE;
93     case FK_Data_4:
94       switch (Modifier) {
95       default:
96         Ctx.reportError(Fixup.getLoc(),
97                         "invalid fixup for 4-byte pc-relative data relocation");
98         return ELF::R_ARM_NONE;
99       case MCSymbolRefExpr::VK_None: {
100         if (const MCSymbolRefExpr *SymRef = Target.getSymA()) {
101           // For GNU AS compatibility expressions such as
102           // _GLOBAL_OFFSET_TABLE_ - label emit a R_ARM_BASE_PREL relocation.
103           if (SymRef->getSymbol().getName() == "_GLOBAL_OFFSET_TABLE_")
104             return ELF::R_ARM_BASE_PREL;
105         }
106         return ELF::R_ARM_REL32;
107       }
108       case MCSymbolRefExpr::VK_GOTTPOFF:
109         return ELF::R_ARM_TLS_IE32;
110       case MCSymbolRefExpr::VK_ARM_GOT_PREL:
111         return ELF::R_ARM_GOT_PREL;
112       case MCSymbolRefExpr::VK_ARM_PREL31:
113         return ELF::R_ARM_PREL31;
114       }
115     case ARM::fixup_arm_blx:
116     case ARM::fixup_arm_uncondbl:
117       switch (Modifier) {
118       case MCSymbolRefExpr::VK_PLT:
119         return ELF::R_ARM_CALL;
120       case MCSymbolRefExpr::VK_TLSCALL:
121         return ELF::R_ARM_TLS_CALL;
122       default:
123         return ELF::R_ARM_CALL;
124       }
125     case ARM::fixup_arm_condbl:
126     case ARM::fixup_arm_condbranch:
127     case ARM::fixup_arm_uncondbranch:
128       return ELF::R_ARM_JUMP24;
129     case ARM::fixup_t2_condbranch:
130       return ELF::R_ARM_THM_JUMP19;
131     case ARM::fixup_t2_uncondbranch:
132       return ELF::R_ARM_THM_JUMP24;
133     case ARM::fixup_arm_movt_hi16:
134       return ELF::R_ARM_MOVT_PREL;
135     case ARM::fixup_arm_movw_lo16:
136       return ELF::R_ARM_MOVW_PREL_NC;
137     case ARM::fixup_t2_movt_hi16:
138       return ELF::R_ARM_THM_MOVT_PREL;
139     case ARM::fixup_t2_movw_lo16:
140       return ELF::R_ARM_THM_MOVW_PREL_NC;
141     case ARM::fixup_arm_thumb_upper_8_15:
142       return ELF::R_ARM_THM_ALU_ABS_G3;
143     case ARM::fixup_arm_thumb_upper_0_7:
144       return ELF::R_ARM_THM_ALU_ABS_G2_NC;
145     case ARM::fixup_arm_thumb_lower_8_15:
146       return ELF::R_ARM_THM_ALU_ABS_G1_NC;
147     case ARM::fixup_arm_thumb_lower_0_7:
148       return ELF::R_ARM_THM_ALU_ABS_G0_NC;
149     case ARM::fixup_arm_thumb_br:
150       return ELF::R_ARM_THM_JUMP11;
151     case ARM::fixup_arm_thumb_bcc:
152       return ELF::R_ARM_THM_JUMP8;
153     case ARM::fixup_arm_thumb_bl:
154     case ARM::fixup_arm_thumb_blx:
155       switch (Modifier) {
156       case MCSymbolRefExpr::VK_TLSCALL:
157         return ELF::R_ARM_THM_TLS_CALL;
158       default:
159         return ELF::R_ARM_THM_CALL;
160       }
161     case ARM::fixup_arm_ldst_pcrel_12:
162       return ELF::R_ARM_LDR_PC_G0;
163     case ARM::fixup_arm_pcrel_10_unscaled:
164       return ELF::R_ARM_LDRS_PC_G0;
165     case ARM::fixup_t2_ldst_pcrel_12:
166       return ELF::R_ARM_THM_PC12;
167     case ARM::fixup_bf_target:
168       return ELF::R_ARM_THM_BF16;
169     case ARM::fixup_bfc_target:
170       return ELF::R_ARM_THM_BF12;
171     case ARM::fixup_bfl_target:
172       return ELF::R_ARM_THM_BF18;
173     }
174   }
175   switch (Kind) {
176   default:
177     Ctx.reportError(Fixup.getLoc(), "unsupported relocation type");
178     return ELF::R_ARM_NONE;
179   case FK_Data_1:
180     switch (Modifier) {
181     default:
182       Ctx.reportError(Fixup.getLoc(),
183                       "invalid fixup for 1-byte data relocation");
184       return ELF::R_ARM_NONE;
185     case MCSymbolRefExpr::VK_None:
186       return ELF::R_ARM_ABS8;
187     }
188   case FK_Data_2:
189     switch (Modifier) {
190     default:
191       Ctx.reportError(Fixup.getLoc(),
192                       "invalid fixup for 2-byte data relocation");
193       return ELF::R_ARM_NONE;
194     case MCSymbolRefExpr::VK_None:
195       return ELF::R_ARM_ABS16;
196     }
197   case FK_Data_4:
198     switch (Modifier) {
199     default:
200       Ctx.reportError(Fixup.getLoc(),
201                       "invalid fixup for 4-byte data relocation");
202       return ELF::R_ARM_NONE;
203     case MCSymbolRefExpr::VK_ARM_NONE:
204       return ELF::R_ARM_NONE;
205     case MCSymbolRefExpr::VK_GOT:
206       return ELF::R_ARM_GOT_BREL;
207     case MCSymbolRefExpr::VK_TLSGD:
208       return ELF::R_ARM_TLS_GD32;
209     case MCSymbolRefExpr::VK_TPOFF:
210       return ELF::R_ARM_TLS_LE32;
211     case MCSymbolRefExpr::VK_GOTTPOFF:
212       return ELF::R_ARM_TLS_IE32;
213     case MCSymbolRefExpr::VK_None:
214       return ELF::R_ARM_ABS32;
215     case MCSymbolRefExpr::VK_GOTOFF:
216       return ELF::R_ARM_GOTOFF32;
217     case MCSymbolRefExpr::VK_ARM_GOT_PREL:
218       return ELF::R_ARM_GOT_PREL;
219     case MCSymbolRefExpr::VK_ARM_TARGET1:
220       return ELF::R_ARM_TARGET1;
221     case MCSymbolRefExpr::VK_ARM_TARGET2:
222       return ELF::R_ARM_TARGET2;
223     case MCSymbolRefExpr::VK_ARM_PREL31:
224       return ELF::R_ARM_PREL31;
225     case MCSymbolRefExpr::VK_ARM_SBREL:
226       return ELF::R_ARM_SBREL32;
227     case MCSymbolRefExpr::VK_ARM_TLSLDO:
228       return ELF::R_ARM_TLS_LDO32;
229     case MCSymbolRefExpr::VK_TLSCALL:
230       return ELF::R_ARM_TLS_CALL;
231     case MCSymbolRefExpr::VK_TLSDESC:
232       return ELF::R_ARM_TLS_GOTDESC;
233     case MCSymbolRefExpr::VK_TLSLDM:
234       return ELF::R_ARM_TLS_LDM32;
235     case MCSymbolRefExpr::VK_ARM_TLSDESCSEQ:
236       return ELF::R_ARM_TLS_DESCSEQ;
237     }
238   case ARM::fixup_arm_condbranch:
239   case ARM::fixup_arm_uncondbranch:
240     return ELF::R_ARM_JUMP24;
241   case ARM::fixup_arm_movt_hi16:
242     switch (Modifier) {
243     default:
244       Ctx.reportError(Fixup.getLoc(), "invalid fixup for ARM MOVT instruction");
245       return ELF::R_ARM_NONE;
246     case MCSymbolRefExpr::VK_None:
247       return ELF::R_ARM_MOVT_ABS;
248     case MCSymbolRefExpr::VK_ARM_SBREL:
249       return ELF::R_ARM_MOVT_BREL;
250     }
251   case ARM::fixup_arm_movw_lo16:
252     switch (Modifier) {
253     default:
254       Ctx.reportError(Fixup.getLoc(), "invalid fixup for ARM MOVW instruction");
255       return ELF::R_ARM_NONE;
256     case MCSymbolRefExpr::VK_None:
257       return ELF::R_ARM_MOVW_ABS_NC;
258     case MCSymbolRefExpr::VK_ARM_SBREL:
259       return ELF::R_ARM_MOVW_BREL_NC;
260     }
261   case ARM::fixup_t2_movt_hi16:
262     switch (Modifier) {
263     default:
264       Ctx.reportError(Fixup.getLoc(),
265                       "invalid fixup for Thumb MOVT instruction");
266       return ELF::R_ARM_NONE;
267     case MCSymbolRefExpr::VK_None:
268       return ELF::R_ARM_THM_MOVT_ABS;
269     case MCSymbolRefExpr::VK_ARM_SBREL:
270       return ELF::R_ARM_THM_MOVT_BREL;
271     }
272   case ARM::fixup_t2_movw_lo16:
273     switch (Modifier) {
274     default:
275       Ctx.reportError(Fixup.getLoc(),
276                       "invalid fixup for Thumb MOVW instruction");
277       return ELF::R_ARM_NONE;
278     case MCSymbolRefExpr::VK_None:
279       return ELF::R_ARM_THM_MOVW_ABS_NC;
280     case MCSymbolRefExpr::VK_ARM_SBREL:
281       return ELF::R_ARM_THM_MOVW_BREL_NC;
282     }
283 
284   case ARM::fixup_arm_thumb_upper_8_15:
285     return ELF::R_ARM_THM_ALU_ABS_G3;
286   case ARM::fixup_arm_thumb_upper_0_7:
287     return ELF::R_ARM_THM_ALU_ABS_G2_NC;
288   case ARM::fixup_arm_thumb_lower_8_15:
289     return ELF::R_ARM_THM_ALU_ABS_G1_NC;
290   case ARM::fixup_arm_thumb_lower_0_7:
291     return ELF::R_ARM_THM_ALU_ABS_G0_NC;
292   }
293 }
294 
295 void ARMELFObjectWriter::addTargetSectionFlags(MCContext &Ctx,
296                                                MCSectionELF &Sec) {
297   // The mix of execute-only and non-execute-only at link time is
298   // non-execute-only. To avoid the empty implicitly created .text
299   // section from making the whole .text section non-execute-only, we
300   // mark it execute-only if it is empty and there is at least one
301   // execute-only section in the object.
302   MCSectionELF *TextSection =
303       static_cast<MCSectionELF *>(Ctx.getObjectFileInfo()->getTextSection());
304   if (Sec.getKind().isExecuteOnly() && !TextSection->hasInstructions()) {
305     for (auto &F : TextSection->getFragmentList())
306       if (auto *DF = dyn_cast<MCDataFragment>(&F))
307         if (!DF->getContents().empty())
308           return;
309     TextSection->setFlags(TextSection->getFlags() | ELF::SHF_ARM_PURECODE);
310   }
311 }
312 
313 std::unique_ptr<MCObjectTargetWriter>
314 llvm::createARMELFObjectWriter(uint8_t OSABI) {
315   return std::make_unique<ARMELFObjectWriter>(OSABI);
316 }
317