| ... | @@ -256,20 +256,89 @@ pub const OptionalHeaderPE64 = extern struct { | ... | @@ -256,20 +256,89 @@ pub const OptionalHeaderPE64 = extern struct { |
| 256 | number_of_rva_and_sizes: u32, | 256 | number_of_rva_and_sizes: u32, |
| 257 | }; | 257 | }; |
| 258 | | 258 | |
| | 259 | pub const IMAGE_NUMBEROF_DIRECTORY_ENTRIES = 16; |
| | 260 | |
| | 261 | pub const DirectoryEntry = enum(u16) { |
| | 262 | /// Export Directory |
| | 263 | EXPORT = 0, |
| | 264 | |
| | 265 | /// Import Directory |
| | 266 | IMPORT = 1, |
| | 267 | |
| | 268 | /// Resource Directory |
| | 269 | RESOURCE = 2, |
| | 270 | |
| | 271 | /// Exception Directory |
| | 272 | EXCEPTION = 3, |
| | 273 | |
| | 274 | /// Security Directory |
| | 275 | SECURITY = 4, |
| | 276 | |
| | 277 | /// Base Relocation Table |
| | 278 | BASERELOC = 5, |
| | 279 | |
| | 280 | /// Debug Directory |
| | 281 | DEBUG = 6, |
| | 282 | |
| | 283 | /// Architecture Specific Data |
| | 284 | ARCHITECTURE = 7, |
| | 285 | |
| | 286 | /// RVA of GP |
| | 287 | GLOBALPTR = 8, |
| | 288 | |
| | 289 | /// TLS Directory |
| | 290 | TLS = 9, |
| | 291 | |
| | 292 | /// Load Configuration Directory |
| | 293 | LOAD_CONFIG = 10, |
| | 294 | |
| | 295 | /// Bound Import Directory in headers |
| | 296 | BOUND_IMPORT = 11, |
| | 297 | |
| | 298 | /// Import Address Table |
| | 299 | IAT = 12, |
| | 300 | |
| | 301 | /// Delay Load Import Descriptors |
| | 302 | DELAY_IMPORT = 13, |
| | 303 | |
| | 304 | /// COM Runtime descriptor |
| | 305 | COM_DESCRIPTOR = 14, |
| | 306 | }; |
| | 307 | |
| | 308 | pub const ImageDataDirectory = extern struct { |
| | 309 | virtual_address: u32, |
| | 310 | size: u32, |
| | 311 | }; |
| | 312 | |
| 259 | pub const DebugDirectoryEntry = extern struct { | 313 | pub const DebugDirectoryEntry = extern struct { |
| 260 | characteristiccs: u32, | 314 | characteristics: u32, |
| 261 | time_date_stamp: u32, | 315 | time_date_stamp: u32, |
| 262 | major_version: u16, | 316 | major_version: u16, |
| 263 | minor_version: u16, | 317 | minor_version: u16, |
| 264 | @"type": u32, | 318 | @"type": DebugType, |
| 265 | size_of_data: u32, | 319 | size_of_data: u32, |
| 266 | address_of_raw_data: u32, | 320 | address_of_raw_data: u32, |
| 267 | pointer_to_raw_data: u32, | 321 | pointer_to_raw_data: u32, |
| 268 | }; | 322 | }; |
| 269 | | 323 | |
| 270 | pub const ImageDataDirectory = extern struct { | 324 | pub const DebugType = enum(u32) { |
| 271 | virtual_address: u32, | 325 | UNKNOWN = 0, |
| 272 | size: u32, | 326 | COFF = 1, |
| | 327 | CODEVIEW = 2, |
| | 328 | FPO = 3, |
| | 329 | MISC = 4, |
| | 330 | EXCEPTION = 5, |
| | 331 | FIXUP = 6, |
| | 332 | OMAP_TO_SRC = 7, |
| | 333 | OMAP_FROM_SRC = 8, |
| | 334 | BORLAND = 9, |
| | 335 | RESERVED10 = 10, |
| | 336 | VC_FEATURE = 12, |
| | 337 | POGO = 13, |
| | 338 | ILTCG = 14, |
| | 339 | MPX = 15, |
| | 340 | REPRO = 16, |
| | 341 | EX_DLLCHARACTERISTICS = 20, |
| 273 | }; | 342 | }; |
| 274 | | 343 | |
| 275 | pub const SectionHeader = extern struct { | 344 | pub const SectionHeader = extern struct { |
| ... | @@ -794,10 +863,6 @@ pub const MachineType = enum(u16) { | ... | @@ -794,10 +863,6 @@ pub const MachineType = enum(u16) { |
| 794 | } | 863 | } |
| 795 | }; | 864 | }; |
| 796 | | 865 | |
| 797 | const IMAGE_NUMBEROF_DIRECTORY_ENTRIES = 16; | | |
| 798 | const IMAGE_DEBUG_TYPE_CODEVIEW = 2; | | |
| 799 | const DEBUG_DIRECTORY = 6; | | |
| 800 | | | |
| 801 | pub const CoffError = error{ | 866 | pub const CoffError = error{ |
| 802 | InvalidPEMagic, | 867 | InvalidPEMagic, |
| 803 | InvalidPEHeader, | 868 | InvalidPEHeader, |
| ... | @@ -831,7 +896,7 @@ pub const Coff = struct { | ... | @@ -831,7 +896,7 @@ pub const Coff = struct { |
| 831 | var stream = std.io.fixedBufferStream(self.data); | 896 | var stream = std.io.fixedBufferStream(self.data); |
| 832 | const reader = stream.reader(); | 897 | const reader = stream.reader(); |
| 833 | try stream.seekTo(pe_pointer_offset); | 898 | try stream.seekTo(pe_pointer_offset); |
| 834 | const coff_header_offset = try reader.readByte(); | 899 | const coff_header_offset = try reader.readIntLittle(u32); |
| 835 | try stream.seekTo(coff_header_offset); | 900 | try stream.seekTo(coff_header_offset); |
| 836 | var buf: [4]u8 = undefined; | 901 | var buf: [4]u8 = undefined; |
| 837 | try reader.readNoEof(&buf); | 902 | try reader.readNoEof(&buf); |
| ... | @@ -862,7 +927,7 @@ pub const Coff = struct { | ... | @@ -862,7 +927,7 @@ pub const Coff = struct { |
| 862 | }; | 927 | }; |
| 863 | | 928 | |
| 864 | const data_dirs = self.getDataDirectories(); | 929 | const data_dirs = self.getDataDirectories(); |
| 865 | const debug_dir = data_dirs[DEBUG_DIRECTORY]; | 930 | const debug_dir = data_dirs[@enumToInt(DirectoryEntry.DEBUG)]; |
| 866 | const file_offset = debug_dir.virtual_address - header.virtual_address + header.pointer_to_raw_data; | 931 | const file_offset = debug_dir.virtual_address - header.virtual_address + header.pointer_to_raw_data; |
| 867 | | 932 | |
| 868 | var stream = std.io.fixedBufferStream(self.data); | 933 | var stream = std.io.fixedBufferStream(self.data); |
| ... | @@ -875,7 +940,7 @@ pub const Coff = struct { | ... | @@ -875,7 +940,7 @@ pub const Coff = struct { |
| 875 | var i: u32 = 0; | 940 | var i: u32 = 0; |
| 876 | blk: while (i < debug_dir_entry_count) : (i += 1) { | 941 | blk: while (i < debug_dir_entry_count) : (i += 1) { |
| 877 | const debug_dir_entry = try reader.readStruct(DebugDirectoryEntry); | 942 | const debug_dir_entry = try reader.readStruct(DebugDirectoryEntry); |
| 878 | if (debug_dir_entry.type == IMAGE_DEBUG_TYPE_CODEVIEW) { | 943 | if (debug_dir_entry.type == .CODEVIEW) { |
| 879 | for (self.getSectionHeaders()) |*section| { | 944 | for (self.getSectionHeaders()) |*section| { |
| 880 | const section_start = section.virtual_address; | 945 | const section_start = section.virtual_address; |
| 881 | const section_size = section.virtual_size; | 946 | const section_size = section.virtual_size; |