Skip to content

armgit / rea

Public Imported from GitHub
0

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

fix(elf): follow loader version index precedence

morluto642f6571ea9d
diff --git a/docs/binary-diagnostics.md b/docs/binary-diagnostics.md
index 53e10642a..84c4abe3b 100644
--- a/docs/binary-diagnostics.md
+++ b/docs/binary-diagnostics.md
@@ -334,11 +334,12 @@ count and SHA-256.
`.gnu.version_r`, and the `hidden` flag from `.gnu.version`. `"unknown"`
means the symbol-version table could not be read. A hidden version is not
the default version that a by-name `dlsym` lookup selects.
+ Definition names take precedence when both tables name the same index.
Unreadable names, out-of-range name ordinals and unreadable forwarders are
listed in `issues`, their entries are not reported, and coverage is `partial`.
Unresolved symbol versions keep their rows: the version name is null when no
-unambiguous definition or requirement names the index, and `version` is
+definition or requirement names the index, and `version` is
`"unknown"` when the version table cannot be read; coverage is likewise
`partial`. A dynamic symbol table without its null entry is malformed. An ELF
without section headers is `unsupported`:
diff --git a/src/native/exports/NativeExportParser.test.ts b/src/native/exports/NativeExportParser.test.ts
index c8d62d383..1237dc438 100644
--- a/src/native/exports/NativeExportParser.test.ts
+++ b/src/native/exports/NativeExportParser.test.ts
@@ -340,7 +340,7 @@ describe("ELF version requirements", () => {
});
});
- it("keeps conflicting names for one version index unknown", () => {
+ it("uses a definition name when a requirement shares its index", () => {
const result = parse(
elfExportImage({
symbols: [{ name: "ambiguous", version: 2 }],
@@ -350,10 +350,13 @@ describe("ELF version requirements", () => {
);
expect(result.exports).toMatchObject([
- { name: "ambiguous", version: { index: 2, name: null, hidden: false } },
+ {
+ name: "ambiguous",
+ version: { index: 2, name: "DEFINITION", hidden: false },
+ },
]);
- expect(result.coverage.status).toBe("partial");
- expect(result.issues).toMatchObject([{ code: "version_unresolved" }]);
+ expect(result.coverage.status).toBe("complete");
+ expect(result.issues).toEqual([]);
});
it("retains exports and reports a truncated version-requirement chain", () => {
diff --git a/src/native/exports/NativeExportParser.ts b/src/native/exports/NativeExportParser.ts
index e127d45fc..d2d3eb9c5 100644
--- a/src/native/exports/NativeExportParser.ts
+++ b/src/native/exports/NativeExportParser.ts
@@ -290,63 +290,66 @@ const readVersionNames = (
): Map<number, string | null> => {
const namesByIndex = new Map<number, string | null>();
const addName = (index: number, name: string): void => {
- const previous = namesByIndex.get(index);
- if (previous === undefined) namesByIndex.set(index, name || null);
- else if (previous !== name) namesByIndex.set(index, null);
+ namesByIndex.set(index, name || null);
};
- const verdef = sections.find(({ type }) => type === SHT_GNU_VERDEF);
- if (verdef !== undefined) {
+ const verneed = sections.find(({ type }) => type === SHT_GNU_VERNEED);
+ if (verneed !== undefined) {
try {
const { table, names } = versionSectionData(
reader,
sections,
- verdef,
- "Version definitions",
+ verneed,
+ "Version requirements",
);
let offset = 0;
- for (let index = 0; index < verdef.info; index++) {
- if (offset + 20 > table.size)
+ for (let record = 0; record < verneed.info; record++) {
+ if (offset + 16 > table.size)
throw new BinaryFormatFailure(
"malformed",
- "Version definition chain leaves its section",
+ "Version requirement chain leaves its section",
);
const at = table.offset + offset;
- const versionIndex = reader.u16(at + 4) & 0x7fff;
- const aux = reader.u32(at + 12);
- const next = reader.u32(at + 16);
- const auxiliaryCount = reader.u16(at + 6);
- if (auxiliaryCount === 0 || aux < 20 || offset + aux + 8 > table.size)
+ const auxiliaryCount = reader.u16(at + 2);
+ const auxiliaryOffset = reader.u32(at + 8);
+ const next = reader.u32(at + 12);
+ if (auxiliaryCount > 0 && auxiliaryOffset < 16)
throw new BinaryFormatFailure(
"malformed",
- "Version definition name entry leaves its section",
+ "Version requirement auxiliary chain starts inside its header",
);
- let auxiliary = offset + aux;
- const versionName = readElfString(
- names,
- reader.u32(table.offset + auxiliary),
- );
- addName(versionIndex, versionName);
+ let auxiliary = offset + auxiliaryOffset;
for (let item = 0; item < auxiliaryCount; item++) {
- if (auxiliary + 8 > table.size)
+ if (auxiliary + 16 > table.size)
throw new BinaryFormatFailure(
"malformed",
- "Version definition auxiliary chain leaves its section",
+ "Version requirement auxiliary chain leaves its section",
);
+ const auxAt = table.offset + auxiliary;
+ const versionIndex = reader.u16(auxAt + 6) & 0x7fff;
+ const auxiliaryNext = reader.u32(auxAt + 12);
+ try {
+ addName(versionIndex, readElfString(names, reader.u32(auxAt + 8)));
+ } catch (cause) {
+ issues.push({
+ code: "version_unresolved",
+ location: hex(verneed.offset + BigInt(auxiliary)),
+ message: `Version requirement name: ${failureMessage(cause)}`,
+ });
+ }
if (item + 1 < auxiliaryCount) {
- const auxiliaryNext = reader.u32(table.offset + auxiliary + 4);
- if (auxiliaryNext < 8)
+ if (auxiliaryNext < 16)
throw new BinaryFormatFailure(
"malformed",
- "Version definition auxiliary chain ends before its declared count",
+ "Version requirement auxiliary chain ends before its declared count",
);
auxiliary += auxiliaryNext;
}
}
- if (index + 1 < verdef.info) {
- if (next < 20)
+ if (record + 1 < verneed.info) {
+ if (next < 16)
throw new BinaryFormatFailure(
"malformed",
- "Version definition chain ends before its declared count",
+ "Version requirement chain ends before its declared count",
);
offset += next;
}
@@ -354,70 +357,65 @@ const readVersionNames = (
} catch (cause) {
issues.push({
code: "version_unresolved",
- location: hex(verdef.offset),
- message: `Version definitions: ${failureMessage(cause)}`,
+ location: hex(verneed.offset),
+ message: `Version requirements: ${failureMessage(cause)}`,
});
}
}
-
- const verneed = sections.find(({ type }) => type === SHT_GNU_VERNEED);
- if (verneed !== undefined) {
+ // GNU lookup installs requirement names first, then definition names.
+ const verdef = sections.find(({ type }) => type === SHT_GNU_VERDEF);
+ if (verdef !== undefined) {
try {
const { table, names } = versionSectionData(
reader,
sections,
- verneed,
- "Version requirements",
+ verdef,
+ "Version definitions",
);
let offset = 0;
- for (let record = 0; record < verneed.info; record++) {
- if (offset + 16 > table.size)
+ for (let index = 0; index < verdef.info; index++) {
+ if (offset + 20 > table.size)
throw new BinaryFormatFailure(
"malformed",
- "Version requirement chain leaves its section",
+ "Version definition chain leaves its section",
);
const at = table.offset + offset;
- const auxiliaryCount = reader.u16(at + 2);
- const auxiliaryOffset = reader.u32(at + 8);
- const next = reader.u32(at + 12);
- if (auxiliaryCount > 0 && auxiliaryOffset < 16)
+ const versionIndex = reader.u16(at + 4) & 0x7fff;
+ const aux = reader.u32(at + 12);
+ const next = reader.u32(at + 16);
+ const auxiliaryCount = reader.u16(at + 6);
+ if (auxiliaryCount === 0 || aux < 20 || offset + aux + 8 > table.size)
throw new BinaryFormatFailure(
"malformed",
- "Version requirement auxiliary chain starts inside its header",
+ "Version definition name entry leaves its section",
);
- let auxiliary = offset + auxiliaryOffset;
+ let auxiliary = offset + aux;
+ const versionName = readElfString(
+ names,
+ reader.u32(table.offset + auxiliary),
+ );
+ addName(versionIndex, versionName);
for (let item = 0; item < auxiliaryCount; item++) {
- if (auxiliary + 16 > table.size)
+ if (auxiliary + 8 > table.size)
throw new BinaryFormatFailure(
"malformed",
- "Version requirement auxiliary chain leaves its section",
+ "Version definition auxiliary chain leaves its section",
);
- const auxAt = table.offset + auxiliary;
- const versionIndex = reader.u16(auxAt + 6) & 0x7fff;
- const auxiliaryNext = reader.u32(auxAt + 12);
- try {
- addName(versionIndex, readElfString(names, reader.u32(auxAt + 8)));
- } catch (cause) {
- issues.push({
- code: "version_unresolved",
- location: hex(verneed.offset + BigInt(auxiliary)),
- message: `Version requirement name: ${failureMessage(cause)}`,
- });
- }
if (item + 1 < auxiliaryCount) {
- if (auxiliaryNext < 16)
+ const auxiliaryNext = reader.u32(table.offset + auxiliary + 4);
+ if (auxiliaryNext < 8)
throw new BinaryFormatFailure(
"malformed",
- "Version requirement auxiliary chain ends before its declared count",
+ "Version definition auxiliary chain ends before its declared count",
);
auxiliary += auxiliaryNext;
}
}
- if (record + 1 < verneed.info) {
- if (next < 16)
+ if (index + 1 < verdef.info) {
+ if (next < 20)
throw new BinaryFormatFailure(
"malformed",
- "Version requirement chain ends before its declared count",
+ "Version definition chain ends before its declared count",
);
offset += next;
}
@@ -425,11 +423,12 @@ const readVersionNames = (
} catch (cause) {
issues.push({
code: "version_unresolved",
- location: hex(verneed.offset),
- message: `Version requirements: ${failureMessage(cause)}`,
+ location: hex(verdef.offset),
+ message: `Version definitions: ${failureMessage(cause)}`,
});
}
}
+
return namesByIndex;
};