refactor(elf): share version table admission (#15514)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-12-01 15:53:40 +00:00
1 parent 6ac87697e7
commit 8535d82158
1 file changed
+82 -94
+82 -94
View File
@@ -794,29 +794,79 @@ const readGnuHashCount = (read: HashWordStepReader, bits: 32 | 64) =>
read,
);
const locateVersionTable = (
const prepareVersionTable = (
mappings: IndexedElfLoadMappings,
fileBytes: number,
section: SectionFacts | undefined,
address: bigint | undefined,
label: string,
declaredCount: number | undefined,
dynamicOffset: number,
kind: "requirements" | "definitions",
issues: ElfDependencyIssue[],
): LocatedTable | null => {
const fromSection = tableFromSection(section, fileBytes, label, issues);
if (fromSection !== null) return fromSection;
if (address === undefined) return null;
const offset = mapAddress(mappings, address, 16);
if (offset === null) return null;
// Without a section header the table extent is unknown; keep every later
// record read inside the PT_LOAD mapping that admitted the table start.
const end = startingLoadFileEnd(mappings, address);
if (end === null) return null;
const prefix = mappings
.index()
.prefix(address, Math.min(end, fileBytes) - offset);
if (prefix === null) return null;
const { size } = prefix;
return { offset, size, location: { offset, bytes: size } };
):
| {
readonly read: true;
readonly table: LocatedTable;
readonly count: number;
}
| ({ readonly read: false } & Pick<
ElfDependencies["version_requirements"],
"status" | "location"
>) => {
const label = `GNU version ${kind}`;
const tag = kind === "requirements" ? "VERNEED" : "VERDEF";
const tableFromAddress = (): LocatedTable | null => {
if (address === undefined) return null;
const offset = mapAddress(mappings, address, 16);
if (offset === null) return null;
// Without a section header the table extent is unknown; keep every later
// record read inside the PT_LOAD mapping that admitted the table start.
const end = startingLoadFileEnd(mappings, address);
if (end === null) return null;
const prefix = mappings
.index()
.prefix(address, Math.min(end, fileBytes) - offset);
if (prefix === null) return null;
const { size } = prefix;
return { offset, size, location: { offset, bytes: size } };
};
const table =
tableFromSection(section, fileBytes, label, issues) ?? tableFromAddress();
if (table === null) {
const declaresRecords = declaredCount !== undefined && declaredCount > 0;
if (address !== undefined)
issues.push({
code: "table_unavailable",
location: hex(address),
message: `${label} could not be located`,
});
else if (section === undefined && declaresRecords)
issues.push({
code: "table_unavailable",
location: hex(dynamicOffset),
message: `${label} declare DT_${tag}NUM without DT_${tag} or SHT_GNU_${tag}`,
});
return {
read: false,
status:
address === undefined && section === undefined && !declaresRecords
? "absent"
: "unreadable",
location: null,
};
}
const count = declaredCount ?? section?.info;
if (count === undefined) {
issues.push({
code: "table_unavailable",
location: hex(table.offset),
message: `${label} have no DT_${tag}NUM or section record count`,
});
return { read: false, status: "unreadable", location: table.location };
}
if (count === 0)
return { read: false, status: "present", location: table.location };
return { read: true, table, count };
};
const emptyInventory = (
@@ -1601,50 +1651,19 @@ const parseVerneed = function* (
versionNames: Map<number, { name: string | null; file: string | null }>,
chargeVersion: () => void,
): ElfDependencySteps<ElfDependencies["version_requirements"]> {
const table = locateVersionTable(
const prepared = prepareVersionTable(
mappings,
window.fileSize,
section,
address,
"GNU version requirements",
declaredCount,
dynamicOffset,
"requirements",
issues,
);
if (table === null) {
const declaresRecords = declaredCount !== undefined && declaredCount > 0;
if (address !== undefined)
issues.push({
code: "table_unavailable",
location: hex(address),
message: "GNU version requirements could not be located",
});
else if (section === undefined && declaresRecords)
issues.push({
code: "table_unavailable",
location: hex(dynamicOffset),
message:
"GNU version requirements declare DT_VERNEEDNUM without DT_VERNEED or SHT_GNU_VERNEED",
});
return {
status:
address === undefined && section === undefined && !declaresRecords
? "absent"
: "unreadable",
location: null,
files: [],
};
}
const count = declaredCount ?? section?.info;
if (count === undefined) {
issues.push({
code: "table_unavailable",
location: hex(table.offset),
message:
"GNU version requirements have no DT_VERNEEDNUM or section record count",
});
return { status: "unreadable", location: table.location, files: [] };
}
if (count === 0)
return { status: "present", location: table.location, files: [] };
if (!prepared.read)
return { status: prepared.status, location: prepared.location, files: [] };
const { table, count } = prepared;
const files: ElfVersionNeedFile[] = [];
const conflicts = new Set<number>();
const settleConflicts = () => {
@@ -1757,50 +1776,19 @@ const parseVerdef = function* (
issues: ElfDependencyIssue[],
chargeVersion: () => void,
): ElfDependencySteps<ElfDependencies["version_definitions"]> {
const table = locateVersionTable(
const prepared = prepareVersionTable(
mappings,
window.fileSize,
section,
address,
"GNU version definitions",
declaredCount,
dynamicOffset,
"definitions",
issues,
);
if (table === null) {
const declaresRecords = declaredCount !== undefined && declaredCount > 0;
if (address !== undefined)
issues.push({
code: "table_unavailable",
location: hex(address),
message: "GNU version definitions could not be located",
});
else if (section === undefined && declaresRecords)
issues.push({
code: "table_unavailable",
location: hex(dynamicOffset),
message:
"GNU version definitions declare DT_VERDEFNUM without DT_VERDEF or SHT_GNU_VERDEF",
});
return {
status:
address === undefined && section === undefined && !declaresRecords
? "absent"
: "unreadable",
location: null,
names: [],
};
}
const count = declaredCount ?? section?.info;
if (count === undefined) {
issues.push({
code: "table_unavailable",
location: hex(table.offset),
message:
"GNU version definitions have no DT_VERDEFNUM or section record count",
});
return { status: "unreadable", location: table.location, names: [] };
}
if (count === 0)
return { status: "present", location: table.location, names: [] };
if (!prepared.read)
return { status: prepared.status, location: prepared.location, names: [] };
const { table, count } = prepared;
const names: ElfVersionDefinition[] = [];
try {
let offset = 0;