From 353a97c49d0c3c8363d0835be7db463570cbd379 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Bastian?= Date: Wed, 25 Apr 2018 18:11:59 +0200 Subject: [PATCH] Refactor code generation: use shared/context_struct.h This allows #include'ing `context_struct.h` in the stack walker implementation as well --- shared/context_struct.h | 5 +++++ src/.gitignore | 1 + src/CodeGenerator.cpp | 8 +++----- src/Makefile | 9 +++++++-- 4 files changed, 16 insertions(+), 7 deletions(-) create mode 100644 shared/context_struct.h diff --git a/shared/context_struct.h b/shared/context_struct.h new file mode 100644 index 0000000..1946274 --- /dev/null +++ b/shared/context_struct.h @@ -0,0 +1,5 @@ +#include + +typedef struct { + uintptr_t rip, rsp, rbp; +} unwind_context_t; diff --git a/src/.gitignore b/src/.gitignore index 2311db3..39b608a 100644 --- a/src/.gitignore +++ b/src/.gitignore @@ -1 +1,2 @@ dwarf-assembly +gen_*.hpp diff --git a/src/CodeGenerator.cpp b/src/CodeGenerator.cpp index e3f3039..8c58a30 100644 --- a/src/CodeGenerator.cpp +++ b/src/CodeGenerator.cpp @@ -1,4 +1,5 @@ #include "CodeGenerator.hpp" +#include "gen_context_struct.hpp" using namespace std; @@ -6,10 +7,6 @@ static const char* PRELUDE = "#include \n" "#include \n" "\n" -"typedef struct {\n" -" uintptr_t rip, rsp, rbp;\n" -"} unwind_context_t;\n" -"\n" "typedef unwind_context_t (*_fde_func_t)(unwind_context_t, uintptr_t);\n" ; @@ -25,7 +22,8 @@ void CodeGenerator::generate() { } void CodeGenerator::gen_of_dwarf() { - os << PRELUDE << '\n' << endl; + os << CONTEXT_STRUCT_STR << '\n' + << PRELUDE << '\n' << endl; vector lookup_entries; diff --git a/src/Makefile b/src/Makefile index b663749..0f130df 100644 --- a/src/Makefile +++ b/src/Makefile @@ -10,13 +10,18 @@ OBJS=DwarfReader.o SimpleDwarf.o CodeGenerator.o main.o all: $(TARGET) -$(TARGET): $(OBJS) +$(TARGET): gen_context_struct.hpp $(OBJS) $(CXX) -o $@ $(CXXFLAGS) $^ $(CXXLIBS) +gen_context_struct.hpp: ../shared/context_struct.h + echo "static const char* CONTEXT_STRUCT_STR =" > $@ + sed 's/"/\\"/g' $< | sed 's/^\(.*\)$$/"\1\\n"/g' >> $@ + echo ";" >> $@ + %.o: %.cpp $(CXX) -o $@ $(CXXFLAGS) -c $< .PHONY: clean clean: - rm -f $(TARGET) *.o + rm -f $(TARGET) *.o gen_*.hpp