From 693a48f0700aec9406a71a1e77583977f90c9453 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9ophile=20Bastian?= Date: Mon, 18 Mar 2019 14:27:24 +0100 Subject: [PATCH] Add synthesize_dwarf.sh Update documentation and Makefile accordingly. --- .gitignore | 2 ++ Makefile | 7 +++- README.md | 22 +++++++++++-- synthesize_dwarf.sh | 80 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 108 insertions(+), 3 deletions(-) create mode 100755 synthesize_dwarf.sh diff --git a/.gitignore b/.gitignore index c5fb759..654dfd7 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,5 @@ setup.data setup.log *.plugin +ml_dwarf_write.bin +tmp.marshal diff --git a/Makefile b/Makefile index 9a80d49..9fbfc32 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,12 @@ BAPBUNDLE=bapbundle ROOT_MODULE=dwarfsynth -all: install +all: install ml_dwarf_write.bin + +.PHONY: ml_dwarf_write.bin +ml_dwarf_write.bin: + $(MAKE) -C DwarfSynth/c_bindings + ln -fs DwarfSynth/c_bindings/ml_dwarf_write.bin . .PHONY: $(ROOT_MODULE).plugin $(ROOT_MODULE).plugin: diff --git a/README.md b/README.md index 857f0d7..b0a51cd 100644 --- a/README.md +++ b/README.md @@ -11,9 +11,27 @@ examine its assembly code and, based solely on that, generate the corresponding This tool relies on [BAP](https://github.com/BinaryAnalysisPlatform/bap), which is available through OPAM. -## Running +## Compiling -First, run `make` to compile and install the BAP plugin. +Simply run `make` to compile all the necessary tools, including compiling and +installing the BAP plugin `dwarfsynth`. + +## Running with a wrapper script + +To generate an `.eh_frame` section for some binary `foo.bin` and write the +output as `foo.eh.bin`, you can run + +``` +./synthesize_dwarf foo.bin foo.eh.bin +``` + +You can also omit the second parameter to simply overwrite `foo.bin`. + +## Running by hand + +If you want, for some reason, to run by hand the multiple components, you can +follow this procedure (by using more appropriate file names, and, possibly, a +temporary directory -- see `mktemp -d`). ### Running the BAP plugin diff --git a/synthesize_dwarf.sh b/synthesize_dwarf.sh new file mode 100755 index 0000000..9a66881 --- /dev/null +++ b/synthesize_dwarf.sh @@ -0,0 +1,80 @@ +#!/bin/bash + +############################################################################### +USAGE="$0 []" + +HELP_TEXT="synthesize_dwarf.sh +Script that sticks all the parts of dwarf-synthesis together. + +Usage: $USAGE + +The provided is expected **NOT** to have any .eh_frame +section. + +If is provided, a binary file equivalent to + that contains an .eh_frame ELF section will be written as +. + +If not, the input file will be overwriten with such a file." +############################################################################### + +function find_ml_dwarf_write { + out=$(which "ml_dwarf_write.bin" 2>/dev/null) + if [ -n "$out" ] ; then + echo $out + return 0 + fi + for location in . "$(dirname $0)" ; do + out="$location/ml_dwarf_write.bin" + if [ -x "$out" ] ; then + echo $out + return 0 + fi + done + return 1 +} + +function bap_synth { + bap "$INPUT_FILE" -p dwarfsynth --dwarfsynth-output "$TMP_DIR/marshal" \ + > /dev/null + return $? +} + +function dwarf_write_synth { + $ML_DWARF_WRITE "$TMP_DIR/marshal" "$INPUT_FILE" "$TMP_DIR/eh_frame" \ + > /dev/null + return $? +} + +function dwarf_plug { + objcopy \ + --add-section .eh_frame="$TMP_DIR/eh_frame" \ + "$INPUT_FILE" "$OUTPUT_FILE" + return $? +} + +if [ "$#" -lt "1" ] ; then + >&2 echo -e "Missing argument.\n\n$HELP_TEXT" + exit 1 +fi + +ML_DWARF_WRITE=$(find_ml_dwarf_write) +if [ "$?" -ne "0" ] ; then + >&2 echo -e "Cannot find ml_dwarf_write" + exit 1 +fi + +INPUT_FILE="$1" +OUTPUT_FILE="$2" + +if ! [ -f "$INPUT_FILE" ] ; then + >&2 echo -e "$INPUT_FILE: no such file.\n\n$HELP_TEXT" +fi + +TMP_DIR="$(mktemp -d)" + +bap_synth \ + && dwarf_write_synth \ + && dwarf_plug + +rm -rf "$TMP_DIR"