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