unstrip 1.2.0

Recover symbols, types, and method signatures from stripped Go binaries. Ghidra/IDA/Binary Ninja exporters included.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
use serde::Serialize;

use crate::error::Error;
use crate::gobin::GoBinary;
use crate::itabs::Itab;
use crate::pclntab::{Function, Pclntab};
use crate::Result;

/// Display interpretation requested for the inspected bytes. Drives
/// the per-row formatter; the raw bytes are always available via the
/// `bytes` field on `DataView` regardless of choice.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum As {
    /// Hex dump, 16 bytes per row.
    Bytes,
    /// Little-endian u64 per 8-byte aligned slot.
    Qwords,
    /// u64 per slot, but every value passes through the symbolizer:
    /// known functions become `name @ off`, known itabs become
    /// `itab(iface=concrete)`, known section addresses become
    /// `<section>+0xN`, unknown stays as hex.
    Ptrs,
    /// 16-byte Go interface header per slot: (itab_ptr, data_ptr).
    /// Resolves itab_ptr against the recovered itab table to print
    /// the concrete type symbolically.
    Ifaces,
    /// 24-byte Go slice header per slot: (data_ptr, len, cap).
    SliceHeader,
    /// 16-byte Go string header per slot: (data_ptr, len). When the
    /// data_ptr resolves to a known section and the len is sane, the
    /// pointed-to bytes are printed as a quoted string.
    String,
}

/// One interpreted record from a `--data-at` scan. The wire shape is
/// stable: stdout text format is one row per record formatted via
/// `format_text`; JSON output emits each record directly.
#[derive(Debug, Clone, Serialize)]
pub struct DataRow {
    /// Address of this row inside the requested span.
    pub addr: u64,
    /// Raw bytes for this row, always present so JSON consumers can
    /// re-interpret without re-reading the binary.
    pub bytes: Vec<u8>,
    /// Human-readable rendering driven by the chosen `As` variant.
    /// For `As::Bytes` this is the hex dump; for symbolizing modes
    /// it is the resolved labels.
    pub rendering: String,
}

/// Resolved symbolic identity of a u64 value. The text formatter
/// renders each variant in a tight one-line form; downstream JSON
/// consumers can pattern-match on the discriminant.
#[derive(Debug, Clone, Serialize)]
#[serde(tag = "kind", rename_all = "kebab-case")]
pub enum Symbol {
    /// `pkg.func @ off` when the address lies inside a recovered
    /// function (offset is bytes into the function, 0 at the entry
    /// point).
    Function {
        name: String,
        entry: u64,
        offset: u64,
    },
    /// `itab(iface=concrete)` when the address matches a recovered
    /// itab record's base. `methods` carries the recovered (method
    /// name, concrete fn address) pairs so callers that already know
    /// they are looking at an iface dispatch slot (e.g. `--data-as
    /// ifaces`) can print the dispatched body address inline without
    /// a second --itabs lookup. Empty when the itab was recovered
    /// without resolvable methods.
    Itab {
        interface: String,
        concrete: String,
        #[serde(skip_serializing_if = "Vec::is_empty")]
        methods: Vec<ItabMethodEntry>,
    },
    /// `<section>+0xN` when no closer symbol matches but the address
    /// lies within a known section.
    SectionOffset { section: String, offset: u64 },
    /// Address is not in any mapped section.
    Unmapped,
    /// Bit pattern that does not look like an address at all (zero,
    /// small ints, etc.). Surface verbatim as a u64 with no claims.
    Scalar { value: u64 },
}

/// One method entry carried inline on [`Symbol::Itab`] so iface
/// dispatch slots can resolve to a callable body in one query.
#[derive(Debug, Clone, Serialize)]
pub struct ItabMethodEntry {
    pub name: String,
    pub concrete_fn: u64,
}

