Skip to content

Commit d6d7353

Browse files
authored
examples: Support DWARF5 filename decoding (#582)
DWARF5 uses 0-indexed file_entry and directory_entry. We should not skip index 0 when decoding the filename and the directory for DWARF5.
1 parent 907a9c3 commit d6d7353

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

examples/dwarf_lineprogram_filenames.py

+17-7
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,14 @@ def line_entry_mapping(line_program):
5454
# a reverse mapping of filename -> #entries.
5555
lp_entries = line_program.get_entries()
5656
for lpe in lp_entries:
57+
if not lpe.state:
58+
continue
59+
filename = lpe_filename(line_program, lpe.state.file)
5760
# We skip LPEs that don't have an associated file.
5861
# This can happen if instructions in the compiled binary
5962
# don't correspond directly to any original source file.
60-
if not lpe.state or lpe.state.file == 0:
63+
if filename is None:
6164
continue
62-
filename = lpe_filename(line_program, lpe.state.file)
6365
filename_map[filename] += 1
6466

6567
for filename, lpe_count in filename_map.items():
@@ -77,16 +79,24 @@ def lpe_filename(line_program, file_index):
7779
lp_header = line_program.header
7880
file_entries = lp_header["file_entry"]
7981

80-
# File and directory indices are 1-indexed.
81-
file_entry = file_entries[file_index - 1]
82+
# File and directory indices are 1-indexed in DWARF version < 5,
83+
# 0-indexed in DWARF5.
84+
if lp_header.version < 5:
85+
file_index -= 1
86+
if file_index == -1:
87+
return None
88+
89+
file_entry = file_entries[file_index]
8290
dir_index = file_entry["dir_index"]
8391

8492
# A dir_index of 0 indicates that no absolute directory was recorded during
85-
# compilation; return just the basename.
86-
if dir_index == 0:
93+
# compilation in DWARF version < 5; return just the basename.
94+
if dir_index == 0 and lp_header.version < 5:
8795
return file_entry.name.decode()
8896

89-
directory = lp_header["include_directory"][dir_index - 1]
97+
if lp_header.version < 5:
98+
dir_index -= 1
99+
directory = lp_header["include_directory"][dir_index]
90100
return posixpath.join(directory, file_entry.name).decode()
91101

92102

0 commit comments

Comments
 (0)