Skip to content

Commit 411fb3b

Browse files
MaskRaymahesh-attarde
authored andcommitted
MCSectionCOFF: Avoid cast
The object file format specific derived classes are used in context like MCStreamer and MCObjectTargetWriter where the type is statically known. We don't use isa/dyn_cast and we want to eliminate MCSection::SectionVariant in the base class.
1 parent 603186b commit 411fb3b

File tree

6 files changed

+23
-24
lines changed

6 files changed

+23
-24
lines changed

llvm/include/llvm/MC/MCMachObjectWriter.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include "llvm/MC/MCExpr.h"
1717
#include "llvm/MC/MCLinkerOptimizationHint.h"
1818
#include "llvm/MC/MCObjectWriter.h"
19-
#include "llvm/MC/MCSection.h"
19+
#include "llvm/MC/MCSectionMachO.h"
2020
#include "llvm/MC/StringTableBuilder.h"
2121
#include "llvm/Support/Compiler.h"
2222
#include "llvm/Support/EndianStream.h"
@@ -276,7 +276,7 @@ class LLVM_ABI MachObjectWriter final : public MCObjectWriter {
276276
uint64_t SectionDataSize, uint32_t MaxProt,
277277
uint32_t InitProt);
278278

279-
void writeSection(const MCAssembler &Asm, const MCSection &Sec,
279+
void writeSection(const MCAssembler &Asm, const MCSectionMachO &Sec,
280280
uint64_t VMAddr, uint64_t FileOffset, unsigned Flags,
281281
uint64_t RelocationsStart, unsigned NumRelocations);
282282

llvm/lib/MC/MCFragment.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ MCFragment::MCFragment(FragmentType Kind, bool HasInstructions)
3535
}
3636

3737
const MCSymbol *MCFragment::getAtom() const {
38-
return cast<MCSectionMachO>(Parent)->getAtom(LayoutOrder);
38+
return static_cast<const MCSectionMachO *>(Parent)->getAtom(LayoutOrder);
3939
}
4040

4141
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)

