Commit f256431838

Andrew Kelley <andrew@ziglang.org>
2023-12-16 23:22:31
fix compilation errors when enabling llvm
1 parent 57afdfc
src/link/Coff/lld.zig
@@ -82,7 +82,7 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
         try man.addOptionalFile(module_obj_path);
         man.hash.addOptionalBytes(comp.config.entry);
         man.hash.add(self.base.stack_size);
-        man.hash.addOptional(self.image_base);
+        man.hash.add(self.image_base);
         man.hash.addListOfBytes(self.lib_dirs);
         man.hash.add(comp.skip_linker_dependencies);
         if (comp.config.link_libc) {
@@ -102,10 +102,10 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
         man.hash.add(self.tsaware);
         man.hash.add(self.nxcompat);
         man.hash.add(self.dynamicbase);
-        man.hash.addOptional(self.base.allow_shlib_undefined);
+        man.hash.add(self.base.allow_shlib_undefined);
         // strip does not need to go into the linker hash because it is part of the hash namespace
-        man.hash.addOptional(self.major_subsystem_version);
-        man.hash.addOptional(self.minor_subsystem_version);
+        man.hash.add(self.major_subsystem_version);
+        man.hash.add(self.minor_subsystem_version);
         man.hash.addOptional(comp.version);
         try man.addOptionalFile(self.module_definition_file);
 
@@ -237,7 +237,7 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
 
         try argv.append(try allocPrint(arena, "-OUT:{s}", .{full_out_path}));
 
-        if (self.implib_emit) |emit| {
+        if (comp.implib_emit) |emit| {
             const implib_out_path = try emit.directory.join(arena, &[_][]const u8{emit.sub_path});
             try argv.append(try allocPrint(arena, "-IMPLIB:{s}", .{implib_out_path}));
         }
@@ -310,16 +310,9 @@ pub fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Nod
         const Mode = enum { uefi, win32 };
         const mode: Mode = mode: {
             if (resolved_subsystem) |subsystem| {
-                const subsystem_suffix = ss: {
-                    if (self.major_subsystem_version) |major| {
-                        if (self.minor_subsystem_version) |minor| {
-                            break :ss try allocPrint(arena, ",{d}.{d}", .{ major, minor });
-                        } else {
-                            break :ss try allocPrint(arena, ",{d}", .{major});
-                        }
-                    }
-                    break :ss "";
-                };
+                const subsystem_suffix = try allocPrint(arena, ",{d}.{d}", .{
+                    self.major_subsystem_version, self.minor_subsystem_version,
+                });
 
                 switch (subsystem) {
                     .Console => {
src/link/Coff.zig
@@ -18,6 +18,8 @@ major_subsystem_version: u16,
 minor_subsystem_version: u16,
 lib_dirs: []const []const u8,
 entry_addr: ?u32,
+module_definition_file: ?[]const u8,
+pdb_out_path: ?[]const u8,
 
 ptr_width: PtrWidth,
 page_size: u32,
@@ -425,6 +427,8 @@ pub fn createEmpty(
         .lib_dirs = options.lib_dirs,
         .entry_addr = math.cast(u32, options.entry_addr orelse 0) orelse
             return error.EntryAddressTooBig,
+        .module_definition_file = options.module_definition_file,
+        .pdb_out_path = options.pdb_out_path,
     };
 
     const use_llvm = comp.config.use_llvm;
src/link/Elf.zig
@@ -23,6 +23,8 @@ soname: ?[]const u8,
 bind_global_refs_locally: bool,
 linker_script: ?[]const u8,
 version_script: ?[]const u8,
+print_icf_sections: bool,
+print_map: bool,
 
 ptr_width: PtrWidth,
 
@@ -307,6 +309,8 @@ pub fn createEmpty(
         .bind_global_refs_locally = options.bind_global_refs_locally,
         .linker_script = options.linker_script,
         .version_script = options.version_script,
+        .print_icf_sections = options.print_icf_sections,
+        .print_map = options.print_map,
     };
     if (use_llvm and comp.config.have_zcu) {
         self.llvm_object = try LlvmObject.create(arena, comp);
@@ -1294,12 +1298,9 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
 
     // Look for entry address in objects if not set by the incremental compiler.
     if (self.entry_index == null) {
-        const entry: ?[]const u8 = entry: {
-            if (comp.config.entry) |entry| break :entry entry;
-            if (!self.base.isDynLib()) break :entry "_start";
-            break :entry null;
-        };
-        self.entry_index = if (entry) |name| self.globalByName(name) else null;
+        if (comp.config.entry) |name| {
+            self.entry_index = self.globalByName(name);
+        }
     }
 
     if (self.base.gc_sections) {
@@ -2420,7 +2421,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
         // We can skip hashing libc and libc++ components that we are in charge of building from Zig
         // installation sources because they are always a product of the compiler version + target information.
         man.hash.addOptionalBytes(comp.config.entry);
-        man.hash.addOptional(self.image_base);
+        man.hash.add(self.image_base);
         man.hash.add(self.base.gc_sections);
         man.hash.addOptional(self.sort_section);
         man.hash.add(self.eh_frame_hdr);
@@ -5896,7 +5897,7 @@ pub fn addSymbol(self: *Elf) !Symbol.Index {
             break :blk index;
         } else {
             log.debug("  (allocating symbol index {d})", .{self.symbols.items.len});
-            const index = @as(Symbol.Index, @intCast(self.symbols.items.len));
+            const index: Symbol.Index = @intCast(self.symbols.items.len);
             _ = self.symbols.addOneAssumeCapacity();
             break :blk index;
         }
@@ -5961,6 +5962,7 @@ pub fn getOrPutGlobal(self: *Elf, name: []const u8) !GetOrPutGlobalResult {
     const gop = try self.resolver.getOrPut(gpa, name_off);
     if (!gop.found_existing) {
         const index = try self.addSymbol();
+        log.debug("added symbol '{s}' at index {d}", .{ name, index });
         const global = self.symbol(index);
         global.name_offset = name_off;
         global.flags.global = true;
@@ -5996,7 +5998,7 @@ pub fn getOrCreateComdatGroupOwner(self: *Elf, name: [:0]const u8) !GetOrCreateC
     const off = try self.strings.insert(gpa, name);
     const gop = try self.comdat_groups_table.getOrPut(gpa, off);
     if (!gop.found_existing) {
-        const index = @as(ComdatGroupOwner.Index, @intCast(self.comdat_groups_owners.items.len));
+        const index: ComdatGroupOwner.Index = @intCast(self.comdat_groups_owners.items.len);
         const owner = try self.comdat_groups_owners.addOne(gpa);
         owner.* = .{};
         gop.value_ptr.* = index;
src/Package/Module.zig
@@ -310,7 +310,39 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module {
         break :b buf.items[0 .. buf.items.len - 1 :0].ptr;
     };
 
-    const builtin_mod = options.builtin_mod orelse b: {
+    const mod = try arena.create(Module);
+    mod.* = .{
+        .root = options.paths.root,
+        .root_src_path = options.paths.root_src_path,
+        .fully_qualified_name = options.fully_qualified_name,
+        .resolved_target = .{
+            .result = target,
+            .is_native_os = resolved_target.is_native_os,
+            .is_native_abi = resolved_target.is_native_abi,
+            .llvm_cpu_features = llvm_cpu_features,
+        },
+        .optimize_mode = optimize_mode,
+        .single_threaded = single_threaded,
+        .error_tracing = error_tracing,
+        .valgrind = valgrind,
+        .pic = pic,
+        .strip = strip,
+        .omit_frame_pointer = omit_frame_pointer,
+        .stack_check = stack_check,
+        .stack_protector = stack_protector,
+        .code_model = code_model,
+        .red_zone = red_zone,
+        .sanitize_c = sanitize_c,
+        .sanitize_thread = sanitize_thread,
+        .unwind_tables = unwind_tables,
+        .cc_argv = options.cc_argv,
+        .structured_cfg = structured_cfg,
+        .builtin_file = null,
+    };
+
+    const opt_builtin_mod = options.builtin_mod orelse b: {
+        if (!options.global.have_zcu) break :b null;
+
         const generated_builtin_source = try Builtin.generate(.{
             .target = target,
             .zig_backend = zig_backend,
@@ -388,38 +420,10 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module {
         break :b new;
     };
 
-    const mod = try arena.create(Module);
-    mod.* = .{
-        .root = options.paths.root,
-        .root_src_path = options.paths.root_src_path,
-        .fully_qualified_name = options.fully_qualified_name,
-        .resolved_target = .{
-            .result = target,
-            .is_native_os = resolved_target.is_native_os,
-            .is_native_abi = resolved_target.is_native_abi,
-            .llvm_cpu_features = llvm_cpu_features,
-        },
-        .optimize_mode = optimize_mode,
-        .single_threaded = single_threaded,
-        .error_tracing = error_tracing,
-        .valgrind = valgrind,
-        .pic = pic,
-        .strip = strip,
-        .omit_frame_pointer = omit_frame_pointer,
-        .stack_check = stack_check,
-        .stack_protector = stack_protector,
-        .code_model = code_model,
-        .red_zone = red_zone,
-        .sanitize_c = sanitize_c,
-        .sanitize_thread = sanitize_thread,
-        .unwind_tables = unwind_tables,
-        .cc_argv = options.cc_argv,
-        .structured_cfg = structured_cfg,
-        .builtin_file = null,
-    };
-
-    try mod.deps.ensureUnusedCapacity(arena, 1);
-    mod.deps.putAssumeCapacityNoClobber("builtin", builtin_mod);
+    if (opt_builtin_mod) |builtin_mod| {
+        try mod.deps.ensureUnusedCapacity(arena, 1);
+        mod.deps.putAssumeCapacityNoClobber("builtin", builtin_mod);
+    }
 
     return mod;
 }
@@ -462,8 +466,10 @@ pub fn createLimited(gpa: Allocator, options: LimitedOptions) Allocator.Error!*P
     return mod;
 }
 
-pub fn getBuiltinDependency(m: *Module) *Module {
-    return m.deps.values()[0];
+pub fn getBuiltinDependency(m: Module) *Module {
+    const result = m.deps.values()[0];
+    assert(result.isBuiltin());
+    return result;
 }
 
 const Module = @This();
src/Compilation.zig
@@ -1410,7 +1410,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
                 .inherited = .{},
                 .global = options.config,
                 .parent = options.root_mod,
-                .builtin_mod = options.root_mod.deps.get("builtin").?,
+                .builtin_mod = options.root_mod.getBuiltinDependency(),
             });
 
             const zcu = try arena.create(Module);
@@ -3830,6 +3830,7 @@ pub fn obtainCObjectCacheManifest(
     // that apply both to @cImport and compiling C objects. No linking stuff here!
     // Also nothing that applies only to compiling .zig code.
     man.hash.add(owner_mod.sanitize_c);
+    man.hash.add(owner_mod.sanitize_thread);
     man.hash.addListOfBytes(owner_mod.cc_argv);
     man.hash.add(comp.config.link_libcpp);
 
@@ -3970,10 +3971,13 @@ pub fn cImport(comp: *Compilation, c_src: []const u8, owner_mod: *Package.Module
 
         const dep_basename = std.fs.path.basename(out_dep_path);
         try man.addDepFilePost(zig_cache_tmp_dir, dep_basename);
-        if (comp.whole_cache_manifest) |whole_cache_manifest| {
-            comp.whole_cache_manifest_mutex.lock();
-            defer comp.whole_cache_manifest_mutex.unlock();
-            try whole_cache_manifest.addDepFilePost(zig_cache_tmp_dir, dep_basename);
+        switch (comp.cache_use) {
+            .whole => |whole| if (whole.cache_manifest) |whole_cache_manifest| {
+                whole.cache_manifest_mutex.lock();
+                defer whole.cache_manifest_mutex.unlock();
+                try whole_cache_manifest.addDepFilePost(zig_cache_tmp_dir, dep_basename);
+            },
+            .incremental => {},
         }
 
         const digest = man.final();
@@ -4425,10 +4429,15 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: *std.P
             const dep_basename = std.fs.path.basename(dep_file_path);
             // Add the files depended on to the cache system.
             try man.addDepFilePost(zig_cache_tmp_dir, dep_basename);
-            if (comp.whole_cache_manifest) |whole_cache_manifest| {
-                comp.whole_cache_manifest_mutex.lock();
-                defer comp.whole_cache_manifest_mutex.unlock();
-                try whole_cache_manifest.addDepFilePost(zig_cache_tmp_dir, dep_basename);
+            switch (comp.cache_use) {
+                .whole => |whole| {
+                    if (whole.cache_manifest) |whole_cache_manifest| {
+                        whole.cache_manifest_mutex.lock();
+                        defer whole.cache_manifest_mutex.unlock();
+                        try whole_cache_manifest.addDepFilePost(zig_cache_tmp_dir, dep_basename);
+                    }
+                },
+                .incremental => {},
             }
             // Just to save disk space, we delete the file because it is never needed again.
             zig_cache_tmp_dir.deleteFile(dep_basename) catch |err| {
@@ -4724,10 +4733,13 @@ fn updateWin32Resource(comp: *Compilation, win32_resource: *Win32Resource, win32
         const dep_basename = std.fs.path.basename(out_dep_path);
         // Add the files depended on to the cache system.
         try man.addDepFilePost(zig_cache_tmp_dir, dep_basename);
-        if (comp.whole_cache_manifest) |whole_cache_manifest| {
-            comp.whole_cache_manifest_mutex.lock();
-            defer comp.whole_cache_manifest_mutex.unlock();
-            try whole_cache_manifest.addDepFilePost(zig_cache_tmp_dir, dep_basename);
+        switch (comp.cache_use) {
+            .whole => |whole| if (whole.cache_manifest) |whole_cache_manifest| {
+                whole.cache_manifest_mutex.lock();
+                defer whole.cache_manifest_mutex.unlock();
+                try whole_cache_manifest.addDepFilePost(zig_cache_tmp_dir, dep_basename);
+            },
+            .incremental => {},
         }
         // Just to save disk space, we delete the file because it is never needed again.
         zig_cache_tmp_dir.deleteFile(dep_basename) catch |err| {
@@ -4799,10 +4811,13 @@ fn updateWin32Resource(comp: *Compilation, win32_resource: *Win32Resource, win32
 
         for (dependencies_list.items) |dep_file_path| {
             try man.addFilePost(dep_file_path);
-            if (comp.whole_cache_manifest) |whole_cache_manifest| {
-                comp.whole_cache_manifest_mutex.lock();
-                defer comp.whole_cache_manifest_mutex.unlock();
-                try whole_cache_manifest.addFilePost(dep_file_path);
+            switch (comp.cache_use) {
+                .whole => |whole| if (whole.cache_manifest) |whole_cache_manifest| {
+                    whole.cache_manifest_mutex.lock();
+                    defer whole.cache_manifest_mutex.unlock();
+                    try whole_cache_manifest.addFilePost(dep_file_path);
+                },
+                .incremental => {},
             }
         }
 
@@ -6247,7 +6262,9 @@ pub fn build_crt_file(
     output_mode: std.builtin.OutputMode,
     misc_task_tag: MiscTask,
     prog_node: *std.Progress.Node,
-    c_source_files: []const CSourceFile,
+    /// These elements have to get mutated to add the owner module after it is
+    /// created within this function.
+    c_source_files: []CSourceFile,
 ) !void {
     const tracy_trace = trace(@src());
     defer tracy_trace.end();
@@ -6305,6 +6322,10 @@ pub fn build_crt_file(
         .builtin_mod = null,
     });
 
+    for (c_source_files) |*item| {
+        item.owner = root_mod;
+    }
+
     const sub_compilation = try Compilation.create(gpa, .{
         .local_cache_directory = comp.global_cache_directory,
         .global_cache_directory = comp.global_cache_directory,
src/glibc.zig
@@ -170,7 +170,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
     defer arena_allocator.deinit();
     const arena = arena_allocator.allocator();
 
-    const target = comp.getTarget();
+    const target = comp.root_mod.resolved_target.result;
     const target_ver = target.os.version_range.linux.glibc;
     const start_old_init_fini = target_ver.order(.{ .major = 2, .minor = 33, .patch = 0 }) != .gt;
 
@@ -196,12 +196,14 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
                 "-DASSEMBLER",
                 "-Wa,--noexecstack",
             });
-            return comp.build_crt_file("crti", .Obj, .@"glibc crti.o", prog_node, &.{
+            var files = [_]Compilation.CSourceFile{
                 .{
                     .src_path = try start_asm_path(comp, arena, "crti.S"),
                     .cache_exempt_flags = args.items,
+                    .owner = comp.root_mod,
                 },
-            });
+            };
+            return comp.build_crt_file("crti", .Obj, .@"glibc crti.o", prog_node, &files);
         },
         .crtn_o => {
             var args = std.ArrayList([]const u8).init(arena);
@@ -215,12 +217,14 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
                 "-DASSEMBLER",
                 "-Wa,--noexecstack",
             });
-            return comp.build_crt_file("crtn", .Obj, .@"glibc crtn.o", prog_node, &.{
+            var files = [_]Compilation.CSourceFile{
                 .{
                     .src_path = try start_asm_path(comp, arena, "crtn.S"),
                     .cache_exempt_flags = args.items,
+                    .owner = undefined,
                 },
-            });
+            };
+            return comp.build_crt_file("crtn", .Obj, .@"glibc crtn.o", prog_node, &files);
         },
         .scrt1_o => {
             const start_o: Compilation.CSourceFile = blk: {
@@ -244,6 +248,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
                 break :blk .{
                     .src_path = try start_asm_path(comp, arena, src_path),
                     .cache_exempt_flags = args.items,
+                    .owner = undefined,
                 };
             };
             const abi_note_o: Compilation.CSourceFile = blk: {
@@ -263,11 +268,11 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
                 break :blk .{
                     .src_path = try lib_path(comp, arena, lib_libc_glibc ++ "csu" ++ path.sep_str ++ "abi-note.S"),
                     .cache_exempt_flags = args.items,
+                    .owner = undefined,
                 };
             };
-            return comp.build_crt_file("Scrt1", .Obj, .@"glibc Scrt1.o", prog_node, &.{
-                start_o, abi_note_o,
-            });
+            var files = [_]Compilation.CSourceFile{ start_o, abi_note_o };
+            return comp.build_crt_file("Scrt1", .Obj, .@"glibc Scrt1.o", prog_node, &files);
         },
         .libc_nonshared_a => {
             const s = path.sep_str;
@@ -364,6 +369,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
                 files_buf[files_index] = .{
                     .src_path = try lib_path(comp, arena, dep.path),
                     .cache_exempt_flags = args.items,
+                    .owner = undefined,
                 };
                 files_index += 1;
             }
@@ -1065,6 +1071,7 @@ fn buildSharedLib(
     const c_source_files = [1]Compilation.CSourceFile{
         .{
             .src_path = try path.join(arena, &[_][]const u8{ bin_directory.path.?, asm_file_basename }),
+            .owner = undefined,
         },
     };
 
src/libcxx.zig
@@ -138,6 +138,50 @@ pub fn buildLibCXX(comp: *Compilation, prog_node: *std.Progress.Node) !void {
     const abi_namespace_arg = try std.fmt.allocPrint(arena, "-D_LIBCPP_ABI_NAMESPACE=__{d}", .{
         @intFromEnum(comp.libcxx_abi_version),
     });
+
+    const optimize_mode = comp.compilerRtOptMode();
+    const strip = comp.compilerRtStrip();
+
+    const config = try Compilation.Config.resolve(.{
+        .output_mode = output_mode,
+        .link_mode = link_mode,
+        .resolved_target = comp.root_mod.resolved_target,
+        .is_test = false,
+        .have_zcu = false,
+        .emit_bin = true,
+        .root_optimize_mode = optimize_mode,
+        .root_strip = strip,
+        .link_libc = true,
+        .lto = comp.config.lto,
+    });
+
+    const root_mod = try Module.create(arena, .{
+        .global_cache_directory = comp.global_cache_directory,
+        .paths = .{
+            .root = .{ .root_dir = comp.zig_lib_directory },
+            .root_src_path = "",
+        },
+        .fully_qualified_name = "root",
+        .inherited = .{
+            .resolved_target = comp.root_mod.resolved_target,
+            .strip = strip,
+            .stack_check = false,
+            .stack_protector = 0,
+            .sanitize_c = false,
+            .sanitize_thread = comp.config.any_sanitize_thread,
+            .red_zone = comp.root_mod.red_zone,
+            .omit_frame_pointer = comp.root_mod.omit_frame_pointer,
+            .valgrind = false,
+            .optimize_mode = optimize_mode,
+            .structured_cfg = comp.root_mod.structured_cfg,
+            .pic = comp.root_mod.pic,
+        },
+        .global = config,
+        .cc_argv = &.{},
+        .parent = null,
+        .builtin_mod = null,
+    });
+
     var c_source_files = try std.ArrayList(Compilation.CSourceFile).initCapacity(arena, libcxx_files.len);
 
     for (libcxx_files) |cxx_src| {
@@ -224,52 +268,10 @@ pub fn buildLibCXX(comp: *Compilation, prog_node: *std.Progress.Node) !void {
             .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ "libcxx", cxx_src }),
             .extra_flags = cflags.items,
             .cache_exempt_flags = cache_exempt_flags.items,
+            .owner = root_mod,
         });
     }
 
-    const optimize_mode = comp.compilerRtOptMode();
-    const strip = comp.compilerRtStrip();
-
-    const config = try Compilation.Config.resolve(.{
-        .output_mode = output_mode,
-        .link_mode = link_mode,
-        .resolved_target = comp.root_mod.resolved_target,
-        .is_test = false,
-        .have_zcu = false,
-        .emit_bin = true,
-        .root_optimize_mode = optimize_mode,
-        .root_strip = strip,
-        .link_libc = true,
-        .lto = comp.config.lto,
-    });
-
-    const root_mod = try Module.create(arena, .{
-        .global_cache_directory = comp.global_cache_directory,
-        .paths = .{
-            .root = .{ .root_dir = comp.zig_lib_directory },
-            .root_src_path = "",
-        },
-        .fully_qualified_name = "root",
-        .inherited = .{
-            .resolved_target = comp.root_mod.resolved_target,
-            .strip = strip,
-            .stack_check = false,
-            .stack_protector = 0,
-            .sanitize_c = false,
-            .sanitize_thread = comp.config.any_sanitize_thread,
-            .red_zone = comp.root_mod.red_zone,
-            .omit_frame_pointer = comp.root_mod.omit_frame_pointer,
-            .valgrind = false,
-            .optimize_mode = optimize_mode,
-            .structured_cfg = comp.root_mod.structured_cfg,
-            .pic = comp.root_mod.pic,
-        },
-        .global = config,
-        .cc_argv = &.{},
-        .parent = null,
-        .builtin_mod = null,
-    });
-
     const sub_compilation = try Compilation.create(comp.gpa, .{
         .local_cache_directory = comp.global_cache_directory,
         .global_cache_directory = comp.global_cache_directory,
@@ -339,6 +341,53 @@ pub fn buildLibCXXABI(comp: *Compilation, prog_node: *std.Progress.Node) !void {
     const abi_namespace_arg = try std.fmt.allocPrint(arena, "-D_LIBCPP_ABI_NAMESPACE=__{d}", .{
         @intFromEnum(comp.libcxx_abi_version),
     });
+
+    const optimize_mode = comp.compilerRtOptMode();
+    const strip = comp.compilerRtStrip();
+    const unwind_tables = true;
+
+    const config = try Compilation.Config.resolve(.{
+        .output_mode = output_mode,
+        .link_mode = link_mode,
+        .resolved_target = comp.root_mod.resolved_target,
+        .is_test = false,
+        .have_zcu = false,
+        .emit_bin = true,
+        .root_optimize_mode = optimize_mode,
+        .root_strip = strip,
+        .link_libc = true,
+        .any_unwind_tables = unwind_tables,
+        .lto = comp.config.lto,
+    });
+
+    const root_mod = try Module.create(arena, .{
+        .global_cache_directory = comp.global_cache_directory,
+        .paths = .{
+            .root = .{ .root_dir = comp.zig_lib_directory },
+            .root_src_path = "",
+        },
+        .fully_qualified_name = "root",
+        .inherited = .{
+            .resolved_target = comp.root_mod.resolved_target,
+            .strip = strip,
+            .stack_check = false,
+            .stack_protector = 0,
+            .sanitize_c = false,
+            .sanitize_thread = comp.config.any_sanitize_thread,
+            .red_zone = comp.root_mod.red_zone,
+            .omit_frame_pointer = comp.root_mod.omit_frame_pointer,
+            .valgrind = false,
+            .optimize_mode = optimize_mode,
+            .structured_cfg = comp.root_mod.structured_cfg,
+            .unwind_tables = unwind_tables,
+            .pic = comp.root_mod.pic,
+        },
+        .global = config,
+        .cc_argv = &.{},
+        .parent = null,
+        .builtin_mod = null,
+    });
+
     var c_source_files = try std.ArrayList(Compilation.CSourceFile).initCapacity(arena, libcxxabi_files.len);
 
     for (libcxxabi_files) |cxxabi_src| {
@@ -406,55 +455,10 @@ pub fn buildLibCXXABI(comp: *Compilation, prog_node: *std.Progress.Node) !void {
             .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ "libcxxabi", cxxabi_src }),
             .extra_flags = cflags.items,
             .cache_exempt_flags = cache_exempt_flags.items,
+            .owner = root_mod,
         });
     }
 
-    const optimize_mode = comp.compilerRtOptMode();
-    const strip = comp.compilerRtStrip();
-    const unwind_tables = true;
-
-    const config = try Compilation.Config.resolve(.{
-        .output_mode = output_mode,
-        .link_mode = link_mode,
-        .resolved_target = comp.root_mod.resolved_target,
-        .is_test = false,
-        .have_zcu = false,
-        .emit_bin = true,
-        .root_optimize_mode = optimize_mode,
-        .root_strip = strip,
-        .link_libc = true,
-        .any_unwind_tables = unwind_tables,
-        .lto = comp.config.lto,
-    });
-
-    const root_mod = try Module.create(arena, .{
-        .global_cache_directory = comp.global_cache_directory,
-        .paths = .{
-            .root = .{ .root_dir = comp.zig_lib_directory },
-            .root_src_path = "",
-        },
-        .fully_qualified_name = "root",
-        .inherited = .{
-            .resolved_target = comp.root_mod.resolved_target,
-            .strip = strip,
-            .stack_check = false,
-            .stack_protector = 0,
-            .sanitize_c = false,
-            .sanitize_thread = comp.config.any_sanitize_thread,
-            .red_zone = comp.root_mod.red_zone,
-            .omit_frame_pointer = comp.root_mod.omit_frame_pointer,
-            .valgrind = false,
-            .optimize_mode = optimize_mode,
-            .structured_cfg = comp.root_mod.structured_cfg,
-            .unwind_tables = unwind_tables,
-            .pic = comp.root_mod.pic,
-        },
-        .global = config,
-        .cc_argv = &.{},
-        .parent = null,
-        .builtin_mod = null,
-    });
-
     const sub_compilation = try Compilation.create(comp.gpa, .{
         .local_cache_directory = comp.global_cache_directory,
         .global_cache_directory = comp.global_cache_directory,
src/libtsan.zig
@@ -34,6 +34,52 @@ pub fn buildTsan(comp: *Compilation, prog_node: *std.Progress.Node) !void {
         .basename = basename,
     };
 
+    const optimize_mode = comp.compilerRtOptMode();
+    const strip = comp.compilerRtStrip();
+
+    const config = try Compilation.Config.resolve(.{
+        .output_mode = output_mode,
+        .link_mode = link_mode,
+        .resolved_target = comp.root_mod.resolved_target,
+        .is_test = false,
+        .have_zcu = false,
+        .emit_bin = true,
+        .root_optimize_mode = optimize_mode,
+        .root_strip = strip,
+        .link_libc = true,
+    });
+
+    const common_flags = [_][]const u8{
+        "-DTSAN_CONTAINS_UBSAN=0",
+    };
+
+    const root_mod = try Module.create(arena, .{
+        .global_cache_directory = comp.global_cache_directory,
+        .paths = .{
+            .root = .{ .root_dir = comp.zig_lib_directory },
+            .root_src_path = "",
+        },
+        .fully_qualified_name = "root",
+        .inherited = .{
+            .resolved_target = comp.root_mod.resolved_target,
+            .strip = strip,
+            .stack_check = false,
+            .stack_protector = 0,
+            .sanitize_c = false,
+            .sanitize_thread = false,
+            .red_zone = comp.root_mod.red_zone,
+            .omit_frame_pointer = comp.root_mod.omit_frame_pointer,
+            .valgrind = false,
+            .optimize_mode = optimize_mode,
+            .structured_cfg = comp.root_mod.structured_cfg,
+            .pic = true,
+        },
+        .global = config,
+        .cc_argv = &common_flags,
+        .parent = null,
+        .builtin_mod = null,
+    });
+
     var c_source_files = std.ArrayList(Compilation.CSourceFile).init(arena);
     try c_source_files.ensureUnusedCapacity(tsan_sources.len);
 
@@ -50,8 +96,9 @@ pub fn buildTsan(comp: *Compilation, prog_node: *std.Progress.Node) !void {
         try cflags.append("-fno-rtti");
 
         c_source_files.appendAssumeCapacity(.{
-            .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ "tsan", tsan_src }),
+            .src_path = try comp.zig_lib_directory.join(arena, &.{ "tsan", tsan_src }),
             .extra_flags = cflags.items,
+            .owner = root_mod,
         });
     }
 
@@ -74,6 +121,7 @@ pub fn buildTsan(comp: *Compilation, prog_node: *std.Progress.Node) !void {
         c_source_files.appendAssumeCapacity(.{
             .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ "tsan", tsan_src }),
             .extra_flags = cflags.items,
+            .owner = root_mod,
         });
     }
     {
@@ -94,6 +142,7 @@ pub fn buildTsan(comp: *Compilation, prog_node: *std.Progress.Node) !void {
         c_source_files.appendAssumeCapacity(.{
             .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ "tsan", asm_source }),
             .extra_flags = cflags.items,
+            .owner = root_mod,
         });
     }
 
@@ -117,6 +166,7 @@ pub fn buildTsan(comp: *Compilation, prog_node: *std.Progress.Node) !void {
                 "tsan", "sanitizer_common", common_src,
             }),
             .extra_flags = cflags.items,
+            .owner = root_mod,
         });
     }
 
@@ -141,6 +191,7 @@ pub fn buildTsan(comp: *Compilation, prog_node: *std.Progress.Node) !void {
                 "tsan", "sanitizer_common", c_src,
             }),
             .extra_flags = cflags.items,
+            .owner = root_mod,
         });
     }
 
@@ -161,6 +212,7 @@ pub fn buildTsan(comp: *Compilation, prog_node: *std.Progress.Node) !void {
                 "tsan", "sanitizer_common", c_src,
             }),
             .extra_flags = cflags.items,
+            .owner = root_mod,
         });
     }
 
@@ -189,56 +241,10 @@ pub fn buildTsan(comp: *Compilation, prog_node: *std.Progress.Node) !void {
                 "tsan", "interception", c_src,
             }),
             .extra_flags = cflags.items,
+            .owner = root_mod,
         });
     }
 
