blob: ee2c68baa481e1c78c431d37a86d3ed0351df018 [file] [log] [blame]
Chris Lattner24943d22010-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 Molenda4e121942010-10-26 12:01:35 +000015#include "lldb/Core/Log.h"
Jason Molenda3a4ea242010-09-10 07:49:16 +000016#include "lldb/Core/Section.h"
Chris Lattner24943d22010-06-08 16:52:24 +000017#include "lldb/Symbol/DWARFCallFrameInfo.h"
18#include "lldb/Core/ArchSpec.h"
19#include "lldb/Core/Module.h"
20#include "lldb/Symbol/ObjectFile.h"
21#include "lldb/Target/RegisterContext.h"
22#include "lldb/Core/Section.h"
23#include "lldb/Target/Thread.h"
Jason Molenda3a4ea242010-09-10 07:49:16 +000024#include "lldb/Symbol/UnwindPlan.h"
Chris Lattner24943d22010-06-08 16:52:24 +000025
26using namespace lldb;
27using namespace lldb_private;
28
Jason Molenda3a4ea242010-09-10 07:49:16 +000029DWARFCallFrameInfo::DWARFCallFrameInfo(ObjectFile& objfile, SectionSP& section, uint32_t reg_kind, bool is_eh_frame) :
Chris Lattner24943d22010-06-08 16:52:24 +000030 m_objfile (objfile),
31 m_section (section),
Jason Molenda3a4ea242010-09-10 07:49:16 +000032 m_reg_kind (reg_kind), // The flavor of registers that the CFI data uses (enum RegisterKind)
Chris Lattner24943d22010-06-08 16:52:24 +000033 m_cie_map (),
Jason Molenda3a4ea242010-09-10 07:49:16 +000034 m_cfi_data (),
35 m_cfi_data_initialized (false),
36 m_fde_index (),
37 m_fde_index_initialized (false),
38 m_is_eh_frame (is_eh_frame)
Chris Lattner24943d22010-06-08 16:52:24 +000039{
Chris Lattner24943d22010-06-08 16:52:24 +000040}
41
42DWARFCallFrameInfo::~DWARFCallFrameInfo()
43{
44}
45
Jason Molenda3a4ea242010-09-10 07:49:16 +000046
Chris Lattner24943d22010-06-08 16:52:24 +000047bool
Jason Molenda3a4ea242010-09-10 07:49:16 +000048DWARFCallFrameInfo::GetAddressRange (Address addr, AddressRange &range)
Chris Lattner24943d22010-06-08 16:52:24 +000049{
Jason Molenda3a4ea242010-09-10 07:49:16 +000050 FDEEntry fde_entry;
51 if (GetFDEEntryByAddress (addr, fde_entry) == false)
52 return false;
53 range = fde_entry.bounds;
54 return true;
Chris Lattner24943d22010-06-08 16:52:24 +000055}
56
Jason Molenda3a4ea242010-09-10 07:49:16 +000057bool
58DWARFCallFrameInfo::GetUnwindPlan (Address addr, UnwindPlan& unwind_plan)
Chris Lattner24943d22010-06-08 16:52:24 +000059{
Jason Molenda3a4ea242010-09-10 07:49:16 +000060 FDEEntry fde_entry;
61 if (GetFDEEntryByAddress (addr, fde_entry) == false)
62 return false;
63 return FDEToUnwindPlan (fde_entry.offset, addr, unwind_plan);
Chris Lattner24943d22010-06-08 16:52:24 +000064}
65
Jason Molenda3a4ea242010-09-10 07:49:16 +000066bool
67DWARFCallFrameInfo::GetFDEEntryByAddress (Address addr, FDEEntry& fde_entry)
Chris Lattner24943d22010-06-08 16:52:24 +000068{
Jason Molenda3a4ea242010-09-10 07:49:16 +000069 if (m_section.get() == NULL)
70 return false;
71 GetFDEIndex();
72
73 struct FDEEntry searchfde;
74 searchfde.bounds = AddressRange (addr, 1);
75
76 std::vector<FDEEntry>::const_iterator idx;
77 if (m_fde_index.size() == 0)
78 return false;
79
80 idx = std::lower_bound (m_fde_index.begin(), m_fde_index.end(), searchfde);
81 if (idx == m_fde_index.end())
82 {
83 --idx;
84 }
85 if (idx != m_fde_index.begin() && idx->bounds.GetBaseAddress().GetOffset() != addr.GetOffset())
86 {
87 --idx;
88 }
89 if (idx->bounds.ContainsFileAddress (addr))
90 {
91 fde_entry = *idx;
92 return true;
93 }
94
95 return false;
Chris Lattner24943d22010-06-08 16:52:24 +000096}
97
Chris Lattner24943d22010-06-08 16:52:24 +000098const DWARFCallFrameInfo::CIE*
99DWARFCallFrameInfo::GetCIE(dw_offset_t cie_offset)
100{
Chris Lattner24943d22010-06-08 16:52:24 +0000101 cie_map_t::iterator pos = m_cie_map.find(cie_offset);
102
103 if (pos != m_cie_map.end())
104 {
105 // Parse and cache the CIE
106 if (pos->second.get() == NULL)
107 pos->second = ParseCIE (cie_offset);
108
109 return pos->second.get();
110 }
111 return NULL;
112}
113
Jason Molenda3a4ea242010-09-10 07:49:16 +0000114DWARFCallFrameInfo::CIESP
Chris Lattner24943d22010-06-08 16:52:24 +0000115DWARFCallFrameInfo::ParseCIE (const dw_offset_t cie_offset)
116{
Jason Molenda3a4ea242010-09-10 07:49:16 +0000117 CIESP cie_sp(new CIE(cie_offset));
Chris Lattner24943d22010-06-08 16:52:24 +0000118 dw_offset_t offset = cie_offset;
Jason Molenda3a4ea242010-09-10 07:49:16 +0000119 if (m_cfi_data_initialized == false)
120 {
121 m_section->ReadSectionDataFromObjectFile (&m_objfile, m_cfi_data);
122 m_cfi_data_initialized = true;
123 }
Chris Lattner24943d22010-06-08 16:52:24 +0000124 const uint32_t length = m_cfi_data.GetU32(&offset);
125 const dw_offset_t cie_id = m_cfi_data.GetU32(&offset);
126 const dw_offset_t end_offset = cie_offset + length + 4;
Jason Molenda3a4ea242010-09-10 07:49:16 +0000127 if (length > 0 && (!m_is_eh_frame && cie_id == 0xfffffffful) || (m_is_eh_frame && cie_id == 0ul))
Chris Lattner24943d22010-06-08 16:52:24 +0000128 {
129 size_t i;
130 // cie.offset = cie_offset;
131 // cie.length = length;
132 // cie.cieID = cieID;
Jason Molendaccfba722010-07-06 22:38:03 +0000133 cie_sp->ptr_encoding = DW_EH_PE_absptr;
Chris Lattner24943d22010-06-08 16:52:24 +0000134 cie_sp->version = m_cfi_data.GetU8(&offset);
135
136 for (i=0; i<CFI_AUG_MAX_SIZE; ++i)
137 {
138 cie_sp->augmentation[i] = m_cfi_data.GetU8(&offset);
139 if (cie_sp->augmentation[i] == '\0')
140 {
141 // Zero out remaining bytes in augmentation string
142 for (size_t j = i+1; j<CFI_AUG_MAX_SIZE; ++j)
143 cie_sp->augmentation[j] = '\0';
144
145 break;
146 }
147 }
148
149 if (i == CFI_AUG_MAX_SIZE && cie_sp->augmentation[CFI_AUG_MAX_SIZE-1] != '\0')
150 {
151 fprintf(stderr, "CIE parse error: CIE augmentation string was too large for the fixed sized buffer of %d bytes.\n", CFI_AUG_MAX_SIZE);
152 return cie_sp;
153 }
154 cie_sp->code_align = (uint32_t)m_cfi_data.GetULEB128(&offset);
155 cie_sp->data_align = (int32_t)m_cfi_data.GetSLEB128(&offset);
156 cie_sp->return_addr_reg_num = m_cfi_data.GetU8(&offset);
157
158 if (cie_sp->augmentation[0])
159 {
160 // Get the length of the eh_frame augmentation data
161 // which starts with a ULEB128 length in bytes
162 const size_t aug_data_len = (size_t)m_cfi_data.GetULEB128(&offset);
163 const size_t aug_data_end = offset + aug_data_len;
164 const size_t aug_str_len = strlen(cie_sp->augmentation);
165 // A 'z' may be present as the first character of the string.
166 // If present, the Augmentation Data field shall be present.
167 // The contents of the Augmentation Data shall be intepreted
168 // according to other characters in the Augmentation String.
169 if (cie_sp->augmentation[0] == 'z')
170 {
171 // Extract the Augmentation Data
172 size_t aug_str_idx = 0;
173 for (aug_str_idx = 1; aug_str_idx < aug_str_len; aug_str_idx++)
174 {
175 char aug = cie_sp->augmentation[aug_str_idx];
176 switch (aug)
177 {
178 case 'L':
179 // Indicates the presence of one argument in the
180 // Augmentation Data of the CIE, and a corresponding
181 // argument in the Augmentation Data of the FDE. The
182 // argument in the Augmentation Data of the CIE is
183 // 1-byte and represents the pointer encoding used
184 // for the argument in the Augmentation Data of the
185 // FDE, which is the address of a language-specific
186 // data area (LSDA). The size of the LSDA pointer is
187 // specified by the pointer encoding used.
188 m_cfi_data.GetU8(&offset);
189 break;
190
191 case 'P':
192 // Indicates the presence of two arguments in the
193 // Augmentation Data of the cie_sp-> The first argument
194 // is 1-byte and represents the pointer encoding
195 // used for the second argument, which is the
196 // address of a personality routine handler. The
197 // size of the personality routine pointer is
198 // specified by the pointer encoding used.
199 {
200 uint8_t arg_ptr_encoding = m_cfi_data.GetU8(&offset);
201 m_cfi_data.GetGNUEHPointer(&offset, arg_ptr_encoding, LLDB_INVALID_ADDRESS, LLDB_INVALID_ADDRESS, LLDB_INVALID_ADDRESS);
202 }
203 break;
204
205 case 'R':
206 // A 'R' may be present at any position after the
207 // first character of the string. The Augmentation
208 // Data shall include a 1 byte argument that
209 // represents the pointer encoding for the address
210 // pointers used in the FDE.
211 cie_sp->ptr_encoding = m_cfi_data.GetU8(&offset);
212 break;
213 }
214 }
215 }
216 else if (strcmp(cie_sp->augmentation, "eh") == 0)
217 {
218 // If the Augmentation string has the value "eh", then
219 // the EH Data field shall be present
220 }
221
222 // Set the offset to be the end of the augmentation data just in case
223 // we didn't understand any of the data.
224 offset = (uint32_t)aug_data_end;
225 }
226
227 if (end_offset > offset)
228 {
229 cie_sp->inst_offset = offset;
230 cie_sp->inst_length = end_offset - offset;
231 }
Jason Molenda3a4ea242010-09-10 07:49:16 +0000232 while (offset < end_offset)
233 {
234 uint8_t inst = m_cfi_data.GetU8(&offset);
235 uint8_t primary_opcode = inst & 0xC0;
236 uint8_t extended_opcode = inst & 0x3F;
237
238 if (extended_opcode == DW_CFA_def_cfa)
239 {
240 // Takes two unsigned LEB128 operands representing a register
241 // number and a (non-factored) offset. The required action
242 // is to define the current CFA rule to use the provided
243 // register and offset.
244 uint32_t reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
245 int op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
246 cie_sp->initial_row.SetCFARegister (reg_num);
247 cie_sp->initial_row.SetCFAOffset (op_offset);
248 continue;
249 }
250 if (primary_opcode == DW_CFA_offset)
251 {
252 // 0x80 - high 2 bits are 0x2, lower 6 bits are register.
253 // Takes two arguments: an unsigned LEB128 constant representing a
254 // factored offset and a register number. The required action is to
255 // change the rule for the register indicated by the register number
256 // to be an offset(N) rule with a value of
257 // (N = factored offset * data_align).
258 uint32_t reg_num = extended_opcode;
259 int op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * cie_sp->data_align;
260 UnwindPlan::Row::RegisterLocation reg_location;
261 reg_location.SetAtCFAPlusOffset(op_offset);
262 cie_sp->initial_row.SetRegisterInfo (reg_num, reg_location);
263 continue;
264 }
265 if (extended_opcode == DW_CFA_nop)
266 {
267 continue;
268 }
269 break; // Stop if we hit an unrecognized opcode
270 }
Chris Lattner24943d22010-06-08 16:52:24 +0000271 }
272
273 return cie_sp;
274}
275
Jason Molenda3a4ea242010-09-10 07:49:16 +0000276// Scan through the eh_frame or debug_frame section looking for FDEs and noting the start/end addresses
277// of the functions and a pointer back to the function's FDE for later expansion.
278// Internalize CIEs as we come across them.
Chris Lattner24943d22010-06-08 16:52:24 +0000279
Jason Molenda3a4ea242010-09-10 07:49:16 +0000280void
281DWARFCallFrameInfo::GetFDEIndex ()
282{
283 if (m_section.get() == NULL)
284 return;
285 if (m_fde_index_initialized)
286 return;
287
Jason Molenda4e121942010-10-26 12:01:35 +0000288
Jason Molenda3a4ea242010-09-10 07:49:16 +0000289 dw_offset_t offset = 0;
290 if (m_cfi_data_initialized == false)
291 {
Jason Molenda4e121942010-10-26 12:01:35 +0000292 Log *log = GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND);
293 if (log)
294 {
295 log->Printf ("Reading eh_frame information for %s", m_objfile.GetFileSpec().GetFilename().GetCString());
296 }
Jason Molenda3a4ea242010-09-10 07:49:16 +0000297 m_section->ReadSectionDataFromObjectFile (&m_objfile, m_cfi_data);
298 m_cfi_data_initialized = true;
299 }
300 while (m_cfi_data.ValidOffsetForDataOfSize (offset, 8))
301 {
302 dw_offset_t current_entry = offset;
303 uint32_t len = m_cfi_data.GetU32 (&offset);
304 dw_offset_t next_entry = current_entry + len + 4;
305 dw_offset_t cie_id = m_cfi_data.GetU32 (&offset);
306
307 if (cie_id == 0 || cie_id == UINT32_MAX)
308 {
309 m_cie_map[current_entry] = ParseCIE (current_entry);
310 offset = next_entry;
311 continue;
312 }
313
314 const CIE *cie = GetCIE (current_entry + 4 - cie_id);
315 assert (cie != NULL);
316
317 const lldb::addr_t pc_rel_addr = m_section->GetFileAddress();
318 const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS;
319 const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
320
321 lldb::addr_t addr = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding, pc_rel_addr, text_addr, data_addr);
322 lldb::addr_t length = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding & DW_EH_PE_MASK_ENCODING, pc_rel_addr, text_addr, data_addr);
323 FDEEntry fde;
324 fde.bounds = AddressRange (addr, length, m_objfile.GetSectionList());
325 fde.offset = current_entry;
326 m_fde_index.push_back(fde);
327
328 offset = next_entry;
329 }
330 std::sort (m_fde_index.begin(), m_fde_index.end());
331 m_fde_index_initialized = true;
332}
333
334bool
335DWARFCallFrameInfo::FDEToUnwindPlan (dw_offset_t offset, Address startaddr, UnwindPlan& unwind_plan)
336{
337 dw_offset_t current_entry = offset;
338
339 if (m_section.get() == NULL)
340 return false;
341
342 if (m_cfi_data_initialized == false)
343 {
344 m_section->ReadSectionDataFromObjectFile (&m_objfile, m_cfi_data);
345 m_cfi_data_initialized = true;
346 }
347
348 uint32_t length = m_cfi_data.GetU32 (&offset);
349 dw_offset_t cie_offset = m_cfi_data.GetU32 (&offset);
350
351 assert (cie_offset != 0 && cie_offset != UINT32_MAX);
Chris Lattner24943d22010-06-08 16:52:24 +0000352
353 // Translate the CIE_id from the eh_frame format, which
354 // is relative to the FDE offset, into a __eh_frame section
355 // offset
Jason Molenda3a4ea242010-09-10 07:49:16 +0000356 if (m_is_eh_frame)
Jason Molenda8280cbe2010-10-25 11:12:07 +0000357 {
358 unwind_plan.SetSourceName ("eh_frame CFI");
Jason Molenda3a4ea242010-09-10 07:49:16 +0000359 cie_offset = current_entry + 4 - cie_offset;
Jason Molenda8280cbe2010-10-25 11:12:07 +0000360 }
361 else
362 {
363 unwind_plan.SetSourceName ("DWARF CFI");
364 }
Chris Lattner24943d22010-06-08 16:52:24 +0000365
Jason Molenda3a4ea242010-09-10 07:49:16 +0000366 const CIE *cie = GetCIE (cie_offset);
367 assert (cie != NULL);
368
369 const dw_offset_t end_offset = current_entry + length + 4;
370
371 const lldb::addr_t pc_rel_addr = m_section->GetFileAddress();
372 const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS;
373 const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
374 lldb::addr_t range_base = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding, pc_rel_addr, text_addr, data_addr);
375 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);
376 AddressRange range (range_base, m_objfile.GetAddressByteSize(), m_objfile.GetSectionList());
377 range.SetByteSize (range_len);
378
379 if (cie->augmentation[0] == 'z')
Chris Lattner24943d22010-06-08 16:52:24 +0000380 {
Jason Molenda3a4ea242010-09-10 07:49:16 +0000381 uint32_t aug_data_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
382 offset += aug_data_len;
Chris Lattner24943d22010-06-08 16:52:24 +0000383 }
Chris Lattner24943d22010-06-08 16:52:24 +0000384
385 uint32_t reg_num = 0;
386 int32_t op_offset = 0;
387 uint32_t tmp_uval32;
388 uint32_t code_align = cie->code_align;
389 int32_t data_align = cie->data_align;
Chris Lattner24943d22010-06-08 16:52:24 +0000390
Jason Molenda3a4ea242010-09-10 07:49:16 +0000391 unwind_plan.SetPlanValidAddressRange (range);
392 UnwindPlan::Row row = cie->initial_row;
Chris Lattner24943d22010-06-08 16:52:24 +0000393
Jason Molenda3a4ea242010-09-10 07:49:16 +0000394 unwind_plan.SetRegisterKind (m_reg_kind);
395
396 UnwindPlan::Row::RegisterLocation reg_location;
Chris Lattner24943d22010-06-08 16:52:24 +0000397 while (m_cfi_data.ValidOffset(offset) && offset < end_offset)
398 {
399 uint8_t inst = m_cfi_data.GetU8(&offset);
400 uint8_t primary_opcode = inst & 0xC0;
401 uint8_t extended_opcode = inst & 0x3F;
402
403 if (primary_opcode)
404 {
405 switch (primary_opcode)
406 {
407 case DW_CFA_advance_loc : // (Row Creation Instruction)
408 { // 0x40 - high 2 bits are 0x1, lower 6 bits are delta
409 // takes a single argument that represents a constant delta. The
410 // required action is to create a new table row with a location
411 // value that is computed by taking the current entry's location
412 // value and adding (delta * code_align). All other
413 // values in the new row are initially identical to the current row.
Jason Molenda3a4ea242010-09-10 07:49:16 +0000414 unwind_plan.AppendRow(row);
Chris Lattner24943d22010-06-08 16:52:24 +0000415 row.SlideOffset(extended_opcode * code_align);
416 }
417 break;
418
419 case DW_CFA_offset :
420 { // 0x80 - high 2 bits are 0x2, lower 6 bits are register
421 // takes two arguments: an unsigned LEB128 constant representing a
422 // factored offset and a register number. The required action is to
423 // change the rule for the register indicated by the register number
424 // to be an offset(N) rule with a value of
425 // (N = factored offset * data_align).
426 reg_num = extended_opcode;
427 op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
428 reg_location.SetAtCFAPlusOffset(op_offset);
429 row.SetRegisterInfo (reg_num, reg_location);
430 }
431 break;
432
433 case DW_CFA_restore :
434 { // 0xC0 - high 2 bits are 0x3, lower 6 bits are register
435 // takes a single argument that represents a register number. The
436 // required action is to change the rule for the indicated register
437 // to the rule assigned it by the initial_instructions in the CIE.
438 reg_num = extended_opcode;
439 // We only keep enough register locations around to
440 // unwind what is in our thread, and these are organized
441 // by the register index in that state, so we need to convert our
Jason Molenda3a4ea242010-09-10 07:49:16 +0000442 // GCC register number from the EH frame info, to a register index
Chris Lattner24943d22010-06-08 16:52:24 +0000443
Jason Molenda3a4ea242010-09-10 07:49:16 +0000444 if (unwind_plan.IsValidRowIndex(0) && unwind_plan.GetRowAtIndex(0).GetRegisterInfo(reg_num, reg_location))
Chris Lattner24943d22010-06-08 16:52:24 +0000445 row.SetRegisterInfo (reg_num, reg_location);
446 }
447 break;
448 }
449 }
450 else
451 {
452 switch (extended_opcode)
453 {
454 case DW_CFA_nop : // 0x0
455 break;
456
457 case DW_CFA_set_loc : // 0x1 (Row Creation Instruction)
458 {
459 // DW_CFA_set_loc takes a single argument that represents an address.
460 // The required action is to create a new table row using the
461 // specified address as the location. All other values in the new row
462 // are initially identical to the current row. The new location value
463 // should always be greater than the current one.
Jason Molenda3a4ea242010-09-10 07:49:16 +0000464 unwind_plan.AppendRow(row);
465 row.SetOffset(m_cfi_data.GetPointer(&offset) - startaddr.GetFileAddress());
Chris Lattner24943d22010-06-08 16:52:24 +0000466 }
467 break;
468
469 case DW_CFA_advance_loc1 : // 0x2 (Row Creation Instruction)
470 {
471 // takes a single uword argument that represents a constant delta.
472 // This instruction is identical to DW_CFA_advance_loc except for the
473 // encoding and size of the delta argument.
Jason Molenda3a4ea242010-09-10 07:49:16 +0000474 unwind_plan.AppendRow(row);
Chris Lattner24943d22010-06-08 16:52:24 +0000475 row.SlideOffset (m_cfi_data.GetU8(&offset) * code_align);
476 }
477 break;
478
479 case DW_CFA_advance_loc2 : // 0x3 (Row Creation Instruction)
480 {
481 // takes a single uword argument that represents a constant delta.
482 // This instruction is identical to DW_CFA_advance_loc except for the
483 // encoding and size of the delta argument.
Jason Molenda3a4ea242010-09-10 07:49:16 +0000484 unwind_plan.AppendRow(row);
Chris Lattner24943d22010-06-08 16:52:24 +0000485 row.SlideOffset (m_cfi_data.GetU16(&offset) * code_align);
486 }
487 break;
488
489 case DW_CFA_advance_loc4 : // 0x4 (Row Creation Instruction)
490 {
491 // takes a single uword argument that represents a constant delta.
492 // This instruction is identical to DW_CFA_advance_loc except for the
493 // encoding and size of the delta argument.
Jason Molenda3a4ea242010-09-10 07:49:16 +0000494 unwind_plan.AppendRow(row);
Chris Lattner24943d22010-06-08 16:52:24 +0000495 row.SlideOffset (m_cfi_data.GetU32(&offset) * code_align);
496 }
497 break;
498
499 case DW_CFA_offset_extended : // 0x5
500 {
501 // takes two unsigned LEB128 arguments representing a register number
502 // and a factored offset. This instruction is identical to DW_CFA_offset
503 // except for the encoding and size of the register argument.
504 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
505 op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
506 reg_location.SetAtCFAPlusOffset(op_offset);
507 row.SetRegisterInfo (reg_num, reg_location);
508 }
509 break;
510
511 case DW_CFA_restore_extended : // 0x6
512 {
513 // takes a single unsigned LEB128 argument that represents a register
514 // number. This instruction is identical to DW_CFA_restore except for
515 // the encoding and size of the register argument.
516 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
Jason Molenda3a4ea242010-09-10 07:49:16 +0000517 if (unwind_plan.IsValidRowIndex(0) && unwind_plan.GetRowAtIndex(0).GetRegisterInfo(reg_num, reg_location))
Chris Lattner24943d22010-06-08 16:52:24 +0000518 row.SetRegisterInfo (reg_num, reg_location);
519 }
520 break;
521
522 case DW_CFA_undefined : // 0x7
523 {
524 // takes a single unsigned LEB128 argument that represents a register
525 // number. The required action is to set the rule for the specified
526 // register to undefined.
527 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
528 reg_location.SetUndefined();
529 row.SetRegisterInfo (reg_num, reg_location);
530 }
531 break;
532
533 case DW_CFA_same_value : // 0x8
534 {
535 // takes a single unsigned LEB128 argument that represents a register
536 // number. The required action is to set the rule for the specified
537 // register to same value.
538 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
539 reg_location.SetSame();
540 row.SetRegisterInfo (reg_num, reg_location);
541 }
542 break;
543
544 case DW_CFA_register : // 0x9
545 {
546 // takes two unsigned LEB128 arguments representing register numbers.
547 // The required action is to set the rule for the first register to be
548 // the second register.
549
550 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
551 uint32_t other_reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
552 reg_location.SetInRegister(other_reg_num);
553 row.SetRegisterInfo (reg_num, reg_location);
554 }
555 break;
556
557 case DW_CFA_remember_state : // 0xA
558 // These instructions define a stack of information. Encountering the
559 // DW_CFA_remember_state instruction means to save the rules for every
560 // register on the current row on the stack. Encountering the
561 // DW_CFA_restore_state instruction means to pop the set of rules off
562 // the stack and place them in the current row. (This operation is
563 // useful for compilers that move epilogue code into the body of a
564 // function.)
Jason Molenda3a4ea242010-09-10 07:49:16 +0000565 unwind_plan.AppendRow (row);
Chris Lattner24943d22010-06-08 16:52:24 +0000566 break;
567
568 case DW_CFA_restore_state : // 0xB
569 // These instructions define a stack of information. Encountering the
570 // DW_CFA_remember_state instruction means to save the rules for every
571 // register on the current row on the stack. Encountering the
572 // DW_CFA_restore_state instruction means to pop the set of rules off
573 // the stack and place them in the current row. (This operation is
574 // useful for compilers that move epilogue code into the body of a
575 // function.)
576 {
Jason Molenda3a4ea242010-09-10 07:49:16 +0000577 row = unwind_plan.GetRowAtIndex(unwind_plan.GetRowCount() - 1);
Chris Lattner24943d22010-06-08 16:52:24 +0000578 }
579 break;
580
581 case DW_CFA_def_cfa : // 0xC (CFA Definition Instruction)
582 {
583 // Takes two unsigned LEB128 operands representing a register
584 // number and a (non-factored) offset. The required action
585 // is to define the current CFA rule to use the provided
586 // register and offset.
587 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
588 op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
589 row.SetCFARegister (reg_num);
590 row.SetCFAOffset (op_offset);
591 }
592 break;
593
594 case DW_CFA_def_cfa_register : // 0xD (CFA Definition Instruction)
595 {
596 // takes a single unsigned LEB128 argument representing a register
597 // number. The required action is to define the current CFA rule to
598 // use the provided register (but to keep the old offset).
599 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
600 row.SetCFARegister (reg_num);
601 }
602 break;
603
604 case DW_CFA_def_cfa_offset : // 0xE (CFA Definition Instruction)
605 {
606 // Takes a single unsigned LEB128 operand representing a
607 // (non-factored) offset. The required action is to define
608 // the current CFA rule to use the provided offset (but
609 // to keep the old register).
610 op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
611 row.SetCFAOffset (op_offset);
612 }
613 break;
614
615 case DW_CFA_def_cfa_expression : // 0xF (CFA Definition Instruction)
616 {
617 size_t block_len = (size_t)m_cfi_data.GetULEB128(&offset);
618 offset += (uint32_t)block_len;
619 }
620 break;
621
622 case DW_CFA_expression : // 0x10
623 {
624 // Takes two operands: an unsigned LEB128 value representing
625 // a register number, and a DW_FORM_block value representing a DWARF
626 // expression. The required action is to change the rule for the
627 // register indicated by the register number to be an expression(E)
628 // rule where E is the DWARF expression. That is, the DWARF
629 // expression computes the address. The value of the CFA is
630 // pushed on the DWARF evaluation stack prior to execution of
631 // the DWARF expression.
632 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
633 uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
634 const uint8_t *block_data = (uint8_t *)m_cfi_data.GetData(&offset, block_len);
635
636 reg_location.SetAtDWARFExpression(block_data, block_len);
637 row.SetRegisterInfo (reg_num, reg_location);
638 }
639 break;
640
641 case DW_CFA_offset_extended_sf : // 0x11
642 {
643 // takes two operands: an unsigned LEB128 value representing a
644 // register number and a signed LEB128 factored offset. This
645 // instruction is identical to DW_CFA_offset_extended except
646 //that the second operand is signed and factored.
647 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
648 op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
649 reg_location.SetAtCFAPlusOffset(op_offset);
650 row.SetRegisterInfo (reg_num, reg_location);
651 }
652 break;
653
654 case DW_CFA_def_cfa_sf : // 0x12 (CFA Definition Instruction)
655 {
656 // Takes two operands: an unsigned LEB128 value representing
657 // a register number and a signed LEB128 factored offset.
658 // This instruction is identical to DW_CFA_def_cfa except
659 // that the second operand is signed and factored.
660 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
661 op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
662 row.SetCFARegister (reg_num);
663 row.SetCFAOffset (op_offset);
664 }
665 break;
666
667 case DW_CFA_def_cfa_offset_sf : // 0x13 (CFA Definition Instruction)
668 {
669 // takes a signed LEB128 operand representing a factored
670 // offset. This instruction is identical to DW_CFA_def_cfa_offset
671 // except that the operand is signed and factored.
672 op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
673 row.SetCFAOffset (op_offset);
674 }
675 break;
676
677 case DW_CFA_val_expression : // 0x16
678 {
679 // takes two operands: an unsigned LEB128 value representing a register
680 // number, and a DW_FORM_block value representing a DWARF expression.
681 // The required action is to change the rule for the register indicated
682 // by the register number to be a val_expression(E) rule where E is the
683 // DWARF expression. That is, the DWARF expression computes the value of
684 // the given register. The value of the CFA is pushed on the DWARF
685 // evaluation stack prior to execution of the DWARF expression.
686 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
687 uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
688 const uint8_t* block_data = (uint8_t*)m_cfi_data.GetData(&offset, block_len);
689//#if defined(__i386__) || defined(__x86_64__)
690// // The EH frame info for EIP and RIP contains code that looks for traps to
691// // be a specific type and increments the PC.
692// // For i386:
693// // DW_CFA_val_expression where:
694// // eip = DW_OP_breg6(+28), DW_OP_deref, DW_OP_dup, DW_OP_plus_uconst(0x34),
695// // DW_OP_deref, DW_OP_swap, DW_OP_plus_uconst(0), DW_OP_deref,
696// // DW_OP_dup, DW_OP_lit3, DW_OP_ne, DW_OP_swap, DW_OP_lit4, DW_OP_ne,
697// // DW_OP_and, DW_OP_plus
698// // This basically does a:
699// // eip = ucontenxt.mcontext32->gpr.eip;
700// // if (ucontenxt.mcontext32->exc.trapno != 3 && ucontenxt.mcontext32->exc.trapno != 4)
701// // eip++;
702// //
703// // For x86_64:
704// // DW_CFA_val_expression where:
705// // rip = DW_OP_breg3(+48), DW_OP_deref, DW_OP_dup, DW_OP_plus_uconst(0x90), DW_OP_deref,
706// // DW_OP_swap, DW_OP_plus_uconst(0), DW_OP_deref_size(4), DW_OP_dup, DW_OP_lit3,
707// // DW_OP_ne, DW_OP_swap, DW_OP_lit4, DW_OP_ne, DW_OP_and, DW_OP_plus
708// // This basically does a:
709// // rip = ucontenxt.mcontext64->gpr.rip;
710// // if (ucontenxt.mcontext64->exc.trapno != 3 && ucontenxt.mcontext64->exc.trapno != 4)
711// // rip++;
712// // The trap comparisons and increments are not needed as it hoses up the unwound PC which
713// // is expected to point at least past the instruction that causes the fault/trap. So we
714// // take it out by trimming the expression right at the first "DW_OP_swap" opcodes
715// if (block_data != NULL && thread->GetPCRegNum(Thread::GCC) == reg_num)
716// {
717// if (thread->Is64Bit())
718// {
719// if (block_len > 9 && block_data[8] == DW_OP_swap && block_data[9] == DW_OP_plus_uconst)
720// block_len = 8;
721// }
722// else
723// {
724// if (block_len > 8 && block_data[7] == DW_OP_swap && block_data[8] == DW_OP_plus_uconst)
725// block_len = 7;
726// }
727// }
728//#endif
729 reg_location.SetIsDWARFExpression(block_data, block_len);
730 row.SetRegisterInfo (reg_num, reg_location);
731 }
732 break;
733
734 case DW_CFA_val_offset : // 0x14
735 case DW_CFA_val_offset_sf : // 0x15
736 default:
737 tmp_uval32 = extended_opcode;
738 break;
739 }
740 }
741 }
Jason Molenda3a4ea242010-09-10 07:49:16 +0000742 unwind_plan.AppendRow(row);
743
744 return true;
Chris Lattner24943d22010-06-08 16:52:24 +0000745}