diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/elfo.zig (renamed from src/elfo-pretty.zig) | 40 |
1 files changed, 30 insertions, 10 deletions
diff --git a/src/elfo-pretty.zig b/src/elfo.zig index 1482670..20f798a 100644 --- a/src/elfo-pretty.zig +++ b/src/elfo.zig @@ -1,5 +1,6 @@ const std = @import("std"); const cs = @import("capstone"); +const meta_opts = @import("meta"); pub fn main(init: std.process.Init) !void { var args = try init.minimal.args.iterateAllocator(init.gpa); @@ -13,19 +14,15 @@ pub fn main(init: std.process.Init) !void { // const mem_config: cs.cs_opt_mem = undefined; // std.debug.assert(cs.cs_option(0, cs.CS_OPT_MEM, @intFromPtr(&mem_config)) == cs.CS_ERR_OK); - var handle: usize = undefined; - defer _ = cs.cs_close(@ptrCast(&handle)); - // TODO: read the arch from the elf so we open the equivalent capstone handle - std.debug.assert(cs.cs_open(cs.CS_ARCH_X86, cs.CS_MODE_64, @ptrCast(&handle)) == cs.CS_ERR_OK); + if (meta_opts.gdb) @breakpoint(); try printElf( init.gpa, init.io, args.next() orelse "./study-samples/split", stderr.terminal(), - handle, .{ - // .show_unaddressable_sections = true, + .show_unaddressable_sections = true, // .skip_sections_content = true, }, ); @@ -36,7 +33,6 @@ pub fn printElf( io: std.Io, path: []const u8, term: std.Io.Terminal, - handle: usize, options: struct { show_unaddressable_sections: bool = false, skip_sections_content: bool = false, @@ -51,6 +47,20 @@ pub fn printElf( var reader = f.reader(io, buffer); const header = try std.elf.Header.read(&reader.interface); + var handle: usize = undefined; + defer _ = cs.cs_close(@ptrCast(&handle)); + const opts: struct { arch: u64, mode: u64 } = switch (header.machine) { + .X86_64 => .{ .arch = cs.CS_ARCH_X86, .mode = cs.CS_MODE_64 }, + // The arm mode isn't working with symbols on the .text section correctly + .ARM => .{ .arch = cs.CS_ARCH_ARM, .mode = cs.CS_MODE_ARM }, + else => { + std.debug.print("found machine: {any}\n", .{header.machine}); + @panic("unhandled arch"); + }, + }; + + std.debug.assert(cs.cs_open(@intCast(opts.arch), @intCast(opts.mode), @ptrCast(&handle)) == cs.CS_ERR_OK); + const shstrtab = blk: { var section_it = header.iterateSectionHeaders(&reader); var section_idx: u32 = 0; @@ -143,7 +153,10 @@ pub fn printElf( var section_it = header.iterateSectionHeaders(&reader); while (try section_it.next()) |s| { if (s.sh_type == std.elf.SHT_SYMTAB) { - try bw.print("sym: {any}\n", .{s}); + + // x64: symtab: .{ .sh_name = 1, .sh_type = 2, .sh_flags = 0, .sh_addr = 0, .sh_offset = 4256, .sh_size = 1680, .sh_link = 27, .sh_info = 45, .sh_addralign = 8, .sh_entsize = 24 } + // arm32: .{ .sh_name = 1, .sh_type = 2, .sh_flags = 0, .sh_addr = 0, .sh_offset = 4264, .sh_size = 1856, .sh_link = 27, .sh_info = 87, .sh_addralign = 4, .sh_entsize = 16 } + try bw.print("symtab: {any}\n", .{s}); break :blk s; } } @@ -154,13 +167,13 @@ pub fn printElf( var section_it = header.iterateSectionHeaders(&reader); while (try section_it.next()) |s| { if (s.sh_type == std.elf.SHT_DYNSYM) { - try bw.print("sym: {any}\n", .{s}); + // try bw.print("sym: {any}\n", .{s}); break :blk s; } } break :blk null; }; - try bw.print("dynsym: {any}\n", .{dynsym}); + // try bw.print("dynsym: {any}\n", .{dynsym}); var symbols_index = blk: { var syms: std.ArrayList(SymbolRange) = try .initCapacity(gpa, 8); @@ -319,6 +332,7 @@ fn allocComment( std.debug.dumpHex(s); break :blk 0; }; + // FIXME: this algorithm isn't working to find addresses "inside" symbols if (v > 0) { const idx = std.sort.lowerBound(SymbolRange, symbols, v, struct { fn inner(a: u64, sym: SymbolRange) std.math.Order { @@ -370,6 +384,12 @@ fn dumpInstr( try term.setColor(.bright_green); try bw.print("{s} ", .{instr.mnemonic}); try term.setColor(.reset); + + const mnemonic_strlen: u64 = @intCast(std.mem.find(u8, &instr.mnemonic, &.{0}).?); + const mnemonic_pad: u64 = 10; + for (0..(mnemonic_pad - mnemonic_strlen)) |_| { + try bw.printAsciiChar(' ', .{}); + } try bw.print("{s}", .{instr.op_str}); const asm_comment = try allocComment(gpa, @ptrCast(@constCast(&instr.op_str)), symbols); defer { |
