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