Commit | Line | Data |
---|---|---|
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 | ||
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 | ||
684a832f SG |
21 | #include "defs.h" |
22 | #include "bfd.h" | |
1225fcbd | 23 | #include <time.h> /* For time_t in libbfd.h. */ |
ddf5d7e8 | 24 | #include <sys/types.h> /* For time_t, if not in time.h. */ |
684a832f | 25 | #include "libbfd.h" |
61a29659 | 26 | #include "som.h" |
003ccb90 | 27 | #include "libhppa.h" |
684a832f SG |
28 | #include <syms.h> |
29 | #include "symtab.h" | |
30 | #include "symfile.h" | |
31 | #include "objfiles.h" | |
32 | #include "buildsym.h" | |
2731625a | 33 | #include "stabsread.h" |
684a832f SG |
34 | #include "gdb-stabs.h" |
35 | #include "complaints.h" | |
36 | #include <string.h> | |
37 | #include "demangle.h" | |
38 | #include <sys/file.h> | |
1225fcbd JK |
39 | |
40 | /* Size of n_value and n_strx fields in a stab symbol. */ | |
41 | #define BYTES_IN_WORD 4 | |
42 | ||
0213d96f | 43 | #include "aout/aout64.h" |
684a832f SG |
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 | ||
fa9265e5 SG |
53 | static void |
54 | read_unwind_info PARAMS ((struct objfile *)); | |
55 | ||
684a832f SG |
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); | |
8d60affd | 84 | prim_record_minimal_symbol (name, address, ms_type, objfile); |
684a832f SG |
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; | |
3cde1ffa JK |
116 | struct symbol_dictionary_record *buf, *bufp, *endbufp; |
117 | char *symname; | |
0213d96f | 118 | CONST int symsize = sizeof (struct symbol_dictionary_record); |
684a832f | 119 | |
0213d96f | 120 | number_of_symbols = bfd_get_symcount (abfd); |
684a832f | 121 | |
0213d96f | 122 | buf = alloca (symsize * number_of_symbols); |
003ccb90 | 123 | bfd_seek (abfd, obj_som_sym_filepos (abfd), L_SET); |
0213d96f SG |
124 | val = bfd_read (buf, symsize * number_of_symbols, 1, abfd); |
125 | if (val != symsize * number_of_symbols) | |
684a832f SG |
126 | error ("Couldn't read symbol dictionary!"); |
127 | ||
003ccb90 KR |
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)) | |
684a832f | 132 | error ("Can't read in HP string table."); |
3cde1ffa JK |
133 | |
134 | endbufp = buf + number_of_symbols; | |
135 | for (bufp = buf; bufp < endbufp; ++bufp) | |
684a832f SG |
136 | { |
137 | enum minimal_symbol_type ms_type; | |
138 | ||
139 | QUIT; | |
140 | ||
3cde1ffa JK |
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 | ||
247145e6 JK |
168 | #if 0 |
169 | /* SS_GLOBAL and SS_LOCAL are two names for the same thing (!). */ | |
3cde1ffa | 170 | case SS_GLOBAL: |
247145e6 | 171 | #endif |
3cde1ffa JK |
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; | |
247145e6 JK |
181 | ms_type = mst_file_text; |
182 | bufp->symbol_value &= ~0x3; /* clear out permission bits */ | |
183 | ||
184 | check_strange_names: | |
3fd4045f JK |
185 | /* GAS leaves labels in .o files after assembling. At |
186 | least labels starting with "LS$", "LBB$", "LBE$", | |
187 | "LC$", and "L$" can happen. This should be fixed in | |
188 | the assembler and/or compiler, to save space in the | |
189 | executable (and because having GDB make gross | |
190 | distinctions based on the name is kind of ugly), but | |
e1164c83 JK |
191 | until then, just ignore them. ("L$" at least, has something |
192 | to do with getting relocation correct, so that one might | |
193 | be hard to fix). */ | |
3cde1ffa | 194 | if (*symname == 'L' |
ffb4ea6a JL |
195 | && (symname[1] == '$' || symname[2] == '$' |
196 | || symname[3] == '$')) | |
3cde1ffa | 197 | continue; |
3cde1ffa JK |
198 | break; |
199 | ||
200 | case ST_PRI_PROG: | |
201 | case ST_SEC_PROG: | |
202 | case ST_ENTRY: | |
203 | case ST_MILLICODE: | |
204 | symname = bufp->name.n_strx + stringtab; | |
205 | ms_type = mst_file_text; | |
206 | bufp->symbol_value &= ~0x3; /* clear out permission bits */ | |
207 | break; | |
247145e6 | 208 | |
3cde1ffa JK |
209 | case ST_DATA: |
210 | symname = bufp->name.n_strx + stringtab; | |
211 | ms_type = mst_file_data; | |
247145e6 JK |
212 | goto check_strange_names; |
213 | ||
3cde1ffa JK |
214 | default: |
215 | continue; | |
216 | } | |
247145e6 JK |
217 | break; |
218 | ||
3cde1ffa JK |
219 | default: |
220 | continue; | |
221 | } | |
684a832f | 222 | |
003ccb90 | 223 | if (bufp->name.n_strx > obj_som_stringtab_size (abfd)) |
684a832f SG |
224 | error ("Invalid symbol data; bad HP string table offset: %d", |
225 | bufp->name.n_strx); | |
226 | ||
3cde1ffa | 227 | record_minimal_symbol (symname, |
684a832f SG |
228 | bufp->symbol_value, ms_type, |
229 | objfile); | |
230 | } | |
231 | ||
232 | install_minimal_symbols (objfile); | |
233 | } | |
234 | ||
fa9265e5 SG |
235 | /* Read in the backtrace information stored in the `$UNWIND_START$' section of |
236 | the object file. This info is used mainly by find_unwind_entry() to find | |
237 | out the stack frame size and frame pointer used by procedures. We put | |
238 | everything on the psymbol obstack in the objfile so that it automatically | |
239 | gets freed when the objfile is destroyed. */ | |
240 | ||
241 | static void | |
242 | read_unwind_info (objfile) | |
243 | struct objfile *objfile; | |
244 | { | |
245 | asection *unwind_sec; | |
246 | struct obj_unwind_info *ui; | |
247 | ||
248 | ui = obstack_alloc (&objfile->psymbol_obstack, | |
249 | sizeof (struct obj_unwind_info)); | |
250 | ||
251 | ui->table = NULL; | |
252 | ui->cache = NULL; | |
253 | ui->last = -1; | |
254 | ||
255 | unwind_sec = bfd_get_section_by_name (objfile->obfd, | |
256 | "$UNWIND_START$"); | |
257 | if (unwind_sec) | |
258 | { | |
259 | int size; | |
260 | int i, *ip; | |
261 | ||
262 | size = bfd_section_size (objfile->obfd, unwind_sec); | |
263 | ui->table = obstack_alloc (&objfile->psymbol_obstack, size); | |
264 | ui->last = size / sizeof (struct unwind_table_entry) - 1; | |
265 | ||
266 | bfd_get_section_contents (objfile->obfd, unwind_sec, ui->table, | |
267 | 0, size); | |
268 | ||
269 | OBJ_UNWIND_INFO (objfile) = ui; | |
270 | } | |
271 | } | |
272 | ||
684a832f SG |
273 | /* Scan and build partial symbols for a symbol file. |
274 | We have been initialized by a call to pa_symfile_init, which | |
275 | currently does nothing. | |
276 | ||
277 | SECTION_OFFSETS is a set of offsets to apply to relocate the symbols | |
278 | in each section. This is ignored, as it isn't needed for the PA. | |
279 | ||
280 | MAINLINE is true if we are reading the main symbol | |
281 | table (as opposed to a shared lib or dynamically loaded file). | |
282 | ||
283 | This function only does the minimum work necessary for letting the | |
284 | user "name" things symbolically; it does not read the entire symtab. | |
285 | Instead, it reads the external and static symbols and puts them in partial | |
286 | symbol tables. When more extensive information is requested of a | |
287 | file, the corresponding partial symbol table is mutated into a full | |
288 | fledged symbol table by going back and reading the symbols | |
289 | for real. | |
290 | ||
291 | We look for sections with specific names, to tell us what debug | |
292 | format to look for: FIXME!!! | |
293 | ||
294 | pastab_build_psymtabs() handles STABS symbols. | |
295 | ||
296 | Note that PA files have a "minimal" symbol table, which is vaguely | |
297 | reminiscent of a COFF symbol table, but has only the minimal information | |
298 | necessary for linking. We process this also, and use the information to | |
299 | build gdb's minimal symbol table. This gives us some minimal debugging | |
300 | capability even for files compiled without -g. */ | |
301 | ||
302 | static void | |
303 | pa_symfile_read (objfile, section_offsets, mainline) | |
304 | struct objfile *objfile; | |
305 | struct section_offsets *section_offsets; | |
306 | int mainline; | |
307 | { | |
308 | bfd *abfd = objfile->obfd; | |
309 | struct cleanup *back_to; | |
310 | CORE_ADDR offset; | |
311 | ||
312 | init_minimal_symbol_collection (); | |
313 | back_to = make_cleanup (discard_minimal_symbols, 0); | |
314 | ||
315 | make_cleanup (free_painfo, (PTR) objfile); | |
316 | ||
317 | /* Process the normal PA symbol table first. */ | |
318 | ||
319 | /* FIXME, should take a section_offsets param, not just an offset. */ | |
320 | ||
321 | offset = ANOFFSET (section_offsets, 0); | |
322 | pa_symtab_read (abfd, offset, objfile); | |
323 | ||
324 | /* Now process debugging information, which is contained in | |
325 | special PA sections. */ | |
326 | ||
327 | pastab_build_psymtabs (objfile, section_offsets, mainline); | |
328 | ||
fa9265e5 SG |
329 | read_unwind_info(objfile); |
330 | ||
684a832f SG |
331 | do_cleanups (back_to); |
332 | } | |
333 | ||
965a5c32 | 334 | /* This cleans up the objfile's sym_stab_info pointer, and the chain of |
684a832f SG |
335 | stab_section_info's, that might be dangling from it. */ |
336 | ||
337 | static void | |
338 | free_painfo (objp) | |
339 | PTR objp; | |
340 | { | |
341 | struct objfile *objfile = (struct objfile *)objp; | |
342 | struct dbx_symfile_info *dbxinfo = (struct dbx_symfile_info *) | |
965a5c32 | 343 | objfile->sym_stab_info; |
684a832f SG |
344 | struct stab_section_info *ssi, *nssi; |
345 | ||
346 | ssi = dbxinfo->stab_section_info; | |
347 | while (ssi) | |
348 | { | |
349 | nssi = ssi->next; | |
350 | mfree (objfile->md, ssi); | |
351 | ssi = nssi; | |
352 | } | |
353 | ||
354 | dbxinfo->stab_section_info = 0; /* Just say No mo info about this. */ | |
355 | } | |
356 | ||
357 | /* Initialize anything that needs initializing when a completely new symbol | |
358 | file is specified (not just adding some symbols from another file, e.g. a | |
359 | shared library). | |
360 | ||
361 | We reinitialize buildsym, since we may be reading stabs from a PA file. */ | |
362 | ||
363 | static void | |
364 | pa_new_init (ignore) | |
365 | struct objfile *ignore; | |
366 | { | |
367 | stabsread_new_init (); | |
368 | buildsym_new_init (); | |
369 | } | |
370 | ||
371 | /* Perform any local cleanups required when we are done with a particular | |
372 | objfile. I.E, we are in the process of discarding all symbol information | |
373 | for an objfile, freeing up all memory held for it, and unlinking the | |
374 | objfile struct from the global list of known objfiles. */ | |
375 | ||
376 | static void | |
377 | pa_symfile_finish (objfile) | |
378 | struct objfile *objfile; | |
379 | { | |
965a5c32 | 380 | if (objfile -> sym_stab_info != NULL) |
684a832f | 381 | { |
965a5c32 | 382 | mfree (objfile -> md, objfile -> sym_stab_info); |
684a832f SG |
383 | } |
384 | } | |
385 | ||
684a832f SG |
386 | /* PA specific initialization routine for reading symbols. |
387 | ||
388 | It is passed a pointer to a struct sym_fns which contains, among other | |
389 | things, the BFD for the file whose symbols are being read, and a slot for | |
390 | a pointer to "private data" which we can fill with goodies. | |
391 | ||
392 | This routine is almost a complete ripoff of dbx_symfile_init. The | |
393 | common parts of these routines should be extracted and used instead of | |
394 | duplicating this code. FIXME. */ | |
395 | ||
396 | static void | |
397 | pa_symfile_init (objfile) | |
398 | struct objfile *objfile; | |
399 | { | |
400 | int val; | |
401 | bfd *sym_bfd = objfile->obfd; | |
402 | char *name = bfd_get_filename (sym_bfd); | |
0213d96f SG |
403 | asection *stabsect; /* Section containing symbol table entries */ |
404 | asection *stringsect; /* Section containing symbol name strings */ | |
405 | ||
406 | stabsect = bfd_get_section_by_name (sym_bfd, "$GDB_SYMBOLS$"); | |
407 | stringsect = bfd_get_section_by_name (sym_bfd, "$GDB_STRINGS$"); | |
684a832f SG |
408 | |
409 | /* Allocate struct to keep track of the symfile */ | |
965a5c32 | 410 | objfile->sym_stab_info = (PTR) |
684a832f SG |
411 | xmmalloc (objfile -> md, sizeof (struct dbx_symfile_info)); |
412 | ||
965a5c32 | 413 | memset ((PTR) objfile->sym_stab_info, 0, sizeof (struct dbx_symfile_info)); |
0213d96f | 414 | |
0213d96f | 415 | |
684a832f | 416 | /* FIXME POKING INSIDE BFD DATA STRUCTURES */ |
0213d96f SG |
417 | #define STRING_TABLE_OFFSET (stringsect->filepos) |
418 | #define SYMBOL_TABLE_OFFSET (stabsect->filepos) | |
684a832f SG |
419 | |
420 | /* FIXME POKING INSIDE BFD DATA STRUCTURES */ | |
421 | ||
422 | DBX_SYMFILE_INFO (objfile)->stab_section_info = NULL; | |
db98e4d8 | 423 | DBX_TEXT_SECT (objfile) = bfd_get_section_by_name (sym_bfd, "$TEXT$"); |
684a832f | 424 | if (!DBX_TEXT_SECT (objfile)) |
db98e4d8 | 425 | error ("Can't find $TEXT$ section in symbol file"); |
684a832f | 426 | |
f3628b39 FF |
427 | if (!stabsect) |
428 | return; | |
429 | ||
430 | if (!stringsect) | |
431 | error ("Found stabs, but not string section"); | |
432 | ||
1225fcbd JK |
433 | /* FIXME: I suspect this should be external_nlist. The size of host |
434 | types like long and bfd_vma should not affect how we read the | |
435 | file. */ | |
0213d96f SG |
436 | DBX_SYMBOL_SIZE (objfile) = sizeof (struct internal_nlist); |
437 | DBX_SYMCOUNT (objfile) = bfd_section_size (sym_bfd, stabsect) | |
438 | / DBX_SYMBOL_SIZE (objfile); | |
684a832f SG |
439 | DBX_SYMTAB_OFFSET (objfile) = SYMBOL_TABLE_OFFSET; |
440 | ||
441 | /* Read the string table and stash it away in the psymbol_obstack. It is | |
442 | only needed as long as we need to expand psymbols into full symbols, | |
443 | so when we blow away the psymbol the string table goes away as well. | |
444 | Note that gdb used to use the results of attempting to malloc the | |
445 | string table, based on the size it read, as a form of sanity check | |
446 | for botched byte swapping, on the theory that a byte swapped string | |
447 | table size would be so totally bogus that the malloc would fail. Now | |
448 | that we put in on the psymbol_obstack, we can't do this since gdb gets | |
449 | a fatal error (out of virtual memory) if the size is bogus. We can | |
450 | however at least check to see if the size is zero or some negative | |
451 | value. */ | |
452 | ||
0213d96f | 453 | DBX_STRINGTAB_SIZE (objfile) = bfd_section_size (sym_bfd, stringsect); |
684a832f SG |
454 | |
455 | if (DBX_SYMCOUNT (objfile) == 0 | |
456 | || DBX_STRINGTAB_SIZE (objfile) == 0) | |
457 | return; | |
458 | ||
459 | if (DBX_STRINGTAB_SIZE (objfile) <= 0 | |
460 | || DBX_STRINGTAB_SIZE (objfile) > bfd_get_size (sym_bfd)) | |
461 | error ("ridiculous string table size (%d bytes).", | |
462 | DBX_STRINGTAB_SIZE (objfile)); | |
463 | ||
464 | DBX_STRINGTAB (objfile) = | |
465 | (char *) obstack_alloc (&objfile -> psymbol_obstack, | |
466 | DBX_STRINGTAB_SIZE (objfile)); | |
467 | ||
468 | /* Now read in the string table in one big gulp. */ | |
469 | ||
470 | val = bfd_seek (sym_bfd, STRING_TABLE_OFFSET, L_SET); | |
471 | if (val < 0) | |
472 | perror_with_name (name); | |
473 | val = bfd_read (DBX_STRINGTAB (objfile), DBX_STRINGTAB_SIZE (objfile), 1, | |
474 | sym_bfd); | |
93af329a JK |
475 | if (val == 0) |
476 | error ("End of file reading string table"); | |
477 | else if (val < 0) | |
478 | /* It's possible bfd_read should be setting bfd_error, and we should be | |
479 | checking that. But currently it doesn't set bfd_error. */ | |
684a832f | 480 | perror_with_name (name); |
93af329a JK |
481 | else if (val != DBX_STRINGTAB_SIZE (objfile)) |
482 | error ("Short read reading string table"); | |
684a832f SG |
483 | } |
484 | ||
485 | /* PA specific parsing routine for section offsets. | |
486 | ||
487 | Plain and simple for now. */ | |
488 | ||
489 | static struct section_offsets * | |
490 | pa_symfile_offsets (objfile, addr) | |
491 | struct objfile *objfile; | |
492 | CORE_ADDR addr; | |
493 | { | |
494 | struct section_offsets *section_offsets; | |
495 | int i; | |
4d57c599 JK |
496 | |
497 | objfile->num_sections = SECT_OFF_MAX; | |
684a832f SG |
498 | section_offsets = (struct section_offsets *) |
499 | obstack_alloc (&objfile -> psymbol_obstack, | |
4d57c599 JK |
500 | sizeof (struct section_offsets) |
501 | + sizeof (section_offsets->offsets) * (SECT_OFF_MAX-1)); | |
684a832f SG |
502 | |
503 | for (i = 0; i < SECT_OFF_MAX; i++) | |
504 | ANOFFSET (section_offsets, i) = addr; | |
4d57c599 | 505 | |
684a832f SG |
506 | return section_offsets; |
507 | } | |
508 | \f | |
00b02f17 | 509 | /* Register that we are able to handle SOM object file formats. */ |
684a832f | 510 | |
684a832f SG |
511 | static struct sym_fns pa_sym_fns = |
512 | { | |
55449edd | 513 | bfd_target_som_flavour, |
684a832f SG |
514 | pa_new_init, /* sym_new_init: init anything gbl to entire symtab */ |
515 | pa_symfile_init, /* sym_init: read initial info, setup for sym_read() */ | |
516 | pa_symfile_read, /* sym_read: read a symbol file into symtab */ | |
517 | pa_symfile_finish, /* sym_finish: finished with file, cleanup */ | |
518 | pa_symfile_offsets, /* sym_offsets: Translate ext. to int. relocation */ | |
519 | NULL /* next: pointer to next struct sym_fns */ | |
520 | }; | |
521 | ||
522 | void | |
523 | _initialize_paread () | |
524 | { | |
525 | add_symtab_fns (&pa_sym_fns); | |
526 | } |