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...@@ -4314,10 +4314,10 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
4314 if (try self.air.value(callee, mod)) |func_value| {4314 if (try self.air.value(callee, mod)) |func_value| {
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 sym_index = try elf_file.getOrCreateMetadataForDecl(func.owner_decl);
4318 const atom = elf_file.atom(atom_index);4318 const sym = elf_file.symbol(sym_index);
4319 _ = try atom.getOrCreateOffsetTableEntry(elf_file);4319 _ = try sym.getOrCreateOffsetTableEntry(elf_file);
4320 const got_addr = @as(u32, @intCast(atom.getOffsetTableAddress(elf_file)));4320 const got_addr = @as(u32, @intCast(sym.getOffsetTableAddress(elf_file)));
4321 try self.genSetReg(Type.usize, .x30, .{ .memory = got_addr });4321 try self.genSetReg(Type.usize, .x30, .{ .memory = got_addr });
4322 } else if (self.bin_file.cast(link.File.MachO)) |macho_file| {4322 } else if (self.bin_file.cast(link.File.MachO)) |macho_file| {
4323 const atom = try macho_file.getOrCreateAtomForDecl(func.owner_decl);4323 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...@@ -4294,10 +4294,10 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
4294 if (try self.air.value(callee, mod)) |func_value| {4294 if (try self.air.value(callee, mod)) |func_value| {
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 sym_index = try elf_file.getOrCreateMetadataForDecl(func.owner_decl);
4298 const atom = elf_file.atom(atom_index);4298 const sym = elf_file.symbol(sym_index);
4299 _ = try atom.getOrCreateOffsetTableEntry(elf_file);4299 _ = try sym.getOrCreateOffsetTableEntry(elf_file);
4300 const got_addr = @as(u32, @intCast(atom.getOffsetTableAddress(elf_file)));4300 const got_addr = @as(u32, @intCast(sym.getOffsetTableAddress(elf_file)));
4301 try self.genSetReg(Type.usize, .lr, .{ .memory = got_addr });4301 try self.genSetReg(Type.usize, .lr, .{ .memory = got_addr });
4302 } else if (self.bin_file.cast(link.File.MachO)) |_| {4302 } else if (self.bin_file.cast(link.File.MachO)) |_| {
4303 unreachable; // unsupported architecture for MachO4303 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...@@ -1747,10 +1747,10 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
1747 if (try self.air.value(callee, mod)) |func_value| {1747 if (try self.air.value(callee, mod)) |func_value| {
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 sym_index = try elf_file.getOrCreateMetadataForDecl(func.owner_decl);
1751 const atom = elf_file.atom(atom_index);1751 const sym = elf_file.symbol(sym_index);
1752 _ = try atom.getOrCreateOffsetTableEntry(elf_file);1752 _ = try sym.getOrCreateOffsetTableEntry(elf_file);
1753 const got_addr = @as(u32, @intCast(atom.getOffsetTableAddress(elf_file)));1753 const got_addr = @as(u32, @intCast(sym.getOffsetTableAddress(elf_file)));
1754 try self.genSetReg(Type.usize, .ra, .{ .memory = got_addr });1754 try self.genSetReg(Type.usize, .ra, .{ .memory = got_addr });
1755 _ = try self.addInst(.{1755 _ = try self.addInst(.{
1756 .tag = .jalr,1756 .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...@@ -1349,10 +1349,10 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
1349 switch (mod.intern_pool.indexToKey(func_value.ip_index)) {1349 switch (mod.intern_pool.indexToKey(func_value.ip_index)) {
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 sym_index = try elf_file.getOrCreateMetadataForDecl(func.owner_decl);
1353 const atom = elf_file.atom(atom_index);1353 const sym = elf_file.symbol(sym_index);
1354 _ = try atom.getOrCreateOffsetTableEntry(elf_file);1354 _ = try sym.getOrCreateOffsetTableEntry(elf_file);
1355 break :blk @as(u32, @intCast(atom.getOffsetTableAddress(elf_file)));1355 break :blk @as(u32, @intCast(sym.getOffsetTableAddress(elf_file)));
1356 } else unreachable;1356 } else unreachable;
13571357
1358 try self.genSetReg(Type.usize, .o7, .{ .memory = got_addr });1358 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...@@ -8149,10 +8149,10 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
8149 else => null,8149 else => null,
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 sym_index = try elf_file.getOrCreateMetadataForDecl(owner_decl);
8153 const atom = elf_file.atom(atom_index);8153 const sym = elf_file.symbol(sym_index);
8154 _ = try atom.getOrCreateOffsetTableEntry(elf_file);8154 _ = try sym.getOrCreateOffsetTableEntry(elf_file);
8155 const got_addr = atom.getOffsetTableAddress(elf_file);8155 const got_addr = sym.getOffsetTableAddress(elf_file);
8156 try self.asmMemory(.{ ._, .call }, Memory.sib(.qword, .{8156 try self.asmMemory(.{ ._, .call }, Memory.sib(.qword, .{
8157 .base = .{ .reg = .ds },8157 .base = .{ .reg = .ds },
8158 .disp = @intCast(got_addr),8158 .disp = @intCast(got_addr),
...@@ -10215,11 +10215,11 @@ fn genLazySymbolRef(...@@ -10215,11 +10215,11 @@ fn genLazySymbolRef(
10215 lazy_sym: link.File.LazySymbol,10215 lazy_sym: link.File.LazySymbol,
10216) InnerError!void {10216) InnerError!void {
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 sym_index = elf_file.getOrCreateMetadataForLazySymbol(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.atom(atom_index);10220 const sym = elf_file.symbol(sym_index);
10221 _ = try atom.getOrCreateOffsetTableEntry(elf_file);10221 _ = try sym.getOrCreateOffsetTableEntry(elf_file);
10222 const got_addr = atom.getOffsetTableAddress(elf_file);10222 const got_addr = sym.getOffsetTableAddress(elf_file);
10223 const got_mem =10223 const got_mem =
10224 Memory.sib(.qword, .{ .base = .{ .reg = .ds }, .disp = @intCast(got_addr) });10224 Memory.sib(.qword, .{ .base = .{ .reg = .ds }, .disp = @intCast(got_addr) });
10225 switch (tag) {10225 switch (tag) {
src/codegen.zig+5-5
...@@ -854,10 +854,10 @@ fn genDeclRef(...@@ -854,10 +854,10 @@ fn genDeclRef(
854 const is_threadlocal = tv.val.isPtrToThreadLocal(mod) and !bin_file.options.single_threaded;854 const is_threadlocal = tv.val.isPtrToThreadLocal(mod) and !bin_file.options.single_threaded;
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 sym_index = try elf_file.getOrCreateMetadataForDecl(decl_index);
858 const atom = elf_file.atom(atom_index);858 const sym = elf_file.symbol(sym_index);
859 _ = try atom.getOrCreateOffsetTableEntry(elf_file);859 _ = try sym.getOrCreateOffsetTableEntry(elf_file);
860 return GenResult.mcv(.{ .memory = atom.getOffsetTableAddress(elf_file) });860 return GenResult.mcv(.{ .memory = sym.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| {
862 const atom_index = try macho_file.getOrCreateAtomForDecl(decl_index);862 const atom_index = try macho_file.getOrCreateAtomForDecl(decl_index);
863 const sym_index = macho_file.getAtom(atom_index).getSymbolIndex().?;863 const sym_index = macho_file.getAtom(atom_index).getSymbolIndex().?;
...@@ -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.symbol(local_sym_index).st_value });895 return GenResult.mcv(.{ .memory = elf_file.symbol(local_sym_index).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+234-426
...@@ -1,4 +1,4 @@...@@ -1,4 +1,4 @@
1base: File,1base: link.File,
2dwarf: ?Dwarf = null,2dwarf: ?Dwarf = null,
33
4ptr_width: PtrWidth,4ptr_width: PtrWidth,
...@@ -6,7 +6,7 @@ ptr_width: PtrWidth,...@@ -6,7 +6,7 @@ ptr_width: PtrWidth,
6/// If this is not null, an object file is created by LLVM and linked with LLD afterwards.6/// If this is not null, an object file is created by LLVM and linked with LLD afterwards.
7llvm_object: ?*LlvmObject = null,7llvm_object: ?*LlvmObject = null,
88
9files: std.MutliArrayList(File.Entry) = .{},9files: std.MultiArrayList(File.Entry) = .{},
10zig_module_index: ?File.Index = null,10zig_module_index: ?File.Index = null,
11linker_defined_index: ?File.Index = null,11linker_defined_index: ?File.Index = null,
1212
...@@ -56,14 +56,12 @@ shstrtab_section_index: ?u16 = null,...@@ -56,14 +56,12 @@ shstrtab_section_index: ?u16 = null,
56strtab_section_index: ?u16 = null,56strtab_section_index: ?u16 = null,
5757
58symbols: std.ArrayListUnmanaged(Symbol) = .{},58symbols: std.ArrayListUnmanaged(Symbol) = .{},
59globals: std.ArrayListUnmanaged(Symbol.Index) = .{},59resolver: std.AutoArrayHashMapUnmanaged(u32, Symbol.Index) = .{},
60resolver: std.StringHashMapUnmanaged(u32) = .{},
61unresolved: std.AutoArrayHashMapUnmanaged(u32, void) = .{},60unresolved: std.AutoArrayHashMapUnmanaged(u32, void) = .{},
6261
63symbols_free_list: std.ArrayListUnmanaged(u32) = .{},62symbols_free_list: std.ArrayListUnmanaged(Symbol.Index) = .{},
64globals_free_list: std.ArrayListUnmanaged(u32) = .{},
6563
66got_table: TableSection(u32) = .{},64got_table: TableSection(Symbol.Index) = .{},
6765
68phdr_table_dirty: bool = false,66phdr_table_dirty: bool = false,
69shdr_table_dirty: bool = false,67shdr_table_dirty: bool = false,
...@@ -88,9 +86,6 @@ decls: std.AutoHashMapUnmanaged(Module.Decl.Index, DeclMetadata) = .{},...@@ -88,9 +86,6 @@ decls: std.AutoHashMapUnmanaged(Module.Decl.Index, DeclMetadata) = .{},
88/// List of atoms that are owned directly by the linker.86/// List of atoms that are owned directly by the linker.
89atoms: std.ArrayListUnmanaged(Atom) = .{},87atoms: std.ArrayListUnmanaged(Atom) = .{},
9088
91/// Table of atoms indexed by the symbol index.
92atom_by_index_table: std.AutoHashMapUnmanaged(u32, Atom.Index) = .{},
93
94/// Table of unnamed constants associated with a parent `Decl`.89/// Table of unnamed constants associated with a parent `Decl`.
95/// We store them here so that we can free the constants whenever the `Decl`90/// We store them here so that we can free the constants whenever the `Decl`
96/// needs updating or is freed.91/// needs updating or is freed.
...@@ -113,12 +108,10 @@ atom_by_index_table: std.AutoHashMapUnmanaged(u32, Atom.Index) = .{},...@@ -113,12 +108,10 @@ atom_by_index_table: std.AutoHashMapUnmanaged(u32, Atom.Index) = .{},
113unnamed_const_atoms: UnnamedConstTable = .{},108unnamed_const_atoms: UnnamedConstTable = .{},
114109
115/// A table of relocations indexed by the owning them `TextBlock`.110/// 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.
118relocs: RelocTable = .{},111relocs: RelocTable = .{},
119112
120const RelocTable = std.AutoHashMapUnmanaged(Atom.Index, std.ArrayListUnmanaged(Atom.Reloc));113const 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));
122const LazySymbolTable = std.AutoArrayHashMapUnmanaged(Module.Decl.OptionalIndex, LazySymbolMetadata);115const LazySymbolTable = std.AutoArrayHashMapUnmanaged(Module.Decl.OptionalIndex, LazySymbolMetadata);
123116
124/// When allocating, the ideal_capacity is calculated by117/// When allocating, the ideal_capacity is calculated by
...@@ -143,13 +136,12 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option...@@ -143,13 +136,12 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option
143 const self = try createEmpty(allocator, options);136 const self = try createEmpty(allocator, options);
144 errdefer self.base.destroy();137 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, .{
147 .truncate = false,140 .truncate = false,
148 .read = true,141 .read = true,
149 .mode = link.determineMode(options),142 .mode = link.determineMode(options),
150 });143 });
151144
152 self.base.file = file;
153 self.shdr_table_dirty = true;145 self.shdr_table_dirty = true;
154146
155 // Index 0 is always a null symbol.147 // Index 0 is always a null symbol.
...@@ -243,19 +235,10 @@ pub fn deinit(self: *Elf) void {...@@ -243,19 +235,10 @@ pub fn deinit(self: *Elf) void {
243 self.strtab.deinit(gpa);235 self.strtab.deinit(gpa);
244 self.symbols.deinit(gpa);236 self.symbols.deinit(gpa);
245 self.symbols_free_list.deinit(gpa);237 self.symbols_free_list.deinit(gpa);
246 self.globals.deinit(gpa);
247 self.globals_free_list.deinit(gpa);
248 self.got_table.deinit(gpa);238 self.got_table.deinit(gpa);
239 self.resolver.deinit(gpa);
249 self.unresolved.deinit(gpa);240 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
259 {242 {
260 var it = self.decls.iterator();243 var it = self.decls.iterator();
261 while (it.next()) |entry| {244 while (it.next()) |entry| {
...@@ -265,7 +248,6 @@ pub fn deinit(self: *Elf) void {...@@ -265,7 +248,6 @@ pub fn deinit(self: *Elf) void {
265 }248 }
266249
267 self.atoms.deinit(gpa);250 self.atoms.deinit(gpa);
268 self.atom_by_index_table.deinit(gpa);
269 self.lazy_syms.deinit(gpa);251 self.lazy_syms.deinit(gpa);
270252
271 {253 {
...@@ -292,13 +274,12 @@ pub fn deinit(self: *Elf) void {...@@ -292,13 +274,12 @@ pub fn deinit(self: *Elf) void {
292pub fn getDeclVAddr(self: *Elf, decl_index: Module.Decl.Index, reloc_info: link.File.RelocInfo) !u64 {274pub fn getDeclVAddr(self: *Elf, decl_index: Module.Decl.Index, reloc_info: link.File.RelocInfo) !u64 {
293 assert(self.llvm_object == null);275 assert(self.llvm_object == null);
294276
295 const this_atom_index = try self.getOrCreateAtomForDecl(decl_index);277 const this_sym_index = try self.getOrCreateMetadataForDecl(decl_index);
296 const this_atom = self.atom(this_atom_index);278 const this_sym = self.symbol(this_sym_index);
297 const target = this_atom.symbolIndex().?;279 const vaddr = this_sym.value;
298 const vaddr = this_atom.symbol(self).st_value;280 const parent_atom_index = self.symbol(reloc_info.parent_atom_index).atom_index;
299 const atom_index = self.atomIndexForSymbol(reloc_info.parent_atom_index).?;281 try Atom.addRelocation(self, parent_atom_index, .{
300 try Atom.addRelocation(self, atom_index, .{282 .target = this_sym,
301 .target = target,
302 .offset = reloc_info.offset,283 .offset = reloc_info.offset,
303 .addend = reloc_info.addend,284 .addend = reloc_info.addend,
304 .prev_vaddr = vaddr,285 .prev_vaddr = vaddr,
...@@ -851,7 +832,7 @@ pub fn populateMissingMetadata(self: *Elf) !void {...@@ -851,7 +832,7 @@ pub fn populateMissingMetadata(self: *Elf) !void {
851 }832 }
852}833}
853834
854fn growAllocSection(self: *Elf, shdr_index: u16, needed_size: u64) !void {835pub fn growAllocSection(self: *Elf, shdr_index: u16, needed_size: u64) !void {
855 // TODO Also detect virtual address collisions.836 // TODO Also detect virtual address collisions.
856 const shdr = &self.sections.items(.shdr)[shdr_index];837 const shdr = &self.sections.items(.shdr)[shdr_index];
857 const phdr_index = self.sections.items(.phdr_index)[shdr_index];838 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 {...@@ -863,8 +844,7 @@ fn growAllocSection(self: *Elf, shdr_index: u16, needed_size: u64) !void {
863 const new_offset = self.findFreeSpace(needed_size, self.page_size);844 const new_offset = self.findFreeSpace(needed_size, self.page_size);
864 const existing_size = if (maybe_last_atom_index) |last_atom_index| blk: {845 const existing_size = if (maybe_last_atom_index) |last_atom_index| blk: {
865 const last = self.atom(last_atom_index);846 const last = self.atom(last_atom_index);
866 const sym = last.symbol(self);847 break :blk (last.value + last.size) - phdr.p_vaddr;
867 break :blk (sym.st_value + sym.st_size) - phdr.p_vaddr;
868 } else if (shdr_index == self.got_section_index.?) blk: {848 } else if (shdr_index == self.got_section_index.?) blk: {
869 break :blk shdr.sh_size;849 break :blk shdr.sh_size;
870 } else 0;850 } else 0;
...@@ -985,6 +965,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node...@@ -985,6 +965,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
985 // TODO This linker code currently assumes there is only 1 compilation unit and it965 // TODO This linker code currently assumes there is only 1 compilation unit and it
986 // corresponds to the Zig source code.966 // corresponds to the Zig source code.
987 const module = self.base.options.module orelse return error.LinkingWithoutZigSourceUnimplemented;967 const module = self.base.options.module orelse return error.LinkingWithoutZigSourceUnimplemented;
968 _ = module;
988969
989 self.zig_module_index = blk: {970 self.zig_module_index = blk: {
990 const index = @as(File.Index, @intCast(try self.files.addOne(gpa)));971 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...@@ -992,9 +973,9 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
992 break :blk index;973 break :blk index;
993 };974 };
994975
995 self.linker_defined = blk: {976 self.linker_defined_index = blk: {
996 const index = @as(File.Index, @intCast(try self.files.addOne(gpa)));977 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 } });
998 break :blk index;979 break :blk index;
999 };980 };
1000981
...@@ -2157,193 +2138,12 @@ fn freeAtom(self: *Elf, atom_index: Atom.Index) void {...@@ -2157,193 +2138,12 @@ fn freeAtom(self: *Elf, atom_index: Atom.Index) void {
2157 const sym_index = atom_ptr.symbolIndex().?;2138 const sym_index = atom_ptr.symbolIndex().?;
21582139
2159 log.debug("adding %{d} to local symbols free list", .{sym_index});2140 log.debug("adding %{d} to local symbols free list", .{sym_index});
2160 self.locals_free_list.append(gpa, sym_index) catch {};2141 self.symbols_free_list.append(gpa, sym_index) catch {};
2161 self.locals.items[sym_index] = null_sym;2142 self.symbols.items[sym_index] = .{};
2162 _ = self.atom_by_index_table.remove(sym_index);
2163 atom_ptr.sym_index = 0;2143 atom_ptr.sym_index = 0;
2164 self.got_table.freeEntry(gpa, sym_index);2144 self.got_table.freeEntry(gpa, sym_index);
2165}2145}
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
2347fn freeUnnamedConsts(self: *Elf, decl_index: Module.Decl.Index) void {2147fn freeUnnamedConsts(self: *Elf, decl_index: Module.Decl.Index) void {
2348 const unnamed_consts = self.unnamed_const_atoms.getPtr(decl_index) orelse return;2148 const unnamed_consts = self.unnamed_const_atoms.getPtr(decl_index) orelse return;
2349 for (unnamed_consts.items) |atom_index| {2149 for (unnamed_consts.items) |atom_index| {
...@@ -2372,40 +2172,50 @@ pub fn freeDecl(self: *Elf, decl_index: Module.Decl.Index) void {...@@ -2372,40 +2172,50 @@ pub fn freeDecl(self: *Elf, decl_index: Module.Decl.Index) void {
2372 }2172 }
2373}2173}
23742174
2375pub fn getOrCreateAtomForLazySymbol(self: *Elf, sym: link.File.LazySymbol) !Atom.Index {2175pub fn getOrCreateMetadataForLazySymbol(self: *Elf, sym: link.File.LazySymbol) !Symbol.Index {
2376 const mod = self.base.options.module.?;2176 const mod = self.base.options.module.?;
2377 const gop = try self.lazy_syms.getOrPut(self.base.allocator, sym.getDecl(mod));2177 const gop = try self.lazy_syms.getOrPut(self.base.allocator, sym.getDecl(mod));
2378 errdefer _ = if (!gop.found_existing) self.lazy_syms.pop();2178 errdefer _ = if (!gop.found_existing) self.lazy_syms.pop();
2379 if (!gop.found_existing) gop.value_ptr.* = .{};2179 if (!gop.found_existing) gop.value_ptr.* = .{};
2380 const metadata: struct { atom: *Atom.Index, state: *LazySymbolMetadata.State } = switch (sym.kind) {2180 const metadata: struct {
2381 .code => .{ .atom = &gop.value_ptr.text_atom, .state = &gop.value_ptr.text_state },2181 symbol_index: *Symbol.Index,
2382 .const_data => .{ .atom = &gop.value_ptr.rodata_atom, .state = &gop.value_ptr.rodata_state },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 },
2383 };2192 };
2193 const zig_module = self.file(self.zig_module_index.?).?.zig_module;
2384 switch (metadata.state.*) {2194 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),
2386 .pending_flush => return metadata.atom.*,2199 .pending_flush => return metadata.atom.*,
2387 .flushed => {},2200 .flushed => {},
2388 }2201 }
2389 metadata.state.* = .pending_flush;2202 metadata.state.* = .pending_flush;
2390 const atom_index = metadata.atom.*;2203 const symbol_index = metadata.symbol_index.*;
2391 // anyerror needs to be deferred until flushModule2204 // anyerror needs to be deferred until flushModule
2392 if (sym.getDecl(mod) != .none) try self.updateLazySymbolAtom(sym, atom_index, switch (sym.kind) {2205 if (sym.getDecl(mod) != .none) try self.updateLazySymbol(sym, symbol_index);
2393 .code => self.text_section_index.?,2206 return symbol_index;
2394 .const_data => self.rodata_section_index.?,
2395 });
2396 return atom_index;
2397}2207}
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 {
2400 const gop = try self.decls.getOrPut(self.base.allocator, decl_index);2210 const gop = try self.decls.getOrPut(self.base.allocator, decl_index);
2401 if (!gop.found_existing) {2211 if (!gop.found_existing) {
2212 const zig_module = self.file(self.zig_module_index.?).?.zig_module;
2402 gop.value_ptr.* = .{2213 gop.value_ptr.* = .{
2403 .atom = try self.createAtom(),2214 .symbol_index = try zig_module.createAtom(self.getDeclShdrIndex(decl_index), self),
2404 .shdr = self.getDeclShdrIndex(decl_index),
2405 .exports = .{},2215 .exports = .{},
2406 };2216 };
2407 }2217 }
2408 return gop.value_ptr.atom;2218 return gop.value_ptr.symbol_index;
2409}2219}
24102220
2411fn getDeclShdrIndex(self: *Elf, decl_index: Module.Decl.Index) u16 {2221fn getDeclShdrIndex(self: *Elf, decl_index: Module.Decl.Index) u16 {
...@@ -2434,7 +2244,13 @@ fn getDeclShdrIndex(self: *Elf, decl_index: Module.Decl.Index) u16 {...@@ -2434,7 +2244,13 @@ fn getDeclShdrIndex(self: *Elf, decl_index: Module.Decl.Index) u16 {
2434 return shdr_index;2244 return shdr_index;
2435}2245}
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 {
2438 const gpa = self.base.allocator;2254 const gpa = self.base.allocator;
2439 const mod = self.base.options.module.?;2255 const mod = self.base.options.module.?;
2440 const decl = mod.declPtr(decl_index);2256 const decl = mod.declPtr(decl_index);
...@@ -2444,60 +2260,52 @@ fn updateDeclCode(self: *Elf, decl_index: Module.Decl.Index, code: []const u8, s...@@ -2444,60 +2260,52 @@ fn updateDeclCode(self: *Elf, decl_index: Module.Decl.Index, code: []const u8, s
2444 log.debug("updateDeclCode {s}{*}", .{ decl_name, decl });2260 log.debug("updateDeclCode {s}{*}", .{ decl_name, decl });
2445 const required_alignment = decl.getAlignment(mod);2261 const required_alignment = decl.getAlignment(mod);
24462262
2447 const decl_metadata = self.decls.get(decl_index).?;2263 const sym = self.symbol(sym_index);
2448 const atom_index = decl_metadata.atom;2264 const esym = sym.sourceSymbol(self);
2449 const atom_ptr = self.atom(atom_index);2265 const atom_ptr = sym.atom(self).?;
2450 const local_sym_index = atom_ptr.symbolIndex().?;2266 const shdr_index = sym.output_section_index;
2451 const local_sym = atom_ptr.symbol(self);
24522267
2453 const shdr_index = decl_metadata.shdr;2268 sym.name_offset = try self.strtab.insert(gpa, decl_name);
2454 if (atom_ptr.symbol(self).st_size != 0 and self.base.child_pid == null) {2269 esym.st_name = sym.name_offset;
2455 local_sym.st_name = try self.strtab.insert(gpa, decl_name);2270 esym.st_info |= stt_bits;
2456 local_sym.st_info = (elf.STB_LOCAL << 4) | stt_bits;2271 esym.st_size = code.len;
2457 local_sym.st_other = 0;
2458 local_sym.st_shndx = shdr_index;
24592272
2460 const capacity = atom_ptr.capacity(self);2273 const old_size = atom_ptr.size;
2461 const need_realloc = code.len > capacity or2274 atom_ptr.alignment = math.log2_int(u64, required_alignment);
2462 !mem.isAlignedGeneric(u64, local_sym.st_value, 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);
2464 if (need_realloc) {2280 if (need_realloc) {
2465 const vaddr = try self.growAtom(atom_index, code.len, required_alignment);2281 const vaddr = try atom_ptr.grow(self);
2466 log.debug("growing {s} from 0x{x} to 0x{x}", .{ decl_name, local_sym.st_value, vaddr });2282 log.debug("growing {s} from 0x{x} to 0x{x}", .{ decl_name, sym.value, vaddr });
2467 if (vaddr != local_sym.st_value) {2283 if (vaddr != sym.value) {
2468 local_sym.st_value = vaddr;2284 sym.value = vaddr;
2285 esym.st_value = vaddr;
24692286
2470 log.debug(" (writing new offset table entry)", .{});2287 log.debug(" (writing new offset table entry)", .{});
2471 const got_entry_index = self.got_table.lookup.get(local_sym_index).?;2288 const got_entry_index = self.got_table.lookup.get(sym_index).?;
2472 self.got_table.entries.items[got_entry_index] = local_sym_index;2289 self.got_table.entries.items[got_entry_index] = sym_index;
2473 try self.writeOffsetTableEntry(got_entry_index);2290 try self.writeOffsetTableEntry(got_entry_index);
2474 }2291 }
2475 } else if (code.len < local_sym.st_size) {2292 } else if (code.len < old_size) {
2476 self.shrinkAtom(atom_index, code.len);2293 atom_ptr.shrink(self);
2477 }2294 }
2478 local_sym.st_size = code.len;
2479 } else {2295 } else {
2480 local_sym.* = .{2296 const vaddr = try atom_ptr.allocate(self);
2481 .st_name = try self.strtab.insert(gpa, decl_name),2297 errdefer self.freeAtom(atom_ptr);
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);
2490 log.debug("allocated text block for {s} at 0x{x}", .{ decl_name, vaddr });2298 log.debug("allocated text block for {s} at 0x{x}", .{ decl_name, vaddr });
24912299
2492 local_sym.st_value = vaddr;2300 sym.value = vaddr;
2493 local_sym.st_size = code.len;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);
2496 try self.writeOffsetTableEntry(got_entry_index);2304 try self.writeOffsetTableEntry(got_entry_index);
2497 }2305 }
24982306
2499 const phdr_index = self.sections.items(.phdr_index)[shdr_index];2307 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;
2501 const file_offset = self.sections.items(.shdr)[shdr_index].sh_offset + section_offset;2309 const file_offset = self.sections.items(.shdr)[shdr_index].sh_offset + section_offset;
25022310
2503 if (self.base.child_pid) |pid| {2311 if (self.base.child_pid) |pid| {
...@@ -2508,7 +2316,7 @@ fn updateDeclCode(self: *Elf, decl_index: Module.Decl.Index, code: []const u8, s...@@ -2508,7 +2316,7 @@ fn updateDeclCode(self: *Elf, decl_index: Module.Decl.Index, code: []const u8, s
2508 .iov_len = code.len,2316 .iov_len = code.len,
2509 }};2317 }};
2510 var remote_vec: [1]std.os.iovec_const = .{.{2318 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)))),
2512 .iov_len = code.len,2320 .iov_len = code.len,
2513 }};2321 }};
2514 const rc = std.os.linux.process_vm_writev(pid, &code_vec, &remote_vec, 0);2322 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...@@ -2522,8 +2330,6 @@ fn updateDeclCode(self: *Elf, decl_index: Module.Decl.Index, code: []const u8, s
2522 }2330 }
25232331
2524 try self.base.file.?.pwriteAll(code, file_offset);2332 try self.base.file.?.pwriteAll(code, file_offset);
2525
2526 return local_sym;
2527}2333}
25282334
2529pub fn updateFunc(self: *Elf, mod: *Module, func_index: InternPool.Index, air: Air, liveness: Liveness) !void {2335pub 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...@@ -2539,9 +2345,9 @@ pub fn updateFunc(self: *Elf, mod: *Module, func_index: InternPool.Index, air: A
2539 const decl_index = func.owner_decl;2345 const decl_index = func.owner_decl;
2540 const decl = mod.declPtr(decl_index);2346 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);
2543 self.freeUnnamedConsts(decl_index);2349 self.freeUnnamedConsts(decl_index);
2544 Atom.freeRelocations(self, atom_index);2350 Atom.freeRelocations(self, self.symbol(sym_index).atom_index);
25452351
2546 var code_buffer = std.ArrayList(u8).init(self.base.allocator);2352 var code_buffer = std.ArrayList(u8).init(self.base.allocator);
2547 defer code_buffer.deinit();2353 defer code_buffer.deinit();
...@@ -2564,13 +2370,14 @@ pub fn updateFunc(self: *Elf, mod: *Module, func_index: InternPool.Index, air: A...@@ -2564,13 +2370,14 @@ pub fn updateFunc(self: *Elf, mod: *Module, func_index: InternPool.Index, air: A
2564 return;2370 return;
2565 },2371 },
2566 };2372 };
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);
2568 if (decl_state) |*ds| {2374 if (decl_state) |*ds| {
2375 const sym = self.symbol(sym_index);
2569 try self.dwarf.?.commitDeclState(2376 try self.dwarf.?.commitDeclState(
2570 mod,2377 mod,
2571 decl_index,2378 decl_index,
2572 local_sym.st_value,2379 sym.value,
2573 local_sym.st_size,2380 sym.atom(self).?.size,
2574 ds,2381 ds,
2575 );2382 );
2576 }2383 }
...@@ -2604,8 +2411,8 @@ pub fn updateDecl(...@@ -2604,8 +2411,8 @@ pub fn updateDecl(
2604 }2411 }
2605 }2412 }
26062413
2607 const atom_index = try self.getOrCreateAtomForDecl(decl_index);2414 const sym_index = try self.getOrCreateMetadataForDecl(decl_index);
2608 Atom.freeRelocations(self, atom_index);2415 Atom.freeRelocations(self, self.symbol(sym_index).atom_index);
26092416
2610 var code_buffer = std.ArrayList(u8).init(self.base.allocator);2417 var code_buffer = std.ArrayList(u8).init(self.base.allocator);
2611 defer code_buffer.deinit();2418 defer code_buffer.deinit();
...@@ -2622,14 +2429,14 @@ pub fn updateDecl(...@@ -2622,14 +2429,14 @@ pub fn updateDecl(
2622 }, &code_buffer, .{2429 }, &code_buffer, .{
2623 .dwarf = ds,2430 .dwarf = ds,
2624 }, .{2431 }, .{
2625 .parent_atom_index = self.atom(atom_index).symbolIndex().?,2432 .parent_atom_index = sym_index,
2626 })2433 })
2627 else2434 else
2628 try codegen.generateSymbol(&self.base, decl.srcLoc(mod), .{2435 try codegen.generateSymbol(&self.base, decl.srcLoc(mod), .{
2629 .ty = decl.ty,2436 .ty = decl.ty,
2630 .val = decl_val,2437 .val = decl_val,
2631 }, &code_buffer, .none, .{2438 }, &code_buffer, .none, .{
2632 .parent_atom_index = self.atom(atom_index).symbolIndex().?,2439 .parent_atom_index = sym_index,
2633 });2440 });
26342441
2635 const code = switch (res) {2442 const code = switch (res) {
...@@ -2641,13 +2448,14 @@ pub fn updateDecl(...@@ -2641,13 +2448,14 @@ pub fn updateDecl(
2641 },2448 },
2642 };2449 };
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);
2645 if (decl_state) |*ds| {2452 if (decl_state) |*ds| {
2453 const sym = self.symbol(sym_index);
2646 try self.dwarf.?.commitDeclState(2454 try self.dwarf.?.commitDeclState(
2647 mod,2455 mod,
2648 decl_index,2456 decl_index,
2649 local_sym.st_value,2457 sym.value,
2650 local_sym.st_size,2458 sym.atom(self).?.size,
2651 ds,2459 ds,
2652 );2460 );
2653 }2461 }
...@@ -2657,12 +2465,7 @@ pub fn updateDecl(...@@ -2657,12 +2465,7 @@ pub fn updateDecl(
2657 return self.updateDeclExports(mod, decl_index, mod.getDeclExports(decl_index));2465 return self.updateDeclExports(mod, decl_index, mod.getDeclExports(decl_index));
2658}2466}
26592467
2660fn updateLazySymbolAtom(2468fn updateLazySymbol(self: *Elf, sym: link.File.LazySymbol, symbol_index: Symbol.Index) !void {
2661 self: *Elf,
2662 sym: link.File.LazySymbol,
2663 atom_index: Atom.Index,
2664 shdr_index: u16,
2665) !void {
2666 const gpa = self.base.allocator;2469 const gpa = self.base.allocator;
2667 const mod = self.base.options.module.?;2470 const mod = self.base.options.module.?;
26682471
...@@ -2680,9 +2483,6 @@ fn updateLazySymbolAtom(...@@ -2680,9 +2483,6 @@ fn updateLazySymbolAtom(
2680 };2483 };
2681 const name = self.strtab.get(name_str_index).?;2484 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
2686 const src = if (sym.ty.getOwnerDeclOrNull(mod)) |owner_decl|2486 const src = if (sym.ty.getOwnerDeclOrNull(mod)) |owner_decl|
2687 mod.declPtr(owner_decl).srcLoc(mod)2487 mod.declPtr(owner_decl).srcLoc(mod)
2688 else2488 else
...@@ -2698,7 +2498,7 @@ fn updateLazySymbolAtom(...@@ -2698,7 +2498,7 @@ fn updateLazySymbolAtom(
2698 &required_alignment,2498 &required_alignment,
2699 &code_buffer,2499 &code_buffer,
2700 .none,2500 .none,
2701 .{ .parent_atom_index = local_sym_index },2501 .{ .parent_atom_index = symbol_index },
2702 );2502 );
2703 const code = switch (res) {2503 const code = switch (res) {
2704 .ok => code_buffer.items,2504 .ok => code_buffer.items,
...@@ -2708,28 +2508,28 @@ fn updateLazySymbolAtom(...@@ -2708,28 +2508,28 @@ fn updateLazySymbolAtom(
2708 },2508 },
2709 };2509 };
27102510
2711 const phdr_index = self.sections.items(.phdr_index)[shdr_index];2511 const local_sym = self.symbol(symbol_index);
2712 const local_sym = atom_ptr.symbol(self);2512 const phdr_index = self.sections.items(.phdr_index)[local_sym.output_section_index];
2713 local_sym.* = .{2513 local_sym.name_offset = name_str_index;
2714 .st_name = name_str_index,2514 const local_esym = local_sym.sourceSymbol(self);
2715 .st_info = (elf.STB_LOCAL << 4) | elf.STT_OBJECT,2515 local_esym.st_name = name_str_index;
2716 .st_other = 0,2516 local_esym.st_info |= elf.STT_OBJECT;
2717 .st_shndx = shdr_index,2517 local_esym.st_size = code.len;
2718 .st_value = 0,2518 const atom_ptr = local_sym.atom(self).?;
2719 .st_size = 0,2519 atom_ptr.alignment = math.log2_int(u64, required_alignment);
2720 };2520 atom_ptr.size = code.len;
2721 const vaddr = try self.allocateAtom(atom_index, code.len, required_alignment);2521 const vaddr = try atom_ptr.allocate(self);
2722 errdefer self.freeAtom(atom_index);2522 errdefer self.freeAtom(atom_ptr);
2723 log.debug("allocated text block for {s} at 0x{x}", .{ name, vaddr });2523 log.debug("allocated text block for {s} at 0x{x}", .{ name, vaddr });
27242524
2725 local_sym.st_value = vaddr;2525 local_sym.value = vaddr;
2726 local_sym.st_size = code.len;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);
2729 try self.writeOffsetTableEntry(got_entry_index);2529 try self.writeOffsetTableEntry(got_entry_index);
27302530
2731 const section_offset = vaddr - self.program_headers.items[phdr_index].p_vaddr;2531 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;
2733 try self.base.file.?.pwriteAll(code, file_offset);2533 try self.base.file.?.pwriteAll(code, file_offset);
2734}2534}
27352535
...@@ -2756,12 +2556,13 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module...@@ -2756,12 +2556,13 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module
2756 };2556 };
2757 const name = self.strtab.get(name_str_index).?;2557 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
2761 const res = try codegen.generateSymbol(&self.base, decl.srcLoc(mod), typed_value, &code_buffer, .{2562 const res = try codegen.generateSymbol(&self.base, decl.srcLoc(mod), typed_value, &code_buffer, .{
2762 .none = {},2563 .none = {},
2763 }, .{2564 }, .{
2764 .parent_atom_index = self.atom(atom_index).symbolIndex().?,2565 .parent_atom_index = sym_index,
2765 });2566 });
2766 const code = switch (res) {2567 const code = switch (res) {
2767 .ok => code_buffer.items,2568 .ok => code_buffer.items,
...@@ -2776,24 +2577,30 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module...@@ -2776,24 +2577,30 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module
2776 const required_alignment = typed_value.ty.abiAlignment(mod);2577 const required_alignment = typed_value.ty.abiAlignment(mod);
2777 const shdr_index = self.rodata_section_index.?;2578 const shdr_index = self.rodata_section_index.?;
2778 const phdr_index = self.sections.items(.phdr_index)[shdr_index];2579 const phdr_index = self.sections.items(.phdr_index)[shdr_index];
2779 const local_sym = self.atom(atom_index).symbol(self);2580 const local_sym = self.symbol(sym_index);
2780 local_sym.st_name = name_str_index;2581 local_sym.name_offset = name_str_index;
2781 local_sym.st_info = (elf.STB_LOCAL << 4) | elf.STT_OBJECT;2582 const local_esym = local_sym.sourceSymbol(self);
2782 local_sym.st_other = 0;2583 local_esym.st_name = name_str_index;
2783 local_sym.st_shndx = shdr_index;2584 local_esym.st_info |= elf.STT_OBJECT;
2784 local_sym.st_size = code.len;2585 local_esym.st_size = code.len;
2785 local_sym.st_value = try self.allocateAtom(atom_index, code.len, required_alignment);2586 const atom_ptr = local_sym.atom(self).?;
2786 errdefer self.freeAtom(atom_index);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
2788 log.debug("allocated text block for {s} at 0x{x}", .{ name, local_sym.st_value });2592 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;
2793 const file_offset = self.sections.items(.shdr)[shdr_index].sh_offset + section_offset;2600 const file_offset = self.sections.items(.shdr)[shdr_index].sh_offset + section_offset;
2794 try self.base.file.?.pwriteAll(code, file_offset);2601 try self.base.file.?.pwriteAll(code, file_offset);
27952602
2796 return self.atom(atom_index).symbolIndex().?;2603 return sym_index;
2797}2604}
27982605
2799pub fn updateDeclExports(2606pub fn updateDeclExports(
...@@ -2815,11 +2622,10 @@ pub fn updateDeclExports(...@@ -2815,11 +2622,10 @@ pub fn updateDeclExports(
2815 const gpa = self.base.allocator;2622 const gpa = self.base.allocator;
28162623
2817 const decl = mod.declPtr(decl_index);2624 const decl = mod.declPtr(decl_index);
2818 const atom_index = try self.getOrCreateAtomForDecl(decl_index);2625 const decl_sym_index = try self.getOrCreateMetadataForDecl(decl_index);
2819 const atom_ptr = self.atom(atom_index);2626 const decl_sym = self.symbol(decl_sym_index);
2820 const decl_sym = atom_ptr.symbol(self);2627 const decl_esym = symbol.sourceSymbol(self);
2821 const decl_metadata = self.decls.getPtr(decl_index).?;2628 const decl_metadata = self.decls.getPtr(decl_index).?;
2822 const shdr_index = decl_metadata.shdr;
28232629
2824 for (exports) |exp| {2630 for (exports) |exp| {
2825 const exp_name = mod.intern_pool.stringToSlice(exp.opts.name);2631 const exp_name = mod.intern_pool.stringToSlice(exp.opts.name);
...@@ -2838,7 +2644,7 @@ pub fn updateDeclExports(...@@ -2838,7 +2644,7 @@ pub fn updateDeclExports(
2838 .Strong => blk: {2644 .Strong => blk: {
2839 const entry_name = self.base.options.entry orelse "_start";2645 const entry_name = self.base.options.entry orelse "_start";
2840 if (mem.eql(u8, exp_name, entry_name)) {2646 if (mem.eql(u8, exp_name, entry_name)) {
2841 self.entry_addr = decl_sym.st_value;2647 self.entry_addr = decl_sym.value;
2842 }2648 }
2843 break :blk elf.STB_GLOBAL;2649 break :blk elf.STB_GLOBAL;
2844 },2650 },
...@@ -2855,22 +2661,18 @@ pub fn updateDeclExports(...@@ -2855,22 +2661,18 @@ pub fn updateDeclExports(
2855 const stt_bits: u8 = @as(u4, @truncate(decl_sym.st_info));2661 const stt_bits: u8 = @as(u4, @truncate(decl_sym.st_info));
28562662
2857 const sym_index = if (decl_metadata.@"export"(self, exp_name)) |exp_index| exp_index.* else blk: {2663 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);
2859 try decl_metadata.exports.append(gpa, sym_index);2666 try decl_metadata.exports.append(gpa, sym_index);
2860 break :blk sym_index;2667 break :blk sym_index;
2861 };2668 };
2862 const sym = self.symbol(sym_index);2669 const sym = self.symbol(sym_index);
2863 sym.* = .{2670 sym.value = decl_sym.value;
2864 .st_name = try self.strtab.insert(gpa, exp_name),2671 sym.atom_index = decl_sym.atom_index;
2865 .st_info = (stb_bits << 4) | stt_bits,2672 sym.output_section_index = decl_sym.output_section_index;
2866 .st_other = 0,2673 const esym = sym.sourceSymbol(self);
2867 .st_shndx = shdr_index,2674 esym.* = decl_esym.*;
2868 .st_value = decl_sym.st_value,2675 esym.st_info = (stb_bits << 4) | stt_bits;
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;
2874 }2676 }
2875}2677}
28762678
...@@ -2901,16 +2703,21 @@ pub fn deleteDeclExport(...@@ -2901,16 +2703,21 @@ pub fn deleteDeclExport(
2901 const mod = self.base.options.module.?;2703 const mod = self.base.options.module.?;
2902 const exp_name = mod.intern_pool.stringToSlice(name);2704 const exp_name = mod.intern_pool.stringToSlice(name);
2903 const sym_index = metadata.@"export"(self, exp_name) orelse return;2705 const sym_index = metadata.@"export"(self, exp_name) orelse return;
2904 const sym = self.symbol(sym_index.*);
2905 log.debug("deleting export '{s}'", .{exp_name});2706 log.debug("deleting export '{s}'", .{exp_name});
2906 sym.* = null_sym;2707 const sym = self.symbol(sym_index.*);
2907 self.locals_free_list.append(gpa, sym_index.*) catch {};2708 const esym = sym.sourceSymbol(self);
29082709 assert(self.resolver.fetchSwapRemove(sym.name_offset) != null); // TODO don't delete it if it's not dominant
2909 if (self.resolver.fetchRemove(exp_name)) |entry| {2710 sym.* = .{};
2910 self.globals_free_list.append(gpa, entry.value) catch {};2711 // TODO free list for esym!
2911 self.globals.items[entry.value] = 0;2712 esym.* = .{
2912 }2713 .st_name = 0,
29132714 .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 {};
2914 sym_index.* = 0;2721 sym_index.* = 0;
2915}2722}
29162723
...@@ -2971,7 +2778,7 @@ fn writeOffsetTableEntry(self: *Elf, index: @TypeOf(self.got_table).Index) !void...@@ -2971,7 +2778,7 @@ fn writeOffsetTableEntry(self: *Elf, index: @TypeOf(self.got_table).Index) !void
2971 const phdr = &self.program_headers.items[self.phdr_got_index.?];2778 const phdr = &self.program_headers.items[self.phdr_got_index.?];
2972 const vaddr = phdr.p_vaddr + @as(u64, entry_size) * index;2779 const vaddr = phdr.p_vaddr + @as(u64, entry_size) * index;
2973 const got_entry = self.got_table.entries.items[index];2780 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;
2975 switch (entry_size) {2782 switch (entry_size) {
2976 2 => {2783 2 => {
2977 var buf: [2]u8 = undefined;2784 var buf: [2]u8 = undefined;
...@@ -3374,98 +3181,99 @@ const CsuObjects = struct {...@@ -3374,98 +3181,99 @@ const CsuObjects = struct {
3374 }3181 }
3375};3182};
33763183
3377fn logSymtab(self: Elf) void {3184pub fn atom(self: *Elf, atom_index: Atom.Index) *Atom {
3378 log.debug("locals:", .{});3185 assert(atom_index < self.atoms.items.len);
3379 for (self.locals.items, 0..) |sym, id| {3186 return &self.atoms.items[atom_index];
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];
3392}3187}
33933188
3394/// Returns name of the symbol at sym_index.3189pub fn addAtom(self: *Elf) !Atom.Index {
3395pub fn symbolName(self: *const Elf, sym_index: u32) []const u8 {3190 const index = @as(Atom.Index, @intCast(self.atoms.items.len));
3396 const sym = self.locals.items[sym_index];3191 const atom_ptr = try self.atoms.addOne(self.base.allocator);
3397 return self.strtab.get(sym.st_name).?;3192 atom_ptr.* = .{ .atom_index = index };
3193 return index;
3398}3194}
33993195
3400/// Returns pointer to the global entry for `name` if one exists.3196pub fn file(self: *Elf, index: File.Index) ?File {
3401pub fn global(self: *const Elf, name: []const u8) ?*u32 {3197 const tag = self.files.items(.tags)[index];
3402 const global_index = self.resolver.get(name) orelse return null;3198 return switch (tag) {
3403 return &self.globals.items[global_index];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 };
3404}3203}
34053204
3406/// Returns the index of the global entry for `name` if one exists.3205/// Returns pointer-to-symbol described at sym_index.
3407pub fn globalIndex(self: *const Elf, name: []const u8) ?u32 {3206pub fn symbol(self: *Elf, sym_index: Symbol.Index) *Symbol {
3408 return self.resolver.get(name);3207 return &self.symbols.items[sym_index];
3409}3208}
34103209
3411/// Returns global entry at `index`.3210pub fn addSymbol(self: *Elf) !Symbol.Index {
3412pub fn globalByIndex(self: *const Elf, index: u32) u32 {3211 try self.symbols.ensureUnusedCapacity(self.base.allocator, 1);
3413 assert(index < self.globals.items.len);3212 const index = blk: {
3414 return self.globals.items[index];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;
3415}3225}
34163226
3417const GetOrPutGlobalResult = struct {3227const GetOrPutGlobalResult = struct {
3418 found_existing: bool,3228 found_existing: bool,
3419 value_ptr: *u32,3229 index: Symbol.Index,
3420};3230};
34213231
3422/// Return pointer to the global entry for `name` if one exists.3232pub fn getOrPutGlobal(self: *Elf, name_off: u32) !GetOrPutGlobalResult {
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 }
3429 const gpa = self.base.allocator;3233 const gpa = self.base.allocator;
3430 const global_index = try self.allocateGlobal();3234 const gop = try self.resolver.getOrPut(gpa, name_off);
3431 const global_name = try gpa.dupe(u8, name);3235 if (!gop.found_existing) {
3432 _ = try self.resolver.put(gpa, global_name, global_index);3236 const index = try self.addSymbol();
3433 const ptr = &self.globals.items[global_index];3237 const global = self.symbol(index);
3434 return GetOrPutGlobalResult{ .found_existing = false, .value_ptr = ptr };3238 global.name_offset = name_off;
3435}3239 gop.value_ptr.* = index;
34363240 }
3437pub fn atom(self: *Elf, atom_index: Atom.Index) *Atom {3241 return .{
3438 assert(atom_index < self.atoms.items.len);3242 .found_existing = gop.found_existing,
3439 return &self.atoms.items[atom_index];3243 .index = gop.value_ptr.*,
3244 };
3440}3245}
34413246
3442/// Returns atom if there is an atom referenced by the symbol.3247pub fn getGlobalByName(self: *Elf, name: []const u8) ?Symbol.Index {
3443/// Returns null on failure.3248 const name_off = self.strtab.getOffset(name) orelse return null;
3444pub fn atomIndexForSymbol(self: *Elf, sym_index: u32) ?Atom.Index {3249 return self.resolver.get(name_off);
3445 return self.atom_by_index_table.get(sym_index);
3446}3250}
34473251
3448fn dumpState(self: *Elf ) std.fmt.Formatter(fmtDumpState) {3252fn dumpState(self: *Elf) std.fmt.Formatter(fmtDumpState) {
3449 return .{ .data = self };3253 return .{ .data = self };
3450}3254}
34513255
3452fn fmtDumpState(self: *Elf,3256fn fmtDumpState(
3257 self: *Elf,
3453 comptime unused_fmt_string: []const u8,3258 comptime unused_fmt_string: []const u8,
3454 options: std.fmt.FormatOptions,3259 options: std.fmt.FormatOptions,
3455 writer: anytype,3260 writer: anytype,
3456) !void {3261) !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 }
3458}3275}
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
3469const default_entry_addr = 0x8000000;3277const default_entry_addr = 0x8000000;
34703278
3471pub const base_tag: link.File.Tag = .elf;3279pub const base_tag: link.File.Tag = .elf;
...@@ -3497,21 +3305,20 @@ const Section = struct {...@@ -3497,21 +3305,20 @@ const Section = struct {
34973305
3498const LazySymbolMetadata = struct {3306const LazySymbolMetadata = struct {
3499 const State = enum { unused, pending_flush, flushed };3307 const State = enum { unused, pending_flush, flushed };
3500 text_atom: Atom.Index = undefined,3308 text_symbol_index: Symbol.Index = undefined,
3501 rodata_atom: Atom.Index = undefined,3309 rodata_symbol_index: Symbol.Index = undefined,
3502 text_state: State = .unused,3310 text_state: State = .unused,
3503 rodata_state: State = .unused,3311 rodata_state: State = .unused,
3504};3312};
35053313
3506const DeclMetadata = struct {3314const DeclMetadata = struct {
3507 atom: Atom.Index,3315 symbol_index: Symbol.Index,
3508 shdr: u16,
3509 /// A list of all exports aliases of this Decl.3316 /// 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 {
3513 for (m.exports.items) |*exp| {3320 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;
3515 }3322 }
3516 return null;3323 return null;
3517 }3324 }
...@@ -3543,13 +3350,14 @@ pub const Atom = @import("Elf/Atom.zig");...@@ -3543,13 +3350,14 @@ pub const Atom = @import("Elf/Atom.zig");
3543const Cache = std.Build.Cache;3350const Cache = std.Build.Cache;
3544const Compilation = @import("../Compilation.zig");3351const Compilation = @import("../Compilation.zig");
3545const Dwarf = @import("Dwarf.zig");3352const Dwarf = @import("Dwarf.zig");
3546const File = @import("Elf/File.zig");3353const File = @import("Elf/file.zig").File;
3547const LinkerDefined = @import("Elf/LinkerDefined.zig");3354const LinkerDefined = @import("Elf/LinkerDefined.zig");
3548const Liveness = @import("../Liveness.zig");3355const Liveness = @import("../Liveness.zig");
3549const LlvmObject = @import("../codegen/llvm.zig").Object;3356const LlvmObject = @import("../codegen/llvm.zig").Object;
3550const Module = @import("../Module.zig");3357const Module = @import("../Module.zig");
3551const InternPool = @import("../InternPool.zig");3358const InternPool = @import("../InternPool.zig");
3552const Package = @import("../Package.zig");3359const Package = @import("../Package.zig");
3360const Symbol = @import("Elf/Symbol.zig");
3553const StringTable = @import("strtab.zig").StringTable;3361const StringTable = @import("strtab.zig").StringTable;
3554const TableSection = @import("table_section.zig").TableSection;3362const TableSection = @import("table_section.zig").TableSection;
3555const Type = @import("../type.zig").Type;3363const Type = @import("../type.zig").Type;
src/link/Elf/Atom.zig+184-61
...@@ -1,81 +1,65 @@...@@ -1,81 +1,65 @@
1/// Each decl always gets a local symbol with the fully qualified name.1/// Address allocated for this Atom.
2/// The vaddr and size are found here directly.2value: u64 = 0,
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,
83
9/// Points to the previous and next neighbors, based on the `text_offset`.4/// Name of this Atom.
10/// This can be used to find, for example, the capacity of this `TextBlock`.5name_offset: u32 = 0,
11prev_index: ?Index = null,
12next_index: ?Index = null,
136
14pub const Index = u32;7/// Index into linker's input file table.
8file_index: File.Index = 0,
159
16pub const Reloc = struct {10/// Size of this atom
17 target: u32,11size: u64 = 0,
18 offset: u64,
19 addend: u32,
20 prev_vaddr: u64,
21};
2212
23pub fn symbolIndex(self: *const Atom) ?u32 {13/// Alignment of this atom as a power of two.
24 if (self.sym_index == 0) return null;14alignment: u8 = 0,
25 return self.sym_index;
26}
2715
28pub fn symbol(self: *const Atom, elf_file: *Elf) *elf.Elf64_Sym {16/// Index of the input section.
29 return elf_file.symbol(self.symbolIndex().?);17input_section_index: u16 = 0,
30}
3118
32pub fn name(self: *const Atom, elf_file: *Elf) []const u8 {19/// Index of the output section.
33 return elf_file.symbolName(self.symbolIndex().?);20output_section_index: u16 = 0,
34}
3521
36/// If entry already exists, returns index to it.22/// Index of the input section containing this atom's relocs.
37/// Otherwise, creates a new entry in the Global Offset Table for this Atom.23relocs_section_index: u16 = 0,
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}
4524
46pub fn getOffsetTableAddress(self: *const Atom, elf_file: *Elf) u64 {25/// Index of this atom in the linker's atoms table.
47 const sym_index = self.symbolIndex().?;26atom_index: Index = 0,
48 const got_entry_index = elf_file.got_table.lookup.get(sym_index).?;27
49 const target = elf_file.base.options.target;28/// Specifies whether this atom is alive or has been garbage collected.
50 const ptr_bits = target.ptrBitWidth();29alive: bool = true,
51 const ptr_bytes: u64 = @divExact(ptr_bits, 8);30
52 const got = elf_file.program_headers.items[elf_file.phdr_got_index.?];31/// Specifies if the atom has been visited during garbage collection.
53 return got.p_vaddr + got_entry_index * ptr_bytes;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);
54}47}
5548
56/// Returns how much room there is to grow in virtual address space.49/// Returns how much room there is to grow in virtual address space.
57/// File offset relocation happens transparently, so it is not included in50/// File offset relocation happens transparently, so it is not included in
58/// this calculation.51/// this calculation.
59pub fn capacity(self: *const Atom, elf_file: *Elf) u64 {52pub fn capacity(self: Atom, elf_file: *Elf) u64 {
60 const self_sym = self.symbol(elf_file);53 const next_value = if (self.next_index) |next_index| elf_file.atom(next_index).value else std.math.maxInt(u32);
61 if (self.next_index) |next_index| {54 return next_value - self.value;
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 }
69}55}
7056
71pub fn freeListEligible(self: *const Atom, elf_file: *Elf) bool {57pub fn freeListEligible(self: Atom, elf_file: *Elf) bool {
72 // No need to keep a free list node for the last block.58 // No need to keep a free list node for the last block.
73 const next_index = self.next_index orelse return false;59 const next_index = self.next_index orelse return false;
74 const next = elf_file.atom(next_index);60 const next = elf_file.atom(next_index);
75 const self_sym = self.symbol(elf_file);61 const cap = next.value - self.value;
76 const next_sym = next.symbol(elf_file);62 const ideal_cap = Elf.padToIdeal(self.size);
77 const cap = next_sym.st_value - self_sym.st_value;
78 const ideal_cap = Elf.padToIdeal(self_sym.st_size);
79 if (cap <= ideal_cap) return false;63 if (cap <= ideal_cap) return false;
80 const surplus = cap - ideal_cap;64 const surplus = cap - ideal_cap;
81 return surplus >= Elf.min_text_capacity;65 return surplus >= Elf.min_text_capacity;
...@@ -95,10 +79,149 @@ pub fn freeRelocations(elf_file: *Elf, atom_index: Index) void {...@@ -95,10 +79,149 @@ pub fn freeRelocations(elf_file: *Elf, atom_index: Index) void {
95 if (removed_relocs) |*relocs| relocs.value.deinit(elf_file.base.allocator);79 if (removed_relocs) |*relocs| relocs.value.deinit(elf_file.base.allocator);
96}80}
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
100const std = @import("std");221const std = @import("std");
101const assert = std.debug.assert;222const assert = std.debug.assert;
102const elf = std.elf;223const elf = std.elf;
103224
225const Atom = @This();
104const Elf = @import("../Elf.zig");226const 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...@@ -23,14 +23,14 @@ pub fn addGlobal(self: *LinkerDefined, name: [:0]const u8, elf_file: *Elf) !u32
23 .st_size = 0,23 .st_size = 0,
24 });24 });
25 const off = try elf_file.internString("{s}", .{name});25 const off = try elf_file.internString("{s}", .{name});
26 const gop = try elf_file.getOrCreateGlobal(off);26 const gop = try elf_file.getOrPutGlobal(off);
27 self.symbols.addOneAssumeCapacity().* = gop.index;27 self.symbols.addOneAssumeCapacity().* = gop.index;
28 return gop.index;28 return gop.index;
29}29}
3030
31pub fn resolveSymbols(self: *LinkerDefined, elf_file: *Elf) void {31pub fn resolveSymbols(self: *LinkerDefined, elf_file: *Elf) void {
32 for (self.symbols.items, 0..) |index, i| {32 for (self.symbols.items, 0..) |index, i| {
33 const sym_idx = @as(u32, @intCast(i));33 const sym_idx = @as(Symbol.Index, @intCast(i));
34 const this_sym = self.symtab.items[sym_idx];34 const this_sym = self.symtab.items[sym_idx];
3535
36 if (this_sym.st_shndx == elf.SHN_UNDEF) continue;36 if (this_sym.st_shndx == elf.SHN_UNDEF) continue;
...@@ -86,15 +86,19 @@ pub fn resolveSymbols(self: *LinkerDefined, elf_file: *Elf) void {...@@ -86,15 +86,19 @@ pub fn resolveSymbols(self: *LinkerDefined, elf_file: *Elf) void {
86// }86// }
87// }87// }
8888
89pub fn asFile(self: *LinkerDefined) File {89pub fn sourceSymbol(self: *LinkerDefined, symbol_index: Symbol.Index) *elf.Elf64_Sym {
90 return .{ .linker_defined = self };90 return &self.symtab.items[symbol_index];
91}91}
9292
93pub inline fn getGlobals(self: *LinkerDefined) []const u32 {93pub fn globals(self: *LinkerDefined) []const Symbol.Index {
94 return self.symbols.items;94 return self.symbols.items;
95}95}
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) {
98 return .{ .data = .{102 return .{ .data = .{
99 .self = self,103 .self = self,
100 .elf_file = elf_file,104 .elf_file = elf_file,
...@@ -102,7 +106,7 @@ pub fn fmtSymtab(self: *InternalObject, elf_file: *Elf) std.fmt.Formatter(format...@@ -102,7 +106,7 @@ pub fn fmtSymtab(self: *InternalObject, elf_file: *Elf) std.fmt.Formatter(format
102}106}
103107
104const FormatContext = struct {108const FormatContext = struct {
105 self: *InternalObject,109 self: *LinkerDefined,
106 elf_file: *Elf,110 elf_file: *Elf,
107};111};
108112
...@@ -115,8 +119,8 @@ fn formatSymtab(...@@ -115,8 +119,8 @@ fn formatSymtab(
115 _ = unused_fmt_string;119 _ = unused_fmt_string;
116 _ = options;120 _ = options;
117 try writer.writeAll(" globals\n");121 try writer.writeAll(" globals\n");
118 for (ctx.self.getGlobals()) |index| {122 for (ctx.self.globals()) |index| {
119 const global = ctx.elf_file.getSymbol(index);123 const global = ctx.elf_file.symbol(index);
120 try writer.print(" {}\n", .{global.fmt(ctx.elf_file)});124 try writer.print(" {}\n", .{global.fmt(ctx.elf_file)});
121 }125 }
122}126}
src/link/Elf/Symbol.zig+58-44
...@@ -32,9 +32,8 @@ extra_index: u32 = 0,...@@ -32,9 +32,8 @@ extra_index: u32 = 0,
3232
33pub fn isAbs(symbol: Symbol, elf_file: *Elf) bool {33pub fn isAbs(symbol: Symbol, elf_file: *Elf) bool {
34 const file_ptr = symbol.file(elf_file).?;34 const file_ptr = symbol.file(elf_file).?;
35 if (file_ptr == .shared) return symbol.sourceSymbol(elf_file).st_shndx == elf.SHN_ABS;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 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;
37 and file_ptr != .linker_defined and file_ptr != .zig_module;
38}37}
3938
40pub fn isLocal(symbol: Symbol) bool {39pub fn isLocal(symbol: Symbol) bool {
...@@ -42,34 +41,30 @@ pub fn isLocal(symbol: Symbol) bool {...@@ -42,34 +41,30 @@ pub fn isLocal(symbol: Symbol) bool {
42}41}
4342
44pub inline fn isIFunc(symbol: Symbol, elf_file: *Elf) bool {43pub 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;
46}45}
4746
48pub fn @"type"(symbol: Symbol, elf_file: *Elf) u4 {47pub fn @"type"(symbol: Symbol, elf_file: *Elf) u4 {
49 const file_ptr = symbol.file(elf_file).?;
50 const s_sym = symbol.sourceSymbol(elf_file);48 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;
52 return s_sym.st_type();51 return s_sym.st_type();
53}52}
5453
55pub fn name(symbol: Symbol, elf_file: *Elf) [:0]const u8 {54pub 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);
57}56}
5857
59pub fn atom(symbol: Symbol, elf_file: *Elf) ?*Atom {58pub fn atom(symbol: Symbol, elf_file: *Elf) ?*Atom {
60 return elf_file.atom(symbol.atom);59 return elf_file.atom(symbol.atom_index);
61}60}
6261
63pub fn file(symbol: Symbol, elf_file: *Elf) ?File {62pub fn file(symbol: Symbol, elf_file: *Elf) ?File {
64 return elf_file.file(symbol.file);63 return elf_file.file(symbol.file_index);
65}64}
6665
67pub fn sourceSymbol(symbol: Symbol, elf_file: *Elf) elf.Elf64_Sym {66pub fn sourceSymbol(symbol: Symbol, elf_file: *Elf) *elf.Elf64_Sym {
68 const file_ptr = symbol.file(elf_file).?;67 return symbol.file(elf_file).?.sourceSymbol(symbol.symbol_index);
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 };
73}68}
7469
75pub fn symbolRank(symbol: Symbol, elf_file: *Elf) u32 {70pub fn symbolRank(symbol: Symbol, elf_file: *Elf) u32 {
...@@ -82,9 +77,29 @@ pub fn symbolRank(symbol: Symbol, elf_file: *Elf) u32 {...@@ -82,9 +77,29 @@ pub fn symbolRank(symbol: Symbol, elf_file: *Elf) u32 {
82 return file_ptr.symbolRank(sym, in_archive);77 return file_ptr.symbolRank(sym, in_archive);
83}78}
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
85pub fn address(symbol: Symbol, opts: struct {98pub fn address(symbol: Symbol, opts: struct {
86 plt: bool = true,99 plt: bool = true,
87}, elf_file: *Elf) u64 {100}, elf_file: *Elf) u64 {
101 _ = elf_file;
102 _ = opts;
88 // if (symbol.flags.copy_rel) {103 // if (symbol.flags.copy_rel) {
89 // return elf_file.sectionAddress(elf_file.copy_rel_sect_index.?) + symbol.value;104 // return elf_file.sectionAddress(elf_file.copy_rel_sect_index.?) + symbol.value;
90 // }105 // }
...@@ -100,11 +115,11 @@ pub fn address(symbol: Symbol, opts: struct {...@@ -100,11 +115,11 @@ pub fn address(symbol: Symbol, opts: struct {
100 return symbol.value;115 return symbol.value;
101}116}
102117
103pub fn gotAddress(symbol: Symbol, elf_file: *Elf) u64 {118// pub fn gotAddress(symbol: Symbol, elf_file: *Elf) u64 {
104 if (!symbol.flags.got) return 0;119// if (!symbol.flags.got) return 0;
105 const extra = symbol.extra(elf_file).?;120// const extra = symbol.extra(elf_file).?;
106 return elf_file.gotEntryAddress(extra.got);121// return elf_file.gotEntryAddress(extra.got);
107}122// }
108123
109// pub fn tlsGdAddress(symbol: Symbol, elf_file: *Elf) u64 {124// pub fn tlsGdAddress(symbol: Symbol, elf_file: *Elf) u64 {
110// if (!symbol.flags.tlsgd) return 0;125// if (!symbol.flags.tlsgd) return 0;
...@@ -136,22 +151,22 @@ pub fn gotAddress(symbol: Symbol, elf_file: *Elf) u64 {...@@ -136,22 +151,22 @@ pub fn gotAddress(symbol: Symbol, elf_file: *Elf) u64 {
136// @min(alignment, try std.math.powi(u64, 2, @ctz(s_sym.st_value)));151// @min(alignment, try std.math.powi(u64, 2, @ctz(s_sym.st_value)));
137// }152// }
138153
139pub fn addExtra(symbol: *Symbol, extra: Extra, elf_file: *Elf) !void {154pub fn addExtra(symbol: *Symbol, extras: Extra, elf_file: *Elf) !void {
140 symbol.extra = try elf_file.addSymbolExtra(extra);155 symbol.extra = try elf_file.addSymbolExtra(extras);
141}156}
142157
143pub fn extra(symbol: Symbol, elf_file: *Elf) ?Extra {158pub fn extra(symbol: Symbol, elf_file: *Elf) ?Extra {
144 return elf_file.symbolExtra(symbol.extra);159 return elf_file.symbolExtra(symbol.extra_index);
145}160}
146161
147pub fn setExtra(symbol: Symbol, extra: Extra, elf_file: *Elf) void {162pub fn setExtra(symbol: Symbol, extras: Extra, elf_file: *Elf) void {
148 elf_file.setSymbolExtra(symbol.extra, extra);163 elf_file.setSymbolExtra(symbol.extra_index, extras);
149}164}
150165
151pub fn asElfSym(symbol: Symbol, st_name: u32, elf_file: *Elf) elf.Elf64_Sym {166pub fn asElfSym(symbol: Symbol, st_name: u32, elf_file: *Elf) elf.Elf64_Sym {
152 const file_ptr = symbol.file(elf_file).?;167 const file_ptr = symbol.file(elf_file).?;
153 const s_sym = symbol.sourceSymbol(elf_file);168 const s_sym = symbol.sourceSymbol(elf_file);
154 const st_type = symbol.@"type"(elf_file);169 const st_type = symbol.type(elf_file);
155 const st_bind: u8 = blk: {170 const st_bind: u8 = blk: {
156 if (symbol.isLocal()) break :blk 0;171 if (symbol.isLocal()) break :blk 0;
157 if (symbol.flags.weak) break :blk elf.STB_WEAK;172 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 {...@@ -161,7 +176,7 @@ pub fn asElfSym(symbol: Symbol, st_name: u32, elf_file: *Elf) elf.Elf64_Sym {
161 const st_shndx = blk: {176 const st_shndx = blk: {
162 // if (symbol.flags.copy_rel) break :blk elf_file.copy_rel_sect_index.?;177 // if (symbol.flags.copy_rel) break :blk elf_file.copy_rel_sect_index.?;
163 // if (file_ptr == .shared or s_sym.st_shndx == elf.SHN_UNDEF) break :blk elf.SHN_UNDEF;178 // 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)
165 break :blk elf.SHN_ABS;180 break :blk elf.SHN_ABS;
166 break :blk symbol.shndx;181 break :blk symbol.shndx;
167 };182 };
...@@ -221,13 +236,13 @@ fn formatName(...@@ -221,13 +236,13 @@ fn formatName(
221 _ = unused_fmt_string;236 _ = unused_fmt_string;
222 const elf_file = ctx.elf_file;237 const elf_file = ctx.elf_file;
223 const symbol = ctx.symbol;238 const symbol = ctx.symbol;
224 try writer.writeAll(symbol.getName(elf_file));239 try writer.writeAll(symbol.name(elf_file));
225 switch (symbol.ver_idx & elf.VERSYM_VERSION) {240 switch (symbol.version_index & elf.VERSYM_VERSION) {
226 elf.VER_NDX_LOCAL, elf.VER_NDX_GLOBAL => {},241 elf.VER_NDX_LOCAL, elf.VER_NDX_GLOBAL => {},
227 else => {242 else => {
228 unreachable;243 unreachable;
229 // const shared = symbol.getFile(elf_file).?.shared;244 // 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)});
231 },246 },
232 }247 }
233}248}
...@@ -248,29 +263,27 @@ fn format2(...@@ -248,29 +263,27 @@ fn format2(
248 _ = options;263 _ = options;
249 _ = unused_fmt_string;264 _ = unused_fmt_string;
250 const symbol = ctx.symbol;265 const symbol = ctx.symbol;
251 try writer.print("%{d} : {s} : @{x}", .{ symbol.sym_idx, symbol.fmtName(ctx.elf_file), symbol.value });266 try writer.print("%{d} : {s} : @{x}", .{ symbol.symbol_index, symbol.fmtName(ctx.elf_file), symbol.value });
252 if (symbol.getFile(ctx.elf_file)) |file| {267 if (symbol.file(ctx.elf_file)) |file_ptr| {
253 if (symbol.isAbs(ctx.elf_file)) {268 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) {
255 try writer.writeAll(" : undef");270 try writer.writeAll(" : undef");
256 } else {271 } else {
257 try writer.writeAll(" : absolute");272 try writer.writeAll(" : absolute");
258 }273 }
259 } else if (symbol.shndx != 0) {274 } else if (symbol.output_section_index != 0) {
260 try writer.print(" : sect({d})", .{symbol.shndx});275 try writer.print(" : sect({d})", .{symbol.output_section_index});
261 }276 }
262 if (symbol.getAtom(ctx.elf_file)) |atom| {277 if (symbol.atom(ctx.elf_file)) |atom_ptr| {
263 try writer.print(" : atom({d})", .{atom.atom_index});278 try writer.print(" : atom({d})", .{atom_ptr.atom_index});
264 }279 }
265 var buf: [2]u8 = .{'_'} ** 2;280 var buf: [2]u8 = .{'_'} ** 2;
266 if (symbol.flags.@"export") buf[0] = 'E';281 if (symbol.flags.@"export") buf[0] = 'E';
267 if (symbol.flags.import) buf[1] = 'I';282 if (symbol.flags.import) buf[1] = 'I';
268 try writer.print(" : {s}", .{&buf});283 try writer.print(" : {s}", .{&buf});
269 if (symbol.flags.weak) try writer.writeAll(" : weak");284 if (symbol.flags.weak) try writer.writeAll(" : weak");
270 switch (file) {285 switch (file_ptr) {
271 .internal => |x| try writer.print(" : internal({d})", .{x.index}),286 inline else => |x| try writer.print(" : {s}({d})", .{ @tagName(file_ptr), x.index }),
272 .object => |x| try writer.print(" : object({d})", .{x.index}),
273 .shared => |x| try writer.print(" : shared({d})", .{x.index}),
274 }287 }
275 } else try writer.writeAll(" : unresolved");288 } else try writer.writeAll(" : unresolved");
276}289}
...@@ -331,7 +344,8 @@ const elf = std.elf;...@@ -331,7 +344,8 @@ const elf = std.elf;
331const Atom = @import("Atom.zig");344const Atom = @import("Atom.zig");
332const Elf = @import("../Elf.zig");345const Elf = @import("../Elf.zig");
333const File = @import("file.zig").File;346const File = @import("file.zig").File;
334const InternalObject = @import("InternalObject.zig");347const LinkerDefined = @import("LinkerDefined.zig");
335const Object = @import("Object.zig");348// const Object = @import("Object.zig");
336const SharedObject = @import("SharedObject.zig");349// const SharedObject = @import("SharedObject.zig");
337const Symbol = @This();350const Symbol = @This();
351const ZigModule = @import("ZigModule.zig");
src/link/Elf/ZigModule.zig+80-17
...@@ -1,29 +1,89 @@...@@ -1,29 +1,89 @@
1index: File.Index,1index: File.Index,
2elf_locals: std.ArrayListUnmanaged(elf.Elf64_Sym) = .{},2
3locals: std.ArrayListUnmanaged(Symbol.Index) = .{},3elf_local_symbols: std.ArrayListUnmanaged(elf.Elf64_Sym) = .{},
4elf_globals: std.ArrayListUnmanaged(elf.Elf64_Sym) = .{},4local_symbols: std.AutoArrayHashMapUnmanaged(Symbol.Index, void) = .{},
5globals: std.ArrayListUnmanaged(Symbol.Index) = .{},5
6elf_global_symbols: std.ArrayListUnmanaged(elf.Elf64_Sym) = .{},
7global_symbols: std.AutoArrayHashMapUnmanaged(Symbol.Index, void) = .{},
8
9atoms: std.ArrayListUnmanaged(Atom.Index) = .{},
10
6alive: bool = true,11alive: bool = true,
712
8// output_symtab_size: Elf.SymtabSize = .{},13// output_symtab_size: Elf.SymtabSize = .{},
914
10pub fn deinit(self: *ZigModule, allocator: Allocator) void {15pub fn deinit(self: *ZigModule, allocator: Allocator) void {
11 self.elf_locals.deinit(allocator);16 self.elf_local_symbols.deinit(allocator);
12 self.locals.deinit(allocator);17 self.local_symbols.deinit(allocator);
13 self.elf_globals.deinit(allocator);18 self.elf_global_symbols.deinit(allocator);
14 self.globals.deinit(allocator);19 self.global_symbols.deinit(allocator);
20 self.atoms.deinit(allocator);
15}21}
1622
17pub fn asFile(self: *ZigModule) File {23pub fn createAtom(self: *ZigModule, output_section_index: u16, elf_file: *Elf) !Symbol.Index {
18 return .{ .zig_module = self };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];
19}75}
2076
21pub fn getLocals(self: *ZigModule) []const Symbol.Index {77pub fn locals(self: *ZigModule) []const Symbol.Index {
22 return self.locals.items;78 return self.local_symbols.keys();
23}79}
2480
25pub fn getGlobals(self: *ZigModule) []const Symbol.Index {81pub fn globals(self: *ZigModule) []const Symbol.Index {
26 return self.globals.items;82 return self.global_symbols.keys();
83}
84
85pub fn asFile(self: *ZigModule) File {
86 return .{ .zig_module = self };
27}87}
2888
29pub fn fmtSymtab(self: *ZigModule, elf_file: *Elf) std.fmt.Formatter(formatSymtab) {89pub fn fmtSymtab(self: *ZigModule, elf_file: *Elf) std.fmt.Formatter(formatSymtab) {
...@@ -47,23 +107,26 @@ fn formatSymtab(...@@ -47,23 +107,26 @@ fn formatSymtab(
47 _ = unused_fmt_string;107 _ = unused_fmt_string;
48 _ = options;108 _ = options;
49 try writer.writeAll(" locals\n");109 try writer.writeAll(" locals\n");
50 for (ctx.self.getLocals()) |index| {110 for (ctx.self.locals()) |index| {
51 const local = ctx.elf_file.symbol(index);111 const local = ctx.elf_file.symbol(index);
52 try writer.print(" {}\n", .{local.fmt(ctx.elf_file)});112 try writer.print(" {}\n", .{local.fmt(ctx.elf_file)});
53 }113 }
54 try writer.writeAll(" globals\n");114 try writer.writeAll(" globals\n");
55 for (ctx.self.getGlobals()) |index| {115 for (ctx.self.globals()) |index| {
56 const global = ctx.elf_file.getSymbol(index);116 const global = ctx.elf_file.symbol(index);
57 try writer.print(" {}\n", .{global.fmt(ctx.elf_file)});117 try writer.print(" {}\n", .{global.fmt(ctx.elf_file)});
58 }118 }
59}119}
60120
121const assert = std.debug.assert;
61const std = @import("std");122const std = @import("std");
62const elf = std.elf;123const elf = std.elf;
63124
64const Allocator = std.mem.Allocator;125const Allocator = std.mem.Allocator;
126const Atom = @import("Atom.zig");
65const Elf = @import("../Elf.zig");127const Elf = @import("../Elf.zig");
66const File = @import("file.zig").File;128const File = @import("file.zig").File;
129const Module = @import("../../Module.zig");
67const ZigModule = @This();130const ZigModule = @This();
68// const Object = @import("Object.zig");131// const Object = @import("Object.zig");
69const Symbol = @import("Symbol.zig");132const Symbol = @import("Symbol.zig");
src/link/Elf/file.zig+7
...@@ -10,6 +10,12 @@ pub const File = union(enum) {...@@ -10,6 +10,12 @@ pub const File = union(enum) {
10 };10 };
11 }11 }
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
13 pub fn fmtPath(file: File) std.fmt.Formatter(formatPath) {19 pub fn fmtPath(file: File) std.fmt.Formatter(formatPath) {
14 return .{ .data = file };20 return .{ .data = file };
15 }21 }
...@@ -107,4 +113,5 @@ const Elf = @import("../Elf.zig");...@@ -107,4 +113,5 @@ const Elf = @import("../Elf.zig");
107const LinkerDefined = @import("LinkerDefined.zig");113const LinkerDefined = @import("LinkerDefined.zig");
108// const Object = @import("Object.zig");114// const Object = @import("Object.zig");
109// const SharedObject = @import("SharedObject.zig");115// const SharedObject = @import("SharedObject.zig");
116const Symbol = @import("Symbol.zig");
110const ZigModule = @import("ZigModule.zig");117const ZigModule = @import("ZigModule.zig");
src/link/strtab.zig+2-2
...@@ -100,13 +100,13 @@ pub fn StringTable(comptime log_scope: @Type(.EnumLiteral)) type {...@@ -100,13 +100,13 @@ pub fn StringTable(comptime log_scope: @Type(.EnumLiteral)) type {
100 });100 });
101 }101 }
102102
103 pub fn get(self: Self, off: u32) ?[]const u8 {103 pub fn get(self: Self, off: u32) ?[:0]const u8 {
104 log.debug("getting string at 0x{x}", .{off});104 log.debug("getting string at 0x{x}", .{off});
105 if (off >= self.buffer.items.len) return null;105 if (off >= self.buffer.items.len) return null;
106 return mem.sliceTo(@as([*:0]const u8, @ptrCast(self.buffer.items.ptr + off)), 0);106 return mem.sliceTo(@as([*:0]const u8, @ptrCast(self.buffer.items.ptr + off)), 0);
107 }107 }
108108
109 pub fn getAssumeExists(self: Self, off: u32) []const u8 {109 pub fn getAssumeExists(self: Self, off: u32) [:0]const u8 {
110 return self.get(off) orelse unreachable;110 return self.get(off) orelse unreachable;
111 }111 }
112112