blob: b8e8cde7151a38777e3dff52970ca7e85e7b057c [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/Core/ArchSpec.h"
18#include "lldb/Core/Module.h"
Chris Lattner24943d22010-06-08 16:52:24 +000019#include "lldb/Core/Section.h"
Greg Claytondf6dc882012-01-05 03:57:59 +000020#include "lldb/Host/Host.h"
21#include "lldb/Symbol/DWARFCallFrameInfo.h"
22#include "lldb/Symbol/ObjectFile.h"
Jason Molenda3a4ea242010-09-10 07:49:16 +000023#include "lldb/Symbol/UnwindPlan.h"
Greg Claytondf6dc882012-01-05 03:57:59 +000024#include "lldb/Target/RegisterContext.h"
25#include "lldb/Target/Thread.h"
Chris Lattner24943d22010-06-08 16:52:24 +000026
27using namespace lldb;
28using namespace lldb_private;
29
Greg Claytonb5a8f142012-02-05 02:38:54 +000030DWARFCallFrameInfo::DWARFCallFrameInfo(ObjectFile& objfile, SectionSP& section_sp, lldb::RegisterKind reg_kind, bool is_eh_frame) :
Chris Lattner24943d22010-06-08 16:52:24 +000031 m_objfile (objfile),
Greg Claytonb5a8f142012-02-05 02:38:54 +000032 m_section_sp (section_sp),
Jason Molenda3a4ea242010-09-10 07:49:16 +000033 m_reg_kind (reg_kind), // The flavor of registers that the CFI data uses (enum RegisterKind)
Stephen Wilsondbeb3e12011-04-11 19:41:40 +000034 m_flags (),
Chris Lattner24943d22010-06-08 16:52:24 +000035 m_cie_map (),
Jason Molenda3a4ea242010-09-10 07:49:16 +000036 m_cfi_data (),
37 m_cfi_data_initialized (false),
38 m_fde_index (),
39 m_fde_index_initialized (false),
Stephen Wilsondbeb3e12011-04-11 19:41:40 +000040 m_is_eh_frame (is_eh_frame)
Chris Lattner24943d22010-06-08 16:52:24 +000041{
Chris Lattner24943d22010-06-08 16:52:24 +000042}
43
44DWARFCallFrameInfo::~DWARFCallFrameInfo()
45{
46}
47
Jason Molenda3a4ea242010-09-10 07:49:16 +000048
Chris Lattner24943d22010-06-08 16:52:24 +000049bool
Jason Molenda3a4ea242010-09-10 07:49:16 +000050DWARFCallFrameInfo::GetAddressRange (Address addr, AddressRange &range)
Chris Lattner24943d22010-06-08 16:52:24 +000051{
Jason Molenda3a4ea242010-09-10 07:49:16 +000052 FDEEntry fde_entry;
53 if (GetFDEEntryByAddress (addr, fde_entry) == false)
54 return false;
55 range = fde_entry.bounds;
56 return true;
Chris Lattner24943d22010-06-08 16:52:24 +000057}
58
Jason Molenda3a4ea242010-09-10 07:49:16 +000059bool
60DWARFCallFrameInfo::GetUnwindPlan (Address addr, UnwindPlan& unwind_plan)
Chris Lattner24943d22010-06-08 16:52:24 +000061{
Jason Molenda3a4ea242010-09-10 07:49:16 +000062 FDEEntry fde_entry;
63 if (GetFDEEntryByAddress (addr, fde_entry) == false)
64 return false;
65 return FDEToUnwindPlan (fde_entry.offset, addr, unwind_plan);
Chris Lattner24943d22010-06-08 16:52:24 +000066}
67
Jason Molenda3a4ea242010-09-10 07:49:16 +000068bool
69DWARFCallFrameInfo::GetFDEEntryByAddress (Address addr, FDEEntry& fde_entry)
Chris Lattner24943d22010-06-08 16:52:24 +000070{
Greg Claytonb5a8f142012-02-05 02:38:54 +000071 if (m_section_sp.get() == NULL || m_section_sp->IsEncrypted())
Jason Molenda3a4ea242010-09-10 07:49:16 +000072 return false;
73 GetFDEIndex();
74
75 struct FDEEntry searchfde;
76 searchfde.bounds = AddressRange (addr, 1);
77
78 std::vector<FDEEntry>::const_iterator idx;
79 if (m_fde_index.size() == 0)
80 return false;
81
82 idx = std::lower_bound (m_fde_index.begin(), m_fde_index.end(), searchfde);
83 if (idx == m_fde_index.end())
84 {
85 --idx;
86 }
Jason Molendadb599d32011-01-25 03:12:34 +000087 if (idx != m_fde_index.begin() && idx->bounds.GetBaseAddress().GetOffset() != addr.GetOffset())
Jason Molenda3a4ea242010-09-10 07:49:16 +000088 {
89 --idx;
90 }
91 if (idx->bounds.ContainsFileAddress (addr))
92 {
93 fde_entry = *idx;
94 return true;
95 }
96
97 return false;
Chris Lattner24943d22010-06-08 16:52:24 +000098}
99
Chris Lattner24943d22010-06-08 16:52:24 +0000100const DWARFCallFrameInfo::CIE*
101DWARFCallFrameInfo::GetCIE(dw_offset_t cie_offset)
102{
Chris Lattner24943d22010-06-08 16:52:24 +0000103 cie_map_t::iterator pos = m_cie_map.find(cie_offset);
104
105 if (pos != m_cie_map.end())
106 {
107 // Parse and cache the CIE
108 if (pos->second.get() == NULL)
109 pos->second = ParseCIE (cie_offset);
110
111 return pos->second.get();
112 }
113 return NULL;
114}
115
Jason Molenda3a4ea242010-09-10 07:49:16 +0000116DWARFCallFrameInfo::CIESP
Chris Lattner24943d22010-06-08 16:52:24 +0000117DWARFCallFrameInfo::ParseCIE (const dw_offset_t cie_offset)
118{
Jason Molenda3a4ea242010-09-10 07:49:16 +0000119 CIESP cie_sp(new CIE(cie_offset));
Chris Lattner24943d22010-06-08 16:52:24 +0000120 dw_offset_t offset = cie_offset;
Jason Molenda3a4ea242010-09-10 07:49:16 +0000121 if (m_cfi_data_initialized == false)
Greg Claytond2909b42011-12-29 00:05:26 +0000122 GetCFIData();
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;
Greg Claytonb3448432011-03-24 21:19:54 +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 {
Greg Claytondf6dc882012-01-05 03:57:59 +0000150 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 Lattner24943d22010-06-08 16:52:24 +0000151 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
Greg Claytond2909b42011-12-29 00:05:26 +0000275void
276DWARFCallFrameInfo::GetCFIData()
277{
278 if (m_cfi_data_initialized == false)
279 {
280 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
281 if (log)
282 m_objfile.GetModule()->LogMessage(log.get(), "Reading EH frame info");
Greg Claytonb5a8f142012-02-05 02:38:54 +0000283 m_objfile.ReadSectionData (m_section_sp.get(), m_cfi_data);
Greg Claytond2909b42011-12-29 00:05:26 +0000284 m_cfi_data_initialized = true;
285 }
286}
Jason Molenda3a4ea242010-09-10 07:49:16 +0000287// Scan through the eh_frame or debug_frame section looking for FDEs and noting the start/end addresses
288// of the functions and a pointer back to the function's FDE for later expansion.
289// Internalize CIEs as we come across them.
Chris Lattner24943d22010-06-08 16:52:24 +0000290
Jason Molenda3a4ea242010-09-10 07:49:16 +0000291void
292DWARFCallFrameInfo::GetFDEIndex ()
293{
Greg Claytonb5a8f142012-02-05 02:38:54 +0000294 if (m_section_sp.get() == NULL || m_section_sp->IsEncrypted())
Jason Molenda3a4ea242010-09-10 07:49:16 +0000295 return;
296 if (m_fde_index_initialized)
297 return;
298
Jason Molenda4e121942010-10-26 12:01:35 +0000299
Jason Molenda3a4ea242010-09-10 07:49:16 +0000300 dw_offset_t offset = 0;
301 if (m_cfi_data_initialized == false)
Greg Claytond2909b42011-12-29 00:05:26 +0000302 GetCFIData();
Jason Molenda3a4ea242010-09-10 07:49:16 +0000303 while (m_cfi_data.ValidOffsetForDataOfSize (offset, 8))
304 {
Greg Clayton63afdb02011-06-17 01:22:15 +0000305 const dw_offset_t current_entry = offset;
Jason Molenda3a4ea242010-09-10 07:49:16 +0000306 uint32_t len = m_cfi_data.GetU32 (&offset);
307 dw_offset_t next_entry = current_entry + len + 4;
308 dw_offset_t cie_id = m_cfi_data.GetU32 (&offset);
309
310 if (cie_id == 0 || cie_id == UINT32_MAX)
311 {
312 m_cie_map[current_entry] = ParseCIE (current_entry);
313 offset = next_entry;
314 continue;
315 }
316
Greg Clayton63afdb02011-06-17 01:22:15 +0000317 const dw_offset_t cie_offset = current_entry + 4 - cie_id;
318 const CIE *cie = GetCIE (cie_offset);
319 if (cie)
320 {
Greg Claytonb5a8f142012-02-05 02:38:54 +0000321 const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
Greg Clayton63afdb02011-06-17 01:22:15 +0000322 const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS;
323 const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
Jason Molenda3a4ea242010-09-10 07:49:16 +0000324
Greg Clayton63afdb02011-06-17 01:22:15 +0000325 lldb::addr_t addr = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding, pc_rel_addr, text_addr, data_addr);
326 lldb::addr_t length = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding & DW_EH_PE_MASK_ENCODING, pc_rel_addr, text_addr, data_addr);
327 FDEEntry fde;
328 fde.bounds = AddressRange (addr, length, m_objfile.GetSectionList());
329 fde.offset = current_entry;
330 m_fde_index.push_back(fde);
331 }
332 else
333 {
Greg Claytondf6dc882012-01-05 03:57:59 +0000334 Host::SystemLog (Host::eSystemLogError,
335 "error: unable to find CIE at 0x%8.8x for cie_id = 0x%8.8x for entry at 0x%8.8x.\n",
336 cie_offset,
337 cie_id,
338 current_entry);
Greg Clayton63afdb02011-06-17 01:22:15 +0000339 }
Jason Molenda3a4ea242010-09-10 07:49:16 +0000340 offset = next_entry;
341 }
342 std::sort (m_fde_index.begin(), m_fde_index.end());
343 m_fde_index_initialized = true;
344}
345
346bool
347DWARFCallFrameInfo::FDEToUnwindPlan (dw_offset_t offset, Address startaddr, UnwindPlan& unwind_plan)
348{
349 dw_offset_t current_entry = offset;
350
Greg Claytonb5a8f142012-02-05 02:38:54 +0000351 if (m_section_sp.get() == NULL || m_section_sp->IsEncrypted())
Jason Molenda3a4ea242010-09-10 07:49:16 +0000352 return false;
353
354 if (m_cfi_data_initialized == false)
Greg Claytond2909b42011-12-29 00:05:26 +0000355 GetCFIData();
Jason Molenda3a4ea242010-09-10 07:49:16 +0000356
357 uint32_t length = m_cfi_data.GetU32 (&offset);
358 dw_offset_t cie_offset = m_cfi_data.GetU32 (&offset);
359
360 assert (cie_offset != 0 && cie_offset != UINT32_MAX);
Chris Lattner24943d22010-06-08 16:52:24 +0000361
362 // Translate the CIE_id from the eh_frame format, which
363 // is relative to the FDE offset, into a __eh_frame section
364 // offset
Jason Molenda3a4ea242010-09-10 07:49:16 +0000365 if (m_is_eh_frame)
Jason Molenda8280cbe2010-10-25 11:12:07 +0000366 {
367 unwind_plan.SetSourceName ("eh_frame CFI");
Jason Molenda3a4ea242010-09-10 07:49:16 +0000368 cie_offset = current_entry + 4 - cie_offset;
Jason Molenda8280cbe2010-10-25 11:12:07 +0000369 }
370 else
371 {
372 unwind_plan.SetSourceName ("DWARF CFI");
373 }
Chris Lattner24943d22010-06-08 16:52:24 +0000374
Jason Molenda3a4ea242010-09-10 07:49:16 +0000375 const CIE *cie = GetCIE (cie_offset);
376 assert (cie != NULL);
377
378 const dw_offset_t end_offset = current_entry + length + 4;
379
Greg Claytonb5a8f142012-02-05 02:38:54 +0000380 const lldb::addr_t pc_rel_addr = m_section_sp->GetFileAddress();
Jason Molenda3a4ea242010-09-10 07:49:16 +0000381 const lldb::addr_t text_addr = LLDB_INVALID_ADDRESS;
382 const lldb::addr_t data_addr = LLDB_INVALID_ADDRESS;
383 lldb::addr_t range_base = m_cfi_data.GetGNUEHPointer(&offset, cie->ptr_encoding, pc_rel_addr, text_addr, data_addr);
384 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);
385 AddressRange range (range_base, m_objfile.GetAddressByteSize(), m_objfile.GetSectionList());
386 range.SetByteSize (range_len);
387
388 if (cie->augmentation[0] == 'z')
Chris Lattner24943d22010-06-08 16:52:24 +0000389 {
Jason Molenda3a4ea242010-09-10 07:49:16 +0000390 uint32_t aug_data_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
391 offset += aug_data_len;
Chris Lattner24943d22010-06-08 16:52:24 +0000392 }
Chris Lattner24943d22010-06-08 16:52:24 +0000393
394 uint32_t reg_num = 0;
395 int32_t op_offset = 0;
396 uint32_t tmp_uval32;
397 uint32_t code_align = cie->code_align;
398 int32_t data_align = cie->data_align;
Chris Lattner24943d22010-06-08 16:52:24 +0000399
Jason Molenda3a4ea242010-09-10 07:49:16 +0000400 unwind_plan.SetPlanValidAddressRange (range);
401 UnwindPlan::Row row = cie->initial_row;
Chris Lattner24943d22010-06-08 16:52:24 +0000402
Jason Molenda3a4ea242010-09-10 07:49:16 +0000403 unwind_plan.SetRegisterKind (m_reg_kind);
404
405 UnwindPlan::Row::RegisterLocation reg_location;
Chris Lattner24943d22010-06-08 16:52:24 +0000406 while (m_cfi_data.ValidOffset(offset) && offset < end_offset)
407 {
408 uint8_t inst = m_cfi_data.GetU8(&offset);
409 uint8_t primary_opcode = inst & 0xC0;
410 uint8_t extended_opcode = inst & 0x3F;
411
412 if (primary_opcode)
413 {
414 switch (primary_opcode)
415 {
416 case DW_CFA_advance_loc : // (Row Creation Instruction)
417 { // 0x40 - high 2 bits are 0x1, lower 6 bits are delta
418 // takes a single argument that represents a constant delta. The
419 // required action is to create a new table row with a location
420 // value that is computed by taking the current entry's location
421 // value and adding (delta * code_align). All other
422 // values in the new row are initially identical to the current row.
Jason Molenda3a4ea242010-09-10 07:49:16 +0000423 unwind_plan.AppendRow(row);
Chris Lattner24943d22010-06-08 16:52:24 +0000424 row.SlideOffset(extended_opcode * code_align);
425 }
426 break;
427
428 case DW_CFA_offset :
429 { // 0x80 - high 2 bits are 0x2, lower 6 bits are register
430 // takes two arguments: an unsigned LEB128 constant representing a
431 // factored offset and a register number. The required action is to
432 // change the rule for the register indicated by the register number
433 // to be an offset(N) rule with a value of
434 // (N = factored offset * data_align).
435 reg_num = extended_opcode;
436 op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
437 reg_location.SetAtCFAPlusOffset(op_offset);
438 row.SetRegisterInfo (reg_num, reg_location);
439 }
440 break;
441
442 case DW_CFA_restore :
443 { // 0xC0 - high 2 bits are 0x3, lower 6 bits are register
444 // takes a single argument that represents a register number. The
445 // required action is to change the rule for the indicated register
446 // to the rule assigned it by the initial_instructions in the CIE.
447 reg_num = extended_opcode;
448 // We only keep enough register locations around to
449 // unwind what is in our thread, and these are organized
450 // by the register index in that state, so we need to convert our
Jason Molenda3a4ea242010-09-10 07:49:16 +0000451 // GCC register number from the EH frame info, to a register index
Chris Lattner24943d22010-06-08 16:52:24 +0000452
Jason Molenda3a4ea242010-09-10 07:49:16 +0000453 if (unwind_plan.IsValidRowIndex(0) && unwind_plan.GetRowAtIndex(0).GetRegisterInfo(reg_num, reg_location))
Chris Lattner24943d22010-06-08 16:52:24 +0000454 row.SetRegisterInfo (reg_num, reg_location);
455 }
456 break;
457 }
458 }
459 else
460 {
461 switch (extended_opcode)
462 {
463 case DW_CFA_nop : // 0x0
464 break;
465
466 case DW_CFA_set_loc : // 0x1 (Row Creation Instruction)
467 {
468 // DW_CFA_set_loc takes a single argument that represents an address.
469 // The required action is to create a new table row using the
470 // specified address as the location. All other values in the new row
471 // are initially identical to the current row. The new location value
472 // should always be greater than the current one.
Jason Molenda3a4ea242010-09-10 07:49:16 +0000473 unwind_plan.AppendRow(row);
474 row.SetOffset(m_cfi_data.GetPointer(&offset) - startaddr.GetFileAddress());
Chris Lattner24943d22010-06-08 16:52:24 +0000475 }
476 break;
477
478 case DW_CFA_advance_loc1 : // 0x2 (Row Creation Instruction)
479 {
480 // takes a single uword argument that represents a constant delta.
481 // This instruction is identical to DW_CFA_advance_loc except for the
482 // encoding and size of the delta argument.
Jason Molenda3a4ea242010-09-10 07:49:16 +0000483 unwind_plan.AppendRow(row);
Chris Lattner24943d22010-06-08 16:52:24 +0000484 row.SlideOffset (m_cfi_data.GetU8(&offset) * code_align);
485 }
486 break;
487
488 case DW_CFA_advance_loc2 : // 0x3 (Row Creation Instruction)
489 {
490 // takes a single uword argument that represents a constant delta.
491 // This instruction is identical to DW_CFA_advance_loc except for the
492 // encoding and size of the delta argument.
Jason Molenda3a4ea242010-09-10 07:49:16 +0000493 unwind_plan.AppendRow(row);
Chris Lattner24943d22010-06-08 16:52:24 +0000494 row.SlideOffset (m_cfi_data.GetU16(&offset) * code_align);
495 }
496 break;
497
498 case DW_CFA_advance_loc4 : // 0x4 (Row Creation Instruction)
499 {
500 // takes a single uword argument that represents a constant delta.
501 // This instruction is identical to DW_CFA_advance_loc except for the
502 // encoding and size of the delta argument.
Jason Molenda3a4ea242010-09-10 07:49:16 +0000503 unwind_plan.AppendRow(row);
Chris Lattner24943d22010-06-08 16:52:24 +0000504 row.SlideOffset (m_cfi_data.GetU32(&offset) * code_align);
505 }
506 break;
507
508 case DW_CFA_offset_extended : // 0x5
509 {
510 // takes two unsigned LEB128 arguments representing a register number
511 // and a factored offset. This instruction is identical to DW_CFA_offset
512 // except for the encoding and size of the register argument.
513 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
514 op_offset = (int32_t)m_cfi_data.GetULEB128(&offset) * data_align;
515 reg_location.SetAtCFAPlusOffset(op_offset);
516 row.SetRegisterInfo (reg_num, reg_location);
517 }
518 break;
519
520 case DW_CFA_restore_extended : // 0x6
521 {
522 // takes a single unsigned LEB128 argument that represents a register
523 // number. This instruction is identical to DW_CFA_restore except for
524 // the encoding and size of the register argument.
525 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
Jason Molenda3a4ea242010-09-10 07:49:16 +0000526 if (unwind_plan.IsValidRowIndex(0) && unwind_plan.GetRowAtIndex(0).GetRegisterInfo(reg_num, reg_location))
Chris Lattner24943d22010-06-08 16:52:24 +0000527 row.SetRegisterInfo (reg_num, reg_location);
528 }
529 break;
530
531 case DW_CFA_undefined : // 0x7
532 {
533 // takes a single unsigned LEB128 argument that represents a register
534 // number. The required action is to set the rule for the specified
535 // register to undefined.
536 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
537 reg_location.SetUndefined();
538 row.SetRegisterInfo (reg_num, reg_location);
539 }
540 break;
541
542 case DW_CFA_same_value : // 0x8
543 {
544 // takes a single unsigned LEB128 argument that represents a register
545 // number. The required action is to set the rule for the specified
546 // register to same value.
547 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
548 reg_location.SetSame();
549 row.SetRegisterInfo (reg_num, reg_location);
550 }
551 break;
552
553 case DW_CFA_register : // 0x9
554 {
555 // takes two unsigned LEB128 arguments representing register numbers.
556 // The required action is to set the rule for the first register to be
557 // the second register.
558
559 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
560 uint32_t other_reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
561 reg_location.SetInRegister(other_reg_num);
562 row.SetRegisterInfo (reg_num, reg_location);
563 }
564 break;
565
566 case DW_CFA_remember_state : // 0xA
567 // These instructions define a stack of information. Encountering the
568 // DW_CFA_remember_state instruction means to save the rules for every
569 // register on the current row on the stack. Encountering the
570 // DW_CFA_restore_state instruction means to pop the set of rules off
571 // the stack and place them in the current row. (This operation is
572 // useful for compilers that move epilogue code into the body of a
573 // function.)
Jason Molenda3a4ea242010-09-10 07:49:16 +0000574 unwind_plan.AppendRow (row);
Chris Lattner24943d22010-06-08 16:52:24 +0000575 break;
576
577 case DW_CFA_restore_state : // 0xB
578 // These instructions define a stack of information. Encountering the
579 // DW_CFA_remember_state instruction means to save the rules for every
580 // register on the current row on the stack. Encountering the
581 // DW_CFA_restore_state instruction means to pop the set of rules off
582 // the stack and place them in the current row. (This operation is
583 // useful for compilers that move epilogue code into the body of a
584 // function.)
585 {
Jason Molenda3a4ea242010-09-10 07:49:16 +0000586 row = unwind_plan.GetRowAtIndex(unwind_plan.GetRowCount() - 1);
Chris Lattner24943d22010-06-08 16:52:24 +0000587 }
588 break;
589
590 case DW_CFA_def_cfa : // 0xC (CFA Definition Instruction)
591 {
592 // Takes two unsigned LEB128 operands representing a register
593 // number and a (non-factored) offset. The required action
594 // is to define the current CFA rule to use the provided
595 // register and offset.
596 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
597 op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
598 row.SetCFARegister (reg_num);
599 row.SetCFAOffset (op_offset);
600 }
601 break;
602
603 case DW_CFA_def_cfa_register : // 0xD (CFA Definition Instruction)
604 {
605 // takes a single unsigned LEB128 argument representing a register
606 // number. The required action is to define the current CFA rule to
607 // use the provided register (but to keep the old offset).
608 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
609 row.SetCFARegister (reg_num);
610 }
611 break;
612
613 case DW_CFA_def_cfa_offset : // 0xE (CFA Definition Instruction)
614 {
615 // Takes a single unsigned LEB128 operand representing a
616 // (non-factored) offset. The required action is to define
617 // the current CFA rule to use the provided offset (but
618 // to keep the old register).
619 op_offset = (int32_t)m_cfi_data.GetULEB128(&offset);
620 row.SetCFAOffset (op_offset);
621 }
622 break;
623
624 case DW_CFA_def_cfa_expression : // 0xF (CFA Definition Instruction)
625 {
626 size_t block_len = (size_t)m_cfi_data.GetULEB128(&offset);
627 offset += (uint32_t)block_len;
628 }
629 break;
630
631 case DW_CFA_expression : // 0x10
632 {
633 // Takes two operands: an unsigned LEB128 value representing
634 // a register number, and a DW_FORM_block value representing a DWARF
635 // expression. The required action is to change the rule for the
636 // register indicated by the register number to be an expression(E)
637 // rule where E is the DWARF expression. That is, the DWARF
638 // expression computes the address. The value of the CFA is
639 // pushed on the DWARF evaluation stack prior to execution of
640 // the DWARF expression.
641 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
642 uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
643 const uint8_t *block_data = (uint8_t *)m_cfi_data.GetData(&offset, block_len);
644
645 reg_location.SetAtDWARFExpression(block_data, block_len);
646 row.SetRegisterInfo (reg_num, reg_location);
647 }
648 break;
649
650 case DW_CFA_offset_extended_sf : // 0x11
651 {
652 // takes two operands: an unsigned LEB128 value representing a
653 // register number and a signed LEB128 factored offset. This
654 // instruction is identical to DW_CFA_offset_extended except
655 //that the second operand is signed and factored.
656 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
657 op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
658 reg_location.SetAtCFAPlusOffset(op_offset);
659 row.SetRegisterInfo (reg_num, reg_location);
660 }
661 break;
662
663 case DW_CFA_def_cfa_sf : // 0x12 (CFA Definition Instruction)
664 {
665 // Takes two operands: an unsigned LEB128 value representing
666 // a register number and a signed LEB128 factored offset.
667 // This instruction is identical to DW_CFA_def_cfa except
668 // that the second operand is signed and factored.
669 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
670 op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
671 row.SetCFARegister (reg_num);
672 row.SetCFAOffset (op_offset);
673 }
674 break;
675
676 case DW_CFA_def_cfa_offset_sf : // 0x13 (CFA Definition Instruction)
677 {
678 // takes a signed LEB128 operand representing a factored
679 // offset. This instruction is identical to DW_CFA_def_cfa_offset
680 // except that the operand is signed and factored.
681 op_offset = (int32_t)m_cfi_data.GetSLEB128(&offset) * data_align;
682 row.SetCFAOffset (op_offset);
683 }
684 break;
685
686 case DW_CFA_val_expression : // 0x16
687 {
688 // takes two operands: an unsigned LEB128 value representing a register
689 // number, and a DW_FORM_block value representing a DWARF expression.
690 // The required action is to change the rule for the register indicated
691 // by the register number to be a val_expression(E) rule where E is the
692 // DWARF expression. That is, the DWARF expression computes the value of
693 // the given register. The value of the CFA is pushed on the DWARF
694 // evaluation stack prior to execution of the DWARF expression.
695 reg_num = (uint32_t)m_cfi_data.GetULEB128(&offset);
696 uint32_t block_len = (uint32_t)m_cfi_data.GetULEB128(&offset);
697 const uint8_t* block_data = (uint8_t*)m_cfi_data.GetData(&offset, block_len);
698//#if defined(__i386__) || defined(__x86_64__)
699// // The EH frame info for EIP and RIP contains code that looks for traps to
700// // be a specific type and increments the PC.
701// // For i386:
702// // DW_CFA_val_expression where:
703// // eip = DW_OP_breg6(+28), DW_OP_deref, DW_OP_dup, DW_OP_plus_uconst(0x34),
704// // DW_OP_deref, DW_OP_swap, DW_OP_plus_uconst(0), DW_OP_deref,
705// // DW_OP_dup, DW_OP_lit3, DW_OP_ne, DW_OP_swap, DW_OP_lit4, DW_OP_ne,
706// // DW_OP_and, DW_OP_plus
707// // This basically does a:
708// // eip = ucontenxt.mcontext32->gpr.eip;
709// // if (ucontenxt.mcontext32->exc.trapno != 3 && ucontenxt.mcontext32->exc.trapno != 4)
710// // eip++;
711// //
712// // For x86_64:
713// // DW_CFA_val_expression where:
714// // rip = DW_OP_breg3(+48), DW_OP_deref, DW_OP_dup, DW_OP_plus_uconst(0x90), DW_OP_deref,
715// // DW_OP_swap, DW_OP_plus_uconst(0), DW_OP_deref_size(4), DW_OP_dup, DW_OP_lit3,
716// // DW_OP_ne, DW_OP_swap, DW_OP_lit4, DW_OP_ne, DW_OP_and, DW_OP_plus
717// // This basically does a:
718// // rip = ucontenxt.mcontext64->gpr.rip;
719// // if (ucontenxt.mcontext64->exc.trapno != 3 && ucontenxt.mcontext64->exc.trapno != 4)
720// // rip++;
721// // The trap comparisons and increments are not needed as it hoses up the unwound PC which
722// // is expected to point at least past the instruction that causes the fault/trap. So we
723// // take it out by trimming the expression right at the first "DW_OP_swap" opcodes
724// if (block_data != NULL && thread->GetPCRegNum(Thread::GCC) == reg_num)
725// {
726// if (thread->Is64Bit())
727// {
728// if (block_len > 9 && block_data[8] == DW_OP_swap && block_data[9] == DW_OP_plus_uconst)
729// block_len = 8;
730// }
731// else
732// {
733// if (block_len > 8 && block_data[7] == DW_OP_swap && block_data[8] == DW_OP_plus_uconst)
734// block_len = 7;
735// }
736// }
737//#endif
738 reg_location.SetIsDWARFExpression(block_data, block_len);
739 row.SetRegisterInfo (reg_num, reg_location);
740 }
741 break;
742
743 case DW_CFA_val_offset : // 0x14
744 case DW_CFA_val_offset_sf : // 0x15
745 default:
746 tmp_uval32 = extended_opcode;
747 break;
748 }
749 }
750 }
Jason Molenda3a4ea242010-09-10 07:49:16 +0000751 unwind_plan.AppendRow(row);
752
753 return true;
Chris Lattner24943d22010-06-08 16:52:24 +0000754}