xref: /dragonfly/contrib/gdb-7/gdb/symfile.c (revision ef5ccd6c)
15796c8dcSSimon Schubert /* Generic symbol file reading for the GNU debugger, GDB.
25796c8dcSSimon Schubert 
3*ef5ccd6cSJohn Marino    Copyright (C) 1990-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    Contributed by Cygnus Support, using pieces from other GDB modules.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This file is part of GDB.
85796c8dcSSimon Schubert 
95796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
105796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
115796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
125796c8dcSSimon Schubert    (at your option) any later version.
135796c8dcSSimon Schubert 
145796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
155796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
165796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
175796c8dcSSimon Schubert    GNU General Public License for more details.
185796c8dcSSimon Schubert 
195796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
205796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
215796c8dcSSimon Schubert 
225796c8dcSSimon Schubert #include "defs.h"
235796c8dcSSimon Schubert #include "arch-utils.h"
245796c8dcSSimon Schubert #include "bfdlink.h"
255796c8dcSSimon Schubert #include "symtab.h"
265796c8dcSSimon Schubert #include "gdbtypes.h"
275796c8dcSSimon Schubert #include "gdbcore.h"
285796c8dcSSimon Schubert #include "frame.h"
295796c8dcSSimon Schubert #include "target.h"
305796c8dcSSimon Schubert #include "value.h"
315796c8dcSSimon Schubert #include "symfile.h"
325796c8dcSSimon Schubert #include "objfiles.h"
335796c8dcSSimon Schubert #include "source.h"
345796c8dcSSimon Schubert #include "gdbcmd.h"
355796c8dcSSimon Schubert #include "breakpoint.h"
365796c8dcSSimon Schubert #include "language.h"
375796c8dcSSimon Schubert #include "complaints.h"
385796c8dcSSimon Schubert #include "demangle.h"
395796c8dcSSimon Schubert #include "inferior.h"
405796c8dcSSimon Schubert #include "regcache.h"
415796c8dcSSimon Schubert #include "filenames.h"		/* for DOSish file names */
425796c8dcSSimon Schubert #include "gdb-stabs.h"
435796c8dcSSimon Schubert #include "gdb_obstack.h"
445796c8dcSSimon Schubert #include "completer.h"
455796c8dcSSimon Schubert #include "bcache.h"
465796c8dcSSimon Schubert #include "hashtab.h"
475796c8dcSSimon Schubert #include "readline/readline.h"
485796c8dcSSimon Schubert #include "gdb_assert.h"
495796c8dcSSimon Schubert #include "block.h"
505796c8dcSSimon Schubert #include "observer.h"
515796c8dcSSimon Schubert #include "exec.h"
525796c8dcSSimon Schubert #include "parser-defs.h"
535796c8dcSSimon Schubert #include "varobj.h"
545796c8dcSSimon Schubert #include "elf-bfd.h"
555796c8dcSSimon Schubert #include "solib.h"
565796c8dcSSimon Schubert #include "remote.h"
57a45ae5f8SJohn Marino #include "stack.h"
58*ef5ccd6cSJohn Marino #include "gdb_bfd.h"
59*ef5ccd6cSJohn Marino #include "cli/cli-utils.h"
605796c8dcSSimon Schubert 
615796c8dcSSimon Schubert #include <sys/types.h>
625796c8dcSSimon Schubert #include <fcntl.h>
635796c8dcSSimon Schubert #include "gdb_string.h"
645796c8dcSSimon Schubert #include "gdb_stat.h"
655796c8dcSSimon Schubert #include <ctype.h>
665796c8dcSSimon Schubert #include <time.h>
675796c8dcSSimon Schubert #include <sys/time.h>
685796c8dcSSimon Schubert 
69cf7f2e2dSJohn Marino #include "psymtab.h"
705796c8dcSSimon Schubert 
71c50c785cSJohn Marino int (*deprecated_ui_load_progress_hook) (const char *section,
72c50c785cSJohn Marino 					 unsigned long num);
735796c8dcSSimon Schubert void (*deprecated_show_load_progress) (const char *section,
745796c8dcSSimon Schubert 			    unsigned long section_sent,
755796c8dcSSimon Schubert 			    unsigned long section_size,
765796c8dcSSimon Schubert 			    unsigned long total_sent,
775796c8dcSSimon Schubert 			    unsigned long total_size);
785796c8dcSSimon Schubert void (*deprecated_pre_add_symbol_hook) (const char *);
795796c8dcSSimon Schubert void (*deprecated_post_add_symbol_hook) (void);
805796c8dcSSimon Schubert 
815796c8dcSSimon Schubert static void clear_symtab_users_cleanup (void *ignore);
825796c8dcSSimon Schubert 
83c50c785cSJohn Marino /* Global variables owned by this file.  */
84c50c785cSJohn Marino int readnow_symbol_files;	/* Read full symbols immediately.  */
855796c8dcSSimon Schubert 
86c50c785cSJohn Marino /* Functions this file defines.  */
875796c8dcSSimon Schubert 
885796c8dcSSimon Schubert static void load_command (char *, int);
895796c8dcSSimon Schubert 
905796c8dcSSimon Schubert static void symbol_file_add_main_1 (char *args, int from_tty, int flags);
915796c8dcSSimon Schubert 
925796c8dcSSimon Schubert static void add_symbol_file_command (char *, int);
935796c8dcSSimon Schubert 
945796c8dcSSimon Schubert bfd *symfile_bfd_open (char *);
955796c8dcSSimon Schubert 
965796c8dcSSimon Schubert int get_section_index (struct objfile *, char *);
975796c8dcSSimon Schubert 
98c50c785cSJohn Marino static const struct sym_fns *find_sym_fns (bfd *);
995796c8dcSSimon Schubert 
1005796c8dcSSimon Schubert static void decrement_reading_symtab (void *);
1015796c8dcSSimon Schubert 
1025796c8dcSSimon Schubert static void overlay_invalidate_all (void);
1035796c8dcSSimon Schubert 
1045796c8dcSSimon Schubert static void overlay_auto_command (char *, int);
1055796c8dcSSimon Schubert 
1065796c8dcSSimon Schubert static void overlay_manual_command (char *, int);
1075796c8dcSSimon Schubert 
1085796c8dcSSimon Schubert static void overlay_off_command (char *, int);
1095796c8dcSSimon Schubert 
1105796c8dcSSimon Schubert static void overlay_load_command (char *, int);
1115796c8dcSSimon Schubert 
1125796c8dcSSimon Schubert static void overlay_command (char *, int);
1135796c8dcSSimon Schubert 
1145796c8dcSSimon Schubert static void simple_free_overlay_table (void);
1155796c8dcSSimon Schubert 
1165796c8dcSSimon Schubert static void read_target_long_array (CORE_ADDR, unsigned int *, int, int,
1175796c8dcSSimon Schubert 				    enum bfd_endian);
1185796c8dcSSimon Schubert 
1195796c8dcSSimon Schubert static int simple_read_overlay_table (void);
1205796c8dcSSimon Schubert 
1215796c8dcSSimon Schubert static int simple_overlay_update_1 (struct obj_section *);
1225796c8dcSSimon Schubert 
1235796c8dcSSimon Schubert static void add_filename_language (char *ext, enum language lang);
1245796c8dcSSimon Schubert 
1255796c8dcSSimon Schubert static void info_ext_lang_command (char *args, int from_tty);
1265796c8dcSSimon Schubert 
1275796c8dcSSimon Schubert static void init_filename_language_table (void);
1285796c8dcSSimon Schubert 
1295796c8dcSSimon Schubert static void symfile_find_segment_sections (struct objfile *objfile);
1305796c8dcSSimon Schubert 
1315796c8dcSSimon Schubert void _initialize_symfile (void);
1325796c8dcSSimon Schubert 
1335796c8dcSSimon Schubert /* List of all available sym_fns.  On gdb startup, each object file reader
1345796c8dcSSimon Schubert    calls add_symtab_fns() to register information on each format it is
1355796c8dcSSimon Schubert    prepared to read.  */
1365796c8dcSSimon Schubert 
137c50c785cSJohn Marino typedef const struct sym_fns *sym_fns_ptr;
138c50c785cSJohn Marino DEF_VEC_P (sym_fns_ptr);
139c50c785cSJohn Marino 
140c50c785cSJohn Marino static VEC (sym_fns_ptr) *symtab_fns = NULL;
1415796c8dcSSimon Schubert 
1425796c8dcSSimon Schubert /* If non-zero, shared library symbols will be added automatically
1435796c8dcSSimon Schubert    when the inferior is created, new libraries are loaded, or when
1445796c8dcSSimon Schubert    attaching to the inferior.  This is almost always what users will
1455796c8dcSSimon Schubert    want to have happen; but for very large programs, the startup time
1465796c8dcSSimon Schubert    will be excessive, and so if this is a problem, the user can clear
1475796c8dcSSimon Schubert    this flag and then add the shared library symbols as needed.  Note
1485796c8dcSSimon Schubert    that there is a potential for confusion, since if the shared
1495796c8dcSSimon Schubert    library symbols are not loaded, commands like "info fun" will *not*
1505796c8dcSSimon Schubert    report all the functions that are actually present.  */
1515796c8dcSSimon Schubert 
1525796c8dcSSimon Schubert int auto_solib_add = 1;
1535796c8dcSSimon Schubert 
1545796c8dcSSimon Schubert 
155cf7f2e2dSJohn Marino /* True if we are reading a symbol table.  */
1565796c8dcSSimon Schubert 
1575796c8dcSSimon Schubert int currently_reading_symtab = 0;
1585796c8dcSSimon Schubert 
1595796c8dcSSimon Schubert static void
decrement_reading_symtab(void * dummy)1605796c8dcSSimon Schubert decrement_reading_symtab (void *dummy)
1615796c8dcSSimon Schubert {
1625796c8dcSSimon Schubert   currently_reading_symtab--;
1635796c8dcSSimon Schubert }
1645796c8dcSSimon Schubert 
165cf7f2e2dSJohn Marino /* Increment currently_reading_symtab and return a cleanup that can be
166cf7f2e2dSJohn Marino    used to decrement it.  */
167cf7f2e2dSJohn Marino struct cleanup *
increment_reading_symtab(void)168cf7f2e2dSJohn Marino increment_reading_symtab (void)
1695796c8dcSSimon Schubert {
170cf7f2e2dSJohn Marino   ++currently_reading_symtab;
171cf7f2e2dSJohn Marino   return make_cleanup (decrement_reading_symtab, NULL);
1725796c8dcSSimon Schubert }
1735796c8dcSSimon Schubert 
1745796c8dcSSimon Schubert /* Remember the lowest-addressed loadable section we've seen.
1755796c8dcSSimon Schubert    This function is called via bfd_map_over_sections.
1765796c8dcSSimon Schubert 
1775796c8dcSSimon Schubert    In case of equal vmas, the section with the largest size becomes the
1785796c8dcSSimon Schubert    lowest-addressed loadable section.
1795796c8dcSSimon Schubert 
1805796c8dcSSimon Schubert    If the vmas and sizes are equal, the last section is considered the
1815796c8dcSSimon Schubert    lowest-addressed loadable section.  */
1825796c8dcSSimon Schubert 
1835796c8dcSSimon Schubert void
find_lowest_section(bfd * abfd,asection * sect,void * obj)1845796c8dcSSimon Schubert find_lowest_section (bfd *abfd, asection *sect, void *obj)
1855796c8dcSSimon Schubert {
1865796c8dcSSimon Schubert   asection **lowest = (asection **) obj;
1875796c8dcSSimon Schubert 
188cf7f2e2dSJohn Marino   if (0 == (bfd_get_section_flags (abfd, sect) & (SEC_ALLOC | SEC_LOAD)))
1895796c8dcSSimon Schubert     return;
1905796c8dcSSimon Schubert   if (!*lowest)
1915796c8dcSSimon Schubert     *lowest = sect;		/* First loadable section */
1925796c8dcSSimon Schubert   else if (bfd_section_vma (abfd, *lowest) > bfd_section_vma (abfd, sect))
1935796c8dcSSimon Schubert     *lowest = sect;		/* A lower loadable section */
1945796c8dcSSimon Schubert   else if (bfd_section_vma (abfd, *lowest) == bfd_section_vma (abfd, sect)
1955796c8dcSSimon Schubert 	   && (bfd_section_size (abfd, (*lowest))
1965796c8dcSSimon Schubert 	       <= bfd_section_size (abfd, sect)))
1975796c8dcSSimon Schubert     *lowest = sect;
1985796c8dcSSimon Schubert }
1995796c8dcSSimon Schubert 
2005796c8dcSSimon Schubert /* Create a new section_addr_info, with room for NUM_SECTIONS.  */
2015796c8dcSSimon Schubert 
2025796c8dcSSimon Schubert struct section_addr_info *
alloc_section_addr_info(size_t num_sections)2035796c8dcSSimon Schubert alloc_section_addr_info (size_t num_sections)
2045796c8dcSSimon Schubert {
2055796c8dcSSimon Schubert   struct section_addr_info *sap;
2065796c8dcSSimon Schubert   size_t size;
2075796c8dcSSimon Schubert 
2085796c8dcSSimon Schubert   size = (sizeof (struct section_addr_info)
2095796c8dcSSimon Schubert 	  +  sizeof (struct other_sections) * (num_sections - 1));
2105796c8dcSSimon Schubert   sap = (struct section_addr_info *) xmalloc (size);
2115796c8dcSSimon Schubert   memset (sap, 0, size);
2125796c8dcSSimon Schubert   sap->num_sections = num_sections;
2135796c8dcSSimon Schubert 
2145796c8dcSSimon Schubert   return sap;
2155796c8dcSSimon Schubert }
2165796c8dcSSimon Schubert 
2175796c8dcSSimon Schubert /* Build (allocate and populate) a section_addr_info struct from
2185796c8dcSSimon Schubert    an existing section table.  */
2195796c8dcSSimon Schubert 
2205796c8dcSSimon Schubert extern struct section_addr_info *
build_section_addr_info_from_section_table(const struct target_section * start,const struct target_section * end)2215796c8dcSSimon Schubert build_section_addr_info_from_section_table (const struct target_section *start,
2225796c8dcSSimon Schubert                                             const struct target_section *end)
2235796c8dcSSimon Schubert {
2245796c8dcSSimon Schubert   struct section_addr_info *sap;
2255796c8dcSSimon Schubert   const struct target_section *stp;
2265796c8dcSSimon Schubert   int oidx;
2275796c8dcSSimon Schubert 
2285796c8dcSSimon Schubert   sap = alloc_section_addr_info (end - start);
2295796c8dcSSimon Schubert 
2305796c8dcSSimon Schubert   for (stp = start, oidx = 0; stp != end; stp++)
2315796c8dcSSimon Schubert     {
2325796c8dcSSimon Schubert       if (bfd_get_section_flags (stp->bfd,
2335796c8dcSSimon Schubert 				 stp->the_bfd_section) & (SEC_ALLOC | SEC_LOAD)
2345796c8dcSSimon Schubert 	  && oidx < end - start)
2355796c8dcSSimon Schubert 	{
2365796c8dcSSimon Schubert 	  sap->other[oidx].addr = stp->addr;
2375796c8dcSSimon Schubert 	  sap->other[oidx].name
2385796c8dcSSimon Schubert 	    = xstrdup (bfd_section_name (stp->bfd, stp->the_bfd_section));
2395796c8dcSSimon Schubert 	  sap->other[oidx].sectindex = stp->the_bfd_section->index;
2405796c8dcSSimon Schubert 	  oidx++;
2415796c8dcSSimon Schubert 	}
2425796c8dcSSimon Schubert     }
2435796c8dcSSimon Schubert 
2445796c8dcSSimon Schubert   return sap;
2455796c8dcSSimon Schubert }
2465796c8dcSSimon Schubert 
247cf7f2e2dSJohn Marino /* Create a section_addr_info from section offsets in ABFD.  */
248cf7f2e2dSJohn Marino 
249cf7f2e2dSJohn Marino static struct section_addr_info *
build_section_addr_info_from_bfd(bfd * abfd)250cf7f2e2dSJohn Marino build_section_addr_info_from_bfd (bfd *abfd)
251cf7f2e2dSJohn Marino {
252cf7f2e2dSJohn Marino   struct section_addr_info *sap;
253cf7f2e2dSJohn Marino   int i;
254cf7f2e2dSJohn Marino   struct bfd_section *sec;
255cf7f2e2dSJohn Marino 
256cf7f2e2dSJohn Marino   sap = alloc_section_addr_info (bfd_count_sections (abfd));
257cf7f2e2dSJohn Marino   for (i = 0, sec = abfd->sections; sec != NULL; sec = sec->next)
258cf7f2e2dSJohn Marino     if (bfd_get_section_flags (abfd, sec) & (SEC_ALLOC | SEC_LOAD))
259cf7f2e2dSJohn Marino       {
260cf7f2e2dSJohn Marino 	sap->other[i].addr = bfd_get_section_vma (abfd, sec);
261cf7f2e2dSJohn Marino 	sap->other[i].name = xstrdup (bfd_get_section_name (abfd, sec));
262cf7f2e2dSJohn Marino 	sap->other[i].sectindex = sec->index;
263cf7f2e2dSJohn Marino 	i++;
264cf7f2e2dSJohn Marino       }
265cf7f2e2dSJohn Marino   return sap;
266cf7f2e2dSJohn Marino }
267cf7f2e2dSJohn Marino 
268cf7f2e2dSJohn Marino /* Create a section_addr_info from section offsets in OBJFILE.  */
269cf7f2e2dSJohn Marino 
270cf7f2e2dSJohn Marino struct section_addr_info *
build_section_addr_info_from_objfile(const struct objfile * objfile)271cf7f2e2dSJohn Marino build_section_addr_info_from_objfile (const struct objfile *objfile)
272cf7f2e2dSJohn Marino {
273cf7f2e2dSJohn Marino   struct section_addr_info *sap;
274cf7f2e2dSJohn Marino   int i;
275cf7f2e2dSJohn Marino 
276cf7f2e2dSJohn Marino   /* Before reread_symbols gets rewritten it is not safe to call:
277cf7f2e2dSJohn Marino      gdb_assert (objfile->num_sections == bfd_count_sections (objfile->obfd));
278cf7f2e2dSJohn Marino      */
279cf7f2e2dSJohn Marino   sap = build_section_addr_info_from_bfd (objfile->obfd);
280cf7f2e2dSJohn Marino   for (i = 0; i < sap->num_sections && sap->other[i].name; i++)
281cf7f2e2dSJohn Marino     {
282cf7f2e2dSJohn Marino       int sectindex = sap->other[i].sectindex;
283cf7f2e2dSJohn Marino 
284cf7f2e2dSJohn Marino       sap->other[i].addr += objfile->section_offsets->offsets[sectindex];
285cf7f2e2dSJohn Marino     }
286cf7f2e2dSJohn Marino   return sap;
287cf7f2e2dSJohn Marino }
2885796c8dcSSimon Schubert 
2895796c8dcSSimon Schubert /* Free all memory allocated by build_section_addr_info_from_section_table.  */
2905796c8dcSSimon Schubert 
2915796c8dcSSimon Schubert extern void
free_section_addr_info(struct section_addr_info * sap)2925796c8dcSSimon Schubert free_section_addr_info (struct section_addr_info *sap)
2935796c8dcSSimon Schubert {
2945796c8dcSSimon Schubert   int idx;
2955796c8dcSSimon Schubert 
2965796c8dcSSimon Schubert   for (idx = 0; idx < sap->num_sections; idx++)
2975796c8dcSSimon Schubert     if (sap->other[idx].name)
2985796c8dcSSimon Schubert       xfree (sap->other[idx].name);
2995796c8dcSSimon Schubert   xfree (sap);
3005796c8dcSSimon Schubert }
3015796c8dcSSimon Schubert 
3025796c8dcSSimon Schubert 
3035796c8dcSSimon Schubert /* Initialize OBJFILE's sect_index_* members.  */
3045796c8dcSSimon Schubert static void
init_objfile_sect_indices(struct objfile * objfile)3055796c8dcSSimon Schubert init_objfile_sect_indices (struct objfile *objfile)
3065796c8dcSSimon Schubert {
3075796c8dcSSimon Schubert   asection *sect;
3085796c8dcSSimon Schubert   int i;
3095796c8dcSSimon Schubert 
3105796c8dcSSimon Schubert   sect = bfd_get_section_by_name (objfile->obfd, ".text");
3115796c8dcSSimon Schubert   if (sect)
3125796c8dcSSimon Schubert     objfile->sect_index_text = sect->index;
3135796c8dcSSimon Schubert 
3145796c8dcSSimon Schubert   sect = bfd_get_section_by_name (objfile->obfd, ".data");
3155796c8dcSSimon Schubert   if (sect)
3165796c8dcSSimon Schubert     objfile->sect_index_data = sect->index;
3175796c8dcSSimon Schubert 
3185796c8dcSSimon Schubert   sect = bfd_get_section_by_name (objfile->obfd, ".bss");
3195796c8dcSSimon Schubert   if (sect)
3205796c8dcSSimon Schubert     objfile->sect_index_bss = sect->index;
3215796c8dcSSimon Schubert 
3225796c8dcSSimon Schubert   sect = bfd_get_section_by_name (objfile->obfd, ".rodata");
3235796c8dcSSimon Schubert   if (sect)
3245796c8dcSSimon Schubert     objfile->sect_index_rodata = sect->index;
3255796c8dcSSimon Schubert 
3265796c8dcSSimon Schubert   /* This is where things get really weird...  We MUST have valid
3275796c8dcSSimon Schubert      indices for the various sect_index_* members or gdb will abort.
3285796c8dcSSimon Schubert      So if for example, there is no ".text" section, we have to
3295796c8dcSSimon Schubert      accomodate that.  First, check for a file with the standard
3305796c8dcSSimon Schubert      one or two segments.  */
3315796c8dcSSimon Schubert 
3325796c8dcSSimon Schubert   symfile_find_segment_sections (objfile);
3335796c8dcSSimon Schubert 
3345796c8dcSSimon Schubert   /* Except when explicitly adding symbol files at some address,
3355796c8dcSSimon Schubert      section_offsets contains nothing but zeros, so it doesn't matter
3365796c8dcSSimon Schubert      which slot in section_offsets the individual sect_index_* members
3375796c8dcSSimon Schubert      index into.  So if they are all zero, it is safe to just point
3385796c8dcSSimon Schubert      all the currently uninitialized indices to the first slot.  But
3395796c8dcSSimon Schubert      beware: if this is the main executable, it may be relocated
3405796c8dcSSimon Schubert      later, e.g. by the remote qOffsets packet, and then this will
3415796c8dcSSimon Schubert      be wrong!  That's why we try segments first.  */
3425796c8dcSSimon Schubert 
3435796c8dcSSimon Schubert   for (i = 0; i < objfile->num_sections; i++)
3445796c8dcSSimon Schubert     {
3455796c8dcSSimon Schubert       if (ANOFFSET (objfile->section_offsets, i) != 0)
3465796c8dcSSimon Schubert 	{
3475796c8dcSSimon Schubert 	  break;
3485796c8dcSSimon Schubert 	}
3495796c8dcSSimon Schubert     }
3505796c8dcSSimon Schubert   if (i == objfile->num_sections)
3515796c8dcSSimon Schubert     {
3525796c8dcSSimon Schubert       if (objfile->sect_index_text == -1)
3535796c8dcSSimon Schubert 	objfile->sect_index_text = 0;
3545796c8dcSSimon Schubert       if (objfile->sect_index_data == -1)
3555796c8dcSSimon Schubert 	objfile->sect_index_data = 0;
3565796c8dcSSimon Schubert       if (objfile->sect_index_bss == -1)
3575796c8dcSSimon Schubert 	objfile->sect_index_bss = 0;
3585796c8dcSSimon Schubert       if (objfile->sect_index_rodata == -1)
3595796c8dcSSimon Schubert 	objfile->sect_index_rodata = 0;
3605796c8dcSSimon Schubert     }
3615796c8dcSSimon Schubert }
3625796c8dcSSimon Schubert 
3635796c8dcSSimon Schubert /* The arguments to place_section.  */
3645796c8dcSSimon Schubert 
3655796c8dcSSimon Schubert struct place_section_arg
3665796c8dcSSimon Schubert {
3675796c8dcSSimon Schubert   struct section_offsets *offsets;
3685796c8dcSSimon Schubert   CORE_ADDR lowest;
3695796c8dcSSimon Schubert };
3705796c8dcSSimon Schubert 
3715796c8dcSSimon Schubert /* Find a unique offset to use for loadable section SECT if
3725796c8dcSSimon Schubert    the user did not provide an offset.  */
3735796c8dcSSimon Schubert 
3745796c8dcSSimon Schubert static void
place_section(bfd * abfd,asection * sect,void * obj)3755796c8dcSSimon Schubert place_section (bfd *abfd, asection *sect, void *obj)
3765796c8dcSSimon Schubert {
3775796c8dcSSimon Schubert   struct place_section_arg *arg = obj;
3785796c8dcSSimon Schubert   CORE_ADDR *offsets = arg->offsets->offsets, start_addr;
3795796c8dcSSimon Schubert   int done;
3805796c8dcSSimon Schubert   ULONGEST align = ((ULONGEST) 1) << bfd_get_section_alignment (abfd, sect);
3815796c8dcSSimon Schubert 
3825796c8dcSSimon Schubert   /* We are only interested in allocated sections.  */
3835796c8dcSSimon Schubert   if ((bfd_get_section_flags (abfd, sect) & SEC_ALLOC) == 0)
3845796c8dcSSimon Schubert     return;
3855796c8dcSSimon Schubert 
3865796c8dcSSimon Schubert   /* If the user specified an offset, honor it.  */
3875796c8dcSSimon Schubert   if (offsets[sect->index] != 0)
3885796c8dcSSimon Schubert     return;
3895796c8dcSSimon Schubert 
3905796c8dcSSimon Schubert   /* Otherwise, let's try to find a place for the section.  */
3915796c8dcSSimon Schubert   start_addr = (arg->lowest + align - 1) & -align;
3925796c8dcSSimon Schubert 
3935796c8dcSSimon Schubert   do {
3945796c8dcSSimon Schubert     asection *cur_sec;
3955796c8dcSSimon Schubert 
3965796c8dcSSimon Schubert     done = 1;
3975796c8dcSSimon Schubert 
3985796c8dcSSimon Schubert     for (cur_sec = abfd->sections; cur_sec != NULL; cur_sec = cur_sec->next)
3995796c8dcSSimon Schubert       {
4005796c8dcSSimon Schubert 	int indx = cur_sec->index;
4015796c8dcSSimon Schubert 
4025796c8dcSSimon Schubert 	/* We don't need to compare against ourself.  */
4035796c8dcSSimon Schubert 	if (cur_sec == sect)
4045796c8dcSSimon Schubert 	  continue;
4055796c8dcSSimon Schubert 
4065796c8dcSSimon Schubert 	/* We can only conflict with allocated sections.  */
4075796c8dcSSimon Schubert 	if ((bfd_get_section_flags (abfd, cur_sec) & SEC_ALLOC) == 0)
4085796c8dcSSimon Schubert 	  continue;
4095796c8dcSSimon Schubert 
4105796c8dcSSimon Schubert 	/* If the section offset is 0, either the section has not been placed
4115796c8dcSSimon Schubert 	   yet, or it was the lowest section placed (in which case LOWEST
4125796c8dcSSimon Schubert 	   will be past its end).  */
4135796c8dcSSimon Schubert 	if (offsets[indx] == 0)
4145796c8dcSSimon Schubert 	  continue;
4155796c8dcSSimon Schubert 
4165796c8dcSSimon Schubert 	/* If this section would overlap us, then we must move up.  */
4175796c8dcSSimon Schubert 	if (start_addr + bfd_get_section_size (sect) > offsets[indx]
4185796c8dcSSimon Schubert 	    && start_addr < offsets[indx] + bfd_get_section_size (cur_sec))
4195796c8dcSSimon Schubert 	  {
4205796c8dcSSimon Schubert 	    start_addr = offsets[indx] + bfd_get_section_size (cur_sec);
4215796c8dcSSimon Schubert 	    start_addr = (start_addr + align - 1) & -align;
4225796c8dcSSimon Schubert 	    done = 0;
4235796c8dcSSimon Schubert 	    break;
4245796c8dcSSimon Schubert 	  }
4255796c8dcSSimon Schubert 
4265796c8dcSSimon Schubert 	/* Otherwise, we appear to be OK.  So far.  */
4275796c8dcSSimon Schubert       }
4285796c8dcSSimon Schubert     }
4295796c8dcSSimon Schubert   while (!done);
4305796c8dcSSimon Schubert 
4315796c8dcSSimon Schubert   offsets[sect->index] = start_addr;
4325796c8dcSSimon Schubert   arg->lowest = start_addr + bfd_get_section_size (sect);
4335796c8dcSSimon Schubert }
4345796c8dcSSimon Schubert 
435cf7f2e2dSJohn Marino /* Store struct section_addr_info as prepared (made relative and with SECTINDEX
436cf7f2e2dSJohn Marino    filled-in) by addr_info_make_relative into SECTION_OFFSETS of NUM_SECTIONS
437cf7f2e2dSJohn Marino    entries.  */
4385796c8dcSSimon Schubert 
4395796c8dcSSimon Schubert void
relative_addr_info_to_section_offsets(struct section_offsets * section_offsets,int num_sections,struct section_addr_info * addrs)440cf7f2e2dSJohn Marino relative_addr_info_to_section_offsets (struct section_offsets *section_offsets,
441cf7f2e2dSJohn Marino 				       int num_sections,
4425796c8dcSSimon Schubert 				       struct section_addr_info *addrs)
4435796c8dcSSimon Schubert {
4445796c8dcSSimon Schubert   int i;
4455796c8dcSSimon Schubert 
446cf7f2e2dSJohn Marino   memset (section_offsets, 0, SIZEOF_N_SECTION_OFFSETS (num_sections));
4475796c8dcSSimon Schubert 
448cf7f2e2dSJohn Marino   /* Now calculate offsets for section that were specified by the caller.  */
4495796c8dcSSimon Schubert   for (i = 0; i < addrs->num_sections && addrs->other[i].name; i++)
4505796c8dcSSimon Schubert     {
4515796c8dcSSimon Schubert       struct other_sections *osp;
4525796c8dcSSimon Schubert 
4535796c8dcSSimon Schubert       osp = &addrs->other[i];
454a45ae5f8SJohn Marino       if (osp->sectindex == -1)
4555796c8dcSSimon Schubert   	continue;
4565796c8dcSSimon Schubert 
457c50c785cSJohn Marino       /* Record all sections in offsets.  */
4585796c8dcSSimon Schubert       /* The section_offsets in the objfile are here filled in using
4595796c8dcSSimon Schubert          the BFD index.  */
460cf7f2e2dSJohn Marino       section_offsets->offsets[osp->sectindex] = osp->addr;
4615796c8dcSSimon Schubert     }
462cf7f2e2dSJohn Marino }
463cf7f2e2dSJohn Marino 
464c50c785cSJohn Marino /* Transform section name S for a name comparison.  prelink can split section
465c50c785cSJohn Marino    `.bss' into two sections `.dynbss' and `.bss' (in this order).  Similarly
466c50c785cSJohn Marino    prelink can split `.sbss' into `.sdynbss' and `.sbss'.  Use virtual address
467c50c785cSJohn Marino    of the new `.dynbss' (`.sdynbss') section as the adjacent new `.bss'
468c50c785cSJohn Marino    (`.sbss') section has invalid (increased) virtual address.  */
469c50c785cSJohn Marino 
470c50c785cSJohn Marino static const char *
addr_section_name(const char * s)471c50c785cSJohn Marino addr_section_name (const char *s)
472c50c785cSJohn Marino {
473c50c785cSJohn Marino   if (strcmp (s, ".dynbss") == 0)
474c50c785cSJohn Marino     return ".bss";
475c50c785cSJohn Marino   if (strcmp (s, ".sdynbss") == 0)
476c50c785cSJohn Marino     return ".sbss";
477c50c785cSJohn Marino 
478c50c785cSJohn Marino   return s;
479c50c785cSJohn Marino }
480c50c785cSJohn Marino 
481cf7f2e2dSJohn Marino /* qsort comparator for addrs_section_sort.  Sort entries in ascending order by
482cf7f2e2dSJohn Marino    their (name, sectindex) pair.  sectindex makes the sort by name stable.  */
483cf7f2e2dSJohn Marino 
484cf7f2e2dSJohn Marino static int
addrs_section_compar(const void * ap,const void * bp)485cf7f2e2dSJohn Marino addrs_section_compar (const void *ap, const void *bp)
486cf7f2e2dSJohn Marino {
487cf7f2e2dSJohn Marino   const struct other_sections *a = *((struct other_sections **) ap);
488cf7f2e2dSJohn Marino   const struct other_sections *b = *((struct other_sections **) bp);
489*ef5ccd6cSJohn Marino   int retval;
490cf7f2e2dSJohn Marino 
491c50c785cSJohn Marino   retval = strcmp (addr_section_name (a->name), addr_section_name (b->name));
492cf7f2e2dSJohn Marino   if (retval)
493cf7f2e2dSJohn Marino     return retval;
494cf7f2e2dSJohn Marino 
495a45ae5f8SJohn Marino   return a->sectindex - b->sectindex;
496cf7f2e2dSJohn Marino }
497cf7f2e2dSJohn Marino 
498cf7f2e2dSJohn Marino /* Provide sorted array of pointers to sections of ADDRS.  The array is
499cf7f2e2dSJohn Marino    terminated by NULL.  Caller is responsible to call xfree for it.  */
500cf7f2e2dSJohn Marino 
501cf7f2e2dSJohn Marino static struct other_sections **
addrs_section_sort(struct section_addr_info * addrs)502cf7f2e2dSJohn Marino addrs_section_sort (struct section_addr_info *addrs)
503cf7f2e2dSJohn Marino {
504cf7f2e2dSJohn Marino   struct other_sections **array;
505cf7f2e2dSJohn Marino   int i;
506cf7f2e2dSJohn Marino 
507cf7f2e2dSJohn Marino   /* `+ 1' for the NULL terminator.  */
508cf7f2e2dSJohn Marino   array = xmalloc (sizeof (*array) * (addrs->num_sections + 1));
509cf7f2e2dSJohn Marino   for (i = 0; i < addrs->num_sections && addrs->other[i].name; i++)
510cf7f2e2dSJohn Marino     array[i] = &addrs->other[i];
511cf7f2e2dSJohn Marino   array[i] = NULL;
512cf7f2e2dSJohn Marino 
513cf7f2e2dSJohn Marino   qsort (array, i, sizeof (*array), addrs_section_compar);
514cf7f2e2dSJohn Marino 
515cf7f2e2dSJohn Marino   return array;
516cf7f2e2dSJohn Marino }
517cf7f2e2dSJohn Marino 
518cf7f2e2dSJohn Marino /* Relativize absolute addresses in ADDRS into offsets based on ABFD.  Fill-in
519cf7f2e2dSJohn Marino    also SECTINDEXes specific to ABFD there.  This function can be used to
520cf7f2e2dSJohn Marino    rebase ADDRS to start referencing different BFD than before.  */
521cf7f2e2dSJohn Marino 
522cf7f2e2dSJohn Marino void
addr_info_make_relative(struct section_addr_info * addrs,bfd * abfd)523cf7f2e2dSJohn Marino addr_info_make_relative (struct section_addr_info *addrs, bfd *abfd)
524cf7f2e2dSJohn Marino {
525cf7f2e2dSJohn Marino   asection *lower_sect;
526cf7f2e2dSJohn Marino   CORE_ADDR lower_offset;
527cf7f2e2dSJohn Marino   int i;
528cf7f2e2dSJohn Marino   struct cleanup *my_cleanup;
529cf7f2e2dSJohn Marino   struct section_addr_info *abfd_addrs;
530cf7f2e2dSJohn Marino   struct other_sections **addrs_sorted, **abfd_addrs_sorted;
531cf7f2e2dSJohn Marino   struct other_sections **addrs_to_abfd_addrs;
532cf7f2e2dSJohn Marino 
533cf7f2e2dSJohn Marino   /* Find lowest loadable section to be used as starting point for
534cf7f2e2dSJohn Marino      continguous sections.  */
535cf7f2e2dSJohn Marino   lower_sect = NULL;
536cf7f2e2dSJohn Marino   bfd_map_over_sections (abfd, find_lowest_section, &lower_sect);
537cf7f2e2dSJohn Marino   if (lower_sect == NULL)
538cf7f2e2dSJohn Marino     {
539cf7f2e2dSJohn Marino       warning (_("no loadable sections found in added symbol-file %s"),
540cf7f2e2dSJohn Marino 	       bfd_get_filename (abfd));
541cf7f2e2dSJohn Marino       lower_offset = 0;
542cf7f2e2dSJohn Marino     }
543cf7f2e2dSJohn Marino   else
544cf7f2e2dSJohn Marino     lower_offset = bfd_section_vma (bfd_get_filename (abfd), lower_sect);
545cf7f2e2dSJohn Marino 
546cf7f2e2dSJohn Marino   /* Create ADDRS_TO_ABFD_ADDRS array to map the sections in ADDRS to sections
547cf7f2e2dSJohn Marino      in ABFD.  Section names are not unique - there can be multiple sections of
548cf7f2e2dSJohn Marino      the same name.  Also the sections of the same name do not have to be
549cf7f2e2dSJohn Marino      adjacent to each other.  Some sections may be present only in one of the
550cf7f2e2dSJohn Marino      files.  Even sections present in both files do not have to be in the same
551cf7f2e2dSJohn Marino      order.
552cf7f2e2dSJohn Marino 
553cf7f2e2dSJohn Marino      Use stable sort by name for the sections in both files.  Then linearly
554cf7f2e2dSJohn Marino      scan both lists matching as most of the entries as possible.  */
555cf7f2e2dSJohn Marino 
556cf7f2e2dSJohn Marino   addrs_sorted = addrs_section_sort (addrs);
557cf7f2e2dSJohn Marino   my_cleanup = make_cleanup (xfree, addrs_sorted);
558cf7f2e2dSJohn Marino 
559cf7f2e2dSJohn Marino   abfd_addrs = build_section_addr_info_from_bfd (abfd);
560cf7f2e2dSJohn Marino   make_cleanup_free_section_addr_info (abfd_addrs);
561cf7f2e2dSJohn Marino   abfd_addrs_sorted = addrs_section_sort (abfd_addrs);
562cf7f2e2dSJohn Marino   make_cleanup (xfree, abfd_addrs_sorted);
563cf7f2e2dSJohn Marino 
564c50c785cSJohn Marino   /* Now create ADDRS_TO_ABFD_ADDRS from ADDRS_SORTED and
565c50c785cSJohn Marino      ABFD_ADDRS_SORTED.  */
566cf7f2e2dSJohn Marino 
567cf7f2e2dSJohn Marino   addrs_to_abfd_addrs = xzalloc (sizeof (*addrs_to_abfd_addrs)
568cf7f2e2dSJohn Marino 				 * addrs->num_sections);
569cf7f2e2dSJohn Marino   make_cleanup (xfree, addrs_to_abfd_addrs);
570cf7f2e2dSJohn Marino 
571cf7f2e2dSJohn Marino   while (*addrs_sorted)
572cf7f2e2dSJohn Marino     {
573c50c785cSJohn Marino       const char *sect_name = addr_section_name ((*addrs_sorted)->name);
574cf7f2e2dSJohn Marino 
575cf7f2e2dSJohn Marino       while (*abfd_addrs_sorted
576c50c785cSJohn Marino 	     && strcmp (addr_section_name ((*abfd_addrs_sorted)->name),
577c50c785cSJohn Marino 			sect_name) < 0)
578cf7f2e2dSJohn Marino 	abfd_addrs_sorted++;
579cf7f2e2dSJohn Marino 
580cf7f2e2dSJohn Marino       if (*abfd_addrs_sorted
581c50c785cSJohn Marino 	  && strcmp (addr_section_name ((*abfd_addrs_sorted)->name),
582c50c785cSJohn Marino 		     sect_name) == 0)
583cf7f2e2dSJohn Marino 	{
584cf7f2e2dSJohn Marino 	  int index_in_addrs;
585cf7f2e2dSJohn Marino 
586cf7f2e2dSJohn Marino 	  /* Make the found item directly addressable from ADDRS.  */
587cf7f2e2dSJohn Marino 	  index_in_addrs = *addrs_sorted - addrs->other;
588cf7f2e2dSJohn Marino 	  gdb_assert (addrs_to_abfd_addrs[index_in_addrs] == NULL);
589cf7f2e2dSJohn Marino 	  addrs_to_abfd_addrs[index_in_addrs] = *abfd_addrs_sorted;
590cf7f2e2dSJohn Marino 
591cf7f2e2dSJohn Marino 	  /* Never use the same ABFD entry twice.  */
592cf7f2e2dSJohn Marino 	  abfd_addrs_sorted++;
593cf7f2e2dSJohn Marino 	}
594cf7f2e2dSJohn Marino 
595cf7f2e2dSJohn Marino       addrs_sorted++;
596cf7f2e2dSJohn Marino     }
597cf7f2e2dSJohn Marino 
598cf7f2e2dSJohn Marino   /* Calculate offsets for the loadable sections.
599cf7f2e2dSJohn Marino      FIXME! Sections must be in order of increasing loadable section
600cf7f2e2dSJohn Marino      so that contiguous sections can use the lower-offset!!!
601cf7f2e2dSJohn Marino 
602cf7f2e2dSJohn Marino      Adjust offsets if the segments are not contiguous.
603cf7f2e2dSJohn Marino      If the section is contiguous, its offset should be set to
604cf7f2e2dSJohn Marino      the offset of the highest loadable section lower than it
605cf7f2e2dSJohn Marino      (the loadable section directly below it in memory).
606cf7f2e2dSJohn Marino      this_offset = lower_offset = lower_addr - lower_orig_addr */
607cf7f2e2dSJohn Marino 
608cf7f2e2dSJohn Marino   for (i = 0; i < addrs->num_sections && addrs->other[i].name; i++)
609cf7f2e2dSJohn Marino     {
610cf7f2e2dSJohn Marino       struct other_sections *sect = addrs_to_abfd_addrs[i];
611cf7f2e2dSJohn Marino 
612cf7f2e2dSJohn Marino       if (sect)
613cf7f2e2dSJohn Marino 	{
614cf7f2e2dSJohn Marino 	  /* This is the index used by BFD.  */
615cf7f2e2dSJohn Marino 	  addrs->other[i].sectindex = sect->sectindex;
616cf7f2e2dSJohn Marino 
617cf7f2e2dSJohn Marino 	  if (addrs->other[i].addr != 0)
618cf7f2e2dSJohn Marino 	    {
619cf7f2e2dSJohn Marino 	      addrs->other[i].addr -= sect->addr;
620cf7f2e2dSJohn Marino 	      lower_offset = addrs->other[i].addr;
621cf7f2e2dSJohn Marino 	    }
622cf7f2e2dSJohn Marino 	  else
623cf7f2e2dSJohn Marino 	    addrs->other[i].addr = lower_offset;
624cf7f2e2dSJohn Marino 	}
625cf7f2e2dSJohn Marino       else
626cf7f2e2dSJohn Marino 	{
627c50c785cSJohn Marino 	  /* addr_section_name transformation is not used for SECT_NAME.  */
628c50c785cSJohn Marino 	  const char *sect_name = addrs->other[i].name;
629c50c785cSJohn Marino 
630cf7f2e2dSJohn Marino 	  /* This section does not exist in ABFD, which is normally
631cf7f2e2dSJohn Marino 	     unexpected and we want to issue a warning.
632cf7f2e2dSJohn Marino 
633cf7f2e2dSJohn Marino 	     However, the ELF prelinker does create a few sections which are
634cf7f2e2dSJohn Marino 	     marked in the main executable as loadable (they are loaded in
635cf7f2e2dSJohn Marino 	     memory from the DYNAMIC segment) and yet are not present in
636cf7f2e2dSJohn Marino 	     separate debug info files.  This is fine, and should not cause
637cf7f2e2dSJohn Marino 	     a warning.  Shared libraries contain just the section
638cf7f2e2dSJohn Marino 	     ".gnu.liblist" but it is not marked as loadable there.  There is
639cf7f2e2dSJohn Marino 	     no other way to identify them than by their name as the sections
640c50c785cSJohn Marino 	     created by prelink have no special flags.
641c50c785cSJohn Marino 
642c50c785cSJohn Marino 	     For the sections `.bss' and `.sbss' see addr_section_name.  */
643cf7f2e2dSJohn Marino 
644cf7f2e2dSJohn Marino 	  if (!(strcmp (sect_name, ".gnu.liblist") == 0
645cf7f2e2dSJohn Marino 		|| strcmp (sect_name, ".gnu.conflict") == 0
646c50c785cSJohn Marino 		|| (strcmp (sect_name, ".bss") == 0
647c50c785cSJohn Marino 		    && i > 0
648c50c785cSJohn Marino 		    && strcmp (addrs->other[i - 1].name, ".dynbss") == 0
649c50c785cSJohn Marino 		    && addrs_to_abfd_addrs[i - 1] != NULL)
650c50c785cSJohn Marino 		|| (strcmp (sect_name, ".sbss") == 0
651c50c785cSJohn Marino 		    && i > 0
652c50c785cSJohn Marino 		    && strcmp (addrs->other[i - 1].name, ".sdynbss") == 0
653c50c785cSJohn Marino 		    && addrs_to_abfd_addrs[i - 1] != NULL)))
654cf7f2e2dSJohn Marino 	    warning (_("section %s not found in %s"), sect_name,
655cf7f2e2dSJohn Marino 		     bfd_get_filename (abfd));
656cf7f2e2dSJohn Marino 
657cf7f2e2dSJohn Marino 	  addrs->other[i].addr = 0;
658a45ae5f8SJohn Marino 	  addrs->other[i].sectindex = -1;
659cf7f2e2dSJohn Marino 	}
660cf7f2e2dSJohn Marino     }
661cf7f2e2dSJohn Marino 
662cf7f2e2dSJohn Marino   do_cleanups (my_cleanup);
663cf7f2e2dSJohn Marino }
664cf7f2e2dSJohn Marino 
665cf7f2e2dSJohn Marino /* Parse the user's idea of an offset for dynamic linking, into our idea
666cf7f2e2dSJohn Marino    of how to represent it for fast symbol reading.  This is the default
667cf7f2e2dSJohn Marino    version of the sym_fns.sym_offsets function for symbol readers that
668cf7f2e2dSJohn Marino    don't need to do anything special.  It allocates a section_offsets table
669cf7f2e2dSJohn Marino    for the objectfile OBJFILE and stuffs ADDR into all of the offsets.  */
670cf7f2e2dSJohn Marino 
671cf7f2e2dSJohn Marino void
default_symfile_offsets(struct objfile * objfile,struct section_addr_info * addrs)672cf7f2e2dSJohn Marino default_symfile_offsets (struct objfile *objfile,
673cf7f2e2dSJohn Marino 			 struct section_addr_info *addrs)
674cf7f2e2dSJohn Marino {
675cf7f2e2dSJohn Marino   objfile->num_sections = bfd_count_sections (objfile->obfd);
676cf7f2e2dSJohn Marino   objfile->section_offsets = (struct section_offsets *)
677cf7f2e2dSJohn Marino     obstack_alloc (&objfile->objfile_obstack,
678cf7f2e2dSJohn Marino 		   SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
679cf7f2e2dSJohn Marino   relative_addr_info_to_section_offsets (objfile->section_offsets,
680cf7f2e2dSJohn Marino 					 objfile->num_sections, addrs);
6815796c8dcSSimon Schubert 
6825796c8dcSSimon Schubert   /* For relocatable files, all loadable sections will start at zero.
6835796c8dcSSimon Schubert      The zero is meaningless, so try to pick arbitrary addresses such
6845796c8dcSSimon Schubert      that no loadable sections overlap.  This algorithm is quadratic,
6855796c8dcSSimon Schubert      but the number of sections in a single object file is generally
6865796c8dcSSimon Schubert      small.  */
6875796c8dcSSimon Schubert   if ((bfd_get_file_flags (objfile->obfd) & (EXEC_P | DYNAMIC)) == 0)
6885796c8dcSSimon Schubert     {
6895796c8dcSSimon Schubert       struct place_section_arg arg;
6905796c8dcSSimon Schubert       bfd *abfd = objfile->obfd;
6915796c8dcSSimon Schubert       asection *cur_sec;
6925796c8dcSSimon Schubert 
6935796c8dcSSimon Schubert       for (cur_sec = abfd->sections; cur_sec != NULL; cur_sec = cur_sec->next)
6945796c8dcSSimon Schubert 	/* We do not expect this to happen; just skip this step if the
6955796c8dcSSimon Schubert 	   relocatable file has a section with an assigned VMA.  */
6965796c8dcSSimon Schubert 	if (bfd_section_vma (abfd, cur_sec) != 0)
6975796c8dcSSimon Schubert 	  break;
6985796c8dcSSimon Schubert 
6995796c8dcSSimon Schubert       if (cur_sec == NULL)
7005796c8dcSSimon Schubert 	{
7015796c8dcSSimon Schubert 	  CORE_ADDR *offsets = objfile->section_offsets->offsets;
7025796c8dcSSimon Schubert 
7035796c8dcSSimon Schubert 	  /* Pick non-overlapping offsets for sections the user did not
7045796c8dcSSimon Schubert 	     place explicitly.  */
7055796c8dcSSimon Schubert 	  arg.offsets = objfile->section_offsets;
7065796c8dcSSimon Schubert 	  arg.lowest = 0;
7075796c8dcSSimon Schubert 	  bfd_map_over_sections (objfile->obfd, place_section, &arg);
7085796c8dcSSimon Schubert 
7095796c8dcSSimon Schubert 	  /* Correctly filling in the section offsets is not quite
7105796c8dcSSimon Schubert 	     enough.  Relocatable files have two properties that
7115796c8dcSSimon Schubert 	     (most) shared objects do not:
7125796c8dcSSimon Schubert 
7135796c8dcSSimon Schubert 	     - Their debug information will contain relocations.  Some
7145796c8dcSSimon Schubert 	     shared libraries do also, but many do not, so this can not
7155796c8dcSSimon Schubert 	     be assumed.
7165796c8dcSSimon Schubert 
7175796c8dcSSimon Schubert 	     - If there are multiple code sections they will be loaded
7185796c8dcSSimon Schubert 	     at different relative addresses in memory than they are
7195796c8dcSSimon Schubert 	     in the objfile, since all sections in the file will start
7205796c8dcSSimon Schubert 	     at address zero.
7215796c8dcSSimon Schubert 
7225796c8dcSSimon Schubert 	     Because GDB has very limited ability to map from an
7235796c8dcSSimon Schubert 	     address in debug info to the correct code section,
7245796c8dcSSimon Schubert 	     it relies on adding SECT_OFF_TEXT to things which might be
7255796c8dcSSimon Schubert 	     code.  If we clear all the section offsets, and set the
7265796c8dcSSimon Schubert 	     section VMAs instead, then symfile_relocate_debug_section
7275796c8dcSSimon Schubert 	     will return meaningful debug information pointing at the
7285796c8dcSSimon Schubert 	     correct sections.
7295796c8dcSSimon Schubert 
7305796c8dcSSimon Schubert 	     GDB has too many different data structures for section
7315796c8dcSSimon Schubert 	     addresses - a bfd, objfile, and so_list all have section
7325796c8dcSSimon Schubert 	     tables, as does exec_ops.  Some of these could probably
7335796c8dcSSimon Schubert 	     be eliminated.  */
7345796c8dcSSimon Schubert 
7355796c8dcSSimon Schubert 	  for (cur_sec = abfd->sections; cur_sec != NULL;
7365796c8dcSSimon Schubert 	       cur_sec = cur_sec->next)
7375796c8dcSSimon Schubert 	    {
7385796c8dcSSimon Schubert 	      if ((bfd_get_section_flags (abfd, cur_sec) & SEC_ALLOC) == 0)
7395796c8dcSSimon Schubert 		continue;
7405796c8dcSSimon Schubert 
7415796c8dcSSimon Schubert 	      bfd_set_section_vma (abfd, cur_sec, offsets[cur_sec->index]);
742c50c785cSJohn Marino 	      exec_set_section_address (bfd_get_filename (abfd),
743c50c785cSJohn Marino 					cur_sec->index,
7445796c8dcSSimon Schubert 					offsets[cur_sec->index]);
7455796c8dcSSimon Schubert 	      offsets[cur_sec->index] = 0;
7465796c8dcSSimon Schubert 	    }
7475796c8dcSSimon Schubert 	}
7485796c8dcSSimon Schubert     }
7495796c8dcSSimon Schubert 
7505796c8dcSSimon Schubert   /* Remember the bfd indexes for the .text, .data, .bss and
7515796c8dcSSimon Schubert      .rodata sections.  */
7525796c8dcSSimon Schubert   init_objfile_sect_indices (objfile);
7535796c8dcSSimon Schubert }
7545796c8dcSSimon Schubert 
7555796c8dcSSimon Schubert 
7565796c8dcSSimon Schubert /* Divide the file into segments, which are individual relocatable units.
7575796c8dcSSimon Schubert    This is the default version of the sym_fns.sym_segments function for
7585796c8dcSSimon Schubert    symbol readers that do not have an explicit representation of segments.
7595796c8dcSSimon Schubert    It assumes that object files do not have segments, and fully linked
7605796c8dcSSimon Schubert    files have a single segment.  */
7615796c8dcSSimon Schubert 
7625796c8dcSSimon Schubert struct symfile_segment_data *
default_symfile_segments(bfd * abfd)7635796c8dcSSimon Schubert default_symfile_segments (bfd *abfd)
7645796c8dcSSimon Schubert {
7655796c8dcSSimon Schubert   int num_sections, i;
7665796c8dcSSimon Schubert   asection *sect;
7675796c8dcSSimon Schubert   struct symfile_segment_data *data;
7685796c8dcSSimon Schubert   CORE_ADDR low, high;
7695796c8dcSSimon Schubert 
7705796c8dcSSimon Schubert   /* Relocatable files contain enough information to position each
7715796c8dcSSimon Schubert      loadable section independently; they should not be relocated
7725796c8dcSSimon Schubert      in segments.  */
7735796c8dcSSimon Schubert   if ((bfd_get_file_flags (abfd) & (EXEC_P | DYNAMIC)) == 0)
7745796c8dcSSimon Schubert     return NULL;
7755796c8dcSSimon Schubert 
7765796c8dcSSimon Schubert   /* Make sure there is at least one loadable section in the file.  */
7775796c8dcSSimon Schubert   for (sect = abfd->sections; sect != NULL; sect = sect->next)
7785796c8dcSSimon Schubert     {
7795796c8dcSSimon Schubert       if ((bfd_get_section_flags (abfd, sect) & SEC_ALLOC) == 0)
7805796c8dcSSimon Schubert 	continue;
7815796c8dcSSimon Schubert 
7825796c8dcSSimon Schubert       break;
7835796c8dcSSimon Schubert     }
7845796c8dcSSimon Schubert   if (sect == NULL)
7855796c8dcSSimon Schubert     return NULL;
7865796c8dcSSimon Schubert 
7875796c8dcSSimon Schubert   low = bfd_get_section_vma (abfd, sect);
7885796c8dcSSimon Schubert   high = low + bfd_get_section_size (sect);
7895796c8dcSSimon Schubert 
7905796c8dcSSimon Schubert   data = XZALLOC (struct symfile_segment_data);
7915796c8dcSSimon Schubert   data->num_segments = 1;
7925796c8dcSSimon Schubert   data->segment_bases = XCALLOC (1, CORE_ADDR);
7935796c8dcSSimon Schubert   data->segment_sizes = XCALLOC (1, CORE_ADDR);
7945796c8dcSSimon Schubert 
7955796c8dcSSimon Schubert   num_sections = bfd_count_sections (abfd);
7965796c8dcSSimon Schubert   data->segment_info = XCALLOC (num_sections, int);
7975796c8dcSSimon Schubert 
7985796c8dcSSimon Schubert   for (i = 0, sect = abfd->sections; sect != NULL; i++, sect = sect->next)
7995796c8dcSSimon Schubert     {
8005796c8dcSSimon Schubert       CORE_ADDR vma;
8015796c8dcSSimon Schubert 
8025796c8dcSSimon Schubert       if ((bfd_get_section_flags (abfd, sect) & SEC_ALLOC) == 0)
8035796c8dcSSimon Schubert 	continue;
8045796c8dcSSimon Schubert 
8055796c8dcSSimon Schubert       vma = bfd_get_section_vma (abfd, sect);
8065796c8dcSSimon Schubert       if (vma < low)
8075796c8dcSSimon Schubert 	low = vma;
8085796c8dcSSimon Schubert       if (vma + bfd_get_section_size (sect) > high)
8095796c8dcSSimon Schubert 	high = vma + bfd_get_section_size (sect);
8105796c8dcSSimon Schubert 
8115796c8dcSSimon Schubert       data->segment_info[i] = 1;
8125796c8dcSSimon Schubert     }
8135796c8dcSSimon Schubert 
8145796c8dcSSimon Schubert   data->segment_bases[0] = low;
8155796c8dcSSimon Schubert   data->segment_sizes[0] = high - low;
8165796c8dcSSimon Schubert 
8175796c8dcSSimon Schubert   return data;
8185796c8dcSSimon Schubert }
8195796c8dcSSimon Schubert 
820*ef5ccd6cSJohn Marino /* This is a convenience function to call sym_read for OBJFILE and
821*ef5ccd6cSJohn Marino    possibly force the partial symbols to be read.  */
822*ef5ccd6cSJohn Marino 
823*ef5ccd6cSJohn Marino static void
read_symbols(struct objfile * objfile,int add_flags)824*ef5ccd6cSJohn Marino read_symbols (struct objfile *objfile, int add_flags)
825*ef5ccd6cSJohn Marino {
826*ef5ccd6cSJohn Marino   (*objfile->sf->sym_read) (objfile, add_flags);
827*ef5ccd6cSJohn Marino 
828*ef5ccd6cSJohn Marino   /* find_separate_debug_file_in_section should be called only if there is
829*ef5ccd6cSJohn Marino      single binary with no existing separate debug info file.  */
830*ef5ccd6cSJohn Marino   if (!objfile_has_partial_symbols (objfile)
831*ef5ccd6cSJohn Marino       && objfile->separate_debug_objfile == NULL
832*ef5ccd6cSJohn Marino       && objfile->separate_debug_objfile_backlink == NULL)
833*ef5ccd6cSJohn Marino     {
834*ef5ccd6cSJohn Marino       bfd *abfd = find_separate_debug_file_in_section (objfile);
835*ef5ccd6cSJohn Marino       struct cleanup *cleanup = make_cleanup_bfd_unref (abfd);
836*ef5ccd6cSJohn Marino 
837*ef5ccd6cSJohn Marino       if (abfd != NULL)
838*ef5ccd6cSJohn Marino 	symbol_file_add_separate (abfd, add_flags, objfile);
839*ef5ccd6cSJohn Marino 
840*ef5ccd6cSJohn Marino       do_cleanups (cleanup);
841*ef5ccd6cSJohn Marino     }
842*ef5ccd6cSJohn Marino   if ((add_flags & SYMFILE_NO_READ) == 0)
843*ef5ccd6cSJohn Marino     require_partial_symbols (objfile, 0);
844*ef5ccd6cSJohn Marino }
845*ef5ccd6cSJohn Marino 
846*ef5ccd6cSJohn Marino /* Initialize entry point information for this objfile.  */
847*ef5ccd6cSJohn Marino 
848*ef5ccd6cSJohn Marino static void
init_entry_point_info(struct objfile * objfile)849*ef5ccd6cSJohn Marino init_entry_point_info (struct objfile *objfile)
850*ef5ccd6cSJohn Marino {
851*ef5ccd6cSJohn Marino   /* Save startup file's range of PC addresses to help blockframe.c
852*ef5ccd6cSJohn Marino      decide where the bottom of the stack is.  */
853*ef5ccd6cSJohn Marino 
854*ef5ccd6cSJohn Marino   if (bfd_get_file_flags (objfile->obfd) & EXEC_P)
855*ef5ccd6cSJohn Marino     {
856*ef5ccd6cSJohn Marino       /* Executable file -- record its entry point so we'll recognize
857*ef5ccd6cSJohn Marino          the startup file because it contains the entry point.  */
858*ef5ccd6cSJohn Marino       objfile->ei.entry_point = bfd_get_start_address (objfile->obfd);
859*ef5ccd6cSJohn Marino       objfile->ei.entry_point_p = 1;
860*ef5ccd6cSJohn Marino     }
861*ef5ccd6cSJohn Marino   else if (bfd_get_file_flags (objfile->obfd) & DYNAMIC
862*ef5ccd6cSJohn Marino 	   && bfd_get_start_address (objfile->obfd) != 0)
863*ef5ccd6cSJohn Marino     {
864*ef5ccd6cSJohn Marino       /* Some shared libraries may have entry points set and be
865*ef5ccd6cSJohn Marino 	 runnable.  There's no clear way to indicate this, so just check
866*ef5ccd6cSJohn Marino 	 for values other than zero.  */
867*ef5ccd6cSJohn Marino       objfile->ei.entry_point = bfd_get_start_address (objfile->obfd);
868*ef5ccd6cSJohn Marino       objfile->ei.entry_point_p = 1;
869*ef5ccd6cSJohn Marino     }
870*ef5ccd6cSJohn Marino   else
871*ef5ccd6cSJohn Marino     {
872*ef5ccd6cSJohn Marino       /* Examination of non-executable.o files.  Short-circuit this stuff.  */
873*ef5ccd6cSJohn Marino       objfile->ei.entry_point_p = 0;
874*ef5ccd6cSJohn Marino     }
875*ef5ccd6cSJohn Marino 
876*ef5ccd6cSJohn Marino   if (objfile->ei.entry_point_p)
877*ef5ccd6cSJohn Marino     {
878*ef5ccd6cSJohn Marino       CORE_ADDR entry_point =  objfile->ei.entry_point;
879*ef5ccd6cSJohn Marino 
880*ef5ccd6cSJohn Marino       /* Make certain that the address points at real code, and not a
881*ef5ccd6cSJohn Marino 	 function descriptor.  */
882*ef5ccd6cSJohn Marino       entry_point
883*ef5ccd6cSJohn Marino 	= gdbarch_convert_from_func_ptr_addr (objfile->gdbarch,
884*ef5ccd6cSJohn Marino 					      entry_point,
885*ef5ccd6cSJohn Marino 					      &current_target);
886*ef5ccd6cSJohn Marino 
887*ef5ccd6cSJohn Marino       /* Remove any ISA markers, so that this matches entries in the
888*ef5ccd6cSJohn Marino 	 symbol table.  */
889*ef5ccd6cSJohn Marino       objfile->ei.entry_point
890*ef5ccd6cSJohn Marino 	= gdbarch_addr_bits_remove (objfile->gdbarch, entry_point);
891*ef5ccd6cSJohn Marino     }
892*ef5ccd6cSJohn Marino }
893*ef5ccd6cSJohn Marino 
8945796c8dcSSimon Schubert /* Process a symbol file, as either the main file or as a dynamically
8955796c8dcSSimon Schubert    loaded file.
8965796c8dcSSimon Schubert 
897*ef5ccd6cSJohn Marino    This function does not set the OBJFILE's entry-point info.
898*ef5ccd6cSJohn Marino 
8995796c8dcSSimon Schubert    OBJFILE is where the symbols are to be read from.
9005796c8dcSSimon Schubert 
9015796c8dcSSimon Schubert    ADDRS is the list of section load addresses.  If the user has given
9025796c8dcSSimon Schubert    an 'add-symbol-file' command, then this is the list of offsets and
9035796c8dcSSimon Schubert    addresses he or she provided as arguments to the command; or, if
9045796c8dcSSimon Schubert    we're handling a shared library, these are the actual addresses the
9055796c8dcSSimon Schubert    sections are loaded at, according to the inferior's dynamic linker
9065796c8dcSSimon Schubert    (as gleaned by GDB's shared library code).  We convert each address
9075796c8dcSSimon Schubert    into an offset from the section VMA's as it appears in the object
9085796c8dcSSimon Schubert    file, and then call the file's sym_offsets function to convert this
9095796c8dcSSimon Schubert    into a format-specific offset table --- a `struct section_offsets'.
9105796c8dcSSimon Schubert    If ADDRS is non-zero, OFFSETS must be zero.
9115796c8dcSSimon Schubert 
9125796c8dcSSimon Schubert    OFFSETS is a table of section offsets already in the right
9135796c8dcSSimon Schubert    format-specific representation.  NUM_OFFSETS is the number of
9145796c8dcSSimon Schubert    elements present in OFFSETS->offsets.  If OFFSETS is non-zero, we
9155796c8dcSSimon Schubert    assume this is the proper table the call to sym_offsets described
9165796c8dcSSimon Schubert    above would produce.  Instead of calling sym_offsets, we just dump
9175796c8dcSSimon Schubert    it right into objfile->section_offsets.  (When we're re-reading
9185796c8dcSSimon Schubert    symbols from an objfile, we don't have the original load address
9195796c8dcSSimon Schubert    list any more; all we have is the section offset table.)  If
9205796c8dcSSimon Schubert    OFFSETS is non-zero, ADDRS must be zero.
9215796c8dcSSimon Schubert 
9225796c8dcSSimon Schubert    ADD_FLAGS encodes verbosity level, whether this is main symbol or
9235796c8dcSSimon Schubert    an extra symbol file such as dynamically loaded code, and wether
9245796c8dcSSimon Schubert    breakpoint reset should be deferred.  */
9255796c8dcSSimon Schubert 
926*ef5ccd6cSJohn Marino static void
syms_from_objfile_1(struct objfile * objfile,struct section_addr_info * addrs,struct section_offsets * offsets,int num_offsets,int add_flags)927*ef5ccd6cSJohn Marino syms_from_objfile_1 (struct objfile *objfile,
9285796c8dcSSimon Schubert 		     struct section_addr_info *addrs,
9295796c8dcSSimon Schubert 		     struct section_offsets *offsets,
9305796c8dcSSimon Schubert 		     int num_offsets,
9315796c8dcSSimon Schubert 		     int add_flags)
9325796c8dcSSimon Schubert {
9335796c8dcSSimon Schubert   struct section_addr_info *local_addr = NULL;
9345796c8dcSSimon Schubert   struct cleanup *old_chain;
9355796c8dcSSimon Schubert   const int mainline = add_flags & SYMFILE_MAINLINE;
9365796c8dcSSimon Schubert 
9375796c8dcSSimon Schubert   gdb_assert (! (addrs && offsets));
9385796c8dcSSimon Schubert 
9395796c8dcSSimon Schubert   objfile->sf = find_sym_fns (objfile->obfd);
9405796c8dcSSimon Schubert 
9415796c8dcSSimon Schubert   if (objfile->sf == NULL)
942*ef5ccd6cSJohn Marino     {
943*ef5ccd6cSJohn Marino       /* No symbols to load, but we still need to make sure
944*ef5ccd6cSJohn Marino 	 that the section_offsets table is allocated.  */
945*ef5ccd6cSJohn Marino       int num_sections = bfd_count_sections (objfile->obfd);
946*ef5ccd6cSJohn Marino       size_t size = SIZEOF_N_SECTION_OFFSETS (num_offsets);
947*ef5ccd6cSJohn Marino 
948*ef5ccd6cSJohn Marino       objfile->num_sections = num_sections;
949*ef5ccd6cSJohn Marino       objfile->section_offsets
950*ef5ccd6cSJohn Marino         = obstack_alloc (&objfile->objfile_obstack, size);
951*ef5ccd6cSJohn Marino       memset (objfile->section_offsets, 0, size);
952*ef5ccd6cSJohn Marino       return;
953*ef5ccd6cSJohn Marino     }
9545796c8dcSSimon Schubert 
9555796c8dcSSimon Schubert   /* Make sure that partially constructed symbol tables will be cleaned up
9565796c8dcSSimon Schubert      if an error occurs during symbol reading.  */
9575796c8dcSSimon Schubert   old_chain = make_cleanup_free_objfile (objfile);
9585796c8dcSSimon Schubert 
9595796c8dcSSimon Schubert   /* If ADDRS and OFFSETS are both NULL, put together a dummy address
9605796c8dcSSimon Schubert      list.  We now establish the convention that an addr of zero means
9615796c8dcSSimon Schubert      no load address was specified.  */
9625796c8dcSSimon Schubert   if (! addrs && ! offsets)
9635796c8dcSSimon Schubert     {
9645796c8dcSSimon Schubert       local_addr
9655796c8dcSSimon Schubert 	= alloc_section_addr_info (bfd_count_sections (objfile->obfd));
9665796c8dcSSimon Schubert       make_cleanup (xfree, local_addr);
9675796c8dcSSimon Schubert       addrs = local_addr;
9685796c8dcSSimon Schubert     }
9695796c8dcSSimon Schubert 
9705796c8dcSSimon Schubert   /* Now either addrs or offsets is non-zero.  */
9715796c8dcSSimon Schubert 
9725796c8dcSSimon Schubert   if (mainline)
9735796c8dcSSimon Schubert     {
9745796c8dcSSimon Schubert       /* We will modify the main symbol table, make sure that all its users
9755796c8dcSSimon Schubert          will be cleaned up if an error occurs during symbol reading.  */
9765796c8dcSSimon Schubert       make_cleanup (clear_symtab_users_cleanup, 0 /*ignore*/);
9775796c8dcSSimon Schubert 
9785796c8dcSSimon Schubert       /* Since no error yet, throw away the old symbol table.  */
9795796c8dcSSimon Schubert 
9805796c8dcSSimon Schubert       if (symfile_objfile != NULL)
9815796c8dcSSimon Schubert 	{
9825796c8dcSSimon Schubert 	  free_objfile (symfile_objfile);
9835796c8dcSSimon Schubert 	  gdb_assert (symfile_objfile == NULL);
9845796c8dcSSimon Schubert 	}
9855796c8dcSSimon Schubert 
9865796c8dcSSimon Schubert       /* Currently we keep symbols from the add-symbol-file command.
9875796c8dcSSimon Schubert          If the user wants to get rid of them, they should do "symbol-file"
9885796c8dcSSimon Schubert          without arguments first.  Not sure this is the best behavior
9895796c8dcSSimon Schubert          (PR 2207).  */
9905796c8dcSSimon Schubert 
9915796c8dcSSimon Schubert       (*objfile->sf->sym_new_init) (objfile);
9925796c8dcSSimon Schubert     }
9935796c8dcSSimon Schubert 
9945796c8dcSSimon Schubert   /* Convert addr into an offset rather than an absolute address.
9955796c8dcSSimon Schubert      We find the lowest address of a loaded segment in the objfile,
9965796c8dcSSimon Schubert      and assume that <addr> is where that got loaded.
9975796c8dcSSimon Schubert 
9985796c8dcSSimon Schubert      We no longer warn if the lowest section is not a text segment (as
9995796c8dcSSimon Schubert      happens for the PA64 port.  */
1000cf7f2e2dSJohn Marino   if (addrs && addrs->other[0].name)
1001cf7f2e2dSJohn Marino     addr_info_make_relative (addrs, objfile->obfd);
10025796c8dcSSimon Schubert 
10035796c8dcSSimon Schubert   /* Initialize symbol reading routines for this objfile, allow complaints to
10045796c8dcSSimon Schubert      appear for this new file, and record how verbose to be, then do the
10055796c8dcSSimon Schubert      initial symbol reading for this file.  */
10065796c8dcSSimon Schubert 
10075796c8dcSSimon Schubert   (*objfile->sf->sym_init) (objfile);
10085796c8dcSSimon Schubert   clear_complaints (&symfile_complaints, 1, add_flags & SYMFILE_VERBOSE);
10095796c8dcSSimon Schubert 
10105796c8dcSSimon Schubert   if (addrs)
10115796c8dcSSimon Schubert     (*objfile->sf->sym_offsets) (objfile, addrs);
10125796c8dcSSimon Schubert   else
10135796c8dcSSimon Schubert     {
10145796c8dcSSimon Schubert       size_t size = SIZEOF_N_SECTION_OFFSETS (num_offsets);
10155796c8dcSSimon Schubert 
10165796c8dcSSimon Schubert       /* Just copy in the offset table directly as given to us.  */
10175796c8dcSSimon Schubert       objfile->num_sections = num_offsets;
10185796c8dcSSimon Schubert       objfile->section_offsets
10195796c8dcSSimon Schubert         = ((struct section_offsets *)
10205796c8dcSSimon Schubert            obstack_alloc (&objfile->objfile_obstack, size));
10215796c8dcSSimon Schubert       memcpy (objfile->section_offsets, offsets, size);
10225796c8dcSSimon Schubert 
10235796c8dcSSimon Schubert       init_objfile_sect_indices (objfile);
10245796c8dcSSimon Schubert     }
10255796c8dcSSimon Schubert 
1026*ef5ccd6cSJohn Marino   read_symbols (objfile, add_flags);
1027c50c785cSJohn Marino 
10285796c8dcSSimon Schubert   /* Discard cleanups as symbol reading was successful.  */
10295796c8dcSSimon Schubert 
10305796c8dcSSimon Schubert   discard_cleanups (old_chain);
10315796c8dcSSimon Schubert   xfree (local_addr);
10325796c8dcSSimon Schubert }
10335796c8dcSSimon Schubert 
1034*ef5ccd6cSJohn Marino /* Same as syms_from_objfile_1, but also initializes the objfile
1035*ef5ccd6cSJohn Marino    entry-point info.  */
1036*ef5ccd6cSJohn Marino 
1037*ef5ccd6cSJohn Marino void
syms_from_objfile(struct objfile * objfile,struct section_addr_info * addrs,struct section_offsets * offsets,int num_offsets,int add_flags)1038*ef5ccd6cSJohn Marino syms_from_objfile (struct objfile *objfile,
1039*ef5ccd6cSJohn Marino 		   struct section_addr_info *addrs,
1040*ef5ccd6cSJohn Marino 		   struct section_offsets *offsets,
1041*ef5ccd6cSJohn Marino 		   int num_offsets,
1042*ef5ccd6cSJohn Marino 		   int add_flags)
1043*ef5ccd6cSJohn Marino {
1044*ef5ccd6cSJohn Marino   syms_from_objfile_1 (objfile, addrs, offsets, num_offsets, add_flags);
1045*ef5ccd6cSJohn Marino   init_entry_point_info (objfile);
1046*ef5ccd6cSJohn Marino }
1047*ef5ccd6cSJohn Marino 
10485796c8dcSSimon Schubert /* Perform required actions after either reading in the initial
10495796c8dcSSimon Schubert    symbols for a new objfile, or mapping in the symbols from a reusable
1050c50c785cSJohn Marino    objfile.  ADD_FLAGS is a bitmask of enum symfile_add_flags.  */
10515796c8dcSSimon Schubert 
10525796c8dcSSimon Schubert void
new_symfile_objfile(struct objfile * objfile,int add_flags)10535796c8dcSSimon Schubert new_symfile_objfile (struct objfile *objfile, int add_flags)
10545796c8dcSSimon Schubert {
10555796c8dcSSimon Schubert   /* If this is the main symbol file we have to clean up all users of the
10565796c8dcSSimon Schubert      old main symbol file.  Otherwise it is sufficient to fixup all the
10575796c8dcSSimon Schubert      breakpoints that may have been redefined by this symbol file.  */
10585796c8dcSSimon Schubert   if (add_flags & SYMFILE_MAINLINE)
10595796c8dcSSimon Schubert     {
10605796c8dcSSimon Schubert       /* OK, make it the "real" symbol file.  */
10615796c8dcSSimon Schubert       symfile_objfile = objfile;
10625796c8dcSSimon Schubert 
1063c50c785cSJohn Marino       clear_symtab_users (add_flags);
10645796c8dcSSimon Schubert     }
10655796c8dcSSimon Schubert   else if ((add_flags & SYMFILE_DEFER_BP_RESET) == 0)
10665796c8dcSSimon Schubert     {
10675796c8dcSSimon Schubert       breakpoint_re_set ();
10685796c8dcSSimon Schubert     }
10695796c8dcSSimon Schubert 
10705796c8dcSSimon Schubert   /* We're done reading the symbol file; finish off complaints.  */
10715796c8dcSSimon Schubert   clear_complaints (&symfile_complaints, 0, add_flags & SYMFILE_VERBOSE);
10725796c8dcSSimon Schubert }
10735796c8dcSSimon Schubert 
10745796c8dcSSimon Schubert /* Process a symbol file, as either the main file or as a dynamically
10755796c8dcSSimon Schubert    loaded file.
10765796c8dcSSimon Schubert 
10775796c8dcSSimon Schubert    ABFD is a BFD already open on the file, as from symfile_bfd_open.
1078*ef5ccd6cSJohn Marino    A new reference is acquired by this function.
10795796c8dcSSimon Schubert 
10805796c8dcSSimon Schubert    ADD_FLAGS encodes verbosity, whether this is main symbol file or
10815796c8dcSSimon Schubert    extra, such as dynamically loaded code, and what to do with breakpoins.
10825796c8dcSSimon Schubert 
10835796c8dcSSimon Schubert    ADDRS, OFFSETS, and NUM_OFFSETS are as described for
10845796c8dcSSimon Schubert    syms_from_objfile, above.
10855796c8dcSSimon Schubert    ADDRS is ignored when SYMFILE_MAINLINE bit is set in ADD_FLAGS.
10865796c8dcSSimon Schubert 
1087a45ae5f8SJohn Marino    PARENT is the original objfile if ABFD is a separate debug info file.
1088a45ae5f8SJohn Marino    Otherwise PARENT is NULL.
1089a45ae5f8SJohn Marino 
10905796c8dcSSimon Schubert    Upon success, returns a pointer to the objfile that was added.
10915796c8dcSSimon Schubert    Upon failure, jumps back to command level (never returns).  */
10925796c8dcSSimon Schubert 
10935796c8dcSSimon Schubert static struct objfile *
symbol_file_add_with_addrs_or_offsets(bfd * abfd,int add_flags,struct section_addr_info * addrs,struct section_offsets * offsets,int num_offsets,int flags,struct objfile * parent)10945796c8dcSSimon Schubert symbol_file_add_with_addrs_or_offsets (bfd *abfd,
10955796c8dcSSimon Schubert                                        int add_flags,
10965796c8dcSSimon Schubert                                        struct section_addr_info *addrs,
10975796c8dcSSimon Schubert                                        struct section_offsets *offsets,
10985796c8dcSSimon Schubert                                        int num_offsets,
1099a45ae5f8SJohn Marino                                        int flags, struct objfile *parent)
11005796c8dcSSimon Schubert {
11015796c8dcSSimon Schubert   struct objfile *objfile;
11025796c8dcSSimon Schubert   const char *name = bfd_get_filename (abfd);
11035796c8dcSSimon Schubert   const int from_tty = add_flags & SYMFILE_VERBOSE;
1104a45ae5f8SJohn Marino   const int mainline = add_flags & SYMFILE_MAINLINE;
1105c50c785cSJohn Marino   const int should_print = ((from_tty || info_verbose)
1106c50c785cSJohn Marino 			    && (readnow_symbol_files
1107c50c785cSJohn Marino 				|| (add_flags & SYMFILE_NO_READ) == 0));
1108c50c785cSJohn Marino 
1109c50c785cSJohn Marino   if (readnow_symbol_files)
1110c50c785cSJohn Marino     {
1111c50c785cSJohn Marino       flags |= OBJF_READNOW;
1112c50c785cSJohn Marino       add_flags &= ~SYMFILE_NO_READ;
1113c50c785cSJohn Marino     }
11145796c8dcSSimon Schubert 
11155796c8dcSSimon Schubert   /* Give user a chance to burp if we'd be
11165796c8dcSSimon Schubert      interactively wiping out any existing symbols.  */
11175796c8dcSSimon Schubert 
11185796c8dcSSimon Schubert   if ((have_full_symbols () || have_partial_symbols ())
1119a45ae5f8SJohn Marino       && mainline
11205796c8dcSSimon Schubert       && from_tty
11215796c8dcSSimon Schubert       && !query (_("Load new symbol table from \"%s\"? "), name))
11225796c8dcSSimon Schubert     error (_("Not confirmed."));
11235796c8dcSSimon Schubert 
1124a45ae5f8SJohn Marino   objfile = allocate_objfile (abfd, flags | (mainline ? OBJF_MAINLINE : 0));
11255796c8dcSSimon Schubert 
1126a45ae5f8SJohn Marino   if (parent)
1127a45ae5f8SJohn Marino     add_separate_debug_objfile (objfile, parent);
1128a45ae5f8SJohn Marino 
11295796c8dcSSimon Schubert   /* We either created a new mapped symbol table, mapped an existing
11305796c8dcSSimon Schubert      symbol table file which has not had initial symbol reading
11315796c8dcSSimon Schubert      performed, or need to read an unmapped symbol table.  */
1132c50c785cSJohn Marino   if (should_print)
11335796c8dcSSimon Schubert     {
11345796c8dcSSimon Schubert       if (deprecated_pre_add_symbol_hook)
11355796c8dcSSimon Schubert 	deprecated_pre_add_symbol_hook (name);
11365796c8dcSSimon Schubert       else
11375796c8dcSSimon Schubert 	{
11385796c8dcSSimon Schubert 	  printf_unfiltered (_("Reading symbols from %s..."), name);
11395796c8dcSSimon Schubert 	  wrap_here ("");
11405796c8dcSSimon Schubert 	  gdb_flush (gdb_stdout);
11415796c8dcSSimon Schubert 	}
11425796c8dcSSimon Schubert     }
11435796c8dcSSimon Schubert   syms_from_objfile (objfile, addrs, offsets, num_offsets,
11445796c8dcSSimon Schubert 		     add_flags);
11455796c8dcSSimon Schubert 
11465796c8dcSSimon Schubert   /* We now have at least a partial symbol table.  Check to see if the
11475796c8dcSSimon Schubert      user requested that all symbols be read on initial access via either
11485796c8dcSSimon Schubert      the gdb startup command line or on a per symbol file basis.  Expand
11495796c8dcSSimon Schubert      all partial symbol tables for this objfile if so.  */
11505796c8dcSSimon Schubert 
1151c50c785cSJohn Marino   if ((flags & OBJF_READNOW))
11525796c8dcSSimon Schubert     {
1153c50c785cSJohn Marino       if (should_print)
11545796c8dcSSimon Schubert 	{
11555796c8dcSSimon Schubert 	  printf_unfiltered (_("expanding to full symbols..."));
11565796c8dcSSimon Schubert 	  wrap_here ("");
11575796c8dcSSimon Schubert 	  gdb_flush (gdb_stdout);
11585796c8dcSSimon Schubert 	}
11595796c8dcSSimon Schubert 
1160cf7f2e2dSJohn Marino       if (objfile->sf)
1161cf7f2e2dSJohn Marino 	objfile->sf->qf->expand_all_symtabs (objfile);
11625796c8dcSSimon Schubert     }
11635796c8dcSSimon Schubert 
1164c50c785cSJohn Marino   if (should_print && !objfile_has_symbols (objfile))
11655796c8dcSSimon Schubert     {
11665796c8dcSSimon Schubert       wrap_here ("");
11675796c8dcSSimon Schubert       printf_unfiltered (_("(no debugging symbols found)..."));
11685796c8dcSSimon Schubert       wrap_here ("");
11695796c8dcSSimon Schubert     }
11705796c8dcSSimon Schubert 
1171c50c785cSJohn Marino   if (should_print)
11725796c8dcSSimon Schubert     {
11735796c8dcSSimon Schubert       if (deprecated_post_add_symbol_hook)
11745796c8dcSSimon Schubert 	deprecated_post_add_symbol_hook ();
11755796c8dcSSimon Schubert       else
11765796c8dcSSimon Schubert 	printf_unfiltered (_("done.\n"));
11775796c8dcSSimon Schubert     }
11785796c8dcSSimon Schubert 
11795796c8dcSSimon Schubert   /* We print some messages regardless of whether 'from_tty ||
11805796c8dcSSimon Schubert      info_verbose' is true, so make sure they go out at the right
11815796c8dcSSimon Schubert      time.  */
11825796c8dcSSimon Schubert   gdb_flush (gdb_stdout);
11835796c8dcSSimon Schubert 
11845796c8dcSSimon Schubert   if (objfile->sf == NULL)
11855796c8dcSSimon Schubert     {
11865796c8dcSSimon Schubert       observer_notify_new_objfile (objfile);
11875796c8dcSSimon Schubert       return objfile;	/* No symbols.  */
11885796c8dcSSimon Schubert     }
11895796c8dcSSimon Schubert 
11905796c8dcSSimon Schubert   new_symfile_objfile (objfile, add_flags);
11915796c8dcSSimon Schubert 
11925796c8dcSSimon Schubert   observer_notify_new_objfile (objfile);
11935796c8dcSSimon Schubert 
11945796c8dcSSimon Schubert   bfd_cache_close_all ();
11955796c8dcSSimon Schubert   return (objfile);
11965796c8dcSSimon Schubert }
11975796c8dcSSimon Schubert 
1198cf7f2e2dSJohn Marino /* Add BFD as a separate debug file for OBJFILE.  */
1199cf7f2e2dSJohn Marino 
1200cf7f2e2dSJohn Marino void
symbol_file_add_separate(bfd * bfd,int symfile_flags,struct objfile * objfile)1201cf7f2e2dSJohn Marino symbol_file_add_separate (bfd *bfd, int symfile_flags, struct objfile *objfile)
1202cf7f2e2dSJohn Marino {
1203cf7f2e2dSJohn Marino   struct objfile *new_objfile;
1204cf7f2e2dSJohn Marino   struct section_addr_info *sap;
1205cf7f2e2dSJohn Marino   struct cleanup *my_cleanup;
1206cf7f2e2dSJohn Marino 
1207cf7f2e2dSJohn Marino   /* Create section_addr_info.  We can't directly use offsets from OBJFILE
1208cf7f2e2dSJohn Marino      because sections of BFD may not match sections of OBJFILE and because
1209cf7f2e2dSJohn Marino      vma may have been modified by tools such as prelink.  */
1210cf7f2e2dSJohn Marino   sap = build_section_addr_info_from_objfile (objfile);
1211cf7f2e2dSJohn Marino   my_cleanup = make_cleanup_free_section_addr_info (sap);
1212cf7f2e2dSJohn Marino 
1213cf7f2e2dSJohn Marino   new_objfile = symbol_file_add_with_addrs_or_offsets
1214cf7f2e2dSJohn Marino     (bfd, symfile_flags,
1215cf7f2e2dSJohn Marino      sap, NULL, 0,
1216cf7f2e2dSJohn Marino      objfile->flags & (OBJF_REORDERED | OBJF_SHARED | OBJF_READNOW
1217a45ae5f8SJohn Marino 		       | OBJF_USERLOADED),
1218a45ae5f8SJohn Marino      objfile);
1219cf7f2e2dSJohn Marino 
1220cf7f2e2dSJohn Marino   do_cleanups (my_cleanup);
1221cf7f2e2dSJohn Marino }
12225796c8dcSSimon Schubert 
12235796c8dcSSimon Schubert /* Process the symbol file ABFD, as either the main file or as a
12245796c8dcSSimon Schubert    dynamically loaded file.
12255796c8dcSSimon Schubert 
12265796c8dcSSimon Schubert    See symbol_file_add_with_addrs_or_offsets's comments for
12275796c8dcSSimon Schubert    details.  */
12285796c8dcSSimon Schubert struct objfile *
symbol_file_add_from_bfd(bfd * abfd,int add_flags,struct section_addr_info * addrs,int flags,struct objfile * parent)12295796c8dcSSimon Schubert symbol_file_add_from_bfd (bfd *abfd, int add_flags,
12305796c8dcSSimon Schubert                           struct section_addr_info *addrs,
1231a45ae5f8SJohn Marino                           int flags, struct objfile *parent)
12325796c8dcSSimon Schubert {
12335796c8dcSSimon Schubert   return symbol_file_add_with_addrs_or_offsets (abfd, add_flags, addrs, 0, 0,
1234a45ae5f8SJohn Marino                                                 flags, parent);
12355796c8dcSSimon Schubert }
12365796c8dcSSimon Schubert 
12375796c8dcSSimon Schubert 
12385796c8dcSSimon Schubert /* Process a symbol file, as either the main file or as a dynamically
12395796c8dcSSimon Schubert    loaded file.  See symbol_file_add_with_addrs_or_offsets's comments
12405796c8dcSSimon Schubert    for details.  */
12415796c8dcSSimon Schubert struct objfile *
symbol_file_add(char * name,int add_flags,struct section_addr_info * addrs,int flags)12425796c8dcSSimon Schubert symbol_file_add (char *name, int add_flags, struct section_addr_info *addrs,
12435796c8dcSSimon Schubert 		 int flags)
12445796c8dcSSimon Schubert {
1245*ef5ccd6cSJohn Marino   bfd *bfd = symfile_bfd_open (name);
1246*ef5ccd6cSJohn Marino   struct cleanup *cleanup = make_cleanup_bfd_unref (bfd);
1247*ef5ccd6cSJohn Marino   struct objfile *objf;
1248*ef5ccd6cSJohn Marino 
1249*ef5ccd6cSJohn Marino   objf = symbol_file_add_from_bfd (bfd, add_flags, addrs, flags, NULL);
1250*ef5ccd6cSJohn Marino   do_cleanups (cleanup);
1251*ef5ccd6cSJohn Marino   return objf;
12525796c8dcSSimon Schubert }
12535796c8dcSSimon Schubert 
12545796c8dcSSimon Schubert 
12555796c8dcSSimon Schubert /* Call symbol_file_add() with default values and update whatever is
12565796c8dcSSimon Schubert    affected by the loading of a new main().
12575796c8dcSSimon Schubert    Used when the file is supplied in the gdb command line
12585796c8dcSSimon Schubert    and by some targets with special loading requirements.
12595796c8dcSSimon Schubert    The auxiliary function, symbol_file_add_main_1(), has the flags
12605796c8dcSSimon Schubert    argument for the switches that can only be specified in the symbol_file
12615796c8dcSSimon Schubert    command itself.  */
12625796c8dcSSimon Schubert 
12635796c8dcSSimon Schubert void
symbol_file_add_main(char * args,int from_tty)12645796c8dcSSimon Schubert symbol_file_add_main (char *args, int from_tty)
12655796c8dcSSimon Schubert {
12665796c8dcSSimon Schubert   symbol_file_add_main_1 (args, from_tty, 0);
12675796c8dcSSimon Schubert }
12685796c8dcSSimon Schubert 
12695796c8dcSSimon Schubert static void
symbol_file_add_main_1(char * args,int from_tty,int flags)12705796c8dcSSimon Schubert symbol_file_add_main_1 (char *args, int from_tty, int flags)
12715796c8dcSSimon Schubert {
1272*ef5ccd6cSJohn Marino   const int add_flags = (current_inferior ()->symfile_flags
1273*ef5ccd6cSJohn Marino 			 | SYMFILE_MAINLINE | (from_tty ? SYMFILE_VERBOSE : 0));
1274*ef5ccd6cSJohn Marino 
12755796c8dcSSimon Schubert   symbol_file_add (args, add_flags, NULL, flags);
12765796c8dcSSimon Schubert 
12775796c8dcSSimon Schubert   /* Getting new symbols may change our opinion about
12785796c8dcSSimon Schubert      what is frameless.  */
12795796c8dcSSimon Schubert   reinit_frame_cache ();
12805796c8dcSSimon Schubert 
1281*ef5ccd6cSJohn Marino   if ((flags & SYMFILE_NO_READ) == 0)
12825796c8dcSSimon Schubert     set_initial_language ();
12835796c8dcSSimon Schubert }
12845796c8dcSSimon Schubert 
12855796c8dcSSimon Schubert void
symbol_file_clear(int from_tty)12865796c8dcSSimon Schubert symbol_file_clear (int from_tty)
12875796c8dcSSimon Schubert {
12885796c8dcSSimon Schubert   if ((have_full_symbols () || have_partial_symbols ())
12895796c8dcSSimon Schubert       && from_tty
12905796c8dcSSimon Schubert       && (symfile_objfile
12915796c8dcSSimon Schubert 	  ? !query (_("Discard symbol table from `%s'? "),
12925796c8dcSSimon Schubert 		    symfile_objfile->name)
12935796c8dcSSimon Schubert 	  : !query (_("Discard symbol table? "))))
12945796c8dcSSimon Schubert     error (_("Not confirmed."));
12955796c8dcSSimon Schubert 
1296cf7f2e2dSJohn Marino   /* solib descriptors may have handles to objfiles.  Wipe them before their
1297cf7f2e2dSJohn Marino      objfiles get stale by free_all_objfiles.  */
12985796c8dcSSimon Schubert   no_shared_libraries (NULL, from_tty);
12995796c8dcSSimon Schubert 
1300cf7f2e2dSJohn Marino   free_all_objfiles ();
1301cf7f2e2dSJohn Marino 
13025796c8dcSSimon Schubert   gdb_assert (symfile_objfile == NULL);
13035796c8dcSSimon Schubert   if (from_tty)
13045796c8dcSSimon Schubert     printf_unfiltered (_("No symbol file now.\n"));
13055796c8dcSSimon Schubert }
13065796c8dcSSimon Schubert 
13075796c8dcSSimon Schubert static char *
get_debug_link_info(struct objfile * objfile,unsigned long * crc32_out)13085796c8dcSSimon Schubert get_debug_link_info (struct objfile *objfile, unsigned long *crc32_out)
13095796c8dcSSimon Schubert {
13105796c8dcSSimon Schubert   asection *sect;
13115796c8dcSSimon Schubert   bfd_size_type debuglink_size;
13125796c8dcSSimon Schubert   unsigned long crc32;
13135796c8dcSSimon Schubert   char *contents;
13145796c8dcSSimon Schubert   int crc_offset;
13155796c8dcSSimon Schubert 
13165796c8dcSSimon Schubert   sect = bfd_get_section_by_name (objfile->obfd, ".gnu_debuglink");
13175796c8dcSSimon Schubert 
13185796c8dcSSimon Schubert   if (sect == NULL)
13195796c8dcSSimon Schubert     return NULL;
13205796c8dcSSimon Schubert 
13215796c8dcSSimon Schubert   debuglink_size = bfd_section_size (objfile->obfd, sect);
13225796c8dcSSimon Schubert 
13235796c8dcSSimon Schubert   contents = xmalloc (debuglink_size);
13245796c8dcSSimon Schubert   bfd_get_section_contents (objfile->obfd, sect, contents,
13255796c8dcSSimon Schubert 			    (file_ptr)0, (bfd_size_type)debuglink_size);
13265796c8dcSSimon Schubert 
13275796c8dcSSimon Schubert   /* Crc value is stored after the filename, aligned up to 4 bytes.  */
13285796c8dcSSimon Schubert   crc_offset = strlen (contents) + 1;
13295796c8dcSSimon Schubert   crc_offset = (crc_offset + 3) & ~3;
13305796c8dcSSimon Schubert 
13315796c8dcSSimon Schubert   crc32 = bfd_get_32 (objfile->obfd, (bfd_byte *) (contents + crc_offset));
13325796c8dcSSimon Schubert 
13335796c8dcSSimon Schubert   *crc32_out = crc32;
13345796c8dcSSimon Schubert   return contents;
13355796c8dcSSimon Schubert }
13365796c8dcSSimon Schubert 
1337a45ae5f8SJohn Marino /* Return 32-bit CRC for ABFD.  If successful store it to *FILE_CRC_RETURN and
1338a45ae5f8SJohn Marino    return 1.  Otherwise print a warning and return 0.  ABFD seek position is
1339a45ae5f8SJohn Marino    not preserved.  */
1340a45ae5f8SJohn Marino 
1341a45ae5f8SJohn Marino static int
get_file_crc(bfd * abfd,unsigned long * file_crc_return)1342a45ae5f8SJohn Marino get_file_crc (bfd *abfd, unsigned long *file_crc_return)
1343a45ae5f8SJohn Marino {
1344a45ae5f8SJohn Marino   unsigned long file_crc = 0;
1345a45ae5f8SJohn Marino 
1346a45ae5f8SJohn Marino   if (bfd_seek (abfd, 0, SEEK_SET) != 0)
1347a45ae5f8SJohn Marino     {
1348a45ae5f8SJohn Marino       warning (_("Problem reading \"%s\" for CRC: %s"),
1349a45ae5f8SJohn Marino 	       bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
1350a45ae5f8SJohn Marino       return 0;
1351a45ae5f8SJohn Marino     }
1352a45ae5f8SJohn Marino 
1353a45ae5f8SJohn Marino   for (;;)
1354a45ae5f8SJohn Marino     {
1355a45ae5f8SJohn Marino       gdb_byte buffer[8 * 1024];
1356a45ae5f8SJohn Marino       bfd_size_type count;
1357a45ae5f8SJohn Marino 
1358a45ae5f8SJohn Marino       count = bfd_bread (buffer, sizeof (buffer), abfd);
1359a45ae5f8SJohn Marino       if (count == (bfd_size_type) -1)
1360a45ae5f8SJohn Marino 	{
1361a45ae5f8SJohn Marino 	  warning (_("Problem reading \"%s\" for CRC: %s"),
1362a45ae5f8SJohn Marino 		   bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
1363a45ae5f8SJohn Marino 	  return 0;
1364a45ae5f8SJohn Marino 	}
1365a45ae5f8SJohn Marino       if (count == 0)
1366a45ae5f8SJohn Marino 	break;
1367*ef5ccd6cSJohn Marino       file_crc = bfd_calc_gnu_debuglink_crc32 (file_crc, buffer, count);
1368a45ae5f8SJohn Marino     }
1369a45ae5f8SJohn Marino 
1370a45ae5f8SJohn Marino   *file_crc_return = file_crc;
1371a45ae5f8SJohn Marino   return 1;
1372a45ae5f8SJohn Marino }
1373a45ae5f8SJohn Marino 
13745796c8dcSSimon Schubert static int
separate_debug_file_exists(const char * name,unsigned long crc,struct objfile * parent_objfile)1375cf7f2e2dSJohn Marino separate_debug_file_exists (const char *name, unsigned long crc,
1376cf7f2e2dSJohn Marino 			    struct objfile *parent_objfile)
13775796c8dcSSimon Schubert {
1378a45ae5f8SJohn Marino   unsigned long file_crc;
1379a45ae5f8SJohn Marino   int file_crc_p;
13805796c8dcSSimon Schubert   bfd *abfd;
1381cf7f2e2dSJohn Marino   struct stat parent_stat, abfd_stat;
1382a45ae5f8SJohn Marino   int verified_as_different;
13835796c8dcSSimon Schubert 
1384cf7f2e2dSJohn Marino   /* Find a separate debug info file as if symbols would be present in
1385cf7f2e2dSJohn Marino      PARENT_OBJFILE itself this function would not be called.  .gnu_debuglink
1386cf7f2e2dSJohn Marino      section can contain just the basename of PARENT_OBJFILE without any
1387cf7f2e2dSJohn Marino      ".debug" suffix as "/usr/lib/debug/path/to/file" is a separate tree where
1388cf7f2e2dSJohn Marino      the separate debug infos with the same basename can exist.  */
1389cf7f2e2dSJohn Marino 
1390c50c785cSJohn Marino   if (filename_cmp (name, parent_objfile->name) == 0)
1391cf7f2e2dSJohn Marino     return 0;
1392cf7f2e2dSJohn Marino 
1393*ef5ccd6cSJohn Marino   abfd = gdb_bfd_open_maybe_remote (name);
13945796c8dcSSimon Schubert 
13955796c8dcSSimon Schubert   if (!abfd)
13965796c8dcSSimon Schubert     return 0;
13975796c8dcSSimon Schubert 
1398c50c785cSJohn Marino   /* Verify symlinks were not the cause of filename_cmp name difference above.
1399cf7f2e2dSJohn Marino 
1400cf7f2e2dSJohn Marino      Some operating systems, e.g. Windows, do not provide a meaningful
1401cf7f2e2dSJohn Marino      st_ino; they always set it to zero.  (Windows does provide a
1402cf7f2e2dSJohn Marino      meaningful st_dev.)  Do not indicate a duplicate library in that
1403cf7f2e2dSJohn Marino      case.  While there is no guarantee that a system that provides
1404cf7f2e2dSJohn Marino      meaningful inode numbers will never set st_ino to zero, this is
1405cf7f2e2dSJohn Marino      merely an optimization, so we do not need to worry about false
1406cf7f2e2dSJohn Marino      negatives.  */
1407cf7f2e2dSJohn Marino 
1408cf7f2e2dSJohn Marino   if (bfd_stat (abfd, &abfd_stat) == 0
1409a45ae5f8SJohn Marino       && abfd_stat.st_ino != 0
1410a45ae5f8SJohn Marino       && bfd_stat (parent_objfile->obfd, &parent_stat) == 0)
1411a45ae5f8SJohn Marino     {
1412a45ae5f8SJohn Marino       if (abfd_stat.st_dev == parent_stat.st_dev
1413a45ae5f8SJohn Marino 	  && abfd_stat.st_ino == parent_stat.st_ino)
1414cf7f2e2dSJohn Marino 	{
1415*ef5ccd6cSJohn Marino 	  gdb_bfd_unref (abfd);
1416cf7f2e2dSJohn Marino 	  return 0;
1417cf7f2e2dSJohn Marino 	}
1418a45ae5f8SJohn Marino       verified_as_different = 1;
1419a45ae5f8SJohn Marino     }
1420a45ae5f8SJohn Marino   else
1421a45ae5f8SJohn Marino     verified_as_different = 0;
1422cf7f2e2dSJohn Marino 
1423a45ae5f8SJohn Marino   file_crc_p = get_file_crc (abfd, &file_crc);
14245796c8dcSSimon Schubert 
1425*ef5ccd6cSJohn Marino   gdb_bfd_unref (abfd);
14265796c8dcSSimon Schubert 
1427a45ae5f8SJohn Marino   if (!file_crc_p)
1428a45ae5f8SJohn Marino     return 0;
1429a45ae5f8SJohn Marino 
1430cf7f2e2dSJohn Marino   if (crc != file_crc)
1431cf7f2e2dSJohn Marino     {
1432a45ae5f8SJohn Marino       /* If one (or both) the files are accessed for example the via "remote:"
1433a45ae5f8SJohn Marino 	 gdbserver way it does not support the bfd_stat operation.  Verify
1434a45ae5f8SJohn Marino 	 whether those two files are not the same manually.  */
1435a45ae5f8SJohn Marino 
1436a45ae5f8SJohn Marino       if (!verified_as_different && !parent_objfile->crc32_p)
1437a45ae5f8SJohn Marino 	{
1438a45ae5f8SJohn Marino 	  parent_objfile->crc32_p = get_file_crc (parent_objfile->obfd,
1439a45ae5f8SJohn Marino 						  &parent_objfile->crc32);
1440a45ae5f8SJohn Marino 	  if (!parent_objfile->crc32_p)
1441a45ae5f8SJohn Marino 	    return 0;
1442a45ae5f8SJohn Marino 	}
1443a45ae5f8SJohn Marino 
1444a45ae5f8SJohn Marino       if (verified_as_different || parent_objfile->crc32 != file_crc)
1445cf7f2e2dSJohn Marino 	warning (_("the debug information found in \"%s\""
1446cf7f2e2dSJohn Marino 		   " does not match \"%s\" (CRC mismatch).\n"),
1447cf7f2e2dSJohn Marino 		 name, parent_objfile->name);
1448a45ae5f8SJohn Marino 
1449cf7f2e2dSJohn Marino       return 0;
1450cf7f2e2dSJohn Marino     }
1451cf7f2e2dSJohn Marino 
1452cf7f2e2dSJohn Marino   return 1;
14535796c8dcSSimon Schubert }
14545796c8dcSSimon Schubert 
14555796c8dcSSimon Schubert char *debug_file_directory = NULL;
14565796c8dcSSimon Schubert static void
show_debug_file_directory(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)14575796c8dcSSimon Schubert show_debug_file_directory (struct ui_file *file, int from_tty,
14585796c8dcSSimon Schubert 			   struct cmd_list_element *c, const char *value)
14595796c8dcSSimon Schubert {
1460c50c785cSJohn Marino   fprintf_filtered (file,
1461c50c785cSJohn Marino 		    _("The directory where separate debug "
1462c50c785cSJohn Marino 		      "symbols are searched for is \"%s\".\n"),
14635796c8dcSSimon Schubert 		    value);
14645796c8dcSSimon Schubert }
14655796c8dcSSimon Schubert 
14665796c8dcSSimon Schubert #if ! defined (DEBUG_SUBDIRECTORY)
14675796c8dcSSimon Schubert #define DEBUG_SUBDIRECTORY ".debug"
14685796c8dcSSimon Schubert #endif
14695796c8dcSSimon Schubert 
1470*ef5ccd6cSJohn Marino /* Find a separate debuginfo file for OBJFILE, using DIR as the directory
1471*ef5ccd6cSJohn Marino    where the original file resides (may not be the same as
1472*ef5ccd6cSJohn Marino    dirname(objfile->name) due to symlinks), and DEBUGLINK as the file we are
1473*ef5ccd6cSJohn Marino    looking for.  Returns the name of the debuginfo, of NULL.  */
1474*ef5ccd6cSJohn Marino 
1475*ef5ccd6cSJohn Marino static char *
find_separate_debug_file(const char * dir,const char * canon_dir,const char * debuglink,unsigned long crc32,struct objfile * objfile)1476*ef5ccd6cSJohn Marino find_separate_debug_file (const char *dir,
1477*ef5ccd6cSJohn Marino 			  const char *canon_dir,
1478*ef5ccd6cSJohn Marino 			  const char *debuglink,
1479*ef5ccd6cSJohn Marino 			  unsigned long crc32, struct objfile *objfile)
14805796c8dcSSimon Schubert {
1481*ef5ccd6cSJohn Marino   char *debugdir;
1482*ef5ccd6cSJohn Marino   char *debugfile;
14835796c8dcSSimon Schubert   int i;
1484*ef5ccd6cSJohn Marino   VEC (char_ptr) *debugdir_vec;
1485*ef5ccd6cSJohn Marino   struct cleanup *back_to;
1486*ef5ccd6cSJohn Marino   int ix;
14875796c8dcSSimon Schubert 
1488*ef5ccd6cSJohn Marino   /* Set I to max (strlen (canon_dir), strlen (dir)).  */
14895796c8dcSSimon Schubert   i = strlen (dir);
1490*ef5ccd6cSJohn Marino   if (canon_dir != NULL && strlen (canon_dir) > i)
1491*ef5ccd6cSJohn Marino     i = strlen (canon_dir);
14925796c8dcSSimon Schubert 
1493cf7f2e2dSJohn Marino   debugfile = xmalloc (strlen (debug_file_directory) + 1
14945796c8dcSSimon Schubert 		       + i
14955796c8dcSSimon Schubert 		       + strlen (DEBUG_SUBDIRECTORY)
14965796c8dcSSimon Schubert 		       + strlen ("/")
1497*ef5ccd6cSJohn Marino 		       + strlen (debuglink)
14985796c8dcSSimon Schubert 		       + 1);
14995796c8dcSSimon Schubert 
15005796c8dcSSimon Schubert   /* First try in the same directory as the original file.  */
15015796c8dcSSimon Schubert   strcpy (debugfile, dir);
1502*ef5ccd6cSJohn Marino   strcat (debugfile, debuglink);
15035796c8dcSSimon Schubert 
1504cf7f2e2dSJohn Marino   if (separate_debug_file_exists (debugfile, crc32, objfile))
1505*ef5ccd6cSJohn Marino     return debugfile;
15065796c8dcSSimon Schubert 
15075796c8dcSSimon Schubert   /* Then try in the subdirectory named DEBUG_SUBDIRECTORY.  */
15085796c8dcSSimon Schubert   strcpy (debugfile, dir);
15095796c8dcSSimon Schubert   strcat (debugfile, DEBUG_SUBDIRECTORY);
15105796c8dcSSimon Schubert   strcat (debugfile, "/");
1511*ef5ccd6cSJohn Marino   strcat (debugfile, debuglink);
15125796c8dcSSimon Schubert 
1513cf7f2e2dSJohn Marino   if (separate_debug_file_exists (debugfile, crc32, objfile))
1514*ef5ccd6cSJohn Marino     return debugfile;
15155796c8dcSSimon Schubert 
1516cf7f2e2dSJohn Marino   /* Then try in the global debugfile directories.
1517cf7f2e2dSJohn Marino 
1518cf7f2e2dSJohn Marino      Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will
1519cf7f2e2dSJohn Marino      cause "/..." lookups.  */
1520cf7f2e2dSJohn Marino 
1521*ef5ccd6cSJohn Marino   debugdir_vec = dirnames_to_char_ptr_vec (debug_file_directory);
1522*ef5ccd6cSJohn Marino   back_to = make_cleanup_free_char_ptr_vec (debugdir_vec);
1523*ef5ccd6cSJohn Marino 
1524*ef5ccd6cSJohn Marino   for (ix = 0; VEC_iterate (char_ptr, debugdir_vec, ix, debugdir); ++ix)
1525cf7f2e2dSJohn Marino     {
1526*ef5ccd6cSJohn Marino       strcpy (debugfile, debugdir);
15275796c8dcSSimon Schubert       strcat (debugfile, "/");
15285796c8dcSSimon Schubert       strcat (debugfile, dir);
1529*ef5ccd6cSJohn Marino       strcat (debugfile, debuglink);
15305796c8dcSSimon Schubert 
1531cf7f2e2dSJohn Marino       if (separate_debug_file_exists (debugfile, crc32, objfile))
1532*ef5ccd6cSJohn Marino 	return debugfile;
15335796c8dcSSimon Schubert 
15345796c8dcSSimon Schubert       /* If the file is in the sysroot, try using its base path in the
15355796c8dcSSimon Schubert 	 global debugfile directory.  */
1536*ef5ccd6cSJohn Marino       if (canon_dir != NULL
1537*ef5ccd6cSJohn Marino 	  && filename_ncmp (canon_dir, gdb_sysroot,
1538c50c785cSJohn Marino 			    strlen (gdb_sysroot)) == 0
1539*ef5ccd6cSJohn Marino 	  && IS_DIR_SEPARATOR (canon_dir[strlen (gdb_sysroot)]))
15405796c8dcSSimon Schubert 	{
1541*ef5ccd6cSJohn Marino 	  strcpy (debugfile, debugdir);
1542*ef5ccd6cSJohn Marino 	  strcat (debugfile, canon_dir + strlen (gdb_sysroot));
15435796c8dcSSimon Schubert 	  strcat (debugfile, "/");
1544*ef5ccd6cSJohn Marino 	  strcat (debugfile, debuglink);
15455796c8dcSSimon Schubert 
1546cf7f2e2dSJohn Marino 	  if (separate_debug_file_exists (debugfile, crc32, objfile))
1547*ef5ccd6cSJohn Marino 	    return debugfile;
1548*ef5ccd6cSJohn Marino 	}
1549cf7f2e2dSJohn Marino     }
1550cf7f2e2dSJohn Marino 
1551*ef5ccd6cSJohn Marino   do_cleanups (back_to);
1552cf7f2e2dSJohn Marino   xfree (debugfile);
1553*ef5ccd6cSJohn Marino   return NULL;
1554*ef5ccd6cSJohn Marino }
1555cf7f2e2dSJohn Marino 
1556*ef5ccd6cSJohn Marino /* Modify PATH to contain only "directory/" part of PATH.
1557*ef5ccd6cSJohn Marino    If there were no directory separators in PATH, PATH will be empty
1558*ef5ccd6cSJohn Marino    string on return.  */
1559*ef5ccd6cSJohn Marino 
1560*ef5ccd6cSJohn Marino static void
terminate_after_last_dir_separator(char * path)1561*ef5ccd6cSJohn Marino terminate_after_last_dir_separator (char *path)
1562*ef5ccd6cSJohn Marino {
1563*ef5ccd6cSJohn Marino   int i;
1564*ef5ccd6cSJohn Marino 
1565*ef5ccd6cSJohn Marino   /* Strip off the final filename part, leaving the directory name,
1566*ef5ccd6cSJohn Marino      followed by a slash.  The directory can be relative or absolute.  */
1567*ef5ccd6cSJohn Marino   for (i = strlen(path) - 1; i >= 0; i--)
1568*ef5ccd6cSJohn Marino     if (IS_DIR_SEPARATOR (path[i]))
1569*ef5ccd6cSJohn Marino       break;
1570*ef5ccd6cSJohn Marino 
1571*ef5ccd6cSJohn Marino   /* If I is -1 then no directory is present there and DIR will be "".  */
1572*ef5ccd6cSJohn Marino   path[i + 1] = '\0';
1573*ef5ccd6cSJohn Marino }
1574*ef5ccd6cSJohn Marino 
1575*ef5ccd6cSJohn Marino /* Find separate debuginfo for OBJFILE (using .gnu_debuglink section).
1576*ef5ccd6cSJohn Marino    Returns pathname, or NULL.  */
1577*ef5ccd6cSJohn Marino 
1578*ef5ccd6cSJohn Marino char *
find_separate_debug_file_by_debuglink(struct objfile * objfile)1579*ef5ccd6cSJohn Marino find_separate_debug_file_by_debuglink (struct objfile *objfile)
1580*ef5ccd6cSJohn Marino {
1581*ef5ccd6cSJohn Marino   char *debuglink;
1582*ef5ccd6cSJohn Marino   char *dir, *canon_dir;
1583*ef5ccd6cSJohn Marino   char *debugfile;
1584*ef5ccd6cSJohn Marino   unsigned long crc32;
1585*ef5ccd6cSJohn Marino   struct cleanup *cleanups;
1586*ef5ccd6cSJohn Marino 
1587*ef5ccd6cSJohn Marino   debuglink = get_debug_link_info (objfile, &crc32);
1588*ef5ccd6cSJohn Marino 
1589*ef5ccd6cSJohn Marino   if (debuglink == NULL)
1590*ef5ccd6cSJohn Marino     {
1591*ef5ccd6cSJohn Marino       /* There's no separate debug info, hence there's no way we could
1592*ef5ccd6cSJohn Marino 	 load it => no warning.  */
1593*ef5ccd6cSJohn Marino       return NULL;
1594*ef5ccd6cSJohn Marino     }
1595*ef5ccd6cSJohn Marino 
1596*ef5ccd6cSJohn Marino   cleanups = make_cleanup (xfree, debuglink);
1597*ef5ccd6cSJohn Marino   dir = xstrdup (objfile->name);
1598*ef5ccd6cSJohn Marino   make_cleanup (xfree, dir);
1599*ef5ccd6cSJohn Marino   terminate_after_last_dir_separator (dir);
1600*ef5ccd6cSJohn Marino   canon_dir = lrealpath (dir);
1601*ef5ccd6cSJohn Marino 
1602*ef5ccd6cSJohn Marino   debugfile = find_separate_debug_file (dir, canon_dir, debuglink,
1603*ef5ccd6cSJohn Marino 					crc32, objfile);
1604*ef5ccd6cSJohn Marino   xfree (canon_dir);
1605*ef5ccd6cSJohn Marino 
1606*ef5ccd6cSJohn Marino   if (debugfile == NULL)
1607*ef5ccd6cSJohn Marino     {
1608*ef5ccd6cSJohn Marino #ifdef HAVE_LSTAT
1609*ef5ccd6cSJohn Marino       /* For PR gdb/9538, try again with realpath (if different from the
1610*ef5ccd6cSJohn Marino 	 original).  */
1611*ef5ccd6cSJohn Marino 
1612*ef5ccd6cSJohn Marino       struct stat st_buf;
1613*ef5ccd6cSJohn Marino 
1614*ef5ccd6cSJohn Marino       if (lstat (objfile->name, &st_buf) == 0 && S_ISLNK(st_buf.st_mode))
1615*ef5ccd6cSJohn Marino 	{
1616*ef5ccd6cSJohn Marino 	  char *symlink_dir;
1617*ef5ccd6cSJohn Marino 
1618*ef5ccd6cSJohn Marino 	  symlink_dir = lrealpath (objfile->name);
1619*ef5ccd6cSJohn Marino 	  if (symlink_dir != NULL)
1620*ef5ccd6cSJohn Marino 	    {
1621*ef5ccd6cSJohn Marino 	      make_cleanup (xfree, symlink_dir);
1622*ef5ccd6cSJohn Marino 	      terminate_after_last_dir_separator (symlink_dir);
1623*ef5ccd6cSJohn Marino 	      if (strcmp (dir, symlink_dir) != 0)
1624*ef5ccd6cSJohn Marino 		{
1625*ef5ccd6cSJohn Marino 		  /* Different directory, so try using it.  */
1626*ef5ccd6cSJohn Marino 		  debugfile = find_separate_debug_file (symlink_dir,
1627*ef5ccd6cSJohn Marino 							symlink_dir,
1628*ef5ccd6cSJohn Marino 							debuglink,
1629*ef5ccd6cSJohn Marino 							crc32,
1630*ef5ccd6cSJohn Marino 							objfile);
1631*ef5ccd6cSJohn Marino 		}
1632*ef5ccd6cSJohn Marino 	    }
1633*ef5ccd6cSJohn Marino 	}
1634*ef5ccd6cSJohn Marino #endif  /* HAVE_LSTAT  */
1635*ef5ccd6cSJohn Marino     }
1636*ef5ccd6cSJohn Marino 
1637*ef5ccd6cSJohn Marino   do_cleanups (cleanups);
1638cf7f2e2dSJohn Marino   return debugfile;
16395796c8dcSSimon Schubert }
16405796c8dcSSimon Schubert 
16415796c8dcSSimon Schubert 
16425796c8dcSSimon Schubert /* This is the symbol-file command.  Read the file, analyze its
16435796c8dcSSimon Schubert    symbols, and add a struct symtab to a symtab list.  The syntax of
16445796c8dcSSimon Schubert    the command is rather bizarre:
16455796c8dcSSimon Schubert 
16465796c8dcSSimon Schubert    1. The function buildargv implements various quoting conventions
16475796c8dcSSimon Schubert    which are undocumented and have little or nothing in common with
16485796c8dcSSimon Schubert    the way things are quoted (or not quoted) elsewhere in GDB.
16495796c8dcSSimon Schubert 
16505796c8dcSSimon Schubert    2. Options are used, which are not generally used in GDB (perhaps
16515796c8dcSSimon Schubert    "set mapped on", "set readnow on" would be better)
16525796c8dcSSimon Schubert 
16535796c8dcSSimon Schubert    3. The order of options matters, which is contrary to GNU
16545796c8dcSSimon Schubert    conventions (because it is confusing and inconvenient).  */
16555796c8dcSSimon Schubert 
16565796c8dcSSimon Schubert void
symbol_file_command(char * args,int from_tty)16575796c8dcSSimon Schubert symbol_file_command (char *args, int from_tty)
16585796c8dcSSimon Schubert {
16595796c8dcSSimon Schubert   dont_repeat ();
16605796c8dcSSimon Schubert 
16615796c8dcSSimon Schubert   if (args == NULL)
16625796c8dcSSimon Schubert     {
16635796c8dcSSimon Schubert       symbol_file_clear (from_tty);
16645796c8dcSSimon Schubert     }
16655796c8dcSSimon Schubert   else
16665796c8dcSSimon Schubert     {
16675796c8dcSSimon Schubert       char **argv = gdb_buildargv (args);
16685796c8dcSSimon Schubert       int flags = OBJF_USERLOADED;
16695796c8dcSSimon Schubert       struct cleanup *cleanups;
16705796c8dcSSimon Schubert       char *name = NULL;
16715796c8dcSSimon Schubert 
16725796c8dcSSimon Schubert       cleanups = make_cleanup_freeargv (argv);
16735796c8dcSSimon Schubert       while (*argv != NULL)
16745796c8dcSSimon Schubert 	{
16755796c8dcSSimon Schubert 	  if (strcmp (*argv, "-readnow") == 0)
16765796c8dcSSimon Schubert 	    flags |= OBJF_READNOW;
16775796c8dcSSimon Schubert 	  else if (**argv == '-')
16785796c8dcSSimon Schubert 	    error (_("unknown option `%s'"), *argv);
16795796c8dcSSimon Schubert 	  else
16805796c8dcSSimon Schubert 	    {
16815796c8dcSSimon Schubert 	      symbol_file_add_main_1 (*argv, from_tty, flags);
16825796c8dcSSimon Schubert 	      name = *argv;
16835796c8dcSSimon Schubert 	    }
16845796c8dcSSimon Schubert 
16855796c8dcSSimon Schubert 	  argv++;
16865796c8dcSSimon Schubert 	}
16875796c8dcSSimon Schubert 
16885796c8dcSSimon Schubert       if (name == NULL)
16895796c8dcSSimon Schubert 	error (_("no symbol file name was specified"));
16905796c8dcSSimon Schubert 
16915796c8dcSSimon Schubert       do_cleanups (cleanups);
16925796c8dcSSimon Schubert     }
16935796c8dcSSimon Schubert }
16945796c8dcSSimon Schubert 
16955796c8dcSSimon Schubert /* Set the initial language.
16965796c8dcSSimon Schubert 
16975796c8dcSSimon Schubert    FIXME: A better solution would be to record the language in the
16985796c8dcSSimon Schubert    psymtab when reading partial symbols, and then use it (if known) to
16995796c8dcSSimon Schubert    set the language.  This would be a win for formats that encode the
17005796c8dcSSimon Schubert    language in an easily discoverable place, such as DWARF.  For
17015796c8dcSSimon Schubert    stabs, we can jump through hoops looking for specially named
17025796c8dcSSimon Schubert    symbols or try to intuit the language from the specific type of
17035796c8dcSSimon Schubert    stabs we find, but we can't do that until later when we read in
17045796c8dcSSimon Schubert    full symbols.  */
17055796c8dcSSimon Schubert 
17065796c8dcSSimon Schubert void
set_initial_language(void)17075796c8dcSSimon Schubert set_initial_language (void)
17085796c8dcSSimon Schubert {
17095796c8dcSSimon Schubert   enum language lang = language_unknown;
17105796c8dcSSimon Schubert 
1711c50c785cSJohn Marino   if (language_of_main != language_unknown)
1712c50c785cSJohn Marino     lang = language_of_main;
1713c50c785cSJohn Marino   else
1714c50c785cSJohn Marino     {
1715c50c785cSJohn Marino       const char *filename;
1716c50c785cSJohn Marino 
1717cf7f2e2dSJohn Marino       filename = find_main_filename ();
1718cf7f2e2dSJohn Marino       if (filename != NULL)
1719cf7f2e2dSJohn Marino 	lang = deduce_language_from_filename (filename);
1720c50c785cSJohn Marino     }
17215796c8dcSSimon Schubert 
17225796c8dcSSimon Schubert   if (lang == language_unknown)
17235796c8dcSSimon Schubert     {
17245796c8dcSSimon Schubert       /* Make C the default language */
17255796c8dcSSimon Schubert       lang = language_c;
17265796c8dcSSimon Schubert     }
17275796c8dcSSimon Schubert 
17285796c8dcSSimon Schubert   set_language (lang);
17295796c8dcSSimon Schubert   expected_language = current_language; /* Don't warn the user.  */
17305796c8dcSSimon Schubert }
1731cf7f2e2dSJohn Marino 
1732cf7f2e2dSJohn Marino /* If NAME is a remote name open the file using remote protocol, otherwise
1733*ef5ccd6cSJohn Marino    open it normally.  Returns a new reference to the BFD.  On error,
1734*ef5ccd6cSJohn Marino    returns NULL with the BFD error set.  */
1735cf7f2e2dSJohn Marino 
1736cf7f2e2dSJohn Marino bfd *
gdb_bfd_open_maybe_remote(const char * name)1737*ef5ccd6cSJohn Marino gdb_bfd_open_maybe_remote (const char *name)
1738cf7f2e2dSJohn Marino {
1739*ef5ccd6cSJohn Marino   bfd *result;
1740*ef5ccd6cSJohn Marino 
1741cf7f2e2dSJohn Marino   if (remote_filename_p (name))
1742*ef5ccd6cSJohn Marino     result = remote_bfd_open (name, gnutarget);
1743cf7f2e2dSJohn Marino   else
1744*ef5ccd6cSJohn Marino     result = gdb_bfd_open (name, gnutarget, -1);
1745*ef5ccd6cSJohn Marino 
1746*ef5ccd6cSJohn Marino   return result;
17475796c8dcSSimon Schubert }
17485796c8dcSSimon Schubert 
1749cf7f2e2dSJohn Marino 
17505796c8dcSSimon Schubert /* Open the file specified by NAME and hand it off to BFD for
17515796c8dcSSimon Schubert    preliminary analysis.  Return a newly initialized bfd *, which
17525796c8dcSSimon Schubert    includes a newly malloc'd` copy of NAME (tilde-expanded and made
17535796c8dcSSimon Schubert    absolute).  In case of trouble, error() is called.  */
17545796c8dcSSimon Schubert 
17555796c8dcSSimon Schubert bfd *
symfile_bfd_open(char * name)17565796c8dcSSimon Schubert symfile_bfd_open (char *name)
17575796c8dcSSimon Schubert {
17585796c8dcSSimon Schubert   bfd *sym_bfd;
17595796c8dcSSimon Schubert   int desc;
17605796c8dcSSimon Schubert   char *absolute_name;
17615796c8dcSSimon Schubert 
17625796c8dcSSimon Schubert   if (remote_filename_p (name))
17635796c8dcSSimon Schubert     {
17645796c8dcSSimon Schubert       sym_bfd = remote_bfd_open (name, gnutarget);
17655796c8dcSSimon Schubert       if (!sym_bfd)
17665796c8dcSSimon Schubert 	error (_("`%s': can't open to read symbols: %s."), name,
17675796c8dcSSimon Schubert 	       bfd_errmsg (bfd_get_error ()));
17685796c8dcSSimon Schubert 
17695796c8dcSSimon Schubert       if (!bfd_check_format (sym_bfd, bfd_object))
17705796c8dcSSimon Schubert 	{
1771*ef5ccd6cSJohn Marino 	  make_cleanup_bfd_unref (sym_bfd);
17725796c8dcSSimon Schubert 	  error (_("`%s': can't read symbols: %s."), name,
17735796c8dcSSimon Schubert 		 bfd_errmsg (bfd_get_error ()));
17745796c8dcSSimon Schubert 	}
17755796c8dcSSimon Schubert 
17765796c8dcSSimon Schubert       return sym_bfd;
17775796c8dcSSimon Schubert     }
17785796c8dcSSimon Schubert 
17795796c8dcSSimon Schubert   name = tilde_expand (name);	/* Returns 1st new malloc'd copy.  */
17805796c8dcSSimon Schubert 
17815796c8dcSSimon Schubert   /* Look down path for it, allocate 2nd new malloc'd copy.  */
17825796c8dcSSimon Schubert   desc = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST, name,
17835796c8dcSSimon Schubert 		O_RDONLY | O_BINARY, &absolute_name);
17845796c8dcSSimon Schubert #if defined(__GO32__) || defined(_WIN32) || defined (__CYGWIN__)
17855796c8dcSSimon Schubert   if (desc < 0)
17865796c8dcSSimon Schubert     {
17875796c8dcSSimon Schubert       char *exename = alloca (strlen (name) + 5);
1788cf7f2e2dSJohn Marino 
17895796c8dcSSimon Schubert       strcat (strcpy (exename, name), ".exe");
17905796c8dcSSimon Schubert       desc = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST, exename,
17915796c8dcSSimon Schubert 		    O_RDONLY | O_BINARY, &absolute_name);
17925796c8dcSSimon Schubert     }
17935796c8dcSSimon Schubert #endif
17945796c8dcSSimon Schubert   if (desc < 0)
17955796c8dcSSimon Schubert     {
17965796c8dcSSimon Schubert       make_cleanup (xfree, name);
17975796c8dcSSimon Schubert       perror_with_name (name);
17985796c8dcSSimon Schubert     }
17995796c8dcSSimon Schubert 
18005796c8dcSSimon Schubert   xfree (name);
18015796c8dcSSimon Schubert   name = absolute_name;
1802*ef5ccd6cSJohn Marino   make_cleanup (xfree, name);
18035796c8dcSSimon Schubert 
1804*ef5ccd6cSJohn Marino   sym_bfd = gdb_bfd_open (name, gnutarget, desc);
18055796c8dcSSimon Schubert   if (!sym_bfd)
18065796c8dcSSimon Schubert     {
18075796c8dcSSimon Schubert       make_cleanup (xfree, name);
18085796c8dcSSimon Schubert       error (_("`%s': can't open to read symbols: %s."), name,
18095796c8dcSSimon Schubert 	     bfd_errmsg (bfd_get_error ()));
18105796c8dcSSimon Schubert     }
18115796c8dcSSimon Schubert   bfd_set_cacheable (sym_bfd, 1);
18125796c8dcSSimon Schubert 
18135796c8dcSSimon Schubert   if (!bfd_check_format (sym_bfd, bfd_object))
18145796c8dcSSimon Schubert     {
1815*ef5ccd6cSJohn Marino       make_cleanup_bfd_unref (sym_bfd);
18165796c8dcSSimon Schubert       error (_("`%s': can't read symbols: %s."), name,
18175796c8dcSSimon Schubert 	     bfd_errmsg (bfd_get_error ()));
18185796c8dcSSimon Schubert     }
18195796c8dcSSimon Schubert 
18205796c8dcSSimon Schubert   return sym_bfd;
18215796c8dcSSimon Schubert }
18225796c8dcSSimon Schubert 
18235796c8dcSSimon Schubert /* Return the section index for SECTION_NAME on OBJFILE.  Return -1 if
18245796c8dcSSimon Schubert    the section was not found.  */
18255796c8dcSSimon Schubert 
18265796c8dcSSimon Schubert int
get_section_index(struct objfile * objfile,char * section_name)18275796c8dcSSimon Schubert get_section_index (struct objfile *objfile, char *section_name)
18285796c8dcSSimon Schubert {
18295796c8dcSSimon Schubert   asection *sect = bfd_get_section_by_name (objfile->obfd, section_name);
18305796c8dcSSimon Schubert 
18315796c8dcSSimon Schubert   if (sect)
18325796c8dcSSimon Schubert     return sect->index;
18335796c8dcSSimon Schubert   else
18345796c8dcSSimon Schubert     return -1;
18355796c8dcSSimon Schubert }
18365796c8dcSSimon Schubert 
18375796c8dcSSimon Schubert /* Link SF into the global symtab_fns list.  Called on startup by the
18385796c8dcSSimon Schubert    _initialize routine in each object file format reader, to register
1839c50c785cSJohn Marino    information about each format the reader is prepared to handle.  */
18405796c8dcSSimon Schubert 
18415796c8dcSSimon Schubert void
add_symtab_fns(const struct sym_fns * sf)1842c50c785cSJohn Marino add_symtab_fns (const struct sym_fns *sf)
18435796c8dcSSimon Schubert {
1844c50c785cSJohn Marino   VEC_safe_push (sym_fns_ptr, symtab_fns, sf);
18455796c8dcSSimon Schubert }
18465796c8dcSSimon Schubert 
18475796c8dcSSimon Schubert /* Initialize OBJFILE to read symbols from its associated BFD.  It
18485796c8dcSSimon Schubert    either returns or calls error().  The result is an initialized
18495796c8dcSSimon Schubert    struct sym_fns in the objfile structure, that contains cached
18505796c8dcSSimon Schubert    information about the symbol file.  */
18515796c8dcSSimon Schubert 
1852c50c785cSJohn Marino static const struct sym_fns *
find_sym_fns(bfd * abfd)18535796c8dcSSimon Schubert find_sym_fns (bfd *abfd)
18545796c8dcSSimon Schubert {
1855c50c785cSJohn Marino   const struct sym_fns *sf;
18565796c8dcSSimon Schubert   enum bfd_flavour our_flavour = bfd_get_flavour (abfd);
1857c50c785cSJohn Marino   int i;
18585796c8dcSSimon Schubert 
18595796c8dcSSimon Schubert   if (our_flavour == bfd_target_srec_flavour
18605796c8dcSSimon Schubert       || our_flavour == bfd_target_ihex_flavour
18615796c8dcSSimon Schubert       || our_flavour == bfd_target_tekhex_flavour)
18625796c8dcSSimon Schubert     return NULL;	/* No symbols.  */
18635796c8dcSSimon Schubert 
1864c50c785cSJohn Marino   for (i = 0; VEC_iterate (sym_fns_ptr, symtab_fns, i, sf); ++i)
18655796c8dcSSimon Schubert     if (our_flavour == sf->sym_flavour)
18665796c8dcSSimon Schubert       return sf;
18675796c8dcSSimon Schubert 
18685796c8dcSSimon Schubert   error (_("I'm sorry, Dave, I can't do that.  Symbol format `%s' unknown."),
18695796c8dcSSimon Schubert 	 bfd_get_target (abfd));
18705796c8dcSSimon Schubert }
18715796c8dcSSimon Schubert 
18725796c8dcSSimon Schubert 
18735796c8dcSSimon Schubert /* This function runs the load command of our current target.  */
18745796c8dcSSimon Schubert 
18755796c8dcSSimon Schubert static void
load_command(char * arg,int from_tty)18765796c8dcSSimon Schubert load_command (char *arg, int from_tty)
18775796c8dcSSimon Schubert {
1878c50c785cSJohn Marino   dont_repeat ();
1879c50c785cSJohn Marino 
18805796c8dcSSimon Schubert   /* The user might be reloading because the binary has changed.  Take
18815796c8dcSSimon Schubert      this opportunity to check.  */
18825796c8dcSSimon Schubert   reopen_exec_file ();
18835796c8dcSSimon Schubert   reread_symbols ();
18845796c8dcSSimon Schubert 
18855796c8dcSSimon Schubert   if (arg == NULL)
18865796c8dcSSimon Schubert     {
18875796c8dcSSimon Schubert       char *parg;
18885796c8dcSSimon Schubert       int count = 0;
18895796c8dcSSimon Schubert 
18905796c8dcSSimon Schubert       parg = arg = get_exec_file (1);
18915796c8dcSSimon Schubert 
18925796c8dcSSimon Schubert       /* Count how many \ " ' tab space there are in the name.  */
18935796c8dcSSimon Schubert       while ((parg = strpbrk (parg, "\\\"'\t ")))
18945796c8dcSSimon Schubert 	{
18955796c8dcSSimon Schubert 	  parg++;
18965796c8dcSSimon Schubert 	  count++;
18975796c8dcSSimon Schubert 	}
18985796c8dcSSimon Schubert 
18995796c8dcSSimon Schubert       if (count)
19005796c8dcSSimon Schubert 	{
19015796c8dcSSimon Schubert 	  /* We need to quote this string so buildargv can pull it apart.  */
19025796c8dcSSimon Schubert 	  char *temp = xmalloc (strlen (arg) + count + 1 );
19035796c8dcSSimon Schubert 	  char *ptemp = temp;
19045796c8dcSSimon Schubert 	  char *prev;
19055796c8dcSSimon Schubert 
19065796c8dcSSimon Schubert 	  make_cleanup (xfree, temp);
19075796c8dcSSimon Schubert 
19085796c8dcSSimon Schubert 	  prev = parg = arg;
19095796c8dcSSimon Schubert 	  while ((parg = strpbrk (parg, "\\\"'\t ")))
19105796c8dcSSimon Schubert 	    {
19115796c8dcSSimon Schubert 	      strncpy (ptemp, prev, parg - prev);
19125796c8dcSSimon Schubert 	      ptemp += parg - prev;
19135796c8dcSSimon Schubert 	      prev = parg++;
19145796c8dcSSimon Schubert 	      *ptemp++ = '\\';
19155796c8dcSSimon Schubert 	    }
19165796c8dcSSimon Schubert 	  strcpy (ptemp, prev);
19175796c8dcSSimon Schubert 
19185796c8dcSSimon Schubert 	  arg = temp;
19195796c8dcSSimon Schubert 	}
19205796c8dcSSimon Schubert     }
19215796c8dcSSimon Schubert 
19225796c8dcSSimon Schubert   target_load (arg, from_tty);
19235796c8dcSSimon Schubert 
19245796c8dcSSimon Schubert   /* After re-loading the executable, we don't really know which
19255796c8dcSSimon Schubert      overlays are mapped any more.  */
19265796c8dcSSimon Schubert   overlay_cache_invalid = 1;
19275796c8dcSSimon Schubert }
19285796c8dcSSimon Schubert 
19295796c8dcSSimon Schubert /* This version of "load" should be usable for any target.  Currently
19305796c8dcSSimon Schubert    it is just used for remote targets, not inftarg.c or core files,
19315796c8dcSSimon Schubert    on the theory that only in that case is it useful.
19325796c8dcSSimon Schubert 
19335796c8dcSSimon Schubert    Avoiding xmodem and the like seems like a win (a) because we don't have
19345796c8dcSSimon Schubert    to worry about finding it, and (b) On VMS, fork() is very slow and so
19355796c8dcSSimon Schubert    we don't want to run a subprocess.  On the other hand, I'm not sure how
19365796c8dcSSimon Schubert    performance compares.  */
19375796c8dcSSimon Schubert 
19385796c8dcSSimon Schubert static int validate_download = 0;
19395796c8dcSSimon Schubert 
19405796c8dcSSimon Schubert /* Callback service function for generic_load (bfd_map_over_sections).  */
19415796c8dcSSimon Schubert 
19425796c8dcSSimon Schubert static void
add_section_size_callback(bfd * abfd,asection * asec,void * data)19435796c8dcSSimon Schubert add_section_size_callback (bfd *abfd, asection *asec, void *data)
19445796c8dcSSimon Schubert {
19455796c8dcSSimon Schubert   bfd_size_type *sum = data;
19465796c8dcSSimon Schubert 
19475796c8dcSSimon Schubert   *sum += bfd_get_section_size (asec);
19485796c8dcSSimon Schubert }
19495796c8dcSSimon Schubert 
19505796c8dcSSimon Schubert /* Opaque data for load_section_callback.  */
19515796c8dcSSimon Schubert struct load_section_data {
1952*ef5ccd6cSJohn Marino   CORE_ADDR load_offset;
19535796c8dcSSimon Schubert   struct load_progress_data *progress_data;
19545796c8dcSSimon Schubert   VEC(memory_write_request_s) *requests;
19555796c8dcSSimon Schubert };
19565796c8dcSSimon Schubert 
19575796c8dcSSimon Schubert /* Opaque data for load_progress.  */
19585796c8dcSSimon Schubert struct load_progress_data {
19595796c8dcSSimon Schubert   /* Cumulative data.  */
19605796c8dcSSimon Schubert   unsigned long write_count;
19615796c8dcSSimon Schubert   unsigned long data_count;
19625796c8dcSSimon Schubert   bfd_size_type total_size;
19635796c8dcSSimon Schubert };
19645796c8dcSSimon Schubert 
19655796c8dcSSimon Schubert /* Opaque data for load_progress for a single section.  */
19665796c8dcSSimon Schubert struct load_progress_section_data {
19675796c8dcSSimon Schubert   struct load_progress_data *cumulative;
19685796c8dcSSimon Schubert 
19695796c8dcSSimon Schubert   /* Per-section data.  */
19705796c8dcSSimon Schubert   const char *section_name;
19715796c8dcSSimon Schubert   ULONGEST section_sent;
19725796c8dcSSimon Schubert   ULONGEST section_size;
19735796c8dcSSimon Schubert   CORE_ADDR lma;
19745796c8dcSSimon Schubert   gdb_byte *buffer;
19755796c8dcSSimon Schubert };
19765796c8dcSSimon Schubert 
19775796c8dcSSimon Schubert /* Target write callback routine for progress reporting.  */
19785796c8dcSSimon Schubert 
19795796c8dcSSimon Schubert static void
load_progress(ULONGEST bytes,void * untyped_arg)19805796c8dcSSimon Schubert load_progress (ULONGEST bytes, void *untyped_arg)
19815796c8dcSSimon Schubert {
19825796c8dcSSimon Schubert   struct load_progress_section_data *args = untyped_arg;
19835796c8dcSSimon Schubert   struct load_progress_data *totals;
19845796c8dcSSimon Schubert 
19855796c8dcSSimon Schubert   if (args == NULL)
19865796c8dcSSimon Schubert     /* Writing padding data.  No easy way to get at the cumulative
19875796c8dcSSimon Schubert        stats, so just ignore this.  */
19885796c8dcSSimon Schubert     return;
19895796c8dcSSimon Schubert 
19905796c8dcSSimon Schubert   totals = args->cumulative;
19915796c8dcSSimon Schubert 
19925796c8dcSSimon Schubert   if (bytes == 0 && args->section_sent == 0)
19935796c8dcSSimon Schubert     {
19945796c8dcSSimon Schubert       /* The write is just starting.  Let the user know we've started
19955796c8dcSSimon Schubert 	 this section.  */
1996a45ae5f8SJohn Marino       ui_out_message (current_uiout, 0, "Loading section %s, size %s lma %s\n",
19975796c8dcSSimon Schubert 		      args->section_name, hex_string (args->section_size),
1998*ef5ccd6cSJohn Marino 		      paddress (target_gdbarch (), args->lma));
19995796c8dcSSimon Schubert       return;
20005796c8dcSSimon Schubert     }
20015796c8dcSSimon Schubert 
20025796c8dcSSimon Schubert   if (validate_download)
20035796c8dcSSimon Schubert     {
20045796c8dcSSimon Schubert       /* Broken memories and broken monitors manifest themselves here
20055796c8dcSSimon Schubert 	 when bring new computers to life.  This doubles already slow
20065796c8dcSSimon Schubert 	 downloads.  */
20075796c8dcSSimon Schubert       /* NOTE: cagney/1999-10-18: A more efficient implementation
20085796c8dcSSimon Schubert 	 might add a verify_memory() method to the target vector and
20095796c8dcSSimon Schubert 	 then use that.  remote.c could implement that method using
20105796c8dcSSimon Schubert 	 the ``qCRC'' packet.  */
20115796c8dcSSimon Schubert       gdb_byte *check = xmalloc (bytes);
20125796c8dcSSimon Schubert       struct cleanup *verify_cleanups = make_cleanup (xfree, check);
20135796c8dcSSimon Schubert 
20145796c8dcSSimon Schubert       if (target_read_memory (args->lma, check, bytes) != 0)
20155796c8dcSSimon Schubert 	error (_("Download verify read failed at %s"),
2016*ef5ccd6cSJohn Marino 	       paddress (target_gdbarch (), args->lma));
20175796c8dcSSimon Schubert       if (memcmp (args->buffer, check, bytes) != 0)
20185796c8dcSSimon Schubert 	error (_("Download verify compare failed at %s"),
2019*ef5ccd6cSJohn Marino 	       paddress (target_gdbarch (), args->lma));
20205796c8dcSSimon Schubert       do_cleanups (verify_cleanups);
20215796c8dcSSimon Schubert     }
20225796c8dcSSimon Schubert   totals->data_count += bytes;
20235796c8dcSSimon Schubert   args->lma += bytes;
20245796c8dcSSimon Schubert   args->buffer += bytes;
20255796c8dcSSimon Schubert   totals->write_count += 1;
20265796c8dcSSimon Schubert   args->section_sent += bytes;
2027*ef5ccd6cSJohn Marino   if (check_quit_flag ()
20285796c8dcSSimon Schubert       || (deprecated_ui_load_progress_hook != NULL
20295796c8dcSSimon Schubert 	  && deprecated_ui_load_progress_hook (args->section_name,
20305796c8dcSSimon Schubert 					       args->section_sent)))
20315796c8dcSSimon Schubert     error (_("Canceled the download"));
20325796c8dcSSimon Schubert 
20335796c8dcSSimon Schubert   if (deprecated_show_load_progress != NULL)
20345796c8dcSSimon Schubert     deprecated_show_load_progress (args->section_name,
20355796c8dcSSimon Schubert 				   args->section_sent,
20365796c8dcSSimon Schubert 				   args->section_size,
20375796c8dcSSimon Schubert 				   totals->data_count,
20385796c8dcSSimon Schubert 				   totals->total_size);
20395796c8dcSSimon Schubert }
20405796c8dcSSimon Schubert 
20415796c8dcSSimon Schubert /* Callback service function for generic_load (bfd_map_over_sections).  */
20425796c8dcSSimon Schubert 
20435796c8dcSSimon Schubert static void
load_section_callback(bfd * abfd,asection * asec,void * data)20445796c8dcSSimon Schubert load_section_callback (bfd *abfd, asection *asec, void *data)
20455796c8dcSSimon Schubert {
20465796c8dcSSimon Schubert   struct memory_write_request *new_request;
20475796c8dcSSimon Schubert   struct load_section_data *args = data;
20485796c8dcSSimon Schubert   struct load_progress_section_data *section_data;
20495796c8dcSSimon Schubert   bfd_size_type size = bfd_get_section_size (asec);
20505796c8dcSSimon Schubert   gdb_byte *buffer;
20515796c8dcSSimon Schubert   const char *sect_name = bfd_get_section_name (abfd, asec);
20525796c8dcSSimon Schubert 
20535796c8dcSSimon Schubert   if ((bfd_get_section_flags (abfd, asec) & SEC_LOAD) == 0)
20545796c8dcSSimon Schubert     return;
20555796c8dcSSimon Schubert 
20565796c8dcSSimon Schubert   if (size == 0)
20575796c8dcSSimon Schubert     return;
20585796c8dcSSimon Schubert 
20595796c8dcSSimon Schubert   new_request = VEC_safe_push (memory_write_request_s,
20605796c8dcSSimon Schubert 			       args->requests, NULL);
20615796c8dcSSimon Schubert   memset (new_request, 0, sizeof (struct memory_write_request));
20625796c8dcSSimon Schubert   section_data = xcalloc (1, sizeof (struct load_progress_section_data));
20635796c8dcSSimon Schubert   new_request->begin = bfd_section_lma (abfd, asec) + args->load_offset;
2064c50c785cSJohn Marino   new_request->end = new_request->begin + size; /* FIXME Should size
2065c50c785cSJohn Marino 						   be in instead?  */
20665796c8dcSSimon Schubert   new_request->data = xmalloc (size);
20675796c8dcSSimon Schubert   new_request->baton = section_data;
20685796c8dcSSimon Schubert 
20695796c8dcSSimon Schubert   buffer = new_request->data;
20705796c8dcSSimon Schubert 
20715796c8dcSSimon Schubert   section_data->cumulative = args->progress_data;
20725796c8dcSSimon Schubert   section_data->section_name = sect_name;
20735796c8dcSSimon Schubert   section_data->section_size = size;
20745796c8dcSSimon Schubert   section_data->lma = new_request->begin;
20755796c8dcSSimon Schubert   section_data->buffer = buffer;
20765796c8dcSSimon Schubert 
20775796c8dcSSimon Schubert   bfd_get_section_contents (abfd, asec, buffer, 0, size);
20785796c8dcSSimon Schubert }
20795796c8dcSSimon Schubert 
20805796c8dcSSimon Schubert /* Clean up an entire memory request vector, including load
20815796c8dcSSimon Schubert    data and progress records.  */
20825796c8dcSSimon Schubert 
20835796c8dcSSimon Schubert static void
clear_memory_write_data(void * arg)20845796c8dcSSimon Schubert clear_memory_write_data (void *arg)
20855796c8dcSSimon Schubert {
20865796c8dcSSimon Schubert   VEC(memory_write_request_s) **vec_p = arg;
20875796c8dcSSimon Schubert   VEC(memory_write_request_s) *vec = *vec_p;
20885796c8dcSSimon Schubert   int i;
20895796c8dcSSimon Schubert   struct memory_write_request *mr;
20905796c8dcSSimon Schubert 
20915796c8dcSSimon Schubert   for (i = 0; VEC_iterate (memory_write_request_s, vec, i, mr); ++i)
20925796c8dcSSimon Schubert     {
20935796c8dcSSimon Schubert       xfree (mr->data);
20945796c8dcSSimon Schubert       xfree (mr->baton);
20955796c8dcSSimon Schubert     }
20965796c8dcSSimon Schubert   VEC_free (memory_write_request_s, vec);
20975796c8dcSSimon Schubert }
20985796c8dcSSimon Schubert 
20995796c8dcSSimon Schubert void
generic_load(char * args,int from_tty)21005796c8dcSSimon Schubert generic_load (char *args, int from_tty)
21015796c8dcSSimon Schubert {
21025796c8dcSSimon Schubert   bfd *loadfile_bfd;
21035796c8dcSSimon Schubert   struct timeval start_time, end_time;
21045796c8dcSSimon Schubert   char *filename;
21055796c8dcSSimon Schubert   struct cleanup *old_cleanups = make_cleanup (null_cleanup, 0);
21065796c8dcSSimon Schubert   struct load_section_data cbdata;
21075796c8dcSSimon Schubert   struct load_progress_data total_progress;
2108a45ae5f8SJohn Marino   struct ui_out *uiout = current_uiout;
21095796c8dcSSimon Schubert 
21105796c8dcSSimon Schubert   CORE_ADDR entry;
21115796c8dcSSimon Schubert   char **argv;
21125796c8dcSSimon Schubert 
21135796c8dcSSimon Schubert   memset (&cbdata, 0, sizeof (cbdata));
21145796c8dcSSimon Schubert   memset (&total_progress, 0, sizeof (total_progress));
21155796c8dcSSimon Schubert   cbdata.progress_data = &total_progress;
21165796c8dcSSimon Schubert 
21175796c8dcSSimon Schubert   make_cleanup (clear_memory_write_data, &cbdata.requests);
21185796c8dcSSimon Schubert 
21195796c8dcSSimon Schubert   if (args == NULL)
21205796c8dcSSimon Schubert     error_no_arg (_("file to load"));
21215796c8dcSSimon Schubert 
21225796c8dcSSimon Schubert   argv = gdb_buildargv (args);
21235796c8dcSSimon Schubert   make_cleanup_freeargv (argv);
21245796c8dcSSimon Schubert 
21255796c8dcSSimon Schubert   filename = tilde_expand (argv[0]);
21265796c8dcSSimon Schubert   make_cleanup (xfree, filename);
21275796c8dcSSimon Schubert 
21285796c8dcSSimon Schubert   if (argv[1] != NULL)
21295796c8dcSSimon Schubert     {
2130*ef5ccd6cSJohn Marino       const char *endptr;
21315796c8dcSSimon Schubert 
2132*ef5ccd6cSJohn Marino       cbdata.load_offset = strtoulst (argv[1], &endptr, 0);
21335796c8dcSSimon Schubert 
21345796c8dcSSimon Schubert       /* If the last word was not a valid number then
21355796c8dcSSimon Schubert          treat it as a file name with spaces in.  */
21365796c8dcSSimon Schubert       if (argv[1] == endptr)
21375796c8dcSSimon Schubert         error (_("Invalid download offset:%s."), argv[1]);
21385796c8dcSSimon Schubert 
21395796c8dcSSimon Schubert       if (argv[2] != NULL)
21405796c8dcSSimon Schubert 	error (_("Too many parameters."));
21415796c8dcSSimon Schubert     }
21425796c8dcSSimon Schubert 
21435796c8dcSSimon Schubert   /* Open the file for loading.  */
2144*ef5ccd6cSJohn Marino   loadfile_bfd = gdb_bfd_open (filename, gnutarget, -1);
21455796c8dcSSimon Schubert   if (loadfile_bfd == NULL)
21465796c8dcSSimon Schubert     {
21475796c8dcSSimon Schubert       perror_with_name (filename);
21485796c8dcSSimon Schubert       return;
21495796c8dcSSimon Schubert     }
21505796c8dcSSimon Schubert 
2151*ef5ccd6cSJohn Marino   make_cleanup_bfd_unref (loadfile_bfd);
21525796c8dcSSimon Schubert 
21535796c8dcSSimon Schubert   if (!bfd_check_format (loadfile_bfd, bfd_object))
21545796c8dcSSimon Schubert     {
21555796c8dcSSimon Schubert       error (_("\"%s\" is not an object file: %s"), filename,
21565796c8dcSSimon Schubert 	     bfd_errmsg (bfd_get_error ()));
21575796c8dcSSimon Schubert     }
21585796c8dcSSimon Schubert 
21595796c8dcSSimon Schubert   bfd_map_over_sections (loadfile_bfd, add_section_size_callback,
21605796c8dcSSimon Schubert 			 (void *) &total_progress.total_size);
21615796c8dcSSimon Schubert 
21625796c8dcSSimon Schubert   bfd_map_over_sections (loadfile_bfd, load_section_callback, &cbdata);
21635796c8dcSSimon Schubert 
21645796c8dcSSimon Schubert   gettimeofday (&start_time, NULL);
21655796c8dcSSimon Schubert 
21665796c8dcSSimon Schubert   if (target_write_memory_blocks (cbdata.requests, flash_discard,
21675796c8dcSSimon Schubert 				  load_progress) != 0)
21685796c8dcSSimon Schubert     error (_("Load failed"));
21695796c8dcSSimon Schubert 
21705796c8dcSSimon Schubert   gettimeofday (&end_time, NULL);
21715796c8dcSSimon Schubert 
21725796c8dcSSimon Schubert   entry = bfd_get_start_address (loadfile_bfd);
2173*ef5ccd6cSJohn Marino   entry = gdbarch_addr_bits_remove (target_gdbarch (), entry);
21745796c8dcSSimon Schubert   ui_out_text (uiout, "Start address ");
2175*ef5ccd6cSJohn Marino   ui_out_field_fmt (uiout, "address", "%s", paddress (target_gdbarch (), entry));
21765796c8dcSSimon Schubert   ui_out_text (uiout, ", load size ");
21775796c8dcSSimon Schubert   ui_out_field_fmt (uiout, "load-size", "%lu", total_progress.data_count);
21785796c8dcSSimon Schubert   ui_out_text (uiout, "\n");
21795796c8dcSSimon Schubert   /* We were doing this in remote-mips.c, I suspect it is right
21805796c8dcSSimon Schubert      for other targets too.  */
21815796c8dcSSimon Schubert   regcache_write_pc (get_current_regcache (), entry);
21825796c8dcSSimon Schubert 
2183cf7f2e2dSJohn Marino   /* Reset breakpoints, now that we have changed the load image.  For
2184cf7f2e2dSJohn Marino      instance, breakpoints may have been set (or reset, by
2185cf7f2e2dSJohn Marino      post_create_inferior) while connected to the target but before we
2186cf7f2e2dSJohn Marino      loaded the program.  In that case, the prologue analyzer could
2187cf7f2e2dSJohn Marino      have read instructions from the target to find the right
2188cf7f2e2dSJohn Marino      breakpoint locations.  Loading has changed the contents of that
2189cf7f2e2dSJohn Marino      memory.  */
2190cf7f2e2dSJohn Marino 
2191cf7f2e2dSJohn Marino   breakpoint_re_set ();
2192cf7f2e2dSJohn Marino 
21935796c8dcSSimon Schubert   /* FIXME: are we supposed to call symbol_file_add or not?  According
21945796c8dcSSimon Schubert      to a comment from remote-mips.c (where a call to symbol_file_add
21955796c8dcSSimon Schubert      was commented out), making the call confuses GDB if more than one
21965796c8dcSSimon Schubert      file is loaded in.  Some targets do (e.g., remote-vx.c) but
21975796c8dcSSimon Schubert      others don't (or didn't - perhaps they have all been deleted).  */
21985796c8dcSSimon Schubert 
21995796c8dcSSimon Schubert   print_transfer_performance (gdb_stdout, total_progress.data_count,
22005796c8dcSSimon Schubert 			      total_progress.write_count,
22015796c8dcSSimon Schubert 			      &start_time, &end_time);
22025796c8dcSSimon Schubert 
22035796c8dcSSimon Schubert   do_cleanups (old_cleanups);
22045796c8dcSSimon Schubert }
22055796c8dcSSimon Schubert 
22065796c8dcSSimon Schubert /* Report how fast the transfer went.  */
22075796c8dcSSimon Schubert 
22085796c8dcSSimon Schubert void
print_transfer_performance(struct ui_file * stream,unsigned long data_count,unsigned long write_count,const struct timeval * start_time,const struct timeval * end_time)22095796c8dcSSimon Schubert print_transfer_performance (struct ui_file *stream,
22105796c8dcSSimon Schubert 			    unsigned long data_count,
22115796c8dcSSimon Schubert 			    unsigned long write_count,
22125796c8dcSSimon Schubert 			    const struct timeval *start_time,
22135796c8dcSSimon Schubert 			    const struct timeval *end_time)
22145796c8dcSSimon Schubert {
22155796c8dcSSimon Schubert   ULONGEST time_count;
2216a45ae5f8SJohn Marino   struct ui_out *uiout = current_uiout;
22175796c8dcSSimon Schubert 
22185796c8dcSSimon Schubert   /* Compute the elapsed time in milliseconds, as a tradeoff between
22195796c8dcSSimon Schubert      accuracy and overflow.  */
22205796c8dcSSimon Schubert   time_count = (end_time->tv_sec - start_time->tv_sec) * 1000;
22215796c8dcSSimon Schubert   time_count += (end_time->tv_usec - start_time->tv_usec) / 1000;
22225796c8dcSSimon Schubert 
22235796c8dcSSimon Schubert   ui_out_text (uiout, "Transfer rate: ");
22245796c8dcSSimon Schubert   if (time_count > 0)
22255796c8dcSSimon Schubert     {
22265796c8dcSSimon Schubert       unsigned long rate = ((ULONGEST) data_count * 1000) / time_count;
22275796c8dcSSimon Schubert 
22285796c8dcSSimon Schubert       if (ui_out_is_mi_like_p (uiout))
22295796c8dcSSimon Schubert 	{
22305796c8dcSSimon Schubert 	  ui_out_field_fmt (uiout, "transfer-rate", "%lu", rate * 8);
22315796c8dcSSimon Schubert 	  ui_out_text (uiout, " bits/sec");
22325796c8dcSSimon Schubert 	}
22335796c8dcSSimon Schubert       else if (rate < 1024)
22345796c8dcSSimon Schubert 	{
22355796c8dcSSimon Schubert 	  ui_out_field_fmt (uiout, "transfer-rate", "%lu", rate);
22365796c8dcSSimon Schubert 	  ui_out_text (uiout, " bytes/sec");
22375796c8dcSSimon Schubert 	}
22385796c8dcSSimon Schubert       else
22395796c8dcSSimon Schubert 	{
22405796c8dcSSimon Schubert 	  ui_out_field_fmt (uiout, "transfer-rate", "%lu", rate / 1024);
22415796c8dcSSimon Schubert 	  ui_out_text (uiout, " KB/sec");
22425796c8dcSSimon Schubert 	}
22435796c8dcSSimon Schubert     }
22445796c8dcSSimon Schubert   else
22455796c8dcSSimon Schubert     {
22465796c8dcSSimon Schubert       ui_out_field_fmt (uiout, "transferred-bits", "%lu", (data_count * 8));
22475796c8dcSSimon Schubert       ui_out_text (uiout, " bits in <1 sec");
22485796c8dcSSimon Schubert     }
22495796c8dcSSimon Schubert   if (write_count > 0)
22505796c8dcSSimon Schubert     {
22515796c8dcSSimon Schubert       ui_out_text (uiout, ", ");
22525796c8dcSSimon Schubert       ui_out_field_fmt (uiout, "write-rate", "%lu", data_count / write_count);
22535796c8dcSSimon Schubert       ui_out_text (uiout, " bytes/write");
22545796c8dcSSimon Schubert     }
22555796c8dcSSimon Schubert   ui_out_text (uiout, ".\n");
22565796c8dcSSimon Schubert }
22575796c8dcSSimon Schubert 
22585796c8dcSSimon Schubert /* This function allows the addition of incrementally linked object files.
22595796c8dcSSimon Schubert    It does not modify any state in the target, only in the debugger.  */
22605796c8dcSSimon Schubert /* Note: ezannoni 2000-04-13 This function/command used to have a
22615796c8dcSSimon Schubert    special case syntax for the rombug target (Rombug is the boot
22625796c8dcSSimon Schubert    monitor for Microware's OS-9 / OS-9000, see remote-os9k.c). In the
22635796c8dcSSimon Schubert    rombug case, the user doesn't need to supply a text address,
22645796c8dcSSimon Schubert    instead a call to target_link() (in target.c) would supply the
22655796c8dcSSimon Schubert    value to use.  We are now discontinuing this type of ad hoc syntax.  */
22665796c8dcSSimon Schubert 
22675796c8dcSSimon Schubert static void
add_symbol_file_command(char * args,int from_tty)22685796c8dcSSimon Schubert add_symbol_file_command (char *args, int from_tty)
22695796c8dcSSimon Schubert {
22705796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_current_arch ();
22715796c8dcSSimon Schubert   char *filename = NULL;
22725796c8dcSSimon Schubert   int flags = OBJF_USERLOADED;
22735796c8dcSSimon Schubert   char *arg;
22745796c8dcSSimon Schubert   int section_index = 0;
22755796c8dcSSimon Schubert   int argcnt = 0;
22765796c8dcSSimon Schubert   int sec_num = 0;
22775796c8dcSSimon Schubert   int i;
22785796c8dcSSimon Schubert   int expecting_sec_name = 0;
22795796c8dcSSimon Schubert   int expecting_sec_addr = 0;
22805796c8dcSSimon Schubert   char **argv;
22815796c8dcSSimon Schubert 
22825796c8dcSSimon Schubert   struct sect_opt
22835796c8dcSSimon Schubert   {
22845796c8dcSSimon Schubert     char *name;
22855796c8dcSSimon Schubert     char *value;
22865796c8dcSSimon Schubert   };
22875796c8dcSSimon Schubert 
22885796c8dcSSimon Schubert   struct section_addr_info *section_addrs;
22895796c8dcSSimon Schubert   struct sect_opt *sect_opts = NULL;
22905796c8dcSSimon Schubert   size_t num_sect_opts = 0;
22915796c8dcSSimon Schubert   struct cleanup *my_cleanups = make_cleanup (null_cleanup, NULL);
22925796c8dcSSimon Schubert 
22935796c8dcSSimon Schubert   num_sect_opts = 16;
22945796c8dcSSimon Schubert   sect_opts = (struct sect_opt *) xmalloc (num_sect_opts
22955796c8dcSSimon Schubert 					   * sizeof (struct sect_opt));
22965796c8dcSSimon Schubert 
22975796c8dcSSimon Schubert   dont_repeat ();
22985796c8dcSSimon Schubert 
22995796c8dcSSimon Schubert   if (args == NULL)
23005796c8dcSSimon Schubert     error (_("add-symbol-file takes a file name and an address"));
23015796c8dcSSimon Schubert 
23025796c8dcSSimon Schubert   argv = gdb_buildargv (args);
23035796c8dcSSimon Schubert   make_cleanup_freeargv (argv);
23045796c8dcSSimon Schubert 
23055796c8dcSSimon Schubert   for (arg = argv[0], argcnt = 0; arg != NULL; arg = argv[++argcnt])
23065796c8dcSSimon Schubert     {
23075796c8dcSSimon Schubert       /* Process the argument.  */
23085796c8dcSSimon Schubert       if (argcnt == 0)
23095796c8dcSSimon Schubert 	{
23105796c8dcSSimon Schubert 	  /* The first argument is the file name.  */
23115796c8dcSSimon Schubert 	  filename = tilde_expand (arg);
23125796c8dcSSimon Schubert 	  make_cleanup (xfree, filename);
23135796c8dcSSimon Schubert 	}
23145796c8dcSSimon Schubert       else
23155796c8dcSSimon Schubert 	if (argcnt == 1)
23165796c8dcSSimon Schubert 	  {
23175796c8dcSSimon Schubert 	    /* The second argument is always the text address at which
23185796c8dcSSimon Schubert                to load the program.  */
23195796c8dcSSimon Schubert 	    sect_opts[section_index].name = ".text";
23205796c8dcSSimon Schubert 	    sect_opts[section_index].value = arg;
23215796c8dcSSimon Schubert 	    if (++section_index >= num_sect_opts)
23225796c8dcSSimon Schubert 	      {
23235796c8dcSSimon Schubert 		num_sect_opts *= 2;
23245796c8dcSSimon Schubert 		sect_opts = ((struct sect_opt *)
23255796c8dcSSimon Schubert 			     xrealloc (sect_opts,
23265796c8dcSSimon Schubert 				       num_sect_opts
23275796c8dcSSimon Schubert 				       * sizeof (struct sect_opt)));
23285796c8dcSSimon Schubert 	      }
23295796c8dcSSimon Schubert 	  }
23305796c8dcSSimon Schubert 	else
23315796c8dcSSimon Schubert 	  {
23325796c8dcSSimon Schubert 	    /* It's an option (starting with '-') or it's an argument
2333c50c785cSJohn Marino 	       to an option.  */
23345796c8dcSSimon Schubert 
23355796c8dcSSimon Schubert 	    if (*arg == '-')
23365796c8dcSSimon Schubert 	      {
23375796c8dcSSimon Schubert 		if (strcmp (arg, "-readnow") == 0)
23385796c8dcSSimon Schubert 		  flags |= OBJF_READNOW;
23395796c8dcSSimon Schubert 		else if (strcmp (arg, "-s") == 0)
23405796c8dcSSimon Schubert 		  {
23415796c8dcSSimon Schubert 		    expecting_sec_name = 1;
23425796c8dcSSimon Schubert 		    expecting_sec_addr = 1;
23435796c8dcSSimon Schubert 		  }
23445796c8dcSSimon Schubert 	      }
23455796c8dcSSimon Schubert 	    else
23465796c8dcSSimon Schubert 	      {
23475796c8dcSSimon Schubert 		if (expecting_sec_name)
23485796c8dcSSimon Schubert 		  {
23495796c8dcSSimon Schubert 		    sect_opts[section_index].name = arg;
23505796c8dcSSimon Schubert 		    expecting_sec_name = 0;
23515796c8dcSSimon Schubert 		  }
23525796c8dcSSimon Schubert 		else
23535796c8dcSSimon Schubert 		  if (expecting_sec_addr)
23545796c8dcSSimon Schubert 		    {
23555796c8dcSSimon Schubert 		      sect_opts[section_index].value = arg;
23565796c8dcSSimon Schubert 		      expecting_sec_addr = 0;
23575796c8dcSSimon Schubert 		      if (++section_index >= num_sect_opts)
23585796c8dcSSimon Schubert 			{
23595796c8dcSSimon Schubert 			  num_sect_opts *= 2;
23605796c8dcSSimon Schubert 			  sect_opts = ((struct sect_opt *)
23615796c8dcSSimon Schubert 				       xrealloc (sect_opts,
23625796c8dcSSimon Schubert 						 num_sect_opts
23635796c8dcSSimon Schubert 						 * sizeof (struct sect_opt)));
23645796c8dcSSimon Schubert 			}
23655796c8dcSSimon Schubert 		    }
23665796c8dcSSimon Schubert 		  else
2367c50c785cSJohn Marino 		    error (_("USAGE: add-symbol-file <filename> <textaddress>"
2368a45ae5f8SJohn Marino 			     " [-readnow] [-s <secname> <addr>]*"));
23695796c8dcSSimon Schubert 	      }
23705796c8dcSSimon Schubert 	  }
23715796c8dcSSimon Schubert     }
23725796c8dcSSimon Schubert 
23735796c8dcSSimon Schubert   /* This command takes at least two arguments.  The first one is a
23745796c8dcSSimon Schubert      filename, and the second is the address where this file has been
23755796c8dcSSimon Schubert      loaded.  Abort now if this address hasn't been provided by the
23765796c8dcSSimon Schubert      user.  */
23775796c8dcSSimon Schubert   if (section_index < 1)
23785796c8dcSSimon Schubert     error (_("The address where %s has been loaded is missing"), filename);
23795796c8dcSSimon Schubert 
23805796c8dcSSimon Schubert   /* Print the prompt for the query below.  And save the arguments into
23815796c8dcSSimon Schubert      a sect_addr_info structure to be passed around to other
23825796c8dcSSimon Schubert      functions.  We have to split this up into separate print
23835796c8dcSSimon Schubert      statements because hex_string returns a local static
23845796c8dcSSimon Schubert      string.  */
23855796c8dcSSimon Schubert 
23865796c8dcSSimon Schubert   printf_unfiltered (_("add symbol table from file \"%s\" at\n"), filename);
23875796c8dcSSimon Schubert   section_addrs = alloc_section_addr_info (section_index);
23885796c8dcSSimon Schubert   make_cleanup (xfree, section_addrs);
23895796c8dcSSimon Schubert   for (i = 0; i < section_index; i++)
23905796c8dcSSimon Schubert     {
23915796c8dcSSimon Schubert       CORE_ADDR addr;
23925796c8dcSSimon Schubert       char *val = sect_opts[i].value;
23935796c8dcSSimon Schubert       char *sec = sect_opts[i].name;
23945796c8dcSSimon Schubert 
23955796c8dcSSimon Schubert       addr = parse_and_eval_address (val);
23965796c8dcSSimon Schubert 
23975796c8dcSSimon Schubert       /* Here we store the section offsets in the order they were
23985796c8dcSSimon Schubert          entered on the command line.  */
23995796c8dcSSimon Schubert       section_addrs->other[sec_num].name = sec;
24005796c8dcSSimon Schubert       section_addrs->other[sec_num].addr = addr;
24015796c8dcSSimon Schubert       printf_unfiltered ("\t%s_addr = %s\n", sec,
24025796c8dcSSimon Schubert 			 paddress (gdbarch, addr));
24035796c8dcSSimon Schubert       sec_num++;
24045796c8dcSSimon Schubert 
24055796c8dcSSimon Schubert       /* The object's sections are initialized when a
24065796c8dcSSimon Schubert 	 call is made to build_objfile_section_table (objfile).
24075796c8dcSSimon Schubert 	 This happens in reread_symbols.
24085796c8dcSSimon Schubert 	 At this point, we don't know what file type this is,
24095796c8dcSSimon Schubert 	 so we can't determine what section names are valid.  */
24105796c8dcSSimon Schubert     }
24115796c8dcSSimon Schubert 
24125796c8dcSSimon Schubert   if (from_tty && (!query ("%s", "")))
24135796c8dcSSimon Schubert     error (_("Not confirmed."));
24145796c8dcSSimon Schubert 
24155796c8dcSSimon Schubert   symbol_file_add (filename, from_tty ? SYMFILE_VERBOSE : 0,
24165796c8dcSSimon Schubert                    section_addrs, flags);
24175796c8dcSSimon Schubert 
24185796c8dcSSimon Schubert   /* Getting new symbols may change our opinion about what is
24195796c8dcSSimon Schubert      frameless.  */
24205796c8dcSSimon Schubert   reinit_frame_cache ();
24215796c8dcSSimon Schubert   do_cleanups (my_cleanups);
24225796c8dcSSimon Schubert }
24235796c8dcSSimon Schubert 
24245796c8dcSSimon Schubert 
2425*ef5ccd6cSJohn Marino typedef struct objfile *objfilep;
2426*ef5ccd6cSJohn Marino 
2427*ef5ccd6cSJohn Marino DEF_VEC_P (objfilep);
2428*ef5ccd6cSJohn Marino 
24295796c8dcSSimon Schubert /* Re-read symbols if a symbol-file has changed.  */
24305796c8dcSSimon Schubert void
reread_symbols(void)24315796c8dcSSimon Schubert reread_symbols (void)
24325796c8dcSSimon Schubert {
24335796c8dcSSimon Schubert   struct objfile *objfile;
24345796c8dcSSimon Schubert   long new_modtime;
24355796c8dcSSimon Schubert   struct stat new_statbuf;
24365796c8dcSSimon Schubert   int res;
2437*ef5ccd6cSJohn Marino   VEC (objfilep) *new_objfiles = NULL;
2438*ef5ccd6cSJohn Marino   struct cleanup *all_cleanups;
2439*ef5ccd6cSJohn Marino 
2440*ef5ccd6cSJohn Marino   all_cleanups = make_cleanup (VEC_cleanup (objfilep), &new_objfiles);
24415796c8dcSSimon Schubert 
24425796c8dcSSimon Schubert   /* With the addition of shared libraries, this should be modified,
24435796c8dcSSimon Schubert      the load time should be saved in the partial symbol tables, since
24445796c8dcSSimon Schubert      different tables may come from different source files.  FIXME.
24455796c8dcSSimon Schubert      This routine should then walk down each partial symbol table
2446c50c785cSJohn Marino      and see if the symbol table that it originates from has been changed.  */
24475796c8dcSSimon Schubert 
24485796c8dcSSimon Schubert   for (objfile = object_files; objfile; objfile = objfile->next)
24495796c8dcSSimon Schubert     {
2450cf7f2e2dSJohn Marino       /* solib-sunos.c creates one objfile with obfd.  */
2451cf7f2e2dSJohn Marino       if (objfile->obfd == NULL)
2452cf7f2e2dSJohn Marino 	continue;
24535796c8dcSSimon Schubert 
2454cf7f2e2dSJohn Marino       /* Separate debug objfiles are handled in the main objfile.  */
2455cf7f2e2dSJohn Marino       if (objfile->separate_debug_objfile_backlink)
2456cf7f2e2dSJohn Marino 	continue;
2457cf7f2e2dSJohn Marino 
2458cf7f2e2dSJohn Marino       /* If this object is from an archive (what you usually create with
2459cf7f2e2dSJohn Marino 	 `ar', often called a `static library' on most systems, though
2460cf7f2e2dSJohn Marino 	 a `shared library' on AIX is also an archive), then you should
2461cf7f2e2dSJohn Marino 	 stat on the archive name, not member name.  */
24625796c8dcSSimon Schubert       if (objfile->obfd->my_archive)
24635796c8dcSSimon Schubert 	res = stat (objfile->obfd->my_archive->filename, &new_statbuf);
24645796c8dcSSimon Schubert       else
24655796c8dcSSimon Schubert 	res = stat (objfile->name, &new_statbuf);
24665796c8dcSSimon Schubert       if (res != 0)
24675796c8dcSSimon Schubert 	{
24685796c8dcSSimon Schubert 	  /* FIXME, should use print_sys_errmsg but it's not filtered.  */
24695796c8dcSSimon Schubert 	  printf_unfiltered (_("`%s' has disappeared; keeping its symbols.\n"),
24705796c8dcSSimon Schubert 			     objfile->name);
24715796c8dcSSimon Schubert 	  continue;
24725796c8dcSSimon Schubert 	}
24735796c8dcSSimon Schubert       new_modtime = new_statbuf.st_mtime;
24745796c8dcSSimon Schubert       if (new_modtime != objfile->mtime)
24755796c8dcSSimon Schubert 	{
24765796c8dcSSimon Schubert 	  struct cleanup *old_cleanups;
24775796c8dcSSimon Schubert 	  struct section_offsets *offsets;
24785796c8dcSSimon Schubert 	  int num_offsets;
24795796c8dcSSimon Schubert 	  char *obfd_filename;
24805796c8dcSSimon Schubert 
24815796c8dcSSimon Schubert 	  printf_unfiltered (_("`%s' has changed; re-reading symbols.\n"),
24825796c8dcSSimon Schubert 			     objfile->name);
24835796c8dcSSimon Schubert 
24845796c8dcSSimon Schubert 	  /* There are various functions like symbol_file_add,
24855796c8dcSSimon Schubert 	     symfile_bfd_open, syms_from_objfile, etc., which might
24865796c8dcSSimon Schubert 	     appear to do what we want.  But they have various other
24875796c8dcSSimon Schubert 	     effects which we *don't* want.  So we just do stuff
24885796c8dcSSimon Schubert 	     ourselves.  We don't worry about mapped files (for one thing,
24895796c8dcSSimon Schubert 	     any mapped file will be out of date).  */
24905796c8dcSSimon Schubert 
24915796c8dcSSimon Schubert 	  /* If we get an error, blow away this objfile (not sure if
24925796c8dcSSimon Schubert 	     that is the correct response for things like shared
24935796c8dcSSimon Schubert 	     libraries).  */
24945796c8dcSSimon Schubert 	  old_cleanups = make_cleanup_free_objfile (objfile);
24955796c8dcSSimon Schubert 	  /* We need to do this whenever any symbols go away.  */
24965796c8dcSSimon Schubert 	  make_cleanup (clear_symtab_users_cleanup, 0 /*ignore*/);
24975796c8dcSSimon Schubert 
2498c50c785cSJohn Marino 	  if (exec_bfd != NULL
2499c50c785cSJohn Marino 	      && filename_cmp (bfd_get_filename (objfile->obfd),
25005796c8dcSSimon Schubert 			       bfd_get_filename (exec_bfd)) == 0)
25015796c8dcSSimon Schubert 	    {
25025796c8dcSSimon Schubert 	      /* Reload EXEC_BFD without asking anything.  */
25035796c8dcSSimon Schubert 
25045796c8dcSSimon Schubert 	      exec_file_attach (bfd_get_filename (objfile->obfd), 0);
25055796c8dcSSimon Schubert 	    }
25065796c8dcSSimon Schubert 
2507a45ae5f8SJohn Marino 	  /* Keep the calls order approx. the same as in free_objfile.  */
2508a45ae5f8SJohn Marino 
2509a45ae5f8SJohn Marino 	  /* Free the separate debug objfiles.  It will be
2510a45ae5f8SJohn Marino 	     automatically recreated by sym_read.  */
2511a45ae5f8SJohn Marino 	  free_objfile_separate_debug (objfile);
2512a45ae5f8SJohn Marino 
2513a45ae5f8SJohn Marino 	  /* Remove any references to this objfile in the global
2514a45ae5f8SJohn Marino 	     value lists.  */
2515a45ae5f8SJohn Marino 	  preserve_values (objfile);
2516a45ae5f8SJohn Marino 
2517a45ae5f8SJohn Marino 	  /* Nuke all the state that we will re-read.  Much of the following
2518a45ae5f8SJohn Marino 	     code which sets things to NULL really is necessary to tell
2519a45ae5f8SJohn Marino 	     other parts of GDB that there is nothing currently there.
2520a45ae5f8SJohn Marino 
2521a45ae5f8SJohn Marino 	     Try to keep the freeing order compatible with free_objfile.  */
2522a45ae5f8SJohn Marino 
2523a45ae5f8SJohn Marino 	  if (objfile->sf != NULL)
2524a45ae5f8SJohn Marino 	    {
2525a45ae5f8SJohn Marino 	      (*objfile->sf->sym_finish) (objfile);
2526a45ae5f8SJohn Marino 	    }
2527a45ae5f8SJohn Marino 
2528a45ae5f8SJohn Marino 	  clear_objfile_data (objfile);
2529a45ae5f8SJohn Marino 
2530*ef5ccd6cSJohn Marino 	  /* Clean up any state BFD has sitting around.  */
2531*ef5ccd6cSJohn Marino 	  {
2532*ef5ccd6cSJohn Marino 	    struct bfd *obfd = objfile->obfd;
2533*ef5ccd6cSJohn Marino 
25345796c8dcSSimon Schubert 	    obfd_filename = bfd_get_filename (objfile->obfd);
2535*ef5ccd6cSJohn Marino 	    /* Open the new BFD before freeing the old one, so that
2536*ef5ccd6cSJohn Marino 	       the filename remains live.  */
2537*ef5ccd6cSJohn Marino 	    objfile->obfd = gdb_bfd_open_maybe_remote (obfd_filename);
25385796c8dcSSimon Schubert 	    if (objfile->obfd == NULL)
2539*ef5ccd6cSJohn Marino 	      {
2540*ef5ccd6cSJohn Marino 		/* We have to make a cleanup and error here, rather
2541*ef5ccd6cSJohn Marino 		   than erroring later, because once we unref OBFD,
2542*ef5ccd6cSJohn Marino 		   OBFD_FILENAME will be freed.  */
2543*ef5ccd6cSJohn Marino 		make_cleanup_bfd_unref (obfd);
2544*ef5ccd6cSJohn Marino 		error (_("Can't open %s to read symbols."), obfd_filename);
2545*ef5ccd6cSJohn Marino 	      }
2546*ef5ccd6cSJohn Marino 	    gdb_bfd_unref (obfd);
2547*ef5ccd6cSJohn Marino 	  }
2548*ef5ccd6cSJohn Marino 
2549*ef5ccd6cSJohn Marino 	  objfile->name = bfd_get_filename (objfile->obfd);
25505796c8dcSSimon Schubert 	  /* bfd_openr sets cacheable to true, which is what we want.  */
25515796c8dcSSimon Schubert 	  if (!bfd_check_format (objfile->obfd, bfd_object))
25525796c8dcSSimon Schubert 	    error (_("Can't read symbols from %s: %s."), objfile->name,
25535796c8dcSSimon Schubert 		   bfd_errmsg (bfd_get_error ()));
25545796c8dcSSimon Schubert 
25555796c8dcSSimon Schubert 	  /* Save the offsets, we will nuke them with the rest of the
25565796c8dcSSimon Schubert 	     objfile_obstack.  */
25575796c8dcSSimon Schubert 	  num_offsets = objfile->num_sections;
25585796c8dcSSimon Schubert 	  offsets = ((struct section_offsets *)
25595796c8dcSSimon Schubert 		     alloca (SIZEOF_N_SECTION_OFFSETS (num_offsets)));
25605796c8dcSSimon Schubert 	  memcpy (offsets, objfile->section_offsets,
25615796c8dcSSimon Schubert 		  SIZEOF_N_SECTION_OFFSETS (num_offsets));
25625796c8dcSSimon Schubert 
25635796c8dcSSimon Schubert 	  /* FIXME: Do we have to free a whole linked list, or is this
25645796c8dcSSimon Schubert 	     enough?  */
25655796c8dcSSimon Schubert 	  if (objfile->global_psymbols.list)
25665796c8dcSSimon Schubert 	    xfree (objfile->global_psymbols.list);
25675796c8dcSSimon Schubert 	  memset (&objfile->global_psymbols, 0,
25685796c8dcSSimon Schubert 		  sizeof (objfile->global_psymbols));
25695796c8dcSSimon Schubert 	  if (objfile->static_psymbols.list)
25705796c8dcSSimon Schubert 	    xfree (objfile->static_psymbols.list);
25715796c8dcSSimon Schubert 	  memset (&objfile->static_psymbols, 0,
25725796c8dcSSimon Schubert 		  sizeof (objfile->static_psymbols));
25735796c8dcSSimon Schubert 
2574c50c785cSJohn Marino 	  /* Free the obstacks for non-reusable objfiles.  */
2575c50c785cSJohn Marino 	  psymbol_bcache_free (objfile->psymbol_cache);
2576c50c785cSJohn Marino 	  objfile->psymbol_cache = psymbol_bcache_init ();
25775796c8dcSSimon Schubert 	  if (objfile->demangled_names_hash != NULL)
25785796c8dcSSimon Schubert 	    {
25795796c8dcSSimon Schubert 	      htab_delete (objfile->demangled_names_hash);
25805796c8dcSSimon Schubert 	      objfile->demangled_names_hash = NULL;
25815796c8dcSSimon Schubert 	    }
25825796c8dcSSimon Schubert 	  obstack_free (&objfile->objfile_obstack, 0);
25835796c8dcSSimon Schubert 	  objfile->sections = NULL;
25845796c8dcSSimon Schubert 	  objfile->symtabs = NULL;
25855796c8dcSSimon Schubert 	  objfile->psymtabs = NULL;
25865796c8dcSSimon Schubert 	  objfile->psymtabs_addrmap = NULL;
25875796c8dcSSimon Schubert 	  objfile->free_psymtabs = NULL;
2588c50c785cSJohn Marino 	  objfile->template_symbols = NULL;
25895796c8dcSSimon Schubert 	  objfile->msymbols = NULL;
25905796c8dcSSimon Schubert 	  objfile->minimal_symbol_count = 0;
25915796c8dcSSimon Schubert 	  memset (&objfile->msymbol_hash, 0,
25925796c8dcSSimon Schubert 		  sizeof (objfile->msymbol_hash));
25935796c8dcSSimon Schubert 	  memset (&objfile->msymbol_demangled_hash, 0,
25945796c8dcSSimon Schubert 		  sizeof (objfile->msymbol_demangled_hash));
25955796c8dcSSimon Schubert 
2596*ef5ccd6cSJohn Marino 	  set_objfile_per_bfd (objfile);
2597*ef5ccd6cSJohn Marino 
25985796c8dcSSimon Schubert 	  /* obstack_init also initializes the obstack so it is
25995796c8dcSSimon Schubert 	     empty.  We could use obstack_specify_allocation but
2600*ef5ccd6cSJohn Marino 	     gdb_obstack.h specifies the alloc/dealloc functions.  */
26015796c8dcSSimon Schubert 	  obstack_init (&objfile->objfile_obstack);
2602*ef5ccd6cSJohn Marino 	  build_objfile_section_table (objfile);
26035796c8dcSSimon Schubert 	  terminate_minimal_symbol_table (objfile);
26045796c8dcSSimon Schubert 
26055796c8dcSSimon Schubert 	  /* We use the same section offsets as from last time.  I'm not
26065796c8dcSSimon Schubert 	     sure whether that is always correct for shared libraries.  */
26075796c8dcSSimon Schubert 	  objfile->section_offsets = (struct section_offsets *)
26085796c8dcSSimon Schubert 	    obstack_alloc (&objfile->objfile_obstack,
26095796c8dcSSimon Schubert 			   SIZEOF_N_SECTION_OFFSETS (num_offsets));
26105796c8dcSSimon Schubert 	  memcpy (objfile->section_offsets, offsets,
26115796c8dcSSimon Schubert 		  SIZEOF_N_SECTION_OFFSETS (num_offsets));
26125796c8dcSSimon Schubert 	  objfile->num_sections = num_offsets;
26135796c8dcSSimon Schubert 
26145796c8dcSSimon Schubert 	  /* What the hell is sym_new_init for, anyway?  The concept of
26155796c8dcSSimon Schubert 	     distinguishing between the main file and additional files
26165796c8dcSSimon Schubert 	     in this way seems rather dubious.  */
26175796c8dcSSimon Schubert 	  if (objfile == symfile_objfile)
26185796c8dcSSimon Schubert 	    {
26195796c8dcSSimon Schubert 	      (*objfile->sf->sym_new_init) (objfile);
26205796c8dcSSimon Schubert 	    }
26215796c8dcSSimon Schubert 
26225796c8dcSSimon Schubert 	  (*objfile->sf->sym_init) (objfile);
26235796c8dcSSimon Schubert 	  clear_complaints (&symfile_complaints, 1, 1);
2624*ef5ccd6cSJohn Marino 
2625c50c785cSJohn Marino 	  objfile->flags &= ~OBJF_PSYMTABS_READ;
2626*ef5ccd6cSJohn Marino 	  read_symbols (objfile, 0);
2627c50c785cSJohn Marino 
2628cf7f2e2dSJohn Marino 	  if (!objfile_has_symbols (objfile))
26295796c8dcSSimon Schubert 	    {
26305796c8dcSSimon Schubert 	      wrap_here ("");
26315796c8dcSSimon Schubert 	      printf_unfiltered (_("(no debugging symbols found)\n"));
26325796c8dcSSimon Schubert 	      wrap_here ("");
26335796c8dcSSimon Schubert 	    }
26345796c8dcSSimon Schubert 
26355796c8dcSSimon Schubert 	  /* We're done reading the symbol file; finish off complaints.  */
26365796c8dcSSimon Schubert 	  clear_complaints (&symfile_complaints, 0, 1);
26375796c8dcSSimon Schubert 
26385796c8dcSSimon Schubert 	  /* Getting new symbols may change our opinion about what is
26395796c8dcSSimon Schubert 	     frameless.  */
26405796c8dcSSimon Schubert 
26415796c8dcSSimon Schubert 	  reinit_frame_cache ();
26425796c8dcSSimon Schubert 
26435796c8dcSSimon Schubert 	  /* Discard cleanups as symbol reading was successful.  */
26445796c8dcSSimon Schubert 	  discard_cleanups (old_cleanups);
26455796c8dcSSimon Schubert 
26465796c8dcSSimon Schubert 	  /* If the mtime has changed between the time we set new_modtime
26475796c8dcSSimon Schubert 	     and now, we *want* this to be out of date, so don't call stat
26485796c8dcSSimon Schubert 	     again now.  */
26495796c8dcSSimon Schubert 	  objfile->mtime = new_modtime;
26505796c8dcSSimon Schubert 	  init_entry_point_info (objfile);
2651*ef5ccd6cSJohn Marino 
2652*ef5ccd6cSJohn Marino 	  VEC_safe_push (objfilep, new_objfiles, objfile);
26535796c8dcSSimon Schubert 	}
26545796c8dcSSimon Schubert     }
26555796c8dcSSimon Schubert 
2656*ef5ccd6cSJohn Marino   if (new_objfiles)
26575796c8dcSSimon Schubert     {
2658*ef5ccd6cSJohn Marino       int ix;
2659*ef5ccd6cSJohn Marino 
26605796c8dcSSimon Schubert       /* Notify objfiles that we've modified objfile sections.  */
26615796c8dcSSimon Schubert       objfiles_changed ();
26625796c8dcSSimon Schubert 
2663c50c785cSJohn Marino       clear_symtab_users (0);
2664*ef5ccd6cSJohn Marino 
2665*ef5ccd6cSJohn Marino       /* clear_objfile_data for each objfile was called before freeing it and
2666*ef5ccd6cSJohn Marino 	 observer_notify_new_objfile (NULL) has been called by
2667*ef5ccd6cSJohn Marino 	 clear_symtab_users above.  Notify the new files now.  */
2668*ef5ccd6cSJohn Marino       for (ix = 0; VEC_iterate (objfilep, new_objfiles, ix, objfile); ix++)
2669*ef5ccd6cSJohn Marino 	observer_notify_new_objfile (objfile);
2670*ef5ccd6cSJohn Marino 
26715796c8dcSSimon Schubert       /* At least one objfile has changed, so we can consider that
26725796c8dcSSimon Schubert          the executable we're debugging has changed too.  */
26735796c8dcSSimon Schubert       observer_notify_executable_changed ();
26745796c8dcSSimon Schubert     }
2675*ef5ccd6cSJohn Marino 
2676*ef5ccd6cSJohn Marino   do_cleanups (all_cleanups);
26775796c8dcSSimon Schubert }
26785796c8dcSSimon Schubert 
26795796c8dcSSimon Schubert 
26805796c8dcSSimon Schubert 
26815796c8dcSSimon Schubert typedef struct
26825796c8dcSSimon Schubert {
26835796c8dcSSimon Schubert   char *ext;
26845796c8dcSSimon Schubert   enum language lang;
26855796c8dcSSimon Schubert }
26865796c8dcSSimon Schubert filename_language;
26875796c8dcSSimon Schubert 
26885796c8dcSSimon Schubert static filename_language *filename_language_table;
26895796c8dcSSimon Schubert static int fl_table_size, fl_table_next;
26905796c8dcSSimon Schubert 
26915796c8dcSSimon Schubert static void
add_filename_language(char * ext,enum language lang)26925796c8dcSSimon Schubert add_filename_language (char *ext, enum language lang)
26935796c8dcSSimon Schubert {
26945796c8dcSSimon Schubert   if (fl_table_next >= fl_table_size)
26955796c8dcSSimon Schubert     {
26965796c8dcSSimon Schubert       fl_table_size += 10;
26975796c8dcSSimon Schubert       filename_language_table =
26985796c8dcSSimon Schubert 	xrealloc (filename_language_table,
26995796c8dcSSimon Schubert 		  fl_table_size * sizeof (*filename_language_table));
27005796c8dcSSimon Schubert     }
27015796c8dcSSimon Schubert 
27025796c8dcSSimon Schubert   filename_language_table[fl_table_next].ext = xstrdup (ext);
27035796c8dcSSimon Schubert   filename_language_table[fl_table_next].lang = lang;
27045796c8dcSSimon Schubert   fl_table_next++;
27055796c8dcSSimon Schubert }
27065796c8dcSSimon Schubert 
27075796c8dcSSimon Schubert static char *ext_args;
27085796c8dcSSimon Schubert static void
show_ext_args(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)27095796c8dcSSimon Schubert show_ext_args (struct ui_file *file, int from_tty,
27105796c8dcSSimon Schubert 	       struct cmd_list_element *c, const char *value)
27115796c8dcSSimon Schubert {
2712c50c785cSJohn Marino   fprintf_filtered (file,
2713c50c785cSJohn Marino 		    _("Mapping between filename extension "
2714c50c785cSJohn Marino 		      "and source language is \"%s\".\n"),
27155796c8dcSSimon Schubert 		    value);
27165796c8dcSSimon Schubert }
27175796c8dcSSimon Schubert 
27185796c8dcSSimon Schubert static void
set_ext_lang_command(char * args,int from_tty,struct cmd_list_element * e)27195796c8dcSSimon Schubert set_ext_lang_command (char *args, int from_tty, struct cmd_list_element *e)
27205796c8dcSSimon Schubert {
27215796c8dcSSimon Schubert   int i;
27225796c8dcSSimon Schubert   char *cp = ext_args;
27235796c8dcSSimon Schubert   enum language lang;
27245796c8dcSSimon Schubert 
27255796c8dcSSimon Schubert   /* First arg is filename extension, starting with '.'  */
27265796c8dcSSimon Schubert   if (*cp != '.')
27275796c8dcSSimon Schubert     error (_("'%s': Filename extension must begin with '.'"), ext_args);
27285796c8dcSSimon Schubert 
27295796c8dcSSimon Schubert   /* Find end of first arg.  */
27305796c8dcSSimon Schubert   while (*cp && !isspace (*cp))
27315796c8dcSSimon Schubert     cp++;
27325796c8dcSSimon Schubert 
27335796c8dcSSimon Schubert   if (*cp == '\0')
2734c50c785cSJohn Marino     error (_("'%s': two arguments required -- "
2735c50c785cSJohn Marino 	     "filename extension and language"),
27365796c8dcSSimon Schubert 	   ext_args);
27375796c8dcSSimon Schubert 
2738c50c785cSJohn Marino   /* Null-terminate first arg.  */
27395796c8dcSSimon Schubert   *cp++ = '\0';
27405796c8dcSSimon Schubert 
27415796c8dcSSimon Schubert   /* Find beginning of second arg, which should be a source language.  */
2742*ef5ccd6cSJohn Marino   cp = skip_spaces (cp);
27435796c8dcSSimon Schubert 
27445796c8dcSSimon Schubert   if (*cp == '\0')
2745c50c785cSJohn Marino     error (_("'%s': two arguments required -- "
2746c50c785cSJohn Marino 	     "filename extension and language"),
27475796c8dcSSimon Schubert 	   ext_args);
27485796c8dcSSimon Schubert 
27495796c8dcSSimon Schubert   /* Lookup the language from among those we know.  */
27505796c8dcSSimon Schubert   lang = language_enum (cp);
27515796c8dcSSimon Schubert 
27525796c8dcSSimon Schubert   /* Now lookup the filename extension: do we already know it?  */
27535796c8dcSSimon Schubert   for (i = 0; i < fl_table_next; i++)
27545796c8dcSSimon Schubert     if (0 == strcmp (ext_args, filename_language_table[i].ext))
27555796c8dcSSimon Schubert       break;
27565796c8dcSSimon Schubert 
27575796c8dcSSimon Schubert   if (i >= fl_table_next)
27585796c8dcSSimon Schubert     {
2759c50c785cSJohn Marino       /* New file extension.  */
27605796c8dcSSimon Schubert       add_filename_language (ext_args, lang);
27615796c8dcSSimon Schubert     }
27625796c8dcSSimon Schubert   else
27635796c8dcSSimon Schubert     {
2764c50c785cSJohn Marino       /* Redefining a previously known filename extension.  */
27655796c8dcSSimon Schubert 
27665796c8dcSSimon Schubert       /* if (from_tty) */
27675796c8dcSSimon Schubert       /*   query ("Really make files of type %s '%s'?", */
27685796c8dcSSimon Schubert       /*          ext_args, language_str (lang));           */
27695796c8dcSSimon Schubert 
27705796c8dcSSimon Schubert       xfree (filename_language_table[i].ext);
27715796c8dcSSimon Schubert       filename_language_table[i].ext = xstrdup (ext_args);
27725796c8dcSSimon Schubert       filename_language_table[i].lang = lang;
27735796c8dcSSimon Schubert     }
27745796c8dcSSimon Schubert }
27755796c8dcSSimon Schubert 
27765796c8dcSSimon Schubert static void
info_ext_lang_command(char * args,int from_tty)27775796c8dcSSimon Schubert info_ext_lang_command (char *args, int from_tty)
27785796c8dcSSimon Schubert {
27795796c8dcSSimon Schubert   int i;
27805796c8dcSSimon Schubert 
27815796c8dcSSimon Schubert   printf_filtered (_("Filename extensions and the languages they represent:"));
27825796c8dcSSimon Schubert   printf_filtered ("\n\n");
27835796c8dcSSimon Schubert   for (i = 0; i < fl_table_next; i++)
27845796c8dcSSimon Schubert     printf_filtered ("\t%s\t- %s\n",
27855796c8dcSSimon Schubert 		     filename_language_table[i].ext,
27865796c8dcSSimon Schubert 		     language_str (filename_language_table[i].lang));
27875796c8dcSSimon Schubert }
27885796c8dcSSimon Schubert 
27895796c8dcSSimon Schubert static void
init_filename_language_table(void)27905796c8dcSSimon Schubert init_filename_language_table (void)
27915796c8dcSSimon Schubert {
2792c50c785cSJohn Marino   if (fl_table_size == 0)	/* Protect against repetition.  */
27935796c8dcSSimon Schubert     {
27945796c8dcSSimon Schubert       fl_table_size = 20;
27955796c8dcSSimon Schubert       fl_table_next = 0;
27965796c8dcSSimon Schubert       filename_language_table =
27975796c8dcSSimon Schubert 	xmalloc (fl_table_size * sizeof (*filename_language_table));
27985796c8dcSSimon Schubert       add_filename_language (".c", language_c);
2799cf7f2e2dSJohn Marino       add_filename_language (".d", language_d);
28005796c8dcSSimon Schubert       add_filename_language (".C", language_cplus);
28015796c8dcSSimon Schubert       add_filename_language (".cc", language_cplus);
28025796c8dcSSimon Schubert       add_filename_language (".cp", language_cplus);
28035796c8dcSSimon Schubert       add_filename_language (".cpp", language_cplus);
28045796c8dcSSimon Schubert       add_filename_language (".cxx", language_cplus);
28055796c8dcSSimon Schubert       add_filename_language (".c++", language_cplus);
28065796c8dcSSimon Schubert       add_filename_language (".java", language_java);
28075796c8dcSSimon Schubert       add_filename_language (".class", language_java);
28085796c8dcSSimon Schubert       add_filename_language (".m", language_objc);
28095796c8dcSSimon Schubert       add_filename_language (".f", language_fortran);
28105796c8dcSSimon Schubert       add_filename_language (".F", language_fortran);
2811cf7f2e2dSJohn Marino       add_filename_language (".for", language_fortran);
2812cf7f2e2dSJohn Marino       add_filename_language (".FOR", language_fortran);
2813cf7f2e2dSJohn Marino       add_filename_language (".ftn", language_fortran);
2814cf7f2e2dSJohn Marino       add_filename_language (".FTN", language_fortran);
2815cf7f2e2dSJohn Marino       add_filename_language (".fpp", language_fortran);
2816cf7f2e2dSJohn Marino       add_filename_language (".FPP", language_fortran);
2817cf7f2e2dSJohn Marino       add_filename_language (".f90", language_fortran);
2818cf7f2e2dSJohn Marino       add_filename_language (".F90", language_fortran);
2819cf7f2e2dSJohn Marino       add_filename_language (".f95", language_fortran);
2820cf7f2e2dSJohn Marino       add_filename_language (".F95", language_fortran);
2821cf7f2e2dSJohn Marino       add_filename_language (".f03", language_fortran);
2822cf7f2e2dSJohn Marino       add_filename_language (".F03", language_fortran);
2823cf7f2e2dSJohn Marino       add_filename_language (".f08", language_fortran);
2824cf7f2e2dSJohn Marino       add_filename_language (".F08", language_fortran);
28255796c8dcSSimon Schubert       add_filename_language (".s", language_asm);
28265796c8dcSSimon Schubert       add_filename_language (".sx", language_asm);
28275796c8dcSSimon Schubert       add_filename_language (".S", language_asm);
28285796c8dcSSimon Schubert       add_filename_language (".pas", language_pascal);
28295796c8dcSSimon Schubert       add_filename_language (".p", language_pascal);
28305796c8dcSSimon Schubert       add_filename_language (".pp", language_pascal);
28315796c8dcSSimon Schubert       add_filename_language (".adb", language_ada);
28325796c8dcSSimon Schubert       add_filename_language (".ads", language_ada);
28335796c8dcSSimon Schubert       add_filename_language (".a", language_ada);
28345796c8dcSSimon Schubert       add_filename_language (".ada", language_ada);
2835cf7f2e2dSJohn Marino       add_filename_language (".dg", language_ada);
28365796c8dcSSimon Schubert     }
28375796c8dcSSimon Schubert }
28385796c8dcSSimon Schubert 
28395796c8dcSSimon Schubert enum language
deduce_language_from_filename(const char * filename)2840c50c785cSJohn Marino deduce_language_from_filename (const char *filename)
28415796c8dcSSimon Schubert {
28425796c8dcSSimon Schubert   int i;
28435796c8dcSSimon Schubert   char *cp;
28445796c8dcSSimon Schubert 
28455796c8dcSSimon Schubert   if (filename != NULL)
28465796c8dcSSimon Schubert     if ((cp = strrchr (filename, '.')) != NULL)
28475796c8dcSSimon Schubert       for (i = 0; i < fl_table_next; i++)
28485796c8dcSSimon Schubert 	if (strcmp (cp, filename_language_table[i].ext) == 0)
28495796c8dcSSimon Schubert 	  return filename_language_table[i].lang;
28505796c8dcSSimon Schubert 
28515796c8dcSSimon Schubert   return language_unknown;
28525796c8dcSSimon Schubert }
28535796c8dcSSimon Schubert 
28545796c8dcSSimon Schubert /* allocate_symtab:
28555796c8dcSSimon Schubert 
28565796c8dcSSimon Schubert    Allocate and partly initialize a new symbol table.  Return a pointer
28575796c8dcSSimon Schubert    to it.  error() if no space.
28585796c8dcSSimon Schubert 
28595796c8dcSSimon Schubert    Caller must set these fields:
28605796c8dcSSimon Schubert    LINETABLE(symtab)
28615796c8dcSSimon Schubert    symtab->blockvector
28625796c8dcSSimon Schubert    symtab->dirname
28635796c8dcSSimon Schubert    symtab->free_code
28645796c8dcSSimon Schubert    symtab->free_ptr
28655796c8dcSSimon Schubert  */
28665796c8dcSSimon Schubert 
28675796c8dcSSimon Schubert struct symtab *
allocate_symtab(const char * filename,struct objfile * objfile)2868c50c785cSJohn Marino allocate_symtab (const char *filename, struct objfile *objfile)
28695796c8dcSSimon Schubert {
28705796c8dcSSimon Schubert   struct symtab *symtab;
28715796c8dcSSimon Schubert 
28725796c8dcSSimon Schubert   symtab = (struct symtab *)
28735796c8dcSSimon Schubert     obstack_alloc (&objfile->objfile_obstack, sizeof (struct symtab));
28745796c8dcSSimon Schubert   memset (symtab, 0, sizeof (*symtab));
2875cf7f2e2dSJohn Marino   symtab->filename = (char *) bcache (filename, strlen (filename) + 1,
2876*ef5ccd6cSJohn Marino 				      objfile->per_bfd->filename_cache);
28775796c8dcSSimon Schubert   symtab->fullname = NULL;
28785796c8dcSSimon Schubert   symtab->language = deduce_language_from_filename (filename);
2879cf7f2e2dSJohn Marino   symtab->debugformat = "unknown";
28805796c8dcSSimon Schubert 
2881c50c785cSJohn Marino   /* Hook it to the objfile it comes from.  */
28825796c8dcSSimon Schubert 
28835796c8dcSSimon Schubert   symtab->objfile = objfile;
28845796c8dcSSimon Schubert   symtab->next = objfile->symtabs;
28855796c8dcSSimon Schubert   objfile->symtabs = symtab;
28865796c8dcSSimon Schubert 
2887*ef5ccd6cSJohn Marino   if (symtab_create_debug)
2888*ef5ccd6cSJohn Marino     {
2889*ef5ccd6cSJohn Marino       /* Be a bit clever with debugging messages, and don't print objfile
2890*ef5ccd6cSJohn Marino 	 every time, only when it changes.  */
2891*ef5ccd6cSJohn Marino       static char *last_objfile_name = NULL;
2892*ef5ccd6cSJohn Marino 
2893*ef5ccd6cSJohn Marino       if (last_objfile_name == NULL
2894*ef5ccd6cSJohn Marino 	  || strcmp (last_objfile_name, objfile->name) != 0)
2895*ef5ccd6cSJohn Marino 	{
2896*ef5ccd6cSJohn Marino 	  xfree (last_objfile_name);
2897*ef5ccd6cSJohn Marino 	  last_objfile_name = xstrdup (objfile->name);
2898*ef5ccd6cSJohn Marino 	  fprintf_unfiltered (gdb_stdlog,
2899*ef5ccd6cSJohn Marino 			      "Creating one or more symtabs for objfile %s ...\n",
2900*ef5ccd6cSJohn Marino 			      last_objfile_name);
2901*ef5ccd6cSJohn Marino 	}
2902*ef5ccd6cSJohn Marino       fprintf_unfiltered (gdb_stdlog,
2903*ef5ccd6cSJohn Marino 			  "Created symtab %s for module %s.\n",
2904*ef5ccd6cSJohn Marino 			  host_address_to_string (symtab), filename);
2905*ef5ccd6cSJohn Marino     }
2906*ef5ccd6cSJohn Marino 
29075796c8dcSSimon Schubert   return (symtab);
29085796c8dcSSimon Schubert }
29095796c8dcSSimon Schubert 
29105796c8dcSSimon Schubert 
29115796c8dcSSimon Schubert /* Reset all data structures in gdb which may contain references to symbol
2912c50c785cSJohn Marino    table data.  ADD_FLAGS is a bitmask of enum symfile_add_flags.  */
29135796c8dcSSimon Schubert 
29145796c8dcSSimon Schubert void
clear_symtab_users(int add_flags)2915c50c785cSJohn Marino clear_symtab_users (int add_flags)
29165796c8dcSSimon Schubert {
29175796c8dcSSimon Schubert   /* Someday, we should do better than this, by only blowing away
29185796c8dcSSimon Schubert      the things that really need to be blown.  */
29195796c8dcSSimon Schubert 
29205796c8dcSSimon Schubert   /* Clear the "current" symtab first, because it is no longer valid.
29215796c8dcSSimon Schubert      breakpoint_re_set may try to access the current symtab.  */
29225796c8dcSSimon Schubert   clear_current_source_symtab_and_line ();
29235796c8dcSSimon Schubert 
29245796c8dcSSimon Schubert   clear_displays ();
2925c50c785cSJohn Marino   if ((add_flags & SYMFILE_DEFER_BP_RESET) == 0)
29265796c8dcSSimon Schubert     breakpoint_re_set ();
2927a45ae5f8SJohn Marino   clear_last_displayed_sal ();
29285796c8dcSSimon Schubert   clear_pc_function_cache ();
29295796c8dcSSimon Schubert   observer_notify_new_objfile (NULL);
29305796c8dcSSimon Schubert 
29315796c8dcSSimon Schubert   /* Clear globals which might have pointed into a removed objfile.
29325796c8dcSSimon Schubert      FIXME: It's not clear which of these are supposed to persist
29335796c8dcSSimon Schubert      between expressions and which ought to be reset each time.  */
29345796c8dcSSimon Schubert   expression_context_block = NULL;
29355796c8dcSSimon Schubert   innermost_block = NULL;
29365796c8dcSSimon Schubert 
29375796c8dcSSimon Schubert   /* Varobj may refer to old symbols, perform a cleanup.  */
29385796c8dcSSimon Schubert   varobj_invalidate ();
29395796c8dcSSimon Schubert 
29405796c8dcSSimon Schubert }
29415796c8dcSSimon Schubert 
29425796c8dcSSimon Schubert static void
clear_symtab_users_cleanup(void * ignore)29435796c8dcSSimon Schubert clear_symtab_users_cleanup (void *ignore)
29445796c8dcSSimon Schubert {
2945c50c785cSJohn Marino   clear_symtab_users (0);
29465796c8dcSSimon Schubert }
29475796c8dcSSimon Schubert 
29485796c8dcSSimon Schubert /* OVERLAYS:
29495796c8dcSSimon Schubert    The following code implements an abstraction for debugging overlay sections.
29505796c8dcSSimon Schubert 
29515796c8dcSSimon Schubert    The target model is as follows:
29525796c8dcSSimon Schubert    1) The gnu linker will permit multiple sections to be mapped into the
29535796c8dcSSimon Schubert    same VMA, each with its own unique LMA (or load address).
29545796c8dcSSimon Schubert    2) It is assumed that some runtime mechanism exists for mapping the
29555796c8dcSSimon Schubert    sections, one by one, from the load address into the VMA address.
29565796c8dcSSimon Schubert    3) This code provides a mechanism for gdb to keep track of which
29575796c8dcSSimon Schubert    sections should be considered to be mapped from the VMA to the LMA.
29585796c8dcSSimon Schubert    This information is used for symbol lookup, and memory read/write.
29595796c8dcSSimon Schubert    For instance, if a section has been mapped then its contents
29605796c8dcSSimon Schubert    should be read from the VMA, otherwise from the LMA.
29615796c8dcSSimon Schubert 
29625796c8dcSSimon Schubert    Two levels of debugger support for overlays are available.  One is
29635796c8dcSSimon Schubert    "manual", in which the debugger relies on the user to tell it which
29645796c8dcSSimon Schubert    overlays are currently mapped.  This level of support is
29655796c8dcSSimon Schubert    implemented entirely in the core debugger, and the information about
29665796c8dcSSimon Schubert    whether a section is mapped is kept in the objfile->obj_section table.
29675796c8dcSSimon Schubert 
29685796c8dcSSimon Schubert    The second level of support is "automatic", and is only available if
29695796c8dcSSimon Schubert    the target-specific code provides functionality to read the target's
29705796c8dcSSimon Schubert    overlay mapping table, and translate its contents for the debugger
29715796c8dcSSimon Schubert    (by updating the mapped state information in the obj_section tables).
29725796c8dcSSimon Schubert 
29735796c8dcSSimon Schubert    The interface is as follows:
29745796c8dcSSimon Schubert    User commands:
29755796c8dcSSimon Schubert    overlay map <name>   -- tell gdb to consider this section mapped
29765796c8dcSSimon Schubert    overlay unmap <name> -- tell gdb to consider this section unmapped
29775796c8dcSSimon Schubert    overlay list         -- list the sections that GDB thinks are mapped
29785796c8dcSSimon Schubert    overlay read-target  -- get the target's state of what's mapped
29795796c8dcSSimon Schubert    overlay off/manual/auto -- set overlay debugging state
29805796c8dcSSimon Schubert    Functional interface:
29815796c8dcSSimon Schubert    find_pc_mapped_section(pc):    if the pc is in the range of a mapped
29825796c8dcSSimon Schubert    section, return that section.
29835796c8dcSSimon Schubert    find_pc_overlay(pc):       find any overlay section that contains
29845796c8dcSSimon Schubert    the pc, either in its VMA or its LMA
29855796c8dcSSimon Schubert    section_is_mapped(sect):       true if overlay is marked as mapped
29865796c8dcSSimon Schubert    section_is_overlay(sect):      true if section's VMA != LMA
29875796c8dcSSimon Schubert    pc_in_mapped_range(pc,sec):    true if pc belongs to section's VMA
29885796c8dcSSimon Schubert    pc_in_unmapped_range(...):     true if pc belongs to section's LMA
29895796c8dcSSimon Schubert    sections_overlap(sec1, sec2):  true if mapped sec1 and sec2 ranges overlap
29905796c8dcSSimon Schubert    overlay_mapped_address(...):   map an address from section's LMA to VMA
29915796c8dcSSimon Schubert    overlay_unmapped_address(...): map an address from section's VMA to LMA
29925796c8dcSSimon Schubert    symbol_overlayed_address(...): Return a "current" address for symbol:
29935796c8dcSSimon Schubert    either in VMA or LMA depending on whether
2994c50c785cSJohn Marino    the symbol's section is currently mapped.  */
29955796c8dcSSimon Schubert 
29965796c8dcSSimon Schubert /* Overlay debugging state: */
29975796c8dcSSimon Schubert 
29985796c8dcSSimon Schubert enum overlay_debugging_state overlay_debugging = ovly_off;
2999c50c785cSJohn Marino int overlay_cache_invalid = 0;	/* True if need to refresh mapped state.  */
30005796c8dcSSimon Schubert 
30015796c8dcSSimon Schubert /* Function: section_is_overlay (SECTION)
30025796c8dcSSimon Schubert    Returns true if SECTION has VMA not equal to LMA, ie.
30035796c8dcSSimon Schubert    SECTION is loaded at an address different from where it will "run".  */
30045796c8dcSSimon Schubert 
30055796c8dcSSimon Schubert int
section_is_overlay(struct obj_section * section)30065796c8dcSSimon Schubert section_is_overlay (struct obj_section *section)
30075796c8dcSSimon Schubert {
30085796c8dcSSimon Schubert   if (overlay_debugging && section)
30095796c8dcSSimon Schubert     {
30105796c8dcSSimon Schubert       bfd *abfd = section->objfile->obfd;
30115796c8dcSSimon Schubert       asection *bfd_section = section->the_bfd_section;
30125796c8dcSSimon Schubert 
30135796c8dcSSimon Schubert       if (bfd_section_lma (abfd, bfd_section) != 0
30145796c8dcSSimon Schubert 	  && bfd_section_lma (abfd, bfd_section)
30155796c8dcSSimon Schubert 	     != bfd_section_vma (abfd, bfd_section))
30165796c8dcSSimon Schubert 	return 1;
30175796c8dcSSimon Schubert     }
30185796c8dcSSimon Schubert 
30195796c8dcSSimon Schubert   return 0;
30205796c8dcSSimon Schubert }
30215796c8dcSSimon Schubert 
30225796c8dcSSimon Schubert /* Function: overlay_invalidate_all (void)
30235796c8dcSSimon Schubert    Invalidate the mapped state of all overlay sections (mark it as stale).  */
30245796c8dcSSimon Schubert 
30255796c8dcSSimon Schubert static void
overlay_invalidate_all(void)30265796c8dcSSimon Schubert overlay_invalidate_all (void)
30275796c8dcSSimon Schubert {
30285796c8dcSSimon Schubert   struct objfile *objfile;
30295796c8dcSSimon Schubert   struct obj_section *sect;
30305796c8dcSSimon Schubert 
30315796c8dcSSimon Schubert   ALL_OBJSECTIONS (objfile, sect)
30325796c8dcSSimon Schubert     if (section_is_overlay (sect))
30335796c8dcSSimon Schubert       sect->ovly_mapped = -1;
30345796c8dcSSimon Schubert }
30355796c8dcSSimon Schubert 
30365796c8dcSSimon Schubert /* Function: section_is_mapped (SECTION)
30375796c8dcSSimon Schubert    Returns true if section is an overlay, and is currently mapped.
30385796c8dcSSimon Schubert 
30395796c8dcSSimon Schubert    Access to the ovly_mapped flag is restricted to this function, so
30405796c8dcSSimon Schubert    that we can do automatic update.  If the global flag
30415796c8dcSSimon Schubert    OVERLAY_CACHE_INVALID is set (by wait_for_inferior), then call
30425796c8dcSSimon Schubert    overlay_invalidate_all.  If the mapped state of the particular
30435796c8dcSSimon Schubert    section is stale, then call TARGET_OVERLAY_UPDATE to refresh it.  */
30445796c8dcSSimon Schubert 
30455796c8dcSSimon Schubert int
section_is_mapped(struct obj_section * osect)30465796c8dcSSimon Schubert section_is_mapped (struct obj_section *osect)
30475796c8dcSSimon Schubert {
30485796c8dcSSimon Schubert   struct gdbarch *gdbarch;
30495796c8dcSSimon Schubert 
30505796c8dcSSimon Schubert   if (osect == 0 || !section_is_overlay (osect))
30515796c8dcSSimon Schubert     return 0;
30525796c8dcSSimon Schubert 
30535796c8dcSSimon Schubert   switch (overlay_debugging)
30545796c8dcSSimon Schubert     {
30555796c8dcSSimon Schubert     default:
30565796c8dcSSimon Schubert     case ovly_off:
30575796c8dcSSimon Schubert       return 0;			/* overlay debugging off */
30585796c8dcSSimon Schubert     case ovly_auto:		/* overlay debugging automatic */
30595796c8dcSSimon Schubert       /* Unles there is a gdbarch_overlay_update function,
3060c50c785cSJohn Marino          there's really nothing useful to do here (can't really go auto).  */
30615796c8dcSSimon Schubert       gdbarch = get_objfile_arch (osect->objfile);
30625796c8dcSSimon Schubert       if (gdbarch_overlay_update_p (gdbarch))
30635796c8dcSSimon Schubert 	{
30645796c8dcSSimon Schubert 	  if (overlay_cache_invalid)
30655796c8dcSSimon Schubert 	    {
30665796c8dcSSimon Schubert 	      overlay_invalidate_all ();
30675796c8dcSSimon Schubert 	      overlay_cache_invalid = 0;
30685796c8dcSSimon Schubert 	    }
30695796c8dcSSimon Schubert 	  if (osect->ovly_mapped == -1)
30705796c8dcSSimon Schubert 	    gdbarch_overlay_update (gdbarch, osect);
30715796c8dcSSimon Schubert 	}
30725796c8dcSSimon Schubert       /* fall thru to manual case */
30735796c8dcSSimon Schubert     case ovly_on:		/* overlay debugging manual */
30745796c8dcSSimon Schubert       return osect->ovly_mapped == 1;
30755796c8dcSSimon Schubert     }
30765796c8dcSSimon Schubert }
30775796c8dcSSimon Schubert 
30785796c8dcSSimon Schubert /* Function: pc_in_unmapped_range
30795796c8dcSSimon Schubert    If PC falls into the lma range of SECTION, return true, else false.  */
30805796c8dcSSimon Schubert 
30815796c8dcSSimon Schubert CORE_ADDR
pc_in_unmapped_range(CORE_ADDR pc,struct obj_section * section)30825796c8dcSSimon Schubert pc_in_unmapped_range (CORE_ADDR pc, struct obj_section *section)
30835796c8dcSSimon Schubert {
30845796c8dcSSimon Schubert   if (section_is_overlay (section))
30855796c8dcSSimon Schubert     {
30865796c8dcSSimon Schubert       bfd *abfd = section->objfile->obfd;
30875796c8dcSSimon Schubert       asection *bfd_section = section->the_bfd_section;
30885796c8dcSSimon Schubert 
30895796c8dcSSimon Schubert       /* We assume the LMA is relocated by the same offset as the VMA.  */
30905796c8dcSSimon Schubert       bfd_vma size = bfd_get_section_size (bfd_section);
30915796c8dcSSimon Schubert       CORE_ADDR offset = obj_section_offset (section);
30925796c8dcSSimon Schubert 
30935796c8dcSSimon Schubert       if (bfd_get_section_lma (abfd, bfd_section) + offset <= pc
30945796c8dcSSimon Schubert 	  && pc < bfd_get_section_lma (abfd, bfd_section) + offset + size)
30955796c8dcSSimon Schubert 	return 1;
30965796c8dcSSimon Schubert     }
30975796c8dcSSimon Schubert 
30985796c8dcSSimon Schubert   return 0;
30995796c8dcSSimon Schubert }
31005796c8dcSSimon Schubert 
31015796c8dcSSimon Schubert /* Function: pc_in_mapped_range
31025796c8dcSSimon Schubert    If PC falls into the vma range of SECTION, return true, else false.  */
31035796c8dcSSimon Schubert 
31045796c8dcSSimon Schubert CORE_ADDR
pc_in_mapped_range(CORE_ADDR pc,struct obj_section * section)31055796c8dcSSimon Schubert pc_in_mapped_range (CORE_ADDR pc, struct obj_section *section)
31065796c8dcSSimon Schubert {
31075796c8dcSSimon Schubert   if (section_is_overlay (section))
31085796c8dcSSimon Schubert     {
31095796c8dcSSimon Schubert       if (obj_section_addr (section) <= pc
31105796c8dcSSimon Schubert 	  && pc < obj_section_endaddr (section))
31115796c8dcSSimon Schubert 	return 1;
31125796c8dcSSimon Schubert     }
31135796c8dcSSimon Schubert 
31145796c8dcSSimon Schubert   return 0;
31155796c8dcSSimon Schubert }
31165796c8dcSSimon Schubert 
31175796c8dcSSimon Schubert 
31185796c8dcSSimon Schubert /* Return true if the mapped ranges of sections A and B overlap, false
31195796c8dcSSimon Schubert    otherwise.  */
31205796c8dcSSimon Schubert static int
sections_overlap(struct obj_section * a,struct obj_section * b)31215796c8dcSSimon Schubert sections_overlap (struct obj_section *a, struct obj_section *b)
31225796c8dcSSimon Schubert {
31235796c8dcSSimon Schubert   CORE_ADDR a_start = obj_section_addr (a);
31245796c8dcSSimon Schubert   CORE_ADDR a_end = obj_section_endaddr (a);
31255796c8dcSSimon Schubert   CORE_ADDR b_start = obj_section_addr (b);
31265796c8dcSSimon Schubert   CORE_ADDR b_end = obj_section_endaddr (b);
31275796c8dcSSimon Schubert 
31285796c8dcSSimon Schubert   return (a_start < b_end && b_start < a_end);
31295796c8dcSSimon Schubert }
31305796c8dcSSimon Schubert 
31315796c8dcSSimon Schubert /* Function: overlay_unmapped_address (PC, SECTION)
31325796c8dcSSimon Schubert    Returns the address corresponding to PC in the unmapped (load) range.
31335796c8dcSSimon Schubert    May be the same as PC.  */
31345796c8dcSSimon Schubert 
31355796c8dcSSimon Schubert CORE_ADDR
overlay_unmapped_address(CORE_ADDR pc,struct obj_section * section)31365796c8dcSSimon Schubert overlay_unmapped_address (CORE_ADDR pc, struct obj_section *section)
31375796c8dcSSimon Schubert {
31385796c8dcSSimon Schubert   if (section_is_overlay (section) && pc_in_mapped_range (pc, section))
31395796c8dcSSimon Schubert     {
31405796c8dcSSimon Schubert       bfd *abfd = section->objfile->obfd;
31415796c8dcSSimon Schubert       asection *bfd_section = section->the_bfd_section;
31425796c8dcSSimon Schubert 
31435796c8dcSSimon Schubert       return pc + bfd_section_lma (abfd, bfd_section)
31445796c8dcSSimon Schubert 		- bfd_section_vma (abfd, bfd_section);
31455796c8dcSSimon Schubert     }
31465796c8dcSSimon Schubert 
31475796c8dcSSimon Schubert   return pc;
31485796c8dcSSimon Schubert }
31495796c8dcSSimon Schubert 
31505796c8dcSSimon Schubert /* Function: overlay_mapped_address (PC, SECTION)
31515796c8dcSSimon Schubert    Returns the address corresponding to PC in the mapped (runtime) range.
31525796c8dcSSimon Schubert    May be the same as PC.  */
31535796c8dcSSimon Schubert 
31545796c8dcSSimon Schubert CORE_ADDR
overlay_mapped_address(CORE_ADDR pc,struct obj_section * section)31555796c8dcSSimon Schubert overlay_mapped_address (CORE_ADDR pc, struct obj_section *section)
31565796c8dcSSimon Schubert {
31575796c8dcSSimon Schubert   if (section_is_overlay (section) && pc_in_unmapped_range (pc, section))
31585796c8dcSSimon Schubert     {
31595796c8dcSSimon Schubert       bfd *abfd = section->objfile->obfd;
31605796c8dcSSimon Schubert       asection *bfd_section = section->the_bfd_section;
31615796c8dcSSimon Schubert 
31625796c8dcSSimon Schubert       return pc + bfd_section_vma (abfd, bfd_section)
31635796c8dcSSimon Schubert 		- bfd_section_lma (abfd, bfd_section);
31645796c8dcSSimon Schubert     }
31655796c8dcSSimon Schubert 
31665796c8dcSSimon Schubert   return pc;
31675796c8dcSSimon Schubert }
31685796c8dcSSimon Schubert 
31695796c8dcSSimon Schubert 
31705796c8dcSSimon Schubert /* Function: symbol_overlayed_address
31715796c8dcSSimon Schubert    Return one of two addresses (relative to the VMA or to the LMA),
31725796c8dcSSimon Schubert    depending on whether the section is mapped or not.  */
31735796c8dcSSimon Schubert 
31745796c8dcSSimon Schubert CORE_ADDR
symbol_overlayed_address(CORE_ADDR address,struct obj_section * section)31755796c8dcSSimon Schubert symbol_overlayed_address (CORE_ADDR address, struct obj_section *section)
31765796c8dcSSimon Schubert {
31775796c8dcSSimon Schubert   if (overlay_debugging)
31785796c8dcSSimon Schubert     {
31795796c8dcSSimon Schubert       /* If the symbol has no section, just return its regular address.  */
31805796c8dcSSimon Schubert       if (section == 0)
31815796c8dcSSimon Schubert 	return address;
3182c50c785cSJohn Marino       /* If the symbol's section is not an overlay, just return its
3183c50c785cSJohn Marino 	 address.  */
31845796c8dcSSimon Schubert       if (!section_is_overlay (section))
31855796c8dcSSimon Schubert 	return address;
3186c50c785cSJohn Marino       /* If the symbol's section is mapped, just return its address.  */
31875796c8dcSSimon Schubert       if (section_is_mapped (section))
31885796c8dcSSimon Schubert 	return address;
31895796c8dcSSimon Schubert       /*
31905796c8dcSSimon Schubert        * HOWEVER: if the symbol is in an overlay section which is NOT mapped,
31915796c8dcSSimon Schubert        * then return its LOADED address rather than its vma address!!
31925796c8dcSSimon Schubert        */
31935796c8dcSSimon Schubert       return overlay_unmapped_address (address, section);
31945796c8dcSSimon Schubert     }
31955796c8dcSSimon Schubert   return address;
31965796c8dcSSimon Schubert }
31975796c8dcSSimon Schubert 
31985796c8dcSSimon Schubert /* Function: find_pc_overlay (PC)
31995796c8dcSSimon Schubert    Return the best-match overlay section for PC:
32005796c8dcSSimon Schubert    If PC matches a mapped overlay section's VMA, return that section.
32015796c8dcSSimon Schubert    Else if PC matches an unmapped section's VMA, return that section.
32025796c8dcSSimon Schubert    Else if PC matches an unmapped section's LMA, return that section.  */
32035796c8dcSSimon Schubert 
32045796c8dcSSimon Schubert struct obj_section *
find_pc_overlay(CORE_ADDR pc)32055796c8dcSSimon Schubert find_pc_overlay (CORE_ADDR pc)
32065796c8dcSSimon Schubert {
32075796c8dcSSimon Schubert   struct objfile *objfile;
32085796c8dcSSimon Schubert   struct obj_section *osect, *best_match = NULL;
32095796c8dcSSimon Schubert 
32105796c8dcSSimon Schubert   if (overlay_debugging)
32115796c8dcSSimon Schubert     ALL_OBJSECTIONS (objfile, osect)
32125796c8dcSSimon Schubert       if (section_is_overlay (osect))
32135796c8dcSSimon Schubert       {
32145796c8dcSSimon Schubert 	if (pc_in_mapped_range (pc, osect))
32155796c8dcSSimon Schubert 	  {
32165796c8dcSSimon Schubert 	    if (section_is_mapped (osect))
32175796c8dcSSimon Schubert 	      return osect;
32185796c8dcSSimon Schubert 	    else
32195796c8dcSSimon Schubert 	      best_match = osect;
32205796c8dcSSimon Schubert 	  }
32215796c8dcSSimon Schubert 	else if (pc_in_unmapped_range (pc, osect))
32225796c8dcSSimon Schubert 	  best_match = osect;
32235796c8dcSSimon Schubert       }
32245796c8dcSSimon Schubert   return best_match;
32255796c8dcSSimon Schubert }
32265796c8dcSSimon Schubert 
32275796c8dcSSimon Schubert /* Function: find_pc_mapped_section (PC)
32285796c8dcSSimon Schubert    If PC falls into the VMA address range of an overlay section that is
32295796c8dcSSimon Schubert    currently marked as MAPPED, return that section.  Else return NULL.  */
32305796c8dcSSimon Schubert 
32315796c8dcSSimon Schubert struct obj_section *
find_pc_mapped_section(CORE_ADDR pc)32325796c8dcSSimon Schubert find_pc_mapped_section (CORE_ADDR pc)
32335796c8dcSSimon Schubert {
32345796c8dcSSimon Schubert   struct objfile *objfile;
32355796c8dcSSimon Schubert   struct obj_section *osect;
32365796c8dcSSimon Schubert 
32375796c8dcSSimon Schubert   if (overlay_debugging)
32385796c8dcSSimon Schubert     ALL_OBJSECTIONS (objfile, osect)
32395796c8dcSSimon Schubert       if (pc_in_mapped_range (pc, osect) && section_is_mapped (osect))
32405796c8dcSSimon Schubert 	return osect;
32415796c8dcSSimon Schubert 
32425796c8dcSSimon Schubert   return NULL;
32435796c8dcSSimon Schubert }
32445796c8dcSSimon Schubert 
32455796c8dcSSimon Schubert /* Function: list_overlays_command
3246c50c785cSJohn Marino    Print a list of mapped sections and their PC ranges.  */
32475796c8dcSSimon Schubert 
3248*ef5ccd6cSJohn Marino static void
list_overlays_command(char * args,int from_tty)32495796c8dcSSimon Schubert list_overlays_command (char *args, int from_tty)
32505796c8dcSSimon Schubert {
32515796c8dcSSimon Schubert   int nmapped = 0;
32525796c8dcSSimon Schubert   struct objfile *objfile;
32535796c8dcSSimon Schubert   struct obj_section *osect;
32545796c8dcSSimon Schubert 
32555796c8dcSSimon Schubert   if (overlay_debugging)
32565796c8dcSSimon Schubert     ALL_OBJSECTIONS (objfile, osect)
32575796c8dcSSimon Schubert       if (section_is_mapped (osect))
32585796c8dcSSimon Schubert       {
32595796c8dcSSimon Schubert 	struct gdbarch *gdbarch = get_objfile_arch (objfile);
32605796c8dcSSimon Schubert 	const char *name;
32615796c8dcSSimon Schubert 	bfd_vma lma, vma;
32625796c8dcSSimon Schubert 	int size;
32635796c8dcSSimon Schubert 
32645796c8dcSSimon Schubert 	vma = bfd_section_vma (objfile->obfd, osect->the_bfd_section);
32655796c8dcSSimon Schubert 	lma = bfd_section_lma (objfile->obfd, osect->the_bfd_section);
32665796c8dcSSimon Schubert 	size = bfd_get_section_size (osect->the_bfd_section);
32675796c8dcSSimon Schubert 	name = bfd_section_name (objfile->obfd, osect->the_bfd_section);
32685796c8dcSSimon Schubert 
32695796c8dcSSimon Schubert 	printf_filtered ("Section %s, loaded at ", name);
32705796c8dcSSimon Schubert 	fputs_filtered (paddress (gdbarch, lma), gdb_stdout);
32715796c8dcSSimon Schubert 	puts_filtered (" - ");
32725796c8dcSSimon Schubert 	fputs_filtered (paddress (gdbarch, lma + size), gdb_stdout);
32735796c8dcSSimon Schubert 	printf_filtered (", mapped at ");
32745796c8dcSSimon Schubert 	fputs_filtered (paddress (gdbarch, vma), gdb_stdout);
32755796c8dcSSimon Schubert 	puts_filtered (" - ");
32765796c8dcSSimon Schubert 	fputs_filtered (paddress (gdbarch, vma + size), gdb_stdout);
32775796c8dcSSimon Schubert 	puts_filtered ("\n");
32785796c8dcSSimon Schubert 
32795796c8dcSSimon Schubert 	nmapped++;
32805796c8dcSSimon Schubert       }
32815796c8dcSSimon Schubert   if (nmapped == 0)
32825796c8dcSSimon Schubert     printf_filtered (_("No sections are mapped.\n"));
32835796c8dcSSimon Schubert }
32845796c8dcSSimon Schubert 
32855796c8dcSSimon Schubert /* Function: map_overlay_command
32865796c8dcSSimon Schubert    Mark the named section as mapped (ie. residing at its VMA address).  */
32875796c8dcSSimon Schubert 
3288*ef5ccd6cSJohn Marino static void
map_overlay_command(char * args,int from_tty)32895796c8dcSSimon Schubert map_overlay_command (char *args, int from_tty)
32905796c8dcSSimon Schubert {
32915796c8dcSSimon Schubert   struct objfile *objfile, *objfile2;
32925796c8dcSSimon Schubert   struct obj_section *sec, *sec2;
32935796c8dcSSimon Schubert 
32945796c8dcSSimon Schubert   if (!overlay_debugging)
3295c50c785cSJohn Marino     error (_("Overlay debugging not enabled.  Use "
3296c50c785cSJohn Marino 	     "either the 'overlay auto' or\n"
3297c50c785cSJohn Marino 	     "the 'overlay manual' command."));
32985796c8dcSSimon Schubert 
32995796c8dcSSimon Schubert   if (args == 0 || *args == 0)
33005796c8dcSSimon Schubert     error (_("Argument required: name of an overlay section"));
33015796c8dcSSimon Schubert 
3302c50c785cSJohn Marino   /* First, find a section matching the user supplied argument.  */
33035796c8dcSSimon Schubert   ALL_OBJSECTIONS (objfile, sec)
33045796c8dcSSimon Schubert     if (!strcmp (bfd_section_name (objfile->obfd, sec->the_bfd_section), args))
33055796c8dcSSimon Schubert     {
33065796c8dcSSimon Schubert       /* Now, check to see if the section is an overlay.  */
33075796c8dcSSimon Schubert       if (!section_is_overlay (sec))
33085796c8dcSSimon Schubert 	continue;		/* not an overlay section */
33095796c8dcSSimon Schubert 
3310c50c785cSJohn Marino       /* Mark the overlay as "mapped".  */
33115796c8dcSSimon Schubert       sec->ovly_mapped = 1;
33125796c8dcSSimon Schubert 
33135796c8dcSSimon Schubert       /* Next, make a pass and unmap any sections that are
33145796c8dcSSimon Schubert          overlapped by this new section: */
33155796c8dcSSimon Schubert       ALL_OBJSECTIONS (objfile2, sec2)
33165796c8dcSSimon Schubert 	if (sec2->ovly_mapped && sec != sec2 && sections_overlap (sec, sec2))
33175796c8dcSSimon Schubert 	{
33185796c8dcSSimon Schubert 	  if (info_verbose)
33195796c8dcSSimon Schubert 	    printf_unfiltered (_("Note: section %s unmapped by overlap\n"),
33205796c8dcSSimon Schubert 			     bfd_section_name (objfile->obfd,
33215796c8dcSSimon Schubert 					       sec2->the_bfd_section));
3322c50c785cSJohn Marino 	  sec2->ovly_mapped = 0;	/* sec2 overlaps sec: unmap sec2.  */
33235796c8dcSSimon Schubert 	}
33245796c8dcSSimon Schubert       return;
33255796c8dcSSimon Schubert     }
33265796c8dcSSimon Schubert   error (_("No overlay section called %s"), args);
33275796c8dcSSimon Schubert }
33285796c8dcSSimon Schubert 
33295796c8dcSSimon Schubert /* Function: unmap_overlay_command
33305796c8dcSSimon Schubert    Mark the overlay section as unmapped
33315796c8dcSSimon Schubert    (ie. resident in its LMA address range, rather than the VMA range).  */
33325796c8dcSSimon Schubert 
3333*ef5ccd6cSJohn Marino static void
unmap_overlay_command(char * args,int from_tty)33345796c8dcSSimon Schubert unmap_overlay_command (char *args, int from_tty)
33355796c8dcSSimon Schubert {
33365796c8dcSSimon Schubert   struct objfile *objfile;
33375796c8dcSSimon Schubert   struct obj_section *sec;
33385796c8dcSSimon Schubert 
33395796c8dcSSimon Schubert   if (!overlay_debugging)
3340c50c785cSJohn Marino     error (_("Overlay debugging not enabled.  "
3341c50c785cSJohn Marino 	     "Use either the 'overlay auto' or\n"
3342c50c785cSJohn Marino 	     "the 'overlay manual' command."));
33435796c8dcSSimon Schubert 
33445796c8dcSSimon Schubert   if (args == 0 || *args == 0)
33455796c8dcSSimon Schubert     error (_("Argument required: name of an overlay section"));
33465796c8dcSSimon Schubert 
3347c50c785cSJohn Marino   /* First, find a section matching the user supplied argument.  */
33485796c8dcSSimon Schubert   ALL_OBJSECTIONS (objfile, sec)
33495796c8dcSSimon Schubert     if (!strcmp (bfd_section_name (objfile->obfd, sec->the_bfd_section), args))
33505796c8dcSSimon Schubert     {
33515796c8dcSSimon Schubert       if (!sec->ovly_mapped)
33525796c8dcSSimon Schubert 	error (_("Section %s is not mapped"), args);
33535796c8dcSSimon Schubert       sec->ovly_mapped = 0;
33545796c8dcSSimon Schubert       return;
33555796c8dcSSimon Schubert     }
33565796c8dcSSimon Schubert   error (_("No overlay section called %s"), args);
33575796c8dcSSimon Schubert }
33585796c8dcSSimon Schubert 
33595796c8dcSSimon Schubert /* Function: overlay_auto_command
33605796c8dcSSimon Schubert    A utility command to turn on overlay debugging.
33615796c8dcSSimon Schubert    Possibly this should be done via a set/show command.  */
33625796c8dcSSimon Schubert 
33635796c8dcSSimon Schubert static void
overlay_auto_command(char * args,int from_tty)33645796c8dcSSimon Schubert overlay_auto_command (char *args, int from_tty)
33655796c8dcSSimon Schubert {
33665796c8dcSSimon Schubert   overlay_debugging = ovly_auto;
33675796c8dcSSimon Schubert   enable_overlay_breakpoints ();
33685796c8dcSSimon Schubert   if (info_verbose)
33695796c8dcSSimon Schubert     printf_unfiltered (_("Automatic overlay debugging enabled."));
33705796c8dcSSimon Schubert }
33715796c8dcSSimon Schubert 
33725796c8dcSSimon Schubert /* Function: overlay_manual_command
33735796c8dcSSimon Schubert    A utility command to turn on overlay debugging.
33745796c8dcSSimon Schubert    Possibly this should be done via a set/show command.  */
33755796c8dcSSimon Schubert 
33765796c8dcSSimon Schubert static void
overlay_manual_command(char * args,int from_tty)33775796c8dcSSimon Schubert overlay_manual_command (char *args, int from_tty)
33785796c8dcSSimon Schubert {
33795796c8dcSSimon Schubert   overlay_debugging = ovly_on;
33805796c8dcSSimon Schubert   disable_overlay_breakpoints ();
33815796c8dcSSimon Schubert   if (info_verbose)
33825796c8dcSSimon Schubert     printf_unfiltered (_("Overlay debugging enabled."));
33835796c8dcSSimon Schubert }
33845796c8dcSSimon Schubert 
33855796c8dcSSimon Schubert /* Function: overlay_off_command
33865796c8dcSSimon Schubert    A utility command to turn on overlay debugging.
33875796c8dcSSimon Schubert    Possibly this should be done via a set/show command.  */
33885796c8dcSSimon Schubert 
33895796c8dcSSimon Schubert static void
overlay_off_command(char * args,int from_tty)33905796c8dcSSimon Schubert overlay_off_command (char *args, int from_tty)
33915796c8dcSSimon Schubert {
33925796c8dcSSimon Schubert   overlay_debugging = ovly_off;
33935796c8dcSSimon Schubert   disable_overlay_breakpoints ();
33945796c8dcSSimon Schubert   if (info_verbose)
33955796c8dcSSimon Schubert     printf_unfiltered (_("Overlay debugging disabled."));
33965796c8dcSSimon Schubert }
33975796c8dcSSimon Schubert 
33985796c8dcSSimon Schubert static void
overlay_load_command(char * args,int from_tty)33995796c8dcSSimon Schubert overlay_load_command (char *args, int from_tty)
34005796c8dcSSimon Schubert {
34015796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_current_arch ();
34025796c8dcSSimon Schubert 
34035796c8dcSSimon Schubert   if (gdbarch_overlay_update_p (gdbarch))
34045796c8dcSSimon Schubert     gdbarch_overlay_update (gdbarch, NULL);
34055796c8dcSSimon Schubert   else
34065796c8dcSSimon Schubert     error (_("This target does not know how to read its overlay state."));
34075796c8dcSSimon Schubert }
34085796c8dcSSimon Schubert 
34095796c8dcSSimon Schubert /* Function: overlay_command
3410c50c785cSJohn Marino    A place-holder for a mis-typed command.  */
34115796c8dcSSimon Schubert 
34125796c8dcSSimon Schubert /* Command list chain containing all defined "overlay" subcommands.  */
3413*ef5ccd6cSJohn Marino static struct cmd_list_element *overlaylist;
34145796c8dcSSimon Schubert 
34155796c8dcSSimon Schubert static void
overlay_command(char * args,int from_tty)34165796c8dcSSimon Schubert overlay_command (char *args, int from_tty)
34175796c8dcSSimon Schubert {
34185796c8dcSSimon Schubert   printf_unfiltered
34195796c8dcSSimon Schubert     ("\"overlay\" must be followed by the name of an overlay command.\n");
34205796c8dcSSimon Schubert   help_list (overlaylist, "overlay ", -1, gdb_stdout);
34215796c8dcSSimon Schubert }
34225796c8dcSSimon Schubert 
34235796c8dcSSimon Schubert 
34245796c8dcSSimon Schubert /* Target Overlays for the "Simplest" overlay manager:
34255796c8dcSSimon Schubert 
34265796c8dcSSimon Schubert    This is GDB's default target overlay layer.  It works with the
34275796c8dcSSimon Schubert    minimal overlay manager supplied as an example by Cygnus.  The
34285796c8dcSSimon Schubert    entry point is via a function pointer "gdbarch_overlay_update",
34295796c8dcSSimon Schubert    so targets that use a different runtime overlay manager can
34305796c8dcSSimon Schubert    substitute their own overlay_update function and take over the
34315796c8dcSSimon Schubert    function pointer.
34325796c8dcSSimon Schubert 
34335796c8dcSSimon Schubert    The overlay_update function pokes around in the target's data structures
34345796c8dcSSimon Schubert    to see what overlays are mapped, and updates GDB's overlay mapping with
34355796c8dcSSimon Schubert    this information.
34365796c8dcSSimon Schubert 
34375796c8dcSSimon Schubert    In this simple implementation, the target data structures are as follows:
34385796c8dcSSimon Schubert    unsigned _novlys;            /# number of overlay sections #/
34395796c8dcSSimon Schubert    unsigned _ovly_table[_novlys][4] = {
34405796c8dcSSimon Schubert    {VMA, SIZE, LMA, MAPPED},    /# one entry per overlay section #/
34415796c8dcSSimon Schubert    {..., ...,  ..., ...},
34425796c8dcSSimon Schubert    }
34435796c8dcSSimon Schubert    unsigned _novly_regions;     /# number of overlay regions #/
34445796c8dcSSimon Schubert    unsigned _ovly_region_table[_novly_regions][3] = {
34455796c8dcSSimon Schubert    {VMA, SIZE, MAPPED_TO_LMA},  /# one entry per overlay region #/
34465796c8dcSSimon Schubert    {..., ...,  ...},
34475796c8dcSSimon Schubert    }
34485796c8dcSSimon Schubert    These functions will attempt to update GDB's mappedness state in the
34495796c8dcSSimon Schubert    symbol section table, based on the target's mappedness state.
34505796c8dcSSimon Schubert 
34515796c8dcSSimon Schubert    To do this, we keep a cached copy of the target's _ovly_table, and
34525796c8dcSSimon Schubert    attempt to detect when the cached copy is invalidated.  The main
34535796c8dcSSimon Schubert    entry point is "simple_overlay_update(SECT), which looks up SECT in
34545796c8dcSSimon Schubert    the cached table and re-reads only the entry for that section from
3455c50c785cSJohn Marino    the target (whenever possible).  */
34565796c8dcSSimon Schubert 
34575796c8dcSSimon Schubert /* Cached, dynamically allocated copies of the target data structures: */
34585796c8dcSSimon Schubert static unsigned (*cache_ovly_table)[4] = 0;
34595796c8dcSSimon Schubert static unsigned cache_novlys = 0;
34605796c8dcSSimon Schubert static CORE_ADDR cache_ovly_table_base = 0;
34615796c8dcSSimon Schubert enum ovly_index
34625796c8dcSSimon Schubert   {
34635796c8dcSSimon Schubert     VMA, SIZE, LMA, MAPPED
34645796c8dcSSimon Schubert   };
34655796c8dcSSimon Schubert 
3466c50c785cSJohn Marino /* Throw away the cached copy of _ovly_table.  */
34675796c8dcSSimon Schubert static void
simple_free_overlay_table(void)34685796c8dcSSimon Schubert simple_free_overlay_table (void)
34695796c8dcSSimon Schubert {
34705796c8dcSSimon Schubert   if (cache_ovly_table)
34715796c8dcSSimon Schubert     xfree (cache_ovly_table);
34725796c8dcSSimon Schubert   cache_novlys = 0;
34735796c8dcSSimon Schubert   cache_ovly_table = NULL;
34745796c8dcSSimon Schubert   cache_ovly_table_base = 0;
34755796c8dcSSimon Schubert }
34765796c8dcSSimon Schubert 
34775796c8dcSSimon Schubert /* Read an array of ints of size SIZE from the target into a local buffer.
3478c50c785cSJohn Marino    Convert to host order.  int LEN is number of ints.  */
34795796c8dcSSimon Schubert static void
read_target_long_array(CORE_ADDR memaddr,unsigned int * myaddr,int len,int size,enum bfd_endian byte_order)34805796c8dcSSimon Schubert read_target_long_array (CORE_ADDR memaddr, unsigned int *myaddr,
34815796c8dcSSimon Schubert 			int len, int size, enum bfd_endian byte_order)
34825796c8dcSSimon Schubert {
34835796c8dcSSimon Schubert   /* FIXME (alloca): Not safe if array is very large.  */
34845796c8dcSSimon Schubert   gdb_byte *buf = alloca (len * size);
34855796c8dcSSimon Schubert   int i;
34865796c8dcSSimon Schubert 
34875796c8dcSSimon Schubert   read_memory (memaddr, buf, len * size);
34885796c8dcSSimon Schubert   for (i = 0; i < len; i++)
34895796c8dcSSimon Schubert     myaddr[i] = extract_unsigned_integer (size * i + buf, size, byte_order);
34905796c8dcSSimon Schubert }
34915796c8dcSSimon Schubert 
34925796c8dcSSimon Schubert /* Find and grab a copy of the target _ovly_table
3493c50c785cSJohn Marino    (and _novlys, which is needed for the table's size).  */
34945796c8dcSSimon Schubert static int
simple_read_overlay_table(void)34955796c8dcSSimon Schubert simple_read_overlay_table (void)
34965796c8dcSSimon Schubert {
34975796c8dcSSimon Schubert   struct minimal_symbol *novlys_msym, *ovly_table_msym;
34985796c8dcSSimon Schubert   struct gdbarch *gdbarch;
34995796c8dcSSimon Schubert   int word_size;
35005796c8dcSSimon Schubert   enum bfd_endian byte_order;
35015796c8dcSSimon Schubert 
35025796c8dcSSimon Schubert   simple_free_overlay_table ();
35035796c8dcSSimon Schubert   novlys_msym = lookup_minimal_symbol ("_novlys", NULL, NULL);
35045796c8dcSSimon Schubert   if (! novlys_msym)
35055796c8dcSSimon Schubert     {
35065796c8dcSSimon Schubert       error (_("Error reading inferior's overlay table: "
35075796c8dcSSimon Schubert              "couldn't find `_novlys' variable\n"
35085796c8dcSSimon Schubert              "in inferior.  Use `overlay manual' mode."));
35095796c8dcSSimon Schubert       return 0;
35105796c8dcSSimon Schubert     }
35115796c8dcSSimon Schubert 
35125796c8dcSSimon Schubert   ovly_table_msym = lookup_minimal_symbol ("_ovly_table", NULL, NULL);
35135796c8dcSSimon Schubert   if (! ovly_table_msym)
35145796c8dcSSimon Schubert     {
35155796c8dcSSimon Schubert       error (_("Error reading inferior's overlay table: couldn't find "
35165796c8dcSSimon Schubert              "`_ovly_table' array\n"
35175796c8dcSSimon Schubert              "in inferior.  Use `overlay manual' mode."));
35185796c8dcSSimon Schubert       return 0;
35195796c8dcSSimon Schubert     }
35205796c8dcSSimon Schubert 
35215796c8dcSSimon Schubert   gdbarch = get_objfile_arch (msymbol_objfile (ovly_table_msym));
35225796c8dcSSimon Schubert   word_size = gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT;
35235796c8dcSSimon Schubert   byte_order = gdbarch_byte_order (gdbarch);
35245796c8dcSSimon Schubert 
35255796c8dcSSimon Schubert   cache_novlys = read_memory_integer (SYMBOL_VALUE_ADDRESS (novlys_msym),
35265796c8dcSSimon Schubert 				      4, byte_order);
35275796c8dcSSimon Schubert   cache_ovly_table
35285796c8dcSSimon Schubert     = (void *) xmalloc (cache_novlys * sizeof (*cache_ovly_table));
35295796c8dcSSimon Schubert   cache_ovly_table_base = SYMBOL_VALUE_ADDRESS (ovly_table_msym);
35305796c8dcSSimon Schubert   read_target_long_array (cache_ovly_table_base,
35315796c8dcSSimon Schubert                           (unsigned int *) cache_ovly_table,
35325796c8dcSSimon Schubert                           cache_novlys * 4, word_size, byte_order);
35335796c8dcSSimon Schubert 
35345796c8dcSSimon Schubert   return 1;			/* SUCCESS */
35355796c8dcSSimon Schubert }
35365796c8dcSSimon Schubert 
35375796c8dcSSimon Schubert /* Function: simple_overlay_update_1
35385796c8dcSSimon Schubert    A helper function for simple_overlay_update.  Assuming a cached copy
35395796c8dcSSimon Schubert    of _ovly_table exists, look through it to find an entry whose vma,
35405796c8dcSSimon Schubert    lma and size match those of OSECT.  Re-read the entry and make sure
35415796c8dcSSimon Schubert    it still matches OSECT (else the table may no longer be valid).
35425796c8dcSSimon Schubert    Set OSECT's mapped state to match the entry.  Return: 1 for
35435796c8dcSSimon Schubert    success, 0 for failure.  */
35445796c8dcSSimon Schubert 
35455796c8dcSSimon Schubert static int
simple_overlay_update_1(struct obj_section * osect)35465796c8dcSSimon Schubert simple_overlay_update_1 (struct obj_section *osect)
35475796c8dcSSimon Schubert {
35485796c8dcSSimon Schubert   int i, size;
35495796c8dcSSimon Schubert   bfd *obfd = osect->objfile->obfd;
35505796c8dcSSimon Schubert   asection *bsect = osect->the_bfd_section;
35515796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_objfile_arch (osect->objfile);
35525796c8dcSSimon Schubert   int word_size = gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT;
35535796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
35545796c8dcSSimon Schubert 
35555796c8dcSSimon Schubert   size = bfd_get_section_size (osect->the_bfd_section);
35565796c8dcSSimon Schubert   for (i = 0; i < cache_novlys; i++)
35575796c8dcSSimon Schubert     if (cache_ovly_table[i][VMA] == bfd_section_vma (obfd, bsect)
35585796c8dcSSimon Schubert 	&& cache_ovly_table[i][LMA] == bfd_section_lma (obfd, bsect)
35595796c8dcSSimon Schubert 	/* && cache_ovly_table[i][SIZE] == size */ )
35605796c8dcSSimon Schubert       {
35615796c8dcSSimon Schubert 	read_target_long_array (cache_ovly_table_base + i * word_size,
35625796c8dcSSimon Schubert 				(unsigned int *) cache_ovly_table[i],
35635796c8dcSSimon Schubert 				4, word_size, byte_order);
35645796c8dcSSimon Schubert 	if (cache_ovly_table[i][VMA] == bfd_section_vma (obfd, bsect)
35655796c8dcSSimon Schubert 	    && cache_ovly_table[i][LMA] == bfd_section_lma (obfd, bsect)
35665796c8dcSSimon Schubert 	    /* && cache_ovly_table[i][SIZE] == size */ )
35675796c8dcSSimon Schubert 	  {
35685796c8dcSSimon Schubert 	    osect->ovly_mapped = cache_ovly_table[i][MAPPED];
35695796c8dcSSimon Schubert 	    return 1;
35705796c8dcSSimon Schubert 	  }
35715796c8dcSSimon Schubert 	else	/* Warning!  Warning!  Target's ovly table has changed!  */
35725796c8dcSSimon Schubert 	  return 0;
35735796c8dcSSimon Schubert       }
35745796c8dcSSimon Schubert   return 0;
35755796c8dcSSimon Schubert }
35765796c8dcSSimon Schubert 
35775796c8dcSSimon Schubert /* Function: simple_overlay_update
35785796c8dcSSimon Schubert    If OSECT is NULL, then update all sections' mapped state
35795796c8dcSSimon Schubert    (after re-reading the entire target _ovly_table).
35805796c8dcSSimon Schubert    If OSECT is non-NULL, then try to find a matching entry in the
35815796c8dcSSimon Schubert    cached ovly_table and update only OSECT's mapped state.
35825796c8dcSSimon Schubert    If a cached entry can't be found or the cache isn't valid, then
35835796c8dcSSimon Schubert    re-read the entire cache, and go ahead and update all sections.  */
35845796c8dcSSimon Schubert 
35855796c8dcSSimon Schubert void
simple_overlay_update(struct obj_section * osect)35865796c8dcSSimon Schubert simple_overlay_update (struct obj_section *osect)
35875796c8dcSSimon Schubert {
35885796c8dcSSimon Schubert   struct objfile *objfile;
35895796c8dcSSimon Schubert 
35905796c8dcSSimon Schubert   /* Were we given an osect to look up?  NULL means do all of them.  */
35915796c8dcSSimon Schubert   if (osect)
35925796c8dcSSimon Schubert     /* Have we got a cached copy of the target's overlay table?  */
35935796c8dcSSimon Schubert     if (cache_ovly_table != NULL)
3594c50c785cSJohn Marino       {
3595c50c785cSJohn Marino 	/* Does its cached location match what's currently in the
3596c50c785cSJohn Marino 	   symtab?  */
3597c50c785cSJohn Marino 	struct minimal_symbol *minsym
3598c50c785cSJohn Marino 	  = lookup_minimal_symbol ("_ovly_table", NULL, NULL);
3599c50c785cSJohn Marino 
3600c50c785cSJohn Marino 	if (minsym == NULL)
3601c50c785cSJohn Marino 	  error (_("Error reading inferior's overlay table: couldn't "
3602c50c785cSJohn Marino 		   "find `_ovly_table' array\n"
3603c50c785cSJohn Marino 		   "in inferior.  Use `overlay manual' mode."));
3604c50c785cSJohn Marino 
3605c50c785cSJohn Marino 	if (cache_ovly_table_base == SYMBOL_VALUE_ADDRESS (minsym))
3606c50c785cSJohn Marino 	  /* Then go ahead and try to look up this single section in
3607c50c785cSJohn Marino 	     the cache.  */
36085796c8dcSSimon Schubert 	  if (simple_overlay_update_1 (osect))
36095796c8dcSSimon Schubert 	    /* Found it!  We're done.  */
36105796c8dcSSimon Schubert 	    return;
3611c50c785cSJohn Marino       }
36125796c8dcSSimon Schubert 
36135796c8dcSSimon Schubert   /* Cached table no good: need to read the entire table anew.
36145796c8dcSSimon Schubert      Or else we want all the sections, in which case it's actually
36155796c8dcSSimon Schubert      more efficient to read the whole table in one block anyway.  */
36165796c8dcSSimon Schubert 
36175796c8dcSSimon Schubert   if (! simple_read_overlay_table ())
36185796c8dcSSimon Schubert     return;
36195796c8dcSSimon Schubert 
36205796c8dcSSimon Schubert   /* Now may as well update all sections, even if only one was requested.  */
36215796c8dcSSimon Schubert   ALL_OBJSECTIONS (objfile, osect)
36225796c8dcSSimon Schubert     if (section_is_overlay (osect))
36235796c8dcSSimon Schubert     {
36245796c8dcSSimon Schubert       int i, size;
36255796c8dcSSimon Schubert       bfd *obfd = osect->objfile->obfd;
36265796c8dcSSimon Schubert       asection *bsect = osect->the_bfd_section;
36275796c8dcSSimon Schubert 
36285796c8dcSSimon Schubert       size = bfd_get_section_size (bsect);
36295796c8dcSSimon Schubert       for (i = 0; i < cache_novlys; i++)
36305796c8dcSSimon Schubert 	if (cache_ovly_table[i][VMA] == bfd_section_vma (obfd, bsect)
36315796c8dcSSimon Schubert 	    && cache_ovly_table[i][LMA] == bfd_section_lma (obfd, bsect)
36325796c8dcSSimon Schubert 	    /* && cache_ovly_table[i][SIZE] == size */ )
3633c50c785cSJohn Marino 	  { /* obj_section matches i'th entry in ovly_table.  */
36345796c8dcSSimon Schubert 	    osect->ovly_mapped = cache_ovly_table[i][MAPPED];
3635c50c785cSJohn Marino 	    break;		/* finished with inner for loop: break out.  */
36365796c8dcSSimon Schubert 	  }
36375796c8dcSSimon Schubert     }
36385796c8dcSSimon Schubert }
36395796c8dcSSimon Schubert 
36405796c8dcSSimon Schubert /* Set the output sections and output offsets for section SECTP in
36415796c8dcSSimon Schubert    ABFD.  The relocation code in BFD will read these offsets, so we
36425796c8dcSSimon Schubert    need to be sure they're initialized.  We map each section to itself,
36435796c8dcSSimon Schubert    with no offset; this means that SECTP->vma will be honored.  */
36445796c8dcSSimon Schubert 
36455796c8dcSSimon Schubert static void
symfile_dummy_outputs(bfd * abfd,asection * sectp,void * dummy)36465796c8dcSSimon Schubert symfile_dummy_outputs (bfd *abfd, asection *sectp, void *dummy)
36475796c8dcSSimon Schubert {
36485796c8dcSSimon Schubert   sectp->output_section = sectp;
36495796c8dcSSimon Schubert   sectp->output_offset = 0;
36505796c8dcSSimon Schubert }
36515796c8dcSSimon Schubert 
3652cf7f2e2dSJohn Marino /* Default implementation for sym_relocate.  */
3653cf7f2e2dSJohn Marino 
3654cf7f2e2dSJohn Marino 
3655cf7f2e2dSJohn Marino bfd_byte *
default_symfile_relocate(struct objfile * objfile,asection * sectp,bfd_byte * buf)3656cf7f2e2dSJohn Marino default_symfile_relocate (struct objfile *objfile, asection *sectp,
3657cf7f2e2dSJohn Marino                           bfd_byte *buf)
3658cf7f2e2dSJohn Marino {
3659*ef5ccd6cSJohn Marino   /* Use sectp->owner instead of objfile->obfd.  sectp may point to a
3660*ef5ccd6cSJohn Marino      DWO file.  */
3661*ef5ccd6cSJohn Marino   bfd *abfd = sectp->owner;
3662cf7f2e2dSJohn Marino 
3663cf7f2e2dSJohn Marino   /* We're only interested in sections with relocation
3664cf7f2e2dSJohn Marino      information.  */
3665cf7f2e2dSJohn Marino   if ((sectp->flags & SEC_RELOC) == 0)
3666cf7f2e2dSJohn Marino     return NULL;
3667cf7f2e2dSJohn Marino 
3668cf7f2e2dSJohn Marino   /* We will handle section offsets properly elsewhere, so relocate as if
3669cf7f2e2dSJohn Marino      all sections begin at 0.  */
3670cf7f2e2dSJohn Marino   bfd_map_over_sections (abfd, symfile_dummy_outputs, NULL);
3671cf7f2e2dSJohn Marino 
3672cf7f2e2dSJohn Marino   return bfd_simple_get_relocated_section_contents (abfd, sectp, buf, NULL);
3673cf7f2e2dSJohn Marino }
3674cf7f2e2dSJohn Marino 
36755796c8dcSSimon Schubert /* Relocate the contents of a debug section SECTP in ABFD.  The
36765796c8dcSSimon Schubert    contents are stored in BUF if it is non-NULL, or returned in a
36775796c8dcSSimon Schubert    malloc'd buffer otherwise.
36785796c8dcSSimon Schubert 
36795796c8dcSSimon Schubert    For some platforms and debug info formats, shared libraries contain
36805796c8dcSSimon Schubert    relocations against the debug sections (particularly for DWARF-2;
36815796c8dcSSimon Schubert    one affected platform is PowerPC GNU/Linux, although it depends on
36825796c8dcSSimon Schubert    the version of the linker in use).  Also, ELF object files naturally
36835796c8dcSSimon Schubert    have unresolved relocations for their debug sections.  We need to apply
36845796c8dcSSimon Schubert    the relocations in order to get the locations of symbols correct.
36855796c8dcSSimon Schubert    Another example that may require relocation processing, is the
36865796c8dcSSimon Schubert    DWARF-2 .eh_frame section in .o files, although it isn't strictly a
36875796c8dcSSimon Schubert    debug section.  */
36885796c8dcSSimon Schubert 
36895796c8dcSSimon Schubert bfd_byte *
symfile_relocate_debug_section(struct objfile * objfile,asection * sectp,bfd_byte * buf)3690cf7f2e2dSJohn Marino symfile_relocate_debug_section (struct objfile *objfile,
3691cf7f2e2dSJohn Marino                                 asection *sectp, bfd_byte *buf)
36925796c8dcSSimon Schubert {
3693cf7f2e2dSJohn Marino   gdb_assert (objfile->sf->sym_relocate);
36945796c8dcSSimon Schubert 
3695cf7f2e2dSJohn Marino   return (*objfile->sf->sym_relocate) (objfile, sectp, buf);
36965796c8dcSSimon Schubert }
36975796c8dcSSimon Schubert 
36985796c8dcSSimon Schubert struct symfile_segment_data *
get_symfile_segment_data(bfd * abfd)36995796c8dcSSimon Schubert get_symfile_segment_data (bfd *abfd)
37005796c8dcSSimon Schubert {
3701c50c785cSJohn Marino   const struct sym_fns *sf = find_sym_fns (abfd);
37025796c8dcSSimon Schubert 
37035796c8dcSSimon Schubert   if (sf == NULL)
37045796c8dcSSimon Schubert     return NULL;
37055796c8dcSSimon Schubert 
37065796c8dcSSimon Schubert   return sf->sym_segments (abfd);
37075796c8dcSSimon Schubert }
37085796c8dcSSimon Schubert 
37095796c8dcSSimon Schubert void
free_symfile_segment_data(struct symfile_segment_data * data)37105796c8dcSSimon Schubert free_symfile_segment_data (struct symfile_segment_data *data)
37115796c8dcSSimon Schubert {
37125796c8dcSSimon Schubert   xfree (data->segment_bases);
37135796c8dcSSimon Schubert   xfree (data->segment_sizes);
37145796c8dcSSimon Schubert   xfree (data->segment_info);
37155796c8dcSSimon Schubert   xfree (data);
37165796c8dcSSimon Schubert }
37175796c8dcSSimon Schubert 
37185796c8dcSSimon Schubert 
37195796c8dcSSimon Schubert /* Given:
37205796c8dcSSimon Schubert    - DATA, containing segment addresses from the object file ABFD, and
37215796c8dcSSimon Schubert      the mapping from ABFD's sections onto the segments that own them,
37225796c8dcSSimon Schubert      and
37235796c8dcSSimon Schubert    - SEGMENT_BASES[0 .. NUM_SEGMENT_BASES - 1], holding the actual
37245796c8dcSSimon Schubert      segment addresses reported by the target,
37255796c8dcSSimon Schubert    store the appropriate offsets for each section in OFFSETS.
37265796c8dcSSimon Schubert 
37275796c8dcSSimon Schubert    If there are fewer entries in SEGMENT_BASES than there are segments
37285796c8dcSSimon Schubert    in DATA, then apply SEGMENT_BASES' last entry to all the segments.
37295796c8dcSSimon Schubert 
37305796c8dcSSimon Schubert    If there are more entries, then ignore the extra.  The target may
37315796c8dcSSimon Schubert    not be able to distinguish between an empty data segment and a
37325796c8dcSSimon Schubert    missing data segment; a missing text segment is less plausible.  */
37335796c8dcSSimon Schubert int
symfile_map_offsets_to_segments(bfd * abfd,struct symfile_segment_data * data,struct section_offsets * offsets,int num_segment_bases,const CORE_ADDR * segment_bases)37345796c8dcSSimon Schubert symfile_map_offsets_to_segments (bfd *abfd, struct symfile_segment_data *data,
37355796c8dcSSimon Schubert 				 struct section_offsets *offsets,
37365796c8dcSSimon Schubert 				 int num_segment_bases,
37375796c8dcSSimon Schubert 				 const CORE_ADDR *segment_bases)
37385796c8dcSSimon Schubert {
37395796c8dcSSimon Schubert   int i;
37405796c8dcSSimon Schubert   asection *sect;
37415796c8dcSSimon Schubert 
37425796c8dcSSimon Schubert   /* It doesn't make sense to call this function unless you have some
37435796c8dcSSimon Schubert      segment base addresses.  */
3744cf7f2e2dSJohn Marino   gdb_assert (num_segment_bases > 0);
37455796c8dcSSimon Schubert 
37465796c8dcSSimon Schubert   /* If we do not have segment mappings for the object file, we
37475796c8dcSSimon Schubert      can not relocate it by segments.  */
37485796c8dcSSimon Schubert   gdb_assert (data != NULL);
37495796c8dcSSimon Schubert   gdb_assert (data->num_segments > 0);
37505796c8dcSSimon Schubert 
37515796c8dcSSimon Schubert   for (i = 0, sect = abfd->sections; sect != NULL; i++, sect = sect->next)
37525796c8dcSSimon Schubert     {
37535796c8dcSSimon Schubert       int which = data->segment_info[i];
37545796c8dcSSimon Schubert 
37555796c8dcSSimon Schubert       gdb_assert (0 <= which && which <= data->num_segments);
37565796c8dcSSimon Schubert 
37575796c8dcSSimon Schubert       /* Don't bother computing offsets for sections that aren't
37585796c8dcSSimon Schubert          loaded as part of any segment.  */
37595796c8dcSSimon Schubert       if (! which)
37605796c8dcSSimon Schubert         continue;
37615796c8dcSSimon Schubert 
37625796c8dcSSimon Schubert       /* Use the last SEGMENT_BASES entry as the address of any extra
37635796c8dcSSimon Schubert          segments mentioned in DATA->segment_info.  */
37645796c8dcSSimon Schubert       if (which > num_segment_bases)
37655796c8dcSSimon Schubert         which = num_segment_bases;
37665796c8dcSSimon Schubert 
37675796c8dcSSimon Schubert       offsets->offsets[i] = (segment_bases[which - 1]
37685796c8dcSSimon Schubert                              - data->segment_bases[which - 1]);
37695796c8dcSSimon Schubert     }
37705796c8dcSSimon Schubert 
37715796c8dcSSimon Schubert   return 1;
37725796c8dcSSimon Schubert }
37735796c8dcSSimon Schubert 
37745796c8dcSSimon Schubert static void
symfile_find_segment_sections(struct objfile * objfile)37755796c8dcSSimon Schubert symfile_find_segment_sections (struct objfile *objfile)
37765796c8dcSSimon Schubert {
37775796c8dcSSimon Schubert   bfd *abfd = objfile->obfd;
37785796c8dcSSimon Schubert   int i;
37795796c8dcSSimon Schubert   asection *sect;
37805796c8dcSSimon Schubert   struct symfile_segment_data *data;
37815796c8dcSSimon Schubert 
37825796c8dcSSimon Schubert   data = get_symfile_segment_data (objfile->obfd);
37835796c8dcSSimon Schubert   if (data == NULL)
37845796c8dcSSimon Schubert     return;
37855796c8dcSSimon Schubert 
37865796c8dcSSimon Schubert   if (data->num_segments != 1 && data->num_segments != 2)
37875796c8dcSSimon Schubert     {
37885796c8dcSSimon Schubert       free_symfile_segment_data (data);
37895796c8dcSSimon Schubert       return;
37905796c8dcSSimon Schubert     }
37915796c8dcSSimon Schubert 
37925796c8dcSSimon Schubert   for (i = 0, sect = abfd->sections; sect != NULL; i++, sect = sect->next)
37935796c8dcSSimon Schubert     {
37945796c8dcSSimon Schubert       int which = data->segment_info[i];
37955796c8dcSSimon Schubert 
37965796c8dcSSimon Schubert       if (which == 1)
37975796c8dcSSimon Schubert 	{
37985796c8dcSSimon Schubert 	  if (objfile->sect_index_text == -1)
37995796c8dcSSimon Schubert 	    objfile->sect_index_text = sect->index;
38005796c8dcSSimon Schubert 
38015796c8dcSSimon Schubert 	  if (objfile->sect_index_rodata == -1)
38025796c8dcSSimon Schubert 	    objfile->sect_index_rodata = sect->index;
38035796c8dcSSimon Schubert 	}
38045796c8dcSSimon Schubert       else if (which == 2)
38055796c8dcSSimon Schubert 	{
38065796c8dcSSimon Schubert 	  if (objfile->sect_index_data == -1)
38075796c8dcSSimon Schubert 	    objfile->sect_index_data = sect->index;
38085796c8dcSSimon Schubert 
38095796c8dcSSimon Schubert 	  if (objfile->sect_index_bss == -1)
38105796c8dcSSimon Schubert 	    objfile->sect_index_bss = sect->index;
38115796c8dcSSimon Schubert 	}
38125796c8dcSSimon Schubert     }
38135796c8dcSSimon Schubert 
38145796c8dcSSimon Schubert   free_symfile_segment_data (data);
38155796c8dcSSimon Schubert }
38165796c8dcSSimon Schubert 
38175796c8dcSSimon Schubert void
_initialize_symfile(void)38185796c8dcSSimon Schubert _initialize_symfile (void)
38195796c8dcSSimon Schubert {
38205796c8dcSSimon Schubert   struct cmd_list_element *c;
38215796c8dcSSimon Schubert 
38225796c8dcSSimon Schubert   c = add_cmd ("symbol-file", class_files, symbol_file_command, _("\
38235796c8dcSSimon Schubert Load symbol table from executable file FILE.\n\
38245796c8dcSSimon Schubert The `file' command can also load symbol tables, as well as setting the file\n\
38255796c8dcSSimon Schubert to execute."), &cmdlist);
38265796c8dcSSimon Schubert   set_cmd_completer (c, filename_completer);
38275796c8dcSSimon Schubert 
38285796c8dcSSimon Schubert   c = add_cmd ("add-symbol-file", class_files, add_symbol_file_command, _("\
38295796c8dcSSimon Schubert Load symbols from FILE, assuming FILE has been dynamically loaded.\n\
3830c50c785cSJohn Marino Usage: add-symbol-file FILE ADDR [-s <SECT> <SECT_ADDR> -s <SECT> <SECT_ADDR>\
3831c50c785cSJohn Marino  ...]\nADDR is the starting address of the file's text.\n\
38325796c8dcSSimon Schubert The optional arguments are section-name section-address pairs and\n\
38335796c8dcSSimon Schubert should be specified if the data and bss segments are not contiguous\n\
38345796c8dcSSimon Schubert with the text.  SECT is a section name to be loaded at SECT_ADDR."),
38355796c8dcSSimon Schubert 	       &cmdlist);
38365796c8dcSSimon Schubert   set_cmd_completer (c, filename_completer);
38375796c8dcSSimon Schubert 
38385796c8dcSSimon Schubert   c = add_cmd ("load", class_files, load_command, _("\
38395796c8dcSSimon Schubert Dynamically load FILE into the running program, and record its symbols\n\
38405796c8dcSSimon Schubert for access from GDB.\n\
38415796c8dcSSimon Schubert A load OFFSET may also be given."), &cmdlist);
38425796c8dcSSimon Schubert   set_cmd_completer (c, filename_completer);
38435796c8dcSSimon Schubert 
38445796c8dcSSimon Schubert   add_prefix_cmd ("overlay", class_support, overlay_command,
38455796c8dcSSimon Schubert 		  _("Commands for debugging overlays."), &overlaylist,
38465796c8dcSSimon Schubert 		  "overlay ", 0, &cmdlist);
38475796c8dcSSimon Schubert 
38485796c8dcSSimon Schubert   add_com_alias ("ovly", "overlay", class_alias, 1);
38495796c8dcSSimon Schubert   add_com_alias ("ov", "overlay", class_alias, 1);
38505796c8dcSSimon Schubert 
38515796c8dcSSimon Schubert   add_cmd ("map-overlay", class_support, map_overlay_command,
38525796c8dcSSimon Schubert 	   _("Assert that an overlay section is mapped."), &overlaylist);
38535796c8dcSSimon Schubert 
38545796c8dcSSimon Schubert   add_cmd ("unmap-overlay", class_support, unmap_overlay_command,
38555796c8dcSSimon Schubert 	   _("Assert that an overlay section is unmapped."), &overlaylist);
38565796c8dcSSimon Schubert 
38575796c8dcSSimon Schubert   add_cmd ("list-overlays", class_support, list_overlays_command,
38585796c8dcSSimon Schubert 	   _("List mappings of overlay sections."), &overlaylist);
38595796c8dcSSimon Schubert 
38605796c8dcSSimon Schubert   add_cmd ("manual", class_support, overlay_manual_command,
38615796c8dcSSimon Schubert 	   _("Enable overlay debugging."), &overlaylist);
38625796c8dcSSimon Schubert   add_cmd ("off", class_support, overlay_off_command,
38635796c8dcSSimon Schubert 	   _("Disable overlay debugging."), &overlaylist);
38645796c8dcSSimon Schubert   add_cmd ("auto", class_support, overlay_auto_command,
38655796c8dcSSimon Schubert 	   _("Enable automatic overlay debugging."), &overlaylist);
38665796c8dcSSimon Schubert   add_cmd ("load-target", class_support, overlay_load_command,
38675796c8dcSSimon Schubert 	   _("Read the overlay mapping state from the target."), &overlaylist);
38685796c8dcSSimon Schubert 
38695796c8dcSSimon Schubert   /* Filename extension to source language lookup table: */
38705796c8dcSSimon Schubert   init_filename_language_table ();
38715796c8dcSSimon Schubert   add_setshow_string_noescape_cmd ("extension-language", class_files,
38725796c8dcSSimon Schubert 				   &ext_args, _("\
38735796c8dcSSimon Schubert Set mapping between filename extension and source language."), _("\
38745796c8dcSSimon Schubert Show mapping between filename extension and source language."), _("\
38755796c8dcSSimon Schubert Usage: set extension-language .foo bar"),
38765796c8dcSSimon Schubert 				   set_ext_lang_command,
38775796c8dcSSimon Schubert 				   show_ext_args,
38785796c8dcSSimon Schubert 				   &setlist, &showlist);
38795796c8dcSSimon Schubert 
38805796c8dcSSimon Schubert   add_info ("extensions", info_ext_lang_command,
38815796c8dcSSimon Schubert 	    _("All filename extensions associated with a source language."));
38825796c8dcSSimon Schubert 
38835796c8dcSSimon Schubert   add_setshow_optional_filename_cmd ("debug-file-directory", class_support,
38845796c8dcSSimon Schubert 				     &debug_file_directory, _("\
3885cf7f2e2dSJohn Marino Set the directories where separate debug symbols are searched for."), _("\
3886cf7f2e2dSJohn Marino Show the directories where separate debug symbols are searched for."), _("\
38875796c8dcSSimon Schubert Separate debug symbols are first searched for in the same\n\
38885796c8dcSSimon Schubert directory as the binary, then in the `" DEBUG_SUBDIRECTORY "' subdirectory,\n\
38895796c8dcSSimon Schubert and lastly at the path of the directory of the binary with\n\
3890cf7f2e2dSJohn Marino each global debug-file-directory component prepended."),
38915796c8dcSSimon Schubert 				     NULL,
38925796c8dcSSimon Schubert 				     show_debug_file_directory,
38935796c8dcSSimon Schubert 				     &setlist, &showlist);
38945796c8dcSSimon Schubert }
3895