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"
35 #include "solib-som.h"
37 /* Read the symbol table of a SOM file.
39 Given an open bfd, a base address to relocate symbols to, and a
40 flag that specifies whether or not this bfd is for an executable
41 or not (may be shared library for example), add all the global
42 function and data symbols to the minimal symbol table. */
45 som_symtab_read (bfd *abfd, struct objfile *objfile,
46 struct section_offsets *section_offsets)
48 struct cleanup *cleanup;
49 struct gdbarch *gdbarch = get_objfile_arch (objfile);
50 unsigned int number_of_symbols;
54 struct som_external_symbol_dictionary_record *buf, *bufp, *endbufp;
56 const int symsize = sizeof (struct som_external_symbol_dictionary_record);
59 number_of_symbols = bfd_get_symcount (abfd);
61 /* Allocate a buffer to read in the debug info.
62 We avoid using alloca because the memory size could be so large
63 that we could hit the stack size limit. */
64 buf = xmalloc (symsize * number_of_symbols);
65 cleanup = make_cleanup (xfree, buf);
66 bfd_seek (abfd, obj_som_sym_filepos (abfd), SEEK_SET);
67 val = bfd_bread (buf, symsize * number_of_symbols, abfd);
68 if (val != symsize * number_of_symbols)
69 error (_("Couldn't read symbol dictionary!"));
71 /* Allocate a buffer to read in the som stringtab section of
72 the debugging info. Again, we avoid using alloca because
73 the data could be so large that we could potentially hit
74 the stack size limitat. */
75 stringtab = xmalloc (obj_som_stringtab_size (abfd));
76 make_cleanup (xfree, stringtab);
77 bfd_seek (abfd, obj_som_str_filepos (abfd), SEEK_SET);
78 val = bfd_bread (stringtab, obj_som_stringtab_size (abfd), abfd);
79 if (val != obj_som_stringtab_size (abfd))
80 error (_("Can't read in HP string table."));
82 /* We need to determine if objfile is a dynamic executable (so we
83 can do the right thing for ST_ENTRY vs ST_CODE symbols).
85 There's nothing in the header which easily allows us to do
88 This code used to rely upon the existence of a $SHLIB_INFO$
89 section to make this determination. HP claims that it is
90 more accurate to check for a nonzero text offset, but they
91 have not provided any information about why that test is
93 dynamic = (ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile)) != 0);
95 endbufp = buf + number_of_symbols;
96 for (bufp = buf; bufp < endbufp; ++bufp)
98 enum minimal_symbol_type ms_type;
99 unsigned int flags = bfd_getb32 (bufp->flags);
100 unsigned int symbol_type
101 = (flags >> SOM_SYMBOL_TYPE_SH) & SOM_SYMBOL_TYPE_MASK;
102 unsigned int symbol_scope
103 = (flags >> SOM_SYMBOL_SCOPE_SH) & SOM_SYMBOL_SCOPE_MASK;
104 CORE_ADDR symbol_value = bfd_getb32 (bufp->symbol_value);
105 asection *section = NULL;
109 /* Compute the section. */
110 switch (symbol_scope)
113 if (symbol_type != ST_STORAGE)
114 section = bfd_und_section_ptr;
116 section = bfd_com_section_ptr;
120 if (symbol_type != ST_STORAGE)
121 section = bfd_und_section_ptr;
123 section = bfd_com_section_ptr;
127 section = bfd_section_from_som_symbol (abfd, bufp);
131 section = bfd_section_from_som_symbol (abfd, bufp);
135 switch (symbol_scope)
149 symname = bfd_getb32 (bufp->name) + stringtab;
151 symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
155 symname = bfd_getb32 (bufp->name) + stringtab;
156 /* For a dynamic executable, ST_ENTRY symbols are
157 the stubs, while the ST_CODE symbol is the real
160 ms_type = mst_solib_trampoline;
163 symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
167 symname = bfd_getb32 (bufp->name) + stringtab;
168 ms_type = mst_solib_trampoline;
169 symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
173 symname = bfd_getb32 (bufp->name) + stringtab;
182 /* SS_GLOBAL and SS_LOCAL are two names for the same thing (!). */
193 symname = bfd_getb32 (bufp->name) + stringtab;
194 ms_type = mst_file_text;
195 symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
198 /* Utah GCC 2.5, FSF GCC 2.6 and later generate correct local
199 label prefixes for stabs, constant data, etc. So we need
200 only filter out L$ symbols which are left in due to
201 limitations in how GAS generates SOM relocations.
203 When linking in the HPUX C-library the HP linker has
204 the nasty habit of placing section symbols from the literal
205 subspaces in the middle of the program's text. Filter
206 those out as best we can. Check for first and last character
209 And finally, the newer HP compilers emit crud like $PIC_foo$N
210 in some circumstance (PIC code I guess). It's also claimed
211 that they emit D$ symbols too. What stupidity. */
212 if ((symname[0] == 'L' && symname[1] == '$')
213 || (symname[0] == '$' && symname[strlen (symname) - 1] == '$')
214 || (symname[0] == 'D' && symname[1] == '$')
215 || (strncmp (symname, "L0\001", 3) == 0)
216 || (strncmp (symname, "$PIC", 4) == 0))
223 symname = bfd_getb32 (bufp->name) + stringtab;
224 ms_type = mst_file_text;
225 symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
229 symname = bfd_getb32 (bufp->name) + stringtab;
230 /* SS_LOCAL symbols in a shared library do not have
231 export stubs, so we do not have to worry about
232 using mst_file_text vs mst_solib_trampoline here like
233 we do for SS_UNIVERSAL and SS_EXTERNAL symbols above. */
234 ms_type = mst_file_text;
235 symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
239 symname = bfd_getb32 (bufp->name) + stringtab;
240 ms_type = mst_solib_trampoline;
241 symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
246 symname = bfd_getb32 (bufp->name) + stringtab;
247 ms_type = mst_file_data;
248 goto check_strange_names;
255 /* This can happen for common symbols when -E is passed to the
256 final link. No idea _why_ that would make the linker force
257 common symbols to have an SS_UNSAT scope, but it does.
259 This also happens for weak symbols, but their type is
266 symname = bfd_getb32 (bufp->name) + stringtab;
279 if (bfd_getb32 (bufp->name) > obj_som_stringtab_size (abfd))
280 error (_("Invalid symbol data; bad HP string table offset: %s"),
281 plongest (bfd_getb32 (bufp->name)));
283 if (bfd_is_const_section (section))
285 struct obj_section *iter;
287 ALL_OBJFILE_OSECTIONS (objfile, iter)
292 if (bfd_is_const_section (iter->the_bfd_section))
295 start = bfd_get_section_vma (iter->objfile->obfd,
296 iter->the_bfd_section);
297 len = bfd_get_section_size (iter->the_bfd_section);
298 if (start <= symbol_value && symbol_value < start + len)
300 section = iter->the_bfd_section;
306 prim_record_minimal_symbol_and_info (symname, symbol_value, ms_type,
307 gdb_bfd_section_index (objfile->obfd,
312 do_cleanups (cleanup);
315 /* Scan and build partial symbols for a symbol file.
316 We have been initialized by a call to som_symfile_init, which
317 currently does nothing.
319 SECTION_OFFSETS is a set of offsets to apply to relocate the symbols
320 in each section. This is ignored, as it isn't needed for SOM.
322 This function only does the minimum work necessary for letting the
323 user "name" things symbolically; it does not read the entire symtab.
324 Instead, it reads the external and static symbols and puts them in partial
325 symbol tables. When more extensive information is requested of a
326 file, the corresponding partial symbol table is mutated into a full
327 fledged symbol table by going back and reading the symbols
330 We look for sections with specific names, to tell us what debug
333 somstab_build_psymtabs() handles STABS symbols.
335 Note that SOM files have a "minimal" symbol table, which is vaguely
336 reminiscent of a COFF symbol table, but has only the minimal information
337 necessary for linking. We process this also, and use the information to
338 build gdb's minimal symbol table. This gives us some minimal debugging
339 capability even for files compiled without -g. */
342 som_symfile_read (struct objfile *objfile, int symfile_flags)
344 bfd *abfd = objfile->obfd;
345 struct cleanup *back_to;
347 init_minimal_symbol_collection ();
348 back_to = make_cleanup_discard_minimal_symbols ();
350 /* Process the normal SOM symbol table first.
351 This reads in the DNTT and string table, but doesn't
352 actually scan the DNTT. It does scan the linker symbol
353 table and thus build up a "minimal symbol table". */
355 som_symtab_read (abfd, objfile, objfile->section_offsets);
357 /* Install any minimal symbols that have been collected as the current
358 minimal symbols for this objfile.
359 Further symbol-reading is done incrementally, file-by-file,
360 in a step known as "psymtab-to-symtab" expansion. hp-symtab-read.c
361 contains the code to do the actual DNTT scanning and symtab building. */
362 install_minimal_symbols (objfile);
363 do_cleanups (back_to);
365 /* Now read information from the stabs debug sections.
366 This is emitted by gcc. */
367 stabsect_build_psymtabs (objfile,
368 "$GDB_SYMBOLS$", "$GDB_STRINGS$", "$TEXT$");
371 /* Initialize anything that needs initializing when a completely new symbol
372 file is specified (not just adding some symbols from another file, e.g. a
375 We reinitialize buildsym, since we may be reading stabs from a SOM file. */
378 som_new_init (struct objfile *ignore)
380 stabsread_new_init ();
381 buildsym_new_init ();
384 /* Perform any local cleanups required when we are done with a particular
385 objfile. I.e, we are in the process of discarding all symbol information
386 for an objfile, freeing up all memory held for it, and unlinking the
387 objfile struct from the global list of known objfiles. */
390 som_symfile_finish (struct objfile *objfile)
394 /* SOM specific initialization routine for reading symbols. */
397 som_symfile_init (struct objfile *objfile)
399 /* SOM objects may be reordered, so set OBJF_REORDERED. If we
400 find this causes a significant slowdown in gdb then we could
401 set it in the debug symbol readers only when necessary. */
402 objfile->flags |= OBJF_REORDERED;
405 /* An object of this type is passed to find_section_offset. */
407 struct find_section_offset_arg
411 struct objfile *objfile;
413 /* Flags to invert. */
417 /* Flags to look for. */
421 /* A text section with non-zero size, if any. */
423 asection *best_section;
425 /* An empty text section, if any. */
427 asection *empty_section;
430 /* A callback for bfd_map_over_sections that tries to find a section
431 with particular flags in an objfile. */
434 find_section_offset (bfd *abfd, asection *sect, void *arg)
436 struct find_section_offset_arg *info = arg;
439 aflag = bfd_get_section_flags (abfd, sect);
441 aflag ^= info->invert;
443 if ((aflag & info->flags) == info->flags)
445 if (bfd_section_size (abfd, sect) > 0)
447 if (info->best_section == NULL)
448 info->best_section = sect;
452 if (info->empty_section == NULL)
453 info->empty_section = sect;
458 /* Set a section index from a BFD. */
461 set_section_index (struct objfile *objfile, flagword invert, flagword flags,
464 struct find_section_offset_arg info;
466 info.objfile = objfile;
467 info.best_section = NULL;
468 info.empty_section = NULL;
469 info.invert = invert;
471 bfd_map_over_sections (objfile->obfd, find_section_offset, &info);
473 if (info.best_section)
474 *index_ptr = info.best_section->index;
475 else if (info.empty_section)
476 *index_ptr = info.empty_section->index;
479 /* SOM specific parsing routine for section offsets.
481 Plain and simple for now. */
484 som_symfile_offsets (struct objfile *objfile,
485 const struct section_addr_info *addrs)
491 objfile->num_sections = bfd_count_sections (objfile->obfd);
492 objfile->section_offsets = (struct section_offsets *)
493 obstack_alloc (&objfile->objfile_obstack,
494 SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
496 set_section_index (objfile, 0, SEC_ALLOC | SEC_CODE,
497 &objfile->sect_index_text);
498 set_section_index (objfile, 0, SEC_ALLOC | SEC_DATA,
499 &objfile->sect_index_data);
500 set_section_index (objfile, SEC_LOAD, SEC_ALLOC | SEC_LOAD,
501 &objfile->sect_index_bss);
502 set_section_index (objfile, 0, SEC_ALLOC | SEC_READONLY,
503 &objfile->sect_index_rodata);
505 /* First see if we're a shared library. If so, get the section
506 offsets from the library, else get them from addrs. */
507 if (!som_solib_section_offsets (objfile, objfile->section_offsets))
509 /* Note: Here is OK to compare with ".text" because this is the
510 name that gdb itself gives to that section, not the SOM
512 for (i = 0; i < addrs->num_sections; i++)
513 if (strcmp (addrs->other[i].name, ".text") == 0)
515 text_addr = addrs->other[i].addr;
517 for (i = 0; i < objfile->num_sections; i++)
518 (objfile->section_offsets)->offsets[i] = text_addr;
524 /* Register that we are able to handle SOM object file formats. */
526 static const struct sym_fns som_sym_fns =
528 som_new_init, /* init anything gbl to entire symtab */
529 som_symfile_init, /* read initial info, setup for sym_read() */
530 som_symfile_read, /* read a symbol file into symtab */
531 NULL, /* sym_read_psymbols */
532 som_symfile_finish, /* finished with file, cleanup */
533 som_symfile_offsets, /* Translate ext. to int. relocation */
534 default_symfile_segments, /* Get segment information from a file. */
536 default_symfile_relocate, /* Relocate a debug section. */
537 NULL, /* sym_get_probes */
541 initialize_file_ftype _initialize_somread;
544 _initialize_somread (void)
546 add_symtab_fns (bfd_target_som_flavour, &som_sym_fns);