impl Symbol {
    pub fn render(&self) -> String {
        match self {
            Symbol::Function {
                name,
                entry: _,
                offset,
            } if *offset == 0 => name.clone(),
            Symbol::Function { name, offset, .. } => format!("{name} + 0x{offset:x}"),
            Symbol::Itab {
                interface,
                concrete,
                methods,
            } => {
                let mut s = format!("itab({interface} = {concrete})");
                // Append every method's dispatched body address inline
                // so the operator does not have to bounce out to
                // --itabs just to learn where dispatch will land. For
                // the common single-method case (most stdlib ifaces
                // and chain-stage style ifaces), this collapses two
                // commands into one. Hidden when no methods resolved
                // so the simple case stays readable.
                if !methods.is_empty() {
                    s.push_str("  [");
                    for (i, m) in methods.iter().enumerate() {
                        if i > 0 {
                            s.push_str(", ");
                        }
                        s.push_str(&format!(".{}() -> 0x{:x}", m.name, m.concrete_fn));
                    }
                    s.push(']');
                }
                s
            }
            Symbol::SectionOffset { section, offset } => format!("{section}+0x{offset:x}"),
            Symbol::Unmapped => "(unmapped)".to_string(),
            Symbol::Scalar { value } => format!("0x{value:x}"),
        }
    }
}

/// Inspect `len` bytes at `addr`, returning interpreted rows for the
/// chosen presentation. The function reads the binary's loaded bytes
/// directly; it never re-opens the file.
pub fn inspect(
    bin: &GoBinary,
    pcln: &Pclntab<'_>,
    itabs_v: &[Itab],
    functions: &[Function],
    addr: u64,
    len: usize,
    mode: As,
) -> Result<Vec<DataRow>> {
    let (file_off, bounded_len) = bounded_read(bin, addr, len)?;
    let bytes = &bin.bytes[file_off..file_off + bounded_len];

    let ctx = SymCtx::new(bin, pcln, itabs_v, functions);

    match mode {
        As::Bytes => Ok(rows_bytes(addr, bytes)),
        As::Qwords => Ok(rows_qwords(addr, bytes, None)),
        As::Ptrs => Ok(rows_qwords(addr, bytes, Some(&ctx))),
        As::Ifaces => Ok(rows_ifaces(addr, bytes, &ctx)),
        As::SliceHeader => Ok(rows_slice_headers(addr, bytes, &ctx)),
        As::String => Ok(rows_strings(addr, bytes, bin, &ctx)),
    }
}

/// Resolve `addr` to its tightest symbolic identity using every map
/// the binary has produced. Public so callers can symbolize ad-hoc.
pub fn symbolize(
    bin: &GoBinary,
    pcln: &Pclntab<'_>,
    itabs_v: &[Itab],
    functions: &[Function],
    value: u64,
) -> Symbol {
    let ctx = SymCtx::new(bin, pcln, itabs_v, functions);
    ctx.symbolize(value)
}

fn bounded_read(bin: &GoBinary, addr: u64, len: usize) -> Result<(usize, usize)> {
    // Hard floor on plausible runtime addresses. Anything below the
    // first OS page is a sentinel value an operator almost never
    // means to inspect (often the result of a bash variable expansion
    // that collapsed to 0 or a numeric typo); refusing here turns
    // "happily decodes section-name bytes as iface" into a clear
    // error before the symbolizer renders confident nonsense.
    if addr < 0x1000 {
        return Err(Error::Xrefs(format!(
            "address 0x{addr:x} is below the first plausible runtime page; \
             refusing to read (this is almost always a typo or a shell-\
             variable that collapsed to 0)"
        )));
    }
    // Find a section that actually carries this address at load time.
    // Filter out sections that were never mapped into program memory
    // (addr == 0 is the convention containers use for shstrtab and
    // other file-only sections), otherwise contains_addr would let a
    // zero-based file-only section claim arbitrary addresses.
    let s = bin
        .sections
        .iter()
        .filter(|s| s.addr != 0)
        .find(|s| s.contains_addr(addr))
        .ok_or_else(|| {
            Error::Xrefs(format!("address 0x{addr:x} is not in any loadable section"))
        })?;
    let file_off = s.file_offset_of(addr).ok_or_else(|| {
        Error::Xrefs(format!(
            "address 0x{addr:x} lies past the file-backed portion of {}",
            s.name
        ))
    })?;
    // A NOBITS section (.bss, .noptrbss) is mapped at load but carries no file
    // bytes: it is zero at startup and filled at runtime. Its addresses have a
    // nominal file offset that can point past the end of the file, and the
    // in-section span can exceed the section's file_size. Clamp to both the
    // section's file-backed span and the real file length, and fail clean when
    // nothing is backed -- never index past the buffer (that was a panic).
    let in_section = (s.file_size as u64).saturating_sub(addr - s.addr) as usize;
    let in_file = bin.bytes.len().saturating_sub(file_off);
    let bounded = len.min(in_section).min(in_file);
    if bounded == 0 {
        return Err(Error::Xrefs(format!(
            "address 0x{addr:x} is in {} but not backed by file bytes \
             (zero-initialized at load, computed at runtime)",
            s.name
        )));
    }
    Ok((file_off, bounded))
}

