|
| 1 | +--- |
| 2 | +title: Tools Related to Lifting |
| 3 | +date: 2025-10-19 |
| 4 | +categories: [Compiler] |
| 5 | +tags: [arm, compiler] # TAG names should always be lowercase |
| 6 | +published: false |
| 7 | +--- |
| 8 | + |
| 9 | +> Problems: I want to find out tools about lifting binaries to assembly code, while preserving relocation and linking information. |
| 10 | +> Problems continued from the last post, I want to convert an executable binary back to position-independent assembly code. |
| 11 | +
|
| 12 | +## Existing tools |
| 13 | +- [Binary Ninja](https://cloud.binary.ninja/bn/cac518d6-6e84-4ae9-a338-a9ec0aacd074?view=Triage&func=66800&il=3&address=66800): which can be used online |
| 14 | + - which has a cfg view, we can get the basic blocks placement information from it. |
| 15 | + - In Binary Ninja, I noticed there actually has already a mapping from pc related address to the data itself. |
| 16 | + -  |
| 17 | + - Also, I noticed that the cfg (needed to place the basic blocks) is also generated by it. |
| 18 | + -  |
| 19 | +- [DDisasm](https://github.com/GrammaTech/ddisasm): a fast disassembler which is accurate enough for the resulting assembly code to be reassembled. |
| 20 | +| Stack | Core idea | Typical output | Sweet spot | |
| 21 | +| --- | --- | --- | --- | |
| 22 | +| **DDisasm → GTIRB → gtirb-pprinter** | Disassemble to a **structured binary IR** (GTIRB) that preserves sections, blocks, edges, symbols, and then pretty-print **re-assemblable .s** | `.gtirb` (graph+metadata) → **assembly with labels per basic block** | When you want a **faithful, labeled assembly** you can tweak and (often) reassemble/relink; also nice for **programmatic block reordering** and regeneration | |
| 23 | +| **angr + Ramblr** | **Lift** binary to VEX IR with powerful static/dynamic analyses (CFG, dataflow, symbolic exec); **Ramblr** reconstructs a **reassemblable** view for **patching/rebuilding** | Python objects (CFG, IR), patched **binary**, or assembly-like output for rewriting | When you need **analysis first** (CFG, slicing, constraint solving) and then **surgical patching** or a **reassembled** artifact to preserve semantics | |
| 24 | + |
| 25 | +Functional differences (practical) |
| 26 | +================================== |
| 27 | + |
| 28 | +* **IR and ecosystem** |
| 29 | + |
| 30 | + * **GTIRB** is a **file/graph IR** for binaries (sections, byte intervals, code blocks, CFG, symbols). Great for **structural transforms** (e.g., reorder blocks, move data). It’s “printer-friendly.” |
| 31 | + |
| 32 | + * **angr** is an **analysis framework** (lifting to **VEX**, symbolic execution, dataflow). It excels at **discovering** things (CFG recovery quality, constant/alias resolution, indirect jumps) and **reasoning** about feasibility. |
| 33 | + |
| 34 | +* **Re-assemblable output** |
| 35 | + |
| 36 | + * **GTIRB + pprinter**: generates **clean GAS** with **one label per basic block**, jump tables in `.rodata`, etc. Very convenient when your goal is **labeled .s** that you can feed to `as/ld`. |
| 37 | + |
| 38 | + * **Ramblr**: focuses on making a **reassembled/patchable** binary; it reconstructs relocations/symbols enough to produce an executable that preserves behavior. Output assembly is more “for rewriting” than for human editing, but it can be used that way. |
| 39 | + |
| 40 | +# Usage of DDisasm |
| 41 | + |
| 42 | +- Using docker |
| 43 | +```shell |
| 44 | +# Directly use the built docker |
| 45 | +docker pull grammatech/ddisasm:latest |
| 46 | +sudo docker images |
| 47 | +REPOSITORY TAG IMAGE ID CREATED SIZE |
| 48 | +grammatech/ddisasm latest 0c4892488a8d 7 weeks ago 429MB |
| 49 | +# run command and delete |
| 50 | +sudo docker run --rm grammatech/ddisasm:latest ls /usr/local/bin |
| 51 | +ddisasm |
| 52 | +gtirb-layout |
| 53 | +gtirb-pprinter |
| 54 | +# check latest version |
| 55 | +sudo docker run --rm grammatech/ddisasm:latest ddisasm --version |
| 56 | +1.9.2 (1cd04b14 2025-08-26) ARM64+IA32+X64+ARM32+MIPS32 |
| 57 | +``` |
| 58 | +- disassembling the binary, generate the gtirb and asm file |
| 59 | +```shell |
| 60 | +sudo docker run --rm -v $PWD:/workspace -w /workspace grammatech/ddisasm:latest ddisasm Shootout-nestedloop --ir Shootout-nestedlo |
| 61 | +op.gtirb |
| 62 | +Building the initial gtirb representation [ 4ms] |
| 63 | +Processing module: Shootout-nestedloop |
| 64 | + disassembly load [ 5ms] compute [ 23ms] transform WARNING: Could not find GLOBAL/WEAK symbol for _DYNAMIC |
| 65 | +[ 2ms] |
| 66 | + SCC analysis compute [ 0ms] transform [ 0ms] |
| 67 | + no return analysis load [ 0ms] compute [ 0ms] transform [ 0ms] |
| 68 | + function inference load [ 0ms] compute [ 0ms] transform [ 0ms] |
| 69 | +sudo docker run --rm -v $PWD:/workspace -w /workspace grammatech/ddisasm:latest ddisasm Shootout-nestedloop --asm Shootout-nestedl |
| 70 | +oop-ddisasm.s |
| 71 | +Building the initial gtirb representation [ 4ms] |
| 72 | +Processing module: Shootout-nestedloop |
| 73 | + disassembly load [ 5ms] compute [ 23ms] transform WARNING: Could not find GLOBAL/WEAK symbol for _DYNAMIC |
| 74 | +[ 2ms] |
| 75 | + SCC analysis compute [ 0ms] transform [ 0ms] |
| 76 | + no return analysis load [ 0ms] compute [ 0ms] transform [ 0ms] |
| 77 | + function inference load [ 0ms] compute [ 0ms] transform [ 0ms] |
| 78 | +Printing assembler [ 6ms] |
| 79 | +``` |
| 80 | +- use gtirb-pprinter to view the generated gtirb file |
| 81 | +```shell |
| 82 | +sudo docker run --rm -v $PWD:/workspace -w /workspace grammatech/ddisasm:latest gtirb-pprinter --ir Shootout-nestedloop.gtirb --as |
| 83 | +m gtirbpprinter-shootout-nestedloop.s |
| 84 | +[sudo] password for qingchen: |
| 85 | +[INFO] (/usr/local/src/gtirb-pprinter/src/gtirb_pprinter/driver/pretty_printer.cpp:291) Reading GTIRB file: "Shootout-nestedloop.gtirb" |
| 86 | +[INFO] (/usr/local/src/gtirb-pprinter/src/gtirb_pprinter/driver/pretty_printer.cpp:577) Generating assembly file for module Shootout-nestedloop |
| 87 | +``` |
| 88 | +Seems the generated assembly file has minimal differences with the assembly file directly disassembled from the binary |
| 89 | +```shell |
| 90 | +diff gtirbpprinter-shootout-nestedloop.s Shootout-nestedloop-ddisasm.s |
| 91 | +524a525 |
| 92 | +> .L_21030: |
| 93 | +545d545 |
| 94 | +< .L_21030: |
| 95 | +``` |
0 commit comments