1 /* Read HP PA/Risc object files for GDB.
2 Copyright (C) 1991-2013 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"
30 #include "gdb_string.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 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 #define text_offset ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile))
60 #define data_offset ANOFFSET (section_offsets, SECT_OFF_DATA (objfile))
62 number_of_symbols
= bfd_get_symcount (abfd
);
64 /* Allocate a buffer to read in the debug info.
65 We avoid using alloca because the memory size could be so large
66 that we could hit the stack size limit. */
67 buf
= xmalloc (symsize
* number_of_symbols
);
68 make_cleanup (xfree
, buf
);
69 bfd_seek (abfd
, obj_som_sym_filepos (abfd
), SEEK_SET
);
70 val
= bfd_bread (buf
, symsize
* number_of_symbols
, abfd
);
71 if (val
!= symsize
* number_of_symbols
)
72 error (_("Couldn't read symbol dictionary!"));
74 /* Allocate a buffer to read in the som stringtab section of
75 the debugging info. Again, we avoid using alloca because
76 the data could be so large that we could potentially hit
77 the stack size limitat. */
78 stringtab
= xmalloc (obj_som_stringtab_size (abfd
));
79 make_cleanup (xfree
, stringtab
);
80 bfd_seek (abfd
, obj_som_str_filepos (abfd
), SEEK_SET
);
81 val
= bfd_bread (stringtab
, obj_som_stringtab_size (abfd
), abfd
);
82 if (val
!= obj_som_stringtab_size (abfd
))
83 error (_("Can't read in HP string table."));
85 /* We need to determine if objfile is a dynamic executable (so we
86 can do the right thing for ST_ENTRY vs ST_CODE symbols).
88 There's nothing in the header which easily allows us to do
91 This code used to rely upon the existence of a $SHLIB_INFO$
92 section to make this determination. HP claims that it is
93 more accurate to check for a nonzero text offset, but they
94 have not provided any information about why that test is
96 dynamic
= (text_offset
!= 0);
98 endbufp
= buf
+ number_of_symbols
;
99 for (bufp
= buf
; bufp
< endbufp
; ++bufp
)
101 enum minimal_symbol_type ms_type
;
102 unsigned int flags
= bfd_getb32 (bufp
->flags
);
103 unsigned int symbol_type
104 = (flags
>> SOM_SYMBOL_TYPE_SH
) & SOM_SYMBOL_TYPE_MASK
;
105 unsigned int symbol_scope
106 = (flags
>> SOM_SYMBOL_SCOPE_SH
) & SOM_SYMBOL_SCOPE_MASK
;
107 CORE_ADDR symbol_value
= bfd_getb32 (bufp
->symbol_value
);
108 asection
*section
= NULL
;
112 /* Compute the section. */
113 switch (symbol_scope
)
116 if (symbol_type
!= ST_STORAGE
)
117 section
= bfd_und_section_ptr
;
119 section
= bfd_com_section_ptr
;
123 if (symbol_type
!= ST_STORAGE
)
124 section
= bfd_und_section_ptr
;
126 section
= bfd_com_section_ptr
;
130 section
= bfd_section_from_som_symbol (abfd
, bufp
);
134 section
= bfd_section_from_som_symbol (abfd
, bufp
);
138 switch (symbol_scope
)
152 symname
= bfd_getb32 (bufp
->name
) + stringtab
;
154 symbol_value
+= text_offset
;
155 symbol_value
= gdbarch_addr_bits_remove (gdbarch
, symbol_value
);
159 symname
= bfd_getb32 (bufp
->name
) + stringtab
;
160 /* For a dynamic executable, ST_ENTRY symbols are
161 the stubs, while the ST_CODE symbol is the real
164 ms_type
= mst_solib_trampoline
;
167 symbol_value
+= text_offset
;
168 symbol_value
= gdbarch_addr_bits_remove (gdbarch
, symbol_value
);
172 symname
= bfd_getb32 (bufp
->name
) + stringtab
;
173 ms_type
= mst_solib_trampoline
;
174 symbol_value
+= text_offset
;
175 symbol_value
= gdbarch_addr_bits_remove (gdbarch
, symbol_value
);
179 symname
= bfd_getb32 (bufp
->name
) + stringtab
;
180 symbol_value
+= data_offset
;
189 /* SS_GLOBAL and SS_LOCAL are two names for the same thing (!). */
200 symname
= bfd_getb32 (bufp
->name
) + stringtab
;
201 ms_type
= mst_file_text
;
202 symbol_value
+= text_offset
;
203 symbol_value
= gdbarch_addr_bits_remove (gdbarch
, symbol_value
);
206 /* Utah GCC 2.5, FSF GCC 2.6 and later generate correct local
207 label prefixes for stabs, constant data, etc. So we need
208 only filter out L$ symbols which are left in due to
209 limitations in how GAS generates SOM relocations.
211 When linking in the HPUX C-library the HP linker has
212 the nasty habit of placing section symbols from the literal
213 subspaces in the middle of the program's text. Filter
214 those out as best we can. Check for first and last character
217 And finally, the newer HP compilers emit crud like $PIC_foo$N
218 in some circumstance (PIC code I guess). It's also claimed
219 that they emit D$ symbols too. What stupidity. */
220 if ((symname
[0] == 'L' && symname
[1] == '$')
221 || (symname
[0] == '$' && symname
[strlen (symname
) - 1] == '$')
222 || (symname
[0] == 'D' && symname
[1] == '$')
223 || (strncmp (symname
, "L0\001", 3) == 0)
224 || (strncmp (symname
, "$PIC", 4) == 0))
231 symname
= bfd_getb32 (bufp
->name
) + stringtab
;
232 ms_type
= mst_file_text
;
233 symbol_value
+= text_offset
;
234 symbol_value
= gdbarch_addr_bits_remove (gdbarch
, symbol_value
);
238 symname
= bfd_getb32 (bufp
->name
) + stringtab
;
239 /* SS_LOCAL symbols in a shared library do not have
240 export stubs, so we do not have to worry about
241 using mst_file_text vs mst_solib_trampoline here like
242 we do for SS_UNIVERSAL and SS_EXTERNAL symbols above. */
243 ms_type
= mst_file_text
;
244 symbol_value
+= text_offset
;
245 symbol_value
= gdbarch_addr_bits_remove (gdbarch
, symbol_value
);
249 symname
= bfd_getb32 (bufp
->name
) + stringtab
;
250 ms_type
= mst_solib_trampoline
;
251 symbol_value
+= text_offset
;
252 symbol_value
= gdbarch_addr_bits_remove (gdbarch
, symbol_value
);
257 symname
= bfd_getb32 (bufp
->name
) + stringtab
;
258 symbol_value
+= data_offset
;
259 ms_type
= mst_file_data
;
260 goto check_strange_names
;
267 /* This can happen for common symbols when -E is passed to the
268 final link. No idea _why_ that would make the linker force
269 common symbols to have an SS_UNSAT scope, but it does.
271 This also happens for weak symbols, but their type is
278 symname
= bfd_getb32 (bufp
->name
) + stringtab
;
279 symbol_value
+= data_offset
;
292 if (bfd_getb32 (bufp
->name
) > obj_som_stringtab_size (abfd
))
293 error (_("Invalid symbol data; bad HP string table offset: %s"),
294 plongest (bfd_getb32 (bufp
->name
)));
296 if (bfd_is_const_section (section
))
298 struct obj_section
*iter
;
300 ALL_OBJFILE_OSECTIONS (objfile
, iter
)
302 if (bfd_is_const_section (iter
->the_bfd_section
))
305 if (obj_section_addr (iter
) <= symbol_value
306 && symbol_value
< obj_section_endaddr (iter
))
308 section
= iter
->the_bfd_section
;
314 prim_record_minimal_symbol_and_info (symname
, symbol_value
, ms_type
,
315 gdb_bfd_section_index (objfile
->obfd
,
322 /* Scan and build partial symbols for a symbol file.
323 We have been initialized by a call to som_symfile_init, which
324 currently does nothing.
326 SECTION_OFFSETS is a set of offsets to apply to relocate the symbols
327 in each section. This is ignored, as it isn't needed for SOM.
329 This function only does the minimum work necessary for letting the
330 user "name" things symbolically; it does not read the entire symtab.
331 Instead, it reads the external and static symbols and puts them in partial
332 symbol tables. When more extensive information is requested of a
333 file, the corresponding partial symbol table is mutated into a full
334 fledged symbol table by going back and reading the symbols
337 We look for sections with specific names, to tell us what debug
340 somstab_build_psymtabs() handles STABS symbols.
342 Note that SOM files have a "minimal" symbol table, which is vaguely
343 reminiscent of a COFF symbol table, but has only the minimal information
344 necessary for linking. We process this also, and use the information to
345 build gdb's minimal symbol table. This gives us some minimal debugging
346 capability even for files compiled without -g. */
349 som_symfile_read (struct objfile
*objfile
, int symfile_flags
)
351 bfd
*abfd
= objfile
->obfd
;
352 struct cleanup
*back_to
;
354 init_minimal_symbol_collection ();
355 back_to
= make_cleanup_discard_minimal_symbols ();
357 /* Process the normal SOM symbol table first.
358 This reads in the DNTT and string table, but doesn't
359 actually scan the DNTT. It does scan the linker symbol
360 table and thus build up a "minimal symbol table". */
362 som_symtab_read (abfd
, objfile
, objfile
->section_offsets
);
364 /* Install any minimal symbols that have been collected as the current
365 minimal symbols for this objfile.
366 Further symbol-reading is done incrementally, file-by-file,
367 in a step known as "psymtab-to-symtab" expansion. hp-symtab-read.c
368 contains the code to do the actual DNTT scanning and symtab building. */
369 install_minimal_symbols (objfile
);
370 do_cleanups (back_to
);
372 /* Now read information from the stabs debug sections.
373 This is emitted by gcc. */
374 stabsect_build_psymtabs (objfile
,
375 "$GDB_SYMBOLS$", "$GDB_STRINGS$", "$TEXT$");
378 /* Initialize anything that needs initializing when a completely new symbol
379 file is specified (not just adding some symbols from another file, e.g. a
382 We reinitialize buildsym, since we may be reading stabs from a SOM file. */
385 som_new_init (struct objfile
*ignore
)
387 stabsread_new_init ();
388 buildsym_new_init ();
391 /* Perform any local cleanups required when we are done with a particular
392 objfile. I.e, we are in the process of discarding all symbol information
393 for an objfile, freeing up all memory held for it, and unlinking the
394 objfile struct from the global list of known objfiles. */
397 som_symfile_finish (struct objfile
*objfile
)
401 /* SOM specific initialization routine for reading symbols. */
404 som_symfile_init (struct objfile
*objfile
)
406 /* SOM objects may be reordered, so set OBJF_REORDERED. If we
407 find this causes a significant slowdown in gdb then we could
408 set it in the debug symbol readers only when necessary. */
409 objfile
->flags
|= OBJF_REORDERED
;
412 /* An object of this type is passed to find_section_offset. */
414 struct find_section_offset_arg
418 struct objfile
*objfile
;
420 /* Flags to invert. */
424 /* Flags to look for. */
428 /* A text section with non-zero size, if any. */
430 asection
*best_section
;
432 /* An empty text section, if any. */
434 asection
*empty_section
;
437 /* A callback for bfd_map_over_sections that tries to find a section
438 with particular flags in an objfile. */
441 find_section_offset (bfd
*abfd
, asection
*sect
, void *arg
)
443 struct find_section_offset_arg
*info
= arg
;
446 aflag
= bfd_get_section_flags (abfd
, sect
);
448 aflag
^= info
->invert
;
450 if ((aflag
& info
->flags
) == info
->flags
)
452 if (bfd_section_size (abfd
, sect
) > 0)
454 if (info
->best_section
== NULL
)
455 info
->best_section
= sect
;
459 if (info
->empty_section
== NULL
)
460 info
->empty_section
= sect
;
465 /* Set a section index from a BFD. */
468 set_section_index (struct objfile
*objfile
, flagword invert
, flagword flags
,
471 struct find_section_offset_arg info
;
473 info
.objfile
= objfile
;
474 info
.best_section
= NULL
;
475 info
.empty_section
= NULL
;
476 info
.invert
= invert
;
478 bfd_map_over_sections (objfile
->obfd
, find_section_offset
, &info
);
480 if (info
.best_section
)
481 *index_ptr
= info
.best_section
->index
;
482 else if (info
.empty_section
)
483 *index_ptr
= info
.empty_section
->index
;
486 /* SOM specific parsing routine for section offsets.
488 Plain and simple for now. */
491 som_symfile_offsets (struct objfile
*objfile
, struct section_addr_info
*addrs
)
497 objfile
->num_sections
= bfd_count_sections (objfile
->obfd
);
498 objfile
->section_offsets
= (struct section_offsets
*)
499 obstack_alloc (&objfile
->objfile_obstack
,
500 SIZEOF_N_SECTION_OFFSETS (objfile
->num_sections
));
502 set_section_index (objfile
, 0, SEC_ALLOC
| SEC_CODE
,
503 &objfile
->sect_index_text
);
504 set_section_index (objfile
, 0, SEC_ALLOC
| SEC_DATA
,
505 &objfile
->sect_index_data
);
506 set_section_index (objfile
, SEC_LOAD
, SEC_ALLOC
| SEC_LOAD
,
507 &objfile
->sect_index_bss
);
508 set_section_index (objfile
, 0, SEC_ALLOC
| SEC_READONLY
,
509 &objfile
->sect_index_rodata
);
511 /* First see if we're a shared library. If so, get the section
512 offsets from the library, else get them from addrs. */
513 if (!som_solib_section_offsets (objfile
, objfile
->section_offsets
))
515 /* Note: Here is OK to compare with ".text" because this is the
516 name that gdb itself gives to that section, not the SOM
518 for (i
= 0; i
< addrs
->num_sections
; i
++)
519 if (strcmp (addrs
->other
[i
].name
, ".text") == 0)
521 text_addr
= addrs
->other
[i
].addr
;
523 for (i
= 0; i
< objfile
->num_sections
; i
++)
524 (objfile
->section_offsets
)->offsets
[i
] = text_addr
;
530 /* Register that we are able to handle SOM object file formats. */
532 static const struct sym_fns som_sym_fns
=
534 bfd_target_som_flavour
,
535 som_new_init
, /* init anything gbl to entire symtab */
536 som_symfile_init
, /* read initial info, setup for sym_read() */
537 som_symfile_read
, /* read a symbol file into symtab */
538 NULL
, /* sym_read_psymbols */
539 som_symfile_finish
, /* finished with file, cleanup */
540 som_symfile_offsets
, /* Translate ext. to int. relocation */
541 default_symfile_segments
, /* Get segment information from a file. */
543 default_symfile_relocate
, /* Relocate a debug section. */
544 NULL
, /* sym_get_probes */
548 initialize_file_ftype _initialize_somread
;
551 _initialize_somread (void)
553 add_symtab_fns (&som_sym_fns
);