gdb: fix off-by-one error in quirk_rust_enum
[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
JK
961
962 /* If not NULL then this frame is the bottom frame of a TAILCALL_FRAME
963 sequence. If NULL then it is a normal case with no TAILCALL_FRAME
964 involved. Non-bottom frames of a virtual tail call frames chain use
965 dwarf2_tailcall_frame_unwind unwinder so this field does not apply for
966 them. */
967 void *tailcall_cache;
8f22cb90 968};
05cbe71a 969
b9362cc7 970static struct dwarf2_frame_cache *
4a4e5149 971dwarf2_frame_cache (struct frame_info *this_frame, void **this_cache)
cfc14b3a 972{
4a4e5149 973 struct gdbarch *gdbarch = get_frame_arch (this_frame);
f6efe3f8 974 const int num_regs = gdbarch_num_cooked_regs (gdbarch);
cfc14b3a 975 struct dwarf2_frame_cache *cache;
cfc14b3a 976 struct dwarf2_fde *fde;
111c6489 977 CORE_ADDR entry_pc;
111c6489 978 const gdb_byte *instr;
cfc14b3a
MK
979
980 if (*this_cache)
9a3c8263 981 return (struct dwarf2_frame_cache *) *this_cache;
cfc14b3a
MK
982
983 /* Allocate a new cache. */
984 cache = FRAME_OBSTACK_ZALLOC (struct dwarf2_frame_cache);
985 cache->reg = FRAME_OBSTACK_CALLOC (num_regs, struct dwarf2_frame_state_reg);
8fbca658 986 *this_cache = cache;
cfc14b3a 987
cfc14b3a
MK
988 /* Unwind the PC.
989
4a4e5149 990 Note that if the next frame is never supposed to return (i.e. a call
cfc14b3a 991 to abort), the compiler might optimize away the instruction at
4a4e5149 992 its return address. As a result the return address will
cfc14b3a 993 point at some random instruction, and the CFI for that
e4e9607c 994 instruction is probably worthless to us. GCC's unwinder solves
cfc14b3a
MK
995 this problem by substracting 1 from the return address to get an
996 address in the middle of a presumed call instruction (or the
997 instruction in the associated delay slot). This should only be
998 done for "normal" frames and not for resume-type frames (signal
e4e9607c 999 handlers, sentinel frames, dummy frames). The function
ad1193e7 1000 get_frame_address_in_block does just this. It's not clear how
e4e9607c
MK
1001 reliable the method is though; there is the potential for the
1002 register state pre-call being different to that on return. */
afe37d6b 1003 CORE_ADDR pc1 = get_frame_address_in_block (this_frame);
cfc14b3a
MK
1004
1005 /* Find the correct FDE. */
afe37d6b 1006 fde = dwarf2_frame_find_fde (&pc1, &cache->text_offset);
cfc14b3a
MK
1007 gdb_assert (fde != NULL);
1008
afe37d6b
YQ
1009 /* Allocate and initialize the frame state. */
1010 struct dwarf2_frame_state fs (pc1, fde->cie);
1011
ae0d2f24 1012 cache->addr_size = fde->cie->addr_size;
cfc14b3a 1013
303b6f5d 1014 /* Check for "quirks" - known bugs in producers. */
afe37d6b 1015 dwarf2_frame_find_quirks (&fs, fde);
303b6f5d 1016
cfc14b3a 1017 /* First decode all the insns in the CIE. */
ae0d2f24 1018 execute_cfa_program (fde, fde->cie->initial_instructions,
0c92d8c1 1019 fde->cie->end, gdbarch,
21982304
TT
1020 get_frame_address_in_block (this_frame), &fs,
1021 cache->text_offset);
cfc14b3a
MK
1022
1023 /* Save the initialized register set. */
afe37d6b 1024 fs.initial = fs.regs;
cfc14b3a 1025
1aff7173
KB
1026 /* Fetching the entry pc for THIS_FRAME won't necessarily result
1027 in an address that's within the range of FDE locations. This
1028 is due to the possibility of the function occupying non-contiguous
1029 ranges. */
1009d92f
TT
1030 LONGEST entry_cfa_sp_offset;
1031 int entry_cfa_sp_offset_p = 0;
1aff7173
KB
1032 if (get_frame_func_if_available (this_frame, &entry_pc)
1033 && fde->initial_location <= entry_pc
1034 && entry_pc < fde->initial_location + fde->address_range)
111c6489
JK
1035 {
1036 /* Decode the insns in the FDE up to the entry PC. */
1037 instr = execute_cfa_program (fde, fde->instructions, fde->end, gdbarch,
21982304 1038 entry_pc, &fs, cache->text_offset);
111c6489 1039
afe37d6b
YQ
1040 if (fs.regs.cfa_how == CFA_REG_OFFSET
1041 && (dwarf_reg_to_regnum (gdbarch, fs.regs.cfa_reg)
111c6489
JK
1042 == gdbarch_sp_regnum (gdbarch)))
1043 {
1009d92f
TT
1044 entry_cfa_sp_offset = fs.regs.cfa_offset;
1045 entry_cfa_sp_offset_p = 1;
111c6489
JK
1046 }
1047 }
1048 else
1049 instr = fde->instructions;
1050
cfc14b3a 1051 /* Then decode the insns in the FDE up to our target PC. */
111c6489 1052 execute_cfa_program (fde, instr, fde->end, gdbarch,
21982304
TT
1053 get_frame_address_in_block (this_frame), &fs,
1054 cache->text_offset);
cfc14b3a 1055
a70b8144 1056 try
cfc14b3a 1057 {
8fbca658 1058 /* Calculate the CFA. */
afe37d6b 1059 switch (fs.regs.cfa_how)
8fbca658
PA
1060 {
1061 case CFA_REG_OFFSET:
afe37d6b
YQ
1062 cache->cfa = read_addr_from_reg (this_frame, fs.regs.cfa_reg);
1063 if (fs.armcc_cfa_offsets_reversed)
1064 cache->cfa -= fs.regs.cfa_offset;
8fbca658 1065 else
afe37d6b 1066 cache->cfa += fs.regs.cfa_offset;
8fbca658
PA
1067 break;
1068
1069 case CFA_EXP:
1070 cache->cfa =
afe37d6b 1071 execute_stack_op (fs.regs.cfa_exp, fs.regs.cfa_exp_len,
8fbca658
PA
1072 cache->addr_size, cache->text_offset,
1073 this_frame, 0, 0);
1074 break;
1075
1076 default:
1077 internal_error (__FILE__, __LINE__, _("Unknown CFA rule."));
1078 }
1079 }
230d2906 1080 catch (const gdb_exception_error &ex)
8fbca658
PA
1081 {
1082 if (ex.error == NOT_AVAILABLE_ERROR)
1083 {
1084 cache->unavailable_retaddr = 1;
1085 return cache;
1086 }
cfc14b3a 1087
eedc3f4f 1088 throw;
cfc14b3a
MK
1089 }
1090
05cbe71a 1091 /* Initialize the register state. */
3e2c4033
AC
1092 {
1093 int regnum;
e4e9607c 1094
3e2c4033 1095 for (regnum = 0; regnum < num_regs; regnum++)
4a4e5149 1096 dwarf2_frame_init_reg (gdbarch, regnum, &cache->reg[regnum], this_frame);
3e2c4033
AC
1097 }
1098
1099 /* Go through the DWARF2 CFI generated table and save its register
79c4cb80
MK
1100 location information in the cache. Note that we don't skip the
1101 return address column; it's perfectly all right for it to
0fde2c53 1102 correspond to a real register. */
3e2c4033
AC
1103 {
1104 int column; /* CFI speak for "register number". */
e4e9607c 1105
780942fc 1106 for (column = 0; column < fs.regs.reg.size (); column++)
3e2c4033 1107 {
3e2c4033 1108 /* Use the GDB register number as the destination index. */
0fde2c53 1109 int regnum = dwarf_reg_to_regnum (gdbarch, column);
3e2c4033 1110
0fde2c53 1111 /* Protect against a target returning a bad register. */
3e2c4033
AC
1112 if (regnum < 0 || regnum >= num_regs)
1113 continue;
1114
1115 /* NOTE: cagney/2003-09-05: CFI should specify the disposition
e4e9607c
MK
1116 of all debug info registers. If it doesn't, complain (but
1117 not too loudly). It turns out that GCC assumes that an
3e2c4033
AC
1118 unspecified register implies "same value" when CFI (draft
1119 7) specifies nothing at all. Such a register could equally
1120 be interpreted as "undefined". Also note that this check
e4e9607c
MK
1121 isn't sufficient; it only checks that all registers in the
1122 range [0 .. max column] are specified, and won't detect
3e2c4033 1123 problems when a debug info register falls outside of the
e4e9607c 1124 table. We need a way of iterating through all the valid
3e2c4033 1125 DWARF2 register numbers. */
afe37d6b 1126 if (fs.regs.reg[column].how == DWARF2_FRAME_REG_UNSPECIFIED)
f059bf6f
AC
1127 {
1128 if (cache->reg[regnum].how == DWARF2_FRAME_REG_UNSPECIFIED)
b98664d3 1129 complaint (_("\
5af949e3 1130incomplete CFI data; unspecified registers (e.g., %s) at %s"),
f059bf6f 1131 gdbarch_register_name (gdbarch, regnum),
afe37d6b 1132 paddress (gdbarch, fs.pc));
f059bf6f 1133 }
35889917 1134 else
afe37d6b 1135 cache->reg[regnum] = fs.regs.reg[column];
3e2c4033
AC
1136 }
1137 }
cfc14b3a 1138
8d5a9abc
MK
1139 /* Eliminate any DWARF2_FRAME_REG_RA rules, and save the information
1140 we need for evaluating DWARF2_FRAME_REG_RA_OFFSET rules. */
35889917
MK
1141 {
1142 int regnum;
1143
1144 for (regnum = 0; regnum < num_regs; regnum++)
1145 {
8d5a9abc
MK
1146 if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA
1147 || cache->reg[regnum].how == DWARF2_FRAME_REG_RA_OFFSET)
35889917 1148 {
780942fc
TT
1149 const std::vector<struct dwarf2_frame_state_reg> &regs
1150 = fs.regs.reg;
1151 ULONGEST retaddr_column = fs.retaddr_column;
05cbe71a 1152
d4f10bf2
MK
1153 /* It seems rather bizarre to specify an "empty" column as
1154 the return adress column. However, this is exactly
1155 what GCC does on some targets. It turns out that GCC
1156 assumes that the return address can be found in the
1157 register corresponding to the return address column.
8d5a9abc
MK
1158 Incidentally, that's how we should treat a return
1159 address column specifying "same value" too. */
780942fc
TT
1160 if (fs.retaddr_column < fs.regs.reg.size ()
1161 && regs[retaddr_column].how != DWARF2_FRAME_REG_UNSPECIFIED
1162 && regs[retaddr_column].how != DWARF2_FRAME_REG_SAME_VALUE)
8d5a9abc
MK
1163 {
1164 if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA)
780942fc 1165 cache->reg[regnum] = regs[retaddr_column];
8d5a9abc 1166 else
780942fc 1167 cache->retaddr_reg = regs[retaddr_column];
8d5a9abc 1168 }
35889917
MK
1169 else
1170 {
8d5a9abc
MK
1171 if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA)
1172 {
afe37d6b 1173 cache->reg[regnum].loc.reg = fs.retaddr_column;
8d5a9abc
MK
1174 cache->reg[regnum].how = DWARF2_FRAME_REG_SAVED_REG;
1175 }
1176 else
1177 {
afe37d6b 1178 cache->retaddr_reg.loc.reg = fs.retaddr_column;
8d5a9abc
MK
1179 cache->retaddr_reg.how = DWARF2_FRAME_REG_SAVED_REG;
1180 }
35889917
MK
1181 }
1182 }
1183 }
1184 }
cfc14b3a 1185
780942fc 1186 if (fs.retaddr_column < fs.regs.reg.size ()
afe37d6b 1187 && fs.regs.reg[fs.retaddr_column].how == DWARF2_FRAME_REG_UNDEFINED)
0228dfb9
DJ
1188 cache->undefined_retaddr = 1;
1189
1009d92f
TT
1190 dwarf2_tailcall_sniffer_first (this_frame, &cache->tailcall_cache,
1191 (entry_cfa_sp_offset_p
1192 ? &entry_cfa_sp_offset : NULL));
1193
cfc14b3a
MK
1194 return cache;
1195}
1196
8fbca658
PA
1197static enum unwind_stop_reason
1198dwarf2_frame_unwind_stop_reason (struct frame_info *this_frame,
1199 void **this_cache)
1200{
1201 struct dwarf2_frame_cache *cache
1202 = dwarf2_frame_cache (this_frame, this_cache);
1203
1204 if (cache->unavailable_retaddr)
1205 return UNWIND_UNAVAILABLE;
1206
1207 if (cache->undefined_retaddr)
1208 return UNWIND_OUTERMOST;
1209
1210 return UNWIND_NO_REASON;
1211}
1212
cfc14b3a 1213static void
4a4e5149 1214dwarf2_frame_this_id (struct frame_info *this_frame, void **this_cache,
cfc14b3a
MK
1215 struct frame_id *this_id)
1216{
1217 struct dwarf2_frame_cache *cache =
4a4e5149 1218 dwarf2_frame_cache (this_frame, this_cache);
cfc14b3a 1219
8fbca658 1220 if (cache->unavailable_retaddr)
5ce0145d
PA
1221 (*this_id) = frame_id_build_unavailable_stack (get_frame_func (this_frame));
1222 else if (cache->undefined_retaddr)
8fbca658 1223 return;
5ce0145d
PA
1224 else
1225 (*this_id) = frame_id_build (cache->cfa, get_frame_func (this_frame));
93d42b30
DJ
1226}
1227
4a4e5149
DJ
1228static struct value *
1229dwarf2_frame_prev_register (struct frame_info *this_frame, void **this_cache,
1230 int regnum)
93d42b30 1231{
4a4e5149 1232 struct gdbarch *gdbarch = get_frame_arch (this_frame);
93d42b30 1233 struct dwarf2_frame_cache *cache =
4a4e5149
DJ
1234 dwarf2_frame_cache (this_frame, this_cache);
1235 CORE_ADDR addr;
1236 int realnum;
cfc14b3a 1237
111c6489
JK
1238 /* Non-bottom frames of a virtual tail call frames chain use
1239 dwarf2_tailcall_frame_unwind unwinder so this code does not apply for
1240 them. If dwarf2_tailcall_prev_register_first does not have specific value
1241 unwind the register, tail call frames are assumed to have the register set
1242 of the top caller. */
1243 if (cache->tailcall_cache)
1244 {
1245 struct value *val;
1246
1247 val = dwarf2_tailcall_prev_register_first (this_frame,
1248 &cache->tailcall_cache,
1249 regnum);
1250 if (val)
1251 return val;
1252 }
1253
cfc14b3a
MK
1254 switch (cache->reg[regnum].how)
1255 {
05cbe71a 1256 case DWARF2_FRAME_REG_UNDEFINED:
3e2c4033 1257 /* If CFI explicitly specified that the value isn't defined,
e4e9607c 1258 mark it as optimized away; the value isn't available. */
4a4e5149 1259 return frame_unwind_got_optimized (this_frame, regnum);
cfc14b3a 1260
05cbe71a 1261 case DWARF2_FRAME_REG_SAVED_OFFSET:
4a4e5149
DJ
1262 addr = cache->cfa + cache->reg[regnum].loc.offset;
1263 return frame_unwind_got_memory (this_frame, regnum, addr);
cfc14b3a 1264
05cbe71a 1265 case DWARF2_FRAME_REG_SAVED_REG:
0fde2c53
DE
1266 realnum = dwarf_reg_to_regnum_or_error
1267 (gdbarch, cache->reg[regnum].loc.reg);
4a4e5149 1268 return frame_unwind_got_register (this_frame, regnum, realnum);
cfc14b3a 1269
05cbe71a 1270 case DWARF2_FRAME_REG_SAVED_EXP:
b348037f
YQ
1271 addr = execute_stack_op (cache->reg[regnum].loc.exp.start,
1272 cache->reg[regnum].loc.exp.len,
ac56253d
TT
1273 cache->addr_size, cache->text_offset,
1274 this_frame, cache->cfa, 1);
4a4e5149 1275 return frame_unwind_got_memory (this_frame, regnum, addr);
cfc14b3a 1276
46ea248b 1277 case DWARF2_FRAME_REG_SAVED_VAL_OFFSET:
4a4e5149
DJ
1278 addr = cache->cfa + cache->reg[regnum].loc.offset;
1279 return frame_unwind_got_constant (this_frame, regnum, addr);
46ea248b
AO
1280
1281 case DWARF2_FRAME_REG_SAVED_VAL_EXP:
b348037f
YQ
1282 addr = execute_stack_op (cache->reg[regnum].loc.exp.start,
1283 cache->reg[regnum].loc.exp.len,
ac56253d
TT
1284 cache->addr_size, cache->text_offset,
1285 this_frame, cache->cfa, 1);
4a4e5149 1286 return frame_unwind_got_constant (this_frame, regnum, addr);
46ea248b 1287
05cbe71a 1288 case DWARF2_FRAME_REG_UNSPECIFIED:
3e2c4033
AC
1289 /* GCC, in its infinite wisdom decided to not provide unwind
1290 information for registers that are "same value". Since
1291 DWARF2 (3 draft 7) doesn't define such behavior, said
1292 registers are actually undefined (which is different to CFI
1293 "undefined"). Code above issues a complaint about this.
1294 Here just fudge the books, assume GCC, and that the value is
1295 more inner on the stack. */
4a4e5149 1296 return frame_unwind_got_register (this_frame, regnum, regnum);
3e2c4033 1297
05cbe71a 1298 case DWARF2_FRAME_REG_SAME_VALUE:
4a4e5149 1299 return frame_unwind_got_register (this_frame, regnum, regnum);
cfc14b3a 1300
05cbe71a 1301 case DWARF2_FRAME_REG_CFA:
4a4e5149 1302 return frame_unwind_got_address (this_frame, regnum, cache->cfa);
35889917 1303
ea7963f0 1304 case DWARF2_FRAME_REG_CFA_OFFSET:
4a4e5149
DJ
1305 addr = cache->cfa + cache->reg[regnum].loc.offset;
1306 return frame_unwind_got_address (this_frame, regnum, addr);
ea7963f0 1307
8d5a9abc 1308 case DWARF2_FRAME_REG_RA_OFFSET:
4a4e5149 1309 addr = cache->reg[regnum].loc.offset;
0fde2c53 1310 regnum = dwarf_reg_to_regnum_or_error
4a4e5149
DJ
1311 (gdbarch, cache->retaddr_reg.loc.reg);
1312 addr += get_frame_register_unsigned (this_frame, regnum);
1313 return frame_unwind_got_address (this_frame, regnum, addr);
8d5a9abc 1314
b39cc962
DJ
1315 case DWARF2_FRAME_REG_FN:
1316 return cache->reg[regnum].loc.fn (this_frame, this_cache, regnum);
1317
cfc14b3a 1318 default:
e2e0b3e5 1319 internal_error (__FILE__, __LINE__, _("Unknown register rule."));
cfc14b3a
MK
1320 }
1321}
1322
111c6489
JK
1323/* Proxy for tailcall_frame_dealloc_cache for bottom frame of a virtual tail
1324 call frames chain. */
1325
1326static void
1327dwarf2_frame_dealloc_cache (struct frame_info *self, void *this_cache)
1328{
1329 struct dwarf2_frame_cache *cache = dwarf2_frame_cache (self, &this_cache);
1330
1331 if (cache->tailcall_cache)
1332 dwarf2_tailcall_frame_unwind.dealloc_cache (self, cache->tailcall_cache);
1333}
1334
4a4e5149
DJ
1335static int
1336dwarf2_frame_sniffer (const struct frame_unwind *self,
1337 struct frame_info *this_frame, void **this_cache)
cfc14b3a 1338{
3c3bb058
AB
1339 if (!dwarf2_frame_unwinders_enabled_p)
1340 return 0;
1341
30baf67b 1342 /* Grab an address that is guaranteed to reside somewhere within the
4a4e5149 1343 function. get_frame_pc(), with a no-return next function, can
93d42b30
DJ
1344 end up returning something past the end of this function's body.
1345 If the frame we're sniffing for is a signal frame whose start
1346 address is placed on the stack by the OS, its FDE must
4a4e5149
DJ
1347 extend one byte before its start address or we could potentially
1348 select the FDE of the previous function. */
1349 CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
ac56253d 1350 struct dwarf2_fde *fde = dwarf2_frame_find_fde (&block_addr, NULL);
9a619af0 1351
56c987f6 1352 if (!fde)
4a4e5149 1353 return 0;
3ed09a32
DJ
1354
1355 /* On some targets, signal trampolines may have unwind information.
1356 We need to recognize them so that we set the frame type
1357 correctly. */
1358
56c987f6 1359 if (fde->cie->signal_frame
4a4e5149
DJ
1360 || dwarf2_frame_signal_frame_p (get_frame_arch (this_frame),
1361 this_frame))
1362 return self->type == SIGTRAMP_FRAME;
1363
111c6489
JK
1364 if (self->type != NORMAL_FRAME)
1365 return 0;
1366
111c6489 1367 return 1;
4a4e5149
DJ
1368}
1369
1370static const struct frame_unwind dwarf2_frame_unwind =
1371{
1372 NORMAL_FRAME,
8fbca658 1373 dwarf2_frame_unwind_stop_reason,
4a4e5149
DJ
1374 dwarf2_frame_this_id,
1375 dwarf2_frame_prev_register,
1376 NULL,
111c6489
JK
1377 dwarf2_frame_sniffer,
1378 dwarf2_frame_dealloc_cache
4a4e5149
DJ
1379};
1380
1381static const struct frame_unwind dwarf2_signal_frame_unwind =
1382{
1383 SIGTRAMP_FRAME,
8fbca658 1384 dwarf2_frame_unwind_stop_reason,
4a4e5149
DJ
1385 dwarf2_frame_this_id,
1386 dwarf2_frame_prev_register,
1387 NULL,
111c6489
JK
1388 dwarf2_frame_sniffer,
1389
1390 /* TAILCALL_CACHE can never be in such frame to need dealloc_cache. */
1391 NULL
4a4e5149 1392};
cfc14b3a 1393
4a4e5149
DJ
1394/* Append the DWARF-2 frame unwinders to GDBARCH's list. */
1395
1396void
1397dwarf2_append_unwinders (struct gdbarch *gdbarch)
1398{
1399 frame_unwind_append_unwinder (gdbarch, &dwarf2_frame_unwind);
1400 frame_unwind_append_unwinder (gdbarch, &dwarf2_signal_frame_unwind);
cfc14b3a
MK
1401}
1402\f
1403
1404/* There is no explicitly defined relationship between the CFA and the
1405 location of frame's local variables and arguments/parameters.
1406 Therefore, frame base methods on this page should probably only be
1407 used as a last resort, just to avoid printing total garbage as a
1408 response to the "info frame" command. */
1409
1410static CORE_ADDR
4a4e5149 1411dwarf2_frame_base_address (struct frame_info *this_frame, void **this_cache)
cfc14b3a
MK
1412{
1413 struct dwarf2_frame_cache *cache =
4a4e5149 1414 dwarf2_frame_cache (this_frame, this_cache);
cfc14b3a
MK
1415
1416 return cache->cfa;
1417}
1418
1419static const struct frame_base dwarf2_frame_base =
1420{
1421 &dwarf2_frame_unwind,
1422 dwarf2_frame_base_address,
1423 dwarf2_frame_base_address,
1424 dwarf2_frame_base_address
1425};
1426
1427const struct frame_base *
4a4e5149 1428dwarf2_frame_base_sniffer (struct frame_info *this_frame)
cfc14b3a 1429{
4a4e5149 1430 CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
9a619af0 1431
ac56253d 1432 if (dwarf2_frame_find_fde (&block_addr, NULL))
cfc14b3a
MK
1433 return &dwarf2_frame_base;
1434
1435 return NULL;
1436}
e7802207
TT
1437
1438/* Compute the CFA for THIS_FRAME, but only if THIS_FRAME came from
1439 the DWARF unwinder. This is used to implement
1440 DW_OP_call_frame_cfa. */
1441
1442CORE_ADDR
1443dwarf2_frame_cfa (struct frame_info *this_frame)
1444{
0b722aec
MM
1445 if (frame_unwinder_is (this_frame, &record_btrace_tailcall_frame_unwind)
1446 || frame_unwinder_is (this_frame, &record_btrace_frame_unwind))
1447 throw_error (NOT_AVAILABLE_ERROR,
1448 _("cfa not available for record btrace target"));
1449
e7802207
TT
1450 while (get_frame_type (this_frame) == INLINE_FRAME)
1451 this_frame = get_prev_frame (this_frame);
32261e52
MM
1452 if (get_frame_unwind_stop_reason (this_frame) == UNWIND_UNAVAILABLE)
1453 throw_error (NOT_AVAILABLE_ERROR,
1454 _("can't compute CFA for this frame: "
1455 "required registers or memory are unavailable"));
14aba1ac
JB
1456
1457 if (get_frame_id (this_frame).stack_status != FID_STACK_VALID)
1458 throw_error (NOT_AVAILABLE_ERROR,
1459 _("can't compute CFA for this frame: "
1460 "frame base not available"));
1461
e7802207
TT
1462 return get_frame_base (this_frame);
1463}
cfc14b3a 1464\f
3d4560f7
TT
1465/* We store the frame data on the BFD. This is only done if it is
1466 independent of the address space and so can be shared. */
1467static const struct bfd_key<comp_unit> dwarf2_frame_bfd_data;
1468
1469/* If any BFD sections require relocations (note; really should be if
1470 any debug info requires relocations), then we store the frame data
1471 on the objfile instead, and do not share it. */
1472const struct objfile_key<comp_unit> dwarf2_frame_objfile_data;
cfc14b3a
MK
1473\f
1474
1475/* Pointer encoding helper functions. */
1476
1477/* GCC supports exception handling based on DWARF2 CFI. However, for
1478 technical reasons, it encodes addresses in its FDE's in a different
1479 way. Several "pointer encodings" are supported. The encoding
1480 that's used for a particular FDE is determined by the 'R'
1481 augmentation in the associated CIE. The argument of this
1482 augmentation is a single byte.
1483
1484 The address can be encoded as 2 bytes, 4 bytes, 8 bytes, or as a
1485 LEB128. This is encoded in bits 0, 1 and 2. Bit 3 encodes whether
1486 the address is signed or unsigned. Bits 4, 5 and 6 encode how the
1487 address should be interpreted (absolute, relative to the current
1488 position in the FDE, ...). Bit 7, indicates that the address
1489 should be dereferenced. */
1490
852483bc 1491static gdb_byte
cfc14b3a
MK
1492encoding_for_size (unsigned int size)
1493{
1494 switch (size)
1495 {
1496 case 2:
1497 return DW_EH_PE_udata2;
1498 case 4:
1499 return DW_EH_PE_udata4;
1500 case 8:
1501 return DW_EH_PE_udata8;
1502 default:
e2e0b3e5 1503 internal_error (__FILE__, __LINE__, _("Unsupported address size"));
cfc14b3a
MK
1504 }
1505}
1506
cfc14b3a 1507static CORE_ADDR
852483bc 1508read_encoded_value (struct comp_unit *unit, gdb_byte encoding,
0d45f56e
TT
1509 int ptr_len, const gdb_byte *buf,
1510 unsigned int *bytes_read_ptr,
ae0d2f24 1511 CORE_ADDR func_base)
cfc14b3a 1512{
68f6cf99 1513 ptrdiff_t offset;
cfc14b3a
MK
1514 CORE_ADDR base;
1515
1516 /* GCC currently doesn't generate DW_EH_PE_indirect encodings for
1517 FDE's. */
1518 if (encoding & DW_EH_PE_indirect)
1519 internal_error (__FILE__, __LINE__,
e2e0b3e5 1520 _("Unsupported encoding: DW_EH_PE_indirect"));
cfc14b3a 1521
68f6cf99
MK
1522 *bytes_read_ptr = 0;
1523
cfc14b3a
MK
1524 switch (encoding & 0x70)
1525 {
1526 case DW_EH_PE_absptr:
1527 base = 0;
1528 break;
1529 case DW_EH_PE_pcrel:
fd361982 1530 base = bfd_section_vma (unit->dwarf_frame_section);
852483bc 1531 base += (buf - unit->dwarf_frame_buffer);
cfc14b3a 1532 break;
0912c7f2
MK
1533 case DW_EH_PE_datarel:
1534 base = unit->dbase;
1535 break;
0fd85043
CV
1536 case DW_EH_PE_textrel:
1537 base = unit->tbase;
1538 break;
03ac2a74 1539 case DW_EH_PE_funcrel:
ae0d2f24 1540 base = func_base;
03ac2a74 1541 break;
68f6cf99
MK
1542 case DW_EH_PE_aligned:
1543 base = 0;
852483bc 1544 offset = buf - unit->dwarf_frame_buffer;
68f6cf99
MK
1545 if ((offset % ptr_len) != 0)
1546 {
1547 *bytes_read_ptr = ptr_len - (offset % ptr_len);
1548 buf += *bytes_read_ptr;
1549 }
1550 break;
cfc14b3a 1551 default:
3e43a32a
MS
1552 internal_error (__FILE__, __LINE__,
1553 _("Invalid or unsupported encoding"));
cfc14b3a
MK
1554 }
1555
b04de778 1556 if ((encoding & 0x07) == 0x00)
f2fec864
DJ
1557 {
1558 encoding |= encoding_for_size (ptr_len);
1559 if (bfd_get_sign_extend_vma (unit->abfd))
1560 encoding |= DW_EH_PE_signed;
1561 }
cfc14b3a
MK
1562
1563 switch (encoding & 0x0f)
1564 {
a81b10ae
MK
1565 case DW_EH_PE_uleb128:
1566 {
9fccedf7 1567 uint64_t value;
0d45f56e 1568 const gdb_byte *end_buf = buf + (sizeof (value) + 1) * 8 / 7;
9a619af0 1569
f664829e 1570 *bytes_read_ptr += safe_read_uleb128 (buf, end_buf, &value) - buf;
a81b10ae
MK
1571 return base + value;
1572 }
cfc14b3a 1573 case DW_EH_PE_udata2:
68f6cf99 1574 *bytes_read_ptr += 2;
cfc14b3a
MK
1575 return (base + bfd_get_16 (unit->abfd, (bfd_byte *) buf));
1576 case DW_EH_PE_udata4:
68f6cf99 1577 *bytes_read_ptr += 4;
cfc14b3a
MK
1578 return (base + bfd_get_32 (unit->abfd, (bfd_byte *) buf));
1579 case DW_EH_PE_udata8:
68f6cf99 1580 *bytes_read_ptr += 8;
cfc14b3a 1581 return (base + bfd_get_64 (unit->abfd, (bfd_byte *) buf));
a81b10ae
MK
1582 case DW_EH_PE_sleb128:
1583 {
9fccedf7 1584 int64_t value;
0d45f56e 1585 const gdb_byte *end_buf = buf + (sizeof (value) + 1) * 8 / 7;
9a619af0 1586
f664829e 1587 *bytes_read_ptr += safe_read_sleb128 (buf, end_buf, &value) - buf;
a81b10ae
MK
1588 return base + value;
1589 }
cfc14b3a 1590 case DW_EH_PE_sdata2:
68f6cf99 1591 *bytes_read_ptr += 2;
cfc14b3a
MK
1592 return (base + bfd_get_signed_16 (unit->abfd, (bfd_byte *) buf));
1593 case DW_EH_PE_sdata4:
68f6cf99 1594 *bytes_read_ptr += 4;
cfc14b3a
MK
1595 return (base + bfd_get_signed_32 (unit->abfd, (bfd_byte *) buf));
1596 case DW_EH_PE_sdata8:
68f6cf99 1597 *bytes_read_ptr += 8;
cfc14b3a
MK
1598 return (base + bfd_get_signed_64 (unit->abfd, (bfd_byte *) buf));
1599 default:
3e43a32a
MS
1600 internal_error (__FILE__, __LINE__,
1601 _("Invalid or unsupported encoding"));
cfc14b3a
MK
1602 }
1603}
1604\f
1605
b01c8410
PP
1606/* Find CIE with the given CIE_POINTER in CIE_TABLE. */
1607static struct dwarf2_cie *
93878f47 1608find_cie (const dwarf2_cie_table &cie_table, ULONGEST cie_pointer)
b01c8410 1609{
93878f47
TT
1610 auto iter = cie_table.find (cie_pointer);
1611 if (iter != cie_table.end ())
1612 return iter->second;
cfc14b3a
MK
1613 return NULL;
1614}
1615
35e65c49
CB
1616static inline int
1617bsearch_fde_cmp (const dwarf2_fde *fde, CORE_ADDR seek_pc)
b01c8410 1618{
35e65c49 1619 if (fde->initial_location + fde->address_range <= seek_pc)
b01c8410 1620 return -1;
35e65c49 1621 if (fde->initial_location <= seek_pc)
b01c8410
PP
1622 return 0;
1623 return 1;
cfc14b3a
MK
1624}
1625
3d4560f7
TT
1626/* Find an existing comp_unit for an objfile, if any. */
1627
1628static comp_unit *
1629find_comp_unit (struct objfile *objfile)
1630{
1631 bfd *abfd = objfile->obfd;
1632 if (gdb_bfd_requires_relocations (abfd))
1633 return dwarf2_frame_bfd_data.get (abfd);
1634 return dwarf2_frame_objfile_data.get (objfile);
1635}
1636
1637/* Store the comp_unit on OBJFILE, or the corresponding BFD, as
1638 appropriate. */
1639
1640static void
1641set_comp_unit (struct objfile *objfile, struct comp_unit *unit)
1642{
1643 bfd *abfd = objfile->obfd;
1644 if (gdb_bfd_requires_relocations (abfd))
1645 return dwarf2_frame_bfd_data.set (abfd, unit);
1646 return dwarf2_frame_objfile_data.set (objfile, unit);
1647}
1648
cfc14b3a 1649/* Find the FDE for *PC. Return a pointer to the FDE, and store the
85102364 1650 initial location associated with it into *PC. */
cfc14b3a
MK
1651
1652static struct dwarf2_fde *
ac56253d 1653dwarf2_frame_find_fde (CORE_ADDR *pc, CORE_ADDR *out_offset)
cfc14b3a 1654{
2030c079 1655 for (objfile *objfile : current_program_space->objfiles ())
cfc14b3a 1656 {
cfc14b3a 1657 CORE_ADDR offset;
b01c8410 1658 CORE_ADDR seek_pc;
cfc14b3a 1659
3d4560f7 1660 comp_unit *unit = find_comp_unit (objfile);
a7a3ae5c 1661 if (unit == NULL)
be391dca
TT
1662 {
1663 dwarf2_build_frame_info (objfile);
3d4560f7 1664 unit = find_comp_unit (objfile);
be391dca 1665 }
a7a3ae5c 1666 gdb_assert (unit != NULL);
be391dca 1667
a7a3ae5c 1668 dwarf2_fde_table *fde_table = &unit->fde_table;
a9d65418 1669 if (fde_table->empty ())
4ae9ee8e
DJ
1670 continue;
1671
6a053cb1 1672 gdb_assert (!objfile->section_offsets.empty ());
b3b3bada 1673 offset = objfile->text_section_offset ();
4ae9ee8e 1674
a9d65418
TT
1675 gdb_assert (!fde_table->empty ());
1676 if (*pc < offset + (*fde_table)[0]->initial_location)
b01c8410
PP
1677 continue;
1678
1679 seek_pc = *pc - offset;
a9d65418
TT
1680 auto it = gdb::binary_search (fde_table->begin (), fde_table->end (),
1681 seek_pc, bsearch_fde_cmp);
1682 if (it != fde_table->end ())
b01c8410 1683 {
35e65c49 1684 *pc = (*it)->initial_location + offset;
ac56253d
TT
1685 if (out_offset)
1686 *out_offset = offset;
35e65c49 1687 return *it;
b01c8410 1688 }
cfc14b3a 1689 }
cfc14b3a
MK
1690 return NULL;
1691}
1692
a9d65418 1693/* Add FDE to FDE_TABLE. */
cfc14b3a 1694static void
a9d65418 1695add_fde (dwarf2_fde_table *fde_table, struct dwarf2_fde *fde)
cfc14b3a 1696{
b01c8410
PP
1697 if (fde->address_range == 0)
1698 /* Discard useless FDEs. */
1699 return;
1700
a9d65418 1701 fde_table->push_back (fde);
cfc14b3a
MK
1702}
1703
cfc14b3a 1704#define DW64_CIE_ID 0xffffffffffffffffULL
cfc14b3a 1705
8bd90839
FM
1706/* Defines the type of eh_frames that are expected to be decoded: CIE, FDE
1707 or any of them. */
1708
1709enum eh_frame_type
1710{
1711 EH_CIE_TYPE_ID = 1 << 0,
1712 EH_FDE_TYPE_ID = 1 << 1,
1713 EH_CIE_OR_FDE_TYPE_ID = EH_CIE_TYPE_ID | EH_FDE_TYPE_ID
1714};
1715
4debb237
TT
1716static const gdb_byte *decode_frame_entry (struct gdbarch *gdbarch,
1717 struct comp_unit *unit,
f664829e
DE
1718 const gdb_byte *start,
1719 int eh_frame_p,
93878f47 1720 dwarf2_cie_table &cie_table,
a9d65418 1721 dwarf2_fde_table *fde_table,
f664829e 1722 enum eh_frame_type entry_type);
8bd90839
FM
1723
1724/* Decode the next CIE or FDE, entry_type specifies the expected type.
1725 Return NULL if invalid input, otherwise the next byte to be processed. */
cfc14b3a 1726
f664829e 1727static const gdb_byte *
4debb237
TT
1728decode_frame_entry_1 (struct gdbarch *gdbarch,
1729 struct comp_unit *unit, const gdb_byte *start,
f664829e 1730 int eh_frame_p,
93878f47 1731 dwarf2_cie_table &cie_table,
a9d65418 1732 dwarf2_fde_table *fde_table,
8bd90839 1733 enum eh_frame_type entry_type)
cfc14b3a 1734{
f664829e 1735 const gdb_byte *buf, *end;
723adb65 1736 ULONGEST length;
cfc14b3a 1737 unsigned int bytes_read;
6896c0c7
RH
1738 int dwarf64_p;
1739 ULONGEST cie_id;
cfc14b3a 1740 ULONGEST cie_pointer;
9fccedf7
DE
1741 int64_t sleb128;
1742 uint64_t uleb128;
cfc14b3a 1743
6896c0c7 1744 buf = start;
4075cb26 1745 length = read_initial_length (unit->abfd, buf, &bytes_read, false);
cfc14b3a 1746 buf += bytes_read;
723adb65 1747 end = buf + (size_t) length;
6896c0c7 1748
cfc14b3a
MK
1749 if (length == 0)
1750 return end;
1751
723adb65
SL
1752 /* Are we still within the section? */
1753 if (end <= buf || end > unit->dwarf_frame_buffer + unit->dwarf_frame_size)
1754 return NULL;
1755
6896c0c7
RH
1756 /* Distinguish between 32 and 64-bit encoded frame info. */
1757 dwarf64_p = (bytes_read == 12);
cfc14b3a 1758
6896c0c7 1759 /* In a .eh_frame section, zero is used to distinguish CIEs from FDEs. */
cfc14b3a
MK
1760 if (eh_frame_p)
1761 cie_id = 0;
1762 else if (dwarf64_p)
1763 cie_id = DW64_CIE_ID;
6896c0c7
RH
1764 else
1765 cie_id = DW_CIE_ID;
cfc14b3a
MK
1766
1767 if (dwarf64_p)
1768 {
1769 cie_pointer = read_8_bytes (unit->abfd, buf);
1770 buf += 8;
1771 }
1772 else
1773 {
1774 cie_pointer = read_4_bytes (unit->abfd, buf);
1775 buf += 4;
1776 }
1777
1778 if (cie_pointer == cie_id)
1779 {
1780 /* This is a CIE. */
1781 struct dwarf2_cie *cie;
1782 char *augmentation;
28ba0b33 1783 unsigned int cie_version;
cfc14b3a 1784
8bd90839
FM
1785 /* Check that a CIE was expected. */
1786 if ((entry_type & EH_CIE_TYPE_ID) == 0)
1787 error (_("Found a CIE when not expecting it."));
1788
cfc14b3a
MK
1789 /* Record the offset into the .debug_frame section of this CIE. */
1790 cie_pointer = start - unit->dwarf_frame_buffer;
1791
1792 /* Check whether we've already read it. */
b01c8410 1793 if (find_cie (cie_table, cie_pointer))
cfc14b3a
MK
1794 return end;
1795
0d404d44 1796 cie = XOBNEW (&unit->obstack, struct dwarf2_cie);
cfc14b3a
MK
1797 cie->initial_instructions = NULL;
1798 cie->cie_pointer = cie_pointer;
1799
1800 /* The encoding for FDE's in a normal .debug_frame section
32b05c07
MK
1801 depends on the target address size. */
1802 cie->encoding = DW_EH_PE_absptr;
cfc14b3a 1803
56c987f6
AO
1804 /* We'll determine the final value later, but we need to
1805 initialize it conservatively. */
1806 cie->signal_frame = 0;
1807
cfc14b3a 1808 /* Check version number. */
28ba0b33 1809 cie_version = read_1_byte (unit->abfd, buf);
2dc7f7b3 1810 if (cie_version != 1 && cie_version != 3 && cie_version != 4)
6896c0c7 1811 return NULL;
303b6f5d 1812 cie->version = cie_version;
cfc14b3a
MK
1813 buf += 1;
1814
1815 /* Interpret the interesting bits of the augmentation. */
303b6f5d 1816 cie->augmentation = augmentation = (char *) buf;
852483bc 1817 buf += (strlen (augmentation) + 1);
cfc14b3a 1818
303b6f5d
DJ
1819 /* Ignore armcc augmentations. We only use them for quirks,
1820 and that doesn't happen until later. */
61012eef 1821 if (startswith (augmentation, "armcc"))
303b6f5d
DJ
1822 augmentation += strlen (augmentation);
1823
cfc14b3a
MK
1824 /* The GCC 2.x "eh" augmentation has a pointer immediately
1825 following the augmentation string, so it must be handled
1826 first. */
1827 if (augmentation[0] == 'e' && augmentation[1] == 'h')
1828 {
1829 /* Skip. */
5e2b427d 1830 buf += gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT;
cfc14b3a
MK
1831 augmentation += 2;
1832 }
1833
2dc7f7b3
TT
1834 if (cie->version >= 4)
1835 {
1836 /* FIXME: check that this is the same as from the CU header. */
1837 cie->addr_size = read_1_byte (unit->abfd, buf);
1838 ++buf;
1839 cie->segment_size = read_1_byte (unit->abfd, buf);
1840 ++buf;
1841 }
1842 else
1843 {
8da614df 1844 cie->addr_size = gdbarch_dwarf2_addr_size (gdbarch);
2dc7f7b3
TT
1845 cie->segment_size = 0;
1846 }
8da614df
CV
1847 /* Address values in .eh_frame sections are defined to have the
1848 target's pointer size. Watchout: This breaks frame info for
1849 targets with pointer size < address size, unless a .debug_frame
0963b4bd 1850 section exists as well. */
8da614df
CV
1851 if (eh_frame_p)
1852 cie->ptr_size = gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT;
1853 else
1854 cie->ptr_size = cie->addr_size;
2dc7f7b3 1855
f664829e
DE
1856 buf = gdb_read_uleb128 (buf, end, &uleb128);
1857 if (buf == NULL)
1858 return NULL;
1859 cie->code_alignment_factor = uleb128;
cfc14b3a 1860
f664829e
DE
1861 buf = gdb_read_sleb128 (buf, end, &sleb128);
1862 if (buf == NULL)
1863 return NULL;
1864 cie->data_alignment_factor = sleb128;
cfc14b3a 1865
28ba0b33
PB
1866 if (cie_version == 1)
1867 {
1868 cie->return_address_register = read_1_byte (unit->abfd, buf);
f664829e 1869 ++buf;
28ba0b33
PB
1870 }
1871 else
f664829e
DE
1872 {
1873 buf = gdb_read_uleb128 (buf, end, &uleb128);
1874 if (buf == NULL)
1875 return NULL;
1876 cie->return_address_register = uleb128;
1877 }
1878
4fc771b8 1879 cie->return_address_register
5e2b427d 1880 = dwarf2_frame_adjust_regnum (gdbarch,
4fc771b8
DJ
1881 cie->return_address_register,
1882 eh_frame_p);
4bf8967c 1883
7131cb6e
RH
1884 cie->saw_z_augmentation = (*augmentation == 'z');
1885 if (cie->saw_z_augmentation)
cfc14b3a 1886 {
b926417a 1887 uint64_t uleb_length;
cfc14b3a 1888
b926417a 1889 buf = gdb_read_uleb128 (buf, end, &uleb_length);
f664829e 1890 if (buf == NULL)
6896c0c7 1891 return NULL;
b926417a 1892 cie->initial_instructions = buf + uleb_length;
cfc14b3a
MK
1893 augmentation++;
1894 }
1895
1896 while (*augmentation)
1897 {
1898 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
1899 if (*augmentation == 'L')
1900 {
1901 /* Skip. */
1902 buf++;
1903 augmentation++;
1904 }
1905
1906 /* "R" indicates a byte indicating how FDE addresses are encoded. */
1907 else if (*augmentation == 'R')
1908 {
1909 cie->encoding = *buf++;
1910 augmentation++;
1911 }
1912
1913 /* "P" indicates a personality routine in the CIE augmentation. */
1914 else if (*augmentation == 'P')
1915 {
1234d960 1916 /* Skip. Avoid indirection since we throw away the result. */
852483bc 1917 gdb_byte encoding = (*buf++) & ~DW_EH_PE_indirect;
8da614df 1918 read_encoded_value (unit, encoding, cie->ptr_size,
ae0d2f24 1919 buf, &bytes_read, 0);
f724bf08 1920 buf += bytes_read;
cfc14b3a
MK
1921 augmentation++;
1922 }
1923
56c987f6
AO
1924 /* "S" indicates a signal frame, such that the return
1925 address must not be decremented to locate the call frame
1926 info for the previous frame; it might even be the first
1927 instruction of a function, so decrementing it would take
1928 us to a different function. */
1929 else if (*augmentation == 'S')
1930 {
1931 cie->signal_frame = 1;
1932 augmentation++;
1933 }
1934
3e9a2e52
DJ
1935 /* Otherwise we have an unknown augmentation. Assume that either
1936 there is no augmentation data, or we saw a 'z' prefix. */
cfc14b3a
MK
1937 else
1938 {
3e9a2e52
DJ
1939 if (cie->initial_instructions)
1940 buf = cie->initial_instructions;
cfc14b3a
MK
1941 break;
1942 }
1943 }
1944
1945 cie->initial_instructions = buf;
1946 cie->end = end;
b01c8410 1947 cie->unit = unit;
cfc14b3a 1948
93878f47 1949 cie_table[cie->cie_pointer] = cie;
cfc14b3a
MK
1950 }
1951 else
1952 {
1953 /* This is a FDE. */
1954 struct dwarf2_fde *fde;
3e29f34a 1955 CORE_ADDR addr;
cfc14b3a 1956
8bd90839
FM
1957 /* Check that an FDE was expected. */
1958 if ((entry_type & EH_FDE_TYPE_ID) == 0)
1959 error (_("Found an FDE when not expecting it."));
1960
6896c0c7
RH
1961 /* In an .eh_frame section, the CIE pointer is the delta between the
1962 address within the FDE where the CIE pointer is stored and the
1963 address of the CIE. Convert it to an offset into the .eh_frame
1964 section. */
cfc14b3a
MK
1965 if (eh_frame_p)
1966 {
cfc14b3a
MK
1967 cie_pointer = buf - unit->dwarf_frame_buffer - cie_pointer;
1968 cie_pointer -= (dwarf64_p ? 8 : 4);
1969 }
1970
6896c0c7
RH
1971 /* In either case, validate the result is still within the section. */
1972 if (cie_pointer >= unit->dwarf_frame_size)
1973 return NULL;
1974
0d404d44 1975 fde = XOBNEW (&unit->obstack, struct dwarf2_fde);
b01c8410 1976 fde->cie = find_cie (cie_table, cie_pointer);
cfc14b3a
MK
1977 if (fde->cie == NULL)
1978 {
4debb237
TT
1979 decode_frame_entry (gdbarch, unit,
1980 unit->dwarf_frame_buffer + cie_pointer,
8bd90839
FM
1981 eh_frame_p, cie_table, fde_table,
1982 EH_CIE_TYPE_ID);
b01c8410 1983 fde->cie = find_cie (cie_table, cie_pointer);
cfc14b3a
MK
1984 }
1985
1986 gdb_assert (fde->cie != NULL);
1987
3e29f34a
MR
1988 addr = read_encoded_value (unit, fde->cie->encoding, fde->cie->ptr_size,
1989 buf, &bytes_read, 0);
1990 fde->initial_location = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
cfc14b3a
MK
1991 buf += bytes_read;
1992
1993 fde->address_range =
ae0d2f24 1994 read_encoded_value (unit, fde->cie->encoding & 0x0f,
8da614df 1995 fde->cie->ptr_size, buf, &bytes_read, 0);
3e29f34a
MR
1996 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + fde->address_range);
1997 fde->address_range = addr - fde->initial_location;
cfc14b3a
MK
1998 buf += bytes_read;
1999
7131cb6e
RH
2000 /* A 'z' augmentation in the CIE implies the presence of an
2001 augmentation field in the FDE as well. The only thing known
2002 to be in here at present is the LSDA entry for EH. So we
2003 can skip the whole thing. */
2004 if (fde->cie->saw_z_augmentation)
2005 {
b926417a 2006 uint64_t uleb_length;
7131cb6e 2007
b926417a 2008 buf = gdb_read_uleb128 (buf, end, &uleb_length);
f664829e
DE
2009 if (buf == NULL)
2010 return NULL;
b926417a 2011 buf += uleb_length;
6896c0c7
RH
2012 if (buf > end)
2013 return NULL;
7131cb6e
RH
2014 }
2015
cfc14b3a
MK
2016 fde->instructions = buf;
2017 fde->end = end;
2018
4bf8967c
AS
2019 fde->eh_frame_p = eh_frame_p;
2020
b01c8410 2021 add_fde (fde_table, fde);
cfc14b3a
MK
2022 }
2023
2024 return end;
2025}
6896c0c7 2026
8bd90839
FM
2027/* Read a CIE or FDE in BUF and decode it. Entry_type specifies whether we
2028 expect an FDE or a CIE. */
2029
f664829e 2030static const gdb_byte *
4debb237
TT
2031decode_frame_entry (struct gdbarch *gdbarch,
2032 struct comp_unit *unit, const gdb_byte *start,
f664829e 2033 int eh_frame_p,
93878f47 2034 dwarf2_cie_table &cie_table,
a9d65418 2035 dwarf2_fde_table *fde_table,
8bd90839 2036 enum eh_frame_type entry_type)
6896c0c7
RH
2037{
2038 enum { NONE, ALIGN4, ALIGN8, FAIL } workaround = NONE;
f664829e 2039 const gdb_byte *ret;
6896c0c7
RH
2040 ptrdiff_t start_offset;
2041
2042 while (1)
2043 {
4debb237 2044 ret = decode_frame_entry_1 (gdbarch, unit, start, eh_frame_p,
8bd90839 2045 cie_table, fde_table, entry_type);
6896c0c7
RH
2046 if (ret != NULL)
2047 break;
2048
2049 /* We have corrupt input data of some form. */
2050
2051 /* ??? Try, weakly, to work around compiler/assembler/linker bugs
2052 and mismatches wrt padding and alignment of debug sections. */
2053 /* Note that there is no requirement in the standard for any
2054 alignment at all in the frame unwind sections. Testing for
2055 alignment before trying to interpret data would be incorrect.
2056
2057 However, GCC traditionally arranged for frame sections to be
2058 sized such that the FDE length and CIE fields happen to be
2059 aligned (in theory, for performance). This, unfortunately,
2060 was done with .align directives, which had the side effect of
2061 forcing the section to be aligned by the linker.
2062
2063 This becomes a problem when you have some other producer that
2064 creates frame sections that are not as strictly aligned. That
2065 produces a hole in the frame info that gets filled by the
2066 linker with zeros.
2067
2068 The GCC behaviour is arguably a bug, but it's effectively now
2069 part of the ABI, so we're now stuck with it, at least at the
2070 object file level. A smart linker may decide, in the process
2071 of compressing duplicate CIE information, that it can rewrite
2072 the entire output section without this extra padding. */
2073
2074 start_offset = start - unit->dwarf_frame_buffer;
2075 if (workaround < ALIGN4 && (start_offset & 3) != 0)
2076 {
2077 start += 4 - (start_offset & 3);
2078 workaround = ALIGN4;
2079 continue;
2080 }
2081 if (workaround < ALIGN8 && (start_offset & 7) != 0)
2082 {
2083 start += 8 - (start_offset & 7);
2084 workaround = ALIGN8;
2085 continue;
2086 }
2087
2088 /* Nothing left to try. Arrange to return as if we've consumed
2089 the entire input section. Hopefully we'll get valid info from
2090 the other of .debug_frame/.eh_frame. */
2091 workaround = FAIL;
2092 ret = unit->dwarf_frame_buffer + unit->dwarf_frame_size;
2093 break;
2094 }
2095
2096 switch (workaround)
2097 {
2098 case NONE:
2099 break;
2100
2101 case ALIGN4:
b98664d3 2102 complaint (_("\
3e43a32a 2103Corrupt data in %s:%s; align 4 workaround apparently succeeded"),
6896c0c7
RH
2104 unit->dwarf_frame_section->owner->filename,
2105 unit->dwarf_frame_section->name);
2106 break;
2107
2108 case ALIGN8:
b98664d3 2109 complaint (_("\
3e43a32a 2110Corrupt data in %s:%s; align 8 workaround apparently succeeded"),
6896c0c7
RH
2111 unit->dwarf_frame_section->owner->filename,
2112 unit->dwarf_frame_section->name);
2113 break;
2114
2115 default:
b98664d3 2116 complaint (_("Corrupt data in %s:%s"),
6896c0c7
RH
2117 unit->dwarf_frame_section->owner->filename,
2118 unit->dwarf_frame_section->name);
2119 break;
2120 }
2121
2122 return ret;
2123}
cfc14b3a 2124\f
39ef2f62
CB
2125static bool
2126fde_is_less_than (const dwarf2_fde *aa, const dwarf2_fde *bb)
b01c8410 2127{
b01c8410 2128 if (aa->initial_location == bb->initial_location)
e5af178f
PP
2129 {
2130 if (aa->address_range != bb->address_range
2131 && aa->eh_frame_p == 0 && bb->eh_frame_p == 0)
2132 /* Linker bug, e.g. gold/10400.
2133 Work around it by keeping stable sort order. */
39ef2f62 2134 return aa < bb;
e5af178f
PP
2135 else
2136 /* Put eh_frame entries after debug_frame ones. */
39ef2f62 2137 return aa->eh_frame_p < bb->eh_frame_p;
e5af178f 2138 }
b01c8410 2139
39ef2f62 2140 return aa->initial_location < bb->initial_location;
b01c8410
PP
2141}
2142
cfc14b3a
MK
2143void
2144dwarf2_build_frame_info (struct objfile *objfile)
2145{
f664829e 2146 const gdb_byte *frame_ptr;
93878f47 2147 dwarf2_cie_table cie_table;
a9d65418 2148 dwarf2_fde_table fde_table;
cfc14b3a 2149
08feed99 2150 struct gdbarch *gdbarch = objfile->arch ();
4debb237 2151
cfc14b3a 2152 /* Build a minimal decoding of the DWARF2 compilation unit. */
a7a3ae5c 2153 std::unique_ptr<comp_unit> unit (new comp_unit (objfile));
cfc14b3a 2154
d40102a1 2155 if (objfile->separate_debug_objfile_backlink == NULL)
cfc14b3a 2156 {
d40102a1
JB
2157 /* Do not read .eh_frame from separate file as they must be also
2158 present in the main file. */
2159 dwarf2_get_section_info (objfile, DWARF2_EH_FRAME,
2160 &unit->dwarf_frame_section,
2161 &unit->dwarf_frame_buffer,
2162 &unit->dwarf_frame_size);
2163 if (unit->dwarf_frame_size)
b01c8410 2164 {
d40102a1
JB
2165 asection *got, *txt;
2166
2167 /* FIXME: kettenis/20030602: This is the DW_EH_PE_datarel base
2168 that is used for the i386/amd64 target, which currently is
2169 the only target in GCC that supports/uses the
2170 DW_EH_PE_datarel encoding. */
2171 got = bfd_get_section_by_name (unit->abfd, ".got");
2172 if (got)
2173 unit->dbase = got->vma;
2174
2175 /* GCC emits the DW_EH_PE_textrel encoding type on sh and ia64
2176 so far. */
2177 txt = bfd_get_section_by_name (unit->abfd, ".text");
2178 if (txt)
2179 unit->tbase = txt->vma;
2180
a70b8144 2181 try
8bd90839
FM
2182 {
2183 frame_ptr = unit->dwarf_frame_buffer;
2184 while (frame_ptr < unit->dwarf_frame_buffer + unit->dwarf_frame_size)
4debb237
TT
2185 frame_ptr = decode_frame_entry (gdbarch, unit.get (),
2186 frame_ptr, 1,
93878f47 2187 cie_table, &fde_table,
8bd90839
FM
2188 EH_CIE_OR_FDE_TYPE_ID);
2189 }
2190
230d2906 2191 catch (const gdb_exception_error &e)
8bd90839
FM
2192 {
2193 warning (_("skipping .eh_frame info of %s: %s"),
3d6e9d23 2194 objfile_name (objfile), e.what ());
8bd90839 2195
a9d65418 2196 fde_table.clear ();
93878f47 2197 /* The cie_table is discarded below. */
8bd90839 2198 }
d40102a1 2199
93878f47 2200 cie_table.clear ();
b01c8410 2201 }
cfc14b3a
MK
2202 }
2203
3017a003 2204 dwarf2_get_section_info (objfile, DWARF2_DEBUG_FRAME,
dce234bc
PP
2205 &unit->dwarf_frame_section,
2206 &unit->dwarf_frame_buffer,
2207 &unit->dwarf_frame_size);
2208 if (unit->dwarf_frame_size)
cfc14b3a 2209 {
a9d65418 2210 size_t num_old_fde_entries = fde_table.size ();
8bd90839 2211
a70b8144 2212 try
8bd90839
FM
2213 {
2214 frame_ptr = unit->dwarf_frame_buffer;
2215 while (frame_ptr < unit->dwarf_frame_buffer + unit->dwarf_frame_size)
4debb237 2216 frame_ptr = decode_frame_entry (gdbarch, unit.get (), frame_ptr, 0,
93878f47 2217 cie_table, &fde_table,
8bd90839
FM
2218 EH_CIE_OR_FDE_TYPE_ID);
2219 }
230d2906 2220 catch (const gdb_exception_error &e)
8bd90839
FM
2221 {
2222 warning (_("skipping .debug_frame info of %s: %s"),
3d6e9d23 2223 objfile_name (objfile), e.what ());
8bd90839 2224
a9d65418 2225 fde_table.resize (num_old_fde_entries);
8bd90839 2226 }
b01c8410
PP
2227 }
2228
a9d65418
TT
2229 struct dwarf2_fde *fde_prev = NULL;
2230 struct dwarf2_fde *first_non_zero_fde = NULL;
b01c8410 2231
a9d65418
TT
2232 /* Prepare FDE table for lookups. */
2233 std::sort (fde_table.begin (), fde_table.end (), fde_is_less_than);
875cdfbb 2234
a9d65418
TT
2235 /* Check for leftovers from --gc-sections. The GNU linker sets
2236 the relevant symbols to zero, but doesn't zero the FDE *end*
2237 ranges because there's no relocation there. It's (offset,
2238 length), not (start, end). On targets where address zero is
2239 just another valid address this can be a problem, since the
2240 FDEs appear to be non-empty in the output --- we could pick
2241 out the wrong FDE. To work around this, when overlaps are
2242 detected, we prefer FDEs that do not start at zero.
2243
2244 Start by finding the first FDE with non-zero start. Below
2245 we'll discard all FDEs that start at zero and overlap this
2246 one. */
2247 for (struct dwarf2_fde *fde : fde_table)
2248 {
2249 if (fde->initial_location != 0)
875cdfbb 2250 {
a9d65418
TT
2251 first_non_zero_fde = fde;
2252 break;
875cdfbb 2253 }
a9d65418
TT
2254 }
2255
2256 /* Since we'll be doing bsearch, squeeze out identical (except
2257 for eh_frame_p) fde entries so bsearch result is predictable.
2258 Also discard leftovers from --gc-sections. */
2259 for (struct dwarf2_fde *fde : fde_table)
2260 {
2261 if (fde->initial_location == 0
2262 && first_non_zero_fde != NULL
2263 && (first_non_zero_fde->initial_location
2264 < fde->initial_location + fde->address_range))
2265 continue;
2266
2267 if (fde_prev != NULL
2268 && fde_prev->initial_location == fde->initial_location)
2269 continue;
b01c8410 2270
a7a3ae5c 2271 unit->fde_table.push_back (fde);
a9d65418 2272 fde_prev = fde;
cfc14b3a 2273 }
a7a3ae5c 2274 unit->fde_table.shrink_to_fit ();
be391dca 2275
3d4560f7 2276 set_comp_unit (objfile, unit.release ());
cfc14b3a 2277}
0d0e1a63 2278
3c3bb058
AB
2279/* Handle 'maintenance show dwarf unwinders'. */
2280
2281static void
2282show_dwarf_unwinders_enabled_p (struct ui_file *file, int from_tty,
2283 struct cmd_list_element *c,
2284 const char *value)
2285{
2286 fprintf_filtered (file,
2287 _("The DWARF stack unwinders are currently %s.\n"),
2288 value);
2289}
2290
6c265988 2291void _initialize_dwarf2_frame ();
0d0e1a63 2292void
6c265988 2293_initialize_dwarf2_frame ()
0d0e1a63 2294{
030f20e1 2295 dwarf2_frame_data = gdbarch_data_register_pre_init (dwarf2_frame_init);
1c90d9f0 2296
3c3bb058
AB
2297 add_setshow_boolean_cmd ("unwinders", class_obscure,
2298 &dwarf2_frame_unwinders_enabled_p , _("\
2299Set whether the DWARF stack frame unwinders are used."), _("\
2300Show whether the DWARF stack frame unwinders are used."), _("\
2301When enabled the DWARF stack frame unwinders can be used for architectures\n\
2302that support the DWARF unwinders. Enabling the DWARF unwinders for an\n\
2303architecture that doesn't support them will have no effect."),
2304 NULL,
2305 show_dwarf_unwinders_enabled_p,
2306 &set_dwarf_cmdlist,
2307 &show_dwarf_cmdlist);
2308
1c90d9f0 2309#if GDB_SELF_TEST
1526853e
SM
2310 selftests::register_test_foreach_arch ("execute_cfa_program",
2311 selftests::execute_cfa_program_test);
1c90d9f0 2312#endif
0d0e1a63 2313}
This page took 1.74554 seconds and 4 git commands to generate.