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;...@@ -18,11 +18,77 @@ const IMAGE_FILE_MACHINE_I386 = 0x014c;
18const IMAGE_FILE_MACHINE_IA64 = 0x0200;18const IMAGE_FILE_MACHINE_IA64 = 0x0200;
19const IMAGE_FILE_MACHINE_AMD64 = 0x8664;19const 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
21// OptionalHeader.magic values73// OptionalHeader.magic values
22// see https://msdn.microsoft.com/en-us/library/windows/desktop/ms680339(v=vs.85).aspx74// see https://msdn.microsoft.com/en-us/library/windows/desktop/ms680339(v=vs.85).aspx
23const IMAGE_NT_OPTIONAL_HDR32_MAGIC = 0x10b;75const IMAGE_NT_OPTIONAL_HDR32_MAGIC = 0x10b;
24const IMAGE_NT_OPTIONAL_HDR64_MAGIC = 0x20b;76const 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
26const IMAGE_NUMBEROF_DIRECTORY_ENTRIES = 16;92const IMAGE_NUMBEROF_DIRECTORY_ENTRIES = 16;
27const IMAGE_DEBUG_TYPE_CODEVIEW = 2;93const IMAGE_DEBUG_TYPE_CODEVIEW = 2;
28const DEBUG_DIRECTORY = 6;94const DEBUG_DIRECTORY = 6;
lib/std/target.zig+57
...@@ -771,6 +771,63 @@ pub const Target = struct {...@@ -771,6 +771,63 @@ pub const Target = struct {
771 };771 };
772 }772 }
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
774 pub fn endian(arch: Arch) builtin.Endian {831 pub fn endian(arch: Arch) builtin.Endian {
775 return switch (arch) {832 return switch (arch) {
776 .avr,833 .avr,
src-self-hosted/Module.zig+2-2
...@@ -2081,14 +2081,14 @@ fn allocateNewDecl(...@@ -2081,14 +2081,14 @@ fn allocateNewDecl(
2081 .deletion_flag = false,2081 .deletion_flag = false,
2082 .contents_hash = contents_hash,2082 .contents_hash = contents_hash,
2083 .link = switch (self.bin_file.tag) {2083 .link = switch (self.bin_file.tag) {
2084 .coff => .{ .coff = {} }, // @TODO2084 .coff => .{ .coff = link.File.Coff.TextBlock.empty },
2085 .elf => .{ .elf = link.File.Elf.TextBlock.empty },2085 .elf => .{ .elf = link.File.Elf.TextBlock.empty },
2086 .macho => .{ .macho = link.File.MachO.TextBlock.empty },2086 .macho => .{ .macho = link.File.MachO.TextBlock.empty },
2087 .c => .{ .c = {} },2087 .c => .{ .c = {} },
2088 .wasm => .{ .wasm = {} },2088 .wasm => .{ .wasm = {} },
2089 },2089 },
2090 .fn_link = switch (self.bin_file.tag) {2090 .fn_link = switch (self.bin_file.tag) {
2091 .coff => .{ .coff = {} }, // @TODO2091 .coff => .{ .coff = {} },
2092 .elf => .{ .elf = link.File.Elf.SrcFn.empty },2092 .elf => .{ .elf = link.File.Elf.SrcFn.empty },
2093 .macho => .{ .macho = link.File.MachO.SrcFn.empty },2093 .macho => .{ .macho = link.File.MachO.SrcFn.empty },
2094 .c => .{ .c = {} },2094 .c => .{ .c = {} },
src-self-hosted/codegen.zig+166-112
...@@ -59,14 +59,21 @@ pub const GenerateSymbolError = error{...@@ -59,14 +59,21 @@ pub const GenerateSymbolError = error{
59 AnalysisFail,59 AnalysisFail,
60};60};
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
62pub fn generateSymbol(71pub fn generateSymbol(
63 bin_file: *link.File,72 bin_file: *link.File,
64 src: usize,73 src: usize,
65 typed_value: TypedValue,74 typed_value: TypedValue,
66 code: *std.ArrayList(u8),75 code: *std.ArrayList(u8),
67 dbg_line: *std.ArrayList(u8),76 debug_output: DebugInfoOutput,
68 dbg_info: *std.ArrayList(u8),
69 dbg_info_type_relocs: *link.File.DbgInfoTypeRelocsTable,
70) GenerateSymbolError!Result {77) GenerateSymbolError!Result {
71 const tracy = trace(@src());78 const tracy = trace(@src());
72 defer tracy.end();79 defer tracy.end();
...@@ -76,56 +83,56 @@ pub fn generateSymbol(...@@ -76,56 +83,56 @@ pub fn generateSymbol(
76 switch (bin_file.options.target.cpu.arch) {83 switch (bin_file.options.target.cpu.arch) {
77 .wasm32 => unreachable, // has its own code path84 .wasm32 => unreachable, // has its own code path
78 .wasm64 => unreachable, // has its own code path85 .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),86 .arm => return Function(.arm).generateSymbol(bin_file, src, typed_value, code, debug_output),
80 .armeb => return Function(.armeb).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),87 .armeb => return Function(.armeb).generateSymbol(bin_file, src, typed_value, code, debug_output),
81 //.aarch64 => return Function(.aarch64).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),88 //.aarch64 => return Function(.aarch64).generateSymbol(bin_file, src, typed_value, code, debug_output),
82 //.aarch64_be => return Function(.aarch64_be).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),89 //.aarch64_be => return Function(.aarch64_be).generateSymbol(bin_file, src, typed_value, code, debug_output),
83 //.aarch64_32 => return Function(.aarch64_32).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),90 //.aarch64_32 => return Function(.aarch64_32).generateSymbol(bin_file, src, typed_value, code, debug_output),
84 //.arc => return Function(.arc).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),91 //.arc => return Function(.arc).generateSymbol(bin_file, src, typed_value, code, debug_output),
85 //.avr => return Function(.avr).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),92 //.avr => return Function(.avr).generateSymbol(bin_file, src, typed_value, code, debug_output),
86 //.bpfel => return Function(.bpfel).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),93 //.bpfel => return Function(.bpfel).generateSymbol(bin_file, src, typed_value, code, debug_output),
87 //.bpfeb => return Function(.bpfeb).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),94 //.bpfeb => return Function(.bpfeb).generateSymbol(bin_file, src, typed_value, code, debug_output),
88 //.hexagon => return Function(.hexagon).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),95 //.hexagon => return Function(.hexagon).generateSymbol(bin_file, src, typed_value, code, debug_output),
89 //.mips => return Function(.mips).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),96 //.mips => return Function(.mips).generateSymbol(bin_file, src, typed_value, code, debug_output),
90 //.mipsel => return Function(.mipsel).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),97 //.mipsel => return Function(.mipsel).generateSymbol(bin_file, src, typed_value, code, debug_output),
91 //.mips64 => return Function(.mips64).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),98 //.mips64 => return Function(.mips64).generateSymbol(bin_file, src, typed_value, code, debug_output),
92 //.mips64el => return Function(.mips64el).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),99 //.mips64el => return Function(.mips64el).generateSymbol(bin_file, src, typed_value, code, debug_output),
93 //.msp430 => return Function(.msp430).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),100 //.msp430 => return Function(.msp430).generateSymbol(bin_file, src, typed_value, code, debug_output),
94 //.powerpc => return Function(.powerpc).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),101 //.powerpc => return Function(.powerpc).generateSymbol(bin_file, src, typed_value, code, debug_output),
95 //.powerpc64 => return Function(.powerpc64).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),102 //.powerpc64 => return Function(.powerpc64).generateSymbol(bin_file, src, typed_value, code, debug_output),
96 //.powerpc64le => return Function(.powerpc64le).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),103 //.powerpc64le => return Function(.powerpc64le).generateSymbol(bin_file, src, typed_value, code, debug_output),
97 //.r600 => return Function(.r600).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),104 //.r600 => return Function(.r600).generateSymbol(bin_file, src, typed_value, code, debug_output),
98 //.amdgcn => return Function(.amdgcn).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),105 //.amdgcn => return Function(.amdgcn).generateSymbol(bin_file, src, typed_value, code, debug_output),
99 //.riscv32 => return Function(.riscv32).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),106 //.riscv32 => return Function(.riscv32).generateSymbol(bin_file, src, typed_value, code, debug_output),
100 .riscv64 => return Function(.riscv64).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),107 .riscv64 => return Function(.riscv64).generateSymbol(bin_file, src, typed_value, code, debug_output),
101 //.sparc => return Function(.sparc).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),108 //.sparc => return Function(.sparc).generateSymbol(bin_file, src, typed_value, code, debug_output),
102 //.sparcv9 => return Function(.sparcv9).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),109 //.sparcv9 => return Function(.sparcv9).generateSymbol(bin_file, src, typed_value, code, debug_output),
103 //.sparcel => return Function(.sparcel).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),110 //.sparcel => return Function(.sparcel).generateSymbol(bin_file, src, typed_value, code, debug_output),
104 //.s390x => return Function(.s390x).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),111 //.s390x => return Function(.s390x).generateSymbol(bin_file, src, typed_value, code, debug_output),
105 .spu_2 => return Function(.spu_2).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),112 .spu_2 => return Function(.spu_2).generateSymbol(bin_file, src, typed_value, code, debug_output),
106 //.tce => return Function(.tce).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),113 //.tce => return Function(.tce).generateSymbol(bin_file, src, typed_value, code, debug_output),
107 //.tcele => return Function(.tcele).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),114 //.tcele => return Function(.tcele).generateSymbol(bin_file, src, typed_value, code, debug_output),
108 //.thumb => return Function(.thumb).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),115 //.thumb => return Function(.thumb).generateSymbol(bin_file, src, typed_value, code, debug_output),
109 //.thumbeb => return Function(.thumbeb).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),116 //.thumbeb => return Function(.thumbeb).generateSymbol(bin_file, src, typed_value, code, debug_output),
110 //.i386 => return Function(.i386).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),117 //.i386 => return Function(.i386).generateSymbol(bin_file, src, typed_value, code, debug_output),
111 .x86_64 => return Function(.x86_64).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),118 .x86_64 => return Function(.x86_64).generateSymbol(bin_file, src, typed_value, code, debug_output),
112 //.xcore => return Function(.xcore).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),119 //.xcore => return Function(.xcore).generateSymbol(bin_file, src, typed_value, code, debug_output),
113 //.nvptx => return Function(.nvptx).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),120 //.nvptx => return Function(.nvptx).generateSymbol(bin_file, src, typed_value, code, debug_output),
114 //.nvptx64 => return Function(.nvptx64).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),121 //.nvptx64 => return Function(.nvptx64).generateSymbol(bin_file, src, typed_value, code, debug_output),
115 //.le32 => return Function(.le32).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),122 //.le32 => return Function(.le32).generateSymbol(bin_file, src, typed_value, code, debug_output),
116 //.le64 => return Function(.le64).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),123 //.le64 => return Function(.le64).generateSymbol(bin_file, src, typed_value, code, debug_output),
117 //.amdil => return Function(.amdil).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),124 //.amdil => return Function(.amdil).generateSymbol(bin_file, src, typed_value, code, debug_output),
118 //.amdil64 => return Function(.amdil64).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),125 //.amdil64 => return Function(.amdil64).generateSymbol(bin_file, src, typed_value, code, debug_output),
119 //.hsail => return Function(.hsail).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),126 //.hsail => return Function(.hsail).generateSymbol(bin_file, src, typed_value, code, debug_output),
120 //.hsail64 => return Function(.hsail64).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),127 //.hsail64 => return Function(.hsail64).generateSymbol(bin_file, src, typed_value, code, debug_output),
121 //.spir => return Function(.spir).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),128 //.spir => return Function(.spir).generateSymbol(bin_file, src, typed_value, code, debug_output),
122 //.spir64 => return Function(.spir64).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),129 //.spir64 => return Function(.spir64).generateSymbol(bin_file, src, typed_value, code, debug_output),
123 //.kalimba => return Function(.kalimba).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),130 //.kalimba => return Function(.kalimba).generateSymbol(bin_file, src, typed_value, code, debug_output),
124 //.shave => return Function(.shave).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),131 //.shave => return Function(.shave).generateSymbol(bin_file, src, typed_value, code, debug_output),
125 //.lanai => return Function(.lanai).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),132 //.lanai => return Function(.lanai).generateSymbol(bin_file, src, typed_value, code, debug_output),
126 //.renderscript32 => return Function(.renderscript32).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),133 //.renderscript32 => return Function(.renderscript32).generateSymbol(bin_file, src, typed_value, code, debug_output),
127 //.renderscript64 => return Function(.renderscript64).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),134 //.renderscript64 => return Function(.renderscript64).generateSymbol(bin_file, src, typed_value, code, debug_output),
128 //.ve => return Function(.ve).generateSymbol(bin_file, src, typed_value, code, dbg_line, dbg_info, dbg_info_type_relocs),135 //.ve => return Function(.ve).generateSymbol(bin_file, src, typed_value, code, debug_output),
129 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."),136 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."),
130 }137 }
131 },138 },
...@@ -139,7 +146,7 @@ pub fn generateSymbol(...@@ -139,7 +146,7 @@ pub fn generateSymbol(
139 switch (try generateSymbol(bin_file, src, .{146 switch (try generateSymbol(bin_file, src, .{
140 .ty = typed_value.ty.elemType(),147 .ty = typed_value.ty.elemType(),
141 .val = sentinel,148 .val = sentinel,
142 }, code, dbg_line, dbg_info, dbg_info_type_relocs)) {149 }, code, debug_output)) {
143 .appended => return Result{ .appended = {} },150 .appended => return Result{ .appended = {} },
144 .externally_managed => |slice| {151 .externally_managed => |slice| {
145 code.appendSliceAssumeCapacity(slice);152 code.appendSliceAssumeCapacity(slice);
...@@ -239,9 +246,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {...@@ -239,9 +246,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
239 target: *const std.Target,246 target: *const std.Target,
240 mod_fn: *const Module.Fn,247 mod_fn: *const Module.Fn,
241 code: *std.ArrayList(u8),248 code: *std.ArrayList(u8),
242 dbg_line: *std.ArrayList(u8),249 debug_output: DebugInfoOutput,
243 dbg_info: *std.ArrayList(u8),
244 dbg_info_type_relocs: *link.File.DbgInfoTypeRelocsTable,
245 err_msg: ?*ErrorMsg,250 err_msg: ?*ErrorMsg,
246 args: []MCValue,251 args: []MCValue,
247 ret_mcv: MCValue,252 ret_mcv: MCValue,
...@@ -419,9 +424,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {...@@ -419,9 +424,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
419 src: usize,424 src: usize,
420 typed_value: TypedValue,425 typed_value: TypedValue,
421 code: *std.ArrayList(u8),426 code: *std.ArrayList(u8),
422 dbg_line: *std.ArrayList(u8),427 debug_output: DebugInfoOutput,
423 dbg_info: *std.ArrayList(u8),
424 dbg_info_type_relocs: *link.File.DbgInfoTypeRelocsTable,
425 ) GenerateSymbolError!Result {428 ) GenerateSymbolError!Result {
426 const module_fn = typed_value.val.cast(Value.Payload.Function).?.func;429 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 {...@@ -457,9 +460,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
457 .bin_file = bin_file,460 .bin_file = bin_file,
458 .mod_fn = module_fn,461 .mod_fn = module_fn,
459 .code = code,462 .code = code,
460 .dbg_line = dbg_line,463 .debug_output = debug_output,
461 .dbg_info = dbg_info,
462 .dbg_info_type_relocs = dbg_info_type_relocs,
463 .err_msg = null,464 .err_msg = null,
464 .args = undefined, // populated after `resolveCallingConventionValues`465 .args = undefined, // populated after `resolveCallingConventionValues`
465 .ret_mcv = undefined, // populated after `resolveCallingConventionValues`466 .ret_mcv = undefined, // populated after `resolveCallingConventionValues`
...@@ -598,35 +599,50 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {...@@ -598,35 +599,50 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
598 }599 }
599600
600 fn dbgSetPrologueEnd(self: *Self) InnerError!void {601 fn dbgSetPrologueEnd(self: *Self) InnerError!void {
601 try self.dbg_line.append(DW.LNS_set_prologue_end);602 switch (self.debug_output) {
602 try self.dbgAdvancePCAndLine(self.prev_di_src);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 }
603 }609 }
604610
605 fn dbgSetEpilogueBegin(self: *Self) InnerError!void {611 fn dbgSetEpilogueBegin(self: *Self) InnerError!void {
606 try self.dbg_line.append(DW.LNS_set_epilogue_begin);612 switch (self.debug_output) {
607 try self.dbgAdvancePCAndLine(self.prev_di_src);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 }
608 }619 }
609620
610 fn dbgAdvancePCAndLine(self: *Self, src: usize) InnerError!void {621 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;
617 self.prev_di_src = src;622 self.prev_di_src = src;
618 self.prev_di_pc = self.code.items.len;623 self.prev_di_pc = self.code.items.len;
619 // TODO Look into using the DWARF special opcodes to compress this data. It lets you emit624 switch (self.debug_output) {
620 // single-byte opcodes that add different numbers to both the PC and the line number625 .dwarf => |dbg_out| {
621 // at the same time.626 // TODO Look into improving the performance here by adding a token-index-to-line
622 try self.dbg_line.ensureCapacity(self.dbg_line.items.len + 11);627 // lookup table, and changing ir.Inst from storing byte offset to token. Currently
623 self.dbg_line.appendAssumeCapacity(DW.LNS_advance_pc);628 // this involves scanning over the source code for newlines
624 leb128.writeULEB128(self.dbg_line.writer(), delta_pc) catch unreachable;629 // (but only from the previous byte offset to the new one).
625 if (delta_line != 0) {630 const delta_line = std.zig.lineDelta(self.source, self.prev_di_src, src);
626 self.dbg_line.appendAssumeCapacity(DW.LNS_advance_line);631 const delta_pc = self.code.items.len - self.prev_di_pc;
627 leb128.writeILEB128(self.dbg_line.writer(), delta_line) catch unreachable;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 => {},
628 }645 }
629 self.dbg_line.appendAssumeCapacity(DW.LNS_copy);
630 }646 }
631647
632 /// Asserts there is already capacity to insert into top branch inst_table.648 /// 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 {...@@ -654,18 +670,23 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
654 /// Adds a Type to the .debug_info at the current position. The bytes will be populated later,670 /// Adds a Type to the .debug_info at the current position. The bytes will be populated later,
655 /// after codegen for this symbol is done.671 /// after codegen for this symbol is done.
656 fn addDbgInfoTypeReloc(self: *Self, ty: Type) !void {672 fn addDbgInfoTypeReloc(self: *Self, ty: Type) !void {
657 assert(ty.hasCodeGenBits());673 switch (self.debug_output) {
658 const index = self.dbg_info.items.len;674 .dwarf => |dbg_out| {
659 try self.dbg_info.resize(index + 4); // DW.AT_type, DW.FORM_ref4675 assert(ty.hasCodeGenBits());
660676 const index = dbg_out.dbg_info.items.len;
661 const gop = try self.dbg_info_type_relocs.getOrPut(self.gpa, ty);677 try dbg_out.dbg_info.resize(index + 4); // DW.AT_type, DW.FORM_ref4
662 if (!gop.found_existing) {678
663 gop.entry.value = .{679 const gop = try dbg_out.dbg_info_type_relocs.getOrPut(self.gpa, ty);
664 .off = undefined,680 if (!gop.found_existing) {
665 .relocs = .{},681 gop.entry.value = .{
666 };682 .off = undefined,
683 .relocs = .{},
684 };
685 }
686 try gop.entry.value.relocs.append(self.gpa, @intCast(u32, index));
687 },
688 .none => {},
667 }689 }
668 try gop.entry.value.relocs.append(self.gpa, @intCast(u32, index));
669 }690 }
670691
671 fn genFuncInst(self: *Self, inst: *ir.Inst) !MCValue {692 fn genFuncInst(self: *Self, inst: *ir.Inst) !MCValue {
...@@ -1258,14 +1279,19 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {...@@ -1258,14 +1279,19 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
1258 self.registers.putAssumeCapacityNoClobber(toCanonicalReg(reg), &inst.base);1279 self.registers.putAssumeCapacityNoClobber(toCanonicalReg(reg), &inst.base);
1259 self.markRegUsed(reg);1280 self.markRegUsed(reg);
12601281
1261 try self.dbg_info.ensureCapacity(self.dbg_info.items.len + 8 + name_with_null.len);1282 switch (self.debug_output) {
1262 self.dbg_info.appendAssumeCapacity(link.File.Elf.abbrev_parameter);1283 .dwarf => |dbg_out| {
1263 self.dbg_info.appendSliceAssumeCapacity(&[2]u8{ // DW.AT_location, DW.FORM_exprloc1284 try dbg_out.dbg_info.ensureCapacity(dbg_out.dbg_info.items.len + 8 + name_with_null.len);
1264 1, // ULEB128 dwarf expression length1285 dbg_out.dbg_info.appendAssumeCapacity(link.File.Elf.abbrev_parameter);
1265 reg.dwarfLocOp(),1286 dbg_out.dbg_info.appendSliceAssumeCapacity(&[2]u8{ // DW.AT_location, DW.FORM_exprloc
1266 });1287 1, // ULEB128 dwarf expression length
1267 try self.addDbgInfoTypeReloc(inst.base.ty); // DW.AT_type, DW.FORM_ref41288 reg.dwarfLocOp(),
1268 self.dbg_info.appendSliceAssumeCapacity(name_with_null); // DW.AT_name, DW.FORM_string1289 });
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 }
1269 },1295 },
1270 else => {},1296 else => {},
1271 }1297 }
...@@ -1302,7 +1328,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {...@@ -1302,7 +1328,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
13021328
1303 // Due to incremental compilation, how function calls are generated depends1329 // Due to incremental compilation, how function calls are generated depends
1304 // on linking.1330 // 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) {
1306 switch (arch) {1332 switch (arch) {
1307 .x86_64 => {1333 .x86_64 => {
1308 for (info.args) |mc_arg, arg_i| {1334 for (info.args) |mc_arg, arg_i| {
...@@ -1341,10 +1367,17 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {...@@ -1341,10 +1367,17 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
1341 if (inst.func.cast(ir.Inst.Constant)) |func_inst| {1367 if (inst.func.cast(ir.Inst.Constant)) |func_inst| {
1342 if (func_inst.val.cast(Value.Payload.Function)) |func_val| {1368 if (func_inst.val.cast(Value.Payload.Function)) |func_val| {
1343 const func = func_val.func;1369 const func = func_val.func;
1344 const got = &elf_file.program_headers.items[elf_file.phdr_got_index.?];1370
1345 const ptr_bits = self.target.cpu.arch.ptrBitWidth();1371 const ptr_bits = self.target.cpu.arch.ptrBitWidth();
1346 const ptr_bytes: u64 = @divExact(ptr_bits, 8);1372 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
1348 // ff 14 25 xx xx xx xx call [addr]1381 // ff 14 25 xx xx xx xx call [addr]
1349 try self.code.ensureCapacity(self.code.items.len + 7);1382 try self.code.ensureCapacity(self.code.items.len + 7);
1350 self.code.appendSliceAssumeCapacity(&[3]u8{ 0xff, 0x14, 0x25 });1383 self.code.appendSliceAssumeCapacity(&[3]u8{ 0xff, 0x14, 0x25 });
...@@ -1362,10 +1395,16 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {...@@ -1362,10 +1395,16 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
1362 if (inst.func.cast(ir.Inst.Constant)) |func_inst| {1395 if (inst.func.cast(ir.Inst.Constant)) |func_inst| {
1363 if (func_inst.val.cast(Value.Payload.Function)) |func_val| {1396 if (func_inst.val.cast(Value.Payload.Function)) |func_val| {
1364 const func = func_val.func;1397 const func = func_val.func;
1365 const got = &elf_file.program_headers.items[elf_file.phdr_got_index.?];1398
1366 const ptr_bits = self.target.cpu.arch.ptrBitWidth();1399 const ptr_bits = self.target.cpu.arch.ptrBitWidth();
1367 const ptr_bytes: u64 = @divExact(ptr_bits, 8);1400 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
1370 try self.genSetReg(inst.base.src, .ra, .{ .memory = got_addr });1409 try self.genSetReg(inst.base.src, .ra, .{ .memory = got_addr });
1371 mem.writeIntLittle(u32, try self.code.addManyAsArray(4), Instruction.jalr(.ra, 0, .ra).toU32());1410 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 {...@@ -1383,8 +1422,14 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
1383 }1422 }
1384 if (func_inst.val.cast(Value.Payload.Function)) |func_val| {1423 if (func_inst.val.cast(Value.Payload.Function)) |func_val| {
1385 const func = func_val.func;1424 const func = func_val.func;
1386 const got = &elf_file.program_headers.items[elf_file.phdr_got_index.?];1425 const got_addr = if (self.bin_file.cast(link.File.Elf)) |elf_file| blk: {
1387 const got_addr = @intCast(u16, got.p_vaddr + func.owner_decl.link.elf.offset_table_index * 2);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
1388 const return_type = func.owner_decl.typed_value.most_recent.typed_value.ty.fnReturnType();1433 const return_type = func.owner_decl.typed_value.most_recent.typed_value.ty.fnReturnType();
1389 // First, push the return address, then jump; if noreturn, don't bother with the first step1434 // First, push the return address, then jump; if noreturn, don't bother with the first step
1390 // TODO: implement packed struct -> u16 at comptime and move the bitcast here1435 // 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 {...@@ -1420,10 +1465,15 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
1420 if (inst.func.cast(ir.Inst.Constant)) |func_inst| {1465 if (inst.func.cast(ir.Inst.Constant)) |func_inst| {
1421 if (func_inst.val.cast(Value.Payload.Function)) |func_val| {1466 if (func_inst.val.cast(Value.Payload.Function)) |func_val| {
1422 const func = func_val.func;1467 const func = func_val.func;
1423 const got = &elf_file.program_headers.items[elf_file.phdr_got_index.?];
1424 const ptr_bits = self.target.cpu.arch.ptrBitWidth();1468 const ptr_bits = self.target.cpu.arch.ptrBitWidth();
1425 const ptr_bytes: u64 = @divExact(ptr_bits, 8);1469 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
1428 // TODO only works with leaf functions1478 // TODO only works with leaf functions
1429 // at the moment, which works fine for1479 // at the moment, which works fine for
...@@ -1983,7 +2033,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {...@@ -1983,7 +2033,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
19832033
1984 if (mem.eql(u8, inst.asm_source, "syscall")) {2034 if (mem.eql(u8, inst.asm_source, "syscall")) {
1985 try self.code.appendSlice(&[_]u8{ 0x0f, 0x05 });2035 try self.code.appendSlice(&[_]u8{ 0x0f, 0x05 });
1986 } else {2036 } else if (inst.asm_source.len != 0) {
1987 return self.fail(inst.base.src, "TODO implement support for more x86 assembly instructions", .{});2037 return self.fail(inst.base.src, "TODO implement support for more x86 assembly instructions", .{});
1988 }2038 }
19892039
...@@ -2541,6 +2591,10 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {...@@ -2541,6 +2591,10 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
2541 const got = &macho_file.sections.items[macho_file.got_section_index.?];2591 const got = &macho_file.sections.items[macho_file.got_section_index.?];
2542 const got_addr = got.addr + decl.link.macho.offset_table_index.? * ptr_bytes;2592 const got_addr = got.addr + decl.link.macho.offset_table_index.? * ptr_bytes;
2543 return MCValue{ .memory = got_addr };2593 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 };
2544 } else {2598 } else {
2545 return self.fail(src, "TODO codegen non-ELF const Decl pointer", .{});2599 return self.fail(src, "TODO codegen non-ELF const Decl pointer", .{});
2546 }2600 }
src-self-hosted/link.zig+2-2
...@@ -34,7 +34,7 @@ pub const File = struct {...@@ -34,7 +34,7 @@ pub const File = struct {
3434
35 pub const LinkBlock = union {35 pub const LinkBlock = union {
36 elf: Elf.TextBlock,36 elf: Elf.TextBlock,
37 coff: void, // @TODO37 coff: Coff.TextBlock,
38 macho: MachO.TextBlock,38 macho: MachO.TextBlock,
39 c: void,39 c: void,
40 wasm: void,40 wasm: void,
...@@ -42,7 +42,7 @@ pub const File = struct {...@@ -42,7 +42,7 @@ pub const File = struct {
4242
43 pub const LinkFn = union {43 pub const LinkFn = union {
44 elf: Elf.SrcFn,44 elf: Elf.SrcFn,
45 coff: void, // @TODO45 coff: Coff.SrcFn,
46 macho: MachO.SrcFn,46 macho: MachO.SrcFn,
47 c: void,47 c: void,
48 wasm: ?Wasm.FnData,48 wasm: ?Wasm.FnData,
src-self-hosted/link/Coff.zig+546-79
...@@ -6,10 +6,22 @@ const Allocator = std.mem.Allocator;...@@ -6,10 +6,22 @@ const Allocator = std.mem.Allocator;
6const assert = std.debug.assert;6const assert = std.debug.assert;
7const fs = std.fs;7const fs = std.fs;
88
9const trace = @import("../tracy.zig").trace;
9const Module = @import("../Module.zig");10const Module = @import("../Module.zig");
10const codegen = @import("../codegen/wasm.zig");11const codegen = @import("../codegen.zig");
11const link = @import("../link.zig");12const 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
13pub const base_tag: link.File.Tag = .coff;25pub const base_tag: link.File.Tag = .coff;
1426
15const msdos_stub = @embedFile("msdos-stub.bin");27const msdos_stub = @embedFile("msdos-stub.bin");
...@@ -18,8 +30,85 @@ base: link.File,...@@ -18,8 +30,85 @@ base: link.File,
18ptr_width: enum { p32, p64 },30ptr_width: enum { p32, p64 },
19error_flags: link.File.ErrorFlags = .{},31error_flags: link.File.ErrorFlags = .{},
2032
21coff_file_header_dirty: bool = false,33text_block_free_list: std.ArrayListUnmanaged(*TextBlock) = .{},
22optional_header_dirty: bool = false,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
24pub fn openPath(allocator: *Allocator, dir: fs.Dir, sub_path: []const u8, options: link.Options) !*link.File {113pub fn openPath(allocator: *Allocator, dir: fs.Dir, sub_path: []const u8, options: link.Options) !*link.File {
25 assert(options.object_format == .coff);114 assert(options.object_format == .coff);
...@@ -42,7 +131,7 @@ pub fn openPath(allocator: *Allocator, dir: fs.Dir, sub_path: []const u8, option...@@ -42,7 +131,7 @@ pub fn openPath(allocator: *Allocator, dir: fs.Dir, sub_path: []const u8, option
42fn openFile(allocator: *Allocator, file: fs.File, options: link.Options) !Coff {131fn openFile(allocator: *Allocator, file: fs.File, options: link.Options) !Coff {
43 switch (options.output_mode) {132 switch (options.output_mode) {
44 .Exe => {},133 .Exe => {},
45 .Obj => return error.IncrFailed, // @TODO DO OBJ FILES134 .Obj => return error.IncrFailed,
46 .Lib => return error.IncrFailed,135 .Lib => return error.IncrFailed,
47 }136 }
48 var self: Coff = .{137 var self: Coff = .{
...@@ -67,8 +156,10 @@ fn openFile(allocator: *Allocator, file: fs.File, options: link.Options) !Coff {...@@ -67,8 +156,10 @@ fn openFile(allocator: *Allocator, file: fs.File, options: link.Options) !Coff {
67/// Truncates the existing file contents and overwrites the contents.156/// Truncates the existing file contents and overwrites the contents.
68/// Returns an error if `file` is not already open with +read +write +seek abilities.157/// Returns an error if `file` is not already open with +read +write +seek abilities.
69fn createFile(allocator: *Allocator, file: fs.File, options: link.Options) !Coff {158fn createFile(allocator: *Allocator, file: fs.File, options: link.Options) !Coff {
159 // TODO Write object specific relocations, COFF symbol table, then enable object file output.
70 switch (options.output_mode) {160 switch (options.output_mode) {
71 .Exe, .Obj => {},161 .Exe => {},
162 .Obj => return error.TODOImplementWritingObjFiles,
72 .Lib => return error.TODOImplementWritingLibFiles,163 .Lib => return error.TODOImplementWritingLibFiles,
73 }164 }
74 var self: Coff = .{165 var self: Coff = .{
...@@ -83,8 +174,6 @@ fn createFile(allocator: *Allocator, file: fs.File, options: link.Options) !Coff...@@ -83,8 +174,6 @@ fn createFile(allocator: *Allocator, file: fs.File, options: link.Options) !Coff
83 64 => .p64,174 64 => .p64,
84 else => return error.UnsupportedCOFFArchitecture,175 else => return error.UnsupportedCOFFArchitecture,
85 },176 },
86 .coff_file_header_dirty = true,
87 .optional_header_dirty = true,
88 };177 };
89 errdefer self.deinit();178 errdefer self.deinit();
90179
...@@ -97,18 +186,14 @@ fn createFile(allocator: *Allocator, file: fs.File, options: link.Options) !Coff...@@ -97,18 +186,14 @@ fn createFile(allocator: *Allocator, file: fs.File, options: link.Options) !Coff
97186
98 // COFF file header187 // COFF file header
99 const data_directory_count = 0;188 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;
101 var index: usize = 0;190 var index: usize = 0;
102191
103 // @TODO Add an enum(u16) in std.coff, add .toCoffMachine to Arch192 const machine = self.base.options.target.cpu.arch.toCoffMachine();
104 const machine_type: u16 = switch (self.base.options.target.cpu.arch) {193 if (machine == .Unknown) {
105 .x86_64 => 0x8664,194 return error.UnsupportedCOFFArchitecture;
106 .i386 => 0x014c,195 }
107 .riscv32 => 0x5032,196 std.mem.writeIntLittle(u16, hdr_data[0..2], @enumToInt(machine));
108 .riscv64 => 0x5064,
109 else => return error.UnsupportedCOFFArchitecture,
110 };
111 std.mem.writeIntLittle(u16, hdr_data[0..2], machine_type);
112 index += 2;197 index += 2;
113198
114 // Number of sections (we only use .got, .text)199 // Number of sections (we only use .got, .text)
...@@ -125,20 +210,33 @@ fn createFile(allocator: *Allocator, file: fs.File, options: link.Options) !Coff...@@ -125,20 +210,33 @@ fn createFile(allocator: *Allocator, file: fs.File, options: link.Options) !Coff
125 },210 },
126 else => 0,211 else => 0,
127 };212 };
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
128 std.mem.writeIntLittle(u16, hdr_data[index..][0..2], optional_header_size);229 std.mem.writeIntLittle(u16, hdr_data[index..][0..2], optional_header_size);
129 index += 2;230 index += 2;
130231
131 // Characteristics - IMAGE_FILE_DEBUG_STRIPPED232 // Characteristics
132 var characteristics: u16 = 0x200; // TODO Remove debug info stripped flag when necessary233 var characteristics: u16 = std.coff.IMAGE_FILE_DEBUG_STRIPPED | std.coff.IMAGE_FILE_RELOCS_STRIPPED; // TODO Remove debug info stripped flag when necessary
133 if (options.output_mode == .Exe) {234 if (options.output_mode == .Exe) {
134 // IMAGE_FILE_EXECUTABLE_IMAGE235 characteristics |= std.coff.IMAGE_FILE_EXECUTABLE_IMAGE;
135 characteristics |= 0x2;
136 }236 }
137 switch (self.ptr_width) {237 switch (self.ptr_width) {
138 // IMAGE_FILE_32BIT_MACHINE238 .p32 => characteristics |= std.coff.IMAGE_FILE_32BIT_MACHINE,
139 .p32 => characteristics |= 0x100,239 .p64 => characteristics |= std.coff.IMAGE_FILE_LARGE_ADDRESS_AWARE,
140 // IMAGE_FILE_LARGE_ADDRESS_AWARE
141 .p64 => characteristics |= 0x20,
142 }240 }
143 std.mem.writeIntLittle(u16, hdr_data[index..][0..2], characteristics);241 std.mem.writeIntLittle(u16, hdr_data[index..][0..2], characteristics);
144 index += 2;242 index += 2;
...@@ -147,6 +245,7 @@ fn createFile(allocator: *Allocator, file: fs.File, options: link.Options) !Coff...@@ -147,6 +245,7 @@ fn createFile(allocator: *Allocator, file: fs.File, options: link.Options) !Coff
147 try self.base.file.?.pwriteAll(hdr_data[0..index], coff_file_header_offset);245 try self.base.file.?.pwriteAll(hdr_data[0..index], coff_file_header_offset);
148246
149 if (options.output_mode == .Exe) {247 if (options.output_mode == .Exe) {
248 self.optional_header_offset = coff_file_header_offset + 20;
150 // Optional header249 // Optional header
151 index = 0;250 index = 0;
152 std.mem.writeIntLittle(u16, hdr_data[0..2], switch (self.ptr_width) {251 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...@@ -155,34 +254,33 @@ fn createFile(allocator: *Allocator, file: fs.File, options: link.Options) !Coff
155 });254 });
156 index += 2;255 index += 2;
157256
158 // Linker version (u8 + u8), SizeOfCode (u32), SizeOfInitializedData (u32), SizeOfUninitializedData (u32), AddressOfEntryPoint (u32)257 // Linker version (u8 + u8)
159 std.mem.set(u8, hdr_data[index..][0..18], 0);258 std.mem.set(u8, hdr_data[index..][0..2], 0);
160 index += 18;259 index += 2;
161260
162 // Base of code relative to the image base261 // SizeOfCode (UNUSED, u32), SizeOfInitializedData (u32), SizeOfUninitializedData (u32), AddressOfEntryPoint (u32), BaseOfCode (UNUSED, u32)
163 // @TODO Check where to put this262 std.mem.set(u8, hdr_data[index..][0..20], 0);
164 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], 0x1000);263 index += 20;
165 index += 4;
166264
167 if (self.ptr_width == .p32) {265 if (self.ptr_width == .p32) {
168 // Base of data relative to the image base266 // Base of data relative to the image base (UNUSED)
169 std.mem.set(u8, hdr_data[index..][0..4], 0);267 std.mem.set(u8, hdr_data[index..][0..4], 0);
170 index += 4;268 index += 4;
171269
172 // Image base address270 // 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);
174 index += 4;272 index += 4;
175 } else {273 } else {
176 // Image base address274 // 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);
178 index += 8;276 index += 8;
179 }277 }
180278
181 // Section alignment279 // 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);
183 index += 4;281 index += 4;
184 // File alignment282 // 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);
186 index += 4;284 index += 4;
187 // Required OS version, 6.0 is vista285 // Required OS version, 6.0 is vista
188 std.mem.writeIntLittle(u16, hdr_data[index..][0..2], 6);286 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...@@ -197,19 +295,25 @@ fn createFile(allocator: *Allocator, file: fs.File, options: link.Options) !Coff
197 index += 2;295 index += 2;
198 std.mem.writeIntLittle(u16, hdr_data[index..][0..2], 0);296 std.mem.writeIntLittle(u16, hdr_data[index..][0..2], 0);
199 index += 2;297 index += 2;
200 // Reserved zeroes (u32), SizeOfImage (u32), SizeOfHeaders (u32), CheckSum (u32)298 // Reserved zeroes (u32)
201 std.mem.set(u8, hdr_data[index..][0..16], 0);299 std.mem.set(u8, hdr_data[index..][0..4], 0);
202 index += 16;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;
203 // Subsystem, TODO: Let users specify the subsystem, always CUI for now308 // Subsystem, TODO: Let users specify the subsystem, always CUI for now
204 std.mem.writeIntLittle(u16, hdr_data[index..][0..2], 3);309 std.mem.writeIntLittle(u16, hdr_data[index..][0..2], 3);
205 index += 2;310 index += 2;
206 // DLL characteristics, TODO: For now we are just using IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE311 // DLL characteristics
207 std.mem.writeIntLittle(u16, hdr_data[index..][0..2], 0x40);312 std.mem.writeIntLittle(u16, hdr_data[index..][0..2], 0x0);
208 index += 2;313 index += 2;
209314
210 switch (self.ptr_width) {315 switch (self.ptr_width) {
211 .p32 => {316 .p32 => {
212 // @TODO See llvm output for 32 bit executables
213 // Size of stack reserve + commit317 // Size of stack reserve + commit
214 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], 0x1_000_000);318 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], 0x1_000_000);
215 index += 4;319 index += 4;
...@@ -242,84 +346,447 @@ fn createFile(allocator: *Allocator, file: fs.File, options: link.Options) !Coff...@@ -242,84 +346,447 @@ fn createFile(allocator: *Allocator, file: fs.File, options: link.Options) !Coff
242 // Number of data directories346 // Number of data directories
243 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], data_directory_count);347 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], data_directory_count);
244 index += 4;348 index += 4;
245 // @TODO Write meaningful stuff here
246 // Initialize data directories to zero349 // 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);
248 index += data_directory_count * 8;351 index += data_directory_count * 8;
249352
250 assert(index == optional_header_size);353 assert(index == optional_header_size);
251 }354 }
252355
253 // @TODO Merge this write with the one above
254 const section_table_offset = coff_file_header_offset + 20 + optional_header_size;
255
256 // Write section table.356 // Write section table.
257 // First, the .got section357 // First, the .got section
258 hdr_data[index..][0..8].* = ".got\x00\x00\x00\x00".*;358 hdr_data[index..][0..8].* = ".got\x00\x00\x00\x00".*;
259 index += 8;359 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)360 if (options.output_mode == .Exe) {
261 std.mem.set(u8, hdr_data[index..][0..12], 0);361 // Virtual size (u32)
262 index += 12;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;
263 // File pointer to the start of the section374 // 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);
265 index += 4;376 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)
267 std.mem.set(u8, hdr_data[index..][0..12], 0);378 std.mem.set(u8, hdr_data[index..][0..12], 0);
268 index += 12;379 index += 12;
269 // Characteristics `IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ = 0x40000040`380 // Section flags
270 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], 0x40000040);381 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], std.coff.IMAGE_SCN_CNT_INITIALIZED_DATA | std.coff.IMAGE_SCN_MEM_READ);
271 index += 4;382 index += 4;
272 // Then, the .text section383 // Then, the .text section
273 hdr_data[index..][0..8].* = ".text\x00\x00\x00".*;384 hdr_data[index..][0..8].* = ".text\x00\x00\x00".*;
274 index += 8;385 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)386 if (options.output_mode == .Exe) {
276 std.mem.set(u8, hdr_data[index..][0..12], 0);387 // Virtual size (u32)
277 index += 12;388 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], default_size_of_code);
278 // File pointer to the start of the section (@TODO Add the initial size of .got)389 index += 4;
279 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], section_table_offset + 2 * 40);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);
280 index += 4;402 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)
282 std.mem.set(u8, hdr_data[index..][0..12], 0);404 std.mem.set(u8, hdr_data[index..][0..12], 0);
283 index += 12;405 index += 12;
284 // Characteristics `IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE = 0xE0000020`406 // Section flags
285 std.mem.writeIntLittle(u32, hdr_data[index..][0..4], 0xE0000020);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 );
286 index += 4;412 index += 4;
287413
288 assert(index == optional_header_size + 2 * 40);414 assert(index == optional_header_size + section_table_size);
289 try self.base.file.?.pwriteAll(hdr_data[0..index], coff_file_header_offset + 20);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
291 return self;418 return self;
292}419}
293420
294pub fn flush(self: *Coff, module: *Module) !void {421pub fn allocateDeclIndexes(self: *Coff, decl: *Module.Decl) !void {
295 // @TODO Implement this422 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;
296}437}
297438
298pub fn freeDecl(self: *Coff, decl: *Module.Decl) void {439fn allocateTextBlock(self: *Coff, text_block: *TextBlock, new_block_size: u64, alignment: u64) !u64 {
299 // @TODO Implement this440 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;
300}527}
301528
302pub fn updateDecl(self: *Coff, module: *Module, decl: *Module.Decl) !void {529fn growTextBlock(self: *Coff, text_block: *TextBlock, new_block_size: u64, alignment: u64) !u64 {
303 // @TODO Implement this530 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);
304}535}
305536
306pub fn updateDeclLineNumber(self: *Coff, module: *Module, decl: *Module.Decl) !void {537fn shrinkTextBlock(self: *Coff, text_block: *TextBlock, new_block_size: u64) void {
307 // @TODO Implement this538 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 }
308}542}
309543
310pub fn allocateDeclIndexes(self: *Coff, decl: *Module.Decl) !void {544fn freeTextBlock(self: *Coff, text_block: *TextBlock) void {
311 // @TODO Implement this545 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 {};
312}719}
313720
314pub fn updateDeclExports(self: *Coff, module: *Module, decl: *const Module.Decl, exports: []const *Module.Export) !void {721pub fn updateDeclExports(self: *Coff, module: *Module, decl: *const Module.Decl, exports: []const *Module.Export) !void {
315 // @TODO Implement this722 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 }
316}778}
317779
318pub fn getDeclVAddr(self: *Coff, decl: *const Module.Decl) u64 {780pub fn getDeclVAddr(self: *Coff, decl: *const Module.Decl) u64 {
319 // @TODO Implement this781 return self.text_section_virtual_address + decl.link.coff.text_offset;
320 return 0;782}
783
784pub fn updateDeclLineNumber(self: *Coff, module: *Module, decl: *Module.Decl) !void {
785 // TODO Implement this
321}786}
322787
323pub fn deinit(self: *Coff) void {788pub fn deinit(self: *Coff) void {
324 // @TODO789 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);
325}792}
src-self-hosted/link/Elf.zig+7-1
...@@ -1735,7 +1735,13 @@ pub fn updateDecl(self: *Elf, module: *Module, decl: *Module.Decl) !void {...@@ -1735,7 +1735,13 @@ pub fn updateDecl(self: *Elf, module: *Module, decl: *Module.Decl) !void {
1735 } else {1735 } else {
1736 // TODO implement .debug_info for global variables1736 // TODO implement .debug_info for global variables
1737 }1737 }
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 });
1739 const code = switch (res) {1745 const code = switch (res) {
1740 .externally_managed => |x| x,1746 .externally_managed => |x| x,
1741 .appended => code_buffer.items,1747 .appended => code_buffer.items,
src-self-hosted/main.zig+9-4
...@@ -524,17 +524,22 @@ fn buildOutputType(...@@ -524,17 +524,22 @@ fn buildOutputType(
524 try stderr.print("\nUnable to parse command: {}\n", .{@errorName(err)});524 try stderr.print("\nUnable to parse command: {}\n", .{@errorName(err)});
525 continue;525 continue;
526 }) |line| {526 }) |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")) {
528 if (output_mode == .Exe) {533 if (output_mode == .Exe) {
529 try module.makeBinFileWritable();534 try module.makeBinFileWritable();
530 }535 }
531 try updateModule(gpa, &module, zir_out_path);536 try updateModule(gpa, &module, zir_out_path);
532 } else if (mem.eql(u8, line, "exit")) {537 } else if (mem.eql(u8, actual_line, "exit")) {
533 break;538 break;
534 } else if (mem.eql(u8, line, "help")) {539 } else if (mem.eql(u8, actual_line, "help")) {
535 try stderr.writeAll(repl_help);540 try stderr.writeAll(repl_help);
536 } else {541 } else {
537 try stderr.print("unknown command: {}\n", .{line});542 try stderr.print("unknown command: {}\n", .{actual_line});
538 }543 }
539 } else {544 } else {
540 break;545 break;