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
signaturebadge-check 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) {...@@ -468,9 +468,12 @@ pub const ET = enum(u16) {
468468
469/// All integers are native endian.469/// All integers are native endian.
470pub const Header = struct {470pub const Header = struct {
471 is_64: bool,
471 endian: std.builtin.Endian,472 endian: std.builtin.Endian,
473 os_abi: OSABI,
474 abi_version: u8,
475 type: ET,
472 machine: EM,476 machine: EM,
473 is_64: bool,
474 entry: u64,477 entry: u64,
475 phoff: u64,478 phoff: u64,
476 shoff: u64,479 shoff: u64,
...@@ -507,6 +510,12 @@ pub const Header = struct {...@@ -507,6 +510,12 @@ pub const Header = struct {
507 if (!mem.eql(u8, hdr32.e_ident[0..4], MAGIC)) return error.InvalidElfMagic;510 if (!mem.eql(u8, hdr32.e_ident[0..4], MAGIC)) return error.InvalidElfMagic;
508 if (hdr32.e_ident[EI_VERSION] != 1) return error.InvalidElfVersion;511 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
510 const endian: std.builtin.Endian = switch (hdr32.e_ident[EI_DATA]) {519 const endian: std.builtin.Endian = switch (hdr32.e_ident[EI_DATA]) {
511 ELFDATA2LSB => .little,520 ELFDATA2LSB => .little,
512 ELFDATA2MSB => .big,521 ELFDATA2MSB => .big,
...@@ -514,11 +523,15 @@ pub const Header = struct {...@@ -514,11 +523,15 @@ pub const Header = struct {
514 };523 };
515 const need_bswap = endian != native_endian;524 const need_bswap = endian != native_endian;
516525
517 const is_64 = switch (hdr32.e_ident[EI_CLASS]) {526 const os_abi: OSABI = @enumFromInt(hdr32.e_ident[EI_OSABI]);
518 ELFCLASS32 => false,527
519 ELFCLASS64 => true,528 // The meaning of this value depends on `os_abi` so just make it available as `u8`.
520 else => return error.InvalidElfClass,529 const abi_version = hdr32.e_ident[EI_ABIVERSION];
521 };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
523 const machine = if (need_bswap) blk: {536 const machine = if (need_bswap) blk: {
524 const value = @intFromEnum(hdr32.e_machine);537 const value = @intFromEnum(hdr32.e_machine);
...@@ -526,9 +539,12 @@ pub const Header = struct {...@@ -526,9 +539,12 @@ pub const Header = struct {
526 } else hdr32.e_machine;539 } else hdr32.e_machine;
527540
528 return @as(Header, .{541 return @as(Header, .{
542 .is_64 = is_64,
529 .endian = endian,543 .endian = endian,
544 .os_abi = os_abi,
545 .abi_version = abi_version,
546 .type = @"type",
530 .machine = machine,547 .machine = machine,
531 .is_64 = is_64,
532 .entry = int(is_64, need_bswap, hdr32.e_entry, hdr64.e_entry),548 .entry = int(is_64, need_bswap, hdr32.e_entry, hdr64.e_entry),
533 .phoff = int(is_64, need_bswap, hdr32.e_phoff, hdr64.e_phoff),549 .phoff = int(is_64, need_bswap, hdr32.e_phoff, hdr64.e_phoff),
534 .shoff = int(is_64, need_bswap, hdr32.e_shoff, hdr64.e_shoff),550 .shoff = int(is_64, need_bswap, hdr32.e_shoff, hdr64.e_shoff),