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