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