| ... | @@ -836,14 +836,20 @@ pub fn populateMissingMetadata(self: *Elf) !void { | ... | @@ -836,14 +836,20 @@ pub fn populateMissingMetadata(self: *Elf) !void { |
| 836 | } }); | 836 | } }); |
| 837 | self.zig_module_index = index; | 837 | self.zig_module_index = index; |
| 838 | const zig_module = self.file(index).?.zig_module; | 838 | const zig_module = self.file(index).?.zig_module; |
| 839 | const sym_index = try zig_module.addLocal(self); | 839 | |
| 840 | const sym = self.symbol(sym_index); | | |
| 841 | const esym = zig_module.sourceSymbol(sym_index, self); | | |
| 842 | const name_off = try self.strtab.insert(gpa, std.fs.path.stem(module.main_pkg.root_src_path)); | 840 | const name_off = try self.strtab.insert(gpa, std.fs.path.stem(module.main_pkg.root_src_path)); |
| 843 | sym.name_offset = name_off; | 841 | const symbol_index = try self.addSymbol(); |
| | 842 | try zig_module.local_symbols.append(gpa, symbol_index); |
| | 843 | const symbol_ptr = self.symbol(symbol_index); |
| | 844 | symbol_ptr.file_index = zig_module.index; |
| | 845 | symbol_ptr.name_offset = name_off; |
| | 846 | |
| | 847 | const esym_index = try zig_module.addLocalEsym(gpa); |
| | 848 | const esym = &zig_module.local_esyms.items[esym_index]; |
| 844 | esym.st_name = name_off; | 849 | esym.st_name = name_off; |
| 845 | esym.st_info |= elf.STT_FILE; | 850 | esym.st_info |= elf.STT_FILE; |
| 846 | esym.st_shndx = elf.SHN_ABS; | 851 | esym.st_shndx = elf.SHN_ABS; |
| | 852 | symbol_ptr.esym_index = esym_index; |
| 847 | } | 853 | } |
| 848 | } | 854 | } |
| 849 | } | 855 | } |
| ... | @@ -2194,7 +2200,7 @@ pub fn getOrCreateMetadataForLazySymbol(self: *Elf, sym: link.File.LazySymbol) ! | ... | @@ -2194,7 +2200,7 @@ pub fn getOrCreateMetadataForLazySymbol(self: *Elf, sym: link.File.LazySymbol) ! |
| 2194 | }; | 2200 | }; |
| 2195 | const zig_module = self.file(self.zig_module_index.?).?.zig_module; | 2201 | const zig_module = self.file(self.zig_module_index.?).?.zig_module; |
| 2196 | switch (metadata.state.*) { | 2202 | switch (metadata.state.*) { |
| 2197 | .unused => metadata.symbol_index.* = try zig_module.createAtom(switch (sym.kind) { | 2203 | .unused => metadata.symbol_index.* = try zig_module.addAtom(switch (sym.kind) { |
| 2198 | .code => self.text_section_index.?, | 2204 | .code => self.text_section_index.?, |
| 2199 | .const_data => self.rodata_section_index.?, | 2205 | .const_data => self.rodata_section_index.?, |
| 2200 | }, self), | 2206 | }, self), |
| ... | @@ -2213,7 +2219,7 @@ pub fn getOrCreateMetadataForDecl(self: *Elf, decl_index: Module.Decl.Index) !Sy | ... | @@ -2213,7 +2219,7 @@ pub fn getOrCreateMetadataForDecl(self: *Elf, decl_index: Module.Decl.Index) !Sy |
| 2213 | if (!gop.found_existing) { | 2219 | if (!gop.found_existing) { |
| 2214 | const zig_module = self.file(self.zig_module_index.?).?.zig_module; | 2220 | const zig_module = self.file(self.zig_module_index.?).?.zig_module; |
| 2215 | gop.value_ptr.* = .{ | 2221 | gop.value_ptr.* = .{ |
| 2216 | .symbol_index = try zig_module.createAtom(self.getDeclShdrIndex(decl_index), self), | 2222 | .symbol_index = try zig_module.addAtom(self.getDeclShdrIndex(decl_index), self), |
| 2217 | .exports = .{}, | 2223 | .exports = .{}, |
| 2218 | }; | 2224 | }; |
| 2219 | } | 2225 | } |
| ... | @@ -2264,7 +2270,7 @@ fn updateDeclCode( | ... | @@ -2264,7 +2270,7 @@ fn updateDeclCode( |
| 2264 | const required_alignment = decl.getAlignment(mod); | 2270 | const required_alignment = decl.getAlignment(mod); |
| 2265 | | 2271 | |
| 2266 | const sym = self.symbol(sym_index); | 2272 | const sym = self.symbol(sym_index); |
| 2267 | const esym = zig_module.sourceSymbol(sym_index, self); | 2273 | const esym = &zig_module.local_esyms.items[sym.esym_index]; |
| 2268 | const atom_ptr = sym.atom(self).?; | 2274 | const atom_ptr = sym.atom(self).?; |
| 2269 | const shdr_index = sym.output_section_index; | 2275 | const shdr_index = sym.output_section_index; |
| 2270 | | 2276 | |
| ... | @@ -2521,7 +2527,7 @@ fn updateLazySymbol(self: *Elf, sym: link.File.LazySymbol, symbol_index: Symbol. | ... | @@ -2521,7 +2527,7 @@ fn updateLazySymbol(self: *Elf, sym: link.File.LazySymbol, symbol_index: Symbol. |
| 2521 | const local_sym = self.symbol(symbol_index); | 2527 | const local_sym = self.symbol(symbol_index); |
| 2522 | const phdr_index = self.phdr_to_shdr_table.get(local_sym.output_section_index).?; | 2528 | const phdr_index = self.phdr_to_shdr_table.get(local_sym.output_section_index).?; |
| 2523 | local_sym.name_offset = name_str_index; | 2529 | local_sym.name_offset = name_str_index; |
| 2524 | const local_esym = zig_module.sourceSymbol(symbol_index, self); | 2530 | const local_esym = &zig_module.local_esyms.items[local_sym.esym_index]; |
| 2525 | local_esym.st_name = name_str_index; | 2531 | local_esym.st_name = name_str_index; |
| 2526 | local_esym.st_info |= elf.STT_OBJECT; | 2532 | local_esym.st_info |= elf.STT_OBJECT; |
| 2527 | local_esym.st_size = code.len; | 2533 | local_esym.st_size = code.len; |
| ... | @@ -2575,7 +2581,7 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module | ... | @@ -2575,7 +2581,7 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module |
| 2575 | const name = self.strtab.get(name_str_index).?; | 2581 | const name = self.strtab.get(name_str_index).?; |
| 2576 | | 2582 | |
| 2577 | const zig_module = self.file(self.zig_module_index.?).?.zig_module; | 2583 | const zig_module = self.file(self.zig_module_index.?).?.zig_module; |
| 2578 | const sym_index = try zig_module.createAtom(self.rodata_section_index.?, self); | 2584 | const sym_index = try zig_module.addAtom(self.rodata_section_index.?, self); |
| 2579 | | 2585 | |
| 2580 | const res = try codegen.generateSymbol(&self.base, decl.srcLoc(mod), typed_value, &code_buffer, .{ | 2586 | const res = try codegen.generateSymbol(&self.base, decl.srcLoc(mod), typed_value, &code_buffer, .{ |
| 2581 | .none = {}, | 2587 | .none = {}, |
| ... | @@ -2597,7 +2603,7 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module | ... | @@ -2597,7 +2603,7 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: Module |
| 2597 | const phdr_index = self.phdr_to_shdr_table.get(shdr_index).?; | 2603 | const phdr_index = self.phdr_to_shdr_table.get(shdr_index).?; |
| 2598 | const local_sym = self.symbol(sym_index); | 2604 | const local_sym = self.symbol(sym_index); |
| 2599 | local_sym.name_offset = name_str_index; | 2605 | local_sym.name_offset = name_str_index; |
| 2600 | const local_esym = zig_module.sourceSymbol(sym_index, self); | 2606 | const local_esym = &zig_module.local_esyms.items[local_sym.esym_index]; |
| 2601 | local_esym.st_name = name_str_index; | 2607 | local_esym.st_name = name_str_index; |
| 2602 | local_esym.st_info |= elf.STT_OBJECT; | 2608 | local_esym.st_info |= elf.STT_OBJECT; |
| 2603 | local_esym.st_size = code.len; | 2609 | local_esym.st_size = code.len; |
| ... | @@ -2642,10 +2648,11 @@ pub fn updateDeclExports( | ... | @@ -2642,10 +2648,11 @@ pub fn updateDeclExports( |
| 2642 | | 2648 | |
| 2643 | const gpa = self.base.allocator; | 2649 | const gpa = self.base.allocator; |
| 2644 | | 2650 | |
| | 2651 | const zig_module = self.file(self.zig_module_index.?).?.zig_module; |
| 2645 | const decl = mod.declPtr(decl_index); | 2652 | const decl = mod.declPtr(decl_index); |
| 2646 | const decl_sym_index = try self.getOrCreateMetadataForDecl(decl_index); | 2653 | const decl_sym_index = try self.getOrCreateMetadataForDecl(decl_index); |
| 2647 | const decl_sym = self.symbol(decl_sym_index); | 2654 | const decl_sym = self.symbol(decl_sym_index); |
| 2648 | const decl_esym = decl_sym.sourceSymbol(self); | 2655 | const decl_esym = zig_module.local_esyms.items[decl_sym.esym_index]; |
| 2649 | const decl_metadata = self.decls.getPtr(decl_index).?; | 2656 | const decl_metadata = self.decls.getPtr(decl_index).?; |
| 2650 | | 2657 | |
| 2651 | for (exports) |exp| { | 2658 | for (exports) |exp| { |
| ... | @@ -2681,21 +2688,21 @@ pub fn updateDeclExports( | ... | @@ -2681,21 +2688,21 @@ pub fn updateDeclExports( |
| 2681 | }; | 2688 | }; |
| 2682 | const stt_bits: u8 = @as(u4, @truncate(decl_esym.st_info)); | 2689 | const stt_bits: u8 = @as(u4, @truncate(decl_esym.st_info)); |
| 2683 | | 2690 | |
| 2684 | const zig_module = self.file(self.zig_module_index.?).?.zig_module; | | |
| 2685 | const sym_index = if (decl_metadata.@"export"(self, exp_name)) |exp_index| exp_index.* else blk: { | 2691 | const sym_index = if (decl_metadata.@"export"(self, exp_name)) |exp_index| exp_index.* else blk: { |
| 2686 | const sym_index = try zig_module.addGlobal(exp_name, self); | 2692 | const sym_index = try zig_module.addGlobalEsym(gpa); |
| | 2693 | _ = try zig_module.global_symbols.addOne(gpa); |
| 2687 | try decl_metadata.exports.append(gpa, sym_index); | 2694 | try decl_metadata.exports.append(gpa, sym_index); |
| 2688 | break :blk sym_index; | 2695 | break :blk sym_index; |
| 2689 | }; | 2696 | }; |
| 2690 | const sym = self.symbol(sym_index); | 2697 | const name_off = try self.strtab.insert(gpa, exp_name); |
| 2691 | sym.flags.@"export" = true; | 2698 | const esym = &zig_module.global_esyms.items[sym_index]; |
| 2692 | sym.value = decl_sym.value; | 2699 | esym.st_value = decl_sym.value; |
| 2693 | sym.atom_index = decl_sym.atom_index; | 2700 | esym.st_shndx = decl_sym.output_section_index; |
| 2694 | sym.output_section_index = decl_sym.output_section_index; | | |
| 2695 | const esym = zig_module.sourceSymbol(sym_index, self); | | |
| 2696 | esym.* = decl_esym; | | |
| 2697 | esym.st_info = (stb_bits << 4) | stt_bits; | 2701 | esym.st_info = (stb_bits << 4) | stt_bits; |
| 2698 | _ = self.unresolved.swapRemove(sym_index); | 2702 | esym.st_name = name_off; |
| | 2703 | |
| | 2704 | const gop = try self.getOrPutGlobal(name_off); |
| | 2705 | zig_module.global_symbols.items[sym_index] = gop.index; |
| 2699 | } | 2706 | } |
| 2700 | } | 2707 | } |
| 2701 | | 2708 | |
| ... | @@ -2722,20 +2729,20 @@ pub fn deleteDeclExport( | ... | @@ -2722,20 +2729,20 @@ pub fn deleteDeclExport( |
| 2722 | ) void { | 2729 | ) void { |
| 2723 | if (self.llvm_object) |_| return; | 2730 | if (self.llvm_object) |_| return; |
| 2724 | const metadata = self.decls.getPtr(decl_index) orelse return; | 2731 | const metadata = self.decls.getPtr(decl_index) orelse return; |
| 2725 | const gpa = self.base.allocator; | | |
| 2726 | const mod = self.base.options.module.?; | 2732 | const mod = self.base.options.module.?; |
| 2727 | const zig_module = self.file(self.zig_module_index.?).?.zig_module; | 2733 | const zig_module = self.file(self.zig_module_index.?).?.zig_module; |
| 2728 | const exp_name = mod.intern_pool.stringToSlice(name); | 2734 | const exp_name = mod.intern_pool.stringToSlice(name); |
| 2729 | const sym_index = metadata.@"export"(self, exp_name) orelse return; | 2735 | const esym_index = metadata.@"export"(self, exp_name) orelse return; |
| 2730 | log.debug("deleting export '{s}'", .{exp_name}); | 2736 | log.debug("deleting export '{s}'", .{exp_name}); |
| 2731 | const sym = self.symbol(sym_index.*); | 2737 | const esym = &zig_module.global_esyms.items[esym_index.*]; |
| 2732 | const esym = zig_module.sourceSymbol(sym_index.*, self); | 2738 | _ = zig_module.globals_lookup.remove(esym.st_name); |
| 2733 | assert(self.resolver.fetchSwapRemove(sym.name_offset) != null); // TODO don't delete it if it's not dominant | 2739 | const sym_index = self.resolver.get(esym.st_name).?; |
| 2734 | sym.* = .{}; | 2740 | const sym = self.symbol(sym_index); |
| 2735 | // TODO free list for esym! | 2741 | if (sym.file_index == zig_module.index) { |
| | 2742 | _ = self.resolver.swapRemove(esym.st_name); |
| | 2743 | sym.* = .{}; |
| | 2744 | } |
| 2736 | esym.* = null_sym; | 2745 | esym.* = null_sym; |
| 2737 | self.symbols_free_list.append(gpa, sym_index.*) catch {}; | | |
| 2738 | sym_index.* = 0; | | |
| 2739 | } | 2746 | } |
| 2740 | | 2747 | |
| 2741 | fn addLinkerDefinedSymbols(self: *Elf) !void { | 2748 | fn addLinkerDefinedSymbols(self: *Elf) !void { |
| ... | @@ -3468,8 +3475,18 @@ pub fn globalByName(self: *Elf, name: []const u8) ?Symbol.Index { | ... | @@ -3468,8 +3475,18 @@ pub fn globalByName(self: *Elf, name: []const u8) ?Symbol.Index { |
| 3468 | | 3475 | |
| 3469 | pub fn getGlobalSymbol(self: *Elf, name: []const u8, lib_name: ?[]const u8) !u32 { | 3476 | pub fn getGlobalSymbol(self: *Elf, name: []const u8, lib_name: ?[]const u8) !u32 { |
| 3470 | _ = lib_name; | 3477 | _ = lib_name; |
| | 3478 | const gpa = self.base.allocator; |
| | 3479 | const off = try self.strtab.insert(gpa, name); |
| | 3480 | const gop = try self.getOrPutGlobal(off); |
| 3471 | const zig_module = self.file(self.zig_module_index.?).?.zig_module; | 3481 | const zig_module = self.file(self.zig_module_index.?).?.zig_module; |
| 3472 | return zig_module.addGlobal(name, self); | 3482 | const lookup_gop = try zig_module.globals_lookup.getOrPut(gpa, off); |
| | 3483 | if (!lookup_gop.found_existing) { |
| | 3484 | const esym_index = try zig_module.addGlobalEsym(gpa); |
| | 3485 | const esym = &zig_module.global_esyms.items[esym_index]; |
| | 3486 | esym.st_name = off; |
| | 3487 | lookup_gop.value_ptr.* = esym_index; |
| | 3488 | } |
| | 3489 | return gop.index; |
| 3473 | } | 3490 | } |
| 3474 | | 3491 | |
| 3475 | const GetOrCreateComdatGroupOwnerResult = struct { | 3492 | const GetOrCreateComdatGroupOwnerResult = struct { |
| ... | @@ -3753,8 +3770,10 @@ const DeclMetadata = struct { | ... | @@ -3753,8 +3770,10 @@ const DeclMetadata = struct { |
| 3753 | exports: std.ArrayListUnmanaged(Symbol.Index) = .{}, | 3770 | exports: std.ArrayListUnmanaged(Symbol.Index) = .{}, |
| 3754 | | 3771 | |
| 3755 | fn @"export"(m: DeclMetadata, elf_file: *Elf, name: []const u8) ?*u32 { | 3772 | fn @"export"(m: DeclMetadata, elf_file: *Elf, name: []const u8) ?*u32 { |
| | 3773 | const zig_module = elf_file.file(elf_file.zig_module_index.?).?.zig_module; |
| 3756 | for (m.exports.items) |*exp| { | 3774 | for (m.exports.items) |*exp| { |
| 3757 | if (mem.eql(u8, name, elf_file.symbol(exp.*).name(elf_file))) return exp; | 3775 | const exp_name = elf_file.strtab.getAssumeExists(zig_module.global_esyms.items[exp.*].st_name); |
| | 3776 | if (mem.eql(u8, name, exp_name)) return exp; |
| 3758 | } | 3777 | } |
| 3759 | return null; | 3778 | return null; |
| 3760 | } | 3779 | } |