]>
Commit | Line | Data |
---|---|---|
cfc14b3a MK |
1 | /* Frame unwinder for frames with DWARF Call Frame Information. |
2 | ||
197e01b6 | 3 | Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc. |
cfc14b3a MK |
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 | |
197e01b6 EZ |
21 | Foundation, Inc., 51 Franklin Street, Fifth Floor, |
22 | Boston, MA 02110-1301, USA. */ | |
cfc14b3a MK |
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. */ | |
852483bc MK |
63 | gdb_byte *initial_instructions; |
64 | gdb_byte *end; | |
cfc14b3a MK |
65 | |
66 | /* Encoding of addresses. */ | |
852483bc | 67 | gdb_byte encoding; |
cfc14b3a | 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. */ | |
852483bc MK |
89 | gdb_byte *instructions; |
90 | gdb_byte *end; | |
cfc14b3a | 91 | |
4bf8967c AS |
92 | /* True if this FDE is read from a .eh_frame instead of a .debug_frame |
93 | section. */ | |
94 | unsigned char eh_frame_p; | |
95 | ||
cfc14b3a MK |
96 | struct dwarf2_fde *next; |
97 | }; | |
98 | ||
99 | static struct dwarf2_fde *dwarf2_frame_find_fde (CORE_ADDR *pc); | |
100 | \f | |
101 | ||
102 | /* Structure describing a frame state. */ | |
103 | ||
104 | struct dwarf2_frame_state | |
105 | { | |
106 | /* Each register save state can be described in terms of a CFA slot, | |
107 | another register, or a location expression. */ | |
108 | struct dwarf2_frame_state_reg_info | |
109 | { | |
05cbe71a | 110 | struct dwarf2_frame_state_reg *reg; |
cfc14b3a MK |
111 | int num_regs; |
112 | ||
113 | /* Used to implement DW_CFA_remember_state. */ | |
114 | struct dwarf2_frame_state_reg_info *prev; | |
115 | } regs; | |
116 | ||
117 | LONGEST cfa_offset; | |
118 | ULONGEST cfa_reg; | |
852483bc | 119 | gdb_byte *cfa_exp; |
cfc14b3a MK |
120 | enum { |
121 | CFA_UNSET, | |
122 | CFA_REG_OFFSET, | |
123 | CFA_EXP | |
124 | } cfa_how; | |
125 | ||
126 | /* The PC described by the current frame state. */ | |
127 | CORE_ADDR pc; | |
128 | ||
129 | /* Initial register set from the CIE. | |
130 | Used to implement DW_CFA_restore. */ | |
131 | struct dwarf2_frame_state_reg_info initial; | |
132 | ||
133 | /* The information we care about from the CIE. */ | |
134 | LONGEST data_align; | |
135 | ULONGEST code_align; | |
136 | ULONGEST retaddr_column; | |
137 | }; | |
138 | ||
139 | /* Store the length the expression for the CFA in the `cfa_reg' field, | |
140 | which is unused in that case. */ | |
141 | #define cfa_exp_len cfa_reg | |
142 | ||
143 | /* Assert that the register set RS is large enough to store NUM_REGS | |
144 | columns. If necessary, enlarge the register set. */ | |
145 | ||
146 | static void | |
147 | dwarf2_frame_state_alloc_regs (struct dwarf2_frame_state_reg_info *rs, | |
148 | int num_regs) | |
149 | { | |
150 | size_t size = sizeof (struct dwarf2_frame_state_reg); | |
151 | ||
152 | if (num_regs <= rs->num_regs) | |
153 | return; | |
154 | ||
155 | rs->reg = (struct dwarf2_frame_state_reg *) | |
156 | xrealloc (rs->reg, num_regs * size); | |
157 | ||
158 | /* Initialize newly allocated registers. */ | |
2473a4a9 | 159 | memset (rs->reg + rs->num_regs, 0, (num_regs - rs->num_regs) * size); |
cfc14b3a MK |
160 | rs->num_regs = num_regs; |
161 | } | |
162 | ||
163 | /* Copy the register columns in register set RS into newly allocated | |
164 | memory and return a pointer to this newly created copy. */ | |
165 | ||
166 | static struct dwarf2_frame_state_reg * | |
167 | dwarf2_frame_state_copy_regs (struct dwarf2_frame_state_reg_info *rs) | |
168 | { | |
d10891d4 | 169 | size_t size = rs->num_regs * sizeof (struct dwarf2_frame_state_reg); |
cfc14b3a MK |
170 | struct dwarf2_frame_state_reg *reg; |
171 | ||
172 | reg = (struct dwarf2_frame_state_reg *) xmalloc (size); | |
173 | memcpy (reg, rs->reg, size); | |
174 | ||
175 | return reg; | |
176 | } | |
177 | ||
178 | /* Release the memory allocated to register set RS. */ | |
179 | ||
180 | static void | |
181 | dwarf2_frame_state_free_regs (struct dwarf2_frame_state_reg_info *rs) | |
182 | { | |
183 | if (rs) | |
184 | { | |
185 | dwarf2_frame_state_free_regs (rs->prev); | |
186 | ||
187 | xfree (rs->reg); | |
188 | xfree (rs); | |
189 | } | |
190 | } | |
191 | ||
192 | /* Release the memory allocated to the frame state FS. */ | |
193 | ||
194 | static void | |
195 | dwarf2_frame_state_free (void *p) | |
196 | { | |
197 | struct dwarf2_frame_state *fs = p; | |
198 | ||
199 | dwarf2_frame_state_free_regs (fs->initial.prev); | |
200 | dwarf2_frame_state_free_regs (fs->regs.prev); | |
201 | xfree (fs->initial.reg); | |
202 | xfree (fs->regs.reg); | |
203 | xfree (fs); | |
204 | } | |
205 | \f | |
206 | ||
207 | /* Helper functions for execute_stack_op. */ | |
208 | ||
209 | static CORE_ADDR | |
210 | read_reg (void *baton, int reg) | |
211 | { | |
212 | struct frame_info *next_frame = (struct frame_info *) baton; | |
05cbe71a | 213 | struct gdbarch *gdbarch = get_frame_arch (next_frame); |
cfc14b3a | 214 | int regnum; |
852483bc | 215 | gdb_byte *buf; |
cfc14b3a MK |
216 | |
217 | regnum = DWARF2_REG_TO_REGNUM (reg); | |
218 | ||
852483bc | 219 | buf = alloca (register_size (gdbarch, regnum)); |
cfc14b3a MK |
220 | frame_unwind_register (next_frame, regnum, buf); |
221 | return extract_typed_address (buf, builtin_type_void_data_ptr); | |
222 | } | |
223 | ||
224 | static void | |
852483bc | 225 | read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len) |
cfc14b3a MK |
226 | { |
227 | read_memory (addr, buf, len); | |
228 | } | |
229 | ||
230 | static void | |
852483bc | 231 | no_get_frame_base (void *baton, gdb_byte **start, size_t *length) |
cfc14b3a MK |
232 | { |
233 | internal_error (__FILE__, __LINE__, | |
e2e0b3e5 | 234 | _("Support for DW_OP_fbreg is unimplemented")); |
cfc14b3a MK |
235 | } |
236 | ||
237 | static CORE_ADDR | |
238 | no_get_tls_address (void *baton, CORE_ADDR offset) | |
239 | { | |
240 | internal_error (__FILE__, __LINE__, | |
e2e0b3e5 | 241 | _("Support for DW_OP_GNU_push_tls_address is unimplemented")); |
cfc14b3a MK |
242 | } |
243 | ||
244 | static CORE_ADDR | |
852483bc | 245 | execute_stack_op (gdb_byte *exp, ULONGEST len, |
cfc14b3a MK |
246 | struct frame_info *next_frame, CORE_ADDR initial) |
247 | { | |
248 | struct dwarf_expr_context *ctx; | |
249 | CORE_ADDR result; | |
250 | ||
251 | ctx = new_dwarf_expr_context (); | |
252 | ctx->baton = next_frame; | |
253 | ctx->read_reg = read_reg; | |
254 | ctx->read_mem = read_mem; | |
255 | ctx->get_frame_base = no_get_frame_base; | |
256 | ctx->get_tls_address = no_get_tls_address; | |
257 | ||
258 | dwarf_expr_push (ctx, initial); | |
259 | dwarf_expr_eval (ctx, exp, len); | |
260 | result = dwarf_expr_fetch (ctx, 0); | |
261 | ||
262 | if (ctx->in_reg) | |
263 | result = read_reg (next_frame, result); | |
264 | ||
265 | free_dwarf_expr_context (ctx); | |
266 | ||
267 | return result; | |
268 | } | |
269 | \f | |
270 | ||
271 | static void | |
852483bc | 272 | execute_cfa_program (gdb_byte *insn_ptr, gdb_byte *insn_end, |
cfc14b3a | 273 | struct frame_info *next_frame, |
4bf8967c | 274 | struct dwarf2_frame_state *fs, int eh_frame_p) |
cfc14b3a MK |
275 | { |
276 | CORE_ADDR pc = frame_pc_unwind (next_frame); | |
277 | int bytes_read; | |
4bf8967c | 278 | struct gdbarch *gdbarch = get_frame_arch (next_frame); |
cfc14b3a MK |
279 | |
280 | while (insn_ptr < insn_end && fs->pc <= pc) | |
281 | { | |
852483bc | 282 | gdb_byte insn = *insn_ptr++; |
cfc14b3a MK |
283 | ULONGEST utmp, reg; |
284 | LONGEST offset; | |
285 | ||
286 | if ((insn & 0xc0) == DW_CFA_advance_loc) | |
287 | fs->pc += (insn & 0x3f) * fs->code_align; | |
288 | else if ((insn & 0xc0) == DW_CFA_offset) | |
289 | { | |
290 | reg = insn & 0x3f; | |
4bf8967c AS |
291 | if (eh_frame_p) |
292 | reg = dwarf2_frame_eh_frame_regnum (gdbarch, reg); | |
cfc14b3a MK |
293 | insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp); |
294 | offset = utmp * fs->data_align; | |
295 | dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1); | |
05cbe71a | 296 | fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET; |
cfc14b3a MK |
297 | fs->regs.reg[reg].loc.offset = offset; |
298 | } | |
299 | else if ((insn & 0xc0) == DW_CFA_restore) | |
300 | { | |
301 | gdb_assert (fs->initial.reg); | |
302 | reg = insn & 0x3f; | |
4bf8967c AS |
303 | if (eh_frame_p) |
304 | reg = dwarf2_frame_eh_frame_regnum (gdbarch, reg); | |
cfc14b3a | 305 | dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1); |
92ad9f6b FR |
306 | if (reg < fs->initial.num_regs) |
307 | fs->regs.reg[reg] = fs->initial.reg[reg]; | |
308 | else | |
309 | fs->regs.reg[reg].how = DWARF2_FRAME_REG_UNSPECIFIED; | |
310 | ||
311 | if (fs->regs.reg[reg].how == DWARF2_FRAME_REG_UNSPECIFIED) | |
312 | complaint (&symfile_complaints, _("\ | |
313 | incomplete CFI data; DW_CFA_restore unspecified\n\ | |
314 | register %s (#%d) at 0x%s"), | |
315 | REGISTER_NAME(DWARF2_REG_TO_REGNUM(reg)), | |
316 | DWARF2_REG_TO_REGNUM(reg), paddr (fs->pc)); | |
cfc14b3a MK |
317 | } |
318 | else | |
319 | { | |
320 | switch (insn) | |
321 | { | |
322 | case DW_CFA_set_loc: | |
323 | fs->pc = dwarf2_read_address (insn_ptr, insn_end, &bytes_read); | |
324 | insn_ptr += bytes_read; | |
325 | break; | |
326 | ||
327 | case DW_CFA_advance_loc1: | |
328 | utmp = extract_unsigned_integer (insn_ptr, 1); | |
329 | fs->pc += utmp * fs->code_align; | |
330 | insn_ptr++; | |
331 | break; | |
332 | case DW_CFA_advance_loc2: | |
333 | utmp = extract_unsigned_integer (insn_ptr, 2); | |
334 | fs->pc += utmp * fs->code_align; | |
335 | insn_ptr += 2; | |
336 | break; | |
337 | case DW_CFA_advance_loc4: | |
338 | utmp = extract_unsigned_integer (insn_ptr, 4); | |
339 | fs->pc += utmp * fs->code_align; | |
340 | insn_ptr += 4; | |
341 | break; | |
342 | ||
343 | case DW_CFA_offset_extended: | |
344 | insn_ptr = read_uleb128 (insn_ptr, insn_end, ®); | |
4bf8967c AS |
345 | if (eh_frame_p) |
346 | reg = dwarf2_frame_eh_frame_regnum (gdbarch, reg); | |
cfc14b3a MK |
347 | insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp); |
348 | offset = utmp * fs->data_align; | |
349 | dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1); | |
05cbe71a | 350 | fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET; |
cfc14b3a MK |
351 | fs->regs.reg[reg].loc.offset = offset; |
352 | break; | |
353 | ||
354 | case DW_CFA_restore_extended: | |
355 | gdb_assert (fs->initial.reg); | |
356 | insn_ptr = read_uleb128 (insn_ptr, insn_end, ®); | |
4bf8967c AS |
357 | if (eh_frame_p) |
358 | reg = dwarf2_frame_eh_frame_regnum (gdbarch, reg); | |
cfc14b3a MK |
359 | dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1); |
360 | fs->regs.reg[reg] = fs->initial.reg[reg]; | |
361 | break; | |
362 | ||
363 | case DW_CFA_undefined: | |
364 | insn_ptr = read_uleb128 (insn_ptr, insn_end, ®); | |
4bf8967c AS |
365 | if (eh_frame_p) |
366 | reg = dwarf2_frame_eh_frame_regnum (gdbarch, reg); | |
cfc14b3a | 367 | dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1); |
05cbe71a | 368 | fs->regs.reg[reg].how = DWARF2_FRAME_REG_UNDEFINED; |
cfc14b3a MK |
369 | break; |
370 | ||
371 | case DW_CFA_same_value: | |
372 | insn_ptr = read_uleb128 (insn_ptr, insn_end, ®); | |
4bf8967c AS |
373 | if (eh_frame_p) |
374 | reg = dwarf2_frame_eh_frame_regnum (gdbarch, reg); | |
cfc14b3a | 375 | dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1); |
05cbe71a | 376 | fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAME_VALUE; |
cfc14b3a MK |
377 | break; |
378 | ||
379 | case DW_CFA_register: | |
380 | insn_ptr = read_uleb128 (insn_ptr, insn_end, ®); | |
4bf8967c AS |
381 | if (eh_frame_p) |
382 | reg = dwarf2_frame_eh_frame_regnum (gdbarch, reg); | |
cfc14b3a | 383 | insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp); |
4bf8967c AS |
384 | if (eh_frame_p) |
385 | utmp = dwarf2_frame_eh_frame_regnum (gdbarch, utmp); | |
cfc14b3a | 386 | dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1); |
05cbe71a | 387 | fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_REG; |
cfc14b3a MK |
388 | fs->regs.reg[reg].loc.reg = utmp; |
389 | break; | |
390 | ||
391 | case DW_CFA_remember_state: | |
392 | { | |
393 | struct dwarf2_frame_state_reg_info *new_rs; | |
394 | ||
395 | new_rs = XMALLOC (struct dwarf2_frame_state_reg_info); | |
396 | *new_rs = fs->regs; | |
397 | fs->regs.reg = dwarf2_frame_state_copy_regs (&fs->regs); | |
398 | fs->regs.prev = new_rs; | |
399 | } | |
400 | break; | |
401 | ||
402 | case DW_CFA_restore_state: | |
403 | { | |
404 | struct dwarf2_frame_state_reg_info *old_rs = fs->regs.prev; | |
405 | ||
50ea7769 MK |
406 | if (old_rs == NULL) |
407 | { | |
e2e0b3e5 AC |
408 | complaint (&symfile_complaints, _("\ |
409 | bad CFI data; mismatched DW_CFA_restore_state at 0x%s"), paddr (fs->pc)); | |
50ea7769 MK |
410 | } |
411 | else | |
412 | { | |
413 | xfree (fs->regs.reg); | |
414 | fs->regs = *old_rs; | |
415 | xfree (old_rs); | |
416 | } | |
cfc14b3a MK |
417 | } |
418 | break; | |
419 | ||
420 | case DW_CFA_def_cfa: | |
421 | insn_ptr = read_uleb128 (insn_ptr, insn_end, &fs->cfa_reg); | |
422 | insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp); | |
423 | fs->cfa_offset = utmp; | |
424 | fs->cfa_how = CFA_REG_OFFSET; | |
425 | break; | |
426 | ||
427 | case DW_CFA_def_cfa_register: | |
428 | insn_ptr = read_uleb128 (insn_ptr, insn_end, &fs->cfa_reg); | |
4bf8967c AS |
429 | if (eh_frame_p) |
430 | fs->cfa_reg = dwarf2_frame_eh_frame_regnum (gdbarch, | |
431 | fs->cfa_reg); | |
cfc14b3a MK |
432 | fs->cfa_how = CFA_REG_OFFSET; |
433 | break; | |
434 | ||
435 | case DW_CFA_def_cfa_offset: | |
852483bc MK |
436 | insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp); |
437 | fs->cfa_offset = utmp; | |
cfc14b3a MK |
438 | /* cfa_how deliberately not set. */ |
439 | break; | |
440 | ||
a8504492 MK |
441 | case DW_CFA_nop: |
442 | break; | |
443 | ||
cfc14b3a MK |
444 | case DW_CFA_def_cfa_expression: |
445 | insn_ptr = read_uleb128 (insn_ptr, insn_end, &fs->cfa_exp_len); | |
446 | fs->cfa_exp = insn_ptr; | |
447 | fs->cfa_how = CFA_EXP; | |
448 | insn_ptr += fs->cfa_exp_len; | |
449 | break; | |
450 | ||
451 | case DW_CFA_expression: | |
452 | insn_ptr = read_uleb128 (insn_ptr, insn_end, ®); | |
4bf8967c AS |
453 | if (eh_frame_p) |
454 | reg = dwarf2_frame_eh_frame_regnum (gdbarch, reg); | |
cfc14b3a MK |
455 | dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1); |
456 | insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp); | |
457 | fs->regs.reg[reg].loc.exp = insn_ptr; | |
458 | fs->regs.reg[reg].exp_len = utmp; | |
05cbe71a | 459 | fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_EXP; |
cfc14b3a MK |
460 | insn_ptr += utmp; |
461 | break; | |
462 | ||
a8504492 MK |
463 | case DW_CFA_offset_extended_sf: |
464 | insn_ptr = read_uleb128 (insn_ptr, insn_end, ®); | |
4bf8967c AS |
465 | if (eh_frame_p) |
466 | reg = dwarf2_frame_eh_frame_regnum (gdbarch, reg); | |
a8504492 | 467 | insn_ptr = read_sleb128 (insn_ptr, insn_end, &offset); |
f6da8dd8 | 468 | offset *= fs->data_align; |
a8504492 | 469 | dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1); |
05cbe71a | 470 | fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET; |
a8504492 MK |
471 | fs->regs.reg[reg].loc.offset = offset; |
472 | break; | |
473 | ||
474 | case DW_CFA_def_cfa_sf: | |
475 | insn_ptr = read_uleb128 (insn_ptr, insn_end, &fs->cfa_reg); | |
4bf8967c AS |
476 | if (eh_frame_p) |
477 | fs->cfa_reg = dwarf2_frame_eh_frame_regnum (gdbarch, | |
478 | fs->cfa_reg); | |
a8504492 MK |
479 | insn_ptr = read_sleb128 (insn_ptr, insn_end, &offset); |
480 | fs->cfa_offset = offset * fs->data_align; | |
481 | fs->cfa_how = CFA_REG_OFFSET; | |
482 | break; | |
483 | ||
484 | case DW_CFA_def_cfa_offset_sf: | |
485 | insn_ptr = read_sleb128 (insn_ptr, insn_end, &offset); | |
486 | fs->cfa_offset = offset * fs->data_align; | |
487 | /* cfa_how deliberately not set. */ | |
cfc14b3a MK |
488 | break; |
489 | ||
a77f4086 MK |
490 | case DW_CFA_GNU_window_save: |
491 | /* This is SPARC-specific code, and contains hard-coded | |
492 | constants for the register numbering scheme used by | |
493 | GCC. Rather than having a architecture-specific | |
494 | operation that's only ever used by a single | |
495 | architecture, we provide the implementation here. | |
496 | Incidentally that's what GCC does too in its | |
497 | unwinder. */ | |
498 | { | |
499 | struct gdbarch *gdbarch = get_frame_arch (next_frame); | |
500 | int size = register_size(gdbarch, 0); | |
501 | dwarf2_frame_state_alloc_regs (&fs->regs, 32); | |
502 | for (reg = 8; reg < 16; reg++) | |
503 | { | |
504 | fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_REG; | |
505 | fs->regs.reg[reg].loc.reg = reg + 16; | |
506 | } | |
507 | for (reg = 16; reg < 32; reg++) | |
508 | { | |
509 | fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET; | |
510 | fs->regs.reg[reg].loc.offset = (reg - 16) * size; | |
511 | } | |
512 | } | |
513 | break; | |
514 | ||
cfc14b3a MK |
515 | case DW_CFA_GNU_args_size: |
516 | /* Ignored. */ | |
517 | insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp); | |
518 | break; | |
519 | ||
520 | default: | |
e2e0b3e5 | 521 | internal_error (__FILE__, __LINE__, _("Unknown CFI encountered.")); |
cfc14b3a MK |
522 | } |
523 | } | |
524 | } | |
525 | ||
526 | /* Don't allow remember/restore between CIE and FDE programs. */ | |
527 | dwarf2_frame_state_free_regs (fs->regs.prev); | |
528 | fs->regs.prev = NULL; | |
529 | } | |
8f22cb90 | 530 | \f |
cfc14b3a | 531 | |
8f22cb90 | 532 | /* Architecture-specific operations. */ |
cfc14b3a | 533 | |
8f22cb90 MK |
534 | /* Per-architecture data key. */ |
535 | static struct gdbarch_data *dwarf2_frame_data; | |
536 | ||
537 | struct dwarf2_frame_ops | |
538 | { | |
539 | /* Pre-initialize the register state REG for register REGNUM. */ | |
aff37fc1 DM |
540 | void (*init_reg) (struct gdbarch *, int, struct dwarf2_frame_state_reg *, |
541 | struct frame_info *); | |
3ed09a32 DJ |
542 | |
543 | /* Check whether the frame preceding NEXT_FRAME will be a signal | |
544 | trampoline. */ | |
545 | int (*signal_frame_p) (struct gdbarch *, struct frame_info *); | |
4bf8967c AS |
546 | |
547 | /* Convert .eh_frame register number to DWARF register number. */ | |
548 | int (*eh_frame_regnum) (struct gdbarch *, int); | |
cfc14b3a MK |
549 | }; |
550 | ||
8f22cb90 MK |
551 | /* Default architecture-specific register state initialization |
552 | function. */ | |
553 | ||
554 | static void | |
555 | dwarf2_frame_default_init_reg (struct gdbarch *gdbarch, int regnum, | |
aff37fc1 DM |
556 | struct dwarf2_frame_state_reg *reg, |
557 | struct frame_info *next_frame) | |
8f22cb90 MK |
558 | { |
559 | /* If we have a register that acts as a program counter, mark it as | |
560 | a destination for the return address. If we have a register that | |
561 | serves as the stack pointer, arrange for it to be filled with the | |
562 | call frame address (CFA). The other registers are marked as | |
563 | unspecified. | |
564 | ||
565 | We copy the return address to the program counter, since many | |
566 | parts in GDB assume that it is possible to get the return address | |
567 | by unwinding the program counter register. However, on ISA's | |
568 | with a dedicated return address register, the CFI usually only | |
569 | contains information to unwind that return address register. | |
570 | ||
571 | The reason we're treating the stack pointer special here is | |
572 | because in many cases GCC doesn't emit CFI for the stack pointer | |
573 | and implicitly assumes that it is equal to the CFA. This makes | |
574 | some sense since the DWARF specification (version 3, draft 8, | |
575 | p. 102) says that: | |
576 | ||
577 | "Typically, the CFA is defined to be the value of the stack | |
578 | pointer at the call site in the previous frame (which may be | |
579 | different from its value on entry to the current frame)." | |
580 | ||
581 | However, this isn't true for all platforms supported by GCC | |
582 | (e.g. IBM S/390 and zSeries). Those architectures should provide | |
583 | their own architecture-specific initialization function. */ | |
05cbe71a | 584 | |
8f22cb90 MK |
585 | if (regnum == PC_REGNUM) |
586 | reg->how = DWARF2_FRAME_REG_RA; | |
587 | else if (regnum == SP_REGNUM) | |
588 | reg->how = DWARF2_FRAME_REG_CFA; | |
589 | } | |
05cbe71a | 590 | |
8f22cb90 | 591 | /* Return a default for the architecture-specific operations. */ |
05cbe71a | 592 | |
8f22cb90 | 593 | static void * |
030f20e1 | 594 | dwarf2_frame_init (struct obstack *obstack) |
8f22cb90 MK |
595 | { |
596 | struct dwarf2_frame_ops *ops; | |
597 | ||
030f20e1 | 598 | ops = OBSTACK_ZALLOC (obstack, struct dwarf2_frame_ops); |
8f22cb90 MK |
599 | ops->init_reg = dwarf2_frame_default_init_reg; |
600 | return ops; | |
601 | } | |
05cbe71a | 602 | |
8f22cb90 MK |
603 | /* Set the architecture-specific register state initialization |
604 | function for GDBARCH to INIT_REG. */ | |
605 | ||
606 | void | |
607 | dwarf2_frame_set_init_reg (struct gdbarch *gdbarch, | |
608 | void (*init_reg) (struct gdbarch *, int, | |
aff37fc1 DM |
609 | struct dwarf2_frame_state_reg *, |
610 | struct frame_info *)) | |
8f22cb90 | 611 | { |
030f20e1 | 612 | struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data); |
8f22cb90 | 613 | |
8f22cb90 MK |
614 | ops->init_reg = init_reg; |
615 | } | |
616 | ||
617 | /* Pre-initialize the register state REG for register REGNUM. */ | |
05cbe71a MK |
618 | |
619 | static void | |
620 | dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum, | |
aff37fc1 DM |
621 | struct dwarf2_frame_state_reg *reg, |
622 | struct frame_info *next_frame) | |
05cbe71a | 623 | { |
030f20e1 | 624 | struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data); |
8f22cb90 | 625 | |
aff37fc1 | 626 | ops->init_reg (gdbarch, regnum, reg, next_frame); |
05cbe71a | 627 | } |
3ed09a32 DJ |
628 | |
629 | /* Set the architecture-specific signal trampoline recognition | |
630 | function for GDBARCH to SIGNAL_FRAME_P. */ | |
631 | ||
632 | void | |
633 | dwarf2_frame_set_signal_frame_p (struct gdbarch *gdbarch, | |
634 | int (*signal_frame_p) (struct gdbarch *, | |
635 | struct frame_info *)) | |
636 | { | |
637 | struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data); | |
638 | ||
639 | ops->signal_frame_p = signal_frame_p; | |
640 | } | |
641 | ||
642 | /* Query the architecture-specific signal frame recognizer for | |
643 | NEXT_FRAME. */ | |
644 | ||
645 | static int | |
646 | dwarf2_frame_signal_frame_p (struct gdbarch *gdbarch, | |
647 | struct frame_info *next_frame) | |
648 | { | |
649 | struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data); | |
650 | ||
651 | if (ops->signal_frame_p == NULL) | |
652 | return 0; | |
653 | return ops->signal_frame_p (gdbarch, next_frame); | |
654 | } | |
4bf8967c AS |
655 | |
656 | /* Set the architecture-specific mapping of .eh_frame register numbers to | |
657 | DWARF register numbers. */ | |
658 | ||
659 | void | |
660 | dwarf2_frame_set_eh_frame_regnum (struct gdbarch *gdbarch, | |
661 | int (*eh_frame_regnum) (struct gdbarch *, | |
662 | int)) | |
663 | { | |
664 | struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data); | |
665 | ||
666 | ops->eh_frame_regnum = eh_frame_regnum; | |
667 | } | |
668 | ||
669 | /* Translate a .eh_frame register to DWARF register. */ | |
670 | ||
671 | int | |
672 | dwarf2_frame_eh_frame_regnum (struct gdbarch *gdbarch, int regnum) | |
673 | { | |
674 | struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data); | |
675 | ||
676 | if (ops->eh_frame_regnum == NULL) | |
677 | return regnum; | |
678 | return ops->eh_frame_regnum (gdbarch, regnum); | |
679 | } | |
8f22cb90 MK |
680 | \f |
681 | ||
682 | struct dwarf2_frame_cache | |
683 | { | |
684 | /* DWARF Call Frame Address. */ | |
685 | CORE_ADDR cfa; | |
686 | ||
0228dfb9 DJ |
687 | /* Set if the return address column was marked as undefined. */ |
688 | int undefined_retaddr; | |
689 | ||
8f22cb90 MK |
690 | /* Saved registers, indexed by GDB register number, not by DWARF |
691 | register number. */ | |
692 | struct dwarf2_frame_state_reg *reg; | |
8d5a9abc MK |
693 | |
694 | /* Return address register. */ | |
695 | struct dwarf2_frame_state_reg retaddr_reg; | |
8f22cb90 | 696 | }; |
05cbe71a | 697 | |
b9362cc7 | 698 | static struct dwarf2_frame_cache * |
cfc14b3a MK |
699 | dwarf2_frame_cache (struct frame_info *next_frame, void **this_cache) |
700 | { | |
701 | struct cleanup *old_chain; | |
05cbe71a | 702 | struct gdbarch *gdbarch = get_frame_arch (next_frame); |
3e2c4033 | 703 | const int num_regs = NUM_REGS + NUM_PSEUDO_REGS; |
cfc14b3a MK |
704 | struct dwarf2_frame_cache *cache; |
705 | struct dwarf2_frame_state *fs; | |
706 | struct dwarf2_fde *fde; | |
cfc14b3a MK |
707 | |
708 | if (*this_cache) | |
709 | return *this_cache; | |
710 | ||
711 | /* Allocate a new cache. */ | |
712 | cache = FRAME_OBSTACK_ZALLOC (struct dwarf2_frame_cache); | |
713 | cache->reg = FRAME_OBSTACK_CALLOC (num_regs, struct dwarf2_frame_state_reg); | |
714 | ||
715 | /* Allocate and initialize the frame state. */ | |
716 | fs = XMALLOC (struct dwarf2_frame_state); | |
717 | memset (fs, 0, sizeof (struct dwarf2_frame_state)); | |
718 | old_chain = make_cleanup (dwarf2_frame_state_free, fs); | |
719 | ||
720 | /* Unwind the PC. | |
721 | ||
722 | Note that if NEXT_FRAME is never supposed to return (i.e. a call | |
723 | to abort), the compiler might optimize away the instruction at | |
724 | NEXT_FRAME's return address. As a result the return address will | |
725 | point at some random instruction, and the CFI for that | |
e4e9607c | 726 | instruction is probably worthless to us. GCC's unwinder solves |
cfc14b3a MK |
727 | this problem by substracting 1 from the return address to get an |
728 | address in the middle of a presumed call instruction (or the | |
729 | instruction in the associated delay slot). This should only be | |
730 | done for "normal" frames and not for resume-type frames (signal | |
e4e9607c MK |
731 | handlers, sentinel frames, dummy frames). The function |
732 | frame_unwind_address_in_block does just this. It's not clear how | |
733 | reliable the method is though; there is the potential for the | |
734 | register state pre-call being different to that on return. */ | |
1ce5d6dd | 735 | fs->pc = frame_unwind_address_in_block (next_frame); |
cfc14b3a MK |
736 | |
737 | /* Find the correct FDE. */ | |
738 | fde = dwarf2_frame_find_fde (&fs->pc); | |
739 | gdb_assert (fde != NULL); | |
740 | ||
741 | /* Extract any interesting information from the CIE. */ | |
742 | fs->data_align = fde->cie->data_alignment_factor; | |
743 | fs->code_align = fde->cie->code_alignment_factor; | |
744 | fs->retaddr_column = fde->cie->return_address_register; | |
745 | ||
746 | /* First decode all the insns in the CIE. */ | |
747 | execute_cfa_program (fde->cie->initial_instructions, | |
4bf8967c | 748 | fde->cie->end, next_frame, fs, fde->eh_frame_p); |
cfc14b3a MK |
749 | |
750 | /* Save the initialized register set. */ | |
751 | fs->initial = fs->regs; | |
752 | fs->initial.reg = dwarf2_frame_state_copy_regs (&fs->regs); | |
753 | ||
754 | /* Then decode the insns in the FDE up to our target PC. */ | |
4bf8967c AS |
755 | execute_cfa_program (fde->instructions, fde->end, next_frame, fs, |
756 | fde->eh_frame_p); | |
cfc14b3a MK |
757 | |
758 | /* Caclulate the CFA. */ | |
759 | switch (fs->cfa_how) | |
760 | { | |
761 | case CFA_REG_OFFSET: | |
762 | cache->cfa = read_reg (next_frame, fs->cfa_reg); | |
763 | cache->cfa += fs->cfa_offset; | |
764 | break; | |
765 | ||
766 | case CFA_EXP: | |
767 | cache->cfa = | |
768 | execute_stack_op (fs->cfa_exp, fs->cfa_exp_len, next_frame, 0); | |
769 | break; | |
770 | ||
771 | default: | |
e2e0b3e5 | 772 | internal_error (__FILE__, __LINE__, _("Unknown CFA rule.")); |
cfc14b3a MK |
773 | } |
774 | ||
05cbe71a | 775 | /* Initialize the register state. */ |
3e2c4033 AC |
776 | { |
777 | int regnum; | |
e4e9607c | 778 | |
3e2c4033 | 779 | for (regnum = 0; regnum < num_regs; regnum++) |
aff37fc1 | 780 | dwarf2_frame_init_reg (gdbarch, regnum, &cache->reg[regnum], next_frame); |
3e2c4033 AC |
781 | } |
782 | ||
783 | /* Go through the DWARF2 CFI generated table and save its register | |
79c4cb80 MK |
784 | location information in the cache. Note that we don't skip the |
785 | return address column; it's perfectly all right for it to | |
786 | correspond to a real register. If it doesn't correspond to a | |
787 | real register, or if we shouldn't treat it as such, | |
788 | DWARF2_REG_TO_REGNUM should be defined to return a number outside | |
789 | the range [0, NUM_REGS). */ | |
3e2c4033 AC |
790 | { |
791 | int column; /* CFI speak for "register number". */ | |
e4e9607c | 792 | |
3e2c4033 AC |
793 | for (column = 0; column < fs->regs.num_regs; column++) |
794 | { | |
3e2c4033 | 795 | /* Use the GDB register number as the destination index. */ |
79c4cb80 | 796 | int regnum = DWARF2_REG_TO_REGNUM (column); |
3e2c4033 AC |
797 | |
798 | /* If there's no corresponding GDB register, ignore it. */ | |
799 | if (regnum < 0 || regnum >= num_regs) | |
800 | continue; | |
801 | ||
802 | /* NOTE: cagney/2003-09-05: CFI should specify the disposition | |
e4e9607c MK |
803 | of all debug info registers. If it doesn't, complain (but |
804 | not too loudly). It turns out that GCC assumes that an | |
3e2c4033 AC |
805 | unspecified register implies "same value" when CFI (draft |
806 | 7) specifies nothing at all. Such a register could equally | |
807 | be interpreted as "undefined". Also note that this check | |
e4e9607c MK |
808 | isn't sufficient; it only checks that all registers in the |
809 | range [0 .. max column] are specified, and won't detect | |
3e2c4033 | 810 | problems when a debug info register falls outside of the |
e4e9607c | 811 | table. We need a way of iterating through all the valid |
3e2c4033 | 812 | DWARF2 register numbers. */ |
05cbe71a | 813 | if (fs->regs.reg[column].how == DWARF2_FRAME_REG_UNSPECIFIED) |
f059bf6f AC |
814 | { |
815 | if (cache->reg[regnum].how == DWARF2_FRAME_REG_UNSPECIFIED) | |
e2e0b3e5 AC |
816 | complaint (&symfile_complaints, _("\ |
817 | incomplete CFI data; unspecified registers (e.g., %s) at 0x%s"), | |
f059bf6f AC |
818 | gdbarch_register_name (gdbarch, regnum), |
819 | paddr_nz (fs->pc)); | |
820 | } | |
35889917 MK |
821 | else |
822 | cache->reg[regnum] = fs->regs.reg[column]; | |
3e2c4033 AC |
823 | } |
824 | } | |
cfc14b3a | 825 | |
8d5a9abc MK |
826 | /* Eliminate any DWARF2_FRAME_REG_RA rules, and save the information |
827 | we need for evaluating DWARF2_FRAME_REG_RA_OFFSET rules. */ | |
35889917 MK |
828 | { |
829 | int regnum; | |
830 | ||
831 | for (regnum = 0; regnum < num_regs; regnum++) | |
832 | { | |
8d5a9abc MK |
833 | if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA |
834 | || cache->reg[regnum].how == DWARF2_FRAME_REG_RA_OFFSET) | |
35889917 | 835 | { |
05cbe71a MK |
836 | struct dwarf2_frame_state_reg *retaddr_reg = |
837 | &fs->regs.reg[fs->retaddr_column]; | |
838 | ||
d4f10bf2 MK |
839 | /* It seems rather bizarre to specify an "empty" column as |
840 | the return adress column. However, this is exactly | |
841 | what GCC does on some targets. It turns out that GCC | |
842 | assumes that the return address can be found in the | |
843 | register corresponding to the return address column. | |
8d5a9abc MK |
844 | Incidentally, that's how we should treat a return |
845 | address column specifying "same value" too. */ | |
d4f10bf2 | 846 | if (fs->retaddr_column < fs->regs.num_regs |
05cbe71a MK |
847 | && retaddr_reg->how != DWARF2_FRAME_REG_UNSPECIFIED |
848 | && retaddr_reg->how != DWARF2_FRAME_REG_SAME_VALUE) | |
8d5a9abc MK |
849 | { |
850 | if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA) | |
851 | cache->reg[regnum] = *retaddr_reg; | |
852 | else | |
853 | cache->retaddr_reg = *retaddr_reg; | |
854 | } | |
35889917 MK |
855 | else |
856 | { | |
8d5a9abc MK |
857 | if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA) |
858 | { | |
859 | cache->reg[regnum].loc.reg = fs->retaddr_column; | |
860 | cache->reg[regnum].how = DWARF2_FRAME_REG_SAVED_REG; | |
861 | } | |
862 | else | |
863 | { | |
864 | cache->retaddr_reg.loc.reg = fs->retaddr_column; | |
865 | cache->retaddr_reg.how = DWARF2_FRAME_REG_SAVED_REG; | |
866 | } | |
35889917 MK |
867 | } |
868 | } | |
869 | } | |
870 | } | |
cfc14b3a | 871 | |
0228dfb9 DJ |
872 | if (fs->retaddr_column < fs->regs.num_regs |
873 | && fs->regs.reg[fs->retaddr_column].how == DWARF2_FRAME_REG_UNDEFINED) | |
874 | cache->undefined_retaddr = 1; | |
875 | ||
cfc14b3a MK |
876 | do_cleanups (old_chain); |
877 | ||
878 | *this_cache = cache; | |
879 | return cache; | |
880 | } | |
881 | ||
882 | static void | |
883 | dwarf2_frame_this_id (struct frame_info *next_frame, void **this_cache, | |
884 | struct frame_id *this_id) | |
885 | { | |
886 | struct dwarf2_frame_cache *cache = | |
887 | dwarf2_frame_cache (next_frame, this_cache); | |
888 | ||
0228dfb9 DJ |
889 | if (cache->undefined_retaddr) |
890 | return; | |
891 | ||
cfc14b3a MK |
892 | (*this_id) = frame_id_build (cache->cfa, frame_func_unwind (next_frame)); |
893 | } | |
894 | ||
895 | static void | |
896 | dwarf2_frame_prev_register (struct frame_info *next_frame, void **this_cache, | |
897 | int regnum, int *optimizedp, | |
898 | enum lval_type *lvalp, CORE_ADDR *addrp, | |
c6826062 | 899 | int *realnump, gdb_byte *valuep) |
cfc14b3a | 900 | { |
05cbe71a | 901 | struct gdbarch *gdbarch = get_frame_arch (next_frame); |
cfc14b3a MK |
902 | struct dwarf2_frame_cache *cache = |
903 | dwarf2_frame_cache (next_frame, this_cache); | |
904 | ||
905 | switch (cache->reg[regnum].how) | |
906 | { | |
05cbe71a | 907 | case DWARF2_FRAME_REG_UNDEFINED: |
3e2c4033 | 908 | /* If CFI explicitly specified that the value isn't defined, |
e4e9607c | 909 | mark it as optimized away; the value isn't available. */ |
cfc14b3a MK |
910 | *optimizedp = 1; |
911 | *lvalp = not_lval; | |
912 | *addrp = 0; | |
913 | *realnump = -1; | |
35889917 | 914 | if (valuep) |
cfc14b3a MK |
915 | { |
916 | /* In some cases, for example %eflags on the i386, we have | |
917 | to provide a sane value, even though this register wasn't | |
918 | saved. Assume we can get it from NEXT_FRAME. */ | |
919 | frame_unwind_register (next_frame, regnum, valuep); | |
920 | } | |
921 | break; | |
922 | ||
05cbe71a | 923 | case DWARF2_FRAME_REG_SAVED_OFFSET: |
cfc14b3a MK |
924 | *optimizedp = 0; |
925 | *lvalp = lval_memory; | |
926 | *addrp = cache->cfa + cache->reg[regnum].loc.offset; | |
927 | *realnump = -1; | |
928 | if (valuep) | |
929 | { | |
930 | /* Read the value in from memory. */ | |
05cbe71a | 931 | read_memory (*addrp, valuep, register_size (gdbarch, regnum)); |
cfc14b3a MK |
932 | } |
933 | break; | |
934 | ||
05cbe71a | 935 | case DWARF2_FRAME_REG_SAVED_REG: |
00b25ff3 AC |
936 | *optimizedp = 0; |
937 | *lvalp = lval_register; | |
938 | *addrp = 0; | |
939 | *realnump = DWARF2_REG_TO_REGNUM (cache->reg[regnum].loc.reg); | |
940 | if (valuep) | |
941 | frame_unwind_register (next_frame, (*realnump), valuep); | |
cfc14b3a MK |
942 | break; |
943 | ||
05cbe71a | 944 | case DWARF2_FRAME_REG_SAVED_EXP: |
cfc14b3a MK |
945 | *optimizedp = 0; |
946 | *lvalp = lval_memory; | |
947 | *addrp = execute_stack_op (cache->reg[regnum].loc.exp, | |
948 | cache->reg[regnum].exp_len, | |
949 | next_frame, cache->cfa); | |
950 | *realnump = -1; | |
951 | if (valuep) | |
952 | { | |
953 | /* Read the value in from memory. */ | |
05cbe71a | 954 | read_memory (*addrp, valuep, register_size (gdbarch, regnum)); |
cfc14b3a MK |
955 | } |
956 | break; | |
957 | ||
05cbe71a | 958 | case DWARF2_FRAME_REG_UNSPECIFIED: |
3e2c4033 AC |
959 | /* GCC, in its infinite wisdom decided to not provide unwind |
960 | information for registers that are "same value". Since | |
961 | DWARF2 (3 draft 7) doesn't define such behavior, said | |
962 | registers are actually undefined (which is different to CFI | |
963 | "undefined"). Code above issues a complaint about this. | |
964 | Here just fudge the books, assume GCC, and that the value is | |
965 | more inner on the stack. */ | |
00b25ff3 AC |
966 | *optimizedp = 0; |
967 | *lvalp = lval_register; | |
968 | *addrp = 0; | |
969 | *realnump = regnum; | |
970 | if (valuep) | |
971 | frame_unwind_register (next_frame, (*realnump), valuep); | |
3e2c4033 AC |
972 | break; |
973 | ||
05cbe71a | 974 | case DWARF2_FRAME_REG_SAME_VALUE: |
00b25ff3 AC |
975 | *optimizedp = 0; |
976 | *lvalp = lval_register; | |
977 | *addrp = 0; | |
978 | *realnump = regnum; | |
979 | if (valuep) | |
980 | frame_unwind_register (next_frame, (*realnump), valuep); | |
cfc14b3a MK |
981 | break; |
982 | ||
05cbe71a | 983 | case DWARF2_FRAME_REG_CFA: |
35889917 MK |
984 | *optimizedp = 0; |
985 | *lvalp = not_lval; | |
986 | *addrp = 0; | |
987 | *realnump = -1; | |
988 | if (valuep) | |
989 | { | |
990 | /* Store the value. */ | |
991 | store_typed_address (valuep, builtin_type_void_data_ptr, cache->cfa); | |
992 | } | |
993 | break; | |
994 | ||
ea7963f0 FR |
995 | case DWARF2_FRAME_REG_CFA_OFFSET: |
996 | *optimizedp = 0; | |
997 | *lvalp = not_lval; | |
998 | *addrp = 0; | |
999 | *realnump = -1; | |
1000 | if (valuep) | |
1001 | { | |
1002 | /* Store the value. */ | |
1003 | store_typed_address (valuep, builtin_type_void_data_ptr, | |
1004 | cache->cfa + cache->reg[regnum].loc.offset); | |
1005 | } | |
1006 | break; | |
1007 | ||
8d5a9abc MK |
1008 | case DWARF2_FRAME_REG_RA_OFFSET: |
1009 | *optimizedp = 0; | |
1010 | *lvalp = not_lval; | |
1011 | *addrp = 0; | |
1012 | *realnump = -1; | |
1013 | if (valuep) | |
1014 | { | |
1015 | CORE_ADDR pc = cache->reg[regnum].loc.offset; | |
1016 | ||
1017 | regnum = DWARF2_REG_TO_REGNUM (cache->retaddr_reg.loc.reg); | |
1018 | pc += frame_unwind_register_unsigned (next_frame, regnum); | |
1019 | store_typed_address (valuep, builtin_type_void_func_ptr, pc); | |
1020 | } | |
1021 | break; | |
1022 | ||
cfc14b3a | 1023 | default: |
e2e0b3e5 | 1024 | internal_error (__FILE__, __LINE__, _("Unknown register rule.")); |
cfc14b3a MK |
1025 | } |
1026 | } | |
1027 | ||
1028 | static const struct frame_unwind dwarf2_frame_unwind = | |
1029 | { | |
1030 | NORMAL_FRAME, | |
1031 | dwarf2_frame_this_id, | |
1032 | dwarf2_frame_prev_register | |
1033 | }; | |
1034 | ||
3ed09a32 DJ |
1035 | static const struct frame_unwind dwarf2_signal_frame_unwind = |
1036 | { | |
1037 | SIGTRAMP_FRAME, | |
1038 | dwarf2_frame_this_id, | |
1039 | dwarf2_frame_prev_register | |
1040 | }; | |
1041 | ||
cfc14b3a | 1042 | const struct frame_unwind * |
336d1bba | 1043 | dwarf2_frame_sniffer (struct frame_info *next_frame) |
cfc14b3a | 1044 | { |
1ce5d6dd AC |
1045 | /* Grab an address that is guarenteed to reside somewhere within the |
1046 | function. frame_pc_unwind(), for a no-return next function, can | |
1047 | end up returning something past the end of this function's body. */ | |
1048 | CORE_ADDR block_addr = frame_unwind_address_in_block (next_frame); | |
3ed09a32 DJ |
1049 | if (!dwarf2_frame_find_fde (&block_addr)) |
1050 | return NULL; | |
1051 | ||
1052 | /* On some targets, signal trampolines may have unwind information. | |
1053 | We need to recognize them so that we set the frame type | |
1054 | correctly. */ | |
1055 | ||
1056 | if (dwarf2_frame_signal_frame_p (get_frame_arch (next_frame), | |
1057 | next_frame)) | |
1058 | return &dwarf2_signal_frame_unwind; | |
cfc14b3a | 1059 | |
3ed09a32 | 1060 | return &dwarf2_frame_unwind; |
cfc14b3a MK |
1061 | } |
1062 | \f | |
1063 | ||
1064 | /* There is no explicitly defined relationship between the CFA and the | |
1065 | location of frame's local variables and arguments/parameters. | |
1066 | Therefore, frame base methods on this page should probably only be | |
1067 | used as a last resort, just to avoid printing total garbage as a | |
1068 | response to the "info frame" command. */ | |
1069 | ||
1070 | static CORE_ADDR | |
1071 | dwarf2_frame_base_address (struct frame_info *next_frame, void **this_cache) | |
1072 | { | |
1073 | struct dwarf2_frame_cache *cache = | |
1074 | dwarf2_frame_cache (next_frame, this_cache); | |
1075 | ||
1076 | return cache->cfa; | |
1077 | } | |
1078 | ||
1079 | static const struct frame_base dwarf2_frame_base = | |
1080 | { | |
1081 | &dwarf2_frame_unwind, | |
1082 | dwarf2_frame_base_address, | |
1083 | dwarf2_frame_base_address, | |
1084 | dwarf2_frame_base_address | |
1085 | }; | |
1086 | ||
1087 | const struct frame_base * | |
336d1bba | 1088 | dwarf2_frame_base_sniffer (struct frame_info *next_frame) |
cfc14b3a | 1089 | { |
336d1bba | 1090 | CORE_ADDR pc = frame_pc_unwind (next_frame); |
cfc14b3a MK |
1091 | if (dwarf2_frame_find_fde (&pc)) |
1092 | return &dwarf2_frame_base; | |
1093 | ||
1094 | return NULL; | |
1095 | } | |
1096 | \f | |
1097 | /* A minimal decoding of DWARF2 compilation units. We only decode | |
1098 | what's needed to get to the call frame information. */ | |
1099 | ||
1100 | struct comp_unit | |
1101 | { | |
1102 | /* Keep the bfd convenient. */ | |
1103 | bfd *abfd; | |
1104 | ||
1105 | struct objfile *objfile; | |
1106 | ||
1107 | /* Linked list of CIEs for this object. */ | |
1108 | struct dwarf2_cie *cie; | |
1109 | ||
cfc14b3a | 1110 | /* Pointer to the .debug_frame section loaded into memory. */ |
852483bc | 1111 | gdb_byte *dwarf_frame_buffer; |
cfc14b3a MK |
1112 | |
1113 | /* Length of the loaded .debug_frame section. */ | |
1114 | unsigned long dwarf_frame_size; | |
1115 | ||
1116 | /* Pointer to the .debug_frame section. */ | |
1117 | asection *dwarf_frame_section; | |
0912c7f2 MK |
1118 | |
1119 | /* Base for DW_EH_PE_datarel encodings. */ | |
1120 | bfd_vma dbase; | |
0fd85043 CV |
1121 | |
1122 | /* Base for DW_EH_PE_textrel encodings. */ | |
1123 | bfd_vma tbase; | |
cfc14b3a MK |
1124 | }; |
1125 | ||
8f22cb90 | 1126 | const struct objfile_data *dwarf2_frame_objfile_data; |
0d0e1a63 | 1127 | |
cfc14b3a | 1128 | static unsigned int |
852483bc | 1129 | read_1_byte (bfd *abfd, gdb_byte *buf) |
cfc14b3a | 1130 | { |
852483bc | 1131 | return bfd_get_8 (abfd, buf); |
cfc14b3a MK |
1132 | } |
1133 | ||
1134 | static unsigned int | |
852483bc | 1135 | read_4_bytes (bfd *abfd, gdb_byte *buf) |
cfc14b3a | 1136 | { |
852483bc | 1137 | return bfd_get_32 (abfd, buf); |
cfc14b3a MK |
1138 | } |
1139 | ||
1140 | static ULONGEST | |
852483bc | 1141 | read_8_bytes (bfd *abfd, gdb_byte *buf) |
cfc14b3a | 1142 | { |
852483bc | 1143 | return bfd_get_64 (abfd, buf); |
cfc14b3a MK |
1144 | } |
1145 | ||
1146 | static ULONGEST | |
852483bc | 1147 | read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr) |
cfc14b3a MK |
1148 | { |
1149 | ULONGEST result; | |
1150 | unsigned int num_read; | |
1151 | int shift; | |
852483bc | 1152 | gdb_byte byte; |
cfc14b3a MK |
1153 | |
1154 | result = 0; | |
1155 | shift = 0; | |
1156 | num_read = 0; | |
1157 | ||
1158 | do | |
1159 | { | |
1160 | byte = bfd_get_8 (abfd, (bfd_byte *) buf); | |
1161 | buf++; | |
1162 | num_read++; | |
1163 | result |= ((byte & 0x7f) << shift); | |
1164 | shift += 7; | |
1165 | } | |
1166 | while (byte & 0x80); | |
1167 | ||
1168 | *bytes_read_ptr = num_read; | |
1169 | ||
1170 | return result; | |
1171 | } | |
1172 | ||
1173 | static LONGEST | |
852483bc | 1174 | read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr) |
cfc14b3a MK |
1175 | { |
1176 | LONGEST result; | |
1177 | int shift; | |
1178 | unsigned int num_read; | |
852483bc | 1179 | gdb_byte byte; |
cfc14b3a MK |
1180 | |
1181 | result = 0; | |
1182 | shift = 0; | |
1183 | num_read = 0; | |
1184 | ||
1185 | do | |
1186 | { | |
1187 | byte = bfd_get_8 (abfd, (bfd_byte *) buf); | |
1188 | buf++; | |
1189 | num_read++; | |
1190 | result |= ((byte & 0x7f) << shift); | |
1191 | shift += 7; | |
1192 | } | |
1193 | while (byte & 0x80); | |
1194 | ||
77e0b926 DJ |
1195 | if (shift < 8 * sizeof (result) && (byte & 0x40)) |
1196 | result |= -(((LONGEST)1) << shift); | |
cfc14b3a MK |
1197 | |
1198 | *bytes_read_ptr = num_read; | |
1199 | ||
1200 | return result; | |
1201 | } | |
1202 | ||
1203 | static ULONGEST | |
852483bc | 1204 | read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr) |
cfc14b3a MK |
1205 | { |
1206 | LONGEST result; | |
1207 | ||
852483bc | 1208 | result = bfd_get_32 (abfd, buf); |
cfc14b3a MK |
1209 | if (result == 0xffffffff) |
1210 | { | |
852483bc | 1211 | result = bfd_get_64 (abfd, buf + 4); |
cfc14b3a MK |
1212 | *bytes_read_ptr = 12; |
1213 | } | |
1214 | else | |
1215 | *bytes_read_ptr = 4; | |
1216 | ||
1217 | return result; | |
1218 | } | |
1219 | \f | |
1220 | ||
1221 | /* Pointer encoding helper functions. */ | |
1222 | ||
1223 | /* GCC supports exception handling based on DWARF2 CFI. However, for | |
1224 | technical reasons, it encodes addresses in its FDE's in a different | |
1225 | way. Several "pointer encodings" are supported. The encoding | |
1226 | that's used for a particular FDE is determined by the 'R' | |
1227 | augmentation in the associated CIE. The argument of this | |
1228 | augmentation is a single byte. | |
1229 | ||
1230 | The address can be encoded as 2 bytes, 4 bytes, 8 bytes, or as a | |
1231 | LEB128. This is encoded in bits 0, 1 and 2. Bit 3 encodes whether | |
1232 | the address is signed or unsigned. Bits 4, 5 and 6 encode how the | |
1233 | address should be interpreted (absolute, relative to the current | |
1234 | position in the FDE, ...). Bit 7, indicates that the address | |
1235 | should be dereferenced. */ | |
1236 | ||
852483bc | 1237 | static gdb_byte |
cfc14b3a MK |
1238 | encoding_for_size (unsigned int size) |
1239 | { | |
1240 | switch (size) | |
1241 | { | |
1242 | case 2: | |
1243 | return DW_EH_PE_udata2; | |
1244 | case 4: | |
1245 | return DW_EH_PE_udata4; | |
1246 | case 8: | |
1247 | return DW_EH_PE_udata8; | |
1248 | default: | |
e2e0b3e5 | 1249 | internal_error (__FILE__, __LINE__, _("Unsupported address size")); |
cfc14b3a MK |
1250 | } |
1251 | } | |
1252 | ||
1253 | static unsigned int | |
852483bc | 1254 | size_of_encoded_value (gdb_byte encoding) |
cfc14b3a MK |
1255 | { |
1256 | if (encoding == DW_EH_PE_omit) | |
1257 | return 0; | |
1258 | ||
1259 | switch (encoding & 0x07) | |
1260 | { | |
1261 | case DW_EH_PE_absptr: | |
1262 | return TYPE_LENGTH (builtin_type_void_data_ptr); | |
1263 | case DW_EH_PE_udata2: | |
1264 | return 2; | |
1265 | case DW_EH_PE_udata4: | |
1266 | return 4; | |
1267 | case DW_EH_PE_udata8: | |
1268 | return 8; | |
1269 | default: | |
e2e0b3e5 | 1270 | internal_error (__FILE__, __LINE__, _("Invalid or unsupported encoding")); |
cfc14b3a MK |
1271 | } |
1272 | } | |
1273 | ||
1274 | static CORE_ADDR | |
852483bc MK |
1275 | read_encoded_value (struct comp_unit *unit, gdb_byte encoding, |
1276 | gdb_byte *buf, unsigned int *bytes_read_ptr) | |
cfc14b3a | 1277 | { |
68f6cf99 MK |
1278 | int ptr_len = size_of_encoded_value (DW_EH_PE_absptr); |
1279 | ptrdiff_t offset; | |
cfc14b3a MK |
1280 | CORE_ADDR base; |
1281 | ||
1282 | /* GCC currently doesn't generate DW_EH_PE_indirect encodings for | |
1283 | FDE's. */ | |
1284 | if (encoding & DW_EH_PE_indirect) | |
1285 | internal_error (__FILE__, __LINE__, | |
e2e0b3e5 | 1286 | _("Unsupported encoding: DW_EH_PE_indirect")); |
cfc14b3a | 1287 | |
68f6cf99 MK |
1288 | *bytes_read_ptr = 0; |
1289 | ||
cfc14b3a MK |
1290 | switch (encoding & 0x70) |
1291 | { | |
1292 | case DW_EH_PE_absptr: | |
1293 | base = 0; | |
1294 | break; | |
1295 | case DW_EH_PE_pcrel: | |
1296 | base = bfd_get_section_vma (unit->bfd, unit->dwarf_frame_section); | |
852483bc | 1297 | base += (buf - unit->dwarf_frame_buffer); |
cfc14b3a | 1298 | break; |
0912c7f2 MK |
1299 | case DW_EH_PE_datarel: |
1300 | base = unit->dbase; | |
1301 | break; | |
0fd85043 CV |
1302 | case DW_EH_PE_textrel: |
1303 | base = unit->tbase; | |
1304 | break; | |
03ac2a74 MK |
1305 | case DW_EH_PE_funcrel: |
1306 | /* FIXME: kettenis/20040501: For now just pretend | |
1307 | DW_EH_PE_funcrel is equivalent to DW_EH_PE_absptr. For | |
1308 | reading the initial location of an FDE it should be treated | |
1309 | as such, and currently that's the only place where this code | |
1310 | is used. */ | |
1311 | base = 0; | |
1312 | break; | |
68f6cf99 MK |
1313 | case DW_EH_PE_aligned: |
1314 | base = 0; | |
852483bc | 1315 | offset = buf - unit->dwarf_frame_buffer; |
68f6cf99 MK |
1316 | if ((offset % ptr_len) != 0) |
1317 | { | |
1318 | *bytes_read_ptr = ptr_len - (offset % ptr_len); | |
1319 | buf += *bytes_read_ptr; | |
1320 | } | |
1321 | break; | |
cfc14b3a | 1322 | default: |
e2e0b3e5 | 1323 | internal_error (__FILE__, __LINE__, _("Invalid or unsupported encoding")); |
cfc14b3a MK |
1324 | } |
1325 | ||
b04de778 | 1326 | if ((encoding & 0x07) == 0x00) |
68f6cf99 | 1327 | encoding |= encoding_for_size (ptr_len); |
cfc14b3a MK |
1328 | |
1329 | switch (encoding & 0x0f) | |
1330 | { | |
a81b10ae MK |
1331 | case DW_EH_PE_uleb128: |
1332 | { | |
1333 | ULONGEST value; | |
852483bc | 1334 | gdb_byte *end_buf = buf + (sizeof (value) + 1) * 8 / 7; |
a7289609 | 1335 | *bytes_read_ptr += read_uleb128 (buf, end_buf, &value) - buf; |
a81b10ae MK |
1336 | return base + value; |
1337 | } | |
cfc14b3a | 1338 | case DW_EH_PE_udata2: |
68f6cf99 | 1339 | *bytes_read_ptr += 2; |
cfc14b3a MK |
1340 | return (base + bfd_get_16 (unit->abfd, (bfd_byte *) buf)); |
1341 | case DW_EH_PE_udata4: | |
68f6cf99 | 1342 | *bytes_read_ptr += 4; |
cfc14b3a MK |
1343 | return (base + bfd_get_32 (unit->abfd, (bfd_byte *) buf)); |
1344 | case DW_EH_PE_udata8: | |
68f6cf99 | 1345 | *bytes_read_ptr += 8; |
cfc14b3a | 1346 | return (base + bfd_get_64 (unit->abfd, (bfd_byte *) buf)); |
a81b10ae MK |
1347 | case DW_EH_PE_sleb128: |
1348 | { | |
1349 | LONGEST value; | |
852483bc | 1350 | gdb_byte *end_buf = buf + (sizeof (value) + 1) * 8 / 7; |
a7289609 | 1351 | *bytes_read_ptr += read_sleb128 (buf, end_buf, &value) - buf; |
a81b10ae MK |
1352 | return base + value; |
1353 | } | |
cfc14b3a | 1354 | case DW_EH_PE_sdata2: |
68f6cf99 | 1355 | *bytes_read_ptr += 2; |
cfc14b3a MK |
1356 | return (base + bfd_get_signed_16 (unit->abfd, (bfd_byte *) buf)); |
1357 | case DW_EH_PE_sdata4: | |
68f6cf99 | 1358 | *bytes_read_ptr += 4; |
cfc14b3a MK |
1359 | return (base + bfd_get_signed_32 (unit->abfd, (bfd_byte *) buf)); |
1360 | case DW_EH_PE_sdata8: | |
68f6cf99 | 1361 | *bytes_read_ptr += 8; |
cfc14b3a MK |
1362 | return (base + bfd_get_signed_64 (unit->abfd, (bfd_byte *) buf)); |
1363 | default: | |
e2e0b3e5 | 1364 | internal_error (__FILE__, __LINE__, _("Invalid or unsupported encoding")); |
cfc14b3a MK |
1365 | } |
1366 | } | |
1367 | \f | |
1368 | ||
1369 | /* GCC uses a single CIE for all FDEs in a .debug_frame section. | |
1370 | That's why we use a simple linked list here. */ | |
1371 | ||
1372 | static struct dwarf2_cie * | |
1373 | find_cie (struct comp_unit *unit, ULONGEST cie_pointer) | |
1374 | { | |
1375 | struct dwarf2_cie *cie = unit->cie; | |
1376 | ||
1377 | while (cie) | |
1378 | { | |
1379 | if (cie->cie_pointer == cie_pointer) | |
1380 | return cie; | |
1381 | ||
1382 | cie = cie->next; | |
1383 | } | |
1384 | ||
1385 | return NULL; | |
1386 | } | |
1387 | ||
1388 | static void | |
1389 | add_cie (struct comp_unit *unit, struct dwarf2_cie *cie) | |
1390 | { | |
1391 | cie->next = unit->cie; | |
1392 | unit->cie = cie; | |
1393 | } | |
1394 | ||
1395 | /* Find the FDE for *PC. Return a pointer to the FDE, and store the | |
1396 | inital location associated with it into *PC. */ | |
1397 | ||
1398 | static struct dwarf2_fde * | |
1399 | dwarf2_frame_find_fde (CORE_ADDR *pc) | |
1400 | { | |
1401 | struct objfile *objfile; | |
1402 | ||
1403 | ALL_OBJFILES (objfile) | |
1404 | { | |
1405 | struct dwarf2_fde *fde; | |
1406 | CORE_ADDR offset; | |
1407 | ||
8f22cb90 | 1408 | fde = objfile_data (objfile, dwarf2_frame_objfile_data); |
4ae9ee8e DJ |
1409 | if (fde == NULL) |
1410 | continue; | |
1411 | ||
1412 | gdb_assert (objfile->section_offsets); | |
1413 | offset = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile)); | |
1414 | ||
cfc14b3a MK |
1415 | while (fde) |
1416 | { | |
1417 | if (*pc >= fde->initial_location + offset | |
1418 | && *pc < fde->initial_location + offset + fde->address_range) | |
1419 | { | |
1420 | *pc = fde->initial_location + offset; | |
1421 | return fde; | |
1422 | } | |
1423 | ||
1424 | fde = fde->next; | |
1425 | } | |
1426 | } | |
1427 | ||
1428 | return NULL; | |
1429 | } | |
1430 | ||
1431 | static void | |
1432 | add_fde (struct comp_unit *unit, struct dwarf2_fde *fde) | |
1433 | { | |
8f22cb90 MK |
1434 | fde->next = objfile_data (unit->objfile, dwarf2_frame_objfile_data); |
1435 | set_objfile_data (unit->objfile, dwarf2_frame_objfile_data, fde); | |
cfc14b3a MK |
1436 | } |
1437 | ||
1438 | #ifdef CC_HAS_LONG_LONG | |
1439 | #define DW64_CIE_ID 0xffffffffffffffffULL | |
1440 | #else | |
1441 | #define DW64_CIE_ID ~0 | |
1442 | #endif | |
1443 | ||
852483bc MK |
1444 | static gdb_byte *decode_frame_entry (struct comp_unit *unit, gdb_byte *start, |
1445 | int eh_frame_p); | |
cfc14b3a | 1446 | |
6896c0c7 RH |
1447 | /* Decode the next CIE or FDE. Return NULL if invalid input, otherwise |
1448 | the next byte to be processed. */ | |
852483bc MK |
1449 | static gdb_byte * |
1450 | decode_frame_entry_1 (struct comp_unit *unit, gdb_byte *start, int eh_frame_p) | |
cfc14b3a | 1451 | { |
852483bc | 1452 | gdb_byte *buf, *end; |
cfc14b3a MK |
1453 | LONGEST length; |
1454 | unsigned int bytes_read; | |
6896c0c7 RH |
1455 | int dwarf64_p; |
1456 | ULONGEST cie_id; | |
cfc14b3a | 1457 | ULONGEST cie_pointer; |
cfc14b3a | 1458 | |
6896c0c7 | 1459 | buf = start; |
cfc14b3a MK |
1460 | length = read_initial_length (unit->abfd, buf, &bytes_read); |
1461 | buf += bytes_read; | |
1462 | end = buf + length; | |
1463 | ||
6896c0c7 RH |
1464 | /* Are we still within the section? */ |
1465 | if (end > unit->dwarf_frame_buffer + unit->dwarf_frame_size) | |
1466 | return NULL; | |
1467 | ||
cfc14b3a MK |
1468 | if (length == 0) |
1469 | return end; | |
1470 | ||
6896c0c7 RH |
1471 | /* Distinguish between 32 and 64-bit encoded frame info. */ |
1472 | dwarf64_p = (bytes_read == 12); | |
cfc14b3a | 1473 | |
6896c0c7 | 1474 | /* In a .eh_frame section, zero is used to distinguish CIEs from FDEs. */ |
cfc14b3a MK |
1475 | if (eh_frame_p) |
1476 | cie_id = 0; | |
1477 | else if (dwarf64_p) | |
1478 | cie_id = DW64_CIE_ID; | |
6896c0c7 RH |
1479 | else |
1480 | cie_id = DW_CIE_ID; | |
cfc14b3a MK |
1481 | |
1482 | if (dwarf64_p) | |
1483 | { | |
1484 | cie_pointer = read_8_bytes (unit->abfd, buf); | |
1485 | buf += 8; | |
1486 | } | |
1487 | else | |
1488 | { | |
1489 | cie_pointer = read_4_bytes (unit->abfd, buf); | |
1490 | buf += 4; | |
1491 | } | |
1492 | ||
1493 | if (cie_pointer == cie_id) | |
1494 | { | |
1495 | /* This is a CIE. */ | |
1496 | struct dwarf2_cie *cie; | |
1497 | char *augmentation; | |
28ba0b33 | 1498 | unsigned int cie_version; |
cfc14b3a MK |
1499 | |
1500 | /* Record the offset into the .debug_frame section of this CIE. */ | |
1501 | cie_pointer = start - unit->dwarf_frame_buffer; | |
1502 | ||
1503 | /* Check whether we've already read it. */ | |
1504 | if (find_cie (unit, cie_pointer)) | |
1505 | return end; | |
1506 | ||
1507 | cie = (struct dwarf2_cie *) | |
8b92e4d5 | 1508 | obstack_alloc (&unit->objfile->objfile_obstack, |
cfc14b3a MK |
1509 | sizeof (struct dwarf2_cie)); |
1510 | cie->initial_instructions = NULL; | |
1511 | cie->cie_pointer = cie_pointer; | |
1512 | ||
1513 | /* The encoding for FDE's in a normal .debug_frame section | |
32b05c07 MK |
1514 | depends on the target address size. */ |
1515 | cie->encoding = DW_EH_PE_absptr; | |
cfc14b3a MK |
1516 | |
1517 | /* Check version number. */ | |
28ba0b33 PB |
1518 | cie_version = read_1_byte (unit->abfd, buf); |
1519 | if (cie_version != 1 && cie_version != 3) | |
6896c0c7 | 1520 | return NULL; |
cfc14b3a MK |
1521 | buf += 1; |
1522 | ||
1523 | /* Interpret the interesting bits of the augmentation. */ | |
852483bc MK |
1524 | augmentation = (char *) buf; |
1525 | buf += (strlen (augmentation) + 1); | |
cfc14b3a MK |
1526 | |
1527 | /* The GCC 2.x "eh" augmentation has a pointer immediately | |
1528 | following the augmentation string, so it must be handled | |
1529 | first. */ | |
1530 | if (augmentation[0] == 'e' && augmentation[1] == 'h') | |
1531 | { | |
1532 | /* Skip. */ | |
1533 | buf += TYPE_LENGTH (builtin_type_void_data_ptr); | |
1534 | augmentation += 2; | |
1535 | } | |
1536 | ||
1537 | cie->code_alignment_factor = | |
1538 | read_unsigned_leb128 (unit->abfd, buf, &bytes_read); | |
1539 | buf += bytes_read; | |
1540 | ||
1541 | cie->data_alignment_factor = | |
1542 | read_signed_leb128 (unit->abfd, buf, &bytes_read); | |
1543 | buf += bytes_read; | |
1544 | ||
28ba0b33 PB |
1545 | if (cie_version == 1) |
1546 | { | |
1547 | cie->return_address_register = read_1_byte (unit->abfd, buf); | |
1548 | bytes_read = 1; | |
1549 | } | |
1550 | else | |
1551 | cie->return_address_register = read_unsigned_leb128 (unit->abfd, buf, | |
1552 | &bytes_read); | |
4bf8967c AS |
1553 | if (eh_frame_p) |
1554 | cie->return_address_register | |
1555 | = dwarf2_frame_eh_frame_regnum (current_gdbarch, | |
1556 | cie->return_address_register); | |
1557 | ||
28ba0b33 | 1558 | buf += bytes_read; |
cfc14b3a | 1559 | |
7131cb6e RH |
1560 | cie->saw_z_augmentation = (*augmentation == 'z'); |
1561 | if (cie->saw_z_augmentation) | |
cfc14b3a MK |
1562 | { |
1563 | ULONGEST length; | |
1564 | ||
1565 | length = read_unsigned_leb128 (unit->abfd, buf, &bytes_read); | |
1566 | buf += bytes_read; | |
6896c0c7 RH |
1567 | if (buf > end) |
1568 | return NULL; | |
cfc14b3a MK |
1569 | cie->initial_instructions = buf + length; |
1570 | augmentation++; | |
1571 | } | |
1572 | ||
1573 | while (*augmentation) | |
1574 | { | |
1575 | /* "L" indicates a byte showing how the LSDA pointer is encoded. */ | |
1576 | if (*augmentation == 'L') | |
1577 | { | |
1578 | /* Skip. */ | |
1579 | buf++; | |
1580 | augmentation++; | |
1581 | } | |
1582 | ||
1583 | /* "R" indicates a byte indicating how FDE addresses are encoded. */ | |
1584 | else if (*augmentation == 'R') | |
1585 | { | |
1586 | cie->encoding = *buf++; | |
1587 | augmentation++; | |
1588 | } | |
1589 | ||
1590 | /* "P" indicates a personality routine in the CIE augmentation. */ | |
1591 | else if (*augmentation == 'P') | |
1592 | { | |
1234d960 | 1593 | /* Skip. Avoid indirection since we throw away the result. */ |
852483bc | 1594 | gdb_byte encoding = (*buf++) & ~DW_EH_PE_indirect; |
f724bf08 MK |
1595 | read_encoded_value (unit, encoding, buf, &bytes_read); |
1596 | buf += bytes_read; | |
cfc14b3a MK |
1597 | augmentation++; |
1598 | } | |
1599 | ||
1600 | /* Otherwise we have an unknown augmentation. | |
1601 | Bail out unless we saw a 'z' prefix. */ | |
1602 | else | |
1603 | { | |
1604 | if (cie->initial_instructions == NULL) | |
1605 | return end; | |
1606 | ||
1607 | /* Skip unknown augmentations. */ | |
1608 | buf = cie->initial_instructions; | |
1609 | break; | |
1610 | } | |
1611 | } | |
1612 | ||
1613 | cie->initial_instructions = buf; | |
1614 | cie->end = end; | |
1615 | ||
1616 | add_cie (unit, cie); | |
1617 | } | |
1618 | else | |
1619 | { | |
1620 | /* This is a FDE. */ | |
1621 | struct dwarf2_fde *fde; | |
1622 | ||
6896c0c7 RH |
1623 | /* In an .eh_frame section, the CIE pointer is the delta between the |
1624 | address within the FDE where the CIE pointer is stored and the | |
1625 | address of the CIE. Convert it to an offset into the .eh_frame | |
1626 | section. */ | |
cfc14b3a MK |
1627 | if (eh_frame_p) |
1628 | { | |
cfc14b3a MK |
1629 | cie_pointer = buf - unit->dwarf_frame_buffer - cie_pointer; |
1630 | cie_pointer -= (dwarf64_p ? 8 : 4); | |
1631 | } | |
1632 | ||
6896c0c7 RH |
1633 | /* In either case, validate the result is still within the section. */ |
1634 | if (cie_pointer >= unit->dwarf_frame_size) | |
1635 | return NULL; | |
1636 | ||
cfc14b3a | 1637 | fde = (struct dwarf2_fde *) |
8b92e4d5 | 1638 | obstack_alloc (&unit->objfile->objfile_obstack, |
cfc14b3a MK |
1639 | sizeof (struct dwarf2_fde)); |
1640 | fde->cie = find_cie (unit, cie_pointer); | |
1641 | if (fde->cie == NULL) | |
1642 | { | |
1643 | decode_frame_entry (unit, unit->dwarf_frame_buffer + cie_pointer, | |
1644 | eh_frame_p); | |
1645 | fde->cie = find_cie (unit, cie_pointer); | |
1646 | } | |
1647 | ||
1648 | gdb_assert (fde->cie != NULL); | |
1649 | ||
1650 | fde->initial_location = | |
1651 | read_encoded_value (unit, fde->cie->encoding, buf, &bytes_read); | |
1652 | buf += bytes_read; | |
1653 | ||
1654 | fde->address_range = | |
1655 | read_encoded_value (unit, fde->cie->encoding & 0x0f, buf, &bytes_read); | |
1656 | buf += bytes_read; | |
1657 | ||
7131cb6e RH |
1658 | /* A 'z' augmentation in the CIE implies the presence of an |
1659 | augmentation field in the FDE as well. The only thing known | |
1660 | to be in here at present is the LSDA entry for EH. So we | |
1661 | can skip the whole thing. */ | |
1662 | if (fde->cie->saw_z_augmentation) | |
1663 | { | |
1664 | ULONGEST length; | |
1665 | ||
1666 | length = read_unsigned_leb128 (unit->abfd, buf, &bytes_read); | |
1667 | buf += bytes_read + length; | |
6896c0c7 RH |
1668 | if (buf > end) |
1669 | return NULL; | |
7131cb6e RH |
1670 | } |
1671 | ||
cfc14b3a MK |
1672 | fde->instructions = buf; |
1673 | fde->end = end; | |
1674 | ||
4bf8967c AS |
1675 | fde->eh_frame_p = eh_frame_p; |
1676 | ||
cfc14b3a MK |
1677 | add_fde (unit, fde); |
1678 | } | |
1679 | ||
1680 | return end; | |
1681 | } | |
6896c0c7 RH |
1682 | |
1683 | /* Read a CIE or FDE in BUF and decode it. */ | |
852483bc MK |
1684 | static gdb_byte * |
1685 | decode_frame_entry (struct comp_unit *unit, gdb_byte *start, int eh_frame_p) | |
6896c0c7 RH |
1686 | { |
1687 | enum { NONE, ALIGN4, ALIGN8, FAIL } workaround = NONE; | |
852483bc | 1688 | gdb_byte *ret; |
6896c0c7 RH |
1689 | const char *msg; |
1690 | ptrdiff_t start_offset; | |
1691 | ||
1692 | while (1) | |
1693 | { | |
1694 | ret = decode_frame_entry_1 (unit, start, eh_frame_p); | |
1695 | if (ret != NULL) | |
1696 | break; | |
1697 | ||
1698 | /* We have corrupt input data of some form. */ | |
1699 | ||
1700 | /* ??? Try, weakly, to work around compiler/assembler/linker bugs | |
1701 | and mismatches wrt padding and alignment of debug sections. */ | |
1702 | /* Note that there is no requirement in the standard for any | |
1703 | alignment at all in the frame unwind sections. Testing for | |
1704 | alignment before trying to interpret data would be incorrect. | |
1705 | ||
1706 | However, GCC traditionally arranged for frame sections to be | |
1707 | sized such that the FDE length and CIE fields happen to be | |
1708 | aligned (in theory, for performance). This, unfortunately, | |
1709 | was done with .align directives, which had the side effect of | |
1710 | forcing the section to be aligned by the linker. | |
1711 | ||
1712 | This becomes a problem when you have some other producer that | |
1713 | creates frame sections that are not as strictly aligned. That | |
1714 | produces a hole in the frame info that gets filled by the | |
1715 | linker with zeros. | |
1716 | ||
1717 | The GCC behaviour is arguably a bug, but it's effectively now | |
1718 | part of the ABI, so we're now stuck with it, at least at the | |
1719 | object file level. A smart linker may decide, in the process | |
1720 | of compressing duplicate CIE information, that it can rewrite | |
1721 | the entire output section without this extra padding. */ | |
1722 | ||
1723 | start_offset = start - unit->dwarf_frame_buffer; | |
1724 | if (workaround < ALIGN4 && (start_offset & 3) != 0) | |
1725 | { | |
1726 | start += 4 - (start_offset & 3); | |
1727 | workaround = ALIGN4; | |
1728 | continue; | |
1729 | } | |
1730 | if (workaround < ALIGN8 && (start_offset & 7) != 0) | |
1731 | { | |
1732 | start += 8 - (start_offset & 7); | |
1733 | workaround = ALIGN8; | |
1734 | continue; | |
1735 | } | |
1736 | ||
1737 | /* Nothing left to try. Arrange to return as if we've consumed | |
1738 | the entire input section. Hopefully we'll get valid info from | |
1739 | the other of .debug_frame/.eh_frame. */ | |
1740 | workaround = FAIL; | |
1741 | ret = unit->dwarf_frame_buffer + unit->dwarf_frame_size; | |
1742 | break; | |
1743 | } | |
1744 | ||
1745 | switch (workaround) | |
1746 | { | |
1747 | case NONE: | |
1748 | break; | |
1749 | ||
1750 | case ALIGN4: | |
1751 | complaint (&symfile_complaints, | |
e2e0b3e5 | 1752 | _("Corrupt data in %s:%s; align 4 workaround apparently succeeded"), |
6896c0c7 RH |
1753 | unit->dwarf_frame_section->owner->filename, |
1754 | unit->dwarf_frame_section->name); | |
1755 | break; | |
1756 | ||
1757 | case ALIGN8: | |
1758 | complaint (&symfile_complaints, | |
e2e0b3e5 | 1759 | _("Corrupt data in %s:%s; align 8 workaround apparently succeeded"), |
6896c0c7 RH |
1760 | unit->dwarf_frame_section->owner->filename, |
1761 | unit->dwarf_frame_section->name); | |
1762 | break; | |
1763 | ||
1764 | default: | |
1765 | complaint (&symfile_complaints, | |
e2e0b3e5 | 1766 | _("Corrupt data in %s:%s"), |
6896c0c7 RH |
1767 | unit->dwarf_frame_section->owner->filename, |
1768 | unit->dwarf_frame_section->name); | |
1769 | break; | |
1770 | } | |
1771 | ||
1772 | return ret; | |
1773 | } | |
cfc14b3a MK |
1774 | \f |
1775 | ||
1776 | /* FIXME: kettenis/20030504: This still needs to be integrated with | |
1777 | dwarf2read.c in a better way. */ | |
1778 | ||
1779 | /* Imported from dwarf2read.c. */ | |
cfc14b3a | 1780 | extern asection *dwarf_frame_section; |
cfc14b3a MK |
1781 | extern asection *dwarf_eh_frame_section; |
1782 | ||
1783 | /* Imported from dwarf2read.c. */ | |
1193688d | 1784 | extern gdb_byte *dwarf2_read_section (struct objfile *objfile, asection *sectp); |
cfc14b3a MK |
1785 | |
1786 | void | |
1787 | dwarf2_build_frame_info (struct objfile *objfile) | |
1788 | { | |
1789 | struct comp_unit unit; | |
852483bc | 1790 | gdb_byte *frame_ptr; |
cfc14b3a MK |
1791 | |
1792 | /* Build a minimal decoding of the DWARF2 compilation unit. */ | |
1793 | unit.abfd = objfile->obfd; | |
1794 | unit.objfile = objfile; | |
0912c7f2 | 1795 | unit.dbase = 0; |
0fd85043 | 1796 | unit.tbase = 0; |
cfc14b3a MK |
1797 | |
1798 | /* First add the information from the .eh_frame section. That way, | |
1799 | the FDEs from that section are searched last. */ | |
188dd5d6 | 1800 | if (dwarf_eh_frame_section) |
cfc14b3a | 1801 | { |
0fd85043 | 1802 | asection *got, *txt; |
0912c7f2 | 1803 | |
cfc14b3a MK |
1804 | unit.cie = NULL; |
1805 | unit.dwarf_frame_buffer = dwarf2_read_section (objfile, | |
cfc14b3a MK |
1806 | dwarf_eh_frame_section); |
1807 | ||
2c500098 | 1808 | unit.dwarf_frame_size = bfd_get_section_size (dwarf_eh_frame_section); |
cfc14b3a MK |
1809 | unit.dwarf_frame_section = dwarf_eh_frame_section; |
1810 | ||
0912c7f2 | 1811 | /* FIXME: kettenis/20030602: This is the DW_EH_PE_datarel base |
37b517aa MK |
1812 | that is used for the i386/amd64 target, which currently is |
1813 | the only target in GCC that supports/uses the | |
1814 | DW_EH_PE_datarel encoding. */ | |
0912c7f2 MK |
1815 | got = bfd_get_section_by_name (unit.abfd, ".got"); |
1816 | if (got) | |
1817 | unit.dbase = got->vma; | |
1818 | ||
22c7ba1a MK |
1819 | /* GCC emits the DW_EH_PE_textrel encoding type on sh and ia64 |
1820 | so far. */ | |
0fd85043 CV |
1821 | txt = bfd_get_section_by_name (unit.abfd, ".text"); |
1822 | if (txt) | |
1823 | unit.tbase = txt->vma; | |
1824 | ||
cfc14b3a MK |
1825 | frame_ptr = unit.dwarf_frame_buffer; |
1826 | while (frame_ptr < unit.dwarf_frame_buffer + unit.dwarf_frame_size) | |
1827 | frame_ptr = decode_frame_entry (&unit, frame_ptr, 1); | |
1828 | } | |
1829 | ||
188dd5d6 | 1830 | if (dwarf_frame_section) |
cfc14b3a MK |
1831 | { |
1832 | unit.cie = NULL; | |
1833 | unit.dwarf_frame_buffer = dwarf2_read_section (objfile, | |
cfc14b3a | 1834 | dwarf_frame_section); |
2c500098 | 1835 | unit.dwarf_frame_size = bfd_get_section_size (dwarf_frame_section); |
cfc14b3a MK |
1836 | unit.dwarf_frame_section = dwarf_frame_section; |
1837 | ||
1838 | frame_ptr = unit.dwarf_frame_buffer; | |
1839 | while (frame_ptr < unit.dwarf_frame_buffer + unit.dwarf_frame_size) | |
1840 | frame_ptr = decode_frame_entry (&unit, frame_ptr, 0); | |
1841 | } | |
1842 | } | |
0d0e1a63 MK |
1843 | |
1844 | /* Provide a prototype to silence -Wmissing-prototypes. */ | |
1845 | void _initialize_dwarf2_frame (void); | |
1846 | ||
1847 | void | |
1848 | _initialize_dwarf2_frame (void) | |
1849 | { | |
030f20e1 | 1850 | dwarf2_frame_data = gdbarch_data_register_pre_init (dwarf2_frame_init); |
8f22cb90 | 1851 | dwarf2_frame_objfile_data = register_objfile_data (); |
0d0e1a63 | 1852 | } |