From 3860e664c596bb4c0425de58768d63ee95fe36ef Mon Sep 17 00:00:00 2001 From: William Sengir Date: Sun, 28 Aug 2022 03:10:39 -0700 Subject: [PATCH 1/2] coff: fix reading COFF header offset --- lib/std/coff.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/std/coff.zig b/lib/std/coff.zig index f9de318e7a..a6d95735cd 100644 --- a/lib/std/coff.zig +++ b/lib/std/coff.zig @@ -831,7 +831,7 @@ pub const Coff = struct { var stream = std.io.fixedBufferStream(self.data); const reader = stream.reader(); try stream.seekTo(pe_pointer_offset); - const coff_header_offset = try reader.readByte(); + const coff_header_offset = try reader.readIntLittle(u32); try stream.seekTo(coff_header_offset); var buf: [4]u8 = undefined; try reader.readNoEof(&buf); From 91b9f295d3ad74ac0ba9d1329bf5c581a45f6e89 Mon Sep 17 00:00:00 2001 From: William Sengir Date: Sun, 28 Aug 2022 03:14:15 -0700 Subject: [PATCH 2/2] coff: publicize and flesh out more image constants --- lib/std/coff.zig | 95 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 80 insertions(+), 15 deletions(-) diff --git a/lib/std/coff.zig b/lib/std/coff.zig index a6d95735cd..47ec155df8 100644 --- a/lib/std/coff.zig +++ b/lib/std/coff.zig @@ -256,15 +256,53 @@ pub const OptionalHeaderPE64 = extern struct { number_of_rva_and_sizes: u32, }; -pub const DebugDirectoryEntry = extern struct { - characteristiccs: u32, - time_date_stamp: u32, - major_version: u16, - minor_version: u16, - @"type": u32, - size_of_data: u32, - address_of_raw_data: u32, - pointer_to_raw_data: u32, +pub const IMAGE_NUMBEROF_DIRECTORY_ENTRIES = 16; + +pub const DirectoryEntry = enum(u16) { + /// Export Directory + EXPORT = 0, + + /// Import Directory + IMPORT = 1, + + /// Resource Directory + RESOURCE = 2, + + /// Exception Directory + EXCEPTION = 3, + + /// Security Directory + SECURITY = 4, + + /// Base Relocation Table + BASERELOC = 5, + + /// Debug Directory + DEBUG = 6, + + /// Architecture Specific Data + ARCHITECTURE = 7, + + /// RVA of GP + GLOBALPTR = 8, + + /// TLS Directory + TLS = 9, + + /// Load Configuration Directory + LOAD_CONFIG = 10, + + /// Bound Import Directory in headers + BOUND_IMPORT = 11, + + /// Import Address Table + IAT = 12, + + /// Delay Load Import Descriptors + DELAY_IMPORT = 13, + + /// COM Runtime descriptor + COM_DESCRIPTOR = 14, }; pub const ImageDataDirectory = extern struct { @@ -272,6 +310,37 @@ pub const ImageDataDirectory = extern struct { size: u32, }; +pub const DebugDirectoryEntry = extern struct { + characteristics: u32, + time_date_stamp: u32, + major_version: u16, + minor_version: u16, + @"type": DebugType, + size_of_data: u32, + address_of_raw_data: u32, + pointer_to_raw_data: u32, +}; + +pub const DebugType = enum(u32) { + UNKNOWN = 0, + COFF = 1, + CODEVIEW = 2, + FPO = 3, + MISC = 4, + EXCEPTION = 5, + FIXUP = 6, + OMAP_TO_SRC = 7, + OMAP_FROM_SRC = 8, + BORLAND = 9, + RESERVED10 = 10, + VC_FEATURE = 12, + POGO = 13, + ILTCG = 14, + MPX = 15, + REPRO = 16, + EX_DLLCHARACTERISTICS = 20, +}; + pub const SectionHeader = extern struct { name: [8]u8, virtual_size: u32, @@ -794,10 +863,6 @@ pub const MachineType = enum(u16) { } }; -const IMAGE_NUMBEROF_DIRECTORY_ENTRIES = 16; -const IMAGE_DEBUG_TYPE_CODEVIEW = 2; -const DEBUG_DIRECTORY = 6; - pub const CoffError = error{ InvalidPEMagic, InvalidPEHeader, @@ -862,7 +927,7 @@ pub const Coff = struct { }; const data_dirs = self.getDataDirectories(); - const debug_dir = data_dirs[DEBUG_DIRECTORY]; + const debug_dir = data_dirs[@enumToInt(DirectoryEntry.DEBUG)]; const file_offset = debug_dir.virtual_address - header.virtual_address + header.pointer_to_raw_data; var stream = std.io.fixedBufferStream(self.data); @@ -875,7 +940,7 @@ pub const Coff = struct { var i: u32 = 0; blk: while (i < debug_dir_entry_count) : (i += 1) { const debug_dir_entry = try reader.readStruct(DebugDirectoryEntry); - if (debug_dir_entry.type == IMAGE_DEBUG_TYPE_CODEVIEW) { + if (debug_dir_entry.type == .CODEVIEW) { for (self.getSectionHeaders()) |*section| { const section_start = section.virtual_address; const section_size = section.virtual_size;