Commit b1a735ac65

Jakub Konka <kubkon@jakubkonka.com>
2023-10-30 19:44:27
elf: put init logic of ZigObject in init function
1 parent 9bdbb63
Changed files (2)
src/link/Elf/ZigObject.zig
@@ -56,6 +56,30 @@ pub const global_symbol_bit: u32 = 0x80000000;
 pub const symbol_mask: u32 = 0x7fffffff;
 pub const SHN_ATOM: u16 = 0x100;
 
+pub fn init(self: *ZigObject, elf_file: *Elf) !void {
+    const gpa = elf_file.base.allocator;
+
+    try self.atoms.append(gpa, 0); // null input section
+
+    const name_off = try elf_file.strtab.insert(gpa, std.fs.path.stem(self.path));
+    const symbol_index = try elf_file.addSymbol();
+    try self.local_symbols.append(gpa, symbol_index);
+    const symbol_ptr = elf_file.symbol(symbol_index);
+    symbol_ptr.file_index = self.index;
+    symbol_ptr.name_offset = name_off;
+
+    const esym_index = try self.addLocalEsym(gpa);
+    const esym = &self.local_esyms.items(.elf_sym)[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;
+
+    if (!elf_file.base.options.strip) {
+        self.dwarf = Dwarf.init(gpa, &elf_file.base, .dwarf32);
+    }
+}
+
 pub fn deinit(self: *ZigObject, allocator: Allocator) void {
     self.local_esyms.deinit(allocator);
     self.global_esyms.deinit(allocator);
@@ -119,7 +143,6 @@ pub fn addGlobalEsym(self: *ZigObject, allocator: Allocator) !Symbol.Index {
 
 pub fn addAtom(self: *ZigObject, elf_file: *Elf) !Symbol.Index {
     const gpa = elf_file.base.allocator;
-
     const atom_index = try elf_file.addAtom();
     const symbol_index = try elf_file.addSymbol();
     const esym_index = try self.addLocalEsym(gpa);
src/link/Elf.zig
@@ -292,28 +292,7 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option
             .path = options.module.?.main_mod.root_src_path,
         } });
         self.zig_object_index = index;
-        const zig_object = self.zigObjectPtr().?;
-
-        try zig_object.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_object.local_symbols.append(allocator, symbol_index);
-        const symbol_ptr = self.symbol(symbol_index);
-        symbol_ptr.file_index = zig_object.index;
-        symbol_ptr.name_offset = name_off;
-
-        const esym_index = try zig_object.addLocalEsym(allocator);
-        const esym = &zig_object.local_esyms.items(.elf_sym)[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;
-
-        if (!options.strip) {
-            zig_object.dwarf = Dwarf.init(allocator, &self.base, .dwarf32);
-        }
-
+        try self.zigObjectPtr().?.init(self);
         try self.initMetadata();
     }