]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/dwarf2-frame.c
2003-09-08 Andrew Cagney <cagney@redhat.com>
[thirdparty/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 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
122 struct 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;
136 enum dwarf2_reg_rule how;
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
173 static void
174 dwarf2_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. */
186 memset (rs->reg + rs->num_regs, 0, (num_regs - rs->num_regs) * size);
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
193 static struct dwarf2_frame_state_reg *
194 dwarf2_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
207 static void
208 dwarf2_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
221 static void
222 dwarf2_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
236 static CORE_ADDR
237 read_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
250 static void
251 read_mem (void *baton, char *buf, CORE_ADDR addr, size_t len)
252 {
253 read_memory (addr, buf, len);
254 }
255
256 static void
257 no_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
263 static CORE_ADDR
264 no_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
270 static CORE_ADDR
271 execute_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
297 static void
298 execute_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);
373 fs->regs.reg[reg].how = REG_UNDEFINED;
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);
379 fs->regs.reg[reg].how = REG_SAME_VALUE;
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
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 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
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);
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
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 }
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 ra 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 -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 }
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 -ve. If this
625 assertion fails, it's a problem with this code and not
626 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 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. */
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 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:
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
890 const struct objfile_data *dwarf2_frame_data;
891
892 static unsigned int
893 read_1_byte (bfd *bfd, char *buf)
894 {
895 return bfd_get_8 (abfd, (bfd_byte *) buf);
896 }
897
898 static unsigned int
899 read_4_bytes (bfd *abfd, char *buf)
900 {
901 return bfd_get_32 (abfd, (bfd_byte *) buf);
902 }
903
904 static ULONGEST
905 read_8_bytes (bfd *abfd, char *buf)
906 {
907 return bfd_get_64 (abfd, (bfd_byte *) buf);
908 }
909
910 static ULONGEST
911 read_unsigned_leb128 (bfd *abfd, char *buf, unsigned int *bytes_read_ptr)
912 {
913 ULONGEST result;
914 unsigned int num_read;
915 int shift;
916 unsigned char byte;
917
918 result = 0;
919 shift = 0;
920 num_read = 0;
921
922 do
923 {
924 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
925 buf++;
926 num_read++;
927 result |= ((byte & 0x7f) << shift);
928 shift += 7;
929 }
930 while (byte & 0x80);
931
932 *bytes_read_ptr = num_read;
933
934 return result;
935 }
936
937 static LONGEST
938 read_signed_leb128 (bfd *abfd, char *buf, unsigned int *bytes_read_ptr)
939 {
940 LONGEST result;
941 int shift;
942 unsigned int num_read;
943 unsigned char byte;
944
945 result = 0;
946 shift = 0;
947 num_read = 0;
948
949 do
950 {
951 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
952 buf++;
953 num_read++;
954 result |= ((byte & 0x7f) << shift);
955 shift += 7;
956 }
957 while (byte & 0x80);
958
959 if ((shift < 32) && (byte & 0x40))
960 result |= -(1 << shift);
961
962 *bytes_read_ptr = num_read;
963
964 return result;
965 }
966
967 static ULONGEST
968 read_initial_length (bfd *abfd, char *buf, unsigned int *bytes_read_ptr)
969 {
970 LONGEST result;
971
972 result = bfd_get_32 (abfd, (bfd_byte *) buf);
973 if (result == 0xffffffff)
974 {
975 result = bfd_get_64 (abfd, (bfd_byte *) buf + 4);
976 *bytes_read_ptr = 12;
977 }
978 else
979 *bytes_read_ptr = 4;
980
981 return result;
982 }
983 \f
984
985 /* Pointer encoding helper functions. */
986
987 /* GCC supports exception handling based on DWARF2 CFI. However, for
988 technical reasons, it encodes addresses in its FDE's in a different
989 way. Several "pointer encodings" are supported. The encoding
990 that's used for a particular FDE is determined by the 'R'
991 augmentation in the associated CIE. The argument of this
992 augmentation is a single byte.
993
994 The address can be encoded as 2 bytes, 4 bytes, 8 bytes, or as a
995 LEB128. This is encoded in bits 0, 1 and 2. Bit 3 encodes whether
996 the address is signed or unsigned. Bits 4, 5 and 6 encode how the
997 address should be interpreted (absolute, relative to the current
998 position in the FDE, ...). Bit 7, indicates that the address
999 should be dereferenced. */
1000
1001 static unsigned char
1002 encoding_for_size (unsigned int size)
1003 {
1004 switch (size)
1005 {
1006 case 2:
1007 return DW_EH_PE_udata2;
1008 case 4:
1009 return DW_EH_PE_udata4;
1010 case 8:
1011 return DW_EH_PE_udata8;
1012 default:
1013 internal_error (__FILE__, __LINE__, "Unsupported address size");
1014 }
1015 }
1016
1017 static unsigned int
1018 size_of_encoded_value (unsigned char encoding)
1019 {
1020 if (encoding == DW_EH_PE_omit)
1021 return 0;
1022
1023 switch (encoding & 0x07)
1024 {
1025 case DW_EH_PE_absptr:
1026 return TYPE_LENGTH (builtin_type_void_data_ptr);
1027 case DW_EH_PE_udata2:
1028 return 2;
1029 case DW_EH_PE_udata4:
1030 return 4;
1031 case DW_EH_PE_udata8:
1032 return 8;
1033 default:
1034 internal_error (__FILE__, __LINE__, "Invalid or unsupported encoding");
1035 }
1036 }
1037
1038 static CORE_ADDR
1039 read_encoded_value (struct comp_unit *unit, unsigned char encoding,
1040 char *buf, unsigned int *bytes_read_ptr)
1041 {
1042 int ptr_len = size_of_encoded_value (DW_EH_PE_absptr);
1043 ptrdiff_t offset;
1044 CORE_ADDR base;
1045
1046 /* GCC currently doesn't generate DW_EH_PE_indirect encodings for
1047 FDE's. */
1048 if (encoding & DW_EH_PE_indirect)
1049 internal_error (__FILE__, __LINE__,
1050 "Unsupported encoding: DW_EH_PE_indirect");
1051
1052 *bytes_read_ptr = 0;
1053
1054 switch (encoding & 0x70)
1055 {
1056 case DW_EH_PE_absptr:
1057 base = 0;
1058 break;
1059 case DW_EH_PE_pcrel:
1060 base = bfd_get_section_vma (unit->bfd, unit->dwarf_frame_section);
1061 base += (buf - unit->dwarf_frame_buffer);
1062 break;
1063 case DW_EH_PE_datarel:
1064 base = unit->dbase;
1065 break;
1066 case DW_EH_PE_aligned:
1067 base = 0;
1068 offset = buf - unit->dwarf_frame_buffer;
1069 if ((offset % ptr_len) != 0)
1070 {
1071 *bytes_read_ptr = ptr_len - (offset % ptr_len);
1072 buf += *bytes_read_ptr;
1073 }
1074 break;
1075 default:
1076 internal_error (__FILE__, __LINE__, "Invalid or unsupported encoding");
1077 }
1078
1079 if ((encoding & 0x0f) == 0x00)
1080 encoding |= encoding_for_size (ptr_len);
1081
1082 switch (encoding & 0x0f)
1083 {
1084 case DW_EH_PE_udata2:
1085 *bytes_read_ptr += 2;
1086 return (base + bfd_get_16 (unit->abfd, (bfd_byte *) buf));
1087 case DW_EH_PE_udata4:
1088 *bytes_read_ptr += 4;
1089 return (base + bfd_get_32 (unit->abfd, (bfd_byte *) buf));
1090 case DW_EH_PE_udata8:
1091 *bytes_read_ptr += 8;
1092 return (base + bfd_get_64 (unit->abfd, (bfd_byte *) buf));
1093 case DW_EH_PE_sdata2:
1094 *bytes_read_ptr += 2;
1095 return (base + bfd_get_signed_16 (unit->abfd, (bfd_byte *) buf));
1096 case DW_EH_PE_sdata4:
1097 *bytes_read_ptr += 4;
1098 return (base + bfd_get_signed_32 (unit->abfd, (bfd_byte *) buf));
1099 case DW_EH_PE_sdata8:
1100 *bytes_read_ptr += 8;
1101 return (base + bfd_get_signed_64 (unit->abfd, (bfd_byte *) buf));
1102 default:
1103 internal_error (__FILE__, __LINE__, "Invalid or unsupported encoding");
1104 }
1105 }
1106 \f
1107
1108 /* GCC uses a single CIE for all FDEs in a .debug_frame section.
1109 That's why we use a simple linked list here. */
1110
1111 static struct dwarf2_cie *
1112 find_cie (struct comp_unit *unit, ULONGEST cie_pointer)
1113 {
1114 struct dwarf2_cie *cie = unit->cie;
1115
1116 while (cie)
1117 {
1118 if (cie->cie_pointer == cie_pointer)
1119 return cie;
1120
1121 cie = cie->next;
1122 }
1123
1124 return NULL;
1125 }
1126
1127 static void
1128 add_cie (struct comp_unit *unit, struct dwarf2_cie *cie)
1129 {
1130 cie->next = unit->cie;
1131 unit->cie = cie;
1132 }
1133
1134 /* Find the FDE for *PC. Return a pointer to the FDE, and store the
1135 inital location associated with it into *PC. */
1136
1137 static struct dwarf2_fde *
1138 dwarf2_frame_find_fde (CORE_ADDR *pc)
1139 {
1140 struct objfile *objfile;
1141
1142 ALL_OBJFILES (objfile)
1143 {
1144 struct dwarf2_fde *fde;
1145 CORE_ADDR offset;
1146
1147 fde = objfile_data (objfile, dwarf2_frame_data);
1148 if (fde == NULL)
1149 continue;
1150
1151 gdb_assert (objfile->section_offsets);
1152 offset = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1153
1154 while (fde)
1155 {
1156 if (*pc >= fde->initial_location + offset
1157 && *pc < fde->initial_location + offset + fde->address_range)
1158 {
1159 *pc = fde->initial_location + offset;
1160 return fde;
1161 }
1162
1163 fde = fde->next;
1164 }
1165 }
1166
1167 return NULL;
1168 }
1169
1170 static void
1171 add_fde (struct comp_unit *unit, struct dwarf2_fde *fde)
1172 {
1173 fde->next = objfile_data (unit->objfile, dwarf2_frame_data);
1174 set_objfile_data (unit->objfile, dwarf2_frame_data, fde);
1175 }
1176
1177 #ifdef CC_HAS_LONG_LONG
1178 #define DW64_CIE_ID 0xffffffffffffffffULL
1179 #else
1180 #define DW64_CIE_ID ~0
1181 #endif
1182
1183 static char *decode_frame_entry (struct comp_unit *unit, char *start,
1184 int eh_frame_p);
1185
1186 /* Decode the next CIE or FDE. Return NULL if invalid input, otherwise
1187 the next byte to be processed. */
1188 static char *
1189 decode_frame_entry_1 (struct comp_unit *unit, char *start, int eh_frame_p)
1190 {
1191 char *buf;
1192 LONGEST length;
1193 unsigned int bytes_read;
1194 int dwarf64_p;
1195 ULONGEST cie_id;
1196 ULONGEST cie_pointer;
1197 char *end;
1198
1199 buf = start;
1200 length = read_initial_length (unit->abfd, buf, &bytes_read);
1201 buf += bytes_read;
1202 end = buf + length;
1203
1204 /* Are we still within the section? */
1205 if (end > unit->dwarf_frame_buffer + unit->dwarf_frame_size)
1206 return NULL;
1207
1208 if (length == 0)
1209 return end;
1210
1211 /* Distinguish between 32 and 64-bit encoded frame info. */
1212 dwarf64_p = (bytes_read == 12);
1213
1214 /* In a .eh_frame section, zero is used to distinguish CIEs from FDEs. */
1215 if (eh_frame_p)
1216 cie_id = 0;
1217 else if (dwarf64_p)
1218 cie_id = DW64_CIE_ID;
1219 else
1220 cie_id = DW_CIE_ID;
1221
1222 if (dwarf64_p)
1223 {
1224 cie_pointer = read_8_bytes (unit->abfd, buf);
1225 buf += 8;
1226 }
1227 else
1228 {
1229 cie_pointer = read_4_bytes (unit->abfd, buf);
1230 buf += 4;
1231 }
1232
1233 if (cie_pointer == cie_id)
1234 {
1235 /* This is a CIE. */
1236 struct dwarf2_cie *cie;
1237 char *augmentation;
1238
1239 /* Record the offset into the .debug_frame section of this CIE. */
1240 cie_pointer = start - unit->dwarf_frame_buffer;
1241
1242 /* Check whether we've already read it. */
1243 if (find_cie (unit, cie_pointer))
1244 return end;
1245
1246 cie = (struct dwarf2_cie *)
1247 obstack_alloc (&unit->objfile->psymbol_obstack,
1248 sizeof (struct dwarf2_cie));
1249 cie->initial_instructions = NULL;
1250 cie->cie_pointer = cie_pointer;
1251
1252 /* The encoding for FDE's in a normal .debug_frame section
1253 depends on the target address size as specified in the
1254 Compilation Unit Header. */
1255 cie->encoding = encoding_for_size (unit->addr_size);
1256
1257 /* Check version number. */
1258 if (read_1_byte (unit->abfd, buf) != DW_CIE_VERSION)
1259 return NULL;
1260 buf += 1;
1261
1262 /* Interpret the interesting bits of the augmentation. */
1263 augmentation = buf;
1264 buf = augmentation + strlen (augmentation) + 1;
1265
1266 /* The GCC 2.x "eh" augmentation has a pointer immediately
1267 following the augmentation string, so it must be handled
1268 first. */
1269 if (augmentation[0] == 'e' && augmentation[1] == 'h')
1270 {
1271 /* Skip. */
1272 buf += TYPE_LENGTH (builtin_type_void_data_ptr);
1273 augmentation += 2;
1274 }
1275
1276 cie->code_alignment_factor =
1277 read_unsigned_leb128 (unit->abfd, buf, &bytes_read);
1278 buf += bytes_read;
1279
1280 cie->data_alignment_factor =
1281 read_signed_leb128 (unit->abfd, buf, &bytes_read);
1282 buf += bytes_read;
1283
1284 cie->return_address_register = read_1_byte (unit->abfd, buf);
1285 buf += 1;
1286
1287 cie->saw_z_augmentation = (*augmentation == 'z');
1288 if (cie->saw_z_augmentation)
1289 {
1290 ULONGEST length;
1291
1292 length = read_unsigned_leb128 (unit->abfd, buf, &bytes_read);
1293 buf += bytes_read;
1294 if (buf > end)
1295 return NULL;
1296 cie->initial_instructions = buf + length;
1297 augmentation++;
1298 }
1299
1300 while (*augmentation)
1301 {
1302 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
1303 if (*augmentation == 'L')
1304 {
1305 /* Skip. */
1306 buf++;
1307 augmentation++;
1308 }
1309
1310 /* "R" indicates a byte indicating how FDE addresses are encoded. */
1311 else if (*augmentation == 'R')
1312 {
1313 cie->encoding = *buf++;
1314 augmentation++;
1315 }
1316
1317 /* "P" indicates a personality routine in the CIE augmentation. */
1318 else if (*augmentation == 'P')
1319 {
1320 /* Skip. */
1321 buf += size_of_encoded_value (*buf++);
1322 augmentation++;
1323 }
1324
1325 /* Otherwise we have an unknown augmentation.
1326 Bail out unless we saw a 'z' prefix. */
1327 else
1328 {
1329 if (cie->initial_instructions == NULL)
1330 return end;
1331
1332 /* Skip unknown augmentations. */
1333 buf = cie->initial_instructions;
1334 break;
1335 }
1336 }
1337
1338 cie->initial_instructions = buf;
1339 cie->end = end;
1340
1341 add_cie (unit, cie);
1342 }
1343 else
1344 {
1345 /* This is a FDE. */
1346 struct dwarf2_fde *fde;
1347
1348 /* In an .eh_frame section, the CIE pointer is the delta between the
1349 address within the FDE where the CIE pointer is stored and the
1350 address of the CIE. Convert it to an offset into the .eh_frame
1351 section. */
1352 if (eh_frame_p)
1353 {
1354 cie_pointer = buf - unit->dwarf_frame_buffer - cie_pointer;
1355 cie_pointer -= (dwarf64_p ? 8 : 4);
1356 }
1357
1358 /* In either case, validate the result is still within the section. */
1359 if (cie_pointer >= unit->dwarf_frame_size)
1360 return NULL;
1361
1362 fde = (struct dwarf2_fde *)
1363 obstack_alloc (&unit->objfile->psymbol_obstack,
1364 sizeof (struct dwarf2_fde));
1365 fde->cie = find_cie (unit, cie_pointer);
1366 if (fde->cie == NULL)
1367 {
1368 decode_frame_entry (unit, unit->dwarf_frame_buffer + cie_pointer,
1369 eh_frame_p);
1370 fde->cie = find_cie (unit, cie_pointer);
1371 }
1372
1373 gdb_assert (fde->cie != NULL);
1374
1375 fde->initial_location =
1376 read_encoded_value (unit, fde->cie->encoding, buf, &bytes_read);
1377 buf += bytes_read;
1378
1379 fde->address_range =
1380 read_encoded_value (unit, fde->cie->encoding & 0x0f, buf, &bytes_read);
1381 buf += bytes_read;
1382
1383 /* A 'z' augmentation in the CIE implies the presence of an
1384 augmentation field in the FDE as well. The only thing known
1385 to be in here at present is the LSDA entry for EH. So we
1386 can skip the whole thing. */
1387 if (fde->cie->saw_z_augmentation)
1388 {
1389 ULONGEST length;
1390
1391 length = read_unsigned_leb128 (unit->abfd, buf, &bytes_read);
1392 buf += bytes_read + length;
1393 if (buf > end)
1394 return NULL;
1395 }
1396
1397 fde->instructions = buf;
1398 fde->end = end;
1399
1400 add_fde (unit, fde);
1401 }
1402
1403 return end;
1404 }
1405
1406 /* Read a CIE or FDE in BUF and decode it. */
1407 static char *
1408 decode_frame_entry (struct comp_unit *unit, char *start, int eh_frame_p)
1409 {
1410 enum { NONE, ALIGN4, ALIGN8, FAIL } workaround = NONE;
1411 char *ret;
1412 const char *msg;
1413 ptrdiff_t start_offset;
1414
1415 while (1)
1416 {
1417 ret = decode_frame_entry_1 (unit, start, eh_frame_p);
1418 if (ret != NULL)
1419 break;
1420
1421 /* We have corrupt input data of some form. */
1422
1423 /* ??? Try, weakly, to work around compiler/assembler/linker bugs
1424 and mismatches wrt padding and alignment of debug sections. */
1425 /* Note that there is no requirement in the standard for any
1426 alignment at all in the frame unwind sections. Testing for
1427 alignment before trying to interpret data would be incorrect.
1428
1429 However, GCC traditionally arranged for frame sections to be
1430 sized such that the FDE length and CIE fields happen to be
1431 aligned (in theory, for performance). This, unfortunately,
1432 was done with .align directives, which had the side effect of
1433 forcing the section to be aligned by the linker.
1434
1435 This becomes a problem when you have some other producer that
1436 creates frame sections that are not as strictly aligned. That
1437 produces a hole in the frame info that gets filled by the
1438 linker with zeros.
1439
1440 The GCC behaviour is arguably a bug, but it's effectively now
1441 part of the ABI, so we're now stuck with it, at least at the
1442 object file level. A smart linker may decide, in the process
1443 of compressing duplicate CIE information, that it can rewrite
1444 the entire output section without this extra padding. */
1445
1446 start_offset = start - unit->dwarf_frame_buffer;
1447 if (workaround < ALIGN4 && (start_offset & 3) != 0)
1448 {
1449 start += 4 - (start_offset & 3);
1450 workaround = ALIGN4;
1451 continue;
1452 }
1453 if (workaround < ALIGN8 && (start_offset & 7) != 0)
1454 {
1455 start += 8 - (start_offset & 7);
1456 workaround = ALIGN8;
1457 continue;
1458 }
1459
1460 /* Nothing left to try. Arrange to return as if we've consumed
1461 the entire input section. Hopefully we'll get valid info from
1462 the other of .debug_frame/.eh_frame. */
1463 workaround = FAIL;
1464 ret = unit->dwarf_frame_buffer + unit->dwarf_frame_size;
1465 break;
1466 }
1467
1468 switch (workaround)
1469 {
1470 case NONE:
1471 break;
1472
1473 case ALIGN4:
1474 complaint (&symfile_complaints,
1475 "Corrupt data in %s:%s; align 4 workaround apparently succeeded",
1476 unit->dwarf_frame_section->owner->filename,
1477 unit->dwarf_frame_section->name);
1478 break;
1479
1480 case ALIGN8:
1481 complaint (&symfile_complaints,
1482 "Corrupt data in %s:%s; align 8 workaround apparently succeeded",
1483 unit->dwarf_frame_section->owner->filename,
1484 unit->dwarf_frame_section->name);
1485 break;
1486
1487 default:
1488 complaint (&symfile_complaints,
1489 "Corrupt data in %s:%s",
1490 unit->dwarf_frame_section->owner->filename,
1491 unit->dwarf_frame_section->name);
1492 break;
1493 }
1494
1495 return ret;
1496 }
1497
1498 \f
1499
1500 /* FIXME: kettenis/20030504: This still needs to be integrated with
1501 dwarf2read.c in a better way. */
1502
1503 /* Imported from dwarf2read.c. */
1504 extern file_ptr dwarf_frame_offset;
1505 extern unsigned int dwarf_frame_size;
1506 extern asection *dwarf_frame_section;
1507 extern file_ptr dwarf_eh_frame_offset;
1508 extern unsigned int dwarf_eh_frame_size;
1509 extern asection *dwarf_eh_frame_section;
1510
1511 /* Imported from dwarf2read.c. */
1512 extern char *dwarf2_read_section (struct objfile *objfile, file_ptr offset,
1513 unsigned int size, asection *sectp);
1514
1515 void
1516 dwarf2_build_frame_info (struct objfile *objfile)
1517 {
1518 struct comp_unit unit;
1519 char *frame_ptr;
1520
1521 /* Build a minimal decoding of the DWARF2 compilation unit. */
1522 unit.abfd = objfile->obfd;
1523 unit.objfile = objfile;
1524 unit.addr_size = objfile->obfd->arch_info->bits_per_address / 8;
1525 unit.dbase = 0;
1526
1527 /* First add the information from the .eh_frame section. That way,
1528 the FDEs from that section are searched last. */
1529 if (dwarf_eh_frame_offset)
1530 {
1531 asection *got;
1532
1533 unit.cie = NULL;
1534 unit.dwarf_frame_buffer = dwarf2_read_section (objfile,
1535 dwarf_eh_frame_offset,
1536 dwarf_eh_frame_size,
1537 dwarf_eh_frame_section);
1538
1539 unit.dwarf_frame_size = dwarf_eh_frame_size;
1540 unit.dwarf_frame_section = dwarf_eh_frame_section;
1541
1542 /* FIXME: kettenis/20030602: This is the DW_EH_PE_datarel base
1543 that for the i386/amd64 target, which currently is the only
1544 target in GCC that supports/uses the DW_EH_PE_datarel
1545 encoding. */
1546 got = bfd_get_section_by_name (unit.abfd, ".got");
1547 if (got)
1548 unit.dbase = got->vma;
1549
1550 frame_ptr = unit.dwarf_frame_buffer;
1551 while (frame_ptr < unit.dwarf_frame_buffer + unit.dwarf_frame_size)
1552 frame_ptr = decode_frame_entry (&unit, frame_ptr, 1);
1553 }
1554
1555 if (dwarf_frame_offset)
1556 {
1557 unit.cie = NULL;
1558 unit.dwarf_frame_buffer = dwarf2_read_section (objfile,
1559 dwarf_frame_offset,
1560 dwarf_frame_size,
1561 dwarf_frame_section);
1562 unit.dwarf_frame_size = dwarf_frame_size;
1563 unit.dwarf_frame_section = dwarf_frame_section;
1564
1565 frame_ptr = unit.dwarf_frame_buffer;
1566 while (frame_ptr < unit.dwarf_frame_buffer + unit.dwarf_frame_size)
1567 frame_ptr = decode_frame_entry (&unit, frame_ptr, 0);
1568 }
1569 }
1570
1571 /* Provide a prototype to silence -Wmissing-prototypes. */
1572 void _initialize_dwarf2_frame (void);
1573
1574 void
1575 _initialize_dwarf2_frame (void)
1576 {
1577 dwarf2_frame_data = register_objfile_data ();
1578 }