Files
reverse-engineering-anything/src/go/GoBuildInfoSearch.ts
T
morluto 264dcd5fee refactor(artifacts): break the artifacts-firmware and artifacts-gpu import cycles (#11331)
* refactor(domain): move BinaryContainer into src/domain

BinaryContainer imports only domain modules and is shared by artifacts, firmware, gpu, go, il2cpp, python, rust and native. Living under src/native made artifacts and firmware import native for its failure types and BinaryReader. Pure move: git mv plus import specifier updates.

* refactor(firmware): move the devicetree parser into src/domain/firmware

The FIT artifact reader in src/artifacts parses embedded devicetree blobs with the parser that lived under src/firmware, while firmware's providers import src/artifacts: the two units imported each other. The parser and its fixture are pure and now sit beside domain/firmware/devicetree.ts. Pure move: git mv plus import specifier and baseline key updates.

* refactor(artifacts): break the artifacts-gpu import cycle (#11335)

* refactor(artifacts): move the LZ4 block decoder into src/artifacts

Pure move: git mv plus import specifier updates. The next commit removes its GPU-specific budget.

* refactor(artifacts): take the LZ4 output budget from the caller

decodeLz4Block bounded every decode by the GPU object budget and threw through rejectGpu, so UnityFS decoding in src/artifacts imported src/gpu while gpu imports artifacts. Callers now pass their budget: GPU payloads keep GPU_OBJECT_BYTES; UnityFS passes its 64 MiB block bound, which its size checks already enforce before decoding. Malformed blocks still throw BinaryFormatFailure with the same messages.

* test(architecture): drop the artifacts-firmware and artifacts-gpu cycles from the layer baseline

* refactor(artifacts): move the PE resource reader into src/artifacts/pe (#11347)

refactor(artifacts): move the PE resource layout and parser into src/artifacts/pe

The Node SEA and Deno standalone readers in src/artifacts locate PE resources with the layout and parser that lived under src/native/pe, while those modules already read through src/artifacts (ArtifactReaderFailure, assertArtifactReadActive). Pure move: git mv plus import specifier, baseline key and spec path updates.
2026-11-16 03:44:53 +08:00

162 lines
5.0 KiB
TypeScript

import type { FileMapping } from "../domain/virtualFileMapping.js";
import {
BinaryFormatFailure,
BinaryReader,
mappedFileOffset,
type BinaryContainer,
} from "../domain/binaryContainer.js";
import { firstKeyed, keyedBy, uniqueKeyed } from "../domain/keyedCollection.js";
export interface GoBuildInfoSearchRange extends FileMapping {
/** Native file mapping containing inline strings after a named Mach-O header. */
readonly inline_mapping?: FileMapping;
}
const fallbackSearchRanges = (
container: BinaryContainer,
): readonly GoBuildInfoSearchRange[] => {
if (container.format === "elf")
return (container.load_regions ?? [])
.filter((region) => region.write && !region.execute)
.map((region) => region.mapping);
if (container.format === "macho")
return (container.load_regions ?? [])
.filter(
(region) =>
region.mapping.address !== 0n &&
region.protection_masks?.current === 3 &&
region.protection_masks.maximum === 3,
)
.slice(0, 1)
.map((region) => region.mapping);
if (container.image_base === undefined) return [];
const section = firstKeyed(
keyedBy(container.sections, (candidate) =>
candidate.format === "pe" &&
candidate.virtual_address !== 0 &&
candidate.raw_size !== 0 &&
(candidate.characteristics & ~0x00600000) >>> 0 === 0xc0000040
? "writable-initialized-data"
: undefined,
),
"writable-initialized-data",
"Go's debug/buildinfo (peExe.DataStart) searches the first writable initialized-data section.",
);
return section?.format === "pe"
? [
{
address: container.image_base + BigInt(section.virtual_address),
offset: section.offset,
size: section.size,
},
]
: [];
};
/** Go owns build-info section selection and validity; native readers only own container facts. */
export const goBuildInfoSearch = (
bytes: Buffer,
container: BinaryContainer,
): readonly GoBuildInfoSearchRange[] => {
const reader = new BinaryReader(
bytes,
container.byte_order === "little",
container.format,
);
const named: GoBuildInfoSearchRange[] = [];
// The build-info section is declared once; a repeat leaves Go's reader
// with no defined choice, whatever the repeated sections contain.
const selected = uniqueKeyed(
keyedBy(container.sections, (section) =>
section.format === "elf"
? section.name
: section.format === "macho"
? section.section_name
: undefined,
),
container.format === "elf" ? ".go.buildinfo" : "__go_buildinfo",
);
if (!selected.ok)
throw new BinaryFormatFailure(
"malformed",
container.format === "elf"
? "ELF contains ambiguous build-info sections"
: "Mach-O has ambiguous build-info sections",
);
const section = selected.value;
if (section !== undefined) {
if (section.format === "elf") {
if (section.type !== 1)
throw new BinaryFormatFailure(
"malformed",
"ELF build-info section is not file-backed data",
);
if (section.compressed)
throw new BinaryFormatFailure(
"unsupported",
"Compressed ELF build-info sections are unsupported",
{ undecodedContent: true },
);
const search = {
...reader.fileRange(
section.declared_range.offset,
section.declared_range.size,
"ELF build-info section",
),
address: section.address,
};
if (
mappedFileOffset(
container.mappings,
search.address,
search.size,
container.zero_fills,
) !== search.offset
)
throw new BinaryFormatFailure(
"malformed",
"ELF build-info section disagrees with its load mapping",
);
named.push(search);
} else if (section.format === "macho") {
if (section.segment_unmapped)
throw new BinaryFormatFailure(
"malformed",
"Mach-O build-info section is in an unmapped segment",
);
if (section.segment_mapping === null)
throw new BinaryFormatFailure(
"malformed",
"Mach-O build-info section is not file mapped",
);
if ([1, 0xc, 0x12].includes(section.type))
throw new BinaryFormatFailure(
"malformed",
"Mach-O build-info section is zero-fill data",
);
const search = {
...reader.fileRange(
section.declared_range.offset,
section.declared_range.size,
"Mach-O build-info section",
),
address: section.address,
inline_mapping: section.segment_mapping,
};
if (
mappedFileOffset(
[section.segment_mapping],
search.address,
search.size,
) !== search.offset
)
throw new BinaryFormatFailure(
"malformed",
"Mach-O build-info section disagrees with its segment",
);
named.push(search);
}
}
return named.length > 0 ? named : fallbackSearchRanges(container);
};