| ... | @@ -1,10 +1,12 @@ | ... | @@ -1,10 +1,12 @@ |
| 1 | const Wasm = @This(); | 1 | const Wasm = @This(); |
| 2 | | 2 | |
| 3 | const std = @import("std"); | 3 | const std = @import("std"); |
| | 4 | const mem = std.mem; |
| 4 | const Allocator = std.mem.Allocator; | 5 | const Allocator = std.mem.Allocator; |
| 5 | const assert = std.debug.assert; | 6 | const assert = std.debug.assert; |
| 6 | const fs = std.fs; | 7 | const fs = std.fs; |
| 7 | const leb = std.debug.leb; | 8 | const leb = std.debug.leb; |
| | 9 | const log = std.log.scoped(.link); |
| 8 | | 10 | |
| 9 | const Module = @import("../Module.zig"); | 11 | const Module = @import("../Module.zig"); |
| 10 | const Compilation = @import("../Compilation.zig"); | 12 | const Compilation = @import("../Compilation.zig"); |
| ... | @@ -12,6 +14,7 @@ const codegen = @import("../codegen/wasm.zig"); | ... | @@ -12,6 +14,7 @@ const codegen = @import("../codegen/wasm.zig"); |
| 12 | const link = @import("../link.zig"); | 14 | const link = @import("../link.zig"); |
| 13 | const trace = @import("../tracy.zig").trace; | 15 | const trace = @import("../tracy.zig").trace; |
| 14 | const build_options = @import("build_options"); | 16 | const build_options = @import("build_options"); |
| | 17 | const Cache = @import("../Cache.zig"); |
| 15 | | 18 | |
| 16 | /// Various magic numbers defined by the wasm spec | 19 | /// Various magic numbers defined by the wasm spec |
| 17 | const spec = struct { | 20 | const spec = struct { |
| ... | @@ -137,7 +140,7 @@ pub fn freeDecl(self: *Wasm, decl: *Module.Decl) void { | ... | @@ -137,7 +140,7 @@ pub fn freeDecl(self: *Wasm, decl: *Module.Decl) void { |
| 137 | | 140 | |
| 138 | pub fn flush(self: *Wasm, comp: *Compilation) !void { | 141 | pub fn flush(self: *Wasm, comp: *Compilation) !void { |
| 139 | if (build_options.have_llvm and self.base.options.use_lld) { | 142 | if (build_options.have_llvm and self.base.options.use_lld) { |
| 140 | return error.WasmLinkingWithLLDUnimplemented; | 143 | return self.linkWithLLD(comp); |
| 141 | } else { | 144 | } else { |
| 142 | return self.flushModule(comp); | 145 | return self.flushModule(comp); |
| 143 | } | 146 | } |
| ... | @@ -248,6 +251,211 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { | ... | @@ -248,6 +251,211 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void { |
| 248 | } | 251 | } |
| 249 | } | 252 | } |
| 250 | | 253 | |
| | 254 | fn linkWithLLD(self: *Wasm, comp: *Compilation) !void { |
| | 255 | const tracy = trace(@src()); |
| | 256 | defer tracy.end(); |
| | 257 | |
| | 258 | var arena_allocator = std.heap.ArenaAllocator.init(self.base.allocator); |
| | 259 | defer arena_allocator.deinit(); |
| | 260 | const arena = &arena_allocator.allocator; |
| | 261 | |
| | 262 | const directory = self.base.options.emit.?.directory; // Just an alias to make it shorter to type. |
| | 263 | |
| | 264 | // If there is no Zig code to compile, then we should skip flushing the output file because it |
| | 265 | // will not be part of the linker line anyway. |
| | 266 | const module_obj_path: ?[]const u8 = if (self.base.options.module) |module| blk: { |
| | 267 | const use_stage1 = build_options.is_stage1 and self.base.options.use_llvm; |
| | 268 | if (use_stage1) { |
| | 269 | const obj_basename = try std.zig.binNameAlloc(arena, .{ |
| | 270 | .root_name = self.base.options.root_name, |
| | 271 | .target = self.base.options.target, |
| | 272 | .output_mode = .Obj, |
| | 273 | }); |
| | 274 | const o_directory = self.base.options.module.?.zig_cache_artifact_directory; |
| | 275 | const full_obj_path = try o_directory.join(arena, &[_][]const u8{obj_basename}); |
| | 276 | break :blk full_obj_path; |
| | 277 | } |
| | 278 | |
| | 279 | try self.flushModule(comp); |
| | 280 | const obj_basename = self.base.intermediary_basename.?; |
| | 281 | const full_obj_path = try directory.join(arena, &[_][]const u8{obj_basename}); |
| | 282 | break :blk full_obj_path; |
| | 283 | } else null; |
| | 284 | |
| | 285 | const target = self.base.options.target; |
| | 286 | |
| | 287 | const id_symlink_basename = "lld.id"; |
| | 288 | |
| | 289 | var man: Cache.Manifest = undefined; |
| | 290 | defer if (!self.base.options.disable_lld_caching) man.deinit(); |
| | 291 | |
| | 292 | var digest: [Cache.hex_digest_len]u8 = undefined; |
| | 293 | |
| | 294 | if (!self.base.options.disable_lld_caching) { |
| | 295 | man = comp.cache_parent.obtain(); |
| | 296 | |
| | 297 | // We are about to obtain this lock, so here we give other processes a chance first. |
| | 298 | self.base.releaseLock(); |
| | 299 | |
| | 300 | try man.addListOfFiles(self.base.options.objects); |
| | 301 | for (comp.c_object_table.items()) |entry| { |
| | 302 | _ = try man.addFile(entry.key.status.success.object_path, null); |
| | 303 | } |
| | 304 | try man.addOptionalFile(module_obj_path); |
| | 305 | man.hash.addOptional(self.base.options.stack_size_override); |
| | 306 | man.hash.addListOfBytes(self.base.options.extra_lld_args); |
| | 307 | |
| | 308 | // We don't actually care whether it's a cache hit or miss; we just need the digest and the lock. |
| | 309 | _ = try man.hit(); |
| | 310 | digest = man.final(); |
| | 311 | |
| | 312 | var prev_digest_buf: [digest.len]u8 = undefined; |
| | 313 | const prev_digest: []u8 = directory.handle.readLink(id_symlink_basename, &prev_digest_buf) catch |err| blk: { |
| | 314 | log.debug("WASM LLD new_digest={} readlink error: {}", .{ digest, @errorName(err) }); |
| | 315 | // Handle this as a cache miss. |
| | 316 | break :blk prev_digest_buf[0..0]; |
| | 317 | }; |
| | 318 | if (mem.eql(u8, prev_digest, &digest)) { |
| | 319 | log.debug("WASM LLD digest={} match - skipping invocation", .{digest}); |
| | 320 | // Hot diggity dog! The output binary is already there. |
| | 321 | self.base.lock = man.toOwnedLock(); |
| | 322 | return; |
| | 323 | } |
| | 324 | log.debug("WASM LLD prev_digest={} new_digest={}", .{ prev_digest, digest }); |
| | 325 | |
| | 326 | // We are about to change the output file to be different, so we invalidate the build hash now. |
| | 327 | directory.handle.deleteFile(id_symlink_basename) catch |err| switch (err) { |
| | 328 | error.FileNotFound => {}, |
| | 329 | else => |e| return e, |
| | 330 | }; |
| | 331 | } |
| | 332 | |
| | 333 | const is_obj = self.base.options.output_mode == .Obj; |
| | 334 | |
| | 335 | // Create an LLD command line and invoke it. |
| | 336 | var argv = std.ArrayList([]const u8).init(self.base.allocator); |
| | 337 | defer argv.deinit(); |
| | 338 | // Even though we're calling LLD as a library it thinks the first argument is its own exe name. |
| | 339 | try argv.append("lld"); |
| | 340 | if (is_obj) { |
| | 341 | try argv.append("-r"); |
| | 342 | } |
| | 343 | |
| | 344 | try argv.append("-error-limit=0"); |
| | 345 | |
| | 346 | if (self.base.options.output_mode == .Exe) { |
| | 347 | // Increase the default stack size to a more reasonable value of 1MB instead of |
| | 348 | // the default of 1 Wasm page being 64KB, unless overriden by the user. |
| | 349 | try argv.append("-z"); |
| | 350 | const stack_size = self.base.options.stack_size_override orelse 1048576; |
| | 351 | const arg = try std.fmt.allocPrint(arena, "stack-size={d}", .{stack_size}); |
| | 352 | try argv.append(arg); |
| | 353 | |
| | 354 | // Put stack before globals so that stack overflow results in segfault immediately |
| | 355 | // before corrupting globals. See https://github.com/ziglang/zig/issues/4496 |
| | 356 | try argv.append("--stack-first"); |
| | 357 | } else { |
| | 358 | try argv.append("--no-entry"); // So lld doesn't look for _start. |
| | 359 | try argv.append("--export-all"); |
| | 360 | } |
| | 361 | try argv.appendSlice(&[_][]const u8{ |
| | 362 | "--allow-undefined", |
| | 363 | "-o", |
| | 364 | try directory.join(arena, &[_][]const u8{self.base.options.emit.?.sub_path}), |
| | 365 | }); |
| | 366 | |
| | 367 | // Positional arguments to the linker such as object files. |
| | 368 | try argv.appendSlice(self.base.options.objects); |
| | 369 | |
| | 370 | for (comp.c_object_table.items()) |entry| { |
| | 371 | try argv.append(entry.key.status.success.object_path); |
| | 372 | } |
| | 373 | if (module_obj_path) |p| { |
| | 374 | try argv.append(p); |
| | 375 | } |
| | 376 | |
| | 377 | if (self.base.options.output_mode == .Exe and !self.base.options.is_compiler_rt_or_libc) { |
| | 378 | if (!self.base.options.link_libc) { |
| | 379 | try argv.append(comp.libc_static_lib.?.full_object_path); |
| | 380 | } |
| | 381 | try argv.append(comp.compiler_rt_static_lib.?.full_object_path); |
| | 382 | } |
| | 383 | |
| | 384 | if (self.base.options.verbose_link) { |
| | 385 | Compilation.dump_argv(argv.items); |
| | 386 | } |
| | 387 | |
| | 388 | // TODO allocSentinel crashed stage1 so this is working around it. |
| | 389 | const new_argv_with_sentinel = try arena.alloc(?[*:0]const u8, argv.items.len + 1); |
| | 390 | new_argv_with_sentinel[argv.items.len] = null; |
| | 391 | const new_argv = new_argv_with_sentinel[0..argv.items.len :null]; |
| | 392 | for (argv.items) |arg, i| { |
| | 393 | new_argv[i] = try arena.dupeZ(u8, arg); |
| | 394 | } |
| | 395 | |
| | 396 | var stderr_context: LLDContext = .{ |
| | 397 | .wasm = self, |
| | 398 | .data = std.ArrayList(u8).init(self.base.allocator), |
| | 399 | }; |
| | 400 | defer stderr_context.data.deinit(); |
| | 401 | var stdout_context: LLDContext = .{ |
| | 402 | .wasm = self, |
| | 403 | .data = std.ArrayList(u8).init(self.base.allocator), |
| | 404 | }; |
| | 405 | defer stdout_context.data.deinit(); |
| | 406 | const llvm = @import("../llvm.zig"); |
| | 407 | const ok = llvm.Link( |
| | 408 | .Wasm, |
| | 409 | new_argv.ptr, |
| | 410 | new_argv.len, |
| | 411 | append_diagnostic, |
| | 412 | @ptrToInt(&stdout_context), |
| | 413 | @ptrToInt(&stderr_context), |
| | 414 | ); |
| | 415 | if (stderr_context.oom or stdout_context.oom) return error.OutOfMemory; |
| | 416 | if (stdout_context.data.items.len != 0) { |
| | 417 | std.log.warn("unexpected LLD stdout: {}", .{stdout_context.data.items}); |
| | 418 | } |
| | 419 | if (!ok) { |
| | 420 | // TODO parse this output and surface with the Compilation API rather than |
| | 421 | // directly outputting to stderr here. |
| | 422 | std.debug.print("{}", .{stderr_context.data.items}); |
| | 423 | return error.LLDReportedFailure; |
| | 424 | } |
| | 425 | if (stderr_context.data.items.len != 0) { |
| | 426 | std.log.warn("unexpected LLD stderr: {}", .{stderr_context.data.items}); |
| | 427 | } |
| | 428 | |
| | 429 | if (!self.base.options.disable_lld_caching) { |
| | 430 | // Update the dangling symlink with the digest. If it fails we can continue; it only |
| | 431 | // means that the next invocation will have an unnecessary cache miss. |
| | 432 | directory.handle.symLink(&digest, id_symlink_basename, .{}) catch |err| { |
| | 433 | std.log.warn("failed to save linking hash digest symlink: {}", .{@errorName(err)}); |
| | 434 | }; |
| | 435 | // Again failure here only means an unnecessary cache miss. |
| | 436 | man.writeManifest() catch |err| { |
| | 437 | std.log.warn("failed to write cache manifest when linking: {}", .{@errorName(err)}); |
| | 438 | }; |
| | 439 | // We hang on to this lock so that the output file path can be used without |
| | 440 | // other processes clobbering it. |
| | 441 | self.base.lock = man.toOwnedLock(); |
| | 442 | } |
| | 443 | } |
| | 444 | |
| | 445 | const LLDContext = struct { |
| | 446 | data: std.ArrayList(u8), |
| | 447 | wasm: *Wasm, |
| | 448 | oom: bool = false, |
| | 449 | }; |
| | 450 | |
| | 451 | fn append_diagnostic(context: usize, ptr: [*]const u8, len: usize) callconv(.C) void { |
| | 452 | const lld_context = @intToPtr(*LLDContext, context); |
| | 453 | const msg = ptr[0..len]; |
| | 454 | lld_context.data.appendSlice(msg) catch |err| switch (err) { |
| | 455 | error.OutOfMemory => lld_context.oom = true, |
| | 456 | }; |
| | 457 | } |
| | 458 | |
| 251 | /// Get the current index of a given Decl in the function list | 459 | /// Get the current index of a given Decl in the function list |
| 252 | /// TODO: we could maintain a hash map to potentially make this | 460 | /// TODO: we could maintain a hash map to potentially make this |
| 253 | fn getFuncidx(self: Wasm, decl: *Module.Decl) ?u32 { | 461 | fn getFuncidx(self: Wasm, decl: *Module.Decl) ?u32 { |