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(unsigned Kind
,
50 MCSymbolRefExpr::VariantKind
&Modifier
,
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_movq_load
:
79 case X86::reloc_branch_4byte_pcrel
:
80 Modifier
= MCSymbolRefExpr::VK_PLT
;
91 static void checkIs32(MCContext
&Ctx
, SMLoc Loc
, X86_64RelType Type
) {
94 "32 bit reloc applied to a field with a different size");
97 static unsigned getRelocType64(MCContext
&Ctx
, SMLoc Loc
,
98 MCSymbolRefExpr::VariantKind Modifier
,
99 X86_64RelType Type
, bool IsPCRel
,
103 llvm_unreachable("Unimplemented");
104 case MCSymbolRefExpr::VK_None
:
105 case MCSymbolRefExpr::VK_X86_ABS8
:
108 if (Modifier
== MCSymbolRefExpr::VK_None
)
109 return ELF::R_X86_64_NONE
;
110 llvm_unreachable("Unimplemented");
112 return IsPCRel
? ELF::R_X86_64_PC64
: ELF::R_X86_64_64
;
114 return IsPCRel
? ELF::R_X86_64_PC32
: ELF::R_X86_64_32
;
116 return ELF::R_X86_64_32S
;
118 return IsPCRel
? ELF::R_X86_64_PC16
: ELF::R_X86_64_16
;
120 return IsPCRel
? ELF::R_X86_64_PC8
: ELF::R_X86_64_8
;
122 llvm_unreachable("unexpected relocation type!");
123 case MCSymbolRefExpr::VK_GOT
:
126 return IsPCRel
? ELF::R_X86_64_GOTPC64
: ELF::R_X86_64_GOT64
;
128 return IsPCRel
? ELF::R_X86_64_GOTPC32
: ELF::R_X86_64_GOT32
;
133 llvm_unreachable("Unimplemented");
135 llvm_unreachable("unexpected relocation type!");
136 case MCSymbolRefExpr::VK_GOTOFF
:
137 assert(Type
== RT64_64
);
139 return ELF::R_X86_64_GOTOFF64
;
140 case MCSymbolRefExpr::VK_TPOFF
:
144 return ELF::R_X86_64_TPOFF64
;
146 return ELF::R_X86_64_TPOFF32
;
151 llvm_unreachable("Unimplemented");
153 llvm_unreachable("unexpected relocation type!");
154 case MCSymbolRefExpr::VK_DTPOFF
:
158 return ELF::R_X86_64_DTPOFF64
;
160 return ELF::R_X86_64_DTPOFF32
;
165 llvm_unreachable("Unimplemented");
167 llvm_unreachable("unexpected relocation type!");
168 case MCSymbolRefExpr::VK_SIZE
:
172 return ELF::R_X86_64_SIZE64
;
174 return ELF::R_X86_64_SIZE32
;
179 llvm_unreachable("Unimplemented");
181 llvm_unreachable("unexpected relocation type!");
182 case MCSymbolRefExpr::VK_TLSCALL
:
183 return ELF::R_X86_64_TLSDESC_CALL
;
184 case MCSymbolRefExpr::VK_TLSDESC
:
185 return ELF::R_X86_64_GOTPC32_TLSDESC
;
186 case MCSymbolRefExpr::VK_TLSGD
:
187 checkIs32(Ctx
, Loc
, Type
);
188 return ELF::R_X86_64_TLSGD
;
189 case MCSymbolRefExpr::VK_GOTTPOFF
:
190 checkIs32(Ctx
, Loc
, Type
);
191 return ELF::R_X86_64_GOTTPOFF
;
192 case MCSymbolRefExpr::VK_TLSLD
:
193 checkIs32(Ctx
, Loc
, Type
);
194 return ELF::R_X86_64_TLSLD
;
195 case MCSymbolRefExpr::VK_PLT
:
196 checkIs32(Ctx
, Loc
, Type
);
197 return ELF::R_X86_64_PLT32
;
198 case MCSymbolRefExpr::VK_GOTPCREL
:
199 checkIs32(Ctx
, Loc
, Type
);
200 // Older versions of ld.bfd/ld.gold/lld
201 // do not support GOTPCRELX/REX_GOTPCRELX,
202 // and we want to keep back-compatibility.
203 if (!Ctx
.getAsmInfo()->canRelaxRelocations())
204 return ELF::R_X86_64_GOTPCREL
;
207 return ELF::R_X86_64_GOTPCREL
;
208 case X86::reloc_riprel_4byte_relax
:
209 return ELF::R_X86_64_GOTPCRELX
;
210 case X86::reloc_riprel_4byte_relax_rex
:
211 case X86::reloc_riprel_4byte_movq_load
:
212 return ELF::R_X86_64_REX_GOTPCRELX
;
214 llvm_unreachable("unexpected relocation type!");
218 enum X86_32RelType
{ RT32_NONE
, RT32_32
, RT32_16
, RT32_8
};
220 static X86_32RelType
getType32(X86_64RelType T
) {
225 llvm_unreachable("Unimplemented");
234 llvm_unreachable("unexpected relocation type!");
237 static unsigned getRelocType32(MCContext
&Ctx
,
238 MCSymbolRefExpr::VariantKind Modifier
,
239 X86_32RelType Type
, bool IsPCRel
,
243 llvm_unreachable("Unimplemented");
244 case MCSymbolRefExpr::VK_None
:
245 case MCSymbolRefExpr::VK_X86_ABS8
:
248 if (Modifier
== MCSymbolRefExpr::VK_None
)
249 return ELF::R_386_NONE
;
250 llvm_unreachable("Unimplemented");
252 return IsPCRel
? ELF::R_386_PC32
: ELF::R_386_32
;
254 return IsPCRel
? ELF::R_386_PC16
: ELF::R_386_16
;
256 return IsPCRel
? ELF::R_386_PC8
: ELF::R_386_8
;
258 llvm_unreachable("unexpected relocation type!");
259 case MCSymbolRefExpr::VK_GOT
:
260 assert(Type
== RT32_32
);
262 return ELF::R_386_GOTPC
;
263 // Older versions of ld.bfd/ld.gold/lld do not support R_386_GOT32X and we
264 // want to maintain compatibility.
265 if (!Ctx
.getAsmInfo()->canRelaxRelocations())
266 return ELF::R_386_GOT32
;
268 return Kind
== X86::reloc_signed_4byte_relax
? ELF::R_386_GOT32X
270 case MCSymbolRefExpr::VK_GOTOFF
:
271 assert(Type
== RT32_32
);
273 return ELF::R_386_GOTOFF
;
274 case MCSymbolRefExpr::VK_TLSCALL
:
275 return ELF::R_386_TLS_DESC_CALL
;
276 case MCSymbolRefExpr::VK_TLSDESC
:
277 return ELF::R_386_TLS_GOTDESC
;
278 case MCSymbolRefExpr::VK_TPOFF
:
279 assert(Type
== RT32_32
);
281 return ELF::R_386_TLS_LE_32
;
282 case MCSymbolRefExpr::VK_DTPOFF
:
283 assert(Type
== RT32_32
);
285 return ELF::R_386_TLS_LDO_32
;
286 case MCSymbolRefExpr::VK_TLSGD
:
287 assert(Type
== RT32_32
);
289 return ELF::R_386_TLS_GD
;
290 case MCSymbolRefExpr::VK_GOTTPOFF
:
291 assert(Type
== RT32_32
);
293 return ELF::R_386_TLS_IE_32
;
294 case MCSymbolRefExpr::VK_PLT
:
295 assert(Type
== RT32_32
);
296 return ELF::R_386_PLT32
;
297 case MCSymbolRefExpr::VK_INDNTPOFF
:
298 assert(Type
== RT32_32
);
300 return ELF::R_386_TLS_IE
;
301 case MCSymbolRefExpr::VK_NTPOFF
:
302 assert(Type
== RT32_32
);
304 return ELF::R_386_TLS_LE
;
305 case MCSymbolRefExpr::VK_GOTNTPOFF
:
306 assert(Type
== RT32_32
);
308 return ELF::R_386_TLS_GOTIE
;
309 case MCSymbolRefExpr::VK_TLSLDM
:
310 assert(Type
== RT32_32
);
312 return ELF::R_386_TLS_LDM
;
316 unsigned X86ELFObjectWriter::getRelocType(MCContext
&Ctx
, const MCValue
&Target
,
317 const MCFixup
&Fixup
,
318 bool IsPCRel
) const {
319 MCSymbolRefExpr::VariantKind Modifier
= Target
.getAccessVariant();
320 unsigned Kind
= Fixup
.getKind();
321 X86_64RelType Type
= getType64(Kind
, Modifier
, IsPCRel
);
322 if (getEMachine() == ELF::EM_X86_64
)
323 return getRelocType64(Ctx
, Fixup
.getLoc(), Modifier
, Type
, IsPCRel
, Kind
);
325 assert((getEMachine() == ELF::EM_386
|| getEMachine() == ELF::EM_IAMCU
) &&
326 "Unsupported ELF machine type.");
327 return getRelocType32(Ctx
, Modifier
, getType32(Type
), IsPCRel
, Kind
);
330 std::unique_ptr
<MCObjectTargetWriter
>
331 llvm::createX86ELFObjectWriter(bool IsELF64
, uint8_t OSABI
, uint16_t EMachine
) {
332 return llvm::make_unique
<X86ELFObjectWriter
>(IsELF64
, OSABI
, EMachine
);