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