1 /* Read HP PA/Risc object files for GDB.
2 Copyright (C) 1991, 1992, 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002,
3 2004 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., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
30 #include "stabsread.h"
31 #include "gdb-stabs.h"
32 #include "complaints.h"
33 #include "gdb_string.h"
38 #include "solib-som.h"
40 /* Prototypes for local functions. */
41 static int init_import_symbols (struct objfile
*objfile
);
43 /* FIXME: These should really be in a common header somewhere */
45 extern void hpread_build_psymtabs (struct objfile
*, int);
47 extern void hpread_symfile_finish (struct objfile
*);
49 extern void hpread_symfile_init (struct objfile
*);
51 extern void do_pxdb (bfd
*);
57 som_symtab_read -- read the symbol table of a SOM file
61 void som_symtab_read (bfd *abfd, struct objfile *objfile,
62 struct section_offsets *section_offsets)
66 Given an open bfd, a base address to relocate symbols to, and a
67 flag that specifies whether or not this bfd is for an executable
68 or not (may be shared library for example), add all the global
69 function and data symbols to the minimal symbol table.
73 som_symtab_read (bfd
*abfd
, struct objfile
*objfile
,
74 struct section_offsets
*section_offsets
)
76 unsigned int number_of_symbols
;
80 struct symbol_dictionary_record
*buf
, *bufp
, *endbufp
;
82 CONST
int symsize
= sizeof (struct symbol_dictionary_record
);
83 CORE_ADDR text_offset
, data_offset
;
86 text_offset
= ANOFFSET (section_offsets
, 0);
87 data_offset
= ANOFFSET (section_offsets
, 1);
89 number_of_symbols
= bfd_get_symcount (abfd
);
91 /* FIXME (alloca): could be quite large. */
92 buf
= alloca (symsize
* number_of_symbols
);
93 bfd_seek (abfd
, obj_som_sym_filepos (abfd
), SEEK_SET
);
94 val
= bfd_bread (buf
, symsize
* number_of_symbols
, abfd
);
95 if (val
!= symsize
* number_of_symbols
)
96 error (_("Couldn't read symbol dictionary!"));
98 /* FIXME (alloca): could be quite large. */
99 stringtab
= alloca (obj_som_stringtab_size (abfd
));
100 bfd_seek (abfd
, obj_som_str_filepos (abfd
), SEEK_SET
);
101 val
= bfd_bread (stringtab
, obj_som_stringtab_size (abfd
), abfd
);
102 if (val
!= obj_som_stringtab_size (abfd
))
103 error (_("Can't read in HP string table."));
105 /* We need to determine if objfile is a dynamic executable (so we
106 can do the right thing for ST_ENTRY vs ST_CODE symbols).
108 There's nothing in the header which easily allows us to do
111 This code used to rely upon the existence of a $SHLIB_INFO$
112 section to make this determination. HP claims that it is
113 more accurate to check for a nonzero text offset, but they
114 have not provided any information about why that test is
116 dynamic
= (text_offset
!= 0);
118 endbufp
= buf
+ number_of_symbols
;
119 for (bufp
= buf
; bufp
< endbufp
; ++bufp
)
121 enum minimal_symbol_type ms_type
;
125 switch (bufp
->symbol_scope
)
129 switch (bufp
->symbol_type
)
139 symname
= bufp
->name
.n_strx
+ stringtab
;
141 bufp
->symbol_value
+= text_offset
;
142 bufp
->symbol_value
= SMASH_TEXT_ADDRESS (bufp
->symbol_value
);
146 symname
= bufp
->name
.n_strx
+ stringtab
;
147 /* For a dynamic executable, ST_ENTRY symbols are
148 the stubs, while the ST_CODE symbol is the real
151 ms_type
= mst_solib_trampoline
;
154 bufp
->symbol_value
+= text_offset
;
155 bufp
->symbol_value
= SMASH_TEXT_ADDRESS (bufp
->symbol_value
);
159 symname
= bufp
->name
.n_strx
+ stringtab
;
160 ms_type
= mst_solib_trampoline
;
161 bufp
->symbol_value
+= text_offset
;
162 bufp
->symbol_value
= SMASH_TEXT_ADDRESS (bufp
->symbol_value
);
166 symname
= bufp
->name
.n_strx
+ stringtab
;
167 bufp
->symbol_value
+= data_offset
;
176 /* SS_GLOBAL and SS_LOCAL are two names for the same thing (!). */
180 switch (bufp
->symbol_type
)
187 symname
= bufp
->name
.n_strx
+ stringtab
;
188 ms_type
= mst_file_text
;
189 bufp
->symbol_value
+= text_offset
;
190 bufp
->symbol_value
= SMASH_TEXT_ADDRESS (bufp
->symbol_value
);
193 /* Utah GCC 2.5, FSF GCC 2.6 and later generate correct local
194 label prefixes for stabs, constant data, etc. So we need
195 only filter out L$ symbols which are left in due to
196 limitations in how GAS generates SOM relocations.
198 When linking in the HPUX C-library the HP linker has
199 the nasty habit of placing section symbols from the literal
200 subspaces in the middle of the program's text. Filter
201 those out as best we can. Check for first and last character
204 And finally, the newer HP compilers emit crud like $PIC_foo$N
205 in some circumstance (PIC code I guess). It's also claimed
206 that they emit D$ symbols too. What stupidity. */
207 if ((symname
[0] == 'L' && symname
[1] == '$')
208 || (symname
[0] == '$' && symname
[strlen (symname
) - 1] == '$')
209 || (symname
[0] == 'D' && symname
[1] == '$')
210 || (strncmp (symname
, "L0\001", 3) == 0)
211 || (strncmp (symname
, "$PIC", 4) == 0))
218 symname
= bufp
->name
.n_strx
+ stringtab
;
219 ms_type
= mst_file_text
;
220 bufp
->symbol_value
+= text_offset
;
221 bufp
->symbol_value
= SMASH_TEXT_ADDRESS (bufp
->symbol_value
);
225 symname
= bufp
->name
.n_strx
+ stringtab
;
226 /* SS_LOCAL symbols in a shared library do not have
227 export stubs, so we do not have to worry about
228 using mst_file_text vs mst_solib_trampoline here like
229 we do for SS_UNIVERSAL and SS_EXTERNAL symbols above. */
230 ms_type
= mst_file_text
;
231 bufp
->symbol_value
+= text_offset
;
232 bufp
->symbol_value
= SMASH_TEXT_ADDRESS (bufp
->symbol_value
);
236 symname
= bufp
->name
.n_strx
+ stringtab
;
237 ms_type
= mst_solib_trampoline
;
238 bufp
->symbol_value
+= text_offset
;
239 bufp
->symbol_value
= SMASH_TEXT_ADDRESS (bufp
->symbol_value
);
244 symname
= bufp
->name
.n_strx
+ stringtab
;
245 bufp
->symbol_value
+= data_offset
;
246 ms_type
= mst_file_data
;
247 goto check_strange_names
;
254 /* This can happen for common symbols when -E is passed to the
255 final link. No idea _why_ that would make the linker force
256 common symbols to have an SS_UNSAT scope, but it does.
258 This also happens for weak symbols, but their type is
261 switch (bufp
->symbol_type
)
265 symname
= bufp
->name
.n_strx
+ stringtab
;
266 bufp
->symbol_value
+= data_offset
;
279 if (bufp
->name
.n_strx
> obj_som_stringtab_size (abfd
))
280 error (_("Invalid symbol data; bad HP string table offset: %d"),
283 prim_record_minimal_symbol (symname
, bufp
->symbol_value
, ms_type
,
288 /* Scan and build partial symbols for a symbol file.
289 We have been initialized by a call to som_symfile_init, which
290 currently does nothing.
292 SECTION_OFFSETS is a set of offsets to apply to relocate the symbols
293 in each section. This is ignored, as it isn't needed for SOM.
295 MAINLINE is true if we are reading the main symbol
296 table (as opposed to a shared lib or dynamically loaded file).
298 This function only does the minimum work necessary for letting the
299 user "name" things symbolically; it does not read the entire symtab.
300 Instead, it reads the external and static symbols and puts them in partial
301 symbol tables. When more extensive information is requested of a
302 file, the corresponding partial symbol table is mutated into a full
303 fledged symbol table by going back and reading the symbols
306 We look for sections with specific names, to tell us what debug
307 format to look for: FIXME!!!
309 somstab_build_psymtabs() handles STABS symbols.
311 Note that SOM files have a "minimal" symbol table, which is vaguely
312 reminiscent of a COFF symbol table, but has only the minimal information
313 necessary for linking. We process this also, and use the information to
314 build gdb's minimal symbol table. This gives us some minimal debugging
315 capability even for files compiled without -g. */
318 som_symfile_read (struct objfile
*objfile
, int mainline
)
320 bfd
*abfd
= objfile
->obfd
;
321 struct cleanup
*back_to
;
323 do_pxdb (symfile_bfd_open (objfile
->name
));
325 init_minimal_symbol_collection ();
326 back_to
= make_cleanup_discard_minimal_symbols ();
328 /* Read in the import list and the export list. Currently
329 the export list isn't used; the import list is used in
330 hp-symtab-read.c to handle static vars declared in other
332 init_import_symbols (objfile
);
333 #if 0 /* Export symbols not used today 1997-08-05 */
334 init_export_symbols (objfile
);
336 objfile
->export_list
= NULL
;
337 objfile
->export_list_size
= 0;
340 /* Process the normal SOM symbol table first.
341 This reads in the DNTT and string table, but doesn't
342 actually scan the DNTT. It does scan the linker symbol
343 table and thus build up a "minimal symbol table". */
345 som_symtab_read (abfd
, objfile
, objfile
->section_offsets
);
347 /* Install any minimal symbols that have been collected as the current
348 minimal symbols for this objfile.
349 Further symbol-reading is done incrementally, file-by-file,
350 in a step known as "psymtab-to-symtab" expansion. hp-symtab-read.c
351 contains the code to do the actual DNTT scanning and symtab building. */
352 install_minimal_symbols (objfile
);
353 do_cleanups (back_to
);
355 /* Now read information from the stabs debug sections.
356 This is a no-op for SOM.
357 Perhaps it is intended for some kind of mixed STABS/SOM
359 stabsect_build_psymtabs (objfile
, mainline
,
360 "$GDB_SYMBOLS$", "$GDB_STRINGS$", "$TEXT$");
362 /* Now read the native debug information.
363 This builds the psymtab. This used to be done via a scan of
364 the DNTT, but is now done via the PXDB-built quick-lookup tables
365 together with a scan of the GNTT. See hp-psymtab-read.c. */
366 hpread_build_psymtabs (objfile
, mainline
);
368 /* Force hppa-tdep.c to re-read the unwind descriptors. */
369 objfile
->deprecated_obj_private
= NULL
;
372 /* Initialize anything that needs initializing when a completely new symbol
373 file is specified (not just adding some symbols from another file, e.g. a
376 We reinitialize buildsym, since we may be reading stabs from a SOM file. */
379 som_new_init (struct objfile
*ignore
)
381 stabsread_new_init ();
382 buildsym_new_init ();
385 /* Perform any local cleanups required when we are done with a particular
386 objfile. I.E, we are in the process of discarding all symbol information
387 for an objfile, freeing up all memory held for it, and unlinking the
388 objfile struct from the global list of known objfiles. */
391 som_symfile_finish (struct objfile
*objfile
)
393 if (objfile
->deprecated_sym_stab_info
!= NULL
)
395 xfree (objfile
->deprecated_sym_stab_info
);
397 hpread_symfile_finish (objfile
);
400 /* SOM specific initialization routine for reading symbols. */
403 som_symfile_init (struct objfile
*objfile
)
405 /* SOM objects may be reordered, so set OBJF_REORDERED. If we
406 find this causes a significant slowdown in gdb then we could
407 set it in the debug symbol readers only when necessary. */
408 objfile
->flags
|= OBJF_REORDERED
;
409 hpread_symfile_init (objfile
);
412 /* SOM specific parsing routine for section offsets.
414 Plain and simple for now. */
417 som_symfile_offsets (struct objfile
*objfile
, struct section_addr_info
*addrs
)
422 objfile
->num_sections
= bfd_count_sections (objfile
->obfd
);
423 objfile
->section_offsets
= (struct section_offsets
*)
424 obstack_alloc (&objfile
->objfile_obstack
,
425 SIZEOF_N_SECTION_OFFSETS (objfile
->num_sections
));
427 /* FIXME: ezannoni 2000-04-20 The section names in SOM are not
428 .text, .data, etc, but $TEXT$, $DATA$,... We should initialize
429 SET_OFF_* from bfd. (See default_symfile_offsets()). But I don't
430 know the correspondence between SOM sections and GDB's idea of
431 section names. So for now we default to what is was before these
433 objfile
->sect_index_text
= 0;
434 objfile
->sect_index_data
= 1;
435 objfile
->sect_index_bss
= 2;
436 objfile
->sect_index_rodata
= 3;
438 /* First see if we're a shared library. If so, get the section
439 offsets from the library, else get them from addrs. */
440 if (!som_solib_section_offsets (objfile
, objfile
->section_offsets
))
442 /* Note: Here is OK to compare with ".text" because this is the
443 name that gdb itself gives to that section, not the SOM
445 for (i
= 0; i
< addrs
->num_sections
&& addrs
->other
[i
].name
; i
++)
446 if (strcmp (addrs
->other
[i
].name
, ".text") == 0)
448 text_addr
= addrs
->other
[i
].addr
;
450 for (i
= 0; i
< objfile
->num_sections
; i
++)
451 (objfile
->section_offsets
)->offsets
[i
] = text_addr
;
455 /* Read in and initialize the SOM import list which is present
456 for all executables and shared libraries. The import list
457 consists of the symbols that are referenced in OBJFILE but
458 not defined there. (Variables that are imported are dealt
459 with as "loc_indirect" vars.)
460 Return value = number of import symbols read in. */
462 init_import_symbols (struct objfile
*objfile
)
464 unsigned int import_list
;
465 unsigned int import_list_size
;
466 unsigned int string_table
;
467 unsigned int string_table_size
;
472 asection
*text_section
; /* section handle */
473 unsigned int dl_header
[12]; /* SOM executable header */
475 /* A struct for an entry in the SOM import list */
478 int name
; /* index into the string table */
479 short dont_care1
; /* we don't use this */
480 unsigned char type
; /* 0 = NULL, 2 = Data, 3 = Code, 7 = Storage, 13 = Plabel */
481 unsigned int reserved2
:8; /* not used */
485 /* We read 100 entries in at a time from the disk file. */
486 #define SOM_READ_IMPORTS_NUM 100
487 #define SOM_READ_IMPORTS_CHUNK_SIZE (sizeof (SomImportEntry) * SOM_READ_IMPORTS_NUM)
488 SomImportEntry buffer
[SOM_READ_IMPORTS_NUM
];
490 /* Initialize in case we error out */
491 objfile
->import_list
= NULL
;
492 objfile
->import_list_size
= 0;
494 /* It doesn't work, for some reason, to read in space $TEXT$;
495 the subspace $SHLIB_INFO$ has to be used. Some BFD quirk? pai/1997-08-05 */
496 text_section
= bfd_get_section_by_name (objfile
->obfd
, "$SHLIB_INFO$");
499 /* Get the SOM executable header */
500 bfd_get_section_contents (objfile
->obfd
, text_section
, dl_header
, 0, 12 * sizeof (int));
502 /* Check header version number for 10.x HP-UX */
503 /* Currently we deal only with 10.x systems; on 9.x the version # is 89060912.
504 FIXME: Change for future HP-UX releases and mods to the SOM executable format */
505 if (dl_header
[0] != 93092112)
508 import_list
= dl_header
[4];
509 import_list_size
= dl_header
[5];
510 if (!import_list_size
)
512 string_table
= dl_header
[10];
513 string_table_size
= dl_header
[11];
514 if (!string_table_size
)
517 /* Suck in SOM string table */
518 string_buffer
= (char *) xmalloc (string_table_size
);
519 bfd_get_section_contents (objfile
->obfd
, text_section
, string_buffer
,
520 string_table
, string_table_size
);
522 /* Allocate import list in the psymbol obstack; this has nothing
523 to do with psymbols, just a matter of convenience. We want the
524 import list to be freed when the objfile is deallocated */
526 = (ImportEntry
*) obstack_alloc (&objfile
->objfile_obstack
,
527 import_list_size
* sizeof (ImportEntry
));
529 /* Read in the import entries, a bunch at a time */
531 j
< (import_list_size
/ SOM_READ_IMPORTS_NUM
);
534 bfd_get_section_contents (objfile
->obfd
, text_section
, buffer
,
535 import_list
+ j
* SOM_READ_IMPORTS_CHUNK_SIZE
,
536 SOM_READ_IMPORTS_CHUNK_SIZE
);
537 for (i
= 0; i
< SOM_READ_IMPORTS_NUM
; i
++, k
++)
539 if (buffer
[i
].type
!= (unsigned char) 0)
541 objfile
->import_list
[k
]
542 = (char *) obstack_alloc (&objfile
->objfile_obstack
, strlen (string_buffer
+ buffer
[i
].name
) + 1);
543 strcpy (objfile
->import_list
[k
], string_buffer
+ buffer
[i
].name
);
544 /* Some day we might want to record the type and other information too */
547 objfile
->import_list
[k
] = NULL
;
552 /* Get the leftovers */
553 if (k
< import_list_size
)
554 bfd_get_section_contents (objfile
->obfd
, text_section
, buffer
,
555 import_list
+ k
* sizeof (SomImportEntry
),
556 (import_list_size
- k
) * sizeof (SomImportEntry
));
557 for (i
= 0; k
< import_list_size
; i
++, k
++)
559 if (buffer
[i
].type
!= (unsigned char) 0)
561 objfile
->import_list
[k
]
562 = (char *) obstack_alloc (&objfile
->objfile_obstack
, strlen (string_buffer
+ buffer
[i
].name
) + 1);
563 strcpy (objfile
->import_list
[k
], string_buffer
+ buffer
[i
].name
);
564 /* Some day we might want to record the type and other information too */
567 objfile
->import_list
[k
] = NULL
;
570 objfile
->import_list_size
= import_list_size
;
571 xfree (string_buffer
);
572 return import_list_size
;
575 /* Read in and initialize the SOM export list which is present
576 for all executables and shared libraries. The import list
577 consists of the symbols that are referenced in OBJFILE but
578 not defined there. (Variables that are imported are dealt
579 with as "loc_indirect" vars.)
580 Return value = number of import symbols read in. */
582 init_export_symbols (struct objfile
*objfile
)
584 unsigned int export_list
;
585 unsigned int export_list_size
;
586 unsigned int string_table
;
587 unsigned int string_table_size
;
592 asection
*text_section
; /* section handle */
593 unsigned int dl_header
[12]; /* SOM executable header */
595 /* A struct for an entry in the SOM export list */
598 int next
; /* for hash table use -- we don't use this */
599 int name
; /* index into string table */
600 int value
; /* offset or plabel */
601 int dont_care1
; /* not used */
602 unsigned char type
; /* 0 = NULL, 2 = Data, 3 = Code, 7 = Storage, 13 = Plabel */
603 char dont_care2
; /* not used */
604 short dont_care3
; /* not used */
608 /* We read 100 entries in at a time from the disk file. */
609 #define SOM_READ_EXPORTS_NUM 100
610 #define SOM_READ_EXPORTS_CHUNK_SIZE (sizeof (SomExportEntry) * SOM_READ_EXPORTS_NUM)
611 SomExportEntry buffer
[SOM_READ_EXPORTS_NUM
];
613 /* Initialize in case we error out */
614 objfile
->export_list
= NULL
;
615 objfile
->export_list_size
= 0;
617 /* It doesn't work, for some reason, to read in space $TEXT$;
618 the subspace $SHLIB_INFO$ has to be used. Some BFD quirk? pai/1997-08-05 */
619 text_section
= bfd_get_section_by_name (objfile
->obfd
, "$SHLIB_INFO$");
622 /* Get the SOM executable header */
623 bfd_get_section_contents (objfile
->obfd
, text_section
, dl_header
, 0, 12 * sizeof (int));
625 /* Check header version number for 10.x HP-UX */
626 /* Currently we deal only with 10.x systems; on 9.x the version # is 89060912.
627 FIXME: Change for future HP-UX releases and mods to the SOM executable format */
628 if (dl_header
[0] != 93092112)
631 export_list
= dl_header
[8];
632 export_list_size
= dl_header
[9];
633 if (!export_list_size
)
635 string_table
= dl_header
[10];
636 string_table_size
= dl_header
[11];
637 if (!string_table_size
)
640 /* Suck in SOM string table */
641 string_buffer
= (char *) xmalloc (string_table_size
);
642 bfd_get_section_contents (objfile
->obfd
, text_section
, string_buffer
,
643 string_table
, string_table_size
);
645 /* Allocate export list in the psymbol obstack; this has nothing
646 to do with psymbols, just a matter of convenience. We want the
647 export list to be freed when the objfile is deallocated */
649 = (ExportEntry
*) obstack_alloc (&objfile
->objfile_obstack
,
650 export_list_size
* sizeof (ExportEntry
));
652 /* Read in the export entries, a bunch at a time */
654 j
< (export_list_size
/ SOM_READ_EXPORTS_NUM
);
657 bfd_get_section_contents (objfile
->obfd
, text_section
, buffer
,
658 export_list
+ j
* SOM_READ_EXPORTS_CHUNK_SIZE
,
659 SOM_READ_EXPORTS_CHUNK_SIZE
);
660 for (i
= 0; i
< SOM_READ_EXPORTS_NUM
; i
++, k
++)
662 if (buffer
[i
].type
!= (unsigned char) 0)
664 objfile
->export_list
[k
].name
665 = (char *) obstack_alloc (&objfile
->objfile_obstack
, strlen (string_buffer
+ buffer
[i
].name
) + 1);
666 strcpy (objfile
->export_list
[k
].name
, string_buffer
+ buffer
[i
].name
);
667 objfile
->export_list
[k
].address
= buffer
[i
].value
;
668 /* Some day we might want to record the type and other information too */
673 objfile
->export_list
[k
].name
= NULL
;
674 objfile
->export_list
[k
].address
= 0;
679 /* Get the leftovers */
680 if (k
< export_list_size
)
681 bfd_get_section_contents (objfile
->obfd
, text_section
, buffer
,
682 export_list
+ k
* sizeof (SomExportEntry
),
683 (export_list_size
- k
) * sizeof (SomExportEntry
));
684 for (i
= 0; k
< export_list_size
; i
++, k
++)
686 if (buffer
[i
].type
!= (unsigned char) 0)
688 objfile
->export_list
[k
].name
689 = (char *) obstack_alloc (&objfile
->objfile_obstack
, strlen (string_buffer
+ buffer
[i
].name
) + 1);
690 strcpy (objfile
->export_list
[k
].name
, string_buffer
+ buffer
[i
].name
);
691 /* Some day we might want to record the type and other information too */
692 objfile
->export_list
[k
].address
= buffer
[i
].value
;
696 objfile
->export_list
[k
].name
= NULL
;
697 objfile
->export_list
[k
].address
= 0;
701 objfile
->export_list_size
= export_list_size
;
702 xfree (string_buffer
);
703 return export_list_size
;
708 /* Register that we are able to handle SOM object file formats. */
710 static struct sym_fns som_sym_fns
=
712 bfd_target_som_flavour
,
713 som_new_init
, /* sym_new_init: init anything gbl to entire symtab */
714 som_symfile_init
, /* sym_init: read initial info, setup for sym_read() */
715 som_symfile_read
, /* sym_read: read a symbol file into symtab */
716 som_symfile_finish
, /* sym_finish: finished with file, cleanup */
717 som_symfile_offsets
, /* sym_offsets: Translate ext. to int. relocation */
718 NULL
/* next: pointer to next struct sym_fns */
722 _initialize_somread (void)
724 add_symtab_fns (&som_sym_fns
);