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 {
25772577 try self.offset_table.ensureUnusedCapacity(self.base.allocator, 1);
25782578 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});
25812584 decl.link.elf.local_sym_index = try self.allocateLocalSymbol();
25822585 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 {
26762679}
26772680
26782681fn 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 });
26802686 const required_alignment = decl.ty.abiAlignment(self.base.options.target);
26812687
26822688 const decl_ptr = self.decls.getPtr(decl).?;
......@@ -2694,7 +2700,7 @@ fn updateDeclCode(self: *Elf, decl: *Module.Decl, code: []const u8, stt_bits: u8
26942700 !mem.isAlignedGeneric(u64, local_sym.st_value, required_alignment);
26952701 if (need_realloc) {
26962702 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 });
26982704 if (vaddr != local_sym.st_value) {
26992705 local_sym.st_value = vaddr;
27002706
......@@ -2706,14 +2712,13 @@ fn updateDeclCode(self: *Elf, decl: *Module.Decl, code: []const u8, stt_bits: u8
27062712 self.shrinkTextBlock(&decl.link.elf, code.len, phdr_index);
27072713 }
27082714 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);
27102716 local_sym.st_info = (elf.STB_LOCAL << 4) | stt_bits;
27112717 local_sym.st_other = 0;
27122718 local_sym.st_shndx = shdr_index;
27132719 // TODO this write could be avoided if no fields of the symbol were changed.
27142720 try self.writeSymbol(decl.link.elf.local_sym_index);
27152721 } else {
2716 const decl_name = mem.sliceTo(decl.name, 0);
27172722 const name_str_index = try self.makeString(decl_name);
27182723 const vaddr = try self.allocateTextBlock(&decl.link.elf, code.len, required_alignment, phdr_index);
27192724 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
28232828 const decl = func.owner_decl;
28242829 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 });
28272835 log.debug(" (decl.src_line={d}, func.lbrace_line={d}, func.rbrace_line={d})", .{
28282836 decl.src_line,
28292837 func.lbrace_line,
......@@ -2860,7 +2868,7 @@ pub fn updateFunc(self: *Elf, module: *Module, func: *Module.Fn, air: Air, liven
28602868 dbg_line_buffer.appendAssumeCapacity(DW.LNS.copy);
28612869
28622870 // .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];
28642872 try dbg_info_buffer.ensureUnusedCapacity(25 + decl_name_with_null.len);
28652873
28662874 const fn_ret_type = decl.ty.fnReturnType();
......@@ -3107,9 +3115,13 @@ pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl: *Module.Decl
31073115 try self.managed_atoms.append(self.base.allocator, atom);
31083116
31093117 const name_str_index = blk: {
3118 const decl_name = try decl.getFullyQualifiedName(self.base.allocator);
3119 defer self.base.allocator.free(decl_name);
3120
31103121 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 });
31123123 defer self.base.allocator.free(name);
3124
31133125 break :blk try self.makeString(name);
31143126 };
31153127 const name = self.getString(name_str_index);
......@@ -3511,7 +3523,10 @@ pub fn updateDeclLineNumber(self: *Elf, module: *Module, decl: *const Module.Dec
35113523 const tracy = trace(@src());
35123524 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
35163531 if (self.llvm_object) |_| return;
35173532