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