-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathframe.c
2324 lines (1917 loc) · 69.4 KB
/
frame.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
/* Frame unwinder for frames with DWARF Call Frame Information.
Copyright (C) 2003-2025 Free Software Foundation, Inc.
Copyright (C) 2020-2025 Advanced Micro Devices, Inc. All rights reserved.
Contributed by Mark Kettenis.
This file is part of GDB.
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 3 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, see <http://www.gnu.org/licenses/>. */
#include "dwarf2/expr.h"
#include "dwarf2.h"
#include "dwarf2/leb.h"
#include "frame.h"
#include "frame-base.h"
#include "frame-unwind.h"
#include "gdbtypes.h"
#include "symtab.h"
#include "objfiles.h"
#include "regcache.h"
#include "value.h"
#include "record.h"
#include "extract-store-integer.h"
#include "producer.h"
#include "complaints.h"
#include "dwarf2/frame.h"
#include "dwarf2/read.h"
#include "dwarf2/public.h"
#include "dwarf2/loc.h"
#include "dwarf2/frame-tailcall.h"
#include "gdbsupport/gdb_binary_search.h"
#if GDB_SELF_TEST
#include "gdbsupport/selftest.h"
#include "selftest-arch.h"
#endif
#include <algorithm>
struct comp_unit;
/* Call Frame Information (CFI). */
/* Common Information Entry (CIE). */
struct dwarf2_cie
{
/* Computation Unit for this CIE. */
struct comp_unit *unit;
/* Offset into the .debug_frame section where this CIE was found.
Used to identify this CIE. */
ULONGEST cie_pointer;
/* Constant that is factored out of all advance location
instructions. */
ULONGEST code_alignment_factor;
/* Constants that is factored out of all offset instructions. */
LONGEST data_alignment_factor;
/* Return address column. */
ULONGEST return_address_register;
/* Instruction sequence to initialize a register set. */
const gdb_byte *initial_instructions;
const gdb_byte *end;
/* Saved augmentation, in case it's needed later. */
const char *augmentation;
/* Encoding of addresses. */
gdb_byte encoding;
/* Target address size in bytes. */
int addr_size;
/* Target pointer size in bytes. */
int ptr_size;
/* True if a 'z' augmentation existed. */
unsigned char saw_z_augmentation;
/* True if an 'S' augmentation existed. */
unsigned char signal_frame;
/* The version recorded in the CIE. */
unsigned char version;
/* The segment size. */
unsigned char segment_size;
};
/* The CIE table is used to find CIEs during parsing, but then
discarded. It maps from the CIE's offset to the CIE. */
using dwarf2_cie_table = gdb::unordered_map<ULONGEST, dwarf2_cie *>;
/* Frame Description Entry (FDE). */
struct dwarf2_fde
{
/* Return the final location in this FDE. */
unrelocated_addr end_addr () const
{
return (unrelocated_addr) ((ULONGEST) initial_location
+ address_range);
}
/* CIE for this FDE. */
struct dwarf2_cie *cie;
/* First location associated with this FDE. */
unrelocated_addr initial_location;
/* Number of bytes of program instructions described by this FDE. */
ULONGEST address_range;
/* Instruction sequence. */
const gdb_byte *instructions;
const gdb_byte *end;
/* True if this FDE is read from a .eh_frame instead of a .debug_frame
section. */
unsigned char eh_frame_p;
};
typedef std::vector<dwarf2_fde *> dwarf2_fde_table;
/* A minimal decoding of DWARF2 compilation units. We only decode
what's needed to get to the call frame information. */
struct comp_unit
{
comp_unit (struct objfile *objf)
: abfd (objf->obfd.get ())
{
}
/* Keep the bfd convenient. */
bfd *abfd;
/* Pointer to the .debug_frame section loaded into memory. */
const gdb_byte *dwarf_frame_buffer = nullptr;
/* Length of the loaded .debug_frame section. */
bfd_size_type dwarf_frame_size = 0;
/* Pointer to the .debug_frame section. */
asection *dwarf_frame_section = nullptr;
/* Base for DW_EH_PE_datarel encodings. */
bfd_vma dbase = 0;
/* Base for DW_EH_PE_textrel encodings. */
bfd_vma tbase = 0;
/* The FDE table. */
dwarf2_fde_table fde_table;
/* Hold data used by this module. */
auto_obstack obstack;
};
static struct dwarf2_fde *dwarf2_frame_find_fde
(CORE_ADDR *pc, dwarf2_per_objfile **out_per_objfile);
static int dwarf2_frame_adjust_regnum (struct gdbarch *gdbarch, int regnum,
int eh_frame_p);
static ULONGEST read_encoded_value (struct comp_unit *unit, gdb_byte encoding,
int ptr_len, const gdb_byte *buf,
unsigned int *bytes_read_ptr,
unrelocated_addr func_base);
/* Store the length the expression for the CFA in the `cfa_reg' field,
which is unused in that case. */
#define cfa_exp_len cfa_reg
dwarf2_frame_state::dwarf2_frame_state (CORE_ADDR pc_, struct dwarf2_cie *cie)
: pc (pc_), data_align (cie->data_alignment_factor),
code_align (cie->code_alignment_factor),
retaddr_column (cie->return_address_register)
{
}
/* Return the value of register number REG (a DWARF register number),
read as an address in a given FRAME. */
static CORE_ADDR
read_addr_from_reg (frame_info_ptr frame, int reg)
{
gdbarch *arch = get_frame_arch (frame);
int regnum = dwarf_reg_to_regnum_or_error (arch, reg);
return address_from_register (regnum, frame);
}
/* Execute the required actions for both the DW_CFA_restore and
DW_CFA_restore_extended instructions. */
static void
dwarf2_restore_rule (struct gdbarch *gdbarch, ULONGEST reg_num,
struct dwarf2_frame_state *fs, int eh_frame_p)
{
ULONGEST reg;
reg = dwarf2_frame_adjust_regnum (gdbarch, reg_num, eh_frame_p);
fs->regs.alloc_regs (reg + 1);
/* Check if this register was explicitly initialized in the
CIE initial instructions. If not, default the rule to
UNSPECIFIED. */
if (reg < fs->initial.reg.size ())
fs->regs.reg[reg] = fs->initial.reg[reg];
else
fs->regs.reg[reg].how = DWARF2_FRAME_REG_UNSPECIFIED;
if (fs->regs.reg[reg].how == DWARF2_FRAME_REG_UNSPECIFIED)
{
int regnum = dwarf_reg_to_regnum (gdbarch, reg);
complaint (_("\
incomplete CFI data; DW_CFA_restore unspecified\n\
register %s (#%d) at %s"),
gdbarch_register_name (gdbarch, regnum), regnum,
paddress (gdbarch, fs->pc));
}
}
static value *
execute_stack_op (const gdb_byte *exp, ULONGEST len, int addr_size,
const frame_info_ptr &this_frame, CORE_ADDR initial,
int initial_in_stack_memory, dwarf2_per_objfile *per_objfile,
struct type* type = nullptr, bool as_lval = true)
{
scoped_value_mark free_values;
struct type *init_type = address_type (per_objfile->objfile->arch (),
addr_size);
value *init_value = value_at_lazy (init_type, initial);
std::vector<value *> init_values;
init_value->set_stack (initial_in_stack_memory);
init_values.push_back (init_value);
value *result_val
= dwarf2_evaluate (exp, len, true, per_objfile, nullptr,
this_frame, addr_size, &init_values, nullptr, type);
/* We need to clean up all the values that are not needed any more.
The problem with a value_ref_ptr class is that it disconnects the
RETVAL from the value garbage collection, so we need to make
a copy of that value on the stack to keep everything consistent.
The value_ref_ptr will clean up after itself at the end of this block. */
value_ref_ptr value_holder = value_ref_ptr::new_reference (result_val);
free_values.free_to_mark ();
return result_val->copy ();
}
/* Execute FDE program from INSN_PTR possibly up to INSN_END or up to inferior
PC. Modify FS state accordingly. Return current INSN_PTR where the
execution has stopped, one can resume it on the next call. */
static const gdb_byte *
execute_cfa_program (struct dwarf2_fde *fde, const gdb_byte *insn_ptr,
const gdb_byte *insn_end, struct gdbarch *gdbarch,
CORE_ADDR pc, struct dwarf2_frame_state *fs,
CORE_ADDR text_offset)
{
int eh_frame_p = fde->eh_frame_p;
unsigned int bytes_read;
enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
while (insn_ptr < insn_end && fs->pc <= pc)
{
gdb_byte insn = *insn_ptr++;
uint64_t utmp, reg;
int64_t offset;
if ((insn & 0xc0) == DW_CFA_advance_loc)
fs->pc += (insn & 0x3f) * fs->code_align;
else if ((insn & 0xc0) == DW_CFA_offset)
{
reg = insn & 0x3f;
reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
offset = utmp * fs->data_align;
fs->regs.alloc_regs (reg + 1);
fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
fs->regs.reg[reg].loc.offset = offset;
}
else if ((insn & 0xc0) == DW_CFA_restore)
{
reg = insn & 0x3f;
dwarf2_restore_rule (gdbarch, reg, fs, eh_frame_p);
}
else
{
switch (insn)
{
case DW_CFA_set_loc:
fs->pc = read_encoded_value (fde->cie->unit, fde->cie->encoding,
fde->cie->ptr_size, insn_ptr,
&bytes_read, fde->initial_location);
/* Apply the text offset for relocatable objects. */
fs->pc += text_offset;
insn_ptr += bytes_read;
break;
case DW_CFA_advance_loc1:
utmp = extract_unsigned_integer (insn_ptr, 1, byte_order);
fs->pc += utmp * fs->code_align;
insn_ptr++;
break;
case DW_CFA_advance_loc2:
utmp = extract_unsigned_integer (insn_ptr, 2, byte_order);
fs->pc += utmp * fs->code_align;
insn_ptr += 2;
break;
case DW_CFA_advance_loc4:
utmp = extract_unsigned_integer (insn_ptr, 4, byte_order);
fs->pc += utmp * fs->code_align;
insn_ptr += 4;
break;
case DW_CFA_offset_extended:
insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®);
reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
offset = utmp * fs->data_align;
fs->regs.alloc_regs (reg + 1);
fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
fs->regs.reg[reg].loc.offset = offset;
break;
case DW_CFA_restore_extended:
insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®);
dwarf2_restore_rule (gdbarch, reg, fs, eh_frame_p);
break;
case DW_CFA_undefined:
insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®);
reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
fs->regs.alloc_regs (reg + 1);
fs->regs.reg[reg].how = DWARF2_FRAME_REG_UNDEFINED;
break;
case DW_CFA_same_value:
insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®);
reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
fs->regs.alloc_regs (reg + 1);
fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAME_VALUE;
break;
case DW_CFA_register:
insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®);
reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
utmp = dwarf2_frame_adjust_regnum (gdbarch, utmp, eh_frame_p);
fs->regs.alloc_regs (reg + 1);
fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_REG;
fs->regs.reg[reg].loc.reg = utmp;
break;
case DW_CFA_remember_state:
{
struct dwarf2_frame_state_reg_info *new_rs;
new_rs = new dwarf2_frame_state_reg_info (fs->regs);
fs->regs.prev = new_rs;
}
break;
case DW_CFA_restore_state:
{
struct dwarf2_frame_state_reg_info *old_rs = fs->regs.prev;
if (old_rs == NULL)
{
complaint (_("\
bad CFI data; mismatched DW_CFA_restore_state at %s"),
paddress (gdbarch, fs->pc));
}
else
fs->regs = std::move (*old_rs);
}
break;
case DW_CFA_def_cfa:
insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®);
fs->regs.cfa_reg = reg;
insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
if (fs->armcc_cfa_offsets_sf)
utmp *= fs->data_align;
fs->regs.cfa_offset = utmp;
fs->regs.cfa_how = CFA_REG_OFFSET;
break;
case DW_CFA_def_cfa_register:
insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®);
fs->regs.cfa_reg = dwarf2_frame_adjust_regnum (gdbarch, reg,
eh_frame_p);
fs->regs.cfa_how = CFA_REG_OFFSET;
break;
case DW_CFA_def_cfa_offset:
insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
if (fs->armcc_cfa_offsets_sf)
utmp *= fs->data_align;
fs->regs.cfa_offset = utmp;
/* cfa_how deliberately not set. */
break;
case DW_CFA_nop:
break;
case DW_CFA_def_cfa_expression:
insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
fs->regs.cfa_exp_len = utmp;
fs->regs.cfa_exp = insn_ptr;
fs->regs.cfa_how = CFA_EXP;
insn_ptr += fs->regs.cfa_exp_len;
break;
case DW_CFA_def_aspace_cfa:
insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®);
fs->regs.cfa_reg = reg;
insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
if (fs->armcc_cfa_offsets_sf)
utmp *= fs->data_align;
fs->regs.cfa_offset = utmp;
insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
fs->regs.cfa_aspace = utmp;
fs->regs.cfa_how = CFA_REG_OFFSET;
break;
case DW_CFA_expression:
insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®);
reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
fs->regs.alloc_regs (reg + 1);
insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
fs->regs.reg[reg].loc.exp.start = insn_ptr;
fs->regs.reg[reg].loc.exp.len = utmp;
fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_EXP;
insn_ptr += utmp;
break;
case DW_CFA_offset_extended_sf:
insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®);
reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
insn_ptr = safe_read_sleb128 (insn_ptr, insn_end, &offset);
offset *= fs->data_align;
fs->regs.alloc_regs (reg + 1);
fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
fs->regs.reg[reg].loc.offset = offset;
break;
case DW_CFA_val_offset:
insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®);
fs->regs.alloc_regs (reg + 1);
insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
offset = utmp * fs->data_align;
fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_OFFSET;
fs->regs.reg[reg].loc.offset = offset;
break;
case DW_CFA_val_offset_sf:
insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®);
fs->regs.alloc_regs (reg + 1);
insn_ptr = safe_read_sleb128 (insn_ptr, insn_end, &offset);
offset *= fs->data_align;
fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_OFFSET;
fs->regs.reg[reg].loc.offset = offset;
break;
case DW_CFA_val_expression:
insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®);
fs->regs.alloc_regs (reg + 1);
insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
fs->regs.reg[reg].loc.exp.start = insn_ptr;
fs->regs.reg[reg].loc.exp.len = utmp;
fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_EXP;
insn_ptr += utmp;
break;
case DW_CFA_def_cfa_sf:
insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®);
fs->regs.cfa_reg = dwarf2_frame_adjust_regnum (gdbarch, reg,
eh_frame_p);
insn_ptr = safe_read_sleb128 (insn_ptr, insn_end, &offset);
fs->regs.cfa_offset = offset * fs->data_align;
fs->regs.cfa_how = CFA_REG_OFFSET;
break;
case DW_CFA_def_cfa_offset_sf:
insn_ptr = safe_read_sleb128 (insn_ptr, insn_end, &offset);
fs->regs.cfa_offset = offset * fs->data_align;
/* cfa_how deliberately not set. */
break;
case DW_CFA_GNU_args_size:
/* Ignored. */
insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
break;
case DW_CFA_GNU_negative_offset_extended:
insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, ®);
reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
offset = utmp * fs->data_align;
fs->regs.alloc_regs (reg + 1);
fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
fs->regs.reg[reg].loc.offset = -offset;
break;
default:
if (insn >= DW_CFA_lo_user && insn <= DW_CFA_hi_user)
{
/* Handle vendor-specific CFI for different architectures. */
if (!gdbarch_execute_dwarf_cfa_vendor_op (gdbarch, insn, fs))
error (_("Call Frame Instruction op %d in vendor extension "
"space is not handled on this architecture."),
insn);
}
else
internal_error (_("Unknown CFI encountered."));
}
}
}
if (fs->initial.reg.empty ())
{
/* Don't allow remember/restore between CIE and FDE programs. */
delete fs->regs.prev;
fs->regs.prev = NULL;
}
return insn_ptr;
}
#if GDB_SELF_TEST
namespace selftests {
/* Unit test to function execute_cfa_program. */
static void
execute_cfa_program_test (struct gdbarch *gdbarch)
{
struct dwarf2_fde fde;
struct dwarf2_cie cie;
memset (&fde, 0, sizeof fde);
memset (&cie, 0, sizeof cie);
cie.data_alignment_factor = -4;
cie.code_alignment_factor = 2;
fde.cie = &cie;
dwarf2_frame_state fs (0, fde.cie);
gdb_byte insns[] =
{
DW_CFA_def_cfa, 1, 4, /* DW_CFA_def_cfa: r1 ofs 4 */
DW_CFA_offset | 0x2, 1, /* DW_CFA_offset: r2 at cfa-4 */
DW_CFA_remember_state,
DW_CFA_restore_state,
};
const gdb_byte *insn_end = insns + sizeof (insns);
const gdb_byte *out = execute_cfa_program (&fde, insns, insn_end, gdbarch,
0, &fs, 0);
SELF_CHECK (out == insn_end);
SELF_CHECK (fs.pc == 0);
/* The instructions above only use r1 and r2, but the register numbers
used are adjusted by dwarf2_frame_adjust_regnum. */
auto r1 = dwarf2_frame_adjust_regnum (gdbarch, 1, fde.eh_frame_p);
auto r2 = dwarf2_frame_adjust_regnum (gdbarch, 2, fde.eh_frame_p);
SELF_CHECK (fs.regs.reg.size () == (std::max (r1, r2) + 1));
SELF_CHECK (fs.regs.reg[r2].how == DWARF2_FRAME_REG_SAVED_OFFSET);
SELF_CHECK (fs.regs.reg[r2].loc.offset == -4);
for (auto i = 0; i < fs.regs.reg.size (); i++)
if (i != r2)
SELF_CHECK (fs.regs.reg[i].how == DWARF2_FRAME_REG_UNSPECIFIED);
SELF_CHECK (fs.regs.cfa_reg == 1);
SELF_CHECK (fs.regs.cfa_offset == 4);
SELF_CHECK (fs.regs.cfa_how == CFA_REG_OFFSET);
SELF_CHECK (fs.regs.cfa_exp == NULL);
SELF_CHECK (fs.regs.prev == NULL);
}
} /* namespace selftests */
#endif /* GDB_SELF_TEST */
/* Architecture-specific operations. */
static void dwarf2_frame_default_init_reg (struct gdbarch *gdbarch,
int regnum,
struct dwarf2_frame_state_reg *reg,
const frame_info_ptr &this_frame);
struct dwarf2_frame_ops
{
/* Pre-initialize the register state REG for register REGNUM. */
void (*init_reg) (struct gdbarch *, int, struct dwarf2_frame_state_reg *,
const frame_info_ptr &)
= dwarf2_frame_default_init_reg;
/* Check whether the THIS_FRAME is a signal trampoline. */
int (*signal_frame_p) (struct gdbarch *, const frame_info_ptr &) = nullptr;
/* Convert .eh_frame register number to DWARF register number, or
adjust .debug_frame register number. */
int (*adjust_regnum) (struct gdbarch *, int, int) = nullptr;
};
/* Per-architecture data key. */
static const registry<gdbarch>::key<dwarf2_frame_ops> dwarf2_frame_data;
/* Get or initialize the frame ops. */
static dwarf2_frame_ops *
get_frame_ops (struct gdbarch *gdbarch)
{
dwarf2_frame_ops *result = dwarf2_frame_data.get (gdbarch);
if (result == nullptr)
result = dwarf2_frame_data.emplace (gdbarch);
return result;
}
/* Default architecture-specific register state initialization
function. */
static void
dwarf2_frame_default_init_reg (struct gdbarch *gdbarch, int regnum,
struct dwarf2_frame_state_reg *reg,
const frame_info_ptr &this_frame)
{
/* If we have a register that acts as a program counter, mark it as
a destination for the return address. If we have a register that
serves as the stack pointer, arrange for it to be filled with the
call frame address (CFA). The other registers are marked as
unspecified.
We copy the return address to the program counter, since many
parts in GDB assume that it is possible to get the return address
by unwinding the program counter register. However, on ISA's
with a dedicated return address register, the CFI usually only
contains information to unwind that return address register.
The reason we're treating the stack pointer special here is
because in many cases GCC doesn't emit CFI for the stack pointer
and implicitly assumes that it is equal to the CFA. This makes
some sense since the DWARF specification (version 3, draft 8,
p. 102) says that:
"Typically, the CFA is defined to be the value of the stack
pointer at the call site in the previous frame (which may be
different from its value on entry to the current frame)."
However, this isn't true for all platforms supported by GCC
(e.g. IBM S/390 and zSeries). Those architectures should provide
their own architecture-specific initialization function. */
if (regnum == gdbarch_pc_regnum (gdbarch))
reg->how = DWARF2_FRAME_REG_RA;
else if (regnum == gdbarch_sp_regnum (gdbarch))
reg->how = DWARF2_FRAME_REG_CFA;
}
/* Set the architecture-specific register state initialization
function for GDBARCH to INIT_REG. */
void
dwarf2_frame_set_init_reg (struct gdbarch *gdbarch,
void (*init_reg) (struct gdbarch *, int,
struct dwarf2_frame_state_reg *,
const frame_info_ptr &))
{
struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch);
ops->init_reg = init_reg;
}
/* Pre-initialize the register state REG for register REGNUM. */
static void
dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
struct dwarf2_frame_state_reg *reg,
const frame_info_ptr &this_frame)
{
struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch);
ops->init_reg (gdbarch, regnum, reg, this_frame);
reg->evaluated = false;
}
/* Set the architecture-specific signal trampoline recognition
function for GDBARCH to SIGNAL_FRAME_P. */
void
dwarf2_frame_set_signal_frame_p (struct gdbarch *gdbarch,
int (*signal_frame_p) (struct gdbarch *,
const frame_info_ptr &))
{
struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch);
ops->signal_frame_p = signal_frame_p;
}
/* Query the architecture-specific signal frame recognizer for
THIS_FRAME. */
static int
dwarf2_frame_signal_frame_p (struct gdbarch *gdbarch,
const frame_info_ptr &this_frame)
{
struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch);
if (ops->signal_frame_p == NULL)
return 0;
return ops->signal_frame_p (gdbarch, this_frame);
}
/* Set the architecture-specific adjustment of .eh_frame and .debug_frame
register numbers. */
void
dwarf2_frame_set_adjust_regnum (struct gdbarch *gdbarch,
int (*adjust_regnum) (struct gdbarch *,
int, int))
{
struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch);
ops->adjust_regnum = adjust_regnum;
}
/* Translate a .eh_frame register to DWARF register, or adjust a .debug_frame
register. */
static int
dwarf2_frame_adjust_regnum (struct gdbarch *gdbarch,
int regnum, int eh_frame_p)
{
struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch);
if (ops->adjust_regnum == NULL)
return regnum;
return ops->adjust_regnum (gdbarch, regnum, eh_frame_p);
}
static void
dwarf2_frame_find_quirks (struct dwarf2_frame_state *fs,
struct dwarf2_fde *fde)
{
struct compunit_symtab *cust;
cust = find_pc_compunit_symtab (fs->pc);
if (cust == NULL)
return;
if (producer_is_realview (cust->producer ()))
{
if (fde->cie->version == 1)
fs->armcc_cfa_offsets_sf = 1;
if (fde->cie->version == 1)
fs->armcc_cfa_offsets_reversed = 1;
/* The reversed offset problem is present in some compilers
using DWARF3, but it was eventually fixed. Check the ARM
defined augmentations, which are in the format "armcc" followed
by a list of one-character options. The "+" option means
this problem is fixed (no quirk needed). If the armcc
augmentation is missing, the quirk is needed. */
if (fde->cie->version == 3
&& (!startswith (fde->cie->augmentation, "armcc")
|| strchr (fde->cie->augmentation + 5, '+') == NULL))
fs->armcc_cfa_offsets_reversed = 1;
return;
}
}
/* See dwarf2/frame.h. */
int
dwarf2_fetch_cfa_info (struct gdbarch *gdbarch, CORE_ADDR pc,
dwarf2_per_cu *data, int *regnum_out,
LONGEST *offset_out, CORE_ADDR *text_offset_out,
const gdb_byte **cfa_start_out,
const gdb_byte **cfa_end_out)
{
struct dwarf2_fde *fde;
dwarf2_per_objfile *per_objfile;
CORE_ADDR pc1 = pc;
/* Find the correct FDE. */
fde = dwarf2_frame_find_fde (&pc1, &per_objfile);
if (fde == NULL)
error (_("Could not compute CFA; needed to translate this expression"));
gdb_assert (per_objfile != nullptr);
dwarf2_frame_state fs (pc1, fde->cie);
/* Check for "quirks" - known bugs in producers. */
dwarf2_frame_find_quirks (&fs, fde);
/* First decode all the insns in the CIE. */
execute_cfa_program (fde, fde->cie->initial_instructions,
fde->cie->end, gdbarch, pc, &fs,
per_objfile->objfile->text_section_offset ());
/* Save the initialized register set. */
fs.initial = fs.regs;
/* Then decode the insns in the FDE up to our target PC. */
execute_cfa_program (fde, fde->instructions, fde->end, gdbarch, pc, &fs,
per_objfile->objfile->text_section_offset ());
/* Calculate the CFA. */
switch (fs.regs.cfa_how)
{
case CFA_REG_OFFSET:
{
int regnum = dwarf_reg_to_regnum_or_error (gdbarch, fs.regs.cfa_reg);
*regnum_out = regnum;
if (fs.armcc_cfa_offsets_reversed)
*offset_out = -fs.regs.cfa_offset;
else
*offset_out = fs.regs.cfa_offset;
return 1;
}
case CFA_EXP:
*text_offset_out = per_objfile->objfile->text_section_offset ();
*cfa_start_out = fs.regs.cfa_exp;
*cfa_end_out = fs.regs.cfa_exp + fs.regs.cfa_exp_len;
return 0;
default:
internal_error (_("Unknown CFA rule."));
}
}
/* Custom function data object for architecture specific prev_register
implementation. Main purpose of this object is to allow caching of
expensive data lookups in the prev_register handling. */
struct dwarf2_frame_fn_data
{
/* The cookie to identify the custom function data by. */
fn_prev_register cookie;
/* The custom function data. */
void *data;
/* Pointer to the next custom function data object for this frame. */
struct dwarf2_frame_fn_data *next;
};
struct dwarf2_frame_cache
{
/* DWARF Call Frame Address. */
CORE_ADDR cfa;
/* Set if the return address column was marked as unavailable
(required non-collected memory or registers to compute). */
int unavailable_retaddr;
/* Set if the return address column was marked as undefined. */
int undefined_retaddr;
/* Saved registers, indexed by GDB register number, not by DWARF
register number. */
struct dwarf2_frame_state_reg *reg;
/* Return address register. */
struct dwarf2_frame_state_reg retaddr_reg;
/* Target address size in bytes. */
int addr_size;
/* The dwarf2_per_objfile from which this frame description came. */
dwarf2_per_objfile *per_objfile;
/* If not NULL then this frame is the bottom frame of a TAILCALL_FRAME
sequence. If NULL then it is a normal case with no TAILCALL_FRAME
involved. Non-bottom frames of a virtual tail call frames chain use
dwarf2_tailcall_frame_unwind unwinder so this field does not apply for
them. */
void *tailcall_cache;
struct dwarf2_frame_fn_data *fn_data;
};
static struct dwarf2_frame_cache *
dwarf2_frame_cache (const frame_info_ptr &this_frame, void **this_cache)
{
struct gdbarch *gdbarch = get_frame_arch (this_frame);
const int num_regs = gdbarch_num_cooked_regs (gdbarch);
struct dwarf2_frame_cache *cache;
struct dwarf2_fde *fde;
CORE_ADDR entry_pc;
const gdb_byte *instr;
if (*this_cache)
return (struct dwarf2_frame_cache *) *this_cache;
/* Allocate a new cache. */
cache = FRAME_OBSTACK_ZALLOC (struct dwarf2_frame_cache);
cache->reg = FRAME_OBSTACK_CALLOC (num_regs, struct dwarf2_frame_state_reg);
*this_cache = cache;
/* Unwind the PC.
Note that if the next frame is never supposed to return (i.e. a call
to abort), the compiler might optimize away the instruction at
its return address. As a result the return address will
point at some random instruction, and the CFI for that
instruction is probably worthless to us. GCC's unwinder solves
this problem by subtracting 1 from the return address to get an
address in the middle of a presumed call instruction (or the
instruction in the associated delay slot). This should only be
done for "normal" frames and not for resume-type frames (signal
handlers, sentinel frames, dummy frames). The function
get_frame_address_in_block does just this. It's not clear how
reliable the method is though; there is the potential for the
register state pre-call being different to that on return. */
CORE_ADDR pc1 = get_frame_address_in_block (this_frame);
/* Find the correct FDE. */
fde = dwarf2_frame_find_fde (&pc1, &cache->per_objfile);
gdb_assert (fde != NULL);
gdb_assert (cache->per_objfile != nullptr);
CORE_ADDR text_offset = cache->per_objfile->objfile->text_section_offset ();
/* Allocate and initialize the frame state. */
struct dwarf2_frame_state fs (pc1, fde->cie);
cache->addr_size = fde->cie->addr_size;
/* Check for "quirks" - known bugs in producers. */
dwarf2_frame_find_quirks (&fs, fde);
/* First decode all the insns in the CIE. */
execute_cfa_program (fde, fde->cie->initial_instructions,
fde->cie->end, gdbarch,
get_frame_address_in_block (this_frame), &fs,
text_offset);
/* Save the initialized register set. */
fs.initial = fs.regs;
/* Fetching the entry pc for THIS_FRAME won't necessarily result
in an address that's within the range of FDE locations. This
is due to the possibility of the function occupying non-contiguous
ranges. */
LONGEST entry_cfa_sp_offset;
int entry_cfa_sp_offset_p = 0;
if (get_frame_func_if_available (this_frame, &entry_pc)
&& fde->initial_location <= (unrelocated_addr) (entry_pc - text_offset)
&& (unrelocated_addr) (entry_pc - text_offset) < fde->end_addr ())
{
/* Decode the insns in the FDE up to the entry PC. */
instr = execute_cfa_program (fde, fde->instructions, fde->end, gdbarch,