arch-utils.c revision 1.3 1 1.1 christos /* Dynamic architecture support for GDB, the GNU debugger.
2 1.1 christos
3 1.3 christos Copyright (C) 1998-2015 Free Software Foundation, Inc.
4 1.1 christos
5 1.1 christos This file is part of GDB.
6 1.1 christos
7 1.1 christos This program is free software; you can redistribute it and/or modify
8 1.1 christos it under the terms of the GNU General Public License as published by
9 1.1 christos the Free Software Foundation; either version 3 of the License, or
10 1.1 christos (at your option) any later version.
11 1.1 christos
12 1.1 christos This program is distributed in the hope that it will be useful,
13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 1.1 christos GNU General Public License for more details.
16 1.1 christos
17 1.1 christos You should have received a copy of the GNU General Public License
18 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 1.1 christos
20 1.1 christos #include "defs.h"
21 1.1 christos
22 1.1 christos #include "arch-utils.h"
23 1.1 christos #include "buildsym.h"
24 1.1 christos #include "gdbcmd.h"
25 1.1 christos #include "inferior.h" /* enum CALL_DUMMY_LOCATION et al. */
26 1.3 christos #include "infrun.h"
27 1.1 christos #include "regcache.h"
28 1.1 christos #include "sim-regno.h"
29 1.1 christos #include "gdbcore.h"
30 1.1 christos #include "osabi.h"
31 1.1 christos #include "target-descriptions.h"
32 1.1 christos #include "objfiles.h"
33 1.1 christos #include "language.h"
34 1.3 christos #include "symtab.h"
35 1.1 christos
36 1.1 christos #include "version.h"
37 1.1 christos
38 1.1 christos #include "floatformat.h"
39 1.1 christos
40 1.1 christos
41 1.1 christos struct displaced_step_closure *
42 1.1 christos simple_displaced_step_copy_insn (struct gdbarch *gdbarch,
43 1.1 christos CORE_ADDR from, CORE_ADDR to,
44 1.1 christos struct regcache *regs)
45 1.1 christos {
46 1.1 christos size_t len = gdbarch_max_insn_length (gdbarch);
47 1.1 christos gdb_byte *buf = xmalloc (len);
48 1.1 christos
49 1.1 christos read_memory (from, buf, len);
50 1.1 christos write_memory (to, buf, len);
51 1.1 christos
52 1.1 christos if (debug_displaced)
53 1.1 christos {
54 1.1 christos fprintf_unfiltered (gdb_stdlog, "displaced: copy %s->%s: ",
55 1.1 christos paddress (gdbarch, from), paddress (gdbarch, to));
56 1.1 christos displaced_step_dump_bytes (gdb_stdlog, buf, len);
57 1.1 christos }
58 1.1 christos
59 1.1 christos return (struct displaced_step_closure *) buf;
60 1.1 christos }
61 1.1 christos
62 1.1 christos
63 1.1 christos void
64 1.1 christos simple_displaced_step_free_closure (struct gdbarch *gdbarch,
65 1.1 christos struct displaced_step_closure *closure)
66 1.1 christos {
67 1.1 christos xfree (closure);
68 1.1 christos }
69 1.1 christos
70 1.1 christos int
71 1.1 christos default_displaced_step_hw_singlestep (struct gdbarch *gdbarch,
72 1.1 christos struct displaced_step_closure *closure)
73 1.1 christos {
74 1.1 christos return !gdbarch_software_single_step_p (gdbarch);
75 1.1 christos }
76 1.1 christos
77 1.1 christos CORE_ADDR
78 1.1 christos displaced_step_at_entry_point (struct gdbarch *gdbarch)
79 1.1 christos {
80 1.1 christos CORE_ADDR addr;
81 1.1 christos int bp_len;
82 1.1 christos
83 1.1 christos addr = entry_point_address ();
84 1.1 christos
85 1.1 christos /* Inferior calls also use the entry point as a breakpoint location.
86 1.1 christos We don't want displaced stepping to interfere with those
87 1.1 christos breakpoints, so leave space. */
88 1.1 christos gdbarch_breakpoint_from_pc (gdbarch, &addr, &bp_len);
89 1.1 christos addr += bp_len * 2;
90 1.1 christos
91 1.1 christos return addr;
92 1.1 christos }
93 1.1 christos
94 1.1 christos int
95 1.1 christos legacy_register_sim_regno (struct gdbarch *gdbarch, int regnum)
96 1.1 christos {
97 1.1 christos /* Only makes sense to supply raw registers. */
98 1.1 christos gdb_assert (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch));
99 1.1 christos /* NOTE: cagney/2002-05-13: The old code did it this way and it is
100 1.1 christos suspected that some GDB/SIM combinations may rely on this
101 1.1 christos behavour. The default should be one2one_register_sim_regno
102 1.1 christos (below). */
103 1.1 christos if (gdbarch_register_name (gdbarch, regnum) != NULL
104 1.1 christos && gdbarch_register_name (gdbarch, regnum)[0] != '\0')
105 1.1 christos return regnum;
106 1.1 christos else
107 1.1 christos return LEGACY_SIM_REGNO_IGNORE;
108 1.1 christos }
109 1.1 christos
110 1.1 christos CORE_ADDR
111 1.1 christos generic_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
112 1.1 christos {
113 1.1 christos return 0;
114 1.1 christos }
115 1.1 christos
116 1.1 christos CORE_ADDR
117 1.1 christos generic_skip_solib_resolver (struct gdbarch *gdbarch, CORE_ADDR pc)
118 1.1 christos {
119 1.1 christos return 0;
120 1.1 christos }
121 1.1 christos
122 1.1 christos int
123 1.1 christos generic_in_solib_return_trampoline (struct gdbarch *gdbarch,
124 1.1 christos CORE_ADDR pc, const char *name)
125 1.1 christos {
126 1.1 christos return 0;
127 1.1 christos }
128 1.1 christos
129 1.1 christos int
130 1.1 christos generic_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
131 1.1 christos {
132 1.1 christos return 0;
133 1.1 christos }
134 1.1 christos
135 1.1 christos /* Helper functions for gdbarch_inner_than */
136 1.1 christos
137 1.1 christos int
138 1.1 christos core_addr_lessthan (CORE_ADDR lhs, CORE_ADDR rhs)
139 1.1 christos {
140 1.1 christos return (lhs < rhs);
141 1.1 christos }
142 1.1 christos
143 1.1 christos int
144 1.1 christos core_addr_greaterthan (CORE_ADDR lhs, CORE_ADDR rhs)
145 1.1 christos {
146 1.1 christos return (lhs > rhs);
147 1.1 christos }
148 1.1 christos
149 1.1 christos /* Misc helper functions for targets. */
150 1.1 christos
151 1.1 christos CORE_ADDR
152 1.1 christos core_addr_identity (struct gdbarch *gdbarch, CORE_ADDR addr)
153 1.1 christos {
154 1.1 christos return addr;
155 1.1 christos }
156 1.1 christos
157 1.1 christos CORE_ADDR
158 1.1 christos convert_from_func_ptr_addr_identity (struct gdbarch *gdbarch, CORE_ADDR addr,
159 1.1 christos struct target_ops *targ)
160 1.1 christos {
161 1.1 christos return addr;
162 1.1 christos }
163 1.1 christos
164 1.1 christos int
165 1.1 christos no_op_reg_to_regnum (struct gdbarch *gdbarch, int reg)
166 1.1 christos {
167 1.1 christos return reg;
168 1.1 christos }
169 1.1 christos
170 1.1 christos void
171 1.3 christos default_coff_make_msymbol_special (int val, struct minimal_symbol *msym)
172 1.1 christos {
173 1.1 christos return;
174 1.1 christos }
175 1.1 christos
176 1.3 christos /* See arch-utils.h. */
177 1.3 christos
178 1.1 christos void
179 1.3 christos default_make_symbol_special (struct symbol *sym, struct objfile *objfile)
180 1.1 christos {
181 1.1 christos return;
182 1.1 christos }
183 1.1 christos
184 1.3 christos /* See arch-utils.h. */
185 1.3 christos
186 1.3 christos CORE_ADDR
187 1.3 christos default_adjust_dwarf2_addr (CORE_ADDR pc)
188 1.3 christos {
189 1.3 christos return pc;
190 1.3 christos }
191 1.3 christos
192 1.3 christos /* See arch-utils.h. */
193 1.3 christos
194 1.3 christos CORE_ADDR
195 1.3 christos default_adjust_dwarf2_line (CORE_ADDR addr, int rel)
196 1.3 christos {
197 1.3 christos return addr;
198 1.3 christos }
199 1.3 christos
200 1.1 christos int
201 1.1 christos cannot_register_not (struct gdbarch *gdbarch, int regnum)
202 1.1 christos {
203 1.1 christos return 0;
204 1.1 christos }
205 1.1 christos
206 1.1 christos /* Legacy version of target_virtual_frame_pointer(). Assumes that
207 1.1 christos there is an gdbarch_deprecated_fp_regnum and that it is the same,
208 1.1 christos cooked or raw. */
209 1.1 christos
210 1.1 christos void
211 1.1 christos legacy_virtual_frame_pointer (struct gdbarch *gdbarch,
212 1.1 christos CORE_ADDR pc,
213 1.1 christos int *frame_regnum,
214 1.1 christos LONGEST *frame_offset)
215 1.1 christos {
216 1.1 christos /* FIXME: cagney/2002-09-13: This code is used when identifying the
217 1.1 christos frame pointer of the current PC. It is assuming that a single
218 1.1 christos register and an offset can determine this. I think it should
219 1.1 christos instead generate a byte code expression as that would work better
220 1.1 christos with things like Dwarf2's CFI. */
221 1.1 christos if (gdbarch_deprecated_fp_regnum (gdbarch) >= 0
222 1.1 christos && gdbarch_deprecated_fp_regnum (gdbarch)
223 1.1 christos < gdbarch_num_regs (gdbarch))
224 1.1 christos *frame_regnum = gdbarch_deprecated_fp_regnum (gdbarch);
225 1.1 christos else if (gdbarch_sp_regnum (gdbarch) >= 0
226 1.1 christos && gdbarch_sp_regnum (gdbarch)
227 1.1 christos < gdbarch_num_regs (gdbarch))
228 1.1 christos *frame_regnum = gdbarch_sp_regnum (gdbarch);
229 1.1 christos else
230 1.1 christos /* Should this be an internal error? I guess so, it is reflecting
231 1.1 christos an architectural limitation in the current design. */
232 1.1 christos internal_error (__FILE__, __LINE__,
233 1.1 christos _("No virtual frame pointer available"));
234 1.1 christos *frame_offset = 0;
235 1.1 christos }
236 1.1 christos
237 1.1 christos
238 1.1 christos int
240 1.1 christos generic_convert_register_p (struct gdbarch *gdbarch, int regnum,
241 1.1 christos struct type *type)
242 1.1 christos {
243 1.1 christos return 0;
244 1.1 christos }
245 1.1 christos
246 1.1 christos int
247 1.1 christos default_stabs_argument_has_addr (struct gdbarch *gdbarch, struct type *type)
248 1.1 christos {
249 1.1 christos return 0;
250 1.1 christos }
251 1.1 christos
252 1.1 christos int
253 1.1 christos generic_instruction_nullified (struct gdbarch *gdbarch,
254 1.1 christos struct regcache *regcache)
255 1.1 christos {
256 1.1 christos return 0;
257 1.1 christos }
258 1.1 christos
259 1.1 christos int
260 1.1 christos default_remote_register_number (struct gdbarch *gdbarch,
261 1.1 christos int regno)
262 1.1 christos {
263 1.1 christos return regno;
264 1.1 christos }
265 1.3 christos
266 1.3 christos /* See arch-utils.h. */
267 1.3 christos
268 1.3 christos int
269 1.3 christos default_vsyscall_range (struct gdbarch *gdbarch, struct mem_range *range)
270 1.3 christos {
271 1.3 christos return 0;
272 1.3 christos }
273 1.1 christos
274 1.1 christos
275 1.1 christos /* Functions to manipulate the endianness of the target. */
277 1.1 christos
278 1.1 christos static int target_byte_order_user = BFD_ENDIAN_UNKNOWN;
279 1.1 christos
280 1.1 christos static const char endian_big[] = "big";
281 1.1 christos static const char endian_little[] = "little";
282 1.1 christos static const char endian_auto[] = "auto";
283 1.1 christos static const char *const endian_enum[] =
284 1.1 christos {
285 1.1 christos endian_big,
286 1.1 christos endian_little,
287 1.1 christos endian_auto,
288 1.1 christos NULL,
289 1.1 christos };
290 1.1 christos static const char *set_endian_string;
291 1.1 christos
292 1.1 christos enum bfd_endian
293 1.1 christos selected_byte_order (void)
294 1.1 christos {
295 1.1 christos return target_byte_order_user;
296 1.1 christos }
297 1.1 christos
298 1.1 christos /* Called by ``show endian''. */
299 1.1 christos
300 1.1 christos static void
301 1.1 christos show_endian (struct ui_file *file, int from_tty, struct cmd_list_element *c,
302 1.1 christos const char *value)
303 1.1 christos {
304 1.1 christos if (target_byte_order_user == BFD_ENDIAN_UNKNOWN)
305 1.1 christos if (gdbarch_byte_order (get_current_arch ()) == BFD_ENDIAN_BIG)
306 1.1 christos fprintf_unfiltered (file, _("The target endianness is set automatically "
307 1.1 christos "(currently big endian)\n"));
308 1.1 christos else
309 1.1 christos fprintf_unfiltered (file, _("The target endianness is set automatically "
310 1.1 christos "(currently little endian)\n"));
311 1.1 christos else
312 1.1 christos if (target_byte_order_user == BFD_ENDIAN_BIG)
313 1.1 christos fprintf_unfiltered (file,
314 1.1 christos _("The target is assumed to be big endian\n"));
315 1.1 christos else
316 1.1 christos fprintf_unfiltered (file,
317 1.1 christos _("The target is assumed to be little endian\n"));
318 1.1 christos }
319 1.1 christos
320 1.1 christos static void
321 1.1 christos set_endian (char *ignore_args, int from_tty, struct cmd_list_element *c)
322 1.1 christos {
323 1.1 christos struct gdbarch_info info;
324 1.1 christos
325 1.1 christos gdbarch_info_init (&info);
326 1.1 christos
327 1.1 christos if (set_endian_string == endian_auto)
328 1.1 christos {
329 1.1 christos target_byte_order_user = BFD_ENDIAN_UNKNOWN;
330 1.1 christos if (! gdbarch_update_p (info))
331 1.1 christos internal_error (__FILE__, __LINE__,
332 1.1 christos _("set_endian: architecture update failed"));
333 1.1 christos }
334 1.1 christos else if (set_endian_string == endian_little)
335 1.1 christos {
336 1.1 christos info.byte_order = BFD_ENDIAN_LITTLE;
337 1.1 christos if (! gdbarch_update_p (info))
338 1.1 christos printf_unfiltered (_("Little endian target not supported by GDB\n"));
339 1.1 christos else
340 1.1 christos target_byte_order_user = BFD_ENDIAN_LITTLE;
341 1.1 christos }
342 1.1 christos else if (set_endian_string == endian_big)
343 1.1 christos {
344 1.1 christos info.byte_order = BFD_ENDIAN_BIG;
345 1.1 christos if (! gdbarch_update_p (info))
346 1.1 christos printf_unfiltered (_("Big endian target not supported by GDB\n"));
347 1.1 christos else
348 1.1 christos target_byte_order_user = BFD_ENDIAN_BIG;
349 1.1 christos }
350 1.1 christos else
351 1.1 christos internal_error (__FILE__, __LINE__,
352 1.1 christos _("set_endian: bad value"));
353 1.1 christos
354 1.1 christos show_endian (gdb_stdout, from_tty, NULL, NULL);
355 1.1 christos }
356 1.1 christos
357 1.1 christos /* Given SELECTED, a currently selected BFD architecture, and
358 1.1 christos TARGET_DESC, the current target description, return what
359 1.1 christos architecture to use.
360 1.1 christos
361 1.1 christos SELECTED may be NULL, in which case we return the architecture
362 1.1 christos associated with TARGET_DESC. If SELECTED specifies a variant
363 1.1 christos of the architecture associtated with TARGET_DESC, return the
364 1.1 christos more specific of the two.
365 1.1 christos
366 1.1 christos If SELECTED is a different architecture, but it is accepted as
367 1.1 christos compatible by the target, we can use the target architecture.
368 1.1 christos
369 1.1 christos If SELECTED is obviously incompatible, warn the user. */
370 1.1 christos
371 1.1 christos static const struct bfd_arch_info *
372 1.1 christos choose_architecture_for_target (const struct target_desc *target_desc,
373 1.1 christos const struct bfd_arch_info *selected)
374 1.1 christos {
375 1.1 christos const struct bfd_arch_info *from_target = tdesc_architecture (target_desc);
376 1.1 christos const struct bfd_arch_info *compat1, *compat2;
377 1.1 christos
378 1.1 christos if (selected == NULL)
379 1.1 christos return from_target;
380 1.1 christos
381 1.1 christos if (from_target == NULL)
382 1.1 christos return selected;
383 1.1 christos
384 1.1 christos /* struct bfd_arch_info objects are singletons: that is, there's
385 1.1 christos supposed to be exactly one instance for a given machine. So you
386 1.1 christos can tell whether two are equivalent by comparing pointers. */
387 1.1 christos if (from_target == selected)
388 1.1 christos return selected;
389 1.1 christos
390 1.1 christos /* BFD's 'A->compatible (A, B)' functions return zero if A and B are
391 1.1 christos incompatible. But if they are compatible, it returns the 'more
392 1.1 christos featureful' of the two arches. That is, if A can run code
393 1.1 christos written for B, but B can't run code written for A, then it'll
394 1.1 christos return A.
395 1.1 christos
396 1.1 christos Some targets (e.g. MIPS as of 2006-12-04) don't fully
397 1.1 christos implement this, instead always returning NULL or the first
398 1.1 christos argument. We detect that case by checking both directions. */
399 1.1 christos
400 1.1 christos compat1 = selected->compatible (selected, from_target);
401 1.1 christos compat2 = from_target->compatible (from_target, selected);
402 1.1 christos
403 1.1 christos if (compat1 == NULL && compat2 == NULL)
404 1.1 christos {
405 1.1 christos /* BFD considers the architectures incompatible. Check our
406 1.1 christos target description whether it accepts SELECTED as compatible
407 1.1 christos anyway. */
408 1.1 christos if (tdesc_compatible_p (target_desc, selected))
409 1.1 christos return from_target;
410 1.1 christos
411 1.1 christos warning (_("Selected architecture %s is not compatible "
412 1.1 christos "with reported target architecture %s"),
413 1.1 christos selected->printable_name, from_target->printable_name);
414 1.1 christos return selected;
415 1.1 christos }
416 1.1 christos
417 1.1 christos if (compat1 == NULL)
418 1.1 christos return compat2;
419 1.1 christos if (compat2 == NULL)
420 1.1 christos return compat1;
421 1.1 christos if (compat1 == compat2)
422 1.1 christos return compat1;
423 1.1 christos
424 1.1 christos /* If the two didn't match, but one of them was a default
425 1.1 christos architecture, assume the more specific one is correct. This
426 1.1 christos handles the case where an executable or target description just
427 1.1 christos says "mips", but the other knows which MIPS variant. */
428 1.1 christos if (compat1->the_default)
429 1.1 christos return compat2;
430 1.1 christos if (compat2->the_default)
431 1.1 christos return compat1;
432 1.1 christos
433 1.1 christos /* We have no idea which one is better. This is a bug, but not
434 1.1 christos a critical problem; warn the user. */
435 1.1 christos warning (_("Selected architecture %s is ambiguous with "
436 1.1 christos "reported target architecture %s"),
437 1.1 christos selected->printable_name, from_target->printable_name);
438 1.1 christos return selected;
439 1.1 christos }
440 1.1 christos
441 1.1 christos /* Functions to manipulate the architecture of the target. */
442 1.1 christos
443 1.1 christos enum set_arch { set_arch_auto, set_arch_manual };
444 1.1 christos
445 1.1 christos static const struct bfd_arch_info *target_architecture_user;
446 1.1 christos
447 1.1 christos static const char *set_architecture_string;
448 1.1 christos
449 1.1 christos const char *
450 1.1 christos selected_architecture_name (void)
451 1.1 christos {
452 1.1 christos if (target_architecture_user == NULL)
453 1.1 christos return NULL;
454 1.1 christos else
455 1.1 christos return set_architecture_string;
456 1.1 christos }
457 1.1 christos
458 1.1 christos /* Called if the user enters ``show architecture'' without an
459 1.1 christos argument. */
460 1.1 christos
461 1.1 christos static void
462 1.1 christos show_architecture (struct ui_file *file, int from_tty,
463 1.1 christos struct cmd_list_element *c, const char *value)
464 1.1 christos {
465 1.1 christos if (target_architecture_user == NULL)
466 1.1 christos fprintf_filtered (file, _("The target architecture is set "
467 1.1 christos "automatically (currently %s)\n"),
468 1.1 christos gdbarch_bfd_arch_info (get_current_arch ())->printable_name);
469 1.1 christos else
470 1.1 christos fprintf_filtered (file, _("The target architecture is assumed to be %s\n"),
471 1.1 christos set_architecture_string);
472 1.1 christos }
473 1.1 christos
474 1.1 christos
475 1.1 christos /* Called if the user enters ``set architecture'' with or without an
476 1.1 christos argument. */
477 1.1 christos
478 1.1 christos static void
479 1.1 christos set_architecture (char *ignore_args, int from_tty, struct cmd_list_element *c)
480 1.1 christos {
481 1.1 christos struct gdbarch_info info;
482 1.1 christos
483 1.1 christos gdbarch_info_init (&info);
484 1.1 christos
485 1.1 christos if (strcmp (set_architecture_string, "auto") == 0)
486 1.1 christos {
487 1.1 christos target_architecture_user = NULL;
488 1.1 christos if (!gdbarch_update_p (info))
489 1.1 christos internal_error (__FILE__, __LINE__,
490 1.1 christos _("could not select an architecture automatically"));
491 1.1 christos }
492 1.1 christos else
493 1.1 christos {
494 1.1 christos info.bfd_arch_info = bfd_scan_arch (set_architecture_string);
495 1.1 christos if (info.bfd_arch_info == NULL)
496 1.1 christos internal_error (__FILE__, __LINE__,
497 1.1 christos _("set_architecture: bfd_scan_arch failed"));
498 1.1 christos if (gdbarch_update_p (info))
499 1.1 christos target_architecture_user = info.bfd_arch_info;
500 1.1 christos else
501 1.1 christos printf_unfiltered (_("Architecture `%s' not recognized.\n"),
502 1.1 christos set_architecture_string);
503 1.1 christos }
504 1.1 christos show_architecture (gdb_stdout, from_tty, NULL, NULL);
505 1.1 christos }
506 1.1 christos
507 1.1 christos /* Try to select a global architecture that matches "info". Return
508 1.1 christos non-zero if the attempt succeeds. */
509 1.1 christos int
510 1.1 christos gdbarch_update_p (struct gdbarch_info info)
511 1.1 christos {
512 1.1 christos struct gdbarch *new_gdbarch;
513 1.1 christos
514 1.1 christos /* Check for the current file. */
515 1.1 christos if (info.abfd == NULL)
516 1.1 christos info.abfd = exec_bfd;
517 1.1 christos if (info.abfd == NULL)
518 1.1 christos info.abfd = core_bfd;
519 1.1 christos
520 1.1 christos /* Check for the current target description. */
521 1.1 christos if (info.target_desc == NULL)
522 1.1 christos info.target_desc = target_current_description ();
523 1.1 christos
524 1.1 christos new_gdbarch = gdbarch_find_by_info (info);
525 1.1 christos
526 1.1 christos /* If there no architecture by that name, reject the request. */
527 1.1 christos if (new_gdbarch == NULL)
528 1.1 christos {
529 1.1 christos if (gdbarch_debug)
530 1.1 christos fprintf_unfiltered (gdb_stdlog, "gdbarch_update_p: "
531 1.1 christos "Architecture not found\n");
532 1.1 christos return 0;
533 1.1 christos }
534 1.1 christos
535 1.1 christos /* If it is the same old architecture, accept the request (but don't
536 1.1 christos swap anything). */
537 1.1 christos if (new_gdbarch == target_gdbarch ())
538 1.1 christos {
539 1.1 christos if (gdbarch_debug)
540 1.1 christos fprintf_unfiltered (gdb_stdlog, "gdbarch_update_p: "
541 1.1 christos "Architecture %s (%s) unchanged\n",
542 1.1 christos host_address_to_string (new_gdbarch),
543 1.1 christos gdbarch_bfd_arch_info (new_gdbarch)->printable_name);
544 1.1 christos return 1;
545 1.1 christos }
546 1.1 christos
547 1.1 christos /* It's a new architecture, swap it in. */
548 1.1 christos if (gdbarch_debug)
549 1.1 christos fprintf_unfiltered (gdb_stdlog, "gdbarch_update_p: "
550 1.1 christos "New architecture %s (%s) selected\n",
551 1.1 christos host_address_to_string (new_gdbarch),
552 1.1 christos gdbarch_bfd_arch_info (new_gdbarch)->printable_name);
553 1.1 christos set_target_gdbarch (new_gdbarch);
554 1.1 christos
555 1.1 christos return 1;
556 1.1 christos }
557 1.1 christos
558 1.1 christos /* Return the architecture for ABFD. If no suitable architecture
559 1.1 christos could be find, return NULL. */
560 1.1 christos
561 1.1 christos struct gdbarch *
562 1.1 christos gdbarch_from_bfd (bfd *abfd)
563 1.1 christos {
564 1.1 christos struct gdbarch_info info;
565 1.1 christos gdbarch_info_init (&info);
566 1.1 christos
567 1.1 christos info.abfd = abfd;
568 1.1 christos return gdbarch_find_by_info (info);
569 1.1 christos }
570 1.1 christos
571 1.1 christos /* Set the dynamic target-system-dependent parameters (architecture,
572 1.1 christos byte-order) using information found in the BFD */
573 1.1 christos
574 1.1 christos void
575 1.1 christos set_gdbarch_from_file (bfd *abfd)
576 1.1 christos {
577 1.1 christos struct gdbarch_info info;
578 1.1 christos struct gdbarch *gdbarch;
579 1.1 christos
580 1.1 christos gdbarch_info_init (&info);
581 1.1 christos info.abfd = abfd;
582 1.1 christos info.target_desc = target_current_description ();
583 1.1 christos gdbarch = gdbarch_find_by_info (info);
584 1.1 christos
585 1.1 christos if (gdbarch == NULL)
586 1.1 christos error (_("Architecture of file not recognized."));
587 1.1 christos set_target_gdbarch (gdbarch);
588 1.1 christos }
589 1.1 christos
590 1.1 christos /* Initialize the current architecture. Update the ``set
591 1.1 christos architecture'' command so that it specifies a list of valid
592 1.1 christos architectures. */
593 1.1 christos
594 1.1 christos #ifdef DEFAULT_BFD_ARCH
595 1.1 christos extern const bfd_arch_info_type DEFAULT_BFD_ARCH;
596 1.1 christos static const bfd_arch_info_type *default_bfd_arch = &DEFAULT_BFD_ARCH;
597 1.1 christos #else
598 1.1 christos static const bfd_arch_info_type *default_bfd_arch;
599 1.1 christos #endif
600 1.1 christos
601 1.1 christos #ifdef DEFAULT_BFD_VEC
602 1.1 christos extern const bfd_target DEFAULT_BFD_VEC;
603 1.1 christos static const bfd_target *default_bfd_vec = &DEFAULT_BFD_VEC;
604 1.1 christos #else
605 1.1 christos static const bfd_target *default_bfd_vec;
606 1.1 christos #endif
607 1.1 christos
608 1.1 christos static int default_byte_order = BFD_ENDIAN_UNKNOWN;
609 1.1 christos
610 1.1 christos void
611 1.1 christos initialize_current_architecture (void)
612 1.1 christos {
613 1.1 christos const char **arches = gdbarch_printable_names ();
614 1.1 christos struct gdbarch_info info;
615 1.1 christos
616 1.1 christos /* determine a default architecture and byte order. */
617 1.1 christos gdbarch_info_init (&info);
618 1.1 christos
619 1.1 christos /* Find a default architecture. */
620 1.1 christos if (default_bfd_arch == NULL)
621 1.1 christos {
622 1.1 christos /* Choose the architecture by taking the first one
623 1.1 christos alphabetically. */
624 1.1 christos const char *chosen = arches[0];
625 1.1 christos const char **arch;
626 1.1 christos for (arch = arches; *arch != NULL; arch++)
627 1.1 christos {
628 1.1 christos if (strcmp (*arch, chosen) < 0)
629 1.1 christos chosen = *arch;
630 1.1 christos }
631 1.1 christos if (chosen == NULL)
632 1.1 christos internal_error (__FILE__, __LINE__,
633 1.1 christos _("initialize_current_architecture: No arch"));
634 1.1 christos default_bfd_arch = bfd_scan_arch (chosen);
635 1.1 christos if (default_bfd_arch == NULL)
636 1.1 christos internal_error (__FILE__, __LINE__,
637 1.1 christos _("initialize_current_architecture: Arch not found"));
638 1.1 christos }
639 1.1 christos
640 1.1 christos info.bfd_arch_info = default_bfd_arch;
641 1.1 christos
642 1.1 christos /* Take several guesses at a byte order. */
643 1.1 christos if (default_byte_order == BFD_ENDIAN_UNKNOWN
644 1.1 christos && default_bfd_vec != NULL)
645 1.1 christos {
646 1.1 christos /* Extract BFD's default vector's byte order. */
647 1.1 christos switch (default_bfd_vec->byteorder)
648 1.1 christos {
649 1.1 christos case BFD_ENDIAN_BIG:
650 1.1 christos default_byte_order = BFD_ENDIAN_BIG;
651 1.1 christos break;
652 1.1 christos case BFD_ENDIAN_LITTLE:
653 1.1 christos default_byte_order = BFD_ENDIAN_LITTLE;
654 1.1 christos break;
655 1.1 christos default:
656 1.1 christos break;
657 1.1 christos }
658 1.1 christos }
659 1.1 christos if (default_byte_order == BFD_ENDIAN_UNKNOWN)
660 1.1 christos {
661 1.1 christos /* look for ``*el-*'' in the target name. */
662 1.1 christos const char *chp;
663 1.1 christos chp = strchr (target_name, '-');
664 1.1 christos if (chp != NULL
665 1.1 christos && chp - 2 >= target_name
666 1.1 christos && strncmp (chp - 2, "el", 2) == 0)
667 1.1 christos default_byte_order = BFD_ENDIAN_LITTLE;
668 1.1 christos }
669 1.1 christos if (default_byte_order == BFD_ENDIAN_UNKNOWN)
670 1.1 christos {
671 1.1 christos /* Wire it to big-endian!!! */
672 1.1 christos default_byte_order = BFD_ENDIAN_BIG;
673 1.1 christos }
674 1.1 christos
675 1.1 christos info.byte_order = default_byte_order;
676 1.1 christos info.byte_order_for_code = info.byte_order;
677 1.1 christos
678 1.1 christos if (! gdbarch_update_p (info))
679 1.1 christos internal_error (__FILE__, __LINE__,
680 1.1 christos _("initialize_current_architecture: Selection of "
681 1.1 christos "initial architecture failed"));
682 1.1 christos
683 1.1 christos /* Create the ``set architecture'' command appending ``auto'' to the
684 1.1 christos list of architectures. */
685 1.1 christos {
686 1.1 christos /* Append ``auto''. */
687 1.1 christos int nr;
688 1.1 christos for (nr = 0; arches[nr] != NULL; nr++);
689 1.1 christos arches = xrealloc (arches, sizeof (char*) * (nr + 2));
690 1.1 christos arches[nr + 0] = "auto";
691 1.1 christos arches[nr + 1] = NULL;
692 1.1 christos add_setshow_enum_cmd ("architecture", class_support,
693 1.1 christos arches, &set_architecture_string,
694 1.1 christos _("Set architecture of target."),
695 1.1 christos _("Show architecture of target."), NULL,
696 1.1 christos set_architecture, show_architecture,
697 1.1 christos &setlist, &showlist);
698 1.1 christos add_alias_cmd ("processor", "architecture", class_support, 1, &setlist);
699 1.1 christos }
700 1.1 christos }
701 1.1 christos
702 1.1 christos
703 1.1 christos /* Initialize a gdbarch info to values that will be automatically
704 1.1 christos overridden. Note: Originally, this ``struct info'' was initialized
705 1.1 christos using memset(0). Unfortunately, that ran into problems, namely
706 1.1 christos BFD_ENDIAN_BIG is zero. An explicit initialization function that
707 1.1 christos can explicitly set each field to a well defined value is used. */
708 1.1 christos
709 1.1 christos void
710 1.1 christos gdbarch_info_init (struct gdbarch_info *info)
711 1.1 christos {
712 1.1 christos memset (info, 0, sizeof (struct gdbarch_info));
713 1.1 christos info->byte_order = BFD_ENDIAN_UNKNOWN;
714 1.1 christos info->byte_order_for_code = info->byte_order;
715 1.1 christos info->osabi = GDB_OSABI_UNINITIALIZED;
716 1.1 christos }
717 1.1 christos
718 1.1 christos /* Similar to init, but this time fill in the blanks. Information is
719 1.1 christos obtained from the global "set ..." options and explicitly
720 1.1 christos initialized INFO fields. */
721 1.1 christos
722 1.1 christos void
723 1.1 christos gdbarch_info_fill (struct gdbarch_info *info)
724 1.1 christos {
725 1.1 christos /* "(gdb) set architecture ...". */
726 1.1 christos if (info->bfd_arch_info == NULL
727 1.1 christos && target_architecture_user)
728 1.1 christos info->bfd_arch_info = target_architecture_user;
729 1.1 christos /* From the file. */
730 1.1 christos if (info->bfd_arch_info == NULL
731 1.1 christos && info->abfd != NULL
732 1.1 christos && bfd_get_arch (info->abfd) != bfd_arch_unknown
733 1.1 christos && bfd_get_arch (info->abfd) != bfd_arch_obscure)
734 1.1 christos info->bfd_arch_info = bfd_get_arch_info (info->abfd);
735 1.1 christos /* From the target. */
736 1.1 christos if (info->target_desc != NULL)
737 1.1 christos info->bfd_arch_info = choose_architecture_for_target
738 1.1 christos (info->target_desc, info->bfd_arch_info);
739 1.1 christos /* From the default. */
740 1.1 christos if (info->bfd_arch_info == NULL)
741 1.1 christos info->bfd_arch_info = default_bfd_arch;
742 1.1 christos
743 1.1 christos /* "(gdb) set byte-order ...". */
744 1.1 christos if (info->byte_order == BFD_ENDIAN_UNKNOWN
745 1.1 christos && target_byte_order_user != BFD_ENDIAN_UNKNOWN)
746 1.1 christos info->byte_order = target_byte_order_user;
747 1.1 christos /* From the INFO struct. */
748 1.1 christos if (info->byte_order == BFD_ENDIAN_UNKNOWN
749 1.1 christos && info->abfd != NULL)
750 1.1 christos info->byte_order = (bfd_big_endian (info->abfd) ? BFD_ENDIAN_BIG
751 1.1 christos : bfd_little_endian (info->abfd) ? BFD_ENDIAN_LITTLE
752 1.1 christos : BFD_ENDIAN_UNKNOWN);
753 1.1 christos /* From the default. */
754 1.1 christos if (info->byte_order == BFD_ENDIAN_UNKNOWN)
755 1.1 christos info->byte_order = default_byte_order;
756 1.1 christos info->byte_order_for_code = info->byte_order;
757 1.1 christos
758 1.1 christos /* "(gdb) set osabi ...". Handled by gdbarch_lookup_osabi. */
759 1.1 christos /* From the manual override, or from file. */
760 1.1 christos if (info->osabi == GDB_OSABI_UNINITIALIZED)
761 1.1 christos info->osabi = gdbarch_lookup_osabi (info->abfd);
762 1.1 christos /* From the target. */
763 1.1 christos if (info->osabi == GDB_OSABI_UNKNOWN && info->target_desc != NULL)
764 1.1 christos info->osabi = tdesc_osabi (info->target_desc);
765 1.1 christos /* From the configured default. */
766 1.1 christos #ifdef GDB_OSABI_DEFAULT
767 1.1 christos if (info->osabi == GDB_OSABI_UNKNOWN)
768 1.1 christos info->osabi = GDB_OSABI_DEFAULT;
769 1.1 christos #endif
770 1.1 christos
771 1.1 christos /* Must have at least filled in the architecture. */
772 1.1 christos gdb_assert (info->bfd_arch_info != NULL);
773 1.1 christos }
774 1.1 christos
775 1.1 christos /* Return "current" architecture. If the target is running, this is
776 1.1 christos the architecture of the selected frame. Otherwise, the "current"
777 1.1 christos architecture defaults to the target architecture.
778 1.1 christos
779 1.1 christos This function should normally be called solely by the command
780 1.1 christos interpreter routines to determine the architecture to execute a
781 1.1 christos command in. */
782 1.1 christos struct gdbarch *
783 1.1 christos get_current_arch (void)
784 1.1 christos {
785 1.1 christos if (has_stack_frames ())
786 1.1 christos return get_frame_arch (get_selected_frame (NULL));
787 1.1 christos else
788 1.1 christos return target_gdbarch ();
789 1.1 christos }
790 1.1 christos
791 1.1 christos int
792 1.1 christos default_has_shared_address_space (struct gdbarch *gdbarch)
793 1.1 christos {
794 1.1 christos /* Simply say no. In most unix-like targets each inferior/process
795 1.1 christos has its own address space. */
796 1.1 christos return 0;
797 1.1 christos }
798 1.1 christos
799 1.1 christos int
800 1.1 christos default_fast_tracepoint_valid_at (struct gdbarch *gdbarch,
801 1.1 christos CORE_ADDR addr, int *isize, char **msg)
802 1.1 christos {
803 1.1 christos /* We don't know if maybe the target has some way to do fast
804 1.1 christos tracepoints that doesn't need gdbarch, so always say yes. */
805 1.1 christos if (msg)
806 1.1 christos *msg = NULL;
807 1.1 christos return 1;
808 1.1 christos }
809 1.1 christos
810 1.1 christos void
811 1.1 christos default_remote_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
812 1.1 christos int *kindptr)
813 1.1 christos {
814 1.1 christos gdbarch_breakpoint_from_pc (gdbarch, pcptr, kindptr);
815 1.1 christos }
816 1.1 christos
817 1.1 christos void
818 1.1 christos default_gen_return_address (struct gdbarch *gdbarch,
819 1.1 christos struct agent_expr *ax, struct axs_value *value,
820 1.1 christos CORE_ADDR scope)
821 1.1 christos {
822 1.1 christos error (_("This architecture has no method to collect a return address."));
823 1.1 christos }
824 1.1 christos
825 1.1 christos int
826 1.1 christos default_return_in_first_hidden_param_p (struct gdbarch *gdbarch,
827 1.1 christos struct type *type)
828 1.1 christos {
829 1.1 christos /* Usually, the return value's address is stored the in the "first hidden"
830 1.1 christos parameter if the return value should be passed by reference, as
831 1.1 christos specified in ABI. */
832 1.1 christos return language_pass_by_reference (type);
833 1.3 christos }
834 1.3 christos
835 1.3 christos int default_insn_is_call (struct gdbarch *gdbarch, CORE_ADDR addr)
836 1.3 christos {
837 1.3 christos return 0;
838 1.3 christos }
839 1.3 christos
840 1.3 christos int default_insn_is_ret (struct gdbarch *gdbarch, CORE_ADDR addr)
841 1.3 christos {
842 1.3 christos return 0;
843 1.3 christos }
844 1.3 christos
845 1.3 christos int default_insn_is_jump (struct gdbarch *gdbarch, CORE_ADDR addr)
846 1.3 christos {
847 1.3 christos return 0;
848 1.3 christos }
849 1.3 christos
850 1.3 christos void
851 1.3 christos default_skip_permanent_breakpoint (struct regcache *regcache)
852 1.3 christos {
853 1.3 christos struct gdbarch *gdbarch = get_regcache_arch (regcache);
854 1.3 christos CORE_ADDR current_pc = regcache_read_pc (regcache);
855 1.3 christos const gdb_byte *bp_insn;
856 1.3 christos int bp_len;
857 1.3 christos
858 1.3 christos bp_insn = gdbarch_breakpoint_from_pc (gdbarch, ¤t_pc, &bp_len);
859 1.3 christos current_pc += bp_len;
860 1.3 christos regcache_write_pc (regcache, current_pc);
861 1.3 christos }
862 1.3 christos
863 1.3 christos CORE_ADDR
864 1.3 christos default_infcall_mmap (CORE_ADDR size, unsigned prot)
865 1.3 christos {
866 1.3 christos error (_("This target does not support inferior memory allocation by mmap."));
867 1.3 christos }
868 1.3 christos
869 1.3 christos /* -mcmodel=large is used so that no GOT (Global Offset Table) is needed to be
870 1.3 christos created in inferior memory by GDB (normally it is set by ld.so). */
871 1.3 christos
872 1.3 christos char *
873 1.3 christos default_gcc_target_options (struct gdbarch *gdbarch)
874 1.3 christos {
875 1.3 christos return xstrprintf ("-m%d%s", gdbarch_ptr_bit (gdbarch),
876 1.3 christos gdbarch_ptr_bit (gdbarch) == 64 ? " -mcmodel=large" : "");
877 1.3 christos }
878 1.3 christos
879 1.3 christos /* gdbarch gnu_triplet_regexp method. */
880 1.3 christos
881 1.3 christos const char *
882 1.3 christos default_gnu_triplet_regexp (struct gdbarch *gdbarch)
883 1.3 christos {
884 1.1 christos return gdbarch_bfd_arch_info (gdbarch)->arch_name;
885 1.1 christos }
886 1.1 christos
887 1.1 christos /* -Wmissing-prototypes */
888 1.1 christos extern initialize_file_ftype _initialize_gdbarch_utils;
889 1.1 christos
890 1.1 christos void
891 1.1 christos _initialize_gdbarch_utils (void)
892 1.1 christos {
893 1.1 christos add_setshow_enum_cmd ("endian", class_support,
894 1.1 christos endian_enum, &set_endian_string,
895 1.1 christos _("Set endianness of target."),
896 1.1 christos _("Show endianness of target."),
897 1.1 christos NULL, set_endian, show_endian,
898 &setlist, &showlist);
899 }
900