Files
nvdriver/tools/extract_mmu_callback_table.py

17 lines
1.4 KiB
Python
Executable File

#!/usr/bin/env python3
"""Record original ELF relocations establishing gmmu-walk callback identity."""
import hashlib,json
from pathlib import Path
from elftools.elf.elffile import ELFFile
R=Path(__file__).resolve().parents[1]
P=Path('/home/geodd/nvidia-driver/analysis-580.178.04/extracted/kernel/nvidia/nv-kernel.o_binary')
with P.open('rb') as f:
e=ELFFile(f);syms=e.get_section_by_name('.symtab');names={s['st_value']:s.name for s in syms.iter_symbols() if s.name.startswith('_nv') and s['st_info']['type']=='STT_FUNC'};sec=e.get_section_by_name('.rela.rodata');table=[]
for x in sec.iter_relocations():
offset=x['r_offset']
if 0x4c5da80<=offset<0x4c5dab8:
sym=syms.get_symbol(x['r_info_sym']);assert e.get_section(sym['st_shndx']).name=='.text'
table.append({'offset':hex(offset),'text_addend':hex(x['r_addend']),'symbol':names[x['r_addend']]})
assert [x['symbol'] for x in table]==['_nv013506rm','_nv013507rm','_nv013511rm','_nv013512rm','_nv013503rm','_nv013500rm']
(R/'reports/mmu-callback-relocations.json').write_text(json.dumps({'artifact':str(P),'sha256':hashlib.sha256(P.read_bytes()).hexdigest(),'rodata_base':'0x4c5da80','callback_order':['LevelAlloc','LevelFree','UpdatePdb','UpdatePde','FillEntries','CopyEntries'],'relocations':table,'interpretation':'Callback order corroborated by exact-version g_gmmuWalkCallbacks definition and complete REA callback behavior'},indent=2)+'\n');print('Six actual ELF callback relocations verified')