drgn/generate_elf_constants.py
Omar Sandoval acbf14d10c dwarf: rewrite DwarfIndex in pure C
Most of drgn.dwarf is not performance-sensitive, and the part that is
(DwarfIndex) can use some extra tuning which is easier to do in C rather
than Cython.
2018-03-25 02:10:15 -07:00

50 lines
1.2 KiB
Python
Executable File

#!/usr/bin/env python3
import re
constant_prefixes = [
'DT_',
'EI_',
'ELFCLASS',
'ELFCOMPRESS',
'ELFDATA',
'ELFMAG',
'ELFOSABI',
'EM_',
'ET_',
'EV_',
'GRP_',
'PF_',
'PT_',
'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')
for constant, value in matches:
if value.startswith("'"):
assert len(value) == 3 and value.endswith("'")
value = hex(ord(value[1]))
elif value.startswith('"'):
continue
if constant == 'DT_PROCNUM':
value = next(match[1] for match in matches if match[0] == value)
# Special case for SHF_EXCLUDE.
if value == '(1U << 31)':
value = '(1 << 31)'
print(f'{constant} = {value}')