authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-12-16 00:39:36-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-01 17:51:20-07:00
log2047a6b82d2e6cdbddde3e7f1c93df9e72216052
treec9717626d48bc85a0e5dbe12bb2d815241ac9c94
parent46297087871dec88c2b632d057f1e55b662126df

fix remaining compile errors except one


28 files changed, 340 insertions(+), 341 deletions(-)

src/Compilation.zig+4-4
......@@ -229,7 +229,7 @@ pub const Emit = struct {
229229
230230 /// Returns the full path to `basename` if it were in the same directory as the
231231 /// `Emit` sub_path.
232 pub fn basenamePath(emit: Emit, arena: Allocator, basename: [:0]const u8) ![:0]const u8 {
232 pub fn basenamePath(emit: Emit, arena: Allocator, basename: []const u8) ![:0]const u8 {
233233 const full_path = if (emit.directory.path) |p|
234234 try std.fs.path.join(arena, &[_][]const u8{ p, emit.sub_path })
235235 else
......@@ -238,7 +238,7 @@ pub const Emit = struct {
238238 if (std.fs.path.dirname(full_path)) |dirname| {
239239 return try std.fs.path.joinZ(arena, &.{ dirname, basename });
240240 } else {
241 return basename;
241 return try arena.dupeZ(u8, basename);
242242 }
243243 }
244244};
......@@ -1038,8 +1038,8 @@ pub const InitOptions = struct {
10381038 linker_compress_debug_sections: ?link.File.Elf.CompressDebugSections = null,
10391039 linker_module_definition_file: ?[]const u8 = null,
10401040 linker_sort_section: ?link.File.Elf.SortSection = null,
1041 major_subsystem_version: ?u32 = null,
1042 minor_subsystem_version: ?u32 = null,
1041 major_subsystem_version: ?u16 = null,
1042 minor_subsystem_version: ?u16 = null,
10431043 clang_passthrough_mode: bool = false,
10441044 verbose_cc: bool = false,
10451045 verbose_link: bool = false,
src/Module.zig+3-8
......@@ -4309,14 +4309,9 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) Allocator.Err
43094309 decl.has_linksection_or_addrspace = has_linksection_or_addrspace;
43104310 decl.zir_decl_index = @enumFromInt(decl_sub_index);
43114311 if (decl.getOwnedFunction(mod) != null) {
4312 switch (comp.bin_file.tag) {
4313 .coff, .elf, .macho, .plan9 => {
4314 // TODO Look into detecting when this would be unnecessary by storing enough state
4315 // in `Decl` to notice that the line number did not change.
4316 comp.work_queue.writeItemAssumeCapacity(.{ .update_line_number = decl_index });
4317 },
4318 .c, .wasm, .spirv, .nvptx => {},
4319 }
4312 // TODO Look into detecting when this would be unnecessary by storing enough state
4313 // in `Decl` to notice that the line number did not change.
4314 comp.work_queue.writeItemAssumeCapacity(.{ .update_line_number = decl_index });
43204315 }
43214316}
43224317
src/Sema.zig+1-1
......@@ -20043,7 +20043,7 @@ fn getErrorReturnTrace(sema: *Sema, block: *Block) CompileError!Air.Inst.Ref {
2004320043
2004420044 if (sema.owner_func_index != .none and
2004520045 ip.funcAnalysis(sema.owner_func_index).calls_or_awaits_errorable_fn and
20046 mod.ownerModule().error_tracing and
20046 block.ownerModule().error_tracing and
2004720047 mod.backendSupportsFeature(.error_return_trace))
2004820048 {
2004920049 return block.addTy(.err_return_trace, opt_ptr_stack_trace_ty);
src/arch/x86_64/CodeGen.zig+28-18
......@@ -800,19 +800,20 @@ pub fn generate(
800800) CodeGenError!Result {
801801 const comp = bin_file.comp;
802802 const gpa = comp.gpa;
803 const mod = comp.module.?;
804 const func = mod.funcInfo(func_index);
805 const fn_owner_decl = mod.declPtr(func.owner_decl);
803 const zcu = comp.module.?;
804 const func = zcu.funcInfo(func_index);
805 const fn_owner_decl = zcu.declPtr(func.owner_decl);
806806 assert(fn_owner_decl.has_tv);
807807 const fn_type = fn_owner_decl.ty;
808 const namespace = mod.namespacePtr(fn_owner_decl.src_namespace);
809 const target = namespace.file_scope.mod.resolved_target.result;
808 const namespace = zcu.namespacePtr(fn_owner_decl.src_namespace);
809 const mod = namespace.file_scope.mod;
810810
811811 var function = Self{
812812 .gpa = gpa,
813813 .air = air,
814814 .liveness = liveness,
815 .target = target,
815 .target = &mod.resolved_target.result,
816 .mod = mod,
816817 .bin_file = bin_file,
817818 .debug_output = debug_output,
818819 .owner = .{ .func_index = func_index },
......@@ -843,7 +844,7 @@ pub fn generate(
843844
844845 wip_mir_log.debug("{}:", .{function.fmtDecl(func.owner_decl)});
845846
846 const ip = &mod.intern_pool;
847 const ip = &zcu.intern_pool;
847848
848849 try function.frame_allocs.resize(gpa, FrameIndex.named_count);
849850 function.frame_allocs.set(
......@@ -858,7 +859,7 @@ pub fn generate(
858859 FrameAlloc.init(.{ .size = 0, .alignment = .@"1" }),
859860 );
860861
861 const fn_info = mod.typeToFunc(fn_type).?;
862 const fn_info = zcu.typeToFunc(fn_type).?;
862863 const cc = abi.resolveCallingConvention(fn_info.cc, function.target.*);
863864 var call_info = function.resolveCallingConventionValues(fn_info, &.{}, .args_frame) catch |err| switch (err) {
864865 error.CodegenFail => return Result{ .fail = function.err_msg.? },
......@@ -877,14 +878,14 @@ pub fn generate(
877878 function.args = call_info.args;
878879 function.ret_mcv = call_info.return_value;
879880 function.frame_allocs.set(@intFromEnum(FrameIndex.ret_addr), FrameAlloc.init(.{
880 .size = Type.usize.abiSize(mod),
881 .alignment = Type.usize.abiAlignment(mod).min(call_info.stack_align),
881 .size = Type.usize.abiSize(zcu),
882 .alignment = Type.usize.abiAlignment(zcu).min(call_info.stack_align),
882883 }));
883884 function.frame_allocs.set(@intFromEnum(FrameIndex.base_ptr), FrameAlloc.init(.{
884 .size = Type.usize.abiSize(mod),
885 .size = Type.usize.abiSize(zcu),
885886 .alignment = Alignment.min(
886887 call_info.stack_align,
887 Alignment.fromNonzeroByteUnits(target.stackAlignment()),
888 Alignment.fromNonzeroByteUnits(function.target.stackAlignment()),
888889 ),
889890 }));
890891 function.frame_allocs.set(
......@@ -927,6 +928,9 @@ pub fn generate(
927928 .mir = mir,
928929 .cc = cc,
929930 .src_loc = src_loc,
931 .output_mode = comp.config.output_mode,
932 .link_mode = comp.config.link_mode,
933 .pic = mod.pic,
930934 },
931935 .debug_output = debug_output,
932936 .code = code,
......@@ -970,16 +974,14 @@ pub fn generateLazy(
970974) CodeGenError!Result {
971975 const comp = bin_file.comp;
972976 const gpa = comp.gpa;
973 const zcu = comp.module.?;
974 const decl_index = lazy_sym.ty.getOwnerDecl(zcu);
975 const decl = zcu.declPtr(decl_index);
976 const namespace = zcu.namespacePtr(decl.src_namespace);
977 const target = namespace.file_scope.mod.resolved_target.result;
977 // This function is for generating global code, so we use the root module.
978 const mod = comp.root_mod;
978979 var function = Self{
979980 .gpa = gpa,
980981 .air = undefined,
981982 .liveness = undefined,
982 .target = target,
983 .target = &mod.resolved_target.result,
984 .mod = mod,
983985 .bin_file = bin_file,
984986 .debug_output = debug_output,
985987 .owner = .{ .lazy_sym = lazy_sym },
......@@ -1020,6 +1022,9 @@ pub fn generateLazy(
10201022 .mir = mir,
10211023 .cc = abi.resolveCallingConvention(.Unspecified, function.target.*),
10221024 .src_loc = src_loc,
1025 .output_mode = comp.config.output_mode,
1026 .link_mode = comp.config.link_mode,
1027 .pic = mod.pic,
10231028 },
10241029 .debug_output = debug_output,
10251030 .code = code,
......@@ -1104,6 +1109,8 @@ fn formatWipMir(
11041109 _: std.fmt.FormatOptions,
11051110 writer: anytype,
11061111) @TypeOf(writer).Error!void {
1112 const comp = data.self.bin_file.comp;
1113 const mod = comp.root_mod;
11071114 var lower = Lower{
11081115 .bin_file = data.self.bin_file,
11091116 .allocator = data.self.gpa,
......@@ -1114,6 +1121,9 @@ fn formatWipMir(
11141121 },
11151122 .cc = .Unspecified,
11161123 .src_loc = data.self.src_loc,
1124 .output_mode = comp.config.output_mode,
1125 .link_mode = comp.config.link_mode,
1126 .pic = mod.pic,
11171127 };
11181128 var first = true;
11191129 for ((lower.lowerMir(data.inst) catch |err| switch (err) {
src/codegen/llvm.zig+1-1
......@@ -1168,7 +1168,7 @@ pub const Object = struct {
11681168 pre_ir_path: ?[]const u8,
11691169 pre_bc_path: ?[]const u8,
11701170 bin_path: ?[*:0]const u8,
1171 emit_asm: ?[*:0]const u8,
1171 asm_path: ?[*:0]const u8,
11721172 post_ir_path: ?[*:0]const u8,
11731173 post_bc_path: ?[*:0]const u8,
11741174
src/link.zig+122-176
......@@ -61,6 +61,7 @@ pub const File = struct {
6161 intermediary_basename: ?[]const u8 = null,
6262 disable_lld_caching: bool,
6363 gc_sections: bool,
64 print_gc_sections: bool,
6465 build_id: std.zig.BuildId,
6566 rpath_list: []const []const u8,
6667 /// List of symbols forced as undefined in the symbol table
......@@ -114,8 +115,8 @@ pub const File = struct {
114115 disable_lld_caching: bool,
115116 hash_style: Elf.HashStyle,
116117 sort_section: ?Elf.SortSection,
117 major_subsystem_version: ?u32,
118 minor_subsystem_version: ?u32,
118 major_subsystem_version: ?u16,
119 minor_subsystem_version: ?u16,
119120 gc_sections: ?bool,
120121 allow_shlib_undefined: ?bool,
121122 subsystem: ?std.Target.SubSystem,
......@@ -181,9 +182,15 @@ pub const File = struct {
181182 emit: Compilation.Emit,
182183 options: OpenOptions,
183184 ) !*File {
184 switch (Tag.fromObjectFormat(comp.root_mod.resolved_target.result.ofmt)) {
185 inline else => |tag| {
186 const ptr = try tag.Type().open(arena, comp, emit, options);
185 const tag = Tag.fromObjectFormat(comp.root_mod.resolved_target.result.ofmt);
186 switch (tag) {
187 .c => {
188 const ptr = try C.open(arena, comp, emit, options);
189 return &ptr.base;
190 },
191 inline else => |t| {
192 if (build_options.only_c) unreachable;
193 const ptr = try t.Type().open(arena, comp, emit, options);
187194 return &ptr.base;
188195 },
189196 }
......@@ -195,9 +202,15 @@ pub const File = struct {
195202 emit: Compilation.Emit,
196203 options: OpenOptions,
197204 ) !*File {
198 switch (Tag.fromObjectFormat(comp.root_mod.resolved_target.result.ofmt)) {
199 inline else => |tag| {
200 const ptr = try tag.Type().createEmpty(arena, comp, emit, options);
205 const tag = Tag.fromObjectFormat(comp.root_mod.resolved_target.result.ofmt);
206 switch (tag) {
207 .c => {
208 const ptr = try C.createEmpty(arena, comp, emit, options);
209 return &ptr.base;
210 },
211 inline else => |t| {
212 if (build_options.only_c) unreachable;
213 const ptr = try t.Type().createEmpty(arena, comp, emit, options);
201214 return &ptr.base;
202215 },
203216 }
......@@ -360,16 +373,12 @@ pub const File = struct {
360373 pub fn lowerUnnamedConst(base: *File, tv: TypedValue, decl_index: InternPool.DeclIndex) UpdateDeclError!u32 {
361374 if (build_options.only_c) @compileError("unreachable");
362375 switch (base.tag) {
363 // zig fmt: off
364 .coff => return @fieldParentPtr(Coff, "base", base).lowerUnnamedConst(tv, decl_index),
365 .elf => return @fieldParentPtr(Elf, "base", base).lowerUnnamedConst(tv, decl_index),
366 .macho => return @fieldParentPtr(MachO, "base", base).lowerUnnamedConst(tv, decl_index),
367 .plan9 => return @fieldParentPtr(Plan9, "base", base).lowerUnnamedConst(tv, decl_index),
368376 .spirv => unreachable,
369 .c => unreachable,
370 .wasm => return @fieldParentPtr(Wasm, "base", base).lowerUnnamedConst(tv, decl_index),
377 .c => unreachable,
371378 .nvptx => unreachable,
372 // zig fmt: on
379 inline else => |t| {
380 return @fieldParentPtr(t.Type(), "base", base).lowerUnnamedConst(tv, decl_index);
381 },
373382 }
374383 }
375384
......@@ -382,16 +391,13 @@ pub const File = struct {
382391 if (build_options.only_c) @compileError("unreachable");
383392 log.debug("getGlobalSymbol '{s}' (expected in '{?s}')", .{ name, lib_name });
384393 switch (base.tag) {
385 // zig fmt: off
386 .coff => return @fieldParentPtr(Coff, "base", base).getGlobalSymbol(name, lib_name),
387 .elf => return @fieldParentPtr(Elf, "base", base).getGlobalSymbol(name, lib_name),
388 .macho => return @fieldParentPtr(MachO, "base", base).getGlobalSymbol(name, lib_name),
389394 .plan9 => unreachable,
390395 .spirv => unreachable,
391 .c => unreachable,
392 .wasm => return @fieldParentPtr(Wasm, "base", base).getGlobalSymbol(name, lib_name),
396 .c => unreachable,
393397 .nvptx => unreachable,
394 // zig fmt: on
398 inline else => |t| {
399 return @fieldParentPtr(t.Type(), "base", base).getGlobalSymbol(name, lib_name);
400 },
395401 }
396402 }
397403
......@@ -399,59 +405,48 @@ pub const File = struct {
399405 pub fn updateDecl(base: *File, module: *Module, decl_index: InternPool.DeclIndex) UpdateDeclError!void {
400406 const decl = module.declPtr(decl_index);
401407 assert(decl.has_tv);
402 if (build_options.only_c) {
403 assert(base.tag == .c);
404 return @fieldParentPtr(C, "base", base).updateDecl(module, decl_index);
405 }
406408 switch (base.tag) {
407 // zig fmt: off
408 .coff => return @fieldParentPtr(Coff, "base", base).updateDecl(module, decl_index),
409 .elf => return @fieldParentPtr(Elf, "base", base).updateDecl(module, decl_index),
410 .macho => return @fieldParentPtr(MachO, "base", base).updateDecl(module, decl_index),
411 .c => return @fieldParentPtr(C, "base", base).updateDecl(module, decl_index),
412 .wasm => return @fieldParentPtr(Wasm, "base", base).updateDecl(module, decl_index),
413 .spirv => return @fieldParentPtr(SpirV, "base", base).updateDecl(module, decl_index),
414 .plan9 => return @fieldParentPtr(Plan9, "base", base).updateDecl(module, decl_index),
415 .nvptx => return @fieldParentPtr(NvPtx, "base", base).updateDecl(module, decl_index),
416 // zig fmt: on
409 .c => {
410 return @fieldParentPtr(C, "base", base).updateDecl(module, decl_index);
411 },
412 inline else => |tag| {
413 if (build_options.only_c) unreachable;
414 return @fieldParentPtr(tag.Type(), "base", base).updateDecl(module, decl_index);
415 },
417416 }
418417 }
419418
420419 /// May be called before or after updateExports for any given Decl.
421 pub fn updateFunc(base: *File, module: *Module, func_index: InternPool.Index, air: Air, liveness: Liveness) UpdateDeclError!void {
422 if (build_options.only_c) {
423 assert(base.tag == .c);
424 return @fieldParentPtr(C, "base", base).updateFunc(module, func_index, air, liveness);
425 }
420 pub fn updateFunc(
421 base: *File,
422 module: *Module,
423 func_index: InternPool.Index,
424 air: Air,
425 liveness: Liveness,
426 ) UpdateDeclError!void {
426427 switch (base.tag) {
427 // zig fmt: off
428 .coff => return @fieldParentPtr(Coff, "base", base).updateFunc(module, func_index, air, liveness),
429 .elf => return @fieldParentPtr(Elf, "base", base).updateFunc(module, func_index, air, liveness),
430 .macho => return @fieldParentPtr(MachO, "base", base).updateFunc(module, func_index, air, liveness),
431 .c => return @fieldParentPtr(C, "base", base).updateFunc(module, func_index, air, liveness),
432 .wasm => return @fieldParentPtr(Wasm, "base", base).updateFunc(module, func_index, air, liveness),
433 .spirv => return @fieldParentPtr(SpirV, "base", base).updateFunc(module, func_index, air, liveness),
434 .plan9 => return @fieldParentPtr(Plan9, "base", base).updateFunc(module, func_index, air, liveness),
435 .nvptx => return @fieldParentPtr(NvPtx, "base", base).updateFunc(module, func_index, air, liveness),
436 // zig fmt: on
428 .c => {
429 return @fieldParentPtr(C, "base", base).updateFunc(module, func_index, air, liveness);
430 },
431 inline else => |tag| {
432 if (build_options.only_c) unreachable;
433 return @fieldParentPtr(tag.Type(), "base", base).updateFunc(module, func_index, air, liveness);
434 },
437435 }
438436 }
439437
440438 pub fn updateDeclLineNumber(base: *File, module: *Module, decl_index: InternPool.DeclIndex) UpdateDeclError!void {
441439 const decl = module.declPtr(decl_index);
442440 assert(decl.has_tv);
443 if (build_options.only_c) {
444 assert(base.tag == .c);
445 return @fieldParentPtr(C, "base", base).updateDeclLineNumber(module, decl_index);
446 }
447441 switch (base.tag) {
448 .coff => return @fieldParentPtr(Coff, "base", base).updateDeclLineNumber(module, decl_index),
449 .elf => return @fieldParentPtr(Elf, "base", base).updateDeclLineNumber(module, decl_index),
450 .macho => return @fieldParentPtr(MachO, "base", base).updateDeclLineNumber(module, decl_index),
451 .c => return @fieldParentPtr(C, "base", base).updateDeclLineNumber(module, decl_index),
452 .wasm => return @fieldParentPtr(Wasm, "base", base).updateDeclLineNumber(module, decl_index),
453 .plan9 => return @fieldParentPtr(Plan9, "base", base).updateDeclLineNumber(module, decl_index),
454442 .spirv, .nvptx => {},
443 .c => {
444 return @fieldParentPtr(C, "base", base).updateDeclLineNumber(module, decl_index);
445 },
446 inline else => |tag| {
447 if (build_options.only_c) unreachable;
448 return @fieldParentPtr(tag.Type(), "base", base).updateDeclLineNumber(module, decl_index);
449 },
455450 }
456451 }
457452
......@@ -477,44 +472,11 @@ pub const File = struct {
477472 base.misc_errors.deinit(gpa);
478473 }
479474 switch (base.tag) {
480 .coff => {
481 if (build_options.only_c) unreachable;
482 const parent = @fieldParentPtr(Coff, "base", base);
483 parent.deinit();
484 },
485 .elf => {
486 if (build_options.only_c) unreachable;
487 const parent = @fieldParentPtr(Elf, "base", base);
488 parent.deinit();
489 },
490 .macho => {
491 if (build_options.only_c) unreachable;
492 const parent = @fieldParentPtr(MachO, "base", base);
493 parent.deinit();
494 },
495 .c => {
496 const parent = @fieldParentPtr(C, "base", base);
497 parent.deinit();
498 },
499 .wasm => {
500 if (build_options.only_c) unreachable;
501 const parent = @fieldParentPtr(Wasm, "base", base);
502 parent.deinit();
503 },
504 .spirv => {
505 if (build_options.only_c) unreachable;
506 const parent = @fieldParentPtr(SpirV, "base", base);
507 parent.deinit();
508 },
509 .plan9 => {
510 if (build_options.only_c) unreachable;
511 const parent = @fieldParentPtr(Plan9, "base", base);
512 parent.deinit();
513 },
514 .nvptx => {
475 .c => @fieldParentPtr(C, "base", base).deinit(),
476
477 inline else => |tag| {
515478 if (build_options.only_c) unreachable;
516 const parent = @fieldParentPtr(NvPtx, "base", base);
517 parent.deinit();
479 @fieldParentPtr(tag.Type(), "base", base).deinit();
518480 },
519481 }
520482 }
......@@ -619,51 +581,36 @@ pub const File = struct {
619581 return base.linkAsArchive(comp, prog_node);
620582 }
621583 switch (base.tag) {
622 .coff => return @fieldParentPtr(Coff, "base", base).flush(comp, prog_node),
623 .elf => return @fieldParentPtr(Elf, "base", base).flush(comp, prog_node),
624 .macho => return @fieldParentPtr(MachO, "base", base).flush(comp, prog_node),
625 .c => return @fieldParentPtr(C, "base", base).flush(comp, prog_node),
626 .wasm => return @fieldParentPtr(Wasm, "base", base).flush(comp, prog_node),
627 .spirv => return @fieldParentPtr(SpirV, "base", base).flush(comp, prog_node),
628 .plan9 => return @fieldParentPtr(Plan9, "base", base).flush(comp, prog_node),
629 .nvptx => return @fieldParentPtr(NvPtx, "base", base).flush(comp, prog_node),
584 inline else => |tag| {
585 return @fieldParentPtr(tag.Type(), "base", base).flush(comp, prog_node);
586 },
630587 }
631588 }
632589
633590 /// Commit pending changes and write headers. Works based on `effectiveOutputMode`
634591 /// rather than final output mode.
635592 pub fn flushModule(base: *File, comp: *Compilation, prog_node: *std.Progress.Node) FlushError!void {
636 if (build_options.only_c) {
637 assert(base.tag == .c);
638 return @fieldParentPtr(C, "base", base).flushModule(comp, prog_node);
639 }
640593 switch (base.tag) {
641 .coff => return @fieldParentPtr(Coff, "base", base).flushModule(comp, prog_node),
642 .elf => return @fieldParentPtr(Elf, "base", base).flushModule(comp, prog_node),
643 .macho => return @fieldParentPtr(MachO, "base", base).flushModule(comp, prog_node),
644 .c => return @fieldParentPtr(C, "base", base).flushModule(comp, prog_node),
645 .wasm => return @fieldParentPtr(Wasm, "base", base).flushModule(comp, prog_node),
646 .spirv => return @fieldParentPtr(SpirV, "base", base).flushModule(comp, prog_node),
647 .plan9 => return @fieldParentPtr(Plan9, "base", base).flushModule(comp, prog_node),
648 .nvptx => return @fieldParentPtr(NvPtx, "base", base).flushModule(comp, prog_node),
594 .c => {
595 return @fieldParentPtr(C, "base", base).flushModule(comp, prog_node);
596 },
597 inline else => |tag| {
598 if (build_options.only_c) unreachable;
599 return @fieldParentPtr(tag.Type(), "base", base).flushModule(comp, prog_node);
600 },
649601 }
650602 }
651603
652604 /// Called when a Decl is deleted from the Module.
653605 pub fn freeDecl(base: *File, decl_index: InternPool.DeclIndex) void {
654 if (build_options.only_c) {
655 assert(base.tag == .c);
656 return @fieldParentPtr(C, "base", base).freeDecl(decl_index);
657 }
658606 switch (base.tag) {
659 .coff => @fieldParentPtr(Coff, "base", base).freeDecl(decl_index),
660 .elf => @fieldParentPtr(Elf, "base", base).freeDecl(decl_index),
661 .macho => @fieldParentPtr(MachO, "base", base).freeDecl(decl_index),
662 .c => @fieldParentPtr(C, "base", base).freeDecl(decl_index),
663 .wasm => @fieldParentPtr(Wasm, "base", base).freeDecl(decl_index),
664 .spirv => @fieldParentPtr(SpirV, "base", base).freeDecl(decl_index),
665 .plan9 => @fieldParentPtr(Plan9, "base", base).freeDecl(decl_index),
666 .nvptx => @fieldParentPtr(NvPtx, "base", base).freeDecl(decl_index),
607 .c => {
608 @fieldParentPtr(C, "base", base).freeDecl(decl_index);
609 },
610 inline else => |tag| {
611 if (build_options.only_c) unreachable;
612 @fieldParentPtr(tag.Type(), "base", base).freeDecl(decl_index);
613 },
667614 }
668615 }
669616
......@@ -682,19 +629,14 @@ pub const File = struct {
682629 exported: Module.Exported,
683630 exports: []const *Module.Export,
684631 ) UpdateExportsError!void {
685 if (build_options.only_c) {
686 assert(base.tag == .c);
687 return @fieldParentPtr(C, "base", base).updateExports(module, exported, exports);
688 }
689632 switch (base.tag) {
690 .coff => return @fieldParentPtr(Coff, "base", base).updateExports(module, exported, exports),
691 .elf => return @fieldParentPtr(Elf, "base", base).updateExports(module, exported, exports),
692 .macho => return @fieldParentPtr(MachO, "base", base).updateExports(module, exported, exports),
693 .c => return @fieldParentPtr(C, "base", base).updateExports(module, exported, exports),
694 .wasm => return @fieldParentPtr(Wasm, "base", base).updateExports(module, exported, exports),
695 .spirv => return @fieldParentPtr(SpirV, "base", base).updateExports(module, exported, exports),
696 .plan9 => return @fieldParentPtr(Plan9, "base", base).updateExports(module, exported, exports),
697 .nvptx => return @fieldParentPtr(NvPtx, "base", base).updateExports(module, exported, exports),
633 .c => {
634 return @fieldParentPtr(C, "base", base).updateExports(module, exported, exports);
635 },
636 inline else => |tag| {
637 if (build_options.only_c) unreachable;
638 return @fieldParentPtr(tag.Type(), "base", base).updateExports(module, exported, exports);
639 },
698640 }
699641 }
700642
......@@ -711,60 +653,64 @@ pub const File = struct {
711653 /// May be called before or after updateFunc/updateDecl therefore it is up to the linker to allocate
712654 /// the block/atom.
713655 pub fn getDeclVAddr(base: *File, decl_index: InternPool.DeclIndex, reloc_info: RelocInfo) !u64 {
714 if (build_options.only_c) unreachable;
656 if (build_options.only_c) @compileError("unreachable");
715657 switch (base.tag) {
716 .coff => return @fieldParentPtr(Coff, "base", base).getDeclVAddr(decl_index, reloc_info),
717 .elf => return @fieldParentPtr(Elf, "base", base).getDeclVAddr(decl_index, reloc_info),
718 .macho => return @fieldParentPtr(MachO, "base", base).getDeclVAddr(decl_index, reloc_info),
719 .plan9 => return @fieldParentPtr(Plan9, "base", base).getDeclVAddr(decl_index, reloc_info),
720658 .c => unreachable,
721 .wasm => return @fieldParentPtr(Wasm, "base", base).getDeclVAddr(decl_index, reloc_info),
722659 .spirv => unreachable,
723660 .nvptx => unreachable,
661 inline else => |tag| {
662 return @fieldParentPtr(tag.Type(), "base", base).getDeclVAddr(decl_index, reloc_info);
663 },
724664 }
725665 }
726666
727667 pub const LowerResult = @import("codegen.zig").Result;
728668
729 pub fn lowerAnonDecl(base: *File, decl_val: InternPool.Index, decl_align: InternPool.Alignment, src_loc: Module.SrcLoc) !LowerResult {
730 if (build_options.only_c) unreachable;
669 pub fn lowerAnonDecl(
670 base: *File,
671 decl_val: InternPool.Index,
672 decl_align: InternPool.Alignment,
673 src_loc: Module.SrcLoc,
674 ) !LowerResult {
675 if (build_options.only_c) @compileError("unreachable");
731676 switch (base.tag) {
732 .coff => return @fieldParentPtr(Coff, "base", base).lowerAnonDecl(decl_val, decl_align, src_loc),
733 .elf => return @fieldParentPtr(Elf, "base", base).lowerAnonDecl(decl_val, decl_align, src_loc),
734 .macho => return @fieldParentPtr(MachO, "base", base).lowerAnonDecl(decl_val, decl_align, src_loc),
735 .plan9 => return @fieldParentPtr(Plan9, "base", base).lowerAnonDecl(decl_val, src_loc),
736677 .c => unreachable,
737 .wasm => return @fieldParentPtr(Wasm, "base", base).lowerAnonDecl(decl_val, decl_align, src_loc),
738678 .spirv => unreachable,
739679 .nvptx => unreachable,
680 inline else => |tag| {
681 return @fieldParentPtr(tag.Type(), "base", base).lowerAnonDecl(decl_val, decl_align, src_loc);
682 },
740683 }
741684 }
742685
743686 pub fn getAnonDeclVAddr(base: *File, decl_val: InternPool.Index, reloc_info: RelocInfo) !u64 {
744 if (build_options.only_c) unreachable;
687 if (build_options.only_c) @compileError("unreachable");
745688 switch (base.tag) {
746 .coff => return @fieldParentPtr(Coff, "base", base).getAnonDeclVAddr(decl_val, reloc_info),
747 .elf => return @fieldParentPtr(Elf, "base", base).getAnonDeclVAddr(decl_val, reloc_info),
748 .macho => return @fieldParentPtr(MachO, "base", base).getAnonDeclVAddr(decl_val, reloc_info),
749 .plan9 => return @fieldParentPtr(Plan9, "base", base).getAnonDeclVAddr(decl_val, reloc_info),
750689 .c => unreachable,
751 .wasm => return @fieldParentPtr(Wasm, "base", base).getAnonDeclVAddr(decl_val, reloc_info),
752690 .spirv => unreachable,
753691 .nvptx => unreachable,
692 inline else => |tag| {
693 return @fieldParentPtr(tag.Type(), "base", base).getAnonDeclVAddr(decl_val, reloc_info);
694 },
754695 }
755696 }
756697
757 pub fn deleteDeclExport(base: *File, decl_index: InternPool.DeclIndex, name: InternPool.NullTerminatedString) !void {
758 if (build_options.only_c) unreachable;
698 pub fn deleteDeclExport(
699 base: *File,
700 decl_index: InternPool.DeclIndex,
701 name: InternPool.NullTerminatedString,
702 ) !void {
703 if (build_options.only_c) @compileError("unreachable");
759704 switch (base.tag) {
760 .coff => return @fieldParentPtr(Coff, "base", base).deleteDeclExport(decl_index, name),
761 .elf => return @fieldParentPtr(Elf, "base", base).deleteDeclExport(decl_index, name),
762 .macho => return @fieldParentPtr(MachO, "base", base).deleteDeclExport(decl_index, name),
763 .plan9 => {},
764 .c => {},
765 .wasm => return @fieldParentPtr(Wasm, "base", base).deleteDeclExport(decl_index),
766 .spirv => {},
767 .nvptx => {},
705 .plan9,
706 .c,
707 .spirv,
708 .nvptx,
709 => {},
710
711 inline else => |tag| {
712 return @fieldParentPtr(tag.Type(), "base", base).deleteDeclExport(decl_index, name);
713 },
768714 }
769715 }
770716
......@@ -1049,7 +995,7 @@ pub const File = struct {
1049995 base: File,
1050996 arena: Allocator,
1051997 opt_loc: ?Compilation.EmitLoc,
1052 ) Allocator.Error!?[*:0]u8 {
998 ) Allocator.Error!?[*:0]const u8 {
1053999 const loc = opt_loc orelse return null;
10541000 const slice = if (loc.directory) |directory|
10551001 try directory.joinZ(arena, &.{loc.basename})
......@@ -1071,7 +1017,7 @@ pub const File = struct {
10711017 sub_prog_node.context.refresh();
10721018 defer sub_prog_node.end();
10731019
1074 try llvm_object.emit(comp, .{
1020 try llvm_object.emit(.{
10751021 .pre_ir_path = comp.verbose_llvm_ir,
10761022 .pre_bc_path = comp.verbose_llvm_bc,
10771023 .bin_path = try base.resolveEmitLoc(arena, .{
......@@ -1079,8 +1025,8 @@ pub const File = struct {
10791025 .basename = base.intermediary_basename.?,
10801026 }),
10811027 .asm_path = try base.resolveEmitLoc(arena, comp.emit_asm),
1082 .post_llvm_ir_path = try base.resolveEmitLoc(arena, comp.emit_llvm_ir),
1083 .post_llvm_bc_path = try base.resolveEmitLoc(arena, comp.emit_llvm_bc),
1028 .post_ir_path = try base.resolveEmitLoc(arena, comp.emit_llvm_ir),
1029 .post_bc_path = try base.resolveEmitLoc(arena, comp.emit_llvm_bc),
10841030
10851031 .is_debug = comp.root_mod.optimize_mode == .Debug,
10861032 .is_small = comp.root_mod.optimize_mode == .ReleaseSmall,
src/link/C.zig+1
......@@ -135,6 +135,7 @@ pub fn createEmpty(
135135 .comp = comp,
136136 .emit = emit,
137137 .gc_sections = options.gc_sections orelse (optimize_mode != .Debug and output_mode != .Obj),
138 .print_gc_sections = options.print_gc_sections,
138139 .stack_size = options.stack_size orelse 16777216,
139140 .allow_shlib_undefined = options.allow_shlib_undefined orelse false,
140141 .file = file,
src/link/Coff.zig+9-6
......@@ -14,9 +14,10 @@ nxcompat: bool,
1414dynamicbase: bool,
1515/// TODO this and minor_subsystem_version should be combined into one property and left as
1616/// default or populated together. They should not be separate fields.
17major_subsystem_version: u32,
18minor_subsystem_version: u32,
17major_subsystem_version: u16,
18minor_subsystem_version: u16,
1919lib_dirs: []const []const u8,
20entry_addr: ?u32,
2021
2122ptr_width: PtrWidth,
2223page_size: u32,
......@@ -238,7 +239,6 @@ pub fn open(
238239 emit: Compilation.Emit,
239240 options: link.File.OpenOptions,
240241) !*Coff {
241 if (build_options.only_c) unreachable;
242242 const target = comp.root_mod.resolved_target.result;
243243 assert(target.ofmt == .coff);
244244
......@@ -390,6 +390,7 @@ pub fn createEmpty(
390390 .emit = emit,
391391 .stack_size = options.stack_size orelse 16777216,
392392 .gc_sections = options.gc_sections orelse (optimize_mode != .Debug),
393 .print_gc_sections = options.print_gc_sections,
393394 .allow_shlib_undefined = options.allow_shlib_undefined orelse false,
394395 .file = null,
395396 .disable_lld_caching = options.disable_lld_caching,
......@@ -422,6 +423,8 @@ pub fn createEmpty(
422423 .major_subsystem_version = options.major_subsystem_version orelse 6,
423424 .minor_subsystem_version = options.minor_subsystem_version orelse 0,
424425 .lib_dirs = options.lib_dirs,
426 .entry_addr = math.cast(u32, options.entry_addr orelse 0) orelse
427 return error.EntryAddressTooBig,
425428 };
426429
427430 const use_llvm = comp.config.use_llvm;
......@@ -2329,8 +2332,8 @@ fn writeHeader(self: *Coff) !void {
23292332 .minor_operating_system_version = 0,
23302333 .major_image_version = 0,
23312334 .minor_image_version = 0,
2332 .major_subsystem_version = self.major_subsystem_version,
2333 .minor_subsystem_version = self.minor_subsystem_version,
2335 .major_subsystem_version = @intCast(self.major_subsystem_version),
2336 .minor_subsystem_version = @intCast(self.minor_subsystem_version),
23342337 .win32_version_value = 0,
23352338 .size_of_image = size_of_image,
23362339 .size_of_headers = size_of_headers,
......@@ -2342,7 +2345,7 @@ fn writeHeader(self: *Coff) !void {
23422345 .size_of_heap_reserve = default_size_of_heap_reserve,
23432346 .size_of_heap_commit = default_size_of_heap_commit,
23442347 .loader_flags = 0,
2345 .number_of_rva_and_sizes = @as(u32, @intCast(self.data_directories.len)),
2348 .number_of_rva_and_sizes = @intCast(self.data_directories.len),
23462349 };
23472350 writer.writeAll(mem.asBytes(&opt_header)) catch unreachable;
23482351 },
src/link/Coff/lld.zig+23-22
......@@ -21,7 +21,8 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
2121 const tracy = trace(@src());
2222 defer tracy.end();
2323
24 var arena_allocator = std.heap.ArenaAllocator.init(self.base.allocator);
24 const gpa = comp.gpa;
25 var arena_allocator = std.heap.ArenaAllocator.init(gpa);
2526 defer arena_allocator.deinit();
2627 const arena = arena_allocator.allocator();
2728
......@@ -30,7 +31,7 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
3031
3132 // If there is no Zig code to compile, then we should skip flushing the output file because it
3233 // will not be part of the linker line anyway.
33 const module_obj_path: ?[]const u8 = if (self.base.comp.module != null) blk: {
34 const module_obj_path: ?[]const u8 = if (comp.module != null) blk: {
3435 try self.flushModule(comp, prog_node);
3536
3637 if (fs.path.dirname(full_out_path)) |dirname| {
......@@ -45,12 +46,12 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
4546 sub_prog_node.context.refresh();
4647 defer sub_prog_node.end();
4748
48 const is_lib = self.base.comp.config.output_mode == .Lib;
49 const is_dyn_lib = self.base.comp.config.link_mode == .Dynamic and is_lib;
50 const is_exe_or_dyn_lib = is_dyn_lib or self.base.comp.config.output_mode == .Exe;
49 const is_lib = comp.config.output_mode == .Lib;
50 const is_dyn_lib = comp.config.link_mode == .Dynamic and is_lib;
51 const is_exe_or_dyn_lib = is_dyn_lib or comp.config.output_mode == .Exe;
5152 const link_in_crt = comp.config.link_libc and is_exe_or_dyn_lib;
52 const target = self.base.comp.root_mod.resolved_target.result;
53 const optimize_mode = self.base.comp.root_mod.optimize_mode;
53 const target = comp.root_mod.resolved_target.result;
54 const optimize_mode = comp.root_mod.optimize_mode;
5455
5556 // See link/Elf.zig for comments on how this mechanism works.
5657 const id_symlink_basename = "lld.id";
......@@ -85,8 +86,8 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
8586 man.hash.addListOfBytes(self.lib_dirs);
8687 man.hash.add(comp.skip_linker_dependencies);
8788 if (comp.config.link_libc) {
88 man.hash.add(self.base.comp.libc_installation != null);
89 if (self.base.comp.libc_installation) |libc_installation| {
89 man.hash.add(comp.libc_installation != null);
90 if (comp.libc_installation) |libc_installation| {
9091 man.hash.addBytes(libc_installation.crt_dir.?);
9192 if (target.abi == .msvc) {
9293 man.hash.addBytes(libc_installation.msvc_lib_dir.?);
......@@ -94,7 +95,7 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
9495 }
9596 }
9697 }
97 try link.hashAddSystemLibs(&man, self.base.comp.system_libs);
98 try link.hashAddSystemLibs(&man, comp.system_libs);
9899 man.hash.addListOfBytes(self.base.force_undefined_symbols.keys());
99100 man.hash.addOptional(self.subsystem);
100101 man.hash.add(comp.config.is_test);
......@@ -136,7 +137,7 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
136137 };
137138 }
138139
139 if (self.base.comp.config.output_mode == .Obj) {
140 if (comp.config.output_mode == .Obj) {
140141 // LLD's COFF driver does not support the equivalent of `-r` so we do a simple file copy
141142 // here. TODO: think carefully about how we can avoid this redundant operation when doing
142143 // build-obj. See also the corresponding TODO in linkAsArchive.
......@@ -161,7 +162,7 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
161162 }
162163 } else {
163164 // Create an LLD command line and invoke it.
164 var argv = std.ArrayList([]const u8).init(self.base.allocator);
165 var argv = std.ArrayList([]const u8).init(gpa);
165166 defer argv.deinit();
166167 // We will invoke ourselves as a child process to gain access to LLD.
167168 // This is necessary because LLD does not behave properly as a library -
......@@ -192,7 +193,7 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
192193 .ReleaseFast, .ReleaseSafe => try argv.append("-OPT:lldlto=3"),
193194 }
194195 }
195 if (self.base.comp.config.output_mode == .Exe) {
196 if (comp.config.output_mode == .Exe) {
196197 try argv.append(try allocPrint(arena, "-STACK:{d}", .{self.base.stack_size}));
197198 }
198199 try argv.append(try std.fmt.allocPrint(arena, "-BASE:{d}", .{self.image_base}));
......@@ -242,7 +243,7 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
242243 }
243244
244245 if (comp.config.link_libc) {
245 if (self.base.comp.libc_installation) |libc_installation| {
246 if (comp.libc_installation) |libc_installation| {
246247 try argv.append(try allocPrint(arena, "-LIBPATH:{s}", .{libc_installation.crt_dir.?}));
247248
248249 if (target.abi == .msvc) {
......@@ -287,7 +288,7 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
287288 if (self.subsystem) |explicit| break :blk explicit;
288289 switch (target.os.tag) {
289290 .windows => {
290 if (self.base.comp.module) |module| {
291 if (comp.module) |module| {
291292 if (module.stage1_flags.have_dllmain_crt_startup or is_dyn_lib)
292293 break :blk null;
293294 if (module.stage1_flags.have_c_main or comp.config.is_test or
......@@ -415,13 +416,13 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
415416 try argv.append(try comp.get_libc_crt_file(arena, "uuid.lib"));
416417
417418 for (mingw.always_link_libs) |name| {
418 if (!self.base.comp.system_libs.contains(name)) {
419 if (!comp.system_libs.contains(name)) {
419420 const lib_basename = try allocPrint(arena, "{s}.lib", .{name});
420421 try argv.append(try comp.get_libc_crt_file(arena, lib_basename));
421422 }
422423 }
423424 } else {
424 const lib_str = switch (self.base.comp.config.link_mode) {
425 const lib_str = switch (comp.config.link_mode) {
425426 .Dynamic => "",
426427 .Static => "lib",
427428 };
......@@ -429,7 +430,7 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
429430 .Debug => "d",
430431 else => "",
431432 };
432 switch (self.base.comp.config.link_mode) {
433 switch (comp.config.link_mode) {
433434 .Static => try argv.append(try allocPrint(arena, "libcmt{s}.lib", .{d_str})),
434435 .Dynamic => try argv.append(try allocPrint(arena, "msvcrt{s}.lib", .{d_str})),
435436 }
......@@ -448,7 +449,7 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
448449 } else {
449450 try argv.append("-NODEFAULTLIB");
450451 if (!is_lib and comp.config.entry == null) {
451 if (self.base.comp.module) |module| {
452 if (comp.module) |module| {
452453 if (module.stage1_flags.have_winmain_crt_startup) {
453454 try argv.append("-ENTRY:WinMainCRTStartup");
454455 } else {
......@@ -485,8 +486,8 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
485486 if (comp.compiler_rt_lib) |lib| try argv.append(lib.full_object_path);
486487 }
487488
488 try argv.ensureUnusedCapacity(self.base.comp.system_libs.count());
489 for (self.base.comp.system_libs.keys()) |key| {
489 try argv.ensureUnusedCapacity(comp.system_libs.count());
490 for (comp.system_libs.keys()) |key| {
490491 const lib_basename = try allocPrint(arena, "{s}.lib", .{key});
491492 if (comp.crt_files.get(lib_basename)) |crt_file| {
492493 argv.appendAssumeCapacity(crt_file.full_object_path);
......@@ -512,7 +513,7 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
512513 return error.DllImportLibraryNotFound;
513514 }
514515
515 if (self.base.comp.verbose_link) {
516 if (comp.verbose_link) {
516517 // Skip over our own name so that the LLD linker name is the first argv item.
517518 Compilation.dump_argv(argv.items[1..]);
518519 }
src/link/Dwarf.zig+4-2
......@@ -1745,6 +1745,7 @@ pub fn freeDecl(self: *Dwarf, decl_index: InternPool.DeclIndex) void {
17451745}
17461746
17471747pub fn writeDbgAbbrev(self: *Dwarf) !void {
1748 const gpa = self.allocator;
17481749 // These are LEB encoded but since the values are all less than 127
17491750 // we can simply append these bytes.
17501751 // zig fmt: off
......@@ -1887,7 +1888,7 @@ pub fn writeDbgAbbrev(self: *Dwarf) !void {
18871888 .wasm => {
18881889 const wasm_file = self.bin_file.cast(File.Wasm).?;
18891890 const debug_abbrev = &wasm_file.getAtomPtr(wasm_file.debug_abbrev_atom.?).code;
1890 try debug_abbrev.resize(wasm_file.base.allocator, needed_size);
1891 try debug_abbrev.resize(gpa, needed_size);
18911892 debug_abbrev.items[0..abbrev_buf.len].* = abbrev_buf;
18921893 },
18931894 else => unreachable,
......@@ -2236,6 +2237,7 @@ fn writeDbgInfoNopsToArrayList(
22362237
22372238pub fn writeDbgAranges(self: *Dwarf, addr: u64, size: u64) !void {
22382239 const comp = self.bin_file.comp;
2240 const gpa = comp.gpa;
22392241 const target = comp.root_mod.resolved_target.result;
22402242 const target_endian = target.cpu.arch.endian();
22412243 const ptr_width_bytes = self.ptrWidthBytes();
......@@ -2301,7 +2303,7 @@ pub fn writeDbgAranges(self: *Dwarf, addr: u64, size: u64) !void {
23012303 .wasm => {
23022304 const wasm_file = self.bin_file.cast(File.Wasm).?;
23032305 const debug_ranges = &wasm_file.getAtomPtr(wasm_file.debug_ranges_atom.?).code;
2304 try debug_ranges.resize(wasm_file.base.allocator, needed_size);
2306 try debug_ranges.resize(gpa, needed_size);
23052307 @memcpy(debug_ranges.items[0..di_buf.items.len], di_buf.items);
23062308 },
23072309 else => unreachable,
src/link/Elf.zig+6-6
......@@ -230,7 +230,6 @@ pub fn open(
230230 emit: Compilation.Emit,
231231 options: link.File.OpenOptions,
232232) !*Elf {
233 if (build_options.only_c) unreachable;
234233 const target = comp.root_mod.resolved_target.result;
235234 assert(target.ofmt == .elf);
236235
......@@ -380,6 +379,7 @@ pub fn createEmpty(
380379 .comp = comp,
381380 .emit = emit,
382381 .gc_sections = options.gc_sections orelse (optimize_mode != .Debug and output_mode != .Obj),
382 .print_gc_sections = options.print_gc_sections,
383383 .stack_size = options.stack_size orelse 16777216,
384384 .allow_shlib_undefined = options.allow_shlib_undefined orelse !is_native_os,
385385 .file = null,
......@@ -1175,7 +1175,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
11751175 const lib_name = flag["-l".len..];
11761176
11771177 success: {
1178 if (!self.isStatic()) {
1178 if (!self.base.isStatic()) {
11791179 if (try self.accessLibPath(&test_path, &checked_paths, lc.crt_dir.?, lib_name, .Dynamic))
11801180 break :success;
11811181 }
......@@ -1664,7 +1664,7 @@ fn dumpArgv(self: *Elf, comp: *Compilation) !void {
16641664 try argv.append(p);
16651665 }
16661666 } else {
1667 if (!self.isStatic()) {
1667 if (!self.base.isStatic()) {
16681668 if (target.dynamic_linker.get()) |path| {
16691669 try argv.append("-dynamic-linker");
16701670 try argv.append(path);
......@@ -1742,7 +1742,7 @@ fn dumpArgv(self: *Elf, comp: *Compilation) !void {
17421742 try argv.append("now");
17431743 }
17441744
1745 if (self.isStatic()) {
1745 if (self.base.isStatic()) {
17461746 try argv.append("-static");
17471747 } else if (self.base.isDynLib()) {
17481748 try argv.append("-shared");
......@@ -2023,7 +2023,7 @@ fn parseLdScript(self: *Elf, lib: SystemLib) ParseError!void {
20232023 // TODO I think technically we should re-use the mechanism used by the frontend here.
20242024 // Maybe we should hoist search-strategy all the way here?
20252025 for (self.lib_dirs) |lib_dir| {
2026 if (!self.isStatic()) {
2026 if (!self.base.isStatic()) {
20272027 if (try self.accessLibPath(&test_path, &checked_paths, lib_dir, lib_name, .Dynamic))
20282028 break :success;
20292029 }
......@@ -3598,7 +3598,7 @@ fn initSyntheticSections(self: *Elf) !void {
35983598 // In this case, if we do generate .interp section and segment, we will get
35993599 // a segfault in the dynamic linker trying to load a binary that is static
36003600 // and doesn't contain .dynamic section.
3601 if (self.isStatic() and !comp.config.pie) break :blk false;
3601 if (self.base.isStatic() and !comp.config.pie) break :blk false;
36023602 break :blk target.dynamic_linker.get() != null;
36033603 };
36043604 if (needs_interp) {
src/link/Elf/Atom.zig+3-3
......@@ -377,8 +377,8 @@ pub fn scanRelocsRequiresCode(self: Atom, elf_file: *Elf) bool {
377377}
378378
379379pub fn scanRelocs(self: Atom, elf_file: *Elf, code: ?[]const u8, undefs: anytype) !void {
380 const is_static = elf_file.isStatic();
381 const is_dyn_lib = elf_file.isDynLib();
380 const is_static = elf_file.base.isStatic();
381 const is_dyn_lib = elf_file.base.isDynLib();
382382 const file_ptr = self.file(elf_file).?;
383383 const rels = self.relocs(elf_file);
384384 var i: usize = 0;
......@@ -660,7 +660,7 @@ fn dynAbsRelocAction(symbol: *const Symbol, elf_file: *Elf) RelocAction {
660660
661661fn outputType(elf_file: *Elf) u2 {
662662 const comp = elf_file.base.comp;
663 assert(!elf_file.isRelocatable());
663 assert(!elf_file.base.isRelocatable());
664664 return switch (elf_file.base.comp.config.output_mode) {
665665 .Obj => unreachable,
666666 .Lib => 0,
src/link/Elf/Object.zig+3-3
......@@ -250,7 +250,7 @@ fn addAtom(self: *Object, shdr: ElfShdr, shndx: u16, elf_file: *Elf) error{OutOf
250250fn initOutputSection(self: Object, elf_file: *Elf, shdr: ElfShdr) error{OutOfMemory}!u16 {
251251 const name = blk: {
252252 const name = self.getString(shdr.sh_name);
253 if (elf_file.isRelocatable()) break :blk name;
253 if (elf_file.base.isRelocatable()) break :blk name;
254254 if (shdr.sh_flags & elf.SHF_MERGE != 0) break :blk name;
255255 const sh_name_prefixes: []const [:0]const u8 = &.{
256256 ".text", ".data.rel.ro", ".data", ".rodata", ".bss.rel.ro", ".bss",
......@@ -278,7 +278,7 @@ fn initOutputSection(self: Object, elf_file: *Elf, shdr: ElfShdr) error{OutOfMem
278278 };
279279 const flags = blk: {
280280 var flags = shdr.sh_flags;
281 if (!elf_file.isRelocatable()) {
281 if (!elf_file.base.isRelocatable()) {
282282 flags &= ~@as(u64, elf.SHF_COMPRESSED | elf.SHF_GROUP | elf.SHF_GNU_RETAIN);
283283 }
284284 break :blk switch (@"type") {
......@@ -520,7 +520,7 @@ pub fn claimUnresolved(self: *Object, elf_file: *Elf) void {
520520 }
521521
522522 const is_import = blk: {
523 if (!elf_file.isDynLib()) break :blk false;
523 if (!elf_file.base.isDynLib()) break :blk false;
524524 const vis = @as(elf.STV, @enumFromInt(esym.st_other));
525525 if (vis == .HIDDEN) break :blk false;
526526 break :blk true;
src/link/Elf/ZigObject.zig+1-1
......@@ -370,7 +370,7 @@ pub fn claimUnresolved(self: ZigObject, elf_file: *Elf) void {
370370 }
371371
372372 const is_import = blk: {
373 if (!elf_file.isDynLib()) break :blk false;
373 if (!elf_file.base.isDynLib()) break :blk false;
374374 const vis = @as(elf.STV, @enumFromInt(esym.st_other));
375375 if (vis == .HIDDEN) break :blk false;
376376 break :blk true;
src/link/Elf/eh_frame.zig+1-1
......@@ -271,7 +271,7 @@ pub fn calcEhFrameSize(elf_file: *Elf) !usize {
271271 }
272272 }
273273
274 if (!elf_file.isRelocatable()) {
274 if (!elf_file.base.isRelocatable()) {
275275 offset += 4; // NULL terminator
276276 }
277277
src/link/MachO.zig+19-15
......@@ -155,6 +155,7 @@ frameworks: []const Framework,
155155install_name: ?[]const u8,
156156/// Path to entitlements file.
157157entitlements: ?[]const u8,
158compatibility_version: ?std.SemanticVersion,
158159
159160/// When adding a new field, remember to update `hashAddFrameworks`.
160161pub const Framework = struct {
......@@ -185,7 +186,6 @@ pub fn open(
185186 emit: Compilation.Emit,
186187 options: link.File.OpenOptions,
187188) !*MachO {
188 if (build_options.only_c) unreachable;
189189 const target = comp.root_mod.resolved_target.result;
190190 const use_lld = build_options.have_llvm and comp.config.use_lld;
191191 const use_llvm = comp.config.use_llvm;
......@@ -295,6 +295,7 @@ pub fn createEmpty(
295295 .comp = comp,
296296 .emit = emit,
297297 .gc_sections = options.gc_sections orelse (optimize_mode != .Debug),
298 .print_gc_sections = options.print_gc_sections,
298299 .stack_size = options.stack_size orelse 16777216,
299300 .allow_shlib_undefined = options.allow_shlib_undefined orelse false,
300301 .file = null,
......@@ -315,6 +316,7 @@ pub fn createEmpty(
315316 .frameworks = options.frameworks,
316317 .install_name = options.install_name,
317318 .entitlements = options.entitlements,
319 .compatibility_version = options.compatibility_version,
318320 };
319321
320322 if (use_llvm and comp.module != null) {
......@@ -632,7 +634,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No
632634 });
633635 {
634636 const platform = Platform.fromTarget(target);
635 const sdk_version: ?std.SemanticVersion = load_commands.inferSdkVersion(arena, comp);
637 const sdk_version: ?std.SemanticVersion = load_commands.inferSdkVersion(self);
636638 if (platform.isBuildVersionCompatible()) {
637639 try load_commands.writeBuildVersionLC(platform, sdk_version, lc_writer);
638640 } else if (platform.isVersionMinCompatible()) {
......@@ -2237,10 +2239,10 @@ fn allocateGlobal(self: *MachO) !u32 {
22372239 return index;
22382240}
22392241
2240pub fn addGotEntry(self: *MachO, target: SymbolWithLoc) !void {
2241 if (self.got_table.lookup.contains(target)) return;
2242pub fn addGotEntry(self: *MachO, reloc_target: SymbolWithLoc) !void {
2243 if (self.got_table.lookup.contains(reloc_target)) return;
22422244 const gpa = self.base.comp.gpa;
2243 const got_index = try self.got_table.allocateEntry(gpa, target);
2245 const got_index = try self.got_table.allocateEntry(gpa, reloc_target);
22442246 if (self.got_section_index == null) {
22452247 self.got_section_index = try self.initSection("__DATA_CONST", "__got", .{
22462248 .flags = macho.S_NON_LAZY_SYMBOL_POINTERS,
......@@ -2249,20 +2251,22 @@ pub fn addGotEntry(self: *MachO, target: SymbolWithLoc) !void {
22492251 if (self.mode == .incremental) {
22502252 try self.writeOffsetTableEntry(got_index);
22512253 self.got_table_count_dirty = true;
2252 self.markRelocsDirtyByTarget(target);
2254 self.markRelocsDirtyByTarget(reloc_target);
22532255 }
22542256}
22552257
2256pub fn addStubEntry(self: *MachO, target: SymbolWithLoc) !void {
2257 if (self.stub_table.lookup.contains(target)) return;
2258 const gpa = self.base.comp.gpa;
2259 const stub_index = try self.stub_table.allocateEntry(gpa, target);
2258pub fn addStubEntry(self: *MachO, reloc_target: SymbolWithLoc) !void {
2259 if (self.stub_table.lookup.contains(reloc_target)) return;
2260 const comp = self.base.comp;
2261 const gpa = comp.gpa;
2262 const cpu_arch = comp.root_mod.resolved_target.result.cpu.arch;
2263 const stub_index = try self.stub_table.allocateEntry(gpa, reloc_target);
22602264 if (self.stubs_section_index == null) {
22612265 self.stubs_section_index = try self.initSection("__TEXT", "__stubs", .{
22622266 .flags = macho.S_SYMBOL_STUBS |
22632267 macho.S_ATTR_PURE_INSTRUCTIONS |
22642268 macho.S_ATTR_SOME_INSTRUCTIONS,
2265 .reserved2 = stubs.stubSize(target.cpu.arch),
2269 .reserved2 = stubs.stubSize(cpu_arch),
22662270 });
22672271 self.stub_helper_section_index = try self.initSection("__TEXT", "__stub_helper", .{
22682272 .flags = macho.S_REGULAR |
......@@ -2276,14 +2280,14 @@ pub fn addStubEntry(self: *MachO, target: SymbolWithLoc) !void {
22762280 if (self.mode == .incremental) {
22772281 try self.writeStubTableEntry(stub_index);
22782282 self.stub_table_count_dirty = true;
2279 self.markRelocsDirtyByTarget(target);
2283 self.markRelocsDirtyByTarget(reloc_target);
22802284 }
22812285}
22822286
2283pub fn addTlvPtrEntry(self: *MachO, target: SymbolWithLoc) !void {
2284 if (self.tlv_ptr_table.lookup.contains(target)) return;
2287pub fn addTlvPtrEntry(self: *MachO, reloc_target: SymbolWithLoc) !void {
2288 if (self.tlv_ptr_table.lookup.contains(reloc_target)) return;
22852289 const gpa = self.base.comp.gpa;
2286 _ = try self.tlv_ptr_table.allocateEntry(gpa, target);
2290 _ = try self.tlv_ptr_table.allocateEntry(gpa, reloc_target);
22872291 if (self.tlv_ptr_section_index == null) {
22882292 self.tlv_ptr_section_index = try self.initSection("__DATA", "__thread_ptrs", .{
22892293 .flags = macho.S_THREAD_LOCAL_VARIABLE_POINTERS,
src/link/MachO/Object.zig+11-5
......@@ -342,19 +342,22 @@ pub const SplitIntoAtomsError = error{
342342};
343343
344344pub fn splitIntoAtoms(self: *Object, macho_file: *MachO, object_id: u32) SplitIntoAtomsError!void {
345 const comp = macho_file.base.comp;
346 const gpa = comp.gpa;
345347 log.debug("splitting object({d}, {s}) into atoms", .{ object_id, self.name });
346348
347349 try self.splitRegularSections(macho_file, object_id);
348350 try self.parseEhFrameSection(macho_file, object_id);
349351 try self.parseUnwindInfo(macho_file, object_id);
350 try self.parseDataInCode(macho_file.base.allocator);
352 try self.parseDataInCode(gpa);
351353}
352354
353355/// Splits input regular sections into Atoms.
354356/// If the Object was compiled with `MH_SUBSECTIONS_VIA_SYMBOLS`, splits section
355357/// into subsections where each subsection then represents an Atom.
356358pub fn splitRegularSections(self: *Object, macho_file: *MachO, object_id: u32) !void {
357 const gpa = macho_file.base.allocator;
359 const comp = macho_file.base.comp;
360 const gpa = comp.gpa;
358361 const target = macho_file.base.comp.root_mod.resolved_target.result;
359362
360363 const sections = self.getSourceSections();
......@@ -555,7 +558,8 @@ fn createAtomFromSubsection(
555558 alignment: Alignment,
556559 out_sect_id: u8,
557560) !Atom.Index {
558 const gpa = macho_file.base.allocator;
561 const comp = macho_file.base.comp;
562 const gpa = comp.gpa;
559563 const atom_index = try macho_file.createAtom(sym_index, .{
560564 .size = size,
561565 .alignment = alignment,
......@@ -671,7 +675,8 @@ fn parseEhFrameSection(self: *Object, macho_file: *MachO, object_id: u32) !void
671675
672676 log.debug("parsing __TEXT,__eh_frame section", .{});
673677
674 const gpa = macho_file.base.allocator;
678 const comp = macho_file.base.comp;
679 const gpa = comp.gpa;
675680
676681 if (macho_file.eh_frame_section_index == null) {
677682 macho_file.eh_frame_section_index = try macho_file.initSection("__TEXT", "__eh_frame", .{});
......@@ -767,7 +772,8 @@ fn parseEhFrameSection(self: *Object, macho_file: *MachO, object_id: u32) !void
767772}
768773
769774fn parseUnwindInfo(self: *Object, macho_file: *MachO, object_id: u32) !void {
770 const gpa = macho_file.base.allocator;
775 const comp = macho_file.base.comp;
776 const gpa = comp.gpa;
771777 const target = macho_file.base.comp.root_mod.resolved_target.result;
772778 const cpu_arch = target.cpu.arch;
773779 const sect_id = self.unwind_info_sect_id orelse {
src/link/MachO/dead_strip.zig+2-1
......@@ -1,7 +1,8 @@
11//! An algorithm for dead stripping of unreferenced Atoms.
22
33pub fn gcAtoms(macho_file: *MachO) !void {
4 const gpa = macho_file.base.allocator;
4 const comp = macho_file.base.comp;
5 const gpa = comp.gpa;
56
67 var arena = std.heap.ArenaAllocator.init(gpa);
78 defer arena.deinit();
src/link/MachO/eh_frame.zig+6-3
......@@ -1,5 +1,6 @@
11pub fn scanRelocs(macho_file: *MachO) !void {
2 const gpa = macho_file.base.allocator;
2 const comp = macho_file.base.comp;
3 const gpa = comp.gpa;
34
45 for (macho_file.objects.items, 0..) |*object, object_id| {
56 var cies = std.AutoHashMap(u32, void).init(gpa);
......@@ -37,7 +38,8 @@ pub fn calcSectionSize(macho_file: *MachO, unwind_info: *const UnwindInfo) error
3738
3839 const target = macho_file.base.comp.root_mod.resolved_target.result;
3940 const cpu_arch = target.cpu.arch;
40 const gpa = macho_file.base.allocator;
41 const comp = macho_file.base.comp;
42 const gpa = comp.gpa;
4143 var size: u32 = 0;
4244
4345 for (macho_file.objects.items, 0..) |*object, object_id| {
......@@ -89,7 +91,8 @@ pub fn write(macho_file: *MachO, unwind_info: *UnwindInfo) !void {
8991
9092 const target = macho_file.base.comp.root_mod.resolved_target.result;
9193 const cpu_arch = target.cpu.arch;
92 const gpa = macho_file.base.allocator;
94 const comp = macho_file.base.comp;
95 const gpa = comp.gpa;
9396
9497 var eh_records = std.AutoArrayHashMap(u32, EhFrameRecord(true)).init(gpa);
9598 defer {
src/link/MachO/load_commands.zig+20-13
......@@ -63,7 +63,7 @@ fn calcLCsSize(m: *MachO, ctx: CalcLCsSizeCtx, assume_max_path_len: bool) !u32 {
6363 }
6464 // LC_RPATH
6565 {
66 var it = RpathIterator.init(gpa, m.rpath_list);
66 var it = RpathIterator.init(gpa, m.base.rpath_list);
6767 defer it.deinit();
6868 while (try it.next()) |rpath| {
6969 sizeofcmds += calcInstallNameLen(
......@@ -189,17 +189,20 @@ fn writeDylibLC(ctx: WriteDylibLCCtx, lc_writer: anytype) !void {
189189 }
190190}
191191
192pub fn writeDylibIdLC(gpa: Allocator, options: *const link.Options, lc_writer: anytype) !void {
193 assert(options.output_mode == .Lib and options.link_mode == .Dynamic);
194 const emit = options.emit.?;
195 const install_name = options.install_name orelse try emit.directory.join(gpa, &.{emit.sub_path});
196 defer if (options.install_name == null) gpa.free(install_name);
197 const curr = options.version orelse std.SemanticVersion{
192pub fn writeDylibIdLC(macho_file: *MachO, lc_writer: anytype) !void {
193 const comp = macho_file.base.comp;
194 const gpa = comp.gpa;
195 assert(comp.config.output_mode == .Lib and comp.config.link_mode == .Dynamic);
196 const emit = macho_file.base.emit;
197 const install_name = macho_file.install_name orelse
198 try emit.directory.join(gpa, &.{emit.sub_path});
199 defer if (macho_file.install_name == null) gpa.free(install_name);
200 const curr = comp.version orelse std.SemanticVersion{
198201 .major = 1,
199202 .minor = 0,
200203 .patch = 0,
201204 };
202 const compat = options.compatibility_version orelse std.SemanticVersion{
205 const compat = macho_file.compatibility_version orelse std.SemanticVersion{
203206 .major = 1,
204207 .minor = 0,
205208 .patch = 0,
......@@ -237,8 +240,11 @@ const RpathIterator = struct {
237240 }
238241};
239242
240pub fn writeRpathLCs(gpa: Allocator, options: *const link.Options, lc_writer: anytype) !void {
241 var it = RpathIterator.init(gpa, options.rpath_list);
243pub fn writeRpathLCs(macho_file: *MachO, lc_writer: anytype) !void {
244 const comp = macho_file.base.comp;
245 const gpa = comp.gpa;
246
247 var it = RpathIterator.init(gpa, macho_file.base.rpath_list);
242248 defer it.deinit();
243249
244250 while (try it.next()) |rpath| {
......@@ -467,13 +473,14 @@ pub inline fn appleVersionToSemanticVersion(version: u32) std.SemanticVersion {
467473 };
468474}
469475
470pub fn inferSdkVersion(gpa: Allocator, comp: *const Compilation) ?std.SemanticVersion {
476pub fn inferSdkVersion(macho_file: *MachO) ?std.SemanticVersion {
477 const comp = macho_file.base.comp;
478 const gpa = comp.gpa;
479
471480 var arena_allocator = std.heap.ArenaAllocator.init(gpa);
472481 defer arena_allocator.deinit();
473482 const arena = arena_allocator.allocator();
474483
475 const macho_file = comp.bin_file.cast(MachO).?;
476
477484 const sdk_layout = macho_file.sdk_layout orelse return null;
478485 const sdk_dir = switch (sdk_layout) {
479486 .sdk => comp.sysroot.?,
src/link/MachO/thunks.zig+4-2
......@@ -68,7 +68,8 @@ pub fn createThunks(macho_file: *MachO, sect_id: u8) !void {
6868 const header = &macho_file.sections.items(.header)[sect_id];
6969 if (header.size == 0) return;
7070
71 const gpa = macho_file.base.allocator;
71 const comp = macho_file.base.comp;
72 const gpa = comp.gpa;
7273 const first_atom_index = macho_file.sections.items(.first_atom_index)[sect_id].?;
7374
7475 header.size = 0;
......@@ -245,7 +246,8 @@ fn scanRelocs(
245246 macho_file.getSymbol(target).n_value,
246247 });
247248
248 const gpa = macho_file.base.allocator;
249 const comp = macho_file.base.comp;
250 const gpa = comp.gpa;
249251 const target_sym = macho_file.getSymbol(target);
250252 const thunk = &macho_file.thunks.items[thunk_index];
251253
src/link/MachO/zld.zig+25-17
......@@ -15,7 +15,7 @@ pub fn linkWithZld(
1515 const arena = arena_allocator.allocator();
1616
1717 const directory = emit.directory; // Just an alias to make it shorter to type.
18 const full_out_path = try directory.join(arena, &[_][]const u8{emit.?.sub_path});
18 const full_out_path = try directory.join(arena, &[_][]const u8{emit.sub_path});
1919 const opt_zcu = macho_file.base.comp.module;
2020
2121 // If there is no Zig code to compile, then we should skip flushing the output file because it
......@@ -71,14 +71,14 @@ pub fn linkWithZld(
7171 // We can skip hashing libc and libc++ components that we are in charge of building from Zig
7272 // installation sources because they are always a product of the compiler version + target information.
7373 man.hash.add(stack_size);
74 man.hash.addOptional(macho_file.pagezero_vmsize);
75 man.hash.addOptional(macho_file.headerpad_size);
74 man.hash.add(macho_file.pagezero_vmsize);
75 man.hash.add(macho_file.headerpad_size);
7676 man.hash.add(macho_file.headerpad_max_install_names);
7777 man.hash.add(macho_file.base.gc_sections);
7878 man.hash.add(macho_file.dead_strip_dylibs);
7979 man.hash.add(macho_file.base.comp.root_mod.strip);
8080 try MachO.hashAddFrameworks(&man, macho_file.frameworks);
81 man.hash.addListOfBytes(macho_file.rpath_list);
81 man.hash.addListOfBytes(macho_file.base.rpath_list);
8282 if (is_dyn_lib) {
8383 man.hash.addOptionalBytes(macho_file.install_name);
8484 man.hash.addOptional(comp.version);
......@@ -151,7 +151,7 @@ pub fn linkWithZld(
151151 try fs.cwd().copyFile(the_object_path, fs.cwd(), full_out_path, .{});
152152 }
153153 } else {
154 const sub_path = emit.?.sub_path;
154 const sub_path = emit.sub_path;
155155
156156 const old_file = macho_file.base.file; // TODO is this needed at all?
157157 defer macho_file.base.file = old_file;
......@@ -241,7 +241,7 @@ pub fn linkWithZld(
241241 try argv.append(@tagName(platform.os_tag));
242242 try argv.append(try std.fmt.allocPrint(arena, "{}", .{platform.version}));
243243
244 const sdk_version: ?std.SemanticVersion = load_commands.inferSdkVersion(arena, comp);
244 const sdk_version: ?std.SemanticVersion = load_commands.inferSdkVersion(macho_file);
245245 if (sdk_version) |ver| {
246246 try argv.append(try std.fmt.allocPrint(arena, "{d}.{d}", .{ ver.major, ver.minor }));
247247 } else {
......@@ -254,13 +254,13 @@ pub fn linkWithZld(
254254 try argv.append(syslibroot);
255255 }
256256
257 for (macho_file.rpath_list) |rpath| {
257 for (macho_file.base.rpath_list) |rpath| {
258258 try argv.append("-rpath");
259259 try argv.append(rpath);
260260 }
261261
262262 try argv.appendSlice(&.{
263 "-pagezero_size", try std.fmt.allocPrint(arena, "0x{x}", .{macho_file.pagezero_size}),
263 "-pagezero_size", try std.fmt.allocPrint(arena, "0x{x}", .{macho_file.pagezero_vmsize}),
264264 "-headerpad_size", try std.fmt.allocPrint(arena, "0x{x}", .{macho_file.headerpad_size}),
265265 });
266266
......@@ -558,7 +558,7 @@ pub fn linkWithZld(
558558 });
559559 {
560560 const platform = Platform.fromTarget(target);
561 const sdk_version: ?std.SemanticVersion = load_commands.inferSdkVersion(arena, comp);
561 const sdk_version: ?std.SemanticVersion = load_commands.inferSdkVersion(macho_file);
562562 if (platform.isBuildVersionCompatible()) {
563563 try load_commands.writeBuildVersionLC(platform, sdk_version, lc_writer);
564564 } else {
......@@ -609,7 +609,8 @@ pub fn linkWithZld(
609609}
610610
611611fn createSegments(macho_file: *MachO) !void {
612 const gpa = macho_file.base.allocator;
612 const comp = macho_file.base.comp;
613 const gpa = comp.gpa;
613614 const target = macho_file.base.comp.root_mod.resolved_target.result;
614615 const page_size = MachO.getPageSize(target.cpu.arch);
615616 const aligned_pagezero_vmsize = mem.alignBackward(u64, macho_file.pagezero_vmsize, page_size);
......@@ -683,7 +684,8 @@ fn createSegments(macho_file: *MachO) !void {
683684}
684685
685686fn writeAtoms(macho_file: *MachO) !void {
686 const gpa = macho_file.base.allocator;
687 const comp = macho_file.base.comp;
688 const gpa = comp.gpa;
687689 const slice = macho_file.sections.slice();
688690
689691 for (slice.items(.first_atom_index), 0..) |first_atom_index, sect_id| {
......@@ -758,7 +760,8 @@ fn writeDyldPrivateAtom(macho_file: *MachO) !void {
758760fn writeThunks(macho_file: *MachO) !void {
759761 const target = macho_file.base.comp.root_mod.resolved_target.result;
760762 assert(target.cpu.arch == .aarch64);
761 const gpa = macho_file.base.allocator;
763 const comp = macho_file.base.comp;
764 const gpa = comp.gpa;
762765
763766 const sect_id = macho_file.text_section_index orelse return;
764767 const header = macho_file.sections.items(.header)[sect_id];
......@@ -778,7 +781,8 @@ fn writeThunks(macho_file: *MachO) !void {
778781}
779782
780783fn writePointerEntries(macho_file: *MachO, sect_id: u8, table: anytype) !void {
781 const gpa = macho_file.base.allocator;
784 const comp = macho_file.base.comp;
785 const gpa = comp.gpa;
782786 const header = macho_file.sections.items(.header)[sect_id];
783787 const capacity = math.cast(usize, header.size) orelse return error.Overflow;
784788 var buffer = try std.ArrayList(u8).initCapacity(gpa, capacity);
......@@ -792,7 +796,8 @@ fn writePointerEntries(macho_file: *MachO, sect_id: u8, table: anytype) !void {
792796}
793797
794798fn writeStubs(macho_file: *MachO) !void {
795 const gpa = macho_file.base.allocator;
799 const comp = macho_file.base.comp;
800 const gpa = comp.gpa;
796801 const target = macho_file.base.comp.root_mod.resolved_target.result;
797802 const cpu_arch = target.cpu.arch;
798803 const stubs_header = macho_file.sections.items(.header)[macho_file.stubs_section_index.?];
......@@ -815,7 +820,8 @@ fn writeStubs(macho_file: *MachO) !void {
815820}
816821
817822fn writeStubHelpers(macho_file: *MachO) !void {
818 const gpa = macho_file.base.allocator;
823 const comp = macho_file.base.comp;
824 const gpa = comp.gpa;
819825 const target = macho_file.base.comp.root_mod.resolved_target.result;
820826 const cpu_arch = target.cpu.arch;
821827 const stub_helper_header = macho_file.sections.items(.header)[macho_file.stub_helper_section_index.?];
......@@ -859,7 +865,8 @@ fn writeStubHelpers(macho_file: *MachO) !void {
859865}
860866
861867fn writeLaSymbolPtrs(macho_file: *MachO) !void {
862 const gpa = macho_file.base.allocator;
868 const comp = macho_file.base.comp;
869 const gpa = comp.gpa;
863870 const target = macho_file.base.comp.root_mod.resolved_target.result;
864871 const cpu_arch = target.cpu.arch;
865872 const la_symbol_ptr_header = macho_file.sections.items(.header)[macho_file.la_symbol_ptr_section_index.?];
......@@ -892,7 +899,8 @@ fn pruneAndSortSections(macho_file: *MachO) !void {
892899 }
893900 };
894901
895 const gpa = macho_file.base.allocator;
902 const comp = macho_file.base.comp;
903 const gpa = comp.gpa;
896904
897905 var entries = try std.ArrayList(Entry).initCapacity(gpa, macho_file.sections.slice().len);
898906 defer entries.deinit();
src/link/NvPtx.zig+1-2
......@@ -31,8 +31,6 @@ pub fn createEmpty(
3131 emit: Compilation.Emit,
3232 options: link.File.OpenOptions,
3333) !*NvPtx {
34 if (build_options.only_c) unreachable;
35
3634 const target = comp.root_mod.resolved_target.result;
3735 const use_lld = build_options.have_llvm and comp.config.use_lld;
3836 const use_llvm = comp.config.use_llvm;
......@@ -55,6 +53,7 @@ pub fn createEmpty(
5553 .comp = comp,
5654 .emit = emit,
5755 .gc_sections = options.gc_sections orelse false,
56 .print_gc_sections = options.print_gc_sections,
5857 .stack_size = options.stack_size orelse 0,
5958 .allow_shlib_undefined = options.allow_shlib_undefined orelse false,
6059 .file = null,
src/link/Plan9.zig+8-3
......@@ -318,6 +318,7 @@ pub fn createEmpty(
318318 .comp = comp,
319319 .emit = emit,
320320 .gc_sections = options.gc_sections orelse (optimize_mode != .Debug and output_mode != .Obj),
321 .print_gc_sections = options.print_gc_sections,
321322 .stack_size = options.stack_size orelse 16777216,
322323 .allow_shlib_undefined = options.allow_shlib_undefined orelse false,
323324 .file = null,
......@@ -1314,8 +1315,6 @@ pub fn open(
13141315 emit: Compilation.Emit,
13151316 options: link.File.OpenOptions,
13161317) !*Plan9 {
1317 if (build_options.only_c) unreachable;
1318
13191318 const target = comp.root_mod.resolved_target.result;
13201319 const use_lld = build_options.have_llvm and comp.config.use_lld;
13211320 const use_llvm = comp.config.use_llvm;
......@@ -1525,7 +1524,13 @@ pub fn getDeclVAddr(
15251524 return undefined;
15261525}
15271526
1528pub fn lowerAnonDecl(self: *Plan9, decl_val: InternPool.Index, src_loc: Module.SrcLoc) !codegen.Result {
1527pub fn lowerAnonDecl(
1528 self: *Plan9,
1529 decl_val: InternPool.Index,
1530 explicit_alignment: InternPool.Alignment,
1531 src_loc: Module.SrcLoc,
1532) !codegen.Result {
1533 _ = explicit_alignment;
15291534 // This is basically the same as lowerUnnamedConst.
15301535 // example:
15311536 // const ty = mod.intern_pool.typeOf(decl_val).toType();
src/link/SpirV.zig+11-8
......@@ -24,7 +24,6 @@ const SpirV = @This();
2424
2525const std = @import("std");
2626const Allocator = std.mem.Allocator;
27const ArenaAllocator = std.heap.ArenaAllocator;
2827const assert = std.debug.assert;
2928const log = std.log.scoped(.link);
3029
......@@ -65,6 +64,7 @@ pub fn createEmpty(
6564 .comp = comp,
6665 .emit = emit,
6766 .gc_sections = options.gc_sections orelse false,
67 .print_gc_sections = options.print_gc_sections,
6868 .stack_size = options.stack_size orelse 0,
6969 .allow_shlib_undefined = options.allow_shlib_undefined orelse false,
7070 .file = null,
......@@ -98,8 +98,6 @@ pub fn open(
9898 emit: Compilation.Emit,
9999 options: link.File.OpenOptions,
100100) !*SpirV {
101 if (build_options.only_c) unreachable;
102
103101 const target = comp.root_mod.resolved_target.result;
104102 const use_lld = build_options.have_llvm and comp.config.use_lld;
105103 const use_llvm = comp.config.use_llvm;
......@@ -194,7 +192,9 @@ pub fn flushModule(self: *SpirV, comp: *Compilation, prog_node: *std.Progress.No
194192
195193 const spv = &self.object.spv;
196194
195 const gpa = comp.gpa;
197196 const target = comp.getTarget();
197
198198 try writeCapabilities(spv, target);
199199 try writeMemoryModel(spv, target);
200200
......@@ -214,11 +214,11 @@ pub fn flushModule(self: *SpirV, comp: *Compilation, prog_node: *std.Progress.No
214214 // name if it contains no strange characters is nice for debugging. URI encoding fits the bill.
215215 // We're using : as separator, which is a reserved character.
216216
217 const escaped_name = try std.Uri.escapeString(self.base.allocator, name);
218 defer self.base.allocator.free(escaped_name);
217 const escaped_name = try std.Uri.escapeString(gpa, name);
218 defer gpa.free(escaped_name);
219219 try error_info.writer().print(":{s}", .{escaped_name});
220220 }
221 try spv.sections.debug_strings.emit(spv.gpa, .OpSourceExtension, .{
221 try spv.sections.debug_strings.emit(gpa, .OpSourceExtension, .{
222222 .extension = error_info.items,
223223 });
224224
......@@ -226,6 +226,7 @@ pub fn flushModule(self: *SpirV, comp: *Compilation, prog_node: *std.Progress.No
226226}
227227
228228fn writeCapabilities(spv: *SpvModule, target: std.Target) !void {
229 const gpa = spv.gpa;
229230 // TODO: Integrate with a hypothetical feature system
230231 const caps: []const spec.Capability = switch (target.os.tag) {
231232 .opencl => &.{ .Kernel, .Addresses, .Int8, .Int16, .Int64, .Float64, .Float16, .GenericPointer },
......@@ -235,13 +236,15 @@ fn writeCapabilities(spv: *SpvModule, target: std.Target) !void {
235236 };
236237
237238 for (caps) |cap| {
238 try spv.sections.capabilities.emit(spv.gpa, .OpCapability, .{
239 try spv.sections.capabilities.emit(gpa, .OpCapability, .{
239240 .capability = cap,
240241 });
241242 }
242243}
243244
244245fn writeMemoryModel(spv: *SpvModule, target: std.Target) !void {
246 const gpa = spv.gpa;
247
245248 const addressing_model = switch (target.os.tag) {
246249 .opencl => switch (target.cpu.arch) {
247250 .spirv32 => spec.AddressingModel.Physical32,
......@@ -260,7 +263,7 @@ fn writeMemoryModel(spv: *SpvModule, target: std.Target) !void {
260263 };
261264
262265 // TODO: Put this in a proper section.
263 try spv.sections.extensions.emit(spv.gpa, .OpMemoryModel, .{
266 try spv.sections.extensions.emit(gpa, .OpMemoryModel, .{
264267 .addressing_model = addressing_model,
265268 .memory_model = memory_model,
266269 });
src/link/Wasm.zig+7-2
......@@ -378,7 +378,6 @@ pub fn open(
378378 emit: Compilation.Emit,
379379 options: link.File.OpenOptions,
380380) !*Wasm {
381 if (build_options.only_c) unreachable;
382381 const gpa = comp.gpa;
383382 const target = comp.root_mod.resolved_target.result;
384383 assert(target.ofmt == .wasm);
......@@ -549,6 +548,7 @@ pub fn createEmpty(
549548 .comp = comp,
550549 .emit = emit,
551550 .gc_sections = options.gc_sections orelse (output_mode != .Obj),
551 .print_gc_sections = options.print_gc_sections,
552552 .stack_size = options.stack_size orelse std.wasm.page_size * 16, // 1MB
553553 .allow_shlib_undefined = options.allow_shlib_undefined orelse false,
554554 .file = null,
......@@ -1893,7 +1893,12 @@ pub fn getAnonDeclVAddr(wasm: *Wasm, decl_val: InternPool.Index, reloc_info: lin
18931893 return target_symbol_index;
18941894}
18951895
1896pub fn deleteDeclExport(wasm: *Wasm, decl_index: InternPool.DeclIndex) void {
1896pub fn deleteDeclExport(
1897 wasm: *Wasm,
1898 decl_index: InternPool.DeclIndex,
1899 name: InternPool.NullTerminatedString,
1900) void {
1901 _ = name;
18971902 if (wasm.llvm_object) |_| return;
18981903 const atom_index = wasm.decls.get(decl_index) orelse return;
18991904 const sym_index = wasm.getAtom(atom_index).sym_index;
src/link/Wasm/Object.zig+6-4
......@@ -883,6 +883,8 @@ fn assertEnd(reader: anytype) !void {
883883
884884/// Parses an object file into atoms, for code and data sections
885885pub fn parseSymbolIntoAtom(object: *Object, object_index: u16, symbol_index: u32, wasm: *Wasm) !Atom.Index {
886 const comp = wasm.base.comp;
887 const gpa = comp.gpa;
886888 const symbol = &object.symtable[symbol_index];
887889 const relocatable_data: RelocatableData = switch (symbol.tag) {
888890 .function => object.relocatable_data.get(.code).?[symbol.index - object.importedCountByKind(.function)],
......@@ -900,7 +902,7 @@ pub fn parseSymbolIntoAtom(object: *Object, object_index: u16, symbol_index: u32
900902 };
901903 const final_index = try wasm.getMatchingSegment(object_index, symbol_index);
902904 const atom_index = @as(Atom.Index, @intCast(wasm.managed_atoms.items.len));
903 const atom = try wasm.managed_atoms.addOne(wasm.base.allocator);
905 const atom = try wasm.managed_atoms.addOne(gpa);
904906 atom.* = Atom.empty;
905907 try wasm.appendAtomAtIndex(final_index, atom_index);
906908
......@@ -910,7 +912,7 @@ pub fn parseSymbolIntoAtom(object: *Object, object_index: u16, symbol_index: u32
910912 atom.alignment = relocatable_data.getAlignment(object);
911913 atom.code = std.ArrayListUnmanaged(u8).fromOwnedSlice(relocatable_data.data[0..relocatable_data.size]);
912914 atom.original_offset = relocatable_data.offset;
913 try wasm.symbol_atom.putNoClobber(wasm.base.allocator, atom.symbolLoc(), atom_index);
915 try wasm.symbol_atom.putNoClobber(gpa, atom.symbolLoc(), atom_index);
914916 const segment: *Wasm.Segment = &wasm.segments.items[final_index];
915917 if (relocatable_data.type == .data) { //code section and custom sections are 1-byte aligned
916918 segment.alignment = segment.alignment.max(atom.alignment);
......@@ -927,7 +929,7 @@ pub fn parseSymbolIntoAtom(object: *Object, object_index: u16, symbol_index: u32
927929 .R_WASM_TABLE_INDEX_SLEB,
928930 .R_WASM_TABLE_INDEX_SLEB64,
929931 => {
930 try wasm.function_table.put(wasm.base.allocator, .{
932 try wasm.function_table.put(gpa, .{
931933 .file = object_index,
932934 .index = reloc.index,
933935 }, 0);
......@@ -938,7 +940,7 @@ pub fn parseSymbolIntoAtom(object: *Object, object_index: u16, symbol_index: u32
938940 const sym = object.symtable[reloc.index];
939941 if (sym.tag != .global) {
940942 try wasm.got_symbols.append(
941 wasm.base.allocator,
943 gpa,
942944 .{ .file = object_index, .index = reloc.index },
943945 );
944946 }
src/main.zig+10-14
......@@ -874,8 +874,8 @@ fn buildOutputType(
874874 var override_lib_dir: ?[]const u8 = try EnvVar.ZIG_LIB_DIR.get(arena);
875875 var clang_preprocessor_mode: Compilation.ClangPreprocessorMode = .no;
876876 var subsystem: ?std.Target.SubSystem = null;
877 var major_subsystem_version: ?u32 = null;
878 var minor_subsystem_version: ?u32 = null;
877 var major_subsystem_version: ?u16 = null;
878 var minor_subsystem_version: ?u16 = null;
879879 var enable_link_snapshots: bool = false;
880880 var debug_incremental: bool = false;
881881 var install_name: ?[]const u8 = null;
......@@ -2321,21 +2321,17 @@ fn buildOutputType(
23212321 _ = linker_args_it.nextOrFatal();
23222322 } else if (mem.eql(u8, arg, "--major-subsystem-version")) {
23232323 const major = linker_args_it.nextOrFatal();
2324 major_subsystem_version = std.fmt.parseUnsigned(
2325 u32,
2326 major,
2327 10,
2328 ) catch |err| {
2329 fatal("unable to parse major subsystem version '{s}': {s}", .{ major, @errorName(err) });
2324 major_subsystem_version = std.fmt.parseUnsigned(u16, major, 10) catch |err| {
2325 fatal("unable to parse major subsystem version '{s}': {s}", .{
2326 major, @errorName(err),
2327 });
23302328 };
23312329 } else if (mem.eql(u8, arg, "--minor-subsystem-version")) {
23322330 const minor = linker_args_it.nextOrFatal();
2333 minor_subsystem_version = std.fmt.parseUnsigned(
2334 u32,
2335 minor,
2336 10,
2337 ) catch |err| {
2338 fatal("unable to parse minor subsystem version '{s}': {s}", .{ minor, @errorName(err) });
2331 minor_subsystem_version = std.fmt.parseUnsigned(u16, minor, 10) catch |err| {
2332 fatal("unable to parse minor subsystem version '{s}': {s}", .{
2333 minor, @errorName(err),
2334 });
23392335 };
23402336 } else if (mem.eql(u8, arg, "-framework")) {
23412337 try frameworks.put(arena, linker_args_it.nextOrFatal(), .{});