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