ccad418a61e8128a8e3c93de7f383f56c9fc08dd
[deliverable/binutils-gdb.git] / gdb / dwarf2-frame.c
1 /* Frame unwinder for frames with DWARF Call Frame Information.
2
3 Copyright 2003 Free Software Foundation, Inc.
4
5 Contributed by Mark Kettenis.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
23
24 #include "defs.h"
25 #include "dwarf2expr.h"
26 #include "elf/dwarf2.h"
27 #include "frame.h"
28 #include "frame-base.h"
29 #include "frame-unwind.h"
30 #include "gdbcore.h"
31 #include "gdbtypes.h"
32 #include "symtab.h"
33 #include "objfiles.h"
34 #include "regcache.h"
35
36 #include "gdb_assert.h"
37 #include "gdb_string.h"
38
39 #include "complaints.h"
40 #include "dwarf2-frame.h"
41
42 /* Call Frame Information (CFI). */
43
44 /* Common Information Entry (CIE). */
45
46 struct 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
69 /* True if a 'z' augmentation existed. */
70 unsigned char saw_z_augmentation;
71
72 struct dwarf2_cie *next;
73 };
74
75 /* Frame Description Entry (FDE). */
76
77 struct 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
95 static struct dwarf2_fde *dwarf2_frame_find_fde (CORE_ADDR *pc);
96 \f
97
98 /* Structure describing a frame state. */
99
100 enum 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 its 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". GCC uses the less formal term "unsaved". Its
111 definition is a combination of REG_UNDEFINED and REG_UNSPECIFIED.
112 The failure to differentiate the two helps explain a few problems
113 with the CFI generated by GCC. */
114 REG_UNDEFINED,
115 REG_SAVED_OFFSET,
116 REG_SAVED_REG,
117 REG_SAVED_EXP,
118 REG_SAME_VALUE
119 };
120
121 struct dwarf2_frame_state
122 {
123 /* Each register save state can be described in terms of a CFA slot,
124 another register, or a location expression. */
125 struct dwarf2_frame_state_reg_info
126 {
127 struct dwarf2_frame_state_reg
128 {
129 union {
130 LONGEST offset;
131 ULONGEST reg;
132 unsigned char *exp;
133 } loc;
134 ULONGEST exp_len;
135 enum dwarf2_reg_rule how;
136 } *reg;
137 int num_regs;
138
139 /* Used to implement DW_CFA_remember_state. */
140 struct dwarf2_frame_state_reg_info *prev;
141 } regs;
142
143 LONGEST cfa_offset;
144 ULONGEST cfa_reg;
145 unsigned char *cfa_exp;
146 enum {
147 CFA_UNSET,
148 CFA_REG_OFFSET,
149 CFA_EXP
150 } cfa_how;
151
152 /* The PC described by the current frame state. */
153 CORE_ADDR pc;
154
155 /* Initial register set from the CIE.
156 Used to implement DW_CFA_restore. */
157 struct dwarf2_frame_state_reg_info initial;
158
159 /* The information we care about from the CIE. */
160 LONGEST data_align;
161 ULONGEST code_align;
162 ULONGEST retaddr_column;
163 };
164
165 /* Store the length the expression for the CFA in the `cfa_reg' field,
166 which is unused in that case. */
167 #define cfa_exp_len cfa_reg
168
169 /* Assert that the register set RS is large enough to store NUM_REGS
170 columns. If necessary, enlarge the register set. */
171
172 static void
173 dwarf2_frame_state_alloc_regs (struct dwarf2_frame_state_reg_info *rs,
174 int num_regs)
175 {
176 size_t size = sizeof (struct dwarf2_frame_state_reg);
177
178 if (num_regs <= rs->num_regs)
179 return;
180
181 rs->reg = (struct dwarf2_frame_state_reg *)
182 xrealloc (rs->reg, num_regs * size);
183
184 /* Initialize newly allocated registers. */
185 memset (rs->reg + rs->num_regs, 0, (num_regs - rs->num_regs) * size);
186 rs->num_regs = num_regs;
187 }
188
189 /* Copy the register columns in register set RS into newly allocated
190 memory and return a pointer to this newly created copy. */
191
192 static struct dwarf2_frame_state_reg *
193 dwarf2_frame_state_copy_regs (struct dwarf2_frame_state_reg_info *rs)
194 {
195 size_t size = rs->num_regs * sizeof (struct dwarf2_frame_state_reg_info);
196 struct dwarf2_frame_state_reg *reg;
197
198 reg = (struct dwarf2_frame_state_reg *) xmalloc (size);
199 memcpy (reg, rs->reg, size);
200
201 return reg;
202 }
203
204 /* Release the memory allocated to register set RS. */
205
206 static void
207 dwarf2_frame_state_free_regs (struct dwarf2_frame_state_reg_info *rs)
208 {
209 if (rs)
210 {
211 dwarf2_frame_state_free_regs (rs->prev);
212
213 xfree (rs->reg);
214 xfree (rs);
215 }
216 }
217
218 /* Release the memory allocated to the frame state FS. */
219
220 static void
221 dwarf2_frame_state_free (void *p)
222 {
223 struct dwarf2_frame_state *fs = p;
224
225 dwarf2_frame_state_free_regs (fs->initial.prev);
226 dwarf2_frame_state_free_regs (fs->regs.prev);
227 xfree (fs->initial.reg);
228 xfree (fs->regs.reg);
229 xfree (fs);
230 }
231 \f
232
233 /* Helper functions for execute_stack_op. */
234
235 static CORE_ADDR
236 read_reg (void *baton, int reg)
237 {
238 struct frame_info *next_frame = (struct frame_info *) baton;
239 int regnum;
240 char *buf;
241
242 regnum = DWARF2_REG_TO_REGNUM (reg);
243
244 buf = (char *) alloca (register_size (current_gdbarch, regnum));
245 frame_unwind_register (next_frame, regnum, buf);
246 return extract_typed_address (buf, builtin_type_void_data_ptr);
247 }
248
249 static void
250 read_mem (void *baton, char *buf, CORE_ADDR addr, size_t len)
251 {
252 read_memory (addr, buf, len);
253 }
254
255 static void
256 no_get_frame_base (void *baton, unsigned char **start, size_t *length)
257 {
258 internal_error (__FILE__, __LINE__,
259 "Support for DW_OP_fbreg is unimplemented");
260 }
261
262 static CORE_ADDR
263 no_get_tls_address (void *baton, CORE_ADDR offset)
264 {
265 internal_error (__FILE__, __LINE__,
266 "Support for DW_OP_GNU_push_tls_address is unimplemented");
267 }
268
269 static CORE_ADDR
270 execute_stack_op (unsigned char *exp, ULONGEST len,
271 struct frame_info *next_frame, CORE_ADDR initial)
272 {
273 struct dwarf_expr_context *ctx;
274 CORE_ADDR result;
275
276 ctx = new_dwarf_expr_context ();
277 ctx->baton = next_frame;
278 ctx->read_reg = read_reg;
279 ctx->read_mem = read_mem;
280 ctx->get_frame_base = no_get_frame_base;
281 ctx->get_tls_address = no_get_tls_address;
282
283 dwarf_expr_push (ctx, initial);
284 dwarf_expr_eval (ctx, exp, len);
285 result = dwarf_expr_fetch (ctx, 0);
286
287 if (ctx->in_reg)
288 result = read_reg (next_frame, result);
289
290 free_dwarf_expr_context (ctx);
291
292 return result;
293 }
294 \f
295
296 static void
297 execute_cfa_program (unsigned char *insn_ptr, unsigned char *insn_end,
298 struct frame_info *next_frame,
299 struct dwarf2_frame_state *fs)
300 {
301 CORE_ADDR pc = frame_pc_unwind (next_frame);
302 int bytes_read;
303
304 while (insn_ptr < insn_end && fs->pc <= pc)
305 {
306 unsigned char insn = *insn_ptr++;
307 ULONGEST utmp, reg;
308 LONGEST offset;
309
310 if ((insn & 0xc0) == DW_CFA_advance_loc)
311 fs->pc += (insn & 0x3f) * fs->code_align;
312 else if ((insn & 0xc0) == DW_CFA_offset)
313 {
314 reg = insn & 0x3f;
315 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
316 offset = utmp * fs->data_align;
317 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
318 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
319 fs->regs.reg[reg].loc.offset = offset;
320 }
321 else if ((insn & 0xc0) == DW_CFA_restore)
322 {
323 gdb_assert (fs->initial.reg);
324 reg = insn & 0x3f;
325 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
326 fs->regs.reg[reg] = fs->initial.reg[reg];
327 }
328 else
329 {
330 switch (insn)
331 {
332 case DW_CFA_set_loc:
333 fs->pc = dwarf2_read_address (insn_ptr, insn_end, &bytes_read);
334 insn_ptr += bytes_read;
335 break;
336
337 case DW_CFA_advance_loc1:
338 utmp = extract_unsigned_integer (insn_ptr, 1);
339 fs->pc += utmp * fs->code_align;
340 insn_ptr++;
341 break;
342 case DW_CFA_advance_loc2:
343 utmp = extract_unsigned_integer (insn_ptr, 2);
344 fs->pc += utmp * fs->code_align;
345 insn_ptr += 2;
346 break;
347 case DW_CFA_advance_loc4:
348 utmp = extract_unsigned_integer (insn_ptr, 4);
349 fs->pc += utmp * fs->code_align;
350 insn_ptr += 4;
351 break;
352
353 case DW_CFA_offset_extended:
354 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
355 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
356 offset = utmp * fs->data_align;
357 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
358 fs->regs.reg[reg].how = REG_SAVED_OFFSET;
359 fs->regs.reg[reg].loc.offset = offset;
360 break;
361
362 case DW_CFA_restore_extended:
363 gdb_assert (fs->initial.reg);
364 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
365 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
366 fs->regs.reg[reg] = fs->initial.reg[reg];
367 break;
368
369 case DW_CFA_undefined:
370 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
371 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
372 fs->regs.reg[reg].how = REG_UNDEFINED;
373 break;
374
375 case DW_CFA_same_value:
376 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
377 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
378 fs->regs.reg[reg].how = REG_SAME_VALUE;
379 break;
380
381 case DW_CFA_register:
382 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
383 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
384 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
385 fs->regs.reg[reg].how = REG_SAVED_REG;
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
465 struct 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
475 static struct dwarf2_frame_cache *
476 dwarf2_frame_cache (struct frame_info *next_frame, void **this_cache)
477 {
478 struct cleanup *old_chain;
479 const int num_regs = NUM_REGS + NUM_PSEUDO_REGS;
480 struct dwarf2_frame_cache *cache;
481 struct dwarf2_frame_state *fs;
482 struct dwarf2_fde *fde;
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 worthless 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). The function
508 frame_unwind_address_in_block does just this. It's not clear how
509 reliable the method is though; there is the potential for the
510 register state pre-call being different to that on return. */
511 fs->pc = frame_unwind_address_in_block (next_frame);
512
513 /* Find the correct FDE. */
514 fde = dwarf2_frame_find_fde (&fs->pc);
515 gdb_assert (fde != NULL);
516
517 /* Extract any interesting information from the CIE. */
518 fs->data_align = fde->cie->data_alignment_factor;
519 fs->code_align = fde->cie->code_alignment_factor;
520 fs->retaddr_column = fde->cie->return_address_register;
521
522 /* First decode all the insns in the CIE. */
523 execute_cfa_program (fde->cie->initial_instructions,
524 fde->cie->end, next_frame, fs);
525
526 /* Save the initialized register set. */
527 fs->initial = fs->regs;
528 fs->initial.reg = dwarf2_frame_state_copy_regs (&fs->regs);
529
530 /* Then decode the insns in the FDE up to our target PC. */
531 execute_cfa_program (fde->instructions, fde->end, next_frame, fs);
532
533 /* Caclulate the CFA. */
534 switch (fs->cfa_how)
535 {
536 case CFA_REG_OFFSET:
537 cache->cfa = read_reg (next_frame, fs->cfa_reg);
538 cache->cfa += fs->cfa_offset;
539 break;
540
541 case CFA_EXP:
542 cache->cfa =
543 execute_stack_op (fs->cfa_exp, fs->cfa_exp_len, next_frame, 0);
544 break;
545
546 default:
547 internal_error (__FILE__, __LINE__, "Unknown CFA rule.");
548 }
549
550 /* Initialize things so that all registers are marked as
551 unspecified. */
552 {
553 int regnum;
554
555 for (regnum = 0; regnum < num_regs; regnum++)
556 cache->reg[regnum].how = REG_UNSPECIFIED;
557 }
558
559 /* Go through the DWARF2 CFI generated table and save its register
560 location information in the cache. */
561 {
562 int column; /* CFI speak for "register number". */
563
564 for (column = 0; column < fs->regs.num_regs; column++)
565 {
566 int regnum;
567
568 /* Skip the return address column. */
569 if (column == fs->retaddr_column)
570 /* NOTE: cagney/2003-06-07: Is this right? What if
571 RETADDR_COLUMN corresponds to a real register (and,
572 worse, that isn't the PC_REGNUM)? I'm guessing that the
573 PC_REGNUM further down is trying to handle this. That
574 can't be right though; PC_REGNUM may not be valid (it can
575 be negative). I think, instead when RETADDR_COLUM isn't
576 a real register, it should map itself onto
577 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. We 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 }
606
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 return address column register. */
610 if (fs->retaddr_column < fs->regs.num_regs
611 && fs->regs.reg[fs->retaddr_column].how != REG_UNDEFINED)
612 {
613 /* See comment above about a possibly negative PC_REGNUM. If
614 this assertion fails, it's a problem with this code and not
615 the architecture. */
616 gdb_assert (PC_REGNUM >= 0);
617 cache->reg[PC_REGNUM] = fs->regs.reg[fs->retaddr_column];
618 }
619 else
620 {
621 int reg = DWARF2_REG_TO_REGNUM (fs->retaddr_column);
622 if (reg != PC_REGNUM)
623 {
624 /* See comment above about PC_REGNUM being negative. If
625 this assertion fails, it's a problem with this code and
626 not the architecture. */
627 gdb_assert (PC_REGNUM >= 0);
628 cache->reg[PC_REGNUM].loc.reg = reg;
629 cache->reg[PC_REGNUM].how = REG_SAVED_REG;
630 }
631 }
632
633 do_cleanups (old_chain);
634
635 *this_cache = cache;
636 return cache;
637 }
638
639 static void
640 dwarf2_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
649 static void
650 dwarf2_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 {
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. */
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? */
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 isn't negative, there
684 is a very real posibility that CFA is an offset from some
685 other register, having nothing to do with the unwound SP
686 value. */
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. */
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
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 complaint.
773 Unfortunately 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:
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
807 static const struct frame_unwind dwarf2_frame_unwind =
808 {
809 NORMAL_FRAME,
810 dwarf2_frame_this_id,
811 dwarf2_frame_prev_register
812 };
813
814 const struct frame_unwind *
815 dwarf2_frame_sniffer (struct frame_info *next_frame)
816 {
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))
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
834 static CORE_ADDR
835 dwarf2_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
843 static 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
851 const struct frame_base *
852 dwarf2_frame_base_sniffer (struct frame_info *next_frame)
853 {
854 CORE_ADDR pc = frame_pc_unwind (next_frame);
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
864 struct 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;
885
886 /* Base for DW_EH_PE_datarel encodings. */
887 bfd_vma dbase;
888
889 /* Base for DW_EH_PE_textrel encodings. */
890 bfd_vma tbase;
891 };
892
893 const struct objfile_data *dwarf2_frame_data;
894
895 static unsigned int
896 read_1_byte (bfd *bfd, char *buf)
897 {
898 return bfd_get_8 (abfd, (bfd_byte *) buf);
899 }
900
901 static unsigned int
902 read_4_bytes (bfd *abfd, char *buf)
903 {
904 return bfd_get_32 (abfd, (bfd_byte *) buf);
905 }
906
907 static ULONGEST
908 read_8_bytes (bfd *abfd, char *buf)
909 {
910 return bfd_get_64 (abfd, (bfd_byte *) buf);
911 }
912
913 static ULONGEST
914 read_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
940 static LONGEST
941 read_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
970 static ULONGEST
971 read_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
1004 static unsigned char
1005 encoding_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
1020 static unsigned int
1021 size_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
1041 static CORE_ADDR
1042 read_encoded_value (struct comp_unit *unit, unsigned char encoding,
1043 char *buf, unsigned int *bytes_read_ptr)
1044 {
1045 int ptr_len = size_of_encoded_value (DW_EH_PE_absptr);
1046 ptrdiff_t offset;
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
1055 *bytes_read_ptr = 0;
1056
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;
1066 case DW_EH_PE_datarel:
1067 base = unit->dbase;
1068 break;
1069 case DW_EH_PE_textrel:
1070 base = unit->tbase;
1071 break;
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;
1081 default:
1082 internal_error (__FILE__, __LINE__, "Invalid or unsupported encoding");
1083 }
1084
1085 if ((encoding & 0x0f) == 0x00)
1086 encoding |= encoding_for_size (ptr_len);
1087
1088 switch (encoding & 0x0f)
1089 {
1090 case DW_EH_PE_udata2:
1091 *bytes_read_ptr += 2;
1092 return (base + bfd_get_16 (unit->abfd, (bfd_byte *) buf));
1093 case DW_EH_PE_udata4:
1094 *bytes_read_ptr += 4;
1095 return (base + bfd_get_32 (unit->abfd, (bfd_byte *) buf));
1096 case DW_EH_PE_udata8:
1097 *bytes_read_ptr += 8;
1098 return (base + bfd_get_64 (unit->abfd, (bfd_byte *) buf));
1099 case DW_EH_PE_sdata2:
1100 *bytes_read_ptr += 2;
1101 return (base + bfd_get_signed_16 (unit->abfd, (bfd_byte *) buf));
1102 case DW_EH_PE_sdata4:
1103 *bytes_read_ptr += 4;
1104 return (base + bfd_get_signed_32 (unit->abfd, (bfd_byte *) buf));
1105 case DW_EH_PE_sdata8:
1106 *bytes_read_ptr += 8;
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
1117 static struct dwarf2_cie *
1118 find_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
1133 static void
1134 add_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
1143 static struct dwarf2_fde *
1144 dwarf2_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
1153 fde = objfile_data (objfile, dwarf2_frame_data);
1154 if (fde == NULL)
1155 continue;
1156
1157 gdb_assert (objfile->section_offsets);
1158 offset = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1159
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
1176 static void
1177 add_fde (struct comp_unit *unit, struct dwarf2_fde *fde)
1178 {
1179 fde->next = objfile_data (unit->objfile, dwarf2_frame_data);
1180 set_objfile_data (unit->objfile, dwarf2_frame_data, fde);
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
1189 static char *decode_frame_entry (struct comp_unit *unit, char *start,
1190 int eh_frame_p);
1191
1192 /* Decode the next CIE or FDE. Return NULL if invalid input, otherwise
1193 the next byte to be processed. */
1194 static char *
1195 decode_frame_entry_1 (struct comp_unit *unit, char *start, int eh_frame_p)
1196 {
1197 char *buf;
1198 LONGEST length;
1199 unsigned int bytes_read;
1200 int dwarf64_p;
1201 ULONGEST cie_id;
1202 ULONGEST cie_pointer;
1203 char *end;
1204
1205 buf = start;
1206 length = read_initial_length (unit->abfd, buf, &bytes_read);
1207 buf += bytes_read;
1208 end = buf + length;
1209
1210 /* Are we still within the section? */
1211 if (end > unit->dwarf_frame_buffer + unit->dwarf_frame_size)
1212 return NULL;
1213
1214 if (length == 0)
1215 return end;
1216
1217 /* Distinguish between 32 and 64-bit encoded frame info. */
1218 dwarf64_p = (bytes_read == 12);
1219
1220 /* In a .eh_frame section, zero is used to distinguish CIEs from FDEs. */
1221 if (eh_frame_p)
1222 cie_id = 0;
1223 else if (dwarf64_p)
1224 cie_id = DW64_CIE_ID;
1225 else
1226 cie_id = DW_CIE_ID;
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. */
1264 if (read_1_byte (unit->abfd, buf) != DW_CIE_VERSION)
1265 return NULL;
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
1293 cie->saw_z_augmentation = (*augmentation == 'z');
1294 if (cie->saw_z_augmentation)
1295 {
1296 ULONGEST length;
1297
1298 length = read_unsigned_leb128 (unit->abfd, buf, &bytes_read);
1299 buf += bytes_read;
1300 if (buf > end)
1301 return NULL;
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
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. */
1358 if (eh_frame_p)
1359 {
1360 cie_pointer = buf - unit->dwarf_frame_buffer - cie_pointer;
1361 cie_pointer -= (dwarf64_p ? 8 : 4);
1362 }
1363
1364 /* In either case, validate the result is still within the section. */
1365 if (cie_pointer >= unit->dwarf_frame_size)
1366 return NULL;
1367
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
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;
1399 if (buf > end)
1400 return NULL;
1401 }
1402
1403 fde->instructions = buf;
1404 fde->end = end;
1405
1406 add_fde (unit, fde);
1407 }
1408
1409 return end;
1410 }
1411
1412 /* Read a CIE or FDE in BUF and decode it. */
1413 static char *
1414 decode_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
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. */
1510 extern file_ptr dwarf_frame_offset;
1511 extern unsigned int dwarf_frame_size;
1512 extern asection *dwarf_frame_section;
1513 extern file_ptr dwarf_eh_frame_offset;
1514 extern unsigned int dwarf_eh_frame_size;
1515 extern asection *dwarf_eh_frame_section;
1516
1517 /* Imported from dwarf2read.c. */
1518 extern char *dwarf2_read_section (struct objfile *objfile, file_ptr offset,
1519 unsigned int size, asection *sectp);
1520
1521 void
1522 dwarf2_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;
1531 unit.dbase = 0;
1532 unit.tbase = 0;
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 {
1538 asection *got, *txt;
1539
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
1549 /* FIXME: kettenis/20030602: This is the DW_EH_PE_datarel base
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. */
1553 got = bfd_get_section_by_name (unit.abfd, ".got");
1554 if (got)
1555 unit.dbase = got->vma;
1556
1557 /* GCC emits the DW_EH_PE_textrel encoding type on sh and ia64
1558 so far. */
1559 txt = bfd_get_section_by_name (unit.abfd, ".text");
1560 if (txt)
1561 unit.tbase = txt->vma;
1562
1563 frame_ptr = unit.dwarf_frame_buffer;
1564 while (frame_ptr < unit.dwarf_frame_buffer + unit.dwarf_frame_size)
1565 frame_ptr = decode_frame_entry (&unit, frame_ptr, 1);
1566 }
1567
1568 if (dwarf_frame_offset)
1569 {
1570 unit.cie = NULL;
1571 unit.dwarf_frame_buffer = dwarf2_read_section (objfile,
1572 dwarf_frame_offset,
1573 dwarf_frame_size,
1574 dwarf_frame_section);
1575 unit.dwarf_frame_size = dwarf_frame_size;
1576 unit.dwarf_frame_section = dwarf_frame_section;
1577
1578 frame_ptr = unit.dwarf_frame_buffer;
1579 while (frame_ptr < unit.dwarf_frame_buffer + unit.dwarf_frame_size)
1580 frame_ptr = decode_frame_entry (&unit, frame_ptr, 0);
1581 }
1582 }
1583
1584 /* Provide a prototype to silence -Wmissing-prototypes. */
1585 void _initialize_dwarf2_frame (void);
1586
1587 void
1588 _initialize_dwarf2_frame (void)
1589 {
1590 dwarf2_frame_data = register_objfile_data ();
1591 }
This page took 0.062912 seconds and 4 git commands to generate.