Files
kam-mobile/tests/helpers.py
T

48 lines
2.2 KiB
Python
Raw Normal View History

2026-09-11 18:18:40 +05:00
"""Generated bytes below are TEST DOUBLES, never real compiler/APK evidence."""
import struct
def elf(cpu='aarch64',needed=(),textrel=False):
cls=1 if cpu=='arm' else 2
machine={'arm':40,'aarch64':183,'x86_64':62}[cpu]
strings=b'\0';offsets=[]
for name in needed:offsets.append(len(strings));strings+=name.encode()+b'\0'
data=bytearray(1024);data[:7]=b'\x7fELF'+bytes([cls,1,1])
struct.pack_into('<HHI',data,16,3,machine,1)
if cls==2:
struct.pack_into('<Q',data,32,64);struct.pack_into('<HHH',data,52,64,56,2)
struct.pack_into('<IIQQQQQQ',data,64,1,5,0,0,0,1024,1024,16384)
struct.pack_into('<IIQQQQQQ',data,120,2,6,256,256,256,192,192,8)
fmt='<qQ';step=16
else:
struct.pack_into('<I',data,28,52);struct.pack_into('<HHH',data,40,52,32,2)
struct.pack_into('<IIIIIIII',data,52,1,0,0,0,1024,1024,5,16384)
struct.pack_into('<IIIIIIII',data,84,2,256,256,256,192,192,6,4)
fmt='<iI';step=8
tags=[(1,n) for n in offsets]+[(5,512),(10,len(strings))]
if textrel:tags.append((22,0))
tags.append((0,0))
for i,pair in enumerate(tags):struct.pack_into(fmt,data,256+i*step,*pair)
data[512:512+len(strings)]=strings
return bytes(data)
def section_elf(cpu='aarch64',debug=b'FIRST DEBUG DATA',code=b'CODE BYTES'):
"""TEST DOUBLE with a section table, not an executable Android program."""
data=bytearray(elf(cpu)); cls=1 if cpu=='arm' else 2
names=b'\0.text\0.shstrtab\0.debug_info\0'
data[640:640+len(code)]=code
data.extend(names);nameoff=1024
debugoff=len(data);data.extend(debug)
while len(data)%8:data.append(0)
table=len(data);fmt='<IIIIIIIIII' if cls==1 else '<IIQQQQIIQQ'
size=struct.calcsize(fmt)
sections=[(0,0,0,0,0,0,0,0,0,0),
(1,1,6,640,640,len(code),0,0,4,0),
(7,3,0,0,nameoff,len(names),0,0,1,0),
(17,1,0,0,debugoff,len(debug),0,0,1,0)]
for row in sections:data.extend(struct.pack(fmt,*row))
if cls==1:
struct.pack_into('<I',data,32,table);struct.pack_into('<HHH',data,46,size,4,2)
else:
struct.pack_into('<Q',data,40,table);struct.pack_into('<HHH',data,58,size,4,2)
return bytes(data)