| ... | @@ -388,8 +388,8 @@ pub const Header = struct { | ... | @@ -388,8 +388,8 @@ pub const Header = struct { |
| 388 | }; | 388 | }; |
| 389 | } | 389 | } |
| 390 | | 390 | |
| 391 | pub fn section_header_iterator(self: Header, parse_source: anytype) SectionHeaderIterator { | 391 | pub fn section_header_iterator(self: Header, parse_source: anytype) SectionHeaderIterator(@TypeOf(parse_source)) { |
| 392 | return .{ | 392 | return SectionHeaderIterator(@TypeOf(parse_source)){ |
| 393 | .elf_header = self, | 393 | .elf_header = self, |
| 394 | .parse_source = parse_source, | 394 | .parse_source = parse_source, |
| 395 | }; | 395 | }; |
| ... | @@ -500,74 +500,76 @@ pub fn ProgramHeaderIterator(ParseSource: anytype) type { | ... | @@ -500,74 +500,76 @@ pub fn ProgramHeaderIterator(ParseSource: anytype) type { |
| 500 | }; | 500 | }; |
| 501 | } | 501 | } |
| 502 | | 502 | |
| 503 | pub const SectionHeaderIterator = struct { | 503 | pub fn SectionHeaderIterator(ParseSource: anytype) type { |
| 504 | elf_header: Header, | 504 | return struct { |
| 505 | parse_source: ParseSource, | 505 | elf_header: Header, |
| 506 | index: usize = 0, | 506 | parse_source: ParseSource, |
| | 507 | index: usize = 0, |
| | 508 | |
| | 509 | pub fn next(self: *@This()) !?Elf64_Shdr { |
| | 510 | if (self.index >= self.elf_header.shnum) return null; |
| | 511 | defer self.index += 1; |
| | 512 | |
| | 513 | if (self.elf_header.is_64) { |
| | 514 | var shdr: Elf64_Shdr = undefined; |
| | 515 | const offset = self.elf_header.shoff + @sizeOf(@TypeOf(shdr)) * self.index; |
| | 516 | try self.parse_source.readNoEof(mem.asBytes(&shdr), offset); |
| 507 | | 517 | |
| 508 | pub fn next(self: *SectionHeaderIterator) !?Elf64_Shdr { | 518 | // ELF endianness matches native endianness. |
| 509 | if (self.index >= self.elf_header.shnum) return null; | 519 | if (self.elf_header.endian == std.builtin.endian) return shdr; |
| 510 | defer self.index += 1; | | |
| 511 | | 520 | |
| 512 | if (self.elf_header.is_64) { | 521 | // Convert fields to native endianness. |
| 513 | var shdr: Elf64_Shdr = undefined; | 522 | return Elf64_Shdr{ |
| | 523 | .sh_name = @byteSwap(@TypeOf(shdr.sh_name), shdr.sh_name), |
| | 524 | .sh_type = @byteSwap(@TypeOf(shdr.sh_type), shdr.sh_type), |
| | 525 | .sh_flags = @byteSwap(@TypeOf(shdr.sh_flags), shdr.sh_flags), |
| | 526 | .sh_addr = @byteSwap(@TypeOf(shdr.sh_addr), shdr.sh_addr), |
| | 527 | .sh_offset = @byteSwap(@TypeOf(shdr.sh_offset), shdr.sh_offset), |
| | 528 | .sh_size = @byteSwap(@TypeOf(shdr.sh_size), shdr.sh_size), |
| | 529 | .sh_link = @byteSwap(@TypeOf(shdr.sh_link), shdr.sh_link), |
| | 530 | .sh_info = @byteSwap(@TypeOf(shdr.sh_info), shdr.sh_info), |
| | 531 | .sh_addralign = @byteSwap(@TypeOf(shdr.sh_addralign), shdr.sh_addralign), |
| | 532 | .sh_entsize = @byteSwap(@TypeOf(shdr.sh_entsize), shdr.sh_entsize), |
| | 533 | }; |
| | 534 | } |
| | 535 | |
| | 536 | var shdr: Elf32_Shdr = undefined; |
| 514 | const offset = self.elf_header.shoff + @sizeOf(@TypeOf(shdr)) * self.index; | 537 | const offset = self.elf_header.shoff + @sizeOf(@TypeOf(shdr)) * self.index; |
| 515 | try self.parse_source.readNoEof(mem.asBytes(&shdr), offset); | 538 | try self.parse_source.readNoEof(mem.asBytes(&shdr), offset); |
| 516 | | 539 | |
| 517 | // ELF endianness matches native endianness. | 540 | // ELF endianness does NOT match native endianness. |
| 518 | if (self.elf_header.endian == std.builtin.endian) return shdr; | 541 | if (self.elf_header.endian != std.builtin.endian) { |
| | 542 | // Convert fields to native endianness. |
| | 543 | shdr = .{ |
| | 544 | .sh_name = @byteSwap(@TypeOf(shdr.sh_name), shdr.sh_name), |
| | 545 | .sh_type = @byteSwap(@TypeOf(shdr.sh_type), shdr.sh_type), |
| | 546 | .sh_flags = @byteSwap(@TypeOf(shdr.sh_flags), shdr.sh_flags), |
| | 547 | .sh_addr = @byteSwap(@TypeOf(shdr.sh_addr), shdr.sh_addr), |
| | 548 | .sh_offset = @byteSwap(@TypeOf(shdr.sh_offset), shdr.sh_offset), |
| | 549 | .sh_size = @byteSwap(@TypeOf(shdr.sh_size), shdr.sh_size), |
| | 550 | .sh_link = @byteSwap(@TypeOf(shdr.sh_link), shdr.sh_link), |
| | 551 | .sh_info = @byteSwap(@TypeOf(shdr.sh_info), shdr.sh_info), |
| | 552 | .sh_addralign = @byteSwap(@TypeOf(shdr.sh_addralign), shdr.sh_addralign), |
| | 553 | .sh_entsize = @byteSwap(@TypeOf(shdr.sh_entsize), shdr.sh_entsize), |
| | 554 | }; |
| | 555 | } |
| 519 | | 556 | |
| 520 | // Convert fields to native endianness. | 557 | // Convert 32-bit header to 64-bit. |
| 521 | return Elf64_Shdr{ | 558 | return Elf64_Shdr{ |
| 522 | .sh_name = @byteSwap(@TypeOf(shdr.sh_name), shdr.sh_name), | 559 | .sh_name = shdr.sh_name, |
| 523 | .sh_type = @byteSwap(@TypeOf(shdr.sh_type), shdr.sh_type), | 560 | .sh_type = shdr.sh_type, |
| 524 | .sh_flags = @byteSwap(@TypeOf(shdr.sh_flags), shdr.sh_flags), | 561 | .sh_flags = shdr.sh_flags, |
| 525 | .sh_addr = @byteSwap(@TypeOf(shdr.sh_addr), shdr.sh_addr), | 562 | .sh_addr = shdr.sh_addr, |
| 526 | .sh_offset = @byteSwap(@TypeOf(shdr.sh_offset), shdr.sh_offset), | 563 | .sh_offset = shdr.sh_offset, |
| 527 | .sh_size = @byteSwap(@TypeOf(shdr.sh_size), shdr.sh_size), | 564 | .sh_size = shdr.sh_size, |
| 528 | .sh_link = @byteSwap(@TypeOf(shdr.sh_link), shdr.sh_link), | 565 | .sh_link = shdr.sh_link, |
| 529 | .sh_info = @byteSwap(@TypeOf(shdr.sh_info), shdr.sh_info), | 566 | .sh_info = shdr.sh_info, |
| 530 | .sh_addralign = @byteSwap(@TypeOf(shdr.sh_addralign), shdr.sh_addralign), | 567 | .sh_addralign = shdr.sh_addralign, |
| 531 | .sh_entsize = @byteSwap(@TypeOf(shdr.sh_entsize), shdr.sh_entsize), | 568 | .sh_entsize = shdr.sh_entsize, |
| 532 | }; | | |
| 533 | } | | |
| 534 | | | |
| 535 | var shdr: Elf32_Shdr = undefined; | | |
| 536 | const offset = self.elf_header.shoff + @sizeOf(@TypeOf(shdr)) * self.index; | | |
| 537 | try self.parse_source.readNoEof(mem.asBytes(&shdr), offset); | | |
| 538 | | | |
| 539 | // ELF endianness does NOT match native endianness. | | |
| 540 | if (self.elf_header.endian != std.builtin.endian) { | | |
| 541 | // Convert fields to native endianness. | | |
| 542 | shdr = .{ | | |
| 543 | .sh_name = @byteSwap(@TypeOf(shdr.sh_name), shdr.sh_name), | | |
| 544 | .sh_type = @byteSwap(@TypeOf(shdr.sh_type), shdr.sh_type), | | |
| 545 | .sh_flags = @byteSwap(@TypeOf(shdr.sh_flags), shdr.sh_flags), | | |
| 546 | .sh_addr = @byteSwap(@TypeOf(shdr.sh_addr), shdr.sh_addr), | | |
| 547 | .sh_offset = @byteSwap(@TypeOf(shdr.sh_offset), shdr.sh_offset), | | |
| 548 | .sh_size = @byteSwap(@TypeOf(shdr.sh_size), shdr.sh_size), | | |
| 549 | .sh_link = @byteSwap(@TypeOf(shdr.sh_link), shdr.sh_link), | | |
| 550 | .sh_info = @byteSwap(@TypeOf(shdr.sh_info), shdr.sh_info), | | |
| 551 | .sh_addralign = @byteSwap(@TypeOf(shdr.sh_addralign), shdr.sh_addralign), | | |
| 552 | .sh_entsize = @byteSwap(@TypeOf(shdr.sh_entsize), shdr.sh_entsize), | | |
| 553 | }; | 569 | }; |
| 554 | } | 570 | } |
| 555 | | 571 | }; |
| 556 | // Convert 32-bit header to 64-bit. | 572 | } |
| 557 | return Elf64_Shdr{ | | |
| 558 | .sh_name = shdr.sh_name, | | |
| 559 | .sh_type = shdr.sh_type, | | |
| 560 | .sh_flags = shdr.sh_flags, | | |
| 561 | .sh_addr = shdr.sh_addr, | | |
| 562 | .sh_offset = shdr.sh_offset, | | |
| 563 | .sh_size = shdr.sh_size, | | |
| 564 | .sh_link = shdr.sh_link, | | |
| 565 | .sh_info = shdr.sh_info, | | |
| 566 | .sh_addralign = shdr.sh_addralign, | | |
| 567 | .sh_entsize = shdr.sh_entsize, | | |
| 568 | }; | | |
| 569 | } | | |
| 570 | }; | | |
| 571 | | 573 | |
| 572 | pub fn int(is_64: bool, need_bswap: bool, int_32: anytype, int_64: anytype) @TypeOf(int_64) { | 574 | pub fn int(is_64: bool, need_bswap: bool, int_32: anytype, int_64: anytype) @TypeOf(int_64) { |
| 573 | if (is_64) { | 575 | if (is_64) { |