blob: 9456a943ac2791177b5f68f40b9a6de0c1cb187b [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)
Jason Molenda8280cbe2010-10-25 11:12:07 +0000350 {
351 unwind_plan.SetSourceName ("eh_frame CFI");
Jason Molenda3a4ea242010-09-10 07:49:16 +0000352 cie_offset = current_entry + 4 - cie_offset;
Jason Molenda8280cbe2010-10-25 11:12:07 +0000353 }
354 else
355 {
356 unwind_plan.SetSourceName ("DWARF CFI");
357 }
Chris Lattner24943d22010-06-08 16:52:24 +0000358
Jason Molenda3a4ea242010-09-10 07:49:16 +0000359 const CIE *cie = GetCIE (cie_offset);
360 assert (cie != NULL);
361
362 const dw_offset_t end_offset = current_entry + length + 4;
363
364 const lldb::addr_t pc_rel_addr = m_section->GetFileAddress();
365 const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS;
366 const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
367 lldb::addr_t range_base = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding, pc_rel_addr, text_addr, data_addr);
368 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);
369 AddressRange range (range_base, m_objfile.GetAddressByteSize(), m_objfile.GetSectionList());
370 range.SetByteSize (range_len);
371
372 if (cie->augmentation[0] == 'z')
Chris Lattner24943d22010-06-08 16:52:24 +0000373 {
Jason Molenda3a4ea242010-09-10 07:49:16 +0000374 uint32_t aug_data_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
375 offset += aug_data_len;
Chris Lattner24943d22010-06-08 16:52:24 +0000376 }
Chris Lattner24943d22010-06-08 16:52:24 +0000377
378 uint32_t reg_num = 0;
379 int32_t op_offset = 0;
380 uint32_t tmp_uval32;
381 uint32_t code_align = cie->code_align;
382 int32_t data_align = cie->data_align;
Chris Lattner24943d22010-06-08 16:52:24 +0000383
Jason Molenda3a4ea242010-09-10 07:49:16 +0000384 unwind_plan.SetPlanValidAddressRange (range);
385 UnwindPlan::Row row = cie->initial_row;
Chris Lattner24943d22010-06-08 16:52:24 +0000386
Jason Molenda3a4ea242010-09-10 07:49:16 +0000387 unwind_plan.SetRegisterKind (m_reg_kind);
388
389 UnwindPlan::Row::RegisterLocation reg_location;
Chris Lattner24943d22010-06-08 16:52:24 +0000390 while (m_cfi_data.ValidOffset(offset) && offset < end_offset)
391 {
392 uint8_t inst = m_cfi_data.GetU8(&offset);
393 uint8_t primary_opcode = inst & 0xC0;
394 uint8_t extended_opcode = inst & 0x3F;
395
396 if (primary_opcode)
397 {
398 switch (primary_opcode)
399 {
400 case DW_CFA_advance_loc : // (Row Creation Instruction)
401 { // 0x40 - high 2 bits are 0x1, lower 6 bits are delta
402 // takes a single argument that represents a constant delta. The
403 // required action is to create a new table row with a location
404 // value that is computed by taking the current entry's location
405 // value and adding (delta * code_align). All other
406 // values in the new row are initially identical to the current row.
Jason Molenda3a4ea242010-09-10 07:49:16 +0000407 unwind_plan.AppendRow(row);
Chris Lattner24943d22010-06-08 16:52:24 +0000408 row.SlideOffset(extended_opcode * code_align);
409 }
410 break;
411
412 case DW_CFA_offset :
413 { // 0x80 - high 2 bits are 0x2, lower 6 bits are register
414 // takes two arguments: an unsigned LEB128 constant representing a
415 // factored offset and a register number. The required action is to
416 // change the rule for the register indicated by the register number
417 // to be an offset(N) rule with a value of
418 // (N = factored offset * data_align).
419 reg_num = extended_opcode;
420 op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
421 reg_location.SetAtCFAPlusOffset(op_offset);
422 row.SetRegisterInfo (reg_num, reg_location);
423 }
424 break;
425
426 case DW_CFA_restore :
427 { // 0xC0 - high 2 bits are 0x3, lower 6 bits are register
428 // takes a single argument that represents a register number. The
429 // required action is to change the rule for the indicated register
430 // to the rule assigned it by the initial_instructions in the CIE.
431 reg_num = extended_opcode;
432 // We only keep enough register locations around to
433 // unwind what is in our thread, and these are organized
434 // by the register index in that state, so we need to convert our
Jason Molenda3a4ea242010-09-10 07:49:16 +0000435 // GCC register number from the EH frame info, to a register index
Chris Lattner24943d22010-06-08 16:52:24 +0000436
Jason Molenda3a4ea242010-09-10 07:49:16 +0000437 if (unwind_plan.IsValidRowIndex(0) && unwind_plan.GetRowAtIndex(0).GetRegisterInfo(reg_num, reg_location))
Chris Lattner24943d22010-06-08 16:52:24 +0000438 row.SetRegisterInfo (reg_num, reg_location);
439 }
440 break;
441 }
442 }
443 else
444 {
445 switch (extended_opcode)
446 {
447 case DW_CFA_nop : // 0x0
448 break;
449
450 case DW_CFA_set_loc : // 0x1 (Row Creation Instruction)
451 {
452 // DW_CFA_set_loc takes a single argument that represents an address.
453 // The required action is to create a new table row using the
454 // specified address as the location. All other values in the new row
455 // are initially identical to the current row. The new location value
456 // should always be greater than the current one.
Jason Molenda3a4ea242010-09-10 07:49:16 +0000457 unwind_plan.AppendRow(row);
458 row.SetOffset(m_cfi_data.GetPointer(&offset) - startaddr.GetFileAddress());
Chris Lattner24943d22010-06-08 16:52:24 +0000459 }
460 break;
461
462 case DW_CFA_advance_loc1 : // 0x2 (Row Creation Instruction)
463 {
464 // takes a single uword argument that represents a constant delta.
465 // This instruction is identical to DW_CFA_advance_loc except for the
466 // encoding and size of the delta argument.
Jason Molenda3a4ea242010-09-10 07:49:16 +0000467 unwind_plan.AppendRow(row);
Chris Lattner24943d22010-06-08 16:52:24 +0000468 row.SlideOffset (m_cfi_data.GetU8(&offset) * code_align);
469 }
470 break;
471
472 case DW_CFA_advance_loc2 : // 0x3 (Row Creation Instruction)
473 {
474 // takes a single uword argument that represents a constant delta.
475 // This instruction is identical to DW_CFA_advance_loc except for the
476 // encoding and size of the delta argument.
Jason Molenda3a4ea242010-09-10 07:49:16 +0000477 unwind_plan.AppendRow(row);
Chris Lattner24943d22010-06-08 16:52:24 +0000478 row.SlideOffset (m_cfi_data.GetU16(&offset) * code_align);
479 }
480 break;
481
482 case DW_CFA_advance_loc4 : // 0x4 (Row Creation Instruction)
483 {
484 // takes a single uword argument that represents a constant delta.
485 // This instruction is identical to DW_CFA_advance_loc except for the
486 // encoding and size of the delta argument.
Jason Molenda3a4ea242010-09-10 07:49:16 +0000487 unwind_plan.AppendRow(row);
Chris Lattner24943d22010-06-08 16:52:24 +0000488 row.SlideOffset (m_cfi_data.GetU32(&offset) * code_align);
489 }
490 break;
491
492 case DW_CFA_offset_extended : // 0x5
493 {
494 // takes two unsigned LEB128 arguments representing a register number
495 // and a factored offset. This instruction is identical to DW_CFA_offset
496 // except for the encoding and size of the register argument.
497 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
498 op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
499 reg_location.SetAtCFAPlusOffset(op_offset);
500 row.SetRegisterInfo (reg_num, reg_location);
501 }
502 break;
503
504 case DW_CFA_restore_extended : // 0x6
505 {
506 // takes a single unsigned LEB128 argument that represents a register
507 // number. This instruction is identical to DW_CFA_restore except for
508 // the encoding and size of the register argument.
509 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
Jason Molenda3a4ea242010-09-10 07:49:16 +0000510 if (unwind_plan.IsValidRowIndex(0) && unwind_plan.GetRowAtIndex(0).GetRegisterInfo(reg_num, reg_location))
Chris Lattner24943d22010-06-08 16:52:24 +0000511 row.SetRegisterInfo (reg_num, reg_location);
512 }
513 break;
514
515 case DW_CFA_undefined : // 0x7
516 {
517 // takes a single unsigned LEB128 argument that represents a register
518 // number. The required action is to set the rule for the specified
519 // register to undefined.
520 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
521 reg_location.SetUndefined();
522 row.SetRegisterInfo (reg_num, reg_location);
523 }
524 break;
525
526 case DW_CFA_same_value : // 0x8
527 {
528 // takes a single unsigned LEB128 argument that represents a register
529 // number. The required action is to set the rule for the specified
530 // register to same value.
531 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
532 reg_location.SetSame();
533 row.SetRegisterInfo (reg_num, reg_location);
534 }
535 break;
536
537 case DW_CFA_register : // 0x9
538 {
539 // takes two unsigned LEB128 arguments representing register numbers.
540 // The required action is to set the rule for the first register to be
541 // the second register.
542
543 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
544 uint32_t other_reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
545 reg_location.SetInRegister(other_reg_num);
546 row.SetRegisterInfo (reg_num, reg_location);
547 }
548 break;
549
550 case DW_CFA_remember_state : // 0xA
551 // These instructions define a stack of information. Encountering the
552 // DW_CFA_remember_state instruction means to save the rules for every
553 // register on the current row on the stack. Encountering the
554 // DW_CFA_restore_state instruction means to pop the set of rules off
555 // the stack and place them in the current row. (This operation is
556 // useful for compilers that move epilogue code into the body of a
557 // function.)
Jason Molenda3a4ea242010-09-10 07:49:16 +0000558 unwind_plan.AppendRow (row);
Chris Lattner24943d22010-06-08 16:52:24 +0000559 break;
560
561 case DW_CFA_restore_state : // 0xB
562 // These instructions define a stack of information. Encountering the
563 // DW_CFA_remember_state instruction means to save the rules for every
564 // register on the current row on the stack. Encountering the
565 // DW_CFA_restore_state instruction means to pop the set of rules off
566 // the stack and place them in the current row. (This operation is
567 // useful for compilers that move epilogue code into the body of a
568 // function.)
569 {
Jason Molenda3a4ea242010-09-10 07:49:16 +0000570 row = unwind_plan.GetRowAtIndex(unwind_plan.GetRowCount() - 1);
Chris Lattner24943d22010-06-08 16:52:24 +0000571 }
572 break;
573
574 case DW_CFA_def_cfa : // 0xC (CFA Definition Instruction)
575 {
576 // Takes two unsigned LEB128 operands representing a register
577 // number and a (non-factored) offset. The required action
578 // is to define the current CFA rule to use the provided
579 // register and offset.
580 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
581 op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
582 row.SetCFARegister (reg_num);
583 row.SetCFAOffset (op_offset);
584 }
585 break;
586
587 case DW_CFA_def_cfa_register : // 0xD (CFA Definition Instruction)
588 {
589 // takes a single unsigned LEB128 argument representing a register
590 // number. The required action is to define the current CFA rule to
591 // use the provided register (but to keep the old offset).
592 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
593 row.SetCFARegister (reg_num);
594 }
595 break;
596
597 case DW_CFA_def_cfa_offset : // 0xE (CFA Definition Instruction)
598 {
599 // Takes a single unsigned LEB128 operand representing a
600 // (non-factored) offset. The required action is to define
601 // the current CFA rule to use the provided offset (but
602 // to keep the old register).
603 op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
604 row.SetCFAOffset (op_offset);
605 }
606 break;
607
608 case DW_CFA_def_cfa_expression : // 0xF (CFA Definition Instruction)
609 {
610 size_t block_len = (size_t)m_cfi_data.GetULEB128(&offset);
611 offset += (uint32_t)block_len;
612 }
613 break;
614
615 case DW_CFA_expression : // 0x10
616 {
617 // Takes two operands: an unsigned LEB128 value representing
618 // a register number, and a DW_FORM_block value representing a DWARF
619 // expression. The required action is to change the rule for the
620 // register indicated by the register number to be an expression(E)
621 // rule where E is the DWARF expression. That is, the DWARF
622 // expression computes the address. The value of the CFA is
623 // pushed on the DWARF evaluation stack prior to execution of
624 // the DWARF expression.
625 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
626 uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
627 const uint8_t *block_data = (uint8_t *)m_cfi_data.GetData(&offset, block_len);
628
629 reg_location.SetAtDWARFExpression(block_data, block_len);
630 row.SetRegisterInfo (reg_num, reg_location);
631 }
632 break;
633
634 case DW_CFA_offset_extended_sf : // 0x11
635 {
636 // takes two operands: an unsigned LEB128 value representing a
637 // register number and a signed LEB128 factored offset. This
638 // instruction is identical to DW_CFA_offset_extended except
639 //that the second operand is signed and factored.
640 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
641 op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
642 reg_location.SetAtCFAPlusOffset(op_offset);
643 row.SetRegisterInfo (reg_num, reg_location);
644 }
645 break;
646
647 case DW_CFA_def_cfa_sf : // 0x12 (CFA Definition Instruction)
648 {
649 // Takes two operands: an unsigned LEB128 value representing
650 // a register number and a signed LEB128 factored offset.
651 // This instruction is identical to DW_CFA_def_cfa except
652 // that the second operand is signed and factored.
653 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
654 op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
655 row.SetCFARegister (reg_num);
656 row.SetCFAOffset (op_offset);
657 }
658 break;
659
660 case DW_CFA_def_cfa_offset_sf : // 0x13 (CFA Definition Instruction)
661 {
662 // takes a signed LEB128 operand representing a factored
663 // offset. This instruction is identical to DW_CFA_def_cfa_offset
664 // except that the operand is signed and factored.
665 op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
666 row.SetCFAOffset (op_offset);
667 }
668 break;
669
670 case DW_CFA_val_expression : // 0x16
671 {
672 // takes two operands: an unsigned LEB128 value representing a register
673 // number, and a DW_FORM_block value representing a DWARF expression.
674 // The required action is to change the rule for the register indicated
675 // by the register number to be a val_expression(E) rule where E is the
676 // DWARF expression. That is, the DWARF expression computes the value of
677 // the given register. The value of the CFA is pushed on the DWARF
678 // evaluation stack prior to execution of the DWARF expression.
679 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
680 uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
681 const uint8_t* block_data = (uint8_t*)m_cfi_data.GetData(&offset, block_len);
682//#if defined(__i386__) || defined(__x86_64__)
683// // The EH frame info for EIP and RIP contains code that looks for traps to
684// // be a specific type and increments the PC.
685// // For i386:
686// // DW_CFA_val_expression where:
687// // eip = DW_OP_breg6(+28), DW_OP_deref, DW_OP_dup, DW_OP_plus_uconst(0x34),
688// // DW_OP_deref, DW_OP_swap, DW_OP_plus_uconst(0), DW_OP_deref,
689// // DW_OP_dup, DW_OP_lit3, DW_OP_ne, DW_OP_swap, DW_OP_lit4, DW_OP_ne,
690// // DW_OP_and, DW_OP_plus
691// // This basically does a:
692// // eip = ucontenxt.mcontext32->gpr.eip;
693// // if (ucontenxt.mcontext32->exc.trapno != 3 && ucontenxt.mcontext32->exc.trapno != 4)
694// // eip++;
695// //
696// // For x86_64:
697// // DW_CFA_val_expression where:
698// // rip = DW_OP_breg3(+48), DW_OP_deref, DW_OP_dup, DW_OP_plus_uconst(0x90), DW_OP_deref,
699// // DW_OP_swap, DW_OP_plus_uconst(0), DW_OP_deref_size(4), DW_OP_dup, DW_OP_lit3,
700// // DW_OP_ne, DW_OP_swap, DW_OP_lit4, DW_OP_ne, DW_OP_and, DW_OP_plus
701// // This basically does a:
702// // rip = ucontenxt.mcontext64->gpr.rip;
703// // if (ucontenxt.mcontext64->exc.trapno != 3 && ucontenxt.mcontext64->exc.trapno != 4)
704// // rip++;
705// // The trap comparisons and increments are not needed as it hoses up the unwound PC which
706// // is expected to point at least past the instruction that causes the fault/trap. So we
707// // take it out by trimming the expression right at the first "DW_OP_swap" opcodes
708// if (block_data != NULL && thread->GetPCRegNum(Thread::GCC) == reg_num)
709// {
710// if (thread->Is64Bit())
711// {
712// if (block_len > 9 && block_data[8] == DW_OP_swap && block_data[9] == DW_OP_plus_uconst)
713// block_len = 8;
714// }
715// else
716// {
717// if (block_len > 8 && block_data[7] == DW_OP_swap && block_data[8] == DW_OP_plus_uconst)
718// block_len = 7;
719// }
720// }
721//#endif
722 reg_location.SetIsDWARFExpression(block_data, block_len);
723 row.SetRegisterInfo (reg_num, reg_location);
724 }
725 break;
726
727 case DW_CFA_val_offset : // 0x14
728 case DW_CFA_val_offset_sf : // 0x15
729 default:
730 tmp_uval32 = extended_opcode;
731 break;
732 }
733 }
734 }
Jason Molenda3a4ea242010-09-10 07:49:16 +0000735 unwind_plan.AppendRow(row);
736
737 return true;
Chris Lattner24943d22010-06-08 16:52:24 +0000738}