2004-10-30 Andrew Cagney <cagney@gnu.org>
[binutils-gdb.git] / gdb / mips-mdebug-tdep.c
1 /* Target-dependent code for the MDEBUG MIPS architecture, for GDB,
2 the GNU Debugger.
3
4 Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
5 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software
6 Foundation, Inc.
7
8 This file is part of GDB.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
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. */
24
25 #include "defs.h"
26 #include "frame.h"
27 #include "mips-tdep.h"
28 #include "trad-frame.h"
29 #include "block.h"
30 #include "symtab.h"
31 #include "objfiles.h"
32 #include "elf/mips.h"
33 #include "elf-bfd.h"
34 #include "gdb_assert.h"
35 #include "frame-unwind.h"
36 #include "frame-base.h"
37 #include "mips-mdebug-tdep.h"
38
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
51
52 struct mips_objfile_private
53 {
54 bfd_size_type size;
55 char *contents;
56 };
57
58 /* Global used to communicate between non_heuristic_proc_desc and
59 compare_pdr_entries within qsort (). */
60 static bfd *the_bfd;
61
62 static int
63 compare_pdr_entries (const void *a, const void *b)
64 {
65 CORE_ADDR lhs = bfd_get_32 (the_bfd, (bfd_byte *) a);
66 CORE_ADDR rhs = bfd_get_32 (the_bfd, (bfd_byte *) b);
67
68 if (lhs < rhs)
69 return -1;
70 else if (lhs == rhs)
71 return 0;
72 else
73 return 1;
74 }
75
76 static const struct objfile_data *mips_pdr_data;
77
78 static struct mdebug_extra_func_info *
79 non_heuristic_proc_desc (CORE_ADDR pc, CORE_ADDR *addrptr)
80 {
81 CORE_ADDR startaddr;
82 struct mdebug_extra_func_info *proc_desc;
83 struct block *b = block_for_pc (pc);
84 struct symbol *sym;
85 struct obj_section *sec;
86 struct mips_objfile_private *priv;
87
88 find_pc_partial_function (pc, NULL, &startaddr, NULL);
89 if (addrptr)
90 *addrptr = startaddr;
91
92 priv = NULL;
93
94 sec = find_pc_section (pc);
95 if (sec != NULL)
96 {
97 priv = (struct mips_objfile_private *) objfile_data (sec->objfile, mips_pdr_data);
98
99 /* Search the ".pdr" section generated by GAS. This includes most of
100 the information normally found in ECOFF PDRs. */
101
102 the_bfd = sec->objfile->obfd;
103 if (priv == NULL
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))
107 {
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). */
111
112 priv = obstack_alloc (&sec->objfile->objfile_obstack,
113 sizeof (struct mips_objfile_private));
114 priv->size = 0;
115 set_objfile_data (sec->objfile, mips_pdr_data, priv);
116 }
117 else if (priv == NULL)
118 {
119 asection *bfdsec;
120
121 priv = obstack_alloc (&sec->objfile->objfile_obstack,
122 sizeof (struct mips_objfile_private));
123
124 bfdsec = bfd_get_section_by_name (sec->objfile->obfd, ".pdr");
125 if (bfdsec != NULL)
126 {
127 priv->size = bfd_section_size (sec->objfile->obfd, bfdsec);
128 priv->contents = obstack_alloc (&sec->objfile->objfile_obstack,
129 priv->size);
130 bfd_get_section_contents (sec->objfile->obfd, bfdsec,
131 priv->contents, 0, priv->size);
132
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
136 binary search. */
137 qsort (priv->contents, priv->size / 32, 32,
138 compare_pdr_entries);
139 }
140 else
141 priv->size = 0;
142
143 set_objfile_data (sec->objfile, mips_pdr_data, priv);
144 }
145 the_bfd = NULL;
146
147 if (priv->size != 0)
148 {
149 int low, mid, high;
150 char *ptr;
151 CORE_ADDR pdr_pc;
152
153 low = 0;
154 high = priv->size / 32;
155
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. */
163
164 /* First, find the last .pdr entry starting at or before PC. */
165 do
166 {
167 mid = (low + high) / 2;
168
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));
173
174 if (pdr_pc > pc)
175 high = mid;
176 else
177 low = mid + 1;
178 }
179 while (low != high);
180
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
184 greater than PC. */
185 if (low > 0)
186 {
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));
191 }
192
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. */
202
203 if (pc >= pdr_pc && pdr_pc >= startaddr)
204 {
205 struct symbol *sym = find_pc_function (pc);
206
207 if (addrptr)
208 *addrptr = pdr_pc;
209
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;
215
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,
219 ptr + 24);
220 PROC_REG_MASK (proc_desc) = bfd_get_32 (sec->objfile->obfd,
221 ptr + 4);
222 PROC_FREG_MASK (proc_desc) = bfd_get_32 (sec->objfile->obfd,
223 ptr + 12);
224 PROC_REG_OFFSET (proc_desc) = bfd_get_32 (sec->objfile->obfd,
225 ptr + 8);
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,
229 ptr + 28);
230 proc_desc->pdr.isym = (long) sym;
231
232 return proc_desc;
233 }
234 }
235 }
236
237 if (b == NULL)
238 return NULL;
239
240 if (startaddr > BLOCK_START (b))
241 {
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
244 symbol reading. */
245 return NULL;
246 }
247
248 sym = lookup_symbol (MDEBUG_EFI_SYMBOL_NAME, b, LABEL_DOMAIN, 0, NULL);
249
250 /* If we never found a PDR for this function in symbol reading, then
251 examine prologues to find the information. */
252 if (sym)
253 {
254 proc_desc = (struct mdebug_extra_func_info *) SYMBOL_VALUE (sym);
255 if (PROC_FRAME_REG (proc_desc) == -1)
256 return NULL;
257 else
258 return proc_desc;
259 }
260 else
261 return NULL;
262 }
263
264 struct mips_frame_cache
265 {
266 CORE_ADDR base;
267 struct trad_frame_saved_reg *saved_regs;
268 };
269
270 static struct mips_frame_cache *
271 mips_mdebug_frame_cache (struct frame_info *next_frame, void **this_cache)
272 {
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 */
279 int kernel_trap;
280 /* What registers have been saved? Bitmasks. */
281 unsigned long gen_mask, float_mask;
282
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);
288
289 /* Get the mdebug proc descriptor. */
290 proc_desc = non_heuristic_proc_desc (frame_pc_unwind (next_frame),
291 &startaddr);
292 /* Must be true. This is only called when the sniffer detected a
293 proc descriptor. */
294 gdb_assert (proc_desc != NULL);
295
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));
299
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);
303
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))
307 || kernel_trap);
308
309 /* Fill in the offsets for the registers which gen_mask says were
310 saved. */
311 {
312 CORE_ADDR reg_position = (cache->base + PROC_REG_OFFSET (proc_desc));
313 int ireg;
314
315 for (ireg = MIPS_NUMREGS - 1; gen_mask; --ireg, gen_mask <<= 1)
316 if (gen_mask & 0x80000000)
317 {
318 cache->saved_regs[NUM_REGS + ireg].addr = reg_position;
319 reg_position -= mips_abi_regsize (gdbarch);
320 }
321 }
322
323 /* Fill in the offsets for the registers which float_mask says were
324 saved. */
325 {
326 CORE_ADDR reg_position = (cache->base
327 + PROC_FREG_OFFSET (proc_desc));
328 int ireg;
329 /* Fill in the offsets for the float registers which float_mask
330 says were saved. */
331 for (ireg = MIPS_NUMREGS - 1; float_mask; --ireg, float_mask <<= 1)
332 if (float_mask & 0x80000000)
333 {
334 if (mips_abi_regsize (gdbarch) == 4
335 && TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
336 {
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
342 double.
343
344 When little-endian the least significant part is
345 stored first leading to the memory order $f[N] and
346 then $f[N+1].
347
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
352 then $f[N].
353
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
358 loop). */
359 if ((ireg & 1))
360 cache->saved_regs[NUM_REGS + mips_regnum (current_gdbarch)->fp0 + ireg]
361 .addr = reg_position - mips_abi_regsize (gdbarch);
362 else
363 cache->saved_regs[NUM_REGS + mips_regnum (current_gdbarch)->fp0 + ireg]
364 .addr = reg_position + mips_abi_regsize (gdbarch);
365 }
366 else
367 cache->saved_regs[NUM_REGS + mips_regnum (current_gdbarch)->fp0 + ireg]
368 .addr = reg_position;
369 reg_position -= mips_abi_regsize (gdbarch);
370 }
371
372 cache->saved_regs[NUM_REGS + mips_regnum (current_gdbarch)->pc]
373 = cache->saved_regs[NUM_REGS + MIPS_RA_REGNUM];
374 }
375
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);
378
379 return (*this_cache);
380 }
381
382 static void
383 mips_mdebug_frame_this_id (struct frame_info *next_frame, void **this_cache,
384 struct frame_id *this_id)
385 {
386 struct mips_frame_cache *info = mips_mdebug_frame_cache (next_frame,
387 this_cache);
388 (*this_id) = frame_id_build (info->base, frame_func_unwind (next_frame));
389 }
390
391 static void
392 mips_mdebug_frame_prev_register (struct frame_info *next_frame,
393 void **this_cache,
394 int regnum, int *optimizedp,
395 enum lval_type *lvalp, CORE_ADDR *addrp,
396 int *realnump, void *valuep)
397 {
398 struct mips_frame_cache *info = mips_mdebug_frame_cache (next_frame,
399 this_cache);
400 trad_frame_get_prev_register (next_frame, info->saved_regs, regnum,
401 optimizedp, lvalp, addrp, realnump, valuep);
402 }
403
404 static const struct frame_unwind mips_mdebug_frame_unwind =
405 {
406 NORMAL_FRAME,
407 mips_mdebug_frame_this_id,
408 mips_mdebug_frame_prev_register
409 };
410
411 static const struct frame_unwind *
412 mips_mdebug_frame_sniffer (struct frame_info *next_frame)
413 {
414 CORE_ADDR pc = frame_pc_unwind (next_frame);
415 CORE_ADDR startaddr = 0;
416 struct mdebug_extra_func_info *proc_desc;
417 int kernel_trap;
418
419 /* Don't use this on MIPS16. */
420 if (mips_pc_is_mips16 (pc))
421 return NULL;
422
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. */
426
427 proc_desc = non_heuristic_proc_desc (pc, &startaddr);
428 if (proc_desc == NULL)
429 return NULL;
430
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
434 yet. */
435 kernel_trap = PROC_REG_MASK (proc_desc) & 1;
436 if (kernel_trap)
437 return &mips_mdebug_frame_unwind;
438
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;
445
446 return NULL;
447 }
448
449 static CORE_ADDR
450 mips_mdebug_frame_base_address (struct frame_info *next_frame,
451 void **this_cache)
452 {
453 struct mips_frame_cache *info = mips_mdebug_frame_cache (next_frame,
454 this_cache);
455 return info->base;
456 }
457
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
463 };
464
465 static const struct frame_base *
466 mips_mdebug_frame_base_sniffer (struct frame_info *next_frame)
467 {
468 if (mips_mdebug_frame_sniffer (next_frame) != NULL)
469 return &mips_mdebug_frame_base;
470 else
471 return NULL;
472 }
473
474 void
475 mips_mdebug_append_sniffers (struct gdbarch *gdbarch)
476 {
477 frame_unwind_append_sniffer (gdbarch, mips_mdebug_frame_sniffer);
478 frame_base_append_sniffer (gdbarch, mips_mdebug_frame_base_sniffer);
479 }
480
481
482 extern void _initialize_mips_mdebug_tdep (void);
483 void
484 _initialize_mips_mdebug_tdep (void)
485 {
486 mips_pdr_data = register_objfile_data ();
487 }