Skip to content

Commit 9d63506

Browse files
committed
[MC] Move ELFWriter::createMemtagRelocs to AArch64ELFStreamer::finishImpl
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.
1 parent 6082a50 commit 9d63506

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

llvm/lib/MC/ELFObjectWriter.cpp

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,6 @@ struct ELFWriter {
193193
MCSectionELF *createRelocationSection(MCContext &Ctx,
194194
const MCSectionELF &Sec);
195195

196-
void createMemtagRelocs(MCAssembler &Asm);
197-
198196
void writeSectionHeader(const MCAsmLayout &Layout,
199197
const SectionIndexMapTy &SectionIndexMap,
200198
const SectionOffsetsTy &SectionOffsets);
@@ -616,23 +614,6 @@ bool ELFWriter::isInSymtab(const MCAsmLayout &Layout, const MCSymbolELF &Symbol,
616614
return true;
617615
}
618616

619-
void ELFWriter::createMemtagRelocs(MCAssembler &Asm) {
620-
MCSectionELF *MemtagRelocs = nullptr;
621-
for (const MCSymbol &Sym : Asm.symbols()) {
622-
const auto &SymE = cast<MCSymbolELF>(Sym);
623-
if (!SymE.isMemtag())
624-
continue;
625-
if (MemtagRelocs == nullptr) {
626-
MemtagRelocs = OWriter.TargetObjectWriter->getMemtagRelocsSection(Asm.getContext());
627-
if (MemtagRelocs == nullptr)
628-
report_fatal_error("Tagged globals are not available on this architecture.");
629-
Asm.registerSection(*MemtagRelocs);
630-
}
631-
ELFRelocationEntry Rec(0, &SymE, ELF::R_AARCH64_NONE, 0, nullptr, 0);
632-
OWriter.Relocations[MemtagRelocs].push_back(Rec);
633-
}
634-
}
635-
636617
void ELFWriter::computeSymbolTable(
637618
MCAssembler &Asm, const MCAsmLayout &Layout,
638619
const SectionIndexMapTy &SectionIndexMap, const RevGroupMapTy &RevGroupMap,
@@ -1094,8 +1075,6 @@ uint64_t ELFWriter::writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) {
10941075
Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0);
10951076
StringTableIndex = addToSectionTable(StrtabSection);
10961077

1097-
createMemtagRelocs(Asm);
1098-
10991078
RevGroupMapTy RevGroupMap;
11001079
SectionIndexMapTy SectionIndexMap;
11011080

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#include "llvm/MC/MCExpr.h"
2929
#include "llvm/MC/MCInst.h"
3030
#include "llvm/MC/MCObjectWriter.h"
31-
#include "llvm/MC/MCSection.h"
31+
#include "llvm/MC/MCSectionELF.h"
3232
#include "llvm/MC/MCStreamer.h"
3333
#include "llvm/MC/MCSubtargetInfo.h"
3434
#include "llvm/MC/MCSymbolELF.h"
@@ -183,7 +183,7 @@ class AArch64ELFStreamer : public MCELFStreamer {
183183
std::move(Emitter)),
184184
MappingSymbolCounter(0), LastEMS(EMS_None) {}
185185

186-
void changeSection(MCSection *Section, uint32_t Subsection) override {
186+
void changeSection(MCSection *Section, uint32_t Subsection = 0) override {
187187
// We have to keep track of the mapping symbol state of any sections we
188188
// use. Each one should start off as EMS_None, which is provided as the
189189
// default constructor by DenseMap::lookup.
@@ -248,6 +248,9 @@ class AArch64ELFStreamer : public MCELFStreamer {
248248
emitDataMappingSymbol();
249249
MCObjectStreamer::emitFill(NumBytes, FillValue, Loc);
250250
}
251+
252+
void finishImpl() override;
253+
251254
private:
252255
enum ElfMappingSymbol {
253256
EMS_None,
@@ -284,6 +287,27 @@ class AArch64ELFStreamer : public MCELFStreamer {
284287
ElfMappingSymbol LastEMS;
285288
};
286289

290+
void AArch64ELFStreamer::finishImpl() {
291+
MCContext &Ctx = getContext();
292+
auto &Asm = getAssembler();
293+
MCSectionELF *MemtagSec = nullptr;
294+
const auto *Zero = MCConstantExpr::create(0, Ctx);
295+
for (const MCSymbol &Symbol : Asm.symbols()) {
296+
const auto &Sym = cast<MCSymbolELF>(Symbol);
297+
if (!Sym.isMemtag())
298+
continue;
299+
if (!MemtagSec) {
300+
MemtagSec = Ctx.getELFSection(".memtag.globals.static",
301+
ELF::SHT_AARCH64_MEMTAG_GLOBALS_STATIC, 0);
302+
switchSection(MemtagSec);
303+
}
304+
auto *SRE = MCSymbolRefExpr::create(&Sym, MCSymbolRefExpr::VK_None, Ctx);
305+
(void)MCObjectStreamer::emitRelocDirective(
306+
*Zero, "BFD_RELOC_NONE", SRE, SMLoc(), *Ctx.getSubtargetInfo());
307+
}
308+
MCELFStreamer::finishImpl();
309+
}
310+
287311
} // end anonymous namespace
288312

289313
AArch64ELFStreamer &AArch64TargetELFStreamer::getStreamer() {

0 commit comments

Comments
 (0)