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