blob: ae99f30a7c165a8bd3a58948ff645975a9cfc0e5 [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"
Zachary Turneraf0f45f2015-03-03 21:05:17 +000016#include "lldb/Core/DataBufferHeap.h"
Jason Molendab12a1362014-12-20 03:12:51 +000017#include "lldb/Core/Log.h"
Jason Molendae589e7e2014-12-08 03:09:00 +000018#include "lldb/Core/Module.h"
19#include "lldb/Core/Section.h"
Jason Molendab12a1362014-12-20 03:12:51 +000020#include "lldb/Core/Section.h"
Jason Molenda5c45c542014-12-21 10:44:54 +000021#include "lldb/Core/StreamString.h"
Jason Molendae589e7e2014-12-08 03:09:00 +000022#include "lldb/Symbol/CompactUnwindInfo.h"
23#include "lldb/Symbol/ObjectFile.h"
24#include "lldb/Symbol/UnwindPlan.h"
Jason Molendab12a1362014-12-20 03:12:51 +000025#include "lldb/Target/Process.h"
26#include "lldb/Target/Target.h"
Jason Molendae589e7e2014-12-08 03:09:00 +000027
Zachary Turner818a3672014-12-08 20:00:33 +000028#include "llvm/Support/MathExtras.h"
29
Jason Molendae589e7e2014-12-08 03:09:00 +000030using namespace lldb;
31using namespace lldb_private;
32
33
34namespace lldb_private {
35
36 // Constants from <mach-o/compact_unwind_encoding.h>
37
38 enum {
39 UNWIND_IS_NOT_FUNCTION_START = 0x80000000,
40 UNWIND_HAS_LSDA = 0x40000000,
41 UNWIND_PERSONALITY_MASK = 0x30000000,
42 };
43
44 enum {
45 UNWIND_X86_MODE_MASK = 0x0F000000,
46 UNWIND_X86_MODE_EBP_FRAME = 0x01000000,
47 UNWIND_X86_MODE_STACK_IMMD = 0x02000000,
48 UNWIND_X86_MODE_STACK_IND = 0x03000000,
49 UNWIND_X86_MODE_DWARF = 0x04000000,
50
51 UNWIND_X86_EBP_FRAME_REGISTERS = 0x00007FFF,
52 UNWIND_X86_EBP_FRAME_OFFSET = 0x00FF0000,
53
54 UNWIND_X86_FRAMELESS_STACK_SIZE = 0x00FF0000,
55 UNWIND_X86_FRAMELESS_STACK_ADJUST = 0x0000E000,
56 UNWIND_X86_FRAMELESS_STACK_REG_COUNT = 0x00001C00,
57 UNWIND_X86_FRAMELESS_STACK_REG_PERMUTATION = 0x000003FF,
58
59 UNWIND_X86_DWARF_SECTION_OFFSET = 0x00FFFFFF,
60 };
61
62 enum {
63 UNWIND_X86_REG_NONE = 0,
64 UNWIND_X86_REG_EBX = 1,
65 UNWIND_X86_REG_ECX = 2,
66 UNWIND_X86_REG_EDX = 3,
67 UNWIND_X86_REG_EDI = 4,
68 UNWIND_X86_REG_ESI = 5,
69 UNWIND_X86_REG_EBP = 6,
70 };
71 enum {
72 UNWIND_X86_64_MODE_MASK = 0x0F000000,
73 UNWIND_X86_64_MODE_RBP_FRAME = 0x01000000,
74 UNWIND_X86_64_MODE_STACK_IMMD = 0x02000000,
75 UNWIND_X86_64_MODE_STACK_IND = 0x03000000,
76 UNWIND_X86_64_MODE_DWARF = 0x04000000,
77
78 UNWIND_X86_64_RBP_FRAME_REGISTERS = 0x00007FFF,
79 UNWIND_X86_64_RBP_FRAME_OFFSET = 0x00FF0000,
80
81 UNWIND_X86_64_FRAMELESS_STACK_SIZE = 0x00FF0000,
82 UNWIND_X86_64_FRAMELESS_STACK_ADJUST = 0x0000E000,
83 UNWIND_X86_64_FRAMELESS_STACK_REG_COUNT = 0x00001C00,
84 UNWIND_X86_64_FRAMELESS_STACK_REG_PERMUTATION = 0x000003FF,
85
86 UNWIND_X86_64_DWARF_SECTION_OFFSET = 0x00FFFFFF,
87 };
88
89 enum {
90 UNWIND_X86_64_REG_NONE = 0,
91 UNWIND_X86_64_REG_RBX = 1,
92 UNWIND_X86_64_REG_R12 = 2,
93 UNWIND_X86_64_REG_R13 = 3,
94 UNWIND_X86_64_REG_R14 = 4,
95 UNWIND_X86_64_REG_R15 = 5,
96 UNWIND_X86_64_REG_RBP = 6,
97 };
98};
99
100
101#ifndef UNWIND_SECOND_LEVEL_REGULAR
102#define UNWIND_SECOND_LEVEL_REGULAR 2
103#endif
104
105#ifndef UNWIND_SECOND_LEVEL_COMPRESSED
106#define UNWIND_SECOND_LEVEL_COMPRESSED 3
107#endif
108
109#ifndef UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET
110#define UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET(entry) (entry & 0x00FFFFFF)
111#endif
112
113#ifndef UNWIND_INFO_COMPRESSED_ENTRY_ENCODING_INDEX
114#define UNWIND_INFO_COMPRESSED_ENTRY_ENCODING_INDEX(entry) ((entry >> 24) & 0xFF)
115#endif
116
117#define EXTRACT_BITS(value, mask) \
Zachary Turner818a3672014-12-08 20:00:33 +0000118 ( (value >> llvm::countTrailingZeros(static_cast<uint32_t>(mask), llvm::ZB_Width)) & \
Benjamin Kramer2ea19612015-02-12 16:16:56 +0000119 (((1 << llvm::countPopulation(static_cast<uint32_t>(mask))))-1) )
Jason Molendae589e7e2014-12-08 03:09:00 +0000120
121
122
123//----------------------
124// constructor
125//----------------------
126
127
128CompactUnwindInfo::CompactUnwindInfo(ObjectFile& objfile, SectionSP& section_sp) :
129 m_objfile (objfile),
130 m_section_sp (section_sp),
Jason Molendab12a1362014-12-20 03:12:51 +0000131 m_section_contents_if_encrypted (),
Jason Molendae589e7e2014-12-08 03:09:00 +0000132 m_mutex (),
133 m_indexes (),
134 m_indexes_computed (eLazyBoolCalculate),
135 m_unwindinfo_data (),
136 m_unwindinfo_data_computed (false),
137 m_unwind_header ()
138{
139
140}
141
142//----------------------
143// destructor
144//----------------------
145
146CompactUnwindInfo::~CompactUnwindInfo()
147{
148}
149
150bool
151CompactUnwindInfo::GetUnwindPlan (Target &target, Address addr, UnwindPlan& unwind_plan)
152{
Jason Molendab12a1362014-12-20 03:12:51 +0000153 if (!IsValid (target.GetProcessSP()))
Jason Molendae589e7e2014-12-08 03:09:00 +0000154 {
155 return false;
156 }
157 FunctionInfo function_info;
158 if (GetCompactUnwindInfoForFunction (target, addr, function_info))
159 {
160 // shortcut return for functions that have no compact unwind
161 if (function_info.encoding == 0)
162 return false;
163
164 ArchSpec arch;
165 if (m_objfile.GetArchitecture (arch))
166 {
Jason Molenda5c45c542014-12-21 10:44:54 +0000167
168 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
169 if (log && log->GetVerbose())
170 {
171 StreamString strm;
172 addr.Dump (&strm, NULL, Address::DumpStyle::DumpStyleResolvedDescriptionNoFunctionArguments, Address::DumpStyle::DumpStyleFileAddress, arch.GetAddressByteSize());
173 log->Printf ("Got compact unwind encoding 0x%x for function %s", function_info.encoding, strm.GetData());
174 }
175
176 if (function_info.valid_range_offset_start != 0 && function_info.valid_range_offset_end != 0)
177 {
178 SectionList *sl = m_objfile.GetSectionList ();
179 if (sl)
180 {
181 addr_t func_range_start_file_addr =
182 function_info.valid_range_offset_start + m_objfile.GetHeaderAddress().GetFileAddress();
183 AddressRange func_range (func_range_start_file_addr,
184 function_info.valid_range_offset_end - function_info.valid_range_offset_start,
185 sl);
186 unwind_plan.SetPlanValidAddressRange (func_range);
187 }
188 }
189
Jason Molendae589e7e2014-12-08 03:09:00 +0000190 if (arch.GetTriple().getArch() == llvm::Triple::x86_64)
191 {
192 return CreateUnwindPlan_x86_64 (target, function_info, unwind_plan, addr);
193 }
194 if (arch.GetTriple().getArch() == llvm::Triple::x86)
195 {
196 return CreateUnwindPlan_i386 (target, function_info, unwind_plan, addr);
197 }
198 }
199 }
200 return false;
201}
202
203bool
Jason Molendab12a1362014-12-20 03:12:51 +0000204CompactUnwindInfo::IsValid (const ProcessSP &process_sp)
Jason Molendae589e7e2014-12-08 03:09:00 +0000205{
Jason Molendab12a1362014-12-20 03:12:51 +0000206 if (m_section_sp.get() == nullptr)
Jason Molendae589e7e2014-12-08 03:09:00 +0000207 return false;
208
209 if (m_indexes_computed == eLazyBoolYes && m_unwindinfo_data_computed)
210 return true;
211
Jason Molendab12a1362014-12-20 03:12:51 +0000212 ScanIndex (process_sp);
Jason Molendae589e7e2014-12-08 03:09:00 +0000213
214 return m_indexes_computed == eLazyBoolYes && m_unwindinfo_data_computed;
215}
216
217void
Jason Molendab12a1362014-12-20 03:12:51 +0000218CompactUnwindInfo::ScanIndex (const ProcessSP &process_sp)
Jason Molendae589e7e2014-12-08 03:09:00 +0000219{
220 Mutex::Locker locker(m_mutex);
221 if (m_indexes_computed == eLazyBoolYes && m_unwindinfo_data_computed)
222 return;
223
224 // We can't read the index for some reason.
225 if (m_indexes_computed == eLazyBoolNo)
226 {
227 return;
228 }
229
Jason Molenda5c45c542014-12-21 10:44:54 +0000230 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
231 if (log)
232 m_objfile.GetModule()->LogMessage(log, "Reading compact unwind first-level indexes");
233
Jason Molendae589e7e2014-12-08 03:09:00 +0000234 if (m_unwindinfo_data_computed == false)
235 {
Jason Molendab12a1362014-12-20 03:12:51 +0000236 if (m_section_sp->IsEncrypted())
237 {
238 // Can't get section contents of a protected/encrypted section until we have a live
239 // process and can read them out of memory.
240 if (process_sp.get() == nullptr)
241 return;
242 m_section_contents_if_encrypted.reset (new DataBufferHeap (m_section_sp->GetByteSize(), 0));
243 Error error;
244 if (process_sp->ReadMemory (
245 m_section_sp->GetLoadBaseAddress (&process_sp->GetTarget()),
246 m_section_contents_if_encrypted->GetBytes(),
247 m_section_sp->GetByteSize(), error) == m_section_sp->GetByteSize() && error.Success())
248 {
249 m_unwindinfo_data.SetAddressByteSize (process_sp->GetTarget().GetArchitecture().GetAddressByteSize());
250 m_unwindinfo_data.SetByteOrder (process_sp->GetTarget().GetArchitecture().GetByteOrder());
251 m_unwindinfo_data.SetData (m_section_contents_if_encrypted, 0);
252 }
253 }
254 else
255 {
256 m_objfile.ReadSectionData (m_section_sp.get(), m_unwindinfo_data);
257 }
258 if (m_unwindinfo_data.GetByteSize() != m_section_sp->GetByteSize())
259 return;
Jason Molendae589e7e2014-12-08 03:09:00 +0000260 m_unwindinfo_data_computed = true;
261 }
262
263 if (m_unwindinfo_data.GetByteSize() > 0)
264 {
265 offset_t offset = 0;
266
267 // struct unwind_info_section_header
268 // {
269 // uint32_t version; // UNWIND_SECTION_VERSION
270 // uint32_t commonEncodingsArraySectionOffset;
271 // uint32_t commonEncodingsArrayCount;
272 // uint32_t personalityArraySectionOffset;
273 // uint32_t personalityArrayCount;
274 // uint32_t indexSectionOffset;
275 // uint32_t indexCount;
276
277 m_unwind_header.version = m_unwindinfo_data.GetU32(&offset);
278 m_unwind_header.common_encodings_array_offset = m_unwindinfo_data.GetU32(&offset);
279 m_unwind_header.common_encodings_array_count = m_unwindinfo_data.GetU32(&offset);
280 m_unwind_header.personality_array_offset = m_unwindinfo_data.GetU32(&offset);
281 m_unwind_header.personality_array_count = m_unwindinfo_data.GetU32(&offset);
282 uint32_t indexSectionOffset = m_unwindinfo_data.GetU32(&offset);
283
284 uint32_t indexCount = m_unwindinfo_data.GetU32(&offset);
285
Jason Molendac48ef342015-04-02 04:35:32 +0000286 if (m_unwind_header.common_encodings_array_offset > m_unwindinfo_data.GetByteSize()
287 || m_unwind_header.personality_array_offset > m_unwindinfo_data.GetByteSize()
288 || indexSectionOffset > m_unwindinfo_data.GetByteSize()
289 || offset > m_unwindinfo_data.GetByteSize())
Jason Molendae589e7e2014-12-08 03:09:00 +0000290 {
Jason Molendac48ef342015-04-02 04:35:32 +0000291 Host::SystemLog (Host::eSystemLogError,
292 "error: Invalid offset encountered in compact unwind info, skipping\n");
293 // don't trust anything from this compact_unwind section if it looks
294 // blatently invalid data in the header.
Jason Molendae589e7e2014-12-08 03:09:00 +0000295 m_indexes_computed = eLazyBoolNo;
Jason Molendac48ef342015-04-02 04:35:32 +0000296 return;
Jason Molendae589e7e2014-12-08 03:09:00 +0000297 }
298
299 // Parse the basic information from the indexes
300 // We wait to scan the second level page info until it's needed
301
302 // struct unwind_info_section_header_index_entry
303 // {
304 // uint32_t functionOffset;
305 // uint32_t secondLevelPagesSectionOffset;
306 // uint32_t lsdaIndexArraySectionOffset;
307 // };
308
309 offset = indexSectionOffset;
Andy Gibbs3acfe1a2014-12-29 13:03:19 +0000310 for (uint32_t idx = 0; idx < indexCount; idx++)
Jason Molendae589e7e2014-12-08 03:09:00 +0000311 {
312 uint32_t function_offset = m_unwindinfo_data.GetU32(&offset); // functionOffset
313 uint32_t second_level_offset = m_unwindinfo_data.GetU32(&offset); // secondLevelPagesSectionOffset
314 uint32_t lsda_offset = m_unwindinfo_data.GetU32(&offset); // lsdaIndexArraySectionOffset
315
316 if (second_level_offset > m_section_sp->GetByteSize() || lsda_offset > m_section_sp->GetByteSize())
317 {
318 m_indexes_computed = eLazyBoolNo;
319 }
320
321 UnwindIndex this_index;
322 this_index.function_offset = function_offset; //
323 this_index.second_level = second_level_offset;
324 this_index.lsda_array_start = lsda_offset;
325
326 if (m_indexes.size() > 0)
327 {
328 m_indexes[m_indexes.size() - 1].lsda_array_end = lsda_offset;
329 }
330
331 if (second_level_offset == 0)
332 {
333 this_index.sentinal_entry = true;
334 }
335
336 m_indexes.push_back (this_index);
337 }
338 m_indexes_computed = eLazyBoolYes;
339 }
340 else
341 {
342 m_indexes_computed = eLazyBoolNo;
343 }
344}
345
346uint32_t
347CompactUnwindInfo::GetLSDAForFunctionOffset (uint32_t lsda_offset, uint32_t lsda_count, uint32_t function_offset)
348{
349 // struct unwind_info_section_header_lsda_index_entry
350 // {
351 // uint32_t functionOffset;
352 // uint32_t lsdaOffset;
353 // };
354
355 offset_t first_entry = lsda_offset;
356 uint32_t low = 0;
357 uint32_t high = lsda_count;
358 while (low < high)
359 {
360 uint32_t mid = (low + high) / 2;
361 offset_t offset = first_entry + (mid * 8);
362 uint32_t mid_func_offset = m_unwindinfo_data.GetU32(&offset); // functionOffset
363 uint32_t mid_lsda_offset = m_unwindinfo_data.GetU32(&offset); // lsdaOffset
364 if (mid_func_offset == function_offset)
365 {
366 return mid_lsda_offset;
367 }
368 if (mid_func_offset < function_offset)
369 {
370 low = mid + 1;
371 }
372 else
373 {
374 high = mid;
375 }
376 }
377 return 0;
378}
379
380lldb::offset_t
Jason Molenda5c45c542014-12-21 10:44:54 +0000381CompactUnwindInfo::BinarySearchRegularSecondPage (uint32_t entry_page_offset, uint32_t entry_count, uint32_t function_offset, uint32_t *entry_func_start_offset, uint32_t *entry_func_end_offset)
Jason Molendae589e7e2014-12-08 03:09:00 +0000382{
383 // typedef uint32_t compact_unwind_encoding_t;
384 // struct unwind_info_regular_second_level_entry
385 // {
386 // uint32_t functionOffset;
387 // compact_unwind_encoding_t encoding;
388
389 offset_t first_entry = entry_page_offset;
390
391 uint32_t low = 0;
392 uint32_t high = entry_count;
393 uint32_t last = high - 1;
394 while (low < high)
395 {
396 uint32_t mid = (low + high) / 2;
397 offset_t offset = first_entry + (mid * 8);
398 uint32_t mid_func_offset = m_unwindinfo_data.GetU32(&offset); // functionOffset
399 uint32_t next_func_offset = 0;
400 if (mid < last)
401 {
402 offset = first_entry + ((mid + 1) * 8);
403 next_func_offset = m_unwindinfo_data.GetU32(&offset); // functionOffset
404 }
405 if (mid_func_offset <= function_offset)
406 {
407 if (mid == last || (next_func_offset > function_offset))
408 {
Jason Molenda5c45c542014-12-21 10:44:54 +0000409 if (entry_func_start_offset)
410 *entry_func_start_offset = mid_func_offset;
411 if (mid != last && entry_func_end_offset)
412 *entry_func_end_offset = next_func_offset;
Jason Molendae589e7e2014-12-08 03:09:00 +0000413 return first_entry + (mid * 8);
414 }
415 else
416 {
417 low = mid + 1;
418 }
419 }
420 else
421 {
422 high = mid;
423 }
424 }
425 return LLDB_INVALID_OFFSET;
426}
427
428uint32_t
Jason Molenda5c45c542014-12-21 10:44:54 +0000429CompactUnwindInfo::BinarySearchCompressedSecondPage (uint32_t entry_page_offset, uint32_t entry_count, uint32_t function_offset_to_find, uint32_t function_offset_base, uint32_t *entry_func_start_offset, uint32_t *entry_func_end_offset)
Jason Molendae589e7e2014-12-08 03:09:00 +0000430{
431 offset_t first_entry = entry_page_offset;
432
433 uint32_t low = 0;
434 uint32_t high = entry_count;
435 uint32_t last = high - 1;
436 while (low < high)
437 {
438 uint32_t mid = (low + high) / 2;
439 offset_t offset = first_entry + (mid * 4);
440 uint32_t entry = m_unwindinfo_data.GetU32(&offset); // entry
441 uint32_t mid_func_offset = UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET (entry);
442 mid_func_offset += function_offset_base;
443 uint32_t next_func_offset = 0;
444 if (mid < last)
445 {
446 offset = first_entry + ((mid + 1) * 4);
447 uint32_t next_entry = m_unwindinfo_data.GetU32(&offset); // entry
448 next_func_offset = UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET (next_entry);
449 next_func_offset += function_offset_base;
450 }
451 if (mid_func_offset <= function_offset_to_find)
452 {
453 if (mid == last || (next_func_offset > function_offset_to_find))
454 {
Jason Molenda5c45c542014-12-21 10:44:54 +0000455 if (entry_func_start_offset)
456 *entry_func_start_offset = mid_func_offset;
457 if (mid != last && entry_func_end_offset)
458 *entry_func_end_offset = next_func_offset;
Jason Molendae589e7e2014-12-08 03:09:00 +0000459 return UNWIND_INFO_COMPRESSED_ENTRY_ENCODING_INDEX (entry);
460 }
461 else
462 {
463 low = mid + 1;
464 }
465 }
466 else
467 {
468 high = mid;
469 }
470 }
471
472 return UINT32_MAX;
473}
474
Jason Molendae589e7e2014-12-08 03:09:00 +0000475bool
476CompactUnwindInfo::GetCompactUnwindInfoForFunction (Target &target, Address address, FunctionInfo &unwind_info)
477{
478 unwind_info.encoding = 0;
479 unwind_info.lsda_address.Clear();
480 unwind_info.personality_ptr_address.Clear();
481
Jason Molendab12a1362014-12-20 03:12:51 +0000482 if (!IsValid (target.GetProcessSP()))
Jason Molendae589e7e2014-12-08 03:09:00 +0000483 return false;
484
Jason Molendae589e7e2014-12-08 03:09:00 +0000485 addr_t text_section_file_address = LLDB_INVALID_ADDRESS;
486 SectionList *sl = m_objfile.GetSectionList ();
487 if (sl)
488 {
489 SectionSP text_sect = sl->FindSectionByType (eSectionTypeCode, true);
490 if (text_sect.get())
491 {
492 text_section_file_address = text_sect->GetFileAddress();
493 }
494 }
495 if (text_section_file_address == LLDB_INVALID_ADDRESS)
496 return false;
497
498 addr_t function_offset = address.GetFileAddress() - m_objfile.GetHeaderAddress().GetFileAddress();
499
500 UnwindIndex key;
501 key.function_offset = function_offset;
502
503 std::vector<UnwindIndex>::const_iterator it;
504 it = std::lower_bound (m_indexes.begin(), m_indexes.end(), key);
505 if (it == m_indexes.end())
506 {
507 return false;
508 }
509
510 if (it->function_offset != key.function_offset)
511 {
512 if (it != m_indexes.begin())
513 --it;
514 }
515
516 if (it->sentinal_entry == true)
517 {
518 return false;
519 }
520
Jason Molenda5c45c542014-12-21 10:44:54 +0000521 auto next_it = it + 1;
522 if (next_it != m_indexes.begin())
523 {
524 // initialize the function offset end range to be the start of the
525 // next index offset. If we find an entry which is at the end of
526 // the index table, this will establish the range end.
527 unwind_info.valid_range_offset_end = next_it->function_offset;
528 }
529
Jason Molendae589e7e2014-12-08 03:09:00 +0000530 offset_t second_page_offset = it->second_level;
531 offset_t lsda_array_start = it->lsda_array_start;
532 offset_t lsda_array_count = (it->lsda_array_end - it->lsda_array_start) / 8;
533
534 offset_t offset = second_page_offset;
535 uint32_t kind = m_unwindinfo_data.GetU32(&offset); // UNWIND_SECOND_LEVEL_REGULAR or UNWIND_SECOND_LEVEL_COMPRESSED
536
537 if (kind == UNWIND_SECOND_LEVEL_REGULAR)
538 {
539 // struct unwind_info_regular_second_level_page_header
540 // {
541 // uint32_t kind; // UNWIND_SECOND_LEVEL_REGULAR
542 // uint16_t entryPageOffset;
543 // uint16_t entryCount;
544
545 // typedef uint32_t compact_unwind_encoding_t;
546 // struct unwind_info_regular_second_level_entry
547 // {
548 // uint32_t functionOffset;
549 // compact_unwind_encoding_t encoding;
550
551 uint16_t entry_page_offset = m_unwindinfo_data.GetU16(&offset); // entryPageOffset
552 uint16_t entry_count = m_unwindinfo_data.GetU16(&offset); // entryCount
553
Jason Molenda5c45c542014-12-21 10:44:54 +0000554 offset_t entry_offset = BinarySearchRegularSecondPage (second_page_offset + entry_page_offset, entry_count, function_offset, &unwind_info.valid_range_offset_start, &unwind_info.valid_range_offset_end);
Jason Molendae589e7e2014-12-08 03:09:00 +0000555 if (entry_offset == LLDB_INVALID_OFFSET)
556 {
557 return false;
558 }
559 entry_offset += 4; // skip over functionOffset
560 unwind_info.encoding = m_unwindinfo_data.GetU32(&entry_offset); // encoding
561 if (unwind_info.encoding & UNWIND_HAS_LSDA)
562 {
563 SectionList *sl = m_objfile.GetSectionList ();
564 if (sl)
565 {
566 uint32_t lsda_offset = GetLSDAForFunctionOffset (lsda_array_start, lsda_array_count, function_offset);
567 addr_t objfile_header_file_address = m_objfile.GetHeaderAddress().GetFileAddress();
568 unwind_info.lsda_address.ResolveAddressUsingFileSections (objfile_header_file_address + lsda_offset, sl);
569 }
570 }
571 if (unwind_info.encoding & UNWIND_PERSONALITY_MASK)
572 {
573 uint32_t personality_index = EXTRACT_BITS (unwind_info.encoding, UNWIND_PERSONALITY_MASK);
574
575 if (personality_index > 0)
576 {
577 personality_index--;
578 if (personality_index < m_unwind_header.personality_array_count)
579 {
580 offset_t offset = m_unwind_header.personality_array_offset;
581 offset += 4 * personality_index;
582 SectionList *sl = m_objfile.GetSectionList ();
583 if (sl)
584 {
585 uint32_t personality_offset = m_unwindinfo_data.GetU32(&offset);
586 addr_t objfile_header_file_address = m_objfile.GetHeaderAddress().GetFileAddress();
587 unwind_info.personality_ptr_address.ResolveAddressUsingFileSections (objfile_header_file_address + personality_offset, sl);
588 }
589 }
590 }
591 }
592 return true;
593 }
594 else if (kind == UNWIND_SECOND_LEVEL_COMPRESSED)
595 {
596 // struct unwind_info_compressed_second_level_page_header
597 // {
598 // uint32_t kind; // UNWIND_SECOND_LEVEL_COMPRESSED
599 // uint16_t entryPageOffset; // offset from this 2nd lvl page idx to array of entries
600 // // (an entry has a function offset and index into the encodings)
601 // // NB function offset from the entry in the compressed page
602 // // must be added to the index's functionOffset value.
603 // uint16_t entryCount;
604 // uint16_t encodingsPageOffset; // offset from this 2nd lvl page idx to array of encodings
605 // uint16_t encodingsCount;
606
607 uint16_t entry_page_offset = m_unwindinfo_data.GetU16(&offset); // entryPageOffset
608 uint16_t entry_count = m_unwindinfo_data.GetU16(&offset); // entryCount
609 uint16_t encodings_page_offset = m_unwindinfo_data.GetU16(&offset); // encodingsPageOffset
610 uint16_t encodings_count = m_unwindinfo_data.GetU16(&offset); // encodingsCount
611
Jason Molenda5c45c542014-12-21 10:44:54 +0000612 uint32_t encoding_index = BinarySearchCompressedSecondPage (second_page_offset + entry_page_offset, entry_count, function_offset, it->function_offset, &unwind_info.valid_range_offset_start, &unwind_info.valid_range_offset_end);
Jason Molendae589e7e2014-12-08 03:09:00 +0000613 if (encoding_index == UINT32_MAX || encoding_index >= encodings_count + m_unwind_header.common_encodings_array_count)
614 {
615 return false;
616 }
617 uint32_t encoding = 0;
618 if (encoding_index < m_unwind_header.common_encodings_array_count)
619 {
620 offset = m_unwind_header.common_encodings_array_offset + (encoding_index * sizeof (uint32_t));
621 encoding = m_unwindinfo_data.GetU32(&offset); // encoding entry from the commonEncodingsArray
622 }
623 else
624 {
625 uint32_t page_specific_entry_index = encoding_index - m_unwind_header.common_encodings_array_count;
626 offset = second_page_offset + encodings_page_offset + (page_specific_entry_index * sizeof (uint32_t));
627 encoding = m_unwindinfo_data.GetU32(&offset); // encoding entry from the page-specific encoding array
628 }
629 if (encoding == 0)
630 return false;
Jason Molendae589e7e2014-12-08 03:09:00 +0000631
632 unwind_info.encoding = encoding;
633 if (unwind_info.encoding & UNWIND_HAS_LSDA)
634 {
635 SectionList *sl = m_objfile.GetSectionList ();
636 if (sl)
637 {
638 uint32_t lsda_offset = GetLSDAForFunctionOffset (lsda_array_start, lsda_array_count, function_offset);
639 addr_t objfile_header_file_address = m_objfile.GetHeaderAddress().GetFileAddress();
640 unwind_info.lsda_address.ResolveAddressUsingFileSections (objfile_header_file_address + lsda_offset, sl);
641 }
642 }
643 if (unwind_info.encoding & UNWIND_PERSONALITY_MASK)
644 {
645 uint32_t personality_index = EXTRACT_BITS (unwind_info.encoding, UNWIND_PERSONALITY_MASK);
646
647 if (personality_index > 0)
648 {
649 personality_index--;
650 if (personality_index < m_unwind_header.personality_array_count)
651 {
652 offset_t offset = m_unwind_header.personality_array_offset;
653 offset += 4 * personality_index;
654 SectionList *sl = m_objfile.GetSectionList ();
655 if (sl)
656 {
657 uint32_t personality_offset = m_unwindinfo_data.GetU32(&offset);
658 addr_t objfile_header_file_address = m_objfile.GetHeaderAddress().GetFileAddress();
659 unwind_info.personality_ptr_address.ResolveAddressUsingFileSections (objfile_header_file_address + personality_offset, sl);
660 }
661 }
662 }
663 }
664 return true;
665 }
666 return false;
667}
668
669enum x86_64_eh_regnum {
670 rax = 0,
671 rdx = 1,
672 rcx = 2,
673 rbx = 3,
674 rsi = 4,
675 rdi = 5,
676 rbp = 6,
677 rsp = 7,
678 r8 = 8,
679 r9 = 9,
680 r10 = 10,
681 r11 = 11,
682 r12 = 12,
683 r13 = 13,
684 r14 = 14,
685 r15 = 15,
686 rip = 16 // this is officially the Return Address register number, but close enough
687};
688
689// Convert the compact_unwind_info.h register numbering scheme
690// to eRegisterKindGCC (eh_frame) register numbering scheme.
691uint32_t
692translate_to_eh_frame_regnum_x86_64 (uint32_t unwind_regno)
693{
694 switch (unwind_regno)
695 {
696 case UNWIND_X86_64_REG_RBX:
697 return x86_64_eh_regnum::rbx;
698 case UNWIND_X86_64_REG_R12:
699 return x86_64_eh_regnum::r12;
700 case UNWIND_X86_64_REG_R13:
701 return x86_64_eh_regnum::r13;
702 case UNWIND_X86_64_REG_R14:
703 return x86_64_eh_regnum::r14;
704 case UNWIND_X86_64_REG_R15:
705 return x86_64_eh_regnum::r15;
706 case UNWIND_X86_64_REG_RBP:
707 return x86_64_eh_regnum::rbp;
708 default:
709 return LLDB_INVALID_REGNUM;
710 }
711}
712
713bool
714CompactUnwindInfo::CreateUnwindPlan_x86_64 (Target &target, FunctionInfo &function_info, UnwindPlan &unwind_plan, Address pc_or_function_start)
715{
716 unwind_plan.SetSourceName ("compact unwind info");
717 unwind_plan.SetSourcedFromCompiler (eLazyBoolYes);
718 unwind_plan.SetUnwindPlanValidAtAllInstructions (eLazyBoolNo);
719 unwind_plan.SetRegisterKind (eRegisterKindGCC);
720
721 unwind_plan.SetLSDAAddress (function_info.lsda_address);
722 unwind_plan.SetPersonalityFunctionPtr (function_info.personality_ptr_address);
723
724 UnwindPlan::RowSP row (new UnwindPlan::Row);
725
726 const int wordsize = 8;
727 int mode = function_info.encoding & UNWIND_X86_64_MODE_MASK;
728 switch (mode)
729 {
730 case UNWIND_X86_64_MODE_RBP_FRAME:
731 {
Pavel Labathab970f52015-02-23 10:19:16 +0000732 row->GetCFAValue().SetIsRegisterPlusOffset (
733 translate_to_eh_frame_regnum_x86_64 (UNWIND_X86_64_REG_RBP),
734 2 * wordsize);
Jason Molendae589e7e2014-12-08 03:09:00 +0000735 row->SetOffset (0);
736 row->SetRegisterLocationToAtCFAPlusOffset (x86_64_eh_regnum::rbp, wordsize * -2, true);
737 row->SetRegisterLocationToAtCFAPlusOffset (x86_64_eh_regnum::rip, wordsize * -1, true);
738 row->SetRegisterLocationToIsCFAPlusOffset (x86_64_eh_regnum::rsp, 0, true);
739
740 uint32_t saved_registers_offset = EXTRACT_BITS (function_info.encoding, UNWIND_X86_64_RBP_FRAME_OFFSET);
741
742 uint32_t saved_registers_locations = EXTRACT_BITS (function_info.encoding, UNWIND_X86_64_RBP_FRAME_REGISTERS);
743
744 saved_registers_offset += 2;
745
746 for (int i = 0; i < 5; i++)
747 {
748 uint32_t regnum = saved_registers_locations & 0x7;
749 switch (regnum)
750 {
751 case UNWIND_X86_64_REG_NONE:
752 break;
753 case UNWIND_X86_64_REG_RBX:
754 case UNWIND_X86_64_REG_R12:
755 case UNWIND_X86_64_REG_R13:
756 case UNWIND_X86_64_REG_R14:
757 case UNWIND_X86_64_REG_R15:
758 row->SetRegisterLocationToAtCFAPlusOffset (translate_to_eh_frame_regnum_x86_64 (regnum), wordsize * -saved_registers_offset, true);
759 break;
760 }
761 saved_registers_offset--;
762 saved_registers_locations >>= 3;
763 }
764 unwind_plan.AppendRow (row);
765 return true;
766 }
767 break;
768
769 case UNWIND_X86_64_MODE_STACK_IND:
770 {
771 // The clang in Xcode 6 is emitting incorrect compact unwind encodings for this
Jason Molenda19ba9fb2014-12-22 11:02:02 +0000772 // style of unwind. It was fixed in llvm r217020.
Jason Molendae589e7e2014-12-08 03:09:00 +0000773 return false;
774 }
775 break;
776
Jason Molendae589e7e2014-12-08 03:09:00 +0000777 case UNWIND_X86_64_MODE_STACK_IMMD:
778 {
Jason Molenda19ba9fb2014-12-22 11:02:02 +0000779 uint32_t stack_size = EXTRACT_BITS (function_info.encoding, UNWIND_X86_64_FRAMELESS_STACK_SIZE);
780 uint32_t register_count = EXTRACT_BITS (function_info.encoding, UNWIND_X86_64_FRAMELESS_STACK_REG_COUNT);
781 uint32_t permutation = EXTRACT_BITS (function_info.encoding, UNWIND_X86_64_FRAMELESS_STACK_REG_PERMUTATION);
Jason Molendae589e7e2014-12-08 03:09:00 +0000782
Jason Molenda19ba9fb2014-12-22 11:02:02 +0000783 if (mode == UNWIND_X86_64_MODE_STACK_IND && function_info.valid_range_offset_start != 0)
Jason Molendae589e7e2014-12-08 03:09:00 +0000784 {
Jason Molenda19ba9fb2014-12-22 11:02:02 +0000785 uint32_t stack_adjust = EXTRACT_BITS (function_info.encoding, UNWIND_X86_64_FRAMELESS_STACK_ADJUST);
Jason Molendae589e7e2014-12-08 03:09:00 +0000786
787 // offset into the function instructions; 0 == beginning of first instruction
Jason Molenda19ba9fb2014-12-22 11:02:02 +0000788 uint32_t offset_to_subl_insn = EXTRACT_BITS (function_info.encoding, UNWIND_X86_64_FRAMELESS_STACK_SIZE);
Jason Molendae589e7e2014-12-08 03:09:00 +0000789
Jason Molenda19ba9fb2014-12-22 11:02:02 +0000790 SectionList *sl = m_objfile.GetSectionList ();
791 if (sl)
792 {
793 ProcessSP process_sp = target.GetProcessSP();
794 if (process_sp)
795 {
796 Address subl_payload_addr (function_info.valid_range_offset_start, sl);
797 subl_payload_addr.Slide (offset_to_subl_insn);
798 Error error;
799 uint64_t large_stack_size = process_sp->ReadUnsignedIntegerFromMemory (subl_payload_addr.GetLoadAddress (&target),
800 4, 0, error);
801 if (large_stack_size != 0 && error.Success ())
802 {
803 // Got the large stack frame size correctly - use it
804 stack_size = large_stack_size + (stack_adjust * wordsize);
805 }
806 else
807 {
808 return false;
809 }
810 }
811 else
812 {
813 return false;
814 }
815 }
816 else
817 {
818 return false;
819 }
Jason Molendae589e7e2014-12-08 03:09:00 +0000820 }
Jason Molendae589e7e2014-12-08 03:09:00 +0000821
Pavel Labathab970f52015-02-23 10:19:16 +0000822 int32_t offset = mode == UNWIND_X86_64_MODE_STACK_IND ? stack_size : stack_size * wordsize;
823 row->GetCFAValue().SetIsRegisterPlusOffset (x86_64_eh_regnum::rsp, offset);
Jason Molenda8122bb02015-01-23 01:02:32 +0000824
Jason Molenda19ba9fb2014-12-22 11:02:02 +0000825 row->SetOffset (0);
826 row->SetRegisterLocationToAtCFAPlusOffset (x86_64_eh_regnum::rip, wordsize * -1, true);
827 row->SetRegisterLocationToIsCFAPlusOffset (x86_64_eh_regnum::rsp, 0, true);
828
829 if (register_count > 0)
Jason Molendae589e7e2014-12-08 03:09:00 +0000830 {
831
832 // We need to include (up to) 6 registers in 10 bits.
833 // That would be 18 bits if we just used 3 bits per reg to indicate
834 // the order they're saved on the stack.
835 //
836 // This is done with Lehmer code permutation, e.g. see
837 // http://stackoverflow.com/questions/1506078/fast-permutation-number-permutation-mapping-algorithms
838 int permunreg[6];
839
840 // This decodes the variable-base number in the 10 bits
841 // and gives us the Lehmer code sequence which can then
842 // be decoded.
843
844 switch (register_count)
845 {
846 case 6:
847 permunreg[0] = permutation/120; // 120 == 5!
848 permutation -= (permunreg[0]*120);
849 permunreg[1] = permutation/24; // 24 == 4!
850 permutation -= (permunreg[1]*24);
851 permunreg[2] = permutation/6; // 6 == 3!
852 permutation -= (permunreg[2]*6);
853 permunreg[3] = permutation/2; // 2 == 2!
854 permutation -= (permunreg[3]*2);
855 permunreg[4] = permutation; // 1 == 1!
856 permunreg[5] = 0;
857 break;
858 case 5:
859 permunreg[0] = permutation/120;
860 permutation -= (permunreg[0]*120);
861 permunreg[1] = permutation/24;
862 permutation -= (permunreg[1]*24);
863 permunreg[2] = permutation/6;
864 permutation -= (permunreg[2]*6);
865 permunreg[3] = permutation/2;
866 permutation -= (permunreg[3]*2);
867 permunreg[4] = permutation;
868 break;
869 case 4:
870 permunreg[0] = permutation/60;
871 permutation -= (permunreg[0]*60);
872 permunreg[1] = permutation/12;
873 permutation -= (permunreg[1]*12);
874 permunreg[2] = permutation/3;
875 permutation -= (permunreg[2]*3);
876 permunreg[3] = permutation;
877 break;
878 case 3:
879 permunreg[0] = permutation/20;
880 permutation -= (permunreg[0]*20);
881 permunreg[1] = permutation/4;
882 permutation -= (permunreg[1]*4);
883 permunreg[2] = permutation;
884 break;
885 case 2:
886 permunreg[0] = permutation/5;
887 permutation -= (permunreg[0]*5);
888 permunreg[1] = permutation;
889 break;
890 case 1:
891 permunreg[0] = permutation;
892 break;
893 }
894
895 // Decode the Lehmer code for this permutation of
896 // the registers v. http://en.wikipedia.org/wiki/Lehmer_code
897
898 int registers[6];
899 bool used[7] = { false, false, false, false, false, false, false };
Andy Gibbs3acfe1a2014-12-29 13:03:19 +0000900 for (uint32_t i = 0; i < register_count; i++)
Jason Molendae589e7e2014-12-08 03:09:00 +0000901 {
902 int renum = 0;
903 for (int j = 1; j < 7; j++)
904 {
905 if (used[j] == false)
906 {
907 if (renum == permunreg[i])
908 {
909 registers[i] = j;
910 used[j] = true;
911 break;
912 }
913 renum++;
914 }
915 }
916 }
917
Jason Molendae589e7e2014-12-08 03:09:00 +0000918 uint32_t saved_registers_offset = 1;
Jason Molendae589e7e2014-12-08 03:09:00 +0000919 saved_registers_offset++;
920
921 for (int i = (sizeof (registers) / sizeof (int)) - 1; i >= 0; i--)
922 {
923 switch (registers[i])
924 {
925 case UNWIND_X86_64_REG_NONE:
926 break;
927 case UNWIND_X86_64_REG_RBX:
Jason Molendae589e7e2014-12-08 03:09:00 +0000928 case UNWIND_X86_64_REG_R12:
Jason Molendae589e7e2014-12-08 03:09:00 +0000929 case UNWIND_X86_64_REG_R13:
Jason Molendae589e7e2014-12-08 03:09:00 +0000930 case UNWIND_X86_64_REG_R14:
Jason Molendae589e7e2014-12-08 03:09:00 +0000931 case UNWIND_X86_64_REG_R15:
Jason Molendae589e7e2014-12-08 03:09:00 +0000932 case UNWIND_X86_64_REG_RBP:
Jason Molenda8122bb02015-01-23 01:02:32 +0000933 row->SetRegisterLocationToAtCFAPlusOffset (translate_to_eh_frame_regnum_x86_64 (registers[i]), wordsize * -saved_registers_offset, true);
934 saved_registers_offset++;
Jason Molenda19ba9fb2014-12-22 11:02:02 +0000935 break;
Jason Molendae589e7e2014-12-08 03:09:00 +0000936 }
Jason Molendae589e7e2014-12-08 03:09:00 +0000937 }
Jason Molendae589e7e2014-12-08 03:09:00 +0000938 }
Jason Molenda19ba9fb2014-12-22 11:02:02 +0000939 unwind_plan.AppendRow (row);
940 return true;
Jason Molendae589e7e2014-12-08 03:09:00 +0000941 }
942 break;
Jason Molendae589e7e2014-12-08 03:09:00 +0000943
944 case UNWIND_X86_64_MODE_DWARF:
945 {
946 return false;
947 }
948 break;
949
950 case 0:
951 {
952 return false;
953 }
954 break;
955 }
956 return false;
957}
958
959enum i386_eh_regnum {
960 eax = 0,
961 ecx = 1,
962 edx = 2,
963 ebx = 3,
964 ebp = 4,
965 esp = 5,
966 esi = 6,
967 edi = 7,
968 eip = 8 // this is officially the Return Address register number, but close enough
969};
970
971// Convert the compact_unwind_info.h register numbering scheme
972// to eRegisterKindGCC (eh_frame) register numbering scheme.
973uint32_t
974translate_to_eh_frame_regnum_i386 (uint32_t unwind_regno)
975{
976 switch (unwind_regno)
977 {
978 case UNWIND_X86_REG_EBX:
979 return i386_eh_regnum::ebx;
980 case UNWIND_X86_REG_ECX:
981 return i386_eh_regnum::ecx;
982 case UNWIND_X86_REG_EDX:
983 return i386_eh_regnum::edx;
984 case UNWIND_X86_REG_EDI:
985 return i386_eh_regnum::edi;
986 case UNWIND_X86_REG_ESI:
987 return i386_eh_regnum::esi;
988 case UNWIND_X86_REG_EBP:
989 return i386_eh_regnum::ebp;
990 default:
991 return LLDB_INVALID_REGNUM;
992 }
993}
994
995
996bool
997CompactUnwindInfo::CreateUnwindPlan_i386 (Target &target, FunctionInfo &function_info, UnwindPlan &unwind_plan, Address pc_or_function_start)
998{
999 unwind_plan.SetSourceName ("compact unwind info");
1000 unwind_plan.SetSourcedFromCompiler (eLazyBoolYes);
1001 unwind_plan.SetUnwindPlanValidAtAllInstructions (eLazyBoolNo);
1002 unwind_plan.SetRegisterKind (eRegisterKindGCC);
1003
1004 unwind_plan.SetLSDAAddress (function_info.lsda_address);
1005 unwind_plan.SetPersonalityFunctionPtr (function_info.personality_ptr_address);
1006
1007 UnwindPlan::RowSP row (new UnwindPlan::Row);
1008
1009 const int wordsize = 4;
1010 int mode = function_info.encoding & UNWIND_X86_MODE_MASK;
1011 switch (mode)
1012 {
1013 case UNWIND_X86_MODE_EBP_FRAME:
1014 {
Pavel Labathab970f52015-02-23 10:19:16 +00001015 row->GetCFAValue().SetIsRegisterPlusOffset (
1016 translate_to_eh_frame_regnum_i386 (UNWIND_X86_REG_EBP), 2 * wordsize);
Jason Molendae589e7e2014-12-08 03:09:00 +00001017 row->SetOffset (0);
1018 row->SetRegisterLocationToAtCFAPlusOffset (i386_eh_regnum::ebp, wordsize * -2, true);
1019 row->SetRegisterLocationToAtCFAPlusOffset (i386_eh_regnum::eip, wordsize * -1, true);
1020 row->SetRegisterLocationToIsCFAPlusOffset (i386_eh_regnum::esp, 0, true);
1021
1022 uint32_t saved_registers_offset = EXTRACT_BITS (function_info.encoding, UNWIND_X86_EBP_FRAME_OFFSET);
1023
1024 uint32_t saved_registers_locations = EXTRACT_BITS (function_info.encoding, UNWIND_X86_EBP_FRAME_REGISTERS);
1025
1026 saved_registers_offset += 2;
1027
1028 for (int i = 0; i < 5; i++)
1029 {
1030 uint32_t regnum = saved_registers_locations & 0x7;
1031 switch (regnum)
1032 {
1033 case UNWIND_X86_REG_NONE:
1034 break;
1035 case UNWIND_X86_REG_EBX:
1036 case UNWIND_X86_REG_ECX:
1037 case UNWIND_X86_REG_EDX:
1038 case UNWIND_X86_REG_EDI:
1039 case UNWIND_X86_REG_ESI:
1040 row->SetRegisterLocationToAtCFAPlusOffset (translate_to_eh_frame_regnum_i386 (regnum), wordsize * -saved_registers_offset, true);
1041 break;
1042 }
1043 saved_registers_offset--;
1044 saved_registers_locations >>= 3;
1045 }
1046 unwind_plan.AppendRow (row);
1047 return true;
1048 }
1049 break;
1050
1051 case UNWIND_X86_MODE_STACK_IND:
1052 case UNWIND_X86_MODE_STACK_IMMD:
Jason Molenda19ba9fb2014-12-22 11:02:02 +00001053 {
1054 uint32_t stack_size = EXTRACT_BITS (function_info.encoding, UNWIND_X86_FRAMELESS_STACK_SIZE);
1055 uint32_t register_count = EXTRACT_BITS (function_info.encoding, UNWIND_X86_FRAMELESS_STACK_REG_COUNT);
1056 uint32_t permutation = EXTRACT_BITS (function_info.encoding, UNWIND_X86_FRAMELESS_STACK_REG_PERMUTATION);
1057
1058 if (mode == UNWIND_X86_MODE_STACK_IND && function_info.valid_range_offset_start != 0)
1059 {
1060 uint32_t stack_adjust = EXTRACT_BITS (function_info.encoding, UNWIND_X86_FRAMELESS_STACK_ADJUST);
1061
1062 // offset into the function instructions; 0 == beginning of first instruction
1063 uint32_t offset_to_subl_insn = EXTRACT_BITS (function_info.encoding, UNWIND_X86_FRAMELESS_STACK_SIZE);
1064
1065 SectionList *sl = m_objfile.GetSectionList ();
1066 if (sl)
1067 {
1068 ProcessSP process_sp = target.GetProcessSP();
1069 if (process_sp)
1070 {
1071 Address subl_payload_addr (function_info.valid_range_offset_start, sl);
1072 subl_payload_addr.Slide (offset_to_subl_insn);
1073 Error error;
1074 uint64_t large_stack_size = process_sp->ReadUnsignedIntegerFromMemory (subl_payload_addr.GetLoadAddress (&target),
1075 4, 0, error);
1076 if (large_stack_size != 0 && error.Success ())
1077 {
1078 // Got the large stack frame size correctly - use it
1079 stack_size = large_stack_size + (stack_adjust * wordsize);
1080 }
1081 else
1082 {
1083 return false;
1084 }
1085 }
1086 else
1087 {
1088 return false;
1089 }
1090 }
1091 else
1092 {
1093 return false;
1094 }
1095 }
1096
Pavel Labathab970f52015-02-23 10:19:16 +00001097 int32_t offset = mode == UNWIND_X86_MODE_STACK_IND ? stack_size : stack_size * wordsize;
1098 row->GetCFAValue().SetIsRegisterPlusOffset (i386_eh_regnum::esp, offset);
Jason Molenda19ba9fb2014-12-22 11:02:02 +00001099 row->SetOffset (0);
1100 row->SetRegisterLocationToAtCFAPlusOffset (i386_eh_regnum::eip, wordsize * -1, true);
1101 row->SetRegisterLocationToIsCFAPlusOffset (i386_eh_regnum::esp, 0, true);
1102
1103 if (register_count > 0)
1104 {
1105
1106 // We need to include (up to) 6 registers in 10 bits.
1107 // That would be 18 bits if we just used 3 bits per reg to indicate
1108 // the order they're saved on the stack.
1109 //
1110 // This is done with Lehmer code permutation, e.g. see
1111 // http://stackoverflow.com/questions/1506078/fast-permutation-number-permutation-mapping-algorithms
1112 int permunreg[6];
1113
1114 // This decodes the variable-base number in the 10 bits
1115 // and gives us the Lehmer code sequence which can then
1116 // be decoded.
1117
1118 switch (register_count)
1119 {
1120 case 6:
1121 permunreg[0] = permutation/120; // 120 == 5!
1122 permutation -= (permunreg[0]*120);
1123 permunreg[1] = permutation/24; // 24 == 4!
1124 permutation -= (permunreg[1]*24);
1125 permunreg[2] = permutation/6; // 6 == 3!
1126 permutation -= (permunreg[2]*6);
1127 permunreg[3] = permutation/2; // 2 == 2!
1128 permutation -= (permunreg[3]*2);
1129 permunreg[4] = permutation; // 1 == 1!
1130 permunreg[5] = 0;
1131 break;
1132 case 5:
1133 permunreg[0] = permutation/120;
1134 permutation -= (permunreg[0]*120);
1135 permunreg[1] = permutation/24;
1136 permutation -= (permunreg[1]*24);
1137 permunreg[2] = permutation/6;
1138 permutation -= (permunreg[2]*6);
1139 permunreg[3] = permutation/2;
1140 permutation -= (permunreg[3]*2);
1141 permunreg[4] = permutation;
1142 break;
1143 case 4:
1144 permunreg[0] = permutation/60;
1145 permutation -= (permunreg[0]*60);
1146 permunreg[1] = permutation/12;
1147 permutation -= (permunreg[1]*12);
1148 permunreg[2] = permutation/3;
1149 permutation -= (permunreg[2]*3);
1150 permunreg[3] = permutation;
1151 break;
1152 case 3:
1153 permunreg[0] = permutation/20;
1154 permutation -= (permunreg[0]*20);
1155 permunreg[1] = permutation/4;
1156 permutation -= (permunreg[1]*4);
1157 permunreg[2] = permutation;
1158 break;
1159 case 2:
1160 permunreg[0] = permutation/5;
1161 permutation -= (permunreg[0]*5);
1162 permunreg[1] = permutation;
1163 break;
1164 case 1:
1165 permunreg[0] = permutation;
1166 break;
1167 }
1168
1169 // Decode the Lehmer code for this permutation of
1170 // the registers v. http://en.wikipedia.org/wiki/Lehmer_code
1171
1172 int registers[6];
1173 bool used[7] = { false, false, false, false, false, false, false };
Andy Gibbs3acfe1a2014-12-29 13:03:19 +00001174 for (uint32_t i = 0; i < register_count; i++)
Jason Molenda19ba9fb2014-12-22 11:02:02 +00001175 {
1176 int renum = 0;
1177 for (int j = 1; j < 7; j++)
1178 {
1179 if (used[j] == false)
1180 {
1181 if (renum == permunreg[i])
1182 {
1183 registers[i] = j;
1184 used[j] = true;
1185 break;
1186 }
1187 renum++;
1188 }
1189 }
1190 }
1191
1192 uint32_t saved_registers_offset = 1;
1193 saved_registers_offset++;
1194
1195 for (int i = (sizeof (registers) / sizeof (int)) - 1; i >= 0; i--)
1196 {
1197 switch (registers[i])
1198 {
1199 case UNWIND_X86_REG_NONE:
1200 break;
1201 case UNWIND_X86_REG_EBX:
1202 case UNWIND_X86_REG_ECX:
1203 case UNWIND_X86_REG_EDX:
1204 case UNWIND_X86_REG_EDI:
1205 case UNWIND_X86_REG_ESI:
1206 case UNWIND_X86_REG_EBP:
Jason Molenda8122bb02015-01-23 01:02:32 +00001207 row->SetRegisterLocationToAtCFAPlusOffset (translate_to_eh_frame_regnum_i386 (registers[i]), wordsize * -saved_registers_offset, true);
1208 saved_registers_offset++;
Jason Molenda19ba9fb2014-12-22 11:02:02 +00001209 break;
1210 }
Jason Molenda19ba9fb2014-12-22 11:02:02 +00001211 }
1212 }
1213
1214 unwind_plan.AppendRow (row);
1215 return true;
1216 }
1217 break;
1218
Jason Molendae589e7e2014-12-08 03:09:00 +00001219 case UNWIND_X86_MODE_DWARF:
1220 {
1221 return false;
1222 }
1223 break;
1224 }
1225 return false;
1226}