2009-09-02 Tristan Gingold <gingold@adacore.com>
[deliverable/binutils-gdb.git] / gdb / dwarf2-frame.c
CommitLineData
cfc14b3a
MK
1/* Frame unwinder for frames with DWARF Call Frame Information.
2
0fb0cc75
JB
3 Copyright (C) 2003, 2004, 2005, 2007, 2008, 2009
4 Free Software Foundation, Inc.
cfc14b3a
MK
5
6 Contributed by Mark Kettenis.
7
8 This file is part of GDB.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
a9762ec7 12 the Free Software Foundation; either version 3 of the License, or
cfc14b3a
MK
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
a9762ec7 21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
cfc14b3a
MK
22
23#include "defs.h"
24#include "dwarf2expr.h"
fa8f86ff 25#include "dwarf2.h"
cfc14b3a
MK
26#include "frame.h"
27#include "frame-base.h"
28#include "frame-unwind.h"
29#include "gdbcore.h"
30#include "gdbtypes.h"
31#include "symtab.h"
32#include "objfiles.h"
33#include "regcache.h"
f2da6b3a 34#include "value.h"
cfc14b3a
MK
35
36#include "gdb_assert.h"
37#include "gdb_string.h"
38
6896c0c7 39#include "complaints.h"
cfc14b3a
MK
40#include "dwarf2-frame.h"
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. */
852483bc
MK
68 gdb_byte *initial_instructions;
69 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
7131cb6e
RH
80 /* True if a 'z' augmentation existed. */
81 unsigned char saw_z_augmentation;
82
56c987f6
AO
83 /* True if an 'S' augmentation existed. */
84 unsigned char signal_frame;
85
303b6f5d
DJ
86 /* The version recorded in the CIE. */
87 unsigned char version;
b01c8410 88};
303b6f5d 89
b01c8410
PP
90struct dwarf2_cie_table
91{
92 int num_entries;
93 struct dwarf2_cie **entries;
cfc14b3a
MK
94};
95
96/* Frame Description Entry (FDE). */
97
98struct dwarf2_fde
99{
100 /* CIE for this FDE. */
101 struct dwarf2_cie *cie;
102
103 /* First location associated with this FDE. */
104 CORE_ADDR initial_location;
105
106 /* Number of bytes of program instructions described by this FDE. */
107 CORE_ADDR address_range;
108
109 /* Instruction sequence. */
852483bc
MK
110 gdb_byte *instructions;
111 gdb_byte *end;
cfc14b3a 112
4bf8967c
AS
113 /* True if this FDE is read from a .eh_frame instead of a .debug_frame
114 section. */
115 unsigned char eh_frame_p;
b01c8410 116};
4bf8967c 117
b01c8410
PP
118struct dwarf2_fde_table
119{
120 int num_entries;
121 struct dwarf2_fde **entries;
cfc14b3a
MK
122};
123
ae0d2f24
UW
124/* A minimal decoding of DWARF2 compilation units. We only decode
125 what's needed to get to the call frame information. */
126
127struct comp_unit
128{
129 /* Keep the bfd convenient. */
130 bfd *abfd;
131
132 struct objfile *objfile;
133
ae0d2f24
UW
134 /* Pointer to the .debug_frame section loaded into memory. */
135 gdb_byte *dwarf_frame_buffer;
136
137 /* Length of the loaded .debug_frame section. */
c098b58b 138 bfd_size_type dwarf_frame_size;
ae0d2f24
UW
139
140 /* Pointer to the .debug_frame section. */
141 asection *dwarf_frame_section;
142
143 /* Base for DW_EH_PE_datarel encodings. */
144 bfd_vma dbase;
145
146 /* Base for DW_EH_PE_textrel encodings. */
147 bfd_vma tbase;
148};
149
cfc14b3a 150static struct dwarf2_fde *dwarf2_frame_find_fde (CORE_ADDR *pc);
4fc771b8
DJ
151
152static int dwarf2_frame_adjust_regnum (struct gdbarch *gdbarch, int regnum,
153 int eh_frame_p);
ae0d2f24
UW
154
155static CORE_ADDR read_encoded_value (struct comp_unit *unit, gdb_byte encoding,
156 int ptr_len, gdb_byte *buf,
157 unsigned int *bytes_read_ptr,
158 CORE_ADDR func_base);
cfc14b3a
MK
159\f
160
161/* Structure describing a frame state. */
162
163struct dwarf2_frame_state
164{
165 /* Each register save state can be described in terms of a CFA slot,
166 another register, or a location expression. */
167 struct dwarf2_frame_state_reg_info
168 {
05cbe71a 169 struct dwarf2_frame_state_reg *reg;
cfc14b3a
MK
170 int num_regs;
171
2fd481e1
PP
172 LONGEST cfa_offset;
173 ULONGEST cfa_reg;
174 enum {
175 CFA_UNSET,
176 CFA_REG_OFFSET,
177 CFA_EXP
178 } cfa_how;
179 gdb_byte *cfa_exp;
180
cfc14b3a
MK
181 /* Used to implement DW_CFA_remember_state. */
182 struct dwarf2_frame_state_reg_info *prev;
183 } regs;
184
cfc14b3a
MK
185 /* The PC described by the current frame state. */
186 CORE_ADDR pc;
187
188 /* Initial register set from the CIE.
189 Used to implement DW_CFA_restore. */
190 struct dwarf2_frame_state_reg_info initial;
191
192 /* The information we care about from the CIE. */
193 LONGEST data_align;
194 ULONGEST code_align;
195 ULONGEST retaddr_column;
303b6f5d
DJ
196
197 /* Flags for known producer quirks. */
198
199 /* The ARM compilers, in DWARF2 mode, assume that DW_CFA_def_cfa
200 and DW_CFA_def_cfa_offset takes a factored offset. */
201 int armcc_cfa_offsets_sf;
202
203 /* The ARM compilers, in DWARF2 or DWARF3 mode, may assume that
204 the CFA is defined as REG - OFFSET rather than REG + OFFSET. */
205 int armcc_cfa_offsets_reversed;
cfc14b3a
MK
206};
207
208/* Store the length the expression for the CFA in the `cfa_reg' field,
209 which is unused in that case. */
210#define cfa_exp_len cfa_reg
211
f57d151a 212/* Assert that the register set RS is large enough to store gdbarch_num_regs
cfc14b3a
MK
213 columns. If necessary, enlarge the register set. */
214
215static void
216dwarf2_frame_state_alloc_regs (struct dwarf2_frame_state_reg_info *rs,
217 int num_regs)
218{
219 size_t size = sizeof (struct dwarf2_frame_state_reg);
220
221 if (num_regs <= rs->num_regs)
222 return;
223
224 rs->reg = (struct dwarf2_frame_state_reg *)
225 xrealloc (rs->reg, num_regs * size);
226
227 /* Initialize newly allocated registers. */
2473a4a9 228 memset (rs->reg + rs->num_regs, 0, (num_regs - rs->num_regs) * size);
cfc14b3a
MK
229 rs->num_regs = num_regs;
230}
231
232/* Copy the register columns in register set RS into newly allocated
233 memory and return a pointer to this newly created copy. */
234
235static struct dwarf2_frame_state_reg *
236dwarf2_frame_state_copy_regs (struct dwarf2_frame_state_reg_info *rs)
237{
d10891d4 238 size_t size = rs->num_regs * sizeof (struct dwarf2_frame_state_reg);
cfc14b3a
MK
239 struct dwarf2_frame_state_reg *reg;
240
241 reg = (struct dwarf2_frame_state_reg *) xmalloc (size);
242 memcpy (reg, rs->reg, size);
243
244 return reg;
245}
246
247/* Release the memory allocated to register set RS. */
248
249static void
250dwarf2_frame_state_free_regs (struct dwarf2_frame_state_reg_info *rs)
251{
252 if (rs)
253 {
254 dwarf2_frame_state_free_regs (rs->prev);
255
256 xfree (rs->reg);
257 xfree (rs);
258 }
259}
260
261/* Release the memory allocated to the frame state FS. */
262
263static void
264dwarf2_frame_state_free (void *p)
265{
266 struct dwarf2_frame_state *fs = p;
267
268 dwarf2_frame_state_free_regs (fs->initial.prev);
269 dwarf2_frame_state_free_regs (fs->regs.prev);
270 xfree (fs->initial.reg);
271 xfree (fs->regs.reg);
272 xfree (fs);
273}
274\f
275
276/* Helper functions for execute_stack_op. */
277
278static CORE_ADDR
279read_reg (void *baton, int reg)
280{
4a4e5149
DJ
281 struct frame_info *this_frame = (struct frame_info *) baton;
282 struct gdbarch *gdbarch = get_frame_arch (this_frame);
cfc14b3a 283 int regnum;
852483bc 284 gdb_byte *buf;
cfc14b3a 285
ad010def 286 regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg);
cfc14b3a 287
852483bc 288 buf = alloca (register_size (gdbarch, regnum));
4a4e5149 289 get_frame_register (this_frame, regnum, buf);
f2da6b3a
DJ
290
291 /* Convert the register to an integer. This returns a LONGEST
292 rather than a CORE_ADDR, but unpack_pointer does the same thing
293 under the covers, and this makes more sense for non-pointer
294 registers. Maybe read_reg and the associated interfaces should
295 deal with "struct value" instead of CORE_ADDR. */
296 return unpack_long (register_type (gdbarch, regnum), buf);
cfc14b3a
MK
297}
298
299static void
852483bc 300read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
cfc14b3a
MK
301{
302 read_memory (addr, buf, len);
303}
304
305static void
852483bc 306no_get_frame_base (void *baton, gdb_byte **start, size_t *length)
cfc14b3a
MK
307{
308 internal_error (__FILE__, __LINE__,
e2e0b3e5 309 _("Support for DW_OP_fbreg is unimplemented"));
cfc14b3a
MK
310}
311
312static CORE_ADDR
313no_get_tls_address (void *baton, CORE_ADDR offset)
314{
315 internal_error (__FILE__, __LINE__,
e2e0b3e5 316 _("Support for DW_OP_GNU_push_tls_address is unimplemented"));
cfc14b3a
MK
317}
318
a6a5a945
LM
319/* Execute the required actions for both the DW_CFA_restore and
320DW_CFA_restore_extended instructions. */
321static void
322dwarf2_restore_rule (struct gdbarch *gdbarch, ULONGEST reg_num,
323 struct dwarf2_frame_state *fs, int eh_frame_p)
324{
325 ULONGEST reg;
326
327 gdb_assert (fs->initial.reg);
328 reg = dwarf2_frame_adjust_regnum (gdbarch, reg_num, eh_frame_p);
329 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
330
331 /* Check if this register was explicitly initialized in the
332 CIE initial instructions. If not, default the rule to
333 UNSPECIFIED. */
334 if (reg < fs->initial.num_regs)
335 fs->regs.reg[reg] = fs->initial.reg[reg];
336 else
337 fs->regs.reg[reg].how = DWARF2_FRAME_REG_UNSPECIFIED;
338
339 if (fs->regs.reg[reg].how == DWARF2_FRAME_REG_UNSPECIFIED)
340 complaint (&symfile_complaints, _("\
341incomplete CFI data; DW_CFA_restore unspecified\n\
5af949e3 342register %s (#%d) at %s"),
a6a5a945
LM
343 gdbarch_register_name
344 (gdbarch, gdbarch_dwarf2_reg_to_regnum (gdbarch, reg)),
345 gdbarch_dwarf2_reg_to_regnum (gdbarch, reg),
5af949e3 346 paddress (gdbarch, fs->pc));
a6a5a945
LM
347}
348
cfc14b3a 349static CORE_ADDR
ae0d2f24 350execute_stack_op (gdb_byte *exp, ULONGEST len, int addr_size,
4a4e5149 351 struct frame_info *this_frame, CORE_ADDR initial)
cfc14b3a
MK
352{
353 struct dwarf_expr_context *ctx;
354 CORE_ADDR result;
4a227398 355 struct cleanup *old_chain;
cfc14b3a
MK
356
357 ctx = new_dwarf_expr_context ();
4a227398
TT
358 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
359
f7fd4728 360 ctx->gdbarch = get_frame_arch (this_frame);
ae0d2f24 361 ctx->addr_size = addr_size;
4a4e5149 362 ctx->baton = this_frame;
cfc14b3a
MK
363 ctx->read_reg = read_reg;
364 ctx->read_mem = read_mem;
365 ctx->get_frame_base = no_get_frame_base;
366 ctx->get_tls_address = no_get_tls_address;
367
368 dwarf_expr_push (ctx, initial);
369 dwarf_expr_eval (ctx, exp, len);
370 result = dwarf_expr_fetch (ctx, 0);
371
372 if (ctx->in_reg)
4a4e5149 373 result = read_reg (this_frame, result);
cfc14b3a 374
4a227398 375 do_cleanups (old_chain);
cfc14b3a
MK
376
377 return result;
378}
379\f
380
381static void
ae0d2f24 382execute_cfa_program (struct dwarf2_fde *fde, gdb_byte *insn_ptr,
4a4e5149 383 gdb_byte *insn_end, struct frame_info *this_frame,
ae0d2f24 384 struct dwarf2_frame_state *fs)
cfc14b3a 385{
ae0d2f24 386 int eh_frame_p = fde->eh_frame_p;
4a4e5149 387 CORE_ADDR pc = get_frame_pc (this_frame);
cfc14b3a 388 int bytes_read;
4a4e5149 389 struct gdbarch *gdbarch = get_frame_arch (this_frame);
e17a4113 390 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
cfc14b3a
MK
391
392 while (insn_ptr < insn_end && fs->pc <= pc)
393 {
852483bc 394 gdb_byte insn = *insn_ptr++;
cfc14b3a
MK
395 ULONGEST utmp, reg;
396 LONGEST offset;
397
398 if ((insn & 0xc0) == DW_CFA_advance_loc)
399 fs->pc += (insn & 0x3f) * fs->code_align;
400 else if ((insn & 0xc0) == DW_CFA_offset)
401 {
402 reg = insn & 0x3f;
4fc771b8 403 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
cfc14b3a
MK
404 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
405 offset = utmp * fs->data_align;
406 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
05cbe71a 407 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
cfc14b3a
MK
408 fs->regs.reg[reg].loc.offset = offset;
409 }
410 else if ((insn & 0xc0) == DW_CFA_restore)
411 {
cfc14b3a 412 reg = insn & 0x3f;
a6a5a945 413 dwarf2_restore_rule (gdbarch, reg, fs, eh_frame_p);
cfc14b3a
MK
414 }
415 else
416 {
417 switch (insn)
418 {
419 case DW_CFA_set_loc:
ae0d2f24
UW
420 fs->pc = read_encoded_value (fde->cie->unit, fde->cie->encoding,
421 fde->cie->addr_size, insn_ptr,
422 &bytes_read, fde->initial_location);
423 /* Apply the objfile offset for relocatable objects. */
424 fs->pc += ANOFFSET (fde->cie->unit->objfile->section_offsets,
425 SECT_OFF_TEXT (fde->cie->unit->objfile));
cfc14b3a
MK
426 insn_ptr += bytes_read;
427 break;
428
429 case DW_CFA_advance_loc1:
e17a4113 430 utmp = extract_unsigned_integer (insn_ptr, 1, byte_order);
cfc14b3a
MK
431 fs->pc += utmp * fs->code_align;
432 insn_ptr++;
433 break;
434 case DW_CFA_advance_loc2:
e17a4113 435 utmp = extract_unsigned_integer (insn_ptr, 2, byte_order);
cfc14b3a
MK
436 fs->pc += utmp * fs->code_align;
437 insn_ptr += 2;
438 break;
439 case DW_CFA_advance_loc4:
e17a4113 440 utmp = extract_unsigned_integer (insn_ptr, 4, byte_order);
cfc14b3a
MK
441 fs->pc += utmp * fs->code_align;
442 insn_ptr += 4;
443 break;
444
445 case DW_CFA_offset_extended:
446 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
4fc771b8 447 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
cfc14b3a
MK
448 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
449 offset = utmp * fs->data_align;
450 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
05cbe71a 451 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
cfc14b3a
MK
452 fs->regs.reg[reg].loc.offset = offset;
453 break;
454
455 case DW_CFA_restore_extended:
cfc14b3a 456 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
a6a5a945 457 dwarf2_restore_rule (gdbarch, reg, fs, eh_frame_p);
cfc14b3a
MK
458 break;
459
460 case DW_CFA_undefined:
461 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
4fc771b8 462 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
cfc14b3a 463 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
05cbe71a 464 fs->regs.reg[reg].how = DWARF2_FRAME_REG_UNDEFINED;
cfc14b3a
MK
465 break;
466
467 case DW_CFA_same_value:
468 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
4fc771b8 469 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
cfc14b3a 470 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
05cbe71a 471 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAME_VALUE;
cfc14b3a
MK
472 break;
473
474 case DW_CFA_register:
475 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
4fc771b8 476 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
cfc14b3a 477 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
4fc771b8 478 utmp = dwarf2_frame_adjust_regnum (gdbarch, utmp, eh_frame_p);
cfc14b3a 479 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
05cbe71a 480 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_REG;
cfc14b3a
MK
481 fs->regs.reg[reg].loc.reg = utmp;
482 break;
483
484 case DW_CFA_remember_state:
485 {
486 struct dwarf2_frame_state_reg_info *new_rs;
487
488 new_rs = XMALLOC (struct dwarf2_frame_state_reg_info);
489 *new_rs = fs->regs;
490 fs->regs.reg = dwarf2_frame_state_copy_regs (&fs->regs);
491 fs->regs.prev = new_rs;
492 }
493 break;
494
495 case DW_CFA_restore_state:
496 {
497 struct dwarf2_frame_state_reg_info *old_rs = fs->regs.prev;
498
50ea7769
MK
499 if (old_rs == NULL)
500 {
e2e0b3e5 501 complaint (&symfile_complaints, _("\
5af949e3
UW
502bad CFI data; mismatched DW_CFA_restore_state at %s"),
503 paddress (gdbarch, fs->pc));
50ea7769
MK
504 }
505 else
506 {
507 xfree (fs->regs.reg);
508 fs->regs = *old_rs;
509 xfree (old_rs);
510 }
cfc14b3a
MK
511 }
512 break;
513
514 case DW_CFA_def_cfa:
2fd481e1 515 insn_ptr = read_uleb128 (insn_ptr, insn_end, &fs->regs.cfa_reg);
cfc14b3a 516 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
303b6f5d
DJ
517
518 if (fs->armcc_cfa_offsets_sf)
519 utmp *= fs->data_align;
520
2fd481e1
PP
521 fs->regs.cfa_offset = utmp;
522 fs->regs.cfa_how = CFA_REG_OFFSET;
cfc14b3a
MK
523 break;
524
525 case DW_CFA_def_cfa_register:
2fd481e1
PP
526 insn_ptr = read_uleb128 (insn_ptr, insn_end, &fs->regs.cfa_reg);
527 fs->regs.cfa_reg = dwarf2_frame_adjust_regnum (gdbarch,
528 fs->regs.cfa_reg,
529 eh_frame_p);
530 fs->regs.cfa_how = CFA_REG_OFFSET;
cfc14b3a
MK
531 break;
532
533 case DW_CFA_def_cfa_offset:
852483bc 534 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
303b6f5d
DJ
535
536 if (fs->armcc_cfa_offsets_sf)
537 utmp *= fs->data_align;
538
2fd481e1 539 fs->regs.cfa_offset = utmp;
cfc14b3a
MK
540 /* cfa_how deliberately not set. */
541 break;
542
a8504492
MK
543 case DW_CFA_nop:
544 break;
545
cfc14b3a 546 case DW_CFA_def_cfa_expression:
2fd481e1
PP
547 insn_ptr = read_uleb128 (insn_ptr, insn_end,
548 &fs->regs.cfa_exp_len);
549 fs->regs.cfa_exp = insn_ptr;
550 fs->regs.cfa_how = CFA_EXP;
551 insn_ptr += fs->regs.cfa_exp_len;
cfc14b3a
MK
552 break;
553
554 case DW_CFA_expression:
555 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
4fc771b8 556 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
cfc14b3a
MK
557 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
558 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
559 fs->regs.reg[reg].loc.exp = insn_ptr;
560 fs->regs.reg[reg].exp_len = utmp;
05cbe71a 561 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_EXP;
cfc14b3a
MK
562 insn_ptr += utmp;
563 break;
564
a8504492
MK
565 case DW_CFA_offset_extended_sf:
566 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
4fc771b8 567 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
a8504492 568 insn_ptr = read_sleb128 (insn_ptr, insn_end, &offset);
f6da8dd8 569 offset *= fs->data_align;
a8504492 570 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
05cbe71a 571 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
a8504492
MK
572 fs->regs.reg[reg].loc.offset = offset;
573 break;
574
46ea248b
AO
575 case DW_CFA_val_offset:
576 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
577 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
578 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
579 offset = utmp * fs->data_align;
580 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_OFFSET;
581 fs->regs.reg[reg].loc.offset = offset;
582 break;
583
584 case DW_CFA_val_offset_sf:
585 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
586 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
587 insn_ptr = read_sleb128 (insn_ptr, insn_end, &offset);
588 offset *= fs->data_align;
589 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_OFFSET;
590 fs->regs.reg[reg].loc.offset = offset;
591 break;
592
593 case DW_CFA_val_expression:
594 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
595 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
596 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
597 fs->regs.reg[reg].loc.exp = insn_ptr;
598 fs->regs.reg[reg].exp_len = utmp;
599 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_EXP;
600 insn_ptr += utmp;
601 break;
602
a8504492 603 case DW_CFA_def_cfa_sf:
2fd481e1
PP
604 insn_ptr = read_uleb128 (insn_ptr, insn_end, &fs->regs.cfa_reg);
605 fs->regs.cfa_reg = dwarf2_frame_adjust_regnum (gdbarch,
606 fs->regs.cfa_reg,
607 eh_frame_p);
a8504492 608 insn_ptr = read_sleb128 (insn_ptr, insn_end, &offset);
2fd481e1
PP
609 fs->regs.cfa_offset = offset * fs->data_align;
610 fs->regs.cfa_how = CFA_REG_OFFSET;
a8504492
MK
611 break;
612
613 case DW_CFA_def_cfa_offset_sf:
614 insn_ptr = read_sleb128 (insn_ptr, insn_end, &offset);
2fd481e1 615 fs->regs.cfa_offset = offset * fs->data_align;
a8504492 616 /* cfa_how deliberately not set. */
cfc14b3a
MK
617 break;
618
a77f4086
MK
619 case DW_CFA_GNU_window_save:
620 /* This is SPARC-specific code, and contains hard-coded
621 constants for the register numbering scheme used by
622 GCC. Rather than having a architecture-specific
623 operation that's only ever used by a single
624 architecture, we provide the implementation here.
625 Incidentally that's what GCC does too in its
626 unwinder. */
627 {
4a4e5149 628 int size = register_size (gdbarch, 0);
a77f4086
MK
629 dwarf2_frame_state_alloc_regs (&fs->regs, 32);
630 for (reg = 8; reg < 16; reg++)
631 {
632 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_REG;
633 fs->regs.reg[reg].loc.reg = reg + 16;
634 }
635 for (reg = 16; reg < 32; reg++)
636 {
637 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
638 fs->regs.reg[reg].loc.offset = (reg - 16) * size;
639 }
640 }
641 break;
642
cfc14b3a
MK
643 case DW_CFA_GNU_args_size:
644 /* Ignored. */
645 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
646 break;
647
58894217
JK
648 case DW_CFA_GNU_negative_offset_extended:
649 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
4fc771b8 650 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
58894217
JK
651 insn_ptr = read_uleb128 (insn_ptr, insn_end, &offset);
652 offset *= fs->data_align;
653 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
654 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
655 fs->regs.reg[reg].loc.offset = -offset;
656 break;
657
cfc14b3a 658 default:
e2e0b3e5 659 internal_error (__FILE__, __LINE__, _("Unknown CFI encountered."));
cfc14b3a
MK
660 }
661 }
662 }
663
664 /* Don't allow remember/restore between CIE and FDE programs. */
665 dwarf2_frame_state_free_regs (fs->regs.prev);
666 fs->regs.prev = NULL;
667}
8f22cb90 668\f
cfc14b3a 669
8f22cb90 670/* Architecture-specific operations. */
cfc14b3a 671
8f22cb90
MK
672/* Per-architecture data key. */
673static struct gdbarch_data *dwarf2_frame_data;
674
675struct dwarf2_frame_ops
676{
677 /* Pre-initialize the register state REG for register REGNUM. */
aff37fc1
DM
678 void (*init_reg) (struct gdbarch *, int, struct dwarf2_frame_state_reg *,
679 struct frame_info *);
3ed09a32 680
4a4e5149 681 /* Check whether the THIS_FRAME is a signal trampoline. */
3ed09a32 682 int (*signal_frame_p) (struct gdbarch *, struct frame_info *);
4bf8967c 683
4fc771b8
DJ
684 /* Convert .eh_frame register number to DWARF register number, or
685 adjust .debug_frame register number. */
686 int (*adjust_regnum) (struct gdbarch *, int, int);
cfc14b3a
MK
687};
688
8f22cb90
MK
689/* Default architecture-specific register state initialization
690 function. */
691
692static void
693dwarf2_frame_default_init_reg (struct gdbarch *gdbarch, int regnum,
aff37fc1 694 struct dwarf2_frame_state_reg *reg,
4a4e5149 695 struct frame_info *this_frame)
8f22cb90
MK
696{
697 /* If we have a register that acts as a program counter, mark it as
698 a destination for the return address. If we have a register that
699 serves as the stack pointer, arrange for it to be filled with the
700 call frame address (CFA). The other registers are marked as
701 unspecified.
702
703 We copy the return address to the program counter, since many
704 parts in GDB assume that it is possible to get the return address
705 by unwinding the program counter register. However, on ISA's
706 with a dedicated return address register, the CFI usually only
707 contains information to unwind that return address register.
708
709 The reason we're treating the stack pointer special here is
710 because in many cases GCC doesn't emit CFI for the stack pointer
711 and implicitly assumes that it is equal to the CFA. This makes
712 some sense since the DWARF specification (version 3, draft 8,
713 p. 102) says that:
714
715 "Typically, the CFA is defined to be the value of the stack
716 pointer at the call site in the previous frame (which may be
717 different from its value on entry to the current frame)."
718
719 However, this isn't true for all platforms supported by GCC
720 (e.g. IBM S/390 and zSeries). Those architectures should provide
721 their own architecture-specific initialization function. */
05cbe71a 722
ad010def 723 if (regnum == gdbarch_pc_regnum (gdbarch))
8f22cb90 724 reg->how = DWARF2_FRAME_REG_RA;
ad010def 725 else if (regnum == gdbarch_sp_regnum (gdbarch))
8f22cb90
MK
726 reg->how = DWARF2_FRAME_REG_CFA;
727}
05cbe71a 728
8f22cb90 729/* Return a default for the architecture-specific operations. */
05cbe71a 730
8f22cb90 731static void *
030f20e1 732dwarf2_frame_init (struct obstack *obstack)
8f22cb90
MK
733{
734 struct dwarf2_frame_ops *ops;
735
030f20e1 736 ops = OBSTACK_ZALLOC (obstack, struct dwarf2_frame_ops);
8f22cb90
MK
737 ops->init_reg = dwarf2_frame_default_init_reg;
738 return ops;
739}
05cbe71a 740
8f22cb90
MK
741/* Set the architecture-specific register state initialization
742 function for GDBARCH to INIT_REG. */
743
744void
745dwarf2_frame_set_init_reg (struct gdbarch *gdbarch,
746 void (*init_reg) (struct gdbarch *, int,
aff37fc1
DM
747 struct dwarf2_frame_state_reg *,
748 struct frame_info *))
8f22cb90 749{
030f20e1 750 struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);
8f22cb90 751
8f22cb90
MK
752 ops->init_reg = init_reg;
753}
754
755/* Pre-initialize the register state REG for register REGNUM. */
05cbe71a
MK
756
757static void
758dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
aff37fc1 759 struct dwarf2_frame_state_reg *reg,
4a4e5149 760 struct frame_info *this_frame)
05cbe71a 761{
030f20e1 762 struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);
8f22cb90 763
4a4e5149 764 ops->init_reg (gdbarch, regnum, reg, this_frame);
05cbe71a 765}
3ed09a32
DJ
766
767/* Set the architecture-specific signal trampoline recognition
768 function for GDBARCH to SIGNAL_FRAME_P. */
769
770void
771dwarf2_frame_set_signal_frame_p (struct gdbarch *gdbarch,
772 int (*signal_frame_p) (struct gdbarch *,
773 struct frame_info *))
774{
775 struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);
776
777 ops->signal_frame_p = signal_frame_p;
778}
779
780/* Query the architecture-specific signal frame recognizer for
4a4e5149 781 THIS_FRAME. */
3ed09a32
DJ
782
783static int
784dwarf2_frame_signal_frame_p (struct gdbarch *gdbarch,
4a4e5149 785 struct frame_info *this_frame)
3ed09a32
DJ
786{
787 struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);
788
789 if (ops->signal_frame_p == NULL)
790 return 0;
4a4e5149 791 return ops->signal_frame_p (gdbarch, this_frame);
3ed09a32 792}
4bf8967c 793
4fc771b8
DJ
794/* Set the architecture-specific adjustment of .eh_frame and .debug_frame
795 register numbers. */
4bf8967c
AS
796
797void
4fc771b8
DJ
798dwarf2_frame_set_adjust_regnum (struct gdbarch *gdbarch,
799 int (*adjust_regnum) (struct gdbarch *,
800 int, int))
4bf8967c
AS
801{
802 struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);
803
4fc771b8 804 ops->adjust_regnum = adjust_regnum;
4bf8967c
AS
805}
806
4fc771b8
DJ
807/* Translate a .eh_frame register to DWARF register, or adjust a .debug_frame
808 register. */
4bf8967c 809
4fc771b8
DJ
810static int
811dwarf2_frame_adjust_regnum (struct gdbarch *gdbarch, int regnum, int eh_frame_p)
4bf8967c
AS
812{
813 struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);
814
4fc771b8 815 if (ops->adjust_regnum == NULL)
4bf8967c 816 return regnum;
4fc771b8 817 return ops->adjust_regnum (gdbarch, regnum, eh_frame_p);
4bf8967c 818}
303b6f5d
DJ
819
820static void
821dwarf2_frame_find_quirks (struct dwarf2_frame_state *fs,
822 struct dwarf2_fde *fde)
823{
824 static const char *arm_idents[] = {
825 "ARM C Compiler, ADS",
826 "Thumb C Compiler, ADS",
827 "ARM C++ Compiler, ADS",
828 "Thumb C++ Compiler, ADS",
829 "ARM/Thumb C/C++ Compiler, RVCT"
830 };
831 int i;
832
833 struct symtab *s;
834
835 s = find_pc_symtab (fs->pc);
836 if (s == NULL || s->producer == NULL)
837 return;
838
839 for (i = 0; i < ARRAY_SIZE (arm_idents); i++)
840 if (strncmp (s->producer, arm_idents[i], strlen (arm_idents[i])) == 0)
841 {
842 if (fde->cie->version == 1)
843 fs->armcc_cfa_offsets_sf = 1;
844
845 if (fde->cie->version == 1)
846 fs->armcc_cfa_offsets_reversed = 1;
847
848 /* The reversed offset problem is present in some compilers
849 using DWARF3, but it was eventually fixed. Check the ARM
850 defined augmentations, which are in the format "armcc" followed
851 by a list of one-character options. The "+" option means
852 this problem is fixed (no quirk needed). If the armcc
853 augmentation is missing, the quirk is needed. */
854 if (fde->cie->version == 3
855 && (strncmp (fde->cie->augmentation, "armcc", 5) != 0
856 || strchr (fde->cie->augmentation + 5, '+') == NULL))
857 fs->armcc_cfa_offsets_reversed = 1;
858
859 return;
860 }
861}
8f22cb90
MK
862\f
863
864struct dwarf2_frame_cache
865{
866 /* DWARF Call Frame Address. */
867 CORE_ADDR cfa;
868
0228dfb9
DJ
869 /* Set if the return address column was marked as undefined. */
870 int undefined_retaddr;
871
8f22cb90
MK
872 /* Saved registers, indexed by GDB register number, not by DWARF
873 register number. */
874 struct dwarf2_frame_state_reg *reg;
8d5a9abc
MK
875
876 /* Return address register. */
877 struct dwarf2_frame_state_reg retaddr_reg;
ae0d2f24
UW
878
879 /* Target address size in bytes. */
880 int addr_size;
8f22cb90 881};
05cbe71a 882
b9362cc7 883static struct dwarf2_frame_cache *
4a4e5149 884dwarf2_frame_cache (struct frame_info *this_frame, void **this_cache)
cfc14b3a
MK
885{
886 struct cleanup *old_chain;
4a4e5149 887 struct gdbarch *gdbarch = get_frame_arch (this_frame);
ad010def
UW
888 const int num_regs = gdbarch_num_regs (gdbarch)
889 + gdbarch_num_pseudo_regs (gdbarch);
cfc14b3a
MK
890 struct dwarf2_frame_cache *cache;
891 struct dwarf2_frame_state *fs;
892 struct dwarf2_fde *fde;
cfc14b3a
MK
893
894 if (*this_cache)
895 return *this_cache;
896
897 /* Allocate a new cache. */
898 cache = FRAME_OBSTACK_ZALLOC (struct dwarf2_frame_cache);
899 cache->reg = FRAME_OBSTACK_CALLOC (num_regs, struct dwarf2_frame_state_reg);
900
901 /* Allocate and initialize the frame state. */
902 fs = XMALLOC (struct dwarf2_frame_state);
903 memset (fs, 0, sizeof (struct dwarf2_frame_state));
904 old_chain = make_cleanup (dwarf2_frame_state_free, fs);
905
906 /* Unwind the PC.
907
4a4e5149 908 Note that if the next frame is never supposed to return (i.e. a call
cfc14b3a 909 to abort), the compiler might optimize away the instruction at
4a4e5149 910 its return address. As a result the return address will
cfc14b3a 911 point at some random instruction, and the CFI for that
e4e9607c 912 instruction is probably worthless to us. GCC's unwinder solves
cfc14b3a
MK
913 this problem by substracting 1 from the return address to get an
914 address in the middle of a presumed call instruction (or the
915 instruction in the associated delay slot). This should only be
916 done for "normal" frames and not for resume-type frames (signal
e4e9607c 917 handlers, sentinel frames, dummy frames). The function
ad1193e7 918 get_frame_address_in_block does just this. It's not clear how
e4e9607c
MK
919 reliable the method is though; there is the potential for the
920 register state pre-call being different to that on return. */
4a4e5149 921 fs->pc = get_frame_address_in_block (this_frame);
cfc14b3a
MK
922
923 /* Find the correct FDE. */
924 fde = dwarf2_frame_find_fde (&fs->pc);
925 gdb_assert (fde != NULL);
926
927 /* Extract any interesting information from the CIE. */
928 fs->data_align = fde->cie->data_alignment_factor;
929 fs->code_align = fde->cie->code_alignment_factor;
930 fs->retaddr_column = fde->cie->return_address_register;
ae0d2f24 931 cache->addr_size = fde->cie->addr_size;
cfc14b3a 932
303b6f5d
DJ
933 /* Check for "quirks" - known bugs in producers. */
934 dwarf2_frame_find_quirks (fs, fde);
935
cfc14b3a 936 /* First decode all the insns in the CIE. */
ae0d2f24 937 execute_cfa_program (fde, fde->cie->initial_instructions,
4a4e5149 938 fde->cie->end, this_frame, fs);
cfc14b3a
MK
939
940 /* Save the initialized register set. */
941 fs->initial = fs->regs;
942 fs->initial.reg = dwarf2_frame_state_copy_regs (&fs->regs);
943
944 /* Then decode the insns in the FDE up to our target PC. */
4a4e5149 945 execute_cfa_program (fde, fde->instructions, fde->end, this_frame, fs);
cfc14b3a 946
938f5214 947 /* Calculate the CFA. */
2fd481e1 948 switch (fs->regs.cfa_how)
cfc14b3a
MK
949 {
950 case CFA_REG_OFFSET:
2fd481e1 951 cache->cfa = read_reg (this_frame, fs->regs.cfa_reg);
303b6f5d 952 if (fs->armcc_cfa_offsets_reversed)
2fd481e1 953 cache->cfa -= fs->regs.cfa_offset;
303b6f5d 954 else
2fd481e1 955 cache->cfa += fs->regs.cfa_offset;
cfc14b3a
MK
956 break;
957
958 case CFA_EXP:
959 cache->cfa =
2fd481e1 960 execute_stack_op (fs->regs.cfa_exp, fs->regs.cfa_exp_len,
4a4e5149 961 cache->addr_size, this_frame, 0);
cfc14b3a
MK
962 break;
963
964 default:
e2e0b3e5 965 internal_error (__FILE__, __LINE__, _("Unknown CFA rule."));
cfc14b3a
MK
966 }
967
05cbe71a 968 /* Initialize the register state. */
3e2c4033
AC
969 {
970 int regnum;
e4e9607c 971
3e2c4033 972 for (regnum = 0; regnum < num_regs; regnum++)
4a4e5149 973 dwarf2_frame_init_reg (gdbarch, regnum, &cache->reg[regnum], this_frame);
3e2c4033
AC
974 }
975
976 /* Go through the DWARF2 CFI generated table and save its register
79c4cb80
MK
977 location information in the cache. Note that we don't skip the
978 return address column; it's perfectly all right for it to
979 correspond to a real register. If it doesn't correspond to a
980 real register, or if we shouldn't treat it as such,
055d23b8 981 gdbarch_dwarf2_reg_to_regnum should be defined to return a number outside
f57d151a 982 the range [0, gdbarch_num_regs). */
3e2c4033
AC
983 {
984 int column; /* CFI speak for "register number". */
e4e9607c 985
3e2c4033
AC
986 for (column = 0; column < fs->regs.num_regs; column++)
987 {
3e2c4033 988 /* Use the GDB register number as the destination index. */
ad010def 989 int regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, column);
3e2c4033
AC
990
991 /* If there's no corresponding GDB register, ignore it. */
992 if (regnum < 0 || regnum >= num_regs)
993 continue;
994
995 /* NOTE: cagney/2003-09-05: CFI should specify the disposition
e4e9607c
MK
996 of all debug info registers. If it doesn't, complain (but
997 not too loudly). It turns out that GCC assumes that an
3e2c4033
AC
998 unspecified register implies "same value" when CFI (draft
999 7) specifies nothing at all. Such a register could equally
1000 be interpreted as "undefined". Also note that this check
e4e9607c
MK
1001 isn't sufficient; it only checks that all registers in the
1002 range [0 .. max column] are specified, and won't detect
3e2c4033 1003 problems when a debug info register falls outside of the
e4e9607c 1004 table. We need a way of iterating through all the valid
3e2c4033 1005 DWARF2 register numbers. */
05cbe71a 1006 if (fs->regs.reg[column].how == DWARF2_FRAME_REG_UNSPECIFIED)
f059bf6f
AC
1007 {
1008 if (cache->reg[regnum].how == DWARF2_FRAME_REG_UNSPECIFIED)
e2e0b3e5 1009 complaint (&symfile_complaints, _("\
5af949e3 1010incomplete CFI data; unspecified registers (e.g., %s) at %s"),
f059bf6f 1011 gdbarch_register_name (gdbarch, regnum),
5af949e3 1012 paddress (gdbarch, fs->pc));
f059bf6f 1013 }
35889917
MK
1014 else
1015 cache->reg[regnum] = fs->regs.reg[column];
3e2c4033
AC
1016 }
1017 }
cfc14b3a 1018
8d5a9abc
MK
1019 /* Eliminate any DWARF2_FRAME_REG_RA rules, and save the information
1020 we need for evaluating DWARF2_FRAME_REG_RA_OFFSET rules. */
35889917
MK
1021 {
1022 int regnum;
1023
1024 for (regnum = 0; regnum < num_regs; regnum++)
1025 {
8d5a9abc
MK
1026 if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA
1027 || cache->reg[regnum].how == DWARF2_FRAME_REG_RA_OFFSET)
35889917 1028 {
05cbe71a
MK
1029 struct dwarf2_frame_state_reg *retaddr_reg =
1030 &fs->regs.reg[fs->retaddr_column];
1031
d4f10bf2
MK
1032 /* It seems rather bizarre to specify an "empty" column as
1033 the return adress column. However, this is exactly
1034 what GCC does on some targets. It turns out that GCC
1035 assumes that the return address can be found in the
1036 register corresponding to the return address column.
8d5a9abc
MK
1037 Incidentally, that's how we should treat a return
1038 address column specifying "same value" too. */
d4f10bf2 1039 if (fs->retaddr_column < fs->regs.num_regs
05cbe71a
MK
1040 && retaddr_reg->how != DWARF2_FRAME_REG_UNSPECIFIED
1041 && retaddr_reg->how != DWARF2_FRAME_REG_SAME_VALUE)
8d5a9abc
MK
1042 {
1043 if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA)
1044 cache->reg[regnum] = *retaddr_reg;
1045 else
1046 cache->retaddr_reg = *retaddr_reg;
1047 }
35889917
MK
1048 else
1049 {
8d5a9abc
MK
1050 if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA)
1051 {
1052 cache->reg[regnum].loc.reg = fs->retaddr_column;
1053 cache->reg[regnum].how = DWARF2_FRAME_REG_SAVED_REG;
1054 }
1055 else
1056 {
1057 cache->retaddr_reg.loc.reg = fs->retaddr_column;
1058 cache->retaddr_reg.how = DWARF2_FRAME_REG_SAVED_REG;
1059 }
35889917
MK
1060 }
1061 }
1062 }
1063 }
cfc14b3a 1064
0228dfb9
DJ
1065 if (fs->retaddr_column < fs->regs.num_regs
1066 && fs->regs.reg[fs->retaddr_column].how == DWARF2_FRAME_REG_UNDEFINED)
1067 cache->undefined_retaddr = 1;
1068
cfc14b3a
MK
1069 do_cleanups (old_chain);
1070
1071 *this_cache = cache;
1072 return cache;
1073}
1074
1075static void
4a4e5149 1076dwarf2_frame_this_id (struct frame_info *this_frame, void **this_cache,
cfc14b3a
MK
1077 struct frame_id *this_id)
1078{
1079 struct dwarf2_frame_cache *cache =
4a4e5149 1080 dwarf2_frame_cache (this_frame, this_cache);
cfc14b3a 1081
0228dfb9
DJ
1082 if (cache->undefined_retaddr)
1083 return;
1084
4a4e5149 1085 (*this_id) = frame_id_build (cache->cfa, get_frame_func (this_frame));
93d42b30
DJ
1086}
1087
4a4e5149
DJ
1088static struct value *
1089dwarf2_frame_prev_register (struct frame_info *this_frame, void **this_cache,
1090 int regnum)
93d42b30 1091{
4a4e5149 1092 struct gdbarch *gdbarch = get_frame_arch (this_frame);
93d42b30 1093 struct dwarf2_frame_cache *cache =
4a4e5149
DJ
1094 dwarf2_frame_cache (this_frame, this_cache);
1095 CORE_ADDR addr;
1096 int realnum;
cfc14b3a
MK
1097
1098 switch (cache->reg[regnum].how)
1099 {
05cbe71a 1100 case DWARF2_FRAME_REG_UNDEFINED:
3e2c4033 1101 /* If CFI explicitly specified that the value isn't defined,
e4e9607c 1102 mark it as optimized away; the value isn't available. */
4a4e5149 1103 return frame_unwind_got_optimized (this_frame, regnum);
cfc14b3a 1104
05cbe71a 1105 case DWARF2_FRAME_REG_SAVED_OFFSET:
4a4e5149
DJ
1106 addr = cache->cfa + cache->reg[regnum].loc.offset;
1107 return frame_unwind_got_memory (this_frame, regnum, addr);
cfc14b3a 1108
05cbe71a 1109 case DWARF2_FRAME_REG_SAVED_REG:
4a4e5149
DJ
1110 realnum
1111 = gdbarch_dwarf2_reg_to_regnum (gdbarch, cache->reg[regnum].loc.reg);
1112 return frame_unwind_got_register (this_frame, regnum, realnum);
cfc14b3a 1113
05cbe71a 1114 case DWARF2_FRAME_REG_SAVED_EXP:
4a4e5149
DJ
1115 addr = execute_stack_op (cache->reg[regnum].loc.exp,
1116 cache->reg[regnum].exp_len,
1117 cache->addr_size, this_frame, cache->cfa);
1118 return frame_unwind_got_memory (this_frame, regnum, addr);
cfc14b3a 1119
46ea248b 1120 case DWARF2_FRAME_REG_SAVED_VAL_OFFSET:
4a4e5149
DJ
1121 addr = cache->cfa + cache->reg[regnum].loc.offset;
1122 return frame_unwind_got_constant (this_frame, regnum, addr);
46ea248b
AO
1123
1124 case DWARF2_FRAME_REG_SAVED_VAL_EXP:
4a4e5149
DJ
1125 addr = execute_stack_op (cache->reg[regnum].loc.exp,
1126 cache->reg[regnum].exp_len,
1127 cache->addr_size, this_frame, cache->cfa);
1128 return frame_unwind_got_constant (this_frame, regnum, addr);
46ea248b 1129
05cbe71a 1130 case DWARF2_FRAME_REG_UNSPECIFIED:
3e2c4033
AC
1131 /* GCC, in its infinite wisdom decided to not provide unwind
1132 information for registers that are "same value". Since
1133 DWARF2 (3 draft 7) doesn't define such behavior, said
1134 registers are actually undefined (which is different to CFI
1135 "undefined"). Code above issues a complaint about this.
1136 Here just fudge the books, assume GCC, and that the value is
1137 more inner on the stack. */
4a4e5149 1138 return frame_unwind_got_register (this_frame, regnum, regnum);
3e2c4033 1139
05cbe71a 1140 case DWARF2_FRAME_REG_SAME_VALUE:
4a4e5149 1141 return frame_unwind_got_register (this_frame, regnum, regnum);
cfc14b3a 1142
05cbe71a 1143 case DWARF2_FRAME_REG_CFA:
4a4e5149 1144 return frame_unwind_got_address (this_frame, regnum, cache->cfa);
35889917 1145
ea7963f0 1146 case DWARF2_FRAME_REG_CFA_OFFSET:
4a4e5149
DJ
1147 addr = cache->cfa + cache->reg[regnum].loc.offset;
1148 return frame_unwind_got_address (this_frame, regnum, addr);
ea7963f0 1149
8d5a9abc 1150 case DWARF2_FRAME_REG_RA_OFFSET:
4a4e5149
DJ
1151 addr = cache->reg[regnum].loc.offset;
1152 regnum = gdbarch_dwarf2_reg_to_regnum
1153 (gdbarch, cache->retaddr_reg.loc.reg);
1154 addr += get_frame_register_unsigned (this_frame, regnum);
1155 return frame_unwind_got_address (this_frame, regnum, addr);
8d5a9abc 1156
b39cc962
DJ
1157 case DWARF2_FRAME_REG_FN:
1158 return cache->reg[regnum].loc.fn (this_frame, this_cache, regnum);
1159
cfc14b3a 1160 default:
e2e0b3e5 1161 internal_error (__FILE__, __LINE__, _("Unknown register rule."));
cfc14b3a
MK
1162 }
1163}
1164
4a4e5149
DJ
1165static int
1166dwarf2_frame_sniffer (const struct frame_unwind *self,
1167 struct frame_info *this_frame, void **this_cache)
cfc14b3a 1168{
1ce5d6dd 1169 /* Grab an address that is guarenteed to reside somewhere within the
4a4e5149 1170 function. get_frame_pc(), with a no-return next function, can
93d42b30
DJ
1171 end up returning something past the end of this function's body.
1172 If the frame we're sniffing for is a signal frame whose start
1173 address is placed on the stack by the OS, its FDE must
4a4e5149
DJ
1174 extend one byte before its start address or we could potentially
1175 select the FDE of the previous function. */
1176 CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
56c987f6
AO
1177 struct dwarf2_fde *fde = dwarf2_frame_find_fde (&block_addr);
1178 if (!fde)
4a4e5149 1179 return 0;
3ed09a32
DJ
1180
1181 /* On some targets, signal trampolines may have unwind information.
1182 We need to recognize them so that we set the frame type
1183 correctly. */
1184
56c987f6 1185 if (fde->cie->signal_frame
4a4e5149
DJ
1186 || dwarf2_frame_signal_frame_p (get_frame_arch (this_frame),
1187 this_frame))
1188 return self->type == SIGTRAMP_FRAME;
1189
1190 return self->type != SIGTRAMP_FRAME;
1191}
1192
1193static const struct frame_unwind dwarf2_frame_unwind =
1194{
1195 NORMAL_FRAME,
1196 dwarf2_frame_this_id,
1197 dwarf2_frame_prev_register,
1198 NULL,
1199 dwarf2_frame_sniffer
1200};
1201
1202static const struct frame_unwind dwarf2_signal_frame_unwind =
1203{
1204 SIGTRAMP_FRAME,
1205 dwarf2_frame_this_id,
1206 dwarf2_frame_prev_register,
1207 NULL,
1208 dwarf2_frame_sniffer
1209};
cfc14b3a 1210
4a4e5149
DJ
1211/* Append the DWARF-2 frame unwinders to GDBARCH's list. */
1212
1213void
1214dwarf2_append_unwinders (struct gdbarch *gdbarch)
1215{
1216 frame_unwind_append_unwinder (gdbarch, &dwarf2_frame_unwind);
1217 frame_unwind_append_unwinder (gdbarch, &dwarf2_signal_frame_unwind);
cfc14b3a
MK
1218}
1219\f
1220
1221/* There is no explicitly defined relationship between the CFA and the
1222 location of frame's local variables and arguments/parameters.
1223 Therefore, frame base methods on this page should probably only be
1224 used as a last resort, just to avoid printing total garbage as a
1225 response to the "info frame" command. */
1226
1227static CORE_ADDR
4a4e5149 1228dwarf2_frame_base_address (struct frame_info *this_frame, void **this_cache)
cfc14b3a
MK
1229{
1230 struct dwarf2_frame_cache *cache =
4a4e5149 1231 dwarf2_frame_cache (this_frame, this_cache);
cfc14b3a
MK
1232
1233 return cache->cfa;
1234}
1235
1236static const struct frame_base dwarf2_frame_base =
1237{
1238 &dwarf2_frame_unwind,
1239 dwarf2_frame_base_address,
1240 dwarf2_frame_base_address,
1241 dwarf2_frame_base_address
1242};
1243
1244const struct frame_base *
4a4e5149 1245dwarf2_frame_base_sniffer (struct frame_info *this_frame)
cfc14b3a 1246{
4a4e5149 1247 CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
93d42b30 1248 if (dwarf2_frame_find_fde (&block_addr))
cfc14b3a
MK
1249 return &dwarf2_frame_base;
1250
1251 return NULL;
1252}
1253\f
8f22cb90 1254const struct objfile_data *dwarf2_frame_objfile_data;
0d0e1a63 1255
cfc14b3a 1256static unsigned int
852483bc 1257read_1_byte (bfd *abfd, gdb_byte *buf)
cfc14b3a 1258{
852483bc 1259 return bfd_get_8 (abfd, buf);
cfc14b3a
MK
1260}
1261
1262static unsigned int
852483bc 1263read_4_bytes (bfd *abfd, gdb_byte *buf)
cfc14b3a 1264{
852483bc 1265 return bfd_get_32 (abfd, buf);
cfc14b3a
MK
1266}
1267
1268static ULONGEST
852483bc 1269read_8_bytes (bfd *abfd, gdb_byte *buf)
cfc14b3a 1270{
852483bc 1271 return bfd_get_64 (abfd, buf);
cfc14b3a
MK
1272}
1273
1274static ULONGEST
852483bc 1275read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
cfc14b3a
MK
1276{
1277 ULONGEST result;
1278 unsigned int num_read;
1279 int shift;
852483bc 1280 gdb_byte byte;
cfc14b3a
MK
1281
1282 result = 0;
1283 shift = 0;
1284 num_read = 0;
1285
1286 do
1287 {
1288 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
1289 buf++;
1290 num_read++;
1291 result |= ((byte & 0x7f) << shift);
1292 shift += 7;
1293 }
1294 while (byte & 0x80);
1295
1296 *bytes_read_ptr = num_read;
1297
1298 return result;
1299}
1300
1301static LONGEST
852483bc 1302read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
cfc14b3a
MK
1303{
1304 LONGEST result;
1305 int shift;
1306 unsigned int num_read;
852483bc 1307 gdb_byte byte;
cfc14b3a
MK
1308
1309 result = 0;
1310 shift = 0;
1311 num_read = 0;
1312
1313 do
1314 {
1315 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
1316 buf++;
1317 num_read++;
1318 result |= ((byte & 0x7f) << shift);
1319 shift += 7;
1320 }
1321 while (byte & 0x80);
1322
77e0b926
DJ
1323 if (shift < 8 * sizeof (result) && (byte & 0x40))
1324 result |= -(((LONGEST)1) << shift);
cfc14b3a
MK
1325
1326 *bytes_read_ptr = num_read;
1327
1328 return result;
1329}
1330
1331static ULONGEST
852483bc 1332read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
cfc14b3a
MK
1333{
1334 LONGEST result;
1335
852483bc 1336 result = bfd_get_32 (abfd, buf);
cfc14b3a
MK
1337 if (result == 0xffffffff)
1338 {
852483bc 1339 result = bfd_get_64 (abfd, buf + 4);
cfc14b3a
MK
1340 *bytes_read_ptr = 12;
1341 }
1342 else
1343 *bytes_read_ptr = 4;
1344
1345 return result;
1346}
1347\f
1348
1349/* Pointer encoding helper functions. */
1350
1351/* GCC supports exception handling based on DWARF2 CFI. However, for
1352 technical reasons, it encodes addresses in its FDE's in a different
1353 way. Several "pointer encodings" are supported. The encoding
1354 that's used for a particular FDE is determined by the 'R'
1355 augmentation in the associated CIE. The argument of this
1356 augmentation is a single byte.
1357
1358 The address can be encoded as 2 bytes, 4 bytes, 8 bytes, or as a
1359 LEB128. This is encoded in bits 0, 1 and 2. Bit 3 encodes whether
1360 the address is signed or unsigned. Bits 4, 5 and 6 encode how the
1361 address should be interpreted (absolute, relative to the current
1362 position in the FDE, ...). Bit 7, indicates that the address
1363 should be dereferenced. */
1364
852483bc 1365static gdb_byte
cfc14b3a
MK
1366encoding_for_size (unsigned int size)
1367{
1368 switch (size)
1369 {
1370 case 2:
1371 return DW_EH_PE_udata2;
1372 case 4:
1373 return DW_EH_PE_udata4;
1374 case 8:
1375 return DW_EH_PE_udata8;
1376 default:
e2e0b3e5 1377 internal_error (__FILE__, __LINE__, _("Unsupported address size"));
cfc14b3a
MK
1378 }
1379}
1380
cfc14b3a 1381static CORE_ADDR
852483bc 1382read_encoded_value (struct comp_unit *unit, gdb_byte encoding,
ae0d2f24
UW
1383 int ptr_len, gdb_byte *buf, unsigned int *bytes_read_ptr,
1384 CORE_ADDR func_base)
cfc14b3a 1385{
68f6cf99 1386 ptrdiff_t offset;
cfc14b3a
MK
1387 CORE_ADDR base;
1388
1389 /* GCC currently doesn't generate DW_EH_PE_indirect encodings for
1390 FDE's. */
1391 if (encoding & DW_EH_PE_indirect)
1392 internal_error (__FILE__, __LINE__,
e2e0b3e5 1393 _("Unsupported encoding: DW_EH_PE_indirect"));
cfc14b3a 1394
68f6cf99
MK
1395 *bytes_read_ptr = 0;
1396
cfc14b3a
MK
1397 switch (encoding & 0x70)
1398 {
1399 case DW_EH_PE_absptr:
1400 base = 0;
1401 break;
1402 case DW_EH_PE_pcrel:
f2fec864 1403 base = bfd_get_section_vma (unit->abfd, unit->dwarf_frame_section);
852483bc 1404 base += (buf - unit->dwarf_frame_buffer);
cfc14b3a 1405 break;
0912c7f2
MK
1406 case DW_EH_PE_datarel:
1407 base = unit->dbase;
1408 break;
0fd85043
CV
1409 case DW_EH_PE_textrel:
1410 base = unit->tbase;
1411 break;
03ac2a74 1412 case DW_EH_PE_funcrel:
ae0d2f24 1413 base = func_base;
03ac2a74 1414 break;
68f6cf99
MK
1415 case DW_EH_PE_aligned:
1416 base = 0;
852483bc 1417 offset = buf - unit->dwarf_frame_buffer;
68f6cf99
MK
1418 if ((offset % ptr_len) != 0)
1419 {
1420 *bytes_read_ptr = ptr_len - (offset % ptr_len);
1421 buf += *bytes_read_ptr;
1422 }
1423 break;
cfc14b3a 1424 default:
e2e0b3e5 1425 internal_error (__FILE__, __LINE__, _("Invalid or unsupported encoding"));
cfc14b3a
MK
1426 }
1427
b04de778 1428 if ((encoding & 0x07) == 0x00)
f2fec864
DJ
1429 {
1430 encoding |= encoding_for_size (ptr_len);
1431 if (bfd_get_sign_extend_vma (unit->abfd))
1432 encoding |= DW_EH_PE_signed;
1433 }
cfc14b3a
MK
1434
1435 switch (encoding & 0x0f)
1436 {
a81b10ae
MK
1437 case DW_EH_PE_uleb128:
1438 {
1439 ULONGEST value;
852483bc 1440 gdb_byte *end_buf = buf + (sizeof (value) + 1) * 8 / 7;
a7289609 1441 *bytes_read_ptr += read_uleb128 (buf, end_buf, &value) - buf;
a81b10ae
MK
1442 return base + value;
1443 }
cfc14b3a 1444 case DW_EH_PE_udata2:
68f6cf99 1445 *bytes_read_ptr += 2;
cfc14b3a
MK
1446 return (base + bfd_get_16 (unit->abfd, (bfd_byte *) buf));
1447 case DW_EH_PE_udata4:
68f6cf99 1448 *bytes_read_ptr += 4;
cfc14b3a
MK
1449 return (base + bfd_get_32 (unit->abfd, (bfd_byte *) buf));
1450 case DW_EH_PE_udata8:
68f6cf99 1451 *bytes_read_ptr += 8;
cfc14b3a 1452 return (base + bfd_get_64 (unit->abfd, (bfd_byte *) buf));
a81b10ae
MK
1453 case DW_EH_PE_sleb128:
1454 {
1455 LONGEST value;
852483bc 1456 gdb_byte *end_buf = buf + (sizeof (value) + 1) * 8 / 7;
a7289609 1457 *bytes_read_ptr += read_sleb128 (buf, end_buf, &value) - buf;
a81b10ae
MK
1458 return base + value;
1459 }
cfc14b3a 1460 case DW_EH_PE_sdata2:
68f6cf99 1461 *bytes_read_ptr += 2;
cfc14b3a
MK
1462 return (base + bfd_get_signed_16 (unit->abfd, (bfd_byte *) buf));
1463 case DW_EH_PE_sdata4:
68f6cf99 1464 *bytes_read_ptr += 4;
cfc14b3a
MK
1465 return (base + bfd_get_signed_32 (unit->abfd, (bfd_byte *) buf));
1466 case DW_EH_PE_sdata8:
68f6cf99 1467 *bytes_read_ptr += 8;
cfc14b3a
MK
1468 return (base + bfd_get_signed_64 (unit->abfd, (bfd_byte *) buf));
1469 default:
e2e0b3e5 1470 internal_error (__FILE__, __LINE__, _("Invalid or unsupported encoding"));
cfc14b3a
MK
1471 }
1472}
1473\f
1474
b01c8410
PP
1475static int
1476bsearch_cie_cmp (const void *key, const void *element)
cfc14b3a 1477{
b01c8410
PP
1478 ULONGEST cie_pointer = *(ULONGEST *) key;
1479 struct dwarf2_cie *cie = *(struct dwarf2_cie **) element;
cfc14b3a 1480
b01c8410
PP
1481 if (cie_pointer == cie->cie_pointer)
1482 return 0;
cfc14b3a 1483
b01c8410
PP
1484 return (cie_pointer < cie->cie_pointer) ? -1 : 1;
1485}
1486
1487/* Find CIE with the given CIE_POINTER in CIE_TABLE. */
1488static struct dwarf2_cie *
1489find_cie (struct dwarf2_cie_table *cie_table, ULONGEST cie_pointer)
1490{
1491 struct dwarf2_cie **p_cie;
cfc14b3a 1492
b01c8410
PP
1493 p_cie = bsearch (&cie_pointer, cie_table->entries, cie_table->num_entries,
1494 sizeof (cie_table->entries[0]), bsearch_cie_cmp);
1495 if (p_cie != NULL)
1496 return *p_cie;
cfc14b3a
MK
1497 return NULL;
1498}
1499
b01c8410 1500/* Add a pointer to new CIE to the CIE_TABLE, allocating space for it. */
cfc14b3a 1501static void
b01c8410 1502add_cie (struct dwarf2_cie_table *cie_table, struct dwarf2_cie *cie)
cfc14b3a 1503{
b01c8410
PP
1504 const int n = cie_table->num_entries;
1505
1506 gdb_assert (n < 1
1507 || cie_table->entries[n - 1]->cie_pointer < cie->cie_pointer);
1508
1509 cie_table->entries =
1510 xrealloc (cie_table->entries, (n + 1) * sizeof (cie_table->entries[0]));
1511 cie_table->entries[n] = cie;
1512 cie_table->num_entries = n + 1;
1513}
1514
1515static int
1516bsearch_fde_cmp (const void *key, const void *element)
1517{
1518 CORE_ADDR seek_pc = *(CORE_ADDR *) key;
1519 struct dwarf2_fde *fde = *(struct dwarf2_fde **) element;
1520 if (seek_pc < fde->initial_location)
1521 return -1;
1522 if (seek_pc < fde->initial_location + fde->address_range)
1523 return 0;
1524 return 1;
cfc14b3a
MK
1525}
1526
1527/* Find the FDE for *PC. Return a pointer to the FDE, and store the
1528 inital location associated with it into *PC. */
1529
1530static struct dwarf2_fde *
1531dwarf2_frame_find_fde (CORE_ADDR *pc)
1532{
1533 struct objfile *objfile;
1534
1535 ALL_OBJFILES (objfile)
1536 {
b01c8410
PP
1537 struct dwarf2_fde_table *fde_table;
1538 struct dwarf2_fde **p_fde;
cfc14b3a 1539 CORE_ADDR offset;
b01c8410 1540 CORE_ADDR seek_pc;
cfc14b3a 1541
b01c8410
PP
1542 fde_table = objfile_data (objfile, dwarf2_frame_objfile_data);
1543 if (fde_table == NULL)
4ae9ee8e
DJ
1544 continue;
1545
1546 gdb_assert (objfile->section_offsets);
1547 offset = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1548
b01c8410
PP
1549 gdb_assert (fde_table->num_entries > 0);
1550 if (*pc < offset + fde_table->entries[0]->initial_location)
1551 continue;
1552
1553 seek_pc = *pc - offset;
1554 p_fde = bsearch (&seek_pc, fde_table->entries, fde_table->num_entries,
1555 sizeof (fde_table->entries[0]), bsearch_fde_cmp);
1556 if (p_fde != NULL)
1557 {
1558 *pc = (*p_fde)->initial_location + offset;
1559 return *p_fde;
1560 }
cfc14b3a 1561 }
cfc14b3a
MK
1562 return NULL;
1563}
1564
b01c8410 1565/* Add a pointer to new FDE to the FDE_TABLE, allocating space for it. */
cfc14b3a 1566static void
b01c8410 1567add_fde (struct dwarf2_fde_table *fde_table, struct dwarf2_fde *fde)
cfc14b3a 1568{
b01c8410
PP
1569 if (fde->address_range == 0)
1570 /* Discard useless FDEs. */
1571 return;
1572
1573 fde_table->num_entries += 1;
1574 fde_table->entries =
1575 xrealloc (fde_table->entries,
1576 fde_table->num_entries * sizeof (fde_table->entries[0]));
1577 fde_table->entries[fde_table->num_entries - 1] = fde;
cfc14b3a
MK
1578}
1579
1580#ifdef CC_HAS_LONG_LONG
1581#define DW64_CIE_ID 0xffffffffffffffffULL
1582#else
1583#define DW64_CIE_ID ~0
1584#endif
1585
852483bc 1586static gdb_byte *decode_frame_entry (struct comp_unit *unit, gdb_byte *start,
b01c8410
PP
1587 int eh_frame_p,
1588 struct dwarf2_cie_table *cie_table,
1589 struct dwarf2_fde_table *fde_table);
cfc14b3a 1590
6896c0c7
RH
1591/* Decode the next CIE or FDE. Return NULL if invalid input, otherwise
1592 the next byte to be processed. */
852483bc 1593static gdb_byte *
b01c8410
PP
1594decode_frame_entry_1 (struct comp_unit *unit, gdb_byte *start, int eh_frame_p,
1595 struct dwarf2_cie_table *cie_table,
1596 struct dwarf2_fde_table *fde_table)
cfc14b3a 1597{
5e2b427d 1598 struct gdbarch *gdbarch = get_objfile_arch (unit->objfile);
852483bc 1599 gdb_byte *buf, *end;
cfc14b3a
MK
1600 LONGEST length;
1601 unsigned int bytes_read;
6896c0c7
RH
1602 int dwarf64_p;
1603 ULONGEST cie_id;
cfc14b3a 1604 ULONGEST cie_pointer;
cfc14b3a 1605
6896c0c7 1606 buf = start;
cfc14b3a
MK
1607 length = read_initial_length (unit->abfd, buf, &bytes_read);
1608 buf += bytes_read;
1609 end = buf + length;
1610
6896c0c7
RH
1611 /* Are we still within the section? */
1612 if (end > unit->dwarf_frame_buffer + unit->dwarf_frame_size)
1613 return NULL;
1614
cfc14b3a
MK
1615 if (length == 0)
1616 return end;
1617
6896c0c7
RH
1618 /* Distinguish between 32 and 64-bit encoded frame info. */
1619 dwarf64_p = (bytes_read == 12);
cfc14b3a 1620
6896c0c7 1621 /* In a .eh_frame section, zero is used to distinguish CIEs from FDEs. */
cfc14b3a
MK
1622 if (eh_frame_p)
1623 cie_id = 0;
1624 else if (dwarf64_p)
1625 cie_id = DW64_CIE_ID;
6896c0c7
RH
1626 else
1627 cie_id = DW_CIE_ID;
cfc14b3a
MK
1628
1629 if (dwarf64_p)
1630 {
1631 cie_pointer = read_8_bytes (unit->abfd, buf);
1632 buf += 8;
1633 }
1634 else
1635 {
1636 cie_pointer = read_4_bytes (unit->abfd, buf);
1637 buf += 4;
1638 }
1639
1640 if (cie_pointer == cie_id)
1641 {
1642 /* This is a CIE. */
1643 struct dwarf2_cie *cie;
1644 char *augmentation;
28ba0b33 1645 unsigned int cie_version;
cfc14b3a
MK
1646
1647 /* Record the offset into the .debug_frame section of this CIE. */
1648 cie_pointer = start - unit->dwarf_frame_buffer;
1649
1650 /* Check whether we've already read it. */
b01c8410 1651 if (find_cie (cie_table, cie_pointer))
cfc14b3a
MK
1652 return end;
1653
1654 cie = (struct dwarf2_cie *)
8b92e4d5 1655 obstack_alloc (&unit->objfile->objfile_obstack,
cfc14b3a
MK
1656 sizeof (struct dwarf2_cie));
1657 cie->initial_instructions = NULL;
1658 cie->cie_pointer = cie_pointer;
1659
1660 /* The encoding for FDE's in a normal .debug_frame section
32b05c07
MK
1661 depends on the target address size. */
1662 cie->encoding = DW_EH_PE_absptr;
cfc14b3a 1663
ae0d2f24
UW
1664 /* The target address size. For .eh_frame FDEs this is considered
1665 equal to the size of a target pointer. For .dwarf_frame FDEs,
1666 this is supposed to be the target address size from the associated
1667 CU header. FIXME: We do not have a good way to determine the
1668 latter. Always use the target pointer size for now. */
5e2b427d 1669 cie->addr_size = gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT;
ae0d2f24 1670
56c987f6
AO
1671 /* We'll determine the final value later, but we need to
1672 initialize it conservatively. */
1673 cie->signal_frame = 0;
1674
cfc14b3a 1675 /* Check version number. */
28ba0b33
PB
1676 cie_version = read_1_byte (unit->abfd, buf);
1677 if (cie_version != 1 && cie_version != 3)
6896c0c7 1678 return NULL;
303b6f5d 1679 cie->version = cie_version;
cfc14b3a
MK
1680 buf += 1;
1681
1682 /* Interpret the interesting bits of the augmentation. */
303b6f5d 1683 cie->augmentation = augmentation = (char *) buf;
852483bc 1684 buf += (strlen (augmentation) + 1);
cfc14b3a 1685
303b6f5d
DJ
1686 /* Ignore armcc augmentations. We only use them for quirks,
1687 and that doesn't happen until later. */
1688 if (strncmp (augmentation, "armcc", 5) == 0)
1689 augmentation += strlen (augmentation);
1690
cfc14b3a
MK
1691 /* The GCC 2.x "eh" augmentation has a pointer immediately
1692 following the augmentation string, so it must be handled
1693 first. */
1694 if (augmentation[0] == 'e' && augmentation[1] == 'h')
1695 {
1696 /* Skip. */
5e2b427d 1697 buf += gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT;
cfc14b3a
MK
1698 augmentation += 2;
1699 }
1700
1701 cie->code_alignment_factor =
1702 read_unsigned_leb128 (unit->abfd, buf, &bytes_read);
1703 buf += bytes_read;
1704
1705 cie->data_alignment_factor =
1706 read_signed_leb128 (unit->abfd, buf, &bytes_read);
1707 buf += bytes_read;
1708
28ba0b33
PB
1709 if (cie_version == 1)
1710 {
1711 cie->return_address_register = read_1_byte (unit->abfd, buf);
1712 bytes_read = 1;
1713 }
1714 else
1715 cie->return_address_register = read_unsigned_leb128 (unit->abfd, buf,
1716 &bytes_read);
4fc771b8 1717 cie->return_address_register
5e2b427d 1718 = dwarf2_frame_adjust_regnum (gdbarch,
4fc771b8
DJ
1719 cie->return_address_register,
1720 eh_frame_p);
4bf8967c 1721
28ba0b33 1722 buf += bytes_read;
cfc14b3a 1723
7131cb6e
RH
1724 cie->saw_z_augmentation = (*augmentation == 'z');
1725 if (cie->saw_z_augmentation)
cfc14b3a
MK
1726 {
1727 ULONGEST length;
1728
1729 length = read_unsigned_leb128 (unit->abfd, buf, &bytes_read);
1730 buf += bytes_read;
6896c0c7
RH
1731 if (buf > end)
1732 return NULL;
cfc14b3a
MK
1733 cie->initial_instructions = buf + length;
1734 augmentation++;
1735 }
1736
1737 while (*augmentation)
1738 {
1739 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
1740 if (*augmentation == 'L')
1741 {
1742 /* Skip. */
1743 buf++;
1744 augmentation++;
1745 }
1746
1747 /* "R" indicates a byte indicating how FDE addresses are encoded. */
1748 else if (*augmentation == 'R')
1749 {
1750 cie->encoding = *buf++;
1751 augmentation++;
1752 }
1753
1754 /* "P" indicates a personality routine in the CIE augmentation. */
1755 else if (*augmentation == 'P')
1756 {
1234d960 1757 /* Skip. Avoid indirection since we throw away the result. */
852483bc 1758 gdb_byte encoding = (*buf++) & ~DW_EH_PE_indirect;
ae0d2f24
UW
1759 read_encoded_value (unit, encoding, cie->addr_size,
1760 buf, &bytes_read, 0);
f724bf08 1761 buf += bytes_read;
cfc14b3a
MK
1762 augmentation++;
1763 }
1764
56c987f6
AO
1765 /* "S" indicates a signal frame, such that the return
1766 address must not be decremented to locate the call frame
1767 info for the previous frame; it might even be the first
1768 instruction of a function, so decrementing it would take
1769 us to a different function. */
1770 else if (*augmentation == 'S')
1771 {
1772 cie->signal_frame = 1;
1773 augmentation++;
1774 }
1775
3e9a2e52
DJ
1776 /* Otherwise we have an unknown augmentation. Assume that either
1777 there is no augmentation data, or we saw a 'z' prefix. */
cfc14b3a
MK
1778 else
1779 {
3e9a2e52
DJ
1780 if (cie->initial_instructions)
1781 buf = cie->initial_instructions;
cfc14b3a
MK
1782 break;
1783 }
1784 }
1785
1786 cie->initial_instructions = buf;
1787 cie->end = end;
b01c8410 1788 cie->unit = unit;
cfc14b3a 1789
b01c8410 1790 add_cie (cie_table, cie);
cfc14b3a
MK
1791 }
1792 else
1793 {
1794 /* This is a FDE. */
1795 struct dwarf2_fde *fde;
1796
6896c0c7
RH
1797 /* In an .eh_frame section, the CIE pointer is the delta between the
1798 address within the FDE where the CIE pointer is stored and the
1799 address of the CIE. Convert it to an offset into the .eh_frame
1800 section. */
cfc14b3a
MK
1801 if (eh_frame_p)
1802 {
cfc14b3a
MK
1803 cie_pointer = buf - unit->dwarf_frame_buffer - cie_pointer;
1804 cie_pointer -= (dwarf64_p ? 8 : 4);
1805 }
1806
6896c0c7
RH
1807 /* In either case, validate the result is still within the section. */
1808 if (cie_pointer >= unit->dwarf_frame_size)
1809 return NULL;
1810
cfc14b3a 1811 fde = (struct dwarf2_fde *)
8b92e4d5 1812 obstack_alloc (&unit->objfile->objfile_obstack,
cfc14b3a 1813 sizeof (struct dwarf2_fde));
b01c8410 1814 fde->cie = find_cie (cie_table, cie_pointer);
cfc14b3a
MK
1815 if (fde->cie == NULL)
1816 {
1817 decode_frame_entry (unit, unit->dwarf_frame_buffer + cie_pointer,
b01c8410
PP
1818 eh_frame_p, cie_table, fde_table);
1819 fde->cie = find_cie (cie_table, cie_pointer);
cfc14b3a
MK
1820 }
1821
1822 gdb_assert (fde->cie != NULL);
1823
1824 fde->initial_location =
ae0d2f24
UW
1825 read_encoded_value (unit, fde->cie->encoding, fde->cie->addr_size,
1826 buf, &bytes_read, 0);
cfc14b3a
MK
1827 buf += bytes_read;
1828
1829 fde->address_range =
ae0d2f24
UW
1830 read_encoded_value (unit, fde->cie->encoding & 0x0f,
1831 fde->cie->addr_size, buf, &bytes_read, 0);
cfc14b3a
MK
1832 buf += bytes_read;
1833
7131cb6e
RH
1834 /* A 'z' augmentation in the CIE implies the presence of an
1835 augmentation field in the FDE as well. The only thing known
1836 to be in here at present is the LSDA entry for EH. So we
1837 can skip the whole thing. */
1838 if (fde->cie->saw_z_augmentation)
1839 {
1840 ULONGEST length;
1841
1842 length = read_unsigned_leb128 (unit->abfd, buf, &bytes_read);
1843 buf += bytes_read + length;
6896c0c7
RH
1844 if (buf > end)
1845 return NULL;
7131cb6e
RH
1846 }
1847
cfc14b3a
MK
1848 fde->instructions = buf;
1849 fde->end = end;
1850
4bf8967c
AS
1851 fde->eh_frame_p = eh_frame_p;
1852
b01c8410 1853 add_fde (fde_table, fde);
cfc14b3a
MK
1854 }
1855
1856 return end;
1857}
6896c0c7
RH
1858
1859/* Read a CIE or FDE in BUF and decode it. */
852483bc 1860static gdb_byte *
b01c8410
PP
1861decode_frame_entry (struct comp_unit *unit, gdb_byte *start, int eh_frame_p,
1862 struct dwarf2_cie_table *cie_table,
1863 struct dwarf2_fde_table *fde_table)
6896c0c7
RH
1864{
1865 enum { NONE, ALIGN4, ALIGN8, FAIL } workaround = NONE;
852483bc 1866 gdb_byte *ret;
6896c0c7
RH
1867 const char *msg;
1868 ptrdiff_t start_offset;
1869
1870 while (1)
1871 {
b01c8410
PP
1872 ret = decode_frame_entry_1 (unit, start, eh_frame_p,
1873 cie_table, fde_table);
6896c0c7
RH
1874 if (ret != NULL)
1875 break;
1876
1877 /* We have corrupt input data of some form. */
1878
1879 /* ??? Try, weakly, to work around compiler/assembler/linker bugs
1880 and mismatches wrt padding and alignment of debug sections. */
1881 /* Note that there is no requirement in the standard for any
1882 alignment at all in the frame unwind sections. Testing for
1883 alignment before trying to interpret data would be incorrect.
1884
1885 However, GCC traditionally arranged for frame sections to be
1886 sized such that the FDE length and CIE fields happen to be
1887 aligned (in theory, for performance). This, unfortunately,
1888 was done with .align directives, which had the side effect of
1889 forcing the section to be aligned by the linker.
1890
1891 This becomes a problem when you have some other producer that
1892 creates frame sections that are not as strictly aligned. That
1893 produces a hole in the frame info that gets filled by the
1894 linker with zeros.
1895
1896 The GCC behaviour is arguably a bug, but it's effectively now
1897 part of the ABI, so we're now stuck with it, at least at the
1898 object file level. A smart linker may decide, in the process
1899 of compressing duplicate CIE information, that it can rewrite
1900 the entire output section without this extra padding. */
1901
1902 start_offset = start - unit->dwarf_frame_buffer;
1903 if (workaround < ALIGN4 && (start_offset & 3) != 0)
1904 {
1905 start += 4 - (start_offset & 3);
1906 workaround = ALIGN4;
1907 continue;
1908 }
1909 if (workaround < ALIGN8 && (start_offset & 7) != 0)
1910 {
1911 start += 8 - (start_offset & 7);
1912 workaround = ALIGN8;
1913 continue;
1914 }
1915
1916 /* Nothing left to try. Arrange to return as if we've consumed
1917 the entire input section. Hopefully we'll get valid info from
1918 the other of .debug_frame/.eh_frame. */
1919 workaround = FAIL;
1920 ret = unit->dwarf_frame_buffer + unit->dwarf_frame_size;
1921 break;
1922 }
1923
1924 switch (workaround)
1925 {
1926 case NONE:
1927 break;
1928
1929 case ALIGN4:
1930 complaint (&symfile_complaints,
e2e0b3e5 1931 _("Corrupt data in %s:%s; align 4 workaround apparently succeeded"),
6896c0c7
RH
1932 unit->dwarf_frame_section->owner->filename,
1933 unit->dwarf_frame_section->name);
1934 break;
1935
1936 case ALIGN8:
1937 complaint (&symfile_complaints,
e2e0b3e5 1938 _("Corrupt data in %s:%s; align 8 workaround apparently succeeded"),
6896c0c7
RH
1939 unit->dwarf_frame_section->owner->filename,
1940 unit->dwarf_frame_section->name);
1941 break;
1942
1943 default:
1944 complaint (&symfile_complaints,
e2e0b3e5 1945 _("Corrupt data in %s:%s"),
6896c0c7
RH
1946 unit->dwarf_frame_section->owner->filename,
1947 unit->dwarf_frame_section->name);
1948 break;
1949 }
1950
1951 return ret;
1952}
cfc14b3a
MK
1953\f
1954
cfc14b3a 1955/* Imported from dwarf2read.c. */
dce234bc
PP
1956extern void dwarf2_get_section_info (struct objfile *, const char *, asection **,
1957 gdb_byte **, bfd_size_type *);
cfc14b3a 1958
b01c8410
PP
1959static int
1960qsort_fde_cmp (const void *a, const void *b)
1961{
1962 struct dwarf2_fde *aa = *(struct dwarf2_fde **)a;
1963 struct dwarf2_fde *bb = *(struct dwarf2_fde **)b;
e5af178f 1964
b01c8410 1965 if (aa->initial_location == bb->initial_location)
e5af178f
PP
1966 {
1967 if (aa->address_range != bb->address_range
1968 && aa->eh_frame_p == 0 && bb->eh_frame_p == 0)
1969 /* Linker bug, e.g. gold/10400.
1970 Work around it by keeping stable sort order. */
1971 return (a < b) ? -1 : 1;
1972 else
1973 /* Put eh_frame entries after debug_frame ones. */
1974 return aa->eh_frame_p - bb->eh_frame_p;
1975 }
b01c8410
PP
1976
1977 return (aa->initial_location < bb->initial_location) ? -1 : 1;
1978}
1979
cfc14b3a
MK
1980void
1981dwarf2_build_frame_info (struct objfile *objfile)
1982{
ae0d2f24 1983 struct comp_unit *unit;
852483bc 1984 gdb_byte *frame_ptr;
b01c8410
PP
1985 struct dwarf2_cie_table cie_table;
1986 struct dwarf2_fde_table fde_table;
1987
1988 cie_table.num_entries = 0;
1989 cie_table.entries = NULL;
1990
1991 fde_table.num_entries = 0;
1992 fde_table.entries = NULL;
cfc14b3a
MK
1993
1994 /* Build a minimal decoding of the DWARF2 compilation unit. */
ae0d2f24
UW
1995 unit = (struct comp_unit *) obstack_alloc (&objfile->objfile_obstack,
1996 sizeof (struct comp_unit));
1997 unit->abfd = objfile->obfd;
1998 unit->objfile = objfile;
1999 unit->dbase = 0;
2000 unit->tbase = 0;
cfc14b3a 2001
dce234bc
PP
2002 dwarf2_get_section_info (objfile, ".eh_frame",
2003 &unit->dwarf_frame_section,
2004 &unit->dwarf_frame_buffer,
2005 &unit->dwarf_frame_size);
2006 if (unit->dwarf_frame_size)
cfc14b3a 2007 {
0fd85043 2008 asection *got, *txt;
0912c7f2 2009
0912c7f2 2010 /* FIXME: kettenis/20030602: This is the DW_EH_PE_datarel base
37b517aa
MK
2011 that is used for the i386/amd64 target, which currently is
2012 the only target in GCC that supports/uses the
2013 DW_EH_PE_datarel encoding. */
ae0d2f24 2014 got = bfd_get_section_by_name (unit->abfd, ".got");
0912c7f2 2015 if (got)
ae0d2f24 2016 unit->dbase = got->vma;
0912c7f2 2017
22c7ba1a
MK
2018 /* GCC emits the DW_EH_PE_textrel encoding type on sh and ia64
2019 so far. */
ae0d2f24 2020 txt = bfd_get_section_by_name (unit->abfd, ".text");
0fd85043 2021 if (txt)
ae0d2f24 2022 unit->tbase = txt->vma;
0fd85043 2023
ae0d2f24
UW
2024 frame_ptr = unit->dwarf_frame_buffer;
2025 while (frame_ptr < unit->dwarf_frame_buffer + unit->dwarf_frame_size)
b01c8410
PP
2026 frame_ptr = decode_frame_entry (unit, frame_ptr, 1,
2027 &cie_table, &fde_table);
2028
2029 if (cie_table.num_entries != 0)
2030 {
2031 /* Reinit cie_table: debug_frame has different CIEs. */
2032 xfree (cie_table.entries);
2033 cie_table.num_entries = 0;
2034 cie_table.entries = NULL;
2035 }
cfc14b3a
MK
2036 }
2037
dce234bc
PP
2038 dwarf2_get_section_info (objfile, ".debug_frame",
2039 &unit->dwarf_frame_section,
2040 &unit->dwarf_frame_buffer,
2041 &unit->dwarf_frame_size);
2042 if (unit->dwarf_frame_size)
cfc14b3a 2043 {
ae0d2f24
UW
2044 frame_ptr = unit->dwarf_frame_buffer;
2045 while (frame_ptr < unit->dwarf_frame_buffer + unit->dwarf_frame_size)
b01c8410
PP
2046 frame_ptr = decode_frame_entry (unit, frame_ptr, 0,
2047 &cie_table, &fde_table);
2048 }
2049
2050 /* Discard the cie_table, it is no longer needed. */
2051 if (cie_table.num_entries != 0)
2052 {
2053 xfree (cie_table.entries);
2054 cie_table.entries = NULL; /* Paranoia. */
2055 cie_table.num_entries = 0; /* Paranoia. */
2056 }
2057
2058 if (fde_table.num_entries != 0)
2059 {
2060 struct dwarf2_fde_table *fde_table2;
2061 int i, j;
2062
2063 /* Prepare FDE table for lookups. */
2064 qsort (fde_table.entries, fde_table.num_entries,
2065 sizeof (fde_table.entries[0]), qsort_fde_cmp);
2066
2067 /* Copy fde_table to obstack: it is needed at runtime. */
2068 fde_table2 = (struct dwarf2_fde_table *)
2069 obstack_alloc (&objfile->objfile_obstack, sizeof (*fde_table2));
2070
2071 /* Since we'll be doing bsearch, squeeze out identical (except for
2072 eh_frame_p) fde entries so bsearch result is predictable. */
2073 for (i = 0, j = 0; j < fde_table.num_entries; ++i)
2074 {
2075 const int k = j;
2076
2077 obstack_grow (&objfile->objfile_obstack, &fde_table.entries[j],
2078 sizeof (fde_table.entries[0]));
2079 while (++j < fde_table.num_entries
2080 && (fde_table.entries[k]->initial_location ==
2081 fde_table.entries[j]->initial_location))
2082 /* Skip. */;
2083 }
2084 fde_table2->entries = obstack_finish (&objfile->objfile_obstack);
2085 fde_table2->num_entries = i;
2086 set_objfile_data (objfile, dwarf2_frame_objfile_data, fde_table2);
2087
2088 /* Discard the original fde_table. */
2089 xfree (fde_table.entries);
cfc14b3a
MK
2090 }
2091}
0d0e1a63
MK
2092
2093/* Provide a prototype to silence -Wmissing-prototypes. */
2094void _initialize_dwarf2_frame (void);
2095
2096void
2097_initialize_dwarf2_frame (void)
2098{
030f20e1 2099 dwarf2_frame_data = gdbarch_data_register_pre_init (dwarf2_frame_init);
8f22cb90 2100 dwarf2_frame_objfile_data = register_objfile_data ();
0d0e1a63 2101}
This page took 0.497986 seconds and 4 git commands to generate.