authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-01-13 22:09:42-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-01-13 22:09:42-05:00
log75b6637d6013b735d36da0ab6e5655002a1b59e9
tree76d3d25d4307138e34450da7ca30310f12197941
parent7e76aab98abe94e01cfd8b01ce288360ed67dbc7
parent5cde5f947fa12440463f684d9417ac00c8b9790a
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #10584 from ziglang/macho-rustc-fixes

zld: a couple of fixes which result in better rustc support

9 files changed, 150 insertions(+), 32 deletions(-)

src/Compilation.zig+13-4
......@@ -654,6 +654,11 @@ pub const ClangPreprocessorMode = enum {
654654pub const SystemLib = link.SystemLib;
655655pub const CacheMode = link.CacheMode;
656656
657pub const LinkObject = struct {
658 path: []const u8,
659 must_link: bool = false,
660};
661
657662pub const InitOptions = struct {
658663 zig_lib_directory: Directory,
659664 local_cache_directory: Directory,
......@@ -698,7 +703,7 @@ pub const InitOptions = struct {
698703 lib_dirs: []const []const u8 = &[0][]const u8{},
699704 rpath_list: []const []const u8 = &[0][]const u8{},
700705 c_source_files: []const CSourceFile = &[0]CSourceFile{},
701 link_objects: []const []const u8 = &[0][]const u8{},
706 link_objects: []LinkObject = &[0]LinkObject{},
702707 framework_dirs: []const []const u8 = &[0][]const u8{},
703708 frameworks: []const []const u8 = &[0][]const u8{},
704709 system_lib_names: []const []const u8 = &.{},
......@@ -1056,7 +1061,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
10561061 if (options.system_lib_names.len != 0)
10571062 break :x true;
10581063 for (options.link_objects) |obj| {
1059 switch (classifyFileExt(obj)) {
1064 switch (classifyFileExt(obj.path)) {
10601065 .shared_library => break :x true,
10611066 else => continue,
10621067 }
......@@ -1459,7 +1464,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
14591464 if (options.c_source_files.len >= 1) {
14601465 hash.addBytes(options.c_source_files[0].src_path);
14611466 } else if (options.link_objects.len >= 1) {
1462 hash.addBytes(options.link_objects[0]);
1467 hash.addBytes(options.link_objects[0].path);
14631468 }
14641469
14651470 const digest = hash.final();
......@@ -2265,7 +2270,11 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes
22652270
22662271 try man.addOptionalFile(comp.bin_file.options.linker_script);
22672272 try man.addOptionalFile(comp.bin_file.options.version_script);
2268 try man.addListOfFiles(comp.bin_file.options.objects);
2273
2274 for (comp.bin_file.options.objects) |obj| {
2275 _ = try man.addFile(obj.path, null);
2276 man.hash.add(obj.must_link);
2277 }
22692278
22702279 for (comp.c_object_table.keys()) |key| {
22712280 _ = try man.addFile(key.src.src_path, null);
src/link.zig+7-4
......@@ -155,7 +155,7 @@ pub const Options = struct {
155155 soname: ?[]const u8,
156156 llvm_cpu_features: ?[*:0]const u8,
157157
158 objects: []const []const u8,
158 objects: []Compilation.LinkObject,
159159 framework_dirs: []const []const u8,
160160 frameworks: []const []const u8,
161161 system_libs: std.StringArrayHashMapUnmanaged(SystemLib),
......@@ -755,7 +755,10 @@ pub const File = struct {
755755 // We are about to obtain this lock, so here we give other processes a chance first.
756756 base.releaseLock();
757757
758 try man.addListOfFiles(base.options.objects);
758 for (base.options.objects) |obj| {
759 _ = try man.addFile(obj.path, null);
760 man.hash.add(obj.must_link);
761 }
759762 for (comp.c_object_table.keys()) |key| {
760763 _ = try man.addFile(key.status.success.object_path, null);
761764 }
......@@ -792,8 +795,8 @@ pub const File = struct {
792795 var object_files = try std.ArrayList([*:0]const u8).initCapacity(base.allocator, num_object_files);
793796 defer object_files.deinit();
794797
795 for (base.options.objects) |obj_path| {
796 object_files.appendAssumeCapacity(try arena.dupeZ(u8, obj_path));
798 for (base.options.objects) |obj| {
799 object_files.appendAssumeCapacity(try arena.dupeZ(u8, obj.path));
797800 }
798801 for (comp.c_object_table.keys()) |key| {
799802 object_files.appendAssumeCapacity(try arena.dupeZ(u8, key.status.success.object_path));
src/link/Coff.zig+9-3
......@@ -943,7 +943,10 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void {
943943
944944 comptime assert(Compilation.link_hash_implementation_version == 1);
945945
946 try man.addListOfFiles(self.base.options.objects);
946 for (self.base.options.objects) |obj| {
947 _ = try man.addFile(obj.path, null);
948 man.hash.add(obj.must_link);
949 }
947950 for (comp.c_object_table.keys()) |key| {
948951 _ = try man.addFile(key.status.success.object_path, null);
949952 }
......@@ -1005,7 +1008,7 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void {
10051008 // build-obj. See also the corresponding TODO in linkAsArchive.
10061009 const the_object_path = blk: {
10071010 if (self.base.options.objects.len != 0)
1008 break :blk self.base.options.objects[0];
1011 break :blk self.base.options.objects[0].path;
10091012
10101013 if (comp.c_object_table.count() != 0)
10111014 break :blk comp.c_object_table.keys()[0].status.success.object_path;
......@@ -1110,7 +1113,10 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void {
11101113 try argv.append(try allocPrint(arena, "-LIBPATH:{s}", .{lib_dir}));
11111114 }
11121115
1113 try argv.appendSlice(self.base.options.objects);
1116 try argv.ensureUnusedCapacity(self.base.options.objects.len);
1117 for (self.base.options.objects) |obj| {
1118 argv.appendAssumeCapacity(obj.path);
1119 }
11141120
11151121 for (comp.c_object_table.keys()) |key| {
11161122 try argv.append(key.status.success.object_path);
src/link/Elf.zig+9-3
......@@ -1384,7 +1384,10 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
13841384
13851385 try man.addOptionalFile(self.base.options.linker_script);
13861386 try man.addOptionalFile(self.base.options.version_script);
1387 try man.addListOfFiles(self.base.options.objects);
1387 for (self.base.options.objects) |obj| {
1388 _ = try man.addFile(obj.path, null);
1389 man.hash.add(obj.must_link);
1390 }
13881391 for (comp.c_object_table.keys()) |key| {
13891392 _ = try man.addFile(key.status.success.object_path, null);
13901393 }
......@@ -1469,7 +1472,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
14691472 // build-obj. See also the corresponding TODO in linkAsArchive.
14701473 const the_object_path = blk: {
14711474 if (self.base.options.objects.len != 0)
1472 break :blk self.base.options.objects[0];
1475 break :blk self.base.options.objects[0].path;
14731476
14741477 if (comp.c_object_table.count() != 0)
14751478 break :blk comp.c_object_table.keys()[0].status.success.object_path;
......@@ -1678,7 +1681,10 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
16781681 }
16791682
16801683 // Positional arguments to the linker such as object files.
1681 try argv.appendSlice(self.base.options.objects);
1684 try argv.ensureUnusedCapacity(self.base.options.objects.len);
1685 for (self.base.options.objects) |obj| {
1686 argv.appendAssumeCapacity(obj.path);
1687 }
16821688
16831689 for (comp.c_object_table.keys()) |key| {
16841690 try argv.append(key.status.success.object_path);
src/link/MachO.zig+86-8
......@@ -141,6 +141,9 @@ objc_selrefs_section_index: ?u16 = null,
141141objc_classrefs_section_index: ?u16 = null,
142142objc_data_section_index: ?u16 = null,
143143
144rustc_section_index: ?u16 = null,
145rustc_section_size: u64 = 0,
146
144147bss_file_offset: u32 = 0,
145148tlv_bss_file_offset: u32 = 0,
146149
......@@ -496,7 +499,10 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
496499
497500 comptime assert(Compilation.link_hash_implementation_version == 1);
498501
499 try man.addListOfFiles(self.base.options.objects);
502 for (self.base.options.objects) |obj| {
503 _ = try man.addFile(obj.path, null);
504 man.hash.add(obj.must_link);
505 }
500506 for (comp.c_object_table.keys()) |key| {
501507 _ = try man.addFile(key.status.success.object_path, null);
502508 }
......@@ -568,8 +574,9 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
568574 // here. TODO: think carefully about how we can avoid this redundant operation when doing
569575 // build-obj. See also the corresponding TODO in linkAsArchive.
570576 const the_object_path = blk: {
571 if (self.base.options.objects.len != 0)
572 break :blk self.base.options.objects[0];
577 if (self.base.options.objects.len != 0) {
578 break :blk self.base.options.objects[0].path;
579 }
573580
574581 if (comp.c_object_table.count() != 0)
575582 break :blk comp.c_object_table.keys()[0].status.success.object_path;
......@@ -678,8 +685,19 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
678685
679686 // Positional arguments to the linker such as object files and static archives.
680687 var positionals = std.ArrayList([]const u8).init(arena);
688 try positionals.ensureUnusedCapacity(self.base.options.objects.len);
681689
682 try positionals.appendSlice(self.base.options.objects);
690 var must_link_archives = std.StringArrayHashMap(void).init(arena);
691 try must_link_archives.ensureUnusedCapacity(self.base.options.objects.len);
692
693 for (self.base.options.objects) |obj| {
694 if (must_link_archives.contains(obj.path)) continue;
695 if (obj.must_link) {
696 _ = must_link_archives.getOrPutAssumeCapacity(obj.path);
697 } else {
698 _ = positionals.appendAssumeCapacity(obj.path);
699 }
700 }
683701
684702 for (comp.c_object_table.keys()) |key| {
685703 try positionals.append(key.status.success.object_path);
......@@ -886,12 +904,17 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
886904 try argv.append("dynamic_lookup");
887905 }
888906
907 for (must_link_archives.keys()) |lib| {
908 try argv.append(try std.fmt.allocPrint(arena, "-force_load {s}", .{lib}));
909 }
910
889911 Compilation.dump_argv(argv.items);
890912 }
891913
892914 var dependent_libs = std.fifo.LinearFifo(Dylib.Id, .Dynamic).init(self.base.allocator);
893915 defer dependent_libs.deinit();
894916 try self.parseInputFiles(positionals.items, self.base.options.sysroot, &dependent_libs);
917 try self.parseAndForceLoadStaticArchives(must_link_archives.keys());
895918 try self.parseLibs(libs.items, self.base.options.sysroot, &dependent_libs);
896919 try self.parseDependentLibs(self.base.options.sysroot, &dependent_libs);
897920 }
......@@ -993,6 +1016,11 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
9931016 try self.writeAtoms();
9941017 }
9951018
1019 if (self.rustc_section_index) |id| {
1020 const seg = &self.load_commands.items[self.data_segment_cmd_index.?].segment;
1021 const sect = &seg.sections.items[id];
1022 sect.size = self.rustc_section_size;
1023 }
9961024 if (self.bss_section_index) |idx| {
9971025 const seg = &self.load_commands.items[self.data_segment_cmd_index.?].segment;
9981026 const sect = &seg.sections.items[idx];
......@@ -1195,7 +1223,7 @@ fn parseObject(self: *MachO, path: []const u8) !bool {
11951223 return true;
11961224}
11971225
1198fn parseArchive(self: *MachO, path: []const u8) !bool {
1226fn parseArchive(self: *MachO, path: []const u8, force_load: bool) !bool {
11991227 const file = fs.cwd().openFile(path, .{}) catch |err| switch (err) {
12001228 error.FileNotFound => return false,
12011229 else => |e| return e,
......@@ -1218,7 +1246,23 @@ fn parseArchive(self: *MachO, path: []const u8) !bool {
12181246 else => |e| return e,
12191247 };
12201248
1221 try self.archives.append(self.base.allocator, archive);
1249 if (force_load) {
1250 defer archive.deinit(self.base.allocator);
1251 // Get all offsets from the ToC
1252 var offsets = std.AutoArrayHashMap(u32, void).init(self.base.allocator);
1253 defer offsets.deinit();
1254 for (archive.toc.values()) |offs| {
1255 for (offs.items) |off| {
1256 _ = try offsets.getOrPut(off);
1257 }
1258 }
1259 for (offsets.keys()) |off| {
1260 const object = try self.objects.addOne(self.base.allocator);
1261 object.* = try archive.parseObject(self.base.allocator, self.base.options.target, off);
1262 }
1263 } else {
1264 try self.archives.append(self.base.allocator, archive);
1265 }
12221266
12231267 return true;
12241268}
......@@ -1303,7 +1347,7 @@ fn parseInputFiles(self: *MachO, files: []const []const u8, syslibroot: ?[]const
13031347 log.debug("parsing input file path '{s}'", .{full_path});
13041348
13051349 if (try self.parseObject(full_path)) continue;
1306 if (try self.parseArchive(full_path)) continue;
1350 if (try self.parseArchive(full_path, false)) continue;
13071351 if (try self.parseDylib(full_path, .{
13081352 .syslibroot = syslibroot,
13091353 .dependent_libs = dependent_libs,
......@@ -1313,6 +1357,21 @@ fn parseInputFiles(self: *MachO, files: []const []const u8, syslibroot: ?[]const
13131357 }
13141358}
13151359
1360fn parseAndForceLoadStaticArchives(self: *MachO, files: []const []const u8) !void {
1361 for (files) |file_name| {
1362 const full_path = full_path: {
1363 var buffer: [fs.MAX_PATH_BYTES]u8 = undefined;
1364 const path = try fs.realpath(file_name, &buffer);
1365 break :full_path try self.base.allocator.dupe(u8, path);
1366 };
1367 defer self.base.allocator.free(full_path);
1368 log.debug("parsing and force loading static archive '{s}'", .{full_path});
1369
1370 if (try self.parseArchive(full_path, true)) continue;
1371 log.warn("unknown filetype: expected static archive: '{s}'", .{file_name});
1372 }
1373}
1374
13161375fn parseLibs(self: *MachO, libs: []const []const u8, syslibroot: ?[]const u8, dependent_libs: anytype) !void {
13171376 for (libs) |lib| {
13181377 log.debug("parsing lib path '{s}'", .{lib});
......@@ -1320,7 +1379,7 @@ fn parseLibs(self: *MachO, libs: []const []const u8, syslibroot: ?[]const u8, de
13201379 .syslibroot = syslibroot,
13211380 .dependent_libs = dependent_libs,
13221381 })) continue;
1323 if (try self.parseArchive(lib)) continue;
1382 if (try self.parseArchive(lib, false)) continue;
13241383
13251384 log.warn("unknown filetype for a library: '{s}'", .{lib});
13261385 }
......@@ -1886,6 +1945,24 @@ pub fn getMatchingSection(self: *MachO, sect: macho.section_64) !?MatchingSectio
18861945 .seg = self.data_segment_cmd_index.?,
18871946 .sect = self.objc_data_section_index.?,
18881947 };
1948 } else if (mem.eql(u8, sectname, ".rustc")) {
1949 if (self.rustc_section_index == null) {
1950 self.rustc_section_index = try self.initSection(
1951 self.data_segment_cmd_index.?,
1952 ".rustc",
1953 sect.size,
1954 sect.@"align",
1955 .{},
1956 );
1957 // We need to preserve the section size for rustc to properly
1958 // decompress the metadata.
1959 self.rustc_section_size = sect.size;
1960 }
1961
1962 break :blk .{
1963 .seg = self.data_segment_cmd_index.?,
1964 .sect = self.rustc_section_index.?,
1965 };
18891966 } else {
18901967 if (self.data_section_index == null) {
18911968 self.data_section_index = try self.initSection(
......@@ -5212,6 +5289,7 @@ fn sortSections(self: *MachO) !void {
52125289
52135290 // __DATA segment
52145291 const indices = &[_]*?u16{
5292 &self.rustc_section_index,
52155293 &self.la_symbol_ptr_section_index,
52165294 &self.objc_const_section_index,
52175295 &self.objc_selrefs_section_index,
src/link/MachO/Atom.zig+2-1
......@@ -419,6 +419,7 @@ pub fn parseRelocs(self: *Atom, relocs: []macho.relocation_info, context: RelocC
419419 .X86_64_RELOC_BRANCH => {
420420 // TODO rewrite relocation
421421 try addStub(target, context);
422 addend = mem.readIntLittle(i32, self.code.items[offset..][0..4]);
422423 },
423424 .X86_64_RELOC_GOT, .X86_64_RELOC_GOT_LOAD => {
424425 // TODO rewrite relocation
......@@ -1003,7 +1004,7 @@ pub fn resolveRelocs(self: *Atom, macho_file: *MachO) !void {
10031004 .X86_64_RELOC_BRANCH => {
10041005 const displacement = try math.cast(
10051006 i32,
1006 @intCast(i64, target_addr) - @intCast(i64, source_addr) - 4,
1007 @intCast(i64, target_addr) - @intCast(i64, source_addr) - 4 + rel.addend,
10071008 );
10081009 mem.writeIntLittle(u32, self.code.items[rel.offset..][0..4], @bitCast(u32, displacement));
10091010 },
src/link/MachO/Object.zig+1-1
......@@ -409,7 +409,7 @@ pub fn parseIntoAtoms(self: *Object, allocator: Allocator, macho_file: *MachO) !
409409 } else blk: {
410410 var iundefsym: usize = sorted_all_nlists.items.len;
411411 while (iundefsym > 0) : (iundefsym -= 1) {
412 const nlist = sorted_all_nlists.items[iundefsym];
412 const nlist = sorted_all_nlists.items[iundefsym - 1];
413413 if (nlist.nlist.sect()) break;
414414 }
415415 break :blk iundefsym;
src/link/Wasm.zig+9-3
......@@ -1128,7 +1128,10 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
11281128
11291129 comptime assert(Compilation.link_hash_implementation_version == 1);
11301130
1131 try man.addListOfFiles(self.base.options.objects);
1131 for (self.base.options.objects) |obj| {
1132 _ = try man.addFile(obj.path, null);
1133 man.hash.add(obj.must_link);
1134 }
11321135 for (comp.c_object_table.keys()) |key| {
11331136 _ = try man.addFile(key.status.success.object_path, null);
11341137 }
......@@ -1181,7 +1184,7 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
11811184 // build-obj. See also the corresponding TODO in linkAsArchive.
11821185 const the_object_path = blk: {
11831186 if (self.base.options.objects.len != 0)
1184 break :blk self.base.options.objects[0];
1187 break :blk self.base.options.objects[0].path;
11851188
11861189 if (comp.c_object_table.count() != 0)
11871190 break :blk comp.c_object_table.keys()[0].status.success.object_path;
......@@ -1346,7 +1349,10 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
13461349 }
13471350
13481351 // Positional arguments to the linker such as object files.
1349 try argv.appendSlice(self.base.options.objects);
1352 try argv.ensureUnusedCapacity(self.base.options.objects.len);
1353 for (self.base.options.objects) |obj| {
1354 argv.appendAssumeCapacity(obj.path);
1355 }
13501356
13511357 for (comp.c_object_table.keys()) |key| {
13521358 try argv.append(key.status.success.object_path);
src/main.zig+14-5
......@@ -705,7 +705,7 @@ fn buildOutputType(
705705 var c_source_files = std.ArrayList(Compilation.CSourceFile).init(gpa);
706706 defer c_source_files.deinit();
707707
708 var link_objects = std.ArrayList([]const u8).init(gpa);
708 var link_objects = std.ArrayList(Compilation.LinkObject).init(gpa);
709709 defer link_objects.deinit();
710710
711711 var framework_dirs = std.ArrayList([]const u8).init(gpa);
......@@ -1238,7 +1238,7 @@ fn buildOutputType(
12381238 }
12391239 } else switch (Compilation.classifyFileExt(arg)) {
12401240 .object, .static_library, .shared_library => {
1241 try link_objects.append(arg);
1241 try link_objects.append(.{ .path = arg });
12421242 },
12431243 .assembly, .c, .cpp, .h, .ll, .bc, .m, .mm => {
12441244 try c_source_files.append(.{
......@@ -1309,7 +1309,7 @@ fn buildOutputType(
13091309 switch (file_ext) {
13101310 .assembly, .c, .cpp, .ll, .bc, .h, .m, .mm => try c_source_files.append(.{ .src_path = it.only_arg }),
13111311 .unknown, .shared_library, .object, .static_library => {
1312 try link_objects.append(it.only_arg);
1312 try link_objects.append(.{ .path = it.only_arg });
13131313 },
13141314 .zig => {
13151315 if (root_src_file) |other| {
......@@ -1748,6 +1748,15 @@ fn buildOutputType(
17481748 fatal("expected linker arg after '{s}'", .{arg});
17491749 }
17501750 install_name = linker_args.items[i];
1751 } else if (mem.eql(u8, arg, "-force_load")) {
1752 i += 1;
1753 if (i >= linker_args.items.len) {
1754 fatal("expected linker arg after '{s}'", .{arg});
1755 }
1756 try link_objects.append(.{
1757 .path = linker_args.items[i],
1758 .must_link = true,
1759 });
17511760 } else {
17521761 warn("unsupported linker arg: {s}", .{arg});
17531762 }
......@@ -1842,7 +1851,7 @@ fn buildOutputType(
18421851 const basename = fs.path.basename(c_source_files.items[0].src_path);
18431852 break :blk basename[0 .. basename.len - fs.path.extension(basename).len];
18441853 } else if (link_objects.items.len >= 1) {
1845 const basename = fs.path.basename(link_objects.items[0]);
1854 const basename = fs.path.basename(link_objects.items[0].path);
18461855 break :blk basename[0 .. basename.len - fs.path.extension(basename).len];
18471856 } else if (emit_bin == .yes) {
18481857 const basename = fs.path.basename(emit_bin.yes);
......@@ -2045,7 +2054,7 @@ fn buildOutputType(
20452054 test_path.items, @errorName(e),
20462055 }),
20472056 };
2048 try link_objects.append(try arena.dupe(u8, test_path.items));
2057 try link_objects.append(.{ .path = try arena.dupe(u8, test_path.items) });
20492058 break;
20502059 } else {
20512060 var search_paths = std.ArrayList(u8).init(arena);