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