* blockframe.c (frameless_look_for_prologue): Correct the
[deliverable/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 /************************************************************************
22 * *
23 * NOTICE *
24 * *
25 * This file is still under construction. When it is complete, this *
26 * notice will be removed. Until then, direct any questions or changes *
27 * to Fred Fish at Cygnus Support (fnf@cygnus.com) *
28 * *
29 * FIXME Still needs support for shared libraries. *
30 * FIXME Still needs support for core files. *
31 * FIXME The ".debug" and ".line" section names are hardwired. *
32 * *
33 ************************************************************************/
34
35 #include "defs.h"
36 #include "bfd.h"
37 #include "libbfd.h"
38 #include "libhppa.h"
39 #include <syms.h>
40 #include "symtab.h"
41 #include "symfile.h"
42 #include "objfiles.h"
43 #include "buildsym.h"
44 #include "gdb-stabs.h"
45 #include "complaints.h"
46 #include <string.h>
47 #include "demangle.h"
48 #include <sys/file.h>
49 #include "aout/aout64.h"
50
51 /* Various things we might complain about... */
52
53 static void
54 pa_symfile_init PARAMS ((struct objfile *));
55
56 static void
57 pa_new_init PARAMS ((struct objfile *));
58
59 static void
60 pa_symfile_read PARAMS ((struct objfile *, struct section_offsets *, int));
61
62 static void
63 pa_symfile_finish PARAMS ((struct objfile *));
64
65 static void
66 pa_symtab_read PARAMS ((bfd *, CORE_ADDR, struct objfile *));
67
68 static void
69 free_painfo PARAMS ((PTR));
70
71 static struct section_offsets *
72 pa_symfile_offsets PARAMS ((struct objfile *, CORE_ADDR));
73
74 static void
75 record_minimal_symbol PARAMS ((char *, CORE_ADDR,
76 enum minimal_symbol_type,
77 struct objfile *));
78
79 static void
80 record_minimal_symbol (name, address, ms_type, objfile)
81 char *name;
82 CORE_ADDR address;
83 enum minimal_symbol_type ms_type;
84 struct objfile *objfile;
85 {
86 name = obsavestring (name, strlen (name), &objfile -> symbol_obstack);
87 prim_record_minimal_symbol (name, address, ms_type);
88 }
89
90 /*
91
92 LOCAL FUNCTION
93
94 pa_symtab_read -- read the symbol table of a PA file
95
96 SYNOPSIS
97
98 void pa_symtab_read (bfd *abfd, CORE_ADDR addr,
99 struct objfile *objfile)
100
101 DESCRIPTION
102
103 Given an open bfd, a base address to relocate symbols to, and a
104 flag that specifies whether or not this bfd is for an executable
105 or not (may be shared library for example), add all the global
106 function and data symbols to the minimal symbol table.
107 */
108
109 static void
110 pa_symtab_read (abfd, addr, objfile)
111 bfd *abfd;
112 CORE_ADDR addr;
113 struct objfile *objfile;
114 {
115 unsigned int number_of_symbols;
116 unsigned int i;
117 int val;
118 char *stringtab;
119 struct symbol_dictionary_record *buf, *bufp;
120 CONST int symsize = sizeof (struct symbol_dictionary_record);
121
122 number_of_symbols = bfd_get_symcount (abfd);
123
124 buf = alloca (symsize * number_of_symbols);
125 bfd_seek (abfd, obj_sym_filepos (abfd), L_SET);
126 val = bfd_read (buf, symsize * number_of_symbols, 1, abfd);
127 if (val != symsize * number_of_symbols)
128 error ("Couldn't read symbol dictionary!");
129
130 stringtab = alloca (obj_stringtab_size (abfd));
131 bfd_seek (abfd, obj_str_filepos (abfd), L_SET);
132 val = bfd_read (stringtab, obj_stringtab_size (abfd), 1, abfd);
133 if (val != obj_stringtab_size (abfd))
134 error ("Can't read in HP string table.");
135
136 for (i = 0, bufp = buf; i < number_of_symbols; i++, bufp++)
137 {
138 enum minimal_symbol_type ms_type;
139
140 QUIT;
141
142 if (bufp->symbol_scope != SS_UNIVERSAL)
143 continue;
144
145 switch (bufp->symbol_type)
146 {
147 case ST_SYM_EXT:
148 case ST_ARG_EXT:
149 continue;
150 case ST_CODE:
151 case ST_PRI_PROG:
152 case ST_SEC_PROG:
153 case ST_ENTRY:
154 case ST_MILLICODE:
155 ms_type = mst_text;
156 bufp->symbol_value &= ~0x3; /* clear out permission bits */
157 break;
158 case ST_DATA:
159 ms_type = mst_data;
160 break;
161 default:
162 continue;
163 }
164
165 if (bufp->name.n_strx > obj_stringtab_size (abfd))
166 error ("Invalid symbol data; bad HP string table offset: %d",
167 bufp->name.n_strx);
168
169 record_minimal_symbol (bufp->name.n_strx + stringtab,
170 bufp->symbol_value, ms_type,
171 objfile);
172 }
173
174 install_minimal_symbols (objfile);
175 }
176
177 /* Scan and build partial symbols for a symbol file.
178 We have been initialized by a call to pa_symfile_init, which
179 currently does nothing.
180
181 SECTION_OFFSETS is a set of offsets to apply to relocate the symbols
182 in each section. This is ignored, as it isn't needed for the PA.
183
184 MAINLINE is true if we are reading the main symbol
185 table (as opposed to a shared lib or dynamically loaded file).
186
187 This function only does the minimum work necessary for letting the
188 user "name" things symbolically; it does not read the entire symtab.
189 Instead, it reads the external and static symbols and puts them in partial
190 symbol tables. When more extensive information is requested of a
191 file, the corresponding partial symbol table is mutated into a full
192 fledged symbol table by going back and reading the symbols
193 for real.
194
195 We look for sections with specific names, to tell us what debug
196 format to look for: FIXME!!!
197
198 pastab_build_psymtabs() handles STABS symbols.
199
200 Note that PA files have a "minimal" symbol table, which is vaguely
201 reminiscent of a COFF symbol table, but has only the minimal information
202 necessary for linking. We process this also, and use the information to
203 build gdb's minimal symbol table. This gives us some minimal debugging
204 capability even for files compiled without -g. */
205
206 static void
207 pa_symfile_read (objfile, section_offsets, mainline)
208 struct objfile *objfile;
209 struct section_offsets *section_offsets;
210 int mainline;
211 {
212 bfd *abfd = objfile->obfd;
213 struct cleanup *back_to;
214 CORE_ADDR offset;
215
216 init_minimal_symbol_collection ();
217 back_to = make_cleanup (discard_minimal_symbols, 0);
218
219 make_cleanup (free_painfo, (PTR) objfile);
220
221 /* Process the normal PA symbol table first. */
222
223 /* FIXME, should take a section_offsets param, not just an offset. */
224
225 offset = ANOFFSET (section_offsets, 0);
226 pa_symtab_read (abfd, offset, objfile);
227
228 /* Now process debugging information, which is contained in
229 special PA sections. */
230
231 pastab_build_psymtabs (objfile, section_offsets, mainline);
232
233 do_cleanups (back_to);
234 }
235
236 /* This cleans up the objfile's sym_private pointer, and the chain of
237 stab_section_info's, that might be dangling from it. */
238
239 static void
240 free_painfo (objp)
241 PTR objp;
242 {
243 struct objfile *objfile = (struct objfile *)objp;
244 struct dbx_symfile_info *dbxinfo = (struct dbx_symfile_info *)
245 objfile->sym_private;
246 struct stab_section_info *ssi, *nssi;
247
248 ssi = dbxinfo->stab_section_info;
249 while (ssi)
250 {
251 nssi = ssi->next;
252 mfree (objfile->md, ssi);
253 ssi = nssi;
254 }
255
256 dbxinfo->stab_section_info = 0; /* Just say No mo info about this. */
257 }
258
259 /* Initialize anything that needs initializing when a completely new symbol
260 file is specified (not just adding some symbols from another file, e.g. a
261 shared library).
262
263 We reinitialize buildsym, since we may be reading stabs from a PA file. */
264
265 static void
266 pa_new_init (ignore)
267 struct objfile *ignore;
268 {
269 stabsread_new_init ();
270 buildsym_new_init ();
271 }
272
273 /* Perform any local cleanups required when we are done with a particular
274 objfile. I.E, we are in the process of discarding all symbol information
275 for an objfile, freeing up all memory held for it, and unlinking the
276 objfile struct from the global list of known objfiles. */
277
278 static void
279 pa_symfile_finish (objfile)
280 struct objfile *objfile;
281 {
282 if (objfile -> sym_private != NULL)
283 {
284 mfree (objfile -> md, objfile -> sym_private);
285 }
286 }
287
288 /* PA specific initialization routine for reading symbols.
289
290 It is passed a pointer to a struct sym_fns which contains, among other
291 things, the BFD for the file whose symbols are being read, and a slot for
292 a pointer to "private data" which we can fill with goodies.
293
294 This routine is almost a complete ripoff of dbx_symfile_init. The
295 common parts of these routines should be extracted and used instead of
296 duplicating this code. FIXME. */
297
298 static void
299 pa_symfile_init (objfile)
300 struct objfile *objfile;
301 {
302 int val;
303 bfd *sym_bfd = objfile->obfd;
304 char *name = bfd_get_filename (sym_bfd);
305 asection *stabsect; /* Section containing symbol table entries */
306 asection *stringsect; /* Section containing symbol name strings */
307
308 stabsect = bfd_get_section_by_name (sym_bfd, "$GDB_SYMBOLS$");
309 stringsect = bfd_get_section_by_name (sym_bfd, "$GDB_STRINGS$");
310
311 /* Allocate struct to keep track of the symfile */
312 objfile->sym_private = (PTR)
313 xmmalloc (objfile -> md, sizeof (struct dbx_symfile_info));
314
315 memset ((PTR) objfile->sym_private, 0, sizeof (struct dbx_symfile_info));
316
317 if (!stabsect)
318 return;
319
320 if (!stringsect)
321 error ("Found stabs, but not string section");
322
323 /* FIXME POKING INSIDE BFD DATA STRUCTURES */
324 #define STRING_TABLE_OFFSET (stringsect->filepos)
325 #define SYMBOL_TABLE_OFFSET (stabsect->filepos)
326
327 /* FIXME POKING INSIDE BFD DATA STRUCTURES */
328
329 DBX_SYMFILE_INFO (objfile)->stab_section_info = NULL;
330 DBX_TEXT_SECT (objfile) = bfd_get_section_by_name (sym_bfd, ".text");
331 if (!DBX_TEXT_SECT (objfile))
332 error ("Can't find .text section in symbol file");
333
334 DBX_SYMBOL_SIZE (objfile) = sizeof (struct internal_nlist);
335 DBX_SYMCOUNT (objfile) = bfd_section_size (sym_bfd, stabsect)
336 / DBX_SYMBOL_SIZE (objfile);
337 DBX_SYMTAB_OFFSET (objfile) = SYMBOL_TABLE_OFFSET;
338
339 /* Read the string table and stash it away in the psymbol_obstack. It is
340 only needed as long as we need to expand psymbols into full symbols,
341 so when we blow away the psymbol the string table goes away as well.
342 Note that gdb used to use the results of attempting to malloc the
343 string table, based on the size it read, as a form of sanity check
344 for botched byte swapping, on the theory that a byte swapped string
345 table size would be so totally bogus that the malloc would fail. Now
346 that we put in on the psymbol_obstack, we can't do this since gdb gets
347 a fatal error (out of virtual memory) if the size is bogus. We can
348 however at least check to see if the size is zero or some negative
349 value. */
350
351 DBX_STRINGTAB_SIZE (objfile) = bfd_section_size (sym_bfd, stringsect);
352
353 if (DBX_SYMCOUNT (objfile) == 0
354 || DBX_STRINGTAB_SIZE (objfile) == 0)
355 return;
356
357 if (DBX_STRINGTAB_SIZE (objfile) <= 0
358 || DBX_STRINGTAB_SIZE (objfile) > bfd_get_size (sym_bfd))
359 error ("ridiculous string table size (%d bytes).",
360 DBX_STRINGTAB_SIZE (objfile));
361
362 DBX_STRINGTAB (objfile) =
363 (char *) obstack_alloc (&objfile -> psymbol_obstack,
364 DBX_STRINGTAB_SIZE (objfile));
365
366 /* Now read in the string table in one big gulp. */
367
368 val = bfd_seek (sym_bfd, STRING_TABLE_OFFSET, L_SET);
369 if (val < 0)
370 perror_with_name (name);
371 val = bfd_read (DBX_STRINGTAB (objfile), DBX_STRINGTAB_SIZE (objfile), 1,
372 sym_bfd);
373 if (val != DBX_STRINGTAB_SIZE (objfile))
374 perror_with_name (name);
375 }
376
377 /* PA specific parsing routine for section offsets.
378
379 Plain and simple for now. */
380
381 static struct section_offsets *
382 pa_symfile_offsets (objfile, addr)
383 struct objfile *objfile;
384 CORE_ADDR addr;
385 {
386 struct section_offsets *section_offsets;
387 int i;
388
389 section_offsets = (struct section_offsets *)
390 obstack_alloc (&objfile -> psymbol_obstack,
391 sizeof (struct section_offsets) +
392 sizeof (section_offsets->offsets) * (SECT_OFF_MAX-1));
393
394 for (i = 0; i < SECT_OFF_MAX; i++)
395 ANOFFSET (section_offsets, i) = addr;
396
397 return section_offsets;
398 }
399 \f
400 /* Register that we are able to handle PA object file formats. */
401
402 /* This is probably a mistake. FIXME. Why can't the HP's use an ordinary
403 file format name with an -hppa suffix? */
404 static struct sym_fns pa_sym_fns =
405 {
406 "hppa", /* sym_name: name or name prefix of BFD target type */
407 4, /* sym_namelen: number of significant sym_name chars */
408 pa_new_init, /* sym_new_init: init anything gbl to entire symtab */
409 pa_symfile_init, /* sym_init: read initial info, setup for sym_read() */
410 pa_symfile_read, /* sym_read: read a symbol file into symtab */
411 pa_symfile_finish, /* sym_finish: finished with file, cleanup */
412 pa_symfile_offsets, /* sym_offsets: Translate ext. to int. relocation */
413 NULL /* next: pointer to next struct sym_fns */
414 };
415
416 void
417 _initialize_paread ()
418 {
419 add_symtab_fns (&pa_sym_fns);
420 }
This page took 0.038143 seconds and 5 git commands to generate.