Skip to content
systemcoding.dev
Systems Programming

Read the Auxiliary Vector with getauxval

getauxval reads the kernel's auxiliary vector: page size, CPU features, secure-mode flag, random bytes and the executable name, without parsing /proc.

7 min read
Summarize in AI
Flat illustration of a magnifier over lines of a log, with the C, Linux and GNU logos.

When the kernel starts a program, it passes more than arguments and environment variables. It also writes a list of numbered facts onto the new stack, called the auxiliary vector: how large a page is, which CPU features exist, whether the program is running with changed privileges, where the interpreter was loaded, and sixteen random bytes. The C library reads this vector during startup, and since glibc 2.16 your own code can read any entry with one function, getauxval(). This post shows the call, the entries worth knowing, how to tell "zero" from "missing", how to see the whole vector with LD_SHOW_AUXV, and what the values mean on x86-64 and arm64. It targets Linux 6.12 and glibc 2.40, and follows getauxval(3) and getauxval constants in <elf.h> and <sys/auxv.h>. The vector's place in the startup sequence is described in the complete guide to program startup.

The function

c
#include <sys/auxv.h>

unsigned long getauxval(unsigned long type);

type is one of the AT_ constants, and the return value is the entry's value, as an unsigned long. For some entries that value is a number (the page size), for others it is a pointer in disguise (the address of a string), and for a few it is a bit mask of capabilities.

If the kernel did not supply an entry, getauxval() returns 0 and sets errno to ENOENT. That is the only way to tell "this entry is absent" from "this entry is present and zero", and it matters for entries such as AT_SECURE, where 0 is a perfectly valid answer.

The entries you will actually use

ConstantMeaningTypical use
AT_PAGESZSystem page size in bytesAlign buffers, size mmap() calls, without calling sysconf()
AT_CLKTCKFrequency of times() ticksConvert tick counts to seconds
AT_SECURENonzero if started with changed credentialsDecide whether to trust the environment
AT_RANDOMAddress of 16 random bytesSeed canaries and hash randomization
AT_EXECFNAddress of the file name passed to execve()Find the executable path
AT_PLATFORMAddress of a string naming the hardware platformPrint or select code for the platform
AT_HWCAP, AT_HWCAP2Bit masks of CPU featuresChoose an optimized code path (arm64, others)
AT_BASEBase address of the interpreterDebug the dynamic loader
AT_SYSINFO_EHDRAddress of the vDSO ELF headerFind the kernel's user-space helper library
AT_UID, AT_EUID, AT_GID, AT_EGIDCredentials at exec timeCheck the IDs the kernel started with

Entries that return an address, AT_RANDOM, AT_EXECFN and AT_PLATFORM, are pointers into the process's stack; cast the unsigned long to const char * or const unsigned char *.

A first program

auxv_show.cc
#include <errno.h>
#include <stdio.h>
#include <sys/auxv.h>

static void show_number(const char *label, unsigned long type)
{
    errno = 0;
    unsigned long v = getauxval(type);
    if (v == 0 && errno == ENOENT)
        printf("%-14s absent\n", label);
    else
        printf("%-14s %lu\n", label, v);
}

int main(void)
{
    show_number("AT_PAGESZ", AT_PAGESZ);
    show_number("AT_CLKTCK", AT_CLKTCK);
    show_number("AT_SECURE", AT_SECURE);
    show_number("AT_UID", AT_UID);

    const char *exec_path = (const char *)getauxval(AT_EXECFN);
    const char *platform = (const char *)getauxval(AT_PLATFORM);
    printf("%-14s %s\n", "AT_EXECFN", exec_path ? exec_path : "(none)");
    printf("%-14s %s\n", "AT_PLATFORM", platform ? platform : "(none)");

    const unsigned char *rnd = (const unsigned char *)getauxval(AT_RANDOM);
    printf("%-14s ", "AT_RANDOM");
    for (int i = 0; i < 4 && rnd; i++)
        printf("%02x", rnd[i]);
    printf("... (first 4 of 16 bytes)\n");
    return 0;
}

Build with gcc -std=c17 -Wall -o auxv_show auxv_show.c and run it:

Text
AT_PAGESZ      4096
AT_CLKTCK      100
AT_SECURE      0
AT_UID         1000
AT_EXECFN      ./auxv_show
AT_PLATFORM    x86_64
AT_RANDOM      9f3a1c07... (first 4 of 16 bytes)

The user ID, and the random bytes in particular, will differ on your machine and on every run. AT_EXECFN is the name exactly as passed to execve(), which is the way to get a program's own path without trusting argv[0], though a path relative to a directory that has since changed is only as useful as the current directory still is. AT_PLATFORM is the platform string the kernel and the loader use to pick library directories.

Why AT_PAGESZ and not 4096

Writing 4096 is a bug waiting for another architecture. arm64 kernels may be built with 16 KiB or 64 KiB base pages, and some are: Asahi Linux on Apple Silicon uses 16 KiB, and several enterprise arm64 distributions ship 64 KiB kernels. Code that rounds sizes to 4096 and then calls mmap() or madvise() fails or wastes memory there. getauxval(AT_PAGESZ) returns the value the kernel is using for this process without a system call or a file read, and sysconf(_SC_PAGESIZE) returns the same number (glibc implements it from the auxiliary vector). Pick one and cache the result; do not hard-code.

c
static size_t round_up_to_page(size_t n)
{
    size_t page = getauxval(AT_PAGESZ);
    return (n + page - 1) & ~(page - 1);
}

The bit trick works because the page size is always a power of two.

AT_SECURE and trusting the environment

AT_SECURE is nonzero when the program was started with credentials that differ from the user who ran it: a setuid or setgid executable, or one with file capabilities, or one run under certain security modules. The C library and the dynamic loader both read it, and the loader ignores LD_PRELOAD and LD_LIBRARY_PATH when it is set. Your code can use it the same way, as the test for "treat the environment as hostile":

c
if (getauxval(AT_SECURE)) {
    /* ignore TMPDIR, PATH-relative lookups, locale search paths, ... */
}

secure_getenv() is the packaged version of exactly this check, and the post on sanitizing the environment of privileged programs builds on it. Use getauxval(AT_SECURE) directly when you need the decision in your own logic rather than a single variable lookup.

CPU feature flags with AT_HWCAP

On arm64 and several other architectures, AT_HWCAP is a bit mask of optional CPU features, and it is the standard way to select an optimized code path at run time. The bit definitions come from <asm/hwcap.h>:

hwcap_arm64.cc
#include <asm/hwcap.h>
#include <stdio.h>
#include <sys/auxv.h>

int main(void)
{
    unsigned long caps = getauxval(AT_HWCAP);
    printf("Advanced SIMD (NEON): %s\n", (caps & HWCAP_ASIMD) ? "yes" : "no");
    printf("AES instructions    : %s\n", (caps & HWCAP_AES) ? "yes" : "no");
    printf("CRC32 instructions  : %s\n", (caps & HWCAP_CRC32) ? "yes" : "no");
    return 0;
}
Text
Advanced SIMD (NEON): yes
AES instructions    : yes
CRC32 instructions  : yes

That output assumes a typical modern arm64 server. The program does not compile on x86-64 because <asm/hwcap.h> has no HWCAP_ASIMD there. On x86 the equivalent facts come from the cpuid instruction, which the compiler exposes through __builtin_cpu_supports("avx2"), so AT_HWCAP on x86 is of limited use. Libraries such as glibc use AT_HWCAP and AT_HWCAP2 to pick memcpy variants at load time, with the result cached in internal tables.

Seeing the whole vector

You do not have to write code to look at the vector. The dynamic loader prints it when the environment variable LD_SHOW_AUXV is set to anything:

Shell
LD_SHOW_AUXV=1 ./auxv_show | head -8
Text
AT_SYSINFO_EHDR:      0x7ffd9a7f8000
AT_MINSIGSTKSZ:       1776
AT_HWCAP:             178bfbff
AT_PAGESZ:            4096
AT_CLKTCK:            100
AT_PHDR:              0x55f4b3c6c040
AT_PHENT:             56
AT_PHNUM:             13

The list continues with AT_BASE, AT_FLAGS, AT_ENTRY, AT_UID, AT_SECURE, AT_RANDOM, AT_EXECFN and AT_PLATFORM, and its length and values depend on your kernel and CPU. This is the quickest way to see an entry you did not know existed. The loader honors LD_SHOW_AUXV only when AT_SECURE is clear, which is the same protective rule again.

What reads from the vector

Everyone relies on the auxiliary vector without noticing. The dynamic loader uses AT_PHDR, AT_PHNUM and AT_BASE to find the program's headers and its own image. glibc uses AT_RANDOM to seed the stack protector canary and the pointer guard, and AT_HWCAP to select optimized string functions. AT_SYSINFO_EHDR points at the vDSO, the small shared object the kernel maps into every process, which is how clock_gettime() can run without a system call. Each of these is a place where the kernel hands user space a fact that would be slow or impossible to discover by other means.

Limits and portability

getauxval() is a glibc and musl function, available in glibc 2.16 and later and in Android's Bionic. It is not POSIX, and macOS and the BSDs have no equivalent under that name: FreeBSD offers elf_aux_info(), which has a different signature. On Linux systems older than glibc 2.16, parse /proc/self/auxv, a binary file of type and value pairs that holds the same data. Entries are architecture-specific: AT_HWCAP bits mean different things on arm64 and PowerPC, and a few entries exist only on some architectures. Check the constants for your target in <elf.h> and <asm/hwcap.h> before using them.

Share

Keep reading

Flat illustration of two options weighed side by side, with the macOS, Linux and Apple logos.
Kernels

Mach Traps vs BSD System Calls on macOS

Learn how macOS routes one trap instruction to either a Mach trap or a BSD system call, how the number sign and class decide, and why you should call libSystem.

8 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.