summaryrefslogtreecommitdiff
path: root/tools/dump-auxv.py
blob: 36f3b37bf42f7bbffe85b28a98857b8664e2de3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/python
# SPDX-License-Identifier: LGPL-2.1-or-later

"""
A program to parse auxv (e.g. /proc/self/auxv).

By default, current arch is assumed, but options can be used to override the
endianness and word size.
"""

import struct

import click

# From /usr/include/elf.h
AT_AUXV = {
    'AT_NULL' :          0,              # End of vector
    'AT_IGNORE' :        1,              # Entry should be ignored
    'AT_EXECFD' :        2,              # File descriptor of program
    'AT_PHDR' :          3,              # Program headers for program
    'AT_PHENT' :         4,              # Size of program header entry
    'AT_PHNUM' :         5,              # Number of program headers
    'AT_PAGESZ' :        6,              # System page size
    'AT_BASE' :          7,              # Base address of interpreter
    'AT_FLAGS' :         8,              # Flags
    'AT_ENTRY' :         9,              # Entry point of program
    'AT_NOTELF' :        10,             # Program is not ELF
    'AT_UID' :           11,             # Real uid
    'AT_EUID' :          12,             # Effective uid
    'AT_GID' :           13,             # Real gid
    'AT_EGID' :          14,             # Effective gid
    'AT_CLKTCK' :        17,             # Frequency of times()

    # Some more special a_type values describing the hardware.
    'AT_PLATFORM' :      15,             # String identifying platform.
    'AT_HWCAP' :         16,             # Machine-dependent hints about processor capabilities.

    # This entry gives some information about the FPU initialization performed by the kernel.
    'AT_FPUCW' :         18,             # Used FPU control word.

    # Cache block sizes.
    'AT_DCACHEBSIZE' :   19,             # Data cache block size.
    'AT_ICACHEBSIZE' :   20,             # Instruction cache block size.
    'AT_UCACHEBSIZE' :   21,             # Unified cache block size.

    # A special ignored value for PPC, used by the kernel to control the
    # interpretation of the AUXV. Must be > 16.
    'AT_IGNOREPPC' :     22,             # Entry should be ignored.

    'AT_SECURE' :        23,             # Boolean, was exec setuid-like?

    'AT_BASE_PLATFORM' : 24,             # String identifying real platforms.

    'AT_RANDOM' :        25,             # Address of 16 random bytes.

    'AT_HWCAP2' :        26,             # More machine-dependent hints about processor capabilities.

    'AT_EXECFN' :        31,             # Filename of executable.

    # Pointer to the global system page used for system calls and other nice things.
    'AT_SYSINFO' :       32,
    'AT_SYSINFO_EHDR' :  33,

    # Shapes of the caches.  Bits 0-3 contains associativity; bits 4-7 contains
    # log2 of line size; mask those to get cache size.
    'AT_L1I_CACHESHAPE' :    34,
    'AT_L1D_CACHESHAPE' :    35,
    'AT_L2_CACHESHAPE' :     36,
    'AT_L3_CACHESHAPE' :     37,

    # Shapes of the caches, with more room to describe them.
    # GEOMETRY are comprised of cache line size in bytes in the bottom 16 bits
    # and the cache associativity in the next 16 bits.
    'AT_L1I_CACHESIZE' :     40,
    'AT_L1I_CACHEGEOMETRY' : 41,
    'AT_L1D_CACHESIZE' :     42,
    'AT_L1D_CACHEGEOMETRY' : 43,
    'AT_L2_CACHESIZE' :      44,
    'AT_L2_CACHEGEOMETRY' :  45,
    'AT_L3_CACHESIZE'     :  46,
    'AT_L3_CACHEGEOMETRY' :  47,

    'AT_MINSIGSTKSZ'      :  51,         # Stack needed for signal delivery
}
AT_AUXV_NAMES = {v:k for k,v in AT_AUXV.items()}

@click.command(help=__doc__)
@click.option('-b', '--big-endian', 'endian',
              flag_value='>',
              help='Input is big-endian')
@click.option('-l', '--little-endian', 'endian',
              flag_value='<',
              help='Input is little-endian')
@click.option('-3', '--32', 'field_width',
              flag_value=32,
              help='Input is 32-bit')
@click.option('-6', '--64', 'field_width',
              flag_value=64,
              help='Input is 64-bit')
@click.argument('file',
                type=click.File(mode='rb'))
def dump(endian, field_width, file):
    data = file.read()

    if field_width is None:
        field_width = struct.calcsize('P') * 8
    if endian is None:
        endian = '@'

    width = {32:'II', 64:'QQ'}[field_width]

    format = f'{endian}{width}'
    print(f'# {format=}')

    seen_null = False

    for item in struct.iter_unpack(format, data):
        key, val = item
        name = AT_AUXV_NAMES.get(key, f'unknown ({key})')
        if name.endswith(('UID', 'GID')):
            pref, fmt = '', 'd'
        else:
            pref, fmt = '0x', 'x'

        if seen_null:
            print('# trailing garbarbage after AT_NULL')

        print(f'{name:18} = {pref}{val:{fmt}}')

        if name == 'AT_NULL':
            seen_null = True

    if not seen_null:
        print('# array not terminated with AT_NULL')

if __name__ == '__main__':
    dump()