authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-08-11 00:46:05+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-08-11 00:46:05+01:00
logb7c01120b41c764e9e453114c3ba840127e60cdd
treedd70c7a5bd3fda5403626ec7cc59affb4be248e8
parent6d54f20c4713c564cfe65a84683b10b445a7d34c
parent5dffd8cb7b5bfa68c9c0cbc1c7dc3590a6e0a42f
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #21018 from ziglang/behavior-test-extern-disable

link: handle pointers to extern symbols

6 files changed, 76 insertions(+), 24 deletions(-)

src/link/Coff.zig+16-4
...@@ -1856,11 +1856,23 @@ pub fn flushModule(self: *Coff, arena: Allocator, tid: Zcu.PerThread.Id, prog_no...@@ -1856,11 +1856,23 @@ pub fn flushModule(self: *Coff, arena: Allocator, tid: Zcu.PerThread.Id, prog_no
1856 assert(!self.imports_count_dirty);1856 assert(!self.imports_count_dirty);
1857}1857}
18581858
1859pub fn getDeclVAddr(self: *Coff, _: Zcu.PerThread, decl_index: InternPool.DeclIndex, reloc_info: link.File.RelocInfo) !u64 {1859pub fn getDeclVAddr(self: *Coff, pt: Zcu.PerThread, decl_index: InternPool.DeclIndex, reloc_info: link.File.RelocInfo) !u64 {
1860 assert(self.llvm_object == null);1860 assert(self.llvm_object == null);
18611861 const zcu = pt.zcu;
1862 const this_atom_index = try self.getOrCreateAtomForDecl(decl_index);1862 const ip = &zcu.intern_pool;
1863 const sym_index = self.getAtom(this_atom_index).getSymbolIndex().?;1863 const decl = zcu.declPtr(decl_index);
1864 log.debug("getDeclVAddr {}({d})", .{ decl.fqn.fmt(ip), decl_index });
1865 const sym_index = if (decl.isExtern(zcu)) blk: {
1866 const name = decl.name.toSlice(ip);
1867 const lib_name = if (decl.getOwnedExternFunc(zcu)) |ext_fn|
1868 ext_fn.lib_name.toSlice(ip)
1869 else
1870 decl.getOwnedVariable(zcu).?.lib_name.toSlice(ip);
1871 break :blk try self.getGlobalSymbol(name, lib_name);
1872 } else blk: {
1873 const this_atom_index = try self.getOrCreateAtomForDecl(decl_index);
1874 break :blk self.getAtom(this_atom_index).getSymbolIndex().?;
1875 };
1864 const atom_index = self.getAtomIndexForSymbol(.{ .sym_index = reloc_info.parent_atom_index, .file = null }).?;1876 const atom_index = self.getAtomIndexForSymbol(.{ .sym_index = reloc_info.parent_atom_index, .file = null }).?;
1865 const target = SymbolWithLoc{ .sym_index = sym_index, .file = null };1877 const target = SymbolWithLoc{ .sym_index = sym_index, .file = null };
1866 try Atom.addRelocation(self, atom_index, .{1878 try Atom.addRelocation(self, atom_index, .{
src/link/Elf.zig+2-2
...@@ -478,9 +478,9 @@ pub fn deinit(self: *Elf) void {...@@ -478,9 +478,9 @@ pub fn deinit(self: *Elf) void {
478 self.comdat_group_sections.deinit(gpa);478 self.comdat_group_sections.deinit(gpa);
479}479}
480480
481pub fn getDeclVAddr(self: *Elf, _: Zcu.PerThread, decl_index: InternPool.DeclIndex, reloc_info: link.File.RelocInfo) !u64 {481pub fn getDeclVAddr(self: *Elf, pt: Zcu.PerThread, decl_index: InternPool.DeclIndex, reloc_info: link.File.RelocInfo) !u64 {
482 assert(self.llvm_object == null);482 assert(self.llvm_object == null);
483 return self.zigObjectPtr().?.getDeclVAddr(self, decl_index, reloc_info);483 return self.zigObjectPtr().?.getDeclVAddr(self, pt, decl_index, reloc_info);
484}484}
485485
486pub fn lowerAnonDecl(486pub fn lowerAnonDecl(
src/link/Elf/ZigObject.zig+26-11
...@@ -579,6 +579,8 @@ pub fn updateSymtabSize(self: *ZigObject, elf_file: *Elf) !void {...@@ -579,6 +579,8 @@ pub fn updateSymtabSize(self: *ZigObject, elf_file: *Elf) !void {
579 for (self.local_symbols.items) |index| {579 for (self.local_symbols.items) |index| {
580 const local = &self.symbols.items[index];580 const local = &self.symbols.items[index];
581 if (local.atom(elf_file)) |atom_ptr| if (!atom_ptr.alive) continue;581 if (local.atom(elf_file)) |atom_ptr| if (!atom_ptr.alive) continue;
582 const name = local.name(elf_file);
583 assert(name.len > 0);
582 const esym = local.elfSym(elf_file);584 const esym = local.elfSym(elf_file);
583 switch (esym.st_type()) {585 switch (esym.st_type()) {
584 elf.STT_SECTION, elf.STT_NOTYPE => continue,586 elf.STT_SECTION, elf.STT_NOTYPE => continue,
...@@ -587,7 +589,7 @@ pub fn updateSymtabSize(self: *ZigObject, elf_file: *Elf) !void {...@@ -587,7 +589,7 @@ pub fn updateSymtabSize(self: *ZigObject, elf_file: *Elf) !void {
587 local.flags.output_symtab = true;589 local.flags.output_symtab = true;
588 local.addExtra(.{ .symtab = self.output_symtab_ctx.nlocals }, elf_file);590 local.addExtra(.{ .symtab = self.output_symtab_ctx.nlocals }, elf_file);
589 self.output_symtab_ctx.nlocals += 1;591 self.output_symtab_ctx.nlocals += 1;
590 self.output_symtab_ctx.strsize += @as(u32, @intCast(local.name(elf_file).len)) + 1;592 self.output_symtab_ctx.strsize += @as(u32, @intCast(name.len)) + 1;
591 }593 }
592594
593 for (self.global_symbols.items, self.symbols_resolver.items) |index, resolv| {595 for (self.global_symbols.items, self.symbols_resolver.items) |index, resolv| {
...@@ -662,10 +664,22 @@ pub fn codeAlloc(self: *ZigObject, elf_file: *Elf, atom_index: Atom.Index) ![]u8...@@ -662,10 +664,22 @@ pub fn codeAlloc(self: *ZigObject, elf_file: *Elf, atom_index: Atom.Index) ![]u8
662pub fn getDeclVAddr(664pub fn getDeclVAddr(
663 self: *ZigObject,665 self: *ZigObject,
664 elf_file: *Elf,666 elf_file: *Elf,
667 pt: Zcu.PerThread,
665 decl_index: InternPool.DeclIndex,668 decl_index: InternPool.DeclIndex,
666 reloc_info: link.File.RelocInfo,669 reloc_info: link.File.RelocInfo,
667) !u64 {670) !u64 {
668 const this_sym_index = try self.getOrCreateMetadataForDecl(elf_file, decl_index);671 const zcu = pt.zcu;
672 const ip = &zcu.intern_pool;
673 const decl = zcu.declPtr(decl_index);
674 log.debug("getDeclVAddr {}({d})", .{ decl.fqn.fmt(ip), decl_index });
675 const this_sym_index = if (decl.isExtern(zcu)) blk: {
676 const name = decl.name.toSlice(ip);
677 const lib_name = if (decl.getOwnedExternFunc(zcu)) |ext_fn|
678 ext_fn.lib_name.toSlice(ip)
679 else
680 decl.getOwnedVariable(zcu).?.lib_name.toSlice(ip);
681 break :blk try self.getGlobalSymbol(elf_file, name, lib_name);
682 } else try self.getOrCreateMetadataForDecl(elf_file, decl_index);
669 const this_sym = self.symbol(this_sym_index);683 const this_sym = self.symbol(this_sym_index);
670 const vaddr = this_sym.address(.{}, elf_file);684 const vaddr = this_sym.address(.{}, elf_file);
671 const parent_atom = self.symbol(reloc_info.parent_atom_index).atom(elf_file).?;685 const parent_atom = self.symbol(reloc_info.parent_atom_index).atom(elf_file).?;
...@@ -808,10 +822,8 @@ fn freeDeclMetadata(self: *ZigObject, elf_file: *Elf, sym_index: Symbol.Index) v...@@ -808,10 +822,8 @@ fn freeDeclMetadata(self: *ZigObject, elf_file: *Elf, sym_index: Symbol.Index) v
808822
809pub fn freeDecl(self: *ZigObject, elf_file: *Elf, decl_index: InternPool.DeclIndex) void {823pub fn freeDecl(self: *ZigObject, elf_file: *Elf, decl_index: InternPool.DeclIndex) void {
810 const gpa = elf_file.base.comp.gpa;824 const gpa = elf_file.base.comp.gpa;
811 const mod = elf_file.base.comp.module.?;
812 const decl = mod.declPtr(decl_index);
813825
814 log.debug("freeDecl {*}", .{decl});826 log.debug("freeDecl ({d})", .{decl_index});
815827
816 if (self.decls.fetchRemove(decl_index)) |const_kv| {828 if (self.decls.fetchRemove(decl_index)) |const_kv| {
817 var kv = const_kv;829 var kv = const_kv;
...@@ -921,7 +933,7 @@ fn updateDeclCode(...@@ -921,7 +933,7 @@ fn updateDeclCode(
921 const ip = &mod.intern_pool;933 const ip = &mod.intern_pool;
922 const decl = mod.declPtr(decl_index);934 const decl = mod.declPtr(decl_index);
923935
924 log.debug("updateDeclCode {}{*}", .{ decl.fqn.fmt(ip), decl });936 log.debug("updateDeclCode {}({d})", .{ decl.fqn.fmt(ip), decl_index });
925937
926 const required_alignment = decl.getAlignment(pt).max(938 const required_alignment = decl.getAlignment(pt).max(
927 target_util.minFunctionAlignment(mod.getTarget()),939 target_util.minFunctionAlignment(mod.getTarget()),
...@@ -1021,7 +1033,7 @@ fn updateTlv(...@@ -1021,7 +1033,7 @@ fn updateTlv(
1021 const gpa = mod.gpa;1033 const gpa = mod.gpa;
1022 const decl = mod.declPtr(decl_index);1034 const decl = mod.declPtr(decl_index);
10231035
1024 log.debug("updateTlv {} ({*})", .{ decl.fqn.fmt(ip), decl });1036 log.debug("updateTlv {}({d})", .{ decl.fqn.fmt(ip), decl_index });
10251037
1026 const required_alignment = decl.getAlignment(pt);1038 const required_alignment = decl.getAlignment(pt);
10271039
...@@ -1075,11 +1087,14 @@ pub fn updateFunc(...@@ -1075,11 +1087,14 @@ pub fn updateFunc(
1075 defer tracy.end();1087 defer tracy.end();
10761088
1077 const mod = pt.zcu;1089 const mod = pt.zcu;
1090 const ip = &mod.intern_pool;
1078 const gpa = elf_file.base.comp.gpa;1091 const gpa = elf_file.base.comp.gpa;
1079 const func = mod.funcInfo(func_index);1092 const func = mod.funcInfo(func_index);
1080 const decl_index = func.owner_decl;1093 const decl_index = func.owner_decl;
1081 const decl = mod.declPtr(decl_index);1094 const decl = mod.declPtr(decl_index);
10821095
1096 log.debug("updateFunc {}({d})", .{ decl.fqn.fmt(ip), decl_index });
1097
1083 const sym_index = try self.getOrCreateMetadataForDecl(elf_file, decl_index);1098 const sym_index = try self.getOrCreateMetadataForDecl(elf_file, decl_index);
1084 self.freeUnnamedConsts(elf_file, decl_index);1099 self.freeUnnamedConsts(elf_file, decl_index);
1085 self.symbol(sym_index).atom(elf_file).?.freeRelocs(elf_file);1100 self.symbol(sym_index).atom(elf_file).?.freeRelocs(elf_file);
...@@ -1137,12 +1152,12 @@ pub fn updateDecl(...@@ -1137,12 +1152,12 @@ pub fn updateDecl(
1137 defer tracy.end();1152 defer tracy.end();
11381153
1139 const mod = pt.zcu;1154 const mod = pt.zcu;
1155 const ip = &mod.intern_pool;
1140 const decl = mod.declPtr(decl_index);1156 const decl = mod.declPtr(decl_index);
11411157
1142 if (decl.val.getExternFunc(mod)) |_| {1158 log.debug("updateDecl {}({d})", .{ decl.fqn.fmt(ip), decl_index });
1143 return;
1144 }
11451159
1160 if (decl.val.getExternFunc(mod)) |_| return;
1146 if (decl.isExtern(mod)) {1161 if (decl.isExtern(mod)) {
1147 // Extern variable gets a .got entry only.1162 // Extern variable gets a .got entry only.
1148 const variable = decl.getOwnedVariable(mod).?;1163 const variable = decl.getOwnedVariable(mod).?;
...@@ -1483,7 +1498,7 @@ pub fn updateDeclLineNumber(...@@ -1483,7 +1498,7 @@ pub fn updateDeclLineNumber(
14831498
1484 const decl = pt.zcu.declPtr(decl_index);1499 const decl = pt.zcu.declPtr(decl_index);
14851500
1486 log.debug("updateDeclLineNumber {}{*}", .{ decl.fqn.fmt(&pt.zcu.intern_pool), decl });1501 log.debug("updateDeclLineNumber {}({d})", .{ decl.fqn.fmt(&pt.zcu.intern_pool), decl_index });
14871502
1488 if (self.dwarf) |*dw| {1503 if (self.dwarf) |*dw| {
1489 try dw.updateDeclLineNumber(pt.zcu, decl_index);1504 try dw.updateDeclLineNumber(pt.zcu, decl_index);
src/link/MachO.zig+2-2
...@@ -3042,9 +3042,9 @@ pub fn freeDecl(self: *MachO, decl_index: InternPool.DeclIndex) void {...@@ -3042,9 +3042,9 @@ pub fn freeDecl(self: *MachO, decl_index: InternPool.DeclIndex) void {
3042 return self.getZigObject().?.freeDecl(decl_index);3042 return self.getZigObject().?.freeDecl(decl_index);
3043}3043}
30443044
3045pub fn getDeclVAddr(self: *MachO, _: Zcu.PerThread, decl_index: InternPool.DeclIndex, reloc_info: link.File.RelocInfo) !u64 {3045pub fn getDeclVAddr(self: *MachO, pt: Zcu.PerThread, decl_index: InternPool.DeclIndex, reloc_info: link.File.RelocInfo) !u64 {
3046 assert(self.llvm_object == null);3046 assert(self.llvm_object == null);
3047 return self.getZigObject().?.getDeclVAddr(self, decl_index, reloc_info);3047 return self.getZigObject().?.getDeclVAddr(self, pt, decl_index, reloc_info);
3048}3048}
30493049
3050pub fn lowerAnonDecl(3050pub fn lowerAnonDecl(
src/link/MachO/ZigObject.zig+16-2
...@@ -550,6 +550,8 @@ pub fn calcSymtabSize(self: *ZigObject, macho_file: *MachO) void {...@@ -550,6 +550,8 @@ pub fn calcSymtabSize(self: *ZigObject, macho_file: *MachO) void {
550 const file = ref.getFile(macho_file) orelse continue;550 const file = ref.getFile(macho_file) orelse continue;
551 if (file.getIndex() != self.index) continue;551 if (file.getIndex() != self.index) continue;
552 if (sym.getAtom(macho_file)) |atom| if (!atom.isAlive()) continue;552 if (sym.getAtom(macho_file)) |atom| if (!atom.isAlive()) continue;
553 const name = sym.getName(macho_file);
554 assert(name.len > 0);
553 sym.flags.output_symtab = true;555 sym.flags.output_symtab = true;
554 if (sym.isLocal()) {556 if (sym.isLocal()) {
555 sym.addExtra(.{ .symtab = self.output_symtab_ctx.nlocals }, macho_file);557 sym.addExtra(.{ .symtab = self.output_symtab_ctx.nlocals }, macho_file);
...@@ -562,7 +564,7 @@ pub fn calcSymtabSize(self: *ZigObject, macho_file: *MachO) void {...@@ -562,7 +564,7 @@ pub fn calcSymtabSize(self: *ZigObject, macho_file: *MachO) void {
562 sym.addExtra(.{ .symtab = self.output_symtab_ctx.nimports }, macho_file);564 sym.addExtra(.{ .symtab = self.output_symtab_ctx.nimports }, macho_file);
563 self.output_symtab_ctx.nimports += 1;565 self.output_symtab_ctx.nimports += 1;
564 }566 }
565 self.output_symtab_ctx.strsize += @as(u32, @intCast(sym.getName(macho_file).len + 1));567 self.output_symtab_ctx.strsize += @as(u32, @intCast(name.len + 1));
566 }568 }
567}569}
568570
...@@ -692,10 +694,22 @@ pub fn flushModule(self: *ZigObject, macho_file: *MachO, tid: Zcu.PerThread.Id)...@@ -692,10 +694,22 @@ pub fn flushModule(self: *ZigObject, macho_file: *MachO, tid: Zcu.PerThread.Id)
692pub fn getDeclVAddr(694pub fn getDeclVAddr(
693 self: *ZigObject,695 self: *ZigObject,
694 macho_file: *MachO,696 macho_file: *MachO,
697 pt: Zcu.PerThread,
695 decl_index: InternPool.DeclIndex,698 decl_index: InternPool.DeclIndex,
696 reloc_info: link.File.RelocInfo,699 reloc_info: link.File.RelocInfo,
697) !u64 {700) !u64 {
698 const sym_index = try self.getOrCreateMetadataForDecl(macho_file, decl_index);701 const zcu = pt.zcu;
702 const ip = &zcu.intern_pool;
703 const decl = zcu.declPtr(decl_index);
704 log.debug("getDeclVAddr {}({d})", .{ decl.fqn.fmt(ip), decl_index });
705 const sym_index = if (decl.isExtern(zcu)) blk: {
706 const name = decl.name.toSlice(ip);
707 const lib_name = if (decl.getOwnedExternFunc(zcu)) |ext_fn|
708 ext_fn.lib_name.toSlice(ip)
709 else
710 decl.getOwnedVariable(zcu).?.lib_name.toSlice(ip);
711 break :blk try self.getGlobalSymbol(macho_file, name, lib_name);
712 } else try self.getOrCreateMetadataForDecl(macho_file, decl_index);
699 const sym = self.symbols.items[sym_index];713 const sym = self.symbols.items[sym_index];
700 const vaddr = sym.getAddress(.{}, macho_file);714 const vaddr = sym.getAddress(.{}, macho_file);
701 const parent_atom = self.symbols.items[reloc_info.parent_atom_index].getAtom(macho_file).?;715 const parent_atom = self.symbols.items[reloc_info.parent_atom_index].getAtom(macho_file).?;
src/link/Wasm/ZigObject.zig+14-3
...@@ -790,11 +790,22 @@ pub fn getDeclVAddr(...@@ -790,11 +790,22 @@ pub fn getDeclVAddr(
790 reloc_info: link.File.RelocInfo,790 reloc_info: link.File.RelocInfo,
791) !u64 {791) !u64 {
792 const target = wasm_file.base.comp.root_mod.resolved_target.result;792 const target = wasm_file.base.comp.root_mod.resolved_target.result;
793 const gpa = pt.zcu.gpa;793 const zcu = pt.zcu;
794 const decl = pt.zcu.declPtr(decl_index);794 const ip = &zcu.intern_pool;
795 const gpa = zcu.gpa;
796 const decl = zcu.declPtr(decl_index);
795797
796 const target_atom_index = try zig_object.getOrCreateAtomForDecl(wasm_file, pt, decl_index);798 const target_atom_index = try zig_object.getOrCreateAtomForDecl(wasm_file, pt, decl_index);
797 const target_symbol_index = @intFromEnum(wasm_file.getAtom(target_atom_index).sym_index);799 const target_atom = wasm_file.getAtom(target_atom_index);
800 const target_symbol_index = @intFromEnum(target_atom.sym_index);
801 if (decl.isExtern(zcu)) {
802 const name = decl.name.toSlice(ip);
803 const lib_name = if (decl.getOwnedExternFunc(zcu)) |ext_fn|
804 ext_fn.lib_name.toSlice(ip)
805 else
806 decl.getOwnedVariable(zcu).?.lib_name.toSlice(ip);
807 try zig_object.addOrUpdateImport(wasm_file, name, target_atom.sym_index, lib_name, null);
808 }
798809
799 std.debug.assert(reloc_info.parent_atom_index != 0);810 std.debug.assert(reloc_info.parent_atom_index != 0);
800 const atom_index = wasm_file.symbol_atom.get(.{ .file = zig_object.index, .index = @enumFromInt(reloc_info.parent_atom_index) }).?;811 const atom_index = wasm_file.symbol_atom.get(.{ .file = zig_object.index, .index = @enumFromInt(reloc_info.parent_atom_index) }).?;