* symfile.c (reread_symbols): When re-reading symbols, do all the
[binutils-gdb.git] / gdb / paread.c
1 /* Read HP PA/Risc object files for GDB.
2 Copyright 1991, 1992 Free Software Foundation, Inc.
3 Written by Fred Fish at Cygnus Support.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21 #include "defs.h"
22 #include "bfd.h"
23 #include <time.h> /* For time_t in libbfd.h. */
24 #include <sys/types.h> /* For time_t, if not in time.h. */
25 #include "libbfd.h"
26 #include "som.h"
27 #include "libhppa.h"
28 #include <syms.h>
29 #include "symtab.h"
30 #include "symfile.h"
31 #include "objfiles.h"
32 #include "buildsym.h"
33 #include "stabsread.h"
34 #include "gdb-stabs.h"
35 #include "complaints.h"
36 #include <string.h>
37 #include "demangle.h"
38 #include <sys/file.h>
39
40 /* Size of n_value and n_strx fields in a stab symbol. */
41 #define BYTES_IN_WORD 4
42
43 #include "aout/aout64.h"
44
45 /* Various things we might complain about... */
46
47 static void
48 pa_symfile_init PARAMS ((struct objfile *));
49
50 static void
51 pa_new_init PARAMS ((struct objfile *));
52
53 static void
54 read_unwind_info PARAMS ((struct objfile *));
55
56 static void
57 pa_symfile_read PARAMS ((struct objfile *, struct section_offsets *, int));
58
59 static void
60 pa_symfile_finish PARAMS ((struct objfile *));
61
62 static void
63 pa_symtab_read PARAMS ((bfd *, CORE_ADDR, struct objfile *));
64
65 static void
66 free_painfo PARAMS ((PTR));
67
68 static struct section_offsets *
69 pa_symfile_offsets PARAMS ((struct objfile *, CORE_ADDR));
70
71 static void
72 record_minimal_symbol PARAMS ((char *, CORE_ADDR,
73 enum minimal_symbol_type,
74 struct objfile *));
75
76 static void
77 record_minimal_symbol (name, address, ms_type, objfile)
78 char *name;
79 CORE_ADDR address;
80 enum minimal_symbol_type ms_type;
81 struct objfile *objfile;
82 {
83 name = obsavestring (name, strlen (name), &objfile -> symbol_obstack);
84 prim_record_minimal_symbol (name, address, ms_type);
85 }
86
87 /*
88
89 LOCAL FUNCTION
90
91 pa_symtab_read -- read the symbol table of a PA file
92
93 SYNOPSIS
94
95 void pa_symtab_read (bfd *abfd, CORE_ADDR addr,
96 struct objfile *objfile)
97
98 DESCRIPTION
99
100 Given an open bfd, a base address to relocate symbols to, and a
101 flag that specifies whether or not this bfd is for an executable
102 or not (may be shared library for example), add all the global
103 function and data symbols to the minimal symbol table.
104 */
105
106 static void
107 pa_symtab_read (abfd, addr, objfile)
108 bfd *abfd;
109 CORE_ADDR addr;
110 struct objfile *objfile;
111 {
112 unsigned int number_of_symbols;
113 unsigned int i;
114 int val;
115 char *stringtab;
116 struct symbol_dictionary_record *buf, *bufp, *endbufp;
117 char *symname;
118 CONST int symsize = sizeof (struct symbol_dictionary_record);
119
120 number_of_symbols = bfd_get_symcount (abfd);
121
122 buf = alloca (symsize * number_of_symbols);
123 bfd_seek (abfd, obj_som_sym_filepos (abfd), L_SET);
124 val = bfd_read (buf, symsize * number_of_symbols, 1, abfd);
125 if (val != symsize * number_of_symbols)
126 error ("Couldn't read symbol dictionary!");
127
128 stringtab = alloca (obj_som_stringtab_size (abfd));
129 bfd_seek (abfd, obj_som_str_filepos (abfd), L_SET);
130 val = bfd_read (stringtab, obj_som_stringtab_size (abfd), 1, abfd);
131 if (val != obj_som_stringtab_size (abfd))
132 error ("Can't read in HP string table.");
133
134 endbufp = buf + number_of_symbols;
135 for (bufp = buf; bufp < endbufp; ++bufp)
136 {
137 enum minimal_symbol_type ms_type;
138
139 QUIT;
140
141 switch (bufp->symbol_scope)
142 {
143 case SS_UNIVERSAL:
144 switch (bufp->symbol_type)
145 {
146 case ST_SYM_EXT:
147 case ST_ARG_EXT:
148 continue;
149
150 case ST_CODE:
151 case ST_PRI_PROG:
152 case ST_SEC_PROG:
153 case ST_ENTRY:
154 case ST_MILLICODE:
155 symname = bufp->name.n_strx + stringtab;
156 ms_type = mst_text;
157 bufp->symbol_value &= ~0x3; /* clear out permission bits */
158 break;
159 case ST_DATA:
160 symname = bufp->name.n_strx + stringtab;
161 ms_type = mst_data;
162 break;
163 default:
164 continue;
165 }
166 break;
167
168 #if 0
169 /* SS_GLOBAL and SS_LOCAL are two names for the same thing (!). */
170 case SS_GLOBAL:
171 #endif
172 case SS_LOCAL:
173 switch (bufp->symbol_type)
174 {
175 case ST_SYM_EXT:
176 case ST_ARG_EXT:
177 continue;
178
179 case ST_CODE:
180 symname = bufp->name.n_strx + stringtab;
181 ms_type = mst_file_text;
182 bufp->symbol_value &= ~0x3; /* clear out permission bits */
183
184 check_strange_names:
185 /* GAS leaves symbols with the prefixes "LS$", "LBB$",
186 and "LBE$" in .o files after assembling. And thus
187 they appear in the final executable. This can
188 cause problems if these special symbols have the
189 same value as real symbols. So ignore them. Also "LC$". */
190 if (*symname == 'L'
191 && (symname[2] == '$' || symname[3] == '$'))
192 continue;
193 break;
194
195 case ST_PRI_PROG:
196 case ST_SEC_PROG:
197 case ST_ENTRY:
198 case ST_MILLICODE:
199 symname = bufp->name.n_strx + stringtab;
200 ms_type = mst_file_text;
201 bufp->symbol_value &= ~0x3; /* clear out permission bits */
202 break;
203
204 case ST_DATA:
205 symname = bufp->name.n_strx + stringtab;
206 ms_type = mst_file_data;
207 goto check_strange_names;
208
209 default:
210 continue;
211 }
212 break;
213
214 default:
215 continue;
216 }
217
218 if (bufp->name.n_strx > obj_som_stringtab_size (abfd))
219 error ("Invalid symbol data; bad HP string table offset: %d",
220 bufp->name.n_strx);
221
222 record_minimal_symbol (symname,
223 bufp->symbol_value, ms_type,
224 objfile);
225 }
226
227 install_minimal_symbols (objfile);
228 }
229
230 /* Read in the backtrace information stored in the `$UNWIND_START$' section of
231 the object file. This info is used mainly by find_unwind_entry() to find
232 out the stack frame size and frame pointer used by procedures. We put
233 everything on the psymbol obstack in the objfile so that it automatically
234 gets freed when the objfile is destroyed. */
235
236 static void
237 read_unwind_info (objfile)
238 struct objfile *objfile;
239 {
240 asection *unwind_sec;
241 struct obj_unwind_info *ui;
242
243 ui = obstack_alloc (&objfile->psymbol_obstack,
244 sizeof (struct obj_unwind_info));
245
246 ui->table = NULL;
247 ui->cache = NULL;
248 ui->last = -1;
249
250 unwind_sec = bfd_get_section_by_name (objfile->obfd,
251 "$UNWIND_START$");
252 if (unwind_sec)
253 {
254 int size;
255 int i, *ip;
256
257 size = bfd_section_size (objfile->obfd, unwind_sec);
258 ui->table = obstack_alloc (&objfile->psymbol_obstack, size);
259 ui->last = size / sizeof (struct unwind_table_entry) - 1;
260
261 bfd_get_section_contents (objfile->obfd, unwind_sec, ui->table,
262 0, size);
263
264 OBJ_UNWIND_INFO (objfile) = ui;
265 }
266 }
267
268 /* Scan and build partial symbols for a symbol file.
269 We have been initialized by a call to pa_symfile_init, which
270 currently does nothing.
271
272 SECTION_OFFSETS is a set of offsets to apply to relocate the symbols
273 in each section. This is ignored, as it isn't needed for the PA.
274
275 MAINLINE is true if we are reading the main symbol
276 table (as opposed to a shared lib or dynamically loaded file).
277
278 This function only does the minimum work necessary for letting the
279 user "name" things symbolically; it does not read the entire symtab.
280 Instead, it reads the external and static symbols and puts them in partial
281 symbol tables. When more extensive information is requested of a
282 file, the corresponding partial symbol table is mutated into a full
283 fledged symbol table by going back and reading the symbols
284 for real.
285
286 We look for sections with specific names, to tell us what debug
287 format to look for: FIXME!!!
288
289 pastab_build_psymtabs() handles STABS symbols.
290
291 Note that PA files have a "minimal" symbol table, which is vaguely
292 reminiscent of a COFF symbol table, but has only the minimal information
293 necessary for linking. We process this also, and use the information to
294 build gdb's minimal symbol table. This gives us some minimal debugging
295 capability even for files compiled without -g. */
296
297 static void
298 pa_symfile_read (objfile, section_offsets, mainline)
299 struct objfile *objfile;
300 struct section_offsets *section_offsets;
301 int mainline;
302 {
303 bfd *abfd = objfile->obfd;
304 struct cleanup *back_to;
305 CORE_ADDR offset;
306
307 init_minimal_symbol_collection ();
308 back_to = make_cleanup (discard_minimal_symbols, 0);
309
310 make_cleanup (free_painfo, (PTR) objfile);
311
312 /* Process the normal PA symbol table first. */
313
314 /* FIXME, should take a section_offsets param, not just an offset. */
315
316 offset = ANOFFSET (section_offsets, 0);
317 pa_symtab_read (abfd, offset, objfile);
318
319 /* Now process debugging information, which is contained in
320 special PA sections. */
321
322 pastab_build_psymtabs (objfile, section_offsets, mainline);
323
324 read_unwind_info(objfile);
325
326 do_cleanups (back_to);
327 }
328
329 /* This cleans up the objfile's sym_stab_info pointer, and the chain of
330 stab_section_info's, that might be dangling from it. */
331
332 static void
333 free_painfo (objp)
334 PTR objp;
335 {
336 struct objfile *objfile = (struct objfile *)objp;
337 struct dbx_symfile_info *dbxinfo = (struct dbx_symfile_info *)
338 objfile->sym_stab_info;
339 struct stab_section_info *ssi, *nssi;
340
341 ssi = dbxinfo->stab_section_info;
342 while (ssi)
343 {
344 nssi = ssi->next;
345 mfree (objfile->md, ssi);
346 ssi = nssi;
347 }
348
349 dbxinfo->stab_section_info = 0; /* Just say No mo info about this. */
350 }
351
352 /* Initialize anything that needs initializing when a completely new symbol
353 file is specified (not just adding some symbols from another file, e.g. a
354 shared library).
355
356 We reinitialize buildsym, since we may be reading stabs from a PA file. */
357
358 static void
359 pa_new_init (ignore)
360 struct objfile *ignore;
361 {
362 stabsread_new_init ();
363 buildsym_new_init ();
364 }
365
366 /* Perform any local cleanups required when we are done with a particular
367 objfile. I.E, we are in the process of discarding all symbol information
368 for an objfile, freeing up all memory held for it, and unlinking the
369 objfile struct from the global list of known objfiles. */
370
371 static void
372 pa_symfile_finish (objfile)
373 struct objfile *objfile;
374 {
375 if (objfile -> sym_stab_info != NULL)
376 {
377 mfree (objfile -> md, objfile -> sym_stab_info);
378 }
379 }
380
381 /* PA specific initialization routine for reading symbols.
382
383 It is passed a pointer to a struct sym_fns which contains, among other
384 things, the BFD for the file whose symbols are being read, and a slot for
385 a pointer to "private data" which we can fill with goodies.
386
387 This routine is almost a complete ripoff of dbx_symfile_init. The
388 common parts of these routines should be extracted and used instead of
389 duplicating this code. FIXME. */
390
391 static void
392 pa_symfile_init (objfile)
393 struct objfile *objfile;
394 {
395 int val;
396 bfd *sym_bfd = objfile->obfd;
397 char *name = bfd_get_filename (sym_bfd);
398 asection *stabsect; /* Section containing symbol table entries */
399 asection *stringsect; /* Section containing symbol name strings */
400
401 stabsect = bfd_get_section_by_name (sym_bfd, "$GDB_SYMBOLS$");
402 stringsect = bfd_get_section_by_name (sym_bfd, "$GDB_STRINGS$");
403
404 /* Allocate struct to keep track of the symfile */
405 objfile->sym_stab_info = (PTR)
406 xmmalloc (objfile -> md, sizeof (struct dbx_symfile_info));
407
408 memset ((PTR) objfile->sym_stab_info, 0, sizeof (struct dbx_symfile_info));
409
410 if (!stabsect)
411 return;
412
413 if (!stringsect)
414 error ("Found stabs, but not string section");
415
416 /* FIXME POKING INSIDE BFD DATA STRUCTURES */
417 #define STRING_TABLE_OFFSET (stringsect->filepos)
418 #define SYMBOL_TABLE_OFFSET (stabsect->filepos)
419
420 /* FIXME POKING INSIDE BFD DATA STRUCTURES */
421
422 DBX_SYMFILE_INFO (objfile)->stab_section_info = NULL;
423 DBX_TEXT_SECT (objfile) = bfd_get_section_by_name (sym_bfd, ".text");
424 if (!DBX_TEXT_SECT (objfile))
425 error ("Can't find .text section in symbol file");
426
427 /* FIXME: I suspect this should be external_nlist. The size of host
428 types like long and bfd_vma should not affect how we read the
429 file. */
430 DBX_SYMBOL_SIZE (objfile) = sizeof (struct internal_nlist);
431 DBX_SYMCOUNT (objfile) = bfd_section_size (sym_bfd, stabsect)
432 / DBX_SYMBOL_SIZE (objfile);
433 DBX_SYMTAB_OFFSET (objfile) = SYMBOL_TABLE_OFFSET;
434
435 /* Read the string table and stash it away in the psymbol_obstack. It is
436 only needed as long as we need to expand psymbols into full symbols,
437 so when we blow away the psymbol the string table goes away as well.
438 Note that gdb used to use the results of attempting to malloc the
439 string table, based on the size it read, as a form of sanity check
440 for botched byte swapping, on the theory that a byte swapped string
441 table size would be so totally bogus that the malloc would fail. Now
442 that we put in on the psymbol_obstack, we can't do this since gdb gets
443 a fatal error (out of virtual memory) if the size is bogus. We can
444 however at least check to see if the size is zero or some negative
445 value. */
446
447 DBX_STRINGTAB_SIZE (objfile) = bfd_section_size (sym_bfd, stringsect);
448
449 if (DBX_SYMCOUNT (objfile) == 0
450 || DBX_STRINGTAB_SIZE (objfile) == 0)
451 return;
452
453 if (DBX_STRINGTAB_SIZE (objfile) <= 0
454 || DBX_STRINGTAB_SIZE (objfile) > bfd_get_size (sym_bfd))
455 error ("ridiculous string table size (%d bytes).",
456 DBX_STRINGTAB_SIZE (objfile));
457
458 DBX_STRINGTAB (objfile) =
459 (char *) obstack_alloc (&objfile -> psymbol_obstack,
460 DBX_STRINGTAB_SIZE (objfile));
461
462 /* Now read in the string table in one big gulp. */
463
464 val = bfd_seek (sym_bfd, STRING_TABLE_OFFSET, L_SET);
465 if (val < 0)
466 perror_with_name (name);
467 val = bfd_read (DBX_STRINGTAB (objfile), DBX_STRINGTAB_SIZE (objfile), 1,
468 sym_bfd);
469 if (val == 0)
470 error ("End of file reading string table");
471 else if (val < 0)
472 /* It's possible bfd_read should be setting bfd_error, and we should be
473 checking that. But currently it doesn't set bfd_error. */
474 perror_with_name (name);
475 else if (val != DBX_STRINGTAB_SIZE (objfile))
476 error ("Short read reading string table");
477 }
478
479 /* PA specific parsing routine for section offsets.
480
481 Plain and simple for now. */
482
483 static struct section_offsets *
484 pa_symfile_offsets (objfile, addr)
485 struct objfile *objfile;
486 CORE_ADDR addr;
487 {
488 struct section_offsets *section_offsets;
489 int i;
490
491 objfile->num_sections = SECT_OFF_MAX;
492 section_offsets = (struct section_offsets *)
493 obstack_alloc (&objfile -> psymbol_obstack,
494 sizeof (struct section_offsets)
495 + sizeof (section_offsets->offsets) * (SECT_OFF_MAX-1));
496
497 for (i = 0; i < SECT_OFF_MAX; i++)
498 ANOFFSET (section_offsets, i) = addr;
499
500 return section_offsets;
501 }
502 \f
503 /* Register that we are able to handle SOM object file formats (does this
504 work for hp300, or just PA? I suspect the latter). */
505
506 static struct sym_fns pa_sym_fns =
507 {
508 "som", /* sym_name: name or name prefix of BFD target type */
509 3, /* sym_namelen: number of significant sym_name chars */
510 pa_new_init, /* sym_new_init: init anything gbl to entire symtab */
511 pa_symfile_init, /* sym_init: read initial info, setup for sym_read() */
512 pa_symfile_read, /* sym_read: read a symbol file into symtab */
513 pa_symfile_finish, /* sym_finish: finished with file, cleanup */
514 pa_symfile_offsets, /* sym_offsets: Translate ext. to int. relocation */
515 NULL /* next: pointer to next struct sym_fns */
516 };
517
518 void
519 _initialize_paread ()
520 {
521 add_symtab_fns (&pa_sym_fns);
522 }