Skip to content

Commit

Permalink
[MC] Move ELFWriter::createMemtagRelocs to AArch64TargetELFStreamer::…
Browse files Browse the repository at this point in the history
…finish

Follow-up to https://reviews.llvm.org/D128958

* Move target-specific code away from the generic ELFWriter.
* All sections should have been created before MCAssembler::layout.
* Remove one `registerSection` use, which should be considered private to MCAssembler.
  • Loading branch information
MaskRay committed Jun 24, 2024
1 parent ffec315 commit fec1b6f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 24 deletions.
21 changes: 0 additions & 21 deletions llvm/lib/MC/ELFObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,6 @@ struct ELFWriter {
MCSectionELF *createRelocationSection(MCContext &Ctx,
const MCSectionELF &Sec);

void createMemtagRelocs(MCAssembler &Asm);

void writeSectionHeader(const MCAsmLayout &Layout,
const SectionIndexMapTy &SectionIndexMap,
const SectionOffsetsTy &SectionOffsets);
Expand Down Expand Up @@ -616,23 +614,6 @@ bool ELFWriter::isInSymtab(const MCAsmLayout &Layout, const MCSymbolELF &Symbol,
return true;
}

void ELFWriter::createMemtagRelocs(MCAssembler &Asm) {
MCSectionELF *MemtagRelocs = nullptr;
for (const MCSymbol &Sym : Asm.symbols()) {
const auto &SymE = cast<MCSymbolELF>(Sym);
if (!SymE.isMemtag())
continue;
if (MemtagRelocs == nullptr) {
MemtagRelocs = OWriter.TargetObjectWriter->getMemtagRelocsSection(Asm.getContext());
if (MemtagRelocs == nullptr)
report_fatal_error("Tagged globals are not available on this architecture.");
Asm.registerSection(*MemtagRelocs);
}
ELFRelocationEntry Rec(0, &SymE, ELF::R_AARCH64_NONE, 0, nullptr, 0);
OWriter.Relocations[MemtagRelocs].push_back(Rec);
}
}

void ELFWriter::computeSymbolTable(
MCAssembler &Asm, const MCAsmLayout &Layout,
const SectionIndexMapTy &SectionIndexMap, const RevGroupMapTy &RevGroupMap,
Expand Down Expand Up @@ -1094,8 +1075,6 @@ uint64_t ELFWriter::writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) {
Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0);
StringTableIndex = addToSectionTable(StrtabSection);

createMemtagRelocs(Asm);

RevGroupMapTy RevGroupMap;
SectionIndexMapTy SectionIndexMap;

Expand Down
37 changes: 34 additions & 3 deletions llvm/lib/Target/AArch64/MCTargetDesc/AArch64ELFStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCInst.h"
#include "llvm/MC/MCObjectWriter.h"
#include "llvm/MC/MCSection.h"
#include "llvm/MC/MCSectionELF.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/MC/MCSymbolELF.h"
Expand Down Expand Up @@ -183,7 +183,7 @@ class AArch64ELFStreamer : public MCELFStreamer {
std::move(Emitter)),
MappingSymbolCounter(0), LastEMS(EMS_None) {}

void changeSection(MCSection *Section, uint32_t Subsection) override {
void changeSection(MCSection *Section, uint32_t Subsection = 0) override {
// We have to keep track of the mapping symbol state of any sections we
// use. Each one should start off as EMS_None, which is provided as the
// default constructor by DenseMap::lookup.
Expand Down Expand Up @@ -248,6 +248,7 @@ class AArch64ELFStreamer : public MCELFStreamer {
emitDataMappingSymbol();
MCObjectStreamer::emitFill(NumBytes, FillValue, Loc);
}

private:
enum ElfMappingSymbol {
EMS_None,
Expand Down Expand Up @@ -283,7 +284,6 @@ class AArch64ELFStreamer : public MCELFStreamer {
DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
ElfMappingSymbol LastEMS;
};

} // end anonymous namespace

AArch64ELFStreamer &AArch64TargetELFStreamer::getStreamer() {
Expand All @@ -299,6 +299,37 @@ void AArch64TargetELFStreamer::emitDirectiveVariantPCS(MCSymbol *Symbol) {
cast<MCSymbolELF>(Symbol)->setOther(ELF::STO_AARCH64_VARIANT_PCS);
}

void AArch64TargetELFStreamer::finish() {
AArch64TargetStreamer::finish();
AArch64ELFStreamer &S = getStreamer();
MCContext &Ctx = S.getContext();
auto &Asm = S.getAssembler();
MCSectionELF *MemtagSec = nullptr;
for (const MCSymbol &Symbol : Asm.symbols()) {
const auto &Sym = cast<MCSymbolELF>(Symbol);
if (Sym.isMemtag()) {
MemtagSec = Ctx.getELFSection(".memtag.globals.static",
ELF::SHT_AARCH64_MEMTAG_GLOBALS_STATIC, 0);
break;
}
}
if (!MemtagSec)
return;

// switchSection registers the section symbol and invalidates symbols(). We
// need a separate symbols() loop.
S.switchSection(MemtagSec);
const auto *Zero = MCConstantExpr::create(0, Ctx);
for (const MCSymbol &Symbol : Asm.symbols()) {
const auto &Sym = cast<MCSymbolELF>(Symbol);
if (!Sym.isMemtag())
continue;
auto *SRE = MCSymbolRefExpr::create(&Sym, MCSymbolRefExpr::VK_None, Ctx);
(void)S.emitRelocDirective(*Zero, "BFD_RELOC_NONE", SRE, SMLoc(),
*Ctx.getSubtargetInfo());
}
}

MCTargetStreamer *
llvm::createAArch64AsmTargetStreamer(MCStreamer &S, formatted_raw_ostream &OS,
MCInstPrinter *InstPrint,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class AArch64TargetELFStreamer : public AArch64TargetStreamer {

void emitInst(uint32_t Inst) override;
void emitDirectiveVariantPCS(MCSymbol *Symbol) override;
void finish() override;

public:
AArch64TargetELFStreamer(MCStreamer &S) : AArch64TargetStreamer(S) {}
Expand Down

0 comments on commit fec1b6f

Please sign in to comment.