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