Skip to content

Commit

Permalink
[snippy] do not insert EH frame sections
Browse files Browse the repository at this point in the history
  • Loading branch information
arrv-sc authored and asi-sc committed Aug 12, 2024
1 parent f847b8d commit 3420f5a
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 14 deletions.
6 changes: 6 additions & 0 deletions llvm/include/llvm/MC/MCObjectStreamer.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ class MCObjectStreamer : public MCStreamer {
void emitFrames(MCAsmBackend *MAB);
void emitCFISections(bool EH, bool Debug) override;

bool setEmitEHFrame(bool Val) { return std::exchange(EmitEHFrame, Val); }

bool setEmitDebugFrame(bool Val) {
return std::exchange(EmitDebugFrame, Val);
}

MCFragment *getCurrentFragment() const;

void insert(MCFragment *F) {
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/tools/llvm-snippy/linker-script-layout.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ histogram:
- [ADDI, 1.0]
- [SW, 5.0]

# UNMANGLED: SNIPPY (rwx) : ORIGIN = 65536, LENGTH = 67072
# UNMANGLED: SNIPPY (rwx) : ORIGIN = 65536, LENGTH = 1536
# UNMANGLED: .snippy.1.rx 65536: {
# UNMANGLED: .snippy.2.rw 66048 (NOLOAD) : {
# UNMANGLED: .snippy.named.rw 66560 (NOLOAD) : {
# MANGLED: SNIPPY_Test (rwx) : ORIGIN = 65536, LENGTH = 67072
# MANGLED: SNIPPY_Test (rwx) : ORIGIN = 65536, LENGTH = 1536
# MANGLED: .snippy.Test.1.rx 65536: {
# MANGLED: .snippy.Test.2.rw 66048 (NOLOAD) : {
# MANGLED: .snippy.Test.named.rw 66560 (NOLOAD) : {
4 changes: 2 additions & 2 deletions llvm/test/tools/llvm-snippy/linker-script-phdrs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ histogram:
- [ADDI, 1.0]
- [SW, 5.0]

# NODEF: SNIPPY (rwx) : ORIGIN = 65536, LENGTH = 67072
# NODEF: SNIPPY (rwx) : ORIGIN = 65536, LENGTH = 1536
# NODEF-NOT: PHDRS
# NODEF: SECTIONS
# WITHDEF: SNIPPY (rwx) : ORIGIN = 65536, LENGTH = 67072
# WITHDEF: SNIPPY (rwx) : ORIGIN = 65536, LENGTH = 1536
# WITHDEF: PHDRS
# WITHDEF: SECTIONS

Expand Down
23 changes: 23 additions & 0 deletions llvm/test/tools/llvm-snippy/no-eh-frame.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# RUN: llvm-snippy %s -o %t -model-plugin=None
# RUN: readelf -S %t.elf | not grep eh_frame

options:
march: riscv64-unknown-elf
mcpu: generic-rv64
num-instrs: 100

sections:
- no: 1
VMA: 0x500000
SIZE: 0x100000
LMA: 0x500000
ACCESS: rx
- no: 2
VMA: 0x600000
SIZE: 0x100000
LMA: 0x600000
ACCESS: rw

histogram:
- [ADD, 1.0]

3 changes: 3 additions & 0 deletions llvm/tools/llvm-snippy/include/snippy/Generator/LLVMState.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ class LLVMState {
return GV;
}

std::unique_ptr<MCStreamer> createObjStreamer(raw_pwrite_stream &OS,
MCContext &Ctx);

AsmPrinter &getOrCreateAsmPrinter() const;
MCCodeEmitter &getCodeEmitter() const;
MCDisassembler &getDisassembler() const;
Expand Down
4 changes: 4 additions & 0 deletions llvm/tools/llvm-snippy/include/snippy/Target/Target.h
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,10 @@ class SnippyTarget {
// Registers that should be valid while calling an external function
virtual std::vector<MCRegister> getGlobalStateRegs() const = 0;

virtual std::unique_ptr<AsmPrinter>
createAsmPrinter(LLVMTargetMachine &TM,
std::unique_ptr<MCStreamer> Streamer) const = 0;

private:
virtual bool matchesArch(Triple::ArchType Arch) const = 0;

Expand Down
7 changes: 6 additions & 1 deletion llvm/tools/llvm-snippy/lib/FlowGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "snippy/Support/Utils.h"
#include "snippy/Target/Target.h"

#include "llvm/CodeGen/AsmPrinter.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
Expand All @@ -41,6 +42,7 @@
#include "llvm/CodeGen/MachineOperand.h"
#include "llvm/CodeGen/Passes.h"
#include "llvm/InitializePasses.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetLoweringObjectFile.h"
Expand Down Expand Up @@ -376,6 +378,7 @@ static RegisterGenerator createRegGen(std::string PluginFileName,

GeneratorResult FlowGenerator::generate(LLVMState &State) {
auto &LLVMTM = State.getTargetMachine();
auto &Tgt = State.getSnippyTarget();
auto &Ctx = State.getCtx();
Module M("SnippyModule", Ctx);

Expand Down Expand Up @@ -462,7 +465,9 @@ GeneratorResult FlowGenerator::generate(LLVMState &State) {

SmallString<32> Output;
raw_svector_ostream OS(Output);
PM.addAsmPrinter(LLVMTM, OS, nullptr, CodeGenFileType::ObjectFile, Context);
auto ObjStreamer = State.createObjStreamer(OS, Context);
auto AsmPrinter = Tgt.createAsmPrinter(LLVMTM, std::move(ObjStreamer));
PM.add(AsmPrinter.release());

PM.run(M);

Expand Down
19 changes: 19 additions & 0 deletions llvm/tools/llvm-snippy/lib/Generator/LLVMState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "llvm/MC/MCDisassembler/MCDisassembler.h"
#include "llvm/MC/MCFixup.h"
#include "llvm/MC/MCObjectFileInfo.h"
#include "llvm/MC/MCObjectStreamer.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Support/raw_ostream.h"
Expand Down Expand Up @@ -107,5 +108,23 @@ bool LLVMState::canAssemble(const MCInst &Inst) const {
return Tmp.size() > 0;
}

std::unique_ptr<MCStreamer> LLVMState::createObjStreamer(raw_pwrite_stream &OS,
MCContext &MCCtx) {
assert(TheTargetMachine);
auto MCStreamerOrErr = TheTargetMachine->createMCStreamer(
OS, nullptr, CodeGenFileType::ObjectFile, MCCtx);
if (auto Err = MCStreamerOrErr.takeError())
snippy::fatal(Ctx, "Internal Error creating MCStreamer",
toString(std::move(Err)));

// We explicitly specified ObjectFile type 3 lines above so we can down-cast
// it safely
auto *ObjStreamer =
static_cast<MCObjectStreamer *>(MCStreamerOrErr->release());
ObjStreamer->setEmitEHFrame(false);
ObjStreamer->setEmitDebugFrame(false);
return std::unique_ptr<MCStreamer>(ObjStreamer);
}

} // namespace snippy
} // namespace llvm
10 changes: 1 addition & 9 deletions llvm/tools/llvm-snippy/lib/Generator/Linker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,16 +350,12 @@ std::vector<std::string> Linker::collectPhdrInfo() const {
std::string Linker::createLinkerScript(bool Export) const {
std::string ScriptText;
llvm::raw_string_ostream STS{ScriptText};
// It is needed to fit other object file sections like
// .eh_frame
constexpr size_t ExtraMemorySpace = 0x10000;

std::string MemoryRegionName =
MangleName.empty() ? "SNIPPY" : "SNIPPY_" + MangleName;
STS << "MEMORY {\n"
<< " " << MemoryRegionName << " (rwx) : ORIGIN = " << MemoryRegion.first
<< ", LENGTH = "
<< (MemoryRegion.second - MemoryRegion.first + ExtraMemorySpace) << "\n";
<< ", LENGTH = " << (MemoryRegion.second - MemoryRegion.first) << "\n";

STS << "}\n";
auto Phdrs = collectPhdrInfo();
Expand Down Expand Up @@ -401,10 +397,6 @@ std::string Linker::createLinkerScript(bool Export) const {
}
STS << "\n";
}
STS << "/DISCARD/ :\n"
<< "{\n"
<< " *(.eh_frame)\n"
<< "}\n";

STS << "}";
return ScriptText;
Expand Down
6 changes: 6 additions & 0 deletions llvm/tools/llvm-snippy/lib/Target/RISCV/Target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2820,6 +2820,12 @@ class SnippyRISCVTarget final : public SnippyTarget {
bool canProduceNaN(const MCInstrDesc &InstrDesc) const override {
return snippy::canProduceNaN(InstrDesc);
}

std::unique_ptr<AsmPrinter>
createAsmPrinter(LLVMTargetMachine &TM,
std::unique_ptr<MCStreamer> Streamer) const override {
return std::make_unique<RISCVAsmPrinter>(TM, std::move(Streamer));
}
};

static unsigned getOpcodeForGPRToFPRInstr(unsigned DstReg, unsigned XLen,
Expand Down
6 changes: 6 additions & 0 deletions llvm/tools/llvm-snippy/lib/Target/X86/Target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,12 @@ class SnippyX86Target : public SnippyTarget {
reportUnimplementedError();
}

std::unique_ptr<AsmPrinter>
createAsmPrinter(LLVMTargetMachine &TM,
std::unique_ptr<MCStreamer> Streamer) const override {
reportUnimplementedError();
}

}; // namespace

bool SnippyX86Target::matchesArch(Triple::ArchType Arch) const {
Expand Down

0 comments on commit 3420f5a

Please sign in to comment.