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