authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-06-24 20:25:16+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-06-24 20:25:16+02:00
log0df7ed79d304afc7d379482005e892979d4a5e4d
tree34fd8af5325aeec01f7d1f0aca18f80850791b41
parentd589047e80133c5f673a7d40dd1cfa50258dcc4f

macho: implement -search_dylibs_first linker option


5 files changed, 114 insertions(+), 56 deletions(-)

lib/std/build.zig+11
...@@ -1586,6 +1586,13 @@ pub const LibExeObjStep = struct {...@@ -1586,6 +1586,13 @@ pub const LibExeObjStep = struct {
1586 /// (Darwin) Size of the pagezero segment.1586 /// (Darwin) Size of the pagezero segment.
1587 pagezero_size: ?u64 = null,1587 pagezero_size: ?u64 = null,
15881588
1589 /// (Darwin) Search strategy for searching system libraries. Either `paths_first` or `dylibs_first`.
1590 /// The former lowers to `-search_paths_first` linker option, while the latter to `-search_dylibs_first`
1591 /// option.
1592 /// By default, if no option is specified, the linker assumes `paths_first` as the default
1593 /// search strategy.
1594 search_strategy: ?enum { paths_first, dylibs_first } = null,
1595
1589 /// Position Independent Code1596 /// Position Independent Code
1590 force_pic: ?bool = null,1597 force_pic: ?bool = null,
15911598
...@@ -2650,6 +2657,10 @@ pub const LibExeObjStep = struct {...@@ -2650,6 +2657,10 @@ pub const LibExeObjStep = struct {
2650 const size = try std.fmt.allocPrint(builder.allocator, "{x}", .{pagezero_size});2657 const size = try std.fmt.allocPrint(builder.allocator, "{x}", .{pagezero_size});
2651 try zig_args.appendSlice(&[_][]const u8{ "-pagezero_size", size });2658 try zig_args.appendSlice(&[_][]const u8{ "-pagezero_size", size });
2652 }2659 }
2660 if (self.search_strategy) |strat| switch (strat) {
2661 .paths_first => try zig_args.append("-search_paths_first"),
2662 .dylibs_first => try zig_args.append("-search_dylibs_first"),
2663 };
26532664
2654 if (self.bundle_compiler_rt) |x| {2665 if (self.bundle_compiler_rt) |x| {
2655 if (x) {2666 if (x) {
src/Compilation.zig+3
...@@ -905,6 +905,8 @@ pub const InitOptions = struct {...@@ -905,6 +905,8 @@ pub const InitOptions = struct {
905 entitlements: ?[]const u8 = null,905 entitlements: ?[]const u8 = null,
906 /// (Darwin) size of the __PAGEZERO segment906 /// (Darwin) size of the __PAGEZERO segment
907 pagezero_size: ?u64 = null,907 pagezero_size: ?u64 = null,
908 /// (Darwin) search strategy for system libraries
909 search_strategy: ?link.File.MachO.SearchStrategy = null,
908};910};
909911
910fn addPackageTableToCacheHash(912fn addPackageTableToCacheHash(
...@@ -1745,6 +1747,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {...@@ -1745,6 +1747,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
1745 .install_name = options.install_name,1747 .install_name = options.install_name,
1746 .entitlements = options.entitlements,1748 .entitlements = options.entitlements,
1747 .pagezero_size = options.pagezero_size,1749 .pagezero_size = options.pagezero_size,
1750 .search_strategy = options.search_strategy,
1748 });1751 });
1749 errdefer bin_file.destroy();1752 errdefer bin_file.destroy();
1750 comp.* = .{1753 comp.* = .{
src/link.zig+3
...@@ -190,6 +190,9 @@ pub const Options = struct {...@@ -190,6 +190,9 @@ pub const Options = struct {
190 /// (Darwin) size of the __PAGEZERO segment190 /// (Darwin) size of the __PAGEZERO segment
191 pagezero_size: ?u64 = null,191 pagezero_size: ?u64 = null,
192192
193 /// (Darwin) search strategy for system libraries
194 search_strategy: ?File.MachO.SearchStrategy = null,
195
193 pub fn effectiveOutputMode(options: Options) std.builtin.OutputMode {196 pub fn effectiveOutputMode(options: Options) std.builtin.OutputMode {
194 return if (options.use_lld) .Obj else options.output_mode;197 return if (options.use_lld) .Obj else options.output_mode;
195 }198 }
src/link/MachO.zig+86-55
...@@ -47,6 +47,11 @@ pub const DebugSymbols = @import("MachO/DebugSymbols.zig");...@@ -47,6 +47,11 @@ pub const DebugSymbols = @import("MachO/DebugSymbols.zig");
4747
48pub const base_tag: File.Tag = File.Tag.macho;48pub const base_tag: File.Tag = File.Tag.macho;
4949
50pub const SearchStrategy = enum {
51 paths_first,
52 dylibs_first,
53};
54
50base: File,55base: File,
5156
52/// If this is not null, an object file is created by LLVM and linked with LLD afterwards.57/// If this is not null, an object file is created by LLVM and linked with LLD afterwards.
...@@ -784,18 +789,43 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No...@@ -784,18 +789,43 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No
784 }789 }
785790
786 var libs = std.ArrayList([]const u8).init(arena);791 var libs = std.ArrayList([]const u8).init(arena);
787 for (search_lib_names.items) |lib_name| {792
788 // Assume ld64 default: -search_paths_first793 // Assume ld64 default -search_paths_first if no strategy specified.
789 // Look in each directory for a dylib (stub first), and then for archive794 const search_strategy = self.base.options.search_strategy orelse .paths_first;
790 // TODO implement alternative: -search_dylibs_first795 outer: for (search_lib_names.items) |lib_name| {
791 for (&[_][]const u8{ ".tbd", ".dylib", ".a" }) |ext| {796 switch (search_strategy) {
792 if (try resolveLib(arena, lib_dirs.items, lib_name, ext)) |full_path| {797 .paths_first => {
793 try libs.append(full_path);798 // Look in each directory for a dylib (stub first), and then for archive
794 break;799 for (lib_dirs.items) |dir| {
795 }800 for (&[_][]const u8{ ".tbd", ".dylib", ".a" }) |ext| {
796 } else {801 if (try resolveLib(arena, dir, lib_name, ext)) |full_path| {
797 log.warn("library not found for '-l{s}'", .{lib_name});802 try libs.append(full_path);
798 lib_not_found = true;803 continue :outer;
804 }
805 }
806 } else {
807 log.warn("library not found for '-l{s}'", .{lib_name});
808 lib_not_found = true;
809 }
810 },
811 .dylibs_first => {
812 // First, look for a dylib in each search dir
813 for (lib_dirs.items) |dir| {
814 for (&[_][]const u8{ ".tbd", ".dylib" }) |ext| {
815 if (try resolveLib(arena, dir, lib_name, ext)) |full_path| {
816 try libs.append(full_path);
817 continue :outer;
818 }
819 }
820 } else for (lib_dirs.items) |dir| {
821 if (try resolveLib(arena, dir, lib_name, ".a")) |full_path| {
822 try libs.append(full_path);
823 } else {
824 log.warn("library not found for '-l{s}'", .{lib_name});
825 lib_not_found = true;
826 }
827 }
828 },
799 }829 }
800 }830 }
801831
...@@ -811,19 +841,23 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No...@@ -811,19 +841,23 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No
811 if (self.base.options.sysroot != null) blk: {841 if (self.base.options.sysroot != null) blk: {
812 // Try stub file first. If we hit it, then we're done as the stub file842 // Try stub file first. If we hit it, then we're done as the stub file
813 // re-exports every single symbol definition.843 // re-exports every single symbol definition.
814 if (try resolveLib(arena, lib_dirs.items, "System", ".tbd")) |full_path| {844 for (lib_dirs.items) |dir| {
815 try libs.append(full_path);845 if (try resolveLib(arena, dir, "System", ".tbd")) |full_path| {
816 libsystem_available = true;846 try libs.append(full_path);
817 break :blk;847 libsystem_available = true;
848 break :blk;
849 }
818 }850 }
819 // If we didn't hit the stub file, try .dylib next. However, libSystem.dylib851 // If we didn't hit the stub file, try .dylib next. However, libSystem.dylib
820 // doesn't export libc.dylib which we'll need to resolve subsequently also.852 // doesn't export libc.dylib which we'll need to resolve subsequently also.
821 if (try resolveLib(arena, lib_dirs.items, "System", ".dylib")) |libsystem_path| {853 for (lib_dirs.items) |dir| {
822 if (try resolveLib(arena, lib_dirs.items, "c", ".dylib")) |libc_path| {854 if (try resolveLib(arena, dir, "System", ".dylib")) |libsystem_path| {
823 try libs.append(libsystem_path);855 if (try resolveLib(arena, dir, "c", ".dylib")) |libc_path| {
824 try libs.append(libc_path);856 try libs.append(libsystem_path);
825 libsystem_available = true;857 try libs.append(libc_path);
826 break :blk;858 libsystem_available = true;
859 break :blk;
860 }
827 }861 }
828 }862 }
829 }863 }
...@@ -847,11 +881,13 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No...@@ -847,11 +881,13 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No
847 }881 }
848 }882 }
849883
850 for (self.base.options.frameworks) |framework| {884 outer: for (self.base.options.frameworks) |framework| {
851 for (&[_][]const u8{ ".tbd", ".dylib", "" }) |ext| {885 for (framework_dirs.items) |dir| {
852 if (try resolveFramework(arena, framework_dirs.items, framework, ext)) |full_path| {886 for (&[_][]const u8{ ".tbd", ".dylib", "" }) |ext| {
853 try libs.append(full_path);887 if (try resolveFramework(arena, dir, framework, ext)) |full_path| {
854 break;888 try libs.append(full_path);
889 continue :outer;
890 }
855 }891 }
856 } else {892 } else {
857 log.warn("framework not found for '-framework {s}'", .{framework});893 log.warn("framework not found for '-framework {s}'", .{framework});
...@@ -934,6 +970,11 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No...@@ -934,6 +970,11 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No
934 try argv.append(try std.fmt.allocPrint(arena, "0x{x}", .{pagezero_size}));970 try argv.append(try std.fmt.allocPrint(arena, "0x{x}", .{pagezero_size}));
935 }971 }
936972
973 if (self.base.options.search_strategy) |strat| switch (strat) {
974 .paths_first => try argv.append("-search_paths_first"),
975 .dylibs_first => try argv.append("-search_dylibs_first"),
976 };
977
937 if (self.base.options.entry) |entry| {978 if (self.base.options.entry) |entry| {
938 try argv.append("-e");979 try argv.append("-e");
939 try argv.append(entry);980 try argv.append(entry);
...@@ -1183,51 +1224,41 @@ fn resolveSearchDir(...@@ -1183,51 +1224,41 @@ fn resolveSearchDir(
11831224
1184fn resolveLib(1225fn resolveLib(
1185 arena: Allocator,1226 arena: Allocator,
1186 search_dirs: []const []const u8,1227 search_dir: []const u8,
1187 name: []const u8,1228 name: []const u8,
1188 ext: []const u8,1229 ext: []const u8,
1189) !?[]const u8 {1230) !?[]const u8 {
1190 const search_name = try std.fmt.allocPrint(arena, "lib{s}{s}", .{ name, ext });1231 const search_name = try std.fmt.allocPrint(arena, "lib{s}{s}", .{ name, ext });
1232 const full_path = try fs.path.join(arena, &[_][]const u8{ search_dir, search_name });
11911233
1192 for (search_dirs) |dir| {1234 // Check if the file exists.
1193 const full_path = try fs.path.join(arena, &[_][]const u8{ dir, search_name });1235 const tmp = fs.cwd().openFile(full_path, .{}) catch |err| switch (err) {
11941236 error.FileNotFound => return null,
1195 // Check if the file exists.1237 else => |e| return e,
1196 const tmp = fs.cwd().openFile(full_path, .{}) catch |err| switch (err) {1238 };
1197 error.FileNotFound => continue,1239 defer tmp.close();
1198 else => |e| return e,
1199 };
1200 defer tmp.close();
1201
1202 return full_path;
1203 }
12041240
1205 return null;1241 return full_path;
1206}1242}
12071243
1208fn resolveFramework(1244fn resolveFramework(
1209 arena: Allocator,1245 arena: Allocator,
1210 search_dirs: []const []const u8,1246 search_dir: []const u8,
1211 name: []const u8,1247 name: []const u8,
1212 ext: []const u8,1248 ext: []const u8,
1213) !?[]const u8 {1249) !?[]const u8 {
1214 const search_name = try std.fmt.allocPrint(arena, "{s}{s}", .{ name, ext });1250 const search_name = try std.fmt.allocPrint(arena, "{s}{s}", .{ name, ext });
1215 const prefix_path = try std.fmt.allocPrint(arena, "{s}.framework", .{name});1251 const prefix_path = try std.fmt.allocPrint(arena, "{s}.framework", .{name});
1252 const full_path = try fs.path.join(arena, &[_][]const u8{ search_dir, prefix_path, search_name });
12161253
1217 for (search_dirs) |dir| {1254 // Check if the file exists.
1218 const full_path = try fs.path.join(arena, &[_][]const u8{ dir, prefix_path, search_name });1255 const tmp = fs.cwd().openFile(full_path, .{}) catch |err| switch (err) {
12191256 error.FileNotFound => return null,
1220 // Check if the file exists.1257 else => |e| return e,
1221 const tmp = fs.cwd().openFile(full_path, .{}) catch |err| switch (err) {1258 };
1222 error.FileNotFound => continue,1259 defer tmp.close();
1223 else => |e| return e,
1224 };
1225 defer tmp.close();
1226
1227 return full_path;
1228 }
12291260
1230 return null;1261 return full_path;
1231}1262}
12321263
1233fn parseObject(self: *MachO, path: []const u8) !bool {1264fn parseObject(self: *MachO, path: []const u8) !bool {
src/main.zig+11-1
...@@ -448,6 +448,8 @@ const usage_build_generic =...@@ -448,6 +448,8 @@ const usage_build_generic =
448 \\ -install_name=[value] (Darwin) add dylib's install name448 \\ -install_name=[value] (Darwin) add dylib's install name
449 \\ --entitlements [path] (Darwin) add path to entitlements file for embedding in code signature449 \\ --entitlements [path] (Darwin) add path to entitlements file for embedding in code signature
450 \\ -pagezero_size [value] (Darwin) size of the __PAGEZERO segment in hexadecimal notation450 \\ -pagezero_size [value] (Darwin) size of the __PAGEZERO segment in hexadecimal notation
451 \\ -search_paths_first (Darwin) search each dir in library search paths for `libx.dylib` then `libx.a`
452 \\ -search_dylibs_first (Darwin) search `libx.dylib` in each dir in library search paths, then `libx.a`
451 \\ --import-memory (WebAssembly) import memory from the environment453 \\ --import-memory (WebAssembly) import memory from the environment
452 \\ --import-table (WebAssembly) import function table from the host environment454 \\ --import-table (WebAssembly) import function table from the host environment
453 \\ --export-table (WebAssembly) export function table to the host environment455 \\ --export-table (WebAssembly) export function table to the host environment
...@@ -696,6 +698,7 @@ fn buildOutputType(...@@ -696,6 +698,7 @@ fn buildOutputType(
696 var hash_style: link.HashStyle = .both;698 var hash_style: link.HashStyle = .both;
697 var entitlements: ?[]const u8 = null;699 var entitlements: ?[]const u8 = null;
698 var pagezero_size: ?u64 = null;700 var pagezero_size: ?u64 = null;
701 var search_strategy: ?link.File.MachO.SearchStrategy = null;
699702
700 // e.g. -m3dnow or -mno-outline-atomics. They correspond to std.Target llvm cpu feature names.703 // e.g. -m3dnow or -mno-outline-atomics. They correspond to std.Target llvm cpu feature names.
701 // This array is populated by zig cc frontend and then has to be converted to zig-style704 // This array is populated by zig cc frontend and then has to be converted to zig-style
...@@ -917,6 +920,10 @@ fn buildOutputType(...@@ -917,6 +920,10 @@ fn buildOutputType(
917 pagezero_size = std.fmt.parseUnsigned(u64, eatIntPrefix(next_arg, 16), 16) catch |err| {920 pagezero_size = std.fmt.parseUnsigned(u64, eatIntPrefix(next_arg, 16), 16) catch |err| {
918 fatal("unable to parse '{s}': {s}", .{ arg, @errorName(err) });921 fatal("unable to parse '{s}': {s}", .{ arg, @errorName(err) });
919 };922 };
923 } else if (mem.eql(u8, arg, "-search_paths_first")) {
924 search_strategy = .paths_first;
925 } else if (mem.eql(u8, arg, "-search_dylibs_first")) {
926 search_strategy = .dylibs_first;
920 } else if (mem.eql(u8, arg, "-T") or mem.eql(u8, arg, "--script")) {927 } else if (mem.eql(u8, arg, "-T") or mem.eql(u8, arg, "--script")) {
921 linker_script = args_iter.next() orelse {928 linker_script = args_iter.next() orelse {
922 fatal("expected parameter after {s}", .{arg});929 fatal("expected parameter after {s}", .{arg});
...@@ -1476,7 +1483,9 @@ fn buildOutputType(...@@ -1476,7 +1483,9 @@ fn buildOutputType(
1476 {1483 {
1477 force_static_libs = true;1484 force_static_libs = true;
1478 } else if (mem.eql(u8, linker_arg, "-search_paths_first")) {1485 } else if (mem.eql(u8, linker_arg, "-search_paths_first")) {
1479 // ignore, since it's the default behavior in both ld64 and zld1486 search_strategy = .paths_first;
1487 } else if (mem.eql(u8, linker_arg, "-search_dylibs_first")) {
1488 search_strategy = .dylibs_first;
1480 } else {1489 } else {
1481 try linker_args.append(linker_arg);1490 try linker_args.append(linker_arg);
1482 }1491 }
...@@ -2784,6 +2793,7 @@ fn buildOutputType(...@@ -2784,6 +2793,7 @@ fn buildOutputType(
2784 .install_name = install_name,2793 .install_name = install_name,
2785 .entitlements = entitlements,2794 .entitlements = entitlements,
2786 .pagezero_size = pagezero_size,2795 .pagezero_size = pagezero_size,
2796 .search_strategy = search_strategy,
2787 }) catch |err| switch (err) {2797 }) catch |err| switch (err) {
2788 error.LibCUnavailable => {2798 error.LibCUnavailable => {
2789 const target = target_info.target;2799 const target = target_info.target;