Skip to content

Commit 5997e7d

Browse files
committed
Revert "[MC] Move ELFWriter::createMemtagRelocs to AArch64ELFStreamer::finishImpl"
This reverts commit 9d63506. There is a heap-use-after-free.
1 parent 4061354 commit 5997e7d

File tree

2 files changed

+23
-26
lines changed

2 files changed

+23
-26
lines changed

llvm/lib/MC/ELFObjectWriter.cpp

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

196+
void createMemtagRelocs(MCAssembler &Asm);
197+
196198
void writeSectionHeader(const MCAsmLayout &Layout,
197199
const SectionIndexMapTy &SectionIndexMap,
198200
const SectionOffsetsTy &SectionOffsets);
@@ -614,6 +616,23 @@ bool ELFWriter::isInSymtab(const MCAsmLayout &Layout, const MCSymbolELF &Symbol,
614616
return true;
615617
}
616618

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+
617636
void ELFWriter::computeSymbolTable(
618637
MCAssembler &Asm, const MCAsmLayout &Layout,
619638
const SectionIndexMapTy &SectionIndexMap, const RevGroupMapTy &RevGroupMap,
@@ -1075,6 +1094,8 @@ uint64_t ELFWriter::writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) {
10751094
Ctx.getELFSection(".strtab", ELF::SHT_STRTAB, 0);
10761095
StringTableIndex = addToSectionTable(StrtabSection);
10771096

1097+
createMemtagRelocs(Asm);
1098+
10781099
RevGroupMapTy RevGroupMap;
10791100
SectionIndexMapTy SectionIndexMap;
10801101

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

Lines changed: 2 additions & 26 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/MCSectionELF.h"
31+
#include "llvm/MC/MCSection.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 = 0) override {
186+
void changeSection(MCSection *Section, uint32_t Subsection) 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,9 +248,6 @@ class AArch64ELFStreamer : public MCELFStreamer {
248248
emitDataMappingSymbol();
249249
MCObjectStreamer::emitFill(NumBytes, FillValue, Loc);
250250
}
251-
252-
void finishImpl() override;
253-
254251
private:
255252
enum ElfMappingSymbol {
256253
EMS_None,
@@ -287,27 +284,6 @@ class AArch64ELFStreamer : public MCELFStreamer {
287284
ElfMappingSymbol LastEMS;
288285
};
289286

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-
311287
} // end anonymous namespace
312288

313289
AArch64ELFStreamer &AArch64TargetELFStreamer::getStreamer() {

0 commit comments

Comments
 (0)