From 7c87f9c828282aa12fb2d77c9c6a2318d83b8dff Mon Sep 17 00:00:00 2001 From: Luuk de Gram Date: Thu, 23 Jun 2022 16:20:20 +0200 Subject: [PATCH] link:clarification & enable MachO getGlobalSymbol This adds clarification to the getGlobalSymbol doc comments, as well as renames the `addExternFn` function for MachO to `getGlobalSymbol`. This function will now be called from 'src/link.zig' as well. Finally, this also enables compiling zig's libc using LLVM even though the `fno-LLVM` flag is given. --- src/Compilation.zig | 3 +-- src/arch/aarch64/CodeGen.zig | 2 +- src/arch/x86_64/CodeGen.zig | 2 +- src/link.zig | 5 +++-- src/link/MachO.zig | 2 +- src/link/Wasm.zig | 3 ++- 6 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/Compilation.zig b/src/Compilation.zig index 0a97ee9f7e..8cb93b5473 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -1920,8 +1920,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation { // and make sure the compiler-rt symbols are emitted. const capable_of_building_compiler_rt = build_options.have_llvm; - const capable_of_building_zig_libc = comp.bin_file.options.use_stage1 or - comp.bin_file.options.use_llvm; + const capable_of_building_zig_libc = build_options.have_llvm; const capable_of_building_ssp = comp.bin_file.options.use_stage1; if (comp.bin_file.options.include_compiler_rt and capable_of_building_compiler_rt) { diff --git a/src/arch/aarch64/CodeGen.zig b/src/arch/aarch64/CodeGen.zig index e74fbd44ac..4cf428bd9a 100644 --- a/src/arch/aarch64/CodeGen.zig +++ b/src/arch/aarch64/CodeGen.zig @@ -3188,7 +3188,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions. lib_name, }); } - const n_strx = try macho_file.addExternFn(mem.sliceTo(decl_name, 0)); + const n_strx = try macho_file.getGlobalSymbol(mem.sliceTo(decl_name, 0)); _ = try self.addInst(.{ .tag = .call_extern, diff --git a/src/arch/x86_64/CodeGen.zig b/src/arch/x86_64/CodeGen.zig index c94eaa37af..5c30d495c7 100644 --- a/src/arch/x86_64/CodeGen.zig +++ b/src/arch/x86_64/CodeGen.zig @@ -3996,7 +3996,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions. lib_name, }); } - const n_strx = try macho_file.addExternFn(mem.sliceTo(decl_name, 0)); + const n_strx = try macho_file.getGlobalSymbol(mem.sliceTo(decl_name, 0)); _ = try self.addInst(.{ .tag = .call_extern, .ops = undefined, diff --git a/src/link.zig b/src/link.zig index 8bad44aa21..c78fb8c2c5 100644 --- a/src/link.zig +++ b/src/link.zig @@ -439,14 +439,15 @@ pub const File = struct { } /// Called from within CodeGen to retrieve the symbol index of a global symbol. - /// If no symbol exists yet with this name, a new one will be created instead. + /// If no symbol exists yet with this name, a new undefined global symbol will + /// be created. This symbol may get resolved once all relocatables are (re-)linked. pub fn getGlobalSymbol(base: *File, name: []const u8) UpdateDeclError!u32 { log.debug("getGlobalSymbol '{s}'", .{name}); switch (base.tag) { // zig fmt: off .coff => unreachable, .elf => unreachable, - .macho => unreachable, + .macho => return @fieldParentPtr(MachO, "base", base).getGlobalSymbol(name), .plan9 => unreachable, .spirv => unreachable, .c => unreachable, diff --git a/src/link/MachO.zig b/src/link/MachO.zig index a7681e976d..0c44901d29 100644 --- a/src/link/MachO.zig +++ b/src/link/MachO.zig @@ -5366,7 +5366,7 @@ fn addAtomToSection(self: *MachO, atom: *Atom, match: MatchingSection) !void { } } -pub fn addExternFn(self: *MachO, name: []const u8) !u32 { +pub fn getGlobalSymbol(self: *MachO, name: []const u8) !u32 { const sym_name = try std.fmt.allocPrint(self.base.allocator, "_{s}", .{name}); defer self.base.allocator.free(sym_name); const n_strx = try self.makeString(sym_name); diff --git a/src/link/Wasm.zig b/src/link/Wasm.zig index d10690142f..4357a588a1 100644 --- a/src/link/Wasm.zig +++ b/src/link/Wasm.zig @@ -864,7 +864,8 @@ pub fn lowerUnnamedConst(self: *Wasm, tv: TypedValue, decl_index: Module.Decl.In return atom.sym_index; } -/// Returns the symbol index from the name of an intrinsic. +/// Returns the symbol index from a symbol of which its flag is set global, +/// such as an exported or imported symbol. /// If the symbol does not yet exist, creates a new one symbol instead /// and then returns the index to it. pub fn getGlobalSymbol(self: *Wasm, name: []const u8) !u32 {