authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-08-27 15:05:43+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-08-30 10:42:21+02:00
log93127a615be716efa0a74ca08e0a17fda2f4074f
tree7574f299f7fbb2ad9c49f95c28a76f35b373b2f6
parented481e3837218659ab1c18d9d4b836cf656307b9

coff: set some defaults for PE headers


2 files changed, 49 insertions(+), 23 deletions(-)

src/Compilation.zig-1
......@@ -1127,7 +1127,6 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
11271127 link_eh_frame_hdr or
11281128 options.link_emit_relocs or
11291129 options.output_mode == .Lib or
1130 options.image_base_override != null or
11311130 options.linker_script != null or options.version_script != null or
11321131 options.emit_implib != null or
11331132 build_id)
src/link/Coff.zig+49-22
......@@ -69,7 +69,10 @@ managed_atoms: std.ArrayListUnmanaged(*Atom) = .{},
6969/// Table of atoms indexed by the symbol index.
7070atom_by_index_table: std.AutoHashMapUnmanaged(u32, *Atom) = .{},
7171
72const page_size: u16 = 0x1000;
72const default_section_alignment: u16 = 0x1000;
73const default_file_alignment: u16 = 0x200;
74const default_image_base_dll: u64 = 0x10000000;
75const default_image_base_exe: u64 = 0x10000;
7376
7477const Section = struct {
7578 header: coff.SectionHeader,
......@@ -701,7 +704,7 @@ fn writeHeader(self: *Coff) !void {
701704 writer.writeAll("PE\x00\x00") catch unreachable;
702705 buffer.items[0x3c] = @intCast(u8, msdos_stub.len + 4);
703706
704 var num_data_directories: u32 = 1;
707 var num_data_directories: u32 = 0;
705708 var size_of_optional_header = switch (self.ptr_width) {
706709 .p32 => @intCast(u32, @sizeOf(coff.OptionalHeaderPE32)),
707710 .p64 => @intCast(u32, @sizeOf(coff.OptionalHeaderPE64)),
......@@ -732,7 +735,25 @@ fn writeHeader(self: *Coff) !void {
732735 try buffer.ensureUnusedCapacity(@sizeOf(coff.CoffHeader));
733736 writer.writeAll(mem.asBytes(&coff_header)) catch unreachable;
734737
738 const dll_flags: coff.DllFlags = .{
739 .HIGH_ENTROPY_VA = 0, // TODO handle ASLR
740 .DYNAMIC_BASE = 0, // TODO handle ASLR
741 .TERMINAL_SERVER_AWARE = 1, // We are not a legacy app
742 .NX_COMPAT = 1, // We are compatible with Data Execution Prevention
743 };
735744 const subsystem: coff.Subsystem = .WINDOWS_CUI;
745 const size_of_headers: u32 = @intCast(
746 u32,
747 msdos_stub.len + 8 + size_of_optional_header + self.sections.slice().len * @sizeOf(coff.SectionHeader),
748 );
749 const size_of_image_aligned: u32 = mem.alignForwardGeneric(u32, size_of_headers, default_section_alignment);
750 const size_of_headers_aligned: u32 = mem.alignForwardGeneric(u32, size_of_headers, default_file_alignment);
751 const image_base = self.base.options.image_base_override orelse switch (self.base.options.output_mode) {
752 .Exe => default_image_base_exe,
753 .Lib => default_image_base_dll,
754 else => unreachable,
755 };
756
736757 switch (self.ptr_width) {
737758 .p32 => {
738759 try buffer.ensureUnusedCapacity(@sizeOf(coff.OptionalHeaderPE32));
......@@ -746,21 +767,21 @@ fn writeHeader(self: *Coff) !void {
746767 .address_of_entry_point = self.entry_addr orelse 0,
747768 .base_of_code = 0,
748769 .base_of_data = 0,
749 .image_base = 0,
750 .section_alignment = 0,
751 .file_alignment = 0,
752 .major_operating_system_version = 0,
770 .image_base = @intCast(u32, image_base),
771 .section_alignment = default_section_alignment,
772 .file_alignment = default_file_alignment,
773 .major_operating_system_version = 6,
753774 .minor_operating_system_version = 0,
754775 .major_image_version = 0,
755776 .minor_image_version = 0,
756 .major_subsystem_version = 0,
777 .major_subsystem_version = 6,
757778 .minor_subsystem_version = 0,
758779 .win32_version_value = 0,
759 .size_of_image = 0,
760 .size_of_headers = 0,
780 .size_of_image = size_of_image_aligned,
781 .size_of_headers = size_of_headers_aligned,
761782 .checksum = 0,
762783 .subsystem = subsystem,
763 .dll_flags = .{},
784 .dll_flags = dll_flags,
764785 .size_of_stack_reserve = 0,
765786 .size_of_stack_commit = 0,
766787 .size_of_heap_reserve = 0,
......@@ -781,21 +802,21 @@ fn writeHeader(self: *Coff) !void {
781802 .size_of_uninitialized_data = 0,
782803 .address_of_entry_point = self.entry_addr orelse 0,
783804 .base_of_code = 0,
784 .image_base = 0,
785 .section_alignment = 0,
786 .file_alignment = 0,
787 .major_operating_system_version = 0,
805 .image_base = image_base,
806 .section_alignment = default_section_alignment,
807 .file_alignment = default_file_alignment,
808 .major_operating_system_version = 6,
788809 .minor_operating_system_version = 0,
789810 .major_image_version = 0,
790811 .minor_image_version = 0,
791 .major_subsystem_version = 0,
812 .major_subsystem_version = 6,
792813 .minor_subsystem_version = 0,
793814 .win32_version_value = 0,
794 .size_of_image = 0,
795 .size_of_headers = 0,
815 .size_of_image = size_of_image_aligned,
816 .size_of_headers = size_of_headers_aligned,
796817 .checksum = 0,
797818 .subsystem = subsystem,
798 .dll_flags = .{},
819 .dll_flags = dll_flags,
799820 .size_of_stack_reserve = 0,
800821 .size_of_stack_commit = 0,
801822 .size_of_heap_reserve = 0,
......@@ -808,11 +829,17 @@ fn writeHeader(self: *Coff) !void {
808829 }
809830
810831 try buffer.ensureUnusedCapacity(num_data_directories * @sizeOf(coff.ImageDataDirectory));
811 writer.writeAll(mem.asBytes(&coff.ImageDataDirectory{
812 .virtual_address = 0,
813 .size = 0,
814 })) catch unreachable;
832 {
833 var i: usize = 0;
834 while (i < num_data_directories) : (i += 1) {
835 writer.writeAll(mem.asBytes(&coff.ImageDataDirectory{
836 .virtual_address = 0,
837 .size = 0,
838 })) catch unreachable;
839 }
840 }
815841
842 try self.base.file.?.pwriteAll(&[_]u8{0}, size_of_headers_aligned);
816843 try self.base.file.?.pwriteAll(buffer.items, 0);
817844}
818845