GTIRB Pretty Printer
A pretty printer from the GTIRB intermediate representation for binary analysis and reverse engineering to gas-syntax assembly code.
The pretty-printer uses C++17, and requires a compiler which supports that standard such as gcc 7, clang 6, or MSVC 2017.
To build and install the pretty printer, the following requirements should be installed:
- Capstone. At the moment we require our own fork https://github.com/GrammaTech/capstone/tree/next that contains some additional fixes (until new official releases are cut).
- Boost, version 1.67.0 or later.
- Requires the libraries:
- Requires the libraries:
Note that these versions are newer than what your package manager may provide by default: This is true on Ubuntu 18, Debian 10, and others. Prefer building these dependencies from sources to avoid versioning problems.
Use the following options to configure cmake:
- You can tell CMake which compiler to use with
- Normally CMake will find GTIRB automatically, but if it does not you
- gtirb-pprinter can make use of GTIRB in static library form (instead of
shared library form, the default) if you use the flag
- Furthermore, if you want to produce a
gtirb-pprinterexecutable that links statically, specify
- You can configure CMake to use a custom location for Capstone by specifying
- You can use vcpkg on Windows to provide some dependencies by passing
Once the dependencies are installed, you can configure and build as follows:
cmake ./ -Bbuild cd build make
See the GTIRB readme.
Generate reassembleable assembly code
Pretty print the GTIRB for a simple hello world executable to an
assembly file named
hello.S, assemble this file with the GNU
assembler to an object file named
hello.o, and link this object file
into an executable.
gtirb-pprinter hello.gtirb --asm hello.S as hello.S -o hello.o ld hello.o -o hello ./hello
Generate a new binary
--binary flag to gtirb-pprinter generates a new binary by
gtirb-pprinter hello.gtirb --binary hello
This option admits an argument
specify additional paths where libraries might be located.
gtirb-pprinter hello.gtirb --binary hello -L . -L /usr/local/lib
In some cases, it is desirable to rebuild a dynamically linked ELF executable without any of the libraries to which it is linked (e.g., if rebuilding an executable from another system).
Normally, the linker needs to have the libraries in order to link with them.
--dummy-so option generates fake libraries that contain the
required symbols used by the binary, which is sufficient for running the
linker. An example is shown:
gtirb-pprinter hello.gtirb --binary hello --dummy-so=yes
If the binary requires verisoned symbols (which is likely if it uses a recent version of glibc), some further arguments may be necessary. Your system's startfiles may differ from the startfiles that the binary was originally built with, causing symbol version incompatibilities when gtirb-pprinter removes startup code and your compiler replaces it with its own. To best work around this, add the following arguments:
gtirb-pprinter hello.gtirb --binary hello --dummy-so=yes --keep-all-functions -c -nostartfiles
These ensure that startup code (such as functions like
__libc_csu_init) present in the GTIRB is used as-is, instead of replacing
them with startup code provided by your compiler, preserving the same symbol
versions that the binary originally used.
AuxData Used by the Pretty Printer
Generating assembly depends on a number of additional pieces of information beyond the symbols and instruction/data bytes in the IR. The pretty printer expects this information to be available in a number of AuxData objects stored with the IR. We document the expected keys along with the associated types and contents in this table.
||Per-instruction or data element comments.|
||UUIDs of the blocks that are entry points of functions.|
||Map from symbols to other symbols. This table is used to forward symbols due to relocations or due to the use of plt and got tables.|
||Map from (typed) data objects to the encoding of the data, expressed as a std::string containing an assembler encoding specifier: "string", "uleb128" or "sleb128".|
||Map from section UUIDs to tuples with the section types and flags.|
||Map from Offsets to vector of cfi directives. A cfi directive contains: a string describing the directive, a vector of numeric arguments, and an optional symbolic argument (represented with the UUID of the symbol).|
||On ELF targets only: Map from symbols to their type, binding, and visibility categories.|
AuxData Used by the Binary Printer
In order to generate new binaries, gtirb-binary-printer also uses the following tables:
||Names of the libraries that are needed.|
||Paths contained in the rpath of the binary|
||Stack executable flag specified by PT_GNU_STACK segment in ELF files. Binary-printed with
||Stack size specified by PT_GNU_STACK segment in ELF files. Binary-printed with