2009-09-02 Tristan Gingold <gingold@adacore.com>
[deliverable/binutils-gdb.git] / gdb / dwarf2-frame.c
... / ...
CommitLineData
1/* Frame unwinder for frames with DWARF Call Frame Information.
2
3 Copyright (C) 2003, 2004, 2005, 2007, 2008, 2009
4 Free Software Foundation, Inc.
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
12 the Free Software Foundation; either version 3 of the License, or
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
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22
23#include "defs.h"
24#include "dwarf2expr.h"
25#include "dwarf2.h"
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"
34#include "value.h"
35
36#include "gdb_assert.h"
37#include "gdb_string.h"
38
39#include "complaints.h"
40#include "dwarf2-frame.h"
41
42struct comp_unit;
43
44/* Call Frame Information (CFI). */
45
46/* Common Information Entry (CIE). */
47
48struct dwarf2_cie
49{
50 /* Computation Unit for this CIE. */
51 struct comp_unit *unit;
52
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. */
68 gdb_byte *initial_instructions;
69 gdb_byte *end;
70
71 /* Saved augmentation, in case it's needed later. */
72 char *augmentation;
73
74 /* Encoding of addresses. */
75 gdb_byte encoding;
76
77 /* Target address size in bytes. */
78 int addr_size;
79
80 /* True if a 'z' augmentation existed. */
81 unsigned char saw_z_augmentation;
82
83 /* True if an 'S' augmentation existed. */
84 unsigned char signal_frame;
85
86 /* The version recorded in the CIE. */
87 unsigned char version;
88};
89
90struct dwarf2_cie_table
91{
92 int num_entries;
93 struct dwarf2_cie **entries;
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. */
110 gdb_byte *instructions;
111 gdb_byte *end;
112
113 /* True if this FDE is read from a .eh_frame instead of a .debug_frame
114 section. */
115 unsigned char eh_frame_p;
116};
117
118struct dwarf2_fde_table
119{
120 int num_entries;
121 struct dwarf2_fde **entries;
122};
123
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
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. */
138 bfd_size_type dwarf_frame_size;
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
150static struct dwarf2_fde *dwarf2_frame_find_fde (CORE_ADDR *pc);
151
152static int dwarf2_frame_adjust_regnum (struct gdbarch *gdbarch, int regnum,
153 int eh_frame_p);
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);
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 {
169 struct dwarf2_frame_state_reg *reg;
170 int num_regs;
171
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
181 /* Used to implement DW_CFA_remember_state. */
182 struct dwarf2_frame_state_reg_info *prev;
183 } regs;
184
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;
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;
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
212/* Assert that the register set RS is large enough to store gdbarch_num_regs
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. */
228 memset (rs->reg + rs->num_regs, 0, (num_regs - rs->num_regs) * size);
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{
238 size_t size = rs->num_regs * sizeof (struct dwarf2_frame_state_reg);
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{
281 struct frame_info *this_frame = (struct frame_info *) baton;
282 struct gdbarch *gdbarch = get_frame_arch (this_frame);
283 int regnum;
284 gdb_byte *buf;
285
286 regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg);
287
288 buf = alloca (register_size (gdbarch, regnum));
289 get_frame_register (this_frame, regnum, buf);
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);
297}
298
299static void
300read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
301{
302 read_memory (addr, buf, len);
303}
304
305static void
306no_get_frame_base (void *baton, gdb_byte **start, size_t *length)
307{
308 internal_error (__FILE__, __LINE__,
309 _("Support for DW_OP_fbreg is unimplemented"));
310}
311
312static CORE_ADDR
313no_get_tls_address (void *baton, CORE_ADDR offset)
314{
315 internal_error (__FILE__, __LINE__,
316 _("Support for DW_OP_GNU_push_tls_address is unimplemented"));
317}
318
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\
342register %s (#%d) at %s"),
343 gdbarch_register_name
344 (gdbarch, gdbarch_dwarf2_reg_to_regnum (gdbarch, reg)),
345 gdbarch_dwarf2_reg_to_regnum (gdbarch, reg),
346 paddress (gdbarch, fs->pc));
347}
348
349static CORE_ADDR
350execute_stack_op (gdb_byte *exp, ULONGEST len, int addr_size,
351 struct frame_info *this_frame, CORE_ADDR initial)
352{
353 struct dwarf_expr_context *ctx;
354 CORE_ADDR result;
355 struct cleanup *old_chain;
356
357 ctx = new_dwarf_expr_context ();
358 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
359
360 ctx->gdbarch = get_frame_arch (this_frame);
361 ctx->addr_size = addr_size;
362 ctx->baton = this_frame;
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)
373 result = read_reg (this_frame, result);
374
375 do_cleanups (old_chain);
376
377 return result;
378}
379\f
380
381static void
382execute_cfa_program (struct dwarf2_fde *fde, gdb_byte *insn_ptr,
383 gdb_byte *insn_end, struct frame_info *this_frame,
384 struct dwarf2_frame_state *fs)
385{
386 int eh_frame_p = fde->eh_frame_p;
387 CORE_ADDR pc = get_frame_pc (this_frame);
388 int bytes_read;
389 struct gdbarch *gdbarch = get_frame_arch (this_frame);
390 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
391
392 while (insn_ptr < insn_end && fs->pc <= pc)
393 {
394 gdb_byte insn = *insn_ptr++;
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;
403 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
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);
407 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
408 fs->regs.reg[reg].loc.offset = offset;
409 }
410 else if ((insn & 0xc0) == DW_CFA_restore)
411 {
412 reg = insn & 0x3f;
413 dwarf2_restore_rule (gdbarch, reg, fs, eh_frame_p);
414 }
415 else
416 {
417 switch (insn)
418 {
419 case DW_CFA_set_loc:
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));
426 insn_ptr += bytes_read;
427 break;
428
429 case DW_CFA_advance_loc1:
430 utmp = extract_unsigned_integer (insn_ptr, 1, byte_order);
431 fs->pc += utmp * fs->code_align;
432 insn_ptr++;
433 break;
434 case DW_CFA_advance_loc2:
435 utmp = extract_unsigned_integer (insn_ptr, 2, byte_order);
436 fs->pc += utmp * fs->code_align;
437 insn_ptr += 2;
438 break;
439 case DW_CFA_advance_loc4:
440 utmp = extract_unsigned_integer (insn_ptr, 4, byte_order);
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);
447 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
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);
451 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
452 fs->regs.reg[reg].loc.offset = offset;
453 break;
454
455 case DW_CFA_restore_extended:
456 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
457 dwarf2_restore_rule (gdbarch, reg, fs, eh_frame_p);
458 break;
459
460 case DW_CFA_undefined:
461 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
462 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
463 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
464 fs->regs.reg[reg].how = DWARF2_FRAME_REG_UNDEFINED;
465 break;
466
467 case DW_CFA_same_value:
468 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
469 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
470 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
471 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAME_VALUE;
472 break;
473
474 case DW_CFA_register:
475 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
476 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
477 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
478 utmp = dwarf2_frame_adjust_regnum (gdbarch, utmp, eh_frame_p);
479 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
480 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_REG;
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
499 if (old_rs == NULL)
500 {
501 complaint (&symfile_complaints, _("\
502bad CFI data; mismatched DW_CFA_restore_state at %s"),
503 paddress (gdbarch, fs->pc));
504 }
505 else
506 {
507 xfree (fs->regs.reg);
508 fs->regs = *old_rs;
509 xfree (old_rs);
510 }
511 }
512 break;
513
514 case DW_CFA_def_cfa:
515 insn_ptr = read_uleb128 (insn_ptr, insn_end, &fs->regs.cfa_reg);
516 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
517
518 if (fs->armcc_cfa_offsets_sf)
519 utmp *= fs->data_align;
520
521 fs->regs.cfa_offset = utmp;
522 fs->regs.cfa_how = CFA_REG_OFFSET;
523 break;
524
525 case DW_CFA_def_cfa_register:
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;
531 break;
532
533 case DW_CFA_def_cfa_offset:
534 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
535
536 if (fs->armcc_cfa_offsets_sf)
537 utmp *= fs->data_align;
538
539 fs->regs.cfa_offset = utmp;
540 /* cfa_how deliberately not set. */
541 break;
542
543 case DW_CFA_nop:
544 break;
545
546 case DW_CFA_def_cfa_expression:
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;
552 break;
553
554 case DW_CFA_expression:
555 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
556 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
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;
561 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_EXP;
562 insn_ptr += utmp;
563 break;
564
565 case DW_CFA_offset_extended_sf:
566 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
567 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
568 insn_ptr = read_sleb128 (insn_ptr, insn_end, &offset);
569 offset *= fs->data_align;
570 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
571 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
572 fs->regs.reg[reg].loc.offset = offset;
573 break;
574
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
603 case DW_CFA_def_cfa_sf:
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);
608 insn_ptr = read_sleb128 (insn_ptr, insn_end, &offset);
609 fs->regs.cfa_offset = offset * fs->data_align;
610 fs->regs.cfa_how = CFA_REG_OFFSET;
611 break;
612
613 case DW_CFA_def_cfa_offset_sf:
614 insn_ptr = read_sleb128 (insn_ptr, insn_end, &offset);
615 fs->regs.cfa_offset = offset * fs->data_align;
616 /* cfa_how deliberately not set. */
617 break;
618
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 {
628 int size = register_size (gdbarch, 0);
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
643 case DW_CFA_GNU_args_size:
644 /* Ignored. */
645 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
646 break;
647
648 case DW_CFA_GNU_negative_offset_extended:
649 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
650 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
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
658 default:
659 internal_error (__FILE__, __LINE__, _("Unknown CFI encountered."));
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}
668\f
669
670/* Architecture-specific operations. */
671
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. */
678 void (*init_reg) (struct gdbarch *, int, struct dwarf2_frame_state_reg *,
679 struct frame_info *);
680
681 /* Check whether the THIS_FRAME is a signal trampoline. */
682 int (*signal_frame_p) (struct gdbarch *, struct frame_info *);
683
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);
687};
688
689/* Default architecture-specific register state initialization
690 function. */
691
692static void
693dwarf2_frame_default_init_reg (struct gdbarch *gdbarch, int regnum,
694 struct dwarf2_frame_state_reg *reg,
695 struct frame_info *this_frame)
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. */
722
723 if (regnum == gdbarch_pc_regnum (gdbarch))
724 reg->how = DWARF2_FRAME_REG_RA;
725 else if (regnum == gdbarch_sp_regnum (gdbarch))
726 reg->how = DWARF2_FRAME_REG_CFA;
727}
728
729/* Return a default for the architecture-specific operations. */
730
731static void *
732dwarf2_frame_init (struct obstack *obstack)
733{
734 struct dwarf2_frame_ops *ops;
735
736 ops = OBSTACK_ZALLOC (obstack, struct dwarf2_frame_ops);
737 ops->init_reg = dwarf2_frame_default_init_reg;
738 return ops;
739}
740
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,
747 struct dwarf2_frame_state_reg *,
748 struct frame_info *))
749{
750 struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);
751
752 ops->init_reg = init_reg;
753}
754
755/* Pre-initialize the register state REG for register REGNUM. */
756
757static void
758dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
759 struct dwarf2_frame_state_reg *reg,
760 struct frame_info *this_frame)
761{
762 struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);
763
764 ops->init_reg (gdbarch, regnum, reg, this_frame);
765}
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
781 THIS_FRAME. */
782
783static int
784dwarf2_frame_signal_frame_p (struct gdbarch *gdbarch,
785 struct frame_info *this_frame)
786{
787 struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);
788
789 if (ops->signal_frame_p == NULL)
790 return 0;
791 return ops->signal_frame_p (gdbarch, this_frame);
792}
793
794/* Set the architecture-specific adjustment of .eh_frame and .debug_frame
795 register numbers. */
796
797void
798dwarf2_frame_set_adjust_regnum (struct gdbarch *gdbarch,
799 int (*adjust_regnum) (struct gdbarch *,
800 int, int))
801{
802 struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);
803
804 ops->adjust_regnum = adjust_regnum;
805}
806
807/* Translate a .eh_frame register to DWARF register, or adjust a .debug_frame
808 register. */
809
810static int
811dwarf2_frame_adjust_regnum (struct gdbarch *gdbarch, int regnum, int eh_frame_p)
812{
813 struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);
814
815 if (ops->adjust_regnum == NULL)
816 return regnum;
817 return ops->adjust_regnum (gdbarch, regnum, eh_frame_p);
818}
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}
862\f
863
864struct dwarf2_frame_cache
865{
866 /* DWARF Call Frame Address. */
867 CORE_ADDR cfa;
868
869 /* Set if the return address column was marked as undefined. */
870 int undefined_retaddr;
871
872 /* Saved registers, indexed by GDB register number, not by DWARF
873 register number. */
874 struct dwarf2_frame_state_reg *reg;
875
876 /* Return address register. */
877 struct dwarf2_frame_state_reg retaddr_reg;
878
879 /* Target address size in bytes. */
880 int addr_size;
881};
882
883static struct dwarf2_frame_cache *
884dwarf2_frame_cache (struct frame_info *this_frame, void **this_cache)
885{
886 struct cleanup *old_chain;
887 struct gdbarch *gdbarch = get_frame_arch (this_frame);
888 const int num_regs = gdbarch_num_regs (gdbarch)
889 + gdbarch_num_pseudo_regs (gdbarch);
890 struct dwarf2_frame_cache *cache;
891 struct dwarf2_frame_state *fs;
892 struct dwarf2_fde *fde;
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
908 Note that if the next frame is never supposed to return (i.e. a call
909 to abort), the compiler might optimize away the instruction at
910 its return address. As a result the return address will
911 point at some random instruction, and the CFI for that
912 instruction is probably worthless to us. GCC's unwinder solves
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
917 handlers, sentinel frames, dummy frames). The function
918 get_frame_address_in_block does just this. It's not clear how
919 reliable the method is though; there is the potential for the
920 register state pre-call being different to that on return. */
921 fs->pc = get_frame_address_in_block (this_frame);
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;
931 cache->addr_size = fde->cie->addr_size;
932
933 /* Check for "quirks" - known bugs in producers. */
934 dwarf2_frame_find_quirks (fs, fde);
935
936 /* First decode all the insns in the CIE. */
937 execute_cfa_program (fde, fde->cie->initial_instructions,
938 fde->cie->end, this_frame, fs);
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. */
945 execute_cfa_program (fde, fde->instructions, fde->end, this_frame, fs);
946
947 /* Calculate the CFA. */
948 switch (fs->regs.cfa_how)
949 {
950 case CFA_REG_OFFSET:
951 cache->cfa = read_reg (this_frame, fs->regs.cfa_reg);
952 if (fs->armcc_cfa_offsets_reversed)
953 cache->cfa -= fs->regs.cfa_offset;
954 else
955 cache->cfa += fs->regs.cfa_offset;
956 break;
957
958 case CFA_EXP:
959 cache->cfa =
960 execute_stack_op (fs->regs.cfa_exp, fs->regs.cfa_exp_len,
961 cache->addr_size, this_frame, 0);
962 break;
963
964 default:
965 internal_error (__FILE__, __LINE__, _("Unknown CFA rule."));
966 }
967
968 /* Initialize the register state. */
969 {
970 int regnum;
971
972 for (regnum = 0; regnum < num_regs; regnum++)
973 dwarf2_frame_init_reg (gdbarch, regnum, &cache->reg[regnum], this_frame);
974 }
975
976 /* Go through the DWARF2 CFI generated table and save its register
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,
981 gdbarch_dwarf2_reg_to_regnum should be defined to return a number outside
982 the range [0, gdbarch_num_regs). */
983 {
984 int column; /* CFI speak for "register number". */
985
986 for (column = 0; column < fs->regs.num_regs; column++)
987 {
988 /* Use the GDB register number as the destination index. */
989 int regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, column);
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
996 of all debug info registers. If it doesn't, complain (but
997 not too loudly). It turns out that GCC assumes that an
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
1001 isn't sufficient; it only checks that all registers in the
1002 range [0 .. max column] are specified, and won't detect
1003 problems when a debug info register falls outside of the
1004 table. We need a way of iterating through all the valid
1005 DWARF2 register numbers. */
1006 if (fs->regs.reg[column].how == DWARF2_FRAME_REG_UNSPECIFIED)
1007 {
1008 if (cache->reg[regnum].how == DWARF2_FRAME_REG_UNSPECIFIED)
1009 complaint (&symfile_complaints, _("\
1010incomplete CFI data; unspecified registers (e.g., %s) at %s"),
1011 gdbarch_register_name (gdbarch, regnum),
1012 paddress (gdbarch, fs->pc));
1013 }
1014 else
1015 cache->reg[regnum] = fs->regs.reg[column];
1016 }
1017 }
1018
1019 /* Eliminate any DWARF2_FRAME_REG_RA rules, and save the information
1020 we need for evaluating DWARF2_FRAME_REG_RA_OFFSET rules. */
1021 {
1022 int regnum;
1023
1024 for (regnum = 0; regnum < num_regs; regnum++)
1025 {
1026 if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA
1027 || cache->reg[regnum].how == DWARF2_FRAME_REG_RA_OFFSET)
1028 {
1029 struct dwarf2_frame_state_reg *retaddr_reg =
1030 &fs->regs.reg[fs->retaddr_column];
1031
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.
1037 Incidentally, that's how we should treat a return
1038 address column specifying "same value" too. */
1039 if (fs->retaddr_column < fs->regs.num_regs
1040 && retaddr_reg->how != DWARF2_FRAME_REG_UNSPECIFIED
1041 && retaddr_reg->how != DWARF2_FRAME_REG_SAME_VALUE)
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 }
1048 else
1049 {
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 }
1060 }
1061 }
1062 }
1063 }
1064
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
1069 do_cleanups (old_chain);
1070
1071 *this_cache = cache;
1072 return cache;
1073}
1074
1075static void
1076dwarf2_frame_this_id (struct frame_info *this_frame, void **this_cache,
1077 struct frame_id *this_id)
1078{
1079 struct dwarf2_frame_cache *cache =
1080 dwarf2_frame_cache (this_frame, this_cache);
1081
1082 if (cache->undefined_retaddr)
1083 return;
1084
1085 (*this_id) = frame_id_build (cache->cfa, get_frame_func (this_frame));
1086}
1087
1088static struct value *
1089dwarf2_frame_prev_register (struct frame_info *this_frame, void **this_cache,
1090 int regnum)
1091{
1092 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1093 struct dwarf2_frame_cache *cache =
1094 dwarf2_frame_cache (this_frame, this_cache);
1095 CORE_ADDR addr;
1096 int realnum;
1097
1098 switch (cache->reg[regnum].how)
1099 {
1100 case DWARF2_FRAME_REG_UNDEFINED:
1101 /* If CFI explicitly specified that the value isn't defined,
1102 mark it as optimized away; the value isn't available. */
1103 return frame_unwind_got_optimized (this_frame, regnum);
1104
1105 case DWARF2_FRAME_REG_SAVED_OFFSET:
1106 addr = cache->cfa + cache->reg[regnum].loc.offset;
1107 return frame_unwind_got_memory (this_frame, regnum, addr);
1108
1109 case DWARF2_FRAME_REG_SAVED_REG:
1110 realnum
1111 = gdbarch_dwarf2_reg_to_regnum (gdbarch, cache->reg[regnum].loc.reg);
1112 return frame_unwind_got_register (this_frame, regnum, realnum);
1113
1114 case DWARF2_FRAME_REG_SAVED_EXP:
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);
1119
1120 case DWARF2_FRAME_REG_SAVED_VAL_OFFSET:
1121 addr = cache->cfa + cache->reg[regnum].loc.offset;
1122 return frame_unwind_got_constant (this_frame, regnum, addr);
1123
1124 case DWARF2_FRAME_REG_SAVED_VAL_EXP:
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);
1129
1130 case DWARF2_FRAME_REG_UNSPECIFIED:
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. */
1138 return frame_unwind_got_register (this_frame, regnum, regnum);
1139
1140 case DWARF2_FRAME_REG_SAME_VALUE:
1141 return frame_unwind_got_register (this_frame, regnum, regnum);
1142
1143 case DWARF2_FRAME_REG_CFA:
1144 return frame_unwind_got_address (this_frame, regnum, cache->cfa);
1145
1146 case DWARF2_FRAME_REG_CFA_OFFSET:
1147 addr = cache->cfa + cache->reg[regnum].loc.offset;
1148 return frame_unwind_got_address (this_frame, regnum, addr);
1149
1150 case DWARF2_FRAME_REG_RA_OFFSET:
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);
1156
1157 case DWARF2_FRAME_REG_FN:
1158 return cache->reg[regnum].loc.fn (this_frame, this_cache, regnum);
1159
1160 default:
1161 internal_error (__FILE__, __LINE__, _("Unknown register rule."));
1162 }
1163}
1164
1165static int
1166dwarf2_frame_sniffer (const struct frame_unwind *self,
1167 struct frame_info *this_frame, void **this_cache)
1168{
1169 /* Grab an address that is guarenteed to reside somewhere within the
1170 function. get_frame_pc(), with a no-return next function, can
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
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);
1177 struct dwarf2_fde *fde = dwarf2_frame_find_fde (&block_addr);
1178 if (!fde)
1179 return 0;
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
1185 if (fde->cie->signal_frame
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};
1210
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);
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
1228dwarf2_frame_base_address (struct frame_info *this_frame, void **this_cache)
1229{
1230 struct dwarf2_frame_cache *cache =
1231 dwarf2_frame_cache (this_frame, this_cache);
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 *
1245dwarf2_frame_base_sniffer (struct frame_info *this_frame)
1246{
1247 CORE_ADDR block_addr = get_frame_address_in_block (this_frame);
1248 if (dwarf2_frame_find_fde (&block_addr))
1249 return &dwarf2_frame_base;
1250
1251 return NULL;
1252}
1253\f
1254const struct objfile_data *dwarf2_frame_objfile_data;
1255
1256static unsigned int
1257read_1_byte (bfd *abfd, gdb_byte *buf)
1258{
1259 return bfd_get_8 (abfd, buf);
1260}
1261
1262static unsigned int
1263read_4_bytes (bfd *abfd, gdb_byte *buf)
1264{
1265 return bfd_get_32 (abfd, buf);
1266}
1267
1268static ULONGEST
1269read_8_bytes (bfd *abfd, gdb_byte *buf)
1270{
1271 return bfd_get_64 (abfd, buf);
1272}
1273
1274static ULONGEST
1275read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
1276{
1277 ULONGEST result;
1278 unsigned int num_read;
1279 int shift;
1280 gdb_byte byte;
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
1302read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
1303{
1304 LONGEST result;
1305 int shift;
1306 unsigned int num_read;
1307 gdb_byte byte;
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
1323 if (shift < 8 * sizeof (result) && (byte & 0x40))
1324 result |= -(((LONGEST)1) << shift);
1325
1326 *bytes_read_ptr = num_read;
1327
1328 return result;
1329}
1330
1331static ULONGEST
1332read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
1333{
1334 LONGEST result;
1335
1336 result = bfd_get_32 (abfd, buf);
1337 if (result == 0xffffffff)
1338 {
1339 result = bfd_get_64 (abfd, buf + 4);
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
1365static gdb_byte
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:
1377 internal_error (__FILE__, __LINE__, _("Unsupported address size"));
1378 }
1379}
1380
1381static CORE_ADDR
1382read_encoded_value (struct comp_unit *unit, gdb_byte encoding,
1383 int ptr_len, gdb_byte *buf, unsigned int *bytes_read_ptr,
1384 CORE_ADDR func_base)
1385{
1386 ptrdiff_t offset;
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__,
1393 _("Unsupported encoding: DW_EH_PE_indirect"));
1394
1395 *bytes_read_ptr = 0;
1396
1397 switch (encoding & 0x70)
1398 {
1399 case DW_EH_PE_absptr:
1400 base = 0;
1401 break;
1402 case DW_EH_PE_pcrel:
1403 base = bfd_get_section_vma (unit->abfd, unit->dwarf_frame_section);
1404 base += (buf - unit->dwarf_frame_buffer);
1405 break;
1406 case DW_EH_PE_datarel:
1407 base = unit->dbase;
1408 break;
1409 case DW_EH_PE_textrel:
1410 base = unit->tbase;
1411 break;
1412 case DW_EH_PE_funcrel:
1413 base = func_base;
1414 break;
1415 case DW_EH_PE_aligned:
1416 base = 0;
1417 offset = buf - unit->dwarf_frame_buffer;
1418 if ((offset % ptr_len) != 0)
1419 {
1420 *bytes_read_ptr = ptr_len - (offset % ptr_len);
1421 buf += *bytes_read_ptr;
1422 }
1423 break;
1424 default:
1425 internal_error (__FILE__, __LINE__, _("Invalid or unsupported encoding"));
1426 }
1427
1428 if ((encoding & 0x07) == 0x00)
1429 {
1430 encoding |= encoding_for_size (ptr_len);
1431 if (bfd_get_sign_extend_vma (unit->abfd))
1432 encoding |= DW_EH_PE_signed;
1433 }
1434
1435 switch (encoding & 0x0f)
1436 {
1437 case DW_EH_PE_uleb128:
1438 {
1439 ULONGEST value;
1440 gdb_byte *end_buf = buf + (sizeof (value) + 1) * 8 / 7;
1441 *bytes_read_ptr += read_uleb128 (buf, end_buf, &value) - buf;
1442 return base + value;
1443 }
1444 case DW_EH_PE_udata2:
1445 *bytes_read_ptr += 2;
1446 return (base + bfd_get_16 (unit->abfd, (bfd_byte *) buf));
1447 case DW_EH_PE_udata4:
1448 *bytes_read_ptr += 4;
1449 return (base + bfd_get_32 (unit->abfd, (bfd_byte *) buf));
1450 case DW_EH_PE_udata8:
1451 *bytes_read_ptr += 8;
1452 return (base + bfd_get_64 (unit->abfd, (bfd_byte *) buf));
1453 case DW_EH_PE_sleb128:
1454 {
1455 LONGEST value;
1456 gdb_byte *end_buf = buf + (sizeof (value) + 1) * 8 / 7;
1457 *bytes_read_ptr += read_sleb128 (buf, end_buf, &value) - buf;
1458 return base + value;
1459 }
1460 case DW_EH_PE_sdata2:
1461 *bytes_read_ptr += 2;
1462 return (base + bfd_get_signed_16 (unit->abfd, (bfd_byte *) buf));
1463 case DW_EH_PE_sdata4:
1464 *bytes_read_ptr += 4;
1465 return (base + bfd_get_signed_32 (unit->abfd, (bfd_byte *) buf));
1466 case DW_EH_PE_sdata8:
1467 *bytes_read_ptr += 8;
1468 return (base + bfd_get_signed_64 (unit->abfd, (bfd_byte *) buf));
1469 default:
1470 internal_error (__FILE__, __LINE__, _("Invalid or unsupported encoding"));
1471 }
1472}
1473\f
1474
1475static int
1476bsearch_cie_cmp (const void *key, const void *element)
1477{
1478 ULONGEST cie_pointer = *(ULONGEST *) key;
1479 struct dwarf2_cie *cie = *(struct dwarf2_cie **) element;
1480
1481 if (cie_pointer == cie->cie_pointer)
1482 return 0;
1483
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;
1492
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;
1497 return NULL;
1498}
1499
1500/* Add a pointer to new CIE to the CIE_TABLE, allocating space for it. */
1501static void
1502add_cie (struct dwarf2_cie_table *cie_table, struct dwarf2_cie *cie)
1503{
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;
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 {
1537 struct dwarf2_fde_table *fde_table;
1538 struct dwarf2_fde **p_fde;
1539 CORE_ADDR offset;
1540 CORE_ADDR seek_pc;
1541
1542 fde_table = objfile_data (objfile, dwarf2_frame_objfile_data);
1543 if (fde_table == NULL)
1544 continue;
1545
1546 gdb_assert (objfile->section_offsets);
1547 offset = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1548
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 }
1561 }
1562 return NULL;
1563}
1564
1565/* Add a pointer to new FDE to the FDE_TABLE, allocating space for it. */
1566static void
1567add_fde (struct dwarf2_fde_table *fde_table, struct dwarf2_fde *fde)
1568{
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;
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
1586static gdb_byte *decode_frame_entry (struct comp_unit *unit, gdb_byte *start,
1587 int eh_frame_p,
1588 struct dwarf2_cie_table *cie_table,
1589 struct dwarf2_fde_table *fde_table);
1590
1591/* Decode the next CIE or FDE. Return NULL if invalid input, otherwise
1592 the next byte to be processed. */
1593static gdb_byte *
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)
1597{
1598 struct gdbarch *gdbarch = get_objfile_arch (unit->objfile);
1599 gdb_byte *buf, *end;
1600 LONGEST length;
1601 unsigned int bytes_read;
1602 int dwarf64_p;
1603 ULONGEST cie_id;
1604 ULONGEST cie_pointer;
1605
1606 buf = start;
1607 length = read_initial_length (unit->abfd, buf, &bytes_read);
1608 buf += bytes_read;
1609 end = buf + length;
1610
1611 /* Are we still within the section? */
1612 if (end > unit->dwarf_frame_buffer + unit->dwarf_frame_size)
1613 return NULL;
1614
1615 if (length == 0)
1616 return end;
1617
1618 /* Distinguish between 32 and 64-bit encoded frame info. */
1619 dwarf64_p = (bytes_read == 12);
1620
1621 /* In a .eh_frame section, zero is used to distinguish CIEs from FDEs. */
1622 if (eh_frame_p)
1623 cie_id = 0;
1624 else if (dwarf64_p)
1625 cie_id = DW64_CIE_ID;
1626 else
1627 cie_id = DW_CIE_ID;
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;
1645 unsigned int cie_version;
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. */
1651 if (find_cie (cie_table, cie_pointer))
1652 return end;
1653
1654 cie = (struct dwarf2_cie *)
1655 obstack_alloc (&unit->objfile->objfile_obstack,
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
1661 depends on the target address size. */
1662 cie->encoding = DW_EH_PE_absptr;
1663
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. */
1669 cie->addr_size = gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT;
1670
1671 /* We'll determine the final value later, but we need to
1672 initialize it conservatively. */
1673 cie->signal_frame = 0;
1674
1675 /* Check version number. */
1676 cie_version = read_1_byte (unit->abfd, buf);
1677 if (cie_version != 1 && cie_version != 3)
1678 return NULL;
1679 cie->version = cie_version;
1680 buf += 1;
1681
1682 /* Interpret the interesting bits of the augmentation. */
1683 cie->augmentation = augmentation = (char *) buf;
1684 buf += (strlen (augmentation) + 1);
1685
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
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. */
1697 buf += gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT;
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
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);
1717 cie->return_address_register
1718 = dwarf2_frame_adjust_regnum (gdbarch,
1719 cie->return_address_register,
1720 eh_frame_p);
1721
1722 buf += bytes_read;
1723
1724 cie->saw_z_augmentation = (*augmentation == 'z');
1725 if (cie->saw_z_augmentation)
1726 {
1727 ULONGEST length;
1728
1729 length = read_unsigned_leb128 (unit->abfd, buf, &bytes_read);
1730 buf += bytes_read;
1731 if (buf > end)
1732 return NULL;
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 {
1757 /* Skip. Avoid indirection since we throw away the result. */
1758 gdb_byte encoding = (*buf++) & ~DW_EH_PE_indirect;
1759 read_encoded_value (unit, encoding, cie->addr_size,
1760 buf, &bytes_read, 0);
1761 buf += bytes_read;
1762 augmentation++;
1763 }
1764
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
1776 /* Otherwise we have an unknown augmentation. Assume that either
1777 there is no augmentation data, or we saw a 'z' prefix. */
1778 else
1779 {
1780 if (cie->initial_instructions)
1781 buf = cie->initial_instructions;
1782 break;
1783 }
1784 }
1785
1786 cie->initial_instructions = buf;
1787 cie->end = end;
1788 cie->unit = unit;
1789
1790 add_cie (cie_table, cie);
1791 }
1792 else
1793 {
1794 /* This is a FDE. */
1795 struct dwarf2_fde *fde;
1796
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. */
1801 if (eh_frame_p)
1802 {
1803 cie_pointer = buf - unit->dwarf_frame_buffer - cie_pointer;
1804 cie_pointer -= (dwarf64_p ? 8 : 4);
1805 }
1806
1807 /* In either case, validate the result is still within the section. */
1808 if (cie_pointer >= unit->dwarf_frame_size)
1809 return NULL;
1810
1811 fde = (struct dwarf2_fde *)
1812 obstack_alloc (&unit->objfile->objfile_obstack,
1813 sizeof (struct dwarf2_fde));
1814 fde->cie = find_cie (cie_table, cie_pointer);
1815 if (fde->cie == NULL)
1816 {
1817 decode_frame_entry (unit, unit->dwarf_frame_buffer + cie_pointer,
1818 eh_frame_p, cie_table, fde_table);
1819 fde->cie = find_cie (cie_table, cie_pointer);
1820 }
1821
1822 gdb_assert (fde->cie != NULL);
1823
1824 fde->initial_location =
1825 read_encoded_value (unit, fde->cie->encoding, fde->cie->addr_size,
1826 buf, &bytes_read, 0);
1827 buf += bytes_read;
1828
1829 fde->address_range =
1830 read_encoded_value (unit, fde->cie->encoding & 0x0f,
1831 fde->cie->addr_size, buf, &bytes_read, 0);
1832 buf += bytes_read;
1833
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;
1844 if (buf > end)
1845 return NULL;
1846 }
1847
1848 fde->instructions = buf;
1849 fde->end = end;
1850
1851 fde->eh_frame_p = eh_frame_p;
1852
1853 add_fde (fde_table, fde);
1854 }
1855
1856 return end;
1857}
1858
1859/* Read a CIE or FDE in BUF and decode it. */
1860static gdb_byte *
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)
1864{
1865 enum { NONE, ALIGN4, ALIGN8, FAIL } workaround = NONE;
1866 gdb_byte *ret;
1867 const char *msg;
1868 ptrdiff_t start_offset;
1869
1870 while (1)
1871 {
1872 ret = decode_frame_entry_1 (unit, start, eh_frame_p,
1873 cie_table, fde_table);
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,
1931 _("Corrupt data in %s:%s; align 4 workaround apparently succeeded"),
1932 unit->dwarf_frame_section->owner->filename,
1933 unit->dwarf_frame_section->name);
1934 break;
1935
1936 case ALIGN8:
1937 complaint (&symfile_complaints,
1938 _("Corrupt data in %s:%s; align 8 workaround apparently succeeded"),
1939 unit->dwarf_frame_section->owner->filename,
1940 unit->dwarf_frame_section->name);
1941 break;
1942
1943 default:
1944 complaint (&symfile_complaints,
1945 _("Corrupt data in %s:%s"),
1946 unit->dwarf_frame_section->owner->filename,
1947 unit->dwarf_frame_section->name);
1948 break;
1949 }
1950
1951 return ret;
1952}
1953\f
1954
1955/* Imported from dwarf2read.c. */
1956extern void dwarf2_get_section_info (struct objfile *, const char *, asection **,
1957 gdb_byte **, bfd_size_type *);
1958
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;
1964
1965 if (aa->initial_location == bb->initial_location)
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 }
1976
1977 return (aa->initial_location < bb->initial_location) ? -1 : 1;
1978}
1979
1980void
1981dwarf2_build_frame_info (struct objfile *objfile)
1982{
1983 struct comp_unit *unit;
1984 gdb_byte *frame_ptr;
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;
1993
1994 /* Build a minimal decoding of the DWARF2 compilation unit. */
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;
2001
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)
2007 {
2008 asection *got, *txt;
2009
2010 /* FIXME: kettenis/20030602: This is the DW_EH_PE_datarel base
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. */
2014 got = bfd_get_section_by_name (unit->abfd, ".got");
2015 if (got)
2016 unit->dbase = got->vma;
2017
2018 /* GCC emits the DW_EH_PE_textrel encoding type on sh and ia64
2019 so far. */
2020 txt = bfd_get_section_by_name (unit->abfd, ".text");
2021 if (txt)
2022 unit->tbase = txt->vma;
2023
2024 frame_ptr = unit->dwarf_frame_buffer;
2025 while (frame_ptr < unit->dwarf_frame_buffer + unit->dwarf_frame_size)
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 }
2036 }
2037
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)
2043 {
2044 frame_ptr = unit->dwarf_frame_buffer;
2045 while (frame_ptr < unit->dwarf_frame_buffer + unit->dwarf_frame_size)
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);
2090 }
2091}
2092
2093/* Provide a prototype to silence -Wmissing-prototypes. */
2094void _initialize_dwarf2_frame (void);
2095
2096void
2097_initialize_dwarf2_frame (void)
2098{
2099 dwarf2_frame_data = gdbarch_data_register_pre_init (dwarf2_frame_init);
2100 dwarf2_frame_objfile_data = register_objfile_data ();
2101}
This page took 0.040191 seconds and 4 git commands to generate.