mirror of
https://github.com/morluto/rea.git
synced 2026-11-16 11:43:20 +00:00
* 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.
142 lines
4.1 KiB
TypeScript
142 lines
4.1 KiB
TypeScript
import type {
|
|
FileMapping,
|
|
ZeroFillMapping,
|
|
} from "../../domain/virtualFileMapping.js";
|
|
import { admitElfLoadSegment } from "../../domain/elfProgramHeader.js";
|
|
import {
|
|
declaredFileRange,
|
|
BinaryFormatFailure,
|
|
BinaryReader,
|
|
type BinaryContainer,
|
|
type ElfDynamicSegment,
|
|
type ImageSection,
|
|
type LoadRegion,
|
|
} from "../../domain/binaryContainer.js";
|
|
import { elfArchitecture } from "../../domain/elfFixedHeader.js";
|
|
import { readElfDirectoryFromBuffer } from "./NativeSectionDirectory.js";
|
|
|
|
/** Read ELF sections and file-backed program mappings without interpreting language metadata. */
|
|
export const readElfImage = (
|
|
bytes: Buffer,
|
|
): Extract<BinaryContainer, { format: "elf" }> => {
|
|
const directory = readElfDirectoryFromBuffer(bytes);
|
|
const { bits } = directory;
|
|
const reader = new BinaryReader(bytes, directory.little, "elf");
|
|
const mappings: FileMapping[] = [];
|
|
const loadRegions: LoadRegion[] = [];
|
|
const executableMappings: FileMapping[] = [];
|
|
const zeroFills: ZeroFillMapping[] = [];
|
|
const sections: ImageSection[] = [];
|
|
const dynamicSegments: ElfDynamicSegment[] = [];
|
|
for (const program of directory.programs) {
|
|
if (program.type === 2) {
|
|
dynamicSegments.push(program);
|
|
continue;
|
|
}
|
|
// PT_INTERP and GNU program headers are not load segments.
|
|
if (program.type !== 1) continue;
|
|
const {
|
|
flags,
|
|
file_offset: fileOffset,
|
|
virtual_address: address,
|
|
file_size: size,
|
|
memory_size: memorySize,
|
|
} = program;
|
|
const admitted = admitElfLoadSegment(
|
|
{ virtualAddress: address, memorySize, fileSize: size },
|
|
bits,
|
|
);
|
|
if (!admitted.ok)
|
|
throw new BinaryFormatFailure("malformed", admitted.error.message);
|
|
const mapping = {
|
|
...(size === 0n
|
|
? { offset: 0, size: 0 }
|
|
: reader.fileRange(fileOffset, size, "ELF load segment")),
|
|
address,
|
|
};
|
|
if (mapping.size !== 0) {
|
|
mappings.push(mapping);
|
|
loadRegions.push({
|
|
mapping,
|
|
memory_size: memorySize,
|
|
read: (flags & 4) !== 0,
|
|
write: (flags & 2) !== 0,
|
|
execute: (flags & 1) !== 0,
|
|
});
|
|
if ((flags & 1) !== 0) executableMappings.push(mapping);
|
|
}
|
|
if (memorySize > size)
|
|
zeroFills.push({ address: address + size, size: memorySize - size });
|
|
}
|
|
for (const section of directory.sections) {
|
|
const range = declaredFileRange(bytes.length, section.offset, section.size);
|
|
const { file_backed, compressed } = section;
|
|
const read_only =
|
|
section.type === 1 &&
|
|
file_backed &&
|
|
!compressed &&
|
|
(section.flags & 1n) === 0n &&
|
|
(section.flags & 4n) === 0n &&
|
|
(section.flags & 2n) !== 0n;
|
|
if ((section.name === null || section.name === "") && !read_only) continue;
|
|
sections.push({
|
|
format: "elf",
|
|
name: section.name,
|
|
type: section.type,
|
|
flags: section.flags,
|
|
address: section.address,
|
|
declared_range: { offset: section.offset, size: section.size },
|
|
...range,
|
|
file_backed,
|
|
compressed,
|
|
read_only,
|
|
});
|
|
}
|
|
return {
|
|
format: "elf",
|
|
elf: {
|
|
machine: directory.machine,
|
|
type: directory.elf_type,
|
|
dynamicSegments,
|
|
},
|
|
architecture: architectureForElfMachine(directory.machine),
|
|
bits,
|
|
byte_order: reader.little ? "little" : "big",
|
|
mappings,
|
|
load_regions: loadRegions,
|
|
executable_mappings: executableMappings,
|
|
zero_fills: zeroFills,
|
|
sections,
|
|
...(directory.section_names_limitation === undefined
|
|
? {}
|
|
: { section_names_limitation: directory.section_names_limitation }),
|
|
};
|
|
};
|
|
|
|
/** Map ELF machine identifiers to stable architecture names. */
|
|
export const architectureForElfMachine = (machine: number): string => {
|
|
const architecture = elfArchitecture(machine);
|
|
if (architecture !== undefined) {
|
|
switch (architecture) {
|
|
case "x86":
|
|
return "386";
|
|
case "x86_64":
|
|
return "amd64";
|
|
case "powerpc":
|
|
return "ppc";
|
|
default:
|
|
return architecture;
|
|
}
|
|
}
|
|
switch (machine) {
|
|
case 21:
|
|
return "ppc64";
|
|
case 22:
|
|
return "s390";
|
|
case 258:
|
|
return "loongarch";
|
|
default:
|
|
return `elf-machine-${machine}`;
|
|
}
|
|
};
|