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...@@ -4315,7 +4315,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
4315 if (func_value.getFunction(mod)) |func| {4315 if (func_value.getFunction(mod)) |func| {
4316 if (self.bin_file.cast(link.File.Elf)) |elf_file| {4316 if (self.bin_file.cast(link.File.Elf)) |elf_file| {
4317 const atom_index = try elf_file.getOrCreateAtomForDecl(func.owner_decl);4317 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);
4319 _ = try atom.getOrCreateOffsetTableEntry(elf_file);4319 _ = try atom.getOrCreateOffsetTableEntry(elf_file);
4320 const got_addr = @as(u32, @intCast(atom.getOffsetTableAddress(elf_file)));4320 const got_addr = @as(u32, @intCast(atom.getOffsetTableAddress(elf_file)));
4321 try self.genSetReg(Type.usize, .x30, .{ .memory = got_addr });4321 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...@@ -4295,7 +4295,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
4295 if (func_value.getFunction(mod)) |func| {4295 if (func_value.getFunction(mod)) |func| {
4296 if (self.bin_file.cast(link.File.Elf)) |elf_file| {4296 if (self.bin_file.cast(link.File.Elf)) |elf_file| {
4297 const atom_index = try elf_file.getOrCreateAtomForDecl(func.owner_decl);4297 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);
4299 _ = try atom.getOrCreateOffsetTableEntry(elf_file);4299 _ = try atom.getOrCreateOffsetTableEntry(elf_file);
4300 const got_addr = @as(u32, @intCast(atom.getOffsetTableAddress(elf_file)));4300 const got_addr = @as(u32, @intCast(atom.getOffsetTableAddress(elf_file)));
4301 try self.genSetReg(Type.usize, .lr, .{ .memory = got_addr });4301 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...@@ -1748,7 +1748,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
1748 switch (mod.intern_pool.indexToKey(func_value.ip_index)) {1748 switch (mod.intern_pool.indexToKey(func_value.ip_index)) {
1749 .func => |func| {1749 .func => |func| {
1750 const atom_index = try elf_file.getOrCreateAtomForDecl(func.owner_decl);1750 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);
1752 _ = try atom.getOrCreateOffsetTableEntry(elf_file);1752 _ = try atom.getOrCreateOffsetTableEntry(elf_file);
1753 const got_addr = @as(u32, @intCast(atom.getOffsetTableAddress(elf_file)));1753 const got_addr = @as(u32, @intCast(atom.getOffsetTableAddress(elf_file)));
1754 try self.genSetReg(Type.usize, .ra, .{ .memory = got_addr });1754 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...@@ -1350,7 +1350,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
1350 .func => |func| {1350 .func => |func| {
1351 const got_addr = if (self.bin_file.cast(link.File.Elf)) |elf_file| blk: {1351 const got_addr = if (self.bin_file.cast(link.File.Elf)) |elf_file| blk: {
1352 const atom_index = try elf_file.getOrCreateAtomForDecl(func.owner_decl);1352 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);
1354 _ = try atom.getOrCreateOffsetTableEntry(elf_file);1354 _ = try atom.getOrCreateOffsetTableEntry(elf_file);
1355 break :blk @as(u32, @intCast(atom.getOffsetTableAddress(elf_file)));1355 break :blk @as(u32, @intCast(atom.getOffsetTableAddress(elf_file)));
1356 } else unreachable;1356 } 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...@@ -8150,7 +8150,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
8150 }) |owner_decl| {8150 }) |owner_decl| {
8151 if (self.bin_file.cast(link.File.Elf)) |elf_file| {8151 if (self.bin_file.cast(link.File.Elf)) |elf_file| {
8152 const atom_index = try elf_file.getOrCreateAtomForDecl(owner_decl);8152 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);
8154 _ = try atom.getOrCreateOffsetTableEntry(elf_file);8154 _ = try atom.getOrCreateOffsetTableEntry(elf_file);
8155 const got_addr = atom.getOffsetTableAddress(elf_file);8155 const got_addr = atom.getOffsetTableAddress(elf_file);
8156 try self.asmMemory(.{ ._, .call }, Memory.sib(.qword, .{8156 try self.asmMemory(.{ ._, .call }, Memory.sib(.qword, .{
...@@ -10217,7 +10217,7 @@ fn genLazySymbolRef(...@@ -10217,7 +10217,7 @@ fn genLazySymbolRef(
10217 if (self.bin_file.cast(link.File.Elf)) |elf_file| {10217 if (self.bin_file.cast(link.File.Elf)) |elf_file| {
10218 const atom_index = elf_file.getOrCreateAtomForLazySymbol(lazy_sym) catch |err|10218 const atom_index = elf_file.getOrCreateAtomForLazySymbol(lazy_sym) catch |err|
10219 return self.fail("{s} creating lazy symbol", .{@errorName(err)});10219 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);
10221 _ = try atom.getOrCreateOffsetTableEntry(elf_file);10221 _ = try atom.getOrCreateOffsetTableEntry(elf_file);
10222 const got_addr = atom.getOffsetTableAddress(elf_file);10222 const got_addr = atom.getOffsetTableAddress(elf_file);
10223 const got_mem =10223 const got_mem =
src/codegen.zig+2-2
...@@ -855,7 +855,7 @@ fn genDeclRef(...@@ -855,7 +855,7 @@ fn genDeclRef(
855855
856 if (bin_file.cast(link.File.Elf)) |elf_file| {856 if (bin_file.cast(link.File.Elf)) |elf_file| {
857 const atom_index = try elf_file.getOrCreateAtomForDecl(decl_index);857 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);
859 _ = try atom.getOrCreateOffsetTableEntry(elf_file);859 _ = try atom.getOrCreateOffsetTableEntry(elf_file);
860 return GenResult.mcv(.{ .memory = atom.getOffsetTableAddress(elf_file) });860 return GenResult.mcv(.{ .memory = atom.getOffsetTableAddress(elf_file) });
861 } else if (bin_file.cast(link.File.MachO)) |macho_file| {861 } else if (bin_file.cast(link.File.MachO)) |macho_file| {
...@@ -892,7 +892,7 @@ fn genUnnamedConst(...@@ -892,7 +892,7 @@ fn genUnnamedConst(
892 return GenResult.fail(bin_file.allocator, src_loc, "lowering unnamed constant failed: {s}", .{@errorName(err)});892 return GenResult.fail(bin_file.allocator, src_loc, "lowering unnamed constant failed: {s}", .{@errorName(err)});
893 };893 };
894 if (bin_file.cast(link.File.Elf)) |elf_file| {894 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 });
896 } else if (bin_file.cast(link.File.MachO)) |_| {896 } else if (bin_file.cast(link.File.MachO)) |_| {
897 return GenResult.mcv(.{ .load_direct = local_sym_index });897 return GenResult.mcv(.{ .load_direct = local_sym_index });
898 } else if (bin_file.cast(link.File.Coff)) |_| {898 } 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....@@ -280,10 +280,10 @@ pub fn getDeclVAddr(self: *Elf, decl_index: Module.Decl.Index, reloc_info: File.
280 assert(self.llvm_object == null);280 assert(self.llvm_object == null);
281281
282 const this_atom_index = try self.getOrCreateAtomForDecl(decl_index);282 const this_atom_index = try self.getOrCreateAtomForDecl(decl_index);
283 const this_atom = self.getAtom(this_atom_index);283 const this_atom = self.atom(this_atom_index);
284 const target = this_atom.getSymbolIndex().?;284 const target = this_atom.symbolIndex().?;
285 const vaddr = this_atom.getSymbol(self).st_value;285 const vaddr = this_atom.symbol(self).st_value;
286 const atom_index = self.getAtomIndexForSymbol(reloc_info.parent_atom_index).?;286 const atom_index = self.atomIndexForSymbol(reloc_info.parent_atom_index).?;
287 try Atom.addRelocation(self, atom_index, .{287 try Atom.addRelocation(self, atom_index, .{
288 .target = target,288 .target = target,
289 .offset = reloc_info.offset,289 .offset = reloc_info.offset,
...@@ -849,8 +849,8 @@ fn growAllocSection(self: *Elf, shdr_index: u16, needed_size: u64) !void {...@@ -849,8 +849,8 @@ fn growAllocSection(self: *Elf, shdr_index: u16, needed_size: u64) !void {
849 // Must move the entire section.849 // Must move the entire section.
850 const new_offset = self.findFreeSpace(needed_size, self.page_size);850 const new_offset = self.findFreeSpace(needed_size, self.page_size);
851 const existing_size = if (maybe_last_atom_index) |last_atom_index| blk: {851 const existing_size = if (maybe_last_atom_index) |last_atom_index| blk: {
852 const last = self.getAtom(last_atom_index);852 const last = self.atom(last_atom_index);
853 const sym = last.getSymbol(self);853 const sym = last.symbol(self);
854 break :blk (sym.st_value + sym.st_size) - phdr.p_vaddr;854 break :blk (sym.st_value + sym.st_size) - phdr.p_vaddr;
855 } else if (shdr_index == self.got_section_index.?) blk: {855 } else if (shdr_index == self.got_section_index.?) blk: {
856 break :blk shdr.sh_size;856 break :blk shdr.sh_size;
...@@ -1010,8 +1010,8 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node...@@ -1010,8 +1010,8 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
1010 while (it.next()) |entry| {1010 while (it.next()) |entry| {
1011 const atom_index = entry.key_ptr.*;1011 const atom_index = entry.key_ptr.*;
1012 const relocs = entry.value_ptr.*;1012 const relocs = entry.value_ptr.*;
1013 const atom = self.getAtom(atom_index);1013 const atom_ptr = self.atom(atom_index);
1014 const source_sym = atom.getSymbol(self);1014 const source_sym = atom_ptr.symbol(self);
1015 const source_shdr = self.sections.items(.shdr)[source_sym.st_shndx];1015 const source_shdr = self.sections.items(.shdr)[source_sym.st_shndx];
10161016
1017 log.debug("relocating '{?s}'", .{self.strtab.get(source_sym.st_name)});1017 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...@@ -1471,9 +1471,9 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
1471 try argv.append(entry);1471 try argv.append(entry);
1472 }1472 }
14731473
1474 for (self.base.options.force_undefined_symbols.keys()) |symbol| {1474 for (self.base.options.force_undefined_symbols.keys()) |sym| {
1475 try argv.append("-u");1475 try argv.append("-u");
1476 try argv.append(symbol);1476 try argv.append(sym);
1477 }1477 }
14781478
1479 switch (self.base.options.hash_style) {1479 switch (self.base.options.hash_style) {
...@@ -2071,13 +2071,13 @@ fn writeElfHeader(self: *Elf) !void {...@@ -2071,13 +2071,13 @@ fn writeElfHeader(self: *Elf) !void {
2071}2071}
20722072
2073fn freeAtom(self: *Elf, atom_index: Atom.Index) void {2073fn freeAtom(self: *Elf, atom_index: Atom.Index) void {
2074 const atom = self.getAtom(atom_index);2074 const atom_ptr = self.atom(atom_index);
2075 log.debug("freeAtom {d} ({s})", .{ atom_index, atom.getName(self) });2075 log.debug("freeAtom {d} ({s})", .{ atom_index, atom_ptr.name(self) });
20762076
2077 Atom.freeRelocations(self, atom_index);2077 Atom.freeRelocations(self, atom_index);
20782078
2079 const gpa = self.base.allocator;2079 const gpa = self.base.allocator;
2080 const shndx = atom.getSymbol(self).st_shndx;2080 const shndx = atom_ptr.symbol(self).st_shndx;
2081 const free_list = &self.sections.items(.free_list)[shndx];2081 const free_list = &self.sections.items(.free_list)[shndx];
2082 var already_have_free_list_node = false;2082 var already_have_free_list_node = false;
2083 {2083 {
...@@ -2088,7 +2088,7 @@ fn freeAtom(self: *Elf, atom_index: Atom.Index) void {...@@ -2088,7 +2088,7 @@ fn freeAtom(self: *Elf, atom_index: Atom.Index) void {
2088 _ = free_list.swapRemove(i);2088 _ = free_list.swapRemove(i);
2089 continue;2089 continue;
2090 }2090 }
2091 if (free_list.items[i] == atom.prev_index) {2091 if (free_list.items[i] == atom_ptr.prev_index) {
2092 already_have_free_list_node = true;2092 already_have_free_list_node = true;
2093 }2093 }
2094 i += 1;2094 i += 1;
...@@ -2098,7 +2098,7 @@ fn freeAtom(self: *Elf, atom_index: Atom.Index) void {...@@ -2098,7 +2098,7 @@ fn freeAtom(self: *Elf, atom_index: Atom.Index) void {
2098 const maybe_last_atom_index = &self.sections.items(.last_atom_index)[shndx];2098 const maybe_last_atom_index = &self.sections.items(.last_atom_index)[shndx];
2099 if (maybe_last_atom_index.*) |last_atom_index| {2099 if (maybe_last_atom_index.*) |last_atom_index| {
2100 if (last_atom_index == atom_index) {2100 if (last_atom_index == atom_index) {
2101 if (atom.prev_index) |prev_index| {2101 if (atom_ptr.prev_index) |prev_index| {
2102 // TODO shrink the section size here2102 // TODO shrink the section size here
2103 maybe_last_atom_index.* = prev_index;2103 maybe_last_atom_index.* = prev_index;
2104 } else {2104 } else {
...@@ -2107,9 +2107,9 @@ fn freeAtom(self: *Elf, atom_index: Atom.Index) void {...@@ -2107,9 +2107,9 @@ fn freeAtom(self: *Elf, atom_index: Atom.Index) void {
2107 }2107 }
2108 }2108 }
21092109
2110 if (atom.prev_index) |prev_index| {2110 if (atom_ptr.prev_index) |prev_index| {
2111 const prev = self.getAtomPtr(prev_index);2111 const prev = self.atom(prev_index);
2112 prev.next_index = atom.next_index;2112 prev.next_index = atom_ptr.next_index;
21132113
2114 if (!already_have_free_list_node and prev.*.freeListEligible(self)) {2114 if (!already_have_free_list_node and prev.*.freeListEligible(self)) {
2115 // The free list is heuristics, it doesn't have to be perfect, so we can2115 // 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 {...@@ -2117,23 +2117,23 @@ fn freeAtom(self: *Elf, atom_index: Atom.Index) void {
2117 free_list.append(gpa, prev_index) catch {};2117 free_list.append(gpa, prev_index) catch {};
2118 }2118 }
2119 } else {2119 } else {
2120 self.getAtomPtr(atom_index).prev_index = null;2120 atom_ptr.prev_index = null;
2121 }2121 }
21222122
2123 if (atom.next_index) |next_index| {2123 if (atom_ptr.next_index) |next_index| {
2124 self.getAtomPtr(next_index).prev_index = atom.prev_index;2124 self.atom(next_index).prev_index = atom_ptr.prev_index;
2125 } else {2125 } else {
2126 self.getAtomPtr(atom_index).next_index = null;2126 atom_ptr.next_index = null;
2127 }2127 }
21282128
2129 // Appending to free lists is allowed to fail because the free lists are heuristics based anyway.2129 // 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
2132 log.debug("adding %{d} to local symbols free list", .{sym_index});2132 log.debug("adding %{d} to local symbols free list", .{sym_index});
2133 self.locals_free_list.append(gpa, sym_index) catch {};2133 self.locals_free_list.append(gpa, sym_index) catch {};
2134 self.locals.items[sym_index] = null_sym;2134 self.locals.items[sym_index] = null_sym;
2135 _ = self.atom_by_index_table.remove(sym_index);2135 _ = self.atom_by_index_table.remove(sym_index);
2136 self.getAtomPtr(atom_index).sym_index = 0;2136 atom_ptr.sym_index = 0;
2137 self.got_table.freeEntry(gpa, sym_index);2137 self.got_table.freeEntry(gpa, sym_index);
2138}2138}
21392139
...@@ -2144,10 +2144,10 @@ fn shrinkAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64) void {...@@ -2144,10 +2144,10 @@ fn shrinkAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64) void {
2144}2144}
21452145
2146fn growAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64, alignment: u64) !u64 {2146fn growAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64, alignment: u64) !u64 {
2147 const atom = self.getAtom(atom_index);2147 const atom_ptr = self.atom(atom_index);
2148 const sym = atom.getSymbol(self);2148 const sym = atom_ptr.symbol(self);
2149 const align_ok = mem.alignBackward(u64, sym.st_value, alignment) == sym.st_value;2149 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);
2151 if (!need_realloc) return sym.st_value;2151 if (!need_realloc) return sym.st_value;
2152 return self.allocateAtom(atom_index, new_block_size, alignment);2152 return self.allocateAtom(atom_index, new_block_size, alignment);
2153}2153}
...@@ -2155,10 +2155,10 @@ fn growAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64, alignment:...@@ -2155,10 +2155,10 @@ fn growAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64, alignment:
2155pub fn createAtom(self: *Elf) !Atom.Index {2155pub fn createAtom(self: *Elf) !Atom.Index {
2156 const gpa = self.base.allocator;2156 const gpa = self.base.allocator;
2157 const atom_index = @as(Atom.Index, @intCast(self.atoms.items.len));2157 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);
2159 const sym_index = try self.allocateSymbol();2159 const sym_index = try self.allocateSymbol();
2160 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom_index);2160 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom_index);
2161 atom.* = .{2161 atom_ptr.* = .{
2162 .sym_index = sym_index,2162 .sym_index = sym_index,
2163 .prev_index = null,2163 .prev_index = null,
2164 .next_index = null,2164 .next_index = null,
...@@ -2168,8 +2168,7 @@ pub fn createAtom(self: *Elf) !Atom.Index {...@@ -2168,8 +2168,7 @@ pub fn createAtom(self: *Elf) !Atom.Index {
2168}2168}
21692169
2170fn allocateAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64, alignment: u64) !u64 {2170fn allocateAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64, alignment: u64) !u64 {
2171 const atom = self.getAtom(atom_index);2171 const sym = self.atom(atom_index).symbol(self);
2172 const sym = atom.getSymbol(self);
2173 const phdr_index = self.sections.items(.phdr_index)[sym.st_shndx];2172 const phdr_index = self.sections.items(.phdr_index)[sym.st_shndx];
2174 const phdr = &self.program_headers.items[phdr_index];2173 const phdr = &self.program_headers.items[phdr_index];
2175 const shdr = &self.sections.items(.shdr)[sym.st_shndx];2174 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...@@ -2191,10 +2190,10 @@ fn allocateAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64, alignme
2191 var i: usize = if (self.base.child_pid == null) 0 else free_list.items.len;2190 var i: usize = if (self.base.child_pid == null) 0 else free_list.items.len;
2192 while (i < free_list.items.len) {2191 while (i < free_list.items.len) {
2193 const big_atom_index = free_list.items[i];2192 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);
2195 // We now have a pointer to a live atom that has too much capacity.2194 // We now have a pointer to a live atom that has too much capacity.
2196 // Is it enough that we could fit this new atom?2195 // 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);
2198 const capacity = big_atom.capacity(self);2197 const capacity = big_atom.capacity(self);
2199 const ideal_capacity = padToIdeal(capacity);2198 const ideal_capacity = padToIdeal(capacity);
2200 const ideal_capacity_end_vaddr = std.math.add(u64, big_atom_sym.st_value, ideal_capacity) catch ideal_capacity;2199 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...@@ -2225,8 +2224,8 @@ fn allocateAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64, alignme
2225 }2224 }
2226 break :blk new_start_vaddr;2225 break :blk new_start_vaddr;
2227 } else if (maybe_last_atom_index.*) |last_index| {2226 } else if (maybe_last_atom_index.*) |last_index| {
2228 const last = self.getAtom(last_index);2227 const last = self.atom(last_index);
2229 const last_sym = last.getSymbol(self);2228 const last_sym = last.symbol(self);
2230 const ideal_capacity = padToIdeal(last_sym.st_size);2229 const ideal_capacity = padToIdeal(last_sym.st_size);
2231 const ideal_capacity_end_vaddr = last_sym.st_value + ideal_capacity;2230 const ideal_capacity_end_vaddr = last_sym.st_value + ideal_capacity;
2232 const new_start_vaddr = mem.alignForward(u64, ideal_capacity_end_vaddr, alignment);2231 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...@@ -2239,7 +2238,7 @@ fn allocateAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64, alignme
2239 };2238 };
22402239
2241 const expand_section = if (atom_placement) |placement_index|2240 const expand_section = if (atom_placement) |placement_index|
2242 self.getAtom(placement_index).next_index == null2241 self.atom(placement_index).next_index == null
2243 else2242 else
2244 true;2243 true;
2245 if (expand_section) {2244 if (expand_section) {
...@@ -2263,23 +2262,22 @@ fn allocateAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64, alignme...@@ -2263,23 +2262,22 @@ fn allocateAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64, alignme
2263 // This function can also reallocate an atom.2262 // This function can also reallocate an atom.
2264 // In this case we need to "unplug" it from its previous location before2263 // In this case we need to "unplug" it from its previous location before
2265 // plugging it in to its new location.2264 // plugging it in to its new location.
2266 if (atom.prev_index) |prev_index| {2265 const atom_ptr = self.atom(atom_index);
2267 const prev = self.getAtomPtr(prev_index);2266 if (atom_ptr.prev_index) |prev_index| {
2268 prev.next_index = atom.next_index;2267 const prev = self.atom(prev_index);
2268 prev.next_index = atom_ptr.next_index;
2269 }2269 }
2270 if (atom.next_index) |next_index| {2270 if (atom_ptr.next_index) |next_index| {
2271 const next = self.getAtomPtr(next_index);2271 const next = self.atom(next_index);
2272 next.prev_index = atom.prev_index;2272 next.prev_index = atom_ptr.prev_index;
2273 }2273 }
22742274
2275 if (atom_placement) |big_atom_index| {2275 if (atom_placement) |big_atom_index| {
2276 const big_atom = self.getAtomPtr(big_atom_index);2276 const big_atom = self.atom(big_atom_index);
2277 const atom_ptr = self.getAtomPtr(atom_index);
2278 atom_ptr.prev_index = big_atom_index;2277 atom_ptr.prev_index = big_atom_index;
2279 atom_ptr.next_index = big_atom.next_index;2278 atom_ptr.next_index = big_atom.next_index;
2280 big_atom.next_index = atom_index;2279 big_atom.next_index = atom_index;
2281 } else {2280 } else {
2282 const atom_ptr = self.getAtomPtr(atom_index);
2283 atom_ptr.prev_index = null;2281 atom_ptr.prev_index = null;
2284 atom_ptr.next_index = null;2282 atom_ptr.next_index = null;
2285 }2283 }
...@@ -2320,7 +2318,7 @@ fn allocateGlobal(self: *Elf) !u32 {...@@ -2320,7 +2318,7 @@ fn allocateGlobal(self: *Elf) !u32 {
2320 log.debug(" (reusing global index {d})", .{index});2318 log.debug(" (reusing global index {d})", .{index});
2321 break :blk index;2319 break :blk index;
2322 } else {2320 } else {
2323 log.debug(" (allocating symbol index {d})", .{self.globals.items.len});2321 log.debug(" (allocating global index {d})", .{self.globals.items.len});
2324 const index = @as(u32, @intCast(self.globals.items.len));2322 const index = @as(u32, @intCast(self.globals.items.len));
2325 _ = self.globals.addOneAssumeCapacity();2323 _ = self.globals.addOneAssumeCapacity();
2326 break :blk index;2324 break :blk index;
...@@ -2332,8 +2330,8 @@ fn allocateGlobal(self: *Elf) !u32 {...@@ -2332,8 +2330,8 @@ fn allocateGlobal(self: *Elf) !u32 {
23322330
2333fn freeUnnamedConsts(self: *Elf, decl_index: Module.Decl.Index) void {2331fn freeUnnamedConsts(self: *Elf, decl_index: Module.Decl.Index) void {
2334 const unnamed_consts = self.unnamed_const_atoms.getPtr(decl_index) orelse return;2332 const unnamed_consts = self.unnamed_const_atoms.getPtr(decl_index) orelse return;
2335 for (unnamed_consts.items) |atom| {2333 for (unnamed_consts.items) |atom_index| {
2336 self.freeAtom(atom);2334 self.freeAtom(atom_index);
2337 }2335 }
2338 unnamed_consts.clearAndFree(self.base.allocator);2336 unnamed_consts.clearAndFree(self.base.allocator);
2339}2337}
...@@ -2373,13 +2371,13 @@ pub fn getOrCreateAtomForLazySymbol(self: *Elf, sym: File.LazySymbol) !Atom.Inde...@@ -2373,13 +2371,13 @@ pub fn getOrCreateAtomForLazySymbol(self: *Elf, sym: File.LazySymbol) !Atom.Inde
2373 .flushed => {},2371 .flushed => {},
2374 }2372 }
2375 metadata.state.* = .pending_flush;2373 metadata.state.* = .pending_flush;
2376 const atom = metadata.atom.*;2374 const atom_index = metadata.atom.*;
2377 // anyerror needs to be deferred until flushModule2375 // 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) {
2379 .code => self.text_section_index.?,2377 .code => self.text_section_index.?,
2380 .const_data => self.rodata_section_index.?,2378 .const_data => self.rodata_section_index.?,
2381 });2379 });
2382 return atom;2380 return atom_index;
2383}2381}
23842382
2385pub fn getOrCreateAtomForDecl(self: *Elf, decl_index: Module.Decl.Index) !Atom.Index {2383pub 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...@@ -2432,18 +2430,18 @@ fn updateDeclCode(self: *Elf, decl_index: Module.Decl.Index, code: []const u8, s
24322430
2433 const decl_metadata = self.decls.get(decl_index).?;2431 const decl_metadata = self.decls.get(decl_index).?;
2434 const atom_index = decl_metadata.atom;2432 const atom_index = decl_metadata.atom;
2435 const atom = self.getAtom(atom_index);2433 const atom_ptr = self.atom(atom_index);
2436 const local_sym_index = atom.getSymbolIndex().?;2434 const local_sym_index = atom_ptr.symbolIndex().?;
2435 const local_sym = atom_ptr.symbol(self);
24372436
2438 const shdr_index = decl_metadata.shdr;2437 const shdr_index = decl_metadata.shdr;
2439 if (atom.getSymbol(self).st_size != 0 and self.base.child_pid == null) {2438 if (atom_ptr.symbol(self).st_size != 0 and self.base.child_pid == null) {
2440 const local_sym = atom.getSymbolPtr(self);
2441 local_sym.st_name = try self.strtab.insert(gpa, decl_name);2439 local_sym.st_name = try self.strtab.insert(gpa, decl_name);
2442 local_sym.st_info = (elf.STB_LOCAL << 4) | stt_bits;2440 local_sym.st_info = (elf.STB_LOCAL << 4) | stt_bits;
2443 local_sym.st_other = 0;2441 local_sym.st_other = 0;
2444 local_sym.st_shndx = shdr_index;2442 local_sym.st_shndx = shdr_index;
24452443
2446 const capacity = atom.capacity(self);2444 const capacity = atom_ptr.capacity(self);
2447 const need_realloc = code.len > capacity or2445 const need_realloc = code.len > capacity or
2448 !mem.isAlignedGeneric(u64, local_sym.st_value, required_alignment);2446 !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...@@ -2463,7 +2461,6 @@ fn updateDeclCode(self: *Elf, decl_index: Module.Decl.Index, code: []const u8, s
2463 }2461 }
2464 local_sym.st_size = code.len;2462 local_sym.st_size = code.len;
2465 } else {2463 } else {
2466 const local_sym = atom.getSymbolPtr(self);
2467 local_sym.* = .{2464 local_sym.* = .{
2468 .st_name = try self.strtab.insert(gpa, decl_name),2465 .st_name = try self.strtab.insert(gpa, decl_name),
2469 .st_info = (elf.STB_LOCAL << 4) | stt_bits,2466 .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...@@ -2479,11 +2476,10 @@ fn updateDeclCode(self: *Elf, decl_index: Module.Decl.Index, code: []const u8, s
2479 local_sym.st_value = vaddr;2476 local_sym.st_value = vaddr;
2480 local_sym.st_size = code.len;2477 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);
2483 try self.writeOffsetTableEntry(got_entry_index);2480 try self.writeOffsetTableEntry(got_entry_index);
2484 }2481 }
24852482
2486 const local_sym = atom.getSymbolPtr(self);
2487 const phdr_index = self.sections.items(.phdr_index)[shdr_index];2483 const phdr_index = self.sections.items(.phdr_index)[shdr_index];
2488 const section_offset = local_sym.st_value - self.program_headers.items[phdr_index].p_vaddr;2484 const section_offset = local_sym.st_value - self.program_headers.items[phdr_index].p_vaddr;
2489 const file_offset = self.sections.items(.shdr)[shdr_index].sh_offset + section_offset;2485 const file_offset = self.sections.items(.shdr)[shdr_index].sh_offset + section_offset;
...@@ -2594,7 +2590,6 @@ pub fn updateDecl(...@@ -2594,7 +2590,6 @@ pub fn updateDecl(
25942590
2595 const atom_index = try self.getOrCreateAtomForDecl(decl_index);2591 const atom_index = try self.getOrCreateAtomForDecl(decl_index);
2596 Atom.freeRelocations(self, atom_index);2592 Atom.freeRelocations(self, atom_index);
2597 const atom = self.getAtom(atom_index);
25982593
2599 var code_buffer = std.ArrayList(u8).init(self.base.allocator);2594 var code_buffer = std.ArrayList(u8).init(self.base.allocator);
2600 defer code_buffer.deinit();2595 defer code_buffer.deinit();
...@@ -2611,14 +2606,14 @@ pub fn updateDecl(...@@ -2611,14 +2606,14 @@ pub fn updateDecl(
2611 }, &code_buffer, .{2606 }, &code_buffer, .{
2612 .dwarf = ds,2607 .dwarf = ds,
2613 }, .{2608 }, .{
2614 .parent_atom_index = atom.getSymbolIndex().?,2609 .parent_atom_index = self.atom(atom_index).symbolIndex().?,
2615 })2610 })
2616 else2611 else
2617 try codegen.generateSymbol(&self.base, decl.srcLoc(mod), .{2612 try codegen.generateSymbol(&self.base, decl.srcLoc(mod), .{
2618 .ty = decl.ty,2613 .ty = decl.ty,
2619 .val = decl_val,2614 .val = decl_val,
2620 }, &code_buffer, .none, .{2615 }, &code_buffer, .none, .{
2621 .parent_atom_index = atom.getSymbolIndex().?,2616 .parent_atom_index = self.atom(atom_index).symbolIndex().?,
2622 });2617 });
26232618
2624 const code = switch (res) {2619 const code = switch (res) {
...@@ -2669,8 +2664,8 @@ fn updateLazySymbolAtom(...@@ -2669,8 +2664,8 @@ fn updateLazySymbolAtom(
2669 };2664 };
2670 const name = self.strtab.get(name_str_index).?;2665 const name = self.strtab.get(name_str_index).?;
26712666
2672 const atom = self.getAtom(atom_index);2667 const atom_ptr = self.atom(atom_index);
2673 const local_sym_index = atom.getSymbolIndex().?;2668 const local_sym_index = atom_ptr.symbolIndex().?;
26742669
2675 const src = if (sym.ty.getOwnerDeclOrNull(mod)) |owner_decl|2670 const src = if (sym.ty.getOwnerDeclOrNull(mod)) |owner_decl|
2676 mod.declPtr(owner_decl).srcLoc(mod)2671 mod.declPtr(owner_decl).srcLoc(mod)
...@@ -2698,7 +2693,7 @@ fn updateLazySymbolAtom(...@@ -2698,7 +2693,7 @@ fn updateLazySymbolAtom(
2698 };2693 };
26992694
2700 const phdr_index = self.sections.items(.phdr_index)[shdr_index];2695 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);
2702 local_sym.* = .{2697 local_sym.* = .{
2703 .st_name = name_str_index,2698 .st_name = name_str_index,
2704 .st_info = (elf.STB_LOCAL << 4) | elf.STT_OBJECT,2699 .st_info = (elf.STB_LOCAL << 4) | elf.STT_OBJECT,
...@@ -2714,7 +2709,7 @@ fn updateLazySymbolAtom(...@@ -2714,7 +2709,7 @@ fn updateLazySymbolAtom(
2714 local_sym.st_value = vaddr;2709 local_sym.st_value = vaddr;
2715 local_sym.st_size = code.len;2710 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);
2718 try self.writeOffsetTableEntry(got_entry_index);2713 try self.writeOffsetTableEntry(got_entry_index);
27192714
2720 const section_offset = vaddr - self.program_headers.items[phdr_index].p_vaddr;2715 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...@@ -2750,7 +2745,7 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module
2750 const res = try codegen.generateSymbol(&self.base, decl.srcLoc(mod), typed_value, &code_buffer, .{2745 const res = try codegen.generateSymbol(&self.base, decl.srcLoc(mod), typed_value, &code_buffer, .{
2751 .none = {},2746 .none = {},
2752 }, .{2747 }, .{
2753 .parent_atom_index = self.getAtom(atom_index).getSymbolIndex().?,2748 .parent_atom_index = self.atom(atom_index).symbolIndex().?,
2754 });2749 });
2755 const code = switch (res) {2750 const code = switch (res) {
2756 .ok => code_buffer.items,2751 .ok => code_buffer.items,
...@@ -2765,7 +2760,7 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module...@@ -2765,7 +2760,7 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module
2765 const required_alignment = typed_value.ty.abiAlignment(mod);2760 const required_alignment = typed_value.ty.abiAlignment(mod);
2766 const shdr_index = self.rodata_section_index.?;2761 const shdr_index = self.rodata_section_index.?;
2767 const phdr_index = self.sections.items(.phdr_index)[shdr_index];2762 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);
2769 local_sym.st_name = name_str_index;2764 local_sym.st_name = name_str_index;
2770 local_sym.st_info = (elf.STB_LOCAL << 4) | elf.STT_OBJECT;2765 local_sym.st_info = (elf.STB_LOCAL << 4) | elf.STT_OBJECT;
2771 local_sym.st_other = 0;2766 local_sym.st_other = 0;
...@@ -2782,7 +2777,7 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module...@@ -2782,7 +2777,7 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module
2782 const file_offset = self.sections.items(.shdr)[shdr_index].sh_offset + section_offset;2777 const file_offset = self.sections.items(.shdr)[shdr_index].sh_offset + section_offset;
2783 try self.base.file.?.pwriteAll(code, file_offset);2778 try self.base.file.?.pwriteAll(code, file_offset);
27842779
2785 return self.getAtom(atom_index).getSymbolIndex().?;2780 return self.atom(atom_index).symbolIndex().?;
2786}2781}
27872782
2788pub fn updateDeclExports(2783pub fn updateDeclExports(
...@@ -2805,8 +2800,8 @@ pub fn updateDeclExports(...@@ -2805,8 +2800,8 @@ pub fn updateDeclExports(
28052800
2806 const decl = mod.declPtr(decl_index);2801 const decl = mod.declPtr(decl_index);
2807 const atom_index = try self.getOrCreateAtomForDecl(decl_index);2802 const atom_index = try self.getOrCreateAtomForDecl(decl_index);
2808 const atom = self.getAtom(atom_index);2803 const atom_ptr = self.atom(atom_index);
2809 const decl_sym = atom.getSymbol(self);2804 const decl_sym = atom_ptr.symbol(self);
2810 const decl_metadata = self.decls.getPtr(decl_index).?;2805 const decl_metadata = self.decls.getPtr(decl_index).?;
2811 const shdr_index = decl_metadata.shdr;2806 const shdr_index = decl_metadata.shdr;
28122807
...@@ -2843,12 +2838,12 @@ pub fn updateDeclExports(...@@ -2843,12 +2838,12 @@ pub fn updateDeclExports(
2843 };2838 };
2844 const stt_bits: u8 = @as(u4, @truncate(decl_sym.st_info));2839 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: {
2847 const sym_index = try self.allocateSymbol();2842 const sym_index = try self.allocateSymbol();
2848 try decl_metadata.exports.append(gpa, sym_index);2843 try decl_metadata.exports.append(gpa, sym_index);
2849 break :blk sym_index;2844 break :blk sym_index;
2850 };2845 };
2851 const sym = self.getSymbolPtr(sym_index);2846 const sym = self.symbol(sym_index);
2852 sym.* = .{2847 sym.* = .{
2853 .st_name = try self.strtab.insert(gpa, exp_name),2848 .st_name = try self.strtab.insert(gpa, exp_name),
2854 .st_info = (stb_bits << 4) | stt_bits,2849 .st_info = (stb_bits << 4) | stt_bits,
...@@ -2857,8 +2852,8 @@ pub fn updateDeclExports(...@@ -2857,8 +2852,8 @@ pub fn updateDeclExports(
2857 .st_value = decl_sym.st_value,2852 .st_value = decl_sym.st_value,
2858 .st_size = decl_sym.st_size,2853 .st_size = decl_sym.st_size,
2859 };2854 };
2860 const sym_name = self.getSymbolName(sym_index);2855 const sym_name = self.symbolName(sym_index);
2861 const gop = try self.getOrPutGlobalPtr(sym_name);2856 const gop = try self.getOrPutGlobal(sym_name);
2862 gop.value_ptr.* = sym_index;2857 gop.value_ptr.* = sym_index;
2863 }2858 }
2864}2859}
...@@ -2889,8 +2884,8 @@ pub fn deleteDeclExport(...@@ -2889,8 +2884,8 @@ pub fn deleteDeclExport(
2889 const gpa = self.base.allocator;2884 const gpa = self.base.allocator;
2890 const mod = self.base.options.module.?;2885 const mod = self.base.options.module.?;
2891 const exp_name = mod.intern_pool.stringToSlice(name);2886 const exp_name = mod.intern_pool.stringToSlice(name);
2892 const sym_index = metadata.getExportPtr(self, exp_name) orelse return;2887 const sym_index = metadata.@"export"(self, exp_name) orelse return;
2893 const sym = self.getSymbolPtr(sym_index.*);2888 const sym = self.symbol(sym_index.*);
2894 log.debug("deleting export '{s}'", .{exp_name});2889 log.debug("deleting export '{s}'", .{exp_name});
2895 sym.* = null_sym;2890 sym.* = null_sym;
2896 self.locals_free_list.append(gpa, sym_index.*) catch {};2891 self.locals_free_list.append(gpa, sym_index.*) catch {};
...@@ -2960,7 +2955,7 @@ fn writeOffsetTableEntry(self: *Elf, index: @TypeOf(self.got_table).Index) !void...@@ -2960,7 +2955,7 @@ fn writeOffsetTableEntry(self: *Elf, index: @TypeOf(self.got_table).Index) !void
2960 const phdr = &self.program_headers.items[self.phdr_got_index.?];2955 const phdr = &self.program_headers.items[self.phdr_got_index.?];
2961 const vaddr = phdr.p_vaddr + @as(u64, entry_size) * index;2956 const vaddr = phdr.p_vaddr + @as(u64, entry_size) * index;
2962 const got_entry = self.got_table.entries.items[index];2957 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;
2964 switch (entry_size) {2959 switch (entry_size) {
2965 2 => {2960 2 => {
2966 var buf: [2]u8 = undefined;2961 var buf: [2]u8 = undefined;
...@@ -3046,8 +3041,8 @@ fn writeSymbols(self: *Elf) !void {...@@ -3046,8 +3041,8 @@ fn writeSymbols(self: *Elf) !void {
3046 }3041 }
3047 }3042 }
30483043
3049 for (buf[self.locals.items.len..], self.globals.items) |*sym, global| {3044 for (buf[self.locals.items.len..], self.globals.items) |*sym, glob| {
3050 elf32SymFromSym(self.getSymbol(global), sym);3045 elf32SymFromSym(self.symbol(glob).*, sym);
3051 if (foreign_endian) {3046 if (foreign_endian) {
3052 mem.byteSwapAllFields(elf.Elf32_Sym, sym);3047 mem.byteSwapAllFields(elf.Elf32_Sym, sym);
3053 }3048 }
...@@ -3064,8 +3059,8 @@ fn writeSymbols(self: *Elf) !void {...@@ -3064,8 +3059,8 @@ fn writeSymbols(self: *Elf) !void {
3064 }3059 }
3065 }3060 }
30663061
3067 for (buf[self.locals.items.len..], self.globals.items) |*sym, global| {3062 for (buf[self.locals.items.len..], self.globals.items) |*sym, glob| {
3068 sym.* = self.getSymbol(global);3063 sym.* = self.symbol(glob).*;
3069 if (foreign_endian) {3064 if (foreign_endian) {
3070 mem.byteSwapAllFields(elf.Elf64_Sym, sym);3065 mem.byteSwapAllFields(elf.Elf64_Sym, sym);
3071 }3066 }
...@@ -3369,8 +3364,8 @@ fn logSymtab(self: Elf) void {...@@ -3369,8 +3364,8 @@ fn logSymtab(self: Elf) void {
3369 log.debug(" {d}: {?s}: @{x} in {d}", .{ id, self.strtab.get(sym.st_name), sym.st_value, sym.st_shndx });3364 log.debug(" {d}: {?s}: @{x} in {d}", .{ id, self.strtab.get(sym.st_name), sym.st_value, sym.st_shndx });
3370 }3365 }
3371 log.debug("globals:", .{});3366 log.debug("globals:", .{});
3372 for (self.globals.items, 0..) |global, id| {3367 for (self.globals.items, 0..) |glob, id| {
3373 const sym = self.getSymbol(global);3368 const sym = self.symbol(glob);
3374 log.debug(" {d}: {?s}: @{x} in {d}", .{ id, self.strtab.get(sym.st_name), sym.st_value, sym.st_shndx });3369 log.debug(" {d}: {?s}: @{x} in {d}", .{ id, self.strtab.get(sym.st_name), sym.st_value, sym.st_shndx });
3375 }3370 }
3376}3371}
...@@ -3386,45 +3381,34 @@ pub fn getProgramHeaderPtr(self: *Elf, shdr_index: u16) *elf.Elf64_Phdr {...@@ -3386,45 +3381,34 @@ pub fn getProgramHeaderPtr(self: *Elf, shdr_index: u16) *elf.Elf64_Phdr {
3386}3381}
33873382
3388/// Returns pointer-to-symbol described at sym_index.3383/// 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 {
3390 return &self.locals.items[sym_index];3385 return &self.locals.items[sym_index];
3391}3386}
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
3398/// Returns name of the symbol at sym_index.3388/// 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 {
3400 const sym = self.locals.items[sym_index];3390 const sym = self.locals.items[sym_index];
3401 return self.strtab.get(sym.st_name).?;3391 return self.strtab.get(sym.st_name).?;
3402}3392}
34033393
3404/// Returns pointer to the global entry for `name` if one exists.3394/// 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 {
3406 const global_index = self.resolver.get(name) orelse return null;3396 const global_index = self.resolver.get(name) orelse return null;
3407 return &self.globals.items[global_index];3397 return &self.globals.items[global_index];
3408}3398}
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
3416/// Returns the index of the global entry for `name` if one exists.3400/// 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 {
3418 return self.resolver.get(name);3402 return self.resolver.get(name);
3419}3403}
34203404
3421/// Returns global entry at `index`.3405/// Returns global entry at `index`.
3422pub fn getGlobalByIndex(self: *const Elf, index: u32) u32 {3406pub fn globalByIndex(self: *const Elf, index: u32) u32 {
3423 assert(index < self.globals.items.len);3407 assert(index < self.globals.items.len);
3424 return self.globals.items[index];3408 return self.globals.items[index];
3425}3409}
34263410
3427const GetOrPutGlobalPtrResult = struct {3411const GetOrPutGlobalResult = struct {
3428 found_existing: bool,3412 found_existing: bool,
3429 value_ptr: *u32,3413 value_ptr: *u32,
3430};3414};
...@@ -3432,31 +3416,26 @@ const GetOrPutGlobalPtrResult = struct {...@@ -3432,31 +3416,26 @@ const GetOrPutGlobalPtrResult = struct {
3432/// Return pointer to the global entry for `name` if one exists.3416/// Return pointer to the global entry for `name` if one exists.
3433/// Puts a new global entry for `name` if one doesn't exist, and3417/// Puts a new global entry for `name` if one doesn't exist, and
3434/// returns a pointer to it.3418/// returns a pointer to it.
3435pub fn getOrPutGlobalPtr(self: *Elf, name: []const u8) !GetOrPutGlobalPtrResult {3419pub fn getOrPutGlobal(self: *Elf, name: []const u8) !GetOrPutGlobalResult {
3436 if (self.getGlobalPtr(name)) |ptr| {3420 if (self.global(name)) |ptr| {
3437 return GetOrPutGlobalPtrResult{ .found_existing = true, .value_ptr = ptr };3421 return GetOrPutGlobalResult{ .found_existing = true, .value_ptr = ptr };
3438 }3422 }
3439 const gpa = self.base.allocator;3423 const gpa = self.base.allocator;
3440 const global_index = try self.allocateGlobal();3424 const global_index = try self.allocateGlobal();
3441 const global_name = try gpa.dupe(u8, name);3425 const global_name = try gpa.dupe(u8, name);
3442 _ = try self.resolver.put(gpa, global_name, global_index);3426 _ = try self.resolver.put(gpa, global_name, global_index);
3443 const ptr = &self.globals.items[global_index];3427 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 };
3445}3429}
34463430
3447pub fn getAtom(self: *const Elf, atom_index: Atom.Index) Atom {3431pub fn atom(self: *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 {
3453 assert(atom_index < self.atoms.items.len);3432 assert(atom_index < self.atoms.items.len);
3454 return &self.atoms.items[atom_index];3433 return &self.atoms.items[atom_index];
3455}3434}
34563435
3457/// Returns atom if there is an atom referenced by the symbol.3436/// Returns atom if there is an atom referenced by the symbol.
3458/// Returns null on failure.3437/// Returns null on failure.
3459pub fn getAtomIndexForSymbol(self: *Elf, sym_index: u32) ?Atom.Index {3438pub fn atomIndexForSymbol(self: *Elf, sym_index: u32) ?Atom.Index {
3460 return self.atom_by_index_table.get(sym_index);3439 return self.atom_by_index_table.get(sym_index);
3461}3440}
34623441
...@@ -3512,16 +3491,9 @@ const DeclMetadata = struct {...@@ -3512,16 +3491,9 @@ const DeclMetadata = struct {
3512 /// A list of all exports aliases of this Decl.3491 /// A list of all exports aliases of this Decl.
3513 exports: std.ArrayListUnmanaged(u32) = .{},3492 exports: std.ArrayListUnmanaged(u32) = .{},
35143493
3515 fn getExport(m: DeclMetadata, elf_file: *const Elf, name: []const u8) ?u32 {3494 fn @"export"(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 {
3523 for (m.exports.items) |*exp| {3495 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;
3525 }3497 }
3526 return null;3498 return null;
3527 }3499 }
src/link/Elf/Atom.zig+17-21
...@@ -20,35 +20,31 @@ pub const Reloc = struct {...@@ -20,35 +20,31 @@ pub const Reloc = struct {
20 prev_vaddr: u64,20 prev_vaddr: u64,
21};21};
2222
23pub fn getSymbolIndex(self: Atom) ?u32 {23pub fn symbolIndex(self: *const Atom) ?u32 {
24 if (self.sym_index == 0) return null;24 if (self.sym_index == 0) return null;
25 return self.sym_index;25 return self.sym_index;
26}26}
2727
28pub fn getSymbol(self: Atom, elf_file: *const Elf) elf.Elf64_Sym {28pub fn symbol(self: *const Atom, elf_file: *Elf) *elf.Elf64_Sym {
29 return elf_file.getSymbol(self.getSymbolIndex().?);29 return elf_file.symbol(self.symbolIndex().?);
30}30}
3131
32pub fn getSymbolPtr(self: Atom, elf_file: *Elf) *elf.Elf64_Sym {32pub fn name(self: *const Atom, elf_file: *Elf) []const u8 {
33 return elf_file.getSymbolPtr(self.getSymbolIndex().?);33 return elf_file.symbolName(self.symbolIndex().?);
34}
35
36pub fn getName(self: Atom, elf_file: *const Elf) []const u8 {
37 return elf_file.getSymbolName(self.getSymbolIndex().?);
38}34}
3935
40/// If entry already exists, returns index to it.36/// If entry already exists, returns index to it.
41/// Otherwise, creates a new entry in the Global Offset Table for this Atom.37/// Otherwise, creates a new entry in the Global Offset Table for this Atom.
42pub fn getOrCreateOffsetTableEntry(self: Atom, elf_file: *Elf) !u32 {38pub fn getOrCreateOffsetTableEntry(self: *const Atom, elf_file: *Elf) !u32 {
43 const sym_index = self.getSymbolIndex().?;39 const sym_index = self.symbolIndex().?;
44 if (elf_file.got_table.lookup.get(sym_index)) |index| return index;40 if (elf_file.got_table.lookup.get(sym_index)) |index| return index;
45 const index = try elf_file.got_table.allocateEntry(elf_file.base.allocator, sym_index);41 const index = try elf_file.got_table.allocateEntry(elf_file.base.allocator, sym_index);
46 elf_file.got_table_count_dirty = true;42 elf_file.got_table_count_dirty = true;
47 return index;43 return index;
48}44}
4945
50pub fn getOffsetTableAddress(self: Atom, elf_file: *Elf) u64 {46pub fn getOffsetTableAddress(self: *const Atom, elf_file: *Elf) u64 {
51 const sym_index = self.getSymbolIndex().?;47 const sym_index = self.symbolIndex().?;
52 const got_entry_index = elf_file.got_table.lookup.get(sym_index).?;48 const got_entry_index = elf_file.got_table.lookup.get(sym_index).?;
53 const target = elf_file.base.options.target;49 const target = elf_file.base.options.target;
54 const ptr_bits = target.ptrBitWidth();50 const ptr_bits = target.ptrBitWidth();
...@@ -60,11 +56,11 @@ pub fn getOffsetTableAddress(self: Atom, elf_file: *Elf) u64 {...@@ -60,11 +56,11 @@ pub fn getOffsetTableAddress(self: Atom, elf_file: *Elf) u64 {
60/// Returns how much room there is to grow in virtual address space.56/// Returns how much room there is to grow in virtual address space.
61/// File offset relocation happens transparently, so it is not included in57/// File offset relocation happens transparently, so it is not included in
62/// this calculation.58/// this calculation.
63pub fn capacity(self: Atom, elf_file: *const Elf) u64 {59pub fn capacity(self: *const Atom, elf_file: *Elf) u64 {
64 const self_sym = self.getSymbol(elf_file);60 const self_sym = self.symbol(elf_file);
65 if (self.next_index) |next_index| {61 if (self.next_index) |next_index| {
66 const next = elf_file.getAtom(next_index);62 const next = elf_file.atom(next_index);
67 const next_sym = next.getSymbol(elf_file);63 const next_sym = next.symbol(elf_file);
68 return next_sym.st_value - self_sym.st_value;64 return next_sym.st_value - self_sym.st_value;
69 } else {65 } else {
70 // We are the last block. The capacity is limited only by virtual address space.66 // 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 {...@@ -72,12 +68,12 @@ pub fn capacity(self: Atom, elf_file: *const Elf) u64 {
72 }68 }
73}69}
7470
75pub fn freeListEligible(self: Atom, elf_file: *const Elf) bool {71pub fn freeListEligible(self: *const Atom, elf_file: *Elf) bool {
76 // No need to keep a free list node for the last block.72 // No need to keep a free list node for the last block.
77 const next_index = self.next_index orelse return false;73 const next_index = self.next_index orelse return false;
78 const next = elf_file.getAtom(next_index);74 const next = elf_file.atom(next_index);
79 const self_sym = self.getSymbol(elf_file);75 const self_sym = self.symbol(elf_file);
80 const next_sym = next.getSymbol(elf_file);76 const next_sym = next.symbol(elf_file);
81 const cap = next_sym.st_value - self_sym.st_value;77 const cap = next_sym.st_value - self_sym.st_value;
82 const ideal_cap = Elf.padToIdeal(self_sym.st_size);78 const ideal_cap = Elf.padToIdeal(self_sym.st_size);
83 if (cap <= ideal_cap) return false;79 if (cap <= ideal_cap) return false;