summaryrefslogtreecommitdiff
path: root/src/elfo.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-03-05 13:00:01 -0300
committerGabriel Schneider <[email protected]>2026-06-08 23:04:50 -0300
commit80ba1db819a80e784a488d6d660a890e14b19282 (patch)
treeda005b5161094f24349c56acd149e4e89876410d /src/elfo.zig
parent70388a8fc6770c797c6f298886e90c982b2c721f (diff)
downloadcodenomicon-80ba1db819a80e784a488d6d660a890e14b19282.tar.gz
codenomicon-80ba1db819a80e784a488d6d660a890e14b19282.zip
simple shock
Diffstat (limited to 'src/elfo.zig')
-rw-r--r--src/elfo.zig54
1 files changed, 28 insertions, 26 deletions
diff --git a/src/elfo.zig b/src/elfo.zig
index 5e0d780..264ba4d 100644
--- a/src/elfo.zig
+++ b/src/elfo.zig
@@ -1,7 +1,7 @@
const std = @import("std");
const cs = @import("capstone");
const hooks = @import("hooks.zig");
-const meta_opts = @import("meta");
+const meta = @import("meta");
pub fn main(init: std.process.Init.Minimal) !void {
var debug_alloc: std.heap.DebugAllocator(.{}) = .init;
@@ -21,21 +21,22 @@ pub fn main(init: std.process.Init.Minimal) !void {
var buffer: [64]u8 = undefined;
const stderr = try io.lockStderr(&buffer, .escape_codes);
- const hook = hooks.init(
- if (meta_opts.gdb) .breakpoint else .none,
- // TODO: this needs to be comptime, it'd be cool to don't need that
- std.heap.page_allocator,
- false,
- );
+ // const hook = hooks.init(
+ // if (meta.gdb) .breakpoint else .none,
+ // // TODO: this needs to be comptime, it'd be cool to don't need that
+ // std.heap.page_allocator,
+ // false,
+ // );
- const mem_config: cs.cs_opt_mem = .{
- .malloc = @ptrCast(&hook.malloc),
- .free = @ptrCast(&hook.free),
- .calloc = @ptrCast(&hook.calloc),
- .realloc = @ptrCast(&hook.realloc),
- .vsnprintf = @ptrCast(&hook.vsnprintf),
- };
- std.debug.assert(cs.cs_option(0, cs.CS_OPT_MEM, @intFromPtr(&mem_config)) == cs.CS_ERR_OK);
+ // const mem_config: cs.cs_opt_mem = .{
+ // .malloc = @ptrCast(&hook.malloc),
+ // .free = @ptrCast(&hook.free),
+ // .calloc = @ptrCast(&hook.calloc),
+ // .realloc = @ptrCast(&hook.realloc),
+ // // TODO: this function needs to be properly implemented.
+ // .vsnprintf = @ptrCast(&hook.vsnprintf),
+ // };
+ // std.debug.assert(cs.cs_option(0, cs.CS_OPT_MEM, @intFromPtr(&mem_config)) == cs.CS_ERR_OK);
try printElf(
alloc,
@@ -43,8 +44,8 @@ pub fn main(init: std.process.Init.Minimal) !void {
args.next() orelse "./study-samples/split",
stderr.terminal(),
.{
- .show_unaddressable_sections = true,
- .skip_sections_content = true,
+ // .show_unaddressable_sections = true,
+ // .skip_sections_content = true,
},
);
}
@@ -73,7 +74,7 @@ pub fn printElf(
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 },
+ .ARM => .{ .arch = cs.CS_ARCH_ARM, .mode = if (header.is_64) cs.CS_MODE_64 else cs.CS_MODE_32 },
else => {
std.debug.print("found machine: {any}\n", .{header.machine});
@panic("unhandled arch");
@@ -258,10 +259,11 @@ pub fn printElf(
section.sh_addr + section.sh_size,
section.sh_type,
});
+
try term.setColor(.bright_green);
if (elf_shstrtab_slice != null)
try bw.print("{s}", .{std.mem.sliceTo(elf_shstrtab_slice.?[section.sh_name..], 0)});
- try bw.print("", .{});
+ try bw.print("\n", .{});
try term.setColor(.reset);
// --
@@ -292,9 +294,9 @@ pub fn printElf(
break :blk insn[0..count];
};
- try dumpInstr(gpa, bw, term, instrs, symbols_index.items);
+ try printDisassembly(gpa, bw, term, instrs, symbols_index.items);
} else {
- try dumpHexFallible(u64, bw, term, section_slice.?, section.sh_addr);
+ try printHexdump(u64, bw, term, section_slice.?, section.sh_addr);
}
}
}
@@ -332,7 +334,7 @@ pub fn printElf(
}
};
if (section_slice != null and !options.skip_sections_content) {
- try dumpHexFallible(u64, bw, term, section_slice.?, section.sh_addr);
+ try printHexdump(u64, bw, term, section_slice.?, section.sh_addr);
}
}
}
@@ -375,7 +377,7 @@ fn allocComment(
return null;
}
-fn dumpInstr(
+fn printDisassembly(
gpa: std.mem.Allocator,
bw: *std.Io.Writer,
term: std.Io.Terminal,
@@ -407,8 +409,8 @@ fn dumpInstr(
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)) |_| {
+ const mnemonic_pad: u64 = 5;
+ for (0..(if (mnemonic_pad >= mnemonic_strlen) mnemonic_pad - mnemonic_strlen else 0)) |_| {
try bw.printAsciiChar(' ', .{});
}
try bw.print("{s}", .{instr.op_str});
@@ -427,7 +429,7 @@ fn dumpInstr(
}
/// Prints a hexadecimal view of the bytes, returning any error that occurs.
-pub fn dumpHexFallible(
+pub fn printHexdump(
_: type,
bw: *std.Io.Writer,
term: std.Io.Terminal,