|
diff --git a/zig-elf/main.zig b/zig-elf/main.zig
|
| 1 |
// $ readelf -h ./elf |
1 |
// $ readelf -h ./elf |
| 2 |
// https://github.com/bminor/binutils-gdb/blob/master/binutils/readelf.c |
2 |
// https://github.com/bminor/binutils-gdb/blob/master/binutils/readelf.c |
| 3 |
|
3 |
|
| 4 |
// There is still a problem with packed structs and aligment padding |
|
|
| 5 |
// better referenced in these two threads (sometime struct is not aligned |
|
|
| 6 |
// properly or even has 1 byte more than its C alternative): |
|
|
| 7 |
// - https://ziggit.dev/t/mapping-64-u8-buffer-to-a-struct/6052/19 |
|
|
| 8 |
// - https://ziggit.dev/t/wrong-sizes-of-extern-packed-structs/6053/6 |
|
|
| 9 |
|
|
|
| 10 |
const std = @import("std"); |
4 |
const std = @import("std"); |
| 11 |
|
5 |
|
| 12 |
const Elf64ExecutionHeader = packed struct { |
6 |
const Elf64ExecutionHeader = packed struct { |
| ... |
| 30 |
var file = try std.fs.cwd().openFile("./elf", .{}); |
24 |
var file = try std.fs.cwd().openFile("./elf", .{}); |
| 31 |
defer file.close(); |
25 |
defer file.close(); |
| 32 |
|
26 |
|
| 33 |
var buffer: [64]u8 = undefined; |
27 |
var buffer: [64]u8 align(@alignOf(Elf64ExecutionHeader)) = undefined; |
| 34 |
_ = try file.read(buffer[0..]); |
28 |
_ = try file.read(buffer[0..@sizeOf(Elf64ExecutionHeader)]); |
| 35 |
|
29 |
|
| 36 |
const header: *Elf64ExecutionHeader = @ptrCast(@alignCast(&buffer)); |
30 |
const header: *Elf64ExecutionHeader = @ptrCast(&buffer); |
| 37 |
|
31 |
|
| 38 |
std.debug.print("sizeOf(Elf64ExecutionHeader) = {}\n", .{@sizeOf(Elf64ExecutionHeader)}); |
32 |
std.debug.print("sizeOf(Elf64ExecutionHeader) = {}\n", .{@sizeOf(Elf64ExecutionHeader)}); |
| 39 |
std.debug.print("bitSizeOf(Elf64ExecutionHeader) = {} ({}/8={d})\n", .{ |
33 |
std.debug.print("bitSizeOf(Elf64ExecutionHeader) = {} ({}/8={d})\n", .{ |
| ... |
| 51 |
std.debug.print("Processor-specific flags: 0x{X:0>2}\n", .{header.flags}); |
45 |
std.debug.print("Processor-specific flags: 0x{X:0>2}\n", .{header.flags}); |
| 52 |
std.debug.print("ELF header size in bytes: {}\n", .{header.ehsize}); |
46 |
std.debug.print("ELF header size in bytes: {}\n", .{header.ehsize}); |
| 53 |
std.debug.print("Program header table entry size: {}\n", .{header.phentsize}); |
47 |
std.debug.print("Program header table entry size: {}\n", .{header.phentsize}); |
| 54 |
// std.debug.print("Program header table entry count: {}\n", .{header.phnum}); |
48 |
std.debug.print("Program header table entry count: {}\n", .{header.phnum}); |
| 55 |
// std.debug.print("Section header table entry size: {}\n", .{header.shentsize}); |
49 |
std.debug.print("Section header table entry size: {}\n", .{header.shentsize}); |
| 56 |
// std.debug.print("Section header table entry count: {}\n", .{header.shnum}); |
50 |
std.debug.print("Section header table entry count: {}\n", .{header.shnum}); |
| 57 |
// std.debug.print("Section header string table index: {}\n", .{header.shstrndx}); |
51 |
std.debug.print("Section header string table index: {}\n", .{header.shstrndx}); |
| 58 |
} |
52 |
} |