Skip to content

Commit 9d3ea92

Browse files
committed
X86ELFObjectWriter: Avoid using the MCContext argument
Prepare for removing MCContext from getRelocType functions.
1 parent b65760b commit 9d3ea92

File tree

1 file changed

+36
-31
lines changed

1 file changed

+36
-31
lines changed

llvm/lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
using namespace llvm;
2525

2626
namespace {
27+
enum X86_32RelType { RT32_NONE, RT32_32, RT32_16, RT32_8 };
28+
enum X86_64RelType { RT64_NONE, RT64_64, RT64_32, RT64_32S, RT64_16, RT64_8 };
2729

2830
class X86ELFObjectWriter : public MCELFObjectTargetWriter {
2931
public:
@@ -35,6 +37,15 @@ class X86ELFObjectWriter : public MCELFObjectTargetWriter {
3537
const MCFixup &Fixup, bool IsPCRel) const override;
3638
bool needsRelocateWithSymbol(const MCValue &Val, const MCSymbol &Sym,
3739
unsigned Type) const override;
40+
41+
void checkIs32(SMLoc Loc, X86_64RelType Type) const;
42+
void checkIs64(SMLoc Loc, X86_64RelType Type) const;
43+
unsigned getRelocType32(SMLoc Loc, X86MCExpr::Specifier Specifier,
44+
X86_32RelType Type, bool IsPCRel,
45+
MCFixupKind Kind) const;
46+
unsigned getRelocType64(SMLoc Loc, X86MCExpr::Specifier Specifier,
47+
X86_64RelType Type, bool IsPCRel,
48+
MCFixupKind Kind) const;
3849
};
3950

4051
} // end anonymous namespace
@@ -47,8 +58,6 @@ X86ELFObjectWriter::X86ELFObjectWriter(bool IsELF64, uint8_t OSABI,
4758
(EMachine != ELF::EM_386) &&
4859
(EMachine != ELF::EM_IAMCU)) {}
4960

