1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/crashreporter/google-breakpad/src/common/dwarf/dwarf2reader.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,2351 @@ 1.4 +// Copyright (c) 2010 Google Inc. All Rights Reserved. 1.5 +// 1.6 +// Redistribution and use in source and binary forms, with or without 1.7 +// modification, are permitted provided that the following conditions are 1.8 +// met: 1.9 +// 1.10 +// * Redistributions of source code must retain the above copyright 1.11 +// notice, this list of conditions and the following disclaimer. 1.12 +// * Redistributions in binary form must reproduce the above 1.13 +// copyright notice, this list of conditions and the following disclaimer 1.14 +// in the documentation and/or other materials provided with the 1.15 +// distribution. 1.16 +// * Neither the name of Google Inc. nor the names of its 1.17 +// contributors may be used to endorse or promote products derived from 1.18 +// this software without specific prior written permission. 1.19 +// 1.20 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.21 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.22 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.23 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.24 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.25 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.26 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.27 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.28 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.29 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.30 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.31 + 1.32 +// CFI reader author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com> 1.33 + 1.34 +// Implementation of dwarf2reader::LineInfo, dwarf2reader::CompilationUnit, 1.35 +// and dwarf2reader::CallFrameInfo. See dwarf2reader.h for details. 1.36 + 1.37 +#include "common/dwarf/dwarf2reader.h" 1.38 + 1.39 +#include <assert.h> 1.40 +#include <stdint.h> 1.41 +#include <stdio.h> 1.42 +#include <string.h> 1.43 + 1.44 +#include <map> 1.45 +#include <memory> 1.46 +#include <stack> 1.47 +#include <string> 1.48 +#include <utility> 1.49 + 1.50 +#include "common/dwarf/bytereader-inl.h" 1.51 +#include "common/dwarf/bytereader.h" 1.52 +#include "common/dwarf/line_state_machine.h" 1.53 +#include "common/using_std_string.h" 1.54 + 1.55 +namespace dwarf2reader { 1.56 + 1.57 +CompilationUnit::CompilationUnit(const SectionMap& sections, uint64 offset, 1.58 + ByteReader* reader, Dwarf2Handler* handler) 1.59 + : offset_from_section_start_(offset), reader_(reader), 1.60 + sections_(sections), handler_(handler), abbrevs_(NULL), 1.61 + string_buffer_(NULL), string_buffer_length_(0) {} 1.62 + 1.63 +// Read a DWARF2/3 abbreviation section. 1.64 +// Each abbrev consists of a abbreviation number, a tag, a byte 1.65 +// specifying whether the tag has children, and a list of 1.66 +// attribute/form pairs. 1.67 +// The list of forms is terminated by a 0 for the attribute, and a 1.68 +// zero for the form. The entire abbreviation section is terminated 1.69 +// by a zero for the code. 1.70 + 1.71 +void CompilationUnit::ReadAbbrevs() { 1.72 + if (abbrevs_) 1.73 + return; 1.74 + 1.75 + // First get the debug_abbrev section. ".debug_abbrev" is the name 1.76 + // recommended in the DWARF spec, and used on Linux; 1.77 + // "__debug_abbrev" is the name used in Mac OS X Mach-O files. 1.78 + SectionMap::const_iterator iter = sections_.find(".debug_abbrev"); 1.79 + if (iter == sections_.end()) 1.80 + iter = sections_.find("__debug_abbrev"); 1.81 + assert(iter != sections_.end()); 1.82 + 1.83 + abbrevs_ = new std::vector<Abbrev>; 1.84 + abbrevs_->resize(1); 1.85 + 1.86 + // The only way to check whether we are reading over the end of the 1.87 + // buffer would be to first compute the size of the leb128 data by 1.88 + // reading it, then go back and read it again. 1.89 + const char* abbrev_start = iter->second.first + 1.90 + header_.abbrev_offset; 1.91 + const char* abbrevptr = abbrev_start; 1.92 +#ifndef NDEBUG 1.93 + const uint64 abbrev_length = iter->second.second - header_.abbrev_offset; 1.94 +#endif 1.95 + 1.96 + while (1) { 1.97 + CompilationUnit::Abbrev abbrev; 1.98 + size_t len; 1.99 + const uint64 number = reader_->ReadUnsignedLEB128(abbrevptr, &len); 1.100 + 1.101 + if (number == 0) 1.102 + break; 1.103 + abbrev.number = number; 1.104 + abbrevptr += len; 1.105 + 1.106 + assert(abbrevptr < abbrev_start + abbrev_length); 1.107 + const uint64 tag = reader_->ReadUnsignedLEB128(abbrevptr, &len); 1.108 + abbrevptr += len; 1.109 + abbrev.tag = static_cast<enum DwarfTag>(tag); 1.110 + 1.111 + assert(abbrevptr < abbrev_start + abbrev_length); 1.112 + abbrev.has_children = reader_->ReadOneByte(abbrevptr); 1.113 + abbrevptr += 1; 1.114 + 1.115 + assert(abbrevptr < abbrev_start + abbrev_length); 1.116 + 1.117 + while (1) { 1.118 + const uint64 nametemp = reader_->ReadUnsignedLEB128(abbrevptr, &len); 1.119 + abbrevptr += len; 1.120 + 1.121 + assert(abbrevptr < abbrev_start + abbrev_length); 1.122 + const uint64 formtemp = reader_->ReadUnsignedLEB128(abbrevptr, &len); 1.123 + abbrevptr += len; 1.124 + if (nametemp == 0 && formtemp == 0) 1.125 + break; 1.126 + 1.127 + const enum DwarfAttribute name = 1.128 + static_cast<enum DwarfAttribute>(nametemp); 1.129 + const enum DwarfForm form = static_cast<enum DwarfForm>(formtemp); 1.130 + abbrev.attributes.push_back(std::make_pair(name, form)); 1.131 + } 1.132 + assert(abbrev.number == abbrevs_->size()); 1.133 + abbrevs_->push_back(abbrev); 1.134 + } 1.135 +} 1.136 + 1.137 +// Skips a single DIE's attributes. 1.138 +const char* CompilationUnit::SkipDIE(const char* start, 1.139 + const Abbrev& abbrev) { 1.140 + for (AttributeList::const_iterator i = abbrev.attributes.begin(); 1.141 + i != abbrev.attributes.end(); 1.142 + i++) { 1.143 + start = SkipAttribute(start, i->second); 1.144 + } 1.145 + return start; 1.146 +} 1.147 + 1.148 +// Skips a single attribute form's data. 1.149 +const char* CompilationUnit::SkipAttribute(const char* start, 1.150 + enum DwarfForm form) { 1.151 + size_t len; 1.152 + 1.153 + switch (form) { 1.154 + case DW_FORM_indirect: 1.155 + form = static_cast<enum DwarfForm>(reader_->ReadUnsignedLEB128(start, 1.156 + &len)); 1.157 + start += len; 1.158 + return SkipAttribute(start, form); 1.159 + 1.160 + case DW_FORM_flag_present: 1.161 + return start; 1.162 + case DW_FORM_data1: 1.163 + case DW_FORM_flag: 1.164 + case DW_FORM_ref1: 1.165 + return start + 1; 1.166 + case DW_FORM_ref2: 1.167 + case DW_FORM_data2: 1.168 + return start + 2; 1.169 + case DW_FORM_ref4: 1.170 + case DW_FORM_data4: 1.171 + return start + 4; 1.172 + case DW_FORM_ref8: 1.173 + case DW_FORM_data8: 1.174 + case DW_FORM_ref_sig8: 1.175 + return start + 8; 1.176 + case DW_FORM_string: 1.177 + return start + strlen(start) + 1; 1.178 + case DW_FORM_udata: 1.179 + case DW_FORM_ref_udata: 1.180 + reader_->ReadUnsignedLEB128(start, &len); 1.181 + return start + len; 1.182 + 1.183 + case DW_FORM_sdata: 1.184 + reader_->ReadSignedLEB128(start, &len); 1.185 + return start + len; 1.186 + case DW_FORM_addr: 1.187 + return start + reader_->AddressSize(); 1.188 + case DW_FORM_ref_addr: 1.189 + // DWARF2 and 3 differ on whether ref_addr is address size or 1.190 + // offset size. 1.191 + assert(header_.version == 2 || header_.version == 3); 1.192 + if (header_.version == 2) { 1.193 + return start + reader_->AddressSize(); 1.194 + } else if (header_.version == 3) { 1.195 + return start + reader_->OffsetSize(); 1.196 + } 1.197 + 1.198 + case DW_FORM_block1: 1.199 + return start + 1 + reader_->ReadOneByte(start); 1.200 + case DW_FORM_block2: 1.201 + return start + 2 + reader_->ReadTwoBytes(start); 1.202 + case DW_FORM_block4: 1.203 + return start + 4 + reader_->ReadFourBytes(start); 1.204 + case DW_FORM_block: 1.205 + case DW_FORM_exprloc: { 1.206 + uint64 size = reader_->ReadUnsignedLEB128(start, &len); 1.207 + return start + size + len; 1.208 + } 1.209 + case DW_FORM_strp: 1.210 + case DW_FORM_sec_offset: 1.211 + return start + reader_->OffsetSize(); 1.212 + } 1.213 + fprintf(stderr,"Unhandled form type"); 1.214 + return NULL; 1.215 +} 1.216 + 1.217 +// Read a DWARF2/3 header. 1.218 +// The header is variable length in DWARF3 (and DWARF2 as extended by 1.219 +// most compilers), and consists of an length field, a version number, 1.220 +// the offset in the .debug_abbrev section for our abbrevs, and an 1.221 +// address size. 1.222 +void CompilationUnit::ReadHeader() { 1.223 + const char* headerptr = buffer_; 1.224 + size_t initial_length_size; 1.225 + 1.226 + assert(headerptr + 4 < buffer_ + buffer_length_); 1.227 + const uint64 initial_length 1.228 + = reader_->ReadInitialLength(headerptr, &initial_length_size); 1.229 + headerptr += initial_length_size; 1.230 + header_.length = initial_length; 1.231 + 1.232 + assert(headerptr + 2 < buffer_ + buffer_length_); 1.233 + header_.version = reader_->ReadTwoBytes(headerptr); 1.234 + headerptr += 2; 1.235 + 1.236 + assert(headerptr + reader_->OffsetSize() < buffer_ + buffer_length_); 1.237 + header_.abbrev_offset = reader_->ReadOffset(headerptr); 1.238 + headerptr += reader_->OffsetSize(); 1.239 + 1.240 + assert(headerptr + 1 < buffer_ + buffer_length_); 1.241 + header_.address_size = reader_->ReadOneByte(headerptr); 1.242 + reader_->SetAddressSize(header_.address_size); 1.243 + headerptr += 1; 1.244 + 1.245 + after_header_ = headerptr; 1.246 + 1.247 + // This check ensures that we don't have to do checking during the 1.248 + // reading of DIEs. header_.length does not include the size of the 1.249 + // initial length. 1.250 + assert(buffer_ + initial_length_size + header_.length <= 1.251 + buffer_ + buffer_length_); 1.252 +} 1.253 + 1.254 +uint64 CompilationUnit::Start() { 1.255 + // First get the debug_info section. ".debug_info" is the name 1.256 + // recommended in the DWARF spec, and used on Linux; "__debug_info" 1.257 + // is the name used in Mac OS X Mach-O files. 1.258 + SectionMap::const_iterator iter = sections_.find(".debug_info"); 1.259 + if (iter == sections_.end()) 1.260 + iter = sections_.find("__debug_info"); 1.261 + assert(iter != sections_.end()); 1.262 + 1.263 + // Set up our buffer 1.264 + buffer_ = iter->second.first + offset_from_section_start_; 1.265 + buffer_length_ = iter->second.second - offset_from_section_start_; 1.266 + 1.267 + // Read the header 1.268 + ReadHeader(); 1.269 + 1.270 + // Figure out the real length from the end of the initial length to 1.271 + // the end of the compilation unit, since that is the value we 1.272 + // return. 1.273 + uint64 ourlength = header_.length; 1.274 + if (reader_->OffsetSize() == 8) 1.275 + ourlength += 12; 1.276 + else 1.277 + ourlength += 4; 1.278 + 1.279 + // See if the user wants this compilation unit, and if not, just return. 1.280 + if (!handler_->StartCompilationUnit(offset_from_section_start_, 1.281 + reader_->AddressSize(), 1.282 + reader_->OffsetSize(), 1.283 + header_.length, 1.284 + header_.version)) 1.285 + return ourlength; 1.286 + 1.287 + // Otherwise, continue by reading our abbreviation entries. 1.288 + ReadAbbrevs(); 1.289 + 1.290 + // Set the string section if we have one. ".debug_str" is the name 1.291 + // recommended in the DWARF spec, and used on Linux; "__debug_str" 1.292 + // is the name used in Mac OS X Mach-O files. 1.293 + iter = sections_.find(".debug_str"); 1.294 + if (iter == sections_.end()) 1.295 + iter = sections_.find("__debug_str"); 1.296 + if (iter != sections_.end()) { 1.297 + string_buffer_ = iter->second.first; 1.298 + string_buffer_length_ = iter->second.second; 1.299 + } 1.300 + 1.301 + // Now that we have our abbreviations, start processing DIE's. 1.302 + ProcessDIEs(); 1.303 + 1.304 + return ourlength; 1.305 +} 1.306 + 1.307 +// If one really wanted, you could merge SkipAttribute and 1.308 +// ProcessAttribute 1.309 +// This is all boring data manipulation and calling of the handler. 1.310 +const char* CompilationUnit::ProcessAttribute( 1.311 + uint64 dieoffset, const char* start, enum DwarfAttribute attr, 1.312 + enum DwarfForm form) { 1.313 + size_t len; 1.314 + 1.315 + switch (form) { 1.316 + // DW_FORM_indirect is never used because it is such a space 1.317 + // waster. 1.318 + case DW_FORM_indirect: 1.319 + form = static_cast<enum DwarfForm>(reader_->ReadUnsignedLEB128(start, 1.320 + &len)); 1.321 + start += len; 1.322 + return ProcessAttribute(dieoffset, start, attr, form); 1.323 + 1.324 + case DW_FORM_flag_present: 1.325 + handler_->ProcessAttributeUnsigned(dieoffset, attr, form, 1); 1.326 + return start; 1.327 + case DW_FORM_data1: 1.328 + case DW_FORM_flag: 1.329 + handler_->ProcessAttributeUnsigned(dieoffset, attr, form, 1.330 + reader_->ReadOneByte(start)); 1.331 + return start + 1; 1.332 + case DW_FORM_data2: 1.333 + handler_->ProcessAttributeUnsigned(dieoffset, attr, form, 1.334 + reader_->ReadTwoBytes(start)); 1.335 + return start + 2; 1.336 + case DW_FORM_data4: 1.337 + handler_->ProcessAttributeUnsigned(dieoffset, attr, form, 1.338 + reader_->ReadFourBytes(start)); 1.339 + return start + 4; 1.340 + case DW_FORM_data8: 1.341 + handler_->ProcessAttributeUnsigned(dieoffset, attr, form, 1.342 + reader_->ReadEightBytes(start)); 1.343 + return start + 8; 1.344 + case DW_FORM_string: { 1.345 + const char* str = start; 1.346 + handler_->ProcessAttributeString(dieoffset, attr, form, 1.347 + str); 1.348 + return start + strlen(str) + 1; 1.349 + } 1.350 + case DW_FORM_udata: 1.351 + handler_->ProcessAttributeUnsigned(dieoffset, attr, form, 1.352 + reader_->ReadUnsignedLEB128(start, 1.353 + &len)); 1.354 + return start + len; 1.355 + 1.356 + case DW_FORM_sdata: 1.357 + handler_->ProcessAttributeSigned(dieoffset, attr, form, 1.358 + reader_->ReadSignedLEB128(start, &len)); 1.359 + return start + len; 1.360 + case DW_FORM_addr: 1.361 + handler_->ProcessAttributeUnsigned(dieoffset, attr, form, 1.362 + reader_->ReadAddress(start)); 1.363 + return start + reader_->AddressSize(); 1.364 + case DW_FORM_sec_offset: 1.365 + handler_->ProcessAttributeUnsigned(dieoffset, attr, form, 1.366 + reader_->ReadOffset(start)); 1.367 + return start + reader_->OffsetSize(); 1.368 + 1.369 + case DW_FORM_ref1: 1.370 + handler_->ProcessAttributeReference(dieoffset, attr, form, 1.371 + reader_->ReadOneByte(start) 1.372 + + offset_from_section_start_); 1.373 + return start + 1; 1.374 + case DW_FORM_ref2: 1.375 + handler_->ProcessAttributeReference(dieoffset, attr, form, 1.376 + reader_->ReadTwoBytes(start) 1.377 + + offset_from_section_start_); 1.378 + return start + 2; 1.379 + case DW_FORM_ref4: 1.380 + handler_->ProcessAttributeReference(dieoffset, attr, form, 1.381 + reader_->ReadFourBytes(start) 1.382 + + offset_from_section_start_); 1.383 + return start + 4; 1.384 + case DW_FORM_ref8: 1.385 + handler_->ProcessAttributeReference(dieoffset, attr, form, 1.386 + reader_->ReadEightBytes(start) 1.387 + + offset_from_section_start_); 1.388 + return start + 8; 1.389 + case DW_FORM_ref_udata: 1.390 + handler_->ProcessAttributeReference(dieoffset, attr, form, 1.391 + reader_->ReadUnsignedLEB128(start, 1.392 + &len) 1.393 + + offset_from_section_start_); 1.394 + return start + len; 1.395 + case DW_FORM_ref_addr: 1.396 + // DWARF2 and 3 differ on whether ref_addr is address size or 1.397 + // offset size. 1.398 + assert(header_.version == 2 || header_.version == 3); 1.399 + if (header_.version == 2) { 1.400 + handler_->ProcessAttributeReference(dieoffset, attr, form, 1.401 + reader_->ReadAddress(start)); 1.402 + return start + reader_->AddressSize(); 1.403 + } else if (header_.version == 3) { 1.404 + handler_->ProcessAttributeReference(dieoffset, attr, form, 1.405 + reader_->ReadOffset(start)); 1.406 + return start + reader_->OffsetSize(); 1.407 + } 1.408 + break; 1.409 + case DW_FORM_ref_sig8: 1.410 + handler_->ProcessAttributeSignature(dieoffset, attr, form, 1.411 + reader_->ReadEightBytes(start)); 1.412 + return start + 8; 1.413 + 1.414 + case DW_FORM_block1: { 1.415 + uint64 datalen = reader_->ReadOneByte(start); 1.416 + handler_->ProcessAttributeBuffer(dieoffset, attr, form, start + 1, 1.417 + datalen); 1.418 + return start + 1 + datalen; 1.419 + } 1.420 + case DW_FORM_block2: { 1.421 + uint64 datalen = reader_->ReadTwoBytes(start); 1.422 + handler_->ProcessAttributeBuffer(dieoffset, attr, form, start + 2, 1.423 + datalen); 1.424 + return start + 2 + datalen; 1.425 + } 1.426 + case DW_FORM_block4: { 1.427 + uint64 datalen = reader_->ReadFourBytes(start); 1.428 + handler_->ProcessAttributeBuffer(dieoffset, attr, form, start + 4, 1.429 + datalen); 1.430 + return start + 4 + datalen; 1.431 + } 1.432 + case DW_FORM_block: 1.433 + case DW_FORM_exprloc: { 1.434 + uint64 datalen = reader_->ReadUnsignedLEB128(start, &len); 1.435 + handler_->ProcessAttributeBuffer(dieoffset, attr, form, start + len, 1.436 + datalen); 1.437 + return start + datalen + len; 1.438 + } 1.439 + case DW_FORM_strp: { 1.440 + assert(string_buffer_ != NULL); 1.441 + 1.442 + const uint64 offset = reader_->ReadOffset(start); 1.443 + assert(string_buffer_ + offset < string_buffer_ + string_buffer_length_); 1.444 + 1.445 + const char* str = string_buffer_ + offset; 1.446 + handler_->ProcessAttributeString(dieoffset, attr, form, 1.447 + str); 1.448 + return start + reader_->OffsetSize(); 1.449 + } 1.450 + } 1.451 + fprintf(stderr, "Unhandled form type\n"); 1.452 + return NULL; 1.453 +} 1.454 + 1.455 +const char* CompilationUnit::ProcessDIE(uint64 dieoffset, 1.456 + const char* start, 1.457 + const Abbrev& abbrev) { 1.458 + for (AttributeList::const_iterator i = abbrev.attributes.begin(); 1.459 + i != abbrev.attributes.end(); 1.460 + i++) { 1.461 + start = ProcessAttribute(dieoffset, start, i->first, i->second); 1.462 + } 1.463 + return start; 1.464 +} 1.465 + 1.466 +void CompilationUnit::ProcessDIEs() { 1.467 + const char* dieptr = after_header_; 1.468 + size_t len; 1.469 + 1.470 + // lengthstart is the place the length field is based on. 1.471 + // It is the point in the header after the initial length field 1.472 + const char* lengthstart = buffer_; 1.473 + 1.474 + // In 64 bit dwarf, the initial length is 12 bytes, because of the 1.475 + // 0xffffffff at the start. 1.476 + if (reader_->OffsetSize() == 8) 1.477 + lengthstart += 12; 1.478 + else 1.479 + lengthstart += 4; 1.480 + 1.481 + std::stack<uint64> die_stack; 1.482 + 1.483 + while (dieptr < (lengthstart + header_.length)) { 1.484 + // We give the user the absolute offset from the beginning of 1.485 + // debug_info, since they need it to deal with ref_addr forms. 1.486 + uint64 absolute_offset = (dieptr - buffer_) + offset_from_section_start_; 1.487 + 1.488 + uint64 abbrev_num = reader_->ReadUnsignedLEB128(dieptr, &len); 1.489 + 1.490 + dieptr += len; 1.491 + 1.492 + // Abbrev == 0 represents the end of a list of children, or padding 1.493 + // at the end of the compilation unit. 1.494 + if (abbrev_num == 0) { 1.495 + if (die_stack.size() == 0) 1.496 + // If it is padding, then we are done with the compilation unit's DIEs. 1.497 + return; 1.498 + const uint64 offset = die_stack.top(); 1.499 + die_stack.pop(); 1.500 + handler_->EndDIE(offset); 1.501 + continue; 1.502 + } 1.503 + 1.504 + const Abbrev& abbrev = abbrevs_->at(static_cast<size_t>(abbrev_num)); 1.505 + const enum DwarfTag tag = abbrev.tag; 1.506 + if (!handler_->StartDIE(absolute_offset, tag)) { 1.507 + dieptr = SkipDIE(dieptr, abbrev); 1.508 + } else { 1.509 + dieptr = ProcessDIE(absolute_offset, dieptr, abbrev); 1.510 + } 1.511 + 1.512 + if (abbrev.has_children) { 1.513 + die_stack.push(absolute_offset); 1.514 + } else { 1.515 + handler_->EndDIE(absolute_offset); 1.516 + } 1.517 + } 1.518 +} 1.519 + 1.520 +LineInfo::LineInfo(const char* buffer, uint64 buffer_length, 1.521 + ByteReader* reader, LineInfoHandler* handler): 1.522 + handler_(handler), reader_(reader), buffer_(buffer), 1.523 + buffer_length_(buffer_length) { 1.524 + header_.std_opcode_lengths = NULL; 1.525 +} 1.526 + 1.527 +uint64 LineInfo::Start() { 1.528 + ReadHeader(); 1.529 + ReadLines(); 1.530 + return after_header_ - buffer_; 1.531 +} 1.532 + 1.533 +// The header for a debug_line section is mildly complicated, because 1.534 +// the line info is very tightly encoded. 1.535 +void LineInfo::ReadHeader() { 1.536 + const char* lineptr = buffer_; 1.537 + size_t initial_length_size; 1.538 + 1.539 + const uint64 initial_length 1.540 + = reader_->ReadInitialLength(lineptr, &initial_length_size); 1.541 + 1.542 + lineptr += initial_length_size; 1.543 + header_.total_length = initial_length; 1.544 + assert(buffer_ + initial_length_size + header_.total_length <= 1.545 + buffer_ + buffer_length_); 1.546 + 1.547 + // Address size *must* be set by CU ahead of time. 1.548 + assert(reader_->AddressSize() != 0); 1.549 + 1.550 + header_.version = reader_->ReadTwoBytes(lineptr); 1.551 + lineptr += 2; 1.552 + 1.553 + header_.prologue_length = reader_->ReadOffset(lineptr); 1.554 + lineptr += reader_->OffsetSize(); 1.555 + 1.556 + header_.min_insn_length = reader_->ReadOneByte(lineptr); 1.557 + lineptr += 1; 1.558 + 1.559 + header_.default_is_stmt = reader_->ReadOneByte(lineptr); 1.560 + lineptr += 1; 1.561 + 1.562 + header_.line_base = *reinterpret_cast<const int8*>(lineptr); 1.563 + lineptr += 1; 1.564 + 1.565 + header_.line_range = reader_->ReadOneByte(lineptr); 1.566 + lineptr += 1; 1.567 + 1.568 + header_.opcode_base = reader_->ReadOneByte(lineptr); 1.569 + lineptr += 1; 1.570 + 1.571 + header_.std_opcode_lengths = new std::vector<unsigned char>; 1.572 + header_.std_opcode_lengths->resize(header_.opcode_base + 1); 1.573 + (*header_.std_opcode_lengths)[0] = 0; 1.574 + for (int i = 1; i < header_.opcode_base; i++) { 1.575 + (*header_.std_opcode_lengths)[i] = reader_->ReadOneByte(lineptr); 1.576 + lineptr += 1; 1.577 + } 1.578 + 1.579 + // It is legal for the directory entry table to be empty. 1.580 + if (*lineptr) { 1.581 + uint32 dirindex = 1; 1.582 + while (*lineptr) { 1.583 + const char* dirname = lineptr; 1.584 + handler_->DefineDir(dirname, dirindex); 1.585 + lineptr += strlen(dirname) + 1; 1.586 + dirindex++; 1.587 + } 1.588 + } 1.589 + lineptr++; 1.590 + 1.591 + // It is also legal for the file entry table to be empty. 1.592 + if (*lineptr) { 1.593 + uint32 fileindex = 1; 1.594 + size_t len; 1.595 + while (*lineptr) { 1.596 + const char* filename = lineptr; 1.597 + lineptr += strlen(filename) + 1; 1.598 + 1.599 + uint64 dirindex = reader_->ReadUnsignedLEB128(lineptr, &len); 1.600 + lineptr += len; 1.601 + 1.602 + uint64 mod_time = reader_->ReadUnsignedLEB128(lineptr, &len); 1.603 + lineptr += len; 1.604 + 1.605 + uint64 filelength = reader_->ReadUnsignedLEB128(lineptr, &len); 1.606 + lineptr += len; 1.607 + handler_->DefineFile(filename, fileindex, static_cast<uint32>(dirindex), 1.608 + mod_time, filelength); 1.609 + fileindex++; 1.610 + } 1.611 + } 1.612 + lineptr++; 1.613 + 1.614 + after_header_ = lineptr; 1.615 +} 1.616 + 1.617 +/* static */ 1.618 +bool LineInfo::ProcessOneOpcode(ByteReader* reader, 1.619 + LineInfoHandler* handler, 1.620 + const struct LineInfoHeader &header, 1.621 + const char* start, 1.622 + struct LineStateMachine* lsm, 1.623 + size_t* len, 1.624 + uintptr pc, 1.625 + bool *lsm_passes_pc) { 1.626 + size_t oplen = 0; 1.627 + size_t templen; 1.628 + uint8 opcode = reader->ReadOneByte(start); 1.629 + oplen++; 1.630 + start++; 1.631 + 1.632 + // If the opcode is great than the opcode_base, it is a special 1.633 + // opcode. Most line programs consist mainly of special opcodes. 1.634 + if (opcode >= header.opcode_base) { 1.635 + opcode -= header.opcode_base; 1.636 + const int64 advance_address = (opcode / header.line_range) 1.637 + * header.min_insn_length; 1.638 + const int32 advance_line = (opcode % header.line_range) 1.639 + + header.line_base; 1.640 + 1.641 + // Check if the lsm passes "pc". If so, mark it as passed. 1.642 + if (lsm_passes_pc && 1.643 + lsm->address <= pc && pc < lsm->address + advance_address) { 1.644 + *lsm_passes_pc = true; 1.645 + } 1.646 + 1.647 + lsm->address += advance_address; 1.648 + lsm->line_num += advance_line; 1.649 + lsm->basic_block = true; 1.650 + *len = oplen; 1.651 + return true; 1.652 + } 1.653 + 1.654 + // Otherwise, we have the regular opcodes 1.655 + switch (opcode) { 1.656 + case DW_LNS_copy: { 1.657 + lsm->basic_block = false; 1.658 + *len = oplen; 1.659 + return true; 1.660 + } 1.661 + 1.662 + case DW_LNS_advance_pc: { 1.663 + uint64 advance_address = reader->ReadUnsignedLEB128(start, &templen); 1.664 + oplen += templen; 1.665 + 1.666 + // Check if the lsm passes "pc". If so, mark it as passed. 1.667 + if (lsm_passes_pc && lsm->address <= pc && 1.668 + pc < lsm->address + header.min_insn_length * advance_address) { 1.669 + *lsm_passes_pc = true; 1.670 + } 1.671 + 1.672 + lsm->address += header.min_insn_length * advance_address; 1.673 + } 1.674 + break; 1.675 + case DW_LNS_advance_line: { 1.676 + const int64 advance_line = reader->ReadSignedLEB128(start, &templen); 1.677 + oplen += templen; 1.678 + lsm->line_num += static_cast<int32>(advance_line); 1.679 + 1.680 + // With gcc 4.2.1, we can get the line_no here for the first time 1.681 + // since DW_LNS_advance_line is called after DW_LNE_set_address is 1.682 + // called. So we check if the lsm passes "pc" here, not in 1.683 + // DW_LNE_set_address. 1.684 + if (lsm_passes_pc && lsm->address == pc) { 1.685 + *lsm_passes_pc = true; 1.686 + } 1.687 + } 1.688 + break; 1.689 + case DW_LNS_set_file: { 1.690 + const uint64 fileno = reader->ReadUnsignedLEB128(start, &templen); 1.691 + oplen += templen; 1.692 + lsm->file_num = static_cast<uint32>(fileno); 1.693 + } 1.694 + break; 1.695 + case DW_LNS_set_column: { 1.696 + const uint64 colno = reader->ReadUnsignedLEB128(start, &templen); 1.697 + oplen += templen; 1.698 + lsm->column_num = static_cast<uint32>(colno); 1.699 + } 1.700 + break; 1.701 + case DW_LNS_negate_stmt: { 1.702 + lsm->is_stmt = !lsm->is_stmt; 1.703 + } 1.704 + break; 1.705 + case DW_LNS_set_basic_block: { 1.706 + lsm->basic_block = true; 1.707 + } 1.708 + break; 1.709 + case DW_LNS_fixed_advance_pc: { 1.710 + const uint16 advance_address = reader->ReadTwoBytes(start); 1.711 + oplen += 2; 1.712 + 1.713 + // Check if the lsm passes "pc". If so, mark it as passed. 1.714 + if (lsm_passes_pc && 1.715 + lsm->address <= pc && pc < lsm->address + advance_address) { 1.716 + *lsm_passes_pc = true; 1.717 + } 1.718 + 1.719 + lsm->address += advance_address; 1.720 + } 1.721 + break; 1.722 + case DW_LNS_const_add_pc: { 1.723 + const int64 advance_address = header.min_insn_length 1.724 + * ((255 - header.opcode_base) 1.725 + / header.line_range); 1.726 + 1.727 + // Check if the lsm passes "pc". If so, mark it as passed. 1.728 + if (lsm_passes_pc && 1.729 + lsm->address <= pc && pc < lsm->address + advance_address) { 1.730 + *lsm_passes_pc = true; 1.731 + } 1.732 + 1.733 + lsm->address += advance_address; 1.734 + } 1.735 + break; 1.736 + case DW_LNS_extended_op: { 1.737 + const uint64 extended_op_len = reader->ReadUnsignedLEB128(start, 1.738 + &templen); 1.739 + start += templen; 1.740 + oplen += templen + extended_op_len; 1.741 + 1.742 + const uint64 extended_op = reader->ReadOneByte(start); 1.743 + start++; 1.744 + 1.745 + switch (extended_op) { 1.746 + case DW_LNE_end_sequence: { 1.747 + lsm->end_sequence = true; 1.748 + *len = oplen; 1.749 + return true; 1.750 + } 1.751 + break; 1.752 + case DW_LNE_set_address: { 1.753 + // With gcc 4.2.1, we cannot tell the line_no here since 1.754 + // DW_LNE_set_address is called before DW_LNS_advance_line is 1.755 + // called. So we do not check if the lsm passes "pc" here. See 1.756 + // also the comment in DW_LNS_advance_line. 1.757 + uint64 address = reader->ReadAddress(start); 1.758 + lsm->address = address; 1.759 + } 1.760 + break; 1.761 + case DW_LNE_define_file: { 1.762 + const char* filename = start; 1.763 + 1.764 + templen = strlen(filename) + 1; 1.765 + start += templen; 1.766 + 1.767 + uint64 dirindex = reader->ReadUnsignedLEB128(start, &templen); 1.768 + oplen += templen; 1.769 + 1.770 + const uint64 mod_time = reader->ReadUnsignedLEB128(start, 1.771 + &templen); 1.772 + oplen += templen; 1.773 + 1.774 + const uint64 filelength = reader->ReadUnsignedLEB128(start, 1.775 + &templen); 1.776 + oplen += templen; 1.777 + 1.778 + if (handler) { 1.779 + handler->DefineFile(filename, -1, static_cast<uint32>(dirindex), 1.780 + mod_time, filelength); 1.781 + } 1.782 + } 1.783 + break; 1.784 + } 1.785 + } 1.786 + break; 1.787 + 1.788 + default: { 1.789 + // Ignore unknown opcode silently 1.790 + if (header.std_opcode_lengths) { 1.791 + for (int i = 0; i < (*header.std_opcode_lengths)[opcode]; i++) { 1.792 + reader->ReadUnsignedLEB128(start, &templen); 1.793 + start += templen; 1.794 + oplen += templen; 1.795 + } 1.796 + } 1.797 + } 1.798 + break; 1.799 + } 1.800 + *len = oplen; 1.801 + return false; 1.802 +} 1.803 + 1.804 +void LineInfo::ReadLines() { 1.805 + struct LineStateMachine lsm; 1.806 + 1.807 + // lengthstart is the place the length field is based on. 1.808 + // It is the point in the header after the initial length field 1.809 + const char* lengthstart = buffer_; 1.810 + 1.811 + // In 64 bit dwarf, the initial length is 12 bytes, because of the 1.812 + // 0xffffffff at the start. 1.813 + if (reader_->OffsetSize() == 8) 1.814 + lengthstart += 12; 1.815 + else 1.816 + lengthstart += 4; 1.817 + 1.818 + const char* lineptr = after_header_; 1.819 + lsm.Reset(header_.default_is_stmt); 1.820 + 1.821 + // The LineInfoHandler interface expects each line's length along 1.822 + // with its address, but DWARF only provides addresses (sans 1.823 + // length), and an end-of-sequence address; one infers the length 1.824 + // from the next address. So we report a line only when we get the 1.825 + // next line's address, or the end-of-sequence address. 1.826 + bool have_pending_line = false; 1.827 + uint64 pending_address = 0; 1.828 + uint32 pending_file_num = 0, pending_line_num = 0, pending_column_num = 0; 1.829 + 1.830 + while (lineptr < lengthstart + header_.total_length) { 1.831 + size_t oplength; 1.832 + bool add_row = ProcessOneOpcode(reader_, handler_, header_, 1.833 + lineptr, &lsm, &oplength, (uintptr)-1, 1.834 + NULL); 1.835 + if (add_row) { 1.836 + if (have_pending_line) 1.837 + handler_->AddLine(pending_address, lsm.address - pending_address, 1.838 + pending_file_num, pending_line_num, 1.839 + pending_column_num); 1.840 + if (lsm.end_sequence) { 1.841 + lsm.Reset(header_.default_is_stmt); 1.842 + have_pending_line = false; 1.843 + } else { 1.844 + pending_address = lsm.address; 1.845 + pending_file_num = lsm.file_num; 1.846 + pending_line_num = lsm.line_num; 1.847 + pending_column_num = lsm.column_num; 1.848 + have_pending_line = true; 1.849 + } 1.850 + } 1.851 + lineptr += oplength; 1.852 + } 1.853 + 1.854 + after_header_ = lengthstart + header_.total_length; 1.855 +} 1.856 + 1.857 +// A DWARF rule for recovering the address or value of a register, or 1.858 +// computing the canonical frame address. There is one subclass of this for 1.859 +// each '*Rule' member function in CallFrameInfo::Handler. 1.860 +// 1.861 +// It's annoying that we have to handle Rules using pointers (because 1.862 +// the concrete instances can have an arbitrary size). They're small, 1.863 +// so it would be much nicer if we could just handle them by value 1.864 +// instead of fretting about ownership and destruction. 1.865 +// 1.866 +// It seems like all these could simply be instances of std::tr1::bind, 1.867 +// except that we need instances to be EqualityComparable, too. 1.868 +// 1.869 +// This could logically be nested within State, but then the qualified names 1.870 +// get horrendous. 1.871 +class CallFrameInfo::Rule { 1.872 + public: 1.873 + virtual ~Rule() { } 1.874 + 1.875 + // Tell HANDLER that, at ADDRESS in the program, REGISTER can be 1.876 + // recovered using this rule. If REGISTER is kCFARegister, then this rule 1.877 + // describes how to compute the canonical frame address. Return what the 1.878 + // HANDLER member function returned. 1.879 + virtual bool Handle(Handler *handler, 1.880 + uint64 address, int register) const = 0; 1.881 + 1.882 + // Equality on rules. We use these to decide which rules we need 1.883 + // to report after a DW_CFA_restore_state instruction. 1.884 + virtual bool operator==(const Rule &rhs) const = 0; 1.885 + 1.886 + bool operator!=(const Rule &rhs) const { return ! (*this == rhs); } 1.887 + 1.888 + // Return a pointer to a copy of this rule. 1.889 + virtual Rule *Copy() const = 0; 1.890 + 1.891 + // If this is a base+offset rule, change its base register to REG. 1.892 + // Otherwise, do nothing. (Ugly, but required for DW_CFA_def_cfa_register.) 1.893 + virtual void SetBaseRegister(unsigned reg) { } 1.894 + 1.895 + // If this is a base+offset rule, change its offset to OFFSET. Otherwise, 1.896 + // do nothing. (Ugly, but required for DW_CFA_def_cfa_offset.) 1.897 + virtual void SetOffset(long long offset) { } 1.898 + 1.899 + // A RTTI workaround, to make it possible to implement equality 1.900 + // comparisons on classes derived from this one. 1.901 + enum CFIRTag { 1.902 + CFIR_UNDEFINED_RULE, 1.903 + CFIR_SAME_VALUE_RULE, 1.904 + CFIR_OFFSET_RULE, 1.905 + CFIR_VAL_OFFSET_RULE, 1.906 + CFIR_REGISTER_RULE, 1.907 + CFIR_EXPRESSION_RULE, 1.908 + CFIR_VAL_EXPRESSION_RULE 1.909 + }; 1.910 + 1.911 + // Produce the tag that identifies the child class of this object. 1.912 + virtual CFIRTag getTag() const = 0; 1.913 +}; 1.914 + 1.915 +// Rule: the value the register had in the caller cannot be recovered. 1.916 +class CallFrameInfo::UndefinedRule: public CallFrameInfo::Rule { 1.917 + public: 1.918 + UndefinedRule() { } 1.919 + ~UndefinedRule() { } 1.920 + CFIRTag getTag() const { return CFIR_UNDEFINED_RULE; } 1.921 + bool Handle(Handler *handler, uint64 address, int reg) const { 1.922 + return handler->UndefinedRule(address, reg); 1.923 + } 1.924 + bool operator==(const Rule &rhs) const { 1.925 + if (rhs.getTag() != CFIR_UNDEFINED_RULE) return false; 1.926 + return true; 1.927 + } 1.928 + Rule *Copy() const { return new UndefinedRule(*this); } 1.929 +}; 1.930 + 1.931 +// Rule: the register's value is the same as that it had in the caller. 1.932 +class CallFrameInfo::SameValueRule: public CallFrameInfo::Rule { 1.933 + public: 1.934 + SameValueRule() { } 1.935 + ~SameValueRule() { } 1.936 + CFIRTag getTag() const { return CFIR_SAME_VALUE_RULE; } 1.937 + bool Handle(Handler *handler, uint64 address, int reg) const { 1.938 + return handler->SameValueRule(address, reg); 1.939 + } 1.940 + bool operator==(const Rule &rhs) const { 1.941 + if (rhs.getTag() != CFIR_SAME_VALUE_RULE) return false; 1.942 + return true; 1.943 + } 1.944 + Rule *Copy() const { return new SameValueRule(*this); } 1.945 +}; 1.946 + 1.947 +// Rule: the register is saved at OFFSET from BASE_REGISTER. BASE_REGISTER 1.948 +// may be CallFrameInfo::Handler::kCFARegister. 1.949 +class CallFrameInfo::OffsetRule: public CallFrameInfo::Rule { 1.950 + public: 1.951 + OffsetRule(int base_register, long offset) 1.952 + : base_register_(base_register), offset_(offset) { } 1.953 + ~OffsetRule() { } 1.954 + CFIRTag getTag() const { return CFIR_OFFSET_RULE; } 1.955 + bool Handle(Handler *handler, uint64 address, int reg) const { 1.956 + return handler->OffsetRule(address, reg, base_register_, offset_); 1.957 + } 1.958 + bool operator==(const Rule &rhs) const { 1.959 + if (rhs.getTag() != CFIR_OFFSET_RULE) return false; 1.960 + const OffsetRule *our_rhs = static_cast<const OffsetRule *>(&rhs); 1.961 + return (base_register_ == our_rhs->base_register_ && 1.962 + offset_ == our_rhs->offset_); 1.963 + } 1.964 + Rule *Copy() const { return new OffsetRule(*this); } 1.965 + // We don't actually need SetBaseRegister or SetOffset here, since they 1.966 + // are only ever applied to CFA rules, for DW_CFA_def_cfa_offset, and it 1.967 + // doesn't make sense to use OffsetRule for computing the CFA: it 1.968 + // computes the address at which a register is saved, not a value. 1.969 + private: 1.970 + int base_register_; 1.971 + long offset_; 1.972 +}; 1.973 + 1.974 +// Rule: the value the register had in the caller is the value of 1.975 +// BASE_REGISTER plus offset. BASE_REGISTER may be 1.976 +// CallFrameInfo::Handler::kCFARegister. 1.977 +class CallFrameInfo::ValOffsetRule: public CallFrameInfo::Rule { 1.978 + public: 1.979 + ValOffsetRule(int base_register, long offset) 1.980 + : base_register_(base_register), offset_(offset) { } 1.981 + ~ValOffsetRule() { } 1.982 + CFIRTag getTag() const { return CFIR_VAL_OFFSET_RULE; } 1.983 + bool Handle(Handler *handler, uint64 address, int reg) const { 1.984 + return handler->ValOffsetRule(address, reg, base_register_, offset_); 1.985 + } 1.986 + bool operator==(const Rule &rhs) const { 1.987 + if (rhs.getTag() != CFIR_VAL_OFFSET_RULE) return false; 1.988 + const ValOffsetRule *our_rhs = static_cast<const ValOffsetRule *>(&rhs); 1.989 + return (base_register_ == our_rhs->base_register_ && 1.990 + offset_ == our_rhs->offset_); 1.991 + } 1.992 + Rule *Copy() const { return new ValOffsetRule(*this); } 1.993 + void SetBaseRegister(unsigned reg) { base_register_ = reg; } 1.994 + void SetOffset(long long offset) { offset_ = offset; } 1.995 + private: 1.996 + int base_register_; 1.997 + long offset_; 1.998 +}; 1.999 + 1.1000 +// Rule: the register has been saved in another register REGISTER_NUMBER_. 1.1001 +class CallFrameInfo::RegisterRule: public CallFrameInfo::Rule { 1.1002 + public: 1.1003 + explicit RegisterRule(int register_number) 1.1004 + : register_number_(register_number) { } 1.1005 + ~RegisterRule() { } 1.1006 + CFIRTag getTag() const { return CFIR_REGISTER_RULE; } 1.1007 + bool Handle(Handler *handler, uint64 address, int reg) const { 1.1008 + return handler->RegisterRule(address, reg, register_number_); 1.1009 + } 1.1010 + bool operator==(const Rule &rhs) const { 1.1011 + if (rhs.getTag() != CFIR_REGISTER_RULE) return false; 1.1012 + const RegisterRule *our_rhs = static_cast<const RegisterRule *>(&rhs); 1.1013 + return (register_number_ == our_rhs->register_number_); 1.1014 + } 1.1015 + Rule *Copy() const { return new RegisterRule(*this); } 1.1016 + private: 1.1017 + int register_number_; 1.1018 +}; 1.1019 + 1.1020 +// Rule: EXPRESSION evaluates to the address at which the register is saved. 1.1021 +class CallFrameInfo::ExpressionRule: public CallFrameInfo::Rule { 1.1022 + public: 1.1023 + explicit ExpressionRule(const string &expression) 1.1024 + : expression_(expression) { } 1.1025 + ~ExpressionRule() { } 1.1026 + CFIRTag getTag() const { return CFIR_EXPRESSION_RULE; } 1.1027 + bool Handle(Handler *handler, uint64 address, int reg) const { 1.1028 + return handler->ExpressionRule(address, reg, expression_); 1.1029 + } 1.1030 + bool operator==(const Rule &rhs) const { 1.1031 + if (rhs.getTag() != CFIR_EXPRESSION_RULE) return false; 1.1032 + const ExpressionRule *our_rhs = static_cast<const ExpressionRule *>(&rhs); 1.1033 + return (expression_ == our_rhs->expression_); 1.1034 + } 1.1035 + Rule *Copy() const { return new ExpressionRule(*this); } 1.1036 + private: 1.1037 + string expression_; 1.1038 +}; 1.1039 + 1.1040 +// Rule: EXPRESSION evaluates to the address at which the register is saved. 1.1041 +class CallFrameInfo::ValExpressionRule: public CallFrameInfo::Rule { 1.1042 + public: 1.1043 + explicit ValExpressionRule(const string &expression) 1.1044 + : expression_(expression) { } 1.1045 + ~ValExpressionRule() { } 1.1046 + CFIRTag getTag() const { return CFIR_VAL_EXPRESSION_RULE; } 1.1047 + bool Handle(Handler *handler, uint64 address, int reg) const { 1.1048 + return handler->ValExpressionRule(address, reg, expression_); 1.1049 + } 1.1050 + bool operator==(const Rule &rhs) const { 1.1051 + if (rhs.getTag() != CFIR_VAL_EXPRESSION_RULE) return false; 1.1052 + const ValExpressionRule *our_rhs = 1.1053 + static_cast<const ValExpressionRule *>(&rhs); 1.1054 + return (expression_ == our_rhs->expression_); 1.1055 + } 1.1056 + Rule *Copy() const { return new ValExpressionRule(*this); } 1.1057 + private: 1.1058 + string expression_; 1.1059 +}; 1.1060 + 1.1061 +// A map from register numbers to rules. 1.1062 +class CallFrameInfo::RuleMap { 1.1063 + public: 1.1064 + RuleMap() : cfa_rule_(NULL) { } 1.1065 + RuleMap(const RuleMap &rhs) : cfa_rule_(NULL) { *this = rhs; } 1.1066 + ~RuleMap() { Clear(); } 1.1067 + 1.1068 + RuleMap &operator=(const RuleMap &rhs); 1.1069 + 1.1070 + // Set the rule for computing the CFA to RULE. Take ownership of RULE. 1.1071 + void SetCFARule(Rule *rule) { delete cfa_rule_; cfa_rule_ = rule; } 1.1072 + 1.1073 + // Return the current CFA rule. Unlike RegisterRule, this RuleMap retains 1.1074 + // ownership of the rule. We use this for DW_CFA_def_cfa_offset and 1.1075 + // DW_CFA_def_cfa_register, and for detecting references to the CFA before 1.1076 + // a rule for it has been established. 1.1077 + Rule *CFARule() const { return cfa_rule_; } 1.1078 + 1.1079 + // Return the rule for REG, or NULL if there is none. The caller takes 1.1080 + // ownership of the result. 1.1081 + Rule *RegisterRule(int reg) const; 1.1082 + 1.1083 + // Set the rule for computing REG to RULE. Take ownership of RULE. 1.1084 + void SetRegisterRule(int reg, Rule *rule); 1.1085 + 1.1086 + // Make all the appropriate calls to HANDLER as if we were changing from 1.1087 + // this RuleMap to NEW_RULES at ADDRESS. We use this to implement 1.1088 + // DW_CFA_restore_state, where lots of rules can change simultaneously. 1.1089 + // Return true if all handlers returned true; otherwise, return false. 1.1090 + bool HandleTransitionTo(Handler *handler, uint64 address, 1.1091 + const RuleMap &new_rules) const; 1.1092 + 1.1093 + private: 1.1094 + // A map from register numbers to Rules. 1.1095 + typedef std::map<int, Rule *> RuleByNumber; 1.1096 + 1.1097 + // Remove all register rules and clear cfa_rule_. 1.1098 + void Clear(); 1.1099 + 1.1100 + // The rule for computing the canonical frame address. This RuleMap owns 1.1101 + // this rule. 1.1102 + Rule *cfa_rule_; 1.1103 + 1.1104 + // A map from register numbers to postfix expressions to recover 1.1105 + // their values. This RuleMap owns the Rules the map refers to. 1.1106 + RuleByNumber registers_; 1.1107 +}; 1.1108 + 1.1109 +CallFrameInfo::RuleMap &CallFrameInfo::RuleMap::operator=(const RuleMap &rhs) { 1.1110 + Clear(); 1.1111 + // Since each map owns the rules it refers to, assignment must copy them. 1.1112 + if (rhs.cfa_rule_) cfa_rule_ = rhs.cfa_rule_->Copy(); 1.1113 + for (RuleByNumber::const_iterator it = rhs.registers_.begin(); 1.1114 + it != rhs.registers_.end(); it++) 1.1115 + registers_[it->first] = it->second->Copy(); 1.1116 + return *this; 1.1117 +} 1.1118 + 1.1119 +CallFrameInfo::Rule *CallFrameInfo::RuleMap::RegisterRule(int reg) const { 1.1120 + assert(reg != Handler::kCFARegister); 1.1121 + RuleByNumber::const_iterator it = registers_.find(reg); 1.1122 + if (it != registers_.end()) 1.1123 + return it->second->Copy(); 1.1124 + else 1.1125 + return NULL; 1.1126 +} 1.1127 + 1.1128 +void CallFrameInfo::RuleMap::SetRegisterRule(int reg, Rule *rule) { 1.1129 + assert(reg != Handler::kCFARegister); 1.1130 + assert(rule); 1.1131 + Rule **slot = ®isters_[reg]; 1.1132 + delete *slot; 1.1133 + *slot = rule; 1.1134 +} 1.1135 + 1.1136 +bool CallFrameInfo::RuleMap::HandleTransitionTo( 1.1137 + Handler *handler, 1.1138 + uint64 address, 1.1139 + const RuleMap &new_rules) const { 1.1140 + // Transition from cfa_rule_ to new_rules.cfa_rule_. 1.1141 + if (cfa_rule_ && new_rules.cfa_rule_) { 1.1142 + if (*cfa_rule_ != *new_rules.cfa_rule_ && 1.1143 + !new_rules.cfa_rule_->Handle(handler, address, 1.1144 + Handler::kCFARegister)) 1.1145 + return false; 1.1146 + } else if (cfa_rule_) { 1.1147 + // this RuleMap has a CFA rule but new_rules doesn't. 1.1148 + // CallFrameInfo::Handler has no way to handle this --- and shouldn't; 1.1149 + // it's garbage input. The instruction interpreter should have 1.1150 + // detected this and warned, so take no action here. 1.1151 + } else if (new_rules.cfa_rule_) { 1.1152 + // This shouldn't be possible: NEW_RULES is some prior state, and 1.1153 + // there's no way to remove entries. 1.1154 + assert(0); 1.1155 + } else { 1.1156 + // Both CFA rules are empty. No action needed. 1.1157 + } 1.1158 + 1.1159 + // Traverse the two maps in order by register number, and report 1.1160 + // whatever differences we find. 1.1161 + RuleByNumber::const_iterator old_it = registers_.begin(); 1.1162 + RuleByNumber::const_iterator new_it = new_rules.registers_.begin(); 1.1163 + while (old_it != registers_.end() && new_it != new_rules.registers_.end()) { 1.1164 + if (old_it->first < new_it->first) { 1.1165 + // This RuleMap has an entry for old_it->first, but NEW_RULES 1.1166 + // doesn't. 1.1167 + // 1.1168 + // This isn't really the right thing to do, but since CFI generally 1.1169 + // only mentions callee-saves registers, and GCC's convention for 1.1170 + // callee-saves registers is that they are unchanged, it's a good 1.1171 + // approximation. 1.1172 + if (!handler->SameValueRule(address, old_it->first)) 1.1173 + return false; 1.1174 + old_it++; 1.1175 + } else if (old_it->first > new_it->first) { 1.1176 + // NEW_RULES has entry for new_it->first, but this RuleMap 1.1177 + // doesn't. This shouldn't be possible: NEW_RULES is some prior 1.1178 + // state, and there's no way to remove entries. 1.1179 + assert(0); 1.1180 + } else { 1.1181 + // Both maps have an entry for this register. Report the new 1.1182 + // rule if it is different. 1.1183 + if (*old_it->second != *new_it->second && 1.1184 + !new_it->second->Handle(handler, address, new_it->first)) 1.1185 + return false; 1.1186 + new_it++, old_it++; 1.1187 + } 1.1188 + } 1.1189 + // Finish off entries from this RuleMap with no counterparts in new_rules. 1.1190 + while (old_it != registers_.end()) { 1.1191 + if (!handler->SameValueRule(address, old_it->first)) 1.1192 + return false; 1.1193 + old_it++; 1.1194 + } 1.1195 + // Since we only make transitions from a rule set to some previously 1.1196 + // saved rule set, and we can only add rules to the map, NEW_RULES 1.1197 + // must have fewer rules than *this. 1.1198 + assert(new_it == new_rules.registers_.end()); 1.1199 + 1.1200 + return true; 1.1201 +} 1.1202 + 1.1203 +// Remove all register rules and clear cfa_rule_. 1.1204 +void CallFrameInfo::RuleMap::Clear() { 1.1205 + delete cfa_rule_; 1.1206 + cfa_rule_ = NULL; 1.1207 + for (RuleByNumber::iterator it = registers_.begin(); 1.1208 + it != registers_.end(); it++) 1.1209 + delete it->second; 1.1210 + registers_.clear(); 1.1211 +} 1.1212 + 1.1213 +// The state of the call frame information interpreter as it processes 1.1214 +// instructions from a CIE and FDE. 1.1215 +class CallFrameInfo::State { 1.1216 + public: 1.1217 + // Create a call frame information interpreter state with the given 1.1218 + // reporter, reader, handler, and initial call frame info address. 1.1219 + State(ByteReader *reader, Handler *handler, Reporter *reporter, 1.1220 + uint64 address) 1.1221 + : reader_(reader), handler_(handler), reporter_(reporter), 1.1222 + address_(address), entry_(NULL), cursor_(NULL) { } 1.1223 + 1.1224 + // Interpret instructions from CIE, save the resulting rule set for 1.1225 + // DW_CFA_restore instructions, and return true. On error, report 1.1226 + // the problem to reporter_ and return false. 1.1227 + bool InterpretCIE(const CIE &cie); 1.1228 + 1.1229 + // Interpret instructions from FDE, and return true. On error, 1.1230 + // report the problem to reporter_ and return false. 1.1231 + bool InterpretFDE(const FDE &fde); 1.1232 + 1.1233 + private: 1.1234 + // The operands of a CFI instruction, for ParseOperands. 1.1235 + struct Operands { 1.1236 + unsigned register_number; // A register number. 1.1237 + uint64 offset; // An offset or address. 1.1238 + long signed_offset; // A signed offset. 1.1239 + string expression; // A DWARF expression. 1.1240 + }; 1.1241 + 1.1242 + // Parse CFI instruction operands from STATE's instruction stream as 1.1243 + // described by FORMAT. On success, populate OPERANDS with the 1.1244 + // results, and return true. On failure, report the problem and 1.1245 + // return false. 1.1246 + // 1.1247 + // Each character of FORMAT should be one of the following: 1.1248 + // 1.1249 + // 'r' unsigned LEB128 register number (OPERANDS->register_number) 1.1250 + // 'o' unsigned LEB128 offset (OPERANDS->offset) 1.1251 + // 's' signed LEB128 offset (OPERANDS->signed_offset) 1.1252 + // 'a' machine-size address (OPERANDS->offset) 1.1253 + // (If the CIE has a 'z' augmentation string, 'a' uses the 1.1254 + // encoding specified by the 'R' argument.) 1.1255 + // '1' a one-byte offset (OPERANDS->offset) 1.1256 + // '2' a two-byte offset (OPERANDS->offset) 1.1257 + // '4' a four-byte offset (OPERANDS->offset) 1.1258 + // '8' an eight-byte offset (OPERANDS->offset) 1.1259 + // 'e' a DW_FORM_block holding a (OPERANDS->expression) 1.1260 + // DWARF expression 1.1261 + bool ParseOperands(const char *format, Operands *operands); 1.1262 + 1.1263 + // Interpret one CFI instruction from STATE's instruction stream, update 1.1264 + // STATE, report any rule changes to handler_, and return true. On 1.1265 + // failure, report the problem and return false. 1.1266 + bool DoInstruction(); 1.1267 + 1.1268 + // The following Do* member functions are subroutines of DoInstruction, 1.1269 + // factoring out the actual work of operations that have several 1.1270 + // different encodings. 1.1271 + 1.1272 + // Set the CFA rule to be the value of BASE_REGISTER plus OFFSET, and 1.1273 + // return true. On failure, report and return false. (Used for 1.1274 + // DW_CFA_def_cfa and DW_CFA_def_cfa_sf.) 1.1275 + bool DoDefCFA(unsigned base_register, long offset); 1.1276 + 1.1277 + // Change the offset of the CFA rule to OFFSET, and return true. On 1.1278 + // failure, report and return false. (Subroutine for 1.1279 + // DW_CFA_def_cfa_offset and DW_CFA_def_cfa_offset_sf.) 1.1280 + bool DoDefCFAOffset(long offset); 1.1281 + 1.1282 + // Specify that REG can be recovered using RULE, and return true. On 1.1283 + // failure, report and return false. 1.1284 + bool DoRule(unsigned reg, Rule *rule); 1.1285 + 1.1286 + // Specify that REG can be found at OFFSET from the CFA, and return true. 1.1287 + // On failure, report and return false. (Subroutine for DW_CFA_offset, 1.1288 + // DW_CFA_offset_extended, and DW_CFA_offset_extended_sf.) 1.1289 + bool DoOffset(unsigned reg, long offset); 1.1290 + 1.1291 + // Specify that the caller's value for REG is the CFA plus OFFSET, 1.1292 + // and return true. On failure, report and return false. (Subroutine 1.1293 + // for DW_CFA_val_offset and DW_CFA_val_offset_sf.) 1.1294 + bool DoValOffset(unsigned reg, long offset); 1.1295 + 1.1296 + // Restore REG to the rule established in the CIE, and return true. On 1.1297 + // failure, report and return false. (Subroutine for DW_CFA_restore and 1.1298 + // DW_CFA_restore_extended.) 1.1299 + bool DoRestore(unsigned reg); 1.1300 + 1.1301 + // Return the section offset of the instruction at cursor. For use 1.1302 + // in error messages. 1.1303 + uint64 CursorOffset() { return entry_->offset + (cursor_ - entry_->start); } 1.1304 + 1.1305 + // Report that entry_ is incomplete, and return false. For brevity. 1.1306 + bool ReportIncomplete() { 1.1307 + reporter_->Incomplete(entry_->offset, entry_->kind); 1.1308 + return false; 1.1309 + } 1.1310 + 1.1311 + // For reading multi-byte values with the appropriate endianness. 1.1312 + ByteReader *reader_; 1.1313 + 1.1314 + // The handler to which we should report the data we find. 1.1315 + Handler *handler_; 1.1316 + 1.1317 + // For reporting problems in the info we're parsing. 1.1318 + Reporter *reporter_; 1.1319 + 1.1320 + // The code address to which the next instruction in the stream applies. 1.1321 + uint64 address_; 1.1322 + 1.1323 + // The entry whose instructions we are currently processing. This is 1.1324 + // first a CIE, and then an FDE. 1.1325 + const Entry *entry_; 1.1326 + 1.1327 + // The next instruction to process. 1.1328 + const char *cursor_; 1.1329 + 1.1330 + // The current set of rules. 1.1331 + RuleMap rules_; 1.1332 + 1.1333 + // The set of rules established by the CIE, used by DW_CFA_restore 1.1334 + // and DW_CFA_restore_extended. We set this after interpreting the 1.1335 + // CIE's instructions. 1.1336 + RuleMap cie_rules_; 1.1337 + 1.1338 + // A stack of saved states, for DW_CFA_remember_state and 1.1339 + // DW_CFA_restore_state. 1.1340 + std::stack<RuleMap> saved_rules_; 1.1341 +}; 1.1342 + 1.1343 +bool CallFrameInfo::State::InterpretCIE(const CIE &cie) { 1.1344 + entry_ = &cie; 1.1345 + cursor_ = entry_->instructions; 1.1346 + while (cursor_ < entry_->end) 1.1347 + if (!DoInstruction()) 1.1348 + return false; 1.1349 + // Note the rules established by the CIE, for use by DW_CFA_restore 1.1350 + // and DW_CFA_restore_extended. 1.1351 + cie_rules_ = rules_; 1.1352 + return true; 1.1353 +} 1.1354 + 1.1355 +bool CallFrameInfo::State::InterpretFDE(const FDE &fde) { 1.1356 + entry_ = &fde; 1.1357 + cursor_ = entry_->instructions; 1.1358 + while (cursor_ < entry_->end) 1.1359 + if (!DoInstruction()) 1.1360 + return false; 1.1361 + return true; 1.1362 +} 1.1363 + 1.1364 +bool CallFrameInfo::State::ParseOperands(const char *format, 1.1365 + Operands *operands) { 1.1366 + size_t len; 1.1367 + const char *operand; 1.1368 + 1.1369 + for (operand = format; *operand; operand++) { 1.1370 + size_t bytes_left = entry_->end - cursor_; 1.1371 + switch (*operand) { 1.1372 + case 'r': 1.1373 + operands->register_number = reader_->ReadUnsignedLEB128(cursor_, &len); 1.1374 + if (len > bytes_left) return ReportIncomplete(); 1.1375 + cursor_ += len; 1.1376 + break; 1.1377 + 1.1378 + case 'o': 1.1379 + operands->offset = reader_->ReadUnsignedLEB128(cursor_, &len); 1.1380 + if (len > bytes_left) return ReportIncomplete(); 1.1381 + cursor_ += len; 1.1382 + break; 1.1383 + 1.1384 + case 's': 1.1385 + operands->signed_offset = reader_->ReadSignedLEB128(cursor_, &len); 1.1386 + if (len > bytes_left) return ReportIncomplete(); 1.1387 + cursor_ += len; 1.1388 + break; 1.1389 + 1.1390 + case 'a': 1.1391 + operands->offset = 1.1392 + reader_->ReadEncodedPointer(cursor_, entry_->cie->pointer_encoding, 1.1393 + &len); 1.1394 + if (len > bytes_left) return ReportIncomplete(); 1.1395 + cursor_ += len; 1.1396 + break; 1.1397 + 1.1398 + case '1': 1.1399 + if (1 > bytes_left) return ReportIncomplete(); 1.1400 + operands->offset = static_cast<unsigned char>(*cursor_++); 1.1401 + break; 1.1402 + 1.1403 + case '2': 1.1404 + if (2 > bytes_left) return ReportIncomplete(); 1.1405 + operands->offset = reader_->ReadTwoBytes(cursor_); 1.1406 + cursor_ += 2; 1.1407 + break; 1.1408 + 1.1409 + case '4': 1.1410 + if (4 > bytes_left) return ReportIncomplete(); 1.1411 + operands->offset = reader_->ReadFourBytes(cursor_); 1.1412 + cursor_ += 4; 1.1413 + break; 1.1414 + 1.1415 + case '8': 1.1416 + if (8 > bytes_left) return ReportIncomplete(); 1.1417 + operands->offset = reader_->ReadEightBytes(cursor_); 1.1418 + cursor_ += 8; 1.1419 + break; 1.1420 + 1.1421 + case 'e': { 1.1422 + size_t expression_length = reader_->ReadUnsignedLEB128(cursor_, &len); 1.1423 + if (len > bytes_left || expression_length > bytes_left - len) 1.1424 + return ReportIncomplete(); 1.1425 + cursor_ += len; 1.1426 + operands->expression = string(cursor_, expression_length); 1.1427 + cursor_ += expression_length; 1.1428 + break; 1.1429 + } 1.1430 + 1.1431 + default: 1.1432 + assert(0); 1.1433 + } 1.1434 + } 1.1435 + 1.1436 + return true; 1.1437 +} 1.1438 + 1.1439 +bool CallFrameInfo::State::DoInstruction() { 1.1440 + CIE *cie = entry_->cie; 1.1441 + Operands ops; 1.1442 + 1.1443 + // Our entry's kind should have been set by now. 1.1444 + assert(entry_->kind != kUnknown); 1.1445 + 1.1446 + // We shouldn't have been invoked unless there were more 1.1447 + // instructions to parse. 1.1448 + assert(cursor_ < entry_->end); 1.1449 + 1.1450 + unsigned opcode = *cursor_++; 1.1451 + if ((opcode & 0xc0) != 0) { 1.1452 + switch (opcode & 0xc0) { 1.1453 + // Advance the address. 1.1454 + case DW_CFA_advance_loc: { 1.1455 + size_t code_offset = opcode & 0x3f; 1.1456 + address_ += code_offset * cie->code_alignment_factor; 1.1457 + break; 1.1458 + } 1.1459 + 1.1460 + // Find a register at an offset from the CFA. 1.1461 + case DW_CFA_offset: 1.1462 + if (!ParseOperands("o", &ops) || 1.1463 + !DoOffset(opcode & 0x3f, ops.offset * cie->data_alignment_factor)) 1.1464 + return false; 1.1465 + break; 1.1466 + 1.1467 + // Restore the rule established for a register by the CIE. 1.1468 + case DW_CFA_restore: 1.1469 + if (!DoRestore(opcode & 0x3f)) return false; 1.1470 + break; 1.1471 + 1.1472 + // The 'if' above should have excluded this possibility. 1.1473 + default: 1.1474 + assert(0); 1.1475 + } 1.1476 + 1.1477 + // Return here, so the big switch below won't be indented. 1.1478 + return true; 1.1479 + } 1.1480 + 1.1481 + switch (opcode) { 1.1482 + // Set the address. 1.1483 + case DW_CFA_set_loc: 1.1484 + if (!ParseOperands("a", &ops)) return false; 1.1485 + address_ = ops.offset; 1.1486 + break; 1.1487 + 1.1488 + // Advance the address. 1.1489 + case DW_CFA_advance_loc1: 1.1490 + if (!ParseOperands("1", &ops)) return false; 1.1491 + address_ += ops.offset * cie->code_alignment_factor; 1.1492 + break; 1.1493 + 1.1494 + // Advance the address. 1.1495 + case DW_CFA_advance_loc2: 1.1496 + if (!ParseOperands("2", &ops)) return false; 1.1497 + address_ += ops.offset * cie->code_alignment_factor; 1.1498 + break; 1.1499 + 1.1500 + // Advance the address. 1.1501 + case DW_CFA_advance_loc4: 1.1502 + if (!ParseOperands("4", &ops)) return false; 1.1503 + address_ += ops.offset * cie->code_alignment_factor; 1.1504 + break; 1.1505 + 1.1506 + // Advance the address. 1.1507 + case DW_CFA_MIPS_advance_loc8: 1.1508 + if (!ParseOperands("8", &ops)) return false; 1.1509 + address_ += ops.offset * cie->code_alignment_factor; 1.1510 + break; 1.1511 + 1.1512 + // Compute the CFA by adding an offset to a register. 1.1513 + case DW_CFA_def_cfa: 1.1514 + if (!ParseOperands("ro", &ops) || 1.1515 + !DoDefCFA(ops.register_number, ops.offset)) 1.1516 + return false; 1.1517 + break; 1.1518 + 1.1519 + // Compute the CFA by adding an offset to a register. 1.1520 + case DW_CFA_def_cfa_sf: 1.1521 + if (!ParseOperands("rs", &ops) || 1.1522 + !DoDefCFA(ops.register_number, 1.1523 + ops.signed_offset * cie->data_alignment_factor)) 1.1524 + return false; 1.1525 + break; 1.1526 + 1.1527 + // Change the base register used to compute the CFA. 1.1528 + case DW_CFA_def_cfa_register: { 1.1529 + Rule *cfa_rule = rules_.CFARule(); 1.1530 + if (!cfa_rule) { 1.1531 + reporter_->NoCFARule(entry_->offset, entry_->kind, CursorOffset()); 1.1532 + return false; 1.1533 + } 1.1534 + if (!ParseOperands("r", &ops)) return false; 1.1535 + cfa_rule->SetBaseRegister(ops.register_number); 1.1536 + if (!cfa_rule->Handle(handler_, address_, 1.1537 + Handler::kCFARegister)) 1.1538 + return false; 1.1539 + break; 1.1540 + } 1.1541 + 1.1542 + // Change the offset used to compute the CFA. 1.1543 + case DW_CFA_def_cfa_offset: 1.1544 + if (!ParseOperands("o", &ops) || 1.1545 + !DoDefCFAOffset(ops.offset)) 1.1546 + return false; 1.1547 + break; 1.1548 + 1.1549 + // Change the offset used to compute the CFA. 1.1550 + case DW_CFA_def_cfa_offset_sf: 1.1551 + if (!ParseOperands("s", &ops) || 1.1552 + !DoDefCFAOffset(ops.signed_offset * cie->data_alignment_factor)) 1.1553 + return false; 1.1554 + break; 1.1555 + 1.1556 + // Specify an expression whose value is the CFA. 1.1557 + case DW_CFA_def_cfa_expression: { 1.1558 + if (!ParseOperands("e", &ops)) 1.1559 + return false; 1.1560 + Rule *rule = new ValExpressionRule(ops.expression); 1.1561 + rules_.SetCFARule(rule); 1.1562 + if (!rule->Handle(handler_, address_, 1.1563 + Handler::kCFARegister)) 1.1564 + return false; 1.1565 + break; 1.1566 + } 1.1567 + 1.1568 + // The register's value cannot be recovered. 1.1569 + case DW_CFA_undefined: { 1.1570 + if (!ParseOperands("r", &ops) || 1.1571 + !DoRule(ops.register_number, new UndefinedRule())) 1.1572 + return false; 1.1573 + break; 1.1574 + } 1.1575 + 1.1576 + // The register's value is unchanged from its value in the caller. 1.1577 + case DW_CFA_same_value: { 1.1578 + if (!ParseOperands("r", &ops) || 1.1579 + !DoRule(ops.register_number, new SameValueRule())) 1.1580 + return false; 1.1581 + break; 1.1582 + } 1.1583 + 1.1584 + // Find a register at an offset from the CFA. 1.1585 + case DW_CFA_offset_extended: 1.1586 + if (!ParseOperands("ro", &ops) || 1.1587 + !DoOffset(ops.register_number, 1.1588 + ops.offset * cie->data_alignment_factor)) 1.1589 + return false; 1.1590 + break; 1.1591 + 1.1592 + // The register is saved at an offset from the CFA. 1.1593 + case DW_CFA_offset_extended_sf: 1.1594 + if (!ParseOperands("rs", &ops) || 1.1595 + !DoOffset(ops.register_number, 1.1596 + ops.signed_offset * cie->data_alignment_factor)) 1.1597 + return false; 1.1598 + break; 1.1599 + 1.1600 + // The register is saved at an offset from the CFA. 1.1601 + case DW_CFA_GNU_negative_offset_extended: 1.1602 + if (!ParseOperands("ro", &ops) || 1.1603 + !DoOffset(ops.register_number, 1.1604 + -ops.offset * cie->data_alignment_factor)) 1.1605 + return false; 1.1606 + break; 1.1607 + 1.1608 + // The register's value is the sum of the CFA plus an offset. 1.1609 + case DW_CFA_val_offset: 1.1610 + if (!ParseOperands("ro", &ops) || 1.1611 + !DoValOffset(ops.register_number, 1.1612 + ops.offset * cie->data_alignment_factor)) 1.1613 + return false; 1.1614 + break; 1.1615 + 1.1616 + // The register's value is the sum of the CFA plus an offset. 1.1617 + case DW_CFA_val_offset_sf: 1.1618 + if (!ParseOperands("rs", &ops) || 1.1619 + !DoValOffset(ops.register_number, 1.1620 + ops.signed_offset * cie->data_alignment_factor)) 1.1621 + return false; 1.1622 + break; 1.1623 + 1.1624 + // The register has been saved in another register. 1.1625 + case DW_CFA_register: { 1.1626 + if (!ParseOperands("ro", &ops) || 1.1627 + !DoRule(ops.register_number, new RegisterRule(ops.offset))) 1.1628 + return false; 1.1629 + break; 1.1630 + } 1.1631 + 1.1632 + // An expression yields the address at which the register is saved. 1.1633 + case DW_CFA_expression: { 1.1634 + if (!ParseOperands("re", &ops) || 1.1635 + !DoRule(ops.register_number, new ExpressionRule(ops.expression))) 1.1636 + return false; 1.1637 + break; 1.1638 + } 1.1639 + 1.1640 + // An expression yields the caller's value for the register. 1.1641 + case DW_CFA_val_expression: { 1.1642 + if (!ParseOperands("re", &ops) || 1.1643 + !DoRule(ops.register_number, new ValExpressionRule(ops.expression))) 1.1644 + return false; 1.1645 + break; 1.1646 + } 1.1647 + 1.1648 + // Restore the rule established for a register by the CIE. 1.1649 + case DW_CFA_restore_extended: 1.1650 + if (!ParseOperands("r", &ops) || 1.1651 + !DoRestore( ops.register_number)) 1.1652 + return false; 1.1653 + break; 1.1654 + 1.1655 + // Save the current set of rules on a stack. 1.1656 + case DW_CFA_remember_state: 1.1657 + saved_rules_.push(rules_); 1.1658 + break; 1.1659 + 1.1660 + // Pop the current set of rules off the stack. 1.1661 + case DW_CFA_restore_state: { 1.1662 + if (saved_rules_.empty()) { 1.1663 + reporter_->EmptyStateStack(entry_->offset, entry_->kind, 1.1664 + CursorOffset()); 1.1665 + return false; 1.1666 + } 1.1667 + const RuleMap &new_rules = saved_rules_.top(); 1.1668 + if (rules_.CFARule() && !new_rules.CFARule()) { 1.1669 + reporter_->ClearingCFARule(entry_->offset, entry_->kind, 1.1670 + CursorOffset()); 1.1671 + return false; 1.1672 + } 1.1673 + rules_.HandleTransitionTo(handler_, address_, new_rules); 1.1674 + rules_ = new_rules; 1.1675 + saved_rules_.pop(); 1.1676 + break; 1.1677 + } 1.1678 + 1.1679 + // No operation. (Padding instruction.) 1.1680 + case DW_CFA_nop: 1.1681 + break; 1.1682 + 1.1683 + // A SPARC register window save: Registers 8 through 15 (%o0-%o7) 1.1684 + // are saved in registers 24 through 31 (%i0-%i7), and registers 1.1685 + // 16 through 31 (%l0-%l7 and %i0-%i7) are saved at CFA offsets 1.1686 + // (0-15 * the register size). The register numbers must be 1.1687 + // hard-coded. A GNU extension, and not a pretty one. 1.1688 + case DW_CFA_GNU_window_save: { 1.1689 + // Save %o0-%o7 in %i0-%i7. 1.1690 + for (int i = 8; i < 16; i++) 1.1691 + if (!DoRule(i, new RegisterRule(i + 16))) 1.1692 + return false; 1.1693 + // Save %l0-%l7 and %i0-%i7 at the CFA. 1.1694 + for (int i = 16; i < 32; i++) 1.1695 + // Assume that the byte reader's address size is the same as 1.1696 + // the architecture's register size. !@#%*^ hilarious. 1.1697 + if (!DoRule(i, new OffsetRule(Handler::kCFARegister, 1.1698 + (i - 16) * reader_->AddressSize()))) 1.1699 + return false; 1.1700 + break; 1.1701 + } 1.1702 + 1.1703 + // I'm not sure what this is. GDB doesn't use it for unwinding. 1.1704 + case DW_CFA_GNU_args_size: 1.1705 + if (!ParseOperands("o", &ops)) return false; 1.1706 + break; 1.1707 + 1.1708 + // An opcode we don't recognize. 1.1709 + default: { 1.1710 + reporter_->BadInstruction(entry_->offset, entry_->kind, CursorOffset()); 1.1711 + return false; 1.1712 + } 1.1713 + } 1.1714 + 1.1715 + return true; 1.1716 +} 1.1717 + 1.1718 +bool CallFrameInfo::State::DoDefCFA(unsigned base_register, long offset) { 1.1719 + Rule *rule = new ValOffsetRule(base_register, offset); 1.1720 + rules_.SetCFARule(rule); 1.1721 + return rule->Handle(handler_, address_, 1.1722 + Handler::kCFARegister); 1.1723 +} 1.1724 + 1.1725 +bool CallFrameInfo::State::DoDefCFAOffset(long offset) { 1.1726 + Rule *cfa_rule = rules_.CFARule(); 1.1727 + if (!cfa_rule) { 1.1728 + reporter_->NoCFARule(entry_->offset, entry_->kind, CursorOffset()); 1.1729 + return false; 1.1730 + } 1.1731 + cfa_rule->SetOffset(offset); 1.1732 + return cfa_rule->Handle(handler_, address_, 1.1733 + Handler::kCFARegister); 1.1734 +} 1.1735 + 1.1736 +bool CallFrameInfo::State::DoRule(unsigned reg, Rule *rule) { 1.1737 + rules_.SetRegisterRule(reg, rule); 1.1738 + return rule->Handle(handler_, address_, reg); 1.1739 +} 1.1740 + 1.1741 +bool CallFrameInfo::State::DoOffset(unsigned reg, long offset) { 1.1742 + if (!rules_.CFARule()) { 1.1743 + reporter_->NoCFARule(entry_->offset, entry_->kind, CursorOffset()); 1.1744 + return false; 1.1745 + } 1.1746 + return DoRule(reg, 1.1747 + new OffsetRule(Handler::kCFARegister, offset)); 1.1748 +} 1.1749 + 1.1750 +bool CallFrameInfo::State::DoValOffset(unsigned reg, long offset) { 1.1751 + if (!rules_.CFARule()) { 1.1752 + reporter_->NoCFARule(entry_->offset, entry_->kind, CursorOffset()); 1.1753 + return false; 1.1754 + } 1.1755 + return DoRule(reg, 1.1756 + new ValOffsetRule(Handler::kCFARegister, offset)); 1.1757 +} 1.1758 + 1.1759 +bool CallFrameInfo::State::DoRestore(unsigned reg) { 1.1760 + // DW_CFA_restore and DW_CFA_restore_extended don't make sense in a CIE. 1.1761 + if (entry_->kind == kCIE) { 1.1762 + reporter_->RestoreInCIE(entry_->offset, CursorOffset()); 1.1763 + return false; 1.1764 + } 1.1765 + Rule *rule = cie_rules_.RegisterRule(reg); 1.1766 + if (!rule) { 1.1767 + // This isn't really the right thing to do, but since CFI generally 1.1768 + // only mentions callee-saves registers, and GCC's convention for 1.1769 + // callee-saves registers is that they are unchanged, it's a good 1.1770 + // approximation. 1.1771 + rule = new SameValueRule(); 1.1772 + } 1.1773 + return DoRule(reg, rule); 1.1774 +} 1.1775 + 1.1776 +bool CallFrameInfo::ReadEntryPrologue(const char *cursor, Entry *entry) { 1.1777 + const char *buffer_end = buffer_ + buffer_length_; 1.1778 + 1.1779 + // Initialize enough of ENTRY for use in error reporting. 1.1780 + entry->offset = cursor - buffer_; 1.1781 + entry->start = cursor; 1.1782 + entry->kind = kUnknown; 1.1783 + entry->end = NULL; 1.1784 + 1.1785 + // Read the initial length. This sets reader_'s offset size. 1.1786 + size_t length_size; 1.1787 + uint64 length = reader_->ReadInitialLength(cursor, &length_size); 1.1788 + if (length_size > size_t(buffer_end - cursor)) 1.1789 + return ReportIncomplete(entry); 1.1790 + cursor += length_size; 1.1791 + 1.1792 + // In a .eh_frame section, a length of zero marks the end of the series 1.1793 + // of entries. 1.1794 + if (length == 0 && eh_frame_) { 1.1795 + entry->kind = kTerminator; 1.1796 + entry->end = cursor; 1.1797 + return true; 1.1798 + } 1.1799 + 1.1800 + // Validate the length. 1.1801 + if (length > size_t(buffer_end - cursor)) 1.1802 + return ReportIncomplete(entry); 1.1803 + 1.1804 + // The length is the number of bytes after the initial length field; 1.1805 + // we have that position handy at this point, so compute the end 1.1806 + // now. (If we're parsing 64-bit-offset DWARF on a 32-bit machine, 1.1807 + // and the length didn't fit in a size_t, we would have rejected it 1.1808 + // above.) 1.1809 + entry->end = cursor + length; 1.1810 + 1.1811 + // Parse the next field: either the offset of a CIE or a CIE id. 1.1812 + size_t offset_size = reader_->OffsetSize(); 1.1813 + if (offset_size > size_t(entry->end - cursor)) return ReportIncomplete(entry); 1.1814 + entry->id = reader_->ReadOffset(cursor); 1.1815 + 1.1816 + // Don't advance cursor past id field yet; in .eh_frame data we need 1.1817 + // the id's position to compute the section offset of an FDE's CIE. 1.1818 + 1.1819 + // Now we can decide what kind of entry this is. 1.1820 + if (eh_frame_) { 1.1821 + // In .eh_frame data, an ID of zero marks the entry as a CIE, and 1.1822 + // anything else is an offset from the id field of the FDE to the start 1.1823 + // of the CIE. 1.1824 + if (entry->id == 0) { 1.1825 + entry->kind = kCIE; 1.1826 + } else { 1.1827 + entry->kind = kFDE; 1.1828 + // Turn the offset from the id into an offset from the buffer's start. 1.1829 + entry->id = (cursor - buffer_) - entry->id; 1.1830 + } 1.1831 + } else { 1.1832 + // In DWARF CFI data, an ID of ~0 (of the appropriate width, given the 1.1833 + // offset size for the entry) marks the entry as a CIE, and anything 1.1834 + // else is the offset of the CIE from the beginning of the section. 1.1835 + if (offset_size == 4) 1.1836 + entry->kind = (entry->id == 0xffffffff) ? kCIE : kFDE; 1.1837 + else { 1.1838 + assert(offset_size == 8); 1.1839 + entry->kind = (entry->id == 0xffffffffffffffffULL) ? kCIE : kFDE; 1.1840 + } 1.1841 + } 1.1842 + 1.1843 + // Now advance cursor past the id. 1.1844 + cursor += offset_size; 1.1845 + 1.1846 + // The fields specific to this kind of entry start here. 1.1847 + entry->fields = cursor; 1.1848 + 1.1849 + entry->cie = NULL; 1.1850 + 1.1851 + return true; 1.1852 +} 1.1853 + 1.1854 +bool CallFrameInfo::ReadCIEFields(CIE *cie) { 1.1855 + const char *cursor = cie->fields; 1.1856 + size_t len; 1.1857 + 1.1858 + assert(cie->kind == kCIE); 1.1859 + 1.1860 + // Prepare for early exit. 1.1861 + cie->version = 0; 1.1862 + cie->augmentation.clear(); 1.1863 + cie->code_alignment_factor = 0; 1.1864 + cie->data_alignment_factor = 0; 1.1865 + cie->return_address_register = 0; 1.1866 + cie->has_z_augmentation = false; 1.1867 + cie->pointer_encoding = DW_EH_PE_absptr; 1.1868 + cie->instructions = 0; 1.1869 + 1.1870 + // Parse the version number. 1.1871 + if (cie->end - cursor < 1) 1.1872 + return ReportIncomplete(cie); 1.1873 + cie->version = reader_->ReadOneByte(cursor); 1.1874 + cursor++; 1.1875 + 1.1876 + // If we don't recognize the version, we can't parse any more fields of the 1.1877 + // CIE. For DWARF CFI, we handle versions 1 through 3 (there was never a 1.1878 + // version 2 of CFI data). For .eh_frame, we handle versions 1 and 3 as well; 1.1879 + // the difference between those versions seems to be the same as for 1.1880 + // .debug_frame. 1.1881 + if (cie->version < 1 || cie->version > 3) { 1.1882 + reporter_->UnrecognizedVersion(cie->offset, cie->version); 1.1883 + return false; 1.1884 + } 1.1885 + 1.1886 + const char *augmentation_start = cursor; 1.1887 + const void *augmentation_end = 1.1888 + memchr(augmentation_start, '\0', cie->end - augmentation_start); 1.1889 + if (! augmentation_end) return ReportIncomplete(cie); 1.1890 + cursor = static_cast<const char *>(augmentation_end); 1.1891 + cie->augmentation = string(augmentation_start, 1.1892 + cursor - augmentation_start); 1.1893 + // Skip the terminating '\0'. 1.1894 + cursor++; 1.1895 + 1.1896 + // Is this CFI augmented? 1.1897 + if (!cie->augmentation.empty()) { 1.1898 + // Is it an augmentation we recognize? 1.1899 + if (cie->augmentation[0] == DW_Z_augmentation_start) { 1.1900 + // Linux C++ ABI 'z' augmentation, used for exception handling data. 1.1901 + cie->has_z_augmentation = true; 1.1902 + } else { 1.1903 + // Not an augmentation we recognize. Augmentations can have arbitrary 1.1904 + // effects on the form of rest of the content, so we have to give up. 1.1905 + reporter_->UnrecognizedAugmentation(cie->offset, cie->augmentation); 1.1906 + return false; 1.1907 + } 1.1908 + } 1.1909 + 1.1910 + // Parse the code alignment factor. 1.1911 + cie->code_alignment_factor = reader_->ReadUnsignedLEB128(cursor, &len); 1.1912 + if (size_t(cie->end - cursor) < len) return ReportIncomplete(cie); 1.1913 + cursor += len; 1.1914 + 1.1915 + // Parse the data alignment factor. 1.1916 + cie->data_alignment_factor = reader_->ReadSignedLEB128(cursor, &len); 1.1917 + if (size_t(cie->end - cursor) < len) return ReportIncomplete(cie); 1.1918 + cursor += len; 1.1919 + 1.1920 + // Parse the return address register. This is a ubyte in version 1, and 1.1921 + // a ULEB128 in version 3. 1.1922 + if (cie->version == 1) { 1.1923 + if (cursor >= cie->end) return ReportIncomplete(cie); 1.1924 + cie->return_address_register = uint8(*cursor++); 1.1925 + } else { 1.1926 + cie->return_address_register = reader_->ReadUnsignedLEB128(cursor, &len); 1.1927 + if (size_t(cie->end - cursor) < len) return ReportIncomplete(cie); 1.1928 + cursor += len; 1.1929 + } 1.1930 + 1.1931 + // If we have a 'z' augmentation string, find the augmentation data and 1.1932 + // use the augmentation string to parse it. 1.1933 + if (cie->has_z_augmentation) { 1.1934 + uint64_t data_size = reader_->ReadUnsignedLEB128(cursor, &len); 1.1935 + if (size_t(cie->end - cursor) < len + data_size) 1.1936 + return ReportIncomplete(cie); 1.1937 + cursor += len; 1.1938 + const char *data = cursor; 1.1939 + cursor += data_size; 1.1940 + const char *data_end = cursor; 1.1941 + 1.1942 + cie->has_z_lsda = false; 1.1943 + cie->has_z_personality = false; 1.1944 + cie->has_z_signal_frame = false; 1.1945 + 1.1946 + // Walk the augmentation string, and extract values from the 1.1947 + // augmentation data as the string directs. 1.1948 + for (size_t i = 1; i < cie->augmentation.size(); i++) { 1.1949 + switch (cie->augmentation[i]) { 1.1950 + case DW_Z_has_LSDA: 1.1951 + // The CIE's augmentation data holds the language-specific data 1.1952 + // area pointer's encoding, and the FDE's augmentation data holds 1.1953 + // the pointer itself. 1.1954 + cie->has_z_lsda = true; 1.1955 + // Fetch the LSDA encoding from the augmentation data. 1.1956 + if (data >= data_end) return ReportIncomplete(cie); 1.1957 + cie->lsda_encoding = DwarfPointerEncoding(*data++); 1.1958 + if (!reader_->ValidEncoding(cie->lsda_encoding)) { 1.1959 + reporter_->InvalidPointerEncoding(cie->offset, cie->lsda_encoding); 1.1960 + return false; 1.1961 + } 1.1962 + // Don't check if the encoding is usable here --- we haven't 1.1963 + // read the FDE's fields yet, so we're not prepared for 1.1964 + // DW_EH_PE_funcrel, although that's a fine encoding for the 1.1965 + // LSDA to use, since it appears in the FDE. 1.1966 + break; 1.1967 + 1.1968 + case DW_Z_has_personality_routine: 1.1969 + // The CIE's augmentation data holds the personality routine 1.1970 + // pointer's encoding, followed by the pointer itself. 1.1971 + cie->has_z_personality = true; 1.1972 + // Fetch the personality routine pointer's encoding from the 1.1973 + // augmentation data. 1.1974 + if (data >= data_end) return ReportIncomplete(cie); 1.1975 + cie->personality_encoding = DwarfPointerEncoding(*data++); 1.1976 + if (!reader_->ValidEncoding(cie->personality_encoding)) { 1.1977 + reporter_->InvalidPointerEncoding(cie->offset, 1.1978 + cie->personality_encoding); 1.1979 + return false; 1.1980 + } 1.1981 + if (!reader_->UsableEncoding(cie->personality_encoding)) { 1.1982 + reporter_->UnusablePointerEncoding(cie->offset, 1.1983 + cie->personality_encoding); 1.1984 + return false; 1.1985 + } 1.1986 + // Fetch the personality routine's pointer itself from the data. 1.1987 + cie->personality_address = 1.1988 + reader_->ReadEncodedPointer(data, cie->personality_encoding, 1.1989 + &len); 1.1990 + if (len > size_t(data_end - data)) 1.1991 + return ReportIncomplete(cie); 1.1992 + data += len; 1.1993 + break; 1.1994 + 1.1995 + case DW_Z_has_FDE_address_encoding: 1.1996 + // The CIE's augmentation data holds the pointer encoding to use 1.1997 + // for addresses in the FDE. 1.1998 + if (data >= data_end) return ReportIncomplete(cie); 1.1999 + cie->pointer_encoding = DwarfPointerEncoding(*data++); 1.2000 + if (!reader_->ValidEncoding(cie->pointer_encoding)) { 1.2001 + reporter_->InvalidPointerEncoding(cie->offset, 1.2002 + cie->pointer_encoding); 1.2003 + return false; 1.2004 + } 1.2005 + if (!reader_->UsableEncoding(cie->pointer_encoding)) { 1.2006 + reporter_->UnusablePointerEncoding(cie->offset, 1.2007 + cie->pointer_encoding); 1.2008 + return false; 1.2009 + } 1.2010 + break; 1.2011 + 1.2012 + case DW_Z_is_signal_trampoline: 1.2013 + // Frames using this CIE are signal delivery frames. 1.2014 + cie->has_z_signal_frame = true; 1.2015 + break; 1.2016 + 1.2017 + default: 1.2018 + // An augmentation we don't recognize. 1.2019 + reporter_->UnrecognizedAugmentation(cie->offset, cie->augmentation); 1.2020 + return false; 1.2021 + } 1.2022 + } 1.2023 + } 1.2024 + 1.2025 + // The CIE's instructions start here. 1.2026 + cie->instructions = cursor; 1.2027 + 1.2028 + return true; 1.2029 +} 1.2030 + 1.2031 +bool CallFrameInfo::ReadFDEFields(FDE *fde) { 1.2032 + const char *cursor = fde->fields; 1.2033 + size_t size; 1.2034 + 1.2035 + fde->address = reader_->ReadEncodedPointer(cursor, fde->cie->pointer_encoding, 1.2036 + &size); 1.2037 + if (size > size_t(fde->end - cursor)) 1.2038 + return ReportIncomplete(fde); 1.2039 + cursor += size; 1.2040 + reader_->SetFunctionBase(fde->address); 1.2041 + 1.2042 + // For the length, we strip off the upper nybble of the encoding used for 1.2043 + // the starting address. 1.2044 + DwarfPointerEncoding length_encoding = 1.2045 + DwarfPointerEncoding(fde->cie->pointer_encoding & 0x0f); 1.2046 + fde->size = reader_->ReadEncodedPointer(cursor, length_encoding, &size); 1.2047 + if (size > size_t(fde->end - cursor)) 1.2048 + return ReportIncomplete(fde); 1.2049 + cursor += size; 1.2050 + 1.2051 + // If the CIE has a 'z' augmentation string, then augmentation data 1.2052 + // appears here. 1.2053 + if (fde->cie->has_z_augmentation) { 1.2054 + uint64_t data_size = reader_->ReadUnsignedLEB128(cursor, &size); 1.2055 + if (size_t(fde->end - cursor) < size + data_size) 1.2056 + return ReportIncomplete(fde); 1.2057 + cursor += size; 1.2058 + 1.2059 + // In the abstract, we should walk the augmentation string, and extract 1.2060 + // items from the FDE's augmentation data as we encounter augmentation 1.2061 + // string characters that specify their presence: the ordering of items 1.2062 + // in the augmentation string determines the arrangement of values in 1.2063 + // the augmentation data. 1.2064 + // 1.2065 + // In practice, there's only ever one value in FDE augmentation data 1.2066 + // that we support --- the LSDA pointer --- and we have to bail if we 1.2067 + // see any unrecognized augmentation string characters. So if there is 1.2068 + // anything here at all, we know what it is, and where it starts. 1.2069 + if (fde->cie->has_z_lsda) { 1.2070 + // Check whether the LSDA's pointer encoding is usable now: only once 1.2071 + // we've parsed the FDE's starting address do we call reader_-> 1.2072 + // SetFunctionBase, so that the DW_EH_PE_funcrel encoding becomes 1.2073 + // usable. 1.2074 + if (!reader_->UsableEncoding(fde->cie->lsda_encoding)) { 1.2075 + reporter_->UnusablePointerEncoding(fde->cie->offset, 1.2076 + fde->cie->lsda_encoding); 1.2077 + return false; 1.2078 + } 1.2079 + 1.2080 + fde->lsda_address = 1.2081 + reader_->ReadEncodedPointer(cursor, fde->cie->lsda_encoding, &size); 1.2082 + if (size > data_size) 1.2083 + return ReportIncomplete(fde); 1.2084 + // Ideally, we would also complain here if there were unconsumed 1.2085 + // augmentation data. 1.2086 + } 1.2087 + 1.2088 + cursor += data_size; 1.2089 + } 1.2090 + 1.2091 + // The FDE's instructions start after those. 1.2092 + fde->instructions = cursor; 1.2093 + 1.2094 + return true; 1.2095 +} 1.2096 + 1.2097 +bool CallFrameInfo::Start() { 1.2098 + const char *buffer_end = buffer_ + buffer_length_; 1.2099 + const char *cursor; 1.2100 + bool all_ok = true; 1.2101 + const char *entry_end; 1.2102 + bool ok; 1.2103 + 1.2104 + // Traverse all the entries in buffer_, skipping CIEs and offering 1.2105 + // FDEs to the handler. 1.2106 + for (cursor = buffer_; cursor < buffer_end; 1.2107 + cursor = entry_end, all_ok = all_ok && ok) { 1.2108 + FDE fde; 1.2109 + 1.2110 + // Make it easy to skip this entry with 'continue': assume that 1.2111 + // things are not okay until we've checked all the data, and 1.2112 + // prepare the address of the next entry. 1.2113 + ok = false; 1.2114 + 1.2115 + // Read the entry's prologue. 1.2116 + if (!ReadEntryPrologue(cursor, &fde)) { 1.2117 + if (!fde.end) { 1.2118 + // If we couldn't even figure out this entry's extent, then we 1.2119 + // must stop processing entries altogether. 1.2120 + all_ok = false; 1.2121 + break; 1.2122 + } 1.2123 + entry_end = fde.end; 1.2124 + continue; 1.2125 + } 1.2126 + 1.2127 + // The next iteration picks up after this entry. 1.2128 + entry_end = fde.end; 1.2129 + 1.2130 + // Did we see an .eh_frame terminating mark? 1.2131 + if (fde.kind == kTerminator) { 1.2132 + // If there appears to be more data left in the section after the 1.2133 + // terminating mark, warn the user. But this is just a warning; 1.2134 + // we leave all_ok true. 1.2135 + if (fde.end < buffer_end) reporter_->EarlyEHTerminator(fde.offset); 1.2136 + break; 1.2137 + } 1.2138 + 1.2139 + // In this loop, we skip CIEs. We only parse them fully when we 1.2140 + // parse an FDE that refers to them. This limits our memory 1.2141 + // consumption (beyond the buffer itself) to that needed to 1.2142 + // process the largest single entry. 1.2143 + if (fde.kind != kFDE) { 1.2144 + ok = true; 1.2145 + continue; 1.2146 + } 1.2147 + 1.2148 + // Validate the CIE pointer. 1.2149 + if (fde.id > buffer_length_) { 1.2150 + reporter_->CIEPointerOutOfRange(fde.offset, fde.id); 1.2151 + continue; 1.2152 + } 1.2153 + 1.2154 + CIE cie; 1.2155 + 1.2156 + // Parse this FDE's CIE header. 1.2157 + if (!ReadEntryPrologue(buffer_ + fde.id, &cie)) 1.2158 + continue; 1.2159 + // This had better be an actual CIE. 1.2160 + if (cie.kind != kCIE) { 1.2161 + reporter_->BadCIEId(fde.offset, fde.id); 1.2162 + continue; 1.2163 + } 1.2164 + if (!ReadCIEFields(&cie)) 1.2165 + continue; 1.2166 + 1.2167 + // We now have the values that govern both the CIE and the FDE. 1.2168 + cie.cie = &cie; 1.2169 + fde.cie = &cie; 1.2170 + 1.2171 + // Parse the FDE's header. 1.2172 + if (!ReadFDEFields(&fde)) 1.2173 + continue; 1.2174 + 1.2175 + // Call Entry to ask the consumer if they're interested. 1.2176 + if (!handler_->Entry(fde.offset, fde.address, fde.size, 1.2177 + cie.version, cie.augmentation, 1.2178 + cie.return_address_register)) { 1.2179 + // The handler isn't interested in this entry. That's not an error. 1.2180 + ok = true; 1.2181 + continue; 1.2182 + } 1.2183 + 1.2184 + if (cie.has_z_augmentation) { 1.2185 + // Report the personality routine address, if we have one. 1.2186 + if (cie.has_z_personality) { 1.2187 + if (!handler_ 1.2188 + ->PersonalityRoutine(cie.personality_address, 1.2189 + IsIndirectEncoding(cie.personality_encoding))) 1.2190 + continue; 1.2191 + } 1.2192 + 1.2193 + // Report the language-specific data area address, if we have one. 1.2194 + if (cie.has_z_lsda) { 1.2195 + if (!handler_ 1.2196 + ->LanguageSpecificDataArea(fde.lsda_address, 1.2197 + IsIndirectEncoding(cie.lsda_encoding))) 1.2198 + continue; 1.2199 + } 1.2200 + 1.2201 + // If this is a signal-handling frame, report that. 1.2202 + if (cie.has_z_signal_frame) { 1.2203 + if (!handler_->SignalHandler()) 1.2204 + continue; 1.2205 + } 1.2206 + } 1.2207 + 1.2208 + // Interpret the CIE's instructions, and then the FDE's instructions. 1.2209 + State state(reader_, handler_, reporter_, fde.address); 1.2210 + ok = state.InterpretCIE(cie) && state.InterpretFDE(fde); 1.2211 + 1.2212 + // Tell the ByteReader that the function start address from the 1.2213 + // FDE header is no longer valid. 1.2214 + reader_->ClearFunctionBase(); 1.2215 + 1.2216 + // Report the end of the entry. 1.2217 + handler_->End(); 1.2218 + } 1.2219 + 1.2220 + return all_ok; 1.2221 +} 1.2222 + 1.2223 +const char *CallFrameInfo::KindName(EntryKind kind) { 1.2224 + if (kind == CallFrameInfo::kUnknown) 1.2225 + return "entry"; 1.2226 + else if (kind == CallFrameInfo::kCIE) 1.2227 + return "common information entry"; 1.2228 + else if (kind == CallFrameInfo::kFDE) 1.2229 + return "frame description entry"; 1.2230 + else { 1.2231 + assert (kind == CallFrameInfo::kTerminator); 1.2232 + return ".eh_frame sequence terminator"; 1.2233 + } 1.2234 +} 1.2235 + 1.2236 +bool CallFrameInfo::ReportIncomplete(Entry *entry) { 1.2237 + reporter_->Incomplete(entry->offset, entry->kind); 1.2238 + return false; 1.2239 +} 1.2240 + 1.2241 +void CallFrameInfo::Reporter::Incomplete(uint64 offset, 1.2242 + CallFrameInfo::EntryKind kind) { 1.2243 + fprintf(stderr, 1.2244 + "%s: CFI %s at offset 0x%llx in '%s': entry ends early\n", 1.2245 + filename_.c_str(), CallFrameInfo::KindName(kind), offset, 1.2246 + section_.c_str()); 1.2247 +} 1.2248 + 1.2249 +void CallFrameInfo::Reporter::EarlyEHTerminator(uint64 offset) { 1.2250 + fprintf(stderr, 1.2251 + "%s: CFI at offset 0x%llx in '%s': saw end-of-data marker" 1.2252 + " before end of section contents\n", 1.2253 + filename_.c_str(), offset, section_.c_str()); 1.2254 +} 1.2255 + 1.2256 +void CallFrameInfo::Reporter::CIEPointerOutOfRange(uint64 offset, 1.2257 + uint64 cie_offset) { 1.2258 + fprintf(stderr, 1.2259 + "%s: CFI frame description entry at offset 0x%llx in '%s':" 1.2260 + " CIE pointer is out of range: 0x%llx\n", 1.2261 + filename_.c_str(), offset, section_.c_str(), cie_offset); 1.2262 +} 1.2263 + 1.2264 +void CallFrameInfo::Reporter::BadCIEId(uint64 offset, uint64 cie_offset) { 1.2265 + fprintf(stderr, 1.2266 + "%s: CFI frame description entry at offset 0x%llx in '%s':" 1.2267 + " CIE pointer does not point to a CIE: 0x%llx\n", 1.2268 + filename_.c_str(), offset, section_.c_str(), cie_offset); 1.2269 +} 1.2270 + 1.2271 +void CallFrameInfo::Reporter::UnrecognizedVersion(uint64 offset, int version) { 1.2272 + fprintf(stderr, 1.2273 + "%s: CFI frame description entry at offset 0x%llx in '%s':" 1.2274 + " CIE specifies unrecognized version: %d\n", 1.2275 + filename_.c_str(), offset, section_.c_str(), version); 1.2276 +} 1.2277 + 1.2278 +void CallFrameInfo::Reporter::UnrecognizedAugmentation(uint64 offset, 1.2279 + const string &aug) { 1.2280 + fprintf(stderr, 1.2281 + "%s: CFI frame description entry at offset 0x%llx in '%s':" 1.2282 + " CIE specifies unrecognized augmentation: '%s'\n", 1.2283 + filename_.c_str(), offset, section_.c_str(), aug.c_str()); 1.2284 +} 1.2285 + 1.2286 +void CallFrameInfo::Reporter::InvalidPointerEncoding(uint64 offset, 1.2287 + uint8 encoding) { 1.2288 + fprintf(stderr, 1.2289 + "%s: CFI common information entry at offset 0x%llx in '%s':" 1.2290 + " 'z' augmentation specifies invalid pointer encoding: 0x%02x\n", 1.2291 + filename_.c_str(), offset, section_.c_str(), encoding); 1.2292 +} 1.2293 + 1.2294 +void CallFrameInfo::Reporter::UnusablePointerEncoding(uint64 offset, 1.2295 + uint8 encoding) { 1.2296 + fprintf(stderr, 1.2297 + "%s: CFI common information entry at offset 0x%llx in '%s':" 1.2298 + " 'z' augmentation specifies a pointer encoding for which" 1.2299 + " we have no base address: 0x%02x\n", 1.2300 + filename_.c_str(), offset, section_.c_str(), encoding); 1.2301 +} 1.2302 + 1.2303 +void CallFrameInfo::Reporter::RestoreInCIE(uint64 offset, uint64 insn_offset) { 1.2304 + fprintf(stderr, 1.2305 + "%s: CFI common information entry at offset 0x%llx in '%s':" 1.2306 + " the DW_CFA_restore instruction at offset 0x%llx" 1.2307 + " cannot be used in a common information entry\n", 1.2308 + filename_.c_str(), offset, section_.c_str(), insn_offset); 1.2309 +} 1.2310 + 1.2311 +void CallFrameInfo::Reporter::BadInstruction(uint64 offset, 1.2312 + CallFrameInfo::EntryKind kind, 1.2313 + uint64 insn_offset) { 1.2314 + fprintf(stderr, 1.2315 + "%s: CFI %s at offset 0x%llx in section '%s':" 1.2316 + " the instruction at offset 0x%llx is unrecognized\n", 1.2317 + filename_.c_str(), CallFrameInfo::KindName(kind), 1.2318 + offset, section_.c_str(), insn_offset); 1.2319 +} 1.2320 + 1.2321 +void CallFrameInfo::Reporter::NoCFARule(uint64 offset, 1.2322 + CallFrameInfo::EntryKind kind, 1.2323 + uint64 insn_offset) { 1.2324 + fprintf(stderr, 1.2325 + "%s: CFI %s at offset 0x%llx in section '%s':" 1.2326 + " the instruction at offset 0x%llx assumes that a CFA rule has" 1.2327 + " been set, but none has been set\n", 1.2328 + filename_.c_str(), CallFrameInfo::KindName(kind), offset, 1.2329 + section_.c_str(), insn_offset); 1.2330 +} 1.2331 + 1.2332 +void CallFrameInfo::Reporter::EmptyStateStack(uint64 offset, 1.2333 + CallFrameInfo::EntryKind kind, 1.2334 + uint64 insn_offset) { 1.2335 + fprintf(stderr, 1.2336 + "%s: CFI %s at offset 0x%llx in section '%s':" 1.2337 + " the DW_CFA_restore_state instruction at offset 0x%llx" 1.2338 + " should pop a saved state from the stack, but the stack is empty\n", 1.2339 + filename_.c_str(), CallFrameInfo::KindName(kind), offset, 1.2340 + section_.c_str(), insn_offset); 1.2341 +} 1.2342 + 1.2343 +void CallFrameInfo::Reporter::ClearingCFARule(uint64 offset, 1.2344 + CallFrameInfo::EntryKind kind, 1.2345 + uint64 insn_offset) { 1.2346 + fprintf(stderr, 1.2347 + "%s: CFI %s at offset 0x%llx in section '%s':" 1.2348 + " the DW_CFA_restore_state instruction at offset 0x%llx" 1.2349 + " would clear the CFA rule in effect\n", 1.2350 + filename_.c_str(), CallFrameInfo::KindName(kind), offset, 1.2351 + section_.c_str(), insn_offset); 1.2352 +} 1.2353 + 1.2354 +} // namespace dwarf2reader