toolkit/crashreporter/google-breakpad/src/common/dwarf/dwarf2reader.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/crashreporter/google-breakpad/src/common/dwarf/dwarf2reader.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,1050 @@
     1.4 +// -*- mode: C++ -*-
     1.5 +
     1.6 +// Copyright (c) 2010 Google Inc. All Rights Reserved.
     1.7 +//
     1.8 +// Redistribution and use in source and binary forms, with or without
     1.9 +// modification, are permitted provided that the following conditions are
    1.10 +// met:
    1.11 +//
    1.12 +//     * Redistributions of source code must retain the above copyright
    1.13 +// notice, this list of conditions and the following disclaimer.
    1.14 +//     * Redistributions in binary form must reproduce the above
    1.15 +// copyright notice, this list of conditions and the following disclaimer
    1.16 +// in the documentation and/or other materials provided with the
    1.17 +// distribution.
    1.18 +//     * Neither the name of Google Inc. nor the names of its
    1.19 +// contributors may be used to endorse or promote products derived from
    1.20 +// this software without specific prior written permission.
    1.21 +//
    1.22 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.23 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.24 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.25 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    1.26 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.27 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    1.28 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.29 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.30 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.31 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.32 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.33 +
    1.34 +// CFI reader author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com>
    1.35 +
    1.36 +// This file contains definitions related to the DWARF2/3 reader and
    1.37 +// it's handler interfaces.
    1.38 +// The DWARF2/3 specification can be found at
    1.39 +// http://dwarf.freestandards.org and should be considered required
    1.40 +// reading if you wish to modify the implementation.
    1.41 +// Only a cursory attempt is made to explain terminology that is
    1.42 +// used here, as it is much better explained in the standard documents
    1.43 +#ifndef COMMON_DWARF_DWARF2READER_H__
    1.44 +#define COMMON_DWARF_DWARF2READER_H__
    1.45 +
    1.46 +#include <list>
    1.47 +#include <map>
    1.48 +#include <string>
    1.49 +#include <utility>
    1.50 +#include <vector>
    1.51 +
    1.52 +#include "common/dwarf/bytereader.h"
    1.53 +#include "common/dwarf/dwarf2enums.h"
    1.54 +#include "common/dwarf/types.h"
    1.55 +#include "common/using_std_string.h"
    1.56 +
    1.57 +namespace dwarf2reader {
    1.58 +struct LineStateMachine;
    1.59 +class Dwarf2Handler;
    1.60 +class LineInfoHandler;
    1.61 +
    1.62 +// This maps from a string naming a section to a pair containing a
    1.63 +// the data for the section, and the size of the section.
    1.64 +typedef std::map<string, std::pair<const char*, uint64> > SectionMap;
    1.65 +typedef std::list<std::pair<enum DwarfAttribute, enum DwarfForm> >
    1.66 +    AttributeList;
    1.67 +typedef AttributeList::iterator AttributeIterator;
    1.68 +typedef AttributeList::const_iterator ConstAttributeIterator;
    1.69 +
    1.70 +struct LineInfoHeader {
    1.71 +  uint64 total_length;
    1.72 +  uint16 version;
    1.73 +  uint64 prologue_length;
    1.74 +  uint8 min_insn_length; // insn stands for instructin
    1.75 +  bool default_is_stmt; // stmt stands for statement
    1.76 +  int8 line_base;
    1.77 +  uint8 line_range;
    1.78 +  uint8 opcode_base;
    1.79 +  // Use a pointer so that signalsafe_addr2line is able to use this structure
    1.80 +  // without heap allocation problem.
    1.81 +  std::vector<unsigned char> *std_opcode_lengths;
    1.82 +};
    1.83 +
    1.84 +class LineInfo {
    1.85 + public:
    1.86 +
    1.87 +  // Initializes a .debug_line reader. Buffer and buffer length point
    1.88 +  // to the beginning and length of the line information to read.
    1.89 +  // Reader is a ByteReader class that has the endianness set
    1.90 +  // properly.
    1.91 +  LineInfo(const char* buffer_, uint64 buffer_length,
    1.92 +           ByteReader* reader, LineInfoHandler* handler);
    1.93 +
    1.94 +  virtual ~LineInfo() {
    1.95 +    if (header_.std_opcode_lengths) {
    1.96 +      delete header_.std_opcode_lengths;
    1.97 +    }
    1.98 +  }
    1.99 +
   1.100 +  // Start processing line info, and calling callbacks in the handler.
   1.101 +  // Consumes the line number information for a single compilation unit.
   1.102 +  // Returns the number of bytes processed.
   1.103 +  uint64 Start();
   1.104 +
   1.105 +  // Process a single line info opcode at START using the state
   1.106 +  // machine at LSM.  Return true if we should define a line using the
   1.107 +  // current state of the line state machine.  Place the length of the
   1.108 +  // opcode in LEN.
   1.109 +  // If LSM_PASSES_PC is non-NULL, this function also checks if the lsm
   1.110 +  // passes the address of PC. In other words, LSM_PASSES_PC will be
   1.111 +  // set to true, if the following condition is met.
   1.112 +  //
   1.113 +  // lsm's old address < PC <= lsm's new address
   1.114 +  static bool ProcessOneOpcode(ByteReader* reader,
   1.115 +                               LineInfoHandler* handler,
   1.116 +                               const struct LineInfoHeader &header,
   1.117 +                               const char* start,
   1.118 +                               struct LineStateMachine* lsm,
   1.119 +                               size_t* len,
   1.120 +                               uintptr pc,
   1.121 +                               bool *lsm_passes_pc);
   1.122 +
   1.123 + private:
   1.124 +  // Reads the DWARF2/3 header for this line info.
   1.125 +  void ReadHeader();
   1.126 +
   1.127 +  // Reads the DWARF2/3 line information
   1.128 +  void ReadLines();
   1.129 +
   1.130 +  // The associated handler to call processing functions in
   1.131 +  LineInfoHandler* handler_;
   1.132 +
   1.133 +  // The associated ByteReader that handles endianness issues for us
   1.134 +  ByteReader* reader_;
   1.135 +
   1.136 +  // A DWARF2/3 line info header.  This is not the same size as
   1.137 +  // in the actual file, as the one in the file may have a 32 bit or
   1.138 +  // 64 bit lengths
   1.139 +
   1.140 +  struct LineInfoHeader header_;
   1.141 +
   1.142 +  // buffer is the buffer for our line info, starting at exactly where
   1.143 +  // the line info to read is.  after_header is the place right after
   1.144 +  // the end of the line information header.
   1.145 +  const char* buffer_;
   1.146 +  uint64 buffer_length_;
   1.147 +  const char* after_header_;
   1.148 +};
   1.149 +
   1.150 +// This class is the main interface between the line info reader and
   1.151 +// the client.  The virtual functions inside this get called for
   1.152 +// interesting events that happen during line info reading.  The
   1.153 +// default implementation does nothing
   1.154 +
   1.155 +class LineInfoHandler {
   1.156 + public:
   1.157 +  LineInfoHandler() { }
   1.158 +
   1.159 +  virtual ~LineInfoHandler() { }
   1.160 +
   1.161 +  // Called when we define a directory.  NAME is the directory name,
   1.162 +  // DIR_NUM is the directory number
   1.163 +  virtual void DefineDir(const string& name, uint32 dir_num) { }
   1.164 +
   1.165 +  // Called when we define a filename. NAME is the filename, FILE_NUM
   1.166 +  // is the file number which is -1 if the file index is the next
   1.167 +  // index after the last numbered index (this happens when files are
   1.168 +  // dynamically defined by the line program), DIR_NUM is the
   1.169 +  // directory index for the directory name of this file, MOD_TIME is
   1.170 +  // the modification time of the file, and LENGTH is the length of
   1.171 +  // the file
   1.172 +  virtual void DefineFile(const string& name, int32 file_num,
   1.173 +                          uint32 dir_num, uint64 mod_time,
   1.174 +                          uint64 length) { }
   1.175 +
   1.176 +  // Called when the line info reader has a new line, address pair
   1.177 +  // ready for us. ADDRESS is the address of the code, LENGTH is the
   1.178 +  // length of its machine code in bytes, FILE_NUM is the file number
   1.179 +  // containing the code, LINE_NUM is the line number in that file for
   1.180 +  // the code, and COLUMN_NUM is the column number the code starts at,
   1.181 +  // if we know it (0 otherwise).
   1.182 +  virtual void AddLine(uint64 address, uint64 length,
   1.183 +                       uint32 file_num, uint32 line_num, uint32 column_num) { }
   1.184 +};
   1.185 +
   1.186 +// The base of DWARF2/3 debug info is a DIE (Debugging Information
   1.187 +// Entry.
   1.188 +// DWARF groups DIE's into a tree and calls the root of this tree a
   1.189 +// "compilation unit".  Most of the time, there is one compilation
   1.190 +// unit in the .debug_info section for each file that had debug info
   1.191 +// generated.
   1.192 +// Each DIE consists of
   1.193 +
   1.194 +// 1. a tag specifying a thing that is being described (ie
   1.195 +// DW_TAG_subprogram for functions, DW_TAG_variable for variables, etc
   1.196 +// 2. attributes (such as DW_AT_location for location in memory,
   1.197 +// DW_AT_name for name), and data for each attribute.
   1.198 +// 3. A flag saying whether the DIE has children or not
   1.199 +
   1.200 +// In order to gain some amount of compression, the format of
   1.201 +// each DIE (tag name, attributes and data forms for the attributes)
   1.202 +// are stored in a separate table called the "abbreviation table".
   1.203 +// This is done because a large number of DIEs have the exact same tag
   1.204 +// and list of attributes, but different data for those attributes.
   1.205 +// As a result, the .debug_info section is just a stream of data, and
   1.206 +// requires reading of the .debug_abbrev section to say what the data
   1.207 +// means.
   1.208 +
   1.209 +// As a warning to the user, it should be noted that the reason for
   1.210 +// using absolute offsets from the beginning of .debug_info is that
   1.211 +// DWARF2/3 supports referencing DIE's from other DIE's by their offset
   1.212 +// from either the current compilation unit start, *or* the beginning
   1.213 +// of the .debug_info section.  This means it is possible to reference
   1.214 +// a DIE in one compilation unit from a DIE in another compilation
   1.215 +// unit.  This style of reference is usually used to eliminate
   1.216 +// duplicated information that occurs across compilation
   1.217 +// units, such as base types, etc.  GCC 3.4+ support this with
   1.218 +// -feliminate-dwarf2-dups.  Other toolchains will sometimes do
   1.219 +// duplicate elimination in the linker.
   1.220 +
   1.221 +class CompilationUnit {
   1.222 + public:
   1.223 +
   1.224 +  // Initialize a compilation unit.  This requires a map of sections,
   1.225 +  // the offset of this compilation unit in the .debug_info section, a
   1.226 +  // ByteReader, and a Dwarf2Handler class to call callbacks in.
   1.227 +  CompilationUnit(const SectionMap& sections, uint64 offset,
   1.228 +                  ByteReader* reader, Dwarf2Handler* handler);
   1.229 +  virtual ~CompilationUnit() {
   1.230 +    if (abbrevs_) delete abbrevs_;
   1.231 +  }
   1.232 +
   1.233 +  // Begin reading a Dwarf2 compilation unit, and calling the
   1.234 +  // callbacks in the Dwarf2Handler
   1.235 +
   1.236 +  // Return the full length of the compilation unit, including
   1.237 +  // headers. This plus the starting offset passed to the constructor
   1.238 +  // is the offset of the end of the compilation unit --- and the
   1.239 +  // start of the next compilation unit, if there is one.
   1.240 +  uint64 Start();
   1.241 +
   1.242 + private:
   1.243 +
   1.244 +  // This struct represents a single DWARF2/3 abbreviation
   1.245 +  // The abbreviation tells how to read a DWARF2/3 DIE, and consist of a
   1.246 +  // tag and a list of attributes, as well as the data form of each attribute.
   1.247 +  struct Abbrev {
   1.248 +    uint64 number;
   1.249 +    enum DwarfTag tag;
   1.250 +    bool has_children;
   1.251 +    AttributeList attributes;
   1.252 +  };
   1.253 +
   1.254 +  // A DWARF2/3 compilation unit header.  This is not the same size as
   1.255 +  // in the actual file, as the one in the file may have a 32 bit or
   1.256 +  // 64 bit length.
   1.257 +  struct CompilationUnitHeader {
   1.258 +    uint64 length;
   1.259 +    uint16 version;
   1.260 +    uint64 abbrev_offset;
   1.261 +    uint8 address_size;
   1.262 +  } header_;
   1.263 +
   1.264 +  // Reads the DWARF2/3 header for this compilation unit.
   1.265 +  void ReadHeader();
   1.266 +
   1.267 +  // Reads the DWARF2/3 abbreviations for this compilation unit
   1.268 +  void ReadAbbrevs();
   1.269 +
   1.270 +  // Processes a single DIE for this compilation unit and return a new
   1.271 +  // pointer just past the end of it
   1.272 +  const char* ProcessDIE(uint64 dieoffset,
   1.273 +                                  const char* start,
   1.274 +                                  const Abbrev& abbrev);
   1.275 +
   1.276 +  // Processes a single attribute and return a new pointer just past the
   1.277 +  // end of it
   1.278 +  const char* ProcessAttribute(uint64 dieoffset,
   1.279 +                                        const char* start,
   1.280 +                                        enum DwarfAttribute attr,
   1.281 +                                        enum DwarfForm form);
   1.282 +
   1.283 +  // Processes all DIEs for this compilation unit
   1.284 +  void ProcessDIEs();
   1.285 +
   1.286 +  // Skips the die with attributes specified in ABBREV starting at
   1.287 +  // START, and return the new place to position the stream to.
   1.288 +  const char* SkipDIE(const char* start,
   1.289 +                               const Abbrev& abbrev);
   1.290 +
   1.291 +  // Skips the attribute starting at START, with FORM, and return the
   1.292 +  // new place to position the stream to.
   1.293 +  const char* SkipAttribute(const char* start,
   1.294 +                                     enum DwarfForm form);
   1.295 +
   1.296 +  // Offset from section start is the offset of this compilation unit
   1.297 +  // from the beginning of the .debug_info section.
   1.298 +  uint64 offset_from_section_start_;
   1.299 +
   1.300 +  // buffer is the buffer for our CU, starting at .debug_info + offset
   1.301 +  // passed in from constructor.
   1.302 +  // after_header points to right after the compilation unit header.
   1.303 +  const char* buffer_;
   1.304 +  uint64 buffer_length_;
   1.305 +  const char* after_header_;
   1.306 +
   1.307 +  // The associated ByteReader that handles endianness issues for us
   1.308 +  ByteReader* reader_;
   1.309 +
   1.310 +  // The map of sections in our file to buffers containing their data
   1.311 +  const SectionMap& sections_;
   1.312 +
   1.313 +  // The associated handler to call processing functions in
   1.314 +  Dwarf2Handler* handler_;
   1.315 +
   1.316 +  // Set of DWARF2/3 abbreviations for this compilation unit.  Indexed
   1.317 +  // by abbreviation number, which means that abbrevs_[0] is not
   1.318 +  // valid.
   1.319 +  std::vector<Abbrev>* abbrevs_;
   1.320 +
   1.321 +  // String section buffer and length, if we have a string section.
   1.322 +  // This is here to avoid doing a section lookup for strings in
   1.323 +  // ProcessAttribute, which is in the hot path for DWARF2 reading.
   1.324 +  const char* string_buffer_;
   1.325 +  uint64 string_buffer_length_;
   1.326 +};
   1.327 +
   1.328 +// This class is the main interface between the reader and the
   1.329 +// client.  The virtual functions inside this get called for
   1.330 +// interesting events that happen during DWARF2 reading.
   1.331 +// The default implementation skips everything.
   1.332 +
   1.333 +class Dwarf2Handler {
   1.334 + public:
   1.335 +  Dwarf2Handler() { }
   1.336 +
   1.337 +  virtual ~Dwarf2Handler() { }
   1.338 +
   1.339 +  // Start to process a compilation unit at OFFSET from the beginning of the
   1.340 +  // .debug_info section. Return false if you would like to skip this
   1.341 +  // compilation unit.
   1.342 +  virtual bool StartCompilationUnit(uint64 offset, uint8 address_size,
   1.343 +                                    uint8 offset_size, uint64 cu_length,
   1.344 +                                    uint8 dwarf_version) { return false; }
   1.345 +
   1.346 +  // Start to process a DIE at OFFSET from the beginning of the .debug_info
   1.347 +  // section. Return false if you would like to skip this DIE.
   1.348 +  virtual bool StartDIE(uint64 offset, enum DwarfTag tag) { return false; }
   1.349 +
   1.350 +  // Called when we have an attribute with unsigned data to give to our
   1.351 +  // handler. The attribute is for the DIE at OFFSET from the beginning of the
   1.352 +  // .debug_info section. Its name is ATTR, its form is FORM, and its value is
   1.353 +  // DATA.
   1.354 +  virtual void ProcessAttributeUnsigned(uint64 offset,
   1.355 +                                        enum DwarfAttribute attr,
   1.356 +                                        enum DwarfForm form,
   1.357 +                                        uint64 data) { }
   1.358 +
   1.359 +  // Called when we have an attribute with signed data to give to our handler.
   1.360 +  // The attribute is for the DIE at OFFSET from the beginning of the
   1.361 +  // .debug_info section. Its name is ATTR, its form is FORM, and its value is
   1.362 +  // DATA.
   1.363 +  virtual void ProcessAttributeSigned(uint64 offset,
   1.364 +                                      enum DwarfAttribute attr,
   1.365 +                                      enum DwarfForm form,
   1.366 +                                      int64 data) { }
   1.367 +
   1.368 +  // Called when we have an attribute whose value is a reference to
   1.369 +  // another DIE. The attribute belongs to the DIE at OFFSET from the
   1.370 +  // beginning of the .debug_info section. Its name is ATTR, its form
   1.371 +  // is FORM, and the offset of the DIE being referred to from the
   1.372 +  // beginning of the .debug_info section is DATA.
   1.373 +  virtual void ProcessAttributeReference(uint64 offset,
   1.374 +                                         enum DwarfAttribute attr,
   1.375 +                                         enum DwarfForm form,
   1.376 +                                         uint64 data) { }
   1.377 +
   1.378 +  // Called when we have an attribute with a buffer of data to give to our
   1.379 +  // handler. The attribute is for the DIE at OFFSET from the beginning of the
   1.380 +  // .debug_info section. Its name is ATTR, its form is FORM, DATA points to
   1.381 +  // the buffer's contents, and its length in bytes is LENGTH. The buffer is
   1.382 +  // owned by the caller, not the callee, and may not persist for very long.
   1.383 +  // If you want the data to be available later, it needs to be copied.
   1.384 +  virtual void ProcessAttributeBuffer(uint64 offset,
   1.385 +                                      enum DwarfAttribute attr,
   1.386 +                                      enum DwarfForm form,
   1.387 +                                      const char* data,
   1.388 +                                      uint64 len) { }
   1.389 +
   1.390 +  // Called when we have an attribute with string data to give to our handler.
   1.391 +  // The attribute is for the DIE at OFFSET from the beginning of the
   1.392 +  // .debug_info section. Its name is ATTR, its form is FORM, and its value is
   1.393 +  // DATA.
   1.394 +  virtual void ProcessAttributeString(uint64 offset,
   1.395 +                                      enum DwarfAttribute attr,
   1.396 +                                      enum DwarfForm form,
   1.397 +                                      const string& data) { }
   1.398 +
   1.399 +  // Called when we have an attribute whose value is the 64-bit signature
   1.400 +  // of a type unit in the .debug_types section. OFFSET is the offset of
   1.401 +  // the DIE whose attribute we're reporting. ATTR and FORM are the
   1.402 +  // attribute's name and form. SIGNATURE is the type unit's signature.
   1.403 +  virtual void ProcessAttributeSignature(uint64 offset,
   1.404 +                                         enum DwarfAttribute attr,
   1.405 +                                         enum DwarfForm form,
   1.406 +                                         uint64 signature) { }
   1.407 +
   1.408 +  // Called when finished processing the DIE at OFFSET.
   1.409 +  // Because DWARF2/3 specifies a tree of DIEs, you may get starts
   1.410 +  // before ends of the previous DIE, as we process children before
   1.411 +  // ending the parent.
   1.412 +  virtual void EndDIE(uint64 offset) { }
   1.413 +
   1.414 +};
   1.415 +
   1.416 +// This class is a reader for DWARF's Call Frame Information.  CFI
   1.417 +// describes how to unwind stack frames --- even for functions that do
   1.418 +// not follow fixed conventions for saving registers, whose frame size
   1.419 +// varies as they execute, etc.
   1.420 +//
   1.421 +// CFI describes, at each machine instruction, how to compute the
   1.422 +// stack frame's base address, how to find the return address, and
   1.423 +// where to find the saved values of the caller's registers (if the
   1.424 +// callee has stashed them somewhere to free up the registers for its
   1.425 +// own use).
   1.426 +//
   1.427 +// For example, suppose we have a function whose machine code looks
   1.428 +// like this (imagine an assembly language that looks like C, for a
   1.429 +// machine with 32-bit registers, and a stack that grows towards lower
   1.430 +// addresses):
   1.431 +//
   1.432 +// func:                                ; entry point; return address at sp
   1.433 +// func+0:      sp = sp - 16            ; allocate space for stack frame
   1.434 +// func+1:      sp[12] = r0             ; save r0 at sp+12
   1.435 +// ...                                  ; other code, not frame-related
   1.436 +// func+10:     sp -= 4; *sp = x        ; push some x on the stack
   1.437 +// ...                                  ; other code, not frame-related
   1.438 +// func+20:     r0 = sp[16]             ; restore saved r0
   1.439 +// func+21:     sp += 20                ; pop whole stack frame
   1.440 +// func+22:     pc = *sp; sp += 4       ; pop return address and jump to it
   1.441 +//
   1.442 +// DWARF CFI is (a very compressed representation of) a table with a
   1.443 +// row for each machine instruction address and a column for each
   1.444 +// register showing how to restore it, if possible.
   1.445 +//
   1.446 +// A special column named "CFA", for "Canonical Frame Address", tells how
   1.447 +// to compute the base address of the frame; registers' entries may
   1.448 +// refer to the CFA in describing where the registers are saved.
   1.449 +//
   1.450 +// Another special column, named "RA", represents the return address.
   1.451 +//
   1.452 +// For example, here is a complete (uncompressed) table describing the
   1.453 +// function above:
   1.454 +// 
   1.455 +//     insn      cfa    r0      r1 ...  ra
   1.456 +//     =======================================
   1.457 +//     func+0:   sp                     cfa[0]
   1.458 +//     func+1:   sp+16                  cfa[0] 
   1.459 +//     func+2:   sp+16  cfa[-4]         cfa[0]
   1.460 +//     func+11:  sp+20  cfa[-4]         cfa[0]
   1.461 +//     func+21:  sp+20                  cfa[0]
   1.462 +//     func+22:  sp                     cfa[0]
   1.463 +//
   1.464 +// Some things to note here:
   1.465 +//
   1.466 +// - Each row describes the state of affairs *before* executing the
   1.467 +//   instruction at the given address.  Thus, the row for func+0
   1.468 +//   describes the state before we allocate the stack frame.  In the
   1.469 +//   next row, the formula for computing the CFA has changed,
   1.470 +//   reflecting that allocation.
   1.471 +//
   1.472 +// - The other entries are written in terms of the CFA; this allows
   1.473 +//   them to remain unchanged as the stack pointer gets bumped around.
   1.474 +//   For example, the rule for recovering the return address (the "ra"
   1.475 +//   column) remains unchanged throughout the function, even as the
   1.476 +//   stack pointer takes on three different offsets from the return
   1.477 +//   address.
   1.478 +//
   1.479 +// - Although we haven't shown it, most calling conventions designate
   1.480 +//   "callee-saves" and "caller-saves" registers. The callee must
   1.481 +//   preserve the values of callee-saves registers; if it uses them,
   1.482 +//   it must save their original values somewhere, and restore them
   1.483 +//   before it returns. In contrast, the callee is free to trash
   1.484 +//   caller-saves registers; if the callee uses these, it will
   1.485 +//   probably not bother to save them anywhere, and the CFI will
   1.486 +//   probably mark their values as "unrecoverable".
   1.487 +//
   1.488 +//   (However, since the caller cannot assume the callee was going to
   1.489 +//   save them, caller-saves registers are probably dead in the caller
   1.490 +//   anyway, so compilers usually don't generate CFA for caller-saves
   1.491 +//   registers.)
   1.492 +// 
   1.493 +// - Exactly where the CFA points is a matter of convention that
   1.494 +//   depends on the architecture and ABI in use. In the example, the
   1.495 +//   CFA is the value the stack pointer had upon entry to the
   1.496 +//   function, pointing at the saved return address. But on the x86,
   1.497 +//   the call frame information generated by GCC follows the
   1.498 +//   convention that the CFA is the address *after* the saved return
   1.499 +//   address.
   1.500 +//
   1.501 +//   But by definition, the CFA remains constant throughout the
   1.502 +//   lifetime of the frame. This makes it a useful value for other
   1.503 +//   columns to refer to. It is also gives debuggers a useful handle
   1.504 +//   for identifying a frame.
   1.505 +//
   1.506 +// If you look at the table above, you'll notice that a given entry is
   1.507 +// often the same as the one immediately above it: most instructions
   1.508 +// change only one or two aspects of the stack frame, if they affect
   1.509 +// it at all. The DWARF format takes advantage of this fact, and
   1.510 +// reduces the size of the data by mentioning only the addresses and
   1.511 +// columns at which changes take place. So for the above, DWARF CFI
   1.512 +// data would only actually mention the following:
   1.513 +// 
   1.514 +//     insn      cfa    r0      r1 ...  ra
   1.515 +//     =======================================
   1.516 +//     func+0:   sp                     cfa[0]
   1.517 +//     func+1:   sp+16
   1.518 +//     func+2:          cfa[-4]
   1.519 +//     func+11:  sp+20
   1.520 +//     func+21:         r0
   1.521 +//     func+22:  sp            
   1.522 +//
   1.523 +// In fact, this is the way the parser reports CFI to the consumer: as
   1.524 +// a series of statements of the form, "At address X, column Y changed
   1.525 +// to Z," and related conventions for describing the initial state.
   1.526 +//
   1.527 +// Naturally, it would be impractical to have to scan the entire
   1.528 +// program's CFI, noting changes as we go, just to recover the
   1.529 +// unwinding rules in effect at one particular instruction. To avoid
   1.530 +// this, CFI data is grouped into "entries", each of which covers a
   1.531 +// specified range of addresses and begins with a complete statement
   1.532 +// of the rules for all recoverable registers at that starting
   1.533 +// address. Each entry typically covers a single function.
   1.534 +//
   1.535 +// Thus, to compute the contents of a given row of the table --- that
   1.536 +// is, rules for recovering the CFA, RA, and registers at a given
   1.537 +// instruction --- the consumer should find the entry that covers that
   1.538 +// instruction's address, start with the initial state supplied at the
   1.539 +// beginning of the entry, and work forward until it has processed all
   1.540 +// the changes up to and including those for the present instruction.
   1.541 +//
   1.542 +// There are seven kinds of rules that can appear in an entry of the
   1.543 +// table:
   1.544 +//
   1.545 +// - "undefined": The given register is not preserved by the callee;
   1.546 +//   its value cannot be recovered.
   1.547 +//
   1.548 +// - "same value": This register has the same value it did in the callee.
   1.549 +//
   1.550 +// - offset(N): The register is saved at offset N from the CFA.
   1.551 +//
   1.552 +// - val_offset(N): The value the register had in the caller is the
   1.553 +//   CFA plus offset N. (This is usually only useful for describing
   1.554 +//   the stack pointer.)
   1.555 +//
   1.556 +// - register(R): The register's value was saved in another register R.
   1.557 +//
   1.558 +// - expression(E): Evaluating the DWARF expression E using the
   1.559 +//   current frame's registers' values yields the address at which the
   1.560 +//   register was saved.
   1.561 +//
   1.562 +// - val_expression(E): Evaluating the DWARF expression E using the
   1.563 +//   current frame's registers' values yields the value the register
   1.564 +//   had in the caller.
   1.565 +
   1.566 +class CallFrameInfo {
   1.567 + public:
   1.568 +  // The different kinds of entries one finds in CFI. Used internally,
   1.569 +  // and for error reporting.
   1.570 +  enum EntryKind { kUnknown, kCIE, kFDE, kTerminator };
   1.571 +
   1.572 +  // The handler class to which the parser hands the parsed call frame
   1.573 +  // information.  Defined below.
   1.574 +  class Handler;
   1.575 +
   1.576 +  // A reporter class, which CallFrameInfo uses to report errors
   1.577 +  // encountered while parsing call frame information.  Defined below.
   1.578 +  class Reporter;
   1.579 +
   1.580 +  // Create a DWARF CFI parser. BUFFER points to the contents of the
   1.581 +  // .debug_frame section to parse; BUFFER_LENGTH is its length in bytes.
   1.582 +  // REPORTER is an error reporter the parser should use to report
   1.583 +  // problems. READER is a ByteReader instance that has the endianness and
   1.584 +  // address size set properly. Report the data we find to HANDLER.
   1.585 +  //
   1.586 +  // This class can also parse Linux C++ exception handling data, as found
   1.587 +  // in '.eh_frame' sections. This data is a variant of DWARF CFI that is
   1.588 +  // placed in loadable segments so that it is present in the program's
   1.589 +  // address space, and is interpreted by the C++ runtime to search the
   1.590 +  // call stack for a handler interested in the exception being thrown,
   1.591 +  // actually pop the frames, and find cleanup code to run.
   1.592 +  //
   1.593 +  // There are two differences between the call frame information described
   1.594 +  // in the DWARF standard and the exception handling data Linux places in
   1.595 +  // the .eh_frame section:
   1.596 +  //
   1.597 +  // - Exception handling data uses uses a different format for call frame
   1.598 +  //   information entry headers. The distinguished CIE id, the way FDEs
   1.599 +  //   refer to their CIEs, and the way the end of the series of entries is
   1.600 +  //   determined are all slightly different.
   1.601 +  //
   1.602 +  //   If the constructor's EH_FRAME argument is true, then the
   1.603 +  //   CallFrameInfo parses the entry headers as Linux C++ exception
   1.604 +  //   handling data. If EH_FRAME is false or omitted, the CallFrameInfo
   1.605 +  //   parses standard DWARF call frame information.
   1.606 +  //
   1.607 +  // - Linux C++ exception handling data uses CIE augmentation strings
   1.608 +  //   beginning with 'z' to specify the presence of additional data after
   1.609 +  //   the CIE and FDE headers and special encodings used for addresses in
   1.610 +  //   frame description entries.
   1.611 +  //
   1.612 +  //   CallFrameInfo can handle 'z' augmentations in either DWARF CFI or
   1.613 +  //   exception handling data if you have supplied READER with the base
   1.614 +  //   addresses needed to interpret the pointer encodings that 'z'
   1.615 +  //   augmentations can specify. See the ByteReader interface for details
   1.616 +  //   about the base addresses. See the CallFrameInfo::Handler interface
   1.617 +  //   for details about the additional information one might find in
   1.618 +  //   'z'-augmented data.
   1.619 +  //
   1.620 +  // Thus:
   1.621 +  //
   1.622 +  // - If you are parsing standard DWARF CFI, as found in a .debug_frame
   1.623 +  //   section, you should pass false for the EH_FRAME argument, or omit
   1.624 +  //   it, and you need not worry about providing READER with the
   1.625 +  //   additional base addresses.
   1.626 +  //
   1.627 +  // - If you want to parse Linux C++ exception handling data from a
   1.628 +  //   .eh_frame section, you should pass EH_FRAME as true, and call
   1.629 +  //   READER's Set*Base member functions before calling our Start method.
   1.630 +  //
   1.631 +  // - If you want to parse DWARF CFI that uses the 'z' augmentations
   1.632 +  //   (although I don't think any toolchain ever emits such data), you
   1.633 +  //   could pass false for EH_FRAME, but call READER's Set*Base members.
   1.634 +  //
   1.635 +  // The extensions the Linux C++ ABI makes to DWARF for exception
   1.636 +  // handling are described here, rather poorly:
   1.637 +  // http://refspecs.linux-foundation.org/LSB_4.0.0/LSB-Core-generic/LSB-Core-generic/dwarfext.html
   1.638 +  // http://refspecs.linux-foundation.org/LSB_4.0.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html
   1.639 +  // 
   1.640 +  // The mechanics of C++ exception handling, personality routines,
   1.641 +  // and language-specific data areas are described here, rather nicely:
   1.642 +  // http://www.codesourcery.com/public/cxx-abi/abi-eh.html
   1.643 +  CallFrameInfo(const char *buffer, size_t buffer_length,
   1.644 +                ByteReader *reader, Handler *handler, Reporter *reporter,
   1.645 +                bool eh_frame = false)
   1.646 +      : buffer_(buffer), buffer_length_(buffer_length),
   1.647 +        reader_(reader), handler_(handler), reporter_(reporter),
   1.648 +        eh_frame_(eh_frame) { }
   1.649 +
   1.650 +  ~CallFrameInfo() { }
   1.651 +
   1.652 +  // Parse the entries in BUFFER, reporting what we find to HANDLER.
   1.653 +  // Return true if we reach the end of the section successfully, or
   1.654 +  // false if we encounter an error.
   1.655 +  bool Start();
   1.656 +
   1.657 +  // Return the textual name of KIND. For error reporting.
   1.658 +  static const char *KindName(EntryKind kind);
   1.659 +
   1.660 + private:
   1.661 +
   1.662 +  struct CIE;
   1.663 +
   1.664 +  // A CFI entry, either an FDE or a CIE.
   1.665 +  struct Entry {
   1.666 +    // The starting offset of the entry in the section, for error
   1.667 +    // reporting.
   1.668 +    size_t offset;
   1.669 +
   1.670 +    // The start of this entry in the buffer.
   1.671 +    const char *start;
   1.672 +    
   1.673 +    // Which kind of entry this is.
   1.674 +    //
   1.675 +    // We want to be able to use this for error reporting even while we're
   1.676 +    // in the midst of parsing. Error reporting code may assume that kind,
   1.677 +    // offset, and start fields are valid, although kind may be kUnknown.
   1.678 +    EntryKind kind;
   1.679 +
   1.680 +    // The end of this entry's common prologue (initial length and id), and
   1.681 +    // the start of this entry's kind-specific fields.
   1.682 +    const char *fields;
   1.683 +
   1.684 +    // The start of this entry's instructions.
   1.685 +    const char *instructions;
   1.686 +
   1.687 +    // The address past the entry's last byte in the buffer. (Note that
   1.688 +    // since offset points to the entry's initial length field, and the
   1.689 +    // length field is the number of bytes after that field, this is not
   1.690 +    // simply buffer_ + offset + length.)
   1.691 +    const char *end;
   1.692 +
   1.693 +    // For both DWARF CFI and .eh_frame sections, this is the CIE id in a
   1.694 +    // CIE, and the offset of the associated CIE in an FDE.
   1.695 +    uint64 id;
   1.696 +
   1.697 +    // The CIE that applies to this entry, if we've parsed it. If this is a
   1.698 +    // CIE, then this field points to this structure.
   1.699 +    CIE *cie;
   1.700 +  };
   1.701 +
   1.702 +  // A common information entry (CIE).
   1.703 +  struct CIE: public Entry {
   1.704 +    uint8 version;                      // CFI data version number
   1.705 +    string augmentation;                // vendor format extension markers
   1.706 +    uint64 code_alignment_factor;       // scale for code address adjustments 
   1.707 +    int data_alignment_factor;          // scale for stack pointer adjustments
   1.708 +    unsigned return_address_register;   // which register holds the return addr
   1.709 +
   1.710 +    // True if this CIE includes Linux C++ ABI 'z' augmentation data.
   1.711 +    bool has_z_augmentation;
   1.712 + 
   1.713 +    // Parsed 'z' augmentation data. These are meaningful only if
   1.714 +    // has_z_augmentation is true.
   1.715 +    bool has_z_lsda;                    // The 'z' augmentation included 'L'.
   1.716 +    bool has_z_personality;             // The 'z' augmentation included 'P'.
   1.717 +    bool has_z_signal_frame;            // The 'z' augmentation included 'S'.
   1.718 +
   1.719 +    // If has_z_lsda is true, this is the encoding to be used for language-
   1.720 +    // specific data area pointers in FDEs.
   1.721 +    DwarfPointerEncoding lsda_encoding;
   1.722 +
   1.723 +    // If has_z_personality is true, this is the encoding used for the
   1.724 +    // personality routine pointer in the augmentation data.
   1.725 +    DwarfPointerEncoding personality_encoding;
   1.726 +
   1.727 +    // If has_z_personality is true, this is the address of the personality
   1.728 +    // routine --- or, if personality_encoding & DW_EH_PE_indirect, the
   1.729 +    // address where the personality routine's address is stored.
   1.730 +    uint64 personality_address;
   1.731 +
   1.732 +    // This is the encoding used for addresses in the FDE header and
   1.733 +    // in DW_CFA_set_loc instructions. This is always valid, whether
   1.734 +    // or not we saw a 'z' augmentation string; its default value is
   1.735 +    // DW_EH_PE_absptr, which is what normal DWARF CFI uses.
   1.736 +    DwarfPointerEncoding pointer_encoding;
   1.737 +  };
   1.738 +
   1.739 +  // A frame description entry (FDE).
   1.740 +  struct FDE: public Entry {
   1.741 +    uint64 address;                     // start address of described code
   1.742 +    uint64 size;                        // size of described code, in bytes
   1.743 +
   1.744 +    // If cie->has_z_lsda is true, then this is the language-specific data
   1.745 +    // area's address --- or its address's address, if cie->lsda_encoding
   1.746 +    // has the DW_EH_PE_indirect bit set.
   1.747 +    uint64 lsda_address;
   1.748 +  };
   1.749 +
   1.750 +  // Internal use.
   1.751 +  class Rule;
   1.752 +  class UndefinedRule;
   1.753 +  class SameValueRule;
   1.754 +  class OffsetRule;
   1.755 +  class ValOffsetRule;
   1.756 +  class RegisterRule;
   1.757 +  class ExpressionRule;
   1.758 +  class ValExpressionRule;
   1.759 +  class RuleMap;
   1.760 +  class State;
   1.761 +  
   1.762 +  // Parse the initial length and id of a CFI entry, either a CIE, an FDE,
   1.763 +  // or a .eh_frame end-of-data mark. CURSOR points to the beginning of the
   1.764 +  // data to parse. On success, populate ENTRY as appropriate, and return
   1.765 +  // true. On failure, report the problem, and return false. Even if we
   1.766 +  // return false, set ENTRY->end to the first byte after the entry if we
   1.767 +  // were able to figure that out, or NULL if we weren't.
   1.768 +  bool ReadEntryPrologue(const char *cursor, Entry *entry);
   1.769 +
   1.770 +  // Parse the fields of a CIE after the entry prologue, including any 'z'
   1.771 +  // augmentation data. Assume that the 'Entry' fields of CIE are
   1.772 +  // populated; use CIE->fields and CIE->end as the start and limit for
   1.773 +  // parsing. On success, populate the rest of *CIE, and return true; on
   1.774 +  // failure, report the problem and return false.
   1.775 +  bool ReadCIEFields(CIE *cie);
   1.776 +
   1.777 +  // Parse the fields of an FDE after the entry prologue, including any 'z'
   1.778 +  // augmentation data. Assume that the 'Entry' fields of *FDE are
   1.779 +  // initialized; use FDE->fields and FDE->end as the start and limit for
   1.780 +  // parsing. Assume that FDE->cie is fully initialized. On success,
   1.781 +  // populate the rest of *FDE, and return true; on failure, report the
   1.782 +  // problem and return false.
   1.783 +  bool ReadFDEFields(FDE *fde);
   1.784 +
   1.785 +  // Report that ENTRY is incomplete, and return false. This is just a
   1.786 +  // trivial wrapper for invoking reporter_->Incomplete; it provides a
   1.787 +  // little brevity.
   1.788 +  bool ReportIncomplete(Entry *entry);
   1.789 +
   1.790 +  // Return true if ENCODING has the DW_EH_PE_indirect bit set.
   1.791 +  static bool IsIndirectEncoding(DwarfPointerEncoding encoding) {
   1.792 +    return encoding & DW_EH_PE_indirect;
   1.793 +  }
   1.794 +
   1.795 +  // The contents of the DWARF .debug_info section we're parsing.
   1.796 +  const char *buffer_;
   1.797 +  size_t buffer_length_;
   1.798 +
   1.799 +  // For reading multi-byte values with the appropriate endianness.
   1.800 +  ByteReader *reader_;
   1.801 +
   1.802 +  // The handler to which we should report the data we find.
   1.803 +  Handler *handler_;
   1.804 +
   1.805 +  // For reporting problems in the info we're parsing.
   1.806 +  Reporter *reporter_;
   1.807 +
   1.808 +  // True if we are processing .eh_frame-format data.
   1.809 +  bool eh_frame_;
   1.810 +};
   1.811 +
   1.812 +// The handler class for CallFrameInfo.  The a CFI parser calls the
   1.813 +// member functions of a handler object to report the data it finds.
   1.814 +class CallFrameInfo::Handler {
   1.815 + public:
   1.816 +  // The pseudo-register number for the canonical frame address.
   1.817 +  enum { kCFARegister = -1 };
   1.818 +
   1.819 +  Handler() { }
   1.820 +  virtual ~Handler() { }
   1.821 +
   1.822 +  // The parser has found CFI for the machine code at ADDRESS,
   1.823 +  // extending for LENGTH bytes. OFFSET is the offset of the frame
   1.824 +  // description entry in the section, for use in error messages.
   1.825 +  // VERSION is the version number of the CFI format. AUGMENTATION is
   1.826 +  // a string describing any producer-specific extensions present in
   1.827 +  // the data. RETURN_ADDRESS is the number of the register that holds
   1.828 +  // the address to which the function should return.
   1.829 +  //
   1.830 +  // Entry should return true to process this CFI, or false to skip to
   1.831 +  // the next entry.
   1.832 +  //
   1.833 +  // The parser invokes Entry for each Frame Description Entry (FDE)
   1.834 +  // it finds.  The parser doesn't report Common Information Entries
   1.835 +  // to the handler explicitly; instead, if the handler elects to
   1.836 +  // process a given FDE, the parser reiterates the appropriate CIE's
   1.837 +  // contents at the beginning of the FDE's rules.
   1.838 +  virtual bool Entry(size_t offset, uint64 address, uint64 length,
   1.839 +                     uint8 version, const string &augmentation,
   1.840 +                     unsigned return_address) = 0;
   1.841 +
   1.842 +  // When the Entry function returns true, the parser calls these
   1.843 +  // handler functions repeatedly to describe the rules for recovering
   1.844 +  // registers at each instruction in the given range of machine code.
   1.845 +  // Immediately after a call to Entry, the handler should assume that
   1.846 +  // the rule for each callee-saves register is "unchanged" --- that
   1.847 +  // is, that the register still has the value it had in the caller.
   1.848 +  // 
   1.849 +  // If a *Rule function returns true, we continue processing this entry's
   1.850 +  // instructions. If a *Rule function returns false, we stop evaluating
   1.851 +  // instructions, and skip to the next entry. Either way, we call End
   1.852 +  // before going on to the next entry.
   1.853 +  //
   1.854 +  // In all of these functions, if the REG parameter is kCFARegister, then
   1.855 +  // the rule describes how to find the canonical frame address.
   1.856 +  // kCFARegister may be passed as a BASE_REGISTER argument, meaning that
   1.857 +  // the canonical frame address should be used as the base address for the
   1.858 +  // computation. All other REG values will be positive.
   1.859 +
   1.860 +  // At ADDRESS, register REG's value is not recoverable.
   1.861 +  virtual bool UndefinedRule(uint64 address, int reg) = 0;
   1.862 +
   1.863 +  // At ADDRESS, register REG's value is the same as that it had in
   1.864 +  // the caller.
   1.865 +  virtual bool SameValueRule(uint64 address, int reg) = 0;
   1.866 +
   1.867 +  // At ADDRESS, register REG has been saved at offset OFFSET from
   1.868 +  // BASE_REGISTER.
   1.869 +  virtual bool OffsetRule(uint64 address, int reg,
   1.870 +                          int base_register, long offset) = 0;
   1.871 +
   1.872 +  // At ADDRESS, the caller's value of register REG is the current
   1.873 +  // value of BASE_REGISTER plus OFFSET. (This rule doesn't provide an
   1.874 +  // address at which the register's value is saved.)
   1.875 +  virtual bool ValOffsetRule(uint64 address, int reg,
   1.876 +                             int base_register, long offset) = 0;
   1.877 +
   1.878 +  // At ADDRESS, register REG has been saved in BASE_REGISTER. This differs
   1.879 +  // from ValOffsetRule(ADDRESS, REG, BASE_REGISTER, 0), in that
   1.880 +  // BASE_REGISTER is the "home" for REG's saved value: if you want to
   1.881 +  // assign to a variable whose home is REG in the calling frame, you
   1.882 +  // should put the value in BASE_REGISTER.
   1.883 +  virtual bool RegisterRule(uint64 address, int reg, int base_register) = 0;
   1.884 +
   1.885 +  // At ADDRESS, the DWARF expression EXPRESSION yields the address at
   1.886 +  // which REG was saved.
   1.887 +  virtual bool ExpressionRule(uint64 address, int reg,
   1.888 +                              const string &expression) = 0;
   1.889 +
   1.890 +  // At ADDRESS, the DWARF expression EXPRESSION yields the caller's
   1.891 +  // value for REG. (This rule doesn't provide an address at which the
   1.892 +  // register's value is saved.)
   1.893 +  virtual bool ValExpressionRule(uint64 address, int reg,
   1.894 +                                 const string &expression) = 0;
   1.895 +
   1.896 +  // Indicate that the rules for the address range reported by the
   1.897 +  // last call to Entry are complete.  End should return true if
   1.898 +  // everything is okay, or false if an error has occurred and parsing
   1.899 +  // should stop.
   1.900 +  virtual bool End() = 0;
   1.901 +
   1.902 +  // Handler functions for Linux C++ exception handling data. These are
   1.903 +  // only called if the data includes 'z' augmentation strings.
   1.904 +
   1.905 +  // The Linux C++ ABI uses an extension of the DWARF CFI format to
   1.906 +  // walk the stack to propagate exceptions from the throw to the
   1.907 +  // appropriate catch, and do the appropriate cleanups along the way.
   1.908 +  // CFI entries used for exception handling have two additional data
   1.909 +  // associated with them:
   1.910 +  //
   1.911 +  // - The "language-specific data area" describes which exception
   1.912 +  //   types the function has 'catch' clauses for, and indicates how
   1.913 +  //   to go about re-entering the function at the appropriate catch
   1.914 +  //   clause. If the exception is not caught, it describes the
   1.915 +  //   destructors that must run before the frame is popped.
   1.916 +  //
   1.917 +  // - The "personality routine" is responsible for interpreting the
   1.918 +  //   language-specific data area's contents, and deciding whether
   1.919 +  //   the exception should continue to propagate down the stack,
   1.920 +  //   perhaps after doing some cleanup for this frame, or whether the
   1.921 +  //   exception will be caught here.
   1.922 +  //
   1.923 +  // In principle, the language-specific data area is opaque to
   1.924 +  // everybody but the personality routine. In practice, these values
   1.925 +  // may be useful or interesting to readers with extra context, and
   1.926 +  // we have to at least skip them anyway, so we might as well report
   1.927 +  // them to the handler.
   1.928 +
   1.929 +  // This entry's exception handling personality routine's address is
   1.930 +  // ADDRESS. If INDIRECT is true, then ADDRESS is the address at
   1.931 +  // which the routine's address is stored. The default definition for
   1.932 +  // this handler function simply returns true, allowing parsing of
   1.933 +  // the entry to continue.
   1.934 +  virtual bool PersonalityRoutine(uint64 address, bool indirect) {
   1.935 +    return true;
   1.936 +  }
   1.937 +
   1.938 +  // This entry's language-specific data area (LSDA) is located at
   1.939 +  // ADDRESS. If INDIRECT is true, then ADDRESS is the address at
   1.940 +  // which the area's address is stored. The default definition for
   1.941 +  // this handler function simply returns true, allowing parsing of
   1.942 +  // the entry to continue.
   1.943 +  virtual bool LanguageSpecificDataArea(uint64 address, bool indirect) {
   1.944 +    return true;
   1.945 +  }
   1.946 +
   1.947 +  // This entry describes a signal trampoline --- this frame is the
   1.948 +  // caller of a signal handler. The default definition for this
   1.949 +  // handler function simply returns true, allowing parsing of the
   1.950 +  // entry to continue.
   1.951 +  //
   1.952 +  // The best description of the rationale for and meaning of signal
   1.953 +  // trampoline CFI entries seems to be in the GCC bug database:
   1.954 +  // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=26208
   1.955 +  virtual bool SignalHandler() { return true; }
   1.956 +};
   1.957 +
   1.958 +// The CallFrameInfo class makes calls on an instance of this class to
   1.959 +// report errors or warn about problems in the data it is parsing. The
   1.960 +// default definitions of these methods print a message to stderr, but
   1.961 +// you can make a derived class that overrides them.
   1.962 +class CallFrameInfo::Reporter {
   1.963 + public:
   1.964 +  // Create an error reporter which attributes troubles to the section
   1.965 +  // named SECTION in FILENAME.
   1.966 +  //
   1.967 +  // Normally SECTION would be .debug_frame, but the Mac puts CFI data
   1.968 +  // in a Mach-O section named __debug_frame. If we support
   1.969 +  // Linux-style exception handling data, we could be reading an
   1.970 +  // .eh_frame section.
   1.971 +  Reporter(const string &filename,
   1.972 +           const string &section = ".debug_frame")
   1.973 +      : filename_(filename), section_(section) { }
   1.974 +  virtual ~Reporter() { }
   1.975 +
   1.976 +  // The CFI entry at OFFSET ends too early to be well-formed. KIND
   1.977 +  // indicates what kind of entry it is; KIND can be kUnknown if we
   1.978 +  // haven't parsed enough of the entry to tell yet.
   1.979 +  virtual void Incomplete(uint64 offset, CallFrameInfo::EntryKind kind);
   1.980 +
   1.981 +  // The .eh_frame data has a four-byte zero at OFFSET where the next
   1.982 +  // entry's length would be; this is a terminator. However, the buffer
   1.983 +  // length as given to the CallFrameInfo constructor says there should be
   1.984 +  // more data.
   1.985 +  virtual void EarlyEHTerminator(uint64 offset);
   1.986 +
   1.987 +  // The FDE at OFFSET refers to the CIE at CIE_OFFSET, but the
   1.988 +  // section is not that large.
   1.989 +  virtual void CIEPointerOutOfRange(uint64 offset, uint64 cie_offset);
   1.990 +
   1.991 +  // The FDE at OFFSET refers to the CIE at CIE_OFFSET, but the entry
   1.992 +  // there is not a CIE.
   1.993 +  virtual void BadCIEId(uint64 offset, uint64 cie_offset);
   1.994 +
   1.995 +  // The FDE at OFFSET refers to a CIE with version number VERSION,
   1.996 +  // which we don't recognize. We cannot parse DWARF CFI if it uses
   1.997 +  // a version number we don't recognize.
   1.998 +  virtual void UnrecognizedVersion(uint64 offset, int version);
   1.999 +
  1.1000 +  // The FDE at OFFSET refers to a CIE with augmentation AUGMENTATION,
  1.1001 +  // which we don't recognize. We cannot parse DWARF CFI if it uses
  1.1002 +  // augmentations we don't recognize.
  1.1003 +  virtual void UnrecognizedAugmentation(uint64 offset,
  1.1004 +                                        const string &augmentation);
  1.1005 +
  1.1006 +  // The pointer encoding ENCODING, specified by the CIE at OFFSET, is not
  1.1007 +  // a valid encoding.
  1.1008 +  virtual void InvalidPointerEncoding(uint64 offset, uint8 encoding);
  1.1009 +
  1.1010 +  // The pointer encoding ENCODING, specified by the CIE at OFFSET, depends
  1.1011 +  // on a base address which has not been supplied.
  1.1012 +  virtual void UnusablePointerEncoding(uint64 offset, uint8 encoding);
  1.1013 +
  1.1014 +  // The CIE at OFFSET contains a DW_CFA_restore instruction at
  1.1015 +  // INSN_OFFSET, which may not appear in a CIE.
  1.1016 +  virtual void RestoreInCIE(uint64 offset, uint64 insn_offset);
  1.1017 +
  1.1018 +  // The entry at OFFSET, of kind KIND, has an unrecognized
  1.1019 +  // instruction at INSN_OFFSET.
  1.1020 +  virtual void BadInstruction(uint64 offset, CallFrameInfo::EntryKind kind,
  1.1021 +                              uint64 insn_offset);
  1.1022 +
  1.1023 +  // The instruction at INSN_OFFSET in the entry at OFFSET, of kind
  1.1024 +  // KIND, establishes a rule that cites the CFA, but we have not
  1.1025 +  // established a CFA rule yet.
  1.1026 +  virtual void NoCFARule(uint64 offset, CallFrameInfo::EntryKind kind, 
  1.1027 +                         uint64 insn_offset);
  1.1028 +
  1.1029 +  // The instruction at INSN_OFFSET in the entry at OFFSET, of kind
  1.1030 +  // KIND, is a DW_CFA_restore_state instruction, but the stack of
  1.1031 +  // saved states is empty.
  1.1032 +  virtual void EmptyStateStack(uint64 offset, CallFrameInfo::EntryKind kind, 
  1.1033 +                               uint64 insn_offset);
  1.1034 +
  1.1035 +  // The DW_CFA_remember_state instruction at INSN_OFFSET in the entry
  1.1036 +  // at OFFSET, of kind KIND, would restore a state that has no CFA
  1.1037 +  // rule, whereas the current state does have a CFA rule. This is
  1.1038 +  // bogus input, which the CallFrameInfo::Handler interface doesn't
  1.1039 +  // (and shouldn't) have any way to report.
  1.1040 +  virtual void ClearingCFARule(uint64 offset, CallFrameInfo::EntryKind kind, 
  1.1041 +                               uint64 insn_offset);
  1.1042 +
  1.1043 + protected:
  1.1044 +  // The name of the file whose CFI we're reading.
  1.1045 +  string filename_;
  1.1046 +
  1.1047 +  // The name of the CFI section in that file.
  1.1048 +  string section_;
  1.1049 +};
  1.1050 +
  1.1051 +}  // namespace dwarf2reader
  1.1052 +
  1.1053 +#endif  // UTIL_DEBUGINFO_DWARF2READER_H__

mercurial