1 /* Read HP PA/Risc object files for GDB.
2 Copyright (C) 1991-2014 Free Software Foundation, Inc.
3 Written by Fred Fish at Cygnus Support.
5 This file is part of GDB.
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 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #include "stabsread.h"
28 #include "gdb-stabs.h"
29 #include "complaints.h"
36 #include "solib-som.h"
38 /* Read the symbol table of a SOM file.
40 Given an open bfd, a base address to relocate symbols to, and a
41 flag that specifies whether or not this bfd is for an executable
42 or not (may be shared library for example), add all the global
43 function and data symbols to the minimal symbol table. */
46 som_symtab_read (bfd
*abfd
, struct objfile
*objfile
,
47 struct section_offsets
*section_offsets
)
49 struct cleanup
*cleanup
;
50 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
51 unsigned int number_of_symbols
;
55 struct som_external_symbol_dictionary_record
*buf
, *bufp
, *endbufp
;
57 const int symsize
= sizeof (struct som_external_symbol_dictionary_record
);
60 number_of_symbols
= bfd_get_symcount (abfd
);
62 /* Allocate a buffer to read in the debug info.
63 We avoid using alloca because the memory size could be so large
64 that we could hit the stack size limit. */
65 buf
= xmalloc (symsize
* number_of_symbols
);
66 cleanup
= make_cleanup (xfree
, buf
);
67 bfd_seek (abfd
, obj_som_sym_filepos (abfd
), SEEK_SET
);
68 val
= bfd_bread (buf
, symsize
* number_of_symbols
, abfd
);
69 if (val
!= symsize
* number_of_symbols
)
70 error (_("Couldn't read symbol dictionary!"));
72 /* Allocate a buffer to read in the som stringtab section of
73 the debugging info. Again, we avoid using alloca because
74 the data could be so large that we could potentially hit
75 the stack size limitat. */
76 stringtab
= xmalloc (obj_som_stringtab_size (abfd
));
77 make_cleanup (xfree
, stringtab
);
78 bfd_seek (abfd
, obj_som_str_filepos (abfd
), SEEK_SET
);
79 val
= bfd_bread (stringtab
, obj_som_stringtab_size (abfd
), abfd
);
80 if (val
!= obj_som_stringtab_size (abfd
))
81 error (_("Can't read in HP string table."));
83 /* We need to determine if objfile is a dynamic executable (so we
84 can do the right thing for ST_ENTRY vs ST_CODE symbols).
86 There's nothing in the header which easily allows us to do
89 This code used to rely upon the existence of a $SHLIB_INFO$
90 section to make this determination. HP claims that it is
91 more accurate to check for a nonzero text offset, but they
92 have not provided any information about why that test is
94 dynamic
= (ANOFFSET (section_offsets
, SECT_OFF_TEXT (objfile
)) != 0);
96 endbufp
= buf
+ number_of_symbols
;
97 for (bufp
= buf
; bufp
< endbufp
; ++bufp
)
99 enum minimal_symbol_type ms_type
;
100 unsigned int flags
= bfd_getb32 (bufp
->flags
);
101 unsigned int symbol_type
102 = (flags
>> SOM_SYMBOL_TYPE_SH
) & SOM_SYMBOL_TYPE_MASK
;
103 unsigned int symbol_scope
104 = (flags
>> SOM_SYMBOL_SCOPE_SH
) & SOM_SYMBOL_SCOPE_MASK
;
105 CORE_ADDR symbol_value
= bfd_getb32 (bufp
->symbol_value
);
106 asection
*section
= NULL
;
110 /* Compute the section. */
111 switch (symbol_scope
)
114 if (symbol_type
!= ST_STORAGE
)
115 section
= bfd_und_section_ptr
;
117 section
= bfd_com_section_ptr
;
121 if (symbol_type
!= ST_STORAGE
)
122 section
= bfd_und_section_ptr
;
124 section
= bfd_com_section_ptr
;
128 section
= bfd_section_from_som_symbol (abfd
, bufp
);
132 section
= bfd_section_from_som_symbol (abfd
, bufp
);
136 switch (symbol_scope
)
150 symname
= bfd_getb32 (bufp
->name
) + stringtab
;
152 symbol_value
= gdbarch_addr_bits_remove (gdbarch
, symbol_value
);
156 symname
= bfd_getb32 (bufp
->name
) + 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 symbol_value
= gdbarch_addr_bits_remove (gdbarch
, symbol_value
);
168 symname
= bfd_getb32 (bufp
->name
) + stringtab
;
169 ms_type
= mst_solib_trampoline
;
170 symbol_value
= gdbarch_addr_bits_remove (gdbarch
, symbol_value
);
174 symname
= bfd_getb32 (bufp
->name
) + stringtab
;
183 /* SS_GLOBAL and SS_LOCAL are two names for the same thing (!). */
194 symname
= bfd_getb32 (bufp
->name
) + stringtab
;
195 ms_type
= mst_file_text
;
196 symbol_value
= gdbarch_addr_bits_remove (gdbarch
, symbol_value
);
199 /* Utah GCC 2.5, FSF GCC 2.6 and later generate correct local
200 label prefixes for stabs, constant data, etc. So we need
201 only filter out L$ symbols which are left in due to
202 limitations in how GAS generates SOM relocations.
204 When linking in the HPUX C-library the HP linker has
205 the nasty habit of placing section symbols from the literal
206 subspaces in the middle of the program's text. Filter
207 those out as best we can. Check for first and last character
210 And finally, the newer HP compilers emit crud like $PIC_foo$N
211 in some circumstance (PIC code I guess). It's also claimed
212 that they emit D$ symbols too. What stupidity. */
213 if ((symname
[0] == 'L' && symname
[1] == '$')
214 || (symname
[0] == '$' && symname
[strlen (symname
) - 1] == '$')
215 || (symname
[0] == 'D' && symname
[1] == '$')
216 || (strncmp (symname
, "L0\001", 3) == 0)
217 || (strncmp (symname
, "$PIC", 4) == 0))
224 symname
= bfd_getb32 (bufp
->name
) + stringtab
;
225 ms_type
= mst_file_text
;
226 symbol_value
= gdbarch_addr_bits_remove (gdbarch
, symbol_value
);
230 symname
= bfd_getb32 (bufp
->name
) + stringtab
;
231 /* SS_LOCAL symbols in a shared library do not have
232 export stubs, so we do not have to worry about
233 using mst_file_text vs mst_solib_trampoline here like
234 we do for SS_UNIVERSAL and SS_EXTERNAL symbols above. */
235 ms_type
= mst_file_text
;
236 symbol_value
= gdbarch_addr_bits_remove (gdbarch
, symbol_value
);
240 symname
= bfd_getb32 (bufp
->name
) + stringtab
;
241 ms_type
= mst_solib_trampoline
;
242 symbol_value
= gdbarch_addr_bits_remove (gdbarch
, symbol_value
);
247 symname
= bfd_getb32 (bufp
->name
) + stringtab
;
248 ms_type
= mst_file_data
;
249 goto check_strange_names
;
256 /* This can happen for common symbols when -E is passed to the
257 final link. No idea _why_ that would make the linker force
258 common symbols to have an SS_UNSAT scope, but it does.
260 This also happens for weak symbols, but their type is
267 symname
= bfd_getb32 (bufp
->name
) + stringtab
;
280 if (bfd_getb32 (bufp
->name
) > obj_som_stringtab_size (abfd
))
281 error (_("Invalid symbol data; bad HP string table offset: %s"),
282 plongest (bfd_getb32 (bufp
->name
)));
284 if (bfd_is_const_section (section
))
286 struct obj_section
*iter
;
288 ALL_OBJFILE_OSECTIONS (objfile
, iter
)
293 if (bfd_is_const_section (iter
->the_bfd_section
))
296 start
= bfd_get_section_vma (iter
->objfile
->obfd
,
297 iter
->the_bfd_section
);
298 len
= bfd_get_section_size (iter
->the_bfd_section
);
299 if (start
<= symbol_value
&& symbol_value
< start
+ len
)
301 section
= iter
->the_bfd_section
;
307 prim_record_minimal_symbol_and_info (symname
, symbol_value
, ms_type
,
308 gdb_bfd_section_index (objfile
->obfd
,
313 do_cleanups (cleanup
);
316 /* Scan and build partial symbols for a symbol file.
317 We have been initialized by a call to som_symfile_init, which
318 currently does nothing.
320 SECTION_OFFSETS is a set of offsets to apply to relocate the symbols
321 in each section. This is ignored, as it isn't needed for SOM.
323 This function only does the minimum work necessary for letting the
324 user "name" things symbolically; it does not read the entire symtab.
325 Instead, it reads the external and static symbols and puts them in partial
326 symbol tables. When more extensive information is requested of a
327 file, the corresponding partial symbol table is mutated into a full
328 fledged symbol table by going back and reading the symbols
331 We look for sections with specific names, to tell us what debug
334 somstab_build_psymtabs() handles STABS symbols.
336 Note that SOM files have a "minimal" symbol table, which is vaguely
337 reminiscent of a COFF symbol table, but has only the minimal information
338 necessary for linking. We process this also, and use the information to
339 build gdb's minimal symbol table. This gives us some minimal debugging
340 capability even for files compiled without -g. */
343 som_symfile_read (struct objfile
*objfile
, int symfile_flags
)
345 bfd
*abfd
= objfile
->obfd
;
346 struct cleanup
*back_to
;
348 init_minimal_symbol_collection ();
349 back_to
= make_cleanup_discard_minimal_symbols ();
351 /* Process the normal SOM symbol table first.
352 This reads in the DNTT and string table, but doesn't
353 actually scan the DNTT. It does scan the linker symbol
354 table and thus build up a "minimal symbol table". */
356 som_symtab_read (abfd
, objfile
, objfile
->section_offsets
);
358 /* Install any minimal symbols that have been collected as the current
359 minimal symbols for this objfile.
360 Further symbol-reading is done incrementally, file-by-file,
361 in a step known as "psymtab-to-symtab" expansion. hp-symtab-read.c
362 contains the code to do the actual DNTT scanning and symtab building. */
363 install_minimal_symbols (objfile
);
364 do_cleanups (back_to
);
366 /* Now read information from the stabs debug sections.
367 This is emitted by gcc. */
368 stabsect_build_psymtabs (objfile
,
369 "$GDB_SYMBOLS$", "$GDB_STRINGS$", "$TEXT$");
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
)
395 /* SOM specific initialization routine for reading symbols. */
398 som_symfile_init (struct objfile
*objfile
)
400 /* SOM objects may be reordered, so set OBJF_REORDERED. If we
401 find this causes a significant slowdown in gdb then we could
402 set it in the debug symbol readers only when necessary. */
403 objfile
->flags
|= OBJF_REORDERED
;
406 /* An object of this type is passed to find_section_offset. */
408 struct find_section_offset_arg
412 struct objfile
*objfile
;
414 /* Flags to invert. */
418 /* Flags to look for. */
422 /* A text section with non-zero size, if any. */
424 asection
*best_section
;
426 /* An empty text section, if any. */
428 asection
*empty_section
;
431 /* A callback for bfd_map_over_sections that tries to find a section
432 with particular flags in an objfile. */
435 find_section_offset (bfd
*abfd
, asection
*sect
, void *arg
)
437 struct find_section_offset_arg
*info
= arg
;
440 aflag
= bfd_get_section_flags (abfd
, sect
);
442 aflag
^= info
->invert
;
444 if ((aflag
& info
->flags
) == info
->flags
)
446 if (bfd_section_size (abfd
, sect
) > 0)
448 if (info
->best_section
== NULL
)
449 info
->best_section
= sect
;
453 if (info
->empty_section
== NULL
)
454 info
->empty_section
= sect
;
459 /* Set a section index from a BFD. */
462 set_section_index (struct objfile
*objfile
, flagword invert
, flagword flags
,
465 struct find_section_offset_arg info
;
467 info
.objfile
= objfile
;
468 info
.best_section
= NULL
;
469 info
.empty_section
= NULL
;
470 info
.invert
= invert
;
472 bfd_map_over_sections (objfile
->obfd
, find_section_offset
, &info
);
474 if (info
.best_section
)
475 *index_ptr
= info
.best_section
->index
;
476 else if (info
.empty_section
)
477 *index_ptr
= info
.empty_section
->index
;
480 /* SOM specific parsing routine for section offsets.
482 Plain and simple for now. */
485 som_symfile_offsets (struct objfile
*objfile
,
486 const struct section_addr_info
*addrs
)
492 objfile
->num_sections
= bfd_count_sections (objfile
->obfd
);
493 objfile
->section_offsets
= (struct section_offsets
*)
494 obstack_alloc (&objfile
->objfile_obstack
,
495 SIZEOF_N_SECTION_OFFSETS (objfile
->num_sections
));
497 set_section_index (objfile
, 0, SEC_ALLOC
| SEC_CODE
,
498 &objfile
->sect_index_text
);
499 set_section_index (objfile
, 0, SEC_ALLOC
| SEC_DATA
,
500 &objfile
->sect_index_data
);
501 set_section_index (objfile
, SEC_LOAD
, SEC_ALLOC
| SEC_LOAD
,
502 &objfile
->sect_index_bss
);
503 set_section_index (objfile
, 0, SEC_ALLOC
| SEC_READONLY
,
504 &objfile
->sect_index_rodata
);
506 /* First see if we're a shared library. If so, get the section
507 offsets from the library, else get them from addrs. */
508 if (!som_solib_section_offsets (objfile
, objfile
->section_offsets
))
510 /* Note: Here is OK to compare with ".text" because this is the
511 name that gdb itself gives to that section, not the SOM
513 for (i
= 0; i
< addrs
->num_sections
; i
++)
514 if (strcmp (addrs
->other
[i
].name
, ".text") == 0)
516 text_addr
= addrs
->other
[i
].addr
;
518 for (i
= 0; i
< objfile
->num_sections
; i
++)
519 (objfile
->section_offsets
)->offsets
[i
] = text_addr
;
525 /* Register that we are able to handle SOM object file formats. */
527 static const struct sym_fns som_sym_fns
=
529 som_new_init
, /* init anything gbl to entire symtab */
530 som_symfile_init
, /* read initial info, setup for sym_read() */
531 som_symfile_read
, /* read a symbol file into symtab */
532 NULL
, /* sym_read_psymbols */
533 som_symfile_finish
, /* finished with file, cleanup */
534 som_symfile_offsets
, /* Translate ext. to int. relocation */
535 default_symfile_segments
, /* Get segment information from a file. */
537 default_symfile_relocate
, /* Relocate a debug section. */
538 NULL
, /* sym_get_probes */
542 initialize_file_ftype _initialize_somread
;
545 _initialize_somread (void)
547 add_symtab_fns (bfd_target_som_flavour
, &som_sym_fns
);