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