Oops. missed a line.
[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
6896c0c7 39#include "complaints.h"
cfc14b3a
MK
40#include "dwarf2-frame.h"
41
42/* Call Frame Information (CFI). */
43
44/* Common Information Entry (CIE). */
45
46struct dwarf2_cie
47{
48 /* Offset into the .debug_frame section where this CIE was found.
49 Used to identify this CIE. */
50 ULONGEST cie_pointer;
51
52 /* Constant that is factored out of all advance location
53 instructions. */
54 ULONGEST code_alignment_factor;
55
56 /* Constants that is factored out of all offset instructions. */
57 LONGEST data_alignment_factor;
58
59 /* Return address column. */
60 ULONGEST return_address_register;
61
62 /* Instruction sequence to initialize a register set. */
63 unsigned char *initial_instructions;
64 unsigned char *end;
65
66 /* Encoding of addresses. */
67 unsigned char encoding;
68
7131cb6e
RH
69 /* True if a 'z' augmentation existed. */
70 unsigned char saw_z_augmentation;
71
cfc14b3a
MK
72 struct dwarf2_cie *next;
73};
74
75/* Frame Description Entry (FDE). */
76
77struct dwarf2_fde
78{
79 /* CIE for this FDE. */
80 struct dwarf2_cie *cie;
81
82 /* First location associated with this FDE. */
83 CORE_ADDR initial_location;
84
85 /* Number of bytes of program instructions described by this FDE. */
86 CORE_ADDR address_range;
87
88 /* Instruction sequence. */
89 unsigned char *instructions;
90 unsigned char *end;
91
92 struct dwarf2_fde *next;
93};
94
95static struct dwarf2_fde *dwarf2_frame_find_fde (CORE_ADDR *pc);
96\f
97
98/* Structure describing a frame state. */
99
3e2c4033
AC
100enum dwarf2_reg_rule
101{
102 /* Make certain that 0 maps onto the correct enum value - the
103 corresponding structure is being initialized using memset zero.
104 This indicates that CFI didn't provide any information at all
105 about a register - leaving how to obtain it's value totally
106 unspecified. */
107 REG_UNSPECIFIED = 0,
108 /* The term "undefined" comes from the DWARF2 CFI spec which this
109 code is moddeling - it indicates that the register's value is
110 "undefined". */
111 /* NOTE: cagney/2003-09-08: GCC uses the less formal term "unsaved"
112 - it's definition is a combination of REG_UNDEFINED and
113 REG_UNSPECIFIED - the failure to differentiate the two helps
114 explain a few problems with the CFI GCC outputs. */
115 REG_UNDEFINED,
116 REG_SAVED_OFFSET,
117 REG_SAVED_REG,
118 REG_SAVED_EXP,
119 REG_SAME_VALUE
120};
121
cfc14b3a
MK
122struct dwarf2_frame_state
123{
124 /* Each register save state can be described in terms of a CFA slot,
125 another register, or a location expression. */
126 struct dwarf2_frame_state_reg_info
127 {
128 struct dwarf2_frame_state_reg
129 {
130 union {
131 LONGEST offset;
132 ULONGEST reg;
133 unsigned char *exp;
134 } loc;
135 ULONGEST exp_len;
3e2c4033 136 enum dwarf2_reg_rule how;
cfc14b3a
MK
137 } *reg;
138 int num_regs;
139
140 /* Used to implement DW_CFA_remember_state. */
141 struct dwarf2_frame_state_reg_info *prev;
142 } regs;
143
144 LONGEST cfa_offset;
145 ULONGEST cfa_reg;
146 unsigned char *cfa_exp;
147 enum {
148 CFA_UNSET,
149 CFA_REG_OFFSET,
150 CFA_EXP
151 } cfa_how;
152
153 /* The PC described by the current frame state. */
154 CORE_ADDR pc;
155
156 /* Initial register set from the CIE.
157 Used to implement DW_CFA_restore. */
158 struct dwarf2_frame_state_reg_info initial;
159
160 /* The information we care about from the CIE. */
161 LONGEST data_align;
162 ULONGEST code_align;
163 ULONGEST retaddr_column;
164};
165
166/* Store the length the expression for the CFA in the `cfa_reg' field,
167 which is unused in that case. */
168#define cfa_exp_len cfa_reg
169
170/* Assert that the register set RS is large enough to store NUM_REGS
171 columns. If necessary, enlarge the register set. */
172
173static void
174dwarf2_frame_state_alloc_regs (struct dwarf2_frame_state_reg_info *rs,
175 int num_regs)
176{
177 size_t size = sizeof (struct dwarf2_frame_state_reg);
178
179 if (num_regs <= rs->num_regs)
180 return;
181
182 rs->reg = (struct dwarf2_frame_state_reg *)
183 xrealloc (rs->reg, num_regs * size);
184
185 /* Initialize newly allocated registers. */
2473a4a9 186 memset (rs->reg + rs->num_regs, 0, (num_regs - rs->num_regs) * size);
cfc14b3a
MK
187 rs->num_regs = num_regs;
188}
189
190/* Copy the register columns in register set RS into newly allocated
191 memory and return a pointer to this newly created copy. */
192
193static struct dwarf2_frame_state_reg *
194dwarf2_frame_state_copy_regs (struct dwarf2_frame_state_reg_info *rs)
195{
196 size_t size = rs->num_regs * sizeof (struct dwarf2_frame_state_reg_info);
197 struct dwarf2_frame_state_reg *reg;
198
199 reg = (struct dwarf2_frame_state_reg *) xmalloc (size);
200 memcpy (reg, rs->reg, size);
201
202 return reg;
203}
204
205/* Release the memory allocated to register set RS. */
206
207static void
208dwarf2_frame_state_free_regs (struct dwarf2_frame_state_reg_info *rs)
209{
210 if (rs)
211 {
212 dwarf2_frame_state_free_regs (rs->prev);
213
214 xfree (rs->reg);
215 xfree (rs);
216 }
217}
218
219/* Release the memory allocated to the frame state FS. */
220
221static void
222dwarf2_frame_state_free (void *p)
223{
224 struct dwarf2_frame_state *fs = p;
225
226 dwarf2_frame_state_free_regs (fs->initial.prev);
227 dwarf2_frame_state_free_regs (fs->regs.prev);
228 xfree (fs->initial.reg);
229 xfree (fs->regs.reg);
230 xfree (fs);
231}
232\f
233
234/* Helper functions for execute_stack_op. */
235
236static CORE_ADDR
237read_reg (void *baton, int reg)
238{
239 struct frame_info *next_frame = (struct frame_info *) baton;
240 int regnum;
241 char *buf;
242
243 regnum = DWARF2_REG_TO_REGNUM (reg);
244
245 buf = (char *) alloca (register_size (current_gdbarch, regnum));
246 frame_unwind_register (next_frame, regnum, buf);
247 return extract_typed_address (buf, builtin_type_void_data_ptr);
248}
249
250static void
251read_mem (void *baton, char *buf, CORE_ADDR addr, size_t len)
252{
253 read_memory (addr, buf, len);
254}
255
256static void
257no_get_frame_base (void *baton, unsigned char **start, size_t *length)
258{
259 internal_error (__FILE__, __LINE__,
260 "Support for DW_OP_fbreg is unimplemented");
261}
262
263static CORE_ADDR
264no_get_tls_address (void *baton, CORE_ADDR offset)
265{
266 internal_error (__FILE__, __LINE__,
267 "Support for DW_OP_GNU_push_tls_address is unimplemented");
268}
269
270static CORE_ADDR
271execute_stack_op (unsigned char *exp, ULONGEST len,
272 struct frame_info *next_frame, CORE_ADDR initial)
273{
274 struct dwarf_expr_context *ctx;
275 CORE_ADDR result;
276
277 ctx = new_dwarf_expr_context ();
278 ctx->baton = next_frame;
279 ctx->read_reg = read_reg;
280 ctx->read_mem = read_mem;
281 ctx->get_frame_base = no_get_frame_base;
282 ctx->get_tls_address = no_get_tls_address;
283
284 dwarf_expr_push (ctx, initial);
285 dwarf_expr_eval (ctx, exp, len);
286 result = dwarf_expr_fetch (ctx, 0);
287
288 if (ctx->in_reg)
289 result = read_reg (next_frame, result);
290
291 free_dwarf_expr_context (ctx);
292
293 return result;
294}
295\f
296
297static void
298execute_cfa_program (unsigned char *insn_ptr, unsigned char *insn_end,
299 struct frame_info *next_frame,
300 struct dwarf2_frame_state *fs)
301{
302 CORE_ADDR pc = frame_pc_unwind (next_frame);
303 int bytes_read;
304
305 while (insn_ptr < insn_end && fs->pc <= pc)
306 {
307 unsigned char insn = *insn_ptr++;
308 ULONGEST utmp, reg;
309 LONGEST offset;
310
311 if ((insn & 0xc0) == DW_CFA_advance_loc)
312 fs->pc += (insn & 0x3f) * fs->code_align;
313 else if ((insn & 0xc0) == DW_CFA_offset)
314 {
315 reg = insn & 0x3f;
316 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
317 offset = utmp * fs->data_align;
318 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
319 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
320 fs->regs.reg[reg].loc.offset = offset;
321 }
322 else if ((insn & 0xc0) == DW_CFA_restore)
323 {
324 gdb_assert (fs->initial.reg);
325 reg = insn & 0x3f;
326 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
327 fs->regs.reg[reg] = fs->initial.reg[reg];
328 }
329 else
330 {
331 switch (insn)
332 {
333 case DW_CFA_set_loc:
334 fs->pc = dwarf2_read_address (insn_ptr, insn_end, &bytes_read);
335 insn_ptr += bytes_read;
336 break;
337
338 case DW_CFA_advance_loc1:
339 utmp = extract_unsigned_integer (insn_ptr, 1);
340 fs->pc += utmp * fs->code_align;
341 insn_ptr++;
342 break;
343 case DW_CFA_advance_loc2:
344 utmp = extract_unsigned_integer (insn_ptr, 2);
345 fs->pc += utmp * fs->code_align;
346 insn_ptr += 2;
347 break;
348 case DW_CFA_advance_loc4:
349 utmp = extract_unsigned_integer (insn_ptr, 4);
350 fs->pc += utmp * fs->code_align;
351 insn_ptr += 4;
352 break;
353
354 case DW_CFA_offset_extended:
355 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
356 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
357 offset = utmp * fs->data_align;
358 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
359 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
360 fs->regs.reg[reg].loc.offset = offset;
361 break;
362
363 case DW_CFA_restore_extended:
364 gdb_assert (fs->initial.reg);
365 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
366 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
367 fs->regs.reg[reg] = fs->initial.reg[reg];
368 break;
369
370 case DW_CFA_undefined:
371 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
372 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
3e2c4033 373 fs->regs.reg[reg].how = REG_UNDEFINED;
cfc14b3a
MK
374 break;
375
376 case DW_CFA_same_value:
377 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
378 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
3e2c4033 379 fs->regs.reg[reg].how = REG_SAME_VALUE;
cfc14b3a
MK
380 break;
381
382 case DW_CFA_register:
383 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
384 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
385 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
386 fs->regs.reg[reg].loc.reg = utmp;
387 break;
388
389 case DW_CFA_remember_state:
390 {
391 struct dwarf2_frame_state_reg_info *new_rs;
392
393 new_rs = XMALLOC (struct dwarf2_frame_state_reg_info);
394 *new_rs = fs->regs;
395 fs->regs.reg = dwarf2_frame_state_copy_regs (&fs->regs);
396 fs->regs.prev = new_rs;
397 }
398 break;
399
400 case DW_CFA_restore_state:
401 {
402 struct dwarf2_frame_state_reg_info *old_rs = fs->regs.prev;
403
404 gdb_assert (old_rs);
405
406 xfree (fs->regs.reg);
407 fs->regs = *old_rs;
408 xfree (old_rs);
409 }
410 break;
411
412 case DW_CFA_def_cfa:
413 insn_ptr = read_uleb128 (insn_ptr, insn_end, &fs->cfa_reg);
414 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
415 fs->cfa_offset = utmp;
416 fs->cfa_how = CFA_REG_OFFSET;
417 break;
418
419 case DW_CFA_def_cfa_register:
420 insn_ptr = read_uleb128 (insn_ptr, insn_end, &fs->cfa_reg);
421 fs->cfa_how = CFA_REG_OFFSET;
422 break;
423
424 case DW_CFA_def_cfa_offset:
425 insn_ptr = read_uleb128 (insn_ptr, insn_end, &fs->cfa_offset);
426 /* cfa_how deliberately not set. */
427 break;
428
429 case DW_CFA_def_cfa_expression:
430 insn_ptr = read_uleb128 (insn_ptr, insn_end, &fs->cfa_exp_len);
431 fs->cfa_exp = insn_ptr;
432 fs->cfa_how = CFA_EXP;
433 insn_ptr += fs->cfa_exp_len;
434 break;
435
436 case DW_CFA_expression:
437 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
438 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
439 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
440 fs->regs.reg[reg].loc.exp = insn_ptr;
441 fs->regs.reg[reg].exp_len = utmp;
442 fs->regs.reg[reg].how = REG_SAVED_EXP;
443 insn_ptr += utmp;
444 break;
445
446 case DW_CFA_nop:
447 break;
448
449 case DW_CFA_GNU_args_size:
450 /* Ignored. */
451 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
452 break;
453
454 default:
455 internal_error (__FILE__, __LINE__, "Unknown CFI encountered.");
456 }
457 }
458 }
459
460 /* Don't allow remember/restore between CIE and FDE programs. */
461 dwarf2_frame_state_free_regs (fs->regs.prev);
462 fs->regs.prev = NULL;
463}
464
465struct dwarf2_frame_cache
466{
467 /* DWARF Call Frame Address. */
468 CORE_ADDR cfa;
469
470 /* Saved registers, indexed by GDB register number, not by DWARF
471 register number. */
472 struct dwarf2_frame_state_reg *reg;
473};
474
b9362cc7 475static struct dwarf2_frame_cache *
cfc14b3a
MK
476dwarf2_frame_cache (struct frame_info *next_frame, void **this_cache)
477{
478 struct cleanup *old_chain;
3e2c4033 479 const int num_regs = NUM_REGS + NUM_PSEUDO_REGS;
cfc14b3a
MK
480 struct dwarf2_frame_cache *cache;
481 struct dwarf2_frame_state *fs;
482 struct dwarf2_fde *fde;
cfc14b3a
MK
483
484 if (*this_cache)
485 return *this_cache;
486
487 /* Allocate a new cache. */
488 cache = FRAME_OBSTACK_ZALLOC (struct dwarf2_frame_cache);
489 cache->reg = FRAME_OBSTACK_CALLOC (num_regs, struct dwarf2_frame_state_reg);
490
491 /* Allocate and initialize the frame state. */
492 fs = XMALLOC (struct dwarf2_frame_state);
493 memset (fs, 0, sizeof (struct dwarf2_frame_state));
494 old_chain = make_cleanup (dwarf2_frame_state_free, fs);
495
496 /* Unwind the PC.
497
498 Note that if NEXT_FRAME is never supposed to return (i.e. a call
499 to abort), the compiler might optimize away the instruction at
500 NEXT_FRAME's return address. As a result the return address will
501 point at some random instruction, and the CFI for that
502 instruction is probably wortless to us. GCC's unwinder solves
503 this problem by substracting 1 from the return address to get an
504 address in the middle of a presumed call instruction (or the
505 instruction in the associated delay slot). This should only be
506 done for "normal" frames and not for resume-type frames (signal
507 handlers, sentinel frames, dummy frames).
508
1ce5d6dd
AC
509 frame_unwind_address_in_block does just this.
510
511 It's not clear how reliable the method is though - there is the
512 potential for the register state pre-call being different to that
513 on return. */
514 fs->pc = frame_unwind_address_in_block (next_frame);
cfc14b3a
MK
515
516 /* Find the correct FDE. */
517 fde = dwarf2_frame_find_fde (&fs->pc);
518 gdb_assert (fde != NULL);
519
520 /* Extract any interesting information from the CIE. */
521 fs->data_align = fde->cie->data_alignment_factor;
522 fs->code_align = fde->cie->code_alignment_factor;
523 fs->retaddr_column = fde->cie->return_address_register;
524
525 /* First decode all the insns in the CIE. */
526 execute_cfa_program (fde->cie->initial_instructions,
527 fde->cie->end, next_frame, fs);
528
529 /* Save the initialized register set. */
530 fs->initial = fs->regs;
531 fs->initial.reg = dwarf2_frame_state_copy_regs (&fs->regs);
532
533 /* Then decode the insns in the FDE up to our target PC. */
534 execute_cfa_program (fde->instructions, fde->end, next_frame, fs);
535
536 /* Caclulate the CFA. */
537 switch (fs->cfa_how)
538 {
539 case CFA_REG_OFFSET:
540 cache->cfa = read_reg (next_frame, fs->cfa_reg);
541 cache->cfa += fs->cfa_offset;
542 break;
543
544 case CFA_EXP:
545 cache->cfa =
546 execute_stack_op (fs->cfa_exp, fs->cfa_exp_len, next_frame, 0);
547 break;
548
549 default:
550 internal_error (__FILE__, __LINE__, "Unknown CFA rule.");
551 }
552
3e2c4033
AC
553 /* Initialize things so that all registers are marked as
554 unspecified. */
555 {
556 int regnum;
557 for (regnum = 0; regnum < num_regs; regnum++)
558 cache->reg[regnum].how = REG_UNSPECIFIED;
559 }
560
561 /* Go through the DWARF2 CFI generated table and save its register
562 location information in the cache. */
563 {
564 int column; /* CFI speak for "register number". */
565 for (column = 0; column < fs->regs.num_regs; column++)
566 {
567 int regnum;
568
569 /* Skip the return address column. */
570 if (column == fs->retaddr_column)
571 /* NOTE: cagney/2003-06-07: Is this right? What if
572 RETADDR_COLUMN corresponds to a real register (and,
573 worse, that isn't the PC_REGNUM)? I'm guessing that the
574 PC_REGNUM further down is trying to handle this. That
575 can't be right though - PC_REGNUM may not be valid (it
576 can be -ve). I think, instead when RETADDR_COLUM isn't a
577 real register, it should map itself onto frame_pc_unwind. */
578 continue;
579
580 /* Use the GDB register number as the destination index. */
581 regnum = DWARF2_REG_TO_REGNUM (column);
582
583 /* If there's no corresponding GDB register, ignore it. */
584 if (regnum < 0 || regnum >= num_regs)
585 continue;
586
587 /* NOTE: cagney/2003-09-05: CFI should specify the disposition
588 of all debug info registers. If it doesn't complain (but
589 not too loudly). It turns out that GCC, assumes that an
590 unspecified register implies "same value" when CFI (draft
591 7) specifies nothing at all. Such a register could equally
592 be interpreted as "undefined". Also note that this check
593 isn't sufficient - it only checks that all registers in the
594 range [0 .. max column] are specified - and won't detect
595 problems when a debug info register falls outside of the
596 table. Need a way of iterating through all the valid
597 DWARF2 register numbers. */
598 if (fs->regs.reg[column].how == REG_UNSPECIFIED)
599 complaint (&symfile_complaints,
600 "Incomplete CFI data; unspecified registers at 0x%s",
601 paddr (fs->pc));
602
603 cache->reg[regnum] = fs->regs.reg[column];
604 }
605 }
cfc14b3a 606
f3e0f90b
RH
607 /* Store the location of the return addess. If the return address
608 column (adjusted) is not the same as gdb's PC_REGNUM, then this
609 implies a copy from the ra column register. */
610 if (fs->retaddr_column < fs->regs.num_regs
3e2c4033 611 && fs->regs.reg[fs->retaddr_column].how != REG_UNDEFINED)
a42e117c
AC
612 {
613 /* See comment above about a possibly -ve PC_REGNUM. If this
614 assertion fails, it's a problem with this code and not the
615 architecture. */
616 gdb_assert (PC_REGNUM >= 0);
617 cache->reg[PC_REGNUM] = fs->regs.reg[fs->retaddr_column];
618 }
f3e0f90b
RH
619 else
620 {
3e2c4033 621 int reg = DWARF2_REG_TO_REGNUM (fs->retaddr_column);
f3e0f90b
RH
622 if (reg != PC_REGNUM)
623 {
a42e117c
AC
624 /* See comment above about PC_REGNUM being -ve. If this
625 assertion fails, it's a problem with this code and not
626 the architecture. */
627 gdb_assert (PC_REGNUM >= 0);
f3e0f90b
RH
628 cache->reg[PC_REGNUM].loc.reg = reg;
629 cache->reg[PC_REGNUM].how = REG_SAVED_REG;
630 }
631 }
cfc14b3a
MK
632
633 do_cleanups (old_chain);
634
635 *this_cache = cache;
636 return cache;
637}
638
639static void
640dwarf2_frame_this_id (struct frame_info *next_frame, void **this_cache,
641 struct frame_id *this_id)
642{
643 struct dwarf2_frame_cache *cache =
644 dwarf2_frame_cache (next_frame, this_cache);
645
646 (*this_id) = frame_id_build (cache->cfa, frame_func_unwind (next_frame));
647}
648
649static void
650dwarf2_frame_prev_register (struct frame_info *next_frame, void **this_cache,
651 int regnum, int *optimizedp,
652 enum lval_type *lvalp, CORE_ADDR *addrp,
653 int *realnump, void *valuep)
654{
655 struct dwarf2_frame_cache *cache =
656 dwarf2_frame_cache (next_frame, this_cache);
657
658 switch (cache->reg[regnum].how)
659 {
3e2c4033
AC
660 case REG_UNDEFINED:
661 /* If CFI explicitly specified that the value isn't defined,
662 mark it as optimized away - the value isn't available. */
cfc14b3a
MK
663 *optimizedp = 1;
664 *lvalp = not_lval;
665 *addrp = 0;
666 *realnump = -1;
667 if (regnum == SP_REGNUM)
668 {
669 /* GCC defines the CFA as the value of the stack pointer
670 just before the call instruction is executed. Do other
671 compilers use the same definition? */
a42e117c
AC
672 /* DWARF V3 Draft 7 p102: Typically, the CFA is defined to
673 be the value of the stack pointer at the call site in the
674 previous frame (which may be different from its value on
675 entry to the current frame). */
676 /* DWARF V3 Draft 7 p103: The first column of the rules
677 defines the rule which computes the CFA value; it may be
678 either a register and a signed offset that are added
679 together or a DWARF expression that is evaluated. */
680 /* FIXME: cagney/2003-07-07: I don't understand this. The
681 CFI info should have provided unwind information for the
682 SP register and then pointed ->cfa_reg at it, not the
683 reverse. Assuming that SP_REGNUM is !-ve, there is a
684 very real posibility that CFA is an offset from some
685 other register, having nothing to do with the unwound SP
686 value. */
3e2c4033
AC
687 /* FIXME: cagney/2003-09-05: I think I understand. GDB was
688 lumping the two states "unspecified" and "undefined"
689 together. Here SP_REGNUM was "unspecified", GCC assuming
690 that in such a case CFA would be used. This branch of
691 the if statement should be deleted - the problem of
692 SP_REGNUM is now handed by the case REG_UNSPECIFIED
693 below. */
cfc14b3a
MK
694 *optimizedp = 0;
695 if (valuep)
696 {
697 /* Store the value. */
698 store_typed_address (valuep, builtin_type_void_data_ptr,
699 cache->cfa);
700 }
701 }
702 else if (valuep)
703 {
704 /* In some cases, for example %eflags on the i386, we have
705 to provide a sane value, even though this register wasn't
706 saved. Assume we can get it from NEXT_FRAME. */
707 frame_unwind_register (next_frame, regnum, valuep);
708 }
709 break;
710
711 case REG_SAVED_OFFSET:
712 *optimizedp = 0;
713 *lvalp = lval_memory;
714 *addrp = cache->cfa + cache->reg[regnum].loc.offset;
715 *realnump = -1;
716 if (valuep)
717 {
718 /* Read the value in from memory. */
719 read_memory (*addrp, valuep,
720 register_size (current_gdbarch, regnum));
721 }
722 break;
723
724 case REG_SAVED_REG:
725 regnum = DWARF2_REG_TO_REGNUM (cache->reg[regnum].loc.reg);
726 frame_register_unwind (next_frame, regnum,
727 optimizedp, lvalp, addrp, realnump, valuep);
728 break;
729
730 case REG_SAVED_EXP:
731 *optimizedp = 0;
732 *lvalp = lval_memory;
733 *addrp = execute_stack_op (cache->reg[regnum].loc.exp,
734 cache->reg[regnum].exp_len,
735 next_frame, cache->cfa);
736 *realnump = -1;
737 if (valuep)
738 {
739 /* Read the value in from memory. */
740 read_memory (*addrp, valuep,
741 register_size (current_gdbarch, regnum));
742 }
743 break;
744
3e2c4033
AC
745 case REG_UNSPECIFIED:
746 /* GCC, in its infinite wisdom decided to not provide unwind
747 information for registers that are "same value". Since
748 DWARF2 (3 draft 7) doesn't define such behavior, said
749 registers are actually undefined (which is different to CFI
750 "undefined"). Code above issues a complaint about this.
751 Here just fudge the books, assume GCC, and that the value is
752 more inner on the stack. */
753 if (SP_REGNUM >= 0 && regnum == SP_REGNUM)
754 {
755 /* Can things get worse? Yep! One of the registers GCC
756 forgot to provide unwind information for was the stack
757 pointer. Outch! GCC appears to assumes that the CFA
758 address can be used - after all it points to the inner
759 most address of the previous frame before the function
760 call and that's always the same as the stack pointer on
761 return, right? Wrong. See GCC's i386 STDCALL option for
762 an ABI that has a different entry and return stack
763 pointer. */
764 /* DWARF V3 Draft 7 p102: Typically, the CFA is defined to
765 be the value of the stack pointer at the call site in the
766 previous frame (which may be different from its value on
767 entry to the current frame). */
768 /* DWARF V3 Draft 7 p103: The first column of the rules
769 defines the rule which computes the CFA value; it may be
770 either a register and a signed offset that are added
771 together or a DWARF expression that is evaluated. */
772 /* NOTE: cagney/2003-09-05: Should issue a complain.
773 Unfortunatly it turns out that DWARF2 CFI has a problem.
774 Since CFI specifies the location at which a register was
775 saved (not its value) it isn't possible to specify
776 something like "unwound(REG) == REG + constant" using CFI
777 as will almost always occure with the stack pointer. I
778 guess CFI should be point SP at CFA. Ref: danielj,
779 "Describing unsaved stack pointers", posted to dwarf2
780 list 2003-08-15. */
781 *optimizedp = 0;
782 *lvalp = not_lval;
783 *addrp = 0;
784 *realnump = -1;
785 if (valuep)
786 /* Store the value. */
787 store_typed_address (valuep, builtin_type_void_data_ptr,
788 cache->cfa);
789 }
790 else
791 /* Assume that the register can be found in the next inner
792 most frame. */
793 frame_register_unwind (next_frame, regnum,
794 optimizedp, lvalp, addrp, realnump, valuep);
795 break;
796
797 case REG_SAME_VALUE:
cfc14b3a
MK
798 frame_register_unwind (next_frame, regnum,
799 optimizedp, lvalp, addrp, realnump, valuep);
800 break;
801
802 default:
803 internal_error (__FILE__, __LINE__, "Unknown register rule.");
804 }
805}
806
807static const struct frame_unwind dwarf2_frame_unwind =
808{
809 NORMAL_FRAME,
810 dwarf2_frame_this_id,
811 dwarf2_frame_prev_register
812};
813
814const struct frame_unwind *
336d1bba 815dwarf2_frame_sniffer (struct frame_info *next_frame)
cfc14b3a 816{
1ce5d6dd
AC
817 /* Grab an address that is guarenteed to reside somewhere within the
818 function. frame_pc_unwind(), for a no-return next function, can
819 end up returning something past the end of this function's body. */
820 CORE_ADDR block_addr = frame_unwind_address_in_block (next_frame);
821 if (dwarf2_frame_find_fde (&block_addr))
cfc14b3a
MK
822 return &dwarf2_frame_unwind;
823
824 return NULL;
825}
826\f
827
828/* There is no explicitly defined relationship between the CFA and the
829 location of frame's local variables and arguments/parameters.
830 Therefore, frame base methods on this page should probably only be
831 used as a last resort, just to avoid printing total garbage as a
832 response to the "info frame" command. */
833
834static CORE_ADDR
835dwarf2_frame_base_address (struct frame_info *next_frame, void **this_cache)
836{
837 struct dwarf2_frame_cache *cache =
838 dwarf2_frame_cache (next_frame, this_cache);
839
840 return cache->cfa;
841}
842
843static const struct frame_base dwarf2_frame_base =
844{
845 &dwarf2_frame_unwind,
846 dwarf2_frame_base_address,
847 dwarf2_frame_base_address,
848 dwarf2_frame_base_address
849};
850
851const struct frame_base *
336d1bba 852dwarf2_frame_base_sniffer (struct frame_info *next_frame)
cfc14b3a 853{
336d1bba 854 CORE_ADDR pc = frame_pc_unwind (next_frame);
cfc14b3a
MK
855 if (dwarf2_frame_find_fde (&pc))
856 return &dwarf2_frame_base;
857
858 return NULL;
859}
860\f
861/* A minimal decoding of DWARF2 compilation units. We only decode
862 what's needed to get to the call frame information. */
863
864struct comp_unit
865{
866 /* Keep the bfd convenient. */
867 bfd *abfd;
868
869 struct objfile *objfile;
870
871 /* Linked list of CIEs for this object. */
872 struct dwarf2_cie *cie;
873
874 /* Address size for this unit - from unit header. */
875 unsigned char addr_size;
876
877 /* Pointer to the .debug_frame section loaded into memory. */
878 char *dwarf_frame_buffer;
879
880 /* Length of the loaded .debug_frame section. */
881 unsigned long dwarf_frame_size;
882
883 /* Pointer to the .debug_frame section. */
884 asection *dwarf_frame_section;
0912c7f2
MK
885
886 /* Base for DW_EH_PE_datarel encodings. */
887 bfd_vma dbase;
0fd85043
CV
888
889 /* Base for DW_EH_PE_textrel encodings. */
890 bfd_vma tbase;
cfc14b3a
MK
891};
892
0d0e1a63
MK
893const struct objfile_data *dwarf2_frame_data;
894
cfc14b3a
MK
895static unsigned int
896read_1_byte (bfd *bfd, char *buf)
897{
898 return bfd_get_8 (abfd, (bfd_byte *) buf);
899}
900
901static unsigned int
902read_4_bytes (bfd *abfd, char *buf)
903{
904 return bfd_get_32 (abfd, (bfd_byte *) buf);
905}
906
907static ULONGEST
908read_8_bytes (bfd *abfd, char *buf)
909{
910 return bfd_get_64 (abfd, (bfd_byte *) buf);
911}
912
913static ULONGEST
914read_unsigned_leb128 (bfd *abfd, char *buf, unsigned int *bytes_read_ptr)
915{
916 ULONGEST result;
917 unsigned int num_read;
918 int shift;
919 unsigned char byte;
920
921 result = 0;
922 shift = 0;
923 num_read = 0;
924
925 do
926 {
927 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
928 buf++;
929 num_read++;
930 result |= ((byte & 0x7f) << shift);
931 shift += 7;
932 }
933 while (byte & 0x80);
934
935 *bytes_read_ptr = num_read;
936
937 return result;
938}
939
940static LONGEST
941read_signed_leb128 (bfd *abfd, char *buf, unsigned int *bytes_read_ptr)
942{
943 LONGEST result;
944 int shift;
945 unsigned int num_read;
946 unsigned char byte;
947
948 result = 0;
949 shift = 0;
950 num_read = 0;
951
952 do
953 {
954 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
955 buf++;
956 num_read++;
957 result |= ((byte & 0x7f) << shift);
958 shift += 7;
959 }
960 while (byte & 0x80);
961
962 if ((shift < 32) && (byte & 0x40))
963 result |= -(1 << shift);
964
965 *bytes_read_ptr = num_read;
966
967 return result;
968}
969
970static ULONGEST
971read_initial_length (bfd *abfd, char *buf, unsigned int *bytes_read_ptr)
972{
973 LONGEST result;
974
975 result = bfd_get_32 (abfd, (bfd_byte *) buf);
976 if (result == 0xffffffff)
977 {
978 result = bfd_get_64 (abfd, (bfd_byte *) buf + 4);
979 *bytes_read_ptr = 12;
980 }
981 else
982 *bytes_read_ptr = 4;
983
984 return result;
985}
986\f
987
988/* Pointer encoding helper functions. */
989
990/* GCC supports exception handling based on DWARF2 CFI. However, for
991 technical reasons, it encodes addresses in its FDE's in a different
992 way. Several "pointer encodings" are supported. The encoding
993 that's used for a particular FDE is determined by the 'R'
994 augmentation in the associated CIE. The argument of this
995 augmentation is a single byte.
996
997 The address can be encoded as 2 bytes, 4 bytes, 8 bytes, or as a
998 LEB128. This is encoded in bits 0, 1 and 2. Bit 3 encodes whether
999 the address is signed or unsigned. Bits 4, 5 and 6 encode how the
1000 address should be interpreted (absolute, relative to the current
1001 position in the FDE, ...). Bit 7, indicates that the address
1002 should be dereferenced. */
1003
1004static unsigned char
1005encoding_for_size (unsigned int size)
1006{
1007 switch (size)
1008 {
1009 case 2:
1010 return DW_EH_PE_udata2;
1011 case 4:
1012 return DW_EH_PE_udata4;
1013 case 8:
1014 return DW_EH_PE_udata8;
1015 default:
1016 internal_error (__FILE__, __LINE__, "Unsupported address size");
1017 }
1018}
1019
1020static unsigned int
1021size_of_encoded_value (unsigned char encoding)
1022{
1023 if (encoding == DW_EH_PE_omit)
1024 return 0;
1025
1026 switch (encoding & 0x07)
1027 {
1028 case DW_EH_PE_absptr:
1029 return TYPE_LENGTH (builtin_type_void_data_ptr);
1030 case DW_EH_PE_udata2:
1031 return 2;
1032 case DW_EH_PE_udata4:
1033 return 4;
1034 case DW_EH_PE_udata8:
1035 return 8;
1036 default:
1037 internal_error (__FILE__, __LINE__, "Invalid or unsupported encoding");
1038 }
1039}
1040
1041static CORE_ADDR
1042read_encoded_value (struct comp_unit *unit, unsigned char encoding,
1043 char *buf, unsigned int *bytes_read_ptr)
1044{
68f6cf99
MK
1045 int ptr_len = size_of_encoded_value (DW_EH_PE_absptr);
1046 ptrdiff_t offset;
cfc14b3a
MK
1047 CORE_ADDR base;
1048
1049 /* GCC currently doesn't generate DW_EH_PE_indirect encodings for
1050 FDE's. */
1051 if (encoding & DW_EH_PE_indirect)
1052 internal_error (__FILE__, __LINE__,
1053 "Unsupported encoding: DW_EH_PE_indirect");
1054
68f6cf99
MK
1055 *bytes_read_ptr = 0;
1056
cfc14b3a
MK
1057 switch (encoding & 0x70)
1058 {
1059 case DW_EH_PE_absptr:
1060 base = 0;
1061 break;
1062 case DW_EH_PE_pcrel:
1063 base = bfd_get_section_vma (unit->bfd, unit->dwarf_frame_section);
1064 base += (buf - unit->dwarf_frame_buffer);
1065 break;
0912c7f2
MK
1066 case DW_EH_PE_datarel:
1067 base = unit->dbase;
1068 break;
0fd85043
CV
1069 case DW_EH_PE_textrel:
1070 base = unit->tbase;
1071 break;
68f6cf99
MK
1072 case DW_EH_PE_aligned:
1073 base = 0;
1074 offset = buf - unit->dwarf_frame_buffer;
1075 if ((offset % ptr_len) != 0)
1076 {
1077 *bytes_read_ptr = ptr_len - (offset % ptr_len);
1078 buf += *bytes_read_ptr;
1079 }
1080 break;
cfc14b3a
MK
1081 default:
1082 internal_error (__FILE__, __LINE__, "Invalid or unsupported encoding");
1083 }
1084
1085 if ((encoding & 0x0f) == 0x00)
68f6cf99 1086 encoding |= encoding_for_size (ptr_len);
cfc14b3a
MK
1087
1088 switch (encoding & 0x0f)
1089 {
1090 case DW_EH_PE_udata2:
68f6cf99 1091 *bytes_read_ptr += 2;
cfc14b3a
MK
1092 return (base + bfd_get_16 (unit->abfd, (bfd_byte *) buf));
1093 case DW_EH_PE_udata4:
68f6cf99 1094 *bytes_read_ptr += 4;
cfc14b3a
MK
1095 return (base + bfd_get_32 (unit->abfd, (bfd_byte *) buf));
1096 case DW_EH_PE_udata8:
68f6cf99 1097 *bytes_read_ptr += 8;
cfc14b3a
MK
1098 return (base + bfd_get_64 (unit->abfd, (bfd_byte *) buf));
1099 case DW_EH_PE_sdata2:
68f6cf99 1100 *bytes_read_ptr += 2;
cfc14b3a
MK
1101 return (base + bfd_get_signed_16 (unit->abfd, (bfd_byte *) buf));
1102 case DW_EH_PE_sdata4:
68f6cf99 1103 *bytes_read_ptr += 4;
cfc14b3a
MK
1104 return (base + bfd_get_signed_32 (unit->abfd, (bfd_byte *) buf));
1105 case DW_EH_PE_sdata8:
68f6cf99 1106 *bytes_read_ptr += 8;
cfc14b3a
MK
1107 return (base + bfd_get_signed_64 (unit->abfd, (bfd_byte *) buf));
1108 default:
1109 internal_error (__FILE__, __LINE__, "Invalid or unsupported encoding");
1110 }
1111}
1112\f
1113
1114/* GCC uses a single CIE for all FDEs in a .debug_frame section.
1115 That's why we use a simple linked list here. */
1116
1117static struct dwarf2_cie *
1118find_cie (struct comp_unit *unit, ULONGEST cie_pointer)
1119{
1120 struct dwarf2_cie *cie = unit->cie;
1121
1122 while (cie)
1123 {
1124 if (cie->cie_pointer == cie_pointer)
1125 return cie;
1126
1127 cie = cie->next;
1128 }
1129
1130 return NULL;
1131}
1132
1133static void
1134add_cie (struct comp_unit *unit, struct dwarf2_cie *cie)
1135{
1136 cie->next = unit->cie;
1137 unit->cie = cie;
1138}
1139
1140/* Find the FDE for *PC. Return a pointer to the FDE, and store the
1141 inital location associated with it into *PC. */
1142
1143static struct dwarf2_fde *
1144dwarf2_frame_find_fde (CORE_ADDR *pc)
1145{
1146 struct objfile *objfile;
1147
1148 ALL_OBJFILES (objfile)
1149 {
1150 struct dwarf2_fde *fde;
1151 CORE_ADDR offset;
1152
0d0e1a63 1153 fde = objfile_data (objfile, dwarf2_frame_data);
4ae9ee8e
DJ
1154 if (fde == NULL)
1155 continue;
1156
1157 gdb_assert (objfile->section_offsets);
1158 offset = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1159
cfc14b3a
MK
1160 while (fde)
1161 {
1162 if (*pc >= fde->initial_location + offset
1163 && *pc < fde->initial_location + offset + fde->address_range)
1164 {
1165 *pc = fde->initial_location + offset;
1166 return fde;
1167 }
1168
1169 fde = fde->next;
1170 }
1171 }
1172
1173 return NULL;
1174}
1175
1176static void
1177add_fde (struct comp_unit *unit, struct dwarf2_fde *fde)
1178{
0d0e1a63
MK
1179 fde->next = objfile_data (unit->objfile, dwarf2_frame_data);
1180 set_objfile_data (unit->objfile, dwarf2_frame_data, fde);
cfc14b3a
MK
1181}
1182
1183#ifdef CC_HAS_LONG_LONG
1184#define DW64_CIE_ID 0xffffffffffffffffULL
1185#else
1186#define DW64_CIE_ID ~0
1187#endif
1188
6896c0c7
RH
1189static char *decode_frame_entry (struct comp_unit *unit, char *start,
1190 int eh_frame_p);
cfc14b3a 1191
6896c0c7
RH
1192/* Decode the next CIE or FDE. Return NULL if invalid input, otherwise
1193 the next byte to be processed. */
cfc14b3a 1194static char *
6896c0c7 1195decode_frame_entry_1 (struct comp_unit *unit, char *start, int eh_frame_p)
cfc14b3a 1196{
6896c0c7 1197 char *buf;
cfc14b3a
MK
1198 LONGEST length;
1199 unsigned int bytes_read;
6896c0c7
RH
1200 int dwarf64_p;
1201 ULONGEST cie_id;
cfc14b3a 1202 ULONGEST cie_pointer;
cfc14b3a
MK
1203 char *end;
1204
6896c0c7 1205 buf = start;
cfc14b3a
MK
1206 length = read_initial_length (unit->abfd, buf, &bytes_read);
1207 buf += bytes_read;
1208 end = buf + length;
1209
6896c0c7
RH
1210 /* Are we still within the section? */
1211 if (end > unit->dwarf_frame_buffer + unit->dwarf_frame_size)
1212 return NULL;
1213
cfc14b3a
MK
1214 if (length == 0)
1215 return end;
1216
6896c0c7
RH
1217 /* Distinguish between 32 and 64-bit encoded frame info. */
1218 dwarf64_p = (bytes_read == 12);
cfc14b3a 1219
6896c0c7 1220 /* In a .eh_frame section, zero is used to distinguish CIEs from FDEs. */
cfc14b3a
MK
1221 if (eh_frame_p)
1222 cie_id = 0;
1223 else if (dwarf64_p)
1224 cie_id = DW64_CIE_ID;
6896c0c7
RH
1225 else
1226 cie_id = DW_CIE_ID;
cfc14b3a
MK
1227
1228 if (dwarf64_p)
1229 {
1230 cie_pointer = read_8_bytes (unit->abfd, buf);
1231 buf += 8;
1232 }
1233 else
1234 {
1235 cie_pointer = read_4_bytes (unit->abfd, buf);
1236 buf += 4;
1237 }
1238
1239 if (cie_pointer == cie_id)
1240 {
1241 /* This is a CIE. */
1242 struct dwarf2_cie *cie;
1243 char *augmentation;
1244
1245 /* Record the offset into the .debug_frame section of this CIE. */
1246 cie_pointer = start - unit->dwarf_frame_buffer;
1247
1248 /* Check whether we've already read it. */
1249 if (find_cie (unit, cie_pointer))
1250 return end;
1251
1252 cie = (struct dwarf2_cie *)
1253 obstack_alloc (&unit->objfile->psymbol_obstack,
1254 sizeof (struct dwarf2_cie));
1255 cie->initial_instructions = NULL;
1256 cie->cie_pointer = cie_pointer;
1257
1258 /* The encoding for FDE's in a normal .debug_frame section
1259 depends on the target address size as specified in the
1260 Compilation Unit Header. */
1261 cie->encoding = encoding_for_size (unit->addr_size);
1262
1263 /* Check version number. */
6896c0c7
RH
1264 if (read_1_byte (unit->abfd, buf) != DW_CIE_VERSION)
1265 return NULL;
cfc14b3a
MK
1266 buf += 1;
1267
1268 /* Interpret the interesting bits of the augmentation. */
1269 augmentation = buf;
1270 buf = augmentation + strlen (augmentation) + 1;
1271
1272 /* The GCC 2.x "eh" augmentation has a pointer immediately
1273 following the augmentation string, so it must be handled
1274 first. */
1275 if (augmentation[0] == 'e' && augmentation[1] == 'h')
1276 {
1277 /* Skip. */
1278 buf += TYPE_LENGTH (builtin_type_void_data_ptr);
1279 augmentation += 2;
1280 }
1281
1282 cie->code_alignment_factor =
1283 read_unsigned_leb128 (unit->abfd, buf, &bytes_read);
1284 buf += bytes_read;
1285
1286 cie->data_alignment_factor =
1287 read_signed_leb128 (unit->abfd, buf, &bytes_read);
1288 buf += bytes_read;
1289
1290 cie->return_address_register = read_1_byte (unit->abfd, buf);
1291 buf += 1;
1292
7131cb6e
RH
1293 cie->saw_z_augmentation = (*augmentation == 'z');
1294 if (cie->saw_z_augmentation)
cfc14b3a
MK
1295 {
1296 ULONGEST length;
1297
1298 length = read_unsigned_leb128 (unit->abfd, buf, &bytes_read);
1299 buf += bytes_read;
6896c0c7
RH
1300 if (buf > end)
1301 return NULL;
cfc14b3a
MK
1302 cie->initial_instructions = buf + length;
1303 augmentation++;
1304 }
1305
1306 while (*augmentation)
1307 {
1308 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
1309 if (*augmentation == 'L')
1310 {
1311 /* Skip. */
1312 buf++;
1313 augmentation++;
1314 }
1315
1316 /* "R" indicates a byte indicating how FDE addresses are encoded. */
1317 else if (*augmentation == 'R')
1318 {
1319 cie->encoding = *buf++;
1320 augmentation++;
1321 }
1322
1323 /* "P" indicates a personality routine in the CIE augmentation. */
1324 else if (*augmentation == 'P')
1325 {
1326 /* Skip. */
1327 buf += size_of_encoded_value (*buf++);
1328 augmentation++;
1329 }
1330
1331 /* Otherwise we have an unknown augmentation.
1332 Bail out unless we saw a 'z' prefix. */
1333 else
1334 {
1335 if (cie->initial_instructions == NULL)
1336 return end;
1337
1338 /* Skip unknown augmentations. */
1339 buf = cie->initial_instructions;
1340 break;
1341 }
1342 }
1343
1344 cie->initial_instructions = buf;
1345 cie->end = end;
1346
1347 add_cie (unit, cie);
1348 }
1349 else
1350 {
1351 /* This is a FDE. */
1352 struct dwarf2_fde *fde;
1353
6896c0c7
RH
1354 /* In an .eh_frame section, the CIE pointer is the delta between the
1355 address within the FDE where the CIE pointer is stored and the
1356 address of the CIE. Convert it to an offset into the .eh_frame
1357 section. */
cfc14b3a
MK
1358 if (eh_frame_p)
1359 {
cfc14b3a
MK
1360 cie_pointer = buf - unit->dwarf_frame_buffer - cie_pointer;
1361 cie_pointer -= (dwarf64_p ? 8 : 4);
1362 }
1363
6896c0c7
RH
1364 /* In either case, validate the result is still within the section. */
1365 if (cie_pointer >= unit->dwarf_frame_size)
1366 return NULL;
1367
cfc14b3a
MK
1368 fde = (struct dwarf2_fde *)
1369 obstack_alloc (&unit->objfile->psymbol_obstack,
1370 sizeof (struct dwarf2_fde));
1371 fde->cie = find_cie (unit, cie_pointer);
1372 if (fde->cie == NULL)
1373 {
1374 decode_frame_entry (unit, unit->dwarf_frame_buffer + cie_pointer,
1375 eh_frame_p);
1376 fde->cie = find_cie (unit, cie_pointer);
1377 }
1378
1379 gdb_assert (fde->cie != NULL);
1380
1381 fde->initial_location =
1382 read_encoded_value (unit, fde->cie->encoding, buf, &bytes_read);
1383 buf += bytes_read;
1384
1385 fde->address_range =
1386 read_encoded_value (unit, fde->cie->encoding & 0x0f, buf, &bytes_read);
1387 buf += bytes_read;
1388
7131cb6e
RH
1389 /* A 'z' augmentation in the CIE implies the presence of an
1390 augmentation field in the FDE as well. The only thing known
1391 to be in here at present is the LSDA entry for EH. So we
1392 can skip the whole thing. */
1393 if (fde->cie->saw_z_augmentation)
1394 {
1395 ULONGEST length;
1396
1397 length = read_unsigned_leb128 (unit->abfd, buf, &bytes_read);
1398 buf += bytes_read + length;
6896c0c7
RH
1399 if (buf > end)
1400 return NULL;
7131cb6e
RH
1401 }
1402
cfc14b3a
MK
1403 fde->instructions = buf;
1404 fde->end = end;
1405
1406 add_fde (unit, fde);
1407 }
1408
1409 return end;
1410}
6896c0c7
RH
1411
1412/* Read a CIE or FDE in BUF and decode it. */
1413static char *
1414decode_frame_entry (struct comp_unit *unit, char *start, int eh_frame_p)
1415{
1416 enum { NONE, ALIGN4, ALIGN8, FAIL } workaround = NONE;
1417 char *ret;
1418 const char *msg;
1419 ptrdiff_t start_offset;
1420
1421 while (1)
1422 {
1423 ret = decode_frame_entry_1 (unit, start, eh_frame_p);
1424 if (ret != NULL)
1425 break;
1426
1427 /* We have corrupt input data of some form. */
1428
1429 /* ??? Try, weakly, to work around compiler/assembler/linker bugs
1430 and mismatches wrt padding and alignment of debug sections. */
1431 /* Note that there is no requirement in the standard for any
1432 alignment at all in the frame unwind sections. Testing for
1433 alignment before trying to interpret data would be incorrect.
1434
1435 However, GCC traditionally arranged for frame sections to be
1436 sized such that the FDE length and CIE fields happen to be
1437 aligned (in theory, for performance). This, unfortunately,
1438 was done with .align directives, which had the side effect of
1439 forcing the section to be aligned by the linker.
1440
1441 This becomes a problem when you have some other producer that
1442 creates frame sections that are not as strictly aligned. That
1443 produces a hole in the frame info that gets filled by the
1444 linker with zeros.
1445
1446 The GCC behaviour is arguably a bug, but it's effectively now
1447 part of the ABI, so we're now stuck with it, at least at the
1448 object file level. A smart linker may decide, in the process
1449 of compressing duplicate CIE information, that it can rewrite
1450 the entire output section without this extra padding. */
1451
1452 start_offset = start - unit->dwarf_frame_buffer;
1453 if (workaround < ALIGN4 && (start_offset & 3) != 0)
1454 {
1455 start += 4 - (start_offset & 3);
1456 workaround = ALIGN4;
1457 continue;
1458 }
1459 if (workaround < ALIGN8 && (start_offset & 7) != 0)
1460 {
1461 start += 8 - (start_offset & 7);
1462 workaround = ALIGN8;
1463 continue;
1464 }
1465
1466 /* Nothing left to try. Arrange to return as if we've consumed
1467 the entire input section. Hopefully we'll get valid info from
1468 the other of .debug_frame/.eh_frame. */
1469 workaround = FAIL;
1470 ret = unit->dwarf_frame_buffer + unit->dwarf_frame_size;
1471 break;
1472 }
1473
1474 switch (workaround)
1475 {
1476 case NONE:
1477 break;
1478
1479 case ALIGN4:
1480 complaint (&symfile_complaints,
1481 "Corrupt data in %s:%s; align 4 workaround apparently succeeded",
1482 unit->dwarf_frame_section->owner->filename,
1483 unit->dwarf_frame_section->name);
1484 break;
1485
1486 case ALIGN8:
1487 complaint (&symfile_complaints,
1488 "Corrupt data in %s:%s; align 8 workaround apparently succeeded",
1489 unit->dwarf_frame_section->owner->filename,
1490 unit->dwarf_frame_section->name);
1491 break;
1492
1493 default:
1494 complaint (&symfile_complaints,
1495 "Corrupt data in %s:%s",
1496 unit->dwarf_frame_section->owner->filename,
1497 unit->dwarf_frame_section->name);
1498 break;
1499 }
1500
1501 return ret;
1502}
1503
cfc14b3a
MK
1504\f
1505
1506/* FIXME: kettenis/20030504: This still needs to be integrated with
1507 dwarf2read.c in a better way. */
1508
1509/* Imported from dwarf2read.c. */
1510extern file_ptr dwarf_frame_offset;
1511extern unsigned int dwarf_frame_size;
1512extern asection *dwarf_frame_section;
1513extern file_ptr dwarf_eh_frame_offset;
1514extern unsigned int dwarf_eh_frame_size;
1515extern asection *dwarf_eh_frame_section;
1516
1517/* Imported from dwarf2read.c. */
1518extern char *dwarf2_read_section (struct objfile *objfile, file_ptr offset,
1519 unsigned int size, asection *sectp);
1520
1521void
1522dwarf2_build_frame_info (struct objfile *objfile)
1523{
1524 struct comp_unit unit;
1525 char *frame_ptr;
1526
1527 /* Build a minimal decoding of the DWARF2 compilation unit. */
1528 unit.abfd = objfile->obfd;
1529 unit.objfile = objfile;
1530 unit.addr_size = objfile->obfd->arch_info->bits_per_address / 8;
0912c7f2 1531 unit.dbase = 0;
0fd85043 1532 unit.tbase = 0;
cfc14b3a
MK
1533
1534 /* First add the information from the .eh_frame section. That way,
1535 the FDEs from that section are searched last. */
1536 if (dwarf_eh_frame_offset)
1537 {
0fd85043 1538 asection *got, *txt;
0912c7f2 1539
cfc14b3a
MK
1540 unit.cie = NULL;
1541 unit.dwarf_frame_buffer = dwarf2_read_section (objfile,
1542 dwarf_eh_frame_offset,
1543 dwarf_eh_frame_size,
1544 dwarf_eh_frame_section);
1545
1546 unit.dwarf_frame_size = dwarf_eh_frame_size;
1547 unit.dwarf_frame_section = dwarf_eh_frame_section;
1548
0912c7f2 1549 /* FIXME: kettenis/20030602: This is the DW_EH_PE_datarel base
37b517aa
MK
1550 that is used for the i386/amd64 target, which currently is
1551 the only target in GCC that supports/uses the
1552 DW_EH_PE_datarel encoding. */
0912c7f2
MK
1553 got = bfd_get_section_by_name (unit.abfd, ".got");
1554 if (got)
1555 unit.dbase = got->vma;
1556
0fd85043
CV
1557 /* GCC emits the DW_EH_PE_textrel encoding type on sh and ia64 so far. */
1558 txt = bfd_get_section_by_name (unit.abfd, ".text");
1559 if (txt)
1560 unit.tbase = txt->vma;
1561
cfc14b3a
MK
1562 frame_ptr = unit.dwarf_frame_buffer;
1563 while (frame_ptr < unit.dwarf_frame_buffer + unit.dwarf_frame_size)
1564 frame_ptr = decode_frame_entry (&unit, frame_ptr, 1);
1565 }
1566
1567 if (dwarf_frame_offset)
1568 {
1569 unit.cie = NULL;
1570 unit.dwarf_frame_buffer = dwarf2_read_section (objfile,
1571 dwarf_frame_offset,
1572 dwarf_frame_size,
1573 dwarf_frame_section);
1574 unit.dwarf_frame_size = dwarf_frame_size;
1575 unit.dwarf_frame_section = dwarf_frame_section;
1576
1577 frame_ptr = unit.dwarf_frame_buffer;
1578 while (frame_ptr < unit.dwarf_frame_buffer + unit.dwarf_frame_size)
1579 frame_ptr = decode_frame_entry (&unit, frame_ptr, 0);
1580 }
1581}
0d0e1a63
MK
1582
1583/* Provide a prototype to silence -Wmissing-prototypes. */
1584void _initialize_dwarf2_frame (void);
1585
1586void
1587_initialize_dwarf2_frame (void)
1588{
1589 dwarf2_frame_data = register_objfile_data ();
1590}
This page took 0.122309 seconds and 4 git commands to generate.