linux_libc_auxv/lib.rs
1/*
2MIT License
3
4Copyright (c) 2025 Philipp Schuster
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22SOFTWARE.
23*/
24
25//! # linux-libc-auxv
26//!
27//! A parser and builder for the Linux process initial stack layout - use it to
28//! decode or construct `argc`, `argv`, `envp`/`envv`, and `auxv` (auxiliary
29//! vector).
30//!
31//! **Keywords**: crt0, stack layout, AT values, AT pairs, auxvec, auxiliary vector
32//!
33//! ## Terminology
34//!
35//! I use `argv`, `envv`, and `auxv` as names for the null-terminated arrays
36//! (vectors) of the corresponding data. They have corresponding "data areas" where
37//! pointers point to: _argv data area_, _envp data area_, and _auxv data area_.
38//!
39//! ## About the Stack Layout
40//!
41//! Linux passes an initial stack layout to applications that contains `argc`,
42//! `argv`, `envp`/`envv`, and the `auxiliary vector`. Normal applications don't
43//! see this as the libc (crt0 component) abstracts this away. For more low-level
44//! developers and kernel hackers creating and parsing this feature becomes
45//! relevant.
46//!
47//!
48//! This crate has been tested successfully by myself in a custom runtime system for
49//! a Microkernel which loads and starts unmodified Linux binaries. The Linux binary
50//! (the libc) was able to find all arguments, environment variables, and the data
51//! from the auxiliary vector. Everything was printed properly to stdout.
52//!
53//! ### Layout Structure
54//!
55//! The following figure shows the technical details of the layout. The figure is
56//! taken from <https://lwn.net/Articles/631631/>. As the structure needs to
57//! know the pointers beforehand, creating the structure is not trivial. The
58//! builder helps to construct this complex binary structure with references
59//! into itself.
60//!
61//! ```text
62//! ------------------------------------------------------------- 0x7fff6c845000
63//! 0x7fff6c844ff8: 0x0000000000000000
64//! _ 4fec: './stackdump\0' <------+
65//! env / 4fe2: 'ENVVAR2=2\0' | <----+
66//! \_ 4fd8: 'ENVVAR1=1\0' | <---+ |
67//! / 4fd4: 'two\0' | | | <----+
68//! args | 4fd0: 'one\0' | | | <---+ |
69//! \_ 4fcb: 'zero\0' | | | <--+ | |
70//! 3020: random gap padded to 16B boundary | | | | | |
71//! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -| | | | | |
72//! 3019: 'x86_64\0' <-+ | | | | | |
73//! auxv 3009: random data: ed99b6...2adcc7 | <-+ | | | | | |
74//! data 3000: zero padding to align stack | | | | | | | |
75//! . . . . . . . . . . . . . . . . . . . . . . . . . . .|. .|. .| | | | | |
76//! 2ff0: AT_NULL(0)=0 | | | | | | | |
77//! 2fe0: AT_PLATFORM(15)=0x7fff6c843019 --+ | | | | | | |
78//! 2fd0: AT_EXECFN(31)=0x7fff6c844fec ------|---+ | | | | |
79//! 2fc0: AT_RANDOM(25)=0x7fff6c843009 ------+ | | | | |
80//! ELF 2fb0: AT_SECURE(23)=0 | | | | |
81//! auxiliary 2fa0: AT_EGID(14)=1000 | | | | |
82//! vector: 2f90: AT_GID(13)=1000 | | | | |
83//! (id,val) 2f80: AT_EUID(12)=1000 | | | | |
84//! pairs 2f70: AT_UID(11)=1000 | | | | |
85//! 2f60: AT_ENTRY(9)=0x4010c0 | | | | |
86//! 2f50: AT_FLAGS(8)=0 | | | | |
87//! 2f40: AT_BASE(7)=0x7ff6c1122000 | | | | |
88//! 2f30: AT_PHNUM(5)=9 | | | | |
89//! 2f20: AT_PHENT(4)=56 | | | | |
90//! 2f10: AT_PHDR(3)=0x400040 | | | | |
91//! 2f00: AT_CLKTCK(17)=100 | | | | |
92//! 2ef0: AT_PAGESZ(6)=4096 | | | | |
93//! 2ee0: AT_HWCAP(16)=0xbfebfbff | | | | |
94//! 2ed0: AT_SYSINFO_EHDR(33)=0x7fff6c86b000 | | | | |
95//! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . | | | | |
96//! 2ec8: environ[2]=(nil) | | | | |
97//! 2ec0: environ[1]=0x7fff6c844fe2 ------------------|-+ | | |
98//! 2eb8: environ[0]=0x7fff6c844fd8 ------------------+ | | |
99//! 2eb0: argv[3]=(nil) | | |
100//! 2ea8: argv[2]=0x7fff6c844fd4 ---------------------------|-|-+
101//! 2ea0: argv[1]=0x7fff6c844fd0 ---------------------------|-+
102//! 2e98: argv[0]=0x7fff6c844fcb ---------------------------+
103//! 0x7fff6c842e90: argc=3
104//!```
105//!_Credits: <https://lwn.net/Articles/631631/>_
106//!
107//! ## Differences to Crates [`crt0stack`] and [`auxv`]
108//!
109//! This crate supports `no_std`-contexts plus **allows construction** of the data
110//! structure **for a different address space**, i.e. the address space of a user
111//! application.
112//!
113//! [`crt0stack`]: https://crates.io/crates/crt0stack
114//! [`auxv`]: https://crates.io/crates/auxv
115//!
116//! ## Functionality
117//!
118//! ✅ build data structure for current address space \
119//! ✅ build data structure for **different address space** \
120//! ✅ parse data structure for current address space + output referenced data/pointers \
121//! ✅ parse data structure for **different address space** + prevent memory error / no dereferencing of pointers
122//!
123//!
124//!
125//! ## MSRV
126//! 1.85.0 stable
127//!
128//! ## Background Information & Links
129//! - <https://lwn.net/Articles/631631/> (good overview with ASCII graphics)
130//! - <https://lwn.net/Articles/519085/>
131//! - <https://elixir.bootlin.com/linux/v5.15.5/source/fs/binfmt_elf.c#L257> (code in Linux that constructs `auxv`)
132//! - <https://man7.org/linux/man-pages/man3/getauxval.3.html>
133//! - <https://refspecs.linuxfoundation.org/ELF/zSeries/lzsabi0_zSeries/x895.html>
134
135#![deny(
136 clippy::all,
137 clippy::cargo,
138 clippy::nursery,
139 clippy::must_use_candidate,
140 // clippy::restriction,
141 // clippy::pedantic
142)]
143// now allow a few rules which are denied by the above statement
144// --> they are ridiculous and not necessary
145#![allow(
146 clippy::suboptimal_flops,
147 clippy::redundant_pub_crate,
148 clippy::fallible_impl_from
149)]
150#![deny(missing_docs)]
151#![deny(missing_debug_implementations)]
152#![deny(rustdoc::all)]
153#![no_std]
154
155#[cfg_attr(feature = "alloc", macro_use)]
156#[cfg(feature = "alloc")]
157extern crate alloc;
158
159#[cfg_attr(test, macro_use)]
160#[cfg(test)]
161extern crate std;
162
163pub use aux_var::{AuxVar, AuxVarFlags, AuxVarRaw, AuxVarType};
164#[cfg(feature = "builder")]
165pub use builder::StackLayoutBuilder;
166pub use parser::StackLayoutRef;
167
168mod aux_var;
169#[cfg(feature = "builder")]
170mod builder;
171mod parser;
172mod util;