Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- DWARFCallFrameInfo.cpp ----------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | |
| 11 | // C Includes |
| 12 | // C++ Includes |
| 13 | #include <list> |
| 14 | |
Jason Molenda | e6194f1 | 2010-10-26 12:01:35 +0000 | [diff] [blame] | 15 | #include "lldb/Core/Log.h" |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 16 | #include "lldb/Core/Section.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 17 | #include "lldb/Core/ArchSpec.h" |
| 18 | #include "lldb/Core/Module.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 19 | #include "lldb/Core/Section.h" |
Jason Molenda | 7430382 | 2013-03-20 21:57:42 +0000 | [diff] [blame] | 20 | #include "lldb/Core/Timer.h" |
Greg Clayton | e38a5ed | 2012-01-05 03:57:59 +0000 | [diff] [blame] | 21 | #include "lldb/Host/Host.h" |
| 22 | #include "lldb/Symbol/DWARFCallFrameInfo.h" |
| 23 | #include "lldb/Symbol/ObjectFile.h" |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 24 | #include "lldb/Symbol/UnwindPlan.h" |
Greg Clayton | e38a5ed | 2012-01-05 03:57:59 +0000 | [diff] [blame] | 25 | #include "lldb/Target/RegisterContext.h" |
| 26 | #include "lldb/Target/Thread.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace lldb; |
| 29 | using namespace lldb_private; |
| 30 | |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 31 | DWARFCallFrameInfo::DWARFCallFrameInfo(ObjectFile& objfile, SectionSP& section_sp, lldb::RegisterKind reg_kind, bool is_eh_frame) : |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 32 | m_objfile (objfile), |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 33 | m_section_sp (section_sp), |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 34 | m_reg_kind (reg_kind), // The flavor of registers that the CFI data uses (enum RegisterKind) |
Stephen Wilson | 71c21d1 | 2011-04-11 19:41:40 +0000 | [diff] [blame] | 35 | m_flags (), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 36 | m_cie_map (), |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 37 | m_cfi_data (), |
| 38 | m_cfi_data_initialized (false), |
| 39 | m_fde_index (), |
| 40 | m_fde_index_initialized (false), |
Stephen Wilson | 71c21d1 | 2011-04-11 19:41:40 +0000 | [diff] [blame] | 41 | m_is_eh_frame (is_eh_frame) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 42 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | DWARFCallFrameInfo::~DWARFCallFrameInfo() |
| 46 | { |
| 47 | } |
| 48 | |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 49 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 50 | bool |
Jason Molenda | 7430382 | 2013-03-20 21:57:42 +0000 | [diff] [blame] | 51 | DWARFCallFrameInfo::GetUnwindPlan (Address addr, UnwindPlan& unwind_plan) |
| 52 | { |
| 53 | FDEEntryMap::Entry fde_entry; |
| 54 | |
| 55 | // Make sure that the Address we're searching for is the same object file |
| 56 | // as this DWARFCallFrameInfo, we only store File offsets in m_fde_index. |
| 57 | ModuleSP module_sp = addr.GetModule(); |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame] | 58 | if (module_sp.get() == nullptr || module_sp->GetObjectFile() == nullptr || module_sp->GetObjectFile() != &m_objfile) |
Jason Molenda | 7430382 | 2013-03-20 21:57:42 +0000 | [diff] [blame] | 59 | return false; |
| 60 | |
| 61 | if (GetFDEEntryByFileAddress (addr.GetFileAddress(), fde_entry) == false) |
| 62 | return false; |
| 63 | return FDEToUnwindPlan (fde_entry.data, addr, unwind_plan); |
| 64 | } |
| 65 | |
| 66 | bool |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 67 | DWARFCallFrameInfo::GetAddressRange (Address addr, AddressRange &range) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 68 | { |
Jason Molenda | 7430382 | 2013-03-20 21:57:42 +0000 | [diff] [blame] | 69 | |
| 70 | // Make sure that the Address we're searching for is the same object file |
| 71 | // as this DWARFCallFrameInfo, we only store File offsets in m_fde_index. |
| 72 | ModuleSP module_sp = addr.GetModule(); |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame] | 73 | if (module_sp.get() == nullptr || module_sp->GetObjectFile() == nullptr || module_sp->GetObjectFile() != &m_objfile) |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 74 | return false; |
Jason Molenda | 7430382 | 2013-03-20 21:57:42 +0000 | [diff] [blame] | 75 | |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame] | 76 | if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted()) |
Jason Molenda | 7430382 | 2013-03-20 21:57:42 +0000 | [diff] [blame] | 77 | return false; |
| 78 | GetFDEIndex(); |
| 79 | FDEEntryMap::Entry *fde_entry = m_fde_index.FindEntryThatContains (addr.GetFileAddress()); |
| 80 | if (!fde_entry) |
| 81 | return false; |
| 82 | |
| 83 | range = AddressRange(fde_entry->base, fde_entry->size, m_objfile.GetSectionList()); |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 84 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 87 | bool |
Jason Molenda | 7430382 | 2013-03-20 21:57:42 +0000 | [diff] [blame] | 88 | DWARFCallFrameInfo::GetFDEEntryByFileAddress (addr_t file_addr, FDEEntryMap::Entry &fde_entry) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 89 | { |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame] | 90 | if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted()) |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 91 | return false; |
Jason Molenda | 7430382 | 2013-03-20 21:57:42 +0000 | [diff] [blame] | 92 | |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 93 | GetFDEIndex(); |
| 94 | |
Jason Molenda | 7430382 | 2013-03-20 21:57:42 +0000 | [diff] [blame] | 95 | if (m_fde_index.IsEmpty()) |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 96 | return false; |
| 97 | |
Jason Molenda | 7430382 | 2013-03-20 21:57:42 +0000 | [diff] [blame] | 98 | FDEEntryMap::Entry *fde = m_fde_index.FindEntryThatContains (file_addr); |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 99 | |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame] | 100 | if (fde == nullptr) |
Jason Molenda | 7430382 | 2013-03-20 21:57:42 +0000 | [diff] [blame] | 101 | return false; |
| 102 | |
| 103 | fde_entry = *fde; |
| 104 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Jason Molenda | 5635f77 | 2013-03-21 03:36:01 +0000 | [diff] [blame] | 107 | void |
| 108 | DWARFCallFrameInfo::GetFunctionAddressAndSizeVector (FunctionAddressAndSizeVector &function_info) |
| 109 | { |
| 110 | GetFDEIndex(); |
| 111 | const size_t count = m_fde_index.GetSize(); |
Jason Molenda | ee7593f | 2013-03-22 22:43:14 +0000 | [diff] [blame] | 112 | function_info.Clear(); |
Jason Molenda | c1946cd | 2013-03-22 23:42:09 +0000 | [diff] [blame] | 113 | if (count > 0) |
| 114 | function_info.Reserve(count); |
Jason Molenda | 5635f77 | 2013-03-21 03:36:01 +0000 | [diff] [blame] | 115 | for (size_t i = 0; i < count; ++i) |
| 116 | { |
| 117 | const FDEEntryMap::Entry *func_offset_data_entry = m_fde_index.GetEntryAtIndex (i); |
| 118 | if (func_offset_data_entry) |
| 119 | { |
| 120 | FunctionAddressAndSizeVector::Entry function_offset_entry (func_offset_data_entry->base, func_offset_data_entry->size); |
| 121 | function_info.Append (function_offset_entry); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 126 | const DWARFCallFrameInfo::CIE* |
| 127 | DWARFCallFrameInfo::GetCIE(dw_offset_t cie_offset) |
| 128 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 129 | cie_map_t::iterator pos = m_cie_map.find(cie_offset); |
| 130 | |
| 131 | if (pos != m_cie_map.end()) |
| 132 | { |
| 133 | // Parse and cache the CIE |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame] | 134 | if (pos->second.get() == nullptr) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 135 | pos->second = ParseCIE (cie_offset); |
| 136 | |
| 137 | return pos->second.get(); |
| 138 | } |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame] | 139 | return nullptr; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 142 | DWARFCallFrameInfo::CIESP |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 143 | DWARFCallFrameInfo::ParseCIE (const dw_offset_t cie_offset) |
| 144 | { |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 145 | CIESP cie_sp(new CIE(cie_offset)); |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 146 | lldb::offset_t offset = cie_offset; |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 147 | if (m_cfi_data_initialized == false) |
Greg Clayton | f97c521 | 2011-12-29 00:05:26 +0000 | [diff] [blame] | 148 | GetCFIData(); |
Todd Fiala | 1767e11 | 2014-08-25 21:39:30 +0000 | [diff] [blame] | 149 | uint32_t length = m_cfi_data.GetU32(&offset); |
| 150 | dw_offset_t cie_id, end_offset; |
| 151 | bool is_64bit = (length == UINT32_MAX); |
| 152 | if (is_64bit) { |
| 153 | length = m_cfi_data.GetU64(&offset); |
| 154 | cie_id = m_cfi_data.GetU64(&offset); |
| 155 | end_offset = cie_offset + length + 12; |
| 156 | } else { |
| 157 | cie_id = m_cfi_data.GetU32(&offset); |
| 158 | end_offset = cie_offset + length + 4; |
| 159 | } |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 160 | if (length > 0 && ((!m_is_eh_frame && cie_id == UINT32_MAX) || (m_is_eh_frame && cie_id == 0ul))) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 161 | { |
| 162 | size_t i; |
| 163 | // cie.offset = cie_offset; |
| 164 | // cie.length = length; |
| 165 | // cie.cieID = cieID; |
Ashok Thirumurthi | 6e264d3 | 2013-07-29 16:05:11 +0000 | [diff] [blame] | 166 | cie_sp->ptr_encoding = DW_EH_PE_absptr; // default |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 167 | cie_sp->version = m_cfi_data.GetU8(&offset); |
| 168 | |
| 169 | for (i=0; i<CFI_AUG_MAX_SIZE; ++i) |
| 170 | { |
| 171 | cie_sp->augmentation[i] = m_cfi_data.GetU8(&offset); |
| 172 | if (cie_sp->augmentation[i] == '\0') |
| 173 | { |
| 174 | // Zero out remaining bytes in augmentation string |
| 175 | for (size_t j = i+1; j<CFI_AUG_MAX_SIZE; ++j) |
| 176 | cie_sp->augmentation[j] = '\0'; |
| 177 | |
| 178 | break; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | if (i == CFI_AUG_MAX_SIZE && cie_sp->augmentation[CFI_AUG_MAX_SIZE-1] != '\0') |
| 183 | { |
Greg Clayton | e38a5ed | 2012-01-05 03:57:59 +0000 | [diff] [blame] | 184 | Host::SystemLog (Host::eSystemLogError, "CIE parse error: CIE augmentation string was too large for the fixed sized buffer of %d bytes.\n", CFI_AUG_MAX_SIZE); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 185 | return cie_sp; |
| 186 | } |
| 187 | cie_sp->code_align = (uint32_t)m_cfi_data.GetULEB128(&offset); |
| 188 | cie_sp->data_align = (int32_t)m_cfi_data.GetSLEB128(&offset); |
| 189 | cie_sp->return_addr_reg_num = m_cfi_data.GetU8(&offset); |
| 190 | |
| 191 | if (cie_sp->augmentation[0]) |
| 192 | { |
| 193 | // Get the length of the eh_frame augmentation data |
| 194 | // which starts with a ULEB128 length in bytes |
| 195 | const size_t aug_data_len = (size_t)m_cfi_data.GetULEB128(&offset); |
| 196 | const size_t aug_data_end = offset + aug_data_len; |
| 197 | const size_t aug_str_len = strlen(cie_sp->augmentation); |
| 198 | // A 'z' may be present as the first character of the string. |
| 199 | // If present, the Augmentation Data field shall be present. |
| 200 | // The contents of the Augmentation Data shall be intepreted |
| 201 | // according to other characters in the Augmentation String. |
| 202 | if (cie_sp->augmentation[0] == 'z') |
| 203 | { |
| 204 | // Extract the Augmentation Data |
| 205 | size_t aug_str_idx = 0; |
| 206 | for (aug_str_idx = 1; aug_str_idx < aug_str_len; aug_str_idx++) |
| 207 | { |
| 208 | char aug = cie_sp->augmentation[aug_str_idx]; |
| 209 | switch (aug) |
| 210 | { |
| 211 | case 'L': |
| 212 | // Indicates the presence of one argument in the |
| 213 | // Augmentation Data of the CIE, and a corresponding |
| 214 | // argument in the Augmentation Data of the FDE. The |
| 215 | // argument in the Augmentation Data of the CIE is |
| 216 | // 1-byte and represents the pointer encoding used |
| 217 | // for the argument in the Augmentation Data of the |
| 218 | // FDE, which is the address of a language-specific |
| 219 | // data area (LSDA). The size of the LSDA pointer is |
| 220 | // specified by the pointer encoding used. |
| 221 | m_cfi_data.GetU8(&offset); |
| 222 | break; |
| 223 | |
| 224 | case 'P': |
| 225 | // Indicates the presence of two arguments in the |
| 226 | // Augmentation Data of the cie_sp-> The first argument |
| 227 | // is 1-byte and represents the pointer encoding |
| 228 | // used for the second argument, which is the |
| 229 | // address of a personality routine handler. The |
| 230 | // size of the personality routine pointer is |
| 231 | // specified by the pointer encoding used. |
| 232 | { |
| 233 | uint8_t arg_ptr_encoding = m_cfi_data.GetU8(&offset); |
| 234 | m_cfi_data.GetGNUEHPointer(&offset, arg_ptr_encoding, LLDB_INVALID_ADDRESS, LLDB_INVALID_ADDRESS, LLDB_INVALID_ADDRESS); |
| 235 | } |
| 236 | break; |
| 237 | |
| 238 | case 'R': |
| 239 | // A 'R' may be present at any position after the |
| 240 | // first character of the string. The Augmentation |
| 241 | // Data shall include a 1 byte argument that |
| 242 | // represents the pointer encoding for the address |
| 243 | // pointers used in the FDE. |
Ashok Thirumurthi | 6e264d3 | 2013-07-29 16:05:11 +0000 | [diff] [blame] | 244 | // Example: 0x1B == DW_EH_PE_pcrel | DW_EH_PE_sdata4 |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 245 | cie_sp->ptr_encoding = m_cfi_data.GetU8(&offset); |
| 246 | break; |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | else if (strcmp(cie_sp->augmentation, "eh") == 0) |
| 251 | { |
| 252 | // If the Augmentation string has the value "eh", then |
| 253 | // the EH Data field shall be present |
| 254 | } |
| 255 | |
| 256 | // Set the offset to be the end of the augmentation data just in case |
| 257 | // we didn't understand any of the data. |
| 258 | offset = (uint32_t)aug_data_end; |
| 259 | } |
| 260 | |
| 261 | if (end_offset > offset) |
| 262 | { |
| 263 | cie_sp->inst_offset = offset; |
| 264 | cie_sp->inst_length = end_offset - offset; |
| 265 | } |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 266 | while (offset < end_offset) |
| 267 | { |
| 268 | uint8_t inst = m_cfi_data.GetU8(&offset); |
| 269 | uint8_t primary_opcode = inst & 0xC0; |
| 270 | uint8_t extended_opcode = inst & 0x3F; |
| 271 | |
| 272 | if (extended_opcode == DW_CFA_def_cfa) |
| 273 | { |
| 274 | // Takes two unsigned LEB128 operands representing a register |
| 275 | // number and a (non-factored) offset. The required action |
| 276 | // is to define the current CFA rule to use the provided |
| 277 | // register and offset. |
| 278 | uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); |
| 279 | int op_offset = (int32_t)m_cfi_data.GetULEB128(&offset); |
| 280 | cie_sp->initial_row.SetCFARegister (reg_num); |
| 281 | cie_sp->initial_row.SetCFAOffset (op_offset); |
| 282 | continue; |
| 283 | } |
| 284 | if (primary_opcode == DW_CFA_offset) |
| 285 | { |
| 286 | // 0x80 - high 2 bits are 0x2, lower 6 bits are register. |
| 287 | // Takes two arguments: an unsigned LEB128 constant representing a |
| 288 | // factored offset and a register number. The required action is to |
| 289 | // change the rule for the register indicated by the register number |
| 290 | // to be an offset(N) rule with a value of |
| 291 | // (N = factored offset * data_align). |
| 292 | uint32_t reg_num = extended_opcode; |
| 293 | int op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * cie_sp->data_align; |
| 294 | UnwindPlan::Row::RegisterLocation reg_location; |
| 295 | reg_location.SetAtCFAPlusOffset(op_offset); |
| 296 | cie_sp->initial_row.SetRegisterInfo (reg_num, reg_location); |
| 297 | continue; |
| 298 | } |
| 299 | if (extended_opcode == DW_CFA_nop) |
| 300 | { |
| 301 | continue; |
| 302 | } |
| 303 | break; // Stop if we hit an unrecognized opcode |
| 304 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | return cie_sp; |
| 308 | } |
| 309 | |
Greg Clayton | f97c521 | 2011-12-29 00:05:26 +0000 | [diff] [blame] | 310 | void |
| 311 | DWARFCallFrameInfo::GetCFIData() |
| 312 | { |
| 313 | if (m_cfi_data_initialized == false) |
| 314 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 315 | Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND)); |
Greg Clayton | f97c521 | 2011-12-29 00:05:26 +0000 | [diff] [blame] | 316 | if (log) |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 317 | m_objfile.GetModule()->LogMessage(log, "Reading EH frame info"); |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 318 | m_objfile.ReadSectionData (m_section_sp.get(), m_cfi_data); |
Greg Clayton | f97c521 | 2011-12-29 00:05:26 +0000 | [diff] [blame] | 319 | m_cfi_data_initialized = true; |
| 320 | } |
| 321 | } |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 322 | // Scan through the eh_frame or debug_frame section looking for FDEs and noting the start/end addresses |
| 323 | // of the functions and a pointer back to the function's FDE for later expansion. |
| 324 | // Internalize CIEs as we come across them. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 325 | |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 326 | void |
| 327 | DWARFCallFrameInfo::GetFDEIndex () |
| 328 | { |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame] | 329 | if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted()) |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 330 | return; |
Sean Callanan | 7d69f12 | 2012-07-12 01:11:40 +0000 | [diff] [blame] | 331 | |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 332 | if (m_fde_index_initialized) |
| 333 | return; |
Sean Callanan | 7d69f12 | 2012-07-12 01:11:40 +0000 | [diff] [blame] | 334 | |
| 335 | Mutex::Locker locker(m_fde_index_mutex); |
| 336 | |
| 337 | if (m_fde_index_initialized) // if two threads hit the locker |
| 338 | return; |
Jason Molenda | e6194f1 | 2010-10-26 12:01:35 +0000 | [diff] [blame] | 339 | |
Jason Molenda | 7430382 | 2013-03-20 21:57:42 +0000 | [diff] [blame] | 340 | Timer scoped_timer (__PRETTY_FUNCTION__, "%s - %s", __PRETTY_FUNCTION__, m_objfile.GetFileSpec().GetFilename().AsCString("")); |
| 341 | |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 342 | lldb::offset_t offset = 0; |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 343 | if (m_cfi_data_initialized == false) |
Greg Clayton | f97c521 | 2011-12-29 00:05:26 +0000 | [diff] [blame] | 344 | GetCFIData(); |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 345 | while (m_cfi_data.ValidOffsetForDataOfSize (offset, 8)) |
| 346 | { |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 347 | const dw_offset_t current_entry = offset; |
Todd Fiala | 1767e11 | 2014-08-25 21:39:30 +0000 | [diff] [blame] | 348 | dw_offset_t cie_id, next_entry, cie_offset; |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 349 | uint32_t len = m_cfi_data.GetU32 (&offset); |
Todd Fiala | 1767e11 | 2014-08-25 21:39:30 +0000 | [diff] [blame] | 350 | bool is_64bit = (len == UINT32_MAX); |
| 351 | if (is_64bit) { |
| 352 | len = m_cfi_data.GetU64 (&offset); |
| 353 | cie_id = m_cfi_data.GetU64 (&offset); |
| 354 | next_entry = current_entry + len + 12; |
| 355 | cie_offset = current_entry + 12 - cie_id; |
| 356 | } else { |
| 357 | cie_id = m_cfi_data.GetU32 (&offset); |
| 358 | next_entry = current_entry + len + 4; |
| 359 | cie_offset = current_entry + 4 - cie_id; |
| 360 | } |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 361 | |
Virgile Bello | 8452cb5 | 2013-08-25 13:24:48 +0000 | [diff] [blame] | 362 | if (cie_id == 0 || cie_id == UINT32_MAX || len == 0) |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 363 | { |
| 364 | m_cie_map[current_entry] = ParseCIE (current_entry); |
| 365 | offset = next_entry; |
| 366 | continue; |
| 367 | } |
| 368 | |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 369 | const CIE *cie = GetCIE (cie_offset); |
| 370 | if (cie) |
| 371 | { |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 372 | const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress(); |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 373 | const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS; |
| 374 | const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS; |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 375 | |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 376 | lldb::addr_t addr = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding, pc_rel_addr, text_addr, data_addr); |
| 377 | lldb::addr_t length = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding & DW_EH_PE_MASK_ENCODING, pc_rel_addr, text_addr, data_addr); |
Jason Molenda | 7430382 | 2013-03-20 21:57:42 +0000 | [diff] [blame] | 378 | FDEEntryMap::Entry fde (addr, length, current_entry); |
| 379 | m_fde_index.Append(fde); |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 380 | } |
| 381 | else |
| 382 | { |
Greg Clayton | e38a5ed | 2012-01-05 03:57:59 +0000 | [diff] [blame] | 383 | Host::SystemLog (Host::eSystemLogError, |
| 384 | "error: unable to find CIE at 0x%8.8x for cie_id = 0x%8.8x for entry at 0x%8.8x.\n", |
| 385 | cie_offset, |
| 386 | cie_id, |
| 387 | current_entry); |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 388 | } |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 389 | offset = next_entry; |
| 390 | } |
Jason Molenda | 7430382 | 2013-03-20 21:57:42 +0000 | [diff] [blame] | 391 | m_fde_index.Sort(); |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 392 | m_fde_index_initialized = true; |
| 393 | } |
| 394 | |
| 395 | bool |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 396 | DWARFCallFrameInfo::FDEToUnwindPlan (dw_offset_t dwarf_offset, Address startaddr, UnwindPlan& unwind_plan) |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 397 | { |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 398 | lldb::offset_t offset = dwarf_offset; |
| 399 | lldb::offset_t current_entry = offset; |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 400 | |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame] | 401 | if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted()) |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 402 | return false; |
| 403 | |
| 404 | if (m_cfi_data_initialized == false) |
Greg Clayton | f97c521 | 2011-12-29 00:05:26 +0000 | [diff] [blame] | 405 | GetCFIData(); |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 406 | |
| 407 | uint32_t length = m_cfi_data.GetU32 (&offset); |
Todd Fiala | 1767e11 | 2014-08-25 21:39:30 +0000 | [diff] [blame] | 408 | dw_offset_t cie_offset; |
| 409 | bool is_64bit = (length == UINT32_MAX); |
| 410 | if (is_64bit) { |
| 411 | length = m_cfi_data.GetU64 (&offset); |
| 412 | cie_offset = m_cfi_data.GetU64 (&offset); |
| 413 | } else { |
| 414 | cie_offset = m_cfi_data.GetU32 (&offset); |
| 415 | } |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 416 | |
| 417 | assert (cie_offset != 0 && cie_offset != UINT32_MAX); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 418 | |
| 419 | // Translate the CIE_id from the eh_frame format, which |
| 420 | // is relative to the FDE offset, into a __eh_frame section |
| 421 | // offset |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 422 | if (m_is_eh_frame) |
Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 423 | { |
| 424 | unwind_plan.SetSourceName ("eh_frame CFI"); |
Todd Fiala | 1767e11 | 2014-08-25 21:39:30 +0000 | [diff] [blame] | 425 | cie_offset = current_entry + (is_64bit ? 12 : 4) - cie_offset; |
Jason Molenda | 60f0bd4 | 2012-10-26 06:08:58 +0000 | [diff] [blame] | 426 | unwind_plan.SetUnwindPlanValidAtAllInstructions (eLazyBoolNo); |
Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 427 | } |
| 428 | else |
| 429 | { |
| 430 | unwind_plan.SetSourceName ("DWARF CFI"); |
Jason Molenda | 60f0bd4 | 2012-10-26 06:08:58 +0000 | [diff] [blame] | 431 | // In theory the debug_frame info should be valid at all call sites |
| 432 | // ("asynchronous unwind info" as it is sometimes called) but in practice |
| 433 | // gcc et al all emit call frame info for the prologue and call sites, but |
| 434 | // not for the epilogue or all the other locations during the function reliably. |
| 435 | unwind_plan.SetUnwindPlanValidAtAllInstructions (eLazyBoolNo); |
Jason Molenda | ab4f192 | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 436 | } |
Jason Molenda | 60f0bd4 | 2012-10-26 06:08:58 +0000 | [diff] [blame] | 437 | unwind_plan.SetSourcedFromCompiler (eLazyBoolYes); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 438 | |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 439 | const CIE *cie = GetCIE (cie_offset); |
Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame] | 440 | assert (cie != nullptr); |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 441 | |
Todd Fiala | 1767e11 | 2014-08-25 21:39:30 +0000 | [diff] [blame] | 442 | const dw_offset_t end_offset = current_entry + length + (is_64bit ? 12 : 4); |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 443 | |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 444 | const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress(); |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 445 | const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS; |
| 446 | const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS; |
| 447 | lldb::addr_t range_base = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding, pc_rel_addr, text_addr, data_addr); |
| 448 | lldb::addr_t range_len = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding & DW_EH_PE_MASK_ENCODING, pc_rel_addr, text_addr, data_addr); |
| 449 | AddressRange range (range_base, m_objfile.GetAddressByteSize(), m_objfile.GetSectionList()); |
| 450 | range.SetByteSize (range_len); |
| 451 | |
| 452 | if (cie->augmentation[0] == 'z') |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 453 | { |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 454 | uint32_t aug_data_len = (uint32_t)m_cfi_data.GetULEB128(&offset); |
| 455 | offset += aug_data_len; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 456 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 457 | |
| 458 | uint32_t reg_num = 0; |
| 459 | int32_t op_offset = 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 460 | uint32_t code_align = cie->code_align; |
| 461 | int32_t data_align = cie->data_align; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 462 | |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 463 | unwind_plan.SetPlanValidAddressRange (range); |
Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 464 | UnwindPlan::Row *cie_initial_row = new UnwindPlan::Row; |
| 465 | *cie_initial_row = cie->initial_row; |
| 466 | UnwindPlan::RowSP row(cie_initial_row); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 467 | |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 468 | unwind_plan.SetRegisterKind (m_reg_kind); |
Jason Molenda | 8eba46c | 2012-08-18 06:53:34 +0000 | [diff] [blame] | 469 | unwind_plan.SetReturnAddressRegister (cie->return_addr_reg_num); |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 470 | |
Richard Mitton | f70d604 | 2013-09-12 23:38:30 +0000 | [diff] [blame] | 471 | std::vector<UnwindPlan::RowSP> stack; |
| 472 | |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 473 | UnwindPlan::Row::RegisterLocation reg_location; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 474 | while (m_cfi_data.ValidOffset(offset) && offset < end_offset) |
| 475 | { |
| 476 | uint8_t inst = m_cfi_data.GetU8(&offset); |
| 477 | uint8_t primary_opcode = inst & 0xC0; |
| 478 | uint8_t extended_opcode = inst & 0x3F; |
| 479 | |
| 480 | if (primary_opcode) |
| 481 | { |
| 482 | switch (primary_opcode) |
| 483 | { |
| 484 | case DW_CFA_advance_loc : // (Row Creation Instruction) |
| 485 | { // 0x40 - high 2 bits are 0x1, lower 6 bits are delta |
| 486 | // takes a single argument that represents a constant delta. The |
| 487 | // required action is to create a new table row with a location |
| 488 | // value that is computed by taking the current entry's location |
| 489 | // value and adding (delta * code_align). All other |
| 490 | // values in the new row are initially identical to the current row. |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 491 | unwind_plan.AppendRow(row); |
Jason Molenda | fa67e87 | 2012-07-31 22:42:30 +0000 | [diff] [blame] | 492 | UnwindPlan::Row *newrow = new UnwindPlan::Row; |
| 493 | *newrow = *row.get(); |
| 494 | row.reset (newrow); |
Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 495 | row->SlideOffset(extended_opcode * code_align); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 496 | } |
| 497 | break; |
| 498 | |
| 499 | case DW_CFA_offset : |
| 500 | { // 0x80 - high 2 bits are 0x2, lower 6 bits are register |
| 501 | // takes two arguments: an unsigned LEB128 constant representing a |
| 502 | // factored offset and a register number. The required action is to |
| 503 | // change the rule for the register indicated by the register number |
| 504 | // to be an offset(N) rule with a value of |
| 505 | // (N = factored offset * data_align). |
| 506 | reg_num = extended_opcode; |
| 507 | op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align; |
| 508 | reg_location.SetAtCFAPlusOffset(op_offset); |
Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 509 | row->SetRegisterInfo (reg_num, reg_location); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 510 | } |
| 511 | break; |
| 512 | |
| 513 | case DW_CFA_restore : |
| 514 | { // 0xC0 - high 2 bits are 0x3, lower 6 bits are register |
| 515 | // takes a single argument that represents a register number. The |
| 516 | // required action is to change the rule for the indicated register |
| 517 | // to the rule assigned it by the initial_instructions in the CIE. |
| 518 | reg_num = extended_opcode; |
| 519 | // We only keep enough register locations around to |
| 520 | // unwind what is in our thread, and these are organized |
| 521 | // by the register index in that state, so we need to convert our |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 522 | // GCC register number from the EH frame info, to a register index |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 523 | |
Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 524 | if (unwind_plan.IsValidRowIndex(0) && unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num, reg_location)) |
| 525 | row->SetRegisterInfo (reg_num, reg_location); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 526 | } |
| 527 | break; |
| 528 | } |
| 529 | } |
| 530 | else |
| 531 | { |
| 532 | switch (extended_opcode) |
| 533 | { |
| 534 | case DW_CFA_nop : // 0x0 |
| 535 | break; |
| 536 | |
| 537 | case DW_CFA_set_loc : // 0x1 (Row Creation Instruction) |
| 538 | { |
| 539 | // DW_CFA_set_loc takes a single argument that represents an address. |
| 540 | // The required action is to create a new table row using the |
| 541 | // specified address as the location. All other values in the new row |
| 542 | // are initially identical to the current row. The new location value |
| 543 | // should always be greater than the current one. |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 544 | unwind_plan.AppendRow(row); |
Jason Molenda | fa67e87 | 2012-07-31 22:42:30 +0000 | [diff] [blame] | 545 | UnwindPlan::Row *newrow = new UnwindPlan::Row; |
| 546 | *newrow = *row.get(); |
| 547 | row.reset (newrow); |
Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 548 | row->SetOffset(m_cfi_data.GetPointer(&offset) - startaddr.GetFileAddress()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 549 | } |
| 550 | break; |
| 551 | |
| 552 | case DW_CFA_advance_loc1 : // 0x2 (Row Creation Instruction) |
| 553 | { |
| 554 | // takes a single uword argument that represents a constant delta. |
| 555 | // This instruction is identical to DW_CFA_advance_loc except for the |
| 556 | // encoding and size of the delta argument. |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 557 | unwind_plan.AppendRow(row); |
Jason Molenda | fa67e87 | 2012-07-31 22:42:30 +0000 | [diff] [blame] | 558 | UnwindPlan::Row *newrow = new UnwindPlan::Row; |
| 559 | *newrow = *row.get(); |
| 560 | row.reset (newrow); |
Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 561 | row->SlideOffset (m_cfi_data.GetU8(&offset) * code_align); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 562 | } |
| 563 | break; |
| 564 | |
| 565 | case DW_CFA_advance_loc2 : // 0x3 (Row Creation Instruction) |
| 566 | { |
| 567 | // takes a single uword argument that represents a constant delta. |
| 568 | // This instruction is identical to DW_CFA_advance_loc except for the |
| 569 | // encoding and size of the delta argument. |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 570 | unwind_plan.AppendRow(row); |
Jason Molenda | fa67e87 | 2012-07-31 22:42:30 +0000 | [diff] [blame] | 571 | UnwindPlan::Row *newrow = new UnwindPlan::Row; |
| 572 | *newrow = *row.get(); |
| 573 | row.reset (newrow); |
Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 574 | row->SlideOffset (m_cfi_data.GetU16(&offset) * code_align); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 575 | } |
| 576 | break; |
| 577 | |
| 578 | case DW_CFA_advance_loc4 : // 0x4 (Row Creation Instruction) |
| 579 | { |
| 580 | // takes a single uword argument that represents a constant delta. |
| 581 | // This instruction is identical to DW_CFA_advance_loc except for the |
| 582 | // encoding and size of the delta argument. |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 583 | unwind_plan.AppendRow(row); |
Jason Molenda | fa67e87 | 2012-07-31 22:42:30 +0000 | [diff] [blame] | 584 | UnwindPlan::Row *newrow = new UnwindPlan::Row; |
| 585 | *newrow = *row.get(); |
| 586 | row.reset (newrow); |
Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 587 | row->SlideOffset (m_cfi_data.GetU32(&offset) * code_align); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 588 | } |
| 589 | break; |
| 590 | |
| 591 | case DW_CFA_offset_extended : // 0x5 |
| 592 | { |
| 593 | // takes two unsigned LEB128 arguments representing a register number |
| 594 | // and a factored offset. This instruction is identical to DW_CFA_offset |
| 595 | // except for the encoding and size of the register argument. |
| 596 | reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); |
| 597 | op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align; |
| 598 | reg_location.SetAtCFAPlusOffset(op_offset); |
Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 599 | row->SetRegisterInfo (reg_num, reg_location); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 600 | } |
| 601 | break; |
| 602 | |
| 603 | case DW_CFA_restore_extended : // 0x6 |
| 604 | { |
| 605 | // takes a single unsigned LEB128 argument that represents a register |
| 606 | // number. This instruction is identical to DW_CFA_restore except for |
| 607 | // the encoding and size of the register argument. |
| 608 | reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); |
Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 609 | if (unwind_plan.IsValidRowIndex(0) && unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num, reg_location)) |
| 610 | row->SetRegisterInfo (reg_num, reg_location); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 611 | } |
| 612 | break; |
| 613 | |
| 614 | case DW_CFA_undefined : // 0x7 |
| 615 | { |
| 616 | // takes a single unsigned LEB128 argument that represents a register |
| 617 | // number. The required action is to set the rule for the specified |
| 618 | // register to undefined. |
| 619 | reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); |
| 620 | reg_location.SetUndefined(); |
Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 621 | row->SetRegisterInfo (reg_num, reg_location); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 622 | } |
| 623 | break; |
| 624 | |
| 625 | case DW_CFA_same_value : // 0x8 |
| 626 | { |
| 627 | // takes a single unsigned LEB128 argument that represents a register |
| 628 | // number. The required action is to set the rule for the specified |
| 629 | // register to same value. |
| 630 | reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); |
| 631 | reg_location.SetSame(); |
Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 632 | row->SetRegisterInfo (reg_num, reg_location); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 633 | } |
| 634 | break; |
| 635 | |
| 636 | case DW_CFA_register : // 0x9 |
| 637 | { |
| 638 | // takes two unsigned LEB128 arguments representing register numbers. |
| 639 | // The required action is to set the rule for the first register to be |
| 640 | // the second register. |
| 641 | |
| 642 | reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); |
| 643 | uint32_t other_reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); |
| 644 | reg_location.SetInRegister(other_reg_num); |
Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 645 | row->SetRegisterInfo (reg_num, reg_location); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 646 | } |
| 647 | break; |
| 648 | |
| 649 | case DW_CFA_remember_state : // 0xA |
Jason Molenda | fa67e87 | 2012-07-31 22:42:30 +0000 | [diff] [blame] | 650 | { |
| 651 | // These instructions define a stack of information. Encountering the |
| 652 | // DW_CFA_remember_state instruction means to save the rules for every |
| 653 | // register on the current row on the stack. Encountering the |
| 654 | // DW_CFA_restore_state instruction means to pop the set of rules off |
| 655 | // the stack and place them in the current row. (This operation is |
| 656 | // useful for compilers that move epilogue code into the body of a |
| 657 | // function.) |
Richard Mitton | f70d604 | 2013-09-12 23:38:30 +0000 | [diff] [blame] | 658 | stack.push_back (row); |
Jason Molenda | fa67e87 | 2012-07-31 22:42:30 +0000 | [diff] [blame] | 659 | UnwindPlan::Row *newrow = new UnwindPlan::Row; |
| 660 | *newrow = *row.get(); |
| 661 | row.reset (newrow); |
| 662 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 663 | break; |
| 664 | |
| 665 | case DW_CFA_restore_state : // 0xB |
| 666 | // These instructions define a stack of information. Encountering the |
| 667 | // DW_CFA_remember_state instruction means to save the rules for every |
| 668 | // register on the current row on the stack. Encountering the |
| 669 | // DW_CFA_restore_state instruction means to pop the set of rules off |
| 670 | // the stack and place them in the current row. (This operation is |
| 671 | // useful for compilers that move epilogue code into the body of a |
| 672 | // function.) |
| 673 | { |
Richard Mitton | f70d604 | 2013-09-12 23:38:30 +0000 | [diff] [blame] | 674 | row = stack.back (); |
| 675 | stack.pop_back (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 676 | } |
| 677 | break; |
| 678 | |
| 679 | case DW_CFA_def_cfa : // 0xC (CFA Definition Instruction) |
| 680 | { |
| 681 | // Takes two unsigned LEB128 operands representing a register |
| 682 | // number and a (non-factored) offset. The required action |
| 683 | // is to define the current CFA rule to use the provided |
| 684 | // register and offset. |
| 685 | reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); |
| 686 | op_offset = (int32_t)m_cfi_data.GetULEB128(&offset); |
Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 687 | row->SetCFARegister (reg_num); |
| 688 | row->SetCFAOffset (op_offset); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 689 | } |
| 690 | break; |
| 691 | |
| 692 | case DW_CFA_def_cfa_register : // 0xD (CFA Definition Instruction) |
| 693 | { |
| 694 | // takes a single unsigned LEB128 argument representing a register |
| 695 | // number. The required action is to define the current CFA rule to |
| 696 | // use the provided register (but to keep the old offset). |
| 697 | reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); |
Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 698 | row->SetCFARegister (reg_num); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 699 | } |
| 700 | break; |
| 701 | |
| 702 | case DW_CFA_def_cfa_offset : // 0xE (CFA Definition Instruction) |
| 703 | { |
| 704 | // Takes a single unsigned LEB128 operand representing a |
| 705 | // (non-factored) offset. The required action is to define |
| 706 | // the current CFA rule to use the provided offset (but |
| 707 | // to keep the old register). |
| 708 | op_offset = (int32_t)m_cfi_data.GetULEB128(&offset); |
Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 709 | row->SetCFAOffset (op_offset); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 710 | } |
| 711 | break; |
| 712 | |
| 713 | case DW_CFA_def_cfa_expression : // 0xF (CFA Definition Instruction) |
| 714 | { |
| 715 | size_t block_len = (size_t)m_cfi_data.GetULEB128(&offset); |
| 716 | offset += (uint32_t)block_len; |
| 717 | } |
| 718 | break; |
| 719 | |
| 720 | case DW_CFA_expression : // 0x10 |
| 721 | { |
| 722 | // Takes two operands: an unsigned LEB128 value representing |
| 723 | // a register number, and a DW_FORM_block value representing a DWARF |
| 724 | // expression. The required action is to change the rule for the |
| 725 | // register indicated by the register number to be an expression(E) |
| 726 | // rule where E is the DWARF expression. That is, the DWARF |
| 727 | // expression computes the address. The value of the CFA is |
| 728 | // pushed on the DWARF evaluation stack prior to execution of |
| 729 | // the DWARF expression. |
| 730 | reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); |
| 731 | uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset); |
| 732 | const uint8_t *block_data = (uint8_t *)m_cfi_data.GetData(&offset, block_len); |
| 733 | |
| 734 | reg_location.SetAtDWARFExpression(block_data, block_len); |
Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 735 | row->SetRegisterInfo (reg_num, reg_location); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 736 | } |
| 737 | break; |
| 738 | |
| 739 | case DW_CFA_offset_extended_sf : // 0x11 |
| 740 | { |
| 741 | // takes two operands: an unsigned LEB128 value representing a |
| 742 | // register number and a signed LEB128 factored offset. This |
| 743 | // instruction is identical to DW_CFA_offset_extended except |
| 744 | //that the second operand is signed and factored. |
| 745 | reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); |
| 746 | op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align; |
| 747 | reg_location.SetAtCFAPlusOffset(op_offset); |
Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 748 | row->SetRegisterInfo (reg_num, reg_location); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 749 | } |
| 750 | break; |
| 751 | |
| 752 | case DW_CFA_def_cfa_sf : // 0x12 (CFA Definition Instruction) |
| 753 | { |
| 754 | // Takes two operands: an unsigned LEB128 value representing |
| 755 | // a register number and a signed LEB128 factored offset. |
| 756 | // This instruction is identical to DW_CFA_def_cfa except |
| 757 | // that the second operand is signed and factored. |
| 758 | reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); |
| 759 | op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align; |
Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 760 | row->SetCFARegister (reg_num); |
| 761 | row->SetCFAOffset (op_offset); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 762 | } |
| 763 | break; |
| 764 | |
| 765 | case DW_CFA_def_cfa_offset_sf : // 0x13 (CFA Definition Instruction) |
| 766 | { |
| 767 | // takes a signed LEB128 operand representing a factored |
| 768 | // offset. This instruction is identical to DW_CFA_def_cfa_offset |
| 769 | // except that the operand is signed and factored. |
| 770 | op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align; |
Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 771 | row->SetCFAOffset (op_offset); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 772 | } |
| 773 | break; |
| 774 | |
| 775 | case DW_CFA_val_expression : // 0x16 |
| 776 | { |
| 777 | // takes two operands: an unsigned LEB128 value representing a register |
| 778 | // number, and a DW_FORM_block value representing a DWARF expression. |
| 779 | // The required action is to change the rule for the register indicated |
| 780 | // by the register number to be a val_expression(E) rule where E is the |
| 781 | // DWARF expression. That is, the DWARF expression computes the value of |
| 782 | // the given register. The value of the CFA is pushed on the DWARF |
| 783 | // evaluation stack prior to execution of the DWARF expression. |
| 784 | reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset); |
| 785 | uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset); |
| 786 | const uint8_t* block_data = (uint8_t*)m_cfi_data.GetData(&offset, block_len); |
| 787 | //#if defined(__i386__) || defined(__x86_64__) |
| 788 | // // The EH frame info for EIP and RIP contains code that looks for traps to |
| 789 | // // be a specific type and increments the PC. |
| 790 | // // For i386: |
| 791 | // // DW_CFA_val_expression where: |
| 792 | // // eip = DW_OP_breg6(+28), DW_OP_deref, DW_OP_dup, DW_OP_plus_uconst(0x34), |
| 793 | // // DW_OP_deref, DW_OP_swap, DW_OP_plus_uconst(0), DW_OP_deref, |
| 794 | // // DW_OP_dup, DW_OP_lit3, DW_OP_ne, DW_OP_swap, DW_OP_lit4, DW_OP_ne, |
| 795 | // // DW_OP_and, DW_OP_plus |
| 796 | // // This basically does a: |
| 797 | // // eip = ucontenxt.mcontext32->gpr.eip; |
| 798 | // // if (ucontenxt.mcontext32->exc.trapno != 3 && ucontenxt.mcontext32->exc.trapno != 4) |
| 799 | // // eip++; |
| 800 | // // |
| 801 | // // For x86_64: |
| 802 | // // DW_CFA_val_expression where: |
| 803 | // // rip = DW_OP_breg3(+48), DW_OP_deref, DW_OP_dup, DW_OP_plus_uconst(0x90), DW_OP_deref, |
| 804 | // // DW_OP_swap, DW_OP_plus_uconst(0), DW_OP_deref_size(4), DW_OP_dup, DW_OP_lit3, |
| 805 | // // DW_OP_ne, DW_OP_swap, DW_OP_lit4, DW_OP_ne, DW_OP_and, DW_OP_plus |
| 806 | // // This basically does a: |
| 807 | // // rip = ucontenxt.mcontext64->gpr.rip; |
| 808 | // // if (ucontenxt.mcontext64->exc.trapno != 3 && ucontenxt.mcontext64->exc.trapno != 4) |
| 809 | // // rip++; |
| 810 | // // The trap comparisons and increments are not needed as it hoses up the unwound PC which |
| 811 | // // is expected to point at least past the instruction that causes the fault/trap. So we |
| 812 | // // take it out by trimming the expression right at the first "DW_OP_swap" opcodes |
| 813 | // if (block_data != NULL && thread->GetPCRegNum(Thread::GCC) == reg_num) |
| 814 | // { |
| 815 | // if (thread->Is64Bit()) |
| 816 | // { |
| 817 | // if (block_len > 9 && block_data[8] == DW_OP_swap && block_data[9] == DW_OP_plus_uconst) |
| 818 | // block_len = 8; |
| 819 | // } |
| 820 | // else |
| 821 | // { |
| 822 | // if (block_len > 8 && block_data[7] == DW_OP_swap && block_data[8] == DW_OP_plus_uconst) |
| 823 | // block_len = 7; |
| 824 | // } |
| 825 | // } |
| 826 | //#endif |
| 827 | reg_location.SetIsDWARFExpression(block_data, block_len); |
Jason Molenda | 1d42c7b | 2012-07-14 04:52:53 +0000 | [diff] [blame] | 828 | row->SetRegisterInfo (reg_num, reg_location); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 829 | } |
| 830 | break; |
| 831 | |
| 832 | case DW_CFA_val_offset : // 0x14 |
| 833 | case DW_CFA_val_offset_sf : // 0x15 |
| 834 | default: |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 835 | break; |
| 836 | } |
| 837 | } |
| 838 | } |
Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 839 | unwind_plan.AppendRow(row); |
| 840 | |
| 841 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 842 | } |