Commit | Line | Data |
---|---|---|
cfc14b3a MK |
1 | /* Frame unwinder for frames with DWARF Call Frame Information. |
2 | ||
4c38e0a4 | 3 | Copyright (C) 2003, 2004, 2005, 2007, 2008, 2009, 2010 |
0fb0cc75 | 4 | Free Software Foundation, Inc. |
cfc14b3a MK |
5 | |
6 | Contributed by Mark Kettenis. | |
7 | ||
8 | This file is part of GDB. | |
9 | ||
10 | This program is free software; you can redistribute it and/or modify | |
11 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 12 | the Free Software Foundation; either version 3 of the License, or |
cfc14b3a MK |
13 | (at your option) any later version. |
14 | ||
15 | This program is distributed in the hope that it will be useful, | |
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | GNU General Public License for more details. | |
19 | ||
20 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 21 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
cfc14b3a MK |
22 | |
23 | #include "defs.h" | |
24 | #include "dwarf2expr.h" | |
fa8f86ff | 25 | #include "dwarf2.h" |
cfc14b3a MK |
26 | #include "frame.h" |
27 | #include "frame-base.h" | |
28 | #include "frame-unwind.h" | |
29 | #include "gdbcore.h" | |
30 | #include "gdbtypes.h" | |
31 | #include "symtab.h" | |
32 | #include "objfiles.h" | |
33 | #include "regcache.h" | |
f2da6b3a | 34 | #include "value.h" |
cfc14b3a MK |
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 | ||
ae0d2f24 UW |
42 | struct comp_unit; |
43 | ||
cfc14b3a MK |
44 | /* Call Frame Information (CFI). */ |
45 | ||
46 | /* Common Information Entry (CIE). */ | |
47 | ||
48 | struct dwarf2_cie | |
49 | { | |
ae0d2f24 UW |
50 | /* Computation Unit for this CIE. */ |
51 | struct comp_unit *unit; | |
52 | ||
cfc14b3a MK |
53 | /* Offset into the .debug_frame section where this CIE was found. |
54 | Used to identify this CIE. */ | |
55 | ULONGEST cie_pointer; | |
56 | ||
57 | /* Constant that is factored out of all advance location | |
58 | instructions. */ | |
59 | ULONGEST code_alignment_factor; | |
60 | ||
61 | /* Constants that is factored out of all offset instructions. */ | |
62 | LONGEST data_alignment_factor; | |
63 | ||
64 | /* Return address column. */ | |
65 | ULONGEST return_address_register; | |
66 | ||
67 | /* Instruction sequence to initialize a register set. */ | |
852483bc MK |
68 | gdb_byte *initial_instructions; |
69 | gdb_byte *end; | |
cfc14b3a | 70 | |
303b6f5d DJ |
71 | /* Saved augmentation, in case it's needed later. */ |
72 | char *augmentation; | |
73 | ||
cfc14b3a | 74 | /* Encoding of addresses. */ |
852483bc | 75 | gdb_byte encoding; |
cfc14b3a | 76 | |
ae0d2f24 UW |
77 | /* Target address size in bytes. */ |
78 | int addr_size; | |
79 | ||
7131cb6e RH |
80 | /* True if a 'z' augmentation existed. */ |
81 | unsigned char saw_z_augmentation; | |
82 | ||
56c987f6 AO |
83 | /* True if an 'S' augmentation existed. */ |
84 | unsigned char signal_frame; | |
85 | ||
303b6f5d DJ |
86 | /* The version recorded in the CIE. */ |
87 | unsigned char version; | |
2dc7f7b3 TT |
88 | |
89 | /* The segment size. */ | |
90 | unsigned char segment_size; | |
b01c8410 | 91 | }; |
303b6f5d | 92 | |
b01c8410 PP |
93 | struct dwarf2_cie_table |
94 | { | |
95 | int num_entries; | |
96 | struct dwarf2_cie **entries; | |
cfc14b3a MK |
97 | }; |
98 | ||
99 | /* Frame Description Entry (FDE). */ | |
100 | ||
101 | struct dwarf2_fde | |
102 | { | |
103 | /* CIE for this FDE. */ | |
104 | struct dwarf2_cie *cie; | |
105 | ||
106 | /* First location associated with this FDE. */ | |
107 | CORE_ADDR initial_location; | |
108 | ||
109 | /* Number of bytes of program instructions described by this FDE. */ | |
110 | CORE_ADDR address_range; | |
111 | ||
112 | /* Instruction sequence. */ | |
852483bc MK |
113 | gdb_byte *instructions; |
114 | gdb_byte *end; | |
cfc14b3a | 115 | |
4bf8967c AS |
116 | /* True if this FDE is read from a .eh_frame instead of a .debug_frame |
117 | section. */ | |
118 | unsigned char eh_frame_p; | |
b01c8410 | 119 | }; |
4bf8967c | 120 | |
b01c8410 PP |
121 | struct dwarf2_fde_table |
122 | { | |
123 | int num_entries; | |
124 | struct dwarf2_fde **entries; | |
cfc14b3a MK |
125 | }; |
126 | ||
ae0d2f24 UW |
127 | /* A minimal decoding of DWARF2 compilation units. We only decode |
128 | what's needed to get to the call frame information. */ | |
129 | ||
130 | struct comp_unit | |
131 | { | |
132 | /* Keep the bfd convenient. */ | |
133 | bfd *abfd; | |
134 | ||
135 | struct objfile *objfile; | |
136 | ||
ae0d2f24 UW |
137 | /* Pointer to the .debug_frame section loaded into memory. */ |
138 | gdb_byte *dwarf_frame_buffer; | |
139 | ||
140 | /* Length of the loaded .debug_frame section. */ | |
c098b58b | 141 | bfd_size_type dwarf_frame_size; |
ae0d2f24 UW |
142 | |
143 | /* Pointer to the .debug_frame section. */ | |
144 | asection *dwarf_frame_section; | |
145 | ||
146 | /* Base for DW_EH_PE_datarel encodings. */ | |
147 | bfd_vma dbase; | |
148 | ||
149 | /* Base for DW_EH_PE_textrel encodings. */ | |
150 | bfd_vma tbase; | |
151 | }; | |
152 | ||
cfc14b3a | 153 | static struct dwarf2_fde *dwarf2_frame_find_fde (CORE_ADDR *pc); |
4fc771b8 DJ |
154 | |
155 | static int dwarf2_frame_adjust_regnum (struct gdbarch *gdbarch, int regnum, | |
156 | int eh_frame_p); | |
ae0d2f24 UW |
157 | |
158 | static CORE_ADDR read_encoded_value (struct comp_unit *unit, gdb_byte encoding, | |
159 | int ptr_len, gdb_byte *buf, | |
160 | unsigned int *bytes_read_ptr, | |
161 | CORE_ADDR func_base); | |
cfc14b3a MK |
162 | \f |
163 | ||
164 | /* Structure describing a frame state. */ | |
165 | ||
166 | struct dwarf2_frame_state | |
167 | { | |
168 | /* Each register save state can be described in terms of a CFA slot, | |
169 | another register, or a location expression. */ | |
170 | struct dwarf2_frame_state_reg_info | |
171 | { | |
05cbe71a | 172 | struct dwarf2_frame_state_reg *reg; |
cfc14b3a MK |
173 | int num_regs; |
174 | ||
2fd481e1 PP |
175 | LONGEST cfa_offset; |
176 | ULONGEST cfa_reg; | |
177 | enum { | |
178 | CFA_UNSET, | |
179 | CFA_REG_OFFSET, | |
180 | CFA_EXP | |
181 | } cfa_how; | |
182 | gdb_byte *cfa_exp; | |
183 | ||
cfc14b3a MK |
184 | /* Used to implement DW_CFA_remember_state. */ |
185 | struct dwarf2_frame_state_reg_info *prev; | |
186 | } regs; | |
187 | ||
cfc14b3a MK |
188 | /* The PC described by the current frame state. */ |
189 | CORE_ADDR pc; | |
190 | ||
191 | /* Initial register set from the CIE. | |
192 | Used to implement DW_CFA_restore. */ | |
193 | struct dwarf2_frame_state_reg_info initial; | |
194 | ||
195 | /* The information we care about from the CIE. */ | |
196 | LONGEST data_align; | |
197 | ULONGEST code_align; | |
198 | ULONGEST retaddr_column; | |
303b6f5d DJ |
199 | |
200 | /* Flags for known producer quirks. */ | |
201 | ||
202 | /* The ARM compilers, in DWARF2 mode, assume that DW_CFA_def_cfa | |
203 | and DW_CFA_def_cfa_offset takes a factored offset. */ | |
204 | int armcc_cfa_offsets_sf; | |
205 | ||
206 | /* The ARM compilers, in DWARF2 or DWARF3 mode, may assume that | |
207 | the CFA is defined as REG - OFFSET rather than REG + OFFSET. */ | |
208 | int armcc_cfa_offsets_reversed; | |
cfc14b3a MK |
209 | }; |
210 | ||
211 | /* Store the length the expression for the CFA in the `cfa_reg' field, | |
212 | which is unused in that case. */ | |
213 | #define cfa_exp_len cfa_reg | |
214 | ||
f57d151a | 215 | /* Assert that the register set RS is large enough to store gdbarch_num_regs |
cfc14b3a MK |
216 | columns. If necessary, enlarge the register set. */ |
217 | ||
218 | static void | |
219 | dwarf2_frame_state_alloc_regs (struct dwarf2_frame_state_reg_info *rs, | |
220 | int num_regs) | |
221 | { | |
222 | size_t size = sizeof (struct dwarf2_frame_state_reg); | |
223 | ||
224 | if (num_regs <= rs->num_regs) | |
225 | return; | |
226 | ||
227 | rs->reg = (struct dwarf2_frame_state_reg *) | |
228 | xrealloc (rs->reg, num_regs * size); | |
229 | ||
230 | /* Initialize newly allocated registers. */ | |
2473a4a9 | 231 | memset (rs->reg + rs->num_regs, 0, (num_regs - rs->num_regs) * size); |
cfc14b3a MK |
232 | rs->num_regs = num_regs; |
233 | } | |
234 | ||
235 | /* Copy the register columns in register set RS into newly allocated | |
236 | memory and return a pointer to this newly created copy. */ | |
237 | ||
238 | static struct dwarf2_frame_state_reg * | |
239 | dwarf2_frame_state_copy_regs (struct dwarf2_frame_state_reg_info *rs) | |
240 | { | |
d10891d4 | 241 | size_t size = rs->num_regs * sizeof (struct dwarf2_frame_state_reg); |
cfc14b3a MK |
242 | struct dwarf2_frame_state_reg *reg; |
243 | ||
244 | reg = (struct dwarf2_frame_state_reg *) xmalloc (size); | |
245 | memcpy (reg, rs->reg, size); | |
246 | ||
247 | return reg; | |
248 | } | |
249 | ||
250 | /* Release the memory allocated to register set RS. */ | |
251 | ||
252 | static void | |
253 | dwarf2_frame_state_free_regs (struct dwarf2_frame_state_reg_info *rs) | |
254 | { | |
255 | if (rs) | |
256 | { | |
257 | dwarf2_frame_state_free_regs (rs->prev); | |
258 | ||
259 | xfree (rs->reg); | |
260 | xfree (rs); | |
261 | } | |
262 | } | |
263 | ||
264 | /* Release the memory allocated to the frame state FS. */ | |
265 | ||
266 | static void | |
267 | dwarf2_frame_state_free (void *p) | |
268 | { | |
269 | struct dwarf2_frame_state *fs = p; | |
270 | ||
271 | dwarf2_frame_state_free_regs (fs->initial.prev); | |
272 | dwarf2_frame_state_free_regs (fs->regs.prev); | |
273 | xfree (fs->initial.reg); | |
274 | xfree (fs->regs.reg); | |
275 | xfree (fs); | |
276 | } | |
277 | \f | |
278 | ||
279 | /* Helper functions for execute_stack_op. */ | |
280 | ||
281 | static CORE_ADDR | |
282 | read_reg (void *baton, int reg) | |
283 | { | |
4a4e5149 DJ |
284 | struct frame_info *this_frame = (struct frame_info *) baton; |
285 | struct gdbarch *gdbarch = get_frame_arch (this_frame); | |
cfc14b3a | 286 | int regnum; |
852483bc | 287 | gdb_byte *buf; |
cfc14b3a | 288 | |
ad010def | 289 | regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg); |
cfc14b3a | 290 | |
852483bc | 291 | buf = alloca (register_size (gdbarch, regnum)); |
4a4e5149 | 292 | get_frame_register (this_frame, regnum, buf); |
f2da6b3a DJ |
293 | |
294 | /* Convert the register to an integer. This returns a LONGEST | |
295 | rather than a CORE_ADDR, but unpack_pointer does the same thing | |
296 | under the covers, and this makes more sense for non-pointer | |
297 | registers. Maybe read_reg and the associated interfaces should | |
298 | deal with "struct value" instead of CORE_ADDR. */ | |
299 | return unpack_long (register_type (gdbarch, regnum), buf); | |
cfc14b3a MK |
300 | } |
301 | ||
302 | static void | |
852483bc | 303 | read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len) |
cfc14b3a MK |
304 | { |
305 | read_memory (addr, buf, len); | |
306 | } | |
307 | ||
308 | static void | |
852483bc | 309 | no_get_frame_base (void *baton, gdb_byte **start, size_t *length) |
cfc14b3a MK |
310 | { |
311 | internal_error (__FILE__, __LINE__, | |
e2e0b3e5 | 312 | _("Support for DW_OP_fbreg is unimplemented")); |
cfc14b3a MK |
313 | } |
314 | ||
e7802207 TT |
315 | /* Helper function for execute_stack_op. */ |
316 | ||
317 | static CORE_ADDR | |
318 | no_get_frame_cfa (void *baton) | |
319 | { | |
320 | internal_error (__FILE__, __LINE__, | |
321 | _("Support for DW_OP_call_frame_cfa is unimplemented")); | |
322 | } | |
323 | ||
cfc14b3a MK |
324 | static CORE_ADDR |
325 | no_get_tls_address (void *baton, CORE_ADDR offset) | |
326 | { | |
327 | internal_error (__FILE__, __LINE__, | |
e2e0b3e5 | 328 | _("Support for DW_OP_GNU_push_tls_address is unimplemented")); |
cfc14b3a MK |
329 | } |
330 | ||
a6a5a945 LM |
331 | /* Execute the required actions for both the DW_CFA_restore and |
332 | DW_CFA_restore_extended instructions. */ | |
333 | static void | |
334 | dwarf2_restore_rule (struct gdbarch *gdbarch, ULONGEST reg_num, | |
335 | struct dwarf2_frame_state *fs, int eh_frame_p) | |
336 | { | |
337 | ULONGEST reg; | |
338 | ||
339 | gdb_assert (fs->initial.reg); | |
340 | reg = dwarf2_frame_adjust_regnum (gdbarch, reg_num, eh_frame_p); | |
341 | dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1); | |
342 | ||
343 | /* Check if this register was explicitly initialized in the | |
344 | CIE initial instructions. If not, default the rule to | |
345 | UNSPECIFIED. */ | |
346 | if (reg < fs->initial.num_regs) | |
347 | fs->regs.reg[reg] = fs->initial.reg[reg]; | |
348 | else | |
349 | fs->regs.reg[reg].how = DWARF2_FRAME_REG_UNSPECIFIED; | |
350 | ||
351 | if (fs->regs.reg[reg].how == DWARF2_FRAME_REG_UNSPECIFIED) | |
352 | complaint (&symfile_complaints, _("\ | |
353 | incomplete CFI data; DW_CFA_restore unspecified\n\ | |
5af949e3 | 354 | register %s (#%d) at %s"), |
a6a5a945 LM |
355 | gdbarch_register_name |
356 | (gdbarch, gdbarch_dwarf2_reg_to_regnum (gdbarch, reg)), | |
357 | gdbarch_dwarf2_reg_to_regnum (gdbarch, reg), | |
5af949e3 | 358 | paddress (gdbarch, fs->pc)); |
a6a5a945 LM |
359 | } |
360 | ||
cfc14b3a | 361 | static CORE_ADDR |
ae0d2f24 | 362 | execute_stack_op (gdb_byte *exp, ULONGEST len, int addr_size, |
44353522 DE |
363 | struct frame_info *this_frame, CORE_ADDR initial, |
364 | int initial_in_stack_memory) | |
cfc14b3a MK |
365 | { |
366 | struct dwarf_expr_context *ctx; | |
367 | CORE_ADDR result; | |
4a227398 | 368 | struct cleanup *old_chain; |
cfc14b3a MK |
369 | |
370 | ctx = new_dwarf_expr_context (); | |
4a227398 TT |
371 | old_chain = make_cleanup_free_dwarf_expr_context (ctx); |
372 | ||
f7fd4728 | 373 | ctx->gdbarch = get_frame_arch (this_frame); |
ae0d2f24 | 374 | ctx->addr_size = addr_size; |
4a4e5149 | 375 | ctx->baton = this_frame; |
cfc14b3a MK |
376 | ctx->read_reg = read_reg; |
377 | ctx->read_mem = read_mem; | |
378 | ctx->get_frame_base = no_get_frame_base; | |
e7802207 | 379 | ctx->get_frame_cfa = no_get_frame_cfa; |
cfc14b3a MK |
380 | ctx->get_tls_address = no_get_tls_address; |
381 | ||
44353522 | 382 | dwarf_expr_push (ctx, initial, initial_in_stack_memory); |
cfc14b3a MK |
383 | dwarf_expr_eval (ctx, exp, len); |
384 | result = dwarf_expr_fetch (ctx, 0); | |
385 | ||
cec03d70 | 386 | if (ctx->location == DWARF_VALUE_REGISTER) |
4a4e5149 | 387 | result = read_reg (this_frame, result); |
cec03d70 TT |
388 | else if (ctx->location != DWARF_VALUE_MEMORY) |
389 | { | |
390 | /* This is actually invalid DWARF, but if we ever do run across | |
391 | it somehow, we might as well support it. So, instead, report | |
392 | it as unimplemented. */ | |
393 | error (_("Not implemented: computing unwound register using explicit value operator")); | |
394 | } | |
cfc14b3a | 395 | |
4a227398 | 396 | do_cleanups (old_chain); |
cfc14b3a MK |
397 | |
398 | return result; | |
399 | } | |
400 | \f | |
401 | ||
402 | static void | |
ae0d2f24 | 403 | execute_cfa_program (struct dwarf2_fde *fde, gdb_byte *insn_ptr, |
4a4e5149 | 404 | gdb_byte *insn_end, struct frame_info *this_frame, |
ae0d2f24 | 405 | struct dwarf2_frame_state *fs) |
cfc14b3a | 406 | { |
ae0d2f24 | 407 | int eh_frame_p = fde->eh_frame_p; |
4a4e5149 | 408 | CORE_ADDR pc = get_frame_pc (this_frame); |
cfc14b3a | 409 | int bytes_read; |
4a4e5149 | 410 | struct gdbarch *gdbarch = get_frame_arch (this_frame); |
e17a4113 | 411 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); |
cfc14b3a MK |
412 | |
413 | while (insn_ptr < insn_end && fs->pc <= pc) | |
414 | { | |
852483bc | 415 | gdb_byte insn = *insn_ptr++; |
cfc14b3a MK |
416 | ULONGEST utmp, reg; |
417 | LONGEST offset; | |
418 | ||
419 | if ((insn & 0xc0) == DW_CFA_advance_loc) | |
420 | fs->pc += (insn & 0x3f) * fs->code_align; | |
421 | else if ((insn & 0xc0) == DW_CFA_offset) | |
422 | { | |
423 | reg = insn & 0x3f; | |
4fc771b8 | 424 | reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p); |
cfc14b3a MK |
425 | insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp); |
426 | offset = utmp * fs->data_align; | |
427 | dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1); | |
05cbe71a | 428 | fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET; |
cfc14b3a MK |
429 | fs->regs.reg[reg].loc.offset = offset; |
430 | } | |
431 | else if ((insn & 0xc0) == DW_CFA_restore) | |
432 | { | |
cfc14b3a | 433 | reg = insn & 0x3f; |
a6a5a945 | 434 | dwarf2_restore_rule (gdbarch, reg, fs, eh_frame_p); |
cfc14b3a MK |
435 | } |
436 | else | |
437 | { | |
438 | switch (insn) | |
439 | { | |
440 | case DW_CFA_set_loc: | |
ae0d2f24 UW |
441 | fs->pc = read_encoded_value (fde->cie->unit, fde->cie->encoding, |
442 | fde->cie->addr_size, insn_ptr, | |
443 | &bytes_read, fde->initial_location); | |
444 | /* Apply the objfile offset for relocatable objects. */ | |
445 | fs->pc += ANOFFSET (fde->cie->unit->objfile->section_offsets, | |
446 | SECT_OFF_TEXT (fde->cie->unit->objfile)); | |
cfc14b3a MK |
447 | insn_ptr += bytes_read; |
448 | break; | |
449 | ||
450 | case DW_CFA_advance_loc1: | |
e17a4113 | 451 | utmp = extract_unsigned_integer (insn_ptr, 1, byte_order); |
cfc14b3a MK |
452 | fs->pc += utmp * fs->code_align; |
453 | insn_ptr++; | |
454 | break; | |
455 | case DW_CFA_advance_loc2: | |
e17a4113 | 456 | utmp = extract_unsigned_integer (insn_ptr, 2, byte_order); |
cfc14b3a MK |
457 | fs->pc += utmp * fs->code_align; |
458 | insn_ptr += 2; | |
459 | break; | |
460 | case DW_CFA_advance_loc4: | |
e17a4113 | 461 | utmp = extract_unsigned_integer (insn_ptr, 4, byte_order); |
cfc14b3a MK |
462 | fs->pc += utmp * fs->code_align; |
463 | insn_ptr += 4; | |
464 | break; | |
465 | ||
466 | case DW_CFA_offset_extended: | |
467 | insn_ptr = read_uleb128 (insn_ptr, insn_end, ®); | |
4fc771b8 | 468 | reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p); |
cfc14b3a MK |
469 | insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp); |
470 | offset = utmp * fs->data_align; | |
471 | dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1); | |
05cbe71a | 472 | fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET; |
cfc14b3a MK |
473 | fs->regs.reg[reg].loc.offset = offset; |
474 | break; | |
475 | ||
476 | case DW_CFA_restore_extended: | |
cfc14b3a | 477 | insn_ptr = read_uleb128 (insn_ptr, insn_end, ®); |
a6a5a945 | 478 | dwarf2_restore_rule (gdbarch, reg, fs, eh_frame_p); |
cfc14b3a MK |
479 | break; |
480 | ||
481 | case DW_CFA_undefined: | |
482 | insn_ptr = read_uleb128 (insn_ptr, insn_end, ®); | |
4fc771b8 | 483 | reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p); |
cfc14b3a | 484 | dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1); |
05cbe71a | 485 | fs->regs.reg[reg].how = DWARF2_FRAME_REG_UNDEFINED; |
cfc14b3a MK |
486 | break; |
487 | ||
488 | case DW_CFA_same_value: | |
489 | insn_ptr = read_uleb128 (insn_ptr, insn_end, ®); | |
4fc771b8 | 490 | reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p); |
cfc14b3a | 491 | dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1); |
05cbe71a | 492 | fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAME_VALUE; |
cfc14b3a MK |
493 | break; |
494 | ||
495 | case DW_CFA_register: | |
496 | insn_ptr = read_uleb128 (insn_ptr, insn_end, ®); | |
4fc771b8 | 497 | reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p); |
cfc14b3a | 498 | insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp); |
4fc771b8 | 499 | utmp = dwarf2_frame_adjust_regnum (gdbarch, utmp, eh_frame_p); |
cfc14b3a | 500 | dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1); |
05cbe71a | 501 | fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_REG; |
cfc14b3a MK |
502 | fs->regs.reg[reg].loc.reg = utmp; |
503 | break; | |
504 | ||
505 | case DW_CFA_remember_state: | |
506 | { | |
507 | struct dwarf2_frame_state_reg_info *new_rs; | |
508 | ||
509 | new_rs = XMALLOC (struct dwarf2_frame_state_reg_info); | |
510 | *new_rs = fs->regs; | |
511 | fs->regs.reg = dwarf2_frame_state_copy_regs (&fs->regs); | |
512 | fs->regs.prev = new_rs; | |
513 | } | |
514 | break; | |
515 | ||
516 | case DW_CFA_restore_state: | |
517 | { | |
518 | struct dwarf2_frame_state_reg_info *old_rs = fs->regs.prev; | |
519 | ||
50ea7769 MK |
520 | if (old_rs == NULL) |
521 | { | |
e2e0b3e5 | 522 | complaint (&symfile_complaints, _("\ |
5af949e3 UW |
523 | bad CFI data; mismatched DW_CFA_restore_state at %s"), |
524 | paddress (gdbarch, fs->pc)); | |
50ea7769 MK |
525 | } |
526 | else | |
527 | { | |
528 | xfree (fs->regs.reg); | |
529 | fs->regs = *old_rs; | |
530 | xfree (old_rs); | |
531 | } | |
cfc14b3a MK |
532 | } |
533 | break; | |
534 | ||
535 | case DW_CFA_def_cfa: | |
2fd481e1 | 536 | insn_ptr = read_uleb128 (insn_ptr, insn_end, &fs->regs.cfa_reg); |
cfc14b3a | 537 | insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp); |
303b6f5d DJ |
538 | |
539 | if (fs->armcc_cfa_offsets_sf) | |
540 | utmp *= fs->data_align; | |
541 | ||
2fd481e1 PP |
542 | fs->regs.cfa_offset = utmp; |
543 | fs->regs.cfa_how = CFA_REG_OFFSET; | |
cfc14b3a MK |
544 | break; |
545 | ||
546 | case DW_CFA_def_cfa_register: | |
2fd481e1 PP |
547 | insn_ptr = read_uleb128 (insn_ptr, insn_end, &fs->regs.cfa_reg); |
548 | fs->regs.cfa_reg = dwarf2_frame_adjust_regnum (gdbarch, | |
549 | fs->regs.cfa_reg, | |
550 | eh_frame_p); | |
551 | fs->regs.cfa_how = CFA_REG_OFFSET; | |
cfc14b3a MK |
552 | break; |
553 | ||
554 | case DW_CFA_def_cfa_offset: | |
852483bc | 555 | insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp); |
303b6f5d DJ |
556 | |
557 | if (fs->armcc_cfa_offsets_sf) | |
558 | utmp *= fs->data_align; | |
559 | ||
2fd481e1 | 560 | fs->regs.cfa_offset = utmp; |
cfc14b3a MK |
561 | /* cfa_how deliberately not set. */ |
562 | break; | |
563 | ||
a8504492 MK |
564 | case DW_CFA_nop: |
565 | break; | |
566 | ||
cfc14b3a | 567 | case DW_CFA_def_cfa_expression: |
2fd481e1 PP |
568 | insn_ptr = read_uleb128 (insn_ptr, insn_end, |
569 | &fs->regs.cfa_exp_len); | |
570 | fs->regs.cfa_exp = insn_ptr; | |
571 | fs->regs.cfa_how = CFA_EXP; | |
572 | insn_ptr += fs->regs.cfa_exp_len; | |
cfc14b3a MK |
573 | break; |
574 | ||
575 | case DW_CFA_expression: | |
576 | insn_ptr = read_uleb128 (insn_ptr, insn_end, ®); | |
4fc771b8 | 577 | reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p); |
cfc14b3a MK |
578 | dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1); |
579 | insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp); | |
580 | fs->regs.reg[reg].loc.exp = insn_ptr; | |
581 | fs->regs.reg[reg].exp_len = utmp; | |
05cbe71a | 582 | fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_EXP; |
cfc14b3a MK |
583 | insn_ptr += utmp; |
584 | break; | |
585 | ||
a8504492 MK |
586 | case DW_CFA_offset_extended_sf: |
587 | insn_ptr = read_uleb128 (insn_ptr, insn_end, ®); | |
4fc771b8 | 588 | reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p); |
a8504492 | 589 | insn_ptr = read_sleb128 (insn_ptr, insn_end, &offset); |
f6da8dd8 | 590 | offset *= fs->data_align; |
a8504492 | 591 | dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1); |
05cbe71a | 592 | fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET; |
a8504492 MK |
593 | fs->regs.reg[reg].loc.offset = offset; |
594 | break; | |
595 | ||
46ea248b AO |
596 | case DW_CFA_val_offset: |
597 | insn_ptr = read_uleb128 (insn_ptr, insn_end, ®); | |
598 | dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1); | |
599 | insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp); | |
600 | offset = utmp * fs->data_align; | |
601 | fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_OFFSET; | |
602 | fs->regs.reg[reg].loc.offset = offset; | |
603 | break; | |
604 | ||
605 | case DW_CFA_val_offset_sf: | |
606 | insn_ptr = read_uleb128 (insn_ptr, insn_end, ®); | |
607 | dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1); | |
608 | insn_ptr = read_sleb128 (insn_ptr, insn_end, &offset); | |
609 | offset *= fs->data_align; | |
610 | fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_OFFSET; | |
611 | fs->regs.reg[reg].loc.offset = offset; | |
612 | break; | |
613 | ||
614 | case DW_CFA_val_expression: | |
615 | insn_ptr = read_uleb128 (insn_ptr, insn_end, ®); | |
616 | dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1); | |
617 | insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp); | |
618 | fs->regs.reg[reg].loc.exp = insn_ptr; | |
619 | fs->regs.reg[reg].exp_len = utmp; | |
620 | fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_EXP; | |
621 | insn_ptr += utmp; | |
622 | break; | |
623 | ||
a8504492 | 624 | case DW_CFA_def_cfa_sf: |
2fd481e1 PP |
625 | insn_ptr = read_uleb128 (insn_ptr, insn_end, &fs->regs.cfa_reg); |
626 | fs->regs.cfa_reg = dwarf2_frame_adjust_regnum (gdbarch, | |
627 | fs->regs.cfa_reg, | |
628 | eh_frame_p); | |
a8504492 | 629 | insn_ptr = read_sleb128 (insn_ptr, insn_end, &offset); |
2fd481e1 PP |
630 | fs->regs.cfa_offset = offset * fs->data_align; |
631 | fs->regs.cfa_how = CFA_REG_OFFSET; | |
a8504492 MK |
632 | break; |
633 | ||
634 | case DW_CFA_def_cfa_offset_sf: | |
635 | insn_ptr = read_sleb128 (insn_ptr, insn_end, &offset); | |
2fd481e1 | 636 | fs->regs.cfa_offset = offset * fs->data_align; |
a8504492 | 637 | /* cfa_how deliberately not set. */ |
cfc14b3a MK |
638 | break; |
639 | ||
a77f4086 MK |
640 | case DW_CFA_GNU_window_save: |
641 | /* This is SPARC-specific code, and contains hard-coded | |
642 | constants for the register numbering scheme used by | |
643 | GCC. Rather than having a architecture-specific | |
644 | operation that's only ever used by a single | |
645 | architecture, we provide the implementation here. | |
646 | Incidentally that's what GCC does too in its | |
647 | unwinder. */ | |
648 | { | |
4a4e5149 | 649 | int size = register_size (gdbarch, 0); |
9a619af0 | 650 | |
a77f4086 MK |
651 | dwarf2_frame_state_alloc_regs (&fs->regs, 32); |
652 | for (reg = 8; reg < 16; reg++) | |
653 | { | |
654 | fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_REG; | |
655 | fs->regs.reg[reg].loc.reg = reg + 16; | |
656 | } | |
657 | for (reg = 16; reg < 32; reg++) | |
658 | { | |
659 | fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET; | |
660 | fs->regs.reg[reg].loc.offset = (reg - 16) * size; | |
661 | } | |
662 | } | |
663 | break; | |
664 | ||
cfc14b3a MK |
665 | case DW_CFA_GNU_args_size: |
666 | /* Ignored. */ | |
667 | insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp); | |
668 | break; | |
669 | ||
58894217 JK |
670 | case DW_CFA_GNU_negative_offset_extended: |
671 | insn_ptr = read_uleb128 (insn_ptr, insn_end, ®); | |
4fc771b8 | 672 | reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p); |
58894217 JK |
673 | insn_ptr = read_uleb128 (insn_ptr, insn_end, &offset); |
674 | offset *= fs->data_align; | |
675 | dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1); | |
676 | fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET; | |
677 | fs->regs.reg[reg].loc.offset = -offset; | |
678 | break; | |
679 | ||
cfc14b3a | 680 | default: |
e2e0b3e5 | 681 | internal_error (__FILE__, __LINE__, _("Unknown CFI encountered.")); |
cfc14b3a MK |
682 | } |
683 | } | |
684 | } | |
685 | ||
686 | /* Don't allow remember/restore between CIE and FDE programs. */ | |
687 | dwarf2_frame_state_free_regs (fs->regs.prev); | |
688 | fs->regs.prev = NULL; | |
689 | } | |
8f22cb90 | 690 | \f |
cfc14b3a | 691 | |
8f22cb90 | 692 | /* Architecture-specific operations. */ |
cfc14b3a | 693 | |
8f22cb90 MK |
694 | /* Per-architecture data key. */ |
695 | static struct gdbarch_data *dwarf2_frame_data; | |
696 | ||
697 | struct dwarf2_frame_ops | |
698 | { | |
699 | /* Pre-initialize the register state REG for register REGNUM. */ | |
aff37fc1 DM |
700 | void (*init_reg) (struct gdbarch *, int, struct dwarf2_frame_state_reg *, |
701 | struct frame_info *); | |
3ed09a32 | 702 | |
4a4e5149 | 703 | /* Check whether the THIS_FRAME is a signal trampoline. */ |
3ed09a32 | 704 | int (*signal_frame_p) (struct gdbarch *, struct frame_info *); |
4bf8967c | 705 | |
4fc771b8 DJ |
706 | /* Convert .eh_frame register number to DWARF register number, or |
707 | adjust .debug_frame register number. */ | |
708 | int (*adjust_regnum) (struct gdbarch *, int, int); | |
cfc14b3a MK |
709 | }; |
710 | ||
8f22cb90 MK |
711 | /* Default architecture-specific register state initialization |
712 | function. */ | |
713 | ||
714 | static void | |
715 | dwarf2_frame_default_init_reg (struct gdbarch *gdbarch, int regnum, | |
aff37fc1 | 716 | struct dwarf2_frame_state_reg *reg, |
4a4e5149 | 717 | struct frame_info *this_frame) |
8f22cb90 MK |
718 | { |
719 | /* If we have a register that acts as a program counter, mark it as | |
720 | a destination for the return address. If we have a register that | |
721 | serves as the stack pointer, arrange for it to be filled with the | |
722 | call frame address (CFA). The other registers are marked as | |
723 | unspecified. | |
724 | ||
725 | We copy the return address to the program counter, since many | |
726 | parts in GDB assume that it is possible to get the return address | |
727 | by unwinding the program counter register. However, on ISA's | |
728 | with a dedicated return address register, the CFI usually only | |
729 | contains information to unwind that return address register. | |
730 | ||
731 | The reason we're treating the stack pointer special here is | |
732 | because in many cases GCC doesn't emit CFI for the stack pointer | |
733 | and implicitly assumes that it is equal to the CFA. This makes | |
734 | some sense since the DWARF specification (version 3, draft 8, | |
735 | p. 102) says that: | |
736 | ||
737 | "Typically, the CFA is defined to be the value of the stack | |
738 | pointer at the call site in the previous frame (which may be | |
739 | different from its value on entry to the current frame)." | |
740 | ||
741 | However, this isn't true for all platforms supported by GCC | |
742 | (e.g. IBM S/390 and zSeries). Those architectures should provide | |
743 | their own architecture-specific initialization function. */ | |
05cbe71a | 744 | |
ad010def | 745 | if (regnum == gdbarch_pc_regnum (gdbarch)) |
8f22cb90 | 746 | reg->how = DWARF2_FRAME_REG_RA; |
ad010def | 747 | else if (regnum == gdbarch_sp_regnum (gdbarch)) |
8f22cb90 MK |
748 | reg->how = DWARF2_FRAME_REG_CFA; |
749 | } | |
05cbe71a | 750 | |
8f22cb90 | 751 | /* Return a default for the architecture-specific operations. */ |
05cbe71a | 752 | |
8f22cb90 | 753 | static void * |
030f20e1 | 754 | dwarf2_frame_init (struct obstack *obstack) |
8f22cb90 MK |
755 | { |
756 | struct dwarf2_frame_ops *ops; | |
757 | ||
030f20e1 | 758 | ops = OBSTACK_ZALLOC (obstack, struct dwarf2_frame_ops); |
8f22cb90 MK |
759 | ops->init_reg = dwarf2_frame_default_init_reg; |
760 | return ops; | |
761 | } | |
05cbe71a | 762 | |
8f22cb90 MK |
763 | /* Set the architecture-specific register state initialization |
764 | function for GDBARCH to INIT_REG. */ | |
765 | ||
766 | void | |
767 | dwarf2_frame_set_init_reg (struct gdbarch *gdbarch, | |
768 | void (*init_reg) (struct gdbarch *, int, | |
aff37fc1 DM |
769 | struct dwarf2_frame_state_reg *, |
770 | struct frame_info *)) | |
8f22cb90 | 771 | { |
030f20e1 | 772 | struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data); |
8f22cb90 | 773 | |
8f22cb90 MK |
774 | ops->init_reg = init_reg; |
775 | } | |
776 | ||
777 | /* Pre-initialize the register state REG for register REGNUM. */ | |
05cbe71a MK |
778 | |
779 | static void | |
780 | dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum, | |
aff37fc1 | 781 | struct dwarf2_frame_state_reg *reg, |
4a4e5149 | 782 | struct frame_info *this_frame) |
05cbe71a | 783 | { |
030f20e1 | 784 | struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data); |
8f22cb90 | 785 | |
4a4e5149 | 786 | ops->init_reg (gdbarch, regnum, reg, this_frame); |
05cbe71a | 787 | } |
3ed09a32 DJ |
788 | |
789 | /* Set the architecture-specific signal trampoline recognition | |
790 | function for GDBARCH to SIGNAL_FRAME_P. */ | |
791 | ||
792 | void | |
793 | dwarf2_frame_set_signal_frame_p (struct gdbarch *gdbarch, | |
794 | int (*signal_frame_p) (struct gdbarch *, | |
795 | struct frame_info *)) | |
796 | { | |
797 | struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data); | |
798 | ||
799 | ops->signal_frame_p = signal_frame_p; | |
800 | } | |
801 | ||
802 | /* Query the architecture-specific signal frame recognizer for | |
4a4e5149 | 803 | THIS_FRAME. */ |
3ed09a32 DJ |
804 | |
805 | static int | |
806 | dwarf2_frame_signal_frame_p (struct gdbarch *gdbarch, | |
4a4e5149 | 807 | struct frame_info *this_frame) |
3ed09a32 DJ |
808 | { |
809 | struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data); | |
810 | ||
811 | if (ops->signal_frame_p == NULL) | |
812 | return 0; | |
4a4e5149 | 813 | return ops->signal_frame_p (gdbarch, this_frame); |
3ed09a32 | 814 | } |
4bf8967c | 815 | |
4fc771b8 DJ |
816 | /* Set the architecture-specific adjustment of .eh_frame and .debug_frame |
817 | register numbers. */ | |
4bf8967c AS |
818 | |
819 | void | |
4fc771b8 DJ |
820 | dwarf2_frame_set_adjust_regnum (struct gdbarch *gdbarch, |
821 | int (*adjust_regnum) (struct gdbarch *, | |
822 | int, int)) | |
4bf8967c AS |
823 | { |
824 | struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data); | |
825 | ||
4fc771b8 | 826 | ops->adjust_regnum = adjust_regnum; |
4bf8967c AS |
827 | } |
828 | ||
4fc771b8 DJ |
829 | /* Translate a .eh_frame register to DWARF register, or adjust a .debug_frame |
830 | register. */ | |
4bf8967c | 831 | |
4fc771b8 DJ |
832 | static int |
833 | dwarf2_frame_adjust_regnum (struct gdbarch *gdbarch, int regnum, int eh_frame_p) | |
4bf8967c AS |
834 | { |
835 | struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data); | |
836 | ||
4fc771b8 | 837 | if (ops->adjust_regnum == NULL) |
4bf8967c | 838 | return regnum; |
4fc771b8 | 839 | return ops->adjust_regnum (gdbarch, regnum, eh_frame_p); |
4bf8967c | 840 | } |
303b6f5d DJ |
841 | |
842 | static void | |
843 | dwarf2_frame_find_quirks (struct dwarf2_frame_state *fs, | |
844 | struct dwarf2_fde *fde) | |
845 | { | |
303b6f5d DJ |
846 | struct symtab *s; |
847 | ||
848 | s = find_pc_symtab (fs->pc); | |
a6c727b2 | 849 | if (s == NULL) |
303b6f5d DJ |
850 | return; |
851 | ||
a6c727b2 DJ |
852 | if (producer_is_realview (s->producer)) |
853 | { | |
854 | if (fde->cie->version == 1) | |
855 | fs->armcc_cfa_offsets_sf = 1; | |
856 | ||
857 | if (fde->cie->version == 1) | |
858 | fs->armcc_cfa_offsets_reversed = 1; | |
859 | ||
860 | /* The reversed offset problem is present in some compilers | |
861 | using DWARF3, but it was eventually fixed. Check the ARM | |
862 | defined augmentations, which are in the format "armcc" followed | |
863 | by a list of one-character options. The "+" option means | |
864 | this problem is fixed (no quirk needed). If the armcc | |
865 | augmentation is missing, the quirk is needed. */ | |
866 | if (fde->cie->version == 3 | |
867 | && (strncmp (fde->cie->augmentation, "armcc", 5) != 0 | |
868 | || strchr (fde->cie->augmentation + 5, '+') == NULL)) | |
869 | fs->armcc_cfa_offsets_reversed = 1; | |
870 | ||
871 | return; | |
872 | } | |
303b6f5d | 873 | } |
8f22cb90 MK |
874 | \f |
875 | ||
876 | struct dwarf2_frame_cache | |
877 | { | |
878 | /* DWARF Call Frame Address. */ | |
879 | CORE_ADDR cfa; | |
880 | ||
0228dfb9 DJ |
881 | /* Set if the return address column was marked as undefined. */ |
882 | int undefined_retaddr; | |
883 | ||
8f22cb90 MK |
884 | /* Saved registers, indexed by GDB register number, not by DWARF |
885 | register number. */ | |
886 | struct dwarf2_frame_state_reg *reg; | |
8d5a9abc MK |
887 | |
888 | /* Return address register. */ | |
889 | struct dwarf2_frame_state_reg retaddr_reg; | |
ae0d2f24 UW |
890 | |
891 | /* Target address size in bytes. */ | |
892 | int addr_size; | |
8f22cb90 | 893 | }; |
05cbe71a | 894 | |
b9362cc7 | 895 | static struct dwarf2_frame_cache * |
4a4e5149 | 896 | dwarf2_frame_cache (struct frame_info *this_frame, void **this_cache) |
cfc14b3a MK |
897 | { |
898 | struct cleanup *old_chain; | |
4a4e5149 | 899 | struct gdbarch *gdbarch = get_frame_arch (this_frame); |
ad010def UW |
900 | const int num_regs = gdbarch_num_regs (gdbarch) |
901 | + gdbarch_num_pseudo_regs (gdbarch); | |
cfc14b3a MK |
902 | struct dwarf2_frame_cache *cache; |
903 | struct dwarf2_frame_state *fs; | |
904 | struct dwarf2_fde *fde; | |
cfc14b3a MK |
905 | |
906 | if (*this_cache) | |
907 | return *this_cache; | |
908 | ||
909 | /* Allocate a new cache. */ | |
910 | cache = FRAME_OBSTACK_ZALLOC (struct dwarf2_frame_cache); | |
911 | cache->reg = FRAME_OBSTACK_CALLOC (num_regs, struct dwarf2_frame_state_reg); | |
912 | ||
913 | /* Allocate and initialize the frame state. */ | |
914 | fs = XMALLOC (struct dwarf2_frame_state); | |
915 | memset (fs, 0, sizeof (struct dwarf2_frame_state)); | |
916 | old_chain = make_cleanup (dwarf2_frame_state_free, fs); | |
917 | ||
918 | /* Unwind the PC. | |
919 | ||
4a4e5149 | 920 | Note that if the next frame is never supposed to return (i.e. a call |
cfc14b3a | 921 | to abort), the compiler might optimize away the instruction at |
4a4e5149 | 922 | its return address. As a result the return address will |
cfc14b3a | 923 | point at some random instruction, and the CFI for that |
e4e9607c | 924 | instruction is probably worthless to us. GCC's unwinder solves |
cfc14b3a MK |
925 | this problem by substracting 1 from the return address to get an |
926 | address in the middle of a presumed call instruction (or the | |
927 | instruction in the associated delay slot). This should only be | |
928 | done for "normal" frames and not for resume-type frames (signal | |
e4e9607c | 929 | handlers, sentinel frames, dummy frames). The function |
ad1193e7 | 930 | get_frame_address_in_block does just this. It's not clear how |
e4e9607c MK |
931 | reliable the method is though; there is the potential for the |
932 | register state pre-call being different to that on return. */ | |
4a4e5149 | 933 | fs->pc = get_frame_address_in_block (this_frame); |
cfc14b3a MK |
934 | |
935 | /* Find the correct FDE. */ | |
936 | fde = dwarf2_frame_find_fde (&fs->pc); | |
937 | gdb_assert (fde != NULL); | |
938 | ||
939 | /* Extract any interesting information from the CIE. */ | |
940 | fs->data_align = fde->cie->data_alignment_factor; | |
941 | fs->code_align = fde->cie->code_alignment_factor; | |
942 | fs->retaddr_column = fde->cie->return_address_register; | |
ae0d2f24 | 943 | cache->addr_size = fde->cie->addr_size; |
cfc14b3a | 944 | |
303b6f5d DJ |
945 | /* Check for "quirks" - known bugs in producers. */ |
946 | dwarf2_frame_find_quirks (fs, fde); | |
947 | ||
cfc14b3a | 948 | /* First decode all the insns in the CIE. */ |
ae0d2f24 | 949 | execute_cfa_program (fde, fde->cie->initial_instructions, |
4a4e5149 | 950 | fde->cie->end, this_frame, fs); |
cfc14b3a MK |
951 | |
952 | /* Save the initialized register set. */ | |
953 | fs->initial = fs->regs; | |
954 | fs->initial.reg = dwarf2_frame_state_copy_regs (&fs->regs); | |
955 | ||
956 | /* Then decode the insns in the FDE up to our target PC. */ | |
4a4e5149 | 957 | execute_cfa_program (fde, fde->instructions, fde->end, this_frame, fs); |
cfc14b3a | 958 | |
938f5214 | 959 | /* Calculate the CFA. */ |
2fd481e1 | 960 | switch (fs->regs.cfa_how) |
cfc14b3a MK |
961 | { |
962 | case CFA_REG_OFFSET: | |
2fd481e1 | 963 | cache->cfa = read_reg (this_frame, fs->regs.cfa_reg); |
303b6f5d | 964 | if (fs->armcc_cfa_offsets_reversed) |
2fd481e1 | 965 | cache->cfa -= fs->regs.cfa_offset; |
303b6f5d | 966 | else |
2fd481e1 | 967 | cache->cfa += fs->regs.cfa_offset; |
cfc14b3a MK |
968 | break; |
969 | ||
970 | case CFA_EXP: | |
971 | cache->cfa = | |
2fd481e1 | 972 | execute_stack_op (fs->regs.cfa_exp, fs->regs.cfa_exp_len, |
44353522 | 973 | cache->addr_size, this_frame, 0, 0); |
cfc14b3a MK |
974 | break; |
975 | ||
976 | default: | |
e2e0b3e5 | 977 | internal_error (__FILE__, __LINE__, _("Unknown CFA rule.")); |
cfc14b3a MK |
978 | } |
979 | ||
05cbe71a | 980 | /* Initialize the register state. */ |
3e2c4033 AC |
981 | { |
982 | int regnum; | |
e4e9607c | 983 | |
3e2c4033 | 984 | for (regnum = 0; regnum < num_regs; regnum++) |
4a4e5149 | 985 | dwarf2_frame_init_reg (gdbarch, regnum, &cache->reg[regnum], this_frame); |
3e2c4033 AC |
986 | } |
987 | ||
988 | /* Go through the DWARF2 CFI generated table and save its register | |
79c4cb80 MK |
989 | location information in the cache. Note that we don't skip the |
990 | return address column; it's perfectly all right for it to | |
991 | correspond to a real register. If it doesn't correspond to a | |
992 | real register, or if we shouldn't treat it as such, | |
055d23b8 | 993 | gdbarch_dwarf2_reg_to_regnum should be defined to return a number outside |
f57d151a | 994 | the range [0, gdbarch_num_regs). */ |
3e2c4033 AC |
995 | { |
996 | int column; /* CFI speak for "register number". */ | |
e4e9607c | 997 | |
3e2c4033 AC |
998 | for (column = 0; column < fs->regs.num_regs; column++) |
999 | { | |
3e2c4033 | 1000 | /* Use the GDB register number as the destination index. */ |
ad010def | 1001 | int regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, column); |
3e2c4033 AC |
1002 | |
1003 | /* If there's no corresponding GDB register, ignore it. */ | |
1004 | if (regnum < 0 || regnum >= num_regs) | |
1005 | continue; | |
1006 | ||
1007 | /* NOTE: cagney/2003-09-05: CFI should specify the disposition | |
e4e9607c MK |
1008 | of all debug info registers. If it doesn't, complain (but |
1009 | not too loudly). It turns out that GCC assumes that an | |
3e2c4033 AC |
1010 | unspecified register implies "same value" when CFI (draft |
1011 | 7) specifies nothing at all. Such a register could equally | |
1012 | be interpreted as "undefined". Also note that this check | |
e4e9607c MK |
1013 | isn't sufficient; it only checks that all registers in the |
1014 | range [0 .. max column] are specified, and won't detect | |
3e2c4033 | 1015 | problems when a debug info register falls outside of the |
e4e9607c | 1016 | table. We need a way of iterating through all the valid |
3e2c4033 | 1017 | DWARF2 register numbers. */ |
05cbe71a | 1018 | if (fs->regs.reg[column].how == DWARF2_FRAME_REG_UNSPECIFIED) |
f059bf6f AC |
1019 | { |
1020 | if (cache->reg[regnum].how == DWARF2_FRAME_REG_UNSPECIFIED) | |
e2e0b3e5 | 1021 | complaint (&symfile_complaints, _("\ |
5af949e3 | 1022 | incomplete CFI data; unspecified registers (e.g., %s) at %s"), |
f059bf6f | 1023 | gdbarch_register_name (gdbarch, regnum), |
5af949e3 | 1024 | paddress (gdbarch, fs->pc)); |
f059bf6f | 1025 | } |
35889917 MK |
1026 | else |
1027 | cache->reg[regnum] = fs->regs.reg[column]; | |
3e2c4033 AC |
1028 | } |
1029 | } | |
cfc14b3a | 1030 | |
8d5a9abc MK |
1031 | /* Eliminate any DWARF2_FRAME_REG_RA rules, and save the information |
1032 | we need for evaluating DWARF2_FRAME_REG_RA_OFFSET rules. */ | |
35889917 MK |
1033 | { |
1034 | int regnum; | |
1035 | ||
1036 | for (regnum = 0; regnum < num_regs; regnum++) | |
1037 | { | |
8d5a9abc MK |
1038 | if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA |
1039 | || cache->reg[regnum].how == DWARF2_FRAME_REG_RA_OFFSET) | |
35889917 | 1040 | { |
05cbe71a MK |
1041 | struct dwarf2_frame_state_reg *retaddr_reg = |
1042 | &fs->regs.reg[fs->retaddr_column]; | |
1043 | ||
d4f10bf2 MK |
1044 | /* It seems rather bizarre to specify an "empty" column as |
1045 | the return adress column. However, this is exactly | |
1046 | what GCC does on some targets. It turns out that GCC | |
1047 | assumes that the return address can be found in the | |
1048 | register corresponding to the return address column. | |
8d5a9abc MK |
1049 | Incidentally, that's how we should treat a return |
1050 | address column specifying "same value" too. */ | |
d4f10bf2 | 1051 | if (fs->retaddr_column < fs->regs.num_regs |
05cbe71a MK |
1052 | && retaddr_reg->how != DWARF2_FRAME_REG_UNSPECIFIED |
1053 | && retaddr_reg->how != DWARF2_FRAME_REG_SAME_VALUE) | |
8d5a9abc MK |
1054 | { |
1055 | if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA) | |
1056 | cache->reg[regnum] = *retaddr_reg; | |
1057 | else | |
1058 | cache->retaddr_reg = *retaddr_reg; | |
1059 | } | |
35889917 MK |
1060 | else |
1061 | { | |
8d5a9abc MK |
1062 | if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA) |
1063 | { | |
1064 | cache->reg[regnum].loc.reg = fs->retaddr_column; | |
1065 | cache->reg[regnum].how = DWARF2_FRAME_REG_SAVED_REG; | |
1066 | } | |
1067 | else | |
1068 | { | |
1069 | cache->retaddr_reg.loc.reg = fs->retaddr_column; | |
1070 | cache->retaddr_reg.how = DWARF2_FRAME_REG_SAVED_REG; | |
1071 | } | |
35889917 MK |
1072 | } |
1073 | } | |
1074 | } | |
1075 | } | |
cfc14b3a | 1076 | |
0228dfb9 DJ |
1077 | if (fs->retaddr_column < fs->regs.num_regs |
1078 | && fs->regs.reg[fs->retaddr_column].how == DWARF2_FRAME_REG_UNDEFINED) | |
1079 | cache->undefined_retaddr = 1; | |
1080 | ||
cfc14b3a MK |
1081 | do_cleanups (old_chain); |
1082 | ||
1083 | *this_cache = cache; | |
1084 | return cache; | |
1085 | } | |
1086 | ||
1087 | static void | |
4a4e5149 | 1088 | dwarf2_frame_this_id (struct frame_info *this_frame, void **this_cache, |
cfc14b3a MK |
1089 | struct frame_id *this_id) |
1090 | { | |
1091 | struct dwarf2_frame_cache *cache = | |
4a4e5149 | 1092 | dwarf2_frame_cache (this_frame, this_cache); |
cfc14b3a | 1093 | |
0228dfb9 DJ |
1094 | if (cache->undefined_retaddr) |
1095 | return; | |
1096 | ||
4a4e5149 | 1097 | (*this_id) = frame_id_build (cache->cfa, get_frame_func (this_frame)); |
93d42b30 DJ |
1098 | } |
1099 | ||
4a4e5149 DJ |
1100 | static struct value * |
1101 | dwarf2_frame_prev_register (struct frame_info *this_frame, void **this_cache, | |
1102 | int regnum) | |
93d42b30 | 1103 | { |
4a4e5149 | 1104 | struct gdbarch *gdbarch = get_frame_arch (this_frame); |
93d42b30 | 1105 | struct dwarf2_frame_cache *cache = |
4a4e5149 DJ |
1106 | dwarf2_frame_cache (this_frame, this_cache); |
1107 | CORE_ADDR addr; | |
1108 | int realnum; | |
cfc14b3a MK |
1109 | |
1110 | switch (cache->reg[regnum].how) | |
1111 | { | |
05cbe71a | 1112 | case DWARF2_FRAME_REG_UNDEFINED: |
3e2c4033 | 1113 | /* If CFI explicitly specified that the value isn't defined, |
e4e9607c | 1114 | mark it as optimized away; the value isn't available. */ |
4a4e5149 | 1115 | return frame_unwind_got_optimized (this_frame, regnum); |
cfc14b3a | 1116 | |
05cbe71a | 1117 | case DWARF2_FRAME_REG_SAVED_OFFSET: |
4a4e5149 DJ |
1118 | addr = cache->cfa + cache->reg[regnum].loc.offset; |
1119 | return frame_unwind_got_memory (this_frame, regnum, addr); | |
cfc14b3a | 1120 | |
05cbe71a | 1121 | case DWARF2_FRAME_REG_SAVED_REG: |
4a4e5149 DJ |
1122 | realnum |
1123 | = gdbarch_dwarf2_reg_to_regnum (gdbarch, cache->reg[regnum].loc.reg); | |
1124 | return frame_unwind_got_register (this_frame, regnum, realnum); | |
cfc14b3a | 1125 | |
05cbe71a | 1126 | case DWARF2_FRAME_REG_SAVED_EXP: |
4a4e5149 DJ |
1127 | addr = execute_stack_op (cache->reg[regnum].loc.exp, |
1128 | cache->reg[regnum].exp_len, | |
44353522 | 1129 | cache->addr_size, this_frame, cache->cfa, 1); |
4a4e5149 | 1130 | return frame_unwind_got_memory (this_frame, regnum, addr); |
cfc14b3a | 1131 | |
46ea248b | 1132 | case DWARF2_FRAME_REG_SAVED_VAL_OFFSET: |
4a4e5149 DJ |
1133 | addr = cache->cfa + cache->reg[regnum].loc.offset; |
1134 | return frame_unwind_got_constant (this_frame, regnum, addr); | |
46ea248b AO |
1135 | |
1136 | case DWARF2_FRAME_REG_SAVED_VAL_EXP: | |
4a4e5149 DJ |
1137 | addr = execute_stack_op (cache->reg[regnum].loc.exp, |
1138 | cache->reg[regnum].exp_len, | |
44353522 | 1139 | cache->addr_size, this_frame, cache->cfa, 1); |
4a4e5149 | 1140 | return frame_unwind_got_constant (this_frame, regnum, addr); |
46ea248b | 1141 | |
05cbe71a | 1142 | case DWARF2_FRAME_REG_UNSPECIFIED: |
3e2c4033 AC |
1143 | /* GCC, in its infinite wisdom decided to not provide unwind |
1144 | information for registers that are "same value". Since | |
1145 | DWARF2 (3 draft 7) doesn't define such behavior, said | |
1146 | registers are actually undefined (which is different to CFI | |
1147 | "undefined"). Code above issues a complaint about this. | |
1148 | Here just fudge the books, assume GCC, and that the value is | |
1149 | more inner on the stack. */ | |
4a4e5149 | 1150 | return frame_unwind_got_register (this_frame, regnum, regnum); |
3e2c4033 | 1151 | |
05cbe71a | 1152 | case DWARF2_FRAME_REG_SAME_VALUE: |
4a4e5149 | 1153 | return frame_unwind_got_register (this_frame, regnum, regnum); |
cfc14b3a | 1154 | |
05cbe71a | 1155 | case DWARF2_FRAME_REG_CFA: |
4a4e5149 | 1156 | return frame_unwind_got_address (this_frame, regnum, cache->cfa); |
35889917 | 1157 | |
ea7963f0 | 1158 | case DWARF2_FRAME_REG_CFA_OFFSET: |
4a4e5149 DJ |
1159 | addr = cache->cfa + cache->reg[regnum].loc.offset; |
1160 | return frame_unwind_got_address (this_frame, regnum, addr); | |
ea7963f0 | 1161 | |
8d5a9abc | 1162 | case DWARF2_FRAME_REG_RA_OFFSET: |
4a4e5149 DJ |
1163 | addr = cache->reg[regnum].loc.offset; |
1164 | regnum = gdbarch_dwarf2_reg_to_regnum | |
1165 | (gdbarch, cache->retaddr_reg.loc.reg); | |
1166 | addr += get_frame_register_unsigned (this_frame, regnum); | |
1167 | return frame_unwind_got_address (this_frame, regnum, addr); | |
8d5a9abc | 1168 | |
b39cc962 DJ |
1169 | case DWARF2_FRAME_REG_FN: |
1170 | return cache->reg[regnum].loc.fn (this_frame, this_cache, regnum); | |
1171 | ||
cfc14b3a | 1172 | default: |
e2e0b3e5 | 1173 | internal_error (__FILE__, __LINE__, _("Unknown register rule.")); |
cfc14b3a MK |
1174 | } |
1175 | } | |
1176 | ||
4a4e5149 DJ |
1177 | static int |
1178 | dwarf2_frame_sniffer (const struct frame_unwind *self, | |
1179 | struct frame_info *this_frame, void **this_cache) | |
cfc14b3a | 1180 | { |
1ce5d6dd | 1181 | /* Grab an address that is guarenteed to reside somewhere within the |
4a4e5149 | 1182 | function. get_frame_pc(), with a no-return next function, can |
93d42b30 DJ |
1183 | end up returning something past the end of this function's body. |
1184 | If the frame we're sniffing for is a signal frame whose start | |
1185 | address is placed on the stack by the OS, its FDE must | |
4a4e5149 DJ |
1186 | extend one byte before its start address or we could potentially |
1187 | select the FDE of the previous function. */ | |
1188 | CORE_ADDR block_addr = get_frame_address_in_block (this_frame); | |
56c987f6 | 1189 | struct dwarf2_fde *fde = dwarf2_frame_find_fde (&block_addr); |
9a619af0 | 1190 | |
56c987f6 | 1191 | if (!fde) |
4a4e5149 | 1192 | return 0; |
3ed09a32 DJ |
1193 | |
1194 | /* On some targets, signal trampolines may have unwind information. | |
1195 | We need to recognize them so that we set the frame type | |
1196 | correctly. */ | |
1197 | ||
56c987f6 | 1198 | if (fde->cie->signal_frame |
4a4e5149 DJ |
1199 | || dwarf2_frame_signal_frame_p (get_frame_arch (this_frame), |
1200 | this_frame)) | |
1201 | return self->type == SIGTRAMP_FRAME; | |
1202 | ||
1203 | return self->type != SIGTRAMP_FRAME; | |
1204 | } | |
1205 | ||
1206 | static const struct frame_unwind dwarf2_frame_unwind = | |
1207 | { | |
1208 | NORMAL_FRAME, | |
1209 | dwarf2_frame_this_id, | |
1210 | dwarf2_frame_prev_register, | |
1211 | NULL, | |
1212 | dwarf2_frame_sniffer | |
1213 | }; | |
1214 | ||
1215 | static const struct frame_unwind dwarf2_signal_frame_unwind = | |
1216 | { | |
1217 | SIGTRAMP_FRAME, | |
1218 | dwarf2_frame_this_id, | |
1219 | dwarf2_frame_prev_register, | |
1220 | NULL, | |
1221 | dwarf2_frame_sniffer | |
1222 | }; | |
cfc14b3a | 1223 | |
4a4e5149 DJ |
1224 | /* Append the DWARF-2 frame unwinders to GDBARCH's list. */ |
1225 | ||
1226 | void | |
1227 | dwarf2_append_unwinders (struct gdbarch *gdbarch) | |
1228 | { | |
1229 | frame_unwind_append_unwinder (gdbarch, &dwarf2_frame_unwind); | |
1230 | frame_unwind_append_unwinder (gdbarch, &dwarf2_signal_frame_unwind); | |
cfc14b3a MK |
1231 | } |
1232 | \f | |
1233 | ||
1234 | /* There is no explicitly defined relationship between the CFA and the | |
1235 | location of frame's local variables and arguments/parameters. | |
1236 | Therefore, frame base methods on this page should probably only be | |
1237 | used as a last resort, just to avoid printing total garbage as a | |
1238 | response to the "info frame" command. */ | |
1239 | ||
1240 | static CORE_ADDR | |
4a4e5149 | 1241 | dwarf2_frame_base_address (struct frame_info *this_frame, void **this_cache) |
cfc14b3a MK |
1242 | { |
1243 | struct dwarf2_frame_cache *cache = | |
4a4e5149 | 1244 | dwarf2_frame_cache (this_frame, this_cache); |
cfc14b3a MK |
1245 | |
1246 | return cache->cfa; | |
1247 | } | |
1248 | ||
1249 | static const struct frame_base dwarf2_frame_base = | |
1250 | { | |
1251 | &dwarf2_frame_unwind, | |
1252 | dwarf2_frame_base_address, | |
1253 | dwarf2_frame_base_address, | |
1254 | dwarf2_frame_base_address | |
1255 | }; | |
1256 | ||
1257 | const struct frame_base * | |
4a4e5149 | 1258 | dwarf2_frame_base_sniffer (struct frame_info *this_frame) |
cfc14b3a | 1259 | { |
4a4e5149 | 1260 | CORE_ADDR block_addr = get_frame_address_in_block (this_frame); |
9a619af0 | 1261 | |
93d42b30 | 1262 | if (dwarf2_frame_find_fde (&block_addr)) |
cfc14b3a MK |
1263 | return &dwarf2_frame_base; |
1264 | ||
1265 | return NULL; | |
1266 | } | |
e7802207 TT |
1267 | |
1268 | /* Compute the CFA for THIS_FRAME, but only if THIS_FRAME came from | |
1269 | the DWARF unwinder. This is used to implement | |
1270 | DW_OP_call_frame_cfa. */ | |
1271 | ||
1272 | CORE_ADDR | |
1273 | dwarf2_frame_cfa (struct frame_info *this_frame) | |
1274 | { | |
1275 | while (get_frame_type (this_frame) == INLINE_FRAME) | |
1276 | this_frame = get_prev_frame (this_frame); | |
1277 | /* This restriction could be lifted if other unwinders are known to | |
1278 | compute the frame base in a way compatible with the DWARF | |
1279 | unwinder. */ | |
1280 | if (! frame_unwinder_is (this_frame, &dwarf2_frame_unwind)) | |
1281 | error (_("can't compute CFA for this frame")); | |
1282 | return get_frame_base (this_frame); | |
1283 | } | |
cfc14b3a | 1284 | \f |
8f22cb90 | 1285 | const struct objfile_data *dwarf2_frame_objfile_data; |
0d0e1a63 | 1286 | |
cfc14b3a | 1287 | static unsigned int |
852483bc | 1288 | read_1_byte (bfd *abfd, gdb_byte *buf) |
cfc14b3a | 1289 | { |
852483bc | 1290 | return bfd_get_8 (abfd, buf); |
cfc14b3a MK |
1291 | } |
1292 | ||
1293 | static unsigned int | |
852483bc | 1294 | read_4_bytes (bfd *abfd, gdb_byte *buf) |
cfc14b3a | 1295 | { |
852483bc | 1296 | return bfd_get_32 (abfd, buf); |
cfc14b3a MK |
1297 | } |
1298 | ||
1299 | static ULONGEST | |
852483bc | 1300 | read_8_bytes (bfd *abfd, gdb_byte *buf) |
cfc14b3a | 1301 | { |
852483bc | 1302 | return bfd_get_64 (abfd, buf); |
cfc14b3a MK |
1303 | } |
1304 | ||
1305 | static ULONGEST | |
852483bc | 1306 | read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr) |
cfc14b3a MK |
1307 | { |
1308 | ULONGEST result; | |
1309 | unsigned int num_read; | |
1310 | int shift; | |
852483bc | 1311 | gdb_byte byte; |
cfc14b3a MK |
1312 | |
1313 | result = 0; | |
1314 | shift = 0; | |
1315 | num_read = 0; | |
1316 | ||
1317 | do | |
1318 | { | |
1319 | byte = bfd_get_8 (abfd, (bfd_byte *) buf); | |
1320 | buf++; | |
1321 | num_read++; | |
1322 | result |= ((byte & 0x7f) << shift); | |
1323 | shift += 7; | |
1324 | } | |
1325 | while (byte & 0x80); | |
1326 | ||
1327 | *bytes_read_ptr = num_read; | |
1328 | ||
1329 | return result; | |
1330 | } | |
1331 | ||
1332 | static LONGEST | |
852483bc | 1333 | read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr) |
cfc14b3a MK |
1334 | { |
1335 | LONGEST result; | |
1336 | int shift; | |
1337 | unsigned int num_read; | |
852483bc | 1338 | gdb_byte byte; |
cfc14b3a MK |
1339 | |
1340 | result = 0; | |
1341 | shift = 0; | |
1342 | num_read = 0; | |
1343 | ||
1344 | do | |
1345 | { | |
1346 | byte = bfd_get_8 (abfd, (bfd_byte *) buf); | |
1347 | buf++; | |
1348 | num_read++; | |
1349 | result |= ((byte & 0x7f) << shift); | |
1350 | shift += 7; | |
1351 | } | |
1352 | while (byte & 0x80); | |
1353 | ||
77e0b926 DJ |
1354 | if (shift < 8 * sizeof (result) && (byte & 0x40)) |
1355 | result |= -(((LONGEST)1) << shift); | |
cfc14b3a MK |
1356 | |
1357 | *bytes_read_ptr = num_read; | |
1358 | ||
1359 | return result; | |
1360 | } | |
1361 | ||
1362 | static ULONGEST | |
852483bc | 1363 | read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr) |
cfc14b3a MK |
1364 | { |
1365 | LONGEST result; | |
1366 | ||
852483bc | 1367 | result = bfd_get_32 (abfd, buf); |
cfc14b3a MK |
1368 | if (result == 0xffffffff) |
1369 | { | |
852483bc | 1370 | result = bfd_get_64 (abfd, buf + 4); |
cfc14b3a MK |
1371 | *bytes_read_ptr = 12; |
1372 | } | |
1373 | else | |
1374 | *bytes_read_ptr = 4; | |
1375 | ||
1376 | return result; | |
1377 | } | |
1378 | \f | |
1379 | ||
1380 | /* Pointer encoding helper functions. */ | |
1381 | ||
1382 | /* GCC supports exception handling based on DWARF2 CFI. However, for | |
1383 | technical reasons, it encodes addresses in its FDE's in a different | |
1384 | way. Several "pointer encodings" are supported. The encoding | |
1385 | that's used for a particular FDE is determined by the 'R' | |
1386 | augmentation in the associated CIE. The argument of this | |
1387 | augmentation is a single byte. | |
1388 | ||
1389 | The address can be encoded as 2 bytes, 4 bytes, 8 bytes, or as a | |
1390 | LEB128. This is encoded in bits 0, 1 and 2. Bit 3 encodes whether | |
1391 | the address is signed or unsigned. Bits 4, 5 and 6 encode how the | |
1392 | address should be interpreted (absolute, relative to the current | |
1393 | position in the FDE, ...). Bit 7, indicates that the address | |
1394 | should be dereferenced. */ | |
1395 | ||
852483bc | 1396 | static gdb_byte |
cfc14b3a MK |
1397 | encoding_for_size (unsigned int size) |
1398 | { | |
1399 | switch (size) | |
1400 | { | |
1401 | case 2: | |
1402 | return DW_EH_PE_udata2; | |
1403 | case 4: | |
1404 | return DW_EH_PE_udata4; | |
1405 | case 8: | |
1406 | return DW_EH_PE_udata8; | |
1407 | default: | |
e2e0b3e5 | 1408 | internal_error (__FILE__, __LINE__, _("Unsupported address size")); |
cfc14b3a MK |
1409 | } |
1410 | } | |
1411 | ||
cfc14b3a | 1412 | static CORE_ADDR |
852483bc | 1413 | read_encoded_value (struct comp_unit *unit, gdb_byte encoding, |
ae0d2f24 UW |
1414 | int ptr_len, gdb_byte *buf, unsigned int *bytes_read_ptr, |
1415 | CORE_ADDR func_base) | |
cfc14b3a | 1416 | { |
68f6cf99 | 1417 | ptrdiff_t offset; |
cfc14b3a MK |
1418 | CORE_ADDR base; |
1419 | ||
1420 | /* GCC currently doesn't generate DW_EH_PE_indirect encodings for | |
1421 | FDE's. */ | |
1422 | if (encoding & DW_EH_PE_indirect) | |
1423 | internal_error (__FILE__, __LINE__, | |
e2e0b3e5 | 1424 | _("Unsupported encoding: DW_EH_PE_indirect")); |
cfc14b3a | 1425 | |
68f6cf99 MK |
1426 | *bytes_read_ptr = 0; |
1427 | ||
cfc14b3a MK |
1428 | switch (encoding & 0x70) |
1429 | { | |
1430 | case DW_EH_PE_absptr: | |
1431 | base = 0; | |
1432 | break; | |
1433 | case DW_EH_PE_pcrel: | |
f2fec864 | 1434 | base = bfd_get_section_vma (unit->abfd, unit->dwarf_frame_section); |
852483bc | 1435 | base += (buf - unit->dwarf_frame_buffer); |
cfc14b3a | 1436 | break; |
0912c7f2 MK |
1437 | case DW_EH_PE_datarel: |
1438 | base = unit->dbase; | |
1439 | break; | |
0fd85043 CV |
1440 | case DW_EH_PE_textrel: |
1441 | base = unit->tbase; | |
1442 | break; | |
03ac2a74 | 1443 | case DW_EH_PE_funcrel: |
ae0d2f24 | 1444 | base = func_base; |
03ac2a74 | 1445 | break; |
68f6cf99 MK |
1446 | case DW_EH_PE_aligned: |
1447 | base = 0; | |
852483bc | 1448 | offset = buf - unit->dwarf_frame_buffer; |
68f6cf99 MK |
1449 | if ((offset % ptr_len) != 0) |
1450 | { | |
1451 | *bytes_read_ptr = ptr_len - (offset % ptr_len); | |
1452 | buf += *bytes_read_ptr; | |
1453 | } | |
1454 | break; | |
cfc14b3a | 1455 | default: |
e2e0b3e5 | 1456 | internal_error (__FILE__, __LINE__, _("Invalid or unsupported encoding")); |
cfc14b3a MK |
1457 | } |
1458 | ||
b04de778 | 1459 | if ((encoding & 0x07) == 0x00) |
f2fec864 DJ |
1460 | { |
1461 | encoding |= encoding_for_size (ptr_len); | |
1462 | if (bfd_get_sign_extend_vma (unit->abfd)) | |
1463 | encoding |= DW_EH_PE_signed; | |
1464 | } | |
cfc14b3a MK |
1465 | |
1466 | switch (encoding & 0x0f) | |
1467 | { | |
a81b10ae MK |
1468 | case DW_EH_PE_uleb128: |
1469 | { | |
1470 | ULONGEST value; | |
852483bc | 1471 | gdb_byte *end_buf = buf + (sizeof (value) + 1) * 8 / 7; |
9a619af0 | 1472 | |
a7289609 | 1473 | *bytes_read_ptr += read_uleb128 (buf, end_buf, &value) - buf; |
a81b10ae MK |
1474 | return base + value; |
1475 | } | |
cfc14b3a | 1476 | case DW_EH_PE_udata2: |
68f6cf99 | 1477 | *bytes_read_ptr += 2; |
cfc14b3a MK |
1478 | return (base + bfd_get_16 (unit->abfd, (bfd_byte *) buf)); |
1479 | case DW_EH_PE_udata4: | |
68f6cf99 | 1480 | *bytes_read_ptr += 4; |
cfc14b3a MK |
1481 | return (base + bfd_get_32 (unit->abfd, (bfd_byte *) buf)); |
1482 | case DW_EH_PE_udata8: | |
68f6cf99 | 1483 | *bytes_read_ptr += 8; |
cfc14b3a | 1484 | return (base + bfd_get_64 (unit->abfd, (bfd_byte *) buf)); |
a81b10ae MK |
1485 | case DW_EH_PE_sleb128: |
1486 | { | |
1487 | LONGEST value; | |
852483bc | 1488 | gdb_byte *end_buf = buf + (sizeof (value) + 1) * 8 / 7; |
9a619af0 | 1489 | |
a7289609 | 1490 | *bytes_read_ptr += read_sleb128 (buf, end_buf, &value) - buf; |
a81b10ae MK |
1491 | return base + value; |
1492 | } | |
cfc14b3a | 1493 | case DW_EH_PE_sdata2: |
68f6cf99 | 1494 | *bytes_read_ptr += 2; |
cfc14b3a MK |
1495 | return (base + bfd_get_signed_16 (unit->abfd, (bfd_byte *) buf)); |
1496 | case DW_EH_PE_sdata4: | |
68f6cf99 | 1497 | *bytes_read_ptr += 4; |
cfc14b3a MK |
1498 | return (base + bfd_get_signed_32 (unit->abfd, (bfd_byte *) buf)); |
1499 | case DW_EH_PE_sdata8: | |
68f6cf99 | 1500 | *bytes_read_ptr += 8; |
cfc14b3a MK |
1501 | return (base + bfd_get_signed_64 (unit->abfd, (bfd_byte *) buf)); |
1502 | default: | |
e2e0b3e5 | 1503 | internal_error (__FILE__, __LINE__, _("Invalid or unsupported encoding")); |
cfc14b3a MK |
1504 | } |
1505 | } | |
1506 | \f | |
1507 | ||
b01c8410 PP |
1508 | static int |
1509 | bsearch_cie_cmp (const void *key, const void *element) | |
cfc14b3a | 1510 | { |
b01c8410 PP |
1511 | ULONGEST cie_pointer = *(ULONGEST *) key; |
1512 | struct dwarf2_cie *cie = *(struct dwarf2_cie **) element; | |
cfc14b3a | 1513 | |
b01c8410 PP |
1514 | if (cie_pointer == cie->cie_pointer) |
1515 | return 0; | |
cfc14b3a | 1516 | |
b01c8410 PP |
1517 | return (cie_pointer < cie->cie_pointer) ? -1 : 1; |
1518 | } | |
1519 | ||
1520 | /* Find CIE with the given CIE_POINTER in CIE_TABLE. */ | |
1521 | static struct dwarf2_cie * | |
1522 | find_cie (struct dwarf2_cie_table *cie_table, ULONGEST cie_pointer) | |
1523 | { | |
1524 | struct dwarf2_cie **p_cie; | |
cfc14b3a | 1525 | |
65a97ab3 PP |
1526 | /* The C standard (ISO/IEC 9899:TC2) requires the BASE argument to |
1527 | bsearch be non-NULL. */ | |
1528 | if (cie_table->entries == NULL) | |
1529 | { | |
1530 | gdb_assert (cie_table->num_entries == 0); | |
1531 | return NULL; | |
1532 | } | |
1533 | ||
b01c8410 PP |
1534 | p_cie = bsearch (&cie_pointer, cie_table->entries, cie_table->num_entries, |
1535 | sizeof (cie_table->entries[0]), bsearch_cie_cmp); | |
1536 | if (p_cie != NULL) | |
1537 | return *p_cie; | |
cfc14b3a MK |
1538 | return NULL; |
1539 | } | |
1540 | ||
b01c8410 | 1541 | /* Add a pointer to new CIE to the CIE_TABLE, allocating space for it. */ |
cfc14b3a | 1542 | static void |
b01c8410 | 1543 | add_cie (struct dwarf2_cie_table *cie_table, struct dwarf2_cie *cie) |
cfc14b3a | 1544 | { |
b01c8410 PP |
1545 | const int n = cie_table->num_entries; |
1546 | ||
1547 | gdb_assert (n < 1 | |
1548 | || cie_table->entries[n - 1]->cie_pointer < cie->cie_pointer); | |
1549 | ||
1550 | cie_table->entries = | |
1551 | xrealloc (cie_table->entries, (n + 1) * sizeof (cie_table->entries[0])); | |
1552 | cie_table->entries[n] = cie; | |
1553 | cie_table->num_entries = n + 1; | |
1554 | } | |
1555 | ||
1556 | static int | |
1557 | bsearch_fde_cmp (const void *key, const void *element) | |
1558 | { | |
1559 | CORE_ADDR seek_pc = *(CORE_ADDR *) key; | |
1560 | struct dwarf2_fde *fde = *(struct dwarf2_fde **) element; | |
9a619af0 | 1561 | |
b01c8410 PP |
1562 | if (seek_pc < fde->initial_location) |
1563 | return -1; | |
1564 | if (seek_pc < fde->initial_location + fde->address_range) | |
1565 | return 0; | |
1566 | return 1; | |
cfc14b3a MK |
1567 | } |
1568 | ||
1569 | /* Find the FDE for *PC. Return a pointer to the FDE, and store the | |
1570 | inital location associated with it into *PC. */ | |
1571 | ||
1572 | static struct dwarf2_fde * | |
1573 | dwarf2_frame_find_fde (CORE_ADDR *pc) | |
1574 | { | |
1575 | struct objfile *objfile; | |
1576 | ||
1577 | ALL_OBJFILES (objfile) | |
1578 | { | |
b01c8410 PP |
1579 | struct dwarf2_fde_table *fde_table; |
1580 | struct dwarf2_fde **p_fde; | |
cfc14b3a | 1581 | CORE_ADDR offset; |
b01c8410 | 1582 | CORE_ADDR seek_pc; |
cfc14b3a | 1583 | |
b01c8410 PP |
1584 | fde_table = objfile_data (objfile, dwarf2_frame_objfile_data); |
1585 | if (fde_table == NULL) | |
be391dca TT |
1586 | { |
1587 | dwarf2_build_frame_info (objfile); | |
1588 | fde_table = objfile_data (objfile, dwarf2_frame_objfile_data); | |
1589 | } | |
1590 | gdb_assert (fde_table != NULL); | |
1591 | ||
1592 | if (fde_table->num_entries == 0) | |
4ae9ee8e DJ |
1593 | continue; |
1594 | ||
1595 | gdb_assert (objfile->section_offsets); | |
1596 | offset = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile)); | |
1597 | ||
b01c8410 PP |
1598 | gdb_assert (fde_table->num_entries > 0); |
1599 | if (*pc < offset + fde_table->entries[0]->initial_location) | |
1600 | continue; | |
1601 | ||
1602 | seek_pc = *pc - offset; | |
1603 | p_fde = bsearch (&seek_pc, fde_table->entries, fde_table->num_entries, | |
1604 | sizeof (fde_table->entries[0]), bsearch_fde_cmp); | |
1605 | if (p_fde != NULL) | |
1606 | { | |
1607 | *pc = (*p_fde)->initial_location + offset; | |
1608 | return *p_fde; | |
1609 | } | |
cfc14b3a | 1610 | } |
cfc14b3a MK |
1611 | return NULL; |
1612 | } | |
1613 | ||
b01c8410 | 1614 | /* Add a pointer to new FDE to the FDE_TABLE, allocating space for it. */ |
cfc14b3a | 1615 | static void |
b01c8410 | 1616 | add_fde (struct dwarf2_fde_table *fde_table, struct dwarf2_fde *fde) |
cfc14b3a | 1617 | { |
b01c8410 PP |
1618 | if (fde->address_range == 0) |
1619 | /* Discard useless FDEs. */ | |
1620 | return; | |
1621 | ||
1622 | fde_table->num_entries += 1; | |
1623 | fde_table->entries = | |
1624 | xrealloc (fde_table->entries, | |
1625 | fde_table->num_entries * sizeof (fde_table->entries[0])); | |
1626 | fde_table->entries[fde_table->num_entries - 1] = fde; | |
cfc14b3a MK |
1627 | } |
1628 | ||
1629 | #ifdef CC_HAS_LONG_LONG | |
1630 | #define DW64_CIE_ID 0xffffffffffffffffULL | |
1631 | #else | |
1632 | #define DW64_CIE_ID ~0 | |
1633 | #endif | |
1634 | ||
852483bc | 1635 | static gdb_byte *decode_frame_entry (struct comp_unit *unit, gdb_byte *start, |
b01c8410 PP |
1636 | int eh_frame_p, |
1637 | struct dwarf2_cie_table *cie_table, | |
1638 | struct dwarf2_fde_table *fde_table); | |
cfc14b3a | 1639 | |
6896c0c7 RH |
1640 | /* Decode the next CIE or FDE. Return NULL if invalid input, otherwise |
1641 | the next byte to be processed. */ | |
852483bc | 1642 | static gdb_byte * |
b01c8410 PP |
1643 | decode_frame_entry_1 (struct comp_unit *unit, gdb_byte *start, int eh_frame_p, |
1644 | struct dwarf2_cie_table *cie_table, | |
1645 | struct dwarf2_fde_table *fde_table) | |
cfc14b3a | 1646 | { |
5e2b427d | 1647 | struct gdbarch *gdbarch = get_objfile_arch (unit->objfile); |
852483bc | 1648 | gdb_byte *buf, *end; |
cfc14b3a MK |
1649 | LONGEST length; |
1650 | unsigned int bytes_read; | |
6896c0c7 RH |
1651 | int dwarf64_p; |
1652 | ULONGEST cie_id; | |
cfc14b3a | 1653 | ULONGEST cie_pointer; |
cfc14b3a | 1654 | |
6896c0c7 | 1655 | buf = start; |
cfc14b3a MK |
1656 | length = read_initial_length (unit->abfd, buf, &bytes_read); |
1657 | buf += bytes_read; | |
1658 | end = buf + length; | |
1659 | ||
6896c0c7 RH |
1660 | /* Are we still within the section? */ |
1661 | if (end > unit->dwarf_frame_buffer + unit->dwarf_frame_size) | |
1662 | return NULL; | |
1663 | ||
cfc14b3a MK |
1664 | if (length == 0) |
1665 | return end; | |
1666 | ||
6896c0c7 RH |
1667 | /* Distinguish between 32 and 64-bit encoded frame info. */ |
1668 | dwarf64_p = (bytes_read == 12); | |
cfc14b3a | 1669 | |
6896c0c7 | 1670 | /* In a .eh_frame section, zero is used to distinguish CIEs from FDEs. */ |
cfc14b3a MK |
1671 | if (eh_frame_p) |
1672 | cie_id = 0; | |
1673 | else if (dwarf64_p) | |
1674 | cie_id = DW64_CIE_ID; | |
6896c0c7 RH |
1675 | else |
1676 | cie_id = DW_CIE_ID; | |
cfc14b3a MK |
1677 | |
1678 | if (dwarf64_p) | |
1679 | { | |
1680 | cie_pointer = read_8_bytes (unit->abfd, buf); | |
1681 | buf += 8; | |
1682 | } | |
1683 | else | |
1684 | { | |
1685 | cie_pointer = read_4_bytes (unit->abfd, buf); | |
1686 | buf += 4; | |
1687 | } | |
1688 | ||
1689 | if (cie_pointer == cie_id) | |
1690 | { | |
1691 | /* This is a CIE. */ | |
1692 | struct dwarf2_cie *cie; | |
1693 | char *augmentation; | |
28ba0b33 | 1694 | unsigned int cie_version; |
cfc14b3a MK |
1695 | |
1696 | /* Record the offset into the .debug_frame section of this CIE. */ | |
1697 | cie_pointer = start - unit->dwarf_frame_buffer; | |
1698 | ||
1699 | /* Check whether we've already read it. */ | |
b01c8410 | 1700 | if (find_cie (cie_table, cie_pointer)) |
cfc14b3a MK |
1701 | return end; |
1702 | ||
1703 | cie = (struct dwarf2_cie *) | |
8b92e4d5 | 1704 | obstack_alloc (&unit->objfile->objfile_obstack, |
cfc14b3a MK |
1705 | sizeof (struct dwarf2_cie)); |
1706 | cie->initial_instructions = NULL; | |
1707 | cie->cie_pointer = cie_pointer; | |
1708 | ||
1709 | /* The encoding for FDE's in a normal .debug_frame section | |
32b05c07 MK |
1710 | depends on the target address size. */ |
1711 | cie->encoding = DW_EH_PE_absptr; | |
cfc14b3a | 1712 | |
ae0d2f24 UW |
1713 | /* The target address size. For .eh_frame FDEs this is considered |
1714 | equal to the size of a target pointer. For .dwarf_frame FDEs, | |
1715 | this is supposed to be the target address size from the associated | |
1716 | CU header. FIXME: We do not have a good way to determine the | |
1717 | latter. Always use the target pointer size for now. */ | |
5e2b427d | 1718 | cie->addr_size = gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT; |
ae0d2f24 | 1719 | |
56c987f6 AO |
1720 | /* We'll determine the final value later, but we need to |
1721 | initialize it conservatively. */ | |
1722 | cie->signal_frame = 0; | |
1723 | ||
cfc14b3a | 1724 | /* Check version number. */ |
28ba0b33 | 1725 | cie_version = read_1_byte (unit->abfd, buf); |
2dc7f7b3 | 1726 | if (cie_version != 1 && cie_version != 3 && cie_version != 4) |
6896c0c7 | 1727 | return NULL; |
303b6f5d | 1728 | cie->version = cie_version; |
cfc14b3a MK |
1729 | buf += 1; |
1730 | ||
1731 | /* Interpret the interesting bits of the augmentation. */ | |
303b6f5d | 1732 | cie->augmentation = augmentation = (char *) buf; |
852483bc | 1733 | buf += (strlen (augmentation) + 1); |
cfc14b3a | 1734 | |
303b6f5d DJ |
1735 | /* Ignore armcc augmentations. We only use them for quirks, |
1736 | and that doesn't happen until later. */ | |
1737 | if (strncmp (augmentation, "armcc", 5) == 0) | |
1738 | augmentation += strlen (augmentation); | |
1739 | ||
cfc14b3a MK |
1740 | /* The GCC 2.x "eh" augmentation has a pointer immediately |
1741 | following the augmentation string, so it must be handled | |
1742 | first. */ | |
1743 | if (augmentation[0] == 'e' && augmentation[1] == 'h') | |
1744 | { | |
1745 | /* Skip. */ | |
5e2b427d | 1746 | buf += gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT; |
cfc14b3a MK |
1747 | augmentation += 2; |
1748 | } | |
1749 | ||
2dc7f7b3 TT |
1750 | if (cie->version >= 4) |
1751 | { | |
1752 | /* FIXME: check that this is the same as from the CU header. */ | |
1753 | cie->addr_size = read_1_byte (unit->abfd, buf); | |
1754 | ++buf; | |
1755 | cie->segment_size = read_1_byte (unit->abfd, buf); | |
1756 | ++buf; | |
1757 | } | |
1758 | else | |
1759 | { | |
1760 | cie->addr_size = gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT; | |
1761 | cie->segment_size = 0; | |
1762 | } | |
1763 | ||
cfc14b3a MK |
1764 | cie->code_alignment_factor = |
1765 | read_unsigned_leb128 (unit->abfd, buf, &bytes_read); | |
1766 | buf += bytes_read; | |
1767 | ||
1768 | cie->data_alignment_factor = | |
1769 | read_signed_leb128 (unit->abfd, buf, &bytes_read); | |
1770 | buf += bytes_read; | |
1771 | ||
28ba0b33 PB |
1772 | if (cie_version == 1) |
1773 | { | |
1774 | cie->return_address_register = read_1_byte (unit->abfd, buf); | |
1775 | bytes_read = 1; | |
1776 | } | |
1777 | else | |
1778 | cie->return_address_register = read_unsigned_leb128 (unit->abfd, buf, | |
1779 | &bytes_read); | |
4fc771b8 | 1780 | cie->return_address_register |
5e2b427d | 1781 | = dwarf2_frame_adjust_regnum (gdbarch, |
4fc771b8 DJ |
1782 | cie->return_address_register, |
1783 | eh_frame_p); | |
4bf8967c | 1784 | |
28ba0b33 | 1785 | buf += bytes_read; |
cfc14b3a | 1786 | |
7131cb6e RH |
1787 | cie->saw_z_augmentation = (*augmentation == 'z'); |
1788 | if (cie->saw_z_augmentation) | |
cfc14b3a MK |
1789 | { |
1790 | ULONGEST length; | |
1791 | ||
1792 | length = read_unsigned_leb128 (unit->abfd, buf, &bytes_read); | |
1793 | buf += bytes_read; | |
6896c0c7 RH |
1794 | if (buf > end) |
1795 | return NULL; | |
cfc14b3a MK |
1796 | cie->initial_instructions = buf + length; |
1797 | augmentation++; | |
1798 | } | |
1799 | ||
1800 | while (*augmentation) | |
1801 | { | |
1802 | /* "L" indicates a byte showing how the LSDA pointer is encoded. */ | |
1803 | if (*augmentation == 'L') | |
1804 | { | |
1805 | /* Skip. */ | |
1806 | buf++; | |
1807 | augmentation++; | |
1808 | } | |
1809 | ||
1810 | /* "R" indicates a byte indicating how FDE addresses are encoded. */ | |
1811 | else if (*augmentation == 'R') | |
1812 | { | |
1813 | cie->encoding = *buf++; | |
1814 | augmentation++; | |
1815 | } | |
1816 | ||
1817 | /* "P" indicates a personality routine in the CIE augmentation. */ | |
1818 | else if (*augmentation == 'P') | |
1819 | { | |
1234d960 | 1820 | /* Skip. Avoid indirection since we throw away the result. */ |
852483bc | 1821 | gdb_byte encoding = (*buf++) & ~DW_EH_PE_indirect; |
ae0d2f24 UW |
1822 | read_encoded_value (unit, encoding, cie->addr_size, |
1823 | buf, &bytes_read, 0); | |
f724bf08 | 1824 | buf += bytes_read; |
cfc14b3a MK |
1825 | augmentation++; |
1826 | } | |
1827 | ||
56c987f6 AO |
1828 | /* "S" indicates a signal frame, such that the return |
1829 | address must not be decremented to locate the call frame | |
1830 | info for the previous frame; it might even be the first | |
1831 | instruction of a function, so decrementing it would take | |
1832 | us to a different function. */ | |
1833 | else if (*augmentation == 'S') | |
1834 | { | |
1835 | cie->signal_frame = 1; | |
1836 | augmentation++; | |
1837 | } | |
1838 | ||
3e9a2e52 DJ |
1839 | /* Otherwise we have an unknown augmentation. Assume that either |
1840 | there is no augmentation data, or we saw a 'z' prefix. */ | |
cfc14b3a MK |
1841 | else |
1842 | { | |
3e9a2e52 DJ |
1843 | if (cie->initial_instructions) |
1844 | buf = cie->initial_instructions; | |
cfc14b3a MK |
1845 | break; |
1846 | } | |
1847 | } | |
1848 | ||
1849 | cie->initial_instructions = buf; | |
1850 | cie->end = end; | |
b01c8410 | 1851 | cie->unit = unit; |
cfc14b3a | 1852 | |
b01c8410 | 1853 | add_cie (cie_table, cie); |
cfc14b3a MK |
1854 | } |
1855 | else | |
1856 | { | |
1857 | /* This is a FDE. */ | |
1858 | struct dwarf2_fde *fde; | |
1859 | ||
6896c0c7 RH |
1860 | /* In an .eh_frame section, the CIE pointer is the delta between the |
1861 | address within the FDE where the CIE pointer is stored and the | |
1862 | address of the CIE. Convert it to an offset into the .eh_frame | |
1863 | section. */ | |
cfc14b3a MK |
1864 | if (eh_frame_p) |
1865 | { | |
cfc14b3a MK |
1866 | cie_pointer = buf - unit->dwarf_frame_buffer - cie_pointer; |
1867 | cie_pointer -= (dwarf64_p ? 8 : 4); | |
1868 | } | |
1869 | ||
6896c0c7 RH |
1870 | /* In either case, validate the result is still within the section. */ |
1871 | if (cie_pointer >= unit->dwarf_frame_size) | |
1872 | return NULL; | |
1873 | ||
cfc14b3a | 1874 | fde = (struct dwarf2_fde *) |
8b92e4d5 | 1875 | obstack_alloc (&unit->objfile->objfile_obstack, |
cfc14b3a | 1876 | sizeof (struct dwarf2_fde)); |
b01c8410 | 1877 | fde->cie = find_cie (cie_table, cie_pointer); |
cfc14b3a MK |
1878 | if (fde->cie == NULL) |
1879 | { | |
1880 | decode_frame_entry (unit, unit->dwarf_frame_buffer + cie_pointer, | |
b01c8410 PP |
1881 | eh_frame_p, cie_table, fde_table); |
1882 | fde->cie = find_cie (cie_table, cie_pointer); | |
cfc14b3a MK |
1883 | } |
1884 | ||
1885 | gdb_assert (fde->cie != NULL); | |
1886 | ||
1887 | fde->initial_location = | |
ae0d2f24 UW |
1888 | read_encoded_value (unit, fde->cie->encoding, fde->cie->addr_size, |
1889 | buf, &bytes_read, 0); | |
cfc14b3a MK |
1890 | buf += bytes_read; |
1891 | ||
1892 | fde->address_range = | |
ae0d2f24 UW |
1893 | read_encoded_value (unit, fde->cie->encoding & 0x0f, |
1894 | fde->cie->addr_size, buf, &bytes_read, 0); | |
cfc14b3a MK |
1895 | buf += bytes_read; |
1896 | ||
7131cb6e RH |
1897 | /* A 'z' augmentation in the CIE implies the presence of an |
1898 | augmentation field in the FDE as well. The only thing known | |
1899 | to be in here at present is the LSDA entry for EH. So we | |
1900 | can skip the whole thing. */ | |
1901 | if (fde->cie->saw_z_augmentation) | |
1902 | { | |
1903 | ULONGEST length; | |
1904 | ||
1905 | length = read_unsigned_leb128 (unit->abfd, buf, &bytes_read); | |
1906 | buf += bytes_read + length; | |
6896c0c7 RH |
1907 | if (buf > end) |
1908 | return NULL; | |
7131cb6e RH |
1909 | } |
1910 | ||
cfc14b3a MK |
1911 | fde->instructions = buf; |
1912 | fde->end = end; | |
1913 | ||
4bf8967c AS |
1914 | fde->eh_frame_p = eh_frame_p; |
1915 | ||
b01c8410 | 1916 | add_fde (fde_table, fde); |
cfc14b3a MK |
1917 | } |
1918 | ||
1919 | return end; | |
1920 | } | |
6896c0c7 RH |
1921 | |
1922 | /* Read a CIE or FDE in BUF and decode it. */ | |
852483bc | 1923 | static gdb_byte * |
b01c8410 PP |
1924 | decode_frame_entry (struct comp_unit *unit, gdb_byte *start, int eh_frame_p, |
1925 | struct dwarf2_cie_table *cie_table, | |
1926 | struct dwarf2_fde_table *fde_table) | |
6896c0c7 RH |
1927 | { |
1928 | enum { NONE, ALIGN4, ALIGN8, FAIL } workaround = NONE; | |
852483bc | 1929 | gdb_byte *ret; |
6896c0c7 RH |
1930 | ptrdiff_t start_offset; |
1931 | ||
1932 | while (1) | |
1933 | { | |
b01c8410 PP |
1934 | ret = decode_frame_entry_1 (unit, start, eh_frame_p, |
1935 | cie_table, fde_table); | |
6896c0c7 RH |
1936 | if (ret != NULL) |
1937 | break; | |
1938 | ||
1939 | /* We have corrupt input data of some form. */ | |
1940 | ||
1941 | /* ??? Try, weakly, to work around compiler/assembler/linker bugs | |
1942 | and mismatches wrt padding and alignment of debug sections. */ | |
1943 | /* Note that there is no requirement in the standard for any | |
1944 | alignment at all in the frame unwind sections. Testing for | |
1945 | alignment before trying to interpret data would be incorrect. | |
1946 | ||
1947 | However, GCC traditionally arranged for frame sections to be | |
1948 | sized such that the FDE length and CIE fields happen to be | |
1949 | aligned (in theory, for performance). This, unfortunately, | |
1950 | was done with .align directives, which had the side effect of | |
1951 | forcing the section to be aligned by the linker. | |
1952 | ||
1953 | This becomes a problem when you have some other producer that | |
1954 | creates frame sections that are not as strictly aligned. That | |
1955 | produces a hole in the frame info that gets filled by the | |
1956 | linker with zeros. | |
1957 | ||
1958 | The GCC behaviour is arguably a bug, but it's effectively now | |
1959 | part of the ABI, so we're now stuck with it, at least at the | |
1960 | object file level. A smart linker may decide, in the process | |
1961 | of compressing duplicate CIE information, that it can rewrite | |
1962 | the entire output section without this extra padding. */ | |
1963 | ||
1964 | start_offset = start - unit->dwarf_frame_buffer; | |
1965 | if (workaround < ALIGN4 && (start_offset & 3) != 0) | |
1966 | { | |
1967 | start += 4 - (start_offset & 3); | |
1968 | workaround = ALIGN4; | |
1969 | continue; | |
1970 | } | |
1971 | if (workaround < ALIGN8 && (start_offset & 7) != 0) | |
1972 | { | |
1973 | start += 8 - (start_offset & 7); | |
1974 | workaround = ALIGN8; | |
1975 | continue; | |
1976 | } | |
1977 | ||
1978 | /* Nothing left to try. Arrange to return as if we've consumed | |
1979 | the entire input section. Hopefully we'll get valid info from | |
1980 | the other of .debug_frame/.eh_frame. */ | |
1981 | workaround = FAIL; | |
1982 | ret = unit->dwarf_frame_buffer + unit->dwarf_frame_size; | |
1983 | break; | |
1984 | } | |
1985 | ||
1986 | switch (workaround) | |
1987 | { | |
1988 | case NONE: | |
1989 | break; | |
1990 | ||
1991 | case ALIGN4: | |
1992 | complaint (&symfile_complaints, | |
e2e0b3e5 | 1993 | _("Corrupt data in %s:%s; align 4 workaround apparently succeeded"), |
6896c0c7 RH |
1994 | unit->dwarf_frame_section->owner->filename, |
1995 | unit->dwarf_frame_section->name); | |
1996 | break; | |
1997 | ||
1998 | case ALIGN8: | |
1999 | complaint (&symfile_complaints, | |
e2e0b3e5 | 2000 | _("Corrupt data in %s:%s; align 8 workaround apparently succeeded"), |
6896c0c7 RH |
2001 | unit->dwarf_frame_section->owner->filename, |
2002 | unit->dwarf_frame_section->name); | |
2003 | break; | |
2004 | ||
2005 | default: | |
2006 | complaint (&symfile_complaints, | |
e2e0b3e5 | 2007 | _("Corrupt data in %s:%s"), |
6896c0c7 RH |
2008 | unit->dwarf_frame_section->owner->filename, |
2009 | unit->dwarf_frame_section->name); | |
2010 | break; | |
2011 | } | |
2012 | ||
2013 | return ret; | |
2014 | } | |
cfc14b3a MK |
2015 | \f |
2016 | ||
cfc14b3a | 2017 | /* Imported from dwarf2read.c. */ |
dce234bc PP |
2018 | extern void dwarf2_get_section_info (struct objfile *, const char *, asection **, |
2019 | gdb_byte **, bfd_size_type *); | |
cfc14b3a | 2020 | |
b01c8410 PP |
2021 | static int |
2022 | qsort_fde_cmp (const void *a, const void *b) | |
2023 | { | |
2024 | struct dwarf2_fde *aa = *(struct dwarf2_fde **)a; | |
2025 | struct dwarf2_fde *bb = *(struct dwarf2_fde **)b; | |
e5af178f | 2026 | |
b01c8410 | 2027 | if (aa->initial_location == bb->initial_location) |
e5af178f PP |
2028 | { |
2029 | if (aa->address_range != bb->address_range | |
2030 | && aa->eh_frame_p == 0 && bb->eh_frame_p == 0) | |
2031 | /* Linker bug, e.g. gold/10400. | |
2032 | Work around it by keeping stable sort order. */ | |
2033 | return (a < b) ? -1 : 1; | |
2034 | else | |
2035 | /* Put eh_frame entries after debug_frame ones. */ | |
2036 | return aa->eh_frame_p - bb->eh_frame_p; | |
2037 | } | |
b01c8410 PP |
2038 | |
2039 | return (aa->initial_location < bb->initial_location) ? -1 : 1; | |
2040 | } | |
2041 | ||
cfc14b3a MK |
2042 | void |
2043 | dwarf2_build_frame_info (struct objfile *objfile) | |
2044 | { | |
ae0d2f24 | 2045 | struct comp_unit *unit; |
852483bc | 2046 | gdb_byte *frame_ptr; |
b01c8410 PP |
2047 | struct dwarf2_cie_table cie_table; |
2048 | struct dwarf2_fde_table fde_table; | |
be391dca | 2049 | struct dwarf2_fde_table *fde_table2; |
b01c8410 PP |
2050 | |
2051 | cie_table.num_entries = 0; | |
2052 | cie_table.entries = NULL; | |
2053 | ||
2054 | fde_table.num_entries = 0; | |
2055 | fde_table.entries = NULL; | |
cfc14b3a MK |
2056 | |
2057 | /* Build a minimal decoding of the DWARF2 compilation unit. */ | |
ae0d2f24 UW |
2058 | unit = (struct comp_unit *) obstack_alloc (&objfile->objfile_obstack, |
2059 | sizeof (struct comp_unit)); | |
2060 | unit->abfd = objfile->obfd; | |
2061 | unit->objfile = objfile; | |
2062 | unit->dbase = 0; | |
2063 | unit->tbase = 0; | |
cfc14b3a | 2064 | |
dce234bc PP |
2065 | dwarf2_get_section_info (objfile, ".eh_frame", |
2066 | &unit->dwarf_frame_section, | |
2067 | &unit->dwarf_frame_buffer, | |
2068 | &unit->dwarf_frame_size); | |
2069 | if (unit->dwarf_frame_size) | |
cfc14b3a | 2070 | { |
0fd85043 | 2071 | asection *got, *txt; |
0912c7f2 | 2072 | |
0912c7f2 | 2073 | /* FIXME: kettenis/20030602: This is the DW_EH_PE_datarel base |
37b517aa MK |
2074 | that is used for the i386/amd64 target, which currently is |
2075 | the only target in GCC that supports/uses the | |
2076 | DW_EH_PE_datarel encoding. */ | |
ae0d2f24 | 2077 | got = bfd_get_section_by_name (unit->abfd, ".got"); |
0912c7f2 | 2078 | if (got) |
ae0d2f24 | 2079 | unit->dbase = got->vma; |
0912c7f2 | 2080 | |
22c7ba1a MK |
2081 | /* GCC emits the DW_EH_PE_textrel encoding type on sh and ia64 |
2082 | so far. */ | |
ae0d2f24 | 2083 | txt = bfd_get_section_by_name (unit->abfd, ".text"); |
0fd85043 | 2084 | if (txt) |
ae0d2f24 | 2085 | unit->tbase = txt->vma; |
0fd85043 | 2086 | |
ae0d2f24 UW |
2087 | frame_ptr = unit->dwarf_frame_buffer; |
2088 | while (frame_ptr < unit->dwarf_frame_buffer + unit->dwarf_frame_size) | |
b01c8410 PP |
2089 | frame_ptr = decode_frame_entry (unit, frame_ptr, 1, |
2090 | &cie_table, &fde_table); | |
2091 | ||
2092 | if (cie_table.num_entries != 0) | |
2093 | { | |
2094 | /* Reinit cie_table: debug_frame has different CIEs. */ | |
2095 | xfree (cie_table.entries); | |
2096 | cie_table.num_entries = 0; | |
2097 | cie_table.entries = NULL; | |
2098 | } | |
cfc14b3a MK |
2099 | } |
2100 | ||
dce234bc PP |
2101 | dwarf2_get_section_info (objfile, ".debug_frame", |
2102 | &unit->dwarf_frame_section, | |
2103 | &unit->dwarf_frame_buffer, | |
2104 | &unit->dwarf_frame_size); | |
2105 | if (unit->dwarf_frame_size) | |
cfc14b3a | 2106 | { |
ae0d2f24 UW |
2107 | frame_ptr = unit->dwarf_frame_buffer; |
2108 | while (frame_ptr < unit->dwarf_frame_buffer + unit->dwarf_frame_size) | |
b01c8410 PP |
2109 | frame_ptr = decode_frame_entry (unit, frame_ptr, 0, |
2110 | &cie_table, &fde_table); | |
2111 | } | |
2112 | ||
2113 | /* Discard the cie_table, it is no longer needed. */ | |
2114 | if (cie_table.num_entries != 0) | |
2115 | { | |
2116 | xfree (cie_table.entries); | |
2117 | cie_table.entries = NULL; /* Paranoia. */ | |
2118 | cie_table.num_entries = 0; /* Paranoia. */ | |
2119 | } | |
2120 | ||
be391dca TT |
2121 | /* Copy fde_table to obstack: it is needed at runtime. */ |
2122 | fde_table2 = (struct dwarf2_fde_table *) | |
2123 | obstack_alloc (&objfile->objfile_obstack, sizeof (*fde_table2)); | |
2124 | ||
2125 | if (fde_table.num_entries == 0) | |
2126 | { | |
2127 | fde_table2->entries = NULL; | |
2128 | fde_table2->num_entries = 0; | |
2129 | } | |
2130 | else | |
b01c8410 | 2131 | { |
875cdfbb PA |
2132 | struct dwarf2_fde *fde_prev = NULL; |
2133 | struct dwarf2_fde *first_non_zero_fde = NULL; | |
2134 | int i; | |
b01c8410 PP |
2135 | |
2136 | /* Prepare FDE table for lookups. */ | |
2137 | qsort (fde_table.entries, fde_table.num_entries, | |
2138 | sizeof (fde_table.entries[0]), qsort_fde_cmp); | |
2139 | ||
875cdfbb PA |
2140 | /* Check for leftovers from --gc-sections. The GNU linker sets |
2141 | the relevant symbols to zero, but doesn't zero the FDE *end* | |
2142 | ranges because there's no relocation there. It's (offset, | |
2143 | length), not (start, end). On targets where address zero is | |
2144 | just another valid address this can be a problem, since the | |
2145 | FDEs appear to be non-empty in the output --- we could pick | |
2146 | out the wrong FDE. To work around this, when overlaps are | |
2147 | detected, we prefer FDEs that do not start at zero. | |
2148 | ||
2149 | Start by finding the first FDE with non-zero start. Below | |
2150 | we'll discard all FDEs that start at zero and overlap this | |
2151 | one. */ | |
2152 | for (i = 0; i < fde_table.num_entries; i++) | |
2153 | { | |
2154 | struct dwarf2_fde *fde = fde_table.entries[i]; | |
b01c8410 | 2155 | |
875cdfbb PA |
2156 | if (fde->initial_location != 0) |
2157 | { | |
2158 | first_non_zero_fde = fde; | |
2159 | break; | |
2160 | } | |
2161 | } | |
2162 | ||
2163 | /* Since we'll be doing bsearch, squeeze out identical (except | |
2164 | for eh_frame_p) fde entries so bsearch result is predictable. | |
2165 | Also discard leftovers from --gc-sections. */ | |
be391dca | 2166 | fde_table2->num_entries = 0; |
875cdfbb PA |
2167 | for (i = 0; i < fde_table.num_entries; i++) |
2168 | { | |
2169 | struct dwarf2_fde *fde = fde_table.entries[i]; | |
2170 | ||
2171 | if (fde->initial_location == 0 | |
2172 | && first_non_zero_fde != NULL | |
2173 | && (first_non_zero_fde->initial_location | |
2174 | < fde->initial_location + fde->address_range)) | |
2175 | continue; | |
2176 | ||
2177 | if (fde_prev != NULL | |
2178 | && fde_prev->initial_location == fde->initial_location) | |
2179 | continue; | |
2180 | ||
2181 | obstack_grow (&objfile->objfile_obstack, &fde_table.entries[i], | |
2182 | sizeof (fde_table.entries[0])); | |
2183 | ++fde_table2->num_entries; | |
2184 | fde_prev = fde; | |
2185 | } | |
b01c8410 | 2186 | fde_table2->entries = obstack_finish (&objfile->objfile_obstack); |
b01c8410 PP |
2187 | |
2188 | /* Discard the original fde_table. */ | |
2189 | xfree (fde_table.entries); | |
cfc14b3a | 2190 | } |
be391dca TT |
2191 | |
2192 | set_objfile_data (objfile, dwarf2_frame_objfile_data, fde_table2); | |
cfc14b3a | 2193 | } |
0d0e1a63 MK |
2194 | |
2195 | /* Provide a prototype to silence -Wmissing-prototypes. */ | |
2196 | void _initialize_dwarf2_frame (void); | |
2197 | ||
2198 | void | |
2199 | _initialize_dwarf2_frame (void) | |
2200 | { | |
030f20e1 | 2201 | dwarf2_frame_data = gdbarch_data_register_pre_init (dwarf2_frame_init); |
8f22cb90 | 2202 | dwarf2_frame_objfile_data = register_objfile_data (); |
0d0e1a63 | 2203 | } |