50-
enum X86_64RelType { RT64_NONE, RT64_64, RT64_32, RT64_32S, RT64_16, RT64_8 };
51-
5261
static X86_64RelType getType64(MCFixupKind Kind,
5362
X86MCExpr::Specifier &Specifier, bool &IsPCRel) {
5463
switch (unsigned(Kind)) {
@@ -89,22 +98,20 @@ static X86_64RelType getType64(MCFixupKind Kind,
8998
}
9099
}
91100

92-
static void checkIs32(MCContext &Ctx, SMLoc Loc, X86_64RelType Type) {
101+
void X86ELFObjectWriter::checkIs32(SMLoc Loc, X86_64RelType Type) const {
93102
if (Type != RT64_32)
94-
Ctx.reportError(Loc,
95-
"32 bit reloc applied to a field with a different size");
103+
reportError(Loc, "32 bit reloc applied to a field with a different size");
96104
}
97105

98-
static void checkIs64(MCContext &Ctx, SMLoc Loc, X86_64RelType Type) {
106+
void X86ELFObjectWriter::checkIs64(SMLoc Loc, X86_64RelType Type) const {
99107
if (Type != RT64_64)
100-
Ctx.reportError(Loc,
101-
"64 bit reloc applied to a field with a different size");
108+
reportError(Loc, "64 bit reloc applied to a field with a different size");
102109
}
103110

104-
static unsigned getRelocType64(MCContext &Ctx, SMLoc Loc,
105-
X86MCExpr::Specifier Specifier,
106-
X86_64RelType Type, bool IsPCRel,
107-
MCFixupKind Kind) {
111+
unsigned X86ELFObjectWriter::getRelocType64(SMLoc Loc,
112+
X86MCExpr::Specifier Specifier,
113+
X86_64RelType Type, bool IsPCRel,
114+
MCFixupKind Kind) const {
108115
switch (Specifier) {
109116
default:
110117
llvm_unreachable("Unimplemented");
@@ -143,7 +150,7 @@ static unsigned getRelocType64(MCContext &Ctx, SMLoc Loc,
143150
case X86MCExpr::VK_GOTOFF:
144151
assert(!IsPCRel);
145152
if (Type != RT64_64)
146-
Ctx.reportError(Loc, "unsupported relocation type");
153+
reportError(Loc, "unsupported relocation type");
147154
return ELF::R_X86_64_GOTOFF64;
148155
case X86MCExpr::VK_TPOFF:
149156
assert(!IsPCRel);
@@ -194,28 +201,28 @@ static unsigned getRelocType64(MCContext &Ctx, SMLoc Loc,
194201
? ELF::R_X86_64_CODE_4_GOTPC32_TLSDESC
195202
: ELF::R_X86_64_GOTPC32_TLSDESC;
196203
case X86MCExpr::VK_TLSGD:
197-
checkIs32(Ctx, Loc, Type);
204+
checkIs32(Loc, Type);
198205
return ELF::R_X86_64_TLSGD;
199206
case X86MCExpr::VK_GOTTPOFF:
200-
checkIs32(Ctx, Loc, Type);
207+
checkIs32(Loc, Type);
201208
if ((unsigned)Kind == X86::reloc_riprel_4byte_movq_load_rex2 ||
202209
(unsigned)Kind == X86::reloc_riprel_4byte_relax_rex2)
203210
return ELF::R_X86_64_CODE_4_GOTTPOFF;
204211
else if ((unsigned)Kind == X86::reloc_riprel_4byte_relax_evex)
205212
return ELF::R_X86_64_CODE_6_GOTTPOFF;
206213
return ELF::R_X86_64_GOTTPOFF;
207214
case X86MCExpr::VK_TLSLD:
208-
checkIs32(Ctx, Loc, Type);
215+
checkIs32(Loc, Type);
209216
return ELF::R_X86_64_TLSLD;
210217
case X86MCExpr::VK_PLT:
211-
checkIs32(Ctx, Loc, Type);
218+
checkIs32(Loc, Type);
212219
return ELF::R_X86_64_PLT32;
213220
case X86MCExpr::VK_GOTPCREL:
214-
checkIs32(Ctx, Loc, Type);
221+
checkIs32(Loc, Type);
215222
// Older versions of ld.bfd/ld.gold/lld
216223
// do not support GOTPCRELX/REX_GOTPCRELX/CODE_4_GOTPCRELX,
217224
// and we want to keep back-compatibility.
218-
if (!Ctx.getTargetOptions()->X86RelaxRelocations)
225+
if (!getContext().getTargetOptions()->X86RelaxRelocations)
219226
return ELF::R_X86_64_GOTPCREL;
220227
switch (unsigned(Kind)) {
221228
default:
@@ -231,20 +238,18 @@ static unsigned getRelocType64(MCContext &Ctx, SMLoc Loc,
231238
}
232239
llvm_unreachable("unexpected relocation type!");
233240
case X86MCExpr::VK_GOTPCREL_NORELAX:
234-
checkIs32(Ctx, Loc, Type);
241+
checkIs32(Loc, Type);
235242
return ELF::R_X86_64_GOTPCREL;
236243
case X86MCExpr::VK_PLTOFF:
237-
checkIs64(Ctx, Loc, Type);
244+
checkIs64(Loc, Type);
238245
return ELF::R_X86_64_PLTOFF64;
239246
}
240247
}
241248

242-
enum X86_32RelType { RT32_NONE, RT32_32, RT32_16, RT32_8 };
243-
244-
static unsigned getRelocType32(MCContext &Ctx, SMLoc Loc,
245-
X86MCExpr::Specifier Specifier,
246-
X86_32RelType Type, bool IsPCRel,
247-
MCFixupKind Kind) {
249+
unsigned X86ELFObjectWriter::getRelocType32(SMLoc Loc,
250+
X86MCExpr::Specifier Specifier,
251+
X86_32RelType Type, bool IsPCRel,
252+
MCFixupKind Kind) const {
248253
switch (Specifier) {
249254
default:
250255
llvm_unreachable("Unimplemented");
@@ -270,7 +275,7 @@ static unsigned getRelocType32(MCContext &Ctx, SMLoc Loc,
270275
return ELF::R_386_GOTPC;
271276
// Older versions of ld.bfd/ld.gold/lld do not support R_386_GOT32X and we
272277
// want to maintain compatibility.
273-
if (!Ctx.getTargetOptions()->X86RelaxRelocations)
278+
if (!getContext().getTargetOptions()->X86RelaxRelocations)
274279
return ELF::R_386_GOT32;
275280

276281
return Kind == MCFixupKind(X86::reloc_signed_4byte_relax)
@@ -330,7 +335,7 @@ static unsigned getRelocType32(MCContext &Ctx, SMLoc Loc,
330335
assert(!IsPCRel);
331336
return ELF::R_386_TLS_LDM;
332337
}
333-
Ctx.reportError(Loc, "unsupported relocation type");
338+
reportError(Loc, "unsupported relocation type");
334339
return ELF::R_386_NONE;
335340
}
336341

@@ -360,7 +365,7 @@ unsigned X86ELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
360365

361366
X86_64RelType Type = getType64(Kind, Specifier, IsPCRel);
362367
if (getEMachine() == ELF::EM_X86_64)
363-
return getRelocType64(Ctx, Fixup.getLoc(), Specifier, Type, IsPCRel, Kind);
368+
return getRelocType64(Fixup.getLoc(), Specifier, Type, IsPCRel, Kind);
364369

365370
assert((getEMachine() == ELF::EM_386 || getEMachine() == ELF::EM_IAMCU) &&
366371
"Unsupported ELF machine type.");
@@ -383,7 +388,7 @@ unsigned X86ELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
383388
RelType = RT32_8;
384389
break;
385390
}
386-
return getRelocType32(Ctx, Fixup.getLoc(), Specifier, RelType, IsPCRel, Kind);
391+
return getRelocType32(Fixup.getLoc(), Specifier, RelType, IsPCRel, Kind);
387392
}
388393

389394
bool X86ELFObjectWriter::needsRelocateWithSymbol(const MCValue &V,

0 commit comments

Comments
 (0)