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