[llvm] fec1b6f - [MC] Move ELFWriter::createMemtagRelocs to AArch64TargetELFStreamer::finish
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Sun Jun 23 21:14:40 PDT 2024
Author: Fangrui Song
Date: 2024-06-23T21:14:34-07:00
New Revision: fec1b6f9d3cf5347b67ffb2078c995eb496acf47
URL: https://github.com/llvm/llvm-project/commit/fec1b6f9d3cf5347b67ffb2078c995eb496acf47
DIFF: https://github.com/llvm/llvm-project/commit/fec1b6f9d3cf5347b67ffb2078c995eb496acf47.diff
LOG: [MC] Move ELFWriter::createMemtagRelocs to AArch64TargetELFStreamer::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.
Added:
Modified:
llvm/lib/MC/ELFObjectWriter.cpp
llvm/lib/Target/AArch64/MCTargetDesc/AArch64ELFStreamer.cpp
llvm/lib/Target/AArch64/MCTargetDesc/AArch64TargetStreamer.h
Removed:
################################################################################
diff --git a/llvm/lib/MC/ELFObjectWriter.cpp b/llvm/lib/MC/ELFObjectWriter.cpp
index b8ef2654ed6e3..5724c5c785603 100644
--- a/llvm/lib/MC/ELFObjectWriter.cpp
+++ b/llvm/lib/MC/ELFObjectWriter.cpp
@@ -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);
@@ -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,
@@ -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;
diff --git a/llvm/lib/Target/AArch64/MCTargetDesc/AArch64ELFStreamer.cpp b/llvm/lib/Target/AArch64/MCTargetDesc/AArch64ELFStreamer.cpp
index b28ff9e0fd872..7dba22c066dcd 100644
--- a/llvm/lib/Target/AArch64/MCTargetDesc/AArch64ELFStreamer.cpp
+++ b/llvm/lib/Target/AArch64/MCTargetDesc/AArch64ELFStreamer.cpp
@@ -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"
@@ -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.
@@ -248,6 +248,7 @@ class AArch64ELFStreamer : public MCELFStreamer {
emitDataMappingSymbol();
MCObjectStreamer::emitFill(NumBytes, FillValue, Loc);
}
+
private:
enum ElfMappingSymbol {
EMS_None,
@@ -283,7 +284,6 @@ class AArch64ELFStreamer : public MCELFStreamer {
DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
ElfMappingSymbol LastEMS;
};
-
} // end anonymous namespace
AArch64ELFStreamer &AArch64TargetELFStreamer::getStreamer() {
@@ -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,
diff --git a/llvm/lib/Target/AArch64/MCTargetDesc/AArch64TargetStreamer.h b/llvm/lib/Target/AArch64/MCTargetDesc/AArch64TargetStreamer.h
index e8a9dc445b96b..ac441ae3b603f 100644
--- a/llvm/lib/Target/AArch64/MCTargetDesc/AArch64TargetStreamer.h
+++ b/llvm/lib/Target/AArch64/MCTargetDesc/AArch64TargetStreamer.h
@@ -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) {}
More information about the llvm-commits
mailing list