-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdwarf.c
2032 lines (1760 loc) · 52 KB
/
dwarf.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
/*
* This file is part of mtrace-ng.
* Copyright (C) 2018 Stefani Seibold <[email protected]>
* This file is based on the libunwind source
*
* This work was sponsored by Rohde & Schwarz GmbH & Co. KG, Munich/Germany.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include <string.h>
#include <sys/mman.h>
#include <sys/ptrace.h>
#include <elf.h>
#include <assert.h>
#include <sys/types.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <limits.h>
#include <inttypes.h>
#include <errno.h>
#include <unistd.h>
#include <malloc.h>
#include "backend.h"
#include "common.h"
#include "debug.h"
#include "dwarf.h"
#include "library.h"
#include "task.h"
#define MAX_EXPR_STACK_SIZE 64
#define NUM_OPERANDS(signature) (((signature) >> 6) & 0x3)
#define OPND1_TYPE(signature) (((signature) >> 3) & 0x7)
#define OPND2_TYPE(signature) (((signature) >> 0) & 0x7)
#define OPND_SIGNATURE(n, t1, t2) (((n) << 6) | ((t1) << 3) | ((t2) << 0))
#define OPND1(t1) OPND_SIGNATURE(1, t1, 0)
#define OPND2(t1, t2) OPND_SIGNATURE(2, t1, t2)
#define VAL8 0x0
#define VAL16 0x1
#define VAL32 0x2
#define VAL64 0x3
#define ULEB128 0x4
#define SLEB128 0x5
#define OFFSET 0x6 /* 32-bit offset for 32-bit DWARF, 64-bit otherwise */
#define ADDR 0x7 /* Machine address. */
#define DWARF_CIE_VERSION 3
#define DWARF_CIE_VERSION_GCC 1 /* GCC emits version 1??? */
#define DWARF_CFA_OPCODE_MASK 0xc0
#define DWARF_CFA_OPERAND_MASK 0x3f
#define DW_EH_VERSION 1 /* The version we're implementing */
/* For uniformity, we'd like to treat the CFA save-location like any
other register save-location, but this doesn't quite work, because
the CFA can be expressed as a (REGISTER,OFFSET) pair. To handle
this, we use two dwarf_save_loc structures to describe the CFA.
The first one (CFA_REG_COLUMN), tells us where the CFA is saved.
In the case of DWARF_WHERE_EXPR, the CFA is defined by a DWARF
location expression whose address is given by member "val". In the
case of DWARF_WHERE_REG, member "val" gives the number of the
base-register and the "val" member of DWARF_CFA_OFF_COLUMN gives
the offset value. */
#define DWARF_CFA_REG_COLUMN(as) ((as)->num_regs + 0)
#define DWARF_CFA_OFF_COLUMN(as) ((as)->num_regs + 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 and
hence we include the definitions here, in the main dwarf.h file.
The Pointer-Encoding format is partially documented in Linux Base
Spec v1.3 (http://www.linuxbase.org/spec/). The rest is reverse
engineered from GCC.
*/
#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 */
/* Flag bit. If set, the resulting pointer is the address of the word
that contains the final address. */
#define DW_EH_PE_indirect 0x80
/* 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 */
struct table_entry {
int32_t start_ip_offset;
int32_t fde_offset;
};
enum dwarf_where {
DWARF_WHERE_UNDEF, /* register isn't saved at all */
DWARF_WHERE_SAME, /* register has same value as in prev. frame */
DWARF_WHERE_CFAREL, /* register saved at CFA-relative address */
DWARF_WHERE_REG, /* register saved in another register */
DWARF_WHERE_EXPR, /* register saved */
DWARF_WHERE_VAL_EXPR, /* register has computed value */
};
enum dwarf_cfa {
DW_CFA_advance_loc = 0x40,
DW_CFA_offset = 0x80,
DW_CFA_restore = 0xc0,
DW_CFA_nop = 0x00,
DW_CFA_set_loc = 0x01,
DW_CFA_advance_loc1 = 0x02,
DW_CFA_advance_loc2 = 0x03,
DW_CFA_advance_loc4 = 0x04,
DW_CFA_offset_extended = 0x05,
DW_CFA_restore_extended = 0x06,
DW_CFA_undefined = 0x07,
DW_CFA_same_value = 0x08,
DW_CFA_register = 0x09,
DW_CFA_remember_state = 0x0a,
DW_CFA_restore_state = 0x0b,
DW_CFA_def_cfa = 0x0c,
DW_CFA_def_cfa_register = 0x0d,
DW_CFA_def_cfa_offset = 0x0e,
DW_CFA_def_cfa_expression = 0x0f,
DW_CFA_expression = 0x10,
DW_CFA_offset_extended_sf = 0x11,
DW_CFA_def_cfa_sf = 0x12,
DW_CFA_def_cfa_offset_sf = 0x13,
DW_CFA_val_expression = 0x16,
DW_CFA_lo_user = 0x1c,
DW_CFA_MIPS_advance_loc8 = 0x1d,
DW_CFA_GNU_window_save = 0x2d,
DW_CFA_GNU_args_size = 0x2e,
DW_CFA_GNU_negative_offset_extended = 0x2f,
DW_CFA_hi_user = 0x3c
};
enum dwarf_expr_op {
DW_OP_addr = 0x03,
DW_OP_deref = 0x06,
DW_OP_const1u = 0x08,
DW_OP_const1s = 0x09,
DW_OP_const2u = 0x0a,
DW_OP_const2s = 0x0b,
DW_OP_const4u = 0x0c,
DW_OP_const4s = 0x0d,
DW_OP_const8u = 0x0e,
DW_OP_const8s = 0x0f,
DW_OP_constu = 0x10,
DW_OP_consts = 0x11,
DW_OP_dup = 0x12,
DW_OP_drop = 0x13,
DW_OP_over = 0x14,
DW_OP_pick = 0x15,
DW_OP_swap = 0x16,
DW_OP_rot = 0x17,
DW_OP_xderef = 0x18,
DW_OP_abs = 0x19,
DW_OP_and = 0x1a,
DW_OP_div = 0x1b,
DW_OP_minus = 0x1c,
DW_OP_mod = 0x1d,
DW_OP_mul = 0x1e,
DW_OP_neg = 0x1f,
DW_OP_not = 0x20,
DW_OP_or = 0x21,
DW_OP_plus = 0x22,
DW_OP_plus_uconst = 0x23,
DW_OP_shl = 0x24,
DW_OP_shr = 0x25,
DW_OP_shra = 0x26,
DW_OP_xor = 0x27,
DW_OP_skip = 0x2f,
DW_OP_bra = 0x28,
DW_OP_eq = 0x29,
DW_OP_ge = 0x2a,
DW_OP_gt = 0x2b,
DW_OP_le = 0x2c,
DW_OP_lt = 0x2d,
DW_OP_ne = 0x2e,
DW_OP_lit0 = 0x30,
DW_OP_lit31 = 0x4f,
DW_OP_reg0 = 0x50,
DW_OP_reg31 = 0x6f,
DW_OP_breg0 = 0x70,
DW_OP_breg31 = 0x8f,
DW_OP_regx = 0x90,
DW_OP_fbreg = 0x91,
DW_OP_bregx = 0x92,
DW_OP_piece = 0x93,
DW_OP_deref_size = 0x94,
DW_OP_xderef_size = 0x95,
DW_OP_nop = 0x96,
DW_OP_push_object_address = 0x97,
DW_OP_call2 = 0x98,
DW_OP_call4 = 0x99,
DW_OP_call_ref = 0x9a,
DW_OP_lo_user = 0xe0,
DW_OP_hi_user = 0xff
};
struct dwarf_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_addr; // address of the FDE
}
binary_search_table[fde_count]; */
};
struct dwarf_reg {
enum dwarf_where where; /* how is the register saved? */
arch_addr_t val; /* where it's saved */
};
struct dwarf_reg_state {
struct dwarf_reg reg[2];/* the registers are dynamically allocated */
};
static const uint8_t dwarf_operands[256] = {
[DW_OP_addr] = OPND1(ADDR),
[DW_OP_const1u] = OPND1(VAL8),
[DW_OP_const1s] = OPND1(VAL8),
[DW_OP_const2u] = OPND1(VAL16),
[DW_OP_const2s] = OPND1(VAL16),
[DW_OP_const4u] = OPND1(VAL32),
[DW_OP_const4s] = OPND1(VAL32),
[DW_OP_const8u] = OPND1(VAL64),
[DW_OP_const8s] = OPND1(VAL64),
[DW_OP_pick] = OPND1(VAL8),
[DW_OP_plus_uconst] = OPND1(ULEB128),
[DW_OP_skip] = OPND1(VAL16),
[DW_OP_bra] = OPND1(VAL16),
[DW_OP_breg0 + 0] = OPND1(SLEB128),
[DW_OP_breg0 + 1] = OPND1(SLEB128),
[DW_OP_breg0 + 2] = OPND1(SLEB128),
[DW_OP_breg0 + 3] = OPND1(SLEB128),
[DW_OP_breg0 + 4] = OPND1(SLEB128),
[DW_OP_breg0 + 5] = OPND1(SLEB128),
[DW_OP_breg0 + 6] = OPND1(SLEB128),
[DW_OP_breg0 + 7] = OPND1(SLEB128),
[DW_OP_breg0 + 8] = OPND1(SLEB128),
[DW_OP_breg0 + 9] = OPND1(SLEB128),
[DW_OP_breg0 + 10] = OPND1(SLEB128),
[DW_OP_breg0 + 11] = OPND1(SLEB128),
[DW_OP_breg0 + 12] = OPND1(SLEB128),
[DW_OP_breg0 + 13] = OPND1(SLEB128),
[DW_OP_breg0 + 14] = OPND1(SLEB128),
[DW_OP_breg0 + 15] = OPND1(SLEB128),
[DW_OP_breg0 + 16] = OPND1(SLEB128),
[DW_OP_breg0 + 17] = OPND1(SLEB128),
[DW_OP_breg0 + 18] = OPND1(SLEB128),
[DW_OP_breg0 + 19] = OPND1(SLEB128),
[DW_OP_breg0 + 20] = OPND1(SLEB128),
[DW_OP_breg0 + 21] = OPND1(SLEB128),
[DW_OP_breg0 + 22] = OPND1(SLEB128),
[DW_OP_breg0 + 23] = OPND1(SLEB128),
[DW_OP_breg0 + 24] = OPND1(SLEB128),
[DW_OP_breg0 + 25] = OPND1(SLEB128),
[DW_OP_breg0 + 26] = OPND1(SLEB128),
[DW_OP_breg0 + 27] = OPND1(SLEB128),
[DW_OP_breg0 + 28] = OPND1(SLEB128),
[DW_OP_breg0 + 29] = OPND1(SLEB128),
[DW_OP_breg0 + 30] = OPND1(SLEB128),
[DW_OP_breg0 + 31] = OPND1(SLEB128),
[DW_OP_regx] = OPND1(ULEB128),
[DW_OP_fbreg] = OPND1(SLEB128),
[DW_OP_bregx] = OPND2(ULEB128, SLEB128),
[DW_OP_piece] = OPND1(ULEB128),
[DW_OP_deref_size] = OPND1(VAL8),
[DW_OP_xderef_size] = OPND1(VAL8),
[DW_OP_call2] = OPND1(VAL16),
[DW_OP_call4] = OPND1(VAL32),
[DW_OP_call_ref] = OPND1(OFFSET)
};
static inline __attribute__((always_inline)) int dwarf_access_mem(struct dwarf_addr_space *as, arch_addr_t addr, void *valp, size_t size)
{
struct dwarf_cursor *c = &as->cursor;
if (unlikely(!addr)) {
debug(DEBUG_DWARF, "invalid null memory access");
return -DWARF_EINVAL;
}
if (unlikely(!valp))
return 0;
if (unlikely(!as)) {
memcpy(valp, (void *)addr, size);
return 0;
}
if (unlikely(copy_from_proc(c->task, addr, valp, size) != (int)size)) {
debug(DEBUG_DWARF, "cannot access memory %#lx of pid %d", addr, c->task->pid);
return -DWARF_EINVAL;
}
return 0;
}
static inline __attribute__((always_inline)) int dwarf_read8(struct dwarf_addr_space *as, arch_addr_t *addr, void *valp)
{
int ret;
ret = dwarf_access_mem(as, *addr, valp, 1);
if (unlikely(ret))
return ret;
*addr += 1;
return 0;
}
static inline __attribute__((always_inline)) int dwarf_read16(struct dwarf_addr_space *as, arch_addr_t *addr, void *valp)
{
int ret;
ret = dwarf_access_mem(as, *addr, valp, 2);
if (unlikely(ret))
return ret;
*addr += 2;
return 0;
}
static inline __attribute__((always_inline)) int dwarf_read32(struct dwarf_addr_space *as, arch_addr_t *addr, void *valp)
{
int ret;
ret = dwarf_access_mem(as, *addr, valp, 4);
if (unlikely(ret))
return ret;
*addr += 4;
return 0;
}
static inline __attribute__((always_inline)) int dwarf_read64(struct dwarf_addr_space *as, arch_addr_t *addr, void *valp)
{
int ret;
ret = dwarf_access_mem(as, *addr, valp, 8);
if (unlikely(ret))
return ret;
*addr += 8;
return 0;
}
static int dwarf_readw(struct dwarf_addr_space *as, arch_addr_t *addr, arch_addr_t *valp, int is_64bit)
{
int ret;
if (is_64bit) {
uint64_t u64 = 0;
ret = dwarf_read64(as, addr, &u64);
if (valp)
*valp = u64;
}
else {
uint32_t u32 = 0;
ret = dwarf_read32(as, addr, &u32);
if (valp)
*valp = u32;
}
return ret;
}
static int dwarf_read_uleb128(struct dwarf_addr_space *as, arch_addr_t *addr, arch_addr_t *valp)
{
arch_addr_t val = 0, shift = 0;
unsigned char byte;
int ret;
do {
if (unlikely((ret = dwarf_read8(as, addr, &byte)) < 0))
return ret;
val |= ((arch_addr_t) byte & 0x7f) << shift;
shift += 7;
}
while (byte & 0x80);
if (valp)
*valp = val;
return 0;
}
static int dwarf_read_sleb128(struct dwarf_addr_space *as, arch_addr_t *addr, arch_addr_t *valp)
{
arch_addr_t val = 0, shift = 0;
unsigned char byte;
int ret;
do {
if (unlikely((ret = dwarf_read8(as, addr, &byte)) < 0))
return ret;
val |= ((arch_addr_t) byte & 0x7f) << shift;
shift += 7;
}
while (byte & 0x80);
if (shift < 8 *sizeof(arch_addr_t) && (byte & 0x40) != 0)
/* sign-extend negative value */
val |= ((arch_addr_t) -1) << shift;
if (valp)
*valp = val;
return 0;
}
static int dwarf_read_encoded_pointer(struct dwarf_addr_space *as, int local,
arch_addr_t *addr, unsigned char encoding, arch_addr_t *valp, arch_addr_t start_ip)
{
struct dwarf_addr_space *indirect_as = as;
arch_addr_t val, initial_addr = *addr;
arch_addr_t gp = as->cursor.libref->pltgot;
int is_64bit = as->is_64bit;
void *tmp_ptr;
int ret;
union {
uint16_t uval16;
uint32_t uval32;
uint64_t uval64;
int16_t sval16;
int32_t sval32;
int64_t sval64;
arch_addr_t addr;
} tmp;
memset(&tmp, 0, sizeof(tmp));
if (valp)
tmp_ptr = &tmp;
else {
valp = &val;
tmp_ptr = NULL;
}
if (local)
as = NULL;
/* DW_EH_PE_omit and DW_EH_PE_aligned don't follow the normal
format/application encoding. Handle them first. */
if (encoding == DW_EH_PE_omit) {
*valp = 0;
return 0;
}
else
if (encoding == DW_EH_PE_aligned) {
int size = is_64bit ? sizeof(uint64_t) : sizeof(uint32_t);
*addr = (initial_addr + size - 1) & -size;
if (unlikely((ret = dwarf_readw(as, addr, tmp_ptr, is_64bit)) < 0))
return ret;
*valp = tmp.addr;
return 0;
}
switch (encoding & DW_EH_PE_FORMAT_MASK) {
case DW_EH_PE_ptr:
if (unlikely((ret = dwarf_readw(as, addr, tmp_ptr, is_64bit)) < 0))
return ret;
val = tmp.addr;
break;
case DW_EH_PE_uleb128:
if (unlikely((ret = dwarf_read_uleb128(as, addr, &val)) < 0))
return ret;
break;
case DW_EH_PE_udata2:
if (unlikely((ret = dwarf_read16(as, addr, tmp_ptr)) < 0))
return ret;
val = tmp.uval16;
break;
case DW_EH_PE_udata4:
if (unlikely((ret = dwarf_read32(as, addr, tmp_ptr)) < 0))
return ret;
val = tmp.uval32;
break;
case DW_EH_PE_udata8:
if (unlikely((ret = dwarf_read64(as, addr, tmp_ptr)) < 0))
return ret;
val = tmp.uval64;
break;
case DW_EH_PE_sleb128:
if (unlikely((ret = dwarf_read_sleb128(as, addr, &val)) < 0))
return ret;
break;
case DW_EH_PE_sdata2:
if (unlikely((ret = dwarf_read16(as, addr, tmp_ptr)) < 0))
return ret;
val = tmp.sval16;
break;
case DW_EH_PE_sdata4:
if (unlikely((ret = dwarf_read32(as, addr, tmp_ptr)) < 0))
return ret;
val = tmp.sval32;
break;
case DW_EH_PE_sdata8:
if (unlikely((ret = dwarf_read64(as, addr, tmp_ptr)) < 0))
return ret;
val = tmp.sval64;
break;
default:
debug(DEBUG_DWARF, "unexpected encoding format 0x%x", encoding & DW_EH_PE_FORMAT_MASK);
return -DWARF_EINVAL;
}
if (!val) {
/* 0 is a special value and always absolute. */
*valp = 0;
return 0;
}
switch (encoding & DW_EH_PE_APPL_MASK) {
case DW_EH_PE_absptr:
break;
case DW_EH_PE_pcrel:
val += initial_addr;
break;
case DW_EH_PE_datarel:
/* XXX For now, assume that data-relative addresses are relative to the global pointer. */
val += gp;
break;
case DW_EH_PE_funcrel:
val += start_ip;
break;
case DW_EH_PE_textrel:
/* XXX For now we don't support text-rel values. */
default:
debug(DEBUG_DWARF, "unexpected application type 0x%x", encoding & DW_EH_PE_APPL_MASK);
return -DWARF_EINVAL;
}
/* Trim off any extra bits. Assume that sign extension isn't
required; the only place it is needed is MIPS kernel space
addresses. */
if (!is_64bit)
val = (uint32_t)val;
if (encoding & DW_EH_PE_indirect) {
if (indirect_as) {
arch_addr_t indirect_addr = val;
if (tmp_ptr) {
if (unlikely((ret = dwarf_readw(indirect_as, &indirect_addr, &val, is_64bit)) < 0))
return ret;
}
else
val = 0;
}
else {
debug(DEBUG_DWARF, "unexpected indirect addressing %#lx", val);
return -DWARF_EINVAL;
}
}
*valp = val;
return 0;
}
static inline int dwarf_read_encoded_pointer_local(struct dwarf_addr_space *as, arch_addr_t *addr, unsigned char encoding, arch_addr_t *valp, arch_addr_t start_ip)
{
#ifdef DEBUG
struct dwarf_cursor *c = &as->cursor;
struct libref *libref = c->libref;
if (*addr < ARCH_ADDR_T(libref->mmap_addr))
fatal("invalid access mem: addr %#lx < %p", *addr, libref->mmap_addr);
if (*addr >= ARCH_ADDR_T(libref->mmap_addr + libref->mmap_size))
fatal("invalid access mem: addr %#lx >= %p", *addr, libref->mmap_addr + libref->mmap_size);
#endif
return dwarf_read_encoded_pointer(as, 1, addr, encoding, valp, start_ip);
}
static int parse_cie(struct dwarf_addr_space *as, arch_addr_t addr, struct dwarf_cie_info *dci)
{
uint8_t version, ch, augstr[5], fde_encoding;
arch_addr_t len, cie_end_addr, aug_size;
uint32_t u32val;
uint64_t u64val;
size_t i;
int ret;
/* 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 (as->is_64bit)
fde_encoding = DW_EH_PE_udata8;
else
fde_encoding = DW_EH_PE_udata4;
dci->lsda_encoding = DW_EH_PE_omit;
if (unlikely((ret = dwarf_read32(NULL, &addr, &u32val)) < 0))
return ret;
if (u32val != 0xffffffff) {
/* the CIE is in the 32-bit DWARF format */
uint32_t cie_id;
len = u32val;
cie_end_addr = addr + len;
if (unlikely((ret = dwarf_read32(NULL, &addr, &cie_id)) < 0))
return ret;
if (cie_id) {
debug(DEBUG_DWARF, "Unexpected CIE id %x", cie_id);
return -DWARF_EINVAL;
}
}
else {
/* the CIE is in the 64-bit DWARF format */
uint64_t cie_id;
if (unlikely((ret = dwarf_read64(NULL, &addr, &u64val)) < 0))
return ret;
len = u64val;
cie_end_addr = addr + len;
if (unlikely((ret = dwarf_read64(NULL, &addr, &cie_id)) < 0))
return ret;
if (cie_id) {
debug(DEBUG_DWARF, "Unexpected CIE id %llx", (long long)cie_id);
return -DWARF_EINVAL;
}
}
dci->cie_instr_end = cie_end_addr;
if (unlikely((ret = dwarf_read8(NULL, &addr, &version)) < 0))
return ret;
if (version != DWARF_CIE_VERSION && version != DWARF_CIE_VERSION_GCC) {
debug(DEBUG_DWARF, "Got CIE version %u, expected version " STR(DWARF_CIE_VERSION) " or " STR(DWARF_CIE_VERSION_GCC), version);
return -DWARF_EBADVERSION;
}
/* read and parse the augmentation string: */
memset(augstr, 0, sizeof(augstr));
for (i = 0;;) {
if (unlikely((ret = dwarf_read8(NULL, &addr, &ch)) < 0))
return ret;
if (!ch)
break; /* end of augmentation string */
if (i < sizeof(augstr) - 1)
augstr[i++] = ch;
}
if (unlikely((ret = dwarf_read_uleb128(NULL, &addr, &dci->code_align)) < 0 || (ret = dwarf_read_sleb128(NULL, &addr, &dci->data_align)) < 0))
return ret;
/* Read the return-address column either as a u8 or as a uleb128. */
if (version == 1) {
if (unlikely((ret = dwarf_read8(NULL, &addr, &ch)) < 0))
return ret;
dci->ret_addr_column = dwarf_to_regnum(ch);
}
else {
arch_addr_t val;
if (unlikely((ret = dwarf_read_uleb128(NULL, &addr, &val)) < 0))
return ret;
dci->ret_addr_column = dwarf_to_regnum(val);
}
i = 0;
if (augstr[0] == 'z') {
dci->sized_augmentation = 1;
if (unlikely((ret = dwarf_read_uleb128(NULL, &addr, &aug_size)) < 0))
return ret;
i++;
}
for (; i < sizeof(augstr) && augstr[i]; ++i) {
switch (augstr[i]) {
case 'L':
/* read the LSDA pointer-encoding format. */
if (unlikely((ret = dwarf_read8(NULL, &addr, &ch)) < 0))
return ret;
dci->lsda_encoding = ch;
break;
case 'R':
/* read the FDE pointer-encoding format. */
if (unlikely((ret = dwarf_read8(NULL, &addr, &fde_encoding)) < 0))
return ret;
break;
case 'P':
{
uint8_t handler_encoding;
/* read the personality-routine pointer-encoding format. */
if (unlikely((ret = dwarf_read8(NULL, &addr, &handler_encoding)) < 0))
return ret;
if (unlikely((ret = dwarf_read_encoded_pointer_local(as, &addr, handler_encoding, NULL, 0)) < 0))
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:
debug(DEBUG_DWARF, "Unexpected augmentation string `%s'", augstr);
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. */
goto done;
else
return -DWARF_EINVAL;
}
}
done:
dci->fde_encoding = fde_encoding;
dci->cie_instr_start = addr;
return 0;
}
static int dwarf_extract_cfi_from_fde(struct dwarf_addr_space *as, void *addrp)
{
int ret;
uint32_t u32val;
arch_addr_t fde_end_addr, cie_addr, cie_offset_addr, cie_offset;
struct dwarf_cie_info *dci = &as->cursor.dci;
arch_addr_t addr = (arch_addr_t)addrp;
if (unlikely((ret = dwarf_read32(NULL, &addr, &u32val)) < 0))
return ret;
if (u32val != 0xffffffff) {
int32_t cie_offset32;
/* In some configurations, an FDE with a 0 length indicates the
end of the FDE-table. */
if (!u32val) {
debug(DEBUG_DWARF, "zero FDE");
return -DWARF_ENOINFO;
}
/* the FDE is in the 32-bit DWARF format */
fde_end_addr = addr + u32val;
cie_offset_addr = addr;
if (unlikely((ret = dwarf_read32(NULL, &addr, &cie_offset32)) < 0))
return ret;
cie_offset = cie_offset32;
} else {
int64_t cie_offset64;
uint64_t u64val;
/* the FDE is in the 64-bit DWARF format */
if (unlikely((ret = dwarf_read64(NULL, &addr, &u64val)) < 0))
return ret;
fde_end_addr = addr + u64val;
cie_offset_addr = addr;
if (unlikely((ret = dwarf_read64(NULL, &addr, &cie_offset64)) < 0))
return ret;
cie_offset = cie_offset64;
}
memset(dci, 0, sizeof(*dci));
/* ignore CIEs (happens during linear searches) */
if (!cie_offset)
return 0;
/* 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. */
cie_addr = cie_offset_addr - cie_offset;
if ((ret = parse_cie(as, cie_addr, dci)) < 0)
return ret;
if (unlikely((ret = dwarf_read_encoded_pointer_local(as, &addr, dci->fde_encoding, &dci->start_ip, 0)) < 0))
return ret;
/* IP-range has same encoding as FDE pointers, except that it's
always an absolute value: */
if (unlikely((ret = dwarf_read_encoded_pointer_local(as, &addr, dci->fde_encoding & DW_EH_PE_FORMAT_MASK, &dci->ip_range, 0)) < 0))
return ret;
if (dci->sized_augmentation) {
arch_addr_t aug_size;
if (unlikely((ret = dwarf_read_uleb128(NULL, &addr, &aug_size)) < 0))
return ret;
dci->fde_instr_start = addr + aug_size;
}
else
dci->fde_instr_start = addr;
dci->fde_instr_end = fde_end_addr;
if (unlikely((ret = dwarf_read_encoded_pointer_local(as, &addr, dci->lsda_encoding, NULL, dci->start_ip)) < 0))
return ret;
if (dci->have_abi_marker) {
if (unlikely((ret = dwarf_read16(NULL, &addr, &dci->abi)) < 0 || (ret = dwarf_read16(NULL, &addr, &dci->tag)) < 0))
return ret;
}
return 0;
}
int dwarf_locate_map(struct dwarf_addr_space *as, arch_addr_t ip)
{
struct dwarf_cursor *c = &as->cursor;
struct libref *libref = c->libref;
if (c->use_prev_instr)
ip -= 1;
if (likely(c->libref)) {
if (ip >= libref->txt_vaddr && ip < libref->txt_vaddr + libref->txt_size)
return 0;
}
c->libref = addr2libref(c->task->leader, ip);
if (!c->libref)
return -DWARF_ENOINFO;
return 0;
}
static const struct table_entry *lookup(const struct table_entry *table, size_t fde_count, int32_t rel_ip)
{
const struct table_entry *e, *f;
unsigned long lo, hi;
if (!fde_count)
return NULL;
lo = 0;
hi = fde_count;
f = NULL;
do {
unsigned long mid = (lo + hi) / 2;
e = table + mid;
if (rel_ip < e->start_ip_offset)
hi = mid;
else {
f = e;
lo = mid + 1;
}
} while(lo < hi);
return f;
}
static int dwarf_search_unwind_table(struct dwarf_addr_space *as, arch_addr_t ip, void *fde_tab, unsigned long fde_count)
{
const struct table_entry *e;
void *fde_addr;
int ret;
struct dwarf_cie_info *dci = &as->cursor.dci;
struct libref *libref = as->cursor.libref;
e = lookup(fde_tab, fde_count, ip - libref->eh_hdr_vaddr);
if (unlikely(!e)) {
/* IP is inside this table's range, but there is no explicit unwind info. */
debug(DEBUG_DWARF, "no unwind info found for IP %#lx", ip);
return -DWARF_ENOINFO;
}
fde_addr = libref->mmap_addr + e->fde_offset + libref->eh_hdr_offset;
if (unlikely((ret = dwarf_extract_cfi_from_fde(as, fde_addr)) < 0))
return ret;
dci->start_ip = dci->start_ip - (arch_addr_t)libref->mmap_addr + libref->txt_vaddr;
if (!as->is_64bit)
dci->start_ip &= 0xffffffff;
if (unlikely(ip < dci->start_ip || ip >= dci->start_ip + dci->ip_range)) {
debug(DEBUG_DWARF, "IP %#lx out of range %#lx-%#lx", ip, dci->start_ip, dci->start_ip + dci->ip_range);
return -DWARF_ENOINFO;
}
return 0;
}
static int dwarf_access_reg(struct dwarf_addr_space *as, unsigned int reg, arch_addr_t *valp)
{
struct dwarf_cursor *c = &as->cursor;
int map = dwarf_arch_map_reg(as, reg);
if (unlikely(map < 0)) {
debug(DEBUG_DWARF, "could not map register %u", reg);
return map;
}
*valp = fetch_reg(c->task, map);
return 0;
}
int dwarf_get(struct dwarf_addr_space *as, struct dwarf_loc loc, arch_addr_t *valp)
{
arch_addr_t addr = DWARF_GET_LOC(loc);
if (DWARF_IS_REG_LOC(loc))
return dwarf_access_reg(as, addr, valp);
if (!as->is_64bit)
addr &= 0xffffffff;
if (DWARF_IS_MEM_LOC(loc))
return dwarf_readw(as, &addr, valp, as->is_64bit);
*valp = addr;
return 0;
}
static int err_inval_reg_num(unsigned int regnum)
{
debug(DEBUG_DWARF, "Invalid register number %u", regnum);
return -DWARF_EBADREG;
}
static int dwarf_get_reg(struct dwarf_addr_space *as, unsigned int reg, arch_addr_t *valp)
{
struct dwarf_cursor *c = &as->cursor;
if (unlikely(reg >= as->num_regs))
return err_inval_reg_num((unsigned int)*valp);
if (as->ip_reg == reg) {
*valp = c->ip;