authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-08-30 12:26:35+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-08-30 12:26:35+02:00
log69d6931be70bf8a1e7ba25bec1c8fe713f8959b5
tree5357c3c6b92b456c50e3a45a754696d982960f91
parente6be6d97683d5443f48ce8b6d86fdf27f0f1d556
parent91b9f295d3ad74ac0ba9d1329bf5c581a45f6e89
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #12662 from wsengir/coff-fix

coff: fix reading COFF header offset

1 files changed, 77 insertions(+), 12 deletions(-)

lib/std/coff.zig+77-12
......@@ -256,20 +256,89 @@ pub const OptionalHeaderPE64 = extern struct {
256256 number_of_rva_and_sizes: u32,
257257};
258258
259pub const IMAGE_NUMBEROF_DIRECTORY_ENTRIES = 16;
260
261pub 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
308pub const ImageDataDirectory = extern struct {
309 virtual_address: u32,
310 size: u32,
311};
312
259313pub const DebugDirectoryEntry = extern struct {
260 characteristiccs: u32,
314 characteristics: u32,
261315 time_date_stamp: u32,
262316 major_version: u16,
263317 minor_version: u16,
264 @"type": u32,
318 @"type": DebugType,
265319 size_of_data: u32,
266320 address_of_raw_data: u32,
267321 pointer_to_raw_data: u32,
268322};
269323
270pub const ImageDataDirectory = extern struct {
271 virtual_address: u32,
272 size: u32,
324pub const DebugType = enum(u32) {
325 UNKNOWN = 0,
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,
273342};
274343
275344pub const SectionHeader = extern struct {
......@@ -794,10 +863,6 @@ pub const MachineType = enum(u16) {
794863 }
795864};
796865
797const IMAGE_NUMBEROF_DIRECTORY_ENTRIES = 16;
798const IMAGE_DEBUG_TYPE_CODEVIEW = 2;
799const DEBUG_DIRECTORY = 6;
800
801866pub const CoffError = error{
802867 InvalidPEMagic,
803868 InvalidPEHeader,
......@@ -831,7 +896,7 @@ pub const Coff = struct {
831896 var stream = std.io.fixedBufferStream(self.data);
832897 const reader = stream.reader();
833898 try stream.seekTo(pe_pointer_offset);
834 const coff_header_offset = try reader.readByte();
899 const coff_header_offset = try reader.readIntLittle(u32);
835900 try stream.seekTo(coff_header_offset);
836901 var buf: [4]u8 = undefined;
837902 try reader.readNoEof(&buf);
......@@ -862,7 +927,7 @@ pub const Coff = struct {
862927 };
863928
864929 const data_dirs = self.getDataDirectories();
865 const debug_dir = data_dirs[DEBUG_DIRECTORY];
930 const debug_dir = data_dirs[@enumToInt(DirectoryEntry.DEBUG)];
866931 const file_offset = debug_dir.virtual_address - header.virtual_address + header.pointer_to_raw_data;
867932
868933 var stream = std.io.fixedBufferStream(self.data);
......@@ -875,7 +940,7 @@ pub const Coff = struct {
875940 var i: u32 = 0;
876941 blk: while (i < debug_dir_entry_count) : (i += 1) {
877942 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) {
879944 for (self.getSectionHeaders()) |*section| {
880945 const section_start = section.virtual_address;
881946 const section_size = section.virtual_size;