* config/pa/tm-hppa.h (unwind_table_entry): Use one of 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 #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 int
51 compare_unwind_entries PARAMS ((struct unwind_table_entry *,
52 struct unwind_table_entry *));
53
54 static void
55 pa_new_init PARAMS ((struct objfile *));
56
57 static void
58 read_unwind_info PARAMS ((struct objfile *));
59
60 static void
61 pa_symfile_read PARAMS ((struct objfile *, struct section_offsets *, int));
62
63 static void
64 pa_symfile_finish PARAMS ((struct objfile *));
65
66 static void
67 pa_symtab_read PARAMS ((bfd *, CORE_ADDR, struct objfile *));
68
69 static void
70 free_painfo PARAMS ((PTR));
71
72 static struct section_offsets *
73 pa_symfile_offsets PARAMS ((struct objfile *, CORE_ADDR));
74
75 static void
76 record_minimal_symbol PARAMS ((char *, CORE_ADDR,
77 enum minimal_symbol_type,
78 struct objfile *));
79
80 static void
81 record_minimal_symbol (name, address, ms_type, objfile)
82 char *name;
83 CORE_ADDR address;
84 enum minimal_symbol_type ms_type;
85 struct objfile *objfile;
86 {
87 name = obsavestring (name, strlen (name), &objfile -> symbol_obstack);
88 prim_record_minimal_symbol (name, address, ms_type, objfile);
89 }
90
91 /*
92
93 LOCAL FUNCTION
94
95 pa_symtab_read -- read the symbol table of a PA file
96
97 SYNOPSIS
98
99 void pa_symtab_read (bfd *abfd, CORE_ADDR addr,
100 struct objfile *objfile)
101
102 DESCRIPTION
103
104 Given an open bfd, a base address to relocate symbols to, and a
105 flag that specifies whether or not this bfd is for an executable
106 or not (may be shared library for example), add all the global
107 function and data symbols to the minimal symbol table.
108 */
109
110 static void
111 pa_symtab_read (abfd, addr, objfile)
112 bfd *abfd;
113 CORE_ADDR addr;
114 struct objfile *objfile;
115 {
116 unsigned int number_of_symbols;
117 unsigned int i;
118 int val;
119 char *stringtab;
120 struct symbol_dictionary_record *buf, *bufp, *endbufp;
121 char *symname;
122 CONST int symsize = sizeof (struct symbol_dictionary_record);
123
124 number_of_symbols = bfd_get_symcount (abfd);
125
126 buf = alloca (symsize * number_of_symbols);
127 bfd_seek (abfd, obj_som_sym_filepos (abfd), L_SET);
128 val = bfd_read (buf, symsize * number_of_symbols, 1, abfd);
129 if (val != symsize * number_of_symbols)
130 error ("Couldn't read symbol dictionary!");
131
132 stringtab = alloca (obj_som_stringtab_size (abfd));
133 bfd_seek (abfd, obj_som_str_filepos (abfd), L_SET);
134 val = bfd_read (stringtab, obj_som_stringtab_size (abfd), 1, abfd);
135 if (val != obj_som_stringtab_size (abfd))
136 error ("Can't read in HP string table.");
137
138 endbufp = buf + number_of_symbols;
139 for (bufp = buf; bufp < endbufp; ++bufp)
140 {
141 enum minimal_symbol_type ms_type;
142
143 QUIT;
144
145 switch (bufp->symbol_scope)
146 {
147 case SS_UNIVERSAL:
148 switch (bufp->symbol_type)
149 {
150 case ST_SYM_EXT:
151 case ST_ARG_EXT:
152 continue;
153
154 case ST_CODE:
155 case ST_PRI_PROG:
156 case ST_SEC_PROG:
157 case ST_ENTRY:
158 case ST_MILLICODE:
159 symname = bufp->name.n_strx + stringtab;
160 ms_type = mst_text;
161 bufp->symbol_value &= ~0x3; /* clear out permission bits */
162 break;
163 case ST_DATA:
164 symname = bufp->name.n_strx + stringtab;
165 ms_type = mst_data;
166 break;
167 default:
168 continue;
169 }
170 break;
171
172 #if 0
173 /* SS_GLOBAL and SS_LOCAL are two names for the same thing (!). */
174 case SS_GLOBAL:
175 #endif
176 case SS_LOCAL:
177 switch (bufp->symbol_type)
178 {
179 case ST_SYM_EXT:
180 case ST_ARG_EXT:
181 continue;
182
183 case ST_CODE:
184 symname = bufp->name.n_strx + stringtab;
185 ms_type = mst_file_text;
186 bufp->symbol_value &= ~0x3; /* clear out permission bits */
187
188 check_strange_names:
189 /* GAS leaves labels in .o files after assembling. At
190 least labels starting with "LS$", "LBB$", "LBE$",
191 "LC$", and "L$" can happen. This should be fixed in
192 the assembler and/or compiler, to save space in the
193 executable (and because having GDB make gross
194 distinctions based on the name is kind of ugly), but
195 until then, just ignore them. ("L$" at least, has something
196 to do with getting relocation correct, so that one might
197 be hard to fix). */
198 if (*symname == 'L'
199 && (symname[1] == '$' || symname[2] == '$'
200 || symname[3] == '$'))
201 continue;
202 break;
203
204 case ST_PRI_PROG:
205 case ST_SEC_PROG:
206 case ST_ENTRY:
207 case ST_MILLICODE:
208 symname = bufp->name.n_strx + stringtab;
209 ms_type = mst_file_text;
210 bufp->symbol_value &= ~0x3; /* clear out permission bits */
211 break;
212
213 case ST_DATA:
214 symname = bufp->name.n_strx + stringtab;
215 ms_type = mst_file_data;
216 goto check_strange_names;
217
218 default:
219 continue;
220 }
221 break;
222
223 default:
224 continue;
225 }
226
227 if (bufp->name.n_strx > obj_som_stringtab_size (abfd))
228 error ("Invalid symbol data; bad HP string table offset: %d",
229 bufp->name.n_strx);
230
231 record_minimal_symbol (symname,
232 bufp->symbol_value, ms_type,
233 objfile);
234 }
235
236 install_minimal_symbols (objfile);
237 }
238
239 /* Compare the start address for two unwind entries returning 1 if
240 the first address is larger than the second, -1 if the second is
241 larger than the first, and zero if they are equal. */
242
243 static int
244 compare_unwind_entries (a, b)
245 struct unwind_table_entry *a;
246 struct unwind_table_entry *b;
247 {
248 if (a->region_start > b->region_start)
249 return 1;
250 else if (a->region_start < b->region_start)
251 return -1;
252 else
253 return 0;
254 }
255
256 /* Read in the backtrace information stored in the `$UNWIND_START$' section of
257 the object file. This info is used mainly by find_unwind_entry() to find
258 out the stack frame size and frame pointer used by procedures. We put
259 everything on the psymbol obstack in the objfile so that it automatically
260 gets freed when the objfile is destroyed. */
261
262 static void
263 read_unwind_info (objfile)
264 struct objfile *objfile;
265 {
266 asection *unwind_sec, *stub_unwind_sec;
267 unsigned unwind_size, stub_unwind_size, total_size;
268 unsigned index, unwind_entries, stub_entries, total_entries;
269 struct obj_unwind_info *ui;
270
271 ui = obstack_alloc (&objfile->psymbol_obstack,
272 sizeof (struct obj_unwind_info));
273
274 ui->table = NULL;
275 ui->cache = NULL;
276 ui->last = -1;
277
278 /* Get hooks to both unwind sections. */
279 unwind_sec = bfd_get_section_by_name (objfile->obfd, "$UNWIND_START$");
280 stub_unwind_sec = bfd_get_section_by_name (objfile->obfd, "$UNWIND_END$");
281
282 /* Get sizes and unwind counts for both sections. */
283 if (unwind_sec)
284 {
285 unwind_size = bfd_section_size (objfile->obfd, unwind_sec);
286 unwind_entries = unwind_size / UNWIND_ENTRY_SIZE;
287 }
288 else
289 {
290 unwind_size = 0;
291 unwind_entries = 0;
292 }
293
294 if (stub_unwind_sec)
295 {
296 stub_unwind_size = bfd_section_size (objfile->obfd, stub_unwind_sec);
297 stub_entries = stub_unwind_size / STUB_UNWIND_ENTRY_SIZE;
298 }
299 else
300 {
301 stub_unwind_size = 0;
302 stub_entries = 0;
303 }
304
305 /* Compute total number of stubs. */
306 total_entries = unwind_entries + stub_entries;
307 total_size = total_entries * sizeof (struct unwind_table_entry);
308
309 /* Allocate memory for the unwind table. */
310 ui->table = obstack_alloc (&objfile->psymbol_obstack, total_size);
311 ui->last = total_entries + 1;
312
313 /* We will read the unwind entries into temporary memory, then
314 fill in the actual unwind table. */
315 if (unwind_size > 0)
316 {
317 unsigned long tmp;
318 unsigned i;
319 char *buf = alloca (unwind_size);
320
321 bfd_get_section_contents (objfile->obfd, unwind_sec, buf, 0, unwind_size);
322
323 /* Now internalize the information being careful to handle host/target
324 endian issues. */
325 for (i = 0; i < unwind_entries; i++)
326 {
327 ui->table[i].region_start = bfd_get_32 (objfile->obfd,
328 (bfd_byte *)buf);
329 buf += 4;
330 ui->table[i].region_end = bfd_get_32 (objfile->obfd, (bfd_byte *)buf);
331 buf += 4;
332 tmp = bfd_get_32 (objfile->obfd, (bfd_byte *)buf);
333 buf += 4;
334 ui->table[i].Cannot_unwind = (tmp >> 31) & 0x1;;
335 ui->table[i].Millicode = (tmp >> 30) & 0x1;
336 ui->table[i].Millicode_save_sr0 = (tmp >> 29) & 0x1;
337 ui->table[i].Region_description = (tmp >> 27) & 0x3;
338 ui->table[i].reserved1 = (tmp >> 26) & 0x1;
339 ui->table[i].Entry_SR = (tmp >> 25) & 0x1;
340 ui->table[i].Entry_FR = (tmp >> 21) & 0xf;
341 ui->table[i].Entry_GR = (tmp >> 16) & 0x1f;
342 ui->table[i].Args_stored = (tmp >> 15) & 0x1;
343 ui->table[i].Variable_Frame = (tmp >> 14) & 0x1;
344 ui->table[i].Separate_Package_Body = (tmp >> 13) & 0x1;
345 ui->table[i].Frame_Extension_Millicode = (tmp >> 12 ) & 0x1;
346 ui->table[i].Stack_Overflow_Check = (tmp >> 11) & 0x1;
347 ui->table[i].Two_Instruction_SP_Increment = (tmp >> 10) & 0x1;
348 ui->table[i].Ada_Region = (tmp >> 9) & 0x1;
349 ui->table[i].reserved2 = (tmp >> 5) & 0xf;
350 ui->table[i].Save_SP = (tmp >> 4) & 0x1;
351 ui->table[i].Save_RP = (tmp >> 3) & 0x1;
352 ui->table[i].Save_MRP_in_frame = (tmp >> 2) & 0x1;
353 ui->table[i].extn_ptr_defined = (tmp >> 1) & 0x1;
354 ui->table[i].Cleanup_defined = tmp & 0x1;
355 tmp = bfd_get_32 (objfile->obfd, (bfd_byte *)buf);
356 buf += 4;
357 ui->table[i].MPE_XL_interrupt_marker = (tmp >> 31) & 0x1;
358 ui->table[i].HP_UX_interrupt_marker = (tmp >> 30) & 0x1;
359 ui->table[i].Large_frame = (tmp >> 29) & 0x1;
360 ui->table[i].reserved4 = (tmp >> 27) & 0x3;
361 ui->table[i].Total_frame_size = tmp & 0x7ffffff;
362 }
363
364 }
365
366 if (stub_unwind_size > 0)
367 {
368 unsigned int i;
369 char *buf = alloca (stub_unwind_size);
370
371 /* Read in the stub unwind entries. */
372 bfd_get_section_contents (objfile->obfd, stub_unwind_sec, buf,
373 0, stub_unwind_size);
374
375 /* Now convert them into regular unwind entries. */
376 index = unwind_entries;
377 for (i = 0; i < stub_entries; i++, index++)
378 {
379 /* Clear out the next unwind entry. */
380 memset (&ui->table[index], 0, sizeof (struct unwind_table_entry));
381
382 /* Convert offset & size into region_start and region_end.
383 Stuff away the stub type into "reserved" fields. */
384 ui->table[index].region_start = bfd_get_32 (objfile->obfd,
385 (bfd_byte *) buf);
386 buf += 4;
387 ui->table[index].stub_type = bfd_get_8 (objfile->obfd,
388 (bfd_byte *) buf);
389 buf += 2;
390 ui->table[index].region_end
391 = ui->table[index].region_start + 4 * bfd_get_16 (objfile->obfd,
392 (bfd_byte *) buf);
393 buf += 2;
394 }
395
396 }
397
398 /* Unwind table needs to be kept sorted. */
399 qsort (ui->table, total_entries, sizeof (struct unwind_table_entry),
400 compare_unwind_entries);
401
402 /* Keep a pointer to the unwind information. */
403 OBJ_UNWIND_INFO (objfile) = ui;
404 }
405
406 /* Scan and build partial symbols for a symbol file.
407 We have been initialized by a call to pa_symfile_init, which
408 currently does nothing.
409
410 SECTION_OFFSETS is a set of offsets to apply to relocate the symbols
411 in each section. This is ignored, as it isn't needed for the PA.
412
413 MAINLINE is true if we are reading the main symbol
414 table (as opposed to a shared lib or dynamically loaded file).
415
416 This function only does the minimum work necessary for letting the
417 user "name" things symbolically; it does not read the entire symtab.
418 Instead, it reads the external and static symbols and puts them in partial
419 symbol tables. When more extensive information is requested of a
420 file, the corresponding partial symbol table is mutated into a full
421 fledged symbol table by going back and reading the symbols
422 for real.
423
424 We look for sections with specific names, to tell us what debug
425 format to look for: FIXME!!!
426
427 pastab_build_psymtabs() handles STABS symbols.
428
429 Note that PA files have a "minimal" symbol table, which is vaguely
430 reminiscent of a COFF symbol table, but has only the minimal information
431 necessary for linking. We process this also, and use the information to
432 build gdb's minimal symbol table. This gives us some minimal debugging
433 capability even for files compiled without -g. */
434
435 static void
436 pa_symfile_read (objfile, section_offsets, mainline)
437 struct objfile *objfile;
438 struct section_offsets *section_offsets;
439 int mainline;
440 {
441 bfd *abfd = objfile->obfd;
442 struct cleanup *back_to;
443 CORE_ADDR offset;
444
445 init_minimal_symbol_collection ();
446 back_to = make_cleanup (discard_minimal_symbols, 0);
447
448 make_cleanup (free_painfo, (PTR) objfile);
449
450 /* Process the normal PA symbol table first. */
451
452 /* FIXME, should take a section_offsets param, not just an offset. */
453
454 offset = ANOFFSET (section_offsets, 0);
455 pa_symtab_read (abfd, offset, objfile);
456
457 /* Now process debugging information, which is contained in
458 special PA sections. */
459
460 pastab_build_psymtabs (objfile, section_offsets, mainline);
461
462 read_unwind_info(objfile);
463
464 do_cleanups (back_to);
465 }
466
467 /* This cleans up the objfile's sym_stab_info pointer, and the chain of
468 stab_section_info's, that might be dangling from it. */
469
470 static void
471 free_painfo (objp)
472 PTR objp;
473 {
474 struct objfile *objfile = (struct objfile *)objp;
475 struct dbx_symfile_info *dbxinfo = (struct dbx_symfile_info *)
476 objfile->sym_stab_info;
477 struct stab_section_info *ssi, *nssi;
478
479 ssi = dbxinfo->stab_section_info;
480 while (ssi)
481 {
482 nssi = ssi->next;
483 mfree (objfile->md, ssi);
484 ssi = nssi;
485 }
486
487 dbxinfo->stab_section_info = 0; /* Just say No mo info about this. */
488 }
489
490 /* Initialize anything that needs initializing when a completely new symbol
491 file is specified (not just adding some symbols from another file, e.g. a
492 shared library).
493
494 We reinitialize buildsym, since we may be reading stabs from a PA file. */
495
496 static void
497 pa_new_init (ignore)
498 struct objfile *ignore;
499 {
500 stabsread_new_init ();
501 buildsym_new_init ();
502 }
503
504 /* Perform any local cleanups required when we are done with a particular
505 objfile. I.E, we are in the process of discarding all symbol information
506 for an objfile, freeing up all memory held for it, and unlinking the
507 objfile struct from the global list of known objfiles. */
508
509 static void
510 pa_symfile_finish (objfile)
511 struct objfile *objfile;
512 {
513 if (objfile -> sym_stab_info != NULL)
514 {
515 mfree (objfile -> md, objfile -> sym_stab_info);
516 }
517 }
518
519 /* PA specific initialization routine for reading symbols.
520
521 It is passed a pointer to a struct sym_fns which contains, among other
522 things, the BFD for the file whose symbols are being read, and a slot for
523 a pointer to "private data" which we can fill with goodies.
524
525 This routine is almost a complete ripoff of dbx_symfile_init. The
526 common parts of these routines should be extracted and used instead of
527 duplicating this code. FIXME. */
528
529 static void
530 pa_symfile_init (objfile)
531 struct objfile *objfile;
532 {
533 int val;
534 bfd *sym_bfd = objfile->obfd;
535 char *name = bfd_get_filename (sym_bfd);
536 asection *stabsect; /* Section containing symbol table entries */
537 asection *stringsect; /* Section containing symbol name strings */
538
539 stabsect = bfd_get_section_by_name (sym_bfd, "$GDB_SYMBOLS$");
540 stringsect = bfd_get_section_by_name (sym_bfd, "$GDB_STRINGS$");
541
542 /* Allocate struct to keep track of the symfile */
543 objfile->sym_stab_info = (PTR)
544 xmmalloc (objfile -> md, sizeof (struct dbx_symfile_info));
545
546 memset ((PTR) objfile->sym_stab_info, 0, sizeof (struct dbx_symfile_info));
547
548
549 /* FIXME POKING INSIDE BFD DATA STRUCTURES */
550 #define STRING_TABLE_OFFSET (stringsect->filepos)
551 #define SYMBOL_TABLE_OFFSET (stabsect->filepos)
552
553 /* FIXME POKING INSIDE BFD DATA STRUCTURES */
554
555 DBX_SYMFILE_INFO (objfile)->stab_section_info = NULL;
556 DBX_TEXT_SECT (objfile) = bfd_get_section_by_name (sym_bfd, "$TEXT$");
557 if (!DBX_TEXT_SECT (objfile))
558 error ("Can't find $TEXT$ section in symbol file");
559
560 if (!stabsect)
561 return;
562
563 if (!stringsect)
564 error ("Found stabs, but not string section");
565
566 /* FIXME: I suspect this should be external_nlist. The size of host
567 types like long and bfd_vma should not affect how we read the
568 file. */
569 DBX_SYMBOL_SIZE (objfile) = sizeof (struct internal_nlist);
570 DBX_SYMCOUNT (objfile) = bfd_section_size (sym_bfd, stabsect)
571 / DBX_SYMBOL_SIZE (objfile);
572 DBX_SYMTAB_OFFSET (objfile) = SYMBOL_TABLE_OFFSET;
573
574 /* Read the string table and stash it away in the psymbol_obstack. It is
575 only needed as long as we need to expand psymbols into full symbols,
576 so when we blow away the psymbol the string table goes away as well.
577 Note that gdb used to use the results of attempting to malloc the
578 string table, based on the size it read, as a form of sanity check
579 for botched byte swapping, on the theory that a byte swapped string
580 table size would be so totally bogus that the malloc would fail. Now
581 that we put in on the psymbol_obstack, we can't do this since gdb gets
582 a fatal error (out of virtual memory) if the size is bogus. We can
583 however at least check to see if the size is zero or some negative
584 value. */
585
586 DBX_STRINGTAB_SIZE (objfile) = bfd_section_size (sym_bfd, stringsect);
587
588 if (DBX_SYMCOUNT (objfile) == 0
589 || DBX_STRINGTAB_SIZE (objfile) == 0)
590 return;
591
592 if (DBX_STRINGTAB_SIZE (objfile) <= 0
593 || DBX_STRINGTAB_SIZE (objfile) > bfd_get_size (sym_bfd))
594 error ("ridiculous string table size (%d bytes).",
595 DBX_STRINGTAB_SIZE (objfile));
596
597 DBX_STRINGTAB (objfile) =
598 (char *) obstack_alloc (&objfile -> psymbol_obstack,
599 DBX_STRINGTAB_SIZE (objfile));
600
601 /* Now read in the string table in one big gulp. */
602
603 val = bfd_seek (sym_bfd, STRING_TABLE_OFFSET, L_SET);
604 if (val < 0)
605 perror_with_name (name);
606 val = bfd_read (DBX_STRINGTAB (objfile), DBX_STRINGTAB_SIZE (objfile), 1,
607 sym_bfd);
608 if (val == 0)
609 error ("End of file reading string table");
610 else if (val < 0)
611 /* It's possible bfd_read should be setting bfd_error, and we should be
612 checking that. But currently it doesn't set bfd_error. */
613 perror_with_name (name);
614 else if (val != DBX_STRINGTAB_SIZE (objfile))
615 error ("Short read reading string table");
616 }
617
618 /* PA specific parsing routine for section offsets.
619
620 Plain and simple for now. */
621
622 static struct section_offsets *
623 pa_symfile_offsets (objfile, addr)
624 struct objfile *objfile;
625 CORE_ADDR addr;
626 {
627 struct section_offsets *section_offsets;
628 int i;
629
630 objfile->num_sections = SECT_OFF_MAX;
631 section_offsets = (struct section_offsets *)
632 obstack_alloc (&objfile -> psymbol_obstack,
633 sizeof (struct section_offsets)
634 + sizeof (section_offsets->offsets) * (SECT_OFF_MAX-1));
635
636 for (i = 0; i < SECT_OFF_MAX; i++)
637 ANOFFSET (section_offsets, i) = addr;
638
639 return section_offsets;
640 }
641 \f
642 /* Register that we are able to handle SOM object file formats. */
643
644 static struct sym_fns pa_sym_fns =
645 {
646 bfd_target_som_flavour,
647 pa_new_init, /* sym_new_init: init anything gbl to entire symtab */
648 pa_symfile_init, /* sym_init: read initial info, setup for sym_read() */
649 pa_symfile_read, /* sym_read: read a symbol file into symtab */
650 pa_symfile_finish, /* sym_finish: finished with file, cleanup */
651 pa_symfile_offsets, /* sym_offsets: Translate ext. to int. relocation */
652 NULL /* next: pointer to next struct sym_fns */
653 };
654
655 void
656 _initialize_paread ()
657 {
658 add_symtab_fns (&pa_sym_fns);
659 }
This page took 0.043324 seconds and 5 git commands to generate.