|
| 1 | +use anyhow::{Context, Result, bail, ensure}; |
| 2 | +use object::{Endianness, Object, ObjectSection}; |
| 3 | + |
| 4 | +use super::{Section, SectionKind}; |
| 5 | + |
| 6 | +const HDRR_SIZE: usize = 0x60; |
| 7 | +const FDR_SIZE: usize = 0x48; |
| 8 | +const PDR_SIZE: usize = 0x34; |
| 9 | +const SYMR_SIZE: usize = 0x0c; |
| 10 | + |
| 11 | +const ST_PROC: u8 = 6; |
| 12 | +const ST_STATICPROC: u8 = 14; |
| 13 | +const ST_END: u8 = 8; |
| 14 | + |
| 15 | +pub(super) fn parse_line_info_mdebug( |
| 16 | + obj_file: &object::File, |
| 17 | + sections: &mut [Section], |
| 18 | +) -> Result<()> { |
| 19 | + let Some(section) = obj_file.section_by_name(".mdebug") else { |
| 20 | + return Ok(()); |
| 21 | + }; |
| 22 | + |
| 23 | + let data = section.data().context("failed to read .mdebug contents")?; |
| 24 | + if data.len() < HDRR_SIZE { |
| 25 | + return Ok(()); |
| 26 | + } |
| 27 | + |
| 28 | + let section_file_offset = section.file_range().map(|(offset, _)| offset as usize); |
| 29 | + |
| 30 | + let endianness = obj_file.endianness(); |
| 31 | + let header = Header::parse(data, endianness)?; |
| 32 | + |
| 33 | + let symbols_data = slice_at( |
| 34 | + data, |
| 35 | + header.cb_sym_offset, |
| 36 | + header.isym_max.checked_mul(SYMR_SIZE as u32).context("symbol table size overflow")?, |
| 37 | + section_file_offset, |
| 38 | + )?; |
| 39 | + let symbols = parse_symbols(symbols_data, endianness)?; |
| 40 | + |
| 41 | + let fdr_data = slice_at( |
| 42 | + data, |
| 43 | + header.cb_fd_offset, |
| 44 | + header |
| 45 | + .ifd_max |
| 46 | + .checked_mul(FDR_SIZE as u32) |
| 47 | + .context("file descriptor table size overflow")?, |
| 48 | + section_file_offset, |
| 49 | + )?; |
| 50 | + |
| 51 | + for fdr_index in 0..header.ifd_max as usize { |
| 52 | + let fdr_offset = fdr_index * FDR_SIZE; |
| 53 | + let fdr = FileDescriptor::parse(&fdr_data[fdr_offset..fdr_offset + FDR_SIZE], endianness)?; |
| 54 | + if fdr.cpd == 0 || fdr.csym == 0 { |
| 55 | + continue; |
| 56 | + } |
| 57 | + |
| 58 | + let sym_base = fdr.isym_base as usize; |
| 59 | + let sym_end = sym_base + fdr.csym as usize; |
| 60 | + if sym_end > symbols.len() { |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + let Some(line_file_offset) = header.cb_line_offset.checked_add(fdr.cb_line_offset) else { |
| 65 | + continue; |
| 66 | + }; |
| 67 | + let Some(line_file_base) = |
| 68 | + resolve_offset(line_file_offset, data.len(), section_file_offset) |
| 69 | + else { |
| 70 | + continue; |
| 71 | + }; |
| 72 | + let Some(line_file_end) = line_file_base.checked_add(fdr.cb_line as usize) else { |
| 73 | + continue; |
| 74 | + }; |
| 75 | + if line_file_end > data.len() { |
| 76 | + continue; |
| 77 | + } |
| 78 | + |
| 79 | + for local_proc_index in 0..fdr.cpd as usize { |
| 80 | + let pdr_index = fdr.ipd_first as usize + local_proc_index; |
| 81 | + let pdr_offset = header |
| 82 | + .cb_pd_offset |
| 83 | + .checked_add((pdr_index as u32) * PDR_SIZE as u32) |
| 84 | + .context("procedure descriptor offset overflow")?; |
| 85 | + let pdr_data = match slice_at(data, pdr_offset, PDR_SIZE as u32, section_file_offset) { |
| 86 | + Ok(data) => data, |
| 87 | + Err(_) => continue, |
| 88 | + }; |
| 89 | + let pdr = ProcDescriptor::parse(pdr_data, endianness)?; |
| 90 | + if pdr.isym as usize >= fdr.csym as usize { |
| 91 | + continue; |
| 92 | + } |
| 93 | + let global_sym_index = sym_base + pdr.isym as usize; |
| 94 | + let Some(start_symbol) = symbols.get(global_sym_index) else { |
| 95 | + continue; |
| 96 | + }; |
| 97 | + if start_symbol.st != ST_PROC && start_symbol.st != ST_STATICPROC { |
| 98 | + continue; |
| 99 | + } |
| 100 | + |
| 101 | + let local_index = pdr.isym as u32; |
| 102 | + let mut size = None; |
| 103 | + for sym in &symbols[global_sym_index..sym_end] { |
| 104 | + if sym.st == ST_END && sym.index == local_index { |
| 105 | + size = Some(sym.value); |
| 106 | + break; |
| 107 | + } |
| 108 | + } |
| 109 | + let Some(size) = size else { |
| 110 | + continue; |
| 111 | + }; |
| 112 | + if size == 0 { |
| 113 | + continue; |
| 114 | + } |
| 115 | + let word_count = (size / 4) as usize; |
| 116 | + if word_count == 0 { |
| 117 | + continue; |
| 118 | + } |
| 119 | + |
| 120 | + let Some(mut cursor) = line_file_base.checked_add(pdr.cb_line_offset as usize) else { |
| 121 | + continue; |
| 122 | + }; |
| 123 | + if cursor >= line_file_end { |
| 124 | + continue; |
| 125 | + } |
| 126 | + |
| 127 | + let mut line_number = pdr.ln_low as i32; |
| 128 | + let mut lines = Vec::with_capacity(word_count); |
| 129 | + while lines.len() < word_count && cursor < line_file_end { |
| 130 | + let b0 = data[cursor]; |
| 131 | + cursor += 1; |
| 132 | + let count = (b0 & 0x0f) as usize + 1; |
| 133 | + let delta = decode_delta(endianness, b0 >> 4, data, &mut cursor, line_file_end)?; |
| 134 | + line_number = line_number.wrapping_add(delta as i32); |
| 135 | + for _ in 0..count { |
| 136 | + if lines.len() == word_count { |
| 137 | + break; |
| 138 | + } |
| 139 | + lines.push(line_number); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + if lines.len() != word_count { |
| 144 | + continue; |
| 145 | + } |
| 146 | + |
| 147 | + assign_lines(sections, fdr.adr as u64 + pdr.addr as u64, &lines); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + Ok(()) |
| 152 | +} |
| 153 | + |
| 154 | +fn assign_lines(sections: &mut [Section], base_address: u64, lines: &[i32]) { |
| 155 | + let mut address = base_address; |
| 156 | + for &line in lines { |
| 157 | + if line >= 0 { |
| 158 | + if let Some(section) = find_code_section(sections, address) { |
| 159 | + section.line_info.insert(address, line as u32); |
| 160 | + } |
| 161 | + } |
| 162 | + address = address.wrapping_add(4); |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +fn find_code_section<'a>(sections: &'a mut [Section], address: u64) -> Option<&'a mut Section> { |
| 167 | + sections.iter_mut().find(|section| { |
| 168 | + section.kind == SectionKind::Code |
| 169 | + && address >= section.address |
| 170 | + && address < section.address + section.size |
| 171 | + }) |
| 172 | +} |
| 173 | + |
| 174 | +fn decode_delta( |
| 175 | + endianness: Endianness, |
| 176 | + nibble: u8, |
| 177 | + data: &[u8], |
| 178 | + cursor: &mut usize, |
| 179 | + end: usize, |
| 180 | +) -> Result<i32> { |
| 181 | + if nibble == 8 { |
| 182 | + ensure!(*cursor + 2 <= end, "extended delta out of range"); |
| 183 | + let bytes: [u8; 2] = data[*cursor..*cursor + 2].try_into().unwrap(); |
| 184 | + *cursor += 2; |
| 185 | + Ok(match endianness { |
| 186 | + Endianness::Big => i16::from_be_bytes(bytes) as i32, |
| 187 | + Endianness::Little => i16::from_le_bytes(bytes) as i32, |
| 188 | + }) |
| 189 | + } else { |
| 190 | + let mut value = (nibble & 0x0f) as i32; |
| 191 | + if value & 0x8 != 0 { |
| 192 | + value -= 0x10; |
| 193 | + } |
| 194 | + Ok(value) |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +fn slice_at<'a>( |
| 199 | + data: &'a [u8], |
| 200 | + offset: u32, |
| 201 | + size: u32, |
| 202 | + section_file_offset: Option<usize>, |
| 203 | +) -> Result<&'a [u8]> { |
| 204 | + let size = size as usize; |
| 205 | + if size == 0 { |
| 206 | + ensure!( |
| 207 | + resolve_offset(offset, data.len(), section_file_offset).is_some(), |
| 208 | + "offset outside of .mdebug section" |
| 209 | + ); |
| 210 | + return Ok(&data[0..0]); |
| 211 | + } |
| 212 | + let Some(offset) = resolve_offset(offset, data.len(), section_file_offset) else { |
| 213 | + bail!("offset outside of .mdebug section"); |
| 214 | + }; |
| 215 | + let end = offset.checked_add(size).context("range overflow")?; |
| 216 | + ensure!(end <= data.len(), "range exceeds .mdebug size"); |
| 217 | + Ok(&data[offset..end]) |
| 218 | +} |
| 219 | + |
| 220 | +fn resolve_offset( |
| 221 | + offset: u32, |
| 222 | + data_len: usize, |
| 223 | + section_file_offset: Option<usize>, |
| 224 | +) -> Option<usize> { |
| 225 | + let offset = offset as usize; |
| 226 | + if offset <= data_len { |
| 227 | + Some(offset) |
| 228 | + } else if let Some(file_offset) = section_file_offset { |
| 229 | + offset.checked_sub(file_offset).filter(|rel| *rel <= data_len) |
| 230 | + } else { |
| 231 | + None |
| 232 | + } |
| 233 | +} |
| 234 | + |
| 235 | +#[derive(Clone, Copy)] |
| 236 | +struct Header { |
| 237 | + cb_line_offset: u32, |
| 238 | + cb_pd_offset: u32, |
| 239 | + cb_sym_offset: u32, |
| 240 | + cb_fd_offset: u32, |
| 241 | + isym_max: u32, |
| 242 | + ifd_max: u32, |
| 243 | +} |
| 244 | + |
| 245 | +impl Header { |
| 246 | + fn parse(data: &[u8], endianness: Endianness) -> Result<Self> { |
| 247 | + ensure!(HDRR_SIZE <= data.len(), ".mdebug header truncated"); |
| 248 | + let mut cursor = 0; |
| 249 | + let magic = read_u16(data, &mut cursor, endianness)?; |
| 250 | + let _vstamp = read_u16(data, &mut cursor, endianness)?; |
| 251 | + ensure!(magic == 0x7009, "unexpected .mdebug magic: {magic:#x}"); |
| 252 | + let _iline_max = read_u32(data, &mut cursor, endianness)?; |
| 253 | + let _cb_line = read_u32(data, &mut cursor, endianness)?; |
| 254 | + let cb_line_offset = read_u32(data, &mut cursor, endianness)?; |
| 255 | + let _idn_max = read_u32(data, &mut cursor, endianness)?; |
| 256 | + let _cb_dn_offset = read_u32(data, &mut cursor, endianness)?; |
| 257 | + let _ipd_max = read_u32(data, &mut cursor, endianness)?; |
| 258 | + let cb_pd_offset = read_u32(data, &mut cursor, endianness)?; |
| 259 | + let isym_max = read_u32(data, &mut cursor, endianness)?; |
| 260 | + let cb_sym_offset = read_u32(data, &mut cursor, endianness)?; |
| 261 | + let _iopt_max = read_u32(data, &mut cursor, endianness)?; |
| 262 | + let _cb_opt_offset = read_u32(data, &mut cursor, endianness)?; |
| 263 | + let _iaux_max = read_u32(data, &mut cursor, endianness)?; |
| 264 | + let _cb_aux_offset = read_u32(data, &mut cursor, endianness)?; |
| 265 | + let _iss_max = read_u32(data, &mut cursor, endianness)?; |
| 266 | + let _cb_ss_offset = read_u32(data, &mut cursor, endianness)?; |
| 267 | + let _iss_ext_max = read_u32(data, &mut cursor, endianness)?; |
| 268 | + let _cb_ss_ext_offset = read_u32(data, &mut cursor, endianness)?; |
| 269 | + let ifd_max = read_u32(data, &mut cursor, endianness)?; |
| 270 | + let cb_fd_offset = read_u32(data, &mut cursor, endianness)?; |
| 271 | + let _crfd = read_u32(data, &mut cursor, endianness)?; |
| 272 | + let _cb_rfd_offset = read_u32(data, &mut cursor, endianness)?; |
| 273 | + let _iext_max = read_u32(data, &mut cursor, endianness)?; |
| 274 | + let _cb_ext_offset = read_u32(data, &mut cursor, endianness)?; |
| 275 | + |
| 276 | + Ok(Header { cb_line_offset, cb_pd_offset, cb_sym_offset, cb_fd_offset, isym_max, ifd_max }) |
| 277 | + } |
| 278 | +} |
| 279 | + |
| 280 | +#[derive(Clone, Copy)] |
| 281 | +struct FileDescriptor { |
| 282 | + adr: u32, |
| 283 | + isym_base: u32, |
| 284 | + csym: u32, |
| 285 | + ipd_first: u16, |
| 286 | + cpd: u16, |
| 287 | + cb_line_offset: u32, |
| 288 | + cb_line: u32, |
| 289 | +} |
| 290 | + |
| 291 | +impl FileDescriptor { |
| 292 | + fn parse(data: &[u8], endianness: Endianness) -> Result<Self> { |
| 293 | + ensure!(data.len() >= FDR_SIZE, "FDR truncated"); |
| 294 | + let mut cursor = 0; |
| 295 | + let adr = read_u32(data, &mut cursor, endianness)?; |
| 296 | + let _rss = read_u32(data, &mut cursor, endianness)?; |
| 297 | + let _iss_base = read_u32(data, &mut cursor, endianness)?; |
| 298 | + let _cb_ss = read_u32(data, &mut cursor, endianness)?; |
| 299 | + let isym_base = read_u32(data, &mut cursor, endianness)?; |
| 300 | + let csym = read_u32(data, &mut cursor, endianness)?; |
| 301 | + let _iline_base = read_u32(data, &mut cursor, endianness)?; |
| 302 | + let _cline = read_u32(data, &mut cursor, endianness)?; |
| 303 | + let _iopt_base = read_u32(data, &mut cursor, endianness)?; |
| 304 | + let _copt = read_u32(data, &mut cursor, endianness)?; |
| 305 | + let ipd_first = read_u16(data, &mut cursor, endianness)?; |
| 306 | + let cpd = read_u16(data, &mut cursor, endianness)?; |
| 307 | + let _iaux_base = read_u32(data, &mut cursor, endianness)?; |
| 308 | + let _caux = read_u32(data, &mut cursor, endianness)?; |
| 309 | + let _rfd_base = read_u32(data, &mut cursor, endianness)?; |
| 310 | + let _crfd = read_u32(data, &mut cursor, endianness)?; |
| 311 | + let _bits = read_u32(data, &mut cursor, endianness)?; |
| 312 | + let cb_line_offset = read_u32(data, &mut cursor, endianness)?; |
| 313 | + let cb_line = read_u32(data, &mut cursor, endianness)?; |
| 314 | + |
| 315 | + Ok(FileDescriptor { adr, isym_base, csym, ipd_first, cpd, cb_line_offset, cb_line }) |
| 316 | + } |
| 317 | +} |
| 318 | + |
| 319 | +#[derive(Clone, Copy)] |
| 320 | +struct ProcDescriptor { |
| 321 | + addr: u32, |
| 322 | + isym: u32, |
| 323 | + ln_low: i32, |
| 324 | + cb_line_offset: u32, |
| 325 | +} |
| 326 | + |
| 327 | +impl ProcDescriptor { |
| 328 | + fn parse(data: &[u8], endianness: Endianness) -> Result<Self> { |
| 329 | + ensure!(data.len() >= PDR_SIZE, "PDR truncated"); |
| 330 | + let mut cursor = 0; |
| 331 | + let addr = read_u32(data, &mut cursor, endianness)?; |
| 332 | + let isym = read_u32(data, &mut cursor, endianness)?; |
| 333 | + let _iline = read_u32(data, &mut cursor, endianness)?; |
| 334 | + let _regmask = read_u32(data, &mut cursor, endianness)?; |
| 335 | + let _regoffset = read_u32(data, &mut cursor, endianness)?; |
| 336 | + let _iopt = read_u32(data, &mut cursor, endianness)?; |
| 337 | + let _fregmask = read_u32(data, &mut cursor, endianness)?; |
| 338 | + let _fregoffset = read_u32(data, &mut cursor, endianness)?; |
| 339 | + let _frameoffset = read_u32(data, &mut cursor, endianness)?; |
| 340 | + let _framereg = read_u16(data, &mut cursor, endianness)?; |
| 341 | + let _pcreg = read_u16(data, &mut cursor, endianness)?; |
| 342 | + let ln_low = read_i32(data, &mut cursor, endianness)?; |
| 343 | + let _ln_high = read_i32(data, &mut cursor, endianness)?; |
| 344 | + let cb_line_offset = read_u32(data, &mut cursor, endianness)?; |
| 345 | + |
| 346 | + Ok(ProcDescriptor { addr, isym, ln_low, cb_line_offset }) |
| 347 | + } |
| 348 | +} |
| 349 | + |
| 350 | +#[derive(Clone, Copy)] |
| 351 | +struct SymbolEntry { |
| 352 | + value: u32, |
| 353 | + st: u8, |
| 354 | + index: u32, |
| 355 | +} |
| 356 | + |
| 357 | +fn parse_symbols(data: &[u8], endianness: Endianness) -> Result<Vec<SymbolEntry>> { |
| 358 | + ensure!(data.len() % SYMR_SIZE == 0, "symbol table misaligned"); |
| 359 | + let mut symbols = Vec::with_capacity(data.len() / SYMR_SIZE); |
| 360 | + let mut cursor = 0; |
| 361 | + while cursor + SYMR_SIZE <= data.len() { |
| 362 | + let _iss = read_u32(data, &mut cursor, endianness)?; |
| 363 | + let value = read_u32(data, &mut cursor, endianness)?; |
| 364 | + let bits = read_u32(data, &mut cursor, endianness)?; |
| 365 | + let (st, index) = match endianness { |
| 366 | + Endianness::Big => (((bits >> 26) & 0x3f) as u8, bits & 0x000f_ffff), |
| 367 | + Endianness::Little => (((bits & 0x3f) as u8), (bits >> 12) & 0x000f_ffff), |
| 368 | + }; |
| 369 | + symbols.push(SymbolEntry { value, st, index }); |
| 370 | + } |
| 371 | + Ok(symbols) |
| 372 | +} |
| 373 | + |
| 374 | +fn read_u16(data: &[u8], cursor: &mut usize, endianness: Endianness) -> Result<u16> { |
| 375 | + ensure!(*cursor + 2 <= data.len(), "unexpected EOF while reading u16"); |
| 376 | + let bytes: [u8; 2] = data[*cursor..*cursor + 2].try_into().unwrap(); |
| 377 | + *cursor += 2; |
| 378 | + Ok(match endianness { |
| 379 | + Endianness::Big => u16::from_be_bytes(bytes), |
| 380 | + Endianness::Little => u16::from_le_bytes(bytes), |
| 381 | + }) |
| 382 | +} |
| 383 | + |
| 384 | +fn read_u32(data: &[u8], cursor: &mut usize, endianness: Endianness) -> Result<u32> { |
| 385 | + ensure!(*cursor + 4 <= data.len(), "unexpected EOF while reading u32"); |
| 386 | + let bytes: [u8; 4] = data[*cursor..*cursor + 4].try_into().unwrap(); |
| 387 | + *cursor += 4; |
| 388 | + Ok(match endianness { |
| 389 | + Endianness::Big => u32::from_be_bytes(bytes), |
| 390 | + Endianness::Little => u32::from_le_bytes(bytes), |
| 391 | + }) |
| 392 | +} |
| 393 | + |
| 394 | +fn read_i32(data: &[u8], cursor: &mut usize, endianness: Endianness) -> Result<i32> { |
| 395 | + Ok(read_u32(data, cursor, endianness)? as i32) |
| 396 | +} |
0 commit comments