-    const common_flags = [_][]const u8{
-        "-DTSAN_CONTAINS_UBSAN=0",
-    };
-
-    const optimize_mode = comp.compilerRtOptMode();
-    const strip = comp.compilerRtStrip();
-
-    const config = try Compilation.Config.resolve(.{
-        .output_mode = output_mode,
-        .link_mode = link_mode,
-        .resolved_target = comp.root_mod.resolved_target,
-        .is_test = false,
-        .have_zcu = false,
-        .emit_bin = true,
-        .root_optimize_mode = optimize_mode,
-        .root_strip = strip,
-        .link_libc = true,
-    });
-
-    const root_mod = try Module.create(arena, .{
-        .global_cache_directory = comp.global_cache_directory,
-        .paths = .{
-            .root = .{ .root_dir = comp.zig_lib_directory },
-            .root_src_path = "",
-        },
-        .fully_qualified_name = "root",
-        .inherited = .{
-            .resolved_target = comp.root_mod.resolved_target,
-            .strip = strip,
-            .stack_check = false,
-            .stack_protector = 0,
-            .sanitize_c = false,
-            .sanitize_thread = false,
-            .red_zone = comp.root_mod.red_zone,
-            .omit_frame_pointer = comp.root_mod.omit_frame_pointer,
-            .valgrind = false,
-            .optimize_mode = optimize_mode,
-            .structured_cfg = comp.root_mod.structured_cfg,
-            .pic = true,
-            .cc_argv = &common_flags,
-        },
-        .global = config,
-        .cc_argv = &.{},
-        .parent = null,
-        .builtin_mod = null,
-    });
-
     const sub_compilation = try Compilation.create(comp.gpa, .{
         .local_cache_directory = comp.global_cache_directory,
         .global_cache_directory = comp.global_cache_directory,
src/libunwind.zig
@@ -33,7 +33,8 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: *std.Progress.Node) !void {
         // Disable LTO to avoid https://github.com/llvm/llvm-project/issues/56825
         .lto = false,
     });
