Skip to content

Commit

Permalink
[MC,ARM] Move SHF_ARM_PUECODE change for .text to ARMTargetELFStreame…
Browse files Browse the repository at this point in the history
…r::finish

and remove MCELFObjectWriter::addTargetSectionFlags.
  • Loading branch information
MaskRay committed Aug 6, 2024
1 parent f301887 commit 5e1a5ff
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 28 deletions.
2 changes: 0 additions & 2 deletions llvm/include/llvm/MC/MCELFObjectWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@ class MCELFObjectTargetWriter : public MCObjectTargetWriter {
virtual void sortRelocs(const MCAssembler &Asm,
std::vector<ELFRelocationEntry> &Relocs);

virtual void addTargetSectionFlags(MCContext &Ctx, MCSectionELF &Sec);

/// \name Accessors
/// @{
uint8_t getOSABI() const { return OSABI; }
Expand Down
2 changes: 0 additions & 2 deletions llvm/lib/MC/ELFObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1018,8 +1018,6 @@ uint64_t ELFWriter::writeObject(MCAssembler &Asm) {
if (RelSection)
Members.second.push_back(RelSection->getOrdinal());
}

OWriter.TargetObjectWriter->addTargetSectionFlags(Ctx, Section);
}

for (auto &[Group, Members] : Groups) {
Expand Down
3 changes: 0 additions & 3 deletions llvm/lib/MC/MCELFObjectTargetWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,3 @@ void
MCELFObjectTargetWriter::sortRelocs(const MCAssembler &Asm,
std::vector<ELFRelocationEntry> &Relocs) {
}

void MCELFObjectTargetWriter::addTargetSectionFlags(MCContext &Ctx,
MCSectionELF &Sec) {}
21 changes: 0 additions & 21 deletions llvm/lib/Target/ARM/MCTargetDesc/ARMELFObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ namespace {

bool needsRelocateWithSymbol(const MCValue &Val, const MCSymbol &Sym,
unsigned Type) const override;

void addTargetSectionFlags(MCContext &Ctx, MCSectionELF &Sec) override;
};

} // end anonymous namespace
Expand Down Expand Up @@ -319,25 +317,6 @@ unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
}
}

void ARMELFObjectWriter::addTargetSectionFlags(MCContext &Ctx,
MCSectionELF &Sec) {
// The mix of execute-only and non-execute-only at link time is
// non-execute-only. To avoid the empty implicitly created .text
// section from making the whole .text section non-execute-only, we
// mark it execute-only if it is empty and there is at least one
// execute-only section in the object.
MCSectionELF *TextSection =
static_cast<MCSectionELF *>(Ctx.getObjectFileInfo()->getTextSection());
bool IsExecOnly = Sec.getFlags() & ELF::SHF_ARM_PURECODE;
if (IsExecOnly && !TextSection->hasInstructions()) {
for (auto &F : *TextSection)
if (auto *DF = dyn_cast<MCDataFragment>(&F))
if (!DF->getContents().empty())
return;
TextSection->setFlags(TextSection->getFlags() | ELF::SHF_ARM_PURECODE);
}
}

std::unique_ptr<MCObjectTargetWriter>
llvm::createARMELFObjectWriter(uint8_t OSABI) {
return std::make_unique<ARMELFObjectWriter>(OSABI);
Expand Down
20 changes: 20 additions & 0 deletions llvm/lib/Target/ARM/MCTargetDesc/ARMELFStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "llvm/MC/MCFragment.h"
#include "llvm/MC/MCInst.h"
#include "llvm/MC/MCInstPrinter.h"
#include "llvm/MC/MCObjectFileInfo.h"
#include "llvm/MC/MCObjectWriter.h"
#include "llvm/MC/MCRegisterInfo.h"
#include "llvm/MC/MCSection.h"
Expand Down Expand Up @@ -1113,6 +1114,25 @@ void ARMTargetELFStreamer::reset() { AttributeSection = nullptr; }
void ARMTargetELFStreamer::finish() {
ARMTargetStreamer::finish();
finishAttributeSection();

// The mix of execute-only and non-execute-only at link time is
// non-execute-only. To avoid the empty implicitly created .text
// section from making the whole .text section non-execute-only, we
// mark it execute-only if it is empty and there is at least one
// execute-only section in the object.
MCContext &Ctx = getStreamer().getContext();
auto &Asm = getStreamer().getAssembler();
if (any_of(Asm, [](const MCSection &Sec) {
return cast<MCSectionELF>(Sec).getFlags() & ELF::SHF_ARM_PURECODE;
})) {
auto *Text =
static_cast<MCSectionELF *>(Ctx.getObjectFileInfo()->getTextSection());
for (auto &F : *Text)
if (auto *DF = dyn_cast<MCDataFragment>(&F))
if (!DF->getContents().empty())
return;
Text->setFlags(Text->getFlags() | ELF::SHF_ARM_PURECODE);
}
}

void ARMELFStreamer::reset() {
Expand Down

0 comments on commit 5e1a5ff

Please sign in to comment.