xref: /dflybsd-src/contrib/gdb-7/gdb/elfread.c (revision 5796c8dc12c637f18a1740c26afd8d40ffa9b719)
1*5796c8dcSSimon Schubert /* Read ELF (Executable and Linking Format) object files for GDB.
2*5796c8dcSSimon Schubert 
3*5796c8dcSSimon Schubert    Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4*5796c8dcSSimon Schubert    2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
5*5796c8dcSSimon Schubert    Free Software Foundation, Inc.
6*5796c8dcSSimon Schubert 
7*5796c8dcSSimon Schubert    Written by Fred Fish at Cygnus Support.
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 "bfd.h"
26*5796c8dcSSimon Schubert #include "gdb_string.h"
27*5796c8dcSSimon Schubert #include "elf-bfd.h"
28*5796c8dcSSimon Schubert #include "elf/common.h"
29*5796c8dcSSimon Schubert #include "elf/internal.h"
30*5796c8dcSSimon Schubert #include "elf/mips.h"
31*5796c8dcSSimon Schubert #include "symtab.h"
32*5796c8dcSSimon Schubert #include "symfile.h"
33*5796c8dcSSimon Schubert #include "objfiles.h"
34*5796c8dcSSimon Schubert #include "buildsym.h"
35*5796c8dcSSimon Schubert #include "stabsread.h"
36*5796c8dcSSimon Schubert #include "gdb-stabs.h"
37*5796c8dcSSimon Schubert #include "complaints.h"
38*5796c8dcSSimon Schubert #include "demangle.h"
39*5796c8dcSSimon Schubert 
40*5796c8dcSSimon Schubert extern void _initialize_elfread (void);
41*5796c8dcSSimon Schubert 
42*5796c8dcSSimon Schubert /* The struct elfinfo is available only during ELF symbol table and
43*5796c8dcSSimon Schubert    psymtab reading.  It is destroyed at the completion of psymtab-reading.
44*5796c8dcSSimon Schubert    It's local to elf_symfile_read.  */
45*5796c8dcSSimon Schubert 
46*5796c8dcSSimon Schubert struct elfinfo
47*5796c8dcSSimon Schubert   {
48*5796c8dcSSimon Schubert     asection *stabsect;		/* Section pointer for .stab section */
49*5796c8dcSSimon Schubert     asection *stabindexsect;	/* Section pointer for .stab.index section */
50*5796c8dcSSimon Schubert     asection *mdebugsect;	/* Section pointer for .mdebug section */
51*5796c8dcSSimon Schubert   };
52*5796c8dcSSimon Schubert 
53*5796c8dcSSimon Schubert static void free_elfinfo (void *);
54*5796c8dcSSimon Schubert 
55*5796c8dcSSimon Schubert /* Locate the segments in ABFD.  */
56*5796c8dcSSimon Schubert 
57*5796c8dcSSimon Schubert static struct symfile_segment_data *
58*5796c8dcSSimon Schubert elf_symfile_segments (bfd *abfd)
59*5796c8dcSSimon Schubert {
60*5796c8dcSSimon Schubert   Elf_Internal_Phdr *phdrs, **segments;
61*5796c8dcSSimon Schubert   long phdrs_size;
62*5796c8dcSSimon Schubert   int num_phdrs, num_segments, num_sections, i;
63*5796c8dcSSimon Schubert   asection *sect;
64*5796c8dcSSimon Schubert   struct symfile_segment_data *data;
65*5796c8dcSSimon Schubert 
66*5796c8dcSSimon Schubert   phdrs_size = bfd_get_elf_phdr_upper_bound (abfd);
67*5796c8dcSSimon Schubert   if (phdrs_size == -1)
68*5796c8dcSSimon Schubert     return NULL;
69*5796c8dcSSimon Schubert 
70*5796c8dcSSimon Schubert   phdrs = alloca (phdrs_size);
71*5796c8dcSSimon Schubert   num_phdrs = bfd_get_elf_phdrs (abfd, phdrs);
72*5796c8dcSSimon Schubert   if (num_phdrs == -1)
73*5796c8dcSSimon Schubert     return NULL;
74*5796c8dcSSimon Schubert 
75*5796c8dcSSimon Schubert   num_segments = 0;
76*5796c8dcSSimon Schubert   segments = alloca (sizeof (Elf_Internal_Phdr *) * num_phdrs);
77*5796c8dcSSimon Schubert   for (i = 0; i < num_phdrs; i++)
78*5796c8dcSSimon Schubert     if (phdrs[i].p_type == PT_LOAD)
79*5796c8dcSSimon Schubert       segments[num_segments++] = &phdrs[i];
80*5796c8dcSSimon Schubert 
81*5796c8dcSSimon Schubert   if (num_segments == 0)
82*5796c8dcSSimon Schubert     return NULL;
83*5796c8dcSSimon Schubert 
84*5796c8dcSSimon Schubert   data = XZALLOC (struct symfile_segment_data);
85*5796c8dcSSimon Schubert   data->num_segments = num_segments;
86*5796c8dcSSimon Schubert   data->segment_bases = XCALLOC (num_segments, CORE_ADDR);
87*5796c8dcSSimon Schubert   data->segment_sizes = XCALLOC (num_segments, CORE_ADDR);
88*5796c8dcSSimon Schubert 
89*5796c8dcSSimon Schubert   for (i = 0; i < num_segments; i++)
90*5796c8dcSSimon Schubert     {
91*5796c8dcSSimon Schubert       data->segment_bases[i] = segments[i]->p_vaddr;
92*5796c8dcSSimon Schubert       data->segment_sizes[i] = segments[i]->p_memsz;
93*5796c8dcSSimon Schubert     }
94*5796c8dcSSimon Schubert 
95*5796c8dcSSimon Schubert   num_sections = bfd_count_sections (abfd);
96*5796c8dcSSimon Schubert   data->segment_info = XCALLOC (num_sections, int);
97*5796c8dcSSimon Schubert 
98*5796c8dcSSimon Schubert   for (i = 0, sect = abfd->sections; sect != NULL; i++, sect = sect->next)
99*5796c8dcSSimon Schubert     {
100*5796c8dcSSimon Schubert       int j;
101*5796c8dcSSimon Schubert       CORE_ADDR vma;
102*5796c8dcSSimon Schubert 
103*5796c8dcSSimon Schubert       if ((bfd_get_section_flags (abfd, sect) & SEC_ALLOC) == 0)
104*5796c8dcSSimon Schubert 	continue;
105*5796c8dcSSimon Schubert 
106*5796c8dcSSimon Schubert       vma = bfd_get_section_vma (abfd, sect);
107*5796c8dcSSimon Schubert 
108*5796c8dcSSimon Schubert       for (j = 0; j < num_segments; j++)
109*5796c8dcSSimon Schubert 	if (segments[j]->p_memsz > 0
110*5796c8dcSSimon Schubert 	    && vma >= segments[j]->p_vaddr
111*5796c8dcSSimon Schubert 	    && (vma - segments[j]->p_vaddr) < segments[j]->p_memsz)
112*5796c8dcSSimon Schubert 	  {
113*5796c8dcSSimon Schubert 	    data->segment_info[i] = j + 1;
114*5796c8dcSSimon Schubert 	    break;
115*5796c8dcSSimon Schubert 	  }
116*5796c8dcSSimon Schubert 
117*5796c8dcSSimon Schubert       if (bfd_get_section_size (sect) > 0 && j == num_segments)
118*5796c8dcSSimon Schubert 	warning (_("Loadable segment \"%s\" outside of ELF segments"),
119*5796c8dcSSimon Schubert 		 bfd_section_name (abfd, sect));
120*5796c8dcSSimon Schubert     }
121*5796c8dcSSimon Schubert 
122*5796c8dcSSimon Schubert   return data;
123*5796c8dcSSimon Schubert }
124*5796c8dcSSimon Schubert 
125*5796c8dcSSimon Schubert /* We are called once per section from elf_symfile_read.  We
126*5796c8dcSSimon Schubert    need to examine each section we are passed, check to see
127*5796c8dcSSimon Schubert    if it is something we are interested in processing, and
128*5796c8dcSSimon Schubert    if so, stash away some access information for the section.
129*5796c8dcSSimon Schubert 
130*5796c8dcSSimon Schubert    For now we recognize the dwarf debug information sections and
131*5796c8dcSSimon Schubert    line number sections from matching their section names.  The
132*5796c8dcSSimon Schubert    ELF definition is no real help here since it has no direct
133*5796c8dcSSimon Schubert    knowledge of DWARF (by design, so any debugging format can be
134*5796c8dcSSimon Schubert    used).
135*5796c8dcSSimon Schubert 
136*5796c8dcSSimon Schubert    We also recognize the ".stab" sections used by the Sun compilers
137*5796c8dcSSimon Schubert    released with Solaris 2.
138*5796c8dcSSimon Schubert 
139*5796c8dcSSimon Schubert    FIXME: The section names should not be hardwired strings (what
140*5796c8dcSSimon Schubert    should they be?  I don't think most object file formats have enough
141*5796c8dcSSimon Schubert    section flags to specify what kind of debug section it is
142*5796c8dcSSimon Schubert    -kingdon).  */
143*5796c8dcSSimon Schubert 
144*5796c8dcSSimon Schubert static void
145*5796c8dcSSimon Schubert elf_locate_sections (bfd *ignore_abfd, asection *sectp, void *eip)
146*5796c8dcSSimon Schubert {
147*5796c8dcSSimon Schubert   struct elfinfo *ei;
148*5796c8dcSSimon Schubert 
149*5796c8dcSSimon Schubert   ei = (struct elfinfo *) eip;
150*5796c8dcSSimon Schubert   if (strcmp (sectp->name, ".stab") == 0)
151*5796c8dcSSimon Schubert     {
152*5796c8dcSSimon Schubert       ei->stabsect = sectp;
153*5796c8dcSSimon Schubert     }
154*5796c8dcSSimon Schubert   else if (strcmp (sectp->name, ".stab.index") == 0)
155*5796c8dcSSimon Schubert     {
156*5796c8dcSSimon Schubert       ei->stabindexsect = sectp;
157*5796c8dcSSimon Schubert     }
158*5796c8dcSSimon Schubert   else if (strcmp (sectp->name, ".mdebug") == 0)
159*5796c8dcSSimon Schubert     {
160*5796c8dcSSimon Schubert       ei->mdebugsect = sectp;
161*5796c8dcSSimon Schubert     }
162*5796c8dcSSimon Schubert }
163*5796c8dcSSimon Schubert 
164*5796c8dcSSimon Schubert static struct minimal_symbol *
165*5796c8dcSSimon Schubert record_minimal_symbol (char *name, CORE_ADDR address,
166*5796c8dcSSimon Schubert 		       enum minimal_symbol_type ms_type,
167*5796c8dcSSimon Schubert 		       asection *bfd_section, struct objfile *objfile)
168*5796c8dcSSimon Schubert {
169*5796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_objfile_arch (objfile);
170*5796c8dcSSimon Schubert 
171*5796c8dcSSimon Schubert   if (ms_type == mst_text || ms_type == mst_file_text)
172*5796c8dcSSimon Schubert     address = gdbarch_smash_text_address (gdbarch, address);
173*5796c8dcSSimon Schubert 
174*5796c8dcSSimon Schubert   return prim_record_minimal_symbol_and_info
175*5796c8dcSSimon Schubert     (name, address, ms_type, bfd_section->index, bfd_section, objfile);
176*5796c8dcSSimon Schubert }
177*5796c8dcSSimon Schubert 
178*5796c8dcSSimon Schubert /*
179*5796c8dcSSimon Schubert 
180*5796c8dcSSimon Schubert    LOCAL FUNCTION
181*5796c8dcSSimon Schubert 
182*5796c8dcSSimon Schubert    elf_symtab_read -- read the symbol table of an ELF file
183*5796c8dcSSimon Schubert 
184*5796c8dcSSimon Schubert    SYNOPSIS
185*5796c8dcSSimon Schubert 
186*5796c8dcSSimon Schubert    void elf_symtab_read (struct objfile *objfile, int type,
187*5796c8dcSSimon Schubert 			 long number_of_symbols, asymbol **symbol_table)
188*5796c8dcSSimon Schubert 
189*5796c8dcSSimon Schubert    DESCRIPTION
190*5796c8dcSSimon Schubert 
191*5796c8dcSSimon Schubert    Given an objfile, a symbol table, and a flag indicating whether the
192*5796c8dcSSimon Schubert    symbol table contains regular, dynamic, or synthetic symbols, add all
193*5796c8dcSSimon Schubert    the global function and data symbols to the minimal symbol table.
194*5796c8dcSSimon Schubert 
195*5796c8dcSSimon Schubert    In stabs-in-ELF, as implemented by Sun, there are some local symbols
196*5796c8dcSSimon Schubert    defined in the ELF symbol table, which can be used to locate
197*5796c8dcSSimon Schubert    the beginnings of sections from each ".o" file that was linked to
198*5796c8dcSSimon Schubert    form the executable objfile.  We gather any such info and record it
199*5796c8dcSSimon Schubert    in data structures hung off the objfile's private data.
200*5796c8dcSSimon Schubert 
201*5796c8dcSSimon Schubert  */
202*5796c8dcSSimon Schubert 
203*5796c8dcSSimon Schubert #define ST_REGULAR 0
204*5796c8dcSSimon Schubert #define ST_DYNAMIC 1
205*5796c8dcSSimon Schubert #define ST_SYNTHETIC 2
206*5796c8dcSSimon Schubert 
207*5796c8dcSSimon Schubert static void
208*5796c8dcSSimon Schubert elf_symtab_read (struct objfile *objfile, int type,
209*5796c8dcSSimon Schubert 		 long number_of_symbols, asymbol **symbol_table)
210*5796c8dcSSimon Schubert {
211*5796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_objfile_arch (objfile);
212*5796c8dcSSimon Schubert   long storage_needed;
213*5796c8dcSSimon Schubert   asymbol *sym;
214*5796c8dcSSimon Schubert   long i;
215*5796c8dcSSimon Schubert   CORE_ADDR symaddr;
216*5796c8dcSSimon Schubert   CORE_ADDR offset;
217*5796c8dcSSimon Schubert   enum minimal_symbol_type ms_type;
218*5796c8dcSSimon Schubert   /* If sectinfo is nonNULL, it contains section info that should end up
219*5796c8dcSSimon Schubert      filed in the objfile.  */
220*5796c8dcSSimon Schubert   struct stab_section_info *sectinfo = NULL;
221*5796c8dcSSimon Schubert   /* If filesym is nonzero, it points to a file symbol, but we haven't
222*5796c8dcSSimon Schubert      seen any section info for it yet.  */
223*5796c8dcSSimon Schubert   asymbol *filesym = 0;
224*5796c8dcSSimon Schubert   /* Name of filesym, as saved on the objfile_obstack.  */
225*5796c8dcSSimon Schubert   char *filesymname = obsavestring ("", 0, &objfile->objfile_obstack);
226*5796c8dcSSimon Schubert   struct dbx_symfile_info *dbx = objfile->deprecated_sym_stab_info;
227*5796c8dcSSimon Schubert   int stripped = (bfd_get_symcount (objfile->obfd) == 0);
228*5796c8dcSSimon Schubert 
229*5796c8dcSSimon Schubert   for (i = 0; i < number_of_symbols; i++)
230*5796c8dcSSimon Schubert     {
231*5796c8dcSSimon Schubert       sym = symbol_table[i];
232*5796c8dcSSimon Schubert       if (sym->name == NULL || *sym->name == '\0')
233*5796c8dcSSimon Schubert 	{
234*5796c8dcSSimon Schubert 	  /* Skip names that don't exist (shouldn't happen), or names
235*5796c8dcSSimon Schubert 	     that are null strings (may happen). */
236*5796c8dcSSimon Schubert 	  continue;
237*5796c8dcSSimon Schubert 	}
238*5796c8dcSSimon Schubert 
239*5796c8dcSSimon Schubert       /* Skip "special" symbols, e.g. ARM mapping symbols.  These are
240*5796c8dcSSimon Schubert 	 symbols which do not correspond to objects in the symbol table,
241*5796c8dcSSimon Schubert 	 but have some other target-specific meaning.  */
242*5796c8dcSSimon Schubert       if (bfd_is_target_special_symbol (objfile->obfd, sym))
243*5796c8dcSSimon Schubert 	{
244*5796c8dcSSimon Schubert 	  if (gdbarch_record_special_symbol_p (gdbarch))
245*5796c8dcSSimon Schubert 	    gdbarch_record_special_symbol (gdbarch, objfile, sym);
246*5796c8dcSSimon Schubert 	  continue;
247*5796c8dcSSimon Schubert 	}
248*5796c8dcSSimon Schubert 
249*5796c8dcSSimon Schubert       offset = ANOFFSET (objfile->section_offsets, sym->section->index);
250*5796c8dcSSimon Schubert       if (type == ST_DYNAMIC
251*5796c8dcSSimon Schubert 	  && sym->section == &bfd_und_section
252*5796c8dcSSimon Schubert 	  && (sym->flags & BSF_FUNCTION))
253*5796c8dcSSimon Schubert 	{
254*5796c8dcSSimon Schubert 	  struct minimal_symbol *msym;
255*5796c8dcSSimon Schubert 	  bfd *abfd = objfile->obfd;
256*5796c8dcSSimon Schubert 	  asection *sect;
257*5796c8dcSSimon Schubert 
258*5796c8dcSSimon Schubert 	  /* Symbol is a reference to a function defined in
259*5796c8dcSSimon Schubert 	     a shared library.
260*5796c8dcSSimon Schubert 	     If its value is non zero then it is usually the address
261*5796c8dcSSimon Schubert 	     of the corresponding entry in the procedure linkage table,
262*5796c8dcSSimon Schubert 	     plus the desired section offset.
263*5796c8dcSSimon Schubert 	     If its value is zero then the dynamic linker has to resolve
264*5796c8dcSSimon Schubert 	     the symbol. We are unable to find any meaningful address
265*5796c8dcSSimon Schubert 	     for this symbol in the executable file, so we skip it.  */
266*5796c8dcSSimon Schubert 	  symaddr = sym->value;
267*5796c8dcSSimon Schubert 	  if (symaddr == 0)
268*5796c8dcSSimon Schubert 	    continue;
269*5796c8dcSSimon Schubert 
270*5796c8dcSSimon Schubert 	  /* sym->section is the undefined section.  However, we want to
271*5796c8dcSSimon Schubert 	     record the section where the PLT stub resides with the
272*5796c8dcSSimon Schubert 	     minimal symbol.  Search the section table for the one that
273*5796c8dcSSimon Schubert 	     covers the stub's address.  */
274*5796c8dcSSimon Schubert 	  for (sect = abfd->sections; sect != NULL; sect = sect->next)
275*5796c8dcSSimon Schubert 	    {
276*5796c8dcSSimon Schubert 	      if ((bfd_get_section_flags (abfd, sect) & SEC_ALLOC) == 0)
277*5796c8dcSSimon Schubert 		continue;
278*5796c8dcSSimon Schubert 
279*5796c8dcSSimon Schubert 	      if (symaddr >= bfd_get_section_vma (abfd, sect)
280*5796c8dcSSimon Schubert 		  && symaddr < bfd_get_section_vma (abfd, sect)
281*5796c8dcSSimon Schubert 			       + bfd_get_section_size (sect))
282*5796c8dcSSimon Schubert 		break;
283*5796c8dcSSimon Schubert 	    }
284*5796c8dcSSimon Schubert 	  if (!sect)
285*5796c8dcSSimon Schubert 	    continue;
286*5796c8dcSSimon Schubert 
287*5796c8dcSSimon Schubert 	  symaddr += ANOFFSET (objfile->section_offsets, sect->index);
288*5796c8dcSSimon Schubert 
289*5796c8dcSSimon Schubert 	  msym = record_minimal_symbol
290*5796c8dcSSimon Schubert 	    ((char *) sym->name, symaddr, mst_solib_trampoline, sect, objfile);
291*5796c8dcSSimon Schubert 	  if (msym != NULL)
292*5796c8dcSSimon Schubert 	    msym->filename = filesymname;
293*5796c8dcSSimon Schubert 	  continue;
294*5796c8dcSSimon Schubert 	}
295*5796c8dcSSimon Schubert 
296*5796c8dcSSimon Schubert       /* If it is a nonstripped executable, do not enter dynamic
297*5796c8dcSSimon Schubert 	 symbols, as the dynamic symbol table is usually a subset
298*5796c8dcSSimon Schubert 	 of the main symbol table.  */
299*5796c8dcSSimon Schubert       if (type == ST_DYNAMIC && !stripped)
300*5796c8dcSSimon Schubert 	continue;
301*5796c8dcSSimon Schubert       if (sym->flags & BSF_FILE)
302*5796c8dcSSimon Schubert 	{
303*5796c8dcSSimon Schubert 	  /* STT_FILE debugging symbol that helps stabs-in-elf debugging.
304*5796c8dcSSimon Schubert 	     Chain any old one onto the objfile; remember new sym.  */
305*5796c8dcSSimon Schubert 	  if (sectinfo != NULL)
306*5796c8dcSSimon Schubert 	    {
307*5796c8dcSSimon Schubert 	      sectinfo->next = dbx->stab_section_info;
308*5796c8dcSSimon Schubert 	      dbx->stab_section_info = sectinfo;
309*5796c8dcSSimon Schubert 	      sectinfo = NULL;
310*5796c8dcSSimon Schubert 	    }
311*5796c8dcSSimon Schubert 	  filesym = sym;
312*5796c8dcSSimon Schubert 	  filesymname =
313*5796c8dcSSimon Schubert 	    obsavestring ((char *) filesym->name, strlen (filesym->name),
314*5796c8dcSSimon Schubert 			  &objfile->objfile_obstack);
315*5796c8dcSSimon Schubert 	}
316*5796c8dcSSimon Schubert       else if (sym->flags & BSF_SECTION_SYM)
317*5796c8dcSSimon Schubert 	continue;
318*5796c8dcSSimon Schubert       else if (sym->flags & (BSF_GLOBAL | BSF_LOCAL | BSF_WEAK))
319*5796c8dcSSimon Schubert 	{
320*5796c8dcSSimon Schubert 	  struct minimal_symbol *msym;
321*5796c8dcSSimon Schubert 
322*5796c8dcSSimon Schubert 	  /* Select global/local/weak symbols.  Note that bfd puts abs
323*5796c8dcSSimon Schubert 	     symbols in their own section, so all symbols we are
324*5796c8dcSSimon Schubert 	     interested in will have a section. */
325*5796c8dcSSimon Schubert 	  /* Bfd symbols are section relative. */
326*5796c8dcSSimon Schubert 	  symaddr = sym->value + sym->section->vma;
327*5796c8dcSSimon Schubert 	  /* Relocate all non-absolute and non-TLS symbols by the
328*5796c8dcSSimon Schubert 	     section offset.  */
329*5796c8dcSSimon Schubert 	  if (sym->section != &bfd_abs_section
330*5796c8dcSSimon Schubert 	      && !(sym->section->flags & SEC_THREAD_LOCAL))
331*5796c8dcSSimon Schubert 	    {
332*5796c8dcSSimon Schubert 	      symaddr += offset;
333*5796c8dcSSimon Schubert 	    }
334*5796c8dcSSimon Schubert 	  /* For non-absolute symbols, use the type of the section
335*5796c8dcSSimon Schubert 	     they are relative to, to intuit text/data.  Bfd provides
336*5796c8dcSSimon Schubert 	     no way of figuring this out for absolute symbols. */
337*5796c8dcSSimon Schubert 	  if (sym->section == &bfd_abs_section)
338*5796c8dcSSimon Schubert 	    {
339*5796c8dcSSimon Schubert 	      /* This is a hack to get the minimal symbol type
340*5796c8dcSSimon Schubert 		 right for Irix 5, which has absolute addresses
341*5796c8dcSSimon Schubert 		 with special section indices for dynamic symbols.
342*5796c8dcSSimon Schubert 
343*5796c8dcSSimon Schubert 		 NOTE: uweigand-20071112: Synthetic symbols do not
344*5796c8dcSSimon Schubert 		 have an ELF-private part, so do not touch those.  */
345*5796c8dcSSimon Schubert 	      unsigned int shndx = type == ST_SYNTHETIC ? 0 :
346*5796c8dcSSimon Schubert 		((elf_symbol_type *) sym)->internal_elf_sym.st_shndx;
347*5796c8dcSSimon Schubert 
348*5796c8dcSSimon Schubert 	      switch (shndx)
349*5796c8dcSSimon Schubert 		{
350*5796c8dcSSimon Schubert 		case SHN_MIPS_TEXT:
351*5796c8dcSSimon Schubert 		  ms_type = mst_text;
352*5796c8dcSSimon Schubert 		  break;
353*5796c8dcSSimon Schubert 		case SHN_MIPS_DATA:
354*5796c8dcSSimon Schubert 		  ms_type = mst_data;
355*5796c8dcSSimon Schubert 		  break;
356*5796c8dcSSimon Schubert 		case SHN_MIPS_ACOMMON:
357*5796c8dcSSimon Schubert 		  ms_type = mst_bss;
358*5796c8dcSSimon Schubert 		  break;
359*5796c8dcSSimon Schubert 		default:
360*5796c8dcSSimon Schubert 		  ms_type = mst_abs;
361*5796c8dcSSimon Schubert 		}
362*5796c8dcSSimon Schubert 
363*5796c8dcSSimon Schubert 	      /* If it is an Irix dynamic symbol, skip section name
364*5796c8dcSSimon Schubert 		 symbols, relocate all others by section offset. */
365*5796c8dcSSimon Schubert 	      if (ms_type != mst_abs)
366*5796c8dcSSimon Schubert 		{
367*5796c8dcSSimon Schubert 		  if (sym->name[0] == '.')
368*5796c8dcSSimon Schubert 		    continue;
369*5796c8dcSSimon Schubert 		  symaddr += offset;
370*5796c8dcSSimon Schubert 		}
371*5796c8dcSSimon Schubert 	    }
372*5796c8dcSSimon Schubert 	  else if (sym->section->flags & SEC_CODE)
373*5796c8dcSSimon Schubert 	    {
374*5796c8dcSSimon Schubert 	      if (sym->flags & (BSF_GLOBAL | BSF_WEAK))
375*5796c8dcSSimon Schubert 		{
376*5796c8dcSSimon Schubert 		  ms_type = mst_text;
377*5796c8dcSSimon Schubert 		}
378*5796c8dcSSimon Schubert 	      else if ((sym->name[0] == '.' && sym->name[1] == 'L')
379*5796c8dcSSimon Schubert 		       || ((sym->flags & BSF_LOCAL)
380*5796c8dcSSimon Schubert 			   && sym->name[0] == '$'
381*5796c8dcSSimon Schubert 			   && sym->name[1] == 'L'))
382*5796c8dcSSimon Schubert 		/* Looks like a compiler-generated label.  Skip
383*5796c8dcSSimon Schubert 		   it.  The assembler should be skipping these (to
384*5796c8dcSSimon Schubert 		   keep executables small), but apparently with
385*5796c8dcSSimon Schubert 		   gcc on the (deleted) delta m88k SVR4, it loses.
386*5796c8dcSSimon Schubert 		   So to have us check too should be harmless (but
387*5796c8dcSSimon Schubert 		   I encourage people to fix this in the assembler
388*5796c8dcSSimon Schubert 		   instead of adding checks here).  */
389*5796c8dcSSimon Schubert 		continue;
390*5796c8dcSSimon Schubert 	      else
391*5796c8dcSSimon Schubert 		{
392*5796c8dcSSimon Schubert 		  ms_type = mst_file_text;
393*5796c8dcSSimon Schubert 		}
394*5796c8dcSSimon Schubert 	    }
395*5796c8dcSSimon Schubert 	  else if (sym->section->flags & SEC_ALLOC)
396*5796c8dcSSimon Schubert 	    {
397*5796c8dcSSimon Schubert 	      if (sym->flags & (BSF_GLOBAL | BSF_WEAK))
398*5796c8dcSSimon Schubert 		{
399*5796c8dcSSimon Schubert 		  if (sym->section->flags & SEC_LOAD)
400*5796c8dcSSimon Schubert 		    {
401*5796c8dcSSimon Schubert 		      ms_type = mst_data;
402*5796c8dcSSimon Schubert 		    }
403*5796c8dcSSimon Schubert 		  else
404*5796c8dcSSimon Schubert 		    {
405*5796c8dcSSimon Schubert 		      ms_type = mst_bss;
406*5796c8dcSSimon Schubert 		    }
407*5796c8dcSSimon Schubert 		}
408*5796c8dcSSimon Schubert 	      else if (sym->flags & BSF_LOCAL)
409*5796c8dcSSimon Schubert 		{
410*5796c8dcSSimon Schubert 		  /* Named Local variable in a Data section.
411*5796c8dcSSimon Schubert 		     Check its name for stabs-in-elf.  */
412*5796c8dcSSimon Schubert 		  int special_local_sect;
413*5796c8dcSSimon Schubert 		  if (strcmp ("Bbss.bss", sym->name) == 0)
414*5796c8dcSSimon Schubert 		    special_local_sect = SECT_OFF_BSS (objfile);
415*5796c8dcSSimon Schubert 		  else if (strcmp ("Ddata.data", sym->name) == 0)
416*5796c8dcSSimon Schubert 		    special_local_sect = SECT_OFF_DATA (objfile);
417*5796c8dcSSimon Schubert 		  else if (strcmp ("Drodata.rodata", sym->name) == 0)
418*5796c8dcSSimon Schubert 		    special_local_sect = SECT_OFF_RODATA (objfile);
419*5796c8dcSSimon Schubert 		  else
420*5796c8dcSSimon Schubert 		    special_local_sect = -1;
421*5796c8dcSSimon Schubert 		  if (special_local_sect >= 0)
422*5796c8dcSSimon Schubert 		    {
423*5796c8dcSSimon Schubert 		      /* Found a special local symbol.  Allocate a
424*5796c8dcSSimon Schubert 			 sectinfo, if needed, and fill it in.  */
425*5796c8dcSSimon Schubert 		      if (sectinfo == NULL)
426*5796c8dcSSimon Schubert 			{
427*5796c8dcSSimon Schubert 			  int max_index;
428*5796c8dcSSimon Schubert 			  size_t size;
429*5796c8dcSSimon Schubert 
430*5796c8dcSSimon Schubert 			  max_index = SECT_OFF_BSS (objfile);
431*5796c8dcSSimon Schubert 			  if (objfile->sect_index_data > max_index)
432*5796c8dcSSimon Schubert 			    max_index = objfile->sect_index_data;
433*5796c8dcSSimon Schubert 			  if (objfile->sect_index_rodata > max_index)
434*5796c8dcSSimon Schubert 			    max_index = objfile->sect_index_rodata;
435*5796c8dcSSimon Schubert 
436*5796c8dcSSimon Schubert 			  /* max_index is the largest index we'll
437*5796c8dcSSimon Schubert 			     use into this array, so we must
438*5796c8dcSSimon Schubert 			     allocate max_index+1 elements for it.
439*5796c8dcSSimon Schubert 			     However, 'struct stab_section_info'
440*5796c8dcSSimon Schubert 			     already includes one element, so we
441*5796c8dcSSimon Schubert 			     need to allocate max_index aadditional
442*5796c8dcSSimon Schubert 			     elements.  */
443*5796c8dcSSimon Schubert 			  size = (sizeof (struct stab_section_info)
444*5796c8dcSSimon Schubert 				  + (sizeof (CORE_ADDR)
445*5796c8dcSSimon Schubert 				     * max_index));
446*5796c8dcSSimon Schubert 			  sectinfo = (struct stab_section_info *)
447*5796c8dcSSimon Schubert 			    xmalloc (size);
448*5796c8dcSSimon Schubert 			  memset (sectinfo, 0, size);
449*5796c8dcSSimon Schubert 			  sectinfo->num_sections = max_index;
450*5796c8dcSSimon Schubert 			  if (filesym == NULL)
451*5796c8dcSSimon Schubert 			    {
452*5796c8dcSSimon Schubert 			      complaint (&symfile_complaints,
453*5796c8dcSSimon Schubert 					 _("elf/stab section information %s without a preceding file symbol"),
454*5796c8dcSSimon Schubert 					 sym->name);
455*5796c8dcSSimon Schubert 			    }
456*5796c8dcSSimon Schubert 			  else
457*5796c8dcSSimon Schubert 			    {
458*5796c8dcSSimon Schubert 			      sectinfo->filename =
459*5796c8dcSSimon Schubert 				(char *) filesym->name;
460*5796c8dcSSimon Schubert 			    }
461*5796c8dcSSimon Schubert 			}
462*5796c8dcSSimon Schubert 		      if (sectinfo->sections[special_local_sect] != 0)
463*5796c8dcSSimon Schubert 			complaint (&symfile_complaints,
464*5796c8dcSSimon Schubert 				   _("duplicated elf/stab section information for %s"),
465*5796c8dcSSimon Schubert 				   sectinfo->filename);
466*5796c8dcSSimon Schubert 		      /* BFD symbols are section relative.  */
467*5796c8dcSSimon Schubert 		      symaddr = sym->value + sym->section->vma;
468*5796c8dcSSimon Schubert 		      /* Relocate non-absolute symbols by the
469*5796c8dcSSimon Schubert 			 section offset.  */
470*5796c8dcSSimon Schubert 		      if (sym->section != &bfd_abs_section)
471*5796c8dcSSimon Schubert 			symaddr += offset;
472*5796c8dcSSimon Schubert 		      sectinfo->sections[special_local_sect] = symaddr;
473*5796c8dcSSimon Schubert 		      /* The special local symbols don't go in the
474*5796c8dcSSimon Schubert 			 minimal symbol table, so ignore this one.  */
475*5796c8dcSSimon Schubert 		      continue;
476*5796c8dcSSimon Schubert 		    }
477*5796c8dcSSimon Schubert 		  /* Not a special stabs-in-elf symbol, do regular
478*5796c8dcSSimon Schubert 		     symbol processing.  */
479*5796c8dcSSimon Schubert 		  if (sym->section->flags & SEC_LOAD)
480*5796c8dcSSimon Schubert 		    {
481*5796c8dcSSimon Schubert 		      ms_type = mst_file_data;
482*5796c8dcSSimon Schubert 		    }
483*5796c8dcSSimon Schubert 		  else
484*5796c8dcSSimon Schubert 		    {
485*5796c8dcSSimon Schubert 		      ms_type = mst_file_bss;
486*5796c8dcSSimon Schubert 		    }
487*5796c8dcSSimon Schubert 		}
488*5796c8dcSSimon Schubert 	      else
489*5796c8dcSSimon Schubert 		{
490*5796c8dcSSimon Schubert 		  ms_type = mst_unknown;
491*5796c8dcSSimon Schubert 		}
492*5796c8dcSSimon Schubert 	    }
493*5796c8dcSSimon Schubert 	  else
494*5796c8dcSSimon Schubert 	    {
495*5796c8dcSSimon Schubert 	      /* FIXME:  Solaris2 shared libraries include lots of
496*5796c8dcSSimon Schubert 		 odd "absolute" and "undefined" symbols, that play
497*5796c8dcSSimon Schubert 		 hob with actions like finding what function the PC
498*5796c8dcSSimon Schubert 		 is in.  Ignore them if they aren't text, data, or bss.  */
499*5796c8dcSSimon Schubert 	      /* ms_type = mst_unknown; */
500*5796c8dcSSimon Schubert 	      continue;	/* Skip this symbol. */
501*5796c8dcSSimon Schubert 	    }
502*5796c8dcSSimon Schubert 	  msym = record_minimal_symbol
503*5796c8dcSSimon Schubert 	    ((char *) sym->name, symaddr,
504*5796c8dcSSimon Schubert 	     ms_type, sym->section, objfile);
505*5796c8dcSSimon Schubert 
506*5796c8dcSSimon Schubert 	  if (msym)
507*5796c8dcSSimon Schubert 	    {
508*5796c8dcSSimon Schubert 	      /* Pass symbol size field in via BFD.  FIXME!!!  */
509*5796c8dcSSimon Schubert 	      elf_symbol_type *elf_sym;
510*5796c8dcSSimon Schubert 
511*5796c8dcSSimon Schubert 	      /* NOTE: uweigand-20071112: A synthetic symbol does not have an
512*5796c8dcSSimon Schubert 		 ELF-private part.  However, in some cases (e.g. synthetic
513*5796c8dcSSimon Schubert 		 'dot' symbols on ppc64) the udata.p entry is set to point back
514*5796c8dcSSimon Schubert 		 to the original ELF symbol it was derived from.  Get the size
515*5796c8dcSSimon Schubert 		 from that symbol.  */
516*5796c8dcSSimon Schubert 	      if (type != ST_SYNTHETIC)
517*5796c8dcSSimon Schubert 		elf_sym = (elf_symbol_type *) sym;
518*5796c8dcSSimon Schubert 	      else
519*5796c8dcSSimon Schubert 		elf_sym = (elf_symbol_type *) sym->udata.p;
520*5796c8dcSSimon Schubert 
521*5796c8dcSSimon Schubert 	      if (elf_sym)
522*5796c8dcSSimon Schubert 		MSYMBOL_SIZE(msym) = elf_sym->internal_elf_sym.st_size;
523*5796c8dcSSimon Schubert 	    }
524*5796c8dcSSimon Schubert 	  if (msym != NULL)
525*5796c8dcSSimon Schubert 	    msym->filename = filesymname;
526*5796c8dcSSimon Schubert 	  gdbarch_elf_make_msymbol_special (gdbarch, sym, msym);
527*5796c8dcSSimon Schubert 
528*5796c8dcSSimon Schubert 	  /* For @plt symbols, also record a trampoline to the
529*5796c8dcSSimon Schubert 	     destination symbol.  The @plt symbol will be used in
530*5796c8dcSSimon Schubert 	     disassembly, and the trampoline will be used when we are
531*5796c8dcSSimon Schubert 	     trying to find the target.  */
532*5796c8dcSSimon Schubert 	  if (msym && ms_type == mst_text && type == ST_SYNTHETIC)
533*5796c8dcSSimon Schubert 	    {
534*5796c8dcSSimon Schubert 	      int len = strlen (sym->name);
535*5796c8dcSSimon Schubert 
536*5796c8dcSSimon Schubert 	      if (len > 4 && strcmp (sym->name + len - 4, "@plt") == 0)
537*5796c8dcSSimon Schubert 		{
538*5796c8dcSSimon Schubert 		  char *base_name = alloca (len - 4 + 1);
539*5796c8dcSSimon Schubert 		  struct minimal_symbol *mtramp;
540*5796c8dcSSimon Schubert 
541*5796c8dcSSimon Schubert 		  memcpy (base_name, sym->name, len - 4);
542*5796c8dcSSimon Schubert 		  base_name[len - 4] = '\0';
543*5796c8dcSSimon Schubert 		  mtramp = record_minimal_symbol (base_name, symaddr,
544*5796c8dcSSimon Schubert 						  mst_solib_trampoline,
545*5796c8dcSSimon Schubert 						  sym->section, objfile);
546*5796c8dcSSimon Schubert 		  if (mtramp)
547*5796c8dcSSimon Schubert 		    {
548*5796c8dcSSimon Schubert 		      MSYMBOL_SIZE (mtramp) = MSYMBOL_SIZE (msym);
549*5796c8dcSSimon Schubert 		      mtramp->filename = filesymname;
550*5796c8dcSSimon Schubert 		      gdbarch_elf_make_msymbol_special (gdbarch, sym, mtramp);
551*5796c8dcSSimon Schubert 		    }
552*5796c8dcSSimon Schubert 		}
553*5796c8dcSSimon Schubert 	    }
554*5796c8dcSSimon Schubert 	}
555*5796c8dcSSimon Schubert     }
556*5796c8dcSSimon Schubert }
557*5796c8dcSSimon Schubert 
558*5796c8dcSSimon Schubert /* Scan and build partial symbols for a symbol file.
559*5796c8dcSSimon Schubert    We have been initialized by a call to elf_symfile_init, which
560*5796c8dcSSimon Schubert    currently does nothing.
561*5796c8dcSSimon Schubert 
562*5796c8dcSSimon Schubert    SECTION_OFFSETS is a set of offsets to apply to relocate the symbols
563*5796c8dcSSimon Schubert    in each section.  We simplify it down to a single offset for all
564*5796c8dcSSimon Schubert    symbols.  FIXME.
565*5796c8dcSSimon Schubert 
566*5796c8dcSSimon Schubert    MAINLINE is true if we are reading the main symbol
567*5796c8dcSSimon Schubert    table (as opposed to a shared lib or dynamically loaded file).
568*5796c8dcSSimon Schubert 
569*5796c8dcSSimon Schubert    This function only does the minimum work necessary for letting the
570*5796c8dcSSimon Schubert    user "name" things symbolically; it does not read the entire symtab.
571*5796c8dcSSimon Schubert    Instead, it reads the external and static symbols and puts them in partial
572*5796c8dcSSimon Schubert    symbol tables.  When more extensive information is requested of a
573*5796c8dcSSimon Schubert    file, the corresponding partial symbol table is mutated into a full
574*5796c8dcSSimon Schubert    fledged symbol table by going back and reading the symbols
575*5796c8dcSSimon Schubert    for real.
576*5796c8dcSSimon Schubert 
577*5796c8dcSSimon Schubert    We look for sections with specific names, to tell us what debug
578*5796c8dcSSimon Schubert    format to look for:  FIXME!!!
579*5796c8dcSSimon Schubert 
580*5796c8dcSSimon Schubert    elfstab_build_psymtabs() handles STABS symbols;
581*5796c8dcSSimon Schubert    mdebug_build_psymtabs() handles ECOFF debugging information.
582*5796c8dcSSimon Schubert 
583*5796c8dcSSimon Schubert    Note that ELF files have a "minimal" symbol table, which looks a lot
584*5796c8dcSSimon Schubert    like a COFF symbol table, but has only the minimal information necessary
585*5796c8dcSSimon Schubert    for linking.  We process this also, and use the information to
586*5796c8dcSSimon Schubert    build gdb's minimal symbol table.  This gives us some minimal debugging
587*5796c8dcSSimon Schubert    capability even for files compiled without -g.  */
588*5796c8dcSSimon Schubert 
589*5796c8dcSSimon Schubert static void
590*5796c8dcSSimon Schubert elf_symfile_read (struct objfile *objfile, int mainline)
591*5796c8dcSSimon Schubert {
592*5796c8dcSSimon Schubert   bfd *abfd = objfile->obfd;
593*5796c8dcSSimon Schubert   struct elfinfo ei;
594*5796c8dcSSimon Schubert   struct cleanup *back_to;
595*5796c8dcSSimon Schubert   CORE_ADDR offset;
596*5796c8dcSSimon Schubert   long symcount = 0, dynsymcount = 0, synthcount, storage_needed;
597*5796c8dcSSimon Schubert   asymbol **symbol_table = NULL, **dyn_symbol_table = NULL;
598*5796c8dcSSimon Schubert   asymbol *synthsyms;
599*5796c8dcSSimon Schubert 
600*5796c8dcSSimon Schubert   init_minimal_symbol_collection ();
601*5796c8dcSSimon Schubert   back_to = make_cleanup_discard_minimal_symbols ();
602*5796c8dcSSimon Schubert 
603*5796c8dcSSimon Schubert   memset ((char *) &ei, 0, sizeof (ei));
604*5796c8dcSSimon Schubert 
605*5796c8dcSSimon Schubert   /* Allocate struct to keep track of the symfile */
606*5796c8dcSSimon Schubert   objfile->deprecated_sym_stab_info = (struct dbx_symfile_info *)
607*5796c8dcSSimon Schubert     xmalloc (sizeof (struct dbx_symfile_info));
608*5796c8dcSSimon Schubert   memset ((char *) objfile->deprecated_sym_stab_info, 0, sizeof (struct dbx_symfile_info));
609*5796c8dcSSimon Schubert   make_cleanup (free_elfinfo, (void *) objfile);
610*5796c8dcSSimon Schubert 
611*5796c8dcSSimon Schubert   /* Process the normal ELF symbol table first.  This may write some
612*5796c8dcSSimon Schubert      chain of info into the dbx_symfile_info in objfile->deprecated_sym_stab_info,
613*5796c8dcSSimon Schubert      which can later be used by elfstab_offset_sections.  */
614*5796c8dcSSimon Schubert 
615*5796c8dcSSimon Schubert   storage_needed = bfd_get_symtab_upper_bound (objfile->obfd);
616*5796c8dcSSimon Schubert   if (storage_needed < 0)
617*5796c8dcSSimon Schubert     error (_("Can't read symbols from %s: %s"), bfd_get_filename (objfile->obfd),
618*5796c8dcSSimon Schubert 	   bfd_errmsg (bfd_get_error ()));
619*5796c8dcSSimon Schubert 
620*5796c8dcSSimon Schubert   if (storage_needed > 0)
621*5796c8dcSSimon Schubert     {
622*5796c8dcSSimon Schubert       symbol_table = (asymbol **) xmalloc (storage_needed);
623*5796c8dcSSimon Schubert       make_cleanup (xfree, symbol_table);
624*5796c8dcSSimon Schubert       symcount = bfd_canonicalize_symtab (objfile->obfd, symbol_table);
625*5796c8dcSSimon Schubert 
626*5796c8dcSSimon Schubert       if (symcount < 0)
627*5796c8dcSSimon Schubert 	error (_("Can't read symbols from %s: %s"), bfd_get_filename (objfile->obfd),
628*5796c8dcSSimon Schubert 	       bfd_errmsg (bfd_get_error ()));
629*5796c8dcSSimon Schubert 
630*5796c8dcSSimon Schubert       elf_symtab_read (objfile, ST_REGULAR, symcount, symbol_table);
631*5796c8dcSSimon Schubert     }
632*5796c8dcSSimon Schubert 
633*5796c8dcSSimon Schubert   /* Add the dynamic symbols.  */
634*5796c8dcSSimon Schubert 
635*5796c8dcSSimon Schubert   storage_needed = bfd_get_dynamic_symtab_upper_bound (objfile->obfd);
636*5796c8dcSSimon Schubert 
637*5796c8dcSSimon Schubert   if (storage_needed > 0)
638*5796c8dcSSimon Schubert     {
639*5796c8dcSSimon Schubert       dyn_symbol_table = (asymbol **) xmalloc (storage_needed);
640*5796c8dcSSimon Schubert       make_cleanup (xfree, dyn_symbol_table);
641*5796c8dcSSimon Schubert       dynsymcount = bfd_canonicalize_dynamic_symtab (objfile->obfd,
642*5796c8dcSSimon Schubert 						     dyn_symbol_table);
643*5796c8dcSSimon Schubert 
644*5796c8dcSSimon Schubert       if (dynsymcount < 0)
645*5796c8dcSSimon Schubert 	error (_("Can't read symbols from %s: %s"), bfd_get_filename (objfile->obfd),
646*5796c8dcSSimon Schubert 	       bfd_errmsg (bfd_get_error ()));
647*5796c8dcSSimon Schubert 
648*5796c8dcSSimon Schubert       elf_symtab_read (objfile, ST_DYNAMIC, dynsymcount, dyn_symbol_table);
649*5796c8dcSSimon Schubert     }
650*5796c8dcSSimon Schubert 
651*5796c8dcSSimon Schubert   /* Add synthetic symbols - for instance, names for any PLT entries.  */
652*5796c8dcSSimon Schubert 
653*5796c8dcSSimon Schubert   synthcount = bfd_get_synthetic_symtab (abfd, symcount, symbol_table,
654*5796c8dcSSimon Schubert 					 dynsymcount, dyn_symbol_table,
655*5796c8dcSSimon Schubert 					 &synthsyms);
656*5796c8dcSSimon Schubert   if (synthcount > 0)
657*5796c8dcSSimon Schubert     {
658*5796c8dcSSimon Schubert       asymbol **synth_symbol_table;
659*5796c8dcSSimon Schubert       long i;
660*5796c8dcSSimon Schubert 
661*5796c8dcSSimon Schubert       make_cleanup (xfree, synthsyms);
662*5796c8dcSSimon Schubert       synth_symbol_table = xmalloc (sizeof (asymbol *) * synthcount);
663*5796c8dcSSimon Schubert       for (i = 0; i < synthcount; i++)
664*5796c8dcSSimon Schubert 	synth_symbol_table[i] = synthsyms + i;
665*5796c8dcSSimon Schubert       make_cleanup (xfree, synth_symbol_table);
666*5796c8dcSSimon Schubert       elf_symtab_read (objfile, ST_SYNTHETIC, synthcount, synth_symbol_table);
667*5796c8dcSSimon Schubert     }
668*5796c8dcSSimon Schubert 
669*5796c8dcSSimon Schubert   /* Install any minimal symbols that have been collected as the current
670*5796c8dcSSimon Schubert      minimal symbols for this objfile.  The debug readers below this point
671*5796c8dcSSimon Schubert      should not generate new minimal symbols; if they do it's their
672*5796c8dcSSimon Schubert      responsibility to install them.  "mdebug" appears to be the only one
673*5796c8dcSSimon Schubert      which will do this.  */
674*5796c8dcSSimon Schubert 
675*5796c8dcSSimon Schubert   install_minimal_symbols (objfile);
676*5796c8dcSSimon Schubert   do_cleanups (back_to);
677*5796c8dcSSimon Schubert 
678*5796c8dcSSimon Schubert   /* Now process debugging information, which is contained in
679*5796c8dcSSimon Schubert      special ELF sections. */
680*5796c8dcSSimon Schubert 
681*5796c8dcSSimon Schubert   /* If we are reinitializing, or if we have never loaded syms yet,
682*5796c8dcSSimon Schubert      set table to empty.  MAINLINE is cleared so that *_read_psymtab
683*5796c8dcSSimon Schubert      functions do not all also re-initialize the psymbol table. */
684*5796c8dcSSimon Schubert   if (mainline)
685*5796c8dcSSimon Schubert     {
686*5796c8dcSSimon Schubert       init_psymbol_list (objfile, 0);
687*5796c8dcSSimon Schubert       mainline = 0;
688*5796c8dcSSimon Schubert     }
689*5796c8dcSSimon Schubert 
690*5796c8dcSSimon Schubert   /* We first have to find them... */
691*5796c8dcSSimon Schubert   bfd_map_over_sections (abfd, elf_locate_sections, (void *) & ei);
692*5796c8dcSSimon Schubert 
693*5796c8dcSSimon Schubert   /* ELF debugging information is inserted into the psymtab in the
694*5796c8dcSSimon Schubert      order of least informative first - most informative last.  Since
695*5796c8dcSSimon Schubert      the psymtab table is searched `most recent insertion first' this
696*5796c8dcSSimon Schubert      increases the probability that more detailed debug information
697*5796c8dcSSimon Schubert      for a section is found.
698*5796c8dcSSimon Schubert 
699*5796c8dcSSimon Schubert      For instance, an object file might contain both .mdebug (XCOFF)
700*5796c8dcSSimon Schubert      and .debug_info (DWARF2) sections then .mdebug is inserted first
701*5796c8dcSSimon Schubert      (searched last) and DWARF2 is inserted last (searched first).  If
702*5796c8dcSSimon Schubert      we don't do this then the XCOFF info is found first - for code in
703*5796c8dcSSimon Schubert      an included file XCOFF info is useless. */
704*5796c8dcSSimon Schubert 
705*5796c8dcSSimon Schubert   if (ei.mdebugsect)
706*5796c8dcSSimon Schubert     {
707*5796c8dcSSimon Schubert       const struct ecoff_debug_swap *swap;
708*5796c8dcSSimon Schubert 
709*5796c8dcSSimon Schubert       /* .mdebug section, presumably holding ECOFF debugging
710*5796c8dcSSimon Schubert          information.  */
711*5796c8dcSSimon Schubert       swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap;
712*5796c8dcSSimon Schubert       if (swap)
713*5796c8dcSSimon Schubert 	elfmdebug_build_psymtabs (objfile, swap, ei.mdebugsect);
714*5796c8dcSSimon Schubert     }
715*5796c8dcSSimon Schubert   if (ei.stabsect)
716*5796c8dcSSimon Schubert     {
717*5796c8dcSSimon Schubert       asection *str_sect;
718*5796c8dcSSimon Schubert 
719*5796c8dcSSimon Schubert       /* Stab sections have an associated string table that looks like
720*5796c8dcSSimon Schubert          a separate section.  */
721*5796c8dcSSimon Schubert       str_sect = bfd_get_section_by_name (abfd, ".stabstr");
722*5796c8dcSSimon Schubert 
723*5796c8dcSSimon Schubert       /* FIXME should probably warn about a stab section without a stabstr.  */
724*5796c8dcSSimon Schubert       if (str_sect)
725*5796c8dcSSimon Schubert 	elfstab_build_psymtabs (objfile,
726*5796c8dcSSimon Schubert 				mainline,
727*5796c8dcSSimon Schubert 				ei.stabsect,
728*5796c8dcSSimon Schubert 				str_sect->filepos,
729*5796c8dcSSimon Schubert 				bfd_section_size (abfd, str_sect));
730*5796c8dcSSimon Schubert     }
731*5796c8dcSSimon Schubert   if (dwarf2_has_info (objfile))
732*5796c8dcSSimon Schubert     {
733*5796c8dcSSimon Schubert       /* DWARF 2 sections */
734*5796c8dcSSimon Schubert       dwarf2_build_psymtabs (objfile, mainline);
735*5796c8dcSSimon Schubert     }
736*5796c8dcSSimon Schubert 
737*5796c8dcSSimon Schubert   /* FIXME: kettenis/20030504: This still needs to be integrated with
738*5796c8dcSSimon Schubert      dwarf2read.c in a better way.  */
739*5796c8dcSSimon Schubert   dwarf2_build_frame_info (objfile);
740*5796c8dcSSimon Schubert }
741*5796c8dcSSimon Schubert 
742*5796c8dcSSimon Schubert /* This cleans up the objfile's deprecated_sym_stab_info pointer, and
743*5796c8dcSSimon Schubert    the chain of stab_section_info's, that might be dangling from
744*5796c8dcSSimon Schubert    it.  */
745*5796c8dcSSimon Schubert 
746*5796c8dcSSimon Schubert static void
747*5796c8dcSSimon Schubert free_elfinfo (void *objp)
748*5796c8dcSSimon Schubert {
749*5796c8dcSSimon Schubert   struct objfile *objfile = (struct objfile *) objp;
750*5796c8dcSSimon Schubert   struct dbx_symfile_info *dbxinfo = objfile->deprecated_sym_stab_info;
751*5796c8dcSSimon Schubert   struct stab_section_info *ssi, *nssi;
752*5796c8dcSSimon Schubert 
753*5796c8dcSSimon Schubert   ssi = dbxinfo->stab_section_info;
754*5796c8dcSSimon Schubert   while (ssi)
755*5796c8dcSSimon Schubert     {
756*5796c8dcSSimon Schubert       nssi = ssi->next;
757*5796c8dcSSimon Schubert       xfree (ssi);
758*5796c8dcSSimon Schubert       ssi = nssi;
759*5796c8dcSSimon Schubert     }
760*5796c8dcSSimon Schubert 
761*5796c8dcSSimon Schubert   dbxinfo->stab_section_info = 0;	/* Just say No mo info about this.  */
762*5796c8dcSSimon Schubert }
763*5796c8dcSSimon Schubert 
764*5796c8dcSSimon Schubert 
765*5796c8dcSSimon Schubert /* Initialize anything that needs initializing when a completely new symbol
766*5796c8dcSSimon Schubert    file is specified (not just adding some symbols from another file, e.g. a
767*5796c8dcSSimon Schubert    shared library).
768*5796c8dcSSimon Schubert 
769*5796c8dcSSimon Schubert    We reinitialize buildsym, since we may be reading stabs from an ELF file.  */
770*5796c8dcSSimon Schubert 
771*5796c8dcSSimon Schubert static void
772*5796c8dcSSimon Schubert elf_new_init (struct objfile *ignore)
773*5796c8dcSSimon Schubert {
774*5796c8dcSSimon Schubert   stabsread_new_init ();
775*5796c8dcSSimon Schubert   buildsym_new_init ();
776*5796c8dcSSimon Schubert }
777*5796c8dcSSimon Schubert 
778*5796c8dcSSimon Schubert /* Perform any local cleanups required when we are done with a particular
779*5796c8dcSSimon Schubert    objfile.  I.E, we are in the process of discarding all symbol information
780*5796c8dcSSimon Schubert    for an objfile, freeing up all memory held for it, and unlinking the
781*5796c8dcSSimon Schubert    objfile struct from the global list of known objfiles. */
782*5796c8dcSSimon Schubert 
783*5796c8dcSSimon Schubert static void
784*5796c8dcSSimon Schubert elf_symfile_finish (struct objfile *objfile)
785*5796c8dcSSimon Schubert {
786*5796c8dcSSimon Schubert   if (objfile->deprecated_sym_stab_info != NULL)
787*5796c8dcSSimon Schubert     {
788*5796c8dcSSimon Schubert       xfree (objfile->deprecated_sym_stab_info);
789*5796c8dcSSimon Schubert     }
790*5796c8dcSSimon Schubert 
791*5796c8dcSSimon Schubert   dwarf2_free_objfile (objfile);
792*5796c8dcSSimon Schubert }
793*5796c8dcSSimon Schubert 
794*5796c8dcSSimon Schubert /* ELF specific initialization routine for reading symbols.
795*5796c8dcSSimon Schubert 
796*5796c8dcSSimon Schubert    It is passed a pointer to a struct sym_fns which contains, among other
797*5796c8dcSSimon Schubert    things, the BFD for the file whose symbols are being read, and a slot for
798*5796c8dcSSimon Schubert    a pointer to "private data" which we can fill with goodies.
799*5796c8dcSSimon Schubert 
800*5796c8dcSSimon Schubert    For now at least, we have nothing in particular to do, so this function is
801*5796c8dcSSimon Schubert    just a stub. */
802*5796c8dcSSimon Schubert 
803*5796c8dcSSimon Schubert static void
804*5796c8dcSSimon Schubert elf_symfile_init (struct objfile *objfile)
805*5796c8dcSSimon Schubert {
806*5796c8dcSSimon Schubert   /* ELF objects may be reordered, so set OBJF_REORDERED.  If we
807*5796c8dcSSimon Schubert      find this causes a significant slowdown in gdb then we could
808*5796c8dcSSimon Schubert      set it in the debug symbol readers only when necessary.  */
809*5796c8dcSSimon Schubert   objfile->flags |= OBJF_REORDERED;
810*5796c8dcSSimon Schubert }
811*5796c8dcSSimon Schubert 
812*5796c8dcSSimon Schubert /* When handling an ELF file that contains Sun STABS debug info,
813*5796c8dcSSimon Schubert    some of the debug info is relative to the particular chunk of the
814*5796c8dcSSimon Schubert    section that was generated in its individual .o file.  E.g.
815*5796c8dcSSimon Schubert    offsets to static variables are relative to the start of the data
816*5796c8dcSSimon Schubert    segment *for that module before linking*.  This information is
817*5796c8dcSSimon Schubert    painfully squirreled away in the ELF symbol table as local symbols
818*5796c8dcSSimon Schubert    with wierd names.  Go get 'em when needed.  */
819*5796c8dcSSimon Schubert 
820*5796c8dcSSimon Schubert void
821*5796c8dcSSimon Schubert elfstab_offset_sections (struct objfile *objfile, struct partial_symtab *pst)
822*5796c8dcSSimon Schubert {
823*5796c8dcSSimon Schubert   char *filename = pst->filename;
824*5796c8dcSSimon Schubert   struct dbx_symfile_info *dbx = objfile->deprecated_sym_stab_info;
825*5796c8dcSSimon Schubert   struct stab_section_info *maybe = dbx->stab_section_info;
826*5796c8dcSSimon Schubert   struct stab_section_info *questionable = 0;
827*5796c8dcSSimon Schubert   int i;
828*5796c8dcSSimon Schubert   char *p;
829*5796c8dcSSimon Schubert 
830*5796c8dcSSimon Schubert   /* The ELF symbol info doesn't include path names, so strip the path
831*5796c8dcSSimon Schubert      (if any) from the psymtab filename.  */
832*5796c8dcSSimon Schubert   while (0 != (p = strchr (filename, '/')))
833*5796c8dcSSimon Schubert     filename = p + 1;
834*5796c8dcSSimon Schubert 
835*5796c8dcSSimon Schubert   /* FIXME:  This linear search could speed up significantly
836*5796c8dcSSimon Schubert      if it was chained in the right order to match how we search it,
837*5796c8dcSSimon Schubert      and if we unchained when we found a match. */
838*5796c8dcSSimon Schubert   for (; maybe; maybe = maybe->next)
839*5796c8dcSSimon Schubert     {
840*5796c8dcSSimon Schubert       if (filename[0] == maybe->filename[0]
841*5796c8dcSSimon Schubert 	  && strcmp (filename, maybe->filename) == 0)
842*5796c8dcSSimon Schubert 	{
843*5796c8dcSSimon Schubert 	  /* We found a match.  But there might be several source files
844*5796c8dcSSimon Schubert 	     (from different directories) with the same name.  */
845*5796c8dcSSimon Schubert 	  if (0 == maybe->found)
846*5796c8dcSSimon Schubert 	    break;
847*5796c8dcSSimon Schubert 	  questionable = maybe;	/* Might use it later.  */
848*5796c8dcSSimon Schubert 	}
849*5796c8dcSSimon Schubert     }
850*5796c8dcSSimon Schubert 
851*5796c8dcSSimon Schubert   if (maybe == 0 && questionable != 0)
852*5796c8dcSSimon Schubert     {
853*5796c8dcSSimon Schubert       complaint (&symfile_complaints,
854*5796c8dcSSimon Schubert 		 _("elf/stab section information questionable for %s"), filename);
855*5796c8dcSSimon Schubert       maybe = questionable;
856*5796c8dcSSimon Schubert     }
857*5796c8dcSSimon Schubert 
858*5796c8dcSSimon Schubert   if (maybe)
859*5796c8dcSSimon Schubert     {
860*5796c8dcSSimon Schubert       /* Found it!  Allocate a new psymtab struct, and fill it in.  */
861*5796c8dcSSimon Schubert       maybe->found++;
862*5796c8dcSSimon Schubert       pst->section_offsets = (struct section_offsets *)
863*5796c8dcSSimon Schubert 	obstack_alloc (&objfile->objfile_obstack,
864*5796c8dcSSimon Schubert 		       SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
865*5796c8dcSSimon Schubert       for (i = 0; i < maybe->num_sections; i++)
866*5796c8dcSSimon Schubert 	(pst->section_offsets)->offsets[i] = maybe->sections[i];
867*5796c8dcSSimon Schubert       return;
868*5796c8dcSSimon Schubert     }
869*5796c8dcSSimon Schubert 
870*5796c8dcSSimon Schubert   /* We were unable to find any offsets for this file.  Complain.  */
871*5796c8dcSSimon Schubert   if (dbx->stab_section_info)	/* If there *is* any info, */
872*5796c8dcSSimon Schubert     complaint (&symfile_complaints,
873*5796c8dcSSimon Schubert 	       _("elf/stab section information missing for %s"), filename);
874*5796c8dcSSimon Schubert }
875*5796c8dcSSimon Schubert 
876*5796c8dcSSimon Schubert /* Register that we are able to handle ELF object file formats.  */
877*5796c8dcSSimon Schubert 
878*5796c8dcSSimon Schubert static struct sym_fns elf_sym_fns =
879*5796c8dcSSimon Schubert {
880*5796c8dcSSimon Schubert   bfd_target_elf_flavour,
881*5796c8dcSSimon Schubert   elf_new_init,			/* sym_new_init: init anything gbl to entire symtab */
882*5796c8dcSSimon Schubert   elf_symfile_init,		/* sym_init: read initial info, setup for sym_read() */
883*5796c8dcSSimon Schubert   elf_symfile_read,		/* sym_read: read a symbol file into symtab */
884*5796c8dcSSimon Schubert   elf_symfile_finish,		/* sym_finish: finished with file, cleanup */
885*5796c8dcSSimon Schubert   default_symfile_offsets,	/* sym_offsets:  Translate ext. to int. relocation */
886*5796c8dcSSimon Schubert   elf_symfile_segments,		/* sym_segments: Get segment information from
887*5796c8dcSSimon Schubert 				   a file.  */
888*5796c8dcSSimon Schubert   NULL,                         /* sym_read_linetable */
889*5796c8dcSSimon Schubert   NULL				/* next: pointer to next struct sym_fns */
890*5796c8dcSSimon Schubert };
891*5796c8dcSSimon Schubert 
892*5796c8dcSSimon Schubert void
893*5796c8dcSSimon Schubert _initialize_elfread (void)
894*5796c8dcSSimon Schubert {
895*5796c8dcSSimon Schubert   add_symtab_fns (&elf_sym_fns);
896*5796c8dcSSimon Schubert }
897