-    const root_mod = Module.create(.{
+    const root_mod = try Module.create(arena, .{
+        .global_cache_directory = comp.global_cache_directory,
         .paths = .{
             .root = .{ .root_dir = comp.zig_lib_directory },
             .root_src_path = "",
@@ -55,6 +56,8 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: *std.Progress.Node) !void {
         },
         .global = config,
         .cc_argv = &.{},
+        .parent = null,
+        .builtin_mod = null,
     });
 
     const root_name = "unwind";
@@ -117,6 +120,7 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: *std.Progress.Node) !void {
         c_source_files[i] = .{
             .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{unwind_src}),
             .extra_flags = cflags.items,
+            .owner = root_mod,
         };
     }
     const sub_compilation = try Compilation.create(comp.gpa, .{
@@ -132,7 +136,6 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: *std.Progress.Node) !void {
         .thread_pool = comp.thread_pool,
         .libc_installation = comp.libc_installation,
         .emit_bin = emit_bin,
-        .link_mode = link_mode,
         .function_sections = comp.function_sections,
         .c_source_files = &c_source_files,
         .verbose_cc = comp.verbose_cc,
@@ -150,8 +153,7 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: *std.Progress.Node) !void {
     try comp.updateSubCompilation(sub_compilation, .libunwind, prog_node);
 
     assert(comp.libunwind_static_lib == null);
-
-    comp.libunwind_static_lib = try sub_compilation.toOwnedLock();
+    comp.libunwind_static_lib = try sub_compilation.toCrtFile();
 }
 
 const unwind_src_list = [_][]const u8{
src/main.zig
@@ -3481,7 +3481,7 @@ fn buildOutputType(
     defer if (!comp_destroyed) comp.destroy();
 
     if (show_builtin) {
-        const builtin_mod = comp.root_mod.deps.get("builtin").?;
+        const builtin_mod = comp.root_mod.getBuiltinDependency();
         const source = builtin_mod.builtin_file.?.source;
         return std.io.getStdOut().writeAll(source);
     }
src/mingw.zig
@@ -41,14 +41,16 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
                 //"-D_UNICODE",
                 //"-DWPRFLAG=1",
             });
-            return comp.build_crt_file("crt2", .Obj, .@"mingw-w64 crt2.o", prog_node, &.{
+            var files = [_]Compilation.CSourceFile{
                 .{
                     .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
                         "libc", "mingw", "crt", "crtexe.c",
                     }),
                     .extra_flags = args.items,
+                    .owner = undefined,
                 },
-            });
+            };
+            return comp.build_crt_file("crt2", .Obj, .@"mingw-w64 crt2.o", prog_node, &files);
         },
 
         .dllcrt2_o => {
@@ -60,14 +62,16 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
                 "-U__CRTDLL__",
                 "-D__MSVCRT__",
             });
