| author | |
| committer | |
| log | fb88cfdf6aa3fabba700d8340f025e4a3e0d3fb2 |
| tree | f613ac6f06fe2cb46c58cd55eb1eaaee09990e07 |
| parent | 9be8a9000faead40b1aec4877506ff10b066659c |
| parent | d31eb744cec1d991def2d6d42a14ded82af1dbbe |
| signature |
link/macho: implement logic for merging literals9 files changed, 1082 insertions(+), 107 deletions(-)
src/link/MachO.zig+122-14| ... | ... | @@ -539,6 +539,7 @@ pub fn flushModule(self: *MachO, arena: Allocator, prog_node: *std.Progress.Node |
| 539 | 539 | |
| 540 | 540 | try self.convertTentativeDefinitions(); |
| 541 | 541 | try self.createObjcSections(); |
| 542 | try self.dedupLiterals(); | |
| 542 | 543 | try self.claimUnresolved(); |
| 543 | 544 | |
| 544 | 545 | if (self.base.gc_sections) { |
| ... | ... | @@ -1491,6 +1492,33 @@ fn createObjcSections(self: *MachO) !void { |
| 1491 | 1492 | const name = eatPrefix(sym.getName(self), "_objc_msgSend$").?; |
| 1492 | 1493 | const selrefs_index = try internal.addObjcMsgsendSections(name, self); |
| 1493 | 1494 | try sym.addExtra(.{ .objc_selrefs = selrefs_index }, self); |
| 1495 | sym.flags.objc_stubs = true; | |
| 1496 | } | |
| 1497 | } | |
| 1498 | ||
| 1499 | pub fn dedupLiterals(self: *MachO) !void { | |
| 1500 | const gpa = self.base.comp.gpa; | |
| 1501 | var lp: LiteralPool = .{}; | |
| 1502 | defer lp.deinit(gpa); | |
| 1503 | ||
| 1504 | if (self.getZigObject()) |zo| { | |
| 1505 | try zo.resolveLiterals(&lp, self); | |
| 1506 | } | |
| 1507 | for (self.objects.items) |index| { | |
| 1508 | try self.getFile(index).?.object.resolveLiterals(&lp, self); | |
| 1509 | } | |
| 1510 | if (self.getInternalObject()) |object| { | |
| 1511 | try object.resolveLiterals(&lp, self); | |
| 1512 | } | |
| 1513 | ||
| 1514 | if (self.getZigObject()) |zo| { | |
| 1515 | zo.dedupLiterals(lp, self); | |
| 1516 | } | |
| 1517 | for (self.objects.items) |index| { | |
| 1518 | self.getFile(index).?.object.dedupLiterals(lp, self); | |
| 1519 | } | |
| 1520 | if (self.getInternalObject()) |object| { | |
| 1521 | object.dedupLiterals(lp, self); | |
| 1494 | 1522 | } |
| 1495 | 1523 | } |
| 1496 | 1524 | |
| ... | ... | @@ -1728,20 +1756,18 @@ fn initOutputSections(self: *MachO) !void { |
| 1728 | 1756 | atom.out_n_sect = try Atom.initOutputSection(atom.getInputSection(self), self); |
| 1729 | 1757 | } |
| 1730 | 1758 | } |
| 1731 | if (self.text_sect_index == null) { | |
| 1732 | self.text_sect_index = try self.addSection("__TEXT", "__text", .{ | |
| 1733 | .alignment = switch (self.getTarget().cpu.arch) { | |
| 1734 | .x86_64 => 0, | |
| 1735 | .aarch64 => 2, | |
| 1736 | else => unreachable, | |
| 1737 | }, | |
| 1738 | .flags = macho.S_REGULAR | | |
| 1739 | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS, | |
| 1740 | }); | |
| 1741 | } | |
| 1742 | if (self.data_sect_index == null) { | |
| 1743 | self.data_sect_index = try self.addSection("__DATA", "__data", .{}); | |
| 1744 | } | |
| 1759 | self.text_sect_index = self.getSectionByName("__TEXT", "__text") orelse | |
| 1760 | try self.addSection("__TEXT", "__text", .{ | |
| 1761 | .alignment = switch (self.getTarget().cpu.arch) { | |
| 1762 | .x86_64 => 0, | |
| 1763 | .aarch64 => 2, | |
| 1764 | else => unreachable, | |
| 1765 | }, | |
| 1766 | .flags = macho.S_REGULAR | | |
| 1767 | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS, | |
| 1768 | }); | |
| 1769 | self.data_sect_index = self.getSectionByName("__DATA", "__data") orelse | |
| 1770 | try self.addSection("__DATA", "__data", .{}); | |
| 1745 | 1771 | } |
| 1746 | 1772 | |
| 1747 | 1773 | fn initSyntheticSections(self: *MachO) !void { |
| ... | ... | @@ -4387,6 +4413,87 @@ const Section = struct { |
| 4387 | 4413 | last_atom_index: Atom.Index = 0, |
| 4388 | 4414 | }; |
| 4389 | 4415 | |
| 4416 | pub const LiteralPool = struct { | |
| 4417 | table: std.AutoArrayHashMapUnmanaged(void, void) = .{}, | |
| 4418 | keys: std.ArrayListUnmanaged(Key) = .{}, | |
| 4419 | values: std.ArrayListUnmanaged(Atom.Index) = .{}, | |
| 4420 | data: std.ArrayListUnmanaged(u8) = .{}, | |
| 4421 | ||
| 4422 | pub fn deinit(lp: *LiteralPool, allocator: Allocator) void { | |
| 4423 | lp.table.deinit(allocator); | |
| 4424 | lp.keys.deinit(allocator); | |
| 4425 | lp.values.deinit(allocator); | |
| 4426 | lp.data.deinit(allocator); | |
| 4427 | } | |
| 4428 | ||
| 4429 | pub fn getAtom(lp: LiteralPool, index: Index, macho_file: *MachO) *Atom { | |
| 4430 | assert(index < lp.values.items.len); | |
| 4431 | return macho_file.getAtom(lp.values.items[index]).?; | |
| 4432 | } | |
| 4433 | ||
| 4434 | const InsertResult = struct { | |
| 4435 | found_existing: bool, | |
| 4436 | index: Index, | |
| 4437 | atom: *Atom.Index, | |
| 4438 | }; | |
| 4439 | ||
| 4440 | pub fn insert(lp: *LiteralPool, allocator: Allocator, @"type": u8, string: []const u8) !InsertResult { | |
| 4441 | const size: u32 = @intCast(string.len); | |
| 4442 | try lp.data.ensureUnusedCapacity(allocator, size); | |
| 4443 | const off: u32 = @intCast(lp.data.items.len); | |
| 4444 | lp.data.appendSliceAssumeCapacity(string); | |
| 4445 | const adapter = Adapter{ .lp = lp }; | |
| 4446 | const key = Key{ .off = off, .size = size, .seed = @"type" }; | |
| 4447 | const gop = try lp.table.getOrPutAdapted(allocator, key, adapter); | |
| 4448 | if (!gop.found_existing) { | |
| 4449 | try lp.keys.append(allocator, key); | |
| 4450 | _ = try lp.values.addOne(allocator); | |
| 4451 | } | |
| 4452 | return .{ | |
| 4453 | .found_existing = gop.found_existing, | |
| 4454 | .index = @intCast(gop.index), | |
| 4455 | .atom = &lp.values.items[gop.index], | |
| 4456 | }; | |
| 4457 | } | |
| 4458 | ||
| 4459 | const Key = struct { | |
| 4460 | off: u32, | |
| 4461 | size: u32, | |
| 4462 | seed: u8, | |
| 4463 | ||
| 4464 | fn getData(key: Key, lp: *const LiteralPool) []const u8 { | |
| 4465 | return lp.data.items[key.off..][0..key.size]; | |
| 4466 | } | |
| 4467 | ||
| 4468 | fn eql(key: Key, other: Key, lp: *const LiteralPool) bool { | |
| 4469 | const key_data = key.getData(lp); | |
| 4470 | const other_data = other.getData(lp); | |
| 4471 | return mem.eql(u8, key_data, other_data); | |
| 4472 | } | |
| 4473 | ||
| 4474 | fn hash(key: Key, lp: *const LiteralPool) u32 { | |
| 4475 | const data = key.getData(lp); | |
| 4476 | return @truncate(Hash.hash(key.seed, data)); | |
| 4477 | } | |
| 4478 | }; | |
| 4479 | ||
| 4480 | const Adapter = struct { | |
| 4481 | lp: *const LiteralPool, | |
| 4482 | ||
| 4483 | pub fn eql(ctx: @This(), key: Key, b_void: void, b_map_index: usize) bool { | |
| 4484 | _ = b_void; | |
| 4485 | const other = ctx.lp.keys.items[b_map_index]; | |
| 4486 | return key.eql(other, ctx.lp); | |
| 4487 | } | |
| 4488 | ||
| 4489 | pub fn hash(ctx: @This(), key: Key) u32 { | |
| 4490 | return key.hash(ctx.lp); | |
| 4491 | } | |
| 4492 | }; | |
| 4493 | ||
| 4494 | pub const Index = u32; | |
| 4495 | }; | |
| 4496 | ||
| 4390 | 4497 | const HotUpdateState = struct { |
| 4391 | 4498 | mach_task: ?std.c.MachTask = null, |
| 4392 | 4499 | }; |
| ... | ... | @@ -4738,6 +4845,7 @@ const Dylib = @import("MachO/Dylib.zig"); |
| 4738 | 4845 | const ExportTrieSection = synthetic.ExportTrieSection; |
| 4739 | 4846 | const File = @import("MachO/file.zig").File; |
| 4740 | 4847 | const GotSection = synthetic.GotSection; |
| 4848 | const Hash = std.hash.Wyhash; | |
| 4741 | 4849 | const Indsymtab = synthetic.Indsymtab; |
| 4742 | 4850 | const InternalObject = @import("MachO/InternalObject.zig"); |
| 4743 | 4851 | const ObjcStubsSection = synthetic.ObjcStubsSection; |
src/link/MachO/Atom.zig+43-15| ... | ... | @@ -113,12 +113,18 @@ pub fn getThunk(self: Atom, macho_file: *MachO) *Thunk { |
| 113 | 113 | return macho_file.getThunk(extra.thunk); |
| 114 | 114 | } |
| 115 | 115 | |
| 116 | pub fn getLiteralPoolIndex(self: Atom, macho_file: *MachO) ?MachO.LiteralPool.Index { | |
| 117 | if (!self.flags.literal_pool) return null; | |
| 118 | return self.getExtra(macho_file).?.literal_index; | |
| 119 | } | |
| 120 | ||
| 116 | 121 | const AddExtraOpts = struct { |
| 117 | 122 | thunk: ?u32 = null, |
| 118 | 123 | rel_index: ?u32 = null, |
| 119 | 124 | rel_count: ?u32 = null, |
| 120 | 125 | unwind_index: ?u32 = null, |
| 121 | 126 | unwind_count: ?u32 = null, |
| 127 | literal_index: ?u32 = null, | |
| 122 | 128 | }; |
| 123 | 129 | |
| 124 | 130 | pub fn addExtra(atom: *Atom, opts: AddExtraOpts, macho_file: *MachO) !void { |
| ... | ... | @@ -143,6 +149,16 @@ pub inline fn setExtra(atom: Atom, extra: Extra, macho_file: *MachO) void { |
| 143 | 149 | } |
| 144 | 150 | |
| 145 | 151 | pub fn initOutputSection(sect: macho.section_64, macho_file: *MachO) !u8 { |
| 152 | if (macho_file.base.isRelocatable()) { | |
| 153 | const osec = macho_file.getSectionByName(sect.segName(), sect.sectName()) orelse | |
| 154 | try macho_file.addSection( | |
| 155 | sect.segName(), | |
| 156 | sect.sectName(), | |
| 157 | .{ .flags = sect.flags }, | |
| 158 | ); | |
| 159 | return osec; | |
| 160 | } | |
| 161 | ||
| 146 | 162 | const segname, const sectname, const flags = blk: { |
| 147 | 163 | if (sect.isCode()) break :blk .{ |
| 148 | 164 | "__TEXT", |
| ... | ... | @@ -200,18 +216,11 @@ pub fn initOutputSection(sect: macho.section_64, macho_file: *MachO) !u8 { |
| 200 | 216 | else => break :blk .{ sect.segName(), sect.sectName(), sect.flags }, |
| 201 | 217 | } |
| 202 | 218 | }; |
| 203 | const osec = macho_file.getSectionByName(segname, sectname) orelse try macho_file.addSection( | |
| 219 | return macho_file.getSectionByName(segname, sectname) orelse try macho_file.addSection( | |
| 204 | 220 | segname, |
| 205 | 221 | sectname, |
| 206 | 222 | .{ .flags = flags }, |
| 207 | 223 | ); |
| 208 | if (mem.eql(u8, segname, "__TEXT") and mem.eql(u8, sectname, "__text")) { | |
| 209 | macho_file.text_sect_index = osec; | |
| 210 | } | |
| 211 | if (mem.eql(u8, segname, "__DATA") and mem.eql(u8, sectname, "__data")) { | |
| 212 | macho_file.data_sect_index = osec; | |
| 213 | } | |
| 214 | return osec; | |
| 215 | 224 | } |
| 216 | 225 | |
| 217 | 226 | /// Returns how much room there is to grow in virtual address space. |
| ... | ... | @@ -651,6 +660,19 @@ fn resolveRelocInner( |
| 651 | 660 | // Address of the __got_zig table entry if any. |
| 652 | 661 | const ZIG_GOT = @as(i64, @intCast(rel.getZigGotTargetAddress(macho_file))); |
| 653 | 662 | |
| 663 | const divExact = struct { | |
| 664 | fn divExact(atom: Atom, r: Relocation, num: u12, den: u12, ctx: *MachO) !u12 { | |
| 665 | return math.divExact(u12, num, den) catch { | |
| 666 | try ctx.reportParseError2(atom.getFile(ctx).getIndex(), "{s}: unexpected remainder when resolving {s} at offset 0x{x}", .{ | |
| 667 | atom.getName(ctx), | |
| 668 | r.fmtPretty(ctx.getTarget().cpu.arch), | |
| 669 | r.offset, | |
| 670 | }); | |
| 671 | return error.UnexpectedRemainder; | |
| 672 | }; | |
| 673 | } | |
| 674 | }.divExact; | |
| 675 | ||
| 654 | 676 | switch (rel.tag) { |
| 655 | 677 | .local => relocs_log.debug(" {x}<+{d}>: {s}: [=> {x}] atom({d})", .{ |
| 656 | 678 | P, |
| ... | ... | @@ -822,12 +844,12 @@ fn resolveRelocInner( |
| 822 | 844 | }; |
| 823 | 845 | inst.load_store_register.offset = switch (inst.load_store_register.size) { |
| 824 | 846 | 0 => if (inst.load_store_register.v == 1) |
| 825 | try math.divExact(u12, @truncate(target), 16) | |
| 847 | try divExact(self, rel, @truncate(target), 16, macho_file) | |
| 826 | 848 | else |
| 827 | 849 | @truncate(target), |
| 828 | 1 => try math.divExact(u12, @truncate(target), 2), | |
| 829 | 2 => try math.divExact(u12, @truncate(target), 4), | |
| 830 | 3 => try math.divExact(u12, @truncate(target), 8), | |
| 850 | 1 => try divExact(self, rel, @truncate(target), 2, macho_file), | |
| 851 | 2 => try divExact(self, rel, @truncate(target), 4, macho_file), | |
| 852 | 3 => try divExact(self, rel, @truncate(target), 8, macho_file), | |
| 831 | 853 | }; |
| 832 | 854 | try writer.writeInt(u32, inst.toU32(), .little); |
| 833 | 855 | } |
| ... | ... | @@ -838,7 +860,7 @@ fn resolveRelocInner( |
| 838 | 860 | assert(rel.meta.length == 2); |
| 839 | 861 | assert(!rel.meta.pcrel); |
| 840 | 862 | const target = math.cast(u64, G + A) orelse return error.Overflow; |
| 841 | aarch64.writeLoadStoreRegInst(try math.divExact(u12, @truncate(target), 8), code[rel_offset..][0..4]); | |
| 863 | aarch64.writeLoadStoreRegInst(try divExact(self, rel, @truncate(target), 8, macho_file), code[rel_offset..][0..4]); | |
| 842 | 864 | }, |
| 843 | 865 | |
| 844 | 866 | .tlvp_pageoff => { |
| ... | ... | @@ -890,7 +912,7 @@ fn resolveRelocInner( |
| 890 | 912 | .load_store_register = .{ |
| 891 | 913 | .rt = reg_info.rd, |
| 892 | 914 | .rn = reg_info.rn, |
| 893 | .offset = try math.divExact(u12, @truncate(target), 8), | |
| 915 | .offset = try divExact(self, rel, @truncate(target), 8, macho_file), | |
| 894 | 916 | .opc = 0b01, |
| 895 | 917 | .op1 = 0b01, |
| 896 | 918 | .v = 0, |
| ... | ... | @@ -1174,7 +1196,7 @@ pub const Flags = packed struct { |
| 1174 | 1196 | /// Specifies whether this atom is alive or has been garbage collected. |
| 1175 | 1197 | alive: bool = true, |
| 1176 | 1198 | |
| 1177 | /// Specifies if the atom has been visited during garbage collection. | |
| 1199 | /// Specifies if this atom has been visited during garbage collection. | |
| 1178 | 1200 | visited: bool = false, |
| 1179 | 1201 | |
| 1180 | 1202 | /// Whether this atom has a range extension thunk. |
| ... | ... | @@ -1185,6 +1207,9 @@ pub const Flags = packed struct { |
| 1185 | 1207 | |
| 1186 | 1208 | /// Whether this atom has any unwind records. |
| 1187 | 1209 | unwind: bool = false, |
| 1210 | ||
| 1211 | /// Whether this atom has LiteralPool entry. | |
| 1212 | literal_pool: bool = false, | |
| 1188 | 1213 | }; |
| 1189 | 1214 | |
| 1190 | 1215 | pub const Extra = struct { |
| ... | ... | @@ -1202,6 +1227,9 @@ pub const Extra = struct { |
| 1202 | 1227 | |
| 1203 | 1228 | /// Count of relocations belonging to this atom. |
| 1204 | 1229 | unwind_count: u32 = 0, |
| 1230 | ||
| 1231 | /// Index into LiteralPool entry for this atom. | |
| 1232 | literal_index: u32 = 0, | |
| 1205 | 1233 | }; |
| 1206 | 1234 | |
| 1207 | 1235 | pub const Alignment = @import("../../InternPool.zig").Alignment; |
src/link/MachO/InternalObject.zig+115-33| ... | ... | @@ -3,7 +3,6 @@ index: File.Index, |
| 3 | 3 | sections: std.MultiArrayList(Section) = .{}, |
| 4 | 4 | atoms: std.ArrayListUnmanaged(Atom.Index) = .{}, |
| 5 | 5 | symbols: std.ArrayListUnmanaged(Symbol.Index) = .{}, |
| 6 | strtab: std.ArrayListUnmanaged(u8) = .{}, | |
| 7 | 6 | |
| 8 | 7 | objc_methnames: std.ArrayListUnmanaged(u8) = .{}, |
| 9 | 8 | objc_selrefs: [@sizeOf(u64)]u8 = [_]u8{0} ** @sizeOf(u64), |
| ... | ... | @@ -18,7 +17,6 @@ pub fn deinit(self: *InternalObject, allocator: Allocator) void { |
| 18 | 17 | self.sections.deinit(allocator); |
| 19 | 18 | self.atoms.deinit(allocator); |
| 20 | 19 | self.symbols.deinit(allocator); |
| 21 | self.strtab.deinit(allocator); | |
| 22 | 20 | self.objc_methnames.deinit(allocator); |
| 23 | 21 | } |
| 24 | 22 | |
| ... | ... | @@ -38,9 +36,9 @@ pub fn addSymbol(self: *InternalObject, name: [:0]const u8, macho_file: *MachO) |
| 38 | 36 | } |
| 39 | 37 | |
| 40 | 38 | /// Creates a fake input sections __TEXT,__objc_methname and __DATA,__objc_selrefs. |
| 41 | pub fn addObjcMsgsendSections(self: *InternalObject, sym_name: []const u8, macho_file: *MachO) !u32 { | |
| 39 | pub fn addObjcMsgsendSections(self: *InternalObject, sym_name: []const u8, macho_file: *MachO) !Atom.Index { | |
| 42 | 40 | const methname_atom_index = try self.addObjcMethnameSection(sym_name, macho_file); |
| 43 | return try self.addObjcSelrefsSection(sym_name, methname_atom_index, macho_file); | |
| 41 | return try self.addObjcSelrefsSection(methname_atom_index, macho_file); | |
| 44 | 42 | } |
| 45 | 43 | |
| 46 | 44 | fn addObjcMethnameSection(self: *InternalObject, methname: []const u8, macho_file: *MachO) !Atom.Index { |
| ... | ... | @@ -48,11 +46,8 @@ fn addObjcMethnameSection(self: *InternalObject, methname: []const u8, macho_fil |
| 48 | 46 | const atom_index = try macho_file.addAtom(); |
| 49 | 47 | try self.atoms.append(gpa, atom_index); |
| 50 | 48 | |
| 51 | const name = try std.fmt.allocPrintZ(gpa, "__TEXT$__objc_methname${s}", .{methname}); | |
| 52 | defer gpa.free(name); | |
| 53 | 49 | const atom = macho_file.getAtom(atom_index).?; |
| 54 | 50 | atom.atom_index = atom_index; |
| 55 | atom.name = try self.addString(gpa, name); | |
| 56 | 51 | atom.file = self.index; |
| 57 | 52 | atom.size = methname.len + 1; |
| 58 | 53 | atom.alignment = .@"1"; |
| ... | ... | @@ -72,21 +67,13 @@ fn addObjcMethnameSection(self: *InternalObject, methname: []const u8, macho_fil |
| 72 | 67 | return atom_index; |
| 73 | 68 | } |
| 74 | 69 | |
| 75 | fn addObjcSelrefsSection( | |
| 76 | self: *InternalObject, | |
| 77 | methname: []const u8, | |
| 78 | methname_atom_index: Atom.Index, | |
| 79 | macho_file: *MachO, | |
| 80 | ) !Atom.Index { | |
| 70 | fn addObjcSelrefsSection(self: *InternalObject, methname_atom_index: Atom.Index, macho_file: *MachO) !Atom.Index { | |
| 81 | 71 | const gpa = macho_file.base.comp.gpa; |
| 82 | 72 | const atom_index = try macho_file.addAtom(); |
| 83 | 73 | try self.atoms.append(gpa, atom_index); |
| 84 | 74 | |
| 85 | const name = try std.fmt.allocPrintZ(gpa, "__DATA$__objc_selrefs${s}", .{methname}); | |
| 86 | defer gpa.free(name); | |
| 87 | 75 | const atom = macho_file.getAtom(atom_index).?; |
| 88 | 76 | atom.atom_index = atom_index; |
| 89 | atom.name = try self.addString(gpa, name); | |
| 90 | 77 | atom.file = self.index; |
| 91 | 78 | atom.size = @sizeOf(u64); |
| 92 | 79 | atom.alignment = .@"8"; |
| ... | ... | @@ -122,6 +109,102 @@ fn addObjcSelrefsSection( |
| 122 | 109 | return atom_index; |
| 123 | 110 | } |
| 124 | 111 | |
| 112 | pub fn resolveLiterals(self: InternalObject, lp: *MachO.LiteralPool, macho_file: *MachO) !void { | |
| 113 | const gpa = macho_file.base.comp.gpa; | |
| 114 | ||
| 115 | var buffer = std.ArrayList(u8).init(gpa); | |
| 116 | defer buffer.deinit(); | |
| 117 | ||
| 118 | const slice = self.sections.slice(); | |
| 119 | for (slice.items(.header), self.atoms.items, 0..) |header, atom_index, n_sect| { | |
| 120 | if (Object.isCstringLiteral(header) or Object.isFixedSizeLiteral(header)) { | |
| 121 | const data = try self.getSectionData(@intCast(n_sect)); | |
| 122 | const atom = macho_file.getAtom(atom_index).?; | |
| 123 | const res = try lp.insert(gpa, header.type(), data); | |
| 124 | if (!res.found_existing) { | |
| 125 | res.atom.* = atom_index; | |
| 126 | } | |
| 127 | atom.flags.literal_pool = true; | |
| 128 | try atom.addExtra(.{ .literal_index = res.index }, macho_file); | |
| 129 | } else if (Object.isPtrLiteral(header)) { | |
| 130 | const atom = macho_file.getAtom(atom_index).?; | |
| 131 | const relocs = atom.getRelocs(macho_file); | |
| 132 | assert(relocs.len == 1); | |
| 133 | const rel = relocs[0]; | |
| 134 | assert(rel.tag == .local); | |
| 135 | const target = macho_file.getAtom(rel.target).?; | |
| 136 | const addend = std.math.cast(u32, rel.addend) orelse return error.Overflow; | |
| 137 | const target_size = std.math.cast(usize, target.size) orelse return error.Overflow; | |
| 138 | try buffer.ensureUnusedCapacity(target_size); | |
| 139 | buffer.resize(target_size) catch unreachable; | |
| 140 | try target.getData(macho_file, buffer.items); | |
| 141 | const res = try lp.insert(gpa, header.type(), buffer.items[addend..]); | |
| 142 | buffer.clearRetainingCapacity(); | |
| 143 | if (!res.found_existing) { | |
| 144 | res.atom.* = atom_index; | |
| 145 | } | |
| 146 | atom.flags.literal_pool = true; | |
| 147 | try atom.addExtra(.{ .literal_index = res.index }, macho_file); | |
| 148 | } | |
| 149 | } | |
| 150 | } | |
| 151 | ||
| 152 | pub fn dedupLiterals(self: InternalObject, lp: MachO.LiteralPool, macho_file: *MachO) void { | |
| 153 | for (self.atoms.items) |atom_index| { | |
| 154 | const atom = macho_file.getAtom(atom_index) orelse continue; | |
| 155 | if (!atom.flags.alive) continue; | |
| 156 | if (!atom.flags.relocs) continue; | |
| 157 | ||
| 158 | const relocs = blk: { | |
| 159 | const extra = atom.getExtra(macho_file).?; | |
| 160 | const relocs = self.sections.items(.relocs)[atom.n_sect].items; | |
| 161 | break :blk relocs[extra.rel_index..][0..extra.rel_count]; | |
| 162 | }; | |
| 163 | for (relocs) |*rel| switch (rel.tag) { | |
| 164 | .local => { | |
| 165 | const target = macho_file.getAtom(rel.target).?; | |
| 166 | if (target.getLiteralPoolIndex(macho_file)) |lp_index| { | |
| 167 | const lp_atom = lp.getAtom(lp_index, macho_file); | |
| 168 | if (target.atom_index != lp_atom.atom_index) { | |
| 169 | lp_atom.alignment = lp_atom.alignment.max(target.alignment); | |
| 170 | target.flags.alive = false; | |
| 171 | rel.target = lp_atom.atom_index; | |
| 172 | } | |
| 173 | } | |
| 174 | }, | |
| 175 | .@"extern" => { | |
| 176 | const target_sym = rel.getTargetSymbol(macho_file); | |
| 177 | if (target_sym.getAtom(macho_file)) |target_atom| { | |
| 178 | if (target_atom.getLiteralPoolIndex(macho_file)) |lp_index| { | |
| 179 | const lp_atom = lp.getAtom(lp_index, macho_file); | |
| 180 | if (target_atom.atom_index != lp_atom.atom_index) { | |
| 181 | lp_atom.alignment = lp_atom.alignment.max(target_atom.alignment); | |
| 182 | target_atom.flags.alive = false; | |
| 183 | target_sym.atom = lp_atom.atom_index; | |
| 184 | } | |
| 185 | } | |
| 186 | } | |
| 187 | }, | |
| 188 | }; | |
| 189 | } | |
| 190 | ||
| 191 | for (self.symbols.items) |sym_index| { | |
| 192 | const sym = macho_file.getSymbol(sym_index); | |
| 193 | if (!sym.flags.objc_stubs) continue; | |
| 194 | var extra = sym.getExtra(macho_file).?; | |
| 195 | const atom = macho_file.getAtom(extra.objc_selrefs).?; | |
| 196 | if (atom.getLiteralPoolIndex(macho_file)) |lp_index| { | |
| 197 | const lp_atom = lp.getAtom(lp_index, macho_file); | |
| 198 | if (atom.atom_index != lp_atom.atom_index) { | |
| 199 | lp_atom.alignment = lp_atom.alignment.max(atom.alignment); | |
| 200 | atom.flags.alive = false; | |
| 201 | extra.objc_selrefs = lp_atom.atom_index; | |
| 202 | sym.setExtra(extra, macho_file); | |
| 203 | } | |
| 204 | } | |
| 205 | } | |
| 206 | } | |
| 207 | ||
| 125 | 208 | pub fn calcSymtabSize(self: *InternalObject, macho_file: *MachO) !void { |
| 126 | 209 | for (self.symbols.items) |sym_index| { |
| 127 | 210 | const sym = macho_file.getSymbol(sym_index); |
| ... | ... | @@ -167,18 +250,23 @@ fn addSection(self: *InternalObject, allocator: Allocator, segname: []const u8, |
| 167 | 250 | return n_sect; |
| 168 | 251 | } |
| 169 | 252 | |
| 170 | pub fn getAtomData(self: *const InternalObject, atom: Atom, buffer: []u8) !void { | |
| 171 | assert(buffer.len == atom.size); | |
| 253 | fn getSectionData(self: *const InternalObject, index: u32) error{Overflow}![]const u8 { | |
| 172 | 254 | const slice = self.sections.slice(); |
| 173 | const sect = slice.items(.header)[atom.n_sect]; | |
| 174 | const extra = slice.items(.extra)[atom.n_sect]; | |
| 175 | const data = if (extra.is_objc_methname) blk: { | |
| 255 | assert(index < slice.items(.header).len); | |
| 256 | const sect = slice.items(.header)[index]; | |
| 257 | const extra = slice.items(.extra)[index]; | |
| 258 | if (extra.is_objc_methname) { | |
| 176 | 259 | const size = std.math.cast(usize, sect.size) orelse return error.Overflow; |
| 177 | break :blk self.objc_methnames.items[sect.offset..][0..size]; | |
| 260 | return self.objc_methnames.items[sect.offset..][0..size]; | |
| 178 | 261 | } else if (extra.is_objc_selref) |
| 179 | &self.objc_selrefs | |
| 262 | return &self.objc_selrefs | |
| 180 | 263 | else |
| 181 | 264 | @panic("ref to non-existent section"); |
| 265 | } | |
| 266 | ||
| 267 | pub fn getAtomData(self: *const InternalObject, atom: Atom, buffer: []u8) error{Overflow}!void { | |
| 268 | assert(buffer.len == atom.size); | |
| 269 | const data = try self.getSectionData(atom.n_sect); | |
| 182 | 270 | const off = std.math.cast(usize, atom.off) orelse return error.Overflow; |
| 183 | 271 | const size = std.math.cast(usize, atom.size) orelse return error.Overflow; |
| 184 | 272 | @memcpy(buffer, data[off..][0..size]); |
| ... | ... | @@ -191,17 +279,11 @@ pub fn getAtomRelocs(self: *const InternalObject, atom: Atom, macho_file: *MachO |
| 191 | 279 | return relocs.items[extra.rel_index..][0..extra.rel_count]; |
| 192 | 280 | } |
| 193 | 281 | |
| 194 | fn addString(self: *InternalObject, allocator: Allocator, name: [:0]const u8) error{OutOfMemory}!u32 { | |
| 195 | const off: u32 = @intCast(self.strtab.items.len); | |
| 196 | try self.strtab.ensureUnusedCapacity(allocator, name.len + 1); | |
| 197 | self.strtab.appendSliceAssumeCapacity(name); | |
| 198 | self.strtab.appendAssumeCapacity(0); | |
| 199 | return off; | |
| 200 | } | |
| 201 | ||
| 202 | 282 | pub fn getString(self: InternalObject, off: u32) [:0]const u8 { |
| 203 | assert(off < self.strtab.items.len); | |
| 204 | return mem.sliceTo(@as([*:0]const u8, @ptrCast(self.strtab.items.ptr + off)), 0); | |
| 283 | _ = self; | |
| 284 | _ = off; | |
| 285 | // We don't have any local strings for synthetic atoms. | |
| 286 | return ""; | |
| 205 | 287 | } |
| 206 | 288 | |
| 207 | 289 | pub fn asFile(self: *InternalObject) File { |
src/link/MachO/Object.zig+236-35| ... | ... | @@ -208,7 +208,9 @@ pub fn parse(self: *Object, macho_file: *MachO) !void { |
| 208 | 208 | try self.initSections(nlists.items, macho_file); |
| 209 | 209 | } |
| 210 | 210 | |
| 211 | try self.initLiteralSections(macho_file); | |
| 211 | try self.initCstringLiterals(macho_file); | |
| 212 | try self.initFixedSizeLiterals(macho_file); | |
| 213 | try self.initPointerLiterals(macho_file); | |
| 212 | 214 | try self.linkNlistToAtom(macho_file); |
| 213 | 215 | |
| 214 | 216 | try self.sortAtoms(macho_file); |
| ... | ... | @@ -263,25 +265,33 @@ pub fn parse(self: *Object, macho_file: *MachO) !void { |
| 263 | 265 | } |
| 264 | 266 | } |
| 265 | 267 | |
| 266 | inline fn isLiteral(sect: macho.section_64) bool { | |
| 268 | pub fn isCstringLiteral(sect: macho.section_64) bool { | |
| 269 | return sect.type() == macho.S_CSTRING_LITERALS; | |
| 270 | } | |
| 271 | ||
| 272 | pub fn isFixedSizeLiteral(sect: macho.section_64) bool { | |
| 267 | 273 | return switch (sect.type()) { |
| 268 | macho.S_CSTRING_LITERALS, | |
| 269 | 274 | macho.S_4BYTE_LITERALS, |
| 270 | 275 | macho.S_8BYTE_LITERALS, |
| 271 | 276 | macho.S_16BYTE_LITERALS, |
| 272 | macho.S_LITERAL_POINTERS, | |
| 273 | 277 | => true, |
| 274 | 278 | else => false, |
| 275 | 279 | }; |
| 276 | 280 | } |
| 277 | 281 | |
| 282 | pub fn isPtrLiteral(sect: macho.section_64) bool { | |
| 283 | return sect.type() == macho.S_LITERAL_POINTERS; | |
| 284 | } | |
| 285 | ||
| 278 | 286 | fn initSubsections(self: *Object, nlists: anytype, macho_file: *MachO) !void { |
| 279 | 287 | const tracy = trace(@src()); |
| 280 | 288 | defer tracy.end(); |
| 281 | 289 | const gpa = macho_file.base.comp.gpa; |
| 282 | 290 | const slice = self.sections.slice(); |
| 283 | 291 | for (slice.items(.header), slice.items(.subsections), 0..) |sect, *subsections, n_sect| { |
| 284 | if (isLiteral(sect)) continue; | |
| 292 | if (isCstringLiteral(sect)) continue; | |
| 293 | if (isFixedSizeLiteral(sect)) continue; | |
| 294 | if (isPtrLiteral(sect)) continue; | |
| 285 | 295 | |
| 286 | 296 | const nlist_start = for (nlists, 0..) |nlist, i| { |
| 287 | 297 | if (nlist.nlist.n_sect - 1 == n_sect) break i; |
| ... | ... | @@ -352,7 +362,9 @@ fn initSections(self: *Object, nlists: anytype, macho_file: *MachO) !void { |
| 352 | 362 | try self.atoms.ensureUnusedCapacity(gpa, self.sections.items(.header).len); |
| 353 | 363 | |
| 354 | 364 | for (slice.items(.header), 0..) |sect, n_sect| { |
| 355 | if (isLiteral(sect)) continue; | |
| 365 | if (isCstringLiteral(sect)) continue; | |
| 366 | if (isFixedSizeLiteral(sect)) continue; | |
| 367 | if (isPtrLiteral(sect)) continue; | |
| 356 | 368 | |
| 357 | 369 | const name = try std.fmt.allocPrintZ(gpa, "{s}${s}", .{ sect.segName(), sect.sectName() }); |
| 358 | 370 | defer gpa.free(name); |
| ... | ... | @@ -393,6 +405,220 @@ fn initSections(self: *Object, nlists: anytype, macho_file: *MachO) !void { |
| 393 | 405 | } |
| 394 | 406 | } |
| 395 | 407 | |
| 408 | fn initCstringLiterals(self: *Object, macho_file: *MachO) !void { | |
| 409 | const tracy = trace(@src()); | |
| 410 | defer tracy.end(); | |
| 411 | ||
| 412 | const gpa = macho_file.base.comp.gpa; | |
| 413 | const slice = self.sections.slice(); | |
| 414 | ||
| 415 | for (slice.items(.header), 0..) |sect, n_sect| { | |
| 416 | if (!isCstringLiteral(sect)) continue; | |
| 417 | ||
| 418 | const data = try self.getSectionData(@intCast(n_sect), macho_file); | |
| 419 | defer gpa.free(data); | |
| 420 | ||
| 421 | var start: u32 = 0; | |
| 422 | while (start < data.len) { | |
| 423 | var end = start; | |
| 424 | while (end < data.len - 1 and data[end] != 0) : (end += 1) {} | |
| 425 | if (data[end] != 0) { | |
| 426 | try macho_file.reportParseError2( | |
| 427 | self.index, | |
| 428 | "string not null terminated in '{s},{s}'", | |
| 429 | .{ sect.segName(), sect.sectName() }, | |
| 430 | ); | |
| 431 | return error.MalformedObject; | |
| 432 | } | |
| 433 | end += 1; | |
| 434 | ||
| 435 | const atom_index = try self.addAtom(.{ | |
| 436 | .name = 0, | |
| 437 | .n_sect = @intCast(n_sect), | |
| 438 | .off = start, | |
| 439 | .size = end - start, | |
| 440 | .alignment = sect.@"align", | |
| 441 | }, macho_file); | |
| 442 | try slice.items(.subsections)[n_sect].append(gpa, .{ | |
| 443 | .atom = atom_index, | |
| 444 | .off = start, | |
| 445 | }); | |
| 446 | ||
| 447 | start = end; | |
| 448 | } | |
| 449 | } | |
| 450 | } | |
| 451 | ||
| 452 | fn initFixedSizeLiterals(self: *Object, macho_file: *MachO) !void { | |
| 453 | const tracy = trace(@src()); | |
| 454 | defer tracy.end(); | |
| 455 | ||
| 456 | const gpa = macho_file.base.comp.gpa; | |
| 457 | const slice = self.sections.slice(); | |
| 458 | ||
| 459 | for (slice.items(.header), 0..) |sect, n_sect| { | |
| 460 | if (!isFixedSizeLiteral(sect)) continue; | |
| 461 | const rec_size: u8 = switch (sect.type()) { | |
| 462 | macho.S_4BYTE_LITERALS => 4, | |
| 463 | macho.S_8BYTE_LITERALS => 8, | |
| 464 | macho.S_16BYTE_LITERALS => 16, | |
| 465 | else => unreachable, | |
| 466 | }; | |
| 467 | if (sect.size % rec_size != 0) { | |
| 468 | try macho_file.reportParseError2( | |
| 469 | self.index, | |
| 470 | "size not multiple of record size in '{s},{s}'", | |
| 471 | .{ sect.segName(), sect.sectName() }, | |
| 472 | ); | |
| 473 | return error.MalformedObject; | |
| 474 | } | |
| 475 | var pos: u32 = 0; | |
| 476 | while (pos < sect.size) : (pos += rec_size) { | |
| 477 | const atom_index = try self.addAtom(.{ | |
| 478 | .name = 0, | |
| 479 | .n_sect = @intCast(n_sect), | |
| 480 | .off = pos, | |
| 481 | .size = rec_size, | |
| 482 | .alignment = sect.@"align", | |
| 483 | }, macho_file); | |
| 484 | try slice.items(.subsections)[n_sect].append(gpa, .{ | |
| 485 | .atom = atom_index, | |
| 486 | .off = pos, | |
| 487 | }); | |
| 488 | } | |
| 489 | } | |
| 490 | } | |
| 491 | ||
| 492 | fn initPointerLiterals(self: *Object, macho_file: *MachO) !void { | |
| 493 | const tracy = trace(@src()); | |
| 494 | defer tracy.end(); | |
| 495 | ||
| 496 | const gpa = macho_file.base.comp.gpa; | |
| 497 | const slice = self.sections.slice(); | |
| 498 | ||
| 499 | for (slice.items(.header), 0..) |sect, n_sect| { | |
| 500 | if (!isPtrLiteral(sect)) continue; | |
| 501 | ||
| 502 | const rec_size: u8 = 8; | |
| 503 | if (sect.size % rec_size != 0) { | |
| 504 | try macho_file.reportParseError2( | |
| 505 | self.index, | |
| 506 | "size not multiple of record size in '{s},{s}'", | |
| 507 | .{ sect.segName(), sect.sectName() }, | |
| 508 | ); | |
| 509 | return error.MalformedObject; | |
| 510 | } | |
| 511 | const num_ptrs = math.cast(usize, @divExact(sect.size, rec_size)) orelse return error.Overflow; | |
| 512 | ||
| 513 | for (0..num_ptrs) |i| { | |
| 514 | const pos: u32 = @as(u32, @intCast(i)) * rec_size; | |
| 515 | const atom_index = try self.addAtom(.{ | |
| 516 | .name = 0, | |
| 517 | .n_sect = @intCast(n_sect), | |
| 518 | .off = pos, | |
| 519 | .size = rec_size, | |
| 520 | .alignment = sect.@"align", | |
| 521 | }, macho_file); | |
| 522 | try slice.items(.subsections)[n_sect].append(gpa, .{ | |
| 523 | .atom = atom_index, | |
| 524 | .off = pos, | |
| 525 | }); | |
| 526 | } | |
| 527 | } | |
| 528 | } | |
| 529 | ||
| 530 | pub fn resolveLiterals(self: Object, lp: *MachO.LiteralPool, macho_file: *MachO) !void { | |
| 531 | const gpa = macho_file.base.comp.gpa; | |
| 532 | ||
| 533 | var buffer = std.ArrayList(u8).init(gpa); | |
| 534 | defer buffer.deinit(); | |
| 535 | ||
| 536 | const slice = self.sections.slice(); | |
| 537 | for (slice.items(.header), slice.items(.subsections), 0..) |header, subs, n_sect| { | |
| 538 | if (isCstringLiteral(header) or isFixedSizeLiteral(header)) { | |
| 539 | const data = try self.getSectionData(@intCast(n_sect), macho_file); | |
| 540 | defer gpa.free(data); | |
| 541 | ||
| 542 | for (subs.items) |sub| { | |
| 543 | const atom = macho_file.getAtom(sub.atom).?; | |
| 544 | const atom_off = math.cast(usize, atom.off) orelse return error.Overflow; | |
| 545 | const atom_size = math.cast(usize, atom.size) orelse return error.Overflow; | |
| 546 | const atom_data = data[atom_off..][0..atom_size]; | |
| 547 | const res = try lp.insert(gpa, header.type(), atom_data); | |
| 548 | if (!res.found_existing) { | |
| 549 | res.atom.* = sub.atom; | |
| 550 | } | |
| 551 | atom.flags.literal_pool = true; | |
| 552 | try atom.addExtra(.{ .literal_index = res.index }, macho_file); | |
| 553 | } | |
| 554 | } else if (isPtrLiteral(header)) { | |
| 555 | for (subs.items) |sub| { | |
| 556 | const atom = macho_file.getAtom(sub.atom).?; | |
| 557 | const relocs = atom.getRelocs(macho_file); | |
| 558 | assert(relocs.len == 1); | |
| 559 | const rel = relocs[0]; | |
| 560 | const target = switch (rel.tag) { | |
| 561 | .local => rel.target, | |
| 562 | .@"extern" => rel.getTargetSymbol(macho_file).atom, | |
| 563 | }; | |
| 564 | const addend = math.cast(u32, rel.addend) orelse return error.Overflow; | |
| 565 | const target_atom = macho_file.getAtom(target).?; | |
| 566 | const target_atom_size = math.cast(usize, target_atom.size) orelse return error.Overflow; | |
| 567 | try buffer.ensureUnusedCapacity(target_atom_size); | |
| 568 | buffer.resize(target_atom_size) catch unreachable; | |
| 569 | try target_atom.getData(macho_file, buffer.items); | |
| 570 | const res = try lp.insert(gpa, header.type(), buffer.items[addend..]); | |
| 571 | buffer.clearRetainingCapacity(); | |
| 572 | if (!res.found_existing) { | |
| 573 | res.atom.* = sub.atom; | |
| 574 | } | |
| 575 | atom.flags.literal_pool = true; | |
| 576 | try atom.addExtra(.{ .literal_index = res.index }, macho_file); | |
| 577 | } | |
| 578 | } | |
| 579 | } | |
| 580 | } | |
| 581 | ||
| 582 | pub fn dedupLiterals(self: Object, lp: MachO.LiteralPool, macho_file: *MachO) void { | |
| 583 | for (self.atoms.items) |atom_index| { | |
| 584 | const atom = macho_file.getAtom(atom_index) orelse continue; | |
| 585 | if (!atom.flags.alive) continue; | |
| 586 | if (!atom.flags.relocs) continue; | |
| 587 | ||
| 588 | const relocs = blk: { | |
| 589 | const extra = atom.getExtra(macho_file).?; | |
| 590 | const relocs = self.sections.items(.relocs)[atom.n_sect].items; | |
| 591 | break :blk relocs[extra.rel_index..][0..extra.rel_count]; | |
| 592 | }; | |
| 593 | for (relocs) |*rel| switch (rel.tag) { | |
| 594 | .local => { | |
| 595 | const target = macho_file.getAtom(rel.target).?; | |
| 596 | if (target.getLiteralPoolIndex(macho_file)) |lp_index| { | |
| 597 | const lp_atom = lp.getAtom(lp_index, macho_file); | |
| 598 | if (target.atom_index != lp_atom.atom_index) { | |
| 599 | lp_atom.alignment = lp_atom.alignment.max(target.alignment); | |
| 600 | target.flags.alive = false; | |
| 601 | rel.target = lp_atom.atom_index; | |
| 602 | } | |
| 603 | } | |
| 604 | }, | |
| 605 | .@"extern" => { | |
| 606 | const target_sym = rel.getTargetSymbol(macho_file); | |
| 607 | if (target_sym.getAtom(macho_file)) |target_atom| { | |
| 608 | if (target_atom.getLiteralPoolIndex(macho_file)) |lp_index| { | |
| 609 | const lp_atom = lp.getAtom(lp_index, macho_file); | |
| 610 | if (target_atom.atom_index != lp_atom.atom_index) { | |
| 611 | lp_atom.alignment = lp_atom.alignment.max(target_atom.alignment); | |
| 612 | target_atom.flags.alive = false; | |
| 613 | target_sym.atom = lp_atom.atom_index; | |
| 614 | } | |
| 615 | } | |
| 616 | } | |
| 617 | }, | |
| 618 | }; | |
| 619 | } | |
| 620 | } | |
| 621 | ||
| 396 | 622 | const AddAtomArgs = struct { |
| 397 | 623 | name: u32, |
| 398 | 624 | n_sect: u8, |
| ... | ... | @@ -416,34 +642,6 @@ fn addAtom(self: *Object, args: AddAtomArgs, macho_file: *MachO) !Atom.Index { |
| 416 | 642 | return atom_index; |
| 417 | 643 | } |
| 418 | 644 | |
| 419 | fn initLiteralSections(self: *Object, macho_file: *MachO) !void { | |
| 420 | const tracy = trace(@src()); | |
| 421 | defer tracy.end(); | |
| 422 | // TODO here we should split into equal-sized records, hash the contents, and then | |
| 423 | // deduplicate - ICF. | |
| 424 | // For now, we simply cover each literal section with one large atom. | |
| 425 | const gpa = macho_file.base.comp.gpa; | |
| 426 | const slice = self.sections.slice(); | |
| 427 | ||
| 428 | try self.atoms.ensureUnusedCapacity(gpa, self.sections.items(.header).len); | |
| 429 | ||
| 430 | for (slice.items(.header), 0..) |sect, n_sect| { | |
| 431 | if (!isLiteral(sect)) continue; | |
| 432 | ||
| 433 | const name = try std.fmt.allocPrintZ(gpa, "{s}${s}", .{ sect.segName(), sect.sectName() }); | |
| 434 | defer gpa.free(name); | |
| 435 | ||
| 436 | const atom_index = try self.addAtom(.{ | |
| 437 | .name = try self.addString(gpa, name), | |
| 438 | .n_sect = @intCast(n_sect), | |
| 439 | .off = 0, | |
| 440 | .size = sect.size, | |
| 441 | .alignment = sect.@"align", | |
| 442 | }, macho_file); | |
| 443 | try slice.items(.subsections)[n_sect].append(gpa, .{ .atom = atom_index, .off = 0 }); | |
| 444 | } | |
| 445 | } | |
| 446 | ||
| 447 | 645 | pub fn findAtom(self: Object, addr: u64) ?Atom.Index { |
| 448 | 646 | const tracy = trace(@src()); |
| 449 | 647 | defer tracy.end(); |
| ... | ... | @@ -1369,7 +1567,10 @@ pub fn calcSymtabSize(self: *Object, macho_file: *MachO) !void { |
| 1369 | 1567 | const name = sym.getName(macho_file); |
| 1370 | 1568 | // TODO in -r mode, we actually want to merge symbol names and emit only one |
| 1371 | 1569 | // work it out when emitting relocs |
| 1372 | if (name.len > 0 and (name[0] == 'L' or name[0] == 'l') and !macho_file.base.isObject()) continue; | |
| 1570 | if (name.len > 0 and | |
| 1571 | (name[0] == 'L' or name[0] == 'l' or | |
| 1572 | mem.startsWith(u8, name, "_OBJC_SELECTOR_REFERENCES_")) and | |
| 1573 | !macho_file.base.isObject()) continue; | |
| 1373 | 1574 | sym.flags.output_symtab = true; |
| 1374 | 1575 | if (sym.isLocal()) { |
| 1375 | 1576 | try sym.addExtra(.{ .symtab = self.output_symtab_ctx.nlocals }, macho_file); |
src/link/MachO/Relocation.zig+53| ... | ... | @@ -60,6 +60,59 @@ pub fn lessThan(ctx: void, lhs: Relocation, rhs: Relocation) bool { |
| 60 | 60 | return lhs.offset < rhs.offset; |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | const FormatCtx = struct { Relocation, std.Target.Cpu.Arch }; | |
| 64 | ||
| 65 | pub fn fmtPretty(rel: Relocation, cpu_arch: std.Target.Cpu.Arch) std.fmt.Formatter(formatPretty) { | |
| 66 | return .{ .data = .{ rel, cpu_arch } }; | |
| 67 | } | |
| 68 | ||
| 69 | fn formatPretty( | |
| 70 | ctx: FormatCtx, | |
| 71 | comptime unused_fmt_string: []const u8, | |
| 72 | options: std.fmt.FormatOptions, | |
| 73 | writer: anytype, | |
| 74 | ) !void { | |
| 75 | _ = options; | |
| 76 | _ = unused_fmt_string; | |
| 77 | const rel, const cpu_arch = ctx; | |
| 78 | const str = switch (rel.type) { | |
| 79 | .signed => "X86_64_RELOC_SIGNED", | |
| 80 | .signed1 => "X86_64_RELOC_SIGNED_1", | |
| 81 | .signed2 => "X86_64_RELOC_SIGNED_2", | |
| 82 | .signed4 => "X86_64_RELOC_SIGNED_4", | |
| 83 | .got_load => "X86_64_RELOC_GOT_LOAD", | |
| 84 | .tlv => "X86_64_RELOC_TLV", | |
| 85 | .zig_got_load => "ZIG_GOT_LOAD", | |
| 86 | .page => "ARM64_RELOC_PAGE21", | |
| 87 | .pageoff => "ARM64_RELOC_PAGEOFF12", | |
| 88 | .got_load_page => "ARM64_RELOC_GOT_LOAD_PAGE21", | |
| 89 | .got_load_pageoff => "ARM64_RELOC_GOT_LOAD_PAGEOFF12", | |
| 90 | .tlvp_page => "ARM64_RELOC_TLVP_LOAD_PAGE21", | |
| 91 | .tlvp_pageoff => "ARM64_RELOC_TLVP_LOAD_PAGEOFF12", | |
| 92 | .branch => switch (cpu_arch) { | |
| 93 | .x86_64 => "X86_64_RELOC_BRANCH", | |
| 94 | .aarch64 => "ARM64_RELOC_BRANCH26", | |
| 95 | else => unreachable, | |
| 96 | }, | |
| 97 | .got => switch (cpu_arch) { | |
| 98 | .x86_64 => "X86_64_RELOC_GOT", | |
| 99 | .aarch64 => "ARM64_RELOC_POINTER_TO_GOT", | |
| 100 | else => unreachable, | |
| 101 | }, | |
| 102 | .subtractor => switch (cpu_arch) { | |
| 103 | .x86_64 => "X86_64_RELOC_SUBTRACTOR", | |
| 104 | .aarch64 => "ARM64_RELOC_SUBTRACTOR", | |
| 105 | else => unreachable, | |
| 106 | }, | |
| 107 | .unsigned => switch (cpu_arch) { | |
| 108 | .x86_64 => "X86_64_RELOC_UNSIGNED", | |
| 109 | .aarch64 => "ARM64_RELOC_UNSIGNED", | |
| 110 | else => unreachable, | |
| 111 | }, | |
| 112 | }; | |
| 113 | try writer.writeAll(str); | |
| 114 | } | |
| 115 | ||
| 63 | 116 | pub const Type = enum { |
| 64 | 117 | // x86_64 |
| 65 | 118 | /// RIP-relative displacement (X86_64_RELOC_SIGNED) |
src/link/MachO/Symbol.zig+2-2| ... | ... | @@ -14,8 +14,8 @@ file: File.Index = 0, |
| 14 | 14 | /// Use `getAtom` to get the pointer to the atom. |
| 15 | 15 | atom: Atom.Index = 0, |
| 16 | 16 | |
| 17 | /// Assigned output section index for this atom. | |
| 18 | out_n_sect: u16 = 0, | |
| 17 | /// Assigned output section index for this symbol. | |
| 18 | out_n_sect: u8 = 0, | |
| 19 | 19 | |
| 20 | 20 | /// Index of the source nlist this symbol references. |
| 21 | 21 | /// Use `getNlist` to pull the nlist from the relevant file. |
src/link/MachO/ZigObject.zig+14| ... | ... | @@ -314,6 +314,20 @@ pub fn checkDuplicates(self: *ZigObject, dupes: anytype, macho_file: *MachO) !vo |
| 314 | 314 | } |
| 315 | 315 | } |
| 316 | 316 | |
| 317 | pub fn resolveLiterals(self: *ZigObject, lp: *MachO.LiteralPool, macho_file: *MachO) !void { | |
| 318 | _ = self; | |
| 319 | _ = lp; | |
| 320 | _ = macho_file; | |
| 321 | // TODO | |
| 322 | } | |
| 323 | ||
| 324 | pub fn dedupLiterals(self: *ZigObject, lp: MachO.LiteralPool, macho_file: *MachO) void { | |
| 325 | _ = self; | |
| 326 | _ = lp; | |
| 327 | _ = macho_file; | |
| 328 | // TODO | |
| 329 | } | |
| 330 | ||
| 317 | 331 | /// This is just a temporary helper function that allows us to re-read what we wrote to file into a buffer. |
| 318 | 332 | /// We need this so that we can write to an archive. |
| 319 | 333 | /// TODO implement writing ZigObject data directly to a buffer instead. |
src/link/MachO/relocatable.zig+13-8| ... | ... | @@ -46,6 +46,7 @@ pub fn flushObject(macho_file: *MachO, comp: *Compilation, module_obj_path: ?[]c |
| 46 | 46 | |
| 47 | 47 | try macho_file.addUndefinedGlobals(); |
| 48 | 48 | try macho_file.resolveSymbols(); |
| 49 | try macho_file.dedupLiterals(); | |
| 49 | 50 | markExports(macho_file); |
| 50 | 51 | claimUnresolved(macho_file); |
| 51 | 52 | try initOutputSections(macho_file); |
| ... | ... | @@ -542,6 +543,9 @@ fn writeAtoms(macho_file: *MachO) !void { |
| 542 | 543 | const cpu_arch = macho_file.getTarget().cpu.arch; |
| 543 | 544 | const slice = macho_file.sections.slice(); |
| 544 | 545 | |
| 546 | var relocs = std.ArrayList(macho.relocation_info).init(gpa); | |
| 547 | defer relocs.deinit(); | |
| 548 | ||
| 545 | 549 | for (slice.items(.header), slice.items(.atoms), 0..) |header, atoms, i| { |
| 546 | 550 | if (atoms.items.len == 0) continue; |
| 547 | 551 | if (header.isZerofill()) continue; |
| ... | ... | @@ -553,8 +557,7 @@ fn writeAtoms(macho_file: *MachO) !void { |
| 553 | 557 | const padding_byte: u8 = if (header.isCode() and cpu_arch == .x86_64) 0xcc else 0; |
| 554 | 558 | @memset(code, padding_byte); |
| 555 | 559 | |
| 556 | var relocs = try std.ArrayList(macho.relocation_info).initCapacity(gpa, header.nreloc); | |
| 557 | defer relocs.deinit(); | |
| 560 | try relocs.ensureTotalCapacity(header.nreloc); | |
| 558 | 561 | |
| 559 | 562 | for (atoms.items) |atom_index| { |
| 560 | 563 | const atom = macho_file.getAtom(atom_index).?; |
| ... | ... | @@ -572,22 +575,24 @@ fn writeAtoms(macho_file: *MachO) !void { |
| 572 | 575 | // TODO scattered writes? |
| 573 | 576 | try macho_file.base.file.?.pwriteAll(code, header.offset); |
| 574 | 577 | try macho_file.base.file.?.pwriteAll(mem.sliceAsBytes(relocs.items), header.reloff); |
| 578 | ||
| 579 | relocs.clearRetainingCapacity(); | |
| 575 | 580 | } |
| 576 | 581 | |
| 577 | 582 | if (macho_file.getZigObject()) |zo| { |
| 578 | 583 | // TODO: this is ugly; perhaps we should aggregrate before? |
| 579 | var relocs = std.AutoArrayHashMap(u8, std.ArrayList(macho.relocation_info)).init(gpa); | |
| 584 | var zo_relocs = std.AutoArrayHashMap(u8, std.ArrayList(macho.relocation_info)).init(gpa); | |
| 580 | 585 | defer { |
| 581 | for (relocs.values()) |*list| { | |
| 586 | for (zo_relocs.values()) |*list| { | |
| 582 | 587 | list.deinit(); |
| 583 | 588 | } |
| 584 | relocs.deinit(); | |
| 589 | zo_relocs.deinit(); | |
| 585 | 590 | } |
| 586 | 591 | |
| 587 | 592 | for (macho_file.sections.items(.header), 0..) |header, n_sect| { |
| 588 | 593 | if (header.isZerofill()) continue; |
| 589 | 594 | if (!macho_file.isZigSection(@intCast(n_sect)) and !macho_file.isDebugSection(@intCast(n_sect))) continue; |
| 590 | const gop = try relocs.getOrPut(@intCast(n_sect)); | |
| 595 | const gop = try zo_relocs.getOrPut(@intCast(n_sect)); | |
| 591 | 596 | if (gop.found_existing) continue; |
| 592 | 597 | gop.value_ptr.* = try std.ArrayList(macho.relocation_info).initCapacity(gpa, header.nreloc); |
| 593 | 598 | } |
| ... | ... | @@ -618,12 +623,12 @@ fn writeAtoms(macho_file: *MachO) !void { |
| 618 | 623 | }, |
| 619 | 624 | }; |
| 620 | 625 | const file_offset = header.offset + atom.value; |
| 621 | const rels = relocs.getPtr(atom.out_n_sect).?; | |
| 626 | const rels = zo_relocs.getPtr(atom.out_n_sect).?; | |
| 622 | 627 | try atom.writeRelocs(macho_file, code, rels); |
| 623 | 628 | try macho_file.base.file.?.pwriteAll(code, file_offset); |
| 624 | 629 | } |
| 625 | 630 | |
| 626 | for (relocs.keys(), relocs.values()) |sect_id, rels| { | |
| 631 | for (zo_relocs.keys(), zo_relocs.values()) |sect_id, rels| { | |
| 627 | 632 | const header = macho_file.sections.items(.header)[sect_id]; |
| 628 | 633 | assert(rels.items.len == header.nreloc); |
| 629 | 634 | mem.sort(macho.relocation_info, rels.items, {}, sortReloc); |
test/link/macho.zig+484| ... | ... | @@ -38,6 +38,10 @@ pub fn testAll(b: *Build, build_opts: BuildOptions) *Step { |
| 38 | 38 | macho_step.dependOn(testLayout(b, .{ .target = default_target })); |
| 39 | 39 | macho_step.dependOn(testLinkingStaticLib(b, .{ .target = default_target })); |
| 40 | 40 | macho_step.dependOn(testLinksection(b, .{ .target = default_target })); |
| 41 | macho_step.dependOn(testMergeLiteralsX64(b, .{ .target = x86_64_target })); | |
| 42 | macho_step.dependOn(testMergeLiteralsArm64(b, .{ .target = aarch64_target })); | |
| 43 | macho_step.dependOn(testMergeLiteralsArm642(b, .{ .target = aarch64_target })); | |
| 44 | macho_step.dependOn(testMergeLiteralsAlignment(b, .{ .target = aarch64_target })); | |
| 41 | 45 | macho_step.dependOn(testMhExecuteHeader(b, .{ .target = default_target })); |
| 42 | 46 | macho_step.dependOn(testNoDeadStrip(b, .{ .target = default_target })); |
| 43 | 47 | macho_step.dependOn(testNoExportsDylib(b, .{ .target = default_target })); |
| ... | ... | @@ -81,6 +85,7 @@ pub fn testAll(b: *Build, build_opts: BuildOptions) *Step { |
| 81 | 85 | macho_step.dependOn(testDeadStripDylibs(b, .{ .target = b.host })); |
| 82 | 86 | macho_step.dependOn(testHeaderpad(b, .{ .target = b.host })); |
| 83 | 87 | macho_step.dependOn(testLinkDirectlyCppTbd(b, .{ .target = b.host })); |
| 88 | macho_step.dependOn(testMergeLiteralsObjc(b, .{ .target = b.host })); | |
| 84 | 89 | macho_step.dependOn(testNeededFramework(b, .{ .target = b.host })); |
| 85 | 90 | macho_step.dependOn(testObjc(b, .{ .target = b.host })); |
| 86 | 91 | macho_step.dependOn(testObjcpp(b, .{ .target = b.host })); |
| ... | ... | @@ -914,6 +919,485 @@ fn testLinksection(b: *Build, opts: Options) *Step { |
| 914 | 919 | return test_step; |
| 915 | 920 | } |
| 916 | 921 | |
| 922 | fn testMergeLiteralsX64(b: *Build, opts: Options) *Step { | |
| 923 | const test_step = addTestStep(b, "merge-literals-x64", opts); | |
| 924 | ||
| 925 | const a_o = addObject(b, opts, .{ .name = "a", .asm_source_bytes = | |
| 926 | \\.globl _q1 | |
| 927 | \\.globl _s1 | |
| 928 | \\ | |
| 929 | \\.align 4 | |
| 930 | \\_q1: | |
| 931 | \\ lea L._q1(%rip), %rax | |
| 932 | \\ mov (%rax), %xmm0 | |
| 933 | \\ ret | |
| 934 | \\ | |
| 935 | \\.section __TEXT,__cstring,cstring_literals | |
| 936 | \\l._s1: | |
| 937 | \\ .asciz "hello" | |
| 938 | \\ | |
| 939 | \\.section __TEXT,__literal8,8byte_literals | |
| 940 | \\.align 8 | |
| 941 | \\L._q1: | |
| 942 | \\ .double 1.2345 | |
| 943 | \\ | |
| 944 | \\.section __DATA,__data | |
| 945 | \\.align 8 | |
| 946 | \\_s1: | |
| 947 | \\ .quad l._s1 | |
| 948 | }); | |
| 949 | ||
| 950 | const b_o = addObject(b, opts, .{ .name = "b", .asm_source_bytes = | |
| 951 | \\.globl _q2 | |
| 952 | \\.globl _s2 | |
| 953 | \\.globl _s3 | |
| 954 | \\ | |
| 955 | \\.align 4 | |
| 956 | \\_q2: | |
| 957 | \\ lea L._q2(%rip), %rax | |
| 958 | \\ mov (%rax), %xmm0 | |
| 959 | \\ ret | |
| 960 | \\ | |
| 961 | \\.section __TEXT,__cstring,cstring_literals | |
| 962 | \\l._s2: | |
| 963 | \\ .asciz "hello" | |
| 964 | \\l._s3: | |
| 965 | \\ .asciz "world" | |
| 966 | \\ | |
| 967 | \\.section __TEXT,__literal8,8byte_literals | |
| 968 | \\.align 8 | |
| 969 | \\L._q2: | |
| 970 | \\ .double 1.2345 | |
| 971 | \\ | |
| 972 | \\.section __DATA,__data | |
| 973 | \\.align 8 | |
| 974 | \\_s2: | |
| 975 | \\ .quad l._s2 | |
| 976 | \\_s3: | |
| 977 | \\ .quad l._s3 | |
| 978 | }); | |
| 979 | ||
| 980 | const main_o = addObject(b, opts, .{ .name = "main", .c_source_bytes = | |
| 981 | \\#include <stdio.h> | |
| 982 | \\extern double q1(); | |
| 983 | \\extern double q2(); | |
| 984 | \\extern const char* s1; | |
| 985 | \\extern const char* s2; | |
| 986 | \\extern const char* s3; | |
| 987 | \\int main() { | |
| 988 | \\ printf("%s, %s, %s, %f, %f", s1, s2, s3, q1(), q2()); | |
| 989 | \\ return 0; | |
| 990 | \\} | |
| 991 | }); | |
| 992 | ||
| 993 | const runWithChecks = struct { | |
| 994 | fn runWithChecks(step: *Step, exe: *Compile) void { | |
| 995 | const run = addRunArtifact(exe); | |
| 996 | run.expectStdOutEqual("hello, hello, world, 1.234500, 1.234500"); | |
| 997 | step.dependOn(&run.step); | |
| 998 | ||
| 999 | const check = exe.checkObject(); | |
| 1000 | check.dumpSection("__TEXT,__const"); | |
| 1001 | check.checkContains("\x8d\x97n\x12\x83\xc0\xf3?"); | |
| 1002 | check.dumpSection("__TEXT,__cstring"); | |
| 1003 | check.checkContains("hello\x00world\x00%s, %s, %s, %f, %f\x00"); | |
| 1004 | step.dependOn(&check.step); | |
| 1005 | } | |
| 1006 | }.runWithChecks; | |
| 1007 | ||
| 1008 | { | |
| 1009 | const exe = addExecutable(b, opts, .{ .name = "main1" }); | |
| 1010 | exe.addObject(a_o); | |
| 1011 | exe.addObject(b_o); | |
| 1012 | exe.addObject(main_o); | |
| 1013 | runWithChecks(test_step, exe); | |
| 1014 | } | |
| 1015 | ||
| 1016 | { | |
| 1017 | const exe = addExecutable(b, opts, .{ .name = "main2" }); | |
| 1018 | exe.addObject(b_o); | |
| 1019 | exe.addObject(a_o); | |
| 1020 | exe.addObject(main_o); | |
| 1021 | runWithChecks(test_step, exe); | |
| 1022 | } | |
| 1023 | ||
| 1024 | { | |
| 1025 | const c_o = addObject(b, opts, .{ .name = "c" }); | |
| 1026 | c_o.addObject(a_o); | |
| 1027 | c_o.addObject(b_o); | |
| 1028 | c_o.addObject(main_o); | |
| 1029 | ||
| 1030 | const exe = addExecutable(b, opts, .{ .name = "main3" }); | |
| 1031 | exe.addObject(c_o); | |
| 1032 | runWithChecks(test_step, exe); | |
| 1033 | } | |
| 1034 | ||
| 1035 | return test_step; | |
| 1036 | } | |
| 1037 | ||
| 1038 | fn testMergeLiteralsArm64(b: *Build, opts: Options) *Step { | |
| 1039 | const test_step = addTestStep(b, "merge-literals-arm64", opts); | |
| 1040 | ||
| 1041 | const a_o = addObject(b, opts, .{ .name = "a", .asm_source_bytes = | |
| 1042 | \\.globl _q1 | |
| 1043 | \\.globl _s1 | |
| 1044 | \\ | |
| 1045 | \\.align 4 | |
| 1046 | \\_q1: | |
| 1047 | \\ adrp x8, L._q1@PAGE | |
| 1048 | \\ ldr d0, [x8, L._q1@PAGEOFF] | |
| 1049 | \\ ret | |
| 1050 | \\ | |
| 1051 | \\.section __TEXT,__cstring,cstring_literals | |
| 1052 | \\l._s1: | |
| 1053 | \\ .asciz "hello" | |
| 1054 | \\ | |
| 1055 | \\.section __TEXT,__literal8,8byte_literals | |
| 1056 | \\.align 8 | |
| 1057 | \\L._q1: | |
| 1058 | \\ .double 1.2345 | |
| 1059 | \\ | |
| 1060 | \\.section __DATA,__data | |
| 1061 | \\.align 8 | |
| 1062 | \\_s1: | |
| 1063 | \\ .quad l._s1 | |
| 1064 | }); | |
| 1065 | ||
| 1066 | const b_o = addObject(b, opts, .{ .name = "b", .asm_source_bytes = | |
| 1067 | \\.globl _q2 | |
| 1068 | \\.globl _s2 | |
| 1069 | \\.globl _s3 | |
| 1070 | \\ | |
| 1071 | \\.align 4 | |
| 1072 | \\_q2: | |
| 1073 | \\ adrp x8, L._q2@PAGE | |
| 1074 | \\ ldr d0, [x8, L._q2@PAGEOFF] | |
| 1075 | \\ ret | |
| 1076 | \\ | |
| 1077 | \\.section __TEXT,__cstring,cstring_literals | |
| 1078 | \\l._s2: | |
| 1079 | \\ .asciz "hello" | |
| 1080 | \\l._s3: | |
| 1081 | \\ .asciz "world" | |
| 1082 | \\ | |
| 1083 | \\.section __TEXT,__literal8,8byte_literals | |
| 1084 | \\.align 8 | |
| 1085 | \\L._q2: | |
| 1086 | \\ .double 1.2345 | |
| 1087 | \\ | |
| 1088 | \\.section __DATA,__data | |
| 1089 | \\.align 8 | |
| 1090 | \\_s2: | |
| 1091 | \\ .quad l._s2 | |
| 1092 | \\_s3: | |
| 1093 | \\ .quad l._s3 | |
| 1094 | }); | |
| 1095 | ||
| 1096 | const main_o = addObject(b, opts, .{ .name = "main", .c_source_bytes = | |
| 1097 | \\#include <stdio.h> | |
| 1098 | \\extern double q1(); | |
| 1099 | \\extern double q2(); | |
| 1100 | \\extern const char* s1; | |
| 1101 | \\extern const char* s2; | |
| 1102 | \\extern const char* s3; | |
| 1103 | \\int main() { | |
| 1104 | \\ printf("%s, %s, %s, %f, %f", s1, s2, s3, q1(), q2()); | |
| 1105 | \\ return 0; | |
| 1106 | \\} | |
| 1107 | }); | |
| 1108 | ||
| 1109 | const runWithChecks = struct { | |
| 1110 | fn runWithChecks(step: *Step, exe: *Compile) void { | |
| 1111 | const run = addRunArtifact(exe); | |
| 1112 | run.expectStdOutEqual("hello, hello, world, 1.234500, 1.234500"); | |
| 1113 | step.dependOn(&run.step); | |
| 1114 | ||
| 1115 | const check = exe.checkObject(); | |
| 1116 | check.dumpSection("__TEXT,__const"); | |
| 1117 | check.checkContains("\x8d\x97n\x12\x83\xc0\xf3?"); | |
| 1118 | check.dumpSection("__TEXT,__cstring"); | |
| 1119 | check.checkContains("hello\x00world\x00%s, %s, %s, %f, %f\x00"); | |
| 1120 | step.dependOn(&check.step); | |
| 1121 | } | |
| 1122 | }.runWithChecks; | |
| 1123 | ||
| 1124 | { | |
| 1125 | const exe = addExecutable(b, opts, .{ .name = "main1" }); | |
| 1126 | exe.addObject(a_o); | |
| 1127 | exe.addObject(b_o); | |
| 1128 | exe.addObject(main_o); | |
| 1129 | runWithChecks(test_step, exe); | |
| 1130 | } | |
| 1131 | ||
| 1132 | { | |
| 1133 | const exe = addExecutable(b, opts, .{ .name = "main2" }); | |
| 1134 | exe.addObject(b_o); | |
| 1135 | exe.addObject(a_o); | |
| 1136 | exe.addObject(main_o); | |
| 1137 | runWithChecks(test_step, exe); | |
| 1138 | } | |
| 1139 | ||
| 1140 | { | |
| 1141 | const c_o = addObject(b, opts, .{ .name = "c" }); | |
| 1142 | c_o.addObject(a_o); | |
| 1143 | c_o.addObject(b_o); | |
| 1144 | c_o.addObject(main_o); | |
| 1145 | ||
| 1146 | const exe = addExecutable(b, opts, .{ .name = "main3" }); | |
| 1147 | exe.addObject(c_o); | |
| 1148 | runWithChecks(test_step, exe); | |
| 1149 | } | |
| 1150 | ||
| 1151 | return test_step; | |
| 1152 | } | |
| 1153 | ||
| 1154 | /// This particular test case will generate invalid machine code that will segfault at runtime. | |
| 1155 | /// However, this is by design as we want to test that the linker does not panic when linking it | |
| 1156 | /// which is also the case for the system linker and lld - linking succeeds, runtime segfaults. | |
| 1157 | /// It should also be mentioned that runtime segfault is not due to the linker but faulty input asm. | |
| 1158 | fn testMergeLiteralsArm642(b: *Build, opts: Options) *Step { | |
| 1159 | const test_step = addTestStep(b, "merge-literals-arm64-2", opts); | |
| 1160 | ||
| 1161 | const a_o = addObject(b, opts, .{ .name = "a", .asm_source_bytes = | |
| 1162 | \\.globl _q1 | |
| 1163 | \\.globl _s1 | |
| 1164 | \\ | |
| 1165 | \\.align 4 | |
| 1166 | \\_q1: | |
| 1167 | \\ adrp x0, L._q1@PAGE | |
| 1168 | \\ ldr x0, [x0, L._q1@PAGEOFF] | |
| 1169 | \\ ret | |
| 1170 | \\ | |
| 1171 | \\.section __TEXT,__cstring,cstring_literals | |
| 1172 | \\_s1: | |
| 1173 | \\ .asciz "hello" | |
| 1174 | \\ | |
| 1175 | \\.section __TEXT,__literal8,8byte_literals | |
| 1176 | \\.align 8 | |
| 1177 | \\L._q1: | |
| 1178 | \\ .double 1.2345 | |
| 1179 | }); | |
| 1180 | ||
| 1181 | const b_o = addObject(b, opts, .{ .name = "b", .asm_source_bytes = | |
| 1182 | \\.globl _q2 | |
| 1183 | \\.globl _s2 | |
| 1184 | \\.globl _s3 | |
| 1185 | \\ | |
| 1186 | \\.align 4 | |
| 1187 | \\_q2: | |
| 1188 | \\ adrp x0, L._q2@PAGE | |
| 1189 | \\ ldr x0, [x0, L._q2@PAGEOFF] | |
| 1190 | \\ ret | |
| 1191 | \\ | |
| 1192 | \\.section __TEXT,__cstring,cstring_literals | |
| 1193 | \\_s2: | |
| 1194 | \\ .asciz "hello" | |
| 1195 | \\_s3: | |
| 1196 | \\ .asciz "world" | |
| 1197 | \\ | |
| 1198 | \\.section __TEXT,__literal8,8byte_literals | |
| 1199 | \\.align 8 | |
| 1200 | \\L._q2: | |
| 1201 | \\ .double 1.2345 | |
| 1202 | }); | |
| 1203 | ||
| 1204 | const main_o = addObject(b, opts, .{ .name = "main", .c_source_bytes = | |
| 1205 | \\#include <stdio.h> | |
| 1206 | \\extern double q1(); | |
| 1207 | \\extern double q2(); | |
| 1208 | \\extern const char* s1; | |
| 1209 | \\extern const char* s2; | |
| 1210 | \\extern const char* s3; | |
| 1211 | \\int main() { | |
| 1212 | \\ printf("%s, %s, %s, %f, %f", s1, s2, s3, q1(), q2()); | |
| 1213 | \\ return 0; | |
| 1214 | \\} | |
| 1215 | }); | |
| 1216 | ||
| 1217 | const exe = addExecutable(b, opts, .{ .name = "main1" }); | |
| 1218 | exe.addObject(a_o); | |
| 1219 | exe.addObject(b_o); | |
| 1220 | exe.addObject(main_o); | |
| 1221 | ||
| 1222 | const check = exe.checkObject(); | |
| 1223 | check.dumpSection("__TEXT,__const"); | |
| 1224 | check.checkContains("\x8d\x97n\x12\x83\xc0\xf3?"); | |
| 1225 | check.dumpSection("__TEXT,__cstring"); | |
| 1226 | check.checkContains("hello\x00world\x00%s, %s, %s, %f, %f\x00"); | |
| 1227 | test_step.dependOn(&check.step); | |
| 1228 | ||
| 1229 | return test_step; | |
| 1230 | } | |
| 1231 | ||
| 1232 | fn testMergeLiteralsAlignment(b: *Build, opts: Options) *Step { | |
| 1233 | const test_step = addTestStep(b, "merge-literals-alignment", opts); | |
| 1234 | ||
| 1235 | const a_o = addObject(b, opts, .{ .name = "a", .asm_source_bytes = | |
| 1236 | \\.globl _s1 | |
| 1237 | \\.globl _s2 | |
| 1238 | \\ | |
| 1239 | \\.section __TEXT,__cstring,cstring_literals | |
| 1240 | \\.align 3 | |
| 1241 | \\_s1: | |
| 1242 | \\ .asciz "str1" | |
| 1243 | \\_s2: | |
| 1244 | \\ .asciz "str2" | |
| 1245 | }); | |
| 1246 | ||
| 1247 | const b_o = addObject(b, opts, .{ .name = "b", .asm_source_bytes = | |
| 1248 | \\.globl _s3 | |
| 1249 | \\.globl _s4 | |
| 1250 | \\ | |
| 1251 | \\.section __TEXT,__cstring,cstring_literals | |
| 1252 | \\.align 2 | |
| 1253 | \\_s3: | |
| 1254 | \\ .asciz "str1" | |
| 1255 | \\_s4: | |
| 1256 | \\ .asciz "str2" | |
| 1257 | }); | |
| 1258 | ||
| 1259 | const main_o = addObject(b, opts, .{ .name = "main", .c_source_bytes = | |
| 1260 | \\#include <assert.h> | |
| 1261 | \\#include <stdint.h> | |
| 1262 | \\#include <stdio.h> | |
| 1263 | \\extern const char* s1; | |
| 1264 | \\extern const char* s2; | |
| 1265 | \\extern const char* s3; | |
| 1266 | \\extern const char* s4; | |
| 1267 | \\int main() { | |
| 1268 | \\ assert((uintptr_t)(&s1) % 8 == 0 && s1 == s3); | |
| 1269 | \\ assert((uintptr_t)(&s2) % 8 == 0 && s2 == s4); | |
| 1270 | \\ printf("%s%s%s%s", &s1, &s2, &s3, &s4); | |
| 1271 | \\ return 0; | |
| 1272 | \\} | |
| 1273 | , .c_source_flags = &.{"-Wno-format"} }); | |
| 1274 | ||
| 1275 | const runWithChecks = struct { | |
| 1276 | fn runWithChecks(step: *Step, exe: *Compile) void { | |
| 1277 | const run = addRunArtifact(exe); | |
| 1278 | run.expectStdOutEqual("str1str2str1str2"); | |
| 1279 | step.dependOn(&run.step); | |
| 1280 | ||
| 1281 | const check = exe.checkObject(); | |
| 1282 | check.dumpSection("__TEXT,__cstring"); | |
| 1283 | check.checkContains("str1\x00\x00\x00\x00str2\x00"); | |
| 1284 | check.checkInHeaders(); | |
| 1285 | check.checkExact("segname __TEXT"); | |
| 1286 | check.checkExact("sectname __cstring"); | |
| 1287 | check.checkExact("align 3"); | |
| 1288 | step.dependOn(&check.step); | |
| 1289 | } | |
| 1290 | }.runWithChecks; | |
| 1291 | ||
| 1292 | { | |
| 1293 | const exe = addExecutable(b, opts, .{ .name = "main1" }); | |
| 1294 | exe.addObject(a_o); | |
| 1295 | exe.addObject(b_o); | |
| 1296 | exe.addObject(main_o); | |
| 1297 | runWithChecks(test_step, exe); | |
| 1298 | } | |
| 1299 | ||
| 1300 | { | |
| 1301 | const exe = addExecutable(b, opts, .{ .name = "main2" }); | |
| 1302 | exe.addObject(b_o); | |
| 1303 | exe.addObject(a_o); | |
| 1304 | exe.addObject(main_o); | |
| 1305 | runWithChecks(test_step, exe); | |
| 1306 | } | |
| 1307 | ||
| 1308 | return test_step; | |
| 1309 | } | |
| 1310 | ||
| 1311 | fn testMergeLiteralsObjc(b: *Build, opts: Options) *Step { | |
| 1312 | const test_step = addTestStep(b, "merge-literals-objc", opts); | |
| 1313 | ||
| 1314 | const main_o = addObject(b, opts, .{ .name = "main", .objc_source_bytes = | |
| 1315 | \\#import <Foundation/Foundation.h>; | |
| 1316 | \\ | |
| 1317 | \\extern void foo(); | |
| 1318 | \\ | |
| 1319 | \\int main() { | |
| 1320 | \\ NSString *thing = @"aaa"; | |
| 1321 | \\ | |
| 1322 | \\ SEL sel = @selector(lowercaseString); | |
| 1323 | \\ NSString *lower = (([thing respondsToSelector:sel]) ? @"YES" : @"NO"); | |
| 1324 | \\ NSLog (@"Responds to lowercaseString: %@", lower); | |
| 1325 | \\ if ([thing respondsToSelector:sel]) //(lower == @"YES") | |
| 1326 | \\ NSLog(@"lowercaseString is: %@", [thing lowercaseString]); | |
| 1327 | \\ | |
| 1328 | \\ foo(); | |
| 1329 | \\} | |
| 1330 | }); | |
| 1331 | ||
| 1332 | const a_o = addObject(b, opts, .{ .name = "a", .objc_source_bytes = | |
| 1333 | \\#import <Foundation/Foundation.h>; | |
| 1334 | \\ | |
| 1335 | \\void foo() { | |
| 1336 | \\ NSString *thing = @"aaa"; | |
| 1337 | \\ SEL sel = @selector(lowercaseString); | |
| 1338 | \\ NSString *lower = (([thing respondsToSelector:sel]) ? @"YES" : @"NO"); | |
| 1339 | \\ NSLog (@"Responds to lowercaseString in foo(): %@", lower); | |
| 1340 | \\ if ([thing respondsToSelector:sel]) //(lower == @"YES") | |
| 1341 | \\ NSLog(@"lowercaseString in foo() is: %@", [thing lowercaseString]); | |
| 1342 | \\ SEL sel2 = @selector(uppercaseString); | |
| 1343 | \\ NSString *upper = (([thing respondsToSelector:sel2]) ? @"YES" : @"NO"); | |
| 1344 | \\ NSLog (@"Responds to uppercaseString in foo(): %@", upper); | |
| 1345 | \\ if ([thing respondsToSelector:sel2]) //(upper == @"YES") | |
| 1346 | \\ NSLog(@"uppercaseString in foo() is: %@", [thing uppercaseString]); | |
| 1347 | \\} | |
| 1348 | }); | |
| 1349 | ||
| 1350 | const runWithChecks = struct { | |
| 1351 | fn runWithChecks(step: *Step, exe: *Compile) void { | |
| 1352 | const builder = step.owner; | |
| 1353 | const run = addRunArtifact(exe); | |
| 1354 | run.addCheck(.{ .expect_stderr_match = builder.dupe("Responds to lowercaseString: YES") }); | |
| 1355 | run.addCheck(.{ .expect_stderr_match = builder.dupe("lowercaseString is: aaa") }); | |
| 1356 | run.addCheck(.{ .expect_stderr_match = builder.dupe("Responds to lowercaseString in foo(): YES") }); | |
| 1357 | run.addCheck(.{ .expect_stderr_match = builder.dupe("lowercaseString in foo() is: aaa") }); | |
| 1358 | run.addCheck(.{ .expect_stderr_match = builder.dupe("Responds to uppercaseString in foo(): YES") }); | |
| 1359 | run.addCheck(.{ .expect_stderr_match = builder.dupe("uppercaseString in foo() is: AAA") }); | |
| 1360 | step.dependOn(&run.step); | |
| 1361 | ||
| 1362 | const check = exe.checkObject(); | |
| 1363 | check.dumpSection("__TEXT,__objc_methname"); | |
| 1364 | check.checkContains("lowercaseString\x00"); | |
| 1365 | check.dumpSection("__TEXT,__objc_methname"); | |
| 1366 | check.checkContains("uppercaseString\x00"); | |
| 1367 | step.dependOn(&check.step); | |
| 1368 | } | |
| 1369 | }.runWithChecks; | |
| 1370 | ||
| 1371 | { | |
| 1372 | const exe = addExecutable(b, opts, .{ .name = "main1" }); | |
| 1373 | exe.addObject(main_o); | |
| 1374 | exe.addObject(a_o); | |
| 1375 | exe.root_module.linkFramework("Foundation", .{}); | |
| 1376 | runWithChecks(test_step, exe); | |
| 1377 | } | |
| 1378 | ||
| 1379 | { | |
| 1380 | const exe = addExecutable(b, opts, .{ .name = "main2" }); | |
| 1381 | exe.addObject(a_o); | |
| 1382 | exe.addObject(main_o); | |
| 1383 | exe.root_module.linkFramework("Foundation", .{}); | |
| 1384 | runWithChecks(test_step, exe); | |
| 1385 | } | |
| 1386 | ||
| 1387 | { | |
| 1388 | const b_o = addObject(b, opts, .{ .name = "b" }); | |
| 1389 | b_o.addObject(a_o); | |
| 1390 | b_o.addObject(main_o); | |
| 1391 | ||
| 1392 | const exe = addExecutable(b, opts, .{ .name = "main3" }); | |
| 1393 | exe.addObject(b_o); | |
| 1394 | exe.root_module.linkFramework("Foundation", .{}); | |
| 1395 | runWithChecks(test_step, exe); | |
| 1396 | } | |
| 1397 | ||
| 1398 | return test_step; | |
| 1399 | } | |
| 1400 | ||
| 917 | 1401 | fn testMhExecuteHeader(b: *Build, opts: Options) *Step { |
| 918 | 1402 | const test_step = addTestStep(b, "mh-execute-header", opts); |
| 919 | 1403 |