| author | |
| committer | |
| log | 2b84592858fe004fcae1372f7a5f97e10a81c2e3 |
| tree | f0ebc5be3d85396d423e837b023057f6258a194a |
| parent | 5d9fd5bcdeb9f7a1af50b0eaffd22b61f3e440a6 |
18 files changed, 564 insertions(+), 301 deletions(-)
src/arch/x86_64/Emit.zig+3-3| ... | ... | @@ -163,12 +163,12 @@ pub fn emitMir(emit: *Emit) Error!void { |
| 163 | 163 | const zo = macho_file.getZigObject().?; |
| 164 | 164 | const atom = zo.symbols.items[data.atom_index].getAtom(macho_file).?; |
| 165 | 165 | const sym = &zo.symbols.items[data.sym_index]; |
| 166 | if (sym.flags.needs_zig_got and !is_obj_or_static_lib) { | |
| 166 | if (sym.getSectionFlags().needs_zig_got and !is_obj_or_static_lib) { | |
| 167 | 167 | _ = try sym.getOrCreateZigGotEntry(data.sym_index, macho_file); |
| 168 | 168 | } |
| 169 | const @"type": link.File.MachO.Relocation.Type = if (sym.flags.needs_zig_got and !is_obj_or_static_lib) | |
| 169 | const @"type": link.File.MachO.Relocation.Type = if (sym.getSectionFlags().needs_zig_got and !is_obj_or_static_lib) | |
| 170 | 170 | .zig_got_load |
| 171 | else if (sym.flags.needs_got) | |
| 171 | else if (sym.getSectionFlags().needs_got) | |
| 172 | 172 | // TODO: it is possible to emit .got_load here that can potentially be relaxed |
| 173 | 173 | // however this requires always to use a MOVQ mnemonic |
| 174 | 174 | .got |
src/arch/x86_64/Lower.zig+1-1| ... | ... | @@ -451,7 +451,7 @@ fn emit(lower: *Lower, prefix: Prefix, mnemonic: Mnemonic, ops: []const Operand) |
| 451 | 451 | break :op .{ .mem = Memory.rip(mem_op.sib.ptr_size, 0) }; |
| 452 | 452 | }, |
| 453 | 453 | .mov => { |
| 454 | if (is_obj_or_static_lib and macho_sym.flags.needs_zig_got) emit_mnemonic = .lea; | |
| 454 | if (is_obj_or_static_lib and macho_sym.getSectionFlags().needs_zig_got) emit_mnemonic = .lea; | |
| 455 | 455 | break :op .{ .mem = Memory.rip(mem_op.sib.ptr_size, 0) }; |
| 456 | 456 | }, |
| 457 | 457 | else => unreachable, |
src/codegen.zig+1-1| ... | ... | @@ -924,7 +924,7 @@ fn genDeclRef( |
| 924 | 924 | const name = decl.name.toSlice(ip); |
| 925 | 925 | const lib_name = if (decl.getOwnedVariable(zcu)) |ov| ov.lib_name.toSlice(ip) else null; |
| 926 | 926 | const sym_index = try macho_file.getGlobalSymbol(name, lib_name); |
| 927 | zo.symbols.items[sym_index].flags.needs_got = true; | |
| 927 | zo.symbols.items[sym_index].setSectionFlags(.{ .needs_got = true }); | |
| 928 | 928 | return GenResult.mcv(.{ .load_symbol = sym_index }); |
| 929 | 929 | } |
| 930 | 930 | const sym_index = try zo.getOrCreateMetadataForDecl(macho_file, decl_index); |
src/link/MachO.zig+423-180| ... | ... | @@ -25,8 +25,10 @@ sections: std.MultiArrayList(Section) = .{}, |
| 25 | 25 | resolver: SymbolResolver = .{}, |
| 26 | 26 | /// This table will be populated after `scanRelocs` has run. |
| 27 | 27 | /// Key is symbol index. |
| 28 | undefs: std.AutoHashMapUnmanaged(SymbolResolver.Index, std.ArrayListUnmanaged(Ref)) = .{}, | |
| 29 | dupes: std.AutoHashMapUnmanaged(SymbolResolver.Index, std.ArrayListUnmanaged(File.Index)) = .{}, | |
| 28 | undefs: std.AutoArrayHashMapUnmanaged(SymbolResolver.Index, std.ArrayListUnmanaged(Ref)) = .{}, | |
| 29 | undefs_mutex: std.Thread.Mutex = .{}, | |
| 30 | dupes: std.AutoArrayHashMapUnmanaged(SymbolResolver.Index, std.ArrayListUnmanaged(File.Index)) = .{}, | |
| 31 | dupes_mutex: std.Thread.Mutex = .{}, | |
| 30 | 32 | |
| 31 | 33 | dyld_info_cmd: macho.dyld_info_command = .{}, |
| 32 | 34 | symtab_cmd: macho.symtab_command = .{}, |
| ... | ... | @@ -93,9 +95,9 @@ debug_str_sect_index: ?u8 = null, |
| 93 | 95 | debug_aranges_sect_index: ?u8 = null, |
| 94 | 96 | debug_line_sect_index: ?u8 = null, |
| 95 | 97 | |
| 96 | has_tlv: bool = false, | |
| 97 | binds_to_weak: bool = false, | |
| 98 | weak_defines: bool = false, | |
| 98 | has_tlv: AtomicBool = AtomicBool.init(false), | |
| 99 | binds_to_weak: AtomicBool = AtomicBool.init(false), | |
| 100 | weak_defines: AtomicBool = AtomicBool.init(false), | |
| 99 | 101 | has_errors: AtomicBool = AtomicBool.init(false), |
| 100 | 102 | |
| 101 | 103 | /// Options |
| ... | ... | @@ -306,20 +308,15 @@ pub fn deinit(self: *MachO) void { |
| 306 | 308 | self.sections.deinit(gpa); |
| 307 | 309 | |
| 308 | 310 | self.resolver.deinit(gpa); |
| 309 | { | |
| 310 | var it = self.undefs.iterator(); | |
| 311 | while (it.next()) |entry| { | |
| 312 | entry.value_ptr.deinit(gpa); | |
| 313 | } | |
| 314 | self.undefs.deinit(gpa); | |
| 311 | ||
| 312 | for (self.undefs.values()) |*val| { | |
| 313 | val.deinit(gpa); | |
| 315 | 314 | } |
| 316 | { | |
| 317 | var it = self.dupes.iterator(); | |
| 318 | while (it.next()) |entry| { | |
| 319 | entry.value_ptr.deinit(gpa); | |
| 320 | } | |
| 321 | self.dupes.deinit(gpa); | |
| 315 | self.undefs.deinit(gpa); | |
| 316 | for (self.dupes.values()) |*val| { | |
| 317 | val.deinit(gpa); | |
| 322 | 318 | } |
| 319 | self.dupes.deinit(gpa); | |
| 323 | 320 | |
| 324 | 321 | self.symtab.deinit(gpa); |
| 325 | 322 | self.strtab.deinit(gpa); |
| ... | ... | @@ -553,12 +550,7 @@ pub fn flushModule(self: *MachO, arena: Allocator, tid: Zcu.PerThread.Id, prog_n |
| 553 | 550 | else => |e| return e, |
| 554 | 551 | }; |
| 555 | 552 | } |
| 556 | self.writeSectionsAndUpdateLinkeditSizes() catch |err| { | |
| 557 | switch (err) { | |
| 558 | error.ResolveFailed => return error.FlushFailure, | |
| 559 | else => |e| return e, | |
| 560 | } | |
| 561 | }; | |
| 553 | try self.writeSectionsAndUpdateLinkeditSizes(); | |
| 562 | 554 | |
| 563 | 555 | try self.writeSectionsToFile(); |
| 564 | 556 | try self.allocateLinkeditSegment(); |
| ... | ... | @@ -907,25 +899,25 @@ pub fn parseInputFiles(self: *MachO) !void { |
| 907 | 899 | defer wg.wait(); |
| 908 | 900 | |
| 909 | 901 | for (self.objects.items) |index| { |
| 910 | tp.spawnWg(&wg, parseInputFileWorker, .{ self, index }); | |
| 902 | tp.spawnWg(&wg, parseInputFileWorker, .{ self, self.getFile(index).? }); | |
| 911 | 903 | } |
| 912 | 904 | for (self.dylibs.items) |index| { |
| 913 | tp.spawnWg(&wg, parseInputFileWorker, .{ self, index }); | |
| 905 | tp.spawnWg(&wg, parseInputFileWorker, .{ self, self.getFile(index).? }); | |
| 914 | 906 | } |
| 915 | 907 | } |
| 916 | 908 | |
| 917 | 909 | if (self.has_errors.swap(false, .seq_cst)) return error.FlushFailure; |
| 918 | 910 | } |
| 919 | 911 | |
| 920 | fn parseInputFileWorker(self: *MachO, index: File.Index) void { | |
| 921 | self.getFile(index).?.parse(self) catch |err| { | |
| 912 | fn parseInputFileWorker(self: *MachO, file: File) void { | |
| 913 | file.parse(self) catch |err| { | |
| 922 | 914 | switch (err) { |
| 923 | 915 | error.MalformedObject, |
| 924 | 916 | error.MalformedDylib, |
| 925 | 917 | error.InvalidCpuArch, |
| 926 | 918 | error.InvalidTarget, |
| 927 | 919 | => {}, // already reported |
| 928 | else => |e| self.reportParseError2(index, "unexpected error: parsing input file failed with error {s}", .{@errorName(e)}) catch {}, | |
| 920 | else => |e| self.reportParseError2(file.getIndex(), "unexpected error: parsing input file failed with error {s}", .{@errorName(e)}) catch {}, | |
| 929 | 921 | } |
| 930 | 922 | _ = self.has_errors.swap(true, .seq_cst); |
| 931 | 923 | }; |
| ... | ... | @@ -1286,13 +1278,50 @@ fn markLive(self: *MachO) void { |
| 1286 | 1278 | } |
| 1287 | 1279 | |
| 1288 | 1280 | fn convertTentativeDefsAndResolveSpecialSymbols(self: *MachO) !void { |
| 1289 | for (self.objects.items) |index| { | |
| 1290 | try self.getFile(index).?.object.convertTentativeDefinitions(self); | |
| 1291 | } | |
| 1292 | if (self.getInternalObject()) |obj| { | |
| 1293 | try obj.resolveBoundarySymbols(self); | |
| 1294 | try obj.resolveObjcMsgSendSymbols(self); | |
| 1281 | const tp = self.base.comp.thread_pool; | |
| 1282 | var wg: WaitGroup = .{}; | |
| 1283 | { | |
| 1284 | wg.reset(); | |
| 1285 | defer wg.wait(); | |
| 1286 | for (self.objects.items) |index| { | |
| 1287 | tp.spawnWg(&wg, convertTentativeDefinitionsWorker, .{ self, self.getFile(index).?.object }); | |
| 1288 | } | |
| 1289 | if (self.getInternalObject()) |obj| { | |
| 1290 | tp.spawnWg(&wg, resolveSpecialSymbolsWorker, .{ self, obj }); | |
| 1291 | } | |
| 1295 | 1292 | } |
| 1293 | if (self.has_errors.swap(false, .seq_cst)) return error.FlushFailure; | |
| 1294 | } | |
| 1295 | ||
| 1296 | fn convertTentativeDefinitionsWorker(self: *MachO, object: *Object) void { | |
| 1297 | const tracy = trace(@src()); | |
| 1298 | defer tracy.end(); | |
| 1299 | object.convertTentativeDefinitions(self) catch |err| { | |
| 1300 | self.reportParseError2( | |
| 1301 | object.index, | |
| 1302 | "unexpected error occurred while converting tentative symbols into defined symbols: {s}", | |
| 1303 | .{@errorName(err)}, | |
| 1304 | ) catch {}; | |
| 1305 | _ = self.has_errors.swap(true, .seq_cst); | |
| 1306 | }; | |
| 1307 | } | |
| 1308 | ||
| 1309 | fn resolveSpecialSymbolsWorker(self: *MachO, obj: *InternalObject) void { | |
| 1310 | const tracy = trace(@src()); | |
| 1311 | defer tracy.end(); | |
| 1312 | obj.resolveBoundarySymbols(self) catch |err| { | |
| 1313 | self.reportUnexpectedError("unexpected error occurred while resolving boundary symbols: {s}", .{ | |
| 1314 | @errorName(err), | |
| 1315 | }) catch {}; | |
| 1316 | _ = self.has_errors.swap(true, .seq_cst); | |
| 1317 | return; | |
| 1318 | }; | |
| 1319 | obj.resolveObjcMsgSendSymbols(self) catch |err| { | |
| 1320 | self.reportUnexpectedError("unexpected error occurred while resolving ObjC msgsend stubs: {s}", .{ | |
| 1321 | @errorName(err), | |
| 1322 | }) catch {}; | |
| 1323 | _ = self.has_errors.swap(true, .seq_cst); | |
| 1324 | }; | |
| 1296 | 1325 | } |
| 1297 | 1326 | |
| 1298 | 1327 | pub fn dedupLiterals(self: *MachO) !void { |
| ... | ... | @@ -1313,14 +1342,20 @@ pub fn dedupLiterals(self: *MachO) !void { |
| 1313 | 1342 | try object.resolveLiterals(&lp, self); |
| 1314 | 1343 | } |
| 1315 | 1344 | |
| 1316 | if (self.getZigObject()) |zo| { | |
| 1317 | zo.dedupLiterals(lp, self); | |
| 1318 | } | |
| 1319 | for (self.objects.items) |index| { | |
| 1320 | self.getFile(index).?.object.dedupLiterals(lp, self); | |
| 1321 | } | |
| 1322 | if (self.getInternalObject()) |object| { | |
| 1323 | object.dedupLiterals(lp, self); | |
| 1345 | const tp = self.base.comp.thread_pool; | |
| 1346 | var wg: WaitGroup = .{}; | |
| 1347 | { | |
| 1348 | wg.reset(); | |
| 1349 | defer wg.wait(); | |
| 1350 | if (self.getZigObject()) |zo| { | |
| 1351 | tp.spawnWg(&wg, File.dedupLiterals, .{ zo.asFile(), lp, self }); | |
| 1352 | } | |
| 1353 | for (self.objects.items) |index| { | |
| 1354 | tp.spawnWg(&wg, File.dedupLiterals, .{ self.getFile(index).?, lp, self }); | |
| 1355 | } | |
| 1356 | if (self.getInternalObject()) |object| { | |
| 1357 | tp.spawnWg(&wg, File.dedupLiterals, .{ object.asFile(), lp, self }); | |
| 1358 | } | |
| 1324 | 1359 | } |
| 1325 | 1360 | } |
| 1326 | 1361 | |
| ... | ... | @@ -1334,18 +1369,41 @@ fn claimUnresolved(self: *MachO) void { |
| 1334 | 1369 | } |
| 1335 | 1370 | |
| 1336 | 1371 | fn checkDuplicates(self: *MachO) !void { |
| 1337 | if (self.getZigObject()) |zo| { | |
| 1338 | try zo.asFile().checkDuplicates(self); | |
| 1339 | } | |
| 1340 | for (self.objects.items) |index| { | |
| 1341 | try self.getFile(index).?.checkDuplicates(self); | |
| 1342 | } | |
| 1343 | if (self.getInternalObject()) |obj| { | |
| 1344 | try obj.asFile().checkDuplicates(self); | |
| 1372 | const tracy = trace(@src()); | |
| 1373 | defer tracy.end(); | |
| 1374 | ||
| 1375 | const tp = self.base.comp.thread_pool; | |
| 1376 | var wg: WaitGroup = .{}; | |
| 1377 | { | |
| 1378 | wg.reset(); | |
| 1379 | defer wg.wait(); | |
| 1380 | if (self.getZigObject()) |zo| { | |
| 1381 | tp.spawnWg(&wg, checkDuplicatesWorker, .{ self, zo.asFile() }); | |
| 1382 | } | |
| 1383 | for (self.objects.items) |index| { | |
| 1384 | tp.spawnWg(&wg, checkDuplicatesWorker, .{ self, self.getFile(index).? }); | |
| 1385 | } | |
| 1386 | if (self.getInternalObject()) |obj| { | |
| 1387 | tp.spawnWg(&wg, checkDuplicatesWorker, .{ self, obj.asFile() }); | |
| 1388 | } | |
| 1345 | 1389 | } |
| 1390 | ||
| 1391 | if (self.has_errors.swap(false, .seq_cst)) return error.FlushFailure; | |
| 1392 | ||
| 1346 | 1393 | try self.reportDuplicates(); |
| 1347 | 1394 | } |
| 1348 | 1395 | |
| 1396 | fn checkDuplicatesWorker(self: *MachO, file: File) void { | |
| 1397 | const tracy = trace(@src()); | |
| 1398 | defer tracy.end(); | |
| 1399 | file.checkDuplicates(self) catch |err| { | |
| 1400 | self.reportParseError2(file.getIndex(), "failed to check for duplicate definitions: {s}", .{ | |
| 1401 | @errorName(err), | |
| 1402 | }) catch {}; | |
| 1403 | _ = self.has_errors.swap(true, .seq_cst); | |
| 1404 | }; | |
| 1405 | } | |
| 1406 | ||
| 1349 | 1407 | fn markImportsAndExports(self: *MachO) void { |
| 1350 | 1408 | const tracy = trace(@src()); |
| 1351 | 1409 | defer tracy.end(); |
| ... | ... | @@ -1384,16 +1442,26 @@ fn scanRelocs(self: *MachO) !void { |
| 1384 | 1442 | const tracy = trace(@src()); |
| 1385 | 1443 | defer tracy.end(); |
| 1386 | 1444 | |
| 1387 | if (self.getZigObject()) |zo| { | |
| 1388 | try zo.scanRelocs(self); | |
| 1389 | } | |
| 1390 | for (self.objects.items) |index| { | |
| 1391 | try self.getFile(index).?.object.scanRelocs(self); | |
| 1392 | } | |
| 1393 | if (self.getInternalObject()) |obj| { | |
| 1394 | obj.scanRelocs(self); | |
| 1445 | const tp = self.base.comp.thread_pool; | |
| 1446 | var wg: WaitGroup = .{}; | |
| 1447 | ||
| 1448 | { | |
| 1449 | wg.reset(); | |
| 1450 | defer wg.wait(); | |
| 1451 | ||
| 1452 | if (self.getZigObject()) |zo| { | |
| 1453 | tp.spawnWg(&wg, scanRelocsWorker, .{ self, zo.asFile() }); | |
| 1454 | } | |
| 1455 | for (self.objects.items) |index| { | |
| 1456 | tp.spawnWg(&wg, scanRelocsWorker, .{ self, self.getFile(index).? }); | |
| 1457 | } | |
| 1458 | if (self.getInternalObject()) |obj| { | |
| 1459 | tp.spawnWg(&wg, scanRelocsWorker, .{ self, obj.asFile() }); | |
| 1460 | } | |
| 1395 | 1461 | } |
| 1396 | 1462 | |
| 1463 | if (self.has_errors.swap(false, .seq_cst)) return error.FlushFailure; | |
| 1464 | ||
| 1397 | 1465 | try self.reportUndefs(); |
| 1398 | 1466 | |
| 1399 | 1467 | if (self.getZigObject()) |zo| { |
| ... | ... | @@ -1410,25 +1478,61 @@ fn scanRelocs(self: *MachO) !void { |
| 1410 | 1478 | } |
| 1411 | 1479 | } |
| 1412 | 1480 | |
| 1481 | fn scanRelocsWorker(self: *MachO, file: File) void { | |
| 1482 | file.scanRelocs(self) catch |err| { | |
| 1483 | self.reportParseError2(file.getIndex(), "failed to scan relocations: {s}", .{ | |
| 1484 | @errorName(err), | |
| 1485 | }) catch {}; | |
| 1486 | _ = self.has_errors.swap(true, .seq_cst); | |
| 1487 | }; | |
| 1488 | } | |
| 1489 | ||
| 1490 | fn sortGlobalSymbolsByName(self: *MachO, symbols: []SymbolResolver.Index) void { | |
| 1491 | const lessThan = struct { | |
| 1492 | fn lessThan(ctx: *MachO, lhs: SymbolResolver.Index, rhs: SymbolResolver.Index) bool { | |
| 1493 | const lhs_name = ctx.resolver.keys.items[lhs - 1].getName(ctx); | |
| 1494 | const rhs_name = ctx.resolver.keys.items[rhs - 1].getName(ctx); | |
| 1495 | return mem.order(u8, lhs_name, rhs_name) == .lt; | |
| 1496 | } | |
| 1497 | }.lessThan; | |
| 1498 | mem.sort(SymbolResolver.Index, symbols, self, lessThan); | |
| 1499 | } | |
| 1500 | ||
| 1413 | 1501 | fn reportUndefs(self: *MachO) !void { |
| 1414 | 1502 | const tracy = trace(@src()); |
| 1415 | 1503 | defer tracy.end(); |
| 1416 | 1504 | |
| 1417 | 1505 | if (self.undefined_treatment == .suppress or |
| 1418 | 1506 | self.undefined_treatment == .dynamic_lookup) return; |
| 1507 | if (self.undefs.keys().len == 0) return; // Nothing to do | |
| 1419 | 1508 | |
| 1509 | const gpa = self.base.comp.gpa; | |
| 1420 | 1510 | const max_notes = 4; |
| 1421 | 1511 | |
| 1422 | var has_undefs = false; | |
| 1423 | var it = self.undefs.iterator(); | |
| 1424 | while (it.next()) |entry| { | |
| 1425 | const undef_sym = self.resolver.keys.items[entry.key_ptr.* - 1]; | |
| 1426 | const notes = entry.value_ptr.*; | |
| 1512 | // We will sort by name, and then by file to ensure deterministic output. | |
| 1513 | var keys = try std.ArrayList(SymbolResolver.Index).initCapacity(gpa, self.undefs.keys().len); | |
| 1514 | defer keys.deinit(); | |
| 1515 | keys.appendSliceAssumeCapacity(self.undefs.keys()); | |
| 1516 | self.sortGlobalSymbolsByName(keys.items); | |
| 1517 | ||
| 1518 | const refLessThan = struct { | |
| 1519 | fn lessThan(ctx: void, lhs: Ref, rhs: Ref) bool { | |
| 1520 | _ = ctx; | |
| 1521 | return lhs.lessThan(rhs); | |
| 1522 | } | |
| 1523 | }.lessThan; | |
| 1524 | ||
| 1525 | for (self.undefs.values()) |*refs| { | |
| 1526 | mem.sort(Ref, refs.items, {}, refLessThan); | |
| 1527 | } | |
| 1528 | ||
| 1529 | for (keys.items) |key| { | |
| 1530 | const undef_sym = self.resolver.keys.items[key - 1]; | |
| 1531 | const notes = self.undefs.get(key).?; | |
| 1427 | 1532 | const nnotes = @min(notes.items.len, max_notes) + @intFromBool(notes.items.len > max_notes); |
| 1428 | 1533 | |
| 1429 | 1534 | var err = try self.base.addErrorWithNotes(nnotes); |
| 1430 | 1535 | try err.addMsg("undefined symbol: {s}", .{undef_sym.getName(self)}); |
| 1431 | has_undefs = true; | |
| 1432 | 1536 | |
| 1433 | 1537 | var inote: usize = 0; |
| 1434 | 1538 | while (inote < @min(notes.items.len, max_notes)) : (inote += 1) { |
| ... | ... | @@ -1443,7 +1547,8 @@ fn reportUndefs(self: *MachO) !void { |
| 1443 | 1547 | try err.addNote("referenced {d} more times", .{remaining}); |
| 1444 | 1548 | } |
| 1445 | 1549 | } |
| 1446 | if (has_undefs) return error.HasUndefinedSymbols; | |
| 1550 | ||
| 1551 | return error.HasUndefinedSymbols; | |
| 1447 | 1552 | } |
| 1448 | 1553 | |
| 1449 | 1554 | fn initOutputSections(self: *MachO) !void { |
| ... | ... | @@ -1679,7 +1784,7 @@ pub fn sortSections(self: *MachO) !void { |
| 1679 | 1784 | if (self.getZigObject()) |zo| { |
| 1680 | 1785 | for (zo.getAtoms()) |atom_index| { |
| 1681 | 1786 | const atom = zo.getAtom(atom_index) orelse continue; |
| 1682 | if (!atom.flags.alive) continue; | |
| 1787 | if (!atom.isAlive()) continue; | |
| 1683 | 1788 | atom.out_n_sect = backlinks[atom.out_n_sect]; |
| 1684 | 1789 | } |
| 1685 | 1790 | } |
| ... | ... | @@ -1688,7 +1793,7 @@ pub fn sortSections(self: *MachO) !void { |
| 1688 | 1793 | const file = self.getFile(index).?; |
| 1689 | 1794 | for (file.getAtoms()) |atom_index| { |
| 1690 | 1795 | const atom = file.getAtom(atom_index) orelse continue; |
| 1691 | if (!atom.flags.alive) continue; | |
| 1796 | if (!atom.isAlive()) continue; | |
| 1692 | 1797 | atom.out_n_sect = backlinks[atom.out_n_sect]; |
| 1693 | 1798 | } |
| 1694 | 1799 | } |
| ... | ... | @@ -1696,7 +1801,7 @@ pub fn sortSections(self: *MachO) !void { |
| 1696 | 1801 | if (self.getInternalObject()) |object| { |
| 1697 | 1802 | for (object.getAtoms()) |atom_index| { |
| 1698 | 1803 | const atom = object.getAtom(atom_index) orelse continue; |
| 1699 | if (!atom.flags.alive) continue; | |
| 1804 | if (!atom.isAlive()) continue; | |
| 1700 | 1805 | atom.out_n_sect = backlinks[atom.out_n_sect]; |
| 1701 | 1806 | } |
| 1702 | 1807 | } |
| ... | ... | @@ -1737,7 +1842,7 @@ pub fn addAtomsToSections(self: *MachO) !void { |
| 1737 | 1842 | if (self.getZigObject()) |zo| { |
| 1738 | 1843 | for (zo.getAtoms()) |atom_index| { |
| 1739 | 1844 | const atom = zo.getAtom(atom_index) orelse continue; |
| 1740 | if (!atom.flags.alive) continue; | |
| 1845 | if (!atom.isAlive()) continue; | |
| 1741 | 1846 | if (self.isZigSection(atom.out_n_sect)) continue; |
| 1742 | 1847 | const atoms = &self.sections.items(.atoms)[atom.out_n_sect]; |
| 1743 | 1848 | try atoms.append(gpa, .{ .index = atom_index, .file = zo.index }); |
| ... | ... | @@ -1747,7 +1852,7 @@ pub fn addAtomsToSections(self: *MachO) !void { |
| 1747 | 1852 | const file = self.getFile(index).?; |
| 1748 | 1853 | for (file.getAtoms()) |atom_index| { |
| 1749 | 1854 | const atom = file.getAtom(atom_index) orelse continue; |
| 1750 | if (!atom.flags.alive) continue; | |
| 1855 | if (!atom.isAlive()) continue; | |
| 1751 | 1856 | const atoms = &self.sections.items(.atoms)[atom.out_n_sect]; |
| 1752 | 1857 | try atoms.append(gpa, .{ .index = atom_index, .file = index }); |
| 1753 | 1858 | } |
| ... | ... | @@ -1755,7 +1860,7 @@ pub fn addAtomsToSections(self: *MachO) !void { |
| 1755 | 1860 | if (self.getInternalObject()) |object| { |
| 1756 | 1861 | for (object.getAtoms()) |atom_index| { |
| 1757 | 1862 | const atom = object.getAtom(atom_index) orelse continue; |
| 1758 | if (!atom.flags.alive) continue; | |
| 1863 | if (!atom.isAlive()) continue; | |
| 1759 | 1864 | const atoms = &self.sections.items(.atoms)[atom.out_n_sect]; |
| 1760 | 1865 | try atoms.append(gpa, .{ .index = atom_index, .file = object.index }); |
| 1761 | 1866 | } |
| ... | ... | @@ -1774,46 +1879,43 @@ fn calcSectionSizes(self: *MachO) !void { |
| 1774 | 1879 | header.@"align" = 3; |
| 1775 | 1880 | } |
| 1776 | 1881 | |
| 1777 | const slice = self.sections.slice(); | |
| 1778 | for (slice.items(.header), slice.items(.atoms)) |*header, atoms| { | |
| 1779 | if (atoms.items.len == 0) continue; | |
| 1780 | if (self.requiresThunks() and header.isCode()) continue; | |
| 1781 | ||
| 1782 | for (atoms.items) |ref| { | |
| 1783 | const atom = ref.getAtom(self).?; | |
| 1784 | const atom_alignment = atom.alignment.toByteUnits() orelse 1; | |
| 1785 | const offset = mem.alignForward(u64, header.size, atom_alignment); | |
| 1786 | const padding = offset - header.size; | |
| 1787 | atom.value = offset; | |
| 1788 | header.size += padding + atom.size; | |
| 1789 | header.@"align" = @max(header.@"align", atom.alignment.toLog2Units()); | |
| 1790 | } | |
| 1791 | } | |
| 1792 | ||
| 1793 | if (self.requiresThunks()) { | |
| 1882 | const tp = self.base.comp.thread_pool; | |
| 1883 | var wg: WaitGroup = .{}; | |
| 1884 | { | |
| 1885 | wg.reset(); | |
| 1886 | defer wg.wait(); | |
| 1887 | const slice = self.sections.slice(); | |
| 1794 | 1888 | for (slice.items(.header), slice.items(.atoms), 0..) |header, atoms, i| { |
| 1795 | if (!header.isCode()) continue; | |
| 1796 | 1889 | if (atoms.items.len == 0) continue; |
| 1890 | if (self.requiresThunks() and header.isCode()) continue; | |
| 1891 | tp.spawnWg(&wg, calcSectionSizeWorker, .{ self, @as(u8, @intCast(i)) }); | |
| 1892 | } | |
| 1797 | 1893 | |
| 1798 | // Create jump/branch range extenders if needed. | |
| 1799 | try thunks.createThunks(@intCast(i), self); | |
| 1894 | if (self.requiresThunks()) { | |
| 1895 | for (slice.items(.header), slice.items(.atoms), 0..) |header, atoms, i| { | |
| 1896 | if (!header.isCode()) continue; | |
| 1897 | if (atoms.items.len == 0) continue; | |
| 1898 | tp.spawnWg(&wg, createThunksWorker, .{ self, @as(u8, @intCast(i)) }); | |
| 1899 | } | |
| 1800 | 1900 | } |
| 1801 | } | |
| 1802 | 1901 | |
| 1803 | // At this point, we can also calculate symtab and data-in-code linkedit section sizes | |
| 1804 | if (self.getZigObject()) |zo| { | |
| 1805 | zo.asFile().calcSymtabSize(self); | |
| 1806 | } | |
| 1807 | for (self.objects.items) |index| { | |
| 1808 | self.getFile(index).?.calcSymtabSize(self); | |
| 1809 | } | |
| 1810 | for (self.dylibs.items) |index| { | |
| 1811 | self.getFile(index).?.calcSymtabSize(self); | |
| 1812 | } | |
| 1813 | if (self.getInternalObject()) |obj| { | |
| 1814 | obj.asFile().calcSymtabSize(self); | |
| 1902 | // At this point, we can also calculate symtab and data-in-code linkedit section sizes | |
| 1903 | if (self.getZigObject()) |zo| { | |
| 1904 | tp.spawnWg(&wg, File.calcSymtabSize, .{ zo.asFile(), self }); | |
| 1905 | } | |
| 1906 | for (self.objects.items) |index| { | |
| 1907 | tp.spawnWg(&wg, File.calcSymtabSize, .{ self.getFile(index).?, self }); | |
| 1908 | } | |
| 1909 | for (self.dylibs.items) |index| { | |
| 1910 | tp.spawnWg(&wg, File.calcSymtabSize, .{ self.getFile(index).?, self }); | |
| 1911 | } | |
| 1912 | if (self.getInternalObject()) |obj| { | |
| 1913 | tp.spawnWg(&wg, File.calcSymtabSize, .{ obj.asFile(), self }); | |
| 1914 | } | |
| 1815 | 1915 | } |
| 1816 | 1916 | |
| 1917 | if (self.has_errors.swap(false, .seq_cst)) return error.FlushFailure; | |
| 1918 | ||
| 1817 | 1919 | try self.calcSymtabSize(); |
| 1818 | 1920 | |
| 1819 | 1921 | if (self.got_sect_index) |idx| { |
| ... | ... | @@ -1861,6 +1963,49 @@ fn calcSectionSizes(self: *MachO) !void { |
| 1861 | 1963 | } |
| 1862 | 1964 | } |
| 1863 | 1965 | |
| 1966 | fn calcSectionSizeWorker(self: *MachO, sect_id: u8) void { | |
| 1967 | const tracy = trace(@src()); | |
| 1968 | defer tracy.end(); | |
| 1969 | const doWork = struct { | |
| 1970 | fn doWork(macho_file: *MachO, header: *macho.section_64, atoms: []const Ref) !void { | |
| 1971 | for (atoms) |ref| { | |
| 1972 | const atom = ref.getAtom(macho_file).?; | |
| 1973 | const atom_alignment = atom.alignment.toByteUnits() orelse 1; | |
| 1974 | const offset = mem.alignForward(u64, header.size, atom_alignment); | |
| 1975 | const padding = offset - header.size; | |
| 1976 | atom.value = offset; | |
| 1977 | header.size += padding + atom.size; | |
| 1978 | header.@"align" = @max(header.@"align", atom.alignment.toLog2Units()); | |
| 1979 | } | |
| 1980 | } | |
| 1981 | }.doWork; | |
| 1982 | const slice = self.sections.slice(); | |
| 1983 | const header = &slice.items(.header)[sect_id]; | |
| 1984 | const atoms = slice.items(.atoms)[sect_id].items; | |
| 1985 | doWork(self, header, atoms) catch |err| { | |
| 1986 | self.reportUnexpectedError("failed to calculate size of section '{s},{s}': {s}", .{ | |
| 1987 | header.segName(), | |
| 1988 | header.sectName(), | |
| 1989 | @errorName(err), | |
| 1990 | }) catch {}; | |
| 1991 | _ = self.has_errors.swap(true, .seq_cst); | |
| 1992 | }; | |
| 1993 | } | |
| 1994 | ||
| 1995 | fn createThunksWorker(self: *MachO, sect_id: u8) void { | |
| 1996 | const tracy = trace(@src()); | |
| 1997 | defer tracy.end(); | |
| 1998 | thunks.createThunks(sect_id, self) catch |err| { | |
| 1999 | const header = self.sections.items(.header)[sect_id]; | |
| 2000 | self.reportUnexpectedError("failed to create thunks and calculate size of section '{s},{s}': {s}", .{ | |
| 2001 | header.segName(), | |
| 2002 | header.sectName(), | |
| 2003 | @errorName(err), | |
| 2004 | }) catch {}; | |
| 2005 | _ = self.has_errors.swap(true, .seq_cst); | |
| 2006 | }; | |
| 2007 | } | |
| 2008 | ||
| 1864 | 2009 | fn generateUnwindInfo(self: *MachO) !void { |
| 1865 | 2010 | const tracy = trace(@src()); |
| 1866 | 2011 | defer tracy.end(); |
| ... | ... | @@ -2249,64 +2394,98 @@ fn writeSectionsAndUpdateLinkeditSizes(self: *MachO) !void { |
| 2249 | 2394 | try self.strtab.resize(gpa, cmd.strsize); |
| 2250 | 2395 | self.strtab.items[0] = 0; |
| 2251 | 2396 | |
| 2252 | for (self.objects.items) |index| { | |
| 2253 | try self.getFile(index).?.writeAtoms(self); | |
| 2254 | } | |
| 2255 | if (self.getZigObject()) |zo| { | |
| 2256 | try zo.writeAtoms(self); | |
| 2257 | } | |
| 2258 | if (self.getInternalObject()) |obj| { | |
| 2259 | try obj.asFile().writeAtoms(self); | |
| 2260 | } | |
| 2261 | for (self.thunks.items) |thunk| { | |
| 2262 | const out = self.sections.items(.out)[thunk.out_n_sect].items; | |
| 2263 | const off = math.cast(usize, thunk.value) orelse return error.Overflow; | |
| 2264 | const size = thunk.size(); | |
| 2265 | var stream = std.io.fixedBufferStream(out[off..][0..size]); | |
| 2266 | try thunk.write(self, stream.writer()); | |
| 2267 | } | |
| 2397 | const tp = self.base.comp.thread_pool; | |
| 2398 | var wg: WaitGroup = .{}; | |
| 2399 | { | |
| 2400 | wg.reset(); | |
| 2401 | defer wg.wait(); | |
| 2268 | 2402 | |
| 2269 | const slice = self.sections.slice(); | |
| 2270 | for (&[_]?u8{ | |
| 2271 | self.eh_frame_sect_index, | |
| 2272 | self.unwind_info_sect_index, | |
| 2273 | self.got_sect_index, | |
| 2274 | self.stubs_sect_index, | |
| 2275 | self.la_symbol_ptr_sect_index, | |
| 2276 | self.tlv_ptr_sect_index, | |
| 2277 | self.objc_stubs_sect_index, | |
| 2278 | }) |maybe_sect_id| { | |
| 2279 | if (maybe_sect_id) |sect_id| { | |
| 2280 | const out = slice.items(.out)[sect_id].items; | |
| 2281 | try self.writeSyntheticSection(sect_id, out); | |
| 2403 | for (self.objects.items) |index| { | |
| 2404 | tp.spawnWg(&wg, writeAtomsWorker, .{ self, self.getFile(index).? }); | |
| 2405 | } | |
| 2406 | if (self.getZigObject()) |zo| { | |
| 2407 | tp.spawnWg(&wg, writeAtomsWorker, .{ self, zo.asFile() }); | |
| 2408 | } | |
| 2409 | if (self.getInternalObject()) |obj| { | |
| 2410 | tp.spawnWg(&wg, writeAtomsWorker, .{ self, obj.asFile() }); | |
| 2411 | } | |
| 2412 | for (self.thunks.items) |thunk| { | |
| 2413 | tp.spawnWg(&wg, writeThunkWorker, .{ self, thunk }); | |
| 2282 | 2414 | } |
| 2283 | } | |
| 2284 | 2415 | |
| 2285 | if (self.la_symbol_ptr_sect_index) |_| { | |
| 2286 | try self.updateLazyBindSize(); | |
| 2287 | } | |
| 2416 | const slice = self.sections.slice(); | |
| 2417 | for (&[_]?u8{ | |
| 2418 | self.eh_frame_sect_index, | |
| 2419 | self.unwind_info_sect_index, | |
| 2420 | self.got_sect_index, | |
| 2421 | self.stubs_sect_index, | |
| 2422 | self.la_symbol_ptr_sect_index, | |
| 2423 | self.tlv_ptr_sect_index, | |
| 2424 | self.objc_stubs_sect_index, | |
| 2425 | }) |maybe_sect_id| { | |
| 2426 | if (maybe_sect_id) |sect_id| { | |
| 2427 | const out = slice.items(.out)[sect_id].items; | |
| 2428 | tp.spawnWg(&wg, writeSyntheticSectionWorker, .{ self, sect_id, out }); | |
| 2429 | } | |
| 2430 | } | |
| 2288 | 2431 | |
| 2289 | try self.rebase.updateSize(self); | |
| 2290 | try self.bind.updateSize(self); | |
| 2291 | try self.weak_bind.updateSize(self); | |
| 2292 | try self.export_trie.updateSize(self); | |
| 2293 | try self.data_in_code.updateSize(self); | |
| 2432 | if (self.la_symbol_ptr_sect_index) |_| { | |
| 2433 | tp.spawnWg(&wg, updateLazyBindSizeWorker, .{self}); | |
| 2434 | } | |
| 2294 | 2435 | |
| 2295 | if (self.getZigObject()) |zo| { | |
| 2296 | zo.asFile().writeSymtab(self, self); | |
| 2297 | } | |
| 2298 | for (self.objects.items) |index| { | |
| 2299 | self.getFile(index).?.writeSymtab(self, self); | |
| 2300 | } | |
| 2301 | for (self.dylibs.items) |index| { | |
| 2302 | self.getFile(index).?.writeSymtab(self, self); | |
| 2303 | } | |
| 2304 | if (self.getInternalObject()) |obj| { | |
| 2305 | obj.asFile().writeSymtab(self, self); | |
| 2436 | tp.spawnWg(&wg, updateLinkeditSizeWorker, .{ self, .rebase }); | |
| 2437 | tp.spawnWg(&wg, updateLinkeditSizeWorker, .{ self, .bind }); | |
| 2438 | tp.spawnWg(&wg, updateLinkeditSizeWorker, .{ self, .weak_bind }); | |
| 2439 | tp.spawnWg(&wg, updateLinkeditSizeWorker, .{ self, .export_trie }); | |
| 2440 | tp.spawnWg(&wg, updateLinkeditSizeWorker, .{ self, .data_in_code }); | |
| 2441 | ||
| 2442 | if (self.getZigObject()) |zo| { | |
| 2443 | tp.spawnWg(&wg, File.writeSymtab, .{ zo.asFile(), self, self }); | |
| 2444 | } | |
| 2445 | for (self.objects.items) |index| { | |
| 2446 | tp.spawnWg(&wg, File.writeSymtab, .{ self.getFile(index).?, self, self }); | |
| 2447 | } | |
| 2448 | for (self.dylibs.items) |index| { | |
| 2449 | tp.spawnWg(&wg, File.writeSymtab, .{ self.getFile(index).?, self, self }); | |
| 2450 | } | |
| 2451 | if (self.getInternalObject()) |obj| { | |
| 2452 | tp.spawnWg(&wg, File.writeSymtab, .{ obj.asFile(), self, self }); | |
| 2453 | } | |
| 2306 | 2454 | } |
| 2455 | ||
| 2456 | if (self.has_errors.swap(false, .seq_cst)) return error.FlushFailure; | |
| 2457 | } | |
| 2458 | ||
| 2459 | fn writeAtomsWorker(self: *MachO, file: File) void { | |
| 2460 | const tracy = trace(@src()); | |
| 2461 | defer tracy.end(); | |
| 2462 | file.writeAtoms(self) catch |err| { | |
| 2463 | self.reportParseError2(file.getIndex(), "failed to resolve relocations and write atoms: {s}", .{ | |
| 2464 | @errorName(err), | |
| 2465 | }) catch {}; | |
| 2466 | _ = self.has_errors.swap(true, .seq_cst); | |
| 2467 | }; | |
| 2307 | 2468 | } |
| 2308 | 2469 | |
| 2309 | fn writeSyntheticSection(self: *MachO, sect_id: u8, out: []u8) !void { | |
| 2470 | fn writeThunkWorker(self: *MachO, thunk: Thunk) void { | |
| 2471 | const tracy = trace(@src()); | |
| 2472 | defer tracy.end(); | |
| 2473 | const doWork = struct { | |
| 2474 | fn doWork(th: Thunk, buffer: []u8, macho_file: *MachO) !void { | |
| 2475 | const off = th.value; | |
| 2476 | const size = th.size(); | |
| 2477 | var stream = std.io.fixedBufferStream(buffer[off..][0..size]); | |
| 2478 | try th.write(macho_file, stream.writer()); | |
| 2479 | } | |
| 2480 | }.doWork; | |
| 2481 | const out = self.sections.items(.out)[thunk.out_n_sect].items; | |
| 2482 | doWork(thunk, out, self) catch |err| { | |
| 2483 | self.reportUnexpectedError("failed to write contents of thunk: {s}", .{@errorName(err)}) catch {}; | |
| 2484 | _ = self.has_errors.swap(true, .seq_cst); | |
| 2485 | }; | |
| 2486 | } | |
| 2487 | ||
| 2488 | fn writeSyntheticSectionWorker(self: *MachO, sect_id: u8, out: []u8) void { | |
| 2310 | 2489 | const tracy = trace(@src()); |
| 2311 | 2490 | defer tracy.end(); |
| 2312 | 2491 | |
| ... | ... | @@ -2320,6 +2499,22 @@ fn writeSyntheticSection(self: *MachO, sect_id: u8, out: []u8) !void { |
| 2320 | 2499 | objc_stubs, |
| 2321 | 2500 | }; |
| 2322 | 2501 | |
| 2502 | const doWork = struct { | |
| 2503 | fn doWork(macho_file: *MachO, tag: Tag, buffer: []u8) !void { | |
| 2504 | var stream = std.io.fixedBufferStream(buffer); | |
| 2505 | switch (tag) { | |
| 2506 | .eh_frame => eh_frame.write(macho_file, buffer), | |
| 2507 | .unwind_info => try macho_file.unwind_info.write(macho_file, buffer), | |
| 2508 | .got => try macho_file.got.write(macho_file, stream.writer()), | |
| 2509 | .stubs => try macho_file.stubs.write(macho_file, stream.writer()), | |
| 2510 | .la_symbol_ptr => try macho_file.la_symbol_ptr.write(macho_file, stream.writer()), | |
| 2511 | .tlv_ptr => try macho_file.tlv_ptr.write(macho_file, stream.writer()), | |
| 2512 | .objc_stubs => try macho_file.objc_stubs.write(macho_file, stream.writer()), | |
| 2513 | } | |
| 2514 | } | |
| 2515 | }.doWork; | |
| 2516 | ||
| 2517 | const header = self.sections.items(.header)[sect_id]; | |
| 2323 | 2518 | const tag: Tag = tag: { |
| 2324 | 2519 | if (self.eh_frame_sect_index != null and |
| 2325 | 2520 | self.eh_frame_sect_index.? == sect_id) break :tag .eh_frame; |
| ... | ... | @@ -2337,26 +2532,57 @@ fn writeSyntheticSection(self: *MachO, sect_id: u8, out: []u8) !void { |
| 2337 | 2532 | self.objc_stubs_sect_index.? == sect_id) break :tag .objc_stubs; |
| 2338 | 2533 | unreachable; |
| 2339 | 2534 | }; |
| 2340 | var stream = std.io.fixedBufferStream(out); | |
| 2341 | switch (tag) { | |
| 2342 | .eh_frame => eh_frame.write(self, out), | |
| 2343 | .unwind_info => try self.unwind_info.write(self, out), | |
| 2344 | .got => try self.got.write(self, stream.writer()), | |
| 2345 | .stubs => try self.stubs.write(self, stream.writer()), | |
| 2346 | .la_symbol_ptr => try self.la_symbol_ptr.write(self, stream.writer()), | |
| 2347 | .tlv_ptr => try self.tlv_ptr.write(self, stream.writer()), | |
| 2348 | .objc_stubs => try self.objc_stubs.write(self, stream.writer()), | |
| 2349 | } | |
| 2535 | doWork(self, tag, out) catch |err| { | |
| 2536 | self.reportUnexpectedError("could not write section '{s},{s}': {s}", .{ | |
| 2537 | header.segName(), | |
| 2538 | header.sectName(), | |
| 2539 | @errorName(err), | |
| 2540 | }) catch {}; | |
| 2541 | _ = self.has_errors.swap(true, .seq_cst); | |
| 2542 | }; | |
| 2350 | 2543 | } |
| 2351 | 2544 | |
| 2352 | fn updateLazyBindSize(self: *MachO) !void { | |
| 2545 | fn updateLazyBindSizeWorker(self: *MachO) void { | |
| 2353 | 2546 | const tracy = trace(@src()); |
| 2354 | 2547 | defer tracy.end(); |
| 2355 | try self.lazy_bind.updateSize(self); | |
| 2356 | const sect_id = self.stubs_helper_sect_index.?; | |
| 2357 | const out = &self.sections.items(.out)[sect_id]; | |
| 2358 | var stream = std.io.fixedBufferStream(out.items); | |
| 2359 | try self.stubs_helper.write(self, stream.writer()); | |
| 2548 | const doWork = struct { | |
| 2549 | fn doWork(macho_file: *MachO) !void { | |
| 2550 | try macho_file.lazy_bind.updateSize(macho_file); | |
| 2551 | const sect_id = macho_file.stubs_helper_sect_index.?; | |
| 2552 | const out = &macho_file.sections.items(.out)[sect_id]; | |
| 2553 | var stream = std.io.fixedBufferStream(out.items); | |
| 2554 | try macho_file.stubs_helper.write(macho_file, stream.writer()); | |
| 2555 | } | |
| 2556 | }.doWork; | |
| 2557 | doWork(self) catch |err| { | |
| 2558 | self.reportUnexpectedError("could not calculate size of lazy binding section: {s}", .{ | |
| 2559 | @errorName(err), | |
| 2560 | }) catch {}; | |
| 2561 | _ = self.has_errors.swap(true, .seq_cst); | |
| 2562 | }; | |
| 2563 | } | |
| 2564 | ||
| 2565 | pub fn updateLinkeditSizeWorker(self: *MachO, tag: enum { | |
| 2566 | rebase, | |
| 2567 | bind, | |
| 2568 | weak_bind, | |
| 2569 | export_trie, | |
| 2570 | data_in_code, | |
| 2571 | }) void { | |
| 2572 | const res = switch (tag) { | |
| 2573 | .rebase => self.rebase.updateSize(self), | |
| 2574 | .bind => self.bind.updateSize(self), | |
| 2575 | .weak_bind => self.weak_bind.updateSize(self), | |
| 2576 | .export_trie => self.export_trie.updateSize(self), | |
| 2577 | .data_in_code => self.data_in_code.updateSize(self), | |
| 2578 | }; | |
| 2579 | res catch |err| { | |
| 2580 | self.reportUnexpectedError("could not calculate size of {s} section: {s}", .{ | |
| 2581 | @tagName(tag), | |
| 2582 | @errorName(err), | |
| 2583 | }) catch {}; | |
| 2584 | _ = self.has_errors.swap(true, .seq_cst); | |
| 2585 | }; | |
| 2360 | 2586 | } |
| 2361 | 2587 | |
| 2362 | 2588 | fn writeSectionsToFile(self: *MachO) !void { |
| ... | ... | @@ -2684,13 +2910,13 @@ fn writeHeader(self: *MachO, ncmds: usize, sizeofcmds: usize) !void { |
| 2684 | 2910 | header.flags |= macho.MH_NO_REEXPORTED_DYLIBS; |
| 2685 | 2911 | } |
| 2686 | 2912 | |
| 2687 | if (self.has_tlv) { | |
| 2913 | if (self.has_tlv.load(.seq_cst)) { | |
| 2688 | 2914 | header.flags |= macho.MH_HAS_TLV_DESCRIPTORS; |
| 2689 | 2915 | } |
| 2690 | if (self.binds_to_weak) { | |
| 2916 | if (self.binds_to_weak.load(.seq_cst)) { | |
| 2691 | 2917 | header.flags |= macho.MH_BINDS_TO_WEAK; |
| 2692 | 2918 | } |
| 2693 | if (self.weak_defines) { | |
| 2919 | if (self.weak_defines.load(.seq_cst)) { | |
| 2694 | 2920 | header.flags |= macho.MH_WEAK_DEFINES; |
| 2695 | 2921 | } |
| 2696 | 2922 | |
| ... | ... | @@ -3586,19 +3812,29 @@ fn reportDuplicates(self: *MachO) error{ HasDuplicates, OutOfMemory }!void { |
| 3586 | 3812 | const tracy = trace(@src()); |
| 3587 | 3813 | defer tracy.end(); |
| 3588 | 3814 | |
| 3815 | if (self.dupes.keys().len == 0) return; // Nothing to do | |
| 3816 | ||
| 3817 | const gpa = self.base.comp.gpa; | |
| 3589 | 3818 | const max_notes = 3; |
| 3590 | 3819 | |
| 3591 | var has_dupes = false; | |
| 3592 | var it = self.dupes.iterator(); | |
| 3593 | while (it.next()) |entry| { | |
| 3594 | const sym = self.resolver.keys.items[entry.key_ptr.* - 1]; | |
| 3595 | const notes = entry.value_ptr.*; | |
| 3820 | // We will sort by name, and then by file to ensure deterministic output. | |
| 3821 | var keys = try std.ArrayList(SymbolResolver.Index).initCapacity(gpa, self.dupes.keys().len); | |
| 3822 | defer keys.deinit(); | |
| 3823 | keys.appendSliceAssumeCapacity(self.dupes.keys()); | |
| 3824 | self.sortGlobalSymbolsByName(keys.items); | |
| 3825 | ||
| 3826 | for (self.dupes.values()) |*refs| { | |
| 3827 | mem.sort(File.Index, refs.items, {}, std.sort.asc(File.Index)); | |
| 3828 | } | |
| 3829 | ||
| 3830 | for (keys.items) |key| { | |
| 3831 | const sym = self.resolver.keys.items[key - 1]; | |
| 3832 | const notes = self.dupes.get(key).?; | |
| 3596 | 3833 | const nnotes = @min(notes.items.len, max_notes) + @intFromBool(notes.items.len > max_notes); |
| 3597 | 3834 | |
| 3598 | 3835 | var err = try self.base.addErrorWithNotes(nnotes + 1); |
| 3599 | 3836 | try err.addMsg("duplicate symbol definition: {s}", .{sym.getName(self)}); |
| 3600 | 3837 | try err.addNote("defined by {}", .{sym.getFile(self).?.fmtPath()}); |
| 3601 | has_dupes = true; | |
| 3602 | 3838 | |
| 3603 | 3839 | var inote: usize = 0; |
| 3604 | 3840 | while (inote < @min(notes.items.len, max_notes)) : (inote += 1) { |
| ... | ... | @@ -3611,7 +3847,7 @@ fn reportDuplicates(self: *MachO) error{ HasDuplicates, OutOfMemory }!void { |
| 3611 | 3847 | try err.addNote("defined {d} more times", .{remaining}); |
| 3612 | 3848 | } |
| 3613 | 3849 | } |
| 3614 | if (has_dupes) return error.HasDuplicates; | |
| 3850 | return error.HasDuplicates; | |
| 3615 | 3851 | } |
| 3616 | 3852 | |
| 3617 | 3853 | pub fn getDebugSymbols(self: *MachO) ?*DebugSymbols { |
| ... | ... | @@ -4210,6 +4446,13 @@ pub const Ref = struct { |
| 4210 | 4446 | return ref.index == other.index and ref.file == other.file; |
| 4211 | 4447 | } |
| 4212 | 4448 | |
| 4449 | pub fn lessThan(ref: Ref, other: Ref) bool { | |
| 4450 | if (ref.file == other.file) { | |
| 4451 | return ref.index < other.index; | |
| 4452 | } | |
| 4453 | return ref.file < other.file; | |
| 4454 | } | |
| 4455 | ||
| 4213 | 4456 | pub fn getFile(ref: Ref, macho_file: *MachO) ?File { |
| 4214 | 4457 | return macho_file.getFile(ref.file); |
| 4215 | 4458 | } |
src/link/MachO/Atom.zig+36-29| ... | ... | @@ -26,7 +26,11 @@ off: u64 = 0, |
| 26 | 26 | /// Index of this atom in the linker's atoms table. |
| 27 | 27 | atom_index: Index = 0, |
| 28 | 28 | |
| 29 | flags: Flags = .{}, | |
| 29 | /// Specifies whether this atom is alive or has been garbage collected. | |
| 30 | alive: AtomicBool = AtomicBool.init(true), | |
| 31 | ||
| 32 | /// Specifies if this atom has been visited during garbage collection. | |
| 33 | visited: AtomicBool = AtomicBool.init(false), | |
| 30 | 34 | |
| 31 | 35 | /// Points to the previous and next neighbors, based on the `text_offset`. |
| 32 | 36 | /// This can be used to find, for example, the capacity of this `TextBlock`. |
| ... | ... | @@ -98,6 +102,14 @@ pub fn markUnwindRecordsDead(self: Atom, macho_file: *MachO) void { |
| 98 | 102 | } |
| 99 | 103 | } |
| 100 | 104 | |
| 105 | pub fn isAlive(self: Atom) bool { | |
| 106 | return self.alive.load(.seq_cst); | |
| 107 | } | |
| 108 | ||
| 109 | pub fn setAlive(self: *Atom, alive: bool) void { | |
| 110 | _ = self.alive.swap(alive, .seq_cst); | |
| 111 | } | |
| 112 | ||
| 101 | 113 | pub fn getThunk(self: Atom, macho_file: *MachO) *Thunk { |
| 102 | 114 | const extra = self.getExtra(macho_file); |
| 103 | 115 | return macho_file.getThunk(extra.thunk); |
| ... | ... | @@ -350,7 +362,7 @@ pub fn allocate(self: *Atom, macho_file: *MachO) !void { |
| 350 | 362 | _ = free_list.swapRemove(i); |
| 351 | 363 | } |
| 352 | 364 | |
| 353 | self.flags.alive = true; | |
| 365 | self.setAlive(true); | |
| 354 | 366 | } |
| 355 | 367 | |
| 356 | 368 | pub fn shrink(self: *Atom, macho_file: *MachO) void { |
| ... | ... | @@ -444,7 +456,7 @@ pub fn freeRelocs(self: *Atom, macho_file: *MachO) void { |
| 444 | 456 | pub fn scanRelocs(self: Atom, macho_file: *MachO) !void { |
| 445 | 457 | const tracy = trace(@src()); |
| 446 | 458 | defer tracy.end(); |
| 447 | assert(self.flags.alive); | |
| 459 | assert(self.isAlive()); | |
| 448 | 460 | |
| 449 | 461 | const relocs = self.getRelocs(macho_file); |
| 450 | 462 | |
| ... | ... | @@ -455,12 +467,12 @@ pub fn scanRelocs(self: Atom, macho_file: *MachO) !void { |
| 455 | 467 | .branch => { |
| 456 | 468 | const symbol = rel.getTargetSymbol(self, macho_file); |
| 457 | 469 | if (symbol.flags.import or (symbol.flags.@"export" and symbol.flags.weak) or symbol.flags.interposable) { |
| 458 | symbol.flags.stubs = true; | |
| 470 | symbol.setSectionFlags(.{ .stubs = true }); | |
| 459 | 471 | if (symbol.flags.weak) { |
| 460 | macho_file.binds_to_weak = true; | |
| 472 | macho_file.binds_to_weak.store(true, .seq_cst); | |
| 461 | 473 | } |
| 462 | 474 | } else if (mem.startsWith(u8, symbol.getName(macho_file), "_objc_msgSend$")) { |
| 463 | symbol.flags.objc_stubs = true; | |
| 475 | symbol.setSectionFlags(.{ .objc_stubs = true }); | |
| 464 | 476 | } |
| 465 | 477 | }, |
| 466 | 478 | |
| ... | ... | @@ -474,19 +486,19 @@ pub fn scanRelocs(self: Atom, macho_file: *MachO) !void { |
| 474 | 486 | symbol.flags.interposable or |
| 475 | 487 | macho_file.getTarget().cpu.arch == .aarch64) // TODO relax on arm64 |
| 476 | 488 | { |
| 477 | symbol.flags.needs_got = true; | |
| 489 | symbol.setSectionFlags(.{ .needs_got = true }); | |
| 478 | 490 | if (symbol.flags.weak) { |
| 479 | macho_file.binds_to_weak = true; | |
| 491 | macho_file.binds_to_weak.store(true, .seq_cst); | |
| 480 | 492 | } |
| 481 | 493 | } |
| 482 | 494 | }, |
| 483 | 495 | |
| 484 | 496 | .zig_got_load => { |
| 485 | assert(rel.getTargetSymbol(self, macho_file).flags.has_zig_got); | |
| 497 | assert(rel.getTargetSymbol(self, macho_file).getSectionFlags().has_zig_got); | |
| 486 | 498 | }, |
| 487 | 499 | |
| 488 | 500 | .got => { |
| 489 | rel.getTargetSymbol(self, macho_file).flags.needs_got = true; | |
| 501 | rel.getTargetSymbol(self, macho_file).setSectionFlags(.{ .needs_got = true }); | |
| 490 | 502 | }, |
| 491 | 503 | |
| 492 | 504 | .tlv, |
| ... | ... | @@ -502,9 +514,9 @@ pub fn scanRelocs(self: Atom, macho_file: *MachO) !void { |
| 502 | 514 | ); |
| 503 | 515 | } |
| 504 | 516 | if (symbol.flags.import or (symbol.flags.@"export" and symbol.flags.weak) or symbol.flags.interposable) { |
| 505 | symbol.flags.tlv_ptr = true; | |
| 517 | symbol.setSectionFlags(.{ .tlv_ptr = true }); | |
| 506 | 518 | if (symbol.flags.weak) { |
| 507 | macho_file.binds_to_weak = true; | |
| 519 | macho_file.binds_to_weak.store(true, .seq_cst); | |
| 508 | 520 | } |
| 509 | 521 | } |
| 510 | 522 | }, |
| ... | ... | @@ -514,17 +526,17 @@ pub fn scanRelocs(self: Atom, macho_file: *MachO) !void { |
| 514 | 526 | if (rel.tag == .@"extern") { |
| 515 | 527 | const symbol = rel.getTargetSymbol(self, macho_file); |
| 516 | 528 | if (symbol.isTlvInit(macho_file)) { |
| 517 | macho_file.has_tlv = true; | |
| 529 | macho_file.has_tlv.store(true, .seq_cst); | |
| 518 | 530 | continue; |
| 519 | 531 | } |
| 520 | 532 | if (symbol.flags.import) { |
| 521 | 533 | if (symbol.flags.weak) { |
| 522 | macho_file.binds_to_weak = true; | |
| 534 | macho_file.binds_to_weak.store(true, .seq_cst); | |
| 523 | 535 | } |
| 524 | 536 | continue; |
| 525 | 537 | } |
| 526 | 538 | if (symbol.flags.@"export" and symbol.flags.weak) { |
| 527 | macho_file.binds_to_weak = true; | |
| 539 | macho_file.binds_to_weak.store(true, .seq_cst); | |
| 528 | 540 | } |
| 529 | 541 | } |
| 530 | 542 | } |
| ... | ... | @@ -548,6 +560,8 @@ fn reportUndefSymbol(self: Atom, rel: Relocation, macho_file: *MachO) !bool { |
| 548 | 560 | const file = self.getFile(macho_file); |
| 549 | 561 | const ref = file.getSymbolRef(rel.target, macho_file); |
| 550 | 562 | if (ref.getFile(macho_file) == null) { |
| 563 | macho_file.undefs_mutex.lock(); | |
| 564 | defer macho_file.undefs_mutex.unlock(); | |
| 551 | 565 | const gpa = macho_file.base.comp.gpa; |
| 552 | 566 | const gop = try macho_file.undefs.getOrPut(gpa, file.getGlobals()[rel.target]); |
| 553 | 567 | if (!gop.found_existing) { |
| ... | ... | @@ -724,7 +738,7 @@ fn resolveRelocInner( |
| 724 | 738 | assert(rel.tag == .@"extern"); |
| 725 | 739 | assert(rel.meta.length == 2); |
| 726 | 740 | assert(rel.meta.pcrel); |
| 727 | if (rel.getTargetSymbol(self, macho_file).flags.has_got) { | |
| 741 | if (rel.getTargetSymbol(self, macho_file).getSectionFlags().has_got) { | |
| 728 | 742 | try writer.writeInt(i32, @intCast(G + A - P), .little); |
| 729 | 743 | } else { |
| 730 | 744 | try x86_64.relaxGotLoad(self, code[rel_offset - 3 ..], rel, macho_file); |
| ... | ... | @@ -748,7 +762,7 @@ fn resolveRelocInner( |
| 748 | 762 | assert(rel.meta.length == 2); |
| 749 | 763 | assert(rel.meta.pcrel); |
| 750 | 764 | const sym = rel.getTargetSymbol(self, macho_file); |
| 751 | if (sym.flags.tlv_ptr) { | |
| 765 | if (sym.getSectionFlags().tlv_ptr) { | |
| 752 | 766 | const S_: i64 = @intCast(sym.getTlvPtrAddress(macho_file)); |
| 753 | 767 | try writer.writeInt(i32, @intCast(S_ + A - P), .little); |
| 754 | 768 | } else { |
| ... | ... | @@ -776,7 +790,7 @@ fn resolveRelocInner( |
| 776 | 790 | const target = switch (rel.type) { |
| 777 | 791 | .page => S + A, |
| 778 | 792 | .got_load_page => G + A, |
| 779 | .tlvp_page => if (sym.flags.tlv_ptr) blk: { | |
| 793 | .tlvp_page => if (sym.getSectionFlags().tlv_ptr) blk: { | |
| 780 | 794 | const S_: i64 = @intCast(sym.getTlvPtrAddress(macho_file)); |
| 781 | 795 | break :blk S_ + A; |
| 782 | 796 | } else S + A, |
| ... | ... | @@ -831,7 +845,7 @@ fn resolveRelocInner( |
| 831 | 845 | |
| 832 | 846 | const sym = rel.getTargetSymbol(self, macho_file); |
| 833 | 847 | const target = target: { |
| 834 | const target = if (sym.flags.tlv_ptr) blk: { | |
| 848 | const target = if (sym.getSectionFlags().tlv_ptr) blk: { | |
| 835 | 849 | const S_: i64 = @intCast(sym.getTlvPtrAddress(macho_file)); |
| 836 | 850 | break :blk S_ + A; |
| 837 | 851 | } else S + A; |
| ... | ... | @@ -869,7 +883,7 @@ fn resolveRelocInner( |
| 869 | 883 | } |
| 870 | 884 | }; |
| 871 | 885 | |
| 872 | var inst = if (sym.flags.tlv_ptr) aarch64.Instruction{ | |
| 886 | var inst = if (sym.getSectionFlags().tlv_ptr) aarch64.Instruction{ | |
| 873 | 887 | .load_store_register = .{ |
| 874 | 888 | .rt = reg_info.rd, |
| 875 | 889 | .rn = reg_info.rn, |
| ... | ... | @@ -1142,7 +1156,7 @@ fn format2( |
| 1142 | 1156 | atom.out_n_sect, atom.alignment, atom.size, |
| 1143 | 1157 | atom.getRelocs(macho_file).len, atom.getExtra(macho_file).thunk, |
| 1144 | 1158 | }); |
| 1145 | if (!atom.flags.alive) try writer.writeAll(" : [*]"); | |
| 1159 | if (!atom.isAlive()) try writer.writeAll(" : [*]"); | |
| 1146 | 1160 | if (atom.getUnwindRecords(macho_file).len > 0) { |
| 1147 | 1161 | try writer.writeAll(" : unwind{ "); |
| 1148 | 1162 | const extra = atom.getExtra(macho_file); |
| ... | ... | @@ -1158,14 +1172,6 @@ fn format2( |
| 1158 | 1172 | |
| 1159 | 1173 | pub const Index = u32; |
| 1160 | 1174 | |
| 1161 | pub const Flags = packed struct { | |
| 1162 | /// Specifies whether this atom is alive or has been garbage collected. | |
| 1163 | alive: bool = true, | |
| 1164 | ||
| 1165 | /// Specifies if this atom has been visited during garbage collection. | |
| 1166 | visited: bool = false, | |
| 1167 | }; | |
| 1168 | ||
| 1169 | 1175 | pub const Extra = struct { |
| 1170 | 1176 | /// Index of the range extension thunk of this atom. |
| 1171 | 1177 | thunk: u32 = 0, |
| ... | ... | @@ -1209,6 +1215,7 @@ const trace = @import("../../tracy.zig").trace; |
| 1209 | 1215 | |
| 1210 | 1216 | const Allocator = mem.Allocator; |
| 1211 | 1217 | const Atom = @This(); |
| 1218 | const AtomicBool = std.atomic.Value(bool); | |
| 1212 | 1219 | const File = @import("file.zig").File; |
| 1213 | 1220 | const MachO = @import("../MachO.zig"); |
| 1214 | 1221 | const Object = @import("Object.zig"); |
src/link/MachO/InternalObject.zig+8-8| ... | ... | @@ -389,7 +389,7 @@ pub fn resolveObjcMsgSendSymbols(self: *InternalObject, macho_file: *MachO) !voi |
| 389 | 389 | }; |
| 390 | 390 | sym.nlist_idx = nlist_idx; |
| 391 | 391 | sym.extra = try self.addSymbolExtra(gpa, .{ .objc_selrefs = selrefs_index }); |
| 392 | sym.flags.objc_stubs = true; | |
| 392 | sym.setSectionFlags(.{ .objc_stubs = true }); | |
| 393 | 393 | |
| 394 | 394 | const idx = ref.getFile(macho_file).?.object.globals.items[ref.index]; |
| 395 | 395 | try self.globals.append(gpa, idx); |
| ... | ... | @@ -427,7 +427,7 @@ pub fn resolveLiterals(self: *InternalObject, lp: *MachO.LiteralPool, macho_file |
| 427 | 427 | const lp_sym = lp.getSymbol(res.index, macho_file); |
| 428 | 428 | const lp_atom = lp_sym.getAtom(macho_file).?; |
| 429 | 429 | lp_atom.alignment = lp_atom.alignment.max(atom.alignment); |
| 430 | atom.flags.alive = false; | |
| 430 | atom.setAlive(false); | |
| 431 | 431 | } |
| 432 | 432 | atom.addExtra(.{ .literal_pool_index = res.index }, macho_file); |
| 433 | 433 | } |
| ... | ... | @@ -439,7 +439,7 @@ pub fn dedupLiterals(self: *InternalObject, lp: MachO.LiteralPool, macho_file: * |
| 439 | 439 | |
| 440 | 440 | for (self.getAtoms()) |atom_index| { |
| 441 | 441 | const atom = self.getAtom(atom_index) orelse continue; |
| 442 | if (!atom.flags.alive) continue; | |
| 442 | if (!atom.isAlive()) continue; | |
| 443 | 443 | |
| 444 | 444 | const relocs = blk: { |
| 445 | 445 | const extra = atom.getExtra(macho_file); |
| ... | ... | @@ -464,7 +464,7 @@ pub fn dedupLiterals(self: *InternalObject, lp: MachO.LiteralPool, macho_file: * |
| 464 | 464 | } |
| 465 | 465 | |
| 466 | 466 | for (self.symbols.items) |*sym| { |
| 467 | if (!sym.flags.objc_stubs) continue; | |
| 467 | if (!sym.getSectionFlags().objc_stubs) continue; | |
| 468 | 468 | const extra = sym.getExtra(macho_file); |
| 469 | 469 | const file = sym.getFile(macho_file).?; |
| 470 | 470 | if (file.getIndex() != self.index) continue; |
| ... | ... | @@ -490,20 +490,20 @@ pub fn scanRelocs(self: *InternalObject, macho_file: *MachO) void { |
| 490 | 490 | if (self.getEntryRef(macho_file)) |ref| { |
| 491 | 491 | if (ref.getFile(macho_file) != null) { |
| 492 | 492 | const sym = ref.getSymbol(macho_file).?; |
| 493 | if (sym.flags.import) sym.flags.stubs = true; | |
| 493 | if (sym.flags.import) sym.setSectionFlags(.{ .stubs = true }); | |
| 494 | 494 | } |
| 495 | 495 | } |
| 496 | 496 | if (self.getDyldStubBinderRef(macho_file)) |ref| { |
| 497 | 497 | if (ref.getFile(macho_file) != null) { |
| 498 | 498 | const sym = ref.getSymbol(macho_file).?; |
| 499 | sym.flags.needs_got = true; | |
| 499 | sym.setSectionFlags(.{ .needs_got = true }); | |
| 500 | 500 | } |
| 501 | 501 | } |
| 502 | 502 | if (self.getObjcMsgSendRef(macho_file)) |ref| { |
| 503 | 503 | if (ref.getFile(macho_file) != null) { |
| 504 | 504 | const sym = ref.getSymbol(macho_file).?; |
| 505 | 505 | // TODO is it always needed, or only if we are synthesising fast stubs |
| 506 | sym.flags.needs_got = true; | |
| 506 | sym.setSectionFlags(.{ .needs_got = true }); | |
| 507 | 507 | } |
| 508 | 508 | } |
| 509 | 509 | } |
| ... | ... | @@ -570,7 +570,7 @@ pub fn writeAtoms(self: *InternalObject, macho_file: *MachO) !void { |
| 570 | 570 | |
| 571 | 571 | for (self.getAtoms()) |atom_index| { |
| 572 | 572 | const atom = self.getAtom(atom_index) orelse continue; |
| 573 | if (!atom.flags.alive) continue; | |
| 573 | if (!atom.isAlive()) continue; | |
| 574 | 574 | const sect = atom.getInputSection(macho_file); |
| 575 | 575 | if (sect.isZerofill()) continue; |
| 576 | 576 | const off = std.math.cast(usize, atom.value) orelse return error.Overflow; |
src/link/MachO/Object.zig+12-12| ... | ... | @@ -263,7 +263,7 @@ pub fn parse(self: *Object, macho_file: *MachO) !void { |
| 263 | 263 | mem.eql(u8, isec.sectName(), "__compact_unwind") or |
| 264 | 264 | isec.attrs() & macho.S_ATTR_DEBUG != 0) |
| 265 | 265 | { |
| 266 | atom.flags.alive = false; | |
| 266 | atom.setAlive(false); | |
| 267 | 267 | } |
| 268 | 268 | } |
| 269 | 269 | |
| ... | ... | @@ -645,7 +645,7 @@ pub fn resolveLiterals(self: *Object, lp: *MachO.LiteralPool, macho_file: *MachO |
| 645 | 645 | const lp_sym = lp.getSymbol(res.index, macho_file); |
| 646 | 646 | const lp_atom = lp_sym.getAtom(macho_file).?; |
| 647 | 647 | lp_atom.alignment = lp_atom.alignment.max(atom.alignment); |
| 648 | atom.flags.alive = false; | |
| 648 | atom.setAlive(false); | |
| 649 | 649 | } |
| 650 | 650 | atom.addExtra(.{ .literal_pool_index = res.index }, macho_file); |
| 651 | 651 | } |
| ... | ... | @@ -683,7 +683,7 @@ pub fn resolveLiterals(self: *Object, lp: *MachO.LiteralPool, macho_file: *MachO |
| 683 | 683 | const lp_sym = lp.getSymbol(res.index, macho_file); |
| 684 | 684 | const lp_atom = lp_sym.getAtom(macho_file).?; |
| 685 | 685 | lp_atom.alignment = lp_atom.alignment.max(atom.alignment); |
| 686 | atom.flags.alive = false; | |
| 686 | atom.setAlive(false); | |
| 687 | 687 | } |
| 688 | 688 | atom.addExtra(.{ .literal_pool_index = res.index }, macho_file); |
| 689 | 689 | } |
| ... | ... | @@ -697,7 +697,7 @@ pub fn dedupLiterals(self: *Object, lp: MachO.LiteralPool, macho_file: *MachO) v |
| 697 | 697 | |
| 698 | 698 | for (self.getAtoms()) |atom_index| { |
| 699 | 699 | const atom = self.getAtom(atom_index) orelse continue; |
| 700 | if (!atom.flags.alive) continue; | |
| 700 | if (!atom.isAlive()) continue; | |
| 701 | 701 | |
| 702 | 702 | const relocs = blk: { |
| 703 | 703 | const extra = atom.getExtra(macho_file); |
| ... | ... | @@ -990,7 +990,7 @@ fn initRelocs(self: *Object, file: File.Handle, cpu_arch: std.Target.Cpu.Arch, m |
| 990 | 990 | var next_reloc: u32 = 0; |
| 991 | 991 | for (subsections.items) |subsection| { |
| 992 | 992 | const atom = self.getAtom(subsection.atom).?; |
| 993 | if (!atom.flags.alive) continue; | |
| 993 | if (!atom.isAlive()) continue; | |
| 994 | 994 | if (next_reloc >= relocs.items.len) break; |
| 995 | 995 | const end_addr = atom.off + atom.size; |
| 996 | 996 | const rel_index = next_reloc; |
| ... | ... | @@ -1483,7 +1483,7 @@ pub fn resolveSymbols(self: *Object, macho_file: *MachO) !void { |
| 1483 | 1483 | if (!nlist.ext()) continue; |
| 1484 | 1484 | if (nlist.sect()) { |
| 1485 | 1485 | const atom = self.getAtom(atom_index).?; |
| 1486 | if (!atom.flags.alive) continue; | |
| 1486 | if (!atom.isAlive()) continue; | |
| 1487 | 1487 | } |
| 1488 | 1488 | |
| 1489 | 1489 | const gop = try macho_file.resolver.getOrPut(gpa, .{ |
| ... | ... | @@ -1552,7 +1552,7 @@ pub fn scanRelocs(self: *Object, macho_file: *MachO) !void { |
| 1552 | 1552 | |
| 1553 | 1553 | for (self.getAtoms()) |atom_index| { |
| 1554 | 1554 | const atom = self.getAtom(atom_index) orelse continue; |
| 1555 | if (!atom.flags.alive) continue; | |
| 1555 | if (!atom.isAlive()) continue; | |
| 1556 | 1556 | const sect = atom.getInputSection(macho_file); |
| 1557 | 1557 | if (sect.isZerofill()) continue; |
| 1558 | 1558 | try atom.scanRelocs(macho_file); |
| ... | ... | @@ -1563,10 +1563,10 @@ pub fn scanRelocs(self: *Object, macho_file: *MachO) !void { |
| 1563 | 1563 | if (!rec.alive) continue; |
| 1564 | 1564 | if (rec.getFde(macho_file)) |fde| { |
| 1565 | 1565 | if (fde.getCie(macho_file).getPersonality(macho_file)) |sym| { |
| 1566 | sym.flags.needs_got = true; | |
| 1566 | sym.setSectionFlags(.{ .needs_got = true }); | |
| 1567 | 1567 | } |
| 1568 | 1568 | } else if (rec.getPersonality(macho_file)) |sym| { |
| 1569 | sym.flags.needs_got = true; | |
| 1569 | sym.setSectionFlags(.{ .needs_got = true }); | |
| 1570 | 1570 | } |
| 1571 | 1571 | } |
| 1572 | 1572 | } |
| ... | ... | @@ -1745,7 +1745,7 @@ pub fn calcSymtabSize(self: *Object, macho_file: *MachO) void { |
| 1745 | 1745 | const ref = self.getSymbolRef(@intCast(i), macho_file); |
| 1746 | 1746 | const file = ref.getFile(macho_file) orelse continue; |
| 1747 | 1747 | if (file.getIndex() != self.index) continue; |
| 1748 | if (sym.getAtom(macho_file)) |atom| if (!atom.flags.alive) continue; | |
| 1748 | if (sym.getAtom(macho_file)) |atom| if (!atom.isAlive()) continue; | |
| 1749 | 1749 | if (sym.isSymbolStab(macho_file)) continue; |
| 1750 | 1750 | const name = sym.getName(macho_file); |
| 1751 | 1751 | if (name.len == 0) continue; |
| ... | ... | @@ -1854,7 +1854,7 @@ pub fn writeAtoms(self: *Object, macho_file: *MachO) !void { |
| 1854 | 1854 | } |
| 1855 | 1855 | for (self.getAtoms()) |atom_index| { |
| 1856 | 1856 | const atom = self.getAtom(atom_index) orelse continue; |
| 1857 | if (!atom.flags.alive) continue; | |
| 1857 | if (!atom.isAlive()) continue; | |
| 1858 | 1858 | const sect = atom.getInputSection(macho_file); |
| 1859 | 1859 | if (sect.isZerofill()) continue; |
| 1860 | 1860 | const value = math.cast(usize, atom.value) orelse return error.Overflow; |
| ... | ... | @@ -1893,7 +1893,7 @@ pub fn writeAtomsRelocatable(self: *Object, macho_file: *MachO) !void { |
| 1893 | 1893 | } |
| 1894 | 1894 | for (self.getAtoms()) |atom_index| { |
| 1895 | 1895 | const atom = self.getAtom(atom_index) orelse continue; |
| 1896 | if (!atom.flags.alive) continue; | |
| 1896 | if (!atom.isAlive()) continue; | |
| 1897 | 1897 | const sect = atom.getInputSection(macho_file); |
| 1898 | 1898 | if (sect.isZerofill()) continue; |
| 1899 | 1899 | const value = math.cast(usize, atom.value) orelse return error.Overflow; |
src/link/MachO/Symbol.zig+24-10| ... | ... | @@ -23,6 +23,8 @@ nlist_idx: u32 = 0, |
| 23 | 23 | /// Misc flags for the symbol packaged as packed struct for compression. |
| 24 | 24 | flags: Flags = .{}, |
| 25 | 25 | |
| 26 | sect_flags: std.atomic.Value(u8) = std.atomic.Value(u8).init(0), | |
| 27 | ||
| 26 | 28 | visibility: Visibility = .local, |
| 27 | 29 | |
| 28 | 30 | extra: u32 = 0, |
| ... | ... | @@ -69,6 +71,14 @@ pub fn getOutputSectionIndex(symbol: Symbol, macho_file: *MachO) u8 { |
| 69 | 71 | return symbol.out_n_sect; |
| 70 | 72 | } |
| 71 | 73 | |
| 74 | pub fn getSectionFlags(symbol: Symbol) SectionFlags { | |
| 75 | return @bitCast(symbol.sect_flags.load(.seq_cst)); | |
| 76 | } | |
| 77 | ||
| 78 | pub fn setSectionFlags(symbol: *Symbol, flags: SectionFlags) void { | |
| 79 | _ = symbol.sect_flags.fetchOr(@bitCast(flags), .seq_cst); | |
| 80 | } | |
| 81 | ||
| 72 | 82 | pub fn getFile(symbol: Symbol, macho_file: *MachO) ?File { |
| 73 | 83 | return macho_file.getFile(symbol.file); |
| 74 | 84 | } |
| ... | ... | @@ -116,9 +126,9 @@ pub fn getAddress(symbol: Symbol, opts: struct { |
| 116 | 126 | stubs: bool = true, |
| 117 | 127 | }, macho_file: *MachO) u64 { |
| 118 | 128 | if (opts.stubs) { |
| 119 | if (symbol.flags.stubs) { | |
| 129 | if (symbol.getSectionFlags().stubs) { | |
| 120 | 130 | return symbol.getStubsAddress(macho_file); |
| 121 | } else if (symbol.flags.objc_stubs) { | |
| 131 | } else if (symbol.getSectionFlags().objc_stubs) { | |
| 122 | 132 | return symbol.getObjcStubsAddress(macho_file); |
| 123 | 133 | } |
| 124 | 134 | } |
| ... | ... | @@ -127,25 +137,25 @@ pub fn getAddress(symbol: Symbol, opts: struct { |
| 127 | 137 | } |
| 128 | 138 | |
| 129 | 139 | pub fn getGotAddress(symbol: Symbol, macho_file: *MachO) u64 { |
| 130 | if (!symbol.flags.has_got) return 0; | |
| 140 | if (!symbol.getSectionFlags().has_got) return 0; | |
| 131 | 141 | const extra = symbol.getExtra(macho_file); |
| 132 | 142 | return macho_file.got.getAddress(extra.got, macho_file); |
| 133 | 143 | } |
| 134 | 144 | |
| 135 | 145 | pub fn getStubsAddress(symbol: Symbol, macho_file: *MachO) u64 { |
| 136 | if (!symbol.flags.stubs) return 0; | |
| 146 | if (!symbol.getSectionFlags().stubs) return 0; | |
| 137 | 147 | const extra = symbol.getExtra(macho_file); |
| 138 | 148 | return macho_file.stubs.getAddress(extra.stubs, macho_file); |
| 139 | 149 | } |
| 140 | 150 | |
| 141 | 151 | pub fn getObjcStubsAddress(symbol: Symbol, macho_file: *MachO) u64 { |
| 142 | if (!symbol.flags.objc_stubs) return 0; | |
| 152 | if (!symbol.getSectionFlags().objc_stubs) return 0; | |
| 143 | 153 | const extra = symbol.getExtra(macho_file); |
| 144 | 154 | return macho_file.objc_stubs.getAddress(extra.objc_stubs, macho_file); |
| 145 | 155 | } |
| 146 | 156 | |
| 147 | 157 | pub fn getObjcSelrefsAddress(symbol: Symbol, macho_file: *MachO) u64 { |
| 148 | if (!symbol.flags.objc_stubs) return 0; | |
| 158 | if (!symbol.getSectionFlags().objc_stubs) return 0; | |
| 149 | 159 | const extra = symbol.getExtra(macho_file); |
| 150 | 160 | const file = symbol.getFile(macho_file).?; |
| 151 | 161 | return switch (file) { |
| ... | ... | @@ -155,7 +165,7 @@ pub fn getObjcSelrefsAddress(symbol: Symbol, macho_file: *MachO) u64 { |
| 155 | 165 | } |
| 156 | 166 | |
| 157 | 167 | pub fn getTlvPtrAddress(symbol: Symbol, macho_file: *MachO) u64 { |
| 158 | if (!symbol.flags.tlv_ptr) return 0; | |
| 168 | if (!symbol.getSectionFlags().tlv_ptr) return 0; | |
| 159 | 169 | const extra = symbol.getExtra(macho_file); |
| 160 | 170 | return macho_file.tlv_ptr.getAddress(extra.tlv_ptr, macho_file); |
| 161 | 171 | } |
| ... | ... | @@ -167,14 +177,14 @@ const GetOrCreateZigGotEntryResult = struct { |
| 167 | 177 | |
| 168 | 178 | pub fn getOrCreateZigGotEntry(symbol: *Symbol, symbol_index: Index, macho_file: *MachO) !GetOrCreateZigGotEntryResult { |
| 169 | 179 | assert(!macho_file.base.isRelocatable()); |
| 170 | assert(symbol.flags.needs_zig_got); | |
| 171 | if (symbol.flags.has_zig_got) return .{ .found_existing = true, .index = symbol.getExtra(macho_file).zig_got }; | |
| 180 | assert(symbol.getSectionFlags().needs_zig_got); | |
| 181 | if (symbol.getSectionFlags().has_zig_got) return .{ .found_existing = true, .index = symbol.getExtra(macho_file).zig_got }; | |
| 172 | 182 | const index = try macho_file.zig_got.addSymbol(symbol_index, macho_file); |
| 173 | 183 | return .{ .found_existing = false, .index = index }; |
| 174 | 184 | } |
| 175 | 185 | |
| 176 | 186 | pub fn getZigGotAddress(symbol: Symbol, macho_file: *MachO) u64 { |
| 177 | if (!symbol.flags.has_zig_got) return 0; | |
| 187 | if (!symbol.getSectionFlags().has_zig_got) return 0; | |
| 178 | 188 | const extras = symbol.getExtra(macho_file); |
| 179 | 189 | return macho_file.zig_got.entryAddress(extras.zig_got, macho_file); |
| 180 | 190 | } |
| ... | ... | @@ -384,7 +394,9 @@ pub const Flags = packed struct { |
| 384 | 394 | |
| 385 | 395 | /// Whether the symbol makes into the output symtab or not. |
| 386 | 396 | output_symtab: bool = false, |
| 397 | }; | |
| 387 | 398 | |
| 399 | pub const SectionFlags = packed struct(u8) { | |
| 388 | 400 | /// Whether the symbol contains __got indirection. |
| 389 | 401 | needs_got: bool = false, |
| 390 | 402 | has_got: bool = false, |
| ... | ... | @@ -401,6 +413,8 @@ pub const Flags = packed struct { |
| 401 | 413 | |
| 402 | 414 | /// Whether the symbol contains __objc_stubs indirection. |
| 403 | 415 | objc_stubs: bool = false, |
| 416 | ||
| 417 | _: u1 = 0, | |
| 404 | 418 | }; |
| 405 | 419 | |
| 406 | 420 | pub const Visibility = enum { |
src/link/MachO/UnwindInfo.zig+1-1| ... | ... | @@ -53,7 +53,7 @@ pub fn generate(info: *UnwindInfo, macho_file: *MachO) !void { |
| 53 | 53 | for (macho_file.sections.items(.atoms)) |atoms| { |
| 54 | 54 | for (atoms.items) |ref| { |
| 55 | 55 | const atom = ref.getAtom(macho_file) orelse continue; |
| 56 | if (!atom.flags.alive) continue; | |
| 56 | if (!atom.isAlive()) continue; | |
| 57 | 57 | const recs = atom.getUnwindRecords(macho_file); |
| 58 | 58 | const file = atom.getFile(macho_file); |
| 59 | 59 | try info.records.ensureUnusedCapacity(gpa, recs.len); |
src/link/MachO/ZigObject.zig+19-19| ... | ... | @@ -245,7 +245,7 @@ pub fn resolveSymbols(self: *ZigObject, macho_file: *MachO) !void { |
| 245 | 245 | if (!nlist.ext()) continue; |
| 246 | 246 | if (nlist.sect()) { |
| 247 | 247 | const atom = self.getAtom(atom_index).?; |
| 248 | if (!atom.flags.alive) continue; | |
| 248 | if (!atom.isAlive()) continue; | |
| 249 | 249 | } |
| 250 | 250 | |
| 251 | 251 | const gop = try macho_file.resolver.getOrPut(gpa, .{ |
| ... | ... | @@ -391,7 +391,7 @@ pub fn claimUnresolved(self: *ZigObject, macho_file: *MachO) void { |
| 391 | 391 | pub fn scanRelocs(self: *ZigObject, macho_file: *MachO) !void { |
| 392 | 392 | for (self.getAtoms()) |atom_index| { |
| 393 | 393 | const atom = self.getAtom(atom_index) orelse continue; |
| 394 | if (!atom.flags.alive) continue; | |
| 394 | if (!atom.isAlive()) continue; | |
| 395 | 395 | const sect = atom.getInputSection(macho_file); |
| 396 | 396 | if (sect.isZerofill()) continue; |
| 397 | 397 | try atom.scanRelocs(macho_file); |
| ... | ... | @@ -403,7 +403,7 @@ pub fn resolveRelocs(self: *ZigObject, macho_file: *MachO) !void { |
| 403 | 403 | var has_error = false; |
| 404 | 404 | for (self.getAtoms()) |atom_index| { |
| 405 | 405 | const atom = self.getAtom(atom_index) orelse continue; |
| 406 | if (!atom.flags.alive) continue; | |
| 406 | if (!atom.isAlive()) continue; | |
| 407 | 407 | const sect = &macho_file.sections.items(.header)[atom.out_n_sect]; |
| 408 | 408 | if (sect.isZerofill()) continue; |
| 409 | 409 | if (!macho_file.isZigSection(atom.out_n_sect)) continue; // Non-Zig sections are handled separately |
| ... | ... | @@ -450,7 +450,7 @@ pub fn resolveRelocs(self: *ZigObject, macho_file: *MachO) !void { |
| 450 | 450 | pub fn calcNumRelocs(self: *ZigObject, macho_file: *MachO) void { |
| 451 | 451 | for (self.getAtoms()) |atom_index| { |
| 452 | 452 | const atom = self.getAtom(atom_index) orelse continue; |
| 453 | if (!atom.flags.alive) continue; | |
| 453 | if (!atom.isAlive()) continue; | |
| 454 | 454 | const header = &macho_file.sections.items(.header)[atom.out_n_sect]; |
| 455 | 455 | if (header.isZerofill()) continue; |
| 456 | 456 | if (!macho_file.isZigSection(atom.out_n_sect) and !macho_file.isDebugSection(atom.out_n_sect)) continue; |
| ... | ... | @@ -465,7 +465,7 @@ pub fn writeRelocs(self: *ZigObject, macho_file: *MachO) !void { |
| 465 | 465 | |
| 466 | 466 | for (self.getAtoms()) |atom_index| { |
| 467 | 467 | const atom = self.getAtom(atom_index) orelse continue; |
| 468 | if (!atom.flags.alive) continue; | |
| 468 | if (!atom.isAlive()) continue; | |
| 469 | 469 | const header = macho_file.sections.items(.header)[atom.out_n_sect]; |
| 470 | 470 | const relocs = macho_file.sections.items(.relocs)[atom.out_n_sect].items; |
| 471 | 471 | if (header.isZerofill()) continue; |
| ... | ... | @@ -505,7 +505,7 @@ pub fn writeAtomsRelocatable(self: *ZigObject, macho_file: *MachO) !void { |
| 505 | 505 | |
| 506 | 506 | for (self.getAtoms()) |atom_index| { |
| 507 | 507 | const atom = self.getAtom(atom_index) orelse continue; |
| 508 | if (!atom.flags.alive) continue; | |
| 508 | if (!atom.isAlive()) continue; | |
| 509 | 509 | const sect = atom.getInputSection(macho_file); |
| 510 | 510 | if (sect.isZerofill()) continue; |
| 511 | 511 | if (macho_file.isZigSection(atom.out_n_sect)) continue; |
| ... | ... | @@ -529,7 +529,7 @@ pub fn writeAtoms(self: *ZigObject, macho_file: *MachO) !void { |
| 529 | 529 | |
| 530 | 530 | for (self.getAtoms()) |atom_index| { |
| 531 | 531 | const atom = self.getAtom(atom_index) orelse continue; |
| 532 | if (!atom.flags.alive) continue; | |
| 532 | if (!atom.isAlive()) continue; | |
| 533 | 533 | const sect = atom.getInputSection(macho_file); |
| 534 | 534 | if (sect.isZerofill()) continue; |
| 535 | 535 | if (macho_file.isZigSection(atom.out_n_sect)) continue; |
| ... | ... | @@ -549,7 +549,7 @@ pub fn calcSymtabSize(self: *ZigObject, macho_file: *MachO) void { |
| 549 | 549 | const ref = self.getSymbolRef(@intCast(i), macho_file); |
| 550 | 550 | const file = ref.getFile(macho_file) orelse continue; |
| 551 | 551 | if (file.getIndex() != self.index) continue; |
| 552 | if (sym.getAtom(macho_file)) |atom| if (!atom.flags.alive) continue; | |
| 552 | if (sym.getAtom(macho_file)) |atom| if (!atom.isAlive()) continue; | |
| 553 | 553 | sym.flags.output_symtab = true; |
| 554 | 554 | if (sym.isLocal()) { |
| 555 | 555 | sym.addExtra(.{ .symtab = self.output_symtab_ctx.nlocals }, macho_file); |
| ... | ... | @@ -914,7 +914,7 @@ pub fn updateDecl( |
| 914 | 914 | const lib_name = variable.lib_name.toSlice(&mod.intern_pool); |
| 915 | 915 | const index = try self.getGlobalSymbol(macho_file, name, lib_name); |
| 916 | 916 | const sym = &self.symbols.items[index]; |
| 917 | sym.flags.needs_got = true; | |
| 917 | sym.setSectionFlags(.{ .needs_got = true }); | |
| 918 | 918 | return; |
| 919 | 919 | } |
| 920 | 920 | |
| ... | ... | @@ -993,7 +993,7 @@ fn updateDeclCode( |
| 993 | 993 | const sym_name = try std.fmt.allocPrintZ(gpa, "_{s}", .{decl.fqn.toSlice(ip)}); |
| 994 | 994 | defer gpa.free(sym_name); |
| 995 | 995 | sym.name = try self.strtab.insert(gpa, sym_name); |
| 996 | atom.flags.alive = true; | |
| 996 | atom.setAlive(true); | |
| 997 | 997 | atom.name = sym.name; |
| 998 | 998 | nlist.n_strx = sym.name; |
| 999 | 999 | nlist.n_type = macho.N_SECT; |
| ... | ... | @@ -1018,7 +1018,7 @@ fn updateDeclCode( |
| 1018 | 1018 | |
| 1019 | 1019 | if (!macho_file.base.isRelocatable()) { |
| 1020 | 1020 | log.debug(" (updating offset table entry)", .{}); |
| 1021 | assert(sym.flags.has_zig_got); | |
| 1021 | assert(sym.getSectionFlags().has_zig_got); | |
| 1022 | 1022 | const extra = sym.getExtra(macho_file); |
| 1023 | 1023 | try macho_file.zig_got.writeOne(macho_file, extra.zig_got); |
| 1024 | 1024 | } |
| ... | ... | @@ -1034,7 +1034,7 @@ fn updateDeclCode( |
| 1034 | 1034 | errdefer self.freeDeclMetadata(macho_file, sym_index); |
| 1035 | 1035 | |
| 1036 | 1036 | sym.value = 0; |
| 1037 | sym.flags.needs_zig_got = true; | |
| 1037 | sym.setSectionFlags(.{ .needs_zig_got = true }); | |
| 1038 | 1038 | nlist.n_value = 0; |
| 1039 | 1039 | |
| 1040 | 1040 | if (!macho_file.base.isRelocatable()) { |
| ... | ... | @@ -1098,7 +1098,7 @@ fn createTlvInitializer( |
| 1098 | 1098 | const atom = sym.getAtom(macho_file).?; |
| 1099 | 1099 | sym.out_n_sect = sect_index; |
| 1100 | 1100 | atom.out_n_sect = sect_index; |
| 1101 | atom.flags.alive = true; | |
| 1101 | atom.setAlive(true); | |
| 1102 | 1102 | atom.alignment = alignment; |
| 1103 | 1103 | atom.size = code.len; |
| 1104 | 1104 | nlist.n_sect = sect_index + 1; |
| ... | ... | @@ -1143,7 +1143,7 @@ fn createTlvDescriptor( |
| 1143 | 1143 | |
| 1144 | 1144 | sym.value = 0; |
| 1145 | 1145 | sym.name = try self.strtab.insert(gpa, name); |
| 1146 | atom.flags.alive = true; | |
| 1146 | atom.setAlive(true); | |
| 1147 | 1147 | atom.name = sym.name; |
| 1148 | 1148 | nlist.n_strx = sym.name; |
| 1149 | 1149 | nlist.n_sect = sect_index + 1; |
| ... | ... | @@ -1317,7 +1317,7 @@ fn lowerConst( |
| 1317 | 1317 | self.symtab.items(.size)[sym.nlist_idx] = code.len; |
| 1318 | 1318 | |
| 1319 | 1319 | const atom = sym.getAtom(macho_file).?; |
| 1320 | atom.flags.alive = true; | |
| 1320 | atom.setAlive(true); | |
| 1321 | 1321 | atom.alignment = required_alignment; |
| 1322 | 1322 | atom.size = code.len; |
| 1323 | 1323 | atom.out_n_sect = output_section_index; |
| ... | ... | @@ -1490,7 +1490,7 @@ fn updateLazySymbol( |
| 1490 | 1490 | self.symtab.items(.size)[sym.nlist_idx] = code.len; |
| 1491 | 1491 | |
| 1492 | 1492 | const atom = sym.getAtom(macho_file).?; |
| 1493 | atom.flags.alive = true; | |
| 1493 | atom.setAlive(true); | |
| 1494 | 1494 | atom.name = name_str_index; |
| 1495 | 1495 | atom.alignment = required_alignment; |
| 1496 | 1496 | atom.size = code.len; |
| ... | ... | @@ -1500,7 +1500,7 @@ fn updateLazySymbol( |
| 1500 | 1500 | errdefer self.freeDeclMetadata(macho_file, symbol_index); |
| 1501 | 1501 | |
| 1502 | 1502 | sym.value = 0; |
| 1503 | sym.flags.needs_zig_got = true; | |
| 1503 | sym.setSectionFlags(.{ .needs_zig_got = true }); | |
| 1504 | 1504 | nlist.n_value = 0; |
| 1505 | 1505 | |
| 1506 | 1506 | if (!macho_file.base.isRelocatable()) { |
| ... | ... | @@ -1576,7 +1576,7 @@ pub fn getOrCreateMetadataForDecl( |
| 1576 | 1576 | if (isThreadlocal(macho_file, decl_index)) { |
| 1577 | 1577 | sym.flags.tlv = true; |
| 1578 | 1578 | } else { |
| 1579 | sym.flags.needs_zig_got = true; | |
| 1579 | sym.setSectionFlags(.{ .needs_zig_got = true }); | |
| 1580 | 1580 | } |
| 1581 | 1581 | gop.value_ptr.* = .{ .symbol_index = sym_index }; |
| 1582 | 1582 | } |
| ... | ... | @@ -1611,7 +1611,7 @@ pub fn getOrCreateMetadataForLazySymbol( |
| 1611 | 1611 | .unused => { |
| 1612 | 1612 | const symbol_index = try self.newSymbolWithAtom(gpa, 0, macho_file); |
| 1613 | 1613 | const sym = &self.symbols.items[symbol_index]; |
| 1614 | sym.flags.needs_zig_got = true; | |
| 1614 | sym.setSectionFlags(.{ .needs_zig_got = true }); | |
| 1615 | 1615 | metadata.symbol_index.* = symbol_index; |
| 1616 | 1616 | }, |
| 1617 | 1617 | .pending_flush => return metadata.symbol_index.*, |
src/link/MachO/dead_strip.zig+10-10| ... | ... | @@ -82,9 +82,8 @@ fn markSymbol(sym: *Symbol, roots: *std.ArrayList(*Atom), macho_file: *MachO) !v |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | fn markAtom(atom: *Atom) bool { |
| 85 | const already_visited = atom.flags.visited; | |
| 86 | atom.flags.visited = true; | |
| 87 | return atom.flags.alive and !already_visited; | |
| 85 | const already_visited = atom.visited.swap(true, .seq_cst); | |
| 86 | return atom.isAlive() and !already_visited; | |
| 88 | 87 | } |
| 89 | 88 | |
| 90 | 89 | fn mark(roots: []*Atom, objects: []const File.Index, macho_file: *MachO) void { |
| ... | ... | @@ -105,7 +104,7 @@ fn mark(roots: []*Atom, objects: []const File.Index, macho_file: *MachO) void { |
| 105 | 104 | !(mem.eql(u8, isec.sectName(), "__eh_frame") or |
| 106 | 105 | mem.eql(u8, isec.sectName(), "__compact_unwind") or |
| 107 | 106 | isec.attrs() & macho.S_ATTR_DEBUG != 0) and |
| 108 | !atom.flags.alive and refersLive(atom, macho_file)) | |
| 107 | !atom.isAlive() and refersLive(atom, macho_file)) | |
| 109 | 108 | { |
| 110 | 109 | markLive(atom, macho_file); |
| 111 | 110 | loop = true; |
| ... | ... | @@ -116,8 +115,8 @@ fn mark(roots: []*Atom, objects: []const File.Index, macho_file: *MachO) void { |
| 116 | 115 | } |
| 117 | 116 | |
| 118 | 117 | fn markLive(atom: *Atom, macho_file: *MachO) void { |
| 119 | assert(atom.flags.visited); | |
| 120 | atom.flags.alive = true; | |
| 118 | assert(atom.visited.load(.seq_cst)); | |
| 119 | atom.setAlive(true); | |
| 121 | 120 | track_live_log.debug("{}marking live atom({d},{s})", .{ |
| 122 | 121 | track_live_level, |
| 123 | 122 | atom.atom_index, |
| ... | ... | @@ -170,7 +169,7 @@ fn refersLive(atom: *Atom, macho_file: *MachO) bool { |
| 170 | 169 | }, |
| 171 | 170 | }; |
| 172 | 171 | if (target_atom) |ta| { |
| 173 | if (ta.flags.alive) return true; | |
| 172 | if (ta.isAlive()) return true; | |
| 174 | 173 | } |
| 175 | 174 | } |
| 176 | 175 | return false; |
| ... | ... | @@ -181,9 +180,10 @@ fn prune(objects: []const File.Index, macho_file: *MachO) void { |
| 181 | 180 | const file = macho_file.getFile(index).?; |
| 182 | 181 | for (file.getAtoms()) |atom_index| { |
| 183 | 182 | const atom = file.getAtom(atom_index) orelse continue; |
| 184 | if (atom.flags.alive and !atom.flags.visited) { | |
| 185 | atom.flags.alive = false; | |
| 186 | atom.markUnwindRecordsDead(macho_file); | |
| 183 | if (!atom.visited.load(.seq_cst)) { | |
| 184 | if (atom.alive.cmpxchgStrong(true, false, .seq_cst, .seq_cst) == null) { | |
| 185 | atom.markUnwindRecordsDead(macho_file); | |
| 186 | } | |
| 187 | 187 | } |
| 188 | 188 | } |
| 189 | 189 | } |
src/link/MachO/dyld_info/Rebase.zig+1-1| ... | ... | @@ -35,7 +35,7 @@ pub fn updateSize(rebase: *Rebase, macho_file: *MachO) !void { |
| 35 | 35 | const file = macho_file.getFile(index).?; |
| 36 | 36 | for (file.getAtoms()) |atom_index| { |
| 37 | 37 | const atom = file.getAtom(atom_index) orelse continue; |
| 38 | if (!atom.flags.alive) continue; | |
| 38 | if (!atom.isAlive()) continue; | |
| 39 | 39 | if (atom.getInputSection(macho_file).isZerofill()) continue; |
| 40 | 40 | const atom_addr = atom.getAddress(macho_file); |
| 41 | 41 | const seg_id = macho_file.sections.items(.segment_id)[atom.out_n_sect]; |
src/link/MachO/dyld_info/Trie.zig+3-3| ... | ... | @@ -102,7 +102,7 @@ pub fn updateSize(self: *Trie, macho_file: *MachO) !void { |
| 102 | 102 | if (ref.getFile(macho_file) == null) continue; |
| 103 | 103 | const sym = ref.getSymbol(macho_file).?; |
| 104 | 104 | if (!sym.flags.@"export") continue; |
| 105 | if (sym.getAtom(macho_file)) |atom| if (!atom.flags.alive) continue; | |
| 105 | if (sym.getAtom(macho_file)) |atom| if (!atom.isAlive()) continue; | |
| 106 | 106 | var flags: u64 = if (sym.flags.abs) |
| 107 | 107 | macho.EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE |
| 108 | 108 | else if (sym.flags.tlv) |
| ... | ... | @@ -111,8 +111,8 @@ pub fn updateSize(self: *Trie, macho_file: *MachO) !void { |
| 111 | 111 | macho.EXPORT_SYMBOL_FLAGS_KIND_REGULAR; |
| 112 | 112 | if (sym.flags.weak) { |
| 113 | 113 | flags |= macho.EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION; |
| 114 | macho_file.weak_defines = true; | |
| 115 | macho_file.binds_to_weak = true; | |
| 114 | macho_file.weak_defines.store(true, .seq_cst); | |
| 115 | macho_file.binds_to_weak.store(true, .seq_cst); | |
| 116 | 116 | } |
| 117 | 117 | try self.put(gpa, .{ |
| 118 | 118 | .name = sym.getName(macho_file), |
src/link/MachO/dyld_info/bind.zig+3-6| ... | ... | @@ -10,10 +10,7 @@ pub const Entry = struct { |
| 10 | 10 | if (entry.target.eql(other.target)) { |
| 11 | 11 | return entry.offset < other.offset; |
| 12 | 12 | } |
| 13 | if (entry.target.file == other.target.file) { | |
| 14 | return entry.target.index < other.target.index; | |
| 15 | } | |
| 16 | return entry.target.file < other.target.file; | |
| 13 | return entry.target.lessThan(other.target); | |
| 17 | 14 | } |
| 18 | 15 | return entry.segment_id < other.segment_id; |
| 19 | 16 | } |
| ... | ... | @@ -47,7 +44,7 @@ pub const Bind = struct { |
| 47 | 44 | const file = macho_file.getFile(index).?; |
| 48 | 45 | for (file.getAtoms()) |atom_index| { |
| 49 | 46 | const atom = file.getAtom(atom_index) orelse continue; |
| 50 | if (!atom.flags.alive) continue; | |
| 47 | if (!atom.isAlive()) continue; | |
| 51 | 48 | if (atom.getInputSection(macho_file).isZerofill()) continue; |
| 52 | 49 | const atom_addr = atom.getAddress(macho_file); |
| 53 | 50 | const relocs = atom.getRelocs(macho_file); |
| ... | ... | @@ -299,7 +296,7 @@ pub const WeakBind = struct { |
| 299 | 296 | const file = macho_file.getFile(index).?; |
| 300 | 297 | for (file.getAtoms()) |atom_index| { |
| 301 | 298 | const atom = file.getAtom(atom_index) orelse continue; |
| 302 | if (!atom.flags.alive) continue; | |
| 299 | if (!atom.isAlive()) continue; | |
| 303 | 300 | if (atom.getInputSection(macho_file).isZerofill()) continue; |
| 304 | 301 | const atom_addr = atom.getAddress(macho_file); |
| 305 | 302 | const relocs = atom.getRelocs(macho_file); |
src/link/MachO/file.zig+11-9| ... | ... | @@ -37,11 +37,10 @@ pub const File = union(enum) { |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | pub fn scanRelocs(file: File, macho_file: *MachO) !void { |
| 40 | switch (file) { | |
| 40 | return switch (file) { | |
| 41 | 41 | .dylib => unreachable, |
| 42 | .internal => |x| x.scanRelocs(macho_file), | |
| 43 | 42 | inline else => |x| x.scanRelocs(macho_file), |
| 44 | } | |
| 43 | }; | |
| 45 | 44 | } |
| 46 | 45 | |
| 47 | 46 | /// Encodes symbol rank so that the following ordering applies: |
| ... | ... | @@ -182,19 +181,19 @@ pub const File = union(enum) { |
| 182 | 181 | if (ref.getFile(macho_file) == null) continue; |
| 183 | 182 | if (ref.file != file.getIndex()) continue; |
| 184 | 183 | const sym = ref.getSymbol(macho_file).?; |
| 185 | if (sym.flags.needs_got) { | |
| 184 | if (sym.getSectionFlags().needs_got) { | |
| 186 | 185 | log.debug("'{s}' needs GOT", .{sym.getName(macho_file)}); |
| 187 | 186 | try macho_file.got.addSymbol(ref, macho_file); |
| 188 | 187 | } |
| 189 | if (sym.flags.stubs) { | |
| 188 | if (sym.getSectionFlags().stubs) { | |
| 190 | 189 | log.debug("'{s}' needs STUBS", .{sym.getName(macho_file)}); |
| 191 | 190 | try macho_file.stubs.addSymbol(ref, macho_file); |
| 192 | 191 | } |
| 193 | if (sym.flags.tlv_ptr) { | |
| 192 | if (sym.getSectionFlags().tlv_ptr) { | |
| 194 | 193 | log.debug("'{s}' needs TLV pointer", .{sym.getName(macho_file)}); |
| 195 | 194 | try macho_file.tlv_ptr.addSymbol(ref, macho_file); |
| 196 | 195 | } |
| 197 | if (sym.flags.objc_stubs) { | |
| 196 | if (sym.getSectionFlags().objc_stubs) { | |
| 198 | 197 | log.debug("'{s}' needs OBJC STUBS", .{sym.getName(macho_file)}); |
| 199 | 198 | try macho_file.objc_stubs.addSymbol(ref, macho_file); |
| 200 | 199 | } |
| ... | ... | @@ -268,6 +267,9 @@ pub const File = union(enum) { |
| 268 | 267 | const ref_file = ref.getFile(macho_file) orelse continue; |
| 269 | 268 | if (ref_file.getIndex() == file.getIndex()) continue; |
| 270 | 269 | |
| 270 | macho_file.dupes_mutex.lock(); | |
| 271 | defer macho_file.dupes_mutex.unlock(); | |
| 272 | ||
| 271 | 273 | const gop = try macho_file.dupes.getOrPut(gpa, file.getGlobals()[i]); |
| 272 | 274 | if (!gop.found_existing) { |
| 273 | 275 | gop.value_ptr.* = .{}; |
| ... | ... | @@ -281,7 +283,7 @@ pub const File = union(enum) { |
| 281 | 283 | defer tracy.end(); |
| 282 | 284 | for (file.getAtoms()) |atom_index| { |
| 283 | 285 | const atom = file.getAtom(atom_index) orelse continue; |
| 284 | if (!atom.flags.alive) continue; | |
| 286 | if (!atom.isAlive()) continue; | |
| 285 | 287 | atom.out_n_sect = try Atom.initOutputSection(atom.getInputSection(macho_file), macho_file); |
| 286 | 288 | } |
| 287 | 289 | } |
| ... | ... | @@ -295,7 +297,7 @@ pub const File = union(enum) { |
| 295 | 297 | |
| 296 | 298 | pub fn writeAtoms(file: File, macho_file: *MachO) !void { |
| 297 | 299 | return switch (file) { |
| 298 | .dylib, .zig_object => unreachable, | |
| 300 | .dylib => unreachable, | |
| 299 | 301 | inline else => |x| x.writeAtoms(macho_file), |
| 300 | 302 | }; |
| 301 | 303 | } |
src/link/MachO/relocatable.zig+1-1| ... | ... | @@ -261,7 +261,7 @@ fn initOutputSections(macho_file: *MachO) !void { |
| 261 | 261 | const file = macho_file.getFile(index).?; |
| 262 | 262 | for (file.getAtoms()) |atom_index| { |
| 263 | 263 | const atom = file.getAtom(atom_index) orelse continue; |
| 264 | if (!atom.flags.alive) continue; | |
| 264 | if (!atom.isAlive()) continue; | |
| 265 | 265 | atom.out_n_sect = try Atom.initOutputSection(atom.getInputSection(macho_file), macho_file); |
| 266 | 266 | } |
| 267 | 267 | } |
src/link/MachO/synthetic.zig+4-4| ... | ... | @@ -24,8 +24,8 @@ pub const ZigGotSection = struct { |
| 24 | 24 | const entry = &zig_got.entries.items[index]; |
| 25 | 25 | entry.* = sym_index; |
| 26 | 26 | const symbol = &zo.symbols.items[sym_index]; |
| 27 | assert(symbol.flags.needs_zig_got); | |
| 28 | symbol.flags.has_zig_got = true; | |
| 27 | assert(symbol.getSectionFlags().needs_zig_got); | |
| 28 | symbol.setSectionFlags(.{ .has_zig_got = true }); | |
| 29 | 29 | symbol.addExtra(.{ .zig_got = index }, macho_file); |
| 30 | 30 | return index; |
| 31 | 31 | } |
| ... | ... | @@ -121,7 +121,7 @@ pub const GotSection = struct { |
| 121 | 121 | const entry = try got.symbols.addOne(gpa); |
| 122 | 122 | entry.* = ref; |
| 123 | 123 | const symbol = ref.getSymbol(macho_file).?; |
| 124 | symbol.flags.has_got = true; | |
| 124 | symbol.setSectionFlags(.{ .has_got = true }); | |
| 125 | 125 | symbol.addExtra(.{ .got = index }, macho_file); |
| 126 | 126 | } |
| 127 | 127 | |
| ... | ... | @@ -689,7 +689,7 @@ pub const DataInCode = struct { |
| 689 | 689 | dices[next_dice].offset < end_off) : (next_dice += 1) |
| 690 | 690 | {} |
| 691 | 691 | |
| 692 | if (atom.flags.alive) for (dices[start_dice..next_dice]) |d| { | |
| 692 | if (atom.isAlive()) for (dices[start_dice..next_dice]) |d| { | |
| 693 | 693 | dice.entries.appendAssumeCapacity(.{ |
| 694 | 694 | .atom_ref = .{ .index = atom_index, .file = index }, |
| 695 | 695 | .offset = @intCast(d.offset - start_off), |
src/link/MachO/thunks.zig+3-3| ... | ... | @@ -17,7 +17,7 @@ pub fn createThunks(sect_id: u8, macho_file: *MachO) !void { |
| 17 | 17 | while (i < atoms.len) { |
| 18 | 18 | const start = i; |
| 19 | 19 | const start_atom = atoms[start].getAtom(macho_file).?; |
| 20 | assert(start_atom.flags.alive); | |
| 20 | assert(start_atom.isAlive()); | |
| 21 | 21 | start_atom.value = advance(header, start_atom.size, start_atom.alignment); |
| 22 | 22 | i += 1; |
| 23 | 23 | |
| ... | ... | @@ -25,7 +25,7 @@ pub fn createThunks(sect_id: u8, macho_file: *MachO) !void { |
| 25 | 25 | header.size - start_atom.value < max_allowed_distance) : (i += 1) |
| 26 | 26 | { |
| 27 | 27 | const atom = atoms[i].getAtom(macho_file).?; |
| 28 | assert(atom.flags.alive); | |
| 28 | assert(atom.isAlive()); | |
| 29 | 29 | atom.value = advance(header, atom.size, atom.alignment); |
| 30 | 30 | } |
| 31 | 31 | |
| ... | ... | @@ -71,7 +71,7 @@ fn scanRelocs(thunk_index: Thunk.Index, gpa: Allocator, atoms: []const MachO.Ref |
| 71 | 71 | |
| 72 | 72 | fn isReachable(atom: *const Atom, rel: Relocation, macho_file: *MachO) bool { |
| 73 | 73 | const target = rel.getTargetSymbol(atom.*, macho_file); |
| 74 | if (target.flags.stubs or target.flags.objc_stubs) return false; | |
| 74 | if (target.getSectionFlags().stubs or target.getSectionFlags().objc_stubs) return false; | |
| 75 | 75 | if (atom.out_n_sect != target.getOutputSectionIndex(macho_file)) return false; |
| 76 | 76 | const target_atom = target.getAtom(macho_file).?; |
| 77 | 77 | if (target_atom.value == @as(u64, @bitCast(@as(i64, -1)))) return false; |