refactor(pe): reuse canonical section header facts

This commit is contained in:
morluto committed 2026-10-26 15:14:10 +08:00
1 parent 943ae604df
commit 53779b59bf
2 files changed
+19 -12

No files matched your search

+10 -4
View File
@@ -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 &&
+9 -8
View File
@@ -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) {