authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-11-10 12:52:52+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-11-10 12:52:56+01:00
log0f2489d8fc314c67bec7d77832cb6ba773f48738
treed70f526927583c469cce908851102714e1701ecc
parent03adafd8023691af6a1e3d784a6e7e1f77d46859

macho: resolve special section/segment boundary symbols

Boundary symbols have a special name prefix: * section$start$segname$sectname * section$stop$segname$sectname * segment$start$segname * segment$stop$segname and will resolve to either start or end of the respective section/segment if found. If not found, we return an error stating we couldn't find the requested section/segment rather than silently failing and resolving the address to 0 which seems to be the case with Apple's ld64.

2 files changed, 164 insertions(+), 5 deletions(-)

src/link/MachO.zig+163-5
......@@ -1416,6 +1416,51 @@ pub fn allocateSpecialSymbols(self: *MachO) !void {
14161416 seg.segName(),
14171417 });
14181418 }
1419
1420 for (self.globals.items) |global| {
1421 const sym = self.getSymbolPtr(global);
1422 if (sym.n_desc != N_BOUNDARY) continue;
1423 if (self.getSectionBoundarySymbol(global)) |bsym| {
1424 const sect_id = self.getSectionByName(bsym.segname, bsym.sectname) orelse {
1425 try self.reportUnresolvedBoundarySymbol(self.getSymbolName(global), "section not found: {s},{s}", .{
1426 bsym.segname, bsym.sectname,
1427 });
1428 continue;
1429 };
1430 const sect = self.sections.items(.header)[sect_id];
1431 sym.n_sect = sect_id + 1;
1432 sym.n_value = switch (bsym.kind) {
1433 .start => sect.addr,
1434 .stop => sect.addr + sect.size,
1435 };
1436
1437 log.debug("allocating {s} at @0x{x} sect({d})", .{
1438 self.getSymbolName(global),
1439 sym.n_value,
1440 sym.n_sect,
1441 });
1442
1443 continue;
1444 }
1445 if (self.getSegmentBoundarySymbol(global)) |bsym| {
1446 const seg_id = self.getSegmentByName(bsym.segname) orelse {
1447 try self.reportUnresolvedBoundarySymbol(self.getSymbolName(global), "segment not found: {s}", .{
1448 bsym.segname,
1449 });
1450
1451 continue;
1452 };
1453 const seg = self.segments.items[seg_id];
1454 sym.n_value = switch (bsym.kind) {
1455 .start => seg.vmaddr,
1456 .stop => seg.vmaddr + seg.vmsize,
1457 };
1458
1459 log.debug("allocating {s} at @0x{x} ", .{ self.getSymbolName(global), sym.n_value });
1460
1461 continue;
1462 }
1463 }
14191464}
14201465
14211466const CreateAtomOpts = struct {
......@@ -1442,6 +1487,7 @@ pub fn createTentativeDefAtoms(self: *MachO) !void {
14421487 const sym = self.getSymbolPtr(global);
14431488 if (!sym.tentative()) continue;
14441489 if (sym.n_desc == N_DEAD) continue;
1490 if (sym.n_desc == N_BOUNDARY) continue;
14451491
14461492 log.debug("creating tentative definition for ATOM(%{d}, '{s}') in object({?})", .{
14471493 global.sym_index, self.getSymbolName(global), global.file,
......@@ -1630,6 +1676,13 @@ pub fn resolveSymbols(self: *MachO) !void {
16301676 try self.createMhExecuteHeaderSymbol();
16311677 try self.createDsoHandleSymbol();
16321678 try self.resolveSymbolsAtLoading();
1679
1680 // Final stop, check if unresolved contain any of the special magic boundary symbols
1681 // * section$start$
1682 // * section$stop$
1683 // * segment$start$
1684 // * segment$stop$
1685 try self.resolveBoundarySymbols();
16331686}
16341687
16351688fn resolveGlobalSymbol(self: *MachO, current: SymbolWithLoc) !void {
......@@ -1845,6 +1898,34 @@ fn resolveSymbolsAtLoading(self: *MachO) !void {
18451898 }
18461899}
18471900
1901fn resolveBoundarySymbols(self: *MachO) !void {
1902 var next_sym: usize = 0;
1903 while (next_sym < self.unresolved.count()) {
1904 const global_index = self.unresolved.keys()[next_sym];
1905 const global = &self.globals.items[global_index];
1906
1907 if (self.getSectionBoundarySymbol(global.*) != null or self.getSegmentBoundarySymbol(global.*) != null) {
1908 const sym_index = try self.allocateSymbol();
1909 const sym_loc = SymbolWithLoc{ .sym_index = sym_index };
1910 const sym = self.getSymbolPtr(sym_loc);
1911 sym.* = .{
1912 .n_strx = try self.strtab.insert(self.base.allocator, self.getSymbolName(global.*)),
1913 .n_type = macho.N_SECT | macho.N_EXT,
1914 .n_sect = 0,
1915 .n_desc = N_BOUNDARY,
1916 .n_value = 0,
1917 };
1918 if (global.getFile()) |file| {
1919 const global_object = &self.objects.items[file];
1920 global_object.globals_lookup[global.sym_index] = global_index;
1921 }
1922 global.* = sym_loc;
1923 _ = self.unresolved.swapRemove(global_index);
1924 continue;
1925 }
1926 }
1927}
1928
18481929pub fn deinit(self: *MachO) void {
18491930 const gpa = self.base.allocator;
18501931
......@@ -3565,6 +3646,7 @@ fn collectRebaseData(self: *MachO, rebase: *Rebase) !void {
35653646 const atom = self.getAtom(atom_index);
35663647 const sym = self.getSymbol(atom.getSymbolWithLoc());
35673648 if (sym.n_desc == N_DEAD) continue;
3649 if (sym.n_desc == N_BOUNDARY) continue;
35683650
35693651 const sect_id = sym.n_sect - 1;
35703652 const section = self.sections.items(.header)[sect_id];
......@@ -3719,6 +3801,7 @@ fn collectBindData(self: *MachO, bind: anytype, raw_bindings: anytype) !void {
37193801 const atom = self.getAtom(atom_index);
37203802 const sym = self.getSymbol(atom.getSymbolWithLoc());
37213803 if (sym.n_desc == N_DEAD) continue;
3804 if (sym.n_desc == N_BOUNDARY) continue;
37223805
37233806 const sect_id = sym.n_sect - 1;
37243807 const section = self.sections.items(.header)[sect_id];
......@@ -3819,6 +3902,7 @@ fn collectExportData(self: *MachO, trie: *Trie) !void {
38193902 if (sym.undf()) continue;
38203903 assert(sym.ext());
38213904 if (sym.n_desc == N_DEAD) continue;
3905 if (sym.n_desc == N_BOUNDARY) continue;
38223906
38233907 const sym_name = self.getSymbolName(global);
38243908 log.debug(" (putting '{s}' defined at 0x{x})", .{ sym_name, sym.n_value });
......@@ -3953,7 +4037,8 @@ const asc_u64 = std.sort.asc(u64);
39534037fn addSymbolToFunctionStarts(self: *MachO, sym_loc: SymbolWithLoc, addresses: *std.ArrayList(u64)) !void {
39544038 const sym = self.getSymbol(sym_loc);
39554039 if (sym.n_strx == 0) return;
3956 if (sym.n_desc == MachO.N_DEAD) return;
4040 if (sym.n_desc == N_DEAD) return;
4041 if (sym.n_desc == N_BOUNDARY) return;
39574042 if (self.symbolIsTemp(sym_loc)) return;
39584043 try addresses.append(sym.n_value);
39594044}
......@@ -4061,7 +4146,8 @@ pub fn writeDataInCode(self: *MachO) !void {
40614146 for (object.exec_atoms.items) |atom_index| {
40624147 const atom = self.getAtom(atom_index);
40634148 const sym = self.getSymbol(atom.getSymbolWithLoc());
4064 if (sym.n_desc == MachO.N_DEAD) continue;
4149 if (sym.n_desc == N_DEAD) continue;
4150 if (sym.n_desc == N_BOUNDARY) return;
40654151
40664152 const source_addr = if (object.getSourceSymbol(atom.sym_index)) |source_sym|
40674153 source_sym.n_value
......@@ -4119,7 +4205,8 @@ fn writeSymtabs(self: *MachO) !void {
41194205fn addLocalToSymtab(self: *MachO, sym_loc: SymbolWithLoc, locals: *std.ArrayList(macho.nlist_64)) !void {
41204206 const sym = self.getSymbol(sym_loc);
41214207 if (sym.n_strx == 0) return; // no name, skip
4122 if (sym.n_desc == MachO.N_DEAD) return; // garbage-collected, skip
4208 if (sym.n_desc == N_DEAD) return; // garbage-collected, skip
4209 if (sym.n_desc == N_BOUNDARY) return; // boundary symbol, skip
41234210 if (sym.ext()) return; // an export lands in its own symtab section, skip
41244211 if (self.symbolIsTemp(sym_loc)) return; // local temp symbol, skip
41254212 var out_sym = sym;
......@@ -4157,6 +4244,7 @@ fn writeSymtab(self: *MachO) !SymtabCtx {
41574244 const sym = self.getSymbol(global);
41584245 if (sym.undf()) continue; // import, skip
41594246 if (sym.n_desc == N_DEAD) continue;
4247 if (sym.n_desc == N_BOUNDARY) continue;
41604248 var out_sym = sym;
41614249 out_sym.n_strx = try self.strtab.insert(gpa, self.getSymbolName(global));
41624250 try exports.append(out_sym);
......@@ -4172,6 +4260,7 @@ fn writeSymtab(self: *MachO) !SymtabCtx {
41724260 if (sym.n_strx == 0) continue; // no name, skip
41734261 if (!sym.undf()) continue; // not an import, skip
41744262 if (sym.n_desc == N_DEAD) continue;
4263 if (sym.n_desc == N_BOUNDARY) continue;
41754264 const new_index = @as(u32, @intCast(imports.items.len));
41764265 var out_sym = sym;
41774266 out_sym.n_strx = try self.strtab.insert(gpa, self.getSymbolName(global));
......@@ -4842,6 +4931,55 @@ pub fn getSymbolName(self: *const MachO, sym_with_loc: SymbolWithLoc) []const u8
48424931 }
48434932}
48444933
4934const BoundarySymbolKind = enum {
4935 start,
4936 stop,
4937};
4938
4939const SectionBoundarySymbol = struct {
4940 kind: BoundarySymbolKind,
4941 segname: []const u8,
4942 sectname: []const u8,
4943};
4944
4945pub fn getSectionBoundarySymbol(self: *const MachO, sym_with_loc: SymbolWithLoc) ?SectionBoundarySymbol {
4946 const sym_name = self.getSymbolName(sym_with_loc);
4947 if (mem.startsWith(u8, sym_name, "section$")) {
4948 const trailing = sym_name["section$".len..];
4949 const kind: BoundarySymbolKind = kind: {
4950 if (mem.startsWith(u8, trailing, "start$")) break :kind .start;
4951 if (mem.startsWith(u8, trailing, "stop$")) break :kind .stop;
4952 return null;
4953 };
4954 const names = trailing[@tagName(kind).len + 1 ..];
4955 const sep_idx = mem.indexOf(u8, names, "$") orelse return null;
4956 const segname = names[0..sep_idx];
4957 const sectname = names[sep_idx + 1 ..];
4958 return .{ .kind = kind, .segname = segname, .sectname = sectname };
4959 }
4960 return null;
4961}
4962
4963const SegmentBoundarySymbol = struct {
4964 kind: BoundarySymbolKind,
4965 segname: []const u8,
4966};
4967
4968pub fn getSegmentBoundarySymbol(self: *const MachO, sym_with_loc: SymbolWithLoc) ?SegmentBoundarySymbol {
4969 const sym_name = self.getSymbolName(sym_with_loc);
4970 if (mem.startsWith(u8, sym_name, "segment$")) {
4971 const trailing = sym_name["segment$".len..];
4972 const kind: BoundarySymbolKind = kind: {
4973 if (mem.startsWith(u8, trailing, "start$")) break :kind .start;
4974 if (mem.startsWith(u8, trailing, "stop$")) break :kind .stop;
4975 return null;
4976 };
4977 const segname = trailing[@tagName(kind).len + 1 ..];
4978 return .{ .kind = kind, .segname = segname };
4979 }
4980 return null;
4981}
4982
48454983/// Returns pointer to the global entry for `name` if one exists.
48464984pub fn getGlobalPtr(self: *MachO, name: []const u8) ?*SymbolWithLoc {
48474985 const global_index = self.resolver.get(name) orelse return null;
......@@ -5137,6 +5275,23 @@ pub fn reportParseError(
51375275 });
51385276}
51395277
5278pub fn reportUnresolvedBoundarySymbol(
5279 self: *MachO,
5280 sym_name: []const u8,
5281 comptime format: []const u8,
5282 args: anytype,
5283) error{OutOfMemory}!void {
5284 const gpa = self.base.allocator;
5285 try self.misc_errors.ensureUnusedCapacity(gpa, 1);
5286 var notes = try gpa.alloc(File.ErrorMsg, 1);
5287 errdefer gpa.free(notes);
5288 notes[0] = .{ .msg = try std.fmt.allocPrint(gpa, "while resolving {s}", .{sym_name}) };
5289 self.misc_errors.appendAssumeCapacity(.{
5290 .msg = try std.fmt.allocPrint(gpa, format, args),
5291 .notes = notes,
5292 });
5293}
5294
51405295pub fn reportUndefined(self: *MachO) error{OutOfMemory}!void {
51415296 const gpa = self.base.allocator;
51425297 const count = self.unresolved.count();
......@@ -5340,7 +5495,8 @@ pub fn logSymtab(self: *MachO) void {
53405495 for (self.globals.items, 0..) |global, i| {
53415496 const sym = self.getSymbol(global);
53425497 if (sym.undf()) continue;
5343 if (sym.n_desc == MachO.N_DEAD) continue;
5498 if (sym.n_desc == N_DEAD) continue;
5499 if (sym.n_desc == N_BOUNDARY) continue;
53445500 scoped_log.debug(" %{d}: {s} @{x} in sect({d}), {s} (def in object({?}))", .{
53455501 i,
53465502 self.getSymbolName(global),
......@@ -5355,7 +5511,8 @@ pub fn logSymtab(self: *MachO) void {
53555511 for (self.globals.items, 0..) |global, i| {
53565512 const sym = self.getSymbol(global);
53575513 if (!sym.undf()) continue;
5358 if (sym.n_desc == MachO.N_DEAD) continue;
5514 if (sym.n_desc == N_DEAD) continue;
5515 if (sym.n_desc == N_BOUNDARY) continue;
53595516 const ord = @divTrunc(sym.n_desc, macho.N_SYMBOL_RESOLVER);
53605517 scoped_log.debug(" %{d}: {s} @{x} in ord({d}), {s}", .{
53615518 i,
......@@ -5466,6 +5623,7 @@ pub fn logAtom(self: *MachO, atom_index: Atom.Index, logger: anytype) void {
54665623
54675624pub const base_tag: File.Tag = File.Tag.macho;
54685625pub const N_DEAD: u16 = @as(u16, @bitCast(@as(i16, -1)));
5626pub const N_BOUNDARY: u16 = @as(u16, @bitCast(@as(i16, -2)));
54695627
54705628/// Mode of operation of the linker.
54715629pub const Mode = enum {
src/link/MachO/dead_strip.zig+1
......@@ -50,6 +50,7 @@ fn collectRoots(macho_file: *MachO, roots: *AtomTable) !void {
5050 for (macho_file.globals.items) |global| {
5151 const sym = macho_file.getSymbol(global);
5252 if (sym.undf()) continue;
53 if (sym.n_desc == MachO.N_BOUNDARY) continue;
5354
5455 if (global.getFile()) |file| {
5556 try addRoot(macho_file, roots, file, global);