refactor(macho): derive named section shape from projection (#17128)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-12-06 00:04:44 +08:00
1 parent 93d5ab31e2
commit 276f636491
1 file changed
+24 -33
+24 -33
View File
@@ -25,6 +25,7 @@ import {
import {
machoSectionHeaderSize,
readMachoSectionHeader,
type MachoSectionHeader,
} from "./machoSectionHeader.js";
import { createHash } from "node:crypto";
import {
@@ -93,6 +94,26 @@ const PROJECTED_STRING_EXPANSION = 24;
const DEFAULT_PROJECTED_BYTES = 64 * 1024 * 1024;
const COMMAND_STRING_PREFIX_BYTES = 4096;
const projectSection = (
section: MachoSectionHeader,
retainBytes: (bytes: Uint8Array) => Uint8Array,
) => ({
name: retainBytes(section.nameBytes),
segmentName: retainBytes(section.segmentNameBytes),
address: section.address,
size: section.size,
fileOffset: section.offset,
alignmentExponent: section.alignmentExponent,
relocationOffset: section.relocationOffset,
relocationCount: section.relocationCount,
reserved1: section.reserved1,
reserved2: section.reserved2,
...(section.reserved3 === undefined ? {} : { reserved3: section.reserved3 }),
flags: section.flags,
});
type NamedSection = Readonly<ReturnType<typeof projectSection>>;
export interface MachoCommandNames {
readonly sliceSize: number;
readonly header: {
@@ -123,20 +144,7 @@ export interface MachoCommandNames {
readonly protection: number;
readonly flags: number;
};
readonly sections?: readonly {
readonly name: Uint8Array;
readonly segmentName: Uint8Array;
readonly address: bigint;
readonly size: bigint;
readonly fileOffset: number;
readonly alignmentExponent: number;
readonly relocationOffset: number;
readonly relocationCount: number;
readonly reserved1: number;
readonly reserved2: number;
readonly reserved3?: number;
readonly flags: number;
}[];
readonly sections?: readonly NamedSection[];
readonly commandString?: Uint8Array;
readonly commandStringOffset?: number;
readonly dylib?: ReturnType<typeof readMachoDylibFields>;
@@ -324,9 +332,7 @@ export const readMachoCommandNames = async (
: null;
let segmentName: Uint8Array | undefined;
let segmentFacts: MachoCommandNames["commands"][number]["segment"];
let sections:
| NonNullable<MachoCommandNames["commands"][number]["sections"]>[number][]
| undefined;
let sections: NamedSection[] | undefined;
if (segmentWide !== null) {
const segmentHeaderSize = machoSegmentHeaderSize(segmentWide);
if (frame.size < segmentHeaderSize)
@@ -367,22 +373,7 @@ export const readMachoCommandNames = async (
table.littleEndian,
);
if (!section.ok) throw malformedInput("macho", section.error.message);
sections.push({
name: retain(section.value.nameBytes),
segmentName: retain(section.value.segmentNameBytes),
address: section.value.address,
size: section.value.size,
fileOffset: section.value.offset,
alignmentExponent: section.value.alignmentExponent,
relocationOffset: section.value.relocationOffset,
relocationCount: section.value.relocationCount,
reserved1: section.value.reserved1,
reserved2: section.value.reserved2,
...(section.value.reserved3 === undefined
? {}
: { reserved3: section.value.reserved3 }),
flags: section.value.flags,
});
sections.push(projectSection(section.value, retain));
}
}
// Dylib and rpath commands own an lc_str at offset 8. Retain bytes up to