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_64
, RT64_32
, RT64_32S
, RT64_16
, RT64_8
};
49 static X86_64RelType
getType64(unsigned Kind
,
50 MCSymbolRefExpr::VariantKind
&Modifier
,
54 llvm_unreachable("Unimplemented");
55 case X86::reloc_global_offset_table8
:
56 Modifier
= MCSymbolRefExpr::VK_GOT
;
61 case X86::reloc_signed_4byte
:
62 case X86::reloc_signed_4byte_relax
:
63 if (Modifier
== MCSymbolRefExpr::VK_None
&& !IsPCRel
)
66 case X86::reloc_global_offset_table
:
67 Modifier
= MCSymbolRefExpr::VK_GOT
;
72 case X86::reloc_riprel_4byte
:
73 case X86::reloc_riprel_4byte_relax
:
74 case X86::reloc_riprel_4byte_relax_rex
:
75 case X86::reloc_riprel_4byte_movq_load
:
77 case X86::reloc_branch_4byte_pcrel
:
78 Modifier
= MCSymbolRefExpr::VK_PLT
;
89 static void checkIs32(MCContext
&Ctx
, SMLoc Loc
, X86_64RelType Type
) {
92 "32 bit reloc applied to a field with a different size");
95 static unsigned getRelocType64(MCContext
&Ctx
, SMLoc Loc
,
96 MCSymbolRefExpr::VariantKind Modifier
,
97 X86_64RelType Type
, bool IsPCRel
,
101 llvm_unreachable("Unimplemented");
102 case MCSymbolRefExpr::VK_None
:
103 case MCSymbolRefExpr::VK_X86_ABS8
:
106 return IsPCRel
? ELF::R_X86_64_PC64
: ELF::R_X86_64_64
;
108 return IsPCRel
? ELF::R_X86_64_PC32
: ELF::R_X86_64_32
;
110 return ELF::R_X86_64_32S
;
112 return IsPCRel
? ELF::R_X86_64_PC16
: ELF::R_X86_64_16
;
114 return IsPCRel
? ELF::R_X86_64_PC8
: ELF::R_X86_64_8
;
116 case MCSymbolRefExpr::VK_GOT
:
119 return IsPCRel
? ELF::R_X86_64_GOTPC64
: ELF::R_X86_64_GOT64
;
121 return IsPCRel
? ELF::R_X86_64_GOTPC32
: ELF::R_X86_64_GOT32
;
125 llvm_unreachable("Unimplemented");
127 case MCSymbolRefExpr::VK_GOTOFF
:
128 assert(Type
== RT64_64
);
130 return ELF::R_X86_64_GOTOFF64
;
131 case MCSymbolRefExpr::VK_TPOFF
:
135 return ELF::R_X86_64_TPOFF64
;
137 return ELF::R_X86_64_TPOFF32
;
141 llvm_unreachable("Unimplemented");
143 case MCSymbolRefExpr::VK_DTPOFF
:
147 return ELF::R_X86_64_DTPOFF64
;
149 return ELF::R_X86_64_DTPOFF32
;
153 llvm_unreachable("Unimplemented");
155 case MCSymbolRefExpr::VK_SIZE
:
159 return ELF::R_X86_64_SIZE64
;
161 return ELF::R_X86_64_SIZE32
;
165 llvm_unreachable("Unimplemented");
167 case MCSymbolRefExpr::VK_TLSCALL
:
168 return ELF::R_X86_64_TLSDESC_CALL
;
169 case MCSymbolRefExpr::VK_TLSDESC
:
170 return ELF::R_X86_64_GOTPC32_TLSDESC
;
171 case MCSymbolRefExpr::VK_TLSGD
:
172 checkIs32(Ctx
, Loc
, Type
);
173 return ELF::R_X86_64_TLSGD
;
174 case MCSymbolRefExpr::VK_GOTTPOFF
:
175 checkIs32(Ctx
, Loc
, Type
);
176 return ELF::R_X86_64_GOTTPOFF
;
177 case MCSymbolRefExpr::VK_TLSLD
:
178 checkIs32(Ctx
, Loc
, Type
);
179 return ELF::R_X86_64_TLSLD
;
180 case MCSymbolRefExpr::VK_PLT
:
181 checkIs32(Ctx
, Loc
, Type
);
182 return ELF::R_X86_64_PLT32
;
183 case MCSymbolRefExpr::VK_GOTPCREL
:
184 checkIs32(Ctx
, Loc
, Type
);
185 // Older versions of ld.bfd/ld.gold/lld
186 // do not support GOTPCRELX/REX_GOTPCRELX,
187 // and we want to keep back-compatibility.
188 if (!Ctx
.getAsmInfo()->canRelaxRelocations())
189 return ELF::R_X86_64_GOTPCREL
;
192 return ELF::R_X86_64_GOTPCREL
;
193 case X86::reloc_riprel_4byte_relax
:
194 return ELF::R_X86_64_GOTPCRELX
;
195 case X86::reloc_riprel_4byte_relax_rex
:
196 case X86::reloc_riprel_4byte_movq_load
:
197 return ELF::R_X86_64_REX_GOTPCRELX
;
202 enum X86_32RelType
{ RT32_32
, RT32_16
, RT32_8
};
204 static X86_32RelType
getType32(X86_64RelType T
) {
207 llvm_unreachable("Unimplemented");
216 llvm_unreachable("unexpected relocation type!");
219 static unsigned getRelocType32(MCContext
&Ctx
,
220 MCSymbolRefExpr::VariantKind Modifier
,
221 X86_32RelType Type
, bool IsPCRel
,
225 llvm_unreachable("Unimplemented");
226 case MCSymbolRefExpr::VK_None
:
227 case MCSymbolRefExpr::VK_X86_ABS8
:
230 return IsPCRel
? ELF::R_386_PC32
: ELF::R_386_32
;
232 return IsPCRel
? ELF::R_386_PC16
: ELF::R_386_16
;
234 return IsPCRel
? ELF::R_386_PC8
: ELF::R_386_8
;
236 case MCSymbolRefExpr::VK_GOT
:
237 assert(Type
== RT32_32
);
239 return ELF::R_386_GOTPC
;
240 // Older versions of ld.bfd/ld.gold/lld do not support R_386_GOT32X and we
241 // want to maintain compatibility.
242 if (!Ctx
.getAsmInfo()->canRelaxRelocations())
243 return ELF::R_386_GOT32
;
245 return Kind
== X86::reloc_signed_4byte_relax
? ELF::R_386_GOT32X
247 case MCSymbolRefExpr::VK_GOTOFF
:
248 assert(Type
== RT32_32
);
250 return ELF::R_386_GOTOFF
;
251 case MCSymbolRefExpr::VK_TPOFF
:
252 assert(Type
== RT32_32
);
254 return ELF::R_386_TLS_LE_32
;
255 case MCSymbolRefExpr::VK_DTPOFF
:
256 assert(Type
== RT32_32
);
258 return ELF::R_386_TLS_LDO_32
;
259 case MCSymbolRefExpr::VK_TLSGD
:
260 assert(Type
== RT32_32
);
262 return ELF::R_386_TLS_GD
;
263 case MCSymbolRefExpr::VK_GOTTPOFF
:
264 assert(Type
== RT32_32
);
266 return ELF::R_386_TLS_IE_32
;
267 case MCSymbolRefExpr::VK_PLT
:
268 assert(Type
== RT32_32
);
269 return ELF::R_386_PLT32
;
270 case MCSymbolRefExpr::VK_INDNTPOFF
:
271 assert(Type
== RT32_32
);
273 return ELF::R_386_TLS_IE
;
274 case MCSymbolRefExpr::VK_NTPOFF
:
275 assert(Type
== RT32_32
);
277 return ELF::R_386_TLS_LE
;
278 case MCSymbolRefExpr::VK_GOTNTPOFF
:
279 assert(Type
== RT32_32
);
281 return ELF::R_386_TLS_GOTIE
;
282 case MCSymbolRefExpr::VK_TLSLDM
:
283 assert(Type
== RT32_32
);
285 return ELF::R_386_TLS_LDM
;
289 unsigned X86ELFObjectWriter::getRelocType(MCContext
&Ctx
, const MCValue
&Target
,
290 const MCFixup
&Fixup
,
291 bool IsPCRel
) const {
292 MCSymbolRefExpr::VariantKind Modifier
= Target
.getAccessVariant();
293 unsigned Kind
= Fixup
.getKind();
294 X86_64RelType Type
= getType64(Kind
, Modifier
, IsPCRel
);
295 if (getEMachine() == ELF::EM_X86_64
)
296 return getRelocType64(Ctx
, Fixup
.getLoc(), Modifier
, Type
, IsPCRel
, Kind
);
298 assert((getEMachine() == ELF::EM_386
|| getEMachine() == ELF::EM_IAMCU
) &&
299 "Unsupported ELF machine type.");
300 return getRelocType32(Ctx
, Modifier
, getType32(Type
), IsPCRel
, Kind
);
303 std::unique_ptr
<MCObjectTargetWriter
>
304 llvm::createX86ELFObjectWriter(bool IsELF64
, uint8_t OSABI
, uint16_t EMachine
) {
305 return llvm::make_unique
<X86ELFObjectWriter
>(IsELF64
, OSABI
, EMachine
);