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