2003-06-07 Andrew Cagney <cagney@redhat.com>
[deliverable/binutils-gdb.git] / gdb / dwarf2-frame.c
1 /* Frame unwinder for frames with DWARF Call Frame Information.
2
3 Copyright 2003 Free Software Foundation, Inc.
4
5 Contributed by Mark Kettenis.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
23
24 #include "defs.h"
25 #include "dwarf2expr.h"
26 #include "elf/dwarf2.h"
27 #include "frame.h"
28 #include "frame-base.h"
29 #include "frame-unwind.h"
30 #include "gdbcore.h"
31 #include "gdbtypes.h"
32 #include "symtab.h"
33 #include "objfiles.h"
34 #include "regcache.h"
35
36 #include "gdb_assert.h"
37 #include "gdb_string.h"
38
39 #include "dwarf2-frame.h"
40
41 /* Call Frame Information (CFI). */
42
43 /* Common Information Entry (CIE). */
44
45 struct dwarf2_cie
46 {
47 /* Offset into the .debug_frame section where this CIE was found.
48 Used to identify this CIE. */
49 ULONGEST cie_pointer;
50
51 /* Constant that is factored out of all advance location
52 instructions. */
53 ULONGEST code_alignment_factor;
54
55 /* Constants that is factored out of all offset instructions. */
56 LONGEST data_alignment_factor;
57
58 /* Return address column. */
59 ULONGEST return_address_register;
60
61 /* Instruction sequence to initialize a register set. */
62 unsigned char *initial_instructions;
63 unsigned char *end;
64
65 /* Encoding of addresses. */
66 unsigned char encoding;
67
68 /* True if a 'z' augmentation existed. */
69 unsigned char saw_z_augmentation;
70
71 struct dwarf2_cie *next;
72 };
73
74 /* Frame Description Entry (FDE). */
75
76 struct dwarf2_fde
77 {
78 /* CIE for this FDE. */
79 struct dwarf2_cie *cie;
80
81 /* First location associated with this FDE. */
82 CORE_ADDR initial_location;
83
84 /* Number of bytes of program instructions described by this FDE. */
85 CORE_ADDR address_range;
86
87 /* Instruction sequence. */
88 unsigned char *instructions;
89 unsigned char *end;
90
91 struct dwarf2_fde *next;
92 };
93
94 static struct dwarf2_fde *dwarf2_frame_find_fde (CORE_ADDR *pc);
95 \f
96
97 /* Structure describing a frame state. */
98
99 struct dwarf2_frame_state
100 {
101 /* Each register save state can be described in terms of a CFA slot,
102 another register, or a location expression. */
103 struct dwarf2_frame_state_reg_info
104 {
105 struct dwarf2_frame_state_reg
106 {
107 union {
108 LONGEST offset;
109 ULONGEST reg;
110 unsigned char *exp;
111 } loc;
112 ULONGEST exp_len;
113 enum {
114 REG_UNSAVED,
115 REG_SAVED_OFFSET,
116 REG_SAVED_REG,
117 REG_SAVED_EXP,
118 REG_UNMODIFIED
119 } how;
120 } *reg;
121 int num_regs;
122
123 /* Used to implement DW_CFA_remember_state. */
124 struct dwarf2_frame_state_reg_info *prev;
125 } regs;
126
127 LONGEST cfa_offset;
128 ULONGEST cfa_reg;
129 unsigned char *cfa_exp;
130 enum {
131 CFA_UNSET,
132 CFA_REG_OFFSET,
133 CFA_EXP
134 } cfa_how;
135
136 /* The PC described by the current frame state. */
137 CORE_ADDR pc;
138
139 /* Initial register set from the CIE.
140 Used to implement DW_CFA_restore. */
141 struct dwarf2_frame_state_reg_info initial;
142
143 /* The information we care about from the CIE. */
144 LONGEST data_align;
145 ULONGEST code_align;
146 ULONGEST retaddr_column;
147 };
148
149 /* Store the length the expression for the CFA in the `cfa_reg' field,
150 which is unused in that case. */
151 #define cfa_exp_len cfa_reg
152
153 /* Assert that the register set RS is large enough to store NUM_REGS
154 columns. If necessary, enlarge the register set. */
155
156 static void
157 dwarf2_frame_state_alloc_regs (struct dwarf2_frame_state_reg_info *rs,
158 int num_regs)
159 {
160 size_t size = sizeof (struct dwarf2_frame_state_reg);
161
162 if (num_regs <= rs->num_regs)
163 return;
164
165 rs->reg = (struct dwarf2_frame_state_reg *)
166 xrealloc (rs->reg, num_regs * size);
167
168 /* Initialize newly allocated registers. */
169 memset (rs->reg + rs->num_regs, 0, (num_regs - rs->num_regs) * size);
170 rs->num_regs = num_regs;
171 }
172
173 /* Copy the register columns in register set RS into newly allocated
174 memory and return a pointer to this newly created copy. */
175
176 static struct dwarf2_frame_state_reg *
177 dwarf2_frame_state_copy_regs (struct dwarf2_frame_state_reg_info *rs)
178 {
179 size_t size = rs->num_regs * sizeof (struct dwarf2_frame_state_reg_info);
180 struct dwarf2_frame_state_reg *reg;
181
182 reg = (struct dwarf2_frame_state_reg *) xmalloc (size);
183 memcpy (reg, rs->reg, size);
184
185 return reg;
186 }
187
188 /* Release the memory allocated to register set RS. */
189
190 static void
191 dwarf2_frame_state_free_regs (struct dwarf2_frame_state_reg_info *rs)
192 {
193 if (rs)
194 {
195 dwarf2_frame_state_free_regs (rs->prev);
196
197 xfree (rs->reg);
198 xfree (rs);
199 }
200 }
201
202 /* Release the memory allocated to the frame state FS. */
203
204 static void
205 dwarf2_frame_state_free (void *p)
206 {
207 struct dwarf2_frame_state *fs = p;
208
209 dwarf2_frame_state_free_regs (fs->initial.prev);
210 dwarf2_frame_state_free_regs (fs->regs.prev);
211 xfree (fs->initial.reg);
212 xfree (fs->regs.reg);
213 xfree (fs);
214 }
215 \f
216
217 /* Helper functions for execute_stack_op. */
218
219 static CORE_ADDR
220 read_reg (void *baton, int reg)
221 {
222 struct frame_info *next_frame = (struct frame_info *) baton;
223 int regnum;
224 char *buf;
225
226 regnum = DWARF2_REG_TO_REGNUM (reg);
227
228 buf = (char *) alloca (register_size (current_gdbarch, regnum));
229 frame_unwind_register (next_frame, regnum, buf);
230 return extract_typed_address (buf, builtin_type_void_data_ptr);
231 }
232
233 static void
234 read_mem (void *baton, char *buf, CORE_ADDR addr, size_t len)
235 {
236 read_memory (addr, buf, len);
237 }
238
239 static void
240 no_get_frame_base (void *baton, unsigned char **start, size_t *length)
241 {
242 internal_error (__FILE__, __LINE__,
243 "Support for DW_OP_fbreg is unimplemented");
244 }
245
246 static CORE_ADDR
247 no_get_tls_address (void *baton, CORE_ADDR offset)
248 {
249 internal_error (__FILE__, __LINE__,
250 "Support for DW_OP_GNU_push_tls_address is unimplemented");
251 }
252
253 static CORE_ADDR
254 execute_stack_op (unsigned char *exp, ULONGEST len,
255 struct frame_info *next_frame, CORE_ADDR initial)
256 {
257 struct dwarf_expr_context *ctx;
258 CORE_ADDR result;
259
260 ctx = new_dwarf_expr_context ();
261 ctx->baton = next_frame;
262 ctx->read_reg = read_reg;
263 ctx->read_mem = read_mem;
264 ctx->get_frame_base = no_get_frame_base;
265 ctx->get_tls_address = no_get_tls_address;
266
267 dwarf_expr_push (ctx, initial);
268 dwarf_expr_eval (ctx, exp, len);
269 result = dwarf_expr_fetch (ctx, 0);
270
271 if (ctx->in_reg)
272 result = read_reg (next_frame, result);
273
274 free_dwarf_expr_context (ctx);
275
276 return result;
277 }
278 \f
279
280 static void
281 execute_cfa_program (unsigned char *insn_ptr, unsigned char *insn_end,
282 struct frame_info *next_frame,
283 struct dwarf2_frame_state *fs)
284 {
285 CORE_ADDR pc = frame_pc_unwind (next_frame);
286 int bytes_read;
287
288 while (insn_ptr < insn_end && fs->pc <= pc)
289 {
290 unsigned char insn = *insn_ptr++;
291 ULONGEST utmp, reg;
292 LONGEST offset;
293
294 if ((insn & 0xc0) == DW_CFA_advance_loc)
295 fs->pc += (insn & 0x3f) * fs->code_align;
296 else if ((insn & 0xc0) == DW_CFA_offset)
297 {
298 reg = insn & 0x3f;
299 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
300 offset = utmp * fs->data_align;
301 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
302 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
303 fs->regs.reg[reg].loc.offset = offset;
304 }
305 else if ((insn & 0xc0) == DW_CFA_restore)
306 {
307 gdb_assert (fs->initial.reg);
308 reg = insn & 0x3f;
309 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
310 fs->regs.reg[reg] = fs->initial.reg[reg];
311 }
312 else
313 {
314 switch (insn)
315 {
316 case DW_CFA_set_loc:
317 fs->pc = dwarf2_read_address (insn_ptr, insn_end, &bytes_read);
318 insn_ptr += bytes_read;
319 break;
320
321 case DW_CFA_advance_loc1:
322 utmp = extract_unsigned_integer (insn_ptr, 1);
323 fs->pc += utmp * fs->code_align;
324 insn_ptr++;
325 break;
326 case DW_CFA_advance_loc2:
327 utmp = extract_unsigned_integer (insn_ptr, 2);
328 fs->pc += utmp * fs->code_align;
329 insn_ptr += 2;
330 break;
331 case DW_CFA_advance_loc4:
332 utmp = extract_unsigned_integer (insn_ptr, 4);
333 fs->pc += utmp * fs->code_align;
334 insn_ptr += 4;
335 break;
336
337 case DW_CFA_offset_extended:
338 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
339 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
340 offset = utmp * fs->data_align;
341 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
342 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
343 fs->regs.reg[reg].loc.offset = offset;
344 break;
345
346 case DW_CFA_restore_extended:
347 gdb_assert (fs->initial.reg);
348 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
349 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
350 fs->regs.reg[reg] = fs->initial.reg[reg];
351 break;
352
353 case DW_CFA_undefined:
354 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
355 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
356 fs->regs.reg[reg].how = REG_UNSAVED;
357 break;
358
359 case DW_CFA_same_value:
360 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
361 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
362 fs->regs.reg[reg].how = REG_UNMODIFIED;
363 break;
364
365 case DW_CFA_register:
366 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
367 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
368 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
369 fs->regs.reg[reg].loc.reg = utmp;
370 break;
371
372 case DW_CFA_remember_state:
373 {
374 struct dwarf2_frame_state_reg_info *new_rs;
375
376 new_rs = XMALLOC (struct dwarf2_frame_state_reg_info);
377 *new_rs = fs->regs;
378 fs->regs.reg = dwarf2_frame_state_copy_regs (&fs->regs);
379 fs->regs.prev = new_rs;
380 }
381 break;
382
383 case DW_CFA_restore_state:
384 {
385 struct dwarf2_frame_state_reg_info *old_rs = fs->regs.prev;
386
387 gdb_assert (old_rs);
388
389 xfree (fs->regs.reg);
390 fs->regs = *old_rs;
391 xfree (old_rs);
392 }
393 break;
394
395 case DW_CFA_def_cfa:
396 insn_ptr = read_uleb128 (insn_ptr, insn_end, &fs->cfa_reg);
397 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
398 fs->cfa_offset = utmp;
399 fs->cfa_how = CFA_REG_OFFSET;
400 break;
401
402 case DW_CFA_def_cfa_register:
403 insn_ptr = read_uleb128 (insn_ptr, insn_end, &fs->cfa_reg);
404 fs->cfa_how = CFA_REG_OFFSET;
405 break;
406
407 case DW_CFA_def_cfa_offset:
408 insn_ptr = read_uleb128 (insn_ptr, insn_end, &fs->cfa_offset);
409 /* cfa_how deliberately not set. */
410 break;
411
412 case DW_CFA_def_cfa_expression:
413 insn_ptr = read_uleb128 (insn_ptr, insn_end, &fs->cfa_exp_len);
414 fs->cfa_exp = insn_ptr;
415 fs->cfa_how = CFA_EXP;
416 insn_ptr += fs->cfa_exp_len;
417 break;
418
419 case DW_CFA_expression:
420 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
421 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
422 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
423 fs->regs.reg[reg].loc.exp = insn_ptr;
424 fs->regs.reg[reg].exp_len = utmp;
425 fs->regs.reg[reg].how = REG_SAVED_EXP;
426 insn_ptr += utmp;
427 break;
428
429 case DW_CFA_nop:
430 break;
431
432 case DW_CFA_GNU_args_size:
433 /* Ignored. */
434 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
435 break;
436
437 default:
438 internal_error (__FILE__, __LINE__, "Unknown CFI encountered.");
439 }
440 }
441 }
442
443 /* Don't allow remember/restore between CIE and FDE programs. */
444 dwarf2_frame_state_free_regs (fs->regs.prev);
445 fs->regs.prev = NULL;
446 }
447
448 struct dwarf2_frame_cache
449 {
450 /* DWARF Call Frame Address. */
451 CORE_ADDR cfa;
452
453 /* Saved registers, indexed by GDB register number, not by DWARF
454 register number. */
455 struct dwarf2_frame_state_reg *reg;
456 };
457
458 struct dwarf2_frame_cache *
459 dwarf2_frame_cache (struct frame_info *next_frame, void **this_cache)
460 {
461 struct cleanup *old_chain;
462 int num_regs = NUM_REGS + NUM_PSEUDO_REGS;
463 struct dwarf2_frame_cache *cache;
464 struct dwarf2_frame_state *fs;
465 struct dwarf2_fde *fde;
466 int reg;
467
468 if (*this_cache)
469 return *this_cache;
470
471 /* Allocate a new cache. */
472 cache = FRAME_OBSTACK_ZALLOC (struct dwarf2_frame_cache);
473 cache->reg = FRAME_OBSTACK_CALLOC (num_regs, struct dwarf2_frame_state_reg);
474
475 /* Allocate and initialize the frame state. */
476 fs = XMALLOC (struct dwarf2_frame_state);
477 memset (fs, 0, sizeof (struct dwarf2_frame_state));
478 old_chain = make_cleanup (dwarf2_frame_state_free, fs);
479
480 /* Unwind the PC.
481
482 Note that if NEXT_FRAME is never supposed to return (i.e. a call
483 to abort), the compiler might optimize away the instruction at
484 NEXT_FRAME's return address. As a result the return address will
485 point at some random instruction, and the CFI for that
486 instruction is probably wortless to us. GCC's unwinder solves
487 this problem by substracting 1 from the return address to get an
488 address in the middle of a presumed call instruction (or the
489 instruction in the associated delay slot). This should only be
490 done for "normal" frames and not for resume-type frames (signal
491 handlers, sentinel frames, dummy frames).
492
493 We don't do what GCC's does here (yet). It's not clear how
494 reliable the method is. There's also a problem with finding the
495 right FDE; see the comment in dwarf_frame_p. If dwarf_frame_p
496 selected this frame unwinder because it found the FDE for the
497 next function, using the adjusted return address might not yield
498 a FDE at all. The problem isn't specific to DWARF CFI; other
499 unwinders loose in similar ways. Therefore it's probably
500 acceptable to leave things slightly broken for now. */
501 fs->pc = frame_pc_unwind (next_frame);
502
503 /* Find the correct FDE. */
504 fde = dwarf2_frame_find_fde (&fs->pc);
505 gdb_assert (fde != NULL);
506
507 /* Extract any interesting information from the CIE. */
508 fs->data_align = fde->cie->data_alignment_factor;
509 fs->code_align = fde->cie->code_alignment_factor;
510 fs->retaddr_column = fde->cie->return_address_register;
511
512 /* First decode all the insns in the CIE. */
513 execute_cfa_program (fde->cie->initial_instructions,
514 fde->cie->end, next_frame, fs);
515
516 /* Save the initialized register set. */
517 fs->initial = fs->regs;
518 fs->initial.reg = dwarf2_frame_state_copy_regs (&fs->regs);
519
520 /* Then decode the insns in the FDE up to our target PC. */
521 execute_cfa_program (fde->instructions, fde->end, next_frame, fs);
522
523 /* Caclulate the CFA. */
524 switch (fs->cfa_how)
525 {
526 case CFA_REG_OFFSET:
527 cache->cfa = read_reg (next_frame, fs->cfa_reg);
528 cache->cfa += fs->cfa_offset;
529 break;
530
531 case CFA_EXP:
532 cache->cfa =
533 execute_stack_op (fs->cfa_exp, fs->cfa_exp_len, next_frame, 0);
534 break;
535
536 default:
537 internal_error (__FILE__, __LINE__, "Unknown CFA rule.");
538 }
539
540 /* Save the register info in the cache. */
541 for (reg = 0; reg < fs->regs.num_regs; reg++)
542 {
543 int regnum;
544
545 /* Skip the return address column. */
546 if (reg == fs->retaddr_column)
547 continue;
548
549 /* Use the GDB register number as index. */
550 regnum = DWARF2_REG_TO_REGNUM (reg);
551
552 if (regnum >= 0 && regnum < num_regs)
553 cache->reg[regnum] = fs->regs.reg[reg];
554 }
555
556 /* Store the location of the return addess. If the return address
557 column (adjusted) is not the same as gdb's PC_REGNUM, then this
558 implies a copy from the ra column register. */
559 if (fs->retaddr_column < fs->regs.num_regs
560 && fs->regs.reg[fs->retaddr_column].how != REG_UNSAVED)
561 cache->reg[PC_REGNUM] = fs->regs.reg[fs->retaddr_column];
562 else
563 {
564 reg = DWARF2_REG_TO_REGNUM (fs->retaddr_column);
565 if (reg != PC_REGNUM)
566 {
567 cache->reg[PC_REGNUM].loc.reg = reg;
568 cache->reg[PC_REGNUM].how = REG_SAVED_REG;
569 }
570 }
571
572 do_cleanups (old_chain);
573
574 *this_cache = cache;
575 return cache;
576 }
577
578 static void
579 dwarf2_frame_this_id (struct frame_info *next_frame, void **this_cache,
580 struct frame_id *this_id)
581 {
582 struct dwarf2_frame_cache *cache =
583 dwarf2_frame_cache (next_frame, this_cache);
584
585 (*this_id) = frame_id_build (cache->cfa, frame_func_unwind (next_frame));
586 }
587
588 static void
589 dwarf2_frame_prev_register (struct frame_info *next_frame, void **this_cache,
590 int regnum, int *optimizedp,
591 enum lval_type *lvalp, CORE_ADDR *addrp,
592 int *realnump, void *valuep)
593 {
594 struct dwarf2_frame_cache *cache =
595 dwarf2_frame_cache (next_frame, this_cache);
596
597 switch (cache->reg[regnum].how)
598 {
599 case REG_UNSAVED:
600 *optimizedp = 1;
601 *lvalp = not_lval;
602 *addrp = 0;
603 *realnump = -1;
604 if (regnum == SP_REGNUM)
605 {
606 /* GCC defines the CFA as the value of the stack pointer
607 just before the call instruction is executed. Do other
608 compilers use the same definition? */
609 *optimizedp = 0;
610 if (valuep)
611 {
612 /* Store the value. */
613 store_typed_address (valuep, builtin_type_void_data_ptr,
614 cache->cfa);
615 }
616 }
617 else if (valuep)
618 {
619 /* In some cases, for example %eflags on the i386, we have
620 to provide a sane value, even though this register wasn't
621 saved. Assume we can get it from NEXT_FRAME. */
622 frame_unwind_register (next_frame, regnum, valuep);
623 }
624 break;
625
626 case REG_SAVED_OFFSET:
627 *optimizedp = 0;
628 *lvalp = lval_memory;
629 *addrp = cache->cfa + cache->reg[regnum].loc.offset;
630 *realnump = -1;
631 if (valuep)
632 {
633 /* Read the value in from memory. */
634 read_memory (*addrp, valuep,
635 register_size (current_gdbarch, regnum));
636 }
637 break;
638
639 case REG_SAVED_REG:
640 regnum = DWARF2_REG_TO_REGNUM (cache->reg[regnum].loc.reg);
641 frame_register_unwind (next_frame, regnum,
642 optimizedp, lvalp, addrp, realnump, valuep);
643 break;
644
645 case REG_SAVED_EXP:
646 *optimizedp = 0;
647 *lvalp = lval_memory;
648 *addrp = execute_stack_op (cache->reg[regnum].loc.exp,
649 cache->reg[regnum].exp_len,
650 next_frame, cache->cfa);
651 *realnump = -1;
652 if (valuep)
653 {
654 /* Read the value in from memory. */
655 read_memory (*addrp, valuep,
656 register_size (current_gdbarch, regnum));
657 }
658 break;
659
660 case REG_UNMODIFIED:
661 frame_register_unwind (next_frame, regnum,
662 optimizedp, lvalp, addrp, realnump, valuep);
663 break;
664
665 default:
666 internal_error (__FILE__, __LINE__, "Unknown register rule.");
667 }
668 }
669
670 static const struct frame_unwind dwarf2_frame_unwind =
671 {
672 NORMAL_FRAME,
673 dwarf2_frame_this_id,
674 dwarf2_frame_prev_register
675 };
676
677 const struct frame_unwind *
678 dwarf2_frame_p (CORE_ADDR pc)
679 {
680 /* The way GDB works, this function can be called with PC just after
681 the last instruction of the function we're supposed to return the
682 unwind methods for. In that case we won't find the correct FDE;
683 instead we find the FDE for the next function, or we won't find
684 an FDE at all. There is a possible solution (see the comment in
685 dwarf2_frame_cache), GDB doesn't pass us enough information to
686 implement it. */
687 if (dwarf2_frame_find_fde (&pc))
688 return &dwarf2_frame_unwind;
689
690 return NULL;
691 }
692 \f
693
694 /* There is no explicitly defined relationship between the CFA and the
695 location of frame's local variables and arguments/parameters.
696 Therefore, frame base methods on this page should probably only be
697 used as a last resort, just to avoid printing total garbage as a
698 response to the "info frame" command. */
699
700 static CORE_ADDR
701 dwarf2_frame_base_address (struct frame_info *next_frame, void **this_cache)
702 {
703 struct dwarf2_frame_cache *cache =
704 dwarf2_frame_cache (next_frame, this_cache);
705
706 return cache->cfa;
707 }
708
709 static const struct frame_base dwarf2_frame_base =
710 {
711 &dwarf2_frame_unwind,
712 dwarf2_frame_base_address,
713 dwarf2_frame_base_address,
714 dwarf2_frame_base_address
715 };
716
717 const struct frame_base *
718 dwarf2_frame_base_p (CORE_ADDR pc)
719 {
720 if (dwarf2_frame_find_fde (&pc))
721 return &dwarf2_frame_base;
722
723 return NULL;
724 }
725 \f
726 /* A minimal decoding of DWARF2 compilation units. We only decode
727 what's needed to get to the call frame information. */
728
729 struct comp_unit
730 {
731 /* Keep the bfd convenient. */
732 bfd *abfd;
733
734 struct objfile *objfile;
735
736 /* Linked list of CIEs for this object. */
737 struct dwarf2_cie *cie;
738
739 /* Address size for this unit - from unit header. */
740 unsigned char addr_size;
741
742 /* Pointer to the .debug_frame section loaded into memory. */
743 char *dwarf_frame_buffer;
744
745 /* Length of the loaded .debug_frame section. */
746 unsigned long dwarf_frame_size;
747
748 /* Pointer to the .debug_frame section. */
749 asection *dwarf_frame_section;
750
751 /* Base for DW_EH_PE_datarel encodings. */
752 bfd_vma dbase;
753 };
754
755 static unsigned int
756 read_1_byte (bfd *bfd, char *buf)
757 {
758 return bfd_get_8 (abfd, (bfd_byte *) buf);
759 }
760
761 static unsigned int
762 read_4_bytes (bfd *abfd, char *buf)
763 {
764 return bfd_get_32 (abfd, (bfd_byte *) buf);
765 }
766
767 static ULONGEST
768 read_8_bytes (bfd *abfd, char *buf)
769 {
770 return bfd_get_64 (abfd, (bfd_byte *) buf);
771 }
772
773 static ULONGEST
774 read_unsigned_leb128 (bfd *abfd, char *buf, unsigned int *bytes_read_ptr)
775 {
776 ULONGEST result;
777 unsigned int num_read;
778 int shift;
779 unsigned char byte;
780
781 result = 0;
782 shift = 0;
783 num_read = 0;
784
785 do
786 {
787 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
788 buf++;
789 num_read++;
790 result |= ((byte & 0x7f) << shift);
791 shift += 7;
792 }
793 while (byte & 0x80);
794
795 *bytes_read_ptr = num_read;
796
797 return result;
798 }
799
800 static LONGEST
801 read_signed_leb128 (bfd *abfd, char *buf, unsigned int *bytes_read_ptr)
802 {
803 LONGEST result;
804 int shift;
805 unsigned int num_read;
806 unsigned char byte;
807
808 result = 0;
809 shift = 0;
810 num_read = 0;
811
812 do
813 {
814 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
815 buf++;
816 num_read++;
817 result |= ((byte & 0x7f) << shift);
818 shift += 7;
819 }
820 while (byte & 0x80);
821
822 if ((shift < 32) && (byte & 0x40))
823 result |= -(1 << shift);
824
825 *bytes_read_ptr = num_read;
826
827 return result;
828 }
829
830 static ULONGEST
831 read_initial_length (bfd *abfd, char *buf, unsigned int *bytes_read_ptr)
832 {
833 LONGEST result;
834
835 result = bfd_get_32 (abfd, (bfd_byte *) buf);
836 if (result == 0xffffffff)
837 {
838 result = bfd_get_64 (abfd, (bfd_byte *) buf + 4);
839 *bytes_read_ptr = 12;
840 }
841 else
842 *bytes_read_ptr = 4;
843
844 return result;
845 }
846 \f
847
848 /* Pointer encoding helper functions. */
849
850 /* GCC supports exception handling based on DWARF2 CFI. However, for
851 technical reasons, it encodes addresses in its FDE's in a different
852 way. Several "pointer encodings" are supported. The encoding
853 that's used for a particular FDE is determined by the 'R'
854 augmentation in the associated CIE. The argument of this
855 augmentation is a single byte.
856
857 The address can be encoded as 2 bytes, 4 bytes, 8 bytes, or as a
858 LEB128. This is encoded in bits 0, 1 and 2. Bit 3 encodes whether
859 the address is signed or unsigned. Bits 4, 5 and 6 encode how the
860 address should be interpreted (absolute, relative to the current
861 position in the FDE, ...). Bit 7, indicates that the address
862 should be dereferenced. */
863
864 static unsigned char
865 encoding_for_size (unsigned int size)
866 {
867 switch (size)
868 {
869 case 2:
870 return DW_EH_PE_udata2;
871 case 4:
872 return DW_EH_PE_udata4;
873 case 8:
874 return DW_EH_PE_udata8;
875 default:
876 internal_error (__FILE__, __LINE__, "Unsupported address size");
877 }
878 }
879
880 static unsigned int
881 size_of_encoded_value (unsigned char encoding)
882 {
883 if (encoding == DW_EH_PE_omit)
884 return 0;
885
886 switch (encoding & 0x07)
887 {
888 case DW_EH_PE_absptr:
889 return TYPE_LENGTH (builtin_type_void_data_ptr);
890 case DW_EH_PE_udata2:
891 return 2;
892 case DW_EH_PE_udata4:
893 return 4;
894 case DW_EH_PE_udata8:
895 return 8;
896 default:
897 internal_error (__FILE__, __LINE__, "Invalid or unsupported encoding");
898 }
899 }
900
901 static CORE_ADDR
902 read_encoded_value (struct comp_unit *unit, unsigned char encoding,
903 char *buf, unsigned int *bytes_read_ptr)
904 {
905 CORE_ADDR base;
906
907 /* GCC currently doesn't generate DW_EH_PE_indirect encodings for
908 FDE's. */
909 if (encoding & DW_EH_PE_indirect)
910 internal_error (__FILE__, __LINE__,
911 "Unsupported encoding: DW_EH_PE_indirect");
912
913 switch (encoding & 0x70)
914 {
915 case DW_EH_PE_absptr:
916 base = 0;
917 break;
918 case DW_EH_PE_pcrel:
919 base = bfd_get_section_vma (unit->bfd, unit->dwarf_frame_section);
920 base += (buf - unit->dwarf_frame_buffer);
921 break;
922 case DW_EH_PE_datarel:
923 base = unit->dbase;
924 break;
925 default:
926 internal_error (__FILE__, __LINE__, "Invalid or unsupported encoding");
927 }
928
929 if ((encoding & 0x0f) == 0x00)
930 encoding |= encoding_for_size (TYPE_LENGTH(builtin_type_void_data_ptr));
931
932 switch (encoding & 0x0f)
933 {
934 case DW_EH_PE_udata2:
935 *bytes_read_ptr = 2;
936 return (base + bfd_get_16 (unit->abfd, (bfd_byte *) buf));
937 case DW_EH_PE_udata4:
938 *bytes_read_ptr = 4;
939 return (base + bfd_get_32 (unit->abfd, (bfd_byte *) buf));
940 case DW_EH_PE_udata8:
941 *bytes_read_ptr = 8;
942 return (base + bfd_get_64 (unit->abfd, (bfd_byte *) buf));
943 case DW_EH_PE_sdata2:
944 *bytes_read_ptr = 2;
945 return (base + bfd_get_signed_16 (unit->abfd, (bfd_byte *) buf));
946 case DW_EH_PE_sdata4:
947 *bytes_read_ptr = 4;
948 return (base + bfd_get_signed_32 (unit->abfd, (bfd_byte *) buf));
949 case DW_EH_PE_sdata8:
950 *bytes_read_ptr = 8;
951 return (base + bfd_get_signed_64 (unit->abfd, (bfd_byte *) buf));
952 default:
953 internal_error (__FILE__, __LINE__, "Invalid or unsupported encoding");
954 }
955 }
956 \f
957
958 /* GCC uses a single CIE for all FDEs in a .debug_frame section.
959 That's why we use a simple linked list here. */
960
961 static struct dwarf2_cie *
962 find_cie (struct comp_unit *unit, ULONGEST cie_pointer)
963 {
964 struct dwarf2_cie *cie = unit->cie;
965
966 while (cie)
967 {
968 if (cie->cie_pointer == cie_pointer)
969 return cie;
970
971 cie = cie->next;
972 }
973
974 return NULL;
975 }
976
977 static void
978 add_cie (struct comp_unit *unit, struct dwarf2_cie *cie)
979 {
980 cie->next = unit->cie;
981 unit->cie = cie;
982 }
983
984 /* Find the FDE for *PC. Return a pointer to the FDE, and store the
985 inital location associated with it into *PC. */
986
987 static struct dwarf2_fde *
988 dwarf2_frame_find_fde (CORE_ADDR *pc)
989 {
990 struct objfile *objfile;
991
992 ALL_OBJFILES (objfile)
993 {
994 struct dwarf2_fde *fde;
995 CORE_ADDR offset;
996
997 offset = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
998
999 fde = objfile->sym_private;
1000 while (fde)
1001 {
1002 if (*pc >= fde->initial_location + offset
1003 && *pc < fde->initial_location + offset + fde->address_range)
1004 {
1005 *pc = fde->initial_location + offset;
1006 return fde;
1007 }
1008
1009 fde = fde->next;
1010 }
1011 }
1012
1013 return NULL;
1014 }
1015
1016 static void
1017 add_fde (struct comp_unit *unit, struct dwarf2_fde *fde)
1018 {
1019 fde->next = unit->objfile->sym_private;
1020 unit->objfile->sym_private = fde;
1021 }
1022
1023 #ifdef CC_HAS_LONG_LONG
1024 #define DW64_CIE_ID 0xffffffffffffffffULL
1025 #else
1026 #define DW64_CIE_ID ~0
1027 #endif
1028
1029 /* Read a CIE or FDE in BUF and decode it. */
1030
1031 static char *
1032 decode_frame_entry (struct comp_unit *unit, char *buf, int eh_frame_p)
1033 {
1034 LONGEST length;
1035 unsigned int bytes_read;
1036 int dwarf64_p = 0;
1037 ULONGEST cie_id = DW_CIE_ID;
1038 ULONGEST cie_pointer;
1039 char *start = buf;
1040 char *end;
1041
1042 length = read_initial_length (unit->abfd, buf, &bytes_read);
1043 buf += bytes_read;
1044 end = buf + length;
1045
1046 if (length == 0)
1047 return end;
1048
1049 if (bytes_read == 12)
1050 dwarf64_p = 1;
1051
1052 /* In a .eh_frame section, zero is used to distinguish CIEs from
1053 FDEs. */
1054 if (eh_frame_p)
1055 cie_id = 0;
1056 else if (dwarf64_p)
1057 cie_id = DW64_CIE_ID;
1058
1059 if (dwarf64_p)
1060 {
1061 cie_pointer = read_8_bytes (unit->abfd, buf);
1062 buf += 8;
1063 }
1064 else
1065 {
1066 cie_pointer = read_4_bytes (unit->abfd, buf);
1067 buf += 4;
1068 }
1069
1070 if (cie_pointer == cie_id)
1071 {
1072 /* This is a CIE. */
1073 struct dwarf2_cie *cie;
1074 char *augmentation;
1075
1076 /* Record the offset into the .debug_frame section of this CIE. */
1077 cie_pointer = start - unit->dwarf_frame_buffer;
1078
1079 /* Check whether we've already read it. */
1080 if (find_cie (unit, cie_pointer))
1081 return end;
1082
1083 cie = (struct dwarf2_cie *)
1084 obstack_alloc (&unit->objfile->psymbol_obstack,
1085 sizeof (struct dwarf2_cie));
1086 cie->initial_instructions = NULL;
1087 cie->cie_pointer = cie_pointer;
1088
1089 /* The encoding for FDE's in a normal .debug_frame section
1090 depends on the target address size as specified in the
1091 Compilation Unit Header. */
1092 cie->encoding = encoding_for_size (unit->addr_size);
1093
1094 /* Check version number. */
1095 gdb_assert (read_1_byte (unit->abfd, buf) == DW_CIE_VERSION);
1096 buf += 1;
1097
1098 /* Interpret the interesting bits of the augmentation. */
1099 augmentation = buf;
1100 buf = augmentation + strlen (augmentation) + 1;
1101
1102 /* The GCC 2.x "eh" augmentation has a pointer immediately
1103 following the augmentation string, so it must be handled
1104 first. */
1105 if (augmentation[0] == 'e' && augmentation[1] == 'h')
1106 {
1107 /* Skip. */
1108 buf += TYPE_LENGTH (builtin_type_void_data_ptr);
1109 augmentation += 2;
1110 }
1111
1112 cie->code_alignment_factor =
1113 read_unsigned_leb128 (unit->abfd, buf, &bytes_read);
1114 buf += bytes_read;
1115
1116 cie->data_alignment_factor =
1117 read_signed_leb128 (unit->abfd, buf, &bytes_read);
1118 buf += bytes_read;
1119
1120 cie->return_address_register = read_1_byte (unit->abfd, buf);
1121 buf += 1;
1122
1123 cie->saw_z_augmentation = (*augmentation == 'z');
1124 if (cie->saw_z_augmentation)
1125 {
1126 ULONGEST length;
1127
1128 length = read_unsigned_leb128 (unit->abfd, buf, &bytes_read);
1129 buf += bytes_read;
1130 cie->initial_instructions = buf + length;
1131 augmentation++;
1132 }
1133
1134 while (*augmentation)
1135 {
1136 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
1137 if (*augmentation == 'L')
1138 {
1139 /* Skip. */
1140 buf++;
1141 augmentation++;
1142 }
1143
1144 /* "R" indicates a byte indicating how FDE addresses are encoded. */
1145 else if (*augmentation == 'R')
1146 {
1147 cie->encoding = *buf++;
1148 augmentation++;
1149 }
1150
1151 /* "P" indicates a personality routine in the CIE augmentation. */
1152 else if (*augmentation == 'P')
1153 {
1154 /* Skip. */
1155 buf += size_of_encoded_value (*buf++);
1156 augmentation++;
1157 }
1158
1159 /* Otherwise we have an unknown augmentation.
1160 Bail out unless we saw a 'z' prefix. */
1161 else
1162 {
1163 if (cie->initial_instructions == NULL)
1164 return end;
1165
1166 /* Skip unknown augmentations. */
1167 buf = cie->initial_instructions;
1168 break;
1169 }
1170 }
1171
1172 cie->initial_instructions = buf;
1173 cie->end = end;
1174
1175 add_cie (unit, cie);
1176 }
1177 else
1178 {
1179 /* This is a FDE. */
1180 struct dwarf2_fde *fde;
1181
1182 if (eh_frame_p)
1183 {
1184 /* In an .eh_frame section, the CIE pointer is the delta
1185 between the address within the FDE where the CIE pointer
1186 is stored and the address of the CIE. Convert it to an
1187 offset into the .eh_frame section. */
1188 cie_pointer = buf - unit->dwarf_frame_buffer - cie_pointer;
1189 cie_pointer -= (dwarf64_p ? 8 : 4);
1190 }
1191
1192 fde = (struct dwarf2_fde *)
1193 obstack_alloc (&unit->objfile->psymbol_obstack,
1194 sizeof (struct dwarf2_fde));
1195 fde->cie = find_cie (unit, cie_pointer);
1196 if (fde->cie == NULL)
1197 {
1198 decode_frame_entry (unit, unit->dwarf_frame_buffer + cie_pointer,
1199 eh_frame_p);
1200 fde->cie = find_cie (unit, cie_pointer);
1201 }
1202
1203 gdb_assert (fde->cie != NULL);
1204
1205 fde->initial_location =
1206 read_encoded_value (unit, fde->cie->encoding, buf, &bytes_read);
1207 buf += bytes_read;
1208
1209 fde->address_range =
1210 read_encoded_value (unit, fde->cie->encoding & 0x0f, buf, &bytes_read);
1211 buf += bytes_read;
1212
1213 /* A 'z' augmentation in the CIE implies the presence of an
1214 augmentation field in the FDE as well. The only thing known
1215 to be in here at present is the LSDA entry for EH. So we
1216 can skip the whole thing. */
1217 if (fde->cie->saw_z_augmentation)
1218 {
1219 ULONGEST length;
1220
1221 length = read_unsigned_leb128 (unit->abfd, buf, &bytes_read);
1222 buf += bytes_read + length;
1223 }
1224
1225 fde->instructions = buf;
1226 fde->end = end;
1227
1228 add_fde (unit, fde);
1229 }
1230
1231 return end;
1232 }
1233 \f
1234
1235 /* FIXME: kettenis/20030504: This still needs to be integrated with
1236 dwarf2read.c in a better way. */
1237
1238 /* Imported from dwarf2read.c. */
1239 extern file_ptr dwarf_frame_offset;
1240 extern unsigned int dwarf_frame_size;
1241 extern asection *dwarf_frame_section;
1242 extern file_ptr dwarf_eh_frame_offset;
1243 extern unsigned int dwarf_eh_frame_size;
1244 extern asection *dwarf_eh_frame_section;
1245
1246 /* Imported from dwarf2read.c. */
1247 extern char *dwarf2_read_section (struct objfile *objfile, file_ptr offset,
1248 unsigned int size, asection *sectp);
1249
1250 void
1251 dwarf2_build_frame_info (struct objfile *objfile)
1252 {
1253 struct comp_unit unit;
1254 char *frame_ptr;
1255
1256 /* Build a minimal decoding of the DWARF2 compilation unit. */
1257 unit.abfd = objfile->obfd;
1258 unit.objfile = objfile;
1259 unit.addr_size = objfile->obfd->arch_info->bits_per_address / 8;
1260 unit.dbase = 0;
1261
1262 /* First add the information from the .eh_frame section. That way,
1263 the FDEs from that section are searched last. */
1264 if (dwarf_eh_frame_offset)
1265 {
1266 asection *got;
1267
1268 unit.cie = NULL;
1269 unit.dwarf_frame_buffer = dwarf2_read_section (objfile,
1270 dwarf_eh_frame_offset,
1271 dwarf_eh_frame_size,
1272 dwarf_eh_frame_section);
1273
1274 unit.dwarf_frame_size = dwarf_eh_frame_size;
1275 unit.dwarf_frame_section = dwarf_eh_frame_section;
1276
1277 /* FIXME: kettenis/20030602: This is the DW_EH_PE_datarel base
1278 that for the i386/amd64 target, which currently is the only
1279 target in GCC that supports/uses the DW_EH_PE_datarel
1280 encoding. */
1281 got = bfd_get_section_by_name (unit.abfd, ".got");
1282 if (got)
1283 unit.dbase = got->vma;
1284
1285 frame_ptr = unit.dwarf_frame_buffer;
1286 while (frame_ptr < unit.dwarf_frame_buffer + unit.dwarf_frame_size)
1287 frame_ptr = decode_frame_entry (&unit, frame_ptr, 1);
1288 }
1289
1290 if (dwarf_frame_offset)
1291 {
1292 unit.cie = NULL;
1293 unit.dwarf_frame_buffer = dwarf2_read_section (objfile,
1294 dwarf_frame_offset,
1295 dwarf_frame_size,
1296 dwarf_frame_section);
1297 unit.dwarf_frame_size = dwarf_frame_size;
1298 unit.dwarf_frame_section = dwarf_frame_section;
1299
1300 frame_ptr = unit.dwarf_frame_buffer;
1301 while (frame_ptr < unit.dwarf_frame_buffer + unit.dwarf_frame_size)
1302 frame_ptr = decode_frame_entry (&unit, frame_ptr, 0);
1303 }
1304 }
This page took 0.059023 seconds and 4 git commands to generate.