WEBVTT

00:00.000 --> 00:05.983
Week 15: Headers Say Where, for the extra-credit exercise 54k_elf_loader.

00:06.583 --> 00:12.998
A script steps this explainer, it was built with Claude, and its numbers are examples, not the lecture's.

00:13.520 --> 00:17.199
This follows the reading on fork, wait, and the user shell.

00:17.799 --> 00:27.562
On Friday, December 4, once the day's exercise passes, you can write elf::load for extra credit: it puts a program where its headers say.

00:28.162 --> 00:31.329
No solution code here: only the machine's state.

00:31.840 --> 00:35.689
An ELF file holds a program, and says where its bytes go.

00:36.289 --> 00:44.521
This page's example file is 8,485 bytes: a 64-byte header, five program headers,

00:45.121 --> 00:50.002
and the ranges of bytes they name, three of them the segments, with gaps between.

00:50.520 --> 00:57.943
In the header, e_entry holds 0x6134, the address of the program's first instruction.

00:58.543 --> 01:05.395
e_phoff, 64, is where the program headers start, and e_phnum, 5, how many.

01:05.920 --> 01:12.009
p_type is the kind, p_flags the permissions, p_offset where the bytes start in the file,

01:12.609 --> 01:14.678
p_vaddr the address they belong at,

01:15.278 --> 01:20.983
p_filesz how many the file holds, p_memsz how much memory they take.

01:21.480 --> 01:26.993
PH1, PH2 and PH3 are PT_LOAD, the loadable kind.

01:27.593 --> 01:34.404
This page calls them .text, .rodata and .data: code, constants and variables.

01:35.004 --> 01:38.651
PH0 and PH4 describe no memory.

01:39.160 --> 01:43.180
The stepper shows each loadable header beside the page it fills.

01:43.780 --> 01:53.016
PH1: p_flags R X, 1,740 bytes, from the file's 0x1000, to belong at 0x6000.

01:53.520 --> 01:56.164
Page 0x6000, mapped R X U,

01:56.764 --> 02:03.035
holds them at offsets 0x000 to 0x6CB; the rest of the page is zero fill.

02:03.635 --> 02:08.231
Those are the header's letters, and the U bit, which lets user mode reach it.

02:09.760 --> 02:18.523
PH2: p_flags just R, 156 bytes, from the file's 0x2000 to belong at 0x7000.

02:19.123 --> 02:24.968
That page, with letters R and U, holds them at offsets 0x000 to 0x09B.

02:26.480 --> 02:29.061
PH3: p_flags R W.

02:29.661 --> 02:33.564
48 bytes in the file, 440 in memory.

02:34.164 --> 02:41.438
The explainer asks: this segment begins at 0x90A8. At which address is its page mapped?

02:44.940 --> 02:47.755
Page 0x9000, R W U.

02:48.355 --> 02:53.197
The file's 48 bytes sit at offsets 0x0A8 to 0x0D7.

02:53.797 --> 02:57.284
Before them, zero fill: the page outside the segment.

02:57.800 --> 03:07.427
Offsets 0x0D8 to 0x25F are .bss: memory the header asks for past the file's bytes. It holds zeros.

03:08.027 --> 03:10.191
The rest of the page is zero fill.

03:10.791 --> 03:13.403
Around the three pages, nothing is mapped.

03:14.920 --> 03:26.137
In user mode, pc is 0x6134, the program's first instruction; epc, the saved pc in the trapframe, holds it too.

03:26.737 --> 03:31.333
The fetch reaches page 0x6000, R X U, and goes through.

03:32.840 --> 03:43.747
The instruction at 0x6144 stores four bytes at 0x90C0, on page 0x9000: they now read done.

03:44.347 --> 03:49.878
The reference, the finished kernel, runs the test's own program, the probe, and passes.

03:50.400 --> 03:53.215
Now three wrong loaders, on the same example.

03:53.815 --> 03:57.428
Kernel A maps the same three pages, with the same letters,

03:58.028 --> 04:01.513
but its program starts at 0, where nothing is mapped.

04:03.040 --> 04:07.039
The fetch at 0 is refused: an instruction page fault.

04:07.639 --> 04:14.955
For kernel A, on the probe, the test prints: [fail] probe faulted — are all three PT_LOAD segments mapped

04:15.480 --> 04:22.873
Kernel B maps the same three pages, but its program starts at 0x6000, the code segment's first byte.

04:23.873 --> 04:29.329
The word there is filler, not an instruction the program runs: an illegal instruction.

04:29.929 --> 04:31.731
The test prints the same line.

04:32.240 --> 04:35.365
Kernel C maps all three pages R X U.

04:36.365 --> 04:41.965
Its program starts at 0x6134, and the fetch goes through.

04:43.480 --> 04:51.010
The store at 0x90C0 is refused: a store page fault. Those four bytes still read slot.

04:51.610 --> 04:53.849
And the test prints the same line again.

04:54.360 --> 04:56.339
Three wrong loaders, one line.

04:56.939 --> 05:05.015
For the skeleton, the code as you receive it, and kernels A, B and C, the test printed this console, byte for byte.

05:05.615 --> 05:11.119
Its first fail line: [fail] probe faulted — are all three PT_LOAD segments mapped

05:11.640 --> 05:15.477
One more wrong loader, kernel D, another broken elf::load:

05:16.077 --> 05:21.587
the test printed the reference's console for it. It passes the test, and it is wrong.

05:22.120 --> 05:25.520
You write the body of elf::load, in elf.rs.

05:26.120 --> 05:36.934
When it returns, each page a loadable segment touches must be mapped at the segment's own addresses, with its own permissions, holding the file's bytes and zeros.

05:37.440 --> 05:39.914
And the program must start at e_entry.

05:40.514 --> 05:45.494
Handed a malformed file, elf::load must refuse it rather than load it.

05:46.000 --> 05:51.906
The test loads its own ELF, the probe, with three loadable segments and a .bss.

05:52.506 --> 05:59.967
When every check passes, the test prints: [ok] entry point, per-segment permissions, and .bss zeroing

06:00.480 --> 06:07.130
To read a failure: when the probe faults, the test stops at its [fail] line, and the checks after it never run.

06:07.730 --> 06:12.987
Every loader that failed printed that same line; the stepper showed where each differs.

06:13.520 --> 06:19.470
An ELF file's headers say where each loadable segment goes, and where its program starts.

06:20.070 --> 06:24.311
Next: the explainer The Timer Takes It Back, then Friday's session.