fn rows_bytes(start: u64, bytes: &[u8]) -> Vec<DataRow> {
    let mut out = Vec::new();
    for (i, chunk) in bytes.chunks(16).enumerate() {
        let mut hex = String::with_capacity(48);
        for b in chunk {
            hex.push_str(&format!("{b:02x} "));
        }
        let mut ascii = String::with_capacity(16);
        for &b in chunk {
            ascii.push(if (0x20..=0x7E).contains(&b) {
                b as char
            } else {
                '.'
            });
        }
        out.push(DataRow {
            addr: start + (i as u64) * 16,
            bytes: chunk.to_vec(),
            rendering: format!("{hex:<48} |{ascii}|"),
        });
    }
    out
}

fn rows_qwords(start: u64, bytes: &[u8], ctx: Option<&SymCtx>) -> Vec<DataRow> {
    let mut out = Vec::new();
    for (i, chunk) in bytes.chunks(8).enumerate() {
        if chunk.len() < 8 {
            break;
        }
        let v = u64::from_le_bytes(chunk.try_into().unwrap());
        let rendering = match ctx {
            Some(c) => format!("0x{v:016x}  {}", c.symbolize(v).render()),
            None => format!("0x{v:016x}"),
        };
        out.push(DataRow {
            addr: start + (i as u64) * 8,
            bytes: chunk.to_vec(),
            rendering,
        });
    }
    out
}

fn rows_ifaces(start: u64, bytes: &[u8], ctx: &SymCtx) -> Vec<DataRow> {
    let mut out = Vec::new();
    for (i, chunk) in bytes.chunks(16).enumerate() {
        if chunk.len() < 16 {
            break;
        }
        let itab_ptr = u64::from_le_bytes(chunk[0..8].try_into().unwrap());
        let data_ptr = u64::from_le_bytes(chunk[8..16].try_into().unwrap());
        let itab_sym = ctx.symbolize(itab_ptr);
        let data_sym = ctx.symbolize(data_ptr);
        out.push(DataRow {
            addr: start + (i as u64) * 16,
            bytes: chunk.to_vec(),
            rendering: format!(
                "iface{{ itab=0x{:016x} ({}), data=0x{:016x} ({}) }}",
                itab_ptr,
                itab_sym.render(),
                data_ptr,
                data_sym.render(),
            ),
        });
    }
    out
}

fn rows_slice_headers(start: u64, bytes: &[u8], ctx: &SymCtx) -> Vec<DataRow> {
    let mut out = Vec::new();
    for (i, chunk) in bytes.chunks(24).enumerate() {
        if chunk.len() < 24 {
            break;
        }
        let data_ptr = u64::from_le_bytes(chunk[0..8].try_into().unwrap());
        let len = u64::from_le_bytes(chunk[8..16].try_into().unwrap());
        let cap = u64::from_le_bytes(chunk[16..24].try_into().unwrap());
        out.push(DataRow {
            addr: start + (i as u64) * 24,
            bytes: chunk.to_vec(),
            rendering: format!(
                "slice{{ data=0x{:016x} ({}), len={}, cap={} }}",
                data_ptr,
                ctx.symbolize(data_ptr).render(),
                len,
                cap,
            ),
        });
    }
    out
}

fn rows_strings(start: u64, bytes: &[u8], bin: &GoBinary, _ctx: &SymCtx) -> Vec<DataRow> {
    let mut out = Vec::new();
    for (i, chunk) in bytes.chunks(16).enumerate() {
        if chunk.len() < 16 {
            break;
        }
        let data_ptr = u64::from_le_bytes(chunk[0..8].try_into().unwrap());
        let len = u64::from_le_bytes(chunk[8..16].try_into().unwrap());
        out.push(DataRow {
            addr: start + (i as u64) * 16,
            bytes: chunk.to_vec(),
            rendering: render_string_header(bin, data_ptr, len),
        });
    }
    out
}

