xref: /dragonfly/contrib/gdb-7/gdb/symfile.c (revision a45ae5f8)
15796c8dcSSimon Schubert /* Generic symbol file reading for the GNU debugger, GDB.
25796c8dcSSimon Schubert 
3*a45ae5f8SJohn Marino    Copyright (C) 1990-2012 Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    Contributed by Cygnus Support, using pieces from other GDB modules.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This file is part of GDB.
85796c8dcSSimon Schubert 
95796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
105796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
115796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
125796c8dcSSimon Schubert    (at your option) any later version.
135796c8dcSSimon Schubert 
145796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
155796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
165796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
175796c8dcSSimon Schubert    GNU General Public License for more details.
185796c8dcSSimon Schubert 
195796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
205796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
215796c8dcSSimon Schubert 
225796c8dcSSimon Schubert #include "defs.h"
235796c8dcSSimon Schubert #include "arch-utils.h"
245796c8dcSSimon Schubert #include "bfdlink.h"
255796c8dcSSimon Schubert #include "symtab.h"
265796c8dcSSimon Schubert #include "gdbtypes.h"
275796c8dcSSimon Schubert #include "gdbcore.h"
285796c8dcSSimon Schubert #include "frame.h"
295796c8dcSSimon Schubert #include "target.h"
305796c8dcSSimon Schubert #include "value.h"
315796c8dcSSimon Schubert #include "symfile.h"
325796c8dcSSimon Schubert #include "objfiles.h"
335796c8dcSSimon Schubert #include "source.h"
345796c8dcSSimon Schubert #include "gdbcmd.h"
355796c8dcSSimon Schubert #include "breakpoint.h"
365796c8dcSSimon Schubert #include "language.h"
375796c8dcSSimon Schubert #include "complaints.h"
385796c8dcSSimon Schubert #include "demangle.h"
395796c8dcSSimon Schubert #include "inferior.h"
405796c8dcSSimon Schubert #include "regcache.h"
415796c8dcSSimon Schubert #include "filenames.h"		/* for DOSish file names */
425796c8dcSSimon Schubert #include "gdb-stabs.h"
435796c8dcSSimon Schubert #include "gdb_obstack.h"
445796c8dcSSimon Schubert #include "completer.h"
455796c8dcSSimon Schubert #include "bcache.h"
465796c8dcSSimon Schubert #include "hashtab.h"
475796c8dcSSimon Schubert #include "readline/readline.h"
485796c8dcSSimon Schubert #include "gdb_assert.h"
495796c8dcSSimon Schubert #include "block.h"
505796c8dcSSimon Schubert #include "observer.h"
515796c8dcSSimon Schubert #include "exec.h"
525796c8dcSSimon Schubert #include "parser-defs.h"
535796c8dcSSimon Schubert #include "varobj.h"
545796c8dcSSimon Schubert #include "elf-bfd.h"
555796c8dcSSimon Schubert #include "solib.h"
565796c8dcSSimon Schubert #include "remote.h"
57*a45ae5f8SJohn Marino #include "stack.h"
585796c8dcSSimon Schubert 
595796c8dcSSimon Schubert #include <sys/types.h>
605796c8dcSSimon Schubert #include <fcntl.h>
615796c8dcSSimon Schubert #include "gdb_string.h"
625796c8dcSSimon Schubert #include "gdb_stat.h"
635796c8dcSSimon Schubert #include <ctype.h>
645796c8dcSSimon Schubert #include <time.h>
655796c8dcSSimon Schubert #include <sys/time.h>
665796c8dcSSimon Schubert 
67cf7f2e2dSJohn Marino #include "psymtab.h"
685796c8dcSSimon Schubert 
69c50c785cSJohn Marino int (*deprecated_ui_load_progress_hook) (const char *section,
70c50c785cSJohn Marino 					 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 
81c50c785cSJohn Marino /* Global variables owned by this file.  */
82c50c785cSJohn Marino 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 
88c50c785cSJohn Marino /* Functions this file defines.  */
895796c8dcSSimon Schubert 
905796c8dcSSimon Schubert static void load_command (char *, int);
915796c8dcSSimon Schubert 
925796c8dcSSimon Schubert static void symbol_file_add_main_1 (char *args, int from_tty, int flags);
935796c8dcSSimon Schubert 
945796c8dcSSimon Schubert static void add_symbol_file_command (char *, int);
955796c8dcSSimon Schubert 
965796c8dcSSimon Schubert bfd *symfile_bfd_open (char *);
975796c8dcSSimon Schubert 
985796c8dcSSimon Schubert int get_section_index (struct objfile *, char *);
995796c8dcSSimon Schubert 
100c50c785cSJohn Marino static const struct sym_fns *find_sym_fns (bfd *);
1015796c8dcSSimon Schubert 
1025796c8dcSSimon Schubert static void decrement_reading_symtab (void *);
1035796c8dcSSimon Schubert 
1045796c8dcSSimon Schubert static void overlay_invalidate_all (void);
1055796c8dcSSimon Schubert 
1065796c8dcSSimon Schubert void list_overlays_command (char *, int);
1075796c8dcSSimon Schubert 
1085796c8dcSSimon Schubert void map_overlay_command (char *, int);
1095796c8dcSSimon Schubert 
1105796c8dcSSimon Schubert void unmap_overlay_command (char *, int);
1115796c8dcSSimon Schubert 
1125796c8dcSSimon Schubert static void overlay_auto_command (char *, int);
1135796c8dcSSimon Schubert 
1145796c8dcSSimon Schubert static void overlay_manual_command (char *, int);
1155796c8dcSSimon Schubert 
1165796c8dcSSimon Schubert static void overlay_off_command (char *, int);
1175796c8dcSSimon Schubert 
1185796c8dcSSimon Schubert static void overlay_load_command (char *, int);
1195796c8dcSSimon Schubert 
1205796c8dcSSimon Schubert static void overlay_command (char *, int);
1215796c8dcSSimon Schubert 
1225796c8dcSSimon Schubert static void simple_free_overlay_table (void);
1235796c8dcSSimon Schubert 
1245796c8dcSSimon Schubert static void read_target_long_array (CORE_ADDR, unsigned int *, int, int,
1255796c8dcSSimon Schubert 				    enum bfd_endian);
1265796c8dcSSimon Schubert 
1275796c8dcSSimon Schubert static int simple_read_overlay_table (void);
1285796c8dcSSimon Schubert 
1295796c8dcSSimon Schubert static int simple_overlay_update_1 (struct obj_section *);
1305796c8dcSSimon Schubert 
1315796c8dcSSimon Schubert static void add_filename_language (char *ext, enum language lang);
1325796c8dcSSimon Schubert 
1335796c8dcSSimon Schubert static void info_ext_lang_command (char *args, int from_tty);
1345796c8dcSSimon Schubert 
1355796c8dcSSimon Schubert static void init_filename_language_table (void);
1365796c8dcSSimon Schubert 
1375796c8dcSSimon Schubert static void symfile_find_segment_sections (struct objfile *objfile);
1385796c8dcSSimon Schubert 
1395796c8dcSSimon Schubert void _initialize_symfile (void);
1405796c8dcSSimon Schubert 
1415796c8dcSSimon Schubert /* List of all available sym_fns.  On gdb startup, each object file reader
1425796c8dcSSimon Schubert    calls add_symtab_fns() to register information on each format it is
1435796c8dcSSimon Schubert    prepared to read.  */
1445796c8dcSSimon Schubert 
145c50c785cSJohn Marino typedef const struct sym_fns *sym_fns_ptr;
146c50c785cSJohn Marino DEF_VEC_P (sym_fns_ptr);
147c50c785cSJohn Marino 
148c50c785cSJohn Marino static VEC (sym_fns_ptr) *symtab_fns = NULL;
1495796c8dcSSimon Schubert 
1505796c8dcSSimon Schubert /* Flag for whether user will be reloading symbols multiple times.
1515796c8dcSSimon Schubert    Defaults to ON for VxWorks, otherwise OFF.  */
1525796c8dcSSimon Schubert 
1535796c8dcSSimon Schubert #ifdef SYMBOL_RELOADING_DEFAULT
1545796c8dcSSimon Schubert int symbol_reloading = SYMBOL_RELOADING_DEFAULT;
1555796c8dcSSimon Schubert #else
1565796c8dcSSimon Schubert int symbol_reloading = 0;
1575796c8dcSSimon Schubert #endif
1585796c8dcSSimon Schubert static void
1595796c8dcSSimon Schubert show_symbol_reloading (struct ui_file *file, int from_tty,
1605796c8dcSSimon Schubert 		       struct cmd_list_element *c, const char *value)
1615796c8dcSSimon Schubert {
162c50c785cSJohn Marino   fprintf_filtered (file, _("Dynamic symbol table reloading "
163c50c785cSJohn Marino 			    "multiple times in one run is %s.\n"),
1645796c8dcSSimon Schubert 		    value);
1655796c8dcSSimon Schubert }
1665796c8dcSSimon Schubert 
1675796c8dcSSimon Schubert /* If non-zero, shared library symbols will be added automatically
1685796c8dcSSimon Schubert    when the inferior is created, new libraries are loaded, or when
1695796c8dcSSimon Schubert    attaching to the inferior.  This is almost always what users will
1705796c8dcSSimon Schubert    want to have happen; but for very large programs, the startup time
1715796c8dcSSimon Schubert    will be excessive, and so if this is a problem, the user can clear
1725796c8dcSSimon Schubert    this flag and then add the shared library symbols as needed.  Note
1735796c8dcSSimon Schubert    that there is a potential for confusion, since if the shared
1745796c8dcSSimon Schubert    library symbols are not loaded, commands like "info fun" will *not*
1755796c8dcSSimon Schubert    report all the functions that are actually present.  */
1765796c8dcSSimon Schubert 
1775796c8dcSSimon Schubert int auto_solib_add = 1;
1785796c8dcSSimon Schubert 
1795796c8dcSSimon Schubert 
1805796c8dcSSimon Schubert /* Make a null terminated copy of the string at PTR with SIZE characters in
1815796c8dcSSimon Schubert    the obstack pointed to by OBSTACKP .  Returns the address of the copy.
182c50c785cSJohn Marino    Note that the string at PTR does not have to be null terminated, I.e. it
1835796c8dcSSimon Schubert    may be part of a larger string and we are only saving a substring.  */
1845796c8dcSSimon Schubert 
1855796c8dcSSimon Schubert char *
1865796c8dcSSimon Schubert obsavestring (const char *ptr, int size, struct obstack *obstackp)
1875796c8dcSSimon Schubert {
1885796c8dcSSimon Schubert   char *p = (char *) obstack_alloc (obstackp, size + 1);
1895796c8dcSSimon Schubert   /* Open-coded memcpy--saves function call time.  These strings are usually
1905796c8dcSSimon Schubert      short.  FIXME: Is this really still true with a compiler that can
1915796c8dcSSimon Schubert      inline memcpy?  */
1925796c8dcSSimon Schubert   {
1935796c8dcSSimon Schubert     const char *p1 = ptr;
1945796c8dcSSimon Schubert     char *p2 = p;
1955796c8dcSSimon Schubert     const char *end = ptr + size;
196cf7f2e2dSJohn Marino 
1975796c8dcSSimon Schubert     while (p1 != end)
1985796c8dcSSimon Schubert       *p2++ = *p1++;
1995796c8dcSSimon Schubert   }
2005796c8dcSSimon Schubert   p[size] = 0;
2015796c8dcSSimon Schubert   return p;
2025796c8dcSSimon Schubert }
2035796c8dcSSimon Schubert 
204c50c785cSJohn Marino /* Concatenate NULL terminated variable argument list of `const char *'
205c50c785cSJohn Marino    strings; return the new string.  Space is found in the OBSTACKP.
206c50c785cSJohn Marino    Argument list must be terminated by a sentinel expression `(char *)
207c50c785cSJohn Marino    NULL'.  */
2085796c8dcSSimon Schubert 
2095796c8dcSSimon Schubert char *
210cf7f2e2dSJohn Marino obconcat (struct obstack *obstackp, ...)
2115796c8dcSSimon Schubert {
212cf7f2e2dSJohn Marino   va_list ap;
213cf7f2e2dSJohn Marino 
214cf7f2e2dSJohn Marino   va_start (ap, obstackp);
215cf7f2e2dSJohn Marino   for (;;)
216cf7f2e2dSJohn Marino     {
217cf7f2e2dSJohn Marino       const char *s = va_arg (ap, const char *);
218cf7f2e2dSJohn Marino 
219cf7f2e2dSJohn Marino       if (s == NULL)
220cf7f2e2dSJohn Marino 	break;
221cf7f2e2dSJohn Marino 
222cf7f2e2dSJohn Marino       obstack_grow_str (obstackp, s);
223cf7f2e2dSJohn Marino     }
224cf7f2e2dSJohn Marino   va_end (ap);
225cf7f2e2dSJohn Marino   obstack_1grow (obstackp, 0);
226cf7f2e2dSJohn Marino 
227cf7f2e2dSJohn Marino   return obstack_finish (obstackp);
2285796c8dcSSimon Schubert }
2295796c8dcSSimon Schubert 
230cf7f2e2dSJohn Marino /* True if we are reading a symbol table.  */
2315796c8dcSSimon Schubert 
2325796c8dcSSimon Schubert int currently_reading_symtab = 0;
2335796c8dcSSimon Schubert 
2345796c8dcSSimon Schubert static void
2355796c8dcSSimon Schubert decrement_reading_symtab (void *dummy)
2365796c8dcSSimon Schubert {
2375796c8dcSSimon Schubert   currently_reading_symtab--;
2385796c8dcSSimon Schubert }
2395796c8dcSSimon Schubert 
240cf7f2e2dSJohn Marino /* Increment currently_reading_symtab and return a cleanup that can be
241cf7f2e2dSJohn Marino    used to decrement it.  */
242cf7f2e2dSJohn Marino struct cleanup *
243cf7f2e2dSJohn Marino increment_reading_symtab (void)
2445796c8dcSSimon Schubert {
245cf7f2e2dSJohn Marino   ++currently_reading_symtab;
246cf7f2e2dSJohn Marino   return make_cleanup (decrement_reading_symtab, NULL);
2475796c8dcSSimon Schubert }
2485796c8dcSSimon Schubert 
2495796c8dcSSimon Schubert /* Remember the lowest-addressed loadable section we've seen.
2505796c8dcSSimon Schubert    This function is called via bfd_map_over_sections.
2515796c8dcSSimon Schubert 
2525796c8dcSSimon Schubert    In case of equal vmas, the section with the largest size becomes the
2535796c8dcSSimon Schubert    lowest-addressed loadable section.
2545796c8dcSSimon Schubert 
2555796c8dcSSimon Schubert    If the vmas and sizes are equal, the last section is considered the
2565796c8dcSSimon Schubert    lowest-addressed loadable section.  */
2575796c8dcSSimon Schubert 
2585796c8dcSSimon Schubert void
2595796c8dcSSimon Schubert find_lowest_section (bfd *abfd, asection *sect, void *obj)
2605796c8dcSSimon Schubert {
2615796c8dcSSimon Schubert   asection **lowest = (asection **) obj;
2625796c8dcSSimon Schubert 
263cf7f2e2dSJohn Marino   if (0 == (bfd_get_section_flags (abfd, sect) & (SEC_ALLOC | SEC_LOAD)))
2645796c8dcSSimon Schubert     return;
2655796c8dcSSimon Schubert   if (!*lowest)
2665796c8dcSSimon Schubert     *lowest = sect;		/* First loadable section */
2675796c8dcSSimon Schubert   else if (bfd_section_vma (abfd, *lowest) > bfd_section_vma (abfd, sect))
2685796c8dcSSimon Schubert     *lowest = sect;		/* A lower loadable section */
2695796c8dcSSimon Schubert   else if (bfd_section_vma (abfd, *lowest) == bfd_section_vma (abfd, sect)
2705796c8dcSSimon Schubert 	   && (bfd_section_size (abfd, (*lowest))
2715796c8dcSSimon Schubert 	       <= bfd_section_size (abfd, sect)))
2725796c8dcSSimon Schubert     *lowest = sect;
2735796c8dcSSimon Schubert }
2745796c8dcSSimon Schubert 
2755796c8dcSSimon Schubert /* Create a new section_addr_info, with room for NUM_SECTIONS.  */
2765796c8dcSSimon Schubert 
2775796c8dcSSimon Schubert struct section_addr_info *
2785796c8dcSSimon Schubert alloc_section_addr_info (size_t num_sections)
2795796c8dcSSimon Schubert {
2805796c8dcSSimon Schubert   struct section_addr_info *sap;
2815796c8dcSSimon Schubert   size_t size;
2825796c8dcSSimon Schubert 
2835796c8dcSSimon Schubert   size = (sizeof (struct section_addr_info)
2845796c8dcSSimon Schubert 	  +  sizeof (struct other_sections) * (num_sections - 1));
2855796c8dcSSimon Schubert   sap = (struct section_addr_info *) xmalloc (size);
2865796c8dcSSimon Schubert   memset (sap, 0, size);
2875796c8dcSSimon Schubert   sap->num_sections = num_sections;
2885796c8dcSSimon Schubert 
2895796c8dcSSimon Schubert   return sap;
2905796c8dcSSimon Schubert }
2915796c8dcSSimon Schubert 
2925796c8dcSSimon Schubert /* Build (allocate and populate) a section_addr_info struct from
2935796c8dcSSimon Schubert    an existing section table.  */
2945796c8dcSSimon Schubert 
2955796c8dcSSimon Schubert extern struct section_addr_info *
2965796c8dcSSimon Schubert build_section_addr_info_from_section_table (const struct target_section *start,
2975796c8dcSSimon Schubert                                             const struct target_section *end)
2985796c8dcSSimon Schubert {
2995796c8dcSSimon Schubert   struct section_addr_info *sap;
3005796c8dcSSimon Schubert   const struct target_section *stp;
3015796c8dcSSimon Schubert   int oidx;
3025796c8dcSSimon Schubert 
3035796c8dcSSimon Schubert   sap = alloc_section_addr_info (end - start);
3045796c8dcSSimon Schubert 
3055796c8dcSSimon Schubert   for (stp = start, oidx = 0; stp != end; stp++)
3065796c8dcSSimon Schubert     {
3075796c8dcSSimon Schubert       if (bfd_get_section_flags (stp->bfd,
3085796c8dcSSimon Schubert 				 stp->the_bfd_section) & (SEC_ALLOC | SEC_LOAD)
3095796c8dcSSimon Schubert 	  && oidx < end - start)
3105796c8dcSSimon Schubert 	{
3115796c8dcSSimon Schubert 	  sap->other[oidx].addr = stp->addr;
3125796c8dcSSimon Schubert 	  sap->other[oidx].name
3135796c8dcSSimon Schubert 	    = xstrdup (bfd_section_name (stp->bfd, stp->the_bfd_section));
3145796c8dcSSimon Schubert 	  sap->other[oidx].sectindex = stp->the_bfd_section->index;
3155796c8dcSSimon Schubert 	  oidx++;
3165796c8dcSSimon Schubert 	}
3175796c8dcSSimon Schubert     }
3185796c8dcSSimon Schubert 
3195796c8dcSSimon Schubert   return sap;
3205796c8dcSSimon Schubert }
3215796c8dcSSimon Schubert 
322cf7f2e2dSJohn Marino /* Create a section_addr_info from section offsets in ABFD.  */
323cf7f2e2dSJohn Marino 
324cf7f2e2dSJohn Marino static struct section_addr_info *
325cf7f2e2dSJohn Marino build_section_addr_info_from_bfd (bfd *abfd)
326cf7f2e2dSJohn Marino {
327cf7f2e2dSJohn Marino   struct section_addr_info *sap;
328cf7f2e2dSJohn Marino   int i;
329cf7f2e2dSJohn Marino   struct bfd_section *sec;
330cf7f2e2dSJohn Marino 
331cf7f2e2dSJohn Marino   sap = alloc_section_addr_info (bfd_count_sections (abfd));
332cf7f2e2dSJohn Marino   for (i = 0, sec = abfd->sections; sec != NULL; sec = sec->next)
333cf7f2e2dSJohn Marino     if (bfd_get_section_flags (abfd, sec) & (SEC_ALLOC | SEC_LOAD))
334cf7f2e2dSJohn Marino       {
335cf7f2e2dSJohn Marino 	sap->other[i].addr = bfd_get_section_vma (abfd, sec);
336cf7f2e2dSJohn Marino 	sap->other[i].name = xstrdup (bfd_get_section_name (abfd, sec));
337cf7f2e2dSJohn Marino 	sap->other[i].sectindex = sec->index;
338cf7f2e2dSJohn Marino 	i++;
339cf7f2e2dSJohn Marino       }
340cf7f2e2dSJohn Marino   return sap;
341cf7f2e2dSJohn Marino }
342cf7f2e2dSJohn Marino 
343cf7f2e2dSJohn Marino /* Create a section_addr_info from section offsets in OBJFILE.  */
344cf7f2e2dSJohn Marino 
345cf7f2e2dSJohn Marino struct section_addr_info *
346cf7f2e2dSJohn Marino build_section_addr_info_from_objfile (const struct objfile *objfile)
347cf7f2e2dSJohn Marino {
348cf7f2e2dSJohn Marino   struct section_addr_info *sap;
349cf7f2e2dSJohn Marino   int i;
350cf7f2e2dSJohn Marino 
351cf7f2e2dSJohn Marino   /* Before reread_symbols gets rewritten it is not safe to call:
352cf7f2e2dSJohn Marino      gdb_assert (objfile->num_sections == bfd_count_sections (objfile->obfd));
353cf7f2e2dSJohn Marino      */
354cf7f2e2dSJohn Marino   sap = build_section_addr_info_from_bfd (objfile->obfd);
355cf7f2e2dSJohn Marino   for (i = 0; i < sap->num_sections && sap->other[i].name; i++)
356cf7f2e2dSJohn Marino     {
357cf7f2e2dSJohn Marino       int sectindex = sap->other[i].sectindex;
358cf7f2e2dSJohn Marino 
359cf7f2e2dSJohn Marino       sap->other[i].addr += objfile->section_offsets->offsets[sectindex];
360cf7f2e2dSJohn Marino     }
361cf7f2e2dSJohn Marino   return sap;
362cf7f2e2dSJohn Marino }
3635796c8dcSSimon Schubert 
3645796c8dcSSimon Schubert /* Free all memory allocated by build_section_addr_info_from_section_table.  */
3655796c8dcSSimon Schubert 
3665796c8dcSSimon Schubert extern void
3675796c8dcSSimon Schubert free_section_addr_info (struct section_addr_info *sap)
3685796c8dcSSimon Schubert {
3695796c8dcSSimon Schubert   int idx;
3705796c8dcSSimon Schubert 
3715796c8dcSSimon Schubert   for (idx = 0; idx < sap->num_sections; idx++)
3725796c8dcSSimon Schubert     if (sap->other[idx].name)
3735796c8dcSSimon Schubert       xfree (sap->other[idx].name);
3745796c8dcSSimon Schubert   xfree (sap);
3755796c8dcSSimon Schubert }
3765796c8dcSSimon Schubert 
3775796c8dcSSimon Schubert 
3785796c8dcSSimon Schubert /* Initialize OBJFILE's sect_index_* members.  */
3795796c8dcSSimon Schubert static void
3805796c8dcSSimon Schubert init_objfile_sect_indices (struct objfile *objfile)
3815796c8dcSSimon Schubert {
3825796c8dcSSimon Schubert   asection *sect;
3835796c8dcSSimon Schubert   int i;
3845796c8dcSSimon Schubert 
3855796c8dcSSimon Schubert   sect = bfd_get_section_by_name (objfile->obfd, ".text");
3865796c8dcSSimon Schubert   if (sect)
3875796c8dcSSimon Schubert     objfile->sect_index_text = sect->index;
3885796c8dcSSimon Schubert 
3895796c8dcSSimon Schubert   sect = bfd_get_section_by_name (objfile->obfd, ".data");
3905796c8dcSSimon Schubert   if (sect)
3915796c8dcSSimon Schubert     objfile->sect_index_data = sect->index;
3925796c8dcSSimon Schubert 
3935796c8dcSSimon Schubert   sect = bfd_get_section_by_name (objfile->obfd, ".bss");
3945796c8dcSSimon Schubert   if (sect)
3955796c8dcSSimon Schubert     objfile->sect_index_bss = sect->index;
3965796c8dcSSimon Schubert 
3975796c8dcSSimon Schubert   sect = bfd_get_section_by_name (objfile->obfd, ".rodata");
3985796c8dcSSimon Schubert   if (sect)
3995796c8dcSSimon Schubert     objfile->sect_index_rodata = sect->index;
4005796c8dcSSimon Schubert 
4015796c8dcSSimon Schubert   /* This is where things get really weird...  We MUST have valid
4025796c8dcSSimon Schubert      indices for the various sect_index_* members or gdb will abort.
4035796c8dcSSimon Schubert      So if for example, there is no ".text" section, we have to
4045796c8dcSSimon Schubert      accomodate that.  First, check for a file with the standard
4055796c8dcSSimon Schubert      one or two segments.  */
4065796c8dcSSimon Schubert 
4075796c8dcSSimon Schubert   symfile_find_segment_sections (objfile);
4085796c8dcSSimon Schubert 
4095796c8dcSSimon Schubert   /* Except when explicitly adding symbol files at some address,
4105796c8dcSSimon Schubert      section_offsets contains nothing but zeros, so it doesn't matter
4115796c8dcSSimon Schubert      which slot in section_offsets the individual sect_index_* members
4125796c8dcSSimon Schubert      index into.  So if they are all zero, it is safe to just point
4135796c8dcSSimon Schubert      all the currently uninitialized indices to the first slot.  But
4145796c8dcSSimon Schubert      beware: if this is the main executable, it may be relocated
4155796c8dcSSimon Schubert      later, e.g. by the remote qOffsets packet, and then this will
4165796c8dcSSimon Schubert      be wrong!  That's why we try segments first.  */
4175796c8dcSSimon Schubert 
4185796c8dcSSimon Schubert   for (i = 0; i < objfile->num_sections; i++)
4195796c8dcSSimon Schubert     {
4205796c8dcSSimon Schubert       if (ANOFFSET (objfile->section_offsets, i) != 0)
4215796c8dcSSimon Schubert 	{
4225796c8dcSSimon Schubert 	  break;
4235796c8dcSSimon Schubert 	}
4245796c8dcSSimon Schubert     }
4255796c8dcSSimon Schubert   if (i == objfile->num_sections)
4265796c8dcSSimon Schubert     {
4275796c8dcSSimon Schubert       if (objfile->sect_index_text == -1)
4285796c8dcSSimon Schubert 	objfile->sect_index_text = 0;
4295796c8dcSSimon Schubert       if (objfile->sect_index_data == -1)
4305796c8dcSSimon Schubert 	objfile->sect_index_data = 0;
4315796c8dcSSimon Schubert       if (objfile->sect_index_bss == -1)
4325796c8dcSSimon Schubert 	objfile->sect_index_bss = 0;
4335796c8dcSSimon Schubert       if (objfile->sect_index_rodata == -1)
4345796c8dcSSimon Schubert 	objfile->sect_index_rodata = 0;
4355796c8dcSSimon Schubert     }
4365796c8dcSSimon Schubert }
4375796c8dcSSimon Schubert 
4385796c8dcSSimon Schubert /* The arguments to place_section.  */
4395796c8dcSSimon Schubert 
4405796c8dcSSimon Schubert struct place_section_arg
4415796c8dcSSimon Schubert {
4425796c8dcSSimon Schubert   struct section_offsets *offsets;
4435796c8dcSSimon Schubert   CORE_ADDR lowest;
4445796c8dcSSimon Schubert };
4455796c8dcSSimon Schubert 
4465796c8dcSSimon Schubert /* Find a unique offset to use for loadable section SECT if
4475796c8dcSSimon Schubert    the user did not provide an offset.  */
4485796c8dcSSimon Schubert 
4495796c8dcSSimon Schubert static void
4505796c8dcSSimon Schubert place_section (bfd *abfd, asection *sect, void *obj)
4515796c8dcSSimon Schubert {
4525796c8dcSSimon Schubert   struct place_section_arg *arg = obj;
4535796c8dcSSimon Schubert   CORE_ADDR *offsets = arg->offsets->offsets, start_addr;
4545796c8dcSSimon Schubert   int done;
4555796c8dcSSimon Schubert   ULONGEST align = ((ULONGEST) 1) << bfd_get_section_alignment (abfd, sect);
4565796c8dcSSimon Schubert 
4575796c8dcSSimon Schubert   /* We are only interested in allocated sections.  */
4585796c8dcSSimon Schubert   if ((bfd_get_section_flags (abfd, sect) & SEC_ALLOC) == 0)
4595796c8dcSSimon Schubert     return;
4605796c8dcSSimon Schubert 
4615796c8dcSSimon Schubert   /* If the user specified an offset, honor it.  */
4625796c8dcSSimon Schubert   if (offsets[sect->index] != 0)
4635796c8dcSSimon Schubert     return;
4645796c8dcSSimon Schubert 
4655796c8dcSSimon Schubert   /* Otherwise, let's try to find a place for the section.  */
4665796c8dcSSimon Schubert   start_addr = (arg->lowest + align - 1) & -align;
4675796c8dcSSimon Schubert 
4685796c8dcSSimon Schubert   do {
4695796c8dcSSimon Schubert     asection *cur_sec;
4705796c8dcSSimon Schubert 
4715796c8dcSSimon Schubert     done = 1;
4725796c8dcSSimon Schubert 
4735796c8dcSSimon Schubert     for (cur_sec = abfd->sections; cur_sec != NULL; cur_sec = cur_sec->next)
4745796c8dcSSimon Schubert       {
4755796c8dcSSimon Schubert 	int indx = cur_sec->index;
4765796c8dcSSimon Schubert 
4775796c8dcSSimon Schubert 	/* We don't need to compare against ourself.  */
4785796c8dcSSimon Schubert 	if (cur_sec == sect)
4795796c8dcSSimon Schubert 	  continue;
4805796c8dcSSimon Schubert 
4815796c8dcSSimon Schubert 	/* We can only conflict with allocated sections.  */
4825796c8dcSSimon Schubert 	if ((bfd_get_section_flags (abfd, cur_sec) & SEC_ALLOC) == 0)
4835796c8dcSSimon Schubert 	  continue;
4845796c8dcSSimon Schubert 
4855796c8dcSSimon Schubert 	/* If the section offset is 0, either the section has not been placed
4865796c8dcSSimon Schubert 	   yet, or it was the lowest section placed (in which case LOWEST
4875796c8dcSSimon Schubert 	   will be past its end).  */
4885796c8dcSSimon Schubert 	if (offsets[indx] == 0)
4895796c8dcSSimon Schubert 	  continue;
4905796c8dcSSimon Schubert 
4915796c8dcSSimon Schubert 	/* If this section would overlap us, then we must move up.  */
4925796c8dcSSimon Schubert 	if (start_addr + bfd_get_section_size (sect) > offsets[indx]
4935796c8dcSSimon Schubert 	    && start_addr < offsets[indx] + bfd_get_section_size (cur_sec))
4945796c8dcSSimon Schubert 	  {
4955796c8dcSSimon Schubert 	    start_addr = offsets[indx] + bfd_get_section_size (cur_sec);
4965796c8dcSSimon Schubert 	    start_addr = (start_addr + align - 1) & -align;
4975796c8dcSSimon Schubert 	    done = 0;
4985796c8dcSSimon Schubert 	    break;
4995796c8dcSSimon Schubert 	  }
5005796c8dcSSimon Schubert 
5015796c8dcSSimon Schubert 	/* Otherwise, we appear to be OK.  So far.  */
5025796c8dcSSimon Schubert       }
5035796c8dcSSimon Schubert     }
5045796c8dcSSimon Schubert   while (!done);
5055796c8dcSSimon Schubert 
5065796c8dcSSimon Schubert   offsets[sect->index] = start_addr;
5075796c8dcSSimon Schubert   arg->lowest = start_addr + bfd_get_section_size (sect);
5085796c8dcSSimon Schubert }
5095796c8dcSSimon Schubert 
510cf7f2e2dSJohn Marino /* Store struct section_addr_info as prepared (made relative and with SECTINDEX
511cf7f2e2dSJohn Marino    filled-in) by addr_info_make_relative into SECTION_OFFSETS of NUM_SECTIONS
512cf7f2e2dSJohn Marino    entries.  */
5135796c8dcSSimon Schubert 
5145796c8dcSSimon Schubert void
515cf7f2e2dSJohn Marino relative_addr_info_to_section_offsets (struct section_offsets *section_offsets,
516cf7f2e2dSJohn Marino 				       int num_sections,
5175796c8dcSSimon Schubert 				       struct section_addr_info *addrs)
5185796c8dcSSimon Schubert {
5195796c8dcSSimon Schubert   int i;
5205796c8dcSSimon Schubert 
521cf7f2e2dSJohn Marino   memset (section_offsets, 0, SIZEOF_N_SECTION_OFFSETS (num_sections));
5225796c8dcSSimon Schubert 
523cf7f2e2dSJohn Marino   /* Now calculate offsets for section that were specified by the caller.  */
5245796c8dcSSimon Schubert   for (i = 0; i < addrs->num_sections && addrs->other[i].name; i++)
5255796c8dcSSimon Schubert     {
5265796c8dcSSimon Schubert       struct other_sections *osp;
5275796c8dcSSimon Schubert 
5285796c8dcSSimon Schubert       osp = &addrs->other[i];
529*a45ae5f8SJohn Marino       if (osp->sectindex == -1)
5305796c8dcSSimon Schubert   	continue;
5315796c8dcSSimon Schubert 
532c50c785cSJohn Marino       /* Record all sections in offsets.  */
5335796c8dcSSimon Schubert       /* The section_offsets in the objfile are here filled in using
5345796c8dcSSimon Schubert          the BFD index.  */
535cf7f2e2dSJohn Marino       section_offsets->offsets[osp->sectindex] = osp->addr;
5365796c8dcSSimon Schubert     }
537cf7f2e2dSJohn Marino }
538cf7f2e2dSJohn Marino 
539c50c785cSJohn Marino /* Transform section name S for a name comparison.  prelink can split section
540c50c785cSJohn Marino    `.bss' into two sections `.dynbss' and `.bss' (in this order).  Similarly
541c50c785cSJohn Marino    prelink can split `.sbss' into `.sdynbss' and `.sbss'.  Use virtual address
542c50c785cSJohn Marino    of the new `.dynbss' (`.sdynbss') section as the adjacent new `.bss'
543c50c785cSJohn Marino    (`.sbss') section has invalid (increased) virtual address.  */
544c50c785cSJohn Marino 
545c50c785cSJohn Marino static const char *
546c50c785cSJohn Marino addr_section_name (const char *s)
547c50c785cSJohn Marino {
548c50c785cSJohn Marino   if (strcmp (s, ".dynbss") == 0)
549c50c785cSJohn Marino     return ".bss";
550c50c785cSJohn Marino   if (strcmp (s, ".sdynbss") == 0)
551c50c785cSJohn Marino     return ".sbss";
552c50c785cSJohn Marino 
553c50c785cSJohn Marino   return s;
554c50c785cSJohn Marino }
555c50c785cSJohn Marino 
556cf7f2e2dSJohn Marino /* qsort comparator for addrs_section_sort.  Sort entries in ascending order by
557cf7f2e2dSJohn Marino    their (name, sectindex) pair.  sectindex makes the sort by name stable.  */
558cf7f2e2dSJohn Marino 
559cf7f2e2dSJohn Marino static int
560cf7f2e2dSJohn Marino addrs_section_compar (const void *ap, const void *bp)
561cf7f2e2dSJohn Marino {
562cf7f2e2dSJohn Marino   const struct other_sections *a = *((struct other_sections **) ap);
563cf7f2e2dSJohn Marino   const struct other_sections *b = *((struct other_sections **) bp);
564cf7f2e2dSJohn Marino   int retval, a_idx, b_idx;
565cf7f2e2dSJohn Marino 
566c50c785cSJohn Marino   retval = strcmp (addr_section_name (a->name), addr_section_name (b->name));
567cf7f2e2dSJohn Marino   if (retval)
568cf7f2e2dSJohn Marino     return retval;
569cf7f2e2dSJohn Marino 
570*a45ae5f8SJohn Marino   return a->sectindex - b->sectindex;
571cf7f2e2dSJohn Marino }
572cf7f2e2dSJohn Marino 
573cf7f2e2dSJohn Marino /* Provide sorted array of pointers to sections of ADDRS.  The array is
574cf7f2e2dSJohn Marino    terminated by NULL.  Caller is responsible to call xfree for it.  */
575cf7f2e2dSJohn Marino 
576cf7f2e2dSJohn Marino static struct other_sections **
577cf7f2e2dSJohn Marino addrs_section_sort (struct section_addr_info *addrs)
578cf7f2e2dSJohn Marino {
579cf7f2e2dSJohn Marino   struct other_sections **array;
580cf7f2e2dSJohn Marino   int i;
581cf7f2e2dSJohn Marino 
582cf7f2e2dSJohn Marino   /* `+ 1' for the NULL terminator.  */
583cf7f2e2dSJohn Marino   array = xmalloc (sizeof (*array) * (addrs->num_sections + 1));
584cf7f2e2dSJohn Marino   for (i = 0; i < addrs->num_sections && addrs->other[i].name; i++)
585cf7f2e2dSJohn Marino     array[i] = &addrs->other[i];
586cf7f2e2dSJohn Marino   array[i] = NULL;
587cf7f2e2dSJohn Marino 
588cf7f2e2dSJohn Marino   qsort (array, i, sizeof (*array), addrs_section_compar);
589cf7f2e2dSJohn Marino 
590cf7f2e2dSJohn Marino   return array;
591cf7f2e2dSJohn Marino }
592cf7f2e2dSJohn Marino 
593cf7f2e2dSJohn Marino /* Relativize absolute addresses in ADDRS into offsets based on ABFD.  Fill-in
594cf7f2e2dSJohn Marino    also SECTINDEXes specific to ABFD there.  This function can be used to
595cf7f2e2dSJohn Marino    rebase ADDRS to start referencing different BFD than before.  */
596cf7f2e2dSJohn Marino 
597cf7f2e2dSJohn Marino void
598cf7f2e2dSJohn Marino addr_info_make_relative (struct section_addr_info *addrs, bfd *abfd)
599cf7f2e2dSJohn Marino {
600cf7f2e2dSJohn Marino   asection *lower_sect;
601cf7f2e2dSJohn Marino   CORE_ADDR lower_offset;
602cf7f2e2dSJohn Marino   int i;
603cf7f2e2dSJohn Marino   struct cleanup *my_cleanup;
604cf7f2e2dSJohn Marino   struct section_addr_info *abfd_addrs;
605cf7f2e2dSJohn Marino   struct other_sections **addrs_sorted, **abfd_addrs_sorted;
606cf7f2e2dSJohn Marino   struct other_sections **addrs_to_abfd_addrs;
607cf7f2e2dSJohn Marino 
608cf7f2e2dSJohn Marino   /* Find lowest loadable section to be used as starting point for
609cf7f2e2dSJohn Marino      continguous sections.  */
610cf7f2e2dSJohn Marino   lower_sect = NULL;
611cf7f2e2dSJohn Marino   bfd_map_over_sections (abfd, find_lowest_section, &lower_sect);
612cf7f2e2dSJohn Marino   if (lower_sect == NULL)
613cf7f2e2dSJohn Marino     {
614cf7f2e2dSJohn Marino       warning (_("no loadable sections found in added symbol-file %s"),
615cf7f2e2dSJohn Marino 	       bfd_get_filename (abfd));
616cf7f2e2dSJohn Marino       lower_offset = 0;
617cf7f2e2dSJohn Marino     }
618cf7f2e2dSJohn Marino   else
619cf7f2e2dSJohn Marino     lower_offset = bfd_section_vma (bfd_get_filename (abfd), lower_sect);
620cf7f2e2dSJohn Marino 
621cf7f2e2dSJohn Marino   /* Create ADDRS_TO_ABFD_ADDRS array to map the sections in ADDRS to sections
622cf7f2e2dSJohn Marino      in ABFD.  Section names are not unique - there can be multiple sections of
623cf7f2e2dSJohn Marino      the same name.  Also the sections of the same name do not have to be
624cf7f2e2dSJohn Marino      adjacent to each other.  Some sections may be present only in one of the
625cf7f2e2dSJohn Marino      files.  Even sections present in both files do not have to be in the same
626cf7f2e2dSJohn Marino      order.
627cf7f2e2dSJohn Marino 
628cf7f2e2dSJohn Marino      Use stable sort by name for the sections in both files.  Then linearly
629cf7f2e2dSJohn Marino      scan both lists matching as most of the entries as possible.  */
630cf7f2e2dSJohn Marino 
631cf7f2e2dSJohn Marino   addrs_sorted = addrs_section_sort (addrs);
632cf7f2e2dSJohn Marino   my_cleanup = make_cleanup (xfree, addrs_sorted);
633cf7f2e2dSJohn Marino 
634cf7f2e2dSJohn Marino   abfd_addrs = build_section_addr_info_from_bfd (abfd);
635cf7f2e2dSJohn Marino   make_cleanup_free_section_addr_info (abfd_addrs);
636cf7f2e2dSJohn Marino   abfd_addrs_sorted = addrs_section_sort (abfd_addrs);
637cf7f2e2dSJohn Marino   make_cleanup (xfree, abfd_addrs_sorted);
638cf7f2e2dSJohn Marino 
639c50c785cSJohn Marino   /* Now create ADDRS_TO_ABFD_ADDRS from ADDRS_SORTED and
640c50c785cSJohn Marino      ABFD_ADDRS_SORTED.  */
641cf7f2e2dSJohn Marino 
642cf7f2e2dSJohn Marino   addrs_to_abfd_addrs = xzalloc (sizeof (*addrs_to_abfd_addrs)
643cf7f2e2dSJohn Marino 				 * addrs->num_sections);
644cf7f2e2dSJohn Marino   make_cleanup (xfree, addrs_to_abfd_addrs);
645cf7f2e2dSJohn Marino 
646cf7f2e2dSJohn Marino   while (*addrs_sorted)
647cf7f2e2dSJohn Marino     {
648c50c785cSJohn Marino       const char *sect_name = addr_section_name ((*addrs_sorted)->name);
649cf7f2e2dSJohn Marino 
650cf7f2e2dSJohn Marino       while (*abfd_addrs_sorted
651c50c785cSJohn Marino 	     && strcmp (addr_section_name ((*abfd_addrs_sorted)->name),
652c50c785cSJohn Marino 			sect_name) < 0)
653cf7f2e2dSJohn Marino 	abfd_addrs_sorted++;
654cf7f2e2dSJohn Marino 
655cf7f2e2dSJohn Marino       if (*abfd_addrs_sorted
656c50c785cSJohn Marino 	  && strcmp (addr_section_name ((*abfd_addrs_sorted)->name),
657c50c785cSJohn Marino 		     sect_name) == 0)
658cf7f2e2dSJohn Marino 	{
659cf7f2e2dSJohn Marino 	  int index_in_addrs;
660cf7f2e2dSJohn Marino 
661cf7f2e2dSJohn Marino 	  /* Make the found item directly addressable from ADDRS.  */
662cf7f2e2dSJohn Marino 	  index_in_addrs = *addrs_sorted - addrs->other;
663cf7f2e2dSJohn Marino 	  gdb_assert (addrs_to_abfd_addrs[index_in_addrs] == NULL);
664cf7f2e2dSJohn Marino 	  addrs_to_abfd_addrs[index_in_addrs] = *abfd_addrs_sorted;
665cf7f2e2dSJohn Marino 
666cf7f2e2dSJohn Marino 	  /* Never use the same ABFD entry twice.  */
667cf7f2e2dSJohn Marino 	  abfd_addrs_sorted++;
668cf7f2e2dSJohn Marino 	}
669cf7f2e2dSJohn Marino 
670cf7f2e2dSJohn Marino       addrs_sorted++;
671cf7f2e2dSJohn Marino     }
672cf7f2e2dSJohn Marino 
673cf7f2e2dSJohn Marino   /* Calculate offsets for the loadable sections.
674cf7f2e2dSJohn Marino      FIXME! Sections must be in order of increasing loadable section
675cf7f2e2dSJohn Marino      so that contiguous sections can use the lower-offset!!!
676cf7f2e2dSJohn Marino 
677cf7f2e2dSJohn Marino      Adjust offsets if the segments are not contiguous.
678cf7f2e2dSJohn Marino      If the section is contiguous, its offset should be set to
679cf7f2e2dSJohn Marino      the offset of the highest loadable section lower than it
680cf7f2e2dSJohn Marino      (the loadable section directly below it in memory).
681cf7f2e2dSJohn Marino      this_offset = lower_offset = lower_addr - lower_orig_addr */
682cf7f2e2dSJohn Marino 
683cf7f2e2dSJohn Marino   for (i = 0; i < addrs->num_sections && addrs->other[i].name; i++)
684cf7f2e2dSJohn Marino     {
685cf7f2e2dSJohn Marino       struct other_sections *sect = addrs_to_abfd_addrs[i];
686cf7f2e2dSJohn Marino 
687cf7f2e2dSJohn Marino       if (sect)
688cf7f2e2dSJohn Marino 	{
689cf7f2e2dSJohn Marino 	  /* This is the index used by BFD.  */
690cf7f2e2dSJohn Marino 	  addrs->other[i].sectindex = sect->sectindex;
691cf7f2e2dSJohn Marino 
692cf7f2e2dSJohn Marino 	  if (addrs->other[i].addr != 0)
693cf7f2e2dSJohn Marino 	    {
694cf7f2e2dSJohn Marino 	      addrs->other[i].addr -= sect->addr;
695cf7f2e2dSJohn Marino 	      lower_offset = addrs->other[i].addr;
696cf7f2e2dSJohn Marino 	    }
697cf7f2e2dSJohn Marino 	  else
698cf7f2e2dSJohn Marino 	    addrs->other[i].addr = lower_offset;
699cf7f2e2dSJohn Marino 	}
700cf7f2e2dSJohn Marino       else
701cf7f2e2dSJohn Marino 	{
702c50c785cSJohn Marino 	  /* addr_section_name transformation is not used for SECT_NAME.  */
703c50c785cSJohn Marino 	  const char *sect_name = addrs->other[i].name;
704c50c785cSJohn Marino 
705cf7f2e2dSJohn Marino 	  /* This section does not exist in ABFD, which is normally
706cf7f2e2dSJohn Marino 	     unexpected and we want to issue a warning.
707cf7f2e2dSJohn Marino 
708cf7f2e2dSJohn Marino 	     However, the ELF prelinker does create a few sections which are
709cf7f2e2dSJohn Marino 	     marked in the main executable as loadable (they are loaded in
710cf7f2e2dSJohn Marino 	     memory from the DYNAMIC segment) and yet are not present in
711cf7f2e2dSJohn Marino 	     separate debug info files.  This is fine, and should not cause
712cf7f2e2dSJohn Marino 	     a warning.  Shared libraries contain just the section
713cf7f2e2dSJohn Marino 	     ".gnu.liblist" but it is not marked as loadable there.  There is
714cf7f2e2dSJohn Marino 	     no other way to identify them than by their name as the sections
715c50c785cSJohn Marino 	     created by prelink have no special flags.
716c50c785cSJohn Marino 
717c50c785cSJohn Marino 	     For the sections `.bss' and `.sbss' see addr_section_name.  */
718cf7f2e2dSJohn Marino 
719cf7f2e2dSJohn Marino 	  if (!(strcmp (sect_name, ".gnu.liblist") == 0
720cf7f2e2dSJohn Marino 		|| strcmp (sect_name, ".gnu.conflict") == 0
721c50c785cSJohn Marino 		|| (strcmp (sect_name, ".bss") == 0
722c50c785cSJohn Marino 		    && i > 0
723c50c785cSJohn Marino 		    && strcmp (addrs->other[i - 1].name, ".dynbss") == 0
724c50c785cSJohn Marino 		    && addrs_to_abfd_addrs[i - 1] != NULL)
725c50c785cSJohn Marino 		|| (strcmp (sect_name, ".sbss") == 0
726c50c785cSJohn Marino 		    && i > 0
727c50c785cSJohn Marino 		    && strcmp (addrs->other[i - 1].name, ".sdynbss") == 0
728c50c785cSJohn Marino 		    && addrs_to_abfd_addrs[i - 1] != NULL)))
729cf7f2e2dSJohn Marino 	    warning (_("section %s not found in %s"), sect_name,
730cf7f2e2dSJohn Marino 		     bfd_get_filename (abfd));
731cf7f2e2dSJohn Marino 
732cf7f2e2dSJohn Marino 	  addrs->other[i].addr = 0;
733*a45ae5f8SJohn Marino 	  addrs->other[i].sectindex = -1;
734cf7f2e2dSJohn Marino 	}
735cf7f2e2dSJohn Marino     }
736cf7f2e2dSJohn Marino 
737cf7f2e2dSJohn Marino   do_cleanups (my_cleanup);
738cf7f2e2dSJohn Marino }
739cf7f2e2dSJohn Marino 
740cf7f2e2dSJohn Marino /* Parse the user's idea of an offset for dynamic linking, into our idea
741cf7f2e2dSJohn Marino    of how to represent it for fast symbol reading.  This is the default
742cf7f2e2dSJohn Marino    version of the sym_fns.sym_offsets function for symbol readers that
743cf7f2e2dSJohn Marino    don't need to do anything special.  It allocates a section_offsets table
744cf7f2e2dSJohn Marino    for the objectfile OBJFILE and stuffs ADDR into all of the offsets.  */
745cf7f2e2dSJohn Marino 
746cf7f2e2dSJohn Marino void
747cf7f2e2dSJohn Marino default_symfile_offsets (struct objfile *objfile,
748cf7f2e2dSJohn Marino 			 struct section_addr_info *addrs)
749cf7f2e2dSJohn Marino {
750cf7f2e2dSJohn Marino   objfile->num_sections = bfd_count_sections (objfile->obfd);
751cf7f2e2dSJohn Marino   objfile->section_offsets = (struct section_offsets *)
752cf7f2e2dSJohn Marino     obstack_alloc (&objfile->objfile_obstack,
753cf7f2e2dSJohn Marino 		   SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
754cf7f2e2dSJohn Marino   relative_addr_info_to_section_offsets (objfile->section_offsets,
755cf7f2e2dSJohn Marino 					 objfile->num_sections, addrs);
7565796c8dcSSimon Schubert 
7575796c8dcSSimon Schubert   /* For relocatable files, all loadable sections will start at zero.
7585796c8dcSSimon Schubert      The zero is meaningless, so try to pick arbitrary addresses such
7595796c8dcSSimon Schubert      that no loadable sections overlap.  This algorithm is quadratic,
7605796c8dcSSimon Schubert      but the number of sections in a single object file is generally
7615796c8dcSSimon Schubert      small.  */
7625796c8dcSSimon Schubert   if ((bfd_get_file_flags (objfile->obfd) & (EXEC_P | DYNAMIC)) == 0)
7635796c8dcSSimon Schubert     {
7645796c8dcSSimon Schubert       struct place_section_arg arg;
7655796c8dcSSimon Schubert       bfd *abfd = objfile->obfd;
7665796c8dcSSimon Schubert       asection *cur_sec;
7675796c8dcSSimon Schubert 
7685796c8dcSSimon Schubert       for (cur_sec = abfd->sections; cur_sec != NULL; cur_sec = cur_sec->next)
7695796c8dcSSimon Schubert 	/* We do not expect this to happen; just skip this step if the
7705796c8dcSSimon Schubert 	   relocatable file has a section with an assigned VMA.  */
7715796c8dcSSimon Schubert 	if (bfd_section_vma (abfd, cur_sec) != 0)
7725796c8dcSSimon Schubert 	  break;
7735796c8dcSSimon Schubert 
7745796c8dcSSimon Schubert       if (cur_sec == NULL)
7755796c8dcSSimon Schubert 	{
7765796c8dcSSimon Schubert 	  CORE_ADDR *offsets = objfile->section_offsets->offsets;
7775796c8dcSSimon Schubert 
7785796c8dcSSimon Schubert 	  /* Pick non-overlapping offsets for sections the user did not
7795796c8dcSSimon Schubert 	     place explicitly.  */
7805796c8dcSSimon Schubert 	  arg.offsets = objfile->section_offsets;
7815796c8dcSSimon Schubert 	  arg.lowest = 0;
7825796c8dcSSimon Schubert 	  bfd_map_over_sections (objfile->obfd, place_section, &arg);
7835796c8dcSSimon Schubert 
7845796c8dcSSimon Schubert 	  /* Correctly filling in the section offsets is not quite
7855796c8dcSSimon Schubert 	     enough.  Relocatable files have two properties that
7865796c8dcSSimon Schubert 	     (most) shared objects do not:
7875796c8dcSSimon Schubert 
7885796c8dcSSimon Schubert 	     - Their debug information will contain relocations.  Some
7895796c8dcSSimon Schubert 	     shared libraries do also, but many do not, so this can not
7905796c8dcSSimon Schubert 	     be assumed.
7915796c8dcSSimon Schubert 
7925796c8dcSSimon Schubert 	     - If there are multiple code sections they will be loaded
7935796c8dcSSimon Schubert 	     at different relative addresses in memory than they are
7945796c8dcSSimon Schubert 	     in the objfile, since all sections in the file will start
7955796c8dcSSimon Schubert 	     at address zero.
7965796c8dcSSimon Schubert 
7975796c8dcSSimon Schubert 	     Because GDB has very limited ability to map from an
7985796c8dcSSimon Schubert 	     address in debug info to the correct code section,
7995796c8dcSSimon Schubert 	     it relies on adding SECT_OFF_TEXT to things which might be
8005796c8dcSSimon Schubert 	     code.  If we clear all the section offsets, and set the
8015796c8dcSSimon Schubert 	     section VMAs instead, then symfile_relocate_debug_section
8025796c8dcSSimon Schubert 	     will return meaningful debug information pointing at the
8035796c8dcSSimon Schubert 	     correct sections.
8045796c8dcSSimon Schubert 
8055796c8dcSSimon Schubert 	     GDB has too many different data structures for section
8065796c8dcSSimon Schubert 	     addresses - a bfd, objfile, and so_list all have section
8075796c8dcSSimon Schubert 	     tables, as does exec_ops.  Some of these could probably
8085796c8dcSSimon Schubert 	     be eliminated.  */
8095796c8dcSSimon Schubert 
8105796c8dcSSimon Schubert 	  for (cur_sec = abfd->sections; cur_sec != NULL;
8115796c8dcSSimon Schubert 	       cur_sec = cur_sec->next)
8125796c8dcSSimon Schubert 	    {
8135796c8dcSSimon Schubert 	      if ((bfd_get_section_flags (abfd, cur_sec) & SEC_ALLOC) == 0)
8145796c8dcSSimon Schubert 		continue;
8155796c8dcSSimon Schubert 
8165796c8dcSSimon Schubert 	      bfd_set_section_vma (abfd, cur_sec, offsets[cur_sec->index]);
817c50c785cSJohn Marino 	      exec_set_section_address (bfd_get_filename (abfd),
818c50c785cSJohn Marino 					cur_sec->index,
8195796c8dcSSimon Schubert 					offsets[cur_sec->index]);
8205796c8dcSSimon Schubert 	      offsets[cur_sec->index] = 0;
8215796c8dcSSimon Schubert 	    }
8225796c8dcSSimon Schubert 	}
8235796c8dcSSimon Schubert     }
8245796c8dcSSimon Schubert 
8255796c8dcSSimon Schubert   /* Remember the bfd indexes for the .text, .data, .bss and
8265796c8dcSSimon Schubert      .rodata sections.  */
8275796c8dcSSimon Schubert   init_objfile_sect_indices (objfile);
8285796c8dcSSimon Schubert }
8295796c8dcSSimon Schubert 
8305796c8dcSSimon Schubert 
8315796c8dcSSimon Schubert /* Divide the file into segments, which are individual relocatable units.
8325796c8dcSSimon Schubert    This is the default version of the sym_fns.sym_segments function for
8335796c8dcSSimon Schubert    symbol readers that do not have an explicit representation of segments.
8345796c8dcSSimon Schubert    It assumes that object files do not have segments, and fully linked
8355796c8dcSSimon Schubert    files have a single segment.  */
8365796c8dcSSimon Schubert 
8375796c8dcSSimon Schubert struct symfile_segment_data *
8385796c8dcSSimon Schubert default_symfile_segments (bfd *abfd)
8395796c8dcSSimon Schubert {
8405796c8dcSSimon Schubert   int num_sections, i;
8415796c8dcSSimon Schubert   asection *sect;
8425796c8dcSSimon Schubert   struct symfile_segment_data *data;
8435796c8dcSSimon Schubert   CORE_ADDR low, high;
8445796c8dcSSimon Schubert 
8455796c8dcSSimon Schubert   /* Relocatable files contain enough information to position each
8465796c8dcSSimon Schubert      loadable section independently; they should not be relocated
8475796c8dcSSimon Schubert      in segments.  */
8485796c8dcSSimon Schubert   if ((bfd_get_file_flags (abfd) & (EXEC_P | DYNAMIC)) == 0)
8495796c8dcSSimon Schubert     return NULL;
8505796c8dcSSimon Schubert 
8515796c8dcSSimon Schubert   /* Make sure there is at least one loadable section in the file.  */
8525796c8dcSSimon Schubert   for (sect = abfd->sections; sect != NULL; sect = sect->next)
8535796c8dcSSimon Schubert     {
8545796c8dcSSimon Schubert       if ((bfd_get_section_flags (abfd, sect) & SEC_ALLOC) == 0)
8555796c8dcSSimon Schubert 	continue;
8565796c8dcSSimon Schubert 
8575796c8dcSSimon Schubert       break;
8585796c8dcSSimon Schubert     }
8595796c8dcSSimon Schubert   if (sect == NULL)
8605796c8dcSSimon Schubert     return NULL;
8615796c8dcSSimon Schubert 
8625796c8dcSSimon Schubert   low = bfd_get_section_vma (abfd, sect);
8635796c8dcSSimon Schubert   high = low + bfd_get_section_size (sect);
8645796c8dcSSimon Schubert 
8655796c8dcSSimon Schubert   data = XZALLOC (struct symfile_segment_data);
8665796c8dcSSimon Schubert   data->num_segments = 1;
8675796c8dcSSimon Schubert   data->segment_bases = XCALLOC (1, CORE_ADDR);
8685796c8dcSSimon Schubert   data->segment_sizes = XCALLOC (1, CORE_ADDR);
8695796c8dcSSimon Schubert 
8705796c8dcSSimon Schubert   num_sections = bfd_count_sections (abfd);
8715796c8dcSSimon Schubert   data->segment_info = XCALLOC (num_sections, int);
8725796c8dcSSimon Schubert 
8735796c8dcSSimon Schubert   for (i = 0, sect = abfd->sections; sect != NULL; i++, sect = sect->next)
8745796c8dcSSimon Schubert     {
8755796c8dcSSimon Schubert       CORE_ADDR vma;
8765796c8dcSSimon Schubert 
8775796c8dcSSimon Schubert       if ((bfd_get_section_flags (abfd, sect) & SEC_ALLOC) == 0)
8785796c8dcSSimon Schubert 	continue;
8795796c8dcSSimon Schubert 
8805796c8dcSSimon Schubert       vma = bfd_get_section_vma (abfd, sect);
8815796c8dcSSimon Schubert       if (vma < low)
8825796c8dcSSimon Schubert 	low = vma;
8835796c8dcSSimon Schubert       if (vma + bfd_get_section_size (sect) > high)
8845796c8dcSSimon Schubert 	high = vma + bfd_get_section_size (sect);
8855796c8dcSSimon Schubert 
8865796c8dcSSimon Schubert       data->segment_info[i] = 1;
8875796c8dcSSimon Schubert     }
8885796c8dcSSimon Schubert 
8895796c8dcSSimon Schubert   data->segment_bases[0] = low;
8905796c8dcSSimon Schubert   data->segment_sizes[0] = high - low;
8915796c8dcSSimon Schubert 
8925796c8dcSSimon Schubert   return data;
8935796c8dcSSimon Schubert }
8945796c8dcSSimon Schubert 
8955796c8dcSSimon Schubert /* Process a symbol file, as either the main file or as a dynamically
8965796c8dcSSimon Schubert    loaded file.
8975796c8dcSSimon Schubert 
8985796c8dcSSimon Schubert    OBJFILE is where the symbols are to be read from.
8995796c8dcSSimon Schubert 
9005796c8dcSSimon Schubert    ADDRS is the list of section load addresses.  If the user has given
9015796c8dcSSimon Schubert    an 'add-symbol-file' command, then this is the list of offsets and
9025796c8dcSSimon Schubert    addresses he or she provided as arguments to the command; or, if
9035796c8dcSSimon Schubert    we're handling a shared library, these are the actual addresses the
9045796c8dcSSimon Schubert    sections are loaded at, according to the inferior's dynamic linker
9055796c8dcSSimon Schubert    (as gleaned by GDB's shared library code).  We convert each address
9065796c8dcSSimon Schubert    into an offset from the section VMA's as it appears in the object
9075796c8dcSSimon Schubert    file, and then call the file's sym_offsets function to convert this
9085796c8dcSSimon Schubert    into a format-specific offset table --- a `struct section_offsets'.
9095796c8dcSSimon Schubert    If ADDRS is non-zero, OFFSETS must be zero.
9105796c8dcSSimon Schubert 
9115796c8dcSSimon Schubert    OFFSETS is a table of section offsets already in the right
9125796c8dcSSimon Schubert    format-specific representation.  NUM_OFFSETS is the number of
9135796c8dcSSimon Schubert    elements present in OFFSETS->offsets.  If OFFSETS is non-zero, we
9145796c8dcSSimon Schubert    assume this is the proper table the call to sym_offsets described
9155796c8dcSSimon Schubert    above would produce.  Instead of calling sym_offsets, we just dump
9165796c8dcSSimon Schubert    it right into objfile->section_offsets.  (When we're re-reading
9175796c8dcSSimon Schubert    symbols from an objfile, we don't have the original load address
9185796c8dcSSimon Schubert    list any more; all we have is the section offset table.)  If
9195796c8dcSSimon Schubert    OFFSETS is non-zero, ADDRS must be zero.
9205796c8dcSSimon Schubert 
9215796c8dcSSimon Schubert    ADD_FLAGS encodes verbosity level, whether this is main symbol or
9225796c8dcSSimon Schubert    an extra symbol file such as dynamically loaded code, and wether
9235796c8dcSSimon Schubert    breakpoint reset should be deferred.  */
9245796c8dcSSimon Schubert 
9255796c8dcSSimon Schubert void
9265796c8dcSSimon Schubert syms_from_objfile (struct objfile *objfile,
9275796c8dcSSimon Schubert                    struct section_addr_info *addrs,
9285796c8dcSSimon Schubert                    struct section_offsets *offsets,
9295796c8dcSSimon Schubert                    int num_offsets,
9305796c8dcSSimon Schubert 		   int add_flags)
9315796c8dcSSimon Schubert {
9325796c8dcSSimon Schubert   struct section_addr_info *local_addr = NULL;
9335796c8dcSSimon Schubert   struct cleanup *old_chain;
9345796c8dcSSimon Schubert   const int mainline = add_flags & SYMFILE_MAINLINE;
9355796c8dcSSimon Schubert 
9365796c8dcSSimon Schubert   gdb_assert (! (addrs && offsets));
9375796c8dcSSimon Schubert 
9385796c8dcSSimon Schubert   init_entry_point_info (objfile);
9395796c8dcSSimon Schubert   objfile->sf = find_sym_fns (objfile->obfd);
9405796c8dcSSimon Schubert 
9415796c8dcSSimon Schubert   if (objfile->sf == NULL)
9425796c8dcSSimon Schubert     return;	/* No symbols.  */
9435796c8dcSSimon Schubert 
9445796c8dcSSimon Schubert   /* Make sure that partially constructed symbol tables will be cleaned up
9455796c8dcSSimon Schubert      if an error occurs during symbol reading.  */
9465796c8dcSSimon Schubert   old_chain = make_cleanup_free_objfile (objfile);
9475796c8dcSSimon Schubert 
9485796c8dcSSimon Schubert   /* If ADDRS and OFFSETS are both NULL, put together a dummy address
9495796c8dcSSimon Schubert      list.  We now establish the convention that an addr of zero means
9505796c8dcSSimon Schubert      no load address was specified.  */
9515796c8dcSSimon Schubert   if (! addrs && ! offsets)
9525796c8dcSSimon Schubert     {
9535796c8dcSSimon Schubert       local_addr
9545796c8dcSSimon Schubert 	= alloc_section_addr_info (bfd_count_sections (objfile->obfd));
9555796c8dcSSimon Schubert       make_cleanup (xfree, local_addr);
9565796c8dcSSimon Schubert       addrs = local_addr;
9575796c8dcSSimon Schubert     }
9585796c8dcSSimon Schubert 
9595796c8dcSSimon Schubert   /* Now either addrs or offsets is non-zero.  */
9605796c8dcSSimon Schubert 
9615796c8dcSSimon Schubert   if (mainline)
9625796c8dcSSimon Schubert     {
9635796c8dcSSimon Schubert       /* We will modify the main symbol table, make sure that all its users
9645796c8dcSSimon Schubert          will be cleaned up if an error occurs during symbol reading.  */
9655796c8dcSSimon Schubert       make_cleanup (clear_symtab_users_cleanup, 0 /*ignore*/);
9665796c8dcSSimon Schubert 
9675796c8dcSSimon Schubert       /* Since no error yet, throw away the old symbol table.  */
9685796c8dcSSimon Schubert 
9695796c8dcSSimon Schubert       if (symfile_objfile != NULL)
9705796c8dcSSimon Schubert 	{
9715796c8dcSSimon Schubert 	  free_objfile (symfile_objfile);
9725796c8dcSSimon Schubert 	  gdb_assert (symfile_objfile == NULL);
9735796c8dcSSimon Schubert 	}
9745796c8dcSSimon Schubert 
9755796c8dcSSimon Schubert       /* Currently we keep symbols from the add-symbol-file command.
9765796c8dcSSimon Schubert          If the user wants to get rid of them, they should do "symbol-file"
9775796c8dcSSimon Schubert          without arguments first.  Not sure this is the best behavior
9785796c8dcSSimon Schubert          (PR 2207).  */
9795796c8dcSSimon Schubert 
9805796c8dcSSimon Schubert       (*objfile->sf->sym_new_init) (objfile);
9815796c8dcSSimon Schubert     }
9825796c8dcSSimon Schubert 
9835796c8dcSSimon Schubert   /* Convert addr into an offset rather than an absolute address.
9845796c8dcSSimon Schubert      We find the lowest address of a loaded segment in the objfile,
9855796c8dcSSimon Schubert      and assume that <addr> is where that got loaded.
9865796c8dcSSimon Schubert 
9875796c8dcSSimon Schubert      We no longer warn if the lowest section is not a text segment (as
9885796c8dcSSimon Schubert      happens for the PA64 port.  */
989cf7f2e2dSJohn Marino   if (addrs && addrs->other[0].name)
990cf7f2e2dSJohn Marino     addr_info_make_relative (addrs, objfile->obfd);
9915796c8dcSSimon Schubert 
9925796c8dcSSimon Schubert   /* Initialize symbol reading routines for this objfile, allow complaints to
9935796c8dcSSimon Schubert      appear for this new file, and record how verbose to be, then do the
9945796c8dcSSimon Schubert      initial symbol reading for this file.  */
9955796c8dcSSimon Schubert 
9965796c8dcSSimon Schubert   (*objfile->sf->sym_init) (objfile);
9975796c8dcSSimon Schubert   clear_complaints (&symfile_complaints, 1, add_flags & SYMFILE_VERBOSE);
9985796c8dcSSimon Schubert 
9995796c8dcSSimon Schubert   if (addrs)
10005796c8dcSSimon Schubert     (*objfile->sf->sym_offsets) (objfile, addrs);
10015796c8dcSSimon Schubert   else
10025796c8dcSSimon Schubert     {
10035796c8dcSSimon Schubert       size_t size = SIZEOF_N_SECTION_OFFSETS (num_offsets);
10045796c8dcSSimon Schubert 
10055796c8dcSSimon Schubert       /* Just copy in the offset table directly as given to us.  */
10065796c8dcSSimon Schubert       objfile->num_sections = num_offsets;
10075796c8dcSSimon Schubert       objfile->section_offsets
10085796c8dcSSimon Schubert         = ((struct section_offsets *)
10095796c8dcSSimon Schubert            obstack_alloc (&objfile->objfile_obstack, size));
10105796c8dcSSimon Schubert       memcpy (objfile->section_offsets, offsets, size);
10115796c8dcSSimon Schubert 
10125796c8dcSSimon Schubert       init_objfile_sect_indices (objfile);
10135796c8dcSSimon Schubert     }
10145796c8dcSSimon Schubert 
1015cf7f2e2dSJohn Marino   (*objfile->sf->sym_read) (objfile, add_flags);
10165796c8dcSSimon Schubert 
1017c50c785cSJohn Marino   if ((add_flags & SYMFILE_NO_READ) == 0)
1018c50c785cSJohn Marino     require_partial_symbols (objfile, 0);
1019c50c785cSJohn Marino 
10205796c8dcSSimon Schubert   /* Discard cleanups as symbol reading was successful.  */
10215796c8dcSSimon Schubert 
10225796c8dcSSimon Schubert   discard_cleanups (old_chain);
10235796c8dcSSimon Schubert   xfree (local_addr);
10245796c8dcSSimon Schubert }
10255796c8dcSSimon Schubert 
10265796c8dcSSimon Schubert /* Perform required actions after either reading in the initial
10275796c8dcSSimon Schubert    symbols for a new objfile, or mapping in the symbols from a reusable
1028c50c785cSJohn Marino    objfile.  ADD_FLAGS is a bitmask of enum symfile_add_flags.  */
10295796c8dcSSimon Schubert 
10305796c8dcSSimon Schubert void
10315796c8dcSSimon Schubert new_symfile_objfile (struct objfile *objfile, int add_flags)
10325796c8dcSSimon Schubert {
10335796c8dcSSimon Schubert   /* If this is the main symbol file we have to clean up all users of the
10345796c8dcSSimon Schubert      old main symbol file.  Otherwise it is sufficient to fixup all the
10355796c8dcSSimon Schubert      breakpoints that may have been redefined by this symbol file.  */
10365796c8dcSSimon Schubert   if (add_flags & SYMFILE_MAINLINE)
10375796c8dcSSimon Schubert     {
10385796c8dcSSimon Schubert       /* OK, make it the "real" symbol file.  */
10395796c8dcSSimon Schubert       symfile_objfile = objfile;
10405796c8dcSSimon Schubert 
1041c50c785cSJohn Marino       clear_symtab_users (add_flags);
10425796c8dcSSimon Schubert     }
10435796c8dcSSimon Schubert   else if ((add_flags & SYMFILE_DEFER_BP_RESET) == 0)
10445796c8dcSSimon Schubert     {
10455796c8dcSSimon Schubert       breakpoint_re_set ();
10465796c8dcSSimon Schubert     }
10475796c8dcSSimon Schubert 
10485796c8dcSSimon Schubert   /* We're done reading the symbol file; finish off complaints.  */
10495796c8dcSSimon Schubert   clear_complaints (&symfile_complaints, 0, add_flags & SYMFILE_VERBOSE);
10505796c8dcSSimon Schubert }
10515796c8dcSSimon Schubert 
10525796c8dcSSimon Schubert /* Process a symbol file, as either the main file or as a dynamically
10535796c8dcSSimon Schubert    loaded file.
10545796c8dcSSimon Schubert 
10555796c8dcSSimon Schubert    ABFD is a BFD already open on the file, as from symfile_bfd_open.
10565796c8dcSSimon Schubert    This BFD will be closed on error, and is always consumed by this function.
10575796c8dcSSimon Schubert 
10585796c8dcSSimon Schubert    ADD_FLAGS encodes verbosity, whether this is main symbol file or
10595796c8dcSSimon Schubert    extra, such as dynamically loaded code, and what to do with breakpoins.
10605796c8dcSSimon Schubert 
10615796c8dcSSimon Schubert    ADDRS, OFFSETS, and NUM_OFFSETS are as described for
10625796c8dcSSimon Schubert    syms_from_objfile, above.
10635796c8dcSSimon Schubert    ADDRS is ignored when SYMFILE_MAINLINE bit is set in ADD_FLAGS.
10645796c8dcSSimon Schubert 
1065*a45ae5f8SJohn Marino    PARENT is the original objfile if ABFD is a separate debug info file.
1066*a45ae5f8SJohn Marino    Otherwise PARENT is NULL.
1067*a45ae5f8SJohn Marino 
10685796c8dcSSimon Schubert    Upon success, returns a pointer to the objfile that was added.
10695796c8dcSSimon Schubert    Upon failure, jumps back to command level (never returns).  */
10705796c8dcSSimon Schubert 
10715796c8dcSSimon Schubert static struct objfile *
10725796c8dcSSimon Schubert symbol_file_add_with_addrs_or_offsets (bfd *abfd,
10735796c8dcSSimon Schubert                                        int add_flags,
10745796c8dcSSimon Schubert                                        struct section_addr_info *addrs,
10755796c8dcSSimon Schubert                                        struct section_offsets *offsets,
10765796c8dcSSimon Schubert                                        int num_offsets,
1077*a45ae5f8SJohn Marino                                        int flags, struct objfile *parent)
10785796c8dcSSimon Schubert {
10795796c8dcSSimon Schubert   struct objfile *objfile;
10805796c8dcSSimon Schubert   struct cleanup *my_cleanups;
10815796c8dcSSimon Schubert   const char *name = bfd_get_filename (abfd);
10825796c8dcSSimon Schubert   const int from_tty = add_flags & SYMFILE_VERBOSE;
1083*a45ae5f8SJohn Marino   const int mainline = add_flags & SYMFILE_MAINLINE;
1084c50c785cSJohn Marino   const int should_print = ((from_tty || info_verbose)
1085c50c785cSJohn Marino 			    && (readnow_symbol_files
1086c50c785cSJohn Marino 				|| (add_flags & SYMFILE_NO_READ) == 0));
1087c50c785cSJohn Marino 
1088c50c785cSJohn Marino   if (readnow_symbol_files)
1089c50c785cSJohn Marino     {
1090c50c785cSJohn Marino       flags |= OBJF_READNOW;
1091c50c785cSJohn Marino       add_flags &= ~SYMFILE_NO_READ;
1092c50c785cSJohn Marino     }
10935796c8dcSSimon Schubert 
10945796c8dcSSimon Schubert   my_cleanups = make_cleanup_bfd_close (abfd);
10955796c8dcSSimon Schubert 
10965796c8dcSSimon Schubert   /* Give user a chance to burp if we'd be
10975796c8dcSSimon Schubert      interactively wiping out any existing symbols.  */
10985796c8dcSSimon Schubert 
10995796c8dcSSimon Schubert   if ((have_full_symbols () || have_partial_symbols ())
1100*a45ae5f8SJohn Marino       && mainline
11015796c8dcSSimon Schubert       && from_tty
11025796c8dcSSimon Schubert       && !query (_("Load new symbol table from \"%s\"? "), name))
11035796c8dcSSimon Schubert     error (_("Not confirmed."));
11045796c8dcSSimon Schubert 
1105*a45ae5f8SJohn Marino   objfile = allocate_objfile (abfd, flags | (mainline ? OBJF_MAINLINE : 0));
11065796c8dcSSimon Schubert   discard_cleanups (my_cleanups);
11075796c8dcSSimon Schubert 
1108*a45ae5f8SJohn Marino   if (parent)
1109*a45ae5f8SJohn Marino     add_separate_debug_objfile (objfile, parent);
1110*a45ae5f8SJohn Marino 
11115796c8dcSSimon Schubert   /* We either created a new mapped symbol table, mapped an existing
11125796c8dcSSimon Schubert      symbol table file which has not had initial symbol reading
11135796c8dcSSimon Schubert      performed, or need to read an unmapped symbol table.  */
1114c50c785cSJohn Marino   if (should_print)
11155796c8dcSSimon Schubert     {
11165796c8dcSSimon Schubert       if (deprecated_pre_add_symbol_hook)
11175796c8dcSSimon Schubert 	deprecated_pre_add_symbol_hook (name);
11185796c8dcSSimon Schubert       else
11195796c8dcSSimon Schubert 	{
11205796c8dcSSimon Schubert 	  printf_unfiltered (_("Reading symbols from %s..."), name);
11215796c8dcSSimon Schubert 	  wrap_here ("");
11225796c8dcSSimon Schubert 	  gdb_flush (gdb_stdout);
11235796c8dcSSimon Schubert 	}
11245796c8dcSSimon Schubert     }
11255796c8dcSSimon Schubert   syms_from_objfile (objfile, addrs, offsets, num_offsets,
11265796c8dcSSimon Schubert 		     add_flags);
11275796c8dcSSimon Schubert 
11285796c8dcSSimon Schubert   /* We now have at least a partial symbol table.  Check to see if the
11295796c8dcSSimon Schubert      user requested that all symbols be read on initial access via either
11305796c8dcSSimon Schubert      the gdb startup command line or on a per symbol file basis.  Expand
11315796c8dcSSimon Schubert      all partial symbol tables for this objfile if so.  */
11325796c8dcSSimon Schubert 
1133c50c785cSJohn Marino   if ((flags & OBJF_READNOW))
11345796c8dcSSimon Schubert     {
1135c50c785cSJohn Marino       if (should_print)
11365796c8dcSSimon Schubert 	{
11375796c8dcSSimon Schubert 	  printf_unfiltered (_("expanding to full symbols..."));
11385796c8dcSSimon Schubert 	  wrap_here ("");
11395796c8dcSSimon Schubert 	  gdb_flush (gdb_stdout);
11405796c8dcSSimon Schubert 	}
11415796c8dcSSimon Schubert 
1142cf7f2e2dSJohn Marino       if (objfile->sf)
1143cf7f2e2dSJohn Marino 	objfile->sf->qf->expand_all_symtabs (objfile);
11445796c8dcSSimon Schubert     }
11455796c8dcSSimon Schubert 
1146c50c785cSJohn Marino   if (should_print && !objfile_has_symbols (objfile))
11475796c8dcSSimon Schubert     {
11485796c8dcSSimon Schubert       wrap_here ("");
11495796c8dcSSimon Schubert       printf_unfiltered (_("(no debugging symbols found)..."));
11505796c8dcSSimon Schubert       wrap_here ("");
11515796c8dcSSimon Schubert     }
11525796c8dcSSimon Schubert 
1153c50c785cSJohn Marino   if (should_print)
11545796c8dcSSimon Schubert     {
11555796c8dcSSimon Schubert       if (deprecated_post_add_symbol_hook)
11565796c8dcSSimon Schubert 	deprecated_post_add_symbol_hook ();
11575796c8dcSSimon Schubert       else
11585796c8dcSSimon Schubert 	printf_unfiltered (_("done.\n"));
11595796c8dcSSimon Schubert     }
11605796c8dcSSimon Schubert 
11615796c8dcSSimon Schubert   /* We print some messages regardless of whether 'from_tty ||
11625796c8dcSSimon Schubert      info_verbose' is true, so make sure they go out at the right
11635796c8dcSSimon Schubert      time.  */
11645796c8dcSSimon Schubert   gdb_flush (gdb_stdout);
11655796c8dcSSimon Schubert 
11665796c8dcSSimon Schubert   if (objfile->sf == NULL)
11675796c8dcSSimon Schubert     {
11685796c8dcSSimon Schubert       observer_notify_new_objfile (objfile);
11695796c8dcSSimon Schubert       return objfile;	/* No symbols.  */
11705796c8dcSSimon Schubert     }
11715796c8dcSSimon Schubert 
11725796c8dcSSimon Schubert   new_symfile_objfile (objfile, add_flags);
11735796c8dcSSimon Schubert 
11745796c8dcSSimon Schubert   observer_notify_new_objfile (objfile);
11755796c8dcSSimon Schubert 
11765796c8dcSSimon Schubert   bfd_cache_close_all ();
11775796c8dcSSimon Schubert   return (objfile);
11785796c8dcSSimon Schubert }
11795796c8dcSSimon Schubert 
1180cf7f2e2dSJohn Marino /* Add BFD as a separate debug file for OBJFILE.  */
1181cf7f2e2dSJohn Marino 
1182cf7f2e2dSJohn Marino void
1183cf7f2e2dSJohn Marino symbol_file_add_separate (bfd *bfd, int symfile_flags, struct objfile *objfile)
1184cf7f2e2dSJohn Marino {
1185cf7f2e2dSJohn Marino   struct objfile *new_objfile;
1186cf7f2e2dSJohn Marino   struct section_addr_info *sap;
1187cf7f2e2dSJohn Marino   struct cleanup *my_cleanup;
1188cf7f2e2dSJohn Marino 
1189cf7f2e2dSJohn Marino   /* Create section_addr_info.  We can't directly use offsets from OBJFILE
1190cf7f2e2dSJohn Marino      because sections of BFD may not match sections of OBJFILE and because
1191cf7f2e2dSJohn Marino      vma may have been modified by tools such as prelink.  */
1192cf7f2e2dSJohn Marino   sap = build_section_addr_info_from_objfile (objfile);
1193cf7f2e2dSJohn Marino   my_cleanup = make_cleanup_free_section_addr_info (sap);
1194cf7f2e2dSJohn Marino 
1195cf7f2e2dSJohn Marino   new_objfile = symbol_file_add_with_addrs_or_offsets
1196cf7f2e2dSJohn Marino     (bfd, symfile_flags,
1197cf7f2e2dSJohn Marino      sap, NULL, 0,
1198cf7f2e2dSJohn Marino      objfile->flags & (OBJF_REORDERED | OBJF_SHARED | OBJF_READNOW
1199*a45ae5f8SJohn Marino 		       | OBJF_USERLOADED),
1200*a45ae5f8SJohn Marino      objfile);
1201cf7f2e2dSJohn Marino 
1202cf7f2e2dSJohn Marino   do_cleanups (my_cleanup);
1203cf7f2e2dSJohn Marino }
12045796c8dcSSimon Schubert 
12055796c8dcSSimon Schubert /* Process the symbol file ABFD, as either the main file or as a
12065796c8dcSSimon Schubert    dynamically loaded file.
12075796c8dcSSimon Schubert 
12085796c8dcSSimon Schubert    See symbol_file_add_with_addrs_or_offsets's comments for
12095796c8dcSSimon Schubert    details.  */
12105796c8dcSSimon Schubert struct objfile *
12115796c8dcSSimon Schubert symbol_file_add_from_bfd (bfd *abfd, int add_flags,
12125796c8dcSSimon Schubert                           struct section_addr_info *addrs,
1213*a45ae5f8SJohn Marino                           int flags, struct objfile *parent)
12145796c8dcSSimon Schubert {
12155796c8dcSSimon Schubert   return symbol_file_add_with_addrs_or_offsets (abfd, add_flags, addrs, 0, 0,
1216*a45ae5f8SJohn Marino                                                 flags, parent);
12175796c8dcSSimon Schubert }
12185796c8dcSSimon Schubert 
12195796c8dcSSimon Schubert 
12205796c8dcSSimon Schubert /* Process a symbol file, as either the main file or as a dynamically
12215796c8dcSSimon Schubert    loaded file.  See symbol_file_add_with_addrs_or_offsets's comments
12225796c8dcSSimon Schubert    for details.  */
12235796c8dcSSimon Schubert struct objfile *
12245796c8dcSSimon Schubert symbol_file_add (char *name, int add_flags, struct section_addr_info *addrs,
12255796c8dcSSimon Schubert 		 int flags)
12265796c8dcSSimon Schubert {
12275796c8dcSSimon Schubert   return symbol_file_add_from_bfd (symfile_bfd_open (name), add_flags, addrs,
1228*a45ae5f8SJohn Marino                                    flags, NULL);
12295796c8dcSSimon Schubert }
12305796c8dcSSimon Schubert 
12315796c8dcSSimon Schubert 
12325796c8dcSSimon Schubert /* Call symbol_file_add() with default values and update whatever is
12335796c8dcSSimon Schubert    affected by the loading of a new main().
12345796c8dcSSimon Schubert    Used when the file is supplied in the gdb command line
12355796c8dcSSimon Schubert    and by some targets with special loading requirements.
12365796c8dcSSimon Schubert    The auxiliary function, symbol_file_add_main_1(), has the flags
12375796c8dcSSimon Schubert    argument for the switches that can only be specified in the symbol_file
12385796c8dcSSimon Schubert    command itself.  */
12395796c8dcSSimon Schubert 
12405796c8dcSSimon Schubert void
12415796c8dcSSimon Schubert symbol_file_add_main (char *args, int from_tty)
12425796c8dcSSimon Schubert {
12435796c8dcSSimon Schubert   symbol_file_add_main_1 (args, from_tty, 0);
12445796c8dcSSimon Schubert }
12455796c8dcSSimon Schubert 
12465796c8dcSSimon Schubert static void
12475796c8dcSSimon Schubert symbol_file_add_main_1 (char *args, int from_tty, int flags)
12485796c8dcSSimon Schubert {
12495796c8dcSSimon Schubert   const int add_flags = SYMFILE_MAINLINE | (from_tty ? SYMFILE_VERBOSE : 0);
12505796c8dcSSimon Schubert   symbol_file_add (args, add_flags, NULL, flags);
12515796c8dcSSimon Schubert 
12525796c8dcSSimon Schubert   /* Getting new symbols may change our opinion about
12535796c8dcSSimon Schubert      what is frameless.  */
12545796c8dcSSimon Schubert   reinit_frame_cache ();
12555796c8dcSSimon Schubert 
12565796c8dcSSimon Schubert   set_initial_language ();
12575796c8dcSSimon Schubert }
12585796c8dcSSimon Schubert 
12595796c8dcSSimon Schubert void
12605796c8dcSSimon Schubert symbol_file_clear (int from_tty)
12615796c8dcSSimon Schubert {
12625796c8dcSSimon Schubert   if ((have_full_symbols () || have_partial_symbols ())
12635796c8dcSSimon Schubert       && from_tty
12645796c8dcSSimon Schubert       && (symfile_objfile
12655796c8dcSSimon Schubert 	  ? !query (_("Discard symbol table from `%s'? "),
12665796c8dcSSimon Schubert 		    symfile_objfile->name)
12675796c8dcSSimon Schubert 	  : !query (_("Discard symbol table? "))))
12685796c8dcSSimon Schubert     error (_("Not confirmed."));
12695796c8dcSSimon Schubert 
1270cf7f2e2dSJohn Marino   /* solib descriptors may have handles to objfiles.  Wipe them before their
1271cf7f2e2dSJohn Marino      objfiles get stale by free_all_objfiles.  */
12725796c8dcSSimon Schubert   no_shared_libraries (NULL, from_tty);
12735796c8dcSSimon Schubert 
1274cf7f2e2dSJohn Marino   free_all_objfiles ();
1275cf7f2e2dSJohn Marino 
12765796c8dcSSimon Schubert   gdb_assert (symfile_objfile == NULL);
12775796c8dcSSimon Schubert   if (from_tty)
12785796c8dcSSimon Schubert     printf_unfiltered (_("No symbol file now.\n"));
12795796c8dcSSimon Schubert }
12805796c8dcSSimon Schubert 
12815796c8dcSSimon Schubert static char *
12825796c8dcSSimon Schubert get_debug_link_info (struct objfile *objfile, unsigned long *crc32_out)
12835796c8dcSSimon Schubert {
12845796c8dcSSimon Schubert   asection *sect;
12855796c8dcSSimon Schubert   bfd_size_type debuglink_size;
12865796c8dcSSimon Schubert   unsigned long crc32;
12875796c8dcSSimon Schubert   char *contents;
12885796c8dcSSimon Schubert   int crc_offset;
12895796c8dcSSimon Schubert 
12905796c8dcSSimon Schubert   sect = bfd_get_section_by_name (objfile->obfd, ".gnu_debuglink");
12915796c8dcSSimon Schubert 
12925796c8dcSSimon Schubert   if (sect == NULL)
12935796c8dcSSimon Schubert     return NULL;
12945796c8dcSSimon Schubert 
12955796c8dcSSimon Schubert   debuglink_size = bfd_section_size (objfile->obfd, sect);
12965796c8dcSSimon Schubert 
12975796c8dcSSimon Schubert   contents = xmalloc (debuglink_size);
12985796c8dcSSimon Schubert   bfd_get_section_contents (objfile->obfd, sect, contents,
12995796c8dcSSimon Schubert 			    (file_ptr)0, (bfd_size_type)debuglink_size);
13005796c8dcSSimon Schubert 
13015796c8dcSSimon Schubert   /* Crc value is stored after the filename, aligned up to 4 bytes.  */
13025796c8dcSSimon Schubert   crc_offset = strlen (contents) + 1;
13035796c8dcSSimon Schubert   crc_offset = (crc_offset + 3) & ~3;
13045796c8dcSSimon Schubert 
13055796c8dcSSimon Schubert   crc32 = bfd_get_32 (objfile->obfd, (bfd_byte *) (contents + crc_offset));
13065796c8dcSSimon Schubert 
13075796c8dcSSimon Schubert   *crc32_out = crc32;
13085796c8dcSSimon Schubert   return contents;
13095796c8dcSSimon Schubert }
13105796c8dcSSimon Schubert 
1311*a45ae5f8SJohn Marino /* Return 32-bit CRC for ABFD.  If successful store it to *FILE_CRC_RETURN and
1312*a45ae5f8SJohn Marino    return 1.  Otherwise print a warning and return 0.  ABFD seek position is
1313*a45ae5f8SJohn Marino    not preserved.  */
1314*a45ae5f8SJohn Marino 
1315*a45ae5f8SJohn Marino static int
1316*a45ae5f8SJohn Marino get_file_crc (bfd *abfd, unsigned long *file_crc_return)
1317*a45ae5f8SJohn Marino {
1318*a45ae5f8SJohn Marino   unsigned long file_crc = 0;
1319*a45ae5f8SJohn Marino 
1320*a45ae5f8SJohn Marino   if (bfd_seek (abfd, 0, SEEK_SET) != 0)
1321*a45ae5f8SJohn Marino     {
1322*a45ae5f8SJohn Marino       warning (_("Problem reading \"%s\" for CRC: %s"),
1323*a45ae5f8SJohn Marino 	       bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
1324*a45ae5f8SJohn Marino       return 0;
1325*a45ae5f8SJohn Marino     }
1326*a45ae5f8SJohn Marino 
1327*a45ae5f8SJohn Marino   for (;;)
1328*a45ae5f8SJohn Marino     {
1329*a45ae5f8SJohn Marino       gdb_byte buffer[8 * 1024];
1330*a45ae5f8SJohn Marino       bfd_size_type count;
1331*a45ae5f8SJohn Marino 
1332*a45ae5f8SJohn Marino       count = bfd_bread (buffer, sizeof (buffer), abfd);
1333*a45ae5f8SJohn Marino       if (count == (bfd_size_type) -1)
1334*a45ae5f8SJohn Marino 	{
1335*a45ae5f8SJohn Marino 	  warning (_("Problem reading \"%s\" for CRC: %s"),
1336*a45ae5f8SJohn Marino 		   bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
1337*a45ae5f8SJohn Marino 	  return 0;
1338*a45ae5f8SJohn Marino 	}
1339*a45ae5f8SJohn Marino       if (count == 0)
1340*a45ae5f8SJohn Marino 	break;
1341*a45ae5f8SJohn Marino       file_crc = gnu_debuglink_crc32 (file_crc, buffer, count);
1342*a45ae5f8SJohn Marino     }
1343*a45ae5f8SJohn Marino 
1344*a45ae5f8SJohn Marino   *file_crc_return = file_crc;
1345*a45ae5f8SJohn Marino   return 1;
1346*a45ae5f8SJohn Marino }
1347*a45ae5f8SJohn Marino 
13485796c8dcSSimon Schubert static int
1349cf7f2e2dSJohn Marino separate_debug_file_exists (const char *name, unsigned long crc,
1350cf7f2e2dSJohn Marino 			    struct objfile *parent_objfile)
13515796c8dcSSimon Schubert {
1352*a45ae5f8SJohn Marino   unsigned long file_crc;
1353*a45ae5f8SJohn Marino   int file_crc_p;
13545796c8dcSSimon Schubert   bfd *abfd;
1355cf7f2e2dSJohn Marino   struct stat parent_stat, abfd_stat;
1356*a45ae5f8SJohn Marino   int verified_as_different;
13575796c8dcSSimon Schubert 
1358cf7f2e2dSJohn Marino   /* Find a separate debug info file as if symbols would be present in
1359cf7f2e2dSJohn Marino      PARENT_OBJFILE itself this function would not be called.  .gnu_debuglink
1360cf7f2e2dSJohn Marino      section can contain just the basename of PARENT_OBJFILE without any
1361cf7f2e2dSJohn Marino      ".debug" suffix as "/usr/lib/debug/path/to/file" is a separate tree where
1362cf7f2e2dSJohn Marino      the separate debug infos with the same basename can exist.  */
1363cf7f2e2dSJohn Marino 
1364c50c785cSJohn Marino   if (filename_cmp (name, parent_objfile->name) == 0)
1365cf7f2e2dSJohn Marino     return 0;
1366cf7f2e2dSJohn Marino 
1367cf7f2e2dSJohn Marino   abfd = bfd_open_maybe_remote (name);
13685796c8dcSSimon Schubert 
13695796c8dcSSimon Schubert   if (!abfd)
13705796c8dcSSimon Schubert     return 0;
13715796c8dcSSimon Schubert 
1372c50c785cSJohn Marino   /* Verify symlinks were not the cause of filename_cmp name difference above.
1373cf7f2e2dSJohn Marino 
1374cf7f2e2dSJohn Marino      Some operating systems, e.g. Windows, do not provide a meaningful
1375cf7f2e2dSJohn Marino      st_ino; they always set it to zero.  (Windows does provide a
1376cf7f2e2dSJohn Marino      meaningful st_dev.)  Do not indicate a duplicate library in that
1377cf7f2e2dSJohn Marino      case.  While there is no guarantee that a system that provides
1378cf7f2e2dSJohn Marino      meaningful inode numbers will never set st_ino to zero, this is
1379cf7f2e2dSJohn Marino      merely an optimization, so we do not need to worry about false
1380cf7f2e2dSJohn Marino      negatives.  */
1381cf7f2e2dSJohn Marino 
1382cf7f2e2dSJohn Marino   if (bfd_stat (abfd, &abfd_stat) == 0
1383*a45ae5f8SJohn Marino       && abfd_stat.st_ino != 0
1384*a45ae5f8SJohn Marino       && bfd_stat (parent_objfile->obfd, &parent_stat) == 0)
1385*a45ae5f8SJohn Marino     {
1386*a45ae5f8SJohn Marino       if (abfd_stat.st_dev == parent_stat.st_dev
1387*a45ae5f8SJohn Marino 	  && abfd_stat.st_ino == parent_stat.st_ino)
1388cf7f2e2dSJohn Marino 	{
1389cf7f2e2dSJohn Marino 	  bfd_close (abfd);
1390cf7f2e2dSJohn Marino 	  return 0;
1391cf7f2e2dSJohn Marino 	}
1392*a45ae5f8SJohn Marino       verified_as_different = 1;
1393*a45ae5f8SJohn Marino     }
1394*a45ae5f8SJohn Marino   else
1395*a45ae5f8SJohn Marino     verified_as_different = 0;
1396cf7f2e2dSJohn Marino 
1397*a45ae5f8SJohn Marino   file_crc_p = get_file_crc (abfd, &file_crc);
13985796c8dcSSimon Schubert 
13995796c8dcSSimon Schubert   bfd_close (abfd);
14005796c8dcSSimon Schubert 
1401*a45ae5f8SJohn Marino   if (!file_crc_p)
1402*a45ae5f8SJohn Marino     return 0;
1403*a45ae5f8SJohn Marino 
1404cf7f2e2dSJohn Marino   if (crc != file_crc)
1405cf7f2e2dSJohn Marino     {
1406*a45ae5f8SJohn Marino       /* If one (or both) the files are accessed for example the via "remote:"
1407*a45ae5f8SJohn Marino 	 gdbserver way it does not support the bfd_stat operation.  Verify
1408*a45ae5f8SJohn Marino 	 whether those two files are not the same manually.  */
1409*a45ae5f8SJohn Marino 
1410*a45ae5f8SJohn Marino       if (!verified_as_different && !parent_objfile->crc32_p)
1411*a45ae5f8SJohn Marino 	{
1412*a45ae5f8SJohn Marino 	  parent_objfile->crc32_p = get_file_crc (parent_objfile->obfd,
1413*a45ae5f8SJohn Marino 						  &parent_objfile->crc32);
1414*a45ae5f8SJohn Marino 	  if (!parent_objfile->crc32_p)
1415*a45ae5f8SJohn Marino 	    return 0;
1416*a45ae5f8SJohn Marino 	}
1417*a45ae5f8SJohn Marino 
1418*a45ae5f8SJohn Marino       if (verified_as_different || parent_objfile->crc32 != file_crc)
1419cf7f2e2dSJohn Marino 	warning (_("the debug information found in \"%s\""
1420cf7f2e2dSJohn Marino 		   " does not match \"%s\" (CRC mismatch).\n"),
1421cf7f2e2dSJohn Marino 		 name, parent_objfile->name);
1422*a45ae5f8SJohn Marino 
1423cf7f2e2dSJohn Marino       return 0;
1424cf7f2e2dSJohn Marino     }
1425cf7f2e2dSJohn Marino 
1426cf7f2e2dSJohn Marino   return 1;
14275796c8dcSSimon Schubert }
14285796c8dcSSimon Schubert 
14295796c8dcSSimon Schubert char *debug_file_directory = NULL;
14305796c8dcSSimon Schubert static void
14315796c8dcSSimon Schubert show_debug_file_directory (struct ui_file *file, int from_tty,
14325796c8dcSSimon Schubert 			   struct cmd_list_element *c, const char *value)
14335796c8dcSSimon Schubert {
1434c50c785cSJohn Marino   fprintf_filtered (file,
1435c50c785cSJohn Marino 		    _("The directory where separate debug "
1436c50c785cSJohn Marino 		      "symbols are searched for is \"%s\".\n"),
14375796c8dcSSimon Schubert 		    value);
14385796c8dcSSimon Schubert }
14395796c8dcSSimon Schubert 
14405796c8dcSSimon Schubert #if ! defined (DEBUG_SUBDIRECTORY)
14415796c8dcSSimon Schubert #define DEBUG_SUBDIRECTORY ".debug"
14425796c8dcSSimon Schubert #endif
14435796c8dcSSimon Schubert 
1444cf7f2e2dSJohn Marino char *
1445cf7f2e2dSJohn Marino find_separate_debug_file_by_debuglink (struct objfile *objfile)
14465796c8dcSSimon Schubert {
1447cf7f2e2dSJohn Marino   char *basename, *debugdir;
1448cf7f2e2dSJohn Marino   char *dir = NULL;
1449cf7f2e2dSJohn Marino   char *debugfile = NULL;
1450cf7f2e2dSJohn Marino   char *canon_name = NULL;
14515796c8dcSSimon Schubert   unsigned long crc32;
14525796c8dcSSimon Schubert   int i;
14535796c8dcSSimon Schubert 
14545796c8dcSSimon Schubert   basename = get_debug_link_info (objfile, &crc32);
14555796c8dcSSimon Schubert 
14565796c8dcSSimon Schubert   if (basename == NULL)
1457cf7f2e2dSJohn Marino     /* There's no separate debug info, hence there's no way we could
1458cf7f2e2dSJohn Marino        load it => no warning.  */
1459cf7f2e2dSJohn Marino     goto cleanup_return_debugfile;
14605796c8dcSSimon Schubert 
14615796c8dcSSimon Schubert   dir = xstrdup (objfile->name);
14625796c8dcSSimon Schubert 
14635796c8dcSSimon Schubert   /* Strip off the final filename part, leaving the directory name,
1464cf7f2e2dSJohn Marino      followed by a slash.  The directory can be relative or absolute.  */
14655796c8dcSSimon Schubert   for (i = strlen(dir) - 1; i >= 0; i--)
14665796c8dcSSimon Schubert     {
14675796c8dcSSimon Schubert       if (IS_DIR_SEPARATOR (dir[i]))
14685796c8dcSSimon Schubert 	break;
14695796c8dcSSimon Schubert     }
1470cf7f2e2dSJohn Marino   /* If I is -1 then no directory is present there and DIR will be "".  */
14715796c8dcSSimon Schubert   dir[i+1] = '\0';
14725796c8dcSSimon Schubert 
14735796c8dcSSimon Schubert   /* Set I to max (strlen (canon_name), strlen (dir)).  */
14745796c8dcSSimon Schubert   canon_name = lrealpath (dir);
14755796c8dcSSimon Schubert   i = strlen (dir);
14765796c8dcSSimon Schubert   if (canon_name && strlen (canon_name) > i)
14775796c8dcSSimon Schubert     i = strlen (canon_name);
14785796c8dcSSimon Schubert 
1479cf7f2e2dSJohn Marino   debugfile = xmalloc (strlen (debug_file_directory) + 1
14805796c8dcSSimon Schubert 		       + i
14815796c8dcSSimon Schubert 		       + strlen (DEBUG_SUBDIRECTORY)
14825796c8dcSSimon Schubert 		       + strlen ("/")
14835796c8dcSSimon Schubert 		       + strlen (basename)
14845796c8dcSSimon Schubert 		       + 1);
14855796c8dcSSimon Schubert 
14865796c8dcSSimon Schubert   /* First try in the same directory as the original file.  */
14875796c8dcSSimon Schubert   strcpy (debugfile, dir);
14885796c8dcSSimon Schubert   strcat (debugfile, basename);
14895796c8dcSSimon Schubert 
1490cf7f2e2dSJohn Marino   if (separate_debug_file_exists (debugfile, crc32, objfile))
1491cf7f2e2dSJohn Marino     goto cleanup_return_debugfile;
14925796c8dcSSimon Schubert 
14935796c8dcSSimon Schubert   /* Then try in the subdirectory named DEBUG_SUBDIRECTORY.  */
14945796c8dcSSimon Schubert   strcpy (debugfile, dir);
14955796c8dcSSimon Schubert   strcat (debugfile, DEBUG_SUBDIRECTORY);
14965796c8dcSSimon Schubert   strcat (debugfile, "/");
14975796c8dcSSimon Schubert   strcat (debugfile, basename);
14985796c8dcSSimon Schubert 
1499cf7f2e2dSJohn Marino   if (separate_debug_file_exists (debugfile, crc32, objfile))
1500cf7f2e2dSJohn Marino     goto cleanup_return_debugfile;
15015796c8dcSSimon Schubert 
1502cf7f2e2dSJohn Marino   /* Then try in the global debugfile directories.
1503cf7f2e2dSJohn Marino 
1504cf7f2e2dSJohn Marino      Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will
1505cf7f2e2dSJohn Marino      cause "/..." lookups.  */
1506cf7f2e2dSJohn Marino 
1507cf7f2e2dSJohn Marino   debugdir = debug_file_directory;
1508cf7f2e2dSJohn Marino   do
1509cf7f2e2dSJohn Marino     {
1510cf7f2e2dSJohn Marino       char *debugdir_end;
1511cf7f2e2dSJohn Marino 
1512cf7f2e2dSJohn Marino       while (*debugdir == DIRNAME_SEPARATOR)
1513cf7f2e2dSJohn Marino 	debugdir++;
1514cf7f2e2dSJohn Marino 
1515cf7f2e2dSJohn Marino       debugdir_end = strchr (debugdir, DIRNAME_SEPARATOR);
1516cf7f2e2dSJohn Marino       if (debugdir_end == NULL)
1517cf7f2e2dSJohn Marino 	debugdir_end = &debugdir[strlen (debugdir)];
1518cf7f2e2dSJohn Marino 
1519cf7f2e2dSJohn Marino       memcpy (debugfile, debugdir, debugdir_end - debugdir);
1520cf7f2e2dSJohn Marino       debugfile[debugdir_end - debugdir] = 0;
15215796c8dcSSimon Schubert       strcat (debugfile, "/");
15225796c8dcSSimon Schubert       strcat (debugfile, dir);
15235796c8dcSSimon Schubert       strcat (debugfile, basename);
15245796c8dcSSimon Schubert 
1525cf7f2e2dSJohn Marino       if (separate_debug_file_exists (debugfile, crc32, objfile))
1526cf7f2e2dSJohn Marino 	goto cleanup_return_debugfile;
15275796c8dcSSimon Schubert 
15285796c8dcSSimon Schubert       /* If the file is in the sysroot, try using its base path in the
15295796c8dcSSimon Schubert 	 global debugfile directory.  */
15305796c8dcSSimon Schubert       if (canon_name
1531c50c785cSJohn Marino 	  && filename_ncmp (canon_name, gdb_sysroot,
1532c50c785cSJohn Marino 			    strlen (gdb_sysroot)) == 0
15335796c8dcSSimon Schubert 	  && IS_DIR_SEPARATOR (canon_name[strlen (gdb_sysroot)]))
15345796c8dcSSimon Schubert 	{
1535cf7f2e2dSJohn Marino 	  memcpy (debugfile, debugdir, debugdir_end - debugdir);
1536cf7f2e2dSJohn Marino 	  debugfile[debugdir_end - debugdir] = 0;
15375796c8dcSSimon Schubert 	  strcat (debugfile, canon_name + strlen (gdb_sysroot));
15385796c8dcSSimon Schubert 	  strcat (debugfile, "/");
15395796c8dcSSimon Schubert 	  strcat (debugfile, basename);
15405796c8dcSSimon Schubert 
1541cf7f2e2dSJohn Marino 	  if (separate_debug_file_exists (debugfile, crc32, objfile))
1542cf7f2e2dSJohn Marino 	    goto cleanup_return_debugfile;
1543cf7f2e2dSJohn Marino 	}
1544cf7f2e2dSJohn Marino 
1545cf7f2e2dSJohn Marino       debugdir = debugdir_end;
1546cf7f2e2dSJohn Marino     }
1547cf7f2e2dSJohn Marino   while (*debugdir != 0);
1548cf7f2e2dSJohn Marino 
1549cf7f2e2dSJohn Marino   xfree (debugfile);
1550cf7f2e2dSJohn Marino   debugfile = NULL;
1551cf7f2e2dSJohn Marino 
1552cf7f2e2dSJohn Marino cleanup_return_debugfile:
15535796c8dcSSimon Schubert   xfree (canon_name);
15545796c8dcSSimon Schubert   xfree (basename);
15555796c8dcSSimon Schubert   xfree (dir);
1556cf7f2e2dSJohn Marino   return debugfile;
15575796c8dcSSimon Schubert }
15585796c8dcSSimon Schubert 
15595796c8dcSSimon Schubert 
15605796c8dcSSimon Schubert /* This is the symbol-file command.  Read the file, analyze its
15615796c8dcSSimon Schubert    symbols, and add a struct symtab to a symtab list.  The syntax of
15625796c8dcSSimon Schubert    the command is rather bizarre:
15635796c8dcSSimon Schubert 
15645796c8dcSSimon Schubert    1. The function buildargv implements various quoting conventions
15655796c8dcSSimon Schubert    which are undocumented and have little or nothing in common with
15665796c8dcSSimon Schubert    the way things are quoted (or not quoted) elsewhere in GDB.
15675796c8dcSSimon Schubert 
15685796c8dcSSimon Schubert    2. Options are used, which are not generally used in GDB (perhaps
15695796c8dcSSimon Schubert    "set mapped on", "set readnow on" would be better)
15705796c8dcSSimon Schubert 
15715796c8dcSSimon Schubert    3. The order of options matters, which is contrary to GNU
15725796c8dcSSimon Schubert    conventions (because it is confusing and inconvenient).  */
15735796c8dcSSimon Schubert 
15745796c8dcSSimon Schubert void
15755796c8dcSSimon Schubert symbol_file_command (char *args, int from_tty)
15765796c8dcSSimon Schubert {
15775796c8dcSSimon Schubert   dont_repeat ();
15785796c8dcSSimon Schubert 
15795796c8dcSSimon Schubert   if (args == NULL)
15805796c8dcSSimon Schubert     {
15815796c8dcSSimon Schubert       symbol_file_clear (from_tty);
15825796c8dcSSimon Schubert     }
15835796c8dcSSimon Schubert   else
15845796c8dcSSimon Schubert     {
15855796c8dcSSimon Schubert       char **argv = gdb_buildargv (args);
15865796c8dcSSimon Schubert       int flags = OBJF_USERLOADED;
15875796c8dcSSimon Schubert       struct cleanup *cleanups;
15885796c8dcSSimon Schubert       char *name = NULL;
15895796c8dcSSimon Schubert 
15905796c8dcSSimon Schubert       cleanups = make_cleanup_freeargv (argv);
15915796c8dcSSimon Schubert       while (*argv != NULL)
15925796c8dcSSimon Schubert 	{
15935796c8dcSSimon Schubert 	  if (strcmp (*argv, "-readnow") == 0)
15945796c8dcSSimon Schubert 	    flags |= OBJF_READNOW;
15955796c8dcSSimon Schubert 	  else if (**argv == '-')
15965796c8dcSSimon Schubert 	    error (_("unknown option `%s'"), *argv);
15975796c8dcSSimon Schubert 	  else
15985796c8dcSSimon Schubert 	    {
15995796c8dcSSimon Schubert 	      symbol_file_add_main_1 (*argv, from_tty, flags);
16005796c8dcSSimon Schubert 	      name = *argv;
16015796c8dcSSimon Schubert 	    }
16025796c8dcSSimon Schubert 
16035796c8dcSSimon Schubert 	  argv++;
16045796c8dcSSimon Schubert 	}
16055796c8dcSSimon Schubert 
16065796c8dcSSimon Schubert       if (name == NULL)
16075796c8dcSSimon Schubert 	error (_("no symbol file name was specified"));
16085796c8dcSSimon Schubert 
16095796c8dcSSimon Schubert       do_cleanups (cleanups);
16105796c8dcSSimon Schubert     }
16115796c8dcSSimon Schubert }
16125796c8dcSSimon Schubert 
16135796c8dcSSimon Schubert /* Set the initial language.
16145796c8dcSSimon Schubert 
16155796c8dcSSimon Schubert    FIXME: A better solution would be to record the language in the
16165796c8dcSSimon Schubert    psymtab when reading partial symbols, and then use it (if known) to
16175796c8dcSSimon Schubert    set the language.  This would be a win for formats that encode the
16185796c8dcSSimon Schubert    language in an easily discoverable place, such as DWARF.  For
16195796c8dcSSimon Schubert    stabs, we can jump through hoops looking for specially named
16205796c8dcSSimon Schubert    symbols or try to intuit the language from the specific type of
16215796c8dcSSimon Schubert    stabs we find, but we can't do that until later when we read in
16225796c8dcSSimon Schubert    full symbols.  */
16235796c8dcSSimon Schubert 
16245796c8dcSSimon Schubert void
16255796c8dcSSimon Schubert set_initial_language (void)
16265796c8dcSSimon Schubert {
16275796c8dcSSimon Schubert   enum language lang = language_unknown;
16285796c8dcSSimon Schubert 
1629c50c785cSJohn Marino   if (language_of_main != language_unknown)
1630c50c785cSJohn Marino     lang = language_of_main;
1631c50c785cSJohn Marino   else
1632c50c785cSJohn Marino     {
1633c50c785cSJohn Marino       const char *filename;
1634c50c785cSJohn Marino 
1635cf7f2e2dSJohn Marino       filename = find_main_filename ();
1636cf7f2e2dSJohn Marino       if (filename != NULL)
1637cf7f2e2dSJohn Marino 	lang = deduce_language_from_filename (filename);
1638c50c785cSJohn Marino     }
16395796c8dcSSimon Schubert 
16405796c8dcSSimon Schubert   if (lang == language_unknown)
16415796c8dcSSimon Schubert     {
16425796c8dcSSimon Schubert       /* Make C the default language */
16435796c8dcSSimon Schubert       lang = language_c;
16445796c8dcSSimon Schubert     }
16455796c8dcSSimon Schubert 
16465796c8dcSSimon Schubert   set_language (lang);
16475796c8dcSSimon Schubert   expected_language = current_language; /* Don't warn the user.  */
16485796c8dcSSimon Schubert }
1649cf7f2e2dSJohn Marino 
1650cf7f2e2dSJohn Marino /* If NAME is a remote name open the file using remote protocol, otherwise
1651cf7f2e2dSJohn Marino    open it normally.  */
1652cf7f2e2dSJohn Marino 
1653cf7f2e2dSJohn Marino bfd *
1654cf7f2e2dSJohn Marino bfd_open_maybe_remote (const char *name)
1655cf7f2e2dSJohn Marino {
1656cf7f2e2dSJohn Marino   if (remote_filename_p (name))
1657cf7f2e2dSJohn Marino     return remote_bfd_open (name, gnutarget);
1658cf7f2e2dSJohn Marino   else
1659cf7f2e2dSJohn Marino     return bfd_openr (name, gnutarget);
16605796c8dcSSimon Schubert }
16615796c8dcSSimon Schubert 
1662cf7f2e2dSJohn Marino 
16635796c8dcSSimon Schubert /* Open the file specified by NAME and hand it off to BFD for
16645796c8dcSSimon Schubert    preliminary analysis.  Return a newly initialized bfd *, which
16655796c8dcSSimon Schubert    includes a newly malloc'd` copy of NAME (tilde-expanded and made
16665796c8dcSSimon Schubert    absolute).  In case of trouble, error() is called.  */
16675796c8dcSSimon Schubert 
16685796c8dcSSimon Schubert bfd *
16695796c8dcSSimon Schubert symfile_bfd_open (char *name)
16705796c8dcSSimon Schubert {
16715796c8dcSSimon Schubert   bfd *sym_bfd;
16725796c8dcSSimon Schubert   int desc;
16735796c8dcSSimon Schubert   char *absolute_name;
16745796c8dcSSimon Schubert 
16755796c8dcSSimon Schubert   if (remote_filename_p (name))
16765796c8dcSSimon Schubert     {
16775796c8dcSSimon Schubert       name = xstrdup (name);
16785796c8dcSSimon Schubert       sym_bfd = remote_bfd_open (name, gnutarget);
16795796c8dcSSimon Schubert       if (!sym_bfd)
16805796c8dcSSimon Schubert 	{
16815796c8dcSSimon Schubert 	  make_cleanup (xfree, name);
16825796c8dcSSimon Schubert 	  error (_("`%s': can't open to read symbols: %s."), name,
16835796c8dcSSimon Schubert 		 bfd_errmsg (bfd_get_error ()));
16845796c8dcSSimon Schubert 	}
16855796c8dcSSimon Schubert 
16865796c8dcSSimon Schubert       if (!bfd_check_format (sym_bfd, bfd_object))
16875796c8dcSSimon Schubert 	{
16885796c8dcSSimon Schubert 	  bfd_close (sym_bfd);
16895796c8dcSSimon Schubert 	  make_cleanup (xfree, name);
16905796c8dcSSimon Schubert 	  error (_("`%s': can't read symbols: %s."), name,
16915796c8dcSSimon Schubert 		 bfd_errmsg (bfd_get_error ()));
16925796c8dcSSimon Schubert 	}
16935796c8dcSSimon Schubert 
16945796c8dcSSimon Schubert       return sym_bfd;
16955796c8dcSSimon Schubert     }
16965796c8dcSSimon Schubert 
16975796c8dcSSimon Schubert   name = tilde_expand (name);	/* Returns 1st new malloc'd copy.  */
16985796c8dcSSimon Schubert 
16995796c8dcSSimon Schubert   /* Look down path for it, allocate 2nd new malloc'd copy.  */
17005796c8dcSSimon Schubert   desc = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST, name,
17015796c8dcSSimon Schubert 		O_RDONLY | O_BINARY, &absolute_name);
17025796c8dcSSimon Schubert #if defined(__GO32__) || defined(_WIN32) || defined (__CYGWIN__)
17035796c8dcSSimon Schubert   if (desc < 0)
17045796c8dcSSimon Schubert     {
17055796c8dcSSimon Schubert       char *exename = alloca (strlen (name) + 5);
1706cf7f2e2dSJohn Marino 
17075796c8dcSSimon Schubert       strcat (strcpy (exename, name), ".exe");
17085796c8dcSSimon Schubert       desc = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST, exename,
17095796c8dcSSimon Schubert 		    O_RDONLY | O_BINARY, &absolute_name);
17105796c8dcSSimon Schubert     }
17115796c8dcSSimon Schubert #endif
17125796c8dcSSimon Schubert   if (desc < 0)
17135796c8dcSSimon Schubert     {
17145796c8dcSSimon Schubert       make_cleanup (xfree, name);
17155796c8dcSSimon Schubert       perror_with_name (name);
17165796c8dcSSimon Schubert     }
17175796c8dcSSimon Schubert 
17185796c8dcSSimon Schubert   /* Free 1st new malloc'd copy, but keep the 2nd malloc'd copy in
17195796c8dcSSimon Schubert      bfd.  It'll be freed in free_objfile().  */
17205796c8dcSSimon Schubert   xfree (name);
17215796c8dcSSimon Schubert   name = absolute_name;
17225796c8dcSSimon Schubert 
17235796c8dcSSimon Schubert   sym_bfd = bfd_fopen (name, gnutarget, FOPEN_RB, desc);
17245796c8dcSSimon Schubert   if (!sym_bfd)
17255796c8dcSSimon Schubert     {
17265796c8dcSSimon Schubert       close (desc);
17275796c8dcSSimon Schubert       make_cleanup (xfree, name);
17285796c8dcSSimon Schubert       error (_("`%s': can't open to read symbols: %s."), name,
17295796c8dcSSimon Schubert 	     bfd_errmsg (bfd_get_error ()));
17305796c8dcSSimon Schubert     }
17315796c8dcSSimon Schubert   bfd_set_cacheable (sym_bfd, 1);
17325796c8dcSSimon Schubert 
17335796c8dcSSimon Schubert   if (!bfd_check_format (sym_bfd, bfd_object))
17345796c8dcSSimon Schubert     {
17355796c8dcSSimon Schubert       /* FIXME: should be checking for errors from bfd_close (for one
17365796c8dcSSimon Schubert          thing, on error it does not free all the storage associated
17375796c8dcSSimon Schubert          with the bfd).  */
17385796c8dcSSimon Schubert       bfd_close (sym_bfd);	/* This also closes desc.  */
17395796c8dcSSimon Schubert       make_cleanup (xfree, name);
17405796c8dcSSimon Schubert       error (_("`%s': can't read symbols: %s."), name,
17415796c8dcSSimon Schubert 	     bfd_errmsg (bfd_get_error ()));
17425796c8dcSSimon Schubert     }
17435796c8dcSSimon Schubert 
17445796c8dcSSimon Schubert   /* bfd_usrdata exists for applications and libbfd must not touch it.  */
17455796c8dcSSimon Schubert   gdb_assert (bfd_usrdata (sym_bfd) == NULL);
17465796c8dcSSimon Schubert 
17475796c8dcSSimon Schubert   return sym_bfd;
17485796c8dcSSimon Schubert }
17495796c8dcSSimon Schubert 
17505796c8dcSSimon Schubert /* Return the section index for SECTION_NAME on OBJFILE.  Return -1 if
17515796c8dcSSimon Schubert    the section was not found.  */
17525796c8dcSSimon Schubert 
17535796c8dcSSimon Schubert int
17545796c8dcSSimon Schubert get_section_index (struct objfile *objfile, char *section_name)
17555796c8dcSSimon Schubert {
17565796c8dcSSimon Schubert   asection *sect = bfd_get_section_by_name (objfile->obfd, section_name);
17575796c8dcSSimon Schubert 
17585796c8dcSSimon Schubert   if (sect)
17595796c8dcSSimon Schubert     return sect->index;
17605796c8dcSSimon Schubert   else
17615796c8dcSSimon Schubert     return -1;
17625796c8dcSSimon Schubert }
17635796c8dcSSimon Schubert 
17645796c8dcSSimon Schubert /* Link SF into the global symtab_fns list.  Called on startup by the
17655796c8dcSSimon Schubert    _initialize routine in each object file format reader, to register
1766c50c785cSJohn Marino    information about each format the reader is prepared to handle.  */
17675796c8dcSSimon Schubert 
17685796c8dcSSimon Schubert void
1769c50c785cSJohn Marino add_symtab_fns (const struct sym_fns *sf)
17705796c8dcSSimon Schubert {
1771c50c785cSJohn Marino   VEC_safe_push (sym_fns_ptr, symtab_fns, sf);
17725796c8dcSSimon Schubert }
17735796c8dcSSimon Schubert 
17745796c8dcSSimon Schubert /* Initialize OBJFILE to read symbols from its associated BFD.  It
17755796c8dcSSimon Schubert    either returns or calls error().  The result is an initialized
17765796c8dcSSimon Schubert    struct sym_fns in the objfile structure, that contains cached
17775796c8dcSSimon Schubert    information about the symbol file.  */
17785796c8dcSSimon Schubert 
1779c50c785cSJohn Marino static const struct sym_fns *
17805796c8dcSSimon Schubert find_sym_fns (bfd *abfd)
17815796c8dcSSimon Schubert {
1782c50c785cSJohn Marino   const struct sym_fns *sf;
17835796c8dcSSimon Schubert   enum bfd_flavour our_flavour = bfd_get_flavour (abfd);
1784c50c785cSJohn Marino   int i;
17855796c8dcSSimon Schubert 
17865796c8dcSSimon Schubert   if (our_flavour == bfd_target_srec_flavour
17875796c8dcSSimon Schubert       || our_flavour == bfd_target_ihex_flavour
17885796c8dcSSimon Schubert       || our_flavour == bfd_target_tekhex_flavour)
17895796c8dcSSimon Schubert     return NULL;	/* No symbols.  */
17905796c8dcSSimon Schubert 
1791c50c785cSJohn Marino   for (i = 0; VEC_iterate (sym_fns_ptr, symtab_fns, i, sf); ++i)
17925796c8dcSSimon Schubert     if (our_flavour == sf->sym_flavour)
17935796c8dcSSimon Schubert       return sf;
17945796c8dcSSimon Schubert 
17955796c8dcSSimon Schubert   error (_("I'm sorry, Dave, I can't do that.  Symbol format `%s' unknown."),
17965796c8dcSSimon Schubert 	 bfd_get_target (abfd));
17975796c8dcSSimon Schubert }
17985796c8dcSSimon Schubert 
17995796c8dcSSimon Schubert 
18005796c8dcSSimon Schubert /* This function runs the load command of our current target.  */
18015796c8dcSSimon Schubert 
18025796c8dcSSimon Schubert static void
18035796c8dcSSimon Schubert load_command (char *arg, int from_tty)
18045796c8dcSSimon Schubert {
1805c50c785cSJohn Marino   dont_repeat ();
1806c50c785cSJohn Marino 
18075796c8dcSSimon Schubert   /* The user might be reloading because the binary has changed.  Take
18085796c8dcSSimon Schubert      this opportunity to check.  */
18095796c8dcSSimon Schubert   reopen_exec_file ();
18105796c8dcSSimon Schubert   reread_symbols ();
18115796c8dcSSimon Schubert 
18125796c8dcSSimon Schubert   if (arg == NULL)
18135796c8dcSSimon Schubert     {
18145796c8dcSSimon Schubert       char *parg;
18155796c8dcSSimon Schubert       int count = 0;
18165796c8dcSSimon Schubert 
18175796c8dcSSimon Schubert       parg = arg = get_exec_file (1);
18185796c8dcSSimon Schubert 
18195796c8dcSSimon Schubert       /* Count how many \ " ' tab space there are in the name.  */
18205796c8dcSSimon Schubert       while ((parg = strpbrk (parg, "\\\"'\t ")))
18215796c8dcSSimon Schubert 	{
18225796c8dcSSimon Schubert 	  parg++;
18235796c8dcSSimon Schubert 	  count++;
18245796c8dcSSimon Schubert 	}
18255796c8dcSSimon Schubert 
18265796c8dcSSimon Schubert       if (count)
18275796c8dcSSimon Schubert 	{
18285796c8dcSSimon Schubert 	  /* We need to quote this string so buildargv can pull it apart.  */
18295796c8dcSSimon Schubert 	  char *temp = xmalloc (strlen (arg) + count + 1 );
18305796c8dcSSimon Schubert 	  char *ptemp = temp;
18315796c8dcSSimon Schubert 	  char *prev;
18325796c8dcSSimon Schubert 
18335796c8dcSSimon Schubert 	  make_cleanup (xfree, temp);
18345796c8dcSSimon Schubert 
18355796c8dcSSimon Schubert 	  prev = parg = arg;
18365796c8dcSSimon Schubert 	  while ((parg = strpbrk (parg, "\\\"'\t ")))
18375796c8dcSSimon Schubert 	    {
18385796c8dcSSimon Schubert 	      strncpy (ptemp, prev, parg - prev);
18395796c8dcSSimon Schubert 	      ptemp += parg - prev;
18405796c8dcSSimon Schubert 	      prev = parg++;
18415796c8dcSSimon Schubert 	      *ptemp++ = '\\';
18425796c8dcSSimon Schubert 	    }
18435796c8dcSSimon Schubert 	  strcpy (ptemp, prev);
18445796c8dcSSimon Schubert 
18455796c8dcSSimon Schubert 	  arg = temp;
18465796c8dcSSimon Schubert 	}
18475796c8dcSSimon Schubert     }
18485796c8dcSSimon Schubert 
18495796c8dcSSimon Schubert   target_load (arg, from_tty);
18505796c8dcSSimon Schubert 
18515796c8dcSSimon Schubert   /* After re-loading the executable, we don't really know which
18525796c8dcSSimon Schubert      overlays are mapped any more.  */
18535796c8dcSSimon Schubert   overlay_cache_invalid = 1;
18545796c8dcSSimon Schubert }
18555796c8dcSSimon Schubert 
18565796c8dcSSimon Schubert /* This version of "load" should be usable for any target.  Currently
18575796c8dcSSimon Schubert    it is just used for remote targets, not inftarg.c or core files,
18585796c8dcSSimon Schubert    on the theory that only in that case is it useful.
18595796c8dcSSimon Schubert 
18605796c8dcSSimon Schubert    Avoiding xmodem and the like seems like a win (a) because we don't have
18615796c8dcSSimon Schubert    to worry about finding it, and (b) On VMS, fork() is very slow and so
18625796c8dcSSimon Schubert    we don't want to run a subprocess.  On the other hand, I'm not sure how
18635796c8dcSSimon Schubert    performance compares.  */
18645796c8dcSSimon Schubert 
18655796c8dcSSimon Schubert static int validate_download = 0;
18665796c8dcSSimon Schubert 
18675796c8dcSSimon Schubert /* Callback service function for generic_load (bfd_map_over_sections).  */
18685796c8dcSSimon Schubert 
18695796c8dcSSimon Schubert static void
18705796c8dcSSimon Schubert add_section_size_callback (bfd *abfd, asection *asec, void *data)
18715796c8dcSSimon Schubert {
18725796c8dcSSimon Schubert   bfd_size_type *sum = data;
18735796c8dcSSimon Schubert 
18745796c8dcSSimon Schubert   *sum += bfd_get_section_size (asec);
18755796c8dcSSimon Schubert }
18765796c8dcSSimon Schubert 
18775796c8dcSSimon Schubert /* Opaque data for load_section_callback.  */
18785796c8dcSSimon Schubert struct load_section_data {
18795796c8dcSSimon Schubert   unsigned long load_offset;
18805796c8dcSSimon Schubert   struct load_progress_data *progress_data;
18815796c8dcSSimon Schubert   VEC(memory_write_request_s) *requests;
18825796c8dcSSimon Schubert };
18835796c8dcSSimon Schubert 
18845796c8dcSSimon Schubert /* Opaque data for load_progress.  */
18855796c8dcSSimon Schubert struct load_progress_data {
18865796c8dcSSimon Schubert   /* Cumulative data.  */
18875796c8dcSSimon Schubert   unsigned long write_count;
18885796c8dcSSimon Schubert   unsigned long data_count;
18895796c8dcSSimon Schubert   bfd_size_type total_size;
18905796c8dcSSimon Schubert };
18915796c8dcSSimon Schubert 
18925796c8dcSSimon Schubert /* Opaque data for load_progress for a single section.  */
18935796c8dcSSimon Schubert struct load_progress_section_data {
18945796c8dcSSimon Schubert   struct load_progress_data *cumulative;
18955796c8dcSSimon Schubert 
18965796c8dcSSimon Schubert   /* Per-section data.  */
18975796c8dcSSimon Schubert   const char *section_name;
18985796c8dcSSimon Schubert   ULONGEST section_sent;
18995796c8dcSSimon Schubert   ULONGEST section_size;
19005796c8dcSSimon Schubert   CORE_ADDR lma;
19015796c8dcSSimon Schubert   gdb_byte *buffer;
19025796c8dcSSimon Schubert };
19035796c8dcSSimon Schubert 
19045796c8dcSSimon Schubert /* Target write callback routine for progress reporting.  */
19055796c8dcSSimon Schubert 
19065796c8dcSSimon Schubert static void
19075796c8dcSSimon Schubert load_progress (ULONGEST bytes, void *untyped_arg)
19085796c8dcSSimon Schubert {
19095796c8dcSSimon Schubert   struct load_progress_section_data *args = untyped_arg;
19105796c8dcSSimon Schubert   struct load_progress_data *totals;
19115796c8dcSSimon Schubert 
19125796c8dcSSimon Schubert   if (args == NULL)
19135796c8dcSSimon Schubert     /* Writing padding data.  No easy way to get at the cumulative
19145796c8dcSSimon Schubert        stats, so just ignore this.  */
19155796c8dcSSimon Schubert     return;
19165796c8dcSSimon Schubert 
19175796c8dcSSimon Schubert   totals = args->cumulative;
19185796c8dcSSimon Schubert 
19195796c8dcSSimon Schubert   if (bytes == 0 && args->section_sent == 0)
19205796c8dcSSimon Schubert     {
19215796c8dcSSimon Schubert       /* The write is just starting.  Let the user know we've started
19225796c8dcSSimon Schubert 	 this section.  */
1923*a45ae5f8SJohn Marino       ui_out_message (current_uiout, 0, "Loading section %s, size %s lma %s\n",
19245796c8dcSSimon Schubert 		      args->section_name, hex_string (args->section_size),
19255796c8dcSSimon Schubert 		      paddress (target_gdbarch, args->lma));
19265796c8dcSSimon Schubert       return;
19275796c8dcSSimon Schubert     }
19285796c8dcSSimon Schubert 
19295796c8dcSSimon Schubert   if (validate_download)
19305796c8dcSSimon Schubert     {
19315796c8dcSSimon Schubert       /* Broken memories and broken monitors manifest themselves here
19325796c8dcSSimon Schubert 	 when bring new computers to life.  This doubles already slow
19335796c8dcSSimon Schubert 	 downloads.  */
19345796c8dcSSimon Schubert       /* NOTE: cagney/1999-10-18: A more efficient implementation
19355796c8dcSSimon Schubert 	 might add a verify_memory() method to the target vector and
19365796c8dcSSimon Schubert 	 then use that.  remote.c could implement that method using
19375796c8dcSSimon Schubert 	 the ``qCRC'' packet.  */
19385796c8dcSSimon Schubert       gdb_byte *check = xmalloc (bytes);
19395796c8dcSSimon Schubert       struct cleanup *verify_cleanups = make_cleanup (xfree, check);
19405796c8dcSSimon Schubert 
19415796c8dcSSimon Schubert       if (target_read_memory (args->lma, check, bytes) != 0)
19425796c8dcSSimon Schubert 	error (_("Download verify read failed at %s"),
19435796c8dcSSimon Schubert 	       paddress (target_gdbarch, args->lma));
19445796c8dcSSimon Schubert       if (memcmp (args->buffer, check, bytes) != 0)
19455796c8dcSSimon Schubert 	error (_("Download verify compare failed at %s"),
19465796c8dcSSimon Schubert 	       paddress (target_gdbarch, args->lma));
19475796c8dcSSimon Schubert       do_cleanups (verify_cleanups);
19485796c8dcSSimon Schubert     }
19495796c8dcSSimon Schubert   totals->data_count += bytes;
19505796c8dcSSimon Schubert   args->lma += bytes;
19515796c8dcSSimon Schubert   args->buffer += bytes;
19525796c8dcSSimon Schubert   totals->write_count += 1;
19535796c8dcSSimon Schubert   args->section_sent += bytes;
19545796c8dcSSimon Schubert   if (quit_flag
19555796c8dcSSimon Schubert       || (deprecated_ui_load_progress_hook != NULL
19565796c8dcSSimon Schubert 	  && deprecated_ui_load_progress_hook (args->section_name,
19575796c8dcSSimon Schubert 					       args->section_sent)))
19585796c8dcSSimon Schubert     error (_("Canceled the download"));
19595796c8dcSSimon Schubert 
19605796c8dcSSimon Schubert   if (deprecated_show_load_progress != NULL)
19615796c8dcSSimon Schubert     deprecated_show_load_progress (args->section_name,
19625796c8dcSSimon Schubert 				   args->section_sent,
19635796c8dcSSimon Schubert 				   args->section_size,
19645796c8dcSSimon Schubert 				   totals->data_count,
19655796c8dcSSimon Schubert 				   totals->total_size);
19665796c8dcSSimon Schubert }
19675796c8dcSSimon Schubert 
19685796c8dcSSimon Schubert /* Callback service function for generic_load (bfd_map_over_sections).  */
19695796c8dcSSimon Schubert 
19705796c8dcSSimon Schubert static void
19715796c8dcSSimon Schubert load_section_callback (bfd *abfd, asection *asec, void *data)
19725796c8dcSSimon Schubert {
19735796c8dcSSimon Schubert   struct memory_write_request *new_request;
19745796c8dcSSimon Schubert   struct load_section_data *args = data;
19755796c8dcSSimon Schubert   struct load_progress_section_data *section_data;
19765796c8dcSSimon Schubert   bfd_size_type size = bfd_get_section_size (asec);
19775796c8dcSSimon Schubert   gdb_byte *buffer;
19785796c8dcSSimon Schubert   const char *sect_name = bfd_get_section_name (abfd, asec);
19795796c8dcSSimon Schubert 
19805796c8dcSSimon Schubert   if ((bfd_get_section_flags (abfd, asec) & SEC_LOAD) == 0)
19815796c8dcSSimon Schubert     return;
19825796c8dcSSimon Schubert 
19835796c8dcSSimon Schubert   if (size == 0)
19845796c8dcSSimon Schubert     return;
19855796c8dcSSimon Schubert 
19865796c8dcSSimon Schubert   new_request = VEC_safe_push (memory_write_request_s,
19875796c8dcSSimon Schubert 			       args->requests, NULL);
19885796c8dcSSimon Schubert   memset (new_request, 0, sizeof (struct memory_write_request));
19895796c8dcSSimon Schubert   section_data = xcalloc (1, sizeof (struct load_progress_section_data));
19905796c8dcSSimon Schubert   new_request->begin = bfd_section_lma (abfd, asec) + args->load_offset;
1991c50c785cSJohn Marino   new_request->end = new_request->begin + size; /* FIXME Should size
1992c50c785cSJohn Marino 						   be in instead?  */
19935796c8dcSSimon Schubert   new_request->data = xmalloc (size);
19945796c8dcSSimon Schubert   new_request->baton = section_data;
19955796c8dcSSimon Schubert 
19965796c8dcSSimon Schubert   buffer = new_request->data;
19975796c8dcSSimon Schubert 
19985796c8dcSSimon Schubert   section_data->cumulative = args->progress_data;
19995796c8dcSSimon Schubert   section_data->section_name = sect_name;
20005796c8dcSSimon Schubert   section_data->section_size = size;
20015796c8dcSSimon Schubert   section_data->lma = new_request->begin;
20025796c8dcSSimon Schubert   section_data->buffer = buffer;
20035796c8dcSSimon Schubert 
20045796c8dcSSimon Schubert   bfd_get_section_contents (abfd, asec, buffer, 0, size);
20055796c8dcSSimon Schubert }
20065796c8dcSSimon Schubert 
20075796c8dcSSimon Schubert /* Clean up an entire memory request vector, including load
20085796c8dcSSimon Schubert    data and progress records.  */
20095796c8dcSSimon Schubert 
20105796c8dcSSimon Schubert static void
20115796c8dcSSimon Schubert clear_memory_write_data (void *arg)
20125796c8dcSSimon Schubert {
20135796c8dcSSimon Schubert   VEC(memory_write_request_s) **vec_p = arg;
20145796c8dcSSimon Schubert   VEC(memory_write_request_s) *vec = *vec_p;
20155796c8dcSSimon Schubert   int i;
20165796c8dcSSimon Schubert   struct memory_write_request *mr;
20175796c8dcSSimon Schubert 
20185796c8dcSSimon Schubert   for (i = 0; VEC_iterate (memory_write_request_s, vec, i, mr); ++i)
20195796c8dcSSimon Schubert     {
20205796c8dcSSimon Schubert       xfree (mr->data);
20215796c8dcSSimon Schubert       xfree (mr->baton);
20225796c8dcSSimon Schubert     }
20235796c8dcSSimon Schubert   VEC_free (memory_write_request_s, vec);
20245796c8dcSSimon Schubert }
20255796c8dcSSimon Schubert 
20265796c8dcSSimon Schubert void
20275796c8dcSSimon Schubert generic_load (char *args, int from_tty)
20285796c8dcSSimon Schubert {
20295796c8dcSSimon Schubert   bfd *loadfile_bfd;
20305796c8dcSSimon Schubert   struct timeval start_time, end_time;
20315796c8dcSSimon Schubert   char *filename;
20325796c8dcSSimon Schubert   struct cleanup *old_cleanups = make_cleanup (null_cleanup, 0);
20335796c8dcSSimon Schubert   struct load_section_data cbdata;
20345796c8dcSSimon Schubert   struct load_progress_data total_progress;
2035*a45ae5f8SJohn Marino   struct ui_out *uiout = current_uiout;
20365796c8dcSSimon Schubert 
20375796c8dcSSimon Schubert   CORE_ADDR entry;
20385796c8dcSSimon Schubert   char **argv;
20395796c8dcSSimon Schubert 
20405796c8dcSSimon Schubert   memset (&cbdata, 0, sizeof (cbdata));
20415796c8dcSSimon Schubert   memset (&total_progress, 0, sizeof (total_progress));
20425796c8dcSSimon Schubert   cbdata.progress_data = &total_progress;
20435796c8dcSSimon Schubert 
20445796c8dcSSimon Schubert   make_cleanup (clear_memory_write_data, &cbdata.requests);
20455796c8dcSSimon Schubert 
20465796c8dcSSimon Schubert   if (args == NULL)
20475796c8dcSSimon Schubert     error_no_arg (_("file to load"));
20485796c8dcSSimon Schubert 
20495796c8dcSSimon Schubert   argv = gdb_buildargv (args);
20505796c8dcSSimon Schubert   make_cleanup_freeargv (argv);
20515796c8dcSSimon Schubert 
20525796c8dcSSimon Schubert   filename = tilde_expand (argv[0]);
20535796c8dcSSimon Schubert   make_cleanup (xfree, filename);
20545796c8dcSSimon Schubert 
20555796c8dcSSimon Schubert   if (argv[1] != NULL)
20565796c8dcSSimon Schubert     {
20575796c8dcSSimon Schubert       char *endptr;
20585796c8dcSSimon Schubert 
20595796c8dcSSimon Schubert       cbdata.load_offset = strtoul (argv[1], &endptr, 0);
20605796c8dcSSimon Schubert 
20615796c8dcSSimon Schubert       /* If the last word was not a valid number then
20625796c8dcSSimon Schubert          treat it as a file name with spaces in.  */
20635796c8dcSSimon Schubert       if (argv[1] == endptr)
20645796c8dcSSimon Schubert         error (_("Invalid download offset:%s."), argv[1]);
20655796c8dcSSimon Schubert 
20665796c8dcSSimon Schubert       if (argv[2] != NULL)
20675796c8dcSSimon Schubert 	error (_("Too many parameters."));
20685796c8dcSSimon Schubert     }
20695796c8dcSSimon Schubert 
20705796c8dcSSimon Schubert   /* Open the file for loading.  */
20715796c8dcSSimon Schubert   loadfile_bfd = bfd_openr (filename, gnutarget);
20725796c8dcSSimon Schubert   if (loadfile_bfd == NULL)
20735796c8dcSSimon Schubert     {
20745796c8dcSSimon Schubert       perror_with_name (filename);
20755796c8dcSSimon Schubert       return;
20765796c8dcSSimon Schubert     }
20775796c8dcSSimon Schubert 
20785796c8dcSSimon Schubert   /* FIXME: should be checking for errors from bfd_close (for one thing,
20795796c8dcSSimon Schubert      on error it does not free all the storage associated with the
20805796c8dcSSimon Schubert      bfd).  */
20815796c8dcSSimon Schubert   make_cleanup_bfd_close (loadfile_bfd);
20825796c8dcSSimon Schubert 
20835796c8dcSSimon Schubert   if (!bfd_check_format (loadfile_bfd, bfd_object))
20845796c8dcSSimon Schubert     {
20855796c8dcSSimon Schubert       error (_("\"%s\" is not an object file: %s"), filename,
20865796c8dcSSimon Schubert 	     bfd_errmsg (bfd_get_error ()));
20875796c8dcSSimon Schubert     }
20885796c8dcSSimon Schubert 
20895796c8dcSSimon Schubert   bfd_map_over_sections (loadfile_bfd, add_section_size_callback,
20905796c8dcSSimon Schubert 			 (void *) &total_progress.total_size);
20915796c8dcSSimon Schubert 
20925796c8dcSSimon Schubert   bfd_map_over_sections (loadfile_bfd, load_section_callback, &cbdata);
20935796c8dcSSimon Schubert 
20945796c8dcSSimon Schubert   gettimeofday (&start_time, NULL);
20955796c8dcSSimon Schubert 
20965796c8dcSSimon Schubert   if (target_write_memory_blocks (cbdata.requests, flash_discard,
20975796c8dcSSimon Schubert 				  load_progress) != 0)
20985796c8dcSSimon Schubert     error (_("Load failed"));
20995796c8dcSSimon Schubert 
21005796c8dcSSimon Schubert   gettimeofday (&end_time, NULL);
21015796c8dcSSimon Schubert 
21025796c8dcSSimon Schubert   entry = bfd_get_start_address (loadfile_bfd);
21035796c8dcSSimon Schubert   ui_out_text (uiout, "Start address ");
21045796c8dcSSimon Schubert   ui_out_field_fmt (uiout, "address", "%s", paddress (target_gdbarch, entry));
21055796c8dcSSimon Schubert   ui_out_text (uiout, ", load size ");
21065796c8dcSSimon Schubert   ui_out_field_fmt (uiout, "load-size", "%lu", total_progress.data_count);
21075796c8dcSSimon Schubert   ui_out_text (uiout, "\n");
21085796c8dcSSimon Schubert   /* We were doing this in remote-mips.c, I suspect it is right
21095796c8dcSSimon Schubert      for other targets too.  */
21105796c8dcSSimon Schubert   regcache_write_pc (get_current_regcache (), entry);
21115796c8dcSSimon Schubert 
2112cf7f2e2dSJohn Marino   /* Reset breakpoints, now that we have changed the load image.  For
2113cf7f2e2dSJohn Marino      instance, breakpoints may have been set (or reset, by
2114cf7f2e2dSJohn Marino      post_create_inferior) while connected to the target but before we
2115cf7f2e2dSJohn Marino      loaded the program.  In that case, the prologue analyzer could
2116cf7f2e2dSJohn Marino      have read instructions from the target to find the right
2117cf7f2e2dSJohn Marino      breakpoint locations.  Loading has changed the contents of that
2118cf7f2e2dSJohn Marino      memory.  */
2119cf7f2e2dSJohn Marino 
2120cf7f2e2dSJohn Marino   breakpoint_re_set ();
2121cf7f2e2dSJohn Marino 
21225796c8dcSSimon Schubert   /* FIXME: are we supposed to call symbol_file_add or not?  According
21235796c8dcSSimon Schubert      to a comment from remote-mips.c (where a call to symbol_file_add
21245796c8dcSSimon Schubert      was commented out), making the call confuses GDB if more than one
21255796c8dcSSimon Schubert      file is loaded in.  Some targets do (e.g., remote-vx.c) but
21265796c8dcSSimon Schubert      others don't (or didn't - perhaps they have all been deleted).  */
21275796c8dcSSimon Schubert 
21285796c8dcSSimon Schubert   print_transfer_performance (gdb_stdout, total_progress.data_count,
21295796c8dcSSimon Schubert 			      total_progress.write_count,
21305796c8dcSSimon Schubert 			      &start_time, &end_time);
21315796c8dcSSimon Schubert 
21325796c8dcSSimon Schubert   do_cleanups (old_cleanups);
21335796c8dcSSimon Schubert }
21345796c8dcSSimon Schubert 
21355796c8dcSSimon Schubert /* Report how fast the transfer went.  */
21365796c8dcSSimon Schubert 
21375796c8dcSSimon Schubert /* DEPRECATED: cagney/1999-10-18: report_transfer_performance is being
21385796c8dcSSimon Schubert    replaced by print_transfer_performance (with a very different
21395796c8dcSSimon Schubert    function signature).  */
21405796c8dcSSimon Schubert 
21415796c8dcSSimon Schubert void
21425796c8dcSSimon Schubert report_transfer_performance (unsigned long data_count, time_t start_time,
21435796c8dcSSimon Schubert 			     time_t end_time)
21445796c8dcSSimon Schubert {
21455796c8dcSSimon Schubert   struct timeval start, end;
21465796c8dcSSimon Schubert 
21475796c8dcSSimon Schubert   start.tv_sec = start_time;
21485796c8dcSSimon Schubert   start.tv_usec = 0;
21495796c8dcSSimon Schubert   end.tv_sec = end_time;
21505796c8dcSSimon Schubert   end.tv_usec = 0;
21515796c8dcSSimon Schubert 
21525796c8dcSSimon Schubert   print_transfer_performance (gdb_stdout, data_count, 0, &start, &end);
21535796c8dcSSimon Schubert }
21545796c8dcSSimon Schubert 
21555796c8dcSSimon Schubert void
21565796c8dcSSimon Schubert print_transfer_performance (struct ui_file *stream,
21575796c8dcSSimon Schubert 			    unsigned long data_count,
21585796c8dcSSimon Schubert 			    unsigned long write_count,
21595796c8dcSSimon Schubert 			    const struct timeval *start_time,
21605796c8dcSSimon Schubert 			    const struct timeval *end_time)
21615796c8dcSSimon Schubert {
21625796c8dcSSimon Schubert   ULONGEST time_count;
2163*a45ae5f8SJohn Marino   struct ui_out *uiout = current_uiout;
21645796c8dcSSimon Schubert 
21655796c8dcSSimon Schubert   /* Compute the elapsed time in milliseconds, as a tradeoff between
21665796c8dcSSimon Schubert      accuracy and overflow.  */
21675796c8dcSSimon Schubert   time_count = (end_time->tv_sec - start_time->tv_sec) * 1000;
21685796c8dcSSimon Schubert   time_count += (end_time->tv_usec - start_time->tv_usec) / 1000;
21695796c8dcSSimon Schubert 
21705796c8dcSSimon Schubert   ui_out_text (uiout, "Transfer rate: ");
21715796c8dcSSimon Schubert   if (time_count > 0)
21725796c8dcSSimon Schubert     {
21735796c8dcSSimon Schubert       unsigned long rate = ((ULONGEST) data_count * 1000) / time_count;
21745796c8dcSSimon Schubert 
21755796c8dcSSimon Schubert       if (ui_out_is_mi_like_p (uiout))
21765796c8dcSSimon Schubert 	{
21775796c8dcSSimon Schubert 	  ui_out_field_fmt (uiout, "transfer-rate", "%lu", rate * 8);
21785796c8dcSSimon Schubert 	  ui_out_text (uiout, " bits/sec");
21795796c8dcSSimon Schubert 	}
21805796c8dcSSimon Schubert       else if (rate < 1024)
21815796c8dcSSimon Schubert 	{
21825796c8dcSSimon Schubert 	  ui_out_field_fmt (uiout, "transfer-rate", "%lu", rate);
21835796c8dcSSimon Schubert 	  ui_out_text (uiout, " bytes/sec");
21845796c8dcSSimon Schubert 	}
21855796c8dcSSimon Schubert       else
21865796c8dcSSimon Schubert 	{
21875796c8dcSSimon Schubert 	  ui_out_field_fmt (uiout, "transfer-rate", "%lu", rate / 1024);
21885796c8dcSSimon Schubert 	  ui_out_text (uiout, " KB/sec");
21895796c8dcSSimon Schubert 	}
21905796c8dcSSimon Schubert     }
21915796c8dcSSimon Schubert   else
21925796c8dcSSimon Schubert     {
21935796c8dcSSimon Schubert       ui_out_field_fmt (uiout, "transferred-bits", "%lu", (data_count * 8));
21945796c8dcSSimon Schubert       ui_out_text (uiout, " bits in <1 sec");
21955796c8dcSSimon Schubert     }
21965796c8dcSSimon Schubert   if (write_count > 0)
21975796c8dcSSimon Schubert     {
21985796c8dcSSimon Schubert       ui_out_text (uiout, ", ");
21995796c8dcSSimon Schubert       ui_out_field_fmt (uiout, "write-rate", "%lu", data_count / write_count);
22005796c8dcSSimon Schubert       ui_out_text (uiout, " bytes/write");
22015796c8dcSSimon Schubert     }
22025796c8dcSSimon Schubert   ui_out_text (uiout, ".\n");
22035796c8dcSSimon Schubert }
22045796c8dcSSimon Schubert 
22055796c8dcSSimon Schubert /* This function allows the addition of incrementally linked object files.
22065796c8dcSSimon Schubert    It does not modify any state in the target, only in the debugger.  */
22075796c8dcSSimon Schubert /* Note: ezannoni 2000-04-13 This function/command used to have a
22085796c8dcSSimon Schubert    special case syntax for the rombug target (Rombug is the boot
22095796c8dcSSimon Schubert    monitor for Microware's OS-9 / OS-9000, see remote-os9k.c). In the
22105796c8dcSSimon Schubert    rombug case, the user doesn't need to supply a text address,
22115796c8dcSSimon Schubert    instead a call to target_link() (in target.c) would supply the
22125796c8dcSSimon Schubert    value to use.  We are now discontinuing this type of ad hoc syntax.  */
22135796c8dcSSimon Schubert 
22145796c8dcSSimon Schubert static void
22155796c8dcSSimon Schubert add_symbol_file_command (char *args, int from_tty)
22165796c8dcSSimon Schubert {
22175796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_current_arch ();
22185796c8dcSSimon Schubert   char *filename = NULL;
22195796c8dcSSimon Schubert   int flags = OBJF_USERLOADED;
22205796c8dcSSimon Schubert   char *arg;
22215796c8dcSSimon Schubert   int section_index = 0;
22225796c8dcSSimon Schubert   int argcnt = 0;
22235796c8dcSSimon Schubert   int sec_num = 0;
22245796c8dcSSimon Schubert   int i;
22255796c8dcSSimon Schubert   int expecting_sec_name = 0;
22265796c8dcSSimon Schubert   int expecting_sec_addr = 0;
22275796c8dcSSimon Schubert   char **argv;
22285796c8dcSSimon Schubert 
22295796c8dcSSimon Schubert   struct sect_opt
22305796c8dcSSimon Schubert   {
22315796c8dcSSimon Schubert     char *name;
22325796c8dcSSimon Schubert     char *value;
22335796c8dcSSimon Schubert   };
22345796c8dcSSimon Schubert 
22355796c8dcSSimon Schubert   struct section_addr_info *section_addrs;
22365796c8dcSSimon Schubert   struct sect_opt *sect_opts = NULL;
22375796c8dcSSimon Schubert   size_t num_sect_opts = 0;
22385796c8dcSSimon Schubert   struct cleanup *my_cleanups = make_cleanup (null_cleanup, NULL);
22395796c8dcSSimon Schubert 
22405796c8dcSSimon Schubert   num_sect_opts = 16;
22415796c8dcSSimon Schubert   sect_opts = (struct sect_opt *) xmalloc (num_sect_opts
22425796c8dcSSimon Schubert 					   * sizeof (struct sect_opt));
22435796c8dcSSimon Schubert 
22445796c8dcSSimon Schubert   dont_repeat ();
22455796c8dcSSimon Schubert 
22465796c8dcSSimon Schubert   if (args == NULL)
22475796c8dcSSimon Schubert     error (_("add-symbol-file takes a file name and an address"));
22485796c8dcSSimon Schubert 
22495796c8dcSSimon Schubert   argv = gdb_buildargv (args);
22505796c8dcSSimon Schubert   make_cleanup_freeargv (argv);
22515796c8dcSSimon Schubert 
22525796c8dcSSimon Schubert   for (arg = argv[0], argcnt = 0; arg != NULL; arg = argv[++argcnt])
22535796c8dcSSimon Schubert     {
22545796c8dcSSimon Schubert       /* Process the argument.  */
22555796c8dcSSimon Schubert       if (argcnt == 0)
22565796c8dcSSimon Schubert 	{
22575796c8dcSSimon Schubert 	  /* The first argument is the file name.  */
22585796c8dcSSimon Schubert 	  filename = tilde_expand (arg);
22595796c8dcSSimon Schubert 	  make_cleanup (xfree, filename);
22605796c8dcSSimon Schubert 	}
22615796c8dcSSimon Schubert       else
22625796c8dcSSimon Schubert 	if (argcnt == 1)
22635796c8dcSSimon Schubert 	  {
22645796c8dcSSimon Schubert 	    /* The second argument is always the text address at which
22655796c8dcSSimon Schubert                to load the program.  */
22665796c8dcSSimon Schubert 	    sect_opts[section_index].name = ".text";
22675796c8dcSSimon Schubert 	    sect_opts[section_index].value = arg;
22685796c8dcSSimon Schubert 	    if (++section_index >= num_sect_opts)
22695796c8dcSSimon Schubert 	      {
22705796c8dcSSimon Schubert 		num_sect_opts *= 2;
22715796c8dcSSimon Schubert 		sect_opts = ((struct sect_opt *)
22725796c8dcSSimon Schubert 			     xrealloc (sect_opts,
22735796c8dcSSimon Schubert 				       num_sect_opts
22745796c8dcSSimon Schubert 				       * sizeof (struct sect_opt)));
22755796c8dcSSimon Schubert 	      }
22765796c8dcSSimon Schubert 	  }
22775796c8dcSSimon Schubert 	else
22785796c8dcSSimon Schubert 	  {
22795796c8dcSSimon Schubert 	    /* It's an option (starting with '-') or it's an argument
2280c50c785cSJohn Marino 	       to an option.  */
22815796c8dcSSimon Schubert 
22825796c8dcSSimon Schubert 	    if (*arg == '-')
22835796c8dcSSimon Schubert 	      {
22845796c8dcSSimon Schubert 		if (strcmp (arg, "-readnow") == 0)
22855796c8dcSSimon Schubert 		  flags |= OBJF_READNOW;
22865796c8dcSSimon Schubert 		else if (strcmp (arg, "-s") == 0)
22875796c8dcSSimon Schubert 		  {
22885796c8dcSSimon Schubert 		    expecting_sec_name = 1;
22895796c8dcSSimon Schubert 		    expecting_sec_addr = 1;
22905796c8dcSSimon Schubert 		  }
22915796c8dcSSimon Schubert 	      }
22925796c8dcSSimon Schubert 	    else
22935796c8dcSSimon Schubert 	      {
22945796c8dcSSimon Schubert 		if (expecting_sec_name)
22955796c8dcSSimon Schubert 		  {
22965796c8dcSSimon Schubert 		    sect_opts[section_index].name = arg;
22975796c8dcSSimon Schubert 		    expecting_sec_name = 0;
22985796c8dcSSimon Schubert 		  }
22995796c8dcSSimon Schubert 		else
23005796c8dcSSimon Schubert 		  if (expecting_sec_addr)
23015796c8dcSSimon Schubert 		    {
23025796c8dcSSimon Schubert 		      sect_opts[section_index].value = arg;
23035796c8dcSSimon Schubert 		      expecting_sec_addr = 0;
23045796c8dcSSimon Schubert 		      if (++section_index >= num_sect_opts)
23055796c8dcSSimon Schubert 			{
23065796c8dcSSimon Schubert 			  num_sect_opts *= 2;
23075796c8dcSSimon Schubert 			  sect_opts = ((struct sect_opt *)
23085796c8dcSSimon Schubert 				       xrealloc (sect_opts,
23095796c8dcSSimon Schubert 						 num_sect_opts
23105796c8dcSSimon Schubert 						 * sizeof (struct sect_opt)));
23115796c8dcSSimon Schubert 			}
23125796c8dcSSimon Schubert 		    }
23135796c8dcSSimon Schubert 		  else
2314c50c785cSJohn Marino 		    error (_("USAGE: add-symbol-file <filename> <textaddress>"
2315*a45ae5f8SJohn Marino 			     " [-readnow] [-s <secname> <addr>]*"));
23165796c8dcSSimon Schubert 	      }
23175796c8dcSSimon Schubert 	  }
23185796c8dcSSimon Schubert     }
23195796c8dcSSimon Schubert 
23205796c8dcSSimon Schubert   /* This command takes at least two arguments.  The first one is a
23215796c8dcSSimon Schubert      filename, and the second is the address where this file has been
23225796c8dcSSimon Schubert      loaded.  Abort now if this address hasn't been provided by the
23235796c8dcSSimon Schubert      user.  */
23245796c8dcSSimon Schubert   if (section_index < 1)
23255796c8dcSSimon Schubert     error (_("The address where %s has been loaded is missing"), filename);
23265796c8dcSSimon Schubert 
23275796c8dcSSimon Schubert   /* Print the prompt for the query below.  And save the arguments into
23285796c8dcSSimon Schubert      a sect_addr_info structure to be passed around to other
23295796c8dcSSimon Schubert      functions.  We have to split this up into separate print
23305796c8dcSSimon Schubert      statements because hex_string returns a local static
23315796c8dcSSimon Schubert      string.  */
23325796c8dcSSimon Schubert 
23335796c8dcSSimon Schubert   printf_unfiltered (_("add symbol table from file \"%s\" at\n"), filename);
23345796c8dcSSimon Schubert   section_addrs = alloc_section_addr_info (section_index);
23355796c8dcSSimon Schubert   make_cleanup (xfree, section_addrs);
23365796c8dcSSimon Schubert   for (i = 0; i < section_index; i++)
23375796c8dcSSimon Schubert     {
23385796c8dcSSimon Schubert       CORE_ADDR addr;
23395796c8dcSSimon Schubert       char *val = sect_opts[i].value;
23405796c8dcSSimon Schubert       char *sec = sect_opts[i].name;
23415796c8dcSSimon Schubert 
23425796c8dcSSimon Schubert       addr = parse_and_eval_address (val);
23435796c8dcSSimon Schubert 
23445796c8dcSSimon Schubert       /* Here we store the section offsets in the order they were
23455796c8dcSSimon Schubert          entered on the command line.  */
23465796c8dcSSimon Schubert       section_addrs->other[sec_num].name = sec;
23475796c8dcSSimon Schubert       section_addrs->other[sec_num].addr = addr;
23485796c8dcSSimon Schubert       printf_unfiltered ("\t%s_addr = %s\n", sec,
23495796c8dcSSimon Schubert 			 paddress (gdbarch, addr));
23505796c8dcSSimon Schubert       sec_num++;
23515796c8dcSSimon Schubert 
23525796c8dcSSimon Schubert       /* The object's sections are initialized when a
23535796c8dcSSimon Schubert 	 call is made to build_objfile_section_table (objfile).
23545796c8dcSSimon Schubert 	 This happens in reread_symbols.
23555796c8dcSSimon Schubert 	 At this point, we don't know what file type this is,
23565796c8dcSSimon Schubert 	 so we can't determine what section names are valid.  */
23575796c8dcSSimon Schubert     }
23585796c8dcSSimon Schubert 
23595796c8dcSSimon Schubert   if (from_tty && (!query ("%s", "")))
23605796c8dcSSimon Schubert     error (_("Not confirmed."));
23615796c8dcSSimon Schubert 
23625796c8dcSSimon Schubert   symbol_file_add (filename, from_tty ? SYMFILE_VERBOSE : 0,
23635796c8dcSSimon Schubert                    section_addrs, flags);
23645796c8dcSSimon Schubert 
23655796c8dcSSimon Schubert   /* Getting new symbols may change our opinion about what is
23665796c8dcSSimon Schubert      frameless.  */
23675796c8dcSSimon Schubert   reinit_frame_cache ();
23685796c8dcSSimon Schubert   do_cleanups (my_cleanups);
23695796c8dcSSimon Schubert }
23705796c8dcSSimon Schubert 
23715796c8dcSSimon Schubert 
23725796c8dcSSimon Schubert /* Re-read symbols if a symbol-file has changed.  */
23735796c8dcSSimon Schubert void
23745796c8dcSSimon Schubert reread_symbols (void)
23755796c8dcSSimon Schubert {
23765796c8dcSSimon Schubert   struct objfile *objfile;
23775796c8dcSSimon Schubert   long new_modtime;
23785796c8dcSSimon Schubert   int reread_one = 0;
23795796c8dcSSimon Schubert   struct stat new_statbuf;
23805796c8dcSSimon Schubert   int res;
23815796c8dcSSimon Schubert 
23825796c8dcSSimon Schubert   /* With the addition of shared libraries, this should be modified,
23835796c8dcSSimon Schubert      the load time should be saved in the partial symbol tables, since
23845796c8dcSSimon Schubert      different tables may come from different source files.  FIXME.
23855796c8dcSSimon Schubert      This routine should then walk down each partial symbol table
2386c50c785cSJohn Marino      and see if the symbol table that it originates from has been changed.  */
23875796c8dcSSimon Schubert 
23885796c8dcSSimon Schubert   for (objfile = object_files; objfile; objfile = objfile->next)
23895796c8dcSSimon Schubert     {
2390cf7f2e2dSJohn Marino       /* solib-sunos.c creates one objfile with obfd.  */
2391cf7f2e2dSJohn Marino       if (objfile->obfd == NULL)
2392cf7f2e2dSJohn Marino 	continue;
23935796c8dcSSimon Schubert 
2394cf7f2e2dSJohn Marino       /* Separate debug objfiles are handled in the main objfile.  */
2395cf7f2e2dSJohn Marino       if (objfile->separate_debug_objfile_backlink)
2396cf7f2e2dSJohn Marino 	continue;
2397cf7f2e2dSJohn Marino 
2398cf7f2e2dSJohn Marino       /* If this object is from an archive (what you usually create with
2399cf7f2e2dSJohn Marino 	 `ar', often called a `static library' on most systems, though
2400cf7f2e2dSJohn Marino 	 a `shared library' on AIX is also an archive), then you should
2401cf7f2e2dSJohn Marino 	 stat on the archive name, not member name.  */
24025796c8dcSSimon Schubert       if (objfile->obfd->my_archive)
24035796c8dcSSimon Schubert 	res = stat (objfile->obfd->my_archive->filename, &new_statbuf);
24045796c8dcSSimon Schubert       else
24055796c8dcSSimon Schubert 	res = stat (objfile->name, &new_statbuf);
24065796c8dcSSimon Schubert       if (res != 0)
24075796c8dcSSimon Schubert 	{
24085796c8dcSSimon Schubert 	  /* FIXME, should use print_sys_errmsg but it's not filtered.  */
24095796c8dcSSimon Schubert 	  printf_unfiltered (_("`%s' has disappeared; keeping its symbols.\n"),
24105796c8dcSSimon Schubert 			     objfile->name);
24115796c8dcSSimon Schubert 	  continue;
24125796c8dcSSimon Schubert 	}
24135796c8dcSSimon Schubert       new_modtime = new_statbuf.st_mtime;
24145796c8dcSSimon Schubert       if (new_modtime != objfile->mtime)
24155796c8dcSSimon Schubert 	{
24165796c8dcSSimon Schubert 	  struct cleanup *old_cleanups;
24175796c8dcSSimon Schubert 	  struct section_offsets *offsets;
24185796c8dcSSimon Schubert 	  int num_offsets;
24195796c8dcSSimon Schubert 	  char *obfd_filename;
24205796c8dcSSimon Schubert 
24215796c8dcSSimon Schubert 	  printf_unfiltered (_("`%s' has changed; re-reading symbols.\n"),
24225796c8dcSSimon Schubert 			     objfile->name);
24235796c8dcSSimon Schubert 
24245796c8dcSSimon Schubert 	  /* There are various functions like symbol_file_add,
24255796c8dcSSimon Schubert 	     symfile_bfd_open, syms_from_objfile, etc., which might
24265796c8dcSSimon Schubert 	     appear to do what we want.  But they have various other
24275796c8dcSSimon Schubert 	     effects which we *don't* want.  So we just do stuff
24285796c8dcSSimon Schubert 	     ourselves.  We don't worry about mapped files (for one thing,
24295796c8dcSSimon Schubert 	     any mapped file will be out of date).  */
24305796c8dcSSimon Schubert 
24315796c8dcSSimon Schubert 	  /* If we get an error, blow away this objfile (not sure if
24325796c8dcSSimon Schubert 	     that is the correct response for things like shared
24335796c8dcSSimon Schubert 	     libraries).  */
24345796c8dcSSimon Schubert 	  old_cleanups = make_cleanup_free_objfile (objfile);
24355796c8dcSSimon Schubert 	  /* We need to do this whenever any symbols go away.  */
24365796c8dcSSimon Schubert 	  make_cleanup (clear_symtab_users_cleanup, 0 /*ignore*/);
24375796c8dcSSimon Schubert 
2438c50c785cSJohn Marino 	  if (exec_bfd != NULL
2439c50c785cSJohn Marino 	      && filename_cmp (bfd_get_filename (objfile->obfd),
24405796c8dcSSimon Schubert 			       bfd_get_filename (exec_bfd)) == 0)
24415796c8dcSSimon Schubert 	    {
24425796c8dcSSimon Schubert 	      /* Reload EXEC_BFD without asking anything.  */
24435796c8dcSSimon Schubert 
24445796c8dcSSimon Schubert 	      exec_file_attach (bfd_get_filename (objfile->obfd), 0);
24455796c8dcSSimon Schubert 	    }
24465796c8dcSSimon Schubert 
2447*a45ae5f8SJohn Marino 	  /* Keep the calls order approx. the same as in free_objfile.  */
2448*a45ae5f8SJohn Marino 
2449*a45ae5f8SJohn Marino 	  /* Free the separate debug objfiles.  It will be
2450*a45ae5f8SJohn Marino 	     automatically recreated by sym_read.  */
2451*a45ae5f8SJohn Marino 	  free_objfile_separate_debug (objfile);
2452*a45ae5f8SJohn Marino 
2453*a45ae5f8SJohn Marino 	  /* Remove any references to this objfile in the global
2454*a45ae5f8SJohn Marino 	     value lists.  */
2455*a45ae5f8SJohn Marino 	  preserve_values (objfile);
2456*a45ae5f8SJohn Marino 
2457*a45ae5f8SJohn Marino 	  /* Nuke all the state that we will re-read.  Much of the following
2458*a45ae5f8SJohn Marino 	     code which sets things to NULL really is necessary to tell
2459*a45ae5f8SJohn Marino 	     other parts of GDB that there is nothing currently there.
2460*a45ae5f8SJohn Marino 
2461*a45ae5f8SJohn Marino 	     Try to keep the freeing order compatible with free_objfile.  */
2462*a45ae5f8SJohn Marino 
2463*a45ae5f8SJohn Marino 	  if (objfile->sf != NULL)
2464*a45ae5f8SJohn Marino 	    {
2465*a45ae5f8SJohn Marino 	      (*objfile->sf->sym_finish) (objfile);
2466*a45ae5f8SJohn Marino 	    }
2467*a45ae5f8SJohn Marino 
2468*a45ae5f8SJohn Marino 	  clear_objfile_data (objfile);
2469*a45ae5f8SJohn Marino 
24705796c8dcSSimon Schubert 	  /* Clean up any state BFD has sitting around.  We don't need
24715796c8dcSSimon Schubert 	     to close the descriptor but BFD lacks a way of closing the
24725796c8dcSSimon Schubert 	     BFD without closing the descriptor.  */
24735796c8dcSSimon Schubert 	  obfd_filename = bfd_get_filename (objfile->obfd);
24745796c8dcSSimon Schubert 	  if (!bfd_close (objfile->obfd))
24755796c8dcSSimon Schubert 	    error (_("Can't close BFD for %s: %s"), objfile->name,
24765796c8dcSSimon Schubert 		   bfd_errmsg (bfd_get_error ()));
2477cf7f2e2dSJohn Marino 	  objfile->obfd = bfd_open_maybe_remote (obfd_filename);
24785796c8dcSSimon Schubert 	  if (objfile->obfd == NULL)
24795796c8dcSSimon Schubert 	    error (_("Can't open %s to read symbols."), objfile->name);
24805796c8dcSSimon Schubert 	  else
24815796c8dcSSimon Schubert 	    objfile->obfd = gdb_bfd_ref (objfile->obfd);
24825796c8dcSSimon Schubert 	  /* bfd_openr sets cacheable to true, which is what we want.  */
24835796c8dcSSimon Schubert 	  if (!bfd_check_format (objfile->obfd, bfd_object))
24845796c8dcSSimon Schubert 	    error (_("Can't read symbols from %s: %s."), objfile->name,
24855796c8dcSSimon Schubert 		   bfd_errmsg (bfd_get_error ()));
24865796c8dcSSimon Schubert 
24875796c8dcSSimon Schubert 	  /* Save the offsets, we will nuke them with the rest of the
24885796c8dcSSimon Schubert 	     objfile_obstack.  */
24895796c8dcSSimon Schubert 	  num_offsets = objfile->num_sections;
24905796c8dcSSimon Schubert 	  offsets = ((struct section_offsets *)
24915796c8dcSSimon Schubert 		     alloca (SIZEOF_N_SECTION_OFFSETS (num_offsets)));
24925796c8dcSSimon Schubert 	  memcpy (offsets, objfile->section_offsets,
24935796c8dcSSimon Schubert 		  SIZEOF_N_SECTION_OFFSETS (num_offsets));
24945796c8dcSSimon Schubert 
24955796c8dcSSimon Schubert 	  /* FIXME: Do we have to free a whole linked list, or is this
24965796c8dcSSimon Schubert 	     enough?  */
24975796c8dcSSimon Schubert 	  if (objfile->global_psymbols.list)
24985796c8dcSSimon Schubert 	    xfree (objfile->global_psymbols.list);
24995796c8dcSSimon Schubert 	  memset (&objfile->global_psymbols, 0,
25005796c8dcSSimon Schubert 		  sizeof (objfile->global_psymbols));
25015796c8dcSSimon Schubert 	  if (objfile->static_psymbols.list)
25025796c8dcSSimon Schubert 	    xfree (objfile->static_psymbols.list);
25035796c8dcSSimon Schubert 	  memset (&objfile->static_psymbols, 0,
25045796c8dcSSimon Schubert 		  sizeof (objfile->static_psymbols));
25055796c8dcSSimon Schubert 
2506c50c785cSJohn Marino 	  /* Free the obstacks for non-reusable objfiles.  */
2507c50c785cSJohn Marino 	  psymbol_bcache_free (objfile->psymbol_cache);
2508c50c785cSJohn Marino 	  objfile->psymbol_cache = psymbol_bcache_init ();
25095796c8dcSSimon Schubert 	  bcache_xfree (objfile->macro_cache);
2510c50c785cSJohn Marino 	  objfile->macro_cache = bcache_xmalloc (NULL, NULL);
2511cf7f2e2dSJohn Marino 	  bcache_xfree (objfile->filename_cache);
2512c50c785cSJohn Marino 	  objfile->filename_cache = bcache_xmalloc (NULL,NULL);
25135796c8dcSSimon Schubert 	  if (objfile->demangled_names_hash != NULL)
25145796c8dcSSimon Schubert 	    {
25155796c8dcSSimon Schubert 	      htab_delete (objfile->demangled_names_hash);
25165796c8dcSSimon Schubert 	      objfile->demangled_names_hash = NULL;
25175796c8dcSSimon Schubert 	    }
25185796c8dcSSimon Schubert 	  obstack_free (&objfile->objfile_obstack, 0);
25195796c8dcSSimon Schubert 	  objfile->sections = NULL;
25205796c8dcSSimon Schubert 	  objfile->symtabs = NULL;
25215796c8dcSSimon Schubert 	  objfile->psymtabs = NULL;
25225796c8dcSSimon Schubert 	  objfile->psymtabs_addrmap = NULL;
25235796c8dcSSimon Schubert 	  objfile->free_psymtabs = NULL;
2524c50c785cSJohn Marino 	  objfile->template_symbols = NULL;
25255796c8dcSSimon Schubert 	  objfile->msymbols = NULL;
25265796c8dcSSimon Schubert 	  objfile->deprecated_sym_private = NULL;
25275796c8dcSSimon Schubert 	  objfile->minimal_symbol_count = 0;
25285796c8dcSSimon Schubert 	  memset (&objfile->msymbol_hash, 0,
25295796c8dcSSimon Schubert 		  sizeof (objfile->msymbol_hash));
25305796c8dcSSimon Schubert 	  memset (&objfile->msymbol_demangled_hash, 0,
25315796c8dcSSimon Schubert 		  sizeof (objfile->msymbol_demangled_hash));
25325796c8dcSSimon Schubert 
25335796c8dcSSimon Schubert 	  /* obstack_init also initializes the obstack so it is
25345796c8dcSSimon Schubert 	     empty.  We could use obstack_specify_allocation but
25355796c8dcSSimon Schubert 	     gdb_obstack.h specifies the alloc/dealloc
25365796c8dcSSimon Schubert 	     functions.  */
25375796c8dcSSimon Schubert 	  obstack_init (&objfile->objfile_obstack);
25385796c8dcSSimon Schubert 	  if (build_objfile_section_table (objfile))
25395796c8dcSSimon Schubert 	    {
25405796c8dcSSimon Schubert 	      error (_("Can't find the file sections in `%s': %s"),
25415796c8dcSSimon Schubert 		     objfile->name, bfd_errmsg (bfd_get_error ()));
25425796c8dcSSimon Schubert 	    }
25435796c8dcSSimon Schubert 	  terminate_minimal_symbol_table (objfile);
25445796c8dcSSimon Schubert 
25455796c8dcSSimon Schubert 	  /* We use the same section offsets as from last time.  I'm not
25465796c8dcSSimon Schubert 	     sure whether that is always correct for shared libraries.  */
25475796c8dcSSimon Schubert 	  objfile->section_offsets = (struct section_offsets *)
25485796c8dcSSimon Schubert 	    obstack_alloc (&objfile->objfile_obstack,
25495796c8dcSSimon Schubert 			   SIZEOF_N_SECTION_OFFSETS (num_offsets));
25505796c8dcSSimon Schubert 	  memcpy (objfile->section_offsets, offsets,
25515796c8dcSSimon Schubert 		  SIZEOF_N_SECTION_OFFSETS (num_offsets));
25525796c8dcSSimon Schubert 	  objfile->num_sections = num_offsets;
25535796c8dcSSimon Schubert 
25545796c8dcSSimon Schubert 	  /* What the hell is sym_new_init for, anyway?  The concept of
25555796c8dcSSimon Schubert 	     distinguishing between the main file and additional files
25565796c8dcSSimon Schubert 	     in this way seems rather dubious.  */
25575796c8dcSSimon Schubert 	  if (objfile == symfile_objfile)
25585796c8dcSSimon Schubert 	    {
25595796c8dcSSimon Schubert 	      (*objfile->sf->sym_new_init) (objfile);
25605796c8dcSSimon Schubert 	    }
25615796c8dcSSimon Schubert 
25625796c8dcSSimon Schubert 	  (*objfile->sf->sym_init) (objfile);
25635796c8dcSSimon Schubert 	  clear_complaints (&symfile_complaints, 1, 1);
2564cf7f2e2dSJohn Marino 	  /* Do not set flags as this is safe and we don't want to be
2565cf7f2e2dSJohn Marino              verbose.  */
25665796c8dcSSimon Schubert 	  (*objfile->sf->sym_read) (objfile, 0);
2567c50c785cSJohn Marino 	  if ((objfile->flags & OBJF_PSYMTABS_READ) != 0)
2568c50c785cSJohn Marino 	    {
2569c50c785cSJohn Marino 	      objfile->flags &= ~OBJF_PSYMTABS_READ;
2570c50c785cSJohn Marino 	      require_partial_symbols (objfile, 0);
2571c50c785cSJohn Marino 	    }
2572c50c785cSJohn Marino 
2573cf7f2e2dSJohn Marino 	  if (!objfile_has_symbols (objfile))
25745796c8dcSSimon Schubert 	    {
25755796c8dcSSimon Schubert 	      wrap_here ("");
25765796c8dcSSimon Schubert 	      printf_unfiltered (_("(no debugging symbols found)\n"));
25775796c8dcSSimon Schubert 	      wrap_here ("");
25785796c8dcSSimon Schubert 	    }
25795796c8dcSSimon Schubert 
25805796c8dcSSimon Schubert 	  /* We're done reading the symbol file; finish off complaints.  */
25815796c8dcSSimon Schubert 	  clear_complaints (&symfile_complaints, 0, 1);
25825796c8dcSSimon Schubert 
25835796c8dcSSimon Schubert 	  /* Getting new symbols may change our opinion about what is
25845796c8dcSSimon Schubert 	     frameless.  */
25855796c8dcSSimon Schubert 
25865796c8dcSSimon Schubert 	  reinit_frame_cache ();
25875796c8dcSSimon Schubert 
25885796c8dcSSimon Schubert 	  /* Discard cleanups as symbol reading was successful.  */
25895796c8dcSSimon Schubert 	  discard_cleanups (old_cleanups);
25905796c8dcSSimon Schubert 
25915796c8dcSSimon Schubert 	  /* If the mtime has changed between the time we set new_modtime
25925796c8dcSSimon Schubert 	     and now, we *want* this to be out of date, so don't call stat
25935796c8dcSSimon Schubert 	     again now.  */
25945796c8dcSSimon Schubert 	  objfile->mtime = new_modtime;
25955796c8dcSSimon Schubert 	  reread_one = 1;
25965796c8dcSSimon Schubert 	  init_entry_point_info (objfile);
25975796c8dcSSimon Schubert 	}
25985796c8dcSSimon Schubert     }
25995796c8dcSSimon Schubert 
26005796c8dcSSimon Schubert   if (reread_one)
26015796c8dcSSimon Schubert     {
26025796c8dcSSimon Schubert       /* Notify objfiles that we've modified objfile sections.  */
26035796c8dcSSimon Schubert       objfiles_changed ();
26045796c8dcSSimon Schubert 
2605c50c785cSJohn Marino       clear_symtab_users (0);
26065796c8dcSSimon Schubert       /* At least one objfile has changed, so we can consider that
26075796c8dcSSimon Schubert          the executable we're debugging has changed too.  */
26085796c8dcSSimon Schubert       observer_notify_executable_changed ();
26095796c8dcSSimon Schubert     }
26105796c8dcSSimon Schubert }
26115796c8dcSSimon Schubert 
26125796c8dcSSimon Schubert 
26135796c8dcSSimon Schubert 
26145796c8dcSSimon Schubert typedef struct
26155796c8dcSSimon Schubert {
26165796c8dcSSimon Schubert   char *ext;
26175796c8dcSSimon Schubert   enum language lang;
26185796c8dcSSimon Schubert }
26195796c8dcSSimon Schubert filename_language;
26205796c8dcSSimon Schubert 
26215796c8dcSSimon Schubert static filename_language *filename_language_table;
26225796c8dcSSimon Schubert static int fl_table_size, fl_table_next;
26235796c8dcSSimon Schubert 
26245796c8dcSSimon Schubert static void
26255796c8dcSSimon Schubert add_filename_language (char *ext, enum language lang)
26265796c8dcSSimon Schubert {
26275796c8dcSSimon Schubert   if (fl_table_next >= fl_table_size)
26285796c8dcSSimon Schubert     {
26295796c8dcSSimon Schubert       fl_table_size += 10;
26305796c8dcSSimon Schubert       filename_language_table =
26315796c8dcSSimon Schubert 	xrealloc (filename_language_table,
26325796c8dcSSimon Schubert 		  fl_table_size * sizeof (*filename_language_table));
26335796c8dcSSimon Schubert     }
26345796c8dcSSimon Schubert 
26355796c8dcSSimon Schubert   filename_language_table[fl_table_next].ext = xstrdup (ext);
26365796c8dcSSimon Schubert   filename_language_table[fl_table_next].lang = lang;
26375796c8dcSSimon Schubert   fl_table_next++;
26385796c8dcSSimon Schubert }
26395796c8dcSSimon Schubert 
26405796c8dcSSimon Schubert static char *ext_args;
26415796c8dcSSimon Schubert static void
26425796c8dcSSimon Schubert show_ext_args (struct ui_file *file, int from_tty,
26435796c8dcSSimon Schubert 	       struct cmd_list_element *c, const char *value)
26445796c8dcSSimon Schubert {
2645c50c785cSJohn Marino   fprintf_filtered (file,
2646c50c785cSJohn Marino 		    _("Mapping between filename extension "
2647c50c785cSJohn Marino 		      "and source language is \"%s\".\n"),
26485796c8dcSSimon Schubert 		    value);
26495796c8dcSSimon Schubert }
26505796c8dcSSimon Schubert 
26515796c8dcSSimon Schubert static void
26525796c8dcSSimon Schubert set_ext_lang_command (char *args, int from_tty, struct cmd_list_element *e)
26535796c8dcSSimon Schubert {
26545796c8dcSSimon Schubert   int i;
26555796c8dcSSimon Schubert   char *cp = ext_args;
26565796c8dcSSimon Schubert   enum language lang;
26575796c8dcSSimon Schubert 
26585796c8dcSSimon Schubert   /* First arg is filename extension, starting with '.'  */
26595796c8dcSSimon Schubert   if (*cp != '.')
26605796c8dcSSimon Schubert     error (_("'%s': Filename extension must begin with '.'"), ext_args);
26615796c8dcSSimon Schubert 
26625796c8dcSSimon Schubert   /* Find end of first arg.  */
26635796c8dcSSimon Schubert   while (*cp && !isspace (*cp))
26645796c8dcSSimon Schubert     cp++;
26655796c8dcSSimon Schubert 
26665796c8dcSSimon Schubert   if (*cp == '\0')
2667c50c785cSJohn Marino     error (_("'%s': two arguments required -- "
2668c50c785cSJohn Marino 	     "filename extension and language"),
26695796c8dcSSimon Schubert 	   ext_args);
26705796c8dcSSimon Schubert 
2671c50c785cSJohn Marino   /* Null-terminate first arg.  */
26725796c8dcSSimon Schubert   *cp++ = '\0';
26735796c8dcSSimon Schubert 
26745796c8dcSSimon Schubert   /* Find beginning of second arg, which should be a source language.  */
26755796c8dcSSimon Schubert   while (*cp && isspace (*cp))
26765796c8dcSSimon Schubert     cp++;
26775796c8dcSSimon Schubert 
26785796c8dcSSimon Schubert   if (*cp == '\0')
2679c50c785cSJohn Marino     error (_("'%s': two arguments required -- "
2680c50c785cSJohn Marino 	     "filename extension and language"),
26815796c8dcSSimon Schubert 	   ext_args);
26825796c8dcSSimon Schubert 
26835796c8dcSSimon Schubert   /* Lookup the language from among those we know.  */
26845796c8dcSSimon Schubert   lang = language_enum (cp);
26855796c8dcSSimon Schubert 
26865796c8dcSSimon Schubert   /* Now lookup the filename extension: do we already know it?  */
26875796c8dcSSimon Schubert   for (i = 0; i < fl_table_next; i++)
26885796c8dcSSimon Schubert     if (0 == strcmp (ext_args, filename_language_table[i].ext))
26895796c8dcSSimon Schubert       break;
26905796c8dcSSimon Schubert 
26915796c8dcSSimon Schubert   if (i >= fl_table_next)
26925796c8dcSSimon Schubert     {
2693c50c785cSJohn Marino       /* New file extension.  */
26945796c8dcSSimon Schubert       add_filename_language (ext_args, lang);
26955796c8dcSSimon Schubert     }
26965796c8dcSSimon Schubert   else
26975796c8dcSSimon Schubert     {
2698c50c785cSJohn Marino       /* Redefining a previously known filename extension.  */
26995796c8dcSSimon Schubert 
27005796c8dcSSimon Schubert       /* if (from_tty) */
27015796c8dcSSimon Schubert       /*   query ("Really make files of type %s '%s'?", */
27025796c8dcSSimon Schubert       /*          ext_args, language_str (lang));           */
27035796c8dcSSimon Schubert 
27045796c8dcSSimon Schubert       xfree (filename_language_table[i].ext);
27055796c8dcSSimon Schubert       filename_language_table[i].ext = xstrdup (ext_args);
27065796c8dcSSimon Schubert       filename_language_table[i].lang = lang;
27075796c8dcSSimon Schubert     }
27085796c8dcSSimon Schubert }
27095796c8dcSSimon Schubert 
27105796c8dcSSimon Schubert static void
27115796c8dcSSimon Schubert info_ext_lang_command (char *args, int from_tty)
27125796c8dcSSimon Schubert {
27135796c8dcSSimon Schubert   int i;
27145796c8dcSSimon Schubert 
27155796c8dcSSimon Schubert   printf_filtered (_("Filename extensions and the languages they represent:"));
27165796c8dcSSimon Schubert   printf_filtered ("\n\n");
27175796c8dcSSimon Schubert   for (i = 0; i < fl_table_next; i++)
27185796c8dcSSimon Schubert     printf_filtered ("\t%s\t- %s\n",
27195796c8dcSSimon Schubert 		     filename_language_table[i].ext,
27205796c8dcSSimon Schubert 		     language_str (filename_language_table[i].lang));
27215796c8dcSSimon Schubert }
27225796c8dcSSimon Schubert 
27235796c8dcSSimon Schubert static void
27245796c8dcSSimon Schubert init_filename_language_table (void)
27255796c8dcSSimon Schubert {
2726c50c785cSJohn Marino   if (fl_table_size == 0)	/* Protect against repetition.  */
27275796c8dcSSimon Schubert     {
27285796c8dcSSimon Schubert       fl_table_size = 20;
27295796c8dcSSimon Schubert       fl_table_next = 0;
27305796c8dcSSimon Schubert       filename_language_table =
27315796c8dcSSimon Schubert 	xmalloc (fl_table_size * sizeof (*filename_language_table));
27325796c8dcSSimon Schubert       add_filename_language (".c", language_c);
2733cf7f2e2dSJohn Marino       add_filename_language (".d", language_d);
27345796c8dcSSimon Schubert       add_filename_language (".C", language_cplus);
27355796c8dcSSimon Schubert       add_filename_language (".cc", language_cplus);
27365796c8dcSSimon Schubert       add_filename_language (".cp", language_cplus);
27375796c8dcSSimon Schubert       add_filename_language (".cpp", language_cplus);
27385796c8dcSSimon Schubert       add_filename_language (".cxx", language_cplus);
27395796c8dcSSimon Schubert       add_filename_language (".c++", language_cplus);
27405796c8dcSSimon Schubert       add_filename_language (".java", language_java);
27415796c8dcSSimon Schubert       add_filename_language (".class", language_java);
27425796c8dcSSimon Schubert       add_filename_language (".m", language_objc);
27435796c8dcSSimon Schubert       add_filename_language (".f", language_fortran);
27445796c8dcSSimon Schubert       add_filename_language (".F", language_fortran);
2745cf7f2e2dSJohn Marino       add_filename_language (".for", language_fortran);
2746cf7f2e2dSJohn Marino       add_filename_language (".FOR", language_fortran);
2747cf7f2e2dSJohn Marino       add_filename_language (".ftn", language_fortran);
2748cf7f2e2dSJohn Marino       add_filename_language (".FTN", language_fortran);
2749cf7f2e2dSJohn Marino       add_filename_language (".fpp", language_fortran);
2750cf7f2e2dSJohn Marino       add_filename_language (".FPP", language_fortran);
2751cf7f2e2dSJohn Marino       add_filename_language (".f90", language_fortran);
2752cf7f2e2dSJohn Marino       add_filename_language (".F90", language_fortran);
2753cf7f2e2dSJohn Marino       add_filename_language (".f95", language_fortran);
2754cf7f2e2dSJohn Marino       add_filename_language (".F95", language_fortran);
2755cf7f2e2dSJohn Marino       add_filename_language (".f03", language_fortran);
2756cf7f2e2dSJohn Marino       add_filename_language (".F03", language_fortran);
2757cf7f2e2dSJohn Marino       add_filename_language (".f08", language_fortran);
2758cf7f2e2dSJohn Marino       add_filename_language (".F08", language_fortran);
27595796c8dcSSimon Schubert       add_filename_language (".s", language_asm);
27605796c8dcSSimon Schubert       add_filename_language (".sx", language_asm);
27615796c8dcSSimon Schubert       add_filename_language (".S", language_asm);
27625796c8dcSSimon Schubert       add_filename_language (".pas", language_pascal);
27635796c8dcSSimon Schubert       add_filename_language (".p", language_pascal);
27645796c8dcSSimon Schubert       add_filename_language (".pp", language_pascal);
27655796c8dcSSimon Schubert       add_filename_language (".adb", language_ada);
27665796c8dcSSimon Schubert       add_filename_language (".ads", language_ada);
27675796c8dcSSimon Schubert       add_filename_language (".a", language_ada);
27685796c8dcSSimon Schubert       add_filename_language (".ada", language_ada);
2769cf7f2e2dSJohn Marino       add_filename_language (".dg", language_ada);
27705796c8dcSSimon Schubert     }
27715796c8dcSSimon Schubert }
27725796c8dcSSimon Schubert 
27735796c8dcSSimon Schubert enum language
2774c50c785cSJohn Marino deduce_language_from_filename (const char *filename)
27755796c8dcSSimon Schubert {
27765796c8dcSSimon Schubert   int i;
27775796c8dcSSimon Schubert   char *cp;
27785796c8dcSSimon Schubert 
27795796c8dcSSimon Schubert   if (filename != NULL)
27805796c8dcSSimon Schubert     if ((cp = strrchr (filename, '.')) != NULL)
27815796c8dcSSimon Schubert       for (i = 0; i < fl_table_next; i++)
27825796c8dcSSimon Schubert 	if (strcmp (cp, filename_language_table[i].ext) == 0)
27835796c8dcSSimon Schubert 	  return filename_language_table[i].lang;
27845796c8dcSSimon Schubert 
27855796c8dcSSimon Schubert   return language_unknown;
27865796c8dcSSimon Schubert }
27875796c8dcSSimon Schubert 
27885796c8dcSSimon Schubert /* allocate_symtab:
27895796c8dcSSimon Schubert 
27905796c8dcSSimon Schubert    Allocate and partly initialize a new symbol table.  Return a pointer
27915796c8dcSSimon Schubert    to it.  error() if no space.
27925796c8dcSSimon Schubert 
27935796c8dcSSimon Schubert    Caller must set these fields:
27945796c8dcSSimon Schubert    LINETABLE(symtab)
27955796c8dcSSimon Schubert    symtab->blockvector
27965796c8dcSSimon Schubert    symtab->dirname
27975796c8dcSSimon Schubert    symtab->free_code
27985796c8dcSSimon Schubert    symtab->free_ptr
27995796c8dcSSimon Schubert  */
28005796c8dcSSimon Schubert 
28015796c8dcSSimon Schubert struct symtab *
2802c50c785cSJohn Marino allocate_symtab (const char *filename, struct objfile *objfile)
28035796c8dcSSimon Schubert {
28045796c8dcSSimon Schubert   struct symtab *symtab;
28055796c8dcSSimon Schubert 
28065796c8dcSSimon Schubert   symtab = (struct symtab *)
28075796c8dcSSimon Schubert     obstack_alloc (&objfile->objfile_obstack, sizeof (struct symtab));
28085796c8dcSSimon Schubert   memset (symtab, 0, sizeof (*symtab));
2809cf7f2e2dSJohn Marino   symtab->filename = (char *) bcache (filename, strlen (filename) + 1,
2810cf7f2e2dSJohn Marino 				      objfile->filename_cache);
28115796c8dcSSimon Schubert   symtab->fullname = NULL;
28125796c8dcSSimon Schubert   symtab->language = deduce_language_from_filename (filename);
2813cf7f2e2dSJohn Marino   symtab->debugformat = "unknown";
28145796c8dcSSimon Schubert 
2815c50c785cSJohn Marino   /* Hook it to the objfile it comes from.  */
28165796c8dcSSimon Schubert 
28175796c8dcSSimon Schubert   symtab->objfile = objfile;
28185796c8dcSSimon Schubert   symtab->next = objfile->symtabs;
28195796c8dcSSimon Schubert   objfile->symtabs = symtab;
28205796c8dcSSimon Schubert 
28215796c8dcSSimon Schubert   return (symtab);
28225796c8dcSSimon Schubert }
28235796c8dcSSimon Schubert 
28245796c8dcSSimon Schubert 
28255796c8dcSSimon Schubert /* Reset all data structures in gdb which may contain references to symbol
2826c50c785cSJohn Marino    table data.  ADD_FLAGS is a bitmask of enum symfile_add_flags.  */
28275796c8dcSSimon Schubert 
28285796c8dcSSimon Schubert void
2829c50c785cSJohn Marino clear_symtab_users (int add_flags)
28305796c8dcSSimon Schubert {
28315796c8dcSSimon Schubert   /* Someday, we should do better than this, by only blowing away
28325796c8dcSSimon Schubert      the things that really need to be blown.  */
28335796c8dcSSimon Schubert 
28345796c8dcSSimon Schubert   /* Clear the "current" symtab first, because it is no longer valid.
28355796c8dcSSimon Schubert      breakpoint_re_set may try to access the current symtab.  */
28365796c8dcSSimon Schubert   clear_current_source_symtab_and_line ();
28375796c8dcSSimon Schubert 
28385796c8dcSSimon Schubert   clear_displays ();
2839c50c785cSJohn Marino   if ((add_flags & SYMFILE_DEFER_BP_RESET) == 0)
28405796c8dcSSimon Schubert     breakpoint_re_set ();
2841*a45ae5f8SJohn Marino   clear_last_displayed_sal ();
28425796c8dcSSimon Schubert   clear_pc_function_cache ();
28435796c8dcSSimon Schubert   observer_notify_new_objfile (NULL);
28445796c8dcSSimon Schubert 
28455796c8dcSSimon Schubert   /* Clear globals which might have pointed into a removed objfile.
28465796c8dcSSimon Schubert      FIXME: It's not clear which of these are supposed to persist
28475796c8dcSSimon Schubert      between expressions and which ought to be reset each time.  */
28485796c8dcSSimon Schubert   expression_context_block = NULL;
28495796c8dcSSimon Schubert   innermost_block = NULL;
28505796c8dcSSimon Schubert 
28515796c8dcSSimon Schubert   /* Varobj may refer to old symbols, perform a cleanup.  */
28525796c8dcSSimon Schubert   varobj_invalidate ();
28535796c8dcSSimon Schubert 
28545796c8dcSSimon Schubert }
28555796c8dcSSimon Schubert 
28565796c8dcSSimon Schubert static void
28575796c8dcSSimon Schubert clear_symtab_users_cleanup (void *ignore)
28585796c8dcSSimon Schubert {
2859c50c785cSJohn Marino   clear_symtab_users (0);
28605796c8dcSSimon Schubert }
28615796c8dcSSimon Schubert 
28625796c8dcSSimon Schubert /* OVERLAYS:
28635796c8dcSSimon Schubert    The following code implements an abstraction for debugging overlay sections.
28645796c8dcSSimon Schubert 
28655796c8dcSSimon Schubert    The target model is as follows:
28665796c8dcSSimon Schubert    1) The gnu linker will permit multiple sections to be mapped into the
28675796c8dcSSimon Schubert    same VMA, each with its own unique LMA (or load address).
28685796c8dcSSimon Schubert    2) It is assumed that some runtime mechanism exists for mapping the
28695796c8dcSSimon Schubert    sections, one by one, from the load address into the VMA address.
28705796c8dcSSimon Schubert    3) This code provides a mechanism for gdb to keep track of which
28715796c8dcSSimon Schubert    sections should be considered to be mapped from the VMA to the LMA.
28725796c8dcSSimon Schubert    This information is used for symbol lookup, and memory read/write.
28735796c8dcSSimon Schubert    For instance, if a section has been mapped then its contents
28745796c8dcSSimon Schubert    should be read from the VMA, otherwise from the LMA.
28755796c8dcSSimon Schubert 
28765796c8dcSSimon Schubert    Two levels of debugger support for overlays are available.  One is
28775796c8dcSSimon Schubert    "manual", in which the debugger relies on the user to tell it which
28785796c8dcSSimon Schubert    overlays are currently mapped.  This level of support is
28795796c8dcSSimon Schubert    implemented entirely in the core debugger, and the information about
28805796c8dcSSimon Schubert    whether a section is mapped is kept in the objfile->obj_section table.
28815796c8dcSSimon Schubert 
28825796c8dcSSimon Schubert    The second level of support is "automatic", and is only available if
28835796c8dcSSimon Schubert    the target-specific code provides functionality to read the target's
28845796c8dcSSimon Schubert    overlay mapping table, and translate its contents for the debugger
28855796c8dcSSimon Schubert    (by updating the mapped state information in the obj_section tables).
28865796c8dcSSimon Schubert 
28875796c8dcSSimon Schubert    The interface is as follows:
28885796c8dcSSimon Schubert    User commands:
28895796c8dcSSimon Schubert    overlay map <name>   -- tell gdb to consider this section mapped
28905796c8dcSSimon Schubert    overlay unmap <name> -- tell gdb to consider this section unmapped
28915796c8dcSSimon Schubert    overlay list         -- list the sections that GDB thinks are mapped
28925796c8dcSSimon Schubert    overlay read-target  -- get the target's state of what's mapped
28935796c8dcSSimon Schubert    overlay off/manual/auto -- set overlay debugging state
28945796c8dcSSimon Schubert    Functional interface:
28955796c8dcSSimon Schubert    find_pc_mapped_section(pc):    if the pc is in the range of a mapped
28965796c8dcSSimon Schubert    section, return that section.
28975796c8dcSSimon Schubert    find_pc_overlay(pc):       find any overlay section that contains
28985796c8dcSSimon Schubert    the pc, either in its VMA or its LMA
28995796c8dcSSimon Schubert    section_is_mapped(sect):       true if overlay is marked as mapped
29005796c8dcSSimon Schubert    section_is_overlay(sect):      true if section's VMA != LMA
29015796c8dcSSimon Schubert    pc_in_mapped_range(pc,sec):    true if pc belongs to section's VMA
29025796c8dcSSimon Schubert    pc_in_unmapped_range(...):     true if pc belongs to section's LMA
29035796c8dcSSimon Schubert    sections_overlap(sec1, sec2):  true if mapped sec1 and sec2 ranges overlap
29045796c8dcSSimon Schubert    overlay_mapped_address(...):   map an address from section's LMA to VMA
29055796c8dcSSimon Schubert    overlay_unmapped_address(...): map an address from section's VMA to LMA
29065796c8dcSSimon Schubert    symbol_overlayed_address(...): Return a "current" address for symbol:
29075796c8dcSSimon Schubert    either in VMA or LMA depending on whether
2908c50c785cSJohn Marino    the symbol's section is currently mapped.  */
29095796c8dcSSimon Schubert 
29105796c8dcSSimon Schubert /* Overlay debugging state: */
29115796c8dcSSimon Schubert 
29125796c8dcSSimon Schubert enum overlay_debugging_state overlay_debugging = ovly_off;
2913c50c785cSJohn Marino int overlay_cache_invalid = 0;	/* True if need to refresh mapped state.  */
29145796c8dcSSimon Schubert 
29155796c8dcSSimon Schubert /* Function: section_is_overlay (SECTION)
29165796c8dcSSimon Schubert    Returns true if SECTION has VMA not equal to LMA, ie.
29175796c8dcSSimon Schubert    SECTION is loaded at an address different from where it will "run".  */
29185796c8dcSSimon Schubert 
29195796c8dcSSimon Schubert int
29205796c8dcSSimon Schubert section_is_overlay (struct obj_section *section)
29215796c8dcSSimon Schubert {
29225796c8dcSSimon Schubert   if (overlay_debugging && section)
29235796c8dcSSimon Schubert     {
29245796c8dcSSimon Schubert       bfd *abfd = section->objfile->obfd;
29255796c8dcSSimon Schubert       asection *bfd_section = section->the_bfd_section;
29265796c8dcSSimon Schubert 
29275796c8dcSSimon Schubert       if (bfd_section_lma (abfd, bfd_section) != 0
29285796c8dcSSimon Schubert 	  && bfd_section_lma (abfd, bfd_section)
29295796c8dcSSimon Schubert 	     != bfd_section_vma (abfd, bfd_section))
29305796c8dcSSimon Schubert 	return 1;
29315796c8dcSSimon Schubert     }
29325796c8dcSSimon Schubert 
29335796c8dcSSimon Schubert   return 0;
29345796c8dcSSimon Schubert }
29355796c8dcSSimon Schubert 
29365796c8dcSSimon Schubert /* Function: overlay_invalidate_all (void)
29375796c8dcSSimon Schubert    Invalidate the mapped state of all overlay sections (mark it as stale).  */
29385796c8dcSSimon Schubert 
29395796c8dcSSimon Schubert static void
29405796c8dcSSimon Schubert overlay_invalidate_all (void)
29415796c8dcSSimon Schubert {
29425796c8dcSSimon Schubert   struct objfile *objfile;
29435796c8dcSSimon Schubert   struct obj_section *sect;
29445796c8dcSSimon Schubert 
29455796c8dcSSimon Schubert   ALL_OBJSECTIONS (objfile, sect)
29465796c8dcSSimon Schubert     if (section_is_overlay (sect))
29475796c8dcSSimon Schubert       sect->ovly_mapped = -1;
29485796c8dcSSimon Schubert }
29495796c8dcSSimon Schubert 
29505796c8dcSSimon Schubert /* Function: section_is_mapped (SECTION)
29515796c8dcSSimon Schubert    Returns true if section is an overlay, and is currently mapped.
29525796c8dcSSimon Schubert 
29535796c8dcSSimon Schubert    Access to the ovly_mapped flag is restricted to this function, so
29545796c8dcSSimon Schubert    that we can do automatic update.  If the global flag
29555796c8dcSSimon Schubert    OVERLAY_CACHE_INVALID is set (by wait_for_inferior), then call
29565796c8dcSSimon Schubert    overlay_invalidate_all.  If the mapped state of the particular
29575796c8dcSSimon Schubert    section is stale, then call TARGET_OVERLAY_UPDATE to refresh it.  */
29585796c8dcSSimon Schubert 
29595796c8dcSSimon Schubert int
29605796c8dcSSimon Schubert section_is_mapped (struct obj_section *osect)
29615796c8dcSSimon Schubert {
29625796c8dcSSimon Schubert   struct gdbarch *gdbarch;
29635796c8dcSSimon Schubert 
29645796c8dcSSimon Schubert   if (osect == 0 || !section_is_overlay (osect))
29655796c8dcSSimon Schubert     return 0;
29665796c8dcSSimon Schubert 
29675796c8dcSSimon Schubert   switch (overlay_debugging)
29685796c8dcSSimon Schubert     {
29695796c8dcSSimon Schubert     default:
29705796c8dcSSimon Schubert     case ovly_off:
29715796c8dcSSimon Schubert       return 0;			/* overlay debugging off */
29725796c8dcSSimon Schubert     case ovly_auto:		/* overlay debugging automatic */
29735796c8dcSSimon Schubert       /* Unles there is a gdbarch_overlay_update function,
2974c50c785cSJohn Marino          there's really nothing useful to do here (can't really go auto).  */
29755796c8dcSSimon Schubert       gdbarch = get_objfile_arch (osect->objfile);
29765796c8dcSSimon Schubert       if (gdbarch_overlay_update_p (gdbarch))
29775796c8dcSSimon Schubert 	{
29785796c8dcSSimon Schubert 	  if (overlay_cache_invalid)
29795796c8dcSSimon Schubert 	    {
29805796c8dcSSimon Schubert 	      overlay_invalidate_all ();
29815796c8dcSSimon Schubert 	      overlay_cache_invalid = 0;
29825796c8dcSSimon Schubert 	    }
29835796c8dcSSimon Schubert 	  if (osect->ovly_mapped == -1)
29845796c8dcSSimon Schubert 	    gdbarch_overlay_update (gdbarch, osect);
29855796c8dcSSimon Schubert 	}
29865796c8dcSSimon Schubert       /* fall thru to manual case */
29875796c8dcSSimon Schubert     case ovly_on:		/* overlay debugging manual */
29885796c8dcSSimon Schubert       return osect->ovly_mapped == 1;
29895796c8dcSSimon Schubert     }
29905796c8dcSSimon Schubert }
29915796c8dcSSimon Schubert 
29925796c8dcSSimon Schubert /* Function: pc_in_unmapped_range
29935796c8dcSSimon Schubert    If PC falls into the lma range of SECTION, return true, else false.  */
29945796c8dcSSimon Schubert 
29955796c8dcSSimon Schubert CORE_ADDR
29965796c8dcSSimon Schubert pc_in_unmapped_range (CORE_ADDR pc, struct obj_section *section)
29975796c8dcSSimon Schubert {
29985796c8dcSSimon Schubert   if (section_is_overlay (section))
29995796c8dcSSimon Schubert     {
30005796c8dcSSimon Schubert       bfd *abfd = section->objfile->obfd;
30015796c8dcSSimon Schubert       asection *bfd_section = section->the_bfd_section;
30025796c8dcSSimon Schubert 
30035796c8dcSSimon Schubert       /* We assume the LMA is relocated by the same offset as the VMA.  */
30045796c8dcSSimon Schubert       bfd_vma size = bfd_get_section_size (bfd_section);
30055796c8dcSSimon Schubert       CORE_ADDR offset = obj_section_offset (section);
30065796c8dcSSimon Schubert 
30075796c8dcSSimon Schubert       if (bfd_get_section_lma (abfd, bfd_section) + offset <= pc
30085796c8dcSSimon Schubert 	  && pc < bfd_get_section_lma (abfd, bfd_section) + offset + size)
30095796c8dcSSimon Schubert 	return 1;
30105796c8dcSSimon Schubert     }
30115796c8dcSSimon Schubert 
30125796c8dcSSimon Schubert   return 0;
30135796c8dcSSimon Schubert }
30145796c8dcSSimon Schubert 
30155796c8dcSSimon Schubert /* Function: pc_in_mapped_range
30165796c8dcSSimon Schubert    If PC falls into the vma range of SECTION, return true, else false.  */
30175796c8dcSSimon Schubert 
30185796c8dcSSimon Schubert CORE_ADDR
30195796c8dcSSimon Schubert pc_in_mapped_range (CORE_ADDR pc, struct obj_section *section)
30205796c8dcSSimon Schubert {
30215796c8dcSSimon Schubert   if (section_is_overlay (section))
30225796c8dcSSimon Schubert     {
30235796c8dcSSimon Schubert       if (obj_section_addr (section) <= pc
30245796c8dcSSimon Schubert 	  && pc < obj_section_endaddr (section))
30255796c8dcSSimon Schubert 	return 1;
30265796c8dcSSimon Schubert     }
30275796c8dcSSimon Schubert 
30285796c8dcSSimon Schubert   return 0;
30295796c8dcSSimon Schubert }
30305796c8dcSSimon Schubert 
30315796c8dcSSimon Schubert 
30325796c8dcSSimon Schubert /* Return true if the mapped ranges of sections A and B overlap, false
30335796c8dcSSimon Schubert    otherwise.  */
30345796c8dcSSimon Schubert static int
30355796c8dcSSimon Schubert sections_overlap (struct obj_section *a, struct obj_section *b)
30365796c8dcSSimon Schubert {
30375796c8dcSSimon Schubert   CORE_ADDR a_start = obj_section_addr (a);
30385796c8dcSSimon Schubert   CORE_ADDR a_end = obj_section_endaddr (a);
30395796c8dcSSimon Schubert   CORE_ADDR b_start = obj_section_addr (b);
30405796c8dcSSimon Schubert   CORE_ADDR b_end = obj_section_endaddr (b);
30415796c8dcSSimon Schubert 
30425796c8dcSSimon Schubert   return (a_start < b_end && b_start < a_end);
30435796c8dcSSimon Schubert }
30445796c8dcSSimon Schubert 
30455796c8dcSSimon Schubert /* Function: overlay_unmapped_address (PC, SECTION)
30465796c8dcSSimon Schubert    Returns the address corresponding to PC in the unmapped (load) range.
30475796c8dcSSimon Schubert    May be the same as PC.  */
30485796c8dcSSimon Schubert 
30495796c8dcSSimon Schubert CORE_ADDR
30505796c8dcSSimon Schubert overlay_unmapped_address (CORE_ADDR pc, struct obj_section *section)
30515796c8dcSSimon Schubert {
30525796c8dcSSimon Schubert   if (section_is_overlay (section) && pc_in_mapped_range (pc, section))
30535796c8dcSSimon Schubert     {
30545796c8dcSSimon Schubert       bfd *abfd = section->objfile->obfd;
30555796c8dcSSimon Schubert       asection *bfd_section = section->the_bfd_section;
30565796c8dcSSimon Schubert 
30575796c8dcSSimon Schubert       return pc + bfd_section_lma (abfd, bfd_section)
30585796c8dcSSimon Schubert 		- bfd_section_vma (abfd, bfd_section);
30595796c8dcSSimon Schubert     }
30605796c8dcSSimon Schubert 
30615796c8dcSSimon Schubert   return pc;
30625796c8dcSSimon Schubert }
30635796c8dcSSimon Schubert 
30645796c8dcSSimon Schubert /* Function: overlay_mapped_address (PC, SECTION)
30655796c8dcSSimon Schubert    Returns the address corresponding to PC in the mapped (runtime) range.
30665796c8dcSSimon Schubert    May be the same as PC.  */
30675796c8dcSSimon Schubert 
30685796c8dcSSimon Schubert CORE_ADDR
30695796c8dcSSimon Schubert overlay_mapped_address (CORE_ADDR pc, struct obj_section *section)
30705796c8dcSSimon Schubert {
30715796c8dcSSimon Schubert   if (section_is_overlay (section) && pc_in_unmapped_range (pc, section))
30725796c8dcSSimon Schubert     {
30735796c8dcSSimon Schubert       bfd *abfd = section->objfile->obfd;
30745796c8dcSSimon Schubert       asection *bfd_section = section->the_bfd_section;
30755796c8dcSSimon Schubert 
30765796c8dcSSimon Schubert       return pc + bfd_section_vma (abfd, bfd_section)
30775796c8dcSSimon Schubert 		- bfd_section_lma (abfd, bfd_section);
30785796c8dcSSimon Schubert     }
30795796c8dcSSimon Schubert 
30805796c8dcSSimon Schubert   return pc;
30815796c8dcSSimon Schubert }
30825796c8dcSSimon Schubert 
30835796c8dcSSimon Schubert 
30845796c8dcSSimon Schubert /* Function: symbol_overlayed_address
30855796c8dcSSimon Schubert    Return one of two addresses (relative to the VMA or to the LMA),
30865796c8dcSSimon Schubert    depending on whether the section is mapped or not.  */
30875796c8dcSSimon Schubert 
30885796c8dcSSimon Schubert CORE_ADDR
30895796c8dcSSimon Schubert symbol_overlayed_address (CORE_ADDR address, struct obj_section *section)
30905796c8dcSSimon Schubert {
30915796c8dcSSimon Schubert   if (overlay_debugging)
30925796c8dcSSimon Schubert     {
30935796c8dcSSimon Schubert       /* If the symbol has no section, just return its regular address.  */
30945796c8dcSSimon Schubert       if (section == 0)
30955796c8dcSSimon Schubert 	return address;
3096c50c785cSJohn Marino       /* If the symbol's section is not an overlay, just return its
3097c50c785cSJohn Marino 	 address.  */
30985796c8dcSSimon Schubert       if (!section_is_overlay (section))
30995796c8dcSSimon Schubert 	return address;
3100c50c785cSJohn Marino       /* If the symbol's section is mapped, just return its address.  */
31015796c8dcSSimon Schubert       if (section_is_mapped (section))
31025796c8dcSSimon Schubert 	return address;
31035796c8dcSSimon Schubert       /*
31045796c8dcSSimon Schubert        * HOWEVER: if the symbol is in an overlay section which is NOT mapped,
31055796c8dcSSimon Schubert        * then return its LOADED address rather than its vma address!!
31065796c8dcSSimon Schubert        */
31075796c8dcSSimon Schubert       return overlay_unmapped_address (address, section);
31085796c8dcSSimon Schubert     }
31095796c8dcSSimon Schubert   return address;
31105796c8dcSSimon Schubert }
31115796c8dcSSimon Schubert 
31125796c8dcSSimon Schubert /* Function: find_pc_overlay (PC)
31135796c8dcSSimon Schubert    Return the best-match overlay section for PC:
31145796c8dcSSimon Schubert    If PC matches a mapped overlay section's VMA, return that section.
31155796c8dcSSimon Schubert    Else if PC matches an unmapped section's VMA, return that section.
31165796c8dcSSimon Schubert    Else if PC matches an unmapped section's LMA, return that section.  */
31175796c8dcSSimon Schubert 
31185796c8dcSSimon Schubert struct obj_section *
31195796c8dcSSimon Schubert find_pc_overlay (CORE_ADDR pc)
31205796c8dcSSimon Schubert {
31215796c8dcSSimon Schubert   struct objfile *objfile;
31225796c8dcSSimon Schubert   struct obj_section *osect, *best_match = NULL;
31235796c8dcSSimon Schubert 
31245796c8dcSSimon Schubert   if (overlay_debugging)
31255796c8dcSSimon Schubert     ALL_OBJSECTIONS (objfile, osect)
31265796c8dcSSimon Schubert       if (section_is_overlay (osect))
31275796c8dcSSimon Schubert       {
31285796c8dcSSimon Schubert 	if (pc_in_mapped_range (pc, osect))
31295796c8dcSSimon Schubert 	  {
31305796c8dcSSimon Schubert 	    if (section_is_mapped (osect))
31315796c8dcSSimon Schubert 	      return osect;
31325796c8dcSSimon Schubert 	    else
31335796c8dcSSimon Schubert 	      best_match = osect;
31345796c8dcSSimon Schubert 	  }
31355796c8dcSSimon Schubert 	else if (pc_in_unmapped_range (pc, osect))
31365796c8dcSSimon Schubert 	  best_match = osect;
31375796c8dcSSimon Schubert       }
31385796c8dcSSimon Schubert   return best_match;
31395796c8dcSSimon Schubert }
31405796c8dcSSimon Schubert 
31415796c8dcSSimon Schubert /* Function: find_pc_mapped_section (PC)
31425796c8dcSSimon Schubert    If PC falls into the VMA address range of an overlay section that is
31435796c8dcSSimon Schubert    currently marked as MAPPED, return that section.  Else return NULL.  */
31445796c8dcSSimon Schubert 
31455796c8dcSSimon Schubert struct obj_section *
31465796c8dcSSimon Schubert find_pc_mapped_section (CORE_ADDR pc)
31475796c8dcSSimon Schubert {
31485796c8dcSSimon Schubert   struct objfile *objfile;
31495796c8dcSSimon Schubert   struct obj_section *osect;
31505796c8dcSSimon Schubert 
31515796c8dcSSimon Schubert   if (overlay_debugging)
31525796c8dcSSimon Schubert     ALL_OBJSECTIONS (objfile, osect)
31535796c8dcSSimon Schubert       if (pc_in_mapped_range (pc, osect) && section_is_mapped (osect))
31545796c8dcSSimon Schubert 	return osect;
31555796c8dcSSimon Schubert 
31565796c8dcSSimon Schubert   return NULL;
31575796c8dcSSimon Schubert }
31585796c8dcSSimon Schubert 
31595796c8dcSSimon Schubert /* Function: list_overlays_command
3160c50c785cSJohn Marino    Print a list of mapped sections and their PC ranges.  */
31615796c8dcSSimon Schubert 
31625796c8dcSSimon Schubert void
31635796c8dcSSimon Schubert list_overlays_command (char *args, int from_tty)
31645796c8dcSSimon Schubert {
31655796c8dcSSimon Schubert   int nmapped = 0;
31665796c8dcSSimon Schubert   struct objfile *objfile;
31675796c8dcSSimon Schubert   struct obj_section *osect;
31685796c8dcSSimon Schubert 
31695796c8dcSSimon Schubert   if (overlay_debugging)
31705796c8dcSSimon Schubert     ALL_OBJSECTIONS (objfile, osect)
31715796c8dcSSimon Schubert       if (section_is_mapped (osect))
31725796c8dcSSimon Schubert       {
31735796c8dcSSimon Schubert 	struct gdbarch *gdbarch = get_objfile_arch (objfile);
31745796c8dcSSimon Schubert 	const char *name;
31755796c8dcSSimon Schubert 	bfd_vma lma, vma;
31765796c8dcSSimon Schubert 	int size;
31775796c8dcSSimon Schubert 
31785796c8dcSSimon Schubert 	vma = bfd_section_vma (objfile->obfd, osect->the_bfd_section);
31795796c8dcSSimon Schubert 	lma = bfd_section_lma (objfile->obfd, osect->the_bfd_section);
31805796c8dcSSimon Schubert 	size = bfd_get_section_size (osect->the_bfd_section);
31815796c8dcSSimon Schubert 	name = bfd_section_name (objfile->obfd, osect->the_bfd_section);
31825796c8dcSSimon Schubert 
31835796c8dcSSimon Schubert 	printf_filtered ("Section %s, loaded at ", name);
31845796c8dcSSimon Schubert 	fputs_filtered (paddress (gdbarch, lma), gdb_stdout);
31855796c8dcSSimon Schubert 	puts_filtered (" - ");
31865796c8dcSSimon Schubert 	fputs_filtered (paddress (gdbarch, lma + size), gdb_stdout);
31875796c8dcSSimon Schubert 	printf_filtered (", mapped at ");
31885796c8dcSSimon Schubert 	fputs_filtered (paddress (gdbarch, vma), gdb_stdout);
31895796c8dcSSimon Schubert 	puts_filtered (" - ");
31905796c8dcSSimon Schubert 	fputs_filtered (paddress (gdbarch, vma + size), gdb_stdout);
31915796c8dcSSimon Schubert 	puts_filtered ("\n");
31925796c8dcSSimon Schubert 
31935796c8dcSSimon Schubert 	nmapped++;
31945796c8dcSSimon Schubert       }
31955796c8dcSSimon Schubert   if (nmapped == 0)
31965796c8dcSSimon Schubert     printf_filtered (_("No sections are mapped.\n"));
31975796c8dcSSimon Schubert }
31985796c8dcSSimon Schubert 
31995796c8dcSSimon Schubert /* Function: map_overlay_command
32005796c8dcSSimon Schubert    Mark the named section as mapped (ie. residing at its VMA address).  */
32015796c8dcSSimon Schubert 
32025796c8dcSSimon Schubert void
32035796c8dcSSimon Schubert map_overlay_command (char *args, int from_tty)
32045796c8dcSSimon Schubert {
32055796c8dcSSimon Schubert   struct objfile *objfile, *objfile2;
32065796c8dcSSimon Schubert   struct obj_section *sec, *sec2;
32075796c8dcSSimon Schubert 
32085796c8dcSSimon Schubert   if (!overlay_debugging)
3209c50c785cSJohn Marino     error (_("Overlay debugging not enabled.  Use "
3210c50c785cSJohn Marino 	     "either the 'overlay auto' or\n"
3211c50c785cSJohn Marino 	     "the 'overlay manual' command."));
32125796c8dcSSimon Schubert 
32135796c8dcSSimon Schubert   if (args == 0 || *args == 0)
32145796c8dcSSimon Schubert     error (_("Argument required: name of an overlay section"));
32155796c8dcSSimon Schubert 
3216c50c785cSJohn Marino   /* First, find a section matching the user supplied argument.  */
32175796c8dcSSimon Schubert   ALL_OBJSECTIONS (objfile, sec)
32185796c8dcSSimon Schubert     if (!strcmp (bfd_section_name (objfile->obfd, sec->the_bfd_section), args))
32195796c8dcSSimon Schubert     {
32205796c8dcSSimon Schubert       /* Now, check to see if the section is an overlay.  */
32215796c8dcSSimon Schubert       if (!section_is_overlay (sec))
32225796c8dcSSimon Schubert 	continue;		/* not an overlay section */
32235796c8dcSSimon Schubert 
3224c50c785cSJohn Marino       /* Mark the overlay as "mapped".  */
32255796c8dcSSimon Schubert       sec->ovly_mapped = 1;
32265796c8dcSSimon Schubert 
32275796c8dcSSimon Schubert       /* Next, make a pass and unmap any sections that are
32285796c8dcSSimon Schubert          overlapped by this new section: */
32295796c8dcSSimon Schubert       ALL_OBJSECTIONS (objfile2, sec2)
32305796c8dcSSimon Schubert 	if (sec2->ovly_mapped && sec != sec2 && sections_overlap (sec, sec2))
32315796c8dcSSimon Schubert 	{
32325796c8dcSSimon Schubert 	  if (info_verbose)
32335796c8dcSSimon Schubert 	    printf_unfiltered (_("Note: section %s unmapped by overlap\n"),
32345796c8dcSSimon Schubert 			     bfd_section_name (objfile->obfd,
32355796c8dcSSimon Schubert 					       sec2->the_bfd_section));
3236c50c785cSJohn Marino 	  sec2->ovly_mapped = 0;	/* sec2 overlaps sec: unmap sec2.  */
32375796c8dcSSimon Schubert 	}
32385796c8dcSSimon Schubert       return;
32395796c8dcSSimon Schubert     }
32405796c8dcSSimon Schubert   error (_("No overlay section called %s"), args);
32415796c8dcSSimon Schubert }
32425796c8dcSSimon Schubert 
32435796c8dcSSimon Schubert /* Function: unmap_overlay_command
32445796c8dcSSimon Schubert    Mark the overlay section as unmapped
32455796c8dcSSimon Schubert    (ie. resident in its LMA address range, rather than the VMA range).  */
32465796c8dcSSimon Schubert 
32475796c8dcSSimon Schubert void
32485796c8dcSSimon Schubert unmap_overlay_command (char *args, int from_tty)
32495796c8dcSSimon Schubert {
32505796c8dcSSimon Schubert   struct objfile *objfile;
32515796c8dcSSimon Schubert   struct obj_section *sec;
32525796c8dcSSimon Schubert 
32535796c8dcSSimon Schubert   if (!overlay_debugging)
3254c50c785cSJohn Marino     error (_("Overlay debugging not enabled.  "
3255c50c785cSJohn Marino 	     "Use either the 'overlay auto' or\n"
3256c50c785cSJohn Marino 	     "the 'overlay manual' command."));
32575796c8dcSSimon Schubert 
32585796c8dcSSimon Schubert   if (args == 0 || *args == 0)
32595796c8dcSSimon Schubert     error (_("Argument required: name of an overlay section"));
32605796c8dcSSimon Schubert 
3261c50c785cSJohn Marino   /* First, find a section matching the user supplied argument.  */
32625796c8dcSSimon Schubert   ALL_OBJSECTIONS (objfile, sec)
32635796c8dcSSimon Schubert     if (!strcmp (bfd_section_name (objfile->obfd, sec->the_bfd_section), args))
32645796c8dcSSimon Schubert     {
32655796c8dcSSimon Schubert       if (!sec->ovly_mapped)
32665796c8dcSSimon Schubert 	error (_("Section %s is not mapped"), args);
32675796c8dcSSimon Schubert       sec->ovly_mapped = 0;
32685796c8dcSSimon Schubert       return;
32695796c8dcSSimon Schubert     }
32705796c8dcSSimon Schubert   error (_("No overlay section called %s"), args);
32715796c8dcSSimon Schubert }
32725796c8dcSSimon Schubert 
32735796c8dcSSimon Schubert /* Function: overlay_auto_command
32745796c8dcSSimon Schubert    A utility command to turn on overlay debugging.
32755796c8dcSSimon Schubert    Possibly this should be done via a set/show command.  */
32765796c8dcSSimon Schubert 
32775796c8dcSSimon Schubert static void
32785796c8dcSSimon Schubert overlay_auto_command (char *args, int from_tty)
32795796c8dcSSimon Schubert {
32805796c8dcSSimon Schubert   overlay_debugging = ovly_auto;
32815796c8dcSSimon Schubert   enable_overlay_breakpoints ();
32825796c8dcSSimon Schubert   if (info_verbose)
32835796c8dcSSimon Schubert     printf_unfiltered (_("Automatic overlay debugging enabled."));
32845796c8dcSSimon Schubert }
32855796c8dcSSimon Schubert 
32865796c8dcSSimon Schubert /* Function: overlay_manual_command
32875796c8dcSSimon Schubert    A utility command to turn on overlay debugging.
32885796c8dcSSimon Schubert    Possibly this should be done via a set/show command.  */
32895796c8dcSSimon Schubert 
32905796c8dcSSimon Schubert static void
32915796c8dcSSimon Schubert overlay_manual_command (char *args, int from_tty)
32925796c8dcSSimon Schubert {
32935796c8dcSSimon Schubert   overlay_debugging = ovly_on;
32945796c8dcSSimon Schubert   disable_overlay_breakpoints ();
32955796c8dcSSimon Schubert   if (info_verbose)
32965796c8dcSSimon Schubert     printf_unfiltered (_("Overlay debugging enabled."));
32975796c8dcSSimon Schubert }
32985796c8dcSSimon Schubert 
32995796c8dcSSimon Schubert /* Function: overlay_off_command
33005796c8dcSSimon Schubert    A utility command to turn on overlay debugging.
33015796c8dcSSimon Schubert    Possibly this should be done via a set/show command.  */
33025796c8dcSSimon Schubert 
33035796c8dcSSimon Schubert static void
33045796c8dcSSimon Schubert overlay_off_command (char *args, int from_tty)
33055796c8dcSSimon Schubert {
33065796c8dcSSimon Schubert   overlay_debugging = ovly_off;
33075796c8dcSSimon Schubert   disable_overlay_breakpoints ();
33085796c8dcSSimon Schubert   if (info_verbose)
33095796c8dcSSimon Schubert     printf_unfiltered (_("Overlay debugging disabled."));
33105796c8dcSSimon Schubert }
33115796c8dcSSimon Schubert 
33125796c8dcSSimon Schubert static void
33135796c8dcSSimon Schubert overlay_load_command (char *args, int from_tty)
33145796c8dcSSimon Schubert {
33155796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_current_arch ();
33165796c8dcSSimon Schubert 
33175796c8dcSSimon Schubert   if (gdbarch_overlay_update_p (gdbarch))
33185796c8dcSSimon Schubert     gdbarch_overlay_update (gdbarch, NULL);
33195796c8dcSSimon Schubert   else
33205796c8dcSSimon Schubert     error (_("This target does not know how to read its overlay state."));
33215796c8dcSSimon Schubert }
33225796c8dcSSimon Schubert 
33235796c8dcSSimon Schubert /* Function: overlay_command
3324c50c785cSJohn Marino    A place-holder for a mis-typed command.  */
33255796c8dcSSimon Schubert 
33265796c8dcSSimon Schubert /* Command list chain containing all defined "overlay" subcommands.  */
33275796c8dcSSimon Schubert struct cmd_list_element *overlaylist;
33285796c8dcSSimon Schubert 
33295796c8dcSSimon Schubert static void
33305796c8dcSSimon Schubert overlay_command (char *args, int from_tty)
33315796c8dcSSimon Schubert {
33325796c8dcSSimon Schubert   printf_unfiltered
33335796c8dcSSimon Schubert     ("\"overlay\" must be followed by the name of an overlay command.\n");
33345796c8dcSSimon Schubert   help_list (overlaylist, "overlay ", -1, gdb_stdout);
33355796c8dcSSimon Schubert }
33365796c8dcSSimon Schubert 
33375796c8dcSSimon Schubert 
33385796c8dcSSimon Schubert /* Target Overlays for the "Simplest" overlay manager:
33395796c8dcSSimon Schubert 
33405796c8dcSSimon Schubert    This is GDB's default target overlay layer.  It works with the
33415796c8dcSSimon Schubert    minimal overlay manager supplied as an example by Cygnus.  The
33425796c8dcSSimon Schubert    entry point is via a function pointer "gdbarch_overlay_update",
33435796c8dcSSimon Schubert    so targets that use a different runtime overlay manager can
33445796c8dcSSimon Schubert    substitute their own overlay_update function and take over the
33455796c8dcSSimon Schubert    function pointer.
33465796c8dcSSimon Schubert 
33475796c8dcSSimon Schubert    The overlay_update function pokes around in the target's data structures
33485796c8dcSSimon Schubert    to see what overlays are mapped, and updates GDB's overlay mapping with
33495796c8dcSSimon Schubert    this information.
33505796c8dcSSimon Schubert 
33515796c8dcSSimon Schubert    In this simple implementation, the target data structures are as follows:
33525796c8dcSSimon Schubert    unsigned _novlys;            /# number of overlay sections #/
33535796c8dcSSimon Schubert    unsigned _ovly_table[_novlys][4] = {
33545796c8dcSSimon Schubert    {VMA, SIZE, LMA, MAPPED},    /# one entry per overlay section #/
33555796c8dcSSimon Schubert    {..., ...,  ..., ...},
33565796c8dcSSimon Schubert    }
33575796c8dcSSimon Schubert    unsigned _novly_regions;     /# number of overlay regions #/
33585796c8dcSSimon Schubert    unsigned _ovly_region_table[_novly_regions][3] = {
33595796c8dcSSimon Schubert    {VMA, SIZE, MAPPED_TO_LMA},  /# one entry per overlay region #/
33605796c8dcSSimon Schubert    {..., ...,  ...},
33615796c8dcSSimon Schubert    }
33625796c8dcSSimon Schubert    These functions will attempt to update GDB's mappedness state in the
33635796c8dcSSimon Schubert    symbol section table, based on the target's mappedness state.
33645796c8dcSSimon Schubert 
33655796c8dcSSimon Schubert    To do this, we keep a cached copy of the target's _ovly_table, and
33665796c8dcSSimon Schubert    attempt to detect when the cached copy is invalidated.  The main
33675796c8dcSSimon Schubert    entry point is "simple_overlay_update(SECT), which looks up SECT in
33685796c8dcSSimon Schubert    the cached table and re-reads only the entry for that section from
3369c50c785cSJohn Marino    the target (whenever possible).  */
33705796c8dcSSimon Schubert 
33715796c8dcSSimon Schubert /* Cached, dynamically allocated copies of the target data structures: */
33725796c8dcSSimon Schubert static unsigned (*cache_ovly_table)[4] = 0;
33735796c8dcSSimon Schubert static unsigned cache_novlys = 0;
33745796c8dcSSimon Schubert static CORE_ADDR cache_ovly_table_base = 0;
33755796c8dcSSimon Schubert enum ovly_index
33765796c8dcSSimon Schubert   {
33775796c8dcSSimon Schubert     VMA, SIZE, LMA, MAPPED
33785796c8dcSSimon Schubert   };
33795796c8dcSSimon Schubert 
3380c50c785cSJohn Marino /* Throw away the cached copy of _ovly_table.  */
33815796c8dcSSimon Schubert static void
33825796c8dcSSimon Schubert simple_free_overlay_table (void)
33835796c8dcSSimon Schubert {
33845796c8dcSSimon Schubert   if (cache_ovly_table)
33855796c8dcSSimon Schubert     xfree (cache_ovly_table);
33865796c8dcSSimon Schubert   cache_novlys = 0;
33875796c8dcSSimon Schubert   cache_ovly_table = NULL;
33885796c8dcSSimon Schubert   cache_ovly_table_base = 0;
33895796c8dcSSimon Schubert }
33905796c8dcSSimon Schubert 
33915796c8dcSSimon Schubert /* Read an array of ints of size SIZE from the target into a local buffer.
3392c50c785cSJohn Marino    Convert to host order.  int LEN is number of ints.  */
33935796c8dcSSimon Schubert static void
33945796c8dcSSimon Schubert read_target_long_array (CORE_ADDR memaddr, unsigned int *myaddr,
33955796c8dcSSimon Schubert 			int len, int size, enum bfd_endian byte_order)
33965796c8dcSSimon Schubert {
33975796c8dcSSimon Schubert   /* FIXME (alloca): Not safe if array is very large.  */
33985796c8dcSSimon Schubert   gdb_byte *buf = alloca (len * size);
33995796c8dcSSimon Schubert   int i;
34005796c8dcSSimon Schubert 
34015796c8dcSSimon Schubert   read_memory (memaddr, buf, len * size);
34025796c8dcSSimon Schubert   for (i = 0; i < len; i++)
34035796c8dcSSimon Schubert     myaddr[i] = extract_unsigned_integer (size * i + buf, size, byte_order);
34045796c8dcSSimon Schubert }
34055796c8dcSSimon Schubert 
34065796c8dcSSimon Schubert /* Find and grab a copy of the target _ovly_table
3407c50c785cSJohn Marino    (and _novlys, which is needed for the table's size).  */
34085796c8dcSSimon Schubert static int
34095796c8dcSSimon Schubert simple_read_overlay_table (void)
34105796c8dcSSimon Schubert {
34115796c8dcSSimon Schubert   struct minimal_symbol *novlys_msym, *ovly_table_msym;
34125796c8dcSSimon Schubert   struct gdbarch *gdbarch;
34135796c8dcSSimon Schubert   int word_size;
34145796c8dcSSimon Schubert   enum bfd_endian byte_order;
34155796c8dcSSimon Schubert 
34165796c8dcSSimon Schubert   simple_free_overlay_table ();
34175796c8dcSSimon Schubert   novlys_msym = lookup_minimal_symbol ("_novlys", NULL, NULL);
34185796c8dcSSimon Schubert   if (! novlys_msym)
34195796c8dcSSimon Schubert     {
34205796c8dcSSimon Schubert       error (_("Error reading inferior's overlay table: "
34215796c8dcSSimon Schubert              "couldn't find `_novlys' variable\n"
34225796c8dcSSimon Schubert              "in inferior.  Use `overlay manual' mode."));
34235796c8dcSSimon Schubert       return 0;
34245796c8dcSSimon Schubert     }
34255796c8dcSSimon Schubert 
34265796c8dcSSimon Schubert   ovly_table_msym = lookup_minimal_symbol ("_ovly_table", NULL, NULL);
34275796c8dcSSimon Schubert   if (! ovly_table_msym)
34285796c8dcSSimon Schubert     {
34295796c8dcSSimon Schubert       error (_("Error reading inferior's overlay table: couldn't find "
34305796c8dcSSimon Schubert              "`_ovly_table' array\n"
34315796c8dcSSimon Schubert              "in inferior.  Use `overlay manual' mode."));
34325796c8dcSSimon Schubert       return 0;
34335796c8dcSSimon Schubert     }
34345796c8dcSSimon Schubert 
34355796c8dcSSimon Schubert   gdbarch = get_objfile_arch (msymbol_objfile (ovly_table_msym));
34365796c8dcSSimon Schubert   word_size = gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT;
34375796c8dcSSimon Schubert   byte_order = gdbarch_byte_order (gdbarch);
34385796c8dcSSimon Schubert 
34395796c8dcSSimon Schubert   cache_novlys = read_memory_integer (SYMBOL_VALUE_ADDRESS (novlys_msym),
34405796c8dcSSimon Schubert 				      4, byte_order);
34415796c8dcSSimon Schubert   cache_ovly_table
34425796c8dcSSimon Schubert     = (void *) xmalloc (cache_novlys * sizeof (*cache_ovly_table));
34435796c8dcSSimon Schubert   cache_ovly_table_base = SYMBOL_VALUE_ADDRESS (ovly_table_msym);
34445796c8dcSSimon Schubert   read_target_long_array (cache_ovly_table_base,
34455796c8dcSSimon Schubert                           (unsigned int *) cache_ovly_table,
34465796c8dcSSimon Schubert                           cache_novlys * 4, word_size, byte_order);
34475796c8dcSSimon Schubert 
34485796c8dcSSimon Schubert   return 1;			/* SUCCESS */
34495796c8dcSSimon Schubert }
34505796c8dcSSimon Schubert 
34515796c8dcSSimon Schubert /* Function: simple_overlay_update_1
34525796c8dcSSimon Schubert    A helper function for simple_overlay_update.  Assuming a cached copy
34535796c8dcSSimon Schubert    of _ovly_table exists, look through it to find an entry whose vma,
34545796c8dcSSimon Schubert    lma and size match those of OSECT.  Re-read the entry and make sure
34555796c8dcSSimon Schubert    it still matches OSECT (else the table may no longer be valid).
34565796c8dcSSimon Schubert    Set OSECT's mapped state to match the entry.  Return: 1 for
34575796c8dcSSimon Schubert    success, 0 for failure.  */
34585796c8dcSSimon Schubert 
34595796c8dcSSimon Schubert static int
34605796c8dcSSimon Schubert simple_overlay_update_1 (struct obj_section *osect)
34615796c8dcSSimon Schubert {
34625796c8dcSSimon Schubert   int i, size;
34635796c8dcSSimon Schubert   bfd *obfd = osect->objfile->obfd;
34645796c8dcSSimon Schubert   asection *bsect = osect->the_bfd_section;
34655796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_objfile_arch (osect->objfile);
34665796c8dcSSimon Schubert   int word_size = gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT;
34675796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
34685796c8dcSSimon Schubert 
34695796c8dcSSimon Schubert   size = bfd_get_section_size (osect->the_bfd_section);
34705796c8dcSSimon Schubert   for (i = 0; i < cache_novlys; i++)
34715796c8dcSSimon Schubert     if (cache_ovly_table[i][VMA] == bfd_section_vma (obfd, bsect)
34725796c8dcSSimon Schubert 	&& cache_ovly_table[i][LMA] == bfd_section_lma (obfd, bsect)
34735796c8dcSSimon Schubert 	/* && cache_ovly_table[i][SIZE] == size */ )
34745796c8dcSSimon Schubert       {
34755796c8dcSSimon Schubert 	read_target_long_array (cache_ovly_table_base + i * word_size,
34765796c8dcSSimon Schubert 				(unsigned int *) cache_ovly_table[i],
34775796c8dcSSimon Schubert 				4, word_size, byte_order);
34785796c8dcSSimon Schubert 	if (cache_ovly_table[i][VMA] == bfd_section_vma (obfd, bsect)
34795796c8dcSSimon Schubert 	    && cache_ovly_table[i][LMA] == bfd_section_lma (obfd, bsect)
34805796c8dcSSimon Schubert 	    /* && cache_ovly_table[i][SIZE] == size */ )
34815796c8dcSSimon Schubert 	  {
34825796c8dcSSimon Schubert 	    osect->ovly_mapped = cache_ovly_table[i][MAPPED];
34835796c8dcSSimon Schubert 	    return 1;
34845796c8dcSSimon Schubert 	  }
34855796c8dcSSimon Schubert 	else	/* Warning!  Warning!  Target's ovly table has changed!  */
34865796c8dcSSimon Schubert 	  return 0;
34875796c8dcSSimon Schubert       }
34885796c8dcSSimon Schubert   return 0;
34895796c8dcSSimon Schubert }
34905796c8dcSSimon Schubert 
34915796c8dcSSimon Schubert /* Function: simple_overlay_update
34925796c8dcSSimon Schubert    If OSECT is NULL, then update all sections' mapped state
34935796c8dcSSimon Schubert    (after re-reading the entire target _ovly_table).
34945796c8dcSSimon Schubert    If OSECT is non-NULL, then try to find a matching entry in the
34955796c8dcSSimon Schubert    cached ovly_table and update only OSECT's mapped state.
34965796c8dcSSimon Schubert    If a cached entry can't be found or the cache isn't valid, then
34975796c8dcSSimon Schubert    re-read the entire cache, and go ahead and update all sections.  */
34985796c8dcSSimon Schubert 
34995796c8dcSSimon Schubert void
35005796c8dcSSimon Schubert simple_overlay_update (struct obj_section *osect)
35015796c8dcSSimon Schubert {
35025796c8dcSSimon Schubert   struct objfile *objfile;
35035796c8dcSSimon Schubert 
35045796c8dcSSimon Schubert   /* Were we given an osect to look up?  NULL means do all of them.  */
35055796c8dcSSimon Schubert   if (osect)
35065796c8dcSSimon Schubert     /* Have we got a cached copy of the target's overlay table?  */
35075796c8dcSSimon Schubert     if (cache_ovly_table != NULL)
3508c50c785cSJohn Marino       {
3509c50c785cSJohn Marino 	/* Does its cached location match what's currently in the
3510c50c785cSJohn Marino 	   symtab?  */
3511c50c785cSJohn Marino 	struct minimal_symbol *minsym
3512c50c785cSJohn Marino 	  = lookup_minimal_symbol ("_ovly_table", NULL, NULL);
3513c50c785cSJohn Marino 
3514c50c785cSJohn Marino 	if (minsym == NULL)
3515c50c785cSJohn Marino 	  error (_("Error reading inferior's overlay table: couldn't "
3516c50c785cSJohn Marino 		   "find `_ovly_table' array\n"
3517c50c785cSJohn Marino 		   "in inferior.  Use `overlay manual' mode."));
3518c50c785cSJohn Marino 
3519c50c785cSJohn Marino 	if (cache_ovly_table_base == SYMBOL_VALUE_ADDRESS (minsym))
3520c50c785cSJohn Marino 	  /* Then go ahead and try to look up this single section in
3521c50c785cSJohn Marino 	     the cache.  */
35225796c8dcSSimon Schubert 	  if (simple_overlay_update_1 (osect))
35235796c8dcSSimon Schubert 	    /* Found it!  We're done.  */
35245796c8dcSSimon Schubert 	    return;
3525c50c785cSJohn Marino       }
35265796c8dcSSimon Schubert 
35275796c8dcSSimon Schubert   /* Cached table no good: need to read the entire table anew.
35285796c8dcSSimon Schubert      Or else we want all the sections, in which case it's actually
35295796c8dcSSimon Schubert      more efficient to read the whole table in one block anyway.  */
35305796c8dcSSimon Schubert 
35315796c8dcSSimon Schubert   if (! simple_read_overlay_table ())
35325796c8dcSSimon Schubert     return;
35335796c8dcSSimon Schubert 
35345796c8dcSSimon Schubert   /* Now may as well update all sections, even if only one was requested.  */
35355796c8dcSSimon Schubert   ALL_OBJSECTIONS (objfile, osect)
35365796c8dcSSimon Schubert     if (section_is_overlay (osect))
35375796c8dcSSimon Schubert     {
35385796c8dcSSimon Schubert       int i, size;
35395796c8dcSSimon Schubert       bfd *obfd = osect->objfile->obfd;
35405796c8dcSSimon Schubert       asection *bsect = osect->the_bfd_section;
35415796c8dcSSimon Schubert 
35425796c8dcSSimon Schubert       size = bfd_get_section_size (bsect);
35435796c8dcSSimon Schubert       for (i = 0; i < cache_novlys; i++)
35445796c8dcSSimon Schubert 	if (cache_ovly_table[i][VMA] == bfd_section_vma (obfd, bsect)
35455796c8dcSSimon Schubert 	    && cache_ovly_table[i][LMA] == bfd_section_lma (obfd, bsect)
35465796c8dcSSimon Schubert 	    /* && cache_ovly_table[i][SIZE] == size */ )
3547c50c785cSJohn Marino 	  { /* obj_section matches i'th entry in ovly_table.  */
35485796c8dcSSimon Schubert 	    osect->ovly_mapped = cache_ovly_table[i][MAPPED];
3549c50c785cSJohn Marino 	    break;		/* finished with inner for loop: break out.  */
35505796c8dcSSimon Schubert 	  }
35515796c8dcSSimon Schubert     }
35525796c8dcSSimon Schubert }
35535796c8dcSSimon Schubert 
35545796c8dcSSimon Schubert /* Set the output sections and output offsets for section SECTP in
35555796c8dcSSimon Schubert    ABFD.  The relocation code in BFD will read these offsets, so we
35565796c8dcSSimon Schubert    need to be sure they're initialized.  We map each section to itself,
35575796c8dcSSimon Schubert    with no offset; this means that SECTP->vma will be honored.  */
35585796c8dcSSimon Schubert 
35595796c8dcSSimon Schubert static void
35605796c8dcSSimon Schubert symfile_dummy_outputs (bfd *abfd, asection *sectp, void *dummy)
35615796c8dcSSimon Schubert {
35625796c8dcSSimon Schubert   sectp->output_section = sectp;
35635796c8dcSSimon Schubert   sectp->output_offset = 0;
35645796c8dcSSimon Schubert }
35655796c8dcSSimon Schubert 
3566cf7f2e2dSJohn Marino /* Default implementation for sym_relocate.  */
3567cf7f2e2dSJohn Marino 
3568cf7f2e2dSJohn Marino 
3569cf7f2e2dSJohn Marino bfd_byte *
3570cf7f2e2dSJohn Marino default_symfile_relocate (struct objfile *objfile, asection *sectp,
3571cf7f2e2dSJohn Marino                           bfd_byte *buf)
3572cf7f2e2dSJohn Marino {
3573cf7f2e2dSJohn Marino   bfd *abfd = objfile->obfd;
3574cf7f2e2dSJohn Marino 
3575cf7f2e2dSJohn Marino   /* We're only interested in sections with relocation
3576cf7f2e2dSJohn Marino      information.  */
3577cf7f2e2dSJohn Marino   if ((sectp->flags & SEC_RELOC) == 0)
3578cf7f2e2dSJohn Marino     return NULL;
3579cf7f2e2dSJohn Marino 
3580cf7f2e2dSJohn Marino   /* We will handle section offsets properly elsewhere, so relocate as if
3581cf7f2e2dSJohn Marino      all sections begin at 0.  */
3582cf7f2e2dSJohn Marino   bfd_map_over_sections (abfd, symfile_dummy_outputs, NULL);
3583cf7f2e2dSJohn Marino 
3584cf7f2e2dSJohn Marino   return bfd_simple_get_relocated_section_contents (abfd, sectp, buf, NULL);
3585cf7f2e2dSJohn Marino }
3586cf7f2e2dSJohn Marino 
35875796c8dcSSimon Schubert /* Relocate the contents of a debug section SECTP in ABFD.  The
35885796c8dcSSimon Schubert    contents are stored in BUF if it is non-NULL, or returned in a
35895796c8dcSSimon Schubert    malloc'd buffer otherwise.
35905796c8dcSSimon Schubert 
35915796c8dcSSimon Schubert    For some platforms and debug info formats, shared libraries contain
35925796c8dcSSimon Schubert    relocations against the debug sections (particularly for DWARF-2;
35935796c8dcSSimon Schubert    one affected platform is PowerPC GNU/Linux, although it depends on
35945796c8dcSSimon Schubert    the version of the linker in use).  Also, ELF object files naturally
35955796c8dcSSimon Schubert    have unresolved relocations for their debug sections.  We need to apply
35965796c8dcSSimon Schubert    the relocations in order to get the locations of symbols correct.
35975796c8dcSSimon Schubert    Another example that may require relocation processing, is the
35985796c8dcSSimon Schubert    DWARF-2 .eh_frame section in .o files, although it isn't strictly a
35995796c8dcSSimon Schubert    debug section.  */
36005796c8dcSSimon Schubert 
36015796c8dcSSimon Schubert bfd_byte *
3602cf7f2e2dSJohn Marino symfile_relocate_debug_section (struct objfile *objfile,
3603cf7f2e2dSJohn Marino                                 asection *sectp, bfd_byte *buf)
36045796c8dcSSimon Schubert {
3605cf7f2e2dSJohn Marino   gdb_assert (objfile->sf->sym_relocate);
36065796c8dcSSimon Schubert 
3607cf7f2e2dSJohn Marino   return (*objfile->sf->sym_relocate) (objfile, sectp, buf);
36085796c8dcSSimon Schubert }
36095796c8dcSSimon Schubert 
36105796c8dcSSimon Schubert struct symfile_segment_data *
36115796c8dcSSimon Schubert get_symfile_segment_data (bfd *abfd)
36125796c8dcSSimon Schubert {
3613c50c785cSJohn Marino   const struct sym_fns *sf = find_sym_fns (abfd);
36145796c8dcSSimon Schubert 
36155796c8dcSSimon Schubert   if (sf == NULL)
36165796c8dcSSimon Schubert     return NULL;
36175796c8dcSSimon Schubert 
36185796c8dcSSimon Schubert   return sf->sym_segments (abfd);
36195796c8dcSSimon Schubert }
36205796c8dcSSimon Schubert 
36215796c8dcSSimon Schubert void
36225796c8dcSSimon Schubert free_symfile_segment_data (struct symfile_segment_data *data)
36235796c8dcSSimon Schubert {
36245796c8dcSSimon Schubert   xfree (data->segment_bases);
36255796c8dcSSimon Schubert   xfree (data->segment_sizes);
36265796c8dcSSimon Schubert   xfree (data->segment_info);
36275796c8dcSSimon Schubert   xfree (data);
36285796c8dcSSimon Schubert }
36295796c8dcSSimon Schubert 
36305796c8dcSSimon Schubert 
36315796c8dcSSimon Schubert /* Given:
36325796c8dcSSimon Schubert    - DATA, containing segment addresses from the object file ABFD, and
36335796c8dcSSimon Schubert      the mapping from ABFD's sections onto the segments that own them,
36345796c8dcSSimon Schubert      and
36355796c8dcSSimon Schubert    - SEGMENT_BASES[0 .. NUM_SEGMENT_BASES - 1], holding the actual
36365796c8dcSSimon Schubert      segment addresses reported by the target,
36375796c8dcSSimon Schubert    store the appropriate offsets for each section in OFFSETS.
36385796c8dcSSimon Schubert 
36395796c8dcSSimon Schubert    If there are fewer entries in SEGMENT_BASES than there are segments
36405796c8dcSSimon Schubert    in DATA, then apply SEGMENT_BASES' last entry to all the segments.
36415796c8dcSSimon Schubert 
36425796c8dcSSimon Schubert    If there are more entries, then ignore the extra.  The target may
36435796c8dcSSimon Schubert    not be able to distinguish between an empty data segment and a
36445796c8dcSSimon Schubert    missing data segment; a missing text segment is less plausible.  */
36455796c8dcSSimon Schubert int
36465796c8dcSSimon Schubert symfile_map_offsets_to_segments (bfd *abfd, struct symfile_segment_data *data,
36475796c8dcSSimon Schubert 				 struct section_offsets *offsets,
36485796c8dcSSimon Schubert 				 int num_segment_bases,
36495796c8dcSSimon Schubert 				 const CORE_ADDR *segment_bases)
36505796c8dcSSimon Schubert {
36515796c8dcSSimon Schubert   int i;
36525796c8dcSSimon Schubert   asection *sect;
36535796c8dcSSimon Schubert 
36545796c8dcSSimon Schubert   /* It doesn't make sense to call this function unless you have some
36555796c8dcSSimon Schubert      segment base addresses.  */
3656cf7f2e2dSJohn Marino   gdb_assert (num_segment_bases > 0);
36575796c8dcSSimon Schubert 
36585796c8dcSSimon Schubert   /* If we do not have segment mappings for the object file, we
36595796c8dcSSimon Schubert      can not relocate it by segments.  */
36605796c8dcSSimon Schubert   gdb_assert (data != NULL);
36615796c8dcSSimon Schubert   gdb_assert (data->num_segments > 0);
36625796c8dcSSimon Schubert 
36635796c8dcSSimon Schubert   for (i = 0, sect = abfd->sections; sect != NULL; i++, sect = sect->next)
36645796c8dcSSimon Schubert     {
36655796c8dcSSimon Schubert       int which = data->segment_info[i];
36665796c8dcSSimon Schubert 
36675796c8dcSSimon Schubert       gdb_assert (0 <= which && which <= data->num_segments);
36685796c8dcSSimon Schubert 
36695796c8dcSSimon Schubert       /* Don't bother computing offsets for sections that aren't
36705796c8dcSSimon Schubert          loaded as part of any segment.  */
36715796c8dcSSimon Schubert       if (! which)
36725796c8dcSSimon Schubert         continue;
36735796c8dcSSimon Schubert 
36745796c8dcSSimon Schubert       /* Use the last SEGMENT_BASES entry as the address of any extra
36755796c8dcSSimon Schubert          segments mentioned in DATA->segment_info.  */
36765796c8dcSSimon Schubert       if (which > num_segment_bases)
36775796c8dcSSimon Schubert         which = num_segment_bases;
36785796c8dcSSimon Schubert 
36795796c8dcSSimon Schubert       offsets->offsets[i] = (segment_bases[which - 1]
36805796c8dcSSimon Schubert                              - data->segment_bases[which - 1]);
36815796c8dcSSimon Schubert     }
36825796c8dcSSimon Schubert 
36835796c8dcSSimon Schubert   return 1;
36845796c8dcSSimon Schubert }
36855796c8dcSSimon Schubert 
36865796c8dcSSimon Schubert static void
36875796c8dcSSimon Schubert symfile_find_segment_sections (struct objfile *objfile)
36885796c8dcSSimon Schubert {
36895796c8dcSSimon Schubert   bfd *abfd = objfile->obfd;
36905796c8dcSSimon Schubert   int i;
36915796c8dcSSimon Schubert   asection *sect;
36925796c8dcSSimon Schubert   struct symfile_segment_data *data;
36935796c8dcSSimon Schubert 
36945796c8dcSSimon Schubert   data = get_symfile_segment_data (objfile->obfd);
36955796c8dcSSimon Schubert   if (data == NULL)
36965796c8dcSSimon Schubert     return;
36975796c8dcSSimon Schubert 
36985796c8dcSSimon Schubert   if (data->num_segments != 1 && data->num_segments != 2)
36995796c8dcSSimon Schubert     {
37005796c8dcSSimon Schubert       free_symfile_segment_data (data);
37015796c8dcSSimon Schubert       return;
37025796c8dcSSimon Schubert     }
37035796c8dcSSimon Schubert 
37045796c8dcSSimon Schubert   for (i = 0, sect = abfd->sections; sect != NULL; i++, sect = sect->next)
37055796c8dcSSimon Schubert     {
37065796c8dcSSimon Schubert       int which = data->segment_info[i];
37075796c8dcSSimon Schubert 
37085796c8dcSSimon Schubert       if (which == 1)
37095796c8dcSSimon Schubert 	{
37105796c8dcSSimon Schubert 	  if (objfile->sect_index_text == -1)
37115796c8dcSSimon Schubert 	    objfile->sect_index_text = sect->index;
37125796c8dcSSimon Schubert 
37135796c8dcSSimon Schubert 	  if (objfile->sect_index_rodata == -1)
37145796c8dcSSimon Schubert 	    objfile->sect_index_rodata = sect->index;
37155796c8dcSSimon Schubert 	}
37165796c8dcSSimon Schubert       else if (which == 2)
37175796c8dcSSimon Schubert 	{
37185796c8dcSSimon Schubert 	  if (objfile->sect_index_data == -1)
37195796c8dcSSimon Schubert 	    objfile->sect_index_data = sect->index;
37205796c8dcSSimon Schubert 
37215796c8dcSSimon Schubert 	  if (objfile->sect_index_bss == -1)
37225796c8dcSSimon Schubert 	    objfile->sect_index_bss = sect->index;
37235796c8dcSSimon Schubert 	}
37245796c8dcSSimon Schubert     }
37255796c8dcSSimon Schubert 
37265796c8dcSSimon Schubert   free_symfile_segment_data (data);
37275796c8dcSSimon Schubert }
37285796c8dcSSimon Schubert 
37295796c8dcSSimon Schubert void
37305796c8dcSSimon Schubert _initialize_symfile (void)
37315796c8dcSSimon Schubert {
37325796c8dcSSimon Schubert   struct cmd_list_element *c;
37335796c8dcSSimon Schubert 
37345796c8dcSSimon Schubert   c = add_cmd ("symbol-file", class_files, symbol_file_command, _("\
37355796c8dcSSimon Schubert Load symbol table from executable file FILE.\n\
37365796c8dcSSimon Schubert The `file' command can also load symbol tables, as well as setting the file\n\
37375796c8dcSSimon Schubert to execute."), &cmdlist);
37385796c8dcSSimon Schubert   set_cmd_completer (c, filename_completer);
37395796c8dcSSimon Schubert 
37405796c8dcSSimon Schubert   c = add_cmd ("add-symbol-file", class_files, add_symbol_file_command, _("\
37415796c8dcSSimon Schubert Load symbols from FILE, assuming FILE has been dynamically loaded.\n\
3742c50c785cSJohn Marino Usage: add-symbol-file FILE ADDR [-s <SECT> <SECT_ADDR> -s <SECT> <SECT_ADDR>\
3743c50c785cSJohn Marino  ...]\nADDR is the starting address of the file's text.\n\
37445796c8dcSSimon Schubert The optional arguments are section-name section-address pairs and\n\
37455796c8dcSSimon Schubert should be specified if the data and bss segments are not contiguous\n\
37465796c8dcSSimon Schubert with the text.  SECT is a section name to be loaded at SECT_ADDR."),
37475796c8dcSSimon Schubert 	       &cmdlist);
37485796c8dcSSimon Schubert   set_cmd_completer (c, filename_completer);
37495796c8dcSSimon Schubert 
37505796c8dcSSimon Schubert   c = add_cmd ("load", class_files, load_command, _("\
37515796c8dcSSimon Schubert Dynamically load FILE into the running program, and record its symbols\n\
37525796c8dcSSimon Schubert for access from GDB.\n\
37535796c8dcSSimon Schubert A load OFFSET may also be given."), &cmdlist);
37545796c8dcSSimon Schubert   set_cmd_completer (c, filename_completer);
37555796c8dcSSimon Schubert 
37565796c8dcSSimon Schubert   add_setshow_boolean_cmd ("symbol-reloading", class_support,
37575796c8dcSSimon Schubert 			   &symbol_reloading, _("\
37585796c8dcSSimon Schubert Set dynamic symbol table reloading multiple times in one run."), _("\
37595796c8dcSSimon Schubert Show dynamic symbol table reloading multiple times in one run."), NULL,
37605796c8dcSSimon Schubert 			   NULL,
37615796c8dcSSimon Schubert 			   show_symbol_reloading,
37625796c8dcSSimon Schubert 			   &setlist, &showlist);
37635796c8dcSSimon Schubert 
37645796c8dcSSimon Schubert   add_prefix_cmd ("overlay", class_support, overlay_command,
37655796c8dcSSimon Schubert 		  _("Commands for debugging overlays."), &overlaylist,
37665796c8dcSSimon Schubert 		  "overlay ", 0, &cmdlist);
37675796c8dcSSimon Schubert 
37685796c8dcSSimon Schubert   add_com_alias ("ovly", "overlay", class_alias, 1);
37695796c8dcSSimon Schubert   add_com_alias ("ov", "overlay", class_alias, 1);
37705796c8dcSSimon Schubert 
37715796c8dcSSimon Schubert   add_cmd ("map-overlay", class_support, map_overlay_command,
37725796c8dcSSimon Schubert 	   _("Assert that an overlay section is mapped."), &overlaylist);
37735796c8dcSSimon Schubert 
37745796c8dcSSimon Schubert   add_cmd ("unmap-overlay", class_support, unmap_overlay_command,
37755796c8dcSSimon Schubert 	   _("Assert that an overlay section is unmapped."), &overlaylist);
37765796c8dcSSimon Schubert 
37775796c8dcSSimon Schubert   add_cmd ("list-overlays", class_support, list_overlays_command,
37785796c8dcSSimon Schubert 	   _("List mappings of overlay sections."), &overlaylist);
37795796c8dcSSimon Schubert 
37805796c8dcSSimon Schubert   add_cmd ("manual", class_support, overlay_manual_command,
37815796c8dcSSimon Schubert 	   _("Enable overlay debugging."), &overlaylist);
37825796c8dcSSimon Schubert   add_cmd ("off", class_support, overlay_off_command,
37835796c8dcSSimon Schubert 	   _("Disable overlay debugging."), &overlaylist);
37845796c8dcSSimon Schubert   add_cmd ("auto", class_support, overlay_auto_command,
37855796c8dcSSimon Schubert 	   _("Enable automatic overlay debugging."), &overlaylist);
37865796c8dcSSimon Schubert   add_cmd ("load-target", class_support, overlay_load_command,
37875796c8dcSSimon Schubert 	   _("Read the overlay mapping state from the target."), &overlaylist);
37885796c8dcSSimon Schubert 
37895796c8dcSSimon Schubert   /* Filename extension to source language lookup table: */
37905796c8dcSSimon Schubert   init_filename_language_table ();
37915796c8dcSSimon Schubert   add_setshow_string_noescape_cmd ("extension-language", class_files,
37925796c8dcSSimon Schubert 				   &ext_args, _("\
37935796c8dcSSimon Schubert Set mapping between filename extension and source language."), _("\
37945796c8dcSSimon Schubert Show mapping between filename extension and source language."), _("\
37955796c8dcSSimon Schubert Usage: set extension-language .foo bar"),
37965796c8dcSSimon Schubert 				   set_ext_lang_command,
37975796c8dcSSimon Schubert 				   show_ext_args,
37985796c8dcSSimon Schubert 				   &setlist, &showlist);
37995796c8dcSSimon Schubert 
38005796c8dcSSimon Schubert   add_info ("extensions", info_ext_lang_command,
38015796c8dcSSimon Schubert 	    _("All filename extensions associated with a source language."));
38025796c8dcSSimon Schubert 
38035796c8dcSSimon Schubert   add_setshow_optional_filename_cmd ("debug-file-directory", class_support,
38045796c8dcSSimon Schubert 				     &debug_file_directory, _("\
3805cf7f2e2dSJohn Marino Set the directories where separate debug symbols are searched for."), _("\
3806cf7f2e2dSJohn Marino Show the directories where separate debug symbols are searched for."), _("\
38075796c8dcSSimon Schubert Separate debug symbols are first searched for in the same\n\
38085796c8dcSSimon Schubert directory as the binary, then in the `" DEBUG_SUBDIRECTORY "' subdirectory,\n\
38095796c8dcSSimon Schubert and lastly at the path of the directory of the binary with\n\
3810cf7f2e2dSJohn Marino each global debug-file-directory component prepended."),
38115796c8dcSSimon Schubert 				     NULL,
38125796c8dcSSimon Schubert 				     show_debug_file_directory,
38135796c8dcSSimon Schubert 				     &setlist, &showlist);
38145796c8dcSSimon Schubert }
3815