Commit 509da2316c

Jakub Konka <kubkon@jakubkonka.com>
2023-10-02 17:55:27
elf: run populateMissingMetadata only if ZigModule exists
1 parent f0f19e1
Changed files (1)
src
link
src/link/Elf.zig
@@ -231,11 +231,34 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option
             self.dwarf = Dwarf.init(allocator, &self.base, options.target);
         }
 
+        const index = @as(File.Index, @intCast(try self.files.addOne(allocator)));
+        self.files.set(index, .{ .zig_module = .{
+            .index = index,
+            .path = options.module.?.main_pkg.root_src_path,
+        } });
+        self.zig_module_index = index;
+        const zig_module = self.file(index).?.zig_module;
+
+        try zig_module.atoms.append(allocator, 0); // null input section
+
+        const name_off = try self.strtab.insert(allocator, std.fs.path.stem(options.module.?.main_mod.root_src_path));
+        const symbol_index = try self.addSymbol();
+        try zig_module.local_symbols.append(allocator, symbol_index);
+        const symbol_ptr = self.symbol(symbol_index);
+        symbol_ptr.file_index = zig_module.index;
+        symbol_ptr.name_offset = name_off;
+
+        const esym_index = try zig_module.addLocalEsym(allocator);
+        const esym = &zig_module.local_esyms.items[esym_index];
+        esym.st_name = name_off;
+        esym.st_info |= elf.STT_FILE;
+        esym.st_shndx = elf.SHN_ABS;
+        symbol_ptr.esym_index = esym_index;
+
         // TODO move populateMissingMetadata here renamed to zig_module.initMetadata();
+        try self.populateMissingMetadata();
     }
 
-    try self.populateMissingMetadata();
-
     return self;
 }
 
@@ -841,32 +864,6 @@ pub fn populateMissingMetadata(self: *Elf) !void {
             self.debug_line_header_dirty = true;
         }
     }
-
-    if (self.base.options.module) |module| {
-        if (self.zig_module_index == null and !self.base.options.use_llvm) {
-            const index: File.Index = @intCast(try self.files.addOne(gpa));
-            self.files.set(index, .{ .zig_module = .{
-                .index = index,
-                .path = module.main_mod.root_src_path,
-            } });
-            self.zig_module_index = index;
-            const zig_module = self.file(index).?.zig_module;
-            try zig_module.atoms.append(gpa, 0); // null input section
-            const name_off = try self.strtab.insert(gpa, std.fs.path.stem(module.main_mod.root_src_path));
-            const symbol_index = try self.addSymbol();
-            try zig_module.local_symbols.append(gpa, symbol_index);
-            const symbol_ptr = self.symbol(symbol_index);
-            symbol_ptr.file_index = zig_module.index;
-            symbol_ptr.name_offset = name_off;
-
-            const esym_index = try zig_module.addLocalEsym(gpa);
-            const esym = &zig_module.local_esyms.items[esym_index];
-            esym.st_name = name_off;
-            esym.st_info |= elf.STT_FILE;
-            esym.st_shndx = elf.SHN_ABS;
-            symbol_ptr.esym_index = esym_index;
-        }
-    }
 }
 
 pub fn growAllocSection(self: *Elf, shdr_index: u16, needed_size: u64) !void {