Files
reverse-engineering-anything/src/domain/keyedCollection.ts
T
morluto 5d5ab53536 refactor(native): select ELF sections through the keyed owner (#10780)
* refactor(native): select ELF sections through the keyed owner

* test(native): pin returned exports and versions for repeated version sections

* refactor(nativeaot): read neither of repeated frozen object regions (#10790)

* refactor(nativeaot): read neither of repeated frozen object regions through the keyed owner

* refactor(parsers): retire every first-match section lookup (#10793)

* fix(nativeaot): report an ambiguous frozen object region as partial (#10812)

* refactor(native): select program headers and load commands through the keyed owner (#10827)

* refactor(native): select the GOT base and dependency segments through the keyed owner (#10847)

* refactor(native): select the GOT base and dependency segments through the keyed owner

resolveGotBase selects the SHT_DYNAMIC section and the allocated .got.plt/.got sections with uniqueKeyed, so a repeat leaves the GOT base unknown instead of taking the first match; elfDependencies reads PT_LOAD segments through the same keyed program table as PT_DYNAMIC. A new keyed-first-match guard recognizes a find/findLast or filter-then-[0] whose predicate compares a key field, whatever the table is called; existing matches are baselined. The unwind fixture can declare PT_DYNAMIC headers and repeated .dynamic/.got.plt sections.

* test(native): pin the PT_DYNAMIC path and destructured keyed lookups

The keyed-first-match guard moves to scripts/verify/keyed-first-match.mjs with focused tests, and now recognizes a destructured key in any position and a destructured filter predicate. The single PT_DYNAMIC unwind test declares .dynamic as SHT_PROGBITS so only the program header supplies DT_PLTGOT, and asserts the resolved ranges.

* fix(macho): refuse repeated chained-fixup and dyld-info tables (#10850)

parseMachoLayout kept the last LC_DYLD_CHAINED_FIXUPS range and merged the rebase/bind streams of every LC_DYLD_INFO(_ONLY) command. A repeat now refuses the slice as malformed. NativeMachoInspection's chained import-name reader already declined repeated LC_DYLD_CHAINED_FIXUPS; that rule is now covered by a test.

* fix(verify): count a filter index only on the filter's own result
2026-11-15 17:03:25 +08:00

106 lines
3.4 KiB
TypeScript

import { err, ok, type Result } from "./result.js";
/**
* What keys a table: a name, or a numeric tag such as an ELF section type.
*/
export type TableKey = string | number;
/** One keyed member of a parsed table, at its position among keyed members. */
export interface KeyedEntry<T, K extends TableKey = string> {
readonly key: K;
readonly ordinal: number;
readonly value: T;
}
/**
* The keyed members of one parsed table or object, in source order, with
* every repeated key retained. A lookup states what a repeat means through
* its selector, instead of inheriting `find` (first) or `Map.set` (last).
*/
export interface Keyed<T, K extends TableKey = string> {
readonly entries: readonly KeyedEntry<T, K>[];
/** Keys declared more than once, in the order they first repeat. */
readonly duplicates: readonly K[];
}
/** A key the format allows once, declared more than once. */
export interface DuplicateKey<K extends TableKey = string> {
readonly kind: "duplicate";
readonly key: K;
/** Ordinals of every declaration of the key. */
readonly ordinals: readonly number[];
}
/**
* Key the members of a parsed table. Members without a key (null or
* undefined), such as an unnamed section, are not keyed and cannot be
* selected by key.
*/
export const keyedBy = <T, K extends TableKey = string>(
values: Iterable<T>,
keyOf: (value: T) => K | null | undefined,
): Keyed<T, K> => {
const entries: KeyedEntry<T, K>[] = [];
const seen = new Set<K>();
const duplicates = new Set<K>();
for (const value of values) {
const key = keyOf(value);
if (key === undefined || key === null) continue;
if (seen.has(key)) duplicates.add(key);
seen.add(key);
entries.push({ key, ordinal: entries.length, value });
}
return { entries, duplicates: [...duplicates] };
};
/**
* The single declaration of a key the format allows once: undefined when
* absent, and a typed failure carrying every ordinal when repeated.
*/
export const uniqueKeyed = <T, K extends TableKey>(
keyed: Keyed<T, K>,
key: K,
): Result<T | undefined, DuplicateKey<K>> => {
const matches = keyed.entries.filter((entry) => entry.key === key);
if (matches.length > 1)
return err({
kind: "duplicate",
key,
ordinals: matches.map(({ ordinal }) => ordinal),
});
return ok(matches[0]?.value);
};
/**
* Every declaration of a key the format allows to repeat, in source order,
* such as same-named sections that a consumer reads together.
*/
export const allKeyed = <T, K extends TableKey>(
keyed: Keyed<T, K>,
key: K,
): readonly T[] =>
keyed.entries.flatMap((entry) => (entry.key === key ? [entry.value] : []));
/**
* The first or last declaration of a key, for a format whose own consumer
* defines that precedence. `precedence` states that rule, so a choice among
* repeats is never inherited from `find` or `Map.set`.
*/
const precedenceKeyed =
(pick: "first" | "last") =>
<T, K extends TableKey>(
keyed: Keyed<T, K>,
key: K,
precedence: string,
): T | undefined => {
if (precedence.trim() === "")
throw new TypeError("A keyed precedence must state its format rule");
const matches = allKeyed(keyed, key);
return pick === "first" ? matches[0] : matches.at(-1);
};
/** The first declaration of a key; see {@link precedenceKeyed}. */
export const firstKeyed = precedenceKeyed("first");
/** The last declaration of a key; see {@link precedenceKeyed}. */
export const lastKeyed = precedenceKeyed("last");