authorgravatar for william@sengir.comWilliam Sengir <william@sengir.com> 2022-08-28 03:14:15-07:00
committergravatar for william@sengir.comWilliam Sengir <william@sengir.com> 2022-08-28 06:20:18-07:00
log91b9f295d3ad74ac0ba9d1329bf5c581a45f6e89
tree07fd8ff6039a87d9beab52712bed46fff07f02d4
parent3860e664c596bb4c0425de58768d63ee95fe36ef
signaturelock-open Commit is signed but in an unrecognized format.

coff: publicize and flesh out more image constants


1 files changed, 76 insertions(+), 11 deletions(-)

lib/std/coff.zig+76-11
...@@ -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};
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
259pub const DebugDirectoryEntry = extern struct {313pub 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};
269323
270pub const ImageDataDirectory = extern struct {324pub 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};
274343
275pub const SectionHeader = extern struct {344pub 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};
796865
797const IMAGE_NUMBEROF_DIRECTORY_ENTRIES = 16;
798const IMAGE_DEBUG_TYPE_CODEVIEW = 2;
799const DEBUG_DIRECTORY = 6;
800
801pub const CoffError = error{866pub const CoffError = error{
802 InvalidPEMagic,867 InvalidPEMagic,
803 InvalidPEHeader,868 InvalidPEHeader,
...@@ -862,7 +927,7 @@ pub const Coff = struct {...@@ -862,7 +927,7 @@ pub const Coff = struct {
862 };927 };
863928
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;
867932
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;