authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-01-25 23:34:57+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-01-26 14:29:14+01:00
log2b5d0ca73b6b0ae96f3362aabe92b792ece2ae63
tree7af59aa058b0ce7d7f280784065fc3e85bc5c6b1
parent041bc71bc8ff45dc2d4df11d48023e457b8b021e

elf: pull out logic for allocating a GOT entry into a helper


1 files changed, 26 insertions(+), 14 deletions(-)

src/link/Elf.zig+26-14
......@@ -2289,31 +2289,43 @@ fn allocateLocalSymbol(self: *Elf) !u32 {
22892289 return index;
22902290}
22912291
2292fn allocateGotOffset(self: *Elf) !u32 {
2293 try self.offset_table.ensureUnusedCapacity(self.base.allocator, 1);
2294
2295 const index = blk: {
2296 if (self.offset_table_free_list.popOrNull()) |index| {
2297 log.debug(" (reusing GOT offset at index {d})", .{index});
2298 break :blk index;
2299 } else {
2300 log.debug(" (allocating GOT offset at index {d})", .{self.offset_table.items.len});
2301 const index = @intCast(u32, self.offset_table.items.len);
2302 _ = self.offset_table.addOneAssumeCapacity();
2303 self.offset_table_count_dirty = true;
2304 break :blk index;
2305 }
2306 };
2307
2308 self.offset_table.items[index] = 0;
2309 return index;
2310}
2311
22922312pub fn allocateDeclIndexes(self: *Elf, decl_index: Module.Decl.Index) !void {
22932313 if (self.llvm_object) |_| return;
22942314
22952315 const mod = self.base.options.module.?;
22962316 const decl = mod.declPtr(decl_index);
2297 if (decl.link.elf.local_sym_index != 0) return;
2298
2299 try self.offset_table.ensureUnusedCapacity(self.base.allocator, 1);
2300 try self.decls.putNoClobber(self.base.allocator, decl_index, null);
2317 const block = &decl.link.elf;
2318 if (block.local_sym_index != 0) return;
23012319
23022320 const decl_name = try decl.getFullyQualifiedName(mod);
23032321 defer self.base.allocator.free(decl_name);
23042322
23052323 log.debug("allocating symbol indexes for {s}", .{decl_name});
2306 decl.link.elf.local_sym_index = try self.allocateLocalSymbol();
2307 try self.atom_by_index_table.putNoClobber(self.base.allocator, decl.link.elf.local_sym_index, &decl.link.elf);
23082324
2309 if (self.offset_table_free_list.popOrNull()) |i| {
2310 decl.link.elf.offset_table_index = i;
2311 } else {
2312 decl.link.elf.offset_table_index = @intCast(u32, self.offset_table.items.len);
2313 _ = self.offset_table.addOneAssumeCapacity();
2314 self.offset_table_count_dirty = true;
2315 }
2316 self.offset_table.items[decl.link.elf.offset_table_index] = 0;
2325 block.local_sym_index = try self.allocateLocalSymbol();
2326 block.offset_table_index = try self.allocateGotOffset();
2327 try self.atom_by_index_table.putNoClobber(self.base.allocator, block.local_sym_index, block);
2328 try self.decls.putNoClobber(self.base.allocator, decl_index, null);
23172329}
23182330
23192331fn freeUnnamedConsts(self: *Elf, decl_index: Module.Decl.Index) void {