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