1 /* Target-dependent code for the MDEBUG MIPS architecture, for GDB,
4 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
5 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2006, 2007
6 Free Software Foundation, Inc.
8 This file is part of GDB.
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
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
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.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 Boston, MA 02110-1301, USA. */
27 #include "mips-tdep.h"
28 #include "trad-frame.h"
34 #include "gdb_assert.h"
35 #include "frame-unwind.h"
36 #include "frame-base.h"
37 #include "mips-mdebug-tdep.h"
38 #include "mdebugread.h"
40 #define PROC_LOW_ADDR(proc) ((proc)->pdr.adr) /* least address */
41 #define PROC_FRAME_OFFSET(proc) ((proc)->pdr.frameoffset)
42 #define PROC_FRAME_REG(proc) ((proc)->pdr.framereg)
43 #define PROC_REG_MASK(proc) ((proc)->pdr.regmask)
44 #define PROC_FREG_MASK(proc) ((proc)->pdr.fregmask)
45 #define PROC_REG_OFFSET(proc) ((proc)->pdr.regoffset)
46 #define PROC_FREG_OFFSET(proc) ((proc)->pdr.fregoffset)
47 #define PROC_PC_REG(proc) ((proc)->pdr.pcreg)
48 /* FIXME drow/2002-06-10: If a pointer on the host is bigger than a long,
49 this will corrupt pdr.iline. Fortunately we don't use it. */
50 #define PROC_SYMBOL(proc) (*(struct symbol**)&(proc)->pdr.isym)
51 #define _PROC_MAGIC_ 0x0F0F0F0F
53 struct mips_objfile_private
59 /* Global used to communicate between non_heuristic_proc_desc and
60 compare_pdr_entries within qsort (). */
64 compare_pdr_entries (const void *a
, const void *b
)
66 CORE_ADDR lhs
= bfd_get_signed_32 (the_bfd
, (bfd_byte
*) a
);
67 CORE_ADDR rhs
= bfd_get_signed_32 (the_bfd
, (bfd_byte
*) b
);
77 static const struct objfile_data
*mips_pdr_data
;
79 static struct mdebug_extra_func_info
*
80 non_heuristic_proc_desc (CORE_ADDR pc
, CORE_ADDR
*addrptr
)
83 struct mdebug_extra_func_info
*proc_desc
;
84 struct block
*b
= block_for_pc (pc
);
86 struct obj_section
*sec
;
87 struct mips_objfile_private
*priv
;
89 find_pc_partial_function (pc
, NULL
, &startaddr
, NULL
);
95 sec
= find_pc_section (pc
);
98 priv
= (struct mips_objfile_private
*) objfile_data (sec
->objfile
, mips_pdr_data
);
100 /* Search the ".pdr" section generated by GAS. This includes most of
101 the information normally found in ECOFF PDRs. */
103 the_bfd
= sec
->objfile
->obfd
;
105 && (the_bfd
->format
== bfd_object
106 && bfd_get_flavour (the_bfd
) == bfd_target_elf_flavour
107 && elf_elfheader (the_bfd
)->e_ident
[EI_CLASS
] == ELFCLASS64
))
109 /* Right now GAS only outputs the address as a four-byte sequence.
110 This means that we should not bother with this method on 64-bit
111 targets (until that is fixed). */
113 priv
= obstack_alloc (&sec
->objfile
->objfile_obstack
,
114 sizeof (struct mips_objfile_private
));
116 set_objfile_data (sec
->objfile
, mips_pdr_data
, priv
);
118 else if (priv
== NULL
)
122 priv
= obstack_alloc (&sec
->objfile
->objfile_obstack
,
123 sizeof (struct mips_objfile_private
));
125 bfdsec
= bfd_get_section_by_name (sec
->objfile
->obfd
, ".pdr");
128 priv
->size
= bfd_section_size (sec
->objfile
->obfd
, bfdsec
);
129 priv
->contents
= obstack_alloc (&sec
->objfile
->objfile_obstack
,
131 bfd_get_section_contents (sec
->objfile
->obfd
, bfdsec
,
132 priv
->contents
, 0, priv
->size
);
134 /* In general, the .pdr section is sorted. However, in the
135 presence of multiple code sections (and other corner cases)
136 it can become unsorted. Sort it so that we can use a faster
138 qsort (priv
->contents
, priv
->size
/ 32, 32,
139 compare_pdr_entries
);
144 set_objfile_data (sec
->objfile
, mips_pdr_data
, priv
);
155 high
= priv
->size
/ 32;
157 /* We've found a .pdr section describing this objfile. We want to
158 find the entry which describes this code address. The .pdr
159 information is not very descriptive; we have only a function
160 start address. We have to look for the closest entry, because
161 the local symbol at the beginning of this function may have
162 been stripped - so if we ask the symbol table for the start
163 address we may get a preceding global function. */
165 /* First, find the last .pdr entry starting at or before PC. */
168 mid
= (low
+ high
) / 2;
170 ptr
= priv
->contents
+ mid
* 32;
171 pdr_pc
= bfd_get_signed_32 (sec
->objfile
->obfd
, ptr
);
172 pdr_pc
+= ANOFFSET (sec
->objfile
->section_offsets
,
173 SECT_OFF_TEXT (sec
->objfile
));
182 /* Both low and high point one past the PDR of interest. If
183 both are zero, that means this PC is before any region
184 covered by a PDR, i.e. pdr_pc for the first PDR entry is
188 ptr
= priv
->contents
+ (low
- 1) * 32;
189 pdr_pc
= bfd_get_signed_32 (sec
->objfile
->obfd
, ptr
);
190 pdr_pc
+= ANOFFSET (sec
->objfile
->section_offsets
,
191 SECT_OFF_TEXT (sec
->objfile
));
194 /* We don't have a range, so we have no way to know for sure
195 whether we're in the correct PDR or a PDR for a preceding
196 function and the current function was a stripped local
197 symbol. But if the PDR's PC is at least as great as the
198 best guess from the symbol table, assume that it does cover
199 the right area; if a .pdr section is present at all then
200 nearly every function will have an entry. The biggest exception
201 will be the dynamic linker stubs; conveniently these are
202 placed before .text instead of after. */
204 if (pc
>= pdr_pc
&& pdr_pc
>= startaddr
)
206 struct symbol
*sym
= find_pc_function (pc
);
211 /* Fill in what we need of the proc_desc. */
212 proc_desc
= (struct mdebug_extra_func_info
*)
213 obstack_alloc (&sec
->objfile
->objfile_obstack
,
214 sizeof (struct mdebug_extra_func_info
));
215 PROC_LOW_ADDR (proc_desc
) = pdr_pc
;
217 PROC_FRAME_OFFSET (proc_desc
)
218 = bfd_get_signed_32 (sec
->objfile
->obfd
, ptr
+ 20);
219 PROC_FRAME_REG (proc_desc
) = bfd_get_32 (sec
->objfile
->obfd
,
221 PROC_REG_MASK (proc_desc
) = bfd_get_32 (sec
->objfile
->obfd
,
223 PROC_FREG_MASK (proc_desc
) = bfd_get_32 (sec
->objfile
->obfd
,
225 PROC_REG_OFFSET (proc_desc
)
226 = bfd_get_signed_32 (sec
->objfile
->obfd
, ptr
+ 8);
227 PROC_FREG_OFFSET (proc_desc
)
228 = bfd_get_signed_32 (sec
->objfile
->obfd
, ptr
+ 16);
229 PROC_PC_REG (proc_desc
) = bfd_get_32 (sec
->objfile
->obfd
,
231 proc_desc
->pdr
.isym
= (long) sym
;
241 if (startaddr
> BLOCK_START (b
))
243 /* This is the "pathological" case referred to in a comment in
244 print_frame_info. It might be better to move this check into
249 sym
= lookup_symbol (MDEBUG_EFI_SYMBOL_NAME
, b
, LABEL_DOMAIN
, 0, NULL
);
251 /* If we never found a PDR for this function in symbol reading, then
252 examine prologues to find the information. */
255 proc_desc
= (struct mdebug_extra_func_info
*) SYMBOL_VALUE (sym
);
256 if (PROC_FRAME_REG (proc_desc
) == -1)
265 struct mips_frame_cache
268 struct trad_frame_saved_reg
*saved_regs
;
271 static struct mips_frame_cache
*
272 mips_mdebug_frame_cache (struct frame_info
*next_frame
, void **this_cache
)
274 CORE_ADDR startaddr
= 0;
275 struct mdebug_extra_func_info
*proc_desc
;
276 struct mips_frame_cache
*cache
;
277 struct gdbarch
*gdbarch
= get_frame_arch (next_frame
);
278 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
279 /* r0 bit means kernel trap */
281 /* What registers have been saved? Bitmasks. */
282 unsigned long gen_mask
, float_mask
;
284 if ((*this_cache
) != NULL
)
285 return (*this_cache
);
286 cache
= FRAME_OBSTACK_ZALLOC (struct mips_frame_cache
);
287 (*this_cache
) = cache
;
288 cache
->saved_regs
= trad_frame_alloc_saved_regs (next_frame
);
290 /* Get the mdebug proc descriptor. */
291 proc_desc
= non_heuristic_proc_desc (frame_pc_unwind (next_frame
),
293 /* Must be true. This is only called when the sniffer detected a
295 gdb_assert (proc_desc
!= NULL
);
297 /* Extract the frame's base. */
298 cache
->base
= (frame_unwind_register_signed (next_frame
, NUM_REGS
+ PROC_FRAME_REG (proc_desc
))
299 + PROC_FRAME_OFFSET (proc_desc
));
301 kernel_trap
= PROC_REG_MASK (proc_desc
) & 1;
302 gen_mask
= kernel_trap
? 0xFFFFFFFF : PROC_REG_MASK (proc_desc
);
303 float_mask
= kernel_trap
? 0xFFFFFFFF : PROC_FREG_MASK (proc_desc
);
305 /* Must be true. The in_prologue case is left for the heuristic
306 unwinder. This is always used on kernel traps. */
307 gdb_assert (!in_prologue (frame_pc_unwind (next_frame
), PROC_LOW_ADDR (proc_desc
))
310 /* Fill in the offsets for the registers which gen_mask says were
313 CORE_ADDR reg_position
= (cache
->base
+ PROC_REG_OFFSET (proc_desc
));
316 for (ireg
= MIPS_NUMREGS
- 1; gen_mask
; --ireg
, gen_mask
<<= 1)
317 if (gen_mask
& 0x80000000)
319 cache
->saved_regs
[NUM_REGS
+ ireg
].addr
= reg_position
;
320 reg_position
-= mips_abi_regsize (gdbarch
);
324 /* Fill in the offsets for the registers which float_mask says were
327 CORE_ADDR reg_position
= (cache
->base
328 + PROC_FREG_OFFSET (proc_desc
));
330 /* Fill in the offsets for the float registers which float_mask
332 for (ireg
= MIPS_NUMREGS
- 1; float_mask
; --ireg
, float_mask
<<= 1)
333 if (float_mask
& 0x80000000)
335 if (mips_abi_regsize (gdbarch
) == 4
336 && TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
)
338 /* On a big endian 32 bit ABI, floating point registers
339 are paired to form doubles such that the most
340 significant part is in $f[N+1] and the least
341 significant in $f[N] vis: $f[N+1] ||| $f[N]. The
342 registers are also spilled as a pair and stored as a
345 When little-endian the least significant part is
346 stored first leading to the memory order $f[N] and
349 Unfortunately, when big-endian the most significant
350 part of the double is stored first, and the least
351 significant is stored second. This leads to the
352 registers being ordered in memory as firt $f[N+1] and
355 For the big-endian case make certain that the
356 addresses point at the correct (swapped) locations
357 $f[N] and $f[N+1] pair (keep in mind that
358 reg_position is decremented each time through the
361 cache
->saved_regs
[NUM_REGS
+ mips_regnum (current_gdbarch
)->fp0
+ ireg
]
362 .addr
= reg_position
- mips_abi_regsize (gdbarch
);
364 cache
->saved_regs
[NUM_REGS
+ mips_regnum (current_gdbarch
)->fp0
+ ireg
]
365 .addr
= reg_position
+ mips_abi_regsize (gdbarch
);
368 cache
->saved_regs
[NUM_REGS
+ mips_regnum (current_gdbarch
)->fp0
+ ireg
]
369 .addr
= reg_position
;
370 reg_position
-= mips_abi_regsize (gdbarch
);
373 cache
->saved_regs
[NUM_REGS
+ mips_regnum (current_gdbarch
)->pc
]
374 = cache
->saved_regs
[NUM_REGS
+ MIPS_RA_REGNUM
];
377 /* SP_REGNUM, contains the value and not the address. */
378 trad_frame_set_value (cache
->saved_regs
, NUM_REGS
+ MIPS_SP_REGNUM
, cache
->base
);
380 return (*this_cache
);
384 mips_mdebug_frame_this_id (struct frame_info
*next_frame
, void **this_cache
,
385 struct frame_id
*this_id
)
387 struct mips_frame_cache
*info
= mips_mdebug_frame_cache (next_frame
,
389 (*this_id
) = frame_id_build (info
->base
, frame_func_unwind (next_frame
));
393 mips_mdebug_frame_prev_register (struct frame_info
*next_frame
,
395 int regnum
, int *optimizedp
,
396 enum lval_type
*lvalp
, CORE_ADDR
*addrp
,
397 int *realnump
, gdb_byte
*valuep
)
399 struct mips_frame_cache
*info
= mips_mdebug_frame_cache (next_frame
,
401 trad_frame_get_prev_register (next_frame
, info
->saved_regs
, regnum
,
402 optimizedp
, lvalp
, addrp
, realnump
, valuep
);
405 static const struct frame_unwind mips_mdebug_frame_unwind
=
408 mips_mdebug_frame_this_id
,
409 mips_mdebug_frame_prev_register
412 static const struct frame_unwind
*
413 mips_mdebug_frame_sniffer (struct frame_info
*next_frame
)
415 CORE_ADDR pc
= frame_pc_unwind (next_frame
);
416 CORE_ADDR startaddr
= 0;
417 struct mdebug_extra_func_info
*proc_desc
;
420 /* Don't use this on MIPS16. */
421 if (mips_pc_is_mips16 (pc
))
424 /* Only use the mdebug frame unwinder on mdebug frames where all the
425 registers have been saved. Leave hard cases such as no mdebug or
426 in prologue for the heuristic unwinders. */
428 proc_desc
= non_heuristic_proc_desc (pc
, &startaddr
);
429 if (proc_desc
== NULL
)
432 /* Not sure exactly what kernel_trap means, but if it means the
433 kernel saves the registers without a prologue doing it, we better
434 not examine the prologue to see whether registers have been saved
436 kernel_trap
= PROC_REG_MASK (proc_desc
) & 1;
438 return &mips_mdebug_frame_unwind
;
440 /* In any frame other than the innermost or a frame interrupted by a
441 signal, we assume that all registers have been saved. This
442 assumes that all register saves in a function happen before the
443 first function call. */
444 if (!in_prologue (pc
, PROC_LOW_ADDR (proc_desc
)))
445 return &mips_mdebug_frame_unwind
;
451 mips_mdebug_frame_base_address (struct frame_info
*next_frame
,
454 struct mips_frame_cache
*info
= mips_mdebug_frame_cache (next_frame
,
459 static const struct frame_base mips_mdebug_frame_base
= {
460 &mips_mdebug_frame_unwind
,
461 mips_mdebug_frame_base_address
,
462 mips_mdebug_frame_base_address
,
463 mips_mdebug_frame_base_address
466 static const struct frame_base
*
467 mips_mdebug_frame_base_sniffer (struct frame_info
*next_frame
)
469 if (mips_mdebug_frame_sniffer (next_frame
) != NULL
)
470 return &mips_mdebug_frame_base
;
476 mips_mdebug_append_sniffers (struct gdbarch
*gdbarch
)
478 frame_unwind_append_sniffer (gdbarch
, mips_mdebug_frame_sniffer
);
479 frame_base_append_sniffer (gdbarch
, mips_mdebug_frame_base_sniffer
);
483 extern void _initialize_mips_mdebug_tdep (void);
485 _initialize_mips_mdebug_tdep (void)
487 mips_pdr_data
= register_objfile_data ();