lib/tldr/src/formats/elf/liveness/mark.zig
daab053ee43316e1809a84551d573ddd1e5bf3d2
1 const std = @import("std");
2 const root = @import("../../../root.zig");
3 const elf = @import("../root.zig");
4 const groups = @import("groups.zig");
5 const roots = @import("roots.zig");
6 const state = @import("state.zig");
7 const symbols = @import("symbols.zig");
8
9 const Allocator = std.mem.Allocator;
10 const model = root.model;
11
12 const GlobalSymbol = elf.layout.GlobalSymbol;
13 const ObjectFile = elf.parser.ObjectFile;
14
15 pub fn discardUnreachable(
16 allocator: Allocator,
17 objects: []ObjectFile,
18 globals: *const std.StringHashMapUnmanaged(GlobalSymbol),
19 options: model.LinkOptions,
20 ) model.Error!void {
21 var live = try state.init(allocator, objects);
22 defer state.deinit(&live, allocator);
23
24 const entry = globals.get(options.entry_symbol) orelse return error.MissingEntrySymbol;
25
26 var group_memberships = try elf.group.Index.init(allocator, objects);
27 defer group_memberships.deinit(allocator);
28
29 try roots.markReserved(objects, &live);
30 try roots.markRetained(objects, &live);
31 try symbols.mark(objects, globals, &live, entry.ref, options);
32
33 var next: usize = 0;
34 while (next < live.pending.items.len) : (next += 1) {
35 const section_ref = live.pending.items[next];
36 try groups.mark(objects, &group_memberships, &live, section_ref);
37 try symbols.markRelocationTargets(objects, globals, &live, section_ref, options);
38 }
39
40 try state.discardUnmarked(&live, allocator, objects);
41 }