Fix null pointer dereference in process_debug_info()
[binutils-gdb.git] / gdb / alpha-mdebug-tdep.c
blobabded2ac192126744b0bf985b721d2024c93f0cc
1 /* Target-dependent mdebug code for the ALPHA architecture.
2 Copyright (C) 1993-2024 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "frame.h"
20 #include "frame-unwind.h"
21 #include "frame-base.h"
22 #include "symtab.h"
23 #include "gdbcore.h"
24 #include "block.h"
25 #include "trad-frame.h"
27 #include "alpha-tdep.h"
28 #include "mdebugread.h"
29 #include "gdbarch.h"
31 /* FIXME: Some of this code should perhaps be merged with mips. */
33 /* Layout of a stack frame on the alpha:
35 | |
36 pdr members: | 7th ... nth arg, |
37 | `pushed' by caller. |
38 | |
39 ----------------|-------------------------------|<-- old_sp == vfp
40 ^ ^ ^ ^ | |
41 | | | | | |
42 | |localoff | Copies of 1st .. 6th |
43 | | | | | argument if necessary. |
44 | | | v | |
45 | | | --- |-------------------------------|<-- LOCALS_ADDRESS
46 | | | | |
47 | | | | Locals and temporaries. |
48 | | | | |
49 | | | |-------------------------------|
50 | | | | |
51 |-fregoffset | Saved float registers. |
52 | | | | F9 |
53 | | | | . |
54 | | | | . |
55 | | | | F2 |
56 | | v | |
57 | | -------|-------------------------------|
58 | | | |
59 | | | Saved registers. |
60 | | | S6 |
61 |-regoffset | . |
62 | | | . |
63 | | | S0 |
64 | | | pdr.pcreg |
65 | v | |
66 | ----------|-------------------------------|
67 | | |
68 frameoffset | Argument build area, gets |
69 | | 7th ... nth arg for any |
70 | | called procedure. |
71 v | |
72 -------------|-------------------------------|<-- sp
73 | |
76 #define PROC_LOW_ADDR(proc) ((proc)->pdr.adr)
77 #define PROC_FRAME_OFFSET(proc) ((proc)->pdr.frameoffset)
78 #define PROC_FRAME_REG(proc) ((proc)->pdr.framereg)
79 #define PROC_REG_MASK(proc) ((proc)->pdr.regmask)
80 #define PROC_FREG_MASK(proc) ((proc)->pdr.fregmask)
81 #define PROC_REG_OFFSET(proc) ((proc)->pdr.regoffset)
82 #define PROC_FREG_OFFSET(proc) ((proc)->pdr.fregoffset)
83 #define PROC_PC_REG(proc) ((proc)->pdr.pcreg)
84 #define PROC_LOCALOFF(proc) ((proc)->pdr.localoff)
86 /* Locate the mdebug PDR for the given PC. Return null if one can't
87 be found; you'll have to fall back to other methods in that case. */
89 static struct mdebug_extra_func_info *
90 find_proc_desc (CORE_ADDR pc)
92 const struct block *b = block_for_pc (pc);
93 struct mdebug_extra_func_info *proc_desc = NULL;
94 struct symbol *sym = NULL;
95 const char *sh_name = NULL;
97 if (b)
99 CORE_ADDR startaddr;
100 find_pc_partial_function (pc, &sh_name, &startaddr, NULL);
102 if (startaddr > b->start ())
103 /* This is the "pathological" case referred to in a comment in
104 print_frame_info. It might be better to move this check into
105 symbol reading. */
106 sym = NULL;
107 else
108 sym = lookup_symbol (MDEBUG_EFI_SYMBOL_NAME, b, SEARCH_LABEL_DOMAIN,
109 0).symbol;
112 if (sym)
114 proc_desc = (struct mdebug_extra_func_info *) sym->value_bytes ();
116 /* Correct incorrect setjmp procedure descriptor from the library
117 to make backtrace through setjmp work. */
118 if (proc_desc->pdr.pcreg == 0
119 && strcmp (sh_name, "setjmp") == 0)
121 proc_desc->pdr.pcreg = ALPHA_RA_REGNUM;
122 proc_desc->pdr.regmask = 0x80000000;
123 proc_desc->pdr.regoffset = -4;
126 /* If we never found a PDR for this function in symbol reading,
127 then examine prologues to find the information. */
128 if (proc_desc->pdr.framereg == -1)
129 proc_desc = NULL;
132 return proc_desc;
135 /* Return a non-zero result if the function is frameless; zero otherwise. */
137 static int
138 alpha_mdebug_frameless (struct mdebug_extra_func_info *proc_desc)
140 return (PROC_FRAME_REG (proc_desc) == ALPHA_SP_REGNUM
141 && PROC_FRAME_OFFSET (proc_desc) == 0);
144 /* This returns the PC of the first inst after the prologue. If we can't
145 find the prologue, then return 0. */
147 static CORE_ADDR
148 alpha_mdebug_after_prologue (CORE_ADDR pc,
149 struct mdebug_extra_func_info *proc_desc)
151 if (proc_desc)
153 /* If function is frameless, then we need to do it the hard way. I
154 strongly suspect that frameless always means prologueless... */
155 if (alpha_mdebug_frameless (proc_desc))
156 return 0;
159 return alpha_after_prologue (pc);
162 /* Return non-zero if we *might* be in a function prologue. Return zero
163 if we are definitively *not* in a function prologue. */
165 static int
166 alpha_mdebug_in_prologue (CORE_ADDR pc,
167 struct mdebug_extra_func_info *proc_desc)
169 CORE_ADDR after_prologue_pc = alpha_mdebug_after_prologue (pc, proc_desc);
170 return (after_prologue_pc == 0 || pc < after_prologue_pc);
174 /* Frame unwinder that reads mdebug PDRs. */
176 struct alpha_mdebug_unwind_cache
178 struct mdebug_extra_func_info *proc_desc;
179 CORE_ADDR vfp;
180 trad_frame_saved_reg *saved_regs;
183 /* Extract all of the information about the frame from PROC_DESC
184 and store the resulting register save locations in the structure. */
186 static struct alpha_mdebug_unwind_cache *
187 alpha_mdebug_frame_unwind_cache (const frame_info_ptr &this_frame,
188 void **this_prologue_cache)
190 struct alpha_mdebug_unwind_cache *info;
191 struct mdebug_extra_func_info *proc_desc;
192 ULONGEST vfp;
193 CORE_ADDR pc, reg_position;
194 unsigned long mask;
195 int ireg, returnreg;
197 if (*this_prologue_cache)
198 return (struct alpha_mdebug_unwind_cache *) *this_prologue_cache;
200 info = FRAME_OBSTACK_ZALLOC (struct alpha_mdebug_unwind_cache);
201 *this_prologue_cache = info;
202 pc = get_frame_address_in_block (this_frame);
204 /* ??? We don't seem to be able to cache the lookup of the PDR
205 from alpha_mdebug_frame_p. It'd be nice if we could change
206 the arguments to that function. Oh well. */
207 proc_desc = find_proc_desc (pc);
208 info->proc_desc = proc_desc;
209 gdb_assert (proc_desc != NULL);
211 info->saved_regs = trad_frame_alloc_saved_regs (this_frame);
213 /* The VFP of the frame is at FRAME_REG+FRAME_OFFSET. */
214 vfp = get_frame_register_unsigned (this_frame, PROC_FRAME_REG (proc_desc));
215 vfp += PROC_FRAME_OFFSET (info->proc_desc);
216 info->vfp = vfp;
218 /* Fill in the offsets for the registers which gen_mask says were saved. */
220 reg_position = vfp + PROC_REG_OFFSET (proc_desc);
221 mask = PROC_REG_MASK (proc_desc);
222 returnreg = PROC_PC_REG (proc_desc);
224 /* Note that RA is always saved first, regardless of its actual
225 register number. */
226 if (mask & (1 << returnreg))
228 /* Clear bit for RA so we don't save it again later. */
229 mask &= ~(1 << returnreg);
231 info->saved_regs[returnreg].set_addr (reg_position);
232 reg_position += 8;
235 for (ireg = 0; ireg <= 31; ++ireg)
236 if (mask & (1 << ireg))
238 info->saved_regs[ireg].set_addr (reg_position);
239 reg_position += 8;
242 reg_position = vfp + PROC_FREG_OFFSET (proc_desc);
243 mask = PROC_FREG_MASK (proc_desc);
245 for (ireg = 0; ireg <= 31; ++ireg)
246 if (mask & (1 << ireg))
248 info->saved_regs[ALPHA_FP0_REGNUM + ireg].set_addr (reg_position);
249 reg_position += 8;
252 /* The stack pointer of the previous frame is computed by popping
253 the current stack frame. */
254 if (!info->saved_regs[ALPHA_SP_REGNUM].is_addr ())
255 info->saved_regs[ALPHA_SP_REGNUM].set_value (vfp);
257 return info;
260 /* Given a GDB frame, determine the address of the calling function's
261 frame. This will be used to create a new GDB frame struct. */
263 static void
264 alpha_mdebug_frame_this_id (const frame_info_ptr &this_frame,
265 void **this_prologue_cache,
266 struct frame_id *this_id)
268 struct alpha_mdebug_unwind_cache *info
269 = alpha_mdebug_frame_unwind_cache (this_frame, this_prologue_cache);
271 *this_id = frame_id_build (info->vfp, get_frame_func (this_frame));
274 /* Retrieve the value of REGNUM in FRAME. Don't give up! */
276 static struct value *
277 alpha_mdebug_frame_prev_register (const frame_info_ptr &this_frame,
278 void **this_prologue_cache, int regnum)
280 struct alpha_mdebug_unwind_cache *info
281 = alpha_mdebug_frame_unwind_cache (this_frame, this_prologue_cache);
283 /* The PC of the previous frame is stored in the link register of
284 the current frame. Frob regnum so that we pull the value from
285 the correct place. */
286 if (regnum == ALPHA_PC_REGNUM)
287 regnum = PROC_PC_REG (info->proc_desc);
289 return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
292 /* Return a non-zero result if the size of the stack frame exceeds the
293 maximum debuggable frame size (512 Kbytes); zero otherwise. */
295 static int
296 alpha_mdebug_max_frame_size_exceeded (struct mdebug_extra_func_info *proc_desc)
298 /* If frame offset is null, we can be in two cases: either the
299 function is frameless (the stack frame is null) or its
300 frame exceeds the maximum debuggable frame size (512 Kbytes). */
302 return (PROC_FRAME_OFFSET (proc_desc) == 0
303 && !alpha_mdebug_frameless (proc_desc));
306 static int
307 alpha_mdebug_frame_sniffer (const struct frame_unwind *self,
308 const frame_info_ptr &this_frame,
309 void **this_cache)
311 CORE_ADDR pc = get_frame_address_in_block (this_frame);
312 struct mdebug_extra_func_info *proc_desc;
314 /* If this PC does not map to a PDR, then clearly this isn't an
315 mdebug frame. */
316 proc_desc = find_proc_desc (pc);
317 if (proc_desc == NULL)
318 return 0;
320 /* If we're in the prologue, the PDR for this frame is not yet valid.
321 Say no here and we'll fall back on the heuristic unwinder. */
322 if (alpha_mdebug_in_prologue (pc, proc_desc))
323 return 0;
325 /* If the maximum debuggable frame size has been exceeded, the
326 proc desc is bogus. Fall back on the heuristic unwinder. */
327 if (alpha_mdebug_max_frame_size_exceeded (proc_desc))
328 return 0;
330 return 1;
333 static const struct frame_unwind alpha_mdebug_frame_unwind =
335 "alpha mdebug",
336 NORMAL_FRAME,
337 default_frame_unwind_stop_reason,
338 alpha_mdebug_frame_this_id,
339 alpha_mdebug_frame_prev_register,
340 NULL,
341 alpha_mdebug_frame_sniffer
344 static CORE_ADDR
345 alpha_mdebug_frame_base_address (const frame_info_ptr &this_frame,
346 void **this_prologue_cache)
348 struct alpha_mdebug_unwind_cache *info
349 = alpha_mdebug_frame_unwind_cache (this_frame, this_prologue_cache);
351 return info->vfp;
354 static CORE_ADDR
355 alpha_mdebug_frame_locals_address (const frame_info_ptr &this_frame,
356 void **this_prologue_cache)
358 struct alpha_mdebug_unwind_cache *info
359 = alpha_mdebug_frame_unwind_cache (this_frame, this_prologue_cache);
361 return info->vfp - PROC_LOCALOFF (info->proc_desc);
364 static CORE_ADDR
365 alpha_mdebug_frame_args_address (const frame_info_ptr &this_frame,
366 void **this_prologue_cache)
368 struct alpha_mdebug_unwind_cache *info
369 = alpha_mdebug_frame_unwind_cache (this_frame, this_prologue_cache);
371 return info->vfp - ALPHA_NUM_ARG_REGS * 8;
374 static const struct frame_base alpha_mdebug_frame_base = {
375 &alpha_mdebug_frame_unwind,
376 alpha_mdebug_frame_base_address,
377 alpha_mdebug_frame_locals_address,
378 alpha_mdebug_frame_args_address
381 static const struct frame_base *
382 alpha_mdebug_frame_base_sniffer (const frame_info_ptr &this_frame)
384 CORE_ADDR pc = get_frame_address_in_block (this_frame);
385 struct mdebug_extra_func_info *proc_desc;
387 /* If this PC does not map to a PDR, then clearly this isn't an
388 mdebug frame. */
389 proc_desc = find_proc_desc (pc);
390 if (proc_desc == NULL)
391 return NULL;
393 /* If the maximum debuggable frame size has been exceeded, the
394 proc desc is bogus. Fall back on the heuristic unwinder. */
395 if (alpha_mdebug_max_frame_size_exceeded (proc_desc))
396 return 0;
398 return &alpha_mdebug_frame_base;
402 void
403 alpha_mdebug_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
405 frame_unwind_append_unwinder (gdbarch, &alpha_mdebug_frame_unwind);
406 frame_base_append_sniffer (gdbarch, alpha_mdebug_frame_base_sniffer);