Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

refactor(macho): separate dyld import and export parsing

morlutoaa357d6d916b
diff --git a/src/native/NativeMachoInspection.ts b/src/native/NativeMachoInspection.ts
index 138c314f6..e90577776 100644
--- a/src/native/NativeMachoInspection.ts
+++ b/src/native/NativeMachoInspection.ts
@@ -13,7 +13,7 @@ import {
MACHO_ARCHITECTURE_POLICIES,
selectMachoArchitectureName,
} from "./MachoArchitectureSelection.js";
-import { parseDyldSymbols } from "./parsers/dyldInfo.js";
+import { parseDyldExports, parseDyldImports } from "./parsers/dyldInfo.js";
import { parseLipoOutput, type LipoArchitecture } from "./parsers/lipo.js";
import { withoutEchoedPathHeader } from "./parsers/echoedPath.js";
import { parseOtoolLoadCommands } from "./parsers/otool.js";
@@ -251,13 +251,7 @@ const normalizeMacho = (
);
const imports = missingImportsUuid
? []
- : parseDyldSymbols(
- sliceOutput(dyldImportsCapture),
- "imports",
- null,
- new Set(),
- hasChainedFixups,
- );
+ : parseDyldImports(sliceOutput(dyldImportsCapture), hasChainedFixups);
const imageBase =
load.segments.find(
(segment) => segment.file_offset === 0 && (segment.file_size ?? 0) > 0,
@@ -265,9 +259,8 @@ const normalizeMacho = (
const nmExports = parseNmExports(byTool("nm").stdout);
const dyldExports = missingExportsUuid
? []
- : parseDyldSymbols(
+ : parseDyldExports(
sliceOutput(dyldExportsCapture),
- "exports",
imageBase,
new Set(nmExports.map(({ name }) => name)),
);
@@ -341,6 +334,11 @@ const normalizeMacho = (
imports: covered(imports, false, [
"dyld_info textual imports may omit chained or toolchain-unsupported metadata.",
...(missingImportsUuid ? [missingUuidLimitation("imports")] : []),
+ ...(imports.some(({ name_status }) => name_status === "ambiguous")
+ ? [
+ "Chained import text contains symbol/addend expressions whose split is ambiguous.",
+ ]
+ : []),
]),
exports: covered(exports, false, [
...(missingExportsUuid
@@ -399,7 +397,7 @@ const namesEveryExport = (
/^offset\s+symbol$/u.test(line)
)
return true;
- const rows = parseDyldSymbols(line, "exports", null, names);
+ const rows = parseDyldExports(line, null, names);
// The parser withholds a row only when nm names both of its readings.
if (rows.length === 0)
return names.has(/^0x[\da-f]+\s+(.+)$/isu.exec(line)?.[1] ?? "");
diff --git a/src/native/parsers/dyldInfo.ts b/src/native/parsers/dyldInfo.ts
index 0f8b8a112..777725f48 100644
--- a/src/native/parsers/dyldInfo.ts
+++ b/src/native/parsers/dyldInfo.ts
@@ -19,111 +19,96 @@ interface ParsedDyldExport extends ParsedDyldSymbolFields {
readonly sourceSymbol?: string;
}
-/** Decode Apple dyld inventory rows, projecting export offsets to VM addresses. */
-export function parseDyldSymbols(
+/** Decode Apple dyld import rows, retaining chained suffix ambiguity. */
+export const parseDyldImports = (
output: string,
- mode: "imports",
- imageBase?: string | null,
- knownExportNames?: ReadonlySet<string>,
- hasChainedFixups?: boolean,
-): ParsedDyldImport[];
-export function parseDyldSymbols(
- output: string,
- mode: "exports",
- imageBase?: string | null,
- knownExportNames?: ReadonlySet<string>,
-): ParsedDyldExport[];
-export function parseDyldSymbols(
+ hasChainedFixups: boolean,
+): ParsedDyldImport[] =>
+ output.split("\n").flatMap<ParsedDyldImport>((rawLine) => {
+ const line = rawLine.trim();
+ const imported =
+ /^(?:0x[\da-f]+\s+)?(.+?)(?:\s+\[([^\]]+)\])?\s+\(from (.+)\)$/iu.exec(
+ line,
+ );
+ if (imported?.[1] === undefined) return [];
+ const ambiguousSuffix = hasChainedFixups
+ ? /^(.*)\+0x[\da-f]+$/iu.test(imported[1])
+ : false;
+ return [
+ {
+ name: ambiguousSuffix ? null : imported[1],
+ raw_name: ambiguousSuffix ? imported[1] : null,
+ name_status: ambiguousSuffix ? "ambiguous" : "exact",
+ addend: null,
+ address: null,
+ weak: /\bweak-import\b/u.test(imported[2] ?? "") ? true : null,
+ reexport: null,
+ source: imported[3] ?? null,
+ },
+ ];
+ });
+
+/** Decode Apple dyld export rows, projecting export offsets to VM addresses. */
+export const parseDyldExports = (
output: string,
- mode: "imports" | "exports",
- imageBase: string | null = null,
- knownExportNames: ReadonlySet<string> = new Set(),
- hasChainedFixups = false,
-): (ParsedDyldImport | ParsedDyldExport)[] {
+ imageBase: string | null,
+ knownExportNames: ReadonlySet<string>,
+): ParsedDyldExport[] => {
const offsets = /^\s*offset\s+symbol\s*$/mu.test(output);
// dyld_info ends rows with LF. A carriage return before it belongs to the
// symbol name, so export rows keep their trailing text.
- return output
- .split("\n")
- .flatMap<ParsedDyldImport | ParsedDyldExport>((rawLine) => {
- const line = mode === "exports" ? rawLine.trimStart() : rawLine.trim();
- if (mode === "imports") {
- const imported =
- /^(?:0x[\da-f]+\s+)?(.+?)(?:\s+\[([^\]]+)\])?\s+\(from (.+)\)$/iu.exec(
- line,
- );
- if (imported?.[1] !== undefined) {
- const ambiguousSuffix = hasChainedFixups
- ? /^(.*)\+0x[\da-f]+$/iu.test(imported[1])
- : false;
- return [
- {
- name: ambiguousSuffix ? null : imported[1],
- raw_name: ambiguousSuffix ? imported[1] : null,
- name_status: ambiguousSuffix ? "ambiguous" : "exact",
- addend: null,
- address: null,
- weak: /\bweak-import\b/u.test(imported[2] ?? "") ? true : null,
- reexport: null,
- source: imported[3] ?? null,
- },
- ];
- }
- return [];
- }
- // dyld_info prints `_sym (from lib)`, or `_new (_old from lib)` when the
- // source library names the symbol differently. Split the trailing
- // parenthesized suffix with string scans so a library path containing
- // " from ", or a source symbol containing parentheses such as
- // `operator()`, cannot pull the match into nested lazy quantifiers.
- const reexport = parseReexportRow(line, knownExportNames);
- if (reexport !== null)
- return [
- {
- name: reexport.name,
- address: null,
- weak: null,
- reexport: true,
- source: reexport.source,
- ...(reexport.sourceSymbol === undefined
- ? {}
- : { sourceSymbol: reexport.sourceSymbol }),
- },
- ];
- const exported = /^(0x[\da-f]+)\s+(.+?)(?:\s+\[([^\]]+)\])?$/isu.exec(
- line,
- );
- if (exported?.[1] === undefined || exported[2] === undefined) return [];
- const rawName = /^0x[\da-f]+\s+(.+)$/isu.exec(line)?.[1];
- const literalName =
- rawName !== undefined && knownExportNames.has(rawName);
- // If nm establishes both interpretations, the textual row cannot bind an address or flags.
- // Let the caller retain the exact nm names without guessing which one owns this row.
- if (
- literalName &&
- exported[3] !== undefined &&
- knownExportNames.has(exported[2])
- )
- return [];
- const annotation = literalName ? undefined : exported[3];
- const absolute = /\babsolute\b/u.test(annotation ?? "");
- const address =
- offsets && !absolute
- ? imageBase === null
- ? null
- : `0x${(BigInt(imageBase) + BigInt(exported[1])).toString(16)}`
- : canonicalHex(exported[1]);
+ return output.split("\n").flatMap<ParsedDyldExport>((rawLine) => {
+ const line = rawLine.trimStart();
+ // dyld_info prints `_sym (from lib)`, or `_new (_old from lib)` when the
+ // source library names the symbol differently. Split the trailing
+ // parenthesized suffix with string scans so a library path containing
+ // " from ", or a source symbol containing parentheses such as
+ // `operator()`, cannot pull the match into nested lazy quantifiers.
+ const reexport = parseReexportRow(line, knownExportNames);
+ if (reexport !== null)
return [
{
- name: literalName ? rawName : exported[2],
- address,
- weak: /\bweak-def\b/u.test(annotation ?? "") ? true : null,
- reexport: false,
- source: null,
+ name: reexport.name,
+ address: null,
+ weak: null,
+ reexport: true,
+ source: reexport.source,
+ ...(reexport.sourceSymbol === undefined
+ ? {}
+ : { sourceSymbol: reexport.sourceSymbol }),
},
];
- });
-}
+ const exported = /^(0x[\da-f]+)\s+(.+?)(?:\s+\[([^\]]+)\])?$/isu.exec(line);
+ if (exported?.[1] === undefined || exported[2] === undefined) return [];
+ const rawName = /^0x[\da-f]+\s+(.+)$/isu.exec(line)?.[1];
+ const literalName = rawName !== undefined && knownExportNames.has(rawName);
+ // If nm establishes both interpretations, the textual row cannot bind an address or flags.
+ // Let the caller retain the exact nm names without guessing which one owns this row.
+ if (
+ literalName &&
+ exported[3] !== undefined &&
+ knownExportNames.has(exported[2])
+ )
+ return [];
+ const annotation = literalName ? undefined : exported[3];
+ const absolute = /\babsolute\b/u.test(annotation ?? "");
+ const address =
+ offsets && !absolute
+ ? imageBase === null
+ ? null
+ : `0x${(BigInt(imageBase) + BigInt(exported[1])).toString(16)}`
+ : canonicalHex(exported[1]);
+ return [
+ {
+ name: literalName ? rawName : exported[2],
+ address,
+ weak: /\bweak-def\b/u.test(annotation ?? "") ? true : null,
+ reexport: false,
+ source: null,
+ },
+ ];
+ });
+};
const canonicalHex = (value: string | undefined): string | null =>
value === undefined ? null : `0x${BigInt(value).toString(16)}`;
diff --git a/tests/boundary/providers/native/nativeInventory.golden.test.ts b/tests/boundary/providers/native/nativeInventory.golden.test.ts
index b8f37160b..ccc86f64d 100644
--- a/tests/boundary/providers/native/nativeInventory.golden.test.ts
+++ b/tests/boundary/providers/native/nativeInventory.golden.test.ts
@@ -16,7 +16,10 @@ import {
} from "../../../../src/native/CommandRunner.js";
import { NativeMacOSProvider } from "../../../../src/native/NativeMacOSProvider.js";
import { parseOtoolLoadCommands } from "../../../../src/native/parsers/otool.js";
-import { parseDyldSymbols } from "../../../../src/native/parsers/dyldInfo.js";
+import {
+ parseDyldExports,
+ parseDyldImports,
+} from "../../../../src/native/parsers/dyldInfo.js";
import {
NativeFixtureRunner as FixtureRunner,
nativeFixture as fixture,
@@ -294,9 +297,7 @@ it("retains native inventory facts from captured Apple tool output", async () =>
]),
},
});
- expect(
- parseDyldSymbols(await fixture("dyld-imports.txt"), "imports"),
- ).toEqual([
+ expect(parseDyldImports(await fixture("dyld-imports.txt"), false)).toEqual([
{
name: "__Block_copy",
raw_name: null,
@@ -319,9 +320,10 @@ it("retains native inventory facts from captured Apple tool output", async () =>
},
]);
expect(
- parseDyldSymbols(
+ parseDyldExports(
await fixture("dyld-inventory/library-reexports.txt"),
- "exports",
+ null,
+ new Set(),
),
).toEqual([
{
@@ -332,7 +334,7 @@ it("retains native inventory facts from captured Apple tool output", async () =>
source: "libc++abi",
},
]);
- expect(parseDyldSymbols(exportsOutput, "exports")).toContainEqual({
+ expect(parseDyldExports(exportsOutput, null, new Set())).toContainEqual({
name: "_fixture_export",
address: null,
weak: null,
@@ -365,6 +367,10 @@ it("reports a chained bind addend separately in native inspection", async () =>
if (!execution.ok) throw execution.error;
expect(execution.value.result).toMatchObject({
imports: {
+ exhaustive: false,
+ limitations: expect.arrayContaining([
+ "Chained import text contains symbol/addend expressions whose split is ambiguous.",
+ ]),
items: [
{
name: "_fixture_data",
@@ -428,13 +434,12 @@ it("splits re-export rows at the source library", () => {
// dyld_info -exports rows for synthetic dylibs: a source symbol named
// `operator()`, and a library whose install name contains ` from `.
expect(
- parseDyldSymbols(
+ parseDyldExports(
[
"[re-export] _call_renamed (operator() from libfoo)",
"[re-export] _g (from lib from foo)",
"[re-export] _nested (from lib (from foo))",
].join("\n"),
- "exports",
null,
new Set(["_call_renamed", "_g", "_nested"]),
),
@@ -466,7 +471,7 @@ it("splits re-export rows at the source library", () => {
it("retains symbol names containing spaces in dyld inventory rows", () => {
expect(
- parseDyldSymbols(" 0x0000 _entry with space (from fixture)", "imports"),
+ parseDyldImports(" 0x0000 _entry with space (from fixture)", false),
).toEqual([
{
name: "_entry with space",
@@ -480,10 +485,10 @@ it("retains symbol names containing spaces in dyld inventory rows", () => {
},
]);
expect(
- parseDyldSymbols(
+ parseDyldExports(
" offset symbol\n 0x00000348 _entry with space",
- "exports",
"0x0",
+ new Set(),
),
).toEqual([
{
@@ -495,7 +500,11 @@ it("retains symbol names containing spaces in dyld inventory rows", () => {
},
]);
expect(
- parseDyldSymbols("[re-export] _entry with space (from fixture)", "exports"),
+ parseDyldExports(
+ "[re-export] _entry with space (from fixture)",
+ null,
+ new Set(),
+ ),
).toEqual([
{
name: "_entry with space",
@@ -506,7 +515,7 @@ it("retains symbol names containing spaces in dyld inventory rows", () => {
},
]);
expect(
- parseDyldSymbols("0x123 _weak with space [weak-def]", "exports"),
+ parseDyldExports("0x123 _weak with space [weak-def]", null, new Set()),
).toEqual([
{
name: "_weak with space",
@@ -652,9 +661,8 @@ it.each(["weak-def", "absolute", "literal suffix"])(
(suffix) => {
const name = `symbol [${suffix}]`;
expect(
- parseDyldSymbols(
+ parseDyldExports(
`offset symbol\n0x120 ${name}`,
- "exports",
"0x1000",
new Set([name]),
),
@@ -666,12 +674,7 @@ it.each(["weak-def", "absolute", "literal suffix"])(
it("keeps true weak annotations when nm establishes the unannotated name", () => {
expect(
- parseDyldSymbols(
- "0x120 _weak [weak-def]",
- "exports",
- null,
- new Set(["_weak"]),
- ),
+ parseDyldExports("0x120 _weak [weak-def]", null, new Set(["_weak"])),
).toEqual([
{
name: "_weak",
@@ -685,11 +688,8 @@ it("keeps true weak annotations when nm establishes the unannotated name", () =>
it("retains chained import suffixes as ambiguous raw text", () => {
expect(
- parseDyldSymbols(
+ parseDyldImports(
"0x0000 _fixture_data (from libFixture)\n0x0001 _fixture_data+0x4 (from libFixture)\n0x0002 _fixture_weak+0x8 [weak-import] (from libFixture)",
- "imports",
- null,
- new Set(),
true,
),
).toEqual([
@@ -725,13 +725,7 @@ it("retains chained import suffixes as ambiguous raw text", () => {
},
]);
expect(
- parseDyldSymbols(
- "0x0001 _literal+0x4 (from libFixture)",
- "imports",
- null,
- new Set(),
- true,
- ),
+ parseDyldImports("0x0001 _literal+0x4 (from libFixture)", true),
).toEqual([
{
name: null,
@@ -745,15 +739,14 @@ it("retains chained import suffixes as ambiguous raw text", () => {
},
]);
expect(
- parseDyldSymbols("0x0001 _literal+0x4 (from libFixture)", "imports"),
+ parseDyldImports("0x0001 _literal+0x4 (from libFixture)", false),
).toMatchObject([{ name: "_literal+0x4", name_status: "exact" }]);
});
it("leaves an ambiguous short and literal name to the caller's nm inventory", () => {
expect(
- parseDyldSymbols(
+ parseDyldExports(
"0x120 symbol [weak-def]",
- "exports",
null,
new Set(["symbol", "symbol [weak-def]"]),
),
@@ -762,12 +755,7 @@ it("leaves an ambiguous short and literal name to the caller's nm inventory", ()
it("retains unmatched re-export rows when the known export inventory is incomplete", () => {
expect(
- parseDyldSymbols(
- "[re-export] _g (from libfoo)",
- "exports",
- null,
- new Set(["_own"]),
- ),
+ parseDyldExports("[re-export] _g (from libfoo)", null, new Set(["_own"])),
).toEqual([
{
name: "_g",
@@ -780,7 +768,7 @@ it("retains unmatched re-export rows when the known export inventory is incomple
});
it("retains annotation parsing without nm name evidence", () => {
- expect(parseDyldSymbols("0x120 _weak [weak-def]", "exports")).toEqual([
+ expect(parseDyldExports("0x120 _weak [weak-def]", null, new Set())).toEqual([
{
name: "_weak",
address: "0x120",