367 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
48 lines
2.2 KiB
Python
48 lines
2.2 KiB
Python
"""Prepare the fixed main-image layout required by the current native bridge.
|
|
|
|
This is an explicit packaging operation, never a runtime patch. Code, imports,
|
|
entry point, resources and relocation records remain unchanged.
|
|
"""
|
|
import hashlib
|
|
from pathlib import Path
|
|
import struct
|
|
|
|
|
|
def prepare(source, output, expected_source_sha):
|
|
source, output = Path(source), Path(output)
|
|
if source.is_symlink() or not source.is_file() or output.exists():
|
|
raise ValueError('Regular source and a new output path required')
|
|
data = source.read_bytes()
|
|
if hashlib.sha256(data).hexdigest() != expected_source_sha:
|
|
raise ValueError('Main-image preimage differs')
|
|
if len(data) < 64 or data[:2] != b'MZ':
|
|
raise ValueError('DOS header differs')
|
|
nt = struct.unpack_from('<I', data, 0x3c)[0]
|
|
optional = nt + 24
|
|
if (optional + 72 > len(data) or data[nt:nt + 4] != b'PE\0\0'
|
|
or struct.unpack_from('<H', data, nt + 4)[0] != 0x8664
|
|
or struct.unpack_from('<H', data, optional)[0] != 0x20b
|
|
or struct.unpack_from('<Q', data, optional + 24)[0] != 0x140000000):
|
|
raise ValueError('Expected AMD64 main-image ABI differs')
|
|
offset = optional + 70
|
|
before = struct.unpack_from('<H', data, offset)[0]
|
|
if before & 0x60 != 0x60:
|
|
raise ValueError('Expected relocatable-image preimage differs')
|
|
after = before & ~0x60
|
|
result = bytearray(data)
|
|
struct.pack_into('<H', result, offset, after)
|
|
if result[:offset] != data[:offset] or result[offset + 2:] != data[offset + 2:]:
|
|
raise ValueError('Unexpected changes outside image-layout flags')
|
|
output.parent.mkdir(parents=True, exist_ok=True)
|
|
output.write_bytes(result)
|
|
output.chmod(source.stat().st_mode & 0o777)
|
|
return {
|
|
'kind': 'fixed-main-image-abi-v1', 'preferred_base': '0x140000000',
|
|
'source_sha256': expected_source_sha,
|
|
'result_sha256': hashlib.sha256(result).hexdigest(),
|
|
'flag_offset': offset, 'flags_before': hex(before), 'flags_after': hex(after),
|
|
'changed_offsets': [i for i, (a, b) in enumerate(zip(data, result)) if a != b],
|
|
'file_size_preserved': True, 'code_imports_resources_entry_point_unchanged': True,
|
|
'runtime_or_authentication_tested': False,
|
|
}
|