From 53779b59bf1bcef002cba1bd09f5056fc203c8f0 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Mon, 26 Oct 2026 15:14:10 +0800 Subject: [PATCH] refactor(pe): reuse canonical section header facts --- src/domain/binaryTarget.ts | 14 ++++++++++---- src/domain/native/nativeAotPe.ts | 17 +++++++++-------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/domain/binaryTarget.ts b/src/domain/binaryTarget.ts index 4a830e19d7..89755f26aa 100644 --- a/src/domain/binaryTarget.ts +++ b/src/domain/binaryTarget.ts @@ -9,6 +9,7 @@ import type { UniversalMachOSlice, } from "./binaryTargetTypes.js"; import { err, ok, type Result } from "./result.js"; +import { readPeSectionHeader } from "./peSectionHeader.js"; import { readElfFixedHeader } from "./elfFixedHeader.js"; import { mzWindowsHeaderLayoutLimitation, @@ -595,10 +596,15 @@ export const peRvaToFileOffset = ( if (rva < sizeOfHeaders && size <= sizeOfHeaders - rva) return rva; for (let i = 0; i < numberOfSections; i++) { const offset = i * 40; - if (sectionTable.length < offset + 40) break; - const virtualAddress = sectionTable.readUInt32LE(offset + 12); - const sizeOfRawData = sectionTable.readUInt32LE(offset + 16); - const pointerToRawData = sectionTable.readUInt32LE(offset + 20); + const parsed = readPeSectionHeader( + sectionTable.subarray(offset, offset + 40), + ); + if (!parsed.ok) break; + const { + virtual_address: virtualAddress, + raw_size: sizeOfRawData, + offset: pointerToRawData, + } = parsed.value; if ( pointerToRawData > 0 && rva >= virtualAddress && diff --git a/src/domain/native/nativeAotPe.ts b/src/domain/native/nativeAotPe.ts index 6be281e3bb..2e816408aa 100644 --- a/src/domain/native/nativeAotPe.ts +++ b/src/domain/native/nativeAotPe.ts @@ -1,4 +1,5 @@ import { createHash } from "node:crypto"; +import { readPeSectionHeader } from "../peSectionHeader.js"; import type { NativeMetadataRecovery } from "./nativeMetadataRecovery.js"; const RTR_SIGNATURE = 0x00525452; @@ -115,7 +116,6 @@ const nativeAotHydrationFailure = (cause: unknown): string => : "DEHYDRATED_DATA could not be hydrated"; type Section = { - readonly name: string; readonly rva: number; readonly virtualSize: number; readonly rawOffset: number; @@ -530,14 +530,15 @@ class PeImage { ); this.sections = Array.from({ length: count }, (_, index) => { const p = sectionOffset + index * 40; - const name = bytes.toString("ascii", p, p + 8).replace(/\0.*$/u, ""); + const parsed = readPeSectionHeader(bytes.subarray(p, p + 40)); + if (!parsed.ok) throw new Error(parsed.error.message); + const section = parsed.value; return { - name, - virtualSize: this.#u32(p + 8), - rva: this.#u32(p + 12), - rawSize: this.#u32(p + 16), - rawOffset: this.#u32(p + 20), - characteristics: this.#u32(p + 36), + virtualSize: section.virtual_size, + rva: section.virtual_address, + rawSize: section.raw_size, + rawOffset: section.offset, + characteristics: section.characteristics, }; }); for (const section of this.sections) {