/// Render a 16-byte window as a Go string header, telling the truth when it is
/// not one. Reading code or a pointer field as a string (a wrong `--data-as
/// string`) yields a wild pointer and a nonsense length; presenting that as a
/// string with a quiet "(unmapped)" reads as real data. So reject an implausible
/// header outright and point at `--data-as bytes`. A real header with non-text
/// bytes is labelled as such rather than dropped.
fn render_string_header(bin: &GoBinary, ptr: u64, len: u64) -> String {
    if len == 0 {
        return format!("string{{ ptr=0x{ptr:016x}, len=0 }} (empty)");
    }
    if !bin.sections.iter().any(|s| s.contains_addr(ptr)) {
        return format!(
            "not a string header here: pointer 0x{ptr:016x} is unmapped (try --data-as bytes)"
        );
    }
    if len as usize > bin.bytes.len() {
        return format!(
            "not a string header here: length {len} exceeds the file (try --data-as bytes)"
        );
    }
    match read_string_preview(bin, ptr, len) {
        Some(s) => format!("string{{ ptr=0x{ptr:016x}, len={len} }} {s:?}"),
        None => {
            format!("string{{ ptr=0x{ptr:016x}, len={len} }} ({len} bytes, not printable text)")
        }
    }
}

fn read_string_preview(bin: &GoBinary, ptr: u64, len: u64) -> Option<String> {
    if len == 0 || len > 4096 {
        return None;
    }
    let s = bin.sections.iter().find(|s| s.contains_addr(ptr))?;
    let off = s.file_offset_of(ptr)?;
    let end = off + (len as usize);
    if end > bin.bytes.len() {
        return None;
    }
    let raw = &bin.bytes[off..end];
    if raw
        .iter()
        .any(|&b| !((0x20..=0x7E).contains(&b) || b == b'\n' || b == b'\t'))
    {
        return None;
    }
    Some(String::from_utf8_lossy(raw).into_owned())
}

struct SymCtx<'a> {
    bin: &'a GoBinary,
    pcln: &'a Pclntab<'a>,
    itab_by_addr: std::collections::HashMap<u64, &'a Itab>,
    function_by_addr: std::collections::HashMap<u64, &'a Function>,
}

impl<'a> SymCtx<'a> {
    fn new(
        bin: &'a GoBinary,
        pcln: &'a Pclntab<'a>,
        itabs_v: &'a [Itab],
        functions: &'a [Function],
    ) -> Self {
        SymCtx {
            bin,
            pcln,
            itab_by_addr: itabs_v.iter().map(|it| (it.addr, it)).collect(),
            function_by_addr: functions.iter().map(|f| (f.address, f)).collect(),
        }
    }

    fn symbolize(&self, value: u64) -> Symbol {
        // Small integers and obvious non-addresses fall through to
        // Scalar so the output does not lie about random qwords being
        // pointers. Threshold matches the lowest plausible VA on
        // amd64 / arm64 user-space.
        if value < 0x1000 {
            return Symbol::Scalar { value };
        }

        if let Some(it) = self.itab_by_addr.get(&value) {
            return Symbol::Itab {
                interface: it.interface_name.clone(),
                concrete: it.concrete_name.clone(),
                methods: it
                    .methods
                    .iter()
                    .map(|m| ItabMethodEntry {
                        name: m.interface_method.clone(),
                        concrete_fn: m.concrete_fn,
                    })
                    .collect(),
            };
        }
        if let Some(f) = self.function_by_addr.get(&value) {
            return Symbol::Function {
                name: f.name.clone(),
                entry: f.address,
                offset: 0,
            };
        }
        // Mid-function PC: look up the containing function via pclntab.
        if let Some(f) = self.pcln.lookup(value) {
            let entry = f.address;
            return Symbol::Function {
                name: f.name,
                entry,
                offset: value - entry,
            };
        }
        for s in &self.bin.sections {
            if s.contains_addr(value) && !s.name.is_empty() {
                return Symbol::SectionOffset {
                    section: s.name.clone(),
                    offset: value - s.addr,
                };
            }
        }
        Symbol::Unmapped
    }
}