authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-30 19:44:27+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-30 19:44:27+01:00
logb1a735ac65126c9fb383ae894e31fb53b94ce0b8
tree523309fa751985059807957591d6cb42003b51a2
parent9bdbb6312f78764c0a5760ab035341f6cf09255b

elf: put init logic of ZigObject in init function


2 files changed, 25 insertions(+), 23 deletions(-)

src/link/Elf.zig+1-22
......@@ -292,28 +292,7 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option
292292 .path = options.module.?.main_mod.root_src_path,
293293 } });
294294 self.zig_object_index = index;
295 const zig_object = self.zigObjectPtr().?;
296
297 try zig_object.atoms.append(allocator, 0); // null input section
298
299 const name_off = try self.strtab.insert(allocator, std.fs.path.stem(options.module.?.main_mod.root_src_path));
300 const symbol_index = try self.addSymbol();
301 try zig_object.local_symbols.append(allocator, symbol_index);
302 const symbol_ptr = self.symbol(symbol_index);
303 symbol_ptr.file_index = zig_object.index;
304 symbol_ptr.name_offset = name_off;
305
306 const esym_index = try zig_object.addLocalEsym(allocator);
307 const esym = &zig_object.local_esyms.items(.elf_sym)[esym_index];
308 esym.st_name = name_off;
309 esym.st_info |= elf.STT_FILE;
310 esym.st_shndx = elf.SHN_ABS;
311 symbol_ptr.esym_index = esym_index;
312
313 if (!options.strip) {
314 zig_object.dwarf = Dwarf.init(allocator, &self.base, .dwarf32);
315 }
316
295 try self.zigObjectPtr().?.init(self);
317296 try self.initMetadata();
318297 }
319298
src/link/Elf/ZigObject.zig+24-1
......@@ -56,6 +56,30 @@ pub const global_symbol_bit: u32 = 0x80000000;
5656pub const symbol_mask: u32 = 0x7fffffff;
5757pub const SHN_ATOM: u16 = 0x100;
5858
59pub fn init(self: *ZigObject, elf_file: *Elf) !void {
60 const gpa = elf_file.base.allocator;
61
62 try self.atoms.append(gpa, 0); // null input section
63
64 const name_off = try elf_file.strtab.insert(gpa, std.fs.path.stem(self.path));
65 const symbol_index = try elf_file.addSymbol();
66 try self.local_symbols.append(gpa, symbol_index);
67 const symbol_ptr = elf_file.symbol(symbol_index);
68 symbol_ptr.file_index = self.index;
69 symbol_ptr.name_offset = name_off;
70
71 const esym_index = try self.addLocalEsym(gpa);
72 const esym = &self.local_esyms.items(.elf_sym)[esym_index];
73 esym.st_name = name_off;
74 esym.st_info |= elf.STT_FILE;
75 esym.st_shndx = elf.SHN_ABS;
76 symbol_ptr.esym_index = esym_index;
77
78 if (!elf_file.base.options.strip) {
79 self.dwarf = Dwarf.init(gpa, &elf_file.base, .dwarf32);
80 }
81}
82
5983pub fn deinit(self: *ZigObject, allocator: Allocator) void {
6084 self.local_esyms.deinit(allocator);
6185 self.global_esyms.deinit(allocator);
......@@ -119,7 +143,6 @@ pub fn addGlobalEsym(self: *ZigObject, allocator: Allocator) !Symbol.Index {
119143
120144pub fn addAtom(self: *ZigObject, elf_file: *Elf) !Symbol.Index {
121145 const gpa = elf_file.base.allocator;
122
123146 const atom_index = try elf_file.addAtom();
124147 const symbol_index = try elf_file.addSymbol();
125148 const esym_index = try self.addLocalEsym(gpa);