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...@@ -362,8 +362,8 @@ pub fn openPath(allocator: *Allocator, sub_path: []const u8, options: link.Optio
362362
363 self.base.file = file;363 self.base.file = file;
364364
365 // Create dSYM bundle.
366 if (!options.strip and options.module != null) {365 if (!options.strip and options.module != null) {
366 // Create dSYM bundle.
367 const dir = options.module.?.zig_cache_artifact_directory;367 const dir = options.module.?.zig_cache_artifact_directory;
368 log.debug("creating {s}.dSYM bundle in {s}", .{ sub_path, dir.path });368 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 {...@@ -1228,7 +1228,11 @@ pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void {
1228 self.shrinkTextBlock(&decl.link.macho, code.len);1228 self.shrinkTextBlock(&decl.link.macho, code.len);
1229 }1229 }
1230 decl.link.macho.size = code.len;1230 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);
1232 symbol.n_type = macho.N_SECT;1236 symbol.n_type = macho.N_SECT;
1233 symbol.n_sect = @intCast(u8, self.text_section_index.?) + 1;1237 symbol.n_sect = @intCast(u8, self.text_section_index.?) + 1;
1234 symbol.n_desc = 0;1238 symbol.n_desc = 0;
...@@ -1237,7 +1241,9 @@ pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void {...@@ -1237,7 +1241,9 @@ pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void {
1237 if (self.d_sym) |*ds|1241 if (self.d_sym) |*ds|
1238 try ds.writeLocalSymbol(decl.link.macho.local_sym_index);1242 try ds.writeLocalSymbol(decl.link.macho.local_sym_index);
1239 } else {1243 } 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
1241 const name_str_index = try self.makeString(decl_name);1247 const name_str_index = try self.makeString(decl_name);
1242 const addr = try self.allocateTextBlock(&decl.link.macho, code.len, required_alignment);1248 const addr = try self.allocateTextBlock(&decl.link.macho, code.len, required_alignment);
12431249
...@@ -1376,6 +1382,9 @@ pub fn updateDeclExports(...@@ -1376,6 +1382,9 @@ pub fn updateDeclExports(
1376 const decl_sym = &self.locals.items[decl.link.macho.local_sym_index];1382 const decl_sym = &self.locals.items[decl.link.macho.local_sym_index];
13771383
1378 for (exports) |exp| {1384 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
1379 if (exp.options.section) |section_name| {1388 if (exp.options.section) |section_name| {
1380 if (!mem.eql(u8, section_name, "__text")) {1389 if (!mem.eql(u8, section_name, "__text")) {
1381 try module.failed_exports.ensureCapacity(module.gpa, module.failed_exports.items().len + 1);1390 try module.failed_exports.ensureCapacity(module.gpa, module.failed_exports.items().len + 1);
...@@ -1403,7 +1412,7 @@ pub fn updateDeclExports(...@@ -1403,7 +1412,7 @@ pub fn updateDeclExports(
1403 // Otherwise, don't do anything since we already have all the flags1412 // Otherwise, don't do anything since we already have all the flags
1404 // set that we need for global (strong) linkage.1413 // set that we need for global (strong) linkage.
1405 // n_type == N_SECT | N_EXT1414 // n_type == N_SECT | N_EXT
1406 if (mem.eql(u8, exp.options.name, "_main")) {1415 if (mem.eql(u8, exp_name, "_main")) {
1407 self.entry_addr = decl_sym.n_value;1416 self.entry_addr = decl_sym.n_value;
1408 }1417 }
1409 },1418 },
...@@ -1425,14 +1434,14 @@ pub fn updateDeclExports(...@@ -1425,14 +1434,14 @@ pub fn updateDeclExports(
1425 if (exp.link.macho.sym_index) |i| {1434 if (exp.link.macho.sym_index) |i| {
1426 const sym = &self.globals.items[i];1435 const sym = &self.globals.items[i];
1427 sym.* = .{1436 sym.* = .{
1428 .n_strx = try self.updateString(sym.n_strx, exp.options.name),1437 .n_strx = try self.updateString(sym.n_strx, exp_name),
1429 .n_type = n_type,1438 .n_type = n_type,
1430 .n_sect = @intCast(u8, self.text_section_index.?) + 1,1439 .n_sect = @intCast(u8, self.text_section_index.?) + 1,
1431 .n_desc = n_desc,1440 .n_desc = n_desc,
1432 .n_value = decl_sym.n_value,1441 .n_value = decl_sym.n_value,
1433 };1442 };
1434 } else {1443 } else {
1435 const name_str_index = try self.makeString(exp.options.name);1444 const name_str_index = try self.makeString(exp_name);
1436 const i = if (self.globals_free_list.popOrNull()) |i| i else blk: {1445 const i = if (self.globals_free_list.popOrNull()) |i| i else blk: {
1437 _ = self.globals.addOneAssumeCapacity();1446 _ = self.globals.addOneAssumeCapacity();
1438 self.export_info_dirty = true;1447 self.export_info_dirty = true;
...@@ -2235,9 +2244,12 @@ fn makeString(self: *MachO, bytes: []const u8) !u32 {...@@ -2235,9 +2244,12 @@ fn makeString(self: *MachO, bytes: []const u8) !u32 {
22352244
2236 try self.string_table.ensureCapacity(self.base.allocator, self.string_table.items.len + bytes.len + 1);2245 try self.string_table.ensureCapacity(self.base.allocator, self.string_table.items.len + bytes.len + 1);
2237 const offset = @intCast(u32, self.string_table.items.len);2246 const offset = @intCast(u32, self.string_table.items.len);
2247
2238 log.debug("writing new string '{s}' into string table at offset 0x{x}", .{ bytes, offset });2248 log.debug("writing new string '{s}' into string table at offset 0x{x}", .{ bytes, offset });
2249
2239 self.string_table.appendSliceAssumeCapacity(bytes);2250 self.string_table.appendSliceAssumeCapacity(bytes);
2240 self.string_table.appendAssumeCapacity(0);2251 self.string_table.appendAssumeCapacity(0);
2252
2241 try self.string_table_directory.putNoClobber(2253 try self.string_table_directory.putNoClobber(
2242 self.base.allocator,2254 self.base.allocator,
2243 try self.base.allocator.dupe(u8, bytes),2255 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...@@ -534,8 +534,8 @@ pub fn flushModule(self: *DebugSymbols, allocator: *Allocator, options: link.Opt
534 mem.writeIntLittle(u64, di_buf.addManyAsArrayAssumeCapacity(8), text_section.size);534 mem.writeIntLittle(u64, di_buf.addManyAsArrayAssumeCapacity(8), text_section.size);
535535
536 // Sentinel.536 // Sentinel.
537 mem.writeIntLittle(u32, di_buf.addManyAsArrayAssumeCapacity(4), 0);537 mem.writeIntLittle(u64, di_buf.addManyAsArrayAssumeCapacity(8), 0);
538 mem.writeIntLittle(u32, di_buf.addManyAsArrayAssumeCapacity(4), 0);538 mem.writeIntLittle(u64, di_buf.addManyAsArrayAssumeCapacity(8), 0);
539539
540 // Go back and populate the initial length.540 // Go back and populate the initial length.
541 const init_len = di_buf.items.len - after_init_len;541 const init_len = di_buf.items.len - after_init_len;
...@@ -1056,6 +1056,32 @@ pub fn commitDeclDebugInfo(...@@ -1056,6 +1056,32 @@ pub fn commitDeclDebugInfo(
1056 mem.writeIntLittle(u32, ptr, @intCast(u32, text_block.size));1056 mem.writeIntLittle(u32, ptr, @intCast(u32, text_block.size));
1057 }1057 }
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
1059 try dbg_line_buffer.appendSlice(&[_]u8{ DW.LNS_extended_op, 1, DW.LNE_end_sequence });1085 try dbg_line_buffer.appendSlice(&[_]u8{ DW.LNS_extended_op, 1, DW.LNE_end_sequence });
10601086
1061 // Now we have the full contents and may allocate a region to store it.1087 // 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 {...@@ -77,7 +77,7 @@ pub fn addCases(ctx: *TestContext) !void {
77 \\extern "c" fn write(usize, usize, usize) usize;77 \\extern "c" fn write(usize, usize, usize) usize;
78 \\extern "c" fn exit(usize) noreturn;78 \\extern "c" fn exit(usize) noreturn;
79 \\79 \\
80 \\export fn _main() noreturn {80 \\export fn main() noreturn {
81 \\ print();81 \\ print();
82 \\82 \\
83 \\ exit(0);83 \\ exit(0);