| ... | @@ -1,4 +1,4 @@ | ... | @@ -1,4 +1,4 @@ |
| 1 | base: File, | 1 | base: link.File, |
| 2 | dwarf: ?Dwarf = null, | 2 | dwarf: ?Dwarf = null, |
| 3 | | 3 | |
| 4 | ptr_width: PtrWidth, | 4 | ptr_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. |
| 7 | llvm_object: ?*LlvmObject = null, | 7 | llvm_object: ?*LlvmObject = null, |
| 8 | | 8 | |
| 9 | files: std.MutliArrayList(File.Entry) = .{}, | 9 | files: std.MultiArrayList(File.Entry) = .{}, |
| 10 | zig_module_index: ?File.Index = null, | 10 | zig_module_index: ?File.Index = null, |
| 11 | linker_defined_index: ?File.Index = null, | 11 | linker_defined_index: ?File.Index = null, |
| 12 | | 12 | |
| ... | @@ -56,14 +56,12 @@ shstrtab_section_index: ?u16 = null, | ... | @@ -56,14 +56,12 @@ shstrtab_section_index: ?u16 = null, |
| 56 | strtab_section_index: ?u16 = null, | 56 | strtab_section_index: ?u16 = null, |
| 57 | | 57 | |
| 58 | symbols: std.ArrayListUnmanaged(Symbol) = .{}, | 58 | symbols: std.ArrayListUnmanaged(Symbol) = .{}, |
| 59 | globals: std.ArrayListUnmanaged(Symbol.Index) = .{}, | 59 | resolver: std.AutoArrayHashMapUnmanaged(u32, Symbol.Index) = .{}, |
| 60 | resolver: std.StringHashMapUnmanaged(u32) = .{}, | | |
| 61 | unresolved: std.AutoArrayHashMapUnmanaged(u32, void) = .{}, | 60 | unresolved: std.AutoArrayHashMapUnmanaged(u32, void) = .{}, |
| 62 | | 61 | |
| 63 | symbols_free_list: std.ArrayListUnmanaged(u32) = .{}, | 62 | symbols_free_list: std.ArrayListUnmanaged(Symbol.Index) = .{}, |
| 64 | globals_free_list: std.ArrayListUnmanaged(u32) = .{}, | | |
| 65 | | 63 | |
| 66 | got_table: TableSection(u32) = .{}, | 64 | got_table: TableSection(Symbol.Index) = .{}, |
| 67 | | 65 | |
| 68 | phdr_table_dirty: bool = false, | 66 | phdr_table_dirty: bool = false, |
| 69 | shdr_table_dirty: bool = false, | 67 | shdr_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. |
| 89 | atoms: std.ArrayListUnmanaged(Atom) = .{}, | 87 | atoms: std.ArrayListUnmanaged(Atom) = .{}, |
| 90 | | 88 | |
| 91 | /// Table of atoms indexed by the symbol index. | | |
| 92 | atom_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) = .{}, |
| 113 | unnamed_const_atoms: UnnamedConstTable = .{}, | 108 | unnamed_const_atoms: UnnamedConstTable = .{}, |
| 114 | | 109 | |
| 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. | | |
| 118 | relocs: RelocTable = .{}, | 111 | relocs: RelocTable = .{}, |
| 119 | | 112 | |
| 120 | const RelocTable = std.AutoHashMapUnmanaged(Atom.Index, std.ArrayListUnmanaged(Atom.Reloc)); | 113 | const RelocTable = std.AutoHashMapUnmanaged(Atom.Index, std.ArrayListUnmanaged(Atom.Reloc)); |
| 121 | const UnnamedConstTable = std.AutoHashMapUnmanaged(Module.Decl.Index, std.ArrayListUnmanaged(Atom.Index)); | 114 | const UnnamedConstTable = std.AutoHashMapUnmanaged(Module.Decl.Index, std.ArrayListUnmanaged(Symbol.Index)); |
| 122 | const LazySymbolTable = std.AutoArrayHashMapUnmanaged(Module.Decl.OptionalIndex, LazySymbolMetadata); | 115 | const LazySymbolTable = std.AutoArrayHashMapUnmanaged(Module.Decl.OptionalIndex, LazySymbolMetadata); |
| 123 | | 116 | |
| 124 | /// When allocating, the ideal_capacity is calculated by | 117 | /// 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(); |
| 145 | | 138 | |
| 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 | }); |
| 151 | | 144 | |
| 152 | self.base.file = file; | | |
| 153 | self.shdr_table_dirty = true; | 145 | self.shdr_table_dirty = true; |
| 154 | | 146 | |
| 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); |
| 250 | | 241 | |
| 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 | } |
| 266 | | 249 | |
| 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); |
| 270 | | 252 | |
| 271 | { | 253 | { |
| ... | @@ -292,13 +274,12 @@ pub fn deinit(self: *Elf) void { | ... | @@ -292,13 +274,12 @@ pub fn deinit(self: *Elf) void { |
| 292 | pub fn getDeclVAddr(self: *Elf, decl_index: Module.Decl.Index, reloc_info: link.File.RelocInfo) !u64 { | 274 | pub 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); |
| 294 | | 276 | |
| 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 | } |
| 853 | | 834 | |
| 854 | fn growAllocSection(self: *Elf, shdr_index: u16, needed_size: u64) !void { | 835 | pub 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 it | 965 | // 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; |
| 988 | | 969 | |
| 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 | }; |
| 994 | | 975 | |
| 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 | }; |
| 1000 | | 981 | |
| ... | @@ -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().?; |
| 2158 | | 2139 | |
| 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 | } |
| 2166 | | 2146 | |
| 2167 | fn shrinkAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64) void { | | |
| 2168 | _ = self; | | |
| 2169 | _ = atom_index; | | |
| 2170 | _ = new_block_size; | | |
| 2171 | } | | |
| 2172 | | | |
| 2173 | fn 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 | | | |
| 2182 | pub 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 | | | |
| 2193 | fn 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 | | | |
| 2313 | pub 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 | | | |
| 2330 | fn 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 | | | |
| 2347 | fn freeUnnamedConsts(self: *Elf, decl_index: Module.Decl.Index) void { | 2147 | fn 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 | } |
| 2374 | | 2174 | |
| 2375 | pub fn getOrCreateAtomForLazySymbol(self: *Elf, sym: link.File.LazySymbol) !Atom.Index { | 2175 | pub 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 flushModule | 2204 | // 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 | } |
| 2398 | | 2208 | |
| 2399 | pub fn getOrCreateAtomForDecl(self: *Elf, decl_index: Module.Decl.Index) !Atom.Index { | 2209 | pub 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 | } |
| 2410 | | 2220 | |
| 2411 | fn getDeclShdrIndex(self: *Elf, decl_index: Module.Decl.Index) u16 { | 2221 | 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,7 +2244,13 @@ fn getDeclShdrIndex(self: *Elf, decl_index: Module.Decl.Index) u16 { |
| 2434 | return shdr_index; | 2244 | return shdr_index; |
| 2435 | } | 2245 | } |
| 2436 | | 2246 | |
| 2437 | fn updateDeclCode(self: *Elf, decl_index: Module.Decl.Index, code: []const u8, stt_bits: u8) !*elf.Elf64_Sym { | 2247 | fn 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); |
| 2446 | | 2262 | |
| 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); | | |
| 2452 | | 2267 | |
| 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; | | |
| 2459 | | 2272 | |
| 2460 | const capacity = atom_ptr.capacity(self); | 2273 | const old_size = atom_ptr.size; |
| 2461 | const need_realloc = code.len > capacity or | 2274 | 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; |
| 2463 | | 2276 | |
| | 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; |
| 2469 | | 2286 | |
| 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 }); |
| 2491 | | 2299 | |
| 2492 | local_sym.st_value = vaddr; | 2300 | sym.value = vaddr; |
| 2493 | local_sym.st_size = code.len; | 2301 | esym.st_value = vaddr; |
| 2494 | | 2302 | |
| 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 | } |
| 2498 | | 2306 | |
| 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; |
| 2502 | | 2310 | |
| 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 | } |
| 2523 | | 2331 | |
| 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 | } |
| 2528 | | 2334 | |
| 2529 | pub fn updateFunc(self: *Elf, mod: *Module, func_index: InternPool.Index, air: Air, liveness: Liveness) !void { | 2335 | pub 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); |
| 2541 | | 2347 | |
| 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); |
| 2545 | | 2351 | |
| 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 | } |
| 2606 | | 2413 | |
| 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); |
| 2609 | | 2416 | |
| 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 | else | 2434 | 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 | }); |
| 2634 | | 2441 | |
| 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 | }; |
| 2643 | | 2450 | |
| 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 | } |
| 2659 | | 2467 | |
| 2660 | fn updateLazySymbolAtom( | 2468 | fn 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.?; |
| 2668 | | 2471 | |
| ... | @@ -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).?; |
| 2682 | | 2485 | |
| 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 | else | 2488 | 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 | }; |
| 2710 | | 2510 | |
| 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 }); |
| 2724 | | 2524 | |
| 2725 | local_sym.st_value = vaddr; | 2525 | local_sym.value = vaddr; |
| 2726 | local_sym.st_size = code.len; | 2526 | local_esym.st_value = vaddr; |
| 2727 | | 2527 | |
| 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); |
| 2730 | | 2530 | |
| 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 | } |
| 2735 | | 2535 | |
| ... | @@ -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).?; |
| 2758 | | 2558 | |
| 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); |
| 2760 | | 2561 | |
| 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); |
| 2787 | | 2591 | |
| 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 }); |
| 2789 | | 2593 | |
| 2790 | try unnamed_consts.append(gpa, atom_index); | 2594 | local_sym.value = vaddr; |
| | 2595 | local_esym.st_value = vaddr; |
| 2791 | | 2596 | |
| 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); |
| 2795 | | 2602 | |
| 2796 | return self.atom(atom_index).symbolIndex().?; | 2603 | return sym_index; |
| 2797 | } | 2604 | } |
| 2798 | | 2605 | |
| 2799 | pub fn updateDeclExports( | 2606 | pub 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; |
| 2816 | | 2623 | |
| 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; | | |
| 2823 | | 2629 | |
| 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)); |
| 2856 | | 2662 | |
| 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 | } |
| 2876 | | 2678 | |
| ... | @@ -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); |
| 2908 | | 2709 | 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, |
| 2913 | | 2714 | .st_info = 0, |
| | 2715 | .st_other = 0, |
| | 2716 | .st_shndx = 0, |
| | 2717 | .st_value = 0, |
| | 2718 | .st_size = 0, |
| | 2719 | }; |
| | 2720 | self.symbols_free_list.append(gpa, sym_index.*) catch {}; |
| 2914 | sym_index.* = 0; | 2721 | sym_index.* = 0; |
| 2915 | } | 2722 | } |
| 2916 | | 2723 | |
| ... | @@ -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 | }; |
| 3376 | | 3183 | |
| 3377 | fn logSymtab(self: Elf) void { | 3184 | pub 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. | | |
| 3390 | pub fn symbol(self: *const Elf, sym_index: u32) *elf.Elf64_Sym { | | |
| 3391 | return &self.locals.items[sym_index]; | | |
| 3392 | } | 3187 | } |
| 3393 | | 3188 | |
| 3394 | /// Returns name of the symbol at sym_index. | 3189 | pub fn addAtom(self: *Elf) !Atom.Index { |
| 3395 | pub 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 | } |
| 3399 | | 3195 | |
| 3400 | /// Returns pointer to the global entry for `name` if one exists. | 3196 | pub fn file(self: *Elf, index: File.Index) ?File { |
| 3401 | pub 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 | } |
| 3405 | | 3204 | |
| 3406 | /// Returns the index of the global entry for `name` if one exists. | 3205 | /// Returns pointer-to-symbol described at sym_index. |
| 3407 | pub fn globalIndex(self: *const Elf, name: []const u8) ?u32 { | 3206 | pub fn symbol(self: *Elf, sym_index: Symbol.Index) *Symbol { |
| 3408 | return self.resolver.get(name); | 3207 | return &self.symbols.items[sym_index]; |
| 3409 | } | 3208 | } |
| 3410 | | 3209 | |
| 3411 | /// Returns global entry at `index`. | 3210 | pub fn addSymbol(self: *Elf) !Symbol.Index { |
| 3412 | pub 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 | } |
| 3416 | | 3226 | |
| 3417 | const GetOrPutGlobalResult = struct { | 3227 | const GetOrPutGlobalResult = struct { |
| 3418 | found_existing: bool, | 3228 | found_existing: bool, |
| 3419 | value_ptr: *u32, | 3229 | index: Symbol.Index, |
| 3420 | }; | 3230 | }; |
| 3421 | | 3231 | |
| 3422 | /// Return pointer to the global entry for `name` if one exists. | 3232 | pub 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. | | |
| 3425 | pub 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; |
| 3436 | | 3240 | } |
| 3437 | pub 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 | } |
| 3441 | | 3246 | |
| 3442 | /// Returns atom if there is an atom referenced by the symbol. | 3247 | pub 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; |
| 3444 | pub 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 | } |
| 3447 | | 3251 | |
| 3448 | fn dumpState(self: *Elf ) std.fmt.Formatter(fmtDumpState) { | 3252 | fn dumpState(self: *Elf) std.fmt.Formatter(fmtDumpState) { |
| 3449 | return .{ .data = self }; | 3253 | return .{ .data = self }; |
| 3450 | } | 3254 | } |
| 3451 | | 3255 | |
| 3452 | fn fmtDumpState(self: *Elf, | 3256 | fn 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; |
| 3457 | | 3264 | |
| | 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 | } |
| 3459 | | 3276 | |
| 3460 | pub 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 | | | |
| 3469 | const default_entry_addr = 0x8000000; | 3277 | const default_entry_addr = 0x8000000; |
| 3470 | | 3278 | |
| 3471 | pub const base_tag: link.File.Tag = .elf; | 3279 | pub const base_tag: link.File.Tag = .elf; |
| ... | @@ -3497,21 +3305,20 @@ const Section = struct { | ... | @@ -3497,21 +3305,20 @@ const Section = struct { |
| 3497 | | 3305 | |
| 3498 | const LazySymbolMetadata = struct { | 3306 | const 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 | }; |
| 3505 | | 3313 | |
| 3506 | const DeclMetadata = struct { | 3314 | const 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) = .{}, |
| 3511 | | 3318 | |
| 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"); |
| 3543 | const Cache = std.Build.Cache; | 3350 | const Cache = std.Build.Cache; |
| 3544 | const Compilation = @import("../Compilation.zig"); | 3351 | const Compilation = @import("../Compilation.zig"); |
| 3545 | const Dwarf = @import("Dwarf.zig"); | 3352 | const Dwarf = @import("Dwarf.zig"); |
| 3546 | const File = @import("Elf/File.zig"); | 3353 | const File = @import("Elf/file.zig").File; |
| 3547 | const LinkerDefined = @import("Elf/LinkerDefined.zig"); | 3354 | const LinkerDefined = @import("Elf/LinkerDefined.zig"); |
| 3548 | const Liveness = @import("../Liveness.zig"); | 3355 | const Liveness = @import("../Liveness.zig"); |
| 3549 | const LlvmObject = @import("../codegen/llvm.zig").Object; | 3356 | const LlvmObject = @import("../codegen/llvm.zig").Object; |
| 3550 | const Module = @import("../Module.zig"); | 3357 | const Module = @import("../Module.zig"); |
| 3551 | const InternPool = @import("../InternPool.zig"); | 3358 | const InternPool = @import("../InternPool.zig"); |
| 3552 | const Package = @import("../Package.zig"); | 3359 | const Package = @import("../Package.zig"); |
| | 3360 | const Symbol = @import("Elf/Symbol.zig"); |
| 3553 | const StringTable = @import("strtab.zig").StringTable; | 3361 | const StringTable = @import("strtab.zig").StringTable; |
| 3554 | const TableSection = @import("table_section.zig").TableSection; | 3362 | const TableSection = @import("table_section.zig").TableSection; |
| 3555 | const Type = @import("../type.zig").Type; | 3363 | const Type = @import("../type.zig").Type; |