Introduce minimal_symbol_reader
[external/binutils.git] / gdb / mipsread.c
1 /* Read a symbol table in MIPS' format (Third-Eye).
2
3    Copyright (C) 1986-2016 Free Software Foundation, Inc.
4
5    Contributed by Alessandro Forin (af@cs.cmu.edu) at CMU.  Major work
6    by Per Bothner, John Gilmore and Ian Lance Taylor at Cygnus Support.
7
8    This file is part of GDB.
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
22
23 /* Read symbols from an ECOFF file.  Most of the work is done in
24    mdebugread.c.  */
25
26 #include "defs.h"
27 #include "bfd.h"
28 #include "symtab.h"
29 #include "objfiles.h"
30 #include "buildsym.h"
31 #include "stabsread.h"
32
33 #include "coff/sym.h"
34 #include "coff/internal.h"
35 #include "coff/ecoff.h"
36 #include "libcoff.h"            /* Private BFD COFF information.  */
37 #include "libecoff.h"           /* Private BFD ECOFF information.  */
38 #include "elf/common.h"
39 #include "elf/internal.h"
40 #include "elf/mips.h"
41
42 #include "psymtab.h"
43
44 static void
45 read_alphacoff_dynamic_symtab (struct section_offsets *,
46                                struct objfile *objfile);
47
48 /* Initialize anything that needs initializing when a completely new
49    symbol file is specified (not just adding some symbols from another
50    file, e.g. a shared library).  */
51
52 static void
53 mipscoff_new_init (struct objfile *ignore)
54 {
55   stabsread_new_init ();
56   buildsym_new_init ();
57 }
58
59 /* Initialize to read a symbol file (nothing to do).  */
60
61 static void
62 mipscoff_symfile_init (struct objfile *objfile)
63 {
64 }
65
66 /* Read a symbol file from a file.  */
67
68 static void
69 mipscoff_symfile_read (struct objfile *objfile, int symfile_flags)
70 {
71   bfd *abfd = objfile->obfd;
72
73   minimal_symbol_reader reader;
74
75   /* Now that the executable file is positioned at symbol table,
76      process it and define symbols accordingly.  */
77
78   if (!((*ecoff_backend (abfd)->debug_swap.read_debug_info)
79         (abfd, (asection *) NULL, &ecoff_data (abfd)->debug_info)))
80     error (_("Error reading symbol table: %s"), bfd_errmsg (bfd_get_error ()));
81
82   mdebug_build_psymtabs (objfile, &ecoff_backend (abfd)->debug_swap,
83                          &ecoff_data (abfd)->debug_info);
84
85   /* Add alpha coff dynamic symbols.  */
86
87   read_alphacoff_dynamic_symtab (objfile->section_offsets, objfile);
88
89   /* Install any minimal symbols that have been collected as the current
90      minimal symbols for this objfile.  */
91
92   reader.install (objfile);
93 }
94
95 /* Perform any local cleanups required when we are done with a
96    particular objfile.  */
97
98 static void
99 mipscoff_symfile_finish (struct objfile *objfile)
100 {
101 }
102
103 /* Alpha OSF/1 encapsulates the dynamic symbols in ELF format in a
104    standard COFF section.  The ELF format for the symbols differs from
105    the format defined in elf/external.h.  It seems that a normal ELF
106    32-bit format is used, and the representation only changes because
107    longs are 64-bit on the alpha.  In addition, the handling of
108    text/data section indices for symbols is different from the ELF
109    ABI.  As the BFD linker currently does not support dynamic linking
110    on the alpha, there seems to be no reason to pollute BFD with
111    another mixture of object file formats for now.  */
112
113 /* Format of an alpha external ELF symbol.  */
114
115 typedef struct
116 {
117   unsigned char st_name[4];     /* Symbol name, index in string table.  */
118   unsigned char st_pad[4];      /* Pad to long word boundary.  */
119   unsigned char st_value[8];    /* Value of the symbol.  */
120   unsigned char st_size[4];     /* Associated symbol size.  */
121   unsigned char st_info[1];     /* Type and binding attributes.  */
122   unsigned char st_other[1];    /* No defined meaning, 0.  */
123   unsigned char st_shndx[2];    /* Associated section index.  */
124 } Elfalpha_External_Sym;
125
126 /* Format of an alpha external ELF dynamic info structure.  */
127
128 typedef struct
129 {
130   unsigned char d_tag[4];       /* Tag.  */
131   unsigned char d_pad[4];       /* Pad to long word boundary.  */
132   union
133   {
134     unsigned char d_ptr[8];     /* Pointer value.  */
135     unsigned char d_val[4];     /* Integer value.  */
136   }
137   d_un;
138 } Elfalpha_External_Dyn;
139
140 /* Struct to obtain the section pointers for alpha dynamic symbol info.  */
141
142 struct alphacoff_dynsecinfo
143 {
144   asection *sym_sect;           /* Section pointer for .dynsym section.  */
145   asection *str_sect;           /* Section pointer for .dynstr section.  */
146   asection *dyninfo_sect;       /* Section pointer for .dynamic section.  */
147   asection *got_sect;           /* Section pointer for .got section.  */
148 };
149
150 /* We are called once per section from read_alphacoff_dynamic_symtab.
151    We need to examine each section we are passed, check to see if it
152    is something we are interested in processing, and if so, stash away
153    some access information for the section.  */
154
155 static void
156 alphacoff_locate_sections (bfd *ignore_abfd, asection *sectp, void *sip)
157 {
158   struct alphacoff_dynsecinfo *si;
159
160   si = (struct alphacoff_dynsecinfo *) sip;
161
162   if (strcmp (sectp->name, ".dynsym") == 0)
163     si->sym_sect = sectp;
164   else if (strcmp (sectp->name, ".dynstr") == 0)
165     si->str_sect = sectp;
166   else if (strcmp (sectp->name, ".dynamic") == 0)
167     si->dyninfo_sect = sectp;
168   else if (strcmp (sectp->name, ".got") == 0)
169       si->got_sect = sectp;
170 }
171
172 /* Scan an alpha dynamic symbol table for symbols of interest and add
173    them to the minimal symbol table.  */
174
175 static void
176 read_alphacoff_dynamic_symtab (struct section_offsets *section_offsets,
177                                struct objfile *objfile)
178 {
179   bfd *abfd = objfile->obfd;
180   struct alphacoff_dynsecinfo si;
181   char *sym_secptr;
182   char *str_secptr;
183   char *dyninfo_secptr;
184   char *got_secptr;
185   bfd_size_type sym_secsize;
186   bfd_size_type str_secsize;
187   bfd_size_type dyninfo_secsize;
188   bfd_size_type got_secsize;
189   int sym_count;
190   int i;
191   int stripped;
192   Elfalpha_External_Sym *x_symp;
193   char *dyninfo_p;
194   char *dyninfo_end;
195   int got_entry_size = 8;
196   int dt_mips_local_gotno = -1;
197   int dt_mips_gotsym = -1;
198   struct cleanup *cleanups;
199
200   /* We currently only know how to handle alpha dynamic symbols.  */
201   if (bfd_get_arch (abfd) != bfd_arch_alpha)
202     return;
203
204   /* Locate the dynamic symbols sections and read them in.  */
205   memset ((char *) &si, 0, sizeof (si));
206   bfd_map_over_sections (abfd, alphacoff_locate_sections, (void *) & si);
207   if (si.sym_sect == NULL || si.str_sect == NULL
208       || si.dyninfo_sect == NULL || si.got_sect == NULL)
209     return;
210
211   sym_secsize = bfd_get_section_size (si.sym_sect);
212   str_secsize = bfd_get_section_size (si.str_sect);
213   dyninfo_secsize = bfd_get_section_size (si.dyninfo_sect);
214   got_secsize = bfd_get_section_size (si.got_sect);
215   sym_secptr = (char *) xmalloc (sym_secsize);
216   cleanups = make_cleanup (xfree, sym_secptr);
217   str_secptr = (char *) xmalloc (str_secsize);
218   make_cleanup (xfree, str_secptr);
219   dyninfo_secptr = (char *) xmalloc (dyninfo_secsize);
220   make_cleanup (xfree, dyninfo_secptr);
221   got_secptr = (char *) xmalloc (got_secsize);
222   make_cleanup (xfree, got_secptr);
223
224   if (!bfd_get_section_contents (abfd, si.sym_sect, sym_secptr,
225                                  (file_ptr) 0, sym_secsize))
226     {
227       do_cleanups (cleanups);
228       return;
229     }
230   if (!bfd_get_section_contents (abfd, si.str_sect, str_secptr,
231                                  (file_ptr) 0, str_secsize))
232     {
233       do_cleanups (cleanups);
234       return;
235     }
236   if (!bfd_get_section_contents (abfd, si.dyninfo_sect, dyninfo_secptr,
237                                  (file_ptr) 0, dyninfo_secsize))
238     {
239       do_cleanups (cleanups);
240       return;
241     }
242   if (!bfd_get_section_contents (abfd, si.got_sect, got_secptr,
243                                  (file_ptr) 0, got_secsize))
244     {
245       do_cleanups (cleanups);
246       return;
247     }
248
249   /* Find the number of local GOT entries and the index for the
250      first dynamic symbol in the GOT.  */
251   for (dyninfo_p = dyninfo_secptr, dyninfo_end = dyninfo_p + dyninfo_secsize;
252        dyninfo_p < dyninfo_end;
253        dyninfo_p += sizeof (Elfalpha_External_Dyn))
254     {
255       Elfalpha_External_Dyn *x_dynp = (Elfalpha_External_Dyn *) dyninfo_p;
256       long dyn_tag;
257
258       dyn_tag = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp->d_tag);
259       if (dyn_tag == DT_NULL)
260         break;
261       else if (dyn_tag == DT_MIPS_LOCAL_GOTNO)
262         {
263           if (dt_mips_local_gotno < 0)
264             dt_mips_local_gotno
265               = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp->d_un.d_val);
266         }
267       else if (dyn_tag == DT_MIPS_GOTSYM)
268         {
269           if (dt_mips_gotsym < 0)
270             dt_mips_gotsym
271               = bfd_h_get_32 (abfd, (bfd_byte *) x_dynp->d_un.d_val);
272         }
273     }
274   if (dt_mips_local_gotno < 0 || dt_mips_gotsym < 0)
275     {
276       do_cleanups (cleanups);
277       return;
278     }
279
280   /* Scan all dynamic symbols and enter them into the minimal symbol
281      table if appropriate.  */
282   sym_count = sym_secsize / sizeof (Elfalpha_External_Sym);
283   stripped = (bfd_get_symcount (abfd) == 0);
284
285   /* Skip first symbol, which is a null dummy.  */
286   for (i = 1, x_symp = (Elfalpha_External_Sym *) sym_secptr + 1;
287        i < sym_count;
288        i++, x_symp++)
289     {
290       unsigned long strx;
291       char *name;
292       bfd_vma sym_value;
293       unsigned char sym_info;
294       unsigned int sym_shndx;
295       int isglobal;
296       enum minimal_symbol_type ms_type;
297
298       strx = bfd_h_get_32 (abfd, (bfd_byte *) x_symp->st_name);
299       if (strx >= str_secsize)
300         continue;
301       name = str_secptr + strx;
302       if (*name == '\0' || *name == '.')
303         continue;
304
305       sym_value = bfd_h_get_64 (abfd, (bfd_byte *) x_symp->st_value);
306       sym_info = bfd_h_get_8 (abfd, (bfd_byte *) x_symp->st_info);
307       sym_shndx = bfd_h_get_16 (abfd, (bfd_byte *) x_symp->st_shndx);
308       if (sym_shndx >= (SHN_LORESERVE & 0xffff))
309         sym_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
310       isglobal = (ELF_ST_BIND (sym_info) == STB_GLOBAL);
311
312       if (sym_shndx == SHN_UNDEF)
313         {
314           /* Handle undefined functions which are defined in a shared
315              library.  */
316           if (ELF_ST_TYPE (sym_info) != STT_FUNC
317               || ELF_ST_BIND (sym_info) != STB_GLOBAL)
318             continue;
319
320           ms_type = mst_solib_trampoline;
321
322           /* If sym_value is nonzero, it points to the shared library
323              trampoline entry, which is what we are looking for.
324
325              If sym_value is zero, then we have to get the GOT entry
326              for the symbol.
327
328              If the GOT entry is nonzero, it represents the quickstart
329              address of the function and we use that as the symbol
330              value.
331
332              If the GOT entry is zero, the function address has to be
333              resolved by the runtime loader before the executable is
334              started.  We are unable to find any meaningful address
335              for these functions in the executable file, so we skip
336              them.  */
337           if (sym_value == 0)
338             {
339               int got_entry_offset =
340                 (i - dt_mips_gotsym + dt_mips_local_gotno) * got_entry_size;
341
342               if (got_entry_offset < 0 || got_entry_offset >= got_secsize)
343                 continue;
344               sym_value =
345                 bfd_h_get_64 (abfd,
346                               (bfd_byte *) (got_secptr + got_entry_offset));
347               if (sym_value == 0)
348                 continue;
349             }
350         }
351       else
352         {
353           /* Symbols defined in the executable itself.  We only care
354              about them if this is a stripped executable, otherwise
355              they have been retrieved from the normal symbol table
356              already.  */
357           if (!stripped)
358             continue;
359
360           if (sym_shndx == SHN_MIPS_TEXT)
361             {
362               if (isglobal)
363                 ms_type = mst_text;
364               else
365                 ms_type = mst_file_text;
366             }
367           else if (sym_shndx == SHN_MIPS_DATA)
368             {
369               if (isglobal)
370                 ms_type = mst_data;
371               else
372                 ms_type = mst_file_data;
373             }
374           else if (sym_shndx == SHN_MIPS_ACOMMON)
375             {
376               if (isglobal)
377                 ms_type = mst_bss;
378               else
379                 ms_type = mst_file_bss;
380             }
381           else if (sym_shndx == SHN_ABS)
382             {
383               ms_type = mst_abs;
384             }
385           else
386             {
387               continue;
388             }
389         }
390
391       prim_record_minimal_symbol (name, sym_value, ms_type, objfile);
392     }
393
394   do_cleanups (cleanups);
395 }
396
397 /* Initialization.  */
398
399 static const struct sym_fns ecoff_sym_fns =
400 {
401   mipscoff_new_init,            /* init anything gbl to entire symtab */
402   mipscoff_symfile_init,        /* read initial info, setup for sym_read() */
403   mipscoff_symfile_read,        /* read a symbol file into symtab */
404   NULL,                         /* sym_read_psymbols */
405   mipscoff_symfile_finish,      /* finished with file, cleanup */
406   default_symfile_offsets,      /* dummy FIXME til implem sym reloc */
407   default_symfile_segments,     /* Get segment information from a file.  */
408   NULL,
409   default_symfile_relocate,     /* Relocate a debug section.  */
410   NULL,                         /* sym_probe_fns */
411   &psym_functions
412 };
413
414 /* Provide a prototype to silence -Wmissing-prototypes.  */
415 void _initialize_mipsread (void);
416
417 void
418 _initialize_mipsread (void)
419 {
420   add_symtab_fns (bfd_target_ecoff_flavour, &ecoff_sym_fns);
421 }