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
18561856 assert(!self.imports_count_dirty);
18571857}
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 {
18601860 assert(self.llvm_object == null);
1861
1862 const this_atom_index = try self.getOrCreateAtomForDecl(decl_index);
1863 const sym_index = self.getAtom(this_atom_index).getSymbolIndex().?;
1861 const zcu = pt.zcu;
1862 const ip = &zcu.intern_pool;
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 };
18641876 const atom_index = self.getAtomIndexForSymbol(.{ .sym_index = reloc_info.parent_atom_index, .file = null }).?;
18651877 const target = SymbolWithLoc{ .sym_index = sym_index, .file = null };
18661878 try Atom.addRelocation(self, atom_index, .{
src/link/Elf.zig+2-2
......@@ -478,9 +478,9 @@ pub fn deinit(self: *Elf) void {
478478 self.comdat_group_sections.deinit(gpa);
479479}
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 {
482482 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);
484484}
485485
486486pub fn lowerAnonDecl(
src/link/Elf/ZigObject.zig+26-11
......@@ -579,6 +579,8 @@ pub fn updateSymtabSize(self: *ZigObject, elf_file: *Elf) !void {
579579 for (self.local_symbols.items) |index| {
580580 const local = &self.symbols.items[index];
581581 if (local.atom(elf_file)) |atom_ptr| if (!atom_ptr.alive) continue;
582 const name = local.name(elf_file);
583 assert(name.len > 0);
582584 const esym = local.elfSym(elf_file);
583585 switch (esym.st_type()) {
584586 elf.STT_SECTION, elf.STT_NOTYPE => continue,
......@@ -587,7 +589,7 @@ pub fn updateSymtabSize(self: *ZigObject, elf_file: *Elf) !void {
587589 local.flags.output_symtab = true;
588590 local.addExtra(.{ .symtab = self.output_symtab_ctx.nlocals }, elf_file);
589591 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;
591593 }
592594
593595 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
662664pub fn getDeclVAddr(
663665 self: *ZigObject,
664666 elf_file: *Elf,
667 pt: Zcu.PerThread,
665668 decl_index: InternPool.DeclIndex,
666669 reloc_info: link.File.RelocInfo,
667670) !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);
669683 const this_sym = self.symbol(this_sym_index);
670684 const vaddr = this_sym.address(.{}, elf_file);
671685 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
808822
809823pub fn freeDecl(self: *ZigObject, elf_file: *Elf, decl_index: InternPool.DeclIndex) void {
810824 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
816828 if (self.decls.fetchRemove(decl_index)) |const_kv| {
817829 var kv = const_kv;
......@@ -921,7 +933,7 @@ fn updateDeclCode(
921933 const ip = &mod.intern_pool;
922934 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
926938 const required_alignment = decl.getAlignment(pt).max(
927939 target_util.minFunctionAlignment(mod.getTarget()),
......@@ -1021,7 +1033,7 @@ fn updateTlv(
10211033 const gpa = mod.gpa;
10221034 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
10261038 const required_alignment = decl.getAlignment(pt);
10271039
......@@ -1075,11 +1087,14 @@ pub fn updateFunc(
10751087 defer tracy.end();
10761088
10771089 const mod = pt.zcu;
1090 const ip = &mod.intern_pool;
10781091 const gpa = elf_file.base.comp.gpa;
10791092 const func = mod.funcInfo(func_index);
10801093 const decl_index = func.owner_decl;
10811094 const decl = mod.declPtr(decl_index);
10821095
1096 log.debug("updateFunc {}({d})", .{ decl.fqn.fmt(ip), decl_index });
1097
10831098 const sym_index = try self.getOrCreateMetadataForDecl(elf_file, decl_index);
10841099 self.freeUnnamedConsts(elf_file, decl_index);
10851100 self.symbol(sym_index).atom(elf_file).?.freeRelocs(elf_file);
......@@ -1137,12 +1152,12 @@ pub fn updateDecl(
11371152 defer tracy.end();
11381153
11391154 const mod = pt.zcu;
1155 const ip = &mod.intern_pool;
11401156 const decl = mod.declPtr(decl_index);
11411157
1142 if (decl.val.getExternFunc(mod)) |_| {
1143 return;
1144 }
1158 log.debug("updateDecl {}({d})", .{ decl.fqn.fmt(ip), decl_index });
11451159
1160 if (decl.val.getExternFunc(mod)) |_| return;
11461161 if (decl.isExtern(mod)) {
11471162 // Extern variable gets a .got entry only.
11481163 const variable = decl.getOwnedVariable(mod).?;
......@@ -1483,7 +1498,7 @@ pub fn updateDeclLineNumber(
14831498
14841499 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
14881503 if (self.dwarf) |*dw| {
14891504 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 {
30423042 return self.getZigObject().?.freeDecl(decl_index);
30433043}
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 {
30463046 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);
30483048}
30493049
30503050pub fn lowerAnonDecl(
src/link/MachO/ZigObject.zig+16-2
......@@ -550,6 +550,8 @@ pub fn calcSymtabSize(self: *ZigObject, macho_file: *MachO) void {
550550 const file = ref.getFile(macho_file) orelse continue;
551551 if (file.getIndex() != self.index) continue;
552552 if (sym.getAtom(macho_file)) |atom| if (!atom.isAlive()) continue;
553 const name = sym.getName(macho_file);
554 assert(name.len > 0);
553555 sym.flags.output_symtab = true;
554556 if (sym.isLocal()) {
555557 sym.addExtra(.{ .symtab = self.output_symtab_ctx.nlocals }, macho_file);
......@@ -562,7 +564,7 @@ pub fn calcSymtabSize(self: *ZigObject, macho_file: *MachO) void {
562564 sym.addExtra(.{ .symtab = self.output_symtab_ctx.nimports }, macho_file);
563565 self.output_symtab_ctx.nimports += 1;
564566 }
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));
566568 }
567569}
568570
......@@ -692,10 +694,22 @@ pub fn flushModule(self: *ZigObject, macho_file: *MachO, tid: Zcu.PerThread.Id)
692694pub fn getDeclVAddr(
693695 self: *ZigObject,
694696 macho_file: *MachO,
697 pt: Zcu.PerThread,
695698 decl_index: InternPool.DeclIndex,
696699 reloc_info: link.File.RelocInfo,
697700) !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);
699713 const sym = self.symbols.items[sym_index];
700714 const vaddr = sym.getAddress(.{}, macho_file);
701715 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(
790790 reloc_info: link.File.RelocInfo,
791791) !u64 {
792792 const target = wasm_file.base.comp.root_mod.resolved_target.result;
793 const gpa = pt.zcu.gpa;
794 const decl = pt.zcu.declPtr(decl_index);
793 const zcu = pt.zcu;
794 const ip = &zcu.intern_pool;
795 const gpa = zcu.gpa;
796 const decl = zcu.declPtr(decl_index);
795797
796798 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
799810 std.debug.assert(reloc_info.parent_atom_index != 0);
800811 const atom_index = wasm_file.symbol_atom.get(.{ .file = zig_object.index, .index = @enumFromInt(reloc_info.parent_atom_index) }).?;