Skip to content

Commit 5d5ce06

Browse files
committed
MCSymbolELF: Migrate away from classof
The object file format specific derived classes are used in context where the type is statically known. We don't use isa/dyn_cast and we want to eliminate MCSymbol::Kind in the base class.
1 parent b51ff27 commit 5d5ce06

File tree

6 files changed

+36
-27
lines changed

6 files changed

+36
-27
lines changed

llvm/include/llvm/MC/MCContext.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ class MCContext {
392392
Environment getObjectFileType() const { return Env; }
393393
bool isELF() const { return Env == IsELF; }
394394
bool isMachO() const { return Env == IsMachO; }
395+
bool isXCOFF() const { return Env == IsXCOFF; }
395396

396397
const StringRef &getSwift5ReflectionSegmentName() const {
397398
return Swift5ReflectionSegmentName;

llvm/lib/MC/MCObjectFileInfo.cpp

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,9 +1135,10 @@ MCObjectFileInfo::getCallGraphSection(const MCSection &TextSec) const {
11351135
Flags |= ELF::SHF_GROUP;
11361136
}
11371137

1138-
return Ctx->getELFSection(".callgraph", ELF::SHT_PROGBITS, Flags, 0,
1139-
GroupName, true, ElfSec.getUniqueID(),
1140-
cast<MCSymbolELF>(TextSec.getBeginSymbol()));
1138+
return Ctx->getELFSection(
1139+
".callgraph", ELF::SHT_PROGBITS, Flags, 0, GroupName, true,
1140+
ElfSec.getUniqueID(),
1141+
static_cast<const MCSymbolELF *>(TextSec.getBeginSymbol()));
11411142
}
11421143

11431144
MCSection *
@@ -1154,9 +1155,10 @@ MCObjectFileInfo::getStackSizesSection(const MCSection &TextSec) const {
11541155
Flags |= ELF::SHF_GROUP;
11551156
}
11561157

1157-
return Ctx->getELFSection(".stack_sizes", ELF::SHT_PROGBITS, Flags, 0,
1158-
GroupName, true, ElfSec.getUniqueID(),
1159-
cast<MCSymbolELF>(TextSec.getBeginSymbol()));
1158+
return Ctx->getELFSection(
1159+
".stack_sizes", ELF::SHT_PROGBITS, Flags, 0, GroupName, true,
1160+
ElfSec.getUniqueID(),
1161+
static_cast<const MCSymbolELF *>(TextSec.getBeginSymbol()));
11601162
}
11611163

11621164
MCSection *
@@ -1174,9 +1176,10 @@ MCObjectFileInfo::getBBAddrMapSection(const MCSection &TextSec) const {
11741176

11751177
// Use the text section's begin symbol and unique ID to create a separate
11761178
// .llvm_bb_addr_map section associated with every unique text section.
1177-
return Ctx->getELFSection(".llvm_bb_addr_map", ELF::SHT_LLVM_BB_ADDR_MAP,
1178-
Flags, 0, GroupName, true, ElfSec.getUniqueID(),
1179-
cast<MCSymbolELF>(TextSec.getBeginSymbol()));
1179+
return Ctx->getELFSection(
1180+
".llvm_bb_addr_map", ELF::SHT_LLVM_BB_ADDR_MAP, Flags, 0, GroupName, true,
1181+
ElfSec.getUniqueID(),
1182+
static_cast<const MCSymbolELF *>(TextSec.getBeginSymbol()));
11801183
}
11811184

11821185
MCSection *
@@ -1192,10 +1195,10 @@ MCObjectFileInfo::getKCFITrapSection(const MCSection &TextSec) const {
11921195
Flags |= ELF::SHF_GROUP;
11931196
}
11941197

1195-
return Ctx->getELFSection(".kcfi_traps", ELF::SHT_PROGBITS, Flags, 0,
1196-
GroupName,
1197-
/*IsComdat=*/true, ElfSec.getUniqueID(),
1198-
cast<MCSymbolELF>(TextSec.getBeginSymbol()));
1198+
return Ctx->getELFSection(
1199+
".kcfi_traps", ELF::SHT_PROGBITS, Flags, 0, GroupName,
1200+
/*IsComdat=*/true, ElfSec.getUniqueID(),
1201+
static_cast<const MCSymbolELF *>(TextSec.getBeginSymbol()));
11991202
}
12001203

12011204
MCSection *
@@ -1211,9 +1214,10 @@ MCObjectFileInfo::getPseudoProbeSection(const MCSection &TextSec) const {
12111214
Flags |= ELF::SHF_GROUP;
12121215
}
12131216

1214-
return Ctx->getELFSection(PseudoProbeSection->getName(), ELF::SHT_PROGBITS,
1215-
Flags, 0, GroupName, true, ElfSec.getUniqueID(),
1216-
cast<MCSymbolELF>(TextSec.getBeginSymbol()));
1217+
return Ctx->getELFSection(
1218+
PseudoProbeSection->getName(), ELF::SHT_PROGBITS, Flags, 0, GroupName,
1219+
true, ElfSec.getUniqueID(),
1220+
static_cast<const MCSymbolELF *>(TextSec.getBeginSymbol()));
12171221
}
12181222

12191223
MCSection *
@@ -1261,7 +1265,7 @@ MCSection *MCObjectFileInfo::getPCSection(StringRef Name,
12611265
GroupName = Group->getName();
12621266
Flags |= ELF::SHF_GROUP;
12631267
}
1264-
return Ctx->getELFSection(Name, ELF::SHT_PROGBITS, Flags, 0, GroupName, true,
1265-
ElfSec.getUniqueID(),
1266-
cast<MCSymbolELF>(TextSec->getBeginSymbol()));
1268+
return Ctx->getELFSection(
1269+
Name, ELF::SHT_PROGBITS, Flags, 0, GroupName, true, ElfSec.getUniqueID(),
1270+
static_cast<const MCSymbolELF *>(TextSec->getBeginSymbol()));
12671271
}

