|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
|
3 |
|
4 // Copyright 2006, 2010 Google Inc. All Rights Reserved. |
|
5 // |
|
6 // Redistribution and use in source and binary forms, with or without |
|
7 // modification, are permitted provided that the following conditions are |
|
8 // met: |
|
9 // |
|
10 // * Redistributions of source code must retain the above copyright |
|
11 // notice, this list of conditions and the following disclaimer. |
|
12 // * Redistributions in binary form must reproduce the above |
|
13 // copyright notice, this list of conditions and the following disclaimer |
|
14 // in the documentation and/or other materials provided with the |
|
15 // distribution. |
|
16 // * Neither the name of Google Inc. nor the names of its |
|
17 // contributors may be used to endorse or promote products derived from |
|
18 // this software without specific prior written permission. |
|
19 // |
|
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
31 |
|
32 // Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com> |
|
33 |
|
34 // This file is derived from the following files in |
|
35 // toolkit/crashreporter/google-breakpad: |
|
36 // src/common/dwarf/types.h |
|
37 // src/common/dwarf/dwarf2enums.h |
|
38 // src/common/dwarf/bytereader.h |
|
39 // src/common/dwarf_cfi_to_module.h |
|
40 // src/common/dwarf/dwarf2reader.h |
|
41 |
|
42 #ifndef LulDwarfExt_h |
|
43 #define LulDwarfExt_h |
|
44 |
|
45 #include <stdint.h> |
|
46 |
|
47 #include "mozilla/Assertions.h" |
|
48 |
|
49 #include "LulDwarfSummariser.h" |
|
50 |
|
51 typedef signed char int8; |
|
52 typedef short int16; |
|
53 typedef int int32; |
|
54 typedef long long int64; |
|
55 |
|
56 typedef unsigned char uint8; |
|
57 typedef unsigned short uint16; |
|
58 typedef unsigned int uint32; |
|
59 typedef unsigned long long uint64; |
|
60 |
|
61 #ifdef __PTRDIFF_TYPE__ |
|
62 typedef __PTRDIFF_TYPE__ intptr; |
|
63 typedef unsigned __PTRDIFF_TYPE__ uintptr; |
|
64 #else |
|
65 #error "Can't find pointer-sized integral types." |
|
66 #endif |
|
67 |
|
68 |
|
69 namespace lul { |
|
70 |
|
71 // Exception handling frame description pointer formats, as described |
|
72 // by the Linux Standard Base Core Specification 4.0, section 11.5, |
|
73 // DWARF Extensions. |
|
74 enum DwarfPointerEncoding |
|
75 { |
|
76 DW_EH_PE_absptr = 0x00, |
|
77 DW_EH_PE_omit = 0xff, |
|
78 DW_EH_PE_uleb128 = 0x01, |
|
79 DW_EH_PE_udata2 = 0x02, |
|
80 DW_EH_PE_udata4 = 0x03, |
|
81 DW_EH_PE_udata8 = 0x04, |
|
82 DW_EH_PE_sleb128 = 0x09, |
|
83 DW_EH_PE_sdata2 = 0x0A, |
|
84 DW_EH_PE_sdata4 = 0x0B, |
|
85 DW_EH_PE_sdata8 = 0x0C, |
|
86 DW_EH_PE_pcrel = 0x10, |
|
87 DW_EH_PE_textrel = 0x20, |
|
88 DW_EH_PE_datarel = 0x30, |
|
89 DW_EH_PE_funcrel = 0x40, |
|
90 DW_EH_PE_aligned = 0x50, |
|
91 |
|
92 // The GNU toolchain sources define this enum value as well, |
|
93 // simply to help classify the lower nybble values into signed and |
|
94 // unsigned groups. |
|
95 DW_EH_PE_signed = 0x08, |
|
96 |
|
97 // This is not documented in LSB 4.0, but it is used in both the |
|
98 // Linux and OS X toolchains. It can be added to any other |
|
99 // encoding (except DW_EH_PE_aligned), and indicates that the |
|
100 // encoded value represents the address at which the true address |
|
101 // is stored, not the true address itself. |
|
102 DW_EH_PE_indirect = 0x80 |
|
103 }; |
|
104 |
|
105 |
|
106 // We can't use the obvious name of LITTLE_ENDIAN and BIG_ENDIAN |
|
107 // because it conflicts with a macro |
|
108 enum Endianness { |
|
109 ENDIANNESS_BIG, |
|
110 ENDIANNESS_LITTLE |
|
111 }; |
|
112 |
|
113 // A ByteReader knows how to read single- and multi-byte values of |
|
114 // various endiannesses, sizes, and encodings, as used in DWARF |
|
115 // debugging information and Linux C++ exception handling data. |
|
116 class ByteReader { |
|
117 public: |
|
118 // Construct a ByteReader capable of reading one-, two-, four-, and |
|
119 // eight-byte values according to ENDIANNESS, absolute machine-sized |
|
120 // addresses, DWARF-style "initial length" values, signed and |
|
121 // unsigned LEB128 numbers, and Linux C++ exception handling data's |
|
122 // encoded pointers. |
|
123 explicit ByteReader(enum Endianness endianness); |
|
124 virtual ~ByteReader(); |
|
125 |
|
126 // Read a single byte from BUFFER and return it as an unsigned 8 bit |
|
127 // number. |
|
128 uint8 ReadOneByte(const char* buffer) const; |
|
129 |
|
130 // Read two bytes from BUFFER and return them as an unsigned 16 bit |
|
131 // number, using this ByteReader's endianness. |
|
132 uint16 ReadTwoBytes(const char* buffer) const; |
|
133 |
|
134 // Read four bytes from BUFFER and return them as an unsigned 32 bit |
|
135 // number, using this ByteReader's endianness. This function returns |
|
136 // a uint64 so that it is compatible with ReadAddress and |
|
137 // ReadOffset. The number it returns will never be outside the range |
|
138 // of an unsigned 32 bit integer. |
|
139 uint64 ReadFourBytes(const char* buffer) const; |
|
140 |
|
141 // Read eight bytes from BUFFER and return them as an unsigned 64 |
|
142 // bit number, using this ByteReader's endianness. |
|
143 uint64 ReadEightBytes(const char* buffer) const; |
|
144 |
|
145 // Read an unsigned LEB128 (Little Endian Base 128) number from |
|
146 // BUFFER and return it as an unsigned 64 bit integer. Set LEN to |
|
147 // the number of bytes read. |
|
148 // |
|
149 // The unsigned LEB128 representation of an integer N is a variable |
|
150 // number of bytes: |
|
151 // |
|
152 // - If N is between 0 and 0x7f, then its unsigned LEB128 |
|
153 // representation is a single byte whose value is N. |
|
154 // |
|
155 // - Otherwise, its unsigned LEB128 representation is (N & 0x7f) | |
|
156 // 0x80, followed by the unsigned LEB128 representation of N / |
|
157 // 128, rounded towards negative infinity. |
|
158 // |
|
159 // In other words, we break VALUE into groups of seven bits, put |
|
160 // them in little-endian order, and then write them as eight-bit |
|
161 // bytes with the high bit on all but the last. |
|
162 uint64 ReadUnsignedLEB128(const char* buffer, size_t* len) const; |
|
163 |
|
164 // Read a signed LEB128 number from BUFFER and return it as an |
|
165 // signed 64 bit integer. Set LEN to the number of bytes read. |
|
166 // |
|
167 // The signed LEB128 representation of an integer N is a variable |
|
168 // number of bytes: |
|
169 // |
|
170 // - If N is between -0x40 and 0x3f, then its signed LEB128 |
|
171 // representation is a single byte whose value is N in two's |
|
172 // complement. |
|
173 // |
|
174 // - Otherwise, its signed LEB128 representation is (N & 0x7f) | |
|
175 // 0x80, followed by the signed LEB128 representation of N / 128, |
|
176 // rounded towards negative infinity. |
|
177 // |
|
178 // In other words, we break VALUE into groups of seven bits, put |
|
179 // them in little-endian order, and then write them as eight-bit |
|
180 // bytes with the high bit on all but the last. |
|
181 int64 ReadSignedLEB128(const char* buffer, size_t* len) const; |
|
182 |
|
183 // Indicate that addresses on this architecture are SIZE bytes long. SIZE |
|
184 // must be either 4 or 8. (DWARF allows addresses to be any number of |
|
185 // bytes in length from 1 to 255, but we only support 32- and 64-bit |
|
186 // addresses at the moment.) You must call this before using the |
|
187 // ReadAddress member function. |
|
188 // |
|
189 // For data in a .debug_info section, or something that .debug_info |
|
190 // refers to like line number or macro data, the compilation unit |
|
191 // header's address_size field indicates the address size to use. Call |
|
192 // frame information doesn't indicate its address size (a shortcoming of |
|
193 // the spec); you must supply the appropriate size based on the |
|
194 // architecture of the target machine. |
|
195 void SetAddressSize(uint8 size); |
|
196 |
|
197 // Return the current address size, in bytes. This is either 4, |
|
198 // indicating 32-bit addresses, or 8, indicating 64-bit addresses. |
|
199 uint8 AddressSize() const { return address_size_; } |
|
200 |
|
201 // Read an address from BUFFER and return it as an unsigned 64 bit |
|
202 // integer, respecting this ByteReader's endianness and address size. You |
|
203 // must call SetAddressSize before calling this function. |
|
204 uint64 ReadAddress(const char* buffer) const; |
|
205 |
|
206 // DWARF actually defines two slightly different formats: 32-bit DWARF |
|
207 // and 64-bit DWARF. This is *not* related to the size of registers or |
|
208 // addresses on the target machine; it refers only to the size of section |
|
209 // offsets and data lengths appearing in the DWARF data. One only needs |
|
210 // 64-bit DWARF when the debugging data itself is larger than 4GiB. |
|
211 // 32-bit DWARF can handle x86_64 or PPC64 code just fine, unless the |
|
212 // debugging data itself is very large. |
|
213 // |
|
214 // DWARF information identifies itself as 32-bit or 64-bit DWARF: each |
|
215 // compilation unit and call frame information entry begins with an |
|
216 // "initial length" field, which, in addition to giving the length of the |
|
217 // data, also indicates the size of section offsets and lengths appearing |
|
218 // in that data. The ReadInitialLength member function, below, reads an |
|
219 // initial length and sets the ByteReader's offset size as a side effect. |
|
220 // Thus, in the normal process of reading DWARF data, the appropriate |
|
221 // offset size is set automatically. So, you should only need to call |
|
222 // SetOffsetSize if you are using the same ByteReader to jump from the |
|
223 // midst of one block of DWARF data into another. |
|
224 |
|
225 // Read a DWARF "initial length" field from START, and return it as |
|
226 // an unsigned 64 bit integer, respecting this ByteReader's |
|
227 // endianness. Set *LEN to the length of the initial length in |
|
228 // bytes, either four or twelve. As a side effect, set this |
|
229 // ByteReader's offset size to either 4 (if we see a 32-bit DWARF |
|
230 // initial length) or 8 (if we see a 64-bit DWARF initial length). |
|
231 // |
|
232 // A DWARF initial length is either: |
|
233 // |
|
234 // - a byte count stored as an unsigned 32-bit value less than |
|
235 // 0xffffff00, indicating that the data whose length is being |
|
236 // measured uses the 32-bit DWARF format, or |
|
237 // |
|
238 // - The 32-bit value 0xffffffff, followed by a 64-bit byte count, |
|
239 // indicating that the data whose length is being measured uses |
|
240 // the 64-bit DWARF format. |
|
241 uint64 ReadInitialLength(const char* start, size_t* len); |
|
242 |
|
243 // Read an offset from BUFFER and return it as an unsigned 64 bit |
|
244 // integer, respecting the ByteReader's endianness. In 32-bit DWARF, the |
|
245 // offset is 4 bytes long; in 64-bit DWARF, the offset is eight bytes |
|
246 // long. You must call ReadInitialLength or SetOffsetSize before calling |
|
247 // this function; see the comments above for details. |
|
248 uint64 ReadOffset(const char* buffer) const; |
|
249 |
|
250 // Return the current offset size, in bytes. |
|
251 // A return value of 4 indicates that we are reading 32-bit DWARF. |
|
252 // A return value of 8 indicates that we are reading 64-bit DWARF. |
|
253 uint8 OffsetSize() const { return offset_size_; } |
|
254 |
|
255 // Indicate that section offsets and lengths are SIZE bytes long. SIZE |
|
256 // must be either 4 (meaning 32-bit DWARF) or 8 (meaning 64-bit DWARF). |
|
257 // Usually, you should not call this function yourself; instead, let a |
|
258 // call to ReadInitialLength establish the data's offset size |
|
259 // automatically. |
|
260 void SetOffsetSize(uint8 size); |
|
261 |
|
262 // The Linux C++ ABI uses a variant of DWARF call frame information |
|
263 // for exception handling. This data is included in the program's |
|
264 // address space as the ".eh_frame" section, and intepreted at |
|
265 // runtime to walk the stack, find exception handlers, and run |
|
266 // cleanup code. The format is mostly the same as DWARF CFI, with |
|
267 // some adjustments made to provide the additional |
|
268 // exception-handling data, and to make the data easier to work with |
|
269 // in memory --- for example, to allow it to be placed in read-only |
|
270 // memory even when describing position-independent code. |
|
271 // |
|
272 // In particular, exception handling data can select a number of |
|
273 // different encodings for pointers that appear in the data, as |
|
274 // described by the DwarfPointerEncoding enum. There are actually |
|
275 // four axes(!) to the encoding: |
|
276 // |
|
277 // - The pointer size: pointers can be 2, 4, or 8 bytes long, or use |
|
278 // the DWARF LEB128 encoding. |
|
279 // |
|
280 // - The pointer's signedness: pointers can be signed or unsigned. |
|
281 // |
|
282 // - The pointer's base address: the data stored in the exception |
|
283 // handling data can be the actual address (that is, an absolute |
|
284 // pointer), or relative to one of a number of different base |
|
285 // addreses --- including that of the encoded pointer itself, for |
|
286 // a form of "pc-relative" addressing. |
|
287 // |
|
288 // - The pointer may be indirect: it may be the address where the |
|
289 // true pointer is stored. (This is used to refer to things via |
|
290 // global offset table entries, program linkage table entries, or |
|
291 // other tricks used in position-independent code.) |
|
292 // |
|
293 // There are also two options that fall outside that matrix |
|
294 // altogether: the pointer may be omitted, or it may have padding to |
|
295 // align it on an appropriate address boundary. (That last option |
|
296 // may seem like it should be just another axis, but it is not.) |
|
297 |
|
298 // Indicate that the exception handling data is loaded starting at |
|
299 // SECTION_BASE, and that the start of its buffer in our own memory |
|
300 // is BUFFER_BASE. This allows us to find the address that a given |
|
301 // byte in our buffer would have when loaded into the program the |
|
302 // data describes. We need this to resolve DW_EH_PE_pcrel pointers. |
|
303 void SetCFIDataBase(uint64 section_base, const char *buffer_base); |
|
304 |
|
305 // Indicate that the base address of the program's ".text" section |
|
306 // is TEXT_BASE. We need this to resolve DW_EH_PE_textrel pointers. |
|
307 void SetTextBase(uint64 text_base); |
|
308 |
|
309 // Indicate that the base address for DW_EH_PE_datarel pointers is |
|
310 // DATA_BASE. The proper value depends on the ABI; it is usually the |
|
311 // address of the global offset table, held in a designated register in |
|
312 // position-independent code. You will need to look at the startup code |
|
313 // for the target system to be sure. I tried; my eyes bled. |
|
314 void SetDataBase(uint64 data_base); |
|
315 |
|
316 // Indicate that the base address for the FDE we are processing is |
|
317 // FUNCTION_BASE. This is the start address of DW_EH_PE_funcrel |
|
318 // pointers. (This encoding does not seem to be used by the GNU |
|
319 // toolchain.) |
|
320 void SetFunctionBase(uint64 function_base); |
|
321 |
|
322 // Indicate that we are no longer processing any FDE, so any use of |
|
323 // a DW_EH_PE_funcrel encoding is an error. |
|
324 void ClearFunctionBase(); |
|
325 |
|
326 // Return true if ENCODING is a valid pointer encoding. |
|
327 bool ValidEncoding(DwarfPointerEncoding encoding) const; |
|
328 |
|
329 // Return true if we have all the information we need to read a |
|
330 // pointer that uses ENCODING. This checks that the appropriate |
|
331 // SetFooBase function for ENCODING has been called. |
|
332 bool UsableEncoding(DwarfPointerEncoding encoding) const; |
|
333 |
|
334 // Read an encoded pointer from BUFFER using ENCODING; return the |
|
335 // absolute address it represents, and set *LEN to the pointer's |
|
336 // length in bytes, including any padding for aligned pointers. |
|
337 // |
|
338 // This function calls 'abort' if ENCODING is invalid or refers to a |
|
339 // base address this reader hasn't been given, so you should check |
|
340 // with ValidEncoding and UsableEncoding first if you would rather |
|
341 // die in a more helpful way. |
|
342 uint64 ReadEncodedPointer(const char *buffer, DwarfPointerEncoding encoding, |
|
343 size_t *len) const; |
|
344 |
|
345 private: |
|
346 |
|
347 // Function pointer type for our address and offset readers. |
|
348 typedef uint64 (ByteReader::*AddressReader)(const char*) const; |
|
349 |
|
350 // Read an offset from BUFFER and return it as an unsigned 64 bit |
|
351 // integer. DWARF2/3 define offsets as either 4 or 8 bytes, |
|
352 // generally depending on the amount of DWARF2/3 info present. |
|
353 // This function pointer gets set by SetOffsetSize. |
|
354 AddressReader offset_reader_; |
|
355 |
|
356 // Read an address from BUFFER and return it as an unsigned 64 bit |
|
357 // integer. DWARF2/3 allow addresses to be any size from 0-255 |
|
358 // bytes currently. Internally we support 4 and 8 byte addresses, |
|
359 // and will CHECK on anything else. |
|
360 // This function pointer gets set by SetAddressSize. |
|
361 AddressReader address_reader_; |
|
362 |
|
363 Endianness endian_; |
|
364 uint8 address_size_; |
|
365 uint8 offset_size_; |
|
366 |
|
367 // Base addresses for Linux C++ exception handling data's encoded pointers. |
|
368 bool have_section_base_, have_text_base_, have_data_base_; |
|
369 bool have_function_base_; |
|
370 uint64 section_base_; |
|
371 uint64 text_base_, data_base_, function_base_; |
|
372 const char *buffer_base_; |
|
373 }; |
|
374 |
|
375 |
|
376 inline uint8 ByteReader::ReadOneByte(const char* buffer) const { |
|
377 return buffer[0]; |
|
378 } |
|
379 |
|
380 inline uint16 ByteReader::ReadTwoBytes(const char* signed_buffer) const { |
|
381 const unsigned char *buffer |
|
382 = reinterpret_cast<const unsigned char *>(signed_buffer); |
|
383 const uint16 buffer0 = buffer[0]; |
|
384 const uint16 buffer1 = buffer[1]; |
|
385 if (endian_ == ENDIANNESS_LITTLE) { |
|
386 return buffer0 | buffer1 << 8; |
|
387 } else { |
|
388 return buffer1 | buffer0 << 8; |
|
389 } |
|
390 } |
|
391 |
|
392 inline uint64 ByteReader::ReadFourBytes(const char* signed_buffer) const { |
|
393 const unsigned char *buffer |
|
394 = reinterpret_cast<const unsigned char *>(signed_buffer); |
|
395 const uint32 buffer0 = buffer[0]; |
|
396 const uint32 buffer1 = buffer[1]; |
|
397 const uint32 buffer2 = buffer[2]; |
|
398 const uint32 buffer3 = buffer[3]; |
|
399 if (endian_ == ENDIANNESS_LITTLE) { |
|
400 return buffer0 | buffer1 << 8 | buffer2 << 16 | buffer3 << 24; |
|
401 } else { |
|
402 return buffer3 | buffer2 << 8 | buffer1 << 16 | buffer0 << 24; |
|
403 } |
|
404 } |
|
405 |
|
406 inline uint64 ByteReader::ReadEightBytes(const char* signed_buffer) const { |
|
407 const unsigned char *buffer |
|
408 = reinterpret_cast<const unsigned char *>(signed_buffer); |
|
409 const uint64 buffer0 = buffer[0]; |
|
410 const uint64 buffer1 = buffer[1]; |
|
411 const uint64 buffer2 = buffer[2]; |
|
412 const uint64 buffer3 = buffer[3]; |
|
413 const uint64 buffer4 = buffer[4]; |
|
414 const uint64 buffer5 = buffer[5]; |
|
415 const uint64 buffer6 = buffer[6]; |
|
416 const uint64 buffer7 = buffer[7]; |
|
417 if (endian_ == ENDIANNESS_LITTLE) { |
|
418 return buffer0 | buffer1 << 8 | buffer2 << 16 | buffer3 << 24 | |
|
419 buffer4 << 32 | buffer5 << 40 | buffer6 << 48 | buffer7 << 56; |
|
420 } else { |
|
421 return buffer7 | buffer6 << 8 | buffer5 << 16 | buffer4 << 24 | |
|
422 buffer3 << 32 | buffer2 << 40 | buffer1 << 48 | buffer0 << 56; |
|
423 } |
|
424 } |
|
425 |
|
426 // Read an unsigned LEB128 number. Each byte contains 7 bits of |
|
427 // information, plus one bit saying whether the number continues or |
|
428 // not. |
|
429 |
|
430 inline uint64 ByteReader::ReadUnsignedLEB128(const char* buffer, |
|
431 size_t* len) const { |
|
432 uint64 result = 0; |
|
433 size_t num_read = 0; |
|
434 unsigned int shift = 0; |
|
435 unsigned char byte; |
|
436 |
|
437 do { |
|
438 byte = *buffer++; |
|
439 num_read++; |
|
440 |
|
441 result |= (static_cast<uint64>(byte & 0x7f)) << shift; |
|
442 |
|
443 shift += 7; |
|
444 |
|
445 } while (byte & 0x80); |
|
446 |
|
447 *len = num_read; |
|
448 |
|
449 return result; |
|
450 } |
|
451 |
|
452 // Read a signed LEB128 number. These are like regular LEB128 |
|
453 // numbers, except the last byte may have a sign bit set. |
|
454 |
|
455 inline int64 ByteReader::ReadSignedLEB128(const char* buffer, |
|
456 size_t* len) const { |
|
457 int64 result = 0; |
|
458 unsigned int shift = 0; |
|
459 size_t num_read = 0; |
|
460 unsigned char byte; |
|
461 |
|
462 do { |
|
463 byte = *buffer++; |
|
464 num_read++; |
|
465 result |= (static_cast<uint64>(byte & 0x7f) << shift); |
|
466 shift += 7; |
|
467 } while (byte & 0x80); |
|
468 |
|
469 if ((shift < 8 * sizeof (result)) && (byte & 0x40)) |
|
470 result |= -((static_cast<int64>(1)) << shift); |
|
471 *len = num_read; |
|
472 return result; |
|
473 } |
|
474 |
|
475 inline uint64 ByteReader::ReadOffset(const char* buffer) const { |
|
476 MOZ_ASSERT(this->offset_reader_); |
|
477 return (this->*offset_reader_)(buffer); |
|
478 } |
|
479 |
|
480 inline uint64 ByteReader::ReadAddress(const char* buffer) const { |
|
481 MOZ_ASSERT(this->address_reader_); |
|
482 return (this->*address_reader_)(buffer); |
|
483 } |
|
484 |
|
485 inline void ByteReader::SetCFIDataBase(uint64 section_base, |
|
486 const char *buffer_base) { |
|
487 section_base_ = section_base; |
|
488 buffer_base_ = buffer_base; |
|
489 have_section_base_ = true; |
|
490 } |
|
491 |
|
492 inline void ByteReader::SetTextBase(uint64 text_base) { |
|
493 text_base_ = text_base; |
|
494 have_text_base_ = true; |
|
495 } |
|
496 |
|
497 inline void ByteReader::SetDataBase(uint64 data_base) { |
|
498 data_base_ = data_base; |
|
499 have_data_base_ = true; |
|
500 } |
|
501 |
|
502 inline void ByteReader::SetFunctionBase(uint64 function_base) { |
|
503 function_base_ = function_base; |
|
504 have_function_base_ = true; |
|
505 } |
|
506 |
|
507 inline void ByteReader::ClearFunctionBase() { |
|
508 have_function_base_ = false; |
|
509 } |
|
510 |
|
511 |
|
512 // (derived from) |
|
513 // dwarf_cfi_to_module.h: Define the DwarfCFIToModule class, which |
|
514 // accepts parsed DWARF call frame info and adds it to a Summariser object. |
|
515 |
|
516 // This class is a reader for DWARF's Call Frame Information. CFI |
|
517 // describes how to unwind stack frames --- even for functions that do |
|
518 // not follow fixed conventions for saving registers, whose frame size |
|
519 // varies as they execute, etc. |
|
520 // |
|
521 // CFI describes, at each machine instruction, how to compute the |
|
522 // stack frame's base address, how to find the return address, and |
|
523 // where to find the saved values of the caller's registers (if the |
|
524 // callee has stashed them somewhere to free up the registers for its |
|
525 // own use). |
|
526 // |
|
527 // For example, suppose we have a function whose machine code looks |
|
528 // like this (imagine an assembly language that looks like C, for a |
|
529 // machine with 32-bit registers, and a stack that grows towards lower |
|
530 // addresses): |
|
531 // |
|
532 // func: ; entry point; return address at sp |
|
533 // func+0: sp = sp - 16 ; allocate space for stack frame |
|
534 // func+1: sp[12] = r0 ; save r0 at sp+12 |
|
535 // ... ; other code, not frame-related |
|
536 // func+10: sp -= 4; *sp = x ; push some x on the stack |
|
537 // ... ; other code, not frame-related |
|
538 // func+20: r0 = sp[16] ; restore saved r0 |
|
539 // func+21: sp += 20 ; pop whole stack frame |
|
540 // func+22: pc = *sp; sp += 4 ; pop return address and jump to it |
|
541 // |
|
542 // DWARF CFI is (a very compressed representation of) a table with a |
|
543 // row for each machine instruction address and a column for each |
|
544 // register showing how to restore it, if possible. |
|
545 // |
|
546 // A special column named "CFA", for "Canonical Frame Address", tells how |
|
547 // to compute the base address of the frame; registers' entries may |
|
548 // refer to the CFA in describing where the registers are saved. |
|
549 // |
|
550 // Another special column, named "RA", represents the return address. |
|
551 // |
|
552 // For example, here is a complete (uncompressed) table describing the |
|
553 // function above: |
|
554 // |
|
555 // insn cfa r0 r1 ... ra |
|
556 // ======================================= |
|
557 // func+0: sp cfa[0] |
|
558 // func+1: sp+16 cfa[0] |
|
559 // func+2: sp+16 cfa[-4] cfa[0] |
|
560 // func+11: sp+20 cfa[-4] cfa[0] |
|
561 // func+21: sp+20 cfa[0] |
|
562 // func+22: sp cfa[0] |
|
563 // |
|
564 // Some things to note here: |
|
565 // |
|
566 // - Each row describes the state of affairs *before* executing the |
|
567 // instruction at the given address. Thus, the row for func+0 |
|
568 // describes the state before we allocate the stack frame. In the |
|
569 // next row, the formula for computing the CFA has changed, |
|
570 // reflecting that allocation. |
|
571 // |
|
572 // - The other entries are written in terms of the CFA; this allows |
|
573 // them to remain unchanged as the stack pointer gets bumped around. |
|
574 // For example, the rule for recovering the return address (the "ra" |
|
575 // column) remains unchanged throughout the function, even as the |
|
576 // stack pointer takes on three different offsets from the return |
|
577 // address. |
|
578 // |
|
579 // - Although we haven't shown it, most calling conventions designate |
|
580 // "callee-saves" and "caller-saves" registers. The callee must |
|
581 // preserve the values of callee-saves registers; if it uses them, |
|
582 // it must save their original values somewhere, and restore them |
|
583 // before it returns. In contrast, the callee is free to trash |
|
584 // caller-saves registers; if the callee uses these, it will |
|
585 // probably not bother to save them anywhere, and the CFI will |
|
586 // probably mark their values as "unrecoverable". |
|
587 // |
|
588 // (However, since the caller cannot assume the callee was going to |
|
589 // save them, caller-saves registers are probably dead in the caller |
|
590 // anyway, so compilers usually don't generate CFA for caller-saves |
|
591 // registers.) |
|
592 // |
|
593 // - Exactly where the CFA points is a matter of convention that |
|
594 // depends on the architecture and ABI in use. In the example, the |
|
595 // CFA is the value the stack pointer had upon entry to the |
|
596 // function, pointing at the saved return address. But on the x86, |
|
597 // the call frame information generated by GCC follows the |
|
598 // convention that the CFA is the address *after* the saved return |
|
599 // address. |
|
600 // |
|
601 // But by definition, the CFA remains constant throughout the |
|
602 // lifetime of the frame. This makes it a useful value for other |
|
603 // columns to refer to. It is also gives debuggers a useful handle |
|
604 // for identifying a frame. |
|
605 // |
|
606 // If you look at the table above, you'll notice that a given entry is |
|
607 // often the same as the one immediately above it: most instructions |
|
608 // change only one or two aspects of the stack frame, if they affect |
|
609 // it at all. The DWARF format takes advantage of this fact, and |
|
610 // reduces the size of the data by mentioning only the addresses and |
|
611 // columns at which changes take place. So for the above, DWARF CFI |
|
612 // data would only actually mention the following: |
|
613 // |
|
614 // insn cfa r0 r1 ... ra |
|
615 // ======================================= |
|
616 // func+0: sp cfa[0] |
|
617 // func+1: sp+16 |
|
618 // func+2: cfa[-4] |
|
619 // func+11: sp+20 |
|
620 // func+21: r0 |
|
621 // func+22: sp |
|
622 // |
|
623 // In fact, this is the way the parser reports CFI to the consumer: as |
|
624 // a series of statements of the form, "At address X, column Y changed |
|
625 // to Z," and related conventions for describing the initial state. |
|
626 // |
|
627 // Naturally, it would be impractical to have to scan the entire |
|
628 // program's CFI, noting changes as we go, just to recover the |
|
629 // unwinding rules in effect at one particular instruction. To avoid |
|
630 // this, CFI data is grouped into "entries", each of which covers a |
|
631 // specified range of addresses and begins with a complete statement |
|
632 // of the rules for all recoverable registers at that starting |
|
633 // address. Each entry typically covers a single function. |
|
634 // |
|
635 // Thus, to compute the contents of a given row of the table --- that |
|
636 // is, rules for recovering the CFA, RA, and registers at a given |
|
637 // instruction --- the consumer should find the entry that covers that |
|
638 // instruction's address, start with the initial state supplied at the |
|
639 // beginning of the entry, and work forward until it has processed all |
|
640 // the changes up to and including those for the present instruction. |
|
641 // |
|
642 // There are seven kinds of rules that can appear in an entry of the |
|
643 // table: |
|
644 // |
|
645 // - "undefined": The given register is not preserved by the callee; |
|
646 // its value cannot be recovered. |
|
647 // |
|
648 // - "same value": This register has the same value it did in the callee. |
|
649 // |
|
650 // - offset(N): The register is saved at offset N from the CFA. |
|
651 // |
|
652 // - val_offset(N): The value the register had in the caller is the |
|
653 // CFA plus offset N. (This is usually only useful for describing |
|
654 // the stack pointer.) |
|
655 // |
|
656 // - register(R): The register's value was saved in another register R. |
|
657 // |
|
658 // - expression(E): Evaluating the DWARF expression E using the |
|
659 // current frame's registers' values yields the address at which the |
|
660 // register was saved. |
|
661 // |
|
662 // - val_expression(E): Evaluating the DWARF expression E using the |
|
663 // current frame's registers' values yields the value the register |
|
664 // had in the caller. |
|
665 |
|
666 class CallFrameInfo { |
|
667 public: |
|
668 // The different kinds of entries one finds in CFI. Used internally, |
|
669 // and for error reporting. |
|
670 enum EntryKind { kUnknown, kCIE, kFDE, kTerminator }; |
|
671 |
|
672 // The handler class to which the parser hands the parsed call frame |
|
673 // information. Defined below. |
|
674 class Handler; |
|
675 |
|
676 // A reporter class, which CallFrameInfo uses to report errors |
|
677 // encountered while parsing call frame information. Defined below. |
|
678 class Reporter; |
|
679 |
|
680 // Create a DWARF CFI parser. BUFFER points to the contents of the |
|
681 // .debug_frame section to parse; BUFFER_LENGTH is its length in bytes. |
|
682 // REPORTER is an error reporter the parser should use to report |
|
683 // problems. READER is a ByteReader instance that has the endianness and |
|
684 // address size set properly. Report the data we find to HANDLER. |
|
685 // |
|
686 // This class can also parse Linux C++ exception handling data, as found |
|
687 // in '.eh_frame' sections. This data is a variant of DWARF CFI that is |
|
688 // placed in loadable segments so that it is present in the program's |
|
689 // address space, and is interpreted by the C++ runtime to search the |
|
690 // call stack for a handler interested in the exception being thrown, |
|
691 // actually pop the frames, and find cleanup code to run. |
|
692 // |
|
693 // There are two differences between the call frame information described |
|
694 // in the DWARF standard and the exception handling data Linux places in |
|
695 // the .eh_frame section: |
|
696 // |
|
697 // - Exception handling data uses uses a different format for call frame |
|
698 // information entry headers. The distinguished CIE id, the way FDEs |
|
699 // refer to their CIEs, and the way the end of the series of entries is |
|
700 // determined are all slightly different. |
|
701 // |
|
702 // If the constructor's EH_FRAME argument is true, then the |
|
703 // CallFrameInfo parses the entry headers as Linux C++ exception |
|
704 // handling data. If EH_FRAME is false or omitted, the CallFrameInfo |
|
705 // parses standard DWARF call frame information. |
|
706 // |
|
707 // - Linux C++ exception handling data uses CIE augmentation strings |
|
708 // beginning with 'z' to specify the presence of additional data after |
|
709 // the CIE and FDE headers and special encodings used for addresses in |
|
710 // frame description entries. |
|
711 // |
|
712 // CallFrameInfo can handle 'z' augmentations in either DWARF CFI or |
|
713 // exception handling data if you have supplied READER with the base |
|
714 // addresses needed to interpret the pointer encodings that 'z' |
|
715 // augmentations can specify. See the ByteReader interface for details |
|
716 // about the base addresses. See the CallFrameInfo::Handler interface |
|
717 // for details about the additional information one might find in |
|
718 // 'z'-augmented data. |
|
719 // |
|
720 // Thus: |
|
721 // |
|
722 // - If you are parsing standard DWARF CFI, as found in a .debug_frame |
|
723 // section, you should pass false for the EH_FRAME argument, or omit |
|
724 // it, and you need not worry about providing READER with the |
|
725 // additional base addresses. |
|
726 // |
|
727 // - If you want to parse Linux C++ exception handling data from a |
|
728 // .eh_frame section, you should pass EH_FRAME as true, and call |
|
729 // READER's Set*Base member functions before calling our Start method. |
|
730 // |
|
731 // - If you want to parse DWARF CFI that uses the 'z' augmentations |
|
732 // (although I don't think any toolchain ever emits such data), you |
|
733 // could pass false for EH_FRAME, but call READER's Set*Base members. |
|
734 // |
|
735 // The extensions the Linux C++ ABI makes to DWARF for exception |
|
736 // handling are described here, rather poorly: |
|
737 // http://refspecs.linux-foundation.org/LSB_4.0.0/LSB-Core-generic/LSB-Core-generic/dwarfext.html |
|
738 // http://refspecs.linux-foundation.org/LSB_4.0.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html |
|
739 // |
|
740 // The mechanics of C++ exception handling, personality routines, |
|
741 // and language-specific data areas are described here, rather nicely: |
|
742 // http://www.codesourcery.com/public/cxx-abi/abi-eh.html |
|
743 |
|
744 CallFrameInfo(const char *buffer, size_t buffer_length, |
|
745 ByteReader *reader, Handler *handler, Reporter *reporter, |
|
746 bool eh_frame = false) |
|
747 : buffer_(buffer), buffer_length_(buffer_length), |
|
748 reader_(reader), handler_(handler), reporter_(reporter), |
|
749 eh_frame_(eh_frame) { } |
|
750 |
|
751 ~CallFrameInfo() { } |
|
752 |
|
753 // Parse the entries in BUFFER, reporting what we find to HANDLER. |
|
754 // Return true if we reach the end of the section successfully, or |
|
755 // false if we encounter an error. |
|
756 bool Start(); |
|
757 |
|
758 // Return the textual name of KIND. For error reporting. |
|
759 static const char *KindName(EntryKind kind); |
|
760 |
|
761 private: |
|
762 |
|
763 struct CIE; |
|
764 |
|
765 // A CFI entry, either an FDE or a CIE. |
|
766 struct Entry { |
|
767 // The starting offset of the entry in the section, for error |
|
768 // reporting. |
|
769 size_t offset; |
|
770 |
|
771 // The start of this entry in the buffer. |
|
772 const char *start; |
|
773 |
|
774 // Which kind of entry this is. |
|
775 // |
|
776 // We want to be able to use this for error reporting even while we're |
|
777 // in the midst of parsing. Error reporting code may assume that kind, |
|
778 // offset, and start fields are valid, although kind may be kUnknown. |
|
779 EntryKind kind; |
|
780 |
|
781 // The end of this entry's common prologue (initial length and id), and |
|
782 // the start of this entry's kind-specific fields. |
|
783 const char *fields; |
|
784 |
|
785 // The start of this entry's instructions. |
|
786 const char *instructions; |
|
787 |
|
788 // The address past the entry's last byte in the buffer. (Note that |
|
789 // since offset points to the entry's initial length field, and the |
|
790 // length field is the number of bytes after that field, this is not |
|
791 // simply buffer_ + offset + length.) |
|
792 const char *end; |
|
793 |
|
794 // For both DWARF CFI and .eh_frame sections, this is the CIE id in a |
|
795 // CIE, and the offset of the associated CIE in an FDE. |
|
796 uint64 id; |
|
797 |
|
798 // The CIE that applies to this entry, if we've parsed it. If this is a |
|
799 // CIE, then this field points to this structure. |
|
800 CIE *cie; |
|
801 }; |
|
802 |
|
803 // A common information entry (CIE). |
|
804 struct CIE: public Entry { |
|
805 uint8 version; // CFI data version number |
|
806 std::string augmentation; // vendor format extension markers |
|
807 uint64 code_alignment_factor; // scale for code address adjustments |
|
808 int data_alignment_factor; // scale for stack pointer adjustments |
|
809 unsigned return_address_register; // which register holds the return addr |
|
810 |
|
811 // True if this CIE includes Linux C++ ABI 'z' augmentation data. |
|
812 bool has_z_augmentation; |
|
813 |
|
814 // Parsed 'z' augmentation data. These are meaningful only if |
|
815 // has_z_augmentation is true. |
|
816 bool has_z_lsda; // The 'z' augmentation included 'L'. |
|
817 bool has_z_personality; // The 'z' augmentation included 'P'. |
|
818 bool has_z_signal_frame; // The 'z' augmentation included 'S'. |
|
819 |
|
820 // If has_z_lsda is true, this is the encoding to be used for language- |
|
821 // specific data area pointers in FDEs. |
|
822 DwarfPointerEncoding lsda_encoding; |
|
823 |
|
824 // If has_z_personality is true, this is the encoding used for the |
|
825 // personality routine pointer in the augmentation data. |
|
826 DwarfPointerEncoding personality_encoding; |
|
827 |
|
828 // If has_z_personality is true, this is the address of the personality |
|
829 // routine --- or, if personality_encoding & DW_EH_PE_indirect, the |
|
830 // address where the personality routine's address is stored. |
|
831 uint64 personality_address; |
|
832 |
|
833 // This is the encoding used for addresses in the FDE header and |
|
834 // in DW_CFA_set_loc instructions. This is always valid, whether |
|
835 // or not we saw a 'z' augmentation string; its default value is |
|
836 // DW_EH_PE_absptr, which is what normal DWARF CFI uses. |
|
837 DwarfPointerEncoding pointer_encoding; |
|
838 }; |
|
839 |
|
840 // A frame description entry (FDE). |
|
841 struct FDE: public Entry { |
|
842 uint64 address; // start address of described code |
|
843 uint64 size; // size of described code, in bytes |
|
844 |
|
845 // If cie->has_z_lsda is true, then this is the language-specific data |
|
846 // area's address --- or its address's address, if cie->lsda_encoding |
|
847 // has the DW_EH_PE_indirect bit set. |
|
848 uint64 lsda_address; |
|
849 }; |
|
850 |
|
851 // Internal use. |
|
852 class Rule; |
|
853 class UndefinedRule; |
|
854 class SameValueRule; |
|
855 class OffsetRule; |
|
856 class ValOffsetRule; |
|
857 class RegisterRule; |
|
858 class ExpressionRule; |
|
859 class ValExpressionRule; |
|
860 class RuleMap; |
|
861 class State; |
|
862 |
|
863 // Parse the initial length and id of a CFI entry, either a CIE, an FDE, |
|
864 // or a .eh_frame end-of-data mark. CURSOR points to the beginning of the |
|
865 // data to parse. On success, populate ENTRY as appropriate, and return |
|
866 // true. On failure, report the problem, and return false. Even if we |
|
867 // return false, set ENTRY->end to the first byte after the entry if we |
|
868 // were able to figure that out, or NULL if we weren't. |
|
869 bool ReadEntryPrologue(const char *cursor, Entry *entry); |
|
870 |
|
871 // Parse the fields of a CIE after the entry prologue, including any 'z' |
|
872 // augmentation data. Assume that the 'Entry' fields of CIE are |
|
873 // populated; use CIE->fields and CIE->end as the start and limit for |
|
874 // parsing. On success, populate the rest of *CIE, and return true; on |
|
875 // failure, report the problem and return false. |
|
876 bool ReadCIEFields(CIE *cie); |
|
877 |
|
878 // Parse the fields of an FDE after the entry prologue, including any 'z' |
|
879 // augmentation data. Assume that the 'Entry' fields of *FDE are |
|
880 // initialized; use FDE->fields and FDE->end as the start and limit for |
|
881 // parsing. Assume that FDE->cie is fully initialized. On success, |
|
882 // populate the rest of *FDE, and return true; on failure, report the |
|
883 // problem and return false. |
|
884 bool ReadFDEFields(FDE *fde); |
|
885 |
|
886 // Report that ENTRY is incomplete, and return false. This is just a |
|
887 // trivial wrapper for invoking reporter_->Incomplete; it provides a |
|
888 // little brevity. |
|
889 bool ReportIncomplete(Entry *entry); |
|
890 |
|
891 // Return true if ENCODING has the DW_EH_PE_indirect bit set. |
|
892 static bool IsIndirectEncoding(DwarfPointerEncoding encoding) { |
|
893 return encoding & DW_EH_PE_indirect; |
|
894 } |
|
895 |
|
896 // The contents of the DWARF .debug_info section we're parsing. |
|
897 const char *buffer_; |
|
898 size_t buffer_length_; |
|
899 |
|
900 // For reading multi-byte values with the appropriate endianness. |
|
901 ByteReader *reader_; |
|
902 |
|
903 // The handler to which we should report the data we find. |
|
904 Handler *handler_; |
|
905 |
|
906 // For reporting problems in the info we're parsing. |
|
907 Reporter *reporter_; |
|
908 |
|
909 // True if we are processing .eh_frame-format data. |
|
910 bool eh_frame_; |
|
911 }; |
|
912 |
|
913 |
|
914 // The handler class for CallFrameInfo. The a CFI parser calls the |
|
915 // member functions of a handler object to report the data it finds. |
|
916 class CallFrameInfo::Handler { |
|
917 public: |
|
918 // The pseudo-register number for the canonical frame address. |
|
919 enum { kCFARegister = DW_REG_CFA }; |
|
920 |
|
921 Handler() { } |
|
922 virtual ~Handler() { } |
|
923 |
|
924 // The parser has found CFI for the machine code at ADDRESS, |
|
925 // extending for LENGTH bytes. OFFSET is the offset of the frame |
|
926 // description entry in the section, for use in error messages. |
|
927 // VERSION is the version number of the CFI format. AUGMENTATION is |
|
928 // a string describing any producer-specific extensions present in |
|
929 // the data. RETURN_ADDRESS is the number of the register that holds |
|
930 // the address to which the function should return. |
|
931 // |
|
932 // Entry should return true to process this CFI, or false to skip to |
|
933 // the next entry. |
|
934 // |
|
935 // The parser invokes Entry for each Frame Description Entry (FDE) |
|
936 // it finds. The parser doesn't report Common Information Entries |
|
937 // to the handler explicitly; instead, if the handler elects to |
|
938 // process a given FDE, the parser reiterates the appropriate CIE's |
|
939 // contents at the beginning of the FDE's rules. |
|
940 virtual bool Entry(size_t offset, uint64 address, uint64 length, |
|
941 uint8 version, const std::string &augmentation, |
|
942 unsigned return_address) = 0; |
|
943 |
|
944 // When the Entry function returns true, the parser calls these |
|
945 // handler functions repeatedly to describe the rules for recovering |
|
946 // registers at each instruction in the given range of machine code. |
|
947 // Immediately after a call to Entry, the handler should assume that |
|
948 // the rule for each callee-saves register is "unchanged" --- that |
|
949 // is, that the register still has the value it had in the caller. |
|
950 // |
|
951 // If a *Rule function returns true, we continue processing this entry's |
|
952 // instructions. If a *Rule function returns false, we stop evaluating |
|
953 // instructions, and skip to the next entry. Either way, we call End |
|
954 // before going on to the next entry. |
|
955 // |
|
956 // In all of these functions, if the REG parameter is kCFARegister, then |
|
957 // the rule describes how to find the canonical frame address. |
|
958 // kCFARegister may be passed as a BASE_REGISTER argument, meaning that |
|
959 // the canonical frame address should be used as the base address for the |
|
960 // computation. All other REG values will be positive. |
|
961 |
|
962 // At ADDRESS, register REG's value is not recoverable. |
|
963 virtual bool UndefinedRule(uint64 address, int reg) = 0; |
|
964 |
|
965 // At ADDRESS, register REG's value is the same as that it had in |
|
966 // the caller. |
|
967 virtual bool SameValueRule(uint64 address, int reg) = 0; |
|
968 |
|
969 // At ADDRESS, register REG has been saved at offset OFFSET from |
|
970 // BASE_REGISTER. |
|
971 virtual bool OffsetRule(uint64 address, int reg, |
|
972 int base_register, long offset) = 0; |
|
973 |
|
974 // At ADDRESS, the caller's value of register REG is the current |
|
975 // value of BASE_REGISTER plus OFFSET. (This rule doesn't provide an |
|
976 // address at which the register's value is saved.) |
|
977 virtual bool ValOffsetRule(uint64 address, int reg, |
|
978 int base_register, long offset) = 0; |
|
979 |
|
980 // At ADDRESS, register REG has been saved in BASE_REGISTER. This differs |
|
981 // from ValOffsetRule(ADDRESS, REG, BASE_REGISTER, 0), in that |
|
982 // BASE_REGISTER is the "home" for REG's saved value: if you want to |
|
983 // assign to a variable whose home is REG in the calling frame, you |
|
984 // should put the value in BASE_REGISTER. |
|
985 virtual bool RegisterRule(uint64 address, int reg, int base_register) = 0; |
|
986 |
|
987 // At ADDRESS, the DWARF expression EXPRESSION yields the address at |
|
988 // which REG was saved. |
|
989 virtual bool ExpressionRule(uint64 address, int reg, |
|
990 const std::string &expression) = 0; |
|
991 |
|
992 // At ADDRESS, the DWARF expression EXPRESSION yields the caller's |
|
993 // value for REG. (This rule doesn't provide an address at which the |
|
994 // register's value is saved.) |
|
995 virtual bool ValExpressionRule(uint64 address, int reg, |
|
996 const std::string &expression) = 0; |
|
997 |
|
998 // Indicate that the rules for the address range reported by the |
|
999 // last call to Entry are complete. End should return true if |
|
1000 // everything is okay, or false if an error has occurred and parsing |
|
1001 // should stop. |
|
1002 virtual bool End() = 0; |
|
1003 |
|
1004 // Handler functions for Linux C++ exception handling data. These are |
|
1005 // only called if the data includes 'z' augmentation strings. |
|
1006 |
|
1007 // The Linux C++ ABI uses an extension of the DWARF CFI format to |
|
1008 // walk the stack to propagate exceptions from the throw to the |
|
1009 // appropriate catch, and do the appropriate cleanups along the way. |
|
1010 // CFI entries used for exception handling have two additional data |
|
1011 // associated with them: |
|
1012 // |
|
1013 // - The "language-specific data area" describes which exception |
|
1014 // types the function has 'catch' clauses for, and indicates how |
|
1015 // to go about re-entering the function at the appropriate catch |
|
1016 // clause. If the exception is not caught, it describes the |
|
1017 // destructors that must run before the frame is popped. |
|
1018 // |
|
1019 // - The "personality routine" is responsible for interpreting the |
|
1020 // language-specific data area's contents, and deciding whether |
|
1021 // the exception should continue to propagate down the stack, |
|
1022 // perhaps after doing some cleanup for this frame, or whether the |
|
1023 // exception will be caught here. |
|
1024 // |
|
1025 // In principle, the language-specific data area is opaque to |
|
1026 // everybody but the personality routine. In practice, these values |
|
1027 // may be useful or interesting to readers with extra context, and |
|
1028 // we have to at least skip them anyway, so we might as well report |
|
1029 // them to the handler. |
|
1030 |
|
1031 // This entry's exception handling personality routine's address is |
|
1032 // ADDRESS. If INDIRECT is true, then ADDRESS is the address at |
|
1033 // which the routine's address is stored. The default definition for |
|
1034 // this handler function simply returns true, allowing parsing of |
|
1035 // the entry to continue. |
|
1036 virtual bool PersonalityRoutine(uint64 address, bool indirect) { |
|
1037 return true; |
|
1038 } |
|
1039 |
|
1040 // This entry's language-specific data area (LSDA) is located at |
|
1041 // ADDRESS. If INDIRECT is true, then ADDRESS is the address at |
|
1042 // which the area's address is stored. The default definition for |
|
1043 // this handler function simply returns true, allowing parsing of |
|
1044 // the entry to continue. |
|
1045 virtual bool LanguageSpecificDataArea(uint64 address, bool indirect) { |
|
1046 return true; |
|
1047 } |
|
1048 |
|
1049 // This entry describes a signal trampoline --- this frame is the |
|
1050 // caller of a signal handler. The default definition for this |
|
1051 // handler function simply returns true, allowing parsing of the |
|
1052 // entry to continue. |
|
1053 // |
|
1054 // The best description of the rationale for and meaning of signal |
|
1055 // trampoline CFI entries seems to be in the GCC bug database: |
|
1056 // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=26208 |
|
1057 virtual bool SignalHandler() { return true; } |
|
1058 }; |
|
1059 |
|
1060 |
|
1061 // The CallFrameInfo class makes calls on an instance of this class to |
|
1062 // report errors or warn about problems in the data it is parsing. |
|
1063 // These messages are sent to the message sink |aLog| provided to the |
|
1064 // constructor. |
|
1065 class CallFrameInfo::Reporter { |
|
1066 public: |
|
1067 // Create an error reporter which attributes troubles to the section |
|
1068 // named SECTION in FILENAME. |
|
1069 // |
|
1070 // Normally SECTION would be .debug_frame, but the Mac puts CFI data |
|
1071 // in a Mach-O section named __debug_frame. If we support |
|
1072 // Linux-style exception handling data, we could be reading an |
|
1073 // .eh_frame section. |
|
1074 Reporter(void (*aLog)(const char*), |
|
1075 const std::string &filename, |
|
1076 const std::string §ion = ".debug_frame") |
|
1077 : log_(aLog), filename_(filename), section_(section) { } |
|
1078 virtual ~Reporter() { } |
|
1079 |
|
1080 // The CFI entry at OFFSET ends too early to be well-formed. KIND |
|
1081 // indicates what kind of entry it is; KIND can be kUnknown if we |
|
1082 // haven't parsed enough of the entry to tell yet. |
|
1083 virtual void Incomplete(uint64 offset, CallFrameInfo::EntryKind kind); |
|
1084 |
|
1085 // The .eh_frame data has a four-byte zero at OFFSET where the next |
|
1086 // entry's length would be; this is a terminator. However, the buffer |
|
1087 // length as given to the CallFrameInfo constructor says there should be |
|
1088 // more data. |
|
1089 virtual void EarlyEHTerminator(uint64 offset); |
|
1090 |
|
1091 // The FDE at OFFSET refers to the CIE at CIE_OFFSET, but the |
|
1092 // section is not that large. |
|
1093 virtual void CIEPointerOutOfRange(uint64 offset, uint64 cie_offset); |
|
1094 |
|
1095 // The FDE at OFFSET refers to the CIE at CIE_OFFSET, but the entry |
|
1096 // there is not a CIE. |
|
1097 virtual void BadCIEId(uint64 offset, uint64 cie_offset); |
|
1098 |
|
1099 // The FDE at OFFSET refers to a CIE with version number VERSION, |
|
1100 // which we don't recognize. We cannot parse DWARF CFI if it uses |
|
1101 // a version number we don't recognize. |
|
1102 virtual void UnrecognizedVersion(uint64 offset, int version); |
|
1103 |
|
1104 // The FDE at OFFSET refers to a CIE with augmentation AUGMENTATION, |
|
1105 // which we don't recognize. We cannot parse DWARF CFI if it uses |
|
1106 // augmentations we don't recognize. |
|
1107 virtual void UnrecognizedAugmentation(uint64 offset, |
|
1108 const std::string &augmentation); |
|
1109 |
|
1110 // The pointer encoding ENCODING, specified by the CIE at OFFSET, is not |
|
1111 // a valid encoding. |
|
1112 virtual void InvalidPointerEncoding(uint64 offset, uint8 encoding); |
|
1113 |
|
1114 // The pointer encoding ENCODING, specified by the CIE at OFFSET, depends |
|
1115 // on a base address which has not been supplied. |
|
1116 virtual void UnusablePointerEncoding(uint64 offset, uint8 encoding); |
|
1117 |
|
1118 // The CIE at OFFSET contains a DW_CFA_restore instruction at |
|
1119 // INSN_OFFSET, which may not appear in a CIE. |
|
1120 virtual void RestoreInCIE(uint64 offset, uint64 insn_offset); |
|
1121 |
|
1122 // The entry at OFFSET, of kind KIND, has an unrecognized |
|
1123 // instruction at INSN_OFFSET. |
|
1124 virtual void BadInstruction(uint64 offset, CallFrameInfo::EntryKind kind, |
|
1125 uint64 insn_offset); |
|
1126 |
|
1127 // The instruction at INSN_OFFSET in the entry at OFFSET, of kind |
|
1128 // KIND, establishes a rule that cites the CFA, but we have not |
|
1129 // established a CFA rule yet. |
|
1130 virtual void NoCFARule(uint64 offset, CallFrameInfo::EntryKind kind, |
|
1131 uint64 insn_offset); |
|
1132 |
|
1133 // The instruction at INSN_OFFSET in the entry at OFFSET, of kind |
|
1134 // KIND, is a DW_CFA_restore_state instruction, but the stack of |
|
1135 // saved states is empty. |
|
1136 virtual void EmptyStateStack(uint64 offset, CallFrameInfo::EntryKind kind, |
|
1137 uint64 insn_offset); |
|
1138 |
|
1139 // The DW_CFA_remember_state instruction at INSN_OFFSET in the entry |
|
1140 // at OFFSET, of kind KIND, would restore a state that has no CFA |
|
1141 // rule, whereas the current state does have a CFA rule. This is |
|
1142 // bogus input, which the CallFrameInfo::Handler interface doesn't |
|
1143 // (and shouldn't) have any way to report. |
|
1144 virtual void ClearingCFARule(uint64 offset, CallFrameInfo::EntryKind kind, |
|
1145 uint64 insn_offset); |
|
1146 |
|
1147 private: |
|
1148 // A logging sink function, as supplied by LUL's user. |
|
1149 void (*log_)(const char*); |
|
1150 |
|
1151 protected: |
|
1152 // The name of the file whose CFI we're reading. |
|
1153 std::string filename_; |
|
1154 |
|
1155 // The name of the CFI section in that file. |
|
1156 std::string section_; |
|
1157 }; |
|
1158 |
|
1159 |
|
1160 using lul::CallFrameInfo; |
|
1161 using lul::Summariser; |
|
1162 |
|
1163 // A class that accepts parsed call frame information from the DWARF |
|
1164 // CFI parser and populates a google_breakpad::Module object with the |
|
1165 // contents. |
|
1166 class DwarfCFIToModule: public CallFrameInfo::Handler { |
|
1167 public: |
|
1168 |
|
1169 // DwarfCFIToModule uses an instance of this class to report errors |
|
1170 // detected while converting DWARF CFI to Breakpad STACK CFI records. |
|
1171 class Reporter { |
|
1172 public: |
|
1173 // Create a reporter that writes messages to the message sink |
|
1174 // |aLog|. FILE is the name of the file we're processing, and |
|
1175 // SECTION is the name of the section within that file that we're |
|
1176 // looking at (.debug_frame, .eh_frame, etc.). |
|
1177 Reporter(void (*aLog)(const char*), |
|
1178 const std::string &file, const std::string §ion) |
|
1179 : log_(aLog), file_(file), section_(section) { } |
|
1180 virtual ~Reporter() { } |
|
1181 |
|
1182 // The DWARF CFI entry at OFFSET says that REG is undefined, but the |
|
1183 // Breakpad symbol file format cannot express this. |
|
1184 virtual void UndefinedNotSupported(size_t offset, |
|
1185 const UniqueString* reg); |
|
1186 |
|
1187 // The DWARF CFI entry at OFFSET says that REG uses a DWARF |
|
1188 // expression to find its value, but DwarfCFIToModule is not |
|
1189 // capable of translating DWARF expressions to Breakpad postfix |
|
1190 // expressions. |
|
1191 virtual void ExpressionsNotSupported(size_t offset, |
|
1192 const UniqueString* reg); |
|
1193 |
|
1194 private: |
|
1195 // A logging sink function, as supplied by LUL's user. |
|
1196 void (*log_)(const char*); |
|
1197 protected: |
|
1198 std::string file_, section_; |
|
1199 }; |
|
1200 |
|
1201 // Register name tables. If TABLE is a vector returned by one of these |
|
1202 // functions, then TABLE[R] is the name of the register numbered R in |
|
1203 // DWARF call frame information. |
|
1204 class RegisterNames { |
|
1205 public: |
|
1206 // Intel's "x86" or IA-32. |
|
1207 static const unsigned int I386(); |
|
1208 |
|
1209 // AMD x86_64, AMD64, Intel EM64T, or Intel 64 |
|
1210 static const unsigned int X86_64(); |
|
1211 |
|
1212 // ARM. |
|
1213 static const unsigned int ARM(); |
|
1214 }; |
|
1215 |
|
1216 // Create a handler for the dwarf2reader::CallFrameInfo parser that |
|
1217 // records the stack unwinding information it receives in SUMM. |
|
1218 // |
|
1219 // Use REGISTER_NAMES[I] as the name of register number I; *this |
|
1220 // keeps a reference to the vector, so the vector should remain |
|
1221 // alive for as long as the DwarfCFIToModule does. |
|
1222 // |
|
1223 // Use REPORTER for reporting problems encountered in the conversion |
|
1224 // process. |
|
1225 DwarfCFIToModule(const unsigned int num_dw_regs, |
|
1226 Reporter *reporter, |
|
1227 /*OUT*/Summariser* summ) |
|
1228 : summ_(summ), num_dw_regs_(num_dw_regs), reporter_(reporter), |
|
1229 return_address_(-1) { |
|
1230 } |
|
1231 virtual ~DwarfCFIToModule() {} |
|
1232 |
|
1233 virtual bool Entry(size_t offset, uint64 address, uint64 length, |
|
1234 uint8 version, const std::string &augmentation, |
|
1235 unsigned return_address); |
|
1236 virtual bool UndefinedRule(uint64 address, int reg); |
|
1237 virtual bool SameValueRule(uint64 address, int reg); |
|
1238 virtual bool OffsetRule(uint64 address, int reg, |
|
1239 int base_register, long offset); |
|
1240 virtual bool ValOffsetRule(uint64 address, int reg, |
|
1241 int base_register, long offset); |
|
1242 virtual bool RegisterRule(uint64 address, int reg, int base_register); |
|
1243 virtual bool ExpressionRule(uint64 address, int reg, |
|
1244 const std::string &expression); |
|
1245 virtual bool ValExpressionRule(uint64 address, int reg, |
|
1246 const std::string &expression); |
|
1247 virtual bool End(); |
|
1248 |
|
1249 private: |
|
1250 // Return the name to use for register REG. |
|
1251 const UniqueString* RegisterName(int i); |
|
1252 |
|
1253 // The Summariser to which we should give entries |
|
1254 Summariser* summ_; |
|
1255 |
|
1256 // The number of Dwarf-defined register names for this architecture. |
|
1257 const unsigned int num_dw_regs_; |
|
1258 |
|
1259 // The reporter to use to report problems. |
|
1260 Reporter *reporter_; |
|
1261 |
|
1262 // The section offset of the current frame description entry, for |
|
1263 // use in error messages. |
|
1264 size_t entry_offset_; |
|
1265 |
|
1266 // The return address column for that entry. |
|
1267 unsigned return_address_; |
|
1268 }; |
|
1269 |
|
1270 } // namespace lul |
|
1271 |
|
1272 #endif // LulDwarfExt_h |