authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-04-22 00:51:14+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-04-22 12:21:37+02:00
loga7e4d1722616ba9fabfae8b830902e3651c645e4
tree8e43482601ef089420de83484f2d43f33e5e2ced
parent3c5e840732053318f5e722d6cc16f65d51cdc297

link/macho: introduce Atom extras for out-of-band data


6 files changed, 157 insertions(+), 41 deletions(-)

src/link/MachO.zig+47
......@@ -65,6 +65,7 @@ entry_index: ?Symbol.Index = null,
6565
6666/// List of atoms that are either synthetic or map directly to the Zig source program.
6767atoms: std.ArrayListUnmanaged(Atom) = .{},
68atoms_extra: std.ArrayListUnmanaged(u32) = .{},
6869thunks: std.ArrayListUnmanaged(Thunk) = .{},
6970unwind_records: std.ArrayListUnmanaged(UnwindInfo.Record) = .{},
7071
......@@ -246,6 +247,7 @@ pub fn createEmpty(
246247 try self.files.append(gpa, .null);
247248 // Atom at index 0 is reserved as null atom
248249 try self.atoms.append(gpa, .{});
250 try self.atoms_extra.append(gpa, 0);
249251 // Append empty string to string tables
250252 try self.strings.buffer.append(gpa, 0);
251253 try self.strtab.append(gpa, 0);
......@@ -350,6 +352,7 @@ pub fn deinit(self: *MachO) void {
350352 self.unwind_info.deinit(gpa);
351353
352354 self.atoms.deinit(gpa);
355 self.atoms_extra.deinit(gpa);
353356 for (self.thunks.items) |*thunk| {
354357 thunk.deinit(gpa);
355358 }
......@@ -3865,6 +3868,50 @@ pub fn getAtom(self: *MachO, index: Atom.Index) ?*Atom {
38653868 return &self.atoms.items[index];
38663869}
38673870
3871pub fn addAtomExtra(self: *MachO, extra: Atom.Extra) !u32 {
3872 const fields = @typeInfo(Atom.Extra).Struct.fields;
3873 try self.atoms_extra.ensureUnusedCapacity(self.base.comp.gpa, fields.len);
3874 return self.addAtomExtraAssumeCapacity(extra);
3875}
3876
3877pub fn addAtomExtraAssumeCapacity(self: *MachO, extra: Atom.Extra) u32 {
3878 const index = @as(u32, @intCast(self.atoms_extra.items.len));
3879 const fields = @typeInfo(Atom.Extra).Struct.fields;
3880 inline for (fields) |field| {
3881 self.atoms_extra.appendAssumeCapacity(switch (field.type) {
3882 u32 => @field(extra, field.name),
3883 else => @compileError("bad field type"),
3884 });
3885 }
3886 return index;
3887}
3888
3889pub fn getAtomExtra(self: *MachO, index: u32) ?Atom.Extra {
3890 if (index == 0) return null;
3891 const fields = @typeInfo(Atom.Extra).Struct.fields;
3892 var i: usize = index;
3893 var result: Atom.Extra = undefined;
3894 inline for (fields) |field| {
3895 @field(result, field.name) = switch (field.type) {
3896 u32 => self.atoms_extra.items[i],
3897 else => @compileError("bad field type"),
3898 };
3899 i += 1;
3900 }
3901 return result;
3902}
3903
3904pub fn setAtomExtra(self: *MachO, index: u32, extra: Atom.Extra) void {
3905 assert(index > 0);
3906 const fields = @typeInfo(Atom.Extra).Struct.fields;
3907 inline for (fields, 0..) |field, i| {
3908 self.atoms_extra.items[index + i] = switch (field.type) {
3909 u32 => @field(extra, field.name),
3910 else => @compileError("bad field type"),
3911 };
3912 }
3913}
3914
38683915pub fn addSymbol(self: *MachO) !Symbol.Index {
38693916 const index = @as(Symbol.Index, @intCast(self.symbols.items.len));
38703917 const symbol = try self.symbols.addOne(self.base.comp.gpa);
src/link/MachO/Atom.zig+80-26
......@@ -23,18 +23,9 @@ out_n_sect: u8 = 0,
2323/// off + size <= parent section size.
2424off: u64 = 0,
2525
26/// Relocations of this atom.
27relocs: Loc = .{},
28
2926/// Index of this atom in the linker's atoms table.
3027atom_index: Index = 0,
3128
32/// Index of the thunk for this atom.
33thunk_index: Thunk.Index = 0,
34
35/// Unwind records associated with this atom.
36unwind_records: Loc = .{},
37
3829flags: Flags = .{},
3930
4031/// Points to the previous and next neighbors, based on the `text_offset`.
......@@ -42,6 +33,8 @@ flags: Flags = .{},
4233prev_index: Index = 0,
4334next_index: Index = 0,
4435
36extra: u32 = 0,
37
4538pub fn getName(self: Atom, macho_file: *MachO) [:0]const u8 {
4639 return switch (self.getFile(macho_file)) {
4740 .dylib => unreachable,
......@@ -67,7 +60,7 @@ pub fn getData(self: Atom, macho_file: *MachO, buffer: []u8) !void {
6760pub fn getRelocs(self: Atom, macho_file: *MachO) []const Relocation {
6861 return switch (self.getFile(macho_file)) {
6962 .dylib => unreachable,
70 inline else => |x| x.getAtomRelocs(self),
63 inline else => |x| x.getAtomRelocs(self, macho_file),
7164 };
7265}
7366
......@@ -95,10 +88,11 @@ pub fn getPriority(self: Atom, macho_file: *MachO) u64 {
9588}
9689
9790pub fn getUnwindRecords(self: Atom, macho_file: *MachO) []const UnwindInfo.Record.Index {
91 if (!self.flags.unwind) return &[0]UnwindInfo.Record.Index{};
92 const extra = self.getExtra(macho_file).?;
9893 return switch (self.getFile(macho_file)) {
99 .dylib => unreachable,
100 .zig_object, .internal => &[0]UnwindInfo.Record.Index{},
101 .object => |x| x.unwind_records.items[self.unwind_records.pos..][0..self.unwind_records.len],
94 .dylib, .zig_object, .internal => unreachable,
95 .object => |x| x.unwind_records.items[extra.unwind_index..][0..extra.unwind_count],
10296 };
10397}
10498
......@@ -114,7 +108,38 @@ pub fn markUnwindRecordsDead(self: Atom, macho_file: *MachO) void {
114108}
115109
116110pub fn getThunk(self: Atom, macho_file: *MachO) *Thunk {
117 return macho_file.getThunk(self.thunk_index);
111 assert(self.flags.thunk);
112 const extra = self.getExtra(macho_file).?;
113 return macho_file.getThunk(extra.thunk);
114}
115
116const AddExtraOpts = struct {
117 thunk: ?u32 = null,
118 rel_index: ?u32 = null,
119 rel_count: ?u32 = null,
120 unwind_index: ?u32 = null,
121 unwind_count: ?u32 = null,
122};
123
124pub fn addExtra(atom: *Atom, opts: AddExtraOpts, macho_file: *MachO) !void {
125 if (atom.getExtra(macho_file) == null) {
126 atom.extra = try macho_file.addAtomExtra(.{});
127 }
128 var extra = atom.getExtra(macho_file).?;
129 inline for (@typeInfo(@TypeOf(opts)).Struct.fields) |field| {
130 if (@field(opts, field.name)) |x| {
131 @field(extra, field.name) = x;
132 }
133 }
134 atom.setExtra(extra, macho_file);
135}
136
137pub inline fn getExtra(atom: Atom, macho_file: *MachO) ?Extra {
138 return macho_file.getAtomExtra(atom.extra);
139}
140
141pub inline fn setExtra(atom: Atom, extra: Extra, macho_file: *MachO) void {
142 macho_file.setAtomExtra(atom.extra, extra);
118143}
119144
120145pub fn initOutputSection(sect: macho.section_64, macho_file: *MachO) !u8 {
......@@ -403,14 +428,20 @@ pub fn addReloc(self: *Atom, macho_file: *MachO, reloc: Relocation) !void {
403428 const gpa = macho_file.base.comp.gpa;
404429 const file = self.getFile(macho_file);
405430 assert(file == .zig_object);
406 const rels = &file.zig_object.relocs.items[self.relocs.pos];
431 assert(self.flags.relocs);
432 var extra = self.getExtra(macho_file).?;
433 const rels = &file.zig_object.relocs.items[extra.rel_index];
407434 try rels.append(gpa, reloc);
408 self.relocs.len += 1;
435 extra.rel_count += 1;
436 self.setExtra(extra, macho_file);
409437}
410438
411439pub fn freeRelocs(self: *Atom, macho_file: *MachO) void {
412 self.getFile(macho_file).zig_object.freeAtomRelocs(self.*);
413 self.relocs.len = 0;
440 if (!self.flags.relocs) return;
441 self.getFile(macho_file).zig_object.freeAtomRelocs(self.*, macho_file);
442 var extra = self.getExtra(macho_file).?;
443 extra.rel_count = 0;
444 self.setExtra(extra, macho_file);
414445}
415446
416447pub fn scanRelocs(self: Atom, macho_file: *MachO) !void {
......@@ -1117,19 +1148,21 @@ fn format2(
11171148 _ = unused_fmt_string;
11181149 const atom = ctx.atom;
11191150 const macho_file = ctx.macho_file;
1120 try writer.print("atom({d}) : {s} : @{x} : sect({d}) : align({x}) : size({x}) : nreloc({d}) : thunk({d})", .{
1151 try writer.print("atom({d}) : {s} : @{x} : sect({d}) : align({x}) : size({x}) : nreloc({d})", .{
11211152 atom.atom_index, atom.getName(macho_file), atom.getAddress(macho_file),
11221153 atom.out_n_sect, atom.alignment, atom.size,
1123 atom.getRelocs(macho_file).len, atom.thunk_index,
1154 atom.getRelocs(macho_file).len,
11241155 });
1156 if (atom.flags.thunk) try writer.print(" : thunk({d})", .{atom.getExtra(macho_file).?.thunk});
11251157 if (!atom.flags.alive) try writer.writeAll(" : [*]");
1126 if (atom.unwind_records.len > 0) {
1158 if (atom.flags.unwind) {
11271159 try writer.writeAll(" : unwind{ ");
1128 for (atom.getUnwindRecords(macho_file), atom.unwind_records.pos..) |index, i| {
1160 const extra = atom.getExtra(macho_file).?;
1161 for (atom.getUnwindRecords(macho_file), extra.unwind_index..) |index, i| {
11291162 const rec = macho_file.getUnwindRecord(index);
11301163 try writer.print("{d}", .{index});
11311164 if (!rec.alive) try writer.writeAll("([*])");
1132 if (i < atom.unwind_records.pos + atom.unwind_records.len - 1) try writer.writeAll(", ");
1165 if (i < extra.unwind_index + extra.unwind_count - 1) try writer.writeAll(", ");
11331166 }
11341167 try writer.writeAll(" }");
11351168 }
......@@ -1143,11 +1176,32 @@ pub const Flags = packed struct {
11431176
11441177 /// Specifies if the atom has been visited during garbage collection.
11451178 visited: bool = false,
1179
1180 /// Whether this atom has a range extension thunk.
1181 thunk: bool = false,
1182
1183 /// Whether this atom has any relocations.
1184 relocs: bool = false,
1185
1186 /// Whether this atom has any unwind records.
1187 unwind: bool = false,
11461188};
11471189
1148pub const Loc = struct {
1149 pos: u32 = 0,
1150 len: u32 = 0,
1190pub const Extra = struct {
1191 /// Index of the range extension thunk of this atom.
1192 thunk: u32 = 0,
1193
1194 /// Start index of relocations belonging to this atom.
1195 rel_index: u32 = 0,
1196
1197 /// Count of relocations belonging to this atom.
1198 rel_count: u32 = 0,
1199
1200 /// Start index of relocations belonging to this atom.
1201 unwind_index: u32 = 0,
1202
1203 /// Count of relocations belonging to this atom.
1204 unwind_count: u32 = 0,
11511205};
11521206
11531207pub const Alignment = @import("../../InternPool.zig").Alignment;
src/link/MachO/InternalObject.zig+6-3
......@@ -115,7 +115,8 @@ fn addObjcSelrefsSection(
115115 .has_subtractor = false,
116116 },
117117 });
118 atom.relocs = .{ .pos = 0, .len = 1 };
118 try atom.addExtra(.{ .rel_index = 0, .rel_count = 1 }, macho_file);
119 atom.flags.relocs = true;
119120 self.num_rebase_relocs += 1;
120121
121122 return atom_index;
......@@ -183,9 +184,11 @@ pub fn getAtomData(self: *const InternalObject, atom: Atom, buffer: []u8) !void
183184 @memcpy(buffer, data[off..][0..size]);
184185}
185186
186pub fn getAtomRelocs(self: *const InternalObject, atom: Atom) []const Relocation {
187pub fn getAtomRelocs(self: *const InternalObject, atom: Atom, macho_file: *MachO) []const Relocation {
188 if (!atom.flags.relocs) return &[0]Relocation{};
189 const extra = atom.getExtra(macho_file).?;
187190 const relocs = self.sections.items(.relocs)[atom.n_sect];
188 return relocs.items[atom.relocs.pos..][0..atom.relocs.len];
191 return relocs.items[extra.rel_index..][0..extra.rel_count];
189192}
190193
191194fn addString(self: *InternalObject, allocator: Allocator, name: [:0]const u8) error{OutOfMemory}!u32 {
src/link/MachO/Object.zig+10-5
......@@ -690,11 +690,13 @@ fn initRelocs(self: *Object, macho_file: *MachO) !void {
690690 if (!atom.flags.alive) continue;
691691 if (next_reloc >= relocs.items.len) break;
692692 const end_addr = atom.off + atom.size;
693 atom.relocs.pos = next_reloc;
693 const rel_index = next_reloc;
694694
695695 while (next_reloc < relocs.items.len and relocs.items[next_reloc].offset < end_addr) : (next_reloc += 1) {}
696696
697 atom.relocs.len = next_reloc - atom.relocs.pos;
697 const rel_count = next_reloc - rel_index;
698 try atom.addExtra(.{ .rel_index = rel_index, .rel_count = rel_count }, macho_file);
699 atom.flags.relocs = true;
698700 }
699701 }
700702}
......@@ -1004,7 +1006,8 @@ fn parseUnwindRecords(self: *Object, macho_file: *MachO) !void {
10041006 {}
10051007
10061008 const atom = rec.getAtom(macho_file);
1007 atom.unwind_records = .{ .pos = start, .len = next_cu - start };
1009 try atom.addExtra(.{ .unwind_index = start, .unwind_count = next_cu - start }, macho_file);
1010 atom.flags.unwind = true;
10081011 }
10091012}
10101013
......@@ -1724,9 +1727,11 @@ pub fn getAtomData(self: *const Object, macho_file: *MachO, atom: Atom, buffer:
17241727 if (amt != buffer.len) return error.InputOutput;
17251728}
17261729
1727pub fn getAtomRelocs(self: *const Object, atom: Atom) []const Relocation {
1730pub fn getAtomRelocs(self: *const Object, atom: Atom, macho_file: *MachO) []const Relocation {
1731 if (!atom.flags.relocs) return &[0]Relocation{};
1732 const extra = atom.getExtra(macho_file).?;
17281733 const relocs = self.sections.items(.relocs)[atom.n_sect];
1729 return relocs.items[atom.relocs.pos..][0..atom.relocs.len];
1734 return relocs.items[extra.rel_index..][0..extra.rel_count];
17301735}
17311736
17321737fn addString(self: *Object, allocator: Allocator, name: [:0]const u8) error{OutOfMemory}!u32 {
src/link/MachO/ZigObject.zig+12-6
......@@ -163,7 +163,8 @@ pub fn addAtom(self: *ZigObject, macho_file: *MachO) !Symbol.Index {
163163 const relocs_index = @as(u32, @intCast(self.relocs.items.len));
164164 const relocs = try self.relocs.addOne(gpa);
165165 relocs.* = .{};
166 atom.relocs = .{ .pos = relocs_index, .len = 0 };
166 try atom.addExtra(.{ .rel_index = relocs_index, .rel_count = 0 }, macho_file);
167 atom.flags.relocs = true;
167168
168169 return symbol_index;
169170}
......@@ -190,13 +191,18 @@ pub fn getAtomData(self: ZigObject, macho_file: *MachO, atom: Atom, buffer: []u8
190191 }
191192}
192193
193pub fn getAtomRelocs(self: *ZigObject, atom: Atom) []const Relocation {
194 const relocs = self.relocs.items[atom.relocs.pos];
195 return relocs.items[0..atom.relocs.len];
194pub fn getAtomRelocs(self: *ZigObject, atom: Atom, macho_file: *MachO) []const Relocation {
195 if (!atom.flags.relocs) return &[0]Relocation{};
196 const extra = atom.getExtra(macho_file).?;
197 const relocs = self.relocs.items[extra.rel_index];
198 return relocs.items[0..extra.rel_count];
196199}
197200
198pub fn freeAtomRelocs(self: *ZigObject, atom: Atom) void {
199 self.relocs.items[atom.relocs.pos].clearRetainingCapacity();
201pub fn freeAtomRelocs(self: *ZigObject, atom: Atom, macho_file: *MachO) void {
202 if (atom.flags.relocs) {
203 const extra = atom.getExtra(macho_file).?;
204 self.relocs.items[extra.rel_index].clearRetainingCapacity();
205 }
200206}
201207
202208pub fn resolveSymbols(self: *ZigObject, macho_file: *MachO) void {
src/link/MachO/thunks.zig+2-1
......@@ -43,7 +43,8 @@ pub fn createThunks(sect_id: u8, macho_file: *MachO) !void {
4343 if (isReachable(atom, rel, macho_file)) continue;
4444 try thunk.symbols.put(gpa, rel.target, {});
4545 }
46 atom.thunk_index = thunk_index;
46 try atom.addExtra(.{ .thunk = thunk_index }, macho_file);
47 atom.flags.thunk = true;
4748 }
4849
4950 thunk.value = try advance(header, thunk.size(), .@"4");