All files / binary101/analyzers/dwarf indexed-tables.ts

100% Statements 255/255
95.34% Branches 164/172
100% Functions 22/22
100% Lines 255/255

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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 174x 1x 321x 321x 321x 321x 321x 321x 1x 1x 321x 321x 321x 321x 321x 321x 1x 1x 114x 114x 4x 4x 4x 114x 114x 114x 1x 1x 25x 25x 6x 25x 25x 25x 25x 1x 1x 47x 47x 47x 42x 18x 47x 1x 1x 18x 18x 14x 18x 1x 1x 38x 38x 38x 38x 1x 1x 89x 89x 89x 89x 1x 1x 88x 88x 88x 88x 88x 88x 88x 1x 1x 83x 83x 83x 83x 1x 1x 1x 83x 83x 83x 1x 1x 88x 88x 3x 88x 1x 1x 20x 19x 20x 20x 20x 20x 20x 20x 20x 1x 1x 20x 5x 5x 5x 19x 20x 1x 1x 8x 6x 6x 6x 2x 8x 1x 1x 27x 27x 27x 25x 9x 27x 8x 8x 27x 7x 7x 7x 27x 27x 27x 1x 1x 5x 1x 1x 1x 4x 5x 5x 5x 4x 5x 1x 1x 1x 4x 5x 1x 1x 8x 8x 8x 3x 3x 1x 1x 8x 8x 1x 1x 96x 96x 96x 96x 1x 1x 142x 142x 142x 1x 1x 44x 44x 44x 40x 42x 41x 41x 41x 36x 36x 42x 44x 1x  
import { DwarfCursor } from "./cursor.js";
import { dwarfAttributeValue, dwarfNumericValue } from "./attribute-values.js";
import { readDwarfInitialLength } from "./initial-length.js";
import { DWARF_ATTRIBUTE, DWARF_FORM } from "./constants.js";
import type { DwarfAttribute, DwarfSectionSource, DwarfUnit, DwarfUnitContext } from "./types.js";
 
type Contribution = {
  offset: number;
  end: number;
  entriesOffset: number;
  format: 32 | 64;
  addressSize: number;
  offsetCount: number;
};
 
// DWARF 5 sections 7.27-7.29: contribution headers and offset-table-relative indices.
// https://dwarfstd.org/doc/DWARF5.pdf
const readContribution = async (
  source: DwarfSectionSource, offset: number, byteOrder: "little" | "big", issues: string[]
): Promise<Contribution | null> => {
  const lengthCursor = new DwarfCursor(source.reader, source.section, offset,
    source.section.size, byteOrder === "little", issues);
  const initial = await readDwarfInitialLength(lengthCursor);
  if (!initial || initial.length === 0n) return null;
  if (initial.length > BigInt(lengthCursor.end - lengthCursor.position)) return null;
  const { format, end } = initial;
  const cursor = new DwarfCursor(source.reader, source.section, lengthCursor.position,
    end, byteOrder === "little", issues);
  const encoding = await readContributionEncoding(cursor, source.section.name);
  if (!encoding) return null;
  const offsetCount = source.section.name === ".debug_addr" || source.section.name === ".debug_str_offsets"
    ? 0 : await cursor.uint32();
  if (offsetCount == null || offsetCount > (cursor.end - cursor.position) / (format / 8)) {
    cursor.fail("Indexed offset table extends beyond its contribution");
    return null;
  }
  return { offset, end, entriesOffset: cursor.position, format, ...encoding, offsetCount };
};
 
const readStringOffsetsHeader = async (
  cursor: DwarfCursor, version: number
): Promise<{ addressSize: number } | null> => {
  const padding = await cursor.uint16();
  if (padding == null) return null;
  if (version !== 5 || padding !== 0) { cursor.fail("Invalid string offsets header"); return null; }
  return { addressSize: 0 };
};
 
const readContributionEncoding = async (cursor: DwarfCursor, name: string): Promise<{ addressSize: number } | null> => {
  const version = await cursor.uint16();
  if (version == null) return null;
  if (name === ".debug_str_offsets") return readStringOffsetsHeader(cursor, version);
  const addressSize = await cursor.uint8();
  const selectorSize = await cursor.uint8();
  if (addressSize == null || selectorSize == null) return null;
  return validIndexedHeader(cursor, version, addressSize, selectorSize) ? { addressSize } : null;
};
 
const validIndexedHeader = (
  cursor: DwarfCursor, version: number, addressSize: number, selectorSize: number
): boolean => {
  if (version === 5 && addressSize > 0 && selectorSize === 0) return true;
  cursor.fail(selectorSize ? "Segmented indexed addresses are unsupported" : "Invalid indexed table header");
  return false;
};
 
