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