llvm/lib/MC/MCMachOStreamer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,13 +443,13 @@ void MCMachOStreamer::finishImpl() {
443443
// Set the fragment atom associations by tracking the last seen atom defining
444444
// symbol.
445445
for (MCSection &Sec : getAssembler()) {
446-
cast<MCSectionMachO>(Sec).allocAtoms();
446+
static_cast<MCSectionMachO &>(Sec).allocAtoms();
447447
const MCSymbol *CurrentAtom = nullptr;
448448
size_t I = 0;
449449
for (MCFragment &Frag : Sec) {
450450
if (const MCSymbol *Symbol = DefiningSymbolMap.lookup(&Frag))
451451
CurrentAtom = Symbol;
452-
cast<MCSectionMachO>(Sec).setAtom(I++, CurrentAtom);
452+
static_cast<MCSectionMachO &>(Sec).setAtom(I++, CurrentAtom);
453453
}
454454
}
455455

llvm/lib/MC/MachObjectWriter.cpp

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ uint64_t MachObjectWriter::getSymbolAddress(const MCSymbol &S) const {
126126
uint64_t MachObjectWriter::getPaddingSize(const MCAssembler &Asm,
127127
const MCSection *Sec) const {
128128
uint64_t EndAddr = getSectionAddress(Sec) + Asm.getSectionAddressSize(*Sec);
129-
unsigned Next = cast<MCSectionMachO>(Sec)->getLayoutOrder() + 1;
129+
unsigned Next =
130+
static_cast<const MCSectionMachO *>(Sec)->getLayoutOrder() + 1;
130131
if (Next >= SectionOrder.size())
131132
return 0;
132133

@@ -259,27 +260,24 @@ void MachObjectWriter::writeSegmentLoadCommand(
259260
}
260261

261262
void MachObjectWriter::writeSection(const MCAssembler &Asm,
262-
const MCSection &Sec, uint64_t VMAddr,
263+
const MCSectionMachO &Sec, uint64_t VMAddr,
263264
uint64_t FileOffset, unsigned Flags,
264265
uint64_t RelocationsStart,
265266
unsigned NumRelocations) {
266-
uint64_t SectionSize = Asm.getSectionAddressSize(Sec);
267-
const MCSectionMachO &Section = cast<MCSectionMachO>(Sec);
268-
269267
// The offset is unused for virtual sections.
270-
if (Section.isBssSection()) {
268+
if (Sec.isBssSection()) {
271269
assert(Asm.getSectionFileSize(Sec) == 0 && "Invalid file size!");
272270
FileOffset = 0;
273271
}
274272

275273
// struct section (68 bytes) or
276274
// struct section_64 (80 bytes)
277275

276+
uint64_t SectionSize = Asm.getSectionAddressSize(Sec);
278277
uint64_t Start = W.OS.tell();
279278
(void) Start;
280-
281-
writeWithPadding(Section.getName(), 16);
282-
writeWithPadding(Section.getSegmentName(), 16);
279+
writeWithPadding(Sec.getName(), 16);
280+
writeWithPadding(Sec.getSegmentName(), 16);
283281
if (is64Bit()) {
284282
W.write<uint64_t>(VMAddr); // address
285283
W.write<uint64_t>(SectionSize); // size
@@ -290,14 +288,14 @@ void MachObjectWriter::writeSection(const MCAssembler &Asm,
290288
assert(isUInt<32>(FileOffset) && "Cannot encode offset of section");
291289
W.write<uint32_t>(FileOffset);
292290

293-
W.write<uint32_t>(Log2(Section.getAlign()));
291+
W.write<uint32_t>(Log2(Sec.getAlign()));
294292
assert((!NumRelocations || isUInt<32>(RelocationsStart)) &&
295293
"Cannot encode offset of relocations");
296294
W.write<uint32_t>(NumRelocations ? RelocationsStart : 0);
297295
W.write<uint32_t>(NumRelocations);
298296
W.write<uint32_t>(Flags);
299297
W.write<uint32_t>(IndirectSymBase.lookup(&Sec)); // reserved1
300-
W.write<uint32_t>(Section.getStubSize()); // reserved2
298+
W.write<uint32_t>(Sec.getStubSize()); // reserved2
301299
if (is64Bit())
302300
W.write<uint32_t>(0); // reserved3
303301

@@ -531,7 +529,7 @@ void MachObjectWriter::bindIndirectSymbols(MCAssembler &Asm) {
531529
// Report errors for use of .indirect_symbol not in a symbol pointer section
532530
// or stub section.
533531
for (IndirectSymbolData &ISD : IndirectSymbols) {
534-
const MCSectionMachO &Section = cast<MCSectionMachO>(*ISD.Section);
532+
const MCSectionMachO &Section = static_cast<MCSectionMachO &>(*ISD.Section);
535533

536534
if (Section.getType() != MachO::S_NON_LAZY_SYMBOL_POINTERS &&
537535
Section.getType() != MachO::S_LAZY_SYMBOL_POINTERS &&
@@ -545,7 +543,7 @@ void MachObjectWriter::bindIndirectSymbols(MCAssembler &Asm) {
545543

546544
// Bind non-lazy symbol pointers first.
547545
for (auto [IndirectIndex, ISD] : enumerate(IndirectSymbols)) {
548-
const auto &Section = cast<MCSectionMachO>(*ISD.Section);
546+
const auto &Section = static_cast<MCSectionMachO &>(*ISD.Section);
549547

550548
if (Section.getType() != MachO::S_NON_LAZY_SYMBOL_POINTERS &&
551549
Section.getType() != MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
@@ -559,7 +557,7 @@ void MachObjectWriter::bindIndirectSymbols(MCAssembler &Asm) {
559557

560558
// Then lazy symbol pointers and symbol stubs.
561559
for (auto [IndirectIndex, ISD] : enumerate(IndirectSymbols)) {
562-
const auto &Section = cast<MCSectionMachO>(*ISD.Section);
560+
const auto &Section = static_cast<MCSectionMachO &>(*ISD.Section);
563561

564562
if (Section.getType() != MachO::S_LAZY_SYMBOL_POINTERS &&
565563
Section.getType() != MachO::S_SYMBOL_STUBS)
@@ -684,13 +682,13 @@ void MachObjectWriter::computeSectionAddresses(const MCAssembler &Asm) {
684682
for (MCSection &Sec : Asm) {
685683
if (!Sec.isBssSection()) {
686684
SectionOrder.push_back(&Sec);
687-
cast<MCSectionMachO>(Sec).setLayoutOrder(i++);
685+
static_cast<MCSectionMachO &>(Sec).setLayoutOrder(i++);
688686
}
689687
}
690688
for (MCSection &Sec : Asm) {
691689
if (Sec.isBssSection()) {
692690
SectionOrder.push_back(&Sec);
693-
cast<MCSectionMachO>(Sec).setLayoutOrder(i++);
691+
static_cast<MCSectionMachO &>(Sec).setLayoutOrder(i++);
694692
}
695693
}
696694

@@ -907,7 +905,7 @@ uint64_t MachObjectWriter::writeObject() {
907905
// ... and then the section headers.
908906
uint64_t RelocTableEnd = SectionDataStart + SectionDataFileSize;
909907
for (const MCSection &Section : Asm) {
910-
const auto &Sec = cast<MCSectionMachO>(Section);
908+
const auto &Sec = static_cast<const MCSectionMachO &>(Section);
911909
std::vector<RelAndSymbol> &Relocs = Relocations[&Sec];
912910
unsigned NumRelocs = Relocs.size();
913911
uint64_t SectionStart = SectionDataStart + getSectionAddress(&Sec);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ static bool canUseLocalRelocation(const MCSectionMachO &Section,
132132
// But only if they don't point to a few forbidden sections.
133133
if (!Symbol.isInSection())
134134
return true;
135-
const MCSectionMachO &RefSec = cast<MCSectionMachO>(Symbol.getSection());
135+
const MCSectionMachO &RefSec =
136+
static_cast<MCSectionMachO &>(Symbol.getSection());
136137
if (RefSec.getType() == MachO::S_CSTRING_LITERALS)
137138
return false;
138139

llvm/tools/dsymutil/MachOUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ static bool createDwarfSegment(const MCAssembler& Asm,uint64_t VMAddr, uint64_t
331331
/* InitProt =*/3);
332332

333333
for (unsigned int i = 0, n = Writer.getSectionOrder().size(); i != n; ++i) {
334-
MCSection *Sec = Writer.getSectionOrder()[i];
334+
auto *Sec = static_cast<MCSectionMachO *>(Writer.getSectionOrder()[i]);
335335
if (!Asm.getSectionFileSize(*Sec))
336336
continue;
337337

0 commit comments

Comments
 (0)