|
| 1 | +// reads the deno binary's PE headers and outputs all DLLs |
| 2 | +// along with whether they are eagerly or delay-loaded. |
| 3 | + |
| 4 | +function readU16(buf: Uint8Array, offset: number): number { |
| 5 | + return buf[offset] | (buf[offset + 1] << 8); |
| 6 | +} |
| 7 | + |
| 8 | +function readU32(buf: Uint8Array, offset: number): number { |
| 9 | + return ( |
| 10 | + (buf[offset] | |
| 11 | + (buf[offset + 1] << 8) | |
| 12 | + (buf[offset + 2] << 16) | |
| 13 | + ((buf[offset + 3] << 24) >>> 0)) >>> |
| 14 | + 0 |
| 15 | + ); |
| 16 | +} |
| 17 | + |
| 18 | +function readCString(buf: Uint8Array, offset: number): string { |
| 19 | + let end = offset; |
| 20 | + while (end < buf.length && buf[end] !== 0) end++; |
| 21 | + return new TextDecoder().decode(buf.subarray(offset, end)); |
| 22 | +} |
| 23 | + |
| 24 | +interface Section { |
| 25 | + name: string; |
| 26 | + virtualAddress: number; |
| 27 | + virtualSize: number; |
| 28 | + rawOffset: number; |
| 29 | + rawSize: number; |
| 30 | +} |
| 31 | + |
| 32 | +function rvaToOffset(rva: number, sections: Section[]): number | null { |
| 33 | + for (const s of sections) { |
| 34 | + if (rva >= s.virtualAddress && rva < s.virtualAddress + s.rawSize) { |
| 35 | + return s.rawOffset + (rva - s.virtualAddress); |
| 36 | + } |
| 37 | + } |
| 38 | + return null; |
| 39 | +} |
| 40 | + |
| 41 | +function parseDllNames( |
| 42 | + buf: Uint8Array, |
| 43 | + tableRva: number, |
| 44 | + sections: Section[], |
| 45 | + entrySize: number, |
| 46 | + nameRvaOffset: number, |
| 47 | +): string[] { |
| 48 | + const names: string[] = []; |
| 49 | + const tableOffset = rvaToOffset(tableRva, sections); |
| 50 | + if (tableOffset === null) return names; |
| 51 | + |
| 52 | + let idx = 0; |
| 53 | + while (true) { |
| 54 | + const entryOff = tableOffset + idx * entrySize; |
| 55 | + const nameRva = readU32(buf, entryOff + nameRvaOffset); |
| 56 | + if (nameRva === 0) break; |
| 57 | + const nameOff = rvaToOffset(nameRva, sections); |
| 58 | + if (nameOff !== null) { |
| 59 | + names.push(readCString(buf, nameOff).toLowerCase()); |
| 60 | + } |
| 61 | + idx++; |
| 62 | + } |
| 63 | + return names; |
| 64 | +} |
| 65 | + |
| 66 | +const exePath = Deno.execPath(); |
| 67 | +const buf = Deno.readFileSync(exePath); |
| 68 | + |
| 69 | +// parse PE |
| 70 | +const peOffset = readU32(buf, 0x3c); |
| 71 | +const numSections = readU16(buf, peOffset + 6); |
| 72 | +const sizeOptHeader = readU16(buf, peOffset + 24 - 4); |
| 73 | +const optStart = peOffset + 24; |
| 74 | +const magic = readU16(buf, optStart); |
| 75 | + |
| 76 | +if (magic !== 0x20b) { |
| 77 | + console.error("not a PE32+ binary"); |
| 78 | + Deno.exit(1); |
| 79 | +} |
| 80 | + |
| 81 | +// data directories start at optStart + 112, preceded by count at +108 |
| 82 | +const ddStart = optStart + 112; |
| 83 | + |
| 84 | +// read sections |
| 85 | +const secStart = optStart + sizeOptHeader; |
| 86 | +const sections: Section[] = []; |
| 87 | +for (let i = 0; i < numSections; i++) { |
| 88 | + const off = secStart + i * 40; |
| 89 | + let nameEnd = 8; |
| 90 | + while (nameEnd > 0 && buf[off + nameEnd - 1] === 0) nameEnd--; |
| 91 | + const name = new TextDecoder().decode(buf.subarray(off, off + nameEnd)); |
| 92 | + sections.push({ |
| 93 | + name, |
| 94 | + virtualSize: readU32(buf, off + 8), |
| 95 | + virtualAddress: readU32(buf, off + 12), |
| 96 | + rawSize: readU32(buf, off + 16), |
| 97 | + rawOffset: readU32(buf, off + 20), |
| 98 | + }); |
| 99 | +} |
| 100 | + |
| 101 | +// import directory (index 1) - entries are 20 bytes, name RVA at offset 12 |
| 102 | +const importRva = readU32(buf, ddStart + 1 * 8); |
| 103 | +const eagerDlls = parseDllNames(buf, importRva, sections, 20, 12); |
| 104 | + |
| 105 | +// delay import directory (index 13) - entries are 32 bytes, name RVA at offset 4 |
| 106 | +const delayRva = readU32(buf, ddStart + 13 * 8); |
| 107 | +const delayDlls = parseDllNames(buf, delayRva, sections, 32, 4); |
| 108 | + |
| 109 | +// deduplicate and collect all dlls with their load type |
| 110 | +const allDlls = new Map<string, string>(); |
| 111 | +for (const dll of eagerDlls) { |
| 112 | + allDlls.set(dll, "eager"); |
| 113 | +} |
| 114 | +for (const dll of delayDlls) { |
| 115 | + allDlls.set(dll, "delay"); |
| 116 | +} |
| 117 | + |
| 118 | +const sorted = [...allDlls.entries()].sort((a, b) => a[0].localeCompare(b[0])); |
| 119 | +for (const [dll, type] of sorted) { |
| 120 | + console.log(`${dll}: ${type}`); |
| 121 | +} |
0 commit comments