1 /* Target-dependent code for the MDEBUG MIPS architecture, for GDB,
4 Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
5 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software
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., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, 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"
39 #define PROC_LOW_ADDR(proc) ((proc)->pdr.adr) /* least address */
40 #define PROC_FRAME_OFFSET(proc) ((proc)->pdr.frameoffset)
41 #define PROC_FRAME_REG(proc) ((proc)->pdr.framereg)
42 #define PROC_REG_MASK(proc) ((proc)->pdr.regmask)
43 #define PROC_FREG_MASK(proc) ((proc)->pdr.fregmask)
44 #define PROC_REG_OFFSET(proc) ((proc)->pdr.regoffset)
45 #define PROC_FREG_OFFSET(proc) ((proc)->pdr.fregoffset)
46 #define PROC_PC_REG(proc) ((proc)->pdr.pcreg)
47 /* FIXME drow/2002-06-10: If a pointer on the host is bigger than a long,
48 this will corrupt pdr.iline. Fortunately we don't use it. */
49 #define PROC_SYMBOL(proc) (*(struct symbol**)&(proc)->pdr.isym)
50 #define _PROC_MAGIC_ 0x0F0F0F0F
52 struct mips_objfile_private
58 /* Global used to communicate between non_heuristic_proc_desc and
59 compare_pdr_entries within qsort (). */
63 compare_pdr_entries (const void *a
, const void *b
)
65 CORE_ADDR lhs
= bfd_get_32 (the_bfd
, (bfd_byte
*) a
);
66 CORE_ADDR rhs
= bfd_get_32 (the_bfd
, (bfd_byte
*) b
);
76 static const struct objfile_data
*mips_pdr_data
;
78 static struct mdebug_extra_func_info
*
79 non_heuristic_proc_desc (CORE_ADDR pc
, CORE_ADDR
*addrptr
)
82 struct mdebug_extra_func_info
*proc_desc
;
83 struct block
*b
= block_for_pc (pc
);
85 struct obj_section
*sec
;
86 struct mips_objfile_private
*priv
;
88 find_pc_partial_function (pc
, NULL
, &startaddr
, NULL
);
94 sec
= find_pc_section (pc
);
97 priv
= (struct mips_objfile_private
*) objfile_data (sec
->objfile
, mips_pdr_data
);
99 /* Search the ".pdr" section generated by GAS. This includes most of
100 the information normally found in ECOFF PDRs. */
102 the_bfd
= sec
->objfile
->obfd
;
104 && (the_bfd
->format
== bfd_object
105 && bfd_get_flavour (the_bfd
) == bfd_target_elf_flavour
106 && elf_elfheader (the_bfd
)->e_ident
[EI_CLASS
] == ELFCLASS64
))
108 /* Right now GAS only outputs the address as a four-byte sequence.
109 This means that we should not bother with this method on 64-bit
110 targets (until that is fixed). */
112 priv
= obstack_alloc (&sec
->objfile
->objfile_obstack
,
113 sizeof (struct mips_objfile_private
));
115 set_objfile_data (sec
->objfile
, mips_pdr_data
, priv
);
117 else if (priv
== NULL
)
121 priv
= obstack_alloc (&sec
->objfile
->objfile_obstack
,
122 sizeof (struct mips_objfile_private
));
124 bfdsec
= bfd_get_section_by_name (sec
->objfile
->obfd
, ".pdr");
127 priv
->size
= bfd_section_size (sec
->objfile
->obfd
, bfdsec
);
128 priv
->contents
= obstack_alloc (&sec
->objfile
->objfile_obstack
,
130 bfd_get_section_contents (sec
->objfile
->obfd
, bfdsec
,
131 priv
->contents
, 0, priv
->size
);
133 /* In general, the .pdr section is sorted. However, in the
134 presence of multiple code sections (and other corner cases)
135 it can become unsorted. Sort it so that we can use a faster
137 qsort (priv
->contents
, priv
->size
/ 32, 32,
138 compare_pdr_entries
);
143 set_objfile_data (sec
->objfile
, mips_pdr_data
, priv
);
154 high
= priv
->size
/ 32;
156 /* We've found a .pdr section describing this objfile. We want to
157 find the entry which describes this code address. The .pdr
158 information is not very descriptive; we have only a function
159 start address. We have to look for the closest entry, because
160 the local symbol at the beginning of this function may have
161 been stripped - so if we ask the symbol table for the start
162 address we may get a preceding global function. */
164 /* First, find the last .pdr entry starting at or before PC. */
167 mid
= (low
+ high
) / 2;
169 ptr
= priv
->contents
+ mid
* 32;
170 pdr_pc
= bfd_get_signed_32 (sec
->objfile
->obfd
, ptr
);
171 pdr_pc
+= ANOFFSET (sec
->objfile
->section_offsets
,
172 SECT_OFF_TEXT (sec
->objfile
));
181 /* Both low and high point one past the PDR of interest. If
182 both are zero, that means this PC is before any region
183 covered by a PDR, i.e. pdr_pc for the first PDR entry is
187 ptr
= priv
->contents
+ (low
- 1) * 32;
188 pdr_pc
= bfd_get_signed_32 (sec
->objfile
->obfd
, ptr
);
189 pdr_pc
+= ANOFFSET (sec
->objfile
->section_offsets
,
190 SECT_OFF_TEXT (sec
->objfile
));
193 /* We don't have a range, so we have no way to know for sure
194 whether we're in the correct PDR or a PDR for a preceding
195 function and the current function was a stripped local
196 symbol. But if the PDR's PC is at least as great as the
197 best guess from the symbol table, assume that it does cover
198 the right area; if a .pdr section is present at all then
199 nearly every function will have an entry. The biggest exception
200 will be the dynamic linker stubs; conveniently these are
201 placed before .text instead of after. */
203 if (pc
>= pdr_pc
&& pdr_pc
>= startaddr
)
205 struct symbol
*sym
= find_pc_function (pc
);
210 /* Fill in what we need of the proc_desc. */
211 proc_desc
= (struct mdebug_extra_func_info
*)
212 obstack_alloc (&sec
->objfile
->objfile_obstack
,
213 sizeof (struct mdebug_extra_func_info
));
214 PROC_LOW_ADDR (proc_desc
) = pdr_pc
;
216 PROC_FRAME_OFFSET (proc_desc
)
217 = bfd_get_32 (sec
->objfile
->obfd
, ptr
+ 20);
218 PROC_FRAME_REG (proc_desc
) = bfd_get_32 (sec
->objfile
->obfd
,
220 PROC_REG_MASK (proc_desc
) = bfd_get_32 (sec
->objfile
->obfd
,
222 PROC_FREG_MASK (proc_desc
) = bfd_get_32 (sec
->objfile
->obfd
,
224 PROC_REG_OFFSET (proc_desc
) = bfd_get_32 (sec
->objfile
->obfd
,
226 PROC_FREG_OFFSET (proc_desc
)
227 = bfd_get_32 (sec
->objfile
->obfd
, ptr
+ 16);
228 PROC_PC_REG (proc_desc
) = bfd_get_32 (sec
->objfile
->obfd
,
230 proc_desc
->pdr
.isym
= (long) sym
;
240 if (startaddr
> BLOCK_START (b
))
242 /* This is the "pathological" case referred to in a comment in
243 print_frame_info. It might be better to move this check into
248 sym
= lookup_symbol (MDEBUG_EFI_SYMBOL_NAME
, b
, LABEL_DOMAIN
, 0, NULL
);
250 /* If we never found a PDR for this function in symbol reading, then
251 examine prologues to find the information. */
254 proc_desc
= (struct mdebug_extra_func_info
*) SYMBOL_VALUE (sym
);
255 if (PROC_FRAME_REG (proc_desc
) == -1)
264 struct mips_frame_cache
267 struct trad_frame_saved_reg
*saved_regs
;
270 static struct mips_frame_cache
*
271 mips_mdebug_frame_cache (struct frame_info
*next_frame
, void **this_cache
)
273 CORE_ADDR startaddr
= 0;
274 struct mdebug_extra_func_info
*proc_desc
;
275 struct mips_frame_cache
*cache
;
276 struct gdbarch
*gdbarch
= get_frame_arch (next_frame
);
277 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
278 /* r0 bit means kernel trap */
280 /* What registers have been saved? Bitmasks. */
281 unsigned long gen_mask
, float_mask
;
283 if ((*this_cache
) != NULL
)
284 return (*this_cache
);
285 cache
= FRAME_OBSTACK_ZALLOC (struct mips_frame_cache
);
286 (*this_cache
) = cache
;
287 cache
->saved_regs
= trad_frame_alloc_saved_regs (next_frame
);
289 /* Get the mdebug proc descriptor. */
290 proc_desc
= non_heuristic_proc_desc (frame_pc_unwind (next_frame
),
292 /* Must be true. This is only called when the sniffer detected a
294 gdb_assert (proc_desc
!= NULL
);
296 /* Extract the frame's base. */
297 cache
->base
= (frame_unwind_register_signed (next_frame
, NUM_REGS
+ PROC_FRAME_REG (proc_desc
))
298 + PROC_FRAME_OFFSET (proc_desc
));
300 kernel_trap
= PROC_REG_MASK (proc_desc
) & 1;
301 gen_mask
= kernel_trap
? 0xFFFFFFFF : PROC_REG_MASK (proc_desc
);
302 float_mask
= kernel_trap
? 0xFFFFFFFF : PROC_FREG_MASK (proc_desc
);
304 /* Must be true. The in_prologue case is left for the heuristic
305 unwinder. This is always used on kernel traps. */
306 gdb_assert (!in_prologue (frame_pc_unwind (next_frame
), PROC_LOW_ADDR (proc_desc
))
309 /* Fill in the offsets for the registers which gen_mask says were
312 CORE_ADDR reg_position
= (cache
->base
+ PROC_REG_OFFSET (proc_desc
));
315 for (ireg
= MIPS_NUMREGS
- 1; gen_mask
; --ireg
, gen_mask
<<= 1)
316 if (gen_mask
& 0x80000000)
318 cache
->saved_regs
[NUM_REGS
+ ireg
].addr
= reg_position
;
319 reg_position
-= mips_abi_regsize (gdbarch
);
323 /* Fill in the offsets for the registers which float_mask says were
326 CORE_ADDR reg_position
= (cache
->base
327 + PROC_FREG_OFFSET (proc_desc
));
329 /* Fill in the offsets for the float registers which float_mask
331 for (ireg
= MIPS_NUMREGS
- 1; float_mask
; --ireg
, float_mask
<<= 1)
332 if (float_mask
& 0x80000000)
334 if (mips_abi_regsize (gdbarch
) == 4
335 && TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
)
337 /* On a big endian 32 bit ABI, floating point registers
338 are paired to form doubles such that the most
339 significant part is in $f[N+1] and the least
340 significant in $f[N] vis: $f[N+1] ||| $f[N]. The
341 registers are also spilled as a pair and stored as a
344 When little-endian the least significant part is
345 stored first leading to the memory order $f[N] and
348 Unfortunately, when big-endian the most significant
349 part of the double is stored first, and the least
350 significant is stored second. This leads to the
351 registers being ordered in memory as firt $f[N+1] and
354 For the big-endian case make certain that the
355 addresses point at the correct (swapped) locations
356 $f[N] and $f[N+1] pair (keep in mind that
357 reg_position is decremented each time through the
360 cache
->saved_regs
[NUM_REGS
+ mips_regnum (current_gdbarch
)->fp0
+ ireg
]
361 .addr
= reg_position
- mips_abi_regsize (gdbarch
);
363 cache
->saved_regs
[NUM_REGS
+ mips_regnum (current_gdbarch
)->fp0
+ ireg
]
364 .addr
= reg_position
+ mips_abi_regsize (gdbarch
);
367 cache
->saved_regs
[NUM_REGS
+ mips_regnum (current_gdbarch
)->fp0
+ ireg
]
368 .addr
= reg_position
;
369 reg_position
-= mips_abi_regsize (gdbarch
);
372 cache
->saved_regs
[NUM_REGS
+ mips_regnum (current_gdbarch
)->pc
]
373 = cache
->saved_regs
[NUM_REGS
+ MIPS_RA_REGNUM
];
376 /* SP_REGNUM, contains the value and not the address. */
377 trad_frame_set_value (cache
->saved_regs
, NUM_REGS
+ MIPS_SP_REGNUM
, cache
->base
);
379 return (*this_cache
);
383 mips_mdebug_frame_this_id (struct frame_info
*next_frame
, void **this_cache
,
384 struct frame_id
*this_id
)
386 struct mips_frame_cache
*info
= mips_mdebug_frame_cache (next_frame
,
388 (*this_id
) = frame_id_build (info
->base
, frame_func_unwind (next_frame
));
392 mips_mdebug_frame_prev_register (struct frame_info
*next_frame
,
394 int regnum
, int *optimizedp
,
395 enum lval_type
*lvalp
, CORE_ADDR
*addrp
,
396 int *realnump
, void *valuep
)
398 struct mips_frame_cache
*info
= mips_mdebug_frame_cache (next_frame
,
400 trad_frame_get_prev_register (next_frame
, info
->saved_regs
, regnum
,
401 optimizedp
, lvalp
, addrp
, realnump
, valuep
);
404 static const struct frame_unwind mips_mdebug_frame_unwind
=
407 mips_mdebug_frame_this_id
,
408 mips_mdebug_frame_prev_register
411 static const struct frame_unwind
*
412 mips_mdebug_frame_sniffer (struct frame_info
*next_frame
)
414 CORE_ADDR pc
= frame_pc_unwind (next_frame
);
415 CORE_ADDR startaddr
= 0;
416 struct mdebug_extra_func_info
*proc_desc
;
419 /* Don't use this on MIPS16. */
420 if (mips_pc_is_mips16 (pc
))
423 /* Only use the mdebug frame unwinder on mdebug frames where all the
424 registers have been saved. Leave hard cases such as no mdebug or
425 in prologue for the heuristic unwinders. */
427 proc_desc
= non_heuristic_proc_desc (pc
, &startaddr
);
428 if (proc_desc
== NULL
)
431 /* Not sure exactly what kernel_trap means, but if it means the
432 kernel saves the registers without a prologue doing it, we better
433 not examine the prologue to see whether registers have been saved
435 kernel_trap
= PROC_REG_MASK (proc_desc
) & 1;
437 return &mips_mdebug_frame_unwind
;
439 /* In any frame other than the innermost or a frame interrupted by a
440 signal, we assume that all registers have been saved. This
441 assumes that all register saves in a function happen before the
442 first function call. */
443 if (!in_prologue (pc
, PROC_LOW_ADDR (proc_desc
)))
444 return &mips_mdebug_frame_unwind
;
450 mips_mdebug_frame_base_address (struct frame_info
*next_frame
,
453 struct mips_frame_cache
*info
= mips_mdebug_frame_cache (next_frame
,
458 static const struct frame_base mips_mdebug_frame_base
= {
459 &mips_mdebug_frame_unwind
,
460 mips_mdebug_frame_base_address
,
461 mips_mdebug_frame_base_address
,
462 mips_mdebug_frame_base_address
465 static const struct frame_base
*
466 mips_mdebug_frame_base_sniffer (struct frame_info
*next_frame
)
468 if (mips_mdebug_frame_sniffer (next_frame
) != NULL
)
469 return &mips_mdebug_frame_base
;
475 mips_mdebug_append_sniffers (struct gdbarch
*gdbarch
)
477 frame_unwind_append_sniffer (gdbarch
, mips_mdebug_frame_sniffer
);
478 frame_base_append_sniffer (gdbarch
, mips_mdebug_frame_base_sniffer
);
482 extern void _initialize_mips_mdebug_tdep (void);
484 _initialize_mips_mdebug_tdep (void)
486 mips_pdr_data
= register_objfile_data ();