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