authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-06-23 16:20:20+02:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-06-24 08:12:17+02:00
log7c87f9c828282aa12fb2d77c9c6a2318d83b8dff
treed1168ba85eca9ea505d901faecd97847b5bea76c
parent3868864695b41c2c94f585b9644d3ed3bda39708

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.

6 files changed, 9 insertions(+), 8 deletions(-)

src/Compilation.zig+1-2
...@@ -1920,8 +1920,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {...@@ -1920,8 +1920,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
1920 // and make sure the compiler-rt symbols are emitted.1920 // and make sure the compiler-rt symbols are emitted.
1921 const capable_of_building_compiler_rt = build_options.have_llvm;1921 const capable_of_building_compiler_rt = build_options.have_llvm;
19221922
1923 const capable_of_building_zig_libc = comp.bin_file.options.use_stage1 or1923 const capable_of_building_zig_libc = build_options.have_llvm;
1924 comp.bin_file.options.use_llvm;
1925 const capable_of_building_ssp = comp.bin_file.options.use_stage1;1924 const capable_of_building_ssp = comp.bin_file.options.use_stage1;
19261925
1927 if (comp.bin_file.options.include_compiler_rt and capable_of_building_compiler_rt) {1926 if (comp.bin_file.options.include_compiler_rt and capable_of_building_compiler_rt) {
src/arch/aarch64/CodeGen.zig+1-1
...@@ -3188,7 +3188,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions....@@ -3188,7 +3188,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions.
3188 lib_name,3188 lib_name,
3189 });3189 });
3190 }3190 }
3191 const n_strx = try macho_file.addExternFn(mem.sliceTo(decl_name, 0));3191 const n_strx = try macho_file.getGlobalSymbol(mem.sliceTo(decl_name, 0));
31923192
3193 _ = try self.addInst(.{3193 _ = try self.addInst(.{
3194 .tag = .call_extern,3194 .tag = .call_extern,
src/arch/x86_64/CodeGen.zig+1-1
...@@ -3996,7 +3996,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions....@@ -3996,7 +3996,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions.
3996 lib_name,3996 lib_name,
3997 });3997 });
3998 }3998 }
3999 const n_strx = try macho_file.addExternFn(mem.sliceTo(decl_name, 0));3999 const n_strx = try macho_file.getGlobalSymbol(mem.sliceTo(decl_name, 0));
4000 _ = try self.addInst(.{4000 _ = try self.addInst(.{
4001 .tag = .call_extern,4001 .tag = .call_extern,
4002 .ops = undefined,4002 .ops = undefined,
src/link.zig+3-2
...@@ -439,14 +439,15 @@ pub const File = struct {...@@ -439,14 +439,15 @@ pub const File = struct {
439 }439 }
440440
441 /// Called from within CodeGen to retrieve the symbol index of a global symbol.441 /// Called from within CodeGen to retrieve the symbol index of a global symbol.
442 /// If no symbol exists yet with this name, a new one will be created instead.442 /// If no symbol exists yet with this name, a new undefined global symbol will
443 /// be created. This symbol may get resolved once all relocatables are (re-)linked.
443 pub fn getGlobalSymbol(base: *File, name: []const u8) UpdateDeclError!u32 {444 pub fn getGlobalSymbol(base: *File, name: []const u8) UpdateDeclError!u32 {
444 log.debug("getGlobalSymbol '{s}'", .{name});445 log.debug("getGlobalSymbol '{s}'", .{name});
445 switch (base.tag) {446 switch (base.tag) {
446 // zig fmt: off447 // zig fmt: off
447 .coff => unreachable,448 .coff => unreachable,
448 .elf => unreachable,449 .elf => unreachable,
449 .macho => unreachable,450 .macho => return @fieldParentPtr(MachO, "base", base).getGlobalSymbol(name),
450 .plan9 => unreachable,451 .plan9 => unreachable,
451 .spirv => unreachable,452 .spirv => unreachable,
452 .c => unreachable,453 .c => unreachable,
src/link/MachO.zig+1-1
...@@ -5366,7 +5366,7 @@ fn addAtomToSection(self: *MachO, atom: *Atom, match: MatchingSection) !void {...@@ -5366,7 +5366,7 @@ fn addAtomToSection(self: *MachO, atom: *Atom, match: MatchingSection) !void {
5366 }5366 }
5367}5367}
53685368
5369pub fn addExternFn(self: *MachO, name: []const u8) !u32 {5369pub fn getGlobalSymbol(self: *MachO, name: []const u8) !u32 {
5370 const sym_name = try std.fmt.allocPrint(self.base.allocator, "_{s}", .{name});5370 const sym_name = try std.fmt.allocPrint(self.base.allocator, "_{s}", .{name});
5371 defer self.base.allocator.free(sym_name);5371 defer self.base.allocator.free(sym_name);
5372 const n_strx = try self.makeString(sym_name);5372 const n_strx = try self.makeString(sym_name);
src/link/Wasm.zig+2-1
...@@ -864,7 +864,8 @@ pub fn lowerUnnamedConst(self: *Wasm, tv: TypedValue, decl_index: Module.Decl.In...@@ -864,7 +864,8 @@ pub fn lowerUnnamedConst(self: *Wasm, tv: TypedValue, decl_index: Module.Decl.In
864 return atom.sym_index;864 return atom.sym_index;
865}865}
866866
867/// Returns the symbol index from the name of an intrinsic.867/// Returns the symbol index from a symbol of which its flag is set global,
868/// such as an exported or imported symbol.
868/// If the symbol does not yet exist, creates a new one symbol instead869/// If the symbol does not yet exist, creates a new one symbol instead
869/// and then returns the index to it.870/// and then returns the index to it.
870pub fn getGlobalSymbol(self: *Wasm, name: []const u8) !u32 {871pub fn getGlobalSymbol(self: *Wasm, name: []const u8) !u32 {