authorgravatar for zach.cheung@uber.comZach Cheung <zach.cheung@uber.com> 2023-03-23 17:16:39-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-04-07 09:09:38-04:00
log1fdea551b22726783356c5bed90bce63706b550f
tree22b8bd9b92578d7d03e38be8ece7d29662a640bf
parent086639630800fc52bd727163e85d174ec1ac1103

add linker -wrap flag


4 files changed, 30 insertions(+), 0 deletions(-)

src/Compilation.zig+3
...@@ -494,6 +494,7 @@ pub const InitOptions = struct {...@@ -494,6 +494,7 @@ pub const InitOptions = struct {
494 clang_argv: []const []const u8 = &[0][]const u8{},494 clang_argv: []const []const u8 = &[0][]const u8{},
495 lib_dirs: []const []const u8 = &[0][]const u8{},495 lib_dirs: []const []const u8 = &[0][]const u8{},
496 rpath_list: []const []const u8 = &[0][]const u8{},496 rpath_list: []const []const u8 = &[0][]const u8{},
497 wrap_list: []const []const u8 = &[0][]const u8{},
497 c_source_files: []const CSourceFile = &[0]CSourceFile{},498 c_source_files: []const CSourceFile = &[0]CSourceFile{},
498 link_objects: []LinkObject = &[0]LinkObject{},499 link_objects: []LinkObject = &[0]LinkObject{},
499 framework_dirs: []const []const u8 = &[0][]const u8{},500 framework_dirs: []const []const u8 = &[0][]const u8{},
...@@ -1438,6 +1439,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {...@@ -1438,6 +1439,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
1438 .wasi_emulated_libs = options.wasi_emulated_libs,1439 .wasi_emulated_libs = options.wasi_emulated_libs,
1439 .lib_dirs = options.lib_dirs,1440 .lib_dirs = options.lib_dirs,
1440 .rpath_list = options.rpath_list,1441 .rpath_list = options.rpath_list,
1442 .wrap_list = options.wrap_list,
1441 .strip = strip,1443 .strip = strip,
1442 .is_native_os = options.is_native_os,1444 .is_native_os = options.is_native_os,
1443 .is_native_abi = options.is_native_abi,1445 .is_native_abi = options.is_native_abi,
...@@ -2259,6 +2261,7 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes...@@ -2259,6 +2261,7 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes
2259 man.hash.add(comp.bin_file.options.rdynamic);2261 man.hash.add(comp.bin_file.options.rdynamic);
2260 man.hash.addListOfBytes(comp.bin_file.options.lib_dirs);2262 man.hash.addListOfBytes(comp.bin_file.options.lib_dirs);
2261 man.hash.addListOfBytes(comp.bin_file.options.rpath_list);2263 man.hash.addListOfBytes(comp.bin_file.options.rpath_list);
2264 man.hash.addListOfBytes(comp.bin_file.options.wrap_list);
2262 man.hash.add(comp.bin_file.options.each_lib_rpath);2265 man.hash.add(comp.bin_file.options.each_lib_rpath);
2263 man.hash.add(comp.bin_file.options.build_id);2266 man.hash.add(comp.bin_file.options.build_id);
2264 man.hash.add(comp.bin_file.options.skip_linker_dependencies);2267 man.hash.add(comp.bin_file.options.skip_linker_dependencies);
src/link.zig+1
...@@ -183,6 +183,7 @@ pub const Options = struct {...@@ -183,6 +183,7 @@ pub const Options = struct {
183 wasi_emulated_libs: []const wasi_libc.CRTFile,183 wasi_emulated_libs: []const wasi_libc.CRTFile,
184 lib_dirs: []const []const u8,184 lib_dirs: []const []const u8,
185 rpath_list: []const []const u8,185 rpath_list: []const []const u8,
186 wrap_list: []const []const u8,
186187
187 /// List of symbols forced as undefined in the symbol table188 /// List of symbols forced as undefined in the symbol table
188 /// thus forcing their resolution by the linker.189 /// thus forcing their resolution by the linker.
src/link/Elf.zig+12
...@@ -1386,6 +1386,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v...@@ -1386,6 +1386,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
1386 man.hash.add(stack_size);1386 man.hash.add(stack_size);
1387 man.hash.add(self.base.options.build_id);1387 man.hash.add(self.base.options.build_id);
1388 }1388 }
1389 man.hash.addListOfBytes(self.base.options.wrap_list);
1389 man.hash.add(self.base.options.skip_linker_dependencies);1390 man.hash.add(self.base.options.skip_linker_dependencies);
1390 man.hash.add(self.base.options.z_nodelete);1391 man.hash.add(self.base.options.z_nodelete);
1391 man.hash.add(self.base.options.z_notext);1392 man.hash.add(self.base.options.z_notext);
...@@ -1665,6 +1666,17 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v...@@ -1665,6 +1666,17 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
1665 try argv.append(rpath);1666 try argv.append(rpath);
1666 }1667 }
1667 }1668 }
1669
1670 // wrap
1671 var wrap_table = std.StringHashMap(void).init(self.base.allocator);
1672 defer wrap_table.deinit();
1673 for (self.base.options.wrap_list) |wrap| {
1674 if ((try wrap_table.fetchPut(wrap, {})) == null) {
1675 try argv.append("-wrap");
1676 try argv.append(wrap);
1677 }
1678 }
1679
1668 if (self.base.options.each_lib_rpath) {1680 if (self.base.options.each_lib_rpath) {
1669 var test_path = std.ArrayList(u8).init(self.base.allocator);1681 var test_path = std.ArrayList(u8).init(self.base.allocator);
1670 defer test_path.deinit();1682 defer test_path.deinit();
src/main.zig+14
...@@ -487,6 +487,7 @@ const usage_build_generic =...@@ -487,6 +487,7 @@ const usage_build_generic =
487 \\ -fno-compiler-rt Prevent including compiler-rt symbols in output487 \\ -fno-compiler-rt Prevent including compiler-rt symbols in output
488 \\ -rdynamic Add all symbols to the dynamic symbol table488 \\ -rdynamic Add all symbols to the dynamic symbol table
489 \\ -rpath [path] Add directory to the runtime library search path489 \\ -rpath [path] Add directory to the runtime library search path
490 \\ -wrap [symbol] Override the symbol with the wrapped symbol definition
490 \\ -feach-lib-rpath Ensure adding rpath for each used dynamic library491 \\ -feach-lib-rpath Ensure adding rpath for each used dynamic library
491 \\ -fno-each-lib-rpath Prevent adding rpath for each used dynamic library492 \\ -fno-each-lib-rpath Prevent adding rpath for each used dynamic library
492 \\ -fallow-shlib-undefined Allows undefined symbols in shared libraries493 \\ -fallow-shlib-undefined Allows undefined symbols in shared libraries
...@@ -874,6 +875,9 @@ fn buildOutputType(...@@ -874,6 +875,9 @@ fn buildOutputType(
874 var rpath_list = std.ArrayList([]const u8).init(gpa);875 var rpath_list = std.ArrayList([]const u8).init(gpa);
875 defer rpath_list.deinit();876 defer rpath_list.deinit();
876877
878 var wrap_list = std.ArrayList([]const u8).init(gpa);
879 defer wrap_list.deinit();
880
877 var c_source_files = std.ArrayList(Compilation.CSourceFile).init(gpa);881 var c_source_files = std.ArrayList(Compilation.CSourceFile).init(gpa);
878 defer c_source_files.deinit();882 defer c_source_files.deinit();
879883
...@@ -1475,6 +1479,9 @@ fn buildOutputType(...@@ -1475,6 +1479,9 @@ fn buildOutputType(
1475 try system_libs.put(arg["-needed-l".len..], .{ .needed = true });1479 try system_libs.put(arg["-needed-l".len..], .{ .needed = true });
1476 } else if (mem.startsWith(u8, arg, "-weak-l")) {1480 } else if (mem.startsWith(u8, arg, "-weak-l")) {
1477 try system_libs.put(arg["-weak-l".len..], .{ .weak = true });1481 try system_libs.put(arg["-weak-l".len..], .{ .weak = true });
1482 } else if (mem.eql(u8, arg, "-wrap")){
1483 try wrap_list.append(arg);
1484 try wrap_list.append(args_iter.nextOrFatal());
1478 } else if (mem.startsWith(u8, arg, "-D")) {1485 } else if (mem.startsWith(u8, arg, "-D")) {
1479 try clang_argv.append(arg);1486 try clang_argv.append(arg);
1480 } else if (mem.startsWith(u8, arg, "-I")) {1487 } else if (mem.startsWith(u8, arg, "-I")) {
...@@ -2155,6 +2162,12 @@ fn buildOutputType(...@@ -2155,6 +2162,12 @@ fn buildOutputType(
2155 next_arg,2162 next_arg,
2156 });2163 });
2157 };2164 };
2165 } else if (mem.eql(u8, arg, "-wrap")) {
2166 i += 1;
2167 if (i >= linker_args.items.len) {
2168 fatal("expected linker arg after '{s}'", .{arg});
2169 }
2170 try wrap_list.append(linker_args.items[i]);
2158 } else if (mem.startsWith(u8, arg, "/subsystem:")) {2171 } else if (mem.startsWith(u8, arg, "/subsystem:")) {
2159 var split_it = mem.splitBackwards(u8, arg, ":");2172 var split_it = mem.splitBackwards(u8, arg, ":");
2160 subsystem = try parseSubSystem(split_it.first());2173 subsystem = try parseSubSystem(split_it.first());
...@@ -3039,6 +3052,7 @@ fn buildOutputType(...@@ -3039,6 +3052,7 @@ fn buildOutputType(
3039 .clang_argv = clang_argv.items,3052 .clang_argv = clang_argv.items,
3040 .lib_dirs = lib_dirs.items,3053 .lib_dirs = lib_dirs.items,
3041 .rpath_list = rpath_list.items,3054 .rpath_list = rpath_list.items,
3055 .wrap_list = wrap_list.items,
3042 .c_source_files = c_source_files.items,3056 .c_source_files = c_source_files.items,
3043 .link_objects = link_objects.items,3057 .link_objects = link_objects.items,
3044 .framework_dirs = framework_dirs.items,3058 .framework_dirs = framework_dirs.items,