authorgravatar for alex_naskos@hotmail.comAlexandros Naskos <alex_naskos@hotmail.com> 2020-09-03 18:24:42+03:00
committergravatar for alex_naskos@hotmail.comAlexandros Naskos <alex_naskos@hotmail.com> 2020-09-04 05:15:03+03:00
loge9b137f23a9b38c6b808452ef216e4e18d3070ed
tree164222ecba97648c89a212588aaeebaa25fd38d8
parentfe0ad8d6e99746850a97266582c0437a8b51f524

Completed basic PE linker for stage2

Added std.coff.MachineType Added image characteristic and section flag valued to std.coff Added std.Target.Cpu.Arch.toCoffMachine Fixed stage2 --watch flag on windows

8 files changed, 855 insertions(+), 200 deletions(-)

lib/std/coff.zig+66
......@@ -18,11 +18,77 @@ const IMAGE_FILE_MACHINE_I386 = 0x014c;
1818const IMAGE_FILE_MACHINE_IA64 = 0x0200;
1919const IMAGE_FILE_MACHINE_AMD64 = 0x8664;
2020
21pub const MachineType = enum(u16) {
22 Unknown = 0x0,
23 /// Matsushita AM33
24 AM33 = 0x1d3,
25 /// x64
26 X64 = 0x8664,
27 /// ARM little endian
28 ARM = 0x1c0,
29 /// ARM64 little endian
30 ARM64 = 0xaa64,
31 /// ARM Thumb-2 little endian
32 ARMNT = 0x1c4,
33 /// EFI byte code
34 EBC = 0xebc,
35 /// Intel 386 or later processors and compatible processors
36 I386 = 0x14c,
37 /// Intel Itanium processor family
38 IA64 = 0x200,
39 /// Mitsubishi M32R little endian
40 M32R = 0x9041,
41 /// MIPS16
42 MIPS16 = 0x266,
43 /// MIPS with FPU
44 MIPSFPU = 0x366,
45 /// MIPS16 with FPU
46 MIPSFPU16 = 0x466,
47 /// Power PC little endian
48 POWERPC = 0x1f0,
49 /// Power PC with floating point support
50 POWERPCFP = 0x1f1,
51 /// MIPS little endian
52 R4000 = 0x166,
53 /// RISC-V 32-bit address space
54 RISCV32 = 0x5032,
55 /// RISC-V 64-bit address space
56 RISCV64 = 0x5064,
57 /// RISC-V 128-bit address space
58 RISCV128 = 0x5128,
59 /// Hitachi SH3
60 SH3 = 0x1a2,
61 /// Hitachi SH3 DSP
62 SH3DSP = 0x1a3,
63 /// Hitachi SH4
64 SH4 = 0x1a6,
65 /// Hitachi SH5
66 SH5 = 0x1a8,
67 /// Thumb
68 Thumb = 0x1c2,
69 /// MIPS little-endian WCE v2
70 WCEMIPSV2 = 0x169,
71};
72
2173// OptionalHeader.magic values
2274// see https://msdn.microsoft.com/en-us/library/windows/desktop/ms680339(v=vs.85).aspx
2375const IMAGE_NT_OPTIONAL_HDR32_MAGIC = 0x10b;
2476const IMAGE_NT_OPTIONAL_HDR64_MAGIC = 0x20b;
2577
78// Image Characteristics
79pub const IMAGE_FILE_RELOCS_STRIPPED = 0x1;
80pub const IMAGE_FILE_DEBUG_STRIPPED = 0x200;
81pub const IMAGE_FILE_EXECUTABLE_IMAGE = 0x2;
82pub const IMAGE_FILE_32BIT_MACHINE = 0x100;
83pub const IMAGE_FILE_LARGE_ADDRESS_AWARE = 0x20;
84
85// Section flags
86pub const IMAGE_SCN_CNT_INITIALIZED_DATA = 0x40;
87pub const IMAGE_SCN_MEM_READ = 0x40000000;
88pub const IMAGE_SCN_CNT_CODE = 0x20;
89pub const IMAGE_SCN_MEM_EXECUTE = 0x20000000;
90pub const IMAGE_SCN_MEM_WRITE = 0x80000000;
91
2692const IMAGE_NUMBEROF_DIRECTORY_ENTRIES = 16;
2793const IMAGE_DEBUG_TYPE_CODEVIEW = 2;
2894const DEBUG_DIRECTORY = 6;
lib/std/target.zig+57
......@@ -771,6 +771,63 @@ pub const Target = struct {
771771 };
772772 }
773773
774 pub fn toCoffMachine(arch: Arch) std.coff.MachineType {
775 return switch (arch) {
776 .avr => .Unknown,
777 .msp430 => .Unknown,
778 .arc => .Unknown,
779 .arm => .ARM,
780 .armeb => .Unknown,
781 .hexagon => .Unknown,
782 .le32 => .Unknown,
783 .mips => .Unknown,
784 .mipsel => .Unknown,
785 .powerpc => .POWERPC,
786 .r600 => .Unknown,
787 .riscv32 => .RISCV32,
788 .sparc => .Unknown,
789 .sparcel => .Unknown,
790 .tce => .Unknown,
791 .tcele => .Unknown,
792 .thumb => .Thumb,
793 .thumbeb => .Thumb,
794 .i386 => .I386,
795 .xcore => .Unknown,
796 .nvptx => .Unknown,
797 .amdil => .Unknown,
798 .hsail => .Unknown,
799 .spir => .Unknown,
800 .kalimba => .Unknown,
801 .shave => .Unknown,
802 .lanai => .Unknown,
803 .wasm32 => .Unknown,
804 .renderscript32 => .Unknown,
805 .aarch64_32 => .ARM64,
806 .aarch64 => .ARM64,
807 .aarch64_be => .Unknown,
808 .mips64 => .Unknown,
809 .mips64el => .Unknown,
810 .powerpc64 => .Unknown,
811 .powerpc64le => .Unknown,
812 .riscv64 => .RISCV64,
813 .x86_64 => .X64,
814 .nvptx64 => .Unknown,
815 .le64 => .Unknown,
816 .amdil64 => .Unknown,
817 .hsail64 => .Unknown,
818 .spir64 => .Unknown,
819 .wasm64 => .Unknown,
820 .renderscript64 => .Unknown,
821 .amdgcn => .Unknown,
822 .bpfel => .Unknown,
823 .bpfeb => .Unknown,
824 .sparcv9 => .Unknown,
825 .s390x => .Unknown,
826 .ve => .Unknown,
827 .spu_2 => .Unknown,
828 };
829 }
830
774831 pub fn endian(arch: Arch) builtin.Endian {
775832 return switch (arch) {
776833 .avr,
src-self-hosted/Module.zig+2-2
......@@ -2081,14 +2081,14 @@ fn allocateNewDecl(
20812081 .deletion_flag = false,
20822082 .contents_hash = contents_hash,
20832083 .link = switch (self.bin_file.tag) {
2084 .coff => .{ .coff = {} }, // @TODO
2084 .coff => .{ .coff = link.File.Coff.TextBlock.empty },
20852085 .elf => .{ .elf = link.File.Elf.TextBlock.empty },
20862086 .macho => .{ .macho = link.File.MachO.TextBlock.empty },
20872087 .c => .{ .c = {} },
20882088 .wasm => .{ .wasm = {} },
20892089 },
20902090 .fn_link = switch (self.bin_file.tag) {
2091 .coff => .{ .coff = {} }, // @TODO
2091 .coff => .{ .coff = {} },
20922092 .elf => .{ .elf = link.File.Elf.SrcFn.empty },
20932093 .macho => .{ .macho = link.File.MachO.SrcFn.empty },
20942094 .c => .{ .c = {} },
src-self-hosted/codegen.zig+166-112
......@@ -59,14 +59,21 @@ pub const GenerateSymbolError = error{
5959 AnalysisFail,
6060};
6161
62pub const DebugInfoOutput = union(enum) {
63 dwarf: struct {
64 dbg_line: *std.ArrayList(u8),
65 dbg_info: *std.ArrayList(u8),
66 dbg_info_type_relocs: *link.File.DbgInfoTypeRelocsTable,
67 },
68 none,
69};
70
6271pub fn generateSymbol(
6372 bin_file: *link.File,
6473 src: usize,
6574 typed_value: TypedValue,
6675 code: *std.ArrayList(u8),
67 dbg_line: *std.ArrayList(u8),
68 dbg_info: *std.ArrayList(u8),
69 dbg_info_type_relocs: *link.File.DbgInfoTypeRelocsTable,
76 debug_output: DebugInfoOutput,
7077) GenerateSymbolError!Result {
7178 const tracy = trace(@src());
7279 defer tracy.end();
......@@ -76,56 +83,56 @@ pub fn generateSymbol(
7683 switch (bin_file.options.target.cpu.arch) {
7784 .wasm32 => unreachable, // has its own code path
7885 .wasm64 => unreachable, // has its own code path
79 .arm => return Function(.arm).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
80 .armeb => return Function(.armeb).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
81 //.aarch64 => return Function(.aarch64).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
82 //.aarch64_be => return Function(.aarch64_be).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
83 //.aarch64_32 => return Function(.aarch64_32).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
84 //.arc => return Function(.arc).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
85 //.avr => return Function(.avr).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
86 //.bpfel => return Function(.bpfel).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
87 //.bpfeb => return Function(.bpfeb).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
88 //.hexagon => return Function(.hexagon).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
89 //.mips => return Function(.mips).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
90 //.mipsel => return Function(.mipsel).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
91 //.mips64 => return Function(.mips64).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
92 //.mips64el => return Function(.mips64el).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
93 //.msp430 => return Function(.msp430).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
94 //.powerpc => return Function(.powerpc).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
95 //.powerpc64 => return Function(.powerpc64).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
96 //.powerpc64le => return Function(.powerpc64le).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
97 //.r600 => return Function(.r600).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
98 //.amdgcn => return Function(.amdgcn).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
99 //.riscv32 => return Function(.riscv32).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
100 .riscv64 => return Function(.riscv64).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
101 //.sparc => return Function(.sparc).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
102 //.sparcv9 => return Function(.sparcv9).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
103 //.sparcel => return Function(.sparcel).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
104 //.s390x => return Function(.s390x).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
105 .spu_2 => return Function(.spu_2).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
106 //.tce => return Function(.tce).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
107 //.tcele => return Function(.tcele).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
108 //.thumb => return Function(.thumb).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
109 //.thumbeb => return Function(.thumbeb).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
110 //.i386 => return Function(.i386).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
111 .x86_64 => return Function(.x86_64).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
112 //.xcore => return Function(.xcore).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
113 //.nvptx => return Function(.nvptx).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
114 //.nvptx64 => return Function(.nvptx64).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
115 //.le32 => return Function(.le32).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
116 //.le64 => return Function(.le64).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
117 //.amdil => return Function(.amdil).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
118 //.amdil64 => return Function(.amdil64).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
119 //.hsail => return Function(.hsail).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
120 //.hsail64 => return Function(.hsail64).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
121 //.spir => return Function(.spir).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
122 //.spir64 => return Function(.spir64).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
123 //.kalimba => return Function(.kalimba).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
124 //.shave => return Function(.shave).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
125 //.lanai => return Function(.lanai).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
126 //.renderscript32 => return Function(.renderscript32).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
127 //.renderscript64 => return Function(.renderscript64).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
128 //.ve => return Function(.ve).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),
86 .arm => return Function(.arm).generateSymbol(bin_file, src, typed_value, code, debug_output),
87 .armeb => return Function(.armeb).generateSymbol(bin_file, src, typed_value, code, debug_output),
88 //.aarch64 => return Function(.aarch64).generateSymbol(bin_file, src, typed_value, code, debug_output),
89 //.aarch64_be => return Function(.aarch64_be).generateSymbol(bin_file, src, typed_value, code, debug_output),
90 //.aarch64_32 => return Function(.aarch64_32).generateSymbol(bin_file, src, typed_value, code, debug_output),
91 //.arc => return Function(.arc).generateSymbol(bin_file, src, typed_value, code, debug_output),
92 //.avr => return Function(.avr).generateSymbol(bin_file, src, typed_value, code, debug_output),
93 //.bpfel => return Function(.bpfel).generateSymbol(bin_file, src, typed_value, code, debug_output),
94 //.bpfeb => return Function(.bpfeb).generateSymbol(bin_file, src, typed_value, code, debug_output),
95 //.hexagon => return Function(.hexagon).generateSymbol(bin_file, src, typed_value, code, debug_output),
96 //.mips => return Function(.mips).generateSymbol(bin_file, src, typed_value, code, debug_output),
97 //.mipsel => return Function(.mipsel).generateSymbol(bin_file, src, typed_value, code, debug_output),
98 //.mips64 => return Function(.mips64).generateSymbol(bin_file, src, typed_value, code, debug_output),
99 //.mips64el => return Function(.mips64el).generateSymbol(bin_file, src, typed_value, code, debug_output),
100 //.msp430 => return Function(.msp430).generateSymbol(bin_file, src, typed_value, code, debug_output),
101 //.powerpc => return Function(.powerpc).generateSymbol(bin_file, src, typed_value, code, debug_output),
102 //.powerpc64 => return Function(.powerpc64).generateSymbol(bin_file, src, typed_value, code, debug_output),
103 //.powerpc64le => return Function(.powerpc64le).generateSymbol(bin_file, src, typed_value, code, debug_output),
104 //.r600 => return Function(.r600).generateSymbol(bin_file, src, typed_value, code, debug_output),
105 //.amdgcn => return Function(.amdgcn).generateSymbol(bin_file, src, typed_value, code, debug_output),
106 //.riscv32 => return Function(.riscv32).generateSymbol(bin_file, src, typed_value, code, debug_output),
107 .riscv64 => return Function(.riscv64).generateSymbol(bin_file, src, typed_value, code, debug_output),
108 //.sparc => return Function(.sparc).generateSymbol(bin_file, src, typed_value, code, debug_output),
109 //.sparcv9 => return Function(.sparcv9).generateSymbol(bin_file, src, typed_value, code, debug_output),
110 //.sparcel => return Function(.sparcel).generateSymbol(bin_file, src, typed_value, code, debug_output),
111 //.s390x => return Function(.s390x).generateSymbol(bin_file, src, typed_value, code, debug_output),
112 .spu_2 => return Function(.spu_2).generateSymbol(bin_file, src, typed_value, code, debug_output),
113 //.tce => return Function(.tce).generateSymbol(bin_file, src, typed_value, code, debug_output),
114 //.tcele => return Function(.tcele).generateSymbol(bin_file, src, typed_value, code, debug_output),
115 //.thumb => return Function(.thumb).generateSymbol(bin_file, src, typed_value, code, debug_output),
116 //.thumbeb => return Function(.thumbeb).generateSymbol(bin_file, src, typed_value, code, debug_output),
117 //.i386 => return Function(.i386).generateSymbol(bin_file, src, typed_value, code, debug_output),
118 .x86_64 => return Function(.x86_64).generateSymbol(bin_file, src, typed_value, code, debug_output),
119 //.xcore => return Function(.xcore).generateSymbol(bin_file, src, typed_value, code, debug_output),
120 //.nvptx => return Function(.nvptx).generateSymbol(bin_file, src, typed_value, code, debug_output),
121 //.nvptx64 => return Function(.nvptx64).generateSymbol(bin_file, src, typed_value, code, debug_output),
122 //.le32 => return Function(.le32).generateSymbol(bin_file, src, typed_value, code, debug_output),
123 //.le64 => return Function(.le64).generateSymbol(bin_file, src, typed_value, code, debug_output),
124 //.amdil => return Function(.amdil).generateSymbol(bin_file, src, typed_value, code, debug_output),
125 //.amdil64 => return Function(.amdil64).generateSymbol(bin_file, src, typed_value, code, debug_output),
126 //.hsail => return Function(.hsail).generateSymbol(bin_file, src, typed_value, code, debug_output),
127 //.hsail64 => return Function(.hsail64).generateSymbol(bin_file, src, typed_value, code, debug_output),
128 //.spir => return Function(.spir).generateSymbol(bin_file, src, typed_value, code, debug_output),
129 //.spir64 => return Function(.spir64).generateSymbol(bin_file, src, typed_value, code, debug_output),
130 //.kalimba => return Function(.kalimba).generateSymbol(bin_file, src, typed_value, code, debug_output),
131 //.shave => return Function(.shave).generateSymbol(bin_file, src, typed_value, code, debug_output),
132 //.lanai => return Function(.lanai).generateSymbol(bin_file, src, typed_value, code, debug_output),
133 //.renderscript32 => return Function(.renderscript32).generateSymbol(bin_file, src, typed_value, code, debug_output),
134 //.renderscript64 => return Function(.renderscript64).generateSymbol(bin_file, src, typed_value, code, debug_output),
135 //.ve => return Function(.ve).generateSymbol(bin_file, src, typed_value, code, debug_output),
129136 else => @panic("Backend architectures that don't have good support yet are commented out, to improve compilation performance. If you are interested in one of these other backends feel free to uncomment them. Eventually these will be completed, but stage1 is slow and a memory hog."),
130137 }
131138 },
......@@ -139,7 +146,7 @@ pub fn generateSymbol(
139146 switch (try generateSymbol(bin_file, src, .{
140147 .ty = typed_value.ty.elemType(),
141148 .val = sentinel,
142 }, code, dbg_line, dbg_info, dbg_info_type_relocs)) {
149 }, code, debug_output)) {
143150 .appended => return Result{ .appended = {} },
144151 .externally_managed => |slice| {
145152 code.appendSliceAssumeCapacity(slice);
......@@ -239,9 +246,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
239246 target: *const std.Target,
240247 mod_fn: *const Module.Fn,
241248 code: *std.ArrayList(u8),
242 dbg_line: *std.ArrayList(u8),
243 dbg_info: *std.ArrayList(u8),
244 dbg_info_type_relocs: *link.File.DbgInfoTypeRelocsTable,
249 debug_output: DebugInfoOutput,
245250 err_msg: ?*ErrorMsg,
246251 args: []MCValue,
247252 ret_mcv: MCValue,
......@@ -419,9 +424,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
419424 src: usize,
420425 typed_value: TypedValue,
421426 code: *std.ArrayList(u8),
422 dbg_line: *std.ArrayList(u8),
423 dbg_info: *std.ArrayList(u8),
424 dbg_info_type_relocs: *link.File.DbgInfoTypeRelocsTable,
427 debug_output: DebugInfoOutput,
425428 ) GenerateSymbolError!Result {
426429 const module_fn = typed_value.val.cast(Value.Payload.Function).?.func;
427430
......@@ -457,9 +460,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
457460 .bin_file = bin_file,
458461 .mod_fn = module_fn,
459462 .code = code,
460 .dbg_line = dbg_line,
461 .dbg_info = dbg_info,
462 .dbg_info_type_relocs = dbg_info_type_relocs,
463 .debug_output = debug_output,
463464 .err_msg = null,
464465 .args = undefined, // populated after `resolveCallingConventionValues`
465466 .ret_mcv = undefined, // populated after `resolveCallingConventionValues`
......@@ -598,35 +599,50 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
598599 }
599600
600601 fn dbgSetPrologueEnd(self: *Self) InnerError!void {
601 try self.dbg_line.append(DW.LNS_set_prologue_end);
602 try self.dbgAdvancePCAndLine(self.prev_di_src);
602 switch (self.debug_output) {
603 .dwarf => |dbg_out| {
604 try dbg_out.dbg_line.append(DW.LNS_set_prologue_end);
605 try self.dbgAdvancePCAndLine(self.prev_di_src);
606 },
607 .none => {},
608 }
603609 }
604610
605611 fn dbgSetEpilogueBegin(self: *Self) InnerError!void {
606 try self.dbg_line.append(DW.LNS_set_epilogue_begin);
607 try self.dbgAdvancePCAndLine(self.prev_di_src);
612 switch (self.debug_output) {
613 .dwarf => |dbg_out| {
614 try dbg_out.dbg_line.append(DW.LNS_set_epilogue_begin);
615 try self.dbgAdvancePCAndLine(self.prev_di_src);
616 },
617 .none => {},
618 }
608619 }
609620
610621 fn dbgAdvancePCAndLine(self: *Self, src: usize) InnerError!void {
611 // TODO Look into improving the performance here by adding a token-index-to-line
612 // lookup table, and changing ir.Inst from storing byte offset to token. Currently
613 // this involves scanning over the source code for newlines
614 // (but only from the previous byte offset to the new one).
615 const delta_line = std.zig.lineDelta(self.source, self.prev_di_src, src);
616 const delta_pc = self.code.items.len - self.prev_di_pc;
617622 self.prev_di_src = src;
618623 self.prev_di_pc = self.code.items.len;
619 // TODO Look into using the DWARF special opcodes to compress this data. It lets you emit
620 // single-byte opcodes that add different numbers to both the PC and the line number
621 // at the same time.
622 try self.dbg_line.ensureCapacity(self.dbg_line.items.len + 11);
623 self.dbg_line.appendAssumeCapacity(DW.LNS_advance_pc);
624 leb128.writeULEB128(self.dbg_line.writer(), delta_pc) catch unreachable;
625 if (delta_line != 0) {
626 self.dbg_line.appendAssumeCapacity(DW.LNS_advance_line);
627 leb128.writeILEB128(self.dbg_line.writer(), delta_line) catch unreachable;
624 switch (self.debug_output) {
625 .dwarf => |dbg_out| {
626 // TODO Look into improving the performance here by adding a token-index-to-line
627 // lookup table, and changing ir.Inst from storing byte offset to token. Currently
628 // this involves scanning over the source code for newlines
629 // (but only from the previous byte offset to the new one).
630 const delta_line = std.zig.lineDelta(self.source, self.prev_di_src, src);
631 const delta_pc = self.code.items.len - self.prev_di_pc;
632 // TODO Look into using the DWARF special opcodes to compress this data. It lets you emit
633 // single-byte opcodes that add different numbers to both the PC and the line number
634 // at the same time.
635 try dbg_out.dbg_line.ensureCapacity(dbg_out.dbg_line.items.len + 11);
636 dbg_out.dbg_line.appendAssumeCapacity(DW.LNS_advance_pc);
637 leb128.writeULEB128(dbg_out.dbg_line.writer(), delta_pc) catch unreachable;
638 if (delta_line != 0) {
639 dbg_out.dbg_line.appendAssumeCapacity(DW.LNS_advance_line);
640 leb128.writeILEB128(dbg_out.dbg_line.writer(), delta_line) catch unreachable;
641 }
642 dbg_out.dbg_line.appendAssumeCapacity(DW.LNS_copy);
643 },
644 .none => {},
628645 }
629 self.dbg_line.appendAssumeCapacity(DW.LNS_copy);
630646 }
631647
632648 /// Asserts there is already capacity to insert into top branch inst_table.
......@@ -654,18 +670,23 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
654670 /// Adds a Type to the .debug_info at the current position. The bytes will be populated later,
655671 /// after codegen for this symbol is done.
656672 fn addDbgInfoTypeReloc(self: *Self, ty: Type) !void {
657 assert(ty.hasCodeGenBits());
658 const index = self.dbg_info.items.len;
659 try self.dbg_info.resize(index + 4); // DW.AT_type, DW.FORM_ref4
660
661 const gop = try self.dbg_info_type_relocs.getOrPut(self.gpa, ty);
662 if (!gop.found_existing) {
663 gop.entry.value = .{
664 .off = undefined,
665 .relocs = .{},
666 };
673 switch (self.debug_output) {
674 .dwarf => |dbg_out| {
675 assert(ty.hasCodeGenBits());
676 const index = dbg_out.dbg_info.items.len;
677 try dbg_out.dbg_info.resize(index + 4); // DW.AT_type, DW.FORM_ref4
678
679 const gop = try dbg_out.dbg_info_type_relocs.getOrPut(self.gpa, ty);
680 if (!gop.found_existing) {
681 gop.entry.value = .{
682 .off = undefined,
683 .relocs = .{},
684 };
685 }
686 try gop.entry.value.relocs.append(self.gpa, @intCast(u32, index));
687 },
688 .none => {},
667689 }
668 try gop.entry.value.relocs.append(self.gpa, @intCast(u32, index));
669690 }
670691
671692 fn genFuncInst(self: *Self, inst: *ir.Inst) !MCValue {
......@@ -1258,14 +1279,19 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
12581279 self.registers.putAssumeCapacityNoClobber(toCanonicalReg(reg), &inst.base);
12591280 self.markRegUsed(reg);
12601281
1261 try self.dbg_info.ensureCapacity(self.dbg_info.items.len + 8 + name_with_null.len);
1262 self.dbg_info.appendAssumeCapacity(link.File.Elf.abbrev_parameter);
1263 self.dbg_info.appendSliceAssumeCapacity(&[2]u8{ // DW.AT_location, DW.FORM_exprloc
1264 1, // ULEB128 dwarf expression length
1265 reg.dwarfLocOp(),
1266 });
1267 try self.addDbgInfoTypeReloc(inst.base.ty); // DW.AT_type, DW.FORM_ref4
1268 self.dbg_info.appendSliceAssumeCapacity(name_with_null); // DW.AT_name, DW.FORM_string
1282 switch (self.debug_output) {
1283 .dwarf => |dbg_out| {
1284 try dbg_out.dbg_info.ensureCapacity(dbg_out.dbg_info.items.len + 8 + name_with_null.len);
1285 dbg_out.dbg_info.appendAssumeCapacity(link.File.Elf.abbrev_parameter);
1286 dbg_out.dbg_info.appendSliceAssumeCapacity(&[2]u8{ // DW.AT_location, DW.FORM_exprloc
1287 1, // ULEB128 dwarf expression length
1288 reg.dwarfLocOp(),
1289 });
1290 try self.addDbgInfoTypeReloc(inst.base.ty); // DW.AT_type, DW.FORM_ref4
1291 dbg_out.dbg_info.appendSliceAssumeCapacity(name_with_null); // DW.AT_name, DW.FORM_string
1292 },
1293 .none => {},
1294 }
12691295 },
12701296 else => {},
12711297 }
......@@ -1302,7 +1328,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
13021328
13031329 // Due to incremental compilation, how function calls are generated depends
13041330 // on linking.
1305 if (self.bin_file.cast(link.File.Elf)) |elf_file| {
1331 if (self.bin_file.tag == link.File.Elf.base_tag or self.bin_file.tag == link.File.Coff.base_tag) {
13061332 switch (arch) {
13071333 .x86_64 => {
13081334 for (info.args) |mc_arg, arg_i| {
......@@ -1341,10 +1367,17 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
13411367 if (inst.func.cast(ir.Inst.Constant)) |func_inst| {
13421368 if (func_inst.val.cast(Value.Payload.Function)) |func_val| {
13431369 const func = func_val.func;
1344 const got = &elf_file.program_headers.items[elf_file.phdr_got_index.?];
1370
13451371 const ptr_bits = self.target.cpu.arch.ptrBitWidth();
13461372 const ptr_bytes: u64 = @divExact(ptr_bits, 8);
1347 const got_addr = @intCast(u32, got.p_vaddr + func.owner_decl.link.elf.offset_table_index * ptr_bytes);
1373 const got_addr = if (self.bin_file.cast(link.File.Elf)) |elf_file| blk: {
1374 const got = &elf_file.program_headers.items[elf_file.phdr_got_index.?];
1375 break :blk @intCast(u32, got.p_vaddr + func.owner_decl.link.elf.offset_table_index * ptr_bytes);
1376 } else if (self.bin_file.cast(link.File.Coff)) |coff_file|
1377 @intCast(u32, coff_file.offset_table_virtual_address + func.owner_decl.link.coff.offset_table_index * ptr_bytes)
1378 else
1379 unreachable;
1380
13481381 // ff 14 25 xx xx xx xx call [addr]
13491382 try self.code.ensureCapacity(self.code.items.len + 7);
13501383 self.code.appendSliceAssumeCapacity(&[3]u8{ 0xff, 0x14, 0x25 });
......@@ -1362,10 +1395,16 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
13621395 if (inst.func.cast(ir.Inst.Constant)) |func_inst| {
13631396 if (func_inst.val.cast(Value.Payload.Function)) |func_val| {
13641397 const func = func_val.func;
1365 const got = &elf_file.program_headers.items[elf_file.phdr_got_index.?];
1398
13661399 const ptr_bits = self.target.cpu.arch.ptrBitWidth();
13671400 const ptr_bytes: u64 = @divExact(ptr_bits, 8);
1368 const got_addr = @intCast(u32, got.p_vaddr + func.owner_decl.link.elf.offset_table_index * ptr_bytes);
1401 const got_addr = if (self.bin_file.cast(link.File.Elf)) |elf_file| blk: {
1402 const got = &elf_file.program_headers.items[elf_file.phdr_got_index.?];
1403 break :blk @intCast(u32, got.p_vaddr + func.owner_decl.link.elf.offset_table_index * ptr_bytes);
1404 } else if (self.bin_file.cast(link.File.Coff)) |coff_file|
1405 coff_file.offset_table_virtual_address + func.owner_decl.link.coff.offset_table_index * ptr_bytes
1406 else
1407 unreachable;
13691408
13701409 try self.genSetReg(inst.base.src, .ra, .{ .memory = got_addr });
13711410 mem.writeIntLittle(u32, try self.code.addManyAsArray(4), Instruction.jalr(.ra, 0, .ra).toU32());
......@@ -1383,8 +1422,14 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
13831422 }
13841423 if (func_inst.val.cast(Value.Payload.Function)) |func_val| {
13851424 const func = func_val.func;
1386 const got = &elf_file.program_headers.items[elf_file.phdr_got_index.?];
1387 const got_addr = @intCast(u16, got.p_vaddr + func.owner_decl.link.elf.offset_table_index * 2);
1425 const got_addr = if (self.bin_file.cast(link.File.Elf)) |elf_file| blk: {
1426 const got = &elf_file.program_headers.items[elf_file.phdr_got_index.?];
1427 break :blk @intCast(u16, got.p_vaddr + func.owner_decl.link.elf.offset_table_index * 2);
1428 } else if (self.bin_file.cast(link.File.Coff)) |coff_file|
1429 @intCast(u16, coff_file.offset_table_virtual_address + func.owner_decl.link.coff.offset_table_index * 2)
1430 else
1431 unreachable;
1432
13881433 const return_type = func.owner_decl.typed_value.most_recent.typed_value.ty.fnReturnType();
13891434 // First, push the return address, then jump; if noreturn, don't bother with the first step
13901435 // TODO: implement packed struct -> u16 at comptime and move the bitcast here
......@@ -1420,10 +1465,15 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
14201465 if (inst.func.cast(ir.Inst.Constant)) |func_inst| {
14211466 if (func_inst.val.cast(Value.Payload.Function)) |func_val| {
14221467 const func = func_val.func;
1423 const got = &elf_file.program_headers.items[elf_file.phdr_got_index.?];
14241468 const ptr_bits = self.target.cpu.arch.ptrBitWidth();
14251469 const ptr_bytes: u64 = @divExact(ptr_bits, 8);
1426 const got_addr = @intCast(u32, got.p_vaddr + func.owner_decl.link.elf.offset_table_index * ptr_bytes);
1470 const got_addr = if (self.bin_file.cast(link.File.Elf)) |elf_file| blk: {
1471 const got = &elf_file.program_headers.items[elf_file.phdr_got_index.?];
1472 break :blk @intCast(u32, got.p_vaddr + func.owner_decl.link.elf.offset_table_index * ptr_bytes);
1473 } else if (self.bin_file.cast(link.File.Coff)) |coff_file|
1474 coff_file.offset_table_virtual_address + func.owner_decl.link.coff.offset_table_index * ptr_bytes
1475 else
1476 unreachable;
14271477
14281478 // TODO only works with leaf functions
14291479 // at the moment, which works fine for
......@@ -1983,7 +2033,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
19832033
19842034 if (mem.eql(u8, inst.asm_source, "syscall")) {
19852035 try self.code.appendSlice(&[_]u8{ 0x0f, 0x05 });
1986 } else {
2036 } else if (inst.asm_source.len != 0) {
19872037 return self.fail(inst.base.src, "TODO implement support for more x86 assembly instructions", .{});
19882038 }
19892039
......@@ -2541,6 +2591,10 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
25412591 const got = &macho_file.sections.items[macho_file.got_section_index.?];
25422592 const got_addr = got.addr + decl.link.macho.offset_table_index.? * ptr_bytes;
25432593 return MCValue{ .memory = got_addr };
2594 } else if (self.bin_file.cast(link.File.Coff)) |coff_file| {
2595 const decl = payload.decl;
2596 const got_addr = coff_file.offset_table_virtual_address + decl.link.coff.offset_table_index * ptr_bytes;
2597 return MCValue{ .memory = got_addr };
25442598 } else {
25452599 return self.fail(src, "TODO codegen non-ELF const Decl pointer", .{});
25462600 }
src-self-hosted/link.zig+2-2
......@@ -34,7 +34,7 @@ pub const File = struct {
3434
3535 pub const LinkBlock = union {
3636 elf: Elf.TextBlock,
37 coff: void, // @TODO
37 coff: Coff.TextBlock,
3838 macho: MachO.TextBlock,
3939 c: void,
4040 wasm: void,
......@@ -42,7 +42,7 @@ pub const File = struct {
4242
4343 pub const LinkFn = union {
4444 elf: Elf.SrcFn,
45 coff: void, // @TODO
45 coff: Coff.SrcFn,
4646 macho: MachO.SrcFn,
4747 c: void,
4848 wasm: ?Wasm.FnData,
src-self-hosted/link/Coff.zig+546-79
......@@ -6,10 +6,22 @@ const Allocator = std.mem.Allocator;
66const assert = std.debug.assert;
77const fs = std.fs;
88
9const trace = @import("../tracy.zig").trace;
910const Module = @import("../Module.zig");
10const codegen = @import("../codegen/wasm.zig");
11const codegen = @import("../codegen.zig");
1112const link = @import("../link.zig");
1213
14const allocation_padding = 4 / 3;
15const minimum_text_block_size = 64 * allocation_padding;
16
17const section_alignment = 4096;
18const file_alignment = 512;
19const image_base = 0x400_000;
20const section_table_size = 2 * 40;
21comptime {
22 std.debug.assert(std.mem.isAligned(image_base, section_alignment));
23}
24
1325pub const base_tag: link.File.Tag = .coff;
1426
1527const msdos_stub = @embedFile("msdos-stub.bin");
......@@ -18,8 +30,85 @@ base: link.File,
1830ptr_width: enum { p32, p64 },
1931error_flags: link.File.ErrorFlags = .{},
2032
21coff_file_header_dirty: bool = false,
22optional_header_dirty: bool = false,
33text_block_free_list: std.ArrayListUnmanaged(*TextBlock) = .{},
34last_text_block: ?*TextBlock = null,
35
36/// Section table file pointer.
37section_table_offset: u32 = 0,
38/// Section data file pointer.
39section_data_offset: u32 = 0,
40/// Optiona header file pointer.
41optional_header_offset: u32 = 0,
42
43/// Absolute virtual address of the offset table when the executable is loaded in memory.
44offset_table_virtual_address: u32 = 0,
45/// Current size of the offset table on disk, must be a multiple of `file_alignment`
46offset_table_size: u32 = 0,
47/// Contains absolute virtual addresses
48offset_table: std.ArrayListUnmanaged(u64) = .{},
49/// Free list of offset table indices
50offset_table_free_list: std.ArrayListUnmanaged(u32) = .{},
51
52/// Virtual address of the entry point procedure relative to `image_base`
53entry_addr: ?u32 = null,
54
55/// Absolute virtual address of the text section when the executable is loaded in memory.
56text_section_virtual_address: u32 = 0,
57/// Current size of the `.text` section on disk, must be a multiple of `file_alignment`
58text_section_size: u32 = 0,
59
60offset_table_size_dirty: bool = false,
61text_section_size_dirty: bool = false,
62/// This flag is set when the virtual size of the whole image file when loaded in memory has changed
63/// and needs to be updated in the optional header.
64size_of_image_dirty: bool = false,
65
66pub const TextBlock = struct {
67 /// Offset of the code relative to the start of the text section
68 text_offset: u32,
69 /// Used size of the text block
70 size: u32,
71 /// This field is undefined for symbols with size = 0.
72 offset_table_index: u32,
73 /// Points to the previous and next neighbors, based on the `text_offset`.
74 /// This can be used to find, for example, the capacity of this `TextBlock`.
75 prev: ?*TextBlock,
76 next: ?*TextBlock,
77
78 pub const empty = TextBlock{
79 .text_offset = 0,
80 .size = 0,
81 .offset_table_index = undefined,
82 .prev = null,
83 .next = null,
84 };
85
86 /// Returns how much room there is to grow in virtual address space.
87 fn capacity(self: TextBlock) u64 {
88 if (self.next) |next| {
89 return next.text_offset - self.text_offset;
90 }
91 // This is the last block, the capacity is only limited by the address space.
92 return std.math.maxInt(u32) - self.text_offset;
93 }
94
95 fn freeListEligible(self: TextBlock) bool {
96 // No need to keep a free list node for the last block.
97 const next = self.next orelse return false;
98 const cap = next.text_offset - self.text_offset;
99 const ideal_cap = self.size * allocation_padding;
100 if (cap <= ideal_cap) return false;
101 const surplus = cap - ideal_cap;
102 return surplus >= minimum_text_block_size;
103 }
104
105 /// Absolute virtual address of the text block when the file is loaded in memory.
106 fn getVAddr(self: TextBlock, coff: Coff) u32 {
107 return coff.text_section_virtual_address + self.text_offset;
108 }
109};
110
111pub const SrcFn = void;
23112
24113pub fn openPath(allocator: *Allocator, dir: fs.Dir, sub_path: []const u8, options: link.Options) !*link.File {
25114 assert(options.object_format == .coff);
......@@ -42,7 +131,7 @@ pub fn openPath(allocator: *Allocator, dir: fs.Dir, sub_path: []const u8, option
42131fn openFile(allocator: *Allocator, file: fs.File, options: link.Options) !Coff {
43132 switch (options.output_mode) {
44133 .Exe => {},
45 .Obj => return error.IncrFailed, // @TODO DO OBJ FILES
134 .Obj => return error.IncrFailed,
46135 .Lib => return error.IncrFailed,
47136 }
48137 var self: Coff = .{
......@@ -67,8 +156,10 @@ fn openFile(allocator: *Allocator, file: fs.File, options: link.Options) !Coff {
67156/// Truncates the existing file contents and overwrites the contents.
68157/// Returns an error if `file` is not already open with +read +write +seek abilities.
69158fn createFile(allocator: *Allocator, file: fs.File, options: link.Options) !Coff {
159 // TODO Write object specific relocations, COFF symbol table, then enable object file output.
70160 switch (options.output_mode) {
71 .Exe, .Obj => {},
161 .Exe => {},
162 .Obj => return error.TODOImplementWritingObjFiles,
72163 .Lib => return error.TODOImplementWritingLibFiles,
73164 }
74165 var self: Coff = .{
......@@ -83,8 +174,6 @@ fn createFile(allocator: *Allocator, file: fs.File, options: link.Options) !Coff
83174 64 => .p64,
84175 else => return error.UnsupportedCOFFArchitecture,
85176 },
86 .coff_file_header_dirty = true,
87 .optional_header_dirty = true,
88177 };
89178 errdefer self.deinit();
90179
......@@ -97,18 +186,14 @@ fn createFile(allocator: *Allocator, file: fs.File, options: link.Options) !Coff
97186
98187 // COFF file header
99188 const data_directory_count = 0;
100 var hdr_data: [112 + data_directory_count * 8 + 2 * 40]u8 = undefined;
189 var hdr_data: [112 + data_directory_count * 8 + section_table_size]u8 = undefined;
101190 var index: usize = 0;
102191
103 // @TODO Add an enum(u16) in std.coff, add .toCoffMachine to Arch
104 const machine_type: u16 = switch (self.base.options.target.cpu.arch) {
105 .x86_64 => 0x8664,
106 .i386 => 0x014c,
107 .riscv32 => 0x5032,
108 .riscv64 => 0x5064,
109 else => return error.UnsupportedCOFFArchitecture,
110 };
111 std.mem.writeIntLittle(u16, hdr_data[0..2], machine_type);
192 const machine = self.base.options.target.cpu.arch.toCoffMachine();
193 if (machine == .Unknown) {
194 return error.UnsupportedCOFFArchitecture;
195 }
196 std.mem.writeIntLittle(u16, hdr_data[0..2], @enumToInt(machine));
112197 index += 2;
113198
114199 // Number of sections (we only use .got, .text)
......@@ -125,20 +210,33 @@ fn createFile(allocator: *Allocator, file: fs.File, options: link.Options) !Coff
125210 },
126211 else => 0,
127212 };
213
214 const section_table_offset = coff_file_header_offset + 20 + optional_header_size;
215 const default_offset_table_size = file_alignment;
216 const default_size_of_code = 0;
217
218 self.section_data_offset = std.mem.alignForwardGeneric(u32, self.section_table_offset + section_table_size, file_alignment);
219 const section_data_relative_virtual_address = std.mem.alignForwardGeneric(u32, self.section_table_offset + section_table_size, section_alignment);
220 self.offset_table_virtual_address = image_base + section_data_relative_virtual_address;
221 self.offset_table_size = default_offset_table_size;
222 self.section_table_offset = section_table_offset;
223 self.text_section_virtual_address = image_base + section_data_relative_virtual_address + section_alignment;
224 self.text_section_size = default_size_of_code;
225
226 // Size of file when loaded in memory
227 const size_of_image = std.mem.alignForwardGeneric(u32, self.text_section_virtual_address - image_base + default_size_of_code, section_alignment);
228
128229 std.mem.writeIntLittle(u16, hdr_data[index..][0..2], optional_header_size);
129230 index += 2;
130231
131 // Characteristics - IMAGE_FILE_DEBUG_STRIPPED
132 var characteristics: u16 = 0x200; // TODO Remove debug info stripped flag when necessary
232 // Characteristics
233 var characteristics: u16 = std.coff.IMAGE_FILE_DEBUG_STRIPPED | std.coff.IMAGE_FILE_RELOCS_STRIPPED; // TODO Remove debug info stripped flag when necessary
133234 if (options.output_mode == .Exe) {
134 // IMAGE_FILE_EXECUTABLE_IMAGE
135 characteristics |= 0x2;
235 characteristics |= std.coff.IMAGE_FILE_EXECUTABLE_IMAGE;
136236 }
137237 switch (self.ptr_width) {
138 // IMAGE_FILE_32BIT_MACHINE
139 .p32 => characteristics |= 0x100,
140 // IMAGE_FILE_LARGE_ADDRESS_AWARE
141 .p64 => characteristics |= 0x20,
238 .p32 => characteristics |= std.coff.IMAGE_FILE_32BIT_MACHINE,
239 .p64 => characteristics |= std.coff.IMAGE_FILE_LARGE_ADDRESS_AWARE,
142240 }
143241 std.mem.writeIntLittle(u16, hdr_data[index..][0..2], characteristics);
144242 index += 2;
......@@ -147,6 +245,7 @@ fn createFile(allocator: *Allocator, file: fs.File, options: link.Options) !Coff
147245 try self.base.file.?.pwriteAll(hdr_data[0..index], coff_file_header_offset);
148246
149247 if (options.output_mode == .Exe) {
248 self.optional_header_offset = coff_file_header_offset + 20;
150249 // Optional header
151250 index = 0;
152251 std.mem.writeIntLittle(u16, hdr_data[0..2], switch (self.ptr_width) {
......@@ -155,34 +254,33 @@ fn createFile(allocator: *Allocator, file: fs.File, options: link.Options) !Coff
155254 });
156255 index += 2;
157256
158 // Linker version (u8 + u8), SizeOfCode (u32), SizeOfInitializedData (u32), SizeOfUninitializedData (u32), AddressOfEntryPoint (u32)
159 std.mem.set(u8, hdr_data[index..][0..18], 0);
160 index += 18;
257 // Linker version (u8 + u8)
258 std.mem.set(u8, hdr_data[index..][0..2], 0);
259 index += 2;
161260
162 // Base of code relative to the image base
163 // @TODO Check where to put this
164 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], 0x1000);
165 index += 4;
261 // SizeOfCode (UNUSED, u32), SizeOfInitializedData (u32), SizeOfUninitializedData (u32), AddressOfEntryPoint (u32), BaseOfCode (UNUSED, u32)
262 std.mem.set(u8, hdr_data[index..][0..20], 0);
263 index += 20;
166264
167265 if (self.ptr_width == .p32) {
168 // Base of data relative to the image base
266 // Base of data relative to the image base (UNUSED)
169267 std.mem.set(u8, hdr_data[index..][0..4], 0);
170268 index += 4;
171269
172270 // Image base address
173 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], 0x400_000);
271 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], image_base);
174272 index += 4;
175273 } else {
176274 // Image base address
177 std.mem.writeIntLittle(u64, hdr_data[index..][0..8], 0x140_000_000);
275 std.mem.writeIntLittle(u64, hdr_data[index..][0..8], image_base);
178276 index += 8;
179277 }
180278
181279 // Section alignment
182 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], 4096);
280 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], section_alignment);
183281 index += 4;
184282 // File alignment
185 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], 512);
283 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], file_alignment);
186284 index += 4;
187285 // Required OS version, 6.0 is vista
188286 std.mem.writeIntLittle(u16, hdr_data[index..][0..2], 6);
......@@ -197,19 +295,25 @@ fn createFile(allocator: *Allocator, file: fs.File, options: link.Options) !Coff
197295 index += 2;
198296 std.mem.writeIntLittle(u16, hdr_data[index..][0..2], 0);
199297 index += 2;
200 // Reserved zeroes (u32), SizeOfImage (u32), SizeOfHeaders (u32), CheckSum (u32)
201 std.mem.set(u8, hdr_data[index..][0..16], 0);
202 index += 16;
298 // Reserved zeroes (u32)
299 std.mem.set(u8, hdr_data[index..][0..4], 0);
300 index += 4;
301 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], size_of_image);
302 index += 4;
303 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], self.section_data_offset);
304 index += 4;
305 // CheckSum (u32)
306 std.mem.set(u8, hdr_data[index..][0..4], 0);
307 index += 4;
203308 // Subsystem, TODO: Let users specify the subsystem, always CUI for now
204309 std.mem.writeIntLittle(u16, hdr_data[index..][0..2], 3);
205310 index += 2;
206 // DLL characteristics, TODO: For now we are just using IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE
207 std.mem.writeIntLittle(u16, hdr_data[index..][0..2], 0x40);
311 // DLL characteristics
312 std.mem.writeIntLittle(u16, hdr_data[index..][0..2], 0x0);
208313 index += 2;
209314
210315 switch (self.ptr_width) {
211316 .p32 => {
212 // @TODO See llvm output for 32 bit executables
213317 // Size of stack reserve + commit
214318 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], 0x1_000_000);
215319 index += 4;
......@@ -242,84 +346,447 @@ fn createFile(allocator: *Allocator, file: fs.File, options: link.Options) !Coff
242346 // Number of data directories
243347 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], data_directory_count);
244348 index += 4;
245 // @TODO Write meaningful stuff here
246349 // Initialize data directories to zero
247 std.mem.set(u8, hdr_data[index..][0..data_directory_count * 8], 0);
350 std.mem.set(u8, hdr_data[index..][0 .. data_directory_count * 8], 0);
248351 index += data_directory_count * 8;
249352
250353 assert(index == optional_header_size);
251354 }
252355
253 // @TODO Merge this write with the one above
254 const section_table_offset = coff_file_header_offset + 20 + optional_header_size;
255
256356 // Write section table.
257357 // First, the .got section
258358 hdr_data[index..][0..8].* = ".got\x00\x00\x00\x00".*;
259359 index += 8;
260 // Virtual size (u32) (@TODO Set to initial value in image files, zero otherwise), Virtual address (u32) (@TODO Set to value in image files, zero otherwise), Size of raw data (u32)
261 std.mem.set(u8, hdr_data[index..][0..12], 0);
262 index += 12;
360 if (options.output_mode == .Exe) {
361 // Virtual size (u32)
362 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], default_offset_table_size);
363 index += 4;
364 // Virtual address (u32)
365 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], self.offset_table_virtual_address - image_base);
366 index += 4;
367 } else {
368 std.mem.set(u8, hdr_data[index..][0..8], 0);
369 index += 8;
370 }
371 // Size of raw data (u32)
372 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], default_offset_table_size);
373 index += 4;
263374 // File pointer to the start of the section
264 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], section_table_offset + 2 * 40);
375 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], self.section_data_offset);
265376 index += 4;
266 // Pointer to relocations (u32) (@TODO Initialize this for object files), PointerToLinenumbers (u32), NumberOfRelocations (u16), (@TODO Initialize this for object files), NumberOfLinenumbers (u16)
377 // Pointer to relocations (u32), PointerToLinenumbers (u32), NumberOfRelocations (u16), NumberOfLinenumbers (u16)
267378 std.mem.set(u8, hdr_data[index..][0..12], 0);
268379 index += 12;
269 // Characteristics `IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ = 0x40000040`
270 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], 0x40000040);
380 // Section flags
381 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], std.coff.IMAGE_SCN_CNT_INITIALIZED_DATA | std.coff.IMAGE_SCN_MEM_READ);
271382 index += 4;
272383 // Then, the .text section
273384 hdr_data[index..][0..8].* = ".text\x00\x00\x00".*;
274385 index += 8;
275 // Virtual size (u32) (@TODO Set to initial value in image files, zero otherwise), Virtual address (u32) (@TODO Set to value in image files, zero otherwise), Size of raw data (u32)
276 std.mem.set(u8, hdr_data[index..][0..12], 0);
277 index += 12;
278 // File pointer to the start of the section (@TODO Add the initial size of .got)
279 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], section_table_offset + 2 * 40);
386 if (options.output_mode == .Exe) {
387 // Virtual size (u32)
388 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], default_size_of_code);
389 index += 4;
390 // Virtual address (u32)
391 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], self.text_section_virtual_address - image_base);
392 index += 4;
393 } else {
394 std.mem.set(u8, hdr_data[index..][0..8], 0);
395 index += 8;
396 }
397 // Size of raw data (u32)
398 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], default_size_of_code);
399 index += 4;
400 // File pointer to the start of the section
401 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], self.section_data_offset + default_offset_table_size);
280402 index += 4;
281 // Pointer to relocations (u32) (@TODO Initialize this for object files), PointerToLinenumbers (u32), NumberOfRelocations (u16), (@TODO Initialize this for object files), NumberOfLinenumbers (u16)
403 // Pointer to relocations (u32), PointerToLinenumbers (u32), NumberOfRelocations (u16), NumberOfLinenumbers (u16)
282404 std.mem.set(u8, hdr_data[index..][0..12], 0);
283405 index += 12;
284 // Characteristics `IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE = 0xE0000020`
285 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], 0xE0000020);
406 // Section flags
407 std.mem.writeIntLittle(
408 u32,
409 hdr_data[index..][0..4],
410 std.coff.IMAGE_SCN_CNT_CODE | std.coff.IMAGE_SCN_MEM_EXECUTE | std.coff.IMAGE_SCN_MEM_READ | std.coff.IMAGE_SCN_MEM_WRITE,
411 );
286412 index += 4;
287413
288 assert(index == optional_header_size + 2 * 40);
289 try self.base.file.?.pwriteAll(hdr_data[0..index], coff_file_header_offset + 20);
414 assert(index == optional_header_size + section_table_size);
415 try self.base.file.?.pwriteAll(hdr_data[0..index], self.optional_header_offset);
416 try self.base.file.?.setEndPos(self.section_data_offset + default_offset_table_size + default_size_of_code);
290417
291418 return self;
292419}
293420
294pub fn flush(self: *Coff, module: *Module) !void {
295 // @TODO Implement this
421pub fn allocateDeclIndexes(self: *Coff, decl: *Module.Decl) !void {
422 try self.offset_table.ensureCapacity(self.base.allocator, self.offset_table.items.len + 1);
423
424 if (self.offset_table_free_list.popOrNull()) |i| {
425 decl.link.coff.offset_table_index = i;
426 } else {
427 decl.link.coff.offset_table_index = @intCast(u32, self.offset_table.items.len);
428 _ = self.offset_table.addOneAssumeCapacity();
429
430 const entry_size = self.base.options.target.cpu.arch.ptrBitWidth() / 8;
431 if (self.offset_table.items.len > self.offset_table_size / entry_size) {
432 self.offset_table_size_dirty = true;
433 }
434 }
435
436 self.offset_table.items[decl.link.coff.offset_table_index] = 0;
296437}
297438
298pub fn freeDecl(self: *Coff, decl: *Module.Decl) void {
299 // @TODO Implement this
439fn allocateTextBlock(self: *Coff, text_block: *TextBlock, new_block_size: u64, alignment: u64) !u64 {
440 const new_block_min_capacity = new_block_size * allocation_padding;
441
442 // We use these to indicate our intention to update metadata, placing the new block,
443 // and possibly removing a free list node.
444 // It would be simpler to do it inside the for loop below, but that would cause a
445 // problem if an error was returned later in the function. So this action
446 // is actually carried out at the end of the function, when errors are no longer possible.
447 var block_placement: ?*TextBlock = null;
448 var free_list_removal: ?usize = null;
449
450 const vaddr = blk: {
451 var i: usize = 0;
452 while (i < self.text_block_free_list.items.len) {
453 const free_block = self.text_block_free_list.items[i];
454
455 const next_block_text_offset = free_block.text_offset + free_block.capacity();
456 const new_block_text_offset = std.mem.alignForwardGeneric(u64, free_block.getVAddr(self.*) + free_block.size, alignment) - self.text_section_virtual_address;
457 if (new_block_text_offset < next_block_text_offset and next_block_text_offset - new_block_text_offset >= new_block_min_capacity) {
458 block_placement = free_block;
459
460 const remaining_capacity = next_block_text_offset - new_block_text_offset - new_block_min_capacity;
461 if (remaining_capacity < minimum_text_block_size) {
462 free_list_removal = i;
463 }
464
465 break :blk new_block_text_offset + self.text_section_virtual_address;
466 } else {
467 if (!free_block.freeListEligible()) {
468 _ = self.text_block_free_list.swapRemove(i);
469 } else {
470 i += 1;
471 }
472 continue;
473 }
474 } else if (self.last_text_block) |last| {
475 const new_block_vaddr = std.mem.alignForwardGeneric(u64, last.getVAddr(self.*) + last.size, alignment);
476 block_placement = last;
477 break :blk new_block_vaddr;
478 } else {
479 break :blk self.text_section_virtual_address;
480 }
481 };
482
483 const expand_text_section = block_placement == null or block_placement.?.next == null;
484 if (expand_text_section) {
485 const needed_size = @intCast(u32, std.mem.alignForwardGeneric(u64, vaddr + new_block_size - self.text_section_virtual_address, file_alignment));
486 if (needed_size > self.text_section_size) {
487 const current_text_section_virtual_size = std.mem.alignForwardGeneric(u32, self.text_section_size, section_alignment);
488 const new_text_section_virtual_size = std.mem.alignForwardGeneric(u32, needed_size, section_alignment);
489 if (current_text_section_virtual_size != new_text_section_virtual_size) {
490 self.size_of_image_dirty = true;
491 // Write new virtual size
492 var buf: [4]u8 = undefined;
493 std.mem.writeIntLittle(u32, &buf, new_text_section_virtual_size);
494 try self.base.file.?.pwriteAll(&buf, self.section_table_offset + 40 + 8);
495 }
496
497 self.text_section_size = needed_size;
498 self.text_section_size_dirty = true;
499 }
500 self.last_text_block = text_block;
501 }
502 text_block.text_offset = @intCast(u32, vaddr - self.text_section_virtual_address);
503 text_block.size = @intCast(u32, new_block_size);
504
505 // This function can also reallocate a text block.
506 // In this case we need to "unplug" it from its previous location before
507 // plugging it in to its new location.
508 if (text_block.prev) |prev| {
509 prev.next = text_block.next;
510 }
511 if (text_block.next) |next| {
512 next.prev = text_block.prev;
513 }
514
515 if (block_placement) |big_block| {
516 text_block.prev = big_block;
517 text_block.next = big_block.next;
518 big_block.next = text_block;
519 } else {
520 text_block.prev = null;
521 text_block.next = null;
522 }
523 if (free_list_removal) |i| {
524 _ = self.text_block_free_list.swapRemove(i);
525 }
526 return vaddr;
300527}
301528
302pub fn updateDecl(self: *Coff, module: *Module, decl: *Module.Decl) !void {
303 // @TODO Implement this
529fn growTextBlock(self: *Coff, text_block: *TextBlock, new_block_size: u64, alignment: u64) !u64 {
530 const block_vaddr = text_block.getVAddr(self.*);
531 const align_ok = std.mem.alignBackwardGeneric(u64, block_vaddr, alignment) == block_vaddr;
532 const need_realloc = !align_ok or new_block_size > text_block.capacity();
533 if (!need_realloc) return @as(u64, block_vaddr);
534 return self.allocateTextBlock(text_block, new_block_size, alignment);
304535}
305536
306pub fn updateDeclLineNumber(self: *Coff, module: *Module, decl: *Module.Decl) !void {
307 // @TODO Implement this
537fn shrinkTextBlock(self: *Coff, text_block: *TextBlock, new_block_size: u64) void {
538 text_block.size = @intCast(u32, new_block_size);
539 if (text_block.capacity() - text_block.size >= minimum_text_block_size) {
540 self.text_block_free_list.append(self.base.allocator, text_block) catch {};
541 }
308542}
309543
310pub fn allocateDeclIndexes(self: *Coff, decl: *Module.Decl) !void {
311 // @TODO Implement this
544fn freeTextBlock(self: *Coff, text_block: *TextBlock) void {
545 var already_have_free_list_node = false;
546 {
547 var i: usize = 0;
548 // TODO turn text_block_free_list into a hash map
549 while (i < self.text_block_free_list.items.len) {
550 if (self.text_block_free_list.items[i] == text_block) {
551 _ = self.text_block_free_list.swapRemove(i);
552 continue;
553 }
554 if (self.text_block_free_list.items[i] == text_block.prev) {
555 already_have_free_list_node = true;
556 }
557 i += 1;
558 }
559 }
560 if (self.last_text_block == text_block) {
561 self.last_text_block = text_block.prev;
562 }
563 if (text_block.prev) |prev| {
564 prev.next = text_block.next;
565
566 if (!already_have_free_list_node and prev.freeListEligible()) {
567 // The free list is heuristics, it doesn't have to be perfect, so we can
568 // ignore the OOM here.
569 self.text_block_free_list.append(self.base.allocator, prev) catch {};
570 }
571 }
572
573 if (text_block.next) |next| {
574 next.prev = text_block.prev;
575 }
576}
577
578fn writeOffsetTableEntry(self: *Coff, index: usize) !void {
579 const entry_size = self.base.options.target.cpu.arch.ptrBitWidth() / 8;
580 const endian = self.base.options.target.cpu.arch.endian();
581
582 const offset_table_start = self.section_data_offset;
583 if (self.offset_table_size_dirty) {
584 const current_raw_size = self.offset_table_size;
585 const new_raw_size = self.offset_table_size * 2;
586 log.debug("growing offset table from raw size {} to {}\n", .{ current_raw_size, new_raw_size });
587
588 // Move the text section to a new place in the executable
589 const current_text_section_start = self.section_data_offset + current_raw_size;
590 const new_text_section_start = self.section_data_offset + new_raw_size;
591
592 const amt = try self.base.file.?.copyRangeAll(current_text_section_start, self.base.file.?, new_text_section_start, self.text_section_size);
593 if (amt != self.text_section_size) return error.InputOutput;
594
595 // Write the new raw size in the .got header
596 var buf: [8]u8 = undefined;
597 std.mem.writeIntLittle(u32, buf[0..4], new_raw_size);
598 try self.base.file.?.pwriteAll(buf[0..4], self.section_table_offset + 16);
599 // Write the new .text section file offset in the .text section header
600 std.mem.writeIntLittle(u32, buf[0..4], new_text_section_start);
601 try self.base.file.?.pwriteAll(buf[0..4], self.section_table_offset + 40 + 20);
602
603 const current_virtual_size = std.mem.alignForwardGeneric(u32, self.offset_table_size, section_alignment);
604 const new_virtual_size = std.mem.alignForwardGeneric(u32, new_raw_size, section_alignment);
605 // If we had to move in the virtual address space, we need to fix the VAs in the offset table, as well as the virtual address of the `.text` section
606 // and the virutal size of the `.got` section
607
608 if (new_virtual_size != current_virtual_size) {
609 log.debug("growing offset table from virtual size {} to {}\n", .{ current_virtual_size, new_virtual_size });
610 self.size_of_image_dirty = true;
611 const va_offset = new_virtual_size - current_virtual_size;
612
613 // Write .got virtual size
614 std.mem.writeIntLittle(u32, buf[0..4], new_virtual_size);
615 try self.base.file.?.pwriteAll(buf[0..4], self.section_table_offset + 8);
616
617 // Write .text new virtual address
618 self.text_section_virtual_address = self.text_section_virtual_address + va_offset;
619 std.mem.writeIntLittle(u32, buf[0..4], self.text_section_virtual_address - image_base);
620 try self.base.file.?.pwriteAll(buf[0..4], self.section_table_offset + 40 + 12);
621
622 // Fix the VAs in the offset table
623 for (self.offset_table.items) |*va, idx| {
624 if (va.* != 0) {
625 va.* += va_offset;
626
627 switch (entry_size) {
628 4 => {
629 std.mem.writeInt(u32, buf[0..4], @intCast(u32, va.*), endian);
630 try self.base.file.?.pwriteAll(buf[0..4], offset_table_start + idx * entry_size);
631 },
632 8 => {
633 std.mem.writeInt(u64, &buf, va.*, endian);
634 try self.base.file.?.pwriteAll(&buf, offset_table_start + idx * entry_size);
635 },
636 else => unreachable,
637 }
638 }
639 }
640 }
641 self.offset_table_size = new_raw_size;
642 self.offset_table_size_dirty = false;
643 }
644 // Write the new entry
645 switch (entry_size) {
646 4 => {
647 var buf: [4]u8 = undefined;
648 std.mem.writeInt(u32, &buf, @intCast(u32, self.offset_table.items[index]), endian);
649 try self.base.file.?.pwriteAll(&buf, offset_table_start + index * entry_size);
650 },
651 8 => {
652 var buf: [8]u8 = undefined;
653 std.mem.writeInt(u64, &buf, self.offset_table.items[index], endian);
654 try self.base.file.?.pwriteAll(&buf, offset_table_start + index * entry_size);
655 },
656 else => unreachable,
657 }
658}
659
660pub fn updateDecl(self: *Coff, module: *Module, decl: *Module.Decl) !void {
661 // TODO COFF/PE debug information
662 // TODO Implement exports
663 const tracy = trace(@src());
664 defer tracy.end();
665
666 var code_buffer = std.ArrayList(u8).init(self.base.allocator);
667 defer code_buffer.deinit();
668
669 const typed_value = decl.typed_value.most_recent.typed_value;
670 const res = try codegen.generateSymbol(&self.base, decl.src(), typed_value, &code_buffer, .none);
671 const code = switch (res) {
672 .externally_managed => |x| x,
673 .appended => code_buffer.items,
674 .fail => |em| {
675 decl.analysis = .codegen_failure;
676 try module.failed_decls.put(module.gpa, decl, em);
677 return;
678 },
679 };
680
681 const required_alignment = typed_value.ty.abiAlignment(self.base.options.target);
682 const curr_size = decl.link.coff.size;
683 if (curr_size != 0) {
684 const capacity = decl.link.coff.capacity();
685 const need_realloc = code.len > capacity or
686 !std.mem.isAlignedGeneric(u32, decl.link.coff.text_offset, required_alignment);
687 if (need_realloc) {
688 const curr_vaddr = self.getDeclVAddr(decl);
689 const vaddr = try self.growTextBlock(&decl.link.coff, code.len, required_alignment);
690 log.debug("growing {} from 0x{x} to 0x{x}\n", .{ decl.name, curr_vaddr, vaddr });
691 if (vaddr != curr_vaddr) {
692 log.debug(" (writing new offset table entry)\n", .{});
693 self.offset_table.items[decl.link.coff.offset_table_index] = vaddr;
694 try self.writeOffsetTableEntry(decl.link.coff.offset_table_index);
695 }
696 } else if (code.len < curr_size) {
697 self.shrinkTextBlock(&decl.link.coff, code.len);
698 }
699 } else {
700 const vaddr = try self.allocateTextBlock(&decl.link.coff, code.len, required_alignment);
701 log.debug("allocated text block for {} at 0x{x} (size: {Bi})\n", .{ std.mem.spanZ(decl.name), vaddr, code.len });
702 errdefer self.freeTextBlock(&decl.link.coff);
703 self.offset_table.items[decl.link.coff.offset_table_index] = vaddr;
704 try self.writeOffsetTableEntry(decl.link.coff.offset_table_index);
705 }
706
707 // Write the code into the file
708 try self.base.file.?.pwriteAll(code, self.section_data_offset + self.offset_table_size + decl.link.coff.text_offset);
709
710 // Since we updated the vaddr and the size, each corresponding export symbol also needs to be updated.
711 const decl_exports = module.decl_exports.get(decl) orelse &[0]*Module.Export{};
712 return self.updateDeclExports(module, decl, decl_exports);
713}
714
715pub fn freeDecl(self: *Coff, decl: *Module.Decl) void {
716 // Appending to free lists is allowed to fail because the free lists are heuristics based anyway.
717 self.freeTextBlock(&decl.link.coff);
718 self.offset_table_free_list.append(self.base.allocator, decl.link.coff.offset_table_index) catch {};
312719}
313720
314721pub fn updateDeclExports(self: *Coff, module: *Module, decl: *const Module.Decl, exports: []const *Module.Export) !void {
315 // @TODO Implement this
722 for (exports) |exp| {
723 if (exp.options.section) |section_name| {
724 if (!std.mem.eql(u8, section_name, ".text")) {
725 try module.failed_exports.ensureCapacity(module.gpa, module.failed_exports.items().len + 1);
726 module.failed_exports.putAssumeCapacityNoClobber(
727 exp,
728 try Module.ErrorMsg.create(self.base.allocator, 0, "Unimplemented: ExportOptions.section", .{}),
729 );
730 continue;
731 }
732 }
733 if (std.mem.eql(u8, exp.options.name, "_start")) {
734 self.entry_addr = decl.link.coff.getVAddr(self.*) - image_base;
735 } else {
736 try module.failed_exports.ensureCapacity(module.gpa, module.failed_exports.items().len + 1);
737 module.failed_exports.putAssumeCapacityNoClobber(
738 exp,
739 try Module.ErrorMsg.create(self.base.allocator, 0, "Unimplemented: Exports other than '_start'", .{}),
740 );
741 continue;
742 }
743 }
744}
745
746pub fn flush(self: *Coff, module: *Module) !void {
747 if (self.text_section_size_dirty) {
748 // Write the new raw size in the .text header
749 var buf: [4]u8 = undefined;
750 std.mem.writeIntLittle(u32, &buf, self.text_section_size);
751 try self.base.file.?.pwriteAll(&buf, self.section_table_offset + 40 + 16);
752 try self.base.file.?.setEndPos(self.section_data_offset + self.offset_table_size + self.text_section_size);
753 self.text_section_size_dirty = false;
754 }
755
756 if (self.base.options.output_mode == .Exe and self.size_of_image_dirty) {
757 const new_size_of_image = std.mem.alignForwardGeneric(u32, self.text_section_virtual_address - image_base + self.text_section_size, section_alignment);
758 var buf: [4]u8 = undefined;
759 std.mem.writeIntLittle(u32, &buf, new_size_of_image);
760 try self.base.file.?.pwriteAll(&buf, self.optional_header_offset + 56);
761 self.size_of_image_dirty = false;
762 }
763
764 if (self.entry_addr == null and self.base.options.output_mode == .Exe) {
765 log.debug("flushing. no_entry_point_found = true\n", .{});
766 self.error_flags.no_entry_point_found = true;
767 } else {
768 log.debug("flushing. no_entry_point_found = false\n", .{});
769 self.error_flags.no_entry_point_found = false;
770
771 if (self.base.options.output_mode == .Exe) {
772 // Write AddressOfEntryPoint
773 var buf: [4]u8 = undefined;
774 std.mem.writeIntLittle(u32, &buf, self.entry_addr.?);
775 try self.base.file.?.pwriteAll(&buf, self.optional_header_offset + 16);
776 }
777 }
316778}
317779
318780pub fn getDeclVAddr(self: *Coff, decl: *const Module.Decl) u64 {
319 // @TODO Implement this
320 return 0;
781 return self.text_section_virtual_address + decl.link.coff.text_offset;
782}
783
784pub fn updateDeclLineNumber(self: *Coff, module: *Module, decl: *Module.Decl) !void {
785 // TODO Implement this
321786}
322787
323788pub fn deinit(self: *Coff) void {
324 // @TODO
789 self.text_block_free_list.deinit(self.base.allocator);
790 self.offset_table.deinit(self.base.allocator);
791 self.offset_table_free_list.deinit(self.base.allocator);
325792}
src-self-hosted/link/Elf.zig+7-1
......@@ -1735,7 +1735,13 @@ pub fn updateDecl(self: *Elf, module: *Module, decl: *Module.Decl) !void {
17351735 } else {
17361736 // TODO implement .debug_info for global variables
17371737 }
1738 const res = try codegen.generateSymbol(&self.base, decl.src(), typed_value, &code_buffer, &dbg_line_buffer, &dbg_info_buffer, &dbg_info_type_relocs);
1738 const res = try codegen.generateSymbol(&self.base, decl.src(), typed_value, &code_buffer, .{
1739 .dwarf = .{
1740 .dbg_line = &dbg_line_buffer,
1741 .dbg_info = &dbg_info_buffer,
1742 .dbg_info_type_relocs = &dbg_info_type_relocs,
1743 },
1744 });
17391745 const code = switch (res) {
17401746 .externally_managed => |x| x,
17411747 .appended => code_buffer.items,
src-self-hosted/main.zig+9-4
......@@ -524,17 +524,22 @@ fn buildOutputType(
524524 try stderr.print("\nUnable to parse command: {}\n", .{@errorName(err)});
525525 continue;
526526 }) |line| {
527 if (mem.eql(u8, line, "update")) {
527 const actual_line = if (line[line.len - 1] == '\r')
528 line[0 .. line.len - 1]
529 else
530 line;
531
532 if (mem.eql(u8, actual_line, "update")) {
528533 if (output_mode == .Exe) {
529534 try module.makeBinFileWritable();
530535 }
531536 try updateModule(gpa, &module, zir_out_path);
532 } else if (mem.eql(u8, line, "exit")) {
537 } else if (mem.eql(u8, actual_line, "exit")) {
533538 break;
534 } else if (mem.eql(u8, line, "help")) {
539 } else if (mem.eql(u8, actual_line, "help")) {
535540 try stderr.writeAll(repl_help);
536541 } else {
537 try stderr.print("unknown command: {}\n", .{line});
542 try stderr.print("unknown command: {}\n", .{actual_line});
538543 }
539544 } else {
540545 break;