Repository navigation
Expand file tree
/
Copy pathremote-unwind.cpp
More file actions
2718 lines (2427 loc) · 96.3 KB
/
Copy pathremote-unwind.cpp
File metadata and controls
2718 lines (2427 loc) · 96.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
/*++
Module Name:
remote-unwind.cpp
Abstract:
Implementation of out of context unwind using libunwind8
remote unwind API.
This file contains code based on libunwind8
Copyright (c) 2003-2005 Hewlett-Packard Development Company, L.P.
Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--*/
#ifdef HOST_UNIX
#include "config.h"
#include "pal/palinternal.h"
#include "pal/dbgmsg.h"
#include "pal/critsect.h"
#include "pal/debug.h"
#include "pal_endian.h"
#include "pal.h"
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include <dlfcn.h>
#ifdef __APPLE__
#include <mach/mach.h>
#include <mach-o/loader.h>
#include <mach-o/nlist.h>
#include <mach-o/dyld_images.h>
#include "compact_unwind_encoding.h"
#define MACOS_ARM64_POINTER_AUTH_MASK 0x7fffffffffffull
#endif
#include <libunwind.h>
SET_DEFAULT_DEBUG_CHANNEL(EXCEPT);
#define TRACE_VERBOSE
#include "crosscomp.h"
#define KNONVOLATILE_CONTEXT_POINTERS T_KNONVOLATILE_CONTEXT_POINTERS
#define CONTEXT T_CONTEXT
#else // HOST_UNIX
#include <windows.h>
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include <libunwind.h>
#include "debugmacros.h"
#include "crosscomp.h"
#define KNONVOLATILE_CONTEXT_POINTERS T_KNONVOLATILE_CONTEXT_POINTERS
#define CONTEXT T_CONTEXT
typedef BOOL(*UnwindReadMemoryCallback)(PVOID address, PVOID buffer, SIZE_T size);
#define ASSERT(x, ...)
#define TRACE(x, ...)
#undef ERROR
#define ERROR(x, ...)
#ifdef TARGET_64BIT
#define ElfW(foo) Elf64_ ## foo
#else // TARGET_64BIT
#define ElfW(foo) Elf32_ ## foo
#endif // TARGET_64BIT
#define PALAPI
#endif // HOST_UNIX
#ifdef HAVE_UNW_GET_ACCESSORS
#ifdef HOST_UNIX
#include <link.h>
#endif // HOST_UNIX
#include <elf.h>
#if defined(TARGET_X86) || defined(TARGET_ARM)
#define PRIx PRIx32
#define PRIu PRIu32
#define PRId PRId32
#define PRIA "08"
#define PRIxA PRIA PRIx
#elif defined(TARGET_AMD64) || defined(TARGET_ARM64) || defined(TARGET_S390X) || defined(TARGET_LOONGARCH64) || defined(TARGET_POWERPC64) || defined(TARGET_RISCV64)
#define PRIx PRIx64
#define PRIu PRIu64
#define PRId PRId64
#define PRIA "016"
#define PRIxA PRIA PRIx
#endif
#ifndef ElfW
#define ElfW(foo) Elf_ ## foo
#endif
#define Ehdr ElfW(Ehdr)
#define Phdr ElfW(Phdr)
#define Shdr ElfW(Shdr)
#define Nhdr ElfW(Nhdr)
#define Dyn ElfW(Dyn)
#ifndef FEATURE_USE_SYSTEM_LIBUNWIND
extern "C" int
unw_get_proc_info_in_range(
unw_word_t start_ip,
unw_word_t end_ip,
unw_word_t eh_frame_table,
unw_word_t eh_frame_table_len,
unw_word_t exidx_frame_table,
unw_word_t exidx_frame_table_len,
unw_addr_space_t as,
unw_word_t ip,
unw_proc_info_t *pi,
int need_unwind_info,
void *arg);
#endif // FEATURE_USE_SYSTEM_LIBUNWIND
#endif // HAVE_UNW_GET_ACCESSORS
#if defined(__APPLE__) || defined(HAVE_UNW_GET_ACCESSORS)
typedef struct _libunwindInfo
{
SIZE_T BaseAddress;
CONTEXT *Context;
ULONG64 FunctionStart;
UnwindReadMemoryCallback ReadMemory;
} libunwindInfo;
#ifdef HOST_UNIX
#define EXTRACT_BITS(value, mask) ((value >> __builtin_ctz(mask)) & (((1 << __builtin_popcount(mask))) - 1))
#define DW_EH_VERSION 1
// DWARF Pointer-Encoding (PEs).
//
// Pointer-Encodings were invented for the GCC exception-handling
// support for C++, but they represent a rather generic way of
// describing the format in which an address/pointer is stored.
// The Pointer-Encoding format is partially documented in Linux Base
// Spec v1.3 (http://www.linuxbase.org/spec/).
// These defines and struct (dwarf_cie_info) were copied from libunwind's dwarf.h
#define DW_EH_PE_FORMAT_MASK 0x0f // format of the encoded value
#define DW_EH_PE_APPL_MASK 0x70 // how the value is to be applied
#define DW_EH_PE_indirect 0x80 // Flag bit. If set, the resulting pointer is the
// address of the word that contains the final address
// Pointer-encoding formats
#define DW_EH_PE_omit 0xff
#define DW_EH_PE_ptr 0x00 // pointer-sized unsigned value
#define DW_EH_PE_uleb128 0x01 // unsigned LE base-128 value
#define DW_EH_PE_udata2 0x02 // unsigned 16-bit value
#define DW_EH_PE_udata4 0x03 // unsigned 32-bit value
#define DW_EH_PE_udata8 0x04 // unsigned 64-bit value
#define DW_EH_PE_sleb128 0x09 // signed LE base-128 value
#define DW_EH_PE_sdata2 0x0a // signed 16-bit value
#define DW_EH_PE_sdata4 0x0b // signed 32-bit value
#define DW_EH_PE_sdata8 0x0c // signed 64-bit value
// Pointer-encoding application
#define DW_EH_PE_absptr 0x00 // absolute value
#define DW_EH_PE_pcrel 0x10 // rel. to addr. of encoded value
#define DW_EH_PE_textrel 0x20 // text-relative (GCC-specific???)
#define DW_EH_PE_datarel 0x30 // data-relative
// The following are not documented by LSB v1.3, yet they are used by
// GCC, presumably they aren't documented by LSB since they aren't
// used on Linux
#define DW_EH_PE_funcrel 0x40 // start-of-procedure-relative
#define DW_EH_PE_aligned 0x50 // aligned pointer
#define DWARF_CIE_VERSION 3 // GCC emits version 1???
// DWARF frame header
typedef struct __attribute__((packed)) _eh_frame_hdr
{
unsigned char version;
unsigned char eh_frame_ptr_enc;
unsigned char fde_count_enc;
unsigned char table_enc;
// The rest of the header is variable-length and consists of the
// following members:
//
// encoded_t eh_frame_ptr;
// encoded_t fde_count;
// struct
// {
// encoded_t start_ip; // first address covered by this FDE
// encoded_t fde_offset; // offset of the FDE
// } binary_search_table[fde_count];
} eh_frame_hdr;
// "DW_EH_PE_datarel|DW_EH_PE_sdata4" encoded fde table entry
typedef struct table_entry
{
int32_t start_ip;
int32_t fde_offset;
} table_entry_t;
// DWARF unwind info
typedef struct dwarf_cie_info
{
unw_word_t cie_instr_start; // start addr. of CIE "initial_instructions"
unw_word_t cie_instr_end; // end addr. of CIE "initial_instructions"
unw_word_t fde_instr_start; // start addr. of FDE "instructions"
unw_word_t fde_instr_end; // end addr. of FDE "instructions"
unw_word_t code_align; // code-alignment factor
unw_word_t data_align; // data-alignment factor
unw_word_t ret_addr_column; // column of return-address register
unw_word_t handler; // address of personality-routine
uint16_t abi;
uint16_t tag;
uint8_t fde_encoding;
uint8_t lsda_encoding;
unsigned int sized_augmentation : 1;
unsigned int have_abi_marker : 1;
unsigned int signal_frame : 1;
} dwarf_cie_info_t;
static bool
ReadValue8(
const libunwindInfo* info,
unw_word_t* addr,
uint8_t* valp)
{
uint8_t value;
if (!info->ReadMemory((PVOID)*addr, &value, sizeof(value))) {
return false;
}
*addr += sizeof(value);
*valp = value;
return true;
}
static bool
ReadValue16(
const libunwindInfo* info,
unw_word_t* addr,
uint16_t* valp)
{
uint16_t value;
if (!info->ReadMemory((PVOID)*addr, &value, sizeof(value))) {
return false;
}
*addr += sizeof(value);
*valp = VAL16(value);
return true;
}
static bool
ReadValue32(
const libunwindInfo* info,
unw_word_t* addr,
uint32_t* valp)
{
uint32_t value;
if (!info->ReadMemory((PVOID)*addr, &value, sizeof(value))) {
return false;
}
*addr += sizeof(value);
*valp = VAL32(value);
return true;
}
static bool
ReadValue64(
const libunwindInfo* info,
unw_word_t* addr,
uint64_t* valp)
{
uint64_t value;
if (!info->ReadMemory((PVOID)*addr, &value, sizeof(value))) {
return false;
}
*addr += sizeof(value);
*valp = VAL64(value);
return true;
}
static bool
ReadPointer(
const libunwindInfo* info,
unw_word_t* addr,
unw_word_t* valp)
{
#ifdef TARGET_64BIT
uint64_t val64;
if (ReadValue64(info, addr, &val64)) {
*valp = val64;
return true;
}
#else
uint32_t val32;
if (ReadValue32(info, addr, &val32)) {
*valp = val32;
return true;
}
#endif
return false;
}
// Read a unsigned "little-endian base 128" value. See Chapter 7.6 of DWARF spec v3.
static bool
ReadULEB128(
const libunwindInfo* info,
unw_word_t* addr,
unw_word_t* valp)
{
unw_word_t value = 0;
unsigned char byte;
int shift = 0;
do
{
if (!ReadValue8(info, addr, &byte)) {
return false;
}
value |= ((unw_word_t)byte & 0x7f) << shift;
shift += 7;
} while (byte & 0x80);
*valp = value;
return true;
}
// Read a signed "little-endian base 128" value. See Chapter 7.6 of DWARF spec v3.
static bool
ReadSLEB128(
const libunwindInfo* info,
unw_word_t* addr,
unw_word_t* valp)
{
unw_word_t value = 0;
unsigned char byte;
int shift = 0;
do
{
if (!ReadValue8(info, addr, &byte)) {
return false;
}
value |= ((unw_word_t)byte & 0x7f) << shift;
shift += 7;
} while (byte & 0x80);
if (((size_t)shift < (8 * sizeof(unw_word_t))) && ((byte & 0x40) != 0)) {
value |= ((unw_word_t)-1) << shift;
}
*valp = value;
return true;
}
static bool
ReadEncodedPointer(
const libunwindInfo* info,
unw_word_t* addr,
unsigned char encoding,
unw_word_t funcRel,
unw_word_t* valp)
{
unw_word_t initialAddr = *addr;
uint16_t value16;
uint32_t value32;
uint64_t value64;
unw_word_t value;
if (encoding == DW_EH_PE_omit)
{
*valp = 0;
return true;
}
else if (encoding == DW_EH_PE_aligned)
{
int size = sizeof(unw_word_t);
*addr = (initialAddr + size - 1) & -size;
return ReadPointer(info, addr, valp);
}
switch (encoding & DW_EH_PE_FORMAT_MASK)
{
case DW_EH_PE_ptr:
if (!ReadPointer(info, addr, &value)) {
return false;
}
break;
case DW_EH_PE_uleb128:
if (!ReadULEB128(info, addr, &value)) {
return false;
}
break;
case DW_EH_PE_sleb128:
if (!ReadSLEB128(info, addr, &value)) {
return false;
}
break;
case DW_EH_PE_udata2:
if (!ReadValue16(info, addr, &value16)) {
return false;
}
value = value16;
break;
case DW_EH_PE_udata4:
if (!ReadValue32(info, addr, &value32)) {
return false;
}
value = value32;
break;
case DW_EH_PE_udata8:
if (!ReadValue64(info, addr, &value64)) {
return false;
}
value = value64;
break;
case DW_EH_PE_sdata2:
if (!ReadValue16(info, addr, &value16)) {
return false;
}
value = (int16_t)value16;
break;
case DW_EH_PE_sdata4:
if (!ReadValue32(info, addr, &value32)) {
return false;
}
value = (int32_t)value32;
break;
case DW_EH_PE_sdata8:
if (!ReadValue64(info, addr, &value64)) {
return false;
}
value = (int64_t)value64;
break;
default:
ASSERT("ReadEncodedPointer: invalid encoding format %x\n", encoding);
return false;
}
// 0 is a special value and always absolute
if (value == 0) {
*valp = 0;
return true;
}
switch (encoding & DW_EH_PE_APPL_MASK)
{
case DW_EH_PE_absptr:
break;
case DW_EH_PE_pcrel:
value += initialAddr;
break;
case DW_EH_PE_funcrel:
_ASSERTE(funcRel != UINTPTR_MAX);
value += funcRel;
break;
case DW_EH_PE_textrel:
case DW_EH_PE_datarel:
default:
ASSERT("ReadEncodedPointer: invalid application type %x\n", encoding);
return false;
}
if (encoding & DW_EH_PE_indirect)
{
unw_word_t indirect_addr = value;
if (!ReadPointer(info, &indirect_addr, &value)) {
return false;
}
}
*valp = value;
return true;
}
#endif // HOST_UNIX
#if defined(__APPLE__) || defined(FEATURE_USE_SYSTEM_LIBUNWIND)
template<class T>
static bool
BinarySearchEntries(
const libunwindInfo* info,
int32_t ip,
unw_word_t tableAddr,
size_t tableCount,
T* entry,
T* entryNext,
bool compressed,
bool* found)
{
size_t low, high, mid;
unw_word_t addr;
int32_t functionOffset;
*found = false;
static_assert_no_msg(sizeof(T) >= sizeof(uint32_t));
#ifdef __APPLE__
static_assert_no_msg(offsetof(unwind_info_section_header_index_entry, functionOffset) == 0);
static_assert_no_msg(sizeof(unwind_info_section_header_index_entry::functionOffset) == sizeof(uint32_t));
static_assert_no_msg(offsetof(unwind_info_regular_second_level_entry, functionOffset) == 0);
static_assert_no_msg(sizeof(unwind_info_regular_second_level_entry::functionOffset) == sizeof(uint32_t));
static_assert_no_msg(offsetof(unwind_info_section_header_lsda_index_entry, functionOffset) == 0);
static_assert_no_msg(sizeof(unwind_info_section_header_lsda_index_entry::functionOffset) == sizeof(uint32_t));
#endif // __APPLE__
// Do a binary search on table
for (low = 0, high = tableCount; low < high;)
{
mid = (low + high) / 2;
// Assumes that the first uint32_t in T is the offset to compare
addr = tableAddr + (mid * sizeof(T));
if (!ReadValue32(info, &addr, (uint32_t*)&functionOffset)) {
return false;
}
#ifdef __APPLE__
if (compressed) {
functionOffset = UNWIND_INFO_COMPRESSED_ENTRY_FUNC_OFFSET(functionOffset);
}
#endif // __APPLE__
if (ip < functionOffset) {
high = mid;
}
else {
low = mid + 1;
}
}
if (high > 0)
{
*found = true;
addr = tableAddr + (high * sizeof(T));
if (!info->ReadMemory((PVOID)addr, entryNext, sizeof(T))) {
return false;
}
}
else
{
// When the ip isn't found return the last entry in the table
high = tableCount;
// No next entry
memset(entryNext, 0, sizeof(T));
}
addr = tableAddr + ((high - 1) * sizeof(T));
if (!info->ReadMemory((PVOID)addr, entry, sizeof(T))) {
return false;
}
return true;
}
static bool
ParseCie(
const libunwindInfo* info,
unw_word_t addr,
dwarf_cie_info_t* dci)
{
uint8_t ch, version, fdeEncoding, handlerEncoding;
unw_word_t cieLength, cieEndAddr;
uint32_t value32;
uint64_t value64;
memset(dci, 0, sizeof(dwarf_cie_info_t));
// Pick appropriate default for FDE-encoding. DWARF spec says
// start-IP (initial_location) and the code-size (address_range) are
// "address-unit sized constants". The `R' augmentation can be used
// to override this, but by default, we pick an address-sized unit
// for fde_encoding.
#if TARGET_64BIT
fdeEncoding = DW_EH_PE_udata8;
#else
fdeEncoding = DW_EH_PE_udata4;
#endif
dci->lsda_encoding = DW_EH_PE_omit;
dci->handler = 0;
if (!ReadValue32(info, &addr, &value32)) {
return false;
}
if (value32 != 0xffffffff)
{
// The CIE is in the 32-bit DWARF format
uint32_t cieId;
// DWARF says CIE id should be 0xffffffff, but in .eh_frame, it's 0
const uint32_t expectedId = 0;
cieLength = value32;
cieEndAddr = addr + cieLength;
if (!ReadValue32(info, &addr, &cieId)) {
return false;
}
if (cieId != expectedId) {
ASSERT("ParseCie: unexpected cie id %x\n", cieId);
return false;
}
}
else
{
// The CIE is in the 64-bit DWARF format
uint64_t cieId;
// DWARF says CIE id should be 0xffffffffffffffff, but in .eh_frame, it's 0
const uint64_t expectedId = 0;
if (!ReadValue64(info, &addr, &value64)) {
return false;
}
cieLength = value64;
cieEndAddr = addr + cieLength;
if (!ReadValue64(info, &addr, &cieId)) {
return false;
}
if (cieId != expectedId) {
ASSERT("ParseCie: unexpected cie id %lx\n", cieId);
return false;
}
}
dci->cie_instr_end = cieEndAddr;
if (!ReadValue8(info, &addr, &version)) {
return false;
}
if (version != 1 && version != DWARF_CIE_VERSION) {
ASSERT("ParseCie: invalid cie version %x\n", version);
return false;
}
// Read the augmentation string
uint8_t augmentationString[8];
memset(augmentationString, 0, sizeof(augmentationString));
for (size_t i = 0; i < sizeof(augmentationString); i++)
{
if (!ReadValue8(info, &addr, &ch)) {
return false;
}
if (ch == 0) {
break;
}
augmentationString[i] = ch;
}
// Read the code and data alignment
if (!ReadULEB128(info, &addr, &dci->code_align)) {
return false;
}
if (!ReadSLEB128(info, &addr, &dci->data_align)) {
return false;
}
// Read the return-address column either as a u8 or as a uleb128
if (version == 1)
{
if (!ReadValue8(info, &addr, &ch)) {
return false;
}
dci->ret_addr_column = ch;
}
else
{
if (!ReadULEB128(info, &addr, &dci->ret_addr_column)) {
return false;
}
}
// Parse the augmentation string
for (size_t i = 0; i < sizeof(augmentationString); i++)
{
bool done = false;
unw_word_t augmentationSize;
switch (augmentationString[i])
{
case '\0':
done = true;
break;
case 'z':
dci->sized_augmentation = 1;
if (!ReadULEB128(info, &addr, &augmentationSize)) {
return false;
}
break;
case 'L':
// read the LSDA pointer-encoding format
if (!ReadValue8(info, &addr, &ch)) {
return false;
}
dci->lsda_encoding = ch;
break;
case 'R':
// read the FDE pointer-encoding format
if (!ReadValue8(info, &addr, &fdeEncoding)) {
return false;
}
break;
case 'P':
// read the personality-routine pointer-encoding format
if (!ReadValue8(info, &addr, &handlerEncoding)) {
return false;
}
if (!ReadEncodedPointer(info, &addr, handlerEncoding, UINTPTR_MAX, &dci->handler)) {
return false;
}
break;
case 'S':
// This is a signal frame
dci->signal_frame = 1;
// Temporarily set it to one so dwarf_parse_fde() knows that
// it should fetch the actual ABI/TAG pair from the FDE.
dci->have_abi_marker = 1;
break;
default:
if (dci->sized_augmentation) {
// If we have the size of the augmentation body, we can skip
// over the parts that we don't understand, so we're OK
done = true;
break;
}
ASSERT("ParseCie: unexpected argumentation string '%s'\n", augmentationString[i]);
return false;
}
if (done) {
break;
}
}
dci->fde_encoding = fdeEncoding;
dci->cie_instr_start = addr;
return true;
}
static bool
ExtractFde(
const libunwindInfo* info,
unw_word_t* addr,
unw_word_t* fdeEndAddr,
unw_word_t* ipStart,
unw_word_t* ipEnd,
dwarf_cie_info_t* dci)
{
unw_word_t cieOffsetAddr, cieAddr;
uint32_t value32;
uint64_t value64;
*fdeEndAddr = 0;
*ipStart = UINT64_MAX;
*ipEnd = 0;
if (!ReadValue32(info, addr, &value32)) {
return false;
}
if (value32 != 0xffffffff)
{
int32_t cieOffset = 0;
// In some configurations, an FDE with a 0 length indicates the end of the FDE-table
if (value32 == 0) {
return false;
}
// the FDE is in the 32-bit DWARF format */
*fdeEndAddr = *addr + value32;
cieOffsetAddr = *addr;
if (!ReadValue32(info, addr, (uint32_t*)&cieOffset)) {
return false;
}
// Ignore CIEs (happens during linear search)
if (cieOffset == 0) {
return true;
}
// DWARF says that the CIE_pointer in the FDE is a .debug_frame-relative offset,
// but the GCC-generated .eh_frame sections instead store a "pcrelative" offset,
// which is just as fine as it's self-contained
cieAddr = cieOffsetAddr - cieOffset;
}
else
{
int64_t cieOffset = 0;
// the FDE is in the 64-bit DWARF format */
if (!ReadValue64(info, addr, (uint64_t*)&value64)) {
return false;
}
*fdeEndAddr = *addr + value64;
cieOffsetAddr = *addr;
if (!ReadValue64(info, addr, (uint64_t*)&cieOffset)) {
return false;
}
// Ignore CIEs (happens during linear search)
if (cieOffset == 0) {
return true;
}
// DWARF says that the CIE_pointer in the FDE is a .debug_frame-relative offset,
// but the GCC-generated .eh_frame sections instead store a "pcrelative" offset,
// which is just as fine as it's self-contained
cieAddr = (unw_word_t)((uint64_t)cieOffsetAddr - cieOffset);
}
if (!ParseCie(info, cieAddr, dci)) {
return false;
}
unw_word_t start, range;
if (!ReadEncodedPointer(info, addr, dci->fde_encoding, UINTPTR_MAX, &start)) {
return false;
}
// IP-range has same encoding as FDE pointers, except that it's always an absolute value
uint8_t ipRangeEncoding = dci->fde_encoding & DW_EH_PE_FORMAT_MASK;
if (!ReadEncodedPointer(info, addr, ipRangeEncoding, UINTPTR_MAX, &range)) {
return false;
}
*ipStart = start;
*ipEnd = start + range;
return true;
}
static bool
ExtractProcInfoFromFde(
const libunwindInfo* info,
unw_word_t* addrp,
unw_proc_info_t *pip,
int need_unwind_info)
{
unw_word_t addr = *addrp;
unw_word_t ipStart, ipEnd;
unw_word_t fdeEndAddr;
dwarf_cie_info_t dci;
if (!ExtractFde(info, &addr, &fdeEndAddr, &ipStart, &ipEnd, &dci)) {
return false;
}
*addrp = fdeEndAddr;
pip->start_ip = ipStart;
pip->end_ip = ipEnd;
pip->handler = dci.handler;
unw_word_t augmentationSize, augmentationEndAddr;
if (dci.sized_augmentation) {
if (!ReadULEB128(info, &addr, &augmentationSize)) {
return false;
}
augmentationEndAddr = addr + augmentationSize;
}
// Read language specific data area address
if (!ReadEncodedPointer(info, &addr, dci.lsda_encoding, pip->start_ip, &pip->lsda)) {
return false;
}
// Now fill out the proc info if requested
if (need_unwind_info)
{
if (dci.have_abi_marker)
{
if (!ReadValue16(info, &addr, &dci.abi)) {
return false;
}
if (!ReadValue16(info, &addr, &dci.tag)) {
return false;
}
}
if (dci.sized_augmentation) {
dci.fde_instr_start = augmentationEndAddr;
}
else {
dci.fde_instr_start = addr;
}
dci.fde_instr_end = fdeEndAddr;
pip->format = UNW_INFO_FORMAT_TABLE;
pip->unwind_info_size = sizeof(dci);
pip->unwind_info = malloc(sizeof(dci));
if (pip->unwind_info == nullptr) {
return -UNW_ENOMEM;
}
memcpy(pip->unwind_info, &dci, sizeof(dci));
}
return true;
}
#ifdef __APPLE__
static bool
SearchCompactEncodingSection(
libunwindInfo* info,
unw_word_t ip,
unw_word_t compactUnwindSectionAddr,
unw_proc_info_t *pip)
{
unwind_info_section_header sectionHeader;
if (!info->ReadMemory((PVOID)compactUnwindSectionAddr, §ionHeader, sizeof(sectionHeader))) {
return false;
}
int32_t offset = ip - info->BaseAddress;
TRACE("Unwind %p offset %08x ver %d common off: %08x common cnt: %d pers off: %08x pers cnt: %d index off: %08x index cnt: %d\n",
(void*)compactUnwindSectionAddr,
offset,
sectionHeader.version,
sectionHeader.commonEncodingsArraySectionOffset,
sectionHeader.commonEncodingsArrayCount,
sectionHeader.personalityArraySectionOffset,
sectionHeader.personalityArrayCount,
sectionHeader.indexSectionOffset,
sectionHeader.indexCount);
if (sectionHeader.version != UNWIND_SECTION_VERSION) {
return false;
}
unwind_info_section_header_index_entry entry;
unwind_info_section_header_index_entry entryNext;
bool found;
if (!BinarySearchEntries(info, offset, compactUnwindSectionAddr + sectionHeader.indexSectionOffset, sectionHeader.indexCount, &entry, &entryNext, false, &found)) {
return false;
}
if (!found) {
ERROR("Top level index not found\n");
return false;
}
uint32_t firstLevelFunctionOffset = entry.functionOffset;
uint32_t firstLevelNextPageFunctionOffset = entryNext.functionOffset;
unw_word_t secondLevelAddr = compactUnwindSectionAddr + entry.secondLevelPagesSectionOffset;
unw_word_t lsdaArrayStartAddr = compactUnwindSectionAddr + entry.lsdaIndexArraySectionOffset;
unw_word_t lsdaArrayEndAddr = compactUnwindSectionAddr + entryNext.lsdaIndexArraySectionOffset;
uint32_t encoding = 0;