authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-08-29 13:21:32+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-08-30 10:42:21+02:00
logb4e3b87a526378c33d4158ccc622a5183eadfb3f
tree81bcebb90af5dda60c0865c5c461c8eaa6e1ba93
parent30baba899cd20c6bc2224f3713f58ba40bbd8709

coff: ...and lift-off!


1 files changed, 16 insertions(+), 18 deletions(-)

src/link/Coff.zig+16-18
......@@ -30,6 +30,7 @@ const TypedValue = @import("../TypedValue.zig");
3030pub const base_tag: link.File.Tag = .coff;
3131
3232const msdos_stub = @embedFile("msdos-stub.bin");
33const N_DATA_DIRS: u5 = 16;
3334
3435/// If this is not null, an object file is created by LLVM and linked with LLD afterwards.
3536llvm_object: ?*LlvmObject = null,
......@@ -43,7 +44,7 @@ page_size: u32,
4344objects: std.ArrayListUnmanaged(Object) = .{},
4445
4546sections: std.MultiArrayList(Section) = .{},
46data_directories: [16]coff.ImageDataDirectory,
47data_directories: [N_DATA_DIRS]coff.ImageDataDirectory,
4748
4849text_section_index: ?u16 = null,
4950got_section_index: ?u16 = null,
......@@ -114,7 +115,7 @@ const UnnamedConstTable = std.AutoHashMapUnmanaged(Module.Decl.Index, std.ArrayL
114115
115116const default_file_alignment: u16 = 0x200;
116117const default_image_base_dll: u64 = 0x10000000;
117const default_image_base_exe: u64 = 0x10000;
118const default_image_base_exe: u64 = 0x400000;
118119const default_size_of_stack_reserve: u32 = 0x1000000;
119120const default_size_of_stack_commit: u32 = 0x1000;
120121const default_size_of_heap_reserve: u32 = 0x100000;
......@@ -210,7 +211,7 @@ pub fn createEmpty(gpa: Allocator, options: link.Options) !*Coff {
210211 },
211212 .ptr_width = ptr_width,
212213 .page_size = page_size,
213 .data_directories = comptime mem.zeroes([16]coff.ImageDataDirectory),
214 .data_directories = comptime mem.zeroes([N_DATA_DIRS]coff.ImageDataDirectory),
214215 };
215216
216217 const use_llvm = build_options.have_llvm and options.use_llvm;
......@@ -487,8 +488,8 @@ fn allocateAtom(self: *Coff, atom: *Atom, new_atom_size: u32, alignment: u32, se
487488 @panic("TODO move section");
488489 }
489490 maybe_last_atom.* = atom;
490 header.virtual_size = needed_size;
491 header.size_of_raw_data = mem.alignForwardGeneric(u32, needed_size, default_file_alignment);
491 // header.virtual_size = needed_size;
492 // header.size_of_raw_data = mem.alignForwardGeneric(u32, needed_size, default_file_alignment);
492493 }
493494
494495 // if (header.getAlignment().? < alignment) {
......@@ -1196,10 +1197,9 @@ fn writeHeader(self: *Coff) !void {
11961197
11971198 try buffer.ensureTotalCapacity(self.getSizeOfHeaders());
11981199 writer.writeAll(msdos_stub) catch unreachable;
1199 writer.writeByteNTimes(0, 4) catch unreachable; // align to 8 bytes
1200 writer.writeAll("PE\x00\x00") catch unreachable;
1201 mem.writeIntLittle(u32, buffer.items[0x3c..][0..4], msdos_stub.len + 4);
1200 mem.writeIntLittle(u32, buffer.items[0x3c..][0..4], msdos_stub.len);
12021201
1202 writer.writeAll("PE\x00\x00") catch unreachable;
12031203 var flags = coff.CoffHeaderFlags{
12041204 .EXECUTABLE_IMAGE = 1,
12051205 .DEBUG_STRIPPED = 1, // TODO
......@@ -1345,10 +1345,10 @@ fn detectAllocCollision(self: *Coff, start: u32, size: u32) ?u32 {
13451345 if (start < headers_size)
13461346 return headers_size;
13471347
1348 const end = start + padToIdeal(size);
1348 const end = start + size;
13491349
13501350 if (self.strtab_offset) |off| {
1351 const increased_size = padToIdeal(@intCast(u32, self.strtab.len()));
1351 const increased_size = @intCast(u32, self.strtab.len());
13521352 const test_end = off + increased_size;
13531353 if (end > off and start < test_end) {
13541354 return test_end;
......@@ -1356,7 +1356,7 @@ fn detectAllocCollision(self: *Coff, start: u32, size: u32) ?u32 {
13561356 }
13571357
13581358 for (self.sections.items(.header)) |header| {
1359 const increased_size = padToIdeal(header.size_of_raw_data);
1359 const increased_size = header.size_of_raw_data;
13601360 const test_end = header.pointer_to_raw_data + increased_size;
13611361 if (end > header.pointer_to_raw_data and start < test_end) {
13621362 return test_end;
......@@ -1389,7 +1389,7 @@ pub fn findFreeSpace(self: *Coff, object_size: u32, min_alignment: u32) u32 {
13891389}
13901390
13911391inline fn getSizeOfHeaders(self: Coff) u32 {
1392 const msdos_hdr_size = msdos_stub.len + 8;
1392 const msdos_hdr_size = msdos_stub.len + 4;
13931393 return @intCast(u32, msdos_hdr_size + @sizeOf(coff.CoffHeader) + self.getOptionalHeaderSize() +
13941394 self.getDataDirectoryHeadersSize() + self.getSectionHeadersSize());
13951395}
......@@ -1410,7 +1410,7 @@ inline fn getSectionHeadersSize(self: Coff) u32 {
14101410}
14111411
14121412inline fn getDataDirectoryHeadersOffset(self: Coff) u32 {
1413 const msdos_hdr_size = msdos_stub.len + 8;
1413 const msdos_hdr_size = msdos_stub.len + 4;
14141414 return @intCast(u32, msdos_hdr_size + @sizeOf(coff.CoffHeader) + self.getOptionalHeaderSize());
14151415}
14161416
......@@ -1419,13 +1419,11 @@ inline fn getSectionHeadersOffset(self: Coff) u32 {
14191419}
14201420
14211421inline fn getSizeOfImage(self: Coff) u32 {
1422 var max_image_size: u32 = 0;
1422 var image_size: u32 = mem.alignForwardGeneric(u32, self.getSizeOfHeaders(), self.page_size);
14231423 for (self.sections.items(.header)) |header| {
1424 if (header.virtual_address + header.virtual_size > max_image_size) {
1425 max_image_size = header.virtual_address + header.virtual_size;
1426 }
1424 image_size += mem.alignForwardGeneric(u32, header.virtual_size, self.page_size);
14271425 }
1428 return mem.alignForwardGeneric(u32, @maximum(max_image_size, self.getSizeOfHeaders()), self.page_size);
1426 return image_size;
14291427}
14301428
14311429/// Returns symbol location corresponding to the set entrypoint (if any).