Skip to content

Commit 23eed7a

Browse files
Update dwarf_info (#13401)
1 parent 2c3a9ea commit 23eed7a

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

infra/base-images/base-builder/indexer/dwarf_info.py

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
from absl import logging
2323
from elftools.elf import elffile
2424

25+
_IGNORED_UNIT_TYPES = ("DW_UT_type", "DW_UT_split_type")
26+
27+
# yapf: disable
28+
2529

2630
@dataclasses.dataclass
2731
class CompilationUnit:
@@ -45,7 +49,8 @@ class CompilationUnit:
4549

4650

4751
def get_all_compilation_units(
48-
elf_file_path: os.PathLike[str],) -> list[CompilationUnit]:
52+
elf_file_path: os.PathLike[str],
53+
) -> list[CompilationUnit]:
4954
"""Parses compilation units from an ELF file.
5055
5156
Args:
@@ -62,20 +67,33 @@ def get_all_compilation_units(
6267
return []
6368
dwarf_info = elf_file.get_dwarf_info()
6469
for compilation_unit in dwarf_info.iter_CUs():
65-
# Only compile and partial unit headers will be followed by a
66-
# DW_TAG_compile_unit or DW_TAG_partial entry with the expected
67-
# attributes.
68-
if compilation_unit.header.unit_type not in (
70+
if compilation_unit.header.version < 5:
71+
# Only DWARF5 has a unit_type field in the header.
72+
# For older versions, we do a best effort approach.
73+
logging.warning(
74+
"[!] Compilation Unit with unsupported DWARF version %d",
75+
compilation_unit.header.version,
76+
)
77+
elif compilation_unit.header.unit_type in _IGNORED_UNIT_TYPES:
78+
# Type units are not interesting for us.
79+
continue
80+
elif compilation_unit.header.unit_type not in (
6981
"DW_UT_compile",
7082
"DW_UT_partial",
7183
):
72-
continue
84+
raise ValueError(
85+
"Unsupported DWARF compilation unit type"
86+
f" {compilation_unit.header.unit_type}"
87+
)
7388

7489
top_debug_info_entry = compilation_unit.get_top_DIE()
7590
if top_debug_info_entry.tag != "DW_TAG_compile_unit":
7691
logging.error("Top DIE is not a full compile unit")
77-
producer = top_debug_info_entry.attributes["DW_AT_producer"].value.decode(
78-
)
92+
93+
producer = top_debug_info_entry.attributes[
94+
"DW_AT_producer"
95+
].value.decode()
96+
7997
name = top_debug_info_entry.attributes["DW_AT_name"].value.decode()
8098
language = top_debug_info_entry.attributes["DW_AT_language"].value
8199
compdir = top_debug_info_entry.attributes["DW_AT_comp_dir"].value.decode()
@@ -85,12 +103,14 @@ def get_all_compilation_units(
85103
apple_flags = None
86104
if top_debug_info_entry.attributes.get("DW_AT_APPLE_flags", None):
87105
apple_flags = top_debug_info_entry.attributes[
88-
"DW_AT_APPLE_flags"].value.decode()
106+
"DW_AT_APPLE_flags"
107+
].value.decode()
89108

90109
isysroot = None
91110
if top_debug_info_entry.attributes.get("DW_AT_LLVM_isysroot", None):
92111
isysroot = top_debug_info_entry.attributes[
93-
"DW_AT_LLVM_isysroot"].value.decode()
112+
"DW_AT_LLVM_isysroot"
113+
].value.decode()
94114

95115
result.append(
96116
CompilationUnit(
@@ -100,7 +120,8 @@ def get_all_compilation_units(
100120
language=language,
101121
apple_flags=apple_flags,
102122
isysroot=isysroot,
103-
))
123+
)
124+
)
104125
return result
105126

106127

0 commit comments

Comments
 (0)