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