aboutsummaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authorJakub Konka <kubkon@jakubkonka.com>2022-08-30 12:26:35 +0200
committerGitHub <noreply@github.com>2022-08-30 12:26:35 +0200
commit69d6931be70bf8a1e7ba25bec1c8fe713f8959b5 (patch)
tree5357c3c6b92b456c50e3a45a754696d982960f91 /lib/std
parente6be6d97683d5443f48ce8b6d86fdf27f0f1d556 (diff)
parent91b9f295d3ad74ac0ba9d1329bf5c581a45f6e89 (diff)
downloadzig-69d6931be70bf8a1e7ba25bec1c8fe713f8959b5.tar.gz
zig-69d6931be70bf8a1e7ba25bec1c8fe713f8959b5.zip
Merge pull request #12662 from wsengir/coff-fix
coff: fix reading COFF header offset
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/coff.zig89
1 files changed, 77 insertions, 12 deletions
diff --git a/lib/std/coff.zig b/lib/std/coff.zig
index f9de318e7a..47ec155df8 100644
--- a/lib/std/coff.zig
+++ b/lib/std/coff.zig
@@ -256,20 +256,89 @@ pub const OptionalHeaderPE64 = extern struct {
number_of_rva_and_sizes: 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 {
+ virtual_address: u32,
+ size: u32,
+};
+
pub const DebugDirectoryEntry = extern struct {
- characteristiccs: u32,
+ characteristics: u32,
time_date_stamp: u32,
major_version: u16,
minor_version: u16,
- @"type": u32,
+ @"type": DebugType,
size_of_data: u32,
address_of_raw_data: u32,
pointer_to_raw_data: u32,
};
-pub const ImageDataDirectory = extern struct {
- virtual_address: u32,
- size: 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 {
@@ -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,
@@ -831,7 +896,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);
@@ -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;