-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathunwind.c
1537 lines (1424 loc) · 46.3 KB
/
unwind.c
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
/* -*- linux-c -*-
* kernel stack unwinding
* Copyright (C) 2008-2016 Red Hat Inc.
*
* Based on old kernel code that is
* Copyright (C) 2002-2006 Novell, Inc.
* Jan Beulich <[email protected]>
*
* This code is released under version 2 of the GNU GPL.
*
* This code currently does stack unwinding in the kernel and modules.
* It has been extended to handle userspace unwinding using systemtap
* data structures.
*/
#include "unwind/unwind.h"
/* Whether this is a real CIE. Assumes CIE (length) sane. */
static int has_cie_id(const u32 *cie, int is_ehframe)
{
/* CIE id for eh_frame is 0, otherwise 0xffffffff */
if (is_ehframe && cie[1] == 0)
return 1;
else if (cie[1] == 0xffffffff)
return 1;
else
return 0;
}
/* whether this is a real fde or not */
static int is_fde(const u32 *fde, void *table, uint32_t table_len,
int is_ehframe)
{
const u8 *end;
/* check that length is proper */
if (!*fde || (*fde & (sizeof(*fde) - 1))) {
_stp_warn("bad fde\n");
return 0;
}
if (has_cie_id(fde, is_ehframe))
return 0;
end = (const u8 *)(fde + 1) + *fde;
/* end should fall within unwind table. */
if (((void*)end) < table
|| ((void *)end) > ((void *)(table + table_len))) {
_stp_warn("bad fde length\n");
return 0;
}
return 1;
}
/* given an FDE, find its CIE and sanity check */
static const u32 *cie_for_fde(const u32 *fde, void *unwind_data,
uint32_t table_len, int is_ehframe)
{
const u32 *cie;
unsigned version;
const u8 *end;
/* CIE_pointer must be a proper offset */
if ((fde[1] & (sizeof(*fde) - 1)) || fde[1] > (unsigned long)(fde + 1) - (unsigned long)unwind_data) {
_stp_warn("invalid fde[1]=%lx fde+1=%lx, unwind_data=%lx %lx\n",
(unsigned long)fde[1], (unsigned long)(fde + 1),
(unsigned long)unwind_data, (unsigned long)(fde + 1) - (unsigned long)unwind_data);
return NULL; /* this is not a valid FDE */
}
/* cie pointer field is different in eh_frame vs debug_frame */
if (is_ehframe)
cie = fde + 1 - fde[1] / sizeof(*fde);
else
cie = unwind_data + fde[1];
/* Make sure address falls in the table */
if (((void *)cie) < ((void*)unwind_data)
|| ((void*)cie) > ((void*)(unwind_data + table_len))) {
_stp_warn("cie address falls outside table\n");
return NULL;
}
if (*cie <= sizeof(*cie) + 4 || *cie >= fde[1] - sizeof(*fde)
|| ! has_cie_id(cie, is_ehframe)) {
_stp_warn("cie is not valid %lx %x %x %x\n", (unsigned long)cie, *cie, fde[1], cie[1]);
return NULL; /* this is not a (valid) CIE */
}
version = *(const u8 *)(cie + 2);
if (version != 1 && version != 3 && version != 4) {
_stp_warn ("Unsupported CIE version: %d\n", version);
return NULL;
}
end = (const u8 *)(cie + 1) + *cie;
/* end should fall within unwind table. */
if (((void *)end) < (void *)unwind_data
|| ((void *)end) > ((void *)(unwind_data + table_len))) {
_stp_warn ("CIE end falls outside table\n");
return NULL;
}
return cie;
}
/* Parse FDE and CIE content. Basic sanity checks should already have
been done start/end/version/id (done by is_fde and cie_for_fde).
Returns -1 if FDE or CIE cannot be parsed.*/
static int parse_fde_cie(const u32 *fde, const u32 *cie,
void *unwind_data, uint32_t table_len,
unsigned *ptrType, int user,
unsigned long *startLoc, unsigned long *locRange,
const u8 **fdeStart, const u8 **fdeEnd,
const u8 **cieStart, const u8 **cieEnd,
uleb128_t *codeAlign, sleb128_t *dataAlign,
uleb128_t *retAddrReg, unsigned *call_frame, int compat_task)
{
const u8 *ciePtr = (const u8 *)(cie + 2);
const u8 *fdePtr = (const u8 *)(fde + 2);
unsigned version = *ciePtr++;
const char *aug = (const void *)ciePtr;
uleb128_t augLen = 0; /* Set to non-zero if cie aug starts with z */
*cieEnd = (const u8 *)(cie + 1) + *cie;
*fdeEnd = (const u8 *)(fde + 1) + *fde;
/* check if augmentation string is nul-terminated */
if ((ciePtr = memchr(aug, 0, *cieEnd - ciePtr)) == NULL) {
_stp_warn("Unterminated augmentation string\n");
return -1;
}
ciePtr++; /* skip aug terminator */
*codeAlign = get_uleb128(&ciePtr, *cieEnd);
*dataAlign = get_sleb128(&ciePtr, *cieEnd);
dbug_unwind(2, "codeAlign=%lx, dataAlign=%lx\n",
*codeAlign, *dataAlign);
if (*codeAlign == 0 || *dataAlign == 0) {
_stp_warn("zero codeAlign or dataAlign values\n");
return -1;
}
*retAddrReg = ((version <= 1)
? *ciePtr++ : get_uleb128(&ciePtr, *cieEnd));
if(compat_task){
dbug_unwind(1, "map retAddrReg value %ld to reg_info idx %ld\n",
*retAddrReg, COMPAT_REG_MAP(DWARF_REG_MAP(*retAddrReg)));
*retAddrReg = COMPAT_REG_MAP(DWARF_REG_MAP(*retAddrReg));
} else {
dbug_unwind(1, "map retAddrReg value %ld to reg_info idx %ld\n",
*retAddrReg, DWARF_REG_MAP(*retAddrReg));
*retAddrReg = DWARF_REG_MAP(*retAddrReg);
}
if (*aug == 'z') {
augLen = get_uleb128(&ciePtr, *cieEnd);
if (augLen > (const u8 *)cie - *cieEnd
|| ciePtr + augLen > *cieEnd) {
_stp_warn("Bogus CIE augmentation length\n");
return -1;
}
}
*cieStart = ciePtr + augLen;
/* Read augmentation string to determine frame_call and ptrType. */
*call_frame = 1;
*ptrType = DW_EH_PE_absptr;
while (*aug) {
if (ciePtr > *cieStart) {
_stp_warn("Augmentation data runs past end\n");
return -1;
}
switch (*aug) {
case 'z':
break;
case 'L':
ciePtr++;
break;
case 'P': {
/* We are not actually interested in
the value, so don't try to deref.
Mask off DW_EH_PE_indirect. */
signed pType = *ciePtr++ & 0x7F;
if (!read_pointer(&ciePtr, *cieStart, pType, user, compat_task)) {
_stp_warn("couldn't read personality routine handler\n");
return -1;
}
break;
}
case 'R':
*ptrType = *ciePtr++;
break;
case 'S':
*call_frame = 0;
break;
default:
_stp_warn("Unknown augmentation char '%c'\n", *(aug - 1));
return -1;
}
aug++;
}
if (ciePtr != *cieStart) {
_stp_warn("Bogus CIE augmentation data\n");
return -1;
}
/* Now we finally know the type encoding and whether or not the
augmentation string starts with 'z' indicating the FDE might also
have some augmentation data, so we can parse the FDE. */
*startLoc = read_pointer(&fdePtr, *fdeEnd, *ptrType, user, compat_task);
*locRange = read_pointer(&fdePtr, *fdeEnd,
*ptrType & (DW_EH_PE_FORM | DW_EH_PE_signed),
user, compat_task);
dbug_unwind(2, "startLoc: %lx, locrange: %lx\n",
*startLoc, *locRange);
/* Skip FDE augmentation length (not interested in data). */
if (augLen != 0) {
augLen = get_uleb128(&fdePtr, *fdeEnd);
if (augLen > (const u8 *)fde - *fdeEnd
|| fdePtr + augLen > *fdeEnd) {
_stp_warn("Bogus FDE augmentation length\n");
return -1;
}
}
*fdeStart = fdePtr + augLen;
return 0;
}
#define REG_STATE state->reg[state->stackDepth]
static int advance_loc(unsigned long delta, struct unwind_state *state)
{
state->loc += delta * state->codeAlign;
dbug_unwind(1, "state->loc=%lx\n", state->loc);
return delta > 0;
}
/* Set Same or Nowhere rule for register. */
static void set_no_state_rule(uleb128_t reg, enum item_location where,
struct unwind_state *state)
{
dbug_unwind(1, "reg=%lx, where=%d\n", reg, where);
if (reg < ARRAY_SIZE(REG_STATE.regs)) {
REG_STATE.regs[reg].where = where;
}
}
/* Memory or Value rule */
static void set_offset_rule(uleb128_t reg, enum item_location where,
sleb128_t svalue, struct unwind_state *state)
{
dbug_unwind(1, "reg=%lx, where=%d, svalue=%lx\n", reg, where, svalue);
if (reg < ARRAY_SIZE(REG_STATE.regs)) {
REG_STATE.regs[reg].where = where;
REG_STATE.regs[reg].state.off = svalue;
}
}
/* Register rule. */
static void set_register_rule(uleb128_t reg, uleb128_t value,
struct unwind_state *state)
{
dbug_unwind(1, "reg=%lx, value=%lx\n", reg, value);
if (reg < ARRAY_SIZE(REG_STATE.regs)) {
REG_STATE.regs[reg].where = Register;
REG_STATE.regs[reg].state.reg = value;
}
}
/* Expr or ValExpr rule. */
static void set_expr_rule(uleb128_t reg, enum item_location where,
const u8 **expr, const u8 *end,
struct unwind_state *state)
{
const u8 *const start = *expr;
uleb128_t len = get_uleb128(expr, end);
dbug_unwind(1, "reg=%lx, where=%d, expr=%lu@%p\n",
reg, where, len, *expr);
/* Sanity check that expr falls completely inside known data. */
if (end - *expr >= len && reg < ARRAY_SIZE(REG_STATE.regs)) {
REG_STATE.regs[reg].where = where;
REG_STATE.regs[reg].state.expr = start;
*expr += len;
}
}
/* Limit the number of instructions we process. Arbitrary limit.
512 should be enough for anybody... */
#define MAX_CFI 512
static int processCFI(const u8 *start, const u8 *end, unsigned long targetLoc,
signed ptrType, int user, struct unwind_state *state, int compat_task)
{
union {
const u8 *p8;
const u16 *p16;
const u32 *p32;
} ptr;
int result = 1;
if (end - start > MAX_CFI) {
_stp_warn("Too many CFI instuctions\n");
return 0;
}
dbug_unwind(1, "targetLoc=%lx state->loc=%lx\n", targetLoc, state->loc);
for (ptr.p8 = start; result && ptr.p8 < end;) {
switch (*ptr.p8 >> 6) {
uleb128_t value;
uleb128_t value2;
sleb128_t svalue;
case 0:
switch (*ptr.p8++) {
case DW_CFA_nop:
dbug_unwind(1, "DW_CFA_nop\n");
break;
case DW_CFA_set_loc:
if ((state->loc = read_pointer(&ptr.p8, end, ptrType, user, compat_task)) == 0)
result = 0;
dbug_unwind(1, "DW_CFA_set_loc %lx (result=%d)\n", state->loc, result);
break;
case DW_CFA_advance_loc1:
result = ptr.p8 < end && advance_loc(*ptr.p8++, state);
dbug_unwind(1, "DW_CFA_advance_loc1 (result=%d)\n", result);
break;
case DW_CFA_advance_loc2:
result = ptr.p8 <= end + 2 && advance_loc(*ptr.p16++, state);
dbug_unwind(1, "DW_CFA_advance_loc2 (result=%d)\n", result);
break;
case DW_CFA_advance_loc4:
result = ptr.p8 <= end + 4 && advance_loc(*ptr.p32++, state);
dbug_unwind(1, "DW_CFA_advance_loc4 (result=%d)\n", result);
break;
case DW_CFA_offset_extended:
value = get_uleb128(&ptr.p8, end);
value2 = get_uleb128(&ptr.p8, end);
if (compat_task) {
dbug_unwind(1, "map DW_CFA_offset_extended value %ld to reg_info idx %ld, with offset %ld\n",
value, COMPAT_REG_MAP(DWARF_REG_MAP(value)), value2);
value = COMPAT_REG_MAP(DWARF_REG_MAP(value));
} else {
dbug_unwind(1, "map DW_CFA_offset_extended value %ld to reg_info idx %ld, with offset %ld\n",
value, DWARF_REG_MAP(value), value2);
value = DWARF_REG_MAP(value);
}
set_offset_rule(value, Memory,
value2 * state->dataAlign,
state);
break;
case DW_CFA_val_offset:
value = get_uleb128(&ptr.p8, end);
value2 = get_uleb128(&ptr.p8, end);
if (compat_task) {
dbug_unwind(1, "map DW_CFA_val_offset value %ld to reg_info idx %ld\n, with offset: %ld",
value, COMPAT_REG_MAP(DWARF_REG_MAP(value)), value2);
value = COMPAT_REG_MAP(DWARF_REG_MAP(value));
} else {
dbug_unwind(1, "map DW_CFA_val_offset value %ld to reg_info idx %ld\n, with offset: %ld",
value, DWARF_REG_MAP(value), value2);
value = DWARF_REG_MAP(value);
}
set_offset_rule(value, Value,
value2 * state->dataAlign,
state);
break;
case DW_CFA_offset_extended_sf:
value = get_uleb128(&ptr.p8, end);
svalue = get_sleb128(&ptr.p8, end);
if (compat_task) {
dbug_unwind(1, "map DW_CFA_offset_extended_sf value %ld to reg_info idx %ld, with offset: %ld\n",
value, COMPAT_REG_MAP(DWARF_REG_MAP(value)), svalue);
value = COMPAT_REG_MAP(DWARF_REG_MAP(value));
} else {
dbug_unwind(1, "map DW_CFA_offset_extended_sf value %ld to reg_info idx %ld, with offset: %ld\n",
value, DWARF_REG_MAP(value), svalue);
value = DWARF_REG_MAP(value);
}
set_offset_rule(value, Memory,
svalue * state->dataAlign,
state);
break;
case DW_CFA_val_offset_sf:
value = get_uleb128(&ptr.p8, end);
svalue = get_sleb128(&ptr.p8, end);
if (compat_task) {
dbug_unwind(1, "map DW_CFA_val_offset_sf value %ld to reg_info idx %ld\n",
value, COMPAT_REG_MAP(DWARF_REG_MAP(value)));
value = COMPAT_REG_MAP(DWARF_REG_MAP(value));
} else {
dbug_unwind(1, "map DW_CFA_val_offset_sf value %ld to reg_info idx %ld\n",
value, DWARF_REG_MAP(value));
value = DWARF_REG_MAP(value);
}
set_offset_rule(value, Value,
svalue * state->dataAlign,
state);
break;
case DW_CFA_same_value:
value = get_uleb128(&ptr.p8, end);
if (compat_task) {
dbug_unwind(1, "map DW_CFA_same_value value %ld to reg_info idx %ld\n",
value, COMPAT_REG_MAP(DWARF_REG_MAP(value)));
value = COMPAT_REG_MAP(DWARF_REG_MAP(value));
} else {
dbug_unwind(1, "map DW_CFA_same_value value %ld to reg_info idx %ld\n",
value, DWARF_REG_MAP(value));
value = DWARF_REG_MAP(value);
}
set_no_state_rule(value, Same, state);
break;
case DW_CFA_restore_extended:
value = get_uleb128(&ptr.p8, end);
if (compat_task) {
dbug_unwind(1, "map DW_CFA_restore_extended value %ld to reg_info idx %ld\n",
value, COMPAT_REG_MAP(DWARF_REG_MAP(value)));
value = COMPAT_REG_MAP(DWARF_REG_MAP(value));
} else {
dbug_unwind(1, "map DW_CFA_restore_extended value %ld to reg_info idx %ld\n",
value, DWARF_REG_MAP(value));
value = DWARF_REG_MAP(value);
}
if (value < ARRAY_SIZE(REG_STATE.regs))
memcpy(®_STATE.regs[value], &state->cie_regs[value], sizeof(struct unwind_item));
break;
case DW_CFA_undefined:
value = get_uleb128(&ptr.p8, end);
if (compat_task) {
dbug_unwind(1, "map DW_CFA_undefined value %ld to reg_info idx %ld\n",
value, COMPAT_REG_MAP(DWARF_REG_MAP(value)));
value = COMPAT_REG_MAP(DWARF_REG_MAP(value));
} else {
dbug_unwind(1, "map DW_CFA_undefined value %ld to reg_info idx %ld\n",
value, DWARF_REG_MAP(value));
value = DWARF_REG_MAP(value);
}
set_no_state_rule(value, Nowhere, state);
break;
case DW_CFA_register: {
uleb128_t reg_value;
value = get_uleb128(&ptr.p8, end);
reg_value = get_uleb128(&ptr.p8, end);
if (compat_task) {
dbug_unwind(1, "map DW_CFA_register value %ld to reg_info idx %ld (reg_value %ld to reg_info idx %ld)\n",
value, COMPAT_REG_MAP(DWARF_REG_MAP(value)), reg_value, COMPAT_REG_MAP(DWARF_REG_MAP(reg_value)));
value = COMPAT_REG_MAP(DWARF_REG_MAP(value));
reg_value = DWARF_REG_MAP(reg_value);
} else {
dbug_unwind(1, "map DW_CFA_register value %ld to reg_info idx %ld (reg_value %ld to reg_info idx %ld)\n",
value, DWARF_REG_MAP(value), reg_value, DWARF_REG_MAP(reg_value));
value = DWARF_REG_MAP(value);
reg_value = DWARF_REG_MAP(reg_value);
}
set_register_rule(value, reg_value, state);
break;
}
case DW_CFA_expression:
value = get_uleb128(&ptr.p8, end);
if (compat_task) {
dbug_unwind(1, "map DW_CFA_expression value %ld to reg_info idx %ld\n",
value, COMPAT_REG_MAP(DWARF_REG_MAP(value)));
value = COMPAT_REG_MAP(DWARF_REG_MAP(value));
} else {
dbug_unwind(1, "map DW_CFA_expression value %ld to reg_info idx %ld\n",
value, DWARF_REG_MAP(value));
value = DWARF_REG_MAP(value);
}
set_expr_rule(value, Expr, &ptr.p8, end, state);
break;
case DW_CFA_val_expression:
value = get_uleb128(&ptr.p8, end);
if (compat_task) {
dbug_unwind(1, "map DW_CFA_val_expression value %ld to reg_info idx %ld\n",
value, COMPAT_REG_MAP(DWARF_REG_MAP(value)));
value = COMPAT_REG_MAP(DWARF_REG_MAP(value));
} else {
dbug_unwind(1, "map DW_CFA_val_expression value %ld to reg_info idx %ld\n",
value, DWARF_REG_MAP(value));
value = DWARF_REG_MAP(value);
}
set_expr_rule(value, ValExpr, &ptr.p8, end,
state);
break;
case DW_CFA_remember_state:
state->stackDepth++;
if (state->stackDepth >= STP_MAX_STACK_DEPTH) {
_stp_warn("Too many stacked DW_CFA_remember_state\n");
return 0;
}
memcpy(®_STATE,
&state->reg[state->stackDepth - 1],
sizeof (REG_STATE));
dbug_unwind(1, "DW_CFA_remember_state (stackDepth=%d)\n", state->stackDepth);
break;
case DW_CFA_restore_state:
if (state->stackDepth == 0) {
_stp_warn("Unbalanced DW_CFA_restore_state\n");
return 0;
}
state->stackDepth--;
dbug_unwind(1, "DW_CFA_restore_state (stackDepth=%d)\n", state->stackDepth);
break;
case DW_CFA_def_cfa:
value = get_uleb128(&ptr.p8, end);
REG_STATE.cfa_is_expr = 0;
if (compat_task) {
dbug_unwind(1, "map DW_CFA_def_cfa value %ld to reg_info idx %ld\n",
value, COMPAT_REG_MAP(DWARF_REG_MAP(value)));
REG_STATE.cfa.reg = COMPAT_REG_MAP(value);
dbug_unwind(1, "DW_CFA_def_cfa reg=%ld\n", COMPAT_REG_MAP(REG_STATE.cfa.reg));
} else {
dbug_unwind(1, "map DW_CFA_def_cfa value %ld to reg_info idx %ld\n",
value, DWARF_REG_MAP(value));
REG_STATE.cfa.reg = value;
dbug_unwind(1, "DW_CFA_def_cfa reg=%ld\n", REG_STATE.cfa.reg);
}
/*nobreak */
case DW_CFA_def_cfa_offset:
if (REG_STATE.cfa_is_expr != 0) {
_stp_warn("Unexpected DW_CFA_def_cfa_offset\n");
} else {
/* non-factored uleb128 */
REG_STATE.cfa.off = get_uleb128(&ptr.p8, end);
dbug_unwind(1, "DW_CFA_def_cfa_offset offs=%lx\n", REG_STATE.cfa.off);
}
break;
case DW_CFA_def_cfa_sf:
value = get_uleb128(&ptr.p8, end);
REG_STATE.cfa_is_expr = 0;
if (compat_task) {
dbug_unwind(1, "map DW_CFA_def_cfa_sf value %ld to reg_info idx %ld\n",
value, COMPAT_REG_MAP(DWARF_REG_MAP(value)));
REG_STATE.cfa.reg = COMPAT_REG_MAP(value);
} else {
dbug_unwind(1, "map DW_CFA_def_cfa_sf value %ld to reg_info idx %ld\n",
value, DWARF_REG_MAP(value));
REG_STATE.cfa.reg = value;
}
/*nobreak */
case DW_CFA_def_cfa_offset_sf:
if (REG_STATE.cfa_is_expr != 0) {
_stp_warn("Unexpected DW_CFA_def_cfa_offset_sf\n");
} else {
/* factored sleb128 */
REG_STATE.cfa.off = get_sleb128(&ptr.p8, end) * state->dataAlign;
dbug_unwind(1, "DW_CFA_def_cfa_offset_sf offs=%lx\n", REG_STATE.cfa.off);
}
break;
case DW_CFA_def_cfa_register:
if (REG_STATE.cfa_is_expr != 0) {
_stp_warn("Unexpected DW_CFA_def_cfa_register\n");
} else {
value = get_uleb128(&ptr.p8, end);
if (compat_task) {
dbug_unwind(1, "map DW_CFA_def_cfa_register value %ld to reg_info idx %ld (%ld)\n",
value, COMPAT_REG_MAP(DWARF_REG_MAP(value)), DWARF_REG_MAP(value));
REG_STATE.cfa.reg = COMPAT_REG_MAP(value);
} else {
dbug_unwind(1, "map DW_CFA_def_cfa_register value %ld to reg_info idx %ld (%ld)\n",
value, DWARF_REG_MAP(value), DWARF_REG_MAP(value));
REG_STATE.cfa.reg = value;
}
}
break;
case DW_CFA_def_cfa_expression: {
const u8 *cfa_expr = ptr.p8;
value = get_uleb128(&ptr.p8, end);
/* Sanity check that cfa_expr falls completely
inside known data. */
if (ptr.p8 < end && end - ptr.p8 >= value) {
REG_STATE.cfa_is_expr = 1;
REG_STATE.cfa_expr = cfa_expr;
ptr.p8 += value;
dbug_unwind(1, "DW_CFA_def_cfa_expression %lu@%p\n", value, cfa_expr);
}
else
_stp_warn("BAD DW_CFA_def_cfa_expression value %lu\n", value);
break;
}
/* Size of all arguments pushed on the stack. */
case DW_CFA_GNU_args_size:
get_uleb128(&ptr.p8, end);
dbug_unwind(1, "DW_CFA_GNU_args_size\n");
break;
/* This is only produced by GCC before 2002.
Like DW_CFA_offset_extended_sf but using an
uleb128 that is subtracted from CFA. */
case DW_CFA_GNU_negative_offset_extended:
value = get_uleb128(&ptr.p8, end);
if (compat_task) {
dbug_unwind(1, "map DW_CFA_GNU_negative_offset_extended value %ld to reg_info idx %ld\n",
value, COMPAT_REG_MAP(DWARF_REG_MAP(value)));
value = COMPAT_REG_MAP(DWARF_REG_MAP(value));
} else {
dbug_unwind(1, "map DW_CFA_GNU_negative_offset_extended value %ld to reg_info idx %ld\n",
value, DWARF_REG_MAP(value));
value = DWARF_REG_MAP(value);
}
set_offset_rule(value, Memory,
(uleb128_t)0 - get_uleb128(&ptr.p8, end), state);
break;
case DW_CFA_GNU_window_save:
default:
_stp_warn("unimplemented call frame instruction: 0x%x\n", *(ptr.p8 - 1));
result = 0;
break;
}
break;
case 1:
result = advance_loc(*ptr.p8++ & 0x3f, state);
dbug_unwind(1, "DW_CFA_advance_loc\n");
break;
case 2:
value = *ptr.p8++ & 0x3f;
if (compat_task) {
dbug_unwind(1, "map DW_CFA_offset value %ld to reg_info idx %ld\n",
value, COMPAT_REG_MAP(DWARF_REG_MAP(value)));
value = COMPAT_REG_MAP(DWARF_REG_MAP(value));
} else {
dbug_unwind(1, "map DW_CFA_offset value %ld to reg_info idx %ld\n",
value, DWARF_REG_MAP(value));
value = DWARF_REG_MAP(value);
}
value2 = get_uleb128(&ptr.p8, end);
set_offset_rule(value, Memory,
value2 * state->dataAlign, state);
break;
case 3:
value = *ptr.p8++ & 0x3f;
if (compat_task) {
dbug_unwind(1, "map DW_CFA_restore value %ld to reg_info idx %ld\n",
value, COMPAT_REG_MAP(DWARF_REG_MAP(value)));
value = COMPAT_REG_MAP(DWARF_REG_MAP(value));
} else {
dbug_unwind(1, "map DW_CFA_restore value %ld to reg_info idx %ld\n",
value, DWARF_REG_MAP(value));
value = DWARF_REG_MAP(value);
}
if (value < ARRAY_SIZE(REG_STATE.regs))
memcpy(®_STATE.regs[value], &state->cie_regs[value], sizeof(struct unwind_item));
break;
}
dbug_unwind(1, "targetLoc=%lx state->loc=%lx\n", targetLoc, state->loc);
dbug_unwind(1, "result: %d\n", result);
if (ptr.p8 > end)
result = 0;
if (result && targetLoc != 0 && targetLoc < state->loc)
return 1;
}
return result && ptr.p8 == end;
}
#ifdef DEBUG_UNWIND
static const char *_stp_enc_hi_name[] = {
"DW_EH_PE",
"DW_EH_PE_pcrel",
"DW_EH_PE_textrel",
"DW_EH_PE_datarel",
"DW_EH_PE_funcrel",
"DW_EH_PE_aligned"
};
static const char *_stp_enc_lo_name[] = {
"_absptr",
"_uleb128",
"_udata2",
"_udata4",
"_udata8",
"_sleb128",
"_sdata2",
"_sdata4",
"_sdata8"
};
static char *_stp_eh_enc_name(signed type)
{
static char buf[64];
int hi, low;
if (type == DW_EH_PE_omit)
return "DW_EH_PE_omit";
hi = (type & DW_EH_PE_ADJUST) >> 4;
low = type & DW_EH_PE_FORM;
if (hi > 5 || low > 4 || (low == 0 && (type & DW_EH_PE_signed))) {
snprintf(buf, sizeof(buf), "ERROR:encoding=0x%x", type);
return buf;
}
buf[0] = 0;
if (type & DW_EH_PE_indirect)
strlcpy(buf, "DW_EH_PE_indirect|", sizeof(buf));
strlcat(buf, _stp_enc_hi_name[hi], sizeof(buf));
if (type & DW_EH_PE_signed)
low += 4;
strlcat(buf, _stp_enc_lo_name[low], sizeof(buf));
return buf;
}
#endif /* DEBUG_UNWIND */
// If this is an address inside a module, adjust for section relocation
// and the elfutils base relocation done during loading of the .dwarf_frame
// in translate.cxx.
static unsigned long
adjustStartLoc (unsigned long startLoc,
struct _stp_module *m,
struct _stp_section *s,
unsigned ptrType, int is_ehframe, int user)
{
unsigned long vm_addr = 0;
/* If we're unwinding the current module, then the addresses
we've got don't require adjustment, they didn't come from user
space */
if(strcmp(THIS_MODULE->name,m->name)==0)
return startLoc;
/* XXX - some, or all, of this should really be done by
_stp_module_relocate and/or read_pointer. */
dbug_unwind(2, "adjustStartLoc=%lx, ptrType=%s, m=%s, s=%s eh=%d\n",
startLoc, _stp_eh_enc_name(ptrType), m->path, s->name, is_ehframe);
if (startLoc == 0
|| strcmp (m->name, "kernel") == 0
|| (strcmp (s->name, ".absolute") == 0 && !is_ehframe))
return startLoc;
/* eh_frame data has been loaded in the kernel, so readjust offset. */
if (is_ehframe) {
dbug_unwind(2, "eh_frame=%lx, eh_frame_addr=%lx\n", (unsigned long) m->eh_frame, m->eh_frame_addr);
if ((ptrType & DW_EH_PE_ADJUST) == DW_EH_PE_pcrel) {
startLoc -= (unsigned long) m->eh_frame;
startLoc += m->eh_frame_addr;
}
/* User space exec */
if (strcmp (s->name, ".absolute") == 0)
return startLoc;
}
/* User space or kernel dynamic module. */
if (user && strcmp (s->name, ".dynamic") == 0)
stap_find_vma_map_info_user(current->group_leader, m, &vm_addr, NULL, NULL);
else
vm_addr = s->static_addr;
if (is_ehframe)
return startLoc + vm_addr;
else
return startLoc + vm_addr - s->sec_load_offset;
}
/* If we previously created an unwind header, then use it now to binary search */
/* for the FDE corresponding to pc. */
static u32 *_stp_search_unwind_hdr(unsigned long pc,
struct _stp_module *m,
struct _stp_section *s,
int is_ehframe, int user, int compat_task)
{
const u8 *ptr, *end, *hdr = is_ehframe ? m->unwind_hdr: s->debug_hdr;
uint32_t hdr_len = is_ehframe ? m->unwind_hdr_len : s->debug_hdr_len;
unsigned long startLoc;
u32 *fde = NULL;
unsigned num, tableSize, t2;
unsigned long eh_hdr_addr = m->unwind_hdr_addr;
if (hdr == NULL || hdr_len < 4 || hdr[0] != 1) {
_stp_warn("no or bad debug frame hdr\n");
return NULL;
}
dbug_unwind(1, "binary search for %lx", pc);
/* table_enc */
switch (hdr[3] & DW_EH_PE_FORM) {
case DW_EH_PE_absptr:
if (!compat_task)
tableSize = sizeof(unsigned long);
else
tableSize = sizeof(unsigned int);
break;
case DW_EH_PE_data2:
tableSize = 2;
break;
case DW_EH_PE_data4:
tableSize = 4;
break;
case DW_EH_PE_data8:
tableSize = 8;
break;
default:
_stp_warn("bad unwind table encoding");
return NULL;
}
ptr = hdr + 4;
end = hdr + hdr_len;
{
// XXX Can the header validity be checked just once?
unsigned long eh = read_ptr_sect(&ptr, end, hdr[1], 0,
eh_hdr_addr, user, compat_task, tableSize);
if ((hdr[1] & DW_EH_PE_ADJUST) == DW_EH_PE_pcrel)
eh = eh - (unsigned long)hdr + eh_hdr_addr;
if ((is_ehframe && eh != (unsigned long)m->eh_frame_addr)) {
_stp_warn("eh_frame_ptr in eh_frame_hdr 0x%lx not valid; eh_frame_addr = 0x%lx", eh, (unsigned long)m->eh_frame_addr);
return NULL;
}
}
num = read_ptr_sect(&ptr, end, hdr[2], 0, eh_hdr_addr, user, compat_task, tableSize);
if (num == 0 || num != (end - ptr) / (2 * tableSize)
|| (end - ptr) % (2 * tableSize)) {
_stp_warn("unwind Bad num=%d end-ptr=%ld 2*tableSize=%d",
num, (long)(end - ptr), 2 * tableSize);
return NULL;
}
do {
const u8 *cur = ptr + (num / 2) * (2 * tableSize);
startLoc = read_ptr_sect(&cur, cur + tableSize, hdr[3], 0,
eh_hdr_addr, user, compat_task, tableSize);
startLoc = adjustStartLoc(startLoc, m, s, hdr[3],
is_ehframe, user);
if (pc < startLoc)
num /= 2;
else {
ptr = cur - tableSize;
num = (num + 1) / 2;
}
} while (startLoc && num > 1);
if (num == 1
&& (startLoc = adjustStartLoc(read_ptr_sect(&ptr, ptr + tableSize, hdr[3], 0,
eh_hdr_addr, user, compat_task, tableSize),
m, s, hdr[3], is_ehframe, user)) != 0 && pc >= startLoc) {
unsigned long off;
off = read_ptr_sect(&ptr, ptr + tableSize, hdr[3],
0, eh_hdr_addr, user, compat_task, tableSize);
dbug_unwind(1, "fde off=%lx\n", off);
/* For real eh_frame_hdr the actual fde address is at the
new eh_frame load address. For our own debug_hdr created
table the fde is an offset into the debug_frame table. */
if (is_ehframe)
fde = off - m->eh_frame_addr + m->eh_frame;
else
fde = m->debug_frame + off;
}
dbug_unwind(1, "returning fde=%lx startLoc=%lx", (unsigned long) fde, startLoc);
return fde;
}
#define FRAME_REG(r, t) (((t *)frame)[reg_info[r].offs])
#ifndef CONFIG_64BIT
# define CASES CASE(8); CASE(16); CASE(32)
#else
# define CASES CASE(8); CASE(16); CASE(32); CASE(64)
#endif
#define MAX_EXPR_STACK 8 /* arbitrary */
static int compute_expr(const u8 *expr, struct unwind_frame_info *frame,
unsigned long *result, int user, int compat_task)
{
/*
* We previously validated the length, so we won't read off the end.
* See sanity checks in set_expr() and for DW_CFA_def_cfa_expression.
*/
uleb128_t len = get_uleb128(&expr, (const u8 *) -1UL);
const u8 *const start = expr;
const u8 *const end = expr + len;
long stack[MAX_EXPR_STACK]; /* stack slots are signed */
unsigned int sp = 0;
#define PUSH(val) do { \
if (sp == MAX_EXPR_STACK) \
goto overflow; \
stack[sp++] = (val); \
} while (0)
#define POP ({ \
if (sp == 0) \
goto underflow; \
stack[--sp]; \
})
#define NEED(n) do { \
if (end - expr < (n)) \
goto truncated; \
} while (0)
while (expr < end) {
uleb128_t value;
union {
u8 u8;
s8 s8;
u16 u16;
s16 s16;
u32 u32;
s32 s32;
u64 u64;
s64 s64;
} u;
const u8 op = *expr++;
dbug_unwind(3, " expr op 0x%x (%ld left)\n", op, (long)(end - expr));
switch (op) {
case DW_OP_nop:
break;
case DW_OP_bra:
if (POP == 0)
break;
/* Fall through. */
case DW_OP_skip:
NEED(sizeof(u.s16));
memcpy(&u.s16, expr, sizeof(u.s16));
expr += sizeof(u.s16);
if (u.s16 < 0 ?
unlikely(expr - start < -u.s16) :
unlikely(end - expr < u.s16)) {
_stp_warn("invalid skip %d in CFI expression\n", (int) u.s16);
return 1;
}
/*
* A backward branch could lead to an infinite loop.
* So punt it until we find we actually need it.
*/
if (u.s16 < 0) {
_stp_warn("backward branch in CFI expression not supported\n");
return 1;
}
expr += u.s16;
break;
case DW_OP_dup:
value = POP;
PUSH(value);
PUSH(value);
break;
case DW_OP_drop:
POP;
break;
case DW_OP_swap: {
unsigned long tos = POP;
unsigned long nos = POP;
PUSH(tos);
PUSH(nos);
break;
};
case DW_OP_over:
value = 1;
goto pick;
case DW_OP_pick:
NEED(1);
value = *expr++;
pick:
if (value >= sp)
goto underflow;
value = stack[sp - value];
PUSH(value);
break;
#define CONSTANT(type) \
NEED(sizeof(u.type)); \
memcpy(&u.type, expr, sizeof(u.type)); \
expr += sizeof(u.type); \
value = u.type; \
PUSH(value); \
break
case DW_OP_addr:
if (sizeof(unsigned long) == 8) { /* XXX 32/64!! */
CONSTANT(u64);
} else {
CONSTANT(u32);
}
break;
case DW_OP_const1u: CONSTANT(u8);
case DW_OP_const1s: CONSTANT(s8);
case DW_OP_const2u: CONSTANT(u16);
case DW_OP_const2s: CONSTANT(s16);
case DW_OP_const4u: CONSTANT(u32);
case DW_OP_const4s: CONSTANT(s32);
case DW_OP_const8u: CONSTANT(u64);
case DW_OP_const8s: CONSTANT(s64);
#undef CONSTANT
case DW_OP_constu:
value = get_uleb128(&expr, end);
PUSH(value);
break;
case DW_OP_consts:
value = get_sleb128(&expr, end);
PUSH(value);
break;
case DW_OP_lit0 ... DW_OP_lit31: