authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-16 00:00:36-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-16 00:00:36-07:00
logae04ec776d87eb6312b9172fc494f1ab25c1c52e
tree1477fdec01060afbba919424d3d11cf327f0a9a6
parent9a95478c62a93b969e719000d3550fc04a2d7b3c
parent6461b9516371d22d347c27b5fcd2e8c22fafd03d

Merge remote-tracking branch 'origin/master' into stage2-whole-file-astgen

Need that _main -> main improvement

3 files changed, 47 insertions(+), 9 deletions(-)

src/link/MachO.zig+18-6
......@@ -362,8 +362,8 @@ pub fn openPath(allocator: *Allocator, sub_path: []const u8, options: link.Optio
362362
363363 self.base.file = file;
364364
365 // Create dSYM bundle.
366365 if (!options.strip and options.module != null) {
366 // Create dSYM bundle.
367367 const dir = options.module.?.zig_cache_artifact_directory;
368368 log.debug("creating {s}.dSYM bundle in {s}", .{ sub_path, dir.path });
369369
......@@ -1228,7 +1228,11 @@ pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void {
12281228 self.shrinkTextBlock(&decl.link.macho, code.len);
12291229 }
12301230 decl.link.macho.size = code.len;
1231 symbol.n_strx = try self.updateString(symbol.n_strx, mem.spanZ(decl.name));
1231
1232 const new_name = try std.fmt.allocPrint(self.base.allocator, "_{s}", .{mem.spanZ(decl.name)});
1233 defer self.base.allocator.free(new_name);
1234
1235 symbol.n_strx = try self.updateString(symbol.n_strx, new_name);
12321236 symbol.n_type = macho.N_SECT;
12331237 symbol.n_sect = @intCast(u8, self.text_section_index.?) + 1;
12341238 symbol.n_desc = 0;
......@@ -1237,7 +1241,9 @@ pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void {
12371241 if (self.d_sym) |*ds|
12381242 try ds.writeLocalSymbol(decl.link.macho.local_sym_index);
12391243 } else {
1240 const decl_name = mem.spanZ(decl.name);
1244 const decl_name = try std.fmt.allocPrint(self.base.allocator, "_{s}", .{mem.spanZ(decl.name)});
1245 defer self.base.allocator.free(decl_name);
1246
12411247 const name_str_index = try self.makeString(decl_name);
12421248 const addr = try self.allocateTextBlock(&decl.link.macho, code.len, required_alignment);
12431249
......@@ -1376,6 +1382,9 @@ pub fn updateDeclExports(
13761382 const decl_sym = &self.locals.items[decl.link.macho.local_sym_index];
13771383
13781384 for (exports) |exp| {
1385 const exp_name = try std.fmt.allocPrint(self.base.allocator, "_{s}", .{exp.options.name});
1386 defer self.base.allocator.free(exp_name);
1387
13791388 if (exp.options.section) |section_name| {
13801389 if (!mem.eql(u8, section_name, "__text")) {
13811390 try module.failed_exports.ensureCapacity(module.gpa, module.failed_exports.items().len + 1);
......@@ -1403,7 +1412,7 @@ pub fn updateDeclExports(
14031412 // Otherwise, don't do anything since we already have all the flags
14041413 // set that we need for global (strong) linkage.
14051414 // n_type == N_SECT | N_EXT
1406 if (mem.eql(u8, exp.options.name, "_main")) {
1415 if (mem.eql(u8, exp_name, "_main")) {
14071416 self.entry_addr = decl_sym.n_value;
14081417 }
14091418 },
......@@ -1425,14 +1434,14 @@ pub fn updateDeclExports(
14251434 if (exp.link.macho.sym_index) |i| {
14261435 const sym = &self.globals.items[i];
14271436 sym.* = .{
1428 .n_strx = try self.updateString(sym.n_strx, exp.options.name),
1437 .n_strx = try self.updateString(sym.n_strx, exp_name),
14291438 .n_type = n_type,
14301439 .n_sect = @intCast(u8, self.text_section_index.?) + 1,
14311440 .n_desc = n_desc,
14321441 .n_value = decl_sym.n_value,
14331442 };
14341443 } else {
1435 const name_str_index = try self.makeString(exp.options.name);
1444 const name_str_index = try self.makeString(exp_name);
14361445 const i = if (self.globals_free_list.popOrNull()) |i| i else blk: {
14371446 _ = self.globals.addOneAssumeCapacity();
14381447 self.export_info_dirty = true;
......@@ -2235,9 +2244,12 @@ fn makeString(self: *MachO, bytes: []const u8) !u32 {
22352244
22362245 try self.string_table.ensureCapacity(self.base.allocator, self.string_table.items.len + bytes.len + 1);
22372246 const offset = @intCast(u32, self.string_table.items.len);
2247
22382248 log.debug("writing new string '{s}' into string table at offset 0x{x}", .{ bytes, offset });
2249
22392250 self.string_table.appendSliceAssumeCapacity(bytes);
22402251 self.string_table.appendAssumeCapacity(0);
2252
22412253 try self.string_table_directory.putNoClobber(
22422254 self.base.allocator,
22432255 try self.base.allocator.dupe(u8, bytes),
src/link/MachO/DebugSymbols.zig+28-2
......@@ -534,8 +534,8 @@ pub fn flushModule(self: *DebugSymbols, allocator: *Allocator, options: link.Opt
534534 mem.writeIntLittle(u64, di_buf.addManyAsArrayAssumeCapacity(8), text_section.size);
535535
536536 // Sentinel.
537 mem.writeIntLittle(u32, di_buf.addManyAsArrayAssumeCapacity(4), 0);
538 mem.writeIntLittle(u32, di_buf.addManyAsArrayAssumeCapacity(4), 0);
537 mem.writeIntLittle(u64, di_buf.addManyAsArrayAssumeCapacity(8), 0);
538 mem.writeIntLittle(u64, di_buf.addManyAsArrayAssumeCapacity(8), 0);
539539
540540 // Go back and populate the initial length.
541541 const init_len = di_buf.items.len - after_init_len;
......@@ -1056,6 +1056,32 @@ pub fn commitDeclDebugInfo(
10561056 mem.writeIntLittle(u32, ptr, @intCast(u32, text_block.size));
10571057 }
10581058
1059 {
1060 // Advance line and PC.
1061 // TODO encapsulate logic in a helper function.
1062 try dbg_line_buffer.append(DW.LNS_advance_pc);
1063 try leb.writeULEB128(dbg_line_buffer.writer(), text_block.size);
1064
1065 try dbg_line_buffer.append(DW.LNS_advance_line);
1066 const line_off: u28 = blk: {
1067 const tree = decl.container.file_scope.tree;
1068 const node_tags = tree.nodes.items(.tag);
1069 const node_datas = tree.nodes.items(.data);
1070 const token_starts = tree.tokens.items(.start);
1071
1072 // TODO Look into improving the performance here by adding a token-index-to-line
1073 // lookup table. Currently this involves scanning over the source code for newlines.
1074 const fn_decl = decl.src_node;
1075 assert(node_tags[fn_decl] == .fn_decl);
1076 const block = node_datas[fn_decl].rhs;
1077 const lbrace = tree.firstToken(block);
1078 const rbrace = tree.lastToken(block);
1079 const line_delta = std.zig.lineDelta(tree.source, token_starts[lbrace], token_starts[rbrace]);
1080 break :blk @intCast(u28, line_delta);
1081 };
1082 try leb.writeULEB128(dbg_line_buffer.writer(), line_off);
1083 }
1084
10591085 try dbg_line_buffer.appendSlice(&[_]u8{ DW.LNS_extended_op, 1, DW.LNE_end_sequence });
10601086
10611087 // Now we have the full contents and may allocate a region to store it.
test/stage2/darwin.zig+1-1
......@@ -77,7 +77,7 @@ pub fn addCases(ctx: *TestContext) !void {
7777 \\extern "c" fn write(usize, usize, usize) usize;
7878 \\extern "c" fn exit(usize) noreturn;
7979 \\
80 \\export fn _main() noreturn {
80 \\export fn main() noreturn {
8181 \\ print();
8282 \\
8383 \\ exit(0);