diff options
| author | Alex Rønne Petersen <alex@alexrp.com> | 2025-02-16 06:29:08 +0100 |
|---|---|---|
| committer | Alex Rønne Petersen <alex@alexrp.com> | 2025-02-22 06:35:19 +0100 |
| commit | f87b443af1f654d3363ce6a1081bdf7932ae354c (patch) | |
| tree | f4b242f4e9463026e1b53cab879fd21acbbb2e9a /src | |
| parent | a7467b9bb2aa667a8d34bc8b678ce35fcb19ebd4 (diff) | |
| download | zig-f87b443af1f654d3363ce6a1081bdf7932ae354c.tar.gz zig-f87b443af1f654d3363ce6a1081bdf7932ae354c.zip | |
link.MachO: Add support for the -x flag (discard local symbols).
This can also be extended to ELF later as it means roughly the same thing there.
This addresses the main issue in #21721 but as I don't have a macOS machine to
do further testing on, I can't confirm whether zig cc is able to pass the entire
cgo test suite after this commit. It can, however, cross-compile a basic program
that uses cgo to x86_64-macos-none which previously failed due to lack of -x
support. Unlike previously, the resulting symbol table does not contain local
symbols (such as C static functions).
I believe this satisfies the related donor bounty: https://ziglang.org/news/second-donor-bounty
Diffstat (limited to 'src')
| -rw-r--r-- | src/Compilation.zig | 4 | ||||
| -rw-r--r-- | src/link.zig | 2 | ||||
| -rw-r--r-- | src/link/MachO.zig | 7 | ||||
| -rw-r--r-- | src/link/MachO/InternalObject.zig | 1 | ||||
| -rw-r--r-- | src/link/MachO/Object.zig | 1 | ||||
| -rw-r--r-- | src/link/MachO/ZigObject.zig | 1 | ||||
| -rw-r--r-- | src/link/MachO/synthetic.zig | 12 | ||||
| -rw-r--r-- | src/main.zig | 6 |
8 files changed, 31 insertions, 3 deletions
diff --git a/src/Compilation.zig b/src/Compilation.zig index c2b72c6b0f..bec6696c92 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -1160,6 +1160,8 @@ pub const CreateOptions = struct { dead_strip_dylibs: bool = false, /// (Darwin) Force load all members of static archives that implement an Objective-C class or category force_load_objc: bool = false, + /// Whether local symbols should be discarded from the symbol table. + discard_local_symbols: bool = false, libcxx_abi_version: libcxx.AbiVersion = libcxx.AbiVersion.default, /// (Windows) PDB source path prefix to instruct the linker how to resolve relative /// paths when consolidating CodeView streams into a single PDB file. @@ -1585,6 +1587,7 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil .headerpad_max_install_names = options.headerpad_max_install_names, .dead_strip_dylibs = options.dead_strip_dylibs, .force_load_objc = options.force_load_objc, + .discard_local_symbols = options.discard_local_symbols, .pdb_source_path = options.pdb_source_path, .pdb_out_path = options.pdb_out_path, .entry_addr = null, // CLI does not expose this option (yet?) @@ -2665,6 +2668,7 @@ fn addNonIncrementalStuffToCacheManifest( man.hash.add(opts.headerpad_max_install_names); man.hash.add(opts.dead_strip_dylibs); man.hash.add(opts.force_load_objc); + man.hash.add(opts.discard_local_symbols); // COFF specific stuff man.hash.addOptional(opts.subsystem); diff --git a/src/link.zig b/src/link.zig index d805b331e3..99d7908be1 100644 --- a/src/link.zig +++ b/src/link.zig @@ -491,6 +491,8 @@ pub const File = struct { /// Force load all members of static archives that implement an /// Objective-C class or category force_load_objc: bool, + /// Whether local symbols should be discarded from the symbol table. + discard_local_symbols: bool, /// Windows-specific linker flags: /// PDB source path prefix to instruct the linker how to resolve relative diff --git a/src/link/MachO.zig b/src/link/MachO.zig index a5e3d2e070..97a84809d7 100644 --- a/src/link/MachO.zig +++ b/src/link/MachO.zig @@ -139,6 +139,8 @@ no_implicit_dylibs: bool = false, /// Whether the linker should parse and always force load objects containing ObjC in archives. // TODO: in Zig we currently take -ObjC as always on force_load_objc: bool = true, +/// Whether local symbols should be discarded from the symbol table. +discard_local_symbols: bool = false, /// Hot-code swapping state. hot_state: if (is_hot_update_compatible) HotUpdateState else struct {} = .{}, @@ -221,6 +223,7 @@ pub fn createEmpty( .lib_directories = options.lib_directories, .framework_dirs = options.framework_dirs, .force_load_objc = options.force_load_objc, + .discard_local_symbols = options.discard_local_symbols, }; if (use_llvm and comp.config.have_zcu) { self.llvm_object = try LlvmObject.create(arena, comp); @@ -720,6 +723,10 @@ fn dumpArgv(self: *MachO, comp: *Compilation) !void { try argv.append("-ObjC"); } + if (self.discard_local_symbols) { + try argv.append("-x"); + } + if (self.entry_name) |entry_name| { try argv.appendSlice(&.{ "-e", entry_name }); } diff --git a/src/link/MachO/InternalObject.zig b/src/link/MachO/InternalObject.zig index 2eb9837833..0218f0c1bb 100644 --- a/src/link/MachO/InternalObject.zig +++ b/src/link/MachO/InternalObject.zig @@ -583,6 +583,7 @@ pub fn calcSymtabSize(self: *InternalObject, macho_file: *MachO) void { const file = ref.getFile(macho_file) orelse continue; if (file.getIndex() != self.index) continue; if (sym.getName(macho_file).len == 0) continue; + if (macho_file.discard_local_symbols and sym.isLocal()) continue; sym.flags.output_symtab = true; if (sym.isLocal()) { sym.addExtra(.{ .symtab = self.output_symtab_ctx.nlocals }, macho_file); diff --git a/src/link/MachO/Object.zig b/src/link/MachO/Object.zig index 000f374035..ec9d07aabc 100644 --- a/src/link/MachO/Object.zig +++ b/src/link/MachO/Object.zig @@ -1722,6 +1722,7 @@ pub fn calcSymtabSize(self: *Object, macho_file: *MachO) void { if (file.getIndex() != self.index) continue; if (sym.getAtom(macho_file)) |atom| if (!atom.isAlive()) continue; if (sym.isSymbolStab(macho_file)) continue; + if (macho_file.discard_local_symbols and sym.isLocal()) continue; const name = sym.getName(macho_file); if (name.len == 0) continue; // TODO in -r mode, we actually want to merge symbol names and emit only one diff --git a/src/link/MachO/ZigObject.zig b/src/link/MachO/ZigObject.zig index 5f2e1291c0..76fa43bc65 100644 --- a/src/link/MachO/ZigObject.zig +++ b/src/link/MachO/ZigObject.zig @@ -500,6 +500,7 @@ pub fn calcSymtabSize(self: *ZigObject, macho_file: *MachO) void { const file = ref.getFile(macho_file) orelse continue; if (file.getIndex() != self.index) continue; if (sym.getAtom(macho_file)) |atom| if (!atom.isAlive()) continue; + if (macho_file.discard_local_symbols and sym.isLocal()) continue; const name = sym.getName(macho_file); assert(name.len > 0); sym.flags.output_symtab = true; diff --git a/src/link/MachO/synthetic.zig b/src/link/MachO/synthetic.zig index 900316a769..6042fe628d 100644 --- a/src/link/MachO/synthetic.zig +++ b/src/link/MachO/synthetic.zig @@ -532,17 +532,23 @@ pub const Indsymtab = struct { for (macho_file.stubs.symbols.items) |ref| { const sym = ref.getSymbol(macho_file).?; - try writer.writeInt(u32, sym.getOutputSymtabIndex(macho_file).?, .little); + if (sym.getOutputSymtabIndex(macho_file)) |idx| { + try writer.writeInt(u32, idx, .little); + } } for (macho_file.got.symbols.items) |ref| { const sym = ref.getSymbol(macho_file).?; - try writer.writeInt(u32, sym.getOutputSymtabIndex(macho_file).?, .little); + if (sym.getOutputSymtabIndex(macho_file)) |idx| { + try writer.writeInt(u32, idx, .little); + } } for (macho_file.stubs.symbols.items) |ref| { const sym = ref.getSymbol(macho_file).?; - try writer.writeInt(u32, sym.getOutputSymtabIndex(macho_file).?, .little); + if (sym.getOutputSymtabIndex(macho_file)) |idx| { + try writer.writeInt(u32, idx, .little); + } } } }; diff --git a/src/main.zig b/src/main.zig index eac8674b19..a5b0f23014 100644 --- a/src/main.zig +++ b/src/main.zig @@ -918,6 +918,7 @@ fn buildOutputType( var headerpad_max_install_names: bool = false; var dead_strip_dylibs: bool = false; var force_load_objc: bool = false; + var discard_local_symbols: bool = false; var contains_res_file: bool = false; var reference_trace: ?u32 = null; var pdb_out_path: ?[]const u8 = null; @@ -1151,6 +1152,8 @@ fn buildOutputType( entry = .{ .named = arg["-fentry=".len..] }; } else if (mem.eql(u8, arg, "--force_undefined")) { try force_undefined_symbols.put(arena, args_iter.nextOrFatal(), {}); + } else if (mem.eql(u8, arg, "--discard-all")) { + discard_local_symbols = true; } else if (mem.eql(u8, arg, "--stack")) { stack_size = parseStackSize(args_iter.nextOrFatal()); } else if (mem.eql(u8, arg, "--image-base")) { @@ -2510,6 +2513,8 @@ fn buildOutputType( entry = .{ .named = linker_args_it.nextOrFatal() }; } else if (mem.eql(u8, arg, "-u")) { try force_undefined_symbols.put(arena, linker_args_it.nextOrFatal(), {}); + } else if (mem.eql(u8, arg, "-x") or mem.eql(u8, arg, "--discard-all")) { + discard_local_symbols = true; } else if (mem.eql(u8, arg, "--stack") or mem.eql(u8, arg, "-stack_size")) { stack_size = parseStackSize(linker_args_it.nextOrFatal()); } else if (mem.eql(u8, arg, "--image-base")) { @@ -3579,6 +3584,7 @@ fn buildOutputType( .headerpad_max_install_names = headerpad_max_install_names, .dead_strip_dylibs = dead_strip_dylibs, .force_load_objc = force_load_objc, + .discard_local_symbols = discard_local_symbols, .reference_trace = reference_trace, .pdb_out_path = pdb_out_path, .error_limit = error_limit, |
