Files
reverse-engineering-anything/src/domain/elfHashSymbolCount.test.ts
T
Leo Camusandgensokyo-dev a140853e9f fix(elf): read eight-byte DT_HASH entries on 64-bit s390x and Alpha (#14630)
* fix(elf): read eight-byte DT_HASH entries on 64-bit s390x and Alpha

On ELF64 s390x and Alpha every SysV hash entry (nbucket, nchain, buckets
and chains) is eight bytes wide; binutils readelf and glibc read them that
way and ld writes .hash with sh_entsize 8. The shared DT_HASH walker always
read 32-bit words, so on a big-endian s390x table it took the high half of
nbucket as the bucket count and the low half as nchain.

On Debian wheezy's s390x libz.so.1.2.7 (nbucket 97, nchain 105) this made
inspect-elf-dependencies report a false ".dynsym section declares 105
symbols, past the DT_HASH dynamic symbol count of 97" issue with partial
coverage, and inspect-native-exports refuse the same library without
section headers because DT_HASH (97) and DT_GNU_HASH (105) disagreed.

The walker now takes the entry layout from e_machine, the ELF class and
the byte order, and the dependency parser and both PT_DYNAMIC export paths
pass it.

* test(elf): cover eight-byte DT_HASH reads from a file handle

The s390x and Alpha .hash fixture now declares sh_entsize 8 to match the
entries it writes, and the sectionless export cases also run through
parseNativeExportsFromHandle, including a wide header that crosses the end
of its load mapping.

---------

Co-authored-by: gensokyo-dev <gensokyo-dev@users.noreply.github.com>
2026-11-27 05:28:24 +08:00

126 lines
4.2 KiB
TypeScript

import { expect, it } from "vitest";
import {
readElfGnuHashSymbolCount,
readElfHashSymbolCount,
type ElfGnuHashCountWalk,
type ElfHashWordReader,
} from "./elfHashSymbolCount.js";
import { err, ok } from "./result.js";
const words =
(values: readonly number[]): ElfHashWordReader =>
(offset) => {
const value = values[offset / 4];
return value === undefined ? err("is outside the image") : ok(value);
};
const finish = (walk: ElfGnuHashCountWalk) => {
for (let step = walk.next(); ; step = walk.next())
if (step.done === true) return step.value;
};
/** nbuckets, symoffset, bloom words, shift, one 8-byte bloom word, buckets, chain. */
const gnuTable = (
buckets: readonly number[],
chain: readonly number[],
firstSymbol = 2,
) => [buckets.length, firstSymbol, 1, 0, 0, 0, ...buckets, ...chain];
it("reads nchain from a complete DT_HASH table", () => {
expect(readElfHashSymbolCount(words([1, 3, 0, 0, 0, 0]))).toEqual(ok(3));
expect(readElfHashSymbolCount(words([1]))).toEqual(
err({
kind: "malformed",
message: expect.stringMatching(/DT_HASH header.*outside/iu),
}),
);
expect(readElfHashSymbolCount(words([1, 3, 0]))).toEqual(
err({
kind: "malformed",
message: expect.stringMatching(/DT_HASH table.*outside/iu),
}),
);
expect(readElfHashSymbolCount(words([1, 9]), 8)).toEqual(
err({ kind: "budget", label: "DT_HASH symbol table", count: 9 }),
);
});
it("reads eight-byte DT_HASH entries in either byte order", () => {
const big = { bytes: 8, little: false } as const;
const little = { bytes: 8, little: true } as const;
expect(
readElfHashSymbolCount(words([0, 1, 0, 3, ...Array(8).fill(0)]), 8, big),
).toEqual(ok(3));
expect(
readElfHashSymbolCount(words([1, 0, 3, 0, ...Array(8).fill(0)]), 8, little),
).toEqual(ok(3));
expect(
readElfHashSymbolCount(words([0, 1, 0, 3, ...Array(7).fill(0)]), 8, big),
).toEqual(
err({
kind: "malformed",
message: expect.stringMatching(/DT_HASH table.*outside/iu),
}),
);
expect(
readElfHashSymbolCount(words([0, 1, 1, 3, ...Array(8).fill(0)]), 8, big),
).toEqual(
err({
kind: "malformed",
message: expect.stringMatching(/DT_HASH header.*32 bits/iu),
}),
);
});
it("follows the last DT_GNU_HASH chain to its end", () => {
expect(
finish(readElfGnuHashSymbolCount(words(gnuTable([2, 3], [0, 0, 1])), 8)),
).toEqual(ok(5));
expect(
finish(readElfGnuHashSymbolCount(words(gnuTable([0], [])), 8)),
).toEqual(ok(null));
const narrow = [1, 1, 1, 0, 0, 1, 1];
expect(finish(readElfGnuHashSymbolCount(words(narrow), 4))).toEqual(ok(2));
});
it("rejects DT_GNU_HASH tables that do not describe a chain", () => {
const malformed = (values: readonly number[], reason: RegExp) =>
expect(finish(readElfGnuHashSymbolCount(words(values), 8))).toEqual(
err({ kind: "malformed", message: expect.stringMatching(reason) }),
);
malformed([1, 2, 1], /header.*outside/iu);
malformed([0, 2, 1, 0], /buckets.*malformed/iu);
malformed([1, 2, 0, 0], /buckets.*malformed/iu);
malformed([1, 2, 1, 0, 0, 0], /buckets.*outside/iu);
malformed(gnuTable([1], [1]), /bucket.*precedes.*symbol/iu);
malformed(gnuTable([2], [0]), /chain.*outside/iu);
});
it("applies a caller budget before walking each table", () => {
const budget = (values: readonly number[], maximum: number) =>
finish(readElfGnuHashSymbolCount(words(values), 8, maximum));
expect(budget([9, 0, 1, 0], 8)).toEqual(
err({ kind: "budget", label: "DT_GNU_HASH bucket table", count: 9 }),
);
expect(budget([1, 0, 9, 0], 8)).toEqual(
err({ kind: "budget", label: "DT_GNU_HASH bloom filter", count: 9 }),
);
expect(budget([1, 9, 1, 0], 8)).toEqual(
err({ kind: "budget", label: "DT_GNU_HASH symbol index", count: 9 }),
);
expect(budget(gnuTable([2], [0, 0, 0, 1]), 3)).toEqual(
err({
kind: "budget",
label: "DT_GNU_HASH symbol table",
count: 4,
// The chain walk stops at the budget, so 4 is a lower bound.
lowerBound: true,
}),
);
});
it("yields between bounded steps so a driver can stop a long walk", () => {
const walk = readElfGnuHashSymbolCount(words(gnuTable([2], [1])), 8);
expect(walk.next().done).toBe(false);
});