Environment variables
| The environment block | |
| Finding a variable | |
| The KEY=VALUE format |
The environment block
After the argv array and its NULL terminator, the stack holds the environment: an array of pointers to strings of the form KEY=VALUE, also terminated by NULL:
_start:
pop rdi ; argc
add rsp, 8 ; skip argv[0] ... walk to NULL
; then: environment pointers begin
Finding a variable
To find a variable, walk the environment pointers, and for each string compare the leading bytes against the key plus the equals sign. The value is the rest of the string after the equals sign:
mov rsi, [rsp] ; one environ string
; compare rsi against "HOME="
; value = rsi + 5
The KEY=VALUE format
The strings are not arrays: they are C strings, so the value runs to the next NUL byte. Print it with sys_write after scanning for that NUL to learn its length.
Next: Floating point (SSE)
Article author: Arthur Isaev