GDB (API)
/home/stan/gdb/src/gdb/symfile.c
Go to the documentation of this file.
00001 /* Generic symbol file reading for the GNU debugger, GDB.
00002 
00003    Copyright (C) 1990-2013 Free Software Foundation, Inc.
00004 
00005    Contributed by Cygnus Support, using pieces from other GDB modules.
00006 
00007    This file is part of GDB.
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; either version 3 of the License, or
00012    (at your option) any later version.
00013 
00014    This program is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017    GNU General Public License for more details.
00018 
00019    You should have received a copy of the GNU General Public License
00020    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00021 
00022 #include "defs.h"
00023 #include "arch-utils.h"
00024 #include "bfdlink.h"
00025 #include "symtab.h"
00026 #include "gdbtypes.h"
00027 #include "gdbcore.h"
00028 #include "frame.h"
00029 #include "target.h"
00030 #include "value.h"
00031 #include "symfile.h"
00032 #include "objfiles.h"
00033 #include "source.h"
00034 #include "gdbcmd.h"
00035 #include "breakpoint.h"
00036 #include "language.h"
00037 #include "complaints.h"
00038 #include "demangle.h"
00039 #include "inferior.h"
00040 #include "regcache.h"
00041 #include "filenames.h"          /* for DOSish file names */
00042 #include "gdb-stabs.h"
00043 #include "gdb_obstack.h"
00044 #include "completer.h"
00045 #include "bcache.h"
00046 #include "hashtab.h"
00047 #include "readline/readline.h"
00048 #include "gdb_assert.h"
00049 #include "block.h"
00050 #include "observer.h"
00051 #include "exec.h"
00052 #include "parser-defs.h"
00053 #include "varobj.h"
00054 #include "elf-bfd.h"
00055 #include "solib.h"
00056 #include "remote.h"
00057 #include "stack.h"
00058 #include "gdb_bfd.h"
00059 #include "cli/cli-utils.h"
00060 
00061 #include <sys/types.h>
00062 #include <fcntl.h>
00063 #include "gdb_string.h"
00064 #include "gdb_stat.h"
00065 #include <ctype.h>
00066 #include <time.h>
00067 #include <sys/time.h>
00068 
00069 #include "psymtab.h"
00070 
00071 int (*deprecated_ui_load_progress_hook) (const char *section,
00072                                          unsigned long num);
00073 void (*deprecated_show_load_progress) (const char *section,
00074                             unsigned long section_sent,
00075                             unsigned long section_size,
00076                             unsigned long total_sent,
00077                             unsigned long total_size);
00078 void (*deprecated_pre_add_symbol_hook) (const char *);
00079 void (*deprecated_post_add_symbol_hook) (void);
00080 
00081 static void clear_symtab_users_cleanup (void *ignore);
00082 
00083 /* Global variables owned by this file.  */
00084 int readnow_symbol_files;       /* Read full symbols immediately.  */
00085 
00086 /* Functions this file defines.  */
00087 
00088 static void load_command (char *, int);
00089 
00090 static void symbol_file_add_main_1 (const char *args, int from_tty, int flags);
00091 
00092 static void add_symbol_file_command (char *, int);
00093 
00094 static const struct sym_fns *find_sym_fns (bfd *);
00095 
00096 static void decrement_reading_symtab (void *);
00097 
00098 static void overlay_invalidate_all (void);
00099 
00100 static void overlay_auto_command (char *, int);
00101 
00102 static void overlay_manual_command (char *, int);
00103 
00104 static void overlay_off_command (char *, int);
00105 
00106 static void overlay_load_command (char *, int);
00107 
00108 static void overlay_command (char *, int);
00109 
00110 static void simple_free_overlay_table (void);
00111 
00112 static void read_target_long_array (CORE_ADDR, unsigned int *, int, int,
00113                                     enum bfd_endian);
00114 
00115 static int simple_read_overlay_table (void);
00116 
00117 static int simple_overlay_update_1 (struct obj_section *);
00118 
00119 static void add_filename_language (char *ext, enum language lang);
00120 
00121 static void info_ext_lang_command (char *args, int from_tty);
00122 
00123 static void init_filename_language_table (void);
00124 
00125 static void symfile_find_segment_sections (struct objfile *objfile);
00126 
00127 void _initialize_symfile (void);
00128 
00129 /* List of all available sym_fns.  On gdb startup, each object file reader
00130    calls add_symtab_fns() to register information on each format it is
00131    prepared to read.  */
00132 
00133 typedef struct
00134 {
00135   /* BFD flavour that we handle.  */
00136   enum bfd_flavour sym_flavour;
00137 
00138   /* The "vtable" of symbol functions.  */
00139   const struct sym_fns *sym_fns;
00140 } registered_sym_fns;
00141 
00142 DEF_VEC_O (registered_sym_fns);
00143 
00144 static VEC (registered_sym_fns) *symtab_fns = NULL;
00145 
00146 /* If non-zero, shared library symbols will be added automatically
00147    when the inferior is created, new libraries are loaded, or when
00148    attaching to the inferior.  This is almost always what users will
00149    want to have happen; but for very large programs, the startup time
00150    will be excessive, and so if this is a problem, the user can clear
00151    this flag and then add the shared library symbols as needed.  Note
00152    that there is a potential for confusion, since if the shared
00153    library symbols are not loaded, commands like "info fun" will *not*
00154    report all the functions that are actually present.  */
00155 
00156 int auto_solib_add = 1;
00157 
00158 
00159 /* True if we are reading a symbol table.  */
00160 
00161 int currently_reading_symtab = 0;
00162 
00163 static void
00164 decrement_reading_symtab (void *dummy)
00165 {
00166   currently_reading_symtab--;
00167   gdb_assert (currently_reading_symtab >= 0);
00168 }
00169 
00170 /* Increment currently_reading_symtab and return a cleanup that can be
00171    used to decrement it.  */
00172 
00173 struct cleanup *
00174 increment_reading_symtab (void)
00175 {
00176   ++currently_reading_symtab;
00177   gdb_assert (currently_reading_symtab > 0);
00178   return make_cleanup (decrement_reading_symtab, NULL);
00179 }
00180 
00181 /* Remember the lowest-addressed loadable section we've seen.
00182    This function is called via bfd_map_over_sections.
00183 
00184    In case of equal vmas, the section with the largest size becomes the
00185    lowest-addressed loadable section.
00186 
00187    If the vmas and sizes are equal, the last section is considered the
00188    lowest-addressed loadable section.  */
00189 
00190 void
00191 find_lowest_section (bfd *abfd, asection *sect, void *obj)
00192 {
00193   asection **lowest = (asection **) obj;
00194 
00195   if (0 == (bfd_get_section_flags (abfd, sect) & (SEC_ALLOC | SEC_LOAD)))
00196     return;
00197   if (!*lowest)
00198     *lowest = sect;             /* First loadable section */
00199   else if (bfd_section_vma (abfd, *lowest) > bfd_section_vma (abfd, sect))
00200     *lowest = sect;             /* A lower loadable section */
00201   else if (bfd_section_vma (abfd, *lowest) == bfd_section_vma (abfd, sect)
00202            && (bfd_section_size (abfd, (*lowest))
00203                <= bfd_section_size (abfd, sect)))
00204     *lowest = sect;
00205 }
00206 
00207 /* Create a new section_addr_info, with room for NUM_SECTIONS.  The
00208    new object's 'num_sections' field is set to 0; it must be updated
00209    by the caller.  */
00210 
00211 struct section_addr_info *
00212 alloc_section_addr_info (size_t num_sections)
00213 {
00214   struct section_addr_info *sap;
00215   size_t size;
00216 
00217   size = (sizeof (struct section_addr_info)
00218           +  sizeof (struct other_sections) * (num_sections - 1));
00219   sap = (struct section_addr_info *) xmalloc (size);
00220   memset (sap, 0, size);
00221 
00222   return sap;
00223 }
00224 
00225 /* Build (allocate and populate) a section_addr_info struct from
00226    an existing section table.  */
00227 
00228 extern struct section_addr_info *
00229 build_section_addr_info_from_section_table (const struct target_section *start,
00230                                             const struct target_section *end)
00231 {
00232   struct section_addr_info *sap;
00233   const struct target_section *stp;
00234   int oidx;
00235 
00236   sap = alloc_section_addr_info (end - start);
00237 
00238   for (stp = start, oidx = 0; stp != end; stp++)
00239     {
00240       struct bfd_section *asect = stp->the_bfd_section;
00241       bfd *abfd = asect->owner;
00242 
00243       if (bfd_get_section_flags (abfd, asect) & (SEC_ALLOC | SEC_LOAD)
00244           && oidx < end - start)
00245         {
00246           sap->other[oidx].addr = stp->addr;
00247           sap->other[oidx].name = xstrdup (bfd_section_name (abfd, asect));
00248           sap->other[oidx].sectindex = gdb_bfd_section_index (abfd, asect);
00249           oidx++;
00250         }
00251     }
00252 
00253   sap->num_sections = oidx;
00254 
00255   return sap;
00256 }
00257 
00258 /* Create a section_addr_info from section offsets in ABFD.  */
00259 
00260 static struct section_addr_info *
00261 build_section_addr_info_from_bfd (bfd *abfd)
00262 {
00263   struct section_addr_info *sap;
00264   int i;
00265   struct bfd_section *sec;
00266 
00267   sap = alloc_section_addr_info (bfd_count_sections (abfd));
00268   for (i = 0, sec = abfd->sections; sec != NULL; sec = sec->next)
00269     if (bfd_get_section_flags (abfd, sec) & (SEC_ALLOC | SEC_LOAD))
00270       {
00271         sap->other[i].addr = bfd_get_section_vma (abfd, sec);
00272         sap->other[i].name = xstrdup (bfd_get_section_name (abfd, sec));
00273         sap->other[i].sectindex = gdb_bfd_section_index (abfd, sec);
00274         i++;
00275       }
00276 
00277   sap->num_sections = i;
00278 
00279   return sap;
00280 }
00281 
00282 /* Create a section_addr_info from section offsets in OBJFILE.  */
00283 
00284 struct section_addr_info *
00285 build_section_addr_info_from_objfile (const struct objfile *objfile)
00286 {
00287   struct section_addr_info *sap;
00288   int i;
00289 
00290   /* Before reread_symbols gets rewritten it is not safe to call:
00291      gdb_assert (objfile->num_sections == bfd_count_sections (objfile->obfd));
00292      */
00293   sap = build_section_addr_info_from_bfd (objfile->obfd);
00294   for (i = 0; i < sap->num_sections; i++)
00295     {
00296       int sectindex = sap->other[i].sectindex;
00297 
00298       sap->other[i].addr += objfile->section_offsets->offsets[sectindex];
00299     }
00300   return sap;
00301 }
00302 
00303 /* Free all memory allocated by build_section_addr_info_from_section_table.  */
00304 
00305 extern void
00306 free_section_addr_info (struct section_addr_info *sap)
00307 {
00308   int idx;
00309 
00310   for (idx = 0; idx < sap->num_sections; idx++)
00311     xfree (sap->other[idx].name);
00312   xfree (sap);
00313 }
00314 
00315 /* Initialize OBJFILE's sect_index_* members.  */
00316 
00317 static void
00318 init_objfile_sect_indices (struct objfile *objfile)
00319 {
00320   asection *sect;
00321   int i;
00322 
00323   sect = bfd_get_section_by_name (objfile->obfd, ".text");
00324   if (sect)
00325     objfile->sect_index_text = sect->index;
00326 
00327   sect = bfd_get_section_by_name (objfile->obfd, ".data");
00328   if (sect)
00329     objfile->sect_index_data = sect->index;
00330 
00331   sect = bfd_get_section_by_name (objfile->obfd, ".bss");
00332   if (sect)
00333     objfile->sect_index_bss = sect->index;
00334 
00335   sect = bfd_get_section_by_name (objfile->obfd, ".rodata");
00336   if (sect)
00337     objfile->sect_index_rodata = sect->index;
00338 
00339   /* This is where things get really weird...  We MUST have valid
00340      indices for the various sect_index_* members or gdb will abort.
00341      So if for example, there is no ".text" section, we have to
00342      accomodate that.  First, check for a file with the standard
00343      one or two segments.  */
00344 
00345   symfile_find_segment_sections (objfile);
00346 
00347   /* Except when explicitly adding symbol files at some address,
00348      section_offsets contains nothing but zeros, so it doesn't matter
00349      which slot in section_offsets the individual sect_index_* members
00350      index into.  So if they are all zero, it is safe to just point
00351      all the currently uninitialized indices to the first slot.  But
00352      beware: if this is the main executable, it may be relocated
00353      later, e.g. by the remote qOffsets packet, and then this will
00354      be wrong!  That's why we try segments first.  */
00355 
00356   for (i = 0; i < objfile->num_sections; i++)
00357     {
00358       if (ANOFFSET (objfile->section_offsets, i) != 0)
00359         {
00360           break;
00361         }
00362     }
00363   if (i == objfile->num_sections)
00364     {
00365       if (objfile->sect_index_text == -1)
00366         objfile->sect_index_text = 0;
00367       if (objfile->sect_index_data == -1)
00368         objfile->sect_index_data = 0;
00369       if (objfile->sect_index_bss == -1)
00370         objfile->sect_index_bss = 0;
00371       if (objfile->sect_index_rodata == -1)
00372         objfile->sect_index_rodata = 0;
00373     }
00374 }
00375 
00376 /* The arguments to place_section.  */
00377 
00378 struct place_section_arg
00379 {
00380   struct section_offsets *offsets;
00381   CORE_ADDR lowest;
00382 };
00383 
00384 /* Find a unique offset to use for loadable section SECT if
00385    the user did not provide an offset.  */
00386 
00387 static void
00388 place_section (bfd *abfd, asection *sect, void *obj)
00389 {
00390   struct place_section_arg *arg = obj;
00391   CORE_ADDR *offsets = arg->offsets->offsets, start_addr;
00392   int done;
00393   ULONGEST align = ((ULONGEST) 1) << bfd_get_section_alignment (abfd, sect);
00394 
00395   /* We are only interested in allocated sections.  */
00396   if ((bfd_get_section_flags (abfd, sect) & SEC_ALLOC) == 0)
00397     return;
00398 
00399   /* If the user specified an offset, honor it.  */
00400   if (offsets[gdb_bfd_section_index (abfd, sect)] != 0)
00401     return;
00402 
00403   /* Otherwise, let's try to find a place for the section.  */
00404   start_addr = (arg->lowest + align - 1) & -align;
00405 
00406   do {
00407     asection *cur_sec;
00408 
00409     done = 1;
00410 
00411     for (cur_sec = abfd->sections; cur_sec != NULL; cur_sec = cur_sec->next)
00412       {
00413         int indx = cur_sec->index;
00414 
00415         /* We don't need to compare against ourself.  */
00416         if (cur_sec == sect)
00417           continue;
00418 
00419         /* We can only conflict with allocated sections.  */
00420         if ((bfd_get_section_flags (abfd, cur_sec) & SEC_ALLOC) == 0)
00421           continue;
00422 
00423         /* If the section offset is 0, either the section has not been placed
00424            yet, or it was the lowest section placed (in which case LOWEST
00425            will be past its end).  */
00426         if (offsets[indx] == 0)
00427           continue;
00428 
00429         /* If this section would overlap us, then we must move up.  */
00430         if (start_addr + bfd_get_section_size (sect) > offsets[indx]
00431             && start_addr < offsets[indx] + bfd_get_section_size (cur_sec))
00432           {
00433             start_addr = offsets[indx] + bfd_get_section_size (cur_sec);
00434             start_addr = (start_addr + align - 1) & -align;
00435             done = 0;
00436             break;
00437           }
00438 
00439         /* Otherwise, we appear to be OK.  So far.  */
00440       }
00441     }
00442   while (!done);
00443 
00444   offsets[gdb_bfd_section_index (abfd, sect)] = start_addr;
00445   arg->lowest = start_addr + bfd_get_section_size (sect);
00446 }
00447 
00448 /* Store struct section_addr_info as prepared (made relative and with SECTINDEX
00449    filled-in) by addr_info_make_relative into SECTION_OFFSETS of NUM_SECTIONS
00450    entries.  */
00451 
00452 void
00453 relative_addr_info_to_section_offsets (struct section_offsets *section_offsets,
00454                                        int num_sections,
00455                                        const struct section_addr_info *addrs)
00456 {
00457   int i;
00458 
00459   memset (section_offsets, 0, SIZEOF_N_SECTION_OFFSETS (num_sections));
00460 
00461   /* Now calculate offsets for section that were specified by the caller.  */
00462   for (i = 0; i < addrs->num_sections; i++)
00463     {
00464       const struct other_sections *osp;
00465 
00466       osp = &addrs->other[i];
00467       if (osp->sectindex == -1)
00468         continue;
00469 
00470       /* Record all sections in offsets.  */
00471       /* The section_offsets in the objfile are here filled in using
00472          the BFD index.  */
00473       section_offsets->offsets[osp->sectindex] = osp->addr;
00474     }
00475 }
00476 
00477 /* Transform section name S for a name comparison.  prelink can split section
00478    `.bss' into two sections `.dynbss' and `.bss' (in this order).  Similarly
00479    prelink can split `.sbss' into `.sdynbss' and `.sbss'.  Use virtual address
00480    of the new `.dynbss' (`.sdynbss') section as the adjacent new `.bss'
00481    (`.sbss') section has invalid (increased) virtual address.  */
00482 
00483 static const char *
00484 addr_section_name (const char *s)
00485 {
00486   if (strcmp (s, ".dynbss") == 0)
00487     return ".bss";
00488   if (strcmp (s, ".sdynbss") == 0)
00489     return ".sbss";
00490 
00491   return s;
00492 }
00493 
00494 /* qsort comparator for addrs_section_sort.  Sort entries in ascending order by
00495    their (name, sectindex) pair.  sectindex makes the sort by name stable.  */
00496 
00497 static int
00498 addrs_section_compar (const void *ap, const void *bp)
00499 {
00500   const struct other_sections *a = *((struct other_sections **) ap);
00501   const struct other_sections *b = *((struct other_sections **) bp);
00502   int retval;
00503 
00504   retval = strcmp (addr_section_name (a->name), addr_section_name (b->name));
00505   if (retval)
00506     return retval;
00507 
00508   return a->sectindex - b->sectindex;
00509 }
00510 
00511 /* Provide sorted array of pointers to sections of ADDRS.  The array is
00512    terminated by NULL.  Caller is responsible to call xfree for it.  */
00513 
00514 static struct other_sections **
00515 addrs_section_sort (struct section_addr_info *addrs)
00516 {
00517   struct other_sections **array;
00518   int i;
00519 
00520   /* `+ 1' for the NULL terminator.  */
00521   array = xmalloc (sizeof (*array) * (addrs->num_sections + 1));
00522   for (i = 0; i < addrs->num_sections; i++)
00523     array[i] = &addrs->other[i];
00524   array[i] = NULL;
00525 
00526   qsort (array, i, sizeof (*array), addrs_section_compar);
00527 
00528   return array;
00529 }
00530 
00531 /* Relativize absolute addresses in ADDRS into offsets based on ABFD.  Fill-in
00532    also SECTINDEXes specific to ABFD there.  This function can be used to
00533    rebase ADDRS to start referencing different BFD than before.  */
00534 
00535 void
00536 addr_info_make_relative (struct section_addr_info *addrs, bfd *abfd)
00537 {
00538   asection *lower_sect;
00539   CORE_ADDR lower_offset;
00540   int i;
00541   struct cleanup *my_cleanup;
00542   struct section_addr_info *abfd_addrs;
00543   struct other_sections **addrs_sorted, **abfd_addrs_sorted;
00544   struct other_sections **addrs_to_abfd_addrs;
00545 
00546   /* Find lowest loadable section to be used as starting point for
00547      continguous sections.  */
00548   lower_sect = NULL;
00549   bfd_map_over_sections (abfd, find_lowest_section, &lower_sect);
00550   if (lower_sect == NULL)
00551     {
00552       warning (_("no loadable sections found in added symbol-file %s"),
00553                bfd_get_filename (abfd));
00554       lower_offset = 0;
00555     }
00556   else
00557     lower_offset = bfd_section_vma (bfd_get_filename (abfd), lower_sect);
00558 
00559   /* Create ADDRS_TO_ABFD_ADDRS array to map the sections in ADDRS to sections
00560      in ABFD.  Section names are not unique - there can be multiple sections of
00561      the same name.  Also the sections of the same name do not have to be
00562      adjacent to each other.  Some sections may be present only in one of the
00563      files.  Even sections present in both files do not have to be in the same
00564      order.
00565 
00566      Use stable sort by name for the sections in both files.  Then linearly
00567      scan both lists matching as most of the entries as possible.  */
00568 
00569   addrs_sorted = addrs_section_sort (addrs);
00570   my_cleanup = make_cleanup (xfree, addrs_sorted);
00571 
00572   abfd_addrs = build_section_addr_info_from_bfd (abfd);
00573   make_cleanup_free_section_addr_info (abfd_addrs);
00574   abfd_addrs_sorted = addrs_section_sort (abfd_addrs);
00575   make_cleanup (xfree, abfd_addrs_sorted);
00576 
00577   /* Now create ADDRS_TO_ABFD_ADDRS from ADDRS_SORTED and
00578      ABFD_ADDRS_SORTED.  */
00579 
00580   addrs_to_abfd_addrs = xzalloc (sizeof (*addrs_to_abfd_addrs)
00581                                  * addrs->num_sections);
00582   make_cleanup (xfree, addrs_to_abfd_addrs);
00583 
00584   while (*addrs_sorted)
00585     {
00586       const char *sect_name = addr_section_name ((*addrs_sorted)->name);
00587 
00588       while (*abfd_addrs_sorted
00589              && strcmp (addr_section_name ((*abfd_addrs_sorted)->name),
00590                         sect_name) < 0)
00591         abfd_addrs_sorted++;
00592 
00593       if (*abfd_addrs_sorted
00594           && strcmp (addr_section_name ((*abfd_addrs_sorted)->name),
00595                      sect_name) == 0)
00596         {
00597           int index_in_addrs;
00598 
00599           /* Make the found item directly addressable from ADDRS.  */
00600           index_in_addrs = *addrs_sorted - addrs->other;
00601           gdb_assert (addrs_to_abfd_addrs[index_in_addrs] == NULL);
00602           addrs_to_abfd_addrs[index_in_addrs] = *abfd_addrs_sorted;
00603 
00604           /* Never use the same ABFD entry twice.  */
00605           abfd_addrs_sorted++;
00606         }
00607 
00608       addrs_sorted++;
00609     }
00610 
00611   /* Calculate offsets for the loadable sections.
00612      FIXME! Sections must be in order of increasing loadable section
00613      so that contiguous sections can use the lower-offset!!!
00614 
00615      Adjust offsets if the segments are not contiguous.
00616      If the section is contiguous, its offset should be set to
00617      the offset of the highest loadable section lower than it
00618      (the loadable section directly below it in memory).
00619      this_offset = lower_offset = lower_addr - lower_orig_addr */
00620 
00621   for (i = 0; i < addrs->num_sections; i++)
00622     {
00623       struct other_sections *sect = addrs_to_abfd_addrs[i];
00624 
00625       if (sect)
00626         {
00627           /* This is the index used by BFD.  */
00628           addrs->other[i].sectindex = sect->sectindex;
00629 
00630           if (addrs->other[i].addr != 0)
00631             {
00632               addrs->other[i].addr -= sect->addr;
00633               lower_offset = addrs->other[i].addr;
00634             }
00635           else
00636             addrs->other[i].addr = lower_offset;
00637         }
00638       else
00639         {
00640           /* addr_section_name transformation is not used for SECT_NAME.  */
00641           const char *sect_name = addrs->other[i].name;
00642 
00643           /* This section does not exist in ABFD, which is normally
00644              unexpected and we want to issue a warning.
00645 
00646              However, the ELF prelinker does create a few sections which are
00647              marked in the main executable as loadable (they are loaded in
00648              memory from the DYNAMIC segment) and yet are not present in
00649              separate debug info files.  This is fine, and should not cause
00650              a warning.  Shared libraries contain just the section
00651              ".gnu.liblist" but it is not marked as loadable there.  There is
00652              no other way to identify them than by their name as the sections
00653              created by prelink have no special flags.
00654 
00655              For the sections `.bss' and `.sbss' see addr_section_name.  */
00656 
00657           if (!(strcmp (sect_name, ".gnu.liblist") == 0
00658                 || strcmp (sect_name, ".gnu.conflict") == 0
00659                 || (strcmp (sect_name, ".bss") == 0
00660                     && i > 0
00661                     && strcmp (addrs->other[i - 1].name, ".dynbss") == 0
00662                     && addrs_to_abfd_addrs[i - 1] != NULL)
00663                 || (strcmp (sect_name, ".sbss") == 0
00664                     && i > 0
00665                     && strcmp (addrs->other[i - 1].name, ".sdynbss") == 0
00666                     && addrs_to_abfd_addrs[i - 1] != NULL)))
00667             warning (_("section %s not found in %s"), sect_name,
00668                      bfd_get_filename (abfd));
00669 
00670           addrs->other[i].addr = 0;
00671           addrs->other[i].sectindex = -1;
00672         }
00673     }
00674 
00675   do_cleanups (my_cleanup);
00676 }
00677 
00678 /* Parse the user's idea of an offset for dynamic linking, into our idea
00679    of how to represent it for fast symbol reading.  This is the default
00680    version of the sym_fns.sym_offsets function for symbol readers that
00681    don't need to do anything special.  It allocates a section_offsets table
00682    for the objectfile OBJFILE and stuffs ADDR into all of the offsets.  */
00683 
00684 void
00685 default_symfile_offsets (struct objfile *objfile,
00686                          const struct section_addr_info *addrs)
00687 {
00688   objfile->num_sections = gdb_bfd_count_sections (objfile->obfd);
00689   objfile->section_offsets = (struct section_offsets *)
00690     obstack_alloc (&objfile->objfile_obstack,
00691                    SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
00692   relative_addr_info_to_section_offsets (objfile->section_offsets,
00693                                          objfile->num_sections, addrs);
00694 
00695   /* For relocatable files, all loadable sections will start at zero.
00696      The zero is meaningless, so try to pick arbitrary addresses such
00697      that no loadable sections overlap.  This algorithm is quadratic,
00698      but the number of sections in a single object file is generally
00699      small.  */
00700   if ((bfd_get_file_flags (objfile->obfd) & (EXEC_P | DYNAMIC)) == 0)
00701     {
00702       struct place_section_arg arg;
00703       bfd *abfd = objfile->obfd;
00704       asection *cur_sec;
00705 
00706       for (cur_sec = abfd->sections; cur_sec != NULL; cur_sec = cur_sec->next)
00707         /* We do not expect this to happen; just skip this step if the
00708            relocatable file has a section with an assigned VMA.  */
00709         if (bfd_section_vma (abfd, cur_sec) != 0)
00710           break;
00711 
00712       if (cur_sec == NULL)
00713         {
00714           CORE_ADDR *offsets = objfile->section_offsets->offsets;
00715 
00716           /* Pick non-overlapping offsets for sections the user did not
00717              place explicitly.  */
00718           arg.offsets = objfile->section_offsets;
00719           arg.lowest = 0;
00720           bfd_map_over_sections (objfile->obfd, place_section, &arg);
00721 
00722           /* Correctly filling in the section offsets is not quite
00723              enough.  Relocatable files have two properties that
00724              (most) shared objects do not:
00725 
00726              - Their debug information will contain relocations.  Some
00727              shared libraries do also, but many do not, so this can not
00728              be assumed.
00729 
00730              - If there are multiple code sections they will be loaded
00731              at different relative addresses in memory than they are
00732              in the objfile, since all sections in the file will start
00733              at address zero.
00734 
00735              Because GDB has very limited ability to map from an
00736              address in debug info to the correct code section,
00737              it relies on adding SECT_OFF_TEXT to things which might be
00738              code.  If we clear all the section offsets, and set the
00739              section VMAs instead, then symfile_relocate_debug_section
00740              will return meaningful debug information pointing at the
00741              correct sections.
00742 
00743              GDB has too many different data structures for section
00744              addresses - a bfd, objfile, and so_list all have section
00745              tables, as does exec_ops.  Some of these could probably
00746              be eliminated.  */
00747 
00748           for (cur_sec = abfd->sections; cur_sec != NULL;
00749                cur_sec = cur_sec->next)
00750             {
00751               if ((bfd_get_section_flags (abfd, cur_sec) & SEC_ALLOC) == 0)
00752                 continue;
00753 
00754               bfd_set_section_vma (abfd, cur_sec, offsets[cur_sec->index]);
00755               exec_set_section_address (bfd_get_filename (abfd),
00756                                         cur_sec->index,
00757                                         offsets[cur_sec->index]);
00758               offsets[cur_sec->index] = 0;
00759             }
00760         }
00761     }
00762 
00763   /* Remember the bfd indexes for the .text, .data, .bss and
00764      .rodata sections.  */
00765   init_objfile_sect_indices (objfile);
00766 }
00767 
00768 /* Divide the file into segments, which are individual relocatable units.
00769    This is the default version of the sym_fns.sym_segments function for
00770    symbol readers that do not have an explicit representation of segments.
00771    It assumes that object files do not have segments, and fully linked
00772    files have a single segment.  */
00773 
00774 struct symfile_segment_data *
00775 default_symfile_segments (bfd *abfd)
00776 {
00777   int num_sections, i;
00778   asection *sect;
00779   struct symfile_segment_data *data;
00780   CORE_ADDR low, high;
00781 
00782   /* Relocatable files contain enough information to position each
00783      loadable section independently; they should not be relocated
00784      in segments.  */
00785   if ((bfd_get_file_flags (abfd) & (EXEC_P | DYNAMIC)) == 0)
00786     return NULL;
00787 
00788   /* Make sure there is at least one loadable section in the file.  */
00789   for (sect = abfd->sections; sect != NULL; sect = sect->next)
00790     {
00791       if ((bfd_get_section_flags (abfd, sect) & SEC_ALLOC) == 0)
00792         continue;
00793 
00794       break;
00795     }
00796   if (sect == NULL)
00797     return NULL;
00798 
00799   low = bfd_get_section_vma (abfd, sect);
00800   high = low + bfd_get_section_size (sect);
00801 
00802   data = XZALLOC (struct symfile_segment_data);
00803   data->num_segments = 1;
00804   data->segment_bases = XCALLOC (1, CORE_ADDR);
00805   data->segment_sizes = XCALLOC (1, CORE_ADDR);
00806 
00807   num_sections = bfd_count_sections (abfd);
00808   data->segment_info = XCALLOC (num_sections, int);
00809 
00810   for (i = 0, sect = abfd->sections; sect != NULL; i++, sect = sect->next)
00811     {
00812       CORE_ADDR vma;
00813 
00814       if ((bfd_get_section_flags (abfd, sect) & SEC_ALLOC) == 0)
00815         continue;
00816 
00817       vma = bfd_get_section_vma (abfd, sect);
00818       if (vma < low)
00819         low = vma;
00820       if (vma + bfd_get_section_size (sect) > high)
00821         high = vma + bfd_get_section_size (sect);
00822 
00823       data->segment_info[i] = 1;
00824     }
00825 
00826   data->segment_bases[0] = low;
00827   data->segment_sizes[0] = high - low;
00828 
00829   return data;
00830 }
00831 
00832 /* This is a convenience function to call sym_read for OBJFILE and
00833    possibly force the partial symbols to be read.  */
00834 
00835 static void
00836 read_symbols (struct objfile *objfile, int add_flags)
00837 {
00838   (*objfile->sf->sym_read) (objfile, add_flags);
00839 
00840   /* find_separate_debug_file_in_section should be called only if there is
00841      single binary with no existing separate debug info file.  */
00842   if (!objfile_has_partial_symbols (objfile)
00843       && objfile->separate_debug_objfile == NULL
00844       && objfile->separate_debug_objfile_backlink == NULL)
00845     {
00846       bfd *abfd = find_separate_debug_file_in_section (objfile);
00847       struct cleanup *cleanup = make_cleanup_bfd_unref (abfd);
00848 
00849       if (abfd != NULL)
00850         {
00851           /* find_separate_debug_file_in_section uses the same filename for the
00852              virtual section-as-bfd like the bfd filename containing the
00853              section.  Therefore use also non-canonical name form for the same
00854              file containing the section.  */
00855           symbol_file_add_separate (abfd, objfile->original_name, add_flags,
00856                                     objfile);
00857         }
00858 
00859       do_cleanups (cleanup);
00860     }
00861   if ((add_flags & SYMFILE_NO_READ) == 0)
00862     require_partial_symbols (objfile, 0);
00863 }
00864 
00865 /* Initialize entry point information for this objfile.  */
00866 
00867 static void
00868 init_entry_point_info (struct objfile *objfile)
00869 {
00870   /* Save startup file's range of PC addresses to help blockframe.c
00871      decide where the bottom of the stack is.  */
00872 
00873   if (bfd_get_file_flags (objfile->obfd) & EXEC_P)
00874     {
00875       /* Executable file -- record its entry point so we'll recognize
00876          the startup file because it contains the entry point.  */
00877       objfile->ei.entry_point = bfd_get_start_address (objfile->obfd);
00878       objfile->ei.entry_point_p = 1;
00879     }
00880   else if (bfd_get_file_flags (objfile->obfd) & DYNAMIC
00881            && bfd_get_start_address (objfile->obfd) != 0)
00882     {
00883       /* Some shared libraries may have entry points set and be
00884          runnable.  There's no clear way to indicate this, so just check
00885          for values other than zero.  */
00886       objfile->ei.entry_point = bfd_get_start_address (objfile->obfd);
00887       objfile->ei.entry_point_p = 1;
00888     }
00889   else
00890     {
00891       /* Examination of non-executable.o files.  Short-circuit this stuff.  */
00892       objfile->ei.entry_point_p = 0;
00893     }
00894 
00895   if (objfile->ei.entry_point_p)
00896     {
00897       CORE_ADDR entry_point =  objfile->ei.entry_point;
00898 
00899       /* Make certain that the address points at real code, and not a
00900          function descriptor.  */
00901       entry_point
00902         = gdbarch_convert_from_func_ptr_addr (get_objfile_arch (objfile),
00903                                               entry_point,
00904                                               &current_target);
00905 
00906       /* Remove any ISA markers, so that this matches entries in the
00907          symbol table.  */
00908       objfile->ei.entry_point
00909         = gdbarch_addr_bits_remove (get_objfile_arch (objfile), entry_point);
00910     }
00911 }
00912 
00913 /* Process a symbol file, as either the main file or as a dynamically
00914    loaded file.
00915 
00916    This function does not set the OBJFILE's entry-point info.
00917 
00918    OBJFILE is where the symbols are to be read from.
00919 
00920    ADDRS is the list of section load addresses.  If the user has given
00921    an 'add-symbol-file' command, then this is the list of offsets and
00922    addresses he or she provided as arguments to the command; or, if
00923    we're handling a shared library, these are the actual addresses the
00924    sections are loaded at, according to the inferior's dynamic linker
00925    (as gleaned by GDB's shared library code).  We convert each address
00926    into an offset from the section VMA's as it appears in the object
00927    file, and then call the file's sym_offsets function to convert this
00928    into a format-specific offset table --- a `struct section_offsets'.
00929 
00930    ADD_FLAGS encodes verbosity level, whether this is main symbol or
00931    an extra symbol file such as dynamically loaded code, and wether
00932    breakpoint reset should be deferred.  */
00933 
00934 static void
00935 syms_from_objfile_1 (struct objfile *objfile,
00936                      struct section_addr_info *addrs,
00937                      int add_flags)
00938 {
00939   struct section_addr_info *local_addr = NULL;
00940   struct cleanup *old_chain;
00941   const int mainline = add_flags & SYMFILE_MAINLINE;
00942 
00943   objfile_set_sym_fns (objfile, find_sym_fns (objfile->obfd));
00944 
00945   if (objfile->sf == NULL)
00946     {
00947       /* No symbols to load, but we still need to make sure
00948          that the section_offsets table is allocated.  */
00949       int num_sections = gdb_bfd_count_sections (objfile->obfd);
00950       size_t size = SIZEOF_N_SECTION_OFFSETS (num_sections);
00951 
00952       objfile->num_sections = num_sections;
00953       objfile->section_offsets
00954         = obstack_alloc (&objfile->objfile_obstack, size);
00955       memset (objfile->section_offsets, 0, size);
00956       return;
00957     }
00958 
00959   /* Make sure that partially constructed symbol tables will be cleaned up
00960      if an error occurs during symbol reading.  */
00961   old_chain = make_cleanup_free_objfile (objfile);
00962 
00963   /* If ADDRS is NULL, put together a dummy address list.
00964      We now establish the convention that an addr of zero means
00965      no load address was specified.  */
00966   if (! addrs)
00967     {
00968       local_addr = alloc_section_addr_info (1);
00969       make_cleanup (xfree, local_addr);
00970       addrs = local_addr;
00971     }
00972 
00973   if (mainline)
00974     {
00975       /* We will modify the main symbol table, make sure that all its users
00976          will be cleaned up if an error occurs during symbol reading.  */
00977       make_cleanup (clear_symtab_users_cleanup, 0 /*ignore*/);
00978 
00979       /* Since no error yet, throw away the old symbol table.  */
00980 
00981       if (symfile_objfile != NULL)
00982         {
00983           free_objfile (symfile_objfile);
00984           gdb_assert (symfile_objfile == NULL);
00985         }
00986 
00987       /* Currently we keep symbols from the add-symbol-file command.
00988          If the user wants to get rid of them, they should do "symbol-file"
00989          without arguments first.  Not sure this is the best behavior
00990          (PR 2207).  */
00991 
00992       (*objfile->sf->sym_new_init) (objfile);
00993     }
00994 
00995   /* Convert addr into an offset rather than an absolute address.
00996      We find the lowest address of a loaded segment in the objfile,
00997      and assume that <addr> is where that got loaded.
00998 
00999      We no longer warn if the lowest section is not a text segment (as
01000      happens for the PA64 port.  */
01001   if (addrs->num_sections > 0)
01002     addr_info_make_relative (addrs, objfile->obfd);
01003 
01004   /* Initialize symbol reading routines for this objfile, allow complaints to
01005      appear for this new file, and record how verbose to be, then do the
01006      initial symbol reading for this file.  */
01007 
01008   (*objfile->sf->sym_init) (objfile);
01009   clear_complaints (&symfile_complaints, 1, add_flags & SYMFILE_VERBOSE);
01010 
01011   (*objfile->sf->sym_offsets) (objfile, addrs);
01012 
01013   read_symbols (objfile, add_flags);
01014 
01015   /* Discard cleanups as symbol reading was successful.  */
01016 
01017   discard_cleanups (old_chain);
01018   xfree (local_addr);
01019 }
01020 
01021 /* Same as syms_from_objfile_1, but also initializes the objfile
01022    entry-point info.  */
01023 
01024 static void
01025 syms_from_objfile (struct objfile *objfile,
01026                    struct section_addr_info *addrs,
01027                    int add_flags)
01028 {
01029   syms_from_objfile_1 (objfile, addrs, add_flags);
01030   init_entry_point_info (objfile);
01031 }
01032 
01033 /* Perform required actions after either reading in the initial
01034    symbols for a new objfile, or mapping in the symbols from a reusable
01035    objfile.  ADD_FLAGS is a bitmask of enum symfile_add_flags.  */
01036 
01037 void
01038 new_symfile_objfile (struct objfile *objfile, int add_flags)
01039 {
01040   /* If this is the main symbol file we have to clean up all users of the
01041      old main symbol file.  Otherwise it is sufficient to fixup all the
01042      breakpoints that may have been redefined by this symbol file.  */
01043   if (add_flags & SYMFILE_MAINLINE)
01044     {
01045       /* OK, make it the "real" symbol file.  */
01046       symfile_objfile = objfile;
01047 
01048       clear_symtab_users (add_flags);
01049     }
01050   else if ((add_flags & SYMFILE_DEFER_BP_RESET) == 0)
01051     {
01052       breakpoint_re_set ();
01053     }
01054 
01055   /* We're done reading the symbol file; finish off complaints.  */
01056   clear_complaints (&symfile_complaints, 0, add_flags & SYMFILE_VERBOSE);
01057 }
01058 
01059 /* Process a symbol file, as either the main file or as a dynamically
01060    loaded file.
01061 
01062    ABFD is a BFD already open on the file, as from symfile_bfd_open.
01063    A new reference is acquired by this function.
01064 
01065    For NAME description see allocate_objfile's definition.
01066 
01067    ADD_FLAGS encodes verbosity, whether this is main symbol file or
01068    extra, such as dynamically loaded code, and what to do with breakpoins.
01069 
01070    ADDRS is as described for syms_from_objfile_1, above.
01071    ADDRS is ignored when SYMFILE_MAINLINE bit is set in ADD_FLAGS.
01072 
01073    PARENT is the original objfile if ABFD is a separate debug info file.
01074    Otherwise PARENT is NULL.
01075 
01076    Upon success, returns a pointer to the objfile that was added.
01077    Upon failure, jumps back to command level (never returns).  */
01078 
01079 static struct objfile *
01080 symbol_file_add_with_addrs (bfd *abfd, const char *name, int add_flags,
01081                             struct section_addr_info *addrs,
01082                             int flags, struct objfile *parent)
01083 {
01084   struct objfile *objfile;
01085   const int from_tty = add_flags & SYMFILE_VERBOSE;
01086   const int mainline = add_flags & SYMFILE_MAINLINE;
01087   const int should_print = ((from_tty || info_verbose)
01088                             && (readnow_symbol_files
01089                                 || (add_flags & SYMFILE_NO_READ) == 0));
01090 
01091   if (readnow_symbol_files)
01092     {
01093       flags |= OBJF_READNOW;
01094       add_flags &= ~SYMFILE_NO_READ;
01095     }
01096 
01097   /* Give user a chance to burp if we'd be
01098      interactively wiping out any existing symbols.  */
01099 
01100   if ((have_full_symbols () || have_partial_symbols ())
01101       && mainline
01102       && from_tty
01103       && !query (_("Load new symbol table from \"%s\"? "), name))
01104     error (_("Not confirmed."));
01105 
01106   objfile = allocate_objfile (abfd, name,
01107                               flags | (mainline ? OBJF_MAINLINE : 0));
01108 
01109   if (parent)
01110     add_separate_debug_objfile (objfile, parent);
01111 
01112   /* We either created a new mapped symbol table, mapped an existing
01113      symbol table file which has not had initial symbol reading
01114      performed, or need to read an unmapped symbol table.  */
01115   if (should_print)
01116     {
01117       if (deprecated_pre_add_symbol_hook)
01118         deprecated_pre_add_symbol_hook (name);
01119       else
01120         {
01121           printf_unfiltered (_("Reading symbols from %s..."), name);
01122           wrap_here ("");
01123           gdb_flush (gdb_stdout);
01124         }
01125     }
01126   syms_from_objfile (objfile, addrs, add_flags);
01127 
01128   /* We now have at least a partial symbol table.  Check to see if the
01129      user requested that all symbols be read on initial access via either
01130      the gdb startup command line or on a per symbol file basis.  Expand
01131      all partial symbol tables for this objfile if so.  */
01132 
01133   if ((flags & OBJF_READNOW))
01134     {
01135       if (should_print)
01136         {
01137           printf_unfiltered (_("expanding to full symbols..."));
01138           wrap_here ("");
01139           gdb_flush (gdb_stdout);
01140         }
01141 
01142       if (objfile->sf)
01143         objfile->sf->qf->expand_all_symtabs (objfile);
01144     }
01145 
01146   if (should_print && !objfile_has_symbols (objfile))
01147     {
01148       wrap_here ("");
01149       printf_unfiltered (_("(no debugging symbols found)..."));
01150       wrap_here ("");
01151     }
01152 
01153   if (should_print)
01154     {
01155       if (deprecated_post_add_symbol_hook)
01156         deprecated_post_add_symbol_hook ();
01157       else
01158         printf_unfiltered (_("done.\n"));
01159     }
01160 
01161   /* We print some messages regardless of whether 'from_tty ||
01162      info_verbose' is true, so make sure they go out at the right
01163      time.  */
01164   gdb_flush (gdb_stdout);
01165 
01166   if (objfile->sf == NULL)
01167     {
01168       observer_notify_new_objfile (objfile);
01169       return objfile;   /* No symbols.  */
01170     }
01171 
01172   new_symfile_objfile (objfile, add_flags);
01173 
01174   observer_notify_new_objfile (objfile);
01175 
01176   bfd_cache_close_all ();
01177   return (objfile);
01178 }
01179 
01180 /* Add BFD as a separate debug file for OBJFILE.  For NAME description
01181    see allocate_objfile's definition.  */
01182 
01183 void
01184 symbol_file_add_separate (bfd *bfd, const char *name, int symfile_flags,
01185                           struct objfile *objfile)
01186 {
01187   struct objfile *new_objfile;
01188   struct section_addr_info *sap;
01189   struct cleanup *my_cleanup;
01190 
01191   /* Create section_addr_info.  We can't directly use offsets from OBJFILE
01192      because sections of BFD may not match sections of OBJFILE and because
01193      vma may have been modified by tools such as prelink.  */
01194   sap = build_section_addr_info_from_objfile (objfile);
01195   my_cleanup = make_cleanup_free_section_addr_info (sap);
01196 
01197   new_objfile = symbol_file_add_with_addrs
01198     (bfd, name, symfile_flags, sap,
01199      objfile->flags & (OBJF_REORDERED | OBJF_SHARED | OBJF_READNOW
01200                        | OBJF_USERLOADED),
01201      objfile);
01202 
01203   do_cleanups (my_cleanup);
01204 }
01205 
01206 /* Process the symbol file ABFD, as either the main file or as a
01207    dynamically loaded file.
01208    See symbol_file_add_with_addrs's comments for details.  */
01209 
01210 struct objfile *
01211 symbol_file_add_from_bfd (bfd *abfd, const char *name, int add_flags,
01212                           struct section_addr_info *addrs,
01213                           int flags, struct objfile *parent)
01214 {
01215   return symbol_file_add_with_addrs (abfd, name, add_flags, addrs, flags,
01216                                      parent);
01217 }
01218 
01219 /* Process a symbol file, as either the main file or as a dynamically
01220    loaded file.  See symbol_file_add_with_addrs's comments for details.  */
01221 
01222 struct objfile *
01223 symbol_file_add (const char *name, int add_flags,
01224                  struct section_addr_info *addrs, int flags)
01225 {
01226   bfd *bfd = symfile_bfd_open (name);
01227   struct cleanup *cleanup = make_cleanup_bfd_unref (bfd);
01228   struct objfile *objf;
01229 
01230   objf = symbol_file_add_from_bfd (bfd, name, add_flags, addrs, flags, NULL);
01231   do_cleanups (cleanup);
01232   return objf;
01233 }
01234 
01235 /* Call symbol_file_add() with default values and update whatever is
01236    affected by the loading of a new main().
01237    Used when the file is supplied in the gdb command line
01238    and by some targets with special loading requirements.
01239    The auxiliary function, symbol_file_add_main_1(), has the flags
01240    argument for the switches that can only be specified in the symbol_file
01241    command itself.  */
01242 
01243 void
01244 symbol_file_add_main (const char *args, int from_tty)
01245 {
01246   symbol_file_add_main_1 (args, from_tty, 0);
01247 }
01248 
01249 static void
01250 symbol_file_add_main_1 (const char *args, int from_tty, int flags)
01251 {
01252   const int add_flags = (current_inferior ()->symfile_flags
01253                          | SYMFILE_MAINLINE | (from_tty ? SYMFILE_VERBOSE : 0));
01254 
01255   symbol_file_add (args, add_flags, NULL, flags);
01256 
01257   /* Getting new symbols may change our opinion about
01258      what is frameless.  */
01259   reinit_frame_cache ();
01260 
01261   if ((flags & SYMFILE_NO_READ) == 0)
01262     set_initial_language ();
01263 }
01264 
01265 void
01266 symbol_file_clear (int from_tty)
01267 {
01268   if ((have_full_symbols () || have_partial_symbols ())
01269       && from_tty
01270       && (symfile_objfile
01271           ? !query (_("Discard symbol table from `%s'? "),
01272                     objfile_name (symfile_objfile))
01273           : !query (_("Discard symbol table? "))))
01274     error (_("Not confirmed."));
01275 
01276   /* solib descriptors may have handles to objfiles.  Wipe them before their
01277      objfiles get stale by free_all_objfiles.  */
01278   no_shared_libraries (NULL, from_tty);
01279 
01280   free_all_objfiles ();
01281 
01282   gdb_assert (symfile_objfile == NULL);
01283   if (from_tty)
01284     printf_unfiltered (_("No symbol file now.\n"));
01285 }
01286 
01287 static int
01288 separate_debug_file_exists (const char *name, unsigned long crc,
01289                             struct objfile *parent_objfile)
01290 {
01291   unsigned long file_crc;
01292   int file_crc_p;
01293   bfd *abfd;
01294   struct stat parent_stat, abfd_stat;
01295   int verified_as_different;
01296 
01297   /* Find a separate debug info file as if symbols would be present in
01298      PARENT_OBJFILE itself this function would not be called.  .gnu_debuglink
01299      section can contain just the basename of PARENT_OBJFILE without any
01300      ".debug" suffix as "/usr/lib/debug/path/to/file" is a separate tree where
01301      the separate debug infos with the same basename can exist.  */
01302 
01303   if (filename_cmp (name, objfile_name (parent_objfile)) == 0)
01304     return 0;
01305 
01306   abfd = gdb_bfd_open_maybe_remote (name);
01307 
01308   if (!abfd)
01309     return 0;
01310 
01311   /* Verify symlinks were not the cause of filename_cmp name difference above.
01312 
01313      Some operating systems, e.g. Windows, do not provide a meaningful
01314      st_ino; they always set it to zero.  (Windows does provide a
01315      meaningful st_dev.)  Do not indicate a duplicate library in that
01316      case.  While there is no guarantee that a system that provides
01317      meaningful inode numbers will never set st_ino to zero, this is
01318      merely an optimization, so we do not need to worry about false
01319      negatives.  */
01320 
01321   if (bfd_stat (abfd, &abfd_stat) == 0
01322       && abfd_stat.st_ino != 0
01323       && bfd_stat (parent_objfile->obfd, &parent_stat) == 0)
01324     {
01325       if (abfd_stat.st_dev == parent_stat.st_dev
01326           && abfd_stat.st_ino == parent_stat.st_ino)
01327         {
01328           gdb_bfd_unref (abfd);
01329           return 0;
01330         }
01331       verified_as_different = 1;
01332     }
01333   else
01334     verified_as_different = 0;
01335 
01336   file_crc_p = gdb_bfd_crc (abfd, &file_crc);
01337 
01338   gdb_bfd_unref (abfd);
01339 
01340   if (!file_crc_p)
01341     return 0;
01342 
01343   if (crc != file_crc)
01344     {
01345       unsigned long parent_crc;
01346 
01347       /* If one (or both) the files are accessed for example the via "remote:"
01348          gdbserver way it does not support the bfd_stat operation.  Verify
01349          whether those two files are not the same manually.  */
01350 
01351       if (!verified_as_different)
01352         {
01353           if (!gdb_bfd_crc (parent_objfile->obfd, &parent_crc))
01354             return 0;
01355         }
01356 
01357       if (verified_as_different || parent_crc != file_crc)
01358         warning (_("the debug information found in \"%s\""
01359                    " does not match \"%s\" (CRC mismatch).\n"),
01360                  name, objfile_name (parent_objfile));
01361 
01362       return 0;
01363     }
01364 
01365   return 1;
01366 }
01367 
01368 char *debug_file_directory = NULL;
01369 static void
01370 show_debug_file_directory (struct ui_file *file, int from_tty,
01371                            struct cmd_list_element *c, const char *value)
01372 {
01373   fprintf_filtered (file,
01374                     _("The directory where separate debug "
01375                       "symbols are searched for is \"%s\".\n"),
01376                     value);
01377 }
01378 
01379 #if ! defined (DEBUG_SUBDIRECTORY)
01380 #define DEBUG_SUBDIRECTORY ".debug"
01381 #endif
01382 
01383 /* Find a separate debuginfo file for OBJFILE, using DIR as the directory
01384    where the original file resides (may not be the same as
01385    dirname(objfile->name) due to symlinks), and DEBUGLINK as the file we are
01386    looking for.  CANON_DIR is the "realpath" form of DIR.
01387    DIR must contain a trailing '/'.
01388    Returns the path of the file with separate debug info, of NULL.  */
01389 
01390 static char *
01391 find_separate_debug_file (const char *dir,
01392                           const char *canon_dir,
01393                           const char *debuglink,
01394                           unsigned long crc32, struct objfile *objfile)
01395 {
01396   char *debugdir;
01397   char *debugfile;
01398   int i;
01399   VEC (char_ptr) *debugdir_vec;
01400   struct cleanup *back_to;
01401   int ix;
01402 
01403   /* Set I to max (strlen (canon_dir), strlen (dir)).  */
01404   i = strlen (dir);
01405   if (canon_dir != NULL && strlen (canon_dir) > i)
01406     i = strlen (canon_dir);
01407 
01408   debugfile = xmalloc (strlen (debug_file_directory) + 1
01409                        + i
01410                        + strlen (DEBUG_SUBDIRECTORY)
01411                        + strlen ("/")
01412                        + strlen (debuglink)
01413                        + 1);
01414 
01415   /* First try in the same directory as the original file.  */
01416   strcpy (debugfile, dir);
01417   strcat (debugfile, debuglink);
01418 
01419   if (separate_debug_file_exists (debugfile, crc32, objfile))
01420     return debugfile;
01421 
01422   /* Then try in the subdirectory named DEBUG_SUBDIRECTORY.  */
01423   strcpy (debugfile, dir);
01424   strcat (debugfile, DEBUG_SUBDIRECTORY);
01425   strcat (debugfile, "/");
01426   strcat (debugfile, debuglink);
01427 
01428   if (separate_debug_file_exists (debugfile, crc32, objfile))
01429     return debugfile;
01430 
01431   /* Then try in the global debugfile directories.
01432 
01433      Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will
01434      cause "/..." lookups.  */
01435 
01436   debugdir_vec = dirnames_to_char_ptr_vec (debug_file_directory);
01437   back_to = make_cleanup_free_char_ptr_vec (debugdir_vec);
01438 
01439   for (ix = 0; VEC_iterate (char_ptr, debugdir_vec, ix, debugdir); ++ix)
01440     {
01441       strcpy (debugfile, debugdir);
01442       strcat (debugfile, "/");
01443       strcat (debugfile, dir);
01444       strcat (debugfile, debuglink);
01445 
01446       if (separate_debug_file_exists (debugfile, crc32, objfile))
01447         {
01448           do_cleanups (back_to);
01449           return debugfile;
01450         }
01451 
01452       /* If the file is in the sysroot, try using its base path in the
01453          global debugfile directory.  */
01454       if (canon_dir != NULL
01455           && filename_ncmp (canon_dir, gdb_sysroot,
01456                             strlen (gdb_sysroot)) == 0
01457           && IS_DIR_SEPARATOR (canon_dir[strlen (gdb_sysroot)]))
01458         {
01459           strcpy (debugfile, debugdir);
01460           strcat (debugfile, canon_dir + strlen (gdb_sysroot));
01461           strcat (debugfile, "/");
01462           strcat (debugfile, debuglink);
01463 
01464           if (separate_debug_file_exists (debugfile, crc32, objfile))
01465             {
01466               do_cleanups (back_to);
01467               return debugfile;
01468             }
01469         }
01470     }
01471 
01472   do_cleanups (back_to);
01473   xfree (debugfile);
01474   return NULL;
01475 }
01476 
01477 /* Modify PATH to contain only "[/]directory/" part of PATH.
01478    If there were no directory separators in PATH, PATH will be empty
01479    string on return.  */
01480 
01481 static void
01482 terminate_after_last_dir_separator (char *path)
01483 {
01484   int i;
01485 
01486   /* Strip off the final filename part, leaving the directory name,
01487      followed by a slash.  The directory can be relative or absolute.  */
01488   for (i = strlen(path) - 1; i >= 0; i--)
01489     if (IS_DIR_SEPARATOR (path[i]))
01490       break;
01491 
01492   /* If I is -1 then no directory is present there and DIR will be "".  */
01493   path[i + 1] = '\0';
01494 }
01495 
01496 /* Find separate debuginfo for OBJFILE (using .gnu_debuglink section).
01497    Returns pathname, or NULL.  */
01498 
01499 char *
01500 find_separate_debug_file_by_debuglink (struct objfile *objfile)
01501 {
01502   char *debuglink;
01503   char *dir, *canon_dir;
01504   char *debugfile;
01505   unsigned long crc32;
01506   struct cleanup *cleanups;
01507 
01508   debuglink = bfd_get_debug_link_info (objfile->obfd, &crc32);
01509 
01510   if (debuglink == NULL)
01511     {
01512       /* There's no separate debug info, hence there's no way we could
01513          load it => no warning.  */
01514       return NULL;
01515     }
01516 
01517   cleanups = make_cleanup (xfree, debuglink);
01518   dir = xstrdup (objfile_name (objfile));
01519   make_cleanup (xfree, dir);
01520   terminate_after_last_dir_separator (dir);
01521   canon_dir = lrealpath (dir);
01522 
01523   debugfile = find_separate_debug_file (dir, canon_dir, debuglink,
01524                                         crc32, objfile);
01525   xfree (canon_dir);
01526 
01527   if (debugfile == NULL)
01528     {
01529 #ifdef HAVE_LSTAT
01530       /* For PR gdb/9538, try again with realpath (if different from the
01531          original).  */
01532 
01533       struct stat st_buf;
01534 
01535       if (lstat (objfile_name (objfile), &st_buf) == 0
01536           && S_ISLNK (st_buf.st_mode))
01537         {
01538           char *symlink_dir;
01539 
01540           symlink_dir = lrealpath (objfile_name (objfile));
01541           if (symlink_dir != NULL)
01542             {
01543               make_cleanup (xfree, symlink_dir);
01544               terminate_after_last_dir_separator (symlink_dir);
01545               if (strcmp (dir, symlink_dir) != 0)
01546                 {
01547                   /* Different directory, so try using it.  */
01548                   debugfile = find_separate_debug_file (symlink_dir,
01549                                                         symlink_dir,
01550                                                         debuglink,
01551                                                         crc32,
01552                                                         objfile);
01553                 }
01554             }
01555         }
01556 #endif  /* HAVE_LSTAT  */
01557     }
01558 
01559   do_cleanups (cleanups);
01560   return debugfile;
01561 }
01562 
01563 /* This is the symbol-file command.  Read the file, analyze its
01564    symbols, and add a struct symtab to a symtab list.  The syntax of
01565    the command is rather bizarre:
01566 
01567    1. The function buildargv implements various quoting conventions
01568    which are undocumented and have little or nothing in common with
01569    the way things are quoted (or not quoted) elsewhere in GDB.
01570 
01571    2. Options are used, which are not generally used in GDB (perhaps
01572    "set mapped on", "set readnow on" would be better)
01573 
01574    3. The order of options matters, which is contrary to GNU
01575    conventions (because it is confusing and inconvenient).  */
01576 
01577 void
01578 symbol_file_command (char *args, int from_tty)
01579 {
01580   dont_repeat ();
01581 
01582   if (args == NULL)
01583     {
01584       symbol_file_clear (from_tty);
01585     }
01586   else
01587     {
01588       char **argv = gdb_buildargv (args);
01589       int flags = OBJF_USERLOADED;
01590       struct cleanup *cleanups;
01591       char *name = NULL;
01592 
01593       cleanups = make_cleanup_freeargv (argv);
01594       while (*argv != NULL)
01595         {
01596           if (strcmp (*argv, "-readnow") == 0)
01597             flags |= OBJF_READNOW;
01598           else if (**argv == '-')
01599             error (_("unknown option `%s'"), *argv);
01600           else
01601             {
01602               symbol_file_add_main_1 (*argv, from_tty, flags);
01603               name = *argv;
01604             }
01605 
01606           argv++;
01607         }
01608 
01609       if (name == NULL)
01610         error (_("no symbol file name was specified"));
01611 
01612       do_cleanups (cleanups);
01613     }
01614 }
01615 
01616 /* Set the initial language.
01617 
01618    FIXME: A better solution would be to record the language in the
01619    psymtab when reading partial symbols, and then use it (if known) to
01620    set the language.  This would be a win for formats that encode the
01621    language in an easily discoverable place, such as DWARF.  For
01622    stabs, we can jump through hoops looking for specially named
01623    symbols or try to intuit the language from the specific type of
01624    stabs we find, but we can't do that until later when we read in
01625    full symbols.  */
01626 
01627 void
01628 set_initial_language (void)
01629 {
01630   enum language lang = language_unknown;
01631 
01632   if (language_of_main != language_unknown)
01633     lang = language_of_main;
01634   else
01635     {
01636       char *name = main_name ();
01637       struct symbol *sym = lookup_symbol (name, NULL, VAR_DOMAIN, NULL);
01638 
01639       if (sym != NULL)
01640         lang = SYMBOL_LANGUAGE (sym);
01641     }
01642 
01643   if (lang == language_unknown)
01644     {
01645       /* Make C the default language */
01646       lang = language_c;
01647     }
01648 
01649   set_language (lang);
01650   expected_language = current_language; /* Don't warn the user.  */
01651 }
01652 
01653 /* If NAME is a remote name open the file using remote protocol, otherwise
01654    open it normally.  Returns a new reference to the BFD.  On error,
01655    returns NULL with the BFD error set.  */
01656 
01657 bfd *
01658 gdb_bfd_open_maybe_remote (const char *name)
01659 {
01660   bfd *result;
01661 
01662   if (remote_filename_p (name))
01663     result = remote_bfd_open (name, gnutarget);
01664   else
01665     result = gdb_bfd_open (name, gnutarget, -1);
01666 
01667   return result;
01668 }
01669 
01670 /* Open the file specified by NAME and hand it off to BFD for
01671    preliminary analysis.  Return a newly initialized bfd *, which
01672    includes a newly malloc'd` copy of NAME (tilde-expanded and made
01673    absolute).  In case of trouble, error() is called.  */
01674 
01675 bfd *
01676 symfile_bfd_open (const char *cname)
01677 {
01678   bfd *sym_bfd;
01679   int desc;
01680   char *name, *absolute_name;
01681   struct cleanup *back_to;
01682 
01683   if (remote_filename_p (cname))
01684     {
01685       sym_bfd = remote_bfd_open (cname, gnutarget);
01686       if (!sym_bfd)
01687         error (_("`%s': can't open to read symbols: %s."), cname,
01688                bfd_errmsg (bfd_get_error ()));
01689 
01690       if (!bfd_check_format (sym_bfd, bfd_object))
01691         {
01692           make_cleanup_bfd_unref (sym_bfd);
01693           error (_("`%s': can't read symbols: %s."), cname,
01694                  bfd_errmsg (bfd_get_error ()));
01695         }
01696 
01697       return sym_bfd;
01698     }
01699 
01700   name = tilde_expand (cname);  /* Returns 1st new malloc'd copy.  */
01701 
01702   /* Look down path for it, allocate 2nd new malloc'd copy.  */
01703   desc = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST | OPF_RETURN_REALPATH, name,
01704                 O_RDONLY | O_BINARY, &absolute_name);
01705 #if defined(__GO32__) || defined(_WIN32) || defined (__CYGWIN__)
01706   if (desc < 0)
01707     {
01708       char *exename = alloca (strlen (name) + 5);
01709 
01710       strcat (strcpy (exename, name), ".exe");
01711       desc = openp (getenv ("PATH"), OPF_TRY_CWD_FIRST | OPF_RETURN_REALPATH,
01712                     exename, O_RDONLY | O_BINARY, &absolute_name);
01713     }
01714 #endif
01715   if (desc < 0)
01716     {
01717       make_cleanup (xfree, name);
01718       perror_with_name (name);
01719     }
01720 
01721   xfree (name);
01722   name = absolute_name;
01723   back_to = make_cleanup (xfree, name);
01724 
01725   sym_bfd = gdb_bfd_open (name, gnutarget, desc);
01726   if (!sym_bfd)
01727     error (_("`%s': can't open to read symbols: %s."), name,
01728            bfd_errmsg (bfd_get_error ()));
01729   bfd_set_cacheable (sym_bfd, 1);
01730 
01731   if (!bfd_check_format (sym_bfd, bfd_object))
01732     {
01733       make_cleanup_bfd_unref (sym_bfd);
01734       error (_("`%s': can't read symbols: %s."), name,
01735              bfd_errmsg (bfd_get_error ()));
01736     }
01737 
01738   do_cleanups (back_to);
01739 
01740   return sym_bfd;
01741 }
01742 
01743 /* Return the section index for SECTION_NAME on OBJFILE.  Return -1 if
01744    the section was not found.  */
01745 
01746 int
01747 get_section_index (struct objfile *objfile, char *section_name)
01748 {
01749   asection *sect = bfd_get_section_by_name (objfile->obfd, section_name);
01750 
01751   if (sect)
01752     return sect->index;
01753   else
01754     return -1;
01755 }
01756 
01757 /* Link SF into the global symtab_fns list.
01758    FLAVOUR is the file format that SF handles.
01759    Called on startup by the _initialize routine in each object file format
01760    reader, to register information about each format the reader is prepared
01761    to handle.  */
01762 
01763 void
01764 add_symtab_fns (enum bfd_flavour flavour, const struct sym_fns *sf)
01765 {
01766   registered_sym_fns fns = { flavour, sf };
01767 
01768   VEC_safe_push (registered_sym_fns, symtab_fns, &fns);
01769 }
01770 
01771 /* Initialize OBJFILE to read symbols from its associated BFD.  It
01772    either returns or calls error().  The result is an initialized
01773    struct sym_fns in the objfile structure, that contains cached
01774    information about the symbol file.  */
01775 
01776 static const struct sym_fns *
01777 find_sym_fns (bfd *abfd)
01778 {
01779   registered_sym_fns *rsf;
01780   enum bfd_flavour our_flavour = bfd_get_flavour (abfd);
01781   int i;
01782 
01783   if (our_flavour == bfd_target_srec_flavour
01784       || our_flavour == bfd_target_ihex_flavour
01785       || our_flavour == bfd_target_tekhex_flavour)
01786     return NULL;        /* No symbols.  */
01787 
01788   for (i = 0; VEC_iterate (registered_sym_fns, symtab_fns, i, rsf); ++i)
01789     if (our_flavour == rsf->sym_flavour)
01790       return rsf->sym_fns;
01791 
01792   error (_("I'm sorry, Dave, I can't do that.  Symbol format `%s' unknown."),
01793          bfd_get_target (abfd));
01794 }
01795 
01796 
01797 /* This function runs the load command of our current target.  */
01798 
01799 static void
01800 load_command (char *arg, int from_tty)
01801 {
01802   struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
01803 
01804   dont_repeat ();
01805 
01806   /* The user might be reloading because the binary has changed.  Take
01807      this opportunity to check.  */
01808   reopen_exec_file ();
01809   reread_symbols ();
01810 
01811   if (arg == NULL)
01812     {
01813       char *parg;
01814       int count = 0;
01815 
01816       parg = arg = get_exec_file (1);
01817 
01818       /* Count how many \ " ' tab space there are in the name.  */
01819       while ((parg = strpbrk (parg, "\\\"'\t ")))
01820         {
01821           parg++;
01822           count++;
01823         }
01824 
01825       if (count)
01826         {
01827           /* We need to quote this string so buildargv can pull it apart.  */
01828           char *temp = xmalloc (strlen (arg) + count + 1 );
01829           char *ptemp = temp;
01830           char *prev;
01831 
01832           make_cleanup (xfree, temp);
01833 
01834           prev = parg = arg;
01835           while ((parg = strpbrk (parg, "\\\"'\t ")))
01836             {
01837               strncpy (ptemp, prev, parg - prev);
01838               ptemp += parg - prev;
01839               prev = parg++;
01840               *ptemp++ = '\\';
01841             }
01842           strcpy (ptemp, prev);
01843 
01844           arg = temp;
01845         }
01846     }
01847 
01848   target_load (arg, from_tty);
01849 
01850   /* After re-loading the executable, we don't really know which
01851      overlays are mapped any more.  */
01852   overlay_cache_invalid = 1;
01853 
01854   do_cleanups (cleanup);
01855 }
01856 
01857 /* This version of "load" should be usable for any target.  Currently
01858    it is just used for remote targets, not inftarg.c or core files,
01859    on the theory that only in that case is it useful.
01860 
01861    Avoiding xmodem and the like seems like a win (a) because we don't have
01862    to worry about finding it, and (b) On VMS, fork() is very slow and so
01863    we don't want to run a subprocess.  On the other hand, I'm not sure how
01864    performance compares.  */
01865 
01866 static int validate_download = 0;
01867 
01868 /* Callback service function for generic_load (bfd_map_over_sections).  */
01869 
01870 static void
01871 add_section_size_callback (bfd *abfd, asection *asec, void *data)
01872 {
01873   bfd_size_type *sum = data;
01874 
01875   *sum += bfd_get_section_size (asec);
01876 }
01877 
01878 /* Opaque data for load_section_callback.  */
01879 struct load_section_data {
01880   CORE_ADDR load_offset;
01881   struct load_progress_data *progress_data;
01882   VEC(memory_write_request_s) *requests;
01883 };
01884 
01885 /* Opaque data for load_progress.  */
01886 struct load_progress_data {
01887   /* Cumulative data.  */
01888   unsigned long write_count;
01889   unsigned long data_count;
01890   bfd_size_type total_size;
01891 };
01892 
01893 /* Opaque data for load_progress for a single section.  */
01894 struct load_progress_section_data {
01895   struct load_progress_data *cumulative;
01896 
01897   /* Per-section data.  */
01898   const char *section_name;
01899   ULONGEST section_sent;
01900   ULONGEST section_size;
01901   CORE_ADDR lma;
01902   gdb_byte *buffer;
01903 };
01904 
01905 /* Target write callback routine for progress reporting.  */
01906 
01907 static void
01908 load_progress (ULONGEST bytes, void *untyped_arg)
01909 {
01910   struct load_progress_section_data *args = untyped_arg;
01911   struct load_progress_data *totals;
01912 
01913   if (args == NULL)
01914     /* Writing padding data.  No easy way to get at the cumulative
01915        stats, so just ignore this.  */
01916     return;
01917 
01918   totals = args->cumulative;
01919 
01920   if (bytes == 0 && args->section_sent == 0)
01921     {
01922       /* The write is just starting.  Let the user know we've started
01923          this section.  */
01924       ui_out_message (current_uiout, 0, "Loading section %s, size %s lma %s\n",
01925                       args->section_name, hex_string (args->section_size),
01926                       paddress (target_gdbarch (), args->lma));
01927       return;
01928     }
01929 
01930   if (validate_download)
01931     {
01932       /* Broken memories and broken monitors manifest themselves here
01933          when bring new computers to life.  This doubles already slow
01934          downloads.  */
01935       /* NOTE: cagney/1999-10-18: A more efficient implementation
01936          might add a verify_memory() method to the target vector and
01937          then use that.  remote.c could implement that method using
01938          the ``qCRC'' packet.  */
01939       gdb_byte *check = xmalloc (bytes);
01940       struct cleanup *verify_cleanups = make_cleanup (xfree, check);
01941 
01942       if (target_read_memory (args->lma, check, bytes) != 0)
01943         error (_("Download verify read failed at %s"),
01944                paddress (target_gdbarch (), args->lma));
01945       if (memcmp (args->buffer, check, bytes) != 0)
01946         error (_("Download verify compare failed at %s"),
01947                paddress (target_gdbarch (), args->lma));
01948       do_cleanups (verify_cleanups);
01949     }
01950   totals->data_count += bytes;
01951   args->lma += bytes;
01952   args->buffer += bytes;
01953   totals->write_count += 1;
01954   args->section_sent += bytes;
01955   if (check_quit_flag ()
01956       || (deprecated_ui_load_progress_hook != NULL
01957           && deprecated_ui_load_progress_hook (args->section_name,
01958                                                args->section_sent)))
01959     error (_("Canceled the download"));
01960 
01961   if (deprecated_show_load_progress != NULL)
01962     deprecated_show_load_progress (args->section_name,
01963                                    args->section_sent,
01964                                    args->section_size,
01965                                    totals->data_count,
01966                                    totals->total_size);
01967 }
01968 
01969 /* Callback service function for generic_load (bfd_map_over_sections).  */
01970 
01971 static void
01972 load_section_callback (bfd *abfd, asection *asec, void *data)
01973 {
01974   struct memory_write_request *new_request;
01975   struct load_section_data *args = data;
01976   struct load_progress_section_data *section_data;
01977   bfd_size_type size = bfd_get_section_size (asec);
01978   gdb_byte *buffer;
01979   const char *sect_name = bfd_get_section_name (abfd, asec);
01980 
01981   if ((bfd_get_section_flags (abfd, asec) & SEC_LOAD) == 0)
01982     return;
01983 
01984   if (size == 0)
01985     return;
01986 
01987   new_request = VEC_safe_push (memory_write_request_s,
01988                                args->requests, NULL);
01989   memset (new_request, 0, sizeof (struct memory_write_request));
01990   section_data = xcalloc (1, sizeof (struct load_progress_section_data));
01991   new_request->begin = bfd_section_lma (abfd, asec) + args->load_offset;
01992   new_request->end = new_request->begin + size; /* FIXME Should size
01993                                                    be in instead?  */
01994   new_request->data = xmalloc (size);
01995   new_request->baton = section_data;
01996 
01997   buffer = new_request->data;
01998 
01999   section_data->cumulative = args->progress_data;
02000   section_data->section_name = sect_name;
02001   section_data->section_size = size;
02002   section_data->lma = new_request->begin;
02003   section_data->buffer = buffer;
02004 
02005   bfd_get_section_contents (abfd, asec, buffer, 0, size);
02006 }
02007 
02008 /* Clean up an entire memory request vector, including load
02009    data and progress records.  */
02010 
02011 static void
02012 clear_memory_write_data (void *arg)
02013 {
02014   VEC(memory_write_request_s) **vec_p = arg;
02015   VEC(memory_write_request_s) *vec = *vec_p;
02016   int i;
02017   struct memory_write_request *mr;
02018 
02019   for (i = 0; VEC_iterate (memory_write_request_s, vec, i, mr); ++i)
02020     {
02021       xfree (mr->data);
02022       xfree (mr->baton);
02023     }
02024   VEC_free (memory_write_request_s, vec);
02025 }
02026 
02027 void
02028 generic_load (char *args, int from_tty)
02029 {
02030   bfd *loadfile_bfd;
02031   struct timeval start_time, end_time;
02032   char *filename;
02033   struct cleanup *old_cleanups = make_cleanup (null_cleanup, 0);
02034   struct load_section_data cbdata;
02035   struct load_progress_data total_progress;
02036   struct ui_out *uiout = current_uiout;
02037 
02038   CORE_ADDR entry;
02039   char **argv;
02040 
02041   memset (&cbdata, 0, sizeof (cbdata));
02042   memset (&total_progress, 0, sizeof (total_progress));
02043   cbdata.progress_data = &total_progress;
02044 
02045   make_cleanup (clear_memory_write_data, &cbdata.requests);
02046 
02047   if (args == NULL)
02048     error_no_arg (_("file to load"));
02049 
02050   argv = gdb_buildargv (args);
02051   make_cleanup_freeargv (argv);
02052 
02053   filename = tilde_expand (argv[0]);
02054   make_cleanup (xfree, filename);
02055 
02056   if (argv[1] != NULL)
02057     {
02058       const char *endptr;
02059 
02060       cbdata.load_offset = strtoulst (argv[1], &endptr, 0);
02061 
02062       /* If the last word was not a valid number then
02063          treat it as a file name with spaces in.  */
02064       if (argv[1] == endptr)
02065         error (_("Invalid download offset:%s."), argv[1]);
02066 
02067       if (argv[2] != NULL)
02068         error (_("Too many parameters."));
02069     }
02070 
02071   /* Open the file for loading.  */
02072   loadfile_bfd = gdb_bfd_open (filename, gnutarget, -1);
02073   if (loadfile_bfd == NULL)
02074     {
02075       perror_with_name (filename);
02076       return;
02077     }
02078 
02079   make_cleanup_bfd_unref (loadfile_bfd);
02080 
02081   if (!bfd_check_format (loadfile_bfd, bfd_object))
02082     {
02083       error (_("\"%s\" is not an object file: %s"), filename,
02084              bfd_errmsg (bfd_get_error ()));
02085     }
02086 
02087   bfd_map_over_sections (loadfile_bfd, add_section_size_callback,
02088                          (void *) &total_progress.total_size);
02089 
02090   bfd_map_over_sections (loadfile_bfd, load_section_callback, &cbdata);
02091 
02092   gettimeofday (&start_time, NULL);
02093 
02094   if (target_write_memory_blocks (cbdata.requests, flash_discard,
02095                                   load_progress) != 0)
02096     error (_("Load failed"));
02097 
02098   gettimeofday (&end_time, NULL);
02099 
02100   entry = bfd_get_start_address (loadfile_bfd);
02101   entry = gdbarch_addr_bits_remove (target_gdbarch (), entry);
02102   ui_out_text (uiout, "Start address ");
02103   ui_out_field_fmt (uiout, "address", "%s", paddress (target_gdbarch (), entry));
02104   ui_out_text (uiout, ", load size ");
02105   ui_out_field_fmt (uiout, "load-size", "%lu", total_progress.data_count);
02106   ui_out_text (uiout, "\n");
02107   /* We were doing this in remote-mips.c, I suspect it is right
02108      for other targets too.  */
02109   regcache_write_pc (get_current_regcache (), entry);
02110 
02111   /* Reset breakpoints, now that we have changed the load image.  For
02112      instance, breakpoints may have been set (or reset, by
02113      post_create_inferior) while connected to the target but before we
02114      loaded the program.  In that case, the prologue analyzer could
02115      have read instructions from the target to find the right
02116      breakpoint locations.  Loading has changed the contents of that
02117      memory.  */
02118 
02119   breakpoint_re_set ();
02120 
02121   /* FIXME: are we supposed to call symbol_file_add or not?  According
02122      to a comment from remote-mips.c (where a call to symbol_file_add
02123      was commented out), making the call confuses GDB if more than one
02124      file is loaded in.  Some targets do (e.g., remote-vx.c) but
02125      others don't (or didn't - perhaps they have all been deleted).  */
02126 
02127   print_transfer_performance (gdb_stdout, total_progress.data_count,
02128                               total_progress.write_count,
02129                               &start_time, &end_time);
02130 
02131   do_cleanups (old_cleanups);
02132 }
02133 
02134 /* Report how fast the transfer went.  */
02135 
02136 void
02137 print_transfer_performance (struct ui_file *stream,
02138                             unsigned long data_count,
02139                             unsigned long write_count,
02140                             const struct timeval *start_time,
02141                             const struct timeval *end_time)
02142 {
02143   ULONGEST time_count;
02144   struct ui_out *uiout = current_uiout;
02145 
02146   /* Compute the elapsed time in milliseconds, as a tradeoff between
02147      accuracy and overflow.  */
02148   time_count = (end_time->tv_sec - start_time->tv_sec) * 1000;
02149   time_count += (end_time->tv_usec - start_time->tv_usec) / 1000;
02150 
02151   ui_out_text (uiout, "Transfer rate: ");
02152   if (time_count > 0)
02153     {
02154       unsigned long rate = ((ULONGEST) data_count * 1000) / time_count;
02155 
02156       if (ui_out_is_mi_like_p (uiout))
02157         {
02158           ui_out_field_fmt (uiout, "transfer-rate", "%lu", rate * 8);
02159           ui_out_text (uiout, " bits/sec");
02160         }
02161       else if (rate < 1024)
02162         {
02163           ui_out_field_fmt (uiout, "transfer-rate", "%lu", rate);
02164           ui_out_text (uiout, " bytes/sec");
02165         }
02166       else
02167         {
02168           ui_out_field_fmt (uiout, "transfer-rate", "%lu", rate / 1024);
02169           ui_out_text (uiout, " KB/sec");
02170         }
02171     }
02172   else
02173     {
02174       ui_out_field_fmt (uiout, "transferred-bits", "%lu", (data_count * 8));
02175       ui_out_text (uiout, " bits in <1 sec");
02176     }
02177   if (write_count > 0)
02178     {
02179       ui_out_text (uiout, ", ");
02180       ui_out_field_fmt (uiout, "write-rate", "%lu", data_count / write_count);
02181       ui_out_text (uiout, " bytes/write");
02182     }
02183   ui_out_text (uiout, ".\n");
02184 }
02185 
02186 /* This function allows the addition of incrementally linked object files.
02187    It does not modify any state in the target, only in the debugger.  */
02188 /* Note: ezannoni 2000-04-13 This function/command used to have a
02189    special case syntax for the rombug target (Rombug is the boot
02190    monitor for Microware's OS-9 / OS-9000, see remote-os9k.c). In the
02191    rombug case, the user doesn't need to supply a text address,
02192    instead a call to target_link() (in target.c) would supply the
02193    value to use.  We are now discontinuing this type of ad hoc syntax.  */
02194 
02195 static void
02196 add_symbol_file_command (char *args, int from_tty)
02197 {
02198   struct gdbarch *gdbarch = get_current_arch ();
02199   char *filename = NULL;
02200   int flags = OBJF_USERLOADED;
02201   char *arg;
02202   int section_index = 0;
02203   int argcnt = 0;
02204   int sec_num = 0;
02205   int i;
02206   int expecting_sec_name = 0;
02207   int expecting_sec_addr = 0;
02208   char **argv;
02209 
02210   struct sect_opt
02211   {
02212     char *name;
02213     char *value;
02214   };
02215 
02216   struct section_addr_info *section_addrs;
02217   struct sect_opt *sect_opts = NULL;
02218   size_t num_sect_opts = 0;
02219   struct cleanup *my_cleanups = make_cleanup (null_cleanup, NULL);
02220 
02221   num_sect_opts = 16;
02222   sect_opts = (struct sect_opt *) xmalloc (num_sect_opts
02223                                            * sizeof (struct sect_opt));
02224 
02225   dont_repeat ();
02226 
02227   if (args == NULL)
02228     error (_("add-symbol-file takes a file name and an address"));
02229 
02230   argv = gdb_buildargv (args);
02231   make_cleanup_freeargv (argv);
02232 
02233   for (arg = argv[0], argcnt = 0; arg != NULL; arg = argv[++argcnt])
02234     {
02235       /* Process the argument.  */
02236       if (argcnt == 0)
02237         {
02238           /* The first argument is the file name.  */
02239           filename = tilde_expand (arg);
02240           make_cleanup (xfree, filename);
02241         }
02242       else if (argcnt == 1)
02243         {
02244           /* The second argument is always the text address at which
02245              to load the program.  */
02246           sect_opts[section_index].name = ".text";
02247           sect_opts[section_index].value = arg;
02248           if (++section_index >= num_sect_opts)
02249             {
02250               num_sect_opts *= 2;
02251               sect_opts = ((struct sect_opt *)
02252                            xrealloc (sect_opts,
02253                                      num_sect_opts
02254                                      * sizeof (struct sect_opt)));
02255             }
02256         }
02257       else
02258         {
02259           /* It's an option (starting with '-') or it's an argument
02260              to an option.  */
02261           if (expecting_sec_name)
02262             {
02263               sect_opts[section_index].name = arg;
02264               expecting_sec_name = 0;
02265             }
02266           else if (expecting_sec_addr)
02267             {
02268               sect_opts[section_index].value = arg;
02269               expecting_sec_addr = 0;
02270               if (++section_index >= num_sect_opts)
02271                 {
02272                   num_sect_opts *= 2;
02273                   sect_opts = ((struct sect_opt *)
02274                                xrealloc (sect_opts,
02275                                          num_sect_opts
02276                                          * sizeof (struct sect_opt)));
02277                 }
02278             }
02279           else if (strcmp (arg, "-readnow") == 0)
02280             flags |= OBJF_READNOW;
02281           else if (strcmp (arg, "-s") == 0)
02282             {
02283               expecting_sec_name = 1;
02284               expecting_sec_addr = 1;
02285             }
02286           else
02287             error (_("USAGE: add-symbol-file <filename> <textaddress>"
02288                      " [-readnow] [-s <secname> <addr>]*"));
02289         }
02290     }
02291 
02292   /* This command takes at least two arguments.  The first one is a
02293      filename, and the second is the address where this file has been
02294      loaded.  Abort now if this address hasn't been provided by the
02295      user.  */
02296   if (section_index < 1)
02297     error (_("The address where %s has been loaded is missing"), filename);
02298 
02299   /* Print the prompt for the query below.  And save the arguments into
02300      a sect_addr_info structure to be passed around to other
02301      functions.  We have to split this up into separate print
02302      statements because hex_string returns a local static
02303      string.  */
02304 
02305   printf_unfiltered (_("add symbol table from file \"%s\" at\n"), filename);
02306   section_addrs = alloc_section_addr_info (section_index);
02307   make_cleanup (xfree, section_addrs);
02308   for (i = 0; i < section_index; i++)
02309     {
02310       CORE_ADDR addr;
02311       char *val = sect_opts[i].value;
02312       char *sec = sect_opts[i].name;
02313 
02314       addr = parse_and_eval_address (val);
02315 
02316       /* Here we store the section offsets in the order they were
02317          entered on the command line.  */
02318       section_addrs->other[sec_num].name = sec;
02319       section_addrs->other[sec_num].addr = addr;
02320       printf_unfiltered ("\t%s_addr = %s\n", sec,
02321                          paddress (gdbarch, addr));
02322       sec_num++;
02323 
02324       /* The object's sections are initialized when a
02325          call is made to build_objfile_section_table (objfile).
02326          This happens in reread_symbols.
02327          At this point, we don't know what file type this is,
02328          so we can't determine what section names are valid.  */
02329     }
02330   section_addrs->num_sections = sec_num;
02331 
02332   if (from_tty && (!query ("%s", "")))
02333     error (_("Not confirmed."));
02334 
02335   symbol_file_add (filename, from_tty ? SYMFILE_VERBOSE : 0,
02336                    section_addrs, flags);
02337 
02338   /* Getting new symbols may change our opinion about what is
02339      frameless.  */
02340   reinit_frame_cache ();
02341   do_cleanups (my_cleanups);
02342 }
02343 
02344 
02345 typedef struct objfile *objfilep;
02346 
02347 DEF_VEC_P (objfilep);
02348 
02349 /* Re-read symbols if a symbol-file has changed.  */
02350 
02351 void
02352 reread_symbols (void)
02353 {
02354   struct objfile *objfile;
02355   long new_modtime;
02356   struct stat new_statbuf;
02357   int res;
02358   VEC (objfilep) *new_objfiles = NULL;
02359   struct cleanup *all_cleanups;
02360 
02361   all_cleanups = make_cleanup (VEC_cleanup (objfilep), &new_objfiles);
02362 
02363   /* With the addition of shared libraries, this should be modified,
02364      the load time should be saved in the partial symbol tables, since
02365      different tables may come from different source files.  FIXME.
02366      This routine should then walk down each partial symbol table
02367      and see if the symbol table that it originates from has been changed.  */
02368 
02369   for (objfile = object_files; objfile; objfile = objfile->next)
02370     {
02371       if (objfile->obfd == NULL)
02372         continue;
02373 
02374       /* Separate debug objfiles are handled in the main objfile.  */
02375       if (objfile->separate_debug_objfile_backlink)
02376         continue;
02377 
02378       /* If this object is from an archive (what you usually create with
02379          `ar', often called a `static library' on most systems, though
02380          a `shared library' on AIX is also an archive), then you should
02381          stat on the archive name, not member name.  */
02382       if (objfile->obfd->my_archive)
02383         res = stat (objfile->obfd->my_archive->filename, &new_statbuf);
02384       else
02385         res = stat (objfile_name (objfile), &new_statbuf);
02386       if (res != 0)
02387         {
02388           /* FIXME, should use print_sys_errmsg but it's not filtered.  */
02389           printf_unfiltered (_("`%s' has disappeared; keeping its symbols.\n"),
02390                              objfile_name (objfile));
02391           continue;
02392         }
02393       new_modtime = new_statbuf.st_mtime;
02394       if (new_modtime != objfile->mtime)
02395         {
02396           struct cleanup *old_cleanups;
02397           struct section_offsets *offsets;
02398           int num_offsets;
02399           char *original_name;
02400 
02401           printf_unfiltered (_("`%s' has changed; re-reading symbols.\n"),
02402                              objfile_name (objfile));
02403 
02404           /* There are various functions like symbol_file_add,
02405              symfile_bfd_open, syms_from_objfile, etc., which might
02406              appear to do what we want.  But they have various other
02407              effects which we *don't* want.  So we just do stuff
02408              ourselves.  We don't worry about mapped files (for one thing,
02409              any mapped file will be out of date).  */
02410 
02411           /* If we get an error, blow away this objfile (not sure if
02412              that is the correct response for things like shared
02413              libraries).  */
02414           old_cleanups = make_cleanup_free_objfile (objfile);
02415           /* We need to do this whenever any symbols go away.  */
02416           make_cleanup (clear_symtab_users_cleanup, 0 /*ignore*/);
02417 
02418           if (exec_bfd != NULL
02419               && filename_cmp (bfd_get_filename (objfile->obfd),
02420                                bfd_get_filename (exec_bfd)) == 0)
02421             {
02422               /* Reload EXEC_BFD without asking anything.  */
02423 
02424               exec_file_attach (bfd_get_filename (objfile->obfd), 0);
02425             }
02426 
02427           /* Keep the calls order approx. the same as in free_objfile.  */
02428 
02429           /* Free the separate debug objfiles.  It will be
02430              automatically recreated by sym_read.  */
02431           free_objfile_separate_debug (objfile);
02432 
02433           /* Remove any references to this objfile in the global
02434              value lists.  */
02435           preserve_values (objfile);
02436 
02437           /* Nuke all the state that we will re-read.  Much of the following
02438              code which sets things to NULL really is necessary to tell
02439              other parts of GDB that there is nothing currently there.
02440 
02441              Try to keep the freeing order compatible with free_objfile.  */
02442 
02443           if (objfile->sf != NULL)
02444             {
02445               (*objfile->sf->sym_finish) (objfile);
02446             }
02447 
02448           clear_objfile_data (objfile);
02449 
02450           /* Clean up any state BFD has sitting around.  */
02451           {
02452             struct bfd *obfd = objfile->obfd;
02453             char *obfd_filename;
02454 
02455             obfd_filename = bfd_get_filename (objfile->obfd);
02456             /* Open the new BFD before freeing the old one, so that
02457                the filename remains live.  */
02458             objfile->obfd = gdb_bfd_open_maybe_remote (obfd_filename);
02459             if (objfile->obfd == NULL)
02460               {
02461                 /* We have to make a cleanup and error here, rather
02462                    than erroring later, because once we unref OBFD,
02463                    OBFD_FILENAME will be freed.  */
02464                 make_cleanup_bfd_unref (obfd);
02465                 error (_("Can't open %s to read symbols."), obfd_filename);
02466               }
02467             gdb_bfd_unref (obfd);
02468           }
02469 
02470           original_name = xstrdup (objfile->original_name);
02471           make_cleanup (xfree, original_name);
02472 
02473           /* bfd_openr sets cacheable to true, which is what we want.  */
02474           if (!bfd_check_format (objfile->obfd, bfd_object))
02475             error (_("Can't read symbols from %s: %s."), objfile_name (objfile),
02476                    bfd_errmsg (bfd_get_error ()));
02477 
02478           /* Save the offsets, we will nuke them with the rest of the
02479              objfile_obstack.  */
02480           num_offsets = objfile->num_sections;
02481           offsets = ((struct section_offsets *)
02482                      alloca (SIZEOF_N_SECTION_OFFSETS (num_offsets)));
02483           memcpy (offsets, objfile->section_offsets,
02484                   SIZEOF_N_SECTION_OFFSETS (num_offsets));
02485 
02486           /* FIXME: Do we have to free a whole linked list, or is this
02487              enough?  */
02488           if (objfile->global_psymbols.list)
02489             xfree (objfile->global_psymbols.list);
02490           memset (&objfile->global_psymbols, 0,
02491                   sizeof (objfile->global_psymbols));
02492           if (objfile->static_psymbols.list)
02493             xfree (objfile->static_psymbols.list);
02494           memset (&objfile->static_psymbols, 0,
02495                   sizeof (objfile->static_psymbols));
02496 
02497           /* Free the obstacks for non-reusable objfiles.  */
02498           psymbol_bcache_free (objfile->psymbol_cache);
02499           objfile->psymbol_cache = psymbol_bcache_init ();
02500           obstack_free (&objfile->objfile_obstack, 0);
02501           objfile->sections = NULL;
02502           objfile->symtabs = NULL;
02503           objfile->psymtabs = NULL;
02504           objfile->psymtabs_addrmap = NULL;
02505           objfile->free_psymtabs = NULL;
02506           objfile->template_symbols = NULL;
02507           objfile->msymbols = NULL;
02508           objfile->minimal_symbol_count = 0;
02509           memset (&objfile->msymbol_hash, 0,
02510                   sizeof (objfile->msymbol_hash));
02511           memset (&objfile->msymbol_demangled_hash, 0,
02512                   sizeof (objfile->msymbol_demangled_hash));
02513 
02514           set_objfile_per_bfd (objfile);
02515 
02516           /* obstack_init also initializes the obstack so it is
02517              empty.  We could use obstack_specify_allocation but
02518              gdb_obstack.h specifies the alloc/dealloc functions.  */
02519           obstack_init (&objfile->objfile_obstack);
02520 
02521           objfile->original_name = obstack_copy0 (&objfile->objfile_obstack,
02522                                                   original_name,
02523                                                   strlen (original_name));
02524 
02525           /* Reset the sym_fns pointer.  The ELF reader can change it
02526              based on whether .gdb_index is present, and we need it to
02527              start over.  PR symtab/15885  */
02528           objfile_set_sym_fns (objfile, find_sym_fns (objfile->obfd));
02529 
02530           build_objfile_section_table (objfile);
02531           terminate_minimal_symbol_table (objfile);
02532 
02533           /* We use the same section offsets as from last time.  I'm not
02534              sure whether that is always correct for shared libraries.  */
02535           objfile->section_offsets = (struct section_offsets *)
02536             obstack_alloc (&objfile->objfile_obstack,
02537                            SIZEOF_N_SECTION_OFFSETS (num_offsets));
02538           memcpy (objfile->section_offsets, offsets,
02539                   SIZEOF_N_SECTION_OFFSETS (num_offsets));
02540           objfile->num_sections = num_offsets;
02541 
02542           /* What the hell is sym_new_init for, anyway?  The concept of
02543              distinguishing between the main file and additional files
02544              in this way seems rather dubious.  */
02545           if (objfile == symfile_objfile)
02546             {
02547               (*objfile->sf->sym_new_init) (objfile);
02548             }
02549 
02550           (*objfile->sf->sym_init) (objfile);
02551           clear_complaints (&symfile_complaints, 1, 1);
02552 
02553           objfile->flags &= ~OBJF_PSYMTABS_READ;
02554           read_symbols (objfile, 0);
02555 
02556           if (!objfile_has_symbols (objfile))
02557             {
02558               wrap_here ("");
02559               printf_unfiltered (_("(no debugging symbols found)\n"));
02560               wrap_here ("");
02561             }
02562 
02563           /* We're done reading the symbol file; finish off complaints.  */
02564           clear_complaints (&symfile_complaints, 0, 1);
02565 
02566           /* Getting new symbols may change our opinion about what is
02567              frameless.  */
02568 
02569           reinit_frame_cache ();
02570 
02571           /* Discard cleanups as symbol reading was successful.  */
02572           discard_cleanups (old_cleanups);
02573 
02574           /* If the mtime has changed between the time we set new_modtime
02575              and now, we *want* this to be out of date, so don't call stat
02576              again now.  */
02577           objfile->mtime = new_modtime;
02578           init_entry_point_info (objfile);
02579 
02580           VEC_safe_push (objfilep, new_objfiles, objfile);
02581         }
02582     }
02583 
02584   if (new_objfiles)
02585     {
02586       int ix;
02587 
02588       /* Notify objfiles that we've modified objfile sections.  */
02589       objfiles_changed ();
02590 
02591       clear_symtab_users (0);
02592 
02593       /* clear_objfile_data for each objfile was called before freeing it and
02594          observer_notify_new_objfile (NULL) has been called by
02595          clear_symtab_users above.  Notify the new files now.  */
02596       for (ix = 0; VEC_iterate (objfilep, new_objfiles, ix, objfile); ix++)
02597         observer_notify_new_objfile (objfile);
02598 
02599       /* At least one objfile has changed, so we can consider that
02600          the executable we're debugging has changed too.  */
02601       observer_notify_executable_changed ();
02602     }
02603 
02604   do_cleanups (all_cleanups);
02605 }
02606 
02607 
02608 typedef struct
02609 {
02610   char *ext;
02611   enum language lang;
02612 }
02613 filename_language;
02614 
02615 static filename_language *filename_language_table;
02616 static int fl_table_size, fl_table_next;
02617 
02618 static void
02619 add_filename_language (char *ext, enum language lang)
02620 {
02621   if (fl_table_next >= fl_table_size)
02622     {
02623       fl_table_size += 10;
02624       filename_language_table =
02625         xrealloc (filename_language_table,
02626                   fl_table_size * sizeof (*filename_language_table));
02627     }
02628 
02629   filename_language_table[fl_table_next].ext = xstrdup (ext);
02630   filename_language_table[fl_table_next].lang = lang;
02631   fl_table_next++;
02632 }
02633 
02634 static char *ext_args;
02635 static void
02636 show_ext_args (struct ui_file *file, int from_tty,
02637                struct cmd_list_element *c, const char *value)
02638 {
02639   fprintf_filtered (file,
02640                     _("Mapping between filename extension "
02641                       "and source language is \"%s\".\n"),
02642                     value);
02643 }
02644 
02645 static void
02646 set_ext_lang_command (char *args, int from_tty, struct cmd_list_element *e)
02647 {
02648   int i;
02649   char *cp = ext_args;
02650   enum language lang;
02651 
02652   /* First arg is filename extension, starting with '.'  */
02653   if (*cp != '.')
02654     error (_("'%s': Filename extension must begin with '.'"), ext_args);
02655 
02656   /* Find end of first arg.  */
02657   while (*cp && !isspace (*cp))
02658     cp++;
02659 
02660   if (*cp == '\0')
02661     error (_("'%s': two arguments required -- "
02662              "filename extension and language"),
02663            ext_args);
02664 
02665   /* Null-terminate first arg.  */
02666   *cp++ = '\0';
02667 
02668   /* Find beginning of second arg, which should be a source language.  */
02669   cp = skip_spaces (cp);
02670 
02671   if (*cp == '\0')
02672     error (_("'%s': two arguments required -- "
02673              "filename extension and language"),
02674            ext_args);
02675 
02676   /* Lookup the language from among those we know.  */
02677   lang = language_enum (cp);
02678 
02679   /* Now lookup the filename extension: do we already know it?  */
02680   for (i = 0; i < fl_table_next; i++)
02681     if (0 == strcmp (ext_args, filename_language_table[i].ext))
02682       break;
02683 
02684   if (i >= fl_table_next)
02685     {
02686       /* New file extension.  */
02687       add_filename_language (ext_args, lang);
02688     }
02689   else
02690     {
02691       /* Redefining a previously known filename extension.  */
02692 
02693       /* if (from_tty) */
02694       /*   query ("Really make files of type %s '%s'?", */
02695       /*          ext_args, language_str (lang));           */
02696 
02697       xfree (filename_language_table[i].ext);
02698       filename_language_table[i].ext = xstrdup (ext_args);
02699       filename_language_table[i].lang = lang;
02700     }
02701 }
02702 
02703 static void
02704 info_ext_lang_command (char *args, int from_tty)
02705 {
02706   int i;
02707 
02708   printf_filtered (_("Filename extensions and the languages they represent:"));
02709   printf_filtered ("\n\n");
02710   for (i = 0; i < fl_table_next; i++)
02711     printf_filtered ("\t%s\t- %s\n",
02712                      filename_language_table[i].ext,
02713                      language_str (filename_language_table[i].lang));
02714 }
02715 
02716 static void
02717 init_filename_language_table (void)
02718 {
02719   if (fl_table_size == 0)       /* Protect against repetition.  */
02720     {
02721       fl_table_size = 20;
02722       fl_table_next = 0;
02723       filename_language_table =
02724         xmalloc (fl_table_size * sizeof (*filename_language_table));
02725       add_filename_language (".c", language_c);
02726       add_filename_language (".d", language_d);
02727       add_filename_language (".C", language_cplus);
02728       add_filename_language (".cc", language_cplus);
02729       add_filename_language (".cp", language_cplus);
02730       add_filename_language (".cpp", language_cplus);
02731       add_filename_language (".cxx", language_cplus);
02732       add_filename_language (".c++", language_cplus);
02733       add_filename_language (".java", language_java);
02734       add_filename_language (".class", language_java);
02735       add_filename_language (".m", language_objc);
02736       add_filename_language (".f", language_fortran);
02737       add_filename_language (".F", language_fortran);
02738       add_filename_language (".for", language_fortran);
02739       add_filename_language (".FOR", language_fortran);
02740       add_filename_language (".ftn", language_fortran);
02741       add_filename_language (".FTN", language_fortran);
02742       add_filename_language (".fpp", language_fortran);
02743       add_filename_language (".FPP", language_fortran);
02744       add_filename_language (".f90", language_fortran);
02745       add_filename_language (".F90", language_fortran);
02746       add_filename_language (".f95", language_fortran);
02747       add_filename_language (".F95", language_fortran);
02748       add_filename_language (".f03", language_fortran);
02749       add_filename_language (".F03", language_fortran);
02750       add_filename_language (".f08", language_fortran);
02751       add_filename_language (".F08", language_fortran);
02752       add_filename_language (".s", language_asm);
02753       add_filename_language (".sx", language_asm);
02754       add_filename_language (".S", language_asm);
02755       add_filename_language (".pas", language_pascal);
02756       add_filename_language (".p", language_pascal);
02757       add_filename_language (".pp", language_pascal);
02758       add_filename_language (".adb", language_ada);
02759       add_filename_language (".ads", language_ada);
02760       add_filename_language (".a", language_ada);
02761       add_filename_language (".ada", language_ada);
02762       add_filename_language (".dg", language_ada);
02763     }
02764 }
02765 
02766 enum language
02767 deduce_language_from_filename (const char *filename)
02768 {
02769   int i;
02770   char *cp;
02771 
02772   if (filename != NULL)
02773     if ((cp = strrchr (filename, '.')) != NULL)
02774       for (i = 0; i < fl_table_next; i++)
02775         if (strcmp (cp, filename_language_table[i].ext) == 0)
02776           return filename_language_table[i].lang;
02777 
02778   return language_unknown;
02779 }
02780 
02781 /* allocate_symtab:
02782 
02783    Allocate and partly initialize a new symbol table.  Return a pointer
02784    to it.  error() if no space.
02785 
02786    Caller must set these fields:
02787    LINETABLE(symtab)
02788    symtab->blockvector
02789    symtab->dirname
02790    symtab->free_code
02791    symtab->free_ptr
02792  */
02793 
02794 struct symtab *
02795 allocate_symtab (const char *filename, struct objfile *objfile)
02796 {
02797   struct symtab *symtab;
02798 
02799   symtab = (struct symtab *)
02800     obstack_alloc (&objfile->objfile_obstack, sizeof (struct symtab));
02801   memset (symtab, 0, sizeof (*symtab));
02802   symtab->filename = (char *) bcache (filename, strlen (filename) + 1,
02803                                       objfile->per_bfd->filename_cache);
02804   symtab->fullname = NULL;
02805   symtab->language = deduce_language_from_filename (filename);
02806   symtab->debugformat = "unknown";
02807 
02808   /* Hook it to the objfile it comes from.  */
02809 
02810   symtab->objfile = objfile;
02811   symtab->next = objfile->symtabs;
02812   objfile->symtabs = symtab;
02813 
02814   if (symtab_create_debug)
02815     {
02816       /* Be a bit clever with debugging messages, and don't print objfile
02817          every time, only when it changes.  */
02818       static char *last_objfile_name = NULL;
02819 
02820       if (last_objfile_name == NULL
02821           || strcmp (last_objfile_name, objfile_name (objfile)) != 0)
02822         {
02823           xfree (last_objfile_name);
02824           last_objfile_name = xstrdup (objfile_name (objfile));
02825           fprintf_unfiltered (gdb_stdlog,
02826                               "Creating one or more symtabs for objfile %s ...\n",
02827                               last_objfile_name);
02828         }
02829       fprintf_unfiltered (gdb_stdlog,
02830                           "Created symtab %s for module %s.\n",
02831                           host_address_to_string (symtab), filename);
02832     }
02833 
02834   return (symtab);
02835 }
02836 
02837 
02838 /* Reset all data structures in gdb which may contain references to symbol
02839    table data.  ADD_FLAGS is a bitmask of enum symfile_add_flags.  */
02840 
02841 void
02842 clear_symtab_users (int add_flags)
02843 {
02844   /* Someday, we should do better than this, by only blowing away
02845      the things that really need to be blown.  */
02846 
02847   /* Clear the "current" symtab first, because it is no longer valid.
02848      breakpoint_re_set may try to access the current symtab.  */
02849   clear_current_source_symtab_and_line ();
02850 
02851   clear_displays ();
02852   if ((add_flags & SYMFILE_DEFER_BP_RESET) == 0)
02853     breakpoint_re_set ();
02854   clear_last_displayed_sal ();
02855   clear_pc_function_cache ();
02856   observer_notify_new_objfile (NULL);
02857 
02858   /* Clear globals which might have pointed into a removed objfile.
02859      FIXME: It's not clear which of these are supposed to persist
02860      between expressions and which ought to be reset each time.  */
02861   expression_context_block = NULL;
02862   innermost_block = NULL;
02863 
02864   /* Varobj may refer to old symbols, perform a cleanup.  */
02865   varobj_invalidate ();
02866 
02867 }
02868 
02869 static void
02870 clear_symtab_users_cleanup (void *ignore)
02871 {
02872   clear_symtab_users (0);
02873 }
02874 
02875 /* OVERLAYS:
02876    The following code implements an abstraction for debugging overlay sections.
02877 
02878    The target model is as follows:
02879    1) The gnu linker will permit multiple sections to be mapped into the
02880    same VMA, each with its own unique LMA (or load address).
02881    2) It is assumed that some runtime mechanism exists for mapping the
02882    sections, one by one, from the load address into the VMA address.
02883    3) This code provides a mechanism for gdb to keep track of which
02884    sections should be considered to be mapped from the VMA to the LMA.
02885    This information is used for symbol lookup, and memory read/write.
02886    For instance, if a section has been mapped then its contents
02887    should be read from the VMA, otherwise from the LMA.
02888 
02889    Two levels of debugger support for overlays are available.  One is
02890    "manual", in which the debugger relies on the user to tell it which
02891    overlays are currently mapped.  This level of support is
02892    implemented entirely in the core debugger, and the information about
02893    whether a section is mapped is kept in the objfile->obj_section table.
02894 
02895    The second level of support is "automatic", and is only available if
02896    the target-specific code provides functionality to read the target's
02897    overlay mapping table, and translate its contents for the debugger
02898    (by updating the mapped state information in the obj_section tables).
02899 
02900    The interface is as follows:
02901    User commands:
02902    overlay map <name>   -- tell gdb to consider this section mapped
02903    overlay unmap <name> -- tell gdb to consider this section unmapped
02904    overlay list         -- list the sections that GDB thinks are mapped
02905    overlay read-target  -- get the target's state of what's mapped
02906    overlay off/manual/auto -- set overlay debugging state
02907    Functional interface:
02908    find_pc_mapped_section(pc):    if the pc is in the range of a mapped
02909    section, return that section.
02910    find_pc_overlay(pc):       find any overlay section that contains
02911    the pc, either in its VMA or its LMA
02912    section_is_mapped(sect):       true if overlay is marked as mapped
02913    section_is_overlay(sect):      true if section's VMA != LMA
02914    pc_in_mapped_range(pc,sec):    true if pc belongs to section's VMA
02915    pc_in_unmapped_range(...):     true if pc belongs to section's LMA
02916    sections_overlap(sec1, sec2):  true if mapped sec1 and sec2 ranges overlap
02917    overlay_mapped_address(...):   map an address from section's LMA to VMA
02918    overlay_unmapped_address(...): map an address from section's VMA to LMA
02919    symbol_overlayed_address(...): Return a "current" address for symbol:
02920    either in VMA or LMA depending on whether
02921    the symbol's section is currently mapped.  */
02922 
02923 /* Overlay debugging state: */
02924 
02925 enum overlay_debugging_state overlay_debugging = ovly_off;
02926 int overlay_cache_invalid = 0;  /* True if need to refresh mapped state.  */
02927 
02928 /* Function: section_is_overlay (SECTION)
02929    Returns true if SECTION has VMA not equal to LMA, ie.
02930    SECTION is loaded at an address different from where it will "run".  */
02931 
02932 int
02933 section_is_overlay (struct obj_section *section)
02934 {
02935   if (overlay_debugging && section)
02936     {
02937       bfd *abfd = section->objfile->obfd;
02938       asection *bfd_section = section->the_bfd_section;
02939 
02940       if (bfd_section_lma (abfd, bfd_section) != 0
02941           && bfd_section_lma (abfd, bfd_section)
02942              != bfd_section_vma (abfd, bfd_section))
02943         return 1;
02944     }
02945 
02946   return 0;
02947 }
02948 
02949 /* Function: overlay_invalidate_all (void)
02950    Invalidate the mapped state of all overlay sections (mark it as stale).  */
02951 
02952 static void
02953 overlay_invalidate_all (void)
02954 {
02955   struct objfile *objfile;
02956   struct obj_section *sect;
02957 
02958   ALL_OBJSECTIONS (objfile, sect)
02959     if (section_is_overlay (sect))
02960       sect->ovly_mapped = -1;
02961 }
02962 
02963 /* Function: section_is_mapped (SECTION)
02964    Returns true if section is an overlay, and is currently mapped.
02965 
02966    Access to the ovly_mapped flag is restricted to this function, so
02967    that we can do automatic update.  If the global flag
02968    OVERLAY_CACHE_INVALID is set (by wait_for_inferior), then call
02969    overlay_invalidate_all.  If the mapped state of the particular
02970    section is stale, then call TARGET_OVERLAY_UPDATE to refresh it.  */
02971 
02972 int
02973 section_is_mapped (struct obj_section *osect)
02974 {
02975   struct gdbarch *gdbarch;
02976 
02977   if (osect == 0 || !section_is_overlay (osect))
02978     return 0;
02979 
02980   switch (overlay_debugging)
02981     {
02982     default:
02983     case ovly_off:
02984       return 0;                 /* overlay debugging off */
02985     case ovly_auto:             /* overlay debugging automatic */
02986       /* Unles there is a gdbarch_overlay_update function,
02987          there's really nothing useful to do here (can't really go auto).  */
02988       gdbarch = get_objfile_arch (osect->objfile);
02989       if (gdbarch_overlay_update_p (gdbarch))
02990         {
02991           if (overlay_cache_invalid)
02992             {
02993               overlay_invalidate_all ();
02994               overlay_cache_invalid = 0;
02995             }
02996           if (osect->ovly_mapped == -1)
02997             gdbarch_overlay_update (gdbarch, osect);
02998         }
02999       /* fall thru to manual case */
03000     case ovly_on:               /* overlay debugging manual */
03001       return osect->ovly_mapped == 1;
03002     }
03003 }
03004 
03005 /* Function: pc_in_unmapped_range
03006    If PC falls into the lma range of SECTION, return true, else false.  */
03007 
03008 CORE_ADDR
03009 pc_in_unmapped_range (CORE_ADDR pc, struct obj_section *section)
03010 {
03011   if (section_is_overlay (section))
03012     {
03013       bfd *abfd = section->objfile->obfd;
03014       asection *bfd_section = section->the_bfd_section;
03015 
03016       /* We assume the LMA is relocated by the same offset as the VMA.  */
03017       bfd_vma size = bfd_get_section_size (bfd_section);
03018       CORE_ADDR offset = obj_section_offset (section);
03019 
03020       if (bfd_get_section_lma (abfd, bfd_section) + offset <= pc
03021           && pc < bfd_get_section_lma (abfd, bfd_section) + offset + size)
03022         return 1;
03023     }
03024 
03025   return 0;
03026 }
03027 
03028 /* Function: pc_in_mapped_range
03029    If PC falls into the vma range of SECTION, return true, else false.  */
03030 
03031 CORE_ADDR
03032 pc_in_mapped_range (CORE_ADDR pc, struct obj_section *section)
03033 {
03034   if (section_is_overlay (section))
03035     {
03036       if (obj_section_addr (section) <= pc
03037           && pc < obj_section_endaddr (section))
03038         return 1;
03039     }
03040 
03041   return 0;
03042 }
03043 
03044 /* Return true if the mapped ranges of sections A and B overlap, false
03045    otherwise.  */
03046 
03047 static int
03048 sections_overlap (struct obj_section *a, struct obj_section *b)
03049 {
03050   CORE_ADDR a_start = obj_section_addr (a);
03051   CORE_ADDR a_end = obj_section_endaddr (a);
03052   CORE_ADDR b_start = obj_section_addr (b);
03053   CORE_ADDR b_end = obj_section_endaddr (b);
03054 
03055   return (a_start < b_end && b_start < a_end);
03056 }
03057 
03058 /* Function: overlay_unmapped_address (PC, SECTION)
03059    Returns the address corresponding to PC in the unmapped (load) range.
03060    May be the same as PC.  */
03061 
03062 CORE_ADDR
03063 overlay_unmapped_address (CORE_ADDR pc, struct obj_section *section)
03064 {
03065   if (section_is_overlay (section) && pc_in_mapped_range (pc, section))
03066     {
03067       bfd *abfd = section->objfile->obfd;
03068       asection *bfd_section = section->the_bfd_section;
03069 
03070       return pc + bfd_section_lma (abfd, bfd_section)
03071                 - bfd_section_vma (abfd, bfd_section);
03072     }
03073 
03074   return pc;
03075 }
03076 
03077 /* Function: overlay_mapped_address (PC, SECTION)
03078    Returns the address corresponding to PC in the mapped (runtime) range.
03079    May be the same as PC.  */
03080 
03081 CORE_ADDR
03082 overlay_mapped_address (CORE_ADDR pc, struct obj_section *section)
03083 {
03084   if (section_is_overlay (section) && pc_in_unmapped_range (pc, section))
03085     {
03086       bfd *abfd = section->objfile->obfd;
03087       asection *bfd_section = section->the_bfd_section;
03088 
03089       return pc + bfd_section_vma (abfd, bfd_section)
03090                 - bfd_section_lma (abfd, bfd_section);
03091     }
03092 
03093   return pc;
03094 }
03095 
03096 /* Function: symbol_overlayed_address
03097    Return one of two addresses (relative to the VMA or to the LMA),
03098    depending on whether the section is mapped or not.  */
03099 
03100 CORE_ADDR
03101 symbol_overlayed_address (CORE_ADDR address, struct obj_section *section)
03102 {
03103   if (overlay_debugging)
03104     {
03105       /* If the symbol has no section, just return its regular address.  */
03106       if (section == 0)
03107         return address;
03108       /* If the symbol's section is not an overlay, just return its
03109          address.  */
03110       if (!section_is_overlay (section))
03111         return address;
03112       /* If the symbol's section is mapped, just return its address.  */
03113       if (section_is_mapped (section))
03114         return address;
03115       /*
03116        * HOWEVER: if the symbol is in an overlay section which is NOT mapped,
03117        * then return its LOADED address rather than its vma address!!
03118        */
03119       return overlay_unmapped_address (address, section);
03120     }
03121   return address;
03122 }
03123 
03124 /* Function: find_pc_overlay (PC)
03125    Return the best-match overlay section for PC:
03126    If PC matches a mapped overlay section's VMA, return that section.
03127    Else if PC matches an unmapped section's VMA, return that section.
03128    Else if PC matches an unmapped section's LMA, return that section.  */
03129 
03130 struct obj_section *
03131 find_pc_overlay (CORE_ADDR pc)
03132 {
03133   struct objfile *objfile;
03134   struct obj_section *osect, *best_match = NULL;
03135 
03136   if (overlay_debugging)
03137     ALL_OBJSECTIONS (objfile, osect)
03138       if (section_is_overlay (osect))
03139       {
03140         if (pc_in_mapped_range (pc, osect))
03141           {
03142             if (section_is_mapped (osect))
03143               return osect;
03144             else
03145               best_match = osect;
03146           }
03147         else if (pc_in_unmapped_range (pc, osect))
03148           best_match = osect;
03149       }
03150   return best_match;
03151 }
03152 
03153 /* Function: find_pc_mapped_section (PC)
03154    If PC falls into the VMA address range of an overlay section that is
03155    currently marked as MAPPED, return that section.  Else return NULL.  */
03156 
03157 struct obj_section *
03158 find_pc_mapped_section (CORE_ADDR pc)
03159 {
03160   struct objfile *objfile;
03161   struct obj_section *osect;
03162 
03163   if (overlay_debugging)
03164     ALL_OBJSECTIONS (objfile, osect)
03165       if (pc_in_mapped_range (pc, osect) && section_is_mapped (osect))
03166         return osect;
03167 
03168   return NULL;
03169 }
03170 
03171 /* Function: list_overlays_command
03172    Print a list of mapped sections and their PC ranges.  */
03173 
03174 static void
03175 list_overlays_command (char *args, int from_tty)
03176 {
03177   int nmapped = 0;
03178   struct objfile *objfile;
03179   struct obj_section *osect;
03180 
03181   if (overlay_debugging)
03182     ALL_OBJSECTIONS (objfile, osect)
03183       if (section_is_mapped (osect))
03184       {
03185         struct gdbarch *gdbarch = get_objfile_arch (objfile);
03186         const char *name;
03187         bfd_vma lma, vma;
03188         int size;
03189 
03190         vma = bfd_section_vma (objfile->obfd, osect->the_bfd_section);
03191         lma = bfd_section_lma (objfile->obfd, osect->the_bfd_section);
03192         size = bfd_get_section_size (osect->the_bfd_section);
03193         name = bfd_section_name (objfile->obfd, osect->the_bfd_section);
03194 
03195         printf_filtered ("Section %s, loaded at ", name);
03196         fputs_filtered (paddress (gdbarch, lma), gdb_stdout);
03197         puts_filtered (" - ");
03198         fputs_filtered (paddress (gdbarch, lma + size), gdb_stdout);
03199         printf_filtered (", mapped at ");
03200         fputs_filtered (paddress (gdbarch, vma), gdb_stdout);
03201         puts_filtered (" - ");
03202         fputs_filtered (paddress (gdbarch, vma + size), gdb_stdout);
03203         puts_filtered ("\n");
03204 
03205         nmapped++;
03206       }
03207   if (nmapped == 0)
03208     printf_filtered (_("No sections are mapped.\n"));
03209 }
03210 
03211 /* Function: map_overlay_command
03212    Mark the named section as mapped (ie. residing at its VMA address).  */
03213 
03214 static void
03215 map_overlay_command (char *args, int from_tty)
03216 {
03217   struct objfile *objfile, *objfile2;
03218   struct obj_section *sec, *sec2;
03219 
03220   if (!overlay_debugging)
03221     error (_("Overlay debugging not enabled.  Use "
03222              "either the 'overlay auto' or\n"
03223              "the 'overlay manual' command."));
03224 
03225   if (args == 0 || *args == 0)
03226     error (_("Argument required: name of an overlay section"));
03227 
03228   /* First, find a section matching the user supplied argument.  */
03229   ALL_OBJSECTIONS (objfile, sec)
03230     if (!strcmp (bfd_section_name (objfile->obfd, sec->the_bfd_section), args))
03231     {
03232       /* Now, check to see if the section is an overlay.  */
03233       if (!section_is_overlay (sec))
03234         continue;               /* not an overlay section */
03235 
03236       /* Mark the overlay as "mapped".  */
03237       sec->ovly_mapped = 1;
03238 
03239       /* Next, make a pass and unmap any sections that are
03240          overlapped by this new section: */
03241       ALL_OBJSECTIONS (objfile2, sec2)
03242         if (sec2->ovly_mapped && sec != sec2 && sections_overlap (sec, sec2))
03243         {
03244           if (info_verbose)
03245             printf_unfiltered (_("Note: section %s unmapped by overlap\n"),
03246                              bfd_section_name (objfile->obfd,
03247                                                sec2->the_bfd_section));
03248           sec2->ovly_mapped = 0;        /* sec2 overlaps sec: unmap sec2.  */
03249         }
03250       return;
03251     }
03252   error (_("No overlay section called %s"), args);
03253 }
03254 
03255 /* Function: unmap_overlay_command
03256    Mark the overlay section as unmapped
03257    (ie. resident in its LMA address range, rather than the VMA range).  */
03258 
03259 static void
03260 unmap_overlay_command (char *args, int from_tty)
03261 {
03262   struct objfile *objfile;
03263   struct obj_section *sec;
03264 
03265   if (!overlay_debugging)
03266     error (_("Overlay debugging not enabled.  "
03267              "Use either the 'overlay auto' or\n"
03268              "the 'overlay manual' command."));
03269 
03270   if (args == 0 || *args == 0)
03271     error (_("Argument required: name of an overlay section"));
03272 
03273   /* First, find a section matching the user supplied argument.  */
03274   ALL_OBJSECTIONS (objfile, sec)
03275     if (!strcmp (bfd_section_name (objfile->obfd, sec->the_bfd_section), args))
03276     {
03277       if (!sec->ovly_mapped)
03278         error (_("Section %s is not mapped"), args);
03279       sec->ovly_mapped = 0;
03280       return;
03281     }
03282   error (_("No overlay section called %s"), args);
03283 }
03284 
03285 /* Function: overlay_auto_command
03286    A utility command to turn on overlay debugging.
03287    Possibly this should be done via a set/show command.  */
03288 
03289 static void
03290 overlay_auto_command (char *args, int from_tty)
03291 {
03292   overlay_debugging = ovly_auto;
03293   enable_overlay_breakpoints ();
03294   if (info_verbose)
03295     printf_unfiltered (_("Automatic overlay debugging enabled."));
03296 }
03297 
03298 /* Function: overlay_manual_command
03299    A utility command to turn on overlay debugging.
03300    Possibly this should be done via a set/show command.  */
03301 
03302 static void
03303 overlay_manual_command (char *args, int from_tty)
03304 {
03305   overlay_debugging = ovly_on;
03306   disable_overlay_breakpoints ();
03307   if (info_verbose)
03308     printf_unfiltered (_("Overlay debugging enabled."));
03309 }
03310 
03311 /* Function: overlay_off_command
03312    A utility command to turn on overlay debugging.
03313    Possibly this should be done via a set/show command.  */
03314 
03315 static void
03316 overlay_off_command (char *args, int from_tty)
03317 {
03318   overlay_debugging = ovly_off;
03319   disable_overlay_breakpoints ();
03320   if (info_verbose)
03321     printf_unfiltered (_("Overlay debugging disabled."));
03322 }
03323 
03324 static void
03325 overlay_load_command (char *args, int from_tty)
03326 {
03327   struct gdbarch *gdbarch = get_current_arch ();
03328 
03329   if (gdbarch_overlay_update_p (gdbarch))
03330     gdbarch_overlay_update (gdbarch, NULL);
03331   else
03332     error (_("This target does not know how to read its overlay state."));
03333 }
03334 
03335 /* Function: overlay_command
03336    A place-holder for a mis-typed command.  */
03337 
03338 /* Command list chain containing all defined "overlay" subcommands.  */
03339 static struct cmd_list_element *overlaylist;
03340 
03341 static void
03342 overlay_command (char *args, int from_tty)
03343 {
03344   printf_unfiltered
03345     ("\"overlay\" must be followed by the name of an overlay command.\n");
03346   help_list (overlaylist, "overlay ", -1, gdb_stdout);
03347 }
03348 
03349 /* Target Overlays for the "Simplest" overlay manager:
03350 
03351    This is GDB's default target overlay layer.  It works with the
03352    minimal overlay manager supplied as an example by Cygnus.  The
03353    entry point is via a function pointer "gdbarch_overlay_update",
03354    so targets that use a different runtime overlay manager can
03355    substitute their own overlay_update function and take over the
03356    function pointer.
03357 
03358    The overlay_update function pokes around in the target's data structures
03359    to see what overlays are mapped, and updates GDB's overlay mapping with
03360    this information.
03361 
03362    In this simple implementation, the target data structures are as follows:
03363    unsigned _novlys;            /# number of overlay sections #/
03364    unsigned _ovly_table[_novlys][4] = {
03365    {VMA, SIZE, LMA, MAPPED},    /# one entry per overlay section #/
03366    {..., ...,  ..., ...},
03367    }
03368    unsigned _novly_regions;     /# number of overlay regions #/
03369    unsigned _ovly_region_table[_novly_regions][3] = {
03370    {VMA, SIZE, MAPPED_TO_LMA},  /# one entry per overlay region #/
03371    {..., ...,  ...},
03372    }
03373    These functions will attempt to update GDB's mappedness state in the
03374    symbol section table, based on the target's mappedness state.
03375 
03376    To do this, we keep a cached copy of the target's _ovly_table, and
03377    attempt to detect when the cached copy is invalidated.  The main
03378    entry point is "simple_overlay_update(SECT), which looks up SECT in
03379    the cached table and re-reads only the entry for that section from
03380    the target (whenever possible).  */
03381 
03382 /* Cached, dynamically allocated copies of the target data structures: */
03383 static unsigned (*cache_ovly_table)[4] = 0;
03384 static unsigned cache_novlys = 0;
03385 static CORE_ADDR cache_ovly_table_base = 0;
03386 enum ovly_index
03387   {
03388     VMA, SIZE, LMA, MAPPED
03389   };
03390 
03391 /* Throw away the cached copy of _ovly_table.  */
03392 
03393 static void
03394 simple_free_overlay_table (void)
03395 {
03396   if (cache_ovly_table)
03397     xfree (cache_ovly_table);
03398   cache_novlys = 0;
03399   cache_ovly_table = NULL;
03400   cache_ovly_table_base = 0;
03401 }
03402 
03403 /* Read an array of ints of size SIZE from the target into a local buffer.
03404    Convert to host order.  int LEN is number of ints.  */
03405 
03406 static void
03407 read_target_long_array (CORE_ADDR memaddr, unsigned int *myaddr,
03408                         int len, int size, enum bfd_endian byte_order)
03409 {
03410   /* FIXME (alloca): Not safe if array is very large.  */
03411   gdb_byte *buf = alloca (len * size);
03412   int i;
03413 
03414   read_memory (memaddr, buf, len * size);
03415   for (i = 0; i < len; i++)
03416     myaddr[i] = extract_unsigned_integer (size * i + buf, size, byte_order);
03417 }
03418 
03419 /* Find and grab a copy of the target _ovly_table
03420    (and _novlys, which is needed for the table's size).  */
03421 
03422 static int
03423 simple_read_overlay_table (void)
03424 {
03425   struct minimal_symbol *novlys_msym;
03426   struct bound_minimal_symbol ovly_table_msym;
03427   struct gdbarch *gdbarch;
03428   int word_size;
03429   enum bfd_endian byte_order;
03430 
03431   simple_free_overlay_table ();
03432   novlys_msym = lookup_minimal_symbol ("_novlys", NULL, NULL);
03433   if (! novlys_msym)
03434     {
03435       error (_("Error reading inferior's overlay table: "
03436              "couldn't find `_novlys' variable\n"
03437              "in inferior.  Use `overlay manual' mode."));
03438       return 0;
03439     }
03440 
03441   ovly_table_msym = lookup_bound_minimal_symbol ("_ovly_table");
03442   if (! ovly_table_msym.minsym)
03443     {
03444       error (_("Error reading inferior's overlay table: couldn't find "
03445              "`_ovly_table' array\n"
03446              "in inferior.  Use `overlay manual' mode."));
03447       return 0;
03448     }
03449 
03450   gdbarch = get_objfile_arch (ovly_table_msym.objfile);
03451   word_size = gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT;
03452   byte_order = gdbarch_byte_order (gdbarch);
03453 
03454   cache_novlys = read_memory_integer (SYMBOL_VALUE_ADDRESS (novlys_msym),
03455                                       4, byte_order);
03456   cache_ovly_table
03457     = (void *) xmalloc (cache_novlys * sizeof (*cache_ovly_table));
03458   cache_ovly_table_base = SYMBOL_VALUE_ADDRESS (ovly_table_msym.minsym);
03459   read_target_long_array (cache_ovly_table_base,
03460                           (unsigned int *) cache_ovly_table,
03461                           cache_novlys * 4, word_size, byte_order);
03462 
03463   return 1;                     /* SUCCESS */
03464 }
03465 
03466 /* Function: simple_overlay_update_1
03467    A helper function for simple_overlay_update.  Assuming a cached copy
03468    of _ovly_table exists, look through it to find an entry whose vma,
03469    lma and size match those of OSECT.  Re-read the entry and make sure
03470    it still matches OSECT (else the table may no longer be valid).
03471    Set OSECT's mapped state to match the entry.  Return: 1 for
03472    success, 0 for failure.  */
03473 
03474 static int
03475 simple_overlay_update_1 (struct obj_section *osect)
03476 {
03477   int i, size;
03478   bfd *obfd = osect->objfile->obfd;
03479   asection *bsect = osect->the_bfd_section;
03480   struct gdbarch *gdbarch = get_objfile_arch (osect->objfile);
03481   int word_size = gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT;
03482   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
03483 
03484   size = bfd_get_section_size (osect->the_bfd_section);
03485   for (i = 0; i < cache_novlys; i++)
03486     if (cache_ovly_table[i][VMA] == bfd_section_vma (obfd, bsect)
03487         && cache_ovly_table[i][LMA] == bfd_section_lma (obfd, bsect)
03488         /* && cache_ovly_table[i][SIZE] == size */ )
03489       {
03490         read_target_long_array (cache_ovly_table_base + i * word_size,
03491                                 (unsigned int *) cache_ovly_table[i],
03492                                 4, word_size, byte_order);
03493         if (cache_ovly_table[i][VMA] == bfd_section_vma (obfd, bsect)
03494             && cache_ovly_table[i][LMA] == bfd_section_lma (obfd, bsect)
03495             /* && cache_ovly_table[i][SIZE] == size */ )
03496           {
03497             osect->ovly_mapped = cache_ovly_table[i][MAPPED];
03498             return 1;
03499           }
03500         else    /* Warning!  Warning!  Target's ovly table has changed!  */
03501           return 0;
03502       }
03503   return 0;
03504 }
03505 
03506 /* Function: simple_overlay_update
03507    If OSECT is NULL, then update all sections' mapped state
03508    (after re-reading the entire target _ovly_table).
03509    If OSECT is non-NULL, then try to find a matching entry in the
03510    cached ovly_table and update only OSECT's mapped state.
03511    If a cached entry can't be found or the cache isn't valid, then
03512    re-read the entire cache, and go ahead and update all sections.  */
03513 
03514 void
03515 simple_overlay_update (struct obj_section *osect)
03516 {
03517   struct objfile *objfile;
03518 
03519   /* Were we given an osect to look up?  NULL means do all of them.  */
03520   if (osect)
03521     /* Have we got a cached copy of the target's overlay table?  */
03522     if (cache_ovly_table != NULL)
03523       {
03524         /* Does its cached location match what's currently in the
03525            symtab?  */
03526         struct minimal_symbol *minsym
03527           = lookup_minimal_symbol ("_ovly_table", NULL, NULL);
03528 
03529         if (minsym == NULL)
03530           error (_("Error reading inferior's overlay table: couldn't "
03531                    "find `_ovly_table' array\n"
03532                    "in inferior.  Use `overlay manual' mode."));
03533         
03534         if (cache_ovly_table_base == SYMBOL_VALUE_ADDRESS (minsym))
03535           /* Then go ahead and try to look up this single section in
03536              the cache.  */
03537           if (simple_overlay_update_1 (osect))
03538             /* Found it!  We're done.  */
03539             return;
03540       }
03541 
03542   /* Cached table no good: need to read the entire table anew.
03543      Or else we want all the sections, in which case it's actually
03544      more efficient to read the whole table in one block anyway.  */
03545 
03546   if (! simple_read_overlay_table ())
03547     return;
03548 
03549   /* Now may as well update all sections, even if only one was requested.  */
03550   ALL_OBJSECTIONS (objfile, osect)
03551     if (section_is_overlay (osect))
03552     {
03553       int i, size;
03554       bfd *obfd = osect->objfile->obfd;
03555       asection *bsect = osect->the_bfd_section;
03556 
03557       size = bfd_get_section_size (bsect);
03558       for (i = 0; i < cache_novlys; i++)
03559         if (cache_ovly_table[i][VMA] == bfd_section_vma (obfd, bsect)
03560             && cache_ovly_table[i][LMA] == bfd_section_lma (obfd, bsect)
03561             /* && cache_ovly_table[i][SIZE] == size */ )
03562           { /* obj_section matches i'th entry in ovly_table.  */
03563             osect->ovly_mapped = cache_ovly_table[i][MAPPED];
03564             break;              /* finished with inner for loop: break out.  */
03565           }
03566     }
03567 }
03568 
03569 /* Set the output sections and output offsets for section SECTP in
03570    ABFD.  The relocation code in BFD will read these offsets, so we
03571    need to be sure they're initialized.  We map each section to itself,
03572    with no offset; this means that SECTP->vma will be honored.  */
03573 
03574 static void
03575 symfile_dummy_outputs (bfd *abfd, asection *sectp, void *dummy)
03576 {
03577   sectp->output_section = sectp;
03578   sectp->output_offset = 0;
03579 }
03580 
03581 /* Default implementation for sym_relocate.  */
03582 
03583 bfd_byte *
03584 default_symfile_relocate (struct objfile *objfile, asection *sectp,
03585                           bfd_byte *buf)
03586 {
03587   /* Use sectp->owner instead of objfile->obfd.  sectp may point to a
03588      DWO file.  */
03589   bfd *abfd = sectp->owner;
03590 
03591   /* We're only interested in sections with relocation
03592      information.  */
03593   if ((sectp->flags & SEC_RELOC) == 0)
03594     return NULL;
03595 
03596   /* We will handle section offsets properly elsewhere, so relocate as if
03597      all sections begin at 0.  */
03598   bfd_map_over_sections (abfd, symfile_dummy_outputs, NULL);
03599 
03600   return bfd_simple_get_relocated_section_contents (abfd, sectp, buf, NULL);
03601 }
03602 
03603 /* Relocate the contents of a debug section SECTP in ABFD.  The
03604    contents are stored in BUF if it is non-NULL, or returned in a
03605    malloc'd buffer otherwise.
03606 
03607    For some platforms and debug info formats, shared libraries contain
03608    relocations against the debug sections (particularly for DWARF-2;
03609    one affected platform is PowerPC GNU/Linux, although it depends on
03610    the version of the linker in use).  Also, ELF object files naturally
03611    have unresolved relocations for their debug sections.  We need to apply
03612    the relocations in order to get the locations of symbols correct.
03613    Another example that may require relocation processing, is the
03614    DWARF-2 .eh_frame section in .o files, although it isn't strictly a
03615    debug section.  */
03616 
03617 bfd_byte *
03618 symfile_relocate_debug_section (struct objfile *objfile,
03619                                 asection *sectp, bfd_byte *buf)
03620 {
03621   gdb_assert (objfile->sf->sym_relocate);
03622 
03623   return (*objfile->sf->sym_relocate) (objfile, sectp, buf);
03624 }
03625 
03626 struct symfile_segment_data *
03627 get_symfile_segment_data (bfd *abfd)
03628 {
03629   const struct sym_fns *sf = find_sym_fns (abfd);
03630 
03631   if (sf == NULL)
03632     return NULL;
03633 
03634   return sf->sym_segments (abfd);
03635 }
03636 
03637 void
03638 free_symfile_segment_data (struct symfile_segment_data *data)
03639 {
03640   xfree (data->segment_bases);
03641   xfree (data->segment_sizes);
03642   xfree (data->segment_info);
03643   xfree (data);
03644 }
03645 
03646 /* Given:
03647    - DATA, containing segment addresses from the object file ABFD, and
03648      the mapping from ABFD's sections onto the segments that own them,
03649      and
03650    - SEGMENT_BASES[0 .. NUM_SEGMENT_BASES - 1], holding the actual
03651      segment addresses reported by the target,
03652    store the appropriate offsets for each section in OFFSETS.
03653 
03654    If there are fewer entries in SEGMENT_BASES than there are segments
03655    in DATA, then apply SEGMENT_BASES' last entry to all the segments.
03656 
03657    If there are more entries, then ignore the extra.  The target may
03658    not be able to distinguish between an empty data segment and a
03659    missing data segment; a missing text segment is less plausible.  */
03660 
03661 int
03662 symfile_map_offsets_to_segments (bfd *abfd,
03663                                  const struct symfile_segment_data *data,
03664                                  struct section_offsets *offsets,
03665                                  int num_segment_bases,
03666                                  const CORE_ADDR *segment_bases)
03667 {
03668   int i;
03669   asection *sect;
03670 
03671   /* It doesn't make sense to call this function unless you have some
03672      segment base addresses.  */
03673   gdb_assert (num_segment_bases > 0);
03674 
03675   /* If we do not have segment mappings for the object file, we
03676      can not relocate it by segments.  */
03677   gdb_assert (data != NULL);
03678   gdb_assert (data->num_segments > 0);
03679 
03680   for (i = 0, sect = abfd->sections; sect != NULL; i++, sect = sect->next)
03681     {
03682       int which = data->segment_info[i];
03683 
03684       gdb_assert (0 <= which && which <= data->num_segments);
03685 
03686       /* Don't bother computing offsets for sections that aren't
03687          loaded as part of any segment.  */
03688       if (! which)
03689         continue;
03690 
03691       /* Use the last SEGMENT_BASES entry as the address of any extra
03692          segments mentioned in DATA->segment_info.  */
03693       if (which > num_segment_bases)
03694         which = num_segment_bases;
03695 
03696       offsets->offsets[i] = (segment_bases[which - 1]
03697                              - data->segment_bases[which - 1]);
03698     }
03699 
03700   return 1;
03701 }
03702 
03703 static void
03704 symfile_find_segment_sections (struct objfile *objfile)
03705 {
03706   bfd *abfd = objfile->obfd;
03707   int i;
03708   asection *sect;
03709   struct symfile_segment_data *data;
03710 
03711   data = get_symfile_segment_data (objfile->obfd);
03712   if (data == NULL)
03713     return;
03714 
03715   if (data->num_segments != 1 && data->num_segments != 2)
03716     {
03717       free_symfile_segment_data (data);
03718       return;
03719     }
03720 
03721   for (i = 0, sect = abfd->sections; sect != NULL; i++, sect = sect->next)
03722     {
03723       int which = data->segment_info[i];
03724 
03725       if (which == 1)
03726         {
03727           if (objfile->sect_index_text == -1)
03728             objfile->sect_index_text = sect->index;
03729 
03730           if (objfile->sect_index_rodata == -1)
03731             objfile->sect_index_rodata = sect->index;
03732         }
03733       else if (which == 2)
03734         {
03735           if (objfile->sect_index_data == -1)
03736             objfile->sect_index_data = sect->index;
03737 
03738           if (objfile->sect_index_bss == -1)
03739             objfile->sect_index_bss = sect->index;
03740         }
03741     }
03742 
03743   free_symfile_segment_data (data);
03744 }
03745 
03746 void
03747 _initialize_symfile (void)
03748 {
03749   struct cmd_list_element *c;
03750 
03751   c = add_cmd ("symbol-file", class_files, symbol_file_command, _("\
03752 Load symbol table from executable file FILE.\n\
03753 The `file' command can also load symbol tables, as well as setting the file\n\
03754 to execute."), &cmdlist);
03755   set_cmd_completer (c, filename_completer);
03756 
03757   c = add_cmd ("add-symbol-file", class_files, add_symbol_file_command, _("\
03758 Load symbols from FILE, assuming FILE has been dynamically loaded.\n\
03759 Usage: add-symbol-file FILE ADDR [-s <SECT> <SECT_ADDR> -s <SECT> <SECT_ADDR>\
03760  ...]\nADDR is the starting address of the file's text.\n\
03761 The optional arguments are section-name section-address pairs and\n\
03762 should be specified if the data and bss segments are not contiguous\n\
03763 with the text.  SECT is a section name to be loaded at SECT_ADDR."),
03764                &cmdlist);
03765   set_cmd_completer (c, filename_completer);
03766 
03767   c = add_cmd ("load", class_files, load_command, _("\
03768 Dynamically load FILE into the running program, and record its symbols\n\
03769 for access from GDB.\n\
03770 A load OFFSET may also be given."), &cmdlist);
03771   set_cmd_completer (c, filename_completer);
03772 
03773   add_prefix_cmd ("overlay", class_support, overlay_command,
03774                   _("Commands for debugging overlays."), &overlaylist,
03775                   "overlay ", 0, &cmdlist);
03776 
03777   add_com_alias ("ovly", "overlay", class_alias, 1);
03778   add_com_alias ("ov", "overlay", class_alias, 1);
03779 
03780   add_cmd ("map-overlay", class_support, map_overlay_command,
03781            _("Assert that an overlay section is mapped."), &overlaylist);
03782 
03783   add_cmd ("unmap-overlay", class_support, unmap_overlay_command,
03784            _("Assert that an overlay section is unmapped."), &overlaylist);
03785 
03786   add_cmd ("list-overlays", class_support, list_overlays_command,
03787            _("List mappings of overlay sections."), &overlaylist);
03788 
03789   add_cmd ("manual", class_support, overlay_manual_command,
03790            _("Enable overlay debugging."), &overlaylist);
03791   add_cmd ("off", class_support, overlay_off_command,
03792            _("Disable overlay debugging."), &overlaylist);
03793   add_cmd ("auto", class_support, overlay_auto_command,
03794            _("Enable automatic overlay debugging."), &overlaylist);
03795   add_cmd ("load-target", class_support, overlay_load_command,
03796            _("Read the overlay mapping state from the target."), &overlaylist);
03797 
03798   /* Filename extension to source language lookup table: */
03799   init_filename_language_table ();
03800   add_setshow_string_noescape_cmd ("extension-language", class_files,
03801                                    &ext_args, _("\
03802 Set mapping between filename extension and source language."), _("\
03803 Show mapping between filename extension and source language."), _("\
03804 Usage: set extension-language .foo bar"),
03805                                    set_ext_lang_command,
03806                                    show_ext_args,
03807                                    &setlist, &showlist);
03808 
03809   add_info ("extensions", info_ext_lang_command,
03810             _("All filename extensions associated with a source language."));
03811 
03812   add_setshow_optional_filename_cmd ("debug-file-directory", class_support,
03813                                      &debug_file_directory, _("\
03814 Set the directories where separate debug symbols are searched for."), _("\
03815 Show the directories where separate debug symbols are searched for."), _("\
03816 Separate debug symbols are first searched for in the same\n\
03817 directory as the binary, then in the `" DEBUG_SUBDIRECTORY "' subdirectory,\n\
03818 and lastly at the path of the directory of the binary with\n\
03819 each global debug-file-directory component prepended."),
03820                                      NULL,
03821                                      show_debug_file_directory,
03822                                      &setlist, &showlist);
03823 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines