authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-04 11:23:19+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-04 11:23:19+02:00
logbc37c95e56b00f011a2cfb0b0795f234829c59da
treed196863fe5a9980113871e091a9452c4b07b5a6e
parent009a349779186548480823140dd1f8758d600221

elf: simplify accessors to symbols, atoms, etc


8 files changed, 118 insertions(+), 150 deletions(-)

src/arch/aarch64/CodeGen.zig+1-1
......@@ -4315,7 +4315,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
43154315 if (func_value.getFunction(mod)) |func| {
43164316 if (self.bin_file.cast(link.File.Elf)) |elf_file| {
43174317 const atom_index = try elf_file.getOrCreateAtomForDecl(func.owner_decl);
4318 const atom = elf_file.getAtom(atom_index);
4318 const atom = elf_file.atom(atom_index);
43194319 _ = try atom.getOrCreateOffsetTableEntry(elf_file);
43204320 const got_addr = @as(u32, @intCast(atom.getOffsetTableAddress(elf_file)));
43214321 try self.genSetReg(Type.usize, .x30, .{ .memory = got_addr });
src/arch/arm/CodeGen.zig+1-1
......@@ -4295,7 +4295,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
42954295 if (func_value.getFunction(mod)) |func| {
42964296 if (self.bin_file.cast(link.File.Elf)) |elf_file| {
42974297 const atom_index = try elf_file.getOrCreateAtomForDecl(func.owner_decl);
4298 const atom = elf_file.getAtom(atom_index);
4298 const atom = elf_file.atom(atom_index);
42994299 _ = try atom.getOrCreateOffsetTableEntry(elf_file);
43004300 const got_addr = @as(u32, @intCast(atom.getOffsetTableAddress(elf_file)));
43014301 try self.genSetReg(Type.usize, .lr, .{ .memory = got_addr });
src/arch/riscv64/CodeGen.zig+1-1
......@@ -1748,7 +1748,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
17481748 switch (mod.intern_pool.indexToKey(func_value.ip_index)) {
17491749 .func => |func| {
17501750 const atom_index = try elf_file.getOrCreateAtomForDecl(func.owner_decl);
1751 const atom = elf_file.getAtom(atom_index);
1751 const atom = elf_file.atom(atom_index);
17521752 _ = try atom.getOrCreateOffsetTableEntry(elf_file);
17531753 const got_addr = @as(u32, @intCast(atom.getOffsetTableAddress(elf_file)));
17541754 try self.genSetReg(Type.usize, .ra, .{ .memory = got_addr });
src/arch/sparc64/CodeGen.zig+1-1
......@@ -1350,7 +1350,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
13501350 .func => |func| {
13511351 const got_addr = if (self.bin_file.cast(link.File.Elf)) |elf_file| blk: {
13521352 const atom_index = try elf_file.getOrCreateAtomForDecl(func.owner_decl);
1353 const atom = elf_file.getAtom(atom_index);
1353 const atom = elf_file.atom(atom_index);
13541354 _ = try atom.getOrCreateOffsetTableEntry(elf_file);
13551355 break :blk @as(u32, @intCast(atom.getOffsetTableAddress(elf_file)));
13561356 } else unreachable;
src/arch/x86_64/CodeGen.zig+2-2
......@@ -8150,7 +8150,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
81508150 }) |owner_decl| {
81518151 if (self.bin_file.cast(link.File.Elf)) |elf_file| {
81528152 const atom_index = try elf_file.getOrCreateAtomForDecl(owner_decl);
8153 const atom = elf_file.getAtom(atom_index);
8153 const atom = elf_file.atom(atom_index);
81548154 _ = try atom.getOrCreateOffsetTableEntry(elf_file);
81558155 const got_addr = atom.getOffsetTableAddress(elf_file);
81568156 try self.asmMemory(.{ ._, .call }, Memory.sib(.qword, .{
......@@ -10217,7 +10217,7 @@ fn genLazySymbolRef(
1021710217 if (self.bin_file.cast(link.File.Elf)) |elf_file| {
1021810218 const atom_index = elf_file.getOrCreateAtomForLazySymbol(lazy_sym) catch |err|
1021910219 return self.fail("{s} creating lazy symbol", .{@errorName(err)});
10220 const atom = elf_file.getAtom(atom_index);
10220 const atom = elf_file.atom(atom_index);
1022110221 _ = try atom.getOrCreateOffsetTableEntry(elf_file);
1022210222 const got_addr = atom.getOffsetTableAddress(elf_file);
1022310223 const got_mem =
src/codegen.zig+2-2
......@@ -855,7 +855,7 @@ fn genDeclRef(
855855
856856 if (bin_file.cast(link.File.Elf)) |elf_file| {
857857 const atom_index = try elf_file.getOrCreateAtomForDecl(decl_index);
858 const atom = elf_file.getAtom(atom_index);
858 const atom = elf_file.atom(atom_index);
859859 _ = try atom.getOrCreateOffsetTableEntry(elf_file);
860860 return GenResult.mcv(.{ .memory = atom.getOffsetTableAddress(elf_file) });
861861 } else if (bin_file.cast(link.File.MachO)) |macho_file| {
......@@ -892,7 +892,7 @@ fn genUnnamedConst(
892892 return GenResult.fail(bin_file.allocator, src_loc, "lowering unnamed constant failed: {s}", .{@errorName(err)});
893893 };
894894 if (bin_file.cast(link.File.Elf)) |elf_file| {
895 return GenResult.mcv(.{ .memory = elf_file.getSymbol(local_sym_index).st_value });
895 return GenResult.mcv(.{ .memory = elf_file.symbol(local_sym_index).st_value });
896896 } else if (bin_file.cast(link.File.MachO)) |_| {
897897 return GenResult.mcv(.{ .load_direct = local_sym_index });
898898 } else if (bin_file.cast(link.File.Coff)) |_| {
src/link/Elf.zig+93-121
......@@ -280,10 +280,10 @@ pub fn getDeclVAddr(self: *Elf, decl_index: Module.Decl.Index, reloc_info: File.
280280 assert(self.llvm_object == null);
281281
282282 const this_atom_index = try self.getOrCreateAtomForDecl(decl_index);
283 const this_atom = self.getAtom(this_atom_index);
284 const target = this_atom.getSymbolIndex().?;
285 const vaddr = this_atom.getSymbol(self).st_value;
286 const atom_index = self.getAtomIndexForSymbol(reloc_info.parent_atom_index).?;
283 const this_atom = self.atom(this_atom_index);
284 const target = this_atom.symbolIndex().?;
285 const vaddr = this_atom.symbol(self).st_value;
286 const atom_index = self.atomIndexForSymbol(reloc_info.parent_atom_index).?;
287287 try Atom.addRelocation(self, atom_index, .{
288288 .target = target,
289289 .offset = reloc_info.offset,
......@@ -849,8 +849,8 @@ fn growAllocSection(self: *Elf, shdr_index: u16, needed_size: u64) !void {
849849 // Must move the entire section.
850850 const new_offset = self.findFreeSpace(needed_size, self.page_size);
851851 const existing_size = if (maybe_last_atom_index) |last_atom_index| blk: {
852 const last = self.getAtom(last_atom_index);
853 const sym = last.getSymbol(self);
852 const last = self.atom(last_atom_index);
853 const sym = last.symbol(self);
854854 break :blk (sym.st_value + sym.st_size) - phdr.p_vaddr;
855855 } else if (shdr_index == self.got_section_index.?) blk: {
856856 break :blk shdr.sh_size;
......@@ -1010,8 +1010,8 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
10101010 while (it.next()) |entry| {
10111011 const atom_index = entry.key_ptr.*;
10121012 const relocs = entry.value_ptr.*;
1013 const atom = self.getAtom(atom_index);
1014 const source_sym = atom.getSymbol(self);
1013 const atom_ptr = self.atom(atom_index);
1014 const source_sym = atom_ptr.symbol(self);
10151015 const source_shdr = self.sections.items(.shdr)[source_sym.st_shndx];
10161016
10171017 log.debug("relocating '{?s}'", .{self.strtab.get(source_sym.st_name)});
......@@ -1471,9 +1471,9 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
14711471 try argv.append(entry);
14721472 }
14731473
1474 for (self.base.options.force_undefined_symbols.keys()) |symbol| {
1474 for (self.base.options.force_undefined_symbols.keys()) |sym| {
14751475 try argv.append("-u");
1476 try argv.append(symbol);
1476 try argv.append(sym);
14771477 }
14781478
14791479 switch (self.base.options.hash_style) {
......@@ -2071,13 +2071,13 @@ fn writeElfHeader(self: *Elf) !void {
20712071}
20722072
20732073fn freeAtom(self: *Elf, atom_index: Atom.Index) void {
2074 const atom = self.getAtom(atom_index);
2075 log.debug("freeAtom {d} ({s})", .{ atom_index, atom.getName(self) });
2074 const atom_ptr = self.atom(atom_index);
2075 log.debug("freeAtom {d} ({s})", .{ atom_index, atom_ptr.name(self) });
20762076
20772077 Atom.freeRelocations(self, atom_index);
20782078
20792079 const gpa = self.base.allocator;
2080 const shndx = atom.getSymbol(self).st_shndx;
2080 const shndx = atom_ptr.symbol(self).st_shndx;
20812081 const free_list = &self.sections.items(.free_list)[shndx];
20822082 var already_have_free_list_node = false;
20832083 {
......@@ -2088,7 +2088,7 @@ fn freeAtom(self: *Elf, atom_index: Atom.Index) void {
20882088 _ = free_list.swapRemove(i);
20892089 continue;
20902090 }
2091 if (free_list.items[i] == atom.prev_index) {
2091 if (free_list.items[i] == atom_ptr.prev_index) {
20922092 already_have_free_list_node = true;
20932093 }
20942094 i += 1;
......@@ -2098,7 +2098,7 @@ fn freeAtom(self: *Elf, atom_index: Atom.Index) void {
20982098 const maybe_last_atom_index = &self.sections.items(.last_atom_index)[shndx];
20992099 if (maybe_last_atom_index.*) |last_atom_index| {
21002100 if (last_atom_index == atom_index) {
2101 if (atom.prev_index) |prev_index| {
2101 if (atom_ptr.prev_index) |prev_index| {
21022102 // TODO shrink the section size here
21032103 maybe_last_atom_index.* = prev_index;
21042104 } else {
......@@ -2107,9 +2107,9 @@ fn freeAtom(self: *Elf, atom_index: Atom.Index) void {
21072107 }
21082108 }
21092109
2110 if (atom.prev_index) |prev_index| {
2111 const prev = self.getAtomPtr(prev_index);
2112 prev.next_index = atom.next_index;
2110 if (atom_ptr.prev_index) |prev_index| {
2111 const prev = self.atom(prev_index);
2112 prev.next_index = atom_ptr.next_index;
21132113
21142114 if (!already_have_free_list_node and prev.*.freeListEligible(self)) {
21152115 // The free list is heuristics, it doesn't have to be perfect, so we can
......@@ -2117,23 +2117,23 @@ fn freeAtom(self: *Elf, atom_index: Atom.Index) void {
21172117 free_list.append(gpa, prev_index) catch {};
21182118 }
21192119 } else {
2120 self.getAtomPtr(atom_index).prev_index = null;
2120 atom_ptr.prev_index = null;
21212121 }
21222122
2123 if (atom.next_index) |next_index| {
2124 self.getAtomPtr(next_index).prev_index = atom.prev_index;
2123 if (atom_ptr.next_index) |next_index| {
2124 self.atom(next_index).prev_index = atom_ptr.prev_index;
21252125 } else {
2126 self.getAtomPtr(atom_index).next_index = null;
2126 atom_ptr.next_index = null;
21272127 }
21282128
21292129 // Appending to free lists is allowed to fail because the free lists are heuristics based anyway.
2130 const sym_index = atom.getSymbolIndex().?;
2130 const sym_index = atom_ptr.symbolIndex().?;
21312131
21322132 log.debug("adding %{d} to local symbols free list", .{sym_index});
21332133 self.locals_free_list.append(gpa, sym_index) catch {};
21342134 self.locals.items[sym_index] = null_sym;
21352135 _ = self.atom_by_index_table.remove(sym_index);
2136 self.getAtomPtr(atom_index).sym_index = 0;
2136 atom_ptr.sym_index = 0;
21372137 self.got_table.freeEntry(gpa, sym_index);
21382138}
21392139
......@@ -2144,10 +2144,10 @@ fn shrinkAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64) void {
21442144}
21452145
21462146fn growAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64, alignment: u64) !u64 {
2147 const atom = self.getAtom(atom_index);
2148 const sym = atom.getSymbol(self);
2147 const atom_ptr = self.atom(atom_index);
2148 const sym = atom_ptr.symbol(self);
21492149 const align_ok = mem.alignBackward(u64, sym.st_value, alignment) == sym.st_value;
2150 const need_realloc = !align_ok or new_block_size > atom.capacity(self);
2150 const need_realloc = !align_ok or new_block_size > atom_ptr.capacity(self);
21512151 if (!need_realloc) return sym.st_value;
21522152 return self.allocateAtom(atom_index, new_block_size, alignment);
21532153}
......@@ -2155,10 +2155,10 @@ fn growAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64, alignment:
21552155pub fn createAtom(self: *Elf) !Atom.Index {
21562156 const gpa = self.base.allocator;
21572157 const atom_index = @as(Atom.Index, @intCast(self.atoms.items.len));
2158 const atom = try self.atoms.addOne(gpa);
2158 const atom_ptr = try self.atoms.addOne(gpa);
21592159 const sym_index = try self.allocateSymbol();
21602160 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom_index);
2161 atom.* = .{
2161 atom_ptr.* = .{
21622162 .sym_index = sym_index,
21632163 .prev_index = null,
21642164 .next_index = null,
......@@ -2168,8 +2168,7 @@ pub fn createAtom(self: *Elf) !Atom.Index {
21682168}
21692169
21702170fn allocateAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64, alignment: u64) !u64 {
2171 const atom = self.getAtom(atom_index);
2172 const sym = atom.getSymbol(self);
2171 const sym = self.atom(atom_index).symbol(self);
21732172 const phdr_index = self.sections.items(.phdr_index)[sym.st_shndx];
21742173 const phdr = &self.program_headers.items[phdr_index];
21752174 const shdr = &self.sections.items(.shdr)[sym.st_shndx];
......@@ -2191,10 +2190,10 @@ fn allocateAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64, alignme
21912190 var i: usize = if (self.base.child_pid == null) 0 else free_list.items.len;
21922191 while (i < free_list.items.len) {
21932192 const big_atom_index = free_list.items[i];
2194 const big_atom = self.getAtom(big_atom_index);
2193 const big_atom = self.atom(big_atom_index);
21952194 // We now have a pointer to a live atom that has too much capacity.
21962195 // Is it enough that we could fit this new atom?
2197 const big_atom_sym = big_atom.getSymbol(self);
2196 const big_atom_sym = big_atom.symbol(self);
21982197 const capacity = big_atom.capacity(self);
21992198 const ideal_capacity = padToIdeal(capacity);
22002199 const ideal_capacity_end_vaddr = std.math.add(u64, big_atom_sym.st_value, ideal_capacity) catch ideal_capacity;
......@@ -2225,8 +2224,8 @@ fn allocateAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64, alignme
22252224 }
22262225 break :blk new_start_vaddr;
22272226 } else if (maybe_last_atom_index.*) |last_index| {
2228 const last = self.getAtom(last_index);
2229 const last_sym = last.getSymbol(self);
2227 const last = self.atom(last_index);
2228 const last_sym = last.symbol(self);
22302229 const ideal_capacity = padToIdeal(last_sym.st_size);
22312230 const ideal_capacity_end_vaddr = last_sym.st_value + ideal_capacity;
22322231 const new_start_vaddr = mem.alignForward(u64, ideal_capacity_end_vaddr, alignment);
......@@ -2239,7 +2238,7 @@ fn allocateAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64, alignme
22392238 };
22402239
22412240 const expand_section = if (atom_placement) |placement_index|
2242 self.getAtom(placement_index).next_index == null
2241 self.atom(placement_index).next_index == null
22432242 else
22442243 true;
22452244 if (expand_section) {
......@@ -2263,23 +2262,22 @@ fn allocateAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64, alignme
22632262 // This function can also reallocate an atom.
22642263 // In this case we need to "unplug" it from its previous location before
22652264 // plugging it in to its new location.
2266 if (atom.prev_index) |prev_index| {
2267 const prev = self.getAtomPtr(prev_index);
2268 prev.next_index = atom.next_index;
2265 const atom_ptr = self.atom(atom_index);
2266 if (atom_ptr.prev_index) |prev_index| {
2267 const prev = self.atom(prev_index);
2268 prev.next_index = atom_ptr.next_index;
22692269 }
2270 if (atom.next_index) |next_index| {
2271 const next = self.getAtomPtr(next_index);
2272 next.prev_index = atom.prev_index;
2270 if (atom_ptr.next_index) |next_index| {
2271 const next = self.atom(next_index);
2272 next.prev_index = atom_ptr.prev_index;
22732273 }
22742274
22752275 if (atom_placement) |big_atom_index| {
2276 const big_atom = self.getAtomPtr(big_atom_index);
2277 const atom_ptr = self.getAtomPtr(atom_index);
2276 const big_atom = self.atom(big_atom_index);
22782277 atom_ptr.prev_index = big_atom_index;
22792278 atom_ptr.next_index = big_atom.next_index;
22802279 big_atom.next_index = atom_index;
22812280 } else {
2282 const atom_ptr = self.getAtomPtr(atom_index);
22832281 atom_ptr.prev_index = null;
22842282 atom_ptr.next_index = null;
22852283 }
......@@ -2320,7 +2318,7 @@ fn allocateGlobal(self: *Elf) !u32 {
23202318 log.debug(" (reusing global index {d})", .{index});
23212319 break :blk index;
23222320 } else {
2323 log.debug(" (allocating symbol index {d})", .{self.globals.items.len});
2321 log.debug(" (allocating global index {d})", .{self.globals.items.len});
23242322 const index = @as(u32, @intCast(self.globals.items.len));
23252323 _ = self.globals.addOneAssumeCapacity();
23262324 break :blk index;
......@@ -2332,8 +2330,8 @@ fn allocateGlobal(self: *Elf) !u32 {
23322330
23332331fn freeUnnamedConsts(self: *Elf, decl_index: Module.Decl.Index) void {
23342332 const unnamed_consts = self.unnamed_const_atoms.getPtr(decl_index) orelse return;
2335 for (unnamed_consts.items) |atom| {
2336 self.freeAtom(atom);
2333 for (unnamed_consts.items) |atom_index| {
2334 self.freeAtom(atom_index);
23372335 }
23382336 unnamed_consts.clearAndFree(self.base.allocator);
23392337}
......@@ -2373,13 +2371,13 @@ pub fn getOrCreateAtomForLazySymbol(self: *Elf, sym: File.LazySymbol) !Atom.Inde
23732371 .flushed => {},
23742372 }
23752373 metadata.state.* = .pending_flush;
2376 const atom = metadata.atom.*;
2374 const atom_index = metadata.atom.*;
23772375 // anyerror needs to be deferred until flushModule
2378 if (sym.getDecl(mod) != .none) try self.updateLazySymbolAtom(sym, atom, switch (sym.kind) {
2376 if (sym.getDecl(mod) != .none) try self.updateLazySymbolAtom(sym, atom_index, switch (sym.kind) {
23792377 .code => self.text_section_index.?,
23802378 .const_data => self.rodata_section_index.?,
23812379 });
2382 return atom;
2380 return atom_index;
23832381}
23842382
23852383pub fn getOrCreateAtomForDecl(self: *Elf, decl_index: Module.Decl.Index) !Atom.Index {
......@@ -2432,18 +2430,18 @@ fn updateDeclCode(self: *Elf, decl_index: Module.Decl.Index, code: []const u8, s
24322430
24332431 const decl_metadata = self.decls.get(decl_index).?;
24342432 const atom_index = decl_metadata.atom;
2435 const atom = self.getAtom(atom_index);
2436 const local_sym_index = atom.getSymbolIndex().?;
2433 const atom_ptr = self.atom(atom_index);
2434 const local_sym_index = atom_ptr.symbolIndex().?;
2435 const local_sym = atom_ptr.symbol(self);
24372436
24382437 const shdr_index = decl_metadata.shdr;
2439 if (atom.getSymbol(self).st_size != 0 and self.base.child_pid == null) {
2440 const local_sym = atom.getSymbolPtr(self);
2438 if (atom_ptr.symbol(self).st_size != 0 and self.base.child_pid == null) {
24412439 local_sym.st_name = try self.strtab.insert(gpa, decl_name);
24422440 local_sym.st_info = (elf.STB_LOCAL << 4) | stt_bits;
24432441 local_sym.st_other = 0;
24442442 local_sym.st_shndx = shdr_index;
24452443
2446 const capacity = atom.capacity(self);
2444 const capacity = atom_ptr.capacity(self);
24472445 const need_realloc = code.len > capacity or
24482446 !mem.isAlignedGeneric(u64, local_sym.st_value, required_alignment);
24492447
......@@ -2463,7 +2461,6 @@ fn updateDeclCode(self: *Elf, decl_index: Module.Decl.Index, code: []const u8, s
24632461 }
24642462 local_sym.st_size = code.len;
24652463 } else {
2466 const local_sym = atom.getSymbolPtr(self);
24672464 local_sym.* = .{
24682465 .st_name = try self.strtab.insert(gpa, decl_name),
24692466 .st_info = (elf.STB_LOCAL << 4) | stt_bits,
......@@ -2479,11 +2476,10 @@ fn updateDeclCode(self: *Elf, decl_index: Module.Decl.Index, code: []const u8, s
24792476 local_sym.st_value = vaddr;
24802477 local_sym.st_size = code.len;
24812478
2482 const got_entry_index = try atom.getOrCreateOffsetTableEntry(self);
2479 const got_entry_index = try atom_ptr.getOrCreateOffsetTableEntry(self);
24832480 try self.writeOffsetTableEntry(got_entry_index);
24842481 }
24852482
2486 const local_sym = atom.getSymbolPtr(self);
24872483 const phdr_index = self.sections.items(.phdr_index)[shdr_index];
24882484 const section_offset = local_sym.st_value - self.program_headers.items[phdr_index].p_vaddr;
24892485 const file_offset = self.sections.items(.shdr)[shdr_index].sh_offset + section_offset;
......@@ -2594,7 +2590,6 @@ pub fn updateDecl(
25942590
25952591 const atom_index = try self.getOrCreateAtomForDecl(decl_index);
25962592 Atom.freeRelocations(self, atom_index);
2597 const atom = self.getAtom(atom_index);
25982593
25992594 var code_buffer = std.ArrayList(u8).init(self.base.allocator);
26002595 defer code_buffer.deinit();
......@@ -2611,14 +2606,14 @@ pub fn updateDecl(
26112606 }, &code_buffer, .{
26122607 .dwarf = ds,
26132608 }, .{
2614 .parent_atom_index = atom.getSymbolIndex().?,
2609 .parent_atom_index = self.atom(atom_index).symbolIndex().?,
26152610 })
26162611 else
26172612 try codegen.generateSymbol(&self.base, decl.srcLoc(mod), .{
26182613 .ty = decl.ty,
26192614 .val = decl_val,
26202615 }, &code_buffer, .none, .{
2621 .parent_atom_index = atom.getSymbolIndex().?,
2616 .parent_atom_index = self.atom(atom_index).symbolIndex().?,
26222617 });
26232618
26242619 const code = switch (res) {
......@@ -2669,8 +2664,8 @@ fn updateLazySymbolAtom(
26692664 };
26702665 const name = self.strtab.get(name_str_index).?;
26712666
2672 const atom = self.getAtom(atom_index);
2673 const local_sym_index = atom.getSymbolIndex().?;
2667 const atom_ptr = self.atom(atom_index);
2668 const local_sym_index = atom_ptr.symbolIndex().?;
26742669
26752670 const src = if (sym.ty.getOwnerDeclOrNull(mod)) |owner_decl|
26762671 mod.declPtr(owner_decl).srcLoc(mod)
......@@ -2698,7 +2693,7 @@ fn updateLazySymbolAtom(
26982693 };
26992694
27002695 const phdr_index = self.sections.items(.phdr_index)[shdr_index];
2701 const local_sym = atom.getSymbolPtr(self);
2696 const local_sym = atom_ptr.symbol(self);
27022697 local_sym.* = .{
27032698 .st_name = name_str_index,
27042699 .st_info = (elf.STB_LOCAL << 4) | elf.STT_OBJECT,
......@@ -2714,7 +2709,7 @@ fn updateLazySymbolAtom(
27142709 local_sym.st_value = vaddr;
27152710 local_sym.st_size = code.len;
27162711
2717 const got_entry_index = try atom.getOrCreateOffsetTableEntry(self);
2712 const got_entry_index = try atom_ptr.getOrCreateOffsetTableEntry(self);
27182713 try self.writeOffsetTableEntry(got_entry_index);
27192714
27202715 const section_offset = vaddr - self.program_headers.items[phdr_index].p_vaddr;
......@@ -2750,7 +2745,7 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module
27502745 const res = try codegen.generateSymbol(&self.base, decl.srcLoc(mod), typed_value, &code_buffer, .{
27512746 .none = {},
27522747 }, .{
2753 .parent_atom_index = self.getAtom(atom_index).getSymbolIndex().?,
2748 .parent_atom_index = self.atom(atom_index).symbolIndex().?,
27542749 });
27552750 const code = switch (res) {
27562751 .ok => code_buffer.items,
......@@ -2765,7 +2760,7 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module
27652760 const required_alignment = typed_value.ty.abiAlignment(mod);
27662761 const shdr_index = self.rodata_section_index.?;
27672762 const phdr_index = self.sections.items(.phdr_index)[shdr_index];
2768 const local_sym = self.getAtom(atom_index).getSymbolPtr(self);
2763 const local_sym = self.atom(atom_index).symbol(self);
27692764 local_sym.st_name = name_str_index;
27702765 local_sym.st_info = (elf.STB_LOCAL << 4) | elf.STT_OBJECT;
27712766 local_sym.st_other = 0;
......@@ -2782,7 +2777,7 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module
27822777 const file_offset = self.sections.items(.shdr)[shdr_index].sh_offset + section_offset;
27832778 try self.base.file.?.pwriteAll(code, file_offset);
27842779
2785 return self.getAtom(atom_index).getSymbolIndex().?;
2780 return self.atom(atom_index).symbolIndex().?;
27862781}
27872782
27882783pub fn updateDeclExports(
......@@ -2805,8 +2800,8 @@ pub fn updateDeclExports(
28052800
28062801 const decl = mod.declPtr(decl_index);
28072802 const atom_index = try self.getOrCreateAtomForDecl(decl_index);
2808 const atom = self.getAtom(atom_index);
2809 const decl_sym = atom.getSymbol(self);
2803 const atom_ptr = self.atom(atom_index);
2804 const decl_sym = atom_ptr.symbol(self);
28102805 const decl_metadata = self.decls.getPtr(decl_index).?;
28112806 const shdr_index = decl_metadata.shdr;
28122807
......@@ -2843,12 +2838,12 @@ pub fn updateDeclExports(
28432838 };
28442839 const stt_bits: u8 = @as(u4, @truncate(decl_sym.st_info));
28452840
2846 const sym_index = decl_metadata.getExport(self, exp_name) orelse blk: {
2841 const sym_index = if (decl_metadata.@"export"(self, exp_name)) |exp_index| exp_index.* else blk: {
28472842 const sym_index = try self.allocateSymbol();
28482843 try decl_metadata.exports.append(gpa, sym_index);
28492844 break :blk sym_index;
28502845 };
2851 const sym = self.getSymbolPtr(sym_index);
2846 const sym = self.symbol(sym_index);
28522847 sym.* = .{
28532848 .st_name = try self.strtab.insert(gpa, exp_name),
28542849 .st_info = (stb_bits << 4) | stt_bits,
......@@ -2857,8 +2852,8 @@ pub fn updateDeclExports(
28572852 .st_value = decl_sym.st_value,
28582853 .st_size = decl_sym.st_size,
28592854 };
2860 const sym_name = self.getSymbolName(sym_index);
2861 const gop = try self.getOrPutGlobalPtr(sym_name);
2855 const sym_name = self.symbolName(sym_index);
2856 const gop = try self.getOrPutGlobal(sym_name);
28622857 gop.value_ptr.* = sym_index;
28632858 }
28642859}
......@@ -2889,8 +2884,8 @@ pub fn deleteDeclExport(
28892884 const gpa = self.base.allocator;
28902885 const mod = self.base.options.module.?;
28912886 const exp_name = mod.intern_pool.stringToSlice(name);
2892 const sym_index = metadata.getExportPtr(self, exp_name) orelse return;
2893 const sym = self.getSymbolPtr(sym_index.*);
2887 const sym_index = metadata.@"export"(self, exp_name) orelse return;
2888 const sym = self.symbol(sym_index.*);
28942889 log.debug("deleting export '{s}'", .{exp_name});
28952890 sym.* = null_sym;
28962891 self.locals_free_list.append(gpa, sym_index.*) catch {};
......@@ -2960,7 +2955,7 @@ fn writeOffsetTableEntry(self: *Elf, index: @TypeOf(self.got_table).Index) !void
29602955 const phdr = &self.program_headers.items[self.phdr_got_index.?];
29612956 const vaddr = phdr.p_vaddr + @as(u64, entry_size) * index;
29622957 const got_entry = self.got_table.entries.items[index];
2963 const got_value = self.getSymbol(got_entry).st_value;
2958 const got_value = self.symbol(got_entry).st_value;
29642959 switch (entry_size) {
29652960 2 => {
29662961 var buf: [2]u8 = undefined;
......@@ -3046,8 +3041,8 @@ fn writeSymbols(self: *Elf) !void {
30463041 }
30473042 }
30483043
3049 for (buf[self.locals.items.len..], self.globals.items) |*sym, global| {
3050 elf32SymFromSym(self.getSymbol(global), sym);
3044 for (buf[self.locals.items.len..], self.globals.items) |*sym, glob| {
3045 elf32SymFromSym(self.symbol(glob).*, sym);
30513046 if (foreign_endian) {
30523047 mem.byteSwapAllFields(elf.Elf32_Sym, sym);
30533048 }
......@@ -3064,8 +3059,8 @@ fn writeSymbols(self: *Elf) !void {
30643059 }
30653060 }
30663061
3067 for (buf[self.locals.items.len..], self.globals.items) |*sym, global| {
3068 sym.* = self.getSymbol(global);
3062 for (buf[self.locals.items.len..], self.globals.items) |*sym, glob| {
3063 sym.* = self.symbol(glob).*;
30693064 if (foreign_endian) {
30703065 mem.byteSwapAllFields(elf.Elf64_Sym, sym);
30713066 }
......@@ -3369,8 +3364,8 @@ fn logSymtab(self: Elf) void {
33693364 log.debug(" {d}: {?s}: @{x} in {d}", .{ id, self.strtab.get(sym.st_name), sym.st_value, sym.st_shndx });
33703365 }
33713366 log.debug("globals:", .{});
3372 for (self.globals.items, 0..) |global, id| {
3373 const sym = self.getSymbol(global);
3367 for (self.globals.items, 0..) |glob, id| {
3368 const sym = self.symbol(glob);
33743369 log.debug(" {d}: {?s}: @{x} in {d}", .{ id, self.strtab.get(sym.st_name), sym.st_value, sym.st_shndx });
33753370 }
33763371}
......@@ -3386,45 +3381,34 @@ pub fn getProgramHeaderPtr(self: *Elf, shdr_index: u16) *elf.Elf64_Phdr {
33863381}
33873382
33883383/// Returns pointer-to-symbol described at sym_index.
3389pub fn getSymbolPtr(self: *Elf, sym_index: u32) *elf.Elf64_Sym {
3384pub fn symbol(self: *const Elf, sym_index: u32) *elf.Elf64_Sym {
33903385 return &self.locals.items[sym_index];
33913386}
33923387
3393/// Returns symbol at sym_index.
3394pub fn getSymbol(self: *const Elf, sym_index: u32) elf.Elf64_Sym {
3395 return self.locals.items[sym_index];
3396}
3397
33983388/// Returns name of the symbol at sym_index.
3399pub fn getSymbolName(self: *const Elf, sym_index: u32) []const u8 {
3389pub fn symbolName(self: *const Elf, sym_index: u32) []const u8 {
34003390 const sym = self.locals.items[sym_index];
34013391 return self.strtab.get(sym.st_name).?;
34023392}
34033393
34043394/// Returns pointer to the global entry for `name` if one exists.
3405pub fn getGlobalPtr(self: *Elf, name: []const u8) ?*u32 {
3395pub fn global(self: *const Elf, name: []const u8) ?*u32 {
34063396 const global_index = self.resolver.get(name) orelse return null;
34073397 return &self.globals.items[global_index];
34083398}
34093399
3410/// Returns the global entry for `name` if one exists.
3411pub fn getGlobal(self: *const Elf, name: []const u8) ?u32 {
3412 const global_index = self.resolver.get(name) orelse return null;
3413 return self.globals.items[global_index];
3414}
3415
34163400/// Returns the index of the global entry for `name` if one exists.
3417pub fn getGlobalIndex(self: *const Elf, name: []const u8) ?u32 {
3401pub fn globalIndex(self: *const Elf, name: []const u8) ?u32 {
34183402 return self.resolver.get(name);
34193403}
34203404
34213405/// Returns global entry at `index`.
3422pub fn getGlobalByIndex(self: *const Elf, index: u32) u32 {
3406pub fn globalByIndex(self: *const Elf, index: u32) u32 {
34233407 assert(index < self.globals.items.len);
34243408 return self.globals.items[index];
34253409}
34263410
3427const GetOrPutGlobalPtrResult = struct {
3411const GetOrPutGlobalResult = struct {
34283412 found_existing: bool,
34293413 value_ptr: *u32,
34303414};
......@@ -3432,31 +3416,26 @@ const GetOrPutGlobalPtrResult = struct {
34323416/// Return pointer to the global entry for `name` if one exists.
34333417/// Puts a new global entry for `name` if one doesn't exist, and
34343418/// returns a pointer to it.
3435pub fn getOrPutGlobalPtr(self: *Elf, name: []const u8) !GetOrPutGlobalPtrResult {
3436 if (self.getGlobalPtr(name)) |ptr| {
3437 return GetOrPutGlobalPtrResult{ .found_existing = true, .value_ptr = ptr };
3419pub fn getOrPutGlobal(self: *Elf, name: []const u8) !GetOrPutGlobalResult {
3420 if (self.global(name)) |ptr| {
3421 return GetOrPutGlobalResult{ .found_existing = true, .value_ptr = ptr };
34383422 }
34393423 const gpa = self.base.allocator;
34403424 const global_index = try self.allocateGlobal();
34413425 const global_name = try gpa.dupe(u8, name);
34423426 _ = try self.resolver.put(gpa, global_name, global_index);
34433427 const ptr = &self.globals.items[global_index];
3444 return GetOrPutGlobalPtrResult{ .found_existing = false, .value_ptr = ptr };
3428 return GetOrPutGlobalResult{ .found_existing = false, .value_ptr = ptr };
34453429}
34463430
3447pub fn getAtom(self: *const Elf, atom_index: Atom.Index) Atom {
3448 assert(atom_index < self.atoms.items.len);
3449 return self.atoms.items[atom_index];
3450}
3451
3452pub fn getAtomPtr(self: *Elf, atom_index: Atom.Index) *Atom {
3431pub fn atom(self: *Elf, atom_index: Atom.Index) *Atom {
34533432 assert(atom_index < self.atoms.items.len);
34543433 return &self.atoms.items[atom_index];
34553434}
34563435
34573436/// Returns atom if there is an atom referenced by the symbol.
34583437/// Returns null on failure.
3459pub fn getAtomIndexForSymbol(self: *Elf, sym_index: u32) ?Atom.Index {
3438pub fn atomIndexForSymbol(self: *Elf, sym_index: u32) ?Atom.Index {
34603439 return self.atom_by_index_table.get(sym_index);
34613440}
34623441
......@@ -3512,16 +3491,9 @@ const DeclMetadata = struct {
35123491 /// A list of all exports aliases of this Decl.
35133492 exports: std.ArrayListUnmanaged(u32) = .{},
35143493
3515 fn getExport(m: DeclMetadata, elf_file: *const Elf, name: []const u8) ?u32 {
3516 for (m.exports.items) |exp| {
3517 if (mem.eql(u8, name, elf_file.getSymbolName(exp))) return exp;
3518 }
3519 return null;
3520 }
3521
3522 fn getExportPtr(m: *DeclMetadata, elf_file: *Elf, name: []const u8) ?*u32 {
3494 fn @"export"(m: DeclMetadata, elf_file: *const Elf, name: []const u8) ?*u32 {
35233495 for (m.exports.items) |*exp| {
3524 if (mem.eql(u8, name, elf_file.getSymbolName(exp.*))) return exp;
3496 if (mem.eql(u8, name, elf_file.symbolName(exp.*))) return exp;
35253497 }
35263498 return null;
35273499 }
src/link/Elf/Atom.zig+17-21
......@@ -20,35 +20,31 @@ pub const Reloc = struct {
2020 prev_vaddr: u64,
2121};
2222
23pub fn getSymbolIndex(self: Atom) ?u32 {
23pub fn symbolIndex(self: *const Atom) ?u32 {
2424 if (self.sym_index == 0) return null;
2525 return self.sym_index;
2626}
2727
28pub fn getSymbol(self: Atom, elf_file: *const Elf) elf.Elf64_Sym {
29 return elf_file.getSymbol(self.getSymbolIndex().?);
28pub fn symbol(self: *const Atom, elf_file: *Elf) *elf.Elf64_Sym {
29 return elf_file.symbol(self.symbolIndex().?);
3030}
3131
32pub fn getSymbolPtr(self: Atom, elf_file: *Elf) *elf.Elf64_Sym {
33 return elf_file.getSymbolPtr(self.getSymbolIndex().?);
34}
35
36pub fn getName(self: Atom, elf_file: *const Elf) []const u8 {
37 return elf_file.getSymbolName(self.getSymbolIndex().?);
32pub fn name(self: *const Atom, elf_file: *Elf) []const u8 {
33 return elf_file.symbolName(self.symbolIndex().?);
3834}
3935
4036/// If entry already exists, returns index to it.
4137/// Otherwise, creates a new entry in the Global Offset Table for this Atom.
42pub fn getOrCreateOffsetTableEntry(self: Atom, elf_file: *Elf) !u32 {
43 const sym_index = self.getSymbolIndex().?;
38pub fn getOrCreateOffsetTableEntry(self: *const Atom, elf_file: *Elf) !u32 {
39 const sym_index = self.symbolIndex().?;
4440 if (elf_file.got_table.lookup.get(sym_index)) |index| return index;
4541 const index = try elf_file.got_table.allocateEntry(elf_file.base.allocator, sym_index);
4642 elf_file.got_table_count_dirty = true;
4743 return index;
4844}
4945
50pub fn getOffsetTableAddress(self: Atom, elf_file: *Elf) u64 {
51 const sym_index = self.getSymbolIndex().?;
46pub fn getOffsetTableAddress(self: *const Atom, elf_file: *Elf) u64 {
47 const sym_index = self.symbolIndex().?;
5248 const got_entry_index = elf_file.got_table.lookup.get(sym_index).?;
5349 const target = elf_file.base.options.target;
5450 const ptr_bits = target.ptrBitWidth();
......@@ -60,11 +56,11 @@ pub fn getOffsetTableAddress(self: Atom, elf_file: *Elf) u64 {
6056/// Returns how much room there is to grow in virtual address space.
6157/// File offset relocation happens transparently, so it is not included in
6258/// this calculation.
63pub fn capacity(self: Atom, elf_file: *const Elf) u64 {
64 const self_sym = self.getSymbol(elf_file);
59pub fn capacity(self: *const Atom, elf_file: *Elf) u64 {
60 const self_sym = self.symbol(elf_file);
6561 if (self.next_index) |next_index| {
66 const next = elf_file.getAtom(next_index);
67 const next_sym = next.getSymbol(elf_file);
62 const next = elf_file.atom(next_index);
63 const next_sym = next.symbol(elf_file);
6864 return next_sym.st_value - self_sym.st_value;
6965 } else {
7066 // We are the last block. The capacity is limited only by virtual address space.
......@@ -72,12 +68,12 @@ pub fn capacity(self: Atom, elf_file: *const Elf) u64 {
7268 }
7369}
7470
75pub fn freeListEligible(self: Atom, elf_file: *const Elf) bool {
71pub fn freeListEligible(self: *const Atom, elf_file: *Elf) bool {
7672 // No need to keep a free list node for the last block.
7773 const next_index = self.next_index orelse return false;
78 const next = elf_file.getAtom(next_index);
79 const self_sym = self.getSymbol(elf_file);
80 const next_sym = next.getSymbol(elf_file);
74 const next = elf_file.atom(next_index);
75 const self_sym = self.symbol(elf_file);
76 const next_sym = next.symbol(elf_file);
8177 const cap = next_sym.st_value - self_sym.st_value;
8278 const ideal_cap = Elf.padToIdeal(self_sym.st_size);
8379 if (cap <= ideal_cap) return false;