Add per-unit obstack
[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
f664829e
DE
1704static const gdb_byte *decode_frame_entry (struct comp_unit *unit,
1705 const gdb_byte *start,
1706 int eh_frame_p,
93878f47 1707 dwarf2_cie_table &cie_table,
a9d65418 1708 dwarf2_fde_table *fde_table,
f664829e 1709 enum eh_frame_type entry_type);
8bd90839
FM
1710
1711/* Decode the next CIE or FDE, entry_type specifies the expected type.
1712 Return NULL if invalid input, otherwise the next byte to be processed. */
cfc14b3a 1713
f664829e
DE
1714static const gdb_byte *
1715decode_frame_entry_1 (struct comp_unit *unit, const gdb_byte *start,
1716 int eh_frame_p,
93878f47 1717 dwarf2_cie_table &cie_table,
a9d65418 1718 dwarf2_fde_table *fde_table,
8bd90839 1719 enum eh_frame_type entry_type)
cfc14b3a 1720{
5e2b427d 1721 struct gdbarch *gdbarch = get_objfile_arch (unit->objfile);
f664829e 1722 const gdb_byte *buf, *end;
723adb65 1723 ULONGEST length;
cfc14b3a 1724 unsigned int bytes_read;
6896c0c7
RH
1725 int dwarf64_p;
1726 ULONGEST cie_id;
cfc14b3a 1727 ULONGEST cie_pointer;
9fccedf7
DE
1728 int64_t sleb128;
1729 uint64_t uleb128;
cfc14b3a 1730
6896c0c7 1731 buf = start;
4075cb26 1732 length = read_initial_length (unit->abfd, buf, &bytes_read, false);
cfc14b3a 1733 buf += bytes_read;
723adb65 1734 end = buf + (size_t) length;
6896c0c7 1735
cfc14b3a
MK
1736 if (length == 0)
1737 return end;
1738
723adb65
SL
1739 /* Are we still within the section? */
1740 if (end <= buf || end > unit->dwarf_frame_buffer + unit->dwarf_frame_size)
1741 return NULL;
1742
6896c0c7
RH
1743 /* Distinguish between 32 and 64-bit encoded frame info. */
1744 dwarf64_p = (bytes_read == 12);
cfc14b3a 1745
6896c0c7 1746 /* In a .eh_frame section, zero is used to distinguish CIEs from FDEs. */
cfc14b3a
MK
1747 if (eh_frame_p)
1748 cie_id = 0;
1749 else if (dwarf64_p)
1750 cie_id = DW64_CIE_ID;
6896c0c7
RH
1751 else
1752 cie_id = DW_CIE_ID;
cfc14b3a
MK
1753
1754 if (dwarf64_p)
1755 {
1756 cie_pointer = read_8_bytes (unit->abfd, buf);
1757 buf += 8;
1758 }
1759 else
1760 {
1761 cie_pointer = read_4_bytes (unit->abfd, buf);
1762 buf += 4;
1763 }
1764
1765 if (cie_pointer == cie_id)
1766 {
1767 /* This is a CIE. */
1768 struct dwarf2_cie *cie;
1769 char *augmentation;
28ba0b33 1770 unsigned int cie_version;
cfc14b3a 1771
8bd90839
FM
1772 /* Check that a CIE was expected. */
1773 if ((entry_type & EH_CIE_TYPE_ID) == 0)
1774 error (_("Found a CIE when not expecting it."));
1775
cfc14b3a
MK
1776 /* Record the offset into the .debug_frame section of this CIE. */
1777 cie_pointer = start - unit->dwarf_frame_buffer;
1778
1779 /* Check whether we've already read it. */
b01c8410 1780 if (find_cie (cie_table, cie_pointer))
cfc14b3a
MK
1781 return end;
1782
0d404d44 1783 cie = XOBNEW (&unit->obstack, struct dwarf2_cie);
cfc14b3a
MK
1784 cie->initial_instructions = NULL;
1785 cie->cie_pointer = cie_pointer;
1786
1787 /* The encoding for FDE's in a normal .debug_frame section
32b05c07
MK
1788 depends on the target address size. */
1789 cie->encoding = DW_EH_PE_absptr;
cfc14b3a 1790
56c987f6
AO
1791 /* We'll determine the final value later, but we need to
1792 initialize it conservatively. */
1793 cie->signal_frame = 0;
1794
cfc14b3a 1795 /* Check version number. */
28ba0b33 1796 cie_version = read_1_byte (unit->abfd, buf);
2dc7f7b3 1797 if (cie_version != 1 && cie_version != 3 && cie_version != 4)
6896c0c7 1798 return NULL;
303b6f5d 1799 cie->version = cie_version;
cfc14b3a
MK
1800 buf += 1;
1801
1802 /* Interpret the interesting bits of the augmentation. */
303b6f5d 1803 cie->augmentation = augmentation = (char *) buf;
852483bc 1804 buf += (strlen (augmentation) + 1);
cfc14b3a 1805
303b6f5d
DJ
1806 /* Ignore armcc augmentations. We only use them for quirks,
1807 and that doesn't happen until later. */
61012eef 1808 if (startswith (augmentation, "armcc"))
303b6f5d
DJ
1809 augmentation += strlen (augmentation);
1810
cfc14b3a
MK
1811 /* The GCC 2.x "eh" augmentation has a pointer immediately
1812 following the augmentation string, so it must be handled
1813 first. */
1814 if (augmentation[0] == 'e' && augmentation[1] == 'h')
1815 {
1816 /* Skip. */
5e2b427d 1817 buf += gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT;
cfc14b3a
MK
1818 augmentation += 2;
1819 }
1820
2dc7f7b3
TT
1821 if (cie->version >= 4)
1822 {
1823 /* FIXME: check that this is the same as from the CU header. */
1824 cie->addr_size = read_1_byte (unit->abfd, buf);
1825 ++buf;
1826 cie->segment_size = read_1_byte (unit->abfd, buf);
1827 ++buf;
1828 }
1829 else
1830 {
8da614df 1831 cie->addr_size = gdbarch_dwarf2_addr_size (gdbarch);
2dc7f7b3
TT
1832 cie->segment_size = 0;
1833 }
8da614df
CV
1834 /* Address values in .eh_frame sections are defined to have the
1835 target's pointer size. Watchout: This breaks frame info for
1836 targets with pointer size < address size, unless a .debug_frame
0963b4bd 1837 section exists as well. */
8da614df
CV
1838 if (eh_frame_p)
1839 cie->ptr_size = gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT;
1840 else
1841 cie->ptr_size = cie->addr_size;
2dc7f7b3 1842
f664829e
DE
1843 buf = gdb_read_uleb128 (buf, end, &uleb128);
1844 if (buf == NULL)
1845 return NULL;
1846 cie->code_alignment_factor = uleb128;
cfc14b3a 1847
f664829e
DE
1848 buf = gdb_read_sleb128 (buf, end, &sleb128);
1849 if (buf == NULL)
1850 return NULL;
1851 cie->data_alignment_factor = sleb128;
cfc14b3a 1852
28ba0b33
PB
1853 if (cie_version == 1)
1854 {
1855 cie->return_address_register = read_1_byte (unit->abfd, buf);
f664829e 1856 ++buf;
28ba0b33
PB
1857 }
1858 else
f664829e
DE
1859 {
1860 buf = gdb_read_uleb128 (buf, end, &uleb128);
1861 if (buf == NULL)
1862 return NULL;
1863 cie->return_address_register = uleb128;
1864 }
1865
4fc771b8 1866 cie->return_address_register
5e2b427d 1867 = dwarf2_frame_adjust_regnum (gdbarch,
4fc771b8
DJ
1868 cie->return_address_register,
1869 eh_frame_p);
4bf8967c 1870
7131cb6e
RH
1871 cie->saw_z_augmentation = (*augmentation == 'z');
1872 if (cie->saw_z_augmentation)
cfc14b3a 1873 {
b926417a 1874 uint64_t uleb_length;
cfc14b3a 1875
b926417a 1876 buf = gdb_read_uleb128 (buf, end, &uleb_length);
f664829e 1877 if (buf == NULL)
6896c0c7 1878 return NULL;
b926417a 1879 cie->initial_instructions = buf + uleb_length;
cfc14b3a
MK
1880 augmentation++;
1881 }
1882
1883 while (*augmentation)
1884 {
1885 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
1886 if (*augmentation == 'L')
1887 {
1888 /* Skip. */
1889 buf++;
1890 augmentation++;
1891 }
1892
1893 /* "R" indicates a byte indicating how FDE addresses are encoded. */
1894 else if (*augmentation == 'R')
1895 {
1896 cie->encoding = *buf++;
1897 augmentation++;
1898 }
1899
1900 /* "P" indicates a personality routine in the CIE augmentation. */
1901 else if (*augmentation == 'P')
1902 {
1234d960 1903 /* Skip. Avoid indirection since we throw away the result. */
852483bc 1904 gdb_byte encoding = (*buf++) & ~DW_EH_PE_indirect;
8da614df 1905 read_encoded_value (unit, encoding, cie->ptr_size,
ae0d2f24 1906 buf, &bytes_read, 0);
f724bf08 1907 buf += bytes_read;
cfc14b3a
MK
1908 augmentation++;
1909 }
1910
56c987f6
AO
1911 /* "S" indicates a signal frame, such that the return
1912 address must not be decremented to locate the call frame
1913 info for the previous frame; it might even be the first
1914 instruction of a function, so decrementing it would take
1915 us to a different function. */
1916 else if (*augmentation == 'S')
1917 {
1918 cie->signal_frame = 1;
1919 augmentation++;
1920 }
1921
3e9a2e52
DJ
1922 /* Otherwise we have an unknown augmentation. Assume that either
1923 there is no augmentation data, or we saw a 'z' prefix. */
cfc14b3a
MK
1924 else
1925 {
3e9a2e52
DJ
1926 if (cie->initial_instructions)
1927 buf = cie->initial_instructions;
cfc14b3a
MK
1928 break;
1929 }
1930 }
1931
1932 cie->initial_instructions = buf;
1933 cie->end = end;
b01c8410 1934 cie->unit = unit;
cfc14b3a 1935
93878f47 1936 cie_table[cie->cie_pointer] = cie;
cfc14b3a
MK
1937 }
1938 else
1939 {
1940 /* This is a FDE. */
1941 struct dwarf2_fde *fde;
3e29f34a 1942 CORE_ADDR addr;
cfc14b3a 1943
8bd90839
FM
1944 /* Check that an FDE was expected. */
1945 if ((entry_type & EH_FDE_TYPE_ID) == 0)
1946 error (_("Found an FDE when not expecting it."));
1947
6896c0c7
RH
1948 /* In an .eh_frame section, the CIE pointer is the delta between the
1949 address within the FDE where the CIE pointer is stored and the
1950 address of the CIE. Convert it to an offset into the .eh_frame
1951 section. */
cfc14b3a
MK
1952 if (eh_frame_p)
1953 {
cfc14b3a
MK
1954 cie_pointer = buf - unit->dwarf_frame_buffer - cie_pointer;
1955 cie_pointer -= (dwarf64_p ? 8 : 4);
1956 }
1957
6896c0c7
RH
1958 /* In either case, validate the result is still within the section. */
1959 if (cie_pointer >= unit->dwarf_frame_size)
1960 return NULL;
1961
0d404d44 1962 fde = XOBNEW (&unit->obstack, struct dwarf2_fde);
b01c8410 1963 fde->cie = find_cie (cie_table, cie_pointer);
cfc14b3a
MK
1964 if (fde->cie == NULL)
1965 {
1966 decode_frame_entry (unit, unit->dwarf_frame_buffer + cie_pointer,
8bd90839
FM
1967 eh_frame_p, cie_table, fde_table,
1968 EH_CIE_TYPE_ID);
b01c8410 1969 fde->cie = find_cie (cie_table, cie_pointer);
cfc14b3a
MK
1970 }
1971
1972 gdb_assert (fde->cie != NULL);
1973
3e29f34a
MR
1974 addr = read_encoded_value (unit, fde->cie->encoding, fde->cie->ptr_size,
1975 buf, &bytes_read, 0);
1976 fde->initial_location = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
cfc14b3a
MK
1977 buf += bytes_read;
1978
1979 fde->address_range =
ae0d2f24 1980 read_encoded_value (unit, fde->cie->encoding & 0x0f,
8da614df 1981 fde->cie->ptr_size, buf, &bytes_read, 0);
3e29f34a
MR
1982 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + fde->address_range);
1983 fde->address_range = addr - fde->initial_location;
cfc14b3a
MK
1984 buf += bytes_read;
1985
7131cb6e
RH
1986 /* A 'z' augmentation in the CIE implies the presence of an
1987 augmentation field in the FDE as well. The only thing known
1988 to be in here at present is the LSDA entry for EH. So we
1989 can skip the whole thing. */
1990 if (fde->cie->saw_z_augmentation)
1991 {
b926417a 1992 uint64_t uleb_length;
7131cb6e 1993
b926417a 1994 buf = gdb_read_uleb128 (buf, end, &uleb_length);
f664829e
DE
1995 if (buf == NULL)
1996 return NULL;
b926417a 1997 buf += uleb_length;
6896c0c7
RH
1998 if (buf > end)
1999 return NULL;
7131cb6e
RH
2000 }
2001
cfc14b3a
MK
2002 fde->instructions = buf;
2003 fde->end = end;
2004
4bf8967c
AS
2005 fde->eh_frame_p = eh_frame_p;
2006
b01c8410 2007 add_fde (fde_table, fde);
cfc14b3a
MK
2008 }
2009
2010 return end;
2011}
6896c0c7 2012
8bd90839
FM
2013/* Read a CIE or FDE in BUF and decode it. Entry_type specifies whether we
2014 expect an FDE or a CIE. */
2015
f664829e
DE
2016static const gdb_byte *
2017decode_frame_entry (struct comp_unit *unit, const gdb_byte *start,
2018 int eh_frame_p,
93878f47 2019 dwarf2_cie_table &cie_table,
a9d65418 2020 dwarf2_fde_table *fde_table,
8bd90839 2021 enum eh_frame_type entry_type)
6896c0c7
RH
2022{
2023 enum { NONE, ALIGN4, ALIGN8, FAIL } workaround = NONE;
f664829e 2024 const gdb_byte *ret;
6896c0c7
RH
2025 ptrdiff_t start_offset;
2026
2027 while (1)
2028 {
b01c8410 2029 ret = decode_frame_entry_1 (unit, start, eh_frame_p,
8bd90839 2030 cie_table, fde_table, entry_type);
6896c0c7
RH
2031 if (ret != NULL)
2032 break;
2033
2034 /* We have corrupt input data of some form. */
2035
2036 /* ??? Try, weakly, to work around compiler/assembler/linker bugs
2037 and mismatches wrt padding and alignment of debug sections. */
2038 /* Note that there is no requirement in the standard for any
2039 alignment at all in the frame unwind sections. Testing for
2040 alignment before trying to interpret data would be incorrect.
2041
2042 However, GCC traditionally arranged for frame sections to be
2043 sized such that the FDE length and CIE fields happen to be
2044 aligned (in theory, for performance). This, unfortunately,
2045 was done with .align directives, which had the side effect of
2046 forcing the section to be aligned by the linker.
2047
2048 This becomes a problem when you have some other producer that
2049 creates frame sections that are not as strictly aligned. That
2050 produces a hole in the frame info that gets filled by the
2051 linker with zeros.
2052
2053 The GCC behaviour is arguably a bug, but it's effectively now
2054 part of the ABI, so we're now stuck with it, at least at the
2055 object file level. A smart linker may decide, in the process
2056 of compressing duplicate CIE information, that it can rewrite
2057 the entire output section without this extra padding. */
2058
2059 start_offset = start - unit->dwarf_frame_buffer;
2060 if (workaround < ALIGN4 && (start_offset & 3) != 0)
2061 {
2062 start += 4 - (start_offset & 3);
2063 workaround = ALIGN4;
2064 continue;
2065 }
2066 if (workaround < ALIGN8 && (start_offset & 7) != 0)
2067 {
2068 start += 8 - (start_offset & 7);
2069 workaround = ALIGN8;
2070 continue;
2071 }
2072
2073 /* Nothing left to try. Arrange to return as if we've consumed
2074 the entire input section. Hopefully we'll get valid info from
2075 the other of .debug_frame/.eh_frame. */
2076 workaround = FAIL;
2077 ret = unit->dwarf_frame_buffer + unit->dwarf_frame_size;
2078 break;
2079 }
2080
2081 switch (workaround)
2082 {
2083 case NONE:
2084 break;
2085
2086 case ALIGN4:
b98664d3 2087 complaint (_("\
3e43a32a 2088Corrupt data in %s:%s; align 4 workaround apparently succeeded"),
6896c0c7
RH
2089 unit->dwarf_frame_section->owner->filename,
2090 unit->dwarf_frame_section->name);
2091 break;
2092
2093 case ALIGN8:
b98664d3 2094 complaint (_("\
3e43a32a 2095Corrupt data in %s:%s; align 8 workaround apparently succeeded"),
6896c0c7
RH
2096 unit->dwarf_frame_section->owner->filename,
2097 unit->dwarf_frame_section->name);
2098 break;
2099
2100 default:
b98664d3 2101 complaint (_("Corrupt data in %s:%s"),
6896c0c7
RH
2102 unit->dwarf_frame_section->owner->filename,
2103 unit->dwarf_frame_section->name);
2104 break;
2105 }
2106
2107 return ret;
2108}
cfc14b3a 2109\f
39ef2f62
CB
2110static bool
2111fde_is_less_than (const dwarf2_fde *aa, const dwarf2_fde *bb)
b01c8410 2112{
b01c8410 2113 if (aa->initial_location == bb->initial_location)
e5af178f
PP
2114 {
2115 if (aa->address_range != bb->address_range
2116 && aa->eh_frame_p == 0 && bb->eh_frame_p == 0)
2117 /* Linker bug, e.g. gold/10400.
2118 Work around it by keeping stable sort order. */
39ef2f62 2119 return aa < bb;
e5af178f
PP
2120 else
2121 /* Put eh_frame entries after debug_frame ones. */
39ef2f62 2122 return aa->eh_frame_p < bb->eh_frame_p;
e5af178f 2123 }
b01c8410 2124
39ef2f62 2125 return aa->initial_location < bb->initial_location;
b01c8410
PP
2126}
2127
cfc14b3a
MK
2128void
2129dwarf2_build_frame_info (struct objfile *objfile)
2130{
f664829e 2131 const gdb_byte *frame_ptr;
93878f47 2132 dwarf2_cie_table cie_table;
a9d65418 2133 dwarf2_fde_table fde_table;
cfc14b3a
MK
2134
2135 /* Build a minimal decoding of the DWARF2 compilation unit. */
a7a3ae5c 2136 std::unique_ptr<comp_unit> unit (new comp_unit (objfile));
cfc14b3a 2137
d40102a1 2138 if (objfile->separate_debug_objfile_backlink == NULL)
cfc14b3a 2139 {
d40102a1
JB
2140 /* Do not read .eh_frame from separate file as they must be also
2141 present in the main file. */
2142 dwarf2_get_section_info (objfile, DWARF2_EH_FRAME,
2143 &unit->dwarf_frame_section,
2144 &unit->dwarf_frame_buffer,
2145 &unit->dwarf_frame_size);
2146 if (unit->dwarf_frame_size)
b01c8410 2147 {
d40102a1
JB
2148 asection *got, *txt;
2149
2150 /* FIXME: kettenis/20030602: This is the DW_EH_PE_datarel base
2151 that is used for the i386/amd64 target, which currently is
2152 the only target in GCC that supports/uses the
2153 DW_EH_PE_datarel encoding. */
2154 got = bfd_get_section_by_name (unit->abfd, ".got");
2155 if (got)
2156 unit->dbase = got->vma;
2157
2158 /* GCC emits the DW_EH_PE_textrel encoding type on sh and ia64
2159 so far. */
2160 txt = bfd_get_section_by_name (unit->abfd, ".text");
2161 if (txt)
2162 unit->tbase = txt->vma;
2163
a70b8144 2164 try
8bd90839
FM
2165 {
2166 frame_ptr = unit->dwarf_frame_buffer;
2167 while (frame_ptr < unit->dwarf_frame_buffer + unit->dwarf_frame_size)
a7a3ae5c 2168 frame_ptr = decode_frame_entry (unit.get (), frame_ptr, 1,
93878f47 2169 cie_table, &fde_table,
8bd90839
FM
2170 EH_CIE_OR_FDE_TYPE_ID);
2171 }
2172
230d2906 2173 catch (const gdb_exception_error &e)
8bd90839
FM
2174 {
2175 warning (_("skipping .eh_frame info of %s: %s"),
3d6e9d23 2176 objfile_name (objfile), e.what ());
8bd90839 2177
a9d65418 2178 fde_table.clear ();
93878f47 2179 /* The cie_table is discarded below. */
8bd90839 2180 }
d40102a1 2181
93878f47 2182 cie_table.clear ();
b01c8410 2183 }
cfc14b3a
MK
2184 }
2185
3017a003 2186 dwarf2_get_section_info (objfile, DWARF2_DEBUG_FRAME,
dce234bc
PP
2187 &unit->dwarf_frame_section,
2188 &unit->dwarf_frame_buffer,
2189 &unit->dwarf_frame_size);
2190 if (unit->dwarf_frame_size)
cfc14b3a 2191 {
a9d65418 2192 size_t num_old_fde_entries = fde_table.size ();
8bd90839 2193
a70b8144 2194 try
8bd90839
FM
2195 {
2196 frame_ptr = unit->dwarf_frame_buffer;
2197 while (frame_ptr < unit->dwarf_frame_buffer + unit->dwarf_frame_size)
a7a3ae5c 2198 frame_ptr = decode_frame_entry (unit.get (), frame_ptr, 0,
93878f47 2199 cie_table, &fde_table,
8bd90839
FM
2200 EH_CIE_OR_FDE_TYPE_ID);
2201 }
230d2906 2202 catch (const gdb_exception_error &e)
8bd90839
FM
2203 {
2204 warning (_("skipping .debug_frame info of %s: %s"),
3d6e9d23 2205 objfile_name (objfile), e.what ());
8bd90839 2206
a9d65418 2207 fde_table.resize (num_old_fde_entries);
8bd90839 2208 }
b01c8410
PP
2209 }
2210
a9d65418
TT
2211 struct dwarf2_fde *fde_prev = NULL;
2212 struct dwarf2_fde *first_non_zero_fde = NULL;
b01c8410 2213
a9d65418
TT
2214 /* Prepare FDE table for lookups. */
2215 std::sort (fde_table.begin (), fde_table.end (), fde_is_less_than);
875cdfbb 2216
a9d65418
TT
2217 /* Check for leftovers from --gc-sections. The GNU linker sets
2218 the relevant symbols to zero, but doesn't zero the FDE *end*
2219 ranges because there's no relocation there. It's (offset,
2220 length), not (start, end). On targets where address zero is
2221 just another valid address this can be a problem, since the
2222 FDEs appear to be non-empty in the output --- we could pick
2223 out the wrong FDE. To work around this, when overlaps are
2224 detected, we prefer FDEs that do not start at zero.
2225
2226 Start by finding the first FDE with non-zero start. Below
2227 we'll discard all FDEs that start at zero and overlap this
2228 one. */
2229 for (struct dwarf2_fde *fde : fde_table)
2230 {
2231 if (fde->initial_location != 0)
875cdfbb 2232 {
a9d65418
TT
2233 first_non_zero_fde = fde;
2234 break;
875cdfbb 2235 }
a9d65418
TT
2236 }
2237
2238 /* Since we'll be doing bsearch, squeeze out identical (except
2239 for eh_frame_p) fde entries so bsearch result is predictable.
2240 Also discard leftovers from --gc-sections. */
2241 for (struct dwarf2_fde *fde : fde_table)
2242 {
2243 if (fde->initial_location == 0
2244 && first_non_zero_fde != NULL
2245 && (first_non_zero_fde->initial_location
2246 < fde->initial_location + fde->address_range))
2247 continue;
2248
2249 if (fde_prev != NULL
2250 && fde_prev->initial_location == fde->initial_location)
2251 continue;
b01c8410 2252
a7a3ae5c 2253 unit->fde_table.push_back (fde);
a9d65418 2254 fde_prev = fde;
cfc14b3a 2255 }
a7a3ae5c 2256 unit->fde_table.shrink_to_fit ();
be391dca 2257
a7a3ae5c 2258 dwarf2_frame_objfile_data.set (objfile, unit.release ());
cfc14b3a 2259}
0d0e1a63 2260
3c3bb058
AB
2261/* Handle 'maintenance show dwarf unwinders'. */
2262
2263static void
2264show_dwarf_unwinders_enabled_p (struct ui_file *file, int from_tty,
2265 struct cmd_list_element *c,
2266 const char *value)
2267{
2268 fprintf_filtered (file,
2269 _("The DWARF stack unwinders are currently %s.\n"),
2270 value);
2271}
2272
6c265988 2273void _initialize_dwarf2_frame ();
0d0e1a63 2274void
6c265988 2275_initialize_dwarf2_frame ()
0d0e1a63 2276{
030f20e1 2277 dwarf2_frame_data = gdbarch_data_register_pre_init (dwarf2_frame_init);
1c90d9f0 2278
3c3bb058
AB
2279 add_setshow_boolean_cmd ("unwinders", class_obscure,
2280 &dwarf2_frame_unwinders_enabled_p , _("\
2281Set whether the DWARF stack frame unwinders are used."), _("\
2282Show whether the DWARF stack frame unwinders are used."), _("\
2283When enabled the DWARF stack frame unwinders can be used for architectures\n\
2284that support the DWARF unwinders. Enabling the DWARF unwinders for an\n\
2285architecture that doesn't support them will have no effect."),
2286 NULL,
2287 show_dwarf_unwinders_enabled_p,
2288 &set_dwarf_cmdlist,
2289 &show_dwarf_cmdlist);
2290
1c90d9f0 2291#if GDB_SELF_TEST
1526853e
SM
2292 selftests::register_test_foreach_arch ("execute_cfa_program",
2293 selftests::execute_cfa_program_test);
1c90d9f0 2294#endif
0d0e1a63 2295}
This page took 1.290242 seconds and 4 git commands to generate.