authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-08-28 08:31:13+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-08-30 10:42:21+02:00
logda00e6dd596685e8f0ad9650145309c00485da6f
tree5a01b11c48f7ec44052570f63e693fc8bf2f7494
parent9fc6933418be00df54db79aa8eb3e2c759a2c038

coff: always write all data directory headers to file

Maximum number is always 16, and this also unbreaks `dumpbin.exe` run on a simple section-less PE image created with our linker.

1 files changed, 10 insertions(+), 30 deletions(-)

src/link/Coff.zig+10-30
...@@ -38,7 +38,7 @@ error_flags: link.File.ErrorFlags = .{},...@@ -38,7 +38,7 @@ error_flags: link.File.ErrorFlags = .{},
38ptr_width: PtrWidth,38ptr_width: PtrWidth,
3939
40sections: std.MultiArrayList(Section) = .{},40sections: std.MultiArrayList(Section) = .{},
41data_directories: std.ArrayListUnmanaged(coff.ImageDataDirectory) = .{},41data_directories: [16]coff.ImageDataDirectory,
4242
43text_section_index: ?u16 = null,43text_section_index: ?u16 = null,
44got_section_index: ?u16 = null,44got_section_index: ?u16 = null,
...@@ -72,7 +72,6 @@ const default_section_alignment: u16 = 0x1000;...@@ -72,7 +72,6 @@ const default_section_alignment: u16 = 0x1000;
72const default_file_alignment: u16 = 0x200;72const default_file_alignment: u16 = 0x200;
73const default_image_base_dll: u64 = 0x10000000;73const default_image_base_dll: u64 = 0x10000000;
74const default_image_base_exe: u64 = 0x10000;74const default_image_base_exe: u64 = 0x10000;
75const default_header_size: u64 = default_section_alignment;
7675
77const Section = struct {76const Section = struct {
78 header: coff.SectionHeader,77 header: coff.SectionHeader,
...@@ -171,6 +170,7 @@ pub fn createEmpty(gpa: Allocator, options: link.Options) !*Coff {...@@ -171,6 +170,7 @@ pub fn createEmpty(gpa: Allocator, options: link.Options) !*Coff {
171 .file = null,170 .file = null,
172 },171 },
173 .ptr_width = ptr_width,172 .ptr_width = ptr_width,
173 .data_directories = comptime mem.zeroes([16]coff.ImageDataDirectory),
174 };174 };
175175
176 const use_llvm = build_options.have_llvm and options.use_llvm;176 const use_llvm = build_options.have_llvm and options.use_llvm;
...@@ -192,7 +192,6 @@ pub fn deinit(self: *Coff) void {...@@ -192,7 +192,6 @@ pub fn deinit(self: *Coff) void {
192 free_list.deinit(gpa);192 free_list.deinit(gpa);
193 }193 }
194 self.sections.deinit(gpa);194 self.sections.deinit(gpa);
195 self.data_directories.deinit(gpa);
196195
197 for (self.managed_atoms.items) |atom| {196 for (self.managed_atoms.items) |atom| {
198 gpa.destroy(atom);197 gpa.destroy(atom);
...@@ -699,7 +698,7 @@ fn writeSectionHeaders(self: *Coff) !void {...@@ -699,7 +698,7 @@ fn writeSectionHeaders(self: *Coff) !void {
699698
700fn writeDataDirectoriesHeaders(self: *Coff) !void {699fn writeDataDirectoriesHeaders(self: *Coff) !void {
701 const offset = self.getDataDirectoryHeadersOffset();700 const offset = self.getDataDirectoryHeadersOffset();
702 try self.base.file.?.pwriteAll(mem.sliceAsBytes(self.data_directories.items), offset);701 try self.base.file.?.pwriteAll(mem.sliceAsBytes(&self.data_directories), offset);
703}702}
704703
705fn writeHeader(self: *Coff) !void {704fn writeHeader(self: *Coff) !void {
...@@ -790,7 +789,7 @@ fn writeHeader(self: *Coff) !void {...@@ -790,7 +789,7 @@ fn writeHeader(self: *Coff) !void {
790 .size_of_heap_reserve = 0,789 .size_of_heap_reserve = 0,
791 .size_of_heap_commit = 0,790 .size_of_heap_commit = 0,
792 .loader_flags = 0,791 .loader_flags = 0,
793 .number_of_rva_and_sizes = @intCast(u32, self.data_directories.items.len),792 .number_of_rva_and_sizes = @intCast(u32, self.data_directories.len),
794 };793 };
795 writer.writeAll(mem.asBytes(&opt_header)) catch unreachable;794 writer.writeAll(mem.asBytes(&opt_header)) catch unreachable;
796 },795 },
...@@ -824,7 +823,7 @@ fn writeHeader(self: *Coff) !void {...@@ -824,7 +823,7 @@ fn writeHeader(self: *Coff) !void {
824 .size_of_heap_reserve = 0,823 .size_of_heap_reserve = 0,
825 .size_of_heap_commit = 0,824 .size_of_heap_commit = 0,
826 .loader_flags = 0,825 .loader_flags = 0,
827 .number_of_rva_and_sizes = @intCast(u32, self.data_directories.items.len),826 .number_of_rva_and_sizes = @intCast(u32, self.data_directories.len),
828 };827 };
829 writer.writeAll(mem.asBytes(&opt_header)) catch unreachable;828 writer.writeAll(mem.asBytes(&opt_header)) catch unreachable;
830 },829 },
...@@ -841,32 +840,13 @@ pub fn padToIdeal(actual_size: anytype) @TypeOf(actual_size) {...@@ -841,32 +840,13 @@ pub fn padToIdeal(actual_size: anytype) @TypeOf(actual_size) {
841}840}
842841
843// fn detectAllocCollision(self: *Coff, start: u64, size: u64) ?u64 {842// fn detectAllocCollision(self: *Coff, start: u64, size: u64) ?u64 {
844// if (start < default_header_size)843// const headers_size = self.getSizeOfHeaders();
845// return default_header_size;844// if (start < headers_size)
845// return headers_size;
846846
847// const end = start + padToIdeal(size);847// const end = start + padToIdeal(size);
848848
849// if (self.symtab_offset) |off| {849// for (self.sections.items(.header)) |header| {
850// const shdr_size: u64 = if (small_ptr) @sizeOf(elf.Elf32_Shdr) else @sizeOf(elf.Elf64_Shdr);
851// const tight_size = self.sections.items.len * shdr_size;
852// const increased_size = padToIdeal(tight_size);
853// const test_end = off + increased_size;
854// if (end > off and start < test_end) {
855// return test_end;
856// }
857// }
858
859// if (self.phdr_table_offset) |off| {
860// const phdr_size: u64 = if (small_ptr) @sizeOf(elf.Elf32_Phdr) else @sizeOf(elf.Elf64_Phdr);
861// const tight_size = self.sections.items.len * phdr_size;
862// const increased_size = padToIdeal(tight_size);
863// const test_end = off + increased_size;
864// if (end > off and start < test_end) {
865// return test_end;
866// }
867// }
868
869// for (self.sections.items) |section| {
870// const increased_size = padToIdeal(section.sh_size);850// const increased_size = padToIdeal(section.sh_size);
871// const test_end = section.sh_offset + increased_size;851// const test_end = section.sh_offset + increased_size;
872// if (end > section.sh_offset and start < test_end) {852// if (end > section.sh_offset and start < test_end) {
...@@ -926,7 +906,7 @@ inline fn getOptionalHeaderSize(self: Coff) usize {...@@ -926,7 +906,7 @@ inline fn getOptionalHeaderSize(self: Coff) usize {
926}906}
927907
928inline fn getDataDirectoryHeadersSize(self: Coff) usize {908inline fn getDataDirectoryHeadersSize(self: Coff) usize {
929 return self.data_directories.items.len * @sizeOf(coff.ImageDataDirectory);909 return self.data_directories.len * @sizeOf(coff.ImageDataDirectory);
930}910}
931911
932inline fn getSectionHeadersSize(self: Coff) usize {912inline fn getSectionHeadersSize(self: Coff) usize {