/
coff.zig
401 lines (345 loc) · 13.2 KB
/
coff.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
// SPDX-License-Identifier: MIT
// Copyright (c) 2015-2021 Zig Contributors
// This file is part of [zig](https://ziglang.org/), which is MIT licensed.
// The MIT license requires this copyright notice to be included in all copies
// and substantial portions of the software.
const builtin = std.builtin;
const std = @import("std.zig");
const io = std.io;
const mem = std.mem;
const os = std.os;
const File = std.fs.File;
const ArrayList = std.ArrayList;
// CoffHeader.machine values
// see https://msdn.microsoft.com/en-us/library/windows/desktop/ms680313(v=vs.85).aspx
const IMAGE_FILE_MACHINE_I386 = 0x014c;
const IMAGE_FILE_MACHINE_IA64 = 0x0200;
const IMAGE_FILE_MACHINE_AMD64 = 0x8664;
pub const MachineType = enum(u16) {
Unknown = 0x0,
/// Matsushita AM33
AM33 = 0x1d3,
/// x64
X64 = 0x8664,
/// ARM little endian
ARM = 0x1c0,
/// ARM64 little endian
ARM64 = 0xaa64,
/// ARM Thumb-2 little endian
ARMNT = 0x1c4,
/// EFI byte code
EBC = 0xebc,
/// Intel 386 or later processors and compatible processors
I386 = 0x14c,
/// Intel Itanium processor family
IA64 = 0x200,
/// Mitsubishi M32R little endian
M32R = 0x9041,
/// MIPS16
MIPS16 = 0x266,
/// MIPS with FPU
MIPSFPU = 0x366,
/// MIPS16 with FPU
MIPSFPU16 = 0x466,
/// Power PC little endian
POWERPC = 0x1f0,
/// Power PC with floating point support
POWERPCFP = 0x1f1,
/// MIPS little endian
R4000 = 0x166,
/// RISC-V 32-bit address space
RISCV32 = 0x5032,
/// RISC-V 64-bit address space
RISCV64 = 0x5064,
/// RISC-V 128-bit address space
RISCV128 = 0x5128,
/// Hitachi SH3
SH3 = 0x1a2,
/// Hitachi SH3 DSP
SH3DSP = 0x1a3,
/// Hitachi SH4
SH4 = 0x1a6,
/// Hitachi SH5
SH5 = 0x1a8,
/// Thumb
Thumb = 0x1c2,
/// MIPS little-endian WCE v2
WCEMIPSV2 = 0x169,
};
// OptionalHeader.magic values
// see https://msdn.microsoft.com/en-us/library/windows/desktop/ms680339(v=vs.85).aspx
const IMAGE_NT_OPTIONAL_HDR32_MAGIC = 0x10b;
const IMAGE_NT_OPTIONAL_HDR64_MAGIC = 0x20b;
// Image Characteristics
pub const IMAGE_FILE_RELOCS_STRIPPED = 0x1;
pub const IMAGE_FILE_DEBUG_STRIPPED = 0x200;
pub const IMAGE_FILE_EXECUTABLE_IMAGE = 0x2;
pub const IMAGE_FILE_32BIT_MACHINE = 0x100;
pub const IMAGE_FILE_LARGE_ADDRESS_AWARE = 0x20;
// Section flags
pub const IMAGE_SCN_CNT_INITIALIZED_DATA = 0x40;
pub const IMAGE_SCN_MEM_READ = 0x40000000;
pub const IMAGE_SCN_CNT_CODE = 0x20;
pub const IMAGE_SCN_MEM_EXECUTE = 0x20000000;
pub const IMAGE_SCN_MEM_WRITE = 0x80000000;
const IMAGE_NUMBEROF_DIRECTORY_ENTRIES = 16;
const IMAGE_DEBUG_TYPE_CODEVIEW = 2;
const DEBUG_DIRECTORY = 6;
pub const CoffError = error{
InvalidPEMagic,
InvalidPEHeader,
InvalidMachine,
MissingCoffSection,
MissingStringTable,
};
// Official documentation of the format: https://docs.microsoft.com/en-us/windows/win32/debug/pe-format
pub const Coff = struct {
in_file: File,
allocator: *mem.Allocator,
coff_header: CoffHeader,
pe_header: OptionalHeader,
sections: ArrayList(Section),
guid: [16]u8,
age: u32,
pub fn init(allocator: *mem.Allocator, in_file: File) Coff {
return Coff{
.in_file = in_file,
.allocator = allocator,
.coff_header = undefined,
.pe_header = undefined,
.sections = ArrayList(Section).init(allocator),
.guid = undefined,
.age = undefined,
};
}
pub fn loadHeader(self: *Coff) !void {
const pe_pointer_offset = 0x3C;
const in = self.in_file.reader();
var magic: [2]u8 = undefined;
try in.readNoEof(magic[0..]);
if (!mem.eql(u8, &magic, "MZ"))
return error.InvalidPEMagic;
// Seek to PE File Header (coff header)
try self.in_file.seekTo(pe_pointer_offset);
const pe_magic_offset = try in.readIntLittle(u32);
try self.in_file.seekTo(pe_magic_offset);
var pe_header_magic: [4]u8 = undefined;
try in.readNoEof(pe_header_magic[0..]);
if (!mem.eql(u8, &pe_header_magic, &[_]u8{ 'P', 'E', 0, 0 }))
return error.InvalidPEHeader;
self.coff_header = CoffHeader{
.machine = try in.readIntLittle(u16),
.number_of_sections = try in.readIntLittle(u16),
.timedate_stamp = try in.readIntLittle(u32),
.pointer_to_symbol_table = try in.readIntLittle(u32),
.number_of_symbols = try in.readIntLittle(u32),
.size_of_optional_header = try in.readIntLittle(u16),
.characteristics = try in.readIntLittle(u16),
};
switch (self.coff_header.machine) {
IMAGE_FILE_MACHINE_I386, IMAGE_FILE_MACHINE_AMD64, IMAGE_FILE_MACHINE_IA64 => {},
else => return error.InvalidMachine,
}
try self.loadOptionalHeader();
}
fn readStringFromTable(self: *Coff, offset: usize, buf: []u8) ![]const u8 {
if (self.coff_header.pointer_to_symbol_table == 0) {
// No symbol table therefore no string table
return error.MissingStringTable;
}
// The string table is at the end of the symbol table and symbols are 18 bytes long
const string_table_offset = self.coff_header.pointer_to_symbol_table + (self.coff_header.number_of_symbols * 18) + offset;
const in = self.in_file.reader();
const old_pos = try self.in_file.getPos();
try self.in_file.seekTo(string_table_offset);
defer {
self.in_file.seekTo(old_pos) catch unreachable;
}
const str = try in.readUntilDelimiterOrEof(buf, 0);
return str orelse "";
}
fn loadOptionalHeader(self: *Coff) !void {
const in = self.in_file.reader();
const opt_header_pos = try self.in_file.getPos();
self.pe_header.magic = try in.readIntLittle(u16);
// All we care about is the image base value and PDB info
// The header structure is different for 32 or 64 bit
var num_rva_pos: u64 = undefined;
if (self.pe_header.magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
num_rva_pos = opt_header_pos + 92;
try self.in_file.seekTo(opt_header_pos + 28);
const image_base32 = try in.readIntLittle(u32);
self.pe_header.image_base = image_base32;
} else if (self.pe_header.magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
num_rva_pos = opt_header_pos + 108;
try self.in_file.seekTo(opt_header_pos + 24);
self.pe_header.image_base = try in.readIntLittle(u64);
} else return error.InvalidPEMagic;
try self.in_file.seekTo(num_rva_pos);
const number_of_rva_and_sizes = try in.readIntLittle(u32);
if (number_of_rva_and_sizes != IMAGE_NUMBEROF_DIRECTORY_ENTRIES)
return error.InvalidPEHeader;
for (self.pe_header.data_directory) |*data_dir| {
data_dir.* = OptionalHeader.DataDirectory{
.virtual_address = try in.readIntLittle(u32),
.size = try in.readIntLittle(u32),
};
}
}
pub fn getPdbPath(self: *Coff, buffer: []u8) !usize {
try self.loadSections();
const header = blk: {
if (self.getSection(".buildid")) |section| {
break :blk section.header;
} else if (self.getSection(".rdata")) |section| {
break :blk section.header;
} else {
return error.MissingCoffSection;
}
};
const debug_dir = &self.pe_header.data_directory[DEBUG_DIRECTORY];
const file_offset = debug_dir.virtual_address - header.virtual_address + header.pointer_to_raw_data;
const in = self.in_file.reader();
try self.in_file.seekTo(file_offset);
// Find the correct DebugDirectoryEntry, and where its data is stored.
// It can be in any section.
const debug_dir_entry_count = debug_dir.size / @sizeOf(DebugDirectoryEntry);
var i: u32 = 0;
blk: while (i < debug_dir_entry_count) : (i += 1) {
const debug_dir_entry = try in.readStruct(DebugDirectoryEntry);
if (debug_dir_entry.type == IMAGE_DEBUG_TYPE_CODEVIEW) {
for (self.sections.items) |*section| {
const section_start = section.header.virtual_address;
const section_size = section.header.misc.virtual_size;
const rva = debug_dir_entry.address_of_raw_data;
const offset = rva - section_start;
if (section_start <= rva and offset < section_size and debug_dir_entry.size_of_data <= section_size - offset) {
try self.in_file.seekTo(section.header.pointer_to_raw_data + offset);
break :blk;
}
}
}
}
var cv_signature: [4]u8 = undefined; // CodeView signature
try in.readNoEof(cv_signature[0..]);
// 'RSDS' indicates PDB70 format, used by lld.
if (!mem.eql(u8, &cv_signature, "RSDS"))
return error.InvalidPEMagic;
try in.readNoEof(self.guid[0..]);
self.age = try in.readIntLittle(u32);
// Finally read the null-terminated string.
var byte = try in.readByte();
i = 0;
while (byte != 0 and i < buffer.len) : (i += 1) {
buffer[i] = byte;
byte = try in.readByte();
}
if (byte != 0 and i == buffer.len)
return error.NameTooLong;
return @as(usize, i);
}
pub fn loadSections(self: *Coff) !void {
if (self.sections.items.len == self.coff_header.number_of_sections)
return;
try self.sections.ensureCapacity(self.coff_header.number_of_sections);
const in = self.in_file.reader();
var name: [32]u8 = undefined;
var i: u16 = 0;
while (i < self.coff_header.number_of_sections) : (i += 1) {
try in.readNoEof(name[0..8]);
if (name[0] == '/') {
// This is a long name and stored in the string table
const offset_len = mem.indexOfScalar(u8, name[1..], 0) orelse 7;
const str_offset = try std.fmt.parseInt(u32, name[1 .. offset_len + 1], 10);
const str = try self.readStringFromTable(str_offset, &name);
std.mem.set(u8, name[str.len..], 0);
} else {
std.mem.set(u8, name[8..], 0);
}
try self.sections.append(Section{
.header = SectionHeader{
.name = name,
.misc = SectionHeader.Misc{ .virtual_size = try in.readIntLittle(u32) },
.virtual_address = try in.readIntLittle(u32),
.size_of_raw_data = try in.readIntLittle(u32),
.pointer_to_raw_data = try in.readIntLittle(u32),
.pointer_to_relocations = try in.readIntLittle(u32),
.pointer_to_line_numbers = try in.readIntLittle(u32),
.number_of_relocations = try in.readIntLittle(u16),
.number_of_line_numbers = try in.readIntLittle(u16),
.characteristics = try in.readIntLittle(u32),
},
});
}
}
pub fn getSection(self: *Coff, comptime name: []const u8) ?*Section {
for (self.sections.items) |*sec| {
if (mem.eql(u8, sec.header.name[0..name.len], name)) {
return sec;
}
}
return null;
}
// Return an owned slice full of the section data
pub fn getSectionData(self: *Coff, comptime name: []const u8, allocator: *mem.Allocator) ![]u8 {
const sec = for (self.sections.items) |*sec| {
if (mem.eql(u8, sec.header.name[0..name.len], name)) {
break sec;
}
} else {
return error.MissingCoffSection;
};
const in = self.in_file.reader();
try self.in_file.seekTo(sec.header.pointer_to_raw_data);
const out_buff = try allocator.alloc(u8, sec.header.misc.virtual_size);
try in.readNoEof(out_buff);
return out_buff;
}
};
const CoffHeader = struct {
machine: u16,
number_of_sections: u16,
timedate_stamp: u32,
pointer_to_symbol_table: u32,
number_of_symbols: u32,
size_of_optional_header: u16,
characteristics: u16,
};
const OptionalHeader = struct {
const DataDirectory = struct {
virtual_address: u32,
size: u32,
};
magic: u16,
data_directory: [IMAGE_NUMBEROF_DIRECTORY_ENTRIES]DataDirectory,
image_base: u64,
};
const DebugDirectoryEntry = packed 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 Section = struct {
header: SectionHeader,
};
const SectionHeader = struct {
const Misc = union {
physical_address: u32,
virtual_size: u32,
};
name: [32]u8,
misc: Misc,
virtual_address: u32,
size_of_raw_data: u32,
pointer_to_raw_data: u32,
pointer_to_relocations: u32,
pointer_to_line_numbers: u32,
number_of_relocations: u16,
number_of_line_numbers: u16,
characteristics: u32,
};