xref: /dragonfly/contrib/gdb-7/gdb/symfile.c (revision cf7f2e2d)
15796c8dcSSimon Schubert /* Generic symbol file reading for the GNU debugger, GDB.
25796c8dcSSimon Schubert 
35796c8dcSSimon Schubert    Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4*cf7f2e2dSJohn Marino    2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
55796c8dcSSimon Schubert    Free Software Foundation, Inc.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    Contributed by Cygnus Support, using pieces from other GDB modules.
85796c8dcSSimon Schubert 
95796c8dcSSimon Schubert    This file is part of GDB.
105796c8dcSSimon Schubert 
115796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
125796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
135796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
145796c8dcSSimon Schubert    (at your option) any later version.
155796c8dcSSimon Schubert 
165796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
175796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
185796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
195796c8dcSSimon Schubert    GNU General Public License for more details.
205796c8dcSSimon Schubert 
215796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
225796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
235796c8dcSSimon Schubert 
245796c8dcSSimon Schubert #include "defs.h"
255796c8dcSSimon Schubert #include "arch-utils.h"
265796c8dcSSimon Schubert #include "bfdlink.h"
275796c8dcSSimon Schubert #include "symtab.h"
285796c8dcSSimon Schubert #include "gdbtypes.h"
295796c8dcSSimon Schubert #include "gdbcore.h"
305796c8dcSSimon Schubert #include "frame.h"
315796c8dcSSimon Schubert #include "target.h"
325796c8dcSSimon Schubert #include "value.h"
335796c8dcSSimon Schubert #include "symfile.h"
345796c8dcSSimon Schubert #include "objfiles.h"
355796c8dcSSimon Schubert #include "source.h"
365796c8dcSSimon Schubert #include "gdbcmd.h"
375796c8dcSSimon Schubert #include "breakpoint.h"
385796c8dcSSimon Schubert #include "language.h"
395796c8dcSSimon Schubert #include "complaints.h"
405796c8dcSSimon Schubert #include "demangle.h"
415796c8dcSSimon Schubert #include "inferior.h"
425796c8dcSSimon Schubert #include "regcache.h"
435796c8dcSSimon Schubert #include "filenames.h"		/* for DOSish file names */
445796c8dcSSimon Schubert #include "gdb-stabs.h"
455796c8dcSSimon Schubert #include "gdb_obstack.h"
465796c8dcSSimon Schubert #include "completer.h"
475796c8dcSSimon Schubert #include "bcache.h"
485796c8dcSSimon Schubert #include "hashtab.h"
495796c8dcSSimon Schubert #include "readline/readline.h"
505796c8dcSSimon Schubert #include "gdb_assert.h"
515796c8dcSSimon Schubert #include "block.h"
525796c8dcSSimon Schubert #include "observer.h"
535796c8dcSSimon Schubert #include "exec.h"
545796c8dcSSimon Schubert #include "parser-defs.h"
555796c8dcSSimon Schubert #include "varobj.h"
565796c8dcSSimon Schubert #include "elf-bfd.h"
575796c8dcSSimon Schubert #include "solib.h"
585796c8dcSSimon Schubert #include "remote.h"
595796c8dcSSimon Schubert 
605796c8dcSSimon Schubert #include <sys/types.h>
615796c8dcSSimon Schubert #include <fcntl.h>
625796c8dcSSimon Schubert #include "gdb_string.h"
635796c8dcSSimon Schubert #include "gdb_stat.h"
645796c8dcSSimon Schubert #include <ctype.h>
655796c8dcSSimon Schubert #include <time.h>
665796c8dcSSimon Schubert #include <sys/time.h>
675796c8dcSSimon Schubert 
68*cf7f2e2dSJohn Marino #include "psymtab.h"
695796c8dcSSimon Schubert 
705796c8dcSSimon Schubert int (*deprecated_ui_load_progress_hook) (const char *section, unsigned long num);
715796c8dcSSimon Schubert void (*deprecated_show_load_progress) (const char *section,
725796c8dcSSimon Schubert 			    unsigned long section_sent,
735796c8dcSSimon Schubert 			    unsigned long section_size,
745796c8dcSSimon Schubert 			    unsigned long total_sent,
755796c8dcSSimon Schubert 			    unsigned long total_size);
765796c8dcSSimon Schubert void (*deprecated_pre_add_symbol_hook) (const char *);
775796c8dcSSimon Schubert void (*deprecated_post_add_symbol_hook) (void);
785796c8dcSSimon Schubert 
795796c8dcSSimon Schubert static void clear_symtab_users_cleanup (void *ignore);
805796c8dcSSimon Schubert 
815796c8dcSSimon Schubert /* Global variables owned by this file */
825796c8dcSSimon Schubert int readnow_symbol_files;	/* Read full symbols immediately */
835796c8dcSSimon Schubert 
845796c8dcSSimon Schubert /* External variables and functions referenced. */
855796c8dcSSimon Schubert 
865796c8dcSSimon Schubert extern void report_transfer_performance (unsigned long, time_t, time_t);
875796c8dcSSimon Schubert 
885796c8dcSSimon Schubert /* Functions this file defines */
895796c8dcSSimon Schubert 
905796c8dcSSimon Schubert #if 0
915796c8dcSSimon Schubert static int simple_read_overlay_region_table (void);
925796c8dcSSimon Schubert static void simple_free_overlay_region_table (void);
935796c8dcSSimon Schubert #endif
945796c8dcSSimon Schubert 
955796c8dcSSimon Schubert static void load_command (char *, int);
965796c8dcSSimon Schubert 
975796c8dcSSimon Schubert static void symbol_file_add_main_1 (char *args, int from_tty, int flags);
985796c8dcSSimon Schubert 
995796c8dcSSimon Schubert static void add_symbol_file_command (char *, int);
1005796c8dcSSimon Schubert 
1015796c8dcSSimon Schubert bfd *symfile_bfd_open (char *);
1025796c8dcSSimon Schubert 
1035796c8dcSSimon Schubert int get_section_index (struct objfile *, char *);
1045796c8dcSSimon Schubert 
1055796c8dcSSimon Schubert static struct sym_fns *find_sym_fns (bfd *);
1065796c8dcSSimon Schubert 
1075796c8dcSSimon Schubert static void decrement_reading_symtab (void *);
1085796c8dcSSimon Schubert 
1095796c8dcSSimon Schubert static void overlay_invalidate_all (void);
1105796c8dcSSimon Schubert 
1115796c8dcSSimon Schubert void list_overlays_command (char *, int);
1125796c8dcSSimon Schubert 
1135796c8dcSSimon Schubert void map_overlay_command (char *, int);
1145796c8dcSSimon Schubert 
1155796c8dcSSimon Schubert void unmap_overlay_command (char *, int);
1165796c8dcSSimon Schubert 
1175796c8dcSSimon Schubert static void overlay_auto_command (char *, int);
1185796c8dcSSimon Schubert 
1195796c8dcSSimon Schubert static void overlay_manual_command (char *, int);
1205796c8dcSSimon Schubert 
1215796c8dcSSimon Schubert static void overlay_off_command (char *, int);
1225796c8dcSSimon Schubert 
1235796c8dcSSimon Schubert static void overlay_load_command (char *, int);
1245796c8dcSSimon Schubert 
1255796c8dcSSimon Schubert static void overlay_command (char *, int);
1265796c8dcSSimon Schubert 
1275796c8dcSSimon Schubert static void simple_free_overlay_table (void);
1285796c8dcSSimon Schubert 
1295796c8dcSSimon Schubert static void read_target_long_array (CORE_ADDR, unsigned int *, int, int,
1305796c8dcSSimon Schubert 				    enum bfd_endian);
1315796c8dcSSimon Schubert 
1325796c8dcSSimon Schubert static int simple_read_overlay_table (void);
1335796c8dcSSimon Schubert 
1345796c8dcSSimon Schubert static int simple_overlay_update_1 (struct obj_section *);
1355796c8dcSSimon Schubert 
1365796c8dcSSimon Schubert static void add_filename_language (char *ext, enum language lang);
1375796c8dcSSimon Schubert 
1385796c8dcSSimon Schubert static void info_ext_lang_command (char *args, int from_tty);
1395796c8dcSSimon Schubert 
1405796c8dcSSimon Schubert static void init_filename_language_table (void);
1415796c8dcSSimon Schubert 
1425796c8dcSSimon Schubert static void symfile_find_segment_sections (struct objfile *objfile);
1435796c8dcSSimon Schubert 
1445796c8dcSSimon Schubert void _initialize_symfile (void);
1455796c8dcSSimon Schubert 
1465796c8dcSSimon Schubert /* List of all available sym_fns.  On gdb startup, each object file reader
1475796c8dcSSimon Schubert    calls add_symtab_fns() to register information on each format it is
1485796c8dcSSimon Schubert    prepared to read. */
1495796c8dcSSimon Schubert 
1505796c8dcSSimon Schubert static struct sym_fns *symtab_fns = NULL;
1515796c8dcSSimon Schubert 
1525796c8dcSSimon Schubert /* Flag for whether user will be reloading symbols multiple times.
1535796c8dcSSimon Schubert    Defaults to ON for VxWorks, otherwise OFF.  */
1545796c8dcSSimon Schubert 
1555796c8dcSSimon Schubert #ifdef SYMBOL_RELOADING_DEFAULT
1565796c8dcSSimon Schubert int symbol_reloading = SYMBOL_RELOADING_DEFAULT;
1575796c8dcSSimon Schubert #else
1585796c8dcSSimon Schubert int symbol_reloading = 0;
1595796c8dcSSimon Schubert #endif
1605796c8dcSSimon Schubert static void
1615796c8dcSSimon Schubert show_symbol_reloading (struct ui_file *file, int from_tty,
1625796c8dcSSimon Schubert 		       struct cmd_list_element *c, const char *value)
1635796c8dcSSimon Schubert {
1645796c8dcSSimon Schubert   fprintf_filtered (file, _("\
1655796c8dcSSimon Schubert Dynamic symbol table reloading multiple times in one run is %s.\n"),
1665796c8dcSSimon Schubert 		    value);
1675796c8dcSSimon Schubert }
1685796c8dcSSimon Schubert 
1695796c8dcSSimon Schubert /* If non-zero, shared library symbols will be added automatically
1705796c8dcSSimon Schubert    when the inferior is created, new libraries are loaded, or when
1715796c8dcSSimon Schubert    attaching to the inferior.  This is almost always what users will
1725796c8dcSSimon Schubert    want to have happen; but for very large programs, the startup time
1735796c8dcSSimon Schubert    will be excessive, and so if this is a problem, the user can clear
1745796c8dcSSimon Schubert    this flag and then add the shared library symbols as needed.  Note
1755796c8dcSSimon Schubert    that there is a potential for confusion, since if the shared
1765796c8dcSSimon Schubert    library symbols are not loaded, commands like "info fun" will *not*
1775796c8dcSSimon Schubert    report all the functions that are actually present. */
1785796c8dcSSimon Schubert 
1795796c8dcSSimon Schubert int auto_solib_add = 1;
1805796c8dcSSimon Schubert 
1815796c8dcSSimon Schubert /* For systems that support it, a threshold size in megabytes.  If
1825796c8dcSSimon Schubert    automatically adding a new library's symbol table to those already
1835796c8dcSSimon Schubert    known to the debugger would cause the total shared library symbol
1845796c8dcSSimon Schubert    size to exceed this threshhold, then the shlib's symbols are not
1855796c8dcSSimon Schubert    added.  The threshold is ignored if the user explicitly asks for a
1865796c8dcSSimon Schubert    shlib to be added, such as when using the "sharedlibrary"
1875796c8dcSSimon Schubert    command. */
1885796c8dcSSimon Schubert 
1895796c8dcSSimon Schubert int auto_solib_limit;
1905796c8dcSSimon Schubert 
1915796c8dcSSimon Schubert 
1925796c8dcSSimon Schubert /* Make a null terminated copy of the string at PTR with SIZE characters in
1935796c8dcSSimon Schubert    the obstack pointed to by OBSTACKP .  Returns the address of the copy.
1945796c8dcSSimon Schubert    Note that the string at PTR does not have to be null terminated, I.E. it
1955796c8dcSSimon Schubert    may be part of a larger string and we are only saving a substring. */
1965796c8dcSSimon Schubert 
1975796c8dcSSimon Schubert char *
1985796c8dcSSimon Schubert obsavestring (const char *ptr, int size, struct obstack *obstackp)
1995796c8dcSSimon Schubert {
2005796c8dcSSimon Schubert   char *p = (char *) obstack_alloc (obstackp, size + 1);
2015796c8dcSSimon Schubert   /* Open-coded memcpy--saves function call time.  These strings are usually
2025796c8dcSSimon Schubert      short.  FIXME: Is this really still true with a compiler that can
2035796c8dcSSimon Schubert      inline memcpy? */
2045796c8dcSSimon Schubert   {
2055796c8dcSSimon Schubert     const char *p1 = ptr;
2065796c8dcSSimon Schubert     char *p2 = p;
2075796c8dcSSimon Schubert     const char *end = ptr + size;
208*cf7f2e2dSJohn Marino 
2095796c8dcSSimon Schubert     while (p1 != end)
2105796c8dcSSimon Schubert       *p2++ = *p1++;
2115796c8dcSSimon Schubert   }
2125796c8dcSSimon Schubert   p[size] = 0;
2135796c8dcSSimon Schubert   return p;
2145796c8dcSSimon Schubert }
2155796c8dcSSimon Schubert 
216*cf7f2e2dSJohn Marino /* Concatenate NULL terminated variable argument list of `const char *' strings;
217*cf7f2e2dSJohn Marino    return the new string.  Space is found in the OBSTACKP.  Argument list must
218*cf7f2e2dSJohn Marino    be terminated by a sentinel expression `(char *) NULL'.  */
2195796c8dcSSimon Schubert 
2205796c8dcSSimon Schubert char *
221*cf7f2e2dSJohn Marino obconcat (struct obstack *obstackp, ...)
2225796c8dcSSimon Schubert {
223*cf7f2e2dSJohn Marino   va_list ap;
224*cf7f2e2dSJohn Marino 
225*cf7f2e2dSJohn Marino   va_start (ap, obstackp);
226*cf7f2e2dSJohn Marino   for (;;)
227*cf7f2e2dSJohn Marino     {
228*cf7f2e2dSJohn Marino       const char *s = va_arg (ap, const char *);
229*cf7f2e2dSJohn Marino 
230*cf7f2e2dSJohn Marino       if (s == NULL)
231*cf7f2e2dSJohn Marino 	break;
232*cf7f2e2dSJohn Marino 
233*cf7f2e2dSJohn Marino       obstack_grow_str (obstackp, s);
234*cf7f2e2dSJohn Marino     }
235*cf7f2e2dSJohn Marino   va_end (ap);
236*cf7f2e2dSJohn Marino   obstack_1grow (obstackp, 0);
237*cf7f2e2dSJohn Marino 
238*cf7f2e2dSJohn Marino   return obstack_finish (obstackp);
2395796c8dcSSimon Schubert }
2405796c8dcSSimon Schubert 
241*cf7f2e2dSJohn Marino /* True if we are reading a symbol table. */
2425796c8dcSSimon Schubert 
2435796c8dcSSimon Schubert int currently_reading_symtab = 0;
2445796c8dcSSimon Schubert 
2455796c8dcSSimon Schubert static void
2465796c8dcSSimon Schubert decrement_reading_symtab (void *dummy)
2475796c8dcSSimon Schubert {
2485796c8dcSSimon Schubert   currently_reading_symtab--;
2495796c8dcSSimon Schubert }
2505796c8dcSSimon Schubert 
251*cf7f2e2dSJohn Marino /* Increment currently_reading_symtab and return a cleanup that can be
252*cf7f2e2dSJohn Marino    used to decrement it.  */
253*cf7f2e2dSJohn Marino struct cleanup *
254*cf7f2e2dSJohn Marino increment_reading_symtab (void)
2555796c8dcSSimon Schubert {
256*cf7f2e2dSJohn Marino   ++currently_reading_symtab;
257*cf7f2e2dSJohn Marino   return make_cleanup (decrement_reading_symtab, NULL);
2585796c8dcSSimon Schubert }
2595796c8dcSSimon Schubert 
2605796c8dcSSimon Schubert /* Remember the lowest-addressed loadable section we've seen.
2615796c8dcSSimon Schubert    This function is called via bfd_map_over_sections.
2625796c8dcSSimon Schubert 
2635796c8dcSSimon Schubert    In case of equal vmas, the section with the largest size becomes the
2645796c8dcSSimon Schubert    lowest-addressed loadable section.
2655796c8dcSSimon Schubert 
2665796c8dcSSimon Schubert    If the vmas and sizes are equal, the last section is considered the
2675796c8dcSSimon Schubert    lowest-addressed loadable section.  */
2685796c8dcSSimon Schubert 
2695796c8dcSSimon Schubert void
2705796c8dcSSimon Schubert find_lowest_section (bfd *abfd, asection *sect, void *obj)
2715796c8dcSSimon Schubert {
2725796c8dcSSimon Schubert   asection **lowest = (asection **) obj;
2735796c8dcSSimon Schubert 
274*cf7f2e2dSJohn Marino   if (0 == (bfd_get_section_flags (abfd, sect) & (SEC_ALLOC | SEC_LOAD)))
2755796c8dcSSimon Schubert     return;
2765796c8dcSSimon Schubert   if (!*lowest)
2775796c8dcSSimon Schubert     *lowest = sect;		/* First loadable section */
2785796c8dcSSimon Schubert   else if (bfd_section_vma (abfd, *lowest) > bfd_section_vma (abfd, sect))
2795796c8dcSSimon Schubert     *lowest = sect;		/* A lower loadable section */
2805796c8dcSSimon Schubert   else if (bfd_section_vma (abfd, *lowest) == bfd_section_vma (abfd, sect)
2815796c8dcSSimon Schubert 	   && (bfd_section_size (abfd, (*lowest))
2825796c8dcSSimon Schubert 	       <= bfd_section_size (abfd, sect)))
2835796c8dcSSimon Schubert     *lowest = sect;
2845796c8dcSSimon Schubert }
2855796c8dcSSimon Schubert 
2865796c8dcSSimon Schubert /* Create a new section_addr_info, with room for NUM_SECTIONS.  */
2875796c8dcSSimon Schubert 
2885796c8dcSSimon Schubert struct section_addr_info *
2895796c8dcSSimon Schubert alloc_section_addr_info (size_t num_sections)
2905796c8dcSSimon Schubert {
2915796c8dcSSimon Schubert   struct section_addr_info *sap;
2925796c8dcSSimon Schubert   size_t size;
2935796c8dcSSimon Schubert 
2945796c8dcSSimon Schubert   size = (sizeof (struct section_addr_info)
2955796c8dcSSimon Schubert 	  +  sizeof (struct other_sections) * (num_sections - 1));
2965796c8dcSSimon Schubert   sap = (struct section_addr_info *) xmalloc (size);
2975796c8dcSSimon Schubert   memset (sap, 0, size);
2985796c8dcSSimon Schubert   sap->num_sections = num_sections;
2995796c8dcSSimon Schubert 
3005796c8dcSSimon Schubert   return sap;
3015796c8dcSSimon Schubert }
3025796c8dcSSimon Schubert 
3035796c8dcSSimon Schubert /* Build (allocate and populate) a section_addr_info struct from
3045796c8dcSSimon Schubert    an existing section table. */
3055796c8dcSSimon Schubert 
3065796c8dcSSimon Schubert extern struct section_addr_info *
3075796c8dcSSimon Schubert build_section_addr_info_from_section_table (const struct target_section *start,
3085796c8dcSSimon Schubert                                             const struct target_section *end)
3095796c8dcSSimon Schubert {
3105796c8dcSSimon Schubert   struct section_addr_info *sap;
3115796c8dcSSimon Schubert   const struct target_section *stp;
3125796c8dcSSimon Schubert   int oidx;
3135796c8dcSSimon Schubert 
3145796c8dcSSimon Schubert   sap = alloc_section_addr_info (end - start);
3155796c8dcSSimon Schubert 
3165796c8dcSSimon Schubert   for (stp = start, oidx = 0; stp != end; stp++)
3175796c8dcSSimon Schubert     {
3185796c8dcSSimon Schubert       if (bfd_get_section_flags (stp->bfd,
3195796c8dcSSimon Schubert 				 stp->the_bfd_section) & (SEC_ALLOC | SEC_LOAD)
3205796c8dcSSimon Schubert 	  && oidx < end - start)
3215796c8dcSSimon Schubert 	{
3225796c8dcSSimon Schubert 	  sap->other[oidx].addr = stp->addr;
3235796c8dcSSimon Schubert 	  sap->other[oidx].name
3245796c8dcSSimon Schubert 	    = xstrdup (bfd_section_name (stp->bfd, stp->the_bfd_section));
3255796c8dcSSimon Schubert 	  sap->other[oidx].sectindex = stp->the_bfd_section->index;
3265796c8dcSSimon Schubert 	  oidx++;
3275796c8dcSSimon Schubert 	}
3285796c8dcSSimon Schubert     }
3295796c8dcSSimon Schubert 
3305796c8dcSSimon Schubert   return sap;
3315796c8dcSSimon Schubert }
3325796c8dcSSimon Schubert 
333*cf7f2e2dSJohn Marino /* Create a section_addr_info from section offsets in ABFD.  */
334*cf7f2e2dSJohn Marino 
335*cf7f2e2dSJohn Marino static struct section_addr_info *
336*cf7f2e2dSJohn Marino build_section_addr_info_from_bfd (bfd *abfd)
337*cf7f2e2dSJohn Marino {
338*cf7f2e2dSJohn Marino   struct section_addr_info *sap;
339*cf7f2e2dSJohn Marino   int i;
340*cf7f2e2dSJohn Marino   struct bfd_section *sec;
341*cf7f2e2dSJohn Marino 
342*cf7f2e2dSJohn Marino   sap = alloc_section_addr_info (bfd_count_sections (abfd));
343*cf7f2e2dSJohn Marino   for (i = 0, sec = abfd->sections; sec != NULL; sec = sec->next)
344*cf7f2e2dSJohn Marino     if (bfd_get_section_flags (abfd, sec) & (SEC_ALLOC | SEC_LOAD))
345*cf7f2e2dSJohn Marino       {
346*cf7f2e2dSJohn Marino 	sap->other[i].addr = bfd_get_section_vma (abfd, sec);
347*cf7f2e2dSJohn Marino 	sap->other[i].name = xstrdup (bfd_get_section_name (abfd, sec));
348*cf7f2e2dSJohn Marino 	sap->other[i].sectindex = sec->index;
349*cf7f2e2dSJohn Marino 	i++;
350*cf7f2e2dSJohn Marino       }
351*cf7f2e2dSJohn Marino   return sap;
352*cf7f2e2dSJohn Marino }
353*cf7f2e2dSJohn Marino 
354*cf7f2e2dSJohn Marino /* Create a section_addr_info from section offsets in OBJFILE.  */
355*cf7f2e2dSJohn Marino 
356*cf7f2e2dSJohn Marino struct section_addr_info *
357*cf7f2e2dSJohn Marino build_section_addr_info_from_objfile (const struct objfile *objfile)
358*cf7f2e2dSJohn Marino {
359*cf7f2e2dSJohn Marino   struct section_addr_info *sap;
360*cf7f2e2dSJohn Marino   int i;
361*cf7f2e2dSJohn Marino 
362*cf7f2e2dSJohn Marino   /* Before reread_symbols gets rewritten it is not safe to call:
363*cf7f2e2dSJohn Marino      gdb_assert (objfile->num_sections == bfd_count_sections (objfile->obfd));
364*cf7f2e2dSJohn Marino      */
365*cf7f2e2dSJohn Marino   sap = build_section_addr_info_from_bfd (objfile->obfd);
366*cf7f2e2dSJohn Marino   for (i = 0; i < sap->num_sections && sap->other[i].name; i++)
367*cf7f2e2dSJohn Marino     {
368*cf7f2e2dSJohn Marino       int sectindex = sap->other[i].sectindex;
369*cf7f2e2dSJohn Marino 
370*cf7f2e2dSJohn Marino       sap->other[i].addr += objfile->section_offsets->offsets[sectindex];
371*cf7f2e2dSJohn Marino     }
372*cf7f2e2dSJohn Marino   return sap;
373*cf7f2e2dSJohn Marino }
3745796c8dcSSimon Schubert 
3755796c8dcSSimon Schubert /* Free all memory allocated by build_section_addr_info_from_section_table. */
3765796c8dcSSimon Schubert 
3775796c8dcSSimon Schubert extern void
3785796c8dcSSimon Schubert free_section_addr_info (struct section_addr_info *sap)
3795796c8dcSSimon Schubert {
3805796c8dcSSimon Schubert   int idx;
3815796c8dcSSimon Schubert 
3825796c8dcSSimon Schubert   for (idx = 0; idx < sap->num_sections; idx++)
3835796c8dcSSimon Schubert     if (sap->other[idx].name)
3845796c8dcSSimon Schubert       xfree (sap->other[idx].name);
3855796c8dcSSimon Schubert   xfree (sap);
3865796c8dcSSimon Schubert }
3875796c8dcSSimon Schubert 
3885796c8dcSSimon Schubert 
3895796c8dcSSimon Schubert /* Initialize OBJFILE's sect_index_* members.  */
3905796c8dcSSimon Schubert static void
3915796c8dcSSimon Schubert init_objfile_sect_indices (struct objfile *objfile)
3925796c8dcSSimon Schubert {
3935796c8dcSSimon Schubert   asection *sect;
3945796c8dcSSimon Schubert   int i;
3955796c8dcSSimon Schubert 
3965796c8dcSSimon Schubert   sect = bfd_get_section_by_name (objfile->obfd, ".text");
3975796c8dcSSimon Schubert   if (sect)
3985796c8dcSSimon Schubert     objfile->sect_index_text = sect->index;
3995796c8dcSSimon Schubert 
4005796c8dcSSimon Schubert   sect = bfd_get_section_by_name (objfile->obfd, ".data");
4015796c8dcSSimon Schubert   if (sect)
4025796c8dcSSimon Schubert     objfile->sect_index_data = sect->index;
4035796c8dcSSimon Schubert 
4045796c8dcSSimon Schubert   sect = bfd_get_section_by_name (objfile->obfd, ".bss");
4055796c8dcSSimon Schubert   if (sect)
4065796c8dcSSimon Schubert     objfile->sect_index_bss = sect->index;
4075796c8dcSSimon Schubert 
4085796c8dcSSimon Schubert   sect = bfd_get_section_by_name (objfile->obfd, ".rodata");
4095796c8dcSSimon Schubert   if (sect)
4105796c8dcSSimon Schubert     objfile->sect_index_rodata = sect->index;
4115796c8dcSSimon Schubert 
4125796c8dcSSimon Schubert   /* This is where things get really weird...  We MUST have valid
4135796c8dcSSimon Schubert      indices for the various sect_index_* members or gdb will abort.
4145796c8dcSSimon Schubert      So if for example, there is no ".text" section, we have to
4155796c8dcSSimon Schubert      accomodate that.  First, check for a file with the standard
4165796c8dcSSimon Schubert      one or two segments.  */
4175796c8dcSSimon Schubert 
4185796c8dcSSimon Schubert   symfile_find_segment_sections (objfile);
4195796c8dcSSimon Schubert 
4205796c8dcSSimon Schubert   /* Except when explicitly adding symbol files at some address,
4215796c8dcSSimon Schubert      section_offsets contains nothing but zeros, so it doesn't matter
4225796c8dcSSimon Schubert      which slot in section_offsets the individual sect_index_* members
4235796c8dcSSimon Schubert      index into.  So if they are all zero, it is safe to just point
4245796c8dcSSimon Schubert      all the currently uninitialized indices to the first slot.  But
4255796c8dcSSimon Schubert      beware: if this is the main executable, it may be relocated
4265796c8dcSSimon Schubert      later, e.g. by the remote qOffsets packet, and then this will
4275796c8dcSSimon Schubert      be wrong!  That's why we try segments first.  */
4285796c8dcSSimon Schubert 
4295796c8dcSSimon Schubert   for (i = 0; i < objfile->num_sections; i++)
4305796c8dcSSimon Schubert     {
4315796c8dcSSimon Schubert       if (ANOFFSET (objfile->section_offsets, i) != 0)
4325796c8dcSSimon Schubert 	{
4335796c8dcSSimon Schubert 	  break;
4345796c8dcSSimon Schubert 	}
4355796c8dcSSimon Schubert     }
4365796c8dcSSimon Schubert   if (i == objfile->num_sections)
4375796c8dcSSimon Schubert     {
4385796c8dcSSimon Schubert       if (objfile->sect_index_text == -1)
4395796c8dcSSimon Schubert 	objfile->sect_index_text = 0;
4405796c8dcSSimon Schubert       if (objfile->sect_index_data == -1)
4415796c8dcSSimon Schubert 	objfile->sect_index_data = 0;
4425796c8dcSSimon Schubert       if (objfile->sect_index_bss == -1)
4435796c8dcSSimon Schubert 	objfile->sect_index_bss = 0;
4445796c8dcSSimon Schubert       if (objfile->sect_index_rodata == -1)
4455796c8dcSSimon Schubert 	objfile->sect_index_rodata = 0;
4465796c8dcSSimon Schubert     }
4475796c8dcSSimon Schubert }
4485796c8dcSSimon Schubert 
4495796c8dcSSimon Schubert /* The arguments to place_section.  */
4505796c8dcSSimon Schubert 
4515796c8dcSSimon Schubert struct place_section_arg
4525796c8dcSSimon Schubert {
4535796c8dcSSimon Schubert   struct section_offsets *offsets;
4545796c8dcSSimon Schubert   CORE_ADDR lowest;
4555796c8dcSSimon Schubert };
4565796c8dcSSimon Schubert 
4575796c8dcSSimon Schubert /* Find a unique offset to use for loadable section SECT if
4585796c8dcSSimon Schubert    the user did not provide an offset.  */
4595796c8dcSSimon Schubert 
4605796c8dcSSimon Schubert static void
4615796c8dcSSimon Schubert place_section (bfd *abfd, asection *sect, void *obj)
4625796c8dcSSimon Schubert {
4635796c8dcSSimon Schubert   struct place_section_arg *arg = obj;
4645796c8dcSSimon Schubert   CORE_ADDR *offsets = arg->offsets->offsets, start_addr;
4655796c8dcSSimon Schubert   int done;
4665796c8dcSSimon Schubert   ULONGEST align = ((ULONGEST) 1) << bfd_get_section_alignment (abfd, sect);
4675796c8dcSSimon Schubert 
4685796c8dcSSimon Schubert   /* We are only interested in allocated sections.  */
4695796c8dcSSimon Schubert   if ((bfd_get_section_flags (abfd, sect) & SEC_ALLOC) == 0)
4705796c8dcSSimon Schubert     return;
4715796c8dcSSimon Schubert 
4725796c8dcSSimon Schubert   /* If the user specified an offset, honor it.  */
4735796c8dcSSimon Schubert   if (offsets[sect->index] != 0)
4745796c8dcSSimon Schubert     return;
4755796c8dcSSimon Schubert 
4765796c8dcSSimon Schubert   /* Otherwise, let's try to find a place for the section.  */
4775796c8dcSSimon Schubert   start_addr = (arg->lowest + align - 1) & -align;
4785796c8dcSSimon Schubert 
4795796c8dcSSimon Schubert   do {
4805796c8dcSSimon Schubert     asection *cur_sec;
4815796c8dcSSimon Schubert 
4825796c8dcSSimon Schubert     done = 1;
4835796c8dcSSimon Schubert 
4845796c8dcSSimon Schubert     for (cur_sec = abfd->sections; cur_sec != NULL; cur_sec = cur_sec->next)
4855796c8dcSSimon Schubert       {
4865796c8dcSSimon Schubert 	int indx = cur_sec->index;
4875796c8dcSSimon Schubert 
4885796c8dcSSimon Schubert 	/* We don't need to compare against ourself.  */
4895796c8dcSSimon Schubert 	if (cur_sec == sect)
4905796c8dcSSimon Schubert 	  continue;
4915796c8dcSSimon Schubert 
4925796c8dcSSimon Schubert 	/* We can only conflict with allocated sections.  */
4935796c8dcSSimon Schubert 	if ((bfd_get_section_flags (abfd, cur_sec) & SEC_ALLOC) == 0)
4945796c8dcSSimon Schubert 	  continue;
4955796c8dcSSimon Schubert 
4965796c8dcSSimon Schubert 	/* If the section offset is 0, either the section has not been placed
4975796c8dcSSimon Schubert 	   yet, or it was the lowest section placed (in which case LOWEST
4985796c8dcSSimon Schubert 	   will be past its end).  */
4995796c8dcSSimon Schubert 	if (offsets[indx] == 0)
5005796c8dcSSimon Schubert 	  continue;
5015796c8dcSSimon Schubert 
5025796c8dcSSimon Schubert 	/* If this section would overlap us, then we must move up.  */
5035796c8dcSSimon Schubert 	if (start_addr + bfd_get_section_size (sect) > offsets[indx]
5045796c8dcSSimon Schubert 	    && start_addr < offsets[indx] + bfd_get_section_size (cur_sec))
5055796c8dcSSimon Schubert 	  {
5065796c8dcSSimon Schubert 	    start_addr = offsets[indx] + bfd_get_section_size (cur_sec);
5075796c8dcSSimon Schubert 	    start_addr = (start_addr + align - 1) & -align;
5085796c8dcSSimon Schubert 	    done = 0;
5095796c8dcSSimon Schubert 	    break;
5105796c8dcSSimon Schubert 	  }
5115796c8dcSSimon Schubert 
5125796c8dcSSimon Schubert 	/* Otherwise, we appear to be OK.  So far.  */
5135796c8dcSSimon Schubert       }
5145796c8dcSSimon Schubert     }
5155796c8dcSSimon Schubert   while (!done);
5165796c8dcSSimon Schubert 
5175796c8dcSSimon Schubert   offsets[sect->index] = start_addr;
5185796c8dcSSimon Schubert   arg->lowest = start_addr + bfd_get_section_size (sect);
5195796c8dcSSimon Schubert }
5205796c8dcSSimon Schubert 
521*cf7f2e2dSJohn Marino /* Store struct section_addr_info as prepared (made relative and with SECTINDEX
522*cf7f2e2dSJohn Marino    filled-in) by addr_info_make_relative into SECTION_OFFSETS of NUM_SECTIONS
523*cf7f2e2dSJohn Marino    entries.  */
5245796c8dcSSimon Schubert 
5255796c8dcSSimon Schubert void
526*cf7f2e2dSJohn Marino relative_addr_info_to_section_offsets (struct section_offsets *section_offsets,
527*cf7f2e2dSJohn Marino 				       int num_sections,
5285796c8dcSSimon Schubert 				       struct section_addr_info *addrs)
5295796c8dcSSimon Schubert {
5305796c8dcSSimon Schubert   int i;
5315796c8dcSSimon Schubert 
532*cf7f2e2dSJohn Marino   memset (section_offsets, 0, SIZEOF_N_SECTION_OFFSETS (num_sections));
5335796c8dcSSimon Schubert 
534*cf7f2e2dSJohn Marino   /* Now calculate offsets for section that were specified by the caller. */
5355796c8dcSSimon Schubert   for (i = 0; i < addrs->num_sections && addrs->other[i].name; i++)
5365796c8dcSSimon Schubert     {
5375796c8dcSSimon Schubert       struct other_sections *osp;
5385796c8dcSSimon Schubert 
5395796c8dcSSimon Schubert       osp = &addrs->other[i];
5405796c8dcSSimon Schubert       if (osp->addr == 0)
5415796c8dcSSimon Schubert   	continue;
5425796c8dcSSimon Schubert 
5435796c8dcSSimon Schubert       /* Record all sections in offsets */
5445796c8dcSSimon Schubert       /* The section_offsets in the objfile are here filled in using
5455796c8dcSSimon Schubert          the BFD index. */
546*cf7f2e2dSJohn Marino       section_offsets->offsets[osp->sectindex] = osp->addr;
5475796c8dcSSimon Schubert     }
548*cf7f2e2dSJohn Marino }
549*cf7f2e2dSJohn Marino 
550*cf7f2e2dSJohn Marino /* qsort comparator for addrs_section_sort.  Sort entries in ascending order by
551*cf7f2e2dSJohn Marino    their (name, sectindex) pair.  sectindex makes the sort by name stable.  */
552*cf7f2e2dSJohn Marino 
553*cf7f2e2dSJohn Marino static int
554*cf7f2e2dSJohn Marino addrs_section_compar (const void *ap, const void *bp)
555*cf7f2e2dSJohn Marino {
556*cf7f2e2dSJohn Marino   const struct other_sections *a = *((struct other_sections **) ap);
557*cf7f2e2dSJohn Marino   const struct other_sections *b = *((struct other_sections **) bp);
558*cf7f2e2dSJohn Marino   int retval, a_idx, b_idx;
559*cf7f2e2dSJohn Marino 
560*cf7f2e2dSJohn Marino   retval = strcmp (a->name, b->name);
561*cf7f2e2dSJohn Marino   if (retval)
562*cf7f2e2dSJohn Marino     return retval;
563*cf7f2e2dSJohn Marino 
564*cf7f2e2dSJohn Marino   /* SECTINDEX is undefined iff ADDR is zero.  */
565*cf7f2e2dSJohn Marino   a_idx = a->addr == 0 ? 0 : a->sectindex;
566*cf7f2e2dSJohn Marino   b_idx = b->addr == 0 ? 0 : b->sectindex;
567*cf7f2e2dSJohn Marino   return a_idx - b_idx;
568*cf7f2e2dSJohn Marino }
569*cf7f2e2dSJohn Marino 
570*cf7f2e2dSJohn Marino /* Provide sorted array of pointers to sections of ADDRS.  The array is
571*cf7f2e2dSJohn Marino    terminated by NULL.  Caller is responsible to call xfree for it.  */
572*cf7f2e2dSJohn Marino 
573*cf7f2e2dSJohn Marino static struct other_sections **
574*cf7f2e2dSJohn Marino addrs_section_sort (struct section_addr_info *addrs)
575*cf7f2e2dSJohn Marino {
576*cf7f2e2dSJohn Marino   struct other_sections **array;
577*cf7f2e2dSJohn Marino   int i;
578*cf7f2e2dSJohn Marino 
579*cf7f2e2dSJohn Marino   /* `+ 1' for the NULL terminator.  */
580*cf7f2e2dSJohn Marino   array = xmalloc (sizeof (*array) * (addrs->num_sections + 1));
581*cf7f2e2dSJohn Marino   for (i = 0; i < addrs->num_sections && addrs->other[i].name; i++)
582*cf7f2e2dSJohn Marino     array[i] = &addrs->other[i];
583*cf7f2e2dSJohn Marino   array[i] = NULL;
584*cf7f2e2dSJohn Marino 
585*cf7f2e2dSJohn Marino   qsort (array, i, sizeof (*array), addrs_section_compar);
586*cf7f2e2dSJohn Marino 
587*cf7f2e2dSJohn Marino   return array;
588*cf7f2e2dSJohn Marino }
589*cf7f2e2dSJohn Marino 
590*cf7f2e2dSJohn Marino /* Relativize absolute addresses in ADDRS into offsets based on ABFD.  Fill-in
591*cf7f2e2dSJohn Marino    also SECTINDEXes specific to ABFD there.  This function can be used to
592*cf7f2e2dSJohn Marino    rebase ADDRS to start referencing different BFD than before.  */
593*cf7f2e2dSJohn Marino 
594*cf7f2e2dSJohn Marino void
595*cf7f2e2dSJohn Marino addr_info_make_relative (struct section_addr_info *addrs, bfd *abfd)
596*cf7f2e2dSJohn Marino {
597*cf7f2e2dSJohn Marino   asection *lower_sect;
598*cf7f2e2dSJohn Marino   CORE_ADDR lower_offset;
599*cf7f2e2dSJohn Marino   int i;
600*cf7f2e2dSJohn Marino   struct cleanup *my_cleanup;
601*cf7f2e2dSJohn Marino   struct section_addr_info *abfd_addrs;
602*cf7f2e2dSJohn Marino   struct other_sections **addrs_sorted, **abfd_addrs_sorted;
603*cf7f2e2dSJohn Marino   struct other_sections **addrs_to_abfd_addrs;
604*cf7f2e2dSJohn Marino 
605*cf7f2e2dSJohn Marino   /* Find lowest loadable section to be used as starting point for
606*cf7f2e2dSJohn Marino      continguous sections.  */
607*cf7f2e2dSJohn Marino   lower_sect = NULL;
608*cf7f2e2dSJohn Marino   bfd_map_over_sections (abfd, find_lowest_section, &lower_sect);
609*cf7f2e2dSJohn Marino   if (lower_sect == NULL)
610*cf7f2e2dSJohn Marino     {
611*cf7f2e2dSJohn Marino       warning (_("no loadable sections found in added symbol-file %s"),
612*cf7f2e2dSJohn Marino 	       bfd_get_filename (abfd));
613*cf7f2e2dSJohn Marino       lower_offset = 0;
614*cf7f2e2dSJohn Marino     }
615*cf7f2e2dSJohn Marino   else
616*cf7f2e2dSJohn Marino     lower_offset = bfd_section_vma (bfd_get_filename (abfd), lower_sect);
617*cf7f2e2dSJohn Marino 
618*cf7f2e2dSJohn Marino   /* Create ADDRS_TO_ABFD_ADDRS array to map the sections in ADDRS to sections
619*cf7f2e2dSJohn Marino      in ABFD.  Section names are not unique - there can be multiple sections of
620*cf7f2e2dSJohn Marino      the same name.  Also the sections of the same name do not have to be
621*cf7f2e2dSJohn Marino      adjacent to each other.  Some sections may be present only in one of the
622*cf7f2e2dSJohn Marino      files.  Even sections present in both files do not have to be in the same
623*cf7f2e2dSJohn Marino      order.
624*cf7f2e2dSJohn Marino 
625*cf7f2e2dSJohn Marino      Use stable sort by name for the sections in both files.  Then linearly
626*cf7f2e2dSJohn Marino      scan both lists matching as most of the entries as possible.  */
627*cf7f2e2dSJohn Marino 
628*cf7f2e2dSJohn Marino   addrs_sorted = addrs_section_sort (addrs);
629*cf7f2e2dSJohn Marino   my_cleanup = make_cleanup (xfree, addrs_sorted);
630*cf7f2e2dSJohn Marino 
631*cf7f2e2dSJohn Marino   abfd_addrs = build_section_addr_info_from_bfd (abfd);
632*cf7f2e2dSJohn Marino   make_cleanup_free_section_addr_info (abfd_addrs);
633*cf7f2e2dSJohn Marino   abfd_addrs_sorted = addrs_section_sort (abfd_addrs);
634*cf7f2e2dSJohn Marino   make_cleanup (xfree, abfd_addrs_sorted);
635*cf7f2e2dSJohn Marino 
636*cf7f2e2dSJohn Marino   /* Now create ADDRS_TO_ABFD_ADDRS from ADDRS_SORTED and ABFD_ADDRS_SORTED.  */
637*cf7f2e2dSJohn Marino 
638*cf7f2e2dSJohn Marino   addrs_to_abfd_addrs = xzalloc (sizeof (*addrs_to_abfd_addrs)
639*cf7f2e2dSJohn Marino 				 * addrs->num_sections);
640*cf7f2e2dSJohn Marino   make_cleanup (xfree, addrs_to_abfd_addrs);
641*cf7f2e2dSJohn Marino 
642*cf7f2e2dSJohn Marino   while (*addrs_sorted)
643*cf7f2e2dSJohn Marino     {
644*cf7f2e2dSJohn Marino       const char *sect_name = (*addrs_sorted)->name;
645*cf7f2e2dSJohn Marino 
646*cf7f2e2dSJohn Marino       while (*abfd_addrs_sorted
647*cf7f2e2dSJohn Marino 	     && strcmp ((*abfd_addrs_sorted)->name, sect_name) < 0)
648*cf7f2e2dSJohn Marino 	abfd_addrs_sorted++;
649*cf7f2e2dSJohn Marino 
650*cf7f2e2dSJohn Marino       if (*abfd_addrs_sorted
651*cf7f2e2dSJohn Marino 	  && strcmp ((*abfd_addrs_sorted)->name, sect_name) == 0)
652*cf7f2e2dSJohn Marino 	{
653*cf7f2e2dSJohn Marino 	  int index_in_addrs;
654*cf7f2e2dSJohn Marino 
655*cf7f2e2dSJohn Marino 	  /* Make the found item directly addressable from ADDRS.  */
656*cf7f2e2dSJohn Marino 	  index_in_addrs = *addrs_sorted - addrs->other;
657*cf7f2e2dSJohn Marino 	  gdb_assert (addrs_to_abfd_addrs[index_in_addrs] == NULL);
658*cf7f2e2dSJohn Marino 	  addrs_to_abfd_addrs[index_in_addrs] = *abfd_addrs_sorted;
659*cf7f2e2dSJohn Marino 
660*cf7f2e2dSJohn Marino 	  /* Never use the same ABFD entry twice.  */
661*cf7f2e2dSJohn Marino 	  abfd_addrs_sorted++;
662*cf7f2e2dSJohn Marino 	}
663*cf7f2e2dSJohn Marino 
664*cf7f2e2dSJohn Marino       addrs_sorted++;
665*cf7f2e2dSJohn Marino     }
666*cf7f2e2dSJohn Marino 
667*cf7f2e2dSJohn Marino   /* Calculate offsets for the loadable sections.
668*cf7f2e2dSJohn Marino      FIXME! Sections must be in order of increasing loadable section
669*cf7f2e2dSJohn Marino      so that contiguous sections can use the lower-offset!!!
670*cf7f2e2dSJohn Marino 
671*cf7f2e2dSJohn Marino      Adjust offsets if the segments are not contiguous.
672*cf7f2e2dSJohn Marino      If the section is contiguous, its offset should be set to
673*cf7f2e2dSJohn Marino      the offset of the highest loadable section lower than it
674*cf7f2e2dSJohn Marino      (the loadable section directly below it in memory).
675*cf7f2e2dSJohn Marino      this_offset = lower_offset = lower_addr - lower_orig_addr */
676*cf7f2e2dSJohn Marino 
677*cf7f2e2dSJohn Marino   for (i = 0; i < addrs->num_sections && addrs->other[i].name; i++)
678*cf7f2e2dSJohn Marino     {
679*cf7f2e2dSJohn Marino       const char *sect_name = addrs->other[i].name;
680*cf7f2e2dSJohn Marino       struct other_sections *sect = addrs_to_abfd_addrs[i];
681*cf7f2e2dSJohn Marino 
682*cf7f2e2dSJohn Marino       if (sect)
683*cf7f2e2dSJohn Marino 	{
684*cf7f2e2dSJohn Marino 	  /* This is the index used by BFD. */
685*cf7f2e2dSJohn Marino 	  addrs->other[i].sectindex = sect->sectindex;
686*cf7f2e2dSJohn Marino 
687*cf7f2e2dSJohn Marino 	  if (addrs->other[i].addr != 0)
688*cf7f2e2dSJohn Marino 	    {
689*cf7f2e2dSJohn Marino 	      addrs->other[i].addr -= sect->addr;
690*cf7f2e2dSJohn Marino 	      lower_offset = addrs->other[i].addr;
691*cf7f2e2dSJohn Marino 	    }
692*cf7f2e2dSJohn Marino 	  else
693*cf7f2e2dSJohn Marino 	    addrs->other[i].addr = lower_offset;
694*cf7f2e2dSJohn Marino 	}
695*cf7f2e2dSJohn Marino       else
696*cf7f2e2dSJohn Marino 	{
697*cf7f2e2dSJohn Marino 	  /* This section does not exist in ABFD, which is normally
698*cf7f2e2dSJohn Marino 	     unexpected and we want to issue a warning.
699*cf7f2e2dSJohn Marino 
700*cf7f2e2dSJohn Marino 	     However, the ELF prelinker does create a few sections which are
701*cf7f2e2dSJohn Marino 	     marked in the main executable as loadable (they are loaded in
702*cf7f2e2dSJohn Marino 	     memory from the DYNAMIC segment) and yet are not present in
703*cf7f2e2dSJohn Marino 	     separate debug info files.  This is fine, and should not cause
704*cf7f2e2dSJohn Marino 	     a warning.  Shared libraries contain just the section
705*cf7f2e2dSJohn Marino 	     ".gnu.liblist" but it is not marked as loadable there.  There is
706*cf7f2e2dSJohn Marino 	     no other way to identify them than by their name as the sections
707*cf7f2e2dSJohn Marino 	     created by prelink have no special flags.  */
708*cf7f2e2dSJohn Marino 
709*cf7f2e2dSJohn Marino 	  if (!(strcmp (sect_name, ".gnu.liblist") == 0
710*cf7f2e2dSJohn Marino 		|| strcmp (sect_name, ".gnu.conflict") == 0
711*cf7f2e2dSJohn Marino 		|| strcmp (sect_name, ".dynbss") == 0
712*cf7f2e2dSJohn Marino 		|| strcmp (sect_name, ".sdynbss") == 0))
713*cf7f2e2dSJohn Marino 	    warning (_("section %s not found in %s"), sect_name,
714*cf7f2e2dSJohn Marino 		     bfd_get_filename (abfd));
715*cf7f2e2dSJohn Marino 
716*cf7f2e2dSJohn Marino 	  addrs->other[i].addr = 0;
717*cf7f2e2dSJohn Marino 
718*cf7f2e2dSJohn Marino 	  /* SECTINDEX is invalid if ADDR is zero.  */
719*cf7f2e2dSJohn Marino 	}
720*cf7f2e2dSJohn Marino     }
721*cf7f2e2dSJohn Marino 
722*cf7f2e2dSJohn Marino   do_cleanups (my_cleanup);
723*cf7f2e2dSJohn Marino }
724*cf7f2e2dSJohn Marino 
725*cf7f2e2dSJohn Marino /* Parse the user's idea of an offset for dynamic linking, into our idea
726*cf7f2e2dSJohn Marino    of how to represent it for fast symbol reading.  This is the default
727*cf7f2e2dSJohn Marino    version of the sym_fns.sym_offsets function for symbol readers that
728*cf7f2e2dSJohn Marino    don't need to do anything special.  It allocates a section_offsets table
729*cf7f2e2dSJohn Marino    for the objectfile OBJFILE and stuffs ADDR into all of the offsets.  */
730*cf7f2e2dSJohn Marino 
731*cf7f2e2dSJohn Marino void
732*cf7f2e2dSJohn Marino default_symfile_offsets (struct objfile *objfile,
733*cf7f2e2dSJohn Marino 			 struct section_addr_info *addrs)
734*cf7f2e2dSJohn Marino {
735*cf7f2e2dSJohn Marino   objfile->num_sections = bfd_count_sections (objfile->obfd);
736*cf7f2e2dSJohn Marino   objfile->section_offsets = (struct section_offsets *)
737*cf7f2e2dSJohn Marino     obstack_alloc (&objfile->objfile_obstack,
738*cf7f2e2dSJohn Marino 		   SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
739*cf7f2e2dSJohn Marino   relative_addr_info_to_section_offsets (objfile->section_offsets,
740*cf7f2e2dSJohn Marino 					 objfile->num_sections, addrs);
7415796c8dcSSimon Schubert 
7425796c8dcSSimon Schubert   /* For relocatable files, all loadable sections will start at zero.
7435796c8dcSSimon Schubert      The zero is meaningless, so try to pick arbitrary addresses such
7445796c8dcSSimon Schubert      that no loadable sections overlap.  This algorithm is quadratic,
7455796c8dcSSimon Schubert      but the number of sections in a single object file is generally
7465796c8dcSSimon Schubert      small.  */
7475796c8dcSSimon Schubert   if ((bfd_get_file_flags (objfile->obfd) & (EXEC_P | DYNAMIC)) == 0)
7485796c8dcSSimon Schubert     {
7495796c8dcSSimon Schubert       struct place_section_arg arg;
7505796c8dcSSimon Schubert       bfd *abfd = objfile->obfd;
7515796c8dcSSimon Schubert       asection *cur_sec;
7525796c8dcSSimon Schubert 
7535796c8dcSSimon Schubert       for (cur_sec = abfd->sections; cur_sec != NULL; cur_sec = cur_sec->next)
7545796c8dcSSimon Schubert 	/* We do not expect this to happen; just skip this step if the
7555796c8dcSSimon Schubert 	   relocatable file has a section with an assigned VMA.  */
7565796c8dcSSimon Schubert 	if (bfd_section_vma (abfd, cur_sec) != 0)
7575796c8dcSSimon Schubert 	  break;
7585796c8dcSSimon Schubert 
7595796c8dcSSimon Schubert       if (cur_sec == NULL)
7605796c8dcSSimon Schubert 	{
7615796c8dcSSimon Schubert 	  CORE_ADDR *offsets = objfile->section_offsets->offsets;
7625796c8dcSSimon Schubert 
7635796c8dcSSimon Schubert 	  /* Pick non-overlapping offsets for sections the user did not
7645796c8dcSSimon Schubert 	     place explicitly.  */
7655796c8dcSSimon Schubert 	  arg.offsets = objfile->section_offsets;
7665796c8dcSSimon Schubert 	  arg.lowest = 0;
7675796c8dcSSimon Schubert 	  bfd_map_over_sections (objfile->obfd, place_section, &arg);
7685796c8dcSSimon Schubert 
7695796c8dcSSimon Schubert 	  /* Correctly filling in the section offsets is not quite
7705796c8dcSSimon Schubert 	     enough.  Relocatable files have two properties that
7715796c8dcSSimon Schubert 	     (most) shared objects do not:
7725796c8dcSSimon Schubert 
7735796c8dcSSimon Schubert 	     - Their debug information will contain relocations.  Some
7745796c8dcSSimon Schubert 	     shared libraries do also, but many do not, so this can not
7755796c8dcSSimon Schubert 	     be assumed.
7765796c8dcSSimon Schubert 
7775796c8dcSSimon Schubert 	     - If there are multiple code sections they will be loaded
7785796c8dcSSimon Schubert 	     at different relative addresses in memory than they are
7795796c8dcSSimon Schubert 	     in the objfile, since all sections in the file will start
7805796c8dcSSimon Schubert 	     at address zero.
7815796c8dcSSimon Schubert 
7825796c8dcSSimon Schubert 	     Because GDB has very limited ability to map from an
7835796c8dcSSimon Schubert 	     address in debug info to the correct code section,
7845796c8dcSSimon Schubert 	     it relies on adding SECT_OFF_TEXT to things which might be
7855796c8dcSSimon Schubert 	     code.  If we clear all the section offsets, and set the
7865796c8dcSSimon Schubert 	     section VMAs instead, then symfile_relocate_debug_section
7875796c8dcSSimon Schubert 	     will return meaningful debug information pointing at the
7885796c8dcSSimon Schubert 	     correct sections.
7895796c8dcSSimon Schubert 
7905796c8dcSSimon Schubert 	     GDB has too many different data structures for section
7915796c8dcSSimon Schubert 	     addresses - a bfd, objfile, and so_list all have section
7925796c8dcSSimon Schubert 	     tables, as does exec_ops.  Some of these could probably
7935796c8dcSSimon Schubert 	     be eliminated.  */
7945796c8dcSSimon Schubert 
7955796c8dcSSimon Schubert 	  for (cur_sec = abfd->sections; cur_sec != NULL;
7965796c8dcSSimon Schubert 	       cur_sec = cur_sec->next)
7975796c8dcSSimon Schubert 	    {
7985796c8dcSSimon Schubert 	      if ((bfd_get_section_flags (abfd, cur_sec) & SEC_ALLOC) == 0)
7995796c8dcSSimon Schubert 		continue;
8005796c8dcSSimon Schubert 
8015796c8dcSSimon Schubert 	      bfd_set_section_vma (abfd, cur_sec, offsets[cur_sec->index]);
8025796c8dcSSimon Schubert 	      exec_set_section_address (bfd_get_filename (abfd), cur_sec->index,
8035796c8dcSSimon Schubert 					offsets[cur_sec->index]);
8045796c8dcSSimon Schubert 	      offsets[cur_sec->index] = 0;
8055796c8dcSSimon Schubert 	    }
8065796c8dcSSimon Schubert 	}
8075796c8dcSSimon Schubert     }
8085796c8dcSSimon Schubert 
8095796c8dcSSimon Schubert   /* Remember the bfd indexes for the .text, .data, .bss and
8105796c8dcSSimon Schubert      .rodata sections. */
8115796c8dcSSimon Schubert   init_objfile_sect_indices (objfile);
8125796c8dcSSimon Schubert }
8135796c8dcSSimon Schubert 
8145796c8dcSSimon Schubert 
8155796c8dcSSimon Schubert /* Divide the file into segments, which are individual relocatable units.
8165796c8dcSSimon Schubert    This is the default version of the sym_fns.sym_segments function for
8175796c8dcSSimon Schubert    symbol readers that do not have an explicit representation of segments.
8185796c8dcSSimon Schubert    It assumes that object files do not have segments, and fully linked
8195796c8dcSSimon Schubert    files have a single segment.  */
8205796c8dcSSimon Schubert 
8215796c8dcSSimon Schubert struct symfile_segment_data *
8225796c8dcSSimon Schubert default_symfile_segments (bfd *abfd)
8235796c8dcSSimon Schubert {
8245796c8dcSSimon Schubert   int num_sections, i;
8255796c8dcSSimon Schubert   asection *sect;
8265796c8dcSSimon Schubert   struct symfile_segment_data *data;
8275796c8dcSSimon Schubert   CORE_ADDR low, high;
8285796c8dcSSimon Schubert 
8295796c8dcSSimon Schubert   /* Relocatable files contain enough information to position each
8305796c8dcSSimon Schubert      loadable section independently; they should not be relocated
8315796c8dcSSimon Schubert      in segments.  */
8325796c8dcSSimon Schubert   if ((bfd_get_file_flags (abfd) & (EXEC_P | DYNAMIC)) == 0)
8335796c8dcSSimon Schubert     return NULL;
8345796c8dcSSimon Schubert 
8355796c8dcSSimon Schubert   /* Make sure there is at least one loadable section in the file.  */
8365796c8dcSSimon Schubert   for (sect = abfd->sections; sect != NULL; sect = sect->next)
8375796c8dcSSimon Schubert     {
8385796c8dcSSimon Schubert       if ((bfd_get_section_flags (abfd, sect) & SEC_ALLOC) == 0)
8395796c8dcSSimon Schubert 	continue;
8405796c8dcSSimon Schubert 
8415796c8dcSSimon Schubert       break;
8425796c8dcSSimon Schubert     }
8435796c8dcSSimon Schubert   if (sect == NULL)
8445796c8dcSSimon Schubert     return NULL;
8455796c8dcSSimon Schubert 
8465796c8dcSSimon Schubert   low = bfd_get_section_vma (abfd, sect);
8475796c8dcSSimon Schubert   high = low + bfd_get_section_size (sect);
8485796c8dcSSimon Schubert 
8495796c8dcSSimon Schubert   data = XZALLOC (struct symfile_segment_data);
8505796c8dcSSimon Schubert   data->num_segments = 1;
8515796c8dcSSimon Schubert   data->segment_bases = XCALLOC (1, CORE_ADDR);
8525796c8dcSSimon Schubert   data->segment_sizes = XCALLOC (1, CORE_ADDR);
8535796c8dcSSimon Schubert 
8545796c8dcSSimon Schubert   num_sections = bfd_count_sections (abfd);
8555796c8dcSSimon Schubert   data->segment_info = XCALLOC (num_sections, int);
8565796c8dcSSimon Schubert 
8575796c8dcSSimon Schubert   for (i = 0, sect = abfd->sections; sect != NULL; i++, sect = sect->next)
8585796c8dcSSimon Schubert     {
8595796c8dcSSimon Schubert       CORE_ADDR vma;
8605796c8dcSSimon Schubert 
8615796c8dcSSimon Schubert       if ((bfd_get_section_flags (abfd, sect) & SEC_ALLOC) == 0)
8625796c8dcSSimon Schubert 	continue;
8635796c8dcSSimon Schubert 
8645796c8dcSSimon Schubert       vma = bfd_get_section_vma (abfd, sect);
8655796c8dcSSimon Schubert       if (vma < low)
8665796c8dcSSimon Schubert 	low = vma;
8675796c8dcSSimon Schubert       if (vma + bfd_get_section_size (sect) > high)
8685796c8dcSSimon Schubert 	high = vma + bfd_get_section_size (sect);
8695796c8dcSSimon Schubert 
8705796c8dcSSimon Schubert       data->segment_info[i] = 1;
8715796c8dcSSimon Schubert     }
8725796c8dcSSimon Schubert 
8735796c8dcSSimon Schubert   data->segment_bases[0] = low;
8745796c8dcSSimon Schubert   data->segment_sizes[0] = high - low;
8755796c8dcSSimon Schubert 
8765796c8dcSSimon Schubert   return data;
8775796c8dcSSimon Schubert }
8785796c8dcSSimon Schubert 
8795796c8dcSSimon Schubert /* Process a symbol file, as either the main file or as a dynamically
8805796c8dcSSimon Schubert    loaded file.
8815796c8dcSSimon Schubert 
8825796c8dcSSimon Schubert    OBJFILE is where the symbols are to be read from.
8835796c8dcSSimon Schubert 
8845796c8dcSSimon Schubert    ADDRS is the list of section load addresses.  If the user has given
8855796c8dcSSimon Schubert    an 'add-symbol-file' command, then this is the list of offsets and
8865796c8dcSSimon Schubert    addresses he or she provided as arguments to the command; or, if
8875796c8dcSSimon Schubert    we're handling a shared library, these are the actual addresses the
8885796c8dcSSimon Schubert    sections are loaded at, according to the inferior's dynamic linker
8895796c8dcSSimon Schubert    (as gleaned by GDB's shared library code).  We convert each address
8905796c8dcSSimon Schubert    into an offset from the section VMA's as it appears in the object
8915796c8dcSSimon Schubert    file, and then call the file's sym_offsets function to convert this
8925796c8dcSSimon Schubert    into a format-specific offset table --- a `struct section_offsets'.
8935796c8dcSSimon Schubert    If ADDRS is non-zero, OFFSETS must be zero.
8945796c8dcSSimon Schubert 
8955796c8dcSSimon Schubert    OFFSETS is a table of section offsets already in the right
8965796c8dcSSimon Schubert    format-specific representation.  NUM_OFFSETS is the number of
8975796c8dcSSimon Schubert    elements present in OFFSETS->offsets.  If OFFSETS is non-zero, we
8985796c8dcSSimon Schubert    assume this is the proper table the call to sym_offsets described
8995796c8dcSSimon Schubert    above would produce.  Instead of calling sym_offsets, we just dump
9005796c8dcSSimon Schubert    it right into objfile->section_offsets.  (When we're re-reading
9015796c8dcSSimon Schubert    symbols from an objfile, we don't have the original load address
9025796c8dcSSimon Schubert    list any more; all we have is the section offset table.)  If
9035796c8dcSSimon Schubert    OFFSETS is non-zero, ADDRS must be zero.
9045796c8dcSSimon Schubert 
9055796c8dcSSimon Schubert    ADD_FLAGS encodes verbosity level, whether this is main symbol or
9065796c8dcSSimon Schubert    an extra symbol file such as dynamically loaded code, and wether
9075796c8dcSSimon Schubert    breakpoint reset should be deferred.  */
9085796c8dcSSimon Schubert 
9095796c8dcSSimon Schubert void
9105796c8dcSSimon Schubert syms_from_objfile (struct objfile *objfile,
9115796c8dcSSimon Schubert                    struct section_addr_info *addrs,
9125796c8dcSSimon Schubert                    struct section_offsets *offsets,
9135796c8dcSSimon Schubert                    int num_offsets,
9145796c8dcSSimon Schubert 		   int add_flags)
9155796c8dcSSimon Schubert {
9165796c8dcSSimon Schubert   struct section_addr_info *local_addr = NULL;
9175796c8dcSSimon Schubert   struct cleanup *old_chain;
9185796c8dcSSimon Schubert   const int mainline = add_flags & SYMFILE_MAINLINE;
9195796c8dcSSimon Schubert 
9205796c8dcSSimon Schubert   gdb_assert (! (addrs && offsets));
9215796c8dcSSimon Schubert 
9225796c8dcSSimon Schubert   init_entry_point_info (objfile);
9235796c8dcSSimon Schubert   objfile->sf = find_sym_fns (objfile->obfd);
9245796c8dcSSimon Schubert 
9255796c8dcSSimon Schubert   if (objfile->sf == NULL)
9265796c8dcSSimon Schubert     return;	/* No symbols. */
9275796c8dcSSimon Schubert 
9285796c8dcSSimon Schubert   /* Make sure that partially constructed symbol tables will be cleaned up
9295796c8dcSSimon Schubert      if an error occurs during symbol reading.  */
9305796c8dcSSimon Schubert   old_chain = make_cleanup_free_objfile (objfile);
9315796c8dcSSimon Schubert 
9325796c8dcSSimon Schubert   /* If ADDRS and OFFSETS are both NULL, put together a dummy address
9335796c8dcSSimon Schubert      list.  We now establish the convention that an addr of zero means
9345796c8dcSSimon Schubert      no load address was specified. */
9355796c8dcSSimon Schubert   if (! addrs && ! offsets)
9365796c8dcSSimon Schubert     {
9375796c8dcSSimon Schubert       local_addr
9385796c8dcSSimon Schubert 	= alloc_section_addr_info (bfd_count_sections (objfile->obfd));
9395796c8dcSSimon Schubert       make_cleanup (xfree, local_addr);
9405796c8dcSSimon Schubert       addrs = local_addr;
9415796c8dcSSimon Schubert     }
9425796c8dcSSimon Schubert 
9435796c8dcSSimon Schubert   /* Now either addrs or offsets is non-zero.  */
9445796c8dcSSimon Schubert 
9455796c8dcSSimon Schubert   if (mainline)
9465796c8dcSSimon Schubert     {
9475796c8dcSSimon Schubert       /* We will modify the main symbol table, make sure that all its users
9485796c8dcSSimon Schubert          will be cleaned up if an error occurs during symbol reading.  */
9495796c8dcSSimon Schubert       make_cleanup (clear_symtab_users_cleanup, 0 /*ignore*/);
9505796c8dcSSimon Schubert 
9515796c8dcSSimon Schubert       /* Since no error yet, throw away the old symbol table.  */
9525796c8dcSSimon Schubert 
9535796c8dcSSimon Schubert       if (symfile_objfile != NULL)
9545796c8dcSSimon Schubert 	{
9555796c8dcSSimon Schubert 	  free_objfile (symfile_objfile);
9565796c8dcSSimon Schubert 	  gdb_assert (symfile_objfile == NULL);
9575796c8dcSSimon Schubert 	}
9585796c8dcSSimon Schubert 
9595796c8dcSSimon Schubert       /* Currently we keep symbols from the add-symbol-file command.
9605796c8dcSSimon Schubert          If the user wants to get rid of them, they should do "symbol-file"
9615796c8dcSSimon Schubert          without arguments first.  Not sure this is the best behavior
9625796c8dcSSimon Schubert          (PR 2207).  */
9635796c8dcSSimon Schubert 
9645796c8dcSSimon Schubert       (*objfile->sf->sym_new_init) (objfile);
9655796c8dcSSimon Schubert     }
9665796c8dcSSimon Schubert 
9675796c8dcSSimon Schubert   /* Convert addr into an offset rather than an absolute address.
9685796c8dcSSimon Schubert      We find the lowest address of a loaded segment in the objfile,
9695796c8dcSSimon Schubert      and assume that <addr> is where that got loaded.
9705796c8dcSSimon Schubert 
9715796c8dcSSimon Schubert      We no longer warn if the lowest section is not a text segment (as
9725796c8dcSSimon Schubert      happens for the PA64 port.  */
973*cf7f2e2dSJohn Marino   if (addrs && addrs->other[0].name)
974*cf7f2e2dSJohn Marino     addr_info_make_relative (addrs, objfile->obfd);
9755796c8dcSSimon Schubert 
9765796c8dcSSimon Schubert   /* Initialize symbol reading routines for this objfile, allow complaints to
9775796c8dcSSimon Schubert      appear for this new file, and record how verbose to be, then do the
9785796c8dcSSimon Schubert      initial symbol reading for this file. */
9795796c8dcSSimon Schubert 
9805796c8dcSSimon Schubert   (*objfile->sf->sym_init) (objfile);
9815796c8dcSSimon Schubert   clear_complaints (&symfile_complaints, 1, add_flags & SYMFILE_VERBOSE);
9825796c8dcSSimon Schubert 
9835796c8dcSSimon Schubert   if (addrs)
9845796c8dcSSimon Schubert     (*objfile->sf->sym_offsets) (objfile, addrs);
9855796c8dcSSimon Schubert   else
9865796c8dcSSimon Schubert     {
9875796c8dcSSimon Schubert       size_t size = SIZEOF_N_SECTION_OFFSETS (num_offsets);
9885796c8dcSSimon Schubert 
9895796c8dcSSimon Schubert       /* Just copy in the offset table directly as given to us.  */
9905796c8dcSSimon Schubert       objfile->num_sections = num_offsets;
9915796c8dcSSimon Schubert       objfile->section_offsets
9925796c8dcSSimon Schubert         = ((struct section_offsets *)
9935796c8dcSSimon Schubert            obstack_alloc (&objfile->objfile_obstack, size));
9945796c8dcSSimon Schubert       memcpy (objfile->section_offsets, offsets, size);
9955796c8dcSSimon Schubert 
9965796c8dcSSimon Schubert       init_objfile_sect_indices (objfile);
9975796c8dcSSimon Schubert     }
9985796c8dcSSimon Schubert 
999*cf7f2e2dSJohn Marino   (*objfile->sf->sym_read) (objfile, add_flags);
10005796c8dcSSimon Schubert 
10015796c8dcSSimon Schubert   /* Discard cleanups as symbol reading was successful.  */
10025796c8dcSSimon Schubert 
10035796c8dcSSimon Schubert   discard_cleanups (old_chain);
10045796c8dcSSimon Schubert   xfree (local_addr);
10055796c8dcSSimon Schubert }
10065796c8dcSSimon Schubert 
10075796c8dcSSimon Schubert /* Perform required actions after either reading in the initial
10085796c8dcSSimon Schubert    symbols for a new objfile, or mapping in the symbols from a reusable
10095796c8dcSSimon Schubert    objfile. */
10105796c8dcSSimon Schubert 
10115796c8dcSSimon Schubert void
10125796c8dcSSimon Schubert new_symfile_objfile (struct objfile *objfile, int add_flags)
10135796c8dcSSimon Schubert {
10145796c8dcSSimon Schubert   /* If this is the main symbol file we have to clean up all users of the
10155796c8dcSSimon Schubert      old main symbol file. Otherwise it is sufficient to fixup all the
10165796c8dcSSimon Schubert      breakpoints that may have been redefined by this symbol file.  */
10175796c8dcSSimon Schubert   if (add_flags & SYMFILE_MAINLINE)
10185796c8dcSSimon Schubert     {
10195796c8dcSSimon Schubert       /* OK, make it the "real" symbol file.  */
10205796c8dcSSimon Schubert       symfile_objfile = objfile;
10215796c8dcSSimon Schubert 
10225796c8dcSSimon Schubert       clear_symtab_users ();
10235796c8dcSSimon Schubert     }
10245796c8dcSSimon Schubert   else if ((add_flags & SYMFILE_DEFER_BP_RESET) == 0)
10255796c8dcSSimon Schubert     {
10265796c8dcSSimon Schubert       breakpoint_re_set ();
10275796c8dcSSimon Schubert     }
10285796c8dcSSimon Schubert 
10295796c8dcSSimon Schubert   /* We're done reading the symbol file; finish off complaints.  */
10305796c8dcSSimon Schubert   clear_complaints (&symfile_complaints, 0, add_flags & SYMFILE_VERBOSE);
10315796c8dcSSimon Schubert }
10325796c8dcSSimon Schubert 
10335796c8dcSSimon Schubert /* Process a symbol file, as either the main file or as a dynamically
10345796c8dcSSimon Schubert    loaded file.
10355796c8dcSSimon Schubert 
10365796c8dcSSimon Schubert    ABFD is a BFD already open on the file, as from symfile_bfd_open.
10375796c8dcSSimon Schubert    This BFD will be closed on error, and is always consumed by this function.
10385796c8dcSSimon Schubert 
10395796c8dcSSimon Schubert    ADD_FLAGS encodes verbosity, whether this is main symbol file or
10405796c8dcSSimon Schubert    extra, such as dynamically loaded code, and what to do with breakpoins.
10415796c8dcSSimon Schubert 
10425796c8dcSSimon Schubert    ADDRS, OFFSETS, and NUM_OFFSETS are as described for
10435796c8dcSSimon Schubert    syms_from_objfile, above.
10445796c8dcSSimon Schubert    ADDRS is ignored when SYMFILE_MAINLINE bit is set in ADD_FLAGS.
10455796c8dcSSimon Schubert 
10465796c8dcSSimon Schubert    Upon success, returns a pointer to the objfile that was added.
10475796c8dcSSimon Schubert    Upon failure, jumps back to command level (never returns). */
10485796c8dcSSimon Schubert 
10495796c8dcSSimon Schubert static struct objfile *
10505796c8dcSSimon Schubert symbol_file_add_with_addrs_or_offsets (bfd *abfd,
10515796c8dcSSimon Schubert                                        int add_flags,
10525796c8dcSSimon Schubert                                        struct section_addr_info *addrs,
10535796c8dcSSimon Schubert                                        struct section_offsets *offsets,
10545796c8dcSSimon Schubert                                        int num_offsets,
10555796c8dcSSimon Schubert                                        int flags)
10565796c8dcSSimon Schubert {
10575796c8dcSSimon Schubert   struct objfile *objfile;
10585796c8dcSSimon Schubert   struct cleanup *my_cleanups;
10595796c8dcSSimon Schubert   const char *name = bfd_get_filename (abfd);
10605796c8dcSSimon Schubert   const int from_tty = add_flags & SYMFILE_VERBOSE;
10615796c8dcSSimon Schubert 
10625796c8dcSSimon Schubert   my_cleanups = make_cleanup_bfd_close (abfd);
10635796c8dcSSimon Schubert 
10645796c8dcSSimon Schubert   /* Give user a chance to burp if we'd be
10655796c8dcSSimon Schubert      interactively wiping out any existing symbols.  */
10665796c8dcSSimon Schubert 
10675796c8dcSSimon Schubert   if ((have_full_symbols () || have_partial_symbols ())
10685796c8dcSSimon Schubert       && (add_flags & SYMFILE_MAINLINE)
10695796c8dcSSimon Schubert       && from_tty
10705796c8dcSSimon Schubert       && !query (_("Load new symbol table from \"%s\"? "), name))
10715796c8dcSSimon Schubert     error (_("Not confirmed."));
10725796c8dcSSimon Schubert 
10735796c8dcSSimon Schubert   objfile = allocate_objfile (abfd, flags);
10745796c8dcSSimon Schubert   discard_cleanups (my_cleanups);
10755796c8dcSSimon Schubert 
10765796c8dcSSimon Schubert   /* We either created a new mapped symbol table, mapped an existing
10775796c8dcSSimon Schubert      symbol table file which has not had initial symbol reading
10785796c8dcSSimon Schubert      performed, or need to read an unmapped symbol table. */
10795796c8dcSSimon Schubert   if (from_tty || info_verbose)
10805796c8dcSSimon Schubert     {
10815796c8dcSSimon Schubert       if (deprecated_pre_add_symbol_hook)
10825796c8dcSSimon Schubert 	deprecated_pre_add_symbol_hook (name);
10835796c8dcSSimon Schubert       else
10845796c8dcSSimon Schubert 	{
10855796c8dcSSimon Schubert 	  printf_unfiltered (_("Reading symbols from %s..."), name);
10865796c8dcSSimon Schubert 	  wrap_here ("");
10875796c8dcSSimon Schubert 	  gdb_flush (gdb_stdout);
10885796c8dcSSimon Schubert 	}
10895796c8dcSSimon Schubert     }
10905796c8dcSSimon Schubert   syms_from_objfile (objfile, addrs, offsets, num_offsets,
10915796c8dcSSimon Schubert 		     add_flags);
10925796c8dcSSimon Schubert 
10935796c8dcSSimon Schubert   /* We now have at least a partial symbol table.  Check to see if the
10945796c8dcSSimon Schubert      user requested that all symbols be read on initial access via either
10955796c8dcSSimon Schubert      the gdb startup command line or on a per symbol file basis.  Expand
10965796c8dcSSimon Schubert      all partial symbol tables for this objfile if so. */
10975796c8dcSSimon Schubert 
10985796c8dcSSimon Schubert   if ((flags & OBJF_READNOW) || readnow_symbol_files)
10995796c8dcSSimon Schubert     {
11005796c8dcSSimon Schubert       if (from_tty || info_verbose)
11015796c8dcSSimon Schubert 	{
11025796c8dcSSimon Schubert 	  printf_unfiltered (_("expanding to full symbols..."));
11035796c8dcSSimon Schubert 	  wrap_here ("");
11045796c8dcSSimon Schubert 	  gdb_flush (gdb_stdout);
11055796c8dcSSimon Schubert 	}
11065796c8dcSSimon Schubert 
1107*cf7f2e2dSJohn Marino       if (objfile->sf)
1108*cf7f2e2dSJohn Marino 	objfile->sf->qf->expand_all_symtabs (objfile);
11095796c8dcSSimon Schubert     }
11105796c8dcSSimon Schubert 
11115796c8dcSSimon Schubert   if ((from_tty || info_verbose)
1112*cf7f2e2dSJohn Marino       && !objfile_has_symbols (objfile))
11135796c8dcSSimon Schubert     {
11145796c8dcSSimon Schubert       wrap_here ("");
11155796c8dcSSimon Schubert       printf_unfiltered (_("(no debugging symbols found)..."));
11165796c8dcSSimon Schubert       wrap_here ("");
11175796c8dcSSimon Schubert     }
11185796c8dcSSimon Schubert 
11195796c8dcSSimon Schubert   if (from_tty || info_verbose)
11205796c8dcSSimon Schubert     {
11215796c8dcSSimon Schubert       if (deprecated_post_add_symbol_hook)
11225796c8dcSSimon Schubert 	deprecated_post_add_symbol_hook ();
11235796c8dcSSimon Schubert       else
11245796c8dcSSimon Schubert 	printf_unfiltered (_("done.\n"));
11255796c8dcSSimon Schubert     }
11265796c8dcSSimon Schubert 
11275796c8dcSSimon Schubert   /* We print some messages regardless of whether 'from_tty ||
11285796c8dcSSimon Schubert      info_verbose' is true, so make sure they go out at the right
11295796c8dcSSimon Schubert      time.  */
11305796c8dcSSimon Schubert   gdb_flush (gdb_stdout);
11315796c8dcSSimon Schubert 
11325796c8dcSSimon Schubert   do_cleanups (my_cleanups);
11335796c8dcSSimon Schubert 
11345796c8dcSSimon Schubert   if (objfile->sf == NULL)
11355796c8dcSSimon Schubert     {
11365796c8dcSSimon Schubert       observer_notify_new_objfile (objfile);
11375796c8dcSSimon Schubert       return objfile;	/* No symbols. */
11385796c8dcSSimon Schubert     }
11395796c8dcSSimon Schubert 
11405796c8dcSSimon Schubert   new_symfile_objfile (objfile, add_flags);
11415796c8dcSSimon Schubert 
11425796c8dcSSimon Schubert   observer_notify_new_objfile (objfile);
11435796c8dcSSimon Schubert 
11445796c8dcSSimon Schubert   bfd_cache_close_all ();
11455796c8dcSSimon Schubert   return (objfile);
11465796c8dcSSimon Schubert }
11475796c8dcSSimon Schubert 
1148*cf7f2e2dSJohn Marino /* Add BFD as a separate debug file for OBJFILE.  */
1149*cf7f2e2dSJohn Marino 
1150*cf7f2e2dSJohn Marino void
1151*cf7f2e2dSJohn Marino symbol_file_add_separate (bfd *bfd, int symfile_flags, struct objfile *objfile)
1152*cf7f2e2dSJohn Marino {
1153*cf7f2e2dSJohn Marino   struct objfile *new_objfile;
1154*cf7f2e2dSJohn Marino   struct section_addr_info *sap;
1155*cf7f2e2dSJohn Marino   struct cleanup *my_cleanup;
1156*cf7f2e2dSJohn Marino 
1157*cf7f2e2dSJohn Marino   /* Create section_addr_info.  We can't directly use offsets from OBJFILE
1158*cf7f2e2dSJohn Marino      because sections of BFD may not match sections of OBJFILE and because
1159*cf7f2e2dSJohn Marino      vma may have been modified by tools such as prelink.  */
1160*cf7f2e2dSJohn Marino   sap = build_section_addr_info_from_objfile (objfile);
1161*cf7f2e2dSJohn Marino   my_cleanup = make_cleanup_free_section_addr_info (sap);
1162*cf7f2e2dSJohn Marino 
1163*cf7f2e2dSJohn Marino   new_objfile = symbol_file_add_with_addrs_or_offsets
1164*cf7f2e2dSJohn Marino     (bfd, symfile_flags,
1165*cf7f2e2dSJohn Marino      sap, NULL, 0,
1166*cf7f2e2dSJohn Marino      objfile->flags & (OBJF_REORDERED | OBJF_SHARED | OBJF_READNOW
1167*cf7f2e2dSJohn Marino 		       | OBJF_USERLOADED));
1168*cf7f2e2dSJohn Marino 
1169*cf7f2e2dSJohn Marino   do_cleanups (my_cleanup);
1170*cf7f2e2dSJohn Marino 
1171*cf7f2e2dSJohn Marino   add_separate_debug_objfile (new_objfile, objfile);
1172*cf7f2e2dSJohn Marino }
11735796c8dcSSimon Schubert 
11745796c8dcSSimon Schubert /* Process the symbol file ABFD, as either the main file or as a
11755796c8dcSSimon Schubert    dynamically loaded file.
11765796c8dcSSimon Schubert 
11775796c8dcSSimon Schubert    See symbol_file_add_with_addrs_or_offsets's comments for
11785796c8dcSSimon Schubert    details.  */
11795796c8dcSSimon Schubert struct objfile *
11805796c8dcSSimon Schubert symbol_file_add_from_bfd (bfd *abfd, int add_flags,
11815796c8dcSSimon Schubert                           struct section_addr_info *addrs,
11825796c8dcSSimon Schubert                           int flags)
11835796c8dcSSimon Schubert {
11845796c8dcSSimon Schubert   return symbol_file_add_with_addrs_or_offsets (abfd, add_flags, addrs, 0, 0,
11855796c8dcSSimon Schubert                                                 flags);
11865796c8dcSSimon Schubert }
11875796c8dcSSimon Schubert 
11885796c8dcSSimon Schubert 
11895796c8dcSSimon Schubert /* Process a symbol file, as either the main file or as a dynamically
11905796c8dcSSimon Schubert    loaded file.  See symbol_file_add_with_addrs_or_offsets's comments
11915796c8dcSSimon Schubert    for details.  */
11925796c8dcSSimon Schubert struct objfile *
11935796c8dcSSimon Schubert symbol_file_add (char *name, int add_flags, struct section_addr_info *addrs,
11945796c8dcSSimon Schubert 		 int flags)
11955796c8dcSSimon Schubert {
11965796c8dcSSimon Schubert   return symbol_file_add_from_bfd (symfile_bfd_open (name), add_flags, addrs,
11975796c8dcSSimon Schubert                                    flags);
11985796c8dcSSimon Schubert }
11995796c8dcSSimon Schubert 
12005796c8dcSSimon Schubert 
12015796c8dcSSimon Schubert /* Call symbol_file_add() with default values and update whatever is
12025796c8dcSSimon Schubert    affected by the loading of a new main().
12035796c8dcSSimon Schubert    Used when the file is supplied in the gdb command line
12045796c8dcSSimon Schubert    and by some targets with special loading requirements.
12055796c8dcSSimon Schubert    The auxiliary function, symbol_file_add_main_1(), has the flags
12065796c8dcSSimon Schubert    argument for the switches that can only be specified in the symbol_file
12075796c8dcSSimon Schubert    command itself.  */
12085796c8dcSSimon Schubert 
12095796c8dcSSimon Schubert void
12105796c8dcSSimon Schubert symbol_file_add_main (char *args, int from_tty)
12115796c8dcSSimon Schubert {
12125796c8dcSSimon Schubert   symbol_file_add_main_1 (args, from_tty, 0);
12135796c8dcSSimon Schubert }
12145796c8dcSSimon Schubert 
12155796c8dcSSimon Schubert static void
12165796c8dcSSimon Schubert symbol_file_add_main_1 (char *args, int from_tty, int flags)
12175796c8dcSSimon Schubert {
12185796c8dcSSimon Schubert   const int add_flags = SYMFILE_MAINLINE | (from_tty ? SYMFILE_VERBOSE : 0);
12195796c8dcSSimon Schubert   symbol_file_add (args, add_flags, NULL, flags);
12205796c8dcSSimon Schubert 
12215796c8dcSSimon Schubert   /* Getting new symbols may change our opinion about
12225796c8dcSSimon Schubert      what is frameless.  */
12235796c8dcSSimon Schubert   reinit_frame_cache ();
12245796c8dcSSimon Schubert 
12255796c8dcSSimon Schubert   set_initial_language ();
12265796c8dcSSimon Schubert }
12275796c8dcSSimon Schubert 
12285796c8dcSSimon Schubert void
12295796c8dcSSimon Schubert symbol_file_clear (int from_tty)
12305796c8dcSSimon Schubert {
12315796c8dcSSimon Schubert   if ((have_full_symbols () || have_partial_symbols ())
12325796c8dcSSimon Schubert       && from_tty
12335796c8dcSSimon Schubert       && (symfile_objfile
12345796c8dcSSimon Schubert 	  ? !query (_("Discard symbol table from `%s'? "),
12355796c8dcSSimon Schubert 		    symfile_objfile->name)
12365796c8dcSSimon Schubert 	  : !query (_("Discard symbol table? "))))
12375796c8dcSSimon Schubert     error (_("Not confirmed."));
12385796c8dcSSimon Schubert 
1239*cf7f2e2dSJohn Marino   /* solib descriptors may have handles to objfiles.  Wipe them before their
1240*cf7f2e2dSJohn Marino      objfiles get stale by free_all_objfiles.  */
12415796c8dcSSimon Schubert   no_shared_libraries (NULL, from_tty);
12425796c8dcSSimon Schubert 
1243*cf7f2e2dSJohn Marino   free_all_objfiles ();
1244*cf7f2e2dSJohn Marino 
12455796c8dcSSimon Schubert   gdb_assert (symfile_objfile == NULL);
12465796c8dcSSimon Schubert   if (from_tty)
12475796c8dcSSimon Schubert     printf_unfiltered (_("No symbol file now.\n"));
12485796c8dcSSimon Schubert }
12495796c8dcSSimon Schubert 
12505796c8dcSSimon Schubert static char *
12515796c8dcSSimon Schubert get_debug_link_info (struct objfile *objfile, unsigned long *crc32_out)
12525796c8dcSSimon Schubert {
12535796c8dcSSimon Schubert   asection *sect;
12545796c8dcSSimon Schubert   bfd_size_type debuglink_size;
12555796c8dcSSimon Schubert   unsigned long crc32;
12565796c8dcSSimon Schubert   char *contents;
12575796c8dcSSimon Schubert   int crc_offset;
12585796c8dcSSimon Schubert 
12595796c8dcSSimon Schubert   sect = bfd_get_section_by_name (objfile->obfd, ".gnu_debuglink");
12605796c8dcSSimon Schubert 
12615796c8dcSSimon Schubert   if (sect == NULL)
12625796c8dcSSimon Schubert     return NULL;
12635796c8dcSSimon Schubert 
12645796c8dcSSimon Schubert   debuglink_size = bfd_section_size (objfile->obfd, sect);
12655796c8dcSSimon Schubert 
12665796c8dcSSimon Schubert   contents = xmalloc (debuglink_size);
12675796c8dcSSimon Schubert   bfd_get_section_contents (objfile->obfd, sect, contents,
12685796c8dcSSimon Schubert 			    (file_ptr)0, (bfd_size_type)debuglink_size);
12695796c8dcSSimon Schubert 
12705796c8dcSSimon Schubert   /* Crc value is stored after the filename, aligned up to 4 bytes. */
12715796c8dcSSimon Schubert   crc_offset = strlen (contents) + 1;
12725796c8dcSSimon Schubert   crc_offset = (crc_offset + 3) & ~3;
12735796c8dcSSimon Schubert 
12745796c8dcSSimon Schubert   crc32 = bfd_get_32 (objfile->obfd, (bfd_byte *) (contents + crc_offset));
12755796c8dcSSimon Schubert 
12765796c8dcSSimon Schubert   *crc32_out = crc32;
12775796c8dcSSimon Schubert   return contents;
12785796c8dcSSimon Schubert }
12795796c8dcSSimon Schubert 
12805796c8dcSSimon Schubert static int
1281*cf7f2e2dSJohn Marino separate_debug_file_exists (const char *name, unsigned long crc,
1282*cf7f2e2dSJohn Marino 			    struct objfile *parent_objfile)
12835796c8dcSSimon Schubert {
12845796c8dcSSimon Schubert   unsigned long file_crc = 0;
12855796c8dcSSimon Schubert   bfd *abfd;
12865796c8dcSSimon Schubert   gdb_byte buffer[8*1024];
12875796c8dcSSimon Schubert   int count;
1288*cf7f2e2dSJohn Marino   struct stat parent_stat, abfd_stat;
12895796c8dcSSimon Schubert 
1290*cf7f2e2dSJohn Marino   /* Find a separate debug info file as if symbols would be present in
1291*cf7f2e2dSJohn Marino      PARENT_OBJFILE itself this function would not be called.  .gnu_debuglink
1292*cf7f2e2dSJohn Marino      section can contain just the basename of PARENT_OBJFILE without any
1293*cf7f2e2dSJohn Marino      ".debug" suffix as "/usr/lib/debug/path/to/file" is a separate tree where
1294*cf7f2e2dSJohn Marino      the separate debug infos with the same basename can exist. */
1295*cf7f2e2dSJohn Marino 
1296*cf7f2e2dSJohn Marino   if (strcmp (name, parent_objfile->name) == 0)
1297*cf7f2e2dSJohn Marino     return 0;
1298*cf7f2e2dSJohn Marino 
1299*cf7f2e2dSJohn Marino   abfd = bfd_open_maybe_remote (name);
13005796c8dcSSimon Schubert 
13015796c8dcSSimon Schubert   if (!abfd)
13025796c8dcSSimon Schubert     return 0;
13035796c8dcSSimon Schubert 
1304*cf7f2e2dSJohn Marino   /* Verify symlinks were not the cause of strcmp name difference above.
1305*cf7f2e2dSJohn Marino 
1306*cf7f2e2dSJohn Marino      Some operating systems, e.g. Windows, do not provide a meaningful
1307*cf7f2e2dSJohn Marino      st_ino; they always set it to zero.  (Windows does provide a
1308*cf7f2e2dSJohn Marino      meaningful st_dev.)  Do not indicate a duplicate library in that
1309*cf7f2e2dSJohn Marino      case.  While there is no guarantee that a system that provides
1310*cf7f2e2dSJohn Marino      meaningful inode numbers will never set st_ino to zero, this is
1311*cf7f2e2dSJohn Marino      merely an optimization, so we do not need to worry about false
1312*cf7f2e2dSJohn Marino      negatives.  */
1313*cf7f2e2dSJohn Marino 
1314*cf7f2e2dSJohn Marino   if (bfd_stat (abfd, &abfd_stat) == 0
1315*cf7f2e2dSJohn Marino       && bfd_stat (parent_objfile->obfd, &parent_stat) == 0
1316*cf7f2e2dSJohn Marino       && abfd_stat.st_dev == parent_stat.st_dev
1317*cf7f2e2dSJohn Marino       && abfd_stat.st_ino == parent_stat.st_ino
1318*cf7f2e2dSJohn Marino       && abfd_stat.st_ino != 0)
1319*cf7f2e2dSJohn Marino     {
1320*cf7f2e2dSJohn Marino       bfd_close (abfd);
1321*cf7f2e2dSJohn Marino       return 0;
1322*cf7f2e2dSJohn Marino     }
1323*cf7f2e2dSJohn Marino 
13245796c8dcSSimon Schubert   while ((count = bfd_bread (buffer, sizeof (buffer), abfd)) > 0)
13255796c8dcSSimon Schubert     file_crc = gnu_debuglink_crc32 (file_crc, buffer, count);
13265796c8dcSSimon Schubert 
13275796c8dcSSimon Schubert   bfd_close (abfd);
13285796c8dcSSimon Schubert 
1329*cf7f2e2dSJohn Marino   if (crc != file_crc)
1330*cf7f2e2dSJohn Marino     {
1331*cf7f2e2dSJohn Marino       warning (_("the debug information found in \"%s\""
1332*cf7f2e2dSJohn Marino 		 " does not match \"%s\" (CRC mismatch).\n"),
1333*cf7f2e2dSJohn Marino 	       name, parent_objfile->name);
1334*cf7f2e2dSJohn Marino       return 0;
1335*cf7f2e2dSJohn Marino     }
1336*cf7f2e2dSJohn Marino 
1337*cf7f2e2dSJohn Marino   return 1;
13385796c8dcSSimon Schubert }
13395796c8dcSSimon Schubert 
13405796c8dcSSimon Schubert char *debug_file_directory = NULL;
13415796c8dcSSimon Schubert static void
13425796c8dcSSimon Schubert show_debug_file_directory (struct ui_file *file, int from_tty,
13435796c8dcSSimon Schubert 			   struct cmd_list_element *c, const char *value)
13445796c8dcSSimon Schubert {
13455796c8dcSSimon Schubert   fprintf_filtered (file, _("\
13465796c8dcSSimon Schubert The directory where separate debug symbols are searched for is \"%s\".\n"),
13475796c8dcSSimon Schubert 		    value);
13485796c8dcSSimon Schubert }
13495796c8dcSSimon Schubert 
13505796c8dcSSimon Schubert #if ! defined (DEBUG_SUBDIRECTORY)
13515796c8dcSSimon Schubert #define DEBUG_SUBDIRECTORY ".debug"
13525796c8dcSSimon Schubert #endif
13535796c8dcSSimon Schubert 
1354*cf7f2e2dSJohn Marino char *
1355*cf7f2e2dSJohn Marino find_separate_debug_file_by_debuglink (struct objfile *objfile)
13565796c8dcSSimon Schubert {
1357*cf7f2e2dSJohn Marino   char *basename, *debugdir;
1358*cf7f2e2dSJohn Marino   char *dir = NULL;
1359*cf7f2e2dSJohn Marino   char *debugfile = NULL;
1360*cf7f2e2dSJohn Marino   char *canon_name = NULL;
13615796c8dcSSimon Schubert   unsigned long crc32;
13625796c8dcSSimon Schubert   int i;
13635796c8dcSSimon Schubert 
13645796c8dcSSimon Schubert   basename = get_debug_link_info (objfile, &crc32);
13655796c8dcSSimon Schubert 
13665796c8dcSSimon Schubert   if (basename == NULL)
1367*cf7f2e2dSJohn Marino     /* There's no separate debug info, hence there's no way we could
1368*cf7f2e2dSJohn Marino        load it => no warning.  */
1369*cf7f2e2dSJohn Marino     goto cleanup_return_debugfile;
13705796c8dcSSimon Schubert 
13715796c8dcSSimon Schubert   dir = xstrdup (objfile->name);
13725796c8dcSSimon Schubert 
13735796c8dcSSimon Schubert   /* Strip off the final filename part, leaving the directory name,
1374*cf7f2e2dSJohn Marino      followed by a slash.  The directory can be relative or absolute.  */
13755796c8dcSSimon Schubert   for (i = strlen(dir) - 1; i >= 0; i--)
13765796c8dcSSimon Schubert     {
13775796c8dcSSimon Schubert       if (IS_DIR_SEPARATOR (dir[i]))
13785796c8dcSSimon Schubert 	break;
13795796c8dcSSimon Schubert     }
1380*cf7f2e2dSJohn Marino   /* If I is -1 then no directory is present there and DIR will be "".  */
13815796c8dcSSimon Schubert   dir[i+1] = '\0';
13825796c8dcSSimon Schubert 
13835796c8dcSSimon Schubert   /* Set I to max (strlen (canon_name), strlen (dir)). */
13845796c8dcSSimon Schubert   canon_name = lrealpath (dir);
13855796c8dcSSimon Schubert   i = strlen (dir);
13865796c8dcSSimon Schubert   if (canon_name && strlen (canon_name) > i)
13875796c8dcSSimon Schubert     i = strlen (canon_name);
13885796c8dcSSimon Schubert 
1389*cf7f2e2dSJohn Marino   debugfile = xmalloc (strlen (debug_file_directory) + 1
13905796c8dcSSimon Schubert 		       + i
13915796c8dcSSimon Schubert 		       + strlen (DEBUG_SUBDIRECTORY)
13925796c8dcSSimon Schubert 		       + strlen ("/")
13935796c8dcSSimon Schubert 		       + strlen (basename)
13945796c8dcSSimon Schubert 		       + 1);
13955796c8dcSSimon Schubert 
13965796c8dcSSimon Schubert   /* First try in the same directory as the original file.  */
13975796c8dcSSimon Schubert   strcpy (debugfile, dir);
13985796c8dcSSimon Schubert   strcat (debugfile, basename);
13995796c8dcSSimon Schubert 
1400*cf7f2e2dSJohn Marino   if (separate_debug_file_exists (debugfile, crc32, objfile))
1401*cf7f2e2dSJohn Marino     goto cleanup_return_debugfile;
14025796c8dcSSimon Schubert 
14035796c8dcSSimon Schubert   /* Then try in the subdirectory named DEBUG_SUBDIRECTORY.  */
14045796c8dcSSimon Schubert   strcpy (debugfile, dir);
14055796c8dcSSimon Schubert   strcat (debugfile, DEBUG_SUBDIRECTORY);
14065796c8dcSSimon Schubert   strcat (debugfile, "/");
14075796c8dcSSimon Schubert   strcat (debugfile, basename);
14085796c8dcSSimon Schubert 
1409*cf7f2e2dSJohn Marino   if (separate_debug_file_exists (debugfile, crc32, objfile))
1410*cf7f2e2dSJohn Marino     goto cleanup_return_debugfile;
14115796c8dcSSimon Schubert 
1412*cf7f2e2dSJohn Marino   /* Then try in the global debugfile directories.
1413*cf7f2e2dSJohn Marino 
1414*cf7f2e2dSJohn Marino      Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will
1415*cf7f2e2dSJohn Marino      cause "/..." lookups.  */
1416*cf7f2e2dSJohn Marino 
1417*cf7f2e2dSJohn Marino   debugdir = debug_file_directory;
1418*cf7f2e2dSJohn Marino   do
1419*cf7f2e2dSJohn Marino     {
1420*cf7f2e2dSJohn Marino       char *debugdir_end;
1421*cf7f2e2dSJohn Marino 
1422*cf7f2e2dSJohn Marino       while (*debugdir == DIRNAME_SEPARATOR)
1423*cf7f2e2dSJohn Marino 	debugdir++;
1424*cf7f2e2dSJohn Marino 
1425*cf7f2e2dSJohn Marino       debugdir_end = strchr (debugdir, DIRNAME_SEPARATOR);
1426*cf7f2e2dSJohn Marino       if (debugdir_end == NULL)
1427*cf7f2e2dSJohn Marino 	debugdir_end = &debugdir[strlen (debugdir)];
1428*cf7f2e2dSJohn Marino 
1429*cf7f2e2dSJohn Marino       memcpy (debugfile, debugdir, debugdir_end - debugdir);
1430*cf7f2e2dSJohn Marino       debugfile[debugdir_end - debugdir] = 0;
14315796c8dcSSimon Schubert       strcat (debugfile, "/");
14325796c8dcSSimon Schubert       strcat (debugfile, dir);
14335796c8dcSSimon Schubert       strcat (debugfile, basename);
14345796c8dcSSimon Schubert 
1435*cf7f2e2dSJohn Marino       if (separate_debug_file_exists (debugfile, crc32, objfile))
1436*cf7f2e2dSJohn Marino 	goto cleanup_return_debugfile;
14375796c8dcSSimon Schubert 
14385796c8dcSSimon Schubert       /* If the file is in the sysroot, try using its base path in the
14395796c8dcSSimon Schubert 	 global debugfile directory.  */
14405796c8dcSSimon Schubert       if (canon_name
14415796c8dcSSimon Schubert 	  && strncmp (canon_name, gdb_sysroot, strlen (gdb_sysroot)) == 0
14425796c8dcSSimon Schubert 	  && IS_DIR_SEPARATOR (canon_name[strlen (gdb_sysroot)]))
14435796c8dcSSimon Schubert 	{
1444*cf7f2e2dSJohn Marino 	  memcpy (debugfile, debugdir, debugdir_end - debugdir);
1445*cf7f2e2dSJohn Marino 	  debugfile[debugdir_end - debugdir] = 0;
14465796c8dcSSimon Schubert 	  strcat (debugfile, canon_name + strlen (gdb_sysroot));
14475796c8dcSSimon Schubert 	  strcat (debugfile, "/");
14485796c8dcSSimon Schubert 	  strcat (debugfile, basename);
14495796c8dcSSimon Schubert 
1450*cf7f2e2dSJohn Marino 	  if (separate_debug_file_exists (debugfile, crc32, objfile))
1451*cf7f2e2dSJohn Marino 	    goto cleanup_return_debugfile;
1452*cf7f2e2dSJohn Marino 	}
1453*cf7f2e2dSJohn Marino 
1454*cf7f2e2dSJohn Marino       debugdir = debugdir_end;
1455*cf7f2e2dSJohn Marino     }
1456*cf7f2e2dSJohn Marino   while (*debugdir != 0);
1457*cf7f2e2dSJohn Marino 
1458*cf7f2e2dSJohn Marino   xfree (debugfile);
1459*cf7f2e2dSJohn Marino   debugfile = NULL;
1460*cf7f2e2dSJohn Marino 
1461*cf7f2e2dSJohn Marino cleanup_return_debugfile:
14625796c8dcSSimon Schubert   xfree (canon_name);
14635796c8dcSSimon Schubert   xfree (basename);
14645796c8dcSSimon Schubert   xfree (dir);
1465*cf7f2e2dSJohn Marino   return debugfile;
14665796c8dcSSimon Schubert }
14675796c8dcSSimon Schubert 
14685796c8dcSSimon Schubert 
14695796c8dcSSimon Schubert /* This is the symbol-file command.  Read the file, analyze its
14705796c8dcSSimon Schubert    symbols, and add a struct symtab to a symtab list.  The syntax of
14715796c8dcSSimon Schubert    the command is rather bizarre:
14725796c8dcSSimon Schubert 
14735796c8dcSSimon Schubert    1. The function buildargv implements various quoting conventions
14745796c8dcSSimon Schubert    which are undocumented and have little or nothing in common with
14755796c8dcSSimon Schubert    the way things are quoted (or not quoted) elsewhere in GDB.
14765796c8dcSSimon Schubert 
14775796c8dcSSimon Schubert    2. Options are used, which are not generally used in GDB (perhaps
14785796c8dcSSimon Schubert    "set mapped on", "set readnow on" would be better)
14795796c8dcSSimon Schubert 
14805796c8dcSSimon Schubert    3. The order of options matters, which is contrary to GNU
14815796c8dcSSimon Schubert    conventions (because it is confusing and inconvenient).  */
14825796c8dcSSimon Schubert 
14835796c8dcSSimon Schubert void
14845796c8dcSSimon Schubert symbol_file_command (char *args, int from_tty)
14855796c8dcSSimon Schubert {
14865796c8dcSSimon Schubert   dont_repeat ();
14875796c8dcSSimon Schubert 
14885796c8dcSSimon Schubert   if (args == NULL)
14895796c8dcSSimon Schubert     {
14905796c8dcSSimon Schubert       symbol_file_clear (from_tty);
14915796c8dcSSimon Schubert     }
14925796c8dcSSimon Schubert   else
14935796c8dcSSimon Schubert     {
14945796c8dcSSimon Schubert       char **argv = gdb_buildargv (args);
14955796c8dcSSimon Schubert       int flags = OBJF_USERLOADED;
14965796c8dcSSimon Schubert       struct cleanup *cleanups;
14975796c8dcSSimon Schubert       char *name = NULL;
14985796c8dcSSimon Schubert 
14995796c8dcSSimon Schubert       cleanups = make_cleanup_freeargv (argv);
15005796c8dcSSimon Schubert       while (*argv != NULL)
15015796c8dcSSimon Schubert 	{
15025796c8dcSSimon Schubert 	  if (strcmp (*argv, "-readnow") == 0)
15035796c8dcSSimon Schubert 	    flags |= OBJF_READNOW;
15045796c8dcSSimon Schubert 	  else if (**argv == '-')
15055796c8dcSSimon Schubert 	    error (_("unknown option `%s'"), *argv);
15065796c8dcSSimon Schubert 	  else
15075796c8dcSSimon Schubert 	    {
15085796c8dcSSimon Schubert 	      symbol_file_add_main_1 (*argv, from_tty, flags);
15095796c8dcSSimon Schubert 	      name = *argv;
15105796c8dcSSimon Schubert 	    }
15115796c8dcSSimon Schubert 
15125796c8dcSSimon Schubert 	  argv++;
15135796c8dcSSimon Schubert 	}
15145796c8dcSSimon Schubert 
15155796c8dcSSimon Schubert       if (name == NULL)
15165796c8dcSSimon Schubert 	error (_("no symbol file name was specified"));
15175796c8dcSSimon Schubert 
15185796c8dcSSimon Schubert       do_cleanups (cleanups);
15195796c8dcSSimon Schubert     }
15205796c8dcSSimon Schubert }
15215796c8dcSSimon Schubert 
15225796c8dcSSimon Schubert /* Set the initial language.
15235796c8dcSSimon Schubert 
15245796c8dcSSimon Schubert    FIXME: A better solution would be to record the language in the
15255796c8dcSSimon Schubert    psymtab when reading partial symbols, and then use it (if known) to
15265796c8dcSSimon Schubert    set the language.  This would be a win for formats that encode the
15275796c8dcSSimon Schubert    language in an easily discoverable place, such as DWARF.  For
15285796c8dcSSimon Schubert    stabs, we can jump through hoops looking for specially named
15295796c8dcSSimon Schubert    symbols or try to intuit the language from the specific type of
15305796c8dcSSimon Schubert    stabs we find, but we can't do that until later when we read in
15315796c8dcSSimon Schubert    full symbols.  */
15325796c8dcSSimon Schubert 
15335796c8dcSSimon Schubert void
15345796c8dcSSimon Schubert set_initial_language (void)
15355796c8dcSSimon Schubert {
1536*cf7f2e2dSJohn Marino   char *filename;
15375796c8dcSSimon Schubert   enum language lang = language_unknown;
15385796c8dcSSimon Schubert 
1539*cf7f2e2dSJohn Marino   filename = find_main_filename ();
1540*cf7f2e2dSJohn Marino   if (filename != NULL)
1541*cf7f2e2dSJohn Marino     lang = deduce_language_from_filename (filename);
15425796c8dcSSimon Schubert 
15435796c8dcSSimon Schubert   if (lang == language_unknown)
15445796c8dcSSimon Schubert     {
15455796c8dcSSimon Schubert       /* Make C the default language */
15465796c8dcSSimon Schubert       lang = language_c;
15475796c8dcSSimon Schubert     }
15485796c8dcSSimon Schubert 
15495796c8dcSSimon Schubert   set_language (lang);
15505796c8dcSSimon Schubert   expected_language = current_language; /* Don't warn the user.  */
15515796c8dcSSimon Schubert }
1552*cf7f2e2dSJohn Marino 
1553*cf7f2e2dSJohn Marino /* If NAME is a remote name open the file using remote protocol, otherwise
1554*cf7f2e2dSJohn Marino    open it normally.  */
1555*cf7f2e2dSJohn Marino 
1556*cf7f2e2dSJohn Marino bfd *
1557*cf7f2e2dSJohn Marino bfd_open_maybe_remote (const char *name)
1558*cf7f2e2dSJohn Marino {
1559*cf7f2e2dSJohn Marino   if (remote_filename_p (name))
1560*cf7f2e2dSJohn Marino     return remote_bfd_open (name, gnutarget);
1561*cf7f2e2dSJohn Marino   else
1562*cf7f2e2dSJohn Marino     return bfd_openr (name, gnutarget);
15635796c8dcSSimon Schubert }
15645796c8dcSSimon Schubert 
1565*cf7f2e2dSJohn Marino 
15665796c8dcSSimon Schubert /* Open the file specified by NAME and hand it off to BFD for
15675796c8dcSSimon Schubert    preliminary analysis.  Return a newly initialized bfd *, which
15685796c8dcSSimon Schubert    includes a newly malloc'd` copy of NAME (tilde-expanded and made
15695796c8dcSSimon Schubert    absolute).  In case of trouble, error() is called.  */
15705796c8dcSSimon Schubert 
15715796c8dcSSimon Schubert bfd *
15725796c8dcSSimon Schubert symfile_bfd_open (char *name)
15735796c8dcSSimon Schubert {
15745796c8dcSSimon Schubert   bfd *sym_bfd;
15755796c8dcSSimon Schubert   int desc;
15765796c8dcSSimon Schubert   char *absolute_name;
15775796c8dcSSimon Schubert 
15785796c8dcSSimon Schubert   if (remote_filename_p (name))
15795796c8dcSSimon Schubert     {
15805796c8dcSSimon Schubert       name = xstrdup (name);
15815796c8dcSSimon Schubert       sym_bfd = remote_bfd_open (name, gnutarget);
15825796c8dcSSimon Schubert       if (!sym_bfd)
15835796c8dcSSimon Schubert 	{
15845796c8dcSSimon Schubert 	  make_cleanup (xfree, name);
15855796c8dcSSimon Schubert 	  error (_("`%s': can't open to read symbols: %s."), name,
15865796c8dcSSimon Schubert 		 bfd_errmsg (bfd_get_error ()));
15875796c8dcSSimon Schubert 	}
15885796c8dcSSimon Schubert 
15895796c8dcSSimon Schubert       if (!bfd_check_format (sym_bfd, bfd_object))
15905796c8dcSSimon Schubert 	{
15915796c8dcSSimon Schubert 	  bfd_close (sym_bfd);
15925796c8dcSSimon Schubert 	  make_cleanup (xfree, name);
15935796c8dcSSimon Schubert 	  error (_("`%s': can't read symbols: %s."), name,
15945796c8dcSSimon Schubert 		 bfd_errmsg (bfd_get_error ()));
15955796c8dcSSimon Schubert 	}
15965796c8dcSSimon Schubert 
15975796c8dcSSimon Schubert       return sym_bfd;
15985796c8dcSSimon Schubert     }
15995796c8dcSSimon Schubert 
16005796c8dcSSimon Schubert   name = tilde_expand (name);	/* Returns 1st new malloc'd copy.  */
16015796c8dcSSimon Schubert 
16025796c8dcSSimon Schubert   /* Look down path for it, allocate 2nd new malloc'd copy.  */
16035796c8dcSSimon Schubert   desc = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST, name,
16045796c8dcSSimon Schubert 		O_RDONLY | O_BINARY, &absolute_name);
16055796c8dcSSimon Schubert #if defined(__GO32__) || defined(_WIN32) || defined (__CYGWIN__)
16065796c8dcSSimon Schubert   if (desc < 0)
16075796c8dcSSimon Schubert     {
16085796c8dcSSimon Schubert       char *exename = alloca (strlen (name) + 5);
1609*cf7f2e2dSJohn Marino 
16105796c8dcSSimon Schubert       strcat (strcpy (exename, name), ".exe");
16115796c8dcSSimon Schubert       desc = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST, exename,
16125796c8dcSSimon Schubert 		    O_RDONLY | O_BINARY, &absolute_name);
16135796c8dcSSimon Schubert     }
16145796c8dcSSimon Schubert #endif
16155796c8dcSSimon Schubert   if (desc < 0)
16165796c8dcSSimon Schubert     {
16175796c8dcSSimon Schubert       make_cleanup (xfree, name);
16185796c8dcSSimon Schubert       perror_with_name (name);
16195796c8dcSSimon Schubert     }
16205796c8dcSSimon Schubert 
16215796c8dcSSimon Schubert   /* Free 1st new malloc'd copy, but keep the 2nd malloc'd copy in
16225796c8dcSSimon Schubert      bfd.  It'll be freed in free_objfile(). */
16235796c8dcSSimon Schubert   xfree (name);
16245796c8dcSSimon Schubert   name = absolute_name;
16255796c8dcSSimon Schubert 
16265796c8dcSSimon Schubert   sym_bfd = bfd_fopen (name, gnutarget, FOPEN_RB, desc);
16275796c8dcSSimon Schubert   if (!sym_bfd)
16285796c8dcSSimon Schubert     {
16295796c8dcSSimon Schubert       close (desc);
16305796c8dcSSimon Schubert       make_cleanup (xfree, name);
16315796c8dcSSimon Schubert       error (_("`%s': can't open to read symbols: %s."), name,
16325796c8dcSSimon Schubert 	     bfd_errmsg (bfd_get_error ()));
16335796c8dcSSimon Schubert     }
16345796c8dcSSimon Schubert   bfd_set_cacheable (sym_bfd, 1);
16355796c8dcSSimon Schubert 
16365796c8dcSSimon Schubert   if (!bfd_check_format (sym_bfd, bfd_object))
16375796c8dcSSimon Schubert     {
16385796c8dcSSimon Schubert       /* FIXME: should be checking for errors from bfd_close (for one
16395796c8dcSSimon Schubert          thing, on error it does not free all the storage associated
16405796c8dcSSimon Schubert          with the bfd).  */
16415796c8dcSSimon Schubert       bfd_close (sym_bfd);	/* This also closes desc.  */
16425796c8dcSSimon Schubert       make_cleanup (xfree, name);
16435796c8dcSSimon Schubert       error (_("`%s': can't read symbols: %s."), name,
16445796c8dcSSimon Schubert 	     bfd_errmsg (bfd_get_error ()));
16455796c8dcSSimon Schubert     }
16465796c8dcSSimon Schubert 
16475796c8dcSSimon Schubert   /* bfd_usrdata exists for applications and libbfd must not touch it.  */
16485796c8dcSSimon Schubert   gdb_assert (bfd_usrdata (sym_bfd) == NULL);
16495796c8dcSSimon Schubert 
16505796c8dcSSimon Schubert   return sym_bfd;
16515796c8dcSSimon Schubert }
16525796c8dcSSimon Schubert 
16535796c8dcSSimon Schubert /* Return the section index for SECTION_NAME on OBJFILE.  Return -1 if
16545796c8dcSSimon Schubert    the section was not found.  */
16555796c8dcSSimon Schubert 
16565796c8dcSSimon Schubert int
16575796c8dcSSimon Schubert get_section_index (struct objfile *objfile, char *section_name)
16585796c8dcSSimon Schubert {
16595796c8dcSSimon Schubert   asection *sect = bfd_get_section_by_name (objfile->obfd, section_name);
16605796c8dcSSimon Schubert 
16615796c8dcSSimon Schubert   if (sect)
16625796c8dcSSimon Schubert     return sect->index;
16635796c8dcSSimon Schubert   else
16645796c8dcSSimon Schubert     return -1;
16655796c8dcSSimon Schubert }
16665796c8dcSSimon Schubert 
16675796c8dcSSimon Schubert /* Link SF into the global symtab_fns list.  Called on startup by the
16685796c8dcSSimon Schubert    _initialize routine in each object file format reader, to register
16695796c8dcSSimon Schubert    information about each format the the reader is prepared to
16705796c8dcSSimon Schubert    handle. */
16715796c8dcSSimon Schubert 
16725796c8dcSSimon Schubert void
16735796c8dcSSimon Schubert add_symtab_fns (struct sym_fns *sf)
16745796c8dcSSimon Schubert {
16755796c8dcSSimon Schubert   sf->next = symtab_fns;
16765796c8dcSSimon Schubert   symtab_fns = sf;
16775796c8dcSSimon Schubert }
16785796c8dcSSimon Schubert 
16795796c8dcSSimon Schubert /* Initialize OBJFILE to read symbols from its associated BFD.  It
16805796c8dcSSimon Schubert    either returns or calls error().  The result is an initialized
16815796c8dcSSimon Schubert    struct sym_fns in the objfile structure, that contains cached
16825796c8dcSSimon Schubert    information about the symbol file.  */
16835796c8dcSSimon Schubert 
16845796c8dcSSimon Schubert static struct sym_fns *
16855796c8dcSSimon Schubert find_sym_fns (bfd *abfd)
16865796c8dcSSimon Schubert {
16875796c8dcSSimon Schubert   struct sym_fns *sf;
16885796c8dcSSimon Schubert   enum bfd_flavour our_flavour = bfd_get_flavour (abfd);
16895796c8dcSSimon Schubert 
16905796c8dcSSimon Schubert   if (our_flavour == bfd_target_srec_flavour
16915796c8dcSSimon Schubert       || our_flavour == bfd_target_ihex_flavour
16925796c8dcSSimon Schubert       || our_flavour == bfd_target_tekhex_flavour)
16935796c8dcSSimon Schubert     return NULL;	/* No symbols.  */
16945796c8dcSSimon Schubert 
16955796c8dcSSimon Schubert   for (sf = symtab_fns; sf != NULL; sf = sf->next)
16965796c8dcSSimon Schubert     if (our_flavour == sf->sym_flavour)
16975796c8dcSSimon Schubert       return sf;
16985796c8dcSSimon Schubert 
16995796c8dcSSimon Schubert   error (_("I'm sorry, Dave, I can't do that.  Symbol format `%s' unknown."),
17005796c8dcSSimon Schubert 	 bfd_get_target (abfd));
17015796c8dcSSimon Schubert }
17025796c8dcSSimon Schubert 
17035796c8dcSSimon Schubert 
17045796c8dcSSimon Schubert /* This function runs the load command of our current target.  */
17055796c8dcSSimon Schubert 
17065796c8dcSSimon Schubert static void
17075796c8dcSSimon Schubert load_command (char *arg, int from_tty)
17085796c8dcSSimon Schubert {
17095796c8dcSSimon Schubert   /* The user might be reloading because the binary has changed.  Take
17105796c8dcSSimon Schubert      this opportunity to check.  */
17115796c8dcSSimon Schubert   reopen_exec_file ();
17125796c8dcSSimon Schubert   reread_symbols ();
17135796c8dcSSimon Schubert 
17145796c8dcSSimon Schubert   if (arg == NULL)
17155796c8dcSSimon Schubert     {
17165796c8dcSSimon Schubert       char *parg;
17175796c8dcSSimon Schubert       int count = 0;
17185796c8dcSSimon Schubert 
17195796c8dcSSimon Schubert       parg = arg = get_exec_file (1);
17205796c8dcSSimon Schubert 
17215796c8dcSSimon Schubert       /* Count how many \ " ' tab space there are in the name.  */
17225796c8dcSSimon Schubert       while ((parg = strpbrk (parg, "\\\"'\t ")))
17235796c8dcSSimon Schubert 	{
17245796c8dcSSimon Schubert 	  parg++;
17255796c8dcSSimon Schubert 	  count++;
17265796c8dcSSimon Schubert 	}
17275796c8dcSSimon Schubert 
17285796c8dcSSimon Schubert       if (count)
17295796c8dcSSimon Schubert 	{
17305796c8dcSSimon Schubert 	  /* We need to quote this string so buildargv can pull it apart.  */
17315796c8dcSSimon Schubert 	  char *temp = xmalloc (strlen (arg) + count + 1 );
17325796c8dcSSimon Schubert 	  char *ptemp = temp;
17335796c8dcSSimon Schubert 	  char *prev;
17345796c8dcSSimon Schubert 
17355796c8dcSSimon Schubert 	  make_cleanup (xfree, temp);
17365796c8dcSSimon Schubert 
17375796c8dcSSimon Schubert 	  prev = parg = arg;
17385796c8dcSSimon Schubert 	  while ((parg = strpbrk (parg, "\\\"'\t ")))
17395796c8dcSSimon Schubert 	    {
17405796c8dcSSimon Schubert 	      strncpy (ptemp, prev, parg - prev);
17415796c8dcSSimon Schubert 	      ptemp += parg - prev;
17425796c8dcSSimon Schubert 	      prev = parg++;
17435796c8dcSSimon Schubert 	      *ptemp++ = '\\';
17445796c8dcSSimon Schubert 	    }
17455796c8dcSSimon Schubert 	  strcpy (ptemp, prev);
17465796c8dcSSimon Schubert 
17475796c8dcSSimon Schubert 	  arg = temp;
17485796c8dcSSimon Schubert 	}
17495796c8dcSSimon Schubert     }
17505796c8dcSSimon Schubert 
17515796c8dcSSimon Schubert   target_load (arg, from_tty);
17525796c8dcSSimon Schubert 
17535796c8dcSSimon Schubert   /* After re-loading the executable, we don't really know which
17545796c8dcSSimon Schubert      overlays are mapped any more.  */
17555796c8dcSSimon Schubert   overlay_cache_invalid = 1;
17565796c8dcSSimon Schubert }
17575796c8dcSSimon Schubert 
17585796c8dcSSimon Schubert /* This version of "load" should be usable for any target.  Currently
17595796c8dcSSimon Schubert    it is just used for remote targets, not inftarg.c or core files,
17605796c8dcSSimon Schubert    on the theory that only in that case is it useful.
17615796c8dcSSimon Schubert 
17625796c8dcSSimon Schubert    Avoiding xmodem and the like seems like a win (a) because we don't have
17635796c8dcSSimon Schubert    to worry about finding it, and (b) On VMS, fork() is very slow and so
17645796c8dcSSimon Schubert    we don't want to run a subprocess.  On the other hand, I'm not sure how
17655796c8dcSSimon Schubert    performance compares.  */
17665796c8dcSSimon Schubert 
17675796c8dcSSimon Schubert static int validate_download = 0;
17685796c8dcSSimon Schubert 
17695796c8dcSSimon Schubert /* Callback service function for generic_load (bfd_map_over_sections).  */
17705796c8dcSSimon Schubert 
17715796c8dcSSimon Schubert static void
17725796c8dcSSimon Schubert add_section_size_callback (bfd *abfd, asection *asec, void *data)
17735796c8dcSSimon Schubert {
17745796c8dcSSimon Schubert   bfd_size_type *sum = data;
17755796c8dcSSimon Schubert 
17765796c8dcSSimon Schubert   *sum += bfd_get_section_size (asec);
17775796c8dcSSimon Schubert }
17785796c8dcSSimon Schubert 
17795796c8dcSSimon Schubert /* Opaque data for load_section_callback.  */
17805796c8dcSSimon Schubert struct load_section_data {
17815796c8dcSSimon Schubert   unsigned long load_offset;
17825796c8dcSSimon Schubert   struct load_progress_data *progress_data;
17835796c8dcSSimon Schubert   VEC(memory_write_request_s) *requests;
17845796c8dcSSimon Schubert };
17855796c8dcSSimon Schubert 
17865796c8dcSSimon Schubert /* Opaque data for load_progress.  */
17875796c8dcSSimon Schubert struct load_progress_data {
17885796c8dcSSimon Schubert   /* Cumulative data.  */
17895796c8dcSSimon Schubert   unsigned long write_count;
17905796c8dcSSimon Schubert   unsigned long data_count;
17915796c8dcSSimon Schubert   bfd_size_type total_size;
17925796c8dcSSimon Schubert };
17935796c8dcSSimon Schubert 
17945796c8dcSSimon Schubert /* Opaque data for load_progress for a single section.  */
17955796c8dcSSimon Schubert struct load_progress_section_data {
17965796c8dcSSimon Schubert   struct load_progress_data *cumulative;
17975796c8dcSSimon Schubert 
17985796c8dcSSimon Schubert   /* Per-section data.  */
17995796c8dcSSimon Schubert   const char *section_name;
18005796c8dcSSimon Schubert   ULONGEST section_sent;
18015796c8dcSSimon Schubert   ULONGEST section_size;
18025796c8dcSSimon Schubert   CORE_ADDR lma;
18035796c8dcSSimon Schubert   gdb_byte *buffer;
18045796c8dcSSimon Schubert };
18055796c8dcSSimon Schubert 
18065796c8dcSSimon Schubert /* Target write callback routine for progress reporting.  */
18075796c8dcSSimon Schubert 
18085796c8dcSSimon Schubert static void
18095796c8dcSSimon Schubert load_progress (ULONGEST bytes, void *untyped_arg)
18105796c8dcSSimon Schubert {
18115796c8dcSSimon Schubert   struct load_progress_section_data *args = untyped_arg;
18125796c8dcSSimon Schubert   struct load_progress_data *totals;
18135796c8dcSSimon Schubert 
18145796c8dcSSimon Schubert   if (args == NULL)
18155796c8dcSSimon Schubert     /* Writing padding data.  No easy way to get at the cumulative
18165796c8dcSSimon Schubert        stats, so just ignore this.  */
18175796c8dcSSimon Schubert     return;
18185796c8dcSSimon Schubert 
18195796c8dcSSimon Schubert   totals = args->cumulative;
18205796c8dcSSimon Schubert 
18215796c8dcSSimon Schubert   if (bytes == 0 && args->section_sent == 0)
18225796c8dcSSimon Schubert     {
18235796c8dcSSimon Schubert       /* The write is just starting.  Let the user know we've started
18245796c8dcSSimon Schubert 	 this section.  */
18255796c8dcSSimon Schubert       ui_out_message (uiout, 0, "Loading section %s, size %s lma %s\n",
18265796c8dcSSimon Schubert 		      args->section_name, hex_string (args->section_size),
18275796c8dcSSimon Schubert 		      paddress (target_gdbarch, args->lma));
18285796c8dcSSimon Schubert       return;
18295796c8dcSSimon Schubert     }
18305796c8dcSSimon Schubert 
18315796c8dcSSimon Schubert   if (validate_download)
18325796c8dcSSimon Schubert     {
18335796c8dcSSimon Schubert       /* Broken memories and broken monitors manifest themselves here
18345796c8dcSSimon Schubert 	 when bring new computers to life.  This doubles already slow
18355796c8dcSSimon Schubert 	 downloads.  */
18365796c8dcSSimon Schubert       /* NOTE: cagney/1999-10-18: A more efficient implementation
18375796c8dcSSimon Schubert 	 might add a verify_memory() method to the target vector and
18385796c8dcSSimon Schubert 	 then use that.  remote.c could implement that method using
18395796c8dcSSimon Schubert 	 the ``qCRC'' packet.  */
18405796c8dcSSimon Schubert       gdb_byte *check = xmalloc (bytes);
18415796c8dcSSimon Schubert       struct cleanup *verify_cleanups = make_cleanup (xfree, check);
18425796c8dcSSimon Schubert 
18435796c8dcSSimon Schubert       if (target_read_memory (args->lma, check, bytes) != 0)
18445796c8dcSSimon Schubert 	error (_("Download verify read failed at %s"),
18455796c8dcSSimon Schubert 	       paddress (target_gdbarch, args->lma));
18465796c8dcSSimon Schubert       if (memcmp (args->buffer, check, bytes) != 0)
18475796c8dcSSimon Schubert 	error (_("Download verify compare failed at %s"),
18485796c8dcSSimon Schubert 	       paddress (target_gdbarch, args->lma));
18495796c8dcSSimon Schubert       do_cleanups (verify_cleanups);
18505796c8dcSSimon Schubert     }
18515796c8dcSSimon Schubert   totals->data_count += bytes;
18525796c8dcSSimon Schubert   args->lma += bytes;
18535796c8dcSSimon Schubert   args->buffer += bytes;
18545796c8dcSSimon Schubert   totals->write_count += 1;
18555796c8dcSSimon Schubert   args->section_sent += bytes;
18565796c8dcSSimon Schubert   if (quit_flag
18575796c8dcSSimon Schubert       || (deprecated_ui_load_progress_hook != NULL
18585796c8dcSSimon Schubert 	  && deprecated_ui_load_progress_hook (args->section_name,
18595796c8dcSSimon Schubert 					       args->section_sent)))
18605796c8dcSSimon Schubert     error (_("Canceled the download"));
18615796c8dcSSimon Schubert 
18625796c8dcSSimon Schubert   if (deprecated_show_load_progress != NULL)
18635796c8dcSSimon Schubert     deprecated_show_load_progress (args->section_name,
18645796c8dcSSimon Schubert 				   args->section_sent,
18655796c8dcSSimon Schubert 				   args->section_size,
18665796c8dcSSimon Schubert 				   totals->data_count,
18675796c8dcSSimon Schubert 				   totals->total_size);
18685796c8dcSSimon Schubert }
18695796c8dcSSimon Schubert 
18705796c8dcSSimon Schubert /* Callback service function for generic_load (bfd_map_over_sections).  */
18715796c8dcSSimon Schubert 
18725796c8dcSSimon Schubert static void
18735796c8dcSSimon Schubert load_section_callback (bfd *abfd, asection *asec, void *data)
18745796c8dcSSimon Schubert {
18755796c8dcSSimon Schubert   struct memory_write_request *new_request;
18765796c8dcSSimon Schubert   struct load_section_data *args = data;
18775796c8dcSSimon Schubert   struct load_progress_section_data *section_data;
18785796c8dcSSimon Schubert   bfd_size_type size = bfd_get_section_size (asec);
18795796c8dcSSimon Schubert   gdb_byte *buffer;
18805796c8dcSSimon Schubert   const char *sect_name = bfd_get_section_name (abfd, asec);
18815796c8dcSSimon Schubert 
18825796c8dcSSimon Schubert   if ((bfd_get_section_flags (abfd, asec) & SEC_LOAD) == 0)
18835796c8dcSSimon Schubert     return;
18845796c8dcSSimon Schubert 
18855796c8dcSSimon Schubert   if (size == 0)
18865796c8dcSSimon Schubert     return;
18875796c8dcSSimon Schubert 
18885796c8dcSSimon Schubert   new_request = VEC_safe_push (memory_write_request_s,
18895796c8dcSSimon Schubert 			       args->requests, NULL);
18905796c8dcSSimon Schubert   memset (new_request, 0, sizeof (struct memory_write_request));
18915796c8dcSSimon Schubert   section_data = xcalloc (1, sizeof (struct load_progress_section_data));
18925796c8dcSSimon Schubert   new_request->begin = bfd_section_lma (abfd, asec) + args->load_offset;
18935796c8dcSSimon Schubert   new_request->end = new_request->begin + size; /* FIXME Should size be in instead?  */
18945796c8dcSSimon Schubert   new_request->data = xmalloc (size);
18955796c8dcSSimon Schubert   new_request->baton = section_data;
18965796c8dcSSimon Schubert 
18975796c8dcSSimon Schubert   buffer = new_request->data;
18985796c8dcSSimon Schubert 
18995796c8dcSSimon Schubert   section_data->cumulative = args->progress_data;
19005796c8dcSSimon Schubert   section_data->section_name = sect_name;
19015796c8dcSSimon Schubert   section_data->section_size = size;
19025796c8dcSSimon Schubert   section_data->lma = new_request->begin;
19035796c8dcSSimon Schubert   section_data->buffer = buffer;
19045796c8dcSSimon Schubert 
19055796c8dcSSimon Schubert   bfd_get_section_contents (abfd, asec, buffer, 0, size);
19065796c8dcSSimon Schubert }
19075796c8dcSSimon Schubert 
19085796c8dcSSimon Schubert /* Clean up an entire memory request vector, including load
19095796c8dcSSimon Schubert    data and progress records.  */
19105796c8dcSSimon Schubert 
19115796c8dcSSimon Schubert static void
19125796c8dcSSimon Schubert clear_memory_write_data (void *arg)
19135796c8dcSSimon Schubert {
19145796c8dcSSimon Schubert   VEC(memory_write_request_s) **vec_p = arg;
19155796c8dcSSimon Schubert   VEC(memory_write_request_s) *vec = *vec_p;
19165796c8dcSSimon Schubert   int i;
19175796c8dcSSimon Schubert   struct memory_write_request *mr;
19185796c8dcSSimon Schubert 
19195796c8dcSSimon Schubert   for (i = 0; VEC_iterate (memory_write_request_s, vec, i, mr); ++i)
19205796c8dcSSimon Schubert     {
19215796c8dcSSimon Schubert       xfree (mr->data);
19225796c8dcSSimon Schubert       xfree (mr->baton);
19235796c8dcSSimon Schubert     }
19245796c8dcSSimon Schubert   VEC_free (memory_write_request_s, vec);
19255796c8dcSSimon Schubert }
19265796c8dcSSimon Schubert 
19275796c8dcSSimon Schubert void
19285796c8dcSSimon Schubert generic_load (char *args, int from_tty)
19295796c8dcSSimon Schubert {
19305796c8dcSSimon Schubert   bfd *loadfile_bfd;
19315796c8dcSSimon Schubert   struct timeval start_time, end_time;
19325796c8dcSSimon Schubert   char *filename;
19335796c8dcSSimon Schubert   struct cleanup *old_cleanups = make_cleanup (null_cleanup, 0);
19345796c8dcSSimon Schubert   struct load_section_data cbdata;
19355796c8dcSSimon Schubert   struct load_progress_data total_progress;
19365796c8dcSSimon Schubert 
19375796c8dcSSimon Schubert   CORE_ADDR entry;
19385796c8dcSSimon Schubert   char **argv;
19395796c8dcSSimon Schubert 
19405796c8dcSSimon Schubert   memset (&cbdata, 0, sizeof (cbdata));
19415796c8dcSSimon Schubert   memset (&total_progress, 0, sizeof (total_progress));
19425796c8dcSSimon Schubert   cbdata.progress_data = &total_progress;
19435796c8dcSSimon Schubert 
19445796c8dcSSimon Schubert   make_cleanup (clear_memory_write_data, &cbdata.requests);
19455796c8dcSSimon Schubert 
19465796c8dcSSimon Schubert   if (args == NULL)
19475796c8dcSSimon Schubert     error_no_arg (_("file to load"));
19485796c8dcSSimon Schubert 
19495796c8dcSSimon Schubert   argv = gdb_buildargv (args);
19505796c8dcSSimon Schubert   make_cleanup_freeargv (argv);
19515796c8dcSSimon Schubert 
19525796c8dcSSimon Schubert   filename = tilde_expand (argv[0]);
19535796c8dcSSimon Schubert   make_cleanup (xfree, filename);
19545796c8dcSSimon Schubert 
19555796c8dcSSimon Schubert   if (argv[1] != NULL)
19565796c8dcSSimon Schubert     {
19575796c8dcSSimon Schubert       char *endptr;
19585796c8dcSSimon Schubert 
19595796c8dcSSimon Schubert       cbdata.load_offset = strtoul (argv[1], &endptr, 0);
19605796c8dcSSimon Schubert 
19615796c8dcSSimon Schubert       /* If the last word was not a valid number then
19625796c8dcSSimon Schubert          treat it as a file name with spaces in.  */
19635796c8dcSSimon Schubert       if (argv[1] == endptr)
19645796c8dcSSimon Schubert         error (_("Invalid download offset:%s."), argv[1]);
19655796c8dcSSimon Schubert 
19665796c8dcSSimon Schubert       if (argv[2] != NULL)
19675796c8dcSSimon Schubert 	error (_("Too many parameters."));
19685796c8dcSSimon Schubert     }
19695796c8dcSSimon Schubert 
19705796c8dcSSimon Schubert   /* Open the file for loading. */
19715796c8dcSSimon Schubert   loadfile_bfd = bfd_openr (filename, gnutarget);
19725796c8dcSSimon Schubert   if (loadfile_bfd == NULL)
19735796c8dcSSimon Schubert     {
19745796c8dcSSimon Schubert       perror_with_name (filename);
19755796c8dcSSimon Schubert       return;
19765796c8dcSSimon Schubert     }
19775796c8dcSSimon Schubert 
19785796c8dcSSimon Schubert   /* FIXME: should be checking for errors from bfd_close (for one thing,
19795796c8dcSSimon Schubert      on error it does not free all the storage associated with the
19805796c8dcSSimon Schubert      bfd).  */
19815796c8dcSSimon Schubert   make_cleanup_bfd_close (loadfile_bfd);
19825796c8dcSSimon Schubert 
19835796c8dcSSimon Schubert   if (!bfd_check_format (loadfile_bfd, bfd_object))
19845796c8dcSSimon Schubert     {
19855796c8dcSSimon Schubert       error (_("\"%s\" is not an object file: %s"), filename,
19865796c8dcSSimon Schubert 	     bfd_errmsg (bfd_get_error ()));
19875796c8dcSSimon Schubert     }
19885796c8dcSSimon Schubert 
19895796c8dcSSimon Schubert   bfd_map_over_sections (loadfile_bfd, add_section_size_callback,
19905796c8dcSSimon Schubert 			 (void *) &total_progress.total_size);
19915796c8dcSSimon Schubert 
19925796c8dcSSimon Schubert   bfd_map_over_sections (loadfile_bfd, load_section_callback, &cbdata);
19935796c8dcSSimon Schubert 
19945796c8dcSSimon Schubert   gettimeofday (&start_time, NULL);
19955796c8dcSSimon Schubert 
19965796c8dcSSimon Schubert   if (target_write_memory_blocks (cbdata.requests, flash_discard,
19975796c8dcSSimon Schubert 				  load_progress) != 0)
19985796c8dcSSimon Schubert     error (_("Load failed"));
19995796c8dcSSimon Schubert 
20005796c8dcSSimon Schubert   gettimeofday (&end_time, NULL);
20015796c8dcSSimon Schubert 
20025796c8dcSSimon Schubert   entry = bfd_get_start_address (loadfile_bfd);
20035796c8dcSSimon Schubert   ui_out_text (uiout, "Start address ");
20045796c8dcSSimon Schubert   ui_out_field_fmt (uiout, "address", "%s", paddress (target_gdbarch, entry));
20055796c8dcSSimon Schubert   ui_out_text (uiout, ", load size ");
20065796c8dcSSimon Schubert   ui_out_field_fmt (uiout, "load-size", "%lu", total_progress.data_count);
20075796c8dcSSimon Schubert   ui_out_text (uiout, "\n");
20085796c8dcSSimon Schubert   /* We were doing this in remote-mips.c, I suspect it is right
20095796c8dcSSimon Schubert      for other targets too.  */
20105796c8dcSSimon Schubert   regcache_write_pc (get_current_regcache (), entry);
20115796c8dcSSimon Schubert 
2012*cf7f2e2dSJohn Marino   /* Reset breakpoints, now that we have changed the load image.  For
2013*cf7f2e2dSJohn Marino      instance, breakpoints may have been set (or reset, by
2014*cf7f2e2dSJohn Marino      post_create_inferior) while connected to the target but before we
2015*cf7f2e2dSJohn Marino      loaded the program.  In that case, the prologue analyzer could
2016*cf7f2e2dSJohn Marino      have read instructions from the target to find the right
2017*cf7f2e2dSJohn Marino      breakpoint locations.  Loading has changed the contents of that
2018*cf7f2e2dSJohn Marino      memory.  */
2019*cf7f2e2dSJohn Marino 
2020*cf7f2e2dSJohn Marino   breakpoint_re_set ();
2021*cf7f2e2dSJohn Marino 
20225796c8dcSSimon Schubert   /* FIXME: are we supposed to call symbol_file_add or not?  According
20235796c8dcSSimon Schubert      to a comment from remote-mips.c (where a call to symbol_file_add
20245796c8dcSSimon Schubert      was commented out), making the call confuses GDB if more than one
20255796c8dcSSimon Schubert      file is loaded in.  Some targets do (e.g., remote-vx.c) but
20265796c8dcSSimon Schubert      others don't (or didn't - perhaps they have all been deleted).  */
20275796c8dcSSimon Schubert 
20285796c8dcSSimon Schubert   print_transfer_performance (gdb_stdout, total_progress.data_count,
20295796c8dcSSimon Schubert 			      total_progress.write_count,
20305796c8dcSSimon Schubert 			      &start_time, &end_time);
20315796c8dcSSimon Schubert 
20325796c8dcSSimon Schubert   do_cleanups (old_cleanups);
20335796c8dcSSimon Schubert }
20345796c8dcSSimon Schubert 
20355796c8dcSSimon Schubert /* Report how fast the transfer went. */
20365796c8dcSSimon Schubert 
20375796c8dcSSimon Schubert /* DEPRECATED: cagney/1999-10-18: report_transfer_performance is being
20385796c8dcSSimon Schubert    replaced by print_transfer_performance (with a very different
20395796c8dcSSimon Schubert    function signature). */
20405796c8dcSSimon Schubert 
20415796c8dcSSimon Schubert void
20425796c8dcSSimon Schubert report_transfer_performance (unsigned long data_count, time_t start_time,
20435796c8dcSSimon Schubert 			     time_t end_time)
20445796c8dcSSimon Schubert {
20455796c8dcSSimon Schubert   struct timeval start, end;
20465796c8dcSSimon Schubert 
20475796c8dcSSimon Schubert   start.tv_sec = start_time;
20485796c8dcSSimon Schubert   start.tv_usec = 0;
20495796c8dcSSimon Schubert   end.tv_sec = end_time;
20505796c8dcSSimon Schubert   end.tv_usec = 0;
20515796c8dcSSimon Schubert 
20525796c8dcSSimon Schubert   print_transfer_performance (gdb_stdout, data_count, 0, &start, &end);
20535796c8dcSSimon Schubert }
20545796c8dcSSimon Schubert 
20555796c8dcSSimon Schubert void
20565796c8dcSSimon Schubert print_transfer_performance (struct ui_file *stream,
20575796c8dcSSimon Schubert 			    unsigned long data_count,
20585796c8dcSSimon Schubert 			    unsigned long write_count,
20595796c8dcSSimon Schubert 			    const struct timeval *start_time,
20605796c8dcSSimon Schubert 			    const struct timeval *end_time)
20615796c8dcSSimon Schubert {
20625796c8dcSSimon Schubert   ULONGEST time_count;
20635796c8dcSSimon Schubert 
20645796c8dcSSimon Schubert   /* Compute the elapsed time in milliseconds, as a tradeoff between
20655796c8dcSSimon Schubert      accuracy and overflow.  */
20665796c8dcSSimon Schubert   time_count = (end_time->tv_sec - start_time->tv_sec) * 1000;
20675796c8dcSSimon Schubert   time_count += (end_time->tv_usec - start_time->tv_usec) / 1000;
20685796c8dcSSimon Schubert 
20695796c8dcSSimon Schubert   ui_out_text (uiout, "Transfer rate: ");
20705796c8dcSSimon Schubert   if (time_count > 0)
20715796c8dcSSimon Schubert     {
20725796c8dcSSimon Schubert       unsigned long rate = ((ULONGEST) data_count * 1000) / time_count;
20735796c8dcSSimon Schubert 
20745796c8dcSSimon Schubert       if (ui_out_is_mi_like_p (uiout))
20755796c8dcSSimon Schubert 	{
20765796c8dcSSimon Schubert 	  ui_out_field_fmt (uiout, "transfer-rate", "%lu", rate * 8);
20775796c8dcSSimon Schubert 	  ui_out_text (uiout, " bits/sec");
20785796c8dcSSimon Schubert 	}
20795796c8dcSSimon Schubert       else if (rate < 1024)
20805796c8dcSSimon Schubert 	{
20815796c8dcSSimon Schubert 	  ui_out_field_fmt (uiout, "transfer-rate", "%lu", rate);
20825796c8dcSSimon Schubert 	  ui_out_text (uiout, " bytes/sec");
20835796c8dcSSimon Schubert 	}
20845796c8dcSSimon Schubert       else
20855796c8dcSSimon Schubert 	{
20865796c8dcSSimon Schubert 	  ui_out_field_fmt (uiout, "transfer-rate", "%lu", rate / 1024);
20875796c8dcSSimon Schubert 	  ui_out_text (uiout, " KB/sec");
20885796c8dcSSimon Schubert 	}
20895796c8dcSSimon Schubert     }
20905796c8dcSSimon Schubert   else
20915796c8dcSSimon Schubert     {
20925796c8dcSSimon Schubert       ui_out_field_fmt (uiout, "transferred-bits", "%lu", (data_count * 8));
20935796c8dcSSimon Schubert       ui_out_text (uiout, " bits in <1 sec");
20945796c8dcSSimon Schubert     }
20955796c8dcSSimon Schubert   if (write_count > 0)
20965796c8dcSSimon Schubert     {
20975796c8dcSSimon Schubert       ui_out_text (uiout, ", ");
20985796c8dcSSimon Schubert       ui_out_field_fmt (uiout, "write-rate", "%lu", data_count / write_count);
20995796c8dcSSimon Schubert       ui_out_text (uiout, " bytes/write");
21005796c8dcSSimon Schubert     }
21015796c8dcSSimon Schubert   ui_out_text (uiout, ".\n");
21025796c8dcSSimon Schubert }
21035796c8dcSSimon Schubert 
21045796c8dcSSimon Schubert /* This function allows the addition of incrementally linked object files.
21055796c8dcSSimon Schubert    It does not modify any state in the target, only in the debugger.  */
21065796c8dcSSimon Schubert /* Note: ezannoni 2000-04-13 This function/command used to have a
21075796c8dcSSimon Schubert    special case syntax for the rombug target (Rombug is the boot
21085796c8dcSSimon Schubert    monitor for Microware's OS-9 / OS-9000, see remote-os9k.c). In the
21095796c8dcSSimon Schubert    rombug case, the user doesn't need to supply a text address,
21105796c8dcSSimon Schubert    instead a call to target_link() (in target.c) would supply the
21115796c8dcSSimon Schubert    value to use. We are now discontinuing this type of ad hoc syntax. */
21125796c8dcSSimon Schubert 
21135796c8dcSSimon Schubert static void
21145796c8dcSSimon Schubert add_symbol_file_command (char *args, int from_tty)
21155796c8dcSSimon Schubert {
21165796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_current_arch ();
21175796c8dcSSimon Schubert   char *filename = NULL;
21185796c8dcSSimon Schubert   int flags = OBJF_USERLOADED;
21195796c8dcSSimon Schubert   char *arg;
21205796c8dcSSimon Schubert   int section_index = 0;
21215796c8dcSSimon Schubert   int argcnt = 0;
21225796c8dcSSimon Schubert   int sec_num = 0;
21235796c8dcSSimon Schubert   int i;
21245796c8dcSSimon Schubert   int expecting_sec_name = 0;
21255796c8dcSSimon Schubert   int expecting_sec_addr = 0;
21265796c8dcSSimon Schubert   char **argv;
21275796c8dcSSimon Schubert 
21285796c8dcSSimon Schubert   struct sect_opt
21295796c8dcSSimon Schubert   {
21305796c8dcSSimon Schubert     char *name;
21315796c8dcSSimon Schubert     char *value;
21325796c8dcSSimon Schubert   };
21335796c8dcSSimon Schubert 
21345796c8dcSSimon Schubert   struct section_addr_info *section_addrs;
21355796c8dcSSimon Schubert   struct sect_opt *sect_opts = NULL;
21365796c8dcSSimon Schubert   size_t num_sect_opts = 0;
21375796c8dcSSimon Schubert   struct cleanup *my_cleanups = make_cleanup (null_cleanup, NULL);
21385796c8dcSSimon Schubert 
21395796c8dcSSimon Schubert   num_sect_opts = 16;
21405796c8dcSSimon Schubert   sect_opts = (struct sect_opt *) xmalloc (num_sect_opts
21415796c8dcSSimon Schubert 					   * sizeof (struct sect_opt));
21425796c8dcSSimon Schubert 
21435796c8dcSSimon Schubert   dont_repeat ();
21445796c8dcSSimon Schubert 
21455796c8dcSSimon Schubert   if (args == NULL)
21465796c8dcSSimon Schubert     error (_("add-symbol-file takes a file name and an address"));
21475796c8dcSSimon Schubert 
21485796c8dcSSimon Schubert   argv = gdb_buildargv (args);
21495796c8dcSSimon Schubert   make_cleanup_freeargv (argv);
21505796c8dcSSimon Schubert 
21515796c8dcSSimon Schubert   for (arg = argv[0], argcnt = 0; arg != NULL; arg = argv[++argcnt])
21525796c8dcSSimon Schubert     {
21535796c8dcSSimon Schubert       /* Process the argument. */
21545796c8dcSSimon Schubert       if (argcnt == 0)
21555796c8dcSSimon Schubert 	{
21565796c8dcSSimon Schubert 	  /* The first argument is the file name. */
21575796c8dcSSimon Schubert 	  filename = tilde_expand (arg);
21585796c8dcSSimon Schubert 	  make_cleanup (xfree, filename);
21595796c8dcSSimon Schubert 	}
21605796c8dcSSimon Schubert       else
21615796c8dcSSimon Schubert 	if (argcnt == 1)
21625796c8dcSSimon Schubert 	  {
21635796c8dcSSimon Schubert 	    /* The second argument is always the text address at which
21645796c8dcSSimon Schubert                to load the program. */
21655796c8dcSSimon Schubert 	    sect_opts[section_index].name = ".text";
21665796c8dcSSimon Schubert 	    sect_opts[section_index].value = arg;
21675796c8dcSSimon Schubert 	    if (++section_index >= num_sect_opts)
21685796c8dcSSimon Schubert 	      {
21695796c8dcSSimon Schubert 		num_sect_opts *= 2;
21705796c8dcSSimon Schubert 		sect_opts = ((struct sect_opt *)
21715796c8dcSSimon Schubert 			     xrealloc (sect_opts,
21725796c8dcSSimon Schubert 				       num_sect_opts
21735796c8dcSSimon Schubert 				       * sizeof (struct sect_opt)));
21745796c8dcSSimon Schubert 	      }
21755796c8dcSSimon Schubert 	  }
21765796c8dcSSimon Schubert 	else
21775796c8dcSSimon Schubert 	  {
21785796c8dcSSimon Schubert 	    /* It's an option (starting with '-') or it's an argument
21795796c8dcSSimon Schubert 	       to an option */
21805796c8dcSSimon Schubert 
21815796c8dcSSimon Schubert 	    if (*arg == '-')
21825796c8dcSSimon Schubert 	      {
21835796c8dcSSimon Schubert 		if (strcmp (arg, "-readnow") == 0)
21845796c8dcSSimon Schubert 		  flags |= OBJF_READNOW;
21855796c8dcSSimon Schubert 		else if (strcmp (arg, "-s") == 0)
21865796c8dcSSimon Schubert 		  {
21875796c8dcSSimon Schubert 		    expecting_sec_name = 1;
21885796c8dcSSimon Schubert 		    expecting_sec_addr = 1;
21895796c8dcSSimon Schubert 		  }
21905796c8dcSSimon Schubert 	      }
21915796c8dcSSimon Schubert 	    else
21925796c8dcSSimon Schubert 	      {
21935796c8dcSSimon Schubert 		if (expecting_sec_name)
21945796c8dcSSimon Schubert 		  {
21955796c8dcSSimon Schubert 		    sect_opts[section_index].name = arg;
21965796c8dcSSimon Schubert 		    expecting_sec_name = 0;
21975796c8dcSSimon Schubert 		  }
21985796c8dcSSimon Schubert 		else
21995796c8dcSSimon Schubert 		  if (expecting_sec_addr)
22005796c8dcSSimon Schubert 		    {
22015796c8dcSSimon Schubert 		      sect_opts[section_index].value = arg;
22025796c8dcSSimon Schubert 		      expecting_sec_addr = 0;
22035796c8dcSSimon Schubert 		      if (++section_index >= num_sect_opts)
22045796c8dcSSimon Schubert 			{
22055796c8dcSSimon Schubert 			  num_sect_opts *= 2;
22065796c8dcSSimon Schubert 			  sect_opts = ((struct sect_opt *)
22075796c8dcSSimon Schubert 				       xrealloc (sect_opts,
22085796c8dcSSimon Schubert 						 num_sect_opts
22095796c8dcSSimon Schubert 						 * sizeof (struct sect_opt)));
22105796c8dcSSimon Schubert 			}
22115796c8dcSSimon Schubert 		    }
22125796c8dcSSimon Schubert 		  else
22135796c8dcSSimon Schubert 		    error (_("USAGE: add-symbol-file <filename> <textaddress> [-mapped] [-readnow] [-s <secname> <addr>]*"));
22145796c8dcSSimon Schubert 	      }
22155796c8dcSSimon Schubert 	  }
22165796c8dcSSimon Schubert     }
22175796c8dcSSimon Schubert 
22185796c8dcSSimon Schubert   /* This command takes at least two arguments.  The first one is a
22195796c8dcSSimon Schubert      filename, and the second is the address where this file has been
22205796c8dcSSimon Schubert      loaded.  Abort now if this address hasn't been provided by the
22215796c8dcSSimon Schubert      user.  */
22225796c8dcSSimon Schubert   if (section_index < 1)
22235796c8dcSSimon Schubert     error (_("The address where %s has been loaded is missing"), filename);
22245796c8dcSSimon Schubert 
22255796c8dcSSimon Schubert   /* Print the prompt for the query below. And save the arguments into
22265796c8dcSSimon Schubert      a sect_addr_info structure to be passed around to other
22275796c8dcSSimon Schubert      functions.  We have to split this up into separate print
22285796c8dcSSimon Schubert      statements because hex_string returns a local static
22295796c8dcSSimon Schubert      string. */
22305796c8dcSSimon Schubert 
22315796c8dcSSimon Schubert   printf_unfiltered (_("add symbol table from file \"%s\" at\n"), filename);
22325796c8dcSSimon Schubert   section_addrs = alloc_section_addr_info (section_index);
22335796c8dcSSimon Schubert   make_cleanup (xfree, section_addrs);
22345796c8dcSSimon Schubert   for (i = 0; i < section_index; i++)
22355796c8dcSSimon Schubert     {
22365796c8dcSSimon Schubert       CORE_ADDR addr;
22375796c8dcSSimon Schubert       char *val = sect_opts[i].value;
22385796c8dcSSimon Schubert       char *sec = sect_opts[i].name;
22395796c8dcSSimon Schubert 
22405796c8dcSSimon Schubert       addr = parse_and_eval_address (val);
22415796c8dcSSimon Schubert 
22425796c8dcSSimon Schubert       /* Here we store the section offsets in the order they were
22435796c8dcSSimon Schubert          entered on the command line. */
22445796c8dcSSimon Schubert       section_addrs->other[sec_num].name = sec;
22455796c8dcSSimon Schubert       section_addrs->other[sec_num].addr = addr;
22465796c8dcSSimon Schubert       printf_unfiltered ("\t%s_addr = %s\n", sec,
22475796c8dcSSimon Schubert 			 paddress (gdbarch, addr));
22485796c8dcSSimon Schubert       sec_num++;
22495796c8dcSSimon Schubert 
22505796c8dcSSimon Schubert       /* The object's sections are initialized when a
22515796c8dcSSimon Schubert 	 call is made to build_objfile_section_table (objfile).
22525796c8dcSSimon Schubert 	 This happens in reread_symbols.
22535796c8dcSSimon Schubert 	 At this point, we don't know what file type this is,
22545796c8dcSSimon Schubert 	 so we can't determine what section names are valid.  */
22555796c8dcSSimon Schubert     }
22565796c8dcSSimon Schubert 
22575796c8dcSSimon Schubert   if (from_tty && (!query ("%s", "")))
22585796c8dcSSimon Schubert     error (_("Not confirmed."));
22595796c8dcSSimon Schubert 
22605796c8dcSSimon Schubert   symbol_file_add (filename, from_tty ? SYMFILE_VERBOSE : 0,
22615796c8dcSSimon Schubert                    section_addrs, flags);
22625796c8dcSSimon Schubert 
22635796c8dcSSimon Schubert   /* Getting new symbols may change our opinion about what is
22645796c8dcSSimon Schubert      frameless.  */
22655796c8dcSSimon Schubert   reinit_frame_cache ();
22665796c8dcSSimon Schubert   do_cleanups (my_cleanups);
22675796c8dcSSimon Schubert }
22685796c8dcSSimon Schubert 
22695796c8dcSSimon Schubert 
22705796c8dcSSimon Schubert /* Re-read symbols if a symbol-file has changed.  */
22715796c8dcSSimon Schubert void
22725796c8dcSSimon Schubert reread_symbols (void)
22735796c8dcSSimon Schubert {
22745796c8dcSSimon Schubert   struct objfile *objfile;
22755796c8dcSSimon Schubert   long new_modtime;
22765796c8dcSSimon Schubert   int reread_one = 0;
22775796c8dcSSimon Schubert   struct stat new_statbuf;
22785796c8dcSSimon Schubert   int res;
22795796c8dcSSimon Schubert 
22805796c8dcSSimon Schubert   /* With the addition of shared libraries, this should be modified,
22815796c8dcSSimon Schubert      the load time should be saved in the partial symbol tables, since
22825796c8dcSSimon Schubert      different tables may come from different source files.  FIXME.
22835796c8dcSSimon Schubert      This routine should then walk down each partial symbol table
22845796c8dcSSimon Schubert      and see if the symbol table that it originates from has been changed */
22855796c8dcSSimon Schubert 
22865796c8dcSSimon Schubert   for (objfile = object_files; objfile; objfile = objfile->next)
22875796c8dcSSimon Schubert     {
2288*cf7f2e2dSJohn Marino       /* solib-sunos.c creates one objfile with obfd.  */
2289*cf7f2e2dSJohn Marino       if (objfile->obfd == NULL)
2290*cf7f2e2dSJohn Marino 	continue;
22915796c8dcSSimon Schubert 
2292*cf7f2e2dSJohn Marino       /* Separate debug objfiles are handled in the main objfile.  */
2293*cf7f2e2dSJohn Marino       if (objfile->separate_debug_objfile_backlink)
2294*cf7f2e2dSJohn Marino 	continue;
2295*cf7f2e2dSJohn Marino 
2296*cf7f2e2dSJohn Marino       /* If this object is from an archive (what you usually create with
2297*cf7f2e2dSJohn Marino 	 `ar', often called a `static library' on most systems, though
2298*cf7f2e2dSJohn Marino 	 a `shared library' on AIX is also an archive), then you should
2299*cf7f2e2dSJohn Marino 	 stat on the archive name, not member name.  */
23005796c8dcSSimon Schubert       if (objfile->obfd->my_archive)
23015796c8dcSSimon Schubert 	res = stat (objfile->obfd->my_archive->filename, &new_statbuf);
23025796c8dcSSimon Schubert       else
23035796c8dcSSimon Schubert 	res = stat (objfile->name, &new_statbuf);
23045796c8dcSSimon Schubert       if (res != 0)
23055796c8dcSSimon Schubert 	{
23065796c8dcSSimon Schubert 	  /* FIXME, should use print_sys_errmsg but it's not filtered. */
23075796c8dcSSimon Schubert 	  printf_unfiltered (_("`%s' has disappeared; keeping its symbols.\n"),
23085796c8dcSSimon Schubert 			     objfile->name);
23095796c8dcSSimon Schubert 	  continue;
23105796c8dcSSimon Schubert 	}
23115796c8dcSSimon Schubert       new_modtime = new_statbuf.st_mtime;
23125796c8dcSSimon Schubert       if (new_modtime != objfile->mtime)
23135796c8dcSSimon Schubert 	{
23145796c8dcSSimon Schubert 	  struct cleanup *old_cleanups;
23155796c8dcSSimon Schubert 	  struct section_offsets *offsets;
23165796c8dcSSimon Schubert 	  int num_offsets;
23175796c8dcSSimon Schubert 	  char *obfd_filename;
23185796c8dcSSimon Schubert 
23195796c8dcSSimon Schubert 	  printf_unfiltered (_("`%s' has changed; re-reading symbols.\n"),
23205796c8dcSSimon Schubert 			     objfile->name);
23215796c8dcSSimon Schubert 
23225796c8dcSSimon Schubert 	  /* There are various functions like symbol_file_add,
23235796c8dcSSimon Schubert 	     symfile_bfd_open, syms_from_objfile, etc., which might
23245796c8dcSSimon Schubert 	     appear to do what we want.  But they have various other
23255796c8dcSSimon Schubert 	     effects which we *don't* want.  So we just do stuff
23265796c8dcSSimon Schubert 	     ourselves.  We don't worry about mapped files (for one thing,
23275796c8dcSSimon Schubert 	     any mapped file will be out of date).  */
23285796c8dcSSimon Schubert 
23295796c8dcSSimon Schubert 	  /* If we get an error, blow away this objfile (not sure if
23305796c8dcSSimon Schubert 	     that is the correct response for things like shared
23315796c8dcSSimon Schubert 	     libraries).  */
23325796c8dcSSimon Schubert 	  old_cleanups = make_cleanup_free_objfile (objfile);
23335796c8dcSSimon Schubert 	  /* We need to do this whenever any symbols go away.  */
23345796c8dcSSimon Schubert 	  make_cleanup (clear_symtab_users_cleanup, 0 /*ignore*/);
23355796c8dcSSimon Schubert 
23365796c8dcSSimon Schubert 	  if (exec_bfd != NULL && strcmp (bfd_get_filename (objfile->obfd),
23375796c8dcSSimon Schubert 					  bfd_get_filename (exec_bfd)) == 0)
23385796c8dcSSimon Schubert 	    {
23395796c8dcSSimon Schubert 	      /* Reload EXEC_BFD without asking anything.  */
23405796c8dcSSimon Schubert 
23415796c8dcSSimon Schubert 	      exec_file_attach (bfd_get_filename (objfile->obfd), 0);
23425796c8dcSSimon Schubert 	    }
23435796c8dcSSimon Schubert 
23445796c8dcSSimon Schubert 	  /* Clean up any state BFD has sitting around.  We don't need
23455796c8dcSSimon Schubert 	     to close the descriptor but BFD lacks a way of closing the
23465796c8dcSSimon Schubert 	     BFD without closing the descriptor.  */
23475796c8dcSSimon Schubert 	  obfd_filename = bfd_get_filename (objfile->obfd);
23485796c8dcSSimon Schubert 	  if (!bfd_close (objfile->obfd))
23495796c8dcSSimon Schubert 	    error (_("Can't close BFD for %s: %s"), objfile->name,
23505796c8dcSSimon Schubert 		   bfd_errmsg (bfd_get_error ()));
2351*cf7f2e2dSJohn Marino 	  objfile->obfd = bfd_open_maybe_remote (obfd_filename);
23525796c8dcSSimon Schubert 	  if (objfile->obfd == NULL)
23535796c8dcSSimon Schubert 	    error (_("Can't open %s to read symbols."), objfile->name);
23545796c8dcSSimon Schubert 	  else
23555796c8dcSSimon Schubert 	    objfile->obfd = gdb_bfd_ref (objfile->obfd);
23565796c8dcSSimon Schubert 	  /* bfd_openr sets cacheable to true, which is what we want.  */
23575796c8dcSSimon Schubert 	  if (!bfd_check_format (objfile->obfd, bfd_object))
23585796c8dcSSimon Schubert 	    error (_("Can't read symbols from %s: %s."), objfile->name,
23595796c8dcSSimon Schubert 		   bfd_errmsg (bfd_get_error ()));
23605796c8dcSSimon Schubert 
23615796c8dcSSimon Schubert 	  /* Save the offsets, we will nuke them with the rest of the
23625796c8dcSSimon Schubert 	     objfile_obstack.  */
23635796c8dcSSimon Schubert 	  num_offsets = objfile->num_sections;
23645796c8dcSSimon Schubert 	  offsets = ((struct section_offsets *)
23655796c8dcSSimon Schubert 		     alloca (SIZEOF_N_SECTION_OFFSETS (num_offsets)));
23665796c8dcSSimon Schubert 	  memcpy (offsets, objfile->section_offsets,
23675796c8dcSSimon Schubert 		  SIZEOF_N_SECTION_OFFSETS (num_offsets));
23685796c8dcSSimon Schubert 
23695796c8dcSSimon Schubert 	  /* Remove any references to this objfile in the global
23705796c8dcSSimon Schubert 	     value lists.  */
23715796c8dcSSimon Schubert 	  preserve_values (objfile);
23725796c8dcSSimon Schubert 
23735796c8dcSSimon Schubert 	  /* Nuke all the state that we will re-read.  Much of the following
23745796c8dcSSimon Schubert 	     code which sets things to NULL really is necessary to tell
23755796c8dcSSimon Schubert 	     other parts of GDB that there is nothing currently there.
23765796c8dcSSimon Schubert 
23775796c8dcSSimon Schubert 	     Try to keep the freeing order compatible with free_objfile.  */
23785796c8dcSSimon Schubert 
23795796c8dcSSimon Schubert 	  if (objfile->sf != NULL)
23805796c8dcSSimon Schubert 	    {
23815796c8dcSSimon Schubert 	      (*objfile->sf->sym_finish) (objfile);
23825796c8dcSSimon Schubert 	    }
23835796c8dcSSimon Schubert 
23845796c8dcSSimon Schubert 	  clear_objfile_data (objfile);
23855796c8dcSSimon Schubert 
2386*cf7f2e2dSJohn Marino 	  /* Free the separate debug objfiles.  It will be
2387*cf7f2e2dSJohn Marino 	     automatically recreated by sym_read.  */
2388*cf7f2e2dSJohn Marino           free_objfile_separate_debug (objfile);
2389*cf7f2e2dSJohn Marino 
23905796c8dcSSimon Schubert 	  /* FIXME: Do we have to free a whole linked list, or is this
23915796c8dcSSimon Schubert 	     enough?  */
23925796c8dcSSimon Schubert 	  if (objfile->global_psymbols.list)
23935796c8dcSSimon Schubert 	    xfree (objfile->global_psymbols.list);
23945796c8dcSSimon Schubert 	  memset (&objfile->global_psymbols, 0,
23955796c8dcSSimon Schubert 		  sizeof (objfile->global_psymbols));
23965796c8dcSSimon Schubert 	  if (objfile->static_psymbols.list)
23975796c8dcSSimon Schubert 	    xfree (objfile->static_psymbols.list);
23985796c8dcSSimon Schubert 	  memset (&objfile->static_psymbols, 0,
23995796c8dcSSimon Schubert 		  sizeof (objfile->static_psymbols));
24005796c8dcSSimon Schubert 
24015796c8dcSSimon Schubert 	  /* Free the obstacks for non-reusable objfiles */
24025796c8dcSSimon Schubert 	  bcache_xfree (objfile->psymbol_cache);
24035796c8dcSSimon Schubert 	  objfile->psymbol_cache = bcache_xmalloc ();
24045796c8dcSSimon Schubert 	  bcache_xfree (objfile->macro_cache);
24055796c8dcSSimon Schubert 	  objfile->macro_cache = bcache_xmalloc ();
2406*cf7f2e2dSJohn Marino 	  bcache_xfree (objfile->filename_cache);
2407*cf7f2e2dSJohn Marino 	  objfile->filename_cache = bcache_xmalloc ();
24085796c8dcSSimon Schubert 	  if (objfile->demangled_names_hash != NULL)
24095796c8dcSSimon Schubert 	    {
24105796c8dcSSimon Schubert 	      htab_delete (objfile->demangled_names_hash);
24115796c8dcSSimon Schubert 	      objfile->demangled_names_hash = NULL;
24125796c8dcSSimon Schubert 	    }
24135796c8dcSSimon Schubert 	  obstack_free (&objfile->objfile_obstack, 0);
24145796c8dcSSimon Schubert 	  objfile->sections = NULL;
24155796c8dcSSimon Schubert 	  objfile->symtabs = NULL;
24165796c8dcSSimon Schubert 	  objfile->psymtabs = NULL;
24175796c8dcSSimon Schubert 	  objfile->psymtabs_addrmap = NULL;
24185796c8dcSSimon Schubert 	  objfile->free_psymtabs = NULL;
24195796c8dcSSimon Schubert 	  objfile->cp_namespace_symtab = NULL;
24205796c8dcSSimon Schubert 	  objfile->msymbols = NULL;
24215796c8dcSSimon Schubert 	  objfile->deprecated_sym_private = NULL;
24225796c8dcSSimon Schubert 	  objfile->minimal_symbol_count = 0;
24235796c8dcSSimon Schubert 	  memset (&objfile->msymbol_hash, 0,
24245796c8dcSSimon Schubert 		  sizeof (objfile->msymbol_hash));
24255796c8dcSSimon Schubert 	  memset (&objfile->msymbol_demangled_hash, 0,
24265796c8dcSSimon Schubert 		  sizeof (objfile->msymbol_demangled_hash));
24275796c8dcSSimon Schubert 
24285796c8dcSSimon Schubert 	  objfile->psymbol_cache = bcache_xmalloc ();
24295796c8dcSSimon Schubert 	  objfile->macro_cache = bcache_xmalloc ();
2430*cf7f2e2dSJohn Marino 	  objfile->filename_cache = bcache_xmalloc ();
24315796c8dcSSimon Schubert 	  /* obstack_init also initializes the obstack so it is
24325796c8dcSSimon Schubert 	     empty.  We could use obstack_specify_allocation but
24335796c8dcSSimon Schubert 	     gdb_obstack.h specifies the alloc/dealloc
24345796c8dcSSimon Schubert 	     functions.  */
24355796c8dcSSimon Schubert 	  obstack_init (&objfile->objfile_obstack);
24365796c8dcSSimon Schubert 	  if (build_objfile_section_table (objfile))
24375796c8dcSSimon Schubert 	    {
24385796c8dcSSimon Schubert 	      error (_("Can't find the file sections in `%s': %s"),
24395796c8dcSSimon Schubert 		     objfile->name, bfd_errmsg (bfd_get_error ()));
24405796c8dcSSimon Schubert 	    }
24415796c8dcSSimon Schubert 	  terminate_minimal_symbol_table (objfile);
24425796c8dcSSimon Schubert 
24435796c8dcSSimon Schubert 	  /* We use the same section offsets as from last time.  I'm not
24445796c8dcSSimon Schubert 	     sure whether that is always correct for shared libraries.  */
24455796c8dcSSimon Schubert 	  objfile->section_offsets = (struct section_offsets *)
24465796c8dcSSimon Schubert 	    obstack_alloc (&objfile->objfile_obstack,
24475796c8dcSSimon Schubert 			   SIZEOF_N_SECTION_OFFSETS (num_offsets));
24485796c8dcSSimon Schubert 	  memcpy (objfile->section_offsets, offsets,
24495796c8dcSSimon Schubert 		  SIZEOF_N_SECTION_OFFSETS (num_offsets));
24505796c8dcSSimon Schubert 	  objfile->num_sections = num_offsets;
24515796c8dcSSimon Schubert 
24525796c8dcSSimon Schubert 	  /* What the hell is sym_new_init for, anyway?  The concept of
24535796c8dcSSimon Schubert 	     distinguishing between the main file and additional files
24545796c8dcSSimon Schubert 	     in this way seems rather dubious.  */
24555796c8dcSSimon Schubert 	  if (objfile == symfile_objfile)
24565796c8dcSSimon Schubert 	    {
24575796c8dcSSimon Schubert 	      (*objfile->sf->sym_new_init) (objfile);
24585796c8dcSSimon Schubert 	    }
24595796c8dcSSimon Schubert 
24605796c8dcSSimon Schubert 	  (*objfile->sf->sym_init) (objfile);
24615796c8dcSSimon Schubert 	  clear_complaints (&symfile_complaints, 1, 1);
2462*cf7f2e2dSJohn Marino 	  /* Do not set flags as this is safe and we don't want to be
2463*cf7f2e2dSJohn Marino              verbose.  */
24645796c8dcSSimon Schubert 	  (*objfile->sf->sym_read) (objfile, 0);
2465*cf7f2e2dSJohn Marino 	  if (!objfile_has_symbols (objfile))
24665796c8dcSSimon Schubert 	    {
24675796c8dcSSimon Schubert 	      wrap_here ("");
24685796c8dcSSimon Schubert 	      printf_unfiltered (_("(no debugging symbols found)\n"));
24695796c8dcSSimon Schubert 	      wrap_here ("");
24705796c8dcSSimon Schubert 	    }
24715796c8dcSSimon Schubert 
24725796c8dcSSimon Schubert 	  /* We're done reading the symbol file; finish off complaints.  */
24735796c8dcSSimon Schubert 	  clear_complaints (&symfile_complaints, 0, 1);
24745796c8dcSSimon Schubert 
24755796c8dcSSimon Schubert 	  /* Getting new symbols may change our opinion about what is
24765796c8dcSSimon Schubert 	     frameless.  */
24775796c8dcSSimon Schubert 
24785796c8dcSSimon Schubert 	  reinit_frame_cache ();
24795796c8dcSSimon Schubert 
24805796c8dcSSimon Schubert 	  /* Discard cleanups as symbol reading was successful.  */
24815796c8dcSSimon Schubert 	  discard_cleanups (old_cleanups);
24825796c8dcSSimon Schubert 
24835796c8dcSSimon Schubert 	  /* If the mtime has changed between the time we set new_modtime
24845796c8dcSSimon Schubert 	     and now, we *want* this to be out of date, so don't call stat
24855796c8dcSSimon Schubert 	     again now.  */
24865796c8dcSSimon Schubert 	  objfile->mtime = new_modtime;
24875796c8dcSSimon Schubert 	  reread_one = 1;
24885796c8dcSSimon Schubert 	  init_entry_point_info (objfile);
24895796c8dcSSimon Schubert 	}
24905796c8dcSSimon Schubert     }
24915796c8dcSSimon Schubert 
24925796c8dcSSimon Schubert   if (reread_one)
24935796c8dcSSimon Schubert     {
24945796c8dcSSimon Schubert       /* Notify objfiles that we've modified objfile sections.  */
24955796c8dcSSimon Schubert       objfiles_changed ();
24965796c8dcSSimon Schubert 
24975796c8dcSSimon Schubert       clear_symtab_users ();
24985796c8dcSSimon Schubert       /* At least one objfile has changed, so we can consider that
24995796c8dcSSimon Schubert          the executable we're debugging has changed too.  */
25005796c8dcSSimon Schubert       observer_notify_executable_changed ();
25015796c8dcSSimon Schubert     }
25025796c8dcSSimon Schubert }
25035796c8dcSSimon Schubert 
25045796c8dcSSimon Schubert 
25055796c8dcSSimon Schubert 
25065796c8dcSSimon Schubert typedef struct
25075796c8dcSSimon Schubert {
25085796c8dcSSimon Schubert   char *ext;
25095796c8dcSSimon Schubert   enum language lang;
25105796c8dcSSimon Schubert }
25115796c8dcSSimon Schubert filename_language;
25125796c8dcSSimon Schubert 
25135796c8dcSSimon Schubert static filename_language *filename_language_table;
25145796c8dcSSimon Schubert static int fl_table_size, fl_table_next;
25155796c8dcSSimon Schubert 
25165796c8dcSSimon Schubert static void
25175796c8dcSSimon Schubert add_filename_language (char *ext, enum language lang)
25185796c8dcSSimon Schubert {
25195796c8dcSSimon Schubert   if (fl_table_next >= fl_table_size)
25205796c8dcSSimon Schubert     {
25215796c8dcSSimon Schubert       fl_table_size += 10;
25225796c8dcSSimon Schubert       filename_language_table =
25235796c8dcSSimon Schubert 	xrealloc (filename_language_table,
25245796c8dcSSimon Schubert 		  fl_table_size * sizeof (*filename_language_table));
25255796c8dcSSimon Schubert     }
25265796c8dcSSimon Schubert 
25275796c8dcSSimon Schubert   filename_language_table[fl_table_next].ext = xstrdup (ext);
25285796c8dcSSimon Schubert   filename_language_table[fl_table_next].lang = lang;
25295796c8dcSSimon Schubert   fl_table_next++;
25305796c8dcSSimon Schubert }
25315796c8dcSSimon Schubert 
25325796c8dcSSimon Schubert static char *ext_args;
25335796c8dcSSimon Schubert static void
25345796c8dcSSimon Schubert show_ext_args (struct ui_file *file, int from_tty,
25355796c8dcSSimon Schubert 	       struct cmd_list_element *c, const char *value)
25365796c8dcSSimon Schubert {
25375796c8dcSSimon Schubert   fprintf_filtered (file, _("\
25385796c8dcSSimon Schubert Mapping between filename extension and source language is \"%s\".\n"),
25395796c8dcSSimon Schubert 		    value);
25405796c8dcSSimon Schubert }
25415796c8dcSSimon Schubert 
25425796c8dcSSimon Schubert static void
25435796c8dcSSimon Schubert set_ext_lang_command (char *args, int from_tty, struct cmd_list_element *e)
25445796c8dcSSimon Schubert {
25455796c8dcSSimon Schubert   int i;
25465796c8dcSSimon Schubert   char *cp = ext_args;
25475796c8dcSSimon Schubert   enum language lang;
25485796c8dcSSimon Schubert 
25495796c8dcSSimon Schubert   /* First arg is filename extension, starting with '.' */
25505796c8dcSSimon Schubert   if (*cp != '.')
25515796c8dcSSimon Schubert     error (_("'%s': Filename extension must begin with '.'"), ext_args);
25525796c8dcSSimon Schubert 
25535796c8dcSSimon Schubert   /* Find end of first arg.  */
25545796c8dcSSimon Schubert   while (*cp && !isspace (*cp))
25555796c8dcSSimon Schubert     cp++;
25565796c8dcSSimon Schubert 
25575796c8dcSSimon Schubert   if (*cp == '\0')
25585796c8dcSSimon Schubert     error (_("'%s': two arguments required -- filename extension and language"),
25595796c8dcSSimon Schubert 	   ext_args);
25605796c8dcSSimon Schubert 
25615796c8dcSSimon Schubert   /* Null-terminate first arg */
25625796c8dcSSimon Schubert   *cp++ = '\0';
25635796c8dcSSimon Schubert 
25645796c8dcSSimon Schubert   /* Find beginning of second arg, which should be a source language.  */
25655796c8dcSSimon Schubert   while (*cp && isspace (*cp))
25665796c8dcSSimon Schubert     cp++;
25675796c8dcSSimon Schubert 
25685796c8dcSSimon Schubert   if (*cp == '\0')
25695796c8dcSSimon Schubert     error (_("'%s': two arguments required -- filename extension and language"),
25705796c8dcSSimon Schubert 	   ext_args);
25715796c8dcSSimon Schubert 
25725796c8dcSSimon Schubert   /* Lookup the language from among those we know.  */
25735796c8dcSSimon Schubert   lang = language_enum (cp);
25745796c8dcSSimon Schubert 
25755796c8dcSSimon Schubert   /* Now lookup the filename extension: do we already know it?  */
25765796c8dcSSimon Schubert   for (i = 0; i < fl_table_next; i++)
25775796c8dcSSimon Schubert     if (0 == strcmp (ext_args, filename_language_table[i].ext))
25785796c8dcSSimon Schubert       break;
25795796c8dcSSimon Schubert 
25805796c8dcSSimon Schubert   if (i >= fl_table_next)
25815796c8dcSSimon Schubert     {
25825796c8dcSSimon Schubert       /* new file extension */
25835796c8dcSSimon Schubert       add_filename_language (ext_args, lang);
25845796c8dcSSimon Schubert     }
25855796c8dcSSimon Schubert   else
25865796c8dcSSimon Schubert     {
25875796c8dcSSimon Schubert       /* redefining a previously known filename extension */
25885796c8dcSSimon Schubert 
25895796c8dcSSimon Schubert       /* if (from_tty) */
25905796c8dcSSimon Schubert       /*   query ("Really make files of type %s '%s'?", */
25915796c8dcSSimon Schubert       /*          ext_args, language_str (lang));           */
25925796c8dcSSimon Schubert 
25935796c8dcSSimon Schubert       xfree (filename_language_table[i].ext);
25945796c8dcSSimon Schubert       filename_language_table[i].ext = xstrdup (ext_args);
25955796c8dcSSimon Schubert       filename_language_table[i].lang = lang;
25965796c8dcSSimon Schubert     }
25975796c8dcSSimon Schubert }
25985796c8dcSSimon Schubert 
25995796c8dcSSimon Schubert static void
26005796c8dcSSimon Schubert info_ext_lang_command (char *args, int from_tty)
26015796c8dcSSimon Schubert {
26025796c8dcSSimon Schubert   int i;
26035796c8dcSSimon Schubert 
26045796c8dcSSimon Schubert   printf_filtered (_("Filename extensions and the languages they represent:"));
26055796c8dcSSimon Schubert   printf_filtered ("\n\n");
26065796c8dcSSimon Schubert   for (i = 0; i < fl_table_next; i++)
26075796c8dcSSimon Schubert     printf_filtered ("\t%s\t- %s\n",
26085796c8dcSSimon Schubert 		     filename_language_table[i].ext,
26095796c8dcSSimon Schubert 		     language_str (filename_language_table[i].lang));
26105796c8dcSSimon Schubert }
26115796c8dcSSimon Schubert 
26125796c8dcSSimon Schubert static void
26135796c8dcSSimon Schubert init_filename_language_table (void)
26145796c8dcSSimon Schubert {
26155796c8dcSSimon Schubert   if (fl_table_size == 0)	/* protect against repetition */
26165796c8dcSSimon Schubert     {
26175796c8dcSSimon Schubert       fl_table_size = 20;
26185796c8dcSSimon Schubert       fl_table_next = 0;
26195796c8dcSSimon Schubert       filename_language_table =
26205796c8dcSSimon Schubert 	xmalloc (fl_table_size * sizeof (*filename_language_table));
26215796c8dcSSimon Schubert       add_filename_language (".c", language_c);
2622*cf7f2e2dSJohn Marino       add_filename_language (".d", language_d);
26235796c8dcSSimon Schubert       add_filename_language (".C", language_cplus);
26245796c8dcSSimon Schubert       add_filename_language (".cc", language_cplus);
26255796c8dcSSimon Schubert       add_filename_language (".cp", language_cplus);
26265796c8dcSSimon Schubert       add_filename_language (".cpp", language_cplus);
26275796c8dcSSimon Schubert       add_filename_language (".cxx", language_cplus);
26285796c8dcSSimon Schubert       add_filename_language (".c++", language_cplus);
26295796c8dcSSimon Schubert       add_filename_language (".java", language_java);
26305796c8dcSSimon Schubert       add_filename_language (".class", language_java);
26315796c8dcSSimon Schubert       add_filename_language (".m", language_objc);
26325796c8dcSSimon Schubert       add_filename_language (".f", language_fortran);
26335796c8dcSSimon Schubert       add_filename_language (".F", language_fortran);
2634*cf7f2e2dSJohn Marino       add_filename_language (".for", language_fortran);
2635*cf7f2e2dSJohn Marino       add_filename_language (".FOR", language_fortran);
2636*cf7f2e2dSJohn Marino       add_filename_language (".ftn", language_fortran);
2637*cf7f2e2dSJohn Marino       add_filename_language (".FTN", language_fortran);
2638*cf7f2e2dSJohn Marino       add_filename_language (".fpp", language_fortran);
2639*cf7f2e2dSJohn Marino       add_filename_language (".FPP", language_fortran);
2640*cf7f2e2dSJohn Marino       add_filename_language (".f90", language_fortran);
2641*cf7f2e2dSJohn Marino       add_filename_language (".F90", language_fortran);
2642*cf7f2e2dSJohn Marino       add_filename_language (".f95", language_fortran);
2643*cf7f2e2dSJohn Marino       add_filename_language (".F95", language_fortran);
2644*cf7f2e2dSJohn Marino       add_filename_language (".f03", language_fortran);
2645*cf7f2e2dSJohn Marino       add_filename_language (".F03", language_fortran);
2646*cf7f2e2dSJohn Marino       add_filename_language (".f08", language_fortran);
2647*cf7f2e2dSJohn Marino       add_filename_language (".F08", language_fortran);
26485796c8dcSSimon Schubert       add_filename_language (".s", language_asm);
26495796c8dcSSimon Schubert       add_filename_language (".sx", language_asm);
26505796c8dcSSimon Schubert       add_filename_language (".S", language_asm);
26515796c8dcSSimon Schubert       add_filename_language (".pas", language_pascal);
26525796c8dcSSimon Schubert       add_filename_language (".p", language_pascal);
26535796c8dcSSimon Schubert       add_filename_language (".pp", language_pascal);
26545796c8dcSSimon Schubert       add_filename_language (".adb", language_ada);
26555796c8dcSSimon Schubert       add_filename_language (".ads", language_ada);
26565796c8dcSSimon Schubert       add_filename_language (".a", language_ada);
26575796c8dcSSimon Schubert       add_filename_language (".ada", language_ada);
2658*cf7f2e2dSJohn Marino       add_filename_language (".dg", language_ada);
26595796c8dcSSimon Schubert     }
26605796c8dcSSimon Schubert }
26615796c8dcSSimon Schubert 
26625796c8dcSSimon Schubert enum language
26635796c8dcSSimon Schubert deduce_language_from_filename (char *filename)
26645796c8dcSSimon Schubert {
26655796c8dcSSimon Schubert   int i;
26665796c8dcSSimon Schubert   char *cp;
26675796c8dcSSimon Schubert 
26685796c8dcSSimon Schubert   if (filename != NULL)
26695796c8dcSSimon Schubert     if ((cp = strrchr (filename, '.')) != NULL)
26705796c8dcSSimon Schubert       for (i = 0; i < fl_table_next; i++)
26715796c8dcSSimon Schubert 	if (strcmp (cp, filename_language_table[i].ext) == 0)
26725796c8dcSSimon Schubert 	  return filename_language_table[i].lang;
26735796c8dcSSimon Schubert 
26745796c8dcSSimon Schubert   return language_unknown;
26755796c8dcSSimon Schubert }
26765796c8dcSSimon Schubert 
26775796c8dcSSimon Schubert /* allocate_symtab:
26785796c8dcSSimon Schubert 
26795796c8dcSSimon Schubert    Allocate and partly initialize a new symbol table.  Return a pointer
26805796c8dcSSimon Schubert    to it.  error() if no space.
26815796c8dcSSimon Schubert 
26825796c8dcSSimon Schubert    Caller must set these fields:
26835796c8dcSSimon Schubert    LINETABLE(symtab)
26845796c8dcSSimon Schubert    symtab->blockvector
26855796c8dcSSimon Schubert    symtab->dirname
26865796c8dcSSimon Schubert    symtab->free_code
26875796c8dcSSimon Schubert    symtab->free_ptr
26885796c8dcSSimon Schubert  */
26895796c8dcSSimon Schubert 
26905796c8dcSSimon Schubert struct symtab *
26915796c8dcSSimon Schubert allocate_symtab (char *filename, struct objfile *objfile)
26925796c8dcSSimon Schubert {
26935796c8dcSSimon Schubert   struct symtab *symtab;
26945796c8dcSSimon Schubert 
26955796c8dcSSimon Schubert   symtab = (struct symtab *)
26965796c8dcSSimon Schubert     obstack_alloc (&objfile->objfile_obstack, sizeof (struct symtab));
26975796c8dcSSimon Schubert   memset (symtab, 0, sizeof (*symtab));
2698*cf7f2e2dSJohn Marino   symtab->filename = (char *) bcache (filename, strlen (filename) + 1,
2699*cf7f2e2dSJohn Marino 				      objfile->filename_cache);
27005796c8dcSSimon Schubert   symtab->fullname = NULL;
27015796c8dcSSimon Schubert   symtab->language = deduce_language_from_filename (filename);
2702*cf7f2e2dSJohn Marino   symtab->debugformat = "unknown";
27035796c8dcSSimon Schubert 
27045796c8dcSSimon Schubert   /* Hook it to the objfile it comes from */
27055796c8dcSSimon Schubert 
27065796c8dcSSimon Schubert   symtab->objfile = objfile;
27075796c8dcSSimon Schubert   symtab->next = objfile->symtabs;
27085796c8dcSSimon Schubert   objfile->symtabs = symtab;
27095796c8dcSSimon Schubert 
27105796c8dcSSimon Schubert   return (symtab);
27115796c8dcSSimon Schubert }
27125796c8dcSSimon Schubert 
27135796c8dcSSimon Schubert 
27145796c8dcSSimon Schubert /* Reset all data structures in gdb which may contain references to symbol
27155796c8dcSSimon Schubert    table data.  */
27165796c8dcSSimon Schubert 
27175796c8dcSSimon Schubert void
27185796c8dcSSimon Schubert clear_symtab_users (void)
27195796c8dcSSimon Schubert {
27205796c8dcSSimon Schubert   /* Someday, we should do better than this, by only blowing away
27215796c8dcSSimon Schubert      the things that really need to be blown.  */
27225796c8dcSSimon Schubert 
27235796c8dcSSimon Schubert   /* Clear the "current" symtab first, because it is no longer valid.
27245796c8dcSSimon Schubert      breakpoint_re_set may try to access the current symtab.  */
27255796c8dcSSimon Schubert   clear_current_source_symtab_and_line ();
27265796c8dcSSimon Schubert 
27275796c8dcSSimon Schubert   clear_displays ();
27285796c8dcSSimon Schubert   breakpoint_re_set ();
2729*cf7f2e2dSJohn Marino   set_default_breakpoint (0, NULL, 0, 0, 0);
27305796c8dcSSimon Schubert   clear_pc_function_cache ();
27315796c8dcSSimon Schubert   observer_notify_new_objfile (NULL);
27325796c8dcSSimon Schubert 
27335796c8dcSSimon Schubert   /* Clear globals which might have pointed into a removed objfile.
27345796c8dcSSimon Schubert      FIXME: It's not clear which of these are supposed to persist
27355796c8dcSSimon Schubert      between expressions and which ought to be reset each time.  */
27365796c8dcSSimon Schubert   expression_context_block = NULL;
27375796c8dcSSimon Schubert   innermost_block = NULL;
27385796c8dcSSimon Schubert 
27395796c8dcSSimon Schubert   /* Varobj may refer to old symbols, perform a cleanup.  */
27405796c8dcSSimon Schubert   varobj_invalidate ();
27415796c8dcSSimon Schubert 
27425796c8dcSSimon Schubert }
27435796c8dcSSimon Schubert 
27445796c8dcSSimon Schubert static void
27455796c8dcSSimon Schubert clear_symtab_users_cleanup (void *ignore)
27465796c8dcSSimon Schubert {
27475796c8dcSSimon Schubert   clear_symtab_users ();
27485796c8dcSSimon Schubert }
27495796c8dcSSimon Schubert 
27505796c8dcSSimon Schubert /* OVERLAYS:
27515796c8dcSSimon Schubert    The following code implements an abstraction for debugging overlay sections.
27525796c8dcSSimon Schubert 
27535796c8dcSSimon Schubert    The target model is as follows:
27545796c8dcSSimon Schubert    1) The gnu linker will permit multiple sections to be mapped into the
27555796c8dcSSimon Schubert    same VMA, each with its own unique LMA (or load address).
27565796c8dcSSimon Schubert    2) It is assumed that some runtime mechanism exists for mapping the
27575796c8dcSSimon Schubert    sections, one by one, from the load address into the VMA address.
27585796c8dcSSimon Schubert    3) This code provides a mechanism for gdb to keep track of which
27595796c8dcSSimon Schubert    sections should be considered to be mapped from the VMA to the LMA.
27605796c8dcSSimon Schubert    This information is used for symbol lookup, and memory read/write.
27615796c8dcSSimon Schubert    For instance, if a section has been mapped then its contents
27625796c8dcSSimon Schubert    should be read from the VMA, otherwise from the LMA.
27635796c8dcSSimon Schubert 
27645796c8dcSSimon Schubert    Two levels of debugger support for overlays are available.  One is
27655796c8dcSSimon Schubert    "manual", in which the debugger relies on the user to tell it which
27665796c8dcSSimon Schubert    overlays are currently mapped.  This level of support is
27675796c8dcSSimon Schubert    implemented entirely in the core debugger, and the information about
27685796c8dcSSimon Schubert    whether a section is mapped is kept in the objfile->obj_section table.
27695796c8dcSSimon Schubert 
27705796c8dcSSimon Schubert    The second level of support is "automatic", and is only available if
27715796c8dcSSimon Schubert    the target-specific code provides functionality to read the target's
27725796c8dcSSimon Schubert    overlay mapping table, and translate its contents for the debugger
27735796c8dcSSimon Schubert    (by updating the mapped state information in the obj_section tables).
27745796c8dcSSimon Schubert 
27755796c8dcSSimon Schubert    The interface is as follows:
27765796c8dcSSimon Schubert    User commands:
27775796c8dcSSimon Schubert    overlay map <name>   -- tell gdb to consider this section mapped
27785796c8dcSSimon Schubert    overlay unmap <name> -- tell gdb to consider this section unmapped
27795796c8dcSSimon Schubert    overlay list         -- list the sections that GDB thinks are mapped
27805796c8dcSSimon Schubert    overlay read-target  -- get the target's state of what's mapped
27815796c8dcSSimon Schubert    overlay off/manual/auto -- set overlay debugging state
27825796c8dcSSimon Schubert    Functional interface:
27835796c8dcSSimon Schubert    find_pc_mapped_section(pc):    if the pc is in the range of a mapped
27845796c8dcSSimon Schubert    section, return that section.
27855796c8dcSSimon Schubert    find_pc_overlay(pc):       find any overlay section that contains
27865796c8dcSSimon Schubert    the pc, either in its VMA or its LMA
27875796c8dcSSimon Schubert    section_is_mapped(sect):       true if overlay is marked as mapped
27885796c8dcSSimon Schubert    section_is_overlay(sect):      true if section's VMA != LMA
27895796c8dcSSimon Schubert    pc_in_mapped_range(pc,sec):    true if pc belongs to section's VMA
27905796c8dcSSimon Schubert    pc_in_unmapped_range(...):     true if pc belongs to section's LMA
27915796c8dcSSimon Schubert    sections_overlap(sec1, sec2):  true if mapped sec1 and sec2 ranges overlap
27925796c8dcSSimon Schubert    overlay_mapped_address(...):   map an address from section's LMA to VMA
27935796c8dcSSimon Schubert    overlay_unmapped_address(...): map an address from section's VMA to LMA
27945796c8dcSSimon Schubert    symbol_overlayed_address(...): Return a "current" address for symbol:
27955796c8dcSSimon Schubert    either in VMA or LMA depending on whether
27965796c8dcSSimon Schubert    the symbol's section is currently mapped
27975796c8dcSSimon Schubert  */
27985796c8dcSSimon Schubert 
27995796c8dcSSimon Schubert /* Overlay debugging state: */
28005796c8dcSSimon Schubert 
28015796c8dcSSimon Schubert enum overlay_debugging_state overlay_debugging = ovly_off;
28025796c8dcSSimon Schubert int overlay_cache_invalid = 0;	/* True if need to refresh mapped state */
28035796c8dcSSimon Schubert 
28045796c8dcSSimon Schubert /* Function: section_is_overlay (SECTION)
28055796c8dcSSimon Schubert    Returns true if SECTION has VMA not equal to LMA, ie.
28065796c8dcSSimon Schubert    SECTION is loaded at an address different from where it will "run".  */
28075796c8dcSSimon Schubert 
28085796c8dcSSimon Schubert int
28095796c8dcSSimon Schubert section_is_overlay (struct obj_section *section)
28105796c8dcSSimon Schubert {
28115796c8dcSSimon Schubert   if (overlay_debugging && section)
28125796c8dcSSimon Schubert     {
28135796c8dcSSimon Schubert       bfd *abfd = section->objfile->obfd;
28145796c8dcSSimon Schubert       asection *bfd_section = section->the_bfd_section;
28155796c8dcSSimon Schubert 
28165796c8dcSSimon Schubert       if (bfd_section_lma (abfd, bfd_section) != 0
28175796c8dcSSimon Schubert 	  && bfd_section_lma (abfd, bfd_section)
28185796c8dcSSimon Schubert 	     != bfd_section_vma (abfd, bfd_section))
28195796c8dcSSimon Schubert 	return 1;
28205796c8dcSSimon Schubert     }
28215796c8dcSSimon Schubert 
28225796c8dcSSimon Schubert   return 0;
28235796c8dcSSimon Schubert }
28245796c8dcSSimon Schubert 
28255796c8dcSSimon Schubert /* Function: overlay_invalidate_all (void)
28265796c8dcSSimon Schubert    Invalidate the mapped state of all overlay sections (mark it as stale).  */
28275796c8dcSSimon Schubert 
28285796c8dcSSimon Schubert static void
28295796c8dcSSimon Schubert overlay_invalidate_all (void)
28305796c8dcSSimon Schubert {
28315796c8dcSSimon Schubert   struct objfile *objfile;
28325796c8dcSSimon Schubert   struct obj_section *sect;
28335796c8dcSSimon Schubert 
28345796c8dcSSimon Schubert   ALL_OBJSECTIONS (objfile, sect)
28355796c8dcSSimon Schubert     if (section_is_overlay (sect))
28365796c8dcSSimon Schubert       sect->ovly_mapped = -1;
28375796c8dcSSimon Schubert }
28385796c8dcSSimon Schubert 
28395796c8dcSSimon Schubert /* Function: section_is_mapped (SECTION)
28405796c8dcSSimon Schubert    Returns true if section is an overlay, and is currently mapped.
28415796c8dcSSimon Schubert 
28425796c8dcSSimon Schubert    Access to the ovly_mapped flag is restricted to this function, so
28435796c8dcSSimon Schubert    that we can do automatic update.  If the global flag
28445796c8dcSSimon Schubert    OVERLAY_CACHE_INVALID is set (by wait_for_inferior), then call
28455796c8dcSSimon Schubert    overlay_invalidate_all.  If the mapped state of the particular
28465796c8dcSSimon Schubert    section is stale, then call TARGET_OVERLAY_UPDATE to refresh it.  */
28475796c8dcSSimon Schubert 
28485796c8dcSSimon Schubert int
28495796c8dcSSimon Schubert section_is_mapped (struct obj_section *osect)
28505796c8dcSSimon Schubert {
28515796c8dcSSimon Schubert   struct gdbarch *gdbarch;
28525796c8dcSSimon Schubert 
28535796c8dcSSimon Schubert   if (osect == 0 || !section_is_overlay (osect))
28545796c8dcSSimon Schubert     return 0;
28555796c8dcSSimon Schubert 
28565796c8dcSSimon Schubert   switch (overlay_debugging)
28575796c8dcSSimon Schubert     {
28585796c8dcSSimon Schubert     default:
28595796c8dcSSimon Schubert     case ovly_off:
28605796c8dcSSimon Schubert       return 0;			/* overlay debugging off */
28615796c8dcSSimon Schubert     case ovly_auto:		/* overlay debugging automatic */
28625796c8dcSSimon Schubert       /* Unles there is a gdbarch_overlay_update function,
28635796c8dcSSimon Schubert          there's really nothing useful to do here (can't really go auto)  */
28645796c8dcSSimon Schubert       gdbarch = get_objfile_arch (osect->objfile);
28655796c8dcSSimon Schubert       if (gdbarch_overlay_update_p (gdbarch))
28665796c8dcSSimon Schubert 	{
28675796c8dcSSimon Schubert 	  if (overlay_cache_invalid)
28685796c8dcSSimon Schubert 	    {
28695796c8dcSSimon Schubert 	      overlay_invalidate_all ();
28705796c8dcSSimon Schubert 	      overlay_cache_invalid = 0;
28715796c8dcSSimon Schubert 	    }
28725796c8dcSSimon Schubert 	  if (osect->ovly_mapped == -1)
28735796c8dcSSimon Schubert 	    gdbarch_overlay_update (gdbarch, osect);
28745796c8dcSSimon Schubert 	}
28755796c8dcSSimon Schubert       /* fall thru to manual case */
28765796c8dcSSimon Schubert     case ovly_on:		/* overlay debugging manual */
28775796c8dcSSimon Schubert       return osect->ovly_mapped == 1;
28785796c8dcSSimon Schubert     }
28795796c8dcSSimon Schubert }
28805796c8dcSSimon Schubert 
28815796c8dcSSimon Schubert /* Function: pc_in_unmapped_range
28825796c8dcSSimon Schubert    If PC falls into the lma range of SECTION, return true, else false.  */
28835796c8dcSSimon Schubert 
28845796c8dcSSimon Schubert CORE_ADDR
28855796c8dcSSimon Schubert pc_in_unmapped_range (CORE_ADDR pc, struct obj_section *section)
28865796c8dcSSimon Schubert {
28875796c8dcSSimon Schubert   if (section_is_overlay (section))
28885796c8dcSSimon Schubert     {
28895796c8dcSSimon Schubert       bfd *abfd = section->objfile->obfd;
28905796c8dcSSimon Schubert       asection *bfd_section = section->the_bfd_section;
28915796c8dcSSimon Schubert 
28925796c8dcSSimon Schubert       /* We assume the LMA is relocated by the same offset as the VMA.  */
28935796c8dcSSimon Schubert       bfd_vma size = bfd_get_section_size (bfd_section);
28945796c8dcSSimon Schubert       CORE_ADDR offset = obj_section_offset (section);
28955796c8dcSSimon Schubert 
28965796c8dcSSimon Schubert       if (bfd_get_section_lma (abfd, bfd_section) + offset <= pc
28975796c8dcSSimon Schubert 	  && pc < bfd_get_section_lma (abfd, bfd_section) + offset + size)
28985796c8dcSSimon Schubert 	return 1;
28995796c8dcSSimon Schubert     }
29005796c8dcSSimon Schubert 
29015796c8dcSSimon Schubert   return 0;
29025796c8dcSSimon Schubert }
29035796c8dcSSimon Schubert 
29045796c8dcSSimon Schubert /* Function: pc_in_mapped_range
29055796c8dcSSimon Schubert    If PC falls into the vma range of SECTION, return true, else false.  */
29065796c8dcSSimon Schubert 
29075796c8dcSSimon Schubert CORE_ADDR
29085796c8dcSSimon Schubert pc_in_mapped_range (CORE_ADDR pc, struct obj_section *section)
29095796c8dcSSimon Schubert {
29105796c8dcSSimon Schubert   if (section_is_overlay (section))
29115796c8dcSSimon Schubert     {
29125796c8dcSSimon Schubert       if (obj_section_addr (section) <= pc
29135796c8dcSSimon Schubert 	  && pc < obj_section_endaddr (section))
29145796c8dcSSimon Schubert 	return 1;
29155796c8dcSSimon Schubert     }
29165796c8dcSSimon Schubert 
29175796c8dcSSimon Schubert   return 0;
29185796c8dcSSimon Schubert }
29195796c8dcSSimon Schubert 
29205796c8dcSSimon Schubert 
29215796c8dcSSimon Schubert /* Return true if the mapped ranges of sections A and B overlap, false
29225796c8dcSSimon Schubert    otherwise.  */
29235796c8dcSSimon Schubert static int
29245796c8dcSSimon Schubert sections_overlap (struct obj_section *a, struct obj_section *b)
29255796c8dcSSimon Schubert {
29265796c8dcSSimon Schubert   CORE_ADDR a_start = obj_section_addr (a);
29275796c8dcSSimon Schubert   CORE_ADDR a_end = obj_section_endaddr (a);
29285796c8dcSSimon Schubert   CORE_ADDR b_start = obj_section_addr (b);
29295796c8dcSSimon Schubert   CORE_ADDR b_end = obj_section_endaddr (b);
29305796c8dcSSimon Schubert 
29315796c8dcSSimon Schubert   return (a_start < b_end && b_start < a_end);
29325796c8dcSSimon Schubert }
29335796c8dcSSimon Schubert 
29345796c8dcSSimon Schubert /* Function: overlay_unmapped_address (PC, SECTION)
29355796c8dcSSimon Schubert    Returns the address corresponding to PC in the unmapped (load) range.
29365796c8dcSSimon Schubert    May be the same as PC.  */
29375796c8dcSSimon Schubert 
29385796c8dcSSimon Schubert CORE_ADDR
29395796c8dcSSimon Schubert overlay_unmapped_address (CORE_ADDR pc, struct obj_section *section)
29405796c8dcSSimon Schubert {
29415796c8dcSSimon Schubert   if (section_is_overlay (section) && pc_in_mapped_range (pc, section))
29425796c8dcSSimon Schubert     {
29435796c8dcSSimon Schubert       bfd *abfd = section->objfile->obfd;
29445796c8dcSSimon Schubert       asection *bfd_section = section->the_bfd_section;
29455796c8dcSSimon Schubert 
29465796c8dcSSimon Schubert       return pc + bfd_section_lma (abfd, bfd_section)
29475796c8dcSSimon Schubert 		- bfd_section_vma (abfd, bfd_section);
29485796c8dcSSimon Schubert     }
29495796c8dcSSimon Schubert 
29505796c8dcSSimon Schubert   return pc;
29515796c8dcSSimon Schubert }
29525796c8dcSSimon Schubert 
29535796c8dcSSimon Schubert /* Function: overlay_mapped_address (PC, SECTION)
29545796c8dcSSimon Schubert    Returns the address corresponding to PC in the mapped (runtime) range.
29555796c8dcSSimon Schubert    May be the same as PC.  */
29565796c8dcSSimon Schubert 
29575796c8dcSSimon Schubert CORE_ADDR
29585796c8dcSSimon Schubert overlay_mapped_address (CORE_ADDR pc, struct obj_section *section)
29595796c8dcSSimon Schubert {
29605796c8dcSSimon Schubert   if (section_is_overlay (section) && pc_in_unmapped_range (pc, section))
29615796c8dcSSimon Schubert     {
29625796c8dcSSimon Schubert       bfd *abfd = section->objfile->obfd;
29635796c8dcSSimon Schubert       asection *bfd_section = section->the_bfd_section;
29645796c8dcSSimon Schubert 
29655796c8dcSSimon Schubert       return pc + bfd_section_vma (abfd, bfd_section)
29665796c8dcSSimon Schubert 		- bfd_section_lma (abfd, bfd_section);
29675796c8dcSSimon Schubert     }
29685796c8dcSSimon Schubert 
29695796c8dcSSimon Schubert   return pc;
29705796c8dcSSimon Schubert }
29715796c8dcSSimon Schubert 
29725796c8dcSSimon Schubert 
29735796c8dcSSimon Schubert /* Function: symbol_overlayed_address
29745796c8dcSSimon Schubert    Return one of two addresses (relative to the VMA or to the LMA),
29755796c8dcSSimon Schubert    depending on whether the section is mapped or not.  */
29765796c8dcSSimon Schubert 
29775796c8dcSSimon Schubert CORE_ADDR
29785796c8dcSSimon Schubert symbol_overlayed_address (CORE_ADDR address, struct obj_section *section)
29795796c8dcSSimon Schubert {
29805796c8dcSSimon Schubert   if (overlay_debugging)
29815796c8dcSSimon Schubert     {
29825796c8dcSSimon Schubert       /* If the symbol has no section, just return its regular address. */
29835796c8dcSSimon Schubert       if (section == 0)
29845796c8dcSSimon Schubert 	return address;
29855796c8dcSSimon Schubert       /* If the symbol's section is not an overlay, just return its address */
29865796c8dcSSimon Schubert       if (!section_is_overlay (section))
29875796c8dcSSimon Schubert 	return address;
29885796c8dcSSimon Schubert       /* If the symbol's section is mapped, just return its address */
29895796c8dcSSimon Schubert       if (section_is_mapped (section))
29905796c8dcSSimon Schubert 	return address;
29915796c8dcSSimon Schubert       /*
29925796c8dcSSimon Schubert        * HOWEVER: if the symbol is in an overlay section which is NOT mapped,
29935796c8dcSSimon Schubert        * then return its LOADED address rather than its vma address!!
29945796c8dcSSimon Schubert        */
29955796c8dcSSimon Schubert       return overlay_unmapped_address (address, section);
29965796c8dcSSimon Schubert     }
29975796c8dcSSimon Schubert   return address;
29985796c8dcSSimon Schubert }
29995796c8dcSSimon Schubert 
30005796c8dcSSimon Schubert /* Function: find_pc_overlay (PC)
30015796c8dcSSimon Schubert    Return the best-match overlay section for PC:
30025796c8dcSSimon Schubert    If PC matches a mapped overlay section's VMA, return that section.
30035796c8dcSSimon Schubert    Else if PC matches an unmapped section's VMA, return that section.
30045796c8dcSSimon Schubert    Else if PC matches an unmapped section's LMA, return that section.  */
30055796c8dcSSimon Schubert 
30065796c8dcSSimon Schubert struct obj_section *
30075796c8dcSSimon Schubert find_pc_overlay (CORE_ADDR pc)
30085796c8dcSSimon Schubert {
30095796c8dcSSimon Schubert   struct objfile *objfile;
30105796c8dcSSimon Schubert   struct obj_section *osect, *best_match = NULL;
30115796c8dcSSimon Schubert 
30125796c8dcSSimon Schubert   if (overlay_debugging)
30135796c8dcSSimon Schubert     ALL_OBJSECTIONS (objfile, osect)
30145796c8dcSSimon Schubert       if (section_is_overlay (osect))
30155796c8dcSSimon Schubert       {
30165796c8dcSSimon Schubert 	if (pc_in_mapped_range (pc, osect))
30175796c8dcSSimon Schubert 	  {
30185796c8dcSSimon Schubert 	    if (section_is_mapped (osect))
30195796c8dcSSimon Schubert 	      return osect;
30205796c8dcSSimon Schubert 	    else
30215796c8dcSSimon Schubert 	      best_match = osect;
30225796c8dcSSimon Schubert 	  }
30235796c8dcSSimon Schubert 	else if (pc_in_unmapped_range (pc, osect))
30245796c8dcSSimon Schubert 	  best_match = osect;
30255796c8dcSSimon Schubert       }
30265796c8dcSSimon Schubert   return best_match;
30275796c8dcSSimon Schubert }
30285796c8dcSSimon Schubert 
30295796c8dcSSimon Schubert /* Function: find_pc_mapped_section (PC)
30305796c8dcSSimon Schubert    If PC falls into the VMA address range of an overlay section that is
30315796c8dcSSimon Schubert    currently marked as MAPPED, return that section.  Else return NULL.  */
30325796c8dcSSimon Schubert 
30335796c8dcSSimon Schubert struct obj_section *
30345796c8dcSSimon Schubert find_pc_mapped_section (CORE_ADDR pc)
30355796c8dcSSimon Schubert {
30365796c8dcSSimon Schubert   struct objfile *objfile;
30375796c8dcSSimon Schubert   struct obj_section *osect;
30385796c8dcSSimon Schubert 
30395796c8dcSSimon Schubert   if (overlay_debugging)
30405796c8dcSSimon Schubert     ALL_OBJSECTIONS (objfile, osect)
30415796c8dcSSimon Schubert       if (pc_in_mapped_range (pc, osect) && section_is_mapped (osect))
30425796c8dcSSimon Schubert 	return osect;
30435796c8dcSSimon Schubert 
30445796c8dcSSimon Schubert   return NULL;
30455796c8dcSSimon Schubert }
30465796c8dcSSimon Schubert 
30475796c8dcSSimon Schubert /* Function: list_overlays_command
30485796c8dcSSimon Schubert    Print a list of mapped sections and their PC ranges */
30495796c8dcSSimon Schubert 
30505796c8dcSSimon Schubert void
30515796c8dcSSimon Schubert list_overlays_command (char *args, int from_tty)
30525796c8dcSSimon Schubert {
30535796c8dcSSimon Schubert   int nmapped = 0;
30545796c8dcSSimon Schubert   struct objfile *objfile;
30555796c8dcSSimon Schubert   struct obj_section *osect;
30565796c8dcSSimon Schubert 
30575796c8dcSSimon Schubert   if (overlay_debugging)
30585796c8dcSSimon Schubert     ALL_OBJSECTIONS (objfile, osect)
30595796c8dcSSimon Schubert       if (section_is_mapped (osect))
30605796c8dcSSimon Schubert       {
30615796c8dcSSimon Schubert 	struct gdbarch *gdbarch = get_objfile_arch (objfile);
30625796c8dcSSimon Schubert 	const char *name;
30635796c8dcSSimon Schubert 	bfd_vma lma, vma;
30645796c8dcSSimon Schubert 	int size;
30655796c8dcSSimon Schubert 
30665796c8dcSSimon Schubert 	vma = bfd_section_vma (objfile->obfd, osect->the_bfd_section);
30675796c8dcSSimon Schubert 	lma = bfd_section_lma (objfile->obfd, osect->the_bfd_section);
30685796c8dcSSimon Schubert 	size = bfd_get_section_size (osect->the_bfd_section);
30695796c8dcSSimon Schubert 	name = bfd_section_name (objfile->obfd, osect->the_bfd_section);
30705796c8dcSSimon Schubert 
30715796c8dcSSimon Schubert 	printf_filtered ("Section %s, loaded at ", name);
30725796c8dcSSimon Schubert 	fputs_filtered (paddress (gdbarch, lma), gdb_stdout);
30735796c8dcSSimon Schubert 	puts_filtered (" - ");
30745796c8dcSSimon Schubert 	fputs_filtered (paddress (gdbarch, lma + size), gdb_stdout);
30755796c8dcSSimon Schubert 	printf_filtered (", mapped at ");
30765796c8dcSSimon Schubert 	fputs_filtered (paddress (gdbarch, vma), gdb_stdout);
30775796c8dcSSimon Schubert 	puts_filtered (" - ");
30785796c8dcSSimon Schubert 	fputs_filtered (paddress (gdbarch, vma + size), gdb_stdout);
30795796c8dcSSimon Schubert 	puts_filtered ("\n");
30805796c8dcSSimon Schubert 
30815796c8dcSSimon Schubert 	nmapped++;
30825796c8dcSSimon Schubert       }
30835796c8dcSSimon Schubert   if (nmapped == 0)
30845796c8dcSSimon Schubert     printf_filtered (_("No sections are mapped.\n"));
30855796c8dcSSimon Schubert }
30865796c8dcSSimon Schubert 
30875796c8dcSSimon Schubert /* Function: map_overlay_command
30885796c8dcSSimon Schubert    Mark the named section as mapped (ie. residing at its VMA address).  */
30895796c8dcSSimon Schubert 
30905796c8dcSSimon Schubert void
30915796c8dcSSimon Schubert map_overlay_command (char *args, int from_tty)
30925796c8dcSSimon Schubert {
30935796c8dcSSimon Schubert   struct objfile *objfile, *objfile2;
30945796c8dcSSimon Schubert   struct obj_section *sec, *sec2;
30955796c8dcSSimon Schubert 
30965796c8dcSSimon Schubert   if (!overlay_debugging)
30975796c8dcSSimon Schubert     error (_("\
30985796c8dcSSimon Schubert Overlay debugging not enabled.  Use either the 'overlay auto' or\n\
30995796c8dcSSimon Schubert the 'overlay manual' command."));
31005796c8dcSSimon Schubert 
31015796c8dcSSimon Schubert   if (args == 0 || *args == 0)
31025796c8dcSSimon Schubert     error (_("Argument required: name of an overlay section"));
31035796c8dcSSimon Schubert 
31045796c8dcSSimon Schubert   /* First, find a section matching the user supplied argument */
31055796c8dcSSimon Schubert   ALL_OBJSECTIONS (objfile, sec)
31065796c8dcSSimon Schubert     if (!strcmp (bfd_section_name (objfile->obfd, sec->the_bfd_section), args))
31075796c8dcSSimon Schubert     {
31085796c8dcSSimon Schubert       /* Now, check to see if the section is an overlay. */
31095796c8dcSSimon Schubert       if (!section_is_overlay (sec))
31105796c8dcSSimon Schubert 	continue;		/* not an overlay section */
31115796c8dcSSimon Schubert 
31125796c8dcSSimon Schubert       /* Mark the overlay as "mapped" */
31135796c8dcSSimon Schubert       sec->ovly_mapped = 1;
31145796c8dcSSimon Schubert 
31155796c8dcSSimon Schubert       /* Next, make a pass and unmap any sections that are
31165796c8dcSSimon Schubert          overlapped by this new section: */
31175796c8dcSSimon Schubert       ALL_OBJSECTIONS (objfile2, sec2)
31185796c8dcSSimon Schubert 	if (sec2->ovly_mapped && sec != sec2 && sections_overlap (sec, sec2))
31195796c8dcSSimon Schubert 	{
31205796c8dcSSimon Schubert 	  if (info_verbose)
31215796c8dcSSimon Schubert 	    printf_unfiltered (_("Note: section %s unmapped by overlap\n"),
31225796c8dcSSimon Schubert 			     bfd_section_name (objfile->obfd,
31235796c8dcSSimon Schubert 					       sec2->the_bfd_section));
31245796c8dcSSimon Schubert 	  sec2->ovly_mapped = 0;	/* sec2 overlaps sec: unmap sec2 */
31255796c8dcSSimon Schubert 	}
31265796c8dcSSimon Schubert       return;
31275796c8dcSSimon Schubert     }
31285796c8dcSSimon Schubert   error (_("No overlay section called %s"), args);
31295796c8dcSSimon Schubert }
31305796c8dcSSimon Schubert 
31315796c8dcSSimon Schubert /* Function: unmap_overlay_command
31325796c8dcSSimon Schubert    Mark the overlay section as unmapped
31335796c8dcSSimon Schubert    (ie. resident in its LMA address range, rather than the VMA range).  */
31345796c8dcSSimon Schubert 
31355796c8dcSSimon Schubert void
31365796c8dcSSimon Schubert unmap_overlay_command (char *args, int from_tty)
31375796c8dcSSimon Schubert {
31385796c8dcSSimon Schubert   struct objfile *objfile;
31395796c8dcSSimon Schubert   struct obj_section *sec;
31405796c8dcSSimon Schubert 
31415796c8dcSSimon Schubert   if (!overlay_debugging)
31425796c8dcSSimon Schubert     error (_("\
31435796c8dcSSimon Schubert Overlay debugging not enabled.  Use either the 'overlay auto' or\n\
31445796c8dcSSimon Schubert the 'overlay manual' command."));
31455796c8dcSSimon Schubert 
31465796c8dcSSimon Schubert   if (args == 0 || *args == 0)
31475796c8dcSSimon Schubert     error (_("Argument required: name of an overlay section"));
31485796c8dcSSimon Schubert 
31495796c8dcSSimon Schubert   /* First, find a section matching the user supplied argument */
31505796c8dcSSimon Schubert   ALL_OBJSECTIONS (objfile, sec)
31515796c8dcSSimon Schubert     if (!strcmp (bfd_section_name (objfile->obfd, sec->the_bfd_section), args))
31525796c8dcSSimon Schubert     {
31535796c8dcSSimon Schubert       if (!sec->ovly_mapped)
31545796c8dcSSimon Schubert 	error (_("Section %s is not mapped"), args);
31555796c8dcSSimon Schubert       sec->ovly_mapped = 0;
31565796c8dcSSimon Schubert       return;
31575796c8dcSSimon Schubert     }
31585796c8dcSSimon Schubert   error (_("No overlay section called %s"), args);
31595796c8dcSSimon Schubert }
31605796c8dcSSimon Schubert 
31615796c8dcSSimon Schubert /* Function: overlay_auto_command
31625796c8dcSSimon Schubert    A utility command to turn on overlay debugging.
31635796c8dcSSimon Schubert    Possibly this should be done via a set/show command. */
31645796c8dcSSimon Schubert 
31655796c8dcSSimon Schubert static void
31665796c8dcSSimon Schubert overlay_auto_command (char *args, int from_tty)
31675796c8dcSSimon Schubert {
31685796c8dcSSimon Schubert   overlay_debugging = ovly_auto;
31695796c8dcSSimon Schubert   enable_overlay_breakpoints ();
31705796c8dcSSimon Schubert   if (info_verbose)
31715796c8dcSSimon Schubert     printf_unfiltered (_("Automatic overlay debugging enabled."));
31725796c8dcSSimon Schubert }
31735796c8dcSSimon Schubert 
31745796c8dcSSimon Schubert /* Function: overlay_manual_command
31755796c8dcSSimon Schubert    A utility command to turn on overlay debugging.
31765796c8dcSSimon Schubert    Possibly this should be done via a set/show command. */
31775796c8dcSSimon Schubert 
31785796c8dcSSimon Schubert static void
31795796c8dcSSimon Schubert overlay_manual_command (char *args, int from_tty)
31805796c8dcSSimon Schubert {
31815796c8dcSSimon Schubert   overlay_debugging = ovly_on;
31825796c8dcSSimon Schubert   disable_overlay_breakpoints ();
31835796c8dcSSimon Schubert   if (info_verbose)
31845796c8dcSSimon Schubert     printf_unfiltered (_("Overlay debugging enabled."));
31855796c8dcSSimon Schubert }
31865796c8dcSSimon Schubert 
31875796c8dcSSimon Schubert /* Function: overlay_off_command
31885796c8dcSSimon Schubert    A utility command to turn on overlay debugging.
31895796c8dcSSimon Schubert    Possibly this should be done via a set/show command. */
31905796c8dcSSimon Schubert 
31915796c8dcSSimon Schubert static void
31925796c8dcSSimon Schubert overlay_off_command (char *args, int from_tty)
31935796c8dcSSimon Schubert {
31945796c8dcSSimon Schubert   overlay_debugging = ovly_off;
31955796c8dcSSimon Schubert   disable_overlay_breakpoints ();
31965796c8dcSSimon Schubert   if (info_verbose)
31975796c8dcSSimon Schubert     printf_unfiltered (_("Overlay debugging disabled."));
31985796c8dcSSimon Schubert }
31995796c8dcSSimon Schubert 
32005796c8dcSSimon Schubert static void
32015796c8dcSSimon Schubert overlay_load_command (char *args, int from_tty)
32025796c8dcSSimon Schubert {
32035796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_current_arch ();
32045796c8dcSSimon Schubert 
32055796c8dcSSimon Schubert   if (gdbarch_overlay_update_p (gdbarch))
32065796c8dcSSimon Schubert     gdbarch_overlay_update (gdbarch, NULL);
32075796c8dcSSimon Schubert   else
32085796c8dcSSimon Schubert     error (_("This target does not know how to read its overlay state."));
32095796c8dcSSimon Schubert }
32105796c8dcSSimon Schubert 
32115796c8dcSSimon Schubert /* Function: overlay_command
32125796c8dcSSimon Schubert    A place-holder for a mis-typed command */
32135796c8dcSSimon Schubert 
32145796c8dcSSimon Schubert /* Command list chain containing all defined "overlay" subcommands. */
32155796c8dcSSimon Schubert struct cmd_list_element *overlaylist;
32165796c8dcSSimon Schubert 
32175796c8dcSSimon Schubert static void
32185796c8dcSSimon Schubert overlay_command (char *args, int from_tty)
32195796c8dcSSimon Schubert {
32205796c8dcSSimon Schubert   printf_unfiltered
32215796c8dcSSimon Schubert     ("\"overlay\" must be followed by the name of an overlay command.\n");
32225796c8dcSSimon Schubert   help_list (overlaylist, "overlay ", -1, gdb_stdout);
32235796c8dcSSimon Schubert }
32245796c8dcSSimon Schubert 
32255796c8dcSSimon Schubert 
32265796c8dcSSimon Schubert /* Target Overlays for the "Simplest" overlay manager:
32275796c8dcSSimon Schubert 
32285796c8dcSSimon Schubert    This is GDB's default target overlay layer.  It works with the
32295796c8dcSSimon Schubert    minimal overlay manager supplied as an example by Cygnus.  The
32305796c8dcSSimon Schubert    entry point is via a function pointer "gdbarch_overlay_update",
32315796c8dcSSimon Schubert    so targets that use a different runtime overlay manager can
32325796c8dcSSimon Schubert    substitute their own overlay_update function and take over the
32335796c8dcSSimon Schubert    function pointer.
32345796c8dcSSimon Schubert 
32355796c8dcSSimon Schubert    The overlay_update function pokes around in the target's data structures
32365796c8dcSSimon Schubert    to see what overlays are mapped, and updates GDB's overlay mapping with
32375796c8dcSSimon Schubert    this information.
32385796c8dcSSimon Schubert 
32395796c8dcSSimon Schubert    In this simple implementation, the target data structures are as follows:
32405796c8dcSSimon Schubert    unsigned _novlys;            /# number of overlay sections #/
32415796c8dcSSimon Schubert    unsigned _ovly_table[_novlys][4] = {
32425796c8dcSSimon Schubert    {VMA, SIZE, LMA, MAPPED},    /# one entry per overlay section #/
32435796c8dcSSimon Schubert    {..., ...,  ..., ...},
32445796c8dcSSimon Schubert    }
32455796c8dcSSimon Schubert    unsigned _novly_regions;     /# number of overlay regions #/
32465796c8dcSSimon Schubert    unsigned _ovly_region_table[_novly_regions][3] = {
32475796c8dcSSimon Schubert    {VMA, SIZE, MAPPED_TO_LMA},  /# one entry per overlay region #/
32485796c8dcSSimon Schubert    {..., ...,  ...},
32495796c8dcSSimon Schubert    }
32505796c8dcSSimon Schubert    These functions will attempt to update GDB's mappedness state in the
32515796c8dcSSimon Schubert    symbol section table, based on the target's mappedness state.
32525796c8dcSSimon Schubert 
32535796c8dcSSimon Schubert    To do this, we keep a cached copy of the target's _ovly_table, and
32545796c8dcSSimon Schubert    attempt to detect when the cached copy is invalidated.  The main
32555796c8dcSSimon Schubert    entry point is "simple_overlay_update(SECT), which looks up SECT in
32565796c8dcSSimon Schubert    the cached table and re-reads only the entry for that section from
32575796c8dcSSimon Schubert    the target (whenever possible).
32585796c8dcSSimon Schubert  */
32595796c8dcSSimon Schubert 
32605796c8dcSSimon Schubert /* Cached, dynamically allocated copies of the target data structures: */
32615796c8dcSSimon Schubert static unsigned (*cache_ovly_table)[4] = 0;
32625796c8dcSSimon Schubert #if 0
32635796c8dcSSimon Schubert static unsigned (*cache_ovly_region_table)[3] = 0;
32645796c8dcSSimon Schubert #endif
32655796c8dcSSimon Schubert static unsigned cache_novlys = 0;
32665796c8dcSSimon Schubert #if 0
32675796c8dcSSimon Schubert static unsigned cache_novly_regions = 0;
32685796c8dcSSimon Schubert #endif
32695796c8dcSSimon Schubert static CORE_ADDR cache_ovly_table_base = 0;
32705796c8dcSSimon Schubert #if 0
32715796c8dcSSimon Schubert static CORE_ADDR cache_ovly_region_table_base = 0;
32725796c8dcSSimon Schubert #endif
32735796c8dcSSimon Schubert enum ovly_index
32745796c8dcSSimon Schubert   {
32755796c8dcSSimon Schubert     VMA, SIZE, LMA, MAPPED
32765796c8dcSSimon Schubert   };
32775796c8dcSSimon Schubert 
32785796c8dcSSimon Schubert /* Throw away the cached copy of _ovly_table */
32795796c8dcSSimon Schubert static void
32805796c8dcSSimon Schubert simple_free_overlay_table (void)
32815796c8dcSSimon Schubert {
32825796c8dcSSimon Schubert   if (cache_ovly_table)
32835796c8dcSSimon Schubert     xfree (cache_ovly_table);
32845796c8dcSSimon Schubert   cache_novlys = 0;
32855796c8dcSSimon Schubert   cache_ovly_table = NULL;
32865796c8dcSSimon Schubert   cache_ovly_table_base = 0;
32875796c8dcSSimon Schubert }
32885796c8dcSSimon Schubert 
32895796c8dcSSimon Schubert #if 0
32905796c8dcSSimon Schubert /* Throw away the cached copy of _ovly_region_table */
32915796c8dcSSimon Schubert static void
32925796c8dcSSimon Schubert simple_free_overlay_region_table (void)
32935796c8dcSSimon Schubert {
32945796c8dcSSimon Schubert   if (cache_ovly_region_table)
32955796c8dcSSimon Schubert     xfree (cache_ovly_region_table);
32965796c8dcSSimon Schubert   cache_novly_regions = 0;
32975796c8dcSSimon Schubert   cache_ovly_region_table = NULL;
32985796c8dcSSimon Schubert   cache_ovly_region_table_base = 0;
32995796c8dcSSimon Schubert }
33005796c8dcSSimon Schubert #endif
33015796c8dcSSimon Schubert 
33025796c8dcSSimon Schubert /* Read an array of ints of size SIZE from the target into a local buffer.
33035796c8dcSSimon Schubert    Convert to host order.  int LEN is number of ints  */
33045796c8dcSSimon Schubert static void
33055796c8dcSSimon Schubert read_target_long_array (CORE_ADDR memaddr, unsigned int *myaddr,
33065796c8dcSSimon Schubert 			int len, int size, enum bfd_endian byte_order)
33075796c8dcSSimon Schubert {
33085796c8dcSSimon Schubert   /* FIXME (alloca): Not safe if array is very large. */
33095796c8dcSSimon Schubert   gdb_byte *buf = alloca (len * size);
33105796c8dcSSimon Schubert   int i;
33115796c8dcSSimon Schubert 
33125796c8dcSSimon Schubert   read_memory (memaddr, buf, len * size);
33135796c8dcSSimon Schubert   for (i = 0; i < len; i++)
33145796c8dcSSimon Schubert     myaddr[i] = extract_unsigned_integer (size * i + buf, size, byte_order);
33155796c8dcSSimon Schubert }
33165796c8dcSSimon Schubert 
33175796c8dcSSimon Schubert /* Find and grab a copy of the target _ovly_table
33185796c8dcSSimon Schubert    (and _novlys, which is needed for the table's size) */
33195796c8dcSSimon Schubert static int
33205796c8dcSSimon Schubert simple_read_overlay_table (void)
33215796c8dcSSimon Schubert {
33225796c8dcSSimon Schubert   struct minimal_symbol *novlys_msym, *ovly_table_msym;
33235796c8dcSSimon Schubert   struct gdbarch *gdbarch;
33245796c8dcSSimon Schubert   int word_size;
33255796c8dcSSimon Schubert   enum bfd_endian byte_order;
33265796c8dcSSimon Schubert 
33275796c8dcSSimon Schubert   simple_free_overlay_table ();
33285796c8dcSSimon Schubert   novlys_msym = lookup_minimal_symbol ("_novlys", NULL, NULL);
33295796c8dcSSimon Schubert   if (! novlys_msym)
33305796c8dcSSimon Schubert     {
33315796c8dcSSimon Schubert       error (_("Error reading inferior's overlay table: "
33325796c8dcSSimon Schubert              "couldn't find `_novlys' variable\n"
33335796c8dcSSimon Schubert              "in inferior.  Use `overlay manual' mode."));
33345796c8dcSSimon Schubert       return 0;
33355796c8dcSSimon Schubert     }
33365796c8dcSSimon Schubert 
33375796c8dcSSimon Schubert   ovly_table_msym = lookup_minimal_symbol ("_ovly_table", NULL, NULL);
33385796c8dcSSimon Schubert   if (! ovly_table_msym)
33395796c8dcSSimon Schubert     {
33405796c8dcSSimon Schubert       error (_("Error reading inferior's overlay table: couldn't find "
33415796c8dcSSimon Schubert              "`_ovly_table' array\n"
33425796c8dcSSimon Schubert              "in inferior.  Use `overlay manual' mode."));
33435796c8dcSSimon Schubert       return 0;
33445796c8dcSSimon Schubert     }
33455796c8dcSSimon Schubert 
33465796c8dcSSimon Schubert   gdbarch = get_objfile_arch (msymbol_objfile (ovly_table_msym));
33475796c8dcSSimon Schubert   word_size = gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT;
33485796c8dcSSimon Schubert   byte_order = gdbarch_byte_order (gdbarch);
33495796c8dcSSimon Schubert 
33505796c8dcSSimon Schubert   cache_novlys = read_memory_integer (SYMBOL_VALUE_ADDRESS (novlys_msym),
33515796c8dcSSimon Schubert 				      4, byte_order);
33525796c8dcSSimon Schubert   cache_ovly_table
33535796c8dcSSimon Schubert     = (void *) xmalloc (cache_novlys * sizeof (*cache_ovly_table));
33545796c8dcSSimon Schubert   cache_ovly_table_base = SYMBOL_VALUE_ADDRESS (ovly_table_msym);
33555796c8dcSSimon Schubert   read_target_long_array (cache_ovly_table_base,
33565796c8dcSSimon Schubert                           (unsigned int *) cache_ovly_table,
33575796c8dcSSimon Schubert                           cache_novlys * 4, word_size, byte_order);
33585796c8dcSSimon Schubert 
33595796c8dcSSimon Schubert   return 1;			/* SUCCESS */
33605796c8dcSSimon Schubert }
33615796c8dcSSimon Schubert 
33625796c8dcSSimon Schubert #if 0
33635796c8dcSSimon Schubert /* Find and grab a copy of the target _ovly_region_table
33645796c8dcSSimon Schubert    (and _novly_regions, which is needed for the table's size) */
33655796c8dcSSimon Schubert static int
33665796c8dcSSimon Schubert simple_read_overlay_region_table (void)
33675796c8dcSSimon Schubert {
33685796c8dcSSimon Schubert   struct minimal_symbol *msym;
33695796c8dcSSimon Schubert   struct gdbarch *gdbarch;
33705796c8dcSSimon Schubert   int word_size;
33715796c8dcSSimon Schubert   enum bfd_endian byte_order;
33725796c8dcSSimon Schubert 
33735796c8dcSSimon Schubert   simple_free_overlay_region_table ();
33745796c8dcSSimon Schubert   msym = lookup_minimal_symbol ("_novly_regions", NULL, NULL);
33755796c8dcSSimon Schubert   if (msym == NULL)
33765796c8dcSSimon Schubert     return 0;			/* failure */
33775796c8dcSSimon Schubert 
33785796c8dcSSimon Schubert   gdbarch = get_objfile_arch (msymbol_objfile (msym));
33795796c8dcSSimon Schubert   word_size = gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT;
33805796c8dcSSimon Schubert   byte_order = gdbarch_byte_order (gdbarch);
33815796c8dcSSimon Schubert 
33825796c8dcSSimon Schubert   cache_novly_regions = read_memory_integer (SYMBOL_VALUE_ADDRESS (msym),
33835796c8dcSSimon Schubert 					     4, byte_order);
33845796c8dcSSimon Schubert 
33855796c8dcSSimon Schubert   cache_ovly_region_table = (void *) xmalloc (cache_novly_regions * 12);
33865796c8dcSSimon Schubert   if (cache_ovly_region_table != NULL)
33875796c8dcSSimon Schubert     {
33885796c8dcSSimon Schubert       msym = lookup_minimal_symbol ("_ovly_region_table", NULL, NULL);
33895796c8dcSSimon Schubert       if (msym != NULL)
33905796c8dcSSimon Schubert 	{
33915796c8dcSSimon Schubert 	  cache_ovly_region_table_base = SYMBOL_VALUE_ADDRESS (msym);
33925796c8dcSSimon Schubert 	  read_target_long_array (cache_ovly_region_table_base,
33935796c8dcSSimon Schubert 				  (unsigned int *) cache_ovly_region_table,
33945796c8dcSSimon Schubert 				  cache_novly_regions * 3,
33955796c8dcSSimon Schubert 				  word_size, byte_order);
33965796c8dcSSimon Schubert 	}
33975796c8dcSSimon Schubert       else
33985796c8dcSSimon Schubert 	return 0;		/* failure */
33995796c8dcSSimon Schubert     }
34005796c8dcSSimon Schubert   else
34015796c8dcSSimon Schubert     return 0;			/* failure */
34025796c8dcSSimon Schubert   return 1;			/* SUCCESS */
34035796c8dcSSimon Schubert }
34045796c8dcSSimon Schubert #endif
34055796c8dcSSimon Schubert 
34065796c8dcSSimon Schubert /* Function: simple_overlay_update_1
34075796c8dcSSimon Schubert    A helper function for simple_overlay_update.  Assuming a cached copy
34085796c8dcSSimon Schubert    of _ovly_table exists, look through it to find an entry whose vma,
34095796c8dcSSimon Schubert    lma and size match those of OSECT.  Re-read the entry and make sure
34105796c8dcSSimon Schubert    it still matches OSECT (else the table may no longer be valid).
34115796c8dcSSimon Schubert    Set OSECT's mapped state to match the entry.  Return: 1 for
34125796c8dcSSimon Schubert    success, 0 for failure.  */
34135796c8dcSSimon Schubert 
34145796c8dcSSimon Schubert static int
34155796c8dcSSimon Schubert simple_overlay_update_1 (struct obj_section *osect)
34165796c8dcSSimon Schubert {
34175796c8dcSSimon Schubert   int i, size;
34185796c8dcSSimon Schubert   bfd *obfd = osect->objfile->obfd;
34195796c8dcSSimon Schubert   asection *bsect = osect->the_bfd_section;
34205796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_objfile_arch (osect->objfile);
34215796c8dcSSimon Schubert   int word_size = gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT;
34225796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
34235796c8dcSSimon Schubert 
34245796c8dcSSimon Schubert   size = bfd_get_section_size (osect->the_bfd_section);
34255796c8dcSSimon Schubert   for (i = 0; i < cache_novlys; i++)
34265796c8dcSSimon Schubert     if (cache_ovly_table[i][VMA] == bfd_section_vma (obfd, bsect)
34275796c8dcSSimon Schubert 	&& cache_ovly_table[i][LMA] == bfd_section_lma (obfd, bsect)
34285796c8dcSSimon Schubert 	/* && cache_ovly_table[i][SIZE] == size */ )
34295796c8dcSSimon Schubert       {
34305796c8dcSSimon Schubert 	read_target_long_array (cache_ovly_table_base + i * word_size,
34315796c8dcSSimon Schubert 				(unsigned int *) cache_ovly_table[i],
34325796c8dcSSimon Schubert 				4, word_size, byte_order);
34335796c8dcSSimon Schubert 	if (cache_ovly_table[i][VMA] == bfd_section_vma (obfd, bsect)
34345796c8dcSSimon Schubert 	    && cache_ovly_table[i][LMA] == bfd_section_lma (obfd, bsect)
34355796c8dcSSimon Schubert 	    /* && cache_ovly_table[i][SIZE] == size */ )
34365796c8dcSSimon Schubert 	  {
34375796c8dcSSimon Schubert 	    osect->ovly_mapped = cache_ovly_table[i][MAPPED];
34385796c8dcSSimon Schubert 	    return 1;
34395796c8dcSSimon Schubert 	  }
34405796c8dcSSimon Schubert 	else	/* Warning!  Warning!  Target's ovly table has changed! */
34415796c8dcSSimon Schubert 	  return 0;
34425796c8dcSSimon Schubert       }
34435796c8dcSSimon Schubert   return 0;
34445796c8dcSSimon Schubert }
34455796c8dcSSimon Schubert 
34465796c8dcSSimon Schubert /* Function: simple_overlay_update
34475796c8dcSSimon Schubert    If OSECT is NULL, then update all sections' mapped state
34485796c8dcSSimon Schubert    (after re-reading the entire target _ovly_table).
34495796c8dcSSimon Schubert    If OSECT is non-NULL, then try to find a matching entry in the
34505796c8dcSSimon Schubert    cached ovly_table and update only OSECT's mapped state.
34515796c8dcSSimon Schubert    If a cached entry can't be found or the cache isn't valid, then
34525796c8dcSSimon Schubert    re-read the entire cache, and go ahead and update all sections.  */
34535796c8dcSSimon Schubert 
34545796c8dcSSimon Schubert void
34555796c8dcSSimon Schubert simple_overlay_update (struct obj_section *osect)
34565796c8dcSSimon Schubert {
34575796c8dcSSimon Schubert   struct objfile *objfile;
34585796c8dcSSimon Schubert 
34595796c8dcSSimon Schubert   /* Were we given an osect to look up?  NULL means do all of them. */
34605796c8dcSSimon Schubert   if (osect)
34615796c8dcSSimon Schubert     /* Have we got a cached copy of the target's overlay table? */
34625796c8dcSSimon Schubert     if (cache_ovly_table != NULL)
34635796c8dcSSimon Schubert       /* Does its cached location match what's currently in the symtab? */
34645796c8dcSSimon Schubert       if (cache_ovly_table_base ==
34655796c8dcSSimon Schubert 	  SYMBOL_VALUE_ADDRESS (lookup_minimal_symbol ("_ovly_table", NULL, NULL)))
34665796c8dcSSimon Schubert 	/* Then go ahead and try to look up this single section in the cache */
34675796c8dcSSimon Schubert 	if (simple_overlay_update_1 (osect))
34685796c8dcSSimon Schubert 	  /* Found it!  We're done. */
34695796c8dcSSimon Schubert 	  return;
34705796c8dcSSimon Schubert 
34715796c8dcSSimon Schubert   /* Cached table no good: need to read the entire table anew.
34725796c8dcSSimon Schubert      Or else we want all the sections, in which case it's actually
34735796c8dcSSimon Schubert      more efficient to read the whole table in one block anyway.  */
34745796c8dcSSimon Schubert 
34755796c8dcSSimon Schubert   if (! simple_read_overlay_table ())
34765796c8dcSSimon Schubert     return;
34775796c8dcSSimon Schubert 
34785796c8dcSSimon Schubert   /* Now may as well update all sections, even if only one was requested. */
34795796c8dcSSimon Schubert   ALL_OBJSECTIONS (objfile, osect)
34805796c8dcSSimon Schubert     if (section_is_overlay (osect))
34815796c8dcSSimon Schubert     {
34825796c8dcSSimon Schubert       int i, size;
34835796c8dcSSimon Schubert       bfd *obfd = osect->objfile->obfd;
34845796c8dcSSimon Schubert       asection *bsect = osect->the_bfd_section;
34855796c8dcSSimon Schubert 
34865796c8dcSSimon Schubert       size = bfd_get_section_size (bsect);
34875796c8dcSSimon Schubert       for (i = 0; i < cache_novlys; i++)
34885796c8dcSSimon Schubert 	if (cache_ovly_table[i][VMA] == bfd_section_vma (obfd, bsect)
34895796c8dcSSimon Schubert 	    && cache_ovly_table[i][LMA] == bfd_section_lma (obfd, bsect)
34905796c8dcSSimon Schubert 	    /* && cache_ovly_table[i][SIZE] == size */ )
34915796c8dcSSimon Schubert 	  { /* obj_section matches i'th entry in ovly_table */
34925796c8dcSSimon Schubert 	    osect->ovly_mapped = cache_ovly_table[i][MAPPED];
34935796c8dcSSimon Schubert 	    break;		/* finished with inner for loop: break out */
34945796c8dcSSimon Schubert 	  }
34955796c8dcSSimon Schubert     }
34965796c8dcSSimon Schubert }
34975796c8dcSSimon Schubert 
34985796c8dcSSimon Schubert /* Set the output sections and output offsets for section SECTP in
34995796c8dcSSimon Schubert    ABFD.  The relocation code in BFD will read these offsets, so we
35005796c8dcSSimon Schubert    need to be sure they're initialized.  We map each section to itself,
35015796c8dcSSimon Schubert    with no offset; this means that SECTP->vma will be honored.  */
35025796c8dcSSimon Schubert 
35035796c8dcSSimon Schubert static void
35045796c8dcSSimon Schubert symfile_dummy_outputs (bfd *abfd, asection *sectp, void *dummy)
35055796c8dcSSimon Schubert {
35065796c8dcSSimon Schubert   sectp->output_section = sectp;
35075796c8dcSSimon Schubert   sectp->output_offset = 0;
35085796c8dcSSimon Schubert }
35095796c8dcSSimon Schubert 
3510*cf7f2e2dSJohn Marino /* Default implementation for sym_relocate.  */
3511*cf7f2e2dSJohn Marino 
3512*cf7f2e2dSJohn Marino 
3513*cf7f2e2dSJohn Marino bfd_byte *
3514*cf7f2e2dSJohn Marino default_symfile_relocate (struct objfile *objfile, asection *sectp,
3515*cf7f2e2dSJohn Marino                           bfd_byte *buf)
3516*cf7f2e2dSJohn Marino {
3517*cf7f2e2dSJohn Marino   bfd *abfd = objfile->obfd;
3518*cf7f2e2dSJohn Marino 
3519*cf7f2e2dSJohn Marino   /* We're only interested in sections with relocation
3520*cf7f2e2dSJohn Marino      information.  */
3521*cf7f2e2dSJohn Marino   if ((sectp->flags & SEC_RELOC) == 0)
3522*cf7f2e2dSJohn Marino     return NULL;
3523*cf7f2e2dSJohn Marino 
3524*cf7f2e2dSJohn Marino   /* We will handle section offsets properly elsewhere, so relocate as if
3525*cf7f2e2dSJohn Marino      all sections begin at 0.  */
3526*cf7f2e2dSJohn Marino   bfd_map_over_sections (abfd, symfile_dummy_outputs, NULL);
3527*cf7f2e2dSJohn Marino 
3528*cf7f2e2dSJohn Marino   return bfd_simple_get_relocated_section_contents (abfd, sectp, buf, NULL);
3529*cf7f2e2dSJohn Marino }
3530*cf7f2e2dSJohn Marino 
35315796c8dcSSimon Schubert /* Relocate the contents of a debug section SECTP in ABFD.  The
35325796c8dcSSimon Schubert    contents are stored in BUF if it is non-NULL, or returned in a
35335796c8dcSSimon Schubert    malloc'd buffer otherwise.
35345796c8dcSSimon Schubert 
35355796c8dcSSimon Schubert    For some platforms and debug info formats, shared libraries contain
35365796c8dcSSimon Schubert    relocations against the debug sections (particularly for DWARF-2;
35375796c8dcSSimon Schubert    one affected platform is PowerPC GNU/Linux, although it depends on
35385796c8dcSSimon Schubert    the version of the linker in use).  Also, ELF object files naturally
35395796c8dcSSimon Schubert    have unresolved relocations for their debug sections.  We need to apply
35405796c8dcSSimon Schubert    the relocations in order to get the locations of symbols correct.
35415796c8dcSSimon Schubert    Another example that may require relocation processing, is the
35425796c8dcSSimon Schubert    DWARF-2 .eh_frame section in .o files, although it isn't strictly a
35435796c8dcSSimon Schubert    debug section.  */
35445796c8dcSSimon Schubert 
35455796c8dcSSimon Schubert bfd_byte *
3546*cf7f2e2dSJohn Marino symfile_relocate_debug_section (struct objfile *objfile,
3547*cf7f2e2dSJohn Marino                                 asection *sectp, bfd_byte *buf)
35485796c8dcSSimon Schubert {
3549*cf7f2e2dSJohn Marino   gdb_assert (objfile->sf->sym_relocate);
35505796c8dcSSimon Schubert 
3551*cf7f2e2dSJohn Marino   return (*objfile->sf->sym_relocate) (objfile, sectp, buf);
35525796c8dcSSimon Schubert }
35535796c8dcSSimon Schubert 
35545796c8dcSSimon Schubert struct symfile_segment_data *
35555796c8dcSSimon Schubert get_symfile_segment_data (bfd *abfd)
35565796c8dcSSimon Schubert {
35575796c8dcSSimon Schubert   struct sym_fns *sf = find_sym_fns (abfd);
35585796c8dcSSimon Schubert 
35595796c8dcSSimon Schubert   if (sf == NULL)
35605796c8dcSSimon Schubert     return NULL;
35615796c8dcSSimon Schubert 
35625796c8dcSSimon Schubert   return sf->sym_segments (abfd);
35635796c8dcSSimon Schubert }
35645796c8dcSSimon Schubert 
35655796c8dcSSimon Schubert void
35665796c8dcSSimon Schubert free_symfile_segment_data (struct symfile_segment_data *data)
35675796c8dcSSimon Schubert {
35685796c8dcSSimon Schubert   xfree (data->segment_bases);
35695796c8dcSSimon Schubert   xfree (data->segment_sizes);
35705796c8dcSSimon Schubert   xfree (data->segment_info);
35715796c8dcSSimon Schubert   xfree (data);
35725796c8dcSSimon Schubert }
35735796c8dcSSimon Schubert 
35745796c8dcSSimon Schubert 
35755796c8dcSSimon Schubert /* Given:
35765796c8dcSSimon Schubert    - DATA, containing segment addresses from the object file ABFD, and
35775796c8dcSSimon Schubert      the mapping from ABFD's sections onto the segments that own them,
35785796c8dcSSimon Schubert      and
35795796c8dcSSimon Schubert    - SEGMENT_BASES[0 .. NUM_SEGMENT_BASES - 1], holding the actual
35805796c8dcSSimon Schubert      segment addresses reported by the target,
35815796c8dcSSimon Schubert    store the appropriate offsets for each section in OFFSETS.
35825796c8dcSSimon Schubert 
35835796c8dcSSimon Schubert    If there are fewer entries in SEGMENT_BASES than there are segments
35845796c8dcSSimon Schubert    in DATA, then apply SEGMENT_BASES' last entry to all the segments.
35855796c8dcSSimon Schubert 
35865796c8dcSSimon Schubert    If there are more entries, then ignore the extra.  The target may
35875796c8dcSSimon Schubert    not be able to distinguish between an empty data segment and a
35885796c8dcSSimon Schubert    missing data segment; a missing text segment is less plausible.  */
35895796c8dcSSimon Schubert int
35905796c8dcSSimon Schubert symfile_map_offsets_to_segments (bfd *abfd, struct symfile_segment_data *data,
35915796c8dcSSimon Schubert 				 struct section_offsets *offsets,
35925796c8dcSSimon Schubert 				 int num_segment_bases,
35935796c8dcSSimon Schubert 				 const CORE_ADDR *segment_bases)
35945796c8dcSSimon Schubert {
35955796c8dcSSimon Schubert   int i;
35965796c8dcSSimon Schubert   asection *sect;
35975796c8dcSSimon Schubert 
35985796c8dcSSimon Schubert   /* It doesn't make sense to call this function unless you have some
35995796c8dcSSimon Schubert      segment base addresses.  */
3600*cf7f2e2dSJohn Marino   gdb_assert (num_segment_bases > 0);
36015796c8dcSSimon Schubert 
36025796c8dcSSimon Schubert   /* If we do not have segment mappings for the object file, we
36035796c8dcSSimon Schubert      can not relocate it by segments.  */
36045796c8dcSSimon Schubert   gdb_assert (data != NULL);
36055796c8dcSSimon Schubert   gdb_assert (data->num_segments > 0);
36065796c8dcSSimon Schubert 
36075796c8dcSSimon Schubert   for (i = 0, sect = abfd->sections; sect != NULL; i++, sect = sect->next)
36085796c8dcSSimon Schubert     {
36095796c8dcSSimon Schubert       int which = data->segment_info[i];
36105796c8dcSSimon Schubert 
36115796c8dcSSimon Schubert       gdb_assert (0 <= which && which <= data->num_segments);
36125796c8dcSSimon Schubert 
36135796c8dcSSimon Schubert       /* Don't bother computing offsets for sections that aren't
36145796c8dcSSimon Schubert          loaded as part of any segment.  */
36155796c8dcSSimon Schubert       if (! which)
36165796c8dcSSimon Schubert         continue;
36175796c8dcSSimon Schubert 
36185796c8dcSSimon Schubert       /* Use the last SEGMENT_BASES entry as the address of any extra
36195796c8dcSSimon Schubert          segments mentioned in DATA->segment_info.  */
36205796c8dcSSimon Schubert       if (which > num_segment_bases)
36215796c8dcSSimon Schubert         which = num_segment_bases;
36225796c8dcSSimon Schubert 
36235796c8dcSSimon Schubert       offsets->offsets[i] = (segment_bases[which - 1]
36245796c8dcSSimon Schubert                              - data->segment_bases[which - 1]);
36255796c8dcSSimon Schubert     }
36265796c8dcSSimon Schubert 
36275796c8dcSSimon Schubert   return 1;
36285796c8dcSSimon Schubert }
36295796c8dcSSimon Schubert 
36305796c8dcSSimon Schubert static void
36315796c8dcSSimon Schubert symfile_find_segment_sections (struct objfile *objfile)
36325796c8dcSSimon Schubert {
36335796c8dcSSimon Schubert   bfd *abfd = objfile->obfd;
36345796c8dcSSimon Schubert   int i;
36355796c8dcSSimon Schubert   asection *sect;
36365796c8dcSSimon Schubert   struct symfile_segment_data *data;
36375796c8dcSSimon Schubert 
36385796c8dcSSimon Schubert   data = get_symfile_segment_data (objfile->obfd);
36395796c8dcSSimon Schubert   if (data == NULL)
36405796c8dcSSimon Schubert     return;
36415796c8dcSSimon Schubert 
36425796c8dcSSimon Schubert   if (data->num_segments != 1 && data->num_segments != 2)
36435796c8dcSSimon Schubert     {
36445796c8dcSSimon Schubert       free_symfile_segment_data (data);
36455796c8dcSSimon Schubert       return;
36465796c8dcSSimon Schubert     }
36475796c8dcSSimon Schubert 
36485796c8dcSSimon Schubert   for (i = 0, sect = abfd->sections; sect != NULL; i++, sect = sect->next)
36495796c8dcSSimon Schubert     {
36505796c8dcSSimon Schubert       int which = data->segment_info[i];
36515796c8dcSSimon Schubert 
36525796c8dcSSimon Schubert       if (which == 1)
36535796c8dcSSimon Schubert 	{
36545796c8dcSSimon Schubert 	  if (objfile->sect_index_text == -1)
36555796c8dcSSimon Schubert 	    objfile->sect_index_text = sect->index;
36565796c8dcSSimon Schubert 
36575796c8dcSSimon Schubert 	  if (objfile->sect_index_rodata == -1)
36585796c8dcSSimon Schubert 	    objfile->sect_index_rodata = sect->index;
36595796c8dcSSimon Schubert 	}
36605796c8dcSSimon Schubert       else if (which == 2)
36615796c8dcSSimon Schubert 	{
36625796c8dcSSimon Schubert 	  if (objfile->sect_index_data == -1)
36635796c8dcSSimon Schubert 	    objfile->sect_index_data = sect->index;
36645796c8dcSSimon Schubert 
36655796c8dcSSimon Schubert 	  if (objfile->sect_index_bss == -1)
36665796c8dcSSimon Schubert 	    objfile->sect_index_bss = sect->index;
36675796c8dcSSimon Schubert 	}
36685796c8dcSSimon Schubert     }
36695796c8dcSSimon Schubert 
36705796c8dcSSimon Schubert   free_symfile_segment_data (data);
36715796c8dcSSimon Schubert }
36725796c8dcSSimon Schubert 
36735796c8dcSSimon Schubert void
36745796c8dcSSimon Schubert _initialize_symfile (void)
36755796c8dcSSimon Schubert {
36765796c8dcSSimon Schubert   struct cmd_list_element *c;
36775796c8dcSSimon Schubert 
36785796c8dcSSimon Schubert   c = add_cmd ("symbol-file", class_files, symbol_file_command, _("\
36795796c8dcSSimon Schubert Load symbol table from executable file FILE.\n\
36805796c8dcSSimon Schubert The `file' command can also load symbol tables, as well as setting the file\n\
36815796c8dcSSimon Schubert to execute."), &cmdlist);
36825796c8dcSSimon Schubert   set_cmd_completer (c, filename_completer);
36835796c8dcSSimon Schubert 
36845796c8dcSSimon Schubert   c = add_cmd ("add-symbol-file", class_files, add_symbol_file_command, _("\
36855796c8dcSSimon Schubert Load symbols from FILE, assuming FILE has been dynamically loaded.\n\
36865796c8dcSSimon Schubert Usage: add-symbol-file FILE ADDR [-s <SECT> <SECT_ADDR> -s <SECT> <SECT_ADDR> ...]\n\
36875796c8dcSSimon Schubert ADDR is the starting address of the file's text.\n\
36885796c8dcSSimon Schubert The optional arguments are section-name section-address pairs and\n\
36895796c8dcSSimon Schubert should be specified if the data and bss segments are not contiguous\n\
36905796c8dcSSimon Schubert with the text.  SECT is a section name to be loaded at SECT_ADDR."),
36915796c8dcSSimon Schubert 	       &cmdlist);
36925796c8dcSSimon Schubert   set_cmd_completer (c, filename_completer);
36935796c8dcSSimon Schubert 
36945796c8dcSSimon Schubert   c = add_cmd ("load", class_files, load_command, _("\
36955796c8dcSSimon Schubert Dynamically load FILE into the running program, and record its symbols\n\
36965796c8dcSSimon Schubert for access from GDB.\n\
36975796c8dcSSimon Schubert A load OFFSET may also be given."), &cmdlist);
36985796c8dcSSimon Schubert   set_cmd_completer (c, filename_completer);
36995796c8dcSSimon Schubert 
37005796c8dcSSimon Schubert   add_setshow_boolean_cmd ("symbol-reloading", class_support,
37015796c8dcSSimon Schubert 			   &symbol_reloading, _("\
37025796c8dcSSimon Schubert Set dynamic symbol table reloading multiple times in one run."), _("\
37035796c8dcSSimon Schubert Show dynamic symbol table reloading multiple times in one run."), NULL,
37045796c8dcSSimon Schubert 			   NULL,
37055796c8dcSSimon Schubert 			   show_symbol_reloading,
37065796c8dcSSimon Schubert 			   &setlist, &showlist);
37075796c8dcSSimon Schubert 
37085796c8dcSSimon Schubert   add_prefix_cmd ("overlay", class_support, overlay_command,
37095796c8dcSSimon Schubert 		  _("Commands for debugging overlays."), &overlaylist,
37105796c8dcSSimon Schubert 		  "overlay ", 0, &cmdlist);
37115796c8dcSSimon Schubert 
37125796c8dcSSimon Schubert   add_com_alias ("ovly", "overlay", class_alias, 1);
37135796c8dcSSimon Schubert   add_com_alias ("ov", "overlay", class_alias, 1);
37145796c8dcSSimon Schubert 
37155796c8dcSSimon Schubert   add_cmd ("map-overlay", class_support, map_overlay_command,
37165796c8dcSSimon Schubert 	   _("Assert that an overlay section is mapped."), &overlaylist);
37175796c8dcSSimon Schubert 
37185796c8dcSSimon Schubert   add_cmd ("unmap-overlay", class_support, unmap_overlay_command,
37195796c8dcSSimon Schubert 	   _("Assert that an overlay section is unmapped."), &overlaylist);
37205796c8dcSSimon Schubert 
37215796c8dcSSimon Schubert   add_cmd ("list-overlays", class_support, list_overlays_command,
37225796c8dcSSimon Schubert 	   _("List mappings of overlay sections."), &overlaylist);
37235796c8dcSSimon Schubert 
37245796c8dcSSimon Schubert   add_cmd ("manual", class_support, overlay_manual_command,
37255796c8dcSSimon Schubert 	   _("Enable overlay debugging."), &overlaylist);
37265796c8dcSSimon Schubert   add_cmd ("off", class_support, overlay_off_command,
37275796c8dcSSimon Schubert 	   _("Disable overlay debugging."), &overlaylist);
37285796c8dcSSimon Schubert   add_cmd ("auto", class_support, overlay_auto_command,
37295796c8dcSSimon Schubert 	   _("Enable automatic overlay debugging."), &overlaylist);
37305796c8dcSSimon Schubert   add_cmd ("load-target", class_support, overlay_load_command,
37315796c8dcSSimon Schubert 	   _("Read the overlay mapping state from the target."), &overlaylist);
37325796c8dcSSimon Schubert 
37335796c8dcSSimon Schubert   /* Filename extension to source language lookup table: */
37345796c8dcSSimon Schubert   init_filename_language_table ();
37355796c8dcSSimon Schubert   add_setshow_string_noescape_cmd ("extension-language", class_files,
37365796c8dcSSimon Schubert 				   &ext_args, _("\
37375796c8dcSSimon Schubert Set mapping between filename extension and source language."), _("\
37385796c8dcSSimon Schubert Show mapping between filename extension and source language."), _("\
37395796c8dcSSimon Schubert Usage: set extension-language .foo bar"),
37405796c8dcSSimon Schubert 				   set_ext_lang_command,
37415796c8dcSSimon Schubert 				   show_ext_args,
37425796c8dcSSimon Schubert 				   &setlist, &showlist);
37435796c8dcSSimon Schubert 
37445796c8dcSSimon Schubert   add_info ("extensions", info_ext_lang_command,
37455796c8dcSSimon Schubert 	    _("All filename extensions associated with a source language."));
37465796c8dcSSimon Schubert 
37475796c8dcSSimon Schubert   add_setshow_optional_filename_cmd ("debug-file-directory", class_support,
37485796c8dcSSimon Schubert 				     &debug_file_directory, _("\
3749*cf7f2e2dSJohn Marino Set the directories where separate debug symbols are searched for."), _("\
3750*cf7f2e2dSJohn Marino Show the directories where separate debug symbols are searched for."), _("\
37515796c8dcSSimon Schubert Separate debug symbols are first searched for in the same\n\
37525796c8dcSSimon Schubert directory as the binary, then in the `" DEBUG_SUBDIRECTORY "' subdirectory,\n\
37535796c8dcSSimon Schubert and lastly at the path of the directory of the binary with\n\
3754*cf7f2e2dSJohn Marino each global debug-file-directory component prepended."),
37555796c8dcSSimon Schubert 				     NULL,
37565796c8dcSSimon Schubert 				     show_debug_file_directory,
37575796c8dcSSimon Schubert 				     &setlist, &showlist);
37585796c8dcSSimon Schubert }
3759