const validStringOffset = (table: Contribution, offset: bigint): boolean =>
  offset + BigInt(table.format / 8) <= BigInt(table.end);
 
export class DwarfIndexedReader {
  readonly #sections: Map<string, DwarfSectionSource>;
  readonly #byteOrder: "little" | "big";
  readonly #issues: string[];
  readonly #skeletons: DwarfUnit[];
  readonly #contributions = new Map<string, Promise<Contribution[]>>();
  readonly #addresses = new Map<string, Promise<bigint | null>>();
  readonly #stringOffsets = new Map<string, Promise<bigint | null>>();
 
  constructor(sections: Map<string, DwarfSectionSource>, byteOrder: "little" | "big",
    issues: string[], skeletons: DwarfUnit[] = []) {
    this.#sections = sections;
    this.#byteOrder = byteOrder;
    this.#issues = issues;
    this.#skeletons = skeletons;
  }
 
  cursor(name: string, offset: bigint, end?: number): DwarfCursor | null {
    const source = this.#sections.get(name);
    if (!source || offset < 0n || offset >= BigInt(source.section.size)) {
      this.#issues.push(`${name}: missing section or offset ${offset} outside the section.`);
      return null;
    }
    return new DwarfCursor(source.reader, source.section, Number(offset),
      end ?? source.section.size, this.#byteOrder === "little", this.#issues);
  }
 
  address(unit: DwarfUnit, index: bigint): Promise<bigint | null> {
    const owner = this.skeleton(unit) ?? unit;
    const base = dwarfNumericValue(dwarfAttributeValue(owner.dies[0], DWARF_ATTRIBUTE.addressBase)) ??
      dwarfNumericValue(dwarfAttributeValue(owner.dies[0], 0x2133)); // DW_AT_GNU_addr_base.
    const key = `${unit.version}:${unit.addressSize}:${base}:${index}`;
    if (!this.#addresses.has(key)) this.#addresses.set(key, this.#readAddress(unit, index, base));
    return this.#addresses.get(key)!;
  }
 
  skeleton(unit: DwarfUnit): DwarfUnit | null {
    if (!unit.sectionName.endsWith(".dwo")) return null;
    const id = unit.dwoId ?? dwarfNumericValue(dwarfAttributeValue(unit.dies[0], 0x2131));
    if (id == null) return null;
    return this.#skeletons.find(candidate => !candidate.sectionName.endsWith(".dwo") &&
      (candidate.dwoId ?? dwarfNumericValue(dwarfAttributeValue(candidate.dies[0], 0x2131))) === id) ?? null;
  }
 
  baseAddress(unit: DwarfUnit): bigint | null {
    const owner = this.skeleton(unit) ?? unit;
    return dwarfNumericValue(dwarfAttributeValue(owner.dies[0], DWARF_ATTRIBUTE.lowPc)) ??
      (unit.sectionName.endsWith(".dwo") && owner === unit ? null : 0n);
  }
 
  async splitStringOffsetsBase(version: number): Promise<bigint | null> {
    // DWO contributions have implicit bases: LLVM DWARFUnit.cpp, determineStringOffsetsTableContribution.
    if (version < 5) return 0n;
    return BigInt((await this.#tables(".debug_str_offsets"))[0]?.entriesOffset ?? -1);
  }
 
  stringOffset(context: DwarfUnitContext, index: bigint): Promise<bigint | null> {
    const key = `${context.version}:${context.format}:${context.stringOffsetsBase}:${index}`;
    if (!this.#stringOffsets.has(key)) this.#stringOffsets.set(key, this.#readStringOffset(context, index));
    return this.#stringOffsets.get(key)!;
  }
 
  async #readStringOffset(context: DwarfUnitContext, index: bigint): Promise<bigint | null> {
    const base = context.stringOffsetsBase;
    if (!this.#validStringIndex(base, index)) return null;
    const checkedBase = base!;
    const table = context.version >= 5 ? await this.#byBase(".debug_str_offsets", checkedBase) : null;
    if (context.version >= 5 && !table) return null;
    return this.#stringEntry(table, checkedBase, index, context.format);
  }
 
  async #stringEntry(table: Contribution | null, base: bigint,
    index: bigint, format: 32 | 64): Promise<bigint | null> {
    const width = (table?.format ?? format) / 8;
    const offset = base + index * BigInt(width);
    if (table && !validStringOffset(table, offset)) {
      this.#issues.push(".debug_str_offsets: index outside its contribution.");
      return null;
    }
    const cursor = this.cursor(".debug_str_offsets", offset, table?.end);
    return cursor ? cursor.unsigned(width) : null;
  }
 
  #validStringIndex(base: bigint | null, index: bigint): boolean {
    if (base != null && index >= 0n) return true;
    this.#issues.push("DW_FORM_strx requires a valid index and DW_AT_str_offsets_base in the unit root.");
    return false;
  }
 
  async #readAddress(unit: DwarfUnit, index: bigint, base: bigint | null): Promise<bigint | null> {
    if (!this.#validAddressIndex(base, index)) return null;
    const checkedBase = base!;
    const contribution = unit.version >= 5 ? await this.#byBase(".debug_addr", checkedBase) : null;
    if (unit.version >= 5 && !contribution) return null;
    const offset = checkedBase + index * BigInt(unit.addressSize);
    if (contribution && !this.#validAddressOffset(contribution, unit.addressSize, offset)) return null;
    const cursor = this.cursor(".debug_addr", offset, contribution?.end);
    return cursor ? cursor.unsigned(unit.addressSize) : null;
  }
 
  #validAddressIndex(base: bigint | null, index: bigint): boolean {
    if (base == null || index < 0n) {
      this.#issues.push("Indexed address requires a valid DW_AT_addr_base in the unit root.");
      return false;
    }
    return true;
  }
 
  #validAddressOffset(contribution: Contribution, addressSize: number, offset: bigint): boolean {
    if (contribution.addressSize === addressSize && offset + BigInt(addressSize) <= BigInt(contribution.end)) {
      return true;
    }
    this.#issues.push(".debug_addr: index outside its contribution or address-size mismatch.");
    return false;
  }
 
  async listCursor(unit: DwarfUnit, attribute: DwarfAttribute, name: string): Promise<DwarfCursor | null> {
    const value = dwarfNumericValue(attribute.value);
    if (value == null) return null;
    if (unit.version < 5) return this.cursor(name, value);
    const indexed = attribute.form === DWARF_FORM.rangeListIndex ||
      attribute.form === DWARF_FORM.locationListIndex;
    const contribution = indexed ? await this.#listContribution(unit, name)
      : (await this.#tables(name)).find(item => value >= BigInt(item.entriesOffset +
        item.offsetCount * (item.format / 8)) && value < BigInt(item.end));
    if (!contribution || contribution.addressSize !== unit.addressSize) {
      this.#issues.push(`${name}: list offset has no matching contribution or address-size mismatch.`);
      return null;
    }
    const offset = indexed ? await this.#listOffset(contribution, name, value) : value;
    return offset == null ? null : this.cursor(name, offset, contribution.end);
  }
 
