blob: 671fe0020eef28d59fd4ddd3da0ea2b600369873 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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 Molendae6194f12010-10-26 12:01:35 +000015#include "lldb/Core/Log.h"
Jason Molendafbcb7f22010-09-10 07:49:16 +000016#include "lldb/Core/Section.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Core/ArchSpec.h"
18#include "lldb/Core/Module.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Core/Section.h"
Jason Molenda74303822013-03-20 21:57:42 +000020#include "lldb/Core/Timer.h"
Greg Claytone38a5ed2012-01-05 03:57:59 +000021#include "lldb/Host/Host.h"
22#include "lldb/Symbol/DWARFCallFrameInfo.h"
23#include "lldb/Symbol/ObjectFile.h"
Jason Molendafbcb7f22010-09-10 07:49:16 +000024#include "lldb/Symbol/UnwindPlan.h"
Greg Claytone38a5ed2012-01-05 03:57:59 +000025#include "lldb/Target/RegisterContext.h"
26#include "lldb/Target/Thread.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027
28using namespace lldb;
29using namespace lldb_private;
30
Greg Claytonc9660542012-02-05 02:38:54 +000031DWARFCallFrameInfo::DWARFCallFrameInfo(ObjectFile& objfile, SectionSP& section_sp, lldb::RegisterKind reg_kind, bool is_eh_frame) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032 m_objfile (objfile),
Greg Claytonc9660542012-02-05 02:38:54 +000033 m_section_sp (section_sp),
Jason Molendafbcb7f22010-09-10 07:49:16 +000034 m_reg_kind (reg_kind), // The flavor of registers that the CFI data uses (enum RegisterKind)
Stephen Wilson71c21d12011-04-11 19:41:40 +000035 m_flags (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036 m_cie_map (),
Jason Molendafbcb7f22010-09-10 07:49:16 +000037 m_cfi_data (),
38 m_cfi_data_initialized (false),
39 m_fde_index (),
40 m_fde_index_initialized (false),
Stephen Wilson71c21d12011-04-11 19:41:40 +000041 m_is_eh_frame (is_eh_frame)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042{
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043}
44
45DWARFCallFrameInfo::~DWARFCallFrameInfo()
46{
47}
48
Jason Molendafbcb7f22010-09-10 07:49:16 +000049
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050bool
Jason Molenda74303822013-03-20 21:57:42 +000051DWARFCallFrameInfo::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 Masted4612ad2014-04-20 13:17:36 +000058 if (module_sp.get() == nullptr || module_sp->GetObjectFile() == nullptr || module_sp->GetObjectFile() != &m_objfile)
Jason Molenda74303822013-03-20 21:57:42 +000059 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
66bool
Jason Molendafbcb7f22010-09-10 07:49:16 +000067DWARFCallFrameInfo::GetAddressRange (Address addr, AddressRange &range)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000068{
Jason Molenda74303822013-03-20 21:57:42 +000069
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 Masted4612ad2014-04-20 13:17:36 +000073 if (module_sp.get() == nullptr || module_sp->GetObjectFile() == nullptr || module_sp->GetObjectFile() != &m_objfile)
Jason Molendafbcb7f22010-09-10 07:49:16 +000074 return false;
Jason Molenda74303822013-03-20 21:57:42 +000075
Ed Masted4612ad2014-04-20 13:17:36 +000076 if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
Jason Molenda74303822013-03-20 21:57:42 +000077 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 Molendafbcb7f22010-09-10 07:49:16 +000084 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000085}
86
Jason Molendafbcb7f22010-09-10 07:49:16 +000087bool
Jason Molenda74303822013-03-20 21:57:42 +000088DWARFCallFrameInfo::GetFDEEntryByFileAddress (addr_t file_addr, FDEEntryMap::Entry &fde_entry)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000089{
Ed Masted4612ad2014-04-20 13:17:36 +000090 if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
Jason Molendafbcb7f22010-09-10 07:49:16 +000091 return false;
Jason Molenda74303822013-03-20 21:57:42 +000092
Jason Molendafbcb7f22010-09-10 07:49:16 +000093 GetFDEIndex();
94
Jason Molenda74303822013-03-20 21:57:42 +000095 if (m_fde_index.IsEmpty())
Jason Molendafbcb7f22010-09-10 07:49:16 +000096 return false;
97
Jason Molenda74303822013-03-20 21:57:42 +000098 FDEEntryMap::Entry *fde = m_fde_index.FindEntryThatContains (file_addr);
Jason Molendafbcb7f22010-09-10 07:49:16 +000099
Ed Masted4612ad2014-04-20 13:17:36 +0000100 if (fde == nullptr)
Jason Molenda74303822013-03-20 21:57:42 +0000101 return false;
102
103 fde_entry = *fde;
104 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000105}
106
Jason Molenda5635f772013-03-21 03:36:01 +0000107void
108DWARFCallFrameInfo::GetFunctionAddressAndSizeVector (FunctionAddressAndSizeVector &function_info)
109{
110 GetFDEIndex();
111 const size_t count = m_fde_index.GetSize();
Jason Molendaee7593f2013-03-22 22:43:14 +0000112 function_info.Clear();
Jason Molendac1946cd2013-03-22 23:42:09 +0000113 if (count > 0)
114 function_info.Reserve(count);
Jason Molenda5635f772013-03-21 03:36:01 +0000115 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 Lattner30fdc8d2010-06-08 16:52:24 +0000126const DWARFCallFrameInfo::CIE*
127DWARFCallFrameInfo::GetCIE(dw_offset_t cie_offset)
128{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000129 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 Masted4612ad2014-04-20 13:17:36 +0000134 if (pos->second.get() == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000135 pos->second = ParseCIE (cie_offset);
136
137 return pos->second.get();
138 }
Ed Masted4612ad2014-04-20 13:17:36 +0000139 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000140}
141
Jason Molendafbcb7f22010-09-10 07:49:16 +0000142DWARFCallFrameInfo::CIESP
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000143DWARFCallFrameInfo::ParseCIE (const dw_offset_t cie_offset)
144{
Jason Molendafbcb7f22010-09-10 07:49:16 +0000145 CIESP cie_sp(new CIE(cie_offset));
Greg Claytonc7bece562013-01-25 18:06:21 +0000146 lldb::offset_t offset = cie_offset;
Jason Molendafbcb7f22010-09-10 07:49:16 +0000147 if (m_cfi_data_initialized == false)
Greg Claytonf97c5212011-12-29 00:05:26 +0000148 GetCFIData();
Todd Fiala1767e112014-08-25 21:39:30 +0000149 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 Claytonc7bece562013-01-25 18:06:21 +0000160 if (length > 0 && ((!m_is_eh_frame && cie_id == UINT32_MAX) || (m_is_eh_frame && cie_id == 0ul)))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000161 {
162 size_t i;
163 // cie.offset = cie_offset;
164 // cie.length = length;
165 // cie.cieID = cieID;
Ashok Thirumurthi6e264d32013-07-29 16:05:11 +0000166 cie_sp->ptr_encoding = DW_EH_PE_absptr; // default
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000167 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 Claytone38a5ed2012-01-05 03:57:59 +0000184 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 Lattner30fdc8d2010-06-08 16:52:24 +0000185 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.
Bruce Mitchener58ef3912015-06-18 05:27:05 +0000200 // The contents of the Augmentation Data shall be interpreted
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000201 // 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.
Jason Molendae9c7ecf2014-11-18 02:27:42 +0000221 cie_sp->lsda_addr_encoding = m_cfi_data.GetU8(&offset);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000222 break;
223
224 case 'P':
225 // Indicates the presence of two arguments in the
Jason Molendae9c7ecf2014-11-18 02:27:42 +0000226 // Augmentation Data of the CIE. The first argument
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000227 // 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.
Jason Molendae9c7ecf2014-11-18 02:27:42 +0000232 //
233 // The address of the personality function will
234 // be stored at this location. Pre-execution, it
235 // will be all zero's so don't read it until we're
236 // trying to do an unwind & the reloc has been
237 // resolved.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000238 {
239 uint8_t arg_ptr_encoding = m_cfi_data.GetU8(&offset);
Jason Molendae9c7ecf2014-11-18 02:27:42 +0000240 const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
241 cie_sp->personality_loc = m_cfi_data.GetGNUEHPointer(&offset, arg_ptr_encoding, pc_rel_addr, LLDB_INVALID_ADDRESS, LLDB_INVALID_ADDRESS);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000242 }
243 break;
244
245 case 'R':
246 // A 'R' may be present at any position after the
247 // first character of the string. The Augmentation
248 // Data shall include a 1 byte argument that
249 // represents the pointer encoding for the address
250 // pointers used in the FDE.
Ashok Thirumurthi6e264d32013-07-29 16:05:11 +0000251 // Example: 0x1B == DW_EH_PE_pcrel | DW_EH_PE_sdata4
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000252 cie_sp->ptr_encoding = m_cfi_data.GetU8(&offset);
253 break;
254 }
255 }
256 }
257 else if (strcmp(cie_sp->augmentation, "eh") == 0)
258 {
259 // If the Augmentation string has the value "eh", then
260 // the EH Data field shall be present
261 }
262
263 // Set the offset to be the end of the augmentation data just in case
264 // we didn't understand any of the data.
265 offset = (uint32_t)aug_data_end;
266 }
267
268 if (end_offset > offset)
269 {
270 cie_sp->inst_offset = offset;
271 cie_sp->inst_length = end_offset - offset;
272 }
Jason Molendafbcb7f22010-09-10 07:49:16 +0000273 while (offset < end_offset)
274 {
275 uint8_t inst = m_cfi_data.GetU8(&offset);
276 uint8_t primary_opcode = inst & 0xC0;
277 uint8_t extended_opcode = inst & 0x3F;
278
Tamas Berghammer623652a2015-07-03 09:30:17 +0000279 if (!HandleCommonDwarfOpcode(primary_opcode, extended_opcode, cie_sp->data_align, offset, cie_sp->initial_row))
280 break; // Stop if we hit an unrecognized opcode
Jason Molendafbcb7f22010-09-10 07:49:16 +0000281 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000282 }
283
284 return cie_sp;
285}
286
Greg Claytonf97c5212011-12-29 00:05:26 +0000287void
288DWARFCallFrameInfo::GetCFIData()
289{
290 if (m_cfi_data_initialized == false)
291 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000292 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
Greg Claytonf97c5212011-12-29 00:05:26 +0000293 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +0000294 m_objfile.GetModule()->LogMessage(log, "Reading EH frame info");
Greg Claytonc9660542012-02-05 02:38:54 +0000295 m_objfile.ReadSectionData (m_section_sp.get(), m_cfi_data);
Greg Claytonf97c5212011-12-29 00:05:26 +0000296 m_cfi_data_initialized = true;
297 }
298}
Jason Molendafbcb7f22010-09-10 07:49:16 +0000299// Scan through the eh_frame or debug_frame section looking for FDEs and noting the start/end addresses
300// of the functions and a pointer back to the function's FDE for later expansion.
301// Internalize CIEs as we come across them.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000302
Jason Molendafbcb7f22010-09-10 07:49:16 +0000303void
304DWARFCallFrameInfo::GetFDEIndex ()
305{
Ed Masted4612ad2014-04-20 13:17:36 +0000306 if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
Jason Molendafbcb7f22010-09-10 07:49:16 +0000307 return;
Sean Callanan7d69f122012-07-12 01:11:40 +0000308
Jason Molendafbcb7f22010-09-10 07:49:16 +0000309 if (m_fde_index_initialized)
310 return;
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000311
312 std::lock_guard<std::mutex> guard(m_fde_index_mutex);
313
Sean Callanan7d69f122012-07-12 01:11:40 +0000314 if (m_fde_index_initialized) // if two threads hit the locker
315 return;
Jason Molendae6194f12010-10-26 12:01:35 +0000316
Jason Molenda74303822013-03-20 21:57:42 +0000317 Timer scoped_timer (__PRETTY_FUNCTION__, "%s - %s", __PRETTY_FUNCTION__, m_objfile.GetFileSpec().GetFilename().AsCString(""));
318
Jason Molendac7afda52016-06-07 02:19:54 +0000319 bool clear_address_zeroth_bit = false;
320 ArchSpec arch;
321 if (m_objfile.GetArchitecture (arch))
322 {
323 if (arch.GetTriple().getArch() == llvm::Triple::arm || arch.GetTriple().getArch() == llvm::Triple::thumb)
324 clear_address_zeroth_bit = true;
325 }
326
Greg Claytonc7bece562013-01-25 18:06:21 +0000327 lldb::offset_t offset = 0;
Jason Molendafbcb7f22010-09-10 07:49:16 +0000328 if (m_cfi_data_initialized == false)
Greg Claytonf97c5212011-12-29 00:05:26 +0000329 GetCFIData();
Jason Molendafbcb7f22010-09-10 07:49:16 +0000330 while (m_cfi_data.ValidOffsetForDataOfSize (offset, 8))
331 {
Greg Clayton73bf5db2011-06-17 01:22:15 +0000332 const dw_offset_t current_entry = offset;
Todd Fiala1767e112014-08-25 21:39:30 +0000333 dw_offset_t cie_id, next_entry, cie_offset;
Jason Molendafbcb7f22010-09-10 07:49:16 +0000334 uint32_t len = m_cfi_data.GetU32 (&offset);
Todd Fiala1767e112014-08-25 21:39:30 +0000335 bool is_64bit = (len == UINT32_MAX);
336 if (is_64bit) {
337 len = m_cfi_data.GetU64 (&offset);
338 cie_id = m_cfi_data.GetU64 (&offset);
339 next_entry = current_entry + len + 12;
340 cie_offset = current_entry + 12 - cie_id;
341 } else {
342 cie_id = m_cfi_data.GetU32 (&offset);
343 next_entry = current_entry + len + 4;
344 cie_offset = current_entry + 4 - cie_id;
345 }
Jason Molendafbcb7f22010-09-10 07:49:16 +0000346
Jason Molendac48ef342015-04-02 04:35:32 +0000347 if (next_entry > m_cfi_data.GetByteSize() + 1)
348 {
349 Host::SystemLog (Host::eSystemLogError,
350 "error: Invalid fde/cie next entry offset of 0x%x found in cie/fde at 0x%x\n",
351 next_entry,
352 current_entry);
Bruce Mitchenere171da52015-07-22 00:16:02 +0000353 // Don't trust anything in this eh_frame section if we find blatantly
Jason Molendac48ef342015-04-02 04:35:32 +0000354 // invalid data.
355 m_fde_index.Clear();
356 m_fde_index_initialized = true;
357 return;
358 }
359 if (cie_offset > m_cfi_data.GetByteSize())
360 {
361 Host::SystemLog (Host::eSystemLogError,
362 "error: Invalid cie offset of 0x%x found in cie/fde at 0x%x\n",
363 cie_offset,
364 current_entry);
Bruce Mitchenere171da52015-07-22 00:16:02 +0000365 // Don't trust anything in this eh_frame section if we find blatantly
Jason Molendac48ef342015-04-02 04:35:32 +0000366 // invalid data.
367 m_fde_index.Clear();
368 m_fde_index_initialized = true;
369 return;
370 }
371
Virgile Bello8452cb52013-08-25 13:24:48 +0000372 if (cie_id == 0 || cie_id == UINT32_MAX || len == 0)
Jason Molendafbcb7f22010-09-10 07:49:16 +0000373 {
374 m_cie_map[current_entry] = ParseCIE (current_entry);
375 offset = next_entry;
376 continue;
377 }
378
Greg Clayton73bf5db2011-06-17 01:22:15 +0000379 const CIE *cie = GetCIE (cie_offset);
380 if (cie)
381 {
Greg Claytonc9660542012-02-05 02:38:54 +0000382 const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
Greg Clayton73bf5db2011-06-17 01:22:15 +0000383 const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS;
384 const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
Jason Molendafbcb7f22010-09-10 07:49:16 +0000385
Greg Clayton73bf5db2011-06-17 01:22:15 +0000386 lldb::addr_t addr = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding, pc_rel_addr, text_addr, data_addr);
Jason Molendac7afda52016-06-07 02:19:54 +0000387 if (clear_address_zeroth_bit)
388 addr &= ~1ull;
389
Greg Clayton73bf5db2011-06-17 01:22:15 +0000390 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 Molenda74303822013-03-20 21:57:42 +0000391 FDEEntryMap::Entry fde (addr, length, current_entry);
392 m_fde_index.Append(fde);
Greg Clayton73bf5db2011-06-17 01:22:15 +0000393 }
394 else
395 {
Greg Claytone38a5ed2012-01-05 03:57:59 +0000396 Host::SystemLog (Host::eSystemLogError,
397 "error: unable to find CIE at 0x%8.8x for cie_id = 0x%8.8x for entry at 0x%8.8x.\n",
398 cie_offset,
399 cie_id,
400 current_entry);
Greg Clayton73bf5db2011-06-17 01:22:15 +0000401 }
Jason Molendafbcb7f22010-09-10 07:49:16 +0000402 offset = next_entry;
403 }
Jason Molenda74303822013-03-20 21:57:42 +0000404 m_fde_index.Sort();
Jason Molendafbcb7f22010-09-10 07:49:16 +0000405 m_fde_index_initialized = true;
406}
407
408bool
Greg Claytonc7bece562013-01-25 18:06:21 +0000409DWARFCallFrameInfo::FDEToUnwindPlan (dw_offset_t dwarf_offset, Address startaddr, UnwindPlan& unwind_plan)
Jason Molendafbcb7f22010-09-10 07:49:16 +0000410{
Greg Claytonc7bece562013-01-25 18:06:21 +0000411 lldb::offset_t offset = dwarf_offset;
412 lldb::offset_t current_entry = offset;
Jason Molendafbcb7f22010-09-10 07:49:16 +0000413
Ed Masted4612ad2014-04-20 13:17:36 +0000414 if (m_section_sp.get() == nullptr || m_section_sp->IsEncrypted())
Jason Molendafbcb7f22010-09-10 07:49:16 +0000415 return false;
416
417 if (m_cfi_data_initialized == false)
Greg Claytonf97c5212011-12-29 00:05:26 +0000418 GetCFIData();
Jason Molendafbcb7f22010-09-10 07:49:16 +0000419
420 uint32_t length = m_cfi_data.GetU32 (&offset);
Todd Fiala1767e112014-08-25 21:39:30 +0000421 dw_offset_t cie_offset;
422 bool is_64bit = (length == UINT32_MAX);
423 if (is_64bit) {
424 length = m_cfi_data.GetU64 (&offset);
425 cie_offset = m_cfi_data.GetU64 (&offset);
426 } else {
427 cie_offset = m_cfi_data.GetU32 (&offset);
428 }
Jason Molendafbcb7f22010-09-10 07:49:16 +0000429
430 assert (cie_offset != 0 && cie_offset != UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000431
432 // Translate the CIE_id from the eh_frame format, which
433 // is relative to the FDE offset, into a __eh_frame section
434 // offset
Jason Molendafbcb7f22010-09-10 07:49:16 +0000435 if (m_is_eh_frame)
Jason Molendaab4f1922010-10-25 11:12:07 +0000436 {
437 unwind_plan.SetSourceName ("eh_frame CFI");
Todd Fiala1767e112014-08-25 21:39:30 +0000438 cie_offset = current_entry + (is_64bit ? 12 : 4) - cie_offset;
Jason Molenda60f0bd42012-10-26 06:08:58 +0000439 unwind_plan.SetUnwindPlanValidAtAllInstructions (eLazyBoolNo);
Jason Molendaab4f1922010-10-25 11:12:07 +0000440 }
441 else
442 {
443 unwind_plan.SetSourceName ("DWARF CFI");
Jason Molenda60f0bd42012-10-26 06:08:58 +0000444 // In theory the debug_frame info should be valid at all call sites
445 // ("asynchronous unwind info" as it is sometimes called) but in practice
446 // gcc et al all emit call frame info for the prologue and call sites, but
447 // not for the epilogue or all the other locations during the function reliably.
448 unwind_plan.SetUnwindPlanValidAtAllInstructions (eLazyBoolNo);
Jason Molendaab4f1922010-10-25 11:12:07 +0000449 }
Jason Molenda60f0bd42012-10-26 06:08:58 +0000450 unwind_plan.SetSourcedFromCompiler (eLazyBoolYes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000451
Jason Molendafbcb7f22010-09-10 07:49:16 +0000452 const CIE *cie = GetCIE (cie_offset);
Ed Masted4612ad2014-04-20 13:17:36 +0000453 assert (cie != nullptr);
Jason Molendafbcb7f22010-09-10 07:49:16 +0000454
Todd Fiala1767e112014-08-25 21:39:30 +0000455 const dw_offset_t end_offset = current_entry + length + (is_64bit ? 12 : 4);
Jason Molendafbcb7f22010-09-10 07:49:16 +0000456
Greg Claytonc9660542012-02-05 02:38:54 +0000457 const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
Jason Molendafbcb7f22010-09-10 07:49:16 +0000458 const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS;
459 const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
460 lldb::addr_t range_base = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding, pc_rel_addr, text_addr, data_addr);
461 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);
462 AddressRange range (range_base, m_objfile.GetAddressByteSize(), m_objfile.GetSectionList());
463 range.SetByteSize (range_len);
464
Jason Molendae9c7ecf2014-11-18 02:27:42 +0000465 addr_t lsda_data_file_address = LLDB_INVALID_ADDRESS;
466
Jason Molendafbcb7f22010-09-10 07:49:16 +0000467 if (cie->augmentation[0] == 'z')
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000468 {
Jason Molendafbcb7f22010-09-10 07:49:16 +0000469 uint32_t aug_data_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
Jason Molendae9c7ecf2014-11-18 02:27:42 +0000470 if (aug_data_len != 0 && cie->lsda_addr_encoding != DW_EH_PE_omit)
471 {
472 offset_t saved_offset = offset;
473 lsda_data_file_address = m_cfi_data.GetGNUEHPointer(&offset, cie->lsda_addr_encoding, pc_rel_addr, text_addr, data_addr);
474 if (offset - saved_offset != aug_data_len)
475 {
476 // There is more in the augmentation region than we know how to process;
477 // don't read anything.
478 lsda_data_file_address = LLDB_INVALID_ADDRESS;
479 }
480 offset = saved_offset;
481 }
Jason Molendafbcb7f22010-09-10 07:49:16 +0000482 offset += aug_data_len;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000483 }
Jason Molendae9c7ecf2014-11-18 02:27:42 +0000484 Address lsda_data;
485 Address personality_function_ptr;
486
487 if (lsda_data_file_address != LLDB_INVALID_ADDRESS && cie->personality_loc != LLDB_INVALID_ADDRESS)
488 {
489 m_objfile.GetModule()->ResolveFileAddress (lsda_data_file_address, lsda_data);
490 m_objfile.GetModule()->ResolveFileAddress (cie->personality_loc, personality_function_ptr);
491 }
492
493 if (lsda_data.IsValid() && personality_function_ptr.IsValid())
494 {
495 unwind_plan.SetLSDAAddress (lsda_data);
496 unwind_plan.SetPersonalityFunctionPtr (personality_function_ptr);
497 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000498
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000499 uint32_t code_align = cie->code_align;
500 int32_t data_align = cie->data_align;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000501
Jason Molendafbcb7f22010-09-10 07:49:16 +0000502 unwind_plan.SetPlanValidAddressRange (range);
Jason Molenda1d42c7b2012-07-14 04:52:53 +0000503 UnwindPlan::Row *cie_initial_row = new UnwindPlan::Row;
504 *cie_initial_row = cie->initial_row;
505 UnwindPlan::RowSP row(cie_initial_row);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000506
Jason Molendafbcb7f22010-09-10 07:49:16 +0000507 unwind_plan.SetRegisterKind (m_reg_kind);
Jason Molenda8eba46c2012-08-18 06:53:34 +0000508 unwind_plan.SetReturnAddressRegister (cie->return_addr_reg_num);
Jason Molendafbcb7f22010-09-10 07:49:16 +0000509
Richard Mittonf70d6042013-09-12 23:38:30 +0000510 std::vector<UnwindPlan::RowSP> stack;
511
Jason Molendafbcb7f22010-09-10 07:49:16 +0000512 UnwindPlan::Row::RegisterLocation reg_location;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000513 while (m_cfi_data.ValidOffset(offset) && offset < end_offset)
514 {
515 uint8_t inst = m_cfi_data.GetU8(&offset);
516 uint8_t primary_opcode = inst & 0xC0;
517 uint8_t extended_opcode = inst & 0x3F;
518
Tamas Berghammer623652a2015-07-03 09:30:17 +0000519 if (!HandleCommonDwarfOpcode(primary_opcode, extended_opcode, data_align, offset, *row))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000520 {
Tamas Berghammer623652a2015-07-03 09:30:17 +0000521 if (primary_opcode)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000522 {
Tamas Berghammer623652a2015-07-03 09:30:17 +0000523 switch (primary_opcode)
524 {
525 case DW_CFA_advance_loc : // (Row Creation Instruction)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000526 { // 0x40 - high 2 bits are 0x1, lower 6 bits are delta
527 // takes a single argument that represents a constant delta. The
528 // required action is to create a new table row with a location
529 // value that is computed by taking the current entry's location
530 // value and adding (delta * code_align). All other
531 // values in the new row are initially identical to the current row.
Jason Molendafbcb7f22010-09-10 07:49:16 +0000532 unwind_plan.AppendRow(row);
Jason Molendafa67e872012-07-31 22:42:30 +0000533 UnwindPlan::Row *newrow = new UnwindPlan::Row;
534 *newrow = *row.get();
535 row.reset (newrow);
Jason Molenda1d42c7b2012-07-14 04:52:53 +0000536 row->SlideOffset(extended_opcode * code_align);
Tamas Berghammer623652a2015-07-03 09:30:17 +0000537 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000538 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000539
Tamas Berghammer623652a2015-07-03 09:30:17 +0000540 case DW_CFA_restore :
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000541 { // 0xC0 - high 2 bits are 0x3, lower 6 bits are register
542 // takes a single argument that represents a register number. The
543 // required action is to change the rule for the indicated register
544 // to the rule assigned it by the initial_instructions in the CIE.
Tamas Berghammer623652a2015-07-03 09:30:17 +0000545 uint32_t reg_num = extended_opcode;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000546 // We only keep enough register locations around to
547 // unwind what is in our thread, and these are organized
548 // by the register index in that state, so we need to convert our
Jason Molenda63bd0db2015-09-15 23:20:34 +0000549 // eh_frame register number from the EH frame info, to a register index
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000550
Jason Molenda1d42c7b2012-07-14 04:52:53 +0000551 if (unwind_plan.IsValidRowIndex(0) && unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num, reg_location))
552 row->SetRegisterInfo (reg_num, reg_location);
Tamas Berghammer623652a2015-07-03 09:30:17 +0000553 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000554 }
Tamas Berghammer623652a2015-07-03 09:30:17 +0000555 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000556 }
Tamas Berghammer623652a2015-07-03 09:30:17 +0000557 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000558 {
Tamas Berghammer623652a2015-07-03 09:30:17 +0000559 switch (extended_opcode)
560 {
561 case DW_CFA_set_loc : // 0x1 (Row Creation Instruction)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000562 {
563 // DW_CFA_set_loc takes a single argument that represents an address.
564 // The required action is to create a new table row using the
565 // specified address as the location. All other values in the new row
566 // are initially identical to the current row. The new location value
567 // should always be greater than the current one.
Jason Molendafbcb7f22010-09-10 07:49:16 +0000568 unwind_plan.AppendRow(row);
Jason Molendafa67e872012-07-31 22:42:30 +0000569 UnwindPlan::Row *newrow = new UnwindPlan::Row;
570 *newrow = *row.get();
571 row.reset (newrow);
Jason Molenda1d42c7b2012-07-14 04:52:53 +0000572 row->SetOffset(m_cfi_data.GetPointer(&offset) - startaddr.GetFileAddress());
Tamas Berghammer623652a2015-07-03 09:30:17 +0000573 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000574 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000575
Tamas Berghammer623652a2015-07-03 09:30:17 +0000576 case DW_CFA_advance_loc1 : // 0x2 (Row Creation Instruction)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000577 {
578 // takes a single uword argument that represents a constant delta.
579 // This instruction is identical to DW_CFA_advance_loc except for the
580 // encoding and size of the delta argument.
Jason Molendafbcb7f22010-09-10 07:49:16 +0000581 unwind_plan.AppendRow(row);
Jason Molendafa67e872012-07-31 22:42:30 +0000582 UnwindPlan::Row *newrow = new UnwindPlan::Row;
583 *newrow = *row.get();
584 row.reset (newrow);
Jason Molenda1d42c7b2012-07-14 04:52:53 +0000585 row->SlideOffset (m_cfi_data.GetU8(&offset) * code_align);
Tamas Berghammer623652a2015-07-03 09:30:17 +0000586 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000587 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000588
Tamas Berghammer623652a2015-07-03 09:30:17 +0000589 case DW_CFA_advance_loc2 : // 0x3 (Row Creation Instruction)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000590 {
591 // takes a single uword argument that represents a constant delta.
592 // This instruction is identical to DW_CFA_advance_loc except for the
593 // encoding and size of the delta argument.
Jason Molendafbcb7f22010-09-10 07:49:16 +0000594 unwind_plan.AppendRow(row);
Jason Molendafa67e872012-07-31 22:42:30 +0000595 UnwindPlan::Row *newrow = new UnwindPlan::Row;
596 *newrow = *row.get();
597 row.reset (newrow);
Jason Molenda1d42c7b2012-07-14 04:52:53 +0000598 row->SlideOffset (m_cfi_data.GetU16(&offset) * code_align);
Tamas Berghammer623652a2015-07-03 09:30:17 +0000599 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000600 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000601
Tamas Berghammer623652a2015-07-03 09:30:17 +0000602 case DW_CFA_advance_loc4 : // 0x4 (Row Creation Instruction)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000603 {
604 // takes a single uword argument that represents a constant delta.
605 // This instruction is identical to DW_CFA_advance_loc except for the
606 // encoding and size of the delta argument.
Jason Molendafbcb7f22010-09-10 07:49:16 +0000607 unwind_plan.AppendRow(row);
Jason Molendafa67e872012-07-31 22:42:30 +0000608 UnwindPlan::Row *newrow = new UnwindPlan::Row;
609 *newrow = *row.get();
610 row.reset (newrow);
Jason Molenda1d42c7b2012-07-14 04:52:53 +0000611 row->SlideOffset (m_cfi_data.GetU32(&offset) * code_align);
Tamas Berghammer623652a2015-07-03 09:30:17 +0000612 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000613 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000614
Tamas Berghammer623652a2015-07-03 09:30:17 +0000615 case DW_CFA_restore_extended : // 0x6
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000616 {
617 // takes a single unsigned LEB128 argument that represents a register
618 // number. This instruction is identical to DW_CFA_restore except for
619 // the encoding and size of the register argument.
Tamas Berghammer623652a2015-07-03 09:30:17 +0000620 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
Jason Molenda1d42c7b2012-07-14 04:52:53 +0000621 if (unwind_plan.IsValidRowIndex(0) && unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num, reg_location))
622 row->SetRegisterInfo (reg_num, reg_location);
Tamas Berghammer623652a2015-07-03 09:30:17 +0000623 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000624 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000625
Tamas Berghammer623652a2015-07-03 09:30:17 +0000626 case DW_CFA_remember_state : // 0xA
Jason Molendafa67e872012-07-31 22:42:30 +0000627 {
628 // These instructions define a stack of information. Encountering the
629 // DW_CFA_remember_state instruction means to save the rules for every
630 // register on the current row on the stack. Encountering the
631 // DW_CFA_restore_state instruction means to pop the set of rules off
632 // the stack and place them in the current row. (This operation is
633 // useful for compilers that move epilogue code into the body of a
634 // function.)
Richard Mittonf70d6042013-09-12 23:38:30 +0000635 stack.push_back (row);
Jason Molendafa67e872012-07-31 22:42:30 +0000636 UnwindPlan::Row *newrow = new UnwindPlan::Row;
637 *newrow = *row.get();
638 row.reset (newrow);
Tamas Berghammer623652a2015-07-03 09:30:17 +0000639 break;
Jason Molendafa67e872012-07-31 22:42:30 +0000640 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000641
Tamas Berghammer623652a2015-07-03 09:30:17 +0000642 case DW_CFA_restore_state : // 0xB
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000643 {
Tamas Berghammer623652a2015-07-03 09:30:17 +0000644 // These instructions define a stack of information. Encountering the
645 // DW_CFA_remember_state instruction means to save the rules for every
646 // register on the current row on the stack. Encountering the
647 // DW_CFA_restore_state instruction means to pop the set of rules off
648 // the stack and place them in the current row. (This operation is
649 // useful for compilers that move epilogue code into the body of a
650 // function.)
Tamas Berghammer49dd6d12015-07-03 09:30:14 +0000651 lldb::addr_t offset = row->GetOffset ();
Richard Mittonf70d6042013-09-12 23:38:30 +0000652 row = stack.back ();
653 stack.pop_back ();
Tamas Berghammer49dd6d12015-07-03 09:30:14 +0000654 row->SetOffset (offset);
Tamas Berghammer623652a2015-07-03 09:30:17 +0000655 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000656 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000657
Tamas Berghammer623652a2015-07-03 09:30:17 +0000658 case DW_CFA_val_offset : // 0x14
659 case DW_CFA_val_offset_sf : // 0x15
660 default:
661 break;
662 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000663 }
664 }
665 }
Jason Molendafbcb7f22010-09-10 07:49:16 +0000666 unwind_plan.AppendRow(row);
667
668 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000669}
Tamas Berghammer623652a2015-07-03 09:30:17 +0000670
671bool
672DWARFCallFrameInfo::HandleCommonDwarfOpcode(uint8_t primary_opcode,
673 uint8_t extended_opcode,
674 int32_t data_align,
675 lldb::offset_t& offset,
676 UnwindPlan::Row& row)
677{
678 UnwindPlan::Row::RegisterLocation reg_location;
679
680 if (primary_opcode)
681 {
682 switch (primary_opcode)
683 {
684 case DW_CFA_offset:
685 { // 0x80 - high 2 bits are 0x2, lower 6 bits are register
686 // takes two arguments: an unsigned LEB128 constant representing a
687 // factored offset and a register number. The required action is to
688 // change the rule for the register indicated by the register number
689 // to be an offset(N) rule with a value of
690 // (N = factored offset * data_align).
691 uint8_t reg_num = extended_opcode;
692 int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
693 reg_location.SetAtCFAPlusOffset(op_offset);
694 row.SetRegisterInfo(reg_num, reg_location);
695 return true;
696 }
697 }
698 }
699 else
700 {
701 switch (extended_opcode)
702 {
703 case DW_CFA_nop : // 0x0
704 return true;
705
706 case DW_CFA_offset_extended : // 0x5
707 {
708 // takes two unsigned LEB128 arguments representing a register number
709 // and a factored offset. This instruction is identical to DW_CFA_offset
710 // except for the encoding and size of the register argument.
711 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
712 int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
713 UnwindPlan::Row::RegisterLocation reg_location;
714 reg_location.SetAtCFAPlusOffset(op_offset);
715 row.SetRegisterInfo(reg_num, reg_location);
716 return true;
717 }
718
719 case DW_CFA_undefined : // 0x7
720 {
721 // takes a single unsigned LEB128 argument that represents a register
722 // number. The required action is to set the rule for the specified
723 // register to undefined.
724 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
725 UnwindPlan::Row::RegisterLocation reg_location;
726 reg_location.SetUndefined();
727 row.SetRegisterInfo(reg_num, reg_location);
728 return true;
729 }
730
731 case DW_CFA_same_value : // 0x8
732 {
733 // takes a single unsigned LEB128 argument that represents a register
734 // number. The required action is to set the rule for the specified
735 // register to same value.
736 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
737 UnwindPlan::Row::RegisterLocation reg_location;
738 reg_location.SetSame();
739 row.SetRegisterInfo (reg_num, reg_location);
740 return true;
741 }
742
743 case DW_CFA_register : // 0x9
744 {
745 // takes two unsigned LEB128 arguments representing register numbers.
746 // The required action is to set the rule for the first register to be
747 // the second register.
748 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
749 uint32_t other_reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
750 UnwindPlan::Row::RegisterLocation reg_location;
751 reg_location.SetInRegister(other_reg_num);
752 row.SetRegisterInfo (reg_num, reg_location);
753 return true;
754 }
755
756 case DW_CFA_def_cfa : // 0xC (CFA Definition Instruction)
757 {
758 // Takes two unsigned LEB128 operands representing a register
759 // number and a (non-factored) offset. The required action
760 // is to define the current CFA rule to use the provided
761 // register and offset.
762 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
763 int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
764 row.GetCFAValue().SetIsRegisterPlusOffset (reg_num, op_offset);
765 return true;
766 }
767
768 case DW_CFA_def_cfa_register : // 0xD (CFA Definition Instruction)
769 {
770 // takes a single unsigned LEB128 argument representing a register
771 // number. The required action is to define the current CFA rule to
772 // use the provided register (but to keep the old offset).
773 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
774 row.GetCFAValue().SetIsRegisterPlusOffset (reg_num, row.GetCFAValue().GetOffset());
775 return true;
776 }
777
778 case DW_CFA_def_cfa_offset : // 0xE (CFA Definition Instruction)
779 {
780 // Takes a single unsigned LEB128 operand representing a
781 // (non-factored) offset. The required action is to define
782 // the current CFA rule to use the provided offset (but
783 // to keep the old register).
784 int32_t op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
785 row.GetCFAValue().SetIsRegisterPlusOffset(row.GetCFAValue().GetRegisterNumber(), op_offset);
786 return true;
787 }
788
789 case DW_CFA_def_cfa_expression : // 0xF (CFA Definition Instruction)
790 {
791 size_t block_len = (size_t)m_cfi_data.GetULEB128(&offset);
792 const uint8_t *block_data = static_cast<const uint8_t*>(m_cfi_data.GetData(&offset, block_len));
793 row.GetCFAValue().SetIsDWARFExpression(block_data, block_len);
794 return true;
795 }
796
797 case DW_CFA_expression : // 0x10
798 {
799 // Takes two operands: an unsigned LEB128 value representing
800 // a register number, and a DW_FORM_block value representing a DWARF
801 // expression. The required action is to change the rule for the
802 // register indicated by the register number to be an expression(E)
803 // rule where E is the DWARF expression. That is, the DWARF
804 // expression computes the address. The value of the CFA is
805 // pushed on the DWARF evaluation stack prior to execution of
806 // the DWARF expression.
807 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
808 uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
809 const uint8_t *block_data = static_cast<const uint8_t*>(m_cfi_data.GetData(&offset, block_len));
810 UnwindPlan::Row::RegisterLocation reg_location;
811 reg_location.SetAtDWARFExpression(block_data, block_len);
812 row.SetRegisterInfo(reg_num, reg_location);
813 return true;
814 }
815
816 case DW_CFA_offset_extended_sf : // 0x11
817 {
818 // takes two operands: an unsigned LEB128 value representing a
819 // register number and a signed LEB128 factored offset. This
820 // instruction is identical to DW_CFA_offset_extended except
821 //that the second operand is signed and factored.
822 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
823 int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
824 UnwindPlan::Row::RegisterLocation reg_location;
825 reg_location.SetAtCFAPlusOffset(op_offset);
826 row.SetRegisterInfo(reg_num, reg_location);
827 return true;
828 }
829
830 case DW_CFA_def_cfa_sf : // 0x12 (CFA Definition Instruction)
831 {
832 // Takes two operands: an unsigned LEB128 value representing
833 // a register number and a signed LEB128 factored offset.
834 // This instruction is identical to DW_CFA_def_cfa except
835 // that the second operand is signed and factored.
836 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
837 int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
838 row.GetCFAValue().SetIsRegisterPlusOffset (reg_num, op_offset);
839 return true;
840 }
841
842 case DW_CFA_def_cfa_offset_sf : // 0x13 (CFA Definition Instruction)
843 {
844 // takes a signed LEB128 operand representing a factored
845 // offset. This instruction is identical to DW_CFA_def_cfa_offset
846 // except that the operand is signed and factored.
847 int32_t op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
848 uint32_t cfa_regnum = row.GetCFAValue().GetRegisterNumber();
849 row.GetCFAValue().SetIsRegisterPlusOffset(cfa_regnum, op_offset);
850 return true;
851 }
852
853 case DW_CFA_val_expression : // 0x16
854 {
855 // takes two operands: an unsigned LEB128 value representing a register
856 // number, and a DW_FORM_block value representing a DWARF expression.
857 // The required action is to change the rule for the register indicated
858 // by the register number to be a val_expression(E) rule where E is the
859 // DWARF expression. That is, the DWARF expression computes the value of
860 // the given register. The value of the CFA is pushed on the DWARF
861 // evaluation stack prior to execution of the DWARF expression.
862 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
863 uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
864 const uint8_t* block_data = (const uint8_t*)m_cfi_data.GetData(&offset, block_len);
865//#if defined(__i386__) || defined(__x86_64__)
866// // The EH frame info for EIP and RIP contains code that looks for traps to
867// // be a specific type and increments the PC.
868// // For i386:
869// // DW_CFA_val_expression where:
870// // eip = DW_OP_breg6(+28), DW_OP_deref, DW_OP_dup, DW_OP_plus_uconst(0x34),
871// // DW_OP_deref, DW_OP_swap, DW_OP_plus_uconst(0), DW_OP_deref,
872// // DW_OP_dup, DW_OP_lit3, DW_OP_ne, DW_OP_swap, DW_OP_lit4, DW_OP_ne,
873// // DW_OP_and, DW_OP_plus
874// // This basically does a:
875// // eip = ucontenxt.mcontext32->gpr.eip;
876// // if (ucontenxt.mcontext32->exc.trapno != 3 && ucontenxt.mcontext32->exc.trapno != 4)
877// // eip++;
878// //
879// // For x86_64:
880// // DW_CFA_val_expression where:
881// // rip = DW_OP_breg3(+48), DW_OP_deref, DW_OP_dup, DW_OP_plus_uconst(0x90), DW_OP_deref,
882// // DW_OP_swap, DW_OP_plus_uconst(0), DW_OP_deref_size(4), DW_OP_dup, DW_OP_lit3,
883// // DW_OP_ne, DW_OP_swap, DW_OP_lit4, DW_OP_ne, DW_OP_and, DW_OP_plus
884// // This basically does a:
885// // rip = ucontenxt.mcontext64->gpr.rip;
886// // if (ucontenxt.mcontext64->exc.trapno != 3 && ucontenxt.mcontext64->exc.trapno != 4)
887// // rip++;
888// // The trap comparisons and increments are not needed as it hoses up the unwound PC which
889// // is expected to point at least past the instruction that causes the fault/trap. So we
890// // take it out by trimming the expression right at the first "DW_OP_swap" opcodes
891// if (block_data != NULL && thread->GetPCRegNum(Thread::GCC) == reg_num)
892// {
893// if (thread->Is64Bit())
894// {
895// if (block_len > 9 && block_data[8] == DW_OP_swap && block_data[9] == DW_OP_plus_uconst)
896// block_len = 8;
897// }
898// else
899// {
900// if (block_len > 8 && block_data[7] == DW_OP_swap && block_data[8] == DW_OP_plus_uconst)
901// block_len = 7;
902// }
903// }
904//#endif
905 reg_location.SetIsDWARFExpression(block_data, block_len);
906 row.SetRegisterInfo (reg_num, reg_location);
907 return true;
908 }
909 }
910 }
911 return false;
912}
Tamas Berghammer6b63b142016-02-18 11:12:18 +0000913
914void
915DWARFCallFrameInfo::ForEachFDEEntries(
916 const std::function<bool(lldb::addr_t, uint32_t, dw_offset_t)>& callback)
917{
918 GetFDEIndex();
919
920 for (size_t i = 0, c = m_fde_index.GetSize(); i < c; ++i)
921 {
922 const FDEEntryMap::Entry& entry = m_fde_index.GetEntryRef(i);
923 if (!callback(entry.base, entry.size, entry.data))
924 break;
925 }
926}