2018-02-19 08:46:39 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
|
|
constant_prefixes = [
|
|
|
|
'DT_',
|
|
|
|
'EI_',
|
|
|
|
'ELFCLASS',
|
|
|
|
'ELFCOMPRESS',
|
|
|
|
'ELFDATA',
|
|
|
|
'ELFMAG',
|
|
|
|
'ELFOSABI',
|
|
|
|
'EM_',
|
|
|
|
'ET_',
|
|
|
|
'EV_',
|
|
|
|
'GRP_',
|
|
|
|
'PF_',
|
|
|
|
'PT_',
|
2018-03-10 01:41:54 +00:00
|
|
|
'R_X86_64',
|
2018-02-19 08:46:39 +00:00
|
|
|
'SHF_',
|
|
|
|
'SHN_',
|
|
|
|
'SHT_',
|
|
|
|
'STB_',
|
|
|
|
'STT_',
|
|
|
|
'STV_',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
with open('/usr/include/elf.h', 'r') as f:
|
|
|
|
elf_h = f.read()
|
|
|
|
elf_h = re.sub(r'/\*.*?\*/', '', elf_h, flags=re.DOTALL)
|
|
|
|
elf_h = re.sub(r'\\\n', '', elf_h)
|
|
|
|
matches = re.findall(r'^#define\s+((?:' + '|'.join(constant_prefixes) + r')\w*)\s+(.+?)\s*$',
|
|
|
|
elf_h, re.MULTILINE)
|
|
|
|
|
|
|
|
print('# Automatically generated from elf.h')
|
2018-03-10 01:41:54 +00:00
|
|
|
print('cdef enum:')
|
2018-02-19 08:46:39 +00:00
|
|
|
for constant, value in matches:
|
|
|
|
if value.startswith("'"):
|
|
|
|
assert len(value) == 3 and value.endswith("'")
|
|
|
|
value = hex(ord(value[1]))
|
|
|
|
elif value.startswith('"'):
|
2018-03-10 01:41:54 +00:00
|
|
|
continue
|
2018-02-19 08:46:39 +00:00
|
|
|
if constant == 'DT_PROCNUM':
|
|
|
|
value = next(match[1] for match in matches if match[0] == value)
|
|
|
|
# Special case for SHF_EXCLUDE.
|
2018-03-10 01:41:54 +00:00
|
|
|
# if value == '(1U << 31)':
|
|
|
|
# value = '(1 << 31)'
|
|
|
|
print(f' {constant} = {value}')
|