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