39
39
from django .template .defaultfilters import pluralize
40
40
41
41
from commoncode .paths import common_prefix
42
+ from elf_inspector .binary import collect_and_parse_elf_symbols
42
43
from elf_inspector .dwarf import get_dwarf_paths
43
44
from extractcode import EXTRACT_SUFFIX
44
45
from go_inspector .plugin import collect_and_parse_symbols
@@ -1708,11 +1709,11 @@ def map_paths_resource(
1708
1709
relations_to_create [rel_key ] = relation
1709
1710
if paths_not_mapped :
1710
1711
to_resource .status = flag .REQUIRES_REVIEW
1711
- to_resource .save ()
1712
1712
logger (
1713
1713
f"WARNING: #{ len (paths_not_mapped )} { map_type } paths NOT mapped for: "
1714
1714
f"{ to_resource .path !r} "
1715
1715
)
1716
+ to_resource .save ()
1716
1717
1717
1718
if relations_to_create :
1718
1719
rels = CodebaseRelation .objects .bulk_create (relations_to_create .values ())
@@ -1795,7 +1796,7 @@ def is_invalid_match(match, matched_path_length):
1795
1796
return matched_path_length == 1 and len (match .resource_ids ) != 1
1796
1797
1797
1798
1798
- def map_elfs (project , logger = None ):
1799
+ def map_elfs_with_dwarf_paths (project , logger = None ):
1799
1800
"""Map ELF binaries to their sources in ``project``."""
1800
1801
from_resources = project .codebaseresources .files ().from_codebase ()
1801
1802
to_resources = (
@@ -1911,10 +1912,10 @@ def map_go_paths(project, logger=None):
1911
1912
)
1912
1913
1913
1914
1914
- def map_rust_paths (project , logger = None ):
1915
- """Map Rust binaries to their source in ``project``."""
1915
+ def map_rust_binaries_with_symbols (project , logger = None ):
1916
+ """Map Rust binaries to their source using symbols in ``project``."""
1916
1917
from_resources = project .codebaseresources .files ().from_codebase ()
1917
- to_resources = (
1918
+ to_binaries = (
1918
1919
project .codebaseresources .files ()
1919
1920
.to_codebase ()
1920
1921
.has_no_relation ()
@@ -1923,41 +1924,82 @@ def map_rust_paths(project, logger=None):
1923
1924
1924
1925
# Collect source symbols from rust source files
1925
1926
rust_from_resources = from_resources .filter (extension = ".rs" )
1927
+
1928
+ map_binaries_with_symbols (
1929
+ project = project ,
1930
+ from_resources = rust_from_resources ,
1931
+ to_resources = to_binaries ,
1932
+ binary_symbols_func = collect_and_parse_rust_symbols ,
1933
+ map_type = "rust_symbols" ,
1934
+ logger = logger ,
1935
+ )
1936
+
1937
+
1938
+ def map_elfs_binaries_with_symbols (project , logger = None ):
1939
+ """Map Elf binaries to their source using symbols in ``project``."""
1940
+ from_resources = project .codebaseresources .files ().from_codebase ()
1941
+ elf_binaries = (
1942
+ project .codebaseresources .files ().to_codebase ().has_no_relation ().elfs ()
1943
+ )
1944
+
1945
+ # Collect source symbols from rust source files
1946
+ elf_from_resources = from_resources .filter (extension__in = [".c" , ".cpp" , ".h" ])
1947
+
1948
+ map_binaries_with_symbols (
1949
+ project = project ,
1950
+ from_resources = elf_from_resources ,
1951
+ to_resources = elf_binaries ,
1952
+ binary_symbols_func = collect_and_parse_elf_symbols ,
1953
+ map_type = "elf_symbols" ,
1954
+ logger = logger ,
1955
+ )
1956
+
1957
+
1958
+ def map_binaries_with_symbols (
1959
+ project ,
1960
+ from_resources ,
1961
+ to_resources ,
1962
+ binary_symbols_func ,
1963
+ map_type ,
1964
+ logger = None ,
1965
+ ):
1966
+ """Map Binaries to their source using symbols in ``project``."""
1926
1967
symbols .collect_and_store_tree_sitter_symbols_and_strings (
1927
1968
project = project ,
1928
1969
logger = logger ,
1929
- project_files = rust_from_resources ,
1970
+ project_files = from_resources ,
1930
1971
)
1931
1972
1932
1973
# Collect binary symbols from rust binaries
1933
1974
for resource in to_resources :
1934
1975
try :
1935
- binary_symbols = collect_and_parse_rust_symbols (resource .location_path )
1976
+ binary_symbols = binary_symbols_func (resource .location )
1936
1977
resource .update_extra_data (binary_symbols )
1937
1978
except Exception as e :
1938
- logger (f"Can not parse { resource .location_path !r} { e !r} " )
1979
+ logger (f"Error parsing binary symbols at: { resource .location_path !r} { e !r} " )
1939
1980
1940
1981
if logger :
1941
1982
logger (
1942
1983
f"Mapping { to_resources .count ():,d} to/ resources using symbols "
1943
- f"with { rust_from_resources .count ():,d} from/ resources."
1984
+ f"with { from_resources .count ():,d} from/ resources."
1944
1985
)
1945
1986
1946
1987
resource_iterator = to_resources .iterator (chunk_size = 2000 )
1947
1988
progress = LoopProgress (to_resources .count (), logger )
1948
1989
for to_resource in progress .iter (resource_iterator ):
1949
- binary_symbols = to_resource .extra_data .get ("rust_symbols" )
1990
+ binary_symbols = to_resource .extra_data .get (map_type )
1950
1991
if not binary_symbols :
1951
1992
continue
1952
1993
1953
1994
if logger :
1954
1995
logger (f"Mapping source files to binary at { to_resource .path } " )
1955
1996
1956
1997
symbolmap .map_resources_with_symbols (
1998
+ project = project ,
1957
1999
to_resource = to_resource ,
1958
- from_resources = rust_from_resources ,
2000
+ from_resources = from_resources ,
1959
2001
binary_symbols = binary_symbols ,
1960
- map_type = "rust_symbols" ,
2002
+ map_type = map_type ,
1961
2003
logger = logger ,
1962
2004
)
1963
2005
0 commit comments