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