-            return comp.build_crt_file("dllcrt2", .Obj, .@"mingw-w64 dllcrt2.o", prog_node, &.{
+            var files = [_]Compilation.CSourceFile{
                 .{
                     .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
                         "libc", "mingw", "crt", "crtdll.c",
                     }),
                     .extra_flags = args.items,
+                    .owner = undefined,
                 },
-            });
+            };
+            return comp.build_crt_file("dllcrt2", .Obj, .@"mingw-w64 dllcrt2.o", prog_node, &files);
         },
 
         .mingw32_lib => {
@@ -97,6 +101,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
                         "libc", "mingw", "crt", dep,
                     }),
                     .extra_flags = args.items,
+                    .owner = undefined,
                 };
             }
             return comp.build_crt_file("mingw32", .Lib, .@"mingw-w64 mingw32.lib", prog_node, &c_source_files);
@@ -125,6 +130,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
                 (try c_source_files.addOne()).* = .{
                     .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ "libc", "mingw", dep }),
                     .extra_flags = extra_flags,
+                    .owner = undefined,
                 };
             }
             if (comp.getTarget().cpu.arch == .x86) {
@@ -134,6 +140,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
                             "libc", "mingw", dep,
                         }),
                         .extra_flags = extra_flags,
+                        .owner = undefined,
                     };
                 }
             } else {
@@ -143,6 +150,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
                             "libc", "mingw", dep,
                         }),
                         .extra_flags = extra_flags,
