Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(75)

Side by Side Diff: experimental/visual_studio_plugin/third_party/breakpad/common/dwarf/dwarf2reader.cc

Issue 10928195: First round of dead file removal (Closed) Base URL: https://github.com/samclegg/nativeclient-sdk.git@master
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2010 Google Inc. All Rights Reserved.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 // * Neither the name of Google Inc. nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 // CFI reader author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com>
30
31 // Implementation of dwarf2reader::LineInfo, dwarf2reader::CompilationUnit,
32 // and dwarf2reader::CallFrameInfo. See dwarf2reader.h for details.
33
34 #include <cassert>
35 #include <cstdio>
36 #include <cstring>
37 #include <map>
38 #include <memory>
39 #include <stack>
40 #include <utility>
41
42 #include "common/dwarf/bytereader-inl.h"
43 #include "common/dwarf/dwarf2reader.h"
44 #include "common/dwarf/bytereader.h"
45 #include "common/dwarf/line_state_machine.h"
46
47 namespace dwarf2reader {
48
49 CompilationUnit::CompilationUnit(const SectionMap& sections, uint64 offset,
50 ByteReader* reader, Dwarf2Handler* handler)
51 : offset_from_section_start_(offset), reader_(reader),
52 sections_(sections), handler_(handler), abbrevs_(NULL),
53 string_buffer_(NULL), string_buffer_length_(0) {}
54
55 // Read a DWARF2/3 abbreviation section.
56 // Each abbrev consists of a abbreviation number, a tag, a byte
57 // specifying whether the tag has children, and a list of
58 // attribute/form pairs.
59 // The list of forms is terminated by a 0 for the attribute, and a
60 // zero for the form. The entire abbreviation section is terminated
61 // by a zero for the code.
62
63 void CompilationUnit::ReadAbbrevs() {
64 if (abbrevs_)
65 return;
66
67 // First get the debug_abbrev section. ".debug_abbrev" is the name
68 // recommended in the DWARF spec, and used on Linux;
69 // "__debug_abbrev" is the name used in Mac OS X Mach-O files.
70 SectionMap::const_iterator iter = sections_.find(".debug_abbrev");
71 if (iter == sections_.end())
72 iter = sections_.find("__debug_abbrev");
73 assert(iter != sections_.end());
74
75 abbrevs_ = new vector<Abbrev>;
76 abbrevs_->resize(1);
77
78 // The only way to check whether we are reading over the end of the
79 // buffer would be to first compute the size of the leb128 data by
80 // reading it, then go back and read it again.
81 const char* abbrev_start = iter->second.first +
82 header_.abbrev_offset;
83 const char* abbrevptr = abbrev_start;
84 #ifndef NDEBUG
85 const uint64 abbrev_length = iter->second.second - header_.abbrev_offset;
86 #endif
87
88 while (1) {
89 CompilationUnit::Abbrev abbrev;
90 size_t len;
91 const uint32 number = reader_->ReadUnsignedLEB128(abbrevptr, &len);
92
93 if (number == 0)
94 break;
95 abbrev.number = number;
96 abbrevptr += len;
97
98 assert(abbrevptr < abbrev_start + abbrev_length);
99 const uint32 tag = reader_->ReadUnsignedLEB128(abbrevptr, &len);
100 abbrevptr += len;
101 abbrev.tag = static_cast<enum DwarfTag>(tag);
102
103 assert(abbrevptr < abbrev_start + abbrev_length);
104 abbrev.has_children = reader_->ReadOneByte(abbrevptr);
105 abbrevptr += 1;
106
107 assert(abbrevptr < abbrev_start + abbrev_length);
108
109 while (1) {
110 const uint32 nametemp = reader_->ReadUnsignedLEB128(abbrevptr, &len);
111 abbrevptr += len;
112
113 assert(abbrevptr < abbrev_start + abbrev_length);
114 const uint32 formtemp = reader_->ReadUnsignedLEB128(abbrevptr, &len);
115 abbrevptr += len;
116 if (nametemp == 0 && formtemp == 0)
117 break;
118
119 const enum DwarfAttribute name =
120 static_cast<enum DwarfAttribute>(nametemp);
121 const enum DwarfForm form = static_cast<enum DwarfForm>(formtemp);
122 abbrev.attributes.push_back(make_pair(name, form));
123 }
124 assert(abbrev.number == abbrevs_->size());
125 abbrevs_->push_back(abbrev);
126 }
127 }
128
129 // Skips a single DIE's attributes.
130 const char* CompilationUnit::SkipDIE(const char* start,
131 const Abbrev& abbrev) {
132 for (AttributeList::const_iterator i = abbrev.attributes.begin();
133 i != abbrev.attributes.end();
134 i++) {
135 start = SkipAttribute(start, i->second);
136 }
137 return start;
138 }
139
140 // Skips a single attribute form's data.
141 const char* CompilationUnit::SkipAttribute(const char* start,
142 enum DwarfForm form) {
143 size_t len;
144
145 switch (form) {
146 case DW_FORM_indirect:
147 form = static_cast<enum DwarfForm>(reader_->ReadUnsignedLEB128(start,
148 &len));
149 start += len;
150 return SkipAttribute(start, form);
151 break;
152
153 case DW_FORM_data1:
154 case DW_FORM_flag:
155 case DW_FORM_ref1:
156 return start + 1;
157 break;
158 case DW_FORM_ref2:
159 case DW_FORM_data2:
160 return start + 2;
161 break;
162 case DW_FORM_ref4:
163 case DW_FORM_data4:
164 return start + 4;
165 break;
166 case DW_FORM_ref8:
167 case DW_FORM_data8:
168 return start + 8;
169 break;
170 case DW_FORM_string:
171 return start + strlen(start) + 1;
172 break;
173 case DW_FORM_udata:
174 case DW_FORM_ref_udata:
175 reader_->ReadUnsignedLEB128(start, &len);
176 return start + len;
177 break;
178
179 case DW_FORM_sdata:
180 reader_->ReadSignedLEB128(start, &len);
181 return start + len;
182 break;
183 case DW_FORM_addr:
184 return start + reader_->AddressSize();
185 break;
186 case DW_FORM_ref_addr:
187 // DWARF2 and 3 differ on whether ref_addr is address size or
188 // offset size.
189 assert(header_.version == 2 || header_.version == 3);
190 if (header_.version == 2) {
191 return start + reader_->AddressSize();
192 } else if (header_.version == 3) {
193 return start + reader_->OffsetSize();
194 }
195 break;
196
197 case DW_FORM_block1:
198 return start + 1 + reader_->ReadOneByte(start);
199 break;
200 case DW_FORM_block2:
201 return start + 2 + reader_->ReadTwoBytes(start);
202 break;
203 case DW_FORM_block4:
204 return start + 4 + reader_->ReadFourBytes(start);
205 break;
206 case DW_FORM_block: {
207 uint64 size = reader_->ReadUnsignedLEB128(start, &len);
208 return start + size + len;
209 }
210 break;
211 case DW_FORM_strp:
212 return start + reader_->OffsetSize();
213 break;
214 default:
215 fprintf(stderr,"Unhandled form type");
216 }
217 fprintf(stderr,"Unhandled form type");
218 return NULL;
219 }
220
221 // Read a DWARF2/3 header.
222 // The header is variable length in DWARF3 (and DWARF2 as extended by
223 // most compilers), and consists of an length field, a version number,
224 // the offset in the .debug_abbrev section for our abbrevs, and an
225 // address size.
226 void CompilationUnit::ReadHeader() {
227 const char* headerptr = buffer_;
228 size_t initial_length_size;
229
230 assert(headerptr + 4 < buffer_ + buffer_length_);
231 const uint64 initial_length
232 = reader_->ReadInitialLength(headerptr, &initial_length_size);
233 headerptr += initial_length_size;
234 header_.length = initial_length;
235
236 assert(headerptr + 2 < buffer_ + buffer_length_);
237 header_.version = reader_->ReadTwoBytes(headerptr);
238 headerptr += 2;
239
240 assert(headerptr + reader_->OffsetSize() < buffer_ + buffer_length_);
241 header_.abbrev_offset = reader_->ReadOffset(headerptr);
242 headerptr += reader_->OffsetSize();
243
244 assert(headerptr + 1 < buffer_ + buffer_length_);
245 header_.address_size = reader_->ReadOneByte(headerptr);
246 reader_->SetAddressSize(header_.address_size);
247 headerptr += 1;
248
249 after_header_ = headerptr;
250
251 // This check ensures that we don't have to do checking during the
252 // reading of DIEs. header_.length does not include the size of the
253 // initial length.
254 assert(buffer_ + initial_length_size + header_.length <=
255 buffer_ + buffer_length_);
256 }
257
258 uint64 CompilationUnit::Start() {
259 // First get the debug_info section. ".debug_info" is the name
260 // recommended in the DWARF spec, and used on Linux; "__debug_info"
261 // is the name used in Mac OS X Mach-O files.
262 SectionMap::const_iterator iter = sections_.find(".debug_info");
263 if (iter == sections_.end())
264 iter = sections_.find("__debug_info");
265 assert(iter != sections_.end());
266
267 // Set up our buffer
268 buffer_ = iter->second.first + offset_from_section_start_;
269 buffer_length_ = iter->second.second - offset_from_section_start_;
270
271 // Read the header
272 ReadHeader();
273
274 // Figure out the real length from the end of the initial length to
275 // the end of the compilation unit, since that is the value we
276 // return.
277 uint64 ourlength = header_.length;
278 if (reader_->OffsetSize() == 8)
279 ourlength += 12;
280 else
281 ourlength += 4;
282
283 // See if the user wants this compilation unit, and if not, just return.
284 if (!handler_->StartCompilationUnit(offset_from_section_start_,
285 reader_->AddressSize(),
286 reader_->OffsetSize(),
287 header_.length,
288 header_.version))
289 return ourlength;
290
291 // Otherwise, continue by reading our abbreviation entries.
292 ReadAbbrevs();
293
294 // Set the string section if we have one. ".debug_str" is the name
295 // recommended in the DWARF spec, and used on Linux; "__debug_str"
296 // is the name used in Mac OS X Mach-O files.
297 iter = sections_.find(".debug_str");
298 if (iter == sections_.end())
299 iter = sections_.find("__debug_str");
300 if (iter != sections_.end()) {
301 string_buffer_ = iter->second.first;
302 string_buffer_length_ = iter->second.second;
303 }
304
305 // Now that we have our abbreviations, start processing DIE's.
306 ProcessDIEs();
307
308 return ourlength;
309 }
310
311 // If one really wanted, you could merge SkipAttribute and
312 // ProcessAttribute
313 // This is all boring data manipulation and calling of the handler.
314 const char* CompilationUnit::ProcessAttribute(
315 uint64 dieoffset, const char* start, enum DwarfAttribute attr,
316 enum DwarfForm form) {
317 size_t len;
318
319 switch (form) {
320 // DW_FORM_indirect is never used because it is such a space
321 // waster.
322 case DW_FORM_indirect:
323 form = static_cast<enum DwarfForm>(reader_->ReadUnsignedLEB128(start,
324 &len));
325 start += len;
326 return ProcessAttribute(dieoffset, start, attr, form);
327 break;
328
329 case DW_FORM_data1:
330 case DW_FORM_flag:
331 handler_->ProcessAttributeUnsigned(dieoffset, attr, form,
332 reader_->ReadOneByte(start));
333 return start + 1;
334 break;
335 case DW_FORM_data2:
336 handler_->ProcessAttributeUnsigned(dieoffset, attr, form,
337 reader_->ReadTwoBytes(start));
338 return start + 2;
339 break;
340 case DW_FORM_data4:
341 handler_->ProcessAttributeUnsigned(dieoffset, attr, form,
342 reader_->ReadFourBytes(start));
343 return start + 4;
344 break;
345 case DW_FORM_data8:
346 handler_->ProcessAttributeUnsigned(dieoffset, attr, form,
347 reader_->ReadEightBytes(start));
348 return start + 8;
349 break;
350 case DW_FORM_string: {
351 const char* str = start;
352 handler_->ProcessAttributeString(dieoffset, attr, form,
353 str);
354 return start + strlen(str) + 1;
355 }
356 break;
357 case DW_FORM_udata:
358 handler_->ProcessAttributeUnsigned(dieoffset, attr, form,
359 reader_->ReadUnsignedLEB128(start,
360 &len));
361 return start + len;
362 break;
363
364 case DW_FORM_sdata:
365 handler_->ProcessAttributeSigned(dieoffset, attr, form,
366 reader_->ReadSignedLEB128(start, &len));
367 return start + len;
368 break;
369 case DW_FORM_addr:
370 handler_->ProcessAttributeUnsigned(dieoffset, attr, form,
371 reader_->ReadAddress(start));
372 return start + reader_->AddressSize();
373 break;
374
375 case DW_FORM_ref1:
376 handler_->ProcessAttributeReference(dieoffset, attr, form,
377 reader_->ReadOneByte(start)
378 + offset_from_section_start_);
379 return start + 1;
380 break;
381 case DW_FORM_ref2:
382 handler_->ProcessAttributeReference(dieoffset, attr, form,
383 reader_->ReadTwoBytes(start)
384 + offset_from_section_start_);
385 return start + 2;
386 break;
387 case DW_FORM_ref4:
388 handler_->ProcessAttributeReference(dieoffset, attr, form,
389 reader_->ReadFourBytes(start)
390 + offset_from_section_start_);
391 return start + 4;
392 break;
393 case DW_FORM_ref8:
394 handler_->ProcessAttributeReference(dieoffset, attr, form,
395 reader_->ReadEightBytes(start)
396 + offset_from_section_start_);
397 return start + 8;
398 break;
399 case DW_FORM_ref_udata:
400 handler_->ProcessAttributeReference(dieoffset, attr, form,
401 reader_->ReadUnsignedLEB128(start,
402 &len)
403 + offset_from_section_start_);
404 return start + len;
405 break;
406 case DW_FORM_ref_addr:
407 // DWARF2 and 3 differ on whether ref_addr is address size or
408 // offset size.
409 assert(header_.version == 2 || header_.version == 3);
410 if (header_.version == 2) {
411 handler_->ProcessAttributeReference(dieoffset, attr, form,
412 reader_->ReadAddress(start));
413 return start + reader_->AddressSize();
414 } else if (header_.version == 3) {
415 handler_->ProcessAttributeReference(dieoffset, attr, form,
416 reader_->ReadOffset(start));
417 return start + reader_->OffsetSize();
418 }
419 break;
420
421 case DW_FORM_block1: {
422 uint64 datalen = reader_->ReadOneByte(start);
423 handler_->ProcessAttributeBuffer(dieoffset, attr, form, start + 1,
424 datalen);
425 return start + 1 + datalen;
426 }
427 break;
428 case DW_FORM_block2: {
429 uint64 datalen = reader_->ReadTwoBytes(start);
430 handler_->ProcessAttributeBuffer(dieoffset, attr, form, start + 2,
431 datalen);
432 return start + 2 + datalen;
433 }
434 break;
435 case DW_FORM_block4: {
436 uint64 datalen = reader_->ReadFourBytes(start);
437 handler_->ProcessAttributeBuffer(dieoffset, attr, form, start + 4,
438 datalen);
439 return start + 4 + datalen;
440 }
441 break;
442 case DW_FORM_block: {
443 uint64 datalen = reader_->ReadUnsignedLEB128(start, &len);
444 handler_->ProcessAttributeBuffer(dieoffset, attr, form, start + len,
445 datalen);
446 return start + datalen + len;
447 }
448 break;
449 case DW_FORM_strp: {
450 assert(string_buffer_ != NULL);
451
452 const uint64 offset = reader_->ReadOffset(start);
453 assert(string_buffer_ + offset < string_buffer_ + string_buffer_length_);
454
455 const char* str = string_buffer_ + offset;
456 handler_->ProcessAttributeString(dieoffset, attr, form,
457 str);
458 return start + reader_->OffsetSize();
459 }
460 break;
461 default:
462 fprintf(stderr, "Unhandled form type");
463 }
464 fprintf(stderr, "Unhandled form type");
465 return NULL;
466 }
467
468 const char* CompilationUnit::ProcessDIE(uint64 dieoffset,
469 const char* start,
470 const Abbrev& abbrev) {
471 for (AttributeList::const_iterator i = abbrev.attributes.begin();
472 i != abbrev.attributes.end();
473 i++) {
474 start = ProcessAttribute(dieoffset, start, i->first, i->second);
475 }
476 return start;
477 }
478
479 void CompilationUnit::ProcessDIEs() {
480 const char* dieptr = after_header_;
481 size_t len;
482
483 // lengthstart is the place the length field is based on.
484 // It is the point in the header after the initial length field
485 const char* lengthstart = buffer_;
486
487 // In 64 bit dwarf, the initial length is 12 bytes, because of the
488 // 0xffffffff at the start.
489 if (reader_->OffsetSize() == 8)
490 lengthstart += 12;
491 else
492 lengthstart += 4;
493
494 // we need semantics of boost scoped_ptr here - no intention of trasnferring
495 // ownership of the stack. use const, but then we limit ourselves to not
496 // ever being able to call .reset() on the smart pointer.
497 std::auto_ptr<stack<uint64> > const die_stack(new stack<uint64>);
498
499 while (dieptr < (lengthstart + header_.length)) {
500 // We give the user the absolute offset from the beginning of
501 // debug_info, since they need it to deal with ref_addr forms.
502 uint64 absolute_offset = (dieptr - buffer_) + offset_from_section_start_;
503
504 uint64 abbrev_num = reader_->ReadUnsignedLEB128(dieptr, &len);
505
506 dieptr += len;
507
508 // Abbrev == 0 represents the end of a list of children.
509 if (abbrev_num == 0) {
510 const uint64 offset = die_stack->top();
511 die_stack->pop();
512 handler_->EndDIE(offset);
513 continue;
514 }
515
516 const Abbrev& abbrev = abbrevs_->at(abbrev_num);
517 const enum DwarfTag tag = abbrev.tag;
518 if (!handler_->StartDIE(absolute_offset, tag, abbrev.attributes)) {
519 dieptr = SkipDIE(dieptr, abbrev);
520 } else {
521 dieptr = ProcessDIE(absolute_offset, dieptr, abbrev);
522 }
523
524 if (abbrev.has_children) {
525 die_stack->push(absolute_offset);
526 } else {
527 handler_->EndDIE(absolute_offset);
528 }
529 }
530 }
531
532 LineInfo::LineInfo(const char* buffer, uint64 buffer_length,
533 ByteReader* reader, LineInfoHandler* handler):
534 handler_(handler), reader_(reader), buffer_(buffer),
535 buffer_length_(buffer_length) {
536 header_.std_opcode_lengths = NULL;
537 }
538
539 uint64 LineInfo::Start() {
540 ReadHeader();
541 ReadLines();
542 return after_header_ - buffer_;
543 }
544
545 // The header for a debug_line section is mildly complicated, because
546 // the line info is very tightly encoded.
547 void LineInfo::ReadHeader() {
548 const char* lineptr = buffer_;
549 size_t initial_length_size;
550
551 const uint64 initial_length
552 = reader_->ReadInitialLength(lineptr, &initial_length_size);
553
554 lineptr += initial_length_size;
555 header_.total_length = initial_length;
556 assert(buffer_ + initial_length_size + header_.total_length <=
557 buffer_ + buffer_length_);
558
559 // Address size *must* be set by CU ahead of time.
560 assert(reader_->AddressSize() != 0);
561
562 header_.version = reader_->ReadTwoBytes(lineptr);
563 lineptr += 2;
564
565 header_.prologue_length = reader_->ReadOffset(lineptr);
566 lineptr += reader_->OffsetSize();
567
568 header_.min_insn_length = reader_->ReadOneByte(lineptr);
569 lineptr += 1;
570
571 header_.default_is_stmt = reader_->ReadOneByte(lineptr);
572 lineptr += 1;
573
574 header_.line_base = *reinterpret_cast<const int8*>(lineptr);
575 lineptr += 1;
576
577 header_.line_range = reader_->ReadOneByte(lineptr);
578 lineptr += 1;
579
580 header_.opcode_base = reader_->ReadOneByte(lineptr);
581 lineptr += 1;
582
583 header_.std_opcode_lengths = new vector<unsigned char>;
584 header_.std_opcode_lengths->resize(header_.opcode_base + 1);
585 (*header_.std_opcode_lengths)[0] = 0;
586 for (int i = 1; i < header_.opcode_base; i++) {
587 (*header_.std_opcode_lengths)[i] = reader_->ReadOneByte(lineptr);
588 lineptr += 1;
589 }
590
591 // It is legal for the directory entry table to be empty.
592 if (*lineptr) {
593 uint32 dirindex = 1;
594 while (*lineptr) {
595 const char* dirname = lineptr;
596 handler_->DefineDir(dirname, dirindex);
597 lineptr += strlen(dirname) + 1;
598 dirindex++;
599 }
600 }
601 lineptr++;
602
603 // It is also legal for the file entry table to be empty.
604 if (*lineptr) {
605 uint32 fileindex = 1;
606 size_t len;
607 while (*lineptr) {
608 const char* filename = lineptr;
609 lineptr += strlen(filename) + 1;
610
611 uint64 dirindex = reader_->ReadUnsignedLEB128(lineptr, &len);
612 lineptr += len;
613
614 uint64 mod_time = reader_->ReadUnsignedLEB128(lineptr, &len);
615 lineptr += len;
616
617 uint64 filelength = reader_->ReadUnsignedLEB128(lineptr, &len);
618 lineptr += len;
619 handler_->DefineFile(filename, fileindex, dirindex, mod_time,
620 filelength);
621 fileindex++;
622 }
623 }
624 lineptr++;
625
626 after_header_ = lineptr;
627 }
628
629 /* static */
630 bool LineInfo::ProcessOneOpcode(ByteReader* reader,
631 LineInfoHandler* handler,
632 const struct LineInfoHeader &header,
633 const char* start,
634 struct LineStateMachine* lsm,
635 size_t* len,
636 uintptr pc,
637 bool *lsm_passes_pc) {
638 size_t oplen = 0;
639 size_t templen;
640 uint8 opcode = reader->ReadOneByte(start);
641 oplen++;
642 start++;
643
644 // If the opcode is great than the opcode_base, it is a special
645 // opcode. Most line programs consist mainly of special opcodes.
646 if (opcode >= header.opcode_base) {
647 opcode -= header.opcode_base;
648 const int64 advance_address = (opcode / header.line_range)
649 * header.min_insn_length;
650 const int64 advance_line = (opcode % header.line_range)
651 + header.line_base;
652
653 // Check if the lsm passes "pc". If so, mark it as passed.
654 if (lsm_passes_pc &&
655 lsm->address <= pc && pc < lsm->address + advance_address) {
656 *lsm_passes_pc = true;
657 }
658
659 lsm->address += advance_address;
660 lsm->line_num += advance_line;
661 lsm->basic_block = true;
662 *len = oplen;
663 return true;
664 }
665
666 // Otherwise, we have the regular opcodes
667 switch (opcode) {
668 case DW_LNS_copy: {
669 lsm->basic_block = false;
670 *len = oplen;
671 return true;
672 }
673
674 case DW_LNS_advance_pc: {
675 uint64 advance_address = reader->ReadUnsignedLEB128(start, &templen);
676 oplen += templen;
677
678 // Check if the lsm passes "pc". If so, mark it as passed.
679 if (lsm_passes_pc && lsm->address <= pc &&
680 pc < lsm->address + header.min_insn_length * advance_address) {
681 *lsm_passes_pc = true;
682 }
683
684 lsm->address += header.min_insn_length * advance_address;
685 }
686 break;
687 case DW_LNS_advance_line: {
688 const int64 advance_line = reader->ReadSignedLEB128(start, &templen);
689 oplen += templen;
690 lsm->line_num += advance_line;
691
692 // With gcc 4.2.1, we can get the line_no here for the first time
693 // since DW_LNS_advance_line is called after DW_LNE_set_address is
694 // called. So we check if the lsm passes "pc" here, not in
695 // DW_LNE_set_address.
696 if (lsm_passes_pc && lsm->address == pc) {
697 *lsm_passes_pc = true;
698 }
699 }
700 break;
701 case DW_LNS_set_file: {
702 const uint64 fileno = reader->ReadUnsignedLEB128(start, &templen);
703 oplen += templen;
704 lsm->file_num = fileno;
705 }
706 break;
707 case DW_LNS_set_column: {
708 const uint64 colno = reader->ReadUnsignedLEB128(start, &templen);
709 oplen += templen;
710 lsm->column_num = colno;
711 }
712 break;
713 case DW_LNS_negate_stmt: {
714 lsm->is_stmt = !lsm->is_stmt;
715 }
716 break;
717 case DW_LNS_set_basic_block: {
718 lsm->basic_block = true;
719 }
720 break;
721 case DW_LNS_fixed_advance_pc: {
722 const uint16 advance_address = reader->ReadTwoBytes(start);
723 oplen += 2;
724
725 // Check if the lsm passes "pc". If so, mark it as passed.
726 if (lsm_passes_pc &&
727 lsm->address <= pc && pc < lsm->address + advance_address) {
728 *lsm_passes_pc = true;
729 }
730
731 lsm->address += advance_address;
732 }
733 break;
734 case DW_LNS_const_add_pc: {
735 const int64 advance_address = header.min_insn_length
736 * ((255 - header.opcode_base)
737 / header.line_range);
738
739 // Check if the lsm passes "pc". If so, mark it as passed.
740 if (lsm_passes_pc &&
741 lsm->address <= pc && pc < lsm->address + advance_address) {
742 *lsm_passes_pc = true;
743 }
744
745 lsm->address += advance_address;
746 }
747 break;
748 case DW_LNS_extended_op: {
749 const size_t extended_op_len = reader->ReadUnsignedLEB128(start,
750 &templen);
751 start += templen;
752 oplen += templen + extended_op_len;
753
754 const uint64 extended_op = reader->ReadOneByte(start);
755 start++;
756
757 switch (extended_op) {
758 case DW_LNE_end_sequence: {
759 lsm->end_sequence = true;
760 *len = oplen;
761 return true;
762 }
763 break;
764 case DW_LNE_set_address: {
765 // With gcc 4.2.1, we cannot tell the line_no here since
766 // DW_LNE_set_address is called before DW_LNS_advance_line is
767 // called. So we do not check if the lsm passes "pc" here. See
768 // also the comment in DW_LNS_advance_line.
769 uint64 address = reader->ReadAddress(start);
770 lsm->address = address;
771 }
772 break;
773 case DW_LNE_define_file: {
774 const char* filename = start;
775
776 templen = strlen(filename) + 1;
777 start += templen;
778
779 uint64 dirindex = reader->ReadUnsignedLEB128(start, &templen);
780 oplen += templen;
781
782 const uint64 mod_time = reader->ReadUnsignedLEB128(start,
783 &templen);
784 oplen += templen;
785
786 const uint64 filelength = reader->ReadUnsignedLEB128(start,
787 &templen);
788 oplen += templen;
789
790 if (handler) {
791 handler->DefineFile(filename, -1, dirindex, mod_time,
792 filelength);
793 }
794 }
795 break;
796 }
797 }
798 break;
799
800 default: {
801 // Ignore unknown opcode silently
802 if (header.std_opcode_lengths) {
803 for (int i = 0; i < (*header.std_opcode_lengths)[opcode]; i++) {
804 size_t templen;
805 reader->ReadUnsignedLEB128(start, &templen);
806 start += templen;
807 oplen += templen;
808 }
809 }
810 }
811 break;
812 }
813 *len = oplen;
814 return false;
815 }
816
817 void LineInfo::ReadLines() {
818 struct LineStateMachine lsm;
819
820 // lengthstart is the place the length field is based on.
821 // It is the point in the header after the initial length field
822 const char* lengthstart = buffer_;
823
824 // In 64 bit dwarf, the initial length is 12 bytes, because of the
825 // 0xffffffff at the start.
826 if (reader_->OffsetSize() == 8)
827 lengthstart += 12;
828 else
829 lengthstart += 4;
830
831 const char* lineptr = after_header_;
832 lsm.Reset(header_.default_is_stmt);
833
834 // The LineInfoHandler interface expects each line's length along
835 // with its address, but DWARF only provides addresses (sans
836 // length), and an end-of-sequence address; one infers the length
837 // from the next address. So we report a line only when we get the
838 // next line's address, or the end-of-sequence address.
839 bool have_pending_line = false;
840 uint64 pending_address = 0;
841 uint32 pending_file_num = 0, pending_line_num = 0, pending_column_num = 0;
842
843 while (lineptr < lengthstart + header_.total_length) {
844 size_t oplength;
845 bool add_row = ProcessOneOpcode(reader_, handler_, header_,
846 lineptr, &lsm, &oplength, (uintptr)-1,
847 NULL);
848 if (add_row) {
849 if (have_pending_line)
850 handler_->AddLine(pending_address, lsm.address - pending_address,
851 pending_file_num, pending_line_num,
852 pending_column_num);
853 if (lsm.end_sequence) {
854 lsm.Reset(header_.default_is_stmt);
855 have_pending_line = false;
856 } else {
857 pending_address = lsm.address;
858 pending_file_num = lsm.file_num;
859 pending_line_num = lsm.line_num;
860 pending_column_num = lsm.column_num;
861 have_pending_line = true;
862 }
863 }
864 lineptr += oplength;
865 }
866
867 after_header_ = lengthstart + header_.total_length;
868 }
869
870 // A DWARF rule for recovering the address or value of a register, or
871 // computing the canonical frame address. There is one subclass of this for
872 // each '*Rule' member function in CallFrameInfo::Handler.
873 //
874 // It's annoying that we have to handle Rules using pointers (because
875 // the concrete instances can have an arbitrary size). They're small,
876 // so it would be much nicer if we could just handle them by value
877 // instead of fretting about ownership and destruction.
878 //
879 // It seems like all these could simply be instances of std::tr1::bind,
880 // except that we need instances to be EqualityComparable, too.
881 //
882 // This could logically be nested within State, but then the qualified names
883 // get horrendous.
884 class CallFrameInfo::Rule {
885 public:
886 virtual ~Rule() { }
887
888 // Tell HANDLER that, at ADDRESS in the program, REGISTER can be
889 // recovered using this rule. If REGISTER is kCFARegister, then this rule
890 // describes how to compute the canonical frame address. Return what the
891 // HANDLER member function returned.
892 virtual bool Handle(Handler *handler,
893 uint64 address, int register) const = 0;
894
895 // Equality on rules. We use these to decide which rules we need
896 // to report after a DW_CFA_restore_state instruction.
897 virtual bool operator==(const Rule &rhs) const = 0;
898
899 bool operator!=(const Rule &rhs) const { return ! (*this == rhs); }
900
901 // Return a pointer to a copy of this rule.
902 virtual Rule *Copy() const = 0;
903
904 // If this is a base+offset rule, change its base register to REG.
905 // Otherwise, do nothing. (Ugly, but required for DW_CFA_def_cfa_register.)
906 virtual void SetBaseRegister(unsigned reg) { }
907
908 // If this is a base+offset rule, change its offset to OFFSET. Otherwise,
909 // do nothing. (Ugly, but required for DW_CFA_def_cfa_offset.)
910 virtual void SetOffset(long long offset) { }
911 };
912
913 // Rule: the value the register had in the caller cannot be recovered.
914 class CallFrameInfo::UndefinedRule: public CallFrameInfo::Rule {
915 public:
916 UndefinedRule() { }
917 ~UndefinedRule() { }
918 bool Handle(Handler *handler, uint64 address, int reg) const {
919 return handler->UndefinedRule(address, reg);
920 }
921 bool operator==(const Rule &rhs) const {
922 // dynamic_cast is allowed by the Google C++ Style Guide, if the use has
923 // been carefully considered; cheap RTTI-like workarounds are forbidden.
924 const UndefinedRule *our_rhs = dynamic_cast<const UndefinedRule *>(&rhs);
925 return (our_rhs != NULL);
926 }
927 Rule *Copy() const { return new UndefinedRule(*this); }
928 };
929
930 // Rule: the register's value is the same as that it had in the caller.
931 class CallFrameInfo::SameValueRule: public CallFrameInfo::Rule {
932 public:
933 SameValueRule() { }
934 ~SameValueRule() { }
935 bool Handle(Handler *handler, uint64 address, int reg) const {
936 return handler->SameValueRule(address, reg);
937 }
938 bool operator==(const Rule &rhs) const {
939 // dynamic_cast is allowed by the Google C++ Style Guide, if the use has
940 // been carefully considered; cheap RTTI-like workarounds are forbidden.
941 const SameValueRule *our_rhs = dynamic_cast<const SameValueRule *>(&rhs);
942 return (our_rhs != NULL);
943 }
944 Rule *Copy() const { return new SameValueRule(*this); }
945 };
946
947 // Rule: the register is saved at OFFSET from BASE_REGISTER. BASE_REGISTER
948 // may be CallFrameInfo::Handler::kCFARegister.
949 class CallFrameInfo::OffsetRule: public CallFrameInfo::Rule {
950 public:
951 OffsetRule(int base_register, long offset)
952 : base_register_(base_register), offset_(offset) { }
953 ~OffsetRule() { }
954 bool Handle(Handler *handler, uint64 address, int reg) const {
955 return handler->OffsetRule(address, reg, base_register_, offset_);
956 }
957 bool operator==(const Rule &rhs) const {
958 // dynamic_cast is allowed by the Google C++ Style Guide, if the use has
959 // been carefully considered; cheap RTTI-like workarounds are forbidden.
960 const OffsetRule *our_rhs = dynamic_cast<const OffsetRule *>(&rhs);
961 return (our_rhs &&
962 base_register_ == our_rhs->base_register_ &&
963 offset_ == our_rhs->offset_);
964 }
965 Rule *Copy() const { return new OffsetRule(*this); }
966 // We don't actually need SetBaseRegister or SetOffset here, since they
967 // are only ever applied to CFA rules, for DW_CFA_def_cfa_offset, and it
968 // doesn't make sense to use OffsetRule for computing the CFA: it
969 // computes the address at which a register is saved, not a value.
970 private:
971 int base_register_;
972 int offset_;
973 };
974
975 // Rule: the value the register had in the caller is the value of
976 // BASE_REGISTER plus offset. BASE_REGISTER may be
977 // CallFrameInfo::Handler::kCFARegister.
978 class CallFrameInfo::ValOffsetRule: public CallFrameInfo::Rule {
979 public:
980 ValOffsetRule(int base_register, long offset)
981 : base_register_(base_register), offset_(offset) { }
982 ~ValOffsetRule() { }
983 bool Handle(Handler *handler, uint64 address, int reg) const {
984 return handler->ValOffsetRule(address, reg, base_register_, offset_);
985 }
986 bool operator==(const Rule &rhs) const {
987 // dynamic_cast is allowed by the Google C++ Style Guide, if the use has
988 // been carefully considered; cheap RTTI-like workarounds are forbidden.
989 const ValOffsetRule *our_rhs = dynamic_cast<const ValOffsetRule *>(&rhs);
990 return (our_rhs &&
991 base_register_ == our_rhs->base_register_ &&
992 offset_ == our_rhs->offset_);
993 }
994 Rule *Copy() const { return new ValOffsetRule(*this); }
995 void SetBaseRegister(unsigned reg) { base_register_ = reg; }
996 void SetOffset(long long offset) { offset_ = offset; }
997 private:
998 int base_register_;
999 int offset_;
1000 };
1001
1002 // Rule: the register has been saved in another register REGISTER_NUMBER_.
1003 class CallFrameInfo::RegisterRule: public CallFrameInfo::Rule {
1004 public:
1005 explicit RegisterRule(int register_number)
1006 : register_number_(register_number) { }
1007 ~RegisterRule() { }
1008 bool Handle(Handler *handler, uint64 address, int reg) const {
1009 return handler->RegisterRule(address, reg, register_number_);
1010 }
1011 bool operator==(const Rule &rhs) const {
1012 // dynamic_cast is allowed by the Google C++ Style Guide, if the use has
1013 // been carefully considered; cheap RTTI-like workarounds are forbidden.
1014 const RegisterRule *our_rhs = dynamic_cast<const RegisterRule *>(&rhs);
1015 return (our_rhs && register_number_ == our_rhs->register_number_);
1016 }
1017 Rule *Copy() const { return new RegisterRule(*this); }
1018 private:
1019 int register_number_;
1020 };
1021
1022 // Rule: EXPRESSION evaluates to the address at which the register is saved.
1023 class CallFrameInfo::ExpressionRule: public CallFrameInfo::Rule {
1024 public:
1025 explicit ExpressionRule(const string &expression)
1026 : expression_(expression) { }
1027 ~ExpressionRule() { }
1028 bool Handle(Handler *handler, uint64 address, int reg) const {
1029 return handler->ExpressionRule(address, reg, expression_);
1030 }
1031 bool operator==(const Rule &rhs) const {
1032 // dynamic_cast is allowed by the Google C++ Style Guide, if the use has
1033 // been carefully considered; cheap RTTI-like workarounds are forbidden.
1034 const ExpressionRule *our_rhs = dynamic_cast<const ExpressionRule *>(&rhs);
1035 return (our_rhs && expression_ == our_rhs->expression_);
1036 }
1037 Rule *Copy() const { return new ExpressionRule(*this); }
1038 private:
1039 string expression_;
1040 };
1041
1042 // Rule: EXPRESSION evaluates to the address at which the register is saved.
1043 class CallFrameInfo::ValExpressionRule: public CallFrameInfo::Rule {
1044 public:
1045 explicit ValExpressionRule(const string &expression)
1046 : expression_(expression) { }
1047 ~ValExpressionRule() { }
1048 bool Handle(Handler *handler, uint64 address, int reg) const {
1049 return handler->ValExpressionRule(address, reg, expression_);
1050 }
1051 bool operator==(const Rule &rhs) const {
1052 // dynamic_cast is allowed by the Google C++ Style Guide, if the use has
1053 // been carefully considered; cheap RTTI-like workarounds are forbidden.
1054 const ValExpressionRule *our_rhs =
1055 dynamic_cast<const ValExpressionRule *>(&rhs);
1056 return (our_rhs && expression_ == our_rhs->expression_);
1057 }
1058 Rule *Copy() const { return new ValExpressionRule(*this); }
1059 private:
1060 string expression_;
1061 };
1062
1063 // A map from register numbers to rules.
1064 class CallFrameInfo::RuleMap {
1065 public:
1066 RuleMap() : cfa_rule_(NULL) { }
1067 RuleMap(const RuleMap &rhs) : cfa_rule_(NULL) { *this = rhs; }
1068 ~RuleMap() { Clear(); }
1069
1070 RuleMap &operator=(const RuleMap &rhs);
1071
1072 // Set the rule for computing the CFA to RULE. Take ownership of RULE.
1073 void SetCFARule(Rule *rule) { delete cfa_rule_; cfa_rule_ = rule; }
1074
1075 // Return the current CFA rule. Unlike RegisterRule, this RuleMap retains
1076 // ownership of the rule. We use this for DW_CFA_def_cfa_offset and
1077 // DW_CFA_def_cfa_register, and for detecting references to the CFA before
1078 // a rule for it has been established.
1079 Rule *CFARule() const { return cfa_rule_; }
1080
1081 // Return the rule for REG, or NULL if there is none. The caller takes
1082 // ownership of the result.
1083 Rule *RegisterRule(int reg) const;
1084
1085 // Set the rule for computing REG to RULE. Take ownership of RULE.
1086 void SetRegisterRule(int reg, Rule *rule);
1087
1088 // Make all the appropriate calls to HANDLER as if we were changing from
1089 // this RuleMap to NEW_RULES at ADDRESS. We use this to implement
1090 // DW_CFA_restore_state, where lots of rules can change simultaneously.
1091 // Return true if all handlers returned true; otherwise, return false.
1092 bool HandleTransitionTo(Handler *handler, uint64 address,
1093 const RuleMap &new_rules) const;
1094
1095 private:
1096 // A map from register numbers to Rules.
1097 typedef map<int, Rule *> RuleByNumber;
1098
1099 // Remove all register rules and clear cfa_rule_.
1100 void Clear();
1101
1102 // The rule for computing the canonical frame address. This RuleMap owns
1103 // this rule.
1104 Rule *cfa_rule_;
1105
1106 // A map from register numbers to postfix expressions to recover
1107 // their values. This RuleMap owns the Rules the map refers to.
1108 RuleByNumber registers_;
1109 };
1110
1111 CallFrameInfo::RuleMap &CallFrameInfo::RuleMap::operator=(const RuleMap &rhs) {
1112 Clear();
1113 // Since each map owns the rules it refers to, assignment must copy them.
1114 if (rhs.cfa_rule_) cfa_rule_ = rhs.cfa_rule_->Copy();
1115 for (RuleByNumber::const_iterator it = rhs.registers_.begin();
1116 it != rhs.registers_.end(); it++)
1117 registers_[it->first] = it->second->Copy();
1118 return *this;
1119 }
1120
1121 CallFrameInfo::Rule *CallFrameInfo::RuleMap::RegisterRule(int reg) const {
1122 assert(reg != Handler::kCFARegister);
1123 RuleByNumber::const_iterator it = registers_.find(reg);
1124 if (it != registers_.end())
1125 return it->second->Copy();
1126 else
1127 return NULL;
1128 }
1129
1130 void CallFrameInfo::RuleMap::SetRegisterRule(int reg, Rule *rule) {
1131 assert(reg != Handler::kCFARegister);
1132 assert(rule);
1133 Rule **slot = &registers_[reg];
1134 delete *slot;
1135 *slot = rule;
1136 }
1137
1138 bool CallFrameInfo::RuleMap::HandleTransitionTo(
1139 Handler *handler,
1140 uint64 address,
1141 const RuleMap &new_rules) const {
1142 // Transition from cfa_rule_ to new_rules.cfa_rule_.
1143 if (cfa_rule_ && new_rules.cfa_rule_) {
1144 if (*cfa_rule_ != *new_rules.cfa_rule_ &&
1145 !new_rules.cfa_rule_->Handle(handler, address,
1146 Handler::kCFARegister))
1147 return false;
1148 } else if (cfa_rule_) {
1149 // this RuleMap has a CFA rule but new_rules doesn't.
1150 // CallFrameInfo::Handler has no way to handle this --- and shouldn't;
1151 // it's garbage input. The instruction interpreter should have
1152 // detected this and warned, so take no action here.
1153 } else if (new_rules.cfa_rule_) {
1154 // This shouldn't be possible: NEW_RULES is some prior state, and
1155 // there's no way to remove entries.
1156 assert(0);
1157 } else {
1158 // Both CFA rules are empty. No action needed.
1159 }
1160
1161 // Traverse the two maps in order by register number, and report
1162 // whatever differences we find.
1163 RuleByNumber::const_iterator old_it = registers_.begin();
1164 RuleByNumber::const_iterator new_it = new_rules.registers_.begin();
1165 while (old_it != registers_.end() && new_it != new_rules.registers_.end()) {
1166 if (old_it->first < new_it->first) {
1167 // This RuleMap has an entry for old_it->first, but NEW_RULES
1168 // doesn't.
1169 //
1170 // This isn't really the right thing to do, but since CFI generally
1171 // only mentions callee-saves registers, and GCC's convention for
1172 // callee-saves registers is that they are unchanged, it's a good
1173 // approximation.
1174 if (!handler->SameValueRule(address, old_it->first))
1175 return false;
1176 old_it++;
1177 } else if (old_it->first > new_it->first) {
1178 // NEW_RULES has entry for new_it->first, but this RuleMap
1179 // doesn't. This shouldn't be possible: NEW_RULES is some prior
1180 // state, and there's no way to remove entries.
1181 assert(0);
1182 } else {
1183 // Both maps have an entry for this register. Report the new
1184 // rule if it is different.
1185 if (*old_it->second != *new_it->second &&
1186 !new_it->second->Handle(handler, address, new_it->first))
1187 return false;
1188 new_it++, old_it++;
1189 }
1190 }
1191 // Finish off entries from this RuleMap with no counterparts in new_rules.
1192 while (old_it != registers_.end()) {
1193 if (!handler->SameValueRule(address, old_it->first))
1194 return false;
1195 old_it++;
1196 }
1197 // Since we only make transitions from a rule set to some previously
1198 // saved rule set, and we can only add rules to the map, NEW_RULES
1199 // must have fewer rules than *this.
1200 assert(new_it == new_rules.registers_.end());
1201
1202 return true;
1203 }
1204
1205 // Remove all register rules and clear cfa_rule_.
1206 void CallFrameInfo::RuleMap::Clear() {
1207 delete cfa_rule_;
1208 cfa_rule_ = NULL;
1209 for (RuleByNumber::iterator it = registers_.begin();
1210 it != registers_.end(); it++)
1211 delete it->second;
1212 registers_.clear();
1213 }
1214
1215 // The state of the call frame information interpreter as it processes
1216 // instructions from a CIE and FDE.
1217 class CallFrameInfo::State {
1218 public:
1219 // Create a call frame information interpreter state with the given
1220 // reporter, reader, handler, and initial call frame info address.
1221 State(ByteReader *reader, Handler *handler, Reporter *reporter,
1222 uint64 address)
1223 : reader_(reader), handler_(handler), reporter_(reporter),
1224 address_(address), entry_(NULL), cursor_(NULL) { }
1225
1226 // Interpret instructions from CIE, save the resulting rule set for
1227 // DW_CFA_restore instructions, and return true. On error, report
1228 // the problem to reporter_ and return false.
1229 bool InterpretCIE(const CIE &cie);
1230
1231 // Interpret instructions from FDE, and return true. On error,
1232 // report the problem to reporter_ and return false.
1233 bool InterpretFDE(const FDE &fde);
1234
1235 private:
1236 // The operands of a CFI instruction, for ParseOperands.
1237 struct Operands {
1238 unsigned register_number; // A register number.
1239 uint64 offset; // An offset or address.
1240 long signed_offset; // A signed offset.
1241 string expression; // A DWARF expression.
1242 };
1243
1244 // Parse CFI instruction operands from STATE's instruction stream as
1245 // described by FORMAT. On success, populate OPERANDS with the
1246 // results, and return true. On failure, report the problem and
1247 // return false.
1248 //
1249 // Each character of FORMAT should be one of the following:
1250 //
1251 // 'r' unsigned LEB128 register number (OPERANDS->register_number)
1252 // 'o' unsigned LEB128 offset (OPERANDS->offset)
1253 // 's' signed LEB128 offset (OPERANDS->signed_offset)
1254 // 'a' machine-size address (OPERANDS->offset)
1255 // (If the CIE has a 'z' augmentation string, 'a' uses the
1256 // encoding specified by the 'R' argument.)
1257 // '1' a one-byte offset (OPERANDS->offset)
1258 // '2' a two-byte offset (OPERANDS->offset)
1259 // '4' a four-byte offset (OPERANDS->offset)
1260 // '8' an eight-byte offset (OPERANDS->offset)
1261 // 'e' a DW_FORM_block holding a (OPERANDS->expression)
1262 // DWARF expression
1263 bool ParseOperands(const char *format, Operands *operands);
1264
1265 // Interpret one CFI instruction from STATE's instruction stream, update
1266 // STATE, report any rule changes to handler_, and return true. On
1267 // failure, report the problem and return false.
1268 bool DoInstruction();
1269
1270 // The following Do* member functions are subroutines of DoInstruction,
1271 // factoring out the actual work of operations that have several
1272 // different encodings.
1273
1274 // Set the CFA rule to be the value of BASE_REGISTER plus OFFSET, and
1275 // return true. On failure, report and return false. (Used for
1276 // DW_CFA_def_cfa and DW_CFA_def_cfa_sf.)
1277 bool DoDefCFA(unsigned base_register, long offset);
1278
1279 // Change the offset of the CFA rule to OFFSET, and return true. On
1280 // failure, report and return false. (Subroutine for
1281 // DW_CFA_def_cfa_offset and DW_CFA_def_cfa_offset_sf.)
1282 bool DoDefCFAOffset(long offset);
1283
1284 // Specify that REG can be recovered using RULE, and return true. On
1285 // failure, report and return false.
1286 bool DoRule(unsigned reg, Rule *rule);
1287
1288 // Specify that REG can be found at OFFSET from the CFA, and return true.
1289 // On failure, report and return false. (Subroutine for DW_CFA_offset,
1290 // DW_CFA_offset_extended, and DW_CFA_offset_extended_sf.)
1291 bool DoOffset(unsigned reg, long offset);
1292
1293 // Specify that the caller's value for REG is the CFA plus OFFSET,
1294 // and return true. On failure, report and return false. (Subroutine
1295 // for DW_CFA_val_offset and DW_CFA_val_offset_sf.)
1296 bool DoValOffset(unsigned reg, long offset);
1297
1298 // Restore REG to the rule established in the CIE, and return true. On
1299 // failure, report and return false. (Subroutine for DW_CFA_restore and
1300 // DW_CFA_restore_extended.)
1301 bool DoRestore(unsigned reg);
1302
1303 // Return the section offset of the instruction at cursor. For use
1304 // in error messages.
1305 uint64 CursorOffset() { return entry_->offset + (cursor_ - entry_->start); }
1306
1307 // Report that entry_ is incomplete, and return false. For brevity.
1308 bool ReportIncomplete() {
1309 reporter_->Incomplete(entry_->offset, entry_->kind);
1310 return false;
1311 }
1312
1313 // For reading multi-byte values with the appropriate endianness.
1314 ByteReader *reader_;
1315
1316 // The handler to which we should report the data we find.
1317 Handler *handler_;
1318
1319 // For reporting problems in the info we're parsing.
1320 Reporter *reporter_;
1321
1322 // The code address to which the next instruction in the stream applies.
1323 uint64 address_;
1324
1325 // The entry whose instructions we are currently processing. This is
1326 // first a CIE, and then an FDE.
1327 const Entry *entry_;
1328
1329 // The next instruction to process.
1330 const char *cursor_;
1331
1332 // The current set of rules.
1333 RuleMap rules_;
1334
1335 // The set of rules established by the CIE, used by DW_CFA_restore
1336 // and DW_CFA_restore_extended. We set this after interpreting the
1337 // CIE's instructions.
1338 RuleMap cie_rules_;
1339
1340 // A stack of saved states, for DW_CFA_remember_state and
1341 // DW_CFA_restore_state.
1342 stack<RuleMap> saved_rules_;
1343 };
1344
1345 bool CallFrameInfo::State::InterpretCIE(const CIE &cie) {
1346 entry_ = &cie;
1347 cursor_ = entry_->instructions;
1348 while (cursor_ < entry_->end)
1349 if (!DoInstruction())
1350 return false;
1351 // Note the rules established by the CIE, for use by DW_CFA_restore
1352 // and DW_CFA_restore_extended.
1353 cie_rules_ = rules_;
1354 return true;
1355 }
1356
1357 bool CallFrameInfo::State::InterpretFDE(const FDE &fde) {
1358 entry_ = &fde;
1359 cursor_ = entry_->instructions;
1360 while (cursor_ < entry_->end)
1361 if (!DoInstruction())
1362 return false;
1363 return true;
1364 }
1365
1366 bool CallFrameInfo::State::ParseOperands(const char *format,
1367 Operands *operands) {
1368 size_t len;
1369 const char *operand;
1370
1371 for (operand = format; *operand; operand++) {
1372 size_t bytes_left = entry_->end - cursor_;
1373 switch (*operand) {
1374 case 'r':
1375 operands->register_number = reader_->ReadUnsignedLEB128(cursor_, &len);
1376 if (len > bytes_left) return ReportIncomplete();
1377 cursor_ += len;
1378 break;
1379
1380 case 'o':
1381 operands->offset = reader_->ReadUnsignedLEB128(cursor_, &len);
1382 if (len > bytes_left) return ReportIncomplete();
1383 cursor_ += len;
1384 break;
1385
1386 case 's':
1387 operands->signed_offset = reader_->ReadSignedLEB128(cursor_, &len);
1388 if (len > bytes_left) return ReportIncomplete();
1389 cursor_ += len;
1390 break;
1391
1392 case 'a':
1393 operands->offset =
1394 reader_->ReadEncodedPointer(cursor_, entry_->cie->pointer_encoding,
1395 &len);
1396 if (len > bytes_left) return ReportIncomplete();
1397 cursor_ += len;
1398 break;
1399
1400 case '1':
1401 if (1 > bytes_left) return ReportIncomplete();
1402 operands->offset = static_cast<unsigned char>(*cursor_++);
1403 break;
1404
1405 case '2':
1406 if (2 > bytes_left) return ReportIncomplete();
1407 operands->offset = reader_->ReadTwoBytes(cursor_);
1408 cursor_ += 2;
1409 break;
1410
1411 case '4':
1412 if (4 > bytes_left) return ReportIncomplete();
1413 operands->offset = reader_->ReadFourBytes(cursor_);
1414 cursor_ += 4;
1415 break;
1416
1417 case '8':
1418 if (8 > bytes_left) return ReportIncomplete();
1419 operands->offset = reader_->ReadEightBytes(cursor_);
1420 cursor_ += 8;
1421 break;
1422
1423 case 'e': {
1424 size_t expression_length = reader_->ReadUnsignedLEB128(cursor_, &len);
1425 if (len > bytes_left || expression_length > bytes_left - len)
1426 return ReportIncomplete();
1427 cursor_ += len;
1428 operands->expression = string(cursor_, expression_length);
1429 cursor_ += expression_length;
1430 break;
1431 }
1432
1433 default:
1434 assert(0);
1435 }
1436 }
1437
1438 return true;
1439 }
1440
1441 bool CallFrameInfo::State::DoInstruction() {
1442 CIE *cie = entry_->cie;
1443 Operands ops;
1444
1445 // Our entry's kind should have been set by now.
1446 assert(entry_->kind != kUnknown);
1447
1448 // We shouldn't have been invoked unless there were more
1449 // instructions to parse.
1450 assert(cursor_ < entry_->end);
1451
1452 unsigned opcode = *cursor_++;
1453 if ((opcode & 0xc0) != 0) {
1454 switch (opcode & 0xc0) {
1455 // Advance the address.
1456 case DW_CFA_advance_loc: {
1457 size_t code_offset = opcode & 0x3f;
1458 address_ += code_offset * cie->code_alignment_factor;
1459 break;
1460 }
1461
1462 // Find a register at an offset from the CFA.
1463 case DW_CFA_offset:
1464 if (!ParseOperands("o", &ops) ||
1465 !DoOffset(opcode & 0x3f, ops.offset * cie->data_alignment_factor))
1466 return false;
1467 break;
1468
1469 // Restore the rule established for a register by the CIE.
1470 case DW_CFA_restore:
1471 if (!DoRestore(opcode & 0x3f)) return false;
1472 break;
1473
1474 // The 'if' above should have excluded this possibility.
1475 default:
1476 assert(0);
1477 }
1478
1479 // Return here, so the big switch below won't be indented.
1480 return true;
1481 }
1482
1483 switch (opcode) {
1484 // Set the address.
1485 case DW_CFA_set_loc:
1486 if (!ParseOperands("a", &ops)) return false;
1487 address_ = ops.offset;
1488 break;
1489
1490 // Advance the address.
1491 case DW_CFA_advance_loc1:
1492 if (!ParseOperands("1", &ops)) return false;
1493 address_ += ops.offset * cie->code_alignment_factor;
1494 break;
1495
1496 // Advance the address.
1497 case DW_CFA_advance_loc2:
1498 if (!ParseOperands("2", &ops)) return false;
1499 address_ += ops.offset * cie->code_alignment_factor;
1500 break;
1501
1502 // Advance the address.
1503 case DW_CFA_advance_loc4:
1504 if (!ParseOperands("4", &ops)) return false;
1505 address_ += ops.offset * cie->code_alignment_factor;
1506 break;
1507
1508 // Advance the address.
1509 case DW_CFA_MIPS_advance_loc8:
1510 if (!ParseOperands("8", &ops)) return false;
1511 address_ += ops.offset * cie->code_alignment_factor;
1512 break;
1513
1514 // Compute the CFA by adding an offset to a register.
1515 case DW_CFA_def_cfa:
1516 if (!ParseOperands("ro", &ops) ||
1517 !DoDefCFA(ops.register_number, ops.offset))
1518 return false;
1519 break;
1520
1521 // Compute the CFA by adding an offset to a register.
1522 case DW_CFA_def_cfa_sf:
1523 if (!ParseOperands("rs", &ops) ||
1524 !DoDefCFA(ops.register_number,
1525 ops.signed_offset * cie->data_alignment_factor))
1526 return false;
1527 break;
1528
1529 // Change the base register used to compute the CFA.
1530 case DW_CFA_def_cfa_register: {
1531 Rule *cfa_rule = rules_.CFARule();
1532 if (!cfa_rule) {
1533 reporter_->NoCFARule(entry_->offset, entry_->kind, CursorOffset());
1534 return false;
1535 }
1536 if (!ParseOperands("r", &ops)) return false;
1537 cfa_rule->SetBaseRegister(ops.register_number);
1538 if (!cfa_rule->Handle(handler_, address_,
1539 Handler::kCFARegister))
1540 return false;
1541 break;
1542 }
1543
1544 // Change the offset used to compute the CFA.
1545 case DW_CFA_def_cfa_offset:
1546 if (!ParseOperands("o", &ops) ||
1547 !DoDefCFAOffset(ops.offset))
1548 return false;
1549 break;
1550
1551 // Change the offset used to compute the CFA.
1552 case DW_CFA_def_cfa_offset_sf:
1553 if (!ParseOperands("s", &ops) ||
1554 !DoDefCFAOffset(ops.signed_offset * cie->data_alignment_factor))
1555 return false;
1556 break;
1557
1558 // Specify an expression whose value is the CFA.
1559 case DW_CFA_def_cfa_expression: {
1560 if (!ParseOperands("e", &ops))
1561 return false;
1562 Rule *rule = new ValExpressionRule(ops.expression);
1563 rules_.SetCFARule(rule);
1564 if (!rule->Handle(handler_, address_,
1565 Handler::kCFARegister))
1566 return false;
1567 break;
1568 }
1569
1570 // The register's value cannot be recovered.
1571 case DW_CFA_undefined: {
1572 if (!ParseOperands("r", &ops) ||
1573 !DoRule(ops.register_number, new UndefinedRule()))
1574 return false;
1575 break;
1576 }
1577
1578 // The register's value is unchanged from its value in the caller.
1579 case DW_CFA_same_value: {
1580 if (!ParseOperands("r", &ops) ||
1581 !DoRule(ops.register_number, new SameValueRule()))
1582 return false;
1583 break;
1584 }
1585
1586 // Find a register at an offset from the CFA.
1587 case DW_CFA_offset_extended:
1588 if (!ParseOperands("ro", &ops) ||
1589 !DoOffset(ops.register_number,
1590 ops.offset * cie->data_alignment_factor))
1591 return false;
1592 break;
1593
1594 // The register is saved at an offset from the CFA.
1595 case DW_CFA_offset_extended_sf:
1596 if (!ParseOperands("rs", &ops) ||
1597 !DoOffset(ops.register_number,
1598 ops.signed_offset * cie->data_alignment_factor))
1599 return false;
1600 break;
1601
1602 // The register is saved at an offset from the CFA.
1603 case DW_CFA_GNU_negative_offset_extended:
1604 if (!ParseOperands("ro", &ops) ||
1605 !DoOffset(ops.register_number,
1606 -ops.offset * cie->data_alignment_factor))
1607 return false;
1608 break;
1609
1610 // The register's value is the sum of the CFA plus an offset.
1611 case DW_CFA_val_offset:
1612 if (!ParseOperands("ro", &ops) ||
1613 !DoValOffset(ops.register_number,
1614 ops.offset * cie->data_alignment_factor))
1615 return false;
1616 break;
1617
1618 // The register's value is the sum of the CFA plus an offset.
1619 case DW_CFA_val_offset_sf:
1620 if (!ParseOperands("rs", &ops) ||
1621 !DoValOffset(ops.register_number,
1622 ops.signed_offset * cie->data_alignment_factor))
1623 return false;
1624 break;
1625
1626 // The register has been saved in another register.
1627 case DW_CFA_register: {
1628 if (!ParseOperands("ro", &ops) ||
1629 !DoRule(ops.register_number, new RegisterRule(ops.offset)))
1630 return false;
1631 break;
1632 }
1633
1634 // An expression yields the address at which the register is saved.
1635 case DW_CFA_expression: {
1636 if (!ParseOperands("re", &ops) ||
1637 !DoRule(ops.register_number, new ExpressionRule(ops.expression)))
1638 return false;
1639 break;
1640 }
1641
1642 // An expression yields the caller's value for the register.
1643 case DW_CFA_val_expression: {
1644 if (!ParseOperands("re", &ops) ||
1645 !DoRule(ops.register_number, new ValExpressionRule(ops.expression)))
1646 return false;
1647 break;
1648 }
1649
1650 // Restore the rule established for a register by the CIE.
1651 case DW_CFA_restore_extended:
1652 if (!ParseOperands("r", &ops) ||
1653 !DoRestore( ops.register_number))
1654 return false;
1655 break;
1656
1657 // Save the current set of rules on a stack.
1658 case DW_CFA_remember_state:
1659 saved_rules_.push(rules_);
1660 break;
1661
1662 // Pop the current set of rules off the stack.
1663 case DW_CFA_restore_state: {
1664 if (saved_rules_.empty()) {
1665 reporter_->EmptyStateStack(entry_->offset, entry_->kind,
1666 CursorOffset());
1667 return false;
1668 }
1669 const RuleMap &new_rules = saved_rules_.top();
1670 if (rules_.CFARule() && !new_rules.CFARule()) {
1671 reporter_->ClearingCFARule(entry_->offset, entry_->kind,
1672 CursorOffset());
1673 return false;
1674 }
1675 rules_.HandleTransitionTo(handler_, address_, new_rules);
1676 rules_ = new_rules;
1677 saved_rules_.pop();
1678 break;
1679 }
1680
1681 // No operation. (Padding instruction.)
1682 case DW_CFA_nop:
1683 break;
1684
1685 // A SPARC register window save: Registers 8 through 15 (%o0-%o7)
1686 // are saved in registers 24 through 31 (%i0-%i7), and registers
1687 // 16 through 31 (%l0-%l7 and %i0-%i7) are saved at CFA offsets
1688 // (0-15 * the register size). The register numbers must be
1689 // hard-coded. A GNU extension, and not a pretty one.
1690 case DW_CFA_GNU_window_save: {
1691 // Save %o0-%o7 in %i0-%i7.
1692 for (int i = 8; i < 16; i++)
1693 if (!DoRule(i, new RegisterRule(i + 16)))
1694 return false;
1695 // Save %l0-%l7 and %i0-%i7 at the CFA.
1696 for (int i = 16; i < 32; i++)
1697 // Assume that the byte reader's address size is the same as
1698 // the architecture's register size. !@#%*^ hilarious.
1699 if (!DoRule(i, new OffsetRule(Handler::kCFARegister,
1700 (i - 16) * reader_->AddressSize())))
1701 return false;
1702 break;
1703 }
1704
1705 // I'm not sure what this is. GDB doesn't use it for unwinding.
1706 case DW_CFA_GNU_args_size:
1707 if (!ParseOperands("o", &ops)) return false;
1708 break;
1709
1710 // An opcode we don't recognize.
1711 default: {
1712 reporter_->BadInstruction(entry_->offset, entry_->kind, CursorOffset());
1713 return false;
1714 }
1715 }
1716
1717 return true;
1718 }
1719
1720 bool CallFrameInfo::State::DoDefCFA(unsigned base_register, long offset) {
1721 Rule *rule = new ValOffsetRule(base_register, offset);
1722 rules_.SetCFARule(rule);
1723 return rule->Handle(handler_, address_,
1724 Handler::kCFARegister);
1725 }
1726
1727 bool CallFrameInfo::State::DoDefCFAOffset(long offset) {
1728 Rule *cfa_rule = rules_.CFARule();
1729 if (!cfa_rule) {
1730 reporter_->NoCFARule(entry_->offset, entry_->kind, CursorOffset());
1731 return false;
1732 }
1733 cfa_rule->SetOffset(offset);
1734 return cfa_rule->Handle(handler_, address_,
1735 Handler::kCFARegister);
1736 }
1737
1738 bool CallFrameInfo::State::DoRule(unsigned reg, Rule *rule) {
1739 rules_.SetRegisterRule(reg, rule);
1740 return rule->Handle(handler_, address_, reg);
1741 }
1742
1743 bool CallFrameInfo::State::DoOffset(unsigned reg, long offset) {
1744 if (!rules_.CFARule()) {
1745 reporter_->NoCFARule(entry_->offset, entry_->kind, CursorOffset());
1746 return false;
1747 }
1748 return DoRule(reg,
1749 new OffsetRule(Handler::kCFARegister, offset));
1750 }
1751
1752 bool CallFrameInfo::State::DoValOffset(unsigned reg, long offset) {
1753 if (!rules_.CFARule()) {
1754 reporter_->NoCFARule(entry_->offset, entry_->kind, CursorOffset());
1755 return false;
1756 }
1757 return DoRule(reg,
1758 new ValOffsetRule(Handler::kCFARegister, offset));
1759 }
1760
1761 bool CallFrameInfo::State::DoRestore(unsigned reg) {
1762 // DW_CFA_restore and DW_CFA_restore_extended don't make sense in a CIE.
1763 if (entry_->kind == kCIE) {
1764 reporter_->RestoreInCIE(entry_->offset, CursorOffset());
1765 return false;
1766 }
1767 Rule *rule = cie_rules_.RegisterRule(reg);
1768 if (!rule) {
1769 // This isn't really the right thing to do, but since CFI generally
1770 // only mentions callee-saves registers, and GCC's convention for
1771 // callee-saves registers is that they are unchanged, it's a good
1772 // approximation.
1773 rule = new SameValueRule();
1774 }
1775 return DoRule(reg, rule);
1776 }
1777
1778 bool CallFrameInfo::ReadEntryPrologue(const char *cursor, Entry *entry) {
1779 const char *buffer_end = buffer_ + buffer_length_;
1780
1781 // Initialize enough of ENTRY for use in error reporting.
1782 entry->offset = cursor - buffer_;
1783 entry->start = cursor;
1784 entry->kind = kUnknown;
1785 entry->end = NULL;
1786
1787 // Read the initial length. This sets reader_'s offset size.
1788 size_t length_size;
1789 uint64 length = reader_->ReadInitialLength(cursor, &length_size);
1790 if (length_size > size_t(buffer_end - cursor))
1791 return ReportIncomplete(entry);
1792 cursor += length_size;
1793
1794 // In a .eh_frame section, a length of zero marks the end of the series
1795 // of entries.
1796 if (length == 0 && eh_frame_) {
1797 entry->kind = kTerminator;
1798 entry->end = cursor;
1799 return true;
1800 }
1801
1802 // Validate the length.
1803 if (length > size_t(buffer_end - cursor))
1804 return ReportIncomplete(entry);
1805
1806 // The length is the number of bytes after the initial length field;
1807 // we have that position handy at this point, so compute the end
1808 // now. (If we're parsing 64-bit-offset DWARF on a 32-bit machine,
1809 // and the length didn't fit in a size_t, we would have rejected it
1810 // above.)
1811 entry->end = cursor + length;
1812
1813 // Parse the next field: either the offset of a CIE or a CIE id.
1814 size_t offset_size = reader_->OffsetSize();
1815 if (offset_size > size_t(entry->end - cursor)) return ReportIncomplete(entry);
1816 entry->id = reader_->ReadOffset(cursor);
1817
1818 // Don't advance cursor past id field yet; in .eh_frame data we need
1819 // the id's position to compute the section offset of an FDE's CIE.
1820
1821 // Now we can decide what kind of entry this is.
1822 if (eh_frame_) {
1823 // In .eh_frame data, an ID of zero marks the entry as a CIE, and
1824 // anything else is an offset from the id field of the FDE to the start
1825 // of the CIE.
1826 if (entry->id == 0) {
1827 entry->kind = kCIE;
1828 } else {
1829 entry->kind = kFDE;
1830 // Turn the offset from the id into an offset from the buffer's start.
1831 entry->id = (cursor - buffer_) - entry->id;
1832 }
1833 } else {
1834 // In DWARF CFI data, an ID of ~0 (of the appropriate width, given the
1835 // offset size for the entry) marks the entry as a CIE, and anything
1836 // else is the offset of the CIE from the beginning of the section.
1837 if (offset_size == 4)
1838 entry->kind = (entry->id == 0xffffffff) ? kCIE : kFDE;
1839 else {
1840 assert(offset_size == 8);
1841 entry->kind = (entry->id == 0xffffffffffffffffULL) ? kCIE : kFDE;
1842 }
1843 }
1844
1845 // Now advance cursor past the id.
1846 cursor += offset_size;
1847
1848 // The fields specific to this kind of entry start here.
1849 entry->fields = cursor;
1850
1851 entry->cie = NULL;
1852
1853 return true;
1854 }
1855
1856 bool CallFrameInfo::ReadCIEFields(CIE *cie) {
1857 const char *cursor = cie->fields;
1858 size_t len;
1859
1860 assert(cie->kind == kCIE);
1861
1862 // Prepare for early exit.
1863 cie->version = 0;
1864 cie->augmentation.clear();
1865 cie->code_alignment_factor = 0;
1866 cie->data_alignment_factor = 0;
1867 cie->return_address_register = 0;
1868 cie->has_z_augmentation = false;
1869 cie->pointer_encoding = DW_EH_PE_absptr;
1870 cie->instructions = 0;
1871
1872 // Parse the version number.
1873 if (cie->end - cursor < 1)
1874 return ReportIncomplete(cie);
1875 cie->version = reader_->ReadOneByte(cursor);
1876 cursor++;
1877
1878 // If we don't recognize the version, we can't parse any more fields
1879 // of the CIE. For DWARF CFI, we handle versions 1 through 3 (there
1880 // was never a version 2 of CFI data). For .eh_frame, we handle only
1881 // version 1.
1882 if (eh_frame_) {
1883 if (cie->version != 1) {
1884 reporter_->UnrecognizedVersion(cie->offset, cie->version);
1885 return false;
1886 }
1887 } else {
1888 if (cie->version < 1 || cie->version > 3) {
1889 reporter_->UnrecognizedVersion(cie->offset, cie->version);
1890 return false;
1891 }
1892 }
1893
1894 const char *augmentation_start = cursor;
1895 const void *augmentation_end =
1896 memchr(augmentation_start, '\0', cie->end - augmentation_start);
1897 if (! augmentation_end) return ReportIncomplete(cie);
1898 cursor = static_cast<const char *>(augmentation_end);
1899 cie->augmentation = string(augmentation_start, cursor - augmentation_start);
1900 // Skip the terminating '\0'.
1901 cursor++;
1902
1903 // Is this CFI augmented?
1904 if (!cie->augmentation.empty()) {
1905 // Is it an augmentation we recognize?
1906 if (cie->augmentation[0] == DW_Z_augmentation_start) {
1907 // Linux C++ ABI 'z' augmentation, used for exception handling data.
1908 cie->has_z_augmentation = true;
1909 } else {
1910 // Not an augmentation we recognize. Augmentations can have arbitrary
1911 // effects on the form of rest of the content, so we have to give up.
1912 reporter_->UnrecognizedAugmentation(cie->offset, cie->augmentation);
1913 return false;
1914 }
1915 }
1916
1917 // Parse the code alignment factor.
1918 cie->code_alignment_factor = reader_->ReadUnsignedLEB128(cursor, &len);
1919 if (size_t(cie->end - cursor) < len) return ReportIncomplete(cie);
1920 cursor += len;
1921
1922 // Parse the data alignment factor.
1923 cie->data_alignment_factor = reader_->ReadSignedLEB128(cursor, &len);
1924 if (size_t(cie->end - cursor) < len) return ReportIncomplete(cie);
1925 cursor += len;
1926
1927 // Parse the return address register. This is a ubyte in version 1, and
1928 // a ULEB128 in version 3.
1929 if (cie->version == 1) {
1930 if (cursor >= cie->end) return ReportIncomplete(cie);
1931 cie->return_address_register = uint8(*cursor++);
1932 } else {
1933 cie->return_address_register = reader_->ReadUnsignedLEB128(cursor, &len);
1934 if (size_t(cie->end - cursor) < len) return ReportIncomplete(cie);
1935 cursor += len;
1936 }
1937
1938 // If we have a 'z' augmentation string, find the augmentation data and
1939 // use the augmentation string to parse it.
1940 if (cie->has_z_augmentation) {
1941 size_t data_size = reader_->ReadUnsignedLEB128(cursor, &len);
1942 if (size_t(cie->end - cursor) < len + data_size)
1943 return ReportIncomplete(cie);
1944 cursor += len;
1945 const char *data = cursor;
1946 cursor += data_size;
1947 const char *data_end = cursor;
1948
1949 cie->has_z_lsda = false;
1950 cie->has_z_personality = false;
1951 cie->has_z_signal_frame = false;
1952
1953 // Walk the augmentation string, and extract values from the
1954 // augmentation data as the string directs.
1955 for (size_t i = 1; i < cie->augmentation.size(); i++) {
1956 switch (cie->augmentation[i]) {
1957 case DW_Z_has_LSDA:
1958 // The CIE's augmentation data holds the language-specific data
1959 // area pointer's encoding, and the FDE's augmentation data holds
1960 // the pointer itself.
1961 cie->has_z_lsda = true;
1962 // Fetch the LSDA encoding from the augmentation data.
1963 if (data >= data_end) return ReportIncomplete(cie);
1964 cie->lsda_encoding = DwarfPointerEncoding(*data++);
1965 if (!reader_->ValidEncoding(cie->lsda_encoding)) {
1966 reporter_->InvalidPointerEncoding(cie->offset, cie->lsda_encoding);
1967 return false;
1968 }
1969 // Don't check if the encoding is usable here --- we haven't
1970 // read the FDE's fields yet, so we're not prepared for
1971 // DW_EH_PE_funcrel, although that's a fine encoding for the
1972 // LSDA to use, since it appears in the FDE.
1973 break;
1974
1975 case DW_Z_has_personality_routine:
1976 // The CIE's augmentation data holds the personality routine
1977 // pointer's encoding, followed by the pointer itself.
1978 cie->has_z_personality = true;
1979 // Fetch the personality routine pointer's encoding from the
1980 // augmentation data.
1981 if (data >= data_end) return ReportIncomplete(cie);
1982 cie->personality_encoding = DwarfPointerEncoding(*data++);
1983 if (!reader_->ValidEncoding(cie->personality_encoding)) {
1984 reporter_->InvalidPointerEncoding(cie->offset,
1985 cie->personality_encoding);
1986 return false;
1987 }
1988 if (!reader_->UsableEncoding(cie->personality_encoding)) {
1989 reporter_->UnusablePointerEncoding(cie->offset,
1990 cie->personality_encoding);
1991 return false;
1992 }
1993 // Fetch the personality routine's pointer itself from the data.
1994 cie->personality_address =
1995 reader_->ReadEncodedPointer(data, cie->personality_encoding,
1996 &len);
1997 if (len > size_t(data_end - data))
1998 return ReportIncomplete(cie);
1999 data += len;
2000 break;
2001
2002 case DW_Z_has_FDE_address_encoding:
2003 // The CIE's augmentation data holds the pointer encoding to use
2004 // for addresses in the FDE.
2005 if (data >= data_end) return ReportIncomplete(cie);
2006 cie->pointer_encoding = DwarfPointerEncoding(*data++);
2007 if (!reader_->ValidEncoding(cie->pointer_encoding)) {
2008 reporter_->InvalidPointerEncoding(cie->offset,
2009 cie->pointer_encoding);
2010 return false;
2011 }
2012 if (!reader_->UsableEncoding(cie->pointer_encoding)) {
2013 reporter_->UnusablePointerEncoding(cie->offset,
2014 cie->pointer_encoding);
2015 return false;
2016 }
2017 break;
2018
2019 case DW_Z_is_signal_trampoline:
2020 // Frames using this CIE are signal delivery frames.
2021 cie->has_z_signal_frame = true;
2022 break;
2023
2024 default:
2025 // An augmentation we don't recognize.
2026 reporter_->UnrecognizedAugmentation(cie->offset, cie->augmentation);
2027 return false;
2028 }
2029 }
2030 }
2031
2032 // The CIE's instructions start here.
2033 cie->instructions = cursor;
2034
2035 return true;
2036 }
2037
2038 bool CallFrameInfo::ReadFDEFields(FDE *fde) {
2039 const char *cursor = fde->fields;
2040 size_t size;
2041
2042 fde->address = reader_->ReadEncodedPointer(cursor, fde->cie->pointer_encoding,
2043 &size);
2044 if (size > size_t(fde->end - cursor))
2045 return ReportIncomplete(fde);
2046 cursor += size;
2047 reader_->SetFunctionBase(fde->address);
2048
2049 // For the length, we strip off the upper nybble of the encoding used for
2050 // the starting address.
2051 DwarfPointerEncoding length_encoding =
2052 DwarfPointerEncoding(fde->cie->pointer_encoding & 0x0f);
2053 fde->size = reader_->ReadEncodedPointer(cursor, length_encoding, &size);
2054 if (size > size_t(fde->end - cursor))
2055 return ReportIncomplete(fde);
2056 cursor += size;
2057
2058 // If the CIE has a 'z' augmentation string, then augmentation data
2059 // appears here.
2060 if (fde->cie->has_z_augmentation) {
2061 size_t data_size = reader_->ReadUnsignedLEB128(cursor, &size);
2062 if (size_t(fde->end - cursor) < size + data_size)
2063 return ReportIncomplete(fde);
2064 cursor += size;
2065
2066 // In the abstract, we should walk the augmentation string, and extract
2067 // items from the FDE's augmentation data as we encounter augmentation
2068 // string characters that specify their presence: the ordering of items
2069 // in the augmentation string determines the arrangement of values in
2070 // the augmentation data.
2071 //
2072 // In practice, there's only ever one value in FDE augmentation data
2073 // that we support --- the LSDA pointer --- and we have to bail if we
2074 // see any unrecognized augmentation string characters. So if there is
2075 // anything here at all, we know what it is, and where it starts.
2076 if (fde->cie->has_z_lsda) {
2077 // Check whether the LSDA's pointer encoding is usable now: only once
2078 // we've parsed the FDE's starting address do we call reader_->
2079 // SetFunctionBase, so that the DW_EH_PE_funcrel encoding becomes
2080 // usable.
2081 if (!reader_->UsableEncoding(fde->cie->lsda_encoding)) {
2082 reporter_->UnusablePointerEncoding(fde->cie->offset,
2083 fde->cie->lsda_encoding);
2084 return false;
2085 }
2086
2087 fde->lsda_address =
2088 reader_->ReadEncodedPointer(cursor, fde->cie->lsda_encoding, &size);
2089 if (size > data_size)
2090 return ReportIncomplete(fde);
2091 // Ideally, we would also complain here if there were unconsumed
2092 // augmentation data.
2093 }
2094
2095 cursor += data_size;
2096 }
2097
2098 // The FDE's instructions start after those.
2099 fde->instructions = cursor;
2100
2101 return true;
2102 }
2103
2104 bool CallFrameInfo::Start() {
2105 const char *buffer_end = buffer_ + buffer_length_;
2106 const char *cursor;
2107 bool all_ok = true;
2108 const char *entry_end;
2109 bool ok;
2110
2111 // Traverse all the entries in buffer_, skipping CIEs and offering
2112 // FDEs to the handler.
2113 for (cursor = buffer_; cursor < buffer_end;
2114 cursor = entry_end, all_ok = all_ok && ok) {
2115 FDE fde;
2116
2117 // Make it easy to skip this entry with 'continue': assume that
2118 // things are not okay until we've checked all the data, and
2119 // prepare the address of the next entry.
2120 ok = false;
2121
2122 // Read the entry's prologue.
2123 if (!ReadEntryPrologue(cursor, &fde)) {
2124 if (!fde.end) {
2125 // If we couldn't even figure out this entry's extent, then we
2126 // must stop processing entries altogether.
2127 all_ok = false;
2128 break;
2129 }
2130 entry_end = fde.end;
2131 continue;
2132 }
2133
2134 // The next iteration picks up after this entry.
2135 entry_end = fde.end;
2136
2137 // Did we see an .eh_frame terminating mark?
2138 if (fde.kind == kTerminator) {
2139 // If there appears to be more data left in the section after the
2140 // terminating mark, warn the user. But this is just a warning;
2141 // we leave all_ok true.
2142 if (fde.end < buffer_end) reporter_->EarlyEHTerminator(fde.offset);
2143 break;
2144 }
2145
2146 // In this loop, we skip CIEs. We only parse them fully when we
2147 // parse an FDE that refers to them. This limits our memory
2148 // consumption (beyond the buffer itself) to that needed to
2149 // process the largest single entry.
2150 if (fde.kind != kFDE) {
2151 ok = true;
2152 continue;
2153 }
2154
2155 // Validate the CIE pointer.
2156 if (fde.id > buffer_length_) {
2157 reporter_->CIEPointerOutOfRange(fde.offset, fde.id);
2158 continue;
2159 }
2160
2161 CIE cie;
2162
2163 // Parse this FDE's CIE header.
2164 if (!ReadEntryPrologue(buffer_ + fde.id, &cie))
2165 continue;
2166 // This had better be an actual CIE.
2167 if (cie.kind != kCIE) {
2168 reporter_->BadCIEId(fde.offset, fde.id);
2169 continue;
2170 }
2171 if (!ReadCIEFields(&cie))
2172 continue;
2173
2174 // We now have the values that govern both the CIE and the FDE.
2175 cie.cie = &cie;
2176 fde.cie = &cie;
2177
2178 // Parse the FDE's header.
2179 if (!ReadFDEFields(&fde))
2180 continue;
2181
2182 // Call Entry to ask the consumer if they're interested.
2183 if (!handler_->Entry(fde.offset, fde.address, fde.size,
2184 cie.version, cie.augmentation,
2185 cie.return_address_register)) {
2186 // The handler isn't interested in this entry. That's not an error.
2187 ok = true;
2188 continue;
2189 }
2190
2191 if (cie.has_z_augmentation) {
2192 // Report the personality routine address, if we have one.
2193 if (cie.has_z_personality) {
2194 if (!handler_
2195 ->PersonalityRoutine(cie.personality_address,
2196 IsIndirectEncoding(cie.personality_encoding)))
2197 continue;
2198 }
2199
2200 // Report the language-specific data area address, if we have one.
2201 if (cie.has_z_lsda) {
2202 if (!handler_
2203 ->LanguageSpecificDataArea(fde.lsda_address,
2204 IsIndirectEncoding(cie.lsda_encoding)))
2205 continue;
2206 }
2207
2208 // If this is a signal-handling frame, report that.
2209 if (cie.has_z_signal_frame) {
2210 if (!handler_->SignalHandler())
2211 continue;
2212 }
2213 }
2214
2215 // Interpret the CIE's instructions, and then the FDE's instructions.
2216 State state(reader_, handler_, reporter_, fde.address);
2217 ok = state.InterpretCIE(cie) && state.InterpretFDE(fde);
2218
2219 // Tell the ByteReader that the function start address from the
2220 // FDE header is no longer valid.
2221 reader_->ClearFunctionBase();
2222
2223 // Report the end of the entry.
2224 handler_->End();
2225 }
2226
2227 return all_ok;
2228 }
2229
2230 const char *CallFrameInfo::KindName(EntryKind kind) {
2231 if (kind == CallFrameInfo::kUnknown)
2232 return "entry";
2233 else if (kind == CallFrameInfo::kCIE)
2234 return "common information entry";
2235 else if (kind == CallFrameInfo::kFDE)
2236 return "frame description entry";
2237 else {
2238 assert (kind == CallFrameInfo::kTerminator);
2239 return ".eh_frame sequence terminator";
2240 }
2241 }
2242
2243 bool CallFrameInfo::ReportIncomplete(Entry *entry) {
2244 reporter_->Incomplete(entry->offset, entry->kind);
2245 return false;
2246 }
2247
2248 void CallFrameInfo::Reporter::Incomplete(uint64 offset,
2249 CallFrameInfo::EntryKind kind) {
2250 fprintf(stderr,
2251 "%s: CFI %s at offset 0x%llx in '%s': entry ends early\n",
2252 filename_.c_str(), CallFrameInfo::KindName(kind), offset,
2253 section_.c_str());
2254 }
2255
2256 void CallFrameInfo::Reporter::EarlyEHTerminator(uint64 offset) {
2257 fprintf(stderr,
2258 "%s: CFI at offset 0x%llx in '%s': saw end-of-data marker"
2259 " before end of section contents\n",
2260 filename_.c_str(), offset, section_.c_str());
2261 }
2262
2263 void CallFrameInfo::Reporter::CIEPointerOutOfRange(uint64 offset,
2264 uint64 cie_offset) {
2265 fprintf(stderr,
2266 "%s: CFI frame description entry at offset 0x%llx in '%s':"
2267 " CIE pointer is out of range: 0x%llx\n",
2268 filename_.c_str(), offset, section_.c_str(), cie_offset);
2269 }
2270
2271 void CallFrameInfo::Reporter::BadCIEId(uint64 offset, uint64 cie_offset) {
2272 fprintf(stderr,
2273 "%s: CFI frame description entry at offset 0x%llx in '%s':"
2274 " CIE pointer does not point to a CIE: 0x%llx\n",
2275 filename_.c_str(), offset, section_.c_str(), cie_offset);
2276 }
2277
2278 void CallFrameInfo::Reporter::UnrecognizedVersion(uint64 offset, int version) {
2279 fprintf(stderr,
2280 "%s: CFI frame description entry at offset 0x%llx in '%s':"
2281 " CIE specifies unrecognized version: %d\n",
2282 filename_.c_str(), offset, section_.c_str(), version);
2283 }
2284
2285 void CallFrameInfo::Reporter::UnrecognizedAugmentation(uint64 offset,
2286 const string &aug) {
2287 fprintf(stderr,
2288 "%s: CFI frame description entry at offset 0x%llx in '%s':"
2289 " CIE specifies unrecognized augmentation: '%s'\n",
2290 filename_.c_str(), offset, section_.c_str(), aug.c_str());
2291 }
2292
2293 void CallFrameInfo::Reporter::InvalidPointerEncoding(uint64 offset,
2294 uint8 encoding) {
2295 fprintf(stderr,
2296 "%s: CFI common information entry at offset 0x%llx in '%s':"
2297 " 'z' augmentation specifies invalid pointer encoding: 0x%02x\n",
2298 filename_.c_str(), offset, section_.c_str(), encoding);
2299 }
2300
2301 void CallFrameInfo::Reporter::UnusablePointerEncoding(uint64 offset,
2302 uint8 encoding) {
2303 fprintf(stderr,
2304 "%s: CFI common information entry at offset 0x%llx in '%s':"
2305 " 'z' augmentation specifies a pointer encoding for which"
2306 " we have no base address: 0x%02x\n",
2307 filename_.c_str(), offset, section_.c_str(), encoding);
2308 }
2309
2310 void CallFrameInfo::Reporter::RestoreInCIE(uint64 offset, uint64 insn_offset) {
2311 fprintf(stderr,
2312 "%s: CFI common information entry at offset 0x%llx in '%s':"
2313 " the DW_CFA_restore instruction at offset 0x%llx"
2314 " cannot be used in a common information entry\n",
2315 filename_.c_str(), offset, section_.c_str(), insn_offset);
2316 }
2317
2318 void CallFrameInfo::Reporter::BadInstruction(uint64 offset,
2319 CallFrameInfo::EntryKind kind,
2320 uint64 insn_offset) {
2321 fprintf(stderr,
2322 "%s: CFI %s at offset 0x%llx in section '%s':"
2323 " the instruction at offset 0x%llx is unrecognized\n",
2324 filename_.c_str(), CallFrameInfo::KindName(kind),
2325 offset, section_.c_str(), insn_offset);
2326 }
2327
2328 void CallFrameInfo::Reporter::NoCFARule(uint64 offset,
2329 CallFrameInfo::EntryKind kind,
2330 uint64 insn_offset) {
2331 fprintf(stderr,
2332 "%s: CFI %s at offset 0x%llx in section '%s':"
2333 " the instruction at offset 0x%llx assumes that a CFA rule has"
2334 " been set, but none has been set\n",
2335 filename_.c_str(), CallFrameInfo::KindName(kind), offset,
2336 section_.c_str(), insn_offset);
2337 }
2338
2339 void CallFrameInfo::Reporter::EmptyStateStack(uint64 offset,
2340 CallFrameInfo::EntryKind kind,
2341 uint64 insn_offset) {
2342 fprintf(stderr,
2343 "%s: CFI %s at offset 0x%llx in section '%s':"
2344 " the DW_CFA_restore_state instruction at offset 0x%llx"
2345 " should pop a saved state from the stack, but the stack is empty\n",
2346 filename_.c_str(), CallFrameInfo::KindName(kind), offset,
2347 section_.c_str(), insn_offset);
2348 }
2349
2350 void CallFrameInfo::Reporter::ClearingCFARule(uint64 offset,
2351 CallFrameInfo::EntryKind kind,
2352 uint64 insn_offset) {
2353 fprintf(stderr,
2354 "%s: CFI %s at offset 0x%llx in section '%s':"
2355 " the DW_CFA_restore_state instruction at offset 0x%llx"
2356 " would clear the CFA rule in effect\n",
2357 filename_.c_str(), CallFrameInfo::KindName(kind), offset,
2358 section_.c_str(), insn_offset);
2359 }
2360
2361 } // namespace dwarf2reader
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698