Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | # HG changeset patch |
michael@0 | 2 | # User Julian Seward <jseward@acm.org> |
michael@0 | 3 | # Date 1371190160 -7200 |
michael@0 | 4 | # Fri Jun 14 08:09:20 2013 +0200 |
michael@0 | 5 | # Node ID e74de3db7dd27ffda8f4772f892cfb52c5c35649 |
michael@0 | 6 | # Parent 4dcd4220c31068e116d88a58e5b396fbb01719dd |
michael@0 | 7 | Bug 863475 - integrate ARM EXIDX unwind parsing into Breakpad. r=glandium,ted |
michael@0 | 8 | |
michael@0 | 9 | diff --git a/Makefile.am b/Makefile.am |
michael@0 | 10 | --- a/Makefile.am |
michael@0 | 11 | +++ b/Makefile.am |
michael@0 | 12 | @@ -428,16 +428,18 @@ src_tools_linux_dump_syms_dump_syms_SOUR |
michael@0 | 13 | src/common/dwarf_line_to_module.cc \ |
michael@0 | 14 | src/common/language.cc \ |
michael@0 | 15 | src/common/module.cc \ |
michael@0 | 16 | src/common/stabs_reader.cc \ |
michael@0 | 17 | src/common/stabs_to_module.cc \ |
michael@0 | 18 | src/common/dwarf/bytereader.cc \ |
michael@0 | 19 | src/common/dwarf/dwarf2diehandler.cc \ |
michael@0 | 20 | src/common/dwarf/dwarf2reader.cc \ |
michael@0 | 21 | + src/common/arm_ex_reader.cc \ |
michael@0 | 22 | + src/common/arm_ex_to_module.cc \ |
michael@0 | 23 | src/common/linux/dump_symbols.cc \ |
michael@0 | 24 | src/common/linux/elf_symbols_to_module.cc \ |
michael@0 | 25 | src/common/linux/elfutils.cc \ |
michael@0 | 26 | src/common/linux/file_id.cc \ |
michael@0 | 27 | src/common/linux/linux_libc_support.cc \ |
michael@0 | 28 | src/common/linux/memory_mapped_file.cc \ |
michael@0 | 29 | src/common/linux/safe_readlink.cc \ |
michael@0 | 30 | src/tools/linux/dump_syms/dump_syms.cc |
michael@0 | 31 | @@ -1010,16 +1012,20 @@ EXTRA_DIST = \ |
michael@0 | 32 | src/client/windows/handler/exception_handler.vcproj \ |
michael@0 | 33 | src/client/windows/sender/crash_report_sender.cc \ |
michael@0 | 34 | src/client/windows/sender/crash_report_sender.h \ |
michael@0 | 35 | src/client/windows/sender/crash_report_sender.vcproj \ |
michael@0 | 36 | src/common/convert_UTF.c \ |
michael@0 | 37 | src/common/convert_UTF.h \ |
michael@0 | 38 | src/common/linux/dump_symbols.cc \ |
michael@0 | 39 | src/common/linux/dump_symbols.h \ |
michael@0 | 40 | + src/common/arm_ex_reader.cc \ |
michael@0 | 41 | + src/common/arm_ex_reader.h \ |
michael@0 | 42 | + src/common/arm_ex_to_module.cc \ |
michael@0 | 43 | + src/common/arm_ex_to_module.h \ |
michael@0 | 44 | src/common/linux/elf_symbols_to_module.cc \ |
michael@0 | 45 | src/common/linux/elf_symbols_to_module.h \ |
michael@0 | 46 | src/common/linux/elfutils.cc \ |
michael@0 | 47 | src/common/linux/elfutils.h \ |
michael@0 | 48 | src/common/linux/file_id.cc \ |
michael@0 | 49 | src/common/linux/file_id.h \ |
michael@0 | 50 | src/common/linux/guid_creator.cc \ |
michael@0 | 51 | src/common/linux/guid_creator.h \ |
michael@0 | 52 | diff --git a/src/common/arm_ex_reader.cc b/src/common/arm_ex_reader.cc |
michael@0 | 53 | new file mode 100644 |
michael@0 | 54 | --- /dev/null |
michael@0 | 55 | +++ b/src/common/arm_ex_reader.cc |
michael@0 | 56 | @@ -0,0 +1,502 @@ |
michael@0 | 57 | + |
michael@0 | 58 | +/* libunwind - a platform-independent unwind library |
michael@0 | 59 | + Copyright 2011 Linaro Limited |
michael@0 | 60 | + |
michael@0 | 61 | +This file is part of libunwind. |
michael@0 | 62 | + |
michael@0 | 63 | +Permission is hereby granted, free of charge, to any person obtaining |
michael@0 | 64 | +a copy of this software and associated documentation files (the |
michael@0 | 65 | +"Software"), to deal in the Software without restriction, including |
michael@0 | 66 | +without limitation the rights to use, copy, modify, merge, publish, |
michael@0 | 67 | +distribute, sublicense, and/or sell copies of the Software, and to |
michael@0 | 68 | +permit persons to whom the Software is furnished to do so, subject to |
michael@0 | 69 | +the following conditions: |
michael@0 | 70 | + |
michael@0 | 71 | +The above copyright notice and this permission notice shall be |
michael@0 | 72 | +included in all copies or substantial portions of the Software. |
michael@0 | 73 | + |
michael@0 | 74 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
michael@0 | 75 | +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
michael@0 | 76 | +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
michael@0 | 77 | +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
michael@0 | 78 | +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
michael@0 | 79 | +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
michael@0 | 80 | +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
michael@0 | 81 | + |
michael@0 | 82 | +// Copyright (c) 2010 Google Inc. |
michael@0 | 83 | +// All rights reserved. |
michael@0 | 84 | +// |
michael@0 | 85 | +// Redistribution and use in source and binary forms, with or without |
michael@0 | 86 | +// modification, are permitted provided that the following conditions are |
michael@0 | 87 | +// met: |
michael@0 | 88 | +// |
michael@0 | 89 | +// * Redistributions of source code must retain the above copyright |
michael@0 | 90 | +// notice, this list of conditions and the following disclaimer. |
michael@0 | 91 | +// * Redistributions in binary form must reproduce the above |
michael@0 | 92 | +// copyright notice, this list of conditions and the following disclaimer |
michael@0 | 93 | +// in the documentation and/or other materials provided with the |
michael@0 | 94 | +// distribution. |
michael@0 | 95 | +// * Neither the name of Google Inc. nor the names of its |
michael@0 | 96 | +// contributors may be used to endorse or promote products derived from |
michael@0 | 97 | +// this software without specific prior written permission. |
michael@0 | 98 | +// |
michael@0 | 99 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 100 | +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 101 | +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 102 | +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 103 | +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 104 | +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 105 | +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 106 | +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 107 | +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 108 | +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 109 | +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 110 | + |
michael@0 | 111 | + |
michael@0 | 112 | +// Derived from libunwind, with extensive modifications. |
michael@0 | 113 | + |
michael@0 | 114 | + |
michael@0 | 115 | +#include "common/arm_ex_reader.h" |
michael@0 | 116 | +#include "common/logging.h" |
michael@0 | 117 | + |
michael@0 | 118 | +#include <assert.h> |
michael@0 | 119 | + |
michael@0 | 120 | +// This file, in conjunction with arm_ex_to_module.cc, translates |
michael@0 | 121 | +// EXIDX unwind information into the same format that Breakpad uses |
michael@0 | 122 | +// for CFI information. Hence Breakpad's CFI unwinding abilities |
michael@0 | 123 | +// also become usable for EXIDX. |
michael@0 | 124 | +// |
michael@0 | 125 | +// See: "Exception Handling ABI for the ARM Architecture", ARM IHI 0038A |
michael@0 | 126 | +// http://infocenter.arm.com/help/topic/com.arm.doc.ihi0038a/IHI0038A_ehabi.pdf |
michael@0 | 127 | + |
michael@0 | 128 | +// EXIDX data is presented in two parts: |
michael@0 | 129 | +// |
michael@0 | 130 | +// * an index table. This contains two words per routine, |
michael@0 | 131 | +// the first of which identifies the routine, and the second |
michael@0 | 132 | +// of which is a reference to the unwind bytecode. If the |
michael@0 | 133 | +// bytecode is very compact -- 3 bytes or less -- it can be |
michael@0 | 134 | +// stored directly in the second word. |
michael@0 | 135 | +// |
michael@0 | 136 | +// * an area containing the unwind bytecodes. |
michael@0 | 137 | + |
michael@0 | 138 | +// General flow is: ExceptionTableInfo::Start iterates over all |
michael@0 | 139 | +// of the index table entries (pairs). For each entry, it: |
michael@0 | 140 | +// |
michael@0 | 141 | +// * calls ExceptionTableInfo::ExtabEntryExtract to copy the bytecode |
michael@0 | 142 | +// out into an intermediate buffer. |
michael@0 | 143 | + |
michael@0 | 144 | +// * uses ExceptionTableInfo::ExtabEntryDecode to parse the intermediate |
michael@0 | 145 | +// buffer. Each bytecode instruction is bundled into a |
michael@0 | 146 | +// arm_ex_to_module::extab_data structure, and handed to .. |
michael@0 | 147 | +// |
michael@0 | 148 | +// * .. ARMExToModule::ImproveStackFrame, which in turn hands it to |
michael@0 | 149 | +// ARMExToModule::TranslateCmd, and that generates the pseudo-CFI |
michael@0 | 150 | +// records that Breakpad stores. |
michael@0 | 151 | + |
michael@0 | 152 | +#define ARM_EXIDX_CANT_UNWIND 0x00000001 |
michael@0 | 153 | +#define ARM_EXIDX_COMPACT 0x80000000 |
michael@0 | 154 | +#define ARM_EXTBL_OP_FINISH 0xb0 |
michael@0 | 155 | +#define ARM_EXIDX_TABLE_LIMIT (255*4) |
michael@0 | 156 | + |
michael@0 | 157 | +namespace arm_ex_reader { |
michael@0 | 158 | + |
michael@0 | 159 | +using arm_ex_to_module::ARM_EXIDX_CMD_FINISH; |
michael@0 | 160 | +using arm_ex_to_module::ARM_EXIDX_CMD_SUB_FROM_VSP; |
michael@0 | 161 | +using arm_ex_to_module::ARM_EXIDX_CMD_ADD_TO_VSP; |
michael@0 | 162 | +using arm_ex_to_module::ARM_EXIDX_CMD_REG_POP; |
michael@0 | 163 | +using arm_ex_to_module::ARM_EXIDX_CMD_REG_TO_SP; |
michael@0 | 164 | +using arm_ex_to_module::ARM_EXIDX_CMD_VFP_POP; |
michael@0 | 165 | +using arm_ex_to_module::ARM_EXIDX_CMD_WREG_POP; |
michael@0 | 166 | +using arm_ex_to_module::ARM_EXIDX_CMD_WCGR_POP; |
michael@0 | 167 | +using arm_ex_to_module::ARM_EXIDX_CMD_RESERVED; |
michael@0 | 168 | +using arm_ex_to_module::ARM_EXIDX_CMD_REFUSED; |
michael@0 | 169 | +using arm_ex_to_module::exidx_entry; |
michael@0 | 170 | +using arm_ex_to_module::ARM_EXIDX_VFP_SHIFT_16; |
michael@0 | 171 | +using arm_ex_to_module::ARM_EXIDX_VFP_FSTMD; |
michael@0 | 172 | +using google_breakpad::MemoryRange; |
michael@0 | 173 | + |
michael@0 | 174 | + |
michael@0 | 175 | +static void* Prel31ToAddr(const void* addr) |
michael@0 | 176 | +{ |
michael@0 | 177 | + uint32_t offset32 = *reinterpret_cast<const uint32_t*>(addr); |
michael@0 | 178 | + // sign extend offset32[30:0] to 64 bits -- copy bit 30 to positions |
michael@0 | 179 | + // 63:31 inclusive. |
michael@0 | 180 | + uint64_t offset64 = offset32; |
michael@0 | 181 | + if (offset64 & (1ULL << 30)) |
michael@0 | 182 | + offset64 |= 0xFFFFFFFF80000000ULL; |
michael@0 | 183 | + else |
michael@0 | 184 | + offset64 &= 0x000000007FFFFFFFULL; |
michael@0 | 185 | + return ((char*)addr) + (uintptr_t)offset64; |
michael@0 | 186 | +} |
michael@0 | 187 | + |
michael@0 | 188 | + |
michael@0 | 189 | +// Extract unwind bytecode for the function denoted by |entry| into |buf|, |
michael@0 | 190 | +// and return the number of bytes of |buf| written, along with a code |
michael@0 | 191 | +// indicating the outcome. |
michael@0 | 192 | + |
michael@0 | 193 | +ExceptionTableInfo::ExExtractResult |
michael@0 | 194 | +ExceptionTableInfo::ExtabEntryExtract(const struct exidx_entry* entry, |
michael@0 | 195 | + uint8_t* buf, size_t buf_size, |
michael@0 | 196 | + /*OUT*/size_t* buf_used) |
michael@0 | 197 | +{ |
michael@0 | 198 | + MemoryRange mr_out(buf, buf_size); |
michael@0 | 199 | + |
michael@0 | 200 | + *buf_used = 0; |
michael@0 | 201 | + |
michael@0 | 202 | +# define PUT_BUF_U8(_byte) \ |
michael@0 | 203 | + do { if (!mr_out.Covers(*buf_used, 1)) return ExOutBufOverflow; \ |
michael@0 | 204 | + buf[(*buf_used)++] = (_byte); } while (0) |
michael@0 | 205 | + |
michael@0 | 206 | +# define GET_EX_U32(_lval, _addr, _sec_mr) \ |
michael@0 | 207 | + do { if (!(_sec_mr).Covers(reinterpret_cast<const uint8_t*>(_addr) \ |
michael@0 | 208 | + - (_sec_mr).data(), 4)) \ |
michael@0 | 209 | + return ExInBufOverflow; \ |
michael@0 | 210 | + (_lval) = *(reinterpret_cast<const uint32_t*>(_addr)); } while (0) |
michael@0 | 211 | + |
michael@0 | 212 | +# define GET_EXIDX_U32(_lval, _addr) \ |
michael@0 | 213 | + GET_EX_U32(_lval, _addr, mr_exidx_) |
michael@0 | 214 | +# define GET_EXTAB_U32(_lval, _addr) \ |
michael@0 | 215 | + GET_EX_U32(_lval, _addr, mr_extab_) |
michael@0 | 216 | + |
michael@0 | 217 | + uint32_t data; |
michael@0 | 218 | + GET_EXIDX_U32(data, &entry->data); |
michael@0 | 219 | + |
michael@0 | 220 | + // A function can be marked CANT_UNWIND if (eg) it is known to be |
michael@0 | 221 | + // at the bottom of the stack. |
michael@0 | 222 | + if (data == ARM_EXIDX_CANT_UNWIND) |
michael@0 | 223 | + return ExCantUnwind; |
michael@0 | 224 | + |
michael@0 | 225 | + uint32_t pers; // personality number |
michael@0 | 226 | + uint32_t extra; // number of extra data words required |
michael@0 | 227 | + uint32_t extra_allowed; // number of extra data words allowed |
michael@0 | 228 | + uint32_t* extbl_data; // the handler entry, if not inlined |
michael@0 | 229 | + |
michael@0 | 230 | + if (data & ARM_EXIDX_COMPACT) { |
michael@0 | 231 | + // The handler table entry has been inlined into the index table entry. |
michael@0 | 232 | + // In this case it can only be an ARM-defined compact model, since |
michael@0 | 233 | + // bit 31 is 1. Only personalities 0, 1 and 2 are defined for the |
michael@0 | 234 | + // ARM compact model, but 1 and 2 are "Long format" and may require |
michael@0 | 235 | + // extra data words. Hence the allowable personalities here are: |
michael@0 | 236 | + // personality 0, in which case 'extra' has no meaning |
michael@0 | 237 | + // personality 1, with zero extra words |
michael@0 | 238 | + // personality 2, with zero extra words |
michael@0 | 239 | + extbl_data = NULL; |
michael@0 | 240 | + pers = (data >> 24) & 0x0F; |
michael@0 | 241 | + extra = (data >> 16) & 0xFF; |
michael@0 | 242 | + extra_allowed = 0; |
michael@0 | 243 | + } |
michael@0 | 244 | + else { |
michael@0 | 245 | + // The index table entry is a pointer to the handler entry. Note |
michael@0 | 246 | + // that Prel31ToAddr will read the given address, but we already |
michael@0 | 247 | + // range-checked above. |
michael@0 | 248 | + extbl_data = reinterpret_cast<uint32_t*>(Prel31ToAddr(&entry->data)); |
michael@0 | 249 | + GET_EXTAB_U32(data, extbl_data); |
michael@0 | 250 | + if (!(data & ARM_EXIDX_COMPACT)) { |
michael@0 | 251 | + // This denotes a "generic model" handler. That will involve |
michael@0 | 252 | + // executing arbitary machine code, which is something we |
michael@0 | 253 | + // can't represent here; hence reject it. |
michael@0 | 254 | + return ExCantRepresent; |
michael@0 | 255 | + } |
michael@0 | 256 | + // So we have a compact model representation. Again, 3 possible |
michael@0 | 257 | + // personalities, but this time up to 255 allowable extra words. |
michael@0 | 258 | + pers = (data >> 24) & 0x0F; |
michael@0 | 259 | + extra = (data >> 16) & 0xFF; |
michael@0 | 260 | + extra_allowed = 255; |
michael@0 | 261 | + extbl_data++; |
michael@0 | 262 | + } |
michael@0 | 263 | + |
michael@0 | 264 | + // Now look at the the handler table entry. The first word is |
michael@0 | 265 | + // |data| and subsequent words start at |*extbl_data|. The number |
michael@0 | 266 | + // of extra words to use is |extra|, provided that the personality |
michael@0 | 267 | + // allows extra words. Even if it does, none may be available -- |
michael@0 | 268 | + // extra_allowed is the maximum number of extra words allowed. */ |
michael@0 | 269 | + if (pers == 0) { |
michael@0 | 270 | + // "Su16" in the documentation -- 3 unwinding insn bytes |
michael@0 | 271 | + // |extra| has no meaning here; instead that byte is an unwind-info byte |
michael@0 | 272 | + PUT_BUF_U8(data >> 16); |
michael@0 | 273 | + PUT_BUF_U8(data >> 8); |
michael@0 | 274 | + PUT_BUF_U8(data); |
michael@0 | 275 | + } |
michael@0 | 276 | + else if ((pers == 1 || pers == 2) && extra <= extra_allowed) { |
michael@0 | 277 | + // "Lu16" or "Lu32" respectively -- 2 unwinding insn bytes, |
michael@0 | 278 | + // and up to 255 extra words. |
michael@0 | 279 | + PUT_BUF_U8(data >> 8); |
michael@0 | 280 | + PUT_BUF_U8(data); |
michael@0 | 281 | + for (uint32_t j = 0; j < extra; j++) { |
michael@0 | 282 | + GET_EXTAB_U32(data, extbl_data); |
michael@0 | 283 | + extbl_data++; |
michael@0 | 284 | + PUT_BUF_U8(data >> 24); |
michael@0 | 285 | + PUT_BUF_U8(data >> 16); |
michael@0 | 286 | + PUT_BUF_U8(data >> 8); |
michael@0 | 287 | + PUT_BUF_U8(data >> 0); |
michael@0 | 288 | + } |
michael@0 | 289 | + } |
michael@0 | 290 | + else { |
michael@0 | 291 | + // The entry is invalid. |
michael@0 | 292 | + return ExInvalid; |
michael@0 | 293 | + } |
michael@0 | 294 | + |
michael@0 | 295 | + // Make sure the entry is terminated with "FINISH" |
michael@0 | 296 | + if (*buf_used > 0 && buf[(*buf_used) - 1] != ARM_EXTBL_OP_FINISH) |
michael@0 | 297 | + PUT_BUF_U8(ARM_EXTBL_OP_FINISH); |
michael@0 | 298 | + |
michael@0 | 299 | + return ExSuccess; |
michael@0 | 300 | + |
michael@0 | 301 | +# undef GET_EXTAB_U32 |
michael@0 | 302 | +# undef GET_EXIDX_U32 |
michael@0 | 303 | +# undef GET_U32 |
michael@0 | 304 | +# undef PUT_BUF_U8 |
michael@0 | 305 | +} |
michael@0 | 306 | + |
michael@0 | 307 | + |
michael@0 | 308 | +// Take the unwind information extracted by ExtabEntryExtract |
michael@0 | 309 | +// and parse it into frame-unwind instructions. These are as |
michael@0 | 310 | +// specified in "Table 4, ARM-defined frame-unwinding instructions" |
michael@0 | 311 | +// in the specification document detailed in comments at the top |
michael@0 | 312 | +// of this file. |
michael@0 | 313 | +// |
michael@0 | 314 | +// This reads from |buf[0, +data_size)|. It checks for overruns of |
michael@0 | 315 | +// the input buffer and returns a negative value if that happens, or |
michael@0 | 316 | +// for any other failure cases. It returns zero in case of success. |
michael@0 | 317 | +int ExceptionTableInfo::ExtabEntryDecode(const uint8_t* buf, size_t buf_size) |
michael@0 | 318 | +{ |
michael@0 | 319 | + if (buf == NULL || buf_size == 0) |
michael@0 | 320 | + return -1; |
michael@0 | 321 | + |
michael@0 | 322 | + MemoryRange mr_in(buf, buf_size); |
michael@0 | 323 | + const uint8_t* buf_initially = buf; |
michael@0 | 324 | + |
michael@0 | 325 | +# define GET_BUF_U8(_lval) \ |
michael@0 | 326 | + do { if (!mr_in.Covers(buf - buf_initially, 1)) return -1; \ |
michael@0 | 327 | + (_lval) = *(buf++); } while (0) |
michael@0 | 328 | + |
michael@0 | 329 | + const uint8_t* end = buf + buf_size; |
michael@0 | 330 | + |
michael@0 | 331 | + while (buf < end) { |
michael@0 | 332 | + struct arm_ex_to_module::extab_data edata; |
michael@0 | 333 | + memset(&edata, 0, sizeof(edata)); |
michael@0 | 334 | + |
michael@0 | 335 | + uint8_t op; |
michael@0 | 336 | + GET_BUF_U8(op); |
michael@0 | 337 | + if ((op & 0xc0) == 0x00) { |
michael@0 | 338 | + // vsp = vsp + (xxxxxx << 2) + 4 |
michael@0 | 339 | + edata.cmd = ARM_EXIDX_CMD_ADD_TO_VSP; |
michael@0 | 340 | + edata.data = (((int)op & 0x3f) << 2) + 4; |
michael@0 | 341 | + } |
michael@0 | 342 | + else if ((op & 0xc0) == 0x40) { |
michael@0 | 343 | + // vsp = vsp - (xxxxxx << 2) - 4 |
michael@0 | 344 | + edata.cmd = ARM_EXIDX_CMD_SUB_FROM_VSP; |
michael@0 | 345 | + edata.data = (((int)op & 0x3f) << 2) + 4; |
michael@0 | 346 | + } |
michael@0 | 347 | + else if ((op & 0xf0) == 0x80) { |
michael@0 | 348 | + uint8_t op2; |
michael@0 | 349 | + GET_BUF_U8(op2); |
michael@0 | 350 | + if (op == 0x80 && op2 == 0x00) { |
michael@0 | 351 | + // Refuse to unwind |
michael@0 | 352 | + edata.cmd = ARM_EXIDX_CMD_REFUSED; |
michael@0 | 353 | + } else { |
michael@0 | 354 | + // Pop up to 12 integer registers under masks {r15-r12},{r11-r4} |
michael@0 | 355 | + edata.cmd = ARM_EXIDX_CMD_REG_POP; |
michael@0 | 356 | + edata.data = ((op & 0xf) << 8) | op2; |
michael@0 | 357 | + edata.data = edata.data << 4; |
michael@0 | 358 | + } |
michael@0 | 359 | + } |
michael@0 | 360 | + else if ((op & 0xf0) == 0x90) { |
michael@0 | 361 | + if (op == 0x9d || op == 0x9f) { |
michael@0 | 362 | + // 9d: Reserved as prefix for ARM register to register moves |
michael@0 | 363 | + // 9f: Reserved as perfix for Intel Wireless MMX reg to reg moves |
michael@0 | 364 | + edata.cmd = ARM_EXIDX_CMD_RESERVED; |
michael@0 | 365 | + } else { |
michael@0 | 366 | + // Set vsp = r[nnnn] |
michael@0 | 367 | + edata.cmd = ARM_EXIDX_CMD_REG_TO_SP; |
michael@0 | 368 | + edata.data = op & 0x0f; |
michael@0 | 369 | + } |
michael@0 | 370 | + } |
michael@0 | 371 | + else if ((op & 0xf0) == 0xa0) { |
michael@0 | 372 | + // Pop r4 to r[4+nnn], or |
michael@0 | 373 | + // Pop r4 to r[4+nnn] and r14 or |
michael@0 | 374 | + unsigned end = (op & 0x07); |
michael@0 | 375 | + edata.data = (1 << (end + 1)) - 1; |
michael@0 | 376 | + edata.data = edata.data << 4; |
michael@0 | 377 | + if (op & 0x08) edata.data |= 1 << 14; |
michael@0 | 378 | + edata.cmd = ARM_EXIDX_CMD_REG_POP; |
michael@0 | 379 | + } |
michael@0 | 380 | + else if (op == ARM_EXTBL_OP_FINISH) { |
michael@0 | 381 | + // Finish |
michael@0 | 382 | + edata.cmd = ARM_EXIDX_CMD_FINISH; |
michael@0 | 383 | + buf = end; |
michael@0 | 384 | + } |
michael@0 | 385 | + else if (op == 0xb1) { |
michael@0 | 386 | + uint8_t op2; |
michael@0 | 387 | + GET_BUF_U8(op2); |
michael@0 | 388 | + if (op2 == 0 || (op2 & 0xf0)) { |
michael@0 | 389 | + // Spare |
michael@0 | 390 | + edata.cmd = ARM_EXIDX_CMD_RESERVED; |
michael@0 | 391 | + } else { |
michael@0 | 392 | + // Pop integer registers under mask {r3,r2,r1,r0} |
michael@0 | 393 | + edata.cmd = ARM_EXIDX_CMD_REG_POP; |
michael@0 | 394 | + edata.data = op2 & 0x0f; |
michael@0 | 395 | + } |
michael@0 | 396 | + } |
michael@0 | 397 | + else if (op == 0xb2) { |
michael@0 | 398 | + // vsp = vsp + 0x204 + (uleb128 << 2) |
michael@0 | 399 | + uint64_t offset = 0; |
michael@0 | 400 | + uint8_t byte, shift = 0; |
michael@0 | 401 | + do { |
michael@0 | 402 | + GET_BUF_U8(byte); |
michael@0 | 403 | + offset |= (byte & 0x7f) << shift; |
michael@0 | 404 | + shift += 7; |
michael@0 | 405 | + } while ((byte & 0x80) && buf < end); |
michael@0 | 406 | + edata.data = offset * 4 + 0x204; |
michael@0 | 407 | + edata.cmd = ARM_EXIDX_CMD_ADD_TO_VSP; |
michael@0 | 408 | + } |
michael@0 | 409 | + else if (op == 0xb3 || op == 0xc8 || op == 0xc9) { |
michael@0 | 410 | + // b3: Pop VFP regs D[ssss] to D[ssss+cccc], FSTMFDX-ishly |
michael@0 | 411 | + // c8: Pop VFP regs D[16+ssss] to D[16+ssss+cccc], FSTMFDD-ishly |
michael@0 | 412 | + // c9: Pop VFP regs D[ssss] to D[ssss+cccc], FSTMFDD-ishly |
michael@0 | 413 | + edata.cmd = ARM_EXIDX_CMD_VFP_POP; |
michael@0 | 414 | + GET_BUF_U8(edata.data); |
michael@0 | 415 | + if (op == 0xc8) edata.data |= ARM_EXIDX_VFP_SHIFT_16; |
michael@0 | 416 | + if (op != 0xb3) edata.data |= ARM_EXIDX_VFP_FSTMD; |
michael@0 | 417 | + } |
michael@0 | 418 | + else if ((op & 0xf8) == 0xb8 || (op & 0xf8) == 0xd0) { |
michael@0 | 419 | + // b8: Pop VFP regs D[8] to D[8+nnn], FSTMFDX-ishly |
michael@0 | 420 | + // d0: Pop VFP regs D[8] to D[8+nnn], FSTMFDD-ishly |
michael@0 | 421 | + edata.cmd = ARM_EXIDX_CMD_VFP_POP; |
michael@0 | 422 | + edata.data = 0x80 | (op & 0x07); |
michael@0 | 423 | + if ((op & 0xf8) == 0xd0) edata.data |= ARM_EXIDX_VFP_FSTMD; |
michael@0 | 424 | + } |
michael@0 | 425 | + else if (op >= 0xc0 && op <= 0xc5) { |
michael@0 | 426 | + // Intel Wireless MMX pop wR[10]-wr[10+nnn], nnn != 6,7 |
michael@0 | 427 | + edata.cmd = ARM_EXIDX_CMD_WREG_POP; |
michael@0 | 428 | + edata.data = 0xa0 | (op & 0x07); |
michael@0 | 429 | + } |
michael@0 | 430 | + else if (op == 0xc6) { |
michael@0 | 431 | + // Intel Wireless MMX pop wR[ssss] to wR[ssss+cccc] |
michael@0 | 432 | + edata.cmd = ARM_EXIDX_CMD_WREG_POP; |
michael@0 | 433 | + GET_BUF_U8(edata.data); |
michael@0 | 434 | + } |
michael@0 | 435 | + else if (op == 0xc7) { |
michael@0 | 436 | + uint8_t op2; |
michael@0 | 437 | + GET_BUF_U8(op2); |
michael@0 | 438 | + if (op2 == 0 || (op2 & 0xf0)) { |
michael@0 | 439 | + // Spare |
michael@0 | 440 | + edata.cmd = ARM_EXIDX_CMD_RESERVED; |
michael@0 | 441 | + } else { |
michael@0 | 442 | + // Intel Wireless MMX pop wCGR registers under mask {wCGR3,2,1,0} |
michael@0 | 443 | + edata.cmd = ARM_EXIDX_CMD_WCGR_POP; |
michael@0 | 444 | + edata.data = op2 & 0x0f; |
michael@0 | 445 | + } |
michael@0 | 446 | + } |
michael@0 | 447 | + else { |
michael@0 | 448 | + // Spare |
michael@0 | 449 | + edata.cmd = ARM_EXIDX_CMD_RESERVED; |
michael@0 | 450 | + } |
michael@0 | 451 | + |
michael@0 | 452 | + int ret = handler_->ImproveStackFrame(&edata); |
michael@0 | 453 | + if (ret < 0) return ret; |
michael@0 | 454 | + } |
michael@0 | 455 | + return 0; |
michael@0 | 456 | + |
michael@0 | 457 | +# undef GET_BUF_U8 |
michael@0 | 458 | +} |
michael@0 | 459 | + |
michael@0 | 460 | +void ExceptionTableInfo::Start() |
michael@0 | 461 | +{ |
michael@0 | 462 | + const struct exidx_entry* start |
michael@0 | 463 | + = reinterpret_cast<const struct exidx_entry*>(mr_exidx_.data()); |
michael@0 | 464 | + const struct exidx_entry* end |
michael@0 | 465 | + = reinterpret_cast<const struct exidx_entry*>(mr_exidx_.data() |
michael@0 | 466 | + + mr_exidx_.length()); |
michael@0 | 467 | + |
michael@0 | 468 | + // Iterate over each of the EXIDX entries (pairs of 32-bit words). |
michael@0 | 469 | + // These occupy the entire .exidx section. |
michael@0 | 470 | + for (const struct exidx_entry* entry = start; entry < end; ++entry) { |
michael@0 | 471 | + |
michael@0 | 472 | + // Figure out the code address range that this table entry is |
michael@0 | 473 | + // associated with. |
michael@0 | 474 | + uint32_t addr = (reinterpret_cast<char*>(Prel31ToAddr(&entry->addr)) |
michael@0 | 475 | + - mapping_addr_ + loading_addr_) & 0x7fffffff; |
michael@0 | 476 | + uint32_t next_addr; |
michael@0 | 477 | + if (entry < end - 1) |
michael@0 | 478 | + next_addr = (reinterpret_cast<char*>(Prel31ToAddr(&((entry + 1)->addr))) |
michael@0 | 479 | + - mapping_addr_ + loading_addr_) & 0x7fffffff; |
michael@0 | 480 | + else { |
michael@0 | 481 | + // This is the last EXIDX entry in the sequence, so we don't |
michael@0 | 482 | + // have an address for the start of the next function, to limit |
michael@0 | 483 | + // this one. Instead use the address of the last byte of the |
michael@0 | 484 | + // text section associated with this .exidx section, that we |
michael@0 | 485 | + // have been given. So as to avoid junking up the CFI unwind |
michael@0 | 486 | + // tables with absurdly large address ranges in the case where |
michael@0 | 487 | + // text_last_svma_ is wrong, only use the value if it is nonzero |
michael@0 | 488 | + // and within one page of |addr|. Otherwise assume a length of 1. |
michael@0 | 489 | + // |
michael@0 | 490 | + // In some cases, gcc has been observed to finish the exidx |
michael@0 | 491 | + // section with an entry of length 1 marked CANT_UNWIND, |
michael@0 | 492 | + // presumably exactly for the purpose of giving a definite |
michael@0 | 493 | + // length for the last real entry, without having to look at |
michael@0 | 494 | + // text segment boundaries. |
michael@0 | 495 | + bool plausible = false; |
michael@0 | 496 | + next_addr = addr + 1; |
michael@0 | 497 | + if (text_last_svma_ != 0) { |
michael@0 | 498 | + uint32_t maybe_next_addr = text_last_svma_ + 1; |
michael@0 | 499 | + if (maybe_next_addr > addr && maybe_next_addr - addr <= 4096) { |
michael@0 | 500 | + next_addr = maybe_next_addr; |
michael@0 | 501 | + plausible = true; |
michael@0 | 502 | + } |
michael@0 | 503 | + } |
michael@0 | 504 | + if (!plausible) |
michael@0 | 505 | + BPLOG(INFO) << "ExceptionTableInfo: implausible EXIDX last entry size " |
michael@0 | 506 | + << (int32_t)(text_last_svma_ - addr) |
michael@0 | 507 | + << "; using 1 instead."; |
michael@0 | 508 | + } |
michael@0 | 509 | + |
michael@0 | 510 | + // Extract the unwind info into |buf|. This might fail for |
michael@0 | 511 | + // various reasons. It involves reading both the .exidx and |
michael@0 | 512 | + // .extab sections. All accesses to those sections are |
michael@0 | 513 | + // bounds-checked. |
michael@0 | 514 | + uint8_t buf[ARM_EXIDX_TABLE_LIMIT]; |
michael@0 | 515 | + size_t buf_used = 0; |
michael@0 | 516 | + ExExtractResult res = ExtabEntryExtract(entry, buf, sizeof(buf), &buf_used); |
michael@0 | 517 | + if (res != ExSuccess) { |
michael@0 | 518 | + // Couldn't extract the unwind info, for some reason. Move on. |
michael@0 | 519 | + switch (res) { |
michael@0 | 520 | + case ExInBufOverflow: |
michael@0 | 521 | + BPLOG(INFO) << "ExtabEntryExtract: .exidx/.extab section overrun"; |
michael@0 | 522 | + break; |
michael@0 | 523 | + case ExOutBufOverflow: |
michael@0 | 524 | + BPLOG(INFO) << "ExtabEntryExtract: bytecode buffer overflow"; |
michael@0 | 525 | + break; |
michael@0 | 526 | + case ExCantUnwind: |
michael@0 | 527 | + BPLOG(INFO) << "ExtabEntryExtract: function is marked CANT_UNWIND"; |
michael@0 | 528 | + break; |
michael@0 | 529 | + case ExCantRepresent: |
michael@0 | 530 | + BPLOG(INFO) << "ExtabEntryExtract: bytecode can't be represented"; |
michael@0 | 531 | + break; |
michael@0 | 532 | + case ExInvalid: |
michael@0 | 533 | + BPLOG(INFO) << "ExtabEntryExtract: index table entry is invalid"; |
michael@0 | 534 | + break; |
michael@0 | 535 | + default: |
michael@0 | 536 | + BPLOG(INFO) << "ExtabEntryExtract: unknown error: " << (int)res; |
michael@0 | 537 | + break; |
michael@0 | 538 | + } |
michael@0 | 539 | + continue; |
michael@0 | 540 | + } |
michael@0 | 541 | + |
michael@0 | 542 | + // Finally, work through the unwind instructions in |buf| and |
michael@0 | 543 | + // create CFI entries that Breakpad can use. This can also fail. |
michael@0 | 544 | + // First, add a new stack frame entry, into which ExtabEntryDecode |
michael@0 | 545 | + // will write the CFI entries. |
michael@0 | 546 | + handler_->AddStackFrame(addr, next_addr - addr); |
michael@0 | 547 | + int ret = ExtabEntryDecode(buf, buf_used); |
michael@0 | 548 | + if (ret < 0) { |
michael@0 | 549 | + handler_->DeleteStackFrame(); |
michael@0 | 550 | + BPLOG(INFO) << "ExtabEntryDecode: failed with error code: " << ret; |
michael@0 | 551 | + continue; |
michael@0 | 552 | + } |
michael@0 | 553 | + handler_->SubmitStackFrame(); |
michael@0 | 554 | + |
michael@0 | 555 | + } /* iterating over .exidx */ |
michael@0 | 556 | +} |
michael@0 | 557 | + |
michael@0 | 558 | +} // arm_ex_reader |
michael@0 | 559 | diff --git a/src/common/arm_ex_reader.h b/src/common/arm_ex_reader.h |
michael@0 | 560 | new file mode 100644 |
michael@0 | 561 | --- /dev/null |
michael@0 | 562 | +++ b/src/common/arm_ex_reader.h |
michael@0 | 563 | @@ -0,0 +1,115 @@ |
michael@0 | 564 | + |
michael@0 | 565 | +/* libunwind - a platform-independent unwind library |
michael@0 | 566 | + Copyright 2011 Linaro Limited |
michael@0 | 567 | + |
michael@0 | 568 | +This file is part of libunwind. |
michael@0 | 569 | + |
michael@0 | 570 | +Permission is hereby granted, free of charge, to any person obtaining |
michael@0 | 571 | +a copy of this software and associated documentation files (the |
michael@0 | 572 | +"Software"), to deal in the Software without restriction, including |
michael@0 | 573 | +without limitation the rights to use, copy, modify, merge, publish, |
michael@0 | 574 | +distribute, sublicense, and/or sell copies of the Software, and to |
michael@0 | 575 | +permit persons to whom the Software is furnished to do so, subject to |
michael@0 | 576 | +the following conditions: |
michael@0 | 577 | + |
michael@0 | 578 | +The above copyright notice and this permission notice shall be |
michael@0 | 579 | +included in all copies or substantial portions of the Software. |
michael@0 | 580 | + |
michael@0 | 581 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
michael@0 | 582 | +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
michael@0 | 583 | +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
michael@0 | 584 | +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
michael@0 | 585 | +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
michael@0 | 586 | +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
michael@0 | 587 | +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
michael@0 | 588 | + |
michael@0 | 589 | +// Copyright (c) 2010 Google Inc. |
michael@0 | 590 | +// All rights reserved. |
michael@0 | 591 | +// |
michael@0 | 592 | +// Redistribution and use in source and binary forms, with or without |
michael@0 | 593 | +// modification, are permitted provided that the following conditions are |
michael@0 | 594 | +// met: |
michael@0 | 595 | +// |
michael@0 | 596 | +// * Redistributions of source code must retain the above copyright |
michael@0 | 597 | +// notice, this list of conditions and the following disclaimer. |
michael@0 | 598 | +// * Redistributions in binary form must reproduce the above |
michael@0 | 599 | +// copyright notice, this list of conditions and the following disclaimer |
michael@0 | 600 | +// in the documentation and/or other materials provided with the |
michael@0 | 601 | +// distribution. |
michael@0 | 602 | +// * Neither the name of Google Inc. nor the names of its |
michael@0 | 603 | +// contributors may be used to endorse or promote products derived from |
michael@0 | 604 | +// this software without specific prior written permission. |
michael@0 | 605 | +// |
michael@0 | 606 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 607 | +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 608 | +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 609 | +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 610 | +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 611 | +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 612 | +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 613 | +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 614 | +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 615 | +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 616 | +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 617 | + |
michael@0 | 618 | + |
michael@0 | 619 | +// Derived from libunwind, with extensive modifications. |
michael@0 | 620 | + |
michael@0 | 621 | +#ifndef COMMON_ARM_EX_READER_H__ |
michael@0 | 622 | +#define COMMON_ARM_EX_READER_H__ |
michael@0 | 623 | + |
michael@0 | 624 | +#include "common/arm_ex_to_module.h" |
michael@0 | 625 | +#include "common/memory_range.h" |
michael@0 | 626 | + |
michael@0 | 627 | +namespace arm_ex_reader { |
michael@0 | 628 | + |
michael@0 | 629 | +// This class is a reader for ARM unwind information |
michael@0 | 630 | +// from .ARM.exidx and .ARM.extab sections. |
michael@0 | 631 | +class ExceptionTableInfo { |
michael@0 | 632 | + public: |
michael@0 | 633 | + ExceptionTableInfo(const char* exidx, size_t exidx_size, |
michael@0 | 634 | + const char* extab, size_t extab_size, |
michael@0 | 635 | + uint32_t text_last_svma, |
michael@0 | 636 | + arm_ex_to_module::ARMExToModule* handler, |
michael@0 | 637 | + const char* mapping_addr, |
michael@0 | 638 | + uint32_t loading_addr) |
michael@0 | 639 | + : mr_exidx_(google_breakpad::MemoryRange(exidx, exidx_size)), |
michael@0 | 640 | + mr_extab_(google_breakpad::MemoryRange(extab, extab_size)), |
michael@0 | 641 | + text_last_svma_(text_last_svma), |
michael@0 | 642 | + handler_(handler), mapping_addr_(mapping_addr), |
michael@0 | 643 | + loading_addr_(loading_addr) { } |
michael@0 | 644 | + |
michael@0 | 645 | + ~ExceptionTableInfo() { } |
michael@0 | 646 | + |
michael@0 | 647 | + // Parses the entries in .ARM.exidx and possibly |
michael@0 | 648 | + // in .ARM.extab tables, reports what we find to |
michael@0 | 649 | + // arm_ex_to_module::ARMExToModule. |
michael@0 | 650 | + void Start(); |
michael@0 | 651 | + |
michael@0 | 652 | + private: |
michael@0 | 653 | + google_breakpad::MemoryRange mr_exidx_; |
michael@0 | 654 | + google_breakpad::MemoryRange mr_extab_; |
michael@0 | 655 | + uint32_t text_last_svma_; |
michael@0 | 656 | + arm_ex_to_module::ARMExToModule* handler_; |
michael@0 | 657 | + const char* mapping_addr_; |
michael@0 | 658 | + uint32_t loading_addr_; |
michael@0 | 659 | + |
michael@0 | 660 | + enum ExExtractResult { |
michael@0 | 661 | + ExSuccess, // success |
michael@0 | 662 | + ExInBufOverflow, // out-of-range while reading .exidx |
michael@0 | 663 | + ExOutBufOverflow, // output buffer is too small |
michael@0 | 664 | + ExCantUnwind, // this function is marked CANT_UNWIND |
michael@0 | 665 | + ExCantRepresent, // entry valid, but we can't represent it |
michael@0 | 666 | + ExInvalid // entry is invalid |
michael@0 | 667 | + }; |
michael@0 | 668 | + ExExtractResult |
michael@0 | 669 | + ExtabEntryExtract(const struct arm_ex_to_module::exidx_entry* entry, |
michael@0 | 670 | + uint8_t* buf, size_t buf_size, |
michael@0 | 671 | + /*OUT*/size_t* buf_used); |
michael@0 | 672 | + |
michael@0 | 673 | + int ExtabEntryDecode(const uint8_t* buf, size_t buf_size); |
michael@0 | 674 | +}; |
michael@0 | 675 | + |
michael@0 | 676 | +} // namespace arm_ex_reader |
michael@0 | 677 | + |
michael@0 | 678 | +#endif // COMMON_ARM_EX_READER_H__ |
michael@0 | 679 | diff --git a/src/common/arm_ex_to_module.cc b/src/common/arm_ex_to_module.cc |
michael@0 | 680 | new file mode 100644 |
michael@0 | 681 | --- /dev/null |
michael@0 | 682 | +++ b/src/common/arm_ex_to_module.cc |
michael@0 | 683 | @@ -0,0 +1,206 @@ |
michael@0 | 684 | + |
michael@0 | 685 | +/* libunwind - a platform-independent unwind library |
michael@0 | 686 | + Copyright 2011 Linaro Limited |
michael@0 | 687 | + |
michael@0 | 688 | +This file is part of libunwind. |
michael@0 | 689 | + |
michael@0 | 690 | +Permission is hereby granted, free of charge, to any person obtaining |
michael@0 | 691 | +a copy of this software and associated documentation files (the |
michael@0 | 692 | +"Software"), to deal in the Software without restriction, including |
michael@0 | 693 | +without limitation the rights to use, copy, modify, merge, publish, |
michael@0 | 694 | +distribute, sublicense, and/or sell copies of the Software, and to |
michael@0 | 695 | +permit persons to whom the Software is furnished to do so, subject to |
michael@0 | 696 | +the following conditions: |
michael@0 | 697 | + |
michael@0 | 698 | +The above copyright notice and this permission notice shall be |
michael@0 | 699 | +included in all copies or substantial portions of the Software. |
michael@0 | 700 | + |
michael@0 | 701 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
michael@0 | 702 | +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
michael@0 | 703 | +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
michael@0 | 704 | +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
michael@0 | 705 | +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
michael@0 | 706 | +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
michael@0 | 707 | +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
michael@0 | 708 | + |
michael@0 | 709 | +// Copyright (c) 2010 Google Inc. |
michael@0 | 710 | +// All rights reserved. |
michael@0 | 711 | +// |
michael@0 | 712 | +// Redistribution and use in source and binary forms, with or without |
michael@0 | 713 | +// modification, are permitted provided that the following conditions are |
michael@0 | 714 | +// met: |
michael@0 | 715 | +// |
michael@0 | 716 | +// * Redistributions of source code must retain the above copyright |
michael@0 | 717 | +// notice, this list of conditions and the following disclaimer. |
michael@0 | 718 | +// * Redistributions in binary form must reproduce the above |
michael@0 | 719 | +// copyright notice, this list of conditions and the following disclaimer |
michael@0 | 720 | +// in the documentation and/or other materials provided with the |
michael@0 | 721 | +// distribution. |
michael@0 | 722 | +// * Neither the name of Google Inc. nor the names of its |
michael@0 | 723 | +// contributors may be used to endorse or promote products derived from |
michael@0 | 724 | +// this software without specific prior written permission. |
michael@0 | 725 | +// |
michael@0 | 726 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 727 | +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 728 | +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 729 | +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 730 | +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 731 | +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 732 | +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 733 | +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 734 | +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 735 | +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 736 | +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 737 | + |
michael@0 | 738 | + |
michael@0 | 739 | +// Derived from libunwind, with extensive modifications. |
michael@0 | 740 | + |
michael@0 | 741 | +#include "common/unique_string.h" |
michael@0 | 742 | +#include "common/arm_ex_to_module.h" |
michael@0 | 743 | + |
michael@0 | 744 | +#include <stdio.h> |
michael@0 | 745 | +#include <assert.h> |
michael@0 | 746 | + |
michael@0 | 747 | +// For big-picture comments on how the EXIDX reader works, |
michael@0 | 748 | +// see arm_ex_reader.cc. |
michael@0 | 749 | + |
michael@0 | 750 | +#define ARM_EXBUF_START(x) (((x) >> 4) & 0x0f) |
michael@0 | 751 | +#define ARM_EXBUF_COUNT(x) ((x) & 0x0f) |
michael@0 | 752 | +#define ARM_EXBUF_END(x) (ARM_EXBUF_START(x) + ARM_EXBUF_COUNT(x)) |
michael@0 | 753 | + |
michael@0 | 754 | +using google_breakpad::ustr__pc; |
michael@0 | 755 | +using google_breakpad::ustr__lr; |
michael@0 | 756 | +using google_breakpad::ustr__sp; |
michael@0 | 757 | +using google_breakpad::Module; |
michael@0 | 758 | +using google_breakpad::ToUniqueString; |
michael@0 | 759 | +using google_breakpad::UniqueString; |
michael@0 | 760 | + |
michael@0 | 761 | +namespace arm_ex_to_module { |
michael@0 | 762 | + |
michael@0 | 763 | +// Translate command from extab_data to command for Module. |
michael@0 | 764 | +int ARMExToModule::TranslateCmd(const struct extab_data* edata, |
michael@0 | 765 | + Module::StackFrameEntry* entry, string& vsp) { |
michael@0 | 766 | + int ret = 0; |
michael@0 | 767 | + switch (edata->cmd) { |
michael@0 | 768 | + case ARM_EXIDX_CMD_FINISH: |
michael@0 | 769 | + /* Copy LR to PC if there isn't currently a rule for PC in force. */ |
michael@0 | 770 | + if (entry->initial_rules.find(ustr__pc()) |
michael@0 | 771 | + == entry->initial_rules.end()) { |
michael@0 | 772 | + if (entry->initial_rules.find(ustr__lr()) |
michael@0 | 773 | + == entry->initial_rules.end()) { |
michael@0 | 774 | + entry->initial_rules[ustr__pc()] = Module::Expr("lr"); |
michael@0 | 775 | + } else { |
michael@0 | 776 | + entry->initial_rules[ustr__pc()] = entry->initial_rules[ustr__lr()]; |
michael@0 | 777 | + } |
michael@0 | 778 | + } |
michael@0 | 779 | + break; |
michael@0 | 780 | + case ARM_EXIDX_CMD_SUB_FROM_VSP: |
michael@0 | 781 | + { |
michael@0 | 782 | + char c[16]; |
michael@0 | 783 | + sprintf(c, " %d -", edata->data); |
michael@0 | 784 | + vsp += c; |
michael@0 | 785 | + } |
michael@0 | 786 | + break; |
michael@0 | 787 | + case ARM_EXIDX_CMD_ADD_TO_VSP: |
michael@0 | 788 | + { |
michael@0 | 789 | + char c[16]; |
michael@0 | 790 | + sprintf(c, " %d +", edata->data); |
michael@0 | 791 | + vsp += c; |
michael@0 | 792 | + } |
michael@0 | 793 | + break; |
michael@0 | 794 | + case ARM_EXIDX_CMD_REG_POP: |
michael@0 | 795 | + for (unsigned int i = 0; i < 16; i++) { |
michael@0 | 796 | + if (edata->data & (1 << i)) { |
michael@0 | 797 | + entry->initial_rules[ToUniqueString(regnames[i])] |
michael@0 | 798 | + = Module::Expr(vsp + " ^"); |
michael@0 | 799 | + vsp += " 4 +"; |
michael@0 | 800 | + } |
michael@0 | 801 | + } |
michael@0 | 802 | + /* Set cfa in case the SP got popped. */ |
michael@0 | 803 | + if (edata->data & (1 << 13)) { |
michael@0 | 804 | + Module::Expr& vsp_expr = entry->initial_rules[ustr__sp()]; |
michael@0 | 805 | + // It must be a postfix expression (we don't generate anything |
michael@0 | 806 | + // else here), so return -1 to fail out if it isn't. |
michael@0 | 807 | + if (!vsp_expr.isExprPostfix()) { |
michael@0 | 808 | + ret = -1; |
michael@0 | 809 | + break; |
michael@0 | 810 | + }; |
michael@0 | 811 | + vsp = vsp_expr.getExprPostfix(); |
michael@0 | 812 | + } |
michael@0 | 813 | + break; |
michael@0 | 814 | + case ARM_EXIDX_CMD_REG_TO_SP: { |
michael@0 | 815 | + assert (edata->data < 16); |
michael@0 | 816 | + const char* const regname = regnames[edata->data]; |
michael@0 | 817 | + const UniqueString* regname_us = ToUniqueString(regname); |
michael@0 | 818 | + if (entry->initial_rules.find(regname_us) == entry->initial_rules.end()) { |
michael@0 | 819 | + entry->initial_rules[ustr__sp()] = Module::Expr(regname); |
michael@0 | 820 | + } else { |
michael@0 | 821 | + entry->initial_rules[ustr__sp()] = entry->initial_rules[regname_us]; |
michael@0 | 822 | + } |
michael@0 | 823 | + Module::Expr& vsp_expr = entry->initial_rules[ustr__sp()]; |
michael@0 | 824 | + if (!vsp_expr.isExprPostfix()) { |
michael@0 | 825 | + ret = -1; |
michael@0 | 826 | + break; |
michael@0 | 827 | + }; |
michael@0 | 828 | + vsp = vsp_expr.getExprPostfix(); |
michael@0 | 829 | + break; |
michael@0 | 830 | + } |
michael@0 | 831 | + case ARM_EXIDX_CMD_VFP_POP: |
michael@0 | 832 | + /* Don't recover VFP registers, but be sure to adjust the stack |
michael@0 | 833 | + pointer. */ |
michael@0 | 834 | + for (unsigned int i = ARM_EXBUF_START(edata->data); |
michael@0 | 835 | + i <= ARM_EXBUF_END(edata->data); i++) { |
michael@0 | 836 | + vsp += " 8 +"; |
michael@0 | 837 | + } |
michael@0 | 838 | + if (!(edata->data & ARM_EXIDX_VFP_FSTMD)) { |
michael@0 | 839 | + vsp += " 4 +"; |
michael@0 | 840 | + } |
michael@0 | 841 | + break; |
michael@0 | 842 | + case ARM_EXIDX_CMD_WREG_POP: |
michael@0 | 843 | + for (unsigned int i = ARM_EXBUF_START(edata->data); |
michael@0 | 844 | + i <= ARM_EXBUF_END(edata->data); i++) { |
michael@0 | 845 | + vsp += " 8 +"; |
michael@0 | 846 | + } |
michael@0 | 847 | + break; |
michael@0 | 848 | + case ARM_EXIDX_CMD_WCGR_POP: |
michael@0 | 849 | + // Pop wCGR registers under mask {wCGR3,2,1,0}, hence "i < 4" |
michael@0 | 850 | + for (unsigned int i = 0; i < 4; i++) { |
michael@0 | 851 | + if (edata->data & (1 << i)) { |
michael@0 | 852 | + vsp += " 4 +"; |
michael@0 | 853 | + } |
michael@0 | 854 | + } |
michael@0 | 855 | + break; |
michael@0 | 856 | + case ARM_EXIDX_CMD_REFUSED: |
michael@0 | 857 | + case ARM_EXIDX_CMD_RESERVED: |
michael@0 | 858 | + ret = -1; |
michael@0 | 859 | + break; |
michael@0 | 860 | + } |
michael@0 | 861 | + return ret; |
michael@0 | 862 | +} |
michael@0 | 863 | + |
michael@0 | 864 | +void ARMExToModule::AddStackFrame(uintptr_t addr, size_t size) { |
michael@0 | 865 | + stack_frame_entry_ = new Module::StackFrameEntry; |
michael@0 | 866 | + stack_frame_entry_->address = addr; |
michael@0 | 867 | + stack_frame_entry_->size = size; |
michael@0 | 868 | + stack_frame_entry_->initial_rules[ToUniqueString(kCFA)] = Module::Expr("sp"); |
michael@0 | 869 | + vsp_ = "sp"; |
michael@0 | 870 | +} |
michael@0 | 871 | + |
michael@0 | 872 | +int ARMExToModule::ImproveStackFrame(const struct extab_data* edata) { |
michael@0 | 873 | + return TranslateCmd(edata, stack_frame_entry_, vsp_) ; |
michael@0 | 874 | +} |
michael@0 | 875 | + |
michael@0 | 876 | +void ARMExToModule::DeleteStackFrame() { |
michael@0 | 877 | + delete stack_frame_entry_; |
michael@0 | 878 | +} |
michael@0 | 879 | + |
michael@0 | 880 | +void ARMExToModule::SubmitStackFrame() { |
michael@0 | 881 | + // return address always winds up in pc |
michael@0 | 882 | + stack_frame_entry_->initial_rules[ToUniqueString(kRA)] |
michael@0 | 883 | + = stack_frame_entry_->initial_rules[ustr__pc()]; |
michael@0 | 884 | + // the final value of vsp is the new value of sp |
michael@0 | 885 | + stack_frame_entry_->initial_rules[ustr__sp()] = vsp_; |
michael@0 | 886 | + module_->AddStackFrameEntry(stack_frame_entry_); |
michael@0 | 887 | +} |
michael@0 | 888 | + |
michael@0 | 889 | +} // namespace arm_ex_to_module |
michael@0 | 890 | diff --git a/src/common/arm_ex_to_module.h b/src/common/arm_ex_to_module.h |
michael@0 | 891 | new file mode 100644 |
michael@0 | 892 | --- /dev/null |
michael@0 | 893 | +++ b/src/common/arm_ex_to_module.h |
michael@0 | 894 | @@ -0,0 +1,129 @@ |
michael@0 | 895 | + |
michael@0 | 896 | +/* libunwind - a platform-independent unwind library |
michael@0 | 897 | + Copyright 2011 Linaro Limited |
michael@0 | 898 | + |
michael@0 | 899 | +This file is part of libunwind. |
michael@0 | 900 | + |
michael@0 | 901 | +Permission is hereby granted, free of charge, to any person obtaining |
michael@0 | 902 | +a copy of this software and associated documentation files (the |
michael@0 | 903 | +"Software"), to deal in the Software without restriction, including |
michael@0 | 904 | +without limitation the rights to use, copy, modify, merge, publish, |
michael@0 | 905 | +distribute, sublicense, and/or sell copies of the Software, and to |
michael@0 | 906 | +permit persons to whom the Software is furnished to do so, subject to |
michael@0 | 907 | +the following conditions: |
michael@0 | 908 | + |
michael@0 | 909 | +The above copyright notice and this permission notice shall be |
michael@0 | 910 | +included in all copies or substantial portions of the Software. |
michael@0 | 911 | + |
michael@0 | 912 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
michael@0 | 913 | +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
michael@0 | 914 | +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
michael@0 | 915 | +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
michael@0 | 916 | +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
michael@0 | 917 | +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
michael@0 | 918 | +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
michael@0 | 919 | + |
michael@0 | 920 | +// Copyright (c) 2010 Google Inc. |
michael@0 | 921 | +// All rights reserved. |
michael@0 | 922 | +// |
michael@0 | 923 | +// Redistribution and use in source and binary forms, with or without |
michael@0 | 924 | +// modification, are permitted provided that the following conditions are |
michael@0 | 925 | +// met: |
michael@0 | 926 | +// |
michael@0 | 927 | +// * Redistributions of source code must retain the above copyright |
michael@0 | 928 | +// notice, this list of conditions and the following disclaimer. |
michael@0 | 929 | +// * Redistributions in binary form must reproduce the above |
michael@0 | 930 | +// copyright notice, this list of conditions and the following disclaimer |
michael@0 | 931 | +// in the documentation and/or other materials provided with the |
michael@0 | 932 | +// distribution. |
michael@0 | 933 | +// * Neither the name of Google Inc. nor the names of its |
michael@0 | 934 | +// contributors may be used to endorse or promote products derived from |
michael@0 | 935 | +// this software without specific prior written permission. |
michael@0 | 936 | +// |
michael@0 | 937 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 938 | +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 939 | +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 940 | +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 941 | +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 942 | +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 943 | +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 944 | +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 945 | +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 946 | +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 947 | +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 948 | + |
michael@0 | 949 | + |
michael@0 | 950 | +// Derived from libunwind, with extensive modifications. |
michael@0 | 951 | + |
michael@0 | 952 | +#ifndef COMMON_ARM_EX_TO_MODULE__ |
michael@0 | 953 | +#define COMMON_ARM_EX_TO_MODULE__ |
michael@0 | 954 | + |
michael@0 | 955 | +#include "common/module.h" |
michael@0 | 956 | + |
michael@0 | 957 | +#include <string.h> |
michael@0 | 958 | + |
michael@0 | 959 | +namespace arm_ex_to_module { |
michael@0 | 960 | + |
michael@0 | 961 | +using google_breakpad::Module; |
michael@0 | 962 | + |
michael@0 | 963 | +typedef enum extab_cmd { |
michael@0 | 964 | + ARM_EXIDX_CMD_FINISH, |
michael@0 | 965 | + ARM_EXIDX_CMD_SUB_FROM_VSP, |
michael@0 | 966 | + ARM_EXIDX_CMD_ADD_TO_VSP, |
michael@0 | 967 | + ARM_EXIDX_CMD_REG_POP, |
michael@0 | 968 | + ARM_EXIDX_CMD_REG_TO_SP, |
michael@0 | 969 | + ARM_EXIDX_CMD_VFP_POP, |
michael@0 | 970 | + ARM_EXIDX_CMD_WREG_POP, |
michael@0 | 971 | + ARM_EXIDX_CMD_WCGR_POP, |
michael@0 | 972 | + ARM_EXIDX_CMD_RESERVED, |
michael@0 | 973 | + ARM_EXIDX_CMD_REFUSED, |
michael@0 | 974 | +} extab_cmd_t; |
michael@0 | 975 | + |
michael@0 | 976 | +struct exidx_entry { |
michael@0 | 977 | + uint32_t addr; |
michael@0 | 978 | + uint32_t data; |
michael@0 | 979 | +}; |
michael@0 | 980 | + |
michael@0 | 981 | +struct extab_data { |
michael@0 | 982 | + extab_cmd_t cmd; |
michael@0 | 983 | + uint32_t data; |
michael@0 | 984 | +}; |
michael@0 | 985 | + |
michael@0 | 986 | +enum extab_cmd_flags { |
michael@0 | 987 | + ARM_EXIDX_VFP_SHIFT_16 = 1 << 16, |
michael@0 | 988 | + ARM_EXIDX_VFP_FSTMD = 1 << 17, // distinguishes FSTMxxD from FSTMxxX |
michael@0 | 989 | +}; |
michael@0 | 990 | + |
michael@0 | 991 | +const string kRA = ".ra"; |
michael@0 | 992 | +const string kCFA = ".cfa"; |
michael@0 | 993 | + |
michael@0 | 994 | +static const char* const regnames[] = { |
michael@0 | 995 | + "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", |
michael@0 | 996 | + "r8", "r9", "r10", "r11", "r12", "sp", "lr", "pc", |
michael@0 | 997 | + "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7", |
michael@0 | 998 | + "fps", "cpsr" |
michael@0 | 999 | +}; |
michael@0 | 1000 | + |
michael@0 | 1001 | +// Receives information from arm_ex_reader::ExceptionTableInfo |
michael@0 | 1002 | +// and adds it to the Module object |
michael@0 | 1003 | +class ARMExToModule { |
michael@0 | 1004 | + public: |
michael@0 | 1005 | + ARMExToModule(Module* module) |
michael@0 | 1006 | + : module_(module) { } |
michael@0 | 1007 | + ~ARMExToModule() { } |
michael@0 | 1008 | + void AddStackFrame(uintptr_t addr, size_t size); |
michael@0 | 1009 | + int ImproveStackFrame(const struct extab_data* edata); |
michael@0 | 1010 | + void DeleteStackFrame(); |
michael@0 | 1011 | + void SubmitStackFrame(); |
michael@0 | 1012 | + private: |
michael@0 | 1013 | + Module* module_; |
michael@0 | 1014 | + Module::StackFrameEntry* stack_frame_entry_; |
michael@0 | 1015 | + string vsp_; |
michael@0 | 1016 | + int TranslateCmd(const struct extab_data* edata, |
michael@0 | 1017 | + Module::StackFrameEntry* entry, |
michael@0 | 1018 | + string& vsp); |
michael@0 | 1019 | +}; |
michael@0 | 1020 | + |
michael@0 | 1021 | +} // namespace arm_ex_to_module |
michael@0 | 1022 | + |
michael@0 | 1023 | +#endif // COMMON_ARM_EX_TO_MODULE__ |
michael@0 | 1024 | diff --git a/src/common/linux/dump_symbols.cc b/src/common/linux/dump_symbols.cc |
michael@0 | 1025 | --- a/src/common/linux/dump_symbols.cc |
michael@0 | 1026 | +++ b/src/common/linux/dump_symbols.cc |
michael@0 | 1027 | @@ -47,16 +47,17 @@ |
michael@0 | 1028 | #include <unistd.h> |
michael@0 | 1029 | |
michael@0 | 1030 | #include <iostream> |
michael@0 | 1031 | #include <set> |
michael@0 | 1032 | #include <string> |
michael@0 | 1033 | #include <utility> |
michael@0 | 1034 | #include <vector> |
michael@0 | 1035 | |
michael@0 | 1036 | +#include "common/arm_ex_reader.h" |
michael@0 | 1037 | #include "common/dwarf/bytereader-inl.h" |
michael@0 | 1038 | #include "common/dwarf/dwarf2diehandler.h" |
michael@0 | 1039 | #include "common/dwarf_cfi_to_module.h" |
michael@0 | 1040 | #include "common/dwarf_cu_to_module.h" |
michael@0 | 1041 | #include "common/dwarf_line_to_module.h" |
michael@0 | 1042 | #include "common/linux/elfutils.h" |
michael@0 | 1043 | #include "common/linux/elfutils-inl.h" |
michael@0 | 1044 | #include "common/linux/elf_symbols_to_module.h" |
michael@0 | 1045 | @@ -65,16 +66,20 @@ |
michael@0 | 1046 | #include "common/scoped_ptr.h" |
michael@0 | 1047 | #ifndef NO_STABS_SUPPORT |
michael@0 | 1048 | #include "common/stabs_reader.h" |
michael@0 | 1049 | #include "common/stabs_to_module.h" |
michael@0 | 1050 | #endif |
michael@0 | 1051 | #include "common/using_std_string.h" |
michael@0 | 1052 | #include "common/logging.h" |
michael@0 | 1053 | |
michael@0 | 1054 | +#if defined(__ANDROID__) && !defined(SHT_ARM_EXIDX) |
michael@0 | 1055 | +# define SHT_ARM_EXIDX (SHT_LOPROC + 1) |
michael@0 | 1056 | +#endif |
michael@0 | 1057 | + |
michael@0 | 1058 | // This namespace contains helper functions. |
michael@0 | 1059 | namespace { |
michael@0 | 1060 | |
michael@0 | 1061 | using google_breakpad::DwarfCFIToModule; |
michael@0 | 1062 | using google_breakpad::DwarfCUToModule; |
michael@0 | 1063 | using google_breakpad::DwarfLineToModule; |
michael@0 | 1064 | using google_breakpad::ElfClass; |
michael@0 | 1065 | using google_breakpad::ElfClass32; |
michael@0 | 1066 | @@ -340,16 +345,62 @@ bool LoadDwarfCFI(const string& dwarf_fi |
michael@0 | 1067 | section_name); |
michael@0 | 1068 | dwarf2reader::CallFrameInfo parser(cfi, cfi_size, |
michael@0 | 1069 | &byte_reader, &handler, &dwarf_reporter, |
michael@0 | 1070 | eh_frame); |
michael@0 | 1071 | parser.Start(); |
michael@0 | 1072 | return true; |
michael@0 | 1073 | } |
michael@0 | 1074 | |
michael@0 | 1075 | +template<typename ElfClass> |
michael@0 | 1076 | +bool LoadARMexidx(const typename ElfClass::Ehdr* elf_header, |
michael@0 | 1077 | + const typename ElfClass::Shdr* exidx_section, |
michael@0 | 1078 | + const typename ElfClass::Shdr* extab_section, |
michael@0 | 1079 | + uint32_t loading_addr, |
michael@0 | 1080 | + Module* module) { |
michael@0 | 1081 | + // To do this properly we need to know: |
michael@0 | 1082 | + // * the bounds of the .ARM.exidx section in the mapped image |
michael@0 | 1083 | + // * the bounds of the .ARM.extab section in the mapped image |
michael@0 | 1084 | + // * the vma of the last byte in the text section associated with the .exidx |
michael@0 | 1085 | + // The first two are easy. The third is a bit tricky. If we can't |
michael@0 | 1086 | + // figure out what it is, just pass in zero. |
michael@0 | 1087 | + const char *exidx_img |
michael@0 | 1088 | + = GetOffset<ElfClass, char>(elf_header, exidx_section->sh_offset); |
michael@0 | 1089 | + size_t exidx_size = exidx_section->sh_size; |
michael@0 | 1090 | + const char *extab_img |
michael@0 | 1091 | + = GetOffset<ElfClass, char>(elf_header, extab_section->sh_offset); |
michael@0 | 1092 | + size_t extab_size = extab_section->sh_size; |
michael@0 | 1093 | + |
michael@0 | 1094 | + // The sh_link field of the exidx section gives the section number |
michael@0 | 1095 | + // for the associated text section. |
michael@0 | 1096 | + uint32_t exidx_text_last_svma = 0; |
michael@0 | 1097 | + int exidx_text_sno = exidx_section->sh_link; |
michael@0 | 1098 | + typedef typename ElfClass::Shdr Shdr; |
michael@0 | 1099 | + // |sections| points to the section header table |
michael@0 | 1100 | + const Shdr* sections |
michael@0 | 1101 | + = GetOffset<ElfClass, Shdr>(elf_header, elf_header->e_shoff); |
michael@0 | 1102 | + const int num_sections = elf_header->e_shnum; |
michael@0 | 1103 | + if (exidx_text_sno >= 0 && exidx_text_sno < num_sections) { |
michael@0 | 1104 | + const Shdr* exidx_text_shdr = §ions[exidx_text_sno]; |
michael@0 | 1105 | + if (exidx_text_shdr->sh_size > 0) { |
michael@0 | 1106 | + exidx_text_last_svma |
michael@0 | 1107 | + = exidx_text_shdr->sh_addr + exidx_text_shdr->sh_size - 1; |
michael@0 | 1108 | + } |
michael@0 | 1109 | + } |
michael@0 | 1110 | + |
michael@0 | 1111 | + arm_ex_to_module::ARMExToModule handler(module); |
michael@0 | 1112 | + arm_ex_reader::ExceptionTableInfo |
michael@0 | 1113 | + parser(exidx_img, exidx_size, extab_img, extab_size, exidx_text_last_svma, |
michael@0 | 1114 | + &handler, |
michael@0 | 1115 | + reinterpret_cast<const char*>(elf_header), |
michael@0 | 1116 | + loading_addr); |
michael@0 | 1117 | + parser.Start(); |
michael@0 | 1118 | + return true; |
michael@0 | 1119 | +} |
michael@0 | 1120 | + |
michael@0 | 1121 | bool LoadELF(const string& obj_file, MmapWrapper* map_wrapper, |
michael@0 | 1122 | void** elf_header) { |
michael@0 | 1123 | int obj_fd = open(obj_file.c_str(), O_RDONLY); |
michael@0 | 1124 | if (obj_fd < 0) { |
michael@0 | 1125 | fprintf(stderr, "Failed to open ELF file '%s': %s\n", |
michael@0 | 1126 | obj_file.c_str(), strerror(errno)); |
michael@0 | 1127 | return false; |
michael@0 | 1128 | } |
michael@0 | 1129 | @@ -629,16 +680,39 @@ bool LoadSymbols(const string& obj_file, |
michael@0 | 1130 | eh_frame_section, true, |
michael@0 | 1131 | got_section, text_section, big_endian, module); |
michael@0 | 1132 | found_usable_info = found_usable_info || result; |
michael@0 | 1133 | if (result) |
michael@0 | 1134 | BPLOG(INFO) << "LoadSymbols: read CFI from .eh_frame"; |
michael@0 | 1135 | } |
michael@0 | 1136 | } |
michael@0 | 1137 | |
michael@0 | 1138 | + // ARM has special unwind tables that can be used. |
michael@0 | 1139 | + const Shdr* arm_exidx_section = |
michael@0 | 1140 | + FindElfSectionByName<ElfClass>(".ARM.exidx", SHT_ARM_EXIDX, |
michael@0 | 1141 | + sections, names, names_end, |
michael@0 | 1142 | + elf_header->e_shnum); |
michael@0 | 1143 | + const Shdr* arm_extab_section = |
michael@0 | 1144 | + FindElfSectionByName<ElfClass>(".ARM.extab", SHT_PROGBITS, |
michael@0 | 1145 | + sections, names, names_end, |
michael@0 | 1146 | + elf_header->e_shnum); |
michael@0 | 1147 | + // Only load information from this section if there isn't a .debug_info |
michael@0 | 1148 | + // section. |
michael@0 | 1149 | + if (!found_debug_info_section |
michael@0 | 1150 | + && arm_exidx_section && arm_extab_section && symbol_data != NO_CFI) { |
michael@0 | 1151 | + info->LoadedSection(".ARM.exidx"); |
michael@0 | 1152 | + info->LoadedSection(".ARM.extab"); |
michael@0 | 1153 | + bool result = LoadARMexidx<ElfClass>(elf_header, |
michael@0 | 1154 | + arm_exidx_section, arm_extab_section, |
michael@0 | 1155 | + loading_addr, module); |
michael@0 | 1156 | + found_usable_info = found_usable_info || result; |
michael@0 | 1157 | + if (result) |
michael@0 | 1158 | + BPLOG(INFO) << "LoadSymbols: read EXIDX from .ARM.{exidx,extab}"; |
michael@0 | 1159 | + } |
michael@0 | 1160 | + |
michael@0 | 1161 | if (!found_debug_info_section && symbol_data != ONLY_CFI) { |
michael@0 | 1162 | fprintf(stderr, "%s: file contains no debugging information" |
michael@0 | 1163 | " (no \".stab\" or \".debug_info\" sections)\n", |
michael@0 | 1164 | obj_file.c_str()); |
michael@0 | 1165 | |
michael@0 | 1166 | // Failed, but maybe there's a .gnu_debuglink section? |
michael@0 | 1167 | if (read_gnu_debug_link) { |
michael@0 | 1168 | const Shdr* gnu_debuglink_section |
michael@0 | 1169 | diff --git a/src/common/module.cc b/src/common/module.cc |
michael@0 | 1170 | --- a/src/common/module.cc |
michael@0 | 1171 | +++ b/src/common/module.cc |
michael@0 | 1172 | @@ -253,17 +253,17 @@ void Module::AssignSourceIds() { |
michael@0 | 1173 | |
michael@0 | 1174 | bool Module::ReportError() { |
michael@0 | 1175 | fprintf(stderr, "error writing symbol file: %s\n", |
michael@0 | 1176 | strerror(errno)); |
michael@0 | 1177 | return false; |
michael@0 | 1178 | } |
michael@0 | 1179 | |
michael@0 | 1180 | std::ostream& operator<<(std::ostream& stream, const Module::Expr& expr) { |
michael@0 | 1181 | - assert(!expr.invalid()); |
michael@0 | 1182 | + assert(!expr.isExprInvalid()); |
michael@0 | 1183 | switch (expr.how_) { |
michael@0 | 1184 | case Module::kExprSimple: |
michael@0 | 1185 | stream << FromUniqueString(expr.ident_) << " " << expr.offset_ << " +"; |
michael@0 | 1186 | break; |
michael@0 | 1187 | case Module::kExprSimpleMem: |
michael@0 | 1188 | stream << FromUniqueString(expr.ident_) << " " << expr.offset_ << " + ^"; |
michael@0 | 1189 | break; |
michael@0 | 1190 | case Module::kExprPostfix: |
michael@0 | 1191 | diff --git a/src/common/module.h b/src/common/module.h |
michael@0 | 1192 | --- a/src/common/module.h |
michael@0 | 1193 | +++ b/src/common/module.h |
michael@0 | 1194 | @@ -160,17 +160,24 @@ class Module { |
michael@0 | 1195 | } |
michael@0 | 1196 | // Construct an invalid expression |
michael@0 | 1197 | Expr() { |
michael@0 | 1198 | postfix_ = ""; |
michael@0 | 1199 | ident_ = NULL; |
michael@0 | 1200 | offset_ = 0; |
michael@0 | 1201 | how_ = kExprInvalid; |
michael@0 | 1202 | } |
michael@0 | 1203 | - bool invalid() const { return how_ == kExprInvalid; } |
michael@0 | 1204 | + bool isExprInvalid() const { return how_ == kExprInvalid; } |
michael@0 | 1205 | + bool isExprPostfix() const { return how_ == kExprPostfix; } |
michael@0 | 1206 | + |
michael@0 | 1207 | + // Return the postfix expression string. This is only |
michael@0 | 1208 | + // meaningful on Exprs for which isExprPostfix returns true. |
michael@0 | 1209 | + // In all other cases it returns an empty string. |
michael@0 | 1210 | + string getExprPostfix() const { return postfix_; } |
michael@0 | 1211 | + |
michael@0 | 1212 | bool operator==(const Expr& other) const { |
michael@0 | 1213 | return how_ == other.how_ && |
michael@0 | 1214 | ident_ == other.ident_ && |
michael@0 | 1215 | offset_ == other.offset_ && |
michael@0 | 1216 | postfix_ == other.postfix_; |
michael@0 | 1217 | } |
michael@0 | 1218 | |
michael@0 | 1219 | // The identifier that gives the starting value for simple expressions. |
michael@0 | 1220 | diff --git a/src/common/unique_string.h b/src/common/unique_string.h |
michael@0 | 1221 | --- a/src/common/unique_string.h |
michael@0 | 1222 | +++ b/src/common/unique_string.h |
michael@0 | 1223 | @@ -230,16 +230,37 @@ inline static const UniqueString* ustr__ |
michael@0 | 1224 | |
michael@0 | 1225 | // ".ra" |
michael@0 | 1226 | inline static const UniqueString* ustr__ZDra() { |
michael@0 | 1227 | static const UniqueString* us = NULL; |
michael@0 | 1228 | if (!us) us = ToUniqueString(".ra"); |
michael@0 | 1229 | return us; |
michael@0 | 1230 | } |
michael@0 | 1231 | |
michael@0 | 1232 | +// "pc" |
michael@0 | 1233 | +inline static const UniqueString* ustr__pc() { |
michael@0 | 1234 | + static const UniqueString* us = NULL; |
michael@0 | 1235 | + if (!us) us = ToUniqueString("pc"); |
michael@0 | 1236 | + return us; |
michael@0 | 1237 | +} |
michael@0 | 1238 | + |
michael@0 | 1239 | +// "lr" |
michael@0 | 1240 | +inline static const UniqueString* ustr__lr() { |
michael@0 | 1241 | + static const UniqueString* us = NULL; |
michael@0 | 1242 | + if (!us) us = ToUniqueString("lr"); |
michael@0 | 1243 | + return us; |
michael@0 | 1244 | +} |
michael@0 | 1245 | + |
michael@0 | 1246 | +// "sp" |
michael@0 | 1247 | +inline static const UniqueString* ustr__sp() { |
michael@0 | 1248 | + static const UniqueString* us = NULL; |
michael@0 | 1249 | + if (!us) us = ToUniqueString("sp"); |
michael@0 | 1250 | + return us; |
michael@0 | 1251 | +} |
michael@0 | 1252 | + |
michael@0 | 1253 | template <typename ValueType> |
michael@0 | 1254 | class UniqueStringMap |
michael@0 | 1255 | { |
michael@0 | 1256 | private: |
michael@0 | 1257 | static const int N_FIXED = 10; |
michael@0 | 1258 | |
michael@0 | 1259 | public: |
michael@0 | 1260 | UniqueStringMap() : n_fixed_(0), n_sets_(0), n_gets_(0), n_clears_(0) {}; |
michael@0 | 1261 | diff --git a/src/processor/cfi_frame_info.cc b/src/processor/cfi_frame_info.cc |
michael@0 | 1262 | --- a/src/processor/cfi_frame_info.cc |
michael@0 | 1263 | +++ b/src/processor/cfi_frame_info.cc |
michael@0 | 1264 | @@ -49,17 +49,17 @@ namespace google_breakpad { |
michael@0 | 1265 | #endif |
michael@0 | 1266 | |
michael@0 | 1267 | template<typename V> |
michael@0 | 1268 | bool CFIFrameInfo::FindCallerRegs(const RegisterValueMap<V> ®isters, |
michael@0 | 1269 | const MemoryRegion &memory, |
michael@0 | 1270 | RegisterValueMap<V> *caller_registers) const { |
michael@0 | 1271 | // If there are not rules for both .ra and .cfa in effect at this address, |
michael@0 | 1272 | // don't use this CFI data for stack walking. |
michael@0 | 1273 | - if (cfa_rule_.invalid() || ra_rule_.invalid()) |
michael@0 | 1274 | + if (cfa_rule_.isExprInvalid() || ra_rule_.isExprInvalid()) |
michael@0 | 1275 | return false; |
michael@0 | 1276 | |
michael@0 | 1277 | RegisterValueMap<V> working; |
michael@0 | 1278 | PostfixEvaluator<V> evaluator(&working, &memory); |
michael@0 | 1279 | |
michael@0 | 1280 | caller_registers->clear(); |
michael@0 | 1281 | |
michael@0 | 1282 | // First, compute the CFA. |
michael@0 | 1283 | @@ -100,20 +100,20 @@ template bool CFIFrameInfo::FindCallerRe |
michael@0 | 1284 | template bool CFIFrameInfo::FindCallerRegs<uint64_t>( |
michael@0 | 1285 | const RegisterValueMap<uint64_t> ®isters, |
michael@0 | 1286 | const MemoryRegion &memory, |
michael@0 | 1287 | RegisterValueMap<uint64_t> *caller_registers) const; |
michael@0 | 1288 | |
michael@0 | 1289 | string CFIFrameInfo::Serialize() const { |
michael@0 | 1290 | std::ostringstream stream; |
michael@0 | 1291 | |
michael@0 | 1292 | - if (!cfa_rule_.invalid()) { |
michael@0 | 1293 | + if (!cfa_rule_.isExprInvalid()) { |
michael@0 | 1294 | stream << ".cfa: " << cfa_rule_; |
michael@0 | 1295 | } |
michael@0 | 1296 | - if (!ra_rule_.invalid()) { |
michael@0 | 1297 | + if (!ra_rule_.isExprInvalid()) { |
michael@0 | 1298 | if (static_cast<std::streamoff>(stream.tellp()) != 0) |
michael@0 | 1299 | stream << " "; |
michael@0 | 1300 | stream << ".ra: " << ra_rule_; |
michael@0 | 1301 | } |
michael@0 | 1302 | |
michael@0 | 1303 | // Visit the register rules in alphabetical order. Because |
michael@0 | 1304 | // register_rules_ has the elements in some arbitrary order, |
michael@0 | 1305 | // get the names out into a vector, sort them, and visit in |