authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-01-13 22:09:42-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-02 21:45:44-07:00
log148b963a606c2a7b71dced22d1d5b42cce4d9c04
treeb13ecc2a299ece729ad92b4e4a35185494a2896d
parentb713ce0249e68edd5db6fea79df4fffadbdc7ef9

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

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

9 files changed, 146 insertions(+), 31 deletions(-)

src/Compilation.zig+8-3
......@@ -636,6 +636,11 @@ pub const ClangPreprocessorMode = enum {
636636
637637pub const SystemLib = link.SystemLib;
638638
639pub const LinkObject = struct {
640 path: []const u8,
641 must_link: bool = false,
642};
643
639644pub const InitOptions = struct {
640645 zig_lib_directory: Directory,
641646 local_cache_directory: Directory,
......@@ -679,7 +684,7 @@ pub const InitOptions = struct {
679684 lib_dirs: []const []const u8 = &[0][]const u8{},
680685 rpath_list: []const []const u8 = &[0][]const u8{},
681686 c_source_files: []const CSourceFile = &[0]CSourceFile{},
682 link_objects: []const []const u8 = &[0][]const u8{},
687 link_objects: []LinkObject = &[0]LinkObject{},
683688 framework_dirs: []const []const u8 = &[0][]const u8{},
684689 frameworks: []const []const u8 = &[0][]const u8{},
685690 system_lib_names: []const []const u8 = &.{},
......@@ -1027,7 +1032,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
10271032 if (options.system_lib_names.len != 0)
10281033 break :x true;
10291034 for (options.link_objects) |obj| {
1030 switch (classifyFileExt(obj)) {
1035 switch (classifyFileExt(obj.path)) {
10311036 .shared_library => break :x true,
10321037 else => continue,
10331038 }
......@@ -1389,7 +1394,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
13891394 if (options.c_source_files.len >= 1) {
13901395 hash.addBytes(options.c_source_files[0].src_path);
13911396 } else if (options.link_objects.len >= 1) {
1392 hash.addBytes(options.link_objects[0]);
1397 hash.addBytes(options.link_objects[0].path);
13931398 }
13941399
13951400 const digest = hash.final();
src/link.zig+7-4
......@@ -137,7 +137,7 @@ pub const Options = struct {
137137 soname: ?[]const u8,
138138 llvm_cpu_features: ?[*:0]const u8,
139139
140 objects: []const []const u8,
140 objects: []Compilation.LinkObject,
141141 framework_dirs: []const []const u8,
142142 frameworks: []const []const u8,
143143 system_libs: std.StringArrayHashMapUnmanaged(SystemLib),
......@@ -683,7 +683,10 @@ pub const File = struct {
683683 // We are about to obtain this lock, so here we give other processes a chance first.
684684 base.releaseLock();
685685
686 try man.addListOfFiles(base.options.objects);
686 for (base.options.objects) |obj| {
687 _ = try man.addFile(obj.path, null);
688 man.hash.add(obj.must_link);
689 }
687690 for (comp.c_object_table.keys()) |key| {
688691 _ = try man.addFile(key.status.success.object_path, null);
689692 }
......@@ -720,8 +723,8 @@ pub const File = struct {
720723 var object_files = try std.ArrayList([*:0]const u8).initCapacity(base.allocator, num_object_files);
721724 defer object_files.deinit();
722725
723 for (base.options.objects) |obj_path| {
724 object_files.appendAssumeCapacity(try arena.dupeZ(u8, obj_path));
726 for (base.options.objects) |obj| {
727 object_files.appendAssumeCapacity(try arena.dupeZ(u8, obj.path));
725728 }
726729 for (comp.c_object_table.keys()) |key| {
727730 object_files.appendAssumeCapacity(try arena.dupeZ(u8, key.status.success.object_path));
src/link/Coff.zig+9-3
......@@ -920,7 +920,10 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void {
920920 man = comp.cache_parent.obtain();
921921 self.base.releaseLock();
922922
923 try man.addListOfFiles(self.base.options.objects);
923 for (self.base.options.objects) |obj| {
924 _ = try man.addFile(obj.path, null);
925 man.hash.add(obj.must_link);
926 }
924927 for (comp.c_object_table.keys()) |key| {
925928 _ = try man.addFile(key.status.success.object_path, null);
926929 }
......@@ -984,7 +987,7 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void {
984987 // build-obj. See also the corresponding TODO in linkAsArchive.
985988 const the_object_path = blk: {
986989 if (self.base.options.objects.len != 0)
987 break :blk self.base.options.objects[0];
990 break :blk self.base.options.objects[0].path;
988991
989992 if (comp.c_object_table.count() != 0)
990993 break :blk comp.c_object_table.keys()[0].status.success.object_path;
......@@ -1093,7 +1096,10 @@ fn linkWithLLD(self: *Coff, comp: *Compilation) !void {
10931096 try argv.append(try allocPrint(arena, "-LIBPATH:{s}", .{lib_dir}));
10941097 }
10951098
1096 try argv.appendSlice(self.base.options.objects);
1099 try argv.ensureUnusedCapacity(self.base.options.objects.len);
1100 for (self.base.options.objects) |obj| {
1101 argv.appendAssumeCapacity(obj.path);
1102 }
10971103
10981104 for (comp.c_object_table.keys()) |key| {
10991105 try argv.append(key.status.success.object_path);
src/link/Elf.zig+9-3
......@@ -1307,7 +1307,10 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
13071307
13081308 try man.addOptionalFile(self.base.options.linker_script);
13091309 try man.addOptionalFile(self.base.options.version_script);
1310 try man.addListOfFiles(self.base.options.objects);
1310 for (self.base.options.objects) |obj| {
1311 _ = try man.addFile(obj.path, null);
1312 man.hash.add(obj.must_link);
1313 }
13111314 for (comp.c_object_table.keys()) |key| {
13121315 _ = try man.addFile(key.status.success.object_path, null);
13131316 }
......@@ -1392,7 +1395,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
13921395 // build-obj. See also the corresponding TODO in linkAsArchive.
13931396 const the_object_path = blk: {
13941397 if (self.base.options.objects.len != 0)
1395 break :blk self.base.options.objects[0];
1398 break :blk self.base.options.objects[0].path;
13961399
13971400 if (comp.c_object_table.count() != 0)
13981401 break :blk comp.c_object_table.keys()[0].status.success.object_path;
......@@ -1607,7 +1610,10 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
16071610 }
16081611
16091612 // Positional arguments to the linker such as object files.
1610 try argv.appendSlice(self.base.options.objects);
1613 try argv.ensureUnusedCapacity(self.base.options.objects.len);
1614 for (self.base.options.objects) |obj| {
1615 argv.appendAssumeCapacity(obj.path);
1616 }
16111617
16121618 for (comp.c_object_table.keys()) |key| {
16131619 try argv.append(key.status.success.object_path);
src/link/MachO.zig+87-8
......@@ -140,6 +140,9 @@ objc_selrefs_section_index: ?u16 = null,
140140objc_classrefs_section_index: ?u16 = null,
141141objc_data_section_index: ?u16 = null,
142142
143rustc_section_index: ?u16 = null,
144rustc_section_size: u64 = 0,
145
143146bss_file_offset: u32 = 0,
144147tlv_bss_file_offset: u32 = 0,
145148
......@@ -466,7 +469,10 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
466469 // We are about to obtain this lock, so here we give other processes a chance first.
467470 self.base.releaseLock();
468471
469 try man.addListOfFiles(self.base.options.objects);
472 for (self.base.options.objects) |obj| {
473 _ = try man.addFile(obj.path, null);
474 man.hash.add(obj.must_link);
475 }
470476 for (comp.c_object_table.keys()) |key| {
471477 _ = try man.addFile(key.status.success.object_path, null);
472478 }
......@@ -539,8 +545,9 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
539545 // here. TODO: think carefully about how we can avoid this redundant operation when doing
540546 // build-obj. See also the corresponding TODO in linkAsArchive.
541547 const the_object_path = blk: {
542 if (self.base.options.objects.len != 0)
543 break :blk self.base.options.objects[0];
548 if (self.base.options.objects.len != 0) {
549 break :blk self.base.options.objects[0].path;
550 }
544551
545552 if (comp.c_object_table.count() != 0)
546553 break :blk comp.c_object_table.keys()[0].status.success.object_path;
......@@ -649,8 +656,19 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
649656
650657 // Positional arguments to the linker such as object files and static archives.
651658 var positionals = std.ArrayList([]const u8).init(arena);
659 try positionals.ensureUnusedCapacity(self.base.options.objects.len);
652660
653 try positionals.appendSlice(self.base.options.objects);
661 var must_link_archives = std.StringArrayHashMap(void).init(arena);
662 try must_link_archives.ensureUnusedCapacity(self.base.options.objects.len);
663
664 for (self.base.options.objects) |obj| {
665 if (must_link_archives.contains(obj.path)) continue;
666 if (obj.must_link) {
667 _ = must_link_archives.getOrPutAssumeCapacity(obj.path);
668 } else {
669 _ = positionals.appendAssumeCapacity(obj.path);
670 }
671 }
654672
655673 for (comp.c_object_table.keys()) |key| {
656674 try positionals.append(key.status.success.object_path);
......@@ -857,12 +875,17 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
857875 try argv.append("dynamic_lookup");
858876 }
859877
878 for (must_link_archives.keys()) |lib| {
879 try argv.append(try std.fmt.allocPrint(arena, "-force_load {s}", .{lib}));
880 }
881
860882 Compilation.dump_argv(argv.items);
861883 }
862884
863885 var dependent_libs = std.fifo.LinearFifo(Dylib.Id, .Dynamic).init(self.base.allocator);
864886 defer dependent_libs.deinit();
865887 try self.parseInputFiles(positionals.items, self.base.options.sysroot, &dependent_libs);
888 try self.parseAndForceLoadStaticArchives(must_link_archives.keys());
866889 try self.parseLibs(libs.items, self.base.options.sysroot, &dependent_libs);
867890 try self.parseDependentLibs(self.base.options.sysroot, &dependent_libs);
868891 }
......@@ -953,6 +976,12 @@ pub fn flushModule(self: *MachO, comp: *Compilation) !void {
953976 try self.writeAtoms();
954977 }
955978
979 if (self.rustc_section_index) |id| {
980 const seg = &self.load_commands.items[self.data_segment_cmd_index.?].segment;
981 const sect = &seg.sections.items[id];
982 sect.size = self.rustc_section_size;
983 }
984
956985 try self.setEntryPoint();
957986 try self.updateSectionOrdinals();
958987 try self.writeLinkeditSegment();
......@@ -1142,7 +1171,7 @@ fn parseObject(self: *MachO, path: []const u8) !bool {
11421171 return true;
11431172}
11441173
1145fn parseArchive(self: *MachO, path: []const u8) !bool {
1174fn parseArchive(self: *MachO, path: []const u8, force_load: bool) !bool {
11461175 const file = fs.cwd().openFile(path, .{}) catch |err| switch (err) {
11471176 error.FileNotFound => return false,
11481177 else => |e| return e,
......@@ -1165,7 +1194,23 @@ fn parseArchive(self: *MachO, path: []const u8) !bool {
11651194 else => |e| return e,
11661195 };
11671196
1168 try self.archives.append(self.base.allocator, archive);
1197 if (force_load) {
1198 defer archive.deinit(self.base.allocator);
1199 // Get all offsets from the ToC
1200 var offsets = std.AutoArrayHashMap(u32, void).init(self.base.allocator);
1201 defer offsets.deinit();
1202 for (archive.toc.values()) |offs| {
1203 for (offs.items) |off| {
1204 _ = try offsets.getOrPut(off);
1205 }
1206 }
1207 for (offsets.keys()) |off| {
1208 const object = try self.objects.addOne(self.base.allocator);
1209 object.* = try archive.parseObject(self.base.allocator, self.base.options.target, off);
1210 }
1211 } else {
1212 try self.archives.append(self.base.allocator, archive);
1213 }
11691214
11701215 return true;
11711216}
......@@ -1250,7 +1295,7 @@ fn parseInputFiles(self: *MachO, files: []const []const u8, syslibroot: ?[]const
12501295 log.debug("parsing input file path '{s}'", .{full_path});
12511296
12521297 if (try self.parseObject(full_path)) continue;
1253 if (try self.parseArchive(full_path)) continue;
1298 if (try self.parseArchive(full_path, false)) continue;
12541299 if (try self.parseDylib(full_path, .{
12551300 .syslibroot = syslibroot,
12561301 .dependent_libs = dependent_libs,
......@@ -1260,6 +1305,21 @@ fn parseInputFiles(self: *MachO, files: []const []const u8, syslibroot: ?[]const
12601305 }
12611306}
12621307
1308fn parseAndForceLoadStaticArchives(self: *MachO, files: []const []const u8) !void {
1309 for (files) |file_name| {
1310 const full_path = full_path: {
1311 var buffer: [fs.MAX_PATH_BYTES]u8 = undefined;
1312 const path = try fs.realpath(file_name, &buffer);
1313 break :full_path try self.base.allocator.dupe(u8, path);
1314 };
1315 defer self.base.allocator.free(full_path);
1316 log.debug("parsing and force loading static archive '{s}'", .{full_path});
1317
1318 if (try self.parseArchive(full_path, true)) continue;
1319 log.warn("unknown filetype: expected static archive: '{s}'", .{file_name});
1320 }
1321}
1322
12631323fn parseLibs(self: *MachO, libs: []const []const u8, syslibroot: ?[]const u8, dependent_libs: anytype) !void {
12641324 for (libs) |lib| {
12651325 log.debug("parsing lib path '{s}'", .{lib});
......@@ -1267,7 +1327,7 @@ fn parseLibs(self: *MachO, libs: []const []const u8, syslibroot: ?[]const u8, de
12671327 .syslibroot = syslibroot,
12681328 .dependent_libs = dependent_libs,
12691329 })) continue;
1270 if (try self.parseArchive(lib)) continue;
1330 if (try self.parseArchive(lib, false)) continue;
12711331
12721332 log.warn("unknown filetype for a library: '{s}'", .{lib});
12731333 }
......@@ -1833,6 +1893,24 @@ pub fn getMatchingSection(self: *MachO, sect: macho.section_64) !?MatchingSectio
18331893 .seg = self.data_segment_cmd_index.?,
18341894 .sect = self.objc_data_section_index.?,
18351895 };
1896 } else if (mem.eql(u8, sectname, ".rustc")) {
1897 if (self.rustc_section_index == null) {
1898 self.rustc_section_index = try self.initSection(
1899 self.data_segment_cmd_index.?,
1900 ".rustc",
1901 sect.size,
1902 sect.@"align",
1903 .{},
1904 );
1905 // We need to preserve the section size for rustc to properly
1906 // decompress the metadata.
1907 self.rustc_section_size = sect.size;
1908 }
1909
1910 break :blk .{
1911 .seg = self.data_segment_cmd_index.?,
1912 .sect = self.rustc_section_index.?,
1913 };
18361914 } else {
18371915 if (self.data_section_index == null) {
18381916 self.data_section_index = try self.initSection(
......@@ -5003,6 +5081,7 @@ fn sortSections(self: *MachO) !void {
50035081
50045082 // __DATA segment
50055083 const indices = &[_]*?u16{
5084 &self.rustc_section_index,
50065085 &self.la_symbol_ptr_section_index,
50075086 &self.objc_const_section_index,
50085087 &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
......@@ -1012,7 +1012,10 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
10121012 // We are about to obtain this lock, so here we give other processes a chance first.
10131013 self.base.releaseLock();
10141014
1015 try man.addListOfFiles(self.base.options.objects);
1015 for (self.base.options.objects) |obj| {
1016 _ = try man.addFile(obj.path, null);
1017 man.hash.add(obj.must_link);
1018 }
10161019 for (comp.c_object_table.keys()) |key| {
10171020 _ = try man.addFile(key.status.success.object_path, null);
10181021 }
......@@ -1065,7 +1068,7 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
10651068 // build-obj. See also the corresponding TODO in linkAsArchive.
10661069 const the_object_path = blk: {
10671070 if (self.base.options.objects.len != 0)
1068 break :blk self.base.options.objects[0];
1071 break :blk self.base.options.objects[0].path;
10691072
10701073 if (comp.c_object_table.count() != 0)
10711074 break :blk comp.c_object_table.keys()[0].status.success.object_path;
......@@ -1225,7 +1228,10 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void {
12251228 }
12261229
12271230 // Positional arguments to the linker such as object files.
1228 try argv.appendSlice(self.base.options.objects);
1231 try argv.ensureUnusedCapacity(self.base.options.objects.len);
1232 for (self.base.options.objects) |obj| {
1233 argv.appendAssumeCapacity(obj.path);
1234 }
12291235
12301236 for (comp.c_object_table.keys()) |key| {
12311237 try argv.append(key.status.success.object_path);
src/main.zig+14-5
......@@ -703,7 +703,7 @@ fn buildOutputType(
703703 var c_source_files = std.ArrayList(Compilation.CSourceFile).init(gpa);
704704 defer c_source_files.deinit();
705705
706 var link_objects = std.ArrayList([]const u8).init(gpa);
706 var link_objects = std.ArrayList(Compilation.LinkObject).init(gpa);
707707 defer link_objects.deinit();
708708
709709 var framework_dirs = std.ArrayList([]const u8).init(gpa);
......@@ -1236,7 +1236,7 @@ fn buildOutputType(
12361236 }
12371237 } else switch (Compilation.classifyFileExt(arg)) {
12381238 .object, .static_library, .shared_library => {
1239 try link_objects.append(arg);
1239 try link_objects.append(.{ .path = arg });
12401240 },
12411241 .assembly, .c, .cpp, .h, .ll, .bc, .m, .mm => {
12421242 try c_source_files.append(.{
......@@ -1307,7 +1307,7 @@ fn buildOutputType(
13071307 switch (file_ext) {
13081308 .assembly, .c, .cpp, .ll, .bc, .h, .m, .mm => try c_source_files.append(.{ .src_path = it.only_arg }),
13091309 .unknown, .shared_library, .object, .static_library => {
1310 try link_objects.append(it.only_arg);
1310 try link_objects.append(.{ .path = it.only_arg });
13111311 },
13121312 .zig => {
13131313 if (root_src_file) |other| {
......@@ -1751,6 +1751,15 @@ fn buildOutputType(
17511751 fatal("expected linker arg after '{s}'", .{arg});
17521752 }
17531753 install_name = linker_args.items[i];
1754 } else if (mem.eql(u8, arg, "-force_load")) {
1755 i += 1;
1756 if (i >= linker_args.items.len) {
1757 fatal("expected linker arg after '{s}'", .{arg});
1758 }
1759 try link_objects.append(.{
1760 .path = linker_args.items[i],
1761 .must_link = true,
1762 });
17541763 } else {
17551764 warn("unsupported linker arg: {s}", .{arg});
17561765 }
......@@ -1845,7 +1854,7 @@ fn buildOutputType(
18451854 const basename = fs.path.basename(c_source_files.items[0].src_path);
18461855 break :blk basename[0 .. basename.len - fs.path.extension(basename).len];
18471856 } else if (link_objects.items.len >= 1) {
1848 const basename = fs.path.basename(link_objects.items[0]);
1857 const basename = fs.path.basename(link_objects.items[0].path);
18491858 break :blk basename[0 .. basename.len - fs.path.extension(basename).len];
18501859 } else if (emit_bin == .yes) {
18511860 const basename = fs.path.basename(emit_bin.yes);
......@@ -2048,7 +2057,7 @@ fn buildOutputType(
20482057 test_path.items, @errorName(e),
20492058 }),
20502059 };
2051 try link_objects.append(try arena.dupe(u8, test_path.items));
2060 try link_objects.append(.{ .path = try arena.dupe(u8, test_path.items) });
20522061 break;
20532062 } else {
20542063 var search_paths = std.ArrayList(u8).init(arena);