|
| 1 | +//! Synthesis of MSVC string-literal symbol names (`??_C@_0…`). |
| 2 | +//! |
| 3 | +//! MSVC emits every string literal as a COMDAT with a deterministic decorated |
| 4 | +//! name derived purely from the string's bytes. Those COMDATs are frequently |
| 5 | +//! folded/anonymous and carry **no** symbol record in the PDB, so a data |
| 6 | +//! reference to one falls through the PDB-symbol lookup to a raw |
| 7 | +//! `__delink_pe_<section>_start + offset` section-relative fallback — which |
| 8 | +//! never matches the `??_C@…` symbol our own recompiled object references. |
| 9 | +//! |
| 10 | +//! Since the name is a pure function of the bytes, we reproduce MSVC's mangling |
| 11 | +//! here and look the literal up **by address** (reading the bytes at the |
| 12 | +//! reference target) rather than by a name that isn't in the PDB. |
| 13 | +//! |
| 14 | +//! Name format: `??_C@_0<len><crc>@<text>@` |
| 15 | +//! * `<len>` — byte length *including* the NUL terminator, as a mangled number |
| 16 | +//! (`1..=10` → `'0'..'9'` = value-1; otherwise base-16 nibbles `A`(0)..`P`(15) |
| 17 | +//! most-significant first, terminated by `@`). |
| 18 | +//! * `<crc>` — CRC-32 (poly 0xEDB88320, init 0xFFFFFFFF, **no** final inversion) |
| 19 | +//! over the bytes *including* the NUL, as 8 nibbles `A`..`P`, MSB first. |
| 20 | +//! * `<text>` — up to the first 32 source bytes, escaped (see [`escape_byte`]), |
| 21 | +//! terminated by `@`. |
| 22 | +
|
| 23 | +/// Precomputed CRC-32 lookup table (reflected, poly 0xEDB88320). |
| 24 | +const fn crc32_table() -> [u32; 256] { |
| 25 | + let mut table = [0u32; 256]; |
| 26 | + let mut n = 0usize; |
| 27 | + while n < 256 { |
| 28 | + let mut c = n as u32; |
| 29 | + let mut k = 0; |
| 30 | + while k < 8 { |
| 31 | + c = if c & 1 != 0 { |
| 32 | + 0xEDB8_8320 ^ (c >> 1) |
| 33 | + } else { |
| 34 | + c >> 1 |
| 35 | + }; |
| 36 | + k += 1; |
| 37 | + } |
| 38 | + table[n] = c; |
| 39 | + n += 1; |
| 40 | + } |
| 41 | + table |
| 42 | +} |
| 43 | + |
| 44 | +const CRC32_TABLE: [u32; 256] = crc32_table(); |
| 45 | + |
| 46 | +/// MSVC's string-literal checksum: CRC-32 with init `0xFFFFFFFF` and **no** |
| 47 | +/// final XOR (unlike zlib, which inverts the result). |
| 48 | +fn msvc_string_crc(bytes: &[u8]) -> u32 { |
| 49 | + let mut c = 0xFFFF_FFFFu32; |
| 50 | + for &b in bytes { |
| 51 | + c = CRC32_TABLE[((c ^ b as u32) & 0xFF) as usize] ^ (c >> 8); |
| 52 | + } |
| 53 | + c |
| 54 | +} |
| 55 | + |
| 56 | +/// Append `n` nibbles of `v` (most-significant first) using the `A`(0)..`P`(15) |
| 57 | +/// alphabet MSVC uses for mangled numbers. |
| 58 | +fn push_nibbles(out: &mut String, v: u32, n: u32) { |
| 59 | + for i in (0..n).rev() { |
| 60 | + let nib = (v >> (i * 4)) & 0xF; |
| 61 | + out.push((b'A' + nib as u8) as char); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +/// Encode a mangled number: `1..=10` as a single digit `'0'..'9'` (value-1), |
| 66 | +/// otherwise base-16 nibbles `A`..`P` (MSB first) terminated by `@`. |
| 67 | +fn encode_number(out: &mut String, value: u32) { |
| 68 | + if (1..=10).contains(&value) { |
| 69 | + out.push((b'0' + (value - 1) as u8) as char); |
| 70 | + return; |
| 71 | + } |
| 72 | + // Minimal nibble count. |
| 73 | + let mut nibbles = 0u32; |
| 74 | + let mut x = value; |
| 75 | + while x != 0 { |
| 76 | + nibbles += 1; |
| 77 | + x >>= 4; |
| 78 | + } |
| 79 | + if nibbles == 0 { |
| 80 | + nibbles = 1; |
| 81 | + } |
| 82 | + push_nibbles(out, value, nibbles); |
| 83 | + out.push('@'); |
| 84 | +} |
| 85 | + |
| 86 | +/// The ten single-character escapes MSVC uses for common punctuation, indexed |
| 87 | +/// `0..=9` → `?0`..`?9`. Order is significant. |
| 88 | +const SPECIAL: &[u8] = b",/\\:. \n\t'-"; |
| 89 | + |
| 90 | +/// Escape one source byte into the string-literal text field. |
| 91 | +fn escape_byte(out: &mut String, b: u8) { |
| 92 | + if b.is_ascii_alphanumeric() || b == b'_' { |
| 93 | + out.push(b as char); |
| 94 | + } else if let Some(idx) = SPECIAL.iter().position(|&c| c == b) { |
| 95 | + out.push('?'); |
| 96 | + out.push((b'0' + idx as u8) as char); |
| 97 | + } else { |
| 98 | + out.push_str("?$"); |
| 99 | + out.push((b'A' + (b >> 4)) as char); |
| 100 | + out.push((b'A' + (b & 0xF)) as char); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +/// The maximum source-byte prefix MSVC embeds in the decorated name's text field. |
| 105 | +const MAX_TEXT_BYTES: usize = 32; |
| 106 | + |
| 107 | +/// Build the MSVC `??_C@` decorated name for a narrow (char) string literal. |
| 108 | +/// |
| 109 | +/// `content` is the string's bytes **without** the terminating NUL; this |
| 110 | +/// function appends it (MSVC hashes and counts the NUL). |
| 111 | +pub fn narrow_string_symbol(content: &[u8]) -> String { |
| 112 | + // Length and CRC are over the bytes including the NUL terminator. |
| 113 | + let total_len = content.len() as u32 + 1; |
| 114 | + // CRC over content + NUL, without allocating a joined buffer. |
| 115 | + let crc = { |
| 116 | + let mut c = msvc_string_crc(content); |
| 117 | + c = CRC32_TABLE[((c ^ 0) & 0xFF) as usize] ^ (c >> 8); |
| 118 | + c |
| 119 | + }; |
| 120 | + |
| 121 | + let mut out = String::from("??_C@_0"); |
| 122 | + encode_number(&mut out, total_len); |
| 123 | + push_nibbles(&mut out, crc, 8); |
| 124 | + out.push('@'); |
| 125 | + // Text: escape up to MAX_TEXT_BYTES source bytes (NUL only appears if the |
| 126 | + // whole string fits, since content excludes it and we cap before it). |
| 127 | + let text_bytes = content.len().min(MAX_TEXT_BYTES); |
| 128 | + for &b in &content[..text_bytes] { |
| 129 | + escape_byte(&mut out, b); |
| 130 | + } |
| 131 | + if content.len() < MAX_TEXT_BYTES { |
| 132 | + // Short enough that the NUL terminator is part of the embedded text. |
| 133 | + escape_byte(&mut out, 0); |
| 134 | + } |
| 135 | + out.push('@'); |
| 136 | + out |
| 137 | +} |
| 138 | + |
| 139 | +#[cfg(test)] |
| 140 | +mod tests { |
| 141 | + use super::*; |
| 142 | + |
| 143 | + #[test] |
| 144 | + fn known_string_literal_names() { |
| 145 | + // Ground-truth (content, decorated name) pairs extracted from |
| 146 | + // MSVC-compiled objects, spanning the single-digit and multi-nibble |
| 147 | + // length forms and the space/`?5` escape. |
| 148 | + let cases: &[(&[u8], &str)] = &[ |
| 149 | + (b"system", "??_C@_06FHFOAHML@system?$AA@"), |
| 150 | + (b"generic", "??_C@_07DCLBNMLN@generic?$AA@"), |
| 151 | + (b"iostream", "??_C@_08LLGCOLLL@iostream?$AA@"), |
| 152 | + (b"tag_resource_lruv", "??_C@_0BC@DKEHMPJE@tag_resource_lruv?$AA@"), |
| 153 | + (b"string too long", "??_C@_0BA@JFNIOLAK@string?5too?5long?$AA@"), |
| 154 | + ]; |
| 155 | + for (content, expected) in cases { |
| 156 | + assert_eq!(&narrow_string_symbol(content), expected, "content = {:?}", content); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + #[test] |
| 161 | + fn crc_matches_reference() { |
| 162 | + // "system\0" → 0x575E07CB |
| 163 | + assert_eq!(msvc_string_crc(b"system\x00"), 0x575E_07CB); |
| 164 | + } |
| 165 | +} |
0 commit comments