~laumann/hadb

8b0cac704b94fa173336a621a99d0271a0fc38ff — Thomas Bracht Laumann Jespersen 4 months ago dccd672
cmd/decodeabbrev: make a cli tool for parsing .debug_abbrev

There's a new script that uses objdump to dump the raw bytes from an
object file. It can be combined with cmd/decodeabbrev like so:

    $ scripts/dumpelfsec.h <binary> .debug_abbrev | ./decodeabbrev

The script will dump any section that exists, it's not specific to
.debug_abbrev, but it relies on the output of objdump -h (I don't know
if that is reasonable).
2 files changed, 50 insertions(+), 0 deletions(-)

A cmd/decodeabbrev/main.ha
A scripts/dumpelfsec.sh
A cmd/decodeabbrev/main.ha => cmd/decodeabbrev/main.ha +34 -0
@@ 0,0 1,34 @@
use fmt;
use format::dwarf;
use os;
use io;

// FIXME There are often multiple sections in the .debug_abbrev
// section, we should try to exhaustively decode all of it.

export fn main() void = {
	fmt::println("usage: decodeabbrev [file]")!;
	for (let i = 0z; i < len(os::args); i +=1 ) {
		fmt::printfln(" - {}", os::args[i])!;
	};

	let f = if (len(os::args) < 2) {
		// read from os::stdin
		yield os::stdin_file;
	} else {
		// open file for reading
		fmt::printfln("opening {}", os::args[1])!;
		yield os::open(os::args[1])!;
	};

	let bin = io::drain(f)!;
	let (abbrevs, read) = dwarf::decodeabbrev(bin)!;
	defer dwarf::freeabbrevs(abbrevs);
	fmt::printfln("read {} ({:_04x}) bytes of {} total", read, read, len(bin))!;
	for (let i = 0z; i < len(abbrevs); i += 1) {
		const abbrev = abbrevs[i];
		fmt::printfln("<{}> tag = {:_02x} [{}]",
			abbrev.code: uint, abbrev.tag: uint,
			if (abbrev.haschildren) "has children" else "no children")!;
	};
};

A scripts/dumpelfsec.sh => scripts/dumpelfsec.sh +16 -0
@@ 0,0 1,16 @@
#!/bin/sh

# Give an ELF executable, extract (as bytes) a given section of it and pipe the
# bytes to stdout

EXE="$1"
SEC="$2"

stats="$(objdump -h "${EXE}" |grep "${SEC}")"
#echo "${stats}"
numbers=($(echo "${stats}" | awk '{ print strtonum("0x" $3), strtonum("0x" $6) }'))
#echo "${numbers[@]}"
#
#echo "count=${numbers[0]} skip=${numbers[1]}"

dd status=none if="${EXE}" bs=1 count="${numbers[0]}" skip="${numbers[1]}"