summaryrefslogtreecommitdiff
path: root/anim/scenes/musl.zig
diff options
context:
space:
mode:
Diffstat (limited to 'anim/scenes/musl.zig')
-rw-r--r--anim/scenes/musl.zig84
1 files changed, 84 insertions, 0 deletions
diff --git a/anim/scenes/musl.zig b/anim/scenes/musl.zig
new file mode 100644
index 0000000..3112a27
--- /dev/null
+++ b/anim/scenes/musl.zig
@@ -0,0 +1,84 @@
+//! The `syscalls` scene's `open`, built by Zig against musl, static: no
+//! PLT and no GOT, and musl compiled from its source with the program, so
+//! the source panel follows the pc all the way down: `main`'s line, musl's
+//! `open.c`, `__syscall_cp.c`, and the one line of `syscall_arch.h` that
+//! is the `syscall`; the kernel's window at the floor, and back up.
+//!
+//! How it moves: as `syscalls` (design/motion.md, "A call followed down",
+//! "A system call"); each window opened by `descend`, the source fading
+//! through to each file it comes to.
+const std = @import("std");
+const anim = @import("anim");
+const canim = @import("canim");
+const recordings = @import("recordings");
+
+pub const background = canim.background;
+pub const scene = anim.Live(frame, struct {});
+
+/// The first moment from `from` whose instruction is `text`.
+fn on(rec: *canim.Recording, from: u64, text: []const u8) u64 {
+ var buf: [256]u8 = undefined;
+ var i = from;
+ while (i < rec.end()) : (i += 1) if (rec.instruction(i, rec.register(i, "rip"), &buf)) |here| if (std.mem.startsWith(u8, here.text, text)) return i;
+ return rec.end();
+}
+
+/// The first moment from `from` on line `line` of a file not the program's own.
+fn libLine(rec: *canim.Recording, from: u64, line: u32) u64 {
+ var i = from;
+ while (i < rec.end()) : (i += 1) if (rec.shown[i].here and rec.shown[i].src != 0 and rec.shown[i].line == line) return i;
+ return rec.end();
+}
+
+fn frame(s: *anim.Frame) void {
+ const rec = canim.open(recordings.musl);
+ const main = rec.call("main", 0);
+ var cur = canim.cursor(s, rec, main.enter);
+ const call_open = on(rec, main.enter, "call");
+ const call_cp = on(rec, call_open + 1, "call");
+ const jump = on(rec, call_cp, "jmp");
+ const crossing = on(rec, jump, "syscall");
+ const ret_cp = on(rec, crossing, "ret");
+ const ret_open = on(rec, ret_cp + 1, "ret");
+ const done = ret_open + 1;
+ cur.journey(.moment, done);
+ cur.levels = .{ .source_rows = 8, .fire = .disasm };
+ const ctx = canim.context(s, cur, .{ .split = canim.layout.split.beside, .syscalls = true, .descent = true });
+ const log = canim.crossing(s, .{ .cursor = cur, .stage = ctx.stage, .focus = true });
+ ctx.map.add(s, canim.Crossing{ .cursor = cur, .stage = ctx.map.stage });
+ var says: canim.Narrator = .{};
+ const t = canim.theme.time;
+ const joins = ctx.ties(s);
+ canim.establish(s, .{ ctx.clock, ctx.registers, ctx.stack, ctx.source, ctx.disasm, log, joins });
+
+ says.line(s, "The same `hello`, built by Zig against musl, static.");
+ cur.walk(s, .line, rec.lineMoment(6, main.enter));
+ says.line(s, "`main` calls `open` itself: no PLT stub, no GOT.\nmusl is compiled from its source with the program.");
+ cur.walk(s, .moment, call_open);
+ canim.descent.step(cur, s, .moment);
+ s.wait(t.phrase_rest);
+ says.line(s, "musl's `open` is open.c: the source follows it.");
+ cur.hurry(s, .moment, libLine(rec, call_open, 9), .{});
+ says.line(s, "Its line 16 calls `__syscall_cp`,\nin __syscall_cp.c.");
+ cur.hurry(s, .moment, call_cp, .{});
+ canim.descent.step(cur, s, .moment);
+ s.wait(t.phrase_rest);
+ says.line(s, "Which jumps on to `__syscall_cp_c`, in the same file.");
+ // __syscall_cp's few rows, and its jump on.
+ while (cur.moment(s) <= jump) canim.descent.step(cur, s, .moment);
+ s.wait(t.phrase_rest);
+ says.line(s, "The `syscall` is one line of C, in syscall_arch.h:\nrax holds 2, open's number, and rdi the path.");
+ cur.hurry(s, .moment, crossing - 2, .{});
+ cur.walk(s, .moment, crossing);
+ says.line(s, "The kernel opens the file and answers in rax: 3.");
+ canim.descent.step(cur, s, .moment);
+ s.wait(t.phrase_rest);
+ cur.hurry(s, .moment, ret_cp, .{});
+ canim.descent.step(cur, s, .moment);
+ s.wait(t.phrase_rest);
+ says.line(s, "Back in open.c, which returns the 3 to `main`.");
+ cur.hurry(s, .moment, ret_open, .{});
+ canim.descent.step(cur, s, .moment);
+ s.wait(t.phrase_rest);
+ says.close(s);
+}