]>
Commit | Line | Data |
---|---|---|
fe692bf9 FG |
1 | use alloc::vec::Vec; |
2 | use core::fmt; | |
3 | use core::num::{NonZeroU64, Wrapping}; | |
4 | use core::result; | |
5 | ||
6 | use crate::common::{ | |
7 | DebugLineOffset, DebugLineStrOffset, DebugStrOffset, DebugStrOffsetsIndex, Encoding, Format, | |
8 | LineEncoding, SectionId, | |
9 | }; | |
10 | use crate::constants; | |
11 | use crate::endianity::Endianity; | |
12 | use crate::read::{AttributeValue, EndianSlice, Error, Reader, ReaderOffset, Result, Section}; | |
13 | ||
14 | /// The `DebugLine` struct contains the source location to instruction mapping | |
15 | /// found in the `.debug_line` section. | |
16 | #[derive(Debug, Default, Clone, Copy)] | |
17 | pub struct DebugLine<R> { | |
18 | debug_line_section: R, | |
19 | } | |
20 | ||
21 | impl<'input, Endian> DebugLine<EndianSlice<'input, Endian>> | |
22 | where | |
23 | Endian: Endianity, | |
24 | { | |
25 | /// Construct a new `DebugLine` instance from the data in the `.debug_line` | |
26 | /// section. | |
27 | /// | |
28 | /// It is the caller's responsibility to read the `.debug_line` section and | |
29 | /// present it as a `&[u8]` slice. That means using some ELF loader on | |
30 | /// Linux, a Mach-O loader on macOS, etc. | |
31 | /// | |
32 | /// ``` | |
33 | /// use gimli::{DebugLine, LittleEndian}; | |
34 | /// | |
35 | /// # let buf = [0x00, 0x01, 0x02, 0x03]; | |
36 | /// # let read_debug_line_section_somehow = || &buf; | |
37 | /// let debug_line = DebugLine::new(read_debug_line_section_somehow(), LittleEndian); | |
38 | /// ``` | |
39 | pub fn new(debug_line_section: &'input [u8], endian: Endian) -> Self { | |
40 | Self::from(EndianSlice::new(debug_line_section, endian)) | |
41 | } | |
42 | } | |
43 | ||
44 | impl<R: Reader> DebugLine<R> { | |
45 | /// Parse the line number program whose header is at the given `offset` in the | |
46 | /// `.debug_line` section. | |
47 | /// | |
48 | /// The `address_size` must match the compilation unit that the lines apply to. | |
49 | /// The `comp_dir` should be from the `DW_AT_comp_dir` attribute of the compilation | |
50 | /// unit. The `comp_name` should be from the `DW_AT_name` attribute of the | |
51 | /// compilation unit. | |
52 | /// | |
53 | /// ```rust,no_run | |
54 | /// use gimli::{DebugLine, DebugLineOffset, IncompleteLineProgram, EndianSlice, LittleEndian}; | |
55 | /// | |
56 | /// # let buf = []; | |
57 | /// # let read_debug_line_section_somehow = || &buf; | |
58 | /// let debug_line = DebugLine::new(read_debug_line_section_somehow(), LittleEndian); | |
59 | /// | |
60 | /// // In a real example, we'd grab the offset via a compilation unit | |
61 | /// // entry's `DW_AT_stmt_list` attribute, and the address size from that | |
62 | /// // unit directly. | |
63 | /// let offset = DebugLineOffset(0); | |
64 | /// let address_size = 8; | |
65 | /// | |
66 | /// let program = debug_line.program(offset, address_size, None, None) | |
67 | /// .expect("should have found a header at that offset, and parsed it OK"); | |
68 | /// ``` | |
69 | pub fn program( | |
70 | &self, | |
71 | offset: DebugLineOffset<R::Offset>, | |
72 | address_size: u8, | |
73 | comp_dir: Option<R>, | |
74 | comp_name: Option<R>, | |
75 | ) -> Result<IncompleteLineProgram<R>> { | |
76 | let input = &mut self.debug_line_section.clone(); | |
77 | input.skip(offset.0)?; | |
78 | let header = LineProgramHeader::parse(input, offset, address_size, comp_dir, comp_name)?; | |
79 | let program = IncompleteLineProgram { header }; | |
80 | Ok(program) | |
81 | } | |
82 | } | |
83 | ||
84 | impl<T> DebugLine<T> { | |
85 | /// Create a `DebugLine` section that references the data in `self`. | |
86 | /// | |
87 | /// This is useful when `R` implements `Reader` but `T` does not. | |
88 | /// | |
89 | /// ## Example Usage | |
90 | /// | |
91 | /// ```rust,no_run | |
92 | /// # let load_section = || unimplemented!(); | |
93 | /// // Read the DWARF section into a `Vec` with whatever object loader you're using. | |
94 | /// let owned_section: gimli::DebugLine<Vec<u8>> = load_section(); | |
95 | /// // Create a reference to the DWARF section. | |
96 | /// let section = owned_section.borrow(|section| { | |
97 | /// gimli::EndianSlice::new(§ion, gimli::LittleEndian) | |
98 | /// }); | |
99 | /// ``` | |
100 | pub fn borrow<'a, F, R>(&'a self, mut borrow: F) -> DebugLine<R> | |
101 | where | |
102 | F: FnMut(&'a T) -> R, | |
103 | { | |
104 | borrow(&self.debug_line_section).into() | |
105 | } | |
106 | } | |
107 | ||
108 | impl<R> Section<R> for DebugLine<R> { | |
109 | fn id() -> SectionId { | |
110 | SectionId::DebugLine | |
111 | } | |
112 | ||
113 | fn reader(&self) -> &R { | |
114 | &self.debug_line_section | |
115 | } | |
116 | } | |
117 | ||
118 | impl<R> From<R> for DebugLine<R> { | |
119 | fn from(debug_line_section: R) -> Self { | |
120 | DebugLine { debug_line_section } | |
121 | } | |
122 | } | |
123 | ||
124 | /// Deprecated. `LineNumberProgram` has been renamed to `LineProgram`. | |
125 | #[deprecated(note = "LineNumberProgram has been renamed to LineProgram, use that instead.")] | |
126 | pub type LineNumberProgram<R, Offset> = dyn LineProgram<R, Offset>; | |
127 | ||
128 | /// A `LineProgram` provides access to a `LineProgramHeader` and | |
129 | /// a way to add files to the files table if necessary. Gimli consumers should | |
130 | /// never need to use or see this trait. | |
131 | pub trait LineProgram<R, Offset = <R as Reader>::Offset> | |
132 | where | |
133 | R: Reader<Offset = Offset>, | |
134 | Offset: ReaderOffset, | |
135 | { | |
136 | /// Get a reference to the held `LineProgramHeader`. | |
137 | fn header(&self) -> &LineProgramHeader<R, Offset>; | |
138 | /// Add a file to the file table if necessary. | |
139 | fn add_file(&mut self, file: FileEntry<R, Offset>); | |
140 | } | |
141 | ||
142 | impl<R, Offset> LineProgram<R, Offset> for IncompleteLineProgram<R, Offset> | |
143 | where | |
144 | R: Reader<Offset = Offset>, | |
145 | Offset: ReaderOffset, | |
146 | { | |
147 | fn header(&self) -> &LineProgramHeader<R, Offset> { | |
148 | &self.header | |
149 | } | |
150 | fn add_file(&mut self, file: FileEntry<R, Offset>) { | |
151 | self.header.file_names.push(file); | |
152 | } | |
153 | } | |
154 | ||
155 | impl<'program, R, Offset> LineProgram<R, Offset> for &'program CompleteLineProgram<R, Offset> | |
156 | where | |
157 | R: Reader<Offset = Offset>, | |
158 | Offset: ReaderOffset, | |
159 | { | |
160 | fn header(&self) -> &LineProgramHeader<R, Offset> { | |
161 | &self.header | |
162 | } | |
163 | fn add_file(&mut self, _: FileEntry<R, Offset>) { | |
164 | // Nop. Our file table is already complete. | |
165 | } | |
166 | } | |
167 | ||
168 | /// Deprecated. `StateMachine` has been renamed to `LineRows`. | |
169 | #[deprecated(note = "StateMachine has been renamed to LineRows, use that instead.")] | |
170 | pub type StateMachine<R, Program, Offset> = LineRows<R, Program, Offset>; | |
171 | ||
172 | /// Executes a `LineProgram` to iterate over the rows in the matrix of line number information. | |
173 | /// | |
174 | /// "The hypothetical machine used by a consumer of the line number information | |
175 | /// to expand the byte-coded instruction stream into a matrix of line number | |
176 | /// information." -- Section 6.2.1 | |
177 | #[derive(Debug, Clone)] | |
178 | pub struct LineRows<R, Program, Offset = <R as Reader>::Offset> | |
179 | where | |
180 | Program: LineProgram<R, Offset>, | |
181 | R: Reader<Offset = Offset>, | |
182 | Offset: ReaderOffset, | |
183 | { | |
184 | program: Program, | |
185 | row: LineRow, | |
186 | instructions: LineInstructions<R>, | |
187 | } | |
188 | ||
189 | type OneShotLineRows<R, Offset = <R as Reader>::Offset> = | |
190 | LineRows<R, IncompleteLineProgram<R, Offset>, Offset>; | |
191 | ||
192 | type ResumedLineRows<'program, R, Offset = <R as Reader>::Offset> = | |
193 | LineRows<R, &'program CompleteLineProgram<R, Offset>, Offset>; | |
194 | ||
195 | impl<R, Program, Offset> LineRows<R, Program, Offset> | |
196 | where | |
197 | Program: LineProgram<R, Offset>, | |
198 | R: Reader<Offset = Offset>, | |
199 | Offset: ReaderOffset, | |
200 | { | |
201 | fn new(program: IncompleteLineProgram<R, Offset>) -> OneShotLineRows<R, Offset> { | |
202 | let row = LineRow::new(program.header()); | |
203 | let instructions = LineInstructions { | |
204 | input: program.header().program_buf.clone(), | |
205 | }; | |
206 | LineRows { | |
207 | program, | |
208 | row, | |
209 | instructions, | |
210 | } | |
211 | } | |
212 | ||
213 | fn resume<'program>( | |
214 | program: &'program CompleteLineProgram<R, Offset>, | |
215 | sequence: &LineSequence<R>, | |
216 | ) -> ResumedLineRows<'program, R, Offset> { | |
217 | let row = LineRow::new(program.header()); | |
218 | let instructions = sequence.instructions.clone(); | |
219 | LineRows { | |
220 | program, | |
221 | row, | |
222 | instructions, | |
223 | } | |
224 | } | |
225 | ||
226 | /// Get a reference to the header for this state machine's line number | |
227 | /// program. | |
228 | #[inline] | |
229 | pub fn header(&self) -> &LineProgramHeader<R, Offset> { | |
230 | self.program.header() | |
231 | } | |
232 | ||
233 | /// Parse and execute the next instructions in the line number program until | |
234 | /// another row in the line number matrix is computed. | |
235 | /// | |
236 | /// The freshly computed row is returned as `Ok(Some((header, row)))`. | |
237 | /// If the matrix is complete, and there are no more new rows in the line | |
238 | /// number matrix, then `Ok(None)` is returned. If there was an error parsing | |
239 | /// an instruction, then `Err(e)` is returned. | |
240 | /// | |
241 | /// Unfortunately, the references mean that this cannot be a | |
242 | /// `FallibleIterator`. | |
243 | pub fn next_row(&mut self) -> Result<Option<(&LineProgramHeader<R, Offset>, &LineRow)>> { | |
244 | // Perform any reset that was required after copying the previous row. | |
245 | self.row.reset(self.program.header()); | |
246 | ||
247 | loop { | |
248 | // Split the borrow here, rather than calling `self.header()`. | |
249 | match self.instructions.next_instruction(self.program.header()) { | |
250 | Err(err) => return Err(err), | |
251 | Ok(None) => return Ok(None), | |
252 | Ok(Some(instruction)) => { | |
253 | if self.row.execute(instruction, &mut self.program) { | |
254 | if self.row.tombstone { | |
255 | // Perform any reset that was required for the tombstone row. | |
256 | // Normally this is done when `next_row` is called again, but for | |
257 | // tombstones we loop immediately. | |
258 | self.row.reset(self.program.header()); | |
259 | } else { | |
260 | return Ok(Some((self.header(), &self.row))); | |
261 | } | |
262 | } | |
263 | // Fall through, parse the next instruction, and see if that | |
264 | // yields a row. | |
265 | } | |
266 | } | |
267 | } | |
268 | } | |
269 | } | |
270 | ||
271 | /// Deprecated. `Opcode` has been renamed to `LineInstruction`. | |
272 | #[deprecated(note = "Opcode has been renamed to LineInstruction, use that instead.")] | |
273 | pub type Opcode<R> = LineInstruction<R, <R as Reader>::Offset>; | |
274 | ||
275 | /// A parsed line number program instruction. | |
276 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] | |
277 | pub enum LineInstruction<R, Offset = <R as Reader>::Offset> | |
278 | where | |
279 | R: Reader<Offset = Offset>, | |
280 | Offset: ReaderOffset, | |
281 | { | |
282 | /// > ### 6.2.5.1 Special Opcodes | |
283 | /// > | |
284 | /// > Each ubyte special opcode has the following effect on the state machine: | |
285 | /// > | |
286 | /// > 1. Add a signed integer to the line register. | |
287 | /// > | |
288 | /// > 2. Modify the operation pointer by incrementing the address and | |
289 | /// > op_index registers as described below. | |
290 | /// > | |
291 | /// > 3. Append a row to the matrix using the current values of the state | |
292 | /// > machine registers. | |
293 | /// > | |
294 | /// > 4. Set the basic_block register to “false.” | |
295 | /// > | |
296 | /// > 5. Set the prologue_end register to “false.” | |
297 | /// > | |
298 | /// > 6. Set the epilogue_begin register to “false.” | |
299 | /// > | |
300 | /// > 7. Set the discriminator register to 0. | |
301 | /// > | |
302 | /// > All of the special opcodes do those same seven things; they differ from | |
303 | /// > one another only in what values they add to the line, address and | |
304 | /// > op_index registers. | |
305 | Special(u8), | |
306 | ||
307 | /// "[`LineInstruction::Copy`] appends a row to the matrix using the current | |
308 | /// values of the state machine registers. Then it sets the discriminator | |
309 | /// register to 0, and sets the basic_block, prologue_end and epilogue_begin | |
310 | /// registers to “false.”" | |
311 | Copy, | |
312 | ||
313 | /// "The DW_LNS_advance_pc opcode takes a single unsigned LEB128 operand as | |
314 | /// the operation advance and modifies the address and op_index registers | |
315 | /// [the same as `LineInstruction::Special`]" | |
316 | AdvancePc(u64), | |
317 | ||
318 | /// "The DW_LNS_advance_line opcode takes a single signed LEB128 operand and | |
319 | /// adds that value to the line register of the state machine." | |
320 | AdvanceLine(i64), | |
321 | ||
322 | /// "The DW_LNS_set_file opcode takes a single unsigned LEB128 operand and | |
323 | /// stores it in the file register of the state machine." | |
324 | SetFile(u64), | |
325 | ||
326 | /// "The DW_LNS_set_column opcode takes a single unsigned LEB128 operand and | |
327 | /// stores it in the column register of the state machine." | |
328 | SetColumn(u64), | |
329 | ||
330 | /// "The DW_LNS_negate_stmt opcode takes no operands. It sets the is_stmt | |
331 | /// register of the state machine to the logical negation of its current | |
332 | /// value." | |
333 | NegateStatement, | |
334 | ||
335 | /// "The DW_LNS_set_basic_block opcode takes no operands. It sets the | |
336 | /// basic_block register of the state machine to “true.”" | |
337 | SetBasicBlock, | |
338 | ||
339 | /// > The DW_LNS_const_add_pc opcode takes no operands. It advances the | |
340 | /// > address and op_index registers by the increments corresponding to | |
341 | /// > special opcode 255. | |
342 | /// > | |
343 | /// > When the line number program needs to advance the address by a small | |
344 | /// > amount, it can use a single special opcode, which occupies a single | |
345 | /// > byte. When it needs to advance the address by up to twice the range of | |
346 | /// > the last special opcode, it can use DW_LNS_const_add_pc followed by a | |
347 | /// > special opcode, for a total of two bytes. Only if it needs to advance | |
348 | /// > the address by more than twice that range will it need to use both | |
349 | /// > DW_LNS_advance_pc and a special opcode, requiring three or more bytes. | |
350 | ConstAddPc, | |
351 | ||
352 | /// > The DW_LNS_fixed_advance_pc opcode takes a single uhalf (unencoded) | |
353 | /// > operand and adds it to the address register of the state machine and | |
354 | /// > sets the op_index register to 0. This is the only standard opcode whose | |
355 | /// > operand is not a variable length number. It also does not multiply the | |
356 | /// > operand by the minimum_instruction_length field of the header. | |
357 | FixedAddPc(u16), | |
358 | ||
359 | /// "[`LineInstruction::SetPrologueEnd`] sets the prologue_end register to “true”." | |
360 | SetPrologueEnd, | |
361 | ||
362 | /// "[`LineInstruction::SetEpilogueBegin`] sets the epilogue_begin register to | |
363 | /// “true”." | |
364 | SetEpilogueBegin, | |
365 | ||
366 | /// "The DW_LNS_set_isa opcode takes a single unsigned LEB128 operand and | |
367 | /// stores that value in the isa register of the state machine." | |
368 | SetIsa(u64), | |
369 | ||
370 | /// An unknown standard opcode with zero operands. | |
371 | UnknownStandard0(constants::DwLns), | |
372 | ||
373 | /// An unknown standard opcode with one operand. | |
374 | UnknownStandard1(constants::DwLns, u64), | |
375 | ||
376 | /// An unknown standard opcode with multiple operands. | |
377 | UnknownStandardN(constants::DwLns, R), | |
378 | ||
379 | /// > [`LineInstruction::EndSequence`] sets the end_sequence register of the state | |
380 | /// > machine to “true” and appends a row to the matrix using the current | |
381 | /// > values of the state-machine registers. Then it resets the registers to | |
382 | /// > the initial values specified above (see Section 6.2.2). Every line | |
383 | /// > number program sequence must end with a DW_LNE_end_sequence instruction | |
384 | /// > which creates a row whose address is that of the byte after the last | |
385 | /// > target machine instruction of the sequence. | |
386 | EndSequence, | |
387 | ||
388 | /// > The DW_LNE_set_address opcode takes a single relocatable address as an | |
389 | /// > operand. The size of the operand is the size of an address on the target | |
390 | /// > machine. It sets the address register to the value given by the | |
391 | /// > relocatable address and sets the op_index register to 0. | |
392 | /// > | |
393 | /// > All of the other line number program opcodes that affect the address | |
394 | /// > register add a delta to it. This instruction stores a relocatable value | |
395 | /// > into it instead. | |
396 | SetAddress(u64), | |
397 | ||
398 | /// Defines a new source file in the line number program and appends it to | |
399 | /// the line number program header's list of source files. | |
400 | DefineFile(FileEntry<R, Offset>), | |
401 | ||
402 | /// "The DW_LNE_set_discriminator opcode takes a single parameter, an | |
403 | /// unsigned LEB128 integer. It sets the discriminator register to the new | |
404 | /// value." | |
405 | SetDiscriminator(u64), | |
406 | ||
407 | /// An unknown extended opcode and the slice of its unparsed operands. | |
408 | UnknownExtended(constants::DwLne, R), | |
409 | } | |
410 | ||
411 | impl<R, Offset> LineInstruction<R, Offset> | |
412 | where | |
413 | R: Reader<Offset = Offset>, | |
414 | Offset: ReaderOffset, | |
415 | { | |
416 | fn parse<'header>( | |
417 | header: &'header LineProgramHeader<R>, | |
418 | input: &mut R, | |
419 | ) -> Result<LineInstruction<R>> | |
420 | where | |
421 | R: 'header, | |
422 | { | |
423 | let opcode = input.read_u8()?; | |
424 | if opcode == 0 { | |
425 | let length = input.read_uleb128().and_then(R::Offset::from_u64)?; | |
426 | let mut instr_rest = input.split(length)?; | |
427 | let opcode = instr_rest.read_u8()?; | |
428 | ||
429 | match constants::DwLne(opcode) { | |
430 | constants::DW_LNE_end_sequence => Ok(LineInstruction::EndSequence), | |
431 | ||
432 | constants::DW_LNE_set_address => { | |
433 | let address = instr_rest.read_address(header.address_size())?; | |
434 | Ok(LineInstruction::SetAddress(address)) | |
435 | } | |
436 | ||
437 | constants::DW_LNE_define_file => { | |
438 | if header.version() <= 4 { | |
439 | let path_name = instr_rest.read_null_terminated_slice()?; | |
440 | let entry = FileEntry::parse(&mut instr_rest, path_name)?; | |
441 | Ok(LineInstruction::DefineFile(entry)) | |
442 | } else { | |
443 | Ok(LineInstruction::UnknownExtended( | |
444 | constants::DW_LNE_define_file, | |
445 | instr_rest, | |
446 | )) | |
447 | } | |
448 | } | |
449 | ||
450 | constants::DW_LNE_set_discriminator => { | |
451 | let discriminator = instr_rest.read_uleb128()?; | |
452 | Ok(LineInstruction::SetDiscriminator(discriminator)) | |
453 | } | |
454 | ||
455 | otherwise => Ok(LineInstruction::UnknownExtended(otherwise, instr_rest)), | |
456 | } | |
457 | } else if opcode >= header.opcode_base { | |
458 | Ok(LineInstruction::Special(opcode)) | |
459 | } else { | |
460 | match constants::DwLns(opcode) { | |
461 | constants::DW_LNS_copy => Ok(LineInstruction::Copy), | |
462 | ||
463 | constants::DW_LNS_advance_pc => { | |
464 | let advance = input.read_uleb128()?; | |
465 | Ok(LineInstruction::AdvancePc(advance)) | |
466 | } | |
467 | ||
468 | constants::DW_LNS_advance_line => { | |
469 | let increment = input.read_sleb128()?; | |
470 | Ok(LineInstruction::AdvanceLine(increment)) | |
471 | } | |
472 | ||
473 | constants::DW_LNS_set_file => { | |
474 | let file = input.read_uleb128()?; | |
475 | Ok(LineInstruction::SetFile(file)) | |
476 | } | |
477 | ||
478 | constants::DW_LNS_set_column => { | |
479 | let column = input.read_uleb128()?; | |
480 | Ok(LineInstruction::SetColumn(column)) | |
481 | } | |
482 | ||
483 | constants::DW_LNS_negate_stmt => Ok(LineInstruction::NegateStatement), | |
484 | ||
485 | constants::DW_LNS_set_basic_block => Ok(LineInstruction::SetBasicBlock), | |
486 | ||
487 | constants::DW_LNS_const_add_pc => Ok(LineInstruction::ConstAddPc), | |
488 | ||
489 | constants::DW_LNS_fixed_advance_pc => { | |
490 | let advance = input.read_u16()?; | |
491 | Ok(LineInstruction::FixedAddPc(advance)) | |
492 | } | |
493 | ||
494 | constants::DW_LNS_set_prologue_end => Ok(LineInstruction::SetPrologueEnd), | |
495 | ||
496 | constants::DW_LNS_set_epilogue_begin => Ok(LineInstruction::SetEpilogueBegin), | |
497 | ||
498 | constants::DW_LNS_set_isa => { | |
499 | let isa = input.read_uleb128()?; | |
500 | Ok(LineInstruction::SetIsa(isa)) | |
501 | } | |
502 | ||
503 | otherwise => { | |
504 | let mut opcode_lengths = header.standard_opcode_lengths().clone(); | |
505 | opcode_lengths.skip(R::Offset::from_u8(opcode - 1))?; | |
506 | let num_args = opcode_lengths.read_u8()? as usize; | |
507 | match num_args { | |
508 | 0 => Ok(LineInstruction::UnknownStandard0(otherwise)), | |
509 | 1 => { | |
510 | let arg = input.read_uleb128()?; | |
511 | Ok(LineInstruction::UnknownStandard1(otherwise, arg)) | |
512 | } | |
513 | _ => { | |
514 | let mut args = input.clone(); | |
515 | for _ in 0..num_args { | |
516 | input.read_uleb128()?; | |
517 | } | |
518 | let len = input.offset_from(&args); | |
519 | args.truncate(len)?; | |
520 | Ok(LineInstruction::UnknownStandardN(otherwise, args)) | |
521 | } | |
522 | } | |
523 | } | |
524 | } | |
525 | } | |
526 | } | |
527 | } | |
528 | ||
529 | impl<R, Offset> fmt::Display for LineInstruction<R, Offset> | |
530 | where | |
531 | R: Reader<Offset = Offset>, | |
532 | Offset: ReaderOffset, | |
533 | { | |
534 | fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> { | |
535 | match *self { | |
536 | LineInstruction::Special(opcode) => write!(f, "Special opcode {}", opcode), | |
537 | LineInstruction::Copy => write!(f, "{}", constants::DW_LNS_copy), | |
538 | LineInstruction::AdvancePc(advance) => { | |
539 | write!(f, "{} by {}", constants::DW_LNS_advance_pc, advance) | |
540 | } | |
541 | LineInstruction::AdvanceLine(increment) => { | |
542 | write!(f, "{} by {}", constants::DW_LNS_advance_line, increment) | |
543 | } | |
544 | LineInstruction::SetFile(file) => { | |
545 | write!(f, "{} to {}", constants::DW_LNS_set_file, file) | |
546 | } | |
547 | LineInstruction::SetColumn(column) => { | |
548 | write!(f, "{} to {}", constants::DW_LNS_set_column, column) | |
549 | } | |
550 | LineInstruction::NegateStatement => write!(f, "{}", constants::DW_LNS_negate_stmt), | |
551 | LineInstruction::SetBasicBlock => write!(f, "{}", constants::DW_LNS_set_basic_block), | |
552 | LineInstruction::ConstAddPc => write!(f, "{}", constants::DW_LNS_const_add_pc), | |
553 | LineInstruction::FixedAddPc(advance) => { | |
554 | write!(f, "{} by {}", constants::DW_LNS_fixed_advance_pc, advance) | |
555 | } | |
556 | LineInstruction::SetPrologueEnd => write!(f, "{}", constants::DW_LNS_set_prologue_end), | |
557 | LineInstruction::SetEpilogueBegin => { | |
558 | write!(f, "{}", constants::DW_LNS_set_epilogue_begin) | |
559 | } | |
560 | LineInstruction::SetIsa(isa) => write!(f, "{} to {}", constants::DW_LNS_set_isa, isa), | |
561 | LineInstruction::UnknownStandard0(opcode) => write!(f, "Unknown {}", opcode), | |
562 | LineInstruction::UnknownStandard1(opcode, arg) => { | |
563 | write!(f, "Unknown {} with operand {}", opcode, arg) | |
564 | } | |
565 | LineInstruction::UnknownStandardN(opcode, ref args) => { | |
566 | write!(f, "Unknown {} with operands {:?}", opcode, args) | |
567 | } | |
568 | LineInstruction::EndSequence => write!(f, "{}", constants::DW_LNE_end_sequence), | |
569 | LineInstruction::SetAddress(address) => { | |
570 | write!(f, "{} to {}", constants::DW_LNE_set_address, address) | |
571 | } | |
572 | LineInstruction::DefineFile(_) => write!(f, "{}", constants::DW_LNE_define_file), | |
573 | LineInstruction::SetDiscriminator(discr) => { | |
574 | write!(f, "{} to {}", constants::DW_LNE_set_discriminator, discr) | |
575 | } | |
576 | LineInstruction::UnknownExtended(opcode, _) => write!(f, "Unknown {}", opcode), | |
577 | } | |
578 | } | |
579 | } | |
580 | ||
581 | /// Deprecated. `OpcodesIter` has been renamed to `LineInstructions`. | |
582 | #[deprecated(note = "OpcodesIter has been renamed to LineInstructions, use that instead.")] | |
583 | pub type OpcodesIter<R> = LineInstructions<R>; | |
584 | ||
585 | /// An iterator yielding parsed instructions. | |
586 | /// | |
587 | /// See | |
588 | /// [`LineProgramHeader::instructions`](./struct.LineProgramHeader.html#method.instructions) | |
589 | /// for more details. | |
590 | #[derive(Clone, Debug)] | |
591 | pub struct LineInstructions<R: Reader> { | |
592 | input: R, | |
593 | } | |
594 | ||
595 | impl<R: Reader> LineInstructions<R> { | |
596 | fn remove_trailing(&self, other: &LineInstructions<R>) -> Result<LineInstructions<R>> { | |
597 | let offset = other.input.offset_from(&self.input); | |
598 | let mut input = self.input.clone(); | |
599 | input.truncate(offset)?; | |
600 | Ok(LineInstructions { input }) | |
601 | } | |
602 | } | |
603 | ||
604 | impl<R: Reader> LineInstructions<R> { | |
605 | /// Advance the iterator and return the next instruction. | |
606 | /// | |
607 | /// Returns the newly parsed instruction as `Ok(Some(instruction))`. Returns | |
608 | /// `Ok(None)` when iteration is complete and all instructions have already been | |
609 | /// parsed and yielded. If an error occurs while parsing the next attribute, | |
610 | /// then this error is returned as `Err(e)`, and all subsequent calls return | |
611 | /// `Ok(None)`. | |
612 | /// | |
613 | /// Unfortunately, the `header` parameter means that this cannot be a | |
614 | /// `FallibleIterator`. | |
615 | #[inline(always)] | |
616 | pub fn next_instruction( | |
617 | &mut self, | |
618 | header: &LineProgramHeader<R>, | |
619 | ) -> Result<Option<LineInstruction<R>>> { | |
620 | if self.input.is_empty() { | |
621 | return Ok(None); | |
622 | } | |
623 | ||
624 | match LineInstruction::parse(header, &mut self.input) { | |
625 | Ok(instruction) => Ok(Some(instruction)), | |
626 | Err(e) => { | |
627 | self.input.empty(); | |
628 | Err(e) | |
629 | } | |
630 | } | |
631 | } | |
632 | } | |
633 | ||
634 | /// Deprecated. `LineNumberRow` has been renamed to `LineRow`. | |
635 | #[deprecated(note = "LineNumberRow has been renamed to LineRow, use that instead.")] | |
636 | pub type LineNumberRow = LineRow; | |
637 | ||
638 | /// A row in the line number program's resulting matrix. | |
639 | /// | |
640 | /// Each row is a copy of the registers of the state machine, as defined in section 6.2.2. | |
641 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] | |
642 | pub struct LineRow { | |
643 | tombstone: bool, | |
644 | address: Wrapping<u64>, | |
645 | op_index: Wrapping<u64>, | |
646 | file: u64, | |
647 | line: Wrapping<u64>, | |
648 | column: u64, | |
649 | is_stmt: bool, | |
650 | basic_block: bool, | |
651 | end_sequence: bool, | |
652 | prologue_end: bool, | |
653 | epilogue_begin: bool, | |
654 | isa: u64, | |
655 | discriminator: u64, | |
656 | } | |
657 | ||
658 | impl LineRow { | |
659 | /// Create a line number row in the initial state for the given program. | |
660 | pub fn new<R: Reader>(header: &LineProgramHeader<R>) -> Self { | |
661 | LineRow { | |
662 | // "At the beginning of each sequence within a line number program, the | |
663 | // state of the registers is:" -- Section 6.2.2 | |
664 | tombstone: false, | |
665 | address: Wrapping(0), | |
666 | op_index: Wrapping(0), | |
667 | file: 1, | |
668 | line: Wrapping(1), | |
669 | column: 0, | |
670 | // "determined by default_is_stmt in the line number program header" | |
671 | is_stmt: header.line_encoding.default_is_stmt, | |
672 | basic_block: false, | |
673 | end_sequence: false, | |
674 | prologue_end: false, | |
675 | epilogue_begin: false, | |
676 | // "The isa value 0 specifies that the instruction set is the | |
677 | // architecturally determined default instruction set. This may be fixed | |
678 | // by the ABI, or it may be specified by other means, for example, by | |
679 | // the object file description." | |
680 | isa: 0, | |
681 | discriminator: 0, | |
682 | } | |
683 | } | |
684 | ||
685 | /// "The program-counter value corresponding to a machine instruction | |
686 | /// generated by the compiler." | |
687 | #[inline] | |
688 | pub fn address(&self) -> u64 { | |
689 | self.address.0 | |
690 | } | |
691 | ||
692 | /// > An unsigned integer representing the index of an operation within a VLIW | |
693 | /// > instruction. The index of the first operation is 0. For non-VLIW | |
694 | /// > architectures, this register will always be 0. | |
695 | /// > | |
696 | /// > The address and op_index registers, taken together, form an operation | |
697 | /// > pointer that can reference any individual operation with the | |
698 | /// > instruction stream. | |
699 | #[inline] | |
700 | pub fn op_index(&self) -> u64 { | |
701 | self.op_index.0 | |
702 | } | |
703 | ||
704 | /// "An unsigned integer indicating the identity of the source file | |
705 | /// corresponding to a machine instruction." | |
706 | #[inline] | |
707 | pub fn file_index(&self) -> u64 { | |
708 | self.file | |
709 | } | |
710 | ||
711 | /// The source file corresponding to the current machine instruction. | |
712 | #[inline] | |
713 | pub fn file<'header, R: Reader>( | |
714 | &self, | |
715 | header: &'header LineProgramHeader<R>, | |
716 | ) -> Option<&'header FileEntry<R>> { | |
717 | header.file(self.file) | |
718 | } | |
719 | ||
720 | /// "An unsigned integer indicating a source line number. Lines are numbered | |
721 | /// beginning at 1. The compiler may emit the value 0 in cases where an | |
722 | /// instruction cannot be attributed to any source line." | |
723 | /// Line number values of 0 are represented as `None`. | |
724 | #[inline] | |
725 | pub fn line(&self) -> Option<NonZeroU64> { | |
726 | NonZeroU64::new(self.line.0) | |
727 | } | |
728 | ||
729 | /// "An unsigned integer indicating a column number within a source | |
730 | /// line. Columns are numbered beginning at 1. The value 0 is reserved to | |
731 | /// indicate that a statement begins at the “left edge” of the line." | |
732 | #[inline] | |
733 | pub fn column(&self) -> ColumnType { | |
734 | NonZeroU64::new(self.column) | |
735 | .map(ColumnType::Column) | |
736 | .unwrap_or(ColumnType::LeftEdge) | |
737 | } | |
738 | ||
739 | /// "A boolean indicating that the current instruction is a recommended | |
740 | /// breakpoint location. A recommended breakpoint location is intended to | |
741 | /// “represent” a line, a statement and/or a semantically distinct subpart | |
742 | /// of a statement." | |
743 | #[inline] | |
744 | pub fn is_stmt(&self) -> bool { | |
745 | self.is_stmt | |
746 | } | |
747 | ||
748 | /// "A boolean indicating that the current instruction is the beginning of a | |
749 | /// basic block." | |
750 | #[inline] | |
751 | pub fn basic_block(&self) -> bool { | |
752 | self.basic_block | |
753 | } | |
754 | ||
755 | /// "A boolean indicating that the current address is that of the first byte | |
756 | /// after the end of a sequence of target machine instructions. end_sequence | |
757 | /// terminates a sequence of lines; therefore other information in the same | |
758 | /// row is not meaningful." | |
759 | #[inline] | |
760 | pub fn end_sequence(&self) -> bool { | |
761 | self.end_sequence | |
762 | } | |
763 | ||
764 | /// "A boolean indicating that the current address is one (of possibly many) | |
765 | /// where execution should be suspended for an entry breakpoint of a | |
766 | /// function." | |
767 | #[inline] | |
768 | pub fn prologue_end(&self) -> bool { | |
769 | self.prologue_end | |
770 | } | |
771 | ||
772 | /// "A boolean indicating that the current address is one (of possibly many) | |
773 | /// where execution should be suspended for an exit breakpoint of a | |
774 | /// function." | |
775 | #[inline] | |
776 | pub fn epilogue_begin(&self) -> bool { | |
777 | self.epilogue_begin | |
778 | } | |
779 | ||
780 | /// Tag for the current instruction set architecture. | |
781 | /// | |
782 | /// > An unsigned integer whose value encodes the applicable instruction set | |
783 | /// > architecture for the current instruction. | |
784 | /// > | |
785 | /// > The encoding of instruction sets should be shared by all users of a | |
786 | /// > given architecture. It is recommended that this encoding be defined by | |
787 | /// > the ABI authoring committee for each architecture. | |
788 | #[inline] | |
789 | pub fn isa(&self) -> u64 { | |
790 | self.isa | |
791 | } | |
792 | ||
793 | /// "An unsigned integer identifying the block to which the current | |
794 | /// instruction belongs. Discriminator values are assigned arbitrarily by | |
795 | /// the DWARF producer and serve to distinguish among multiple blocks that | |
796 | /// may all be associated with the same source file, line, and column. Where | |
797 | /// only one block exists for a given source position, the discriminator | |
798 | /// value should be zero." | |
799 | #[inline] | |
800 | pub fn discriminator(&self) -> u64 { | |
801 | self.discriminator | |
802 | } | |
803 | ||
804 | /// Execute the given instruction, and return true if a new row in the | |
805 | /// line number matrix needs to be generated. | |
806 | /// | |
807 | /// Unknown opcodes are treated as no-ops. | |
808 | #[inline] | |
809 | pub fn execute<R, Program>( | |
810 | &mut self, | |
811 | instruction: LineInstruction<R>, | |
812 | program: &mut Program, | |
813 | ) -> bool | |
814 | where | |
815 | Program: LineProgram<R>, | |
816 | R: Reader, | |
817 | { | |
818 | match instruction { | |
819 | LineInstruction::Special(opcode) => { | |
820 | self.exec_special_opcode(opcode, program.header()); | |
821 | true | |
822 | } | |
823 | ||
824 | LineInstruction::Copy => true, | |
825 | ||
826 | LineInstruction::AdvancePc(operation_advance) => { | |
827 | self.apply_operation_advance(operation_advance, program.header()); | |
828 | false | |
829 | } | |
830 | ||
831 | LineInstruction::AdvanceLine(line_increment) => { | |
832 | self.apply_line_advance(line_increment); | |
833 | false | |
834 | } | |
835 | ||
836 | LineInstruction::SetFile(file) => { | |
837 | self.file = file; | |
838 | false | |
839 | } | |
840 | ||
841 | LineInstruction::SetColumn(column) => { | |
842 | self.column = column; | |
843 | false | |
844 | } | |
845 | ||
846 | LineInstruction::NegateStatement => { | |
847 | self.is_stmt = !self.is_stmt; | |
848 | false | |
849 | } | |
850 | ||
851 | LineInstruction::SetBasicBlock => { | |
852 | self.basic_block = true; | |
853 | false | |
854 | } | |
855 | ||
856 | LineInstruction::ConstAddPc => { | |
857 | let adjusted = self.adjust_opcode(255, program.header()); | |
858 | let operation_advance = adjusted / program.header().line_encoding.line_range; | |
859 | self.apply_operation_advance(u64::from(operation_advance), program.header()); | |
860 | false | |
861 | } | |
862 | ||
863 | LineInstruction::FixedAddPc(operand) => { | |
864 | self.address += Wrapping(u64::from(operand)); | |
865 | self.op_index.0 = 0; | |
866 | false | |
867 | } | |
868 | ||
869 | LineInstruction::SetPrologueEnd => { | |
870 | self.prologue_end = true; | |
871 | false | |
872 | } | |
873 | ||
874 | LineInstruction::SetEpilogueBegin => { | |
875 | self.epilogue_begin = true; | |
876 | false | |
877 | } | |
878 | ||
879 | LineInstruction::SetIsa(isa) => { | |
880 | self.isa = isa; | |
881 | false | |
882 | } | |
883 | ||
884 | LineInstruction::EndSequence => { | |
885 | self.end_sequence = true; | |
886 | true | |
887 | } | |
888 | ||
889 | LineInstruction::SetAddress(address) => { | |
890 | let tombstone_address = !0 >> (64 - program.header().encoding.address_size * 8); | |
891 | self.tombstone = address == tombstone_address; | |
892 | self.address.0 = address; | |
893 | self.op_index.0 = 0; | |
894 | false | |
895 | } | |
896 | ||
897 | LineInstruction::DefineFile(entry) => { | |
898 | program.add_file(entry); | |
899 | false | |
900 | } | |
901 | ||
902 | LineInstruction::SetDiscriminator(discriminator) => { | |
903 | self.discriminator = discriminator; | |
904 | false | |
905 | } | |
906 | ||
907 | // Compatibility with future opcodes. | |
908 | LineInstruction::UnknownStandard0(_) | |
909 | | LineInstruction::UnknownStandard1(_, _) | |
910 | | LineInstruction::UnknownStandardN(_, _) | |
911 | | LineInstruction::UnknownExtended(_, _) => false, | |
912 | } | |
913 | } | |
914 | ||
915 | /// Perform any reset that was required after copying the previous row. | |
916 | #[inline] | |
917 | pub fn reset<R: Reader>(&mut self, header: &LineProgramHeader<R>) { | |
918 | if self.end_sequence { | |
919 | // Previous instruction was EndSequence, so reset everything | |
920 | // as specified in Section 6.2.5.3. | |
921 | *self = Self::new(header); | |
922 | } else { | |
923 | // Previous instruction was one of: | |
924 | // - Special - specified in Section 6.2.5.1, steps 4-7 | |
925 | // - Copy - specified in Section 6.2.5.2 | |
926 | // The reset behaviour is the same in both cases. | |
927 | self.discriminator = 0; | |
928 | self.basic_block = false; | |
929 | self.prologue_end = false; | |
930 | self.epilogue_begin = false; | |
931 | } | |
932 | } | |
933 | ||
934 | /// Step 1 of section 6.2.5.1 | |
935 | fn apply_line_advance(&mut self, line_increment: i64) { | |
936 | if line_increment < 0 { | |
937 | let decrement = -line_increment as u64; | |
938 | if decrement <= self.line.0 { | |
939 | self.line.0 -= decrement; | |
940 | } else { | |
941 | self.line.0 = 0; | |
942 | } | |
943 | } else { | |
944 | self.line += Wrapping(line_increment as u64); | |
945 | } | |
946 | } | |
947 | ||
948 | /// Step 2 of section 6.2.5.1 | |
949 | fn apply_operation_advance<R: Reader>( | |
950 | &mut self, | |
951 | operation_advance: u64, | |
952 | header: &LineProgramHeader<R>, | |
953 | ) { | |
954 | let operation_advance = Wrapping(operation_advance); | |
955 | ||
956 | let minimum_instruction_length = u64::from(header.line_encoding.minimum_instruction_length); | |
957 | let minimum_instruction_length = Wrapping(minimum_instruction_length); | |
958 | ||
959 | let maximum_operations_per_instruction = | |
960 | u64::from(header.line_encoding.maximum_operations_per_instruction); | |
961 | let maximum_operations_per_instruction = Wrapping(maximum_operations_per_instruction); | |
962 | ||
963 | if maximum_operations_per_instruction.0 == 1 { | |
964 | self.address += minimum_instruction_length * operation_advance; | |
965 | self.op_index.0 = 0; | |
966 | } else { | |
967 | let op_index_with_advance = self.op_index + operation_advance; | |
968 | self.address += minimum_instruction_length | |
969 | * (op_index_with_advance / maximum_operations_per_instruction); | |
970 | self.op_index = op_index_with_advance % maximum_operations_per_instruction; | |
971 | } | |
972 | } | |
973 | ||
974 | #[inline] | |
975 | fn adjust_opcode<R: Reader>(&self, opcode: u8, header: &LineProgramHeader<R>) -> u8 { | |
976 | opcode - header.opcode_base | |
977 | } | |
978 | ||
979 | /// Section 6.2.5.1 | |
980 | fn exec_special_opcode<R: Reader>(&mut self, opcode: u8, header: &LineProgramHeader<R>) { | |
981 | let adjusted_opcode = self.adjust_opcode(opcode, header); | |
982 | ||
983 | let line_range = header.line_encoding.line_range; | |
984 | let line_advance = adjusted_opcode % line_range; | |
985 | let operation_advance = adjusted_opcode / line_range; | |
986 | ||
987 | // Step 1 | |
988 | let line_base = i64::from(header.line_encoding.line_base); | |
989 | self.apply_line_advance(line_base + i64::from(line_advance)); | |
990 | ||
991 | // Step 2 | |
992 | self.apply_operation_advance(u64::from(operation_advance), header); | |
993 | } | |
994 | } | |
995 | ||
996 | /// The type of column that a row is referring to. | |
997 | #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] | |
998 | pub enum ColumnType { | |
999 | /// The `LeftEdge` means that the statement begins at the start of the new | |
1000 | /// line. | |
1001 | LeftEdge, | |
1002 | /// A column number, whose range begins at 1. | |
1003 | Column(NonZeroU64), | |
1004 | } | |
1005 | ||
1006 | /// Deprecated. `LineNumberSequence` has been renamed to `LineSequence`. | |
1007 | #[deprecated(note = "LineNumberSequence has been renamed to LineSequence, use that instead.")] | |
1008 | pub type LineNumberSequence<R> = LineSequence<R>; | |
1009 | ||
1010 | /// A sequence within a line number program. A sequence, as defined in section | |
1011 | /// 6.2.5 of the standard, is a linear subset of a line number program within | |
1012 | /// which addresses are monotonically increasing. | |
1013 | #[derive(Clone, Debug)] | |
1014 | pub struct LineSequence<R: Reader> { | |
1015 | /// The first address that is covered by this sequence within the line number | |
1016 | /// program. | |
1017 | pub start: u64, | |
1018 | /// The first address that is *not* covered by this sequence within the line | |
1019 | /// number program. | |
1020 | pub end: u64, | |
1021 | instructions: LineInstructions<R>, | |
1022 | } | |
1023 | ||
1024 | /// Deprecated. `LineNumberProgramHeader` has been renamed to `LineProgramHeader`. | |
1025 | #[deprecated( | |
1026 | note = "LineNumberProgramHeader has been renamed to LineProgramHeader, use that instead." | |
1027 | )] | |
1028 | pub type LineNumberProgramHeader<R, Offset> = LineProgramHeader<R, Offset>; | |
1029 | ||
1030 | /// A header for a line number program in the `.debug_line` section, as defined | |
1031 | /// in section 6.2.4 of the standard. | |
1032 | #[derive(Clone, Debug, Eq, PartialEq)] | |
1033 | pub struct LineProgramHeader<R, Offset = <R as Reader>::Offset> | |
1034 | where | |
1035 | R: Reader<Offset = Offset>, | |
1036 | Offset: ReaderOffset, | |
1037 | { | |
1038 | encoding: Encoding, | |
1039 | offset: DebugLineOffset<Offset>, | |
1040 | unit_length: Offset, | |
1041 | ||
1042 | header_length: Offset, | |
1043 | ||
1044 | line_encoding: LineEncoding, | |
1045 | ||
1046 | /// "The number assigned to the first special opcode." | |
1047 | opcode_base: u8, | |
1048 | ||
1049 | /// "This array specifies the number of LEB128 operands for each of the | |
1050 | /// standard opcodes. The first element of the array corresponds to the | |
1051 | /// opcode whose value is 1, and the last element corresponds to the opcode | |
1052 | /// whose value is `opcode_base - 1`." | |
1053 | standard_opcode_lengths: R, | |
1054 | ||
1055 | /// "A sequence of directory entry format descriptions." | |
1056 | directory_entry_format: Vec<FileEntryFormat>, | |
1057 | ||
1058 | /// > Entries in this sequence describe each path that was searched for | |
1059 | /// > included source files in this compilation. (The paths include those | |
1060 | /// > directories specified explicitly by the user for the compiler to search | |
1061 | /// > and those the compiler searches without explicit direction.) Each path | |
1062 | /// > entry is either a full path name or is relative to the current directory | |
1063 | /// > of the compilation. | |
1064 | /// > | |
1065 | /// > The last entry is followed by a single null byte. | |
1066 | include_directories: Vec<AttributeValue<R, Offset>>, | |
1067 | ||
1068 | /// "A sequence of file entry format descriptions." | |
1069 | file_name_entry_format: Vec<FileEntryFormat>, | |
1070 | ||
1071 | /// "Entries in this sequence describe source files that contribute to the | |
1072 | /// line number information for this compilation unit or is used in other | |
1073 | /// contexts." | |
1074 | file_names: Vec<FileEntry<R, Offset>>, | |
1075 | ||
1076 | /// The encoded line program instructions. | |
1077 | program_buf: R, | |
1078 | ||
1079 | /// The current directory of the compilation. | |
1080 | comp_dir: Option<R>, | |
1081 | ||
1082 | /// The primary source file. | |
1083 | comp_file: Option<FileEntry<R, Offset>>, | |
1084 | } | |
1085 | ||
1086 | impl<R, Offset> LineProgramHeader<R, Offset> | |
1087 | where | |
1088 | R: Reader<Offset = Offset>, | |
1089 | Offset: ReaderOffset, | |
1090 | { | |
1091 | /// Return the offset of the line number program header in the `.debug_line` section. | |
1092 | pub fn offset(&self) -> DebugLineOffset<R::Offset> { | |
1093 | self.offset | |
1094 | } | |
1095 | ||
1096 | /// Return the length of the line number program and header, not including | |
1097 | /// the length of the encoded length itself. | |
1098 | pub fn unit_length(&self) -> R::Offset { | |
1099 | self.unit_length | |
1100 | } | |
1101 | ||
1102 | /// Return the encoding parameters for this header's line program. | |
1103 | pub fn encoding(&self) -> Encoding { | |
1104 | self.encoding | |
1105 | } | |
1106 | ||
1107 | /// Get the version of this header's line program. | |
1108 | pub fn version(&self) -> u16 { | |
1109 | self.encoding.version | |
1110 | } | |
1111 | ||
1112 | /// Get the length of the encoded line number program header, not including | |
1113 | /// the length of the encoded length itself. | |
1114 | pub fn header_length(&self) -> R::Offset { | |
1115 | self.header_length | |
1116 | } | |
1117 | ||
1118 | /// Get the size in bytes of a target machine address. | |
1119 | pub fn address_size(&self) -> u8 { | |
1120 | self.encoding.address_size | |
1121 | } | |
1122 | ||
1123 | /// Whether this line program is encoded in 64- or 32-bit DWARF. | |
1124 | pub fn format(&self) -> Format { | |
1125 | self.encoding.format | |
1126 | } | |
1127 | ||
1128 | /// Get the line encoding parameters for this header's line program. | |
1129 | pub fn line_encoding(&self) -> LineEncoding { | |
1130 | self.line_encoding | |
1131 | } | |
1132 | ||
1133 | /// Get the minimum instruction length any instruction in this header's line | |
1134 | /// program may have. | |
1135 | pub fn minimum_instruction_length(&self) -> u8 { | |
1136 | self.line_encoding.minimum_instruction_length | |
1137 | } | |
1138 | ||
1139 | /// Get the maximum number of operations each instruction in this header's | |
1140 | /// line program may have. | |
1141 | pub fn maximum_operations_per_instruction(&self) -> u8 { | |
1142 | self.line_encoding.maximum_operations_per_instruction | |
1143 | } | |
1144 | ||
1145 | /// Get the default value of the `is_stmt` register for this header's line | |
1146 | /// program. | |
1147 | pub fn default_is_stmt(&self) -> bool { | |
1148 | self.line_encoding.default_is_stmt | |
1149 | } | |
1150 | ||
1151 | /// Get the line base for this header's line program. | |
1152 | pub fn line_base(&self) -> i8 { | |
1153 | self.line_encoding.line_base | |
1154 | } | |
1155 | ||
1156 | /// Get the line range for this header's line program. | |
1157 | pub fn line_range(&self) -> u8 { | |
1158 | self.line_encoding.line_range | |
1159 | } | |
1160 | ||
1161 | /// Get opcode base for this header's line program. | |
1162 | pub fn opcode_base(&self) -> u8 { | |
1163 | self.opcode_base | |
1164 | } | |
1165 | ||
1166 | /// An array of `u8` that specifies the number of LEB128 operands for | |
1167 | /// each of the standard opcodes. | |
1168 | pub fn standard_opcode_lengths(&self) -> &R { | |
1169 | &self.standard_opcode_lengths | |
1170 | } | |
1171 | ||
1172 | /// Get the format of a directory entry. | |
1173 | pub fn directory_entry_format(&self) -> &[FileEntryFormat] { | |
1174 | &self.directory_entry_format[..] | |
1175 | } | |
1176 | ||
1177 | /// Get the set of include directories for this header's line program. | |
1178 | /// | |
1179 | /// For DWARF version <= 4, the compilation's current directory is not included | |
1180 | /// in the return value, but is implicitly considered to be in the set per spec. | |
1181 | pub fn include_directories(&self) -> &[AttributeValue<R, Offset>] { | |
1182 | &self.include_directories[..] | |
1183 | } | |
1184 | ||
1185 | /// The include directory with the given directory index. | |
1186 | /// | |
1187 | /// A directory index of 0 corresponds to the compilation unit directory. | |
1188 | pub fn directory(&self, directory: u64) -> Option<AttributeValue<R, Offset>> { | |
1189 | if self.encoding.version <= 4 { | |
1190 | if directory == 0 { | |
1191 | self.comp_dir.clone().map(AttributeValue::String) | |
1192 | } else { | |
1193 | let directory = directory as usize - 1; | |
1194 | self.include_directories.get(directory).cloned() | |
1195 | } | |
1196 | } else { | |
1197 | self.include_directories.get(directory as usize).cloned() | |
1198 | } | |
1199 | } | |
1200 | ||
1201 | /// Get the format of a file name entry. | |
1202 | pub fn file_name_entry_format(&self) -> &[FileEntryFormat] { | |
1203 | &self.file_name_entry_format[..] | |
1204 | } | |
1205 | ||
1206 | /// Return true if the file entries may have valid timestamps. | |
1207 | /// | |
1208 | /// Only returns false if we definitely know that all timestamp fields | |
1209 | /// are invalid. | |
1210 | pub fn file_has_timestamp(&self) -> bool { | |
1211 | self.encoding.version <= 4 | |
1212 | || self | |
1213 | .file_name_entry_format | |
1214 | .iter() | |
1215 | .any(|x| x.content_type == constants::DW_LNCT_timestamp) | |
1216 | } | |
1217 | ||
1218 | /// Return true if the file entries may have valid sizes. | |
1219 | /// | |
1220 | /// Only returns false if we definitely know that all size fields | |
1221 | /// are invalid. | |
1222 | pub fn file_has_size(&self) -> bool { | |
1223 | self.encoding.version <= 4 | |
1224 | || self | |
1225 | .file_name_entry_format | |
1226 | .iter() | |
1227 | .any(|x| x.content_type == constants::DW_LNCT_size) | |
1228 | } | |
1229 | ||
1230 | /// Return true if the file name entry format contains an MD5 field. | |
1231 | pub fn file_has_md5(&self) -> bool { | |
1232 | self.file_name_entry_format | |
1233 | .iter() | |
1234 | .any(|x| x.content_type == constants::DW_LNCT_MD5) | |
1235 | } | |
1236 | ||
1237 | /// Get the list of source files that appear in this header's line program. | |
1238 | pub fn file_names(&self) -> &[FileEntry<R, Offset>] { | |
1239 | &self.file_names[..] | |
1240 | } | |
1241 | ||
1242 | /// The source file with the given file index. | |
1243 | /// | |
1244 | /// A file index of 0 corresponds to the compilation unit file. | |
1245 | /// Note that a file index of 0 is invalid for DWARF version <= 4, | |
1246 | /// but we support it anyway. | |
1247 | pub fn file(&self, file: u64) -> Option<&FileEntry<R, Offset>> { | |
1248 | if self.encoding.version <= 4 { | |
1249 | if file == 0 { | |
1250 | self.comp_file.as_ref() | |
1251 | } else { | |
1252 | let file = file as usize - 1; | |
1253 | self.file_names.get(file) | |
1254 | } | |
1255 | } else { | |
1256 | self.file_names.get(file as usize) | |
1257 | } | |
1258 | } | |
1259 | ||
1260 | /// Get the raw, un-parsed `EndianSlice` containing this header's line number | |
1261 | /// program. | |
1262 | /// | |
1263 | /// ``` | |
1264 | /// # fn foo() { | |
1265 | /// use gimli::{LineProgramHeader, EndianSlice, NativeEndian}; | |
1266 | /// | |
1267 | /// fn get_line_number_program_header<'a>() -> LineProgramHeader<EndianSlice<'a, NativeEndian>> { | |
1268 | /// // Get a line number program header from some offset in a | |
1269 | /// // `.debug_line` section... | |
1270 | /// # unimplemented!() | |
1271 | /// } | |
1272 | /// | |
1273 | /// let header = get_line_number_program_header(); | |
1274 | /// let raw_program = header.raw_program_buf(); | |
1275 | /// println!("The length of the raw program in bytes is {}", raw_program.len()); | |
1276 | /// # } | |
1277 | /// ``` | |
1278 | pub fn raw_program_buf(&self) -> R { | |
1279 | self.program_buf.clone() | |
1280 | } | |
1281 | ||
1282 | /// Iterate over the instructions in this header's line number program, parsing | |
1283 | /// them as we go. | |
1284 | pub fn instructions(&self) -> LineInstructions<R> { | |
1285 | LineInstructions { | |
1286 | input: self.program_buf.clone(), | |
1287 | } | |
1288 | } | |
1289 | ||
1290 | fn parse( | |
1291 | input: &mut R, | |
1292 | offset: DebugLineOffset<Offset>, | |
1293 | mut address_size: u8, | |
1294 | mut comp_dir: Option<R>, | |
1295 | comp_name: Option<R>, | |
1296 | ) -> Result<LineProgramHeader<R, Offset>> { | |
1297 | let (unit_length, format) = input.read_initial_length()?; | |
1298 | let rest = &mut input.split(unit_length)?; | |
1299 | ||
1300 | let version = rest.read_u16()?; | |
1301 | if version < 2 || version > 5 { | |
1302 | return Err(Error::UnknownVersion(u64::from(version))); | |
1303 | } | |
1304 | ||
1305 | if version >= 5 { | |
1306 | address_size = rest.read_u8()?; | |
1307 | let segment_selector_size = rest.read_u8()?; | |
1308 | if segment_selector_size != 0 { | |
1309 | return Err(Error::UnsupportedSegmentSize); | |
1310 | } | |
1311 | } | |
1312 | ||
1313 | let encoding = Encoding { | |
1314 | format, | |
1315 | version, | |
1316 | address_size, | |
1317 | }; | |
1318 | ||
1319 | let header_length = rest.read_length(format)?; | |
1320 | ||
1321 | let mut program_buf = rest.clone(); | |
1322 | program_buf.skip(header_length)?; | |
1323 | rest.truncate(header_length)?; | |
1324 | ||
1325 | let minimum_instruction_length = rest.read_u8()?; | |
1326 | if minimum_instruction_length == 0 { | |
1327 | return Err(Error::MinimumInstructionLengthZero); | |
1328 | } | |
1329 | ||
1330 | // This field did not exist before DWARF 4, but is specified to be 1 for | |
1331 | // non-VLIW architectures, which makes it a no-op. | |
1332 | let maximum_operations_per_instruction = if version >= 4 { rest.read_u8()? } else { 1 }; | |
1333 | if maximum_operations_per_instruction == 0 { | |
1334 | return Err(Error::MaximumOperationsPerInstructionZero); | |
1335 | } | |
1336 | ||
1337 | let default_is_stmt = rest.read_u8()? != 0; | |
1338 | let line_base = rest.read_i8()?; | |
1339 | let line_range = rest.read_u8()?; | |
1340 | if line_range == 0 { | |
1341 | return Err(Error::LineRangeZero); | |
1342 | } | |
1343 | let line_encoding = LineEncoding { | |
1344 | minimum_instruction_length, | |
1345 | maximum_operations_per_instruction, | |
1346 | default_is_stmt, | |
1347 | line_base, | |
1348 | line_range, | |
1349 | }; | |
1350 | ||
1351 | let opcode_base = rest.read_u8()?; | |
1352 | if opcode_base == 0 { | |
1353 | return Err(Error::OpcodeBaseZero); | |
1354 | } | |
1355 | ||
1356 | let standard_opcode_count = R::Offset::from_u8(opcode_base - 1); | |
1357 | let standard_opcode_lengths = rest.split(standard_opcode_count)?; | |
1358 | ||
1359 | let directory_entry_format; | |
1360 | let mut include_directories = Vec::new(); | |
1361 | if version <= 4 { | |
1362 | directory_entry_format = Vec::new(); | |
1363 | loop { | |
1364 | let directory = rest.read_null_terminated_slice()?; | |
1365 | if directory.is_empty() { | |
1366 | break; | |
1367 | } | |
1368 | include_directories.push(AttributeValue::String(directory)); | |
1369 | } | |
1370 | } else { | |
1371 | comp_dir = None; | |
1372 | directory_entry_format = FileEntryFormat::parse(rest)?; | |
1373 | let count = rest.read_uleb128()?; | |
1374 | for _ in 0..count { | |
1375 | include_directories.push(parse_directory_v5( | |
1376 | rest, | |
1377 | encoding, | |
1378 | &directory_entry_format, | |
1379 | )?); | |
1380 | } | |
1381 | } | |
1382 | ||
1383 | let comp_file; | |
1384 | let file_name_entry_format; | |
1385 | let mut file_names = Vec::new(); | |
1386 | if version <= 4 { | |
1387 | comp_file = comp_name.map(|name| FileEntry { | |
1388 | path_name: AttributeValue::String(name), | |
1389 | directory_index: 0, | |
1390 | timestamp: 0, | |
1391 | size: 0, | |
1392 | md5: [0; 16], | |
1393 | }); | |
1394 | ||
1395 | file_name_entry_format = Vec::new(); | |
1396 | loop { | |
1397 | let path_name = rest.read_null_terminated_slice()?; | |
1398 | if path_name.is_empty() { | |
1399 | break; | |
1400 | } | |
1401 | file_names.push(FileEntry::parse(rest, path_name)?); | |
1402 | } | |
1403 | } else { | |
1404 | comp_file = None; | |
1405 | file_name_entry_format = FileEntryFormat::parse(rest)?; | |
1406 | let count = rest.read_uleb128()?; | |
1407 | for _ in 0..count { | |
1408 | file_names.push(parse_file_v5(rest, encoding, &file_name_entry_format)?); | |
1409 | } | |
1410 | } | |
1411 | ||
1412 | let header = LineProgramHeader { | |
1413 | encoding, | |
1414 | offset, | |
1415 | unit_length, | |
1416 | header_length, | |
1417 | line_encoding, | |
1418 | opcode_base, | |
1419 | standard_opcode_lengths, | |
1420 | directory_entry_format, | |
1421 | include_directories, | |
1422 | file_name_entry_format, | |
1423 | file_names, | |
1424 | program_buf, | |
1425 | comp_dir, | |
1426 | comp_file, | |
1427 | }; | |
1428 | Ok(header) | |
1429 | } | |
1430 | } | |
1431 | ||
1432 | /// Deprecated. `IncompleteLineNumberProgram` has been renamed to `IncompleteLineProgram`. | |
1433 | #[deprecated( | |
1434 | note = "IncompleteLineNumberProgram has been renamed to IncompleteLineProgram, use that instead." | |
1435 | )] | |
1436 | pub type IncompleteLineNumberProgram<R, Offset> = IncompleteLineProgram<R, Offset>; | |
1437 | ||
1438 | /// A line number program that has not been run to completion. | |
1439 | #[derive(Clone, Debug, Eq, PartialEq)] | |
1440 | pub struct IncompleteLineProgram<R, Offset = <R as Reader>::Offset> | |
1441 | where | |
1442 | R: Reader<Offset = Offset>, | |
1443 | Offset: ReaderOffset, | |
1444 | { | |
1445 | header: LineProgramHeader<R, Offset>, | |
1446 | } | |
1447 | ||
1448 | impl<R, Offset> IncompleteLineProgram<R, Offset> | |
1449 | where | |
1450 | R: Reader<Offset = Offset>, | |
1451 | Offset: ReaderOffset, | |
1452 | { | |
1453 | /// Retrieve the `LineProgramHeader` for this program. | |
1454 | pub fn header(&self) -> &LineProgramHeader<R, Offset> { | |
1455 | &self.header | |
1456 | } | |
1457 | ||
1458 | /// Construct a new `LineRows` for executing this program to iterate | |
1459 | /// over rows in the line information matrix. | |
1460 | pub fn rows(self) -> OneShotLineRows<R, Offset> { | |
1461 | OneShotLineRows::new(self) | |
1462 | } | |
1463 | ||
1464 | /// Execute the line number program, completing the `IncompleteLineProgram` | |
1465 | /// into a `CompleteLineProgram` and producing an array of sequences within | |
1466 | /// the line number program that can later be used with | |
1467 | /// `CompleteLineProgram::resume_from`. | |
1468 | /// | |
1469 | /// ``` | |
1470 | /// # fn foo() { | |
1471 | /// use gimli::{IncompleteLineProgram, EndianSlice, NativeEndian}; | |
1472 | /// | |
1473 | /// fn get_line_number_program<'a>() -> IncompleteLineProgram<EndianSlice<'a, NativeEndian>> { | |
1474 | /// // Get a line number program from some offset in a | |
1475 | /// // `.debug_line` section... | |
1476 | /// # unimplemented!() | |
1477 | /// } | |
1478 | /// | |
1479 | /// let program = get_line_number_program(); | |
1480 | /// let (program, sequences) = program.sequences().unwrap(); | |
1481 | /// println!("There are {} sequences in this line number program", sequences.len()); | |
1482 | /// # } | |
1483 | /// ``` | |
1484 | #[allow(clippy::type_complexity)] | |
1485 | pub fn sequences(self) -> Result<(CompleteLineProgram<R, Offset>, Vec<LineSequence<R>>)> { | |
1486 | let mut sequences = Vec::new(); | |
1487 | let mut rows = self.rows(); | |
1488 | let mut instructions = rows.instructions.clone(); | |
1489 | let mut sequence_start_addr = None; | |
1490 | loop { | |
1491 | let sequence_end_addr; | |
1492 | if rows.next_row()?.is_none() { | |
1493 | break; | |
1494 | } | |
1495 | ||
1496 | let row = &rows.row; | |
1497 | if row.end_sequence() { | |
1498 | sequence_end_addr = row.address(); | |
1499 | } else if sequence_start_addr.is_none() { | |
1500 | sequence_start_addr = Some(row.address()); | |
1501 | continue; | |
1502 | } else { | |
1503 | continue; | |
1504 | } | |
1505 | ||
1506 | // We just finished a sequence. | |
1507 | sequences.push(LineSequence { | |
1508 | // In theory one could have multiple DW_LNE_end_sequence instructions | |
1509 | // in a row. | |
1510 | start: sequence_start_addr.unwrap_or(0), | |
1511 | end: sequence_end_addr, | |
1512 | instructions: instructions.remove_trailing(&rows.instructions)?, | |
1513 | }); | |
1514 | sequence_start_addr = None; | |
1515 | instructions = rows.instructions.clone(); | |
1516 | } | |
1517 | ||
1518 | let program = CompleteLineProgram { | |
1519 | header: rows.program.header, | |
1520 | }; | |
1521 | Ok((program, sequences)) | |
1522 | } | |
1523 | } | |
1524 | ||
1525 | /// Deprecated. `CompleteLineNumberProgram` has been renamed to `CompleteLineProgram`. | |
1526 | #[deprecated( | |
1527 | note = "CompleteLineNumberProgram has been renamed to CompleteLineProgram, use that instead." | |
1528 | )] | |
1529 | pub type CompleteLineNumberProgram<R, Offset> = CompleteLineProgram<R, Offset>; | |
1530 | ||
1531 | /// A line number program that has previously been run to completion. | |
1532 | #[derive(Clone, Debug, Eq, PartialEq)] | |
1533 | pub struct CompleteLineProgram<R, Offset = <R as Reader>::Offset> | |
1534 | where | |
1535 | R: Reader<Offset = Offset>, | |
1536 | Offset: ReaderOffset, | |
1537 | { | |
1538 | header: LineProgramHeader<R, Offset>, | |
1539 | } | |
1540 | ||
1541 | impl<R, Offset> CompleteLineProgram<R, Offset> | |
1542 | where | |
1543 | R: Reader<Offset = Offset>, | |
1544 | Offset: ReaderOffset, | |
1545 | { | |
1546 | /// Retrieve the `LineProgramHeader` for this program. | |
1547 | pub fn header(&self) -> &LineProgramHeader<R, Offset> { | |
1548 | &self.header | |
1549 | } | |
1550 | ||
1551 | /// Construct a new `LineRows` for executing the subset of the line | |
1552 | /// number program identified by 'sequence' and generating the line information | |
1553 | /// matrix. | |
1554 | /// | |
1555 | /// ``` | |
1556 | /// # fn foo() { | |
1557 | /// use gimli::{IncompleteLineProgram, EndianSlice, NativeEndian}; | |
1558 | /// | |
1559 | /// fn get_line_number_program<'a>() -> IncompleteLineProgram<EndianSlice<'a, NativeEndian>> { | |
1560 | /// // Get a line number program from some offset in a | |
1561 | /// // `.debug_line` section... | |
1562 | /// # unimplemented!() | |
1563 | /// } | |
1564 | /// | |
1565 | /// let program = get_line_number_program(); | |
1566 | /// let (program, sequences) = program.sequences().unwrap(); | |
1567 | /// for sequence in &sequences { | |
1568 | /// let mut sm = program.resume_from(sequence); | |
1569 | /// } | |
1570 | /// # } | |
1571 | /// ``` | |
1572 | pub fn resume_from<'program>( | |
1573 | &'program self, | |
1574 | sequence: &LineSequence<R>, | |
1575 | ) -> ResumedLineRows<'program, R, Offset> { | |
1576 | ResumedLineRows::resume(self, sequence) | |
1577 | } | |
1578 | } | |
1579 | ||
1580 | /// An entry in the `LineProgramHeader`'s `file_names` set. | |
1581 | #[derive(Copy, Clone, Debug, PartialEq, Eq)] | |
1582 | pub struct FileEntry<R, Offset = <R as Reader>::Offset> | |
1583 | where | |
1584 | R: Reader<Offset = Offset>, | |
1585 | Offset: ReaderOffset, | |
1586 | { | |
1587 | path_name: AttributeValue<R, Offset>, | |
1588 | directory_index: u64, | |
1589 | timestamp: u64, | |
1590 | size: u64, | |
1591 | md5: [u8; 16], | |
1592 | } | |
1593 | ||
1594 | impl<R, Offset> FileEntry<R, Offset> | |
1595 | where | |
1596 | R: Reader<Offset = Offset>, | |
1597 | Offset: ReaderOffset, | |
1598 | { | |
1599 | // version 2-4 | |
1600 | fn parse(input: &mut R, path_name: R) -> Result<FileEntry<R, Offset>> { | |
1601 | let directory_index = input.read_uleb128()?; | |
1602 | let timestamp = input.read_uleb128()?; | |
1603 | let size = input.read_uleb128()?; | |
1604 | ||
1605 | let entry = FileEntry { | |
1606 | path_name: AttributeValue::String(path_name), | |
1607 | directory_index, | |
1608 | timestamp, | |
1609 | size, | |
1610 | md5: [0; 16], | |
1611 | }; | |
1612 | ||
1613 | Ok(entry) | |
1614 | } | |
1615 | ||
1616 | /// > A slice containing the full or relative path name of | |
1617 | /// > a source file. If the entry contains a file name or a relative path | |
1618 | /// > name, the file is located relative to either the compilation directory | |
1619 | /// > (as specified by the DW_AT_comp_dir attribute given in the compilation | |
1620 | /// > unit) or one of the directories in the include_directories section. | |
1621 | pub fn path_name(&self) -> AttributeValue<R, Offset> { | |
1622 | self.path_name.clone() | |
1623 | } | |
1624 | ||
1625 | /// > An unsigned LEB128 number representing the directory index of the | |
1626 | /// > directory in which the file was found. | |
1627 | /// > | |
1628 | /// > ... | |
1629 | /// > | |
1630 | /// > The directory index represents an entry in the include_directories | |
1631 | /// > section of the line number program header. The index is 0 if the file | |
1632 | /// > was found in the current directory of the compilation, 1 if it was found | |
1633 | /// > in the first directory in the include_directories section, and so | |
1634 | /// > on. The directory index is ignored for file names that represent full | |
1635 | /// > path names. | |
1636 | pub fn directory_index(&self) -> u64 { | |
1637 | self.directory_index | |
1638 | } | |
1639 | ||
1640 | /// Get this file's directory. | |
1641 | /// | |
1642 | /// A directory index of 0 corresponds to the compilation unit directory. | |
1643 | pub fn directory(&self, header: &LineProgramHeader<R>) -> Option<AttributeValue<R, Offset>> { | |
1644 | header.directory(self.directory_index) | |
1645 | } | |
1646 | ||
1647 | /// The implementation-defined time of last modification of the file, | |
1648 | /// or 0 if not available. | |
1649 | pub fn timestamp(&self) -> u64 { | |
1650 | self.timestamp | |
1651 | } | |
1652 | ||
1653 | /// "An unsigned LEB128 number representing the time of last modification of | |
1654 | /// the file, or 0 if not available." | |
1655 | // Terminology changed in DWARF version 5. | |
1656 | #[doc(hidden)] | |
1657 | pub fn last_modification(&self) -> u64 { | |
1658 | self.timestamp | |
1659 | } | |
1660 | ||
1661 | /// The size of the file in bytes, or 0 if not available. | |
1662 | pub fn size(&self) -> u64 { | |
1663 | self.size | |
1664 | } | |
1665 | ||
1666 | /// "An unsigned LEB128 number representing the length in bytes of the file, | |
1667 | /// or 0 if not available." | |
1668 | // Terminology changed in DWARF version 5. | |
1669 | #[doc(hidden)] | |
1670 | pub fn length(&self) -> u64 { | |
1671 | self.size | |
1672 | } | |
1673 | ||
1674 | /// A 16-byte MD5 digest of the file contents. | |
1675 | /// | |
1676 | /// Only valid if `LineProgramHeader::file_has_md5` returns `true`. | |
1677 | pub fn md5(&self) -> &[u8; 16] { | |
1678 | &self.md5 | |
1679 | } | |
1680 | } | |
1681 | ||
1682 | /// The format of a component of an include directory or file name entry. | |
1683 | #[derive(Copy, Clone, Debug, PartialEq, Eq)] | |
1684 | pub struct FileEntryFormat { | |
1685 | /// The type of information that is represented by the component. | |
1686 | pub content_type: constants::DwLnct, | |
1687 | ||
1688 | /// The encoding form of the component value. | |
1689 | pub form: constants::DwForm, | |
1690 | } | |
1691 | ||
1692 | impl FileEntryFormat { | |
1693 | fn parse<R: Reader>(input: &mut R) -> Result<Vec<FileEntryFormat>> { | |
1694 | let format_count = input.read_u8()? as usize; | |
1695 | let mut format = Vec::with_capacity(format_count); | |
1696 | let mut path_count = 0; | |
1697 | for _ in 0..format_count { | |
1698 | let content_type = input.read_uleb128()?; | |
1699 | let content_type = if content_type > u64::from(u16::max_value()) { | |
1700 | constants::DwLnct(u16::max_value()) | |
1701 | } else { | |
1702 | constants::DwLnct(content_type as u16) | |
1703 | }; | |
1704 | if content_type == constants::DW_LNCT_path { | |
1705 | path_count += 1; | |
1706 | } | |
1707 | ||
1708 | let form = constants::DwForm(input.read_uleb128_u16()?); | |
1709 | ||
1710 | format.push(FileEntryFormat { content_type, form }); | |
1711 | } | |
1712 | if path_count != 1 { | |
1713 | return Err(Error::MissingFileEntryFormatPath); | |
1714 | } | |
1715 | Ok(format) | |
1716 | } | |
1717 | } | |
1718 | ||
1719 | fn parse_directory_v5<R: Reader>( | |
1720 | input: &mut R, | |
1721 | encoding: Encoding, | |
1722 | formats: &[FileEntryFormat], | |
1723 | ) -> Result<AttributeValue<R>> { | |
1724 | let mut path_name = None; | |
1725 | ||
1726 | for format in formats { | |
1727 | let value = parse_attribute(input, encoding, format.form)?; | |
1728 | if format.content_type == constants::DW_LNCT_path { | |
1729 | path_name = Some(value); | |
1730 | } | |
1731 | } | |
1732 | ||
1733 | Ok(path_name.unwrap()) | |
1734 | } | |
1735 | ||
1736 | fn parse_file_v5<R: Reader>( | |
1737 | input: &mut R, | |
1738 | encoding: Encoding, | |
1739 | formats: &[FileEntryFormat], | |
1740 | ) -> Result<FileEntry<R>> { | |
1741 | let mut path_name = None; | |
1742 | let mut directory_index = 0; | |
1743 | let mut timestamp = 0; | |
1744 | let mut size = 0; | |
1745 | let mut md5 = [0; 16]; | |
1746 | ||
1747 | for format in formats { | |
1748 | let value = parse_attribute(input, encoding, format.form)?; | |
1749 | match format.content_type { | |
1750 | constants::DW_LNCT_path => path_name = Some(value), | |
1751 | constants::DW_LNCT_directory_index => { | |
1752 | if let Some(value) = value.udata_value() { | |
1753 | directory_index = value; | |
1754 | } | |
1755 | } | |
1756 | constants::DW_LNCT_timestamp => { | |
1757 | if let Some(value) = value.udata_value() { | |
1758 | timestamp = value; | |
1759 | } | |
1760 | } | |
1761 | constants::DW_LNCT_size => { | |
1762 | if let Some(value) = value.udata_value() { | |
1763 | size = value; | |
1764 | } | |
1765 | } | |
1766 | constants::DW_LNCT_MD5 => { | |
1767 | if let AttributeValue::Block(mut value) = value { | |
1768 | if value.len().into_u64() == 16 { | |
1769 | md5 = value.read_u8_array()?; | |
1770 | } | |
1771 | } | |
1772 | } | |
1773 | // Ignore unknown content types. | |
1774 | _ => {} | |
1775 | } | |
1776 | } | |
1777 | ||
1778 | Ok(FileEntry { | |
1779 | path_name: path_name.unwrap(), | |
1780 | directory_index, | |
1781 | timestamp, | |
1782 | size, | |
1783 | md5, | |
1784 | }) | |
1785 | } | |
1786 | ||
1787 | // TODO: this should be shared with unit::parse_attribute(), but that is hard to do. | |
1788 | fn parse_attribute<R: Reader>( | |
1789 | input: &mut R, | |
1790 | encoding: Encoding, | |
1791 | form: constants::DwForm, | |
1792 | ) -> Result<AttributeValue<R>> { | |
1793 | Ok(match form { | |
1794 | constants::DW_FORM_block1 => { | |
1795 | let len = input.read_u8().map(R::Offset::from_u8)?; | |
1796 | let block = input.split(len)?; | |
1797 | AttributeValue::Block(block) | |
1798 | } | |
1799 | constants::DW_FORM_block2 => { | |
1800 | let len = input.read_u16().map(R::Offset::from_u16)?; | |
1801 | let block = input.split(len)?; | |
1802 | AttributeValue::Block(block) | |
1803 | } | |
1804 | constants::DW_FORM_block4 => { | |
1805 | let len = input.read_u32().map(R::Offset::from_u32)?; | |
1806 | let block = input.split(len)?; | |
1807 | AttributeValue::Block(block) | |
1808 | } | |
1809 | constants::DW_FORM_block => { | |
1810 | let len = input.read_uleb128().and_then(R::Offset::from_u64)?; | |
1811 | let block = input.split(len)?; | |
1812 | AttributeValue::Block(block) | |
1813 | } | |
1814 | constants::DW_FORM_data1 => { | |
1815 | let data = input.read_u8()?; | |
1816 | AttributeValue::Data1(data) | |
1817 | } | |
1818 | constants::DW_FORM_data2 => { | |
1819 | let data = input.read_u16()?; | |
1820 | AttributeValue::Data2(data) | |
1821 | } | |
1822 | constants::DW_FORM_data4 => { | |
1823 | let data = input.read_u32()?; | |
1824 | AttributeValue::Data4(data) | |
1825 | } | |
1826 | constants::DW_FORM_data8 => { | |
1827 | let data = input.read_u64()?; | |
1828 | AttributeValue::Data8(data) | |
1829 | } | |
1830 | constants::DW_FORM_data16 => { | |
1831 | let block = input.split(R::Offset::from_u8(16))?; | |
1832 | AttributeValue::Block(block) | |
1833 | } | |
1834 | constants::DW_FORM_udata => { | |
1835 | let data = input.read_uleb128()?; | |
1836 | AttributeValue::Udata(data) | |
1837 | } | |
1838 | constants::DW_FORM_sdata => { | |
1839 | let data = input.read_sleb128()?; | |
1840 | AttributeValue::Sdata(data) | |
1841 | } | |
1842 | constants::DW_FORM_flag => { | |
1843 | let present = input.read_u8()?; | |
1844 | AttributeValue::Flag(present != 0) | |
1845 | } | |
1846 | constants::DW_FORM_sec_offset => { | |
1847 | let offset = input.read_offset(encoding.format)?; | |
1848 | AttributeValue::SecOffset(offset) | |
1849 | } | |
1850 | constants::DW_FORM_string => { | |
1851 | let string = input.read_null_terminated_slice()?; | |
1852 | AttributeValue::String(string) | |
1853 | } | |
1854 | constants::DW_FORM_strp => { | |
1855 | let offset = input.read_offset(encoding.format)?; | |
1856 | AttributeValue::DebugStrRef(DebugStrOffset(offset)) | |
1857 | } | |
1858 | constants::DW_FORM_strp_sup | constants::DW_FORM_GNU_strp_alt => { | |
1859 | let offset = input.read_offset(encoding.format)?; | |
1860 | AttributeValue::DebugStrRefSup(DebugStrOffset(offset)) | |
1861 | } | |
1862 | constants::DW_FORM_line_strp => { | |
1863 | let offset = input.read_offset(encoding.format)?; | |
1864 | AttributeValue::DebugLineStrRef(DebugLineStrOffset(offset)) | |
1865 | } | |
1866 | constants::DW_FORM_strx | constants::DW_FORM_GNU_str_index => { | |
1867 | let index = input.read_uleb128().and_then(R::Offset::from_u64)?; | |
1868 | AttributeValue::DebugStrOffsetsIndex(DebugStrOffsetsIndex(index)) | |
1869 | } | |
1870 | constants::DW_FORM_strx1 => { | |
1871 | let index = input.read_u8().map(R::Offset::from_u8)?; | |
1872 | AttributeValue::DebugStrOffsetsIndex(DebugStrOffsetsIndex(index)) | |
1873 | } | |
1874 | constants::DW_FORM_strx2 => { | |
1875 | let index = input.read_u16().map(R::Offset::from_u16)?; | |
1876 | AttributeValue::DebugStrOffsetsIndex(DebugStrOffsetsIndex(index)) | |
1877 | } | |
1878 | constants::DW_FORM_strx3 => { | |
1879 | let index = input.read_uint(3).and_then(R::Offset::from_u64)?; | |
1880 | AttributeValue::DebugStrOffsetsIndex(DebugStrOffsetsIndex(index)) | |
1881 | } | |
1882 | constants::DW_FORM_strx4 => { | |
1883 | let index = input.read_u32().map(R::Offset::from_u32)?; | |
1884 | AttributeValue::DebugStrOffsetsIndex(DebugStrOffsetsIndex(index)) | |
1885 | } | |
1886 | _ => { | |
1887 | return Err(Error::UnknownForm); | |
1888 | } | |
1889 | }) | |
1890 | } | |
1891 | ||
1892 | #[cfg(test)] | |
1893 | mod tests { | |
1894 | use super::*; | |
1895 | use crate::constants; | |
1896 | use crate::endianity::LittleEndian; | |
1897 | use crate::read::{EndianSlice, Error}; | |
1898 | use crate::test_util::GimliSectionMethods; | |
1899 | use core::u64; | |
1900 | use core::u8; | |
1901 | use test_assembler::{Endian, Label, LabelMaker, Section}; | |
1902 | ||
1903 | #[test] | |
1904 | fn test_parse_debug_line_32_ok() { | |
1905 | #[rustfmt::skip] | |
1906 | let buf = [ | |
1907 | // 32-bit length = 62. | |
1908 | 0x3e, 0x00, 0x00, 0x00, | |
1909 | // Version. | |
1910 | 0x04, 0x00, | |
1911 | // Header length = 40. | |
1912 | 0x28, 0x00, 0x00, 0x00, | |
1913 | // Minimum instruction length. | |
1914 | 0x01, | |
1915 | // Maximum operations per byte. | |
1916 | 0x01, | |
1917 | // Default is_stmt. | |
1918 | 0x01, | |
1919 | // Line base. | |
1920 | 0x00, | |
1921 | // Line range. | |
1922 | 0x01, | |
1923 | // Opcode base. | |
1924 | 0x03, | |
1925 | // Standard opcode lengths for opcodes 1 .. opcode base - 1. | |
1926 | 0x01, 0x02, | |
1927 | // Include directories = '/', 'i', 'n', 'c', '\0', '/', 'i', 'n', 'c', '2', '\0', '\0' | |
1928 | 0x2f, 0x69, 0x6e, 0x63, 0x00, 0x2f, 0x69, 0x6e, 0x63, 0x32, 0x00, 0x00, | |
1929 | // File names | |
1930 | // foo.rs | |
1931 | 0x66, 0x6f, 0x6f, 0x2e, 0x72, 0x73, 0x00, | |
1932 | 0x00, | |
1933 | 0x00, | |
1934 | 0x00, | |
1935 | // bar.h | |
1936 | 0x62, 0x61, 0x72, 0x2e, 0x68, 0x00, | |
1937 | 0x01, | |
1938 | 0x00, | |
1939 | 0x00, | |
1940 | // End file names. | |
1941 | 0x00, | |
1942 | ||
1943 | // Dummy line program data. | |
1944 | 0x00, 0x00, 0x00, 0x00, | |
1945 | 0x00, 0x00, 0x00, 0x00, | |
1946 | 0x00, 0x00, 0x00, 0x00, | |
1947 | 0x00, 0x00, 0x00, 0x00, | |
1948 | ||
1949 | // Dummy next line program. | |
1950 | 0x00, 0x00, 0x00, 0x00, | |
1951 | 0x00, 0x00, 0x00, 0x00, | |
1952 | 0x00, 0x00, 0x00, 0x00, | |
1953 | 0x00, 0x00, 0x00, 0x00, | |
1954 | ]; | |
1955 | ||
1956 | let rest = &mut EndianSlice::new(&buf, LittleEndian); | |
1957 | let comp_dir = EndianSlice::new(b"/comp_dir", LittleEndian); | |
1958 | let comp_name = EndianSlice::new(b"/comp_name", LittleEndian); | |
1959 | ||
1960 | let header = | |
1961 | LineProgramHeader::parse(rest, DebugLineOffset(0), 4, Some(comp_dir), Some(comp_name)) | |
1962 | .expect("should parse header ok"); | |
1963 | ||
1964 | assert_eq!( | |
1965 | *rest, | |
1966 | EndianSlice::new(&buf[buf.len() - 16..], LittleEndian) | |
1967 | ); | |
1968 | ||
1969 | assert_eq!(header.offset, DebugLineOffset(0)); | |
1970 | assert_eq!(header.version(), 4); | |
1971 | assert_eq!(header.minimum_instruction_length(), 1); | |
1972 | assert_eq!(header.maximum_operations_per_instruction(), 1); | |
1973 | assert_eq!(header.default_is_stmt(), true); | |
1974 | assert_eq!(header.line_base(), 0); | |
1975 | assert_eq!(header.line_range(), 1); | |
1976 | assert_eq!(header.opcode_base(), 3); | |
1977 | assert_eq!(header.directory(0), Some(AttributeValue::String(comp_dir))); | |
1978 | assert_eq!( | |
1979 | header.file(0).unwrap().path_name, | |
1980 | AttributeValue::String(comp_name) | |
1981 | ); | |
1982 | ||
1983 | let expected_lengths = [1, 2]; | |
1984 | assert_eq!(header.standard_opcode_lengths().slice(), &expected_lengths); | |
1985 | ||
1986 | let expected_include_directories = [ | |
1987 | AttributeValue::String(EndianSlice::new(b"/inc", LittleEndian)), | |
1988 | AttributeValue::String(EndianSlice::new(b"/inc2", LittleEndian)), | |
1989 | ]; | |
1990 | assert_eq!(header.include_directories(), &expected_include_directories); | |
1991 | ||
1992 | let expected_file_names = [ | |
1993 | FileEntry { | |
1994 | path_name: AttributeValue::String(EndianSlice::new(b"foo.rs", LittleEndian)), | |
1995 | directory_index: 0, | |
1996 | timestamp: 0, | |
1997 | size: 0, | |
1998 | md5: [0; 16], | |
1999 | }, | |
2000 | FileEntry { | |
2001 | path_name: AttributeValue::String(EndianSlice::new(b"bar.h", LittleEndian)), | |
2002 | directory_index: 1, | |
2003 | timestamp: 0, | |
2004 | size: 0, | |
2005 | md5: [0; 16], | |
2006 | }, | |
2007 | ]; | |
2008 | assert_eq!(&*header.file_names(), &expected_file_names); | |
2009 | } | |
2010 | ||
2011 | #[test] | |
2012 | fn test_parse_debug_line_header_length_too_short() { | |
2013 | #[rustfmt::skip] | |
2014 | let buf = [ | |
2015 | // 32-bit length = 62. | |
2016 | 0x3e, 0x00, 0x00, 0x00, | |
2017 | // Version. | |
2018 | 0x04, 0x00, | |
2019 | // Header length = 20. TOO SHORT!!! | |
2020 | 0x15, 0x00, 0x00, 0x00, | |
2021 | // Minimum instruction length. | |
2022 | 0x01, | |
2023 | // Maximum operations per byte. | |
2024 | 0x01, | |
2025 | // Default is_stmt. | |
2026 | 0x01, | |
2027 | // Line base. | |
2028 | 0x00, | |
2029 | // Line range. | |
2030 | 0x01, | |
2031 | // Opcode base. | |
2032 | 0x03, | |
2033 | // Standard opcode lengths for opcodes 1 .. opcode base - 1. | |
2034 | 0x01, 0x02, | |
2035 | // Include directories = '/', 'i', 'n', 'c', '\0', '/', 'i', 'n', 'c', '2', '\0', '\0' | |
2036 | 0x2f, 0x69, 0x6e, 0x63, 0x00, 0x2f, 0x69, 0x6e, 0x63, 0x32, 0x00, 0x00, | |
2037 | // File names | |
2038 | // foo.rs | |
2039 | 0x66, 0x6f, 0x6f, 0x2e, 0x72, 0x73, 0x00, | |
2040 | 0x00, | |
2041 | 0x00, | |
2042 | 0x00, | |
2043 | // bar.h | |
2044 | 0x62, 0x61, 0x72, 0x2e, 0x68, 0x00, | |
2045 | 0x01, | |
2046 | 0x00, | |
2047 | 0x00, | |
2048 | // End file names. | |
2049 | 0x00, | |
2050 | ||
2051 | // Dummy line program data. | |
2052 | 0x00, 0x00, 0x00, 0x00, | |
2053 | 0x00, 0x00, 0x00, 0x00, | |
2054 | 0x00, 0x00, 0x00, 0x00, | |
2055 | 0x00, 0x00, 0x00, 0x00, | |
2056 | ||
2057 | // Dummy next line program. | |
2058 | 0x00, 0x00, 0x00, 0x00, | |
2059 | 0x00, 0x00, 0x00, 0x00, | |
2060 | 0x00, 0x00, 0x00, 0x00, | |
2061 | 0x00, 0x00, 0x00, 0x00, | |
2062 | ]; | |
2063 | ||
2064 | let input = &mut EndianSlice::new(&buf, LittleEndian); | |
2065 | ||
2066 | match LineProgramHeader::parse(input, DebugLineOffset(0), 4, None, None) { | |
2067 | Err(Error::UnexpectedEof(_)) => return, | |
2068 | otherwise => panic!("Unexpected result: {:?}", otherwise), | |
2069 | } | |
2070 | } | |
2071 | ||
2072 | #[test] | |
2073 | fn test_parse_debug_line_unit_length_too_short() { | |
2074 | #[rustfmt::skip] | |
2075 | let buf = [ | |
2076 | // 32-bit length = 40. TOO SHORT!!! | |
2077 | 0x28, 0x00, 0x00, 0x00, | |
2078 | // Version. | |
2079 | 0x04, 0x00, | |
2080 | // Header length = 40. | |
2081 | 0x28, 0x00, 0x00, 0x00, | |
2082 | // Minimum instruction length. | |
2083 | 0x01, | |
2084 | // Maximum operations per byte. | |
2085 | 0x01, | |
2086 | // Default is_stmt. | |
2087 | 0x01, | |
2088 | // Line base. | |
2089 | 0x00, | |
2090 | // Line range. | |
2091 | 0x01, | |
2092 | // Opcode base. | |
2093 | 0x03, | |
2094 | // Standard opcode lengths for opcodes 1 .. opcode base - 1. | |
2095 | 0x01, 0x02, | |
2096 | // Include directories = '/', 'i', 'n', 'c', '\0', '/', 'i', 'n', 'c', '2', '\0', '\0' | |
2097 | 0x2f, 0x69, 0x6e, 0x63, 0x00, 0x2f, 0x69, 0x6e, 0x63, 0x32, 0x00, 0x00, | |
2098 | // File names | |
2099 | // foo.rs | |
2100 | 0x66, 0x6f, 0x6f, 0x2e, 0x72, 0x73, 0x00, | |
2101 | 0x00, | |
2102 | 0x00, | |
2103 | 0x00, | |
2104 | // bar.h | |
2105 | 0x62, 0x61, 0x72, 0x2e, 0x68, 0x00, | |
2106 | 0x01, | |
2107 | 0x00, | |
2108 | 0x00, | |
2109 | // End file names. | |
2110 | 0x00, | |
2111 | ||
2112 | // Dummy line program data. | |
2113 | 0x00, 0x00, 0x00, 0x00, | |
2114 | 0x00, 0x00, 0x00, 0x00, | |
2115 | 0x00, 0x00, 0x00, 0x00, | |
2116 | 0x00, 0x00, 0x00, 0x00, | |
2117 | ||
2118 | // Dummy next line program. | |
2119 | 0x00, 0x00, 0x00, 0x00, | |
2120 | 0x00, 0x00, 0x00, 0x00, | |
2121 | 0x00, 0x00, 0x00, 0x00, | |
2122 | 0x00, 0x00, 0x00, 0x00, | |
2123 | ]; | |
2124 | ||
2125 | let input = &mut EndianSlice::new(&buf, LittleEndian); | |
2126 | ||
2127 | match LineProgramHeader::parse(input, DebugLineOffset(0), 4, None, None) { | |
2128 | Err(Error::UnexpectedEof(_)) => return, | |
2129 | otherwise => panic!("Unexpected result: {:?}", otherwise), | |
2130 | } | |
2131 | } | |
2132 | ||
2133 | const OPCODE_BASE: u8 = 13; | |
2134 | const STANDARD_OPCODE_LENGTHS: &[u8] = &[0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1]; | |
2135 | ||
2136 | fn make_test_header( | |
2137 | buf: EndianSlice<LittleEndian>, | |
2138 | ) -> LineProgramHeader<EndianSlice<LittleEndian>> { | |
2139 | let encoding = Encoding { | |
2140 | format: Format::Dwarf32, | |
2141 | version: 4, | |
2142 | address_size: 8, | |
2143 | }; | |
2144 | let line_encoding = LineEncoding { | |
2145 | line_base: -3, | |
2146 | line_range: 12, | |
2147 | ..Default::default() | |
2148 | }; | |
2149 | LineProgramHeader { | |
2150 | encoding, | |
2151 | offset: DebugLineOffset(0), | |
2152 | unit_length: 1, | |
2153 | header_length: 1, | |
2154 | line_encoding, | |
2155 | opcode_base: OPCODE_BASE, | |
2156 | standard_opcode_lengths: EndianSlice::new(STANDARD_OPCODE_LENGTHS, LittleEndian), | |
2157 | file_names: vec![ | |
2158 | FileEntry { | |
2159 | path_name: AttributeValue::String(EndianSlice::new(b"foo.c", LittleEndian)), | |
2160 | directory_index: 0, | |
2161 | timestamp: 0, | |
2162 | size: 0, | |
2163 | md5: [0; 16], | |
2164 | }, | |
2165 | FileEntry { | |
2166 | path_name: AttributeValue::String(EndianSlice::new(b"bar.rs", LittleEndian)), | |
2167 | directory_index: 0, | |
2168 | timestamp: 0, | |
2169 | size: 0, | |
2170 | md5: [0; 16], | |
2171 | }, | |
2172 | ], | |
2173 | include_directories: vec![], | |
2174 | directory_entry_format: vec![], | |
2175 | file_name_entry_format: vec![], | |
2176 | program_buf: buf, | |
2177 | comp_dir: None, | |
2178 | comp_file: None, | |
2179 | } | |
2180 | } | |
2181 | ||
2182 | fn make_test_program( | |
2183 | buf: EndianSlice<LittleEndian>, | |
2184 | ) -> IncompleteLineProgram<EndianSlice<LittleEndian>> { | |
2185 | IncompleteLineProgram { | |
2186 | header: make_test_header(buf), | |
2187 | } | |
2188 | } | |
2189 | ||
2190 | #[test] | |
2191 | fn test_parse_special_opcodes() { | |
2192 | for i in OPCODE_BASE..u8::MAX { | |
2193 | let input = [i, 0, 0, 0]; | |
2194 | let input = EndianSlice::new(&input, LittleEndian); | |
2195 | let header = make_test_header(input); | |
2196 | ||
2197 | let mut rest = input; | |
2198 | let opcode = | |
2199 | LineInstruction::parse(&header, &mut rest).expect("Should parse the opcode OK"); | |
2200 | ||
2201 | assert_eq!(*rest, *input.range_from(1..)); | |
2202 | assert_eq!(opcode, LineInstruction::Special(i)); | |
2203 | } | |
2204 | } | |
2205 | ||
2206 | #[test] | |
2207 | fn test_parse_standard_opcodes() { | |
2208 | fn test<Operands>( | |
2209 | raw: constants::DwLns, | |
2210 | operands: Operands, | |
2211 | expected: LineInstruction<EndianSlice<LittleEndian>>, | |
2212 | ) where | |
2213 | Operands: AsRef<[u8]>, | |
2214 | { | |
2215 | let mut input = Vec::new(); | |
2216 | input.push(raw.0); | |
2217 | input.extend_from_slice(operands.as_ref()); | |
2218 | ||
2219 | let expected_rest = [0, 1, 2, 3, 4]; | |
2220 | input.extend_from_slice(&expected_rest); | |
2221 | ||
2222 | let input = EndianSlice::new(&*input, LittleEndian); | |
2223 | let header = make_test_header(input); | |
2224 | ||
2225 | let mut rest = input; | |
2226 | let opcode = | |
2227 | LineInstruction::parse(&header, &mut rest).expect("Should parse the opcode OK"); | |
2228 | ||
2229 | assert_eq!(opcode, expected); | |
2230 | assert_eq!(*rest, expected_rest); | |
2231 | } | |
2232 | ||
2233 | test(constants::DW_LNS_copy, [], LineInstruction::Copy); | |
2234 | test( | |
2235 | constants::DW_LNS_advance_pc, | |
2236 | [42], | |
2237 | LineInstruction::AdvancePc(42), | |
2238 | ); | |
2239 | test( | |
2240 | constants::DW_LNS_advance_line, | |
2241 | [9], | |
2242 | LineInstruction::AdvanceLine(9), | |
2243 | ); | |
2244 | test(constants::DW_LNS_set_file, [7], LineInstruction::SetFile(7)); | |
2245 | test( | |
2246 | constants::DW_LNS_set_column, | |
2247 | [1], | |
2248 | LineInstruction::SetColumn(1), | |
2249 | ); | |
2250 | test( | |
2251 | constants::DW_LNS_negate_stmt, | |
2252 | [], | |
2253 | LineInstruction::NegateStatement, | |
2254 | ); | |
2255 | test( | |
2256 | constants::DW_LNS_set_basic_block, | |
2257 | [], | |
2258 | LineInstruction::SetBasicBlock, | |
2259 | ); | |
2260 | test( | |
2261 | constants::DW_LNS_const_add_pc, | |
2262 | [], | |
2263 | LineInstruction::ConstAddPc, | |
2264 | ); | |
2265 | test( | |
2266 | constants::DW_LNS_fixed_advance_pc, | |
2267 | [42, 0], | |
2268 | LineInstruction::FixedAddPc(42), | |
2269 | ); | |
2270 | test( | |
2271 | constants::DW_LNS_set_prologue_end, | |
2272 | [], | |
2273 | LineInstruction::SetPrologueEnd, | |
2274 | ); | |
2275 | test( | |
2276 | constants::DW_LNS_set_isa, | |
2277 | [57 + 0x80, 100], | |
2278 | LineInstruction::SetIsa(12857), | |
2279 | ); | |
2280 | } | |
2281 | ||
2282 | #[test] | |
2283 | fn test_parse_unknown_standard_opcode_no_args() { | |
2284 | let input = [OPCODE_BASE, 1, 2, 3]; | |
2285 | let input = EndianSlice::new(&input, LittleEndian); | |
2286 | let mut standard_opcode_lengths = Vec::new(); | |
2287 | let mut header = make_test_header(input); | |
2288 | standard_opcode_lengths.extend(header.standard_opcode_lengths.slice()); | |
2289 | standard_opcode_lengths.push(0); | |
2290 | header.opcode_base += 1; | |
2291 | header.standard_opcode_lengths = EndianSlice::new(&standard_opcode_lengths, LittleEndian); | |
2292 | ||
2293 | let mut rest = input; | |
2294 | let opcode = | |
2295 | LineInstruction::parse(&header, &mut rest).expect("Should parse the opcode OK"); | |
2296 | ||
2297 | assert_eq!( | |
2298 | opcode, | |
2299 | LineInstruction::UnknownStandard0(constants::DwLns(OPCODE_BASE)) | |
2300 | ); | |
2301 | assert_eq!(*rest, *input.range_from(1..)); | |
2302 | } | |
2303 | ||
2304 | #[test] | |
2305 | fn test_parse_unknown_standard_opcode_one_arg() { | |
2306 | let input = [OPCODE_BASE, 1, 2, 3]; | |
2307 | let input = EndianSlice::new(&input, LittleEndian); | |
2308 | let mut standard_opcode_lengths = Vec::new(); | |
2309 | let mut header = make_test_header(input); | |
2310 | standard_opcode_lengths.extend(header.standard_opcode_lengths.slice()); | |
2311 | standard_opcode_lengths.push(1); | |
2312 | header.opcode_base += 1; | |
2313 | header.standard_opcode_lengths = EndianSlice::new(&standard_opcode_lengths, LittleEndian); | |
2314 | ||
2315 | let mut rest = input; | |
2316 | let opcode = | |
2317 | LineInstruction::parse(&header, &mut rest).expect("Should parse the opcode OK"); | |
2318 | ||
2319 | assert_eq!( | |
2320 | opcode, | |
2321 | LineInstruction::UnknownStandard1(constants::DwLns(OPCODE_BASE), 1) | |
2322 | ); | |
2323 | assert_eq!(*rest, *input.range_from(2..)); | |
2324 | } | |
2325 | ||
2326 | #[test] | |
2327 | fn test_parse_unknown_standard_opcode_many_args() { | |
2328 | let input = [OPCODE_BASE, 1, 2, 3]; | |
2329 | let input = EndianSlice::new(&input, LittleEndian); | |
2330 | let args = EndianSlice::new(&input[1..], LittleEndian); | |
2331 | let mut standard_opcode_lengths = Vec::new(); | |
2332 | let mut header = make_test_header(input); | |
2333 | standard_opcode_lengths.extend(header.standard_opcode_lengths.slice()); | |
2334 | standard_opcode_lengths.push(3); | |
2335 | header.opcode_base += 1; | |
2336 | header.standard_opcode_lengths = EndianSlice::new(&standard_opcode_lengths, LittleEndian); | |
2337 | ||
2338 | let mut rest = input; | |
2339 | let opcode = | |
2340 | LineInstruction::parse(&header, &mut rest).expect("Should parse the opcode OK"); | |
2341 | ||
2342 | assert_eq!( | |
2343 | opcode, | |
2344 | LineInstruction::UnknownStandardN(constants::DwLns(OPCODE_BASE), args) | |
2345 | ); | |
2346 | assert_eq!(*rest, []); | |
2347 | } | |
2348 | ||
2349 | #[test] | |
2350 | fn test_parse_extended_opcodes() { | |
2351 | fn test<Operands>( | |
2352 | raw: constants::DwLne, | |
2353 | operands: Operands, | |
2354 | expected: LineInstruction<EndianSlice<LittleEndian>>, | |
2355 | ) where | |
2356 | Operands: AsRef<[u8]>, | |
2357 | { | |
2358 | let mut input = Vec::new(); | |
2359 | input.push(0); | |
2360 | ||
2361 | let operands = operands.as_ref(); | |
2362 | input.push(1 + operands.len() as u8); | |
2363 | ||
2364 | input.push(raw.0); | |
2365 | input.extend_from_slice(operands); | |
2366 | ||
2367 | let expected_rest = [0, 1, 2, 3, 4]; | |
2368 | input.extend_from_slice(&expected_rest); | |
2369 | ||
2370 | let input = EndianSlice::new(&input, LittleEndian); | |
2371 | let header = make_test_header(input); | |
2372 | ||
2373 | let mut rest = input; | |
2374 | let opcode = | |
2375 | LineInstruction::parse(&header, &mut rest).expect("Should parse the opcode OK"); | |
2376 | ||
2377 | assert_eq!(opcode, expected); | |
2378 | assert_eq!(*rest, expected_rest); | |
2379 | } | |
2380 | ||
2381 | test( | |
2382 | constants::DW_LNE_end_sequence, | |
2383 | [], | |
2384 | LineInstruction::EndSequence, | |
2385 | ); | |
2386 | test( | |
2387 | constants::DW_LNE_set_address, | |
2388 | [1, 2, 3, 4, 5, 6, 7, 8], | |
2389 | LineInstruction::SetAddress(578_437_695_752_307_201), | |
2390 | ); | |
2391 | test( | |
2392 | constants::DW_LNE_set_discriminator, | |
2393 | [42], | |
2394 | LineInstruction::SetDiscriminator(42), | |
2395 | ); | |
2396 | ||
2397 | let mut file = Vec::new(); | |
2398 | // "foo.c" | |
2399 | let path_name = [b'f', b'o', b'o', b'.', b'c', 0]; | |
2400 | file.extend_from_slice(&path_name); | |
2401 | // Directory index. | |
2402 | file.push(0); | |
2403 | // Last modification of file. | |
2404 | file.push(1); | |
2405 | // Size of file. | |
2406 | file.push(2); | |
2407 | ||
2408 | test( | |
2409 | constants::DW_LNE_define_file, | |
2410 | file, | |
2411 | LineInstruction::DefineFile(FileEntry { | |
2412 | path_name: AttributeValue::String(EndianSlice::new(b"foo.c", LittleEndian)), | |
2413 | directory_index: 0, | |
2414 | timestamp: 1, | |
2415 | size: 2, | |
2416 | md5: [0; 16], | |
2417 | }), | |
2418 | ); | |
2419 | ||
2420 | // Unknown extended opcode. | |
2421 | let operands = [1, 2, 3, 4, 5, 6]; | |
2422 | let opcode = constants::DwLne(99); | |
2423 | test( | |
2424 | opcode, | |
2425 | operands, | |
2426 | LineInstruction::UnknownExtended(opcode, EndianSlice::new(&operands, LittleEndian)), | |
2427 | ); | |
2428 | } | |
2429 | ||
2430 | #[test] | |
2431 | fn test_file_entry_directory() { | |
2432 | let path_name = [b'f', b'o', b'o', b'.', b'r', b's', 0]; | |
2433 | ||
2434 | let mut file = FileEntry { | |
2435 | path_name: AttributeValue::String(EndianSlice::new(&path_name, LittleEndian)), | |
2436 | directory_index: 1, | |
2437 | timestamp: 0, | |
2438 | size: 0, | |
2439 | md5: [0; 16], | |
2440 | }; | |
2441 | ||
2442 | let mut header = make_test_header(EndianSlice::new(&[], LittleEndian)); | |
2443 | ||
2444 | let dir = AttributeValue::String(EndianSlice::new(b"dir", LittleEndian)); | |
2445 | header.include_directories.push(dir); | |
2446 | ||
2447 | assert_eq!(file.directory(&header), Some(dir)); | |
2448 | ||
2449 | // Now test the compilation's current directory. | |
2450 | file.directory_index = 0; | |
2451 | assert_eq!(file.directory(&header), None); | |
2452 | } | |
2453 | ||
2454 | fn assert_exec_opcode<'input>( | |
2455 | header: LineProgramHeader<EndianSlice<'input, LittleEndian>>, | |
2456 | mut registers: LineRow, | |
2457 | opcode: LineInstruction<EndianSlice<'input, LittleEndian>>, | |
2458 | expected_registers: LineRow, | |
2459 | expect_new_row: bool, | |
2460 | ) { | |
2461 | let mut program = IncompleteLineProgram { header }; | |
2462 | let is_new_row = registers.execute(opcode, &mut program); | |
2463 | ||
2464 | assert_eq!(is_new_row, expect_new_row); | |
2465 | assert_eq!(registers, expected_registers); | |
2466 | } | |
2467 | ||
2468 | #[test] | |
2469 | fn test_exec_special_noop() { | |
2470 | let header = make_test_header(EndianSlice::new(&[], LittleEndian)); | |
2471 | ||
2472 | let initial_registers = LineRow::new(&header); | |
2473 | let opcode = LineInstruction::Special(16); | |
2474 | let expected_registers = initial_registers; | |
2475 | ||
2476 | assert_exec_opcode(header, initial_registers, opcode, expected_registers, true); | |
2477 | } | |
2478 | ||
2479 | #[test] | |
2480 | fn test_exec_special_negative_line_advance() { | |
2481 | let header = make_test_header(EndianSlice::new(&[], LittleEndian)); | |
2482 | ||
2483 | let mut initial_registers = LineRow::new(&header); | |
2484 | initial_registers.line.0 = 10; | |
2485 | ||
2486 | let opcode = LineInstruction::Special(13); | |
2487 | ||
2488 | let mut expected_registers = initial_registers; | |
2489 | expected_registers.line.0 -= 3; | |
2490 | ||
2491 | assert_exec_opcode(header, initial_registers, opcode, expected_registers, true); | |
2492 | } | |
2493 | ||
2494 | #[test] | |
2495 | fn test_exec_special_positive_line_advance() { | |
2496 | let header = make_test_header(EndianSlice::new(&[], LittleEndian)); | |
2497 | ||
2498 | let initial_registers = LineRow::new(&header); | |
2499 | ||
2500 | let opcode = LineInstruction::Special(19); | |
2501 | ||
2502 | let mut expected_registers = initial_registers; | |
2503 | expected_registers.line.0 += 3; | |
2504 | ||
2505 | assert_exec_opcode(header, initial_registers, opcode, expected_registers, true); | |
2506 | } | |
2507 | ||
2508 | #[test] | |
2509 | fn test_exec_special_positive_address_advance() { | |
2510 | let header = make_test_header(EndianSlice::new(&[], LittleEndian)); | |
2511 | ||
2512 | let initial_registers = LineRow::new(&header); | |
2513 | ||
2514 | let opcode = LineInstruction::Special(52); | |
2515 | ||
2516 | let mut expected_registers = initial_registers; | |
2517 | expected_registers.address.0 += 3; | |
2518 | ||
2519 | assert_exec_opcode(header, initial_registers, opcode, expected_registers, true); | |
2520 | } | |
2521 | ||
2522 | #[test] | |
2523 | fn test_exec_special_positive_address_and_line_advance() { | |
2524 | let header = make_test_header(EndianSlice::new(&[], LittleEndian)); | |
2525 | ||
2526 | let initial_registers = LineRow::new(&header); | |
2527 | ||
2528 | let opcode = LineInstruction::Special(55); | |
2529 | ||
2530 | let mut expected_registers = initial_registers; | |
2531 | expected_registers.address.0 += 3; | |
2532 | expected_registers.line.0 += 3; | |
2533 | ||
2534 | assert_exec_opcode(header, initial_registers, opcode, expected_registers, true); | |
2535 | } | |
2536 | ||
2537 | #[test] | |
2538 | fn test_exec_special_positive_address_and_negative_line_advance() { | |
2539 | let header = make_test_header(EndianSlice::new(&[], LittleEndian)); | |
2540 | ||
2541 | let mut initial_registers = LineRow::new(&header); | |
2542 | initial_registers.line.0 = 10; | |
2543 | ||
2544 | let opcode = LineInstruction::Special(49); | |
2545 | ||
2546 | let mut expected_registers = initial_registers; | |
2547 | expected_registers.address.0 += 3; | |
2548 | expected_registers.line.0 -= 3; | |
2549 | ||
2550 | assert_exec_opcode(header, initial_registers, opcode, expected_registers, true); | |
2551 | } | |
2552 | ||
2553 | #[test] | |
2554 | fn test_exec_special_line_underflow() { | |
2555 | let header = make_test_header(EndianSlice::new(&[], LittleEndian)); | |
2556 | ||
2557 | let mut initial_registers = LineRow::new(&header); | |
2558 | initial_registers.line.0 = 2; | |
2559 | ||
2560 | // -3 line advance. | |
2561 | let opcode = LineInstruction::Special(13); | |
2562 | ||
2563 | let mut expected_registers = initial_registers; | |
2564 | // Clamp at 0. No idea if this is the best way to handle this situation | |
2565 | // or not... | |
2566 | expected_registers.line.0 = 0; | |
2567 | ||
2568 | assert_exec_opcode(header, initial_registers, opcode, expected_registers, true); | |
2569 | } | |
2570 | ||
2571 | #[test] | |
2572 | fn test_exec_copy() { | |
2573 | let header = make_test_header(EndianSlice::new(&[], LittleEndian)); | |
2574 | ||
2575 | let mut initial_registers = LineRow::new(&header); | |
2576 | initial_registers.address.0 = 1337; | |
2577 | initial_registers.line.0 = 42; | |
2578 | ||
2579 | let opcode = LineInstruction::Copy; | |
2580 | ||
2581 | let expected_registers = initial_registers; | |
2582 | ||
2583 | assert_exec_opcode(header, initial_registers, opcode, expected_registers, true); | |
2584 | } | |
2585 | ||
2586 | #[test] | |
2587 | fn test_exec_advance_pc() { | |
2588 | let header = make_test_header(EndianSlice::new(&[], LittleEndian)); | |
2589 | let initial_registers = LineRow::new(&header); | |
2590 | let opcode = LineInstruction::AdvancePc(42); | |
2591 | ||
2592 | let mut expected_registers = initial_registers; | |
2593 | expected_registers.address.0 += 42; | |
2594 | ||
2595 | assert_exec_opcode(header, initial_registers, opcode, expected_registers, false); | |
2596 | } | |
2597 | ||
2598 | #[test] | |
2599 | fn test_exec_advance_pc_overflow() { | |
2600 | let header = make_test_header(EndianSlice::new(&[], LittleEndian)); | |
2601 | let opcode = LineInstruction::AdvancePc(42); | |
2602 | ||
2603 | let mut initial_registers = LineRow::new(&header); | |
2604 | initial_registers.address.0 = u64::MAX; | |
2605 | ||
2606 | let mut expected_registers = initial_registers; | |
2607 | expected_registers.address.0 = 41; | |
2608 | ||
2609 | assert_exec_opcode(header, initial_registers, opcode, expected_registers, false); | |
2610 | } | |
2611 | ||
2612 | #[test] | |
2613 | fn test_exec_advance_line() { | |
2614 | let header = make_test_header(EndianSlice::new(&[], LittleEndian)); | |
2615 | let initial_registers = LineRow::new(&header); | |
2616 | let opcode = LineInstruction::AdvanceLine(42); | |
2617 | ||
2618 | let mut expected_registers = initial_registers; | |
2619 | expected_registers.line.0 += 42; | |
2620 | ||
2621 | assert_exec_opcode(header, initial_registers, opcode, expected_registers, false); | |
2622 | } | |
2623 | ||
2624 | #[test] | |
2625 | fn test_exec_advance_line_overflow() { | |
2626 | let header = make_test_header(EndianSlice::new(&[], LittleEndian)); | |
2627 | let opcode = LineInstruction::AdvanceLine(42); | |
2628 | ||
2629 | let mut initial_registers = LineRow::new(&header); | |
2630 | initial_registers.line.0 = u64::MAX; | |
2631 | ||
2632 | let mut expected_registers = initial_registers; | |
2633 | expected_registers.line.0 = 41; | |
2634 | ||
2635 | assert_exec_opcode(header, initial_registers, opcode, expected_registers, false); | |
2636 | } | |
2637 | ||
2638 | #[test] | |
2639 | fn test_exec_set_file_in_bounds() { | |
2640 | for file_idx in 1..3 { | |
2641 | let header = make_test_header(EndianSlice::new(&[], LittleEndian)); | |
2642 | let initial_registers = LineRow::new(&header); | |
2643 | let opcode = LineInstruction::SetFile(file_idx); | |
2644 | ||
2645 | let mut expected_registers = initial_registers; | |
2646 | expected_registers.file = file_idx; | |
2647 | ||
2648 | assert_exec_opcode(header, initial_registers, opcode, expected_registers, false); | |
2649 | } | |
2650 | } | |
2651 | ||
2652 | #[test] | |
2653 | fn test_exec_set_file_out_of_bounds() { | |
2654 | let header = make_test_header(EndianSlice::new(&[], LittleEndian)); | |
2655 | let initial_registers = LineRow::new(&header); | |
2656 | let opcode = LineInstruction::SetFile(100); | |
2657 | ||
2658 | // The spec doesn't say anything about rejecting input programs | |
2659 | // that set the file register out of bounds of the actual number | |
2660 | // of files that have been defined. Instead, we cross our | |
2661 | // fingers and hope that one gets defined before | |
2662 | // `LineRow::file` gets called and handle the error at | |
2663 | // that time if need be. | |
2664 | let mut expected_registers = initial_registers; | |
2665 | expected_registers.file = 100; | |
2666 | ||
2667 | assert_exec_opcode(header, initial_registers, opcode, expected_registers, false); | |
2668 | } | |
2669 | ||
2670 | #[test] | |
2671 | fn test_file_entry_file_index_out_of_bounds() { | |
2672 | // These indices are 1-based, so 0 is invalid. 100 is way more than the | |
2673 | // number of files defined in the header. | |
2674 | let out_of_bounds_indices = [0, 100]; | |
2675 | ||
2676 | for file_idx in &out_of_bounds_indices[..] { | |
2677 | let header = make_test_header(EndianSlice::new(&[], LittleEndian)); | |
2678 | let mut row = LineRow::new(&header); | |
2679 | ||
2680 | row.file = *file_idx; | |
2681 | ||
2682 | assert_eq!(row.file(&header), None); | |
2683 | } | |
2684 | } | |
2685 | ||
2686 | #[test] | |
2687 | fn test_file_entry_file_index_in_bounds() { | |
2688 | let header = make_test_header(EndianSlice::new(&[], LittleEndian)); | |
2689 | let mut row = LineRow::new(&header); | |
2690 | ||
2691 | row.file = 2; | |
2692 | ||
2693 | assert_eq!(row.file(&header), Some(&header.file_names()[1])); | |
2694 | } | |
2695 | ||
2696 | #[test] | |
2697 | fn test_exec_set_column() { | |
2698 | let header = make_test_header(EndianSlice::new(&[], LittleEndian)); | |
2699 | let initial_registers = LineRow::new(&header); | |
2700 | let opcode = LineInstruction::SetColumn(42); | |
2701 | ||
2702 | let mut expected_registers = initial_registers; | |
2703 | expected_registers.column = 42; | |
2704 | ||
2705 | assert_exec_opcode(header, initial_registers, opcode, expected_registers, false); | |
2706 | } | |
2707 | ||
2708 | #[test] | |
2709 | fn test_exec_negate_statement() { | |
2710 | let header = make_test_header(EndianSlice::new(&[], LittleEndian)); | |
2711 | let initial_registers = LineRow::new(&header); | |
2712 | let opcode = LineInstruction::NegateStatement; | |
2713 | ||
2714 | let mut expected_registers = initial_registers; | |
2715 | expected_registers.is_stmt = !initial_registers.is_stmt; | |
2716 | ||
2717 | assert_exec_opcode(header, initial_registers, opcode, expected_registers, false); | |
2718 | } | |
2719 | ||
2720 | #[test] | |
2721 | fn test_exec_set_basic_block() { | |
2722 | let header = make_test_header(EndianSlice::new(&[], LittleEndian)); | |
2723 | ||
2724 | let mut initial_registers = LineRow::new(&header); | |
2725 | initial_registers.basic_block = false; | |
2726 | ||
2727 | let opcode = LineInstruction::SetBasicBlock; | |
2728 | ||
2729 | let mut expected_registers = initial_registers; | |
2730 | expected_registers.basic_block = true; | |
2731 | ||
2732 | assert_exec_opcode(header, initial_registers, opcode, expected_registers, false); | |
2733 | } | |
2734 | ||
2735 | #[test] | |
2736 | fn test_exec_const_add_pc() { | |
2737 | let header = make_test_header(EndianSlice::new(&[], LittleEndian)); | |
2738 | let initial_registers = LineRow::new(&header); | |
2739 | let opcode = LineInstruction::ConstAddPc; | |
2740 | ||
2741 | let mut expected_registers = initial_registers; | |
2742 | expected_registers.address.0 += 20; | |
2743 | ||
2744 | assert_exec_opcode(header, initial_registers, opcode, expected_registers, false); | |
2745 | } | |
2746 | ||
2747 | #[test] | |
2748 | fn test_exec_fixed_add_pc() { | |
2749 | let header = make_test_header(EndianSlice::new(&[], LittleEndian)); | |
2750 | ||
2751 | let mut initial_registers = LineRow::new(&header); | |
2752 | initial_registers.op_index.0 = 1; | |
2753 | ||
2754 | let opcode = LineInstruction::FixedAddPc(10); | |
2755 | ||
2756 | let mut expected_registers = initial_registers; | |
2757 | expected_registers.address.0 += 10; | |
2758 | expected_registers.op_index.0 = 0; | |
2759 | ||
2760 | assert_exec_opcode(header, initial_registers, opcode, expected_registers, false); | |
2761 | } | |
2762 | ||
2763 | #[test] | |
2764 | fn test_exec_set_prologue_end() { | |
2765 | let header = make_test_header(EndianSlice::new(&[], LittleEndian)); | |
2766 | ||
2767 | let mut initial_registers = LineRow::new(&header); | |
2768 | initial_registers.prologue_end = false; | |
2769 | ||
2770 | let opcode = LineInstruction::SetPrologueEnd; | |
2771 | ||
2772 | let mut expected_registers = initial_registers; | |
2773 | expected_registers.prologue_end = true; | |
2774 | ||
2775 | assert_exec_opcode(header, initial_registers, opcode, expected_registers, false); | |
2776 | } | |
2777 | ||
2778 | #[test] | |
2779 | fn test_exec_set_isa() { | |
2780 | let header = make_test_header(EndianSlice::new(&[], LittleEndian)); | |
2781 | let initial_registers = LineRow::new(&header); | |
2782 | let opcode = LineInstruction::SetIsa(1993); | |
2783 | ||
2784 | let mut expected_registers = initial_registers; | |
2785 | expected_registers.isa = 1993; | |
2786 | ||
2787 | assert_exec_opcode(header, initial_registers, opcode, expected_registers, false); | |
2788 | } | |
2789 | ||
2790 | #[test] | |
2791 | fn test_exec_unknown_standard_0() { | |
2792 | let header = make_test_header(EndianSlice::new(&[], LittleEndian)); | |
2793 | let initial_registers = LineRow::new(&header); | |
2794 | let opcode = LineInstruction::UnknownStandard0(constants::DwLns(111)); | |
2795 | let expected_registers = initial_registers; | |
2796 | assert_exec_opcode(header, initial_registers, opcode, expected_registers, false); | |
2797 | } | |
2798 | ||
2799 | #[test] | |
2800 | fn test_exec_unknown_standard_1() { | |
2801 | let header = make_test_header(EndianSlice::new(&[], LittleEndian)); | |
2802 | let initial_registers = LineRow::new(&header); | |
2803 | let opcode = LineInstruction::UnknownStandard1(constants::DwLns(111), 2); | |
2804 | let expected_registers = initial_registers; | |
2805 | assert_exec_opcode(header, initial_registers, opcode, expected_registers, false); | |
2806 | } | |
2807 | ||
2808 | #[test] | |
2809 | fn test_exec_unknown_standard_n() { | |
2810 | let header = make_test_header(EndianSlice::new(&[], LittleEndian)); | |
2811 | let initial_registers = LineRow::new(&header); | |
2812 | let opcode = LineInstruction::UnknownStandardN( | |
2813 | constants::DwLns(111), | |
2814 | EndianSlice::new(&[2, 2, 2], LittleEndian), | |
2815 | ); | |
2816 | let expected_registers = initial_registers; | |
2817 | assert_exec_opcode(header, initial_registers, opcode, expected_registers, false); | |
2818 | } | |
2819 | ||
2820 | #[test] | |
2821 | fn test_exec_end_sequence() { | |
2822 | let header = make_test_header(EndianSlice::new(&[], LittleEndian)); | |
2823 | let initial_registers = LineRow::new(&header); | |
2824 | let opcode = LineInstruction::EndSequence; | |
2825 | ||
2826 | let mut expected_registers = initial_registers; | |
2827 | expected_registers.end_sequence = true; | |
2828 | ||
2829 | assert_exec_opcode(header, initial_registers, opcode, expected_registers, true); | |
2830 | } | |
2831 | ||
2832 | #[test] | |
2833 | fn test_exec_set_address() { | |
2834 | let header = make_test_header(EndianSlice::new(&[], LittleEndian)); | |
2835 | let initial_registers = LineRow::new(&header); | |
2836 | let opcode = LineInstruction::SetAddress(3030); | |
2837 | ||
2838 | let mut expected_registers = initial_registers; | |
2839 | expected_registers.address.0 = 3030; | |
2840 | ||
2841 | assert_exec_opcode(header, initial_registers, opcode, expected_registers, false); | |
2842 | } | |
2843 | ||
2844 | #[test] | |
2845 | fn test_exec_set_address_tombstone() { | |
2846 | let header = make_test_header(EndianSlice::new(&[], LittleEndian)); | |
2847 | let initial_registers = LineRow::new(&header); | |
2848 | let opcode = LineInstruction::SetAddress(!0); | |
2849 | ||
2850 | let mut expected_registers = initial_registers; | |
2851 | expected_registers.tombstone = true; | |
2852 | expected_registers.address.0 = !0; | |
2853 | ||
2854 | assert_exec_opcode(header, initial_registers, opcode, expected_registers, false); | |
2855 | } | |
2856 | ||
2857 | #[test] | |
2858 | fn test_exec_define_file() { | |
2859 | let mut program = make_test_program(EndianSlice::new(&[], LittleEndian)); | |
2860 | let mut row = LineRow::new(program.header()); | |
2861 | ||
2862 | let file = FileEntry { | |
2863 | path_name: AttributeValue::String(EndianSlice::new(b"test.cpp", LittleEndian)), | |
2864 | directory_index: 0, | |
2865 | timestamp: 0, | |
2866 | size: 0, | |
2867 | md5: [0; 16], | |
2868 | }; | |
2869 | ||
2870 | let opcode = LineInstruction::DefineFile(file); | |
2871 | let is_new_row = row.execute(opcode, &mut program); | |
2872 | ||
2873 | assert_eq!(is_new_row, false); | |
2874 | assert_eq!(Some(&file), program.header().file_names.last()); | |
2875 | } | |
2876 | ||
2877 | #[test] | |
2878 | fn test_exec_set_discriminator() { | |
2879 | let header = make_test_header(EndianSlice::new(&[], LittleEndian)); | |
2880 | let initial_registers = LineRow::new(&header); | |
2881 | let opcode = LineInstruction::SetDiscriminator(9); | |
2882 | ||
2883 | let mut expected_registers = initial_registers; | |
2884 | expected_registers.discriminator = 9; | |
2885 | ||
2886 | assert_exec_opcode(header, initial_registers, opcode, expected_registers, false); | |
2887 | } | |
2888 | ||
2889 | #[test] | |
2890 | fn test_exec_unknown_extended() { | |
2891 | let header = make_test_header(EndianSlice::new(&[], LittleEndian)); | |
2892 | let initial_registers = LineRow::new(&header); | |
2893 | let opcode = LineInstruction::UnknownExtended( | |
2894 | constants::DwLne(74), | |
2895 | EndianSlice::new(&[], LittleEndian), | |
2896 | ); | |
2897 | let expected_registers = initial_registers; | |
2898 | assert_exec_opcode(header, initial_registers, opcode, expected_registers, false); | |
2899 | } | |
2900 | ||
2901 | /// Ensure that `LineRows<R,P>` is covariant wrt R. | |
2902 | /// This only needs to compile. | |
2903 | #[allow(dead_code, unreachable_code, unused_variables)] | |
2904 | fn test_line_rows_variance<'a, 'b>(_: &'a [u8], _: &'b [u8]) | |
2905 | where | |
2906 | 'a: 'b, | |
2907 | { | |
2908 | let a: &OneShotLineRows<EndianSlice<'a, LittleEndian>> = unimplemented!(); | |
2909 | let _: &OneShotLineRows<EndianSlice<'b, LittleEndian>> = a; | |
2910 | } | |
2911 | ||
2912 | #[test] | |
2913 | fn test_parse_debug_line_v5_ok() { | |
2914 | let expected_lengths = &[1, 2]; | |
2915 | let expected_program = &[0, 1, 2, 3, 4]; | |
2916 | let expected_rest = &[5, 6, 7, 8, 9]; | |
2917 | let expected_include_directories = [ | |
2918 | AttributeValue::String(EndianSlice::new(b"dir1", LittleEndian)), | |
2919 | AttributeValue::String(EndianSlice::new(b"dir2", LittleEndian)), | |
2920 | ]; | |
2921 | let expected_file_names = [ | |
2922 | FileEntry { | |
2923 | path_name: AttributeValue::String(EndianSlice::new(b"file1", LittleEndian)), | |
2924 | directory_index: 0, | |
2925 | timestamp: 0, | |
2926 | size: 0, | |
2927 | md5: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], | |
2928 | }, | |
2929 | FileEntry { | |
2930 | path_name: AttributeValue::String(EndianSlice::new(b"file2", LittleEndian)), | |
2931 | directory_index: 1, | |
2932 | timestamp: 0, | |
2933 | size: 0, | |
2934 | md5: [ | |
2935 | 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, | |
2936 | ], | |
2937 | }, | |
2938 | ]; | |
2939 | ||
2940 | for format in vec![Format::Dwarf32, Format::Dwarf64] { | |
2941 | let length = Label::new(); | |
2942 | let header_length = Label::new(); | |
2943 | let start = Label::new(); | |
2944 | let header_start = Label::new(); | |
2945 | let end = Label::new(); | |
2946 | let header_end = Label::new(); | |
2947 | let section = Section::with_endian(Endian::Little) | |
2948 | .initial_length(format, &length, &start) | |
2949 | .D16(5) | |
2950 | // Address size. | |
2951 | .D8(4) | |
2952 | // Segment selector size. | |
2953 | .D8(0) | |
2954 | .word_label(format.word_size(), &header_length) | |
2955 | .mark(&header_start) | |
2956 | // Minimum instruction length. | |
2957 | .D8(1) | |
2958 | // Maximum operations per byte. | |
2959 | .D8(1) | |
2960 | // Default is_stmt. | |
2961 | .D8(1) | |
2962 | // Line base. | |
2963 | .D8(0) | |
2964 | // Line range. | |
2965 | .D8(1) | |
2966 | // Opcode base. | |
2967 | .D8(expected_lengths.len() as u8 + 1) | |
2968 | // Standard opcode lengths for opcodes 1 .. opcode base - 1. | |
2969 | .append_bytes(expected_lengths) | |
2970 | // Directory entry format count. | |
2971 | .D8(1) | |
2972 | .uleb(constants::DW_LNCT_path.0 as u64) | |
2973 | .uleb(constants::DW_FORM_string.0 as u64) | |
2974 | // Directory count. | |
2975 | .D8(2) | |
2976 | .append_bytes(b"dir1\0") | |
2977 | .append_bytes(b"dir2\0") | |
2978 | // File entry format count. | |
2979 | .D8(3) | |
2980 | .uleb(constants::DW_LNCT_path.0 as u64) | |
2981 | .uleb(constants::DW_FORM_string.0 as u64) | |
2982 | .uleb(constants::DW_LNCT_directory_index.0 as u64) | |
2983 | .uleb(constants::DW_FORM_data1.0 as u64) | |
2984 | .uleb(constants::DW_LNCT_MD5.0 as u64) | |
2985 | .uleb(constants::DW_FORM_data16.0 as u64) | |
2986 | // File count. | |
2987 | .D8(2) | |
2988 | .append_bytes(b"file1\0") | |
2989 | .D8(0) | |
2990 | .append_bytes(&expected_file_names[0].md5) | |
2991 | .append_bytes(b"file2\0") | |
2992 | .D8(1) | |
2993 | .append_bytes(&expected_file_names[1].md5) | |
2994 | .mark(&header_end) | |
2995 | // Dummy line program data. | |
2996 | .append_bytes(expected_program) | |
2997 | .mark(&end) | |
2998 | // Dummy trailing data. | |
2999 | .append_bytes(expected_rest); | |
3000 | length.set_const((&end - &start) as u64); | |
3001 | header_length.set_const((&header_end - &header_start) as u64); | |
3002 | let section = section.get_contents().unwrap(); | |
3003 | ||
3004 | let input = &mut EndianSlice::new(§ion, LittleEndian); | |
3005 | ||
3006 | let header = LineProgramHeader::parse(input, DebugLineOffset(0), 0, None, None) | |
3007 | .expect("should parse header ok"); | |
3008 | ||
3009 | assert_eq!(header.raw_program_buf().slice(), expected_program); | |
3010 | assert_eq!(input.slice(), expected_rest); | |
3011 | ||
3012 | assert_eq!(header.offset, DebugLineOffset(0)); | |
3013 | assert_eq!(header.version(), 5); | |
3014 | assert_eq!(header.address_size(), 4); | |
3015 | assert_eq!(header.minimum_instruction_length(), 1); | |
3016 | assert_eq!(header.maximum_operations_per_instruction(), 1); | |
3017 | assert_eq!(header.default_is_stmt(), true); | |
3018 | assert_eq!(header.line_base(), 0); | |
3019 | assert_eq!(header.line_range(), 1); | |
3020 | assert_eq!(header.opcode_base(), expected_lengths.len() as u8 + 1); | |
3021 | assert_eq!(header.standard_opcode_lengths().slice(), expected_lengths); | |
3022 | assert_eq!( | |
3023 | header.directory_entry_format(), | |
3024 | &[FileEntryFormat { | |
3025 | content_type: constants::DW_LNCT_path, | |
3026 | form: constants::DW_FORM_string, | |
3027 | }] | |
3028 | ); | |
3029 | assert_eq!(header.include_directories(), expected_include_directories); | |
3030 | assert_eq!(header.directory(0), Some(expected_include_directories[0])); | |
3031 | assert_eq!( | |
3032 | header.file_name_entry_format(), | |
3033 | &[ | |
3034 | FileEntryFormat { | |
3035 | content_type: constants::DW_LNCT_path, | |
3036 | form: constants::DW_FORM_string, | |
3037 | }, | |
3038 | FileEntryFormat { | |
3039 | content_type: constants::DW_LNCT_directory_index, | |
3040 | form: constants::DW_FORM_data1, | |
3041 | }, | |
3042 | FileEntryFormat { | |
3043 | content_type: constants::DW_LNCT_MD5, | |
3044 | form: constants::DW_FORM_data16, | |
3045 | } | |
3046 | ] | |
3047 | ); | |
3048 | assert_eq!(header.file_names(), expected_file_names); | |
3049 | assert_eq!(header.file(0), Some(&expected_file_names[0])); | |
3050 | } | |
3051 | } | |
3052 | ||
3053 | #[test] | |
3054 | fn test_sequences() { | |
3055 | #[rustfmt::skip] | |
3056 | let buf = [ | |
3057 | // 32-bit length | |
3058 | 94, 0x00, 0x00, 0x00, | |
3059 | // Version. | |
3060 | 0x04, 0x00, | |
3061 | // Header length = 40. | |
3062 | 0x28, 0x00, 0x00, 0x00, | |
3063 | // Minimum instruction length. | |
3064 | 0x01, | |
3065 | // Maximum operations per byte. | |
3066 | 0x01, | |
3067 | // Default is_stmt. | |
3068 | 0x01, | |
3069 | // Line base. | |
3070 | 0x00, | |
3071 | // Line range. | |
3072 | 0x01, | |
3073 | // Opcode base. | |
3074 | 0x03, | |
3075 | // Standard opcode lengths for opcodes 1 .. opcode base - 1. | |
3076 | 0x01, 0x02, | |
3077 | // Include directories = '/', 'i', 'n', 'c', '\0', '/', 'i', 'n', 'c', '2', '\0', '\0' | |
3078 | 0x2f, 0x69, 0x6e, 0x63, 0x00, 0x2f, 0x69, 0x6e, 0x63, 0x32, 0x00, 0x00, | |
3079 | // File names | |
3080 | // foo.rs | |
3081 | 0x66, 0x6f, 0x6f, 0x2e, 0x72, 0x73, 0x00, | |
3082 | 0x00, | |
3083 | 0x00, | |
3084 | 0x00, | |
3085 | // bar.h | |
3086 | 0x62, 0x61, 0x72, 0x2e, 0x68, 0x00, | |
3087 | 0x01, | |
3088 | 0x00, | |
3089 | 0x00, | |
3090 | // End file names. | |
3091 | 0x00, | |
3092 | ||
3093 | 0, 5, constants::DW_LNE_set_address.0, 1, 0, 0, 0, | |
3094 | constants::DW_LNS_copy.0, | |
3095 | constants::DW_LNS_advance_pc.0, 1, | |
3096 | constants::DW_LNS_copy.0, | |
3097 | constants::DW_LNS_advance_pc.0, 2, | |
3098 | 0, 1, constants::DW_LNE_end_sequence.0, | |
3099 | ||
3100 | // Tombstone | |
3101 | 0, 5, constants::DW_LNE_set_address.0, 0xff, 0xff, 0xff, 0xff, | |
3102 | constants::DW_LNS_copy.0, | |
3103 | constants::DW_LNS_advance_pc.0, 1, | |
3104 | constants::DW_LNS_copy.0, | |
3105 | constants::DW_LNS_advance_pc.0, 2, | |
3106 | 0, 1, constants::DW_LNE_end_sequence.0, | |
3107 | ||
3108 | 0, 5, constants::DW_LNE_set_address.0, 11, 0, 0, 0, | |
3109 | constants::DW_LNS_copy.0, | |
3110 | constants::DW_LNS_advance_pc.0, 1, | |
3111 | constants::DW_LNS_copy.0, | |
3112 | constants::DW_LNS_advance_pc.0, 2, | |
3113 | 0, 1, constants::DW_LNE_end_sequence.0, | |
3114 | ]; | |
3115 | assert_eq!(buf[0] as usize, buf.len() - 4); | |
3116 | ||
3117 | let rest = &mut EndianSlice::new(&buf, LittleEndian); | |
3118 | ||
3119 | let header = LineProgramHeader::parse(rest, DebugLineOffset(0), 4, None, None) | |
3120 | .expect("should parse header ok"); | |
3121 | let program = IncompleteLineProgram { header }; | |
3122 | ||
3123 | let sequences = program.sequences().unwrap().1; | |
3124 | assert_eq!(sequences.len(), 2); | |
3125 | assert_eq!(sequences[0].start, 1); | |
3126 | assert_eq!(sequences[0].end, 4); | |
3127 | assert_eq!(sequences[1].start, 11); | |
3128 | assert_eq!(sequences[1].end, 14); | |
3129 | } | |
3130 | } |