#!/usr/bin/env python3 """Compare captured runtime code to modulefile at existing PT decode errors. Reads only cached code, never /proc/kcore; does not decode/capture or mutateperf. """ from pathlib import Path from elftools.elf.elffile import ELFFile import json,re,argparse p=argparse.ArgumentParser();p.add_argument('--cache',type=Path,required=True);p.add_argument('--errors',type=Path,required=True);p.add_argument('--output',type=Path,required=True);a=p.parse_args() M=Path('/lib/modules/5.15.0-202-generic/updates/dkms/nvidia.ko');C=a.cache moduleline=next(x for x in(C/'modules').read_text().splitlines()if x.split()[0]=='nvidia');textbase=int(moduleline.split()[5],16) with M.open('rb')as f: e=ELFFile(f);text=e.get_section_by_name('.text').data();textindex=e.get_section_index('.text');symtab=e.get_section_by_name('.symtab');relocs=[{'offset':r['r_offset'],'type':r['r_info_type'],'symbol':symtab.get_symbol(r['r_info_sym']).name}for r in e.get_section_by_name('.rela.text').iter_relocations()];syms=sorted((s['st_value'],s.name)for s in symtab.iter_symbols()if s['st_shndx']==textindex and s['st_info']['type']=='STT_FUNC') with(C/'kcore').open('rb')as f: e=ELFFile(f);segments=[p for p in e.iter_segments()if p['p_type']=='PT_LOAD'];rows=[] for line in a.errors.read_text().splitlines()[:12]: match=re.search(r' ip (0x[0-9a-f]+)',line) if not match:continue ip=int(match[1],16);ph=next((p for p in segments if p['p_vaddr']<=ip and ip+16<=p['p_vaddr']+p['p_filesz']),None) if ph is None:rows.append({'ip':hex(ip),'cached_code_present':False});continue f.seek(ph['p_offset']+ip-ph['p_vaddr']);runtime=f.read(16);offset=ip-textbase;disk=text[offset:offset+16];near=max((s for s in syms if s[0]<=offset),default=(0,''));near_reloc=[{'relative_to_ip':r['offset']-offset,'type':r['type'],'symbol':r['symbol']}for r in relocs if offset-8<=r['offset']