authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-09-14 10:42:29-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-09-14 10:42:29-07:00
log04f6a26955bbb947983cbf3e0681d9092aaaa13f
tree000d28126a84a4e191305f03f9ce2319789fc0c7
parentc58e9951effddacfd5cef6c05019bc3f312747f8

fix stage1 regressions in this branch

also prepare for supporting linking into archives

9 files changed, 64 insertions(+), 28 deletions(-)

src-self-hosted/Compilation.zig+8-1
......@@ -305,6 +305,8 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
305305 options.system_libs.len != 0 or
306306 options.link_libc or options.link_libcpp or
307307 options.link_eh_frame_hdr or
308 options.output_mode == .Lib or
309 options.lld_argv.len != 0 or
308310 options.linker_script != null or options.version_script != null)
309311 {
310312 break :blk true;
......@@ -320,12 +322,17 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
320322 break :blk false;
321323 };
322324
325 const is_exe_or_dyn_lib = switch (options.output_mode) {
326 .Obj => false,
327 .Lib => (options.link_mode orelse .Static) == .Dynamic,
328 .Exe => true,
329 };
323330 const must_dynamic_link = dl: {
324331 if (target_util.cannotDynamicLink(options.target))
325332 break :dl false;
326333 if (target_util.osRequiresLibC(options.target))
327334 break :dl true;
328 if (options.link_libc and options.target.isGnuLibC())
335 if (is_exe_or_dyn_lib and options.link_libc and options.target.isGnuLibC())
329336 break :dl true;
330337 if (options.system_libs.len != 0)
331338 break :dl true;
src-self-hosted/link.zig+24-15
......@@ -279,16 +279,19 @@ pub const File = struct {
279279 }
280280
281281 pub fn flush(base: *File, comp: *Compilation) !void {
282 const tracy = trace(@src());
283 defer tracy.end();
284
285 try switch (base.tag) {
286 .coff => @fieldParentPtr(Coff, "base", base).flush(comp),
287 .elf => @fieldParentPtr(Elf, "base", base).flush(comp),
288 .macho => @fieldParentPtr(MachO, "base", base).flush(comp),
289 .c => @fieldParentPtr(C, "base", base).flush(comp),
290 .wasm => @fieldParentPtr(Wasm, "base", base).flush(comp),
291 };
282 const use_lld = build_options.have_llvm and base.options.use_lld;
283 if (base.options.output_mode == .Lib and base.options.link_mode == .Static and
284 !base.options.target.isWasm())
285 {
286 return base.linkAsArchive(comp);
287 }
288 switch (base.tag) {
289 .coff => return @fieldParentPtr(Coff, "base", base).flush(comp),
290 .elf => return @fieldParentPtr(Elf, "base", base).flush(comp),
291 .macho => return @fieldParentPtr(MachO, "base", base).flush(comp),
292 .c => return @fieldParentPtr(C, "base", base).flush(comp),
293 .wasm => return @fieldParentPtr(Wasm, "base", base).flush(comp),
294 }
292295 }
293296
294297 pub fn freeDecl(base: *File, decl: *Module.Decl) void {
......@@ -302,13 +305,13 @@ pub const File = struct {
302305 }
303306
304307 pub fn errorFlags(base: *File) ErrorFlags {
305 return switch (base.tag) {
306 .coff => @fieldParentPtr(Coff, "base", base).error_flags,
307 .elf => @fieldParentPtr(Elf, "base", base).error_flags,
308 .macho => @fieldParentPtr(MachO, "base", base).error_flags,
308 switch (base.tag) {
309 .coff => return @fieldParentPtr(Coff, "base", base).error_flags,
310 .elf => return @fieldParentPtr(Elf, "base", base).error_flags,
311 .macho => return @fieldParentPtr(MachO, "base", base).error_flags,
309312 .c => return .{ .no_entry_point_found = false },
310313 .wasm => return ErrorFlags{},
311 };
314 }
312315 }
313316
314317 /// May be called before or after updateDecl, but must be called after
......@@ -338,6 +341,12 @@ pub const File = struct {
338341 }
339342 }
340343
344 fn linkAsArchive(base: *File, comp: *Compilation) !void {
345 // TODO follow pattern from ELF linkWithLLD
346 // ZigLLVMWriteArchive
347 return error.TODOMakeArchive;
348 }
349
341350 pub const Tag = enum {
342351 coff,
343352 elf,
src-self-hosted/link/C.zig+4
......@@ -7,6 +7,7 @@ const Compilation = @import("../Compilation.zig");
77const fs = std.fs;
88const codegen = @import("../codegen/c.zig");
99const link = @import("../link.zig");
10const trace = @import("../tracy.zig").trace;
1011const File = link.File;
1112const C = @This();
1213
......@@ -73,6 +74,9 @@ pub fn updateDecl(self: *C, module: *Module, decl: *Module.Decl) !void {
7374}
7475
7576pub fn flush(self: *C, comp: *Compilation) !void {
77 const tracy = trace(@src());
78 defer tracy.end();
79
7680 const writer = self.base.file.?.writer();
7781 try writer.writeAll(@embedFile("cbe.h"));
7882 var includes = false;
src-self-hosted/link/Coff.zig+3
......@@ -724,6 +724,9 @@ pub fn updateDeclExports(self: *Coff, module: *Module, decl: *const Module.Decl,
724724}
725725
726726pub fn flush(self: *Coff, comp: *Compilation) !void {
727 const tracy = trace(@src());
728 defer tracy.end();
729
727730 if (self.text_section_size_dirty) {
728731 // Write the new raw size in the .text header
729732 var buf: [4]u8 = undefined;
src-self-hosted/link/Elf.zig+8-9
......@@ -725,6 +725,9 @@ pub fn flush(self: *Elf, comp: *Compilation) !void {
725725
726726/// Commit pending changes and write headers.
727727fn flushInner(self: *Elf, comp: *Compilation) !void {
728 const tracy = trace(@src());
729 defer tracy.end();
730
728731 // TODO This linker code currently assumes there is only 1 compilation unit and it corresponds to the
729732 // Zig source code.
730733 const module = self.base.options.module orelse return error.LinkingWithoutZigSourceUnimplemented;
......@@ -1206,6 +1209,9 @@ fn flushInner(self: *Elf, comp: *Compilation) !void {
12061209}
12071210
12081211fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
1212 const tracy = trace(@src());
1213 defer tracy.end();
1214
12091215 var arena_allocator = std.heap.ArenaAllocator.init(self.base.allocator);
12101216 defer arena_allocator.deinit();
12111217 const arena = &arena_allocator.allocator;
......@@ -1315,13 +1321,6 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
13151321 if (is_obj) {
13161322 try argv.append("-r");
13171323 }
1318 if (self.base.options.output_mode == .Lib and
1319 self.base.options.link_mode == .Static and
1320 !target.isWasm())
1321 {
1322 // TODO port the code from link.cpp
1323 return error.TODOMakeArchive;
1324 }
13251324 const link_in_crt = self.base.options.link_libc and self.base.options.output_mode == .Exe;
13261325
13271326 try argv.append("-error-limit=0");
......@@ -1581,8 +1580,8 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
15811580 new_argv[i] = try arena.dupeZ(u8, arg);
15821581 }
15831582
1584 const ZigLLDLink = @import("../llvm.zig").ZigLLDLink;
1585 const ok = ZigLLDLink(.ELF, new_argv.ptr, new_argv.len, append_diagnostic, 0, 0);
1583 const llvm = @import("../llvm.zig");
1584 const ok = llvm.Link(.ELF, new_argv.ptr, new_argv.len, append_diagnostic, 0, 0);
15861585 if (!ok) return error.LLDReportedFailure;
15871586
15881587 // Update the dangling symlink "id.txt" with the digest. If it fails we can continue; it only
src-self-hosted/link/MachO.zig+3
......@@ -178,6 +178,9 @@ pub fn createEmpty(gpa: *Allocator, options: link.Options) !*MachO {
178178}
179179
180180pub fn flush(self: *MachO, comp: *Compilation) !void {
181 const tracy = trace(@src());
182 defer tracy.end();
183
181184 switch (self.base.options.output_mode) {
182185 .Exe => {
183186 var last_cmd_offset: usize = @sizeOf(macho.mach_header_64);
src-self-hosted/link/Wasm.zig+4
......@@ -10,6 +10,7 @@ const Module = @import("../Module.zig");
1010const Compilation = @import("../Compilation.zig");
1111const codegen = @import("../codegen/wasm.zig");
1212const link = @import("../link.zig");
13const trace = @import("../tracy.zig").trace;
1314
1415/// Various magic numbers defined by the wasm spec
1516const spec = struct {
......@@ -134,6 +135,9 @@ pub fn freeDecl(self: *Wasm, decl: *Module.Decl) void {
134135}
135136
136137pub fn flush(self: *Wasm, comp: *Compilation) !void {
138 const tracy = trace(@src());
139 defer tracy.end();
140
137141 const file = self.base.file.?;
138142 const header_size = 5 + 1;
139143
src-self-hosted/llvm.zig+9-2
......@@ -1,8 +1,9 @@
11//! We do this instead of @cImport because the self-hosted compiler is easier
22//! to bootstrap if it does not depend on translate-c.
33
4pub const Link = ZigLLDLink;
45pub extern fn ZigLLDLink(
5 oformat: ZigLLVM_ObjectFormatType,
6 oformat: ObjectFormatType,
67 args: [*:null]const ?[*:0]const u8,
78 arg_count: usize,
89 append_diagnostic: fn (context: usize, ptr: [*]const u8, len: usize) callconv(.C) void,
......@@ -10,7 +11,7 @@ pub extern fn ZigLLDLink(
1011 context_stderr: usize,
1112) bool;
1213
13pub const ZigLLVM_ObjectFormatType = extern enum(c_int) {
14pub const ObjectFormatType = extern enum(c_int) {
1415 Unknown,
1516 COFF,
1617 ELF,
......@@ -18,3 +19,9 @@ pub const ZigLLVM_ObjectFormatType = extern enum(c_int) {
1819 Wasm,
1920 XCOFF,
2021};
22
23pub const GetHostCPUName = LLVMGetHostCPUName;
24extern fn LLVMGetHostCPUName() ?[*:0]u8;
25
26pub const GetNativeFeatures = ZigLLVMGetNativeFeatures;
27extern fn ZigLLVMGetNativeFeatures() ?[*:0]u8;
src/codegen.cpp+1-1
......@@ -9013,7 +9013,7 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
90139013 if (g->zig_target->os_builtin_str != nullptr) {
90149014 buf_append_str(contents, g->zig_target->os_builtin_str);
90159015 } else {
9016 buf_appendf(contents, "Target.Os.defaultVersionRange(.%s);\n", cur_os);
9016 buf_appendf(contents, "Target.Os.Tag.defaultVersionRange(.%s);\n", cur_os);
90179017 }
90189018 }
90199019 buf_appendf(contents, "pub const object_format = ObjectFormat.%s;\n", cur_obj_fmt);