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