blob: d3d962896694cbe8f9174621e6fbee65b249d203 [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();
58 if (module_sp.get() == NULL || module_sp->GetObjectFile() == NULL || module_sp->GetObjectFile() != &m_objfile)
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
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();
73 if (module_sp.get() == NULL || module_sp->GetObjectFile() == NULL || module_sp->GetObjectFile() != &m_objfile)
Jason Molendafbcb7f22010-09-10 07:49:16 +000074 return false;
Jason Molenda74303822013-03-20 21:57:42 +000075
76 if (m_section_sp.get() == NULL || m_section_sp->IsEncrypted())
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 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{
Greg Claytonc9660542012-02-05 02:38:54 +000090 if (m_section_sp.get() == NULL || 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
Jason Molenda74303822013-03-20 21:57:42 +0000100 if (fde == NULL)
101 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
134 if (pos->second.get() == NULL)
135 pos->second = ParseCIE (cie_offset);
136
137 return pos->second.get();
138 }
139 return NULL;
140}
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();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000149 const uint32_t length = m_cfi_data.GetU32(&offset);
150 const dw_offset_t cie_id = m_cfi_data.GetU32(&offset);
151 const dw_offset_t end_offset = cie_offset + length + 4;
Greg Claytonc7bece562013-01-25 18:06:21 +0000152 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 +0000153 {
154 size_t i;
155 // cie.offset = cie_offset;
156 // cie.length = length;
157 // cie.cieID = cieID;
Ashok Thirumurthi6e264d32013-07-29 16:05:11 +0000158 cie_sp->ptr_encoding = DW_EH_PE_absptr; // default
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000159 cie_sp->version = m_cfi_data.GetU8(&offset);
160
161 for (i=0; i<CFI_AUG_MAX_SIZE; ++i)
162 {
163 cie_sp->augmentation[i] = m_cfi_data.GetU8(&offset);
164 if (cie_sp->augmentation[i] == '\0')
165 {
166 // Zero out remaining bytes in augmentation string
167 for (size_t j = i+1; j<CFI_AUG_MAX_SIZE; ++j)
168 cie_sp->augmentation[j] = '\0';
169
170 break;
171 }
172 }
173
174 if (i == CFI_AUG_MAX_SIZE && cie_sp->augmentation[CFI_AUG_MAX_SIZE-1] != '\0')
175 {
Greg Claytone38a5ed2012-01-05 03:57:59 +0000176 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 +0000177 return cie_sp;
178 }
179 cie_sp->code_align = (uint32_t)m_cfi_data.GetULEB128(&offset);
180 cie_sp->data_align = (int32_t)m_cfi_data.GetSLEB128(&offset);
181 cie_sp->return_addr_reg_num = m_cfi_data.GetU8(&offset);
182
183 if (cie_sp->augmentation[0])
184 {
185 // Get the length of the eh_frame augmentation data
186 // which starts with a ULEB128 length in bytes
187 const size_t aug_data_len = (size_t)m_cfi_data.GetULEB128(&offset);
188 const size_t aug_data_end = offset + aug_data_len;
189 const size_t aug_str_len = strlen(cie_sp->augmentation);
190 // A 'z' may be present as the first character of the string.
191 // If present, the Augmentation Data field shall be present.
192 // The contents of the Augmentation Data shall be intepreted
193 // according to other characters in the Augmentation String.
194 if (cie_sp->augmentation[0] == 'z')
195 {
196 // Extract the Augmentation Data
197 size_t aug_str_idx = 0;
198 for (aug_str_idx = 1; aug_str_idx < aug_str_len; aug_str_idx++)
199 {
200 char aug = cie_sp->augmentation[aug_str_idx];
201 switch (aug)
202 {
203 case 'L':
204 // Indicates the presence of one argument in the
205 // Augmentation Data of the CIE, and a corresponding
206 // argument in the Augmentation Data of the FDE. The
207 // argument in the Augmentation Data of the CIE is
208 // 1-byte and represents the pointer encoding used
209 // for the argument in the Augmentation Data of the
210 // FDE, which is the address of a language-specific
211 // data area (LSDA). The size of the LSDA pointer is
212 // specified by the pointer encoding used.
213 m_cfi_data.GetU8(&offset);
214 break;
215
216 case 'P':
217 // Indicates the presence of two arguments in the
218 // Augmentation Data of the cie_sp-> The first argument
219 // is 1-byte and represents the pointer encoding
220 // used for the second argument, which is the
221 // address of a personality routine handler. The
222 // size of the personality routine pointer is
223 // specified by the pointer encoding used.
224 {
225 uint8_t arg_ptr_encoding = m_cfi_data.GetU8(&offset);
226 m_cfi_data.GetGNUEHPointer(&offset, arg_ptr_encoding, LLDB_INVALID_ADDRESS, LLDB_INVALID_ADDRESS, LLDB_INVALID_ADDRESS);
227 }
228 break;
229
230 case 'R':
231 // A 'R' may be present at any position after the
232 // first character of the string. The Augmentation
233 // Data shall include a 1 byte argument that
234 // represents the pointer encoding for the address
235 // pointers used in the FDE.
Ashok Thirumurthi6e264d32013-07-29 16:05:11 +0000236 // Example: 0x1B == DW_EH_PE_pcrel | DW_EH_PE_sdata4
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000237 cie_sp->ptr_encoding = m_cfi_data.GetU8(&offset);
238 break;
239 }
240 }
241 }
242 else if (strcmp(cie_sp->augmentation, "eh") == 0)
243 {
244 // If the Augmentation string has the value "eh", then
245 // the EH Data field shall be present
246 }
247
248 // Set the offset to be the end of the augmentation data just in case
249 // we didn't understand any of the data.
250 offset = (uint32_t)aug_data_end;
251 }
252
253 if (end_offset > offset)
254 {
255 cie_sp->inst_offset = offset;
256 cie_sp->inst_length = end_offset - offset;
257 }
Jason Molendafbcb7f22010-09-10 07:49:16 +0000258 while (offset < end_offset)
259 {
260 uint8_t inst = m_cfi_data.GetU8(&offset);
261 uint8_t primary_opcode = inst & 0xC0;
262 uint8_t extended_opcode = inst & 0x3F;
263
264 if (extended_opcode == DW_CFA_def_cfa)
265 {
266 // Takes two unsigned LEB128 operands representing a register
267 // number and a (non-factored) offset. The required action
268 // is to define the current CFA rule to use the provided
269 // register and offset.
270 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
271 int op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
272 cie_sp->initial_row.SetCFARegister (reg_num);
273 cie_sp->initial_row.SetCFAOffset (op_offset);
274 continue;
275 }
276 if (primary_opcode == DW_CFA_offset)
277 {
278 // 0x80 - high 2 bits are 0x2, lower 6 bits are register.
279 // Takes two arguments: an unsigned LEB128 constant representing a
280 // factored offset and a register number. The required action is to
281 // change the rule for the register indicated by the register number
282 // to be an offset(N) rule with a value of
283 // (N = factored offset * data_align).
284 uint32_t reg_num = extended_opcode;
285 int op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * cie_sp->data_align;
286 UnwindPlan::Row::RegisterLocation reg_location;
287 reg_location.SetAtCFAPlusOffset(op_offset);
288 cie_sp->initial_row.SetRegisterInfo (reg_num, reg_location);
289 continue;
290 }
291 if (extended_opcode == DW_CFA_nop)
292 {
293 continue;
294 }
295 break; // Stop if we hit an unrecognized opcode
296 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000297 }
298
299 return cie_sp;
300}
301
Greg Claytonf97c5212011-12-29 00:05:26 +0000302void
303DWARFCallFrameInfo::GetCFIData()
304{
305 if (m_cfi_data_initialized == false)
306 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000307 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
Greg Claytonf97c5212011-12-29 00:05:26 +0000308 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +0000309 m_objfile.GetModule()->LogMessage(log, "Reading EH frame info");
Greg Claytonc9660542012-02-05 02:38:54 +0000310 m_objfile.ReadSectionData (m_section_sp.get(), m_cfi_data);
Greg Claytonf97c5212011-12-29 00:05:26 +0000311 m_cfi_data_initialized = true;
312 }
313}
Jason Molendafbcb7f22010-09-10 07:49:16 +0000314// Scan through the eh_frame or debug_frame section looking for FDEs and noting the start/end addresses
315// of the functions and a pointer back to the function's FDE for later expansion.
316// Internalize CIEs as we come across them.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000317
Jason Molendafbcb7f22010-09-10 07:49:16 +0000318void
319DWARFCallFrameInfo::GetFDEIndex ()
320{
Greg Claytonc9660542012-02-05 02:38:54 +0000321 if (m_section_sp.get() == NULL || m_section_sp->IsEncrypted())
Jason Molendafbcb7f22010-09-10 07:49:16 +0000322 return;
Sean Callanan7d69f122012-07-12 01:11:40 +0000323
Jason Molendafbcb7f22010-09-10 07:49:16 +0000324 if (m_fde_index_initialized)
325 return;
Sean Callanan7d69f122012-07-12 01:11:40 +0000326
327 Mutex::Locker locker(m_fde_index_mutex);
328
329 if (m_fde_index_initialized) // if two threads hit the locker
330 return;
Jason Molendae6194f12010-10-26 12:01:35 +0000331
Jason Molenda74303822013-03-20 21:57:42 +0000332 Timer scoped_timer (__PRETTY_FUNCTION__, "%s - %s", __PRETTY_FUNCTION__, m_objfile.GetFileSpec().GetFilename().AsCString(""));
333
Greg Claytonc7bece562013-01-25 18:06:21 +0000334 lldb::offset_t offset = 0;
Jason Molendafbcb7f22010-09-10 07:49:16 +0000335 if (m_cfi_data_initialized == false)
Greg Claytonf97c5212011-12-29 00:05:26 +0000336 GetCFIData();
Jason Molendafbcb7f22010-09-10 07:49:16 +0000337 while (m_cfi_data.ValidOffsetForDataOfSize (offset, 8))
338 {
Greg Clayton73bf5db2011-06-17 01:22:15 +0000339 const dw_offset_t current_entry = offset;
Jason Molendafbcb7f22010-09-10 07:49:16 +0000340 uint32_t len = m_cfi_data.GetU32 (&offset);
341 dw_offset_t next_entry = current_entry + len + 4;
342 dw_offset_t cie_id = m_cfi_data.GetU32 (&offset);
343
Virgile Bello8452cb52013-08-25 13:24:48 +0000344 if (cie_id == 0 || cie_id == UINT32_MAX || len == 0)
Jason Molendafbcb7f22010-09-10 07:49:16 +0000345 {
346 m_cie_map[current_entry] = ParseCIE (current_entry);
347 offset = next_entry;
348 continue;
349 }
350
Greg Clayton73bf5db2011-06-17 01:22:15 +0000351 const dw_offset_t cie_offset = current_entry + 4 - cie_id;
352 const CIE *cie = GetCIE (cie_offset);
353 if (cie)
354 {
Greg Claytonc9660542012-02-05 02:38:54 +0000355 const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
Greg Clayton73bf5db2011-06-17 01:22:15 +0000356 const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS;
357 const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
Jason Molendafbcb7f22010-09-10 07:49:16 +0000358
Greg Clayton73bf5db2011-06-17 01:22:15 +0000359 lldb::addr_t addr = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding, pc_rel_addr, text_addr, data_addr);
360 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 +0000361 FDEEntryMap::Entry fde (addr, length, current_entry);
362 m_fde_index.Append(fde);
Greg Clayton73bf5db2011-06-17 01:22:15 +0000363 }
364 else
365 {
Greg Claytone38a5ed2012-01-05 03:57:59 +0000366 Host::SystemLog (Host::eSystemLogError,
367 "error: unable to find CIE at 0x%8.8x for cie_id = 0x%8.8x for entry at 0x%8.8x.\n",
368 cie_offset,
369 cie_id,
370 current_entry);
Greg Clayton73bf5db2011-06-17 01:22:15 +0000371 }
Jason Molendafbcb7f22010-09-10 07:49:16 +0000372 offset = next_entry;
373 }
Jason Molenda74303822013-03-20 21:57:42 +0000374 m_fde_index.Sort();
Jason Molendafbcb7f22010-09-10 07:49:16 +0000375 m_fde_index_initialized = true;
376}
377
378bool
Greg Claytonc7bece562013-01-25 18:06:21 +0000379DWARFCallFrameInfo::FDEToUnwindPlan (dw_offset_t dwarf_offset, Address startaddr, UnwindPlan& unwind_plan)
Jason Molendafbcb7f22010-09-10 07:49:16 +0000380{
Greg Claytonc7bece562013-01-25 18:06:21 +0000381 lldb::offset_t offset = dwarf_offset;
382 lldb::offset_t current_entry = offset;
Jason Molendafbcb7f22010-09-10 07:49:16 +0000383
Greg Claytonc9660542012-02-05 02:38:54 +0000384 if (m_section_sp.get() == NULL || m_section_sp->IsEncrypted())
Jason Molendafbcb7f22010-09-10 07:49:16 +0000385 return false;
386
387 if (m_cfi_data_initialized == false)
Greg Claytonf97c5212011-12-29 00:05:26 +0000388 GetCFIData();
Jason Molendafbcb7f22010-09-10 07:49:16 +0000389
390 uint32_t length = m_cfi_data.GetU32 (&offset);
391 dw_offset_t cie_offset = m_cfi_data.GetU32 (&offset);
392
393 assert (cie_offset != 0 && cie_offset != UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000394
395 // Translate the CIE_id from the eh_frame format, which
396 // is relative to the FDE offset, into a __eh_frame section
397 // offset
Jason Molendafbcb7f22010-09-10 07:49:16 +0000398 if (m_is_eh_frame)
Jason Molendaab4f1922010-10-25 11:12:07 +0000399 {
400 unwind_plan.SetSourceName ("eh_frame CFI");
Jason Molendafbcb7f22010-09-10 07:49:16 +0000401 cie_offset = current_entry + 4 - cie_offset;
Jason Molenda60f0bd42012-10-26 06:08:58 +0000402 unwind_plan.SetUnwindPlanValidAtAllInstructions (eLazyBoolNo);
Jason Molendaab4f1922010-10-25 11:12:07 +0000403 }
404 else
405 {
406 unwind_plan.SetSourceName ("DWARF CFI");
Jason Molenda60f0bd42012-10-26 06:08:58 +0000407 // In theory the debug_frame info should be valid at all call sites
408 // ("asynchronous unwind info" as it is sometimes called) but in practice
409 // gcc et al all emit call frame info for the prologue and call sites, but
410 // not for the epilogue or all the other locations during the function reliably.
411 unwind_plan.SetUnwindPlanValidAtAllInstructions (eLazyBoolNo);
Jason Molendaab4f1922010-10-25 11:12:07 +0000412 }
Jason Molenda60f0bd42012-10-26 06:08:58 +0000413 unwind_plan.SetSourcedFromCompiler (eLazyBoolYes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000414
Jason Molendafbcb7f22010-09-10 07:49:16 +0000415 const CIE *cie = GetCIE (cie_offset);
416 assert (cie != NULL);
417
418 const dw_offset_t end_offset = current_entry + length + 4;
419
Greg Claytonc9660542012-02-05 02:38:54 +0000420 const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
Jason Molendafbcb7f22010-09-10 07:49:16 +0000421 const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS;
422 const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
423 lldb::addr_t range_base = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding, pc_rel_addr, text_addr, data_addr);
424 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);
425 AddressRange range (range_base, m_objfile.GetAddressByteSize(), m_objfile.GetSectionList());
426 range.SetByteSize (range_len);
427
428 if (cie->augmentation[0] == 'z')
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000429 {
Jason Molendafbcb7f22010-09-10 07:49:16 +0000430 uint32_t aug_data_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
431 offset += aug_data_len;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000432 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000433
434 uint32_t reg_num = 0;
435 int32_t op_offset = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000436 uint32_t code_align = cie->code_align;
437 int32_t data_align = cie->data_align;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000438
Jason Molendafbcb7f22010-09-10 07:49:16 +0000439 unwind_plan.SetPlanValidAddressRange (range);
Jason Molenda1d42c7b2012-07-14 04:52:53 +0000440 UnwindPlan::Row *cie_initial_row = new UnwindPlan::Row;
441 *cie_initial_row = cie->initial_row;
442 UnwindPlan::RowSP row(cie_initial_row);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000443
Jason Molendafbcb7f22010-09-10 07:49:16 +0000444 unwind_plan.SetRegisterKind (m_reg_kind);
Jason Molenda8eba46c2012-08-18 06:53:34 +0000445 unwind_plan.SetReturnAddressRegister (cie->return_addr_reg_num);
Jason Molendafbcb7f22010-09-10 07:49:16 +0000446
Richard Mittonf70d6042013-09-12 23:38:30 +0000447 std::vector<UnwindPlan::RowSP> stack;
448
Jason Molendafbcb7f22010-09-10 07:49:16 +0000449 UnwindPlan::Row::RegisterLocation reg_location;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000450 while (m_cfi_data.ValidOffset(offset) && offset < end_offset)
451 {
452 uint8_t inst = m_cfi_data.GetU8(&offset);
453 uint8_t primary_opcode = inst & 0xC0;
454 uint8_t extended_opcode = inst & 0x3F;
455
456 if (primary_opcode)
457 {
458 switch (primary_opcode)
459 {
460 case DW_CFA_advance_loc : // (Row Creation Instruction)
461 { // 0x40 - high 2 bits are 0x1, lower 6 bits are delta
462 // takes a single argument that represents a constant delta. The
463 // required action is to create a new table row with a location
464 // value that is computed by taking the current entry's location
465 // value and adding (delta * code_align). All other
466 // values in the new row are initially identical to the current row.
Jason Molendafbcb7f22010-09-10 07:49:16 +0000467 unwind_plan.AppendRow(row);
Jason Molendafa67e872012-07-31 22:42:30 +0000468 UnwindPlan::Row *newrow = new UnwindPlan::Row;
469 *newrow = *row.get();
470 row.reset (newrow);
Jason Molenda1d42c7b2012-07-14 04:52:53 +0000471 row->SlideOffset(extended_opcode * code_align);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000472 }
473 break;
474
475 case DW_CFA_offset :
476 { // 0x80 - high 2 bits are 0x2, lower 6 bits are register
477 // takes two arguments: an unsigned LEB128 constant representing a
478 // factored offset and a register number. The required action is to
479 // change the rule for the register indicated by the register number
480 // to be an offset(N) rule with a value of
481 // (N = factored offset * data_align).
482 reg_num = extended_opcode;
483 op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
484 reg_location.SetAtCFAPlusOffset(op_offset);
Jason Molenda1d42c7b2012-07-14 04:52:53 +0000485 row->SetRegisterInfo (reg_num, reg_location);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000486 }
487 break;
488
489 case DW_CFA_restore :
490 { // 0xC0 - high 2 bits are 0x3, lower 6 bits are register
491 // takes a single argument that represents a register number. The
492 // required action is to change the rule for the indicated register
493 // to the rule assigned it by the initial_instructions in the CIE.
494 reg_num = extended_opcode;
495 // We only keep enough register locations around to
496 // unwind what is in our thread, and these are organized
497 // by the register index in that state, so we need to convert our
Jason Molendafbcb7f22010-09-10 07:49:16 +0000498 // GCC register number from the EH frame info, to a register index
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000499
Jason Molenda1d42c7b2012-07-14 04:52:53 +0000500 if (unwind_plan.IsValidRowIndex(0) && unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num, reg_location))
501 row->SetRegisterInfo (reg_num, reg_location);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000502 }
503 break;
504 }
505 }
506 else
507 {
508 switch (extended_opcode)
509 {
510 case DW_CFA_nop : // 0x0
511 break;
512
513 case DW_CFA_set_loc : // 0x1 (Row Creation Instruction)
514 {
515 // DW_CFA_set_loc takes a single argument that represents an address.
516 // The required action is to create a new table row using the
517 // specified address as the location. All other values in the new row
518 // are initially identical to the current row. The new location value
519 // should always be greater than the current one.
Jason Molendafbcb7f22010-09-10 07:49:16 +0000520 unwind_plan.AppendRow(row);
Jason Molendafa67e872012-07-31 22:42:30 +0000521 UnwindPlan::Row *newrow = new UnwindPlan::Row;
522 *newrow = *row.get();
523 row.reset (newrow);
Jason Molenda1d42c7b2012-07-14 04:52:53 +0000524 row->SetOffset(m_cfi_data.GetPointer(&offset) - startaddr.GetFileAddress());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000525 }
526 break;
527
528 case DW_CFA_advance_loc1 : // 0x2 (Row Creation Instruction)
529 {
530 // takes a single uword argument that represents a constant delta.
531 // This instruction is identical to DW_CFA_advance_loc except for the
532 // encoding and size of the delta argument.
Jason Molendafbcb7f22010-09-10 07:49:16 +0000533 unwind_plan.AppendRow(row);
Jason Molendafa67e872012-07-31 22:42:30 +0000534 UnwindPlan::Row *newrow = new UnwindPlan::Row;
535 *newrow = *row.get();
536 row.reset (newrow);
Jason Molenda1d42c7b2012-07-14 04:52:53 +0000537 row->SlideOffset (m_cfi_data.GetU8(&offset) * code_align);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000538 }
539 break;
540
541 case DW_CFA_advance_loc2 : // 0x3 (Row Creation Instruction)
542 {
543 // takes a single uword argument that represents a constant delta.
544 // This instruction is identical to DW_CFA_advance_loc except for the
545 // encoding and size of the delta argument.
Jason Molendafbcb7f22010-09-10 07:49:16 +0000546 unwind_plan.AppendRow(row);
Jason Molendafa67e872012-07-31 22:42:30 +0000547 UnwindPlan::Row *newrow = new UnwindPlan::Row;
548 *newrow = *row.get();
549 row.reset (newrow);
Jason Molenda1d42c7b2012-07-14 04:52:53 +0000550 row->SlideOffset (m_cfi_data.GetU16(&offset) * code_align);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000551 }
552 break;
553
554 case DW_CFA_advance_loc4 : // 0x4 (Row Creation Instruction)
555 {
556 // takes a single uword argument that represents a constant delta.
557 // This instruction is identical to DW_CFA_advance_loc except for the
558 // encoding and size of the delta argument.
Jason Molendafbcb7f22010-09-10 07:49:16 +0000559 unwind_plan.AppendRow(row);
Jason Molendafa67e872012-07-31 22:42:30 +0000560 UnwindPlan::Row *newrow = new UnwindPlan::Row;
561 *newrow = *row.get();
562 row.reset (newrow);
Jason Molenda1d42c7b2012-07-14 04:52:53 +0000563 row->SlideOffset (m_cfi_data.GetU32(&offset) * code_align);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000564 }
565 break;
566
567 case DW_CFA_offset_extended : // 0x5
568 {
569 // takes two unsigned LEB128 arguments representing a register number
570 // and a factored offset. This instruction is identical to DW_CFA_offset
571 // except for the encoding and size of the register argument.
572 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
573 op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
574 reg_location.SetAtCFAPlusOffset(op_offset);
Jason Molenda1d42c7b2012-07-14 04:52:53 +0000575 row->SetRegisterInfo (reg_num, reg_location);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000576 }
577 break;
578
579 case DW_CFA_restore_extended : // 0x6
580 {
581 // takes a single unsigned LEB128 argument that represents a register
582 // number. This instruction is identical to DW_CFA_restore except for
583 // the encoding and size of the register argument.
584 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
Jason Molenda1d42c7b2012-07-14 04:52:53 +0000585 if (unwind_plan.IsValidRowIndex(0) && unwind_plan.GetRowAtIndex(0)->GetRegisterInfo(reg_num, reg_location))
586 row->SetRegisterInfo (reg_num, reg_location);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000587 }
588 break;
589
590 case DW_CFA_undefined : // 0x7
591 {
592 // takes a single unsigned LEB128 argument that represents a register
593 // number. The required action is to set the rule for the specified
594 // register to undefined.
595 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
596 reg_location.SetUndefined();
Jason Molenda1d42c7b2012-07-14 04:52:53 +0000597 row->SetRegisterInfo (reg_num, reg_location);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000598 }
599 break;
600
601 case DW_CFA_same_value : // 0x8
602 {
603 // takes a single unsigned LEB128 argument that represents a register
604 // number. The required action is to set the rule for the specified
605 // register to same value.
606 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
607 reg_location.SetSame();
Jason Molenda1d42c7b2012-07-14 04:52:53 +0000608 row->SetRegisterInfo (reg_num, reg_location);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000609 }
610 break;
611
612 case DW_CFA_register : // 0x9
613 {
614 // takes two unsigned LEB128 arguments representing register numbers.
615 // The required action is to set the rule for the first register to be
616 // the second register.
617
618 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
619 uint32_t other_reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
620 reg_location.SetInRegister(other_reg_num);
Jason Molenda1d42c7b2012-07-14 04:52:53 +0000621 row->SetRegisterInfo (reg_num, reg_location);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000622 }
623 break;
624
625 case DW_CFA_remember_state : // 0xA
Jason Molendafa67e872012-07-31 22:42:30 +0000626 {
627 // These instructions define a stack of information. Encountering the
628 // DW_CFA_remember_state instruction means to save the rules for every
629 // register on the current row on the stack. Encountering the
630 // DW_CFA_restore_state instruction means to pop the set of rules off
631 // the stack and place them in the current row. (This operation is
632 // useful for compilers that move epilogue code into the body of a
633 // function.)
Richard Mittonf70d6042013-09-12 23:38:30 +0000634 stack.push_back (row);
Jason Molendafa67e872012-07-31 22:42:30 +0000635 UnwindPlan::Row *newrow = new UnwindPlan::Row;
636 *newrow = *row.get();
637 row.reset (newrow);
638 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000639 break;
640
641 case DW_CFA_restore_state : // 0xB
642 // These instructions define a stack of information. Encountering the
643 // DW_CFA_remember_state instruction means to save the rules for every
644 // register on the current row on the stack. Encountering the
645 // DW_CFA_restore_state instruction means to pop the set of rules off
646 // the stack and place them in the current row. (This operation is
647 // useful for compilers that move epilogue code into the body of a
648 // function.)
649 {
Richard Mittonf70d6042013-09-12 23:38:30 +0000650 row = stack.back ();
651 stack.pop_back ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000652 }
653 break;
654
655 case DW_CFA_def_cfa : // 0xC (CFA Definition Instruction)
656 {
657 // Takes two unsigned LEB128 operands representing a register
658 // number and a (non-factored) offset. The required action
659 // is to define the current CFA rule to use the provided
660 // register and offset.
661 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
662 op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
Jason Molenda1d42c7b2012-07-14 04:52:53 +0000663 row->SetCFARegister (reg_num);
664 row->SetCFAOffset (op_offset);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000665 }
666 break;
667
668 case DW_CFA_def_cfa_register : // 0xD (CFA Definition Instruction)
669 {
670 // takes a single unsigned LEB128 argument representing a register
671 // number. The required action is to define the current CFA rule to
672 // use the provided register (but to keep the old offset).
673 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
Jason Molenda1d42c7b2012-07-14 04:52:53 +0000674 row->SetCFARegister (reg_num);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000675 }
676 break;
677
678 case DW_CFA_def_cfa_offset : // 0xE (CFA Definition Instruction)
679 {
680 // Takes a single unsigned LEB128 operand representing a
681 // (non-factored) offset. The required action is to define
682 // the current CFA rule to use the provided offset (but
683 // to keep the old register).
684 op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
Jason Molenda1d42c7b2012-07-14 04:52:53 +0000685 row->SetCFAOffset (op_offset);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000686 }
687 break;
688
689 case DW_CFA_def_cfa_expression : // 0xF (CFA Definition Instruction)
690 {
691 size_t block_len = (size_t)m_cfi_data.GetULEB128(&offset);
692 offset += (uint32_t)block_len;
693 }
694 break;
695
696 case DW_CFA_expression : // 0x10
697 {
698 // Takes two operands: an unsigned LEB128 value representing
699 // a register number, and a DW_FORM_block value representing a DWARF
700 // expression. The required action is to change the rule for the
701 // register indicated by the register number to be an expression(E)
702 // rule where E is the DWARF expression. That is, the DWARF
703 // expression computes the address. The value of the CFA is
704 // pushed on the DWARF evaluation stack prior to execution of
705 // the DWARF expression.
706 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
707 uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
708 const uint8_t *block_data = (uint8_t *)m_cfi_data.GetData(&offset, block_len);
709
710 reg_location.SetAtDWARFExpression(block_data, block_len);
Jason Molenda1d42c7b2012-07-14 04:52:53 +0000711 row->SetRegisterInfo (reg_num, reg_location);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000712 }
713 break;
714
715 case DW_CFA_offset_extended_sf : // 0x11
716 {
717 // takes two operands: an unsigned LEB128 value representing a
718 // register number and a signed LEB128 factored offset. This
719 // instruction is identical to DW_CFA_offset_extended except
720 //that the second operand is signed and factored.
721 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
722 op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
723 reg_location.SetAtCFAPlusOffset(op_offset);
Jason Molenda1d42c7b2012-07-14 04:52:53 +0000724 row->SetRegisterInfo (reg_num, reg_location);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000725 }
726 break;
727
728 case DW_CFA_def_cfa_sf : // 0x12 (CFA Definition Instruction)
729 {
730 // Takes two operands: an unsigned LEB128 value representing
731 // a register number and a signed LEB128 factored offset.
732 // This instruction is identical to DW_CFA_def_cfa except
733 // that the second operand is signed and factored.
734 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
735 op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
Jason Molenda1d42c7b2012-07-14 04:52:53 +0000736 row->SetCFARegister (reg_num);
737 row->SetCFAOffset (op_offset);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000738 }
739 break;
740
741 case DW_CFA_def_cfa_offset_sf : // 0x13 (CFA Definition Instruction)
742 {
743 // takes a signed LEB128 operand representing a factored
744 // offset. This instruction is identical to DW_CFA_def_cfa_offset
745 // except that the operand is signed and factored.
746 op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
Jason Molenda1d42c7b2012-07-14 04:52:53 +0000747 row->SetCFAOffset (op_offset);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000748 }
749 break;
750
751 case DW_CFA_val_expression : // 0x16
752 {
753 // takes two operands: an unsigned LEB128 value representing a register
754 // number, and a DW_FORM_block value representing a DWARF expression.
755 // The required action is to change the rule for the register indicated
756 // by the register number to be a val_expression(E) rule where E is the
757 // DWARF expression. That is, the DWARF expression computes the value of
758 // the given register. The value of the CFA is pushed on the DWARF
759 // evaluation stack prior to execution of the DWARF expression.
760 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
761 uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
762 const uint8_t* block_data = (uint8_t*)m_cfi_data.GetData(&offset, block_len);
763//#if defined(__i386__) || defined(__x86_64__)
764// // The EH frame info for EIP and RIP contains code that looks for traps to
765// // be a specific type and increments the PC.
766// // For i386:
767// // DW_CFA_val_expression where:
768// // eip = DW_OP_breg6(+28), DW_OP_deref, DW_OP_dup, DW_OP_plus_uconst(0x34),
769// // DW_OP_deref, DW_OP_swap, DW_OP_plus_uconst(0), DW_OP_deref,
770// // DW_OP_dup, DW_OP_lit3, DW_OP_ne, DW_OP_swap, DW_OP_lit4, DW_OP_ne,
771// // DW_OP_and, DW_OP_plus
772// // This basically does a:
773// // eip = ucontenxt.mcontext32->gpr.eip;
774// // if (ucontenxt.mcontext32->exc.trapno != 3 && ucontenxt.mcontext32->exc.trapno != 4)
775// // eip++;
776// //
777// // For x86_64:
778// // DW_CFA_val_expression where:
779// // rip = DW_OP_breg3(+48), DW_OP_deref, DW_OP_dup, DW_OP_plus_uconst(0x90), DW_OP_deref,
780// // DW_OP_swap, DW_OP_plus_uconst(0), DW_OP_deref_size(4), DW_OP_dup, DW_OP_lit3,
781// // DW_OP_ne, DW_OP_swap, DW_OP_lit4, DW_OP_ne, DW_OP_and, DW_OP_plus
782// // This basically does a:
783// // rip = ucontenxt.mcontext64->gpr.rip;
784// // if (ucontenxt.mcontext64->exc.trapno != 3 && ucontenxt.mcontext64->exc.trapno != 4)
785// // rip++;
786// // The trap comparisons and increments are not needed as it hoses up the unwound PC which
787// // is expected to point at least past the instruction that causes the fault/trap. So we
788// // take it out by trimming the expression right at the first "DW_OP_swap" opcodes
789// if (block_data != NULL && thread->GetPCRegNum(Thread::GCC) == reg_num)
790// {
791// if (thread->Is64Bit())
792// {
793// if (block_len > 9 && block_data[8] == DW_OP_swap && block_data[9] == DW_OP_plus_uconst)
794// block_len = 8;
795// }
796// else
797// {
798// if (block_len > 8 && block_data[7] == DW_OP_swap && block_data[8] == DW_OP_plus_uconst)
799// block_len = 7;
800// }
801// }
802//#endif
803 reg_location.SetIsDWARFExpression(block_data, block_len);
Jason Molenda1d42c7b2012-07-14 04:52:53 +0000804 row->SetRegisterInfo (reg_num, reg_location);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000805 }
806 break;
807
808 case DW_CFA_val_offset : // 0x14
809 case DW_CFA_val_offset_sf : // 0x15
810 default:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000811 break;
812 }
813 }
814 }
Jason Molendafbcb7f22010-09-10 07:49:16 +0000815 unwind_plan.AppendRow(row);
816
817 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000818}