Home | History | Annotate | Line # | Download | only in gdb
symfile.c revision 1.1.1.6
      1      1.1  christos /* Generic symbol file reading for the GNU debugger, GDB.
      2      1.1  christos 
      3  1.1.1.6  christos    Copyright (C) 1990-2019 Free Software Foundation, Inc.
      4      1.1  christos 
      5      1.1  christos    Contributed by Cygnus Support, using pieces from other GDB modules.
      6      1.1  christos 
      7      1.1  christos    This file is part of GDB.
      8      1.1  christos 
      9      1.1  christos    This program is free software; you can redistribute it and/or modify
     10      1.1  christos    it under the terms of the GNU General Public License as published by
     11      1.1  christos    the Free Software Foundation; either version 3 of the License, or
     12      1.1  christos    (at your option) any later version.
     13      1.1  christos 
     14      1.1  christos    This program is distributed in the hope that it will be useful,
     15      1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     16      1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17      1.1  christos    GNU General Public License for more details.
     18      1.1  christos 
     19      1.1  christos    You should have received a copy of the GNU General Public License
     20      1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     21      1.1  christos 
     22      1.1  christos #include "defs.h"
     23      1.1  christos #include "arch-utils.h"
     24      1.1  christos #include "bfdlink.h"
     25      1.1  christos #include "symtab.h"
     26      1.1  christos #include "gdbtypes.h"
     27      1.1  christos #include "gdbcore.h"
     28      1.1  christos #include "frame.h"
     29      1.1  christos #include "target.h"
     30      1.1  christos #include "value.h"
     31      1.1  christos #include "symfile.h"
     32      1.1  christos #include "objfiles.h"
     33      1.1  christos #include "source.h"
     34      1.1  christos #include "gdbcmd.h"
     35      1.1  christos #include "breakpoint.h"
     36      1.1  christos #include "language.h"
     37      1.1  christos #include "complaints.h"
     38      1.1  christos #include "demangle.h"
     39      1.1  christos #include "inferior.h"
     40      1.1  christos #include "regcache.h"
     41      1.1  christos #include "filenames.h"		/* for DOSish file names */
     42      1.1  christos #include "gdb-stabs.h"
     43      1.1  christos #include "gdb_obstack.h"
     44      1.1  christos #include "completer.h"
     45      1.1  christos #include "bcache.h"
     46      1.1  christos #include "hashtab.h"
     47      1.1  christos #include "readline/readline.h"
     48      1.1  christos #include "block.h"
     49  1.1.1.6  christos #include "observable.h"
     50      1.1  christos #include "exec.h"
     51      1.1  christos #include "parser-defs.h"
     52      1.1  christos #include "varobj.h"
     53      1.1  christos #include "elf-bfd.h"
     54      1.1  christos #include "solib.h"
     55      1.1  christos #include "remote.h"
     56      1.1  christos #include "stack.h"
     57      1.1  christos #include "gdb_bfd.h"
     58      1.1  christos #include "cli/cli-utils.h"
     59  1.1.1.6  christos #include "common/byte-vector.h"
     60  1.1.1.6  christos #include "common/pathstuff.h"
     61  1.1.1.6  christos #include "common/selftest.h"
     62  1.1.1.6  christos #include "cli/cli-style.h"
     63  1.1.1.6  christos #include "common/forward-scope-exit.h"
     64      1.1  christos 
     65      1.1  christos #include <sys/types.h>
     66      1.1  christos #include <fcntl.h>
     67      1.1  christos #include <sys/stat.h>
     68      1.1  christos #include <ctype.h>
     69  1.1.1.5  christos #include <chrono>
     70  1.1.1.6  christos #include <algorithm>
     71      1.1  christos 
     72      1.1  christos #include "psymtab.h"
     73      1.1  christos 
     74      1.1  christos int (*deprecated_ui_load_progress_hook) (const char *section,
     75      1.1  christos 					 unsigned long num);
     76      1.1  christos void (*deprecated_show_load_progress) (const char *section,
     77      1.1  christos 			    unsigned long section_sent,
     78      1.1  christos 			    unsigned long section_size,
     79      1.1  christos 			    unsigned long total_sent,
     80      1.1  christos 			    unsigned long total_size);
     81      1.1  christos void (*deprecated_pre_add_symbol_hook) (const char *);
     82      1.1  christos void (*deprecated_post_add_symbol_hook) (void);
     83      1.1  christos 
     84  1.1.1.6  christos using clear_symtab_users_cleanup
     85  1.1.1.6  christos   = FORWARD_SCOPE_EXIT (clear_symtab_users);
     86      1.1  christos 
     87      1.1  christos /* Global variables owned by this file.  */
     88      1.1  christos int readnow_symbol_files;	/* Read full symbols immediately.  */
     89  1.1.1.6  christos int readnever_symbol_files;	/* Never read full symbols.  */
     90      1.1  christos 
     91      1.1  christos /* Functions this file defines.  */
     92      1.1  christos 
     93  1.1.1.5  christos static void symbol_file_add_main_1 (const char *args, symfile_add_flags add_flags,
     94  1.1.1.6  christos 				    objfile_flags flags, CORE_ADDR reloff);
     95      1.1  christos 
     96      1.1  christos static const struct sym_fns *find_sym_fns (bfd *);
     97      1.1  christos 
     98      1.1  christos static void overlay_invalidate_all (void);
     99      1.1  christos 
    100      1.1  christos static void simple_free_overlay_table (void);
    101      1.1  christos 
    102      1.1  christos static void read_target_long_array (CORE_ADDR, unsigned int *, int, int,
    103      1.1  christos 				    enum bfd_endian);
    104      1.1  christos 
    105      1.1  christos static int simple_read_overlay_table (void);
    106      1.1  christos 
    107      1.1  christos static int simple_overlay_update_1 (struct obj_section *);
    108      1.1  christos 
    109      1.1  christos static void symfile_find_segment_sections (struct objfile *objfile);
    110      1.1  christos 
    111      1.1  christos /* List of all available sym_fns.  On gdb startup, each object file reader
    112      1.1  christos    calls add_symtab_fns() to register information on each format it is
    113      1.1  christos    prepared to read.  */
    114      1.1  christos 
    115  1.1.1.6  christos struct registered_sym_fns
    116      1.1  christos {
    117  1.1.1.6  christos   registered_sym_fns (bfd_flavour sym_flavour_, const struct sym_fns *sym_fns_)
    118  1.1.1.6  christos   : sym_flavour (sym_flavour_), sym_fns (sym_fns_)
    119  1.1.1.6  christos   {}
    120  1.1.1.6  christos 
    121      1.1  christos   /* BFD flavour that we handle.  */
    122      1.1  christos   enum bfd_flavour sym_flavour;
    123      1.1  christos 
    124      1.1  christos   /* The "vtable" of symbol functions.  */
    125      1.1  christos   const struct sym_fns *sym_fns;
    126  1.1.1.6  christos };
    127      1.1  christos 
    128  1.1.1.6  christos static std::vector<registered_sym_fns> symtab_fns;
    129      1.1  christos 
    130  1.1.1.2  christos /* Values for "set print symbol-loading".  */
    131  1.1.1.2  christos 
    132  1.1.1.2  christos const char print_symbol_loading_off[] = "off";
    133  1.1.1.2  christos const char print_symbol_loading_brief[] = "brief";
    134  1.1.1.2  christos const char print_symbol_loading_full[] = "full";
    135  1.1.1.2  christos static const char *print_symbol_loading_enums[] =
    136  1.1.1.2  christos {
    137  1.1.1.2  christos   print_symbol_loading_off,
    138  1.1.1.2  christos   print_symbol_loading_brief,
    139  1.1.1.2  christos   print_symbol_loading_full,
    140  1.1.1.2  christos   NULL
    141  1.1.1.2  christos };
    142  1.1.1.2  christos static const char *print_symbol_loading = print_symbol_loading_full;
    143  1.1.1.2  christos 
    144      1.1  christos /* If non-zero, shared library symbols will be added automatically
    145      1.1  christos    when the inferior is created, new libraries are loaded, or when
    146      1.1  christos    attaching to the inferior.  This is almost always what users will
    147      1.1  christos    want to have happen; but for very large programs, the startup time
    148      1.1  christos    will be excessive, and so if this is a problem, the user can clear
    149      1.1  christos    this flag and then add the shared library symbols as needed.  Note
    150      1.1  christos    that there is a potential for confusion, since if the shared
    151      1.1  christos    library symbols are not loaded, commands like "info fun" will *not*
    152      1.1  christos    report all the functions that are actually present.  */
    153      1.1  christos 
    154      1.1  christos int auto_solib_add = 1;
    155      1.1  christos 
    156      1.1  christos 
    158  1.1.1.2  christos /* Return non-zero if symbol-loading messages should be printed.
    159  1.1.1.2  christos    FROM_TTY is the standard from_tty argument to gdb commands.
    160  1.1.1.2  christos    If EXEC is non-zero the messages are for the executable.
    161  1.1.1.2  christos    Otherwise, messages are for shared libraries.
    162  1.1.1.2  christos    If FULL is non-zero then the caller is printing a detailed message.
    163  1.1.1.2  christos    E.g., the message includes the shared library name.
    164  1.1.1.2  christos    Otherwise, the caller is printing a brief "summary" message.  */
    165  1.1.1.2  christos 
    166  1.1.1.2  christos int
    167  1.1.1.2  christos print_symbol_loading_p (int from_tty, int exec, int full)
    168  1.1.1.2  christos {
    169  1.1.1.2  christos   if (!from_tty && !info_verbose)
    170  1.1.1.2  christos     return 0;
    171  1.1.1.2  christos 
    172  1.1.1.2  christos   if (exec)
    173  1.1.1.2  christos     {
    174  1.1.1.2  christos       /* We don't check FULL for executables, there are few such
    175  1.1.1.2  christos 	 messages, therefore brief == full.  */
    176  1.1.1.2  christos       return print_symbol_loading != print_symbol_loading_off;
    177  1.1.1.2  christos     }
    178  1.1.1.2  christos   if (full)
    179  1.1.1.2  christos     return print_symbol_loading == print_symbol_loading_full;
    180  1.1.1.2  christos   return print_symbol_loading == print_symbol_loading_brief;
    181  1.1.1.2  christos }
    182      1.1  christos 
    183      1.1  christos /* True if we are reading a symbol table.  */
    184      1.1  christos 
    185      1.1  christos int currently_reading_symtab = 0;
    186      1.1  christos 
    187      1.1  christos /* Increment currently_reading_symtab and return a cleanup that can be
    188      1.1  christos    used to decrement it.  */
    189  1.1.1.5  christos 
    190      1.1  christos scoped_restore_tmpl<int>
    191      1.1  christos increment_reading_symtab (void)
    192  1.1.1.5  christos {
    193  1.1.1.5  christos   gdb_assert (currently_reading_symtab >= 0);
    194  1.1.1.5  christos   return make_scoped_restore (&currently_reading_symtab,
    195      1.1  christos 			      currently_reading_symtab + 1);
    196      1.1  christos }
    197      1.1  christos 
    198      1.1  christos /* Remember the lowest-addressed loadable section we've seen.
    199      1.1  christos    This function is called via bfd_map_over_sections.
    200      1.1  christos 
    201      1.1  christos    In case of equal vmas, the section with the largest size becomes the
    202      1.1  christos    lowest-addressed loadable section.
    203      1.1  christos 
    204      1.1  christos    If the vmas and sizes are equal, the last section is considered the
    205      1.1  christos    lowest-addressed loadable section.  */
    206      1.1  christos 
    207      1.1  christos void
    208      1.1  christos find_lowest_section (bfd *abfd, asection *sect, void *obj)
    209      1.1  christos {
    210      1.1  christos   asection **lowest = (asection **) obj;
    211      1.1  christos 
    212      1.1  christos   if (0 == (bfd_get_section_flags (abfd, sect) & (SEC_ALLOC | SEC_LOAD)))
    213      1.1  christos     return;
    214      1.1  christos   if (!*lowest)
    215      1.1  christos     *lowest = sect;		/* First loadable section */
    216      1.1  christos   else if (bfd_section_vma (abfd, *lowest) > bfd_section_vma (abfd, sect))
    217      1.1  christos     *lowest = sect;		/* A lower loadable section */
    218      1.1  christos   else if (bfd_section_vma (abfd, *lowest) == bfd_section_vma (abfd, sect)
    219      1.1  christos 	   && (bfd_section_size (abfd, (*lowest))
    220      1.1  christos 	       <= bfd_section_size (abfd, sect)))
    221      1.1  christos     *lowest = sect;
    222      1.1  christos }
    223      1.1  christos 
    224      1.1  christos /* Build (allocate and populate) a section_addr_info struct from
    225      1.1  christos    an existing section table.  */
    226  1.1.1.6  christos 
    227      1.1  christos section_addr_info
    228      1.1  christos build_section_addr_info_from_section_table (const struct target_section *start,
    229      1.1  christos                                             const struct target_section *end)
    230      1.1  christos {
    231      1.1  christos   const struct target_section *stp;
    232  1.1.1.6  christos 
    233      1.1  christos   section_addr_info sap;
    234  1.1.1.6  christos 
    235      1.1  christos   for (stp = start; stp != end; stp++)
    236      1.1  christos     {
    237      1.1  christos       struct bfd_section *asect = stp->the_bfd_section;
    238      1.1  christos       bfd *abfd = asect->owner;
    239      1.1  christos 
    240  1.1.1.6  christos       if (bfd_get_section_flags (abfd, asect) & (SEC_ALLOC | SEC_LOAD)
    241  1.1.1.6  christos 	  && sap.size () < end - start)
    242  1.1.1.6  christos 	sap.emplace_back (stp->addr,
    243  1.1.1.6  christos 			  bfd_section_name (abfd, asect),
    244      1.1  christos 			  gdb_bfd_section_index (abfd, asect));
    245      1.1  christos     }
    246      1.1  christos 
    247      1.1  christos   return sap;
    248      1.1  christos }
    249      1.1  christos 
    250      1.1  christos /* Create a section_addr_info from section offsets in ABFD.  */
    251  1.1.1.6  christos 
    252      1.1  christos static section_addr_info
    253      1.1  christos build_section_addr_info_from_bfd (bfd *abfd)
    254      1.1  christos {
    255      1.1  christos   struct bfd_section *sec;
    256  1.1.1.6  christos 
    257  1.1.1.6  christos   section_addr_info sap;
    258      1.1  christos   for (sec = abfd->sections; sec != NULL; sec = sec->next)
    259  1.1.1.6  christos     if (bfd_get_section_flags (abfd, sec) & (SEC_ALLOC | SEC_LOAD))
    260  1.1.1.6  christos       sap.emplace_back (bfd_get_section_vma (abfd, sec),
    261  1.1.1.6  christos 			bfd_get_section_name (abfd, sec),
    262      1.1  christos 			gdb_bfd_section_index (abfd, sec));
    263      1.1  christos 
    264      1.1  christos   return sap;
    265      1.1  christos }
    266      1.1  christos 
    267      1.1  christos /* Create a section_addr_info from section offsets in OBJFILE.  */
    268  1.1.1.6  christos 
    269      1.1  christos section_addr_info
    270      1.1  christos build_section_addr_info_from_objfile (const struct objfile *objfile)
    271      1.1  christos {
    272      1.1  christos   int i;
    273      1.1  christos 
    274      1.1  christos   /* Before reread_symbols gets rewritten it is not safe to call:
    275      1.1  christos      gdb_assert (objfile->num_sections == bfd_count_sections (objfile->obfd));
    276  1.1.1.6  christos      */
    277  1.1.1.6  christos   section_addr_info sap = build_section_addr_info_from_bfd (objfile->obfd);
    278      1.1  christos   for (i = 0; i < sap.size (); i++)
    279  1.1.1.6  christos     {
    280      1.1  christos       int sectindex = sap[i].sectindex;
    281  1.1.1.6  christos 
    282      1.1  christos       sap[i].addr += objfile->section_offsets->offsets[sectindex];
    283      1.1  christos     }
    284      1.1  christos   return sap;
    285      1.1  christos }
    286      1.1  christos 
    287      1.1  christos /* Initialize OBJFILE's sect_index_* members.  */
    288      1.1  christos 
    289      1.1  christos static void
    290      1.1  christos init_objfile_sect_indices (struct objfile *objfile)
    291      1.1  christos {
    292      1.1  christos   asection *sect;
    293      1.1  christos   int i;
    294      1.1  christos 
    295      1.1  christos   sect = bfd_get_section_by_name (objfile->obfd, ".text");
    296      1.1  christos   if (sect)
    297      1.1  christos     objfile->sect_index_text = sect->index;
    298      1.1  christos 
    299      1.1  christos   sect = bfd_get_section_by_name (objfile->obfd, ".data");
    300      1.1  christos   if (sect)
    301      1.1  christos     objfile->sect_index_data = sect->index;
    302      1.1  christos 
    303      1.1  christos   sect = bfd_get_section_by_name (objfile->obfd, ".bss");
    304      1.1  christos   if (sect)
    305      1.1  christos     objfile->sect_index_bss = sect->index;
    306      1.1  christos 
    307      1.1  christos   sect = bfd_get_section_by_name (objfile->obfd, ".rodata");
    308      1.1  christos   if (sect)
    309      1.1  christos     objfile->sect_index_rodata = sect->index;
    310      1.1  christos 
    311      1.1  christos   /* This is where things get really weird...  We MUST have valid
    312      1.1  christos      indices for the various sect_index_* members or gdb will abort.
    313      1.1  christos      So if for example, there is no ".text" section, we have to
    314      1.1  christos      accomodate that.  First, check for a file with the standard
    315      1.1  christos      one or two segments.  */
    316      1.1  christos 
    317      1.1  christos   symfile_find_segment_sections (objfile);
    318      1.1  christos 
    319      1.1  christos   /* Except when explicitly adding symbol files at some address,
    320      1.1  christos      section_offsets contains nothing but zeros, so it doesn't matter
    321      1.1  christos      which slot in section_offsets the individual sect_index_* members
    322      1.1  christos      index into.  So if they are all zero, it is safe to just point
    323      1.1  christos      all the currently uninitialized indices to the first slot.  But
    324      1.1  christos      beware: if this is the main executable, it may be relocated
    325      1.1  christos      later, e.g. by the remote qOffsets packet, and then this will
    326      1.1  christos      be wrong!  That's why we try segments first.  */
    327      1.1  christos 
    328      1.1  christos   for (i = 0; i < objfile->num_sections; i++)
    329      1.1  christos     {
    330      1.1  christos       if (ANOFFSET (objfile->section_offsets, i) != 0)
    331      1.1  christos 	{
    332      1.1  christos 	  break;
    333      1.1  christos 	}
    334      1.1  christos     }
    335      1.1  christos   if (i == objfile->num_sections)
    336      1.1  christos     {
    337      1.1  christos       if (objfile->sect_index_text == -1)
    338      1.1  christos 	objfile->sect_index_text = 0;
    339      1.1  christos       if (objfile->sect_index_data == -1)
    340      1.1  christos 	objfile->sect_index_data = 0;
    341      1.1  christos       if (objfile->sect_index_bss == -1)
    342      1.1  christos 	objfile->sect_index_bss = 0;
    343      1.1  christos       if (objfile->sect_index_rodata == -1)
    344      1.1  christos 	objfile->sect_index_rodata = 0;
    345      1.1  christos     }
    346      1.1  christos }
    347      1.1  christos 
    348      1.1  christos /* The arguments to place_section.  */
    349      1.1  christos 
    350      1.1  christos struct place_section_arg
    351      1.1  christos {
    352      1.1  christos   struct section_offsets *offsets;
    353      1.1  christos   CORE_ADDR lowest;
    354      1.1  christos };
    355      1.1  christos 
    356      1.1  christos /* Find a unique offset to use for loadable section SECT if
    357      1.1  christos    the user did not provide an offset.  */
    358      1.1  christos 
    359      1.1  christos static void
    360      1.1  christos place_section (bfd *abfd, asection *sect, void *obj)
    361  1.1.1.4  christos {
    362      1.1  christos   struct place_section_arg *arg = (struct place_section_arg *) obj;
    363      1.1  christos   CORE_ADDR *offsets = arg->offsets->offsets, start_addr;
    364      1.1  christos   int done;
    365      1.1  christos   ULONGEST align = ((ULONGEST) 1) << bfd_get_section_alignment (abfd, sect);
    366      1.1  christos 
    367      1.1  christos   /* We are only interested in allocated sections.  */
    368      1.1  christos   if ((bfd_get_section_flags (abfd, sect) & SEC_ALLOC) == 0)
    369      1.1  christos     return;
    370      1.1  christos 
    371      1.1  christos   /* If the user specified an offset, honor it.  */
    372      1.1  christos   if (offsets[gdb_bfd_section_index (abfd, sect)] != 0)
    373      1.1  christos     return;
    374      1.1  christos 
    375      1.1  christos   /* Otherwise, let's try to find a place for the section.  */
    376      1.1  christos   start_addr = (arg->lowest + align - 1) & -align;
    377      1.1  christos 
    378      1.1  christos   do {
    379      1.1  christos     asection *cur_sec;
    380      1.1  christos 
    381      1.1  christos     done = 1;
    382      1.1  christos 
    383      1.1  christos     for (cur_sec = abfd->sections; cur_sec != NULL; cur_sec = cur_sec->next)
    384      1.1  christos       {
    385      1.1  christos 	int indx = cur_sec->index;
    386      1.1  christos 
    387      1.1  christos 	/* We don't need to compare against ourself.  */
    388      1.1  christos 	if (cur_sec == sect)
    389      1.1  christos 	  continue;
    390      1.1  christos 
    391      1.1  christos 	/* We can only conflict with allocated sections.  */
    392      1.1  christos 	if ((bfd_get_section_flags (abfd, cur_sec) & SEC_ALLOC) == 0)
    393      1.1  christos 	  continue;
    394      1.1  christos 
    395      1.1  christos 	/* If the section offset is 0, either the section has not been placed
    396      1.1  christos 	   yet, or it was the lowest section placed (in which case LOWEST
    397      1.1  christos 	   will be past its end).  */
    398      1.1  christos 	if (offsets[indx] == 0)
    399      1.1  christos 	  continue;
    400      1.1  christos 
    401      1.1  christos 	/* If this section would overlap us, then we must move up.  */
    402      1.1  christos 	if (start_addr + bfd_get_section_size (sect) > offsets[indx]
    403      1.1  christos 	    && start_addr < offsets[indx] + bfd_get_section_size (cur_sec))
    404      1.1  christos 	  {
    405      1.1  christos 	    start_addr = offsets[indx] + bfd_get_section_size (cur_sec);
    406      1.1  christos 	    start_addr = (start_addr + align - 1) & -align;
    407      1.1  christos 	    done = 0;
    408      1.1  christos 	    break;
    409      1.1  christos 	  }
    410      1.1  christos 
    411      1.1  christos 	/* Otherwise, we appear to be OK.  So far.  */
    412      1.1  christos       }
    413      1.1  christos     }
    414      1.1  christos   while (!done);
    415      1.1  christos 
    416      1.1  christos   offsets[gdb_bfd_section_index (abfd, sect)] = start_addr;
    417      1.1  christos   arg->lowest = start_addr + bfd_get_section_size (sect);
    418      1.1  christos }
    419  1.1.1.6  christos 
    420      1.1  christos /* Store section_addr_info as prepared (made relative and with SECTINDEX
    421      1.1  christos    filled-in) by addr_info_make_relative into SECTION_OFFSETS of NUM_SECTIONS
    422      1.1  christos    entries.  */
    423      1.1  christos 
    424      1.1  christos void
    425      1.1  christos relative_addr_info_to_section_offsets (struct section_offsets *section_offsets,
    426  1.1.1.6  christos 				       int num_sections,
    427      1.1  christos 				       const section_addr_info &addrs)
    428      1.1  christos {
    429      1.1  christos   int i;
    430      1.1  christos 
    431      1.1  christos   memset (section_offsets, 0, SIZEOF_N_SECTION_OFFSETS (num_sections));
    432      1.1  christos 
    433  1.1.1.6  christos   /* Now calculate offsets for section that were specified by the caller.  */
    434      1.1  christos   for (i = 0; i < addrs.size (); i++)
    435      1.1  christos     {
    436      1.1  christos       const struct other_sections *osp;
    437  1.1.1.6  christos 
    438      1.1  christos       osp = &addrs[i];
    439      1.1  christos       if (osp->sectindex == -1)
    440      1.1  christos   	continue;
    441      1.1  christos 
    442      1.1  christos       /* Record all sections in offsets.  */
    443      1.1  christos       /* The section_offsets in the objfile are here filled in using
    444      1.1  christos          the BFD index.  */
    445      1.1  christos       section_offsets->offsets[osp->sectindex] = osp->addr;
    446      1.1  christos     }
    447      1.1  christos }
    448      1.1  christos 
    449      1.1  christos /* Transform section name S for a name comparison.  prelink can split section
    450      1.1  christos    `.bss' into two sections `.dynbss' and `.bss' (in this order).  Similarly
    451      1.1  christos    prelink can split `.sbss' into `.sdynbss' and `.sbss'.  Use virtual address
    452      1.1  christos    of the new `.dynbss' (`.sdynbss') section as the adjacent new `.bss'
    453      1.1  christos    (`.sbss') section has invalid (increased) virtual address.  */
    454      1.1  christos 
    455      1.1  christos static const char *
    456      1.1  christos addr_section_name (const char *s)
    457      1.1  christos {
    458      1.1  christos   if (strcmp (s, ".dynbss") == 0)
    459      1.1  christos     return ".bss";
    460      1.1  christos   if (strcmp (s, ".sdynbss") == 0)
    461      1.1  christos     return ".sbss";
    462      1.1  christos 
    463      1.1  christos   return s;
    464      1.1  christos }
    465  1.1.1.6  christos 
    466  1.1.1.6  christos /* std::sort comparator for addrs_section_sort.  Sort entries in
    467  1.1.1.6  christos    ascending order by their (name, sectindex) pair.  sectindex makes
    468  1.1.1.6  christos    the sort by name stable.  */
    469  1.1.1.6  christos 
    470  1.1.1.6  christos static bool
    471  1.1.1.6  christos addrs_section_compar (const struct other_sections *a,
    472      1.1  christos 		      const struct other_sections *b)
    473      1.1  christos {
    474      1.1  christos   int retval;
    475  1.1.1.6  christos 
    476  1.1.1.6  christos   retval = strcmp (addr_section_name (a->name.c_str ()),
    477  1.1.1.6  christos 		   addr_section_name (b->name.c_str ()));
    478  1.1.1.6  christos   if (retval != 0)
    479      1.1  christos     return retval < 0;
    480  1.1.1.6  christos 
    481      1.1  christos   return a->sectindex < b->sectindex;
    482      1.1  christos }
    483  1.1.1.6  christos 
    484      1.1  christos /* Provide sorted array of pointers to sections of ADDRS.  */
    485  1.1.1.6  christos 
    486  1.1.1.6  christos static std::vector<const struct other_sections *>
    487      1.1  christos addrs_section_sort (const section_addr_info &addrs)
    488      1.1  christos {
    489      1.1  christos   int i;
    490  1.1.1.6  christos 
    491  1.1.1.6  christos   std::vector<const struct other_sections *> array (addrs.size ());
    492  1.1.1.6  christos   for (i = 0; i < addrs.size (); i++)
    493      1.1  christos     array[i] = &addrs[i];
    494  1.1.1.6  christos 
    495      1.1  christos   std::sort (array.begin (), array.end (), addrs_section_compar);
    496      1.1  christos 
    497      1.1  christos   return array;
    498      1.1  christos }
    499      1.1  christos 
    500      1.1  christos /* Relativize absolute addresses in ADDRS into offsets based on ABFD.  Fill-in
    501      1.1  christos    also SECTINDEXes specific to ABFD there.  This function can be used to
    502      1.1  christos    rebase ADDRS to start referencing different BFD than before.  */
    503      1.1  christos 
    504  1.1.1.6  christos void
    505      1.1  christos addr_info_make_relative (section_addr_info *addrs, bfd *abfd)
    506      1.1  christos {
    507      1.1  christos   asection *lower_sect;
    508      1.1  christos   CORE_ADDR lower_offset;
    509      1.1  christos   int i;
    510      1.1  christos 
    511      1.1  christos   /* Find lowest loadable section to be used as starting point for
    512      1.1  christos      continguous sections.  */
    513      1.1  christos   lower_sect = NULL;
    514      1.1  christos   bfd_map_over_sections (abfd, find_lowest_section, &lower_sect);
    515      1.1  christos   if (lower_sect == NULL)
    516      1.1  christos     {
    517      1.1  christos       warning (_("no loadable sections found in added symbol-file %s"),
    518      1.1  christos 	       bfd_get_filename (abfd));
    519      1.1  christos       lower_offset = 0;
    520      1.1  christos     }
    521      1.1  christos   else
    522      1.1  christos     lower_offset = bfd_section_vma (bfd_get_filename (abfd), lower_sect);
    523      1.1  christos 
    524      1.1  christos   /* Create ADDRS_TO_ABFD_ADDRS array to map the sections in ADDRS to sections
    525      1.1  christos      in ABFD.  Section names are not unique - there can be multiple sections of
    526      1.1  christos      the same name.  Also the sections of the same name do not have to be
    527      1.1  christos      adjacent to each other.  Some sections may be present only in one of the
    528      1.1  christos      files.  Even sections present in both files do not have to be in the same
    529      1.1  christos      order.
    530      1.1  christos 
    531      1.1  christos      Use stable sort by name for the sections in both files.  Then linearly
    532      1.1  christos      scan both lists matching as most of the entries as possible.  */
    533  1.1.1.6  christos 
    534  1.1.1.6  christos   std::vector<const struct other_sections *> addrs_sorted
    535      1.1  christos     = addrs_section_sort (*addrs);
    536  1.1.1.6  christos 
    537  1.1.1.6  christos   section_addr_info abfd_addrs = build_section_addr_info_from_bfd (abfd);
    538  1.1.1.6  christos   std::vector<const struct other_sections *> abfd_addrs_sorted
    539      1.1  christos     = addrs_section_sort (abfd_addrs);
    540      1.1  christos 
    541      1.1  christos   /* Now create ADDRS_TO_ABFD_ADDRS from ADDRS_SORTED and
    542      1.1  christos      ABFD_ADDRS_SORTED.  */
    543  1.1.1.6  christos 
    544  1.1.1.6  christos   std::vector<const struct other_sections *>
    545      1.1  christos     addrs_to_abfd_addrs (addrs->size (), nullptr);
    546  1.1.1.6  christos 
    547  1.1.1.6  christos   std::vector<const struct other_sections *>::iterator abfd_sorted_iter
    548  1.1.1.6  christos     = abfd_addrs_sorted.begin ();
    549      1.1  christos   for (const other_sections *sect : addrs_sorted)
    550  1.1.1.6  christos     {
    551      1.1  christos       const char *sect_name = addr_section_name (sect->name.c_str ());
    552  1.1.1.6  christos 
    553  1.1.1.6  christos       while (abfd_sorted_iter != abfd_addrs_sorted.end ()
    554      1.1  christos 	     && strcmp (addr_section_name ((*abfd_sorted_iter)->name.c_str ()),
    555  1.1.1.6  christos 			sect_name) < 0)
    556      1.1  christos 	abfd_sorted_iter++;
    557  1.1.1.6  christos 
    558  1.1.1.6  christos       if (abfd_sorted_iter != abfd_addrs_sorted.end ()
    559      1.1  christos 	  && strcmp (addr_section_name ((*abfd_sorted_iter)->name.c_str ()),
    560      1.1  christos 		     sect_name) == 0)
    561      1.1  christos 	{
    562      1.1  christos 	  int index_in_addrs;
    563      1.1  christos 
    564  1.1.1.6  christos 	  /* Make the found item directly addressable from ADDRS.  */
    565      1.1  christos 	  index_in_addrs = sect - addrs->data ();
    566  1.1.1.6  christos 	  gdb_assert (addrs_to_abfd_addrs[index_in_addrs] == NULL);
    567      1.1  christos 	  addrs_to_abfd_addrs[index_in_addrs] = *abfd_sorted_iter;
    568      1.1  christos 
    569  1.1.1.6  christos 	  /* Never use the same ABFD entry twice.  */
    570      1.1  christos 	  abfd_sorted_iter++;
    571      1.1  christos 	}
    572      1.1  christos     }
    573      1.1  christos 
    574      1.1  christos   /* Calculate offsets for the loadable sections.
    575      1.1  christos      FIXME! Sections must be in order of increasing loadable section
    576      1.1  christos      so that contiguous sections can use the lower-offset!!!
    577      1.1  christos 
    578      1.1  christos      Adjust offsets if the segments are not contiguous.
    579      1.1  christos      If the section is contiguous, its offset should be set to
    580      1.1  christos      the offset of the highest loadable section lower than it
    581      1.1  christos      (the loadable section directly below it in memory).
    582      1.1  christos      this_offset = lower_offset = lower_addr - lower_orig_addr */
    583  1.1.1.6  christos 
    584      1.1  christos   for (i = 0; i < addrs->size (); i++)
    585  1.1.1.6  christos     {
    586      1.1  christos       const struct other_sections *sect = addrs_to_abfd_addrs[i];
    587      1.1  christos 
    588      1.1  christos       if (sect)
    589      1.1  christos 	{
    590  1.1.1.6  christos 	  /* This is the index used by BFD.  */
    591      1.1  christos 	  (*addrs)[i].sectindex = sect->sectindex;
    592  1.1.1.6  christos 
    593      1.1  christos 	  if ((*addrs)[i].addr != 0)
    594  1.1.1.6  christos 	    {
    595  1.1.1.6  christos 	      (*addrs)[i].addr -= sect->addr;
    596      1.1  christos 	      lower_offset = (*addrs)[i].addr;
    597      1.1  christos 	    }
    598  1.1.1.6  christos 	  else
    599      1.1  christos 	    (*addrs)[i].addr = lower_offset;
    600      1.1  christos 	}
    601      1.1  christos       else
    602      1.1  christos 	{
    603  1.1.1.6  christos 	  /* addr_section_name transformation is not used for SECT_NAME.  */
    604      1.1  christos 	  const std::string &sect_name = (*addrs)[i].name;
    605      1.1  christos 
    606      1.1  christos 	  /* This section does not exist in ABFD, which is normally
    607      1.1  christos 	     unexpected and we want to issue a warning.
    608      1.1  christos 
    609      1.1  christos 	     However, the ELF prelinker does create a few sections which are
    610      1.1  christos 	     marked in the main executable as loadable (they are loaded in
    611      1.1  christos 	     memory from the DYNAMIC segment) and yet are not present in
    612      1.1  christos 	     separate debug info files.  This is fine, and should not cause
    613      1.1  christos 	     a warning.  Shared libraries contain just the section
    614      1.1  christos 	     ".gnu.liblist" but it is not marked as loadable there.  There is
    615      1.1  christos 	     no other way to identify them than by their name as the sections
    616      1.1  christos 	     created by prelink have no special flags.
    617      1.1  christos 
    618      1.1  christos 	     For the sections `.bss' and `.sbss' see addr_section_name.  */
    619  1.1.1.6  christos 
    620  1.1.1.6  christos 	  if (!(sect_name == ".gnu.liblist"
    621  1.1.1.6  christos 		|| sect_name == ".gnu.conflict"
    622      1.1  christos 		|| (sect_name == ".bss"
    623  1.1.1.6  christos 		    && i > 0
    624      1.1  christos 		    && (*addrs)[i - 1].name == ".dynbss"
    625  1.1.1.6  christos 		    && addrs_to_abfd_addrs[i - 1] != NULL)
    626      1.1  christos 		|| (sect_name == ".sbss"
    627  1.1.1.6  christos 		    && i > 0
    628      1.1  christos 		    && (*addrs)[i - 1].name == ".sdynbss"
    629  1.1.1.6  christos 		    && addrs_to_abfd_addrs[i - 1] != NULL)))
    630      1.1  christos 	    warning (_("section %s not found in %s"), sect_name.c_str (),
    631      1.1  christos 		     bfd_get_filename (abfd));
    632  1.1.1.6  christos 
    633  1.1.1.6  christos 	  (*addrs)[i].addr = 0;
    634      1.1  christos 	  (*addrs)[i].sectindex = -1;
    635      1.1  christos 	}
    636      1.1  christos     }
    637      1.1  christos }
    638      1.1  christos 
    639      1.1  christos /* Parse the user's idea of an offset for dynamic linking, into our idea
    640      1.1  christos    of how to represent it for fast symbol reading.  This is the default
    641      1.1  christos    version of the sym_fns.sym_offsets function for symbol readers that
    642      1.1  christos    don't need to do anything special.  It allocates a section_offsets table
    643      1.1  christos    for the objectfile OBJFILE and stuffs ADDR into all of the offsets.  */
    644      1.1  christos 
    645      1.1  christos void
    646  1.1.1.6  christos default_symfile_offsets (struct objfile *objfile,
    647      1.1  christos 			 const section_addr_info &addrs)
    648      1.1  christos {
    649      1.1  christos   objfile->num_sections = gdb_bfd_count_sections (objfile->obfd);
    650      1.1  christos   objfile->section_offsets = (struct section_offsets *)
    651      1.1  christos     obstack_alloc (&objfile->objfile_obstack,
    652      1.1  christos 		   SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
    653      1.1  christos   relative_addr_info_to_section_offsets (objfile->section_offsets,
    654      1.1  christos 					 objfile->num_sections, addrs);
    655      1.1  christos 
    656      1.1  christos   /* For relocatable files, all loadable sections will start at zero.
    657      1.1  christos      The zero is meaningless, so try to pick arbitrary addresses such
    658      1.1  christos      that no loadable sections overlap.  This algorithm is quadratic,
    659      1.1  christos      but the number of sections in a single object file is generally
    660      1.1  christos      small.  */
    661      1.1  christos   if ((bfd_get_file_flags (objfile->obfd) & (EXEC_P | DYNAMIC)) == 0)
    662      1.1  christos     {
    663      1.1  christos       struct place_section_arg arg;
    664      1.1  christos       bfd *abfd = objfile->obfd;
    665      1.1  christos       asection *cur_sec;
    666      1.1  christos 
    667      1.1  christos       for (cur_sec = abfd->sections; cur_sec != NULL; cur_sec = cur_sec->next)
    668      1.1  christos 	/* We do not expect this to happen; just skip this step if the
    669      1.1  christos 	   relocatable file has a section with an assigned VMA.  */
    670      1.1  christos 	if (bfd_section_vma (abfd, cur_sec) != 0)
    671      1.1  christos 	  break;
    672      1.1  christos 
    673      1.1  christos       if (cur_sec == NULL)
    674      1.1  christos 	{
    675      1.1  christos 	  CORE_ADDR *offsets = objfile->section_offsets->offsets;
    676      1.1  christos 
    677      1.1  christos 	  /* Pick non-overlapping offsets for sections the user did not
    678      1.1  christos 	     place explicitly.  */
    679      1.1  christos 	  arg.offsets = objfile->section_offsets;
    680      1.1  christos 	  arg.lowest = 0;
    681      1.1  christos 	  bfd_map_over_sections (objfile->obfd, place_section, &arg);
    682      1.1  christos 
    683      1.1  christos 	  /* Correctly filling in the section offsets is not quite
    684      1.1  christos 	     enough.  Relocatable files have two properties that
    685      1.1  christos 	     (most) shared objects do not:
    686      1.1  christos 
    687      1.1  christos 	     - Their debug information will contain relocations.  Some
    688      1.1  christos 	     shared libraries do also, but many do not, so this can not
    689      1.1  christos 	     be assumed.
    690      1.1  christos 
    691      1.1  christos 	     - If there are multiple code sections they will be loaded
    692      1.1  christos 	     at different relative addresses in memory than they are
    693      1.1  christos 	     in the objfile, since all sections in the file will start
    694      1.1  christos 	     at address zero.
    695      1.1  christos 
    696      1.1  christos 	     Because GDB has very limited ability to map from an
    697      1.1  christos 	     address in debug info to the correct code section,
    698      1.1  christos 	     it relies on adding SECT_OFF_TEXT to things which might be
    699      1.1  christos 	     code.  If we clear all the section offsets, and set the
    700      1.1  christos 	     section VMAs instead, then symfile_relocate_debug_section
    701      1.1  christos 	     will return meaningful debug information pointing at the
    702      1.1  christos 	     correct sections.
    703      1.1  christos 
    704      1.1  christos 	     GDB has too many different data structures for section
    705      1.1  christos 	     addresses - a bfd, objfile, and so_list all have section
    706      1.1  christos 	     tables, as does exec_ops.  Some of these could probably
    707      1.1  christos 	     be eliminated.  */
    708      1.1  christos 
    709      1.1  christos 	  for (cur_sec = abfd->sections; cur_sec != NULL;
    710      1.1  christos 	       cur_sec = cur_sec->next)
    711      1.1  christos 	    {
    712      1.1  christos 	      if ((bfd_get_section_flags (abfd, cur_sec) & SEC_ALLOC) == 0)
    713      1.1  christos 		continue;
    714      1.1  christos 
    715      1.1  christos 	      bfd_set_section_vma (abfd, cur_sec, offsets[cur_sec->index]);
    716      1.1  christos 	      exec_set_section_address (bfd_get_filename (abfd),
    717      1.1  christos 					cur_sec->index,
    718      1.1  christos 					offsets[cur_sec->index]);
    719      1.1  christos 	      offsets[cur_sec->index] = 0;
    720      1.1  christos 	    }
    721      1.1  christos 	}
    722      1.1  christos     }
    723      1.1  christos 
    724      1.1  christos   /* Remember the bfd indexes for the .text, .data, .bss and
    725      1.1  christos      .rodata sections.  */
    726      1.1  christos   init_objfile_sect_indices (objfile);
    727      1.1  christos }
    728      1.1  christos 
    729      1.1  christos /* Divide the file into segments, which are individual relocatable units.
    730      1.1  christos    This is the default version of the sym_fns.sym_segments function for
    731      1.1  christos    symbol readers that do not have an explicit representation of segments.
    732      1.1  christos    It assumes that object files do not have segments, and fully linked
    733      1.1  christos    files have a single segment.  */
    734      1.1  christos 
    735      1.1  christos struct symfile_segment_data *
    736      1.1  christos default_symfile_segments (bfd *abfd)
    737      1.1  christos {
    738      1.1  christos   int num_sections, i;
    739      1.1  christos   asection *sect;
    740      1.1  christos   struct symfile_segment_data *data;
    741      1.1  christos   CORE_ADDR low, high;
    742      1.1  christos 
    743      1.1  christos   /* Relocatable files contain enough information to position each
    744      1.1  christos      loadable section independently; they should not be relocated
    745      1.1  christos      in segments.  */
    746      1.1  christos   if ((bfd_get_file_flags (abfd) & (EXEC_P | DYNAMIC)) == 0)
    747      1.1  christos     return NULL;
    748      1.1  christos 
    749      1.1  christos   /* Make sure there is at least one loadable section in the file.  */
    750      1.1  christos   for (sect = abfd->sections; sect != NULL; sect = sect->next)
    751      1.1  christos     {
    752      1.1  christos       if ((bfd_get_section_flags (abfd, sect) & SEC_ALLOC) == 0)
    753      1.1  christos 	continue;
    754      1.1  christos 
    755      1.1  christos       break;
    756      1.1  christos     }
    757      1.1  christos   if (sect == NULL)
    758      1.1  christos     return NULL;
    759      1.1  christos 
    760      1.1  christos   low = bfd_get_section_vma (abfd, sect);
    761      1.1  christos   high = low + bfd_get_section_size (sect);
    762  1.1.1.2  christos 
    763      1.1  christos   data = XCNEW (struct symfile_segment_data);
    764  1.1.1.2  christos   data->num_segments = 1;
    765  1.1.1.2  christos   data->segment_bases = XCNEW (CORE_ADDR);
    766      1.1  christos   data->segment_sizes = XCNEW (CORE_ADDR);
    767      1.1  christos 
    768  1.1.1.2  christos   num_sections = bfd_count_sections (abfd);
    769      1.1  christos   data->segment_info = XCNEWVEC (int, num_sections);
    770      1.1  christos 
    771      1.1  christos   for (i = 0, sect = abfd->sections; sect != NULL; i++, sect = sect->next)
    772      1.1  christos     {
    773      1.1  christos       CORE_ADDR vma;
    774      1.1  christos 
    775      1.1  christos       if ((bfd_get_section_flags (abfd, sect) & SEC_ALLOC) == 0)
    776      1.1  christos 	continue;
    777      1.1  christos 
    778      1.1  christos       vma = bfd_get_section_vma (abfd, sect);
    779      1.1  christos       if (vma < low)
    780      1.1  christos 	low = vma;
    781      1.1  christos       if (vma + bfd_get_section_size (sect) > high)
    782      1.1  christos 	high = vma + bfd_get_section_size (sect);
    783      1.1  christos 
    784      1.1  christos       data->segment_info[i] = 1;
    785      1.1  christos     }
    786      1.1  christos 
    787      1.1  christos   data->segment_bases[0] = low;
    788      1.1  christos   data->segment_sizes[0] = high - low;
    789      1.1  christos 
    790      1.1  christos   return data;
    791      1.1  christos }
    792      1.1  christos 
    793      1.1  christos /* This is a convenience function to call sym_read for OBJFILE and
    794      1.1  christos    possibly force the partial symbols to be read.  */
    795      1.1  christos 
    796  1.1.1.5  christos static void
    797      1.1  christos read_symbols (struct objfile *objfile, symfile_add_flags add_flags)
    798      1.1  christos {
    799  1.1.1.6  christos   (*objfile->sf->sym_read) (objfile, add_flags);
    800      1.1  christos   objfile->per_bfd->minsyms_read = true;
    801      1.1  christos 
    802      1.1  christos   /* find_separate_debug_file_in_section should be called only if there is
    803      1.1  christos      single binary with no existing separate debug info file.  */
    804      1.1  christos   if (!objfile_has_partial_symbols (objfile)
    805      1.1  christos       && objfile->separate_debug_objfile == NULL
    806      1.1  christos       && objfile->separate_debug_objfile_backlink == NULL)
    807  1.1.1.5  christos     {
    808      1.1  christos       gdb_bfd_ref_ptr abfd (find_separate_debug_file_in_section (objfile));
    809      1.1  christos 
    810      1.1  christos       if (abfd != NULL)
    811      1.1  christos 	{
    812      1.1  christos 	  /* find_separate_debug_file_in_section uses the same filename for the
    813      1.1  christos 	     virtual section-as-bfd like the bfd filename containing the
    814      1.1  christos 	     section.  Therefore use also non-canonical name form for the same
    815  1.1.1.6  christos 	     file containing the section.  */
    816  1.1.1.6  christos 	  symbol_file_add_separate (abfd.get (),
    817  1.1.1.6  christos 				    bfd_get_filename (abfd.get ()),
    818      1.1  christos 				    add_flags | SYMFILE_NOT_FILENAME, objfile);
    819      1.1  christos 	}
    820      1.1  christos     }
    821      1.1  christos   if ((add_flags & SYMFILE_NO_READ) == 0)
    822      1.1  christos     require_partial_symbols (objfile, 0);
    823      1.1  christos }
    824      1.1  christos 
    825      1.1  christos /* Initialize entry point information for this objfile.  */
    826      1.1  christos 
    827      1.1  christos static void
    828      1.1  christos init_entry_point_info (struct objfile *objfile)
    829  1.1.1.2  christos {
    830  1.1.1.2  christos   struct entry_info *ei = &objfile->per_bfd->ei;
    831  1.1.1.2  christos 
    832  1.1.1.2  christos   if (ei->initialized)
    833  1.1.1.2  christos     return;
    834  1.1.1.2  christos   ei->initialized = 1;
    835      1.1  christos 
    836      1.1  christos   /* Save startup file's range of PC addresses to help blockframe.c
    837      1.1  christos      decide where the bottom of the stack is.  */
    838      1.1  christos 
    839      1.1  christos   if (bfd_get_file_flags (objfile->obfd) & EXEC_P)
    840      1.1  christos     {
    841      1.1  christos       /* Executable file -- record its entry point so we'll recognize
    842  1.1.1.2  christos          the startup file because it contains the entry point.  */
    843  1.1.1.2  christos       ei->entry_point = bfd_get_start_address (objfile->obfd);
    844      1.1  christos       ei->entry_point_p = 1;
    845      1.1  christos     }
    846      1.1  christos   else if (bfd_get_file_flags (objfile->obfd) & DYNAMIC
    847      1.1  christos 	   && bfd_get_start_address (objfile->obfd) != 0)
    848      1.1  christos     {
    849      1.1  christos       /* Some shared libraries may have entry points set and be
    850      1.1  christos 	 runnable.  There's no clear way to indicate this, so just check
    851  1.1.1.2  christos 	 for values other than zero.  */
    852  1.1.1.2  christos       ei->entry_point = bfd_get_start_address (objfile->obfd);
    853      1.1  christos       ei->entry_point_p = 1;
    854      1.1  christos     }
    855      1.1  christos   else
    856      1.1  christos     {
    857  1.1.1.2  christos       /* Examination of non-executable.o files.  Short-circuit this stuff.  */
    858      1.1  christos       ei->entry_point_p = 0;
    859      1.1  christos     }
    860  1.1.1.2  christos 
    861      1.1  christos   if (ei->entry_point_p)
    862  1.1.1.2  christos     {
    863  1.1.1.2  christos       struct obj_section *osect;
    864  1.1.1.2  christos       CORE_ADDR entry_point =  ei->entry_point;
    865      1.1  christos       int found;
    866      1.1  christos 
    867      1.1  christos       /* Make certain that the address points at real code, and not a
    868      1.1  christos 	 function descriptor.  */
    869      1.1  christos       entry_point
    870      1.1  christos 	= gdbarch_convert_from_func_ptr_addr (get_objfile_arch (objfile),
    871  1.1.1.6  christos 					      entry_point,
    872      1.1  christos 					      current_top_target ());
    873      1.1  christos 
    874      1.1  christos       /* Remove any ISA markers, so that this matches entries in the
    875  1.1.1.2  christos 	 symbol table.  */
    876      1.1  christos       ei->entry_point
    877  1.1.1.2  christos 	= gdbarch_addr_bits_remove (get_objfile_arch (objfile), entry_point);
    878  1.1.1.2  christos 
    879  1.1.1.2  christos       found = 0;
    880  1.1.1.2  christos       ALL_OBJFILE_OSECTIONS (objfile, osect)
    881  1.1.1.2  christos 	{
    882  1.1.1.2  christos 	  struct bfd_section *sect = osect->the_bfd_section;
    883  1.1.1.2  christos 
    884  1.1.1.2  christos 	  if (entry_point >= bfd_get_section_vma (objfile->obfd, sect)
    885  1.1.1.2  christos 	      && entry_point < (bfd_get_section_vma (objfile->obfd, sect)
    886  1.1.1.2  christos 				+ bfd_get_section_size (sect)))
    887  1.1.1.2  christos 	    {
    888  1.1.1.2  christos 	      ei->the_bfd_section_index
    889  1.1.1.2  christos 		= gdb_bfd_section_index (objfile->obfd, sect);
    890  1.1.1.2  christos 	      found = 1;
    891  1.1.1.2  christos 	      break;
    892  1.1.1.2  christos 	    }
    893  1.1.1.2  christos 	}
    894  1.1.1.2  christos 
    895  1.1.1.2  christos       if (!found)
    896      1.1  christos 	ei->the_bfd_section_index = SECT_OFF_TEXT (objfile);
    897      1.1  christos     }
    898      1.1  christos }
    899      1.1  christos 
    900      1.1  christos /* Process a symbol file, as either the main file or as a dynamically
    901      1.1  christos    loaded file.
    902      1.1  christos 
    903      1.1  christos    This function does not set the OBJFILE's entry-point info.
    904      1.1  christos 
    905      1.1  christos    OBJFILE is where the symbols are to be read from.
    906      1.1  christos 
    907      1.1  christos    ADDRS is the list of section load addresses.  If the user has given
    908      1.1  christos    an 'add-symbol-file' command, then this is the list of offsets and
    909      1.1  christos    addresses he or she provided as arguments to the command; or, if
    910      1.1  christos    we're handling a shared library, these are the actual addresses the
    911      1.1  christos    sections are loaded at, according to the inferior's dynamic linker
    912      1.1  christos    (as gleaned by GDB's shared library code).  We convert each address
    913      1.1  christos    into an offset from the section VMA's as it appears in the object
    914      1.1  christos    file, and then call the file's sym_offsets function to convert this
    915  1.1.1.6  christos    into a format-specific offset table --- a `struct section_offsets'.
    916  1.1.1.6  christos    The sectindex field is used to control the ordering of sections
    917  1.1.1.6  christos    with the same name.  Upon return, it is updated to contain the
    918      1.1  christos    correspondig BFD section index, or -1 if the section was not found.
    919      1.1  christos 
    920      1.1  christos    ADD_FLAGS encodes verbosity level, whether this is main symbol or
    921      1.1  christos    an extra symbol file such as dynamically loaded code, and wether
    922      1.1  christos    breakpoint reset should be deferred.  */
    923      1.1  christos 
    924      1.1  christos static void
    925  1.1.1.6  christos syms_from_objfile_1 (struct objfile *objfile,
    926  1.1.1.5  christos 		     section_addr_info *addrs,
    927      1.1  christos 		     symfile_add_flags add_flags)
    928  1.1.1.6  christos {
    929      1.1  christos   section_addr_info local_addr;
    930      1.1  christos   const int mainline = add_flags & SYMFILE_MAINLINE;
    931      1.1  christos 
    932      1.1  christos   objfile_set_sym_fns (objfile, find_sym_fns (objfile->obfd));
    933      1.1  christos 
    934      1.1  christos   if (objfile->sf == NULL)
    935      1.1  christos     {
    936      1.1  christos       /* No symbols to load, but we still need to make sure
    937      1.1  christos 	 that the section_offsets table is allocated.  */
    938      1.1  christos       int num_sections = gdb_bfd_count_sections (objfile->obfd);
    939      1.1  christos       size_t size = SIZEOF_N_SECTION_OFFSETS (num_sections);
    940      1.1  christos 
    941      1.1  christos       objfile->num_sections = num_sections;
    942  1.1.1.4  christos       objfile->section_offsets
    943  1.1.1.4  christos 	= (struct section_offsets *) obstack_alloc (&objfile->objfile_obstack,
    944      1.1  christos 						    size);
    945      1.1  christos       memset (objfile->section_offsets, 0, size);
    946      1.1  christos       return;
    947      1.1  christos     }
    948      1.1  christos 
    949      1.1  christos   /* Make sure that partially constructed symbol tables will be cleaned up
    950  1.1.1.6  christos      if an error occurs during symbol reading.  */
    951  1.1.1.6  christos   gdb::optional<clear_symtab_users_cleanup> defer_clear_users;
    952  1.1.1.6  christos 
    953      1.1  christos   std::unique_ptr<struct objfile> objfile_holder (objfile);
    954      1.1  christos 
    955      1.1  christos   /* If ADDRS is NULL, put together a dummy address list.
    956      1.1  christos      We now establish the convention that an addr of zero means
    957      1.1  christos      no load address was specified.  */
    958  1.1.1.6  christos   if (! addrs)
    959      1.1  christos     addrs = &local_addr;
    960      1.1  christos 
    961      1.1  christos   if (mainline)
    962      1.1  christos     {
    963      1.1  christos       /* We will modify the main symbol table, make sure that all its users
    964  1.1.1.6  christos          will be cleaned up if an error occurs during symbol reading.  */
    965      1.1  christos       defer_clear_users.emplace ((symfile_add_flag) 0);
    966      1.1  christos 
    967      1.1  christos       /* Since no error yet, throw away the old symbol table.  */
    968      1.1  christos 
    969      1.1  christos       if (symfile_objfile != NULL)
    970  1.1.1.6  christos 	{
    971      1.1  christos 	  delete symfile_objfile;
    972      1.1  christos 	  gdb_assert (symfile_objfile == NULL);
    973      1.1  christos 	}
    974      1.1  christos 
    975      1.1  christos       /* Currently we keep symbols from the add-symbol-file command.
    976      1.1  christos          If the user wants to get rid of them, they should do "symbol-file"
    977      1.1  christos          without arguments first.  Not sure this is the best behavior
    978      1.1  christos          (PR 2207).  */
    979      1.1  christos 
    980      1.1  christos       (*objfile->sf->sym_new_init) (objfile);
    981      1.1  christos     }
    982      1.1  christos 
    983      1.1  christos   /* Convert addr into an offset rather than an absolute address.
    984      1.1  christos      We find the lowest address of a loaded segment in the objfile,
    985      1.1  christos      and assume that <addr> is where that got loaded.
    986      1.1  christos 
    987      1.1  christos      We no longer warn if the lowest section is not a text segment (as
    988  1.1.1.6  christos      happens for the PA64 port.  */
    989      1.1  christos   if (addrs->size () > 0)
    990      1.1  christos     addr_info_make_relative (addrs, objfile->obfd);
    991      1.1  christos 
    992      1.1  christos   /* Initialize symbol reading routines for this objfile, allow complaints to
    993      1.1  christos      appear for this new file, and record how verbose to be, then do the
    994      1.1  christos      initial symbol reading for this file.  */
    995      1.1  christos 
    996  1.1.1.6  christos   (*objfile->sf->sym_init) (objfile);
    997      1.1  christos   clear_complaints ();
    998  1.1.1.6  christos 
    999      1.1  christos   (*objfile->sf->sym_offsets) (objfile, *addrs);
   1000      1.1  christos 
   1001      1.1  christos   read_symbols (objfile, add_flags);
   1002      1.1  christos 
   1003      1.1  christos   /* Discard cleanups as symbol reading was successful.  */
   1004  1.1.1.6  christos 
   1005  1.1.1.6  christos   objfile_holder.release ();
   1006  1.1.1.6  christos   if (defer_clear_users)
   1007      1.1  christos     defer_clear_users->release ();
   1008      1.1  christos }
   1009      1.1  christos 
   1010      1.1  christos /* Same as syms_from_objfile_1, but also initializes the objfile
   1011      1.1  christos    entry-point info.  */
   1012      1.1  christos 
   1013      1.1  christos static void
   1014  1.1.1.6  christos syms_from_objfile (struct objfile *objfile,
   1015  1.1.1.5  christos 		   section_addr_info *addrs,
   1016      1.1  christos 		   symfile_add_flags add_flags)
   1017      1.1  christos {
   1018      1.1  christos   syms_from_objfile_1 (objfile, addrs, add_flags);
   1019      1.1  christos   init_entry_point_info (objfile);
   1020      1.1  christos }
   1021      1.1  christos 
   1022      1.1  christos /* Perform required actions after either reading in the initial
   1023      1.1  christos    symbols for a new objfile, or mapping in the symbols from a reusable
   1024      1.1  christos    objfile.  ADD_FLAGS is a bitmask of enum symfile_add_flags.  */
   1025  1.1.1.3  christos 
   1026  1.1.1.5  christos static void
   1027      1.1  christos finish_new_objfile (struct objfile *objfile, symfile_add_flags add_flags)
   1028      1.1  christos {
   1029      1.1  christos   /* If this is the main symbol file we have to clean up all users of the
   1030      1.1  christos      old main symbol file.  Otherwise it is sufficient to fixup all the
   1031      1.1  christos      breakpoints that may have been redefined by this symbol file.  */
   1032      1.1  christos   if (add_flags & SYMFILE_MAINLINE)
   1033      1.1  christos     {
   1034      1.1  christos       /* OK, make it the "real" symbol file.  */
   1035      1.1  christos       symfile_objfile = objfile;
   1036      1.1  christos 
   1037      1.1  christos       clear_symtab_users (add_flags);
   1038      1.1  christos     }
   1039      1.1  christos   else if ((add_flags & SYMFILE_DEFER_BP_RESET) == 0)
   1040      1.1  christos     {
   1041      1.1  christos       breakpoint_re_set ();
   1042      1.1  christos     }
   1043      1.1  christos 
   1044  1.1.1.6  christos   /* We're done reading the symbol file; finish off complaints.  */
   1045      1.1  christos   clear_complaints ();
   1046      1.1  christos }
   1047      1.1  christos 
   1048      1.1  christos /* Process a symbol file, as either the main file or as a dynamically
   1049      1.1  christos    loaded file.
   1050      1.1  christos 
   1051      1.1  christos    ABFD is a BFD already open on the file, as from symfile_bfd_open.
   1052      1.1  christos    A new reference is acquired by this function.
   1053  1.1.1.6  christos 
   1054      1.1  christos    For NAME description see the objfile constructor.
   1055      1.1  christos 
   1056      1.1  christos    ADD_FLAGS encodes verbosity, whether this is main symbol file or
   1057      1.1  christos    extra, such as dynamically loaded code, and what to do with breakpoins.
   1058      1.1  christos 
   1059      1.1  christos    ADDRS is as described for syms_from_objfile_1, above.
   1060      1.1  christos    ADDRS is ignored when SYMFILE_MAINLINE bit is set in ADD_FLAGS.
   1061      1.1  christos 
   1062      1.1  christos    PARENT is the original objfile if ABFD is a separate debug info file.
   1063      1.1  christos    Otherwise PARENT is NULL.
   1064      1.1  christos 
   1065      1.1  christos    Upon success, returns a pointer to the objfile that was added.
   1066      1.1  christos    Upon failure, jumps back to command level (never returns).  */
   1067      1.1  christos 
   1068  1.1.1.5  christos static struct objfile *
   1069  1.1.1.5  christos symbol_file_add_with_addrs (bfd *abfd, const char *name,
   1070  1.1.1.6  christos 			    symfile_add_flags add_flags,
   1071  1.1.1.5  christos 			    section_addr_info *addrs,
   1072      1.1  christos 			    objfile_flags flags, struct objfile *parent)
   1073      1.1  christos {
   1074      1.1  christos   struct objfile *objfile;
   1075      1.1  christos   const int from_tty = add_flags & SYMFILE_VERBOSE;
   1076  1.1.1.2  christos   const int mainline = add_flags & SYMFILE_MAINLINE;
   1077      1.1  christos   const int should_print = (print_symbol_loading_p (from_tty, mainline, 1)
   1078      1.1  christos 			    && (readnow_symbol_files
   1079      1.1  christos 				|| (add_flags & SYMFILE_NO_READ) == 0));
   1080      1.1  christos 
   1081      1.1  christos   if (readnow_symbol_files)
   1082      1.1  christos     {
   1083      1.1  christos       flags |= OBJF_READNOW;
   1084      1.1  christos       add_flags &= ~SYMFILE_NO_READ;
   1085  1.1.1.6  christos     }
   1086  1.1.1.6  christos   else if (readnever_symbol_files
   1087  1.1.1.6  christos 	   || (parent != NULL && (parent->flags & OBJF_READNEVER)))
   1088  1.1.1.6  christos     {
   1089  1.1.1.6  christos       flags |= OBJF_READNEVER;
   1090  1.1.1.6  christos       add_flags |= SYMFILE_NO_READ;
   1091  1.1.1.6  christos     }
   1092  1.1.1.6  christos   if ((add_flags & SYMFILE_NOT_FILENAME) != 0)
   1093      1.1  christos     flags |= OBJF_NOT_FILENAME;
   1094      1.1  christos 
   1095      1.1  christos   /* Give user a chance to burp if we'd be
   1096      1.1  christos      interactively wiping out any existing symbols.  */
   1097      1.1  christos 
   1098      1.1  christos   if ((have_full_symbols () || have_partial_symbols ())
   1099      1.1  christos       && mainline
   1100      1.1  christos       && from_tty
   1101      1.1  christos       && !query (_("Load new symbol table from \"%s\"? "), name))
   1102      1.1  christos     error (_("Not confirmed."));
   1103  1.1.1.5  christos 
   1104  1.1.1.5  christos   if (mainline)
   1105  1.1.1.6  christos     flags |= OBJF_MAINLINE;
   1106      1.1  christos   objfile = new struct objfile (abfd, name, flags);
   1107      1.1  christos 
   1108      1.1  christos   if (parent)
   1109      1.1  christos     add_separate_debug_objfile (objfile, parent);
   1110      1.1  christos 
   1111      1.1  christos   /* We either created a new mapped symbol table, mapped an existing
   1112      1.1  christos      symbol table file which has not had initial symbol reading
   1113      1.1  christos      performed, or need to read an unmapped symbol table.  */
   1114      1.1  christos   if (should_print)
   1115      1.1  christos     {
   1116      1.1  christos       if (deprecated_pre_add_symbol_hook)
   1117      1.1  christos 	deprecated_pre_add_symbol_hook (name);
   1118      1.1  christos       else
   1119  1.1.1.6  christos 	{
   1120  1.1.1.6  christos 	  puts_filtered (_("Reading symbols from "));
   1121  1.1.1.6  christos 	  fputs_styled (name, file_name_style.style (), gdb_stdout);
   1122      1.1  christos 	  puts_filtered ("...\n");
   1123      1.1  christos 	}
   1124      1.1  christos     }
   1125      1.1  christos   syms_from_objfile (objfile, addrs, add_flags);
   1126      1.1  christos 
   1127      1.1  christos   /* We now have at least a partial symbol table.  Check to see if the
   1128      1.1  christos      user requested that all symbols be read on initial access via either
   1129      1.1  christos      the gdb startup command line or on a per symbol file basis.  Expand
   1130      1.1  christos      all partial symbol tables for this objfile if so.  */
   1131      1.1  christos 
   1132      1.1  christos   if ((flags & OBJF_READNOW))
   1133      1.1  christos     {
   1134  1.1.1.6  christos       if (should_print)
   1135      1.1  christos 	printf_filtered (_("Expanding full symbols from %s...\n"), name);
   1136      1.1  christos 
   1137      1.1  christos       if (objfile->sf)
   1138      1.1  christos 	objfile->sf->qf->expand_all_symtabs (objfile);
   1139      1.1  christos     }
   1140  1.1.1.6  christos 
   1141  1.1.1.6  christos   /* Note that we only print a message if we have no symbols and have
   1142  1.1.1.6  christos      no separate debug file.  If there is a separate debug file which
   1143  1.1.1.6  christos      does not have symbols, we'll have emitted this message for that
   1144  1.1.1.6  christos      file, and so printing it twice is just redundant.  */
   1145  1.1.1.6  christos   if (should_print && !objfile_has_symbols (objfile)
   1146  1.1.1.6  christos       && objfile->separate_debug_objfile == nullptr)
   1147      1.1  christos     printf_filtered (_("(No debugging symbols found in %s)\n"), name);
   1148      1.1  christos 
   1149      1.1  christos   if (should_print)
   1150      1.1  christos     {
   1151      1.1  christos       if (deprecated_post_add_symbol_hook)
   1152      1.1  christos 	deprecated_post_add_symbol_hook ();
   1153      1.1  christos     }
   1154      1.1  christos 
   1155      1.1  christos   /* We print some messages regardless of whether 'from_tty ||
   1156      1.1  christos      info_verbose' is true, so make sure they go out at the right
   1157      1.1  christos      time.  */
   1158      1.1  christos   gdb_flush (gdb_stdout);
   1159      1.1  christos 
   1160      1.1  christos   if (objfile->sf == NULL)
   1161  1.1.1.6  christos     {
   1162      1.1  christos       gdb::observers::new_objfile.notify (objfile);
   1163      1.1  christos       return objfile;	/* No symbols.  */
   1164      1.1  christos     }
   1165  1.1.1.3  christos 
   1166      1.1  christos   finish_new_objfile (objfile, add_flags);
   1167  1.1.1.6  christos 
   1168      1.1  christos   gdb::observers::new_objfile.notify (objfile);
   1169      1.1  christos 
   1170      1.1  christos   bfd_cache_close_all ();
   1171      1.1  christos   return (objfile);
   1172      1.1  christos }
   1173      1.1  christos 
   1174  1.1.1.6  christos /* Add BFD as a separate debug file for OBJFILE.  For NAME description
   1175      1.1  christos    see the objfile constructor.  */
   1176      1.1  christos 
   1177  1.1.1.5  christos void
   1178  1.1.1.5  christos symbol_file_add_separate (bfd *bfd, const char *name,
   1179      1.1  christos 			  symfile_add_flags symfile_flags,
   1180      1.1  christos 			  struct objfile *objfile)
   1181      1.1  christos {
   1182      1.1  christos   /* Create section_addr_info.  We can't directly use offsets from OBJFILE
   1183      1.1  christos      because sections of BFD may not match sections of OBJFILE and because
   1184  1.1.1.6  christos      vma may have been modified by tools such as prelink.  */
   1185      1.1  christos   section_addr_info sap = build_section_addr_info_from_objfile (objfile);
   1186  1.1.1.4  christos 
   1187  1.1.1.6  christos   symbol_file_add_with_addrs
   1188      1.1  christos     (bfd, name, symfile_flags, &sap,
   1189      1.1  christos      objfile->flags & (OBJF_REORDERED | OBJF_SHARED | OBJF_READNOW
   1190      1.1  christos 		       | OBJF_USERLOADED),
   1191      1.1  christos      objfile);
   1192      1.1  christos }
   1193      1.1  christos 
   1194      1.1  christos /* Process the symbol file ABFD, as either the main file or as a
   1195      1.1  christos    dynamically loaded file.
   1196      1.1  christos    See symbol_file_add_with_addrs's comments for details.  */
   1197      1.1  christos 
   1198  1.1.1.5  christos struct objfile *
   1199  1.1.1.5  christos symbol_file_add_from_bfd (bfd *abfd, const char *name,
   1200  1.1.1.6  christos 			  symfile_add_flags add_flags,
   1201  1.1.1.5  christos 			  section_addr_info *addrs,
   1202      1.1  christos                           objfile_flags flags, struct objfile *parent)
   1203      1.1  christos {
   1204      1.1  christos   return symbol_file_add_with_addrs (abfd, name, add_flags, addrs, flags,
   1205      1.1  christos 				     parent);
   1206      1.1  christos }
   1207      1.1  christos 
   1208      1.1  christos /* Process a symbol file, as either the main file or as a dynamically
   1209      1.1  christos    loaded file.  See symbol_file_add_with_addrs's comments for details.  */
   1210      1.1  christos 
   1211  1.1.1.5  christos struct objfile *
   1212  1.1.1.6  christos symbol_file_add (const char *name, symfile_add_flags add_flags,
   1213      1.1  christos 		 section_addr_info *addrs, objfile_flags flags)
   1214  1.1.1.5  christos {
   1215      1.1  christos   gdb_bfd_ref_ptr bfd (symfile_bfd_open (name));
   1216  1.1.1.5  christos 
   1217  1.1.1.5  christos   return symbol_file_add_from_bfd (bfd.get (), name, add_flags, addrs,
   1218      1.1  christos 				   flags, NULL);
   1219      1.1  christos }
   1220      1.1  christos 
   1221      1.1  christos /* Call symbol_file_add() with default values and update whatever is
   1222      1.1  christos    affected by the loading of a new main().
   1223      1.1  christos    Used when the file is supplied in the gdb command line
   1224      1.1  christos    and by some targets with special loading requirements.
   1225      1.1  christos    The auxiliary function, symbol_file_add_main_1(), has the flags
   1226      1.1  christos    argument for the switches that can only be specified in the symbol_file
   1227      1.1  christos    command itself.  */
   1228      1.1  christos 
   1229  1.1.1.5  christos void
   1230      1.1  christos symbol_file_add_main (const char *args, symfile_add_flags add_flags)
   1231  1.1.1.6  christos {
   1232      1.1  christos   symbol_file_add_main_1 (args, add_flags, 0, 0);
   1233      1.1  christos }
   1234      1.1  christos 
   1235  1.1.1.5  christos static void
   1236  1.1.1.6  christos symbol_file_add_main_1 (const char *args, symfile_add_flags add_flags,
   1237      1.1  christos 			objfile_flags flags, CORE_ADDR reloff)
   1238  1.1.1.5  christos {
   1239      1.1  christos   add_flags |= current_inferior ()->symfile_flags | SYMFILE_MAINLINE;
   1240  1.1.1.6  christos 
   1241  1.1.1.6  christos   struct objfile *objfile = symbol_file_add (args, add_flags, NULL, flags);
   1242  1.1.1.6  christos   if (reloff != 0)
   1243      1.1  christos     objfile_rebase (objfile, reloff);
   1244      1.1  christos 
   1245      1.1  christos   /* Getting new symbols may change our opinion about
   1246      1.1  christos      what is frameless.  */
   1247      1.1  christos   reinit_frame_cache ();
   1248  1.1.1.5  christos 
   1249      1.1  christos   if ((add_flags & SYMFILE_NO_READ) == 0)
   1250      1.1  christos     set_initial_language ();
   1251      1.1  christos }
   1252      1.1  christos 
   1253      1.1  christos void
   1254      1.1  christos symbol_file_clear (int from_tty)
   1255      1.1  christos {
   1256      1.1  christos   if ((have_full_symbols () || have_partial_symbols ())
   1257      1.1  christos       && from_tty
   1258      1.1  christos       && (symfile_objfile
   1259      1.1  christos 	  ? !query (_("Discard symbol table from `%s'? "),
   1260      1.1  christos 		    objfile_name (symfile_objfile))
   1261      1.1  christos 	  : !query (_("Discard symbol table? "))))
   1262      1.1  christos     error (_("Not confirmed."));
   1263      1.1  christos 
   1264      1.1  christos   /* solib descriptors may have handles to objfiles.  Wipe them before their
   1265      1.1  christos      objfiles get stale by free_all_objfiles.  */
   1266      1.1  christos   no_shared_libraries (NULL, from_tty);
   1267      1.1  christos 
   1268      1.1  christos   free_all_objfiles ();
   1269      1.1  christos 
   1270      1.1  christos   gdb_assert (symfile_objfile == NULL);
   1271  1.1.1.6  christos   if (from_tty)
   1272      1.1  christos     printf_filtered (_("No symbol file now.\n"));
   1273      1.1  christos }
   1274  1.1.1.6  christos 
   1275  1.1.1.6  christos /* See symfile.h.  */
   1276  1.1.1.6  christos 
   1277  1.1.1.6  christos int separate_debug_file_debug = 0;
   1278      1.1  christos 
   1279  1.1.1.6  christos static int
   1280      1.1  christos separate_debug_file_exists (const std::string &name, unsigned long crc,
   1281      1.1  christos 			    struct objfile *parent_objfile)
   1282      1.1  christos {
   1283      1.1  christos   unsigned long file_crc;
   1284      1.1  christos   int file_crc_p;
   1285      1.1  christos   struct stat parent_stat, abfd_stat;
   1286      1.1  christos   int verified_as_different;
   1287      1.1  christos 
   1288      1.1  christos   /* Find a separate debug info file as if symbols would be present in
   1289      1.1  christos      PARENT_OBJFILE itself this function would not be called.  .gnu_debuglink
   1290      1.1  christos      section can contain just the basename of PARENT_OBJFILE without any
   1291      1.1  christos      ".debug" suffix as "/usr/lib/debug/path/to/file" is a separate tree where
   1292      1.1  christos      the separate debug infos with the same basename can exist.  */
   1293  1.1.1.6  christos 
   1294      1.1  christos   if (filename_cmp (name.c_str (), objfile_name (parent_objfile)) == 0)
   1295      1.1  christos     return 0;
   1296  1.1.1.6  christos 
   1297  1.1.1.6  christos   if (separate_debug_file_debug)
   1298  1.1.1.6  christos     {
   1299  1.1.1.6  christos       printf_filtered (_("  Trying %s..."), name.c_str ());
   1300  1.1.1.6  christos       gdb_flush (gdb_stdout);
   1301  1.1.1.6  christos     }
   1302  1.1.1.6  christos 
   1303      1.1  christos   gdb_bfd_ref_ptr abfd (gdb_bfd_open (name.c_str (), gnutarget, -1));
   1304  1.1.1.5  christos 
   1305  1.1.1.6  christos   if (abfd == NULL)
   1306  1.1.1.6  christos     {
   1307  1.1.1.6  christos       if (separate_debug_file_debug)
   1308  1.1.1.6  christos 	printf_filtered (_(" no, unable to open.\n"));
   1309  1.1.1.6  christos 
   1310  1.1.1.6  christos       return 0;
   1311      1.1  christos     }
   1312      1.1  christos 
   1313      1.1  christos   /* Verify symlinks were not the cause of filename_cmp name difference above.
   1314      1.1  christos 
   1315      1.1  christos      Some operating systems, e.g. Windows, do not provide a meaningful
   1316  1.1.1.3  christos      st_ino; they always set it to zero.  (Windows does provide a
   1317  1.1.1.3  christos      meaningful st_dev.)  Files accessed from gdbservers that do not
   1318  1.1.1.3  christos      support the vFile:fstat packet will also have st_ino set to zero.
   1319  1.1.1.3  christos      Do not indicate a duplicate library in either case.  While there
   1320  1.1.1.3  christos      is no guarantee that a system that provides meaningful inode
   1321  1.1.1.3  christos      numbers will never set st_ino to zero, this is merely an
   1322      1.1  christos      optimization, so we do not need to worry about false negatives.  */
   1323  1.1.1.5  christos 
   1324      1.1  christos   if (bfd_stat (abfd.get (), &abfd_stat) == 0
   1325      1.1  christos       && abfd_stat.st_ino != 0
   1326      1.1  christos       && bfd_stat (parent_objfile->obfd, &parent_stat) == 0)
   1327      1.1  christos     {
   1328      1.1  christos       if (abfd_stat.st_dev == parent_stat.st_dev
   1329  1.1.1.6  christos 	  && abfd_stat.st_ino == parent_stat.st_ino)
   1330  1.1.1.6  christos 	{
   1331  1.1.1.6  christos 	  if (separate_debug_file_debug)
   1332  1.1.1.6  christos 	    printf_filtered (_(" no, same file as the objfile.\n"));
   1333  1.1.1.6  christos 
   1334  1.1.1.6  christos 	  return 0;
   1335      1.1  christos 	}
   1336      1.1  christos       verified_as_different = 1;
   1337      1.1  christos     }
   1338      1.1  christos   else
   1339      1.1  christos     verified_as_different = 0;
   1340  1.1.1.5  christos 
   1341      1.1  christos   file_crc_p = gdb_bfd_crc (abfd.get (), &file_crc);
   1342      1.1  christos 
   1343  1.1.1.6  christos   if (!file_crc_p)
   1344  1.1.1.6  christos     {
   1345  1.1.1.6  christos       if (separate_debug_file_debug)
   1346  1.1.1.6  christos 	printf_filtered (_(" no, error computing CRC.\n"));
   1347  1.1.1.6  christos 
   1348  1.1.1.6  christos       return 0;
   1349      1.1  christos     }
   1350      1.1  christos 
   1351      1.1  christos   if (crc != file_crc)
   1352      1.1  christos     {
   1353      1.1  christos       unsigned long parent_crc;
   1354  1.1.1.3  christos 
   1355  1.1.1.3  christos       /* If the files could not be verified as different with
   1356  1.1.1.3  christos 	 bfd_stat then we need to calculate the parent's CRC
   1357      1.1  christos 	 to verify whether the files are different or not.  */
   1358      1.1  christos 
   1359      1.1  christos       if (!verified_as_different)
   1360      1.1  christos 	{
   1361  1.1.1.6  christos 	  if (!gdb_bfd_crc (parent_objfile->obfd, &parent_crc))
   1362  1.1.1.6  christos 	    {
   1363  1.1.1.6  christos 	      if (separate_debug_file_debug)
   1364  1.1.1.6  christos 		printf_filtered (_(" no, error computing CRC.\n"));
   1365  1.1.1.6  christos 
   1366  1.1.1.6  christos 	      return 0;
   1367      1.1  christos 	    }
   1368      1.1  christos 	}
   1369      1.1  christos 
   1370      1.1  christos       if (verified_as_different || parent_crc != file_crc)
   1371      1.1  christos 	warning (_("the debug information found in \"%s\""
   1372  1.1.1.6  christos 		   " does not match \"%s\" (CRC mismatch).\n"),
   1373  1.1.1.6  christos 		 name.c_str (), objfile_name (parent_objfile));
   1374  1.1.1.6  christos 
   1375  1.1.1.6  christos       if (separate_debug_file_debug)
   1376      1.1  christos 	printf_filtered (_(" no, CRC doesn't match.\n"));
   1377      1.1  christos 
   1378      1.1  christos       return 0;
   1379      1.1  christos     }
   1380  1.1.1.6  christos 
   1381  1.1.1.6  christos   if (separate_debug_file_debug)
   1382  1.1.1.6  christos     printf_filtered (_(" yes!\n"));
   1383      1.1  christos 
   1384      1.1  christos   return 1;
   1385      1.1  christos }
   1386      1.1  christos 
   1387      1.1  christos char *debug_file_directory = NULL;
   1388      1.1  christos static void
   1389      1.1  christos show_debug_file_directory (struct ui_file *file, int from_tty,
   1390      1.1  christos 			   struct cmd_list_element *c, const char *value)
   1391      1.1  christos {
   1392      1.1  christos   fprintf_filtered (file,
   1393      1.1  christos 		    _("The directory where separate debug "
   1394      1.1  christos 		      "symbols are searched for is \"%s\".\n"),
   1395      1.1  christos 		    value);
   1396      1.1  christos }
   1397      1.1  christos 
   1398      1.1  christos #if ! defined (DEBUG_SUBDIRECTORY)
   1399      1.1  christos #define DEBUG_SUBDIRECTORY ".debug"
   1400      1.1  christos #endif
   1401      1.1  christos 
   1402      1.1  christos /* Find a separate debuginfo file for OBJFILE, using DIR as the directory
   1403      1.1  christos    where the original file resides (may not be the same as
   1404      1.1  christos    dirname(objfile->name) due to symlinks), and DEBUGLINK as the file we are
   1405      1.1  christos    looking for.  CANON_DIR is the "realpath" form of DIR.
   1406  1.1.1.6  christos    DIR must contain a trailing '/'.
   1407  1.1.1.6  christos    Returns the path of the file with separate debug info, or an empty
   1408      1.1  christos    string.  */
   1409  1.1.1.6  christos 
   1410      1.1  christos static std::string
   1411      1.1  christos find_separate_debug_file (const char *dir,
   1412      1.1  christos 			  const char *canon_dir,
   1413      1.1  christos 			  const char *debuglink,
   1414      1.1  christos 			  unsigned long crc32, struct objfile *objfile)
   1415  1.1.1.6  christos {
   1416  1.1.1.6  christos   if (separate_debug_file_debug)
   1417  1.1.1.6  christos     printf_filtered (_("\nLooking for separate debug info (debug link) for "
   1418      1.1  christos 		       "%s\n"), objfile_name (objfile));
   1419      1.1  christos 
   1420  1.1.1.6  christos   /* First try in the same directory as the original file.  */
   1421  1.1.1.6  christos   std::string debugfile = dir;
   1422      1.1  christos   debugfile += debuglink;
   1423      1.1  christos 
   1424      1.1  christos   if (separate_debug_file_exists (debugfile, crc32, objfile))
   1425      1.1  christos     return debugfile;
   1426      1.1  christos 
   1427  1.1.1.6  christos   /* Then try in the subdirectory named DEBUG_SUBDIRECTORY.  */
   1428  1.1.1.6  christos   debugfile = dir;
   1429  1.1.1.6  christos   debugfile += DEBUG_SUBDIRECTORY;
   1430  1.1.1.6  christos   debugfile += "/";
   1431      1.1  christos   debugfile += debuglink;
   1432      1.1  christos 
   1433      1.1  christos   if (separate_debug_file_exists (debugfile, crc32, objfile))
   1434      1.1  christos     return debugfile;
   1435      1.1  christos 
   1436      1.1  christos   /* Then try in the global debugfile directories.
   1437      1.1  christos 
   1438      1.1  christos      Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will
   1439      1.1  christos      cause "/..." lookups.  */
   1440  1.1.1.6  christos 
   1441  1.1.1.6  christos   bool target_prefix = startswith (dir, "target:");
   1442  1.1.1.6  christos   const char *dir_notarget = target_prefix ? dir + strlen ("target:") : dir;
   1443  1.1.1.6  christos   std::vector<gdb::unique_xmalloc_ptr<char>> debugdir_vec
   1444  1.1.1.6  christos     = dirnames_to_char_ptr_vec (debug_file_directory);
   1445  1.1.1.6  christos   gdb::unique_xmalloc_ptr<char> canon_sysroot = gdb_realpath (gdb_sysroot);
   1446  1.1.1.6  christos 
   1447  1.1.1.6  christos  /* MS-Windows/MS-DOS don't allow colons in file names; we must
   1448  1.1.1.6  christos     convert the drive letter into a one-letter directory, so that the
   1449  1.1.1.6  christos     file name resulting from splicing below will be valid.
   1450  1.1.1.6  christos 
   1451  1.1.1.6  christos     FIXME: The below only works when GDB runs on MS-Windows/MS-DOS.
   1452  1.1.1.6  christos     There are various remote-debugging scenarios where such a
   1453  1.1.1.6  christos     transformation of the drive letter might be required when GDB runs
   1454  1.1.1.6  christos     on a Posix host, see
   1455  1.1.1.6  christos 
   1456  1.1.1.6  christos     https://sourceware.org/ml/gdb-patches/2019-04/msg00605.html
   1457  1.1.1.6  christos 
   1458  1.1.1.6  christos     If some of those scenarions need to be supported, we will need to
   1459  1.1.1.6  christos     use a different condition for HAS_DRIVE_SPEC and a different macro
   1460  1.1.1.6  christos     instead of STRIP_DRIVE_SPEC, which work on Posix systems as well.  */
   1461  1.1.1.6  christos   std::string drive;
   1462  1.1.1.6  christos   if (HAS_DRIVE_SPEC (dir_notarget))
   1463  1.1.1.6  christos     {
   1464  1.1.1.6  christos       drive = dir_notarget[0];
   1465  1.1.1.6  christos       dir_notarget = STRIP_DRIVE_SPEC (dir_notarget);
   1466  1.1.1.6  christos     }
   1467  1.1.1.6  christos 
   1468  1.1.1.6  christos   for (const gdb::unique_xmalloc_ptr<char> &debugdir : debugdir_vec)
   1469  1.1.1.6  christos     {
   1470  1.1.1.6  christos       debugfile = target_prefix ? "target:" : "";
   1471  1.1.1.6  christos       debugfile += debugdir.get ();
   1472  1.1.1.6  christos       debugfile += "/";
   1473  1.1.1.6  christos       debugfile += drive;
   1474  1.1.1.6  christos       debugfile += dir_notarget;
   1475      1.1  christos       debugfile += debuglink;
   1476      1.1  christos 
   1477  1.1.1.6  christos       if (separate_debug_file_exists (debugfile, crc32, objfile))
   1478  1.1.1.6  christos 	return debugfile;
   1479  1.1.1.6  christos 
   1480  1.1.1.6  christos       const char *base_path = NULL;
   1481      1.1  christos       if (canon_dir != NULL)
   1482  1.1.1.6  christos 	{
   1483  1.1.1.6  christos 	  if (canon_sysroot.get () != NULL)
   1484  1.1.1.6  christos 	    base_path = child_path (canon_sysroot.get (), canon_dir);
   1485  1.1.1.6  christos 	  else
   1486      1.1  christos 	    base_path = child_path (gdb_sysroot, canon_dir);
   1487  1.1.1.6  christos 	}
   1488  1.1.1.6  christos       if (base_path != NULL)
   1489  1.1.1.6  christos 	{
   1490  1.1.1.6  christos 	  /* If the file is in the sysroot, try using its base path in
   1491  1.1.1.6  christos 	     the global debugfile directory.  */
   1492  1.1.1.6  christos 	  debugfile = target_prefix ? "target:" : "";
   1493  1.1.1.6  christos 	  debugfile += debugdir.get ();
   1494  1.1.1.6  christos 	  debugfile += "/";
   1495  1.1.1.6  christos 	  debugfile += base_path;
   1496  1.1.1.6  christos 	  debugfile += "/";
   1497      1.1  christos 	  debugfile += debuglink;
   1498  1.1.1.6  christos 
   1499  1.1.1.6  christos 	  if (separate_debug_file_exists (debugfile, crc32, objfile))
   1500  1.1.1.6  christos 	    return debugfile;
   1501  1.1.1.6  christos 
   1502  1.1.1.6  christos 	  /* If the file is in the sysroot, try using its base path in
   1503  1.1.1.6  christos 	     the sysroot's global debugfile directory.  */
   1504  1.1.1.6  christos 	  debugfile = target_prefix ? "target:" : "";
   1505  1.1.1.6  christos 	  debugfile += gdb_sysroot;
   1506  1.1.1.6  christos 	  debugfile += debugdir.get ();
   1507  1.1.1.6  christos 	  debugfile += "/";
   1508  1.1.1.6  christos 	  debugfile += base_path;
   1509  1.1.1.6  christos 	  debugfile += "/";
   1510      1.1  christos 	  debugfile += debuglink;
   1511      1.1  christos 
   1512  1.1.1.6  christos 	  if (separate_debug_file_exists (debugfile, crc32, objfile))
   1513      1.1  christos 	    return debugfile;
   1514  1.1.1.6  christos 	}
   1515      1.1  christos 
   1516      1.1  christos     }
   1517  1.1.1.6  christos 
   1518      1.1  christos   return std::string ();
   1519      1.1  christos }
   1520      1.1  christos 
   1521      1.1  christos /* Modify PATH to contain only "[/]directory/" part of PATH.
   1522      1.1  christos    If there were no directory separators in PATH, PATH will be empty
   1523      1.1  christos    string on return.  */
   1524      1.1  christos 
   1525      1.1  christos static void
   1526      1.1  christos terminate_after_last_dir_separator (char *path)
   1527      1.1  christos {
   1528      1.1  christos   int i;
   1529      1.1  christos 
   1530      1.1  christos   /* Strip off the final filename part, leaving the directory name,
   1531      1.1  christos      followed by a slash.  The directory can be relative or absolute.  */
   1532      1.1  christos   for (i = strlen(path) - 1; i >= 0; i--)
   1533      1.1  christos     if (IS_DIR_SEPARATOR (path[i]))
   1534      1.1  christos       break;
   1535      1.1  christos 
   1536      1.1  christos   /* If I is -1 then no directory is present there and DIR will be "".  */
   1537      1.1  christos   path[i + 1] = '\0';
   1538      1.1  christos }
   1539      1.1  christos 
   1540  1.1.1.6  christos /* Find separate debuginfo for OBJFILE (using .gnu_debuglink section).
   1541      1.1  christos    Returns pathname, or an empty string.  */
   1542  1.1.1.6  christos 
   1543      1.1  christos std::string
   1544      1.1  christos find_separate_debug_file_by_debuglink (struct objfile *objfile)
   1545      1.1  christos {
   1546      1.1  christos   unsigned long crc32;
   1547  1.1.1.6  christos 
   1548  1.1.1.6  christos   gdb::unique_xmalloc_ptr<char> debuglink
   1549      1.1  christos     (bfd_get_debug_link_info (objfile->obfd, &crc32));
   1550      1.1  christos 
   1551      1.1  christos   if (debuglink == NULL)
   1552      1.1  christos     {
   1553      1.1  christos       /* There's no separate debug info, hence there's no way we could
   1554  1.1.1.6  christos 	 load it => no warning.  */
   1555      1.1  christos       return std::string ();
   1556      1.1  christos     }
   1557  1.1.1.6  christos 
   1558  1.1.1.6  christos   std::string dir = objfile_name (objfile);
   1559  1.1.1.6  christos   terminate_after_last_dir_separator (&dir[0]);
   1560      1.1  christos   gdb::unique_xmalloc_ptr<char> canon_dir (lrealpath (dir.c_str ()));
   1561  1.1.1.6  christos 
   1562  1.1.1.6  christos   std::string debugfile
   1563  1.1.1.6  christos     = find_separate_debug_file (dir.c_str (), canon_dir.get (),
   1564      1.1  christos 				debuglink.get (), crc32, objfile);
   1565  1.1.1.6  christos 
   1566      1.1  christos   if (debugfile.empty ())
   1567      1.1  christos     {
   1568      1.1  christos       /* For PR gdb/9538, try again with realpath (if different from the
   1569      1.1  christos 	 original).  */
   1570      1.1  christos 
   1571      1.1  christos       struct stat st_buf;
   1572      1.1  christos 
   1573      1.1  christos       if (lstat (objfile_name (objfile), &st_buf) == 0
   1574      1.1  christos 	  && S_ISLNK (st_buf.st_mode))
   1575  1.1.1.6  christos 	{
   1576  1.1.1.6  christos 	  gdb::unique_xmalloc_ptr<char> symlink_dir
   1577      1.1  christos 	    (lrealpath (objfile_name (objfile)));
   1578      1.1  christos 	  if (symlink_dir != NULL)
   1579  1.1.1.6  christos 	    {
   1580  1.1.1.6  christos 	      terminate_after_last_dir_separator (symlink_dir.get ());
   1581      1.1  christos 	      if (dir != symlink_dir.get ())
   1582      1.1  christos 		{
   1583  1.1.1.6  christos 		  /* Different directory, so try using it.  */
   1584  1.1.1.6  christos 		  debugfile = find_separate_debug_file (symlink_dir.get (),
   1585  1.1.1.6  christos 							symlink_dir.get (),
   1586      1.1  christos 							debuglink.get (),
   1587      1.1  christos 							crc32,
   1588      1.1  christos 							objfile);
   1589      1.1  christos 		}
   1590      1.1  christos 	    }
   1591      1.1  christos 	}
   1592      1.1  christos     }
   1593      1.1  christos 
   1594      1.1  christos   return debugfile;
   1595      1.1  christos }
   1596  1.1.1.6  christos 
   1597  1.1.1.6  christos /* Make sure that OBJF_{READNOW,READNEVER} are not set
   1598  1.1.1.6  christos    simultaneously.  */
   1599  1.1.1.6  christos 
   1600  1.1.1.6  christos static void
   1601  1.1.1.6  christos validate_readnow_readnever (objfile_flags flags)
   1602  1.1.1.6  christos {
   1603  1.1.1.6  christos   if ((flags & OBJF_READNOW) && (flags & OBJF_READNEVER))
   1604  1.1.1.6  christos     error (_("-readnow and -readnever cannot be used simultaneously"));
   1605  1.1.1.6  christos }
   1606      1.1  christos 
   1607      1.1  christos /* This is the symbol-file command.  Read the file, analyze its
   1608      1.1  christos    symbols, and add a struct symtab to a symtab list.  The syntax of
   1609      1.1  christos    the command is rather bizarre:
   1610      1.1  christos 
   1611      1.1  christos    1. The function buildargv implements various quoting conventions
   1612      1.1  christos    which are undocumented and have little or nothing in common with
   1613      1.1  christos    the way things are quoted (or not quoted) elsewhere in GDB.
   1614      1.1  christos 
   1615      1.1  christos    2. Options are used, which are not generally used in GDB (perhaps
   1616      1.1  christos    "set mapped on", "set readnow on" would be better)
   1617      1.1  christos 
   1618      1.1  christos    3. The order of options matters, which is contrary to GNU
   1619      1.1  christos    conventions (because it is confusing and inconvenient).  */
   1620      1.1  christos 
   1621  1.1.1.6  christos void
   1622      1.1  christos symbol_file_command (const char *args, int from_tty)
   1623      1.1  christos {
   1624      1.1  christos   dont_repeat ();
   1625      1.1  christos 
   1626      1.1  christos   if (args == NULL)
   1627      1.1  christos     {
   1628      1.1  christos       symbol_file_clear (from_tty);
   1629      1.1  christos     }
   1630      1.1  christos   else
   1631  1.1.1.5  christos     {
   1632  1.1.1.5  christos       objfile_flags flags = OBJF_USERLOADED;
   1633      1.1  christos       symfile_add_flags add_flags = 0;
   1634  1.1.1.6  christos       char *name = NULL;
   1635  1.1.1.6  christos       bool stop_processing_options = false;
   1636  1.1.1.6  christos       CORE_ADDR offset = 0;
   1637  1.1.1.6  christos       int idx;
   1638      1.1  christos       char *arg;
   1639  1.1.1.5  christos 
   1640  1.1.1.5  christos       if (from_tty)
   1641  1.1.1.5  christos 	add_flags |= SYMFILE_VERBOSE;
   1642  1.1.1.6  christos 
   1643  1.1.1.6  christos       gdb_argv built_argv (args);
   1644      1.1  christos       for (arg = built_argv[0], idx = 0; arg != NULL; arg = built_argv[++idx])
   1645  1.1.1.6  christos 	{
   1646      1.1  christos 	  if (stop_processing_options || *arg != '-')
   1647  1.1.1.6  christos 	    {
   1648  1.1.1.6  christos 	      if (name == NULL)
   1649  1.1.1.6  christos 		name = arg;
   1650  1.1.1.6  christos 	      else
   1651      1.1  christos 		error (_("Unrecognized argument \"%s\""), arg);
   1652  1.1.1.6  christos 	    }
   1653  1.1.1.6  christos 	  else if (strcmp (arg, "-readnow") == 0)
   1654  1.1.1.6  christos 	    flags |= OBJF_READNOW;
   1655  1.1.1.6  christos 	  else if (strcmp (arg, "-readnever") == 0)
   1656  1.1.1.6  christos 	    flags |= OBJF_READNEVER;
   1657  1.1.1.6  christos 	  else if (strcmp (arg, "-o") == 0)
   1658  1.1.1.6  christos 	    {
   1659  1.1.1.6  christos 	      arg = built_argv[++idx];
   1660  1.1.1.6  christos 	      if (arg == NULL)
   1661      1.1  christos 		error (_("Missing argument to -o"));
   1662  1.1.1.6  christos 
   1663  1.1.1.6  christos 	      offset = parse_and_eval_address (arg);
   1664  1.1.1.6  christos 	    }
   1665  1.1.1.6  christos 	  else if (strcmp (arg, "--") == 0)
   1666  1.1.1.6  christos 	    stop_processing_options = true;
   1667  1.1.1.6  christos 	  else
   1668      1.1  christos 	    error (_("Unrecognized argument \"%s\""), arg);
   1669      1.1  christos 	}
   1670      1.1  christos 
   1671      1.1  christos       if (name == NULL)
   1672      1.1  christos 	error (_("no symbol file name was specified"));
   1673  1.1.1.6  christos 
   1674  1.1.1.6  christos       validate_readnow_readnever (flags);
   1675  1.1.1.6  christos 
   1676      1.1  christos       symbol_file_add_main_1 (name, add_flags, flags, offset);
   1677      1.1  christos     }
   1678      1.1  christos }
   1679      1.1  christos 
   1680      1.1  christos /* Set the initial language.
   1681      1.1  christos 
   1682      1.1  christos    FIXME: A better solution would be to record the language in the
   1683      1.1  christos    psymtab when reading partial symbols, and then use it (if known) to
   1684      1.1  christos    set the language.  This would be a win for formats that encode the
   1685      1.1  christos    language in an easily discoverable place, such as DWARF.  For
   1686      1.1  christos    stabs, we can jump through hoops looking for specially named
   1687      1.1  christos    symbols or try to intuit the language from the specific type of
   1688      1.1  christos    stabs we find, but we can't do that until later when we read in
   1689      1.1  christos    full symbols.  */
   1690      1.1  christos 
   1691      1.1  christos void
   1692      1.1  christos set_initial_language (void)
   1693  1.1.1.2  christos {
   1694      1.1  christos   enum language lang = main_language ();
   1695  1.1.1.2  christos 
   1696      1.1  christos   if (lang == language_unknown)
   1697      1.1  christos     {
   1698  1.1.1.4  christos       char *name = main_name ();
   1699      1.1  christos       struct symbol *sym = lookup_symbol (name, NULL, VAR_DOMAIN, NULL).symbol;
   1700      1.1  christos 
   1701      1.1  christos       if (sym != NULL)
   1702      1.1  christos 	lang = SYMBOL_LANGUAGE (sym);
   1703      1.1  christos     }
   1704      1.1  christos 
   1705      1.1  christos   if (lang == language_unknown)
   1706      1.1  christos     {
   1707      1.1  christos       /* Make C the default language */
   1708      1.1  christos       lang = language_c;
   1709      1.1  christos     }
   1710      1.1  christos 
   1711      1.1  christos   set_language (lang);
   1712      1.1  christos   expected_language = current_language; /* Don't warn the user.  */
   1713      1.1  christos }
   1714      1.1  christos 
   1715      1.1  christos /* Open the file specified by NAME and hand it off to BFD for
   1716      1.1  christos    preliminary analysis.  Return a newly initialized bfd *, which
   1717      1.1  christos    includes a newly malloc'd` copy of NAME (tilde-expanded and made
   1718      1.1  christos    absolute).  In case of trouble, error() is called.  */
   1719  1.1.1.5  christos 
   1720  1.1.1.3  christos gdb_bfd_ref_ptr
   1721      1.1  christos symfile_bfd_open (const char *name)
   1722  1.1.1.3  christos {
   1723      1.1  christos   int desc = -1;
   1724  1.1.1.6  christos 
   1725  1.1.1.3  christos   gdb::unique_xmalloc_ptr<char> absolute_name;
   1726      1.1  christos   if (!is_target_filename (name))
   1727  1.1.1.6  christos     {
   1728      1.1  christos       gdb::unique_xmalloc_ptr<char> expanded_name (tilde_expand (name));
   1729  1.1.1.3  christos 
   1730  1.1.1.3  christos       /* Look down path for it, allocate 2nd new malloc'd copy.  */
   1731  1.1.1.3  christos       desc = openp (getenv ("PATH"),
   1732  1.1.1.6  christos 		    OPF_TRY_CWD_FIRST | OPF_RETURN_REALPATH,
   1733      1.1  christos 		    expanded_name.get (), O_RDONLY | O_BINARY, &absolute_name);
   1734  1.1.1.3  christos #if defined(__GO32__) || defined(_WIN32) || defined (__CYGWIN__)
   1735  1.1.1.3  christos       if (desc < 0)
   1736  1.1.1.6  christos 	{
   1737      1.1  christos 	  char *exename = (char *) alloca (strlen (expanded_name.get ()) + 5);
   1738  1.1.1.6  christos 
   1739  1.1.1.3  christos 	  strcat (strcpy (exename, expanded_name.get ()), ".exe");
   1740  1.1.1.3  christos 	  desc = openp (getenv ("PATH"),
   1741  1.1.1.3  christos 			OPF_TRY_CWD_FIRST | OPF_RETURN_REALPATH,
   1742  1.1.1.3  christos 			exename, O_RDONLY | O_BINARY, &absolute_name);
   1743      1.1  christos 	}
   1744  1.1.1.3  christos #endif
   1745  1.1.1.6  christos       if (desc < 0)
   1746      1.1  christos 	perror_with_name (expanded_name.get ());
   1747  1.1.1.6  christos 
   1748  1.1.1.3  christos       name = absolute_name.get ();
   1749      1.1  christos     }
   1750  1.1.1.5  christos 
   1751  1.1.1.5  christos   gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (name, gnutarget, desc));
   1752      1.1  christos   if (sym_bfd == NULL)
   1753      1.1  christos     error (_("`%s': can't open to read symbols: %s."), name,
   1754  1.1.1.3  christos 	   bfd_errmsg (bfd_get_error ()));
   1755  1.1.1.5  christos 
   1756  1.1.1.5  christos   if (!gdb_bfd_has_target_filename (sym_bfd.get ()))
   1757      1.1  christos     bfd_set_cacheable (sym_bfd.get (), 1);
   1758  1.1.1.5  christos 
   1759  1.1.1.5  christos   if (!bfd_check_format (sym_bfd.get (), bfd_object))
   1760  1.1.1.5  christos     error (_("`%s': can't read symbols: %s."), name,
   1761      1.1  christos 	   bfd_errmsg (bfd_get_error ()));
   1762      1.1  christos 
   1763      1.1  christos   return sym_bfd;
   1764      1.1  christos }
   1765      1.1  christos 
   1766      1.1  christos /* Return the section index for SECTION_NAME on OBJFILE.  Return -1 if
   1767      1.1  christos    the section was not found.  */
   1768      1.1  christos 
   1769  1.1.1.5  christos int
   1770      1.1  christos get_section_index (struct objfile *objfile, const char *section_name)
   1771      1.1  christos {
   1772      1.1  christos   asection *sect = bfd_get_section_by_name (objfile->obfd, section_name);
   1773      1.1  christos 
   1774      1.1  christos   if (sect)
   1775      1.1  christos     return sect->index;
   1776      1.1  christos   else
   1777      1.1  christos     return -1;
   1778      1.1  christos }
   1779      1.1  christos 
   1780      1.1  christos /* Link SF into the global symtab_fns list.
   1781      1.1  christos    FLAVOUR is the file format that SF handles.
   1782      1.1  christos    Called on startup by the _initialize routine in each object file format
   1783      1.1  christos    reader, to register information about each format the reader is prepared
   1784      1.1  christos    to handle.  */
   1785      1.1  christos 
   1786      1.1  christos void
   1787      1.1  christos add_symtab_fns (enum bfd_flavour flavour, const struct sym_fns *sf)
   1788  1.1.1.6  christos {
   1789      1.1  christos   symtab_fns.emplace_back (flavour, sf);
   1790      1.1  christos }
   1791      1.1  christos 
   1792      1.1  christos /* Initialize OBJFILE to read symbols from its associated BFD.  It
   1793      1.1  christos    either returns or calls error().  The result is an initialized
   1794      1.1  christos    struct sym_fns in the objfile structure, that contains cached
   1795      1.1  christos    information about the symbol file.  */
   1796      1.1  christos 
   1797      1.1  christos static const struct sym_fns *
   1798      1.1  christos find_sym_fns (bfd *abfd)
   1799      1.1  christos {
   1800      1.1  christos   enum bfd_flavour our_flavour = bfd_get_flavour (abfd);
   1801      1.1  christos 
   1802      1.1  christos   if (our_flavour == bfd_target_srec_flavour
   1803      1.1  christos       || our_flavour == bfd_target_ihex_flavour
   1804      1.1  christos       || our_flavour == bfd_target_tekhex_flavour)
   1805      1.1  christos     return NULL;	/* No symbols.  */
   1806  1.1.1.6  christos 
   1807  1.1.1.6  christos   for (const registered_sym_fns &rsf : symtab_fns)
   1808  1.1.1.6  christos     if (our_flavour == rsf.sym_flavour)
   1809      1.1  christos       return rsf.sym_fns;
   1810      1.1  christos 
   1811      1.1  christos   error (_("I'm sorry, Dave, I can't do that.  Symbol format `%s' unknown."),
   1812      1.1  christos 	 bfd_get_target (abfd));
   1813      1.1  christos }
   1814      1.1  christos 
   1815      1.1  christos 
   1817      1.1  christos /* This function runs the load command of our current target.  */
   1818  1.1.1.6  christos 
   1819      1.1  christos static void
   1820      1.1  christos load_command (const char *arg, int from_tty)
   1821      1.1  christos {
   1822      1.1  christos   dont_repeat ();
   1823      1.1  christos 
   1824      1.1  christos   /* The user might be reloading because the binary has changed.  Take
   1825      1.1  christos      this opportunity to check.  */
   1826      1.1  christos   reopen_exec_file ();
   1827  1.1.1.6  christos   reread_symbols ();
   1828      1.1  christos 
   1829      1.1  christos   std::string temp;
   1830  1.1.1.6  christos   if (arg == NULL)
   1831      1.1  christos     {
   1832  1.1.1.6  christos       const char *parg, *prev;
   1833      1.1  christos 
   1834  1.1.1.6  christos       arg = get_exec_file (1);
   1835  1.1.1.6  christos 
   1836  1.1.1.6  christos       /* We may need to quote this string so buildargv can pull it
   1837      1.1  christos 	 apart.  */
   1838      1.1  christos       prev = parg = arg;
   1839  1.1.1.6  christos       while ((parg = strpbrk (parg, "\\\"'\t ")))
   1840  1.1.1.6  christos 	{
   1841  1.1.1.6  christos 	  temp.append (prev, parg - prev);
   1842  1.1.1.6  christos 	  prev = parg++;
   1843  1.1.1.6  christos 	  temp.push_back ('\\');
   1844  1.1.1.6  christos 	}
   1845  1.1.1.6  christos       /* If we have not copied anything yet, then we didn't see a
   1846      1.1  christos 	 character to quote, and we can just leave ARG unchanged.  */
   1847  1.1.1.6  christos       if (!temp.empty ())
   1848  1.1.1.6  christos 	{
   1849      1.1  christos 	  temp.append (prev);
   1850      1.1  christos 	  arg = temp.c_str ();
   1851      1.1  christos 	}
   1852      1.1  christos     }
   1853      1.1  christos 
   1854      1.1  christos   target_load (arg, from_tty);
   1855      1.1  christos 
   1856      1.1  christos   /* After re-loading the executable, we don't really know which
   1857      1.1  christos      overlays are mapped any more.  */
   1858      1.1  christos   overlay_cache_invalid = 1;
   1859      1.1  christos }
   1860      1.1  christos 
   1861      1.1  christos /* This version of "load" should be usable for any target.  Currently
   1862      1.1  christos    it is just used for remote targets, not inftarg.c or core files,
   1863      1.1  christos    on the theory that only in that case is it useful.
   1864      1.1  christos 
   1865      1.1  christos    Avoiding xmodem and the like seems like a win (a) because we don't have
   1866      1.1  christos    to worry about finding it, and (b) On VMS, fork() is very slow and so
   1867      1.1  christos    we don't want to run a subprocess.  On the other hand, I'm not sure how
   1868      1.1  christos    performance compares.  */
   1869      1.1  christos 
   1870      1.1  christos static int validate_download = 0;
   1871      1.1  christos 
   1872      1.1  christos /* Callback service function for generic_load (bfd_map_over_sections).  */
   1873      1.1  christos 
   1874      1.1  christos static void
   1875  1.1.1.4  christos add_section_size_callback (bfd *abfd, asection *asec, void *data)
   1876      1.1  christos {
   1877      1.1  christos   bfd_size_type *sum = (bfd_size_type *) data;
   1878      1.1  christos 
   1879      1.1  christos   *sum += bfd_get_section_size (asec);
   1880      1.1  christos }
   1881  1.1.1.6  christos 
   1882  1.1.1.6  christos /* Opaque data for load_progress.  */
   1883      1.1  christos struct load_progress_data
   1884  1.1.1.6  christos {
   1885  1.1.1.6  christos   /* Cumulative data.  */
   1886  1.1.1.6  christos   unsigned long write_count = 0;
   1887      1.1  christos   unsigned long data_count = 0;
   1888      1.1  christos   bfd_size_type total_size = 0;
   1889      1.1  christos };
   1890  1.1.1.6  christos 
   1891  1.1.1.6  christos /* Opaque data for load_progress for a single section.  */
   1892  1.1.1.6  christos struct load_progress_section_data
   1893  1.1.1.6  christos {
   1894  1.1.1.6  christos   load_progress_section_data (load_progress_data *cumulative_,
   1895  1.1.1.6  christos 			      const char *section_name_, ULONGEST section_size_,
   1896  1.1.1.6  christos 			      CORE_ADDR lma_, gdb_byte *buffer_)
   1897  1.1.1.6  christos     : cumulative (cumulative_), section_name (section_name_),
   1898  1.1.1.6  christos       section_size (section_size_), lma (lma_), buffer (buffer_)
   1899      1.1  christos   {}
   1900      1.1  christos 
   1901      1.1  christos   struct load_progress_data *cumulative;
   1902      1.1  christos 
   1903  1.1.1.6  christos   /* Per-section data.  */
   1904      1.1  christos   const char *section_name;
   1905      1.1  christos   ULONGEST section_sent = 0;
   1906      1.1  christos   ULONGEST section_size;
   1907      1.1  christos   CORE_ADDR lma;
   1908      1.1  christos   gdb_byte *buffer;
   1909  1.1.1.6  christos };
   1910  1.1.1.6  christos 
   1911  1.1.1.6  christos /* Opaque data for load_section_callback.  */
   1912  1.1.1.6  christos struct load_section_data
   1913  1.1.1.6  christos {
   1914  1.1.1.6  christos   load_section_data (load_progress_data *progress_data_)
   1915  1.1.1.6  christos     : progress_data (progress_data_)
   1916  1.1.1.6  christos   {}
   1917  1.1.1.6  christos 
   1918  1.1.1.6  christos   ~load_section_data ()
   1919  1.1.1.6  christos   {
   1920  1.1.1.6  christos     for (auto &&request : requests)
   1921  1.1.1.6  christos       {
   1922  1.1.1.6  christos 	xfree (request.data);
   1923  1.1.1.6  christos 	delete ((load_progress_section_data *) request.baton);
   1924  1.1.1.6  christos       }
   1925  1.1.1.6  christos   }
   1926  1.1.1.6  christos 
   1927  1.1.1.6  christos   CORE_ADDR load_offset = 0;
   1928  1.1.1.6  christos   struct load_progress_data *progress_data;
   1929  1.1.1.6  christos   std::vector<struct memory_write_request> requests;
   1930      1.1  christos };
   1931      1.1  christos 
   1932      1.1  christos /* Target write callback routine for progress reporting.  */
   1933      1.1  christos 
   1934      1.1  christos static void
   1935  1.1.1.4  christos load_progress (ULONGEST bytes, void *untyped_arg)
   1936  1.1.1.4  christos {
   1937      1.1  christos   struct load_progress_section_data *args
   1938      1.1  christos     = (struct load_progress_section_data *) untyped_arg;
   1939      1.1  christos   struct load_progress_data *totals;
   1940      1.1  christos 
   1941      1.1  christos   if (args == NULL)
   1942      1.1  christos     /* Writing padding data.  No easy way to get at the cumulative
   1943      1.1  christos        stats, so just ignore this.  */
   1944      1.1  christos     return;
   1945      1.1  christos 
   1946      1.1  christos   totals = args->cumulative;
   1947      1.1  christos 
   1948      1.1  christos   if (bytes == 0 && args->section_sent == 0)
   1949      1.1  christos     {
   1950  1.1.1.5  christos       /* The write is just starting.  Let the user know we've started
   1951  1.1.1.5  christos 	 this section.  */
   1952  1.1.1.5  christos       current_uiout->message ("Loading section %s, size %s lma %s\n",
   1953  1.1.1.5  christos 			      args->section_name,
   1954      1.1  christos 			      hex_string (args->section_size),
   1955      1.1  christos 			      paddress (target_gdbarch (), args->lma));
   1956      1.1  christos       return;
   1957      1.1  christos     }
   1958      1.1  christos 
   1959      1.1  christos   if (validate_download)
   1960      1.1  christos     {
   1961      1.1  christos       /* Broken memories and broken monitors manifest themselves here
   1962      1.1  christos 	 when bring new computers to life.  This doubles already slow
   1963      1.1  christos 	 downloads.  */
   1964      1.1  christos       /* NOTE: cagney/1999-10-18: A more efficient implementation
   1965      1.1  christos 	 might add a verify_memory() method to the target vector and
   1966  1.1.1.6  christos 	 then use that.  remote.c could implement that method using
   1967      1.1  christos 	 the ``qCRC'' packet.  */
   1968  1.1.1.6  christos       gdb::byte_vector check (bytes);
   1969      1.1  christos 
   1970      1.1  christos       if (target_read_memory (args->lma, check.data (), bytes) != 0)
   1971  1.1.1.6  christos 	error (_("Download verify read failed at %s"),
   1972      1.1  christos 	       paddress (target_gdbarch (), args->lma));
   1973      1.1  christos       if (memcmp (args->buffer, check.data (), bytes) != 0)
   1974      1.1  christos 	error (_("Download verify compare failed at %s"),
   1975      1.1  christos 	       paddress (target_gdbarch (), args->lma));
   1976      1.1  christos     }
   1977      1.1  christos   totals->data_count += bytes;
   1978      1.1  christos   args->lma += bytes;
   1979      1.1  christos   args->buffer += bytes;
   1980      1.1  christos   totals->write_count += 1;
   1981      1.1  christos   args->section_sent += bytes;
   1982      1.1  christos   if (check_quit_flag ()
   1983      1.1  christos       || (deprecated_ui_load_progress_hook != NULL
   1984      1.1  christos 	  && deprecated_ui_load_progress_hook (args->section_name,
   1985      1.1  christos 					       args->section_sent)))
   1986      1.1  christos     error (_("Canceled the download"));
   1987      1.1  christos 
   1988      1.1  christos   if (deprecated_show_load_progress != NULL)
   1989      1.1  christos     deprecated_show_load_progress (args->section_name,
   1990      1.1  christos 				   args->section_sent,
   1991      1.1  christos 				   args->section_size,
   1992      1.1  christos 				   totals->data_count,
   1993      1.1  christos 				   totals->total_size);
   1994      1.1  christos }
   1995      1.1  christos 
   1996      1.1  christos /* Callback service function for generic_load (bfd_map_over_sections).  */
   1997      1.1  christos 
   1998      1.1  christos static void
   1999  1.1.1.4  christos load_section_callback (bfd *abfd, asection *asec, void *data)
   2000      1.1  christos {
   2001      1.1  christos   struct load_section_data *args = (struct load_section_data *) data;
   2002      1.1  christos   bfd_size_type size = bfd_get_section_size (asec);
   2003      1.1  christos   const char *sect_name = bfd_get_section_name (abfd, asec);
   2004      1.1  christos 
   2005      1.1  christos   if ((bfd_get_section_flags (abfd, asec) & SEC_LOAD) == 0)
   2006      1.1  christos     return;
   2007      1.1  christos 
   2008      1.1  christos   if (size == 0)
   2009  1.1.1.6  christos     return;
   2010  1.1.1.6  christos 
   2011  1.1.1.6  christos   ULONGEST begin = bfd_section_lma (abfd, asec) + args->load_offset;
   2012      1.1  christos   ULONGEST end = begin + size;
   2013      1.1  christos   gdb_byte *buffer = (gdb_byte *) xmalloc (size);
   2014  1.1.1.6  christos   bfd_get_section_contents (abfd, asec, buffer, 0, size);
   2015  1.1.1.6  christos 
   2016  1.1.1.6  christos   load_progress_section_data *section_data
   2017      1.1  christos     = new load_progress_section_data (args->progress_data, sect_name, size,
   2018  1.1.1.6  christos 				      begin, buffer);
   2019      1.1  christos 
   2020      1.1  christos   args->requests.emplace_back (begin, end, buffer, section_data);
   2021  1.1.1.5  christos }
   2022  1.1.1.5  christos 
   2023  1.1.1.5  christos static void print_transfer_performance (struct ui_file *stream,
   2024  1.1.1.5  christos 					unsigned long data_count,
   2025  1.1.1.5  christos 					unsigned long write_count,
   2026      1.1  christos 				        std::chrono::steady_clock::duration d);
   2027  1.1.1.2  christos 
   2028      1.1  christos void
   2029      1.1  christos generic_load (const char *args, int from_tty)
   2030  1.1.1.6  christos {
   2031      1.1  christos   struct load_progress_data total_progress;
   2032      1.1  christos   struct load_section_data cbdata (&total_progress);
   2033      1.1  christos   struct ui_out *uiout = current_uiout;
   2034      1.1  christos 
   2035      1.1  christos   if (args == NULL)
   2036  1.1.1.6  christos     error_no_arg (_("file to load"));
   2037      1.1  christos 
   2038  1.1.1.6  christos   gdb_argv argv (args);
   2039      1.1  christos 
   2040      1.1  christos   gdb::unique_xmalloc_ptr<char> filename (tilde_expand (argv[0]));
   2041      1.1  christos 
   2042      1.1  christos   if (argv[1] != NULL)
   2043      1.1  christos     {
   2044      1.1  christos       const char *endptr;
   2045      1.1  christos 
   2046      1.1  christos       cbdata.load_offset = strtoulst (argv[1], &endptr, 0);
   2047      1.1  christos 
   2048      1.1  christos       /* If the last word was not a valid number then
   2049      1.1  christos          treat it as a file name with spaces in.  */
   2050      1.1  christos       if (argv[1] == endptr)
   2051      1.1  christos         error (_("Invalid download offset:%s."), argv[1]);
   2052      1.1  christos 
   2053      1.1  christos       if (argv[2] != NULL)
   2054      1.1  christos 	error (_("Too many parameters."));
   2055      1.1  christos     }
   2056  1.1.1.6  christos 
   2057      1.1  christos   /* Open the file for loading.  */
   2058  1.1.1.6  christos   gdb_bfd_ref_ptr loadfile_bfd (gdb_bfd_open (filename.get (), gnutarget, -1));
   2059      1.1  christos   if (loadfile_bfd == NULL)
   2060  1.1.1.5  christos     perror_with_name (filename.get ());
   2061      1.1  christos 
   2062  1.1.1.6  christos   if (!bfd_check_format (loadfile_bfd.get (), bfd_object))
   2063      1.1  christos     {
   2064      1.1  christos       error (_("\"%s\" is not an object file: %s"), filename.get (),
   2065      1.1  christos 	     bfd_errmsg (bfd_get_error ()));
   2066  1.1.1.5  christos     }
   2067      1.1  christos 
   2068      1.1  christos   bfd_map_over_sections (loadfile_bfd.get (), add_section_size_callback,
   2069  1.1.1.5  christos 			 (void *) &total_progress.total_size);
   2070      1.1  christos 
   2071  1.1.1.5  christos   bfd_map_over_sections (loadfile_bfd.get (), load_section_callback, &cbdata);
   2072  1.1.1.5  christos 
   2073  1.1.1.5  christos   using namespace std::chrono;
   2074      1.1  christos 
   2075      1.1  christos   steady_clock::time_point start_time = steady_clock::now ();
   2076      1.1  christos 
   2077      1.1  christos   if (target_write_memory_blocks (cbdata.requests, flash_discard,
   2078      1.1  christos 				  load_progress) != 0)
   2079  1.1.1.5  christos     error (_("Load failed"));
   2080      1.1  christos 
   2081  1.1.1.6  christos   steady_clock::time_point end_time = steady_clock::now ();
   2082      1.1  christos 
   2083  1.1.1.5  christos   CORE_ADDR entry = bfd_get_start_address (loadfile_bfd.get ());
   2084  1.1.1.5  christos   entry = gdbarch_addr_bits_remove (target_gdbarch (), entry);
   2085  1.1.1.5  christos   uiout->text ("Start address ");
   2086  1.1.1.5  christos   uiout->field_fmt ("address", "%s", paddress (target_gdbarch (), entry));
   2087  1.1.1.5  christos   uiout->text (", load size ");
   2088      1.1  christos   uiout->field_fmt ("load-size", "%lu", total_progress.data_count);
   2089      1.1  christos   uiout->text ("\n");
   2090      1.1  christos   regcache_write_pc (get_current_regcache (), entry);
   2091      1.1  christos 
   2092      1.1  christos   /* Reset breakpoints, now that we have changed the load image.  For
   2093      1.1  christos      instance, breakpoints may have been set (or reset, by
   2094      1.1  christos      post_create_inferior) while connected to the target but before we
   2095      1.1  christos      loaded the program.  In that case, the prologue analyzer could
   2096      1.1  christos      have read instructions from the target to find the right
   2097      1.1  christos      breakpoint locations.  Loading has changed the contents of that
   2098      1.1  christos      memory.  */
   2099      1.1  christos 
   2100      1.1  christos   breakpoint_re_set ();
   2101      1.1  christos 
   2102  1.1.1.5  christos   print_transfer_performance (gdb_stdout, total_progress.data_count,
   2103      1.1  christos 			      total_progress.write_count,
   2104      1.1  christos 			      end_time - start_time);
   2105  1.1.1.5  christos }
   2106  1.1.1.5  christos 
   2107  1.1.1.5  christos /* Report on STREAM the performance of a memory transfer operation,
   2108  1.1.1.5  christos    such as 'load'.  DATA_COUNT is the number of bytes transferred.
   2109  1.1.1.5  christos    WRITE_COUNT is the number of separate write operations, or 0, if
   2110      1.1  christos    that information is not available.  TIME is how long the operation
   2111  1.1.1.5  christos    lasted.  */
   2112      1.1  christos 
   2113      1.1  christos static void
   2114      1.1  christos print_transfer_performance (struct ui_file *stream,
   2115  1.1.1.5  christos 			    unsigned long data_count,
   2116      1.1  christos 			    unsigned long write_count,
   2117  1.1.1.5  christos 			    std::chrono::steady_clock::duration time)
   2118      1.1  christos {
   2119      1.1  christos   using namespace std::chrono;
   2120  1.1.1.5  christos   struct ui_out *uiout = current_uiout;
   2121      1.1  christos 
   2122  1.1.1.5  christos   milliseconds ms = duration_cast<milliseconds> (time);
   2123  1.1.1.5  christos 
   2124      1.1  christos   uiout->text ("Transfer rate: ");
   2125  1.1.1.5  christos   if (ms.count () > 0)
   2126      1.1  christos     {
   2127  1.1.1.5  christos       unsigned long rate = ((ULONGEST) data_count * 1000) / ms.count ();
   2128      1.1  christos 
   2129  1.1.1.5  christos       if (uiout->is_mi_like_p ())
   2130  1.1.1.5  christos 	{
   2131      1.1  christos 	  uiout->field_fmt ("transfer-rate", "%lu", rate * 8);
   2132      1.1  christos 	  uiout->text (" bits/sec");
   2133      1.1  christos 	}
   2134  1.1.1.5  christos       else if (rate < 1024)
   2135  1.1.1.5  christos 	{
   2136      1.1  christos 	  uiout->field_fmt ("transfer-rate", "%lu", rate);
   2137      1.1  christos 	  uiout->text (" bytes/sec");
   2138      1.1  christos 	}
   2139  1.1.1.5  christos       else
   2140  1.1.1.5  christos 	{
   2141      1.1  christos 	  uiout->field_fmt ("transfer-rate", "%lu", rate / 1024);
   2142      1.1  christos 	  uiout->text (" KB/sec");
   2143      1.1  christos 	}
   2144      1.1  christos     }
   2145  1.1.1.5  christos   else
   2146  1.1.1.5  christos     {
   2147      1.1  christos       uiout->field_fmt ("transferred-bits", "%lu", (data_count * 8));
   2148      1.1  christos       uiout->text (" bits in <1 sec");
   2149      1.1  christos     }
   2150  1.1.1.5  christos   if (write_count > 0)
   2151  1.1.1.5  christos     {
   2152  1.1.1.5  christos       uiout->text (", ");
   2153      1.1  christos       uiout->field_fmt ("write-rate", "%lu", data_count / write_count);
   2154  1.1.1.5  christos       uiout->text (" bytes/write");
   2155      1.1  christos     }
   2156      1.1  christos   uiout->text (".\n");
   2157  1.1.1.6  christos }
   2158  1.1.1.6  christos 
   2159  1.1.1.6  christos /* Add an OFFSET to the start address of each section in OBJF, except
   2160  1.1.1.6  christos    sections that were specified in ADDRS.  */
   2161  1.1.1.6  christos 
   2162  1.1.1.6  christos static void
   2163  1.1.1.6  christos set_objfile_default_section_offset (struct objfile *objf,
   2164  1.1.1.6  christos 				    const section_addr_info &addrs,
   2165  1.1.1.6  christos 				    CORE_ADDR offset)
   2166  1.1.1.6  christos {
   2167  1.1.1.6  christos   /* Add OFFSET to all sections by default.  */
   2168  1.1.1.6  christos   std::vector<struct section_offsets> offsets (objf->num_sections,
   2169  1.1.1.6  christos 					       { { offset } });
   2170  1.1.1.6  christos 
   2171  1.1.1.6  christos   /* Create sorted lists of all sections in ADDRS as well as all
   2172  1.1.1.6  christos      sections in OBJF.  */
   2173  1.1.1.6  christos 
   2174  1.1.1.6  christos   std::vector<const struct other_sections *> addrs_sorted
   2175  1.1.1.6  christos     = addrs_section_sort (addrs);
   2176  1.1.1.6  christos 
   2177  1.1.1.6  christos   section_addr_info objf_addrs
   2178  1.1.1.6  christos     = build_section_addr_info_from_objfile (objf);
   2179  1.1.1.6  christos   std::vector<const struct other_sections *> objf_addrs_sorted
   2180  1.1.1.6  christos     = addrs_section_sort (objf_addrs);
   2181  1.1.1.6  christos 
   2182  1.1.1.6  christos   /* Walk the BFD section list, and if a matching section is found in
   2183  1.1.1.6  christos      ADDRS_SORTED_LIST, set its offset to zero to keep its address
   2184  1.1.1.6  christos      unchanged.
   2185  1.1.1.6  christos 
   2186  1.1.1.6  christos      Note that both lists may contain multiple sections with the same
   2187  1.1.1.6  christos      name, and then the sections from ADDRS are matched in BFD order
   2188  1.1.1.6  christos      (thanks to sectindex).  */
   2189  1.1.1.6  christos 
   2190  1.1.1.6  christos   std::vector<const struct other_sections *>::iterator addrs_sorted_iter
   2191  1.1.1.6  christos     = addrs_sorted.begin ();
   2192  1.1.1.6  christos   for (const other_sections *objf_sect : objf_addrs_sorted)
   2193  1.1.1.6  christos     {
   2194  1.1.1.6  christos       const char *objf_name = addr_section_name (objf_sect->name.c_str ());
   2195  1.1.1.6  christos       int cmp = -1;
   2196  1.1.1.6  christos 
   2197  1.1.1.6  christos       while (cmp < 0 && addrs_sorted_iter != addrs_sorted.end ())
   2198  1.1.1.6  christos 	{
   2199  1.1.1.6  christos 	  const struct other_sections *sect = *addrs_sorted_iter;
   2200  1.1.1.6  christos 	  const char *sect_name = addr_section_name (sect->name.c_str ());
   2201  1.1.1.6  christos 	  cmp = strcmp (sect_name, objf_name);
   2202  1.1.1.6  christos 	  if (cmp <= 0)
   2203  1.1.1.6  christos 	    ++addrs_sorted_iter;
   2204  1.1.1.6  christos 	}
   2205  1.1.1.6  christos 
   2206  1.1.1.6  christos       if (cmp == 0)
   2207  1.1.1.6  christos 	offsets[objf_sect->sectindex].offsets[0] = 0;
   2208  1.1.1.6  christos     }
   2209  1.1.1.6  christos 
   2210  1.1.1.6  christos   /* Apply the new section offsets.  */
   2211  1.1.1.6  christos   objfile_relocate (objf, offsets.data ());
   2212      1.1  christos }
   2213      1.1  christos 
   2214      1.1  christos /* This function allows the addition of incrementally linked object files.
   2215      1.1  christos    It does not modify any state in the target, only in the debugger.  */
   2216      1.1  christos /* Note: ezannoni 2000-04-13 This function/command used to have a
   2217      1.1  christos    special case syntax for the rombug target (Rombug is the boot
   2218      1.1  christos    monitor for Microware's OS-9 / OS-9000, see remote-os9k.c). In the
   2219      1.1  christos    rombug case, the user doesn't need to supply a text address,
   2220      1.1  christos    instead a call to target_link() (in target.c) would supply the
   2221      1.1  christos    value to use.  We are now discontinuing this type of ad hoc syntax.  */
   2222  1.1.1.6  christos 
   2223      1.1  christos static void
   2224      1.1  christos add_symbol_file_command (const char *args, int from_tty)
   2225  1.1.1.6  christos {
   2226      1.1  christos   struct gdbarch *gdbarch = get_current_arch ();
   2227      1.1  christos   gdb::unique_xmalloc_ptr<char> filename;
   2228      1.1  christos   char *arg;
   2229  1.1.1.5  christos   int argcnt = 0;
   2230  1.1.1.5  christos   struct objfile *objf;
   2231  1.1.1.5  christos   objfile_flags flags = OBJF_USERLOADED | OBJF_SHARED;
   2232  1.1.1.5  christos   symfile_add_flags add_flags = 0;
   2233  1.1.1.5  christos 
   2234      1.1  christos   if (from_tty)
   2235      1.1  christos     add_flags |= SYMFILE_VERBOSE;
   2236      1.1  christos 
   2237  1.1.1.5  christos   struct sect_opt
   2238  1.1.1.5  christos   {
   2239      1.1  christos     const char *name;
   2240      1.1  christos     const char *value;
   2241  1.1.1.6  christos   };
   2242  1.1.1.6  christos 
   2243  1.1.1.6  christos   std::vector<sect_opt> sect_opts = { { ".text", NULL } };
   2244      1.1  christos   bool stop_processing_options = false;
   2245      1.1  christos   CORE_ADDR offset = 0;
   2246      1.1  christos 
   2247      1.1  christos   dont_repeat ();
   2248      1.1  christos 
   2249      1.1  christos   if (args == NULL)
   2250  1.1.1.6  christos     error (_("add-symbol-file takes a file name and an address"));
   2251  1.1.1.6  christos 
   2252  1.1.1.6  christos   bool seen_addr = false;
   2253      1.1  christos   bool seen_offset = false;
   2254      1.1  christos   gdb_argv argv (args);
   2255      1.1  christos 
   2256  1.1.1.6  christos   for (arg = argv[0], argcnt = 0; arg != NULL; arg = argv[++argcnt])
   2257      1.1  christos     {
   2258  1.1.1.6  christos       if (stop_processing_options || *arg != '-')
   2259      1.1  christos 	{
   2260  1.1.1.6  christos 	  if (filename == NULL)
   2261  1.1.1.6  christos 	    {
   2262      1.1  christos 	      /* First non-option argument is always the filename.  */
   2263  1.1.1.6  christos 	      filename.reset (tilde_expand (arg));
   2264      1.1  christos 	    }
   2265  1.1.1.6  christos 	  else if (!seen_addr)
   2266  1.1.1.6  christos 	    {
   2267  1.1.1.6  christos 	      /* The second non-option argument is always the text
   2268  1.1.1.6  christos 		 address at which to load the program.  */
   2269      1.1  christos 	      sect_opts[0].value = arg;
   2270      1.1  christos 	      seen_addr = true;
   2271  1.1.1.6  christos 	    }
   2272  1.1.1.6  christos 	  else
   2273  1.1.1.6  christos 	    error (_("Unrecognized argument \"%s\""), arg);
   2274  1.1.1.6  christos 	}
   2275  1.1.1.6  christos       else if (strcmp (arg, "-readnow") == 0)
   2276  1.1.1.6  christos 	flags |= OBJF_READNOW;
   2277  1.1.1.6  christos       else if (strcmp (arg, "-readnever") == 0)
   2278  1.1.1.6  christos 	flags |= OBJF_READNEVER;
   2279  1.1.1.6  christos       else if (strcmp (arg, "-s") == 0)
   2280  1.1.1.6  christos 	{
   2281  1.1.1.6  christos 	  if (argv[argcnt + 1] == NULL)
   2282  1.1.1.6  christos 	    error (_("Missing section name after \"-s\""));
   2283  1.1.1.6  christos 	  else if (argv[argcnt + 2] == NULL)
   2284  1.1.1.6  christos 	    error (_("Missing section address after \"-s\""));
   2285  1.1.1.6  christos 
   2286  1.1.1.6  christos 	  sect_opt sect = { argv[argcnt + 1], argv[argcnt + 2] };
   2287  1.1.1.6  christos 
   2288  1.1.1.6  christos 	  sect_opts.push_back (sect);
   2289  1.1.1.6  christos 	  argcnt += 2;
   2290  1.1.1.6  christos 	}
   2291  1.1.1.6  christos       else if (strcmp (arg, "-o") == 0)
   2292  1.1.1.6  christos 	{
   2293  1.1.1.6  christos 	  arg = argv[++argcnt];
   2294  1.1.1.6  christos 	  if (arg == NULL)
   2295  1.1.1.6  christos 	    error (_("Missing argument to -o"));
   2296  1.1.1.6  christos 
   2297      1.1  christos 	  offset = parse_and_eval_address (arg);
   2298  1.1.1.6  christos 	  seen_offset = true;
   2299  1.1.1.6  christos 	}
   2300  1.1.1.6  christos       else if (strcmp (arg, "--") == 0)
   2301  1.1.1.6  christos 	stop_processing_options = true;
   2302      1.1  christos       else
   2303      1.1  christos 	error (_("Unrecognized argument \"%s\""), arg);
   2304  1.1.1.6  christos     }
   2305  1.1.1.6  christos 
   2306  1.1.1.6  christos   if (filename == NULL)
   2307  1.1.1.6  christos     error (_("You must provide a filename to be loaded."));
   2308      1.1  christos 
   2309      1.1  christos   validate_readnow_readnever (flags);
   2310      1.1  christos 
   2311      1.1  christos   /* Print the prompt for the query below.  And save the arguments into
   2312      1.1  christos      a sect_addr_info structure to be passed around to other
   2313      1.1  christos      functions.  We have to split this up into separate print
   2314      1.1  christos      statements because hex_string returns a local static
   2315  1.1.1.6  christos      string.  */
   2316  1.1.1.6  christos 
   2317  1.1.1.6  christos   printf_unfiltered (_("add symbol table from file \"%s\""),
   2318  1.1.1.6  christos 		     filename.get ());
   2319  1.1.1.6  christos   section_addr_info section_addrs;
   2320  1.1.1.6  christos   std::vector<sect_opt>::const_iterator it = sect_opts.begin ();
   2321  1.1.1.6  christos   if (!seen_addr)
   2322      1.1  christos     ++it;
   2323      1.1  christos   for (; it != sect_opts.end (); ++it)
   2324  1.1.1.6  christos     {
   2325  1.1.1.6  christos       CORE_ADDR addr;
   2326      1.1  christos       const char *val = it->value;
   2327  1.1.1.6  christos       const char *sec = it->name;
   2328  1.1.1.6  christos 
   2329      1.1  christos       if (section_addrs.empty ())
   2330      1.1  christos 	printf_unfiltered (_(" at\n"));
   2331      1.1  christos       addr = parse_and_eval_address (val);
   2332  1.1.1.6  christos 
   2333  1.1.1.6  christos       /* Here we store the section offsets in the order they were
   2334  1.1.1.6  christos          entered on the command line.  Every array element is
   2335  1.1.1.6  christos          assigned an ascending section index to preserve the above
   2336  1.1.1.6  christos          order over an unstable sorting algorithm.  This dummy
   2337  1.1.1.6  christos          index is not used for any other purpose.
   2338  1.1.1.6  christos       */
   2339  1.1.1.6  christos       section_addrs.emplace_back (addr, sec, section_addrs.size ());
   2340      1.1  christos       printf_filtered ("\t%s_addr = %s\n", sec,
   2341      1.1  christos 		       paddress (gdbarch, addr));
   2342      1.1  christos 
   2343      1.1  christos       /* The object's sections are initialized when a
   2344      1.1  christos 	 call is made to build_objfile_section_table (objfile).
   2345      1.1  christos 	 This happens in reread_symbols.
   2346      1.1  christos 	 At this point, we don't know what file type this is,
   2347  1.1.1.6  christos 	 so we can't determine what section names are valid.  */
   2348  1.1.1.6  christos     }
   2349  1.1.1.6  christos   if (seen_offset)
   2350  1.1.1.6  christos       printf_unfiltered (_("%s offset by %s\n"),
   2351  1.1.1.6  christos 			 (section_addrs.empty ()
   2352  1.1.1.6  christos 			  ? _(" with all sections")
   2353  1.1.1.6  christos 			  : _("with other sections")),
   2354  1.1.1.6  christos 			 paddress (gdbarch, offset));
   2355      1.1  christos   else if (section_addrs.empty ())
   2356      1.1  christos     printf_unfiltered ("\n");
   2357      1.1  christos 
   2358      1.1  christos   if (from_tty && (!query ("%s", "")))
   2359  1.1.1.6  christos     error (_("Not confirmed."));
   2360  1.1.1.6  christos 
   2361  1.1.1.6  christos   objf = symbol_file_add (filename.get (), add_flags, &section_addrs,
   2362  1.1.1.6  christos 			  flags);
   2363  1.1.1.6  christos 
   2364      1.1  christos   if (seen_offset)
   2365      1.1  christos     set_objfile_default_section_offset (objf, section_addrs, offset);
   2366      1.1  christos 
   2367      1.1  christos   add_target_sections_of_objfile (objf);
   2368      1.1  christos 
   2369      1.1  christos   /* Getting new symbols may change our opinion about what is
   2370      1.1  christos      frameless.  */
   2371      1.1  christos   reinit_frame_cache ();
   2372      1.1  christos }
   2373      1.1  christos 
   2374      1.1  christos 
   2376  1.1.1.6  christos /* This function removes a symbol file that was added via add-symbol-file.  */
   2377      1.1  christos 
   2378      1.1  christos static void
   2379      1.1  christos remove_symbol_file_command (const char *args, int from_tty)
   2380      1.1  christos {
   2381      1.1  christos   struct objfile *objf = NULL;
   2382      1.1  christos   struct program_space *pspace = current_program_space;
   2383      1.1  christos 
   2384      1.1  christos   dont_repeat ();
   2385      1.1  christos 
   2386  1.1.1.6  christos   if (args == NULL)
   2387      1.1  christos     error (_("remove-symbol-file: no symbol file provided"));
   2388      1.1  christos 
   2389      1.1  christos   gdb_argv argv (args);
   2390      1.1  christos 
   2391      1.1  christos   if (strcmp (argv[0], "-a") == 0)
   2392      1.1  christos     {
   2393      1.1  christos       /* Interpret the next argument as an address.  */
   2394      1.1  christos       CORE_ADDR addr;
   2395      1.1  christos 
   2396      1.1  christos       if (argv[1] == NULL)
   2397      1.1  christos 	error (_("Missing address argument"));
   2398      1.1  christos 
   2399      1.1  christos       if (argv[2] != NULL)
   2400      1.1  christos 	error (_("Junk after %s"), argv[1]);
   2401  1.1.1.6  christos 
   2402      1.1  christos       addr = parse_and_eval_address (argv[1]);
   2403  1.1.1.6  christos 
   2404  1.1.1.6  christos       for (objfile *objfile : current_program_space->objfiles ())
   2405  1.1.1.6  christos 	{
   2406  1.1.1.6  christos 	  if ((objfile->flags & OBJF_USERLOADED) != 0
   2407  1.1.1.6  christos 	      && (objfile->flags & OBJF_SHARED) != 0
   2408  1.1.1.6  christos 	      && objfile->pspace == pspace
   2409  1.1.1.6  christos 	      && is_addr_in_objfile (addr, objfile))
   2410  1.1.1.6  christos 	    {
   2411      1.1  christos 	      objf = objfile;
   2412      1.1  christos 	      break;
   2413      1.1  christos 	    }
   2414      1.1  christos 	}
   2415      1.1  christos     }
   2416      1.1  christos   else if (argv[0] != NULL)
   2417      1.1  christos     {
   2418      1.1  christos       /* Interpret the current argument as a file name.  */
   2419      1.1  christos 
   2420  1.1.1.6  christos       if (argv[1] != NULL)
   2421      1.1  christos 	error (_("Junk after %s"), argv[0]);
   2422  1.1.1.6  christos 
   2423      1.1  christos       gdb::unique_xmalloc_ptr<char> filename (tilde_expand (argv[0]));
   2424  1.1.1.6  christos 
   2425  1.1.1.6  christos       for (objfile *objfile : current_program_space->objfiles ())
   2426  1.1.1.6  christos 	{
   2427  1.1.1.6  christos 	  if ((objfile->flags & OBJF_USERLOADED) != 0
   2428  1.1.1.6  christos 	      && (objfile->flags & OBJF_SHARED) != 0
   2429  1.1.1.6  christos 	      && objfile->pspace == pspace
   2430  1.1.1.6  christos 	      && filename_cmp (filename.get (), objfile_name (objfile)) == 0)
   2431  1.1.1.6  christos 	    {
   2432      1.1  christos 	      objf = objfile;
   2433      1.1  christos 	      break;
   2434      1.1  christos 	    }
   2435      1.1  christos 	}
   2436      1.1  christos     }
   2437      1.1  christos 
   2438      1.1  christos   if (objf == NULL)
   2439      1.1  christos     error (_("No symbol file found"));
   2440      1.1  christos 
   2441      1.1  christos   if (from_tty
   2442      1.1  christos       && !query (_("Remove symbol table from file \"%s\"? "),
   2443  1.1.1.6  christos 		 objfile_name (objf)))
   2444      1.1  christos     error (_("Not confirmed."));
   2445      1.1  christos 
   2446      1.1  christos   delete objf;
   2447      1.1  christos   clear_symtab_users (0);
   2448      1.1  christos }
   2449      1.1  christos 
   2450      1.1  christos /* Re-read symbols if a symbol-file has changed.  */
   2451      1.1  christos 
   2452      1.1  christos void
   2453      1.1  christos reread_symbols (void)
   2454      1.1  christos {
   2455      1.1  christos   struct objfile *objfile;
   2456  1.1.1.5  christos   long new_modtime;
   2457      1.1  christos   struct stat new_statbuf;
   2458      1.1  christos   int res;
   2459      1.1  christos   std::vector<struct objfile *> new_objfiles;
   2460      1.1  christos 
   2461      1.1  christos   /* With the addition of shared libraries, this should be modified,
   2462      1.1  christos      the load time should be saved in the partial symbol tables, since
   2463      1.1  christos      different tables may come from different source files.  FIXME.
   2464      1.1  christos      This routine should then walk down each partial symbol table
   2465      1.1  christos      and see if the symbol table that it originates from has been changed.  */
   2466      1.1  christos 
   2467      1.1  christos   for (objfile = object_files; objfile; objfile = objfile->next)
   2468      1.1  christos     {
   2469      1.1  christos       if (objfile->obfd == NULL)
   2470      1.1  christos 	continue;
   2471      1.1  christos 
   2472      1.1  christos       /* Separate debug objfiles are handled in the main objfile.  */
   2473      1.1  christos       if (objfile->separate_debug_objfile_backlink)
   2474      1.1  christos 	continue;
   2475      1.1  christos 
   2476      1.1  christos       /* If this object is from an archive (what you usually create with
   2477      1.1  christos 	 `ar', often called a `static library' on most systems, though
   2478      1.1  christos 	 a `shared library' on AIX is also an archive), then you should
   2479      1.1  christos 	 stat on the archive name, not member name.  */
   2480      1.1  christos       if (objfile->obfd->my_archive)
   2481      1.1  christos 	res = stat (objfile->obfd->my_archive->filename, &new_statbuf);
   2482      1.1  christos       else
   2483      1.1  christos 	res = stat (objfile_name (objfile), &new_statbuf);
   2484  1.1.1.6  christos       if (res != 0)
   2485  1.1.1.6  christos 	{
   2486      1.1  christos 	  /* FIXME, should use print_sys_errmsg but it's not filtered.  */
   2487      1.1  christos 	  printf_filtered (_("`%s' has disappeared; keeping its symbols.\n"),
   2488      1.1  christos 			   objfile_name (objfile));
   2489      1.1  christos 	  continue;
   2490      1.1  christos 	}
   2491      1.1  christos       new_modtime = new_statbuf.st_mtime;
   2492      1.1  christos       if (new_modtime != objfile->mtime)
   2493      1.1  christos 	{
   2494  1.1.1.6  christos 	  struct section_offsets *offsets;
   2495  1.1.1.6  christos 	  int num_offsets;
   2496      1.1  christos 
   2497      1.1  christos 	  printf_filtered (_("`%s' has changed; re-reading symbols.\n"),
   2498      1.1  christos 			   objfile_name (objfile));
   2499      1.1  christos 
   2500      1.1  christos 	  /* There are various functions like symbol_file_add,
   2501      1.1  christos 	     symfile_bfd_open, syms_from_objfile, etc., which might
   2502      1.1  christos 	     appear to do what we want.  But they have various other
   2503      1.1  christos 	     effects which we *don't* want.  So we just do stuff
   2504      1.1  christos 	     ourselves.  We don't worry about mapped files (for one thing,
   2505      1.1  christos 	     any mapped file will be out of date).  */
   2506      1.1  christos 
   2507  1.1.1.6  christos 	  /* If we get an error, blow away this objfile (not sure if
   2508  1.1.1.6  christos 	     that is the correct response for things like shared
   2509      1.1  christos 	     libraries).  */
   2510  1.1.1.6  christos 	  std::unique_ptr<struct objfile> objfile_holder (objfile);
   2511      1.1  christos 
   2512      1.1  christos 	  /* We need to do this whenever any symbols go away.  */
   2513      1.1  christos 	  clear_symtab_users_cleanup defer_clear_users (0);
   2514      1.1  christos 
   2515      1.1  christos 	  if (exec_bfd != NULL
   2516      1.1  christos 	      && filename_cmp (bfd_get_filename (objfile->obfd),
   2517      1.1  christos 			       bfd_get_filename (exec_bfd)) == 0)
   2518      1.1  christos 	    {
   2519      1.1  christos 	      /* Reload EXEC_BFD without asking anything.  */
   2520      1.1  christos 
   2521      1.1  christos 	      exec_file_attach (bfd_get_filename (objfile->obfd), 0);
   2522      1.1  christos 	    }
   2523      1.1  christos 
   2524      1.1  christos 	  /* Keep the calls order approx. the same as in free_objfile.  */
   2525      1.1  christos 
   2526      1.1  christos 	  /* Free the separate debug objfiles.  It will be
   2527      1.1  christos 	     automatically recreated by sym_read.  */
   2528      1.1  christos 	  free_objfile_separate_debug (objfile);
   2529      1.1  christos 
   2530      1.1  christos 	  /* Remove any references to this objfile in the global
   2531      1.1  christos 	     value lists.  */
   2532      1.1  christos 	  preserve_values (objfile);
   2533      1.1  christos 
   2534      1.1  christos 	  /* Nuke all the state that we will re-read.  Much of the following
   2535      1.1  christos 	     code which sets things to NULL really is necessary to tell
   2536      1.1  christos 	     other parts of GDB that there is nothing currently there.
   2537      1.1  christos 
   2538      1.1  christos 	     Try to keep the freeing order compatible with free_objfile.  */
   2539      1.1  christos 
   2540      1.1  christos 	  if (objfile->sf != NULL)
   2541      1.1  christos 	    {
   2542      1.1  christos 	      (*objfile->sf->sym_finish) (objfile);
   2543      1.1  christos 	    }
   2544      1.1  christos 
   2545      1.1  christos 	  clear_objfile_data (objfile);
   2546  1.1.1.5  christos 
   2547      1.1  christos 	  /* Clean up any state BFD has sitting around.  */
   2548      1.1  christos 	  {
   2549      1.1  christos 	    gdb_bfd_ref_ptr obfd (objfile->obfd);
   2550      1.1  christos 	    char *obfd_filename;
   2551      1.1  christos 
   2552  1.1.1.5  christos 	    obfd_filename = bfd_get_filename (objfile->obfd);
   2553  1.1.1.5  christos 	    /* Open the new BFD before freeing the old one, so that
   2554      1.1  christos 	       the filename remains live.  */
   2555  1.1.1.5  christos 	    gdb_bfd_ref_ptr temp (gdb_bfd_open (obfd_filename, gnutarget, -1));
   2556      1.1  christos 	    objfile->obfd = temp.release ();
   2557      1.1  christos 	    if (objfile->obfd == NULL)
   2558  1.1.1.6  christos 	      error (_("Can't open %s to read symbols."), obfd_filename);
   2559      1.1  christos 	  }
   2560      1.1  christos 
   2561      1.1  christos 	  std::string original_name = objfile->original_name;
   2562      1.1  christos 
   2563      1.1  christos 	  /* bfd_openr sets cacheable to true, which is what we want.  */
   2564      1.1  christos 	  if (!bfd_check_format (objfile->obfd, bfd_object))
   2565      1.1  christos 	    error (_("Can't read symbols from %s: %s."), objfile_name (objfile),
   2566      1.1  christos 		   bfd_errmsg (bfd_get_error ()));
   2567      1.1  christos 
   2568      1.1  christos 	  /* Save the offsets, we will nuke them with the rest of the
   2569      1.1  christos 	     objfile_obstack.  */
   2570      1.1  christos 	  num_offsets = objfile->num_sections;
   2571      1.1  christos 	  offsets = ((struct section_offsets *)
   2572      1.1  christos 		     alloca (SIZEOF_N_SECTION_OFFSETS (num_offsets)));
   2573  1.1.1.6  christos 	  memcpy (offsets, objfile->section_offsets,
   2574  1.1.1.6  christos 		  SIZEOF_N_SECTION_OFFSETS (num_offsets));
   2575  1.1.1.6  christos 
   2576  1.1.1.6  christos 	  objfile->reset_psymtabs ();
   2577      1.1  christos 
   2578      1.1  christos 	  /* NB: after this call to obstack_free, objfiles_changed
   2579  1.1.1.2  christos 	     will need to be called (see discussion below).  */
   2580      1.1  christos 	  obstack_free (&objfile->objfile_obstack, 0);
   2581  1.1.1.6  christos 	  objfile->sections = NULL;
   2582      1.1  christos 	  objfile->compunit_symtabs = NULL;
   2583      1.1  christos 	  objfile->template_symbols = NULL;
   2584      1.1  christos 	  objfile->static_links = NULL;
   2585      1.1  christos 
   2586      1.1  christos 	  /* obstack_init also initializes the obstack so it is
   2587      1.1  christos 	     empty.  We could use obstack_specify_allocation but
   2588      1.1  christos 	     gdb_obstack.h specifies the alloc/dealloc functions.  */
   2589      1.1  christos 	  obstack_init (&objfile->objfile_obstack);
   2590      1.1  christos 
   2591      1.1  christos 	  /* set_objfile_per_bfd potentially allocates the per-bfd
   2592      1.1  christos 	     data on the objfile's obstack (if sharing data across
   2593      1.1  christos 	     multiple users is not possible), so it's important to
   2594  1.1.1.4  christos 	     do it *after* the obstack has been initialized.  */
   2595  1.1.1.6  christos 	  set_objfile_per_bfd (objfile);
   2596  1.1.1.6  christos 
   2597  1.1.1.6  christos 	  objfile->original_name
   2598      1.1  christos 	    = (char *) obstack_copy0 (&objfile->objfile_obstack,
   2599      1.1  christos 				      original_name.c_str (),
   2600      1.1  christos 				      original_name.size ());
   2601      1.1  christos 
   2602      1.1  christos 	  /* Reset the sym_fns pointer.  The ELF reader can change it
   2603      1.1  christos 	     based on whether .gdb_index is present, and we need it to
   2604      1.1  christos 	     start over.  PR symtab/15885  */
   2605      1.1  christos 	  objfile_set_sym_fns (objfile, find_sym_fns (objfile->obfd));
   2606      1.1  christos 
   2607      1.1  christos 	  build_objfile_section_table (objfile);
   2608      1.1  christos 	  terminate_minimal_symbol_table (objfile);
   2609      1.1  christos 
   2610      1.1  christos 	  /* We use the same section offsets as from last time.  I'm not
   2611      1.1  christos 	     sure whether that is always correct for shared libraries.  */
   2612      1.1  christos 	  objfile->section_offsets = (struct section_offsets *)
   2613      1.1  christos 	    obstack_alloc (&objfile->objfile_obstack,
   2614      1.1  christos 			   SIZEOF_N_SECTION_OFFSETS (num_offsets));
   2615      1.1  christos 	  memcpy (objfile->section_offsets, offsets,
   2616      1.1  christos 		  SIZEOF_N_SECTION_OFFSETS (num_offsets));
   2617      1.1  christos 	  objfile->num_sections = num_offsets;
   2618      1.1  christos 
   2619      1.1  christos 	  /* What the hell is sym_new_init for, anyway?  The concept of
   2620      1.1  christos 	     distinguishing between the main file and additional files
   2621      1.1  christos 	     in this way seems rather dubious.  */
   2622      1.1  christos 	  if (objfile == symfile_objfile)
   2623      1.1  christos 	    {
   2624      1.1  christos 	      (*objfile->sf->sym_new_init) (objfile);
   2625  1.1.1.6  christos 	    }
   2626      1.1  christos 
   2627      1.1  christos 	  (*objfile->sf->sym_init) (objfile);
   2628  1.1.1.6  christos 	  clear_complaints ();
   2629  1.1.1.6  christos 
   2630  1.1.1.6  christos 	  objfile->flags &= ~OBJF_PSYMTABS_READ;
   2631  1.1.1.6  christos 
   2632  1.1.1.6  christos 	  /* We are about to read new symbols and potentially also
   2633  1.1.1.6  christos 	     DWARF information.  Some targets may want to pass addresses
   2634  1.1.1.6  christos 	     read from DWARF DIE's through an adjustment function before
   2635  1.1.1.6  christos 	     saving them, like MIPS, which may call into
   2636  1.1.1.6  christos 	     "find_pc_section".  When called, that function will make
   2637  1.1.1.6  christos 	     use of per-objfile program space data.
   2638  1.1.1.6  christos 
   2639  1.1.1.6  christos 	     Since we discarded our section information above, we have
   2640  1.1.1.6  christos 	     dangling pointers in the per-objfile program space data
   2641  1.1.1.6  christos 	     structure.  Force GDB to update the section mapping
   2642  1.1.1.6  christos 	     information by letting it know the objfile has changed,
   2643  1.1.1.6  christos 	     making the dangling pointers point to correct data
   2644  1.1.1.6  christos 	     again.  */
   2645      1.1  christos 
   2646      1.1  christos 	  objfiles_changed ();
   2647      1.1  christos 
   2648      1.1  christos 	  read_symbols (objfile, 0);
   2649      1.1  christos 
   2650  1.1.1.6  christos 	  if (!objfile_has_symbols (objfile))
   2651      1.1  christos 	    {
   2652      1.1  christos 	      wrap_here ("");
   2653      1.1  christos 	      printf_filtered (_("(no debugging symbols found)\n"));
   2654      1.1  christos 	      wrap_here ("");
   2655  1.1.1.6  christos 	    }
   2656      1.1  christos 
   2657      1.1  christos 	  /* We're done reading the symbol file; finish off complaints.  */
   2658      1.1  christos 	  clear_complaints ();
   2659      1.1  christos 
   2660      1.1  christos 	  /* Getting new symbols may change our opinion about what is
   2661      1.1  christos 	     frameless.  */
   2662      1.1  christos 
   2663  1.1.1.6  christos 	  reinit_frame_cache ();
   2664  1.1.1.6  christos 
   2665      1.1  christos 	  /* Discard cleanups as symbol reading was successful.  */
   2666      1.1  christos 	  objfile_holder.release ();
   2667      1.1  christos 	  defer_clear_users.release ();
   2668      1.1  christos 
   2669      1.1  christos 	  /* If the mtime has changed between the time we set new_modtime
   2670      1.1  christos 	     and now, we *want* this to be out of date, so don't call stat
   2671      1.1  christos 	     again now.  */
   2672  1.1.1.5  christos 	  objfile->mtime = new_modtime;
   2673      1.1  christos 	  init_entry_point_info (objfile);
   2674      1.1  christos 
   2675      1.1  christos 	  new_objfiles.push_back (objfile);
   2676  1.1.1.5  christos 	}
   2677      1.1  christos     }
   2678      1.1  christos 
   2679      1.1  christos   if (!new_objfiles.empty ())
   2680      1.1  christos     {
   2681  1.1.1.6  christos       clear_symtab_users (0);
   2682      1.1  christos 
   2683  1.1.1.5  christos       /* clear_objfile_data for each objfile was called before freeing it and
   2684  1.1.1.6  christos 	 gdb::observers::new_objfile.notify (NULL) has been called by
   2685      1.1  christos 	 clear_symtab_users above.  Notify the new files now.  */
   2686      1.1  christos       for (auto iter : new_objfiles)
   2687      1.1  christos 	gdb::observers::new_objfile.notify (iter);
   2688  1.1.1.6  christos 
   2689      1.1  christos       /* At least one objfile has changed, so we can consider that
   2690      1.1  christos          the executable we're debugging has changed too.  */
   2691      1.1  christos       gdb::observers::executable_changed.notify ();
   2692      1.1  christos     }
   2693  1.1.1.6  christos }
   2694      1.1  christos 
   2695  1.1.1.6  christos 
   2697  1.1.1.6  christos struct filename_language
   2698      1.1  christos {
   2699  1.1.1.6  christos   filename_language (const std::string &ext_, enum language lang_)
   2700  1.1.1.6  christos   : ext (ext_), lang (lang_)
   2701  1.1.1.6  christos   {}
   2702      1.1  christos 
   2703  1.1.1.6  christos   std::string ext;
   2704  1.1.1.4  christos   enum language lang;
   2705  1.1.1.4  christos };
   2706  1.1.1.4  christos 
   2707  1.1.1.4  christos static std::vector<filename_language> filename_language_table;
   2708  1.1.1.4  christos 
   2709      1.1  christos /* See symfile.h.  */
   2710  1.1.1.6  christos 
   2711      1.1  christos void
   2712      1.1  christos add_filename_language (const char *ext, enum language lang)
   2713      1.1  christos {
   2714      1.1  christos   filename_language_table.emplace_back (ext, lang);
   2715      1.1  christos }
   2716      1.1  christos 
   2717      1.1  christos static char *ext_args;
   2718      1.1  christos static void
   2719      1.1  christos show_ext_args (struct ui_file *file, int from_tty,
   2720      1.1  christos 	       struct cmd_list_element *c, const char *value)
   2721      1.1  christos {
   2722      1.1  christos   fprintf_filtered (file,
   2723      1.1  christos 		    _("Mapping between filename extension "
   2724      1.1  christos 		      "and source language is \"%s\".\n"),
   2725  1.1.1.6  christos 		    value);
   2726  1.1.1.6  christos }
   2727      1.1  christos 
   2728      1.1  christos static void
   2729      1.1  christos set_ext_lang_command (const char *args,
   2730      1.1  christos 		      int from_tty, struct cmd_list_element *e)
   2731      1.1  christos {
   2732      1.1  christos   char *cp = ext_args;
   2733      1.1  christos   enum language lang;
   2734      1.1  christos 
   2735      1.1  christos   /* First arg is filename extension, starting with '.'  */
   2736      1.1  christos   if (*cp != '.')
   2737      1.1  christos     error (_("'%s': Filename extension must begin with '.'"), ext_args);
   2738      1.1  christos 
   2739      1.1  christos   /* Find end of first arg.  */
   2740      1.1  christos   while (*cp && !isspace (*cp))
   2741      1.1  christos     cp++;
   2742      1.1  christos 
   2743      1.1  christos   if (*cp == '\0')
   2744      1.1  christos     error (_("'%s': two arguments required -- "
   2745      1.1  christos 	     "filename extension and language"),
   2746      1.1  christos 	   ext_args);
   2747      1.1  christos 
   2748      1.1  christos   /* Null-terminate first arg.  */
   2749      1.1  christos   *cp++ = '\0';
   2750      1.1  christos 
   2751      1.1  christos   /* Find beginning of second arg, which should be a source language.  */
   2752      1.1  christos   cp = skip_spaces (cp);
   2753      1.1  christos 
   2754      1.1  christos   if (*cp == '\0')
   2755      1.1  christos     error (_("'%s': two arguments required -- "
   2756      1.1  christos 	     "filename extension and language"),
   2757      1.1  christos 	   ext_args);
   2758  1.1.1.6  christos 
   2759      1.1  christos   /* Lookup the language from among those we know.  */
   2760  1.1.1.6  christos   lang = language_enum (cp);
   2761  1.1.1.4  christos 
   2762  1.1.1.6  christos   auto it = filename_language_table.begin ();
   2763  1.1.1.4  christos   /* Now lookup the filename extension: do we already know it?  */
   2764  1.1.1.4  christos   for (; it != filename_language_table.end (); it++)
   2765      1.1  christos     {
   2766  1.1.1.6  christos       if (it->ext == ext_args)
   2767      1.1  christos 	break;
   2768      1.1  christos     }
   2769      1.1  christos 
   2770      1.1  christos   if (it == filename_language_table.end ())
   2771      1.1  christos     {
   2772      1.1  christos       /* New file extension.  */
   2773      1.1  christos       add_filename_language (ext_args, lang);
   2774      1.1  christos     }
   2775      1.1  christos   else
   2776      1.1  christos     {
   2777      1.1  christos       /* Redefining a previously known filename extension.  */
   2778      1.1  christos 
   2779  1.1.1.6  christos       /* if (from_tty) */
   2780      1.1  christos       /*   query ("Really make files of type %s '%s'?", */
   2781      1.1  christos       /*          ext_args, language_str (lang));           */
   2782      1.1  christos 
   2783      1.1  christos       it->lang = lang;
   2784  1.1.1.6  christos     }
   2785      1.1  christos }
   2786      1.1  christos 
   2787      1.1  christos static void
   2788  1.1.1.6  christos info_ext_lang_command (const char *args, int from_tty)
   2789  1.1.1.6  christos {
   2790  1.1.1.6  christos   printf_filtered (_("Filename extensions and the languages they represent:"));
   2791      1.1  christos   printf_filtered ("\n\n");
   2792      1.1  christos   for (const filename_language &entry : filename_language_table)
   2793      1.1  christos     printf_filtered ("\t%s\t- %s\n", entry.ext.c_str (),
   2794      1.1  christos 		     language_str (entry.lang));
   2795      1.1  christos }
   2796  1.1.1.4  christos 
   2797      1.1  christos enum language
   2798      1.1  christos deduce_language_from_filename (const char *filename)
   2799      1.1  christos {
   2800  1.1.1.4  christos   const char *cp;
   2801  1.1.1.6  christos 
   2802  1.1.1.6  christos   if (filename != NULL)
   2803  1.1.1.6  christos     if ((cp = strrchr (filename, '.')) != NULL)
   2804  1.1.1.4  christos       {
   2805      1.1  christos 	for (const filename_language &entry : filename_language_table)
   2806      1.1  christos 	  if (entry.ext == cp)
   2807      1.1  christos 	    return entry.lang;
   2808      1.1  christos       }
   2809  1.1.1.2  christos 
   2810  1.1.1.2  christos   return language_unknown;
   2811      1.1  christos }
   2812      1.1  christos 
   2813  1.1.1.2  christos /* Allocate and initialize a new symbol table.
   2815  1.1.1.2  christos    CUST is from the result of allocate_compunit_symtab.  */
   2816  1.1.1.2  christos 
   2817  1.1.1.2  christos struct symtab *
   2818      1.1  christos allocate_symtab (struct compunit_symtab *cust, const char *filename)
   2819  1.1.1.4  christos {
   2820  1.1.1.4  christos   struct objfile *objfile = cust->objfile;
   2821      1.1  christos   struct symtab *symtab
   2822      1.1  christos     = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symtab);
   2823      1.1  christos 
   2824      1.1  christos   symtab->filename
   2825      1.1  christos     = (const char *) bcache (filename, strlen (filename) + 1,
   2826      1.1  christos 			     objfile->per_bfd->filename_cache);
   2827      1.1  christos   symtab->fullname = NULL;
   2828      1.1  christos   symtab->language = deduce_language_from_filename (filename);
   2829      1.1  christos 
   2830      1.1  christos   /* This can be very verbose with lots of headers.
   2831      1.1  christos      Only print at higher debug levels.  */
   2832      1.1  christos   if (symtab_create_debug >= 2)
   2833      1.1  christos     {
   2834      1.1  christos       /* Be a bit clever with debugging messages, and don't print objfile
   2835      1.1  christos 	 every time, only when it changes.  */
   2836      1.1  christos       static char *last_objfile_name = NULL;
   2837      1.1  christos 
   2838  1.1.1.6  christos       if (last_objfile_name == NULL
   2839  1.1.1.6  christos 	  || strcmp (last_objfile_name, objfile_name (objfile)) != 0)
   2840  1.1.1.6  christos 	{
   2841  1.1.1.6  christos 	  xfree (last_objfile_name);
   2842  1.1.1.6  christos 	  last_objfile_name = xstrdup (objfile_name (objfile));
   2843  1.1.1.6  christos 	  fprintf_filtered (gdb_stdlog,
   2844  1.1.1.6  christos 			    "Creating one or more symtabs for objfile %s ...\n",
   2845      1.1  christos 			    last_objfile_name);
   2846      1.1  christos 	}
   2847  1.1.1.2  christos       fprintf_filtered (gdb_stdlog,
   2848  1.1.1.2  christos 			"Created symtab %s for module %s.\n",
   2849  1.1.1.2  christos 			host_address_to_string (symtab), filename);
   2850  1.1.1.2  christos     }
   2851  1.1.1.2  christos 
   2852  1.1.1.2  christos   /* Add it to CUST's list of symtabs.  */
   2853  1.1.1.2  christos   if (cust->filetabs == NULL)
   2854  1.1.1.2  christos     {
   2855  1.1.1.2  christos       cust->filetabs = symtab;
   2856  1.1.1.2  christos       cust->last_filetab = symtab;
   2857  1.1.1.2  christos     }
   2858  1.1.1.2  christos   else
   2859  1.1.1.2  christos     {
   2860  1.1.1.2  christos       cust->last_filetab->next = symtab;
   2861  1.1.1.2  christos       cust->last_filetab = symtab;
   2862  1.1.1.2  christos     }
   2863  1.1.1.2  christos 
   2864  1.1.1.2  christos   /* Backlink to the containing compunit symtab.  */
   2865  1.1.1.2  christos   symtab->compunit_symtab = cust;
   2866  1.1.1.2  christos 
   2867  1.1.1.2  christos   return symtab;
   2868  1.1.1.2  christos }
   2869  1.1.1.2  christos 
   2870  1.1.1.2  christos /* Allocate and initialize a new compunit.
   2871  1.1.1.2  christos    NAME is the name of the main source file, if there is one, or some
   2872  1.1.1.2  christos    descriptive text if there are no source files.  */
   2873  1.1.1.2  christos 
   2874  1.1.1.2  christos struct compunit_symtab *
   2875  1.1.1.2  christos allocate_compunit_symtab (struct objfile *objfile, const char *name)
   2876  1.1.1.2  christos {
   2877  1.1.1.2  christos   struct compunit_symtab *cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
   2878  1.1.1.2  christos 					       struct compunit_symtab);
   2879  1.1.1.2  christos   const char *saved_name;
   2880  1.1.1.2  christos 
   2881  1.1.1.2  christos   cu->objfile = objfile;
   2882  1.1.1.4  christos 
   2883  1.1.1.4  christos   /* The name we record here is only for display/debugging purposes.
   2884  1.1.1.4  christos      Just save the basename to avoid path issues (too long for display,
   2885  1.1.1.2  christos      relative vs absolute, etc.).  */
   2886  1.1.1.2  christos   saved_name = lbasename (name);
   2887  1.1.1.2  christos   cu->name
   2888  1.1.1.2  christos     = (const char *) obstack_copy0 (&objfile->objfile_obstack, saved_name,
   2889  1.1.1.2  christos 				    strlen (saved_name));
   2890  1.1.1.6  christos 
   2891  1.1.1.6  christos   COMPUNIT_DEBUGFORMAT (cu) = "unknown";
   2892  1.1.1.6  christos 
   2893  1.1.1.6  christos   if (symtab_create_debug)
   2894  1.1.1.2  christos     {
   2895  1.1.1.2  christos       fprintf_filtered (gdb_stdlog,
   2896  1.1.1.2  christos 			"Created compunit symtab %s for %s.\n",
   2897  1.1.1.2  christos 			host_address_to_string (cu),
   2898  1.1.1.2  christos 			cu->name);
   2899  1.1.1.2  christos     }
   2900  1.1.1.2  christos 
   2901  1.1.1.2  christos   return cu;
   2902  1.1.1.2  christos }
   2903  1.1.1.2  christos 
   2904  1.1.1.2  christos /* Hook CU to the objfile it comes from.  */
   2905  1.1.1.2  christos 
   2906      1.1  christos void
   2907      1.1  christos add_compunit_symtab_to_objfile (struct compunit_symtab *cu)
   2908      1.1  christos {
   2909  1.1.1.5  christos   cu->next = cu->objfile->compunit_symtabs;
   2910  1.1.1.5  christos   cu->objfile->compunit_symtabs = cu;
   2911      1.1  christos }
   2912      1.1  christos 
   2913  1.1.1.5  christos 
   2915      1.1  christos /* Reset all data structures in gdb which may contain references to
   2916      1.1  christos    symbol table data.  */
   2917      1.1  christos 
   2918      1.1  christos void
   2919      1.1  christos clear_symtab_users (symfile_add_flags add_flags)
   2920      1.1  christos {
   2921      1.1  christos   /* Someday, we should do better than this, by only blowing away
   2922      1.1  christos      the things that really need to be blown.  */
   2923      1.1  christos 
   2924      1.1  christos   /* Clear the "current" symtab first, because it is no longer valid.
   2925  1.1.1.6  christos      breakpoint_re_set may try to access the current symtab.  */
   2926      1.1  christos   clear_current_source_symtab_and_line ();
   2927      1.1  christos 
   2928      1.1  christos   clear_displays ();
   2929      1.1  christos   clear_last_displayed_sal ();
   2930      1.1  christos   clear_pc_function_cache ();
   2931  1.1.1.6  christos   gdb::observers::new_objfile.notify (NULL);
   2932      1.1  christos 
   2933      1.1  christos   /* Clear globals which might have pointed into a removed objfile.
   2934      1.1  christos      FIXME: It's not clear which of these are supposed to persist
   2935      1.1  christos      between expressions and which ought to be reset each time.  */
   2936  1.1.1.2  christos   expression_context_block = NULL;
   2937  1.1.1.2  christos   innermost_block.reset ();
   2938  1.1.1.2  christos 
   2939  1.1.1.2  christos   /* Varobj may refer to old symbols, perform a cleanup.  */
   2940      1.1  christos   varobj_invalidate ();
   2941      1.1  christos 
   2942      1.1  christos   /* Now that the various caches have been cleared, we can re_set
   2943      1.1  christos      our breakpoints without risking it using stale data.  */
   2944      1.1  christos   if ((add_flags & SYMFILE_DEFER_BP_RESET) == 0)
   2945      1.1  christos     breakpoint_re_set ();
   2946      1.1  christos }
   2947      1.1  christos 
   2948      1.1  christos /* OVERLAYS:
   2950      1.1  christos    The following code implements an abstraction for debugging overlay sections.
   2951      1.1  christos 
   2952      1.1  christos    The target model is as follows:
   2953      1.1  christos    1) The gnu linker will permit multiple sections to be mapped into the
   2954      1.1  christos    same VMA, each with its own unique LMA (or load address).
   2955      1.1  christos    2) It is assumed that some runtime mechanism exists for mapping the
   2956      1.1  christos    sections, one by one, from the load address into the VMA address.
   2957      1.1  christos    3) This code provides a mechanism for gdb to keep track of which
   2958      1.1  christos    sections should be considered to be mapped from the VMA to the LMA.
   2959      1.1  christos    This information is used for symbol lookup, and memory read/write.
   2960      1.1  christos    For instance, if a section has been mapped then its contents
   2961      1.1  christos    should be read from the VMA, otherwise from the LMA.
   2962      1.1  christos 
   2963      1.1  christos    Two levels of debugger support for overlays are available.  One is
   2964      1.1  christos    "manual", in which the debugger relies on the user to tell it which
   2965      1.1  christos    overlays are currently mapped.  This level of support is
   2966      1.1  christos    implemented entirely in the core debugger, and the information about
   2967      1.1  christos    whether a section is mapped is kept in the objfile->obj_section table.
   2968      1.1  christos 
   2969      1.1  christos    The second level of support is "automatic", and is only available if
   2970      1.1  christos    the target-specific code provides functionality to read the target's
   2971      1.1  christos    overlay mapping table, and translate its contents for the debugger
   2972      1.1  christos    (by updating the mapped state information in the obj_section tables).
   2973      1.1  christos 
   2974      1.1  christos    The interface is as follows:
   2975      1.1  christos    User commands:
   2976      1.1  christos    overlay map <name>   -- tell gdb to consider this section mapped
   2977      1.1  christos    overlay unmap <name> -- tell gdb to consider this section unmapped
   2978      1.1  christos    overlay list         -- list the sections that GDB thinks are mapped
   2979      1.1  christos    overlay read-target  -- get the target's state of what's mapped
   2980      1.1  christos    overlay off/manual/auto -- set overlay debugging state
   2981      1.1  christos    Functional interface:
   2982      1.1  christos    find_pc_mapped_section(pc):    if the pc is in the range of a mapped
   2983      1.1  christos    section, return that section.
   2984      1.1  christos    find_pc_overlay(pc):       find any overlay section that contains
   2985      1.1  christos    the pc, either in its VMA or its LMA
   2986      1.1  christos    section_is_mapped(sect):       true if overlay is marked as mapped
   2987      1.1  christos    section_is_overlay(sect):      true if section's VMA != LMA
   2988      1.1  christos    pc_in_mapped_range(pc,sec):    true if pc belongs to section's VMA
   2989      1.1  christos    pc_in_unmapped_range(...):     true if pc belongs to section's LMA
   2990      1.1  christos    sections_overlap(sec1, sec2):  true if mapped sec1 and sec2 ranges overlap
   2991      1.1  christos    overlay_mapped_address(...):   map an address from section's LMA to VMA
   2992      1.1  christos    overlay_unmapped_address(...): map an address from section's VMA to LMA
   2993      1.1  christos    symbol_overlayed_address(...): Return a "current" address for symbol:
   2994      1.1  christos    either in VMA or LMA depending on whether
   2995      1.1  christos    the symbol's section is currently mapped.  */
   2996      1.1  christos 
   2997      1.1  christos /* Overlay debugging state: */
   2998      1.1  christos 
   2999      1.1  christos enum overlay_debugging_state overlay_debugging = ovly_off;
   3000      1.1  christos int overlay_cache_invalid = 0;	/* True if need to refresh mapped state.  */
   3001      1.1  christos 
   3002      1.1  christos /* Function: section_is_overlay (SECTION)
   3003      1.1  christos    Returns true if SECTION has VMA not equal to LMA, ie.
   3004      1.1  christos    SECTION is loaded at an address different from where it will "run".  */
   3005      1.1  christos 
   3006      1.1  christos int
   3007      1.1  christos section_is_overlay (struct obj_section *section)
   3008      1.1  christos {
   3009      1.1  christos   if (overlay_debugging && section)
   3010      1.1  christos     {
   3011      1.1  christos       asection *bfd_section = section->the_bfd_section;
   3012      1.1  christos 
   3013      1.1  christos       if (bfd_section_lma (abfd, bfd_section) != 0
   3014      1.1  christos 	  && bfd_section_lma (abfd, bfd_section)
   3015      1.1  christos 	     != bfd_section_vma (abfd, bfd_section))
   3016      1.1  christos 	return 1;
   3017      1.1  christos     }
   3018      1.1  christos 
   3019      1.1  christos   return 0;
   3020      1.1  christos }
   3021      1.1  christos 
   3022      1.1  christos /* Function: overlay_invalidate_all (void)
   3023  1.1.1.6  christos    Invalidate the mapped state of all overlay sections (mark it as stale).  */
   3024  1.1.1.6  christos 
   3025  1.1.1.6  christos static void
   3026  1.1.1.6  christos overlay_invalidate_all (void)
   3027      1.1  christos {
   3028      1.1  christos   struct obj_section *sect;
   3029      1.1  christos 
   3030      1.1  christos   for (objfile *objfile : current_program_space->objfiles ())
   3031      1.1  christos     ALL_OBJFILE_OSECTIONS (objfile, sect)
   3032      1.1  christos       if (section_is_overlay (sect))
   3033      1.1  christos 	sect->ovly_mapped = -1;
   3034      1.1  christos }
   3035      1.1  christos 
   3036      1.1  christos /* Function: section_is_mapped (SECTION)
   3037      1.1  christos    Returns true if section is an overlay, and is currently mapped.
   3038      1.1  christos 
   3039      1.1  christos    Access to the ovly_mapped flag is restricted to this function, so
   3040      1.1  christos    that we can do automatic update.  If the global flag
   3041      1.1  christos    OVERLAY_CACHE_INVALID is set (by wait_for_inferior), then call
   3042      1.1  christos    overlay_invalidate_all.  If the mapped state of the particular
   3043      1.1  christos    section is stale, then call TARGET_OVERLAY_UPDATE to refresh it.  */
   3044      1.1  christos 
   3045      1.1  christos int
   3046      1.1  christos section_is_mapped (struct obj_section *osect)
   3047      1.1  christos {
   3048      1.1  christos   struct gdbarch *gdbarch;
   3049      1.1  christos 
   3050      1.1  christos   if (osect == 0 || !section_is_overlay (osect))
   3051      1.1  christos     return 0;
   3052      1.1  christos 
   3053      1.1  christos   switch (overlay_debugging)
   3054      1.1  christos     {
   3055      1.1  christos     default:
   3056      1.1  christos     case ovly_off:
   3057      1.1  christos       return 0;			/* overlay debugging off */
   3058      1.1  christos     case ovly_auto:		/* overlay debugging automatic */
   3059      1.1  christos       /* Unles there is a gdbarch_overlay_update function,
   3060      1.1  christos          there's really nothing useful to do here (can't really go auto).  */
   3061      1.1  christos       gdbarch = get_objfile_arch (osect->objfile);
   3062      1.1  christos       if (gdbarch_overlay_update_p (gdbarch))
   3063      1.1  christos 	{
   3064      1.1  christos 	  if (overlay_cache_invalid)
   3065  1.1.1.6  christos 	    {
   3066      1.1  christos 	      overlay_invalidate_all ();
   3067      1.1  christos 	      overlay_cache_invalid = 0;
   3068      1.1  christos 	    }
   3069      1.1  christos 	  if (osect->ovly_mapped == -1)
   3070      1.1  christos 	    gdbarch_overlay_update (gdbarch, osect);
   3071      1.1  christos 	}
   3072      1.1  christos       /* fall thru */
   3073      1.1  christos     case ovly_on:		/* overlay debugging manual */
   3074      1.1  christos       return osect->ovly_mapped == 1;
   3075      1.1  christos     }
   3076      1.1  christos }
   3077      1.1  christos 
   3078      1.1  christos /* Function: pc_in_unmapped_range
   3079      1.1  christos    If PC falls into the lma range of SECTION, return true, else false.  */
   3080      1.1  christos 
   3081      1.1  christos CORE_ADDR
   3082      1.1  christos pc_in_unmapped_range (CORE_ADDR pc, struct obj_section *section)
   3083      1.1  christos {
   3084      1.1  christos   if (section_is_overlay (section))
   3085      1.1  christos     {
   3086      1.1  christos       bfd *abfd = section->objfile->obfd;
   3087      1.1  christos       asection *bfd_section = section->the_bfd_section;
   3088      1.1  christos 
   3089      1.1  christos       /* We assume the LMA is relocated by the same offset as the VMA.  */
   3090      1.1  christos       bfd_vma size = bfd_get_section_size (bfd_section);
   3091      1.1  christos       CORE_ADDR offset = obj_section_offset (section);
   3092      1.1  christos 
   3093      1.1  christos       if (bfd_get_section_lma (abfd, bfd_section) + offset <= pc
   3094      1.1  christos 	  && pc < bfd_get_section_lma (abfd, bfd_section) + offset + size)
   3095      1.1  christos 	return 1;
   3096      1.1  christos     }
   3097      1.1  christos 
   3098      1.1  christos   return 0;
   3099      1.1  christos }
   3100      1.1  christos 
   3101      1.1  christos /* Function: pc_in_mapped_range
   3102      1.1  christos    If PC falls into the vma range of SECTION, return true, else false.  */
   3103      1.1  christos 
   3104      1.1  christos CORE_ADDR
   3105      1.1  christos pc_in_mapped_range (CORE_ADDR pc, struct obj_section *section)
   3106      1.1  christos {
   3107      1.1  christos   if (section_is_overlay (section))
   3108      1.1  christos     {
   3109      1.1  christos       if (obj_section_addr (section) <= pc
   3110      1.1  christos 	  && pc < obj_section_endaddr (section))
   3111      1.1  christos 	return 1;
   3112      1.1  christos     }
   3113      1.1  christos 
   3114      1.1  christos   return 0;
   3115      1.1  christos }
   3116      1.1  christos 
   3117      1.1  christos /* Return true if the mapped ranges of sections A and B overlap, false
   3118      1.1  christos    otherwise.  */
   3119      1.1  christos 
   3120      1.1  christos static int
   3121      1.1  christos sections_overlap (struct obj_section *a, struct obj_section *b)
   3122      1.1  christos {
   3123      1.1  christos   CORE_ADDR a_start = obj_section_addr (a);
   3124      1.1  christos   CORE_ADDR a_end = obj_section_endaddr (a);
   3125      1.1  christos   CORE_ADDR b_start = obj_section_addr (b);
   3126      1.1  christos   CORE_ADDR b_end = obj_section_endaddr (b);
   3127      1.1  christos 
   3128      1.1  christos   return (a_start < b_end && b_start < a_end);
   3129      1.1  christos }
   3130      1.1  christos 
   3131      1.1  christos /* Function: overlay_unmapped_address (PC, SECTION)
   3132      1.1  christos    Returns the address corresponding to PC in the unmapped (load) range.
   3133      1.1  christos    May be the same as PC.  */
   3134      1.1  christos 
   3135      1.1  christos CORE_ADDR
   3136      1.1  christos overlay_unmapped_address (CORE_ADDR pc, struct obj_section *section)
   3137      1.1  christos {
   3138      1.1  christos   if (section_is_overlay (section) && pc_in_mapped_range (pc, section))
   3139      1.1  christos     {
   3140      1.1  christos       asection *bfd_section = section->the_bfd_section;
   3141      1.1  christos 
   3142      1.1  christos       return pc + bfd_section_lma (abfd, bfd_section)
   3143      1.1  christos 		- bfd_section_vma (abfd, bfd_section);
   3144      1.1  christos     }
   3145      1.1  christos 
   3146      1.1  christos   return pc;
   3147      1.1  christos }
   3148      1.1  christos 
   3149      1.1  christos /* Function: overlay_mapped_address (PC, SECTION)
   3150      1.1  christos    Returns the address corresponding to PC in the mapped (runtime) range.
   3151      1.1  christos    May be the same as PC.  */
   3152      1.1  christos 
   3153      1.1  christos CORE_ADDR
   3154      1.1  christos overlay_mapped_address (CORE_ADDR pc, struct obj_section *section)
   3155      1.1  christos {
   3156      1.1  christos   if (section_is_overlay (section) && pc_in_unmapped_range (pc, section))
   3157      1.1  christos     {
   3158      1.1  christos       asection *bfd_section = section->the_bfd_section;
   3159      1.1  christos 
   3160      1.1  christos       return pc + bfd_section_vma (abfd, bfd_section)
   3161      1.1  christos 		- bfd_section_lma (abfd, bfd_section);
   3162      1.1  christos     }
   3163      1.1  christos 
   3164      1.1  christos   return pc;
   3165      1.1  christos }
   3166      1.1  christos 
   3167      1.1  christos /* Function: symbol_overlayed_address
   3168      1.1  christos    Return one of two addresses (relative to the VMA or to the LMA),
   3169      1.1  christos    depending on whether the section is mapped or not.  */
   3170      1.1  christos 
   3171      1.1  christos CORE_ADDR
   3172      1.1  christos symbol_overlayed_address (CORE_ADDR address, struct obj_section *section)
   3173      1.1  christos {
   3174      1.1  christos   if (overlay_debugging)
   3175      1.1  christos     {
   3176      1.1  christos       /* If the symbol has no section, just return its regular address.  */
   3177      1.1  christos       if (section == 0)
   3178      1.1  christos 	return address;
   3179      1.1  christos       /* If the symbol's section is not an overlay, just return its
   3180      1.1  christos 	 address.  */
   3181      1.1  christos       if (!section_is_overlay (section))
   3182      1.1  christos 	return address;
   3183      1.1  christos       /* If the symbol's section is mapped, just return its address.  */
   3184      1.1  christos       if (section_is_mapped (section))
   3185      1.1  christos 	return address;
   3186      1.1  christos       /*
   3187      1.1  christos        * HOWEVER: if the symbol is in an overlay section which is NOT mapped,
   3188      1.1  christos        * then return its LOADED address rather than its vma address!!
   3189      1.1  christos        */
   3190      1.1  christos       return overlay_unmapped_address (address, section);
   3191      1.1  christos     }
   3192      1.1  christos   return address;
   3193      1.1  christos }
   3194      1.1  christos 
   3195      1.1  christos /* Function: find_pc_overlay (PC)
   3196      1.1  christos    Return the best-match overlay section for PC:
   3197      1.1  christos    If PC matches a mapped overlay section's VMA, return that section.
   3198      1.1  christos    Else if PC matches an unmapped section's VMA, return that section.
   3199      1.1  christos    Else if PC matches an unmapped section's LMA, return that section.  */
   3200  1.1.1.4  christos 
   3201  1.1.1.6  christos struct obj_section *
   3202  1.1.1.6  christos find_pc_overlay (CORE_ADDR pc)
   3203  1.1.1.6  christos {
   3204  1.1.1.6  christos   struct obj_section *osect, *best_match = NULL;
   3205  1.1.1.6  christos 
   3206  1.1.1.6  christos   if (overlay_debugging)
   3207  1.1.1.6  christos     {
   3208  1.1.1.6  christos       for (objfile *objfile : current_program_space->objfiles ())
   3209  1.1.1.6  christos 	ALL_OBJFILE_OSECTIONS (objfile, osect)
   3210  1.1.1.6  christos 	  if (section_is_overlay (osect))
   3211  1.1.1.6  christos 	    {
   3212  1.1.1.6  christos 	      if (pc_in_mapped_range (pc, osect))
   3213  1.1.1.6  christos 		{
   3214  1.1.1.6  christos 		  if (section_is_mapped (osect))
   3215  1.1.1.4  christos 		    return osect;
   3216      1.1  christos 		  else
   3217      1.1  christos 		    best_match = osect;
   3218      1.1  christos 		}
   3219      1.1  christos 	      else if (pc_in_unmapped_range (pc, osect))
   3220      1.1  christos 		best_match = osect;
   3221      1.1  christos 	    }
   3222      1.1  christos     }
   3223      1.1  christos   return best_match;
   3224      1.1  christos }
   3225      1.1  christos 
   3226      1.1  christos /* Function: find_pc_mapped_section (PC)
   3227      1.1  christos    If PC falls into the VMA address range of an overlay section that is
   3228      1.1  christos    currently marked as MAPPED, return that section.  Else return NULL.  */
   3229  1.1.1.4  christos 
   3230  1.1.1.6  christos struct obj_section *
   3231  1.1.1.6  christos find_pc_mapped_section (CORE_ADDR pc)
   3232  1.1.1.6  christos {
   3233  1.1.1.6  christos   struct obj_section *osect;
   3234  1.1.1.4  christos 
   3235      1.1  christos   if (overlay_debugging)
   3236      1.1  christos     {
   3237      1.1  christos       for (objfile *objfile : current_program_space->objfiles ())
   3238      1.1  christos 	ALL_OBJFILE_OSECTIONS (objfile, osect)
   3239      1.1  christos 	  if (pc_in_mapped_range (pc, osect) && section_is_mapped (osect))
   3240      1.1  christos 	    return osect;
   3241      1.1  christos     }
   3242      1.1  christos 
   3243  1.1.1.6  christos   return NULL;
   3244      1.1  christos }
   3245      1.1  christos 
   3246      1.1  christos /* Function: list_overlays_command
   3247      1.1  christos    Print a list of mapped sections and their PC ranges.  */
   3248      1.1  christos 
   3249  1.1.1.4  christos static void
   3250  1.1.1.6  christos list_overlays_command (const char *args, int from_tty)
   3251  1.1.1.6  christos {
   3252  1.1.1.6  christos   int nmapped = 0;
   3253  1.1.1.6  christos   struct obj_section *osect;
   3254  1.1.1.6  christos 
   3255  1.1.1.6  christos   if (overlay_debugging)
   3256  1.1.1.6  christos     {
   3257  1.1.1.6  christos       for (objfile *objfile : current_program_space->objfiles ())
   3258  1.1.1.6  christos 	ALL_OBJFILE_OSECTIONS (objfile, osect)
   3259  1.1.1.6  christos 	  if (section_is_mapped (osect))
   3260  1.1.1.6  christos 	    {
   3261  1.1.1.6  christos 	      struct gdbarch *gdbarch = get_objfile_arch (objfile);
   3262  1.1.1.6  christos 	      const char *name;
   3263  1.1.1.6  christos 	      bfd_vma lma, vma;
   3264  1.1.1.6  christos 	      int size;
   3265  1.1.1.6  christos 
   3266  1.1.1.6  christos 	      vma = bfd_section_vma (objfile->obfd, osect->the_bfd_section);
   3267  1.1.1.6  christos 	      lma = bfd_section_lma (objfile->obfd, osect->the_bfd_section);
   3268  1.1.1.6  christos 	      size = bfd_get_section_size (osect->the_bfd_section);
   3269  1.1.1.6  christos 	      name = bfd_section_name (objfile->obfd, osect->the_bfd_section);
   3270  1.1.1.6  christos 
   3271  1.1.1.6  christos 	      printf_filtered ("Section %s, loaded at ", name);
   3272  1.1.1.6  christos 	      fputs_filtered (paddress (gdbarch, lma), gdb_stdout);
   3273      1.1  christos 	      puts_filtered (" - ");
   3274  1.1.1.6  christos 	      fputs_filtered (paddress (gdbarch, lma + size), gdb_stdout);
   3275  1.1.1.6  christos 	      printf_filtered (", mapped at ");
   3276  1.1.1.4  christos 	      fputs_filtered (paddress (gdbarch, vma), gdb_stdout);
   3277      1.1  christos 	      puts_filtered (" - ");
   3278      1.1  christos 	      fputs_filtered (paddress (gdbarch, vma + size), gdb_stdout);
   3279      1.1  christos 	      puts_filtered ("\n");
   3280      1.1  christos 
   3281      1.1  christos 	      nmapped++;
   3282      1.1  christos 	    }
   3283      1.1  christos     }
   3284      1.1  christos   if (nmapped == 0)
   3285  1.1.1.6  christos     printf_filtered (_("No sections are mapped.\n"));
   3286      1.1  christos }
   3287      1.1  christos 
   3288      1.1  christos /* Function: map_overlay_command
   3289      1.1  christos    Mark the named section as mapped (ie. residing at its VMA address).  */
   3290      1.1  christos 
   3291      1.1  christos static void
   3292      1.1  christos map_overlay_command (const char *args, int from_tty)
   3293      1.1  christos {
   3294      1.1  christos   struct obj_section *sec, *sec2;
   3295      1.1  christos 
   3296      1.1  christos   if (!overlay_debugging)
   3297      1.1  christos     error (_("Overlay debugging not enabled.  Use "
   3298  1.1.1.6  christos 	     "either the 'overlay auto' or\n"
   3299  1.1.1.6  christos 	     "the 'overlay manual' command."));
   3300  1.1.1.6  christos 
   3301  1.1.1.6  christos   if (args == 0 || *args == 0)
   3302  1.1.1.6  christos     error (_("Argument required: name of an overlay section"));
   3303  1.1.1.6  christos 
   3304  1.1.1.6  christos   /* First, find a section matching the user supplied argument.  */
   3305  1.1.1.6  christos   for (objfile *obj_file : current_program_space->objfiles ())
   3306  1.1.1.6  christos     ALL_OBJFILE_OSECTIONS (obj_file, sec)
   3307  1.1.1.6  christos       if (!strcmp (bfd_section_name (obj_file->obfd, sec->the_bfd_section),
   3308  1.1.1.6  christos 		   args))
   3309  1.1.1.6  christos 	{
   3310  1.1.1.6  christos 	  /* Now, check to see if the section is an overlay.  */
   3311  1.1.1.6  christos 	  if (!section_is_overlay (sec))
   3312  1.1.1.6  christos 	    continue;		/* not an overlay section */
   3313  1.1.1.6  christos 
   3314  1.1.1.6  christos 	  /* Mark the overlay as "mapped".  */
   3315  1.1.1.6  christos 	  sec->ovly_mapped = 1;
   3316  1.1.1.6  christos 
   3317  1.1.1.6  christos 	  /* Next, make a pass and unmap any sections that are
   3318  1.1.1.6  christos 	     overlapped by this new section: */
   3319  1.1.1.6  christos 	  for (objfile *objfile2 : current_program_space->objfiles ())
   3320  1.1.1.6  christos 	    ALL_OBJFILE_OSECTIONS (objfile2, sec2)
   3321  1.1.1.6  christos 	      if (sec2->ovly_mapped && sec != sec2 && sections_overlap (sec,
   3322  1.1.1.6  christos 									sec2))
   3323  1.1.1.6  christos 		{
   3324      1.1  christos 		  if (info_verbose)
   3325      1.1  christos 		    printf_unfiltered (_("Note: section %s unmapped by overlap\n"),
   3326      1.1  christos 				       bfd_section_name (obj_file->obfd,
   3327      1.1  christos 							 sec2->the_bfd_section));
   3328      1.1  christos 		  sec2->ovly_mapped = 0; /* sec2 overlaps sec: unmap sec2.  */
   3329      1.1  christos 		}
   3330      1.1  christos 	  return;
   3331      1.1  christos 	}
   3332      1.1  christos   error (_("No overlay section called %s"), args);
   3333  1.1.1.6  christos }
   3334      1.1  christos 
   3335  1.1.1.3  christos /* Function: unmap_overlay_command
   3336      1.1  christos    Mark the overlay section as unmapped
   3337      1.1  christos    (ie. resident in its LMA address range, rather than the VMA range).  */
   3338      1.1  christos 
   3339      1.1  christos static void
   3340      1.1  christos unmap_overlay_command (const char *args, int from_tty)
   3341      1.1  christos {
   3342      1.1  christos   struct obj_section *sec = NULL;
   3343      1.1  christos 
   3344      1.1  christos   if (!overlay_debugging)
   3345      1.1  christos     error (_("Overlay debugging not enabled.  "
   3346  1.1.1.6  christos 	     "Use either the 'overlay auto' or\n"
   3347  1.1.1.6  christos 	     "the 'overlay manual' command."));
   3348  1.1.1.6  christos 
   3349  1.1.1.6  christos   if (args == 0 || *args == 0)
   3350  1.1.1.6  christos     error (_("Argument required: name of an overlay section"));
   3351  1.1.1.6  christos 
   3352  1.1.1.6  christos   /* First, find a section matching the user supplied argument.  */
   3353  1.1.1.6  christos   for (objfile *objfile : current_program_space->objfiles ())
   3354  1.1.1.6  christos     ALL_OBJFILE_OSECTIONS (objfile, sec)
   3355      1.1  christos       if (!strcmp (bfd_section_name (objfile->obfd, sec->the_bfd_section), args))
   3356      1.1  christos 	{
   3357      1.1  christos 	  if (!sec->ovly_mapped)
   3358      1.1  christos 	    error (_("Section %s is not mapped"), args);
   3359      1.1  christos 	  sec->ovly_mapped = 0;
   3360      1.1  christos 	  return;
   3361      1.1  christos 	}
   3362      1.1  christos   error (_("No overlay section called %s"), args);
   3363  1.1.1.6  christos }
   3364      1.1  christos 
   3365      1.1  christos /* Function: overlay_auto_command
   3366      1.1  christos    A utility command to turn on overlay debugging.
   3367      1.1  christos    Possibly this should be done via a set/show command.  */
   3368      1.1  christos 
   3369      1.1  christos static void
   3370      1.1  christos overlay_auto_command (const char *args, int from_tty)
   3371      1.1  christos {
   3372      1.1  christos   overlay_debugging = ovly_auto;
   3373      1.1  christos   enable_overlay_breakpoints ();
   3374      1.1  christos   if (info_verbose)
   3375      1.1  christos     printf_unfiltered (_("Automatic overlay debugging enabled."));
   3376  1.1.1.6  christos }
   3377      1.1  christos 
   3378      1.1  christos /* Function: overlay_manual_command
   3379      1.1  christos    A utility command to turn on overlay debugging.
   3380      1.1  christos    Possibly this should be done via a set/show command.  */
   3381      1.1  christos 
   3382      1.1  christos static void
   3383      1.1  christos overlay_manual_command (const char *args, int from_tty)
   3384      1.1  christos {
   3385      1.1  christos   overlay_debugging = ovly_on;
   3386      1.1  christos   disable_overlay_breakpoints ();
   3387      1.1  christos   if (info_verbose)
   3388      1.1  christos     printf_unfiltered (_("Overlay debugging enabled."));
   3389  1.1.1.6  christos }
   3390      1.1  christos 
   3391      1.1  christos /* Function: overlay_off_command
   3392      1.1  christos    A utility command to turn on overlay debugging.
   3393      1.1  christos    Possibly this should be done via a set/show command.  */
   3394      1.1  christos 
   3395      1.1  christos static void
   3396      1.1  christos overlay_off_command (const char *args, int from_tty)
   3397      1.1  christos {
   3398  1.1.1.6  christos   overlay_debugging = ovly_off;
   3399      1.1  christos   disable_overlay_breakpoints ();
   3400      1.1  christos   if (info_verbose)
   3401      1.1  christos     printf_unfiltered (_("Overlay debugging disabled."));
   3402      1.1  christos }
   3403      1.1  christos 
   3404      1.1  christos static void
   3405      1.1  christos overlay_load_command (const char *args, int from_tty)
   3406      1.1  christos {
   3407      1.1  christos   struct gdbarch *gdbarch = get_current_arch ();
   3408      1.1  christos 
   3409      1.1  christos   if (gdbarch_overlay_update_p (gdbarch))
   3410      1.1  christos     gdbarch_overlay_update (gdbarch, NULL);
   3411      1.1  christos   else
   3412      1.1  christos     error (_("This target does not know how to read its overlay state."));
   3413      1.1  christos }
   3414      1.1  christos 
   3415  1.1.1.6  christos /* Function: overlay_command
   3416      1.1  christos    A place-holder for a mis-typed command.  */
   3417      1.1  christos 
   3418      1.1  christos /* Command list chain containing all defined "overlay" subcommands.  */
   3419  1.1.1.2  christos static struct cmd_list_element *overlaylist;
   3420      1.1  christos 
   3421      1.1  christos static void
   3422      1.1  christos overlay_command (const char *args, int from_tty)
   3423      1.1  christos {
   3424      1.1  christos   printf_unfiltered
   3425      1.1  christos     ("\"overlay\" must be followed by the name of an overlay command.\n");
   3426      1.1  christos   help_list (overlaylist, "overlay ", all_commands, gdb_stdout);
   3427      1.1  christos }
   3428      1.1  christos 
   3429      1.1  christos /* Target Overlays for the "Simplest" overlay manager:
   3430      1.1  christos 
   3431      1.1  christos    This is GDB's default target overlay layer.  It works with the
   3432      1.1  christos    minimal overlay manager supplied as an example by Cygnus.  The
   3433      1.1  christos    entry point is via a function pointer "gdbarch_overlay_update",
   3434      1.1  christos    so targets that use a different runtime overlay manager can
   3435      1.1  christos    substitute their own overlay_update function and take over the
   3436      1.1  christos    function pointer.
   3437      1.1  christos 
   3438  1.1.1.4  christos    The overlay_update function pokes around in the target's data structures
   3439      1.1  christos    to see what overlays are mapped, and updates GDB's overlay mapping with
   3440      1.1  christos    this information.
   3441      1.1  christos 
   3442      1.1  christos    In this simple implementation, the target data structures are as follows:
   3443  1.1.1.4  christos    unsigned _novlys;            /# number of overlay sections #/
   3444      1.1  christos    unsigned _ovly_table[_novlys][4] = {
   3445      1.1  christos    {VMA, OSIZE, LMA, MAPPED},    /# one entry per overlay section #/
   3446      1.1  christos    {..., ...,  ..., ...},
   3447      1.1  christos    }
   3448      1.1  christos    unsigned _novly_regions;     /# number of overlay regions #/
   3449      1.1  christos    unsigned _ovly_region_table[_novly_regions][3] = {
   3450      1.1  christos    {VMA, OSIZE, MAPPED_TO_LMA},  /# one entry per overlay region #/
   3451      1.1  christos    {..., ...,  ...},
   3452      1.1  christos    }
   3453      1.1  christos    These functions will attempt to update GDB's mappedness state in the
   3454      1.1  christos    symbol section table, based on the target's mappedness state.
   3455      1.1  christos 
   3456      1.1  christos    To do this, we keep a cached copy of the target's _ovly_table, and
   3457      1.1  christos    attempt to detect when the cached copy is invalidated.  The main
   3458      1.1  christos    entry point is "simple_overlay_update(SECT), which looks up SECT in
   3459      1.1  christos    the cached table and re-reads only the entry for that section from
   3460      1.1  christos    the target (whenever possible).  */
   3461  1.1.1.4  christos 
   3462      1.1  christos /* Cached, dynamically allocated copies of the target data structures: */
   3463      1.1  christos static unsigned (*cache_ovly_table)[4] = 0;
   3464      1.1  christos static unsigned cache_novlys = 0;
   3465      1.1  christos static CORE_ADDR cache_ovly_table_base = 0;
   3466      1.1  christos enum ovly_index
   3467      1.1  christos   {
   3468      1.1  christos     VMA, OSIZE, LMA, MAPPED
   3469      1.1  christos   };
   3470      1.1  christos 
   3471      1.1  christos /* Throw away the cached copy of _ovly_table.  */
   3472      1.1  christos 
   3473      1.1  christos static void
   3474      1.1  christos simple_free_overlay_table (void)
   3475      1.1  christos {
   3476      1.1  christos   if (cache_ovly_table)
   3477      1.1  christos     xfree (cache_ovly_table);
   3478      1.1  christos   cache_novlys = 0;
   3479      1.1  christos   cache_ovly_table = NULL;
   3480      1.1  christos   cache_ovly_table_base = 0;
   3481      1.1  christos }
   3482      1.1  christos 
   3483      1.1  christos /* Read an array of ints of size SIZE from the target into a local buffer.
   3484  1.1.1.4  christos    Convert to host order.  int LEN is number of ints.  */
   3485      1.1  christos 
   3486      1.1  christos static void
   3487      1.1  christos read_target_long_array (CORE_ADDR memaddr, unsigned int *myaddr,
   3488      1.1  christos 			int len, int size, enum bfd_endian byte_order)
   3489      1.1  christos {
   3490      1.1  christos   /* FIXME (alloca): Not safe if array is very large.  */
   3491      1.1  christos   gdb_byte *buf = (gdb_byte *) alloca (len * size);
   3492      1.1  christos   int i;
   3493      1.1  christos 
   3494      1.1  christos   read_memory (memaddr, buf, len * size);
   3495      1.1  christos   for (i = 0; i < len; i++)
   3496      1.1  christos     myaddr[i] = extract_unsigned_integer (size * i + buf, size, byte_order);
   3497      1.1  christos }
   3498  1.1.1.2  christos 
   3499      1.1  christos /* Find and grab a copy of the target _ovly_table
   3500      1.1  christos    (and _novlys, which is needed for the table's size).  */
   3501      1.1  christos 
   3502      1.1  christos static int
   3503      1.1  christos simple_read_overlay_table (void)
   3504      1.1  christos {
   3505      1.1  christos   struct bound_minimal_symbol novlys_msym;
   3506  1.1.1.2  christos   struct bound_minimal_symbol ovly_table_msym;
   3507      1.1  christos   struct gdbarch *gdbarch;
   3508      1.1  christos   int word_size;
   3509      1.1  christos   enum bfd_endian byte_order;
   3510      1.1  christos 
   3511      1.1  christos   simple_free_overlay_table ();
   3512      1.1  christos   novlys_msym = lookup_minimal_symbol ("_novlys", NULL, NULL);
   3513      1.1  christos   if (! novlys_msym.minsym)
   3514      1.1  christos     {
   3515      1.1  christos       error (_("Error reading inferior's overlay table: "
   3516      1.1  christos              "couldn't find `_novlys' variable\n"
   3517      1.1  christos              "in inferior.  Use `overlay manual' mode."));
   3518      1.1  christos       return 0;
   3519      1.1  christos     }
   3520      1.1  christos 
   3521      1.1  christos   ovly_table_msym = lookup_bound_minimal_symbol ("_ovly_table");
   3522      1.1  christos   if (! ovly_table_msym.minsym)
   3523      1.1  christos     {
   3524      1.1  christos       error (_("Error reading inferior's overlay table: couldn't find "
   3525      1.1  christos              "`_ovly_table' array\n"
   3526      1.1  christos              "in inferior.  Use `overlay manual' mode."));
   3527  1.1.1.2  christos       return 0;
   3528      1.1  christos     }
   3529      1.1  christos 
   3530  1.1.1.4  christos   gdbarch = get_objfile_arch (ovly_table_msym.objfile);
   3531  1.1.1.2  christos   word_size = gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT;
   3532      1.1  christos   byte_order = gdbarch_byte_order (gdbarch);
   3533      1.1  christos 
   3534      1.1  christos   cache_novlys = read_memory_integer (BMSYMBOL_VALUE_ADDRESS (novlys_msym),
   3535      1.1  christos 				      4, byte_order);
   3536      1.1  christos   cache_ovly_table
   3537      1.1  christos     = (unsigned int (*)[4]) xmalloc (cache_novlys * sizeof (*cache_ovly_table));
   3538      1.1  christos   cache_ovly_table_base = BMSYMBOL_VALUE_ADDRESS (ovly_table_msym);
   3539      1.1  christos   read_target_long_array (cache_ovly_table_base,
   3540      1.1  christos                           (unsigned int *) cache_ovly_table,
   3541      1.1  christos                           cache_novlys * 4, word_size, byte_order);
   3542      1.1  christos 
   3543      1.1  christos   return 1;			/* SUCCESS */
   3544      1.1  christos }
   3545      1.1  christos 
   3546      1.1  christos /* Function: simple_overlay_update_1
   3547      1.1  christos    A helper function for simple_overlay_update.  Assuming a cached copy
   3548      1.1  christos    of _ovly_table exists, look through it to find an entry whose vma,
   3549      1.1  christos    lma and size match those of OSECT.  Re-read the entry and make sure
   3550  1.1.1.4  christos    it still matches OSECT (else the table may no longer be valid).
   3551      1.1  christos    Set OSECT's mapped state to match the entry.  Return: 1 for
   3552      1.1  christos    success, 0 for failure.  */
   3553      1.1  christos 
   3554      1.1  christos static int
   3555      1.1  christos simple_overlay_update_1 (struct obj_section *osect)
   3556      1.1  christos {
   3557      1.1  christos   int i;
   3558  1.1.1.4  christos   asection *bsect = osect->the_bfd_section;
   3559      1.1  christos   struct gdbarch *gdbarch = get_objfile_arch (osect->objfile);
   3560      1.1  christos   int word_size = gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT;
   3561      1.1  christos   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
   3562      1.1  christos 
   3563      1.1  christos   for (i = 0; i < cache_novlys; i++)
   3564  1.1.1.4  christos     if (cache_ovly_table[i][VMA] == bfd_section_vma (obfd, bsect)
   3565      1.1  christos 	&& cache_ovly_table[i][LMA] == bfd_section_lma (obfd, bsect))
   3566      1.1  christos       {
   3567      1.1  christos 	read_target_long_array (cache_ovly_table_base + i * word_size,
   3568      1.1  christos 				(unsigned int *) cache_ovly_table[i],
   3569      1.1  christos 				4, word_size, byte_order);
   3570      1.1  christos 	if (cache_ovly_table[i][VMA] == bfd_section_vma (obfd, bsect)
   3571      1.1  christos 	    && cache_ovly_table[i][LMA] == bfd_section_lma (obfd, bsect))
   3572      1.1  christos 	  {
   3573      1.1  christos 	    osect->ovly_mapped = cache_ovly_table[i][MAPPED];
   3574      1.1  christos 	    return 1;
   3575      1.1  christos 	  }
   3576      1.1  christos 	else	/* Warning!  Warning!  Target's ovly table has changed!  */
   3577      1.1  christos 	  return 0;
   3578      1.1  christos       }
   3579      1.1  christos   return 0;
   3580      1.1  christos }
   3581      1.1  christos 
   3582      1.1  christos /* Function: simple_overlay_update
   3583      1.1  christos    If OSECT is NULL, then update all sections' mapped state
   3584      1.1  christos    (after re-reading the entire target _ovly_table).
   3585      1.1  christos    If OSECT is non-NULL, then try to find a matching entry in the
   3586      1.1  christos    cached ovly_table and update only OSECT's mapped state.
   3587      1.1  christos    If a cached entry can't be found or the cache isn't valid, then
   3588      1.1  christos    re-read the entire cache, and go ahead and update all sections.  */
   3589      1.1  christos 
   3590      1.1  christos void
   3591      1.1  christos simple_overlay_update (struct obj_section *osect)
   3592      1.1  christos {
   3593  1.1.1.2  christos   /* Were we given an osect to look up?  NULL means do all of them.  */
   3594      1.1  christos   if (osect)
   3595      1.1  christos     /* Have we got a cached copy of the target's overlay table?  */
   3596  1.1.1.2  christos     if (cache_ovly_table != NULL)
   3597      1.1  christos       {
   3598      1.1  christos 	/* Does its cached location match what's currently in the
   3599      1.1  christos 	   symtab?  */
   3600      1.1  christos 	struct bound_minimal_symbol minsym
   3601  1.1.1.2  christos 	  = lookup_minimal_symbol ("_ovly_table", NULL, NULL);
   3602      1.1  christos 
   3603      1.1  christos 	if (minsym.minsym == NULL)
   3604      1.1  christos 	  error (_("Error reading inferior's overlay table: couldn't "
   3605      1.1  christos 		   "find `_ovly_table' array\n"
   3606      1.1  christos 		   "in inferior.  Use `overlay manual' mode."));
   3607      1.1  christos 
   3608      1.1  christos 	if (cache_ovly_table_base == BMSYMBOL_VALUE_ADDRESS (minsym))
   3609      1.1  christos 	  /* Then go ahead and try to look up this single section in
   3610      1.1  christos 	     the cache.  */
   3611      1.1  christos 	  if (simple_overlay_update_1 (osect))
   3612      1.1  christos 	    /* Found it!  We're done.  */
   3613      1.1  christos 	    return;
   3614      1.1  christos       }
   3615      1.1  christos 
   3616      1.1  christos   /* Cached table no good: need to read the entire table anew.
   3617  1.1.1.6  christos      Or else we want all the sections, in which case it's actually
   3618  1.1.1.6  christos      more efficient to read the whole table in one block anyway.  */
   3619  1.1.1.6  christos 
   3620  1.1.1.6  christos   if (! simple_read_overlay_table ())
   3621  1.1.1.6  christos     return;
   3622  1.1.1.6  christos 
   3623  1.1.1.6  christos   /* Now may as well update all sections, even if only one was requested.  */
   3624  1.1.1.6  christos   for (objfile *objfile : current_program_space->objfiles ())
   3625  1.1.1.6  christos     ALL_OBJFILE_OSECTIONS (objfile, osect)
   3626  1.1.1.6  christos       if (section_is_overlay (osect))
   3627  1.1.1.6  christos 	{
   3628  1.1.1.6  christos 	  int i;
   3629  1.1.1.6  christos 	  asection *bsect = osect->the_bfd_section;
   3630  1.1.1.6  christos 
   3631  1.1.1.6  christos 	  for (i = 0; i < cache_novlys; i++)
   3632      1.1  christos 	    if (cache_ovly_table[i][VMA] == bfd_section_vma (obfd, bsect)
   3633      1.1  christos 		&& cache_ovly_table[i][LMA] == bfd_section_lma (obfd, bsect))
   3634      1.1  christos 	      { /* obj_section matches i'th entry in ovly_table.  */
   3635      1.1  christos 		osect->ovly_mapped = cache_ovly_table[i][MAPPED];
   3636      1.1  christos 		break;		/* finished with inner for loop: break out.  */
   3637      1.1  christos 	      }
   3638      1.1  christos 	}
   3639      1.1  christos }
   3640      1.1  christos 
   3641      1.1  christos /* Set the output sections and output offsets for section SECTP in
   3642      1.1  christos    ABFD.  The relocation code in BFD will read these offsets, so we
   3643      1.1  christos    need to be sure they're initialized.  We map each section to itself,
   3644      1.1  christos    with no offset; this means that SECTP->vma will be honored.  */
   3645      1.1  christos 
   3646      1.1  christos static void
   3647      1.1  christos symfile_dummy_outputs (bfd *abfd, asection *sectp, void *dummy)
   3648      1.1  christos {
   3649      1.1  christos   sectp->output_section = sectp;
   3650      1.1  christos   sectp->output_offset = 0;
   3651      1.1  christos }
   3652      1.1  christos 
   3653      1.1  christos /* Default implementation for sym_relocate.  */
   3654      1.1  christos 
   3655      1.1  christos bfd_byte *
   3656      1.1  christos default_symfile_relocate (struct objfile *objfile, asection *sectp,
   3657      1.1  christos                           bfd_byte *buf)
   3658      1.1  christos {
   3659      1.1  christos   /* Use sectp->owner instead of objfile->obfd.  sectp may point to a
   3660      1.1  christos      DWO file.  */
   3661      1.1  christos   bfd *abfd = sectp->owner;
   3662      1.1  christos 
   3663      1.1  christos   /* We're only interested in sections with relocation
   3664      1.1  christos      information.  */
   3665      1.1  christos   if ((sectp->flags & SEC_RELOC) == 0)
   3666      1.1  christos     return NULL;
   3667      1.1  christos 
   3668      1.1  christos   /* We will handle section offsets properly elsewhere, so relocate as if
   3669      1.1  christos      all sections begin at 0.  */
   3670      1.1  christos   bfd_map_over_sections (abfd, symfile_dummy_outputs, NULL);
   3671      1.1  christos 
   3672      1.1  christos   return bfd_simple_get_relocated_section_contents (abfd, sectp, buf, NULL);
   3673      1.1  christos }
   3674      1.1  christos 
   3675      1.1  christos /* Relocate the contents of a debug section SECTP in ABFD.  The
   3676      1.1  christos    contents are stored in BUF if it is non-NULL, or returned in a
   3677      1.1  christos    malloc'd buffer otherwise.
   3678      1.1  christos 
   3679      1.1  christos    For some platforms and debug info formats, shared libraries contain
   3680      1.1  christos    relocations against the debug sections (particularly for DWARF-2;
   3681      1.1  christos    one affected platform is PowerPC GNU/Linux, although it depends on
   3682      1.1  christos    the version of the linker in use).  Also, ELF object files naturally
   3683      1.1  christos    have unresolved relocations for their debug sections.  We need to apply
   3684      1.1  christos    the relocations in order to get the locations of symbols correct.
   3685      1.1  christos    Another example that may require relocation processing, is the
   3686      1.1  christos    DWARF-2 .eh_frame section in .o files, although it isn't strictly a
   3687      1.1  christos    debug section.  */
   3688      1.1  christos 
   3689      1.1  christos bfd_byte *
   3690      1.1  christos symfile_relocate_debug_section (struct objfile *objfile,
   3691      1.1  christos                                 asection *sectp, bfd_byte *buf)
   3692      1.1  christos {
   3693      1.1  christos   gdb_assert (objfile->sf->sym_relocate);
   3694      1.1  christos 
   3695      1.1  christos   return (*objfile->sf->sym_relocate) (objfile, sectp, buf);
   3696      1.1  christos }
   3697      1.1  christos 
   3698      1.1  christos struct symfile_segment_data *
   3699      1.1  christos get_symfile_segment_data (bfd *abfd)
   3700      1.1  christos {
   3701      1.1  christos   const struct sym_fns *sf = find_sym_fns (abfd);
   3702      1.1  christos 
   3703      1.1  christos   if (sf == NULL)
   3704      1.1  christos     return NULL;
   3705      1.1  christos 
   3706      1.1  christos   return sf->sym_segments (abfd);
   3707      1.1  christos }
   3708      1.1  christos 
   3709      1.1  christos void
   3710      1.1  christos free_symfile_segment_data (struct symfile_segment_data *data)
   3711      1.1  christos {
   3712      1.1  christos   xfree (data->segment_bases);
   3713      1.1  christos   xfree (data->segment_sizes);
   3714      1.1  christos   xfree (data->segment_info);
   3715      1.1  christos   xfree (data);
   3716      1.1  christos }
   3717      1.1  christos 
   3718      1.1  christos /* Given:
   3719      1.1  christos    - DATA, containing segment addresses from the object file ABFD, and
   3720      1.1  christos      the mapping from ABFD's sections onto the segments that own them,
   3721      1.1  christos      and
   3722      1.1  christos    - SEGMENT_BASES[0 .. NUM_SEGMENT_BASES - 1], holding the actual
   3723      1.1  christos      segment addresses reported by the target,
   3724      1.1  christos    store the appropriate offsets for each section in OFFSETS.
   3725      1.1  christos 
   3726      1.1  christos    If there are fewer entries in SEGMENT_BASES than there are segments
   3727      1.1  christos    in DATA, then apply SEGMENT_BASES' last entry to all the segments.
   3728      1.1  christos 
   3729      1.1  christos    If there are more entries, then ignore the extra.  The target may
   3730      1.1  christos    not be able to distinguish between an empty data segment and a
   3731      1.1  christos    missing data segment; a missing text segment is less plausible.  */
   3732      1.1  christos 
   3733      1.1  christos int
   3734      1.1  christos symfile_map_offsets_to_segments (bfd *abfd,
   3735      1.1  christos 				 const struct symfile_segment_data *data,
   3736      1.1  christos 				 struct section_offsets *offsets,
   3737      1.1  christos 				 int num_segment_bases,
   3738      1.1  christos 				 const CORE_ADDR *segment_bases)
   3739      1.1  christos {
   3740      1.1  christos   int i;
   3741      1.1  christos   asection *sect;
   3742      1.1  christos 
   3743      1.1  christos   /* It doesn't make sense to call this function unless you have some
   3744      1.1  christos      segment base addresses.  */
   3745      1.1  christos   gdb_assert (num_segment_bases > 0);
   3746      1.1  christos 
   3747      1.1  christos   /* If we do not have segment mappings for the object file, we
   3748      1.1  christos      can not relocate it by segments.  */
   3749      1.1  christos   gdb_assert (data != NULL);
   3750      1.1  christos   gdb_assert (data->num_segments > 0);
   3751      1.1  christos 
   3752      1.1  christos   for (i = 0, sect = abfd->sections; sect != NULL; i++, sect = sect->next)
   3753      1.1  christos     {
   3754      1.1  christos       int which = data->segment_info[i];
   3755      1.1  christos 
   3756      1.1  christos       gdb_assert (0 <= which && which <= data->num_segments);
   3757      1.1  christos 
   3758      1.1  christos       /* Don't bother computing offsets for sections that aren't
   3759      1.1  christos          loaded as part of any segment.  */
   3760      1.1  christos       if (! which)
   3761      1.1  christos         continue;
   3762      1.1  christos 
   3763      1.1  christos       /* Use the last SEGMENT_BASES entry as the address of any extra
   3764      1.1  christos          segments mentioned in DATA->segment_info.  */
   3765      1.1  christos       if (which > num_segment_bases)
   3766      1.1  christos         which = num_segment_bases;
   3767      1.1  christos 
   3768      1.1  christos       offsets->offsets[i] = (segment_bases[which - 1]
   3769      1.1  christos                              - data->segment_bases[which - 1]);
   3770      1.1  christos     }
   3771      1.1  christos 
   3772      1.1  christos   return 1;
   3773      1.1  christos }
   3774      1.1  christos 
   3775      1.1  christos static void
   3776      1.1  christos symfile_find_segment_sections (struct objfile *objfile)
   3777      1.1  christos {
   3778      1.1  christos   bfd *abfd = objfile->obfd;
   3779      1.1  christos   int i;
   3780      1.1  christos   asection *sect;
   3781      1.1  christos   struct symfile_segment_data *data;
   3782      1.1  christos 
   3783      1.1  christos   data = get_symfile_segment_data (objfile->obfd);
   3784      1.1  christos   if (data == NULL)
   3785      1.1  christos     return;
   3786      1.1  christos 
   3787      1.1  christos   if (data->num_segments != 1 && data->num_segments != 2)
   3788      1.1  christos     {
   3789      1.1  christos       free_symfile_segment_data (data);
   3790      1.1  christos       return;
   3791      1.1  christos     }
   3792      1.1  christos 
   3793      1.1  christos   for (i = 0, sect = abfd->sections; sect != NULL; i++, sect = sect->next)
   3794      1.1  christos     {
   3795      1.1  christos       int which = data->segment_info[i];
   3796      1.1  christos 
   3797      1.1  christos       if (which == 1)
   3798      1.1  christos 	{
   3799      1.1  christos 	  if (objfile->sect_index_text == -1)
   3800      1.1  christos 	    objfile->sect_index_text = sect->index;
   3801      1.1  christos 
   3802      1.1  christos 	  if (objfile->sect_index_rodata == -1)
   3803      1.1  christos 	    objfile->sect_index_rodata = sect->index;
   3804      1.1  christos 	}
   3805      1.1  christos       else if (which == 2)
   3806      1.1  christos 	{
   3807      1.1  christos 	  if (objfile->sect_index_data == -1)
   3808      1.1  christos 	    objfile->sect_index_data = sect->index;
   3809      1.1  christos 
   3810      1.1  christos 	  if (objfile->sect_index_bss == -1)
   3811      1.1  christos 	    objfile->sect_index_bss = sect->index;
   3812      1.1  christos 	}
   3813      1.1  christos     }
   3814      1.1  christos 
   3815      1.1  christos   free_symfile_segment_data (data);
   3816  1.1.1.2  christos }
   3817  1.1.1.2  christos 
   3818      1.1  christos /* Listen for free_objfile events.  */
   3819      1.1  christos 
   3820      1.1  christos static void
   3821  1.1.1.2  christos symfile_free_objfile (struct objfile *objfile)
   3822  1.1.1.2  christos {
   3823  1.1.1.2  christos   /* Remove the target sections owned by this objfile.  */
   3824  1.1.1.2  christos   if (objfile != NULL)
   3825  1.1.1.2  christos     remove_target_sections ((void *) objfile);
   3826  1.1.1.5  christos }
   3827  1.1.1.5  christos 
   3828  1.1.1.6  christos /* Wrapper around the quick_symbol_functions expand_symtabs_matching "method".
   3829  1.1.1.5  christos    Expand all symtabs that match the specified criteria.
   3830  1.1.1.5  christos    See quick_symbol_functions.expand_symtabs_matching for details.  */
   3831  1.1.1.5  christos 
   3832  1.1.1.2  christos void
   3833  1.1.1.6  christos expand_symtabs_matching
   3834  1.1.1.6  christos   (gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
   3835  1.1.1.6  christos    const lookup_name_info &lookup_name,
   3836  1.1.1.6  christos    gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
   3837  1.1.1.6  christos    gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
   3838  1.1.1.6  christos    enum search_domain kind)
   3839  1.1.1.6  christos {
   3840  1.1.1.6  christos   for (objfile *objfile : current_program_space->objfiles ())
   3841  1.1.1.2  christos     {
   3842  1.1.1.2  christos       if (objfile->sf)
   3843  1.1.1.2  christos 	objfile->sf->qf->expand_symtabs_matching (objfile, file_matcher,
   3844  1.1.1.2  christos 						  lookup_name,
   3845  1.1.1.2  christos 						  symbol_matcher,
   3846  1.1.1.2  christos 						  expansion_notify, kind);
   3847  1.1.1.2  christos     }
   3848  1.1.1.2  christos }
   3849  1.1.1.2  christos 
   3850  1.1.1.2  christos /* Wrapper around the quick_symbol_functions map_symbol_filenames "method".
   3851  1.1.1.6  christos    Map function FUN over every file.
   3852  1.1.1.6  christos    See quick_symbol_functions.map_symbol_filenames for details.  */
   3853  1.1.1.6  christos 
   3854  1.1.1.6  christos void
   3855  1.1.1.6  christos map_symbol_filenames (symbol_filename_ftype *fun, void *data,
   3856  1.1.1.6  christos 		      int need_fullname)
   3857  1.1.1.6  christos {
   3858  1.1.1.2  christos   for (objfile *objfile : current_program_space->objfiles ())
   3859  1.1.1.6  christos     {
   3860  1.1.1.6  christos       if (objfile->sf)
   3861  1.1.1.6  christos 	objfile->sf->qf->map_symbol_filenames (objfile, fun, data,
   3862  1.1.1.6  christos 					       need_fullname);
   3863  1.1.1.6  christos     }
   3864  1.1.1.6  christos }
   3865  1.1.1.6  christos 
   3866  1.1.1.6  christos #if GDB_SELF_TEST
   3867  1.1.1.6  christos 
   3868  1.1.1.6  christos namespace selftests {
   3869  1.1.1.6  christos namespace filename_language {
   3870  1.1.1.6  christos 
   3871  1.1.1.6  christos static void test_filename_language ()
   3872  1.1.1.6  christos {
   3873  1.1.1.6  christos   /* This test messes up the filename_language_table global.  */
   3874  1.1.1.6  christos   scoped_restore restore_flt = make_scoped_restore (&filename_language_table);
   3875  1.1.1.6  christos 
   3876  1.1.1.6  christos   /* Test deducing an unknown extension.  */
   3877  1.1.1.6  christos   language lang = deduce_language_from_filename ("myfile.blah");
   3878  1.1.1.6  christos   SELF_CHECK (lang == language_unknown);
   3879  1.1.1.6  christos 
   3880  1.1.1.6  christos   /* Test deducing a known extension.  */
   3881  1.1.1.6  christos   lang = deduce_language_from_filename ("myfile.c");
   3882  1.1.1.6  christos   SELF_CHECK (lang == language_c);
   3883  1.1.1.6  christos 
   3884  1.1.1.6  christos   /* Test adding a new extension using the internal API.  */
   3885  1.1.1.6  christos   add_filename_language (".blah", language_pascal);
   3886  1.1.1.6  christos   lang = deduce_language_from_filename ("myfile.blah");
   3887  1.1.1.6  christos   SELF_CHECK (lang == language_pascal);
   3888  1.1.1.6  christos }
   3889  1.1.1.6  christos 
   3890  1.1.1.6  christos static void
   3891  1.1.1.6  christos test_set_ext_lang_command ()
   3892  1.1.1.6  christos {
   3893  1.1.1.6  christos   /* This test messes up the filename_language_table global.  */
   3894  1.1.1.6  christos   scoped_restore restore_flt = make_scoped_restore (&filename_language_table);
   3895  1.1.1.6  christos 
   3896  1.1.1.6  christos   /* Confirm that the .hello extension is not known.  */
   3897  1.1.1.6  christos   language lang = deduce_language_from_filename ("cake.hello");
   3898  1.1.1.6  christos   SELF_CHECK (lang == language_unknown);
   3899  1.1.1.6  christos 
   3900  1.1.1.6  christos   /* Test adding a new extension using the CLI command.  */
   3901  1.1.1.6  christos   gdb::unique_xmalloc_ptr<char> args_holder (xstrdup (".hello rust"));
   3902  1.1.1.6  christos   ext_args = args_holder.get ();
   3903  1.1.1.6  christos   set_ext_lang_command (NULL, 1, NULL);
   3904  1.1.1.6  christos 
   3905  1.1.1.6  christos   lang = deduce_language_from_filename ("cake.hello");
   3906  1.1.1.6  christos   SELF_CHECK (lang == language_rust);
   3907  1.1.1.6  christos 
   3908  1.1.1.6  christos   /* Test overriding an existing extension using the CLI command.  */
   3909  1.1.1.6  christos   int size_before = filename_language_table.size ();
   3910  1.1.1.6  christos   args_holder.reset (xstrdup (".hello pascal"));
   3911  1.1.1.2  christos   ext_args = args_holder.get ();
   3912  1.1.1.2  christos   set_ext_lang_command (NULL, 1, NULL);
   3913  1.1.1.6  christos   int size_after = filename_language_table.size ();
   3914  1.1.1.6  christos 
   3915  1.1.1.6  christos   lang = deduce_language_from_filename ("cake.hello");
   3916  1.1.1.6  christos   SELF_CHECK (lang == language_pascal);
   3917  1.1.1.6  christos   SELF_CHECK (size_before == size_after);
   3918      1.1  christos }
   3919      1.1  christos 
   3920      1.1  christos } /* namespace filename_language */
   3921      1.1  christos } /* namespace selftests */
   3922      1.1  christos 
   3923  1.1.1.6  christos #endif /* GDB_SELF_TEST */
   3924  1.1.1.6  christos 
   3925  1.1.1.6  christos void
   3926  1.1.1.6  christos _initialize_symfile (void)
   3927  1.1.1.6  christos {
   3928  1.1.1.6  christos   struct cmd_list_element *c;
   3929  1.1.1.6  christos 
   3930  1.1.1.6  christos   gdb::observers::free_objfile.attach (symfile_free_objfile);
   3931      1.1  christos 
   3932      1.1  christos #define READNOW_READNEVER_HELP \
   3933      1.1  christos   "The '-readnow' option will cause GDB to read the entire symbol file\n\
   3934  1.1.1.6  christos immediately.  This makes the command slower, but may make future operations\n\
   3935  1.1.1.6  christos faster.\n\
   3936      1.1  christos The '-readnever' option will prevent GDB from reading the symbol file's\n\
   3937  1.1.1.6  christos symbolic debug information."
   3938      1.1  christos 
   3939      1.1  christos   c = add_cmd ("symbol-file", class_files, symbol_file_command, _("\
   3940      1.1  christos Load symbol table from executable file FILE.\n\
   3941      1.1  christos Usage: symbol-file [-readnow | -readnever] [-o OFF] FILE\n\
   3942  1.1.1.6  christos OFF is an optional offset which is added to each section address.\n\
   3943  1.1.1.6  christos The `file' command can also load symbol tables, as well as setting the file\n\
   3944  1.1.1.6  christos to execute.\n" READNOW_READNEVER_HELP), &cmdlist);
   3945  1.1.1.6  christos   set_cmd_completer (c, filename_completer);
   3946      1.1  christos 
   3947  1.1.1.6  christos   c = add_cmd ("add-symbol-file", class_files, add_symbol_file_command, _("\
   3948  1.1.1.6  christos Load symbols from FILE, assuming FILE has been dynamically loaded.\n\
   3949  1.1.1.6  christos Usage: add-symbol-file FILE [-readnow | -readnever] [-o OFF] [ADDR] \
   3950  1.1.1.6  christos [-s SECT-NAME SECT-ADDR]...\n\
   3951      1.1  christos ADDR is the starting address of the file's text.\n\
   3952      1.1  christos Each '-s' argument provides a section name and address, and\n\
   3953      1.1  christos should be specified if the data and bss segments are not contiguous\n\
   3954      1.1  christos with the text.  SECT-NAME is a section name to be loaded at SECT-ADDR.\n\
   3955      1.1  christos OFF is an optional offset which is added to the default load addresses\n\
   3956      1.1  christos of all sections for which no other address was specified.\n"
   3957      1.1  christos READNOW_READNEVER_HELP),
   3958      1.1  christos 	       &cmdlist);
   3959      1.1  christos   set_cmd_completer (c, filename_completer);
   3960      1.1  christos 
   3961      1.1  christos   c = add_cmd ("remove-symbol-file", class_files,
   3962      1.1  christos 	       remove_symbol_file_command, _("\
   3963      1.1  christos Remove a symbol file added via the add-symbol-file command.\n\
   3964      1.1  christos Usage: remove-symbol-file FILENAME\n\
   3965      1.1  christos        remove-symbol-file -a ADDRESS\n\
   3966  1.1.1.6  christos The file to remove can be identified by its filename or by an address\n\
   3967  1.1.1.5  christos that lies within the boundaries of this symbol file in memory."),
   3968  1.1.1.5  christos 	       &cmdlist);
   3969  1.1.1.6  christos 
   3970      1.1  christos   c = add_cmd ("load", class_files, load_command, _("\
   3971      1.1  christos Dynamically load FILE into the running program, and record its symbols\n\
   3972      1.1  christos for access from GDB.\n\
   3973      1.1  christos Usage: load [FILE] [OFFSET]\n\
   3974      1.1  christos An optional load OFFSET may also be given as a literal address.\n\
   3975      1.1  christos When OFFSET is provided, FILE must also be provided.  FILE can be provided\n\
   3976      1.1  christos on its own."), &cmdlist);
   3977      1.1  christos   set_cmd_completer (c, filename_completer);
   3978      1.1  christos 
   3979      1.1  christos   add_prefix_cmd ("overlay", class_support, overlay_command,
   3980      1.1  christos 		  _("Commands for debugging overlays."), &overlaylist,
   3981      1.1  christos 		  "overlay ", 0, &cmdlist);
   3982      1.1  christos 
   3983      1.1  christos   add_com_alias ("ovly", "overlay", class_alias, 1);
   3984      1.1  christos   add_com_alias ("ov", "overlay", class_alias, 1);
   3985      1.1  christos 
   3986      1.1  christos   add_cmd ("map-overlay", class_support, map_overlay_command,
   3987      1.1  christos 	   _("Assert that an overlay section is mapped."), &overlaylist);
   3988      1.1  christos 
   3989      1.1  christos   add_cmd ("unmap-overlay", class_support, unmap_overlay_command,
   3990      1.1  christos 	   _("Assert that an overlay section is unmapped."), &overlaylist);
   3991      1.1  christos 
   3992      1.1  christos   add_cmd ("list-overlays", class_support, list_overlays_command,
   3993      1.1  christos 	   _("List mappings of overlay sections."), &overlaylist);
   3994      1.1  christos 
   3995      1.1  christos   add_cmd ("manual", class_support, overlay_manual_command,
   3996      1.1  christos 	   _("Enable overlay debugging."), &overlaylist);
   3997      1.1  christos   add_cmd ("off", class_support, overlay_off_command,
   3998      1.1  christos 	   _("Disable overlay debugging."), &overlaylist);
   3999      1.1  christos   add_cmd ("auto", class_support, overlay_auto_command,
   4000      1.1  christos 	   _("Enable automatic overlay debugging."), &overlaylist);
   4001      1.1  christos   add_cmd ("load-target", class_support, overlay_load_command,
   4002      1.1  christos 	   _("Read the overlay mapping state from the target."), &overlaylist);
   4003      1.1  christos 
   4004      1.1  christos   /* Filename extension to source language lookup table: */
   4005      1.1  christos   add_setshow_string_noescape_cmd ("extension-language", class_files,
   4006      1.1  christos 				   &ext_args, _("\
   4007      1.1  christos Set mapping between filename extension and source language."), _("\
   4008      1.1  christos Show mapping between filename extension and source language."), _("\
   4009      1.1  christos Usage: set extension-language .foo bar"),
   4010      1.1  christos 				   set_ext_lang_command,
   4011      1.1  christos 				   show_ext_args,
   4012      1.1  christos 				   &setlist, &showlist);
   4013      1.1  christos 
   4014      1.1  christos   add_info ("extensions", info_ext_lang_command,
   4015      1.1  christos 	    _("All filename extensions associated with a source language."));
   4016      1.1  christos 
   4017      1.1  christos   add_setshow_optional_filename_cmd ("debug-file-directory", class_support,
   4018      1.1  christos 				     &debug_file_directory, _("\
   4019      1.1  christos Set the directories where separate debug symbols are searched for."), _("\
   4020      1.1  christos Show the directories where separate debug symbols are searched for."), _("\
   4021  1.1.1.2  christos Separate debug symbols are first searched for in the same\n\
   4022  1.1.1.2  christos directory as the binary, then in the `" DEBUG_SUBDIRECTORY "' subdirectory,\n\
   4023  1.1.1.2  christos and lastly at the path of the directory of the binary with\n\
   4024  1.1.1.2  christos each global debug-file-directory component prepended."),
   4025  1.1.1.2  christos 				     NULL,
   4026  1.1.1.2  christos 				     show_debug_file_directory,
   4027  1.1.1.2  christos 				     &setlist, &showlist);
   4028  1.1.1.2  christos 
   4029  1.1.1.2  christos   add_setshow_enum_cmd ("symbol-loading", no_class,
   4030  1.1.1.2  christos 			print_symbol_loading_enums, &print_symbol_loading,
   4031  1.1.1.2  christos 			_("\
   4032  1.1.1.2  christos Set printing of symbol loading messages."), _("\
   4033  1.1.1.2  christos Show printing of symbol loading messages."), _("\
   4034  1.1.1.2  christos off   == turn all messages off\n\
   4035  1.1.1.6  christos brief == print messages for the executable,\n\
   4036  1.1.1.6  christos          and brief messages for shared libraries\n\
   4037  1.1.1.6  christos full  == print messages for the executable,\n\
   4038  1.1.1.6  christos          and messages for each shared library."),
   4039  1.1.1.6  christos 			NULL,
   4040  1.1.1.6  christos 			NULL,
   4041  1.1.1.6  christos 			&setprintlist, &showprintlist);
   4042  1.1.1.6  christos 
   4043  1.1.1.6  christos   add_setshow_boolean_cmd ("separate-debug-file", no_class,
   4044  1.1.1.6  christos 			   &separate_debug_file_debug, _("\
   4045  1.1.1.6  christos Set printing of separate debug info file search debug."), _("\
   4046  1.1.1.6  christos Show printing of separate debug info file search debug."), _("\
   4047  1.1.1.6  christos When on, GDB prints the searched locations while looking for separate debug \
   4048  1.1.1.6  christos info files."), NULL, NULL, &setdebuglist, &showdebuglist);
   4049  1.1.1.6  christos 
   4050      1.1  christos #if GDB_SELF_TEST
   4051                      selftests::register_test
   4052                        ("filename_language", selftests::filename_language::test_filename_language);
   4053                      selftests::register_test
   4054                        ("set_ext_lang_command",
   4055                         selftests::filename_language::test_set_ext_lang_command);
   4056                    #endif
   4057                    }
   4058