Skip to content

Commit fec1b6f

Browse files
committed
[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.
1 parent ffec315 commit fec1b6f

File tree

3 files changed

+35
-24
lines changed

3 files changed

+35
-24
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: 34 additions & 3 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,7 @@ class AArch64ELFStreamer : public MCELFStreamer {
248248
emitDataMappingSymbol();
249249
MCObjectStreamer::emitFill(NumBytes, FillValue, Loc);
250250
}
251+
251252
private:
252253
enum ElfMappingSymbol {
253254
EMS_None,
@@ -283,7 +284,6 @@ class AArch64ELFStreamer : public MCELFStreamer {
283284
DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
284285
ElfMappingSymbol LastEMS;
285286
};
286-
287287
} // end anonymous namespace
288288

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

302+
void AArch64TargetELFStreamer::finish() {
303+
AArch64TargetStreamer::finish();
304+
AArch64ELFStreamer &S = getStreamer();
305+
MCContext &Ctx = S.getContext();
306+
auto &Asm = S.getAssembler();
307+
MCSectionELF *MemtagSec = nullptr;
308+
for (const MCSymbol &Symbol : Asm.symbols()) {
309+
const auto &Sym = cast<MCSymbolELF>(Symbol);
310+
if (Sym.isMemtag()) {
311+
MemtagSec = Ctx.getELFSection(".memtag.globals.static",
312+
ELF::SHT_AARCH64_MEMTAG_GLOBALS_STATIC, 0);
313+
break;
314+
}
315+
}
316+
if (!MemtagSec)
317+
return;
318+
319+
// switchSection registers the section symbol and invalidates symbols(). We
320+
// need a separate symbols() loop.
321+
S.switchSection(MemtagSec);
322+
const auto *Zero = MCConstantExpr::create(0, Ctx);
323+
for (const MCSymbol &Symbol : Asm.symbols()) {
324+
const auto &Sym = cast<MCSymbolELF>(Symbol);
325+
if (!Sym.isMemtag())
326+
continue;
327+
auto *SRE = MCSymbolRefExpr::create(&Sym, MCSymbolRefExpr::VK_None, Ctx);
328+
(void)S.emitRelocDirective(*Zero, "BFD_RELOC_NONE", SRE, SMLoc(),
329+
*Ctx.getSubtargetInfo());
330+
}
331+
}
332+
302333
MCTargetStreamer *
303334
llvm::createAArch64AsmTargetStreamer(MCStreamer &S, formatted_raw_ostream &OS,
304335
MCInstPrinter *InstPrint,

llvm/lib/Target/AArch64/MCTargetDesc/AArch64TargetStreamer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ class AArch64TargetELFStreamer : public AArch64TargetStreamer {
9393

9494
void emitInst(uint32_t Inst) override;
9595
void emitDirectiveVariantPCS(MCSymbol *Symbol) override;
96+
void finish() override;
9697

9798
public:
9899
AArch64TargetELFStreamer(MCStreamer &S) : AArch64TargetStreamer(S) {}

0 commit comments

Comments
 (0)