authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-02-23 21:57:40+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-02-24 00:01:11+01:00
logffb7ac6755230264b35b236a440e21381040ad3b
tree8f5c6a2722b724ee01442ed02a58a27ddb6d6ce2
parent9d098318e2b16a35803d84b70b48c04111f2dddf

elf: use fully qualified decl names in the linker


1 files changed, 24 insertions(+), 9 deletions(-)

src/link/Elf.zig+24-9
...@@ -2577,7 +2577,10 @@ pub fn allocateDeclIndexes(self: *Elf, decl: *Module.Decl) !void {...@@ -2577,7 +2577,10 @@ pub fn allocateDeclIndexes(self: *Elf, decl: *Module.Decl) !void {
2577 try self.offset_table.ensureUnusedCapacity(self.base.allocator, 1);2577 try self.offset_table.ensureUnusedCapacity(self.base.allocator, 1);
2578 try self.decls.putNoClobber(self.base.allocator, decl, null);2578 try self.decls.putNoClobber(self.base.allocator, decl, null);
25792579
2580 log.debug("allocating symbol indexes for {s}", .{decl.name});2580 const decl_name = try decl.getFullyQualifiedName(self.base.allocator);
2581 defer self.base.allocator.free(decl_name);
2582
2583 log.debug("allocating symbol indexes for {s}", .{decl_name});
2581 decl.link.elf.local_sym_index = try self.allocateLocalSymbol();2584 decl.link.elf.local_sym_index = try self.allocateLocalSymbol();
2582 try self.atom_by_index_table.putNoClobber(self.base.allocator, decl.link.elf.local_sym_index, &decl.link.elf);2585 try self.atom_by_index_table.putNoClobber(self.base.allocator, decl.link.elf.local_sym_index, &decl.link.elf);
25832586
...@@ -2676,7 +2679,10 @@ fn getDeclPhdrIndex(self: *Elf, decl: *Module.Decl) !u16 {...@@ -2676,7 +2679,10 @@ fn getDeclPhdrIndex(self: *Elf, decl: *Module.Decl) !u16 {
2676}2679}
26772680
2678fn updateDeclCode(self: *Elf, decl: *Module.Decl, code: []const u8, stt_bits: u8) !*elf.Elf64_Sym {2681fn updateDeclCode(self: *Elf, decl: *Module.Decl, code: []const u8, stt_bits: u8) !*elf.Elf64_Sym {
2679 log.debug("updateDeclCode {s}{*}", .{ mem.sliceTo(decl.name, 0), decl });2682 const decl_name = try decl.getFullyQualifiedName(self.base.allocator);
2683 defer self.base.allocator.free(decl_name);
2684
2685 log.debug("updateDeclCode {s}{*}", .{ decl_name, decl });
2680 const required_alignment = decl.ty.abiAlignment(self.base.options.target);2686 const required_alignment = decl.ty.abiAlignment(self.base.options.target);
26812687
2682 const decl_ptr = self.decls.getPtr(decl).?;2688 const decl_ptr = self.decls.getPtr(decl).?;
...@@ -2694,7 +2700,7 @@ fn updateDeclCode(self: *Elf, decl: *Module.Decl, code: []const u8, stt_bits: u8...@@ -2694,7 +2700,7 @@ fn updateDeclCode(self: *Elf, decl: *Module.Decl, code: []const u8, stt_bits: u8
2694 !mem.isAlignedGeneric(u64, local_sym.st_value, required_alignment);2700 !mem.isAlignedGeneric(u64, local_sym.st_value, required_alignment);
2695 if (need_realloc) {2701 if (need_realloc) {
2696 const vaddr = try self.growTextBlock(&decl.link.elf, code.len, required_alignment, phdr_index);2702 const vaddr = try self.growTextBlock(&decl.link.elf, code.len, required_alignment, phdr_index);
2697 log.debug("growing {s} from 0x{x} to 0x{x}", .{ decl.name, local_sym.st_value, vaddr });2703 log.debug("growing {s} from 0x{x} to 0x{x}", .{ decl_name, local_sym.st_value, vaddr });
2698 if (vaddr != local_sym.st_value) {2704 if (vaddr != local_sym.st_value) {
2699 local_sym.st_value = vaddr;2705 local_sym.st_value = vaddr;
27002706
...@@ -2706,14 +2712,13 @@ fn updateDeclCode(self: *Elf, decl: *Module.Decl, code: []const u8, stt_bits: u8...@@ -2706,14 +2712,13 @@ fn updateDeclCode(self: *Elf, decl: *Module.Decl, code: []const u8, stt_bits: u8
2706 self.shrinkTextBlock(&decl.link.elf, code.len, phdr_index);2712 self.shrinkTextBlock(&decl.link.elf, code.len, phdr_index);
2707 }2713 }
2708 local_sym.st_size = code.len;2714 local_sym.st_size = code.len;
2709 local_sym.st_name = try self.updateString(local_sym.st_name, mem.sliceTo(decl.name, 0));2715 local_sym.st_name = try self.updateString(local_sym.st_name, decl_name);
2710 local_sym.st_info = (elf.STB_LOCAL << 4) | stt_bits;2716 local_sym.st_info = (elf.STB_LOCAL << 4) | stt_bits;
2711 local_sym.st_other = 0;2717 local_sym.st_other = 0;
2712 local_sym.st_shndx = shdr_index;2718 local_sym.st_shndx = shdr_index;
2713 // TODO this write could be avoided if no fields of the symbol were changed.2719 // TODO this write could be avoided if no fields of the symbol were changed.
2714 try self.writeSymbol(decl.link.elf.local_sym_index);2720 try self.writeSymbol(decl.link.elf.local_sym_index);
2715 } else {2721 } else {
2716 const decl_name = mem.sliceTo(decl.name, 0);
2717 const name_str_index = try self.makeString(decl_name);2722 const name_str_index = try self.makeString(decl_name);
2718 const vaddr = try self.allocateTextBlock(&decl.link.elf, code.len, required_alignment, phdr_index);2723 const vaddr = try self.allocateTextBlock(&decl.link.elf, code.len, required_alignment, phdr_index);
2719 errdefer self.freeTextBlock(&decl.link.elf, phdr_index);2724 errdefer self.freeTextBlock(&decl.link.elf, phdr_index);
...@@ -2823,7 +2828,10 @@ pub fn updateFunc(self: *Elf, module: *Module, func: *Module.Fn, air: Air, liven...@@ -2823,7 +2828,10 @@ pub fn updateFunc(self: *Elf, module: *Module, func: *Module.Fn, air: Air, liven
2823 const decl = func.owner_decl;2828 const decl = func.owner_decl;
2824 self.freeUnnamedConsts(decl);2829 self.freeUnnamedConsts(decl);
28252830
2826 log.debug("updateFunc {s}{*}", .{ decl.name, func.owner_decl });2831 const decl_name = try decl.getFullyQualifiedName(self.base.allocator);
2832 defer self.base.allocator.free(decl_name);
2833
2834 log.debug("updateFunc {s}{*}", .{ decl_name, func.owner_decl });
2827 log.debug(" (decl.src_line={d}, func.lbrace_line={d}, func.rbrace_line={d})", .{2835 log.debug(" (decl.src_line={d}, func.lbrace_line={d}, func.rbrace_line={d})", .{
2828 decl.src_line,2836 decl.src_line,
2829 func.lbrace_line,2837 func.lbrace_line,
...@@ -2860,7 +2868,7 @@ pub fn updateFunc(self: *Elf, module: *Module, func: *Module.Fn, air: Air, liven...@@ -2860,7 +2868,7 @@ pub fn updateFunc(self: *Elf, module: *Module, func: *Module.Fn, air: Air, liven
2860 dbg_line_buffer.appendAssumeCapacity(DW.LNS.copy);2868 dbg_line_buffer.appendAssumeCapacity(DW.LNS.copy);
28612869
2862 // .debug_info subprogram2870 // .debug_info subprogram
2863 const decl_name_with_null = decl.name[0 .. mem.sliceTo(decl.name, 0).len + 1];2871 const decl_name_with_null = decl_name[0 .. decl_name.len + 1];
2864 try dbg_info_buffer.ensureUnusedCapacity(25 + decl_name_with_null.len);2872 try dbg_info_buffer.ensureUnusedCapacity(25 + decl_name_with_null.len);
28652873
2866 const fn_ret_type = decl.ty.fnReturnType();2874 const fn_ret_type = decl.ty.fnReturnType();
...@@ -3107,9 +3115,13 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl: *Module.Decl...@@ -3107,9 +3115,13 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl: *Module.Decl
3107 try self.managed_atoms.append(self.base.allocator, atom);3115 try self.managed_atoms.append(self.base.allocator, atom);
31083116
3109 const name_str_index = blk: {3117 const name_str_index = blk: {
3118 const decl_name = try decl.getFullyQualifiedName(self.base.allocator);
3119 defer self.base.allocator.free(decl_name);
3120
3110 const index = unnamed_consts.items.len;3121 const index = unnamed_consts.items.len;
3111 const name = try std.fmt.allocPrint(self.base.allocator, "__unnamed_{s}_{d}", .{ decl.name, index });3122 const name = try std.fmt.allocPrint(self.base.allocator, "__unnamed_{s}_{d}", .{ decl_name, index });
3112 defer self.base.allocator.free(name);3123 defer self.base.allocator.free(name);
3124
3113 break :blk try self.makeString(name);3125 break :blk try self.makeString(name);
3114 };3126 };
3115 const name = self.getString(name_str_index);3127 const name = self.getString(name_str_index);
...@@ -3511,7 +3523,10 @@ pub fn updateDeclLineNumber(self: *Elf, module: *Module, decl: *const Module.Dec...@@ -3511,7 +3523,10 @@ pub fn updateDeclLineNumber(self: *Elf, module: *Module, decl: *const Module.Dec
3511 const tracy = trace(@src());3523 const tracy = trace(@src());
3512 defer tracy.end();3524 defer tracy.end();
35133525
3514 log.debug("updateDeclLineNumber {s}{*}", .{ decl.name, decl });3526 const decl_name = try decl.getFullyQualifiedName(self.base.allocator);
3527 defer self.base.allocator.free(decl_name);
3528
3529 log.debug("updateDeclLineNumber {s}{*}", .{ decl_name, decl });
35153530
3516 if (self.llvm_object) |_| return;3531 if (self.llvm_object) |_| return;
35173532