b1bebe21e0bc74fbd65535e9421bc3dc8c5e2322
explain/proc.md
... | ... | @@ -0,0 +1,35 @@ |
1 | +## Process creation |
|
2 | + |
|
3 | +Main source: "How programs get run", on lwn |
|
4 | +[1](https://lwn.net/Articles/630727/), [2](https://lwn.net/Articles/631631/). |
|
5 | +**READ THIS FIRST** |
|
6 | + |
|
7 | +There are a few details that are crucial for sizecoding stuff. On program entry: |
|
8 | + |
|
9 | +* `PT_LOAD` phdrs allocate memory, or map data or code from the executable into |
|
10 | + memory. |
|
11 | +* `PT_INTERP` makes the kernel load a second program and execute *that one*, |
|
12 | + after mapping the first one into memory. |
|
13 | +* The kernel doesn't care about other phdrs. |
|
14 | +* There is a minimum address for memory mapping, addresses lower than this |
|
15 | + value cannot be mapped into userspace memory. This config is available at |
|
16 | + `/proc/sys/vm/mmap_min_addr`, but can only be written to by root. |
|
17 | +* The kernel maps pages, not bytes, so the size fields in a phdr are always |
|
18 | + aligned to the next page. Bytes that are not mapped from a file, or are |
|
19 | + "loaded" after the end of the file, are set to zero. |
|
20 | +* Pretty much all registers that aren't a stack pointer or program counter are |
|
21 | + set to zero. *This is NOT true when doing dynamic linking*! |
|
22 | +* On `x86_64` (and maybe `i386`?), [the stack is aligned to 16 bytes |
|
23 | + ](https://refspecs.linuxbase.org/elf/x86_64-abi-0.99.pdf). The |
|
24 | + `x86_64` calling convention says that the stac pointer mod 16 must be 8 when |
|
25 | + calling a function. [SIMD instructions sometimes require 16-byte alignment |
|
26 | + ](https://pcy.ulyssis.be/intelrefspec.pdf). |
|
27 | + Data on which SIMD instructions are working is sometimes stored on the stack. |
|
28 | + *This means that, if you do not manually realign the stack, crashes will |
|
29 | + happen when doing SIMD. **This code may be in libraries you're depending on, |
|
30 | + and depending on the distro, libraries may or may not be compiled with SIMD |
|
31 | + instructions!** * This can be fixed with one byte: `push rax`. |
|
32 | +* Lots of interesting data is placed on the stack at program entry. See the |
|
33 | + second lwn article for details. |
|
34 | +* For dynamic linking-related stuff on program entry, see [this |
|
35 | + page](/explain/rtld) |