authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-04-07 19:58:55-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-04-07 19:58:55-04:00
log55a8b7e1fa4d36f5283d1f8655d3baecfefeffa3
treea427e2fac6d904759d6f7e00356744f7ec331e48
parent4ebf483e0da2f85e00918b7900afaf73e111b727
parentb086b7da9e9adbb4445363f635d3bb37177b2f18
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #15097 from zachcheu/add-wrap-flag

add linker -wrap flag

4 files changed, 24 insertions(+), 1 deletions(-)

src/Compilation.zig+5-1
......@@ -494,6 +494,7 @@ pub const InitOptions = struct {
494494 clang_argv: []const []const u8 = &[0][]const u8{},
495495 lib_dirs: []const []const u8 = &[0][]const u8{},
496496 rpath_list: []const []const u8 = &[0][]const u8{},
497 symbol_wrap_set: std.StringArrayHashMapUnmanaged(void) = .{},
497498 c_source_files: []const CSourceFile = &[0]CSourceFile{},
498499 link_objects: []LinkObject = &[0]LinkObject{},
499500 framework_dirs: []const []const u8 = &[0][]const u8{},
......@@ -825,7 +826,8 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
825826 options.output_mode == .Lib or
826827 options.linker_script != null or options.version_script != null or
827828 options.emit_implib != null or
828 build_id)
829 build_id or
830 options.symbol_wrap_set.count() > 0)
829831 {
830832 break :blk true;
831833 }
......@@ -1438,6 +1440,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
14381440 .wasi_emulated_libs = options.wasi_emulated_libs,
14391441 .lib_dirs = options.lib_dirs,
14401442 .rpath_list = options.rpath_list,
1443 .symbol_wrap_set = options.symbol_wrap_set,
14411444 .strip = strip,
14421445 .is_native_os = options.is_native_os,
14431446 .is_native_abi = options.is_native_abi,
......@@ -2259,6 +2262,7 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes
22592262 man.hash.add(comp.bin_file.options.rdynamic);
22602263 man.hash.addListOfBytes(comp.bin_file.options.lib_dirs);
22612264 man.hash.addListOfBytes(comp.bin_file.options.rpath_list);
2265 man.hash.addListOfBytes(comp.bin_file.options.symbol_wrap_set.keys());
22622266 man.hash.add(comp.bin_file.options.each_lib_rpath);
22632267 man.hash.add(comp.bin_file.options.build_id);
22642268 man.hash.add(comp.bin_file.options.skip_linker_dependencies);
src/link.zig+7
......@@ -188,6 +188,13 @@ pub const Options = struct {
188188 /// thus forcing their resolution by the linker.
189189 /// Corresponds to `-u <symbol>` for ELF/MachO and `/include:<symbol>` for COFF/PE.
190190 force_undefined_symbols: std.StringArrayHashMapUnmanaged(void),
191 /// Use a wrapper function for symbol. Any undefined reference to symbol
192 /// will be resolved to __wrap_symbol. Any undefined reference to
193 /// __real_symbol will be resolved to symbol. This can be used to provide a
194 /// wrapper for a system function. The wrapper function should be called
195 /// __wrap_symbol. If it wishes to call the system function, it should call
196 /// __real_symbol.
197 symbol_wrap_set: std.StringArrayHashMapUnmanaged(void),
191198
192199 version: ?std.builtin.Version,
193200 compatibility_version: ?std.builtin.Version,
src/link/Elf.zig+6
......@@ -1386,6 +1386,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
13861386 man.hash.add(stack_size);
13871387 man.hash.add(self.base.options.build_id);
13881388 }
1389 man.hash.addListOfBytes(self.base.options.symbol_wrap_set.keys());
13891390 man.hash.add(self.base.options.skip_linker_dependencies);
13901391 man.hash.add(self.base.options.z_nodelete);
13911392 man.hash.add(self.base.options.z_notext);
......@@ -1665,6 +1666,11 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
16651666 try argv.append(rpath);
16661667 }
16671668 }
1669
1670 for (self.base.options.symbol_wrap_set.keys()) |symbol_name| {
1671 try argv.appendSlice(&.{ "-wrap", symbol_name });
1672 }
1673
16681674 if (self.base.options.each_lib_rpath) {
16691675 var test_path = std.ArrayList(u8).init(self.base.allocator);
16701676 defer test_path.deinit();
src/main.zig+6
......@@ -874,6 +874,8 @@ fn buildOutputType(
874874 var rpath_list = std.ArrayList([]const u8).init(gpa);
875875 defer rpath_list.deinit();
876876
877 var symbol_wrap_set: std.StringArrayHashMapUnmanaged(void) = .{};
878
877879 var c_source_files = std.ArrayList(Compilation.CSourceFile).init(gpa);
878880 defer c_source_files.deinit();
879881
......@@ -2155,6 +2157,9 @@ fn buildOutputType(
21552157 next_arg,
21562158 });
21572159 };
2160 } else if (mem.eql(u8, arg, "-wrap")) {
2161 const next_arg = linker_args_it.nextOrFatal();
2162 try symbol_wrap_set.put(arena, next_arg, {});
21582163 } else if (mem.startsWith(u8, arg, "/subsystem:")) {
21592164 var split_it = mem.splitBackwards(u8, arg, ":");
21602165 subsystem = try parseSubSystem(split_it.first());
......@@ -3039,6 +3044,7 @@ fn buildOutputType(
30393044 .clang_argv = clang_argv.items,
30403045 .lib_dirs = lib_dirs.items,
30413046 .rpath_list = rpath_list.items,
3047 .symbol_wrap_set = symbol_wrap_set,
30423048 .c_source_files = c_source_files.items,
30433049 .link_objects = link_objects.items,
30443050 .framework_dirs = framework_dirs.items,