1 /* Read HP PA/Risc object files for GDB.
2 Copyright 1991, 1992, 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
4 Written by Fred Fish at Cygnus Support.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
30 #include "stabsread.h"
31 #include "gdb-stabs.h"
32 #include "complaints.h"
33 #include "gdb_string.h"
38 /* Various things we might complain about... */
40 static void som_symfile_init (struct objfile
*);
42 static void som_new_init (struct objfile
*);
44 static void som_symfile_read (struct objfile
*, int);
46 static void som_symfile_finish (struct objfile
*);
48 static void som_symtab_read (bfd
*, struct objfile
*,
49 struct section_offsets
*);
51 static void som_symfile_offsets (struct objfile
*, struct section_addr_info
*);
53 /* FIXME: These should really be in a common header somewhere */
55 extern void hpread_build_psymtabs (struct objfile
*, int);
57 extern void hpread_symfile_finish (struct objfile
*);
59 extern void hpread_symfile_init (struct objfile
*);
61 extern void do_pxdb (bfd
*);
67 som_symtab_read -- read the symbol table of a SOM file
71 void som_symtab_read (bfd *abfd, struct objfile *objfile,
72 struct section_offsets *section_offsets)
76 Given an open bfd, a base address to relocate symbols to, and a
77 flag that specifies whether or not this bfd is for an executable
78 or not (may be shared library for example), add all the global
79 function and data symbols to the minimal symbol table.
83 som_symtab_read (bfd
*abfd
, struct objfile
*objfile
,
84 struct section_offsets
*section_offsets
)
86 unsigned int number_of_symbols
;
90 struct symbol_dictionary_record
*buf
, *bufp
, *endbufp
;
92 CONST
int symsize
= sizeof (struct symbol_dictionary_record
);
93 CORE_ADDR text_offset
, data_offset
;
96 text_offset
= ANOFFSET (section_offsets
, 0);
97 data_offset
= ANOFFSET (section_offsets
, 1);
99 number_of_symbols
= bfd_get_symcount (abfd
);
101 /* FIXME (alloca): could be quite large. */
102 buf
= alloca (symsize
* number_of_symbols
);
103 bfd_seek (abfd
, obj_som_sym_filepos (abfd
), SEEK_SET
);
104 val
= bfd_bread (buf
, symsize
* number_of_symbols
, abfd
);
105 if (val
!= symsize
* number_of_symbols
)
106 error ("Couldn't read symbol dictionary!");
108 /* FIXME (alloca): could be quite large. */
109 stringtab
= alloca (obj_som_stringtab_size (abfd
));
110 bfd_seek (abfd
, obj_som_str_filepos (abfd
), SEEK_SET
);
111 val
= bfd_bread (stringtab
, obj_som_stringtab_size (abfd
), abfd
);
112 if (val
!= obj_som_stringtab_size (abfd
))
113 error ("Can't read in HP string table.");
115 /* We need to determine if objfile is a dynamic executable (so we
116 can do the right thing for ST_ENTRY vs ST_CODE symbols).
118 There's nothing in the header which easily allows us to do
121 This code used to rely upon the existence of a $SHLIB_INFO$
122 section to make this determination. HP claims that it is
123 more accurate to check for a nonzero text offset, but they
124 have not provided any information about why that test is
126 dynamic
= (text_offset
!= 0);
128 endbufp
= buf
+ number_of_symbols
;
129 for (bufp
= buf
; bufp
< endbufp
; ++bufp
)
131 enum minimal_symbol_type ms_type
;
135 switch (bufp
->symbol_scope
)
139 switch (bufp
->symbol_type
)
149 symname
= bufp
->name
.n_strx
+ stringtab
;
151 bufp
->symbol_value
+= text_offset
;
152 bufp
->symbol_value
= SMASH_TEXT_ADDRESS (bufp
->symbol_value
);
156 symname
= bufp
->name
.n_strx
+ stringtab
;
157 /* For a dynamic executable, ST_ENTRY symbols are
158 the stubs, while the ST_CODE symbol is the real
161 ms_type
= mst_solib_trampoline
;
164 bufp
->symbol_value
+= text_offset
;
165 bufp
->symbol_value
= SMASH_TEXT_ADDRESS (bufp
->symbol_value
);
169 symname
= bufp
->name
.n_strx
+ stringtab
;
170 ms_type
= mst_solib_trampoline
;
171 bufp
->symbol_value
+= text_offset
;
172 bufp
->symbol_value
= SMASH_TEXT_ADDRESS (bufp
->symbol_value
);
176 symname
= bufp
->name
.n_strx
+ stringtab
;
177 bufp
->symbol_value
+= data_offset
;
186 /* SS_GLOBAL and SS_LOCAL are two names for the same thing (!). */
190 switch (bufp
->symbol_type
)
197 symname
= bufp
->name
.n_strx
+ stringtab
;
198 ms_type
= mst_file_text
;
199 bufp
->symbol_value
+= text_offset
;
200 bufp
->symbol_value
= SMASH_TEXT_ADDRESS (bufp
->symbol_value
);
203 /* Utah GCC 2.5, FSF GCC 2.6 and later generate correct local
204 label prefixes for stabs, constant data, etc. So we need
205 only filter out L$ symbols which are left in due to
206 limitations in how GAS generates SOM relocations.
208 When linking in the HPUX C-library the HP linker has
209 the nasty habit of placing section symbols from the literal
210 subspaces in the middle of the program's text. Filter
211 those out as best we can. Check for first and last character
214 And finally, the newer HP compilers emit crud like $PIC_foo$N
215 in some circumstance (PIC code I guess). It's also claimed
216 that they emit D$ symbols too. What stupidity. */
217 if ((symname
[0] == 'L' && symname
[1] == '$')
218 || (symname
[0] == '$' && symname
[strlen (symname
) - 1] == '$')
219 || (symname
[0] == 'D' && symname
[1] == '$')
220 || (strncmp (symname
, "$PIC", 4) == 0))
227 symname
= bufp
->name
.n_strx
+ stringtab
;
228 ms_type
= mst_file_text
;
229 bufp
->symbol_value
+= text_offset
;
230 bufp
->symbol_value
= SMASH_TEXT_ADDRESS (bufp
->symbol_value
);
234 symname
= bufp
->name
.n_strx
+ stringtab
;
235 /* SS_LOCAL symbols in a shared library do not have
236 export stubs, so we do not have to worry about
237 using mst_file_text vs mst_solib_trampoline here like
238 we do for SS_UNIVERSAL and SS_EXTERNAL symbols above. */
239 ms_type
= mst_file_text
;
240 bufp
->symbol_value
+= text_offset
;
241 bufp
->symbol_value
= SMASH_TEXT_ADDRESS (bufp
->symbol_value
);
245 symname
= bufp
->name
.n_strx
+ stringtab
;
246 ms_type
= mst_solib_trampoline
;
247 bufp
->symbol_value
+= text_offset
;
248 bufp
->symbol_value
= SMASH_TEXT_ADDRESS (bufp
->symbol_value
);
253 symname
= bufp
->name
.n_strx
+ stringtab
;
254 bufp
->symbol_value
+= data_offset
;
255 ms_type
= mst_file_data
;
256 goto check_strange_names
;
263 /* This can happen for common symbols when -E is passed to the
264 final link. No idea _why_ that would make the linker force
265 common symbols to have an SS_UNSAT scope, but it does.
267 This also happens for weak symbols, but their type is
270 switch (bufp
->symbol_type
)
274 symname
= bufp
->name
.n_strx
+ stringtab
;
275 bufp
->symbol_value
+= data_offset
;
288 if (bufp
->name
.n_strx
> obj_som_stringtab_size (abfd
))
289 error ("Invalid symbol data; bad HP string table offset: %d",
292 prim_record_minimal_symbol (symname
, bufp
->symbol_value
, ms_type
,
297 /* Scan and build partial symbols for a symbol file.
298 We have been initialized by a call to som_symfile_init, which
299 currently does nothing.
301 SECTION_OFFSETS is a set of offsets to apply to relocate the symbols
302 in each section. This is ignored, as it isn't needed for SOM.
304 MAINLINE is true if we are reading the main symbol
305 table (as opposed to a shared lib or dynamically loaded file).
307 This function only does the minimum work necessary for letting the
308 user "name" things symbolically; it does not read the entire symtab.
309 Instead, it reads the external and static symbols and puts them in partial
310 symbol tables. When more extensive information is requested of a
311 file, the corresponding partial symbol table is mutated into a full
312 fledged symbol table by going back and reading the symbols
315 We look for sections with specific names, to tell us what debug
316 format to look for: FIXME!!!
318 somstab_build_psymtabs() handles STABS symbols.
320 Note that SOM files have a "minimal" symbol table, which is vaguely
321 reminiscent of a COFF symbol table, but has only the minimal information
322 necessary for linking. We process this also, and use the information to
323 build gdb's minimal symbol table. This gives us some minimal debugging
324 capability even for files compiled without -g. */
327 som_symfile_read (struct objfile
*objfile
, int mainline
)
329 bfd
*abfd
= objfile
->obfd
;
330 struct cleanup
*back_to
;
332 do_pxdb (symfile_bfd_open (objfile
->name
));
334 init_minimal_symbol_collection ();
335 back_to
= make_cleanup_discard_minimal_symbols ();
337 /* Read in the import list and the export list. Currently
338 the export list isn't used; the import list is used in
339 hp-symtab-read.c to handle static vars declared in other
341 init_import_symbols (objfile
);
342 #if 0 /* Export symbols not used today 1997-08-05 */
343 init_export_symbols (objfile
);
345 objfile
->export_list
= NULL
;
346 objfile
->export_list_size
= 0;
349 /* Process the normal SOM symbol table first.
350 This reads in the DNTT and string table, but doesn't
351 actually scan the DNTT. It does scan the linker symbol
352 table and thus build up a "minimal symbol table". */
354 som_symtab_read (abfd
, objfile
, objfile
->section_offsets
);
356 /* Install any minimal symbols that have been collected as the current
357 minimal symbols for this objfile.
358 Further symbol-reading is done incrementally, file-by-file,
359 in a step known as "psymtab-to-symtab" expansion. hp-symtab-read.c
360 contains the code to do the actual DNTT scanning and symtab building. */
361 install_minimal_symbols (objfile
);
362 do_cleanups (back_to
);
364 /* Now read information from the stabs debug sections.
365 This is a no-op for SOM.
366 Perhaps it is intended for some kind of mixed STABS/SOM
368 stabsect_build_psymtabs (objfile
, mainline
,
369 "$GDB_SYMBOLS$", "$GDB_STRINGS$", "$TEXT$");
371 /* Now read the native debug information.
372 This builds the psymtab. This used to be done via a scan of
373 the DNTT, but is now done via the PXDB-built quick-lookup tables
374 together with a scan of the GNTT. See hp-psymtab-read.c. */
375 hpread_build_psymtabs (objfile
, mainline
);
377 /* Force hppa-tdep.c to re-read the unwind descriptors. */
378 objfile
->obj_private
= NULL
;
381 /* Initialize anything that needs initializing when a completely new symbol
382 file is specified (not just adding some symbols from another file, e.g. a
385 We reinitialize buildsym, since we may be reading stabs from a SOM file. */
388 som_new_init (struct objfile
*ignore
)
390 stabsread_new_init ();
391 buildsym_new_init ();
394 /* Perform any local cleanups required when we are done with a particular
395 objfile. I.E, we are in the process of discarding all symbol information
396 for an objfile, freeing up all memory held for it, and unlinking the
397 objfile struct from the global list of known objfiles. */
400 som_symfile_finish (struct objfile
*objfile
)
402 if (objfile
->sym_stab_info
!= NULL
)
404 xmfree (objfile
->md
, objfile
->sym_stab_info
);
406 hpread_symfile_finish (objfile
);
409 /* SOM specific initialization routine for reading symbols. */
412 som_symfile_init (struct objfile
*objfile
)
414 /* SOM objects may be reordered, so set OBJF_REORDERED. If we
415 find this causes a significant slowdown in gdb then we could
416 set it in the debug symbol readers only when necessary. */
417 objfile
->flags
|= OBJF_REORDERED
;
418 hpread_symfile_init (objfile
);
421 /* SOM specific parsing routine for section offsets.
423 Plain and simple for now. */
426 som_symfile_offsets (struct objfile
*objfile
, struct section_addr_info
*addrs
)
431 objfile
->num_sections
= SECT_OFF_MAX
;
432 objfile
->section_offsets
= (struct section_offsets
*)
433 obstack_alloc (&objfile
->psymbol_obstack
, SIZEOF_SECTION_OFFSETS
);
435 /* FIXME: ezannoni 2000-04-20 The section names in SOM are not
436 .text, .data, etc, but $TEXT$, $DATA$,... We should initialize
437 SET_OFF_* from bfd. (See default_symfile_offsets()). But I don't
438 know the correspondence between SOM sections and GDB's idea of
439 section names. So for now we default to what is was before these
441 objfile
->sect_index_text
= 0;
442 objfile
->sect_index_data
= 1;
443 objfile
->sect_index_bss
= 2;
444 objfile
->sect_index_rodata
= 3;
446 /* First see if we're a shared library. If so, get the section
447 offsets from the library, else get them from addrs. */
448 if (!som_solib_section_offsets (objfile
, objfile
->section_offsets
))
450 /* Note: Here is OK to compare with ".text" because this is the
451 name that gdb itself gives to that section, not the SOM
453 for (i
= 0; i
< SECT_OFF_MAX
&& addrs
->other
[i
].name
; i
++)
454 if (strcmp (addrs
->other
[i
].name
, ".text") == 0)
456 text_addr
= addrs
->other
[i
].addr
;
458 for (i
= 0; i
< SECT_OFF_MAX
; i
++)
459 (objfile
->section_offsets
)->offsets
[i
] = text_addr
;
463 /* Read in and initialize the SOM import list which is present
464 for all executables and shared libraries. The import list
465 consists of the symbols that are referenced in OBJFILE but
466 not defined there. (Variables that are imported are dealt
467 with as "loc_indirect" vars.)
468 Return value = number of import symbols read in. */
470 init_import_symbols (struct objfile
*objfile
)
472 unsigned int import_list
;
473 unsigned int import_list_size
;
474 unsigned int string_table
;
475 unsigned int string_table_size
;
480 asection
*text_section
; /* section handle */
481 unsigned int dl_header
[12]; /* SOM executable header */
483 /* A struct for an entry in the SOM import list */
486 int name
; /* index into the string table */
487 short dont_care1
; /* we don't use this */
488 unsigned char type
; /* 0 = NULL, 2 = Data, 3 = Code, 7 = Storage, 13 = Plabel */
489 unsigned int reserved2
:8; /* not used */
493 /* We read 100 entries in at a time from the disk file. */
494 #define SOM_READ_IMPORTS_NUM 100
495 #define SOM_READ_IMPORTS_CHUNK_SIZE (sizeof (SomImportEntry) * SOM_READ_IMPORTS_NUM)
496 SomImportEntry buffer
[SOM_READ_IMPORTS_NUM
];
498 /* Initialize in case we error out */
499 objfile
->import_list
= NULL
;
500 objfile
->import_list_size
= 0;
502 /* It doesn't work, for some reason, to read in space $TEXT$;
503 the subspace $SHLIB_INFO$ has to be used. Some BFD quirk? pai/1997-08-05 */
504 text_section
= bfd_get_section_by_name (objfile
->obfd
, "$SHLIB_INFO$");
507 /* Get the SOM executable header */
508 bfd_get_section_contents (objfile
->obfd
, text_section
, dl_header
, 0, 12 * sizeof (int));
510 /* Check header version number for 10.x HP-UX */
511 /* Currently we deal only with 10.x systems; on 9.x the version # is 89060912.
512 FIXME: Change for future HP-UX releases and mods to the SOM executable format */
513 if (dl_header
[0] != 93092112)
516 import_list
= dl_header
[4];
517 import_list_size
= dl_header
[5];
518 if (!import_list_size
)
520 string_table
= dl_header
[10];
521 string_table_size
= dl_header
[11];
522 if (!string_table_size
)
525 /* Suck in SOM string table */
526 string_buffer
= (char *) xmalloc (string_table_size
);
527 bfd_get_section_contents (objfile
->obfd
, text_section
, string_buffer
,
528 string_table
, string_table_size
);
530 /* Allocate import list in the psymbol obstack; this has nothing
531 to do with psymbols, just a matter of convenience. We want the
532 import list to be freed when the objfile is deallocated */
534 = (ImportEntry
*) obstack_alloc (&objfile
->psymbol_obstack
,
535 import_list_size
* sizeof (ImportEntry
));
537 /* Read in the import entries, a bunch at a time */
539 j
< (import_list_size
/ SOM_READ_IMPORTS_NUM
);
542 bfd_get_section_contents (objfile
->obfd
, text_section
, buffer
,
543 import_list
+ j
* SOM_READ_IMPORTS_CHUNK_SIZE
,
544 SOM_READ_IMPORTS_CHUNK_SIZE
);
545 for (i
= 0; i
< SOM_READ_IMPORTS_NUM
; i
++, k
++)
547 if (buffer
[i
].type
!= (unsigned char) 0)
549 objfile
->import_list
[k
]
550 = (char *) obstack_alloc (&objfile
->psymbol_obstack
, strlen (string_buffer
+ buffer
[i
].name
) + 1);
551 strcpy (objfile
->import_list
[k
], string_buffer
+ buffer
[i
].name
);
552 /* Some day we might want to record the type and other information too */
555 objfile
->import_list
[k
] = NULL
;
560 /* Get the leftovers */
561 if (k
< import_list_size
)
562 bfd_get_section_contents (objfile
->obfd
, text_section
, buffer
,
563 import_list
+ k
* sizeof (SomImportEntry
),
564 (import_list_size
- k
) * sizeof (SomImportEntry
));
565 for (i
= 0; k
< import_list_size
; i
++, k
++)
567 if (buffer
[i
].type
!= (unsigned char) 0)
569 objfile
->import_list
[k
]
570 = (char *) obstack_alloc (&objfile
->psymbol_obstack
, strlen (string_buffer
+ buffer
[i
].name
) + 1);
571 strcpy (objfile
->import_list
[k
], string_buffer
+ buffer
[i
].name
);
572 /* Some day we might want to record the type and other information too */
575 objfile
->import_list
[k
] = NULL
;
578 objfile
->import_list_size
= import_list_size
;
579 xfree (string_buffer
);
580 return import_list_size
;
583 /* Read in and initialize the SOM export list which is present
584 for all executables and shared libraries. The import list
585 consists of the symbols that are referenced in OBJFILE but
586 not defined there. (Variables that are imported are dealt
587 with as "loc_indirect" vars.)
588 Return value = number of import symbols read in. */
590 init_export_symbols (struct objfile
*objfile
)
592 unsigned int export_list
;
593 unsigned int export_list_size
;
594 unsigned int string_table
;
595 unsigned int string_table_size
;
600 asection
*text_section
; /* section handle */
601 unsigned int dl_header
[12]; /* SOM executable header */
603 /* A struct for an entry in the SOM export list */
606 int next
; /* for hash table use -- we don't use this */
607 int name
; /* index into string table */
608 int value
; /* offset or plabel */
609 int dont_care1
; /* not used */
610 unsigned char type
; /* 0 = NULL, 2 = Data, 3 = Code, 7 = Storage, 13 = Plabel */
611 char dont_care2
; /* not used */
612 short dont_care3
; /* not used */
616 /* We read 100 entries in at a time from the disk file. */
617 #define SOM_READ_EXPORTS_NUM 100
618 #define SOM_READ_EXPORTS_CHUNK_SIZE (sizeof (SomExportEntry) * SOM_READ_EXPORTS_NUM)
619 SomExportEntry buffer
[SOM_READ_EXPORTS_NUM
];
621 /* Initialize in case we error out */
622 objfile
->export_list
= NULL
;
623 objfile
->export_list_size
= 0;
625 /* It doesn't work, for some reason, to read in space $TEXT$;
626 the subspace $SHLIB_INFO$ has to be used. Some BFD quirk? pai/1997-08-05 */
627 text_section
= bfd_get_section_by_name (objfile
->obfd
, "$SHLIB_INFO$");
630 /* Get the SOM executable header */
631 bfd_get_section_contents (objfile
->obfd
, text_section
, dl_header
, 0, 12 * sizeof (int));
633 /* Check header version number for 10.x HP-UX */
634 /* Currently we deal only with 10.x systems; on 9.x the version # is 89060912.
635 FIXME: Change for future HP-UX releases and mods to the SOM executable format */
636 if (dl_header
[0] != 93092112)
639 export_list
= dl_header
[8];
640 export_list_size
= dl_header
[9];
641 if (!export_list_size
)
643 string_table
= dl_header
[10];
644 string_table_size
= dl_header
[11];
645 if (!string_table_size
)
648 /* Suck in SOM string table */
649 string_buffer
= (char *) xmalloc (string_table_size
);
650 bfd_get_section_contents (objfile
->obfd
, text_section
, string_buffer
,
651 string_table
, string_table_size
);
653 /* Allocate export list in the psymbol obstack; this has nothing
654 to do with psymbols, just a matter of convenience. We want the
655 export list to be freed when the objfile is deallocated */
657 = (ExportEntry
*) obstack_alloc (&objfile
->psymbol_obstack
,
658 export_list_size
* sizeof (ExportEntry
));
660 /* Read in the export entries, a bunch at a time */
662 j
< (export_list_size
/ SOM_READ_EXPORTS_NUM
);
665 bfd_get_section_contents (objfile
->obfd
, text_section
, buffer
,
666 export_list
+ j
* SOM_READ_EXPORTS_CHUNK_SIZE
,
667 SOM_READ_EXPORTS_CHUNK_SIZE
);
668 for (i
= 0; i
< SOM_READ_EXPORTS_NUM
; i
++, k
++)
670 if (buffer
[i
].type
!= (unsigned char) 0)
672 objfile
->export_list
[k
].name
673 = (char *) obstack_alloc (&objfile
->psymbol_obstack
, strlen (string_buffer
+ buffer
[i
].name
) + 1);
674 strcpy (objfile
->export_list
[k
].name
, string_buffer
+ buffer
[i
].name
);
675 objfile
->export_list
[k
].address
= buffer
[i
].value
;
676 /* Some day we might want to record the type and other information too */
681 objfile
->export_list
[k
].name
= NULL
;
682 objfile
->export_list
[k
].address
= 0;
687 /* Get the leftovers */
688 if (k
< export_list_size
)
689 bfd_get_section_contents (objfile
->obfd
, text_section
, buffer
,
690 export_list
+ k
* sizeof (SomExportEntry
),
691 (export_list_size
- k
) * sizeof (SomExportEntry
));
692 for (i
= 0; k
< export_list_size
; i
++, k
++)
694 if (buffer
[i
].type
!= (unsigned char) 0)
696 objfile
->export_list
[k
].name
697 = (char *) obstack_alloc (&objfile
->psymbol_obstack
, strlen (string_buffer
+ buffer
[i
].name
) + 1);
698 strcpy (objfile
->export_list
[k
].name
, string_buffer
+ buffer
[i
].name
);
699 /* Some day we might want to record the type and other information too */
700 objfile
->export_list
[k
].address
= buffer
[i
].value
;
704 objfile
->export_list
[k
].name
= NULL
;
705 objfile
->export_list
[k
].address
= 0;
709 objfile
->export_list_size
= export_list_size
;
710 xfree (string_buffer
);
711 return export_list_size
;
716 /* Register that we are able to handle SOM object file formats. */
718 static struct sym_fns som_sym_fns
=
720 bfd_target_som_flavour
,
721 som_new_init
, /* sym_new_init: init anything gbl to entire symtab */
722 som_symfile_init
, /* sym_init: read initial info, setup for sym_read() */
723 som_symfile_read
, /* sym_read: read a symbol file into symtab */
724 som_symfile_finish
, /* sym_finish: finished with file, cleanup */
725 som_symfile_offsets
, /* sym_offsets: Translate ext. to int. relocation */
726 NULL
/* next: pointer to next struct sym_fns */
730 _initialize_somread (void)
732 add_symtab_fns (&som_sym_fns
);