Skip to content

armgit / rea

Public Imported from GitHub
0

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

fix(native): retain every repeated otool load-command field (#4048)

Kaoru082247b4628bac73
diff --git a/docs/native-investigation.md b/docs/native-investigation.md
index ed9ffe46f..4ec367cda 100644
--- a/docs/native-investigation.md
+++ b/docs/native-investigation.md
@@ -270,7 +270,9 @@ Mach-O image. Architecture
`file_offset` values are container-relative. Entrypoint, segment and section
`file_offset` values retain their source meaning and are relative to the
selected Mach-O slice. Raw load-command fields retain their original values.
-Evidence locations map the LC_MAIN entrypoint and nonempty file-backed segment
+A field otool prints more than once, such as each `LC_BUILD_VERSION` tool, is
+keyed `name #n` in print order. Each `LC_LINKER_OPTION` string is keyed
+`string #n` and checked against the declared `count`. Evidence locations map the LC_MAIN entrypoint and nonempty file-backed segment
ranges to container offsets by adding the observed slice offset. Zero-fill
segments do not produce file ranges. When the selected slice's container offset
is unknown, those mapped locations are omitted with an explicit limitation;
diff --git a/src/native/parsers/otool.ts b/src/native/parsers/otool.ts
index 10804b126..d4e9f59ff 100644
--- a/src/native/parsers/otool.ts
+++ b/src/native/parsers/otool.ts
@@ -280,6 +280,7 @@ const parseLoadCommand = (block: string, state: ParsedLoadCommands): void => {
if (kind === null) throw new TypeError("otool load command is missing cmd");
for (const key of scalarFields(kind)) requireUniqueField(parsed, key);
if (DYLIB_COMMANDS.has(kind)) validateDylibCommandLines(parsed);
+ if (kind === "LC_LINKER_OPTION") validateLinkerOptionLines(parsed);
state.commands.push({
index,
kind,
@@ -294,7 +295,7 @@ const parseLoadCommand = (block: string, state: ParsedLoadCommands): void => {
const validateDylibCommandLines = (parsed: ParsedFields): void => {
const encodingFields = DYLIB_ENCODING_FIELDS.filter((key) =>
- Object.hasOwn(parsed.values, key),
+ parsed.rows.some((field) => field?.[0] === key),
);
if (encodingFields.length !== 1)
throw new TypeError(
@@ -311,9 +312,24 @@ const validateDylibCommandLines = (parsed: ParsedFields): void => {
}
};
+/** Each declared option appears once, in order, as `string #1..count`. */
+const validateLinkerOptionLines = (parsed: ParsedFields): void => {
+ const count = numberField(parsed.values, "count");
+ const labels = parsed.rows
+ .map((field) => field?.[0])
+ .filter((key) => key !== "cmd" && key !== "cmdsize" && key !== "count");
+ if (
+ count === null ||
+ labels.length !== count ||
+ labels.some((key, index) => key !== `string #${String(index + 1)}`)
+ )
+ throw new TypeError("otool linker-option strings do not match count");
+};
+
const scalarFields = (kind: string): readonly string[] => {
const common = ["cmd", "cmdsize"];
if (DYLIB_COMMANDS.has(kind)) return DYLIB_FIELDS;
+ if (kind === "LC_LINKER_OPTION") return [...common, "count"];
if (kind === "LC_LOAD_DYLINKER" || kind === "LC_ID_DYLINKER")
return [...common, "name"];
if (kind === "LC_UUID") return [...common, "uuid"];
@@ -420,24 +436,41 @@ interface ParsedFields {
readonly rows: readonly (readonly [string, string] | undefined)[];
}
+/**
+ * A key otool prints more than once, such as each LC_BUILD_VERSION tool, is
+ * labeled `key #n` in print order so no row overwrites another. Linker-option
+ * strings keep otool's own `string #n` label at every count.
+ */
const parseFields = (block: string): ParsedFields => {
- const fields: Record<string, string | number | null> = {};
const rows: Array<readonly [string, string] | undefined> = [];
- for (const rawLine of block.split(/\r?\n/u).slice(1)) {
- if (rawLine.trim().length === 0) continue;
- const field = parseField(rawLine);
- rows.push(field);
+ for (const rawLine of block.split(/\r?\n/u).slice(1))
+ if (rawLine.trim().length > 0) rows.push(parseField(rawLine));
+ const occurrences = new Map<string, number>();
+ for (const field of rows)
+ if (field !== undefined)
+ occurrences.set(field[0], (occurrences.get(field[0]) ?? 0) + 1);
+ const fields: Record<string, string | number | null> = {};
+ const ordinals = new Map<string, number>();
+ for (const field of rows) {
if (field === undefined) continue;
const [key, value] = field;
- fields[key] =
- key === "segname" || key === "sectname"
- ? value
- : (numeric(value) ?? value);
+ const ordinal = (ordinals.get(key) ?? 0) + 1;
+ ordinals.set(key, ordinal);
+ const label =
+ (occurrences.get(key) ?? 0) > 1 ? `${key} #${String(ordinal)}` : key;
+ fields[label] = TEXT_FIELDS.test(key) ? value : (numeric(value) ?? value);
}
return { values: fields, rows };
};
+/** Names and linker options are text even when they look numeric. */
+const TEXT_FIELDS = /^(?:segname|sectname|string #\d+)$/u;
+
const parseField = (rawLine: string): readonly [string, string] | undefined => {
+ // otool prints each linker option as `string #n <option>`; keep the option
+ // exactly, including surrounding spaces, under otool's ordinal label.
+ const option = /^\s*(string #[1-9]\d*)(?: (.*))?$/u.exec(rawLine);
+ if (option?.[1] !== undefined) return [option[1], option[2] ?? ""];
const line = rawLine.trim();
// Match time stamp as a whole without swallowing `name stamp plugin.dylib`.
const match = /^((?:time stamp)|\S+(?:\s+version)?)(?:\s+(.+))?$/u.exec(line);
diff --git a/tests/boundary/cli/nativeMachoLinkerOptionsCli.test.ts b/tests/boundary/cli/nativeMachoLinkerOptionsCli.test.ts
new file mode 100644
index 000000000..dd1631887
--- /dev/null
+++ b/tests/boundary/cli/nativeMachoLinkerOptionsCli.test.ts
@@ -0,0 +1,68 @@
+import { join } from "node:path";
+import { writeFile } from "node:fs/promises";
+
+import { describe, expect } from "vitest";
+import { z } from "zod";
+
+import { inspectMachoSchema } from "../../../src/domain/native/nativeInspection.js";
+import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
+import { cliTest } from "../../support/cli/cliFixture.js";
+
+const evidenceSchema = z.object({ normalized_result: inspectMachoSchema });
+
+describe.skipIf(process.platform !== "darwin")(
+ "native Mach-O CLI linker options",
+ () => {
+ cliTest(
+ "retains every compiler-recorded linker option string in order",
+ async ({ cli, processes }) => {
+ const directory = await createTestTempDirectory("rea-linker-option-");
+ const source = join(directory, "options.c");
+ const object = join(directory, "options.o");
+ await writeFile(
+ source,
+ [
+ '__asm__(".linker_option \\"-framework\\", \\"Foundation\\"");',
+ '__asm__(".linker_option \\"-lz\\"");',
+ "int sample(void) { return 7; }",
+ "",
+ ].join("\n"),
+ );
+ const compiled = await processes.run(
+ "/usr/bin/xcrun",
+ ["clang", "-c", source, "-o", object],
+ { timeoutMs: 30_000 },
+ );
+ expect(compiled.exitCode, compiled.stderr).toBe(0);
+ const result = await cli.run({
+ arguments: ["inspect-macho", object, "--json"],
+ environment: { REA_LOG_LEVEL: "silent" },
+ });
+ expect(result.exitCode).toBe(0);
+ const { load_commands: loadCommands } = evidenceSchema.parse(
+ result.json,
+ ).normalized_result;
+ expect(loadCommands.exhaustive).toBe(true);
+ expect(
+ loadCommands.items
+ .filter((command) => command.kind === "LC_LINKER_OPTION")
+ .map(({ fields }) => fields),
+ ).toEqual([
+ {
+ cmd: "LC_LINKER_OPTION",
+ cmdsize: 40,
+ count: 2,
+ "string #1": "-framework",
+ "string #2": "Foundation",
+ },
+ {
+ cmd: "LC_LINKER_OPTION",
+ cmdsize: 16,
+ count: 1,
+ "string #1": "-lz",
+ },
+ ]);
+ },
+ );
+ },
+);
diff --git a/tests/boundary/providers/native/nativeLoadKinds.golden.test.ts b/tests/boundary/providers/native/nativeLoadKinds.golden.test.ts
index dd83a1b3c..3d744fbd8 100644
--- a/tests/boundary/providers/native/nativeLoadKinds.golden.test.ts
+++ b/tests/boundary/providers/native/nativeLoadKinds.golden.test.ts
@@ -282,7 +282,67 @@ it("names numeric build-version platforms and tools", () => {
tools: [{ name: "SWIFT", version: "5.9" }],
},
]);
- expect(load.commands[0]?.fields.platform).toBe(1);
+ expect(load.commands[0]?.fields).toEqual({
+ cmd: "LC_BUILD_VERSION",
+ cmdsize: 48,
+ platform: 1,
+ minos: "11.0",
+ sdk: "26.5",
+ ntools: 2,
+ "tool #1": 3,
+ "version #1": "1267.0",
+ "tool #2": 1029,
+ "version #2": "1.0",
+ });
+ expect(load.commands[2]?.fields).toMatchObject({
+ tool: "SWIFT",
+ version: "5.9",
+ });
+});
+
+// Captured from `otool -l` on a clang `-c` object declaring
+// `.linker_option "-framework", "Foundation"`.
+const linkerOption = (strings: readonly string[], count = strings.length) =>
+ withOtoolMachHeader(
+ [
+ "Load command 0",
+ " cmd LC_LINKER_OPTION",
+ " cmdsize 40",
+ ` count ${String(count)}`,
+ ...strings.map((line) => ` string ${line}`),
+ "",
+ ].join("\n"),
+ );
+
+it("retains every linker-option string exactly and in order", () => {
+ expect(
+ parseOtoolLoadCommands(
+ linkerOption(["#1 -framework", "#2 Foundation", "#3 -lx ", "#4 1"]),
+ ).commands[0]?.fields,
+ ).toEqual({
+ cmd: "LC_LINKER_OPTION",
+ cmdsize: 40,
+ count: 4,
+ "string #1": "-framework",
+ "string #2": "Foundation",
+ "string #3": " -lx ",
+ "string #4": "1",
+ });
+});
+
+it.each([
+ ["fewer strings than count", linkerOption(["#1 -framework"], 2)],
+ ["more strings than count", linkerOption(["#1 -lz", "#2 -lm"], 1)],
+ ["an out-of-order ordinal", linkerOption(["#2 Foundation", "#1 -lz"])],
+ ["a repeated ordinal", linkerOption(["#1 -lz", "#1 -lm"])],
+ [
+ "an unlabeled row",
+ linkerOption(["#1 -lz", "#2 -lm"]).replace("string #2", "string"),
+ ],
+])("rejects linker options with %s", (_name, raw) => {
+ expect(() => parseOtoolLoadCommands(raw)).toThrow(
+ "otool linker-option strings do not match count",
+ );
});
it("does not classify LC_LOAD_DYLINKER as a dylib dependency", async () => {