119370Spst/* Generic symbol file reading for the GNU debugger, GDB.
299000Sobrien
399000Sobrien   Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4130809Smarcel   1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
599000Sobrien
619370Spst   Contributed by Cygnus Support, using pieces from other GDB modules.
719370Spst
899000Sobrien   This file is part of GDB.
919370Spst
1099000Sobrien   This program is free software; you can redistribute it and/or modify
1199000Sobrien   it under the terms of the GNU General Public License as published by
1299000Sobrien   the Free Software Foundation; either version 2 of the License, or
1399000Sobrien   (at your option) any later version.
1419370Spst
1599000Sobrien   This program is distributed in the hope that it will be useful,
1699000Sobrien   but WITHOUT ANY WARRANTY; without even the implied warranty of
1799000Sobrien   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1899000Sobrien   GNU General Public License for more details.
1919370Spst
2099000Sobrien   You should have received a copy of the GNU General Public License
2199000Sobrien   along with this program; if not, write to the Free Software
2299000Sobrien   Foundation, Inc., 59 Temple Place - Suite 330,
2399000Sobrien   Boston, MA 02111-1307, USA.  */
2419370Spst
2519370Spst#include "defs.h"
26130809Smarcel#include "bfdlink.h"
2719370Spst#include "symtab.h"
2819370Spst#include "gdbtypes.h"
2919370Spst#include "gdbcore.h"
3019370Spst#include "frame.h"
3119370Spst#include "target.h"
3219370Spst#include "value.h"
3319370Spst#include "symfile.h"
3419370Spst#include "objfiles.h"
35130809Smarcel#include "source.h"
3619370Spst#include "gdbcmd.h"
3719370Spst#include "breakpoint.h"
3819370Spst#include "language.h"
3919370Spst#include "complaints.h"
4019370Spst#include "demangle.h"
4199000Sobrien#include "inferior.h"		/* for write_pc */
42130809Smarcel#include "filenames.h"		/* for DOSish file names */
4346289Sdfr#include "gdb-stabs.h"
44130809Smarcel#include "gdb_obstack.h"
4599000Sobrien#include "completer.h"
46130809Smarcel#include "bcache.h"
47130809Smarcel#include "hashtab.h"
48130809Smarcel#include "readline/readline.h"
49130809Smarcel#include "gdb_assert.h"
50130809Smarcel#include "block.h"
5119370Spst
5219370Spst#include <sys/types.h>
5319370Spst#include <fcntl.h>
5419370Spst#include "gdb_string.h"
5519370Spst#include "gdb_stat.h"
5619370Spst#include <ctype.h>
5719370Spst#include <time.h>
5819370Spst
5919370Spst#ifndef O_BINARY
6019370Spst#define O_BINARY 0
6119370Spst#endif
6219370Spst
6346289Sdfr#ifdef HPUXHPPA
6446289Sdfr
6546289Sdfr/* Some HP-UX related globals to clear when a new "main"
6646289Sdfr   symbol file is loaded. HP-specific.  */
6746289Sdfr
6846289Sdfrextern int hp_som_som_object_present;
6946289Sdfrextern int hp_cxx_exception_support_initialized;
7046289Sdfr#define RESET_HP_UX_GLOBALS() do {\
7146289Sdfr                                    hp_som_som_object_present = 0;             /* indicates HP-compiled code */        \
7246289Sdfr                                    hp_cxx_exception_support_initialized = 0;  /* must reinitialize exception stuff */ \
7346289Sdfr                              } while (0)
7446289Sdfr#endif
7546289Sdfr
7699000Sobrienint (*ui_load_progress_hook) (const char *section, unsigned long num);
7799000Sobrienvoid (*show_load_progress) (const char *section,
7899000Sobrien			    unsigned long section_sent,
7999000Sobrien			    unsigned long section_size,
8099000Sobrien			    unsigned long total_sent,
8199000Sobrien			    unsigned long total_size);
8299000Sobrienvoid (*pre_add_symbol_hook) (char *);
8399000Sobrienvoid (*post_add_symbol_hook) (void);
8499000Sobrienvoid (*target_new_objfile_hook) (struct objfile *);
8546289Sdfr
8699000Sobrienstatic void clear_symtab_users_cleanup (void *ignore);
8799000Sobrien
8819370Spst/* Global variables owned by this file */
8999000Sobrienint readnow_symbol_files;	/* Read full symbols immediately */
9019370Spst
9119370Spst/* External variables and functions referenced. */
9219370Spst
9399000Sobrienextern void report_transfer_performance (unsigned long, time_t, time_t);
9419370Spst
9519370Spst/* Functions this file defines */
9619370Spst
9746289Sdfr#if 0
9899000Sobrienstatic int simple_read_overlay_region_table (void);
9999000Sobrienstatic void simple_free_overlay_region_table (void);
10046289Sdfr#endif
10119370Spst
10299000Sobrienstatic void set_initial_language (void);
10319370Spst
10499000Sobrienstatic void load_command (char *, int);
10519370Spst
10699000Sobrienstatic void symbol_file_add_main_1 (char *args, int from_tty, int flags);
10719370Spst
10899000Sobrienstatic void add_symbol_file_command (char *, int);
10919370Spst
11099000Sobrienstatic void add_shared_symbol_files_command (char *, int);
11119370Spst
112130809Smarcelstatic void reread_separate_symbols (struct objfile *objfile);
113130809Smarcel
11499000Sobrienstatic void cashier_psymtab (struct partial_symtab *);
11519370Spst
11699000Sobrienbfd *symfile_bfd_open (char *);
11719370Spst
11899000Sobrienint get_section_index (struct objfile *, char *);
11919370Spst
12099000Sobrienstatic void find_sym_fns (struct objfile *);
12146289Sdfr
12299000Sobrienstatic void decrement_reading_symtab (void *);
12346289Sdfr
12499000Sobrienstatic void overlay_invalidate_all (void);
12546289Sdfr
12699000Sobrienstatic int overlay_is_mapped (struct obj_section *);
12746289Sdfr
12899000Sobrienvoid list_overlays_command (char *, int);
12946289Sdfr
13099000Sobrienvoid map_overlay_command (char *, int);
13146289Sdfr
13299000Sobrienvoid unmap_overlay_command (char *, int);
13346289Sdfr
13499000Sobrienstatic void overlay_auto_command (char *, int);
13546289Sdfr
13699000Sobrienstatic void overlay_manual_command (char *, int);
13746289Sdfr
13899000Sobrienstatic void overlay_off_command (char *, int);
13946289Sdfr
14099000Sobrienstatic void overlay_load_command (char *, int);
14146289Sdfr
14299000Sobrienstatic void overlay_command (char *, int);
14346289Sdfr
14499000Sobrienstatic void simple_free_overlay_table (void);
14546289Sdfr
14699000Sobrienstatic void read_target_long_array (CORE_ADDR, unsigned int *, int);
14746289Sdfr
14899000Sobrienstatic int simple_read_overlay_table (void);
14946289Sdfr
15099000Sobrienstatic int simple_overlay_update_1 (struct obj_section *);
15146289Sdfr
15299000Sobrienstatic void add_filename_language (char *ext, enum language lang);
15346289Sdfr
15499000Sobrienstatic void set_ext_lang_command (char *args, int from_tty);
15599000Sobrien
15699000Sobrienstatic void info_ext_lang_command (char *args, int from_tty);
15799000Sobrien
158130809Smarcelstatic char *find_separate_debug_file (struct objfile *objfile);
159130809Smarcel
16099000Sobrienstatic void init_filename_language_table (void);
16199000Sobrien
16299000Sobrienvoid _initialize_symfile (void);
16399000Sobrien
16419370Spst/* List of all available sym_fns.  On gdb startup, each object file reader
16519370Spst   calls add_symtab_fns() to register information on each format it is
16619370Spst   prepared to read. */
16719370Spst
16819370Spststatic struct sym_fns *symtab_fns = NULL;
16919370Spst
17019370Spst/* Flag for whether user will be reloading symbols multiple times.
17119370Spst   Defaults to ON for VxWorks, otherwise OFF.  */
17219370Spst
17319370Spst#ifdef SYMBOL_RELOADING_DEFAULT
17419370Spstint symbol_reloading = SYMBOL_RELOADING_DEFAULT;
17519370Spst#else
17619370Spstint symbol_reloading = 0;
17719370Spst#endif
17819370Spst
17999000Sobrien/* If non-zero, shared library symbols will be added automatically
18099000Sobrien   when the inferior is created, new libraries are loaded, or when
18199000Sobrien   attaching to the inferior.  This is almost always what users will
18299000Sobrien   want to have happen; but for very large programs, the startup time
18399000Sobrien   will be excessive, and so if this is a problem, the user can clear
18499000Sobrien   this flag and then add the shared library symbols as needed.  Note
18599000Sobrien   that there is a potential for confusion, since if the shared
18619370Spst   library symbols are not loaded, commands like "info fun" will *not*
18799000Sobrien   report all the functions that are actually present. */
18819370Spst
18919370Spstint auto_solib_add = 1;
19019370Spst
19199000Sobrien/* For systems that support it, a threshold size in megabytes.  If
19299000Sobrien   automatically adding a new library's symbol table to those already
19399000Sobrien   known to the debugger would cause the total shared library symbol
19499000Sobrien   size to exceed this threshhold, then the shlib's symbols are not
19599000Sobrien   added.  The threshold is ignored if the user explicitly asks for a
19699000Sobrien   shlib to be added, such as when using the "sharedlibrary"
19799000Sobrien   command. */
19899000Sobrien
19999000Sobrienint auto_solib_limit;
20019370Spst
20199000Sobrien
202130809Smarcel/* This compares two partial symbols by names, using strcmp_iw_ordered
203130809Smarcel   for the comparison.  */
20419370Spst
20519370Spststatic int
20699000Sobriencompare_psymbols (const void *s1p, const void *s2p)
20719370Spst{
208130809Smarcel  struct partial_symbol *const *s1 = s1p;
209130809Smarcel  struct partial_symbol *const *s2 = s2p;
21019370Spst
211130809Smarcel  return strcmp_iw_ordered (SYMBOL_NATURAL_NAME (*s1),
212130809Smarcel			    SYMBOL_NATURAL_NAME (*s2));
21319370Spst}
21419370Spst
21519370Spstvoid
21699000Sobriensort_pst_symbols (struct partial_symtab *pst)
21719370Spst{
21819370Spst  /* Sort the global list; don't sort the static list */
21919370Spst
22099000Sobrien  qsort (pst->objfile->global_psymbols.list + pst->globals_offset,
22199000Sobrien	 pst->n_global_syms, sizeof (struct partial_symbol *),
22219370Spst	 compare_psymbols);
22319370Spst}
22419370Spst
22546289Sdfr/* Make a null terminated copy of the string at PTR with SIZE characters in
22646289Sdfr   the obstack pointed to by OBSTACKP .  Returns the address of the copy.
22746289Sdfr   Note that the string at PTR does not have to be null terminated, I.E. it
22846289Sdfr   may be part of a larger string and we are only saving a substring. */
22919370Spst
23019370Spstchar *
231130809Smarcelobsavestring (const char *ptr, int size, struct obstack *obstackp)
23219370Spst{
233130809Smarcel  char *p = (char *) obstack_alloc (obstackp, size + 1);
23446289Sdfr  /* Open-coded memcpy--saves function call time.  These strings are usually
23546289Sdfr     short.  FIXME: Is this really still true with a compiler that can
23646289Sdfr     inline memcpy? */
23719370Spst  {
238130809Smarcel    const char *p1 = ptr;
239130809Smarcel    char *p2 = p;
240130809Smarcel    const char *end = ptr + size;
24119370Spst    while (p1 != end)
24219370Spst      *p2++ = *p1++;
24319370Spst  }
24419370Spst  p[size] = 0;
24519370Spst  return p;
24619370Spst}
24719370Spst
24846289Sdfr/* Concatenate strings S1, S2 and S3; return the new string.  Space is found
24946289Sdfr   in the obstack pointed to by OBSTACKP.  */
25019370Spst
25119370Spstchar *
25299000Sobrienobconcat (struct obstack *obstackp, const char *s1, const char *s2,
25399000Sobrien	  const char *s3)
25419370Spst{
255130809Smarcel  int len = strlen (s1) + strlen (s2) + strlen (s3) + 1;
256130809Smarcel  char *val = (char *) obstack_alloc (obstackp, len);
25719370Spst  strcpy (val, s1);
25819370Spst  strcat (val, s2);
25919370Spst  strcat (val, s3);
26019370Spst  return val;
26119370Spst}
26219370Spst
26319370Spst/* True if we are nested inside psymtab_to_symtab. */
26419370Spst
26519370Spstint currently_reading_symtab = 0;
26619370Spst
26719370Spststatic void
26899000Sobriendecrement_reading_symtab (void *dummy)
26919370Spst{
27019370Spst  currently_reading_symtab--;
27119370Spst}
27219370Spst
27319370Spst/* Get the symbol table that corresponds to a partial_symtab.
27419370Spst   This is fast after the first time you do it.  In fact, there
27519370Spst   is an even faster macro PSYMTAB_TO_SYMTAB that does the fast
27619370Spst   case inline.  */
27719370Spst
27819370Spststruct symtab *
279130809Smarcelpsymtab_to_symtab (struct partial_symtab *pst)
28019370Spst{
28119370Spst  /* If it's been looked up before, return it. */
28219370Spst  if (pst->symtab)
28319370Spst    return pst->symtab;
28419370Spst
28519370Spst  /* If it has not yet been read in, read it.  */
28619370Spst  if (!pst->readin)
28799000Sobrien    {
28819370Spst      struct cleanup *back_to = make_cleanup (decrement_reading_symtab, NULL);
28919370Spst      currently_reading_symtab++;
29019370Spst      (*pst->read_symtab) (pst);
29119370Spst      do_cleanups (back_to);
29219370Spst    }
29319370Spst
29419370Spst  return pst->symtab;
29519370Spst}
29619370Spst
29719370Spst/* Remember the lowest-addressed loadable section we've seen.
29819370Spst   This function is called via bfd_map_over_sections.
29919370Spst
30019370Spst   In case of equal vmas, the section with the largest size becomes the
30119370Spst   lowest-addressed loadable section.
30219370Spst
30319370Spst   If the vmas and sizes are equal, the last section is considered the
30419370Spst   lowest-addressed loadable section.  */
30519370Spst
30619370Spstvoid
307130809Smarcelfind_lowest_section (bfd *abfd, asection *sect, void *obj)
30819370Spst{
30999000Sobrien  asection **lowest = (asection **) obj;
31019370Spst
31119370Spst  if (0 == (bfd_get_section_flags (abfd, sect) & SEC_LOAD))
31219370Spst    return;
31319370Spst  if (!*lowest)
31419370Spst    *lowest = sect;		/* First loadable section */
31519370Spst  else if (bfd_section_vma (abfd, *lowest) > bfd_section_vma (abfd, sect))
31619370Spst    *lowest = sect;		/* A lower loadable section */
31719370Spst  else if (bfd_section_vma (abfd, *lowest) == bfd_section_vma (abfd, sect)
31819370Spst	   && (bfd_section_size (abfd, (*lowest))
31919370Spst	       <= bfd_section_size (abfd, sect)))
32019370Spst    *lowest = sect;
32119370Spst}
32219370Spst
323130809Smarcel/* Create a new section_addr_info, with room for NUM_SECTIONS.  */
32499000Sobrien
325130809Smarcelstruct section_addr_info *
326130809Smarcelalloc_section_addr_info (size_t num_sections)
327130809Smarcel{
328130809Smarcel  struct section_addr_info *sap;
329130809Smarcel  size_t size;
330130809Smarcel
331130809Smarcel  size = (sizeof (struct section_addr_info)
332130809Smarcel	  +  sizeof (struct other_sections) * (num_sections - 1));
333130809Smarcel  sap = (struct section_addr_info *) xmalloc (size);
334130809Smarcel  memset (sap, 0, size);
335130809Smarcel  sap->num_sections = num_sections;
336130809Smarcel
337130809Smarcel  return sap;
338130809Smarcel}
339130809Smarcel
34099000Sobrien/* Build (allocate and populate) a section_addr_info struct from
34199000Sobrien   an existing section table. */
34299000Sobrien
34399000Sobrienextern struct section_addr_info *
34499000Sobrienbuild_section_addr_info_from_section_table (const struct section_table *start,
34599000Sobrien                                            const struct section_table *end)
34699000Sobrien{
34799000Sobrien  struct section_addr_info *sap;
34899000Sobrien  const struct section_table *stp;
34999000Sobrien  int oidx;
35099000Sobrien
351130809Smarcel  sap = alloc_section_addr_info (end - start);
35299000Sobrien
35399000Sobrien  for (stp = start, oidx = 0; stp != end; stp++)
35499000Sobrien    {
35599000Sobrien      if (bfd_get_section_flags (stp->bfd,
35699000Sobrien				 stp->the_bfd_section) & (SEC_ALLOC | SEC_LOAD)
357130809Smarcel	  && oidx < end - start)
35899000Sobrien	{
35999000Sobrien	  sap->other[oidx].addr = stp->addr;
36099000Sobrien	  sap->other[oidx].name
36199000Sobrien	    = xstrdup (bfd_section_name (stp->bfd, stp->the_bfd_section));
36299000Sobrien	  sap->other[oidx].sectindex = stp->the_bfd_section->index;
36399000Sobrien	  oidx++;
36499000Sobrien	}
36599000Sobrien    }
36699000Sobrien
36799000Sobrien  return sap;
36899000Sobrien}
36999000Sobrien
37099000Sobrien
37199000Sobrien/* Free all memory allocated by build_section_addr_info_from_section_table. */
37299000Sobrien
37399000Sobrienextern void
37499000Sobrienfree_section_addr_info (struct section_addr_info *sap)
37599000Sobrien{
37699000Sobrien  int idx;
37799000Sobrien
378130809Smarcel  for (idx = 0; idx < sap->num_sections; idx++)
37999000Sobrien    if (sap->other[idx].name)
38099000Sobrien      xfree (sap->other[idx].name);
38199000Sobrien  xfree (sap);
38299000Sobrien}
38399000Sobrien
38499000Sobrien
385130809Smarcel/* Initialize OBJFILE's sect_index_* members.  */
386130809Smarcelstatic void
387130809Smarcelinit_objfile_sect_indices (struct objfile *objfile)
38846289Sdfr{
389130809Smarcel  asection *sect;
39046289Sdfr  int i;
391130809Smarcel
39299000Sobrien  sect = bfd_get_section_by_name (objfile->obfd, ".text");
39399000Sobrien  if (sect)
39499000Sobrien    objfile->sect_index_text = sect->index;
39599000Sobrien
39699000Sobrien  sect = bfd_get_section_by_name (objfile->obfd, ".data");
39799000Sobrien  if (sect)
39899000Sobrien    objfile->sect_index_data = sect->index;
39999000Sobrien
40099000Sobrien  sect = bfd_get_section_by_name (objfile->obfd, ".bss");
40199000Sobrien  if (sect)
40299000Sobrien    objfile->sect_index_bss = sect->index;
40399000Sobrien
40499000Sobrien  sect = bfd_get_section_by_name (objfile->obfd, ".rodata");
40599000Sobrien  if (sect)
40699000Sobrien    objfile->sect_index_rodata = sect->index;
40799000Sobrien
40899000Sobrien  /* This is where things get really weird...  We MUST have valid
40999000Sobrien     indices for the various sect_index_* members or gdb will abort.
41099000Sobrien     So if for example, there is no ".text" section, we have to
41199000Sobrien     accomodate that.  Except when explicitly adding symbol files at
41299000Sobrien     some address, section_offsets contains nothing but zeros, so it
41399000Sobrien     doesn't matter which slot in section_offsets the individual
41499000Sobrien     sect_index_* members index into.  So if they are all zero, it is
41599000Sobrien     safe to just point all the currently uninitialized indices to the
41699000Sobrien     first slot. */
41799000Sobrien
41899000Sobrien  for (i = 0; i < objfile->num_sections; i++)
41999000Sobrien    {
42099000Sobrien      if (ANOFFSET (objfile->section_offsets, i) != 0)
42199000Sobrien	{
42299000Sobrien	  break;
42399000Sobrien	}
42499000Sobrien    }
42599000Sobrien  if (i == objfile->num_sections)
42699000Sobrien    {
42799000Sobrien      if (objfile->sect_index_text == -1)
42899000Sobrien	objfile->sect_index_text = 0;
42999000Sobrien      if (objfile->sect_index_data == -1)
43099000Sobrien	objfile->sect_index_data = 0;
43199000Sobrien      if (objfile->sect_index_bss == -1)
43299000Sobrien	objfile->sect_index_bss = 0;
43399000Sobrien      if (objfile->sect_index_rodata == -1)
43499000Sobrien	objfile->sect_index_rodata = 0;
43599000Sobrien    }
43646289Sdfr}
43746289Sdfr
438130809Smarcel
439130809Smarcel/* Parse the user's idea of an offset for dynamic linking, into our idea
440130809Smarcel   of how to represent it for fast symbol reading.  This is the default
441130809Smarcel   version of the sym_fns.sym_offsets function for symbol readers that
442130809Smarcel   don't need to do anything special.  It allocates a section_offsets table
443130809Smarcel   for the objectfile OBJFILE and stuffs ADDR into all of the offsets.  */
444130809Smarcel
445130809Smarcelvoid
446130809Smarceldefault_symfile_offsets (struct objfile *objfile,
447130809Smarcel			 struct section_addr_info *addrs)
448130809Smarcel{
449130809Smarcel  int i;
450130809Smarcel
451130809Smarcel  objfile->num_sections = bfd_count_sections (objfile->obfd);
452130809Smarcel  objfile->section_offsets = (struct section_offsets *)
453130809Smarcel    obstack_alloc (&objfile->objfile_obstack,
454130809Smarcel		   SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
455130809Smarcel  memset (objfile->section_offsets, 0,
456130809Smarcel	  SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
457130809Smarcel
458130809Smarcel  /* Now calculate offsets for section that were specified by the
459130809Smarcel     caller. */
460130809Smarcel  for (i = 0; i < addrs->num_sections && addrs->other[i].name; i++)
461130809Smarcel    {
462130809Smarcel      struct other_sections *osp ;
463130809Smarcel
464130809Smarcel      osp = &addrs->other[i] ;
465130809Smarcel      if (osp->addr == 0)
466130809Smarcel  	continue;
467130809Smarcel
468130809Smarcel      /* Record all sections in offsets */
469130809Smarcel      /* The section_offsets in the objfile are here filled in using
470130809Smarcel         the BFD index. */
471130809Smarcel      (objfile->section_offsets)->offsets[osp->sectindex] = osp->addr;
472130809Smarcel    }
473130809Smarcel
474130809Smarcel  /* Remember the bfd indexes for the .text, .data, .bss and
475130809Smarcel     .rodata sections. */
476130809Smarcel  init_objfile_sect_indices (objfile);
477130809Smarcel}
478130809Smarcel
479130809Smarcel
48019370Spst/* Process a symbol file, as either the main file or as a dynamically
48119370Spst   loaded file.
48219370Spst
48399000Sobrien   OBJFILE is where the symbols are to be read from.
48419370Spst
485130809Smarcel   ADDRS is the list of section load addresses.  If the user has given
486130809Smarcel   an 'add-symbol-file' command, then this is the list of offsets and
487130809Smarcel   addresses he or she provided as arguments to the command; or, if
488130809Smarcel   we're handling a shared library, these are the actual addresses the
489130809Smarcel   sections are loaded at, according to the inferior's dynamic linker
490130809Smarcel   (as gleaned by GDB's shared library code).  We convert each address
491130809Smarcel   into an offset from the section VMA's as it appears in the object
492130809Smarcel   file, and then call the file's sym_offsets function to convert this
493130809Smarcel   into a format-specific offset table --- a `struct section_offsets'.
494130809Smarcel   If ADDRS is non-zero, OFFSETS must be zero.
49599000Sobrien
496130809Smarcel   OFFSETS is a table of section offsets already in the right
497130809Smarcel   format-specific representation.  NUM_OFFSETS is the number of
498130809Smarcel   elements present in OFFSETS->offsets.  If OFFSETS is non-zero, we
499130809Smarcel   assume this is the proper table the call to sym_offsets described
500130809Smarcel   above would produce.  Instead of calling sym_offsets, we just dump
501130809Smarcel   it right into objfile->section_offsets.  (When we're re-reading
502130809Smarcel   symbols from an objfile, we don't have the original load address
503130809Smarcel   list any more; all we have is the section offset table.)  If
504130809Smarcel   OFFSETS is non-zero, ADDRS must be zero.
505130809Smarcel
50699000Sobrien   MAINLINE is nonzero if this is the main symbol file, or zero if
50799000Sobrien   it's an extra symbol file such as dynamically loaded code.
50899000Sobrien
50999000Sobrien   VERBO is nonzero if the caller has printed a verbose message about
51099000Sobrien   the symbol reading (and complaints can be more terse about it).  */
51199000Sobrien
51219370Spstvoid
513130809Smarcelsyms_from_objfile (struct objfile *objfile,
514130809Smarcel                   struct section_addr_info *addrs,
515130809Smarcel                   struct section_offsets *offsets,
516130809Smarcel                   int num_offsets,
517130809Smarcel		   int mainline,
518130809Smarcel                   int verbo)
51919370Spst{
520130809Smarcel  struct section_addr_info *local_addr = NULL;
52119370Spst  struct cleanup *old_chain;
52219370Spst
523130809Smarcel  gdb_assert (! (addrs && offsets));
52499000Sobrien
52519370Spst  init_entry_point_info (objfile);
52619370Spst  find_sym_fns (objfile);
52719370Spst
528130809Smarcel  if (objfile->sf == NULL)
529130809Smarcel    return;	/* No symbols. */
530130809Smarcel
53119370Spst  /* Make sure that partially constructed symbol tables will be cleaned up
53219370Spst     if an error occurs during symbol reading.  */
53399000Sobrien  old_chain = make_cleanup_free_objfile (objfile);
53419370Spst
535130809Smarcel  /* If ADDRS and OFFSETS are both NULL, put together a dummy address
536130809Smarcel     list.  We now establish the convention that an addr of zero means
537130809Smarcel     no load address was specified. */
538130809Smarcel  if (! addrs && ! offsets)
539130809Smarcel    {
540130809Smarcel      local_addr
541130809Smarcel	= alloc_section_addr_info (bfd_count_sections (objfile->obfd));
542130809Smarcel      make_cleanup (xfree, local_addr);
543130809Smarcel      addrs = local_addr;
544130809Smarcel    }
545130809Smarcel
546130809Smarcel  /* Now either addrs or offsets is non-zero.  */
547130809Smarcel
54899000Sobrien  if (mainline)
54919370Spst    {
55019370Spst      /* We will modify the main symbol table, make sure that all its users
55199000Sobrien         will be cleaned up if an error occurs during symbol reading.  */
55299000Sobrien      make_cleanup (clear_symtab_users_cleanup, 0 /*ignore*/);
55319370Spst
55419370Spst      /* Since no error yet, throw away the old symbol table.  */
55519370Spst
55619370Spst      if (symfile_objfile != NULL)
55719370Spst	{
55819370Spst	  free_objfile (symfile_objfile);
55919370Spst	  symfile_objfile = NULL;
56019370Spst	}
56119370Spst
56219370Spst      /* Currently we keep symbols from the add-symbol-file command.
56399000Sobrien         If the user wants to get rid of them, they should do "symbol-file"
56499000Sobrien         without arguments first.  Not sure this is the best behavior
56599000Sobrien         (PR 2207).  */
56619370Spst
56799000Sobrien      (*objfile->sf->sym_new_init) (objfile);
56819370Spst    }
56919370Spst
57019370Spst  /* Convert addr into an offset rather than an absolute address.
57119370Spst     We find the lowest address of a loaded segment in the objfile,
57299000Sobrien     and assume that <addr> is where that got loaded.
57319370Spst
57499000Sobrien     We no longer warn if the lowest section is not a text segment (as
57599000Sobrien     happens for the PA64 port.  */
576130809Smarcel  if (!mainline && addrs && addrs->other[0].name)
57719370Spst    {
578130809Smarcel      asection *lower_sect;
579130809Smarcel      asection *sect;
580130809Smarcel      CORE_ADDR lower_offset;
581130809Smarcel      int i;
582130809Smarcel
58399000Sobrien      /* Find lowest loadable section to be used as starting point for
58499000Sobrien         continguous sections. FIXME!! won't work without call to find
58599000Sobrien	 .text first, but this assumes text is lowest section. */
58699000Sobrien      lower_sect = bfd_get_section_by_name (objfile->obfd, ".text");
58799000Sobrien      if (lower_sect == NULL)
58819370Spst	bfd_map_over_sections (objfile->obfd, find_lowest_section,
589130809Smarcel			       &lower_sect);
59099000Sobrien      if (lower_sect == NULL)
59119370Spst	warning ("no loadable sections found in added symbol-file %s",
59219370Spst		 objfile->name);
59399000Sobrien      else
59499000Sobrien	if ((bfd_get_section_flags (objfile->obfd, lower_sect) & SEC_CODE) == 0)
59599000Sobrien	  warning ("Lowest section in %s is %s at %s",
59699000Sobrien		   objfile->name,
59799000Sobrien		   bfd_section_name (objfile->obfd, lower_sect),
59899000Sobrien		   paddr (bfd_section_vma (objfile->obfd, lower_sect)));
59999000Sobrien      if (lower_sect != NULL)
60099000Sobrien 	lower_offset = bfd_section_vma (objfile->obfd, lower_sect);
60199000Sobrien      else
60299000Sobrien 	lower_offset = 0;
60399000Sobrien
604130809Smarcel      /* Calculate offsets for the loadable sections.
60599000Sobrien 	 FIXME! Sections must be in order of increasing loadable section
60699000Sobrien 	 so that contiguous sections can use the lower-offset!!!
60799000Sobrien
608130809Smarcel         Adjust offsets if the segments are not contiguous.
609130809Smarcel         If the section is contiguous, its offset should be set to
61099000Sobrien 	 the offset of the highest loadable section lower than it
61199000Sobrien 	 (the loadable section directly below it in memory).
61299000Sobrien 	 this_offset = lower_offset = lower_addr - lower_orig_addr */
61319370Spst
614130809Smarcel        for (i = 0; i < addrs->num_sections && addrs->other[i].name; i++)
615130809Smarcel          {
616130809Smarcel            if (addrs->other[i].addr != 0)
617130809Smarcel              {
618130809Smarcel                sect = bfd_get_section_by_name (objfile->obfd,
619130809Smarcel                                                addrs->other[i].name);
620130809Smarcel                if (sect)
621130809Smarcel                  {
622130809Smarcel                    addrs->other[i].addr
623130809Smarcel                      -= bfd_section_vma (objfile->obfd, sect);
624130809Smarcel                    lower_offset = addrs->other[i].addr;
625130809Smarcel                    /* This is the index used by BFD. */
626130809Smarcel                    addrs->other[i].sectindex = sect->index ;
627130809Smarcel                  }
628130809Smarcel                else
629130809Smarcel                  {
630130809Smarcel                    warning ("section %s not found in %s",
631130809Smarcel                             addrs->other[i].name,
632130809Smarcel                             objfile->name);
633130809Smarcel                    addrs->other[i].addr = 0;
634130809Smarcel                  }
635130809Smarcel              }
636130809Smarcel            else
637130809Smarcel              addrs->other[i].addr = lower_offset;
638130809Smarcel          }
63919370Spst    }
64019370Spst
64119370Spst  /* Initialize symbol reading routines for this objfile, allow complaints to
64219370Spst     appear for this new file, and record how verbose to be, then do the
64319370Spst     initial symbol reading for this file. */
64419370Spst
64599000Sobrien  (*objfile->sf->sym_init) (objfile);
646130809Smarcel  clear_complaints (&symfile_complaints, 1, verbo);
64719370Spst
648130809Smarcel  if (addrs)
649130809Smarcel    (*objfile->sf->sym_offsets) (objfile, addrs);
650130809Smarcel  else
651130809Smarcel    {
652130809Smarcel      size_t size = SIZEOF_N_SECTION_OFFSETS (num_offsets);
65319370Spst
654130809Smarcel      /* Just copy in the offset table directly as given to us.  */
655130809Smarcel      objfile->num_sections = num_offsets;
656130809Smarcel      objfile->section_offsets
657130809Smarcel        = ((struct section_offsets *)
658130809Smarcel           obstack_alloc (&objfile->objfile_obstack, size));
659130809Smarcel      memcpy (objfile->section_offsets, offsets, size);
660130809Smarcel
661130809Smarcel      init_objfile_sect_indices (objfile);
662130809Smarcel    }
663130809Smarcel
664130809Smarcel#ifndef DEPRECATED_IBM6000_TARGET
66519370Spst  /* This is a SVR4/SunOS specific hack, I think.  In any event, it
66619370Spst     screws RS/6000.  sym_offsets should be doing this sort of thing,
66719370Spst     because it knows the mapping between bfd sections and
66819370Spst     section_offsets.  */
66919370Spst  /* This is a hack.  As far as I can tell, section offsets are not
67019370Spst     target dependent.  They are all set to addr with a couple of
67119370Spst     exceptions.  The exceptions are sysvr4 shared libraries, whose
67219370Spst     offsets are kept in solib structures anyway and rs6000 xcoff
67319370Spst     which handles shared libraries in a completely unique way.
67419370Spst
67519370Spst     Section offsets are built similarly, except that they are built
67619370Spst     by adding addr in all cases because there is no clear mapping
67719370Spst     from section_offsets into actual sections.  Note that solib.c
67899000Sobrien     has a different algorithm for finding section offsets.
67919370Spst
68019370Spst     These should probably all be collapsed into some target
68119370Spst     independent form of shared library support.  FIXME.  */
68219370Spst
68399000Sobrien  if (addrs)
68419370Spst    {
68519370Spst      struct obj_section *s;
68619370Spst
68799000Sobrien 	/* Map section offsets in "addr" back to the object's
68899000Sobrien 	   sections by comparing the section names with bfd's
68999000Sobrien 	   section names.  Then adjust the section address by
69099000Sobrien 	   the offset. */ /* for gdb/13815 */
69199000Sobrien
69299000Sobrien      ALL_OBJFILE_OSECTIONS (objfile, s)
69319370Spst	{
69499000Sobrien	  CORE_ADDR s_addr = 0;
69599000Sobrien	  int i;
69699000Sobrien
69799000Sobrien 	    for (i = 0;
698130809Smarcel	         !s_addr && i < addrs->num_sections && addrs->other[i].name;
69999000Sobrien		 i++)
70099000Sobrien 	      if (strcmp (bfd_section_name (s->objfile->obfd,
70199000Sobrien					    s->the_bfd_section),
70299000Sobrien			  addrs->other[i].name) == 0)
70399000Sobrien 	        s_addr = addrs->other[i].addr; /* end added for gdb/13815 */
70499000Sobrien
70519370Spst	  s->addr -= s->offset;
70699000Sobrien	  s->addr += s_addr;
70719370Spst	  s->endaddr -= s->offset;
70899000Sobrien	  s->endaddr += s_addr;
70999000Sobrien	  s->offset += s_addr;
71019370Spst	}
71119370Spst    }
712130809Smarcel#endif /* not DEPRECATED_IBM6000_TARGET */
71319370Spst
71499000Sobrien  (*objfile->sf->sym_read) (objfile, mainline);
71519370Spst
71619370Spst  /* Don't allow char * to have a typename (else would get caddr_t).
71719370Spst     Ditto void *.  FIXME: Check whether this is now done by all the
71819370Spst     symbol readers themselves (many of them now do), and if so remove
71919370Spst     it from here.  */
72019370Spst
72119370Spst  TYPE_NAME (lookup_pointer_type (builtin_type_char)) = 0;
72219370Spst  TYPE_NAME (lookup_pointer_type (builtin_type_void)) = 0;
72319370Spst
72419370Spst  /* Mark the objfile has having had initial symbol read attempted.  Note
72519370Spst     that this does not mean we found any symbols... */
72619370Spst
72799000Sobrien  objfile->flags |= OBJF_SYMS;
72819370Spst
72919370Spst  /* Discard cleanups as symbol reading was successful.  */
73019370Spst
73119370Spst  discard_cleanups (old_chain);
73219370Spst}
73319370Spst
73419370Spst/* Perform required actions after either reading in the initial
73519370Spst   symbols for a new objfile, or mapping in the symbols from a reusable
73619370Spst   objfile. */
73799000Sobrien
73819370Spstvoid
73999000Sobriennew_symfile_objfile (struct objfile *objfile, int mainline, int verbo)
74019370Spst{
74119370Spst
74219370Spst  /* If this is the main symbol file we have to clean up all users of the
74319370Spst     old main symbol file. Otherwise it is sufficient to fixup all the
74419370Spst     breakpoints that may have been redefined by this symbol file.  */
74519370Spst  if (mainline)
74619370Spst    {
74719370Spst      /* OK, make it the "real" symbol file.  */
74819370Spst      symfile_objfile = objfile;
74919370Spst
75019370Spst      clear_symtab_users ();
75119370Spst    }
75219370Spst  else
75319370Spst    {
75419370Spst      breakpoint_re_set ();
75519370Spst    }
75619370Spst
75719370Spst  /* We're done reading the symbol file; finish off complaints.  */
758130809Smarcel  clear_complaints (&symfile_complaints, 0, verbo);
75919370Spst}
76019370Spst
76119370Spst/* Process a symbol file, as either the main file or as a dynamically
76219370Spst   loaded file.
76319370Spst
76419370Spst   NAME is the file name (which will be tilde-expanded and made
76519370Spst   absolute herein) (but we don't free or modify NAME itself).
76619370Spst
767130809Smarcel   FROM_TTY says how verbose to be.
768130809Smarcel
769130809Smarcel   MAINLINE specifies whether this is the main symbol file, or whether
770130809Smarcel   it's an extra symbol file such as dynamically loaded code.
771130809Smarcel
772130809Smarcel   ADDRS, OFFSETS, and NUM_OFFSETS are as described for
773130809Smarcel   syms_from_objfile, above.  ADDRS is ignored when MAINLINE is
774130809Smarcel   non-zero.
775130809Smarcel
77619370Spst   Upon success, returns a pointer to the objfile that was added.
77719370Spst   Upon failure, jumps back to command level (never returns). */
778130809Smarcelstatic struct objfile *
779130809Smarcelsymbol_file_add_with_addrs_or_offsets (char *name, int from_tty,
780130809Smarcel                                       struct section_addr_info *addrs,
781130809Smarcel                                       struct section_offsets *offsets,
782130809Smarcel                                       int num_offsets,
783130809Smarcel                                       int mainline, int flags)
78419370Spst{
78519370Spst  struct objfile *objfile;
78619370Spst  struct partial_symtab *psymtab;
787130809Smarcel  char *debugfile;
78819370Spst  bfd *abfd;
789130809Smarcel  struct section_addr_info *orig_addrs;
790130809Smarcel  struct cleanup *my_cleanups;
79119370Spst
79219370Spst  /* Open a bfd for the file, and give user a chance to burp if we'd be
79319370Spst     interactively wiping out any existing symbols.  */
79419370Spst
79519370Spst  abfd = symfile_bfd_open (name);
79619370Spst
79719370Spst  if ((have_full_symbols () || have_partial_symbols ())
79819370Spst      && mainline
79919370Spst      && from_tty
80019370Spst      && !query ("Load new symbol table from \"%s\"? ", name))
80199000Sobrien    error ("Not confirmed.");
80219370Spst
80399000Sobrien  objfile = allocate_objfile (abfd, flags);
80419370Spst
805130809Smarcel  orig_addrs = alloc_section_addr_info (bfd_count_sections (abfd));
806130809Smarcel  my_cleanups = make_cleanup (xfree, orig_addrs);
807130809Smarcel  if (addrs)
808130809Smarcel    {
809130809Smarcel      int i;
810130809Smarcel      orig_addrs->num_sections = addrs->num_sections;
811130809Smarcel      for (i = 0; i < addrs->num_sections; i++)
812130809Smarcel	orig_addrs->other[i] = addrs->other[i];
813130809Smarcel    }
81419370Spst
815130809Smarcel  /* We either created a new mapped symbol table, mapped an existing
816130809Smarcel     symbol table file which has not had initial symbol reading
817130809Smarcel     performed, or need to read an unmapped symbol table. */
818130809Smarcel  if (from_tty || info_verbose)
81919370Spst    {
820130809Smarcel      if (pre_add_symbol_hook)
821130809Smarcel	pre_add_symbol_hook (name);
822130809Smarcel      else
82319370Spst	{
824130809Smarcel	  printf_unfiltered ("Reading symbols from %s...", name);
82519370Spst	  wrap_here ("");
82619370Spst	  gdb_flush (gdb_stdout);
82719370Spst	}
82819370Spst    }
829130809Smarcel  syms_from_objfile (objfile, addrs, offsets, num_offsets,
830130809Smarcel		     mainline, from_tty);
83119370Spst
83219370Spst  /* We now have at least a partial symbol table.  Check to see if the
83319370Spst     user requested that all symbols be read on initial access via either
83419370Spst     the gdb startup command line or on a per symbol file basis.  Expand
83519370Spst     all partial symbol tables for this objfile if so. */
83619370Spst
83799000Sobrien  if ((flags & OBJF_READNOW) || readnow_symbol_files)
83819370Spst    {
83919370Spst      if (from_tty || info_verbose)
84019370Spst	{
841130809Smarcel	  printf_unfiltered ("expanding to full symbols...");
84219370Spst	  wrap_here ("");
84319370Spst	  gdb_flush (gdb_stdout);
84419370Spst	}
84519370Spst
84699000Sobrien      for (psymtab = objfile->psymtabs;
84719370Spst	   psymtab != NULL;
84899000Sobrien	   psymtab = psymtab->next)
84919370Spst	{
85019370Spst	  psymtab_to_symtab (psymtab);
85119370Spst	}
85219370Spst    }
85319370Spst
854130809Smarcel  debugfile = find_separate_debug_file (objfile);
855130809Smarcel  if (debugfile)
856130809Smarcel    {
857130809Smarcel      if (addrs != NULL)
858130809Smarcel	{
859130809Smarcel	  objfile->separate_debug_objfile
860130809Smarcel            = symbol_file_add (debugfile, from_tty, orig_addrs, 0, flags);
861130809Smarcel	}
862130809Smarcel      else
863130809Smarcel	{
864130809Smarcel	  objfile->separate_debug_objfile
865130809Smarcel            = symbol_file_add (debugfile, from_tty, NULL, 0, flags);
866130809Smarcel	}
867130809Smarcel      objfile->separate_debug_objfile->separate_debug_objfile_backlink
868130809Smarcel        = objfile;
869130809Smarcel
870130809Smarcel      /* Put the separate debug object before the normal one, this is so that
871130809Smarcel         usage of the ALL_OBJFILES_SAFE macro will stay safe. */
872130809Smarcel      put_objfile_before (objfile->separate_debug_objfile, objfile);
873130809Smarcel
874130809Smarcel      xfree (debugfile);
875130809Smarcel    }
876130809Smarcel
877130809Smarcel  if (!have_partial_symbols () && !have_full_symbols ())
878130809Smarcel    {
879130809Smarcel      wrap_here ("");
880130809Smarcel      printf_unfiltered ("(no debugging symbols found)...");
881130809Smarcel      wrap_here ("");
882130809Smarcel    }
883130809Smarcel
88419370Spst  if (from_tty || info_verbose)
88519370Spst    {
88646289Sdfr      if (post_add_symbol_hook)
88799000Sobrien	post_add_symbol_hook ();
88846289Sdfr      else
88999000Sobrien	{
890130809Smarcel	  printf_unfiltered ("done.\n");
89199000Sobrien	}
89219370Spst    }
89319370Spst
894130809Smarcel  /* We print some messages regardless of whether 'from_tty ||
895130809Smarcel     info_verbose' is true, so make sure they go out at the right
896130809Smarcel     time.  */
897130809Smarcel  gdb_flush (gdb_stdout);
898130809Smarcel
899130809Smarcel  do_cleanups (my_cleanups);
900130809Smarcel
901130809Smarcel  if (objfile->sf == NULL)
902130809Smarcel    return objfile;	/* No symbols. */
903130809Smarcel
90419370Spst  new_symfile_objfile (objfile, mainline, from_tty);
90519370Spst
90699000Sobrien  if (target_new_objfile_hook)
90799000Sobrien    target_new_objfile_hook (objfile);
90846289Sdfr
90919370Spst  return (objfile);
91019370Spst}
91119370Spst
912130809Smarcel
913130809Smarcel/* Process a symbol file, as either the main file or as a dynamically
914130809Smarcel   loaded file.  See symbol_file_add_with_addrs_or_offsets's comments
915130809Smarcel   for details.  */
916130809Smarcelstruct objfile *
917130809Smarcelsymbol_file_add (char *name, int from_tty, struct section_addr_info *addrs,
918130809Smarcel		 int mainline, int flags)
919130809Smarcel{
920130809Smarcel  return symbol_file_add_with_addrs_or_offsets (name, from_tty, addrs, 0, 0,
921130809Smarcel                                                mainline, flags);
922130809Smarcel}
923130809Smarcel
924130809Smarcel
92599000Sobrien/* Call symbol_file_add() with default values and update whatever is
92699000Sobrien   affected by the loading of a new main().
92799000Sobrien   Used when the file is supplied in the gdb command line
92899000Sobrien   and by some targets with special loading requirements.
92999000Sobrien   The auxiliary function, symbol_file_add_main_1(), has the flags
93099000Sobrien   argument for the switches that can only be specified in the symbol_file
93199000Sobrien   command itself.  */
93299000Sobrien
93399000Sobrienvoid
93499000Sobriensymbol_file_add_main (char *args, int from_tty)
93599000Sobrien{
93699000Sobrien  symbol_file_add_main_1 (args, from_tty, 0);
93799000Sobrien}
93899000Sobrien
93999000Sobrienstatic void
94099000Sobriensymbol_file_add_main_1 (char *args, int from_tty, int flags)
94199000Sobrien{
94299000Sobrien  symbol_file_add (args, from_tty, NULL, 1, flags);
94399000Sobrien
94499000Sobrien#ifdef HPUXHPPA
94599000Sobrien  RESET_HP_UX_GLOBALS ();
94699000Sobrien#endif
94799000Sobrien
94899000Sobrien  /* Getting new symbols may change our opinion about
94999000Sobrien     what is frameless.  */
95099000Sobrien  reinit_frame_cache ();
95199000Sobrien
95299000Sobrien  set_initial_language ();
95399000Sobrien}
95499000Sobrien
95599000Sobrienvoid
95699000Sobriensymbol_file_clear (int from_tty)
95799000Sobrien{
95899000Sobrien  if ((have_full_symbols () || have_partial_symbols ())
95999000Sobrien      && from_tty
96099000Sobrien      && !query ("Discard symbol table from `%s'? ",
96199000Sobrien		 symfile_objfile->name))
96299000Sobrien    error ("Not confirmed.");
96399000Sobrien    free_all_objfiles ();
96499000Sobrien
96599000Sobrien    /* solib descriptors may have handles to objfiles.  Since their
96699000Sobrien       storage has just been released, we'd better wipe the solib
96799000Sobrien       descriptors as well.
96899000Sobrien     */
96999000Sobrien#if defined(SOLIB_RESTART)
97099000Sobrien    SOLIB_RESTART ();
97199000Sobrien#endif
97299000Sobrien
97399000Sobrien    symfile_objfile = NULL;
97499000Sobrien    if (from_tty)
97599000Sobrien      printf_unfiltered ("No symbol file now.\n");
97699000Sobrien#ifdef HPUXHPPA
97799000Sobrien    RESET_HP_UX_GLOBALS ();
97899000Sobrien#endif
97999000Sobrien}
98099000Sobrien
981130809Smarcelstatic char *
982130809Smarcelget_debug_link_info (struct objfile *objfile, unsigned long *crc32_out)
983130809Smarcel{
984130809Smarcel  asection *sect;
985130809Smarcel  bfd_size_type debuglink_size;
986130809Smarcel  unsigned long crc32;
987130809Smarcel  char *contents;
988130809Smarcel  int crc_offset;
989130809Smarcel  unsigned char *p;
990130809Smarcel
991130809Smarcel  sect = bfd_get_section_by_name (objfile->obfd, ".gnu_debuglink");
992130809Smarcel
993130809Smarcel  if (sect == NULL)
994130809Smarcel    return NULL;
995130809Smarcel
996130809Smarcel  debuglink_size = bfd_section_size (objfile->obfd, sect);
997130809Smarcel
998130809Smarcel  contents = xmalloc (debuglink_size);
999130809Smarcel  bfd_get_section_contents (objfile->obfd, sect, contents,
1000130809Smarcel			    (file_ptr)0, (bfd_size_type)debuglink_size);
1001130809Smarcel
1002130809Smarcel  /* Crc value is stored after the filename, aligned up to 4 bytes. */
1003130809Smarcel  crc_offset = strlen (contents) + 1;
1004130809Smarcel  crc_offset = (crc_offset + 3) & ~3;
1005130809Smarcel
1006130809Smarcel  crc32 = bfd_get_32 (objfile->obfd, (bfd_byte *) (contents + crc_offset));
1007130809Smarcel
1008130809Smarcel  *crc32_out = crc32;
1009130809Smarcel  return contents;
1010130809Smarcel}
1011130809Smarcel
1012130809Smarcelstatic int
1013130809Smarcelseparate_debug_file_exists (const char *name, unsigned long crc)
1014130809Smarcel{
1015130809Smarcel  unsigned long file_crc = 0;
1016130809Smarcel  int fd;
1017130809Smarcel  char buffer[8*1024];
1018130809Smarcel  int count;
1019130809Smarcel
1020130809Smarcel  fd = open (name, O_RDONLY | O_BINARY);
1021130809Smarcel  if (fd < 0)
1022130809Smarcel    return 0;
1023130809Smarcel
1024130809Smarcel  while ((count = read (fd, buffer, sizeof (buffer))) > 0)
1025130809Smarcel    file_crc = gnu_debuglink_crc32 (file_crc, buffer, count);
1026130809Smarcel
1027130809Smarcel  close (fd);
1028130809Smarcel
1029130809Smarcel  return crc == file_crc;
1030130809Smarcel}
1031130809Smarcel
1032130809Smarcelstatic char *debug_file_directory = NULL;
1033130809Smarcel
1034130809Smarcel#if ! defined (DEBUG_SUBDIRECTORY)
1035130809Smarcel#define DEBUG_SUBDIRECTORY ".debug"
1036130809Smarcel#endif
1037130809Smarcel
1038130809Smarcelstatic char *
1039130809Smarcelfind_separate_debug_file (struct objfile *objfile)
1040130809Smarcel{
1041130809Smarcel  asection *sect;
1042130809Smarcel  char *basename;
1043130809Smarcel  char *dir;
1044130809Smarcel  char *debugfile;
1045130809Smarcel  char *name_copy;
1046130809Smarcel  bfd_size_type debuglink_size;
1047130809Smarcel  unsigned long crc32;
1048130809Smarcel  int i;
1049130809Smarcel
1050130809Smarcel  basename = get_debug_link_info (objfile, &crc32);
1051130809Smarcel
1052130809Smarcel  if (basename == NULL)
1053130809Smarcel    return NULL;
1054130809Smarcel
1055130809Smarcel  dir = xstrdup (objfile->name);
1056130809Smarcel
1057130809Smarcel  /* Strip off the final filename part, leaving the directory name,
1058130809Smarcel     followed by a slash.  Objfile names should always be absolute and
1059130809Smarcel     tilde-expanded, so there should always be a slash in there
1060130809Smarcel     somewhere.  */
1061130809Smarcel  for (i = strlen(dir) - 1; i >= 0; i--)
1062130809Smarcel    {
1063130809Smarcel      if (IS_DIR_SEPARATOR (dir[i]))
1064130809Smarcel	break;
1065130809Smarcel    }
1066130809Smarcel  gdb_assert (i >= 0 && IS_DIR_SEPARATOR (dir[i]));
1067130809Smarcel  dir[i+1] = '\0';
1068130809Smarcel
1069130809Smarcel  debugfile = alloca (strlen (debug_file_directory) + 1
1070130809Smarcel                      + strlen (dir)
1071130809Smarcel                      + strlen (DEBUG_SUBDIRECTORY)
1072130809Smarcel                      + strlen ("/")
1073130809Smarcel                      + strlen (basename)
1074130809Smarcel                      + 1);
1075130809Smarcel
1076130809Smarcel  /* First try in the same directory as the original file.  */
1077130809Smarcel  strcpy (debugfile, dir);
1078130809Smarcel  strcat (debugfile, basename);
1079130809Smarcel
1080130809Smarcel  if (separate_debug_file_exists (debugfile, crc32))
1081130809Smarcel    {
1082130809Smarcel      xfree (basename);
1083130809Smarcel      xfree (dir);
1084130809Smarcel      return xstrdup (debugfile);
1085130809Smarcel    }
1086130809Smarcel
1087130809Smarcel  /* Then try in the subdirectory named DEBUG_SUBDIRECTORY.  */
1088130809Smarcel  strcpy (debugfile, dir);
1089130809Smarcel  strcat (debugfile, DEBUG_SUBDIRECTORY);
1090130809Smarcel  strcat (debugfile, "/");
1091130809Smarcel  strcat (debugfile, basename);
1092130809Smarcel
1093130809Smarcel  if (separate_debug_file_exists (debugfile, crc32))
1094130809Smarcel    {
1095130809Smarcel      xfree (basename);
1096130809Smarcel      xfree (dir);
1097130809Smarcel      return xstrdup (debugfile);
1098130809Smarcel    }
1099130809Smarcel
1100130809Smarcel  /* Then try in the global debugfile directory.  */
1101130809Smarcel  strcpy (debugfile, debug_file_directory);
1102130809Smarcel  strcat (debugfile, "/");
1103130809Smarcel  strcat (debugfile, dir);
1104130809Smarcel  strcat (debugfile, basename);
1105130809Smarcel
1106130809Smarcel  if (separate_debug_file_exists (debugfile, crc32))
1107130809Smarcel    {
1108130809Smarcel      xfree (basename);
1109130809Smarcel      xfree (dir);
1110130809Smarcel      return xstrdup (debugfile);
1111130809Smarcel    }
1112130809Smarcel
1113130809Smarcel  xfree (basename);
1114130809Smarcel  xfree (dir);
1115130809Smarcel  return NULL;
1116130809Smarcel}
1117130809Smarcel
1118130809Smarcel
111919370Spst/* This is the symbol-file command.  Read the file, analyze its
112019370Spst   symbols, and add a struct symtab to a symtab list.  The syntax of
112119370Spst   the command is rather bizarre--(1) buildargv implements various
112219370Spst   quoting conventions which are undocumented and have little or
112319370Spst   nothing in common with the way things are quoted (or not quoted)
112419370Spst   elsewhere in GDB, (2) options are used, which are not generally
112519370Spst   used in GDB (perhaps "set mapped on", "set readnow on" would be
112619370Spst   better), (3) the order of options matters, which is contrary to GNU
112719370Spst   conventions (because it is confusing and inconvenient).  */
112899000Sobrien/* Note: ezannoni 2000-04-17. This function used to have support for
112999000Sobrien   rombug (see remote-os9k.c). It consisted of a call to target_link()
113099000Sobrien   (target.c) to get the address of the text segment from the target,
113199000Sobrien   and pass that to symbol_file_add(). This is no longer supported. */
113219370Spst
113319370Spstvoid
113499000Sobriensymbol_file_command (char *args, int from_tty)
113519370Spst{
113619370Spst  char **argv;
113719370Spst  char *name = NULL;
113819370Spst  struct cleanup *cleanups;
113999000Sobrien  int flags = OBJF_USERLOADED;
114019370Spst
114119370Spst  dont_repeat ();
114219370Spst
114319370Spst  if (args == NULL)
114419370Spst    {
114599000Sobrien      symbol_file_clear (from_tty);
114619370Spst    }
114719370Spst  else
114819370Spst    {
114919370Spst      if ((argv = buildargv (args)) == NULL)
115019370Spst	{
115119370Spst	  nomem (0);
115219370Spst	}
115399000Sobrien      cleanups = make_cleanup_freeargv (argv);
115419370Spst      while (*argv != NULL)
115519370Spst	{
1156130809Smarcel	  if (strcmp (*argv, "-readnow") == 0)
1157130809Smarcel	    flags |= OBJF_READNOW;
1158130809Smarcel	  else if (**argv == '-')
1159130809Smarcel	    error ("unknown option `%s'", *argv);
1160130809Smarcel	  else
1161130809Smarcel	    {
1162130809Smarcel	      name = *argv;
1163130809Smarcel
1164130809Smarcel	      symbol_file_add_main_1 (name, from_tty, flags);
1165130809Smarcel	    }
116619370Spst	  argv++;
116719370Spst	}
116819370Spst
116919370Spst      if (name == NULL)
117019370Spst	{
117119370Spst	  error ("no symbol file name was specified");
117219370Spst	}
117319370Spst      do_cleanups (cleanups);
117419370Spst    }
117519370Spst}
117619370Spst
117719370Spst/* Set the initial language.
117819370Spst
117919370Spst   A better solution would be to record the language in the psymtab when reading
118019370Spst   partial symbols, and then use it (if known) to set the language.  This would
118119370Spst   be a win for formats that encode the language in an easily discoverable place,
118219370Spst   such as DWARF.  For stabs, we can jump through hoops looking for specially
118319370Spst   named symbols or try to intuit the language from the specific type of stabs
118419370Spst   we find, but we can't do that until later when we read in full symbols.
118519370Spst   FIXME.  */
118619370Spst
118719370Spststatic void
118899000Sobrienset_initial_language (void)
118919370Spst{
119019370Spst  struct partial_symtab *pst;
119199000Sobrien  enum language lang = language_unknown;
119219370Spst
119319370Spst  pst = find_main_psymtab ();
119419370Spst  if (pst != NULL)
119519370Spst    {
119699000Sobrien      if (pst->filename != NULL)
119719370Spst	{
119899000Sobrien	  lang = deduce_language_from_filename (pst->filename);
119999000Sobrien	}
120019370Spst      if (lang == language_unknown)
120119370Spst	{
120299000Sobrien	  /* Make C the default language */
120399000Sobrien	  lang = language_c;
120419370Spst	}
120519370Spst      set_language (lang);
120619370Spst      expected_language = current_language;	/* Don't warn the user */
120719370Spst    }
120819370Spst}
120919370Spst
121019370Spst/* Open file specified by NAME and hand it off to BFD for preliminary
121119370Spst   analysis.  Result is a newly initialized bfd *, which includes a newly
121219370Spst   malloc'd` copy of NAME (tilde-expanded and made absolute).
121319370Spst   In case of trouble, error() is called.  */
121419370Spst
121546289Sdfrbfd *
121699000Sobriensymfile_bfd_open (char *name)
121719370Spst{
121819370Spst  bfd *sym_bfd;
121919370Spst  int desc;
122019370Spst  char *absolute_name;
122119370Spst
122246289Sdfr
122346289Sdfr
122419370Spst  name = tilde_expand (name);	/* Returns 1st new malloc'd copy */
122519370Spst
122619370Spst  /* Look down path for it, allocate 2nd new malloc'd copy.  */
122719370Spst  desc = openp (getenv ("PATH"), 1, name, O_RDONLY | O_BINARY, 0, &absolute_name);
122899000Sobrien#if defined(__GO32__) || defined(_WIN32) || defined (__CYGWIN__)
122919370Spst  if (desc < 0)
123019370Spst    {
123146289Sdfr      char *exename = alloca (strlen (name) + 5);
123246289Sdfr      strcat (strcpy (exename, name), ".exe");
123346289Sdfr      desc = openp (getenv ("PATH"), 1, exename, O_RDONLY | O_BINARY,
123499000Sobrien		    0, &absolute_name);
123546289Sdfr    }
123646289Sdfr#endif
123746289Sdfr  if (desc < 0)
123846289Sdfr    {
123999000Sobrien      make_cleanup (xfree, name);
124019370Spst      perror_with_name (name);
124119370Spst    }
124299000Sobrien  xfree (name);			/* Free 1st new malloc'd copy */
124319370Spst  name = absolute_name;		/* Keep 2nd malloc'd copy in bfd */
124499000Sobrien  /* It'll be freed in free_objfile(). */
124519370Spst
124619370Spst  sym_bfd = bfd_fdopenr (name, gnutarget, desc);
124719370Spst  if (!sym_bfd)
124819370Spst    {
124919370Spst      close (desc);
125099000Sobrien      make_cleanup (xfree, name);
125119370Spst      error ("\"%s\": can't open to read symbols: %s.", name,
125219370Spst	     bfd_errmsg (bfd_get_error ()));
125319370Spst    }
1254130809Smarcel  bfd_set_cacheable (sym_bfd, 1);
125519370Spst
125619370Spst  if (!bfd_check_format (sym_bfd, bfd_object))
125719370Spst    {
125819370Spst      /* FIXME: should be checking for errors from bfd_close (for one thing,
125999000Sobrien         on error it does not free all the storage associated with the
126099000Sobrien         bfd).  */
126119370Spst      bfd_close (sym_bfd);	/* This also closes desc */
126299000Sobrien      make_cleanup (xfree, name);
126319370Spst      error ("\"%s\": can't read symbols: %s.", name,
126419370Spst	     bfd_errmsg (bfd_get_error ()));
126519370Spst    }
126619370Spst  return (sym_bfd);
126719370Spst}
126819370Spst
126999000Sobrien/* Return the section index for the given section name. Return -1 if
127099000Sobrien   the section was not found. */
127199000Sobrienint
127299000Sobrienget_section_index (struct objfile *objfile, char *section_name)
127399000Sobrien{
127499000Sobrien  asection *sect = bfd_get_section_by_name (objfile->obfd, section_name);
127599000Sobrien  if (sect)
127699000Sobrien    return sect->index;
127799000Sobrien  else
127899000Sobrien    return -1;
127999000Sobrien}
128099000Sobrien
128119370Spst/* Link a new symtab_fns into the global symtab_fns list.  Called on gdb
128219370Spst   startup by the _initialize routine in each object file format reader,
128319370Spst   to register information about each format the the reader is prepared
128419370Spst   to handle. */
128519370Spst
128619370Spstvoid
128799000Sobrienadd_symtab_fns (struct sym_fns *sf)
128819370Spst{
128919370Spst  sf->next = symtab_fns;
129019370Spst  symtab_fns = sf;
129119370Spst}
129219370Spst
129319370Spst
129419370Spst/* Initialize to read symbols from the symbol file sym_bfd.  It either
129519370Spst   returns or calls error().  The result is an initialized struct sym_fns
129619370Spst   in the objfile structure, that contains cached information about the
129719370Spst   symbol file.  */
129819370Spst
129919370Spststatic void
130099000Sobrienfind_sym_fns (struct objfile *objfile)
130119370Spst{
130219370Spst  struct sym_fns *sf;
130399000Sobrien  enum bfd_flavour our_flavour = bfd_get_flavour (objfile->obfd);
130499000Sobrien  char *our_target = bfd_get_target (objfile->obfd);
130519370Spst
1306130809Smarcel  if (our_flavour == bfd_target_srec_flavour
1307130809Smarcel      || our_flavour == bfd_target_ihex_flavour
1308130809Smarcel      || our_flavour == bfd_target_tekhex_flavour)
1309130809Smarcel    return;	/* No symbols. */
131019370Spst
131199000Sobrien  for (sf = symtab_fns; sf != NULL; sf = sf->next)
131219370Spst    {
131399000Sobrien      if (our_flavour == sf->sym_flavour)
131419370Spst	{
131599000Sobrien	  objfile->sf = sf;
131619370Spst	  return;
131719370Spst	}
131819370Spst    }
131919370Spst  error ("I'm sorry, Dave, I can't do that.  Symbol format `%s' unknown.",
132099000Sobrien	 bfd_get_target (objfile->obfd));
132119370Spst}
132219370Spst
132319370Spst/* This function runs the load command of our current target.  */
132419370Spst
132519370Spststatic void
132699000Sobrienload_command (char *arg, int from_tty)
132719370Spst{
132819370Spst  if (arg == NULL)
132919370Spst    arg = get_exec_file (1);
133019370Spst  target_load (arg, from_tty);
133199000Sobrien
133299000Sobrien  /* After re-loading the executable, we don't really know which
133399000Sobrien     overlays are mapped any more.  */
133499000Sobrien  overlay_cache_invalid = 1;
133519370Spst}
133619370Spst
133719370Spst/* This version of "load" should be usable for any target.  Currently
133819370Spst   it is just used for remote targets, not inftarg.c or core files,
133919370Spst   on the theory that only in that case is it useful.
134019370Spst
134119370Spst   Avoiding xmodem and the like seems like a win (a) because we don't have
134219370Spst   to worry about finding it, and (b) On VMS, fork() is very slow and so
134319370Spst   we don't want to run a subprocess.  On the other hand, I'm not sure how
134419370Spst   performance compares.  */
134599000Sobrien
134699000Sobrienstatic int download_write_size = 512;
134799000Sobrienstatic int validate_download = 0;
134899000Sobrien
134999000Sobrien/* Callback service function for generic_load (bfd_map_over_sections).  */
135099000Sobrien
135199000Sobrienstatic void
135299000Sobrienadd_section_size_callback (bfd *abfd, asection *asec, void *data)
135399000Sobrien{
135499000Sobrien  bfd_size_type *sum = data;
135599000Sobrien
1356218822Sdim  *sum += bfd_get_section_size (asec);
135799000Sobrien}
135899000Sobrien
135999000Sobrien/* Opaque data for load_section_callback.  */
136099000Sobrienstruct load_section_data {
136199000Sobrien  unsigned long load_offset;
136299000Sobrien  unsigned long write_count;
136399000Sobrien  unsigned long data_count;
136499000Sobrien  bfd_size_type total_size;
136599000Sobrien};
136699000Sobrien
136799000Sobrien/* Callback service function for generic_load (bfd_map_over_sections).  */
136899000Sobrien
136999000Sobrienstatic void
137099000Sobrienload_section_callback (bfd *abfd, asection *asec, void *data)
137199000Sobrien{
137299000Sobrien  struct load_section_data *args = data;
137399000Sobrien
137499000Sobrien  if (bfd_get_section_flags (abfd, asec) & SEC_LOAD)
137599000Sobrien    {
1376218822Sdim      bfd_size_type size = bfd_get_section_size (asec);
137799000Sobrien      if (size > 0)
137899000Sobrien	{
137999000Sobrien	  char *buffer;
138099000Sobrien	  struct cleanup *old_chain;
138199000Sobrien	  CORE_ADDR lma = bfd_section_lma (abfd, asec) + args->load_offset;
138299000Sobrien	  bfd_size_type block_size;
138399000Sobrien	  int err;
138499000Sobrien	  const char *sect_name = bfd_get_section_name (abfd, asec);
138599000Sobrien	  bfd_size_type sent;
138699000Sobrien
138799000Sobrien	  if (download_write_size > 0 && size > download_write_size)
138899000Sobrien	    block_size = download_write_size;
138999000Sobrien	  else
139099000Sobrien	    block_size = size;
139199000Sobrien
139299000Sobrien	  buffer = xmalloc (size);
139399000Sobrien	  old_chain = make_cleanup (xfree, buffer);
139499000Sobrien
139599000Sobrien	  /* Is this really necessary?  I guess it gives the user something
139699000Sobrien	     to look at during a long download.  */
139799000Sobrien	  ui_out_message (uiout, 0, "Loading section %s, size 0x%s lma 0x%s\n",
139899000Sobrien			  sect_name, paddr_nz (size), paddr_nz (lma));
139999000Sobrien
140099000Sobrien	  bfd_get_section_contents (abfd, asec, buffer, 0, size);
140199000Sobrien
140299000Sobrien	  sent = 0;
140399000Sobrien	  do
140499000Sobrien	    {
140599000Sobrien	      int len;
140699000Sobrien	      bfd_size_type this_transfer = size - sent;
140799000Sobrien
140899000Sobrien	      if (this_transfer >= block_size)
140999000Sobrien		this_transfer = block_size;
141099000Sobrien	      len = target_write_memory_partial (lma, buffer,
141199000Sobrien						 this_transfer, &err);
141299000Sobrien	      if (err)
141399000Sobrien		break;
141499000Sobrien	      if (validate_download)
141599000Sobrien		{
141699000Sobrien		  /* Broken memories and broken monitors manifest
141799000Sobrien		     themselves here when bring new computers to
141899000Sobrien		     life.  This doubles already slow downloads.  */
141999000Sobrien		  /* NOTE: cagney/1999-10-18: A more efficient
142099000Sobrien		     implementation might add a verify_memory()
142199000Sobrien		     method to the target vector and then use
142299000Sobrien		     that.  remote.c could implement that method
142399000Sobrien		     using the ``qCRC'' packet.  */
142499000Sobrien		  char *check = xmalloc (len);
142599000Sobrien		  struct cleanup *verify_cleanups =
142699000Sobrien		    make_cleanup (xfree, check);
142799000Sobrien
142899000Sobrien		  if (target_read_memory (lma, check, len) != 0)
142999000Sobrien		    error ("Download verify read failed at 0x%s",
143099000Sobrien			   paddr (lma));
143199000Sobrien		  if (memcmp (buffer, check, len) != 0)
143299000Sobrien		    error ("Download verify compare failed at 0x%s",
143399000Sobrien			   paddr (lma));
143499000Sobrien		  do_cleanups (verify_cleanups);
143599000Sobrien		}
143699000Sobrien	      args->data_count += len;
143799000Sobrien	      lma += len;
143899000Sobrien	      buffer += len;
143999000Sobrien	      args->write_count += 1;
144099000Sobrien	      sent += len;
144199000Sobrien	      if (quit_flag
144299000Sobrien		  || (ui_load_progress_hook != NULL
144399000Sobrien		      && ui_load_progress_hook (sect_name, sent)))
144499000Sobrien		error ("Canceled the download");
144599000Sobrien
144699000Sobrien	      if (show_load_progress != NULL)
144799000Sobrien		show_load_progress (sect_name, sent, size,
144899000Sobrien				    args->data_count, args->total_size);
144999000Sobrien	    }
145099000Sobrien	  while (sent < size);
145199000Sobrien
145299000Sobrien	  if (err != 0)
145399000Sobrien	    error ("Memory access error while loading section %s.", sect_name);
145499000Sobrien
145599000Sobrien	  do_cleanups (old_chain);
145699000Sobrien	}
145799000Sobrien    }
145899000Sobrien}
145999000Sobrien
146019370Spstvoid
146199000Sobriengeneric_load (char *args, int from_tty)
146219370Spst{
146319370Spst  asection *s;
146419370Spst  bfd *loadfile_bfd;
146519370Spst  time_t start_time, end_time;	/* Start and end times of download */
146699000Sobrien  char *filename;
146799000Sobrien  struct cleanup *old_cleanups;
146899000Sobrien  char *offptr;
146999000Sobrien  struct load_section_data cbdata;
147099000Sobrien  CORE_ADDR entry;
147119370Spst
147299000Sobrien  cbdata.load_offset = 0;	/* Offset to add to vma for each section. */
147399000Sobrien  cbdata.write_count = 0;	/* Number of writes needed. */
147499000Sobrien  cbdata.data_count = 0;	/* Number of bytes written to target memory. */
147599000Sobrien  cbdata.total_size = 0;	/* Total size of all bfd sectors. */
147699000Sobrien
147799000Sobrien  /* Parse the input argument - the user can specify a load offset as
147899000Sobrien     a second argument. */
147999000Sobrien  filename = xmalloc (strlen (args) + 1);
148099000Sobrien  old_cleanups = make_cleanup (xfree, filename);
148199000Sobrien  strcpy (filename, args);
148299000Sobrien  offptr = strchr (filename, ' ');
148399000Sobrien  if (offptr != NULL)
148499000Sobrien    {
148599000Sobrien      char *endptr;
148699000Sobrien
148799000Sobrien      cbdata.load_offset = strtoul (offptr, &endptr, 0);
148899000Sobrien      if (offptr == endptr)
148999000Sobrien	error ("Invalid download offset:%s\n", offptr);
149099000Sobrien      *offptr = '\0';
149199000Sobrien    }
149246289Sdfr  else
149399000Sobrien    cbdata.load_offset = 0;
149446289Sdfr
149599000Sobrien  /* Open the file for loading. */
149619370Spst  loadfile_bfd = bfd_openr (filename, gnutarget);
149719370Spst  if (loadfile_bfd == NULL)
149819370Spst    {
149919370Spst      perror_with_name (filename);
150019370Spst      return;
150119370Spst    }
150299000Sobrien
150319370Spst  /* FIXME: should be checking for errors from bfd_close (for one thing,
150419370Spst     on error it does not free all the storage associated with the
150519370Spst     bfd).  */
150699000Sobrien  make_cleanup_bfd_close (loadfile_bfd);
150719370Spst
150899000Sobrien  if (!bfd_check_format (loadfile_bfd, bfd_object))
150919370Spst    {
151019370Spst      error ("\"%s\" is not an object file: %s", filename,
151119370Spst	     bfd_errmsg (bfd_get_error ()));
151219370Spst    }
151319370Spst
151499000Sobrien  bfd_map_over_sections (loadfile_bfd, add_section_size_callback,
151599000Sobrien			 (void *) &cbdata.total_size);
151619370Spst
151799000Sobrien  start_time = time (NULL);
151819370Spst
151999000Sobrien  bfd_map_over_sections (loadfile_bfd, load_section_callback, &cbdata);
152019370Spst
152199000Sobrien  end_time = time (NULL);
152219370Spst
152399000Sobrien  entry = bfd_get_start_address (loadfile_bfd);
152499000Sobrien  ui_out_text (uiout, "Start address ");
152599000Sobrien  ui_out_field_fmt (uiout, "address", "0x%s", paddr_nz (entry));
152699000Sobrien  ui_out_text (uiout, ", load size ");
152799000Sobrien  ui_out_field_fmt (uiout, "load-size", "%lu", cbdata.data_count);
152899000Sobrien  ui_out_text (uiout, "\n");
152999000Sobrien  /* We were doing this in remote-mips.c, I suspect it is right
153099000Sobrien     for other targets too.  */
153199000Sobrien  write_pc (entry);
153219370Spst
1533130809Smarcel  /* FIXME: are we supposed to call symbol_file_add or not?  According
1534130809Smarcel     to a comment from remote-mips.c (where a call to symbol_file_add
1535130809Smarcel     was commented out), making the call confuses GDB if more than one
1536130809Smarcel     file is loaded in.  Some targets do (e.g., remote-vx.c) but
1537130809Smarcel     others don't (or didn't - perhaphs they have all been deleted).  */
153819370Spst
153999000Sobrien  print_transfer_performance (gdb_stdout, cbdata.data_count,
154099000Sobrien			      cbdata.write_count, end_time - start_time);
154119370Spst
154219370Spst  do_cleanups (old_cleanups);
154319370Spst}
154419370Spst
154546289Sdfr/* Report how fast the transfer went. */
154646289Sdfr
154799000Sobrien/* DEPRECATED: cagney/1999-10-18: report_transfer_performance is being
154899000Sobrien   replaced by print_transfer_performance (with a very different
154999000Sobrien   function signature). */
155099000Sobrien
155146289Sdfrvoid
155299000Sobrienreport_transfer_performance (unsigned long data_count, time_t start_time,
155399000Sobrien			     time_t end_time)
155446289Sdfr{
155599000Sobrien  print_transfer_performance (gdb_stdout, data_count,
155699000Sobrien			      end_time - start_time, 0);
155799000Sobrien}
155899000Sobrien
155999000Sobrienvoid
156099000Sobrienprint_transfer_performance (struct ui_file *stream,
156199000Sobrien			    unsigned long data_count,
156299000Sobrien			    unsigned long write_count,
156399000Sobrien			    unsigned long time_count)
156499000Sobrien{
156599000Sobrien  ui_out_text (uiout, "Transfer rate: ");
156699000Sobrien  if (time_count > 0)
156799000Sobrien    {
156899000Sobrien      ui_out_field_fmt (uiout, "transfer-rate", "%lu",
156999000Sobrien			(data_count * 8) / time_count);
157099000Sobrien      ui_out_text (uiout, " bits/sec");
157199000Sobrien    }
157246289Sdfr  else
157399000Sobrien    {
157499000Sobrien      ui_out_field_fmt (uiout, "transferred-bits", "%lu", (data_count * 8));
157599000Sobrien      ui_out_text (uiout, " bits in <1 sec");
157699000Sobrien    }
157799000Sobrien  if (write_count > 0)
157899000Sobrien    {
157999000Sobrien      ui_out_text (uiout, ", ");
158099000Sobrien      ui_out_field_fmt (uiout, "write-rate", "%lu", data_count / write_count);
158199000Sobrien      ui_out_text (uiout, " bytes/write");
158299000Sobrien    }
158399000Sobrien  ui_out_text (uiout, ".\n");
158446289Sdfr}
158546289Sdfr
158619370Spst/* This function allows the addition of incrementally linked object files.
158719370Spst   It does not modify any state in the target, only in the debugger.  */
158899000Sobrien/* Note: ezannoni 2000-04-13 This function/command used to have a
158999000Sobrien   special case syntax for the rombug target (Rombug is the boot
159099000Sobrien   monitor for Microware's OS-9 / OS-9000, see remote-os9k.c). In the
159199000Sobrien   rombug case, the user doesn't need to supply a text address,
159299000Sobrien   instead a call to target_link() (in target.c) would supply the
159399000Sobrien   value to use. We are now discontinuing this type of ad hoc syntax. */
159419370Spst
159519370Spststatic void
159699000Sobrienadd_symbol_file_command (char *args, int from_tty)
159719370Spst{
159899000Sobrien  char *filename = NULL;
159999000Sobrien  int flags = OBJF_USERLOADED;
160019370Spst  char *arg;
160199000Sobrien  int expecting_option = 0;
160299000Sobrien  int section_index = 0;
160399000Sobrien  int argcnt = 0;
160499000Sobrien  int sec_num = 0;
160599000Sobrien  int i;
160699000Sobrien  int expecting_sec_name = 0;
160799000Sobrien  int expecting_sec_addr = 0;
160899000Sobrien
1609130809Smarcel  struct sect_opt
161099000Sobrien  {
161199000Sobrien    char *name;
161299000Sobrien    char *value;
1613130809Smarcel  };
161499000Sobrien
1615130809Smarcel  struct section_addr_info *section_addrs;
1616130809Smarcel  struct sect_opt *sect_opts = NULL;
1617130809Smarcel  size_t num_sect_opts = 0;
161899000Sobrien  struct cleanup *my_cleanups = make_cleanup (null_cleanup, NULL);
161999000Sobrien
1620130809Smarcel  num_sect_opts = 16;
1621130809Smarcel  sect_opts = (struct sect_opt *) xmalloc (num_sect_opts
1622130809Smarcel					   * sizeof (struct sect_opt));
1623130809Smarcel
162419370Spst  dont_repeat ();
162519370Spst
162619370Spst  if (args == NULL)
162799000Sobrien    error ("add-symbol-file takes a file name and an address");
162819370Spst
162919370Spst  /* Make a copy of the string that we can safely write into. */
163099000Sobrien  args = xstrdup (args);
163119370Spst
163299000Sobrien  while (*args != '\000')
163399000Sobrien    {
163499000Sobrien      /* Any leading spaces? */
163599000Sobrien      while (isspace (*args))
163699000Sobrien	args++;
163719370Spst
163899000Sobrien      /* Point arg to the beginning of the argument. */
163919370Spst      arg = args;
164099000Sobrien
164199000Sobrien      /* Move args pointer over the argument. */
164299000Sobrien      while ((*args != '\000') && !isspace (*args))
164399000Sobrien	args++;
164499000Sobrien
164599000Sobrien      /* If there are more arguments, terminate arg and
164699000Sobrien         proceed past it. */
164719370Spst      if (*args != '\000')
164899000Sobrien	*args++ = '\000';
164999000Sobrien
165099000Sobrien      /* Now process the argument. */
165199000Sobrien      if (argcnt == 0)
165219370Spst	{
165399000Sobrien	  /* The first argument is the file name. */
165499000Sobrien	  filename = tilde_expand (arg);
165599000Sobrien	  make_cleanup (xfree, filename);
165619370Spst	}
165719370Spst      else
165899000Sobrien	if (argcnt == 1)
165999000Sobrien	  {
166099000Sobrien	    /* The second argument is always the text address at which
166199000Sobrien               to load the program. */
166299000Sobrien	    sect_opts[section_index].name = ".text";
166399000Sobrien	    sect_opts[section_index].value = arg;
1664130809Smarcel	    if (++section_index > num_sect_opts)
1665130809Smarcel	      {
1666130809Smarcel		num_sect_opts *= 2;
1667130809Smarcel		sect_opts = ((struct sect_opt *)
1668130809Smarcel			     xrealloc (sect_opts,
1669130809Smarcel				       num_sect_opts
1670130809Smarcel				       * sizeof (struct sect_opt)));
1671130809Smarcel	      }
167299000Sobrien	  }
167399000Sobrien	else
167499000Sobrien	  {
167599000Sobrien	    /* It's an option (starting with '-') or it's an argument
167699000Sobrien	       to an option */
167799000Sobrien
167899000Sobrien	    if (*arg == '-')
167999000Sobrien	      {
1680130809Smarcel		if (strcmp (arg, "-readnow") == 0)
1681130809Smarcel		  flags |= OBJF_READNOW;
1682130809Smarcel		else if (strcmp (arg, "-s") == 0)
1683130809Smarcel		  {
1684130809Smarcel		    expecting_sec_name = 1;
1685130809Smarcel		    expecting_sec_addr = 1;
1686130809Smarcel		  }
168799000Sobrien	      }
168899000Sobrien	    else
168999000Sobrien	      {
169099000Sobrien		if (expecting_sec_name)
169199000Sobrien		  {
169299000Sobrien		    sect_opts[section_index].name = arg;
169399000Sobrien		    expecting_sec_name = 0;
169499000Sobrien		  }
169599000Sobrien		else
169699000Sobrien		  if (expecting_sec_addr)
169799000Sobrien		    {
169899000Sobrien		      sect_opts[section_index].value = arg;
169999000Sobrien		      expecting_sec_addr = 0;
1700130809Smarcel		      if (++section_index > num_sect_opts)
1701130809Smarcel			{
1702130809Smarcel			  num_sect_opts *= 2;
1703130809Smarcel			  sect_opts = ((struct sect_opt *)
1704130809Smarcel				       xrealloc (sect_opts,
1705130809Smarcel						 num_sect_opts
1706130809Smarcel						 * sizeof (struct sect_opt)));
1707130809Smarcel			}
170899000Sobrien		    }
170999000Sobrien		  else
171099000Sobrien		    error ("USAGE: add-symbol-file <filename> <textaddress> [-mapped] [-readnow] [-s <secname> <addr>]*");
171199000Sobrien	      }
171299000Sobrien	  }
171399000Sobrien      argcnt++;
171419370Spst    }
171519370Spst
171699000Sobrien  /* Print the prompt for the query below. And save the arguments into
171799000Sobrien     a sect_addr_info structure to be passed around to other
171899000Sobrien     functions.  We have to split this up into separate print
171999000Sobrien     statements because local_hex_string returns a local static
172099000Sobrien     string. */
172199000Sobrien
1722130809Smarcel  printf_unfiltered ("add symbol table from file \"%s\" at\n", filename);
1723130809Smarcel  section_addrs = alloc_section_addr_info (section_index);
1724130809Smarcel  make_cleanup (xfree, section_addrs);
172599000Sobrien  for (i = 0; i < section_index; i++)
172619370Spst    {
172799000Sobrien      CORE_ADDR addr;
172899000Sobrien      char *val = sect_opts[i].value;
172999000Sobrien      char *sec = sect_opts[i].name;
173099000Sobrien
1731130809Smarcel      addr = parse_and_eval_address (val);
173219370Spst
173399000Sobrien      /* Here we store the section offsets in the order they were
173499000Sobrien         entered on the command line. */
1735130809Smarcel      section_addrs->other[sec_num].name = sec;
1736130809Smarcel      section_addrs->other[sec_num].addr = addr;
1737130809Smarcel      printf_unfiltered ("\t%s_addr = %s\n",
173899000Sobrien		       sec,
173999000Sobrien		       local_hex_string ((unsigned long)addr));
174099000Sobrien      sec_num++;
174199000Sobrien
174299000Sobrien      /* The object's sections are initialized when a
174399000Sobrien	 call is made to build_objfile_section_table (objfile).
174499000Sobrien	 This happens in reread_symbols.
174599000Sobrien	 At this point, we don't know what file type this is,
174699000Sobrien	 so we can't determine what section names are valid.  */
174719370Spst    }
174819370Spst
174999000Sobrien  if (from_tty && (!query ("%s", "")))
175019370Spst    error ("Not confirmed.");
175119370Spst
1752130809Smarcel  symbol_file_add (filename, from_tty, section_addrs, 0, flags);
175319370Spst
175419370Spst  /* Getting new symbols may change our opinion about what is
175519370Spst     frameless.  */
175619370Spst  reinit_frame_cache ();
175799000Sobrien  do_cleanups (my_cleanups);
175819370Spst}
175919370Spst
176019370Spststatic void
176199000Sobrienadd_shared_symbol_files_command (char *args, int from_tty)
176219370Spst{
176319370Spst#ifdef ADD_SHARED_SYMBOL_FILES
176419370Spst  ADD_SHARED_SYMBOL_FILES (args, from_tty);
176519370Spst#else
176619370Spst  error ("This command is not available in this configuration of GDB.");
176799000Sobrien#endif
176819370Spst}
176919370Spst
177019370Spst/* Re-read symbols if a symbol-file has changed.  */
177119370Spstvoid
177299000Sobrienreread_symbols (void)
177319370Spst{
177419370Spst  struct objfile *objfile;
177519370Spst  long new_modtime;
177619370Spst  int reread_one = 0;
177719370Spst  struct stat new_statbuf;
177819370Spst  int res;
177919370Spst
178019370Spst  /* With the addition of shared libraries, this should be modified,
178119370Spst     the load time should be saved in the partial symbol tables, since
178219370Spst     different tables may come from different source files.  FIXME.
178319370Spst     This routine should then walk down each partial symbol table
178419370Spst     and see if the symbol table that it originates from has been changed */
178519370Spst
178699000Sobrien  for (objfile = object_files; objfile; objfile = objfile->next)
178799000Sobrien    {
178899000Sobrien      if (objfile->obfd)
178999000Sobrien	{
1790130809Smarcel#ifdef DEPRECATED_IBM6000_TARGET
179199000Sobrien	  /* If this object is from a shared library, then you should
179299000Sobrien	     stat on the library name, not member name. */
179319370Spst
179499000Sobrien	  if (objfile->obfd->my_archive)
179599000Sobrien	    res = stat (objfile->obfd->my_archive->filename, &new_statbuf);
179699000Sobrien	  else
179719370Spst#endif
179899000Sobrien	    res = stat (objfile->name, &new_statbuf);
179999000Sobrien	  if (res != 0)
180099000Sobrien	    {
180199000Sobrien	      /* FIXME, should use print_sys_errmsg but it's not filtered. */
1802130809Smarcel	      printf_unfiltered ("`%s' has disappeared; keeping its symbols.\n",
180399000Sobrien			       objfile->name);
180499000Sobrien	      continue;
180599000Sobrien	    }
180699000Sobrien	  new_modtime = new_statbuf.st_mtime;
180799000Sobrien	  if (new_modtime != objfile->mtime)
180899000Sobrien	    {
180999000Sobrien	      struct cleanup *old_cleanups;
181099000Sobrien	      struct section_offsets *offsets;
181199000Sobrien	      int num_offsets;
181299000Sobrien	      char *obfd_filename;
181319370Spst
1814130809Smarcel	      printf_unfiltered ("`%s' has changed; re-reading symbols.\n",
181599000Sobrien			       objfile->name);
181619370Spst
181799000Sobrien	      /* There are various functions like symbol_file_add,
181899000Sobrien	         symfile_bfd_open, syms_from_objfile, etc., which might
181999000Sobrien	         appear to do what we want.  But they have various other
182099000Sobrien	         effects which we *don't* want.  So we just do stuff
182199000Sobrien	         ourselves.  We don't worry about mapped files (for one thing,
182299000Sobrien	         any mapped file will be out of date).  */
182319370Spst
182499000Sobrien	      /* If we get an error, blow away this objfile (not sure if
182599000Sobrien	         that is the correct response for things like shared
182699000Sobrien	         libraries).  */
182799000Sobrien	      old_cleanups = make_cleanup_free_objfile (objfile);
182899000Sobrien	      /* We need to do this whenever any symbols go away.  */
182999000Sobrien	      make_cleanup (clear_symtab_users_cleanup, 0 /*ignore*/);
183019370Spst
183199000Sobrien	      /* Clean up any state BFD has sitting around.  We don't need
183299000Sobrien	         to close the descriptor but BFD lacks a way of closing the
183399000Sobrien	         BFD without closing the descriptor.  */
183499000Sobrien	      obfd_filename = bfd_get_filename (objfile->obfd);
183599000Sobrien	      if (!bfd_close (objfile->obfd))
183699000Sobrien		error ("Can't close BFD for %s: %s", objfile->name,
183799000Sobrien		       bfd_errmsg (bfd_get_error ()));
183899000Sobrien	      objfile->obfd = bfd_openr (obfd_filename, gnutarget);
183999000Sobrien	      if (objfile->obfd == NULL)
184099000Sobrien		error ("Can't open %s to read symbols.", objfile->name);
184199000Sobrien	      /* bfd_openr sets cacheable to true, which is what we want.  */
184299000Sobrien	      if (!bfd_check_format (objfile->obfd, bfd_object))
184399000Sobrien		error ("Can't read symbols from %s: %s.", objfile->name,
184499000Sobrien		       bfd_errmsg (bfd_get_error ()));
184519370Spst
184699000Sobrien	      /* Save the offsets, we will nuke them with the rest of the
1847130809Smarcel	         objfile_obstack.  */
184899000Sobrien	      num_offsets = objfile->num_sections;
1849130809Smarcel	      offsets = ((struct section_offsets *)
1850130809Smarcel			 alloca (SIZEOF_N_SECTION_OFFSETS (num_offsets)));
1851130809Smarcel	      memcpy (offsets, objfile->section_offsets,
1852130809Smarcel		      SIZEOF_N_SECTION_OFFSETS (num_offsets));
185319370Spst
185499000Sobrien	      /* Nuke all the state that we will re-read.  Much of the following
185599000Sobrien	         code which sets things to NULL really is necessary to tell
185699000Sobrien	         other parts of GDB that there is nothing currently there.  */
185719370Spst
185899000Sobrien	      /* FIXME: Do we have to free a whole linked list, or is this
185999000Sobrien	         enough?  */
186099000Sobrien	      if (objfile->global_psymbols.list)
186199000Sobrien		xmfree (objfile->md, objfile->global_psymbols.list);
186299000Sobrien	      memset (&objfile->global_psymbols, 0,
186399000Sobrien		      sizeof (objfile->global_psymbols));
186499000Sobrien	      if (objfile->static_psymbols.list)
186599000Sobrien		xmfree (objfile->md, objfile->static_psymbols.list);
186699000Sobrien	      memset (&objfile->static_psymbols, 0,
186799000Sobrien		      sizeof (objfile->static_psymbols));
186819370Spst
186999000Sobrien	      /* Free the obstacks for non-reusable objfiles */
1870130809Smarcel	      bcache_xfree (objfile->psymbol_cache);
1871130809Smarcel	      objfile->psymbol_cache = bcache_xmalloc ();
1872130809Smarcel	      bcache_xfree (objfile->macro_cache);
1873130809Smarcel	      objfile->macro_cache = bcache_xmalloc ();
1874130809Smarcel	      if (objfile->demangled_names_hash != NULL)
1875130809Smarcel		{
1876130809Smarcel		  htab_delete (objfile->demangled_names_hash);
1877130809Smarcel		  objfile->demangled_names_hash = NULL;
1878130809Smarcel		}
1879130809Smarcel	      obstack_free (&objfile->objfile_obstack, 0);
188099000Sobrien	      objfile->sections = NULL;
188199000Sobrien	      objfile->symtabs = NULL;
188299000Sobrien	      objfile->psymtabs = NULL;
188399000Sobrien	      objfile->free_psymtabs = NULL;
1884130809Smarcel	      objfile->cp_namespace_symtab = NULL;
188599000Sobrien	      objfile->msymbols = NULL;
1886130809Smarcel	      objfile->sym_private = NULL;
188799000Sobrien	      objfile->minimal_symbol_count = 0;
188899000Sobrien	      memset (&objfile->msymbol_hash, 0,
188999000Sobrien		      sizeof (objfile->msymbol_hash));
189099000Sobrien	      memset (&objfile->msymbol_demangled_hash, 0,
189199000Sobrien		      sizeof (objfile->msymbol_demangled_hash));
189299000Sobrien	      objfile->fundamental_types = NULL;
1893130809Smarcel	      clear_objfile_data (objfile);
189499000Sobrien	      if (objfile->sf != NULL)
189599000Sobrien		{
189699000Sobrien		  (*objfile->sf->sym_finish) (objfile);
189799000Sobrien		}
189819370Spst
189999000Sobrien	      /* We never make this a mapped file.  */
190099000Sobrien	      objfile->md = NULL;
1901130809Smarcel	      objfile->psymbol_cache = bcache_xmalloc ();
1902130809Smarcel	      objfile->macro_cache = bcache_xmalloc ();
1903130809Smarcel	      /* obstack_init also initializes the obstack so it is
1904130809Smarcel	         empty.  We could use obstack_specify_allocation but
1905130809Smarcel	         gdb_obstack.h specifies the alloc/dealloc
1906130809Smarcel	         functions.  */
1907130809Smarcel	      obstack_init (&objfile->objfile_obstack);
190899000Sobrien	      if (build_objfile_section_table (objfile))
190999000Sobrien		{
191099000Sobrien		  error ("Can't find the file sections in `%s': %s",
191199000Sobrien			 objfile->name, bfd_errmsg (bfd_get_error ()));
191299000Sobrien		}
1913130809Smarcel              terminate_minimal_symbol_table (objfile);
191419370Spst
191599000Sobrien	      /* We use the same section offsets as from last time.  I'm not
191699000Sobrien	         sure whether that is always correct for shared libraries.  */
191799000Sobrien	      objfile->section_offsets = (struct section_offsets *)
1918130809Smarcel		obstack_alloc (&objfile->objfile_obstack,
1919130809Smarcel			       SIZEOF_N_SECTION_OFFSETS (num_offsets));
1920130809Smarcel	      memcpy (objfile->section_offsets, offsets,
1921130809Smarcel		      SIZEOF_N_SECTION_OFFSETS (num_offsets));
192299000Sobrien	      objfile->num_sections = num_offsets;
192319370Spst
192499000Sobrien	      /* What the hell is sym_new_init for, anyway?  The concept of
192599000Sobrien	         distinguishing between the main file and additional files
192699000Sobrien	         in this way seems rather dubious.  */
192799000Sobrien	      if (objfile == symfile_objfile)
192899000Sobrien		{
192999000Sobrien		  (*objfile->sf->sym_new_init) (objfile);
193046289Sdfr#ifdef HPUXHPPA
193199000Sobrien		  RESET_HP_UX_GLOBALS ();
193246289Sdfr#endif
193399000Sobrien		}
193419370Spst
193599000Sobrien	      (*objfile->sf->sym_init) (objfile);
1936130809Smarcel	      clear_complaints (&symfile_complaints, 1, 1);
193799000Sobrien	      /* The "mainline" parameter is a hideous hack; I think leaving it
193899000Sobrien	         zero is OK since dbxread.c also does what it needs to do if
193999000Sobrien	         objfile->global_psymbols.size is 0.  */
194099000Sobrien	      (*objfile->sf->sym_read) (objfile, 0);
194199000Sobrien	      if (!have_partial_symbols () && !have_full_symbols ())
194299000Sobrien		{
194399000Sobrien		  wrap_here ("");
1944130809Smarcel		  printf_unfiltered ("(no debugging symbols found)\n");
194599000Sobrien		  wrap_here ("");
194699000Sobrien		}
194799000Sobrien	      objfile->flags |= OBJF_SYMS;
194819370Spst
194999000Sobrien	      /* We're done reading the symbol file; finish off complaints.  */
1950130809Smarcel	      clear_complaints (&symfile_complaints, 0, 1);
195119370Spst
195299000Sobrien	      /* Getting new symbols may change our opinion about what is
195399000Sobrien	         frameless.  */
195419370Spst
195599000Sobrien	      reinit_frame_cache ();
195619370Spst
195799000Sobrien	      /* Discard cleanups as symbol reading was successful.  */
195899000Sobrien	      discard_cleanups (old_cleanups);
195919370Spst
196099000Sobrien	      /* If the mtime has changed between the time we set new_modtime
196199000Sobrien	         and now, we *want* this to be out of date, so don't call stat
196299000Sobrien	         again now.  */
196399000Sobrien	      objfile->mtime = new_modtime;
196499000Sobrien	      reread_one = 1;
1965130809Smarcel              reread_separate_symbols (objfile);
196699000Sobrien	    }
196719370Spst	}
196819370Spst    }
196919370Spst
197019370Spst  if (reread_one)
197119370Spst    clear_symtab_users ();
197219370Spst}
1973130809Smarcel
1974130809Smarcel
1975130809Smarcel/* Handle separate debug info for OBJFILE, which has just been
1976130809Smarcel   re-read:
1977130809Smarcel   - If we had separate debug info before, but now we don't, get rid
1978130809Smarcel     of the separated objfile.
1979130809Smarcel   - If we didn't have separated debug info before, but now we do,
1980130809Smarcel     read in the new separated debug info file.
1981130809Smarcel   - If the debug link points to a different file, toss the old one
1982130809Smarcel     and read the new one.
1983130809Smarcel   This function does *not* handle the case where objfile is still
1984130809Smarcel   using the same separate debug info file, but that file's timestamp
1985130809Smarcel   has changed.  That case should be handled by the loop in
1986130809Smarcel   reread_symbols already.  */
1987130809Smarcelstatic void
1988130809Smarcelreread_separate_symbols (struct objfile *objfile)
1989130809Smarcel{
1990130809Smarcel  char *debug_file;
1991130809Smarcel  unsigned long crc32;
1992130809Smarcel
1993130809Smarcel  /* Does the updated objfile's debug info live in a
1994130809Smarcel     separate file?  */
1995130809Smarcel  debug_file = find_separate_debug_file (objfile);
1996130809Smarcel
1997130809Smarcel  if (objfile->separate_debug_objfile)
1998130809Smarcel    {
1999130809Smarcel      /* There are two cases where we need to get rid of
2000130809Smarcel         the old separated debug info objfile:
2001130809Smarcel         - if the new primary objfile doesn't have
2002130809Smarcel         separated debug info, or
2003130809Smarcel         - if the new primary objfile has separate debug
2004130809Smarcel         info, but it's under a different filename.
2005130809Smarcel
2006130809Smarcel         If the old and new objfiles both have separate
2007130809Smarcel         debug info, under the same filename, then we're
2008130809Smarcel         okay --- if the separated file's contents have
2009130809Smarcel         changed, we will have caught that when we
2010130809Smarcel         visited it in this function's outermost
2011130809Smarcel         loop.  */
2012130809Smarcel      if (! debug_file
2013130809Smarcel          || strcmp (debug_file, objfile->separate_debug_objfile->name) != 0)
2014130809Smarcel        free_objfile (objfile->separate_debug_objfile);
2015130809Smarcel    }
2016130809Smarcel
2017130809Smarcel  /* If the new objfile has separate debug info, and we
2018130809Smarcel     haven't loaded it already, do so now.  */
2019130809Smarcel  if (debug_file
2020130809Smarcel      && ! objfile->separate_debug_objfile)
2021130809Smarcel    {
2022130809Smarcel      /* Use the same section offset table as objfile itself.
2023130809Smarcel         Preserve the flags from objfile that make sense.  */
2024130809Smarcel      objfile->separate_debug_objfile
2025130809Smarcel        = (symbol_file_add_with_addrs_or_offsets
2026130809Smarcel           (debug_file,
2027130809Smarcel            info_verbose, /* from_tty: Don't override the default. */
2028130809Smarcel            0, /* No addr table.  */
2029130809Smarcel            objfile->section_offsets, objfile->num_sections,
2030130809Smarcel            0, /* Not mainline.  See comments about this above.  */
2031130809Smarcel            objfile->flags & (OBJF_REORDERED | OBJF_SHARED | OBJF_READNOW
2032130809Smarcel                              | OBJF_USERLOADED)));
2033130809Smarcel      objfile->separate_debug_objfile->separate_debug_objfile_backlink
2034130809Smarcel        = objfile;
2035130809Smarcel    }
2036130809Smarcel}
2037130809Smarcel
2038130809Smarcel
203919370Spst
204046289Sdfr
204199000Sobrien
204299000Sobrientypedef struct
204399000Sobrien{
204499000Sobrien  char *ext;
204546289Sdfr  enum language lang;
204699000Sobrien}
204799000Sobrienfilename_language;
204846289Sdfr
204999000Sobrienstatic filename_language *filename_language_table;
205046289Sdfrstatic int fl_table_size, fl_table_next;
205146289Sdfr
205246289Sdfrstatic void
205399000Sobrienadd_filename_language (char *ext, enum language lang)
205446289Sdfr{
205546289Sdfr  if (fl_table_next >= fl_table_size)
205646289Sdfr    {
205746289Sdfr      fl_table_size += 10;
205899000Sobrien      filename_language_table =
205999000Sobrien	xrealloc (filename_language_table,
206099000Sobrien		  fl_table_size * sizeof (*filename_language_table));
206146289Sdfr    }
206246289Sdfr
206399000Sobrien  filename_language_table[fl_table_next].ext = xstrdup (ext);
206446289Sdfr  filename_language_table[fl_table_next].lang = lang;
206546289Sdfr  fl_table_next++;
206646289Sdfr}
206746289Sdfr
206846289Sdfrstatic char *ext_args;
206946289Sdfr
207046289Sdfrstatic void
207199000Sobrienset_ext_lang_command (char *args, int from_tty)
207246289Sdfr{
207346289Sdfr  int i;
207446289Sdfr  char *cp = ext_args;
207546289Sdfr  enum language lang;
207646289Sdfr
207746289Sdfr  /* First arg is filename extension, starting with '.' */
207846289Sdfr  if (*cp != '.')
207946289Sdfr    error ("'%s': Filename extension must begin with '.'", ext_args);
208046289Sdfr
208146289Sdfr  /* Find end of first arg.  */
208299000Sobrien  while (*cp && !isspace (*cp))
208346289Sdfr    cp++;
208446289Sdfr
208546289Sdfr  if (*cp == '\0')
208646289Sdfr    error ("'%s': two arguments required -- filename extension and language",
208746289Sdfr	   ext_args);
208846289Sdfr
208946289Sdfr  /* Null-terminate first arg */
209099000Sobrien  *cp++ = '\0';
209146289Sdfr
209246289Sdfr  /* Find beginning of second arg, which should be a source language.  */
209346289Sdfr  while (*cp && isspace (*cp))
209446289Sdfr    cp++;
209546289Sdfr
209646289Sdfr  if (*cp == '\0')
209746289Sdfr    error ("'%s': two arguments required -- filename extension and language",
209846289Sdfr	   ext_args);
209946289Sdfr
210046289Sdfr  /* Lookup the language from among those we know.  */
210146289Sdfr  lang = language_enum (cp);
210246289Sdfr
210346289Sdfr  /* Now lookup the filename extension: do we already know it?  */
210446289Sdfr  for (i = 0; i < fl_table_next; i++)
210546289Sdfr    if (0 == strcmp (ext_args, filename_language_table[i].ext))
210646289Sdfr      break;
210746289Sdfr
210846289Sdfr  if (i >= fl_table_next)
210946289Sdfr    {
211046289Sdfr      /* new file extension */
211146289Sdfr      add_filename_language (ext_args, lang);
211246289Sdfr    }
211346289Sdfr  else
211446289Sdfr    {
211546289Sdfr      /* redefining a previously known filename extension */
211646289Sdfr
211746289Sdfr      /* if (from_tty) */
211846289Sdfr      /*   query ("Really make files of type %s '%s'?", */
211946289Sdfr      /*          ext_args, language_str (lang));           */
212046289Sdfr
212199000Sobrien      xfree (filename_language_table[i].ext);
212299000Sobrien      filename_language_table[i].ext = xstrdup (ext_args);
212346289Sdfr      filename_language_table[i].lang = lang;
212446289Sdfr    }
212546289Sdfr}
212646289Sdfr
212746289Sdfrstatic void
212899000Sobrieninfo_ext_lang_command (char *args, int from_tty)
212946289Sdfr{
213046289Sdfr  int i;
213146289Sdfr
213246289Sdfr  printf_filtered ("Filename extensions and the languages they represent:");
213346289Sdfr  printf_filtered ("\n\n");
213446289Sdfr  for (i = 0; i < fl_table_next; i++)
213599000Sobrien    printf_filtered ("\t%s\t- %s\n",
213699000Sobrien		     filename_language_table[i].ext,
213746289Sdfr		     language_str (filename_language_table[i].lang));
213846289Sdfr}
213946289Sdfr
214046289Sdfrstatic void
214199000Sobrieninit_filename_language_table (void)
214246289Sdfr{
214346289Sdfr  if (fl_table_size == 0)	/* protect against repetition */
214446289Sdfr    {
214546289Sdfr      fl_table_size = 20;
214646289Sdfr      fl_table_next = 0;
214799000Sobrien      filename_language_table =
214846289Sdfr	xmalloc (fl_table_size * sizeof (*filename_language_table));
214999000Sobrien      add_filename_language (".c", language_c);
215099000Sobrien      add_filename_language (".C", language_cplus);
215199000Sobrien      add_filename_language (".cc", language_cplus);
215299000Sobrien      add_filename_language (".cp", language_cplus);
215399000Sobrien      add_filename_language (".cpp", language_cplus);
215499000Sobrien      add_filename_language (".cxx", language_cplus);
215599000Sobrien      add_filename_language (".c++", language_cplus);
215699000Sobrien      add_filename_language (".java", language_java);
215746289Sdfr      add_filename_language (".class", language_java);
2158130809Smarcel      add_filename_language (".m", language_objc);
215999000Sobrien      add_filename_language (".f", language_fortran);
216099000Sobrien      add_filename_language (".F", language_fortran);
216199000Sobrien      add_filename_language (".s", language_asm);
216299000Sobrien      add_filename_language (".S", language_asm);
216399000Sobrien      add_filename_language (".pas", language_pascal);
216499000Sobrien      add_filename_language (".p", language_pascal);
216599000Sobrien      add_filename_language (".pp", language_pascal);
216646289Sdfr    }
216746289Sdfr}
216846289Sdfr
216919370Spstenum language
217099000Sobriendeduce_language_from_filename (char *filename)
217119370Spst{
217246289Sdfr  int i;
217346289Sdfr  char *cp;
217419370Spst
217546289Sdfr  if (filename != NULL)
217646289Sdfr    if ((cp = strrchr (filename, '.')) != NULL)
217746289Sdfr      for (i = 0; i < fl_table_next; i++)
217846289Sdfr	if (strcmp (cp, filename_language_table[i].ext) == 0)
217946289Sdfr	  return filename_language_table[i].lang;
218046289Sdfr
218146289Sdfr  return language_unknown;
218219370Spst}
218319370Spst
218419370Spst/* allocate_symtab:
218519370Spst
218619370Spst   Allocate and partly initialize a new symbol table.  Return a pointer
218719370Spst   to it.  error() if no space.
218819370Spst
218919370Spst   Caller must set these fields:
219099000Sobrien   LINETABLE(symtab)
219199000Sobrien   symtab->blockvector
219299000Sobrien   symtab->dirname
219399000Sobrien   symtab->free_code
219499000Sobrien   symtab->free_ptr
219599000Sobrien   possibly free_named_symtabs (symtab->filename);
219619370Spst */
219719370Spst
219819370Spststruct symtab *
219999000Sobrienallocate_symtab (char *filename, struct objfile *objfile)
220019370Spst{
2201130809Smarcel  struct symtab *symtab;
220219370Spst
220319370Spst  symtab = (struct symtab *)
2204130809Smarcel    obstack_alloc (&objfile->objfile_obstack, sizeof (struct symtab));
220519370Spst  memset (symtab, 0, sizeof (*symtab));
220699000Sobrien  symtab->filename = obsavestring (filename, strlen (filename),
2207130809Smarcel				   &objfile->objfile_obstack);
220899000Sobrien  symtab->fullname = NULL;
220999000Sobrien  symtab->language = deduce_language_from_filename (filename);
221099000Sobrien  symtab->debugformat = obsavestring ("unknown", 7,
2211130809Smarcel				      &objfile->objfile_obstack);
221219370Spst
221319370Spst  /* Hook it to the objfile it comes from */
221419370Spst
221599000Sobrien  symtab->objfile = objfile;
221699000Sobrien  symtab->next = objfile->symtabs;
221799000Sobrien  objfile->symtabs = symtab;
221819370Spst
221946289Sdfr  /* FIXME: This should go away.  It is only defined for the Z8000,
222046289Sdfr     and the Z8000 definition of this macro doesn't have anything to
222146289Sdfr     do with the now-nonexistent EXTRA_SYMTAB_INFO macro, it's just
222246289Sdfr     here for convenience.  */
222319370Spst#ifdef INIT_EXTRA_SYMTAB_INFO
222419370Spst  INIT_EXTRA_SYMTAB_INFO (symtab);
222519370Spst#endif
222619370Spst
222719370Spst  return (symtab);
222819370Spst}
222919370Spst
223019370Spststruct partial_symtab *
223199000Sobrienallocate_psymtab (char *filename, struct objfile *objfile)
223219370Spst{
223319370Spst  struct partial_symtab *psymtab;
223419370Spst
223599000Sobrien  if (objfile->free_psymtabs)
223619370Spst    {
223799000Sobrien      psymtab = objfile->free_psymtabs;
223899000Sobrien      objfile->free_psymtabs = psymtab->next;
223919370Spst    }
224019370Spst  else
224119370Spst    psymtab = (struct partial_symtab *)
2242130809Smarcel      obstack_alloc (&objfile->objfile_obstack,
224319370Spst		     sizeof (struct partial_symtab));
224419370Spst
224519370Spst  memset (psymtab, 0, sizeof (struct partial_symtab));
224699000Sobrien  psymtab->filename = obsavestring (filename, strlen (filename),
2247130809Smarcel				    &objfile->objfile_obstack);
224899000Sobrien  psymtab->symtab = NULL;
224919370Spst
225046289Sdfr  /* Prepend it to the psymtab list for the objfile it belongs to.
225146289Sdfr     Psymtabs are searched in most recent inserted -> least recent
225246289Sdfr     inserted order. */
225319370Spst
225499000Sobrien  psymtab->objfile = objfile;
225599000Sobrien  psymtab->next = objfile->psymtabs;
225699000Sobrien  objfile->psymtabs = psymtab;
225746289Sdfr#if 0
225846289Sdfr  {
225946289Sdfr    struct partial_symtab **prev_pst;
226099000Sobrien    psymtab->objfile = objfile;
226199000Sobrien    psymtab->next = NULL;
226299000Sobrien    prev_pst = &(objfile->psymtabs);
226346289Sdfr    while ((*prev_pst) != NULL)
226499000Sobrien      prev_pst = &((*prev_pst)->next);
226546289Sdfr    (*prev_pst) = psymtab;
226699000Sobrien  }
226746289Sdfr#endif
226899000Sobrien
226919370Spst  return (psymtab);
227019370Spst}
227119370Spst
227246289Sdfrvoid
227399000Sobriendiscard_psymtab (struct partial_symtab *pst)
227446289Sdfr{
227546289Sdfr  struct partial_symtab **prev_pst;
227646289Sdfr
227746289Sdfr  /* From dbxread.c:
227846289Sdfr     Empty psymtabs happen as a result of header files which don't
227946289Sdfr     have any symbols in them.  There can be a lot of them.  But this
228046289Sdfr     check is wrong, in that a psymtab with N_SLINE entries but
228146289Sdfr     nothing else is not empty, but we don't realize that.  Fixing
228246289Sdfr     that without slowing things down might be tricky.  */
228346289Sdfr
228446289Sdfr  /* First, snip it out of the psymtab chain */
228546289Sdfr
228646289Sdfr  prev_pst = &(pst->objfile->psymtabs);
228746289Sdfr  while ((*prev_pst) != pst)
228846289Sdfr    prev_pst = &((*prev_pst)->next);
228946289Sdfr  (*prev_pst) = pst->next;
229046289Sdfr
229146289Sdfr  /* Next, put it on a free list for recycling */
229246289Sdfr
229346289Sdfr  pst->next = pst->objfile->free_psymtabs;
229446289Sdfr  pst->objfile->free_psymtabs = pst;
229546289Sdfr}
229699000Sobrien
229746289Sdfr
229819370Spst/* Reset all data structures in gdb which may contain references to symbol
229946289Sdfr   table data.  */
230019370Spst
230119370Spstvoid
230299000Sobrienclear_symtab_users (void)
230319370Spst{
230419370Spst  /* Someday, we should do better than this, by only blowing away
230519370Spst     the things that really need to be blown.  */
230619370Spst  clear_value_history ();
230719370Spst  clear_displays ();
230819370Spst  clear_internalvars ();
230919370Spst  breakpoint_re_set ();
231019370Spst  set_default_breakpoint (0, 0, 0, 0);
2311130809Smarcel  clear_current_source_symtab_and_line ();
231219370Spst  clear_pc_function_cache ();
231399000Sobrien  if (target_new_objfile_hook)
231499000Sobrien    target_new_objfile_hook (NULL);
231519370Spst}
231619370Spst
231799000Sobrienstatic void
231899000Sobrienclear_symtab_users_cleanup (void *ignore)
231999000Sobrien{
232099000Sobrien  clear_symtab_users ();
232199000Sobrien}
232299000Sobrien
232319370Spst/* clear_symtab_users_once:
232419370Spst
232519370Spst   This function is run after symbol reading, or from a cleanup.
232619370Spst   If an old symbol table was obsoleted, the old symbol table
232719370Spst   has been blown away, but the other GDB data structures that may
232819370Spst   reference it have not yet been cleared or re-directed.  (The old
232919370Spst   symtab was zapped, and the cleanup queued, in free_named_symtab()
233019370Spst   below.)
233119370Spst
233219370Spst   This function can be queued N times as a cleanup, or called
233319370Spst   directly; it will do all the work the first time, and then will be a
233419370Spst   no-op until the next time it is queued.  This works by bumping a
233519370Spst   counter at queueing time.  Much later when the cleanup is run, or at
233619370Spst   the end of symbol processing (in case the cleanup is discarded), if
233719370Spst   the queued count is greater than the "done-count", we do the work
233819370Spst   and set the done-count to the queued count.  If the queued count is
233919370Spst   less than or equal to the done-count, we just ignore the call.  This
234019370Spst   is needed because reading a single .o file will often replace many
234119370Spst   symtabs (one per .h file, for example), and we don't want to reset
234219370Spst   the breakpoints N times in the user's face.
234319370Spst
234419370Spst   The reason we both queue a cleanup, and call it directly after symbol
234519370Spst   reading, is because the cleanup protects us in case of errors, but is
234619370Spst   discarded if symbol reading is successful.  */
234719370Spst
234819370Spst#if 0
234919370Spst/* FIXME:  As free_named_symtabs is currently a big noop this function
235019370Spst   is no longer needed.  */
235199000Sobrienstatic void clear_symtab_users_once (void);
235219370Spst
235319370Spststatic int clear_symtab_users_queued;
235419370Spststatic int clear_symtab_users_done;
235519370Spst
235619370Spststatic void
235799000Sobrienclear_symtab_users_once (void)
235819370Spst{
235919370Spst  /* Enforce once-per-`do_cleanups'-semantics */
236019370Spst  if (clear_symtab_users_queued <= clear_symtab_users_done)
236119370Spst    return;
236219370Spst  clear_symtab_users_done = clear_symtab_users_queued;
236319370Spst
236419370Spst  clear_symtab_users ();
236519370Spst}
236619370Spst#endif
236719370Spst
236819370Spst/* Delete the specified psymtab, and any others that reference it.  */
236919370Spst
237019370Spststatic void
237199000Sobriencashier_psymtab (struct partial_symtab *pst)
237219370Spst{
237319370Spst  struct partial_symtab *ps, *pprev = NULL;
237419370Spst  int i;
237519370Spst
237619370Spst  /* Find its previous psymtab in the chain */
237799000Sobrien  for (ps = pst->objfile->psymtabs; ps; ps = ps->next)
237899000Sobrien    {
237999000Sobrien      if (ps == pst)
238099000Sobrien	break;
238199000Sobrien      pprev = ps;
238299000Sobrien    }
238319370Spst
238499000Sobrien  if (ps)
238599000Sobrien    {
238699000Sobrien      /* Unhook it from the chain.  */
238799000Sobrien      if (ps == pst->objfile->psymtabs)
238899000Sobrien	pst->objfile->psymtabs = ps->next;
238999000Sobrien      else
239099000Sobrien	pprev->next = ps->next;
239119370Spst
239299000Sobrien      /* FIXME, we can't conveniently deallocate the entries in the
239399000Sobrien         partial_symbol lists (global_psymbols/static_psymbols) that
239499000Sobrien         this psymtab points to.  These just take up space until all
239599000Sobrien         the psymtabs are reclaimed.  Ditto the dependencies list and
2396130809Smarcel         filename, which are all in the objfile_obstack.  */
239719370Spst
239899000Sobrien      /* We need to cashier any psymtab that has this one as a dependency... */
239999000Sobrien    again:
240099000Sobrien      for (ps = pst->objfile->psymtabs; ps; ps = ps->next)
240199000Sobrien	{
240299000Sobrien	  for (i = 0; i < ps->number_of_dependencies; i++)
240399000Sobrien	    {
240499000Sobrien	      if (ps->dependencies[i] == pst)
240599000Sobrien		{
240699000Sobrien		  cashier_psymtab (ps);
240799000Sobrien		  goto again;	/* Must restart, chain has been munged. */
240899000Sobrien		}
240999000Sobrien	    }
241019370Spst	}
241119370Spst    }
241219370Spst}
241319370Spst
241419370Spst/* If a symtab or psymtab for filename NAME is found, free it along
241519370Spst   with any dependent breakpoints, displays, etc.
241619370Spst   Used when loading new versions of object modules with the "add-file"
241719370Spst   command.  This is only called on the top-level symtab or psymtab's name;
241819370Spst   it is not called for subsidiary files such as .h files.
241919370Spst
242019370Spst   Return value is 1 if we blew away the environment, 0 if not.
242199000Sobrien   FIXME.  The return value appears to never be used.
242219370Spst
242319370Spst   FIXME.  I think this is not the best way to do this.  We should
242419370Spst   work on being gentler to the environment while still cleaning up
242519370Spst   all stray pointers into the freed symtab.  */
242619370Spst
242719370Spstint
242899000Sobrienfree_named_symtabs (char *name)
242919370Spst{
243019370Spst#if 0
243119370Spst  /* FIXME:  With the new method of each objfile having it's own
243219370Spst     psymtab list, this function needs serious rethinking.  In particular,
243319370Spst     why was it ever necessary to toss psymtabs with specific compilation
243419370Spst     unit filenames, as opposed to all psymtabs from a particular symbol
243519370Spst     file?  -- fnf
243619370Spst     Well, the answer is that some systems permit reloading of particular
243719370Spst     compilation units.  We want to blow away any old info about these
243819370Spst     compilation units, regardless of which objfiles they arrived in. --gnu.  */
243919370Spst
2440130809Smarcel  struct symtab *s;
2441130809Smarcel  struct symtab *prev;
2442130809Smarcel  struct partial_symtab *ps;
244319370Spst  struct blockvector *bv;
244419370Spst  int blewit = 0;
244519370Spst
244619370Spst  /* We only wack things if the symbol-reload switch is set.  */
244719370Spst  if (!symbol_reloading)
244819370Spst    return 0;
244919370Spst
245019370Spst  /* Some symbol formats have trouble providing file names... */
245119370Spst  if (name == 0 || *name == '\0')
245219370Spst    return 0;
245319370Spst
245419370Spst  /* Look for a psymtab with the specified name.  */
245519370Spst
245619370Spstagain2:
245799000Sobrien  for (ps = partial_symtab_list; ps; ps = ps->next)
245899000Sobrien    {
2459130809Smarcel      if (strcmp (name, ps->filename) == 0)
246099000Sobrien	{
246199000Sobrien	  cashier_psymtab (ps);	/* Blow it away...and its little dog, too.  */
246299000Sobrien	  goto again2;		/* Must restart, chain has been munged */
246399000Sobrien	}
246419370Spst    }
246519370Spst
246619370Spst  /* Look for a symtab with the specified name.  */
246719370Spst
246819370Spst  for (s = symtab_list; s; s = s->next)
246919370Spst    {
2470130809Smarcel      if (strcmp (name, s->filename) == 0)
247119370Spst	break;
247219370Spst      prev = s;
247319370Spst    }
247419370Spst
247519370Spst  if (s)
247619370Spst    {
247719370Spst      if (s == symtab_list)
247819370Spst	symtab_list = s->next;
247919370Spst      else
248019370Spst	prev->next = s->next;
248119370Spst
248219370Spst      /* For now, queue a delete for all breakpoints, displays, etc., whether
248399000Sobrien         or not they depend on the symtab being freed.  This should be
248499000Sobrien         changed so that only those data structures affected are deleted.  */
248519370Spst
248619370Spst      /* But don't delete anything if the symtab is empty.
248799000Sobrien         This test is necessary due to a bug in "dbxread.c" that
248899000Sobrien         causes empty symtabs to be created for N_SO symbols that
248999000Sobrien         contain the pathname of the object file.  (This problem
249099000Sobrien         has been fixed in GDB 3.9x).  */
249119370Spst
249219370Spst      bv = BLOCKVECTOR (s);
249319370Spst      if (BLOCKVECTOR_NBLOCKS (bv) > 2
249419370Spst	  || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK))
249519370Spst	  || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK)))
249619370Spst	{
2497130809Smarcel	  complaint (&symfile_complaints, "Replacing old symbols for `%s'",
2498130809Smarcel		     name);
249919370Spst	  clear_symtab_users_queued++;
250019370Spst	  make_cleanup (clear_symtab_users_once, 0);
250119370Spst	  blewit = 1;
250299000Sobrien	}
250399000Sobrien      else
250499000Sobrien	{
2505130809Smarcel	  complaint (&symfile_complaints, "Empty symbol table found for `%s'",
2506130809Smarcel		     name);
250719370Spst	}
250819370Spst
250919370Spst      free_symtab (s);
251019370Spst    }
251119370Spst  else
251219370Spst    {
251319370Spst      /* It is still possible that some breakpoints will be affected
251499000Sobrien         even though no symtab was found, since the file might have
251599000Sobrien         been compiled without debugging, and hence not be associated
251699000Sobrien         with a symtab.  In order to handle this correctly, we would need
251799000Sobrien         to keep a list of text address ranges for undebuggable files.
251899000Sobrien         For now, we do nothing, since this is a fairly obscure case.  */
251919370Spst      ;
252019370Spst    }
252119370Spst
252219370Spst  /* FIXME, what about the minimal symbol table? */
252319370Spst  return blewit;
252419370Spst#else
252519370Spst  return (0);
252619370Spst#endif
252719370Spst}
252819370Spst
252919370Spst/* Allocate and partially fill a partial symtab.  It will be
253019370Spst   completely filled at the end of the symbol list.
253119370Spst
253299000Sobrien   FILENAME is the name of the symbol-file we are reading from. */
253319370Spst
253419370Spststruct partial_symtab *
253599000Sobrienstart_psymtab_common (struct objfile *objfile,
253699000Sobrien		      struct section_offsets *section_offsets, char *filename,
253799000Sobrien		      CORE_ADDR textlow, struct partial_symbol **global_syms,
253899000Sobrien		      struct partial_symbol **static_syms)
253919370Spst{
254019370Spst  struct partial_symtab *psymtab;
254119370Spst
254219370Spst  psymtab = allocate_psymtab (filename, objfile);
254399000Sobrien  psymtab->section_offsets = section_offsets;
254499000Sobrien  psymtab->textlow = textlow;
254599000Sobrien  psymtab->texthigh = psymtab->textlow;		/* default */
254699000Sobrien  psymtab->globals_offset = global_syms - objfile->global_psymbols.list;
254799000Sobrien  psymtab->statics_offset = static_syms - objfile->static_psymbols.list;
254819370Spst  return (psymtab);
254919370Spst}
255019370Spst
255119370Spst/* Add a symbol with a long value to a psymtab.
2552130809Smarcel   Since one arg is a struct, we pass in a ptr and deref it (sigh).
2553130809Smarcel   Return the partial symbol that has been added.  */
255419370Spst
2555130809Smarcel/* NOTE: carlton/2003-09-11: The reason why we return the partial
2556130809Smarcel   symbol is so that callers can get access to the symbol's demangled
2557130809Smarcel   name, which they don't have any cheap way to determine otherwise.
2558130809Smarcel   (Currenly, dwarf2read.c is the only file who uses that information,
2559130809Smarcel   though it's possible that other readers might in the future.)
2560130809Smarcel   Elena wasn't thrilled about that, and I don't blame her, but we
2561130809Smarcel   couldn't come up with a better way to get that information.  If
2562130809Smarcel   it's needed in other situations, we could consider breaking up
2563130809Smarcel   SYMBOL_SET_NAMES to provide access to the demangled name lookup
2564130809Smarcel   cache.  */
2565130809Smarcel
2566130809Smarcelconst struct partial_symbol *
2567130809Smarceladd_psymbol_to_list (char *name, int namelength, domain_enum domain,
256899000Sobrien		     enum address_class class,
256999000Sobrien		     struct psymbol_allocation_list *list, long val,	/* Value as a long */
257099000Sobrien		     CORE_ADDR coreaddr,	/* Value as a CORE_ADDR */
257199000Sobrien		     enum language language, struct objfile *objfile)
257219370Spst{
2573130809Smarcel  struct partial_symbol *psym;
257419370Spst  char *buf = alloca (namelength + 1);
257519370Spst  /* psymbol is static so that there will be no uninitialized gaps in the
257619370Spst     structure which might contain random data, causing cache misses in
257719370Spst     bcache. */
257819370Spst  static struct partial_symbol psymbol;
257919370Spst
258019370Spst  /* Create local copy of the partial symbol */
258119370Spst  memcpy (buf, name, namelength);
258219370Spst  buf[namelength] = '\0';
258319370Spst  /* val and coreaddr are mutually exclusive, one of them *will* be zero */
258419370Spst  if (val != 0)
258519370Spst    {
258619370Spst      SYMBOL_VALUE (&psymbol) = val;
258719370Spst    }
258819370Spst  else
258919370Spst    {
259019370Spst      SYMBOL_VALUE_ADDRESS (&psymbol) = coreaddr;
259119370Spst    }
259219370Spst  SYMBOL_SECTION (&psymbol) = 0;
259319370Spst  SYMBOL_LANGUAGE (&psymbol) = language;
2594130809Smarcel  PSYMBOL_DOMAIN (&psymbol) = domain;
259519370Spst  PSYMBOL_CLASS (&psymbol) = class;
259619370Spst
2597130809Smarcel  SYMBOL_SET_NAMES (&psymbol, buf, namelength, objfile);
2598130809Smarcel
259919370Spst  /* Stash the partial symbol away in the cache */
2600130809Smarcel  psym = deprecated_bcache (&psymbol, sizeof (struct partial_symbol),
2601130809Smarcel			    objfile->psymbol_cache);
260219370Spst
260319370Spst  /* Save pointer to partial symbol in psymtab, growing symtab if needed. */
260419370Spst  if (list->next >= list->list + list->size)
260519370Spst    {
260619370Spst      extend_psymbol_list (list, objfile);
260719370Spst    }
260819370Spst  *list->next++ = psym;
260919370Spst  OBJSTAT (objfile, n_psyms++);
2610130809Smarcel
2611130809Smarcel  return psym;
261219370Spst}
261319370Spst
261446289Sdfr/* Add a symbol with a long value to a psymtab. This differs from
261546289Sdfr * add_psymbol_to_list above in taking both a mangled and a demangled
261646289Sdfr * name. */
261746289Sdfr
261846289Sdfrvoid
261999000Sobrienadd_psymbol_with_dem_name_to_list (char *name, int namelength, char *dem_name,
2620130809Smarcel				   int dem_namelength, domain_enum domain,
262199000Sobrien				   enum address_class class,
262299000Sobrien				   struct psymbol_allocation_list *list, long val,	/* Value as a long */
262399000Sobrien				   CORE_ADDR coreaddr,	/* Value as a CORE_ADDR */
262499000Sobrien				   enum language language,
262599000Sobrien				   struct objfile *objfile)
262646289Sdfr{
2627130809Smarcel  struct partial_symbol *psym;
262846289Sdfr  char *buf = alloca (namelength + 1);
262946289Sdfr  /* psymbol is static so that there will be no uninitialized gaps in the
263046289Sdfr     structure which might contain random data, causing cache misses in
263146289Sdfr     bcache. */
263246289Sdfr  static struct partial_symbol psymbol;
263346289Sdfr
263446289Sdfr  /* Create local copy of the partial symbol */
263546289Sdfr
263646289Sdfr  memcpy (buf, name, namelength);
263746289Sdfr  buf[namelength] = '\0';
2638130809Smarcel  DEPRECATED_SYMBOL_NAME (&psymbol) = deprecated_bcache (buf, namelength + 1,
2639130809Smarcel							 objfile->psymbol_cache);
264046289Sdfr
264146289Sdfr  buf = alloca (dem_namelength + 1);
264246289Sdfr  memcpy (buf, dem_name, dem_namelength);
264346289Sdfr  buf[dem_namelength] = '\0';
264499000Sobrien
264546289Sdfr  switch (language)
264646289Sdfr    {
264799000Sobrien    case language_c:
264899000Sobrien    case language_cplus:
264999000Sobrien      SYMBOL_CPLUS_DEMANGLED_NAME (&psymbol) =
2650130809Smarcel	deprecated_bcache (buf, dem_namelength + 1, objfile->psymbol_cache);
265199000Sobrien      break;
265246289Sdfr      /* FIXME What should be done for the default case? Ignoring for now. */
265346289Sdfr    }
265446289Sdfr
265546289Sdfr  /* val and coreaddr are mutually exclusive, one of them *will* be zero */
265646289Sdfr  if (val != 0)
265746289Sdfr    {
265846289Sdfr      SYMBOL_VALUE (&psymbol) = val;
265946289Sdfr    }
266046289Sdfr  else
266146289Sdfr    {
266246289Sdfr      SYMBOL_VALUE_ADDRESS (&psymbol) = coreaddr;
266346289Sdfr    }
266446289Sdfr  SYMBOL_SECTION (&psymbol) = 0;
266546289Sdfr  SYMBOL_LANGUAGE (&psymbol) = language;
2666130809Smarcel  PSYMBOL_DOMAIN (&psymbol) = domain;
266746289Sdfr  PSYMBOL_CLASS (&psymbol) = class;
266846289Sdfr  SYMBOL_INIT_LANGUAGE_SPECIFIC (&psymbol, language);
266946289Sdfr
267046289Sdfr  /* Stash the partial symbol away in the cache */
2671130809Smarcel  psym = deprecated_bcache (&psymbol, sizeof (struct partial_symbol),
2672130809Smarcel			    objfile->psymbol_cache);
267346289Sdfr
267446289Sdfr  /* Save pointer to partial symbol in psymtab, growing symtab if needed. */
267546289Sdfr  if (list->next >= list->list + list->size)
267646289Sdfr    {
267746289Sdfr      extend_psymbol_list (list, objfile);
267846289Sdfr    }
267946289Sdfr  *list->next++ = psym;
268046289Sdfr  OBJSTAT (objfile, n_psyms++);
268146289Sdfr}
268246289Sdfr
268319370Spst/* Initialize storage for partial symbols.  */
268419370Spst
268519370Spstvoid
268699000Sobrieninit_psymbol_list (struct objfile *objfile, int total_symbols)
268719370Spst{
268819370Spst  /* Free any previously allocated psymbol lists.  */
268999000Sobrien
269099000Sobrien  if (objfile->global_psymbols.list)
269119370Spst    {
2692130809Smarcel      xmfree (objfile->md, objfile->global_psymbols.list);
269319370Spst    }
269499000Sobrien  if (objfile->static_psymbols.list)
269519370Spst    {
2696130809Smarcel      xmfree (objfile->md, objfile->static_psymbols.list);
269719370Spst    }
269899000Sobrien
269919370Spst  /* Current best guess is that approximately a twentieth
270019370Spst     of the total symbols (in a debugging file) are global or static
270119370Spst     oriented symbols */
270246289Sdfr
270399000Sobrien  objfile->global_psymbols.size = total_symbols / 10;
270499000Sobrien  objfile->static_psymbols.size = total_symbols / 10;
270599000Sobrien
270699000Sobrien  if (objfile->global_psymbols.size > 0)
270746289Sdfr    {
270899000Sobrien      objfile->global_psymbols.next =
270999000Sobrien	objfile->global_psymbols.list = (struct partial_symbol **)
271099000Sobrien	xmmalloc (objfile->md, (objfile->global_psymbols.size
271199000Sobrien				* sizeof (struct partial_symbol *)));
271246289Sdfr    }
271399000Sobrien  if (objfile->static_psymbols.size > 0)
271446289Sdfr    {
271599000Sobrien      objfile->static_psymbols.next =
271699000Sobrien	objfile->static_psymbols.list = (struct partial_symbol **)
271799000Sobrien	xmmalloc (objfile->md, (objfile->static_psymbols.size
271899000Sobrien				* sizeof (struct partial_symbol *)));
271946289Sdfr    }
272019370Spst}
272146289Sdfr
272246289Sdfr/* OVERLAYS:
272346289Sdfr   The following code implements an abstraction for debugging overlay sections.
272446289Sdfr
272546289Sdfr   The target model is as follows:
272646289Sdfr   1) The gnu linker will permit multiple sections to be mapped into the
272799000Sobrien   same VMA, each with its own unique LMA (or load address).
272846289Sdfr   2) It is assumed that some runtime mechanism exists for mapping the
272999000Sobrien   sections, one by one, from the load address into the VMA address.
273046289Sdfr   3) This code provides a mechanism for gdb to keep track of which
273199000Sobrien   sections should be considered to be mapped from the VMA to the LMA.
273299000Sobrien   This information is used for symbol lookup, and memory read/write.
273399000Sobrien   For instance, if a section has been mapped then its contents
273499000Sobrien   should be read from the VMA, otherwise from the LMA.
273546289Sdfr
273646289Sdfr   Two levels of debugger support for overlays are available.  One is
273746289Sdfr   "manual", in which the debugger relies on the user to tell it which
273846289Sdfr   overlays are currently mapped.  This level of support is
273946289Sdfr   implemented entirely in the core debugger, and the information about
274046289Sdfr   whether a section is mapped is kept in the objfile->obj_section table.
274146289Sdfr
274246289Sdfr   The second level of support is "automatic", and is only available if
274346289Sdfr   the target-specific code provides functionality to read the target's
274446289Sdfr   overlay mapping table, and translate its contents for the debugger
274546289Sdfr   (by updating the mapped state information in the obj_section tables).
274646289Sdfr
274746289Sdfr   The interface is as follows:
274899000Sobrien   User commands:
274999000Sobrien   overlay map <name>   -- tell gdb to consider this section mapped
275099000Sobrien   overlay unmap <name> -- tell gdb to consider this section unmapped
275199000Sobrien   overlay list         -- list the sections that GDB thinks are mapped
275299000Sobrien   overlay read-target  -- get the target's state of what's mapped
275399000Sobrien   overlay off/manual/auto -- set overlay debugging state
275499000Sobrien   Functional interface:
275599000Sobrien   find_pc_mapped_section(pc):    if the pc is in the range of a mapped
275699000Sobrien   section, return that section.
275799000Sobrien   find_pc_overlay(pc):       find any overlay section that contains
275899000Sobrien   the pc, either in its VMA or its LMA
275999000Sobrien   overlay_is_mapped(sect):       true if overlay is marked as mapped
276099000Sobrien   section_is_overlay(sect):      true if section's VMA != LMA
276199000Sobrien   pc_in_mapped_range(pc,sec):    true if pc belongs to section's VMA
276299000Sobrien   pc_in_unmapped_range(...):     true if pc belongs to section's LMA
276399000Sobrien   sections_overlap(sec1, sec2):  true if mapped sec1 and sec2 ranges overlap
276499000Sobrien   overlay_mapped_address(...):   map an address from section's LMA to VMA
276599000Sobrien   overlay_unmapped_address(...): map an address from section's VMA to LMA
276699000Sobrien   symbol_overlayed_address(...): Return a "current" address for symbol:
276799000Sobrien   either in VMA or LMA depending on whether
276899000Sobrien   the symbol's section is currently mapped
276946289Sdfr */
277046289Sdfr
277146289Sdfr/* Overlay debugging state: */
277246289Sdfr
277399000Sobrienenum overlay_debugging_state overlay_debugging = ovly_off;
277446289Sdfrint overlay_cache_invalid = 0;	/* True if need to refresh mapped state */
277546289Sdfr
277646289Sdfr/* Target vector for refreshing overlay mapped state */
277799000Sobrienstatic void simple_overlay_update (struct obj_section *);
277899000Sobrienvoid (*target_overlay_update) (struct obj_section *) = simple_overlay_update;
277946289Sdfr
278046289Sdfr/* Function: section_is_overlay (SECTION)
278146289Sdfr   Returns true if SECTION has VMA not equal to LMA, ie.
278246289Sdfr   SECTION is loaded at an address different from where it will "run".  */
278346289Sdfr
278446289Sdfrint
278599000Sobriensection_is_overlay (asection *section)
278646289Sdfr{
278799000Sobrien  /* FIXME: need bfd *, so we can use bfd_section_lma methods. */
278899000Sobrien
278946289Sdfr  if (overlay_debugging)
279046289Sdfr    if (section && section->lma != 0 &&
279146289Sdfr	section->vma != section->lma)
279246289Sdfr      return 1;
279346289Sdfr
279446289Sdfr  return 0;
279546289Sdfr}
279646289Sdfr
279746289Sdfr/* Function: overlay_invalidate_all (void)
279846289Sdfr   Invalidate the mapped state of all overlay sections (mark it as stale).  */
279946289Sdfr
280046289Sdfrstatic void
280199000Sobrienoverlay_invalidate_all (void)
280246289Sdfr{
280399000Sobrien  struct objfile *objfile;
280446289Sdfr  struct obj_section *sect;
280546289Sdfr
280646289Sdfr  ALL_OBJSECTIONS (objfile, sect)
280746289Sdfr    if (section_is_overlay (sect->the_bfd_section))
280899000Sobrien    sect->ovly_mapped = -1;
280946289Sdfr}
281046289Sdfr
281146289Sdfr/* Function: overlay_is_mapped (SECTION)
281246289Sdfr   Returns true if section is an overlay, and is currently mapped.
281346289Sdfr   Private: public access is thru function section_is_mapped.
281446289Sdfr
281546289Sdfr   Access to the ovly_mapped flag is restricted to this function, so
281646289Sdfr   that we can do automatic update.  If the global flag
281746289Sdfr   OVERLAY_CACHE_INVALID is set (by wait_for_inferior), then call
281846289Sdfr   overlay_invalidate_all.  If the mapped state of the particular
281946289Sdfr   section is stale, then call TARGET_OVERLAY_UPDATE to refresh it.  */
282046289Sdfr
282199000Sobrienstatic int
282299000Sobrienoverlay_is_mapped (struct obj_section *osect)
282346289Sdfr{
282446289Sdfr  if (osect == 0 || !section_is_overlay (osect->the_bfd_section))
282546289Sdfr    return 0;
282646289Sdfr
282799000Sobrien  switch (overlay_debugging)
282846289Sdfr    {
282946289Sdfr    default:
283099000Sobrien    case ovly_off:
283199000Sobrien      return 0;			/* overlay debugging off */
283299000Sobrien    case ovly_auto:		/* overlay debugging automatic */
283346289Sdfr      /* Unles there is a target_overlay_update function,
283499000Sobrien         there's really nothing useful to do here (can't really go auto)  */
283546289Sdfr      if (target_overlay_update)
283646289Sdfr	{
283746289Sdfr	  if (overlay_cache_invalid)
283846289Sdfr	    {
283946289Sdfr	      overlay_invalidate_all ();
284046289Sdfr	      overlay_cache_invalid = 0;
284146289Sdfr	    }
284246289Sdfr	  if (osect->ovly_mapped == -1)
284346289Sdfr	    (*target_overlay_update) (osect);
284446289Sdfr	}
284546289Sdfr      /* fall thru to manual case */
284699000Sobrien    case ovly_on:		/* overlay debugging manual */
284746289Sdfr      return osect->ovly_mapped == 1;
284846289Sdfr    }
284946289Sdfr}
285046289Sdfr
285146289Sdfr/* Function: section_is_mapped
285246289Sdfr   Returns true if section is an overlay, and is currently mapped.  */
285346289Sdfr
285446289Sdfrint
285599000Sobriensection_is_mapped (asection *section)
285646289Sdfr{
285799000Sobrien  struct objfile *objfile;
285846289Sdfr  struct obj_section *osect;
285946289Sdfr
286046289Sdfr  if (overlay_debugging)
286146289Sdfr    if (section && section_is_overlay (section))
286246289Sdfr      ALL_OBJSECTIONS (objfile, osect)
286346289Sdfr	if (osect->the_bfd_section == section)
286499000Sobrien	return overlay_is_mapped (osect);
286546289Sdfr
286646289Sdfr  return 0;
286746289Sdfr}
286846289Sdfr
286946289Sdfr/* Function: pc_in_unmapped_range
287046289Sdfr   If PC falls into the lma range of SECTION, return true, else false.  */
287146289Sdfr
287246289SdfrCORE_ADDR
287399000Sobrienpc_in_unmapped_range (CORE_ADDR pc, asection *section)
287446289Sdfr{
287599000Sobrien  /* FIXME: need bfd *, so we can use bfd_section_lma methods. */
287699000Sobrien
287746289Sdfr  int size;
287846289Sdfr
287946289Sdfr  if (overlay_debugging)
288046289Sdfr    if (section && section_is_overlay (section))
288146289Sdfr      {
2882218822Sdim	size = bfd_get_section_size (section);
288346289Sdfr	if (section->lma <= pc && pc < section->lma + size)
288446289Sdfr	  return 1;
288546289Sdfr      }
288646289Sdfr  return 0;
288746289Sdfr}
288846289Sdfr
288946289Sdfr/* Function: pc_in_mapped_range
289046289Sdfr   If PC falls into the vma range of SECTION, return true, else false.  */
289146289Sdfr
289246289SdfrCORE_ADDR
289399000Sobrienpc_in_mapped_range (CORE_ADDR pc, asection *section)
289446289Sdfr{
289599000Sobrien  /* FIXME: need bfd *, so we can use bfd_section_vma methods. */
289699000Sobrien
289746289Sdfr  int size;
289846289Sdfr
289946289Sdfr  if (overlay_debugging)
290046289Sdfr    if (section && section_is_overlay (section))
290146289Sdfr      {
2902218822Sdim	size = bfd_get_section_size (section);
290346289Sdfr	if (section->vma <= pc && pc < section->vma + size)
290446289Sdfr	  return 1;
290546289Sdfr      }
290646289Sdfr  return 0;
290746289Sdfr}
290846289Sdfr
290999000Sobrien
291099000Sobrien/* Return true if the mapped ranges of sections A and B overlap, false
291199000Sobrien   otherwise.  */
2912130809Smarcelstatic int
291399000Sobriensections_overlap (asection *a, asection *b)
291499000Sobrien{
291599000Sobrien  /* FIXME: need bfd *, so we can use bfd_section_vma methods. */
291699000Sobrien
291799000Sobrien  CORE_ADDR a_start = a->vma;
2918218822Sdim  CORE_ADDR a_end = a->vma + bfd_get_section_size (a);
291999000Sobrien  CORE_ADDR b_start = b->vma;
2920218822Sdim  CORE_ADDR b_end = b->vma + bfd_get_section_size (b);
292199000Sobrien
292299000Sobrien  return (a_start < b_end && b_start < a_end);
292399000Sobrien}
292499000Sobrien
292546289Sdfr/* Function: overlay_unmapped_address (PC, SECTION)
292646289Sdfr   Returns the address corresponding to PC in the unmapped (load) range.
292746289Sdfr   May be the same as PC.  */
292846289Sdfr
292946289SdfrCORE_ADDR
293099000Sobrienoverlay_unmapped_address (CORE_ADDR pc, asection *section)
293146289Sdfr{
293299000Sobrien  /* FIXME: need bfd *, so we can use bfd_section_lma methods. */
293399000Sobrien
293446289Sdfr  if (overlay_debugging)
293546289Sdfr    if (section && section_is_overlay (section) &&
293646289Sdfr	pc_in_mapped_range (pc, section))
293746289Sdfr      return pc + section->lma - section->vma;
293846289Sdfr
293946289Sdfr  return pc;
294046289Sdfr}
294146289Sdfr
294246289Sdfr/* Function: overlay_mapped_address (PC, SECTION)
294346289Sdfr   Returns the address corresponding to PC in the mapped (runtime) range.
294446289Sdfr   May be the same as PC.  */
294546289Sdfr
294646289SdfrCORE_ADDR
294799000Sobrienoverlay_mapped_address (CORE_ADDR pc, asection *section)
294846289Sdfr{
294999000Sobrien  /* FIXME: need bfd *, so we can use bfd_section_vma methods. */
295099000Sobrien
295146289Sdfr  if (overlay_debugging)
295246289Sdfr    if (section && section_is_overlay (section) &&
295346289Sdfr	pc_in_unmapped_range (pc, section))
295446289Sdfr      return pc + section->vma - section->lma;
295546289Sdfr
295646289Sdfr  return pc;
295746289Sdfr}
295846289Sdfr
295946289Sdfr
296046289Sdfr/* Function: symbol_overlayed_address
296146289Sdfr   Return one of two addresses (relative to the VMA or to the LMA),
296246289Sdfr   depending on whether the section is mapped or not.  */
296346289Sdfr
296499000SobrienCORE_ADDR
296599000Sobriensymbol_overlayed_address (CORE_ADDR address, asection *section)
296646289Sdfr{
296746289Sdfr  if (overlay_debugging)
296846289Sdfr    {
296946289Sdfr      /* If the symbol has no section, just return its regular address. */
297046289Sdfr      if (section == 0)
297146289Sdfr	return address;
297246289Sdfr      /* If the symbol's section is not an overlay, just return its address */
297346289Sdfr      if (!section_is_overlay (section))
297446289Sdfr	return address;
297546289Sdfr      /* If the symbol's section is mapped, just return its address */
297646289Sdfr      if (section_is_mapped (section))
297746289Sdfr	return address;
297846289Sdfr      /*
297946289Sdfr       * HOWEVER: if the symbol is in an overlay section which is NOT mapped,
298046289Sdfr       * then return its LOADED address rather than its vma address!!
298146289Sdfr       */
298246289Sdfr      return overlay_unmapped_address (address, section);
298346289Sdfr    }
298446289Sdfr  return address;
298546289Sdfr}
298646289Sdfr
298746289Sdfr/* Function: find_pc_overlay (PC)
298846289Sdfr   Return the best-match overlay section for PC:
298946289Sdfr   If PC matches a mapped overlay section's VMA, return that section.
299046289Sdfr   Else if PC matches an unmapped section's VMA, return that section.
299146289Sdfr   Else if PC matches an unmapped section's LMA, return that section.  */
299246289Sdfr
299346289Sdfrasection *
299499000Sobrienfind_pc_overlay (CORE_ADDR pc)
299546289Sdfr{
299699000Sobrien  struct objfile *objfile;
299746289Sdfr  struct obj_section *osect, *best_match = NULL;
299846289Sdfr
299946289Sdfr  if (overlay_debugging)
300046289Sdfr    ALL_OBJSECTIONS (objfile, osect)
300146289Sdfr      if (section_is_overlay (osect->the_bfd_section))
300299000Sobrien      {
300399000Sobrien	if (pc_in_mapped_range (pc, osect->the_bfd_section))
300499000Sobrien	  {
300599000Sobrien	    if (overlay_is_mapped (osect))
300699000Sobrien	      return osect->the_bfd_section;
300799000Sobrien	    else
300899000Sobrien	      best_match = osect;
300999000Sobrien	  }
301099000Sobrien	else if (pc_in_unmapped_range (pc, osect->the_bfd_section))
301199000Sobrien	  best_match = osect;
301299000Sobrien      }
301346289Sdfr  return best_match ? best_match->the_bfd_section : NULL;
301446289Sdfr}
301546289Sdfr
301646289Sdfr/* Function: find_pc_mapped_section (PC)
301746289Sdfr   If PC falls into the VMA address range of an overlay section that is
301846289Sdfr   currently marked as MAPPED, return that section.  Else return NULL.  */
301946289Sdfr
302046289Sdfrasection *
302199000Sobrienfind_pc_mapped_section (CORE_ADDR pc)
302246289Sdfr{
302399000Sobrien  struct objfile *objfile;
302446289Sdfr  struct obj_section *osect;
302546289Sdfr
302646289Sdfr  if (overlay_debugging)
302746289Sdfr    ALL_OBJSECTIONS (objfile, osect)
302846289Sdfr      if (pc_in_mapped_range (pc, osect->the_bfd_section) &&
302946289Sdfr	  overlay_is_mapped (osect))
303099000Sobrien      return osect->the_bfd_section;
303146289Sdfr
303246289Sdfr  return NULL;
303346289Sdfr}
303446289Sdfr
303546289Sdfr/* Function: list_overlays_command
303646289Sdfr   Print a list of mapped sections and their PC ranges */
303746289Sdfr
303819370Spstvoid
303999000Sobrienlist_overlays_command (char *args, int from_tty)
304046289Sdfr{
304199000Sobrien  int nmapped = 0;
304299000Sobrien  struct objfile *objfile;
304346289Sdfr  struct obj_section *osect;
304446289Sdfr
304546289Sdfr  if (overlay_debugging)
304646289Sdfr    ALL_OBJSECTIONS (objfile, osect)
304746289Sdfr      if (overlay_is_mapped (osect))
304899000Sobrien      {
304999000Sobrien	const char *name;
305099000Sobrien	bfd_vma lma, vma;
305199000Sobrien	int size;
305246289Sdfr
305399000Sobrien	vma = bfd_section_vma (objfile->obfd, osect->the_bfd_section);
305499000Sobrien	lma = bfd_section_lma (objfile->obfd, osect->the_bfd_section);
3055218822Sdim	size = bfd_get_section_size (osect->the_bfd_section);
305699000Sobrien	name = bfd_section_name (objfile->obfd, osect->the_bfd_section);
305746289Sdfr
305899000Sobrien	printf_filtered ("Section %s, loaded at ", name);
305999000Sobrien	print_address_numeric (lma, 1, gdb_stdout);
306099000Sobrien	puts_filtered (" - ");
306199000Sobrien	print_address_numeric (lma + size, 1, gdb_stdout);
306299000Sobrien	printf_filtered (", mapped at ");
306399000Sobrien	print_address_numeric (vma, 1, gdb_stdout);
306499000Sobrien	puts_filtered (" - ");
306599000Sobrien	print_address_numeric (vma + size, 1, gdb_stdout);
306699000Sobrien	puts_filtered ("\n");
306746289Sdfr
306899000Sobrien	nmapped++;
306999000Sobrien      }
307046289Sdfr  if (nmapped == 0)
307146289Sdfr    printf_filtered ("No sections are mapped.\n");
307246289Sdfr}
307346289Sdfr
307446289Sdfr/* Function: map_overlay_command
307546289Sdfr   Mark the named section as mapped (ie. residing at its VMA address).  */
307646289Sdfr
307746289Sdfrvoid
307899000Sobrienmap_overlay_command (char *args, int from_tty)
307946289Sdfr{
308099000Sobrien  struct objfile *objfile, *objfile2;
308199000Sobrien  struct obj_section *sec, *sec2;
308299000Sobrien  asection *bfdsec;
308346289Sdfr
308446289Sdfr  if (!overlay_debugging)
308599000Sobrien    error ("\
308699000SobrienOverlay debugging not enabled.  Use either the 'overlay auto' or\n\
308799000Sobrienthe 'overlay manual' command.");
308846289Sdfr
308946289Sdfr  if (args == 0 || *args == 0)
309046289Sdfr    error ("Argument required: name of an overlay section");
309146289Sdfr
309246289Sdfr  /* First, find a section matching the user supplied argument */
309346289Sdfr  ALL_OBJSECTIONS (objfile, sec)
309446289Sdfr    if (!strcmp (bfd_section_name (objfile->obfd, sec->the_bfd_section), args))
309599000Sobrien    {
309699000Sobrien      /* Now, check to see if the section is an overlay. */
309799000Sobrien      bfdsec = sec->the_bfd_section;
309899000Sobrien      if (!section_is_overlay (bfdsec))
309999000Sobrien	continue;		/* not an overlay section */
310046289Sdfr
310199000Sobrien      /* Mark the overlay as "mapped" */
310299000Sobrien      sec->ovly_mapped = 1;
310346289Sdfr
310499000Sobrien      /* Next, make a pass and unmap any sections that are
310599000Sobrien         overlapped by this new section: */
310699000Sobrien      ALL_OBJSECTIONS (objfile2, sec2)
310799000Sobrien	if (sec2->ovly_mapped
310899000Sobrien            && sec != sec2
310999000Sobrien            && sec->the_bfd_section != sec2->the_bfd_section
311099000Sobrien            && sections_overlap (sec->the_bfd_section,
311199000Sobrien                                 sec2->the_bfd_section))
311299000Sobrien	{
311399000Sobrien	  if (info_verbose)
3114130809Smarcel	    printf_unfiltered ("Note: section %s unmapped by overlap\n",
311599000Sobrien			     bfd_section_name (objfile->obfd,
311699000Sobrien					       sec2->the_bfd_section));
311799000Sobrien	  sec2->ovly_mapped = 0;	/* sec2 overlaps sec: unmap sec2 */
311899000Sobrien	}
311999000Sobrien      return;
312099000Sobrien    }
312146289Sdfr  error ("No overlay section called %s", args);
312246289Sdfr}
312346289Sdfr
312446289Sdfr/* Function: unmap_overlay_command
312546289Sdfr   Mark the overlay section as unmapped
312646289Sdfr   (ie. resident in its LMA address range, rather than the VMA range).  */
312746289Sdfr
312846289Sdfrvoid
312999000Sobrienunmap_overlay_command (char *args, int from_tty)
313046289Sdfr{
313199000Sobrien  struct objfile *objfile;
313246289Sdfr  struct obj_section *sec;
313346289Sdfr
313446289Sdfr  if (!overlay_debugging)
313599000Sobrien    error ("\
313699000SobrienOverlay debugging not enabled.  Use either the 'overlay auto' or\n\
313799000Sobrienthe 'overlay manual' command.");
313846289Sdfr
313946289Sdfr  if (args == 0 || *args == 0)
314046289Sdfr    error ("Argument required: name of an overlay section");
314146289Sdfr
314246289Sdfr  /* First, find a section matching the user supplied argument */
314346289Sdfr  ALL_OBJSECTIONS (objfile, sec)
314446289Sdfr    if (!strcmp (bfd_section_name (objfile->obfd, sec->the_bfd_section), args))
314599000Sobrien    {
314699000Sobrien      if (!sec->ovly_mapped)
314799000Sobrien	error ("Section %s is not mapped", args);
314899000Sobrien      sec->ovly_mapped = 0;
314999000Sobrien      return;
315099000Sobrien    }
315146289Sdfr  error ("No overlay section called %s", args);
315246289Sdfr}
315346289Sdfr
315446289Sdfr/* Function: overlay_auto_command
315546289Sdfr   A utility command to turn on overlay debugging.
315646289Sdfr   Possibly this should be done via a set/show command. */
315746289Sdfr
315846289Sdfrstatic void
315999000Sobrienoverlay_auto_command (char *args, int from_tty)
316046289Sdfr{
316199000Sobrien  overlay_debugging = ovly_auto;
316299000Sobrien  enable_overlay_breakpoints ();
316346289Sdfr  if (info_verbose)
3164130809Smarcel    printf_unfiltered ("Automatic overlay debugging enabled.");
316546289Sdfr}
316646289Sdfr
316746289Sdfr/* Function: overlay_manual_command
316846289Sdfr   A utility command to turn on overlay debugging.
316946289Sdfr   Possibly this should be done via a set/show command. */
317046289Sdfr
317146289Sdfrstatic void
317299000Sobrienoverlay_manual_command (char *args, int from_tty)
317346289Sdfr{
317499000Sobrien  overlay_debugging = ovly_on;
317599000Sobrien  disable_overlay_breakpoints ();
317646289Sdfr  if (info_verbose)
3177130809Smarcel    printf_unfiltered ("Overlay debugging enabled.");
317846289Sdfr}
317946289Sdfr
318046289Sdfr/* Function: overlay_off_command
318146289Sdfr   A utility command to turn on overlay debugging.
318246289Sdfr   Possibly this should be done via a set/show command. */
318346289Sdfr
318446289Sdfrstatic void
318599000Sobrienoverlay_off_command (char *args, int from_tty)
318646289Sdfr{
318799000Sobrien  overlay_debugging = ovly_off;
318899000Sobrien  disable_overlay_breakpoints ();
318946289Sdfr  if (info_verbose)
3190130809Smarcel    printf_unfiltered ("Overlay debugging disabled.");
319146289Sdfr}
319246289Sdfr
319346289Sdfrstatic void
319499000Sobrienoverlay_load_command (char *args, int from_tty)
319546289Sdfr{
319646289Sdfr  if (target_overlay_update)
319746289Sdfr    (*target_overlay_update) (NULL);
319846289Sdfr  else
319946289Sdfr    error ("This target does not know how to read its overlay state.");
320046289Sdfr}
320146289Sdfr
320246289Sdfr/* Function: overlay_command
320346289Sdfr   A place-holder for a mis-typed command */
320446289Sdfr
320546289Sdfr/* Command list chain containing all defined "overlay" subcommands. */
320646289Sdfrstruct cmd_list_element *overlaylist;
320746289Sdfr
320846289Sdfrstatic void
320999000Sobrienoverlay_command (char *args, int from_tty)
321046289Sdfr{
321199000Sobrien  printf_unfiltered
321246289Sdfr    ("\"overlay\" must be followed by the name of an overlay command.\n");
321346289Sdfr  help_list (overlaylist, "overlay ", -1, gdb_stdout);
321446289Sdfr}
321546289Sdfr
321646289Sdfr
321746289Sdfr/* Target Overlays for the "Simplest" overlay manager:
321846289Sdfr
321946289Sdfr   This is GDB's default target overlay layer.  It works with the
322046289Sdfr   minimal overlay manager supplied as an example by Cygnus.  The
322146289Sdfr   entry point is via a function pointer "target_overlay_update",
322246289Sdfr   so targets that use a different runtime overlay manager can
322346289Sdfr   substitute their own overlay_update function and take over the
322446289Sdfr   function pointer.
322546289Sdfr
322646289Sdfr   The overlay_update function pokes around in the target's data structures
322746289Sdfr   to see what overlays are mapped, and updates GDB's overlay mapping with
322846289Sdfr   this information.
322946289Sdfr
323046289Sdfr   In this simple implementation, the target data structures are as follows:
323199000Sobrien   unsigned _novlys;            /# number of overlay sections #/
323299000Sobrien   unsigned _ovly_table[_novlys][4] = {
323399000Sobrien   {VMA, SIZE, LMA, MAPPED},    /# one entry per overlay section #/
323499000Sobrien   {..., ...,  ..., ...},
323599000Sobrien   }
323699000Sobrien   unsigned _novly_regions;     /# number of overlay regions #/
323799000Sobrien   unsigned _ovly_region_table[_novly_regions][3] = {
323899000Sobrien   {VMA, SIZE, MAPPED_TO_LMA},  /# one entry per overlay region #/
323999000Sobrien   {..., ...,  ...},
324099000Sobrien   }
324146289Sdfr   These functions will attempt to update GDB's mappedness state in the
324246289Sdfr   symbol section table, based on the target's mappedness state.
324346289Sdfr
324446289Sdfr   To do this, we keep a cached copy of the target's _ovly_table, and
324546289Sdfr   attempt to detect when the cached copy is invalidated.  The main
324646289Sdfr   entry point is "simple_overlay_update(SECT), which looks up SECT in
324746289Sdfr   the cached table and re-reads only the entry for that section from
324846289Sdfr   the target (whenever possible).
324946289Sdfr */
325046289Sdfr
325146289Sdfr/* Cached, dynamically allocated copies of the target data structures: */
325299000Sobrienstatic unsigned (*cache_ovly_table)[4] = 0;
325346289Sdfr#if 0
325499000Sobrienstatic unsigned (*cache_ovly_region_table)[3] = 0;
325546289Sdfr#endif
325699000Sobrienstatic unsigned cache_novlys = 0;
325746289Sdfr#if 0
325899000Sobrienstatic unsigned cache_novly_regions = 0;
325946289Sdfr#endif
326046289Sdfrstatic CORE_ADDR cache_ovly_table_base = 0;
326146289Sdfr#if 0
326246289Sdfrstatic CORE_ADDR cache_ovly_region_table_base = 0;
326346289Sdfr#endif
326499000Sobrienenum ovly_index
326599000Sobrien  {
326699000Sobrien    VMA, SIZE, LMA, MAPPED
326799000Sobrien  };
326846289Sdfr#define TARGET_LONG_BYTES (TARGET_LONG_BIT / TARGET_CHAR_BIT)
326946289Sdfr
327046289Sdfr/* Throw away the cached copy of _ovly_table */
327146289Sdfrstatic void
327299000Sobriensimple_free_overlay_table (void)
327346289Sdfr{
327446289Sdfr  if (cache_ovly_table)
327599000Sobrien    xfree (cache_ovly_table);
327699000Sobrien  cache_novlys = 0;
327746289Sdfr  cache_ovly_table = NULL;
327846289Sdfr  cache_ovly_table_base = 0;
327946289Sdfr}
328046289Sdfr
328146289Sdfr#if 0
328246289Sdfr/* Throw away the cached copy of _ovly_region_table */
328346289Sdfrstatic void
328499000Sobriensimple_free_overlay_region_table (void)
328546289Sdfr{
328646289Sdfr  if (cache_ovly_region_table)
328799000Sobrien    xfree (cache_ovly_region_table);
328899000Sobrien  cache_novly_regions = 0;
328946289Sdfr  cache_ovly_region_table = NULL;
329046289Sdfr  cache_ovly_region_table_base = 0;
329146289Sdfr}
329246289Sdfr#endif
329346289Sdfr
329446289Sdfr/* Read an array of ints from the target into a local buffer.
329546289Sdfr   Convert to host order.  int LEN is number of ints  */
329646289Sdfrstatic void
329799000Sobrienread_target_long_array (CORE_ADDR memaddr, unsigned int *myaddr, int len)
329846289Sdfr{
329999000Sobrien  /* FIXME (alloca): Not safe if array is very large. */
330046289Sdfr  char *buf = alloca (len * TARGET_LONG_BYTES);
330199000Sobrien  int i;
330246289Sdfr
330346289Sdfr  read_memory (memaddr, buf, len * TARGET_LONG_BYTES);
330446289Sdfr  for (i = 0; i < len; i++)
330599000Sobrien    myaddr[i] = extract_unsigned_integer (TARGET_LONG_BYTES * i + buf,
330646289Sdfr					  TARGET_LONG_BYTES);
330746289Sdfr}
330846289Sdfr
330946289Sdfr/* Find and grab a copy of the target _ovly_table
331046289Sdfr   (and _novlys, which is needed for the table's size) */
331199000Sobrienstatic int
331299000Sobriensimple_read_overlay_table (void)
331346289Sdfr{
331499000Sobrien  struct minimal_symbol *novlys_msym, *ovly_table_msym;
331546289Sdfr
331646289Sdfr  simple_free_overlay_table ();
331799000Sobrien  novlys_msym = lookup_minimal_symbol ("_novlys", NULL, NULL);
331899000Sobrien  if (! novlys_msym)
331946289Sdfr    {
332099000Sobrien      error ("Error reading inferior's overlay table: "
332199000Sobrien             "couldn't find `_novlys' variable\n"
332299000Sobrien             "in inferior.  Use `overlay manual' mode.");
332399000Sobrien      return 0;
332446289Sdfr    }
332599000Sobrien
332699000Sobrien  ovly_table_msym = lookup_minimal_symbol ("_ovly_table", NULL, NULL);
332799000Sobrien  if (! ovly_table_msym)
332899000Sobrien    {
332999000Sobrien      error ("Error reading inferior's overlay table: couldn't find "
333099000Sobrien             "`_ovly_table' array\n"
333199000Sobrien             "in inferior.  Use `overlay manual' mode.");
333299000Sobrien      return 0;
333399000Sobrien    }
333499000Sobrien
333599000Sobrien  cache_novlys = read_memory_integer (SYMBOL_VALUE_ADDRESS (novlys_msym), 4);
333699000Sobrien  cache_ovly_table
333799000Sobrien    = (void *) xmalloc (cache_novlys * sizeof (*cache_ovly_table));
333899000Sobrien  cache_ovly_table_base = SYMBOL_VALUE_ADDRESS (ovly_table_msym);
333999000Sobrien  read_target_long_array (cache_ovly_table_base,
334099000Sobrien                          (int *) cache_ovly_table,
334199000Sobrien                          cache_novlys * 4);
334299000Sobrien
334399000Sobrien  return 1;			/* SUCCESS */
334446289Sdfr}
334546289Sdfr
334646289Sdfr#if 0
334746289Sdfr/* Find and grab a copy of the target _ovly_region_table
334846289Sdfr   (and _novly_regions, which is needed for the table's size) */
334999000Sobrienstatic int
335099000Sobriensimple_read_overlay_region_table (void)
335146289Sdfr{
335246289Sdfr  struct minimal_symbol *msym;
335346289Sdfr
335446289Sdfr  simple_free_overlay_region_table ();
335599000Sobrien  msym = lookup_minimal_symbol ("_novly_regions", NULL, NULL);
335646289Sdfr  if (msym != NULL)
335746289Sdfr    cache_novly_regions = read_memory_integer (SYMBOL_VALUE_ADDRESS (msym), 4);
335899000Sobrien  else
335999000Sobrien    return 0;			/* failure */
336046289Sdfr  cache_ovly_region_table = (void *) xmalloc (cache_novly_regions * 12);
336146289Sdfr  if (cache_ovly_region_table != NULL)
336246289Sdfr    {
336399000Sobrien      msym = lookup_minimal_symbol ("_ovly_region_table", NULL, NULL);
336446289Sdfr      if (msym != NULL)
336546289Sdfr	{
336646289Sdfr	  cache_ovly_region_table_base = SYMBOL_VALUE_ADDRESS (msym);
336799000Sobrien	  read_target_long_array (cache_ovly_region_table_base,
336899000Sobrien				  (int *) cache_ovly_region_table,
336946289Sdfr				  cache_novly_regions * 3);
337046289Sdfr	}
337199000Sobrien      else
337299000Sobrien	return 0;		/* failure */
337346289Sdfr    }
337499000Sobrien  else
337599000Sobrien    return 0;			/* failure */
337699000Sobrien  return 1;			/* SUCCESS */
337746289Sdfr}
337846289Sdfr#endif
337946289Sdfr
338046289Sdfr/* Function: simple_overlay_update_1
338146289Sdfr   A helper function for simple_overlay_update.  Assuming a cached copy
338246289Sdfr   of _ovly_table exists, look through it to find an entry whose vma,
338346289Sdfr   lma and size match those of OSECT.  Re-read the entry and make sure
338446289Sdfr   it still matches OSECT (else the table may no longer be valid).
338546289Sdfr   Set OSECT's mapped state to match the entry.  Return: 1 for
338646289Sdfr   success, 0 for failure.  */
338746289Sdfr
338846289Sdfrstatic int
338999000Sobriensimple_overlay_update_1 (struct obj_section *osect)
339046289Sdfr{
339146289Sdfr  int i, size;
339299000Sobrien  bfd *obfd = osect->objfile->obfd;
339399000Sobrien  asection *bsect = osect->the_bfd_section;
339446289Sdfr
3395218822Sdim  size = bfd_get_section_size (osect->the_bfd_section);
339646289Sdfr  for (i = 0; i < cache_novlys; i++)
339799000Sobrien    if (cache_ovly_table[i][VMA] == bfd_section_vma (obfd, bsect)
339899000Sobrien	&& cache_ovly_table[i][LMA] == bfd_section_lma (obfd, bsect)
339999000Sobrien	/* && cache_ovly_table[i][SIZE] == size */ )
340046289Sdfr      {
340146289Sdfr	read_target_long_array (cache_ovly_table_base + i * TARGET_LONG_BYTES,
340246289Sdfr				(int *) cache_ovly_table[i], 4);
340399000Sobrien	if (cache_ovly_table[i][VMA] == bfd_section_vma (obfd, bsect)
340499000Sobrien	    && cache_ovly_table[i][LMA] == bfd_section_lma (obfd, bsect)
340599000Sobrien	    /* && cache_ovly_table[i][SIZE] == size */ )
340646289Sdfr	  {
340746289Sdfr	    osect->ovly_mapped = cache_ovly_table[i][MAPPED];
340846289Sdfr	    return 1;
340946289Sdfr	  }
341046289Sdfr	else	/* Warning!  Warning!  Target's ovly table has changed! */
341146289Sdfr	  return 0;
341246289Sdfr      }
341346289Sdfr  return 0;
341446289Sdfr}
341546289Sdfr
341646289Sdfr/* Function: simple_overlay_update
341746289Sdfr   If OSECT is NULL, then update all sections' mapped state
341846289Sdfr   (after re-reading the entire target _ovly_table).
341946289Sdfr   If OSECT is non-NULL, then try to find a matching entry in the
342046289Sdfr   cached ovly_table and update only OSECT's mapped state.
342146289Sdfr   If a cached entry can't be found or the cache isn't valid, then
342246289Sdfr   re-read the entire cache, and go ahead and update all sections.  */
342346289Sdfr
342446289Sdfrstatic void
342599000Sobriensimple_overlay_update (struct obj_section *osect)
342646289Sdfr{
342799000Sobrien  struct objfile *objfile;
342846289Sdfr
342946289Sdfr  /* Were we given an osect to look up?  NULL means do all of them. */
343046289Sdfr  if (osect)
343146289Sdfr    /* Have we got a cached copy of the target's overlay table? */
343246289Sdfr    if (cache_ovly_table != NULL)
343346289Sdfr      /* Does its cached location match what's currently in the symtab? */
343499000Sobrien      if (cache_ovly_table_base ==
343599000Sobrien	  SYMBOL_VALUE_ADDRESS (lookup_minimal_symbol ("_ovly_table", NULL, NULL)))
343646289Sdfr	/* Then go ahead and try to look up this single section in the cache */
343746289Sdfr	if (simple_overlay_update_1 (osect))
343846289Sdfr	  /* Found it!  We're done. */
343946289Sdfr	  return;
344046289Sdfr
344146289Sdfr  /* Cached table no good: need to read the entire table anew.
344246289Sdfr     Or else we want all the sections, in which case it's actually
344346289Sdfr     more efficient to read the whole table in one block anyway.  */
344446289Sdfr
344599000Sobrien  if (! simple_read_overlay_table ())
344699000Sobrien    return;
344799000Sobrien
344846289Sdfr  /* Now may as well update all sections, even if only one was requested. */
344946289Sdfr  ALL_OBJSECTIONS (objfile, osect)
345046289Sdfr    if (section_is_overlay (osect->the_bfd_section))
345199000Sobrien    {
345299000Sobrien      int i, size;
345399000Sobrien      bfd *obfd = osect->objfile->obfd;
345499000Sobrien      asection *bsect = osect->the_bfd_section;
345546289Sdfr
3456218822Sdim      size = bfd_get_section_size (osect->the_bfd_section);
345799000Sobrien      for (i = 0; i < cache_novlys; i++)
345899000Sobrien	if (cache_ovly_table[i][VMA] == bfd_section_vma (obfd, bsect)
345999000Sobrien	    && cache_ovly_table[i][LMA] == bfd_section_lma (obfd, bsect)
346099000Sobrien	    /* && cache_ovly_table[i][SIZE] == size */ )
346199000Sobrien	  { /* obj_section matches i'th entry in ovly_table */
346299000Sobrien	    osect->ovly_mapped = cache_ovly_table[i][MAPPED];
346399000Sobrien	    break;		/* finished with inner for loop: break out */
346499000Sobrien	  }
346599000Sobrien    }
346646289Sdfr}
346746289Sdfr
3468130809Smarcel/* Set the output sections and output offsets for section SECTP in
3469130809Smarcel   ABFD.  The relocation code in BFD will read these offsets, so we
3470130809Smarcel   need to be sure they're initialized.  We map each section to itself,
3471130809Smarcel   with no offset; this means that SECTP->vma will be honored.  */
347246289Sdfr
3473130809Smarcelstatic void
3474130809Smarcelsymfile_dummy_outputs (bfd *abfd, asection *sectp, void *dummy)
3475130809Smarcel{
3476130809Smarcel  sectp->output_section = sectp;
3477130809Smarcel  sectp->output_offset = 0;
3478130809Smarcel}
3479130809Smarcel
3480130809Smarcel/* Relocate the contents of a debug section SECTP in ABFD.  The
3481130809Smarcel   contents are stored in BUF if it is non-NULL, or returned in a
3482130809Smarcel   malloc'd buffer otherwise.
3483130809Smarcel
3484130809Smarcel   For some platforms and debug info formats, shared libraries contain
3485130809Smarcel   relocations against the debug sections (particularly for DWARF-2;
3486130809Smarcel   one affected platform is PowerPC GNU/Linux, although it depends on
3487130809Smarcel   the version of the linker in use).  Also, ELF object files naturally
3488130809Smarcel   have unresolved relocations for their debug sections.  We need to apply
3489130809Smarcel   the relocations in order to get the locations of symbols correct.  */
3490130809Smarcel
3491130809Smarcelbfd_byte *
3492130809Smarcelsymfile_relocate_debug_section (bfd *abfd, asection *sectp, bfd_byte *buf)
3493130809Smarcel{
3494130809Smarcel  /* We're only interested in debugging sections with relocation
3495130809Smarcel     information.  */
3496130809Smarcel  if ((sectp->flags & SEC_RELOC) == 0)
3497130809Smarcel    return NULL;
3498130809Smarcel  if ((sectp->flags & SEC_DEBUGGING) == 0)
3499130809Smarcel    return NULL;
3500130809Smarcel
3501130809Smarcel  /* We will handle section offsets properly elsewhere, so relocate as if
3502130809Smarcel     all sections begin at 0.  */
3503130809Smarcel  bfd_map_over_sections (abfd, symfile_dummy_outputs, NULL);
3504130809Smarcel
3505130809Smarcel  return bfd_simple_get_relocated_section_contents (abfd, sectp, buf, NULL);
3506130809Smarcel}
3507130809Smarcel
350846289Sdfrvoid
350999000Sobrien_initialize_symfile (void)
351019370Spst{
351119370Spst  struct cmd_list_element *c;
351299000Sobrien
351319370Spst  c = add_cmd ("symbol-file", class_files, symbol_file_command,
351499000Sobrien	       "Load symbol table from executable file FILE.\n\
351519370SpstThe `file' command can also load symbol tables, as well as setting the file\n\
351619370Spstto execute.", &cmdlist);
3517130809Smarcel  set_cmd_completer (c, filename_completer);
351819370Spst
351919370Spst  c = add_cmd ("add-symbol-file", class_files, add_symbol_file_command,
352099000Sobrien	       "Usage: add-symbol-file FILE ADDR [-s <SECT> <SECT_ADDR> -s <SECT> <SECT_ADDR> ...]\n\
352119370SpstLoad the symbols from FILE, assuming FILE has been dynamically loaded.\n\
352299000SobrienADDR is the starting address of the file's text.\n\
352399000SobrienThe optional arguments are section-name section-address pairs and\n\
352499000Sobrienshould be specified if the data and bss segments are not contiguous\n\
3525130809Smarcelwith the text.  SECT is a section name to be loaded at SECT_ADDR.",
352619370Spst	       &cmdlist);
3527130809Smarcel  set_cmd_completer (c, filename_completer);
352819370Spst
352919370Spst  c = add_cmd ("add-shared-symbol-files", class_files,
353019370Spst	       add_shared_symbol_files_command,
353119370Spst   "Load the symbols from shared objects in the dynamic linker's link map.",
353299000Sobrien	       &cmdlist);
353319370Spst  c = add_alias_cmd ("assf", "add-shared-symbol-files", class_files, 1,
353419370Spst		     &cmdlist);
353519370Spst
353619370Spst  c = add_cmd ("load", class_files, load_command,
353799000Sobrien	       "Dynamically load FILE into the running program, and record its symbols\n\
353819370Spstfor access from GDB.", &cmdlist);
3539130809Smarcel  set_cmd_completer (c, filename_completer);
354019370Spst
354119370Spst  add_show_from_set
354219370Spst    (add_set_cmd ("symbol-reloading", class_support, var_boolean,
354399000Sobrien		  (char *) &symbol_reloading,
354499000Sobrien	    "Set dynamic symbol table reloading multiple times in one run.",
354519370Spst		  &setlist),
354619370Spst     &showlist);
354719370Spst
354899000Sobrien  add_prefix_cmd ("overlay", class_support, overlay_command,
354999000Sobrien		  "Commands for debugging overlays.", &overlaylist,
355046289Sdfr		  "overlay ", 0, &cmdlist);
355146289Sdfr
355246289Sdfr  add_com_alias ("ovly", "overlay", class_alias, 1);
355346289Sdfr  add_com_alias ("ov", "overlay", class_alias, 1);
355446289Sdfr
355599000Sobrien  add_cmd ("map-overlay", class_support, map_overlay_command,
355646289Sdfr	   "Assert that an overlay section is mapped.", &overlaylist);
355746289Sdfr
355899000Sobrien  add_cmd ("unmap-overlay", class_support, unmap_overlay_command,
355946289Sdfr	   "Assert that an overlay section is unmapped.", &overlaylist);
356046289Sdfr
356199000Sobrien  add_cmd ("list-overlays", class_support, list_overlays_command,
356246289Sdfr	   "List mappings of overlay sections.", &overlaylist);
356346289Sdfr
356499000Sobrien  add_cmd ("manual", class_support, overlay_manual_command,
356546289Sdfr	   "Enable overlay debugging.", &overlaylist);
356699000Sobrien  add_cmd ("off", class_support, overlay_off_command,
356746289Sdfr	   "Disable overlay debugging.", &overlaylist);
356899000Sobrien  add_cmd ("auto", class_support, overlay_auto_command,
356946289Sdfr	   "Enable automatic overlay debugging.", &overlaylist);
357099000Sobrien  add_cmd ("load-target", class_support, overlay_load_command,
357146289Sdfr	   "Read the overlay mapping state from the target.", &overlaylist);
357246289Sdfr
357346289Sdfr  /* Filename extension to source language lookup table: */
357446289Sdfr  init_filename_language_table ();
357546289Sdfr  c = add_set_cmd ("extension-language", class_files, var_string_noescape,
357699000Sobrien		   (char *) &ext_args,
357746289Sdfr		   "Set mapping between filename extension and source language.\n\
357846289SdfrUsage: set extension-language .foo bar",
357999000Sobrien		   &setlist);
358099000Sobrien  set_cmd_cfunc (c, set_ext_lang_command);
358146289Sdfr
358299000Sobrien  add_info ("extensions", info_ext_lang_command,
358346289Sdfr	    "All filename extensions associated with a source language.");
358499000Sobrien
358599000Sobrien  add_show_from_set
358699000Sobrien    (add_set_cmd ("download-write-size", class_obscure,
358799000Sobrien		  var_integer, (char *) &download_write_size,
358899000Sobrien		  "Set the write size used when downloading a program.\n"
358999000Sobrien		  "Only used when downloading a program onto a remote\n"
359099000Sobrien		  "target. Specify zero, or a negative value, to disable\n"
359199000Sobrien		  "blocked writes. The actual size of each transfer is also\n"
359299000Sobrien		  "limited by the size of the target packet and the memory\n"
359399000Sobrien		  "cache.\n",
359499000Sobrien		  &setlist),
359599000Sobrien     &showlist);
3596130809Smarcel
3597130809Smarcel  debug_file_directory = xstrdup (DEBUGDIR);
3598130809Smarcel  c = (add_set_cmd
3599130809Smarcel       ("debug-file-directory", class_support, var_string,
3600130809Smarcel        (char *) &debug_file_directory,
3601130809Smarcel        "Set the directory where separate debug symbols are searched for.\n"
3602130809Smarcel        "Separate debug symbols are first searched for in the same\n"
3603130809Smarcel        "directory as the binary, then in the `" DEBUG_SUBDIRECTORY
3604130809Smarcel        "' subdirectory,\n"
3605130809Smarcel        "and lastly at the path of the directory of the binary with\n"
3606130809Smarcel        "the global debug-file directory prepended\n",
3607130809Smarcel        &setlist));
3608130809Smarcel  add_show_from_set (c, &showlist);
3609130809Smarcel  set_cmd_completer (c, filename_completer);
361019370Spst}
3611