authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-02-23 22:40:06+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-02-23 22:40:06+01:00
log9dd839b7ed15d1191f3303d069cffe0473e03e83
tree92d59fe28a7cb39e437a996086f479065a69bbb6
parent5d89955543d30b851777ff119160e480d04a6f0e
parentb7760ad742fa754c1f63b6d9465333c629a4b43e
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #10976 from ziglang/x64-macos-fixes

x64: print test runner results on macos

3 files changed, 70 insertions(+), 53 deletions(-)

lib/std/special/test_runner.zig+1-1
......@@ -143,7 +143,7 @@ pub fn main2() anyerror!void {
143143 }
144144 if (builtin.zig_backend == .stage2_llvm or
145145 builtin.zig_backend == .stage2_wasm or
146 (builtin.zig_backend == .stage2_x86_64 and builtin.os.tag != .macos))
146 builtin.zig_backend == .stage2_x86_64)
147147 {
148148 const passed = builtin.test_functions.len - skipped - failed;
149149 const stderr = std.io.getStdErr();
src/arch/x86_64/CodeGen.zig+8-5
......@@ -49,7 +49,7 @@ arg_index: u32,
4949src_loc: Module.SrcLoc,
5050stack_align: u32,
5151
52ret_backpatch: ?Mir.Inst.Index = null,
52ret_backpatches: std.ArrayListUnmanaged(Mir.Inst.Index) = .{},
5353compare_flags_inst: ?Air.Inst.Index = null,
5454
5555/// MIR Instructions
......@@ -310,6 +310,7 @@ pub fn generate(
310310 std.AutoHashMap(Mir.Inst.Index, Air.Inst.Index).init(bin_file.allocator)
311311 else {},
312312 };
313 defer function.ret_backpatches.deinit(bin_file.allocator);
313314 defer function.stack.deinit(bin_file.allocator);
314315 defer function.blocks.deinit(bin_file.allocator);
315316 defer function.exitlude_jump_relocs.deinit(bin_file.allocator);
......@@ -473,8 +474,8 @@ fn gen(self: *Self) InnerError!void {
473474 };
474475 inline for (callee_preserved_regs) |reg, i| {
475476 if (self.register_manager.isRegAllocated(reg)) {
476 if (self.ret_backpatch) |inst| {
477 if (reg.to64() == .rdi) {
477 if (reg.to64() == .rdi) {
478 for (self.ret_backpatches.items) |inst| {
478479 const ops = Mir.Ops.decode(self.mir_instructions.items(.ops)[inst]);
479480 self.mir_instructions.set(inst, Mir.Inst{
480481 .tag = .mov,
......@@ -3312,7 +3313,7 @@ fn airRet(self: *Self, inst: Air.Inst.Index) !void {
33123313 self.register_manager.freezeRegs(&.{ .rax, .rcx, .rdi });
33133314 defer self.register_manager.unfreezeRegs(&.{ .rax, .rcx, .rdi });
33143315 const reg = try self.register_manager.allocReg(null);
3315 self.ret_backpatch = try self.addInst(.{
3316 const backpatch = try self.addInst(.{
33163317 .tag = .mov,
33173318 .ops = (Mir.Ops{
33183319 .reg1 = reg,
......@@ -3320,6 +3321,7 @@ fn airRet(self: *Self, inst: Air.Inst.Index) !void {
33203321 }).encode(),
33213322 .data = undefined,
33223323 });
3324 try self.ret_backpatches.append(self.gpa, backpatch);
33233325 try self.genSetStack(ret_ty, 0, operand, .{
33243326 .source_stack_base = .rbp,
33253327 .dest_stack_base = reg,
......@@ -3354,7 +3356,7 @@ fn airRetLoad(self: *Self, inst: Air.Inst.Index) !void {
33543356 self.register_manager.freezeRegs(&.{ .rax, .rcx, .rdi });
33553357 defer self.register_manager.unfreezeRegs(&.{ .rax, .rcx, .rdi });
33563358 const reg = try self.register_manager.allocReg(null);
3357 self.ret_backpatch = try self.addInst(.{
3359 const backpatch = try self.addInst(.{
33583360 .tag = .mov,
33593361 .ops = (Mir.Ops{
33603362 .reg1 = reg,
......@@ -3362,6 +3364,7 @@ fn airRetLoad(self: *Self, inst: Air.Inst.Index) !void {
33623364 }).encode(),
33633365 .data = undefined,
33643366 });
3367 try self.ret_backpatches.append(self.gpa, backpatch);
33653368 try self.genInlineMemcpy(0, elem_ty, ptr, .{
33663369 .source_stack_base = .rbp,
33673370 .dest_stack_base = reg,
src/link/MachO.zig+61-47
......@@ -3047,6 +3047,9 @@ fn createMhExecuteHeaderAtom(self: *MachO) !void {
30473047 .seg = self.text_segment_cmd_index.?,
30483048 .sect = self.text_section_index.?,
30493049 };
3050 const seg = self.load_commands.items[match.seg].segment;
3051 const sect = seg.sections.items[match.sect];
3052
30503053 const n_strx = try self.makeString("__mh_execute_header");
30513054 const local_sym_index = @intCast(u32, self.locals.items.len);
30523055 var nlist = macho.nlist_64{
......@@ -3054,29 +3057,42 @@ fn createMhExecuteHeaderAtom(self: *MachO) !void {
30543057 .n_type = macho.N_SECT,
30553058 .n_sect = @intCast(u8, self.section_ordinals.getIndex(match).? + 1),
30563059 .n_desc = 0,
3057 .n_value = 0,
3060 .n_value = sect.addr,
30583061 };
30593062 try self.locals.append(self.base.allocator, nlist);
3063 self.mh_execute_header_index = local_sym_index;
30603064
3061 nlist.n_type |= macho.N_EXT;
3062 const global_sym_index = @intCast(u32, self.globals.items.len);
3063 try self.globals.append(self.base.allocator, nlist);
3064 try self.symbol_resolver.putNoClobber(self.base.allocator, n_strx, .{
3065 .where = .global,
3066 .where_index = global_sym_index,
3067 .local_sym_index = local_sym_index,
3068 .file = null,
3069 });
3065 if (self.symbol_resolver.getPtr(n_strx)) |resolv| {
3066 const global = &self.globals.items[resolv.where_index];
3067 if (!(global.weakDef() or !global.pext())) {
3068 log.err("symbol '__mh_execute_header' defined multiple times", .{});
3069 return error.MultipleSymbolDefinitions;
3070 }
3071 resolv.local_sym_index = local_sym_index;
3072 } else {
3073 const global_sym_index = @intCast(u32, self.globals.items.len);
3074 nlist.n_type |= macho.N_EXT;
3075 try self.globals.append(self.base.allocator, nlist);
3076 try self.symbol_resolver.putNoClobber(self.base.allocator, n_strx, .{
3077 .where = .global,
3078 .where_index = global_sym_index,
3079 .local_sym_index = local_sym_index,
3080 .file = null,
3081 });
3082 }
30703083
3084 // We always set the __mh_execute_header to point to the beginning of the __TEXT,__text section
30713085 const atom = try self.createEmptyAtom(local_sym_index, 0, 0);
3072
3073 if (self.needs_prealloc) {
3074 const sym = &self.locals.items[local_sym_index];
3075 const vaddr = try self.allocateAtom(atom, 0, 1, match);
3076 sym.n_value = vaddr;
3077 } else try self.addAtomToSection(atom, match);
3078
3079 self.mh_execute_header_index = local_sym_index;
3086 if (self.atoms.get(match)) |last| {
3087 var first = last;
3088 while (first.prev) |prev| {
3089 first = prev;
3090 }
3091 atom.next = first;
3092 first.prev = atom;
3093 } else {
3094 try self.atoms.putNoClobber(self.base.allocator, match, atom);
3095 }
30803096}
30813097
30823098fn resolveDyldStubBinder(self: *MachO) !void {
......@@ -3747,9 +3763,12 @@ pub fn lowerUnnamedConst(self: *MachO, typed_value: TypedValue, decl: *Module.De
37473763 }
37483764 const unnamed_consts = gop.value_ptr;
37493765
3766 const decl_name = try decl.getFullyQualifiedName(self.base.allocator);
3767 defer self.base.allocator.free(decl_name);
3768
37503769 const name_str_index = blk: {
37513770 const index = unnamed_consts.items.len;
3752 const name = try std.fmt.allocPrint(self.base.allocator, "__unnamed_{s}_{d}", .{ decl.name, index });
3771 const name = try std.fmt.allocPrint(self.base.allocator, "__unnamed_{s}_{d}", .{ decl_name, index });
37533772 defer self.base.allocator.free(name);
37543773 break :blk try self.makeString(name);
37553774 };
......@@ -4041,14 +4060,17 @@ fn placeDecl(self: *MachO, decl: *Module.Decl, code_len: usize) !*macho.nlist_64
40414060 decl_ptr.* = try self.getMatchingSectionAtom(&decl.link.macho, decl.ty, decl.val);
40424061 }
40434062 const match = decl_ptr.*.?;
4063 const sym_name = try decl.getFullyQualifiedName(self.base.allocator);
4064 defer self.base.allocator.free(sym_name);
40444065
40454066 if (decl.link.macho.size != 0) {
40464067 const capacity = decl.link.macho.capacity(self.*);
40474068 const need_realloc = code_len > capacity or !mem.isAlignedGeneric(u64, symbol.n_value, required_alignment);
4069
40484070 if (need_realloc) {
40494071 const vaddr = try self.growAtom(&decl.link.macho, code_len, required_alignment, match);
40504072
4051 log.debug("growing {s} and moving from 0x{x} to 0x{x}", .{ decl.name, symbol.n_value, vaddr });
4073 log.debug("growing {s} and moving from 0x{x} to 0x{x}", .{ sym_name, symbol.n_value, vaddr });
40524074
40534075 if (vaddr != symbol.n_value) {
40544076 log.debug(" (writing new GOT entry)", .{});
......@@ -4074,25 +4096,15 @@ fn placeDecl(self: *MachO, decl: *Module.Decl, code_len: usize) !*macho.nlist_64
40744096 decl.link.macho.size = code_len;
40754097 decl.link.macho.dirty = true;
40764098
4077 const new_name = try std.fmt.allocPrint(self.base.allocator, "_{s}", .{
4078 mem.sliceTo(decl.name, 0),
4079 });
4080 defer self.base.allocator.free(new_name);
4081
4082 symbol.n_strx = try self.makeString(new_name);
4099 symbol.n_strx = try self.makeString(sym_name);
40834100 symbol.n_type = macho.N_SECT;
40844101 symbol.n_sect = @intCast(u8, self.text_section_index.?) + 1;
40854102 symbol.n_desc = 0;
40864103 } else {
4087 const decl_name = try std.fmt.allocPrint(self.base.allocator, "_{s}", .{
4088 mem.sliceTo(decl.name, 0),
4089 });
4090 defer self.base.allocator.free(decl_name);
4091
4092 const name_str_index = try self.makeString(decl_name);
4104 const name_str_index = try self.makeString(sym_name);
40934105 const addr = try self.allocateAtom(&decl.link.macho, code_len, required_alignment, match);
40944106
4095 log.debug("allocated atom for {s} at 0x{x}", .{ decl_name, addr });
4107 log.debug("allocated atom for {s} at 0x{x}", .{ sym_name, addr });
40964108
40974109 errdefer self.freeAtom(&decl.link.macho, match, false);
40984110
......@@ -6675,17 +6687,17 @@ fn snapshotState(self: *MachO) !void {
66756687fn logSymtab(self: MachO) void {
66766688 log.debug("locals:", .{});
66776689 for (self.locals.items) |sym, id| {
6678 log.debug(" {d}: {s}: {}", .{ id, self.getString(sym.n_strx), sym });
6690 log.debug(" {d}: {s}: @{x} in {d}", .{ id, self.getString(sym.n_strx), sym.n_value, sym.n_sect });
66796691 }
66806692
66816693 log.debug("globals:", .{});
66826694 for (self.globals.items) |sym, id| {
6683 log.debug(" {d}: {s}: {}", .{ id, self.getString(sym.n_strx), sym });
6695 log.debug(" {d}: {s}: @{x} in {d}", .{ id, self.getString(sym.n_strx), sym.n_value, sym.n_sect });
66846696 }
66856697
66866698 log.debug("undefs:", .{});
66876699 for (self.undefs.items) |sym, id| {
6688 log.debug(" {d}: {s}: {}", .{ id, self.getString(sym.n_strx), sym });
6700 log.debug(" {d}: {s}: in {d}", .{ id, self.getString(sym.n_strx), sym.n_desc });
66896701 }
66906702
66916703 {
......@@ -6700,26 +6712,28 @@ fn logSymtab(self: MachO) void {
67006712 for (self.got_entries_table.values()) |value| {
67016713 const key = self.got_entries.items[value].target;
67026714 const atom = self.got_entries.items[value].atom;
6715 const n_value = self.locals.items[atom.local_sym_index].n_value;
67036716 switch (key) {
6704 .local => {
6705 const sym = self.locals.items[atom.local_sym_index];
6706 log.debug(" {} => {s}", .{ key, self.getString(sym.n_strx) });
6707 },
6708 .global => |n_strx| log.debug(" {} => {s}", .{ key, self.getString(n_strx) }),
6717 .local => |ndx| log.debug(" {d}: @{x}", .{ ndx, n_value }),
6718 .global => |n_strx| log.debug(" {s}: @{x}", .{ self.getString(n_strx), n_value }),
67096719 }
67106720 }
67116721
67126722 log.debug("__thread_ptrs entries:", .{});
6713 for (self.tlv_ptr_entries_table.keys()) |key| {
6714 switch (key) {
6715 .local => unreachable,
6716 .global => |n_strx| log.debug(" {} => {s}", .{ key, self.getString(n_strx) }),
6717 }
6723 for (self.tlv_ptr_entries_table.values()) |value| {
6724 const key = self.tlv_ptr_entries.items[value].target;
6725 const atom = self.tlv_ptr_entries.items[value].atom;
6726 const n_value = self.locals.items[atom.local_sym_index].n_value;
6727 assert(key == .global);
6728 log.debug(" {s}: @{x}", .{ self.getString(key.global), n_value });
67186729 }
67196730
67206731 log.debug("stubs:", .{});
67216732 for (self.stubs_table.keys()) |key| {
6722 log.debug(" {} => {s}", .{ key, self.getString(key) });
6733 const value = self.stubs_table.get(key).?;
6734 const atom = self.stubs.items[value];
6735 const sym = self.locals.items[atom.local_sym_index];
6736 log.debug(" {s}: @{x}", .{ self.getString(key), sym.n_value });
67236737 }
67246738}
67256739