+                        .owner = undefined,
                     };
                 }
             }
@@ -175,6 +183,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
                         "libc", "mingw", dep,
                     }),
                     .extra_flags = extra_flags,
+                    .owner = undefined,
                 };
             }
             const target = comp.getTarget();
@@ -185,6 +194,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
                             "libc", "mingw", dep,
                         }),
                         .extra_flags = extra_flags,
+                        .owner = undefined,
                     };
                 }
             } else if (target.cpu.arch.isARM()) {
@@ -194,6 +204,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
                             "libc", "mingw", dep,
                         }),
                         .extra_flags = extra_flags,
+                        .owner = undefined,
                     };
                 }
             } else if (target.cpu.arch.isAARCH64()) {
@@ -203,6 +214,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
                             "libc", "mingw", dep,
                         }),
                         .extra_flags = extra_flags,
+                        .owner = undefined,
                     };
                 }
             } else {
@@ -238,6 +250,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
                         "libc", "mingw", "libsrc", dep,
                     }),
                     .extra_flags = extra_flags,
+                    .owner = undefined,
                 };
             }
             return comp.build_crt_file("uuid", .Lib, .@"mingw-w64 uuid.lib", prog_node, &c_source_files);
src/Module.zig
@@ -5275,7 +5275,7 @@ pub fn populateTestFunctions(
 ) !void {
     const gpa = mod.gpa;
     const ip = &mod.intern_pool;
-    const builtin_mod = mod.main_mod.deps.get("builtin").?;
+    const builtin_mod = mod.main_mod.getBuiltinDependency();
     const builtin_file = (mod.importPkg(builtin_mod) catch unreachable).file;
     const root_decl = mod.declPtr(builtin_file.root_decl.unwrap().?);
     const builtin_namespace = mod.namespacePtr(root_decl.src_namespace);
src/musl.zig
@@ -34,12 +34,14 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
             try args.appendSlice(&[_][]const u8{
                 "-Qunused-arguments",
             });
-            return comp.build_crt_file("crti", .Obj, .@"musl crti.o", prog_node, &.{
+            var files = [_]Compilation.CSourceFile{
                 .{
                     .src_path = try start_asm_path(comp, arena, "crti.s"),
                     .extra_flags = args.items,
+                    .owner = undefined,
                 },
-            });
+            };
+            return comp.build_crt_file("crti", .Obj, .@"musl crti.o", prog_node, &files);
         },
         .crtn_o => {
             var args = std.ArrayList([]const u8).init(arena);
@@ -47,12 +49,14 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
             try args.appendSlice(&[_][]const u8{
                 "-Qunused-arguments",
             });
-            return comp.build_crt_file("crtn", .Obj, .@"musl crtn.o", prog_node, &.{
+            var files = [_]Compilation.CSourceFile{
                 .{
                     .src_path = try start_asm_path(comp, arena, "crtn.s"),
                     .extra_flags = args.items,
+                    .owner = undefined,
                 },
-            });
+            };
+            return comp.build_crt_file("crtn", .Obj, .@"musl crtn.o", prog_node, &files);
         },
         .crt1_o => {
             var args = std.ArrayList([]const u8).init(arena);
@@ -61,14 +65,16 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
                 "-fno-stack-protector",
                 "-DCRT",
             });
