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