]> Git Repo - binutils.git/blob - gdb/dwarf2/frame.c
Automatic date update in version.in
[binutils.git] / gdb / dwarf2 / frame.c
1 /* Frame unwinder for frames with DWARF Call Frame Information.
2
3    Copyright (C) 2003-2022 Free Software Foundation, Inc.
4
5    Contributed by Mark Kettenis.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22 #include "defs.h"
23 #include "dwarf2/expr.h"
24 #include "dwarf2.h"
25 #include "dwarf2/leb.h"
26 #include "frame.h"
27 #include "frame-base.h"
28 #include "frame-unwind.h"
29 #include "gdbcore.h"
30 #include "gdbtypes.h"
31 #include "symtab.h"
32 #include "objfiles.h"
33 #include "regcache.h"
34 #include "value.h"
35 #include "record.h"
36
37 #include "complaints.h"
38 #include "dwarf2/frame.h"
39 #include "dwarf2/read.h"
40 #include "dwarf2/public.h"
41 #include "ax.h"
42 #include "dwarf2/loc.h"
43 #include "dwarf2/frame-tailcall.h"
44 #include "gdbsupport/gdb_binary_search.h"
45 #if GDB_SELF_TEST
46 #include "gdbsupport/selftest.h"
47 #include "selftest-arch.h"
48 #endif
49 #include <unordered_map>
50
51 #include <algorithm>
52
53 struct comp_unit;
54
55 /* Call Frame Information (CFI).  */
56
57 /* Common Information Entry (CIE).  */
58
59 struct dwarf2_cie
60 {
61   /* Computation Unit for this CIE.  */
62   struct comp_unit *unit;
63
64   /* Offset into the .debug_frame section where this CIE was found.
65      Used to identify this CIE.  */
66   ULONGEST cie_pointer;
67
68   /* Constant that is factored out of all advance location
69      instructions.  */
70   ULONGEST code_alignment_factor;
71
72   /* Constants that is factored out of all offset instructions.  */
73   LONGEST data_alignment_factor;
74
75   /* Return address column.  */
76   ULONGEST return_address_register;
77
78   /* Instruction sequence to initialize a register set.  */
79   const gdb_byte *initial_instructions;
80   const gdb_byte *end;
81
82   /* Saved augmentation, in case it's needed later.  */
83   char *augmentation;
84
85   /* Encoding of addresses.  */
86   gdb_byte encoding;
87
88   /* Target address size in bytes.  */
89   int addr_size;
90
91   /* Target pointer size in bytes.  */
92   int ptr_size;
93
94   /* True if a 'z' augmentation existed.  */
95   unsigned char saw_z_augmentation;
96
97   /* True if an 'S' augmentation existed.  */
98   unsigned char signal_frame;
99
100   /* The version recorded in the CIE.  */
101   unsigned char version;
102
103   /* The segment size.  */
104   unsigned char segment_size;
105 };
106
107 /* The CIE table is used to find CIEs during parsing, but then
108    discarded.  It maps from the CIE's offset to the CIE.  */
109 typedef std::unordered_map<ULONGEST, dwarf2_cie *> dwarf2_cie_table;
110
111 /* Frame Description Entry (FDE).  */
112
113 struct dwarf2_fde
114 {
115   /* CIE for this FDE.  */
116   struct dwarf2_cie *cie;
117
118   /* First location associated with this FDE.  */
119   CORE_ADDR initial_location;
120
121   /* Number of bytes of program instructions described by this FDE.  */
122   CORE_ADDR address_range;
123
124   /* Instruction sequence.  */
125   const gdb_byte *instructions;
126   const gdb_byte *end;
127
128   /* True if this FDE is read from a .eh_frame instead of a .debug_frame
129      section.  */
130   unsigned char eh_frame_p;
131 };
132
133 typedef std::vector<dwarf2_fde *> dwarf2_fde_table;
134
135 /* A minimal decoding of DWARF2 compilation units.  We only decode
136    what's needed to get to the call frame information.  */
137
138 struct comp_unit
139 {
140   comp_unit (struct objfile *objf)
141     : abfd (objf->obfd.get ())
142   {
143   }
144
145   /* Keep the bfd convenient.  */
146   bfd *abfd;
147
148   /* Pointer to the .debug_frame section loaded into memory.  */
149   const gdb_byte *dwarf_frame_buffer = nullptr;
150
151   /* Length of the loaded .debug_frame section.  */
152   bfd_size_type dwarf_frame_size = 0;
153
154   /* Pointer to the .debug_frame section.  */
155   asection *dwarf_frame_section = nullptr;
156
157   /* Base for DW_EH_PE_datarel encodings.  */
158   bfd_vma dbase = 0;
159
160   /* Base for DW_EH_PE_textrel encodings.  */
161   bfd_vma tbase = 0;
162
163   /* The FDE table.  */
164   dwarf2_fde_table fde_table;
165
166   /* Hold data used by this module.  */
167   auto_obstack obstack;
168 };
169
170 static struct dwarf2_fde *dwarf2_frame_find_fde
171   (CORE_ADDR *pc, dwarf2_per_objfile **out_per_objfile);
172
173 static int dwarf2_frame_adjust_regnum (struct gdbarch *gdbarch, int regnum,
174                                        int eh_frame_p);
175
176 static CORE_ADDR read_encoded_value (struct comp_unit *unit, gdb_byte encoding,
177                                      int ptr_len, const gdb_byte *buf,
178                                      unsigned int *bytes_read_ptr,
179                                      CORE_ADDR func_base);
180 \f
181
182 /* See dwarf2/frame.h.  */
183 bool dwarf2_frame_unwinders_enabled_p = true;
184
185 /* Store the length the expression for the CFA in the `cfa_reg' field,
186    which is unused in that case.  */
187 #define cfa_exp_len cfa_reg
188
189 dwarf2_frame_state::dwarf2_frame_state (CORE_ADDR pc_, struct dwarf2_cie *cie)
190   : pc (pc_), data_align (cie->data_alignment_factor),
191     code_align (cie->code_alignment_factor),
192     retaddr_column (cie->return_address_register)
193 {
194 }
195
196 /* Execute the required actions for both the DW_CFA_restore and
197 DW_CFA_restore_extended instructions.  */
198 static void
199 dwarf2_restore_rule (struct gdbarch *gdbarch, ULONGEST reg_num,
200                      struct dwarf2_frame_state *fs, int eh_frame_p)
201 {
202   ULONGEST reg;
203
204   reg = dwarf2_frame_adjust_regnum (gdbarch, reg_num, eh_frame_p);
205   fs->regs.alloc_regs (reg + 1);
206
207   /* Check if this register was explicitly initialized in the
208   CIE initial instructions.  If not, default the rule to
209   UNSPECIFIED.  */
210   if (reg < fs->initial.reg.size ())
211     fs->regs.reg[reg] = fs->initial.reg[reg];
212   else
213     fs->regs.reg[reg].how = DWARF2_FRAME_REG_UNSPECIFIED;
214
215   if (fs->regs.reg[reg].how == DWARF2_FRAME_REG_UNSPECIFIED)
216     {
217       int regnum = dwarf_reg_to_regnum (gdbarch, reg);
218
219       complaint (_("\
220 incomplete CFI data; DW_CFA_restore unspecified\n\
221 register %s (#%d) at %s"),
222                  gdbarch_register_name (gdbarch, regnum), regnum,
223                  paddress (gdbarch, fs->pc));
224     }
225 }
226
227 static CORE_ADDR
228 execute_stack_op (const gdb_byte *exp, ULONGEST len, int addr_size,
229                   frame_info_ptr this_frame, CORE_ADDR initial,
230                   int initial_in_stack_memory, dwarf2_per_objfile *per_objfile)
231 {
232   dwarf_expr_context ctx (per_objfile, addr_size);
233   scoped_value_mark free_values;
234
235   ctx.push_address (initial, initial_in_stack_memory);
236   value *result_val = ctx.evaluate (exp, len, true, nullptr, this_frame);
237
238   if (VALUE_LVAL (result_val) == lval_memory)
239     return value_address (result_val);
240   else
241     return value_as_address (result_val);
242 }
243 \f
244
245 /* Execute FDE program from INSN_PTR possibly up to INSN_END or up to inferior
246    PC.  Modify FS state accordingly.  Return current INSN_PTR where the
247    execution has stopped, one can resume it on the next call.  */
248
249 static const gdb_byte *
250 execute_cfa_program (struct dwarf2_fde *fde, const gdb_byte *insn_ptr,
251                      const gdb_byte *insn_end, struct gdbarch *gdbarch,
252                      CORE_ADDR pc, struct dwarf2_frame_state *fs,
253                      CORE_ADDR text_offset)
254 {
255   int eh_frame_p = fde->eh_frame_p;
256   unsigned int bytes_read;
257   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
258
259   while (insn_ptr < insn_end && fs->pc <= pc)
260     {
261       gdb_byte insn = *insn_ptr++;
262       uint64_t utmp, reg;
263       int64_t offset;
264
265       if ((insn & 0xc0) == DW_CFA_advance_loc)
266         fs->pc += (insn & 0x3f) * fs->code_align;
267       else if ((insn & 0xc0) == DW_CFA_offset)
268         {
269           reg = insn & 0x3f;
270           reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
271           insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
272           offset = utmp * fs->data_align;
273           fs->regs.alloc_regs (reg + 1);
274           fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
275           fs->regs.reg[reg].loc.offset = offset;
276         }
277       else if ((insn & 0xc0) == DW_CFA_restore)
278         {
279           reg = insn & 0x3f;
280           dwarf2_restore_rule (gdbarch, reg, fs, eh_frame_p);
281         }
282       else
283         {
284           switch (insn)
285             {
286             case DW_CFA_set_loc:
287               fs->pc = read_encoded_value (fde->cie->unit, fde->cie->encoding,
288                                            fde->cie->ptr_size, insn_ptr,
289                                            &bytes_read, fde->initial_location);
290               /* Apply the text offset for relocatable objects.  */
291               fs->pc += text_offset;
292               insn_ptr += bytes_read;
293               break;
294
295             case DW_CFA_advance_loc1:
296               utmp = extract_unsigned_integer (insn_ptr, 1, byte_order);
297               fs->pc += utmp * fs->code_align;
298               insn_ptr++;
299               break;
300             case DW_CFA_advance_loc2:
301               utmp = extract_unsigned_integer (insn_ptr, 2, byte_order);
302               fs->pc += utmp * fs->code_align;
303               insn_ptr += 2;
304               break;
305             case DW_CFA_advance_loc4:
306               utmp = extract_unsigned_integer (insn_ptr, 4, byte_order);
307               fs->pc += utmp * fs->code_align;
308               insn_ptr += 4;
309               break;
310
311             case DW_CFA_offset_extended:
312               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
313               reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
314               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
315               offset = utmp * fs->data_align;
316               fs->regs.alloc_regs (reg + 1);
317               fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
318               fs->regs.reg[reg].loc.offset = offset;
319               break;
320
321             case DW_CFA_restore_extended:
322               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
323               dwarf2_restore_rule (gdbarch, reg, fs, eh_frame_p);
324               break;
325
326             case DW_CFA_undefined:
327               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
328               reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
329               fs->regs.alloc_regs (reg + 1);
330               fs->regs.reg[reg].how = DWARF2_FRAME_REG_UNDEFINED;
331               break;
332
333             case DW_CFA_same_value:
334               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
335               reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
336               fs->regs.alloc_regs (reg + 1);
337               fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAME_VALUE;
338               break;
339
340             case DW_CFA_register:
341               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
342               reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
343               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
344               utmp = dwarf2_frame_adjust_regnum (gdbarch, utmp, eh_frame_p);
345               fs->regs.alloc_regs (reg + 1);
346               fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_REG;
347               fs->regs.reg[reg].loc.reg = utmp;
348               break;
349
350             case DW_CFA_remember_state:
351               {
352                 struct dwarf2_frame_state_reg_info *new_rs;
353
354                 new_rs = new dwarf2_frame_state_reg_info (fs->regs);
355                 fs->regs.prev = new_rs;
356               }
357               break;
358
359             case DW_CFA_restore_state:
360               {
361                 struct dwarf2_frame_state_reg_info *old_rs = fs->regs.prev;
362
363                 if (old_rs == NULL)
364                   {
365                     complaint (_("\
366 bad CFI data; mismatched DW_CFA_restore_state at %s"),
367                                paddress (gdbarch, fs->pc));
368                   }
369                 else
370                   fs->regs = std::move (*old_rs);
371               }
372               break;
373
374             case DW_CFA_def_cfa:
375               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
376               fs->regs.cfa_reg = reg;
377               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
378
379               if (fs->armcc_cfa_offsets_sf)
380                 utmp *= fs->data_align;
381
382               fs->regs.cfa_offset = utmp;
383               fs->regs.cfa_how = CFA_REG_OFFSET;
384               break;
385
386             case DW_CFA_def_cfa_register:
387               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
388               fs->regs.cfa_reg = dwarf2_frame_adjust_regnum (gdbarch, reg,
389                                                              eh_frame_p);
390               fs->regs.cfa_how = CFA_REG_OFFSET;
391               break;
392
393             case DW_CFA_def_cfa_offset:
394               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
395
396               if (fs->armcc_cfa_offsets_sf)
397                 utmp *= fs->data_align;
398
399               fs->regs.cfa_offset = utmp;
400               /* cfa_how deliberately not set.  */
401               break;
402
403             case DW_CFA_nop:
404               break;
405
406             case DW_CFA_def_cfa_expression:
407               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
408               fs->regs.cfa_exp_len = utmp;
409               fs->regs.cfa_exp = insn_ptr;
410               fs->regs.cfa_how = CFA_EXP;
411               insn_ptr += fs->regs.cfa_exp_len;
412               break;
413
414             case DW_CFA_expression:
415               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
416               reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
417               fs->regs.alloc_regs (reg + 1);
418               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
419               fs->regs.reg[reg].loc.exp.start = insn_ptr;
420               fs->regs.reg[reg].loc.exp.len = utmp;
421               fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_EXP;
422               insn_ptr += utmp;
423               break;
424
425             case DW_CFA_offset_extended_sf:
426               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
427               reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
428               insn_ptr = safe_read_sleb128 (insn_ptr, insn_end, &offset);
429               offset *= fs->data_align;
430               fs->regs.alloc_regs (reg + 1);
431               fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
432               fs->regs.reg[reg].loc.offset = offset;
433               break;
434
435             case DW_CFA_val_offset:
436               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
437               fs->regs.alloc_regs (reg + 1);
438               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
439               offset = utmp * fs->data_align;
440               fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_OFFSET;
441               fs->regs.reg[reg].loc.offset = offset;
442               break;
443
444             case DW_CFA_val_offset_sf:
445               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
446               fs->regs.alloc_regs (reg + 1);
447               insn_ptr = safe_read_sleb128 (insn_ptr, insn_end, &offset);
448               offset *= fs->data_align;
449               fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_OFFSET;
450               fs->regs.reg[reg].loc.offset = offset;
451               break;
452
453             case DW_CFA_val_expression:
454               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
455               fs->regs.alloc_regs (reg + 1);
456               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
457               fs->regs.reg[reg].loc.exp.start = insn_ptr;
458               fs->regs.reg[reg].loc.exp.len = utmp;
459               fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_EXP;
460               insn_ptr += utmp;
461               break;
462
463             case DW_CFA_def_cfa_sf:
464               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
465               fs->regs.cfa_reg = dwarf2_frame_adjust_regnum (gdbarch, reg,
466                                                              eh_frame_p);
467               insn_ptr = safe_read_sleb128 (insn_ptr, insn_end, &offset);
468               fs->regs.cfa_offset = offset * fs->data_align;
469               fs->regs.cfa_how = CFA_REG_OFFSET;
470               break;
471
472             case DW_CFA_def_cfa_offset_sf:
473               insn_ptr = safe_read_sleb128 (insn_ptr, insn_end, &offset);
474               fs->regs.cfa_offset = offset * fs->data_align;
475               /* cfa_how deliberately not set.  */
476               break;
477
478             case DW_CFA_GNU_args_size:
479               /* Ignored.  */
480               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
481               break;
482
483             case DW_CFA_GNU_negative_offset_extended:
484               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &reg);
485               reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
486               insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
487               offset = utmp * fs->data_align;
488               fs->regs.alloc_regs (reg + 1);
489               fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
490               fs->regs.reg[reg].loc.offset = -offset;
491               break;
492
493             default:
494               if (insn >= DW_CFA_lo_user && insn <= DW_CFA_hi_user)
495                 {
496                   /* Handle vendor-specific CFI for different architectures.  */
497                   if (!gdbarch_execute_dwarf_cfa_vendor_op (gdbarch, insn, fs))
498                     error (_("Call Frame Instruction op %d in vendor extension "
499                              "space is not handled on this architecture."),
500                            insn);
501                 }
502               else
503                 internal_error (_("Unknown CFI encountered."));
504             }
505         }
506     }
507
508   if (fs->initial.reg.empty ())
509     {
510       /* Don't allow remember/restore between CIE and FDE programs.  */
511       delete fs->regs.prev;
512       fs->regs.prev = NULL;
513     }
514
515   return insn_ptr;
516 }
517
518 #if GDB_SELF_TEST
519
520 namespace selftests {
521
522 /* Unit test to function execute_cfa_program.  */
523
524 static void
525 execute_cfa_program_test (struct gdbarch *gdbarch)
526 {
527   struct dwarf2_fde fde;
528   struct dwarf2_cie cie;
529
530   memset (&fde, 0, sizeof fde);
531   memset (&cie, 0, sizeof cie);
532
533   cie.data_alignment_factor = -4;
534   cie.code_alignment_factor = 2;
535   fde.cie = &cie;
536
537   dwarf2_frame_state fs (0, fde.cie);
538
539   gdb_byte insns[] =
540     {
541       DW_CFA_def_cfa, 1, 4,  /* DW_CFA_def_cfa: r1 ofs 4 */
542       DW_CFA_offset | 0x2, 1,  /* DW_CFA_offset: r2 at cfa-4 */
543       DW_CFA_remember_state,
544       DW_CFA_restore_state,
545     };
546
547   const gdb_byte *insn_end = insns + sizeof (insns);
548   const gdb_byte *out = execute_cfa_program (&fde, insns, insn_end, gdbarch,
549                                              0, &fs, 0);
550
551   SELF_CHECK (out == insn_end);
552   SELF_CHECK (fs.pc == 0);
553
554   /* The instructions above only use r1 and r2, but the register numbers
555      used are adjusted by dwarf2_frame_adjust_regnum.  */
556   auto r1 = dwarf2_frame_adjust_regnum (gdbarch, 1, fde.eh_frame_p);
557   auto r2 = dwarf2_frame_adjust_regnum (gdbarch, 2, fde.eh_frame_p);
558
559   SELF_CHECK (fs.regs.reg.size () == (std::max (r1, r2) + 1));
560
561   SELF_CHECK (fs.regs.reg[r2].how == DWARF2_FRAME_REG_SAVED_OFFSET);
562   SELF_CHECK (fs.regs.reg[r2].loc.offset == -4);
563
564   for (auto i = 0; i < fs.regs.reg.size (); i++)
565     if (i != r2)
566       SELF_CHECK (fs.regs.reg[i].how == DWARF2_FRAME_REG_UNSPECIFIED);
567
568   SELF_CHECK (fs.regs.cfa_reg == 1);
569   SELF_CHECK (fs.regs.cfa_offset == 4);
570   SELF_CHECK (fs.regs.cfa_how == CFA_REG_OFFSET);
571   SELF_CHECK (fs.regs.cfa_exp == NULL);
572   SELF_CHECK (fs.regs.prev == NULL);
573 }
574
575 } // namespace selftests
576 #endif /* GDB_SELF_TEST */
577
578 \f
579
580 /* Architecture-specific operations.  */
581
582 static void dwarf2_frame_default_init_reg (struct gdbarch *gdbarch,
583                                            int regnum,
584                                            struct dwarf2_frame_state_reg *reg,
585                                            frame_info_ptr this_frame);
586
587 struct dwarf2_frame_ops
588 {
589   /* Pre-initialize the register state REG for register REGNUM.  */
590   void (*init_reg) (struct gdbarch *, int, struct dwarf2_frame_state_reg *,
591                     frame_info_ptr)
592     = dwarf2_frame_default_init_reg;
593
594   /* Check whether the THIS_FRAME is a signal trampoline.  */
595   int (*signal_frame_p) (struct gdbarch *, frame_info_ptr) = nullptr;
596
597   /* Convert .eh_frame register number to DWARF register number, or
598      adjust .debug_frame register number.  */
599   int (*adjust_regnum) (struct gdbarch *, int, int) = nullptr;
600 };
601
602 /* Per-architecture data key.  */
603 static const registry<gdbarch>::key<dwarf2_frame_ops> dwarf2_frame_data;
604
605 /* Get or initialize the frame ops.  */
606 static dwarf2_frame_ops *
607 get_frame_ops (struct gdbarch *gdbarch)
608 {
609   dwarf2_frame_ops *result = dwarf2_frame_data.get (gdbarch);
610   if (result == nullptr)
611     result = dwarf2_frame_data.emplace (gdbarch);
612   return result;
613 }
614
615 /* Default architecture-specific register state initialization
616    function.  */
617
618 static void
619 dwarf2_frame_default_init_reg (struct gdbarch *gdbarch, int regnum,
620                                struct dwarf2_frame_state_reg *reg,
621                                frame_info_ptr this_frame)
622 {
623   /* If we have a register that acts as a program counter, mark it as
624      a destination for the return address.  If we have a register that
625      serves as the stack pointer, arrange for it to be filled with the
626      call frame address (CFA).  The other registers are marked as
627      unspecified.
628
629      We copy the return address to the program counter, since many
630      parts in GDB assume that it is possible to get the return address
631      by unwinding the program counter register.  However, on ISA's
632      with a dedicated return address register, the CFI usually only
633      contains information to unwind that return address register.
634
635      The reason we're treating the stack pointer special here is
636      because in many cases GCC doesn't emit CFI for the stack pointer
637      and implicitly assumes that it is equal to the CFA.  This makes
638      some sense since the DWARF specification (version 3, draft 8,
639      p. 102) says that:
640
641      "Typically, the CFA is defined to be the value of the stack
642      pointer at the call site in the previous frame (which may be
643      different from its value on entry to the current frame)."
644
645      However, this isn't true for all platforms supported by GCC
646      (e.g. IBM S/390 and zSeries).  Those architectures should provide
647      their own architecture-specific initialization function.  */
648
649   if (regnum == gdbarch_pc_regnum (gdbarch))
650     reg->how = DWARF2_FRAME_REG_RA;
651   else if (regnum == gdbarch_sp_regnum (gdbarch))
652     reg->how = DWARF2_FRAME_REG_CFA;
653 }
654
655 /* Set the architecture-specific register state initialization
656    function for GDBARCH to INIT_REG.  */
657
658 void
659 dwarf2_frame_set_init_reg (struct gdbarch *gdbarch,
660                            void (*init_reg) (struct gdbarch *, int,
661                                              struct dwarf2_frame_state_reg *,
662                                              frame_info_ptr))
663 {
664   struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch);
665
666   ops->init_reg = init_reg;
667 }
668
669 /* Pre-initialize the register state REG for register REGNUM.  */
670
671 static void
672 dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
673                        struct dwarf2_frame_state_reg *reg,
674                        frame_info_ptr this_frame)
675 {
676   struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch);
677
678   ops->init_reg (gdbarch, regnum, reg, this_frame);
679 }
680
681 /* Set the architecture-specific signal trampoline recognition
682    function for GDBARCH to SIGNAL_FRAME_P.  */
683
684 void
685 dwarf2_frame_set_signal_frame_p (struct gdbarch *gdbarch,
686                                  int (*signal_frame_p) (struct gdbarch *,
687                                                         frame_info_ptr))
688 {
689   struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch);
690
691   ops->signal_frame_p = signal_frame_p;
692 }
693
694 /* Query the architecture-specific signal frame recognizer for
695    THIS_FRAME.  */
696
697 static int
698 dwarf2_frame_signal_frame_p (struct gdbarch *gdbarch,
699                              frame_info_ptr this_frame)
700 {
701   struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch);
702
703   if (ops->signal_frame_p == NULL)
704     return 0;
705   return ops->signal_frame_p (gdbarch, this_frame);
706 }
707
708 /* Set the architecture-specific adjustment of .eh_frame and .debug_frame
709    register numbers.  */
710
711 void
712 dwarf2_frame_set_adjust_regnum (struct gdbarch *gdbarch,
713                                 int (*adjust_regnum) (struct gdbarch *,
714                                                       int, int))
715 {
716   struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch);
717
718   ops->adjust_regnum = adjust_regnum;
719 }
720
721 /* Translate a .eh_frame register to DWARF register, or adjust a .debug_frame
722    register.  */
723
724 static int
725 dwarf2_frame_adjust_regnum (struct gdbarch *gdbarch,
726                             int regnum, int eh_frame_p)
727 {
728   struct dwarf2_frame_ops *ops = get_frame_ops (gdbarch);
729
730   if (ops->adjust_regnum == NULL)
731     return regnum;
732   return ops->adjust_regnum (gdbarch, regnum, eh_frame_p);
733 }
734
735 static void
736 dwarf2_frame_find_quirks (struct dwarf2_frame_state *fs,
737                           struct dwarf2_fde *fde)
738 {
739   struct compunit_symtab *cust;
740
741   cust = find_pc_compunit_symtab (fs->pc);
742   if (cust == NULL)
743     return;
744
745   if (producer_is_realview (cust->producer ()))
746     {
747       if (fde->cie->version == 1)
748         fs->armcc_cfa_offsets_sf = 1;
749
750       if (fde->cie->version == 1)
751         fs->armcc_cfa_offsets_reversed = 1;
752
753       /* The reversed offset problem is present in some compilers
754          using DWARF3, but it was eventually fixed.  Check the ARM
755          defined augmentations, which are in the format "armcc" followed
756          by a list of one-character options.  The "+" option means
757          this problem is fixed (no quirk needed).  If the armcc
758          augmentation is missing, the quirk is needed.  */
759       if (fde->cie->version == 3
760           && (!startswith (fde->cie->augmentation, "armcc")
761               || strchr (fde->cie->augmentation + 5, '+') == NULL))
762         fs->armcc_cfa_offsets_reversed = 1;
763
764       return;
765     }
766 }
767 \f
768
769 /* See dwarf2/frame.h.  */
770
771 int
772 dwarf2_fetch_cfa_info (struct gdbarch *gdbarch, CORE_ADDR pc,
773                        struct dwarf2_per_cu_data *data,
774                        int *regnum_out, LONGEST *offset_out,
775                        CORE_ADDR *text_offset_out,
776                        const gdb_byte **cfa_start_out,
777                        const gdb_byte **cfa_end_out)
778 {
779   struct dwarf2_fde *fde;
780   dwarf2_per_objfile *per_objfile;
781   CORE_ADDR pc1 = pc;
782
783   /* Find the correct FDE.  */
784   fde = dwarf2_frame_find_fde (&pc1, &per_objfile);
785   if (fde == NULL)
786     error (_("Could not compute CFA; needed to translate this expression"));
787
788   gdb_assert (per_objfile != nullptr);
789
790   dwarf2_frame_state fs (pc1, fde->cie);
791
792   /* Check for "quirks" - known bugs in producers.  */
793   dwarf2_frame_find_quirks (&fs, fde);
794
795   /* First decode all the insns in the CIE.  */
796   execute_cfa_program (fde, fde->cie->initial_instructions,
797                        fde->cie->end, gdbarch, pc, &fs,
798                        per_objfile->objfile->text_section_offset ());
799
800   /* Save the initialized register set.  */
801   fs.initial = fs.regs;
802
803   /* Then decode the insns in the FDE up to our target PC.  */
804   execute_cfa_program (fde, fde->instructions, fde->end, gdbarch, pc, &fs,
805                        per_objfile->objfile->text_section_offset ());
806
807   /* Calculate the CFA.  */
808   switch (fs.regs.cfa_how)
809     {
810     case CFA_REG_OFFSET:
811       {
812         int regnum = dwarf_reg_to_regnum_or_error (gdbarch, fs.regs.cfa_reg);
813
814         *regnum_out = regnum;
815         if (fs.armcc_cfa_offsets_reversed)
816           *offset_out = -fs.regs.cfa_offset;
817         else
818           *offset_out = fs.regs.cfa_offset;
819         return 1;
820       }
821
822     case CFA_EXP:
823       *text_offset_out = per_objfile->objfile->text_section_offset ();
824       *cfa_start_out = fs.regs.cfa_exp;
825       *cfa_end_out = fs.regs.cfa_exp + fs.regs.cfa_exp_len;
826       return 0;
827
828     default:
829       internal_error (_("Unknown CFA rule."));
830     }
831 }
832
833 \f
834 struct dwarf2_frame_cache
835 {
836   /* DWARF Call Frame Address.  */
837   CORE_ADDR cfa;
838
839   /* Set if the return address column was marked as unavailable
840      (required non-collected memory or registers to compute).  */
841   int unavailable_retaddr;
842
843   /* Set if the return address column was marked as undefined.  */
844   int undefined_retaddr;
845
846   /* Saved registers, indexed by GDB register number, not by DWARF
847      register number.  */
848   struct dwarf2_frame_state_reg *reg;
849
850   /* Return address register.  */
851   struct dwarf2_frame_state_reg retaddr_reg;
852
853   /* Target address size in bytes.  */
854   int addr_size;
855
856   /* The dwarf2_per_objfile from which this frame description came.  */
857   dwarf2_per_objfile *per_objfile;
858
859   /* If not NULL then this frame is the bottom frame of a TAILCALL_FRAME
860      sequence.  If NULL then it is a normal case with no TAILCALL_FRAME
861      involved.  Non-bottom frames of a virtual tail call frames chain use
862      dwarf2_tailcall_frame_unwind unwinder so this field does not apply for
863      them.  */
864   void *tailcall_cache;
865 };
866
867 static struct dwarf2_frame_cache *
868 dwarf2_frame_cache (frame_info_ptr this_frame, void **this_cache)
869 {
870   struct gdbarch *gdbarch = get_frame_arch (this_frame);
871   const int num_regs = gdbarch_num_cooked_regs (gdbarch);
872   struct dwarf2_frame_cache *cache;
873   struct dwarf2_fde *fde;
874   CORE_ADDR entry_pc;
875   const gdb_byte *instr;
876
877   if (*this_cache)
878     return (struct dwarf2_frame_cache *) *this_cache;
879
880   /* Allocate a new cache.  */
881   cache = FRAME_OBSTACK_ZALLOC (struct dwarf2_frame_cache);
882   cache->reg = FRAME_OBSTACK_CALLOC (num_regs, struct dwarf2_frame_state_reg);
883   *this_cache = cache;
884
885   /* Unwind the PC.
886
887      Note that if the next frame is never supposed to return (i.e. a call
888      to abort), the compiler might optimize away the instruction at
889      its return address.  As a result the return address will
890      point at some random instruction, and the CFI for that
891      instruction is probably worthless to us.  GCC's unwinder solves
892      this problem by substracting 1 from the return address to get an
893      address in the middle of a presumed call instruction (or the
894      instruction in the associated delay slot).  This should only be
895      done for "normal" frames and not for resume-type frames (signal
896      handlers, sentinel frames, dummy frames).  The function
897      get_frame_address_in_block does just this.  It's not clear how
898      reliable the method is though; there is the potential for the
899      register state pre-call being different to that on return.  */
900   CORE_ADDR pc1 = get_frame_address_in_block (this_frame);
901
902   /* Find the correct FDE.  */
903   fde = dwarf2_frame_find_fde (&pc1, &cache->per_objfile);
904   gdb_assert (fde != NULL);
905   gdb_assert (cache->per_objfile != nullptr);
906
907   /* Allocate and initialize the frame state.  */
908   struct dwarf2_frame_state fs (pc1, fde->cie);
909
910   cache->addr_size = fde->cie->addr_size;
911
912   /* Check for "quirks" - known bugs in producers.  */
913   dwarf2_frame_find_quirks (&fs, fde);
914
915   /* First decode all the insns in the CIE.  */
916   execute_cfa_program (fde, fde->cie->initial_instructions,
917                        fde->cie->end, gdbarch,
918                        get_frame_address_in_block (this_frame), &fs,
919                        cache->per_objfile->objfile->text_section_offset ());
920
921   /* Save the initialized register set.  */
922   fs.initial = fs.regs;
923
924   /* Fetching the entry pc for THIS_FRAME won't necessarily result
925      in an address that's within the range of FDE locations.  This
926      is due to the possibility of the function occupying non-contiguous
927      ranges.  */
928   LONGEST entry_cfa_sp_offset;
929   int entry_cfa_sp_offset_p = 0;
930   if (get_frame_func_if_available (this_frame, &entry_pc)
931       && fde->initial_location <= entry_pc
932       && entry_pc < fde->initial_location + fde->address_range)
933     {
934       /* Decode the insns in the FDE up to the entry PC.  */
935       instr = execute_cfa_program
936         (fde, fde->instructions, fde->end, gdbarch, entry_pc, &fs,
937          cache->per_objfile->objfile->text_section_offset ());
938
939       if (fs.regs.cfa_how == CFA_REG_OFFSET
940           && (dwarf_reg_to_regnum (gdbarch, fs.regs.cfa_reg)
941               == gdbarch_sp_regnum (gdbarch)))
942         {
943           entry_cfa_sp_offset = fs.regs.cfa_offset;
944           entry_cfa_sp_offset_p = 1;
945         }
946     }
947   else
948     instr = fde->instructions;
949
950   /* Then decode the insns in the FDE up to our target PC.  */
951   execute_cfa_program (fde, instr, fde->end, gdbarch,
952                        get_frame_address_in_block (this_frame), &fs,
953                        cache->per_objfile->objfile->text_section_offset ());
954
955   try
956     {
957       /* Calculate the CFA.  */
958       switch (fs.regs.cfa_how)
959         {
960         case CFA_REG_OFFSET:
961           cache->cfa = read_addr_from_reg (this_frame, fs.regs.cfa_reg);
962           if (fs.armcc_cfa_offsets_reversed)
963             cache->cfa -= fs.regs.cfa_offset;
964           else
965             cache->cfa += fs.regs.cfa_offset;
966           break;
967
968         case CFA_EXP:
969           cache->cfa =
970             execute_stack_op (fs.regs.cfa_exp, fs.regs.cfa_exp_len,
971                               cache->addr_size, this_frame, 0, 0,
972                               cache->per_objfile);
973           break;
974
975         default:
976           internal_error (_("Unknown CFA rule."));
977         }
978     }
979   catch (const gdb_exception_error &ex)
980     {
981       if (ex.error == NOT_AVAILABLE_ERROR)
982         {
983           cache->unavailable_retaddr = 1;
984           return cache;
985         }
986
987       throw;
988     }
989
990   /* Initialize the register state.  */
991   {
992     int regnum;
993
994     for (regnum = 0; regnum < num_regs; regnum++)
995       dwarf2_frame_init_reg (gdbarch, regnum, &cache->reg[regnum], this_frame);
996   }
997
998   /* Go through the DWARF2 CFI generated table and save its register
999      location information in the cache.  Note that we don't skip the
1000      return address column; it's perfectly all right for it to
1001      correspond to a real register.  */
1002   {
1003     int column;         /* CFI speak for "register number".  */
1004
1005     for (column = 0; column < fs.regs.reg.size (); column++)
1006       {
1007         /* Use the GDB register number as the destination index.  */
1008         int regnum = dwarf_reg_to_regnum (gdbarch, column);
1009
1010         /* Protect against a target returning a bad register.  */
1011         if (regnum < 0 || regnum >= num_regs)
1012           continue;
1013
1014         /* NOTE: cagney/2003-09-05: CFI should specify the disposition
1015            of all debug info registers.  If it doesn't, complain (but
1016            not too loudly).  It turns out that GCC assumes that an
1017            unspecified register implies "same value" when CFI (draft
1018            7) specifies nothing at all.  Such a register could equally
1019            be interpreted as "undefined".  Also note that this check
1020            isn't sufficient; it only checks that all registers in the
1021            range [0 .. max column] are specified, and won't detect
1022            problems when a debug info register falls outside of the
1023            table.  We need a way of iterating through all the valid
1024            DWARF2 register numbers.  */
1025         if (fs.regs.reg[column].how == DWARF2_FRAME_REG_UNSPECIFIED)
1026           {
1027             if (cache->reg[regnum].how == DWARF2_FRAME_REG_UNSPECIFIED)
1028               complaint (_("\
1029 incomplete CFI data; unspecified registers (e.g., %s) at %s"),
1030                          gdbarch_register_name (gdbarch, regnum),
1031                          paddress (gdbarch, fs.pc));
1032           }
1033         else
1034           cache->reg[regnum] = fs.regs.reg[column];
1035       }
1036   }
1037
1038   /* Eliminate any DWARF2_FRAME_REG_RA rules, and save the information
1039      we need for evaluating DWARF2_FRAME_REG_RA_OFFSET rules.  */
1040   {
1041     int regnum;
1042
1043     for (regnum = 0; regnum < num_regs; regnum++)
1044       {
1045         if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA
1046             || cache->reg[regnum].how == DWARF2_FRAME_REG_RA_OFFSET)
1047           {
1048             const std::vector<struct dwarf2_frame_state_reg> &regs
1049               = fs.regs.reg;
1050             ULONGEST retaddr_column = fs.retaddr_column;
1051
1052             /* It seems rather bizarre to specify an "empty" column as
1053                the return adress column.  However, this is exactly
1054                what GCC does on some targets.  It turns out that GCC
1055                assumes that the return address can be found in the
1056                register corresponding to the return address column.
1057                Incidentally, that's how we should treat a return
1058                address column specifying "same value" too.  */
1059             if (fs.retaddr_column < fs.regs.reg.size ()
1060                 && regs[retaddr_column].how != DWARF2_FRAME_REG_UNSPECIFIED
1061                 && regs[retaddr_column].how != DWARF2_FRAME_REG_SAME_VALUE)
1062               {
1063                 if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA)
1064                   cache->reg[regnum] = regs[retaddr_column];
1065                 else
1066                   cache->retaddr_reg = regs[retaddr_column];
1067               }
1068             else
1069               {
1070                 if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA)
1071                   {
1072                     cache->reg[regnum].loc.reg = fs.retaddr_column;
1073                     cache->reg[regnum].how = DWARF2_FRAME_REG_SAVED_REG;
1074                   }
1075                 else
1076                   {
1077                     cache->retaddr_reg.loc.reg = fs.retaddr_column;
1078                     cache->retaddr_reg.how = DWARF2_FRAME_REG_SAVED_REG;
1079                   }
1080               }
1081           }
1082       }
1083   }
1084
1085   if (fs.retaddr_column < fs.regs.reg.size ()
1086       && fs.regs.reg[fs.retaddr_column].how == DWARF2_FRAME_REG_UNDEFINED)
1087     cache->undefined_retaddr = 1;
1088
1089   dwarf2_tailcall_sniffer_first (this_frame, &cache->tailcall_cache,
1090                                  (entry_cfa_sp_offset_p
1091                                   ? &entry_cfa_sp_offset : NULL));
1092
1093   return cache;
1094 }
1095
1096 static enum unwind_stop_reason
1097 dwarf2_frame_unwind_stop_reason (frame_info_ptr this_frame,
1098                                  void **this_cache)
1099 {
1100   struct dwarf2_frame_cache *cache
1101     = dwarf2_frame_cache (this_frame, this_cache);
1102
1103   if (cache->unavailable_retaddr)
1104     return UNWIND_UNAVAILABLE;
1105
1106   if (cache->undefined_retaddr)
1107     return UNWIND_OUTERMOST;
1108
1109   return UNWIND_NO_REASON;
1110 }
1111
1112 static void
1113 dwarf2_frame_this_id (frame_info_ptr this_frame, void **this_cache,
1114                       struct frame_id *this_id)
1115 {
1116   struct dwarf2_frame_cache *cache =
1117     dwarf2_frame_cache (this_frame, this_cache);
1118
1119   if (cache->unavailable_retaddr)
1120     (*this_id) = frame_id_build_unavailable_stack (get_frame_func (this_frame));
1121   else if (cache->undefined_retaddr)
1122     return;
1123   else
1124     (*this_id) = frame_id_build (cache->cfa, get_frame_func (this_frame));
1125 }
1126
1127 static struct value *
1128 dwarf2_frame_prev_register (frame_info_ptr this_frame, void **this_cache,
1129                             int regnum)
1130 {
1131   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1132   struct dwarf2_frame_cache *cache =
1133     dwarf2_frame_cache (this_frame, this_cache);
1134   CORE_ADDR addr;
1135   int realnum;
1136
1137   /* Non-bottom frames of a virtual tail call frames chain use
1138      dwarf2_tailcall_frame_unwind unwinder so this code does not apply for
1139      them.  If dwarf2_tailcall_prev_register_first does not have specific value
1140      unwind the register, tail call frames are assumed to have the register set
1141      of the top caller.  */
1142   if (cache->tailcall_cache)
1143     {
1144       struct value *val;
1145       
1146       val = dwarf2_tailcall_prev_register_first (this_frame,
1147                                                  &cache->tailcall_cache,
1148                                                  regnum);
1149       if (val)
1150         return val;
1151     }
1152
1153   switch (cache->reg[regnum].how)
1154     {
1155     case DWARF2_FRAME_REG_UNDEFINED:
1156       /* If CFI explicitly specified that the value isn't defined,
1157          mark it as optimized away; the value isn't available.  */
1158       return frame_unwind_got_optimized (this_frame, regnum);
1159
1160     case DWARF2_FRAME_REG_SAVED_OFFSET:
1161       addr = cache->cfa + cache->reg[regnum].loc.offset;
1162       return frame_unwind_got_memory (this_frame, regnum, addr);
1163
1164     case DWARF2_FRAME_REG_SAVED_REG:
1165       realnum = dwarf_reg_to_regnum_or_error
1166         (gdbarch, cache->reg[regnum].loc.reg);
1167       return frame_unwind_got_register (this_frame, regnum, realnum);
1168
1169     case DWARF2_FRAME_REG_SAVED_EXP:
1170       addr = execute_stack_op (cache->reg[regnum].loc.exp.start,
1171                                cache->reg[regnum].loc.exp.len,
1172                                cache->addr_size,
1173                                this_frame, cache->cfa, 1,
1174                                cache->per_objfile);
1175       return frame_unwind_got_memory (this_frame, regnum, addr);
1176
1177     case DWARF2_FRAME_REG_SAVED_VAL_OFFSET:
1178       addr = cache->cfa + cache->reg[regnum].loc.offset;
1179       return frame_unwind_got_constant (this_frame, regnum, addr);
1180
1181     case DWARF2_FRAME_REG_SAVED_VAL_EXP:
1182       addr = execute_stack_op (cache->reg[regnum].loc.exp.start,
1183                                cache->reg[regnum].loc.exp.len,
1184                                cache->addr_size,
1185                                this_frame, cache->cfa, 1,
1186                                cache->per_objfile);
1187       return frame_unwind_got_constant (this_frame, regnum, addr);
1188
1189     case DWARF2_FRAME_REG_UNSPECIFIED:
1190       /* GCC, in its infinite wisdom decided to not provide unwind
1191          information for registers that are "same value".  Since
1192          DWARF2 (3 draft 7) doesn't define such behavior, said
1193          registers are actually undefined (which is different to CFI
1194          "undefined").  Code above issues a complaint about this.
1195          Here just fudge the books, assume GCC, and that the value is
1196          more inner on the stack.  */
1197       return frame_unwind_got_register (this_frame, regnum, regnum);
1198
1199     case DWARF2_FRAME_REG_SAME_VALUE:
1200       return frame_unwind_got_register (this_frame, regnum, regnum);
1201
1202     case DWARF2_FRAME_REG_CFA:
1203       return frame_unwind_got_address (this_frame, regnum, cache->cfa);
1204
1205     case DWARF2_FRAME_REG_CFA_OFFSET:
1206       addr = cache->cfa + cache->reg[regnum].loc.offset;
1207       return frame_unwind_got_address (this_frame, regnum, addr);
1208
1209     case DWARF2_FRAME_REG_RA_OFFSET:
1210       addr = cache->reg[regnum].loc.offset;
1211       regnum = dwarf_reg_to_regnum_or_error
1212         (gdbarch, cache->retaddr_reg.loc.reg);
1213       addr += get_frame_register_unsigned (this_frame, regnum);
1214       return frame_unwind_got_address (this_frame, regnum, addr);
1215
1216     case DWARF2_FRAME_REG_FN:
1217       return cache->reg[regnum].loc.fn (this_frame, this_cache, regnum);
1218
1219     default:
1220       internal_error (_("Unknown register rule."));
1221     }
1222 }
1223
1224 /* Proxy for tailcall_frame_dealloc_cache for bottom frame of a virtual tail
1225    call frames chain.  */
1226
1227 static void
1228 dwarf2_frame_dealloc_cache (frame_info *self, void *this_cache)
1229 {
1230   struct dwarf2_frame_cache *cache
1231       = dwarf2_frame_cache (frame_info_ptr (self), &this_cache);
1232
1233   if (cache->tailcall_cache)
1234     dwarf2_tailcall_frame_unwind.dealloc_cache (self, cache->tailcall_cache);
1235 }
1236
1237 static int
1238 dwarf2_frame_sniffer (const struct frame_unwind *self,
1239                       frame_info_ptr this_frame, void **this_cache)
1240 {
1241   if (!dwarf2_frame_unwinders_enabled_p)
1242     return 0;
1243
1244   /* Grab an address that is guaranteed to reside somewhere within the
1245      function.  get_frame_pc(), with a no-return next function, can
1246      end up returning something past the end of this function's body.
1247      If the frame we're sniffing for is a signal frame whose start
1248      address is placed on the stack by the OS, its FDE must
1249      extend one byte before its start address or we could potentially
1250      select the FDE of the previous function.  */
1251   CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
1252   struct dwarf2_fde *fde = dwarf2_frame_find_fde (&block_addr, NULL);
1253
1254   if (!fde)
1255     return 0;
1256
1257   /* On some targets, signal trampolines may have unwind information.
1258      We need to recognize them so that we set the frame type
1259      correctly.  */
1260
1261   if (fde->cie->signal_frame
1262       || dwarf2_frame_signal_frame_p (get_frame_arch (this_frame),
1263                                       this_frame))
1264     return self->type == SIGTRAMP_FRAME;
1265
1266   if (self->type != NORMAL_FRAME)
1267     return 0;
1268
1269   return 1;
1270 }
1271
1272 static const struct frame_unwind dwarf2_frame_unwind =
1273 {
1274   "dwarf2",
1275   NORMAL_FRAME,
1276   dwarf2_frame_unwind_stop_reason,
1277   dwarf2_frame_this_id,
1278   dwarf2_frame_prev_register,
1279   NULL,
1280   dwarf2_frame_sniffer,
1281   dwarf2_frame_dealloc_cache
1282 };
1283
1284 static const struct frame_unwind dwarf2_signal_frame_unwind =
1285 {
1286   "dwarf2 signal",
1287   SIGTRAMP_FRAME,
1288   dwarf2_frame_unwind_stop_reason,
1289   dwarf2_frame_this_id,
1290   dwarf2_frame_prev_register,
1291   NULL,
1292   dwarf2_frame_sniffer,
1293
1294   /* TAILCALL_CACHE can never be in such frame to need dealloc_cache.  */
1295   NULL
1296 };
1297
1298 /* Append the DWARF-2 frame unwinders to GDBARCH's list.  */
1299
1300 void
1301 dwarf2_append_unwinders (struct gdbarch *gdbarch)
1302 {
1303   frame_unwind_append_unwinder (gdbarch, &dwarf2_frame_unwind);
1304   frame_unwind_append_unwinder (gdbarch, &dwarf2_signal_frame_unwind);
1305 }
1306 \f
1307
1308 /* There is no explicitly defined relationship between the CFA and the
1309    location of frame's local variables and arguments/parameters.
1310    Therefore, frame base methods on this page should probably only be
1311    used as a last resort, just to avoid printing total garbage as a
1312    response to the "info frame" command.  */
1313
1314 static CORE_ADDR
1315 dwarf2_frame_base_address (frame_info_ptr this_frame, void **this_cache)
1316 {
1317   struct dwarf2_frame_cache *cache =
1318     dwarf2_frame_cache (this_frame, this_cache);
1319
1320   return cache->cfa;
1321 }
1322
1323 static const struct frame_base dwarf2_frame_base =
1324 {
1325   &dwarf2_frame_unwind,
1326   dwarf2_frame_base_address,
1327   dwarf2_frame_base_address,
1328   dwarf2_frame_base_address
1329 };
1330
1331 const struct frame_base *
1332 dwarf2_frame_base_sniffer (frame_info_ptr this_frame)
1333 {
1334   CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
1335
1336   if (dwarf2_frame_find_fde (&block_addr, NULL))
1337     return &dwarf2_frame_base;
1338
1339   return NULL;
1340 }
1341
1342 /* Compute the CFA for THIS_FRAME, but only if THIS_FRAME came from
1343    the DWARF unwinder.  This is used to implement
1344    DW_OP_call_frame_cfa.  */
1345
1346 CORE_ADDR
1347 dwarf2_frame_cfa (frame_info_ptr this_frame)
1348 {
1349   if (frame_unwinder_is (this_frame, &record_btrace_tailcall_frame_unwind)
1350       || frame_unwinder_is (this_frame, &record_btrace_frame_unwind))
1351     throw_error (NOT_AVAILABLE_ERROR,
1352                  _("cfa not available for record btrace target"));
1353
1354   while (get_frame_type (this_frame) == INLINE_FRAME)
1355     this_frame = get_prev_frame (this_frame);
1356   if (get_frame_unwind_stop_reason (this_frame) == UNWIND_UNAVAILABLE)
1357     throw_error (NOT_AVAILABLE_ERROR,
1358                 _("can't compute CFA for this frame: "
1359                   "required registers or memory are unavailable"));
1360
1361   if (get_frame_id (this_frame).stack_status != FID_STACK_VALID)
1362     throw_error (NOT_AVAILABLE_ERROR,
1363                 _("can't compute CFA for this frame: "
1364                   "frame base not available"));
1365
1366   return get_frame_base (this_frame);
1367 }
1368 \f
1369 /* We store the frame data on the BFD.  This is only done if it is
1370    independent of the address space and so can be shared.  */
1371 static const registry<bfd>::key<comp_unit> dwarf2_frame_bfd_data;
1372
1373 /* If any BFD sections require relocations (note; really should be if
1374    any debug info requires relocations), then we store the frame data
1375    on the objfile instead, and do not share it.  */
1376 static const registry<objfile>::key<comp_unit> dwarf2_frame_objfile_data;
1377 \f
1378
1379 /* Pointer encoding helper functions.  */
1380
1381 /* GCC supports exception handling based on DWARF2 CFI.  However, for
1382    technical reasons, it encodes addresses in its FDE's in a different
1383    way.  Several "pointer encodings" are supported.  The encoding
1384    that's used for a particular FDE is determined by the 'R'
1385    augmentation in the associated CIE.  The argument of this
1386    augmentation is a single byte.  
1387
1388    The address can be encoded as 2 bytes, 4 bytes, 8 bytes, or as a
1389    LEB128.  This is encoded in bits 0, 1 and 2.  Bit 3 encodes whether
1390    the address is signed or unsigned.  Bits 4, 5 and 6 encode how the
1391    address should be interpreted (absolute, relative to the current
1392    position in the FDE, ...).  Bit 7, indicates that the address
1393    should be dereferenced.  */
1394
1395 static gdb_byte
1396 encoding_for_size (unsigned int size)
1397 {
1398   switch (size)
1399     {
1400     case 2:
1401       return DW_EH_PE_udata2;
1402     case 4:
1403       return DW_EH_PE_udata4;
1404     case 8:
1405       return DW_EH_PE_udata8;
1406     default:
1407       internal_error (_("Unsupported address size"));
1408     }
1409 }
1410
1411 static CORE_ADDR
1412 read_encoded_value (struct comp_unit *unit, gdb_byte encoding,
1413                     int ptr_len, const gdb_byte *buf,
1414                     unsigned int *bytes_read_ptr,
1415                     CORE_ADDR func_base)
1416 {
1417   ptrdiff_t offset;
1418   CORE_ADDR base;
1419
1420   /* GCC currently doesn't generate DW_EH_PE_indirect encodings for
1421      FDE's.  */
1422   if (encoding & DW_EH_PE_indirect)
1423     internal_error (_("Unsupported encoding: DW_EH_PE_indirect"));
1424
1425   *bytes_read_ptr = 0;
1426
1427   switch (encoding & 0x70)
1428     {
1429     case DW_EH_PE_absptr:
1430       base = 0;
1431       break;
1432     case DW_EH_PE_pcrel:
1433       base = bfd_section_vma (unit->dwarf_frame_section);
1434       base += (buf - unit->dwarf_frame_buffer);
1435       break;
1436     case DW_EH_PE_datarel:
1437       base = unit->dbase;
1438       break;
1439     case DW_EH_PE_textrel:
1440       base = unit->tbase;
1441       break;
1442     case DW_EH_PE_funcrel:
1443       base = func_base;
1444       break;
1445     case DW_EH_PE_aligned:
1446       base = 0;
1447       offset = buf - unit->dwarf_frame_buffer;
1448       if ((offset % ptr_len) != 0)
1449         {
1450           *bytes_read_ptr = ptr_len - (offset % ptr_len);
1451           buf += *bytes_read_ptr;
1452         }
1453       break;
1454     default:
1455       internal_error (_("Invalid or unsupported encoding"));
1456     }
1457
1458   if ((encoding & 0x07) == 0x00)
1459     {
1460       encoding |= encoding_for_size (ptr_len);
1461       if (bfd_get_sign_extend_vma (unit->abfd))
1462         encoding |= DW_EH_PE_signed;
1463     }
1464
1465   switch (encoding & 0x0f)
1466     {
1467     case DW_EH_PE_uleb128:
1468       {
1469         uint64_t value;
1470         const gdb_byte *end_buf = buf + (sizeof (value) + 1) * 8 / 7;
1471
1472         *bytes_read_ptr += safe_read_uleb128 (buf, end_buf, &value) - buf;
1473         return base + value;
1474       }
1475     case DW_EH_PE_udata2:
1476       *bytes_read_ptr += 2;
1477       return (base + bfd_get_16 (unit->abfd, (bfd_byte *) buf));
1478     case DW_EH_PE_udata4:
1479       *bytes_read_ptr += 4;
1480       return (base + bfd_get_32 (unit->abfd, (bfd_byte *) buf));
1481     case DW_EH_PE_udata8:
1482       *bytes_read_ptr += 8;
1483       return (base + bfd_get_64 (unit->abfd, (bfd_byte *) buf));
1484     case DW_EH_PE_sleb128:
1485       {
1486         int64_t value;
1487         const gdb_byte *end_buf = buf + (sizeof (value) + 1) * 8 / 7;
1488
1489         *bytes_read_ptr += safe_read_sleb128 (buf, end_buf, &value) - buf;
1490         return base + value;
1491       }
1492     case DW_EH_PE_sdata2:
1493       *bytes_read_ptr += 2;
1494       return (base + bfd_get_signed_16 (unit->abfd, (bfd_byte *) buf));
1495     case DW_EH_PE_sdata4:
1496       *bytes_read_ptr += 4;
1497       return (base + bfd_get_signed_32 (unit->abfd, (bfd_byte *) buf));
1498     case DW_EH_PE_sdata8:
1499       *bytes_read_ptr += 8;
1500       return (base + bfd_get_signed_64 (unit->abfd, (bfd_byte *) buf));
1501     default:
1502       internal_error (_("Invalid or unsupported encoding"));
1503     }
1504 }
1505 \f
1506
1507 /* Find CIE with the given CIE_POINTER in CIE_TABLE.  */
1508 static struct dwarf2_cie *
1509 find_cie (const dwarf2_cie_table &cie_table, ULONGEST cie_pointer)
1510 {
1511   auto iter = cie_table.find (cie_pointer);
1512   if (iter != cie_table.end ())
1513     return iter->second;
1514   return NULL;
1515 }
1516
1517 static inline int
1518 bsearch_fde_cmp (const dwarf2_fde *fde, CORE_ADDR seek_pc)
1519 {
1520   if (fde->initial_location + fde->address_range <= seek_pc)
1521     return -1;
1522   if (fde->initial_location <= seek_pc)
1523     return 0;
1524   return 1;
1525 }
1526
1527 /* Find an existing comp_unit for an objfile, if any.  */
1528
1529 static comp_unit *
1530 find_comp_unit (struct objfile *objfile)
1531 {
1532   bfd *abfd = objfile->obfd.get ();
1533   if (gdb_bfd_requires_relocations (abfd))
1534     return dwarf2_frame_objfile_data.get (objfile);
1535
1536   return dwarf2_frame_bfd_data.get (abfd);
1537 }
1538
1539 /* Store the comp_unit on OBJFILE, or the corresponding BFD, as
1540    appropriate.  */
1541
1542 static void
1543 set_comp_unit (struct objfile *objfile, struct comp_unit *unit)
1544 {
1545   bfd *abfd = objfile->obfd.get ();
1546   if (gdb_bfd_requires_relocations (abfd))
1547     return dwarf2_frame_objfile_data.set (objfile, unit);
1548
1549   return dwarf2_frame_bfd_data.set (abfd, unit);
1550 }
1551
1552 /* Find the FDE for *PC.  Return a pointer to the FDE, and store the
1553    initial location associated with it into *PC.  */
1554
1555 static struct dwarf2_fde *
1556 dwarf2_frame_find_fde (CORE_ADDR *pc, dwarf2_per_objfile **out_per_objfile)
1557 {
1558   for (objfile *objfile : current_program_space->objfiles ())
1559     {
1560       CORE_ADDR offset;
1561       CORE_ADDR seek_pc;
1562
1563       if (objfile->obfd == nullptr)
1564         continue;
1565
1566       comp_unit *unit = find_comp_unit (objfile);
1567       if (unit == NULL)
1568         {
1569           dwarf2_build_frame_info (objfile);
1570           unit = find_comp_unit (objfile);
1571         }
1572       gdb_assert (unit != NULL);
1573
1574       dwarf2_fde_table *fde_table = &unit->fde_table;
1575       if (fde_table->empty ())
1576         continue;
1577
1578       gdb_assert (!objfile->section_offsets.empty ());
1579       offset = objfile->text_section_offset ();
1580
1581       gdb_assert (!fde_table->empty ());
1582       if (*pc < offset + (*fde_table)[0]->initial_location)
1583         continue;
1584
1585       seek_pc = *pc - offset;
1586       auto it = gdb::binary_search (fde_table->begin (), fde_table->end (),
1587                                     seek_pc, bsearch_fde_cmp);
1588       if (it != fde_table->end ())
1589         {
1590           *pc = (*it)->initial_location + offset;
1591           if (out_per_objfile != nullptr)
1592             *out_per_objfile = get_dwarf2_per_objfile (objfile);
1593
1594           return *it;
1595         }
1596     }
1597   return NULL;
1598 }
1599
1600 /* Add FDE to FDE_TABLE.  */
1601 static void
1602 add_fde (dwarf2_fde_table *fde_table, struct dwarf2_fde *fde)
1603 {
1604   if (fde->address_range == 0)
1605     /* Discard useless FDEs.  */
1606     return;
1607
1608   fde_table->push_back (fde);
1609 }
1610
1611 #define DW64_CIE_ID 0xffffffffffffffffULL
1612
1613 /* Defines the type of eh_frames that are expected to be decoded: CIE, FDE
1614    or any of them.  */
1615
1616 enum eh_frame_type
1617 {
1618   EH_CIE_TYPE_ID = 1 << 0,
1619   EH_FDE_TYPE_ID = 1 << 1,
1620   EH_CIE_OR_FDE_TYPE_ID = EH_CIE_TYPE_ID | EH_FDE_TYPE_ID
1621 };
1622
1623 static const gdb_byte *decode_frame_entry (struct gdbarch *gdbarch,
1624                                            struct comp_unit *unit,
1625                                            const gdb_byte *start,
1626                                            int eh_frame_p,
1627                                            dwarf2_cie_table &cie_table,
1628                                            dwarf2_fde_table *fde_table,
1629                                            enum eh_frame_type entry_type);
1630
1631 /* Decode the next CIE or FDE, entry_type specifies the expected type.
1632    Return NULL if invalid input, otherwise the next byte to be processed.  */
1633
1634 static const gdb_byte *
1635 decode_frame_entry_1 (struct gdbarch *gdbarch,
1636                       struct comp_unit *unit, const gdb_byte *start,
1637                       int eh_frame_p,
1638                       dwarf2_cie_table &cie_table,
1639                       dwarf2_fde_table *fde_table,
1640                       enum eh_frame_type entry_type)
1641 {
1642   const gdb_byte *buf, *end;
1643   ULONGEST length;
1644   unsigned int bytes_read;
1645   int dwarf64_p;
1646   ULONGEST cie_id;
1647   ULONGEST cie_pointer;
1648   int64_t sleb128;
1649   uint64_t uleb128;
1650
1651   buf = start;
1652   length = read_initial_length (unit->abfd, buf, &bytes_read, false);
1653   buf += bytes_read;
1654   end = buf + (size_t) length;
1655
1656   if (length == 0)
1657     return end;
1658
1659   /* Are we still within the section?  */
1660   if (end <= buf || end > unit->dwarf_frame_buffer + unit->dwarf_frame_size)
1661     return NULL;
1662
1663   /* Distinguish between 32 and 64-bit encoded frame info.  */
1664   dwarf64_p = (bytes_read == 12);
1665
1666   /* In a .eh_frame section, zero is used to distinguish CIEs from FDEs.  */
1667   if (eh_frame_p)
1668     cie_id = 0;
1669   else if (dwarf64_p)
1670     cie_id = DW64_CIE_ID;
1671   else
1672     cie_id = DW_CIE_ID;
1673
1674   if (dwarf64_p)
1675     {
1676       cie_pointer = read_8_bytes (unit->abfd, buf);
1677       buf += 8;
1678     }
1679   else
1680     {
1681       cie_pointer = read_4_bytes (unit->abfd, buf);
1682       buf += 4;
1683     }
1684
1685   if (cie_pointer == cie_id)
1686     {
1687       /* This is a CIE.  */
1688       struct dwarf2_cie *cie;
1689       char *augmentation;
1690       unsigned int cie_version;
1691
1692       /* Check that a CIE was expected.  */
1693       if ((entry_type & EH_CIE_TYPE_ID) == 0)
1694         error (_("Found a CIE when not expecting it."));
1695
1696       /* Record the offset into the .debug_frame section of this CIE.  */
1697       cie_pointer = start - unit->dwarf_frame_buffer;
1698
1699       /* Check whether we've already read it.  */
1700       if (find_cie (cie_table, cie_pointer))
1701         return end;
1702
1703       cie = XOBNEW (&unit->obstack, struct dwarf2_cie);
1704       cie->initial_instructions = NULL;
1705       cie->cie_pointer = cie_pointer;
1706
1707       /* The encoding for FDE's in a normal .debug_frame section
1708          depends on the target address size.  */
1709       cie->encoding = DW_EH_PE_absptr;
1710
1711       /* We'll determine the final value later, but we need to
1712          initialize it conservatively.  */
1713       cie->signal_frame = 0;
1714
1715       /* Check version number.  */
1716       cie_version = read_1_byte (unit->abfd, buf);
1717       if (cie_version != 1 && cie_version != 3 && cie_version != 4)
1718         return NULL;
1719       cie->version = cie_version;
1720       buf += 1;
1721
1722       /* Interpret the interesting bits of the augmentation.  */
1723       cie->augmentation = augmentation = (char *) buf;
1724       buf += (strlen (augmentation) + 1);
1725
1726       /* Ignore armcc augmentations.  We only use them for quirks,
1727          and that doesn't happen until later.  */
1728       if (startswith (augmentation, "armcc"))
1729         augmentation += strlen (augmentation);
1730
1731       /* The GCC 2.x "eh" augmentation has a pointer immediately
1732          following the augmentation string, so it must be handled
1733          first.  */
1734       if (augmentation[0] == 'e' && augmentation[1] == 'h')
1735         {
1736           /* Skip.  */
1737           buf += gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT;
1738           augmentation += 2;
1739         }
1740
1741       if (cie->version >= 4)
1742         {
1743           /* FIXME: check that this is the same as from the CU header.  */
1744           cie->addr_size = read_1_byte (unit->abfd, buf);
1745           ++buf;
1746           cie->segment_size = read_1_byte (unit->abfd, buf);
1747           ++buf;
1748         }
1749       else
1750         {
1751           cie->addr_size = gdbarch_dwarf2_addr_size (gdbarch);
1752           cie->segment_size = 0;
1753         }
1754       /* Address values in .eh_frame sections are defined to have the
1755          target's pointer size.  Watchout: This breaks frame info for
1756          targets with pointer size < address size, unless a .debug_frame
1757          section exists as well.  */
1758       if (eh_frame_p)
1759         cie->ptr_size = gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT;
1760       else
1761         cie->ptr_size = cie->addr_size;
1762
1763       buf = gdb_read_uleb128 (buf, end, &uleb128);
1764       if (buf == NULL)
1765         return NULL;
1766       cie->code_alignment_factor = uleb128;
1767
1768       buf = gdb_read_sleb128 (buf, end, &sleb128);
1769       if (buf == NULL)
1770         return NULL;
1771       cie->data_alignment_factor = sleb128;
1772
1773       if (cie_version == 1)
1774         {
1775           cie->return_address_register = read_1_byte (unit->abfd, buf);
1776           ++buf;
1777         }
1778       else
1779         {
1780           buf = gdb_read_uleb128 (buf, end, &uleb128);
1781           if (buf == NULL)
1782             return NULL;
1783           cie->return_address_register = uleb128;
1784         }
1785
1786       cie->return_address_register
1787         = dwarf2_frame_adjust_regnum (gdbarch,
1788                                       cie->return_address_register,
1789                                       eh_frame_p);
1790
1791       cie->saw_z_augmentation = (*augmentation == 'z');
1792       if (cie->saw_z_augmentation)
1793         {
1794           uint64_t uleb_length;
1795
1796           buf = gdb_read_uleb128 (buf, end, &uleb_length);
1797           if (buf == NULL)
1798             return NULL;
1799           cie->initial_instructions = buf + uleb_length;
1800           augmentation++;
1801         }
1802
1803       while (*augmentation)
1804         {
1805           /* "L" indicates a byte showing how the LSDA pointer is encoded.  */
1806           if (*augmentation == 'L')
1807             {
1808               /* Skip.  */
1809               buf++;
1810               augmentation++;
1811             }
1812
1813           /* "R" indicates a byte indicating how FDE addresses are encoded.  */
1814           else if (*augmentation == 'R')
1815             {
1816               cie->encoding = *buf++;
1817               augmentation++;
1818             }
1819
1820           /* "P" indicates a personality routine in the CIE augmentation.  */
1821           else if (*augmentation == 'P')
1822             {
1823               /* Skip.  Avoid indirection since we throw away the result.  */
1824               gdb_byte encoding = (*buf++) & ~DW_EH_PE_indirect;
1825               read_encoded_value (unit, encoding, cie->ptr_size,
1826                                   buf, &bytes_read, 0);
1827               buf += bytes_read;
1828               augmentation++;
1829             }
1830
1831           /* "S" indicates a signal frame, such that the return
1832              address must not be decremented to locate the call frame
1833              info for the previous frame; it might even be the first
1834              instruction of a function, so decrementing it would take
1835              us to a different function.  */
1836           else if (*augmentation == 'S')
1837             {
1838               cie->signal_frame = 1;
1839               augmentation++;
1840             }
1841
1842           /* Otherwise we have an unknown augmentation.  Assume that either
1843              there is no augmentation data, or we saw a 'z' prefix.  */
1844           else
1845             {
1846               if (cie->initial_instructions)
1847                 buf = cie->initial_instructions;
1848               break;
1849             }
1850         }
1851
1852       cie->initial_instructions = buf;
1853       cie->end = end;
1854       cie->unit = unit;
1855
1856       cie_table[cie->cie_pointer] = cie;
1857     }
1858   else
1859     {
1860       /* This is a FDE.  */
1861       struct dwarf2_fde *fde;
1862       CORE_ADDR addr;
1863
1864       /* Check that an FDE was expected.  */
1865       if ((entry_type & EH_FDE_TYPE_ID) == 0)
1866         error (_("Found an FDE when not expecting it."));
1867
1868       /* In an .eh_frame section, the CIE pointer is the delta between the
1869          address within the FDE where the CIE pointer is stored and the
1870          address of the CIE.  Convert it to an offset into the .eh_frame
1871          section.  */
1872       if (eh_frame_p)
1873         {
1874           cie_pointer = buf - unit->dwarf_frame_buffer - cie_pointer;
1875           cie_pointer -= (dwarf64_p ? 8 : 4);
1876         }
1877
1878       /* In either case, validate the result is still within the section.  */
1879       if (cie_pointer >= unit->dwarf_frame_size)
1880         return NULL;
1881
1882       fde = XOBNEW (&unit->obstack, struct dwarf2_fde);
1883       fde->cie = find_cie (cie_table, cie_pointer);
1884       if (fde->cie == NULL)
1885         {
1886           decode_frame_entry (gdbarch, unit,
1887                               unit->dwarf_frame_buffer + cie_pointer,
1888                               eh_frame_p, cie_table, fde_table,
1889                               EH_CIE_TYPE_ID);
1890           fde->cie = find_cie (cie_table, cie_pointer);
1891         }
1892
1893       gdb_assert (fde->cie != NULL);
1894
1895       addr = read_encoded_value (unit, fde->cie->encoding, fde->cie->ptr_size,
1896                                  buf, &bytes_read, 0);
1897       fde->initial_location = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
1898       buf += bytes_read;
1899
1900       fde->address_range =
1901         read_encoded_value (unit, fde->cie->encoding & 0x0f,
1902                             fde->cie->ptr_size, buf, &bytes_read, 0);
1903       addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + fde->address_range);
1904       fde->address_range = addr - fde->initial_location;
1905       buf += bytes_read;
1906
1907       /* A 'z' augmentation in the CIE implies the presence of an
1908          augmentation field in the FDE as well.  The only thing known
1909          to be in here at present is the LSDA entry for EH.  So we
1910          can skip the whole thing.  */
1911       if (fde->cie->saw_z_augmentation)
1912         {
1913           uint64_t uleb_length;
1914
1915           buf = gdb_read_uleb128 (buf, end, &uleb_length);
1916           if (buf == NULL)
1917             return NULL;
1918           buf += uleb_length;
1919           if (buf > end)
1920             return NULL;
1921         }
1922
1923       fde->instructions = buf;
1924       fde->end = end;
1925
1926       fde->eh_frame_p = eh_frame_p;
1927
1928       add_fde (fde_table, fde);
1929     }
1930
1931   return end;
1932 }
1933
1934 /* Read a CIE or FDE in BUF and decode it. Entry_type specifies whether we
1935    expect an FDE or a CIE.  */
1936
1937 static const gdb_byte *
1938 decode_frame_entry (struct gdbarch *gdbarch,
1939                     struct comp_unit *unit, const gdb_byte *start,
1940                     int eh_frame_p,
1941                     dwarf2_cie_table &cie_table,
1942                     dwarf2_fde_table *fde_table,
1943                     enum eh_frame_type entry_type)
1944 {
1945   enum { NONE, ALIGN4, ALIGN8, FAIL } workaround = NONE;
1946   const gdb_byte *ret;
1947   ptrdiff_t start_offset;
1948
1949   while (1)
1950     {
1951       ret = decode_frame_entry_1 (gdbarch, unit, start, eh_frame_p,
1952                                   cie_table, fde_table, entry_type);
1953       if (ret != NULL)
1954         break;
1955
1956       /* We have corrupt input data of some form.  */
1957
1958       /* ??? Try, weakly, to work around compiler/assembler/linker bugs
1959          and mismatches wrt padding and alignment of debug sections.  */
1960       /* Note that there is no requirement in the standard for any
1961          alignment at all in the frame unwind sections.  Testing for
1962          alignment before trying to interpret data would be incorrect.
1963
1964          However, GCC traditionally arranged for frame sections to be
1965          sized such that the FDE length and CIE fields happen to be
1966          aligned (in theory, for performance).  This, unfortunately,
1967          was done with .align directives, which had the side effect of
1968          forcing the section to be aligned by the linker.
1969
1970          This becomes a problem when you have some other producer that
1971          creates frame sections that are not as strictly aligned.  That
1972          produces a hole in the frame info that gets filled by the 
1973          linker with zeros.
1974
1975          The GCC behaviour is arguably a bug, but it's effectively now
1976          part of the ABI, so we're now stuck with it, at least at the
1977          object file level.  A smart linker may decide, in the process
1978          of compressing duplicate CIE information, that it can rewrite
1979          the entire output section without this extra padding.  */
1980
1981       start_offset = start - unit->dwarf_frame_buffer;
1982       if (workaround < ALIGN4 && (start_offset & 3) != 0)
1983         {
1984           start += 4 - (start_offset & 3);
1985           workaround = ALIGN4;
1986           continue;
1987         }
1988       if (workaround < ALIGN8 && (start_offset & 7) != 0)
1989         {
1990           start += 8 - (start_offset & 7);
1991           workaround = ALIGN8;
1992           continue;
1993         }
1994
1995       /* Nothing left to try.  Arrange to return as if we've consumed
1996          the entire input section.  Hopefully we'll get valid info from
1997          the other of .debug_frame/.eh_frame.  */
1998       workaround = FAIL;
1999       ret = unit->dwarf_frame_buffer + unit->dwarf_frame_size;
2000       break;
2001     }
2002
2003   switch (workaround)
2004     {
2005     case NONE:
2006       break;
2007
2008     case ALIGN4:
2009       complaint (_("\
2010 Corrupt data in %s:%s; align 4 workaround apparently succeeded"),
2011                  bfd_get_filename (unit->dwarf_frame_section->owner),
2012                  bfd_section_name (unit->dwarf_frame_section));
2013       break;
2014
2015     case ALIGN8:
2016       complaint (_("\
2017 Corrupt data in %s:%s; align 8 workaround apparently succeeded"),
2018                  bfd_get_filename (unit->dwarf_frame_section->owner),
2019                  bfd_section_name (unit->dwarf_frame_section));
2020       break;
2021
2022     default:
2023       complaint (_("Corrupt data in %s:%s"),
2024                  bfd_get_filename (unit->dwarf_frame_section->owner),
2025                  bfd_section_name (unit->dwarf_frame_section));
2026       break;
2027     }
2028
2029   return ret;
2030 }
2031 \f
2032 static bool
2033 fde_is_less_than (const dwarf2_fde *aa, const dwarf2_fde *bb)
2034 {
2035   if (aa->initial_location == bb->initial_location)
2036     {
2037       if (aa->address_range != bb->address_range
2038           && aa->eh_frame_p == 0 && bb->eh_frame_p == 0)
2039         /* Linker bug, e.g. gold/10400.
2040            Work around it by keeping stable sort order.  */
2041         return aa < bb;
2042       else
2043         /* Put eh_frame entries after debug_frame ones.  */
2044         return aa->eh_frame_p < bb->eh_frame_p;
2045     }
2046
2047   return aa->initial_location < bb->initial_location;
2048 }
2049
2050 void
2051 dwarf2_build_frame_info (struct objfile *objfile)
2052 {
2053   const gdb_byte *frame_ptr;
2054   dwarf2_cie_table cie_table;
2055   dwarf2_fde_table fde_table;
2056
2057   struct gdbarch *gdbarch = objfile->arch ();
2058
2059   /* Build a minimal decoding of the DWARF2 compilation unit.  */
2060   std::unique_ptr<comp_unit> unit (new comp_unit (objfile));
2061
2062   if (objfile->separate_debug_objfile_backlink == NULL)
2063     {
2064       /* Do not read .eh_frame from separate file as they must be also
2065          present in the main file.  */
2066       dwarf2_get_section_info (objfile, DWARF2_EH_FRAME,
2067                                &unit->dwarf_frame_section,
2068                                &unit->dwarf_frame_buffer,
2069                                &unit->dwarf_frame_size);
2070       if (unit->dwarf_frame_size)
2071         {
2072           asection *got, *txt;
2073
2074           /* FIXME: kettenis/20030602: This is the DW_EH_PE_datarel base
2075              that is used for the i386/amd64 target, which currently is
2076              the only target in GCC that supports/uses the
2077              DW_EH_PE_datarel encoding.  */
2078           got = bfd_get_section_by_name (unit->abfd, ".got");
2079           if (got)
2080             unit->dbase = got->vma;
2081
2082           /* GCC emits the DW_EH_PE_textrel encoding type on sh and ia64
2083              so far.  */
2084           txt = bfd_get_section_by_name (unit->abfd, ".text");
2085           if (txt)
2086             unit->tbase = txt->vma;
2087
2088           try
2089             {
2090               frame_ptr = unit->dwarf_frame_buffer;
2091               while (frame_ptr < unit->dwarf_frame_buffer + unit->dwarf_frame_size)
2092                 frame_ptr = decode_frame_entry (gdbarch, unit.get (),
2093                                                 frame_ptr, 1,
2094                                                 cie_table, &fde_table,
2095                                                 EH_CIE_OR_FDE_TYPE_ID);
2096             }
2097
2098           catch (const gdb_exception_error &e)
2099             {
2100               warning (_("skipping .eh_frame info of %s: %s"),
2101                        objfile_name (objfile), e.what ());
2102
2103               fde_table.clear ();
2104               /* The cie_table is discarded below.  */
2105             }
2106
2107           cie_table.clear ();
2108         }
2109     }
2110
2111   dwarf2_get_section_info (objfile, DWARF2_DEBUG_FRAME,
2112                            &unit->dwarf_frame_section,
2113                            &unit->dwarf_frame_buffer,
2114                            &unit->dwarf_frame_size);
2115   if (unit->dwarf_frame_size)
2116     {
2117       size_t num_old_fde_entries = fde_table.size ();
2118
2119       try
2120         {
2121           frame_ptr = unit->dwarf_frame_buffer;
2122           while (frame_ptr < unit->dwarf_frame_buffer + unit->dwarf_frame_size)
2123             frame_ptr = decode_frame_entry (gdbarch, unit.get (), frame_ptr, 0,
2124                                             cie_table, &fde_table,
2125                                             EH_CIE_OR_FDE_TYPE_ID);
2126         }
2127       catch (const gdb_exception_error &e)
2128         {
2129           warning (_("skipping .debug_frame info of %s: %s"),
2130                    objfile_name (objfile), e.what ());
2131
2132           fde_table.resize (num_old_fde_entries);
2133         }
2134     }
2135
2136   struct dwarf2_fde *fde_prev = NULL;
2137   struct dwarf2_fde *first_non_zero_fde = NULL;
2138
2139   /* Prepare FDE table for lookups.  */
2140   std::sort (fde_table.begin (), fde_table.end (), fde_is_less_than);
2141
2142   /* Check for leftovers from --gc-sections.  The GNU linker sets
2143      the relevant symbols to zero, but doesn't zero the FDE *end*
2144      ranges because there's no relocation there.  It's (offset,
2145      length), not (start, end).  On targets where address zero is
2146      just another valid address this can be a problem, since the
2147      FDEs appear to be non-empty in the output --- we could pick
2148      out the wrong FDE.  To work around this, when overlaps are
2149      detected, we prefer FDEs that do not start at zero.
2150
2151      Start by finding the first FDE with non-zero start.  Below
2152      we'll discard all FDEs that start at zero and overlap this
2153      one.  */
2154   for (struct dwarf2_fde *fde : fde_table)
2155     {
2156       if (fde->initial_location != 0)
2157         {
2158           first_non_zero_fde = fde;
2159           break;
2160         }
2161     }
2162
2163   /* Since we'll be doing bsearch, squeeze out identical (except
2164      for eh_frame_p) fde entries so bsearch result is predictable.
2165      Also discard leftovers from --gc-sections.  */
2166   for (struct dwarf2_fde *fde : fde_table)
2167     {
2168       if (fde->initial_location == 0
2169           && first_non_zero_fde != NULL
2170           && (first_non_zero_fde->initial_location
2171               < fde->initial_location + fde->address_range))
2172         continue;
2173
2174       if (fde_prev != NULL
2175           && fde_prev->initial_location == fde->initial_location)
2176         continue;
2177
2178       unit->fde_table.push_back (fde);
2179       fde_prev = fde;
2180     }
2181   unit->fde_table.shrink_to_fit ();
2182
2183   set_comp_unit (objfile, unit.release ());
2184 }
2185
2186 /* Handle 'maintenance show dwarf unwinders'.  */
2187
2188 static void
2189 show_dwarf_unwinders_enabled_p (struct ui_file *file, int from_tty,
2190                                 struct cmd_list_element *c,
2191                                 const char *value)
2192 {
2193   gdb_printf (file,
2194               _("The DWARF stack unwinders are currently %s.\n"),
2195               value);
2196 }
2197
2198 void _initialize_dwarf2_frame ();
2199 void
2200 _initialize_dwarf2_frame ()
2201 {
2202   add_setshow_boolean_cmd ("unwinders", class_obscure,
2203                            &dwarf2_frame_unwinders_enabled_p , _("\
2204 Set whether the DWARF stack frame unwinders are used."), _("\
2205 Show whether the DWARF stack frame unwinders are used."), _("\
2206 When enabled the DWARF stack frame unwinders can be used for architectures\n\
2207 that support the DWARF unwinders.  Enabling the DWARF unwinders for an\n\
2208 architecture that doesn't support them will have no effect."),
2209                            NULL,
2210                            show_dwarf_unwinders_enabled_p,
2211                            &set_dwarf_cmdlist,
2212                            &show_dwarf_cmdlist);
2213
2214 #if GDB_SELF_TEST
2215   selftests::register_test_foreach_arch ("execute_cfa_program",
2216                                          selftests::execute_cfa_program_test);
2217 #endif
2218 }
This page took 0.146876 seconds and 4 git commands to generate.