From e8616df2031b39535e62dce1142b029e65723c95 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 11 Nov 2026 11:52:21 -0300 Subject: design: the address space as one tall rectangle; aligned as a listing From the user. segments is the textbook picture: one rectangle, high addresses on top, each mapping a band in its ink (heights by the log of their size, holes folded with their size beside them), labels outside on leaders, a file's mappings bracketed as one, the stack's and the heap's growth as arrows, rsp and rip pinned in their bands. aligned is one panel: a line as the head of its instructions, line numbers and addresses in one column since both say where, a rule over every head. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_016Y9MNCKgYCGiANnEwcEQxa --- design/README.md | 97 ++++++----- design/mock/30-intro-words-480.png | Bin 0 -> 43111 bytes design/mock/30-intro-words.png | Bin 224784 -> 230085 bytes design/mock/30-intro-words.svg | 287 ++++++++++++++++++++++++--------- design/mock/32-intro-code.png | Bin 104319 -> 108875 bytes design/mock/32-intro-code.svg | 287 ++++++++++++++++----------------- design/mock/33-aligned-480.png | Bin 18796 -> 15933 bytes design/mock/33-aligned.png | Bin 135867 -> 98391 bytes design/mock/33-aligned.svg | 318 +++++++++++++++---------------------- design/mock/34-segments.png | Bin 0 -> 113202 bytes design/mock/34-segments.svg | 213 +++++++++++++++++++++++++ design/mock/intro.py | 213 ++++++++++++++++++------- 12 files changed, 907 insertions(+), 508 deletions(-) create mode 100644 design/mock/30-intro-words-480.png create mode 100644 design/mock/34-segments.png create mode 100644 design/mock/34-segments.svg diff --git a/design/README.md b/design/README.md index ffbb820..f9bdf35 100644 --- a/design/README.md +++ b/design/README.md @@ -1321,45 +1321,68 @@ What stays, and why: ### segments -The process's address space, which is where the inks come from: a panel -of its mappings as the recording has them, low addresses on top, a row -each. A bar 8 wide in the mapping's ink, its first address, its file or -name (`list`, `heap`, `libc.so.6`, `[vdso]`, `[stack]`), what it is (code, -read-only data, data, malloc's, the stack: from its permissions), its -size ranged right (`4 KiB`, `1.5 MiB`). Rows, not a scale: the code and -the stack are 127 TiB apart, and a hole between mappings is one row, a -faint zig-zag and its size (`nothing mapped · 127 TiB`). When a word takes -its ink, the mapping it points into is framed as read with it (an -`echo`). It is the legend's long form: the registers' tag carries its -inks in short; the title card's legend and the map's key draw this panel. -It is not the far view (mock 8), which is time over pages; the far view -can stand on it as its axis. Mock 30. +The address space, the textbook's picture: one tall rectangle, high +addresses on top (the stack at the top growing down, the heap growing up), +the mapped parts as bands in their inks (55% toward the page), the holes +between them broken. It is where the inks come from. + +| Part | Drawn | +| --- | --- | +| the rectangle | 140 wide; `ffffffffffffffff` over it and `0` under it, faint | +| a band | a mapping: its ink as fill, a 2-unit page line at its top so a file's mappings read as parts of one thing; 24 high at least and 4 more a doubling past 4 KiB (1.5 MiB is 58): schematic, by the logarithm of its size, and its title says `not to scale` | +| a hole | what is not mapped: a fixed 40, two zig-zags across it, its size beside it (`nothing mapped · 127 TiB`; `15 EiB` above the stack) | +| its labels | outside, so the bands stay clean: on the left the band's first address in its ink, at the band's foot; on the right, after a 2-unit leader, its permissions in its ink, what it is (code, read-only, data, data (anonymous), `[stack]`, `[heap]`, `[vdso]`), its size | +| a file's parts | a bracket right of the labels spanning its bands, the file's name beside it (`libc.so.6`: r--, r-x, r--, r--, rw-) | +| growth | a 2-unit `bright` arrow in the stack's band pointing down, in the heap's pointing up | +| live | rsp's and rip's pins at their place in their bands; a band framed as read (3 units) when a word is inked into it; the heap's top glides when `brk` grows it (12 f); a new mapping fades into its hole (12 f), the hole closing round it | + +**Orientation.** High addresses on top, against every panel of the +language, which keeps low addresses on top. The rectangle is the whole +space at once, read as the textbook draws it, as the user asked; the +panels are rows of memory read like text. The introduction says it once, +in the beat that sets the stack panel beside the rectangle: the stack +grows down the rectangle and up its panel, the same pushes; the growth +arrows carry it. + +**Where it lives.** The introduction (mock 30), the map's key, the title +card's legend. In the working frame it has no standing place (the +registers' tag carries its inks in short); a scene about memory layout can +take it as its drawing (`brk`, `mmap`), its view the mappings as rows. +Mocks 30 and 34. ### aligned -The source and the disassembly side by side, a table in address order: -each source line level with the first row of its instructions, a quiet -bracket by the gutter from the line down its run's last row. A line whose -code comes in several runs (a `for`'s test and step, a `while` tested -before and after its body, code the compiler moved) is drawn at its first -run and repeated faint at each other; a line with no code of its own is -not drawn, and the gap in the numbers says so (the scene's caption may); -instructions of no line (padding, another function without source) have -the function's name beside them, faint. The pc's row and its line are one -band across both columns and the gutter: the alignment is the thread, so -no thread is drawn. A call followed down puts the callee's lines beside -the callee's rows, under the caller's waiting call (the `descent`, both -columns at once). - -It is a split a scene chooses, and the default nowhere: the program -column becomes the source (left, 880) and the state column the -disassembly (right, 880), the band and the registers and stack giving way -for as long as it lasts. Across the gutter is "the same thing one level -down", as in the band; down is still deeper in each column. Reach for it -to teach what a line compiles to, in the introduction, and in a pair (the -source in the middle, 586 wide, each build's rows aligned to it on its -side). The two columns scroll as one. `align` and `stack` change between -it and the stacked form. Mocks 32 and 33. +The source with the instructions it compiled to, as one panel and one +listing, as `objdump -S` prints it: each run of a line's instructions is a +group, its head a row with the line on `raised` paper, its rows under it, +indented one step under the line's text. Lines and addresses share one +column, the place column (ranged right at 196: `21` over `1001926`): both +say where. A 2-unit `rule` across over every head marks where a line's +group starts and ends; nothing alternates. + +- A group of one row: its head and one row; of many: its head and all of + them, never folded in the working frame. +- A line's second run (a `for`'s step, a `while` tested again after its + body): its head again, on the page, faint, `again: its second run`. +- Rows of no line (padding, another function without source): a faint head + with the function's name, `_init · no line`. +- A line with no code of its own has no group; the gap in the numbers says + so. +- The pc's row has the wash and the plaque, its line's number the gold + plaque in its head. The grouping is the thread. + +It replaces the side-by-side rows first drawn: the user asked for one +panel, the two closer, a line's start and end unmistakable, and the line +number and the address in one column, and the interleaved listing gives +all four while keeping what was liked, each instruction level with its +line, right under it. A `pair` puts the source in the middle and each +build's rows aligned to it on its side. + +A split a scene chooses, the default nowhere: the panel spans both columns +(1792), the band and the state giving way while it lasts. Reach for it to +teach what a line compiles to, in the introduction, and in a pair. `align` +and `stack` change between it and the stacked form; it scrolls as one. +Mocks 32 and 33. ### pair @@ -1542,7 +1565,7 @@ Frames at 60 a second. `s` is the frame a strike lands. Curves: | `column` | rows gaining a column (offsets, roles, pins, lines) | the rows make room (18 f, as rows do), then the column's values fade in top down, 2 f apart, 12 f each | | `frame up` | a panel arriving round rows already built | its rule draws round them (12 f), then its tag fades in (12 f): the last thing, never the first | | `decode` | an instruction's bytes becoming its row | the bytes of one instruction gather (12 f), the mnemonic and operands fade in beside them (12 f), and the bytes dim; the row then travels to its place (`glide`, 24 f) | -| `align` | the stacked program column to `aligned`, and back (`stack`) | the band and the state column fade out (12 f); the disassembly glides from under the source to beside it (36 f, `glide`); then the source's lines glide apart to their instructions' rows (24 f, rows making room) and the run brackets fade in (12 f). `stack` is the reverse | +| `align` | the stacked program column to `aligned`, and back (`stack`) | the band and the state column fade out (12 f); the panel widens to both columns (24 f, `glide`); the source's lines glide to their heads and the instructions glide in under them (36 f, rows making room), each instruction from its row in the disassembly to its row under its line; the heads' paper and rules fade in last (12 f). `stack` is the reverse | | `arc` | a pointer to a row of its panel | written: with the arm (s−12) the old arc goes dashed; from s+6 it fades (12 f) while the new one draws from the pointer's row along its lane to the target, head first, 12 f, `standard`, at full ink; with the cool at the advance it quiets to standing (24 f). Read: to full ink with the read (6 f), back with the advance. Overwritten with a target off the panel or NULL: it fades from s+6 (12 f). A lane change glides 12 f. Scrolled: it rides its rows, fading as its target leaves. At a run 8 f, at a sprint none | | `advance` | the current marks | last. The thread, the plaques and rip move together: 12 f to an adjacent row, 24 f up to eight rows, `standard`; farther, out and in over 6 f. A taken jump's line draws in the jump lane with it | | `cascade` | a step | `fire` (12); then its effects a level at a time, 18 f apart: CPU (`reg`s, the pc's annotation), memory (`slot`s, `bytes`), the idea (the drawing, by `project`); each armed, then struck; hold 36 f after the last strike and 12 f a further value, 84 at most; `advance`; rest 24 f. A level with nothing on screen to change gives up its slot | diff --git a/design/mock/30-intro-words-480.png b/design/mock/30-intro-words-480.png new file mode 100644 index 0000000..4156b0a Binary files /dev/null and b/design/mock/30-intro-words-480.png differ diff --git a/design/mock/30-intro-words.png b/design/mock/30-intro-words.png index faba6f4..463726e 100644 Binary files a/design/mock/30-intro-words.png and b/design/mock/30-intro-words.png differ diff --git a/design/mock/30-intro-words.svg b/design/mock/30-intro-words.svg index fd1502b..267a344 100644 --- a/design/mock/30-intro-words.svg +++ b/design/mock/30-intro-words.svg @@ -165,81 +165,218 @@ 1 - - - -the address space -the recording's mappings, low on top - -1000000 -list -headers, read-only -4 KiB - -1001000 -list -code -4 KiB - - -1002000 -list -read-only data -4 KiB - -1003000 -list -data -4 KiB - -1004000 -heap -malloc's -132 KiB - -nothing mapped · 127 TiB - -7ffff7c00000 -libc.so.6 -read-only -144 KiB - -7ffff7c24000 -libc.so.6 -code -1.5 MiB - -7ffff7d9f000 -libc.so.6 -read-only data -472 KiB - -7ffff7e15000 -libc.so.6 -data -56 KiB - -7ffff7fbc000 -[vdso] -code -8 KiB - -7ffff7fbf000 -ld.so -code -180 KiB - -7ffff7ffb000 -ld.so -data -16 KiB - -nothing mapped · 127 MiB - -7ffffffde000 -[stack] -the stack -132 KiB + + + +ffffffffffffffff + + + +--x +[vsyscall] +4 KiB +ffffffffff600000 + + + + +nothing mapped · 15 EiB + + + +rw- +[stack] +132 KiB +7ffffffde000 + + + + +nothing mapped · 127 MiB + + + +rw- +data (anonymous) +4 KiB +7ffff7ffe000 + + + +rw- +data +4 KiB +7ffff7ffd000 + + + +r-- +read-only +8 KiB +7ffff7ffb000 + + + +r-- +read-only +60 KiB +7ffff7fec000 + + + +r-x +code +180 KiB +7ffff7fbf000 + + + +r-- +read-only +4 KiB +7ffff7fbe000 + + + +r-x +[vdso] +8 KiB +7ffff7fbc000 + + + +r-- +[vvar] +24 KiB +7ffff7fb6000 + + + +r-- +read-only +260 KiB +7ffff7f75000 + + + +rw- +data (anonymous) +20 KiB +7ffff7f70000 + + + + +nothing mapped · 1.3 MiB + + + +rw- +data (anonymous) +32 KiB +7ffff7e1b000 + + + +rw- +data +8 KiB +7ffff7e19000 + + + +r-- +read-only +16 KiB +7ffff7e15000 + + + +r-- +read-only +472 KiB +7ffff7d9f000 + + + +r-x +code +1.5 MiB +7ffff7c24000 + + + +r-- +read-only +144 KiB +7ffff7c00000 + + + + +nothing mapped · 127 TiB + + + +rw- +[heap] +132 KiB +1004000 + + + +rw- +data +4 KiB +1003000 + + + +r-- +read-only +4 KiB +1002000 + + + +r-x +code +4 KiB +1001000 + + + + +r-- +read-only +4 KiB +1000000 + + + +ld.so + + + +ld.so.cache + + + +libc.so.6 + + + +list +0 + + + + +rsp + +the address space +high addresses on top · not to scale: holes 40, bands by the log of their size + Eight bytes make a word, stored low byte first: read right to left, this one is 10018b0, an address in the code, so it takes code's ink. diff --git a/design/mock/32-intro-code.png b/design/mock/32-intro-code.png index eb22ba7..2d9b70b 100644 Binary files a/design/mock/32-intro-code.png and b/design/mock/32-intro-code.png differ diff --git a/design/mock/32-intro-code.svg b/design/mock/32-intro-code.svg index 35597b8..a5eafc0 100644 --- a/design/mock/32-intro-code.svg +++ b/design/mock/32-intro-code.svg @@ -1,155 +1,144 @@ - - - -list.c -each line beside the first of its instructions - - - -disassembly -reverse - - - - - - - - - - - - - - - -20 -while -( -head -) -{ -10019 -10 -48 85 ff -test -rdi, rdi -10019 -13 -74 20 -je -1001935 -10019 -15 -55 -push -rbp -10019 -16 -48 89 e5 -mov -rbp, rsp -10019 -19 -31 c0 -xor -eax, eax -10019 -1b -0f 1f 44 00 00 -nop -[rax + rax] -10019 -20 -48 89 c1 -mov -rcx, rax -10019 -23 -48 89 f8 -mov -rax, rdi - - - - - - -21 -struct -node -* -next -= -head -- -> -next -; -10019 -26 -48 8b 7f 08 -mov -rdi, [rdi + 8] -1004098 - - -1004070 - - -9 -22 -head -- -> -next -= -prev -; -10019 -2a -48 89 48 08 -mov -[rax + 8], rcx -20 -while (head) { -10019 -2e -48 85 ff -test -rdi, rdi -10019 -31 -75 ed -jne -1001920 -10019 -33 -5d -pop -rbp -26 -return -prev -; -10019 -34 -c3 -ret - -10019 -35 -31 c0 -xor -eax, eax -10019 -37 -c3 -ret - + + + +list.c · reverse +each line, then the instructions it compiled to · lines and addresses in one column + + +20 +while +( +head +) +{ +1001 +910 +48 85 ff +test +rdi, rdi +1001 +913 +74 20 +je +1001935 +1001 +915 +55 +push +rbp +1001 +916 +48 89 e5 +mov +rbp, rsp +1001 +919 +31 c0 +xor +eax, eax +1001 +91b +0f 1f 44 00 00 +nop +[rax + rax] +1001 +920 +48 89 c1 +mov +rcx, rax +1001 +923 +48 89 f8 +mov +rax, rdi + + + +21 +struct +node +* +next += +head +- +> +next +; + + + +1001 +926 +48 8b 7f 08 +mov +rdi, [rdi + 8] +1004098 + + +1004070 + + +9 + + +22 +head +- +> +next += +prev +; +1001 +92a +48 89 48 08 +mov +[rax + 8], rcx + + +20 +while (head) { again: its second run +1001 +92e +48 85 ff +test +rdi, rdi +1001 +931 +75 ed +jne +1001920 +1001 +933 +5d +pop +rbp + + +26 +return +prev +; +1001 +934 +c3 +ret + +1001 +935 +31 c0 +xor +eax, eax +1001 +937 +c3 +ret + + 48 8b 7f 08 is one instruction: mov rdi, [rdi + 8], line 21's head->next. The line sits level with its code: they are one row. diff --git a/design/mock/33-aligned-480.png b/design/mock/33-aligned-480.png index a54620d..77ecf25 100644 Binary files a/design/mock/33-aligned-480.png and b/design/mock/33-aligned-480.png differ diff --git a/design/mock/33-aligned.png b/design/mock/33-aligned.png index 5ed74b0..b649754 100644 Binary files a/design/mock/33-aligned.png and b/design/mock/33-aligned.png differ diff --git a/design/mock/33-aligned.svg b/design/mock/33-aligned.svg index 5c46e27..1762f33 100644 --- a/design/mock/33-aligned.svg +++ b/design/mock/33-aligned.svg @@ -1,198 +1,128 @@ - - - -list.c -each line beside the first of its instructions - - - -disassembly -reverse - - - - - - - - - - - - - - - -20 -while -( -head -) -{ -10019 -10 -test -rdi, rdi -10019 -13 -je -1001935 -10019 -15 -push -rbp -10019 -16 -mov -rbp, rsp -10019 -19 -xor -eax, eax -10019 -1b -nop -[rax + rax] -10019 -20 -mov -rcx, rax -10019 -23 -mov -rax, rdi - - - - - - -21 -struct -node -* -next -= -head -- -> -next -; -10019 -26 -mov -rdi, [rdi + 8] -1004098 - - -1004070 - - -9 -22 -head -- -> -next -= -prev -; -10019 -2a -mov -[rax + 8], rcx -20 -while (head) { -10019 -2e -test -rdi, rdi -10019 -31 -jne -1001920 -10019 -33 -pop -rbp -26 -return -prev -; -10019 -34 -ret - -10019 -35 -xor -eax, eax -10019 -37 -ret - -_init · no line -10019 -38 -endbr64 - -10019 -3c -sub -rsp, 8 -10019 -40 -mov -rax, [rip + 0x1231] -10019 -47 -test -rax, rax -10019 -4a -je -100194e -10019 -4c -call -rax -10019 -4e -add -rsp, 8 -10019 -52 -ret - -_fini · no line -10019 -54 -endbr64 - -10019 -58 -sub -rsp, 8 -10019 -5c -add -rsp, 8 -10019 -60 -ret - -strtol@plt-0x10 · no line -10019 -70 -push -[rip + 0x1222] -10019 -76 -jmp -[rip + 0x1224] -Line 20, while (head), has two runs: its test before the body -and again after it. Lines 23 to 25 have no code of their own. + + + +list.c · reverse +each line, then the instructions it compiled to · lines and addresses in one column + + +20 +while +( +head +) +{ +1001 +910 +test +rdi, rdi +1001 +913 +je +1001935 +1001 +915 +push +rbp +1001 +916 +mov +rbp, rsp +1001 +919 +xor +eax, eax +1001 +91b +nop +[rax + rax] +1001 +920 +mov +rcx, rax +1001 +923 +mov +rax, rdi + + + +21 +struct +node +* +next += +head +- +> +next +; + + + +1001 +926 +mov +rdi, [rdi + 8] +1004098 + + +1004070 + + +9 + + +22 +head +- +> +next += +prev +; +1001 +92a +mov +[rax + 8], rcx + + +20 +while (head) { again: its second run +1001 +92e +test +rdi, rdi +1001 +931 +jne +1001920 +1001 +933 +pop +rbp + + +26 +return +prev +; +1001 +934 +ret + +1001 +935 +xor +eax, eax +1001 +937 +ret + + +Line 20, while (head), comes twice: its test before the body and +again after it. Lines 23 to 25 have no code of their own: no group. diff --git a/design/mock/34-segments.png b/design/mock/34-segments.png new file mode 100644 index 0000000..03d26db Binary files /dev/null and b/design/mock/34-segments.png differ diff --git a/design/mock/34-segments.svg b/design/mock/34-segments.svg new file mode 100644 index 0000000..5c5505b --- /dev/null +++ b/design/mock/34-segments.svg @@ -0,0 +1,213 @@ + + + +ffffffffffffffff + + + +--x +[vsyscall] +4 KiB +ffffffffff600000 + + + + +nothing mapped · 15 EiB + + + +rw- +[stack] +132 KiB +7ffffffde000 + + + + +nothing mapped · 127 MiB + + + +rw- +data (anonymous) +4 KiB +7ffff7ffe000 + + + +rw- +data +4 KiB +7ffff7ffd000 + + + +r-- +read-only +8 KiB +7ffff7ffb000 + + + +r-- +read-only +60 KiB +7ffff7fec000 + + + +r-x +code +180 KiB +7ffff7fbf000 + + + +r-- +read-only +4 KiB +7ffff7fbe000 + + + +r-x +[vdso] +8 KiB +7ffff7fbc000 + + + +r-- +[vvar] +24 KiB +7ffff7fb6000 + + + +r-- +read-only +260 KiB +7ffff7f75000 + + + +rw- +data (anonymous) +20 KiB +7ffff7f70000 + + + + +nothing mapped · 1.3 MiB + + + +rw- +data (anonymous) +32 KiB +7ffff7e1b000 + + + +rw- +data +8 KiB +7ffff7e19000 + + + +r-- +read-only +16 KiB +7ffff7e15000 + + + +r-- +read-only +472 KiB +7ffff7d9f000 + + + +r-x +code +1.5 MiB +7ffff7c24000 + + + +r-- +read-only +144 KiB +7ffff7c00000 + + + + +nothing mapped · 127 TiB + + + +rw- +[heap] +132 KiB +1004000 + + + +rw- +data +4 KiB +1003000 + + + +r-- +read-only +4 KiB +1002000 + + + +r-x +code +4 KiB +1001000 + + + +r-- +read-only +4 KiB +1000000 + + + +ld.so + + + +ld.so.cache + + + +libc.so.6 + + + +list +0 + + + + +rsp + +rip + +the address space +high addresses on top · not to scale: holes 40, bands by the log of their size + diff --git a/design/mock/intro.py b/design/mock/intro.py index b9c7d7d..098455c 100644 --- a/design/mock/intro.py +++ b/design/mock/intro.py @@ -26,14 +26,14 @@ CODE_BYTES = [(0x1001920, "48 89 c1"), (0x1001923, "48 89 f8"), (0x1001926, "48 def size(n): - for unit, k in (("TiB", 1 << 40), ("GiB", 1 << 30), ("MiB", 1 << 20), ("KiB", 1 << 10)): + for unit, k in (("EiB", 1 << 60), ("PiB", 1 << 50), ("TiB", 1 << 40), ("GiB", 1 << 30), ("MiB", 1 << 20), ("KiB", 1 << 10)): if n >= k: v = n / k return ("%d %s" if v == int(v) or v >= 10 else "%.1f %s") % (v, unit) return "%d B" % n -def segments(g, x, y, w, hot=()): +def segment_rows(g, x, y, w, hot=()): """`segments`: the process's address space as its mappings, low addresses on top, a row each: a bar in the region's ink, the first address, what it is, its size; a hole between them one row, how big it is. Rows, not a scale: the stack and the code are 127 TiB apart. hot: the mappings framed as read (a word just inked).""" @@ -60,6 +60,104 @@ def segments(g, x, y, w, hot=()): return y0 +# the mappings at the stop, as `ztrace show` prints them (range, permissions, file offset, file); high on top +MAPPINGS = [(0xffffffffff600000, 0xffffffffff601000, "--x", "[vsyscall]"), + (0x7ffffffde000, 0x7ffffffff000, "rw-", "[stack]"), + (0x7ffff7ffe000, 0x7ffff7fff000, "rw-", ""), (0x7ffff7ffd000, 0x7ffff7ffe000, "rw-", "ld.so"), + (0x7ffff7ffb000, 0x7ffff7ffd000, "r--", "ld.so"), (0x7ffff7fec000, 0x7ffff7ffb000, "r--", "ld.so"), + (0x7ffff7fbf000, 0x7ffff7fec000, "r-x", "ld.so"), (0x7ffff7fbe000, 0x7ffff7fbf000, "r--", "ld.so"), + (0x7ffff7fbc000, 0x7ffff7fbe000, "r-x", "[vdso]"), (0x7ffff7fb6000, 0x7ffff7fbc000, "r--", "[vvar]"), + (0x7ffff7f75000, 0x7ffff7fb6000, "r--", "ld.so.cache"), (0x7ffff7f70000, 0x7ffff7f75000, "rw-", ""), + (0x7ffff7e1b000, 0x7ffff7e23000, "rw-", ""), (0x7ffff7e19000, 0x7ffff7e1b000, "rw-", "libc.so.6"), + (0x7ffff7e15000, 0x7ffff7e19000, "r--", "libc.so.6"), (0x7ffff7d9f000, 0x7ffff7e15000, "r--", "libc.so.6"), + (0x7ffff7c24000, 0x7ffff7d9f000, "r-x", "libc.so.6"), (0x7ffff7c00000, 0x7ffff7c24000, "r--", "libc.so.6"), + (0x1004000, 0x1025000, "rw-", "[heap]"), (0x1003000, 0x1004000, "rw-", "list"), + (0x1002000, 0x1003000, "r--", "list"), (0x1001000, 0x1002000, "r-x", "list"), (0x1000000, 0x1001000, "r--", "list")] +INK = {"r-x": CODE, "--x": CODE, "r--": VELLUM, "rw-": DATA} +WHAT = {"r-x": "code", "--x": "code", "r--": "read-only", "rw-": "data"} + + +def band_h(n): + """A band's height: 24 at least (a label's row), and 4 a doubling past 4 KiB: schematic, not to scale.""" + import math + return 24 + 4 * max(math.log2(n / 4096), 0) + + +def segments(g, x, y, hot=(), rsp=None, rip=None, label_x=None): + """`segments`: the address space as one tall rectangle, high addresses on top, as the textbook draws it. + Bands in their mapping's ink (55% toward the page), a file's mappings touching, its parts split by a page + line; a hole a fixed 40 with a break and its size. Labels beside with leaders: the left the band's first + address, the right its permissions, what it is, its size; a file's parts bracketed with the file's name. + Live: rsp's and rip's pins at their place; a band framed when a word is inked into it; growth arrows.""" + W = 140 + lx = label_x or x + W + 40 + yy, last_lo, groups, pos = y, None, [], {} + g.t(x - 12, yy - 6, "ffffffffffffffff", 18, FAINT, "sans", "end") + for lo, hi, perm, name in MAPPINGS: + if last_lo is not None and hi < last_lo: # a hole: compressed to 40, a break and its size + gap = last_lo - hi + g.rect(x, yy, W, 40, PAGE) + g.frame(x, yy - 1, W, 42, RULE, 2) + for dy in (14, 24): + g.path(" ".join(["M%d,%d" % (x - 6, yy + dy)] + ["l%d,%d" % (10, -6 if k % 2 == 0 else 6) + for k in range(16)]), RULE, 2) + for kk, (unit, k) in enumerate((("TiB", 1 << 40), ("GiB", 1 << 30), ("MiB", 1 << 20), ("KiB", 1 << 10))): + if gap >= k: + g.t(lx, yy + 26, "nothing mapped · %s" % size(gap), 18, FAINT, "sans") + break + yy += 40 + h = band_h(hi - lo) + ink = INK[perm] if name not in ("[stack]", "[heap]") else (STACK if name == "[stack]" else HEAP) + g.rect(x, yy, W, h, mix(ink, PAGE, 0.55)) + g.rect(x, yy, W, 2, PAGE) # a mapping's edge + mid = yy + h / 2 + g.path(f"M{x + W + 4},{mid} H{lx - 8}", RULE, 2) # the leader + g.t(lx, mid + 7, perm, S, ink if perm != "r--" else VELLUM, MONO) + g.t(lx + 4 * A, mid + 7, (name if name.startswith("[") else WHAT[perm]) + ("" if name else " (anonymous)"), + 18, DIM, "sans") + g.t(lx + 4 * A + 170, mid + 7, size(hi - lo), S, FAINT, MONO) + C.addr(g, x - 12 - len("%x" % lo) * A, yy + h - ROW, lo, ink) + if lo in hot: + g.frame(x - 4, yy - 2, W + 8, h + 4, READ, 3) + pos[name, lo] = (yy, h) + if name and not name.startswith("[") and (not groups or groups[-1][0] != name): + groups.append([name, yy, yy + h]) + elif name and groups and groups[-1][0] == name: + groups[-1][2] = yy + h + yy += h + last_lo = lo + for name, a, z in groups: # a file's mappings: one thing, with parts + C.bracket(g, lx + 4 * A + 280, a + 3, z - 3, DIM, 2, 6, side=-1) + g.t(lx + 4 * A + 292, (a + z) / 2 + 7, name, S, VELLUM, MONO) + g.t(x - 12, yy + 18, "0", 18, FAINT, "sans", "end") + # growth: the stack down from its top, the heap up from its base + sy, sh = pos["[stack]", 0x7ffffffde000] + gen.arrow(g, [(x + W - 24, sy + 8), (x + W - 24, sy + sh - 6)], BRIGHT, 2, hl=8, hw=8) + hy, hh = pos["[heap]", 0x1004000] + gen.arrow(g, [(x + W - 24, hy + hh - 6), (x + W - 24, hy + 8)], BRIGHT, 2, hl=8, hw=8) + if rsp: # a register's pin at its place in its band + f = (0x7ffffffff000 - rsp) / (0x7ffffffff000 - 0x7ffffffde000) + py = sy + f * sh + g.t(x + 8, py + 7, "rsp", S, VELLUM, BOLD) + g.path(f"M{x + 48},{py} l9,-6 v12 Z", fill=STACK) + if rip: + cy, ch = pos["list", 0x1001000] + g.t(x + 8, cy + ch / 2 + 7, "rip", S, VELLUM, BOLD) + g.path(f"M{x + 48},{cy + ch / 2} l9,-6 v12 Z", fill=CODE) + g.t(x, y - 34, "the address space", 20, VELLUM, "sansm") + g.t(x + 200, y - 34, "high addresses on top · not to scale: holes 40, bands by the log of their size", 18, + FAINT, "sans") + return yy + + +def frame_space(name): + """Mock 34: the rectangle alone, every label reading.""" + gen.W, gen.H = 1920, 1080 + g = Svg() + segments(g, 560, 110, rsp=RSP, rip=0x1001926, label_x=760) + g.save(name) + + def camera(g, k, cx, cy): """The camera at scale k on the point (cx, cy) of the frame: all drawing after this is through it.""" g.o.append(f'') @@ -108,7 +206,11 @@ def frame_words(name): k = 1 # 7fffffffece8: the return address, main+80 g.frame(12 * A + 12 - 4, ys[k] - 1, 8 * 30 + 4, ROW + 2, READ, 2) g.frame(12 * A + 12 + 8 * 30 + 24 - 6, ys[k] - 1, 7 * A + 12, ROW + 2, READ, 2) - segments(g, 12 * A + 12 + 8 * 30 + 24 + 13 * A + 36, -24, 560, hot={0x1001000}) + g.o.append("") + g.o.append('') + g.o.append('') # the stage holds it whole above the caption + segments(g, 200, 0, hot={0x1001000}, rsp=RSP, label_x=380) + g.o.append("") g.o.append("") gen.caption(g, [[("Eight bytes make a word, stored low byte first: read right to left,",)], [("this one is ",), ("10018b0", "mono"), (", an address in the code, so it takes code's ink.",)]]) @@ -136,56 +238,60 @@ BYTES = {0x1001910: "48 85 ff", 0x1001913: "74 20", 0x1001915: "55", 0x1001916: def aligned(g, y, first, n, cur, cur_line, bytes_=False, title="reverse"): - """`aligned`: the source and the disassembly side by side, a table in address order: each source line level - with the first row of its run of instructions, a line that comes again (a second run) repeated faint at that - run, a line with no code of its own not drawn (its number missing says so), a row of no line (padding) with - nothing beside it. The pc's row and its line are one band across both columns: the alignment is the thread.""" + """`aligned`, one panel: the listing interleaved as objdump -S prints it. Each run of a line's instructions + is a group: a head row (the line on raised paper, its number in the place column) and under it its rows, + their addresses in the same column, indented under the line's text. A 2-unit rule over every head: where a + line starts and ends. A line's second run: its head again, faint; rows of no line: a faint head saying so.""" rows = [r for r in D.disasm_rows() if r[0] >= first][:n] - h = ph(n) - C.tag(g, 64, y, 880, h, "list.c", note="each line beside the first of its instructions") - C.tag(g, 976, y, 880, h, "disassembly", note=title) - y0 = y + TAG + PAD - seen, last_line = set(), None - lead = C.shared([r[0] for r in rows]) - i0 = 0 # each run's extent: a quiet bracket by the gutter, line to its last row - for i in range(1, n + 1): - if i == n or rows[i][2] != rows[i0][2]: - if rows[i0][2] is not None: - C.bracket(g, 932, y + TAG + PAD + i0 * ROW + 3, y + TAG + PAD + i * ROW - 3, RULE, 2, 5, side=-1) - i0 = i - for i, (a, func, line, mn, ops) in enumerate(rows): - yy = y0 + i * ROW - on = a == cur - if on: # one band through the gutter: the line and its instruction are one - g.rect(64 + 2, yy, 880 - 4, ROW, C.WASH) - g.rect(944, yy, 32, ROW, C.WASH) - g.rect(976 + 2, yy, 880 - 4, ROW, C.WASH) - g.rect(976 + 2, yy, 24, ROW, GOLD) - g.path(f"M{976 + 20},{yy + 12} l-10,-6 v12 Z", fill=gen.INK) - if line is not None and line != last_line: - again = line in seen - if line == cur_line and on: - g.rect(64 + 2, yy, 50, ROW, GOLD) - g.t(64 + 46, b(yy), str(line), S, gen.INK, BOLD, "end") + groups, cur_g = [], None + for r in rows: + key = (r[2], r[1]) if r[2] is not None else (None, r[1]) + if cur_g is None or cur_g[0] != key: + cur_g = [key, []] + groups.append(cur_g) + cur_g[1].append(r) + total = len(rows) + len(groups) + C.tag(g, 64, y, 1792, ph(total), "list.c · %s" % title, focus=True, + note="each line, then the instructions it compiled to · lines and addresses in one column") + yy, seen = y + TAG + PAD, set() + PLACE, TEXT = 100 + 7 * A, 220 # the place column ends at 196; the text 24 after it + for (line, func), grp in groups: + g.rect(64 + 2, yy, 1792 - 4, 2, RULE) # where a line's group starts (and the last one ends) + again = line in seen + on_line = line == cur_line and any(r[0] == cur for r in grp) + g.rect(64 + 2, yy + 2, 1792 - 4, ROW - 2, RAISED if not again else PAGE) + if line is None: + g.t(TEXT, b(yy), "%s · no line" % func, 18, FAINT, "sans") + else: + if on_line: + g.rect(PLACE - 50 + 12, yy + 2, 50, ROW - 2, GOLD) + g.t(PLACE + 12, b(yy), str(line), S, gen.INK if on_line else (FAINT if again else DIM), BOLD if on_line else MONO, + "end") + text = D.SRC[line - 1].strip() + if again: + g.t(TEXT, b(yy), text + " again: its second run", S, FAINT, MONO) else: - g.t(64 + 46, b(yy), str(line), S, FAINT, MONO, "end") - text = D.SRC[line - 1] - C.c_line(g, 64 + 64, yy, text, 66) if not again else g.t(64 + 64, b(yy), text.strip(), S, FAINT, MONO) + C.c_line(g, TEXT, yy, text, 80) seen.add(line) - elif line is None and (i == 0 or rows[i - 1][1] != func): - g.t(64 + 64, b(yy), "%s · no line" % func, 18, FAINT, "sans") - last_line = line - C.addr(g, 976 + 40, yy, a, CODE, lead=lead) - x = 976 + 40 + 8 * A + 12 - if bytes_: - g.t(x, b(yy), BYTES.get(a, ""), S, BRIGHT if on else DIM, MONO) - x += 15 * A - g.t(x, b(yy), mn, S, FAINT if mn == "nop" else KW, MONO if mn == "nop" else BOLD) - C.operands(g, x + 7 * A, yy, mn, ops, VELLUM) - if on: - C.chain(g, 976 + 600, yy, [(0x1004098, HEAP), (0x1004070, HEAP)], ("int", 9), {0: "read"} if False else None, - room=976 + 868) - return y0 + yy += ROW + for a, f_, ln, mn, ops in grp: + on = a == cur + if on: + g.rect(64 + 2, yy, 1792 - 4, ROW, C.WASH) + g.rect(64 + 2, yy, 24, ROW, GOLD) + g.path(f"M{64 + 20},{yy + 12} l-10,-6 v12 Z", fill=gen.INK) + C.addr(g, PLACE + 12 - len("%x" % a) * A, yy, a, CODE, lead=4) + x = TEXT + 2 * A # indented under the line it came from + if bytes_: + g.t(x, b(yy), BYTES.get(a, ""), S, BRIGHT if on else DIM, MONO) + x += 15 * A + g.t(x, b(yy), mn, S, FAINT if mn == "nop" else KW, MONO if mn == "nop" else BOLD) + C.operands(g, x + 7 * A, yy, mn, ops, VELLUM) + if on: + C.chain(g, 64 + 900, yy, [(0x1004098, HEAP), (0x1004070, HEAP)], ("int", 9), None, room=64 + 1780) + yy += ROW + g.rect(64 + 2, yy, 1792 - 4, 2, RULE) + return yy def frame_code(name): @@ -203,9 +309,9 @@ def frame_aligned(name): """Mock 33: the `aligned` split, reverse and list_done, the pc at the load.""" gen.W, gen.H = 1920, 1080 g = Svg() - aligned(g, 48, 0x1001910, 30, 0x1001926, 21) - gen.caption(g, [[("Line 20, ",), ("while (head)", "mono"), (", has two runs: its test before the body",)], - [("and again after it. Lines 23 to 25 have no code of their own.",)]]) + aligned(g, 48, 0x1001910, 16, 0x1001926, 21) + gen.caption(g, [[("Line 20, ",), ("while (head)", "mono"), (", comes twice: its test before the body and",)], + [("again after it. Lines 23 to 25 have no code of their own: no group.",)]]) g.save(name) @@ -215,3 +321,4 @@ if __name__ == "__main__": frame_stack("31-intro-stack") frame_code("32-intro-code") frame_aligned("33-aligned") + frame_space("34-segments") -- cgit v1.3