Skip to content

Commit eaed5a5

Browse files
committed
initial support for sourcemap
1 parent c2ed2ff commit eaed5a5

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

chb/bctypes/BCFiles.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
# ------------------------------------------------------------------------------
2727
"""Contains all global types in a CIL file."""
2828

29-
from typing import Any, cast, Dict, List, Optional, TYPE_CHECKING
29+
from typing import Any, cast, Dict, List, Optional, Tuple, TYPE_CHECKING
3030

3131
import xml.etree.ElementTree as ET
3232

@@ -55,6 +55,7 @@ def __init__(self, app: "AppAccess", xnode: ET.Element) -> None:
5555
self._gvardecls: List[BCVarInfo] = []
5656
self._gvardefs: List[BCVarInfo] = []
5757
self._functions: Dict[str, BCFunctionDefinition] = {}
58+
self._srcmap: List[Tuple[str, int, str, str]] = []
5859
self.initialize(xnode)
5960

6061
@property
@@ -102,6 +103,10 @@ def gvardefs(self) -> List[BCVarInfo]:
102103
def globalvars(self) -> List[BCVarInfo]:
103104
return self._gvardefs + self._gvardecls
104105

106+
@property
107+
def srcmap(self) -> List[Tuple[str, int, str, str]]:
108+
return self._srcmap
109+
105110
@property
106111
def functions(self) -> Dict[str, BCFunctionDefinition]:
107112
return self._functions
@@ -149,6 +154,7 @@ def initialize(self, xnode: ET.Element) -> None:
149154
self.initialize_vardefs(xnode.find("varinfos"))
150155
self.initialize_vardecls(xnode.find("varinfodecls"))
151156
self.initialize_functions(xnode.find("ifuns"))
157+
self.initialize_srcmap(xnode.find("srcmap"))
152158

153159
def initialize_compinfos(self, tnode: Optional[ET.Element]) -> None:
154160
if tnode:
@@ -209,6 +215,18 @@ def initialize_functions(self, tnode: Optional[ET.Element]) -> None:
209215
self._functions[svinfo.vname] = BCFunctionDefinition(
210216
self, svinfo.vname, xfundef)
211217

218+
def initialize_srcmap(self, tnode: Optional[ET.Element]) -> None:
219+
if tnode is not None:
220+
for x in tnode.findall("srcloc"):
221+
vix = x.get("vix")
222+
lnr = x.get("lnr")
223+
ixfn = x.get("ixfn")
224+
binloc = x.get("binloc")
225+
if vix and lnr and ixfn and binloc:
226+
srcvinfo = self.bcd.varinfo(int(vix))
227+
srcfile = self.bcd.string(int(ixfn))
228+
self._srcmap.append((srcfile, int(lnr), srcvinfo.vname, binloc))
229+
212230
def __str__(self) -> str:
213231
lines: List[str] = []
214232
lines.append("\n".join(str(t) for t in self.gtypes))

chb/cmdline/chkx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,11 @@ def parse() -> argparse.Namespace:
584584
ddatamd5s.add_argument("xname", help="name of executable")
585585
ddatamd5s.set_defaults(func=UCC.ddata_md5s)
586586

587+
# -- ddata srcmap --
588+
ddatasrcmap = ddataparsers.add_parser("srcmap")
589+
ddatasrcmap.add_argument("xname", help="name of executable")
590+
ddatasrcmap.set_defaults(func=UCC.ddata_srcmap)
591+
587592
# ----------------------------------------------------------------- results --
588593
resultscmd = subparsers.add_parser('results')
589594
resultscmd.set_defaults(func=resultscommand)

chb/cmdline/commandutil.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2561,3 +2561,34 @@ def ddata_md5s(args: argparse.Namespace) -> NoReturn:
25612561
print(md5 + ": " + str(count))
25622562

25632563
exit(0)
2564+
2565+
2566+
def ddata_srcmap(args: argparse.Namespace) -> NoReturn:
2567+
2568+
# arguments
2569+
xname: str = str(args.xname)
2570+
2571+
try:
2572+
(path, xfile) = get_path_filename(xname)
2573+
except UF.CHBError as e:
2574+
print(str(e.wrap()))
2575+
exit(1)
2576+
2577+
xinfo = XI.XInfo()
2578+
xinfo.load(path, xfile)
2579+
2580+
app = get_app(path, xfile, xinfo)
2581+
srcmap = app.bcfiles.srcmap
2582+
2583+
result: Dict[str, Dict[int, Tuple[str, str]]] = {}
2584+
2585+
for (fname, linenr, vname, binloc) in srcmap:
2586+
result.setdefault(fname, {})
2587+
result[fname][linenr] = (vname, binloc)
2588+
2589+
for fname in result:
2590+
print(fname)
2591+
for (linenr, (vname, binloc)) in sorted(result[fname].items()):
2592+
print(str(linenr).rjust(6) + ": " + binloc.rjust(10) + " " + vname)
2593+
2594+
exit(0)

chb/cmdline/reportcmds.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,11 +1149,11 @@ def consider(faddr: str):
11491149
# heuristics, which (for now) means shared object stubs.
11501150
if calltgt.is_so_target:
11511151
opcode = instr.opcode # type: ignore
1152-
if "MIPS" in repr(opcode): # ugly but concise!
1152+
if app.is_mips:
11531153
# No support (or need?) for MIPS just yet
11541154
continue
11551155
optgt = opcode.opargs[0]
1156-
tgtname = cast(StubTarget, calltgt).stub.name
1156+
tgtname = cast("StubTarget", calltgt).stub.name
11571157
if optgt.is_absolute:
11581158
tgtaddr = optgt.opkind.address.get_hex()
11591159
consider_pair(tgtaddr, tgtname)

0 commit comments

Comments
 (0)