| ... | ... | @@ -280,10 +280,10 @@ pub fn getDeclVAddr(self: *Elf, decl_index: Module.Decl.Index, reloc_info: File. |
| 280 | 280 | assert(self.llvm_object == null); |
| 281 | 281 | |
| 282 | 282 | const this_atom_index = try self.getOrCreateAtomForDecl(decl_index); |
| 283 | | const this_atom = self.getAtom(this_atom_index); |
| 284 | | const target = this_atom.getSymbolIndex().?; |
| 285 | | const vaddr = this_atom.getSymbol(self).st_value; |
| 286 | | const atom_index = self.getAtomIndexForSymbol(reloc_info.parent_atom_index).?; |
| 283 | const this_atom = self.atom(this_atom_index); |
| 284 | const target = this_atom.symbolIndex().?; |
| 285 | const vaddr = this_atom.symbol(self).st_value; |
| 286 | const atom_index = self.atomIndexForSymbol(reloc_info.parent_atom_index).?; |
| 287 | 287 | try Atom.addRelocation(self, atom_index, .{ |
| 288 | 288 | .target = target, |
| 289 | 289 | .offset = reloc_info.offset, |
| ... | ... | @@ -849,8 +849,8 @@ fn growAllocSection(self: *Elf, shdr_index: u16, needed_size: u64) !void { |
| 849 | 849 | // Must move the entire section. |
| 850 | 850 | const new_offset = self.findFreeSpace(needed_size, self.page_size); |
| 851 | 851 | const existing_size = if (maybe_last_atom_index) |last_atom_index| blk: { |
| 852 | | const last = self.getAtom(last_atom_index); |
| 853 | | const sym = last.getSymbol(self); |
| 852 | const last = self.atom(last_atom_index); |
| 853 | const sym = last.symbol(self); |
| 854 | 854 | break :blk (sym.st_value + sym.st_size) - phdr.p_vaddr; |
| 855 | 855 | } else if (shdr_index == self.got_section_index.?) blk: { |
| 856 | 856 | break :blk shdr.sh_size; |
| ... | ... | @@ -1010,8 +1010,8 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node |
| 1010 | 1010 | while (it.next()) |entry| { |
| 1011 | 1011 | const atom_index = entry.key_ptr.*; |
| 1012 | 1012 | const relocs = entry.value_ptr.*; |
| 1013 | | const atom = self.getAtom(atom_index); |
| 1014 | | const source_sym = atom.getSymbol(self); |
| 1013 | const atom_ptr = self.atom(atom_index); |
| 1014 | const source_sym = atom_ptr.symbol(self); |
| 1015 | 1015 | const source_shdr = self.sections.items(.shdr)[source_sym.st_shndx]; |
| 1016 | 1016 | |
| 1017 | 1017 | log.debug("relocating '{?s}'", .{self.strtab.get(source_sym.st_name)}); |
| ... | ... | @@ -1471,9 +1471,9 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v |
| 1471 | 1471 | try argv.append(entry); |
| 1472 | 1472 | } |
| 1473 | 1473 | |
| 1474 | | for (self.base.options.force_undefined_symbols.keys()) |symbol| { |
| 1474 | for (self.base.options.force_undefined_symbols.keys()) |sym| { |
| 1475 | 1475 | try argv.append("-u"); |
| 1476 | | try argv.append(symbol); |
| 1476 | try argv.append(sym); |
| 1477 | 1477 | } |
| 1478 | 1478 | |
| 1479 | 1479 | switch (self.base.options.hash_style) { |
| ... | ... | @@ -2071,13 +2071,13 @@ fn writeElfHeader(self: *Elf) !void { |
| 2071 | 2071 | } |
| 2072 | 2072 | |
| 2073 | 2073 | fn freeAtom(self: *Elf, atom_index: Atom.Index) void { |
| 2074 | | const atom = self.getAtom(atom_index); |
| 2075 | | log.debug("freeAtom {d} ({s})", .{ atom_index, atom.getName(self) }); |
| 2074 | const atom_ptr = self.atom(atom_index); |
| 2075 | log.debug("freeAtom {d} ({s})", .{ atom_index, atom_ptr.name(self) }); |
| 2076 | 2076 | |
| 2077 | 2077 | Atom.freeRelocations(self, atom_index); |
| 2078 | 2078 | |
| 2079 | 2079 | const gpa = self.base.allocator; |
| 2080 | | const shndx = atom.getSymbol(self).st_shndx; |
| 2080 | const shndx = atom_ptr.symbol(self).st_shndx; |
| 2081 | 2081 | const free_list = &self.sections.items(.free_list)[shndx]; |
| 2082 | 2082 | var already_have_free_list_node = false; |
| 2083 | 2083 | { |
| ... | ... | @@ -2088,7 +2088,7 @@ fn freeAtom(self: *Elf, atom_index: Atom.Index) void { |
| 2088 | 2088 | _ = free_list.swapRemove(i); |
| 2089 | 2089 | continue; |
| 2090 | 2090 | } |
| 2091 | | if (free_list.items[i] == atom.prev_index) { |
| 2091 | if (free_list.items[i] == atom_ptr.prev_index) { |
| 2092 | 2092 | already_have_free_list_node = true; |
| 2093 | 2093 | } |
| 2094 | 2094 | i += 1; |
| ... | ... | @@ -2098,7 +2098,7 @@ fn freeAtom(self: *Elf, atom_index: Atom.Index) void { |
| 2098 | 2098 | const maybe_last_atom_index = &self.sections.items(.last_atom_index)[shndx]; |
| 2099 | 2099 | if (maybe_last_atom_index.*) |last_atom_index| { |
| 2100 | 2100 | if (last_atom_index == atom_index) { |
| 2101 | | if (atom.prev_index) |prev_index| { |
| 2101 | if (atom_ptr.prev_index) |prev_index| { |
| 2102 | 2102 | // TODO shrink the section size here |
| 2103 | 2103 | maybe_last_atom_index.* = prev_index; |
| 2104 | 2104 | } else { |
| ... | ... | @@ -2107,9 +2107,9 @@ fn freeAtom(self: *Elf, atom_index: Atom.Index) void { |
| 2107 | 2107 | } |
| 2108 | 2108 | } |
| 2109 | 2109 | |
| 2110 | | if (atom.prev_index) |prev_index| { |
| 2111 | | const prev = self.getAtomPtr(prev_index); |
| 2112 | | prev.next_index = atom.next_index; |
| 2110 | if (atom_ptr.prev_index) |prev_index| { |
| 2111 | const prev = self.atom(prev_index); |
| 2112 | prev.next_index = atom_ptr.next_index; |
| 2113 | 2113 | |
| 2114 | 2114 | if (!already_have_free_list_node and prev.*.freeListEligible(self)) { |
| 2115 | 2115 | // The free list is heuristics, it doesn't have to be perfect, so we can |
| ... | ... | @@ -2117,23 +2117,23 @@ fn freeAtom(self: *Elf, atom_index: Atom.Index) void { |
| 2117 | 2117 | free_list.append(gpa, prev_index) catch {}; |
| 2118 | 2118 | } |
| 2119 | 2119 | } else { |
| 2120 | | self.getAtomPtr(atom_index).prev_index = null; |
| 2120 | atom_ptr.prev_index = null; |
| 2121 | 2121 | } |
| 2122 | 2122 | |
| 2123 | | if (atom.next_index) |next_index| { |
| 2124 | | self.getAtomPtr(next_index).prev_index = atom.prev_index; |
| 2123 | if (atom_ptr.next_index) |next_index| { |
| 2124 | self.atom(next_index).prev_index = atom_ptr.prev_index; |
| 2125 | 2125 | } else { |
| 2126 | | self.getAtomPtr(atom_index).next_index = null; |
| 2126 | atom_ptr.next_index = null; |
| 2127 | 2127 | } |
| 2128 | 2128 | |
| 2129 | 2129 | // Appending to free lists is allowed to fail because the free lists are heuristics based anyway. |
| 2130 | | const sym_index = atom.getSymbolIndex().?; |
| 2130 | const sym_index = atom_ptr.symbolIndex().?; |
| 2131 | 2131 | |
| 2132 | 2132 | log.debug("adding %{d} to local symbols free list", .{sym_index}); |
| 2133 | 2133 | self.locals_free_list.append(gpa, sym_index) catch {}; |
| 2134 | 2134 | self.locals.items[sym_index] = null_sym; |
| 2135 | 2135 | _ = self.atom_by_index_table.remove(sym_index); |
| 2136 | | self.getAtomPtr(atom_index).sym_index = 0; |
| 2136 | atom_ptr.sym_index = 0; |
| 2137 | 2137 | self.got_table.freeEntry(gpa, sym_index); |
| 2138 | 2138 | } |
| 2139 | 2139 | |
| ... | ... | @@ -2144,10 +2144,10 @@ fn shrinkAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64) void { |
| 2144 | 2144 | } |
| 2145 | 2145 | |
| 2146 | 2146 | fn growAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64, alignment: u64) !u64 { |
| 2147 | | const atom = self.getAtom(atom_index); |
| 2148 | | const sym = atom.getSymbol(self); |
| 2147 | const atom_ptr = self.atom(atom_index); |
| 2148 | const sym = atom_ptr.symbol(self); |
| 2149 | 2149 | const align_ok = mem.alignBackward(u64, sym.st_value, alignment) == sym.st_value; |
| 2150 | | const need_realloc = !align_ok or new_block_size > atom.capacity(self); |
| 2150 | const need_realloc = !align_ok or new_block_size > atom_ptr.capacity(self); |
| 2151 | 2151 | if (!need_realloc) return sym.st_value; |
| 2152 | 2152 | return self.allocateAtom(atom_index, new_block_size, alignment); |
| 2153 | 2153 | } |
| ... | ... | @@ -2155,10 +2155,10 @@ fn growAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64, alignment: |
| 2155 | 2155 | pub fn createAtom(self: *Elf) !Atom.Index { |
| 2156 | 2156 | const gpa = self.base.allocator; |
| 2157 | 2157 | const atom_index = @as(Atom.Index, @intCast(self.atoms.items.len)); |
| 2158 | | const atom = try self.atoms.addOne(gpa); |
| 2158 | const atom_ptr = try self.atoms.addOne(gpa); |
| 2159 | 2159 | const sym_index = try self.allocateSymbol(); |
| 2160 | 2160 | try self.atom_by_index_table.putNoClobber(gpa, sym_index, atom_index); |
| 2161 | | atom.* = .{ |
| 2161 | atom_ptr.* = .{ |
| 2162 | 2162 | .sym_index = sym_index, |
| 2163 | 2163 | .prev_index = null, |
| 2164 | 2164 | .next_index = null, |
| ... | ... | @@ -2168,8 +2168,7 @@ pub fn createAtom(self: *Elf) !Atom.Index { |
| 2168 | 2168 | } |
| 2169 | 2169 | |
| 2170 | 2170 | fn allocateAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64, alignment: u64) !u64 { |
| 2171 | | const atom = self.getAtom(atom_index); |
| 2172 | | const sym = atom.getSymbol(self); |
| 2171 | const sym = self.atom(atom_index).symbol(self); |
| 2173 | 2172 | const phdr_index = self.sections.items(.phdr_index)[sym.st_shndx]; |
| 2174 | 2173 | const phdr = &self.program_headers.items[phdr_index]; |
| 2175 | 2174 | const shdr = &self.sections.items(.shdr)[sym.st_shndx]; |
| ... | ... | @@ -2191,10 +2190,10 @@ fn allocateAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64, alignme |
| 2191 | 2190 | var i: usize = if (self.base.child_pid == null) 0 else free_list.items.len; |
| 2192 | 2191 | while (i < free_list.items.len) { |
| 2193 | 2192 | const big_atom_index = free_list.items[i]; |
| 2194 | | const big_atom = self.getAtom(big_atom_index); |
| 2193 | const big_atom = self.atom(big_atom_index); |
| 2195 | 2194 | // We now have a pointer to a live atom that has too much capacity. |
| 2196 | 2195 | // Is it enough that we could fit this new atom? |
| 2197 | | const big_atom_sym = big_atom.getSymbol(self); |
| 2196 | const big_atom_sym = big_atom.symbol(self); |
| 2198 | 2197 | const capacity = big_atom.capacity(self); |
| 2199 | 2198 | const ideal_capacity = padToIdeal(capacity); |
| 2200 | 2199 | const ideal_capacity_end_vaddr = std.math.add(u64, big_atom_sym.st_value, ideal_capacity) catch ideal_capacity; |
| ... | ... | @@ -2225,8 +2224,8 @@ fn allocateAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64, alignme |
| 2225 | 2224 | } |
| 2226 | 2225 | break :blk new_start_vaddr; |
| 2227 | 2226 | } else if (maybe_last_atom_index.*) |last_index| { |
| 2228 | | const last = self.getAtom(last_index); |
| 2229 | | const last_sym = last.getSymbol(self); |
| 2227 | const last = self.atom(last_index); |
| 2228 | const last_sym = last.symbol(self); |
| 2230 | 2229 | const ideal_capacity = padToIdeal(last_sym.st_size); |
| 2231 | 2230 | const ideal_capacity_end_vaddr = last_sym.st_value + ideal_capacity; |
| 2232 | 2231 | const new_start_vaddr = mem.alignForward(u64, ideal_capacity_end_vaddr, alignment); |
| ... | ... | @@ -2239,7 +2238,7 @@ fn allocateAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64, alignme |
| 2239 | 2238 | }; |
| 2240 | 2239 | |
| 2241 | 2240 | const expand_section = if (atom_placement) |placement_index| |
| 2242 | | self.getAtom(placement_index).next_index == null |
| 2241 | self.atom(placement_index).next_index == null |
| 2243 | 2242 | else |
| 2244 | 2243 | true; |
| 2245 | 2244 | if (expand_section) { |
| ... | ... | @@ -2263,23 +2262,22 @@ fn allocateAtom(self: *Elf, atom_index: Atom.Index, new_block_size: u64, alignme |
| 2263 | 2262 | // This function can also reallocate an atom. |
| 2264 | 2263 | // In this case we need to "unplug" it from its previous location before |
| 2265 | 2264 | // plugging it in to its new location. |
| 2266 | | if (atom.prev_index) |prev_index| { |
| 2267 | | const prev = self.getAtomPtr(prev_index); |
| 2268 | | prev.next_index = atom.next_index; |
| 2265 | const atom_ptr = self.atom(atom_index); |
| 2266 | if (atom_ptr.prev_index) |prev_index| { |
| 2267 | const prev = self.atom(prev_index); |
| 2268 | prev.next_index = atom_ptr.next_index; |
| 2269 | 2269 | } |
| 2270 | | if (atom.next_index) |next_index| { |
| 2271 | | const next = self.getAtomPtr(next_index); |
| 2272 | | next.prev_index = atom.prev_index; |
| 2270 | if (atom_ptr.next_index) |next_index| { |
| 2271 | const next = self.atom(next_index); |
| 2272 | next.prev_index = atom_ptr.prev_index; |
| 2273 | 2273 | } |
| 2274 | 2274 | |
| 2275 | 2275 | if (atom_placement) |big_atom_index| { |
| 2276 | | const big_atom = self.getAtomPtr(big_atom_index); |
| 2277 | | const atom_ptr = self.getAtomPtr(atom_index); |
| 2276 | const big_atom = self.atom(big_atom_index); |
| 2278 | 2277 | atom_ptr.prev_index = big_atom_index; |
| 2279 | 2278 | atom_ptr.next_index = big_atom.next_index; |
| 2280 | 2279 | big_atom.next_index = atom_index; |
| 2281 | 2280 | } else { |
| 2282 | | const atom_ptr = self.getAtomPtr(atom_index); |
| 2283 | 2281 | atom_ptr.prev_index = null; |
| 2284 | 2282 | atom_ptr.next_index = null; |
| 2285 | 2283 | } |
| ... | ... | @@ -2320,7 +2318,7 @@ fn allocateGlobal(self: *Elf) !u32 { |
| 2320 | 2318 | log.debug(" (reusing global index {d})", .{index}); |
| 2321 | 2319 | break :blk index; |
| 2322 | 2320 | } else { |
| 2323 | | log.debug(" (allocating symbol index {d})", .{self.globals.items.len}); |
| 2321 | log.debug(" (allocating global index {d})", .{self.globals.items.len}); |
| 2324 | 2322 | const index = @as(u32, @intCast(self.globals.items.len)); |
| 2325 | 2323 | _ = self.globals.addOneAssumeCapacity(); |
| 2326 | 2324 | break :blk index; |
| ... | ... | @@ -2332,8 +2330,8 @@ fn allocateGlobal(self: *Elf) !u32 { |
| 2332 | 2330 | |
| 2333 | 2331 | fn freeUnnamedConsts(self: *Elf, decl_index: Module.Decl.Index) void { |
| 2334 | 2332 | const unnamed_consts = self.unnamed_const_atoms.getPtr(decl_index) orelse return; |
| 2335 | | for (unnamed_consts.items) |atom| { |
| 2336 | | self.freeAtom(atom); |
| 2333 | for (unnamed_consts.items) |atom_index| { |
| 2334 | self.freeAtom(atom_index); |
| 2337 | 2335 | } |
| 2338 | 2336 | unnamed_consts.clearAndFree(self.base.allocator); |
| 2339 | 2337 | } |
| ... | ... | @@ -2373,13 +2371,13 @@ pub fn getOrCreateAtomForLazySymbol(self: *Elf, sym: File.LazySymbol) !Atom.Inde |
| 2373 | 2371 | .flushed => {}, |
| 2374 | 2372 | } |
| 2375 | 2373 | metadata.state.* = .pending_flush; |
| 2376 | | const atom = metadata.atom.*; |
| 2374 | const atom_index = metadata.atom.*; |
| 2377 | 2375 | // anyerror needs to be deferred until flushModule |
| 2378 | | if (sym.getDecl(mod) != .none) try self.updateLazySymbolAtom(sym, atom, switch (sym.kind) { |
| 2376 | if (sym.getDecl(mod) != .none) try self.updateLazySymbolAtom(sym, atom_index, switch (sym.kind) { |
| 2379 | 2377 | .code => self.text_section_index.?, |
| 2380 | 2378 | .const_data => self.rodata_section_index.?, |
| 2381 | 2379 | }); |
| 2382 | | return atom; |
| 2380 | return atom_index; |
| 2383 | 2381 | } |
| 2384 | 2382 | |
| 2385 | 2383 | pub fn getOrCreateAtomForDecl(self: *Elf, decl_index: Module.Decl.Index) !Atom.Index { |
| ... | ... | @@ -2432,18 +2430,18 @@ fn updateDeclCode(self: *Elf, decl_index: Module.Decl.Index, code: []const u8, s |
| 2432 | 2430 | |
| 2433 | 2431 | const decl_metadata = self.decls.get(decl_index).?; |
| 2434 | 2432 | const atom_index = decl_metadata.atom; |
| 2435 | | const atom = self.getAtom(atom_index); |
| 2436 | | const local_sym_index = atom.getSymbolIndex().?; |
| 2433 | const atom_ptr = self.atom(atom_index); |
| 2434 | const local_sym_index = atom_ptr.symbolIndex().?; |
| 2435 | const local_sym = atom_ptr.symbol(self); |
| 2437 | 2436 | |
| 2438 | 2437 | const shdr_index = decl_metadata.shdr; |
| 2439 | | if (atom.getSymbol(self).st_size != 0 and self.base.child_pid == null) { |
| 2440 | | const local_sym = atom.getSymbolPtr(self); |
| 2438 | if (atom_ptr.symbol(self).st_size != 0 and self.base.child_pid == null) { |
| 2441 | 2439 | local_sym.st_name = try self.strtab.insert(gpa, decl_name); |
| 2442 | 2440 | local_sym.st_info = (elf.STB_LOCAL << 4) | stt_bits; |
| 2443 | 2441 | local_sym.st_other = 0; |
| 2444 | 2442 | local_sym.st_shndx = shdr_index; |
| 2445 | 2443 | |
| 2446 | | const capacity = atom.capacity(self); |
| 2444 | const capacity = atom_ptr.capacity(self); |
| 2447 | 2445 | const need_realloc = code.len > capacity or |
| 2448 | 2446 | !mem.isAlignedGeneric(u64, local_sym.st_value, required_alignment); |
| 2449 | 2447 | |
| ... | ... | @@ -2463,7 +2461,6 @@ fn updateDeclCode(self: *Elf, decl_index: Module.Decl.Index, code: []const u8, s |
| 2463 | 2461 | } |
| 2464 | 2462 | local_sym.st_size = code.len; |
| 2465 | 2463 | } else { |
| 2466 | | const local_sym = atom.getSymbolPtr(self); |
| 2467 | 2464 | local_sym.* = .{ |
| 2468 | 2465 | .st_name = try self.strtab.insert(gpa, decl_name), |
| 2469 | 2466 | .st_info = (elf.STB_LOCAL << 4) | stt_bits, |
| ... | ... | @@ -2479,11 +2476,10 @@ fn updateDeclCode(self: *Elf, decl_index: Module.Decl.Index, code: []const u8, s |
| 2479 | 2476 | local_sym.st_value = vaddr; |
| 2480 | 2477 | local_sym.st_size = code.len; |
| 2481 | 2478 | |
| 2482 | | const got_entry_index = try atom.getOrCreateOffsetTableEntry(self); |
| 2479 | const got_entry_index = try atom_ptr.getOrCreateOffsetTableEntry(self); |
| 2483 | 2480 | try self.writeOffsetTableEntry(got_entry_index); |
| 2484 | 2481 | } |
| 2485 | 2482 | |
| 2486 | | const local_sym = atom.getSymbolPtr(self); |
| 2487 | 2483 | const phdr_index = self.sections.items(.phdr_index)[shdr_index]; |
| 2488 | 2484 | const section_offset = local_sym.st_value - self.program_headers.items[phdr_index].p_vaddr; |
| 2489 | 2485 | const file_offset = self.sections.items(.shdr)[shdr_index].sh_offset + section_offset; |
| ... | ... | @@ -2594,7 +2590,6 @@ pub fn updateDecl( |
| 2594 | 2590 | |
| 2595 | 2591 | const atom_index = try self.getOrCreateAtomForDecl(decl_index); |
| 2596 | 2592 | Atom.freeRelocations(self, atom_index); |
| 2597 | | const atom = self.getAtom(atom_index); |
| 2598 | 2593 | |
| 2599 | 2594 | var code_buffer = std.ArrayList(u8).init(self.base.allocator); |
| 2600 | 2595 | defer code_buffer.deinit(); |
| ... | ... | @@ -2611,14 +2606,14 @@ pub fn updateDecl( |
| 2611 | 2606 | }, &code_buffer, .{ |
| 2612 | 2607 | .dwarf = ds, |
| 2613 | 2608 | }, .{ |
| 2614 | | .parent_atom_index = atom.getSymbolIndex().?, |
| 2609 | .parent_atom_index = self.atom(atom_index).symbolIndex().?, |
| 2615 | 2610 | }) |
| 2616 | 2611 | else |
| 2617 | 2612 | try codegen.generateSymbol(&self.base, decl.srcLoc(mod), .{ |
| 2618 | 2613 | .ty = decl.ty, |
| 2619 | 2614 | .val = decl_val, |
| 2620 | 2615 | }, &code_buffer, .none, .{ |
| 2621 | | .parent_atom_index = atom.getSymbolIndex().?, |
| 2616 | .parent_atom_index = self.atom(atom_index).symbolIndex().?, |
| 2622 | 2617 | }); |
| 2623 | 2618 | |
| 2624 | 2619 | const code = switch (res) { |
| ... | ... | @@ -2669,8 +2664,8 @@ fn updateLazySymbolAtom( |
| 2669 | 2664 | }; |
| 2670 | 2665 | const name = self.strtab.get(name_str_index).?; |
| 2671 | 2666 | |
| 2672 | | const atom = self.getAtom(atom_index); |
| 2673 | | const local_sym_index = atom.getSymbolIndex().?; |
| 2667 | const atom_ptr = self.atom(atom_index); |
| 2668 | const local_sym_index = atom_ptr.symbolIndex().?; |
| 2674 | 2669 | |
| 2675 | 2670 | const src = if (sym.ty.getOwnerDeclOrNull(mod)) |owner_decl| |
| 2676 | 2671 | mod.declPtr(owner_decl).srcLoc(mod) |
| ... | ... | @@ -2698,7 +2693,7 @@ fn updateLazySymbolAtom( |
| 2698 | 2693 | }; |
| 2699 | 2694 | |
| 2700 | 2695 | const phdr_index = self.sections.items(.phdr_index)[shdr_index]; |
| 2701 | | const local_sym = atom.getSymbolPtr(self); |
| 2696 | const local_sym = atom_ptr.symbol(self); |
| 2702 | 2697 | local_sym.* = .{ |
| 2703 | 2698 | .st_name = name_str_index, |
| 2704 | 2699 | .st_info = (elf.STB_LOCAL << 4) | elf.STT_OBJECT, |
| ... | ... | @@ -2714,7 +2709,7 @@ fn updateLazySymbolAtom( |
| 2714 | 2709 | local_sym.st_value = vaddr; |
| 2715 | 2710 | local_sym.st_size = code.len; |
| 2716 | 2711 | |
| 2717 | | const got_entry_index = try atom.getOrCreateOffsetTableEntry(self); |
| 2712 | const got_entry_index = try atom_ptr.getOrCreateOffsetTableEntry(self); |
| 2718 | 2713 | try self.writeOffsetTableEntry(got_entry_index); |
| 2719 | 2714 | |
| 2720 | 2715 | const section_offset = vaddr - self.program_headers.items[phdr_index].p_vaddr; |
| ... | ... | @@ -2750,7 +2745,7 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module |
| 2750 | 2745 | const res = try codegen.generateSymbol(&self.base, decl.srcLoc(mod), typed_value, &code_buffer, .{ |
| 2751 | 2746 | .none = {}, |
| 2752 | 2747 | }, .{ |
| 2753 | | .parent_atom_index = self.getAtom(atom_index).getSymbolIndex().?, |
| 2748 | .parent_atom_index = self.atom(atom_index).symbolIndex().?, |
| 2754 | 2749 | }); |
| 2755 | 2750 | const code = switch (res) { |
| 2756 | 2751 | .ok => code_buffer.items, |
| ... | ... | @@ -2765,7 +2760,7 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module |
| 2765 | 2760 | const required_alignment = typed_value.ty.abiAlignment(mod); |
| 2766 | 2761 | const shdr_index = self.rodata_section_index.?; |
| 2767 | 2762 | const phdr_index = self.sections.items(.phdr_index)[shdr_index]; |
| 2768 | | const local_sym = self.getAtom(atom_index).getSymbolPtr(self); |
| 2763 | const local_sym = self.atom(atom_index).symbol(self); |
| 2769 | 2764 | local_sym.st_name = name_str_index; |
| 2770 | 2765 | local_sym.st_info = (elf.STB_LOCAL << 4) | elf.STT_OBJECT; |
| 2771 | 2766 | local_sym.st_other = 0; |
| ... | ... | @@ -2782,7 +2777,7 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module |
| 2782 | 2777 | const file_offset = self.sections.items(.shdr)[shdr_index].sh_offset + section_offset; |
| 2783 | 2778 | try self.base.file.?.pwriteAll(code, file_offset); |
| 2784 | 2779 | |
| 2785 | | return self.getAtom(atom_index).getSymbolIndex().?; |
| 2780 | return self.atom(atom_index).symbolIndex().?; |
| 2786 | 2781 | } |
| 2787 | 2782 | |
| 2788 | 2783 | pub fn updateDeclExports( |
| ... | ... | @@ -2805,8 +2800,8 @@ pub fn updateDeclExports( |
| 2805 | 2800 | |
| 2806 | 2801 | const decl = mod.declPtr(decl_index); |
| 2807 | 2802 | const atom_index = try self.getOrCreateAtomForDecl(decl_index); |
| 2808 | | const atom = self.getAtom(atom_index); |
| 2809 | | const decl_sym = atom.getSymbol(self); |
| 2803 | const atom_ptr = self.atom(atom_index); |
| 2804 | const decl_sym = atom_ptr.symbol(self); |
| 2810 | 2805 | const decl_metadata = self.decls.getPtr(decl_index).?; |
| 2811 | 2806 | const shdr_index = decl_metadata.shdr; |
| 2812 | 2807 | |
| ... | ... | @@ -2843,12 +2838,12 @@ pub fn updateDeclExports( |
| 2843 | 2838 | }; |
| 2844 | 2839 | const stt_bits: u8 = @as(u4, @truncate(decl_sym.st_info)); |
| 2845 | 2840 | |
| 2846 | | const sym_index = decl_metadata.getExport(self, exp_name) orelse blk: { |
| 2841 | const sym_index = if (decl_metadata.@"export"(self, exp_name)) |exp_index| exp_index.* else blk: { |
| 2847 | 2842 | const sym_index = try self.allocateSymbol(); |
| 2848 | 2843 | try decl_metadata.exports.append(gpa, sym_index); |
| 2849 | 2844 | break :blk sym_index; |
| 2850 | 2845 | }; |
| 2851 | | const sym = self.getSymbolPtr(sym_index); |
| 2846 | const sym = self.symbol(sym_index); |
| 2852 | 2847 | sym.* = .{ |
| 2853 | 2848 | .st_name = try self.strtab.insert(gpa, exp_name), |
| 2854 | 2849 | .st_info = (stb_bits << 4) | stt_bits, |
| ... | ... | @@ -2857,8 +2852,8 @@ pub fn updateDeclExports( |
| 2857 | 2852 | .st_value = decl_sym.st_value, |
| 2858 | 2853 | .st_size = decl_sym.st_size, |
| 2859 | 2854 | }; |
| 2860 | | const sym_name = self.getSymbolName(sym_index); |
| 2861 | | const gop = try self.getOrPutGlobalPtr(sym_name); |
| 2855 | const sym_name = self.symbolName(sym_index); |
| 2856 | const gop = try self.getOrPutGlobal(sym_name); |
| 2862 | 2857 | gop.value_ptr.* = sym_index; |
| 2863 | 2858 | } |
| 2864 | 2859 | } |
| ... | ... | @@ -2889,8 +2884,8 @@ pub fn deleteDeclExport( |
| 2889 | 2884 | const gpa = self.base.allocator; |
| 2890 | 2885 | const mod = self.base.options.module.?; |
| 2891 | 2886 | const exp_name = mod.intern_pool.stringToSlice(name); |
| 2892 | | const sym_index = metadata.getExportPtr(self, exp_name) orelse return; |
| 2893 | | const sym = self.getSymbolPtr(sym_index.*); |
| 2887 | const sym_index = metadata.@"export"(self, exp_name) orelse return; |
| 2888 | const sym = self.symbol(sym_index.*); |
| 2894 | 2889 | log.debug("deleting export '{s}'", .{exp_name}); |
| 2895 | 2890 | sym.* = null_sym; |
| 2896 | 2891 | self.locals_free_list.append(gpa, sym_index.*) catch {}; |
| ... | ... | @@ -2960,7 +2955,7 @@ fn writeOffsetTableEntry(self: *Elf, index: @TypeOf(self.got_table).Index) !void |
| 2960 | 2955 | const phdr = &self.program_headers.items[self.phdr_got_index.?]; |
| 2961 | 2956 | const vaddr = phdr.p_vaddr + @as(u64, entry_size) * index; |
| 2962 | 2957 | const got_entry = self.got_table.entries.items[index]; |
| 2963 | | const got_value = self.getSymbol(got_entry).st_value; |
| 2958 | const got_value = self.symbol(got_entry).st_value; |
| 2964 | 2959 | switch (entry_size) { |
| 2965 | 2960 | 2 => { |
| 2966 | 2961 | var buf: [2]u8 = undefined; |
| ... | ... | @@ -3046,8 +3041,8 @@ fn writeSymbols(self: *Elf) !void { |
| 3046 | 3041 | } |
| 3047 | 3042 | } |
| 3048 | 3043 | |
| 3049 | | for (buf[self.locals.items.len..], self.globals.items) |*sym, global| { |
| 3050 | | elf32SymFromSym(self.getSymbol(global), sym); |
| 3044 | for (buf[self.locals.items.len..], self.globals.items) |*sym, glob| { |
| 3045 | elf32SymFromSym(self.symbol(glob).*, sym); |
| 3051 | 3046 | if (foreign_endian) { |
| 3052 | 3047 | mem.byteSwapAllFields(elf.Elf32_Sym, sym); |
| 3053 | 3048 | } |
| ... | ... | @@ -3064,8 +3059,8 @@ fn writeSymbols(self: *Elf) !void { |
| 3064 | 3059 | } |
| 3065 | 3060 | } |
| 3066 | 3061 | |
| 3067 | | for (buf[self.locals.items.len..], self.globals.items) |*sym, global| { |
| 3068 | | sym.* = self.getSymbol(global); |
| 3062 | for (buf[self.locals.items.len..], self.globals.items) |*sym, glob| { |
| 3063 | sym.* = self.symbol(glob).*; |
| 3069 | 3064 | if (foreign_endian) { |
| 3070 | 3065 | mem.byteSwapAllFields(elf.Elf64_Sym, sym); |
| 3071 | 3066 | } |
| ... | ... | @@ -3369,8 +3364,8 @@ fn logSymtab(self: Elf) void { |
| 3369 | 3364 | log.debug(" {d}: {?s}: @{x} in {d}", .{ id, self.strtab.get(sym.st_name), sym.st_value, sym.st_shndx }); |
| 3370 | 3365 | } |
| 3371 | 3366 | log.debug("globals:", .{}); |
| 3372 | | for (self.globals.items, 0..) |global, id| { |
| 3373 | | const sym = self.getSymbol(global); |
| 3367 | for (self.globals.items, 0..) |glob, id| { |
| 3368 | const sym = self.symbol(glob); |
| 3374 | 3369 | log.debug(" {d}: {?s}: @{x} in {d}", .{ id, self.strtab.get(sym.st_name), sym.st_value, sym.st_shndx }); |
| 3375 | 3370 | } |
| 3376 | 3371 | } |
| ... | ... | @@ -3386,45 +3381,34 @@ pub fn getProgramHeaderPtr(self: *Elf, shdr_index: u16) *elf.Elf64_Phdr { |
| 3386 | 3381 | } |
| 3387 | 3382 | |
| 3388 | 3383 | /// Returns pointer-to-symbol described at sym_index. |
| 3389 | | pub fn getSymbolPtr(self: *Elf, sym_index: u32) *elf.Elf64_Sym { |
| 3384 | pub fn symbol(self: *const Elf, sym_index: u32) *elf.Elf64_Sym { |
| 3390 | 3385 | return &self.locals.items[sym_index]; |
| 3391 | 3386 | } |
| 3392 | 3387 | |
| 3393 | | /// Returns symbol at sym_index. |
| 3394 | | pub fn getSymbol(self: *const Elf, sym_index: u32) elf.Elf64_Sym { |
| 3395 | | return self.locals.items[sym_index]; |
| 3396 | | } |
| 3397 | | |
| 3398 | 3388 | /// Returns name of the symbol at sym_index. |
| 3399 | | pub fn getSymbolName(self: *const Elf, sym_index: u32) []const u8 { |
| 3389 | pub fn symbolName(self: *const Elf, sym_index: u32) []const u8 { |
| 3400 | 3390 | const sym = self.locals.items[sym_index]; |
| 3401 | 3391 | return self.strtab.get(sym.st_name).?; |
| 3402 | 3392 | } |
| 3403 | 3393 | |
| 3404 | 3394 | /// Returns pointer to the global entry for `name` if one exists. |
| 3405 | | pub fn getGlobalPtr(self: *Elf, name: []const u8) ?*u32 { |
| 3395 | pub fn global(self: *const Elf, name: []const u8) ?*u32 { |
| 3406 | 3396 | const global_index = self.resolver.get(name) orelse return null; |
| 3407 | 3397 | return &self.globals.items[global_index]; |
| 3408 | 3398 | } |
| 3409 | 3399 | |
| 3410 | | /// Returns the global entry for `name` if one exists. |
| 3411 | | pub fn getGlobal(self: *const Elf, name: []const u8) ?u32 { |
| 3412 | | const global_index = self.resolver.get(name) orelse return null; |
| 3413 | | return self.globals.items[global_index]; |
| 3414 | | } |
| 3415 | | |
| 3416 | 3400 | /// Returns the index of the global entry for `name` if one exists. |
| 3417 | | pub fn getGlobalIndex(self: *const Elf, name: []const u8) ?u32 { |
| 3401 | pub fn globalIndex(self: *const Elf, name: []const u8) ?u32 { |
| 3418 | 3402 | return self.resolver.get(name); |
| 3419 | 3403 | } |
| 3420 | 3404 | |
| 3421 | 3405 | /// Returns global entry at `index`. |
| 3422 | | pub fn getGlobalByIndex(self: *const Elf, index: u32) u32 { |
| 3406 | pub fn globalByIndex(self: *const Elf, index: u32) u32 { |
| 3423 | 3407 | assert(index < self.globals.items.len); |
| 3424 | 3408 | return self.globals.items[index]; |
| 3425 | 3409 | } |
| 3426 | 3410 | |
| 3427 | | const GetOrPutGlobalPtrResult = struct { |
| 3411 | const GetOrPutGlobalResult = struct { |
| 3428 | 3412 | found_existing: bool, |
| 3429 | 3413 | value_ptr: *u32, |
| 3430 | 3414 | }; |
| ... | ... | @@ -3432,31 +3416,26 @@ const GetOrPutGlobalPtrResult = struct { |
| 3432 | 3416 | /// Return pointer to the global entry for `name` if one exists. |
| 3433 | 3417 | /// Puts a new global entry for `name` if one doesn't exist, and |
| 3434 | 3418 | /// returns a pointer to it. |
| 3435 | | pub fn getOrPutGlobalPtr(self: *Elf, name: []const u8) !GetOrPutGlobalPtrResult { |
| 3436 | | if (self.getGlobalPtr(name)) |ptr| { |
| 3437 | | return GetOrPutGlobalPtrResult{ .found_existing = true, .value_ptr = ptr }; |
| 3419 | pub fn getOrPutGlobal(self: *Elf, name: []const u8) !GetOrPutGlobalResult { |
| 3420 | if (self.global(name)) |ptr| { |
| 3421 | return GetOrPutGlobalResult{ .found_existing = true, .value_ptr = ptr }; |
| 3438 | 3422 | } |
| 3439 | 3423 | const gpa = self.base.allocator; |
| 3440 | 3424 | const global_index = try self.allocateGlobal(); |
| 3441 | 3425 | const global_name = try gpa.dupe(u8, name); |
| 3442 | 3426 | _ = try self.resolver.put(gpa, global_name, global_index); |
| 3443 | 3427 | const ptr = &self.globals.items[global_index]; |
| 3444 | | return GetOrPutGlobalPtrResult{ .found_existing = false, .value_ptr = ptr }; |
| 3428 | return GetOrPutGlobalResult{ .found_existing = false, .value_ptr = ptr }; |
| 3445 | 3429 | } |
| 3446 | 3430 | |
| 3447 | | pub fn getAtom(self: *const Elf, atom_index: Atom.Index) Atom { |
| 3448 | | assert(atom_index < self.atoms.items.len); |
| 3449 | | return self.atoms.items[atom_index]; |
| 3450 | | } |
| 3451 | | |
| 3452 | | pub fn getAtomPtr(self: *Elf, atom_index: Atom.Index) *Atom { |
| 3431 | pub fn atom(self: *Elf, atom_index: Atom.Index) *Atom { |
| 3453 | 3432 | assert(atom_index < self.atoms.items.len); |
| 3454 | 3433 | return &self.atoms.items[atom_index]; |
| 3455 | 3434 | } |
| 3456 | 3435 | |
| 3457 | 3436 | /// Returns atom if there is an atom referenced by the symbol. |
| 3458 | 3437 | /// Returns null on failure. |
| 3459 | | pub fn getAtomIndexForSymbol(self: *Elf, sym_index: u32) ?Atom.Index { |
| 3438 | pub fn atomIndexForSymbol(self: *Elf, sym_index: u32) ?Atom.Index { |
| 3460 | 3439 | return self.atom_by_index_table.get(sym_index); |
| 3461 | 3440 | } |
| 3462 | 3441 | |
| ... | ... | @@ -3512,16 +3491,9 @@ const DeclMetadata = struct { |
| 3512 | 3491 | /// A list of all exports aliases of this Decl. |
| 3513 | 3492 | exports: std.ArrayListUnmanaged(u32) = .{}, |
| 3514 | 3493 | |
| 3515 | | fn getExport(m: DeclMetadata, elf_file: *const Elf, name: []const u8) ?u32 { |
| 3516 | | for (m.exports.items) |exp| { |
| 3517 | | if (mem.eql(u8, name, elf_file.getSymbolName(exp))) return exp; |
| 3518 | | } |
| 3519 | | return null; |
| 3520 | | } |
| 3521 | | |
| 3522 | | fn getExportPtr(m: *DeclMetadata, elf_file: *Elf, name: []const u8) ?*u32 { |
| 3494 | fn @"export"(m: DeclMetadata, elf_file: *const Elf, name: []const u8) ?*u32 { |
| 3523 | 3495 | for (m.exports.items) |*exp| { |
| 3524 | | if (mem.eql(u8, name, elf_file.getSymbolName(exp.*))) return exp; |
| 3496 | if (mem.eql(u8, name, elf_file.symbolName(exp.*))) return exp; |
| 3525 | 3497 | } |
| 3526 | 3498 | return null; |
| 3527 | 3499 | } |