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

Side by Side Diff: obsolete/breakpad/common/dwarf/dwarf2reader.h

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 // -*- mode: C++ -*-
2
3 // Copyright (c) 2010 Google Inc. All Rights Reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 // CFI reader author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com>
32
33 // This file contains definitions related to the DWARF2/3 reader and
34 // it's handler interfaces.
35 // The DWARF2/3 specification can be found at
36 // http://dwarf.freestandards.org and should be considered required
37 // reading if you wish to modify the implementation.
38 // Only a cursory attempt is made to explain terminology that is
39 // used here, as it is much better explained in the standard documents
40 #ifndef COMMON_DWARF_DWARF2READER_H__
41 #define COMMON_DWARF_DWARF2READER_H__
42
43 #include <list>
44 #include <map>
45 #include <string>
46 #include <utility>
47 #include <vector>
48
49 #ifdef WIN32
50 #undef min
51 #undef max
52 #pragma warning(disable:4800 4244)
53 #endif
54
55 #include "common/dwarf/bytereader.h"
56 #include "common/dwarf/dwarf2enums.h"
57 #include "common/dwarf/types.h"
58
59 using namespace std;
60
61 namespace dwarf2reader {
62 struct LineStateMachine;
63 class Dwarf2Handler;
64 class LineInfoHandler;
65
66 // This maps from a string naming a section to a pair containing a
67 // the data for the section, and the size of the section.
68 typedef map<string, pair<const char*, uint64> > SectionMap;
69 typedef list<pair<enum DwarfAttribute, enum DwarfForm> > AttributeList;
70 typedef AttributeList::iterator AttributeIterator;
71 typedef AttributeList::const_iterator ConstAttributeIterator;
72
73 struct LineInfoHeader {
74 uint64 total_length;
75 uint16 version;
76 uint64 prologue_length;
77 uint8 min_insn_length; // insn stands for instructin
78 bool default_is_stmt; // stmt stands for statement
79 int8 line_base;
80 uint8 line_range;
81 uint8 opcode_base;
82 // Use a pointer so that signalsafe_addr2line is able to use this structure
83 // without heap allocation problem.
84 vector<unsigned char> *std_opcode_lengths;
85 };
86
87 class LineInfo {
88 public:
89
90 // Initializes a .debug_line reader. Buffer and buffer length point
91 // to the beginning and length of the line information to read.
92 // Reader is a ByteReader class that has the endianness set
93 // properly.
94 LineInfo(const char* buffer_, uint64 buffer_length,
95 ByteReader* reader, LineInfoHandler* handler);
96
97 virtual ~LineInfo() {
98 if (header_.std_opcode_lengths) {
99 delete header_.std_opcode_lengths;
100 }
101 }
102
103 // Start processing line info, and calling callbacks in the handler.
104 // Consumes the line number information for a single compilation unit.
105 // Returns the number of bytes processed.
106 uint64 Start();
107
108 // Process a single line info opcode at START using the state
109 // machine at LSM. Return true if we should define a line using the
110 // current state of the line state machine. Place the length of the
111 // opcode in LEN.
112 // If LSM_PASSES_PC is non-NULL, this function also checks if the lsm
113 // passes the address of PC. In other words, LSM_PASSES_PC will be
114 // set to true, if the following condition is met.
115 //
116 // lsm's old address < PC <= lsm's new address
117 static bool ProcessOneOpcode(ByteReader* reader,
118 LineInfoHandler* handler,
119 const struct LineInfoHeader &header,
120 const char* start,
121 struct LineStateMachine* lsm,
122 size_t* len,
123 uintptr pc,
124 bool *lsm_passes_pc);
125
126 private:
127 // Reads the DWARF2/3 header for this line info.
128 void ReadHeader();
129
130 // Reads the DWARF2/3 line information
131 void ReadLines();
132
133 // The associated handler to call processing functions in
134 LineInfoHandler* handler_;
135
136 // The associated ByteReader that handles endianness issues for us
137 ByteReader* reader_;
138
139 // A DWARF2/3 line info header. This is not the same size as
140 // in the actual file, as the one in the file may have a 32 bit or
141 // 64 bit lengths
142
143 struct LineInfoHeader header_;
144
145 // buffer is the buffer for our line info, starting at exactly where
146 // the line info to read is. after_header is the place right after
147 // the end of the line information header.
148 const char* buffer_;
149 uint64 buffer_length_;
150 const char* after_header_;
151 };
152
153 // This class is the main interface between the line info reader and
154 // the client. The virtual functions inside this get called for
155 // interesting events that happen during line info reading. The
156 // default implementation does nothing
157
158 class LineInfoHandler {
159 public:
160 LineInfoHandler() { }
161
162 virtual ~LineInfoHandler() { }
163
164 // Called when we define a directory. NAME is the directory name,
165 // DIR_NUM is the directory number
166 virtual void DefineDir(const string& name, uint32 dir_num) { }
167
168 // Called when we define a filename. NAME is the filename, FILE_NUM
169 // is the file number which is -1 if the file index is the next
170 // index after the last numbered index (this happens when files are
171 // dynamically defined by the line program), DIR_NUM is the
172 // directory index for the directory name of this file, MOD_TIME is
173 // the modification time of the file, and LENGTH is the length of
174 // the file
175 virtual void DefineFile(const string& name, int32 file_num,
176 uint32 dir_num, uint64 mod_time,
177 uint64 length) { }
178
179 // Called when the line info reader has a new line, address pair
180 // ready for us. ADDRESS is the address of the code, LENGTH is the
181 // length of its machine code in bytes, FILE_NUM is the file number
182 // containing the code, LINE_NUM is the line number in that file for
183 // the code, and COLUMN_NUM is the column number the code starts at,
184 // if we know it (0 otherwise).
185 virtual void AddLine(uint64 address, uint64 length,
186 uint32 file_num, uint32 line_num, uint32 column_num) { }
187 };
188
189 // The base of DWARF2/3 debug info is a DIE (Debugging Information
190 // Entry.
191 // DWARF groups DIE's into a tree and calls the root of this tree a
192 // "compilation unit". Most of the time, there is one compilation
193 // unit in the .debug_info section for each file that had debug info
194 // generated.
195 // Each DIE consists of
196
197 // 1. a tag specifying a thing that is being described (ie
198 // DW_TAG_subprogram for functions, DW_TAG_variable for variables, etc
199 // 2. attributes (such as DW_AT_location for location in memory,
200 // DW_AT_name for name), and data for each attribute.
201 // 3. A flag saying whether the DIE has children or not
202
203 // In order to gain some amount of compression, the format of
204 // each DIE (tag name, attributes and data forms for the attributes)
205 // are stored in a separate table called the "abbreviation table".
206 // This is done because a large number of DIEs have the exact same tag
207 // and list of attributes, but different data for those attributes.
208 // As a result, the .debug_info section is just a stream of data, and
209 // requires reading of the .debug_abbrev section to say what the data
210 // means.
211
212 // As a warning to the user, it should be noted that the reason for
213 // using absolute offsets from the beginning of .debug_info is that
214 // DWARF2/3 supports referencing DIE's from other DIE's by their offset
215 // from either the current compilation unit start, *or* the beginning
216 // of the .debug_info section. This means it is possible to reference
217 // a DIE in one compilation unit from a DIE in another compilation
218 // unit. This style of reference is usually used to eliminate
219 // duplicated information that occurs across compilation
220 // units, such as base types, etc. GCC 3.4+ support this with
221 // -feliminate-dwarf2-dups. Other toolchains will sometimes do
222 // duplicate elimination in the linker.
223
224 class CompilationUnit {
225 public:
226
227 // Initialize a compilation unit. This requires a map of sections,
228 // the offset of this compilation unit in the .debug_info section, a
229 // ByteReader, and a Dwarf2Handler class to call callbacks in.
230 CompilationUnit(const SectionMap& sections, uint64 offset,
231 ByteReader* reader, Dwarf2Handler* handler);
232 virtual ~CompilationUnit() {
233 if (abbrevs_) delete abbrevs_;
234 }
235
236 // Begin reading a Dwarf2 compilation unit, and calling the
237 // callbacks in the Dwarf2Handler
238
239 // Return the full length of the compilation unit, including
240 // headers. This plus the starting offset passed to the constructor
241 // is the offset of the end of the compilation unit --- and the
242 // start of the next compilation unit, if there is one.
243 uint64 Start();
244
245 private:
246
247 // This struct represents a single DWARF2/3 abbreviation
248 // The abbreviation tells how to read a DWARF2/3 DIE, and consist of a
249 // tag and a list of attributes, as well as the data form of each attribute.
250 struct Abbrev {
251 uint32 number;
252 enum DwarfTag tag;
253 bool has_children;
254 AttributeList attributes;
255 };
256
257 // A DWARF2/3 compilation unit header. This is not the same size as
258 // in the actual file, as the one in the file may have a 32 bit or
259 // 64 bit length.
260 struct CompilationUnitHeader {
261 uint64 length;
262 uint16 version;
263 uint64 abbrev_offset;
264 uint8 address_size;
265 } header_;
266
267 // Reads the DWARF2/3 header for this compilation unit.
268 void ReadHeader();
269
270 // Reads the DWARF2/3 abbreviations for this compilation unit
271 void ReadAbbrevs();
272
273 // Processes a single DIE for this compilation unit and return a new
274 // pointer just past the end of it
275 const char* ProcessDIE(uint64 dieoffset,
276 const char* start,
277 const Abbrev& abbrev);
278
279 // Processes a single attribute and return a new pointer just past the
280 // end of it
281 const char* ProcessAttribute(uint64 dieoffset,
282 const char* start,
283 enum DwarfAttribute attr,
284 enum DwarfForm form);
285
286 // Processes all DIEs for this compilation unit
287 void ProcessDIEs();
288
289 // Skips the die with attributes specified in ABBREV starting at
290 // START, and return the new place to position the stream to.
291 const char* SkipDIE(const char* start,
292 const Abbrev& abbrev);
293
294 // Skips the attribute starting at START, with FORM, and return the
295 // new place to position the stream to.
296 const char* SkipAttribute(const char* start,
297 enum DwarfForm form);
298
299 // Offset from section start is the offset of this compilation unit
300 // from the beginning of the .debug_info section.
301 uint64 offset_from_section_start_;
302
303 // buffer is the buffer for our CU, starting at .debug_info + offset
304 // passed in from constructor.
305 // after_header points to right after the compilation unit header.
306 const char* buffer_;
307 uint64 buffer_length_;
308 const char* after_header_;
309
310 // The associated ByteReader that handles endianness issues for us
311 ByteReader* reader_;
312
313 // The map of sections in our file to buffers containing their data
314 const SectionMap& sections_;
315
316 // The associated handler to call processing functions in
317 Dwarf2Handler* handler_;
318
319 // Set of DWARF2/3 abbreviations for this compilation unit. Indexed
320 // by abbreviation number, which means that abbrevs_[0] is not
321 // valid.
322 vector<Abbrev>* abbrevs_;
323
324 // String section buffer and length, if we have a string section.
325 // This is here to avoid doing a section lookup for strings in
326 // ProcessAttribute, which is in the hot path for DWARF2 reading.
327 const char* string_buffer_;
328 uint64 string_buffer_length_;
329 };
330
331 // This class is the main interface between the reader and the
332 // client. The virtual functions inside this get called for
333 // interesting events that happen during DWARF2 reading.
334 // The default implementation skips everything.
335
336 class Dwarf2Handler {
337 public:
338 Dwarf2Handler() { }
339
340 virtual ~Dwarf2Handler() { }
341
342 // Start to process a compilation unit at OFFSET from the beginning of the
343 // .debug_info section. Return false if you would like to skip this
344 // compilation unit.
345 virtual bool StartCompilationUnit(uint64 offset, uint8 address_size,
346 uint8 offset_size, uint64 cu_length,
347 uint8 dwarf_version) { return false; }
348
349 // Start to process a DIE at OFFSET from the beginning of the .debug_info
350 // section. Return false if you would like to skip this DIE.
351 virtual bool StartDIE(uint64 offset, enum DwarfTag tag,
352 const AttributeList& attrs) { return false; }
353
354 // Called when we have an attribute with unsigned data to give to our
355 // handler. The attribute is for the DIE at OFFSET from the beginning of the
356 // .debug_info section. Its name is ATTR, its form is FORM, and its value is
357 // DATA.
358 virtual void ProcessAttributeUnsigned(uint64 offset,
359 enum DwarfAttribute attr,
360 enum DwarfForm form,
361 uint64 data) { }
362
363 // Called when we have an attribute with signed data to give to our handler.
364 // The attribute is for the DIE at OFFSET from the beginning of the
365 // .debug_info section. Its name is ATTR, its form is FORM, and its value is
366 // DATA.
367 virtual void ProcessAttributeSigned(uint64 offset,
368 enum DwarfAttribute attr,
369 enum DwarfForm form,
370 int64 data) { }
371
372 // Called when we have an attribute whose value is a reference to
373 // another DIE. The attribute belongs to the DIE at OFFSET from the
374 // beginning of the .debug_info section. Its name is ATTR, its form
375 // is FORM, and the offset of the DIE being referred to from the
376 // beginning of the .debug_info section is DATA.
377 virtual void ProcessAttributeReference(uint64 offset,
378 enum DwarfAttribute attr,
379 enum DwarfForm form,
380 uint64 data) { }
381
382 // Called when we have an attribute with a buffer of data to give to our
383 // handler. The attribute is for the DIE at OFFSET from the beginning of the
384 // .debug_info section. Its name is ATTR, its form is FORM, DATA points to
385 // the buffer's contents, and its length in bytes is LENGTH. The buffer is
386 // owned by the caller, not the callee, and may not persist for very long.
387 // If you want the data to be available later, it needs to be copied.
388 virtual void ProcessAttributeBuffer(uint64 offset,
389 enum DwarfAttribute attr,
390 enum DwarfForm form,
391 const char* data,
392 uint64 len) { }
393
394 // Called when we have an attribute with string data to give to our handler.
395 // The attribute is for the DIE at OFFSET from the beginning of the
396 // .debug_info section. Its name is ATTR, its form is FORM, and its value is
397 // DATA.
398 virtual void ProcessAttributeString(uint64 offset,
399 enum DwarfAttribute attr,
400 enum DwarfForm form,
401 const string& data) { }
402
403 // Called when finished processing the DIE at OFFSET.
404 // Because DWARF2/3 specifies a tree of DIEs, you may get starts
405 // before ends of the previous DIE, as we process children before
406 // ending the parent.
407 virtual void EndDIE(uint64 offset) { }
408
409 };
410
411 // This class is a reader for DWARF's Call Frame Information. CFI
412 // describes how to unwind stack frames --- even for functions that do
413 // not follow fixed conventions for saving registers, whose frame size
414 // varies as they execute, etc.
415 //
416 // CFI describes, at each machine instruction, how to compute the
417 // stack frame's base address, how to find the return address, and
418 // where to find the saved values of the caller's registers (if the
419 // callee has stashed them somewhere to free up the registers for its
420 // own use).
421 //
422 // For example, suppose we have a function whose machine code looks
423 // like this (imagine an assembly language that looks like C, for a
424 // machine with 32-bit registers, and a stack that grows towards lower
425 // addresses):
426 //
427 // func: ; entry point; return address at sp
428 // func+0: sp = sp - 16 ; allocate space for stack frame
429 // func+1: sp[12] = r0 ; save r0 at sp+12
430 // ... ; other code, not frame-related
431 // func+10: sp -= 4; *sp = x ; push some x on the stack
432 // ... ; other code, not frame-related
433 // func+20: r0 = sp[16] ; restore saved r0
434 // func+21: sp += 20 ; pop whole stack frame
435 // func+22: pc = *sp; sp += 4 ; pop return address and jump to it
436 //
437 // DWARF CFI is (a very compressed representation of) a table with a
438 // row for each machine instruction address and a column for each
439 // register showing how to restore it, if possible.
440 //
441 // A special column named "CFA", for "Canonical Frame Address", tells how
442 // to compute the base address of the frame; registers' entries may
443 // refer to the CFA in describing where the registers are saved.
444 //
445 // Another special column, named "RA", represents the return address.
446 //
447 // For example, here is a complete (uncompressed) table describing the
448 // function above:
449 //
450 // insn cfa r0 r1 ... ra
451 // =======================================
452 // func+0: sp cfa[0]
453 // func+1: sp+16 cfa[0]
454 // func+2: sp+16 cfa[-4] cfa[0]
455 // func+11: sp+20 cfa[-4] cfa[0]
456 // func+21: sp+20 cfa[0]
457 // func+22: sp cfa[0]
458 //
459 // Some things to note here:
460 //
461 // - Each row describes the state of affairs *before* executing the
462 // instruction at the given address. Thus, the row for func+0
463 // describes the state before we allocate the stack frame. In the
464 // next row, the formula for computing the CFA has changed,
465 // reflecting that allocation.
466 //
467 // - The other entries are written in terms of the CFA; this allows
468 // them to remain unchanged as the stack pointer gets bumped around.
469 // For example, the rule for recovering the return address (the "ra"
470 // column) remains unchanged throughout the function, even as the
471 // stack pointer takes on three different offsets from the return
472 // address.
473 //
474 // - Although we haven't shown it, most calling conventions designate
475 // "callee-saves" and "caller-saves" registers. The callee must
476 // preserve the values of callee-saves registers; if it uses them,
477 // it must save their original values somewhere, and restore them
478 // before it returns. In contrast, the callee is free to trash
479 // caller-saves registers; if the callee uses these, it will
480 // probably not bother to save them anywhere, and the CFI will
481 // probably mark their values as "unrecoverable".
482 //
483 // (However, since the caller cannot assume the callee was going to
484 // save them, caller-saves registers are probably dead in the caller
485 // anyway, so compilers usually don't generate CFA for caller-saves
486 // registers.)
487 //
488 // - Exactly where the CFA points is a matter of convention that
489 // depends on the architecture and ABI in use. In the example, the
490 // CFA is the value the stack pointer had upon entry to the
491 // function, pointing at the saved return address. But on the x86,
492 // the call frame information generated by GCC follows the
493 // convention that the CFA is the address *after* the saved return
494 // address.
495 //
496 // But by definition, the CFA remains constant throughout the
497 // lifetime of the frame. This makes it a useful value for other
498 // columns to refer to. It is also gives debuggers a useful handle
499 // for identifying a frame.
500 //
501 // If you look at the table above, you'll notice that a given entry is
502 // often the same as the one immediately above it: most instructions
503 // change only one or two aspects of the stack frame, if they affect
504 // it at all. The DWARF format takes advantage of this fact, and
505 // reduces the size of the data by mentioning only the addresses and
506 // columns at which changes take place. So for the above, DWARF CFI
507 // data would only actually mention the following:
508 //
509 // insn cfa r0 r1 ... ra
510 // =======================================
511 // func+0: sp cfa[0]
512 // func+1: sp+16
513 // func+2: cfa[-4]
514 // func+11: sp+20
515 // func+21: r0
516 // func+22: sp
517 //
518 // In fact, this is the way the parser reports CFI to the consumer: as
519 // a series of statements of the form, "At address X, column Y changed
520 // to Z," and related conventions for describing the initial state.
521 //
522 // Naturally, it would be impractical to have to scan the entire
523 // program's CFI, noting changes as we go, just to recover the
524 // unwinding rules in effect at one particular instruction. To avoid
525 // this, CFI data is grouped into "entries", each of which covers a
526 // specified range of addresses and begins with a complete statement
527 // of the rules for all recoverable registers at that starting
528 // address. Each entry typically covers a single function.
529 //
530 // Thus, to compute the contents of a given row of the table --- that
531 // is, rules for recovering the CFA, RA, and registers at a given
532 // instruction --- the consumer should find the entry that covers that
533 // instruction's address, start with the initial state supplied at the
534 // beginning of the entry, and work forward until it has processed all
535 // the changes up to and including those for the present instruction.
536 //
537 // There are seven kinds of rules that can appear in an entry of the
538 // table:
539 //
540 // - "undefined": The given register is not preserved by the callee;
541 // its value cannot be recovered.
542 //
543 // - "same value": This register has the same value it did in the callee.
544 //
545 // - offset(N): The register is saved at offset N from the CFA.
546 //
547 // - val_offset(N): The value the register had in the caller is the
548 // CFA plus offset N. (This is usually only useful for describing
549 // the stack pointer.)
550 //
551 // - register(R): The register's value was saved in another register R.
552 //
553 // - expression(E): Evaluating the DWARF expression E using the
554 // current frame's registers' values yields the address at which the
555 // register was saved.
556 //
557 // - val_expression(E): Evaluating the DWARF expression E using the
558 // current frame's registers' values yields the value the register
559 // had in the caller.
560
561 class CallFrameInfo {
562 public:
563 // The different kinds of entries one finds in CFI. Used internally,
564 // and for error reporting.
565 enum EntryKind { kUnknown, kCIE, kFDE, kTerminator };
566
567 // The handler class to which the parser hands the parsed call frame
568 // information. Defined below.
569 class Handler;
570
571 // A reporter class, which CallFrameInfo uses to report errors
572 // encountered while parsing call frame information. Defined below.
573 class Reporter;
574
575 // Create a DWARF CFI parser. BUFFER points to the contents of the
576 // .debug_frame section to parse; BUFFER_LENGTH is its length in bytes.
577 // REPORTER is an error reporter the parser should use to report
578 // problems. READER is a ByteReader instance that has the endianness and
579 // address size set properly. Report the data we find to HANDLER.
580 //
581 // This class can also parse Linux C++ exception handling data, as found
582 // in '.eh_frame' sections. This data is a variant of DWARF CFI that is
583 // placed in loadable segments so that it is present in the program's
584 // address space, and is interpreted by the C++ runtime to search the
585 // call stack for a handler interested in the exception being thrown,
586 // actually pop the frames, and find cleanup code to run.
587 //
588 // There are two differences between the call frame information described
589 // in the DWARF standard and the exception handling data Linux places in
590 // the .eh_frame section:
591 //
592 // - Exception handling data uses uses a different format for call frame
593 // information entry headers. The distinguished CIE id, the way FDEs
594 // refer to their CIEs, and the way the end of the series of entries is
595 // determined are all slightly different.
596 //
597 // If the constructor's EH_FRAME argument is true, then the
598 // CallFrameInfo parses the entry headers as Linux C++ exception
599 // handling data. If EH_FRAME is false or omitted, the CallFrameInfo
600 // parses standard DWARF call frame information.
601 //
602 // - Linux C++ exception handling data uses CIE augmentation strings
603 // beginning with 'z' to specify the presence of additional data after
604 // the CIE and FDE headers and special encodings used for addresses in
605 // frame description entries.
606 //
607 // CallFrameInfo can handle 'z' augmentations in either DWARF CFI or
608 // exception handling data if you have supplied READER with the base
609 // addresses needed to interpret the pointer encodings that 'z'
610 // augmentations can specify. See the ByteReader interface for details
611 // about the base addresses. See the CallFrameInfo::Handler interface
612 // for details about the additional information one might find in
613 // 'z'-augmented data.
614 //
615 // Thus:
616 //
617 // - If you are parsing standard DWARF CFI, as found in a .debug_frame
618 // section, you should pass false for the EH_FRAME argument, or omit
619 // it, and you need not worry about providing READER with the
620 // additional base addresses.
621 //
622 // - If you want to parse Linux C++ exception handling data from a
623 // .eh_frame section, you should pass EH_FRAME as true, and call
624 // READER's Set*Base member functions before calling our Start method.
625 //
626 // - If you want to parse DWARF CFI that uses the 'z' augmentations
627 // (although I don't think any toolchain ever emits such data), you
628 // could pass false for EH_FRAME, but call READER's Set*Base members.
629 //
630 // The extensions the Linux C++ ABI makes to DWARF for exception
631 // handling are described here, rather poorly:
632 // http://refspecs.linux-foundation.org/LSB_4.0.0/LSB-Core-generic/LSB-Core-ge neric/dwarfext.html
633 // http://refspecs.linux-foundation.org/LSB_4.0.0/LSB-Core-generic/LSB-Core-ge neric/ehframechpt.html
634 //
635 // The mechanics of C++ exception handling, personality routines,
636 // and language-specific data areas are described here, rather nicely:
637 // http://www.codesourcery.com/public/cxx-abi/abi-eh.html
638 CallFrameInfo(const char *buffer, size_t buffer_length,
639 ByteReader *reader, Handler *handler, Reporter *reporter,
640 bool eh_frame = false)
641 : buffer_(buffer), buffer_length_(buffer_length),
642 reader_(reader), handler_(handler), reporter_(reporter),
643 eh_frame_(eh_frame) { }
644
645 ~CallFrameInfo() { }
646
647 // Parse the entries in BUFFER, reporting what we find to HANDLER.
648 // Return true if we reach the end of the section successfully, or
649 // false if we encounter an error.
650 bool Start();
651
652 // Return the textual name of KIND. For error reporting.
653 static const char *KindName(EntryKind kind);
654
655 private:
656
657 struct CIE;
658
659 // A CFI entry, either an FDE or a CIE.
660 struct Entry {
661 // The starting offset of the entry in the section, for error
662 // reporting.
663 size_t offset;
664
665 // The start of this entry in the buffer.
666 const char *start;
667
668 // Which kind of entry this is.
669 //
670 // We want to be able to use this for error reporting even while we're
671 // in the midst of parsing. Error reporting code may assume that kind,
672 // offset, and start fields are valid, although kind may be kUnknown.
673 EntryKind kind;
674
675 // The end of this entry's common prologue (initial length and id), and
676 // the start of this entry's kind-specific fields.
677 const char *fields;
678
679 // The start of this entry's instructions.
680 const char *instructions;
681
682 // The address past the entry's last byte in the buffer. (Note that
683 // since offset points to the entry's initial length field, and the
684 // length field is the number of bytes after that field, this is not
685 // simply buffer_ + offset + length.)
686 const char *end;
687
688 // For both DWARF CFI and .eh_frame sections, this is the CIE id in a
689 // CIE, and the offset of the associated CIE in an FDE.
690 uint64 id;
691
692 // The CIE that applies to this entry, if we've parsed it. If this is a
693 // CIE, then this field points to this structure.
694 CIE *cie;
695 };
696
697 // A common information entry (CIE).
698 struct CIE: public Entry {
699 uint8 version; // CFI data version number
700 string augmentation; // vendor format extension markers
701 uint64 code_alignment_factor; // scale for code address adjustments
702 int data_alignment_factor; // scale for stack pointer adjustments
703 unsigned return_address_register; // which register holds the return addr
704
705 // True if this CIE includes Linux C++ ABI 'z' augmentation data.
706 bool has_z_augmentation;
707
708 // Parsed 'z' augmentation data. These are meaningful only if
709 // has_z_augmentation is true.
710 bool has_z_lsda; // The 'z' augmentation included 'L'.
711 bool has_z_personality; // The 'z' augmentation included 'P'.
712 bool has_z_signal_frame; // The 'z' augmentation included 'S'.
713
714 // If has_z_lsda is true, this is the encoding to be used for language-
715 // specific data area pointers in FDEs.
716 DwarfPointerEncoding lsda_encoding;
717
718 // If has_z_personality is true, this is the encoding used for the
719 // personality routine pointer in the augmentation data.
720 DwarfPointerEncoding personality_encoding;
721
722 // If has_z_personality is true, this is the address of the personality
723 // routine --- or, if personality_encoding & DW_EH_PE_indirect, the
724 // address where the personality routine's address is stored.
725 uint64 personality_address;
726
727 // This is the encoding used for addresses in the FDE header and
728 // in DW_CFA_set_loc instructions. This is always valid, whether
729 // or not we saw a 'z' augmentation string; its default value is
730 // DW_EH_PE_absptr, which is what normal DWARF CFI uses.
731 DwarfPointerEncoding pointer_encoding;
732 };
733
734 // A frame description entry (FDE).
735 struct FDE: public Entry {
736 uint64 address; // start address of described code
737 uint64 size; // size of described code, in bytes
738
739 // If cie->has_z_lsda is true, then this is the language-specific data
740 // area's address --- or its address's address, if cie->lsda_encoding
741 // has the DW_EH_PE_indirect bit set.
742 uint64 lsda_address;
743 };
744
745 // Internal use.
746 class Rule;
747 class UndefinedRule;
748 class SameValueRule;
749 class OffsetRule;
750 class ValOffsetRule;
751 class RegisterRule;
752 class ExpressionRule;
753 class ValExpressionRule;
754 class RuleMap;
755 class State;
756
757 // Parse the initial length and id of a CFI entry, either a CIE, an FDE,
758 // or a .eh_frame end-of-data mark. CURSOR points to the beginning of the
759 // data to parse. On success, populate ENTRY as appropriate, and return
760 // true. On failure, report the problem, and return false. Even if we
761 // return false, set ENTRY->end to the first byte after the entry if we
762 // were able to figure that out, or NULL if we weren't.
763 bool ReadEntryPrologue(const char *cursor, Entry *entry);
764
765 // Parse the fields of a CIE after the entry prologue, including any 'z'
766 // augmentation data. Assume that the 'Entry' fields of CIE are
767 // populated; use CIE->fields and CIE->end as the start and limit for
768 // parsing. On success, populate the rest of *CIE, and return true; on
769 // failure, report the problem and return false.
770 bool ReadCIEFields(CIE *cie);
771
772 // Parse the fields of an FDE after the entry prologue, including any 'z'
773 // augmentation data. Assume that the 'Entry' fields of *FDE are
774 // initialized; use FDE->fields and FDE->end as the start and limit for
775 // parsing. Assume that FDE->cie is fully initialized. On success,
776 // populate the rest of *FDE, and return true; on failure, report the
777 // problem and return false.
778 bool ReadFDEFields(FDE *fde);
779
780 // Report that ENTRY is incomplete, and return false. This is just a
781 // trivial wrapper for invoking reporter_->Incomplete; it provides a
782 // little brevity.
783 bool ReportIncomplete(Entry *entry);
784
785 // Return true if ENCODING has the DW_EH_PE_indirect bit set.
786 static bool IsIndirectEncoding(DwarfPointerEncoding encoding) {
787 return encoding & DW_EH_PE_indirect;
788 }
789
790 // The contents of the DWARF .debug_info section we're parsing.
791 const char *buffer_;
792 size_t buffer_length_;
793
794 // For reading multi-byte values with the appropriate endianness.
795 ByteReader *reader_;
796
797 // The handler to which we should report the data we find.
798 Handler *handler_;
799
800 // For reporting problems in the info we're parsing.
801 Reporter *reporter_;
802
803 // True if we are processing .eh_frame-format data.
804 bool eh_frame_;
805 };
806
807 // The handler class for CallFrameInfo. The a CFI parser calls the
808 // member functions of a handler object to report the data it finds.
809 class CallFrameInfo::Handler {
810 public:
811 // The pseudo-register number for the canonical frame address.
812 enum { kCFARegister = -1 };
813
814 Handler() { }
815 virtual ~Handler() { }
816
817 // The parser has found CFI for the machine code at ADDRESS,
818 // extending for LENGTH bytes. OFFSET is the offset of the frame
819 // description entry in the section, for use in error messages.
820 // VERSION is the version number of the CFI format. AUGMENTATION is
821 // a string describing any producer-specific extensions present in
822 // the data. RETURN_ADDRESS is the number of the register that holds
823 // the address to which the function should return.
824 //
825 // Entry should return true to process this CFI, or false to skip to
826 // the next entry.
827 //
828 // The parser invokes Entry for each Frame Description Entry (FDE)
829 // it finds. The parser doesn't report Common Information Entries
830 // to the handler explicitly; instead, if the handler elects to
831 // process a given FDE, the parser reiterates the appropriate CIE's
832 // contents at the beginning of the FDE's rules.
833 virtual bool Entry(size_t offset, uint64 address, uint64 length,
834 uint8 version, const string &augmentation,
835 unsigned return_address) = 0;
836
837 // When the Entry function returns true, the parser calls these
838 // handler functions repeatedly to describe the rules for recovering
839 // registers at each instruction in the given range of machine code.
840 // Immediately after a call to Entry, the handler should assume that
841 // the rule for each callee-saves register is "unchanged" --- that
842 // is, that the register still has the value it had in the caller.
843 //
844 // If a *Rule function returns true, we continue processing this entry's
845 // instructions. If a *Rule function returns false, we stop evaluating
846 // instructions, and skip to the next entry. Either way, we call End
847 // before going on to the next entry.
848 //
849 // In all of these functions, if the REG parameter is kCFARegister, then
850 // the rule describes how to find the canonical frame address.
851 // kCFARegister may be passed as a BASE_REGISTER argument, meaning that
852 // the canonical frame address should be used as the base address for the
853 // computation. All other REG values will be positive.
854
855 // At ADDRESS, register REG's value is not recoverable.
856 virtual bool UndefinedRule(uint64 address, int reg) = 0;
857
858 // At ADDRESS, register REG's value is the same as that it had in
859 // the caller.
860 virtual bool SameValueRule(uint64 address, int reg) = 0;
861
862 // At ADDRESS, register REG has been saved at offset OFFSET from
863 // BASE_REGISTER.
864 virtual bool OffsetRule(uint64 address, int reg,
865 int base_register, long offset) = 0;
866
867 // At ADDRESS, the caller's value of register REG is the current
868 // value of BASE_REGISTER plus OFFSET. (This rule doesn't provide an
869 // address at which the register's value is saved.)
870 virtual bool ValOffsetRule(uint64 address, int reg,
871 int base_register, long offset) = 0;
872
873 // At ADDRESS, register REG has been saved in BASE_REGISTER. This differs
874 // from ValOffsetRule(ADDRESS, REG, BASE_REGISTER, 0), in that
875 // BASE_REGISTER is the "home" for REG's saved value: if you want to
876 // assign to a variable whose home is REG in the calling frame, you
877 // should put the value in BASE_REGISTER.
878 virtual bool RegisterRule(uint64 address, int reg, int base_register) = 0;
879
880 // At ADDRESS, the DWARF expression EXPRESSION yields the address at
881 // which REG was saved.
882 virtual bool ExpressionRule(uint64 address, int reg,
883 const string &expression) = 0;
884
885 // At ADDRESS, the DWARF expression EXPRESSION yields the caller's
886 // value for REG. (This rule doesn't provide an address at which the
887 // register's value is saved.)
888 virtual bool ValExpressionRule(uint64 address, int reg,
889 const string &expression) = 0;
890
891 // Indicate that the rules for the address range reported by the
892 // last call to Entry are complete. End should return true if
893 // everything is okay, or false if an error has occurred and parsing
894 // should stop.
895 virtual bool End() = 0;
896
897 // Handler functions for Linux C++ exception handling data. These are
898 // only called if the data includes 'z' augmentation strings.
899
900 // The Linux C++ ABI uses an extension of the DWARF CFI format to
901 // walk the stack to propagate exceptions from the throw to the
902 // appropriate catch, and do the appropriate cleanups along the way.
903 // CFI entries used for exception handling have two additional data
904 // associated with them:
905 //
906 // - The "language-specific data area" describes which exception
907 // types the function has 'catch' clauses for, and indicates how
908 // to go about re-entering the function at the appropriate catch
909 // clause. If the exception is not caught, it describes the
910 // destructors that must run before the frame is popped.
911 //
912 // - The "personality routine" is responsible for interpreting the
913 // language-specific data area's contents, and deciding whether
914 // the exception should continue to propagate down the stack,
915 // perhaps after doing some cleanup for this frame, or whether the
916 // exception will be caught here.
917 //
918 // In principle, the language-specific data area is opaque to
919 // everybody but the personality routine. In practice, these values
920 // may be useful or interesting to readers with extra context, and
921 // we have to at least skip them anyway, so we might as well report
922 // them to the handler.
923
924 // This entry's exception handling personality routine's address is
925 // ADDRESS. If INDIRECT is true, then ADDRESS is the address at
926 // which the routine's address is stored. The default definition for
927 // this handler function simply returns true, allowing parsing of
928 // the entry to continue.
929 virtual bool PersonalityRoutine(uint64 address, bool indirect) {
930 return true;
931 }
932
933 // This entry's language-specific data area (LSDA) is located at
934 // ADDRESS. If INDIRECT is true, then ADDRESS is the address at
935 // which the area's address is stored. The default definition for
936 // this handler function simply returns true, allowing parsing of
937 // the entry to continue.
938 virtual bool LanguageSpecificDataArea(uint64 address, bool indirect) {
939 return true;
940 }
941
942 // This entry describes a signal trampoline --- this frame is the
943 // caller of a signal handler. The default definition for this
944 // handler function simply returns true, allowing parsing of the
945 // entry to continue.
946 //
947 // The best description of the rationale for and meaning of signal
948 // trampoline CFI entries seems to be in the GCC bug database:
949 // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=26208
950 virtual bool SignalHandler() { return true; }
951 };
952
953 // The CallFrameInfo class makes calls on an instance of this class to
954 // report errors or warn about problems in the data it is parsing. The
955 // default definitions of these methods print a message to stderr, but
956 // you can make a derived class that overrides them.
957 class CallFrameInfo::Reporter {
958 public:
959 // Create an error reporter which attributes troubles to the section
960 // named SECTION in FILENAME.
961 //
962 // Normally SECTION would be .debug_frame, but the Mac puts CFI data
963 // in a Mach-O section named __debug_frame. If we support
964 // Linux-style exception handling data, we could be reading an
965 // .eh_frame section.
966 Reporter(const string &filename,
967 const string &section = ".debug_frame")
968 : filename_(filename), section_(section) { }
969 virtual ~Reporter() { }
970
971 // The CFI entry at OFFSET ends too early to be well-formed. KIND
972 // indicates what kind of entry it is; KIND can be kUnknown if we
973 // haven't parsed enough of the entry to tell yet.
974 virtual void Incomplete(uint64 offset, CallFrameInfo::EntryKind kind);
975
976 // The .eh_frame data has a four-byte zero at OFFSET where the next
977 // entry's length would be; this is a terminator. However, the buffer
978 // length as given to the CallFrameInfo constructor says there should be
979 // more data.
980 virtual void EarlyEHTerminator(uint64 offset);
981
982 // The FDE at OFFSET refers to the CIE at CIE_OFFSET, but the
983 // section is not that large.
984 virtual void CIEPointerOutOfRange(uint64 offset, uint64 cie_offset);
985
986 // The FDE at OFFSET refers to the CIE at CIE_OFFSET, but the entry
987 // there is not a CIE.
988 virtual void BadCIEId(uint64 offset, uint64 cie_offset);
989
990 // The FDE at OFFSET refers to a CIE with version number VERSION,
991 // which we don't recognize. We cannot parse DWARF CFI if it uses
992 // a version number we don't recognize.
993 virtual void UnrecognizedVersion(uint64 offset, int version);
994
995 // The FDE at OFFSET refers to a CIE with augmentation AUGMENTATION,
996 // which we don't recognize. We cannot parse DWARF CFI if it uses
997 // augmentations we don't recognize.
998 virtual void UnrecognizedAugmentation(uint64 offset,
999 const string &augmentation);
1000
1001 // The pointer encoding ENCODING, specified by the CIE at OFFSET, is not
1002 // a valid encoding.
1003 virtual void InvalidPointerEncoding(uint64 offset, uint8 encoding);
1004
1005 // The pointer encoding ENCODING, specified by the CIE at OFFSET, depends
1006 // on a base address which has not been supplied.
1007 virtual void UnusablePointerEncoding(uint64 offset, uint8 encoding);
1008
1009 // The CIE at OFFSET contains a DW_CFA_restore instruction at
1010 // INSN_OFFSET, which may not appear in a CIE.
1011 virtual void RestoreInCIE(uint64 offset, uint64 insn_offset);
1012
1013 // The entry at OFFSET, of kind KIND, has an unrecognized
1014 // instruction at INSN_OFFSET.
1015 virtual void BadInstruction(uint64 offset, CallFrameInfo::EntryKind kind,
1016 uint64 insn_offset);
1017
1018 // The instruction at INSN_OFFSET in the entry at OFFSET, of kind
1019 // KIND, establishes a rule that cites the CFA, but we have not
1020 // established a CFA rule yet.
1021 virtual void NoCFARule(uint64 offset, CallFrameInfo::EntryKind kind,
1022 uint64 insn_offset);
1023
1024 // The instruction at INSN_OFFSET in the entry at OFFSET, of kind
1025 // KIND, is a DW_CFA_restore_state instruction, but the stack of
1026 // saved states is empty.
1027 virtual void EmptyStateStack(uint64 offset, CallFrameInfo::EntryKind kind,
1028 uint64 insn_offset);
1029
1030 // The DW_CFA_remember_state instruction at INSN_OFFSET in the entry
1031 // at OFFSET, of kind KIND, would restore a state that has no CFA
1032 // rule, whereas the current state does have a CFA rule. This is
1033 // bogus input, which the CallFrameInfo::Handler interface doesn't
1034 // (and shouldn't) have any way to report.
1035 virtual void ClearingCFARule(uint64 offset, CallFrameInfo::EntryKind kind,
1036 uint64 insn_offset);
1037
1038 protected:
1039 // The name of the file whose CFI we're reading.
1040 string filename_;
1041
1042 // The name of the CFI section in that file.
1043 string section_;
1044 };
1045
1046 } // namespace dwarf2reader
1047
1048 #endif // UTIL_DEBUGINFO_DWARF2READER_H__
OLDNEW
« no previous file with comments | « obsolete/breakpad/common/dwarf/dwarf2enums.h ('k') | obsolete/breakpad/common/dwarf/dwarf2reader.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698