authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-05 23:10:04+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-06 13:14:00+02:00
loga9df098cd2dd04e2c363b439233ff2e14e198413
treee9b6373bcc6bd41feddc4bf00325b0c4ef2383ed
parentd9fffd431a89ed4104bcc0b2165bfb9917cdd82b

elf: make everything upside down - track by Symbol.Index rather than Atom.Index


13 files changed, 607 insertions(+), 588 deletions(-)

src/arch/aarch64/CodeGen.zig+4-4
......@@ -4314,10 +4314,10 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
43144314 if (try self.air.value(callee, mod)) |func_value| {
43154315 if (func_value.getFunction(mod)) |func| {
43164316 if (self.bin_file.cast(link.File.Elf)) |elf_file| {
4317 const atom_index = try elf_file.getOrCreateAtomForDecl(func.owner_decl);
4318 const atom = elf_file.atom(atom_index);
4319 _ = try atom.getOrCreateOffsetTableEntry(elf_file);
4320 const got_addr = @as(u32, @intCast(atom.getOffsetTableAddress(elf_file)));
4317 const sym_index = try elf_file.getOrCreateMetadataForDecl(func.owner_decl);
4318 const sym = elf_file.symbol(sym_index);
4319 _ = try sym.getOrCreateOffsetTableEntry(elf_file);
4320 const got_addr = @as(u32, @intCast(sym.getOffsetTableAddress(elf_file)));
43214321 try self.genSetReg(Type.usize, .x30, .{ .memory = got_addr });
43224322 } else if (self.bin_file.cast(link.File.MachO)) |macho_file| {
43234323 const atom = try macho_file.getOrCreateAtomForDecl(func.owner_decl);
src/arch/arm/CodeGen.zig+4-4
......@@ -4294,10 +4294,10 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
42944294 if (try self.air.value(callee, mod)) |func_value| {
42954295 if (func_value.getFunction(mod)) |func| {
42964296 if (self.bin_file.cast(link.File.Elf)) |elf_file| {
4297 const atom_index = try elf_file.getOrCreateAtomForDecl(func.owner_decl);
4298 const atom = elf_file.atom(atom_index);
4299 _ = try atom.getOrCreateOffsetTableEntry(elf_file);
4300 const got_addr = @as(u32, @intCast(atom.getOffsetTableAddress(elf_file)));
4297 const sym_index = try elf_file.getOrCreateMetadataForDecl(func.owner_decl);
4298 const sym = elf_file.symbol(sym_index);
4299 _ = try sym.getOrCreateOffsetTableEntry(elf_file);
4300 const got_addr = @as(u32, @intCast(sym.getOffsetTableAddress(elf_file)));
43014301 try self.genSetReg(Type.usize, .lr, .{ .memory = got_addr });
43024302 } else if (self.bin_file.cast(link.File.MachO)) |_| {
43034303 unreachable; // unsupported architecture for MachO
src/arch/riscv64/CodeGen.zig+4-4
......@@ -1747,10 +1747,10 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
17471747 if (try self.air.value(callee, mod)) |func_value| {
17481748 switch (mod.intern_pool.indexToKey(func_value.ip_index)) {
17491749 .func => |func| {
1750 const atom_index = try elf_file.getOrCreateAtomForDecl(func.owner_decl);
1751 const atom = elf_file.atom(atom_index);
1752 _ = try atom.getOrCreateOffsetTableEntry(elf_file);
1753 const got_addr = @as(u32, @intCast(atom.getOffsetTableAddress(elf_file)));
1750 const sym_index = try elf_file.getOrCreateMetadataForDecl(func.owner_decl);
1751 const sym = elf_file.symbol(sym_index);
1752 _ = try sym.getOrCreateOffsetTableEntry(elf_file);
1753 const got_addr = @as(u32, @intCast(sym.getOffsetTableAddress(elf_file)));
17541754 try self.genSetReg(Type.usize, .ra, .{ .memory = got_addr });
17551755 _ = try self.addInst(.{
17561756 .tag = .jalr,
src/arch/sparc64/CodeGen.zig+4-4
......@@ -1349,10 +1349,10 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
13491349 switch (mod.intern_pool.indexToKey(func_value.ip_index)) {
13501350 .func => |func| {
13511351 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);
1353 const atom = elf_file.atom(atom_index);
1354 _ = try atom.getOrCreateOffsetTableEntry(elf_file);
1355 break :blk @as(u32, @intCast(atom.getOffsetTableAddress(elf_file)));
1352 const sym_index = try elf_file.getOrCreateMetadataForDecl(func.owner_decl);
1353 const sym = elf_file.symbol(sym_index);
1354 _ = try sym.getOrCreateOffsetTableEntry(elf_file);
1355 break :blk @as(u32, @intCast(sym.getOffsetTableAddress(elf_file)));
13561356 } else unreachable;
13571357
13581358 try self.genSetReg(Type.usize, .o7, .{ .memory = got_addr });
src/arch/x86_64/CodeGen.zig+8-8
......@@ -8149,10 +8149,10 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
81498149 else => null,
81508150 }) |owner_decl| {
81518151 if (self.bin_file.cast(link.File.Elf)) |elf_file| {
8152 const atom_index = try elf_file.getOrCreateAtomForDecl(owner_decl);
8153 const atom = elf_file.atom(atom_index);
8154 _ = try atom.getOrCreateOffsetTableEntry(elf_file);
8155 const got_addr = atom.getOffsetTableAddress(elf_file);
8152 const sym_index = try elf_file.getOrCreateMetadataForDecl(owner_decl);
8153 const sym = elf_file.symbol(sym_index);
8154 _ = try sym.getOrCreateOffsetTableEntry(elf_file);
8155 const got_addr = sym.getOffsetTableAddress(elf_file);
81568156 try self.asmMemory(.{ ._, .call }, Memory.sib(.qword, .{
81578157 .base = .{ .reg = .ds },
81588158 .disp = @intCast(got_addr),
......@@ -10215,11 +10215,11 @@ fn genLazySymbolRef(
1021510215 lazy_sym: link.File.LazySymbol,
1021610216) InnerError!void {
1021710217 if (self.bin_file.cast(link.File.Elf)) |elf_file| {
10218 const atom_index = elf_file.getOrCreateAtomForLazySymbol(lazy_sym) catch |err|
10218 const sym_index = elf_file.getOrCreateMetadataForLazySymbol(lazy_sym) catch |err|
1021910219 return self.fail("{s} creating lazy symbol", .{@errorName(err)});
10220 const atom = elf_file.atom(atom_index);
10221 _ = try atom.getOrCreateOffsetTableEntry(elf_file);
10222 const got_addr = atom.getOffsetTableAddress(elf_file);
10220 const sym = elf_file.symbol(sym_index);
10221 _ = try sym.getOrCreateOffsetTableEntry(elf_file);
10222 const got_addr = sym.getOffsetTableAddress(elf_file);
1022310223 const got_mem =
1022410224 Memory.sib(.qword, .{ .base = .{ .reg = .ds }, .disp = @intCast(got_addr) });
1022510225 switch (tag) {
src/codegen.zig+5-5
......@@ -854,10 +854,10 @@ fn genDeclRef(
854854 const is_threadlocal = tv.val.isPtrToThreadLocal(mod) and !bin_file.options.single_threaded;
855855
856856 if (bin_file.cast(link.File.Elf)) |elf_file| {
857 const atom_index = try elf_file.getOrCreateAtomForDecl(decl_index);
858 const atom = elf_file.atom(atom_index);
859 _ = try atom.getOrCreateOffsetTableEntry(elf_file);
860 return GenResult.mcv(.{ .memory = atom.getOffsetTableAddress(elf_file) });
857 const sym_index = try elf_file.getOrCreateMetadataForDecl(decl_index);
858 const sym = elf_file.symbol(sym_index);
859 _ = try sym.getOrCreateOffsetTableEntry(elf_file);
860 return GenResult.mcv(.{ .memory = sym.getOffsetTableAddress(elf_file) });
861861 } else if (bin_file.cast(link.File.MachO)) |macho_file| {
862862 const atom_index = try macho_file.getOrCreateAtomForDecl(decl_index);
863863 const sym_index = macho_file.getAtom(atom_index).getSymbolIndex().?;
......@@ -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.symbol(local_sym_index).st_value });
895 return GenResult.mcv(.{ .memory = elf_file.symbol(local_sym_index).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+234-426
......@@ -1,4 +1,4 @@
1base: File,
1base: link.File,
22dwarf: ?Dwarf = null,
33
44ptr_width: PtrWidth,
......@@ -6,7 +6,7 @@ ptr_width: PtrWidth,
66/// If this is not null, an object file is created by LLVM and linked with LLD afterwards.
77llvm_object: ?*LlvmObject = null,
88
9files: std.MutliArrayList(File.Entry) = .{},
9files: std.MultiArrayList(File.Entry) = .{},
1010zig_module_index: ?File.Index = null,
1111linker_defined_index: ?File.Index = null,
1212
......@@ -56,14 +56,12 @@ shstrtab_section_index: ?u16 = null,
5656strtab_section_index: ?u16 = null,
5757
5858symbols: std.ArrayListUnmanaged(Symbol) = .{},
59globals: std.ArrayListUnmanaged(Symbol.Index) = .{},
60resolver: std.StringHashMapUnmanaged(u32) = .{},
59resolver: std.AutoArrayHashMapUnmanaged(u32, Symbol.Index) = .{},
6160unresolved: std.AutoArrayHashMapUnmanaged(u32, void) = .{},
6261
63symbols_free_list: std.ArrayListUnmanaged(u32) = .{},
64globals_free_list: std.ArrayListUnmanaged(u32) = .{},
62symbols_free_list: std.ArrayListUnmanaged(Symbol.Index) = .{},
6563
66got_table: TableSection(u32) = .{},
64got_table: TableSection(Symbol.Index) = .{},
6765
6866phdr_table_dirty: bool = false,
6967shdr_table_dirty: bool = false,
......@@ -88,9 +86,6 @@ decls: std.AutoHashMapUnmanaged(Module.Decl.Index, DeclMetadata) = .{},
8886/// List of atoms that are owned directly by the linker.
8987atoms: std.ArrayListUnmanaged(Atom) = .{},
9088
91/// Table of atoms indexed by the symbol index.
92atom_by_index_table: std.AutoHashMapUnmanaged(u32, Atom.Index) = .{},
93
9489/// Table of unnamed constants associated with a parent `Decl`.
9590/// We store them here so that we can free the constants whenever the `Decl`
9691/// needs updating or is freed.
......@@ -113,12 +108,10 @@ atom_by_index_table: std.AutoHashMapUnmanaged(u32, Atom.Index) = .{},
113108unnamed_const_atoms: UnnamedConstTable = .{},
114109
115110/// A table of relocations indexed by the owning them `TextBlock`.
116/// Note that once we refactor `TextBlock`'s lifetime and ownership rules,
117/// this will be a table indexed by index into the list of Atoms.
118111relocs: RelocTable = .{},
119112
120113const RelocTable = std.AutoHashMapUnmanaged(Atom.Index, std.ArrayListUnmanaged(Atom.Reloc));
121const UnnamedConstTable = std.AutoHashMapUnmanaged(Module.Decl.Index, std.ArrayListUnmanaged(Atom.Index));
114const UnnamedConstTable = std.AutoHashMapUnmanaged(Module.Decl.Index, std.ArrayListUnmanaged(Symbol.Index));
122115const LazySymbolTable = std.AutoArrayHashMapUnmanaged(Module.Decl.OptionalIndex, LazySymbolMetadata);
123116
124117/// When allocating, the ideal_capacity is calculated by
......@@ -143,13 +136,12 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option
143136 const self = try createEmpty(allocator, options);
144137 errdefer self.base.destroy();
145138
146 const file = try options.emit.?.directory.handle.createFile(sub_path, .{
139 self.base.file = try options.emit.?.directory.handle.createFile(sub_path, .{
147140 .truncate = false,
148141 .read = true,
149142 .mode = link.determineMode(options),
150143 });
151144
152 self.base.file = file;
153145 self.shdr_table_dirty = true;
154146
155147 // Index 0 is always a null symbol.
......@@ -243,19 +235,10 @@ pub fn deinit(self: *Elf) void {
243235 self.strtab.deinit(gpa);
244236 self.symbols.deinit(gpa);
245237 self.symbols_free_list.deinit(gpa);
246 self.globals.deinit(gpa);
247 self.globals_free_list.deinit(gpa);
248238 self.got_table.deinit(gpa);
239 self.resolver.deinit(gpa);
249240 self.unresolved.deinit(gpa);
250241
251 {
252 var it = self.resolver.keyIterator();
253 while (it.next()) |key_ptr| {
254 gpa.free(key_ptr.*);
255 }
256 self.resolver.deinit(gpa);
257 }
258
259242 {
260243 var it = self.decls.iterator();
261244 while (it.next()) |entry| {
......@@ -265,7 +248,6 @@ pub fn deinit(self: *Elf) void {
265248 }
266249
267250 self.atoms.deinit(gpa);
268 self.atom_by_index_table.deinit(gpa);
269251 self.lazy_syms.deinit(gpa);
270252
271253 {
......@@ -292,13 +274,12 @@ pub fn deinit(self: *Elf) void {
292274pub fn getDeclVAddr(self: *Elf, decl_index: Module.Decl.Index, reloc_info: link.File.RelocInfo) !u64 {
293275 assert(self.llvm_object == null);
294276
295 const this_atom_index = try self.getOrCreateAtomForDecl(decl_index);
296 const this_atom = self.atom(this_atom_index);
297 const target = this_atom.symbolIndex().?;
298 const vaddr = this_atom.symbol(self).st_value;
299 const atom_index = self.atomIndexForSymbol(reloc_info.parent_atom_index).?;
300 try Atom.addRelocation(self, atom_index, .{
301 .target = target,
277 const this_sym_index = try self.getOrCreateMetadataForDecl(decl_index);
278 const this_sym = self.symbol(this_sym_index);
279 const vaddr = this_sym.value;
280 const parent_atom_index = self.symbol(reloc_info.parent_atom_index).atom_index;
281 try Atom.addRelocation(self, parent_atom_index, .{
282 .target = this_sym,
302283 .offset = reloc_info.offset,
303284 .addend = reloc_info.addend,
304285 .prev_vaddr = vaddr,
......@@ -851,7 +832,7 @@ pub fn populateMissingMetadata(self: *Elf) !void {
851832 }
852833}
853834
854fn growAllocSection(self: *Elf, shdr_index: u16, needed_size: u64) !void {
835pub fn growAllocSection(self: *Elf, shdr_index: u16, needed_size: u64) !void {
855836 // TODO Also detect virtual address collisions.
856837 const shdr = &self.sections.items(.shdr)[shdr_index];
857838 const phdr_index = self.sections.items(.phdr_index)[shdr_index];
......@@ -863,8 +844,7 @@ fn growAllocSection(self: *Elf, shdr_index: u16, needed_size: u64) !void {
863844 const new_offset = self.findFreeSpace(needed_size, self.page_size);
864845 const existing_size = if (maybe_last_atom_index) |last_atom_index| blk: {
865846 const last = self.atom(last_atom_index);
866 const sym = last.symbol(self);
867 break :blk (sym.st_value + sym.st_size) - phdr.p_vaddr;
847 break :blk (last.value + last.size) - phdr.p_vaddr;
868848 } else if (shdr_index == self.got_section_index.?) blk: {
869849 break :blk shdr.sh_size;
870850 } else 0;
......@@ -985,6 +965,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
985965 // TODO This linker code currently assumes there is only 1 compilation unit and it
986966 // corresponds to the Zig source code.
987967 const module = self.base.options.module orelse return error.LinkingWithoutZigSourceUnimplemented;
968 _ = module;
988969
989970 self.zig_module_index = blk: {
990971 const index = @as(File.Index, @intCast(try self.files.addOne(gpa)));
......@@ -992,9 +973,9 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
992973 break :blk index;
993974 };
994975
995 self.linker_defined = blk: {
976 self.linker_defined_index = blk: {
996977 const index = @as(File.Index, @intCast(try self.files.addOne(gpa)));
997 self.files.set(index, .{ .linker_defined = .{} });
978 self.files.set(index, .{ .linker_defined = .{ .index = index } });
998979 break :blk index;
999980 };
1000981
......@@ -2157,193 +2138,12 @@ fn freeAtom(self: *Elf, atom_index: Atom.Index) void {
21572138 const sym_index = atom_ptr.symbolIndex().?;
21582139
21592140 log.debug("adding %{d} to local symbols free list", .{sym_index});
2160 self.locals_free_list.append(gpa, sym_index) catch {};
2161 self.locals.items[sym_index] = null_sym;
2162 _ = self.atom_by_index_table.remove(sym_index);
2141 self.symbols_free_list.append(gpa, sym_index) catch {};
2142 self.symbols.items[sym_index] = .{};
21632143 atom_ptr.sym_index = 0;
21642144 self.got_table.freeEntry(gpa, sym_index);
21652145}
21662146
2167fn shrinkAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64) void {
2168 _ = self;
2169 _ = atom_index;
2170 _ = new_block_size;
2171}
2172
2173fn growAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64, alignment: u64) !u64 {
2174 const atom_ptr = self.atom(atom_index);
2175 const sym = atom_ptr.symbol(self);
2176 const align_ok = mem.alignBackward(u64, sym.st_value, alignment) == sym.st_value;
2177 const need_realloc = !align_ok or new_block_size > atom_ptr.capacity(self);
2178 if (!need_realloc) return sym.st_value;
2179 return self.allocateAtom(atom_index, new_block_size, alignment);
2180}
2181
2182pub fn createAtom(self: *Elf) !Atom.Index {
2183 const gpa = self.base.allocator;
2184 const atom_index = @as(Atom.Index, @intCast(self.atoms.items.len));
2185 const atom_ptr = try self.atoms.addOne(gpa);
2186 const sym_index = try self.allocateSymbol();
2187 try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom_index);
2188 atom_ptr.* = .{ .sym_index = sym_index };
2189 log.debug("creating ATOM(%{d}) at index {d}", .{ sym_index, atom_index });
2190 return atom_index;
2191}
2192
2193fn allocateAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64, alignment: u64) !u64 {
2194 const sym = self.atom(atom_index).symbol(self);
2195 const phdr_index = self.sections.items(.phdr_index)[sym.st_shndx];
2196 const phdr = &self.program_headers.items[phdr_index];
2197 const shdr = &self.sections.items(.shdr)[sym.st_shndx];
2198 const free_list = &self.sections.items(.free_list)[sym.st_shndx];
2199 const maybe_last_atom_index = &self.sections.items(.last_atom_index)[sym.st_shndx];
2200 const new_atom_ideal_capacity = padToIdeal(new_block_size);
2201
2202 // We use these to indicate our intention to update metadata, placing the new atom,
2203 // and possibly removing a free list node.
2204 // It would be simpler to do it inside the for loop below, but that would cause a
2205 // problem if an error was returned later in the function. So this action
2206 // is actually carried out at the end of the function, when errors are no longer possible.
2207 var atom_placement: ?Atom.Index = null;
2208 var free_list_removal: ?usize = null;
2209
2210 // First we look for an appropriately sized free list node.
2211 // The list is unordered. We'll just take the first thing that works.
2212 const vaddr = blk: {
2213 var i: usize = if (self.base.child_pid == null) 0 else free_list.items.len;
2214 while (i < free_list.items.len) {
2215 const big_atom_index = free_list.items[i];
2216 const big_atom = self.atom(big_atom_index);
2217 // We now have a pointer to a live atom that has too much capacity.
2218 // Is it enough that we could fit this new atom?
2219 const big_atom_sym = big_atom.symbol(self);
2220 const capacity = big_atom.capacity(self);
2221 const ideal_capacity = padToIdeal(capacity);
2222 const ideal_capacity_end_vaddr = std.math.add(u64, big_atom_sym.st_value, ideal_capacity) catch ideal_capacity;
2223 const capacity_end_vaddr = big_atom_sym.st_value + capacity;
2224 const new_start_vaddr_unaligned = capacity_end_vaddr - new_atom_ideal_capacity;
2225 const new_start_vaddr = mem.alignBackward(u64, new_start_vaddr_unaligned, alignment);
2226 if (new_start_vaddr < ideal_capacity_end_vaddr) {
2227 // Additional bookkeeping here to notice if this free list node
2228 // should be deleted because the block that it points to has grown to take up
2229 // more of the extra capacity.
2230 if (!big_atom.freeListEligible(self)) {
2231 _ = free_list.swapRemove(i);
2232 } else {
2233 i += 1;
2234 }
2235 continue;
2236 }
2237 // At this point we know that we will place the new block here. But the
2238 // remaining question is whether there is still yet enough capacity left
2239 // over for there to still be a free list node.
2240 const remaining_capacity = new_start_vaddr - ideal_capacity_end_vaddr;
2241 const keep_free_list_node = remaining_capacity >= min_text_capacity;
2242
2243 // Set up the metadata to be updated, after errors are no longer possible.
2244 atom_placement = big_atom_index;
2245 if (!keep_free_list_node) {
2246 free_list_removal = i;
2247 }
2248 break :blk new_start_vaddr;
2249 } else if (maybe_last_atom_index.*) |last_index| {
2250 const last = self.atom(last_index);
2251 const last_sym = last.symbol(self);
2252 const ideal_capacity = padToIdeal(last_sym.st_size);
2253 const ideal_capacity_end_vaddr = last_sym.st_value + ideal_capacity;
2254 const new_start_vaddr = mem.alignForward(u64, ideal_capacity_end_vaddr, alignment);
2255 // Set up the metadata to be updated, after errors are no longer possible.
2256 atom_placement = last_index;
2257 break :blk new_start_vaddr;
2258 } else {
2259 break :blk phdr.p_vaddr;
2260 }
2261 };
2262
2263 const expand_section = if (atom_placement) |placement_index|
2264 self.atom(placement_index).next_index == null
2265 else
2266 true;
2267 if (expand_section) {
2268 const needed_size = (vaddr + new_block_size) - phdr.p_vaddr;
2269 try self.growAllocSection(sym.st_shndx, needed_size);
2270 maybe_last_atom_index.* = atom_index;
2271
2272 if (self.dwarf) |_| {
2273 // The .debug_info section has `low_pc` and `high_pc` values which is the virtual address
2274 // range of the compilation unit. When we expand the text section, this range changes,
2275 // so the DW_TAG.compile_unit tag of the .debug_info section becomes dirty.
2276 self.debug_info_header_dirty = true;
2277 // This becomes dirty for the same reason. We could potentially make this more
2278 // fine-grained with the addition of support for more compilation units. It is planned to
2279 // model each package as a different compilation unit.
2280 self.debug_aranges_section_dirty = true;
2281 }
2282 }
2283 shdr.sh_addralign = @max(shdr.sh_addralign, alignment);
2284
2285 // This function can also reallocate an atom.
2286 // In this case we need to "unplug" it from its previous location before
2287 // plugging it in to its new location.
2288 const atom_ptr = self.atom(atom_index);
2289 if (atom_ptr.prev_index) |prev_index| {
2290 const prev = self.atom(prev_index);
2291 prev.next_index = atom_ptr.next_index;
2292 }
2293 if (atom_ptr.next_index) |next_index| {
2294 const next = self.atom(next_index);
2295 next.prev_index = atom_ptr.prev_index;
2296 }
2297
2298 if (atom_placement) |big_atom_index| {
2299 const big_atom = self.atom(big_atom_index);
2300 atom_ptr.prev_index = big_atom_index;
2301 atom_ptr.next_index = big_atom.next_index;
2302 big_atom.next_index = atom_index;
2303 } else {
2304 atom_ptr.prev_index = null;
2305 atom_ptr.next_index = null;
2306 }
2307 if (free_list_removal) |i| {
2308 _ = free_list.swapRemove(i);
2309 }
2310 return vaddr;
2311}
2312
2313pub fn allocateSymbol(self: *Elf) !u32 {
2314 try self.locals.ensureUnusedCapacity(self.base.allocator, 1);
2315 const index = blk: {
2316 if (self.locals_free_list.popOrNull()) |index| {
2317 log.debug(" (reusing symbol index {d})", .{index});
2318 break :blk index;
2319 } else {
2320 log.debug(" (allocating symbol index {d})", .{self.locals.items.len});
2321 const index = @as(u32, @intCast(self.locals.items.len));
2322 _ = self.locals.addOneAssumeCapacity();
2323 break :blk index;
2324 }
2325 };
2326 self.locals.items[index] = null_sym;
2327 return index;
2328}
2329
2330fn allocateGlobal(self: *Elf) !u32 {
2331 try self.globals.ensureUnusedCapacity(self.base.allocator, 1);
2332 const index = blk: {
2333 if (self.globals_free_list.popOrNull()) |index| {
2334 log.debug(" (reusing global index {d})", .{index});
2335 break :blk index;
2336 } else {
2337 log.debug(" (allocating global index {d})", .{self.globals.items.len});
2338 const index = @as(u32, @intCast(self.globals.items.len));
2339 _ = self.globals.addOneAssumeCapacity();
2340 break :blk index;
2341 }
2342 };
2343 self.globals.items[index] = 0;
2344 return index;
2345}
2346
23472147fn freeUnnamedConsts(self: *Elf, decl_index: Module.Decl.Index) void {
23482148 const unnamed_consts = self.unnamed_const_atoms.getPtr(decl_index) orelse return;
23492149 for (unnamed_consts.items) |atom_index| {
......@@ -2372,40 +2172,50 @@ pub fn freeDecl(self: *Elf, decl_index: Module.Decl.Index) void {
23722172 }
23732173}
23742174
2375pub fn getOrCreateAtomForLazySymbol(self: *Elf, sym: link.File.LazySymbol) !Atom.Index {
2175pub fn getOrCreateMetadataForLazySymbol(self: *Elf, sym: link.File.LazySymbol) !Symbol.Index {
23762176 const mod = self.base.options.module.?;
23772177 const gop = try self.lazy_syms.getOrPut(self.base.allocator, sym.getDecl(mod));
23782178 errdefer _ = if (!gop.found_existing) self.lazy_syms.pop();
23792179 if (!gop.found_existing) gop.value_ptr.* = .{};
2380 const metadata: struct { atom: *Atom.Index, state: *LazySymbolMetadata.State } = switch (sym.kind) {
2381 .code => .{ .atom = &gop.value_ptr.text_atom, .state = &gop.value_ptr.text_state },
2382 .const_data => .{ .atom = &gop.value_ptr.rodata_atom, .state = &gop.value_ptr.rodata_state },
2180 const metadata: struct {
2181 symbol_index: *Symbol.Index,
2182 state: *LazySymbolMetadata.State,
2183 } = switch (sym.kind) {
2184 .code => .{
2185 .symbol_index = &gop.value_ptr.text_symbol_index,
2186 .state = &gop.value_ptr.text_state,
2187 },
2188 .const_data => .{
2189 .symbol_index = &gop.value_ptr.rodata_symbol_index,
2190 .state = &gop.value_ptr.rodata_state,
2191 },
23832192 };
2193 const zig_module = self.file(self.zig_module_index.?).?.zig_module;
23842194 switch (metadata.state.*) {
2385 .unused => metadata.atom.* = try self.createAtom(),
2195 .unused => metadata.symbol_index.* = try zig_module.createAtom(switch (sym.kind) {
2196 .code => self.text_section_index.?,
2197 .const_data => self.rodata_section_index.?,
2198 }, self),
23862199 .pending_flush => return metadata.atom.*,
23872200 .flushed => {},
23882201 }
23892202 metadata.state.* = .pending_flush;
2390 const atom_index = metadata.atom.*;
2203 const symbol_index = metadata.symbol_index.*;
23912204 // anyerror needs to be deferred until flushModule
2392 if (sym.getDecl(mod) != .none) try self.updateLazySymbolAtom(sym, atom_index, switch (sym.kind) {
2393 .code => self.text_section_index.?,
2394 .const_data => self.rodata_section_index.?,
2395 });
2396 return atom_index;
2205 if (sym.getDecl(mod) != .none) try self.updateLazySymbol(sym, symbol_index);
2206 return symbol_index;
23972207}
23982208
2399pub fn getOrCreateAtomForDecl(self: *Elf, decl_index: Module.Decl.Index) !Atom.Index {
2209pub fn getOrCreateMetadataForDecl(self: *Elf, decl_index: Module.Decl.Index) !Symbol.Index {
24002210 const gop = try self.decls.getOrPut(self.base.allocator, decl_index);
24012211 if (!gop.found_existing) {
2212 const zig_module = self.file(self.zig_module_index.?).?.zig_module;
24022213 gop.value_ptr.* = .{
2403 .atom = try self.createAtom(),
2404 .shdr = self.getDeclShdrIndex(decl_index),
2214 .symbol_index = try zig_module.createAtom(self.getDeclShdrIndex(decl_index), self),
24052215 .exports = .{},
24062216 };
24072217 }
2408 return gop.value_ptr.atom;
2218 return gop.value_ptr.symbol_index;
24092219}
24102220
24112221fn getDeclShdrIndex(self: *Elf, decl_index: Module.Decl.Index) u16 {
......@@ -2434,7 +2244,13 @@ fn getDeclShdrIndex(self: *Elf, decl_index: Module.Decl.Index) u16 {
24342244 return shdr_index;
24352245}
24362246
2437fn updateDeclCode(self: *Elf, decl_index: Module.Decl.Index, code: []const u8, stt_bits: u8) !*elf.Elf64_Sym {
2247fn updateDeclCode(
2248 self: *Elf,
2249 decl_index: Module.Decl.Index,
2250 sym_index: Symbol.Index,
2251 code: []const u8,
2252 stt_bits: u8,
2253) !void {
24382254 const gpa = self.base.allocator;
24392255 const mod = self.base.options.module.?;
24402256 const decl = mod.declPtr(decl_index);
......@@ -2444,60 +2260,52 @@ fn updateDeclCode(self: *Elf, decl_index: Module.Decl.Index, code: []const u8, s
24442260 log.debug("updateDeclCode {s}{*}", .{ decl_name, decl });
24452261 const required_alignment = decl.getAlignment(mod);
24462262
2447 const decl_metadata = self.decls.get(decl_index).?;
2448 const atom_index = decl_metadata.atom;
2449 const atom_ptr = self.atom(atom_index);
2450 const local_sym_index = atom_ptr.symbolIndex().?;
2451 const local_sym = atom_ptr.symbol(self);
2263 const sym = self.symbol(sym_index);
2264 const esym = sym.sourceSymbol(self);
2265 const atom_ptr = sym.atom(self).?;
2266 const shdr_index = sym.output_section_index;
24522267
2453 const shdr_index = decl_metadata.shdr;
2454 if (atom_ptr.symbol(self).st_size != 0 and self.base.child_pid == null) {
2455 local_sym.st_name = try self.strtab.insert(gpa, decl_name);
2456 local_sym.st_info = (elf.STB_LOCAL << 4) | stt_bits;
2457 local_sym.st_other = 0;
2458 local_sym.st_shndx = shdr_index;
2268 sym.name_offset = try self.strtab.insert(gpa, decl_name);
2269 esym.st_name = sym.name_offset;
2270 esym.st_info |= stt_bits;
2271 esym.st_size = code.len;
24592272
2460 const capacity = atom_ptr.capacity(self);
2461 const need_realloc = code.len > capacity or
2462 !mem.isAlignedGeneric(u64, local_sym.st_value, required_alignment);
2273 const old_size = atom_ptr.size;
2274 atom_ptr.alignment = math.log2_int(u64, required_alignment);
2275 atom_ptr.size = code.len;
24632276
2277 if (old_size > 0 and self.base.child_pid == null) {
2278 const capacity = atom_ptr.capacity(self);
2279 const need_realloc = code.len > capacity or !mem.isAlignedGeneric(u64, sym.value, required_alignment);
24642280 if (need_realloc) {
2465 const vaddr = try self.growAtom(atom_index, code.len, required_alignment);
2466 log.debug("growing {s} from 0x{x} to 0x{x}", .{ decl_name, local_sym.st_value, vaddr });
2467 if (vaddr != local_sym.st_value) {
2468 local_sym.st_value = vaddr;
2281 const vaddr = try atom_ptr.grow(self);
2282 log.debug("growing {s} from 0x{x} to 0x{x}", .{ decl_name, sym.value, vaddr });
2283 if (vaddr != sym.value) {
2284 sym.value = vaddr;
2285 esym.st_value = vaddr;
24692286
24702287 log.debug(" (writing new offset table entry)", .{});
2471 const got_entry_index = self.got_table.lookup.get(local_sym_index).?;
2472 self.got_table.entries.items[got_entry_index] = local_sym_index;
2288 const got_entry_index = self.got_table.lookup.get(sym_index).?;
2289 self.got_table.entries.items[got_entry_index] = sym_index;
24732290 try self.writeOffsetTableEntry(got_entry_index);
24742291 }
2475 } else if (code.len < local_sym.st_size) {
2476 self.shrinkAtom(atom_index, code.len);
2292 } else if (code.len < old_size) {
2293 atom_ptr.shrink(self);
24772294 }
2478 local_sym.st_size = code.len;
24792295 } else {
2480 local_sym.* = .{
2481 .st_name = try self.strtab.insert(gpa, decl_name),
2482 .st_info = (elf.STB_LOCAL << 4) | stt_bits,
2483 .st_other = 0,
2484 .st_shndx = shdr_index,
2485 .st_value = 0,
2486 .st_size = 0,
2487 };
2488 const vaddr = try self.allocateAtom(atom_index, code.len, required_alignment);
2489 errdefer self.freeAtom(atom_index);
2296 const vaddr = try atom_ptr.allocate(self);
2297 errdefer self.freeAtom(atom_ptr);
24902298 log.debug("allocated text block for {s} at 0x{x}", .{ decl_name, vaddr });
24912299
2492 local_sym.st_value = vaddr;
2493 local_sym.st_size = code.len;
2300 sym.value = vaddr;
2301 esym.st_value = vaddr;
24942302
2495 const got_entry_index = try atom_ptr.getOrCreateOffsetTableEntry(self);
2303 const got_entry_index = try sym.getOrCreateOffsetTableEntry(self);
24962304 try self.writeOffsetTableEntry(got_entry_index);
24972305 }
24982306
24992307 const phdr_index = self.sections.items(.phdr_index)[shdr_index];
2500 const section_offset = local_sym.st_value - self.program_headers.items[phdr_index].p_vaddr;
2308 const section_offset = sym.value - self.program_headers.items[phdr_index].p_vaddr;
25012309 const file_offset = self.sections.items(.shdr)[shdr_index].sh_offset + section_offset;
25022310
25032311 if (self.base.child_pid) |pid| {
......@@ -2508,7 +2316,7 @@ fn updateDeclCode(self: *Elf, decl_index: Module.Decl.Index, code: []const u8, s
25082316 .iov_len = code.len,
25092317 }};
25102318 var remote_vec: [1]std.os.iovec_const = .{.{
2511 .iov_base = @as([*]u8, @ptrFromInt(@as(usize, @intCast(local_sym.st_value)))),
2319 .iov_base = @as([*]u8, @ptrFromInt(@as(usize, @intCast(sym.value)))),
25122320 .iov_len = code.len,
25132321 }};
25142322 const rc = std.os.linux.process_vm_writev(pid, &code_vec, &remote_vec, 0);
......@@ -2522,8 +2330,6 @@ fn updateDeclCode(self: *Elf, decl_index: Module.Decl.Index, code: []const u8, s
25222330 }
25232331
25242332 try self.base.file.?.pwriteAll(code, file_offset);
2525
2526 return local_sym;
25272333}
25282334
25292335pub fn updateFunc(self: *Elf, mod: *Module, func_index: InternPool.Index, air: Air, liveness: Liveness) !void {
......@@ -2539,9 +2345,9 @@ pub fn updateFunc(self: *Elf, mod: *Module, func_index: InternPool.Index, air: A
25392345 const decl_index = func.owner_decl;
25402346 const decl = mod.declPtr(decl_index);
25412347
2542 const atom_index = try self.getOrCreateAtomForDecl(decl_index);
2348 const sym_index = try self.getOrCreateMetadataForDecl(decl_index);
25432349 self.freeUnnamedConsts(decl_index);
2544 Atom.freeRelocations(self, atom_index);
2350 Atom.freeRelocations(self, self.symbol(sym_index).atom_index);
25452351
25462352 var code_buffer = std.ArrayList(u8).init(self.base.allocator);
25472353 defer code_buffer.deinit();
......@@ -2564,13 +2370,14 @@ pub fn updateFunc(self: *Elf, mod: *Module, func_index: InternPool.Index, air: A
25642370 return;
25652371 },
25662372 };
2567 const local_sym = try self.updateDeclCode(decl_index, code, elf.STT_FUNC);
2373 try self.updateDeclCode(decl_index, sym_index, code, elf.STT_FUNC);
25682374 if (decl_state) |*ds| {
2375 const sym = self.symbol(sym_index);
25692376 try self.dwarf.?.commitDeclState(
25702377 mod,
25712378 decl_index,
2572 local_sym.st_value,
2573 local_sym.st_size,
2379 sym.value,
2380 sym.atom(self).?.size,
25742381 ds,
25752382 );
25762383 }
......@@ -2604,8 +2411,8 @@ pub fn updateDecl(
26042411 }
26052412 }
26062413
2607 const atom_index = try self.getOrCreateAtomForDecl(decl_index);
2608 Atom.freeRelocations(self, atom_index);
2414 const sym_index = try self.getOrCreateMetadataForDecl(decl_index);
2415 Atom.freeRelocations(self, self.symbol(sym_index).atom_index);
26092416
26102417 var code_buffer = std.ArrayList(u8).init(self.base.allocator);
26112418 defer code_buffer.deinit();
......@@ -2622,14 +2429,14 @@ pub fn updateDecl(
26222429 }, &code_buffer, .{
26232430 .dwarf = ds,
26242431 }, .{
2625 .parent_atom_index = self.atom(atom_index).symbolIndex().?,
2432 .parent_atom_index = sym_index,
26262433 })
26272434 else
26282435 try codegen.generateSymbol(&self.base, decl.srcLoc(mod), .{
26292436 .ty = decl.ty,
26302437 .val = decl_val,
26312438 }, &code_buffer, .none, .{
2632 .parent_atom_index = self.atom(atom_index).symbolIndex().?,
2439 .parent_atom_index = sym_index,
26332440 });
26342441
26352442 const code = switch (res) {
......@@ -2641,13 +2448,14 @@ pub fn updateDecl(
26412448 },
26422449 };
26432450
2644 const local_sym = try self.updateDeclCode(decl_index, code, elf.STT_OBJECT);
2451 try self.updateDeclCode(decl_index, sym_index, code, elf.STT_OBJECT);
26452452 if (decl_state) |*ds| {
2453 const sym = self.symbol(sym_index);
26462454 try self.dwarf.?.commitDeclState(
26472455 mod,
26482456 decl_index,
2649 local_sym.st_value,
2650 local_sym.st_size,
2457 sym.value,
2458 sym.atom(self).?.size,
26512459 ds,
26522460 );
26532461 }
......@@ -2657,12 +2465,7 @@ pub fn updateDecl(
26572465 return self.updateDeclExports(mod, decl_index, mod.getDeclExports(decl_index));
26582466}
26592467
2660fn updateLazySymbolAtom(
2661 self: *Elf,
2662 sym: link.File.LazySymbol,
2663 atom_index: Atom.Index,
2664 shdr_index: u16,
2665) !void {
2468fn updateLazySymbol(self: *Elf, sym: link.File.LazySymbol, symbol_index: Symbol.Index) !void {
26662469 const gpa = self.base.allocator;
26672470 const mod = self.base.options.module.?;
26682471
......@@ -2680,9 +2483,6 @@ fn updateLazySymbolAtom(
26802483 };
26812484 const name = self.strtab.get(name_str_index).?;
26822485
2683 const atom_ptr = self.atom(atom_index);
2684 const local_sym_index = atom_ptr.symbolIndex().?;
2685
26862486 const src = if (sym.ty.getOwnerDeclOrNull(mod)) |owner_decl|
26872487 mod.declPtr(owner_decl).srcLoc(mod)
26882488 else
......@@ -2698,7 +2498,7 @@ fn updateLazySymbolAtom(
26982498 &required_alignment,
26992499 &code_buffer,
27002500 .none,
2701 .{ .parent_atom_index = local_sym_index },
2501 .{ .parent_atom_index = symbol_index },
27022502 );
27032503 const code = switch (res) {
27042504 .ok => code_buffer.items,
......@@ -2708,28 +2508,28 @@ fn updateLazySymbolAtom(
27082508 },
27092509 };
27102510
2711 const phdr_index = self.sections.items(.phdr_index)[shdr_index];
2712 const local_sym = atom_ptr.symbol(self);
2713 local_sym.* = .{
2714 .st_name = name_str_index,
2715 .st_info = (elf.STB_LOCAL << 4) | elf.STT_OBJECT,
2716 .st_other = 0,
2717 .st_shndx = shdr_index,
2718 .st_value = 0,
2719 .st_size = 0,
2720 };
2721 const vaddr = try self.allocateAtom(atom_index, code.len, required_alignment);
2722 errdefer self.freeAtom(atom_index);
2511 const local_sym = self.symbol(symbol_index);
2512 const phdr_index = self.sections.items(.phdr_index)[local_sym.output_section_index];
2513 local_sym.name_offset = name_str_index;
2514 const local_esym = local_sym.sourceSymbol(self);
2515 local_esym.st_name = name_str_index;
2516 local_esym.st_info |= elf.STT_OBJECT;
2517 local_esym.st_size = code.len;
2518 const atom_ptr = local_sym.atom(self).?;
2519 atom_ptr.alignment = math.log2_int(u64, required_alignment);
2520 atom_ptr.size = code.len;
2521 const vaddr = try atom_ptr.allocate(self);
2522 errdefer self.freeAtom(atom_ptr);
27232523 log.debug("allocated text block for {s} at 0x{x}", .{ name, vaddr });
27242524
2725 local_sym.st_value = vaddr;
2726 local_sym.st_size = code.len;
2525 local_sym.value = vaddr;
2526 local_esym.st_value = vaddr;
27272527
2728 const got_entry_index = try atom_ptr.getOrCreateOffsetTableEntry(self);
2528 const got_entry_index = try local_sym.getOrCreateOffsetTableEntry(self);
27292529 try self.writeOffsetTableEntry(got_entry_index);
27302530
27312531 const section_offset = vaddr - self.program_headers.items[phdr_index].p_vaddr;
2732 const file_offset = self.sections.items(.shdr)[shdr_index].sh_offset + section_offset;
2532 const file_offset = self.sections.items(.shdr)[local_sym.output_section_index].sh_offset + section_offset;
27332533 try self.base.file.?.pwriteAll(code, file_offset);
27342534}
27352535
......@@ -2756,12 +2556,13 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module
27562556 };
27572557 const name = self.strtab.get(name_str_index).?;
27582558
2759 const atom_index = try self.createAtom();
2559 const zig_module = self.file(self.zig_module_index.?).?.zig_module;
2560 const sym_index = try zig_module.createAtom(self.rodata_section_index.?, self);
27602561
27612562 const res = try codegen.generateSymbol(&self.base, decl.srcLoc(mod), typed_value, &code_buffer, .{
27622563 .none = {},
27632564 }, .{
2764 .parent_atom_index = self.atom(atom_index).symbolIndex().?,
2565 .parent_atom_index = sym_index,
27652566 });
27662567 const code = switch (res) {
27672568 .ok => code_buffer.items,
......@@ -2776,24 +2577,30 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module
27762577 const required_alignment = typed_value.ty.abiAlignment(mod);
27772578 const shdr_index = self.rodata_section_index.?;
27782579 const phdr_index = self.sections.items(.phdr_index)[shdr_index];
2779 const local_sym = self.atom(atom_index).symbol(self);
2780 local_sym.st_name = name_str_index;
2781 local_sym.st_info = (elf.STB_LOCAL << 4) | elf.STT_OBJECT;
2782 local_sym.st_other = 0;
2783 local_sym.st_shndx = shdr_index;
2784 local_sym.st_size = code.len;
2785 local_sym.st_value = try self.allocateAtom(atom_index, code.len, required_alignment);
2786 errdefer self.freeAtom(atom_index);
2580 const local_sym = self.symbol(sym_index);
2581 local_sym.name_offset = name_str_index;
2582 const local_esym = local_sym.sourceSymbol(self);
2583 local_esym.st_name = name_str_index;
2584 local_esym.st_info |= elf.STT_OBJECT;
2585 local_esym.st_size = code.len;
2586 const atom_ptr = local_sym.atom(self).?;
2587 atom_ptr.alignment = math.log2_int(u64, required_alignment);
2588 atom_ptr.size = code.len;
2589 const vaddr = try atom_ptr.allocateAtom(self);
2590 errdefer self.freeAtom(atom_ptr);
27872591
27882592 log.debug("allocated text block for {s} at 0x{x}", .{ name, local_sym.st_value });
27892593
2790 try unnamed_consts.append(gpa, atom_index);
2594 local_sym.value = vaddr;
2595 local_esym.st_value = vaddr;
27912596
2792 const section_offset = local_sym.st_value - self.program_headers.items[phdr_index].p_vaddr;
2597 try unnamed_consts.append(gpa, atom_ptr.atom_index);
2598
2599 const section_offset = local_sym.value - self.program_headers.items[phdr_index].p_vaddr;
27932600 const file_offset = self.sections.items(.shdr)[shdr_index].sh_offset + section_offset;
27942601 try self.base.file.?.pwriteAll(code, file_offset);
27952602
2796 return self.atom(atom_index).symbolIndex().?;
2603 return sym_index;
27972604}
27982605
27992606pub fn updateDeclExports(
......@@ -2815,11 +2622,10 @@ pub fn updateDeclExports(
28152622 const gpa = self.base.allocator;
28162623
28172624 const decl = mod.declPtr(decl_index);
2818 const atom_index = try self.getOrCreateAtomForDecl(decl_index);
2819 const atom_ptr = self.atom(atom_index);
2820 const decl_sym = atom_ptr.symbol(self);
2625 const decl_sym_index = try self.getOrCreateMetadataForDecl(decl_index);
2626 const decl_sym = self.symbol(decl_sym_index);
2627 const decl_esym = symbol.sourceSymbol(self);
28212628 const decl_metadata = self.decls.getPtr(decl_index).?;
2822 const shdr_index = decl_metadata.shdr;
28232629
28242630 for (exports) |exp| {
28252631 const exp_name = mod.intern_pool.stringToSlice(exp.opts.name);
......@@ -2838,7 +2644,7 @@ pub fn updateDeclExports(
28382644 .Strong => blk: {
28392645 const entry_name = self.base.options.entry orelse "_start";
28402646 if (mem.eql(u8, exp_name, entry_name)) {
2841 self.entry_addr = decl_sym.st_value;
2647 self.entry_addr = decl_sym.value;
28422648 }
28432649 break :blk elf.STB_GLOBAL;
28442650 },
......@@ -2855,22 +2661,18 @@ pub fn updateDeclExports(
28552661 const stt_bits: u8 = @as(u4, @truncate(decl_sym.st_info));
28562662
28572663 const sym_index = if (decl_metadata.@"export"(self, exp_name)) |exp_index| exp_index.* else blk: {
2858 const sym_index = try self.allocateSymbol();
2664 const zig_module = self.file(self.zig_module_index.?).?.zig_module;
2665 const sym_index = try zig_module.addGlobal(exp_name, self);
28592666 try decl_metadata.exports.append(gpa, sym_index);
28602667 break :blk sym_index;
28612668 };
28622669 const sym = self.symbol(sym_index);
2863 sym.* = .{
2864 .st_name = try self.strtab.insert(gpa, exp_name),
2865 .st_info = (stb_bits << 4) | stt_bits,
2866 .st_other = 0,
2867 .st_shndx = shdr_index,
2868 .st_value = decl_sym.st_value,
2869 .st_size = decl_sym.st_size,
2870 };
2871 const sym_name = self.symbolName(sym_index);
2872 const gop = try self.getOrPutGlobal(sym_name);
2873 gop.value_ptr.* = sym_index;
2670 sym.value = decl_sym.value;
2671 sym.atom_index = decl_sym.atom_index;
2672 sym.output_section_index = decl_sym.output_section_index;
2673 const esym = sym.sourceSymbol(self);
2674 esym.* = decl_esym.*;
2675 esym.st_info = (stb_bits << 4) | stt_bits;
28742676 }
28752677}
28762678
......@@ -2901,16 +2703,21 @@ pub fn deleteDeclExport(
29012703 const mod = self.base.options.module.?;
29022704 const exp_name = mod.intern_pool.stringToSlice(name);
29032705 const sym_index = metadata.@"export"(self, exp_name) orelse return;
2904 const sym = self.symbol(sym_index.*);
29052706 log.debug("deleting export '{s}'", .{exp_name});
2906 sym.* = null_sym;
2907 self.locals_free_list.append(gpa, sym_index.*) catch {};
2908
2909 if (self.resolver.fetchRemove(exp_name)) |entry| {
2910 self.globals_free_list.append(gpa, entry.value) catch {};
2911 self.globals.items[entry.value] = 0;
2912 }
2913
2707 const sym = self.symbol(sym_index.*);
2708 const esym = sym.sourceSymbol(self);
2709 assert(self.resolver.fetchSwapRemove(sym.name_offset) != null); // TODO don't delete it if it's not dominant
2710 sym.* = .{};
2711 // TODO free list for esym!
2712 esym.* = .{
2713 .st_name = 0,
2714 .st_info = 0,
2715 .st_other = 0,
2716 .st_shndx = 0,
2717 .st_value = 0,
2718 .st_size = 0,
2719 };
2720 self.symbols_free_list.append(gpa, sym_index.*) catch {};
29142721 sym_index.* = 0;
29152722}
29162723
......@@ -2971,7 +2778,7 @@ fn writeOffsetTableEntry(self: *Elf, index: @TypeOf(self.got_table).Index) !void
29712778 const phdr = &self.program_headers.items[self.phdr_got_index.?];
29722779 const vaddr = phdr.p_vaddr + @as(u64, entry_size) * index;
29732780 const got_entry = self.got_table.entries.items[index];
2974 const got_value = self.symbol(got_entry).st_value;
2781 const got_value = self.symbol(got_entry).value;
29752782 switch (entry_size) {
29762783 2 => {
29772784 var buf: [2]u8 = undefined;
......@@ -3374,98 +3181,99 @@ const CsuObjects = struct {
33743181 }
33753182};
33763183
3377fn logSymtab(self: Elf) void {
3378 log.debug("locals:", .{});
3379 for (self.locals.items, 0..) |sym, id| {
3380 log.debug(" {d}: {?s}: @{x} in {d}", .{ id, self.strtab.get(sym.st_name), sym.st_value, sym.st_shndx });
3381 }
3382 log.debug("globals:", .{});
3383 for (self.globals.items, 0..) |glob, id| {
3384 const sym = self.symbol(glob);
3385 log.debug(" {d}: {?s}: @{x} in {d}", .{ id, self.strtab.get(sym.st_name), sym.st_value, sym.st_shndx });
3386 }
3387}
3388
3389/// Returns pointer-to-symbol described at sym_index.
3390pub fn symbol(self: *const Elf, sym_index: u32) *elf.Elf64_Sym {
3391 return &self.locals.items[sym_index];
3184pub fn atom(self: *Elf, atom_index: Atom.Index) *Atom {
3185 assert(atom_index < self.atoms.items.len);
3186 return &self.atoms.items[atom_index];
33923187}
33933188
3394/// Returns name of the symbol at sym_index.
3395pub fn symbolName(self: *const Elf, sym_index: u32) []const u8 {
3396 const sym = self.locals.items[sym_index];
3397 return self.strtab.get(sym.st_name).?;
3189pub fn addAtom(self: *Elf) !Atom.Index {
3190 const index = @as(Atom.Index, @intCast(self.atoms.items.len));
3191 const atom_ptr = try self.atoms.addOne(self.base.allocator);
3192 atom_ptr.* = .{ .atom_index = index };
3193 return index;
33983194}
33993195
3400/// Returns pointer to the global entry for `name` if one exists.
3401pub fn global(self: *const Elf, name: []const u8) ?*u32 {
3402 const global_index = self.resolver.get(name) orelse return null;
3403 return &self.globals.items[global_index];
3196pub fn file(self: *Elf, index: File.Index) ?File {
3197 const tag = self.files.items(.tags)[index];
3198 return switch (tag) {
3199 .null => null,
3200 .linker_defined => .{ .linker_defined = &self.files.items(.data)[index].linker_defined },
3201 .zig_module => .{ .zig_module = &self.files.items(.data)[index].zig_module },
3202 };
34043203}
34053204
3406/// Returns the index of the global entry for `name` if one exists.
3407pub fn globalIndex(self: *const Elf, name: []const u8) ?u32 {
3408 return self.resolver.get(name);
3205/// Returns pointer-to-symbol described at sym_index.
3206pub fn symbol(self: *Elf, sym_index: Symbol.Index) *Symbol {
3207 return &self.symbols.items[sym_index];
34093208}
34103209
3411/// Returns global entry at `index`.
3412pub fn globalByIndex(self: *const Elf, index: u32) u32 {
3413 assert(index < self.globals.items.len);
3414 return self.globals.items[index];
3210pub fn addSymbol(self: *Elf) !Symbol.Index {
3211 try self.symbols.ensureUnusedCapacity(self.base.allocator, 1);
3212 const index = blk: {
3213 if (self.symbols_free_list.popOrNull()) |index| {
3214 log.debug(" (reusing symbol index {d})", .{index});
3215 break :blk index;
3216 } else {
3217 log.debug(" (allocating symbol index {d})", .{self.symbols.items.len});
3218 const index = @as(Symbol.Index, @intCast(self.symbols.items.len));
3219 _ = self.symbols.addOneAssumeCapacity();
3220 break :blk index;
3221 }
3222 };
3223 self.symbols.items[index] = .{ .symbol_index = index };
3224 return index;
34153225}
34163226
34173227const GetOrPutGlobalResult = struct {
34183228 found_existing: bool,
3419 value_ptr: *u32,
3229 index: Symbol.Index,
34203230};
34213231
3422/// Return pointer to the global entry for `name` if one exists.
3423/// Puts a new global entry for `name` if one doesn't exist, and
3424/// returns a pointer to it.
3425pub fn getOrPutGlobal(self: *Elf, name: []const u8) !GetOrPutGlobalResult {
3426 if (self.global(name)) |ptr| {
3427 return GetOrPutGlobalResult{ .found_existing = true, .value_ptr = ptr };
3428 }
3232pub fn getOrPutGlobal(self: *Elf, name_off: u32) !GetOrPutGlobalResult {
34293233 const gpa = self.base.allocator;
3430 const global_index = try self.allocateGlobal();
3431 const global_name = try gpa.dupe(u8, name);
3432 _ = try self.resolver.put(gpa, global_name, global_index);
3433 const ptr = &self.globals.items[global_index];
3434 return GetOrPutGlobalResult{ .found_existing = false, .value_ptr = ptr };
3435}
3436
3437pub fn atom(self: *Elf, atom_index: Atom.Index) *Atom {
3438 assert(atom_index < self.atoms.items.len);
3439 return &self.atoms.items[atom_index];
3234 const gop = try self.resolver.getOrPut(gpa, name_off);
3235 if (!gop.found_existing) {
3236 const index = try self.addSymbol();
3237 const global = self.symbol(index);
3238 global.name_offset = name_off;
3239 gop.value_ptr.* = index;
3240 }
3241 return .{
3242 .found_existing = gop.found_existing,
3243 .index = gop.value_ptr.*,
3244 };
34403245}
34413246
3442/// Returns atom if there is an atom referenced by the symbol.
3443/// Returns null on failure.
3444pub fn atomIndexForSymbol(self: *Elf, sym_index: u32) ?Atom.Index {
3445 return self.atom_by_index_table.get(sym_index);
3247pub fn getGlobalByName(self: *Elf, name: []const u8) ?Symbol.Index {
3248 const name_off = self.strtab.getOffset(name) orelse return null;
3249 return self.resolver.get(name_off);
34463250}
34473251
3448fn dumpState(self: *Elf ) std.fmt.Formatter(fmtDumpState) {
3252fn dumpState(self: *Elf) std.fmt.Formatter(fmtDumpState) {
34493253 return .{ .data = self };
34503254}
34513255
3452fn fmtDumpState(self: *Elf,
3256fn fmtDumpState(
3257 self: *Elf,
34533258 comptime unused_fmt_string: []const u8,
34543259 options: std.fmt.FormatOptions,
34553260 writer: anytype,
34563261) !void {
3262 _ = unused_fmt_string;
3263 _ = options;
34573264
3265 if (self.zig_module_index) |index| {
3266 const zig_module = self.file(index).?.zig_module;
3267 try writer.print("zig_module({d}) : (zig module)\n", .{index});
3268 try writer.print("{}\n", .{zig_module.fmtSymtab(self)});
3269 }
3270 if (self.linker_defined_index) |index| {
3271 const linker_defined = self.file(index).?.linker_defined;
3272 try writer.print("linker_defined({d}) : (linker defined)\n", .{index});
3273 try writer.print("{}\n", .{linker_defined.fmtSymtab(self)});
3274 }
34583275}
34593276
3460pub const null_sym = elf.Elf64_Sym{
3461 .st_name = 0,
3462 .st_info = 0,
3463 .st_other = 0,
3464 .st_shndx = 0,
3465 .st_value = 0,
3466 .st_size = 0,
3467};
3468
34693277const default_entry_addr = 0x8000000;
34703278
34713279pub const base_tag: link.File.Tag = .elf;
......@@ -3497,21 +3305,20 @@ const Section = struct {
34973305
34983306const LazySymbolMetadata = struct {
34993307 const State = enum { unused, pending_flush, flushed };
3500 text_atom: Atom.Index = undefined,
3501 rodata_atom: Atom.Index = undefined,
3308 text_symbol_index: Symbol.Index = undefined,
3309 rodata_symbol_index: Symbol.Index = undefined,
35023310 text_state: State = .unused,
35033311 rodata_state: State = .unused,
35043312};
35053313
35063314const DeclMetadata = struct {
3507 atom: Atom.Index,
3508 shdr: u16,
3315 symbol_index: Symbol.Index,
35093316 /// A list of all exports aliases of this Decl.
3510 exports: std.ArrayListUnmanaged(u32) = .{},
3317 exports: std.ArrayListUnmanaged(Symbol.Index) = .{},
35113318
3512 fn @"export"(m: DeclMetadata, elf_file: *const Elf, name: []const u8) ?*u32 {
3319 fn @"export"(m: DeclMetadata, elf_file: *Elf, name: []const u8) ?*u32 {
35133320 for (m.exports.items) |*exp| {
3514 if (mem.eql(u8, name, elf_file.symbolName(exp.*))) return exp;
3321 if (mem.eql(u8, name, elf_file.symbol(exp.*).name(elf_file))) return exp;
35153322 }
35163323 return null;
35173324 }
......@@ -3543,13 +3350,14 @@ pub const Atom = @import("Elf/Atom.zig");
35433350const Cache = std.Build.Cache;
35443351const Compilation = @import("../Compilation.zig");
35453352const Dwarf = @import("Dwarf.zig");
3546const File = @import("Elf/File.zig");
3353const File = @import("Elf/file.zig").File;
35473354const LinkerDefined = @import("Elf/LinkerDefined.zig");
35483355const Liveness = @import("../Liveness.zig");
35493356const LlvmObject = @import("../codegen/llvm.zig").Object;
35503357const Module = @import("../Module.zig");
35513358const InternPool = @import("../InternPool.zig");
35523359const Package = @import("../Package.zig");
3360const Symbol = @import("Elf/Symbol.zig");
35533361const StringTable = @import("strtab.zig").StringTable;
35543362const TableSection = @import("table_section.zig").TableSection;
35553363const Type = @import("../type.zig").Type;
src/link/Elf/Atom.zig+184-61
......@@ -1,81 +1,65 @@
1/// Each decl always gets a local symbol with the fully qualified name.
2/// The vaddr and size are found here directly.
3/// The file offset is found by computing the vaddr offset from the section vaddr
4/// the symbol references, and adding that to the file offset of the section.
5/// If this field is 0, it means the codegen size = 0 and there is no symbol or
6/// offset table entry.
7sym_index: u32 = 0,
1/// Address allocated for this Atom.
2value: u64 = 0,
83
9/// Points to the previous and next neighbors, based on the `text_offset`.
10/// This can be used to find, for example, the capacity of this `TextBlock`.
11prev_index: ?Index = null,
12next_index: ?Index = null,
4/// Name of this Atom.
5name_offset: u32 = 0,
136
14pub const Index = u32;
7/// Index into linker's input file table.
8file_index: File.Index = 0,
159
16pub const Reloc = struct {
17 target: u32,
18 offset: u64,
19 addend: u32,
20 prev_vaddr: u64,
21};
10/// Size of this atom
11size: u64 = 0,
2212
23pub fn symbolIndex(self: *const Atom) ?u32 {
24 if (self.sym_index == 0) return null;
25 return self.sym_index;
26}
13/// Alignment of this atom as a power of two.
14alignment: u8 = 0,
2715
28pub fn symbol(self: *const Atom, elf_file: *Elf) *elf.Elf64_Sym {
29 return elf_file.symbol(self.symbolIndex().?);
30}
16/// Index of the input section.
17input_section_index: u16 = 0,
3118
32pub fn name(self: *const Atom, elf_file: *Elf) []const u8 {
33 return elf_file.symbolName(self.symbolIndex().?);
34}
19/// Index of the output section.
20output_section_index: u16 = 0,
3521
36/// If entry already exists, returns index to it.
37/// Otherwise, creates a new entry in the Global Offset Table for this Atom.
38pub fn getOrCreateOffsetTableEntry(self: *const Atom, elf_file: *Elf) !u32 {
39 const sym_index = self.symbolIndex().?;
40 if (elf_file.got_table.lookup.get(sym_index)) |index| return index;
41 const index = try elf_file.got_table.allocateEntry(elf_file.base.allocator, sym_index);
42 elf_file.got_table_count_dirty = true;
43 return index;
44}
22/// Index of the input section containing this atom's relocs.
23relocs_section_index: u16 = 0,
4524
46pub fn getOffsetTableAddress(self: *const Atom, elf_file: *Elf) u64 {
47 const sym_index = self.symbolIndex().?;
48 const got_entry_index = elf_file.got_table.lookup.get(sym_index).?;
49 const target = elf_file.base.options.target;
50 const ptr_bits = target.ptrBitWidth();
51 const ptr_bytes: u64 = @divExact(ptr_bits, 8);
52 const got = elf_file.program_headers.items[elf_file.phdr_got_index.?];
53 return got.p_vaddr + got_entry_index * ptr_bytes;
25/// Index of this atom in the linker's atoms table.
26atom_index: Index = 0,
27
28/// Specifies whether this atom is alive or has been garbage collected.
29alive: bool = true,
30
31/// Specifies if the atom has been visited during garbage collection.
32visited: bool = false,
33
34/// Start index of FDEs referencing this atom.
35fde_start: u32 = 0,
36
37/// End index of FDEs referencing this atom.
38fde_end: u32 = 0,
39
40/// Points to the previous and next neighbors, based on the `text_offset`.
41/// This can be used to find, for example, the capacity of this `TextBlock`.
42prev_index: ?Index = null,
43next_index: ?Index = null,
44
45pub fn name(self: Atom, elf_file: *Elf) []const u8 {
46 return elf_file.strtab.getAssumeExists(self.name_offset);
5447}
5548
5649/// Returns how much room there is to grow in virtual address space.
5750/// File offset relocation happens transparently, so it is not included in
5851/// this calculation.
59pub fn capacity(self: *const Atom, elf_file: *Elf) u64 {
60 const self_sym = self.symbol(elf_file);
61 if (self.next_index) |next_index| {
62 const next = elf_file.atom(next_index);
63 const next_sym = next.symbol(elf_file);
64 return next_sym.st_value - self_sym.st_value;
65 } else {
66 // We are the last block. The capacity is limited only by virtual address space.
67 return std.math.maxInt(u32) - self_sym.st_value;
68 }
52pub fn capacity(self: Atom, elf_file: *Elf) u64 {
53 const next_value = if (self.next_index) |next_index| elf_file.atom(next_index).value else std.math.maxInt(u32);
54 return next_value - self.value;
6955}
7056
71pub fn freeListEligible(self: *const Atom, elf_file: *Elf) bool {
57pub fn freeListEligible(self: Atom, elf_file: *Elf) bool {
7258 // No need to keep a free list node for the last block.
7359 const next_index = self.next_index orelse return false;
7460 const next = elf_file.atom(next_index);
75 const self_sym = self.symbol(elf_file);
76 const next_sym = next.symbol(elf_file);
77 const cap = next_sym.st_value - self_sym.st_value;
78 const ideal_cap = Elf.padToIdeal(self_sym.st_size);
61 const cap = next.value - self.value;
62 const ideal_cap = Elf.padToIdeal(self.size);
7963 if (cap <= ideal_cap) return false;
8064 const surplus = cap - ideal_cap;
8165 return surplus >= Elf.min_text_capacity;
......@@ -95,10 +79,149 @@ pub fn freeRelocations(elf_file: *Elf, atom_index: Index) void {
9579 if (removed_relocs) |*relocs| relocs.value.deinit(elf_file.base.allocator);
9680}
9781
98const Atom = @This();
82pub fn allocate(self: *Atom, elf_file: *Elf) !u64 {
83 const phdr_index = elf_file.sections.items(.phdr_index)[self.output_section_index];
84 const phdr = &elf_file.program_headers.items[phdr_index];
85 const shdr = &elf_file.sections.items(.shdr)[self.output_section_index];
86 const free_list = &elf_file.sections.items(.free_list)[self.output_section_index];
87 const maybe_last_atom_index = &elf_file.sections.items(.last_atom_index)[self.output_section_index];
88 const new_atom_ideal_capacity = Elf.padToIdeal(self.size);
89 const alignment = try std.math.powi(u64, 2, self.alignment);
90
91 // We use these to indicate our intention to update metadata, placing the new atom,
92 // and possibly removing a free list node.
93 // It would be simpler to do it inside the for loop below, but that would cause a
94 // problem if an error was returned later in the function. So this action
95 // is actually carried out at the end of the function, when errors are no longer possible.
96 var atom_placement: ?Atom.Index = null;
97 var free_list_removal: ?usize = null;
98
99 // First we look for an appropriately sized free list node.
100 // The list is unordered. We'll just take the first thing that works.
101 const vaddr = blk: {
102 var i: usize = if (elf_file.base.child_pid == null) 0 else free_list.items.len;
103 while (i < free_list.items.len) {
104 const big_atom_index = free_list.items[i];
105 const big_atom = elf_file.atom(big_atom_index);
106 // We now have a pointer to a live atom that has too much capacity.
107 // Is it enough that we could fit this new atom?
108 const cap = big_atom.capacity(elf_file);
109 const ideal_capacity = Elf.padToIdeal(cap);
110 const ideal_capacity_end_vaddr = std.math.add(u64, big_atom.value, ideal_capacity) catch ideal_capacity;
111 const capacity_end_vaddr = big_atom.value + cap;
112 const new_start_vaddr_unaligned = capacity_end_vaddr - new_atom_ideal_capacity;
113 const new_start_vaddr = std.mem.alignBackward(u64, new_start_vaddr_unaligned, alignment);
114 if (new_start_vaddr < ideal_capacity_end_vaddr) {
115 // Additional bookkeeping here to notice if this free list node
116 // should be deleted because the block that it points to has grown to take up
117 // more of the extra capacity.
118 if (!big_atom.freeListEligible(elf_file)) {
119 _ = free_list.swapRemove(i);
120 } else {
121 i += 1;
122 }
123 continue;
124 }
125 // At this point we know that we will place the new block here. But the
126 // remaining question is whether there is still yet enough capacity left
127 // over for there to still be a free list node.
128 const remaining_capacity = new_start_vaddr - ideal_capacity_end_vaddr;
129 const keep_free_list_node = remaining_capacity >= Elf.min_text_capacity;
130
131 // Set up the metadata to be updated, after errors are no longer possible.
132 atom_placement = big_atom_index;
133 if (!keep_free_list_node) {
134 free_list_removal = i;
135 }
136 break :blk new_start_vaddr;
137 } else if (maybe_last_atom_index.*) |last_index| {
138 const last = elf_file.atom(last_index);
139 const ideal_capacity = Elf.padToIdeal(last.size);
140 const ideal_capacity_end_vaddr = last.value + ideal_capacity;
141 const new_start_vaddr = std.mem.alignForward(u64, ideal_capacity_end_vaddr, alignment);
142 // Set up the metadata to be updated, after errors are no longer possible.
143 atom_placement = last_index;
144 break :blk new_start_vaddr;
145 } else {
146 break :blk phdr.p_vaddr;
147 }
148 };
149
150 const expand_section = if (atom_placement) |placement_index|
151 elf_file.atom(placement_index).next_index == null
152 else
153 true;
154 if (expand_section) {
155 const needed_size = (vaddr + self.size) - phdr.p_vaddr;
156 try elf_file.growAllocSection(self.output_section_index, needed_size);
157 maybe_last_atom_index.* = self.atom_index;
158
159 if (elf_file.dwarf) |_| {
160 // The .debug_info section has `low_pc` and `high_pc` values which is the virtual address
161 // range of the compilation unit. When we expand the text section, this range changes,
162 // so the DW_TAG.compile_unit tag of the .debug_info section becomes dirty.
163 elf_file.debug_info_header_dirty = true;
164 // This becomes dirty for the same reason. We could potentially make this more
165 // fine-grained with the addition of support for more compilation units. It is planned to
166 // model each package as a different compilation unit.
167 elf_file.debug_aranges_section_dirty = true;
168 }
169 }
170 shdr.sh_addralign = @max(shdr.sh_addralign, alignment);
171
172 // This function can also reallocate an atom.
173 // In this case we need to "unplug" it from its previous location before
174 // plugging it in to its new location.
175 if (self.prev_index) |prev_index| {
176 const prev = elf_file.atom(prev_index);
177 prev.next_index = self.next_index;
178 }
179 if (self.next_index) |next_index| {
180 const next = elf_file.atom(next_index);
181 next.prev_index = self.prev_index;
182 }
183
184 if (atom_placement) |big_atom_index| {
185 const big_atom = elf_file.atom(big_atom_index);
186 self.prev_index = big_atom_index;
187 self.next_index = big_atom.next_index;
188 big_atom.next_index = self.atom_index;
189 } else {
190 self.prev_index = null;
191 self.next_index = null;
192 }
193 if (free_list_removal) |i| {
194 _ = free_list.swapRemove(i);
195 }
196 return vaddr;
197}
198
199pub fn shrink(self: *Atom, elf_file: *Elf) void {
200 _ = self;
201 _ = elf_file;
202}
203
204pub fn grow(self: *Atom, elf_file: *Elf) !u64 {
205 const alignment = try std.math.powi(u64, 2, self.alignment);
206 const align_ok = std.mem.alignBackward(u64, self.value, alignment) == self.value;
207 const need_realloc = !align_ok or self.size > self.capacity(elf_file);
208 if (!need_realloc) return self.value;
209 return self.allocate(elf_file);
210}
211
212pub const Index = u32;
213
214pub const Reloc = struct {
215 target: u32,
216 offset: u64,
217 addend: u32,
218 prev_vaddr: u64,
219};
99220
100221const std = @import("std");
101222const assert = std.debug.assert;
102223const elf = std.elf;
103224
225const Atom = @This();
104226const Elf = @import("../Elf.zig");
227const File = @import("file.zig").File;
src/link/Elf/LinkerDefined.zig+13-9
......@@ -23,14 +23,14 @@ pub fn addGlobal(self: *LinkerDefined, name: [:0]const u8, elf_file: *Elf) !u32
2323 .st_size = 0,
2424 });
2525 const off = try elf_file.internString("{s}", .{name});
26 const gop = try elf_file.getOrCreateGlobal(off);
26 const gop = try elf_file.getOrPutGlobal(off);
2727 self.symbols.addOneAssumeCapacity().* = gop.index;
2828 return gop.index;
2929}
3030
3131pub fn resolveSymbols(self: *LinkerDefined, elf_file: *Elf) void {
3232 for (self.symbols.items, 0..) |index, i| {
33 const sym_idx = @as(u32, @intCast(i));
33 const sym_idx = @as(Symbol.Index, @intCast(i));
3434 const this_sym = self.symtab.items[sym_idx];
3535
3636 if (this_sym.st_shndx == elf.SHN_UNDEF) continue;
......@@ -86,15 +86,19 @@ pub fn resolveSymbols(self: *LinkerDefined, elf_file: *Elf) void {
8686// }
8787// }
8888
89pub fn asFile(self: *LinkerDefined) File {
90 return .{ .linker_defined = self };
89pub fn sourceSymbol(self: *LinkerDefined, symbol_index: Symbol.Index) *elf.Elf64_Sym {
90 return &self.symtab.items[symbol_index];
9191}
9292
93pub inline fn getGlobals(self: *LinkerDefined) []const u32 {
93pub fn globals(self: *LinkerDefined) []const Symbol.Index {
9494 return self.symbols.items;
9595}
9696
97pub fn fmtSymtab(self: *InternalObject, elf_file: *Elf) std.fmt.Formatter(formatSymtab) {
97pub fn asFile(self: *LinkerDefined) File {
98 return .{ .linker_defined = self };
99}
100
101pub fn fmtSymtab(self: *LinkerDefined, elf_file: *Elf) std.fmt.Formatter(formatSymtab) {
98102 return .{ .data = .{
99103 .self = self,
100104 .elf_file = elf_file,
......@@ -102,7 +106,7 @@ pub fn fmtSymtab(self: *InternalObject, elf_file: *Elf) std.fmt.Formatter(format
102106}
103107
104108const FormatContext = struct {
105 self: *InternalObject,
109 self: *LinkerDefined,
106110 elf_file: *Elf,
107111};
108112
......@@ -115,8 +119,8 @@ fn formatSymtab(
115119 _ = unused_fmt_string;
116120 _ = options;
117121 try writer.writeAll(" globals\n");
118 for (ctx.self.getGlobals()) |index| {
119 const global = ctx.elf_file.getSymbol(index);
122 for (ctx.self.globals()) |index| {
123 const global = ctx.elf_file.symbol(index);
120124 try writer.print(" {}\n", .{global.fmt(ctx.elf_file)});
121125 }
122126}
src/link/Elf/Symbol.zig+58-44
......@@ -32,9 +32,8 @@ extra_index: u32 = 0,
3232
3333pub fn isAbs(symbol: Symbol, elf_file: *Elf) bool {
3434 const file_ptr = symbol.file(elf_file).?;
35 if (file_ptr == .shared) return symbol.sourceSymbol(elf_file).st_shndx == elf.SHN_ABS;
36 return !symbol.flags.import and symbol.atom(elf_file) == null and symbol.shndx == 0
37 and file_ptr != .linker_defined and file_ptr != .zig_module;
35 // if (file_ptr == .shared) return symbol.sourceSymbol(elf_file).st_shndx == elf.SHN_ABS;
36 return !symbol.flags.import and symbol.atom(elf_file) == null and symbol.output_section_index == 0 and file_ptr != .linker_defined and file_ptr != .zig_module;
3837}
3938
4039pub fn isLocal(symbol: Symbol) bool {
......@@ -42,34 +41,30 @@ pub fn isLocal(symbol: Symbol) bool {
4241}
4342
4443pub inline fn isIFunc(symbol: Symbol, elf_file: *Elf) bool {
45 return symbol.@"type"(elf_file) == elf.STT_GNU_IFUNC;
44 return symbol.type(elf_file) == elf.STT_GNU_IFUNC;
4645}
4746
4847pub fn @"type"(symbol: Symbol, elf_file: *Elf) u4 {
49 const file_ptr = symbol.file(elf_file).?;
5048 const s_sym = symbol.sourceSymbol(elf_file);
51 if (s_sym.st_type() == elf.STT_GNU_IFUNC and file_ptr == .shared) return elf.STT_FUNC;
49 // const file_ptr = symbol.file(elf_file).?;
50 // if (s_sym.st_type() == elf.STT_GNU_IFUNC and file_ptr == .shared) return elf.STT_FUNC;
5251 return s_sym.st_type();
5352}
5453
5554pub fn name(symbol: Symbol, elf_file: *Elf) [:0]const u8 {
56 return elf_file.strtab.getAssumeExists(symbol.name);
55 return elf_file.strtab.getAssumeExists(symbol.name_offset);
5756}
5857
5958pub fn atom(symbol: Symbol, elf_file: *Elf) ?*Atom {
60 return elf_file.atom(symbol.atom);
59 return elf_file.atom(symbol.atom_index);
6160}
6261
6362pub fn file(symbol: Symbol, elf_file: *Elf) ?File {
64 return elf_file.file(symbol.file);
63 return elf_file.file(symbol.file_index);
6564}
6665
67pub fn sourceSymbol(symbol: Symbol, elf_file: *Elf) elf.Elf64_Sym {
68 const file_ptr = symbol.file(elf_file).?;
69 return switch (file_ptr) {
70 .linker_defined, .zig_module => |x| x.symtab.items[symbol.sym_idx],
71 inline else => |x| x.symtab[symbol.sym_idx],
72 };
66pub fn sourceSymbol(symbol: Symbol, elf_file: *Elf) *elf.Elf64_Sym {
67 return symbol.file(elf_file).?.sourceSymbol(symbol.symbol_index);
7368}
7469
7570pub fn symbolRank(symbol: Symbol, elf_file: *Elf) u32 {
......@@ -82,9 +77,29 @@ pub fn symbolRank(symbol: Symbol, elf_file: *Elf) u32 {
8277 return file_ptr.symbolRank(sym, in_archive);
8378}
8479
80/// If entry already exists, returns index to it.
81/// Otherwise, creates a new entry in the Global Offset Table for this Symbol.
82pub fn getOrCreateOffsetTableEntry(self: Symbol, elf_file: *Elf) !Symbol.Index {
83 if (elf_file.got_table.lookup.get(self.symbol_index)) |index| return index;
84 const index = try elf_file.got_table.allocateEntry(elf_file.base.allocator, self.symbol_index);
85 elf_file.got_table_count_dirty = true;
86 return index;
87}
88
89pub fn getOffsetTableAddress(self: Symbol, elf_file: *Elf) u64 {
90 const got_entry_index = elf_file.got_table.lookup.get(self.symbol_index).?;
91 const target = elf_file.base.options.target;
92 const ptr_bits = target.ptrBitWidth();
93 const ptr_bytes: u64 = @divExact(ptr_bits, 8);
94 const got = elf_file.program_headers.items[elf_file.phdr_got_index.?];
95 return got.p_vaddr + got_entry_index * ptr_bytes;
96}
97
8598pub fn address(symbol: Symbol, opts: struct {
8699 plt: bool = true,
87100}, elf_file: *Elf) u64 {
101 _ = elf_file;
102 _ = opts;
88103 // if (symbol.flags.copy_rel) {
89104 // return elf_file.sectionAddress(elf_file.copy_rel_sect_index.?) + symbol.value;
90105 // }
......@@ -100,11 +115,11 @@ pub fn address(symbol: Symbol, opts: struct {
100115 return symbol.value;
101116}
102117
103pub fn gotAddress(symbol: Symbol, elf_file: *Elf) u64 {
104 if (!symbol.flags.got) return 0;
105 const extra = symbol.extra(elf_file).?;
106 return elf_file.gotEntryAddress(extra.got);
107}
118// pub fn gotAddress(symbol: Symbol, elf_file: *Elf) u64 {
119// if (!symbol.flags.got) return 0;
120// const extra = symbol.extra(elf_file).?;
121// return elf_file.gotEntryAddress(extra.got);
122// }
108123
109124// pub fn tlsGdAddress(symbol: Symbol, elf_file: *Elf) u64 {
110125// if (!symbol.flags.tlsgd) return 0;
......@@ -136,22 +151,22 @@ pub fn gotAddress(symbol: Symbol, elf_file: *Elf) u64 {
136151// @min(alignment, try std.math.powi(u64, 2, @ctz(s_sym.st_value)));
137152// }
138153
139pub fn addExtra(symbol: *Symbol, extra: Extra, elf_file: *Elf) !void {
140 symbol.extra = try elf_file.addSymbolExtra(extra);
154pub fn addExtra(symbol: *Symbol, extras: Extra, elf_file: *Elf) !void {
155 symbol.extra = try elf_file.addSymbolExtra(extras);
141156}
142157
143158pub fn extra(symbol: Symbol, elf_file: *Elf) ?Extra {
144 return elf_file.symbolExtra(symbol.extra);
159 return elf_file.symbolExtra(symbol.extra_index);
145160}
146161
147pub fn setExtra(symbol: Symbol, extra: Extra, elf_file: *Elf) void {
148 elf_file.setSymbolExtra(symbol.extra, extra);
162pub fn setExtra(symbol: Symbol, extras: Extra, elf_file: *Elf) void {
163 elf_file.setSymbolExtra(symbol.extra_index, extras);
149164}
150165
151166pub fn asElfSym(symbol: Symbol, st_name: u32, elf_file: *Elf) elf.Elf64_Sym {
152167 const file_ptr = symbol.file(elf_file).?;
153168 const s_sym = symbol.sourceSymbol(elf_file);
154 const st_type = symbol.@"type"(elf_file);
169 const st_type = symbol.type(elf_file);
155170 const st_bind: u8 = blk: {
156171 if (symbol.isLocal()) break :blk 0;
157172 if (symbol.flags.weak) break :blk elf.STB_WEAK;
......@@ -161,7 +176,7 @@ pub fn asElfSym(symbol: Symbol, st_name: u32, elf_file: *Elf) elf.Elf64_Sym {
161176 const st_shndx = blk: {
162177 // if (symbol.flags.copy_rel) break :blk elf_file.copy_rel_sect_index.?;
163178 // if (file_ptr == .shared or s_sym.st_shndx == elf.SHN_UNDEF) break :blk elf.SHN_UNDEF;
164 if (symbol.atom(elf_file) == null and file_ptr != .linker_defined and file_ptr != .zig_module)
179 if (symbol.atom(elf_file) == null and file_ptr != .linker_defined and file_ptr != .zig_module)
165180 break :blk elf.SHN_ABS;
166181 break :blk symbol.shndx;
167182 };
......@@ -221,13 +236,13 @@ fn formatName(
221236 _ = unused_fmt_string;
222237 const elf_file = ctx.elf_file;
223238 const symbol = ctx.symbol;
224 try writer.writeAll(symbol.getName(elf_file));
225 switch (symbol.ver_idx & elf.VERSYM_VERSION) {
239 try writer.writeAll(symbol.name(elf_file));
240 switch (symbol.version_index & elf.VERSYM_VERSION) {
226241 elf.VER_NDX_LOCAL, elf.VER_NDX_GLOBAL => {},
227242 else => {
228243 unreachable;
229244 // const shared = symbol.getFile(elf_file).?.shared;
230 // try writer.print("@{s}", .{shared.getVersionString(symbol.ver_idx)});
245 // try writer.print("@{s}", .{shared.getVersionString(symbol.version_index)});
231246 },
232247 }
233248}
......@@ -248,29 +263,27 @@ fn format2(
248263 _ = options;
249264 _ = unused_fmt_string;
250265 const symbol = ctx.symbol;
251 try writer.print("%{d} : {s} : @{x}", .{ symbol.sym_idx, symbol.fmtName(ctx.elf_file), symbol.value });
252 if (symbol.getFile(ctx.elf_file)) |file| {
266 try writer.print("%{d} : {s} : @{x}", .{ symbol.symbol_index, symbol.fmtName(ctx.elf_file), symbol.value });
267 if (symbol.file(ctx.elf_file)) |file_ptr| {
253268 if (symbol.isAbs(ctx.elf_file)) {
254 if (symbol.getSourceSymbol(ctx.elf_file).st_shndx == elf.SHN_UNDEF) {
269 if (symbol.sourceSymbol(ctx.elf_file).st_shndx == elf.SHN_UNDEF) {
255270 try writer.writeAll(" : undef");
256271 } else {
257272 try writer.writeAll(" : absolute");
258273 }
259 } else if (symbol.shndx != 0) {
260 try writer.print(" : sect({d})", .{symbol.shndx});
274 } else if (symbol.output_section_index != 0) {
275 try writer.print(" : sect({d})", .{symbol.output_section_index});
261276 }
262 if (symbol.getAtom(ctx.elf_file)) |atom| {
263 try writer.print(" : atom({d})", .{atom.atom_index});
277 if (symbol.atom(ctx.elf_file)) |atom_ptr| {
278 try writer.print(" : atom({d})", .{atom_ptr.atom_index});
264279 }
265280 var buf: [2]u8 = .{'_'} ** 2;
266281 if (symbol.flags.@"export") buf[0] = 'E';
267282 if (symbol.flags.import) buf[1] = 'I';
268283 try writer.print(" : {s}", .{&buf});
269284 if (symbol.flags.weak) try writer.writeAll(" : weak");
270 switch (file) {
271 .internal => |x| try writer.print(" : internal({d})", .{x.index}),
272 .object => |x| try writer.print(" : object({d})", .{x.index}),
273 .shared => |x| try writer.print(" : shared({d})", .{x.index}),
285 switch (file_ptr) {
286 inline else => |x| try writer.print(" : {s}({d})", .{ @tagName(file_ptr), x.index }),
274287 }
275288 } else try writer.writeAll(" : unresolved");
276289}
......@@ -331,7 +344,8 @@ const elf = std.elf;
331344const Atom = @import("Atom.zig");
332345const Elf = @import("../Elf.zig");
333346const File = @import("file.zig").File;
334const InternalObject = @import("InternalObject.zig");
335const Object = @import("Object.zig");
336const SharedObject = @import("SharedObject.zig");
347const LinkerDefined = @import("LinkerDefined.zig");
348// const Object = @import("Object.zig");
349// const SharedObject = @import("SharedObject.zig");
337350const Symbol = @This();
351const ZigModule = @import("ZigModule.zig");
src/link/Elf/ZigModule.zig+80-17
......@@ -1,29 +1,89 @@
11index: File.Index,
2elf_locals: std.ArrayListUnmanaged(elf.Elf64_Sym) = .{},
3locals: std.ArrayListUnmanaged(Symbol.Index) = .{},
4elf_globals: std.ArrayListUnmanaged(elf.Elf64_Sym) = .{},
5globals: std.ArrayListUnmanaged(Symbol.Index) = .{},
2
3elf_local_symbols: std.ArrayListUnmanaged(elf.Elf64_Sym) = .{},
4local_symbols: std.AutoArrayHashMapUnmanaged(Symbol.Index, void) = .{},
5
6elf_global_symbols: std.ArrayListUnmanaged(elf.Elf64_Sym) = .{},
7global_symbols: std.AutoArrayHashMapUnmanaged(Symbol.Index, void) = .{},
8
9atoms: std.ArrayListUnmanaged(Atom.Index) = .{},
10
611alive: bool = true,
712
813// output_symtab_size: Elf.SymtabSize = .{},
914
1015pub fn deinit(self: *ZigModule, allocator: Allocator) void {
11 self.elf_locals.deinit(allocator);
12 self.locals.deinit(allocator);
13 self.elf_globals.deinit(allocator);
14 self.globals.deinit(allocator);
16 self.elf_local_symbols.deinit(allocator);
17 self.local_symbols.deinit(allocator);
18 self.elf_global_symbols.deinit(allocator);
19 self.global_symbols.deinit(allocator);
20 self.atoms.deinit(allocator);
1521}
1622
17pub fn asFile(self: *ZigModule) File {
18 return .{ .zig_module = self };
23pub fn createAtom(self: *ZigModule, output_section_index: u16, elf_file: *Elf) !Symbol.Index {
24 const gpa = elf_file.base.allocator;
25 const atom_index = try elf_file.addAtom();
26 const symbol_index = try elf_file.addSymbol();
27
28 const atom_ptr = elf_file.atom(atom_index);
29 atom_ptr.file_index = self.index;
30 atom_ptr.output_section_index = output_section_index;
31
32 const symbol_ptr = elf_file.symbol(symbol_index);
33 symbol_ptr.file_index = self.index;
34 symbol_ptr.atom_index = atom_index;
35 symbol_ptr.output_section_index = output_section_index;
36
37 const local_esym = try self.elf_local_symbols.addOne(gpa);
38 local_esym.* = .{
39 .st_name = 0,
40 .st_info = elf.STB_LOCAL << 4,
41 .st_other = 0,
42 .st_shndx = output_section_index,
43 .st_value = 0,
44 .st_size = 0,
45 };
46
47 try self.atoms.append(gpa, atom_index);
48 try self.local_symbols.putNoClobber(gpa, symbol_index, {});
49
50 return symbol_index;
51}
52
53pub fn addGlobal(self: *ZigModule, name: [:0]const u8, elf_file: *Elf) !Symbol.Index {
54 const gpa = elf_file.base.allocator;
55 try self.elf_global_symbols.ensureUnusedCapacity(gpa, 1);
56 try self.global_symbols.ensureUnusedCapacity(gpa, 1);
57 const off = try elf_file.strtab.insert(gpa, name);
58 self.elf_global_symbols.appendAssumeCapacity(.{
59 .st_name = off,
60 .st_info = elf.STB_GLOBAL << 4,
61 .st_other = 0,
62 .st_shndx = 0,
63 .st_value = 0,
64 .st_size = 0,
65 });
66 const gop = try elf_file.getOrPutGlobal(off);
67 self.global_symbols.putAssumeCapacityNoClobber(gop.index, {});
68 return gop.index;
69}
70
71pub fn sourceSymbol(self: *ZigModule, symbol_index: Symbol.Index) *elf.Elf64_Sym {
72 if (self.local_symbols.get(symbol_index)) |_| return &self.elf_local_symbols.items[symbol_index];
73 assert(self.global_symbols.get(symbol_index) != null);
74 return &self.elf_global_symbols.items[symbol_index];
1975}
2076
21pub fn getLocals(self: *ZigModule) []const Symbol.Index {
22 return self.locals.items;
77pub fn locals(self: *ZigModule) []const Symbol.Index {
78 return self.local_symbols.keys();
2379}
2480
25pub fn getGlobals(self: *ZigModule) []const Symbol.Index {
26 return self.globals.items;
81pub fn globals(self: *ZigModule) []const Symbol.Index {
82 return self.global_symbols.keys();
83}
84
85pub fn asFile(self: *ZigModule) File {
86 return .{ .zig_module = self };
2787}
2888
2989pub fn fmtSymtab(self: *ZigModule, elf_file: *Elf) std.fmt.Formatter(formatSymtab) {
......@@ -47,23 +107,26 @@ fn formatSymtab(
47107 _ = unused_fmt_string;
48108 _ = options;
49109 try writer.writeAll(" locals\n");
50 for (ctx.self.getLocals()) |index| {
110 for (ctx.self.locals()) |index| {
51111 const local = ctx.elf_file.symbol(index);
52112 try writer.print(" {}\n", .{local.fmt(ctx.elf_file)});
53113 }
54114 try writer.writeAll(" globals\n");
55 for (ctx.self.getGlobals()) |index| {
56 const global = ctx.elf_file.getSymbol(index);
115 for (ctx.self.globals()) |index| {
116 const global = ctx.elf_file.symbol(index);
57117 try writer.print(" {}\n", .{global.fmt(ctx.elf_file)});
58118 }
59119}
60120
121const assert = std.debug.assert;
61122const std = @import("std");
62123const elf = std.elf;
63124
64125const Allocator = std.mem.Allocator;
126const Atom = @import("Atom.zig");
65127const Elf = @import("../Elf.zig");
66128const File = @import("file.zig").File;
129const Module = @import("../../Module.zig");
67130const ZigModule = @This();
68131// const Object = @import("Object.zig");
69132const Symbol = @import("Symbol.zig");
src/link/Elf/file.zig+7
......@@ -10,6 +10,12 @@ pub const File = union(enum) {
1010 };
1111 }
1212
13 pub fn sourceSymbol(file: File, symbol_index: Symbol.Index) *elf.Elf64_Sym {
14 return switch (file) {
15 inline else => |x| x.sourceSymbol(symbol_index),
16 };
17 }
18
1319 pub fn fmtPath(file: File) std.fmt.Formatter(formatPath) {
1420 return .{ .data = file };
1521 }
......@@ -107,4 +113,5 @@ const Elf = @import("../Elf.zig");
107113const LinkerDefined = @import("LinkerDefined.zig");
108114// const Object = @import("Object.zig");
109115// const SharedObject = @import("SharedObject.zig");
116const Symbol = @import("Symbol.zig");
110117const ZigModule = @import("ZigModule.zig");
src/link/strtab.zig+2-2
......@@ -100,13 +100,13 @@ pub fn StringTable(comptime log_scope: @Type(.EnumLiteral)) type {
100100 });
101101 }
102102
103 pub fn get(self: Self, off: u32) ?[]const u8 {
103 pub fn get(self: Self, off: u32) ?[:0]const u8 {
104104 log.debug("getting string at 0x{x}", .{off});
105105 if (off >= self.buffer.items.len) return null;
106106 return mem.sliceTo(@as([*:0]const u8, @ptrCast(self.buffer.items.ptr + off)), 0);
107107 }
108108
109 pub fn getAssumeExists(self: Self, off: u32) []const u8 {
109 pub fn getAssumeExists(self: Self, off: u32) [:0]const u8 {
110110 return self.get(off) orelse unreachable;
111111 }
112112