1 //===-- X86ELFObjectWriter.cpp - X86 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/X86FixupKinds.h"
10 #include "MCTargetDesc/X86MCTargetDesc.h"
11 #include "llvm/BinaryFormat/ELF.h"
12 #include "llvm/MC/MCAsmInfo.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCELFObjectWriter.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCFixup.h"
17 #include "llvm/MC/MCObjectWriter.h"
18 #include "llvm/MC/MCValue.h"
19 #include "llvm/Support/ErrorHandling.h"
27 class X86ELFObjectWriter
: public MCELFObjectTargetWriter
{
29 X86ELFObjectWriter(bool IsELF64
, uint8_t OSABI
, uint16_t EMachine
);
30 ~X86ELFObjectWriter() override
= default;
33 unsigned getRelocType(MCContext
&Ctx
, const MCValue
&Target
,
34 const MCFixup
&Fixup
, bool IsPCRel
) const override
;
37 } // end anonymous namespace
39 X86ELFObjectWriter::X86ELFObjectWriter(bool IsELF64
, uint8_t OSABI
,
41 : MCELFObjectTargetWriter(IsELF64
, OSABI
, EMachine
,
42 // Only i386 and IAMCU use Rel instead of RelA.
43 /*HasRelocationAddend*/
44 (EMachine
!= ELF::EM_386
) &&
45 (EMachine
!= ELF::EM_IAMCU
)) {}
47 enum X86_64RelType
{ RT64_NONE
, RT64_64
, RT64_32
, RT64_32S
, RT64_16
, RT64_8
};
49 static X86_64RelType
getType64(MCFixupKind Kind
,
50 MCSymbolRefExpr::VariantKind
&Modifier
,
52 switch (unsigned(Kind
)) {
54 llvm_unreachable("Unimplemented");
57 case X86::reloc_global_offset_table8
:
58 Modifier
= MCSymbolRefExpr::VK_GOT
;
63 case X86::reloc_signed_4byte
:
64 case X86::reloc_signed_4byte_relax
:
65 if (Modifier
== MCSymbolRefExpr::VK_None
&& !IsPCRel
)
68 case X86::reloc_global_offset_table
:
69 Modifier
= MCSymbolRefExpr::VK_GOT
;
74 case X86::reloc_riprel_4byte
:
75 case X86::reloc_riprel_4byte_relax
:
76 case X86::reloc_riprel_4byte_relax_rex
:
77 case X86::reloc_riprel_4byte_relax_rex2
:
78 case X86::reloc_riprel_4byte_movq_load
:
79 case X86::reloc_riprel_4byte_movq_load_rex2
:
80 case X86::reloc_riprel_4byte_relax_evex
:
82 case X86::reloc_branch_4byte_pcrel
:
83 Modifier
= MCSymbolRefExpr::VK_PLT
;
94 static void checkIs32(MCContext
&Ctx
, SMLoc Loc
, X86_64RelType Type
) {
97 "32 bit reloc applied to a field with a different size");
100 static void checkIs64(MCContext
&Ctx
, SMLoc Loc
, X86_64RelType Type
) {
103 "64 bit reloc applied to a field with a different size");
106 static unsigned getRelocType64(MCContext
&Ctx
, SMLoc Loc
,
107 MCSymbolRefExpr::VariantKind Modifier
,
108 X86_64RelType Type
, bool IsPCRel
,
112 llvm_unreachable("Unimplemented");
113 case MCSymbolRefExpr::VK_None
:
114 case MCSymbolRefExpr::VK_X86_ABS8
:
117 if (Modifier
== MCSymbolRefExpr::VK_None
)
118 return ELF::R_X86_64_NONE
;
119 llvm_unreachable("Unimplemented");
121 return IsPCRel
? ELF::R_X86_64_PC64
: ELF::R_X86_64_64
;
123 return IsPCRel
? ELF::R_X86_64_PC32
: ELF::R_X86_64_32
;
125 return ELF::R_X86_64_32S
;
127 return IsPCRel
? ELF::R_X86_64_PC16
: ELF::R_X86_64_16
;
129 return IsPCRel
? ELF::R_X86_64_PC8
: ELF::R_X86_64_8
;
131 llvm_unreachable("unexpected relocation type!");
132 case MCSymbolRefExpr::VK_GOT
:
135 return IsPCRel
? ELF::R_X86_64_GOTPC64
: ELF::R_X86_64_GOT64
;
137 return IsPCRel
? ELF::R_X86_64_GOTPC32
: ELF::R_X86_64_GOT32
;
142 llvm_unreachable("Unimplemented");
144 llvm_unreachable("unexpected relocation type!");
145 case MCSymbolRefExpr::VK_GOTOFF
:
148 Ctx
.reportError(Loc
, "unsupported relocation type");
149 return ELF::R_X86_64_GOTOFF64
;
150 case MCSymbolRefExpr::VK_TPOFF
:
154 return ELF::R_X86_64_TPOFF64
;
156 return ELF::R_X86_64_TPOFF32
;
161 llvm_unreachable("Unimplemented");
163 llvm_unreachable("unexpected relocation type!");
164 case MCSymbolRefExpr::VK_DTPOFF
:
168 return ELF::R_X86_64_DTPOFF64
;
170 return ELF::R_X86_64_DTPOFF32
;
175 llvm_unreachable("Unimplemented");
177 llvm_unreachable("unexpected relocation type!");
178 case MCSymbolRefExpr::VK_SIZE
:
182 return ELF::R_X86_64_SIZE64
;
184 return ELF::R_X86_64_SIZE32
;
189 llvm_unreachable("Unimplemented");
191 llvm_unreachable("unexpected relocation type!");
192 case MCSymbolRefExpr::VK_TLSCALL
:
193 return ELF::R_X86_64_TLSDESC_CALL
;
194 case MCSymbolRefExpr::VK_TLSDESC
:
195 return ((unsigned)Kind
== X86::reloc_riprel_4byte_relax_rex2
)
196 ? ELF::R_X86_64_CODE_4_GOTPC32_TLSDESC
197 : ELF::R_X86_64_GOTPC32_TLSDESC
;
198 case MCSymbolRefExpr::VK_TLSGD
:
199 checkIs32(Ctx
, Loc
, Type
);
200 return ELF::R_X86_64_TLSGD
;
201 case MCSymbolRefExpr::VK_GOTTPOFF
:
202 checkIs32(Ctx
, Loc
, Type
);
203 if ((unsigned)Kind
== X86::reloc_riprel_4byte_movq_load_rex2
||
204 (unsigned)Kind
== X86::reloc_riprel_4byte_relax_rex2
)
205 return ELF::R_X86_64_CODE_4_GOTTPOFF
;
206 else if ((unsigned)Kind
== X86::reloc_riprel_4byte_relax_evex
)
207 return ELF::R_X86_64_CODE_6_GOTTPOFF
;
208 return ELF::R_X86_64_GOTTPOFF
;
209 case MCSymbolRefExpr::VK_TLSLD
:
210 checkIs32(Ctx
, Loc
, Type
);
211 return ELF::R_X86_64_TLSLD
;
212 case MCSymbolRefExpr::VK_PLT
:
213 checkIs32(Ctx
, Loc
, Type
);
214 return ELF::R_X86_64_PLT32
;
215 case MCSymbolRefExpr::VK_GOTPCREL
:
216 checkIs32(Ctx
, Loc
, Type
);
217 // Older versions of ld.bfd/ld.gold/lld
218 // do not support GOTPCRELX/REX_GOTPCRELX/CODE_4_GOTPCRELX,
219 // and we want to keep back-compatibility.
220 if (!Ctx
.getTargetOptions()->X86RelaxRelocations
)
221 return ELF::R_X86_64_GOTPCREL
;
222 switch (unsigned(Kind
)) {
224 return ELF::R_X86_64_GOTPCREL
;
225 case X86::reloc_riprel_4byte_relax
:
226 return ELF::R_X86_64_GOTPCRELX
;
227 case X86::reloc_riprel_4byte_relax_rex
:
228 case X86::reloc_riprel_4byte_movq_load
:
229 return ELF::R_X86_64_REX_GOTPCRELX
;
230 case X86::reloc_riprel_4byte_relax_rex2
:
231 case X86::reloc_riprel_4byte_movq_load_rex2
:
232 return ELF::R_X86_64_CODE_4_GOTPCRELX
;
234 llvm_unreachable("unexpected relocation type!");
235 case MCSymbolRefExpr::VK_GOTPCREL_NORELAX
:
236 checkIs32(Ctx
, Loc
, Type
);
237 return ELF::R_X86_64_GOTPCREL
;
238 case MCSymbolRefExpr::VK_X86_PLTOFF
:
239 checkIs64(Ctx
, Loc
, Type
);
240 return ELF::R_X86_64_PLTOFF64
;
244 enum X86_32RelType
{ RT32_NONE
, RT32_32
, RT32_16
, RT32_8
};
246 static unsigned getRelocType32(MCContext
&Ctx
, SMLoc Loc
,
247 MCSymbolRefExpr::VariantKind Modifier
,
248 X86_32RelType Type
, bool IsPCRel
,
252 llvm_unreachable("Unimplemented");
253 case MCSymbolRefExpr::VK_None
:
254 case MCSymbolRefExpr::VK_X86_ABS8
:
257 if (Modifier
== MCSymbolRefExpr::VK_None
)
258 return ELF::R_386_NONE
;
259 llvm_unreachable("Unimplemented");
261 return IsPCRel
? ELF::R_386_PC32
: ELF::R_386_32
;
263 return IsPCRel
? ELF::R_386_PC16
: ELF::R_386_16
;
265 return IsPCRel
? ELF::R_386_PC8
: ELF::R_386_8
;
267 llvm_unreachable("unexpected relocation type!");
268 case MCSymbolRefExpr::VK_GOT
:
272 return ELF::R_386_GOTPC
;
273 // Older versions of ld.bfd/ld.gold/lld do not support R_386_GOT32X and we
274 // want to maintain compatibility.
275 if (!Ctx
.getTargetOptions()->X86RelaxRelocations
)
276 return ELF::R_386_GOT32
;
278 return Kind
== MCFixupKind(X86::reloc_signed_4byte_relax
)
281 case MCSymbolRefExpr::VK_GOTOFF
:
285 return ELF::R_386_GOTOFF
;
286 case MCSymbolRefExpr::VK_TLSCALL
:
287 return ELF::R_386_TLS_DESC_CALL
;
288 case MCSymbolRefExpr::VK_TLSDESC
:
289 return ELF::R_386_TLS_GOTDESC
;
290 case MCSymbolRefExpr::VK_TPOFF
:
294 return ELF::R_386_TLS_LE_32
;
295 case MCSymbolRefExpr::VK_DTPOFF
:
299 return ELF::R_386_TLS_LDO_32
;
300 case MCSymbolRefExpr::VK_TLSGD
:
304 return ELF::R_386_TLS_GD
;
305 case MCSymbolRefExpr::VK_GOTTPOFF
:
309 return ELF::R_386_TLS_IE_32
;
310 case MCSymbolRefExpr::VK_PLT
:
313 return ELF::R_386_PLT32
;
314 case MCSymbolRefExpr::VK_INDNTPOFF
:
318 return ELF::R_386_TLS_IE
;
319 case MCSymbolRefExpr::VK_NTPOFF
:
323 return ELF::R_386_TLS_LE
;
324 case MCSymbolRefExpr::VK_GOTNTPOFF
:
328 return ELF::R_386_TLS_GOTIE
;
329 case MCSymbolRefExpr::VK_TLSLDM
:
333 return ELF::R_386_TLS_LDM
;
335 Ctx
.reportError(Loc
, "unsupported relocation type");
336 return ELF::R_386_NONE
;
339 unsigned X86ELFObjectWriter::getRelocType(MCContext
&Ctx
, const MCValue
&Target
,
340 const MCFixup
&Fixup
,
341 bool IsPCRel
) const {
342 MCFixupKind Kind
= Fixup
.getKind();
343 if (Kind
>= FirstLiteralRelocationKind
)
344 return Kind
- FirstLiteralRelocationKind
;
345 MCSymbolRefExpr::VariantKind Modifier
= Target
.getAccessVariant();
346 X86_64RelType Type
= getType64(Kind
, Modifier
, IsPCRel
);
347 if (getEMachine() == ELF::EM_X86_64
)
348 return getRelocType64(Ctx
, Fixup
.getLoc(), Modifier
, Type
, IsPCRel
, Kind
);
350 assert((getEMachine() == ELF::EM_386
|| getEMachine() == ELF::EM_IAMCU
) &&
351 "Unsupported ELF machine type.");
353 X86_32RelType RelType
= RT32_NONE
;
358 Ctx
.reportError(Fixup
.getLoc(), "unsupported relocation type");
359 return ELF::R_386_NONE
;
371 return getRelocType32(Ctx
, Fixup
.getLoc(), Modifier
, RelType
, IsPCRel
, Kind
);
374 std::unique_ptr
<MCObjectTargetWriter
>
375 llvm::createX86ELFObjectWriter(bool IsELF64
, uint8_t OSABI
, uint16_t EMachine
) {
376 return std::make_unique
<X86ELFObjectWriter
>(IsELF64
, OSABI
, EMachine
);