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
7 changes: 4 additions & 3 deletions cle/backends/elf/elf.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,20 +742,21 @@ def _load_line_info(self, dwarf):
continue
if lineprog is None:
continue
index_offset = 1 if lineprog.header["version"] < 5 else 0
file_cache = {}
for line in lineprog.get_entries():
if line.state is None:
continue
if line.state.file in file_cache:
filename = file_cache[line.state.file]
else:
file_entry = lineprog.header["file_entry"][line.state.file - 1]
if file_entry["dir_index"] == 0:
file_entry = lineprog.header["file_entry"][line.state.file - index_offset]
if file_entry["dir_index"] == 0 and index_offset == 1:
filename = pathlib.PurePosixPath(comp_dir) / file_entry.name.decode()
else:
filename = (
pathlib.PurePosixPath(comp_dir)
/ lineprog.header["include_directory"][file_entry["dir_index"] - 1].decode()
/ lineprog.header["include_directory"][file_entry["dir_index"] - index_offset].decode()
/ file_entry.name.decode()
)
file_cache[line.state.file] = filename
Expand Down
32 changes: 32 additions & 0 deletions tests/test_dwarf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import annotations

import os
from pathlib import PurePosixPath
from unittest import TestCase, main

import cle
Expand All @@ -10,6 +11,37 @@


class TestDwarf(TestCase):
def test_line_program_file_indices(self):
load_options = (
(0x400000, {}),
(0x700000, {"base_addr": 0x700000, "force_rebase": True}),
)
for variant in ("v4", "v5_relative", "v5_absolute"):
binary_path = os.path.join(TESTS_BASE, "x86_64", f"dwarf_line_filenames_{variant}")
for base_addr, main_opts in load_options:
with self.subTest(variant=variant, base_addr=base_addr):
ld = cle.Loader(
binary_path,
auto_load_libs=False,
load_debug_info=True,
main_opts=main_opts,
)
elf = ld.main_object
assert isinstance(elf, cle.backends.ELF)
assert elf.mapped_base == base_addr
assert elf.compilation_units is not None and len(elf.compilation_units) == 1
comp_dir = PurePosixPath(elf.compilation_units[0].comp_dir)
assert comp_dir.is_absolute()

main_symbol = elf.get_symbol("main")
increment_symbol = elf.get_symbol("increment")
assert main_symbol is not None
assert increment_symbol is not None
main_path = comp_dir / "main.c"
header_path = comp_dir / "include" / "header.h"
assert elf.addr_to_line[main_symbol.rebased_addr] == {(str(main_path), 4)}
assert elf.addr_to_line[increment_symbol.rebased_addr] == {(str(header_path), 2)}

def test_rust_binary(self):
binary_path = os.path.join(
TESTS_BASE, "x86_64", "1cbbf108f44c8f4babde546d26425ca5340dccf878d306b90eb0fbec2f83ab51"
Expand Down
Loading