diff --git a/src/domain/apple/machoCommandNames.ts b/src/domain/apple/machoCommandNames.ts index 8e523d02375..db9c15dced9 100644 --- a/src/domain/apple/machoCommandNames.ts +++ b/src/domain/apple/machoCommandNames.ts @@ -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>; + 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; @@ -324,9 +332,7 @@ export const readMachoCommandNames = async ( : null; let segmentName: Uint8Array | undefined; let segmentFacts: MachoCommandNames["commands"][number]["segment"]; - let sections: - | NonNullable[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