From 5511861e6dc81e702587bb4d07deba35571db293 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Sun, 5 Jan 2020 14:02:18 -0800 Subject: [PATCH] [MC][ARM] Delete MCSection::HasData and move SHF_ARM_PURECODE logic to ARMELFObjectWriter::addTargetSectionFlags This simplifies the generic interface and also makes SHF_ARM_PURECODE more robust (fixes a TODO). Inspecting MCDataFragment contents covers more cases than MCObjectStreamer::EmitBytes. --- llvm/include/llvm/MC/MCSection.h | 7 ------- llvm/lib/MC/MCObjectStreamer.cpp | 6 ------ llvm/lib/MC/MCSection.cpp | 3 +-- llvm/lib/Target/ARM/MCTargetDesc/ARMELFObjectWriter.cpp | 7 +++++-- 4 files changed, 6 insertions(+), 17 deletions(-) diff --git a/llvm/include/llvm/MC/MCSection.h b/llvm/include/llvm/MC/MCSection.h index d66e42b656b1..d80cc5b086b3 100644 --- a/llvm/include/llvm/MC/MCSection.h +++ b/llvm/include/llvm/MC/MCSection.h @@ -78,10 +78,6 @@ private: /// Whether this section has had instructions emitted into it. bool HasInstructions : 1; - /// Whether this section has had data emitted into it. - /// Right now this is only used by the ARM backend. - bool HasData : 1; - bool IsRegistered : 1; MCDummyFragment DummyFragment; @@ -150,9 +146,6 @@ public: bool hasInstructions() const { return HasInstructions; } void setHasInstructions(bool Value) { HasInstructions = Value; } - bool hasData() const { return HasData; } - void setHasData(bool Value) { HasData = Value; } - bool isRegistered() const { return IsRegistered; } void setIsRegistered(bool Value) { IsRegistered = Value; } diff --git a/llvm/lib/MC/MCObjectStreamer.cpp b/llvm/lib/MC/MCObjectStreamer.cpp index 5c42667f9910..d8a7ab043eb8 100644 --- a/llvm/lib/MC/MCObjectStreamer.cpp +++ b/llvm/lib/MC/MCObjectStreamer.cpp @@ -569,12 +569,6 @@ void MCObjectStreamer::EmitBytes(StringRef Data) { MCDataFragment *DF = getOrCreateDataFragment(); flushPendingLabels(DF, DF->getContents().size()); DF->getContents().append(Data.begin(), Data.end()); - - // EmitBytes might not cover all possible ways we emit data (or could be used - // to emit executable code in some cases), but is the best method we have - // right now for checking this. - MCSection *Sec = getCurrentSectionOnly(); - Sec->setHasData(true); } void MCObjectStreamer::EmitValueToAlignment(unsigned ByteAlignment, diff --git a/llvm/lib/MC/MCSection.cpp b/llvm/lib/MC/MCSection.cpp index 7a953054e729..074534bd73db 100644 --- a/llvm/lib/MC/MCSection.cpp +++ b/llvm/lib/MC/MCSection.cpp @@ -22,8 +22,7 @@ using namespace llvm; MCSection::MCSection(SectionVariant V, SectionKind K, MCSymbol *Begin) : Begin(Begin), BundleGroupBeforeFirstInst(false), HasInstructions(false), - HasData(false), IsRegistered(false), DummyFragment(this), Variant(V), - Kind(K) {} + IsRegistered(false), DummyFragment(this), Variant(V), Kind(K) {} MCSymbol *MCSection::getEndSymbol(MCContext &Ctx) { if (!End) diff --git a/llvm/lib/Target/ARM/MCTargetDesc/ARMELFObjectWriter.cpp b/llvm/lib/Target/ARM/MCTargetDesc/ARMELFObjectWriter.cpp index 1fee38821a49..2c26dd388c05 100644 --- a/llvm/lib/Target/ARM/MCTargetDesc/ARMELFObjectWriter.cpp +++ b/llvm/lib/Target/ARM/MCTargetDesc/ARMELFObjectWriter.cpp @@ -255,8 +255,11 @@ void ARMELFObjectWriter::addTargetSectionFlags(MCContext &Ctx, // execute-only section in the object. MCSectionELF *TextSection = static_cast(Ctx.getObjectFileInfo()->getTextSection()); - if (Sec.getKind().isExecuteOnly() && !TextSection->hasInstructions() && - !TextSection->hasData()) { + if (Sec.getKind().isExecuteOnly() && !TextSection->hasInstructions()) { + for (auto &F : TextSection->getFragmentList()) + if (auto *DF = dyn_cast(&F)) + if (!DF->getContents().empty()) + return; TextSection->setFlags(TextSection->getFlags() | ELF::SHF_ARM_PURECODE); } }