  async #listOffset(table: Contribution, name: string, index: bigint): Promise<bigint | null> {
    if (index < 0n || index >= BigInt(table.offsetCount)) {
      this.#issues.push(`${name}: list index ${index} outside its contribution offset table.`);
      return null;
    }
    const cursor = this.cursor(name, BigInt(table.entriesOffset) + index * BigInt(table.format / 8), table.end);
    const relative = await cursor?.unsigned(table.format / 8);
    if (relative == null) return null;
    const offset = BigInt(table.entriesOffset) + relative;
    if (offset < BigInt(table.entriesOffset + table.offsetCount * (table.format / 8)) ||
        offset >= BigInt(table.end)) {
      this.#issues.push(`${name}: indexed list offset outside the contribution payload.`);
      return null;
    }
    return offset;
  }
 
  async #listContribution(unit: DwarfUnit, name: string): Promise<Contribution | null> {
    const base = dwarfNumericValue(dwarfAttributeValue(unit.dies[0],
      name === ".debug_rnglists" ? DWARF_ATTRIBUTE.rangeListsBase : DWARF_ATTRIBUTE.locationListsBase));
    if (base == null) {
      if (unit.sectionName.endsWith(".dwo")) return (await this.#tables(name))[0] ?? null;
      this.#issues.push(`${name}: indexed list requires a base attribute in the unit root.`);
      return null;
    }
    return this.#byBase(name, base);
  }
 
  async #byBase(name: string, base: bigint): Promise<Contribution | null> {
    const table = (await this.#tables(name)).find(item => BigInt(item.entriesOffset) === base);
    if (!table) this.#issues.push(`${name}: base ${base} does not identify a contribution table.`);
    return table ?? null;
  }
 
  #tables(name: string): Promise<Contribution[]> {
    if (!this.#contributions.has(name)) this.#contributions.set(name, this.#readTables(name));
    return this.#contributions.get(name)!;
  }
 
  async #readTables(name: string): Promise<Contribution[]> {
    const source = this.#sections.get(name);
    if (!source) { this.#issues.push(`${name}: indexed section is missing.`); return []; }
    const tables: Contribution[] = [];
    let offset = 0;
    while (offset < source.section.size) {
      const table = await readContribution(source, offset, this.#byteOrder, this.#issues);
      if (!table) break;
      tables.push(table);
      offset = table.end;
    }
    return tables;
  }
}