| 1 |
#!/usr/bin/env python3 |
| 2 |
# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
|
| 5 |
import argparse |
| 6 |
import itertools |
| 7 |
import sys |
| 8 |
from typing import TextIO |
| 9 |
|
| 10 |
from codegen_utils import c_string_literal |
| 11 |
|
| 12 |
DWARF_INDEX_SECTIONS = ( |
| 13 |
".debug_info", |
| 14 |
".debug_types", |
| 15 |
".debug_abbrev", |
| 16 |
".debug_str", |
| 17 |
".debug_str_offsets", |
| 18 |
) |
| 19 |
|
| 20 |
CACHED_SECTIONS = ( |
| 21 |
".debug_addr", |
| 22 |
".debug_frame", |
| 23 |
".eh_frame", |
| 24 |
".debug_loc", |
| 25 |
".debug_loclists", |
| 26 |
) |
| 27 |
|
| 28 |
UNCACHED_SECTIONS = ( |
| 29 |
".text", |
| 30 |
".got", |
| 31 |
".gnu_debuglink", |
| 32 |
".gnu_debugaltlink", |
| 33 |
) |
| 34 |
|
| 35 |
|
| 36 |
def section_enumerator_name(section_name: str) -> str: |
| 37 |
return "DRGN_SCN_" + section_name.lstrip(".").upper() |
| 38 |
|
| 39 |
|
| 40 |
def gen_elf_sections_h(out_file: TextIO) -> None: |
| 41 |
out_file.write( |
| 42 |
"""\ |
| 43 |
/* Generated by libdrgn/build-aux/gen_elf_sections.py -H. */ |
| 44 |
|
| 45 |
#ifndef DRGN_ELF_SECTIONS_H |
| 46 |
#define DRGN_ELF_SECTIONS_H |
| 47 |
|
| 48 |
/** |
| 49 |
* Identifiers for important ELF sections so that they can be referenced by |
| 50 |
* index rather than name. |
| 51 |
*/ |
| 52 |
enum drgn_section_index { |
| 53 |
""" |
| 54 |
) |
| 55 |
for section_name in DWARF_INDEX_SECTIONS: |
| 56 |
out_file.write(f"\t{section_enumerator_name(section_name)},\n") |
| 57 |
out_file.write( |
| 58 |
"""\ |
| 59 |
/** Indices less than this are used by the DWARF index. */ |
| 60 |
DRGN_SECTION_INDEX_NUM_DWARF_INDEX, |
| 61 |
""" |
| 62 |
) |
| 63 |
|
| 64 |
for i, section_name in enumerate(CACHED_SECTIONS): |
| 65 |
if i == 0: |
| 66 |
out_file.write( |
| 67 |
f"\t{section_enumerator_name(section_name)} = DRGN_SECTION_INDEX_NUM_DWARF_INDEX,\n" |
| 68 |
) |
| 69 |
else: |
| 70 |
out_file.write(f"\t{section_enumerator_name(section_name)},\n") |
| 71 |
out_file.write( |
| 72 |
"""\ |
| 73 |
/** Indices less than this may have their data cached. */ |
| 74 |
DRGN_SECTION_INDEX_NUM_DATA, |
| 75 |
""" |
| 76 |
) |
| 77 |
|
| 78 |
for i, section_name in enumerate(UNCACHED_SECTIONS): |
| 79 |
if i == 0: |
| 80 |
out_file.write( |
| 81 |
f"\t{section_enumerator_name(section_name)} = DRGN_SECTION_INDEX_NUM_DATA,\n" |
| 82 |
) |
| 83 |
else: |
| 84 |
out_file.write(f"\t{section_enumerator_name(section_name)},\n") |
| 85 |
out_file.write( |
| 86 |
"""\ |
| 87 |
/** Number of section indices. */ |
| 88 |
DRGN_SECTION_INDEX_NUM |
| 89 |
}; |
| 90 |
|
| 91 |
#endif /* DRGN_ELF_SECTIONS_H */ |
| 92 |
""" |
| 93 |
) |
| 94 |
|
| 95 |
|
| 96 |
def gen_drgn_section_name_to_index_inc_strswitch(out_file: TextIO) -> None: |
| 97 |
debug_sections = [] |
| 98 |
non_debug_sections = [] |
| 99 |
for section_name in itertools.chain( |
| 100 |
DWARF_INDEX_SECTIONS, CACHED_SECTIONS, UNCACHED_SECTIONS |
| 101 |
): |
| 102 |
if section_name.startswith(".debug_"): |
| 103 |
debug_sections.append(section_name) |
| 104 |
else: |
| 105 |
non_debug_sections.append(section_name) |
| 106 |
|
| 107 |
out_file.write( |
| 108 |
"""\ |
| 109 |
/* Generated by libdrgn/build-aux/gen_elf_sections.py. */ |
| 110 |
|
| 111 |
static enum drgn_section_index drgn_debug_section_name_to_index(const char *name, size_t len) |
| 112 |
{ |
| 113 |
@memswitch (name, len)@ |
| 114 |
""" |
| 115 |
) |
| 116 |
for section_name in debug_sections: |
| 117 |
out_file.write(f"\t@case {c_string_literal(section_name[len('.debug_'):])}@\n") |
| 118 |
out_file.write(f"\t\treturn {section_enumerator_name(section_name)};\n") |
| 119 |
out_file.write( |
| 120 |
"""\ |
| 121 |
@default@ |
| 122 |
return DRGN_SECTION_INDEX_NUM; |
| 123 |
@endswitch@ |
| 124 |
} |
| 125 |
|
| 126 |
static enum drgn_section_index drgn_non_debug_section_name_to_index(const char *name) |
| 127 |
{ |
| 128 |
@strswitch (name)@ |
| 129 |
""" |
| 130 |
) |
| 131 |
for section_name in non_debug_sections: |
| 132 |
out_file.write(f"\t@case {c_string_literal(section_name)}@\n") |
| 133 |
out_file.write(f"\t\treturn {section_enumerator_name(section_name)};\n") |
| 134 |
out_file.write( |
| 135 |
"""\ |
| 136 |
@default@ |
| 137 |
return DRGN_SECTION_INDEX_NUM; |
| 138 |
@endswitch@ |
| 139 |
} |
| 140 |
""" |
| 141 |
) |
| 142 |
|
| 143 |
|
| 144 |
def main() -> None: |
| 145 |
parser = argparse.ArgumentParser() |
| 146 |
parser.add_argument( |
| 147 |
"--header", "-H", action="store_true", help="generate header file" |
| 148 |
) |
| 149 |
args = parser.parse_args() |
| 150 |
|
| 151 |
if args.header: |
| 152 |
gen_elf_sections_h(sys.stdout) |
| 153 |
else: |
| 154 |
gen_drgn_section_name_to_index_inc_strswitch(sys.stdout) |
| 155 |
|
| 156 |
|
| 157 |
if __name__ == "__main__": |
| 158 |
main() |