Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 52 additions & 24 deletions cle/backends/elf/elf.py
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,32 @@ def _load_die_namespace(
new_namespace = namespace
self._load_die_namespace(die_child, dwarf, aranges, cu_, expr_parser, type_list, cu, new_namespace)

@staticmethod
def _dwarf_idx_to_filename(cu: CompileUnit, filename_idx: int, default_filename: str | None = None) -> str | None:
debug_line = cu.dwarfinfo.line_program_for_CU(cu)
assert debug_line is not None
if debug_line.header.file_names is not None:
basename = debug_line.header.file_names[filename_idx]
basename_str = basename.DW_LNCT_path.decode(errors="replace")
dirname_idx = basename.DW_LNCT_directory_index
dirname = debug_line.header.directories[dirname_idx]
dirname_str = dirname.DW_LNCT_path.decode(errors="replace")
filename = f"{dirname_str}/{basename_str}"
elif debug_line.header.file_entry is not None:
basename = debug_line.header.file_entry[filename_idx - 1]
basename_str = basename.name.decode(errors="replace")
dirname_idx = basename.dir_index
if dirname_idx == 0:
dirname_str = "."
else:
dirname_str = debug_line.header.include_directory[dirname_idx - 1].decode(errors="replace")
filename = f"{dirname_str}/{basename_str}"
else:
assert filename_idx == 1
filename = default_filename

return filename

def _load_die_lex_block(
self,
die: DIE,
Expand Down Expand Up @@ -943,27 +969,7 @@ def _load_die_lex_block(
filename_idx = None

if filename_idx is not None:
debug_line = dwarf.line_program_for_CU(cu)
assert debug_line is not None
if debug_line.header.file_names is not None:
basename = debug_line.header.file_names[filename_idx]
basename_str = basename.DW_LNCT_path.decode(errors="replace")
dirname_idx = basename.DW_LNCT_directory_index
dirname = debug_line.header.directories[dirname_idx]
dirname_str = dirname.DW_LNCT_path.decode(errors="replace")
filename = f"{dirname_str}/{basename_str}"
elif debug_line.header.file_entry is not None:
basename = debug_line.header.file_entry[filename_idx - 1]
basename_str = basename.name.decode(errors="replace")
dirname_idx = basename.dir_index
if dirname_idx == 0:
dirname_str = "."
else:
dirname_str = debug_line.header.include_directory[dirname_idx - 1].decode(errors="replace")
filename = f"{dirname_str}/{basename_str}"
else:
assert filename_idx == 1
filename = file_path
filename = self._dwarf_idx_to_filename(cu, filename_idx, file_path)
else:
filename = None

Expand All @@ -974,6 +980,13 @@ def _load_die_lex_block(
else:
line = None

if "DW_AT_decl_column" in die.attributes:
column = die.attributes["DW_AT_decl_column"].value
elif origin is not None and "DW_AT_decl_column" in origin.attributes:
column = origin.attributes["DW_AT_decl_column"].value
else:
column = None

low_pc, high_pc = self._load_low_high_pc_form_die(die)
ranges = None
if low_pc is None or high_pc is None:
Expand All @@ -982,9 +995,9 @@ def _load_die_lex_block(
return None

if subprogram is None:
subprogram = block = Subprogram(name, low_pc, high_pc, ranges, filename, line)
subprogram = block = Subprogram(name, low_pc, high_pc, ranges, filename, line, column)
else:
block = LexicalBlock(low_pc, high_pc, ranges, filename, line)
block = LexicalBlock(low_pc, high_pc, ranges, filename, line, column)

for sub_die in cu.iter_DIE_children(die):
if sub_die.tag in ["DW_TAG_variable", "DW_TAG_formal_parameter"]:
Expand All @@ -1010,6 +1023,19 @@ def _load_die_lex_block(
ranges = self._load_ranges_form_die(sub_die, aranges)
if ranges is not None:
subr.ranges = ranges
call_filename = None
call_line = None
call_col = 0
if "DW_AT_call_file" in sub_die.attributes:
call_filename = self._dwarf_idx_to_filename(
cu, sub_die.attributes["DW_AT_call_file"].value, file_path
)
if "DW_AT_call_line" in sub_die.attributes:
call_line = sub_die.attributes["DW_AT_call_line"].value
if "DW_AT_call_column" in sub_die.attributes:
call_col = sub_die.attributes["DW_AT_call_column"].value
if call_filename is not None and call_line is not None:
subr.call_site = (call_filename, call_line, call_col)
subr.void = "DW_AT_type" not in sub_die.attributes
if "DW_AT_abstract_origin" in sub_die.attributes:
origin = sub_die.get_DIE_from_attribute("DW_AT_abstract_origin")
Expand All @@ -1022,14 +1048,16 @@ def _load_die_lex_block(
if arg_die.tag == "DW_TAG_formal_parameter":
nargs += 1
subr.nargs = nargs
subr.void = "DW_AT_type" not in origin.attributes
subr.void = "DW_AT_type" not in origin.attributes

subprogram.inlined_functions.append(subr)

sub_block = self._load_die_lex_block(
sub_die, dwarf, aranges, expr_parser, type_list, cu, file_path, subprogram, namespace
)
if sub_block is not None:
if sub_block.source_file is not None and sub_block.source_line is not None:
subr.decl_site = (sub_block.source_file, sub_block.source_line, sub_block.source_column or 0)
block.child_blocks.append(sub_block)

return block
Expand Down
5 changes: 4 additions & 1 deletion cle/backends/elf/subprogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ def __init__(
ranges: list[tuple[int, int]] | None = None,
source_file: str | None = None,
source_line: int | None = None,
source_column: int | None = None,
) -> None:
self.source_file = source_file
self.source_line = source_line
self.source_column = source_column

if low_pc is None and high_pc is None:
if ranges is not None:
Expand Down Expand Up @@ -77,10 +79,11 @@ def __init__(
ranges: list[tuple[int, int]] | None = None,
source_file: str | None = None,
source_line: int | None = None,
source_column: int | None = None,
) -> None:
# pass self as the super_block of this subprogram
self.subprogram = self
super().__init__(low_pc, high_pc, ranges, source_file, source_line)
super().__init__(low_pc, high_pc, ranges, source_file, source_line, source_column)
self.name = name
self.local_variables: list[Variable] = []
self.inlined_functions: list[InlinedFunction] = []
Expand Down
2 changes: 2 additions & 0 deletions cle/backends/inlined_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class InlinedFunction:
entry: int | None = None
nargs: int | None = None
void: bool = False
decl_site: tuple[str, int, int] | None = None
call_site: tuple[str, int, int] | None = None

@property
def low_pc(self):
Expand Down
Loading