aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJakub Konka <kubkon@jakubkonka.com>2022-08-22 19:48:14 +0200
committerJakub Konka <kubkon@jakubkonka.com>2022-08-23 08:55:04 +0200
commitc84e5ee87852eafff0cbf986bf02c5221cbcec35 (patch)
treea978b80bbcc7273ad3a0e3d4de843a8a3ec947e9 /src
parent9043e665a549b87fa73ade7af06682368092d8a9 (diff)
downloadzig-c84e5ee87852eafff0cbf986bf02c5221cbcec35.tar.gz
zig-c84e5ee87852eafff0cbf986bf02c5221cbcec35.zip
coff: improve default COFF/PE object parser
We now do not allocate memory for headers and other metadata unless requested by the caller. Instead, we read-in the entire contents of the image into memory and operate on pointers and casts wherever possible. I have a left a TODO to hook up Windows' memory-mapped API here in-place of standard `readToEndAlloc` which should be more memory proof on memory constrained hosts. This commit also supplements our `std.coff` with a lot missing basic extern structs required to make our COFF linker.
Diffstat (limited to 'src')
-rw-r--r--src/link/Coff.zig29
1 files changed, 18 insertions, 11 deletions
diff --git a/src/link/Coff.zig b/src/link/Coff.zig
index a01b9cf7c3..6536fbd1ac 100644
--- a/src/link/Coff.zig
+++ b/src/link/Coff.zig
@@ -204,15 +204,18 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option
index += 2;
// Characteristics
- var characteristics: u16 = std.coff.IMAGE_FILE_DEBUG_STRIPPED | std.coff.IMAGE_FILE_RELOCS_STRIPPED; // TODO Remove debug info stripped flag when necessary
+ var characteristics: std.coff.CoffHeaderFlags = .{
+ .DEBUG_STRIPPED = 1, // TODO remove debug info stripped flag when necessary
+ .RELOCS_STRIPPED = 1,
+ };
if (options.output_mode == .Exe) {
- characteristics |= std.coff.IMAGE_FILE_EXECUTABLE_IMAGE;
+ characteristics.EXECUTABLE_IMAGE = 1;
}
switch (self.ptr_width) {
- .p32 => characteristics |= std.coff.IMAGE_FILE_32BIT_MACHINE,
- .p64 => characteristics |= std.coff.IMAGE_FILE_LARGE_ADDRESS_AWARE,
+ .p32 => characteristics.@"32BIT_MACHINE" = 1,
+ .p64 => characteristics.LARGE_ADDRESS_AWARE = 1,
}
- mem.writeIntLittle(u16, hdr_data[index..][0..2], characteristics);
+ mem.writeIntLittle(u16, hdr_data[index..][0..2], @bitCast(u16, characteristics));
index += 2;
assert(index == 20);
@@ -352,7 +355,10 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option
mem.set(u8, hdr_data[index..][0..12], 0);
index += 12;
// Section flags
- mem.writeIntLittle(u32, hdr_data[index..][0..4], std.coff.IMAGE_SCN_CNT_INITIALIZED_DATA | std.coff.IMAGE_SCN_MEM_READ);
+ mem.writeIntLittle(u32, hdr_data[index..][0..4], @bitCast(u32, std.coff.SectionHeaderFlags{
+ .CNT_INITIALIZED_DATA = 1,
+ .MEM_READ = 1,
+ }));
index += 4;
// Then, the .text section
hdr_data[index..][0..8].* = ".text\x00\x00\x00".*;
@@ -378,11 +384,12 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option
mem.set(u8, hdr_data[index..][0..12], 0);
index += 12;
// Section flags
- mem.writeIntLittle(
- u32,
- hdr_data[index..][0..4],
- std.coff.IMAGE_SCN_CNT_CODE | std.coff.IMAGE_SCN_MEM_EXECUTE | std.coff.IMAGE_SCN_MEM_READ | std.coff.IMAGE_SCN_MEM_WRITE,
- );
+ mem.writeIntLittle(u32, hdr_data[index..][0..4], @bitCast(u32, std.coff.SectionHeaderFlags{
+ .CNT_CODE = 1,
+ .MEM_EXECUTE = 1,
+ .MEM_READ = 1,
+ .MEM_WRITE = 1,
+ }));
index += 4;
assert(index == optional_header_size + section_table_size);