All files / binary101/analyzers/dwarf attribute-values.ts

100% Statements 42/42
100% Branches 40/40
100% Functions 1/1
100% Lines 42/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 431x 1x 1x 1x 2129x 2129x 2129x 1x 1x 2347x 1x 1x 2143x 1x 1x 1x 282x 282x 282x 282x 1x 1x 295x 295x 293x 282x 282x 282x 282x 282x 282x 282x 282x 282x 282x 295x 295x 295x 295x 295x 295x 295x  
import type { DwarfDie, DwarfFormValue, DwarfUnit, DwarfUnitRoot } from "./types.js";
import { DWARF_ATTRIBUTE } from "./constants.js";
 
export const dwarfAttributeValue = (
  die: DwarfDie | undefined,
  name: number
): DwarfFormValue | undefined => die?.attributes.find(item => item.name === name)?.value;
 
export const dwarfNumericValue = (value: DwarfFormValue | undefined): bigint | null =>
  value?.kind === "unsigned" || value?.kind === "signed" ? value.value : null;
 
export const dwarfStringValue = (value: DwarfFormValue | undefined): string | null =>
  value?.kind === "string" ? value.value : null;
 
// A view for consumers of unit metadata; parsed units retain only canonical DIEs.
const rootLanguage = (root: DwarfDie): number | null => {
  const language = dwarfNumericValue(dwarfAttributeValue(root, DWARF_ATTRIBUTE.language));
  return language == null || language < 0n || language > BigInt(Number.MAX_SAFE_INTEGER)
    ? null : Number(language);
};
 
export const dwarfUnitRoot = (unit: DwarfUnit | undefined): DwarfUnitRoot | null => {
  const root = unit?.dies[0];
  if (!root) return null;
  const name = dwarfStringValue(dwarfAttributeValue(root, DWARF_ATTRIBUTE.name));
  const producer = dwarfStringValue(dwarfAttributeValue(root, DWARF_ATTRIBUTE.producer));
  const compilationDirectory = dwarfStringValue(
    dwarfAttributeValue(root, DWARF_ATTRIBUTE.compilationDirectory)
  );
  const language = rootLanguage(root);
  const statementListOffset = dwarfNumericValue(
    dwarfAttributeValue(root, DWARF_ATTRIBUTE.statementList)
  );
  return {
    tag: root.tag,
    ...(name == null ? {} : { name }),
    ...(producer == null ? {} : { producer }),
    ...(compilationDirectory == null ? {} : { compilationDirectory }),
    ...(language == null ? {} : { language }),
    ...(statementListOffset == null || statementListOffset < 0n ? {} : { statementListOffset })
  };
};