-            return comp.build_crt_file("crt1", .Obj, .@"musl crt1.o", prog_node, &.{
+            var files = [_]Compilation.CSourceFile{
                 .{
                     .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
                         "libc", "musl", "crt", "crt1.c",
                     }),
                     .extra_flags = args.items,
+                    .owner = undefined,
                 },
-            });
+            };
+            return comp.build_crt_file("crt1", .Obj, .@"musl crt1.o", prog_node, &files);
         },
         .rcrt1_o => {
             var args = std.ArrayList([]const u8).init(arena);
@@ -78,14 +84,16 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
                 "-fno-stack-protector",
                 "-DCRT",
             });
-            return comp.build_crt_file("rcrt1", .Obj, .@"musl rcrt1.o", prog_node, &.{
+            var files = [_]Compilation.CSourceFile{
                 .{
                     .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
                         "libc", "musl", "crt", "rcrt1.c",
                     }),
                     .extra_flags = args.items,
+                    .owner = undefined,
                 },
-            });
+            };
+            return comp.build_crt_file("rcrt1", .Obj, .@"musl rcrt1.o", prog_node, &files);
         },
         .scrt1_o => {
             var args = std.ArrayList([]const u8).init(arena);
@@ -95,14 +103,16 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
                 "-fno-stack-protector",
                 "-DCRT",
             });
