refactor(elf): share fixed symbol record widths (#12379)

This commit is contained in:
morluto authored and GitHub committed 2026-11-19 15:28:37 +00:00
1 parent 0eac37277d
commit 205ab4a587
10 files changed
+37 -31

No files matched your search

@@ -18,7 +18,6 @@
"src/domain/elfRawTableDeclarations.ts": 7,
"src/domain/elfRelocation.ts": 6,
"src/domain/elfSectionHeader.ts": 6,
"src/domain/elfSymbol.ts": 8,
"src/domain/gamecubeDol.ts": 6,
"src/domain/inesContainer.ts": 5,
"src/domain/managed/managedCliHeaderPrefix.ts": 5,
+19 -12
View File
@@ -1,5 +1,10 @@
import { BinaryReader } from "./binaryContainer.js";
import { err, ok } from "./result.js";
/** Wire widths of the fixed Elf32_Sym and Elf64_Sym prefixes. */
export const elfSymbolSize = (bits: 32 | 64): 16 | 24 =>
bits === 64 ? 24 : 16;
/** Fixed ELF32/ELF64 symbol facts; consumers own definition, visibility and processor-flag policies.
* section_index is the raw st_shndx field. Use readElfSymbolSectionReference
* before treating it as a directory index: SHN_XINDEX requires a companion.
@@ -10,24 +15,26 @@ export const readElfSymbol = (
littleEndian: boolean,
) => {
const wide = bits === 64;
if (bytes.byteLength < (wide ? 24 : 16))
if (bytes.byteLength < elfSymbolSize(bits))
return err({
kind: "malformed" as const,
message: "ELF symbol record is truncated",
});
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
const info = view.getUint8(wide ? 4 : 12);
const reader = new BinaryReader(
Buffer.isBuffer(bytes)
? bytes
: Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength),
littleEndian,
"elf",
);
const info = reader.u8(wide ? 4 : 12);
return ok({
name_offset: view.getUint32(0, littleEndian),
name_offset: reader.u32(0),
binding: info >>> 4,
type: info & 15,
other: view.getUint8(wide ? 5 : 13),
section_index: view.getUint16(wide ? 6 : 14, littleEndian),
value: wide
? view.getBigUint64(8, littleEndian)
: BigInt(view.getUint32(4, littleEndian)),
size: wide
? view.getBigUint64(16, littleEndian)
: BigInt(view.getUint32(8, littleEndian)),
other: reader.u8(wide ? 5 : 13),
section_index: reader.u16(wide ? 6 : 14),
value: reader.word(wide ? 8 : 4, bits),
size: reader.word(wide ? 16 : 8, bits),
});
};
+2 -2
View File
@@ -5,7 +5,7 @@ import {
readElfRelocationEntry,
} from "../elfRelocation.js";
import { readLeb128, SLEB64 } from "../leb128.js";
import { readElfSymbol } from "../elfSymbol.js";
import { elfSymbolSize, readElfSymbol } from "../elfSymbol.js";
import {
readElfHashSymbolCount,
readElfGnuHashSymbolCount,
@@ -661,7 +661,7 @@ export function* readElfRelocationWritesSteps(
value === null ? fileOffset(address, width) : target(address, width),
...record,
});
const symbolBytes = input.bits === 64 ? 24 : 16;
const symbolBytes = elfSymbolSize(input.bits);
const symbolTable =
tags.get(DT_SYMENT) === BigInt(symbolBytes)
? tags.get(DT_SYMTAB)
+2 -2
View File
@@ -10,7 +10,7 @@ import {
} from "../binaryContainer.js";
import { architectureForElfMachine } from "./elfImage.js";
import { readElfStringBytes } from "./elfStringBytes.js";
import { readElfSymbol } from "../elfSymbol.js";
import { elfSymbolSize, readElfSymbol } from "../elfSymbol.js";
import {
readElfDirectoryFromBuffer,
type NativeSection,
@@ -721,7 +721,7 @@ function* parseElfExports(
...ELF_LIMITATIONS,
],
};
const width = directory.bits === 64 ? 24 : 16;
const width = elfSymbolSize(directory.bits);
const strings = sections[dynsym.link];
const compressed = dynsym.compressed
? dynsym
+2 -2
View File
@@ -13,7 +13,7 @@ import {
} from "../domain/elfHashSymbolCount.js";
import { readElfSectionHeader } from "../domain/elfSectionHeader.js";
import { readElfProgramHeader } from "../domain/elfProgramHeader.js";
import { readElfSymbol } from "../domain/elfSymbol.js";
import { elfSymbolSize, readElfSymbol } from "../domain/elfSymbol.js";
import { readElfRawTableDeclarations } from "../domain/elfRawTableDeclarations.js";
import { validateElfSectionTableFields } from "../domain/elfDeclaredRanges.js";
import { Buffer } from "node:buffer";
@@ -216,7 +216,7 @@ export const readElfSymbols = async (
e_phnum,
} = parsed.header;
const reader = new BoundedReader(bytes);
const entrySize = is64 ? 24 : 16;
const entrySize = elfSymbolSize(is64 ? 64 : 32);
const segments: { vaddr: number; offset: number; filesz: number }[] = [];
let dynamicSegment: { offset: number; filesz: number } | null = null;
for (let index = 0; index < e_phnum; index += 1) {
+2 -2
View File
@@ -28,7 +28,7 @@ import {
type NativeSectionDirectory,
} from "../domain/native/nativeSectionDirectory.js";
import { readElfStringBytes } from "../domain/native/elfStringBytes.js";
import { readElfSymbol } from "../domain/elfSymbol.js";
import { elfSymbolSize, readElfSymbol } from "../domain/elfSymbol.js";
import {
ELF_SHN_XINDEX,
ElfSymbolSectionIndexTables,
@@ -276,7 +276,7 @@ const cudaKernels = async (
if (section.type !== 2 && section.type !== 11) continue;
symbolTables++;
const width = gpuCount(section.entry_size),
minimum = directory.bits === 64 ? 24 : 16;
minimum = elfSymbolSize(directory.bits);
if (width < minimum || section.size % BigInt(width) !== 0n)
rejectGpu("CUBIN symbol table width is invalid");
const namesSection = directory.sections[section.link];
+2 -2
View File
@@ -29,7 +29,7 @@ import {
elfStringTableText,
observeElfStringTableEntry,
} from "../../domain/elfStringTable.js";
import { readElfSymbol } from "../../domain/elfSymbol.js";
import { elfSymbolSize, readElfSymbol } from "../../domain/elfSymbol.js";
import {
readElfGnuHashSymbolCount,
readElfHashSymbolCount,
@@ -982,7 +982,7 @@ export const readElfDependenciesSteps = function* (
);
// Elf32_Sym and Elf64_Sym have fixed widths, so a DT_SYMENT or .dynsym
// sh_entsize that disagrees is reported and never used to stride the table.
const symbolEntrySize = bits === 64 ? 24 : 16;
const symbolEntrySize = elfSymbolSize(bits);
if (syment !== undefined && syment !== symbolEntrySize)
issues.push({
code: "malformed_entry",
+2 -2
View File
@@ -8,7 +8,7 @@ import {
import { assertArtifactReadActive } from "../../resources/index.js";
import { decodeLimit } from "../../domain/parseFailure.js";
import { readElfDynamicEntry } from "../../domain/elfDynamicEntry.js";
import { readElfSymbol } from "../../domain/elfSymbol.js";
import { elfSymbolSize, readElfSymbol } from "../../domain/elfSymbol.js";
import {
elfStringTableText,
observeElfStringTableEntry,
@@ -1805,7 +1805,7 @@ const scanSymbols = (
const tables = directory.sections.filter(
(section) => section.type === SHT_SYMTAB || section.type === SHT_DYNSYM,
);
const standard = directory.bits === 64 ? 24 : 16;
const standard = elfSymbolSize(directory.bits);
// First section per index, matching a directory-order find.
const byIndex = new Map<number, NativeSection>();
for (const section of directory.sections)
+3 -3
View File
@@ -24,7 +24,7 @@ import {
} from "../../domain/elfDynamicEntry.js";
import { elfArchitecture } from "../../domain/elfFixedHeader.js";
import { admitElfLoadSegment } from "../../domain/elfProgramHeader.js";
import { readElfSymbol } from "../../domain/elfSymbol.js";
import { elfSymbolSize, readElfSymbol } from "../../domain/elfSymbol.js";
import {
readElfGnuHashSymbolCount,
readElfHashSymbolCount,
@@ -505,7 +505,7 @@ const readSymbols = (
"An SHT_DYNSYM section exists, but no DT_SYMTAB declares it, so its names are not established as imports.",
names: [],
};
const width = bits === 64 ? 24 : 16;
const width = elfSymbolSize(bits);
const syment = dynamic.entries.get(DT_SYMENT);
if (syment !== undefined && syment.value !== BigInt(width))
malformed("ELF DT_SYMENT does not match the ELF class symbol width");
@@ -584,7 +584,7 @@ const readSymbolNames = (
strings: Buffer,
stringFileOffset: number,
): SymbolScan => {
const width = bits === 64 ? 24 : 16;
const width = elfSymbolSize(bits);
const names: SymbolName[] = [];
let scanned = 0;
let unterminated = false;
@@ -1,7 +1,7 @@
import { setImmediate as immediate } from "node:timers/promises";
import { assertArtifactReadActive } from "../../resources/index.js";
import { readElfRelocationEntry } from "../../domain/elfRelocation.js";
import { readElfSymbol } from "../../domain/elfSymbol.js";
import { elfSymbolSize, readElfSymbol } from "../../domain/elfSymbol.js";
import {
ELF_SHN_XINDEX,
ElfSymbolSectionIndexTables,
@@ -176,7 +176,7 @@ const readTable = (
readonly truncated: boolean;
readonly incomplete: boolean;
} => {
const width = facts.bits === 64 ? 24 : 16;
const width = elfSymbolSize(facts.bits);
const strings = stringTableBytes(facts.bytes, sections[section.link]);
const compressedStrings = sections[section.link]?.compressed === true;
if (compressedStrings)
@@ -320,7 +320,7 @@ export const loadElfSymbolIndex = (
unreadable = true;
return;
}
const width = BigInt(facts.bits === 64 ? 24 : 16);
const width = BigInt(elfSymbolSize(facts.bits));
if (section.entry_size !== 0n && section.entry_size !== width) {
notes.add(
"A symbol table has an unexpected entry size; its symbols were not decoded.",