mirror of
https://github.com/astromaddie/HaloCE-VR.git
synced 2026-10-20 10:00:14 +02:00
92 lines
3.4 KiB
Python
92 lines
3.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Generate the guest image's host import stubs.
|
|
|
|
Every function the guest imports from the host (port/android/host_imports.list,
|
|
plus the generated OpenGL ES list) gets a stub in the image that loads the
|
|
host's 64-bit function address from __host_import_table and branches to it.
|
|
The arguments are left untouched in their registers; the host fills in the
|
|
table when it loads the image (port/android/host/host_loader.c).
|
|
|
|
Usage: android_imports.py [--host-table table.c] output.s list...
|
|
|
|
With --host-table, also writes the host's name-to-function table for the
|
|
names in the lists (OpenGL names, which the host resolves at run time, are
|
|
not part of it).
|
|
"""
|
|
|
|
import sys
|
|
|
|
|
|
def main():
|
|
arguments = sys.argv[1:]
|
|
host_table = None
|
|
if arguments[0] == "--host-table":
|
|
host_table = arguments[1]
|
|
arguments = arguments[2:]
|
|
output, lists = arguments[0], arguments[1:]
|
|
names = []
|
|
for path in lists:
|
|
for line in open(path, encoding="utf-8"):
|
|
line = line.split("#", 1)[0].strip()
|
|
if line and line not in names:
|
|
names.append(line)
|
|
out = ["/* generated by tools/android_imports.py; do not edit */", "\t.text"]
|
|
for index, name in enumerate(names):
|
|
offset = index * 8
|
|
out += [
|
|
f"\t.globl {name}",
|
|
f"\t.type {name},%function",
|
|
"\t.p2align 2",
|
|
f"{name}:",
|
|
f"\tadrp x16, __host_import_table+{offset}",
|
|
f"\tldr x16, [x16, :lo12:__host_import_table+{offset}]",
|
|
"\tbr x16",
|
|
]
|
|
out += [
|
|
"\t.section .rodata",
|
|
"\t.globl __host_import_names",
|
|
"__host_import_names:",
|
|
]
|
|
out += [f'\t.asciz "{name}"' for name in names]
|
|
out += [
|
|
"\t.p2align 2",
|
|
"\t.globl __host_import_count",
|
|
"__host_import_count:",
|
|
f"\t.long {len(names)}",
|
|
"\t.data",
|
|
"\t.p2align 3",
|
|
"\t.globl __host_import_table",
|
|
"__host_import_table:",
|
|
f"\t.zero {max(len(names), 1) * 8}",
|
|
]
|
|
with open(output, "w", encoding="utf-8") as f:
|
|
f.write("\n".join(out) + "\n")
|
|
if host_table:
|
|
host_names = [n for n in names if not n.startswith("hostgl_")]
|
|
table = ["/* generated by tools/android_imports.py; do not edit */",
|
|
"#include <string.h>", ""]
|
|
def symbol(n):
|
|
# hostposix_<name> imports port/linux/src/posix.h's posix_<name>
|
|
return "posix_" + n[len("hostposix_"):] if n.startswith("hostposix_") else n
|
|
table += [f"extern void {symbol(n)}(void);" for n in host_names]
|
|
table += ["", "static const struct { const char *name; void *function; } imports[] =", "{"]
|
|
table += [f'\t{{ "{n}", (void *){symbol(n)} }},' for n in host_names]
|
|
table += ["};", "",
|
|
"void *host_resolve_import(const char *name)",
|
|
"{",
|
|
"\tunsigned long index;",
|
|
"",
|
|
"\tfor (index = 0; index < sizeof(imports) / sizeof(imports[0]); index++)",
|
|
"\t{",
|
|
"\t\tif (!strcmp(imports[index].name, name))",
|
|
"\t\t\treturn imports[index].function;",
|
|
"\t}",
|
|
"\treturn NULL;",
|
|
"}"]
|
|
with open(host_table, "w", encoding="utf-8") as f:
|
|
f.write("\n".join(table) + "\n")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|