feat: remove mgc denoise

This commit is contained in:
zhoubinjia committed 2026-08-07 15:52:26 +08:00
1 parent a2700c2d9e
commit 2b2be29093
25 files changed
+156 -14768

No files matched your search

@@ -1,416 +0,0 @@
#!/usr/bin/env python3
"""Analyze the verified MGC full-resolution denoise static closure.
This is intentionally a mechanical analyzer, not a reimplementation. It
records the exact arm64 Halide code ranges, every constant page referenced by
ADRP, link-time task relocations, host callbacks and recovered ABI from the
pinned MGC 9.6.080 V24 libgcastartup.so. The output is research metadata only;
it contains no executable payload and is never packaged by the Android app.
"""
from __future__ import annotations
import argparse
import hashlib
import json
import struct
from dataclasses import dataclass
from pathlib import Path
SOURCE_SHA256 = "2103c54ebba1627733b79e3646105595ed1ccefa2b1629f63fb7e1785a48d6a0"
PAGE_SIZE = 4096
MANIFEST_FORMAT = "mgc-full-resolution-denoise-static-lift-v1"
SHT_RELA = 4
R_AARCH64_RELATIVE = 0x403
HALIDE_DO_PAR_FOR = 0x05A7FEF0
RGB_TO_YUV_NOISE_MATRIX_VADDR = 0x0D64C24
RUN_FULL_RESOLUTION_DENOISE_ENTRY = 0x04670524
RUN_FULL_RESOLUTION_DENOISE_CALLER = 0x0466A3A0
PECAN_LUMA_WRAPPER_ENTRY = 0x04EDE4E0
@dataclass(frozen=True)
class CodeRange:
name: str
start: int
entry: int
end: int
CODE_RANGES = (
CodeRange(
"ComputeBayerNoiseModelF32TileSize16",
0x037A6E14,
0x037AAB68,
0x037B9AF4,
),
CodeRange(
"ComputeRGBNoiseModelF32TileSize16",
0x037B9AF4,
0x037B9AF4,
0x037BECDC,
),
CodeRange(
"DownsampleRawF16ToFloatTileSize16",
0x037BECDC,
0x037BECDC,
0x037C1A8C,
),
CodeRange(
"DownsampleRgbF16ToFloatTileSize16",
0x037C1A8C,
0x037C1A8C,
0x037DB248,
),
CodeRange(
"DownsampleRgbF16ToFloatTileSize16CopyHelper",
0x038561A4,
0x03856414,
0x0385642C,
),
CodeRange("BayerRawToYuv1xS16", 0x049C3484, 0x049C7B64, 0x049CAA54),
CodeRange("RgbRawToYuv1xS16", 0x04A17EA8, 0x04A198B8, 0x04A1C3E8),
CodeRange(
"ChromaDenoisePyramidCompleteS16",
0x04BE28CC,
0x04BE28CC,
0x04BE8764,
),
CodeRange(
"ChromaDenoisePyramidLevelS16",
0x04BEE520,
0x04BF1874,
0x04BF1878,
),
CodeRange(
"ChromaDenoisePyramidDownsampleS16",
0x04BF4CDC,
0x04C394A0,
0x04C394BC,
),
CodeRange("YuvToRgbS16", 0x04CE4F50, 0x04CE554C, 0x04CE61B4),
CodeRange("PecanLumaDenoiseS16", 0x04EF0000, 0x04EF1760, 0x04F1BB2C),
CodeRange(
"ComputeDenoiseStrengthMapsU16",
0x054DBE04,
0x054DCD34,
0x054DE730,
),
CodeRange(
"DownsampleRgbF16ToFloatTileSize16Mode0",
0x05DACC58,
0x05DACC58,
0x05DAD588,
),
CodeRange(
"DownsampleRgbF16ToFloatTileSize16Mode1",
0x05DAD588,
0x05DAD588,
0x05DAE16C,
),
)
# Successful oracle runs reached only malloc/free/trace/par-for. memset is
# retained as a known Halide runtime target for callers whose dimensions select
# a different generated schedule.
HOST_TARGETS = {
0x05EF2634: "halide_malloc",
0x05EF2650: "halide_free",
0x05EF54BC: "halide_trace",
0x05A7FEF0: "halide_do_par_for",
0x05FB8540: "memset",
0x05DAE16C: "downsample_rgb_dispatch",
}
def page_floor(value: int) -> int:
return value & ~(PAGE_SIZE - 1)
def page_ceil(value: int) -> int:
return (value + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1)
def sign_extend(value: int, bits: int) -> int:
sign = 1 << (bits - 1)
return (value ^ sign) - sign
def adrp_target(pc: int, instruction: int) -> int:
immediate = (((instruction >> 5) & 0x7FFFF) << 2) | (
(instruction >> 29) & 0x3
)
return page_floor(pc) + (sign_extend(immediate, 21) << 12)
def branch_target(pc: int, instruction: int) -> int:
return pc + (sign_extend(instruction & 0x03FFFFFF, 26) << 2)
def is_adrp(instruction: int) -> bool:
return instruction & 0x9F000000 == 0x90000000
def is_direct_branch(instruction: int) -> bool:
return instruction & 0xFC000000 in (0x14000000, 0x94000000)
def source_slice(source: bytes, start: int, size: int) -> bytes:
# All selected pages belong to the first two PT_LOAD segments, whose file
# offset equals virtual address in this pinned ELF.
end = start + size
if start < 0 or end > len(source):
raise ValueError(f"source range 0x{start:x}..0x{end:x} is unavailable")
return source[start:end]
def do_par_for_task_slot(
source: bytes,
code_range: CodeRange,
call_pc: int,
) -> int:
"""Recover the task-table slot loaded into x1 for one do_par_for call."""
load_pc = None
load_instruction = 0
for pc in range(call_pc - 4, max(code_range.start, call_pc - 32), -4):
instruction = struct.unpack_from("<I", source, pc)[0]
# LDR X1, [X1, #imm12 * 8]
if instruction & 0xFFC003FF == 0xF9400021:
load_pc = pc
load_instruction = instruction
break
if load_pc is None:
raise ValueError(
f"do_par_for at 0x{call_pc:x} has no nearby x1 task load"
)
for pc in range(load_pc - 4, code_range.start - 1, -4):
instruction = struct.unpack_from("<I", source, pc)[0]
# ADRP X1, target_page
if instruction & 0x9F00001F == 0x90000001:
target_page = adrp_target(pc, instruction)
byte_offset = ((load_instruction >> 10) & 0xFFF) * 8
return target_page + byte_offset
raise ValueError(
f"task load at 0x{load_pc:x} has no preceding ADRP x1"
)
def iter_elf64_rela(source: bytes):
"""Yield (offset, type, addend) from every ELF64 SHT_RELA section."""
if (
source[:4] != b"\x7fELF"
or source[4] != 2
or source[5] != 1
):
raise ValueError("source is not a little-endian ELF64 image")
section_offset = struct.unpack_from("<Q", source, 0x28)[0]
section_entry_size = struct.unpack_from("<H", source, 0x3A)[0]
section_count = struct.unpack_from("<H", source, 0x3C)[0]
if section_entry_size < 64:
raise ValueError("ELF64 section table entry is truncated")
for index in range(section_count):
header = section_offset + index * section_entry_size
section_type = struct.unpack_from("<I", source, header + 4)[0]
if section_type != SHT_RELA:
continue
payload_offset = struct.unpack_from("<Q", source, header + 0x18)[0]
payload_size = struct.unpack_from("<Q", source, header + 0x20)[0]
entry_size = struct.unpack_from("<Q", source, header + 0x38)[0]
if entry_size < 24 or payload_size % entry_size != 0:
raise ValueError("ELF64 RELA section has an invalid entry size")
for cursor in range(
payload_offset,
payload_offset + payload_size,
entry_size,
):
offset, info, addend = struct.unpack_from("<QQq", source, cursor)
yield offset, info & 0xFFFFFFFF, addend
def analyze(source_path: Path, manifest_path: Path) -> None:
source = source_path.read_bytes()
source_digest = hashlib.sha256(source).hexdigest()
if source_digest != SOURCE_SHA256:
raise ValueError(
"refusing a different libgcastartup.so: "
f"expected {SOURCE_SHA256}, got {source_digest}"
)
pages: set[int] = set()
direct_targets: dict[str, set[int]] = {}
do_par_for_calls: list[tuple[CodeRange, int]] = []
for code_range in CODE_RANGES:
for page in range(
page_floor(code_range.start),
page_ceil(code_range.end),
PAGE_SIZE,
):
pages.add(page)
targets: set[int] = set()
code = source_slice(
source,
code_range.start,
code_range.end - code_range.start,
)
for offset in range(0, len(code) - 3, 4):
instruction = struct.unpack_from("<I", code, offset)[0]
pc = code_range.start + offset
if is_adrp(instruction):
target_page = adrp_target(pc, instruction)
if 0 <= target_page < len(source):
pages.add(target_page)
if is_direct_branch(instruction):
target = branch_target(pc, instruction)
targets.add(target)
if target == HALIDE_DO_PAR_FOR:
do_par_for_calls.append((code_range, pc))
direct_targets[code_range.name] = targets
relative_relocations = {
location: target
for location, relocation_type, target in iter_elf64_rela(source)
if relocation_type == R_AARCH64_RELATIVE
}
task_slots = [
do_par_for_task_slot(source, code_range, call_pc)
for code_range, call_pc in do_par_for_calls
]
if len(set(task_slots)) != len(task_slots):
raise ValueError("multiple do_par_for calls unexpectedly share a task slot")
task_relocations = {}
for slot in task_slots:
if slot not in relative_relocations:
raise ValueError(
f"do_par_for task slot 0x{slot:x} has no RELATIVE relocation"
)
task_relocations[slot] = relative_relocations[slot]
code_intervals = [(item.start, item.end) for item in CODE_RANGES]
for location, target in task_relocations.items():
if not any(start <= target < end for start, end in code_intervals):
raise ValueError(
f"task slot 0x{location:x} targets code outside closure: "
f"0x{target:x}"
)
ordered_pages = sorted(pages)
ranges_json = [
{
"name": item.name,
"start": f"0x{item.start:x}",
"entry": f"0x{item.entry:x}",
"end": f"0x{item.end:x}",
}
for item in CODE_RANGES
]
branch_json: dict[str, list[dict[str, str]]] = {}
for name, targets in direct_targets.items():
external = []
for target in sorted(targets):
if any(start <= target < end for start, end in code_intervals):
continue
external.append(
{
"target": f"0x{target:x}",
"host": HOST_TARGETS.get(target, "halide_error_path"),
}
)
branch_json[name] = external
manifest = {
"format": MANIFEST_FORMAT,
"source": str(source_path),
"source_sha256": source_digest,
"page_size": PAGE_SIZE,
"page_count": len(ordered_pages),
"do_par_for_call_count": len(do_par_for_calls),
"code_ranges": ranges_json,
"task_relocations": {
f"0x{location:x}": f"0x{target:x}"
for location, target in sorted(task_relocations.items())
},
"direct_external_targets": branch_json,
"recovered_abi": {
"RgbRawToYuv1xS16": {
"input": "u16[x,y,rgb]",
"output": "s16[x,y,yuv]",
"neutral_wrapper_arguments": {
"black_levels": [0.0, 0.0, 0.0],
"channel_gains": [1.0, 1.0, 1.0],
"input_white_level": 16383,
"output_white_level": 16383,
"gain_map": "f32[x,y,4]=1",
"sample_rate": [1.0, 1.0],
"zoom_ratio": 1.0,
},
},
"ChromaDenoisePyramidCompleteS16": {
"input": "s16[x,y,yuv]",
"strength": "u16[x/4,y/4,3]",
"log2_strength_decimation": 2,
"log2_downsample_factor": 0,
"quadratic": "f32[level=4,branch=2], strides=[1,12]",
"shot": "f32[level=4,branch=2], strides=[1,12]",
"read": "f32[level=4,channel=3,branch=2], strides=[1,4,12]",
"outlier": "u8[level=4,branch=2], strides=[1,4]",
"output": "s16[x,y,yuv]",
},
"YuvToRgbS16": {
"input": "s16[x,y,yuv]",
"output": "s16[x,y,rgb]",
},
"PecanLumaDenoiseS16": {
"input": "s16[x,y]",
"strength": "u16[x/4,y/4,3]",
"read": "f32[level=4,branch=2], strides=[1,4]",
"shot": "f32[level=4,branch=2], strides=[1,4]",
"quadratic": "f32[level=4,branch=2], strides=[1,4]",
"outlier_distance": "f32[level=5]",
"revert_factor": "f32[level=5]",
"output": "s16[x,y]",
},
},
"run_full_resolution_denoise_contract": {
"entry": f"0x{RUN_FULL_RESOLUTION_DENOISE_ENTRY:x}",
"unique_direct_caller": (
f"0x{RUN_FULL_RESOLUTION_DENOISE_CALLER:x}"
),
"non_ai_pecan_wrapper": f"0x{PECAN_LUMA_WRAPPER_ENTRY:x}",
"input": "planar s16 YUV; non-AI Pecan consumes channel-0 luma",
"output": "planar s16 full-resolution luma",
"chroma_boundary": (
"ChromaDenoisePyramidComplete is a separate sibling stage; "
"it is not part of RunFullResolutionDenoise"
),
},
"rgb_to_yuv_noise_matrix": {
"vaddr": f"0x{RGB_TO_YUV_NOISE_MATRIX_VADDR:x}",
"layout": "row-major Y,Cb,Cr by R,G,B",
"values": list(
struct.unpack_from(
"<9f",
source,
RGB_TO_YUV_NOISE_MATRIX_VADDR,
)
),
},
"pages": [f"0x{page:x}" for page in ordered_pages],
}
manifest_path.write_text(
json.dumps(manifest, indent=2, sort_keys=True) + "\n",
encoding="utf-8",
)
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("source", type=Path)
parser.add_argument("manifest", type=Path)
args = parser.parse_args()
analyze(args.source, args.manifest)
if __name__ == "__main__":
main()
@@ -1,480 +0,0 @@
#!/usr/bin/env python3
"""Lift the pinned MGC Halide closure into normal link-time assembly.
The generator copies only the referenced, unpatched code and constant spans
into a compact capsule. The generated source uses ``.incbin`` against that
capsule and replaces every page-relative or cross-range reference with a
standard ELF relocation. Android's normal linker therefore owns code
placement and relocation; normal builds need neither the original
libgcastartup.so nor a runtime page container, mmap, mprotect, or branch
patching.
This is a mechanical binary lift. It intentionally does not translate the
Halide schedule into a hand-written approximation.
"""
from __future__ import annotations
import argparse
import hashlib
import json
import struct
from dataclasses import dataclass
from pathlib import Path
SOURCE_SHA256 = "2103c54ebba1627733b79e3646105595ed1ccefa2b1629f63fb7e1785a48d6a0"
PAGE_SIZE = 4096
@dataclass(frozen=True)
class CodeRange:
name: str
start: int
entry: int
end: int
symbol: str | None = None
CODE_RANGES = (
CodeRange(
"ComputeBayerNoiseModelF32TileSize16",
0x037A6E14,
0x037AAB68,
0x037B9AF4,
"photon_mgc_compute_bayer_noise_model_f32_tile_size_16",
),
CodeRange(
"ComputeRGBNoiseModelF32TileSize16",
0x037B9AF4,
0x037B9AF4,
0x037BECDC,
"photon_mgc_compute_rgb_noise_model_f32_tile_size_16",
),
CodeRange(
"DownsampleRawF16ToFloatTileSize16",
0x037BECDC,
0x037BECDC,
0x037C1A8C,
"photon_mgc_downsample_raw_f16_to_float_tile_size_16",
),
CodeRange(
"DownsampleRgbF16ToFloatTileSize16",
0x037C1A8C,
0x037C1A8C,
0x037DB248,
"photon_mgc_downsample_rgb_f16_to_float_tile_size_16",
),
CodeRange(
"DownsampleRgbF16ToFloatTileSize16CopyHelper",
0x038561A4,
0x03856414,
0x0385642C,
),
CodeRange("BayerRawToYuv1xS16", 0x049C3484, 0x049C7B64, 0x049CAA54),
CodeRange(
"RgbRawToYuv1xS16",
0x04A17EA8,
0x04A198B8,
0x04A1C3E8,
"photon_mgc_rgb_raw_to_yuv_s16",
),
CodeRange(
"ChromaDenoisePyramidCompleteS16",
0x04BE28CC,
0x04BE28CC,
0x04BE8764,
"photon_mgc_chroma_denoise_pyramid_complete_s16",
),
CodeRange(
"ChromaDenoisePyramidLevelS16",
0x04BEE520,
0x04BF1874,
0x04BF1878,
),
CodeRange(
"ChromaDenoisePyramidDownsampleS16",
0x04BF4CDC,
0x04C394A0,
0x04C394BC,
),
CodeRange(
"YuvToRgbS16",
0x04CE4F50,
0x04CE554C,
0x04CE61B4,
"photon_mgc_yuv_to_rgb_s16",
),
CodeRange(
"PecanLumaDenoiseS16",
0x04EF0000,
0x04EF1760,
0x04F1BB2C,
"photon_mgc_pecan_luma_denoise_s16",
),
CodeRange(
"ComputeDenoiseStrengthMapsU16",
0x054DBE04,
0x054DCD34,
0x054DE730,
"photon_mgc_compute_denoise_strength_maps_u16",
),
CodeRange(
"DownsampleRgbF16ToFloatTileSize16Mode0",
0x05DACC58,
0x05DACC58,
0x05DAD588,
"photon_mgc_downsample_rgb_mode_0",
),
CodeRange(
"DownsampleRgbF16ToFloatTileSize16Mode1",
0x05DAD588,
0x05DAD588,
0x05DAE16C,
"photon_mgc_downsample_rgb_mode_1",
),
)
HOST_TARGETS = {
0x05EF2634: "photon_mgc_halide_malloc",
0x05EF2650: "photon_mgc_halide_free",
0x05EF54BC: "photon_mgc_halide_trace",
0x05A7FEF0: "photon_mgc_halide_do_par_for",
0x05FB8540: "memset",
0x05DAE16C: "photon_mgc_downsample_rgb_dispatch",
}
def page_floor(value: int) -> int:
return value & ~(PAGE_SIZE - 1)
def page_ceil(value: int) -> int:
return (value + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1)
def sign_extend(value: int, bits: int) -> int:
sign = 1 << (bits - 1)
return (value ^ sign) - sign
def adrp_target(pc: int, instruction: int) -> int:
immediate = (((instruction >> 5) & 0x7FFFF) << 2) | (
(instruction >> 29) & 0x3
)
return page_floor(pc) + (sign_extend(immediate, 21) << 12)
def adr_target(pc: int, instruction: int) -> int:
immediate = (((instruction >> 5) & 0x7FFFF) << 2) | (
(instruction >> 29) & 0x3
)
return pc + sign_extend(immediate, 21)
def branch_target(pc: int, instruction: int) -> int:
return pc + (sign_extend(instruction & 0x03FFFFFF, 26) << 2)
def relative_imm19_target(pc: int, instruction: int) -> int:
return pc + (sign_extend((instruction >> 5) & 0x7FFFF, 19) << 2)
def relative_imm14_target(pc: int, instruction: int) -> int:
return pc + (sign_extend((instruction >> 5) & 0x3FFF, 14) << 2)
def range_for(address: int) -> CodeRange | None:
return next(
(item for item in CODE_RANGES if item.start <= address < item.end),
None,
)
def address_symbol(address: int) -> str:
return f"photon_mgc_address_{address:x}"
def page_symbol(address: int) -> str:
return f"photon_mgc_page_{address:x}"
def error_symbol(address: int) -> str:
return f"photon_mgc_halide_error_{address:x}"
class CapsuleWriter:
def __init__(self, source: bytes) -> None:
self._source = source
self._capsule = bytearray()
def emit_incbin(self, lines: list[str], start: int, end: int) -> None:
if end <= start:
return
if start < 0 or end > len(self._source):
raise ValueError(
f"capsule span 0x{start:x}..0x{end:x} exceeds source size "
f"0x{len(self._source):x}"
)
capsule_offset = len(self._capsule)
self._capsule.extend(self._source[start:end])
lines.append(
f' .incbin "@MGC_DENOISE_CAPSULE@", '
f"0x{capsule_offset:x}, 0x{end - start:x}"
)
def write(self, output_path: Path) -> str:
capsule = bytes(self._capsule)
output_path.write_bytes(capsule)
return hashlib.sha256(capsule).hexdigest()
def validate_local_pc_relative(
code_range: CodeRange,
pc: int,
instruction: int,
) -> None:
target: int | None = None
kind = ""
if instruction & 0x9F000000 == 0x10000000:
target = adr_target(pc, instruction)
kind = "ADR"
elif instruction & 0x3B000000 == 0x18000000:
target = relative_imm19_target(pc, instruction)
kind = "literal load"
elif instruction & 0xFF000010 == 0x54000000:
target = relative_imm19_target(pc, instruction)
kind = "conditional branch"
elif instruction & 0x7E000000 == 0x34000000:
target = relative_imm19_target(pc, instruction)
kind = "compare branch"
elif instruction & 0x7E000000 == 0x36000000:
target = relative_imm14_target(pc, instruction)
kind = "test branch"
if target is not None and not (
page_floor(code_range.start)
<= target
< page_ceil(code_range.end)
):
raise ValueError(
f"{kind} at 0x{pc:x} escapes {code_range.name}: 0x{target:x}"
)
def generate(
source_path: Path,
manifest_path: Path,
output_path: Path,
capsule_path: Path,
) -> None:
source = source_path.read_bytes()
digest = hashlib.sha256(source).hexdigest()
if digest != SOURCE_SHA256:
raise ValueError(
f"expected libgcastartup.so {SOURCE_SHA256}, got {digest}"
)
manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
pages = {int(value, 16) for value in manifest["pages"]}
task_relocations = {
int(location, 16): int(target, 16)
for location, target in manifest["task_relocations"].items()
}
expected_task_count = manifest["do_par_for_call_count"]
if len(task_relocations) != expected_task_count:
raise ValueError(
f"manifest records {expected_task_count} do_par_for calls but "
f"{len(task_relocations)} task relocations"
)
capsule_writer = CapsuleWriter(source)
code_pages: set[int] = set()
labels: dict[int, list[str]] = {}
patches: dict[int, str] = {}
external_error_targets: set[int] = set()
for code_range in CODE_RANGES:
section_start = page_floor(code_range.start)
section_end = page_ceil(code_range.end)
code_pages.update(range(section_start, section_end, PAGE_SIZE))
if code_range.symbol is not None:
labels.setdefault(code_range.entry, []).append(code_range.symbol)
labels.setdefault(code_range.entry, []).append(
address_symbol(code_range.entry)
)
for pc in range(code_range.start, code_range.end, 4):
instruction = struct.unpack_from("<I", source, pc)[0]
if instruction & 0x9F000000 == 0x90000000:
target_page = adrp_target(pc, instruction)
if target_page not in pages:
raise ValueError(
f"ADRP at 0x{pc:x} references missing page "
f"0x{target_page:x}"
)
register = instruction & 0x1F
patches[pc] = (
f" adrp x{register}, {page_symbol(target_page)}"
)
continue
if instruction & 0xFC000000 in (0x14000000, 0x94000000):
target = branch_target(pc, instruction)
target_range = range_for(target)
if target_range is code_range:
continue
mnemonic = (
"bl"
if instruction & 0xFC000000 == 0x94000000
else "b"
)
if target_range is not None:
symbol = address_symbol(target)
labels.setdefault(target, []).append(symbol)
else:
symbol = HOST_TARGETS.get(target, error_symbol(target))
if target not in HOST_TARGETS:
external_error_targets.add(target)
patches[pc] = f" {mnemonic} {symbol}"
continue
validate_local_pc_relative(code_range, pc, instruction)
for target in task_relocations.values():
if range_for(target) is None:
raise ValueError(
f"task relocation target 0x{target:x} is outside code closure"
)
labels.setdefault(target, []).append(address_symbol(target))
lines = [
"/* Generated mechanically from pinned MGC 9.6.080 V24.",
" * Do not edit; regenerate with",
" * scripts/research/generate_mgc_denoise_static_asm.py.",
" */",
"",
"#if !defined(__aarch64__)",
'#error "MGC denoise static lift is arm64-only"',
"#endif",
"",
]
for target in sorted(external_error_targets):
lines.append(f" .hidden {error_symbol(target)}")
for symbol in sorted(set(HOST_TARGETS.values()) - {"memset"}):
lines.append(f" .hidden {symbol}")
lines.append("")
code_groups: list[list[CodeRange]] = []
for code_range in sorted(CODE_RANGES, key=lambda value: value.start):
section_start = page_floor(code_range.start)
if (
not code_groups
or section_start >= max(
page_ceil(value.end) for value in code_groups[-1]
)
):
code_groups.append([code_range])
else:
code_groups[-1].append(code_range)
for code_group in code_groups:
section_start = min(page_floor(value.start) for value in code_group)
section_end = max(page_ceil(value.end) for value in code_group)
section_name = "__".join(value.name for value in code_group)
lines.extend(
[
(
f'.section .text.photon_mgc.{section_name},'
'"ax",@progbits'
),
" .p2align 12",
]
)
for code_range in code_group:
if code_range.symbol is not None:
lines.extend(
[
f" .global {code_range.symbol}",
f" .hidden {code_range.symbol}",
f" .type {code_range.symbol}, %function",
]
)
events = set(range(section_start, section_end, PAGE_SIZE))
events.update(
address
for address in labels
if section_start <= address < section_end
)
events.update(
address
for address in patches
if section_start <= address < section_end
)
position = section_start
for address in sorted(events):
capsule_writer.emit_incbin(lines, position, address)
if address % PAGE_SIZE == 0:
lines.append(f"{page_symbol(address)}:")
for symbol in dict.fromkeys(labels.get(address, [])):
lines.append(f"{symbol}:")
patch = patches.get(address)
if patch is not None:
lines.append(patch)
position = address + 4
else:
position = address
capsule_writer.emit_incbin(lines, position, section_end)
lines.append("")
relocation_pages = {page_floor(value) for value in task_relocations}
for page in sorted(pages - code_pages):
if page in relocation_pages:
lines.append(
f'.section .data.rel.ro.photon_mgc.page_{page:x},'
'"aw",@progbits'
)
else:
lines.append(
f'.section .rodata.photon_mgc.page_{page:x},"a",@progbits'
)
lines.extend([" .p2align 12", f"{page_symbol(page)}:"])
page_relocations = sorted(
(location, target)
for location, target in task_relocations.items()
if page_floor(location) == page
)
position = page
for location, target in page_relocations:
capsule_writer.emit_incbin(lines, position, location)
lines.append(f" .xword {address_symbol(target)}")
position = location + 8
capsule_writer.emit_incbin(lines, position, page + PAGE_SIZE)
lines.append("")
lines.extend(
[
'.section .note.GNU-stack,"",@progbits',
"",
]
)
output_path.write_text("\n".join(lines), encoding="utf-8")
capsule_sha256 = capsule_writer.write(capsule_path)
print(
f"generated={output_path} pages={len(pages)} "
f"patches={len(patches)} "
f"task_relocations={len(task_relocations)} "
f"error_targets={len(external_error_targets)} "
f"capsule={capsule_path} capsule_sha256={capsule_sha256}"
)
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("source", type=Path)
parser.add_argument("manifest", type=Path)
parser.add_argument("output", type=Path)
parser.add_argument("capsule", type=Path)
args = parser.parse_args()
generate(args.source, args.manifest, args.output, args.capsule)
if __name__ == "__main__":
main()
File diff suppressed because it is too large. Load diff