198944Sobrien/* Dynamic architecture support for GDB, the GNU debugger.
298944Sobrien
3130803Smarcel   Copyright 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation,
4130803Smarcel   Inc.
5130803Smarcel
698944Sobrien   This file is part of GDB.
798944Sobrien
898944Sobrien   This program is free software; you can redistribute it and/or modify
998944Sobrien   it under the terms of the GNU General Public License as published by
1098944Sobrien   the Free Software Foundation; either version 2 of the License, or
1198944Sobrien   (at your option) any later version.
1298944Sobrien
1398944Sobrien   This program is distributed in the hope that it will be useful,
1498944Sobrien   but WITHOUT ANY WARRANTY; without even the implied warranty of
1598944Sobrien   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1698944Sobrien   GNU General Public License for more details.
1798944Sobrien
1898944Sobrien   You should have received a copy of the GNU General Public License
1998944Sobrien   along with this program; if not, write to the Free Software
2098944Sobrien   Foundation, Inc., 59 Temple Place - Suite 330,
2198944Sobrien   Boston, MA 02111-1307, USA.  */
2298944Sobrien
2398944Sobrien#include "defs.h"
2498944Sobrien
2598944Sobrien#include "arch-utils.h"
26130803Smarcel#include "buildsym.h"
2798944Sobrien#include "gdbcmd.h"
2898944Sobrien#include "inferior.h"		/* enum CALL_DUMMY_LOCATION et.al. */
2998944Sobrien#include "gdb_string.h"
3098944Sobrien#include "regcache.h"
3198944Sobrien#include "gdb_assert.h"
32130803Smarcel#include "sim-regno.h"
3398944Sobrien
34130803Smarcel#include "osabi.h"
35130803Smarcel
3698944Sobrien#include "version.h"
3798944Sobrien
3898944Sobrien#include "floatformat.h"
3998944Sobrien
40130803Smarcel/* Implementation of extract return value that grubs around in the
41130803Smarcel   register cache.  */
42130803Smarcelvoid
43130803Smarcellegacy_extract_return_value (struct type *type, struct regcache *regcache,
44130803Smarcel			     void *valbuf)
45130803Smarcel{
46130803Smarcel  char *registers = deprecated_grub_regcache_for_registers (regcache);
47130803Smarcel  bfd_byte *buf = valbuf;
48130803Smarcel  DEPRECATED_EXTRACT_RETURN_VALUE (type, registers, buf); /* OK */
49130803Smarcel}
5098944Sobrien
51130803Smarcel/* Implementation of store return value that grubs the register cache.
52130803Smarcel   Takes a local copy of the buffer to avoid const problems.  */
53130803Smarcelvoid
54130803Smarcellegacy_store_return_value (struct type *type, struct regcache *regcache,
55130803Smarcel			   const void *buf)
5698944Sobrien{
57130803Smarcel  bfd_byte *b = alloca (TYPE_LENGTH (type));
58130803Smarcel  gdb_assert (regcache == current_regcache);
59130803Smarcel  memcpy (b, buf, TYPE_LENGTH (type));
60130803Smarcel  DEPRECATED_STORE_RETURN_VALUE (type, b);
6198944Sobrien}
6298944Sobrien
63130803Smarcel
6498944Sobrienint
65130803Smarcelalways_use_struct_convention (int gcc_p, struct type *value_type)
6698944Sobrien{
67130803Smarcel  return 1;
6898944Sobrien}
6998944Sobrien
70130803Smarcel
7198944Sobrienint
72130803Smarcellegacy_register_sim_regno (int regnum)
73130803Smarcel{
74130803Smarcel  /* Only makes sense to supply raw registers.  */
75130803Smarcel  gdb_assert (regnum >= 0 && regnum < NUM_REGS);
76130803Smarcel  /* NOTE: cagney/2002-05-13: The old code did it this way and it is
77130803Smarcel     suspected that some GDB/SIM combinations may rely on this
78130803Smarcel     behavour.  The default should be one2one_register_sim_regno
79130803Smarcel     (below).  */
80130803Smarcel  if (REGISTER_NAME (regnum) != NULL
81130803Smarcel      && REGISTER_NAME (regnum)[0] != '\0')
82130803Smarcel    return regnum;
83130803Smarcel  else
84130803Smarcel    return LEGACY_SIM_REGNO_IGNORE;
85130803Smarcel}
86130803Smarcel
87130803Smarcelint
8898944Sobriengeneric_return_value_on_stack_not (struct type *type)
8998944Sobrien{
9098944Sobrien  return 0;
9198944Sobrien}
9298944Sobrien
9398944SobrienCORE_ADDR
9498944Sobriengeneric_skip_trampoline_code (CORE_ADDR pc)
9598944Sobrien{
9698944Sobrien  return 0;
9798944Sobrien}
9898944Sobrien
99130803SmarcelCORE_ADDR
100130803Smarcelgeneric_skip_solib_resolver (struct gdbarch *gdbarch, CORE_ADDR pc)
101130803Smarcel{
102130803Smarcel  return 0;
103130803Smarcel}
104130803Smarcel
10598944Sobrienint
10698944Sobriengeneric_in_solib_call_trampoline (CORE_ADDR pc, char *name)
10798944Sobrien{
10898944Sobrien  return 0;
10998944Sobrien}
11098944Sobrien
11198944Sobrienint
112130803Smarcelgeneric_in_solib_return_trampoline (CORE_ADDR pc, char *name)
11398944Sobrien{
11498944Sobrien  return 0;
11598944Sobrien}
11698944Sobrien
117130803Smarcelint
118130803Smarcelgeneric_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
11998944Sobrien{
120130803Smarcel  return 0;
12198944Sobrien}
12298944Sobrien
12398944Sobrien#if defined (CALL_DUMMY)
12498944SobrienLONGEST legacy_call_dummy_words[] = CALL_DUMMY;
12598944Sobrien#else
12698944SobrienLONGEST legacy_call_dummy_words[1];
12798944Sobrien#endif
12898944Sobrienint legacy_sizeof_call_dummy_words = sizeof (legacy_call_dummy_words);
12998944Sobrien
13098944Sobrienvoid
131130803Smarcelgeneric_remote_translate_xfer_address (struct gdbarch *gdbarch,
132130803Smarcel				       struct regcache *regcache,
133130803Smarcel				       CORE_ADDR gdb_addr, int gdb_len,
13498944Sobrien				       CORE_ADDR * rem_addr, int *rem_len)
13598944Sobrien{
13698944Sobrien  *rem_addr = gdb_addr;
13798944Sobrien  *rem_len = gdb_len;
13898944Sobrien}
13998944Sobrien
14098944Sobrien/* Helper functions for INNER_THAN */
14198944Sobrien
14298944Sobrienint
14398944Sobriencore_addr_lessthan (CORE_ADDR lhs, CORE_ADDR rhs)
14498944Sobrien{
14598944Sobrien  return (lhs < rhs);
14698944Sobrien}
14798944Sobrien
14898944Sobrienint
14998944Sobriencore_addr_greaterthan (CORE_ADDR lhs, CORE_ADDR rhs)
15098944Sobrien{
15198944Sobrien  return (lhs > rhs);
15298944Sobrien}
15398944Sobrien
15498944Sobrien
15598944Sobrien/* Helper functions for TARGET_{FLOAT,DOUBLE}_FORMAT */
15698944Sobrien
15798944Sobrienconst struct floatformat *
15898944Sobriendefault_float_format (struct gdbarch *gdbarch)
15998944Sobrien{
16098944Sobrien  int byte_order = gdbarch_byte_order (gdbarch);
16198944Sobrien  switch (byte_order)
16298944Sobrien    {
16398944Sobrien    case BFD_ENDIAN_BIG:
16498944Sobrien      return &floatformat_ieee_single_big;
16598944Sobrien    case BFD_ENDIAN_LITTLE:
16698944Sobrien      return &floatformat_ieee_single_little;
16798944Sobrien    default:
16898944Sobrien      internal_error (__FILE__, __LINE__,
16998944Sobrien		      "default_float_format: bad byte order");
17098944Sobrien    }
17198944Sobrien}
17298944Sobrien
17398944Sobrien
17498944Sobrienconst struct floatformat *
17598944Sobriendefault_double_format (struct gdbarch *gdbarch)
17698944Sobrien{
17798944Sobrien  int byte_order = gdbarch_byte_order (gdbarch);
17898944Sobrien  switch (byte_order)
17998944Sobrien    {
18098944Sobrien    case BFD_ENDIAN_BIG:
18198944Sobrien      return &floatformat_ieee_double_big;
18298944Sobrien    case BFD_ENDIAN_LITTLE:
18398944Sobrien      return &floatformat_ieee_double_little;
18498944Sobrien    default:
18598944Sobrien      internal_error (__FILE__, __LINE__,
18698944Sobrien		      "default_double_format: bad byte order");
18798944Sobrien    }
18898944Sobrien}
18998944Sobrien
19098944Sobrien/* Misc helper functions for targets. */
19198944Sobrien
19298944SobrienCORE_ADDR
193130803Smarcelcore_addr_identity (CORE_ADDR addr)
19498944Sobrien{
195130803Smarcel  return addr;
19698944Sobrien}
19798944Sobrien
19898944SobrienCORE_ADDR
199130803Smarcelconvert_from_func_ptr_addr_identity (struct gdbarch *gdbarch, CORE_ADDR addr,
200130803Smarcel				     struct target_ops *targ)
20198944Sobrien{
20298944Sobrien  return addr;
20398944Sobrien}
20498944Sobrien
20598944Sobrienint
20698944Sobrienno_op_reg_to_regnum (int reg)
20798944Sobrien{
20898944Sobrien  return reg;
20998944Sobrien}
21098944Sobrien
21198944SobrienCORE_ADDR
212130803Smarceldeprecated_init_frame_pc_default (int fromleaf, struct frame_info *prev)
21398944Sobrien{
214130803Smarcel  if (fromleaf && DEPRECATED_SAVED_PC_AFTER_CALL_P ())
215130803Smarcel    return DEPRECATED_SAVED_PC_AFTER_CALL (get_next_frame (prev));
216130803Smarcel  else if (get_next_frame (prev) != NULL)
217130803Smarcel    return DEPRECATED_FRAME_SAVED_PC (get_next_frame (prev));
21898944Sobrien  else
219130803Smarcel    return read_pc ();
22098944Sobrien}
22198944Sobrien
22298944Sobrienvoid
22398944Sobriendefault_elf_make_msymbol_special (asymbol *sym, struct minimal_symbol *msym)
22498944Sobrien{
22598944Sobrien  return;
22698944Sobrien}
22798944Sobrien
22898944Sobrienvoid
22998944Sobriendefault_coff_make_msymbol_special (int val, struct minimal_symbol *msym)
23098944Sobrien{
23198944Sobrien  return;
23298944Sobrien}
23398944Sobrien
23498944Sobrienint
23598944Sobriencannot_register_not (int regnum)
23698944Sobrien{
23798944Sobrien  return 0;
23898944Sobrien}
23998944Sobrien
24098944Sobrien/* Legacy version of target_virtual_frame_pointer().  Assumes that
241130803Smarcel   there is an DEPRECATED_FP_REGNUM and that it is the same, cooked or
242130803Smarcel   raw.  */
24398944Sobrien
24498944Sobrienvoid
24598944Sobrienlegacy_virtual_frame_pointer (CORE_ADDR pc,
24698944Sobrien			      int *frame_regnum,
24798944Sobrien			      LONGEST *frame_offset)
24898944Sobrien{
249130803Smarcel  /* FIXME: cagney/2002-09-13: This code is used when identifying the
250130803Smarcel     frame pointer of the current PC.  It is assuming that a single
251130803Smarcel     register and an offset can determine this.  I think it should
252130803Smarcel     instead generate a byte code expression as that would work better
253130803Smarcel     with things like Dwarf2's CFI.  */
254130803Smarcel  if (DEPRECATED_FP_REGNUM >= 0 && DEPRECATED_FP_REGNUM < NUM_REGS)
255130803Smarcel    *frame_regnum = DEPRECATED_FP_REGNUM;
256130803Smarcel  else if (SP_REGNUM >= 0 && SP_REGNUM < NUM_REGS)
257130803Smarcel    *frame_regnum = SP_REGNUM;
258130803Smarcel  else
259130803Smarcel    /* Should this be an internal error?  I guess so, it is reflecting
260130803Smarcel       an architectural limitation in the current design.  */
261130803Smarcel    internal_error (__FILE__, __LINE__, "No virtual frame pointer available");
26298944Sobrien  *frame_offset = 0;
26398944Sobrien}
26498944Sobrien
265130803Smarcel/* Assume the world is sane, every register's virtual and real size
266130803Smarcel   is identical.  */
26798944Sobrien
26898944Sobrienint
269130803Smarcelgeneric_register_size (int regnum)
27098944Sobrien{
27198944Sobrien  gdb_assert (regnum >= 0 && regnum < NUM_REGS + NUM_PSEUDO_REGS);
272130803Smarcel  if (gdbarch_register_type_p (current_gdbarch))
273130803Smarcel    return TYPE_LENGTH (gdbarch_register_type (current_gdbarch, regnum));
274130803Smarcel  else
275130803Smarcel    /* FIXME: cagney/2003-03-01: Once all architectures implement
276130803Smarcel       gdbarch_register_type(), this entire function can go away.  It
277130803Smarcel       is made obsolete by register_size().  */
278130803Smarcel    return TYPE_LENGTH (DEPRECATED_REGISTER_VIRTUAL_TYPE (regnum)); /* OK */
27998944Sobrien}
28098944Sobrien
281130803Smarcel/* Assume all registers are adjacent.  */
28298944Sobrien
28398944Sobrienint
284130803Smarcelgeneric_register_byte (int regnum)
28598944Sobrien{
286130803Smarcel  int byte;
287130803Smarcel  int i;
288130803Smarcel  gdb_assert (regnum >= 0 && regnum < NUM_REGS + NUM_PSEUDO_REGS);
289130803Smarcel  byte = 0;
290130803Smarcel  for (i = 0; i < regnum; i++)
291130803Smarcel    {
292130803Smarcel      byte += generic_register_size (i);
293130803Smarcel    }
294130803Smarcel  return byte;
29598944Sobrien}
29698944Sobrien
29798944Sobrien
298130803Smarcelint
299130803Smarcellegacy_pc_in_sigtramp (CORE_ADDR pc, char *name)
300130803Smarcel{
301130803Smarcel#if !defined (IN_SIGTRAMP)
302130803Smarcel  if (SIGTRAMP_START_P ())
303130803Smarcel    return (pc) >= SIGTRAMP_START (pc) && (pc) < SIGTRAMP_END (pc);
304130803Smarcel  else
305130803Smarcel    return name && strcmp ("_sigtramp", name) == 0;
306130803Smarcel#else
307130803Smarcel  return IN_SIGTRAMP (pc, name);
308130803Smarcel#endif
309130803Smarcel}
310130803Smarcel
311130803Smarcelint
312130803Smarcellegacy_convert_register_p (int regnum, struct type *type)
313130803Smarcel{
314130803Smarcel  return (DEPRECATED_REGISTER_CONVERTIBLE_P ()
315130803Smarcel	  && DEPRECATED_REGISTER_CONVERTIBLE (regnum));
316130803Smarcel}
317130803Smarcel
318130803Smarcelvoid
319130803Smarcellegacy_register_to_value (struct frame_info *frame, int regnum,
320130803Smarcel			  struct type *type, void *to)
321130803Smarcel{
322130803Smarcel  char from[MAX_REGISTER_SIZE];
323130803Smarcel  get_frame_register (frame, regnum, from);
324130803Smarcel  DEPRECATED_REGISTER_CONVERT_TO_VIRTUAL (regnum, type, from, to);
325130803Smarcel}
326130803Smarcel
327130803Smarcelvoid
328130803Smarcellegacy_value_to_register (struct frame_info *frame, int regnum,
329130803Smarcel			  struct type *type, const void *tmp)
330130803Smarcel{
331130803Smarcel  char to[MAX_REGISTER_SIZE];
332130803Smarcel  char *from = alloca (TYPE_LENGTH (type));
333130803Smarcel  memcpy (from, from, TYPE_LENGTH (type));
334130803Smarcel  DEPRECATED_REGISTER_CONVERT_TO_RAW (type, regnum, from, to);
335130803Smarcel  put_frame_register (frame, regnum, to);
336130803Smarcel}
337130803Smarcel
338130803Smarcelint
339130803Smarceldefault_stabs_argument_has_addr (struct gdbarch *gdbarch, struct type *type)
340130803Smarcel{
341130803Smarcel  if (DEPRECATED_REG_STRUCT_HAS_ADDR_P ()
342130803Smarcel      && DEPRECATED_REG_STRUCT_HAS_ADDR (processing_gcc_compilation, type))
343130803Smarcel    {
344130803Smarcel      CHECK_TYPEDEF (type);
345130803Smarcel
346130803Smarcel      return (TYPE_CODE (type) == TYPE_CODE_STRUCT
347130803Smarcel	      || TYPE_CODE (type) == TYPE_CODE_UNION
348130803Smarcel	      || TYPE_CODE (type) == TYPE_CODE_SET
349130803Smarcel	      || TYPE_CODE (type) == TYPE_CODE_BITSTRING);
350130803Smarcel    }
351130803Smarcel
352130803Smarcel  return 0;
353130803Smarcel}
354130803Smarcel
355130803Smarcel
35698944Sobrien/* Functions to manipulate the endianness of the target.  */
35798944Sobrien
35898944Sobrien/* ``target_byte_order'' is only used when non- multi-arch.
35998944Sobrien   Multi-arch targets obtain the current byte order using the
36098944Sobrien   TARGET_BYTE_ORDER gdbarch method.
36198944Sobrien
36298944Sobrien   The choice of initial value is entirely arbitrary.  During startup,
36398944Sobrien   the function initialize_current_architecture() updates this value
36498944Sobrien   based on default byte-order information extracted from BFD.  */
365130803Smarcelstatic int target_byte_order = BFD_ENDIAN_BIG;
366130803Smarcelstatic int target_byte_order_auto = 1;
36798944Sobrien
368130803Smarcelenum bfd_endian
369130803Smarcelselected_byte_order (void)
370130803Smarcel{
371130803Smarcel  if (target_byte_order_auto)
372130803Smarcel    return BFD_ENDIAN_UNKNOWN;
373130803Smarcel  else
374130803Smarcel    return target_byte_order;
375130803Smarcel}
376130803Smarcel
37798944Sobrienstatic const char endian_big[] = "big";
37898944Sobrienstatic const char endian_little[] = "little";
37998944Sobrienstatic const char endian_auto[] = "auto";
38098944Sobrienstatic const char *endian_enum[] =
38198944Sobrien{
38298944Sobrien  endian_big,
38398944Sobrien  endian_little,
38498944Sobrien  endian_auto,
38598944Sobrien  NULL,
38698944Sobrien};
38798944Sobrienstatic const char *set_endian_string;
38898944Sobrien
38998944Sobrien/* Called by ``show endian''.  */
39098944Sobrien
39198944Sobrienstatic void
39298944Sobrienshow_endian (char *args, int from_tty)
39398944Sobrien{
394130803Smarcel  if (target_byte_order_auto)
39598944Sobrien    printf_unfiltered ("The target endianness is set automatically (currently %s endian)\n",
39698944Sobrien		       (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG ? "big" : "little"));
39798944Sobrien  else
39898944Sobrien    printf_unfiltered ("The target is assumed to be %s endian\n",
39998944Sobrien		       (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG ? "big" : "little"));
40098944Sobrien}
40198944Sobrien
40298944Sobrienstatic void
40398944Sobrienset_endian (char *ignore_args, int from_tty, struct cmd_list_element *c)
40498944Sobrien{
40598944Sobrien  if (set_endian_string == endian_auto)
40698944Sobrien    {
40798944Sobrien      target_byte_order_auto = 1;
40898944Sobrien    }
40998944Sobrien  else if (set_endian_string == endian_little)
41098944Sobrien    {
411130803Smarcel      struct gdbarch_info info;
41298944Sobrien      target_byte_order_auto = 0;
413130803Smarcel      gdbarch_info_init (&info);
414130803Smarcel      info.byte_order = BFD_ENDIAN_LITTLE;
415130803Smarcel      if (! gdbarch_update_p (info))
416130803Smarcel	printf_unfiltered ("Little endian target not supported by GDB\n");
41798944Sobrien    }
41898944Sobrien  else if (set_endian_string == endian_big)
41998944Sobrien    {
420130803Smarcel      struct gdbarch_info info;
42198944Sobrien      target_byte_order_auto = 0;
422130803Smarcel      gdbarch_info_init (&info);
423130803Smarcel      info.byte_order = BFD_ENDIAN_BIG;
424130803Smarcel      if (! gdbarch_update_p (info))
425130803Smarcel	printf_unfiltered ("Big endian target not supported by GDB\n");
42698944Sobrien    }
42798944Sobrien  else
42898944Sobrien    internal_error (__FILE__, __LINE__,
42998944Sobrien		    "set_endian: bad value");
43098944Sobrien  show_endian (NULL, from_tty);
43198944Sobrien}
43298944Sobrien
43398944Sobrien/* Functions to manipulate the architecture of the target */
43498944Sobrien
43598944Sobrienenum set_arch { set_arch_auto, set_arch_manual };
43698944Sobrien
437130803Smarcelstatic int target_architecture_auto = 1;
43898944Sobrien
439130803Smarcelstatic const char *set_architecture_string;
44098944Sobrien
441130803Smarcelconst char *
442130803Smarcelselected_architecture_name (void)
44398944Sobrien{
444130803Smarcel  if (target_architecture_auto)
445130803Smarcel    return NULL;
44698944Sobrien  else
447130803Smarcel    return set_architecture_string;
44898944Sobrien}
44998944Sobrien
45098944Sobrien/* Called if the user enters ``show architecture'' without an
45198944Sobrien   argument. */
45298944Sobrien
45398944Sobrienstatic void
45498944Sobrienshow_architecture (char *args, int from_tty)
45598944Sobrien{
45698944Sobrien  const char *arch;
45798944Sobrien  arch = TARGET_ARCHITECTURE->printable_name;
45898944Sobrien  if (target_architecture_auto)
45998944Sobrien    printf_filtered ("The target architecture is set automatically (currently %s)\n", arch);
46098944Sobrien  else
46198944Sobrien    printf_filtered ("The target architecture is assumed to be %s\n", arch);
46298944Sobrien}
46398944Sobrien
46498944Sobrien
46598944Sobrien/* Called if the user enters ``set architecture'' with or without an
46698944Sobrien   argument. */
46798944Sobrien
46898944Sobrienstatic void
46998944Sobrienset_architecture (char *ignore_args, int from_tty, struct cmd_list_element *c)
47098944Sobrien{
47198944Sobrien  if (strcmp (set_architecture_string, "auto") == 0)
47298944Sobrien    {
47398944Sobrien      target_architecture_auto = 1;
47498944Sobrien    }
475130803Smarcel  else
47698944Sobrien    {
47798944Sobrien      struct gdbarch_info info;
47898944Sobrien      gdbarch_info_init (&info);
47998944Sobrien      info.bfd_arch_info = bfd_scan_arch (set_architecture_string);
48098944Sobrien      if (info.bfd_arch_info == NULL)
48198944Sobrien	internal_error (__FILE__, __LINE__,
48298944Sobrien			"set_architecture: bfd_scan_arch failed");
48398944Sobrien      if (gdbarch_update_p (info))
48498944Sobrien	target_architecture_auto = 0;
48598944Sobrien      else
48698944Sobrien	printf_unfiltered ("Architecture `%s' not recognized.\n",
48798944Sobrien			   set_architecture_string);
48898944Sobrien    }
489130803Smarcel  show_architecture (NULL, from_tty);
490130803Smarcel}
491130803Smarcel
492130803Smarcel/* Try to select a global architecture that matches "info".  Return
493130803Smarcel   non-zero if the attempt succeds.  */
494130803Smarcelint
495130803Smarcelgdbarch_update_p (struct gdbarch_info info)
496130803Smarcel{
497130803Smarcel  struct gdbarch *new_gdbarch = gdbarch_find_by_info (info);
498130803Smarcel
499130803Smarcel  /* If there no architecture by that name, reject the request.  */
500130803Smarcel  if (new_gdbarch == NULL)
50198944Sobrien    {
502130803Smarcel      if (gdbarch_debug)
503130803Smarcel	fprintf_unfiltered (gdb_stdlog, "gdbarch_update_p: "
504130803Smarcel			    "Architecture not found\n");
505130803Smarcel      return 0;
50698944Sobrien    }
507130803Smarcel
508130803Smarcel  /* If it is the same old architecture, accept the request (but don't
509130803Smarcel     swap anything).  */
510130803Smarcel  if (new_gdbarch == current_gdbarch)
511130803Smarcel    {
512130803Smarcel      if (gdbarch_debug)
513130803Smarcel	fprintf_unfiltered (gdb_stdlog, "gdbarch_update_p: "
514130803Smarcel			    "Architecture 0x%08lx (%s) unchanged\n",
515130803Smarcel			    (long) new_gdbarch,
516130803Smarcel			    gdbarch_bfd_arch_info (new_gdbarch)->printable_name);
517130803Smarcel      return 1;
518130803Smarcel    }
519130803Smarcel
520130803Smarcel  /* It's a new architecture, swap it in.  */
521130803Smarcel  if (gdbarch_debug)
522130803Smarcel    fprintf_unfiltered (gdb_stdlog, "gdbarch_update_p: "
523130803Smarcel			"New architecture 0x%08lx (%s) selected\n",
524130803Smarcel			(long) new_gdbarch,
525130803Smarcel			gdbarch_bfd_arch_info (new_gdbarch)->printable_name);
526130803Smarcel  deprecated_current_gdbarch_select_hack (new_gdbarch);
527130803Smarcel
528130803Smarcel  return 1;
52998944Sobrien}
53098944Sobrien
531130803Smarcel/* Return the architecture for ABFD.  If no suitable architecture
532130803Smarcel   could be find, return NULL.  */
533130803Smarcel
534130803Smarcelstruct gdbarch *
535130803Smarcelgdbarch_from_bfd (bfd *abfd)
536130803Smarcel{
537130803Smarcel  struct gdbarch *old_gdbarch = current_gdbarch;
538130803Smarcel  struct gdbarch *new_gdbarch;
539130803Smarcel  struct gdbarch_info info;
540130803Smarcel
541130803Smarcel  gdbarch_info_init (&info);
542130803Smarcel  info.abfd = abfd;
543130803Smarcel  return gdbarch_find_by_info (info);
544130803Smarcel}
545130803Smarcel
54698944Sobrien/* Set the dynamic target-system-dependent parameters (architecture,
54798944Sobrien   byte-order) using information found in the BFD */
54898944Sobrien
54998944Sobrienvoid
55098944Sobrienset_gdbarch_from_file (bfd *abfd)
55198944Sobrien{
552130803Smarcel  struct gdbarch *gdbarch;
553130803Smarcel
554130803Smarcel  gdbarch = gdbarch_from_bfd (abfd);
555130803Smarcel  if (gdbarch == NULL)
556130803Smarcel    error ("Architecture of file not recognized.\n");
557130803Smarcel  deprecated_current_gdbarch_select_hack (gdbarch);
55898944Sobrien}
55998944Sobrien
56098944Sobrien/* Initialize the current architecture.  Update the ``set
56198944Sobrien   architecture'' command so that it specifies a list of valid
56298944Sobrien   architectures.  */
56398944Sobrien
56498944Sobrien#ifdef DEFAULT_BFD_ARCH
56598944Sobrienextern const bfd_arch_info_type DEFAULT_BFD_ARCH;
56698944Sobrienstatic const bfd_arch_info_type *default_bfd_arch = &DEFAULT_BFD_ARCH;
56798944Sobrien#else
56898944Sobrienstatic const bfd_arch_info_type *default_bfd_arch;
56998944Sobrien#endif
57098944Sobrien
57198944Sobrien#ifdef DEFAULT_BFD_VEC
57298944Sobrienextern const bfd_target DEFAULT_BFD_VEC;
57398944Sobrienstatic const bfd_target *default_bfd_vec = &DEFAULT_BFD_VEC;
57498944Sobrien#else
57598944Sobrienstatic const bfd_target *default_bfd_vec;
57698944Sobrien#endif
57798944Sobrien
57898944Sobrienvoid
57998944Sobrieninitialize_current_architecture (void)
58098944Sobrien{
58198944Sobrien  const char **arches = gdbarch_printable_names ();
58298944Sobrien
58398944Sobrien  /* determine a default architecture and byte order. */
58498944Sobrien  struct gdbarch_info info;
58598944Sobrien  gdbarch_info_init (&info);
58698944Sobrien
58798944Sobrien  /* Find a default architecture. */
58898944Sobrien  if (info.bfd_arch_info == NULL
58998944Sobrien      && default_bfd_arch != NULL)
59098944Sobrien    info.bfd_arch_info = default_bfd_arch;
59198944Sobrien  if (info.bfd_arch_info == NULL)
59298944Sobrien    {
59398944Sobrien      /* Choose the architecture by taking the first one
59498944Sobrien	 alphabetically. */
59598944Sobrien      const char *chosen = arches[0];
59698944Sobrien      const char **arch;
59798944Sobrien      for (arch = arches; *arch != NULL; arch++)
59898944Sobrien	{
59998944Sobrien	  if (strcmp (*arch, chosen) < 0)
60098944Sobrien	    chosen = *arch;
60198944Sobrien	}
60298944Sobrien      if (chosen == NULL)
60398944Sobrien	internal_error (__FILE__, __LINE__,
60498944Sobrien			"initialize_current_architecture: No arch");
60598944Sobrien      info.bfd_arch_info = bfd_scan_arch (chosen);
60698944Sobrien      if (info.bfd_arch_info == NULL)
60798944Sobrien	internal_error (__FILE__, __LINE__,
60898944Sobrien			"initialize_current_architecture: Arch not found");
60998944Sobrien    }
61098944Sobrien
61198944Sobrien  /* Take several guesses at a byte order.  */
61298944Sobrien  if (info.byte_order == BFD_ENDIAN_UNKNOWN
61398944Sobrien      && default_bfd_vec != NULL)
61498944Sobrien    {
61598944Sobrien      /* Extract BFD's default vector's byte order. */
61698944Sobrien      switch (default_bfd_vec->byteorder)
61798944Sobrien	{
61898944Sobrien	case BFD_ENDIAN_BIG:
61998944Sobrien	  info.byte_order = BFD_ENDIAN_BIG;
62098944Sobrien	  break;
62198944Sobrien	case BFD_ENDIAN_LITTLE:
62298944Sobrien	  info.byte_order = BFD_ENDIAN_LITTLE;
62398944Sobrien	  break;
62498944Sobrien	default:
62598944Sobrien	  break;
62698944Sobrien	}
62798944Sobrien    }
62898944Sobrien  if (info.byte_order == BFD_ENDIAN_UNKNOWN)
62998944Sobrien    {
63098944Sobrien      /* look for ``*el-*'' in the target name. */
63198944Sobrien      const char *chp;
63298944Sobrien      chp = strchr (target_name, '-');
63398944Sobrien      if (chp != NULL
63498944Sobrien	  && chp - 2 >= target_name
63598944Sobrien	  && strncmp (chp - 2, "el", 2) == 0)
63698944Sobrien	info.byte_order = BFD_ENDIAN_LITTLE;
63798944Sobrien    }
63898944Sobrien  if (info.byte_order == BFD_ENDIAN_UNKNOWN)
63998944Sobrien    {
64098944Sobrien      /* Wire it to big-endian!!! */
64198944Sobrien      info.byte_order = BFD_ENDIAN_BIG;
64298944Sobrien    }
64398944Sobrien
644130803Smarcel  if (! gdbarch_update_p (info))
645130803Smarcel    internal_error (__FILE__, __LINE__,
646130803Smarcel		    "initialize_current_architecture: Selection of initial architecture failed");
64798944Sobrien
64898944Sobrien  /* Create the ``set architecture'' command appending ``auto'' to the
64998944Sobrien     list of architectures. */
65098944Sobrien  {
65198944Sobrien    struct cmd_list_element *c;
65298944Sobrien    /* Append ``auto''. */
65398944Sobrien    int nr;
65498944Sobrien    for (nr = 0; arches[nr] != NULL; nr++);
65598944Sobrien    arches = xrealloc (arches, sizeof (char*) * (nr + 2));
65698944Sobrien    arches[nr + 0] = "auto";
65798944Sobrien    arches[nr + 1] = NULL;
65898944Sobrien    /* FIXME: add_set_enum_cmd() uses an array of ``char *'' instead
65998944Sobrien       of ``const char *''.  We just happen to know that the casts are
66098944Sobrien       safe. */
66198944Sobrien    c = add_set_enum_cmd ("architecture", class_support,
66298944Sobrien			  arches, &set_architecture_string,
66398944Sobrien			  "Set architecture of target.",
66498944Sobrien			  &setlist);
66598944Sobrien    set_cmd_sfunc (c, set_architecture);
66698944Sobrien    add_alias_cmd ("processor", "architecture", class_support, 1, &setlist);
66798944Sobrien    /* Don't use set_from_show - need to print both auto/manual and
66898944Sobrien       current setting. */
66998944Sobrien    add_cmd ("architecture", class_support, show_architecture,
67098944Sobrien	     "Show the current target architecture", &showlist);
67198944Sobrien  }
67298944Sobrien}
67398944Sobrien
67498944Sobrien
67598944Sobrien/* Initialize a gdbarch info to values that will be automatically
67698944Sobrien   overridden.  Note: Originally, this ``struct info'' was initialized
677130803Smarcel   using memset(0).  Unfortunately, that ran into problems, namely
67898944Sobrien   BFD_ENDIAN_BIG is zero.  An explicit initialization function that
67998944Sobrien   can explicitly set each field to a well defined value is used.  */
68098944Sobrien
68198944Sobrienvoid
68298944Sobriengdbarch_info_init (struct gdbarch_info *info)
68398944Sobrien{
68498944Sobrien  memset (info, 0, sizeof (struct gdbarch_info));
68598944Sobrien  info->byte_order = BFD_ENDIAN_UNKNOWN;
686130803Smarcel  info->osabi = GDB_OSABI_UNINITIALIZED;
68798944Sobrien}
68898944Sobrien
689130803Smarcel/* Similar to init, but this time fill in the blanks.  Information is
690130803Smarcel   obtained from the specified architecture, global "set ..." options,
691130803Smarcel   and explicitly initialized INFO fields.  */
692130803Smarcel
693130803Smarcelvoid
694130803Smarcelgdbarch_info_fill (struct gdbarch *gdbarch, struct gdbarch_info *info)
695130803Smarcel{
696130803Smarcel  /* "(gdb) set architecture ...".  */
697130803Smarcel  if (info->bfd_arch_info == NULL
698130803Smarcel      && !target_architecture_auto
699130803Smarcel      && gdbarch != NULL)
700130803Smarcel    info->bfd_arch_info = gdbarch_bfd_arch_info (gdbarch);
701130803Smarcel  if (info->bfd_arch_info == NULL
702130803Smarcel      && info->abfd != NULL
703130803Smarcel      && bfd_get_arch (info->abfd) != bfd_arch_unknown
704130803Smarcel      && bfd_get_arch (info->abfd) != bfd_arch_obscure)
705130803Smarcel    info->bfd_arch_info = bfd_get_arch_info (info->abfd);
706130803Smarcel  if (info->bfd_arch_info == NULL
707130803Smarcel      && gdbarch != NULL)
708130803Smarcel    info->bfd_arch_info = gdbarch_bfd_arch_info (gdbarch);
709130803Smarcel
710130803Smarcel  /* "(gdb) set byte-order ...".  */
711130803Smarcel  if (info->byte_order == BFD_ENDIAN_UNKNOWN
712130803Smarcel      && !target_byte_order_auto
713130803Smarcel      && gdbarch != NULL)
714130803Smarcel    info->byte_order = gdbarch_byte_order (gdbarch);
715130803Smarcel  /* From the INFO struct.  */
716130803Smarcel  if (info->byte_order == BFD_ENDIAN_UNKNOWN
717130803Smarcel      && info->abfd != NULL)
718130803Smarcel    info->byte_order = (bfd_big_endian (info->abfd) ? BFD_ENDIAN_BIG
719130803Smarcel		       : bfd_little_endian (info->abfd) ? BFD_ENDIAN_LITTLE
720130803Smarcel		       : BFD_ENDIAN_UNKNOWN);
721130803Smarcel  /* From the current target.  */
722130803Smarcel  if (info->byte_order == BFD_ENDIAN_UNKNOWN
723130803Smarcel      && gdbarch != NULL)
724130803Smarcel    info->byte_order = gdbarch_byte_order (gdbarch);
725130803Smarcel
726130803Smarcel  /* "(gdb) set osabi ...".  Handled by gdbarch_lookup_osabi.  */
727130803Smarcel  if (info->osabi == GDB_OSABI_UNINITIALIZED)
728130803Smarcel    info->osabi = gdbarch_lookup_osabi (info->abfd);
729130803Smarcel  if (info->osabi == GDB_OSABI_UNINITIALIZED
730130803Smarcel      && gdbarch != NULL)
731130803Smarcel    info->osabi = gdbarch_osabi (gdbarch);
732130803Smarcel
733130803Smarcel  /* Must have at least filled in the architecture.  */
734130803Smarcel  gdb_assert (info->bfd_arch_info != NULL);
735130803Smarcel}
736130803Smarcel
73798944Sobrien/* */
73898944Sobrien
739130803Smarcelextern initialize_file_ftype _initialize_gdbarch_utils; /* -Wmissing-prototypes */
74098944Sobrien
74198944Sobrienvoid
74298944Sobrien_initialize_gdbarch_utils (void)
74398944Sobrien{
74498944Sobrien  struct cmd_list_element *c;
74598944Sobrien  c = add_set_enum_cmd ("endian", class_support,
74698944Sobrien			endian_enum, &set_endian_string,
74798944Sobrien			"Set endianness of target.",
74898944Sobrien			&setlist);
74998944Sobrien  set_cmd_sfunc (c, set_endian);
75098944Sobrien  /* Don't use set_from_show - need to print both auto/manual and
75198944Sobrien     current setting. */
75298944Sobrien  add_cmd ("endian", class_support, show_endian,
75398944Sobrien	   "Show the current byte-order", &showlist);
75498944Sobrien}
755