authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2024-10-01 03:54:42+02:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2024-10-01 03:54:42+02:00
log9bbfc864af0311403aa19538999b170dbd35b7dc
tree620dc5718c1ac3e8b317bb2c0afe1a0e48e2357a
parent604ff131dd79064535f82b453c5dcb648add171d
signature Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

std.elf: Parse and make available some extra fields on Header.

Closes #19830.

1 files changed, 23 insertions(+), 7 deletions(-)

lib/std/elf.zig+23-7
......@@ -468,9 +468,12 @@ pub const ET = enum(u16) {
468468
469469/// All integers are native endian.
470470pub const Header = struct {
471 is_64: bool,
471472 endian: std.builtin.Endian,
473 os_abi: OSABI,
474 abi_version: u8,
475 type: ET,
472476 machine: EM,
473 is_64: bool,
474477 entry: u64,
475478 phoff: u64,
476479 shoff: u64,
......@@ -507,6 +510,12 @@ pub const Header = struct {
507510 if (!mem.eql(u8, hdr32.e_ident[0..4], MAGIC)) return error.InvalidElfMagic;
508511 if (hdr32.e_ident[EI_VERSION] != 1) return error.InvalidElfVersion;
509512
513 const is_64 = switch (hdr32.e_ident[EI_CLASS]) {
514 ELFCLASS32 => false,
515 ELFCLASS64 => true,
516 else => return error.InvalidElfClass,
517 };
518
510519 const endian: std.builtin.Endian = switch (hdr32.e_ident[EI_DATA]) {
511520 ELFDATA2LSB => .little,
512521 ELFDATA2MSB => .big,
......@@ -514,11 +523,15 @@ pub const Header = struct {
514523 };
515524 const need_bswap = endian != native_endian;
516525
517 const is_64 = switch (hdr32.e_ident[EI_CLASS]) {
518 ELFCLASS32 => false,
519 ELFCLASS64 => true,
520 else => return error.InvalidElfClass,
521 };
526 const os_abi: OSABI = @enumFromInt(hdr32.e_ident[EI_OSABI]);
527
528 // The meaning of this value depends on `os_abi` so just make it available as `u8`.
529 const abi_version = hdr32.e_ident[EI_ABIVERSION];
530
531 const @"type" = if (need_bswap) blk: {
532 const value = @intFromEnum(hdr32.e_type);
533 break :blk @as(ET, @enumFromInt(@byteSwap(value)));
534 } else hdr32.e_type;
522535
523536 const machine = if (need_bswap) blk: {
524537 const value = @intFromEnum(hdr32.e_machine);
......@@ -526,9 +539,12 @@ pub const Header = struct {
526539 } else hdr32.e_machine;
527540
528541 return @as(Header, .{
542 .is_64 = is_64,
529543 .endian = endian,
544 .os_abi = os_abi,
545 .abi_version = abi_version,
546 .type = @"type",
530547 .machine = machine,
531 .is_64 = is_64,
532548 .entry = int(is_64, need_bswap, hdr32.e_entry, hdr64.e_entry),
533549 .phoff = int(is_64, need_bswap, hdr32.e_phoff, hdr64.e_phoff),
534550 .shoff = int(is_64, need_bswap, hdr32.e_shoff, hdr64.e_shoff),