llvm/lib/MC/MCParser/ELFAsmParser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ bool ELFAsmParser::parseDirectiveSize(StringRef, SMLoc) {
200200
StringRef Name;
201201
if (getParser().parseIdentifier(Name))
202202
return TokError("expected identifier");
203-
MCSymbolELF *Sym = cast<MCSymbolELF>(getContext().getOrCreateSymbol(Name));
203+
auto *Sym = static_cast<MCSymbolELF *>(getContext().getOrCreateSymbol(Name));
204204

205205
if (getLexer().isNot(AsmToken::Comma))
206206
return TokError("expected comma");
@@ -466,7 +466,7 @@ bool ELFAsmParser::parseLinkedToSym(MCSymbolELF *&LinkedToSym) {
466466
}
467467
return TokError("invalid linked-to symbol");
468468
}
469-
LinkedToSym = dyn_cast_or_null<MCSymbolELF>(getContext().lookupSymbol(Name));
469+
LinkedToSym = static_cast<MCSymbolELF *>(getContext().lookupSymbol(Name));
470470
if (!LinkedToSym || !LinkedToSym->isInSection())
471471
return Error(StartLoc, "linked-to symbol is not in a section: " + Name);
472472
return false;

llvm/lib/Target/AArch64/MCTargetDesc/AArch64ELFObjectWriter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,8 @@ bool AArch64ELFObjectWriter::needsRelocateWithSymbol(const MCValue &Val,
488488
// this global needs to be tagged. In addition, the linker needs to know
489489
// whether to emit a special addend when relocating `end` symbols, and this
490490
// can only be determined by the attributes of the symbol itself.
491-
if (Val.getAddSym() && cast<MCSymbolELF>(Val.getAddSym())->isMemtag())
491+
if (Val.getAddSym() &&
492+
static_cast<const MCSymbolELF *>(Val.getAddSym())->isMemtag())
492493
return true;
493494

494495
if ((Val.getSpecifier() & AArch64::S_GOT) == AArch64::S_GOT)

llvm/lib/Target/Mips/MCTargetDesc/MipsELFObjectWriter.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ bool MipsELFObjectWriter::needsRelocateWithSymbol(const MCValue &V,
450450
needsRelocateWithSymbol(V, (Type >> 8) & 0xff) ||
451451
needsRelocateWithSymbol(V, (Type >> 16) & 0xff);
452452

453+
auto *Sym = static_cast<const MCSymbolELF *>(V.getAddSym());
453454
switch (Type) {
454455
default:
455456
errs() << Type << "\n";
@@ -481,7 +482,7 @@ bool MipsELFObjectWriter::needsRelocateWithSymbol(const MCValue &V,
481482
// FIXME: It should be safe to return false for the STO_MIPS_MICROMIPS but
482483
// we neglect to handle the adjustment to the LSB of the addend that
483484
// it causes in applyFixup() and similar.
484-
if (cast<MCSymbolELF>(V.getAddSym())->getOther() & ELF::STO_MIPS_MICROMIPS)
485+
if (Sym->getOther() & ELF::STO_MIPS_MICROMIPS)
485486
return true;
486487
return false;
487488

@@ -492,7 +493,7 @@ bool MipsELFObjectWriter::needsRelocateWithSymbol(const MCValue &V,
492493
case ELF::R_MIPS_16:
493494
case ELF::R_MIPS_32:
494495
case ELF::R_MIPS_GPREL32:
495-
if (cast<MCSymbolELF>(V.getAddSym())->getOther() & ELF::STO_MIPS_MICROMIPS)
496+
if (Sym->getOther() & ELF::STO_MIPS_MICROMIPS)
496497
return true;
497498
[[fallthrough]];
498499
case ELF::R_MIPS_26:

llvm/lib/Target/PowerPC/MCTargetDesc/PPCAsmBackend.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "llvm/BinaryFormat/MachO.h"
1414
#include "llvm/MC/MCAsmBackend.h"
1515
#include "llvm/MC/MCAssembler.h"
16+
#include "llvm/MC/MCContext.h"
1617
#include "llvm/MC/MCELFObjectWriter.h"
1718
#include "llvm/MC/MCMachObjectWriter.h"
1819
#include "llvm/MC/MCObjectWriter.h"
@@ -112,14 +113,15 @@ class PPCAsmBackend : public MCAsmBackend {
112113
// to resolve the fixup directly. Emit a relocation and leave
113114
// resolution of the final target address to the linker.
114115
if (const auto *A = Target.getAddSym()) {
115-
if (const auto *S = dyn_cast<MCSymbolELF>(A)) {
116+
if (getContext().isELF()) {
116117
// The "other" values are stored in the last 6 bits of the second
117118
// byte. The traditional defines for STO values assume the full byte
118119
// and thus the shift to pack it.
119-
unsigned Other = S->getOther() << 2;
120+
unsigned Other = static_cast<const MCSymbolELF *>(A)->getOther() << 2;
120121
if ((Other & ELF::STO_PPC64_LOCAL_MASK) != 0)
121122
return true;
122-
} else if (const auto *S = dyn_cast<MCSymbolXCOFF>(A)) {
123+
} else if (getContext().isXCOFF()) {
124+
auto *S = static_cast<const MCSymbolXCOFF *>(A);
123125
return !Target.isAbsolute() && S->isExternal() &&
124126
S->getStorageClass() == XCOFF::C_WEAKEXT;
125127
}

0 commit comments

Comments
 (0)