-            return comp.build_crt_file("Scrt1", .Obj, .@"musl Scrt1.o", prog_node, &.{
+            var files = [_]Compilation.CSourceFile{
                 .{
                     .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
                         "libc", "musl", "crt", "Scrt1.c",
                     }),
                     .extra_flags = args.items,
+                    .owner = undefined,
                 },
-            });
+            };
+            return comp.build_crt_file("Scrt1", .Obj, .@"musl Scrt1.o", prog_node, &files);
         },
         .libc_a => {
             // When there is a src/<arch>/foo.* then it should substitute for src/foo.*
@@ -186,6 +196,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
                 c_source_file.* = .{
                     .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ "libc", src_file }),
                     .extra_flags = args.items,
+                    .owner = undefined,
                 };
             }
             return comp.build_crt_file("c", .Lib, .@"musl libc.a", prog_node, c_source_files.items);
@@ -235,7 +246,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
                     .structured_cfg = comp.root_mod.structured_cfg,
                 },
                 .global = config,
-                .cc_argv = &.{},
+                .cc_argv = cc_argv,
                 .parent = null,
                 .builtin_mod = null,
             });
@@ -261,9 +272,11 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
                 .verbose_llvm_cpu_features = comp.verbose_llvm_cpu_features,
                 .clang_passthrough_mode = comp.clang_passthrough_mode,
                 .c_source_files = &[_]Compilation.CSourceFile{
-                    .{ .src_path = try comp.zig_lib_directory.join(arena, &.{ "libc", "musl", "libc.S" }) },
+                    .{
+                        .src_path = try comp.zig_lib_directory.join(arena, &.{ "libc", "musl", "libc.S" }),
+                        .owner = root_mod,
+                    },
                 },
-                .cc_argv = cc_argv,
                 .skip_linker_dependencies = true,
                 .soname = "libc.so",
             });
src/Sema.zig
@@ -5759,14 +5759,39 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr
         };
         return sema.failWithOwnedErrorMsg(&child_block, msg);
     }
-    const c_import_mod = try Package.Module.create(comp.arena.allocator(), .{
-        .root = .{
-            .root_dir = Compilation.Directory.cwd(),
-            .sub_path = std.fs.path.dirname(c_import_res.out_zig_path) orelse "",
+    const parent_mod = parent_block.ownerModule();
+    const c_import_mod = Package.Module.create(comp.arena.allocator(), .{
+        .global_cache_directory = comp.global_cache_directory,
+        .paths = .{
+            .root = .{
+                .root_dir = Compilation.Directory.cwd(),
+                .sub_path = std.fs.path.dirname(c_import_res.out_zig_path) orelse "",
+            },
+            .root_src_path = std.fs.path.basename(c_import_res.out_zig_path),
         },
-        .root_src_path = std.fs.path.basename(c_import_res.out_zig_path),
         .fully_qualified_name = c_import_res.out_zig_path,
-    });
+        .cc_argv = parent_mod.cc_argv,
+        .inherited = .{},
+        .global = comp.config,
+        .parent = parent_mod,
+        .builtin_mod = parent_mod.getBuiltinDependency(),
+    }) catch |err| switch (err) {
+        // None of these are possible because we are creating a package with
+        // the exact same configuration as the parent package, which already
+        // passed these checks.
+        error.ValgrindUnsupportedOnTarget => unreachable,
+        error.TargetRequiresSingleThreaded => unreachable,
+        error.BackendRequiresSingleThreaded => unreachable,
+        error.TargetRequiresPic => unreachable,
+        error.PieRequiresPic => unreachable,
+        error.DynamicLinkingRequiresPic => unreachable,
+        error.TargetHasNoRedZone => unreachable,
+        error.StackCheckUnsupportedByTarget => unreachable,
+        error.StackProtectorUnsupportedByTarget => unreachable,
+        error.StackProtectorUnavailableWithoutLibC => unreachable,
+
+        else => |e| return e,
+    };
 
     const result = mod.importPkg(c_import_mod) catch |err|
         return sema.fail(&child_block, src, "C import failed: {s}", .{@errorName(err)});
src/wasi_libc.zig
@@ -74,27 +74,31 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
             var args = std.ArrayList([]const u8).init(arena);
             try addCCArgs(comp, arena, &args, .{});
             try addLibcBottomHalfIncludes(comp, arena, &args);
-            return comp.build_crt_file("crt1-reactor", .Obj, .@"wasi crt1-reactor.o", prog_node, &.{
+            var files = [_]Compilation.CSourceFile{
                 .{
                     .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
                         "libc", try sanitize(arena, crt1_reactor_src_file),
                     }),
                     .extra_flags = args.items,
+                    .owner = undefined,
                 },
-            });
+            };
+            return comp.build_crt_file("crt1-reactor", .Obj, .@"wasi crt1-reactor.o", prog_node, &files);
         },
         .crt1_command_o => {
             var args = std.ArrayList([]const u8).init(arena);
             try addCCArgs(comp, arena, &args, .{});
             try addLibcBottomHalfIncludes(comp, arena, &args);
-            return comp.build_crt_file("crt1-command", .Obj, .@"wasi crt1-command.o", prog_node, &.{
+            var files = [_]Compilation.CSourceFile{
                 .{
                     .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
                         "libc", try sanitize(arena, crt1_command_src_file),
                     }),
                     .extra_flags = args.items,
+                    .owner = undefined,
                 },
-            });
+            };
+            return comp.build_crt_file("crt1-command", .Obj, .@"wasi crt1-command.o", prog_node, &files);
         },
         .libc_a => {
             var libc_sources = std.ArrayList(Compilation.CSourceFile).init(arena);
@@ -109,6 +113,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
                             "libc", try sanitize(arena, file_path),
                         }),
                         .extra_flags = args.items,
+                        .owner = undefined,
                     });
                 }
             }
@@ -125,6 +130,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
                             "libc", try sanitize(arena, file_path),
                         }),
                         .extra_flags = args.items,
+                        .owner = undefined,
                     });
                 }
             }
@@ -141,6 +147,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
                             "libc", try sanitize(arena, file_path),
                         }),
                         .extra_flags = args.items,
+                        .owner = undefined,
                     });
                 }
             }
@@ -159,6 +166,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
                         "libc", try sanitize(arena, file_path),
                     }),
                     .extra_flags = args.items,
+                    .owner = undefined,
                 });
             }
             try comp.build_crt_file("wasi-emulated-process-clocks", .Lib, .@"libwasi-emulated-process-clocks.a", prog_node, emu_clocks_sources.items);
@@ -175,6 +183,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
                         "libc", try sanitize(arena, file_path),
                     }),
                     .extra_flags = args.items,
+                    .owner = undefined,
                 });
             }
             try comp.build_crt_file("wasi-emulated-getpid", .Lib, .@"libwasi-emulated-getpid.a", prog_node, emu_getpid_sources.items);
@@ -191,6 +200,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
                         "libc", try sanitize(arena, file_path),
                     }),
                     .extra_flags = args.items,
+                    .owner = undefined,
                 });
             }
             try comp.build_crt_file("wasi-emulated-mman", .Lib, .@"libwasi-emulated-mman.a", prog_node, emu_mman_sources.items);
@@ -208,6 +218,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
                             "libc", try sanitize(arena, file_path),
                         }),
                         .extra_flags = args.items,
+                        .owner = undefined,
                     });
                 }
             }
@@ -224,6 +235,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
                             "libc", try sanitize(arena, file_path),
                         }),
                         .extra_flags = args.items,
+                        .owner = undefined,
                     });
                 }
             }