2003-06-08 Andrew Cagney <cagney@redhat.com>
[deliverable/binutils-gdb.git] / gdb / dwarf2-frame.c
CommitLineData
cfc14b3a
MK
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
45struct 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
7131cb6e
RH
68 /* True if a 'z' augmentation existed. */
69 unsigned char saw_z_augmentation;
70
cfc14b3a
MK
71 struct dwarf2_cie *next;
72};
73
74/* Frame Description Entry (FDE). */
75
76struct 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
94static struct dwarf2_fde *dwarf2_frame_find_fde (CORE_ADDR *pc);
95\f
96
97/* Structure describing a frame state. */
98
99struct 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
156static void
157dwarf2_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. */
2473a4a9 169 memset (rs->reg + rs->num_regs, 0, (num_regs - rs->num_regs) * size);
cfc14b3a
MK
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
176static struct dwarf2_frame_state_reg *
177dwarf2_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
190static void
191dwarf2_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
204static void
205dwarf2_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
219static CORE_ADDR
220read_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
233static void
234read_mem (void *baton, char *buf, CORE_ADDR addr, size_t len)
235{
236 read_memory (addr, buf, len);
237}
238
239static void
240no_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
246static CORE_ADDR
247no_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
253static CORE_ADDR
254execute_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
280static void
281execute_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
448struct 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
458struct dwarf2_frame_cache *
459dwarf2_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)
a42e117c
AC
547 /* NOTE: cagney/2003-06-07: Is this right? What if the
548 RETADDR_COLUM corresponds to a real register (and, worse,
549 that isn't the PC_REGNUM)? I'm guessing that the PC_REGNUM
550 further down is trying to handle this. That can't be right
551 though - PC_REGNUM may not be valid (it can be -ve). I
552 think, instead when RETADDR_COLUM isn't a real register, it
553 should map itself onto frame_pc_unwind. */
cfc14b3a
MK
554 continue;
555
556 /* Use the GDB register number as index. */
557 regnum = DWARF2_REG_TO_REGNUM (reg);
558
559 if (regnum >= 0 && regnum < num_regs)
560 cache->reg[regnum] = fs->regs.reg[reg];
561 }
562
f3e0f90b
RH
563 /* Store the location of the return addess. If the return address
564 column (adjusted) is not the same as gdb's PC_REGNUM, then this
565 implies a copy from the ra column register. */
566 if (fs->retaddr_column < fs->regs.num_regs
567 && fs->regs.reg[fs->retaddr_column].how != REG_UNSAVED)
a42e117c
AC
568 {
569 /* See comment above about a possibly -ve PC_REGNUM. If this
570 assertion fails, it's a problem with this code and not the
571 architecture. */
572 gdb_assert (PC_REGNUM >= 0);
573 cache->reg[PC_REGNUM] = fs->regs.reg[fs->retaddr_column];
574 }
f3e0f90b
RH
575 else
576 {
577 reg = DWARF2_REG_TO_REGNUM (fs->retaddr_column);
578 if (reg != PC_REGNUM)
579 {
a42e117c
AC
580 /* See comment above about PC_REGNUM being -ve. If this
581 assertion fails, it's a problem with this code and not
582 the architecture. */
583 gdb_assert (PC_REGNUM >= 0);
f3e0f90b
RH
584 cache->reg[PC_REGNUM].loc.reg = reg;
585 cache->reg[PC_REGNUM].how = REG_SAVED_REG;
586 }
587 }
cfc14b3a
MK
588
589 do_cleanups (old_chain);
590
591 *this_cache = cache;
592 return cache;
593}
594
595static void
596dwarf2_frame_this_id (struct frame_info *next_frame, void **this_cache,
597 struct frame_id *this_id)
598{
599 struct dwarf2_frame_cache *cache =
600 dwarf2_frame_cache (next_frame, this_cache);
601
602 (*this_id) = frame_id_build (cache->cfa, frame_func_unwind (next_frame));
603}
604
605static void
606dwarf2_frame_prev_register (struct frame_info *next_frame, void **this_cache,
607 int regnum, int *optimizedp,
608 enum lval_type *lvalp, CORE_ADDR *addrp,
609 int *realnump, void *valuep)
610{
611 struct dwarf2_frame_cache *cache =
612 dwarf2_frame_cache (next_frame, this_cache);
613
614 switch (cache->reg[regnum].how)
615 {
616 case REG_UNSAVED:
617 *optimizedp = 1;
618 *lvalp = not_lval;
619 *addrp = 0;
620 *realnump = -1;
621 if (regnum == SP_REGNUM)
622 {
623 /* GCC defines the CFA as the value of the stack pointer
624 just before the call instruction is executed. Do other
625 compilers use the same definition? */
a42e117c
AC
626 /* DWARF V3 Draft 7 p102: Typically, the CFA is defined to
627 be the value of the stack pointer at the call site in the
628 previous frame (which may be different from its value on
629 entry to the current frame). */
630 /* DWARF V3 Draft 7 p103: The first column of the rules
631 defines the rule which computes the CFA value; it may be
632 either a register and a signed offset that are added
633 together or a DWARF expression that is evaluated. */
634 /* FIXME: cagney/2003-07-07: I don't understand this. The
635 CFI info should have provided unwind information for the
636 SP register and then pointed ->cfa_reg at it, not the
637 reverse. Assuming that SP_REGNUM is !-ve, there is a
638 very real posibility that CFA is an offset from some
639 other register, having nothing to do with the unwound SP
640 value. */
cfc14b3a
MK
641 *optimizedp = 0;
642 if (valuep)
643 {
644 /* Store the value. */
645 store_typed_address (valuep, builtin_type_void_data_ptr,
646 cache->cfa);
647 }
648 }
649 else if (valuep)
650 {
651 /* In some cases, for example %eflags on the i386, we have
652 to provide a sane value, even though this register wasn't
653 saved. Assume we can get it from NEXT_FRAME. */
654 frame_unwind_register (next_frame, regnum, valuep);
655 }
656 break;
657
658 case REG_SAVED_OFFSET:
659 *optimizedp = 0;
660 *lvalp = lval_memory;
661 *addrp = cache->cfa + cache->reg[regnum].loc.offset;
662 *realnump = -1;
663 if (valuep)
664 {
665 /* Read the value in from memory. */
666 read_memory (*addrp, valuep,
667 register_size (current_gdbarch, regnum));
668 }
669 break;
670
671 case REG_SAVED_REG:
672 regnum = DWARF2_REG_TO_REGNUM (cache->reg[regnum].loc.reg);
673 frame_register_unwind (next_frame, regnum,
674 optimizedp, lvalp, addrp, realnump, valuep);
675 break;
676
677 case REG_SAVED_EXP:
678 *optimizedp = 0;
679 *lvalp = lval_memory;
680 *addrp = execute_stack_op (cache->reg[regnum].loc.exp,
681 cache->reg[regnum].exp_len,
682 next_frame, cache->cfa);
683 *realnump = -1;
684 if (valuep)
685 {
686 /* Read the value in from memory. */
687 read_memory (*addrp, valuep,
688 register_size (current_gdbarch, regnum));
689 }
690 break;
691
692 case REG_UNMODIFIED:
693 frame_register_unwind (next_frame, regnum,
694 optimizedp, lvalp, addrp, realnump, valuep);
695 break;
696
697 default:
698 internal_error (__FILE__, __LINE__, "Unknown register rule.");
699 }
700}
701
702static const struct frame_unwind dwarf2_frame_unwind =
703{
704 NORMAL_FRAME,
705 dwarf2_frame_this_id,
706 dwarf2_frame_prev_register
707};
708
709const struct frame_unwind *
710dwarf2_frame_p (CORE_ADDR pc)
711{
712 /* The way GDB works, this function can be called with PC just after
713 the last instruction of the function we're supposed to return the
714 unwind methods for. In that case we won't find the correct FDE;
715 instead we find the FDE for the next function, or we won't find
716 an FDE at all. There is a possible solution (see the comment in
717 dwarf2_frame_cache), GDB doesn't pass us enough information to
718 implement it. */
719 if (dwarf2_frame_find_fde (&pc))
720 return &dwarf2_frame_unwind;
721
722 return NULL;
723}
724\f
725
726/* There is no explicitly defined relationship between the CFA and the
727 location of frame's local variables and arguments/parameters.
728 Therefore, frame base methods on this page should probably only be
729 used as a last resort, just to avoid printing total garbage as a
730 response to the "info frame" command. */
731
732static CORE_ADDR
733dwarf2_frame_base_address (struct frame_info *next_frame, void **this_cache)
734{
735 struct dwarf2_frame_cache *cache =
736 dwarf2_frame_cache (next_frame, this_cache);
737
738 return cache->cfa;
739}
740
741static const struct frame_base dwarf2_frame_base =
742{
743 &dwarf2_frame_unwind,
744 dwarf2_frame_base_address,
745 dwarf2_frame_base_address,
746 dwarf2_frame_base_address
747};
748
749const struct frame_base *
750dwarf2_frame_base_p (CORE_ADDR pc)
751{
752 if (dwarf2_frame_find_fde (&pc))
753 return &dwarf2_frame_base;
754
755 return NULL;
756}
757\f
758/* A minimal decoding of DWARF2 compilation units. We only decode
759 what's needed to get to the call frame information. */
760
761struct comp_unit
762{
763 /* Keep the bfd convenient. */
764 bfd *abfd;
765
766 struct objfile *objfile;
767
768 /* Linked list of CIEs for this object. */
769 struct dwarf2_cie *cie;
770
771 /* Address size for this unit - from unit header. */
772 unsigned char addr_size;
773
774 /* Pointer to the .debug_frame section loaded into memory. */
775 char *dwarf_frame_buffer;
776
777 /* Length of the loaded .debug_frame section. */
778 unsigned long dwarf_frame_size;
779
780 /* Pointer to the .debug_frame section. */
781 asection *dwarf_frame_section;
0912c7f2
MK
782
783 /* Base for DW_EH_PE_datarel encodings. */
784 bfd_vma dbase;
cfc14b3a
MK
785};
786
787static unsigned int
788read_1_byte (bfd *bfd, char *buf)
789{
790 return bfd_get_8 (abfd, (bfd_byte *) buf);
791}
792
793static unsigned int
794read_4_bytes (bfd *abfd, char *buf)
795{
796 return bfd_get_32 (abfd, (bfd_byte *) buf);
797}
798
799static ULONGEST
800read_8_bytes (bfd *abfd, char *buf)
801{
802 return bfd_get_64 (abfd, (bfd_byte *) buf);
803}
804
805static ULONGEST
806read_unsigned_leb128 (bfd *abfd, char *buf, unsigned int *bytes_read_ptr)
807{
808 ULONGEST result;
809 unsigned int num_read;
810 int shift;
811 unsigned char byte;
812
813 result = 0;
814 shift = 0;
815 num_read = 0;
816
817 do
818 {
819 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
820 buf++;
821 num_read++;
822 result |= ((byte & 0x7f) << shift);
823 shift += 7;
824 }
825 while (byte & 0x80);
826
827 *bytes_read_ptr = num_read;
828
829 return result;
830}
831
832static LONGEST
833read_signed_leb128 (bfd *abfd, char *buf, unsigned int *bytes_read_ptr)
834{
835 LONGEST result;
836 int shift;
837 unsigned int num_read;
838 unsigned char byte;
839
840 result = 0;
841 shift = 0;
842 num_read = 0;
843
844 do
845 {
846 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
847 buf++;
848 num_read++;
849 result |= ((byte & 0x7f) << shift);
850 shift += 7;
851 }
852 while (byte & 0x80);
853
854 if ((shift < 32) && (byte & 0x40))
855 result |= -(1 << shift);
856
857 *bytes_read_ptr = num_read;
858
859 return result;
860}
861
862static ULONGEST
863read_initial_length (bfd *abfd, char *buf, unsigned int *bytes_read_ptr)
864{
865 LONGEST result;
866
867 result = bfd_get_32 (abfd, (bfd_byte *) buf);
868 if (result == 0xffffffff)
869 {
870 result = bfd_get_64 (abfd, (bfd_byte *) buf + 4);
871 *bytes_read_ptr = 12;
872 }
873 else
874 *bytes_read_ptr = 4;
875
876 return result;
877}
878\f
879
880/* Pointer encoding helper functions. */
881
882/* GCC supports exception handling based on DWARF2 CFI. However, for
883 technical reasons, it encodes addresses in its FDE's in a different
884 way. Several "pointer encodings" are supported. The encoding
885 that's used for a particular FDE is determined by the 'R'
886 augmentation in the associated CIE. The argument of this
887 augmentation is a single byte.
888
889 The address can be encoded as 2 bytes, 4 bytes, 8 bytes, or as a
890 LEB128. This is encoded in bits 0, 1 and 2. Bit 3 encodes whether
891 the address is signed or unsigned. Bits 4, 5 and 6 encode how the
892 address should be interpreted (absolute, relative to the current
893 position in the FDE, ...). Bit 7, indicates that the address
894 should be dereferenced. */
895
896static unsigned char
897encoding_for_size (unsigned int size)
898{
899 switch (size)
900 {
901 case 2:
902 return DW_EH_PE_udata2;
903 case 4:
904 return DW_EH_PE_udata4;
905 case 8:
906 return DW_EH_PE_udata8;
907 default:
908 internal_error (__FILE__, __LINE__, "Unsupported address size");
909 }
910}
911
912static unsigned int
913size_of_encoded_value (unsigned char encoding)
914{
915 if (encoding == DW_EH_PE_omit)
916 return 0;
917
918 switch (encoding & 0x07)
919 {
920 case DW_EH_PE_absptr:
921 return TYPE_LENGTH (builtin_type_void_data_ptr);
922 case DW_EH_PE_udata2:
923 return 2;
924 case DW_EH_PE_udata4:
925 return 4;
926 case DW_EH_PE_udata8:
927 return 8;
928 default:
929 internal_error (__FILE__, __LINE__, "Invalid or unsupported encoding");
930 }
931}
932
933static CORE_ADDR
934read_encoded_value (struct comp_unit *unit, unsigned char encoding,
935 char *buf, unsigned int *bytes_read_ptr)
936{
937 CORE_ADDR base;
938
939 /* GCC currently doesn't generate DW_EH_PE_indirect encodings for
940 FDE's. */
941 if (encoding & DW_EH_PE_indirect)
942 internal_error (__FILE__, __LINE__,
943 "Unsupported encoding: DW_EH_PE_indirect");
944
945 switch (encoding & 0x70)
946 {
947 case DW_EH_PE_absptr:
948 base = 0;
949 break;
950 case DW_EH_PE_pcrel:
951 base = bfd_get_section_vma (unit->bfd, unit->dwarf_frame_section);
952 base += (buf - unit->dwarf_frame_buffer);
953 break;
0912c7f2
MK
954 case DW_EH_PE_datarel:
955 base = unit->dbase;
956 break;
cfc14b3a
MK
957 default:
958 internal_error (__FILE__, __LINE__, "Invalid or unsupported encoding");
959 }
960
961 if ((encoding & 0x0f) == 0x00)
962 encoding |= encoding_for_size (TYPE_LENGTH(builtin_type_void_data_ptr));
963
964 switch (encoding & 0x0f)
965 {
966 case DW_EH_PE_udata2:
967 *bytes_read_ptr = 2;
968 return (base + bfd_get_16 (unit->abfd, (bfd_byte *) buf));
969 case DW_EH_PE_udata4:
970 *bytes_read_ptr = 4;
971 return (base + bfd_get_32 (unit->abfd, (bfd_byte *) buf));
972 case DW_EH_PE_udata8:
973 *bytes_read_ptr = 8;
974 return (base + bfd_get_64 (unit->abfd, (bfd_byte *) buf));
975 case DW_EH_PE_sdata2:
976 *bytes_read_ptr = 2;
977 return (base + bfd_get_signed_16 (unit->abfd, (bfd_byte *) buf));
978 case DW_EH_PE_sdata4:
979 *bytes_read_ptr = 4;
980 return (base + bfd_get_signed_32 (unit->abfd, (bfd_byte *) buf));
981 case DW_EH_PE_sdata8:
982 *bytes_read_ptr = 8;
983 return (base + bfd_get_signed_64 (unit->abfd, (bfd_byte *) buf));
984 default:
985 internal_error (__FILE__, __LINE__, "Invalid or unsupported encoding");
986 }
987}
988\f
989
990/* GCC uses a single CIE for all FDEs in a .debug_frame section.
991 That's why we use a simple linked list here. */
992
993static struct dwarf2_cie *
994find_cie (struct comp_unit *unit, ULONGEST cie_pointer)
995{
996 struct dwarf2_cie *cie = unit->cie;
997
998 while (cie)
999 {
1000 if (cie->cie_pointer == cie_pointer)
1001 return cie;
1002
1003 cie = cie->next;
1004 }
1005
1006 return NULL;
1007}
1008
1009static void
1010add_cie (struct comp_unit *unit, struct dwarf2_cie *cie)
1011{
1012 cie->next = unit->cie;
1013 unit->cie = cie;
1014}
1015
1016/* Find the FDE for *PC. Return a pointer to the FDE, and store the
1017 inital location associated with it into *PC. */
1018
1019static struct dwarf2_fde *
1020dwarf2_frame_find_fde (CORE_ADDR *pc)
1021{
1022 struct objfile *objfile;
1023
1024 ALL_OBJFILES (objfile)
1025 {
1026 struct dwarf2_fde *fde;
1027 CORE_ADDR offset;
1028
1029 offset = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1030
1031 fde = objfile->sym_private;
1032 while (fde)
1033 {
1034 if (*pc >= fde->initial_location + offset
1035 && *pc < fde->initial_location + offset + fde->address_range)
1036 {
1037 *pc = fde->initial_location + offset;
1038 return fde;
1039 }
1040
1041 fde = fde->next;
1042 }
1043 }
1044
1045 return NULL;
1046}
1047
1048static void
1049add_fde (struct comp_unit *unit, struct dwarf2_fde *fde)
1050{
1051 fde->next = unit->objfile->sym_private;
1052 unit->objfile->sym_private = fde;
1053}
1054
1055#ifdef CC_HAS_LONG_LONG
1056#define DW64_CIE_ID 0xffffffffffffffffULL
1057#else
1058#define DW64_CIE_ID ~0
1059#endif
1060
1061/* Read a CIE or FDE in BUF and decode it. */
1062
1063static char *
1064decode_frame_entry (struct comp_unit *unit, char *buf, int eh_frame_p)
1065{
1066 LONGEST length;
1067 unsigned int bytes_read;
1068 int dwarf64_p = 0;
1069 ULONGEST cie_id = DW_CIE_ID;
1070 ULONGEST cie_pointer;
1071 char *start = buf;
1072 char *end;
1073
1074 length = read_initial_length (unit->abfd, buf, &bytes_read);
1075 buf += bytes_read;
1076 end = buf + length;
1077
1078 if (length == 0)
1079 return end;
1080
1081 if (bytes_read == 12)
1082 dwarf64_p = 1;
1083
1084 /* In a .eh_frame section, zero is used to distinguish CIEs from
1085 FDEs. */
1086 if (eh_frame_p)
1087 cie_id = 0;
1088 else if (dwarf64_p)
1089 cie_id = DW64_CIE_ID;
1090
1091 if (dwarf64_p)
1092 {
1093 cie_pointer = read_8_bytes (unit->abfd, buf);
1094 buf += 8;
1095 }
1096 else
1097 {
1098 cie_pointer = read_4_bytes (unit->abfd, buf);
1099 buf += 4;
1100 }
1101
1102 if (cie_pointer == cie_id)
1103 {
1104 /* This is a CIE. */
1105 struct dwarf2_cie *cie;
1106 char *augmentation;
1107
1108 /* Record the offset into the .debug_frame section of this CIE. */
1109 cie_pointer = start - unit->dwarf_frame_buffer;
1110
1111 /* Check whether we've already read it. */
1112 if (find_cie (unit, cie_pointer))
1113 return end;
1114
1115 cie = (struct dwarf2_cie *)
1116 obstack_alloc (&unit->objfile->psymbol_obstack,
1117 sizeof (struct dwarf2_cie));
1118 cie->initial_instructions = NULL;
1119 cie->cie_pointer = cie_pointer;
1120
1121 /* The encoding for FDE's in a normal .debug_frame section
1122 depends on the target address size as specified in the
1123 Compilation Unit Header. */
1124 cie->encoding = encoding_for_size (unit->addr_size);
1125
1126 /* Check version number. */
1127 gdb_assert (read_1_byte (unit->abfd, buf) == DW_CIE_VERSION);
1128 buf += 1;
1129
1130 /* Interpret the interesting bits of the augmentation. */
1131 augmentation = buf;
1132 buf = augmentation + strlen (augmentation) + 1;
1133
1134 /* The GCC 2.x "eh" augmentation has a pointer immediately
1135 following the augmentation string, so it must be handled
1136 first. */
1137 if (augmentation[0] == 'e' && augmentation[1] == 'h')
1138 {
1139 /* Skip. */
1140 buf += TYPE_LENGTH (builtin_type_void_data_ptr);
1141 augmentation += 2;
1142 }
1143
1144 cie->code_alignment_factor =
1145 read_unsigned_leb128 (unit->abfd, buf, &bytes_read);
1146 buf += bytes_read;
1147
1148 cie->data_alignment_factor =
1149 read_signed_leb128 (unit->abfd, buf, &bytes_read);
1150 buf += bytes_read;
1151
1152 cie->return_address_register = read_1_byte (unit->abfd, buf);
1153 buf += 1;
1154
7131cb6e
RH
1155 cie->saw_z_augmentation = (*augmentation == 'z');
1156 if (cie->saw_z_augmentation)
cfc14b3a
MK
1157 {
1158 ULONGEST length;
1159
1160 length = read_unsigned_leb128 (unit->abfd, buf, &bytes_read);
1161 buf += bytes_read;
1162 cie->initial_instructions = buf + length;
1163 augmentation++;
1164 }
1165
1166 while (*augmentation)
1167 {
1168 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
1169 if (*augmentation == 'L')
1170 {
1171 /* Skip. */
1172 buf++;
1173 augmentation++;
1174 }
1175
1176 /* "R" indicates a byte indicating how FDE addresses are encoded. */
1177 else if (*augmentation == 'R')
1178 {
1179 cie->encoding = *buf++;
1180 augmentation++;
1181 }
1182
1183 /* "P" indicates a personality routine in the CIE augmentation. */
1184 else if (*augmentation == 'P')
1185 {
1186 /* Skip. */
1187 buf += size_of_encoded_value (*buf++);
1188 augmentation++;
1189 }
1190
1191 /* Otherwise we have an unknown augmentation.
1192 Bail out unless we saw a 'z' prefix. */
1193 else
1194 {
1195 if (cie->initial_instructions == NULL)
1196 return end;
1197
1198 /* Skip unknown augmentations. */
1199 buf = cie->initial_instructions;
1200 break;
1201 }
1202 }
1203
1204 cie->initial_instructions = buf;
1205 cie->end = end;
1206
1207 add_cie (unit, cie);
1208 }
1209 else
1210 {
1211 /* This is a FDE. */
1212 struct dwarf2_fde *fde;
1213
1214 if (eh_frame_p)
1215 {
1216 /* In an .eh_frame section, the CIE pointer is the delta
1217 between the address within the FDE where the CIE pointer
1218 is stored and the address of the CIE. Convert it to an
1219 offset into the .eh_frame section. */
1220 cie_pointer = buf - unit->dwarf_frame_buffer - cie_pointer;
1221 cie_pointer -= (dwarf64_p ? 8 : 4);
1222 }
1223
1224 fde = (struct dwarf2_fde *)
1225 obstack_alloc (&unit->objfile->psymbol_obstack,
1226 sizeof (struct dwarf2_fde));
1227 fde->cie = find_cie (unit, cie_pointer);
1228 if (fde->cie == NULL)
1229 {
1230 decode_frame_entry (unit, unit->dwarf_frame_buffer + cie_pointer,
1231 eh_frame_p);
1232 fde->cie = find_cie (unit, cie_pointer);
1233 }
1234
1235 gdb_assert (fde->cie != NULL);
1236
1237 fde->initial_location =
1238 read_encoded_value (unit, fde->cie->encoding, buf, &bytes_read);
1239 buf += bytes_read;
1240
1241 fde->address_range =
1242 read_encoded_value (unit, fde->cie->encoding & 0x0f, buf, &bytes_read);
1243 buf += bytes_read;
1244
7131cb6e
RH
1245 /* A 'z' augmentation in the CIE implies the presence of an
1246 augmentation field in the FDE as well. The only thing known
1247 to be in here at present is the LSDA entry for EH. So we
1248 can skip the whole thing. */
1249 if (fde->cie->saw_z_augmentation)
1250 {
1251 ULONGEST length;
1252
1253 length = read_unsigned_leb128 (unit->abfd, buf, &bytes_read);
1254 buf += bytes_read + length;
1255 }
1256
cfc14b3a
MK
1257 fde->instructions = buf;
1258 fde->end = end;
1259
1260 add_fde (unit, fde);
1261 }
1262
1263 return end;
1264}
1265\f
1266
1267/* FIXME: kettenis/20030504: This still needs to be integrated with
1268 dwarf2read.c in a better way. */
1269
1270/* Imported from dwarf2read.c. */
1271extern file_ptr dwarf_frame_offset;
1272extern unsigned int dwarf_frame_size;
1273extern asection *dwarf_frame_section;
1274extern file_ptr dwarf_eh_frame_offset;
1275extern unsigned int dwarf_eh_frame_size;
1276extern asection *dwarf_eh_frame_section;
1277
1278/* Imported from dwarf2read.c. */
1279extern char *dwarf2_read_section (struct objfile *objfile, file_ptr offset,
1280 unsigned int size, asection *sectp);
1281
1282void
1283dwarf2_build_frame_info (struct objfile *objfile)
1284{
1285 struct comp_unit unit;
1286 char *frame_ptr;
1287
1288 /* Build a minimal decoding of the DWARF2 compilation unit. */
1289 unit.abfd = objfile->obfd;
1290 unit.objfile = objfile;
1291 unit.addr_size = objfile->obfd->arch_info->bits_per_address / 8;
0912c7f2 1292 unit.dbase = 0;
cfc14b3a
MK
1293
1294 /* First add the information from the .eh_frame section. That way,
1295 the FDEs from that section are searched last. */
1296 if (dwarf_eh_frame_offset)
1297 {
0912c7f2
MK
1298 asection *got;
1299
cfc14b3a
MK
1300 unit.cie = NULL;
1301 unit.dwarf_frame_buffer = dwarf2_read_section (objfile,
1302 dwarf_eh_frame_offset,
1303 dwarf_eh_frame_size,
1304 dwarf_eh_frame_section);
1305
1306 unit.dwarf_frame_size = dwarf_eh_frame_size;
1307 unit.dwarf_frame_section = dwarf_eh_frame_section;
1308
0912c7f2
MK
1309 /* FIXME: kettenis/20030602: This is the DW_EH_PE_datarel base
1310 that for the i386/amd64 target, which currently is the only
1311 target in GCC that supports/uses the DW_EH_PE_datarel
1312 encoding. */
1313 got = bfd_get_section_by_name (unit.abfd, ".got");
1314 if (got)
1315 unit.dbase = got->vma;
1316
cfc14b3a
MK
1317 frame_ptr = unit.dwarf_frame_buffer;
1318 while (frame_ptr < unit.dwarf_frame_buffer + unit.dwarf_frame_size)
1319 frame_ptr = decode_frame_entry (&unit, frame_ptr, 1);
1320 }
1321
1322 if (dwarf_frame_offset)
1323 {
1324 unit.cie = NULL;
1325 unit.dwarf_frame_buffer = dwarf2_read_section (objfile,
1326 dwarf_frame_offset,
1327 dwarf_frame_size,
1328 dwarf_frame_section);
1329 unit.dwarf_frame_size = dwarf_frame_size;
1330 unit.dwarf_frame_section = dwarf_frame_section;
1331
1332 frame_ptr = unit.dwarf_frame_buffer;
1333 while (frame_ptr < unit.dwarf_frame_buffer + unit.dwarf_frame_size)
1334 frame_ptr = decode_frame_entry (&unit, frame_ptr, 0);
1335 }
1336}
This page took 0.079153 seconds and 4 git commands to generate.