1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
const std = @import("std");
const assert = std.debug.assert;
const log = std.log.scoped(.dead_strip);
const macho = std.macho;
const math = std.math;
const mem = std.mem;
const Allocator = mem.Allocator;
const Atom = @import("Atom.zig");
const MachO = @import("../MachO.zig");
pub fn gcAtoms(macho_file: *MachO) !void {
const gpa = macho_file.base.allocator;
var arena_allocator = std.heap.ArenaAllocator.init(gpa);
defer arena_allocator.deinit();
const arena = arena_allocator.allocator();
var roots = std.AutoHashMap(*Atom, void).init(arena);
try collectRoots(&roots, macho_file);
var alive = std.AutoHashMap(*Atom, void).init(arena);
try mark(roots, &alive, macho_file);
try prune(arena, alive, macho_file);
}
fn removeAtomFromSection(atom: *Atom, match: u8, macho_file: *MachO) void {
var section = macho_file.sections.get(match);
// If we want to enable GC for incremental codepath, we need to take into
// account any padding that might have been left here.
section.header.size -= atom.size;
if (atom.prev) |prev| {
prev.next = atom.next;
}
if (atom.next) |next| {
next.prev = atom.prev;
} else {
if (atom.prev) |prev| {
section.last_atom = prev;
} else {
// The section will be GCed in the next step.
section.last_atom = null;
section.header.size = 0;
}
}
macho_file.sections.set(match, section);
}
fn collectRoots(roots: *std.AutoHashMap(*Atom, void), macho_file: *MachO) !void {
const output_mode = macho_file.base.options.output_mode;
switch (output_mode) {
.Exe => {
// Add entrypoint as GC root
const global = try macho_file.getEntryPoint();
const atom = macho_file.getAtomForSymbol(global).?; // panic here means fatal error
_ = try roots.getOrPut(atom);
},
else => |other| {
assert(other == .Lib);
// Add exports as GC roots
for (macho_file.globals.items) |global| {
const sym = macho_file.getSymbol(global);
if (!sym.sect()) continue;
const atom = macho_file.getAtomForSymbol(global) orelse {
log.debug("skipping {s}", .{macho_file.getSymbolName(global)});
continue;
};
_ = try roots.getOrPut(atom);
log.debug("adding root", .{});
macho_file.logAtom(atom);
}
},
}
// TODO just a temp until we learn how to parse unwind records
if (macho_file.getGlobal("___gxx_personality_v0")) |global| {
if (macho_file.getAtomForSymbol(global)) |atom| {
_ = try roots.getOrPut(atom);
log.debug("adding root", .{});
macho_file.logAtom(atom);
}
}
for (macho_file.objects.items) |object| {
for (object.managed_atoms.items) |atom| {
const source_sym = object.getSourceSymbol(atom.sym_index) orelse continue;
if (source_sym.tentative()) continue;
const source_sect = object.getSourceSection(source_sym.n_sect - 1);
const is_gc_root = blk: {
if (source_sect.isDontDeadStrip()) break :blk true;
if (mem.eql(u8, "__StaticInit", source_sect.sectName())) break :blk true;
switch (source_sect.@"type"()) {
macho.S_MOD_INIT_FUNC_POINTERS,
macho.S_MOD_TERM_FUNC_POINTERS,
=> break :blk true,
else => break :blk false,
}
};
if (is_gc_root) {
try roots.putNoClobber(atom, {});
log.debug("adding root", .{});
macho_file.logAtom(atom);
}
}
}
}
fn markLive(atom: *Atom, alive: *std.AutoHashMap(*Atom, void), macho_file: *MachO) anyerror!void {
const gop = try alive.getOrPut(atom);
if (gop.found_existing) return;
log.debug("marking live", .{});
macho_file.logAtom(atom);
for (atom.relocs.items) |rel| {
const target_atom = rel.getTargetAtom(macho_file) orelse continue;
try markLive(target_atom, alive, macho_file);
}
}
fn refersLive(atom: *Atom, alive: std.AutoHashMap(*Atom, void), macho_file: *MachO) bool {
for (atom.relocs.items) |rel| {
const target_atom = rel.getTargetAtom(macho_file) orelse continue;
if (alive.contains(target_atom)) return true;
}
return false;
}
fn refersDead(atom: *Atom, macho_file: *MachO) bool {
for (atom.relocs.items) |rel| {
const target_atom = rel.getTargetAtom(macho_file) orelse continue;
const target_sym = target_atom.getSymbol(macho_file);
if (target_sym.n_desc == MachO.N_DESC_GCED) return true;
}
return false;
}
fn mark(
roots: std.AutoHashMap(*Atom, void),
alive: *std.AutoHashMap(*Atom, void),
macho_file: *MachO,
) !void {
try alive.ensureUnusedCapacity(roots.count());
var it = roots.keyIterator();
while (it.next()) |root| {
try markLive(root.*, alive, macho_file);
}
var loop: bool = true;
while (loop) {
loop = false;
for (macho_file.objects.items) |object| {
for (object.managed_atoms.items) |atom| {
if (alive.contains(atom)) continue;
const source_sym = object.getSourceSymbol(atom.sym_index) orelse continue;
if (source_sym.tentative()) continue;
const source_sect = object.getSourceSection(source_sym.n_sect - 1);
if (source_sect.isDontDeadStripIfReferencesLive() and refersLive(atom, alive.*, macho_file)) {
try markLive(atom, alive, macho_file);
loop = true;
}
}
}
}
}
fn prune(arena: Allocator, alive: std.AutoHashMap(*Atom, void), macho_file: *MachO) !void {
// Any section that ends up here will be updated, that is,
// its size and alignment recalculated.
var gc_sections = std.AutoHashMap(u8, void).init(arena);
var loop: bool = true;
while (loop) {
loop = false;
for (macho_file.objects.items) |object| {
for (object.in_symtab) |_, source_index| {
const atom = object.getAtomForSymbol(@intCast(u32, source_index)) orelse continue;
if (alive.contains(atom)) continue;
const global = atom.getSymbolWithLoc();
const sym = atom.getSymbolPtr(macho_file);
const match = sym.n_sect - 1;
if (sym.n_desc == MachO.N_DESC_GCED) continue;
if (!sym.ext() and !refersDead(atom, macho_file)) continue;
macho_file.logAtom(atom);
sym.n_desc = MachO.N_DESC_GCED;
removeAtomFromSection(atom, match, macho_file);
_ = try gc_sections.put(match, {});
for (atom.contained.items) |sym_off| {
const inner = macho_file.getSymbolPtr(.{
.sym_index = sym_off.sym_index,
.file = atom.file,
});
inner.n_desc = MachO.N_DESC_GCED;
}
if (macho_file.got_entries_table.contains(global)) {
const got_atom = macho_file.getGotAtomForSymbol(global).?;
const got_sym = got_atom.getSymbolPtr(macho_file);
got_sym.n_desc = MachO.N_DESC_GCED;
}
if (macho_file.stubs_table.contains(global)) {
const stubs_atom = macho_file.getStubsAtomForSymbol(global).?;
const stubs_sym = stubs_atom.getSymbolPtr(macho_file);
stubs_sym.n_desc = MachO.N_DESC_GCED;
}
if (macho_file.tlv_ptr_entries_table.contains(global)) {
const tlv_ptr_atom = macho_file.getTlvPtrAtomForSymbol(global).?;
const tlv_ptr_sym = tlv_ptr_atom.getSymbolPtr(macho_file);
tlv_ptr_sym.n_desc = MachO.N_DESC_GCED;
}
loop = true;
}
}
}
for (macho_file.got_entries.items) |entry| {
const sym = entry.getSymbol(macho_file);
if (sym.n_desc != MachO.N_DESC_GCED) continue;
// TODO tombstone
const atom = entry.getAtom(macho_file);
const match = sym.n_sect - 1;
removeAtomFromSection(atom, match, macho_file);
_ = try gc_sections.put(match, {});
_ = macho_file.got_entries_table.remove(entry.target);
}
for (macho_file.stubs.items) |entry| {
const sym = entry.getSymbol(macho_file);
if (sym.n_desc != MachO.N_DESC_GCED) continue;
// TODO tombstone
const atom = entry.getAtom(macho_file);
const match = sym.n_sect - 1;
removeAtomFromSection(atom, match, macho_file);
_ = try gc_sections.put(match, {});
_ = macho_file.stubs_table.remove(entry.target);
}
for (macho_file.tlv_ptr_entries.items) |entry| {
const sym = entry.getSymbol(macho_file);
if (sym.n_desc != MachO.N_DESC_GCED) continue;
// TODO tombstone
const atom = entry.getAtom(macho_file);
const match = sym.n_sect - 1;
removeAtomFromSection(atom, match, macho_file);
_ = try gc_sections.put(match, {});
_ = macho_file.tlv_ptr_entries_table.remove(entry.target);
}
var gc_sections_it = gc_sections.iterator();
while (gc_sections_it.next()) |entry| {
const match = entry.key_ptr.*;
var section = macho_file.sections.get(match);
if (section.header.size == 0) continue; // Pruning happens automatically in next step.
section.header.@"align" = 0;
section.header.size = 0;
var atom = section.last_atom.?;
while (atom.prev) |prev| {
atom = prev;
}
while (true) {
const atom_alignment = try math.powi(u32, 2, atom.alignment);
const aligned_end_addr = mem.alignForwardGeneric(u64, section.header.size, atom_alignment);
const padding = aligned_end_addr - section.header.size;
section.header.size += padding + atom.size;
section.header.@"align" = @maximum(section.header.@"align", atom.alignment);
if (atom.next) |next| {
atom = next;
} else break;
}
macho_file.sections.set(match, section);
}
}
|