- tools/ : lecteur LE (DOS/4GW) et traducteur x86 -> C (Capstone) - runtime/ : CPU, FPU x87, DOS, VGA/PIT, mixeur et emulation AIL, remplacements de fonctions, hotes SDL2 et headless - build.bat : generation et compilation MSVC 2022 - README, feuille de route (docs/ROADMAP.md), .gitignore excluant tout fichier du jeu et tout code genere Etat du code au moment du commit : travail en cours sur le son des cinematiques (runtime/ail.c). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
156 lines
5.5 KiB
Python
156 lines
5.5 KiB
Python
"""Chargeur LE (DOS/4GW) pour le traducteur : objets en mémoire, relocations appliquées.
|
|
|
|
Lit le TUCKER.EXE d'origine (jamais modifié). Les objets gardent leur adresse de base LE
|
|
(code 0x10000, données 0x40000), comme dans le jeu sous DOS/4GW.
|
|
`fixups` liste les adresses où une adresse absolue 32 bits a été écrite, avec sa cible :
|
|
c'est la meilleure source pour trouver les pointeurs de code (tables de saut, rappels).
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import struct
|
|
|
|
SEL_CODE, SEL_DATA = 0x170, 0x178 # mêmes valeurs que runtime/rt.h
|
|
from dataclasses import dataclass, field
|
|
|
|
|
|
@dataclass
|
|
class LEObject:
|
|
index: int
|
|
base: int
|
|
vsize: int
|
|
flags: int
|
|
data: bytearray
|
|
|
|
@property
|
|
def is_code(self) -> bool:
|
|
return bool(self.flags & 4)
|
|
|
|
@property
|
|
def end(self) -> int:
|
|
return self.base + max(self.vsize, len(self.data))
|
|
|
|
|
|
@dataclass
|
|
class LEImage:
|
|
objects: list[LEObject]
|
|
entry: int
|
|
stack: int
|
|
fixups: dict[int, int] = field(default_factory=dict) # adresse du champ -> cible
|
|
|
|
def obj_at(self, addr: int) -> LEObject | None:
|
|
for o in self.objects:
|
|
if o.base <= addr < o.end:
|
|
return o
|
|
return None
|
|
|
|
def read(self, addr: int, n: int) -> bytes:
|
|
o = self.obj_at(addr)
|
|
if o is None:
|
|
raise KeyError(f"{addr:#x} hors image")
|
|
i = addr - o.base
|
|
return bytes(o.data[i:i + n]).ljust(n, b"\0")
|
|
|
|
def u32(self, addr: int) -> int:
|
|
return struct.unpack("<I", self.read(addr, 4))[0]
|
|
|
|
@property
|
|
def code(self) -> LEObject:
|
|
return next(o for o in self.objects if o.is_code)
|
|
|
|
|
|
def load_le(path: str) -> LEImage:
|
|
data = open(path, "rb").read()
|
|
u16 = lambda o: struct.unpack_from("<H", data, o)[0]
|
|
s16 = lambda o: struct.unpack_from("<h", data, o)[0]
|
|
u32 = lambda o: struct.unpack_from("<I", data, o)[0]
|
|
le = u32(0x3C)
|
|
if data[le:le + 2] != b"LE":
|
|
raise ValueError("pas un exécutable LE")
|
|
h = lambda off: u32(le + off)
|
|
npages, eip_obj, eip, esp_obj, esp = h(0x14), h(0x18), h(0x1C), h(0x20), h(0x24)
|
|
page_size, last_page = h(0x28), h(0x2C)
|
|
obj_tab, nobj, opt = h(0x40), h(0x44), h(0x48)
|
|
fpt, frt = h(0x68), h(0x6C)
|
|
data_pages = h(0x80)
|
|
|
|
raw = []
|
|
for i in range(nobj):
|
|
vsize, base, flags, pidx, pcnt = struct.unpack_from("<IIIII", data, le + obj_tab + i * 24)
|
|
raw.append((vsize, base, flags, pidx, pcnt))
|
|
|
|
def page_file_off(n: int) -> int:
|
|
e = data[le + opt + (n - 1) * 4: le + opt + n * 4]
|
|
num = (e[0] << 16) | (e[1] << 8) | e[2]
|
|
return data_pages + (num - 1) * page_size
|
|
|
|
objects = []
|
|
for i, (vsize, base, flags, pidx, pcnt) in enumerate(raw):
|
|
buf = bytearray(max(vsize, pcnt * page_size))
|
|
for k in range(pcnt):
|
|
n = pidx + k
|
|
ln = last_page if n == npages else page_size
|
|
off = page_file_off(n)
|
|
buf[k * page_size: k * page_size + ln] = data[off: off + ln]
|
|
objects.append(LEObject(i + 1, base, vsize, flags, buf))
|
|
|
|
fixups: dict[int, int] = {}
|
|
for ob, (vsize, base, flags, pidx, pcnt) in zip(objects, raw):
|
|
for k in range(pcnt):
|
|
n = pidx + k
|
|
p = le + frt + u32(le + fpt + (n - 1) * 4)
|
|
end = le + frt + u32(le + fpt + n * 4)
|
|
while p < end:
|
|
srct, flg = data[p], data[p + 1]
|
|
p += 2
|
|
stype = srct & 0x0F
|
|
if srct & 0x20:
|
|
cnt = data[p]
|
|
p += 1
|
|
offs = None
|
|
else:
|
|
offs = [s16(p)]
|
|
p += 2
|
|
if flg & 3:
|
|
raise ValueError(f"relocation externe non gérée ({flg & 3})")
|
|
if flg & 0x40:
|
|
tobj = u16(p)
|
|
p += 2
|
|
else:
|
|
tobj = data[p]
|
|
p += 1
|
|
toff = 0
|
|
if stype != 2:
|
|
if flg & 0x10:
|
|
toff = u32(p)
|
|
p += 4
|
|
else:
|
|
toff = u16(p)
|
|
p += 2
|
|
if offs is None:
|
|
offs = [s16(p + 2 * j) for j in range(cnt)]
|
|
p += 2 * cnt
|
|
target = objects[tobj - 1].base + toff
|
|
for so in offs:
|
|
a = k * page_size + so
|
|
if stype == 2: # sélecteur 16 bits : modèle à plat du runtime (rt.h)
|
|
sel = SEL_CODE if objects[tobj - 1].is_code else SEL_DATA
|
|
if 0 <= a <= len(ob.data) - 2:
|
|
struct.pack_into("<H", ob.data, a, sel)
|
|
continue
|
|
if stype != 7:
|
|
raise ValueError(f"relocation de type {stype} non gérée")
|
|
if 0 <= a <= len(ob.data) - 4:
|
|
struct.pack_into("<I", ob.data, a, target)
|
|
fixups[ob.base + a] = target
|
|
return LEImage(objects, objects[eip_obj - 1].base + eip, objects[esp_obj - 1].base + esp, fixups)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import sys
|
|
img = load_le(sys.argv[1])
|
|
for o in img.objects:
|
|
print(f"objet {o.index}: {o.base:#x}..{o.end:#x} {'code' if o.is_code else 'données'}")
|
|
print(f"entrée {img.entry:#x}, pile {img.stack:#x}, {len(img.fixups)} relocations")
|
|
tgt_code = sum(1 for t in img.fixups.values() if img.code.base <= t < img.code.end)
|
|
print(f"relocations vers le code : {tgt_code}")
|