blob: 09c0ac7083e5585de8513f79ed55deed9503fc83 [file] [log] [blame]
Jason Molendae589e7e2014-12-08 03:09:00 +00001//===-- CompactUnwindInfo.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 <algorithm>
14
Jason Molendae589e7e2014-12-08 03:09:00 +000015#include "lldb/Core/ArchSpec.h"
Jason Molendab12a1362014-12-20 03:12:51 +000016#include "lldb/Core/Log.h"
Jason Molendae589e7e2014-12-08 03:09:00 +000017#include "lldb/Core/Module.h"
18#include "lldb/Core/Section.h"
Jason Molendab12a1362014-12-20 03:12:51 +000019#include "lldb/Core/Section.h"
Jason Molendae589e7e2014-12-08 03:09:00 +000020#include "lldb/Symbol/CompactUnwindInfo.h"
21#include "lldb/Symbol/ObjectFile.h"
22#include "lldb/Symbol/UnwindPlan.h"
Jason Molendab12a1362014-12-20 03:12:51 +000023#include "lldb/Target/Process.h"
24#include "lldb/Target/Target.h"
Jason Molendae589e7e2014-12-08 03:09:00 +000025
Zachary Turner818a3672014-12-08 20:00:33 +000026#include "llvm/Support/MathExtras.h"
27
Jason Molendae589e7e2014-12-08 03:09:00 +000028using namespace lldb;
29using namespace lldb_private;
30
31
32namespace lldb_private {
33
34 // Constants from <mach-o/compact_unwind_encoding.h>
35
36 enum {
37 UNWIND_IS_NOT_FUNCTION_START = 0x80000000,
38 UNWIND_HAS_LSDA = 0x40000000,
39 UNWIND_PERSONALITY_MASK = 0x30000000,
40 };
41
42 enum {
43 UNWIND_X86_MODE_MASK = 0x0F000000,
44 UNWIND_X86_MODE_EBP_FRAME = 0x01000000,
45 UNWIND_X86_MODE_STACK_IMMD = 0x02000000,
46 UNWIND_X86_MODE_STACK_IND = 0x03000000,
47 UNWIND_X86_MODE_DWARF = 0x04000000,
48
49 UNWIND_X86_EBP_FRAME_REGISTERS = 0x00007FFF,
50 UNWIND_X86_EBP_FRAME_OFFSET = 0x00FF0000,
51
52 UNWIND_X86_FRAMELESS_STACK_SIZE = 0x00FF0000,
53 UNWIND_X86_FRAMELESS_STACK_ADJUST = 0x0000E000,
54 UNWIND_X86_FRAMELESS_STACK_REG_COUNT = 0x00001C00,
55 UNWIND_X86_FRAMELESS_STACK_REG_PERMUTATION = 0x000003FF,
56
57 UNWIND_X86_DWARF_SECTION_OFFSET = 0x00FFFFFF,
58 };
59
60 enum {
61 UNWIND_X86_REG_NONE = 0,
62 UNWIND_X86_REG_EBX = 1,
63 UNWIND_X86_REG_ECX = 2,
64 UNWIND_X86_REG_EDX = 3,
65 UNWIND_X86_REG_EDI = 4,
66 UNWIND_X86_REG_ESI = 5,
67 UNWIND_X86_REG_EBP = 6,
68 };
69 enum {
70 UNWIND_X86_64_MODE_MASK = 0x0F000000,
71 UNWIND_X86_64_MODE_RBP_FRAME = 0x01000000,
72 UNWIND_X86_64_MODE_STACK_IMMD = 0x02000000,
73 UNWIND_X86_64_MODE_STACK_IND = 0x03000000,
74 UNWIND_X86_64_MODE_DWARF = 0x04000000,
75
76 UNWIND_X86_64_RBP_FRAME_REGISTERS = 0x00007FFF,
77 UNWIND_X86_64_RBP_FRAME_OFFSET = 0x00FF0000,
78
79 UNWIND_X86_64_FRAMELESS_STACK_SIZE = 0x00FF0000,
80 UNWIND_X86_64_FRAMELESS_STACK_ADJUST = 0x0000E000,
81 UNWIND_X86_64_FRAMELESS_STACK_REG_COUNT = 0x00001C00,
82 UNWIND_X86_64_FRAMELESS_STACK_REG_PERMUTATION = 0x000003FF,
83
84 UNWIND_X86_64_DWARF_SECTION_OFFSET = 0x00FFFFFF,
85 };
86
87 enum {
88 UNWIND_X86_64_REG_NONE = 0,
89 UNWIND_X86_64_REG_RBX = 1,
90 UNWIND_X86_64_REG_R12 = 2,
91 UNWIND_X86_64_REG_R13 = 3,
92 UNWIND_X86_64_REG_R14 = 4,
93 UNWIND_X86_64_REG_R15 = 5,
94 UNWIND_X86_64_REG_RBP = 6,
95 };
96};
97
98
99#ifndef UNWIND_SECOND_LEVEL_REGULAR
100#define UNWIND_SECOND_LEVEL_REGULAR 2
101#endif
102
103#ifndef UNWIND_SECOND_LEVEL_COMPRESSED
104#define UNWIND_SECOND_LEVEL_COMPRESSED 3
105#endif
106
107#ifndef UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET
108#define UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET(entry) (entry & 0x00FFFFFF)
109#endif
110
111#ifndef UNWIND_INFO_COMPRESSED_ENTRY_ENCODING_INDEX
112#define UNWIND_INFO_COMPRESSED_ENTRY_ENCODING_INDEX(entry) ((entry >> 24) & 0xFF)
113#endif
114
115#define EXTRACT_BITS(value, mask) \
Zachary Turner818a3672014-12-08 20:00:33 +0000116 ( (value >> llvm::countTrailingZeros(static_cast<uint32_t>(mask), llvm::ZB_Width)) & \
117 (((1 << llvm::CountPopulation_32(static_cast<uint32_t>(mask))))-1) )
Jason Molendae589e7e2014-12-08 03:09:00 +0000118
119
120
121//----------------------
122// constructor
123//----------------------
124
125
126CompactUnwindInfo::CompactUnwindInfo(ObjectFile& objfile, SectionSP& section_sp) :
127 m_objfile (objfile),
128 m_section_sp (section_sp),
Jason Molendab12a1362014-12-20 03:12:51 +0000129 m_section_contents_if_encrypted (),
Jason Molendae589e7e2014-12-08 03:09:00 +0000130 m_mutex (),
131 m_indexes (),
132 m_indexes_computed (eLazyBoolCalculate),
133 m_unwindinfo_data (),
134 m_unwindinfo_data_computed (false),
135 m_unwind_header ()
136{
137
138}
139
140//----------------------
141// destructor
142//----------------------
143
144CompactUnwindInfo::~CompactUnwindInfo()
145{
146}
147
148bool
149CompactUnwindInfo::GetUnwindPlan (Target &target, Address addr, UnwindPlan& unwind_plan)
150{
Jason Molendab12a1362014-12-20 03:12:51 +0000151 if (!IsValid (target.GetProcessSP()))
Jason Molendae589e7e2014-12-08 03:09:00 +0000152 {
153 return false;
154 }
155 FunctionInfo function_info;
156 if (GetCompactUnwindInfoForFunction (target, addr, function_info))
157 {
158 // shortcut return for functions that have no compact unwind
159 if (function_info.encoding == 0)
160 return false;
161
162 ArchSpec arch;
163 if (m_objfile.GetArchitecture (arch))
164 {
165 if (arch.GetTriple().getArch() == llvm::Triple::x86_64)
166 {
167 return CreateUnwindPlan_x86_64 (target, function_info, unwind_plan, addr);
168 }
169 if (arch.GetTriple().getArch() == llvm::Triple::x86)
170 {
171 return CreateUnwindPlan_i386 (target, function_info, unwind_plan, addr);
172 }
173 }
174 }
175 return false;
176}
177
178bool
Jason Molendab12a1362014-12-20 03:12:51 +0000179CompactUnwindInfo::IsValid (const ProcessSP &process_sp)
Jason Molendae589e7e2014-12-08 03:09:00 +0000180{
Jason Molendab12a1362014-12-20 03:12:51 +0000181 if (m_section_sp.get() == nullptr)
Jason Molendae589e7e2014-12-08 03:09:00 +0000182 return false;
183
184 if (m_indexes_computed == eLazyBoolYes && m_unwindinfo_data_computed)
185 return true;
186
Jason Molendab12a1362014-12-20 03:12:51 +0000187 ScanIndex (process_sp);
Jason Molendae589e7e2014-12-08 03:09:00 +0000188
189 return m_indexes_computed == eLazyBoolYes && m_unwindinfo_data_computed;
190}
191
192void
Jason Molendab12a1362014-12-20 03:12:51 +0000193CompactUnwindInfo::ScanIndex (const ProcessSP &process_sp)
Jason Molendae589e7e2014-12-08 03:09:00 +0000194{
195 Mutex::Locker locker(m_mutex);
196 if (m_indexes_computed == eLazyBoolYes && m_unwindinfo_data_computed)
197 return;
198
199 // We can't read the index for some reason.
200 if (m_indexes_computed == eLazyBoolNo)
201 {
202 return;
203 }
204
205 if (m_unwindinfo_data_computed == false)
206 {
Jason Molendab12a1362014-12-20 03:12:51 +0000207 if (m_section_sp->IsEncrypted())
208 {
209 // Can't get section contents of a protected/encrypted section until we have a live
210 // process and can read them out of memory.
211 if (process_sp.get() == nullptr)
212 return;
213 m_section_contents_if_encrypted.reset (new DataBufferHeap (m_section_sp->GetByteSize(), 0));
214 Error error;
215 if (process_sp->ReadMemory (
216 m_section_sp->GetLoadBaseAddress (&process_sp->GetTarget()),
217 m_section_contents_if_encrypted->GetBytes(),
218 m_section_sp->GetByteSize(), error) == m_section_sp->GetByteSize() && error.Success())
219 {
220 m_unwindinfo_data.SetAddressByteSize (process_sp->GetTarget().GetArchitecture().GetAddressByteSize());
221 m_unwindinfo_data.SetByteOrder (process_sp->GetTarget().GetArchitecture().GetByteOrder());
222 m_unwindinfo_data.SetData (m_section_contents_if_encrypted, 0);
223 }
224 }
225 else
226 {
227 m_objfile.ReadSectionData (m_section_sp.get(), m_unwindinfo_data);
228 }
229 if (m_unwindinfo_data.GetByteSize() != m_section_sp->GetByteSize())
230 return;
Jason Molendae589e7e2014-12-08 03:09:00 +0000231 m_unwindinfo_data_computed = true;
232 }
233
234 if (m_unwindinfo_data.GetByteSize() > 0)
235 {
236 offset_t offset = 0;
237
238 // struct unwind_info_section_header
239 // {
240 // uint32_t version; // UNWIND_SECTION_VERSION
241 // uint32_t commonEncodingsArraySectionOffset;
242 // uint32_t commonEncodingsArrayCount;
243 // uint32_t personalityArraySectionOffset;
244 // uint32_t personalityArrayCount;
245 // uint32_t indexSectionOffset;
246 // uint32_t indexCount;
247
248 m_unwind_header.version = m_unwindinfo_data.GetU32(&offset);
249 m_unwind_header.common_encodings_array_offset = m_unwindinfo_data.GetU32(&offset);
250 m_unwind_header.common_encodings_array_count = m_unwindinfo_data.GetU32(&offset);
251 m_unwind_header.personality_array_offset = m_unwindinfo_data.GetU32(&offset);
252 m_unwind_header.personality_array_count = m_unwindinfo_data.GetU32(&offset);
253 uint32_t indexSectionOffset = m_unwindinfo_data.GetU32(&offset);
254
255 uint32_t indexCount = m_unwindinfo_data.GetU32(&offset);
256
257 if (m_unwind_header.version != 1)
258 {
259 m_indexes_computed = eLazyBoolNo;
260 }
261
262 // Parse the basic information from the indexes
263 // We wait to scan the second level page info until it's needed
264
265 // struct unwind_info_section_header_index_entry
266 // {
267 // uint32_t functionOffset;
268 // uint32_t secondLevelPagesSectionOffset;
269 // uint32_t lsdaIndexArraySectionOffset;
270 // };
271
272 offset = indexSectionOffset;
273 for (int idx = 0; idx < indexCount; idx++)
274 {
275 uint32_t function_offset = m_unwindinfo_data.GetU32(&offset); // functionOffset
276 uint32_t second_level_offset = m_unwindinfo_data.GetU32(&offset); // secondLevelPagesSectionOffset
277 uint32_t lsda_offset = m_unwindinfo_data.GetU32(&offset); // lsdaIndexArraySectionOffset
278
279 if (second_level_offset > m_section_sp->GetByteSize() || lsda_offset > m_section_sp->GetByteSize())
280 {
281 m_indexes_computed = eLazyBoolNo;
282 }
283
284 UnwindIndex this_index;
285 this_index.function_offset = function_offset; //
286 this_index.second_level = second_level_offset;
287 this_index.lsda_array_start = lsda_offset;
288
289 if (m_indexes.size() > 0)
290 {
291 m_indexes[m_indexes.size() - 1].lsda_array_end = lsda_offset;
292 }
293
294 if (second_level_offset == 0)
295 {
296 this_index.sentinal_entry = true;
297 }
298
299 m_indexes.push_back (this_index);
300 }
301 m_indexes_computed = eLazyBoolYes;
302 }
303 else
304 {
305 m_indexes_computed = eLazyBoolNo;
306 }
307}
308
309uint32_t
310CompactUnwindInfo::GetLSDAForFunctionOffset (uint32_t lsda_offset, uint32_t lsda_count, uint32_t function_offset)
311{
312 // struct unwind_info_section_header_lsda_index_entry
313 // {
314 // uint32_t functionOffset;
315 // uint32_t lsdaOffset;
316 // };
317
318 offset_t first_entry = lsda_offset;
319 uint32_t low = 0;
320 uint32_t high = lsda_count;
321 while (low < high)
322 {
323 uint32_t mid = (low + high) / 2;
324 offset_t offset = first_entry + (mid * 8);
325 uint32_t mid_func_offset = m_unwindinfo_data.GetU32(&offset); // functionOffset
326 uint32_t mid_lsda_offset = m_unwindinfo_data.GetU32(&offset); // lsdaOffset
327 if (mid_func_offset == function_offset)
328 {
329 return mid_lsda_offset;
330 }
331 if (mid_func_offset < function_offset)
332 {
333 low = mid + 1;
334 }
335 else
336 {
337 high = mid;
338 }
339 }
340 return 0;
341}
342
343lldb::offset_t
344CompactUnwindInfo::BinarySearchRegularSecondPage (uint32_t entry_page_offset, uint32_t entry_count, uint32_t function_offset)
345{
346 // typedef uint32_t compact_unwind_encoding_t;
347 // struct unwind_info_regular_second_level_entry
348 // {
349 // uint32_t functionOffset;
350 // compact_unwind_encoding_t encoding;
351
352 offset_t first_entry = entry_page_offset;
353
354 uint32_t low = 0;
355 uint32_t high = entry_count;
356 uint32_t last = high - 1;
357 while (low < high)
358 {
359 uint32_t mid = (low + high) / 2;
360 offset_t offset = first_entry + (mid * 8);
361 uint32_t mid_func_offset = m_unwindinfo_data.GetU32(&offset); // functionOffset
362 uint32_t next_func_offset = 0;
363 if (mid < last)
364 {
365 offset = first_entry + ((mid + 1) * 8);
366 next_func_offset = m_unwindinfo_data.GetU32(&offset); // functionOffset
367 }
368 if (mid_func_offset <= function_offset)
369 {
370 if (mid == last || (next_func_offset > function_offset))
371 {
372 return first_entry + (mid * 8);
373 }
374 else
375 {
376 low = mid + 1;
377 }
378 }
379 else
380 {
381 high = mid;
382 }
383 }
384 return LLDB_INVALID_OFFSET;
385}
386
387uint32_t
388CompactUnwindInfo::BinarySearchCompressedSecondPage (uint32_t entry_page_offset, uint32_t entry_count, uint32_t function_offset_to_find, uint32_t function_offset_base)
389{
390 offset_t first_entry = entry_page_offset;
391
392 uint32_t low = 0;
393 uint32_t high = entry_count;
394 uint32_t last = high - 1;
395 while (low < high)
396 {
397 uint32_t mid = (low + high) / 2;
398 offset_t offset = first_entry + (mid * 4);
399 uint32_t entry = m_unwindinfo_data.GetU32(&offset); // entry
400 uint32_t mid_func_offset = UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET (entry);
401 mid_func_offset += function_offset_base;
402 uint32_t next_func_offset = 0;
403 if (mid < last)
404 {
405 offset = first_entry + ((mid + 1) * 4);
406 uint32_t next_entry = m_unwindinfo_data.GetU32(&offset); // entry
407 next_func_offset = UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET (next_entry);
408 next_func_offset += function_offset_base;
409 }
410 if (mid_func_offset <= function_offset_to_find)
411 {
412 if (mid == last || (next_func_offset > function_offset_to_find))
413 {
414 return UNWIND_INFO_COMPRESSED_ENTRY_ENCODING_INDEX (entry);
415 }
416 else
417 {
418 low = mid + 1;
419 }
420 }
421 else
422 {
423 high = mid;
424 }
425 }
426
427 return UINT32_MAX;
428}
429
430
431bool
432CompactUnwindInfo::GetCompactUnwindInfoForFunction (Target &target, Address address, FunctionInfo &unwind_info)
433{
434 unwind_info.encoding = 0;
435 unwind_info.lsda_address.Clear();
436 unwind_info.personality_ptr_address.Clear();
437
Jason Molendab12a1362014-12-20 03:12:51 +0000438 if (!IsValid (target.GetProcessSP()))
Jason Molendae589e7e2014-12-08 03:09:00 +0000439 return false;
440
Jason Molendae589e7e2014-12-08 03:09:00 +0000441 addr_t text_section_file_address = LLDB_INVALID_ADDRESS;
442 SectionList *sl = m_objfile.GetSectionList ();
443 if (sl)
444 {
445 SectionSP text_sect = sl->FindSectionByType (eSectionTypeCode, true);
446 if (text_sect.get())
447 {
448 text_section_file_address = text_sect->GetFileAddress();
449 }
450 }
451 if (text_section_file_address == LLDB_INVALID_ADDRESS)
452 return false;
453
454 addr_t function_offset = address.GetFileAddress() - m_objfile.GetHeaderAddress().GetFileAddress();
455
456 UnwindIndex key;
457 key.function_offset = function_offset;
458
459 std::vector<UnwindIndex>::const_iterator it;
460 it = std::lower_bound (m_indexes.begin(), m_indexes.end(), key);
461 if (it == m_indexes.end())
462 {
463 return false;
464 }
465
466 if (it->function_offset != key.function_offset)
467 {
468 if (it != m_indexes.begin())
469 --it;
470 }
471
472 if (it->sentinal_entry == true)
473 {
474 return false;
475 }
476
477 offset_t second_page_offset = it->second_level;
478 offset_t lsda_array_start = it->lsda_array_start;
479 offset_t lsda_array_count = (it->lsda_array_end - it->lsda_array_start) / 8;
480
481 offset_t offset = second_page_offset;
482 uint32_t kind = m_unwindinfo_data.GetU32(&offset); // UNWIND_SECOND_LEVEL_REGULAR or UNWIND_SECOND_LEVEL_COMPRESSED
483
484 if (kind == UNWIND_SECOND_LEVEL_REGULAR)
485 {
486 // struct unwind_info_regular_second_level_page_header
487 // {
488 // uint32_t kind; // UNWIND_SECOND_LEVEL_REGULAR
489 // uint16_t entryPageOffset;
490 // uint16_t entryCount;
491
492 // typedef uint32_t compact_unwind_encoding_t;
493 // struct unwind_info_regular_second_level_entry
494 // {
495 // uint32_t functionOffset;
496 // compact_unwind_encoding_t encoding;
497
498 uint16_t entry_page_offset = m_unwindinfo_data.GetU16(&offset); // entryPageOffset
499 uint16_t entry_count = m_unwindinfo_data.GetU16(&offset); // entryCount
500
501 offset_t entry_offset = BinarySearchRegularSecondPage (second_page_offset + entry_page_offset, entry_count, function_offset);
502 if (entry_offset == LLDB_INVALID_OFFSET)
503 {
504 return false;
505 }
506 entry_offset += 4; // skip over functionOffset
507 unwind_info.encoding = m_unwindinfo_data.GetU32(&entry_offset); // encoding
508 if (unwind_info.encoding & UNWIND_HAS_LSDA)
509 {
510 SectionList *sl = m_objfile.GetSectionList ();
511 if (sl)
512 {
513 uint32_t lsda_offset = GetLSDAForFunctionOffset (lsda_array_start, lsda_array_count, function_offset);
514 addr_t objfile_header_file_address = m_objfile.GetHeaderAddress().GetFileAddress();
515 unwind_info.lsda_address.ResolveAddressUsingFileSections (objfile_header_file_address + lsda_offset, sl);
516 }
517 }
518 if (unwind_info.encoding & UNWIND_PERSONALITY_MASK)
519 {
520 uint32_t personality_index = EXTRACT_BITS (unwind_info.encoding, UNWIND_PERSONALITY_MASK);
521
522 if (personality_index > 0)
523 {
524 personality_index--;
525 if (personality_index < m_unwind_header.personality_array_count)
526 {
527 offset_t offset = m_unwind_header.personality_array_offset;
528 offset += 4 * personality_index;
529 SectionList *sl = m_objfile.GetSectionList ();
530 if (sl)
531 {
532 uint32_t personality_offset = m_unwindinfo_data.GetU32(&offset);
533 addr_t objfile_header_file_address = m_objfile.GetHeaderAddress().GetFileAddress();
534 unwind_info.personality_ptr_address.ResolveAddressUsingFileSections (objfile_header_file_address + personality_offset, sl);
535 }
536 }
537 }
538 }
539 return true;
540 }
541 else if (kind == UNWIND_SECOND_LEVEL_COMPRESSED)
542 {
543 // struct unwind_info_compressed_second_level_page_header
544 // {
545 // uint32_t kind; // UNWIND_SECOND_LEVEL_COMPRESSED
546 // uint16_t entryPageOffset; // offset from this 2nd lvl page idx to array of entries
547 // // (an entry has a function offset and index into the encodings)
548 // // NB function offset from the entry in the compressed page
549 // // must be added to the index's functionOffset value.
550 // uint16_t entryCount;
551 // uint16_t encodingsPageOffset; // offset from this 2nd lvl page idx to array of encodings
552 // uint16_t encodingsCount;
553
554 uint16_t entry_page_offset = m_unwindinfo_data.GetU16(&offset); // entryPageOffset
555 uint16_t entry_count = m_unwindinfo_data.GetU16(&offset); // entryCount
556 uint16_t encodings_page_offset = m_unwindinfo_data.GetU16(&offset); // encodingsPageOffset
557 uint16_t encodings_count = m_unwindinfo_data.GetU16(&offset); // encodingsCount
558
559 uint32_t encoding_index = BinarySearchCompressedSecondPage (second_page_offset + entry_page_offset, entry_count, function_offset, it->function_offset);
560 if (encoding_index == UINT32_MAX || encoding_index >= encodings_count + m_unwind_header.common_encodings_array_count)
561 {
562 return false;
563 }
564 uint32_t encoding = 0;
565 if (encoding_index < m_unwind_header.common_encodings_array_count)
566 {
567 offset = m_unwind_header.common_encodings_array_offset + (encoding_index * sizeof (uint32_t));
568 encoding = m_unwindinfo_data.GetU32(&offset); // encoding entry from the commonEncodingsArray
569 }
570 else
571 {
572 uint32_t page_specific_entry_index = encoding_index - m_unwind_header.common_encodings_array_count;
573 offset = second_page_offset + encodings_page_offset + (page_specific_entry_index * sizeof (uint32_t));
574 encoding = m_unwindinfo_data.GetU32(&offset); // encoding entry from the page-specific encoding array
575 }
576 if (encoding == 0)
577 return false;
578 unwind_info.encoding = encoding;
579
580 unwind_info.encoding = encoding;
581 if (unwind_info.encoding & UNWIND_HAS_LSDA)
582 {
583 SectionList *sl = m_objfile.GetSectionList ();
584 if (sl)
585 {
586 uint32_t lsda_offset = GetLSDAForFunctionOffset (lsda_array_start, lsda_array_count, function_offset);
587 addr_t objfile_header_file_address = m_objfile.GetHeaderAddress().GetFileAddress();
588 unwind_info.lsda_address.ResolveAddressUsingFileSections (objfile_header_file_address + lsda_offset, sl);
589 }
590 }
591 if (unwind_info.encoding & UNWIND_PERSONALITY_MASK)
592 {
593 uint32_t personality_index = EXTRACT_BITS (unwind_info.encoding, UNWIND_PERSONALITY_MASK);
594
595 if (personality_index > 0)
596 {
597 personality_index--;
598 if (personality_index < m_unwind_header.personality_array_count)
599 {
600 offset_t offset = m_unwind_header.personality_array_offset;
601 offset += 4 * personality_index;
602 SectionList *sl = m_objfile.GetSectionList ();
603 if (sl)
604 {
605 uint32_t personality_offset = m_unwindinfo_data.GetU32(&offset);
606 addr_t objfile_header_file_address = m_objfile.GetHeaderAddress().GetFileAddress();
607 unwind_info.personality_ptr_address.ResolveAddressUsingFileSections (objfile_header_file_address + personality_offset, sl);
608 }
609 }
610 }
611 }
612 return true;
613 }
614 return false;
615}
616
617enum x86_64_eh_regnum {
618 rax = 0,
619 rdx = 1,
620 rcx = 2,
621 rbx = 3,
622 rsi = 4,
623 rdi = 5,
624 rbp = 6,
625 rsp = 7,
626 r8 = 8,
627 r9 = 9,
628 r10 = 10,
629 r11 = 11,
630 r12 = 12,
631 r13 = 13,
632 r14 = 14,
633 r15 = 15,
634 rip = 16 // this is officially the Return Address register number, but close enough
635};
636
637// Convert the compact_unwind_info.h register numbering scheme
638// to eRegisterKindGCC (eh_frame) register numbering scheme.
639uint32_t
640translate_to_eh_frame_regnum_x86_64 (uint32_t unwind_regno)
641{
642 switch (unwind_regno)
643 {
644 case UNWIND_X86_64_REG_RBX:
645 return x86_64_eh_regnum::rbx;
646 case UNWIND_X86_64_REG_R12:
647 return x86_64_eh_regnum::r12;
648 case UNWIND_X86_64_REG_R13:
649 return x86_64_eh_regnum::r13;
650 case UNWIND_X86_64_REG_R14:
651 return x86_64_eh_regnum::r14;
652 case UNWIND_X86_64_REG_R15:
653 return x86_64_eh_regnum::r15;
654 case UNWIND_X86_64_REG_RBP:
655 return x86_64_eh_regnum::rbp;
656 default:
657 return LLDB_INVALID_REGNUM;
658 }
659}
660
661bool
662CompactUnwindInfo::CreateUnwindPlan_x86_64 (Target &target, FunctionInfo &function_info, UnwindPlan &unwind_plan, Address pc_or_function_start)
663{
664 unwind_plan.SetSourceName ("compact unwind info");
665 unwind_plan.SetSourcedFromCompiler (eLazyBoolYes);
666 unwind_plan.SetUnwindPlanValidAtAllInstructions (eLazyBoolNo);
667 unwind_plan.SetRegisterKind (eRegisterKindGCC);
668
669 unwind_plan.SetLSDAAddress (function_info.lsda_address);
670 unwind_plan.SetPersonalityFunctionPtr (function_info.personality_ptr_address);
671
672 UnwindPlan::RowSP row (new UnwindPlan::Row);
673
674 const int wordsize = 8;
675 int mode = function_info.encoding & UNWIND_X86_64_MODE_MASK;
676 switch (mode)
677 {
678 case UNWIND_X86_64_MODE_RBP_FRAME:
679 {
680 row->SetCFARegister (translate_to_eh_frame_regnum_x86_64 (UNWIND_X86_64_REG_RBP));
681 row->SetCFAOffset (2 * wordsize);
682 row->SetOffset (0);
683 row->SetRegisterLocationToAtCFAPlusOffset (x86_64_eh_regnum::rbp, wordsize * -2, true);
684 row->SetRegisterLocationToAtCFAPlusOffset (x86_64_eh_regnum::rip, wordsize * -1, true);
685 row->SetRegisterLocationToIsCFAPlusOffset (x86_64_eh_regnum::rsp, 0, true);
686
687 uint32_t saved_registers_offset = EXTRACT_BITS (function_info.encoding, UNWIND_X86_64_RBP_FRAME_OFFSET);
688
689 uint32_t saved_registers_locations = EXTRACT_BITS (function_info.encoding, UNWIND_X86_64_RBP_FRAME_REGISTERS);
690
691 saved_registers_offset += 2;
692
693 for (int i = 0; i < 5; i++)
694 {
695 uint32_t regnum = saved_registers_locations & 0x7;
696 switch (regnum)
697 {
698 case UNWIND_X86_64_REG_NONE:
699 break;
700 case UNWIND_X86_64_REG_RBX:
701 case UNWIND_X86_64_REG_R12:
702 case UNWIND_X86_64_REG_R13:
703 case UNWIND_X86_64_REG_R14:
704 case UNWIND_X86_64_REG_R15:
705 row->SetRegisterLocationToAtCFAPlusOffset (translate_to_eh_frame_regnum_x86_64 (regnum), wordsize * -saved_registers_offset, true);
706 break;
707 }
708 saved_registers_offset--;
709 saved_registers_locations >>= 3;
710 }
711 unwind_plan.AppendRow (row);
712 return true;
713 }
714 break;
715
716 case UNWIND_X86_64_MODE_STACK_IND:
717 {
718 // The clang in Xcode 6 is emitting incorrect compact unwind encodings for this
719 // style of unwind. It was fixed in llvm r217020 although the algorith being
720 // used to compute this style of unwind in generateCompactUnwindEncodingImpl()
721 // isn't as foolproof as I'm comfortable with -- if any instructions other than
722 // a push are scheduled before the subq, it will give bogus encoding results.
723
724 // The target and pc_or_function_start arguments will be needed to handle this
725 // encoding style correctly -- to find the start address of the function and
726 // read memory offset from there.
727 return false;
728 }
729 break;
730
731#if 0
732 case UNWIND_X86_64_MODE_STACK_IMMD:
733 {
734 uint32_t stack_size = EXTRACT_BITS (encoding, UNWIND_X86_64_FRAMELESS_STACK_SIZE);
735 uint32_t register_count = EXTRACT_BITS (encoding, UNWIND_X86_64_FRAMELESS_STACK_REG_COUNT);
736 uint32_t permutation = EXTRACT_BITS (encoding, UNWIND_X86_64_FRAMELESS_STACK_REG_PERMUTATION);
737
738 if (mode == UNWIND_X86_64_MODE_STACK_IND && function_start)
739 {
740 uint32_t stack_adjust = EXTRACT_BITS (encoding, UNWIND_X86_64_FRAMELESS_STACK_ADJUST);
741
742 // offset into the function instructions; 0 == beginning of first instruction
743 uint32_t offset_to_subl_insn = EXTRACT_BITS (encoding, UNWIND_X86_64_FRAMELESS_STACK_SIZE);
744
745 stack_size = *((uint32_t*) (function_start + offset_to_subl_insn));
746
747 stack_size += stack_adjust * 8;
748
749 printf ("large stack ");
750 }
751
752 printf ("frameless function: stack size %d, register count %d ", stack_size * 8, register_count);
753
754 if (register_count == 0)
755 {
756 printf (" no registers saved");
757 }
758 else
759 {
760
761 // We need to include (up to) 6 registers in 10 bits.
762 // That would be 18 bits if we just used 3 bits per reg to indicate
763 // the order they're saved on the stack.
764 //
765 // This is done with Lehmer code permutation, e.g. see
766 // http://stackoverflow.com/questions/1506078/fast-permutation-number-permutation-mapping-algorithms
767 int permunreg[6];
768
769 // This decodes the variable-base number in the 10 bits
770 // and gives us the Lehmer code sequence which can then
771 // be decoded.
772
773 switch (register_count)
774 {
775 case 6:
776 permunreg[0] = permutation/120; // 120 == 5!
777 permutation -= (permunreg[0]*120);
778 permunreg[1] = permutation/24; // 24 == 4!
779 permutation -= (permunreg[1]*24);
780 permunreg[2] = permutation/6; // 6 == 3!
781 permutation -= (permunreg[2]*6);
782 permunreg[3] = permutation/2; // 2 == 2!
783 permutation -= (permunreg[3]*2);
784 permunreg[4] = permutation; // 1 == 1!
785 permunreg[5] = 0;
786 break;
787 case 5:
788 permunreg[0] = permutation/120;
789 permutation -= (permunreg[0]*120);
790 permunreg[1] = permutation/24;
791 permutation -= (permunreg[1]*24);
792 permunreg[2] = permutation/6;
793 permutation -= (permunreg[2]*6);
794 permunreg[3] = permutation/2;
795 permutation -= (permunreg[3]*2);
796 permunreg[4] = permutation;
797 break;
798 case 4:
799 permunreg[0] = permutation/60;
800 permutation -= (permunreg[0]*60);
801 permunreg[1] = permutation/12;
802 permutation -= (permunreg[1]*12);
803 permunreg[2] = permutation/3;
804 permutation -= (permunreg[2]*3);
805 permunreg[3] = permutation;
806 break;
807 case 3:
808 permunreg[0] = permutation/20;
809 permutation -= (permunreg[0]*20);
810 permunreg[1] = permutation/4;
811 permutation -= (permunreg[1]*4);
812 permunreg[2] = permutation;
813 break;
814 case 2:
815 permunreg[0] = permutation/5;
816 permutation -= (permunreg[0]*5);
817 permunreg[1] = permutation;
818 break;
819 case 1:
820 permunreg[0] = permutation;
821 break;
822 }
823
824 // Decode the Lehmer code for this permutation of
825 // the registers v. http://en.wikipedia.org/wiki/Lehmer_code
826
827 int registers[6];
828 bool used[7] = { false, false, false, false, false, false, false };
829 for (int i = 0; i < register_count; i++)
830 {
831 int renum = 0;
832 for (int j = 1; j < 7; j++)
833 {
834 if (used[j] == false)
835 {
836 if (renum == permunreg[i])
837 {
838 registers[i] = j;
839 used[j] = true;
840 break;
841 }
842 renum++;
843 }
844 }
845 }
846
847
848 printf (" CFA is rsp+%d ", stack_size * 8);
849
850 uint32_t saved_registers_offset = 1;
851 printf (" rip=[CFA-%d]", saved_registers_offset * 8);
852 saved_registers_offset++;
853
854 for (int i = (sizeof (registers) / sizeof (int)) - 1; i >= 0; i--)
855 {
856 switch (registers[i])
857 {
858 case UNWIND_X86_64_REG_NONE:
859 break;
860 case UNWIND_X86_64_REG_RBX:
861 printf (" rbx=[CFA-%d]", saved_registers_offset * 8);
862 break;
863 case UNWIND_X86_64_REG_R12:
864 printf (" r12=[CFA-%d]", saved_registers_offset * 8);
865 break;
866 case UNWIND_X86_64_REG_R13:
867 printf (" r13=[CFA-%d]", saved_registers_offset * 8);
868 break;
869 case UNWIND_X86_64_REG_R14:
870 printf (" r14=[CFA-%d]", saved_registers_offset * 8);
871 break;
872 case UNWIND_X86_64_REG_R15:
873 printf (" r15=[CFA-%d]", saved_registers_offset * 8);
874 break;
875 case UNWIND_X86_64_REG_RBP:
876 printf (" rbp=[CFA-%d]", saved_registers_offset * 8);
877 break;
878 }
879 saved_registers_offset++;
880 }
881
882 }
883
884 }
885 break;
886#endif
887
888 case UNWIND_X86_64_MODE_DWARF:
889 {
890 return false;
891 }
892 break;
893
894 case 0:
895 {
896 return false;
897 }
898 break;
899 }
900 return false;
901}
902
903enum i386_eh_regnum {
904 eax = 0,
905 ecx = 1,
906 edx = 2,
907 ebx = 3,
908 ebp = 4,
909 esp = 5,
910 esi = 6,
911 edi = 7,
912 eip = 8 // this is officially the Return Address register number, but close enough
913};
914
915// Convert the compact_unwind_info.h register numbering scheme
916// to eRegisterKindGCC (eh_frame) register numbering scheme.
917uint32_t
918translate_to_eh_frame_regnum_i386 (uint32_t unwind_regno)
919{
920 switch (unwind_regno)
921 {
922 case UNWIND_X86_REG_EBX:
923 return i386_eh_regnum::ebx;
924 case UNWIND_X86_REG_ECX:
925 return i386_eh_regnum::ecx;
926 case UNWIND_X86_REG_EDX:
927 return i386_eh_regnum::edx;
928 case UNWIND_X86_REG_EDI:
929 return i386_eh_regnum::edi;
930 case UNWIND_X86_REG_ESI:
931 return i386_eh_regnum::esi;
932 case UNWIND_X86_REG_EBP:
933 return i386_eh_regnum::ebp;
934 default:
935 return LLDB_INVALID_REGNUM;
936 }
937}
938
939
940bool
941CompactUnwindInfo::CreateUnwindPlan_i386 (Target &target, FunctionInfo &function_info, UnwindPlan &unwind_plan, Address pc_or_function_start)
942{
943 unwind_plan.SetSourceName ("compact unwind info");
944 unwind_plan.SetSourcedFromCompiler (eLazyBoolYes);
945 unwind_plan.SetUnwindPlanValidAtAllInstructions (eLazyBoolNo);
946 unwind_plan.SetRegisterKind (eRegisterKindGCC);
947
948 unwind_plan.SetLSDAAddress (function_info.lsda_address);
949 unwind_plan.SetPersonalityFunctionPtr (function_info.personality_ptr_address);
950
951 UnwindPlan::RowSP row (new UnwindPlan::Row);
952
953 const int wordsize = 4;
954 int mode = function_info.encoding & UNWIND_X86_MODE_MASK;
955 switch (mode)
956 {
957 case UNWIND_X86_MODE_EBP_FRAME:
958 {
959 row->SetCFARegister (translate_to_eh_frame_regnum_i386 (UNWIND_X86_REG_EBP));
960 row->SetCFAOffset (2 * wordsize);
961 row->SetOffset (0);
962 row->SetRegisterLocationToAtCFAPlusOffset (i386_eh_regnum::ebp, wordsize * -2, true);
963 row->SetRegisterLocationToAtCFAPlusOffset (i386_eh_regnum::eip, wordsize * -1, true);
964 row->SetRegisterLocationToIsCFAPlusOffset (i386_eh_regnum::esp, 0, true);
965
966 uint32_t saved_registers_offset = EXTRACT_BITS (function_info.encoding, UNWIND_X86_EBP_FRAME_OFFSET);
967
968 uint32_t saved_registers_locations = EXTRACT_BITS (function_info.encoding, UNWIND_X86_EBP_FRAME_REGISTERS);
969
970 saved_registers_offset += 2;
971
972 for (int i = 0; i < 5; i++)
973 {
974 uint32_t regnum = saved_registers_locations & 0x7;
975 switch (regnum)
976 {
977 case UNWIND_X86_REG_NONE:
978 break;
979 case UNWIND_X86_REG_EBX:
980 case UNWIND_X86_REG_ECX:
981 case UNWIND_X86_REG_EDX:
982 case UNWIND_X86_REG_EDI:
983 case UNWIND_X86_REG_ESI:
984 row->SetRegisterLocationToAtCFAPlusOffset (translate_to_eh_frame_regnum_i386 (regnum), wordsize * -saved_registers_offset, true);
985 break;
986 }
987 saved_registers_offset--;
988 saved_registers_locations >>= 3;
989 }
990 unwind_plan.AppendRow (row);
991 return true;
992 }
993 break;
994
995 case UNWIND_X86_MODE_STACK_IND:
996 case UNWIND_X86_MODE_STACK_IMMD:
997 case UNWIND_X86_MODE_DWARF:
998 {
999 return false;
1000 }
1001 break;
1002 }
1003 return false;
1004}