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