authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-05-14 11:46:50+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-05-14 13:08:56+02:00
log00ebbe6df2249ba8201c0e5472d95022bf73e782
tree8a71469cc3f1e2ec94360a9317ebcce576924c9a
parent8eea5eddf773a8d1f9f883e71d00409e454ee0bd

macho: require _main as global export in self-hosted

Clean up type and description flags generation for exports in self-hosted MachO backend.

2 files changed, 32 insertions(+), 15 deletions(-)

src/link/MachO.zig+25-8
......@@ -1369,15 +1369,32 @@ pub fn updateDeclExports(
13691369 continue;
13701370 }
13711371 }
1372 const n_desc = switch (exp.options.linkage) {
1373 .Internal => macho.REFERENCE_FLAG_PRIVATE_DEFINED,
1374 .Strong => blk: {
1375 if (mem.eql(u8, exp.options.name, "_start")) {
1372
1373 var n_type: u8 = macho.N_SECT | macho.N_EXT;
1374 var n_desc: u16 = 0;
1375
1376 switch (exp.options.linkage) {
1377 .Internal => {
1378 // Symbol should be hidden, or in MachO lingo, private extern.
1379 // We should also mark the symbol as Weak: n_desc == N_WEAK_DEF.
1380 // TODO work out when to add N_WEAK_REF.
1381 n_type |= macho.N_PEXT;
1382 n_desc |= macho.N_WEAK_DEF;
1383 },
1384 .Strong => {
1385 // Check if the export is _main, and note if os.
1386 // Otherwise, don't do anything since we already have all the flags
1387 // set that we need for global (strong) linkage.
1388 // n_type == N_SECT | N_EXT
1389 if (mem.eql(u8, exp.options.name, "_main")) {
13761390 self.entry_addr = decl_sym.n_value;
13771391 }
1378 break :blk macho.REFERENCE_FLAG_DEFINED;
13791392 },
1380 .Weak => macho.N_WEAK_REF,
1393 .Weak => {
1394 // Weak linkage is specified as part of n_desc field.
1395 // Symbol's n_type is like for a symbol with strong linkage.
1396 n_desc |= macho.N_WEAK_DEF;
1397 },
13811398 .LinkOnce => {
13821399 try module.failed_exports.ensureCapacity(module.gpa, module.failed_exports.items().len + 1);
13831400 module.failed_exports.putAssumeCapacityNoClobber(
......@@ -1386,8 +1403,8 @@ pub fn updateDeclExports(
13861403 );
13871404 continue;
13881405 },
1389 };
1390 const n_type = decl_sym.n_type | macho.N_EXT;
1406 }
1407
13911408 if (exp.link.macho.sym_index) |i| {
13921409 const sym = &self.globals.items[i];
13931410 sym.* = .{
test/stage2/darwin.zig+7-7
......@@ -17,7 +17,7 @@ pub fn addCases(ctx: *TestContext) !void {
1717
1818 // Incorrect return type
1919 case.addError(
20 \\export fn _start() noreturn {
20 \\export fn _main() noreturn {
2121 \\}
2222 , &[_][]const u8{":2:1: error: expected noreturn, found void"});
2323
......@@ -26,7 +26,7 @@ pub fn addCases(ctx: *TestContext) !void {
2626 \\extern "c" fn write(usize, usize, usize) usize;
2727 \\extern "c" fn exit(usize) noreturn;
2828 \\
29 \\export fn _start() noreturn {
29 \\export fn _main() noreturn {
3030 \\ print();
3131 \\
3232 \\ exit(0);
......@@ -46,7 +46,7 @@ pub fn addCases(ctx: *TestContext) !void {
4646 \\extern "c" fn write(usize, usize, usize) usize;
4747 \\extern "c" fn exit(usize) noreturn;
4848 \\
49 \\export fn _start() noreturn {
49 \\export fn _main() noreturn {
5050 \\ print();
5151 \\ print();
5252 \\ print();
......@@ -73,7 +73,7 @@ pub fn addCases(ctx: *TestContext) !void {
7373 \\extern "c" fn write(usize, usize, usize) usize;
7474 \\extern "c" fn exit(usize) noreturn;
7575 \\
76 \\export fn _start() noreturn {
76 \\export fn _main() noreturn {
7777 \\ print();
7878 \\
7979 \\ exit(0);
......@@ -93,7 +93,7 @@ pub fn addCases(ctx: *TestContext) !void {
9393 \\extern "c" fn write(usize, usize, usize) usize;
9494 \\extern "c" fn exit(usize) noreturn;
9595 \\
96 \\export fn _start() noreturn {
96 \\export fn _main() noreturn {
9797 \\ print();
9898 \\ print();
9999 \\
......@@ -119,7 +119,7 @@ pub fn addCases(ctx: *TestContext) !void {
119119 case.addCompareOutput(
120120 \\extern "c" fn exit(usize) noreturn;
121121 \\
122 \\export fn _start() noreturn {
122 \\export fn _main() noreturn {
123123 \\ exit(0);
124124 \\}
125125 ,
......@@ -130,7 +130,7 @@ pub fn addCases(ctx: *TestContext) !void {
130130 \\extern "c" fn exit(usize) noreturn;
131131 \\extern "c" fn write(usize, usize, usize) usize;
132132 \\
133 \\export fn _start() noreturn {
133 \\export fn _main() noreturn {
134134 \\ _ = write(1, @ptrToInt("Hey!\n"), 5);
135135 \\ exit(0);
136136 \\}