| ... | ... | @@ -1,4 +1,4 @@ |
| 1 | | base: File, |
| 1 | base: link.File, |
| 2 | 2 | dwarf: ?Dwarf = null, |
| 3 | 3 | |
| 4 | 4 | ptr_width: PtrWidth, |
| ... | ... | @@ -6,7 +6,7 @@ ptr_width: PtrWidth, |
| 6 | 6 | /// If this is not null, an object file is created by LLVM and linked with LLD afterwards. |
| 7 | 7 | llvm_object: ?*LlvmObject = null, |
| 8 | 8 | |
| 9 | | files: std.MutliArrayList(File.Entry) = .{}, |
| 9 | files: std.MultiArrayList(File.Entry) = .{}, |
| 10 | 10 | zig_module_index: ?File.Index = null, |
| 11 | 11 | linker_defined_index: ?File.Index = null, |
| 12 | 12 | |
| ... | ... | @@ -56,14 +56,12 @@ shstrtab_section_index: ?u16 = null, |
| 56 | 56 | strtab_section_index: ?u16 = null, |
| 57 | 57 | |
| 58 | 58 | symbols: std.ArrayListUnmanaged(Symbol) = .{}, |
| 59 | | globals: std.ArrayListUnmanaged(Symbol.Index) = .{}, |
| 60 | | resolver: std.StringHashMapUnmanaged(u32) = .{}, |
| 59 | resolver: std.AutoArrayHashMapUnmanaged(u32, Symbol.Index) = .{}, |
| 61 | 60 | unresolved: std.AutoArrayHashMapUnmanaged(u32, void) = .{}, |
| 62 | 61 | |
| 63 | | symbols_free_list: std.ArrayListUnmanaged(u32) = .{}, |
| 64 | | globals_free_list: std.ArrayListUnmanaged(u32) = .{}, |
| 62 | symbols_free_list: std.ArrayListUnmanaged(Symbol.Index) = .{}, |
| 65 | 63 | |
| 66 | | got_table: TableSection(u32) = .{}, |
| 64 | got_table: TableSection(Symbol.Index) = .{}, |
| 67 | 65 | |
| 68 | 66 | phdr_table_dirty: bool = false, |
| 69 | 67 | shdr_table_dirty: bool = false, |
| ... | ... | @@ -88,9 +86,6 @@ decls: std.AutoHashMapUnmanaged(Module.Decl.Index, DeclMetadata) = .{}, |
| 88 | 86 | /// List of atoms that are owned directly by the linker. |
| 89 | 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 | 89 | /// Table of unnamed constants associated with a parent `Decl`. |
| 95 | 90 | /// We store them here so that we can free the constants whenever the `Decl` |
| 96 | 91 | /// needs updating or is freed. |
| ... | ... | @@ -113,12 +108,10 @@ atom_by_index_table: std.AutoHashMapUnmanaged(u32, Atom.Index) = .{}, |
| 113 | 108 | unnamed_const_atoms: UnnamedConstTable = .{}, |
| 114 | 109 | |
| 115 | 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 | 111 | relocs: RelocTable = .{}, |
| 119 | 112 | |
| 120 | 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 | 115 | const LazySymbolTable = std.AutoArrayHashMapUnmanaged(Module.Decl.OptionalIndex, LazySymbolMetadata); |
| 123 | 116 | |
| 124 | 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 | 136 | const self = try createEmpty(allocator, options); |
| 144 | 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 | 140 | .truncate = false, |
| 148 | 141 | .read = true, |
| 149 | 142 | .mode = link.determineMode(options), |
| 150 | 143 | }); |
| 151 | 144 | |
| 152 | | self.base.file = file; |
| 153 | 145 | self.shdr_table_dirty = true; |
| 154 | 146 | |
| 155 | 147 | // Index 0 is always a null symbol. |
| ... | ... | @@ -243,19 +235,10 @@ pub fn deinit(self: *Elf) void { |
| 243 | 235 | self.strtab.deinit(gpa); |
| 244 | 236 | self.symbols.deinit(gpa); |
| 245 | 237 | self.symbols_free_list.deinit(gpa); |
| 246 | | self.globals.deinit(gpa); |
| 247 | | self.globals_free_list.deinit(gpa); |
| 248 | 238 | self.got_table.deinit(gpa); |
| 239 | self.resolver.deinit(gpa); |
| 249 | 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 | 243 | var it = self.decls.iterator(); |
| 261 | 244 | while (it.next()) |entry| { |
| ... | ... | @@ -265,7 +248,6 @@ pub fn deinit(self: *Elf) void { |
| 265 | 248 | } |
| 266 | 249 | |
| 267 | 250 | self.atoms.deinit(gpa); |
| 268 | | self.atom_by_index_table.deinit(gpa); |
| 269 | 251 | self.lazy_syms.deinit(gpa); |
| 270 | 252 | |
| 271 | 253 | { |
| ... | ... | @@ -292,13 +274,12 @@ pub fn deinit(self: *Elf) void { |
| 292 | 274 | pub fn getDeclVAddr(self: *Elf, decl_index: Module.Decl.Index, reloc_info: link.File.RelocInfo) !u64 { |
| 293 | 275 | assert(self.llvm_object == null); |
| 294 | 276 | |
| 295 | | const this_atom_index = try self.getOrCreateAtomForDecl(decl_index); |
| 296 | | const this_atom = self.atom(this_atom_index); |
| 297 | | const target = this_atom.symbolIndex().?; |
| 298 | | const vaddr = this_atom.symbol(self).st_value; |
| 299 | | const atom_index = self.atomIndexForSymbol(reloc_info.parent_atom_index).?; |
| 300 | | try Atom.addRelocation(self, atom_index, .{ |
| 301 | | .target = target, |
| 277 | const this_sym_index = try self.getOrCreateMetadataForDecl(decl_index); |
| 278 | const this_sym = self.symbol(this_sym_index); |
| 279 | const vaddr = this_sym.value; |
| 280 | const parent_atom_index = self.symbol(reloc_info.parent_atom_index).atom_index; |
| 281 | try Atom.addRelocation(self, parent_atom_index, .{ |
| 282 | .target = this_sym, |
| 302 | 283 | .offset = reloc_info.offset, |
| 303 | 284 | .addend = reloc_info.addend, |
| 304 | 285 | .prev_vaddr = vaddr, |
| ... | ... | @@ -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 | 836 | // TODO Also detect virtual address collisions. |
| 856 | 837 | const shdr = &self.sections.items(.shdr)[shdr_index]; |
| 857 | 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 | 844 | const new_offset = self.findFreeSpace(needed_size, self.page_size); |
| 864 | 845 | const existing_size = if (maybe_last_atom_index) |last_atom_index| blk: { |
| 865 | 846 | const last = self.atom(last_atom_index); |
| 866 | | const sym = last.symbol(self); |
| 867 | | break :blk (sym.st_value + sym.st_size) - phdr.p_vaddr; |
| 847 | break :blk (last.value + last.size) - phdr.p_vaddr; |
| 868 | 848 | } else if (shdr_index == self.got_section_index.?) blk: { |
| 869 | 849 | break :blk shdr.sh_size; |
| 870 | 850 | } else 0; |
| ... | ... | @@ -985,6 +965,7 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 985 | 965 | // TODO This linker code currently assumes there is only 1 compilation unit and it |
| 986 | 966 | // corresponds to the Zig source code. |
| 987 | 967 | const module = self.base.options.module orelse return error.LinkingWithoutZigSourceUnimplemented; |
| 968 | _ = module; |
| 988 | 969 | |
| 989 | 970 | self.zig_module_index = blk: { |
| 990 | 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 | 973 | break :blk index; |
| 993 | 974 | }; |
| 994 | 975 | |
| 995 | | self.linker_defined = blk: { |
| 976 | self.linker_defined_index = blk: { |
| 996 | 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 | 979 | break :blk index; |
| 999 | 980 | }; |
| 1000 | 981 | |
| ... | ... | @@ -2157,193 +2138,12 @@ fn freeAtom(self: *Elf, atom_index: Atom.Index) void { |
| 2157 | 2138 | const sym_index = atom_ptr.symbolIndex().?; |
| 2158 | 2139 | |
| 2159 | 2140 | log.debug("adding %{d} to local symbols free list", .{sym_index}); |
| 2160 | | self.locals_free_list.append(gpa, sym_index) catch {}; |
| 2161 | | self.locals.items[sym_index] = null_sym; |
| 2162 | | _ = self.atom_by_index_table.remove(sym_index); |
| 2141 | self.symbols_free_list.append(gpa, sym_index) catch {}; |
| 2142 | self.symbols.items[sym_index] = .{}; |
| 2163 | 2143 | atom_ptr.sym_index = 0; |
| 2164 | 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 | 2147 | fn freeUnnamedConsts(self: *Elf, decl_index: Module.Decl.Index) void { |
| 2348 | 2148 | const unnamed_consts = self.unnamed_const_atoms.getPtr(decl_index) orelse return; |
| 2349 | 2149 | for (unnamed_consts.items) |atom_index| { |
| ... | ... | @@ -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 | 2176 | const mod = self.base.options.module.?; |
| 2377 | 2177 | const gop = try self.lazy_syms.getOrPut(self.base.allocator, sym.getDecl(mod)); |
| 2378 | 2178 | errdefer _ = if (!gop.found_existing) self.lazy_syms.pop(); |
| 2379 | 2179 | if (!gop.found_existing) gop.value_ptr.* = .{}; |
| 2380 | | const metadata: struct { atom: *Atom.Index, state: *LazySymbolMetadata.State } = switch (sym.kind) { |
| 2381 | | .code => .{ .atom = &gop.value_ptr.text_atom, .state = &gop.value_ptr.text_state }, |
| 2382 | | .const_data => .{ .atom = &gop.value_ptr.rodata_atom, .state = &gop.value_ptr.rodata_state }, |
| 2180 | const metadata: struct { |
| 2181 | symbol_index: *Symbol.Index, |
| 2182 | state: *LazySymbolMetadata.State, |
| 2183 | } = switch (sym.kind) { |
| 2184 | .code => .{ |
| 2185 | .symbol_index = &gop.value_ptr.text_symbol_index, |
| 2186 | .state = &gop.value_ptr.text_state, |
| 2187 | }, |
| 2188 | .const_data => .{ |
| 2189 | .symbol_index = &gop.value_ptr.rodata_symbol_index, |
| 2190 | .state = &gop.value_ptr.rodata_state, |
| 2191 | }, |
| 2383 | 2192 | }; |
| 2193 | const zig_module = self.file(self.zig_module_index.?).?.zig_module; |
| 2384 | 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 | 2199 | .pending_flush => return metadata.atom.*, |
| 2387 | 2200 | .flushed => {}, |
| 2388 | 2201 | } |
| 2389 | 2202 | metadata.state.* = .pending_flush; |
| 2390 | | const atom_index = metadata.atom.*; |
| 2203 | const symbol_index = metadata.symbol_index.*; |
| 2391 | 2204 | // anyerror needs to be deferred until flushModule |
| 2392 | | if (sym.getDecl(mod) != .none) try self.updateLazySymbolAtom(sym, atom_index, switch (sym.kind) { |
| 2393 | | .code => self.text_section_index.?, |
| 2394 | | .const_data => self.rodata_section_index.?, |
| 2395 | | }); |
| 2396 | | return atom_index; |
| 2205 | if (sym.getDecl(mod) != .none) try self.updateLazySymbol(sym, symbol_index); |
| 2206 | return symbol_index; |
| 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 | 2210 | const gop = try self.decls.getOrPut(self.base.allocator, decl_index); |
| 2401 | 2211 | if (!gop.found_existing) { |
| 2212 | const zig_module = self.file(self.zig_module_index.?).?.zig_module; |
| 2402 | 2213 | gop.value_ptr.* = .{ |
| 2403 | | .atom = try self.createAtom(), |
| 2404 | | .shdr = self.getDeclShdrIndex(decl_index), |
| 2214 | .symbol_index = try zig_module.createAtom(self.getDeclShdrIndex(decl_index), self), |
| 2405 | 2215 | .exports = .{}, |
| 2406 | 2216 | }; |
| 2407 | 2217 | } |
| 2408 | | return gop.value_ptr.atom; |
| 2218 | return gop.value_ptr.symbol_index; |
| 2409 | 2219 | } |
| 2410 | 2220 | |
| 2411 | 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 | 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 | 2254 | const gpa = self.base.allocator; |
| 2439 | 2255 | const mod = self.base.options.module.?; |
| 2440 | 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 | 2260 | log.debug("updateDeclCode {s}{*}", .{ decl_name, decl }); |
| 2445 | 2261 | const required_alignment = decl.getAlignment(mod); |
| 2446 | 2262 | |
| 2447 | | const decl_metadata = self.decls.get(decl_index).?; |
| 2448 | | const atom_index = decl_metadata.atom; |
| 2449 | | const atom_ptr = self.atom(atom_index); |
| 2450 | | const local_sym_index = atom_ptr.symbolIndex().?; |
| 2451 | | const local_sym = atom_ptr.symbol(self); |
| 2263 | const sym = self.symbol(sym_index); |
| 2264 | const esym = sym.sourceSymbol(self); |
| 2265 | const atom_ptr = sym.atom(self).?; |
| 2266 | const shdr_index = sym.output_section_index; |
| 2452 | 2267 | |
| 2453 | | const shdr_index = decl_metadata.shdr; |
| 2454 | | if (atom_ptr.symbol(self).st_size != 0 and self.base.child_pid == null) { |
| 2455 | | local_sym.st_name = try self.strtab.insert(gpa, decl_name); |
| 2456 | | local_sym.st_info = (elf.STB_LOCAL << 4) | stt_bits; |
| 2457 | | local_sym.st_other = 0; |
| 2458 | | local_sym.st_shndx = shdr_index; |
| 2268 | sym.name_offset = try self.strtab.insert(gpa, decl_name); |
| 2269 | esym.st_name = sym.name_offset; |
| 2270 | esym.st_info |= stt_bits; |
| 2271 | esym.st_size = code.len; |
| 2459 | 2272 | |
| 2460 | | const capacity = atom_ptr.capacity(self); |
| 2461 | | const need_realloc = code.len > capacity or |
| 2462 | | !mem.isAlignedGeneric(u64, local_sym.st_value, required_alignment); |
| 2273 | const old_size = atom_ptr.size; |
| 2274 | atom_ptr.alignment = math.log2_int(u64, required_alignment); |
| 2275 | atom_ptr.size = code.len; |
| 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 | 2280 | if (need_realloc) { |
| 2465 | | const vaddr = try self.growAtom(atom_index, code.len, required_alignment); |
| 2466 | | log.debug("growing {s} from 0x{x} to 0x{x}", .{ decl_name, local_sym.st_value, vaddr }); |
| 2467 | | if (vaddr != local_sym.st_value) { |
| 2468 | | local_sym.st_value = vaddr; |
| 2281 | const vaddr = try atom_ptr.grow(self); |
| 2282 | log.debug("growing {s} from 0x{x} to 0x{x}", .{ decl_name, sym.value, vaddr }); |
| 2283 | if (vaddr != sym.value) { |
| 2284 | sym.value = vaddr; |
| 2285 | esym.st_value = vaddr; |
| 2469 | 2286 | |
| 2470 | 2287 | log.debug(" (writing new offset table entry)", .{}); |
| 2471 | | const got_entry_index = self.got_table.lookup.get(local_sym_index).?; |
| 2472 | | self.got_table.entries.items[got_entry_index] = local_sym_index; |
| 2288 | const got_entry_index = self.got_table.lookup.get(sym_index).?; |
| 2289 | self.got_table.entries.items[got_entry_index] = sym_index; |
| 2473 | 2290 | try self.writeOffsetTableEntry(got_entry_index); |
| 2474 | 2291 | } |
| 2475 | | } else if (code.len < local_sym.st_size) { |
| 2476 | | self.shrinkAtom(atom_index, code.len); |
| 2292 | } else if (code.len < old_size) { |
| 2293 | atom_ptr.shrink(self); |
| 2477 | 2294 | } |
| 2478 | | local_sym.st_size = code.len; |
| 2479 | 2295 | } else { |
| 2480 | | local_sym.* = .{ |
| 2481 | | .st_name = try self.strtab.insert(gpa, decl_name), |
| 2482 | | .st_info = (elf.STB_LOCAL << 4) | stt_bits, |
| 2483 | | .st_other = 0, |
| 2484 | | .st_shndx = shdr_index, |
| 2485 | | .st_value = 0, |
| 2486 | | .st_size = 0, |
| 2487 | | }; |
| 2488 | | const vaddr = try self.allocateAtom(atom_index, code.len, required_alignment); |
| 2489 | | errdefer self.freeAtom(atom_index); |
| 2296 | const vaddr = try atom_ptr.allocate(self); |
| 2297 | errdefer self.freeAtom(atom_ptr); |
| 2490 | 2298 | log.debug("allocated text block for {s} at 0x{x}", .{ decl_name, vaddr }); |
| 2491 | 2299 | |
| 2492 | | local_sym.st_value = vaddr; |
| 2493 | | local_sym.st_size = code.len; |
| 2300 | sym.value = vaddr; |
| 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 | 2304 | try self.writeOffsetTableEntry(got_entry_index); |
| 2497 | 2305 | } |
| 2498 | 2306 | |
| 2499 | 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 | 2309 | const file_offset = self.sections.items(.shdr)[shdr_index].sh_offset + section_offset; |
| 2502 | 2310 | |
| 2503 | 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 | 2316 | .iov_len = code.len, |
| 2509 | 2317 | }}; |
| 2510 | 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 | 2320 | .iov_len = code.len, |
| 2513 | 2321 | }}; |
| 2514 | 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 | 2330 | } |
| 2523 | 2331 | |
| 2524 | 2332 | try self.base.file.?.pwriteAll(code, file_offset); |
| 2525 | | |
| 2526 | | return local_sym; |
| 2527 | 2333 | } |
| 2528 | 2334 | |
| 2529 | 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 | 2345 | const decl_index = func.owner_decl; |
| 2540 | 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 | 2349 | self.freeUnnamedConsts(decl_index); |
| 2544 | | Atom.freeRelocations(self, atom_index); |
| 2350 | Atom.freeRelocations(self, self.symbol(sym_index).atom_index); |
| 2545 | 2351 | |
| 2546 | 2352 | var code_buffer = std.ArrayList(u8).init(self.base.allocator); |
| 2547 | 2353 | defer code_buffer.deinit(); |
| ... | ... | @@ -2564,13 +2370,14 @@ pub fn updateFunc(self: *Elf, mod: *Module, func_index: InternPool.Index, air: A |
| 2564 | 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 | 2374 | if (decl_state) |*ds| { |
| 2375 | const sym = self.symbol(sym_index); |
| 2569 | 2376 | try self.dwarf.?.commitDeclState( |
| 2570 | 2377 | mod, |
| 2571 | 2378 | decl_index, |
| 2572 | | local_sym.st_value, |
| 2573 | | local_sym.st_size, |
| 2379 | sym.value, |
| 2380 | sym.atom(self).?.size, |
| 2574 | 2381 | ds, |
| 2575 | 2382 | ); |
| 2576 | 2383 | } |
| ... | ... | @@ -2604,8 +2411,8 @@ pub fn updateDecl( |
| 2604 | 2411 | } |
| 2605 | 2412 | } |
| 2606 | 2413 | |
| 2607 | | const atom_index = try self.getOrCreateAtomForDecl(decl_index); |
| 2608 | | Atom.freeRelocations(self, atom_index); |
| 2414 | const sym_index = try self.getOrCreateMetadataForDecl(decl_index); |
| 2415 | Atom.freeRelocations(self, self.symbol(sym_index).atom_index); |
| 2609 | 2416 | |
| 2610 | 2417 | var code_buffer = std.ArrayList(u8).init(self.base.allocator); |
| 2611 | 2418 | defer code_buffer.deinit(); |
| ... | ... | @@ -2622,14 +2429,14 @@ pub fn updateDecl( |
| 2622 | 2429 | }, &code_buffer, .{ |
| 2623 | 2430 | .dwarf = ds, |
| 2624 | 2431 | }, .{ |
| 2625 | | .parent_atom_index = self.atom(atom_index).symbolIndex().?, |
| 2432 | .parent_atom_index = sym_index, |
| 2626 | 2433 | }) |
| 2627 | 2434 | else |
| 2628 | 2435 | try codegen.generateSymbol(&self.base, decl.srcLoc(mod), .{ |
| 2629 | 2436 | .ty = decl.ty, |
| 2630 | 2437 | .val = decl_val, |
| 2631 | 2438 | }, &code_buffer, .none, .{ |
| 2632 | | .parent_atom_index = self.atom(atom_index).symbolIndex().?, |
| 2439 | .parent_atom_index = sym_index, |
| 2633 | 2440 | }); |
| 2634 | 2441 | |
| 2635 | 2442 | const code = switch (res) { |
| ... | ... | @@ -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 | 2452 | if (decl_state) |*ds| { |
| 2453 | const sym = self.symbol(sym_index); |
| 2646 | 2454 | try self.dwarf.?.commitDeclState( |
| 2647 | 2455 | mod, |
| 2648 | 2456 | decl_index, |
| 2649 | | local_sym.st_value, |
| 2650 | | local_sym.st_size, |
| 2457 | sym.value, |
| 2458 | sym.atom(self).?.size, |
| 2651 | 2459 | ds, |
| 2652 | 2460 | ); |
| 2653 | 2461 | } |
| ... | ... | @@ -2657,12 +2465,7 @@ pub fn updateDecl( |
| 2657 | 2465 | return self.updateDeclExports(mod, decl_index, mod.getDeclExports(decl_index)); |
| 2658 | 2466 | } |
| 2659 | 2467 | |
| 2660 | | fn updateLazySymbolAtom( |
| 2661 | | self: *Elf, |
| 2662 | | sym: link.File.LazySymbol, |
| 2663 | | atom_index: Atom.Index, |
| 2664 | | shdr_index: u16, |
| 2665 | | ) !void { |
| 2468 | fn updateLazySymbol(self: *Elf, sym: link.File.LazySymbol, symbol_index: Symbol.Index) !void { |
| 2666 | 2469 | const gpa = self.base.allocator; |
| 2667 | 2470 | const mod = self.base.options.module.?; |
| 2668 | 2471 | |
| ... | ... | @@ -2680,9 +2483,6 @@ fn updateLazySymbolAtom( |
| 2680 | 2483 | }; |
| 2681 | 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 | 2486 | const src = if (sym.ty.getOwnerDeclOrNull(mod)) |owner_decl| |
| 2687 | 2487 | mod.declPtr(owner_decl).srcLoc(mod) |
| 2688 | 2488 | else |
| ... | ... | @@ -2698,7 +2498,7 @@ fn updateLazySymbolAtom( |
| 2698 | 2498 | &required_alignment, |
| 2699 | 2499 | &code_buffer, |
| 2700 | 2500 | .none, |
| 2701 | | .{ .parent_atom_index = local_sym_index }, |
| 2501 | .{ .parent_atom_index = symbol_index }, |
| 2702 | 2502 | ); |
| 2703 | 2503 | const code = switch (res) { |
| 2704 | 2504 | .ok => code_buffer.items, |
| ... | ... | @@ -2708,28 +2508,28 @@ fn updateLazySymbolAtom( |
| 2708 | 2508 | }, |
| 2709 | 2509 | }; |
| 2710 | 2510 | |
| 2711 | | const phdr_index = self.sections.items(.phdr_index)[shdr_index]; |
| 2712 | | const local_sym = atom_ptr.symbol(self); |
| 2713 | | local_sym.* = .{ |
| 2714 | | .st_name = name_str_index, |
| 2715 | | .st_info = (elf.STB_LOCAL << 4) | elf.STT_OBJECT, |
| 2716 | | .st_other = 0, |
| 2717 | | .st_shndx = shdr_index, |
| 2718 | | .st_value = 0, |
| 2719 | | .st_size = 0, |
| 2720 | | }; |
| 2721 | | const vaddr = try self.allocateAtom(atom_index, code.len, required_alignment); |
| 2722 | | errdefer self.freeAtom(atom_index); |
| 2511 | const local_sym = self.symbol(symbol_index); |
| 2512 | const phdr_index = self.sections.items(.phdr_index)[local_sym.output_section_index]; |
| 2513 | local_sym.name_offset = name_str_index; |
| 2514 | const local_esym = local_sym.sourceSymbol(self); |
| 2515 | local_esym.st_name = name_str_index; |
| 2516 | local_esym.st_info |= elf.STT_OBJECT; |
| 2517 | local_esym.st_size = code.len; |
| 2518 | const atom_ptr = local_sym.atom(self).?; |
| 2519 | atom_ptr.alignment = math.log2_int(u64, required_alignment); |
| 2520 | atom_ptr.size = code.len; |
| 2521 | const vaddr = try atom_ptr.allocate(self); |
| 2522 | errdefer self.freeAtom(atom_ptr); |
| 2723 | 2523 | log.debug("allocated text block for {s} at 0x{x}", .{ name, vaddr }); |
| 2724 | 2524 | |
| 2725 | | local_sym.st_value = vaddr; |
| 2726 | | local_sym.st_size = code.len; |
| 2525 | local_sym.value = vaddr; |
| 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 | 2529 | try self.writeOffsetTableEntry(got_entry_index); |
| 2730 | 2530 | |
| 2731 | 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 | 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 | 2556 | }; |
| 2757 | 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 | 2562 | const res = try codegen.generateSymbol(&self.base, decl.srcLoc(mod), typed_value, &code_buffer, .{ |
| 2762 | 2563 | .none = {}, |
| 2763 | 2564 | }, .{ |
| 2764 | | .parent_atom_index = self.atom(atom_index).symbolIndex().?, |
| 2565 | .parent_atom_index = sym_index, |
| 2765 | 2566 | }); |
| 2766 | 2567 | const code = switch (res) { |
| 2767 | 2568 | .ok => code_buffer.items, |
| ... | ... | @@ -2776,24 +2577,30 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module |
| 2776 | 2577 | const required_alignment = typed_value.ty.abiAlignment(mod); |
| 2777 | 2578 | const shdr_index = self.rodata_section_index.?; |
| 2778 | 2579 | const phdr_index = self.sections.items(.phdr_index)[shdr_index]; |
| 2779 | | const local_sym = self.atom(atom_index).symbol(self); |
| 2780 | | local_sym.st_name = name_str_index; |
| 2781 | | local_sym.st_info = (elf.STB_LOCAL << 4) | elf.STT_OBJECT; |
| 2782 | | local_sym.st_other = 0; |
| 2783 | | local_sym.st_shndx = shdr_index; |
| 2784 | | local_sym.st_size = code.len; |
| 2785 | | local_sym.st_value = try self.allocateAtom(atom_index, code.len, required_alignment); |
| 2786 | | errdefer self.freeAtom(atom_index); |
| 2580 | const local_sym = self.symbol(sym_index); |
| 2581 | local_sym.name_offset = name_str_index; |
| 2582 | const local_esym = local_sym.sourceSymbol(self); |
| 2583 | local_esym.st_name = name_str_index; |
| 2584 | local_esym.st_info |= elf.STT_OBJECT; |
| 2585 | local_esym.st_size = code.len; |
| 2586 | const atom_ptr = local_sym.atom(self).?; |
| 2587 | atom_ptr.alignment = math.log2_int(u64, required_alignment); |
| 2588 | atom_ptr.size = code.len; |
| 2589 | const vaddr = try atom_ptr.allocateAtom(self); |
| 2590 | errdefer self.freeAtom(atom_ptr); |
| 2787 | 2591 | |
| 2788 | 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 | 2600 | const file_offset = self.sections.items(.shdr)[shdr_index].sh_offset + section_offset; |
| 2794 | 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 | 2606 | pub fn updateDeclExports( |
| ... | ... | @@ -2815,11 +2622,10 @@ pub fn updateDeclExports( |
| 2815 | 2622 | const gpa = self.base.allocator; |
| 2816 | 2623 | |
| 2817 | 2624 | const decl = mod.declPtr(decl_index); |
| 2818 | | const atom_index = try self.getOrCreateAtomForDecl(decl_index); |
| 2819 | | const atom_ptr = self.atom(atom_index); |
| 2820 | | const decl_sym = atom_ptr.symbol(self); |
| 2625 | const decl_sym_index = try self.getOrCreateMetadataForDecl(decl_index); |
| 2626 | const decl_sym = self.symbol(decl_sym_index); |
| 2627 | const decl_esym = symbol.sourceSymbol(self); |
| 2821 | 2628 | const decl_metadata = self.decls.getPtr(decl_index).?; |
| 2822 | | const shdr_index = decl_metadata.shdr; |
| 2823 | 2629 | |
| 2824 | 2630 | for (exports) |exp| { |
| 2825 | 2631 | const exp_name = mod.intern_pool.stringToSlice(exp.opts.name); |
| ... | ... | @@ -2838,7 +2644,7 @@ pub fn updateDeclExports( |
| 2838 | 2644 | .Strong => blk: { |
| 2839 | 2645 | const entry_name = self.base.options.entry orelse "_start"; |
| 2840 | 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 | 2649 | break :blk elf.STB_GLOBAL; |
| 2844 | 2650 | }, |
| ... | ... | @@ -2855,22 +2661,18 @@ pub fn updateDeclExports( |
| 2855 | 2661 | const stt_bits: u8 = @as(u4, @truncate(decl_sym.st_info)); |
| 2856 | 2662 | |
| 2857 | 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 | 2666 | try decl_metadata.exports.append(gpa, sym_index); |
| 2860 | 2667 | break :blk sym_index; |
| 2861 | 2668 | }; |
| 2862 | 2669 | const sym = self.symbol(sym_index); |
| 2863 | | sym.* = .{ |
| 2864 | | .st_name = try self.strtab.insert(gpa, exp_name), |
| 2865 | | .st_info = (stb_bits << 4) | stt_bits, |
| 2866 | | .st_other = 0, |
| 2867 | | .st_shndx = shdr_index, |
| 2868 | | .st_value = decl_sym.st_value, |
| 2869 | | .st_size = decl_sym.st_size, |
| 2870 | | }; |
| 2871 | | const sym_name = self.symbolName(sym_index); |
| 2872 | | const gop = try self.getOrPutGlobal(sym_name); |
| 2873 | | gop.value_ptr.* = sym_index; |
| 2670 | sym.value = decl_sym.value; |
| 2671 | sym.atom_index = decl_sym.atom_index; |
| 2672 | sym.output_section_index = decl_sym.output_section_index; |
| 2673 | const esym = sym.sourceSymbol(self); |
| 2674 | esym.* = decl_esym.*; |
| 2675 | esym.st_info = (stb_bits << 4) | stt_bits; |
| 2874 | 2676 | } |
| 2875 | 2677 | } |
| 2876 | 2678 | |
| ... | ... | @@ -2901,16 +2703,21 @@ pub fn deleteDeclExport( |
| 2901 | 2703 | const mod = self.base.options.module.?; |
| 2902 | 2704 | const exp_name = mod.intern_pool.stringToSlice(name); |
| 2903 | 2705 | const sym_index = metadata.@"export"(self, exp_name) orelse return; |
| 2904 | | const sym = self.symbol(sym_index.*); |
| 2905 | 2706 | log.debug("deleting export '{s}'", .{exp_name}); |
| 2906 | | sym.* = null_sym; |
| 2907 | | self.locals_free_list.append(gpa, sym_index.*) catch {}; |
| 2908 | | |
| 2909 | | if (self.resolver.fetchRemove(exp_name)) |entry| { |
| 2910 | | self.globals_free_list.append(gpa, entry.value) catch {}; |
| 2911 | | self.globals.items[entry.value] = 0; |
| 2912 | | } |
| 2913 | | |
| 2707 | const sym = self.symbol(sym_index.*); |
| 2708 | const esym = sym.sourceSymbol(self); |
| 2709 | assert(self.resolver.fetchSwapRemove(sym.name_offset) != null); // TODO don't delete it if it's not dominant |
| 2710 | sym.* = .{}; |
| 2711 | // TODO free list for esym! |
| 2712 | esym.* = .{ |
| 2713 | .st_name = 0, |
| 2714 | .st_info = 0, |
| 2715 | .st_other = 0, |
| 2716 | .st_shndx = 0, |
| 2717 | .st_value = 0, |
| 2718 | .st_size = 0, |
| 2719 | }; |
| 2720 | self.symbols_free_list.append(gpa, sym_index.*) catch {}; |
| 2914 | 2721 | sym_index.* = 0; |
| 2915 | 2722 | } |
| 2916 | 2723 | |
| ... | ... | @@ -2971,7 +2778,7 @@ fn writeOffsetTableEntry(self: *Elf, index: @TypeOf(self.got_table).Index) !void |
| 2971 | 2778 | const phdr = &self.program_headers.items[self.phdr_got_index.?]; |
| 2972 | 2779 | const vaddr = phdr.p_vaddr + @as(u64, entry_size) * index; |
| 2973 | 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 | 2782 | switch (entry_size) { |
| 2976 | 2783 | 2 => { |
| 2977 | 2784 | var buf: [2]u8 = undefined; |
| ... | ... | @@ -3374,98 +3181,99 @@ const CsuObjects = struct { |
| 3374 | 3181 | } |
| 3375 | 3182 | }; |
| 3376 | 3183 | |
| 3377 | | fn logSymtab(self: Elf) void { |
| 3378 | | log.debug("locals:", .{}); |
| 3379 | | for (self.locals.items, 0..) |sym, id| { |
| 3380 | | log.debug(" {d}: {?s}: @{x} in {d}", .{ id, self.strtab.get(sym.st_name), sym.st_value, sym.st_shndx }); |
| 3381 | | } |
| 3382 | | log.debug("globals:", .{}); |
| 3383 | | for (self.globals.items, 0..) |glob, id| { |
| 3384 | | const sym = self.symbol(glob); |
| 3385 | | log.debug(" {d}: {?s}: @{x} in {d}", .{ id, self.strtab.get(sym.st_name), sym.st_value, sym.st_shndx }); |
| 3386 | | } |
| 3387 | | } |
| 3388 | | |
| 3389 | | /// Returns pointer-to-symbol described at sym_index. |
| 3390 | | pub fn symbol(self: *const Elf, sym_index: u32) *elf.Elf64_Sym { |
| 3391 | | return &self.locals.items[sym_index]; |
| 3184 | pub fn atom(self: *Elf, atom_index: Atom.Index) *Atom { |
| 3185 | assert(atom_index < self.atoms.items.len); |
| 3186 | return &self.atoms.items[atom_index]; |
| 3392 | 3187 | } |
| 3393 | 3188 | |
| 3394 | | /// Returns name of the symbol at sym_index. |
| 3395 | | pub fn symbolName(self: *const Elf, sym_index: u32) []const u8 { |
| 3396 | | const sym = self.locals.items[sym_index]; |
| 3397 | | return self.strtab.get(sym.st_name).?; |
| 3189 | pub fn addAtom(self: *Elf) !Atom.Index { |
| 3190 | const index = @as(Atom.Index, @intCast(self.atoms.items.len)); |
| 3191 | const atom_ptr = try self.atoms.addOne(self.base.allocator); |
| 3192 | atom_ptr.* = .{ .atom_index = index }; |
| 3193 | return index; |
| 3398 | 3194 | } |
| 3399 | 3195 | |
| 3400 | | /// Returns pointer to the global entry for `name` if one exists. |
| 3401 | | pub fn global(self: *const Elf, name: []const u8) ?*u32 { |
| 3402 | | const global_index = self.resolver.get(name) orelse return null; |
| 3403 | | return &self.globals.items[global_index]; |
| 3196 | pub fn file(self: *Elf, index: File.Index) ?File { |
| 3197 | const tag = self.files.items(.tags)[index]; |
| 3198 | return switch (tag) { |
| 3199 | .null => null, |
| 3200 | .linker_defined => .{ .linker_defined = &self.files.items(.data)[index].linker_defined }, |
| 3201 | .zig_module => .{ .zig_module = &self.files.items(.data)[index].zig_module }, |
| 3202 | }; |
| 3404 | 3203 | } |
| 3405 | 3204 | |
| 3406 | | /// Returns the index of the global entry for `name` if one exists. |
| 3407 | | pub fn globalIndex(self: *const Elf, name: []const u8) ?u32 { |
| 3408 | | return self.resolver.get(name); |
| 3205 | /// Returns pointer-to-symbol described at sym_index. |
| 3206 | pub fn symbol(self: *Elf, sym_index: Symbol.Index) *Symbol { |
| 3207 | return &self.symbols.items[sym_index]; |
| 3409 | 3208 | } |
| 3410 | 3209 | |
| 3411 | | /// Returns global entry at `index`. |
| 3412 | | pub fn globalByIndex(self: *const Elf, index: u32) u32 { |
| 3413 | | assert(index < self.globals.items.len); |
| 3414 | | return self.globals.items[index]; |
| 3210 | pub fn addSymbol(self: *Elf) !Symbol.Index { |
| 3211 | try self.symbols.ensureUnusedCapacity(self.base.allocator, 1); |
| 3212 | const index = blk: { |
| 3213 | if (self.symbols_free_list.popOrNull()) |index| { |
| 3214 | log.debug(" (reusing symbol index {d})", .{index}); |
| 3215 | break :blk index; |
| 3216 | } else { |
| 3217 | log.debug(" (allocating symbol index {d})", .{self.symbols.items.len}); |
| 3218 | const index = @as(Symbol.Index, @intCast(self.symbols.items.len)); |
| 3219 | _ = self.symbols.addOneAssumeCapacity(); |
| 3220 | break :blk index; |
| 3221 | } |
| 3222 | }; |
| 3223 | self.symbols.items[index] = .{ .symbol_index = index }; |
| 3224 | return index; |
| 3415 | 3225 | } |
| 3416 | 3226 | |
| 3417 | 3227 | const GetOrPutGlobalResult = struct { |
| 3418 | 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. |
| 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 | | } |
| 3232 | pub fn getOrPutGlobal(self: *Elf, name_off: u32) !GetOrPutGlobalResult { |
| 3429 | 3233 | const gpa = self.base.allocator; |
| 3430 | | const global_index = try self.allocateGlobal(); |
| 3431 | | const global_name = try gpa.dupe(u8, name); |
| 3432 | | _ = try self.resolver.put(gpa, global_name, global_index); |
| 3433 | | const ptr = &self.globals.items[global_index]; |
| 3434 | | return GetOrPutGlobalResult{ .found_existing = false, .value_ptr = ptr }; |
| 3435 | | } |
| 3436 | | |
| 3437 | | pub fn atom(self: *Elf, atom_index: Atom.Index) *Atom { |
| 3438 | | assert(atom_index < self.atoms.items.len); |
| 3439 | | return &self.atoms.items[atom_index]; |
| 3234 | const gop = try self.resolver.getOrPut(gpa, name_off); |
| 3235 | if (!gop.found_existing) { |
| 3236 | const index = try self.addSymbol(); |
| 3237 | const global = self.symbol(index); |
| 3238 | global.name_offset = name_off; |
| 3239 | gop.value_ptr.* = index; |
| 3240 | } |
| 3241 | return .{ |
| 3242 | .found_existing = gop.found_existing, |
| 3243 | .index = gop.value_ptr.*, |
| 3244 | }; |
| 3440 | 3245 | } |
| 3441 | 3246 | |
| 3442 | | /// Returns atom if there is an atom referenced by the symbol. |
| 3443 | | /// Returns null on failure. |
| 3444 | | pub fn atomIndexForSymbol(self: *Elf, sym_index: u32) ?Atom.Index { |
| 3445 | | return self.atom_by_index_table.get(sym_index); |
| 3247 | pub fn getGlobalByName(self: *Elf, name: []const u8) ?Symbol.Index { |
| 3248 | const name_off = self.strtab.getOffset(name) orelse return null; |
| 3249 | return self.resolver.get(name_off); |
| 3446 | 3250 | } |
| 3447 | 3251 | |
| 3448 | | fn dumpState(self: *Elf ) std.fmt.Formatter(fmtDumpState) { |
| 3252 | fn dumpState(self: *Elf) std.fmt.Formatter(fmtDumpState) { |
| 3449 | 3253 | return .{ .data = self }; |
| 3450 | 3254 | } |
| 3451 | 3255 | |
| 3452 | | fn fmtDumpState(self: *Elf, |
| 3256 | fn fmtDumpState( |
| 3257 | self: *Elf, |
| 3453 | 3258 | comptime unused_fmt_string: []const u8, |
| 3454 | 3259 | options: std.fmt.FormatOptions, |
| 3455 | 3260 | writer: anytype, |
| 3456 | 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 | 3277 | const default_entry_addr = 0x8000000; |
| 3470 | 3278 | |
| 3471 | 3279 | pub const base_tag: link.File.Tag = .elf; |
| ... | ... | @@ -3497,21 +3305,20 @@ const Section = struct { |
| 3497 | 3305 | |
| 3498 | 3306 | const LazySymbolMetadata = struct { |
| 3499 | 3307 | const State = enum { unused, pending_flush, flushed }; |
| 3500 | | text_atom: Atom.Index = undefined, |
| 3501 | | rodata_atom: Atom.Index = undefined, |
| 3308 | text_symbol_index: Symbol.Index = undefined, |
| 3309 | rodata_symbol_index: Symbol.Index = undefined, |
| 3502 | 3310 | text_state: State = .unused, |
| 3503 | 3311 | rodata_state: State = .unused, |
| 3504 | 3312 | }; |
| 3505 | 3313 | |
| 3506 | 3314 | const DeclMetadata = struct { |
| 3507 | | atom: Atom.Index, |
| 3508 | | shdr: u16, |
| 3315 | symbol_index: Symbol.Index, |
| 3509 | 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 | 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 | 3323 | return null; |
| 3517 | 3324 | } |
| ... | ... | @@ -3543,13 +3350,14 @@ pub const Atom = @import("Elf/Atom.zig"); |
| 3543 | 3350 | const Cache = std.Build.Cache; |
| 3544 | 3351 | const Compilation = @import("../Compilation.zig"); |
| 3545 | 3352 | const Dwarf = @import("Dwarf.zig"); |
| 3546 | | const File = @import("Elf/File.zig"); |
| 3353 | const File = @import("Elf/file.zig").File; |
| 3547 | 3354 | const LinkerDefined = @import("Elf/LinkerDefined.zig"); |
| 3548 | 3355 | const Liveness = @import("../Liveness.zig"); |
| 3549 | 3356 | const LlvmObject = @import("../codegen/llvm.zig").Object; |
| 3550 | 3357 | const Module = @import("../Module.zig"); |
| 3551 | 3358 | const InternPool = @import("../InternPool.zig"); |
| 3552 | 3359 | const Package = @import("../Package.zig"); |
| 3360 | const Symbol = @import("Elf/Symbol.zig"); |
| 3553 | 3361 | const StringTable = @import("strtab.zig").StringTable; |
| 3554 | 3362 | const TableSection = @import("table_section.zig").TableSection; |
| 3555 | 3363 | const Type = @import("../type.zig").Type; |