load_elf: copy linux's auxv, argv, and env layout
[openpower-isa.git] / src / openpower / syscalls / ppc_flags.py
1 # SPDX-License-Identifier: LGPLv3+
2 # Copyright (C) 2023 Jacob Lifshay <programmerjake@gmail.com>
3 # Funded by NLnet http://nlnet.nl
4 """ flags for PowerPC syscalls
5
6 related bugs:
7
8 * https://bugs.libre-soc.org/show_bug.cgi?id=1169
9 """
10
11 def parse_defines(flags, compiler):
12 """ parse `#define`s into the dict `flags` using the given `compiler`
13 """
14 from subprocess import run, PIPE
15 inp = """
16 #include <sys/mman.h>
17 #include <errno.h>
18 #include <unistd.h>
19 #include <sys/syscall.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <linux/utsname.h>
24 #include <linux/auxvec.h>
25 #include <sys/auxv.h>
26 """
27 if isinstance(compiler, str):
28 compiler = [compiler]
29 out = run([*compiler, '-E', '-dM', '-'], input=inp,
30 check=True, stdout=PIPE, encoding='utf-8').stdout
31 def_start = '#define '
32 defines = {}
33 for define in out.splitlines():
34 assert define.startswith(def_start)
35 define = define[len(def_start):]
36 name, space, value = define.partition(' ')
37 assert space == ' '
38 if not name.isidentifier():
39 continue
40 defines[name] = value
41 # resolve things defined in terms of other things
42 more_substitutions = True
43 while more_substitutions:
44 more_substitutions = False
45 for name, value in defines.items():
46 new_value = defines.get(value)
47 if new_value is not None and new_value != value:
48 defines[name] = new_value
49 more_substitutions = True
50 for name, value in defines.items():
51 if value.startswith('(') and value.endswith(')'):
52 value = value[1:-2]
53 if len(value) > 1 and value.startswith('0') and value[1].isdigit():
54 value = '0o' + value[1:]
55 try:
56 flags[name] = int(value, 0)
57 except ValueError:
58 pass
59 return flags
60
61 parse_defines(globals(), 'powerpc64le-linux-gnu-gcc')
62
63 def _host_defines():
64 import sysconfig
65 return parse_defines({}, sysconfig.get_config_var('CC').split(' '))
66
67 host_defines = _host_defines()
68 del _host_defines