| 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. |
| 4 | |
| 5 | This file is part of GDB. |
| 6 | |
| 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. |
| 11 | |
| 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. |
| 16 | |
| 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/>. */ |
| 19 | |
| 20 | #include "defs.h" |
| 21 | #include "bfd.h" |
| 22 | #include "som/aout.h" |
| 23 | #include "symtab.h" |
| 24 | #include "symfile.h" |
| 25 | #include "objfiles.h" |
| 26 | #include "buildsym.h" |
| 27 | #include "stabsread.h" |
| 28 | #include "gdb-stabs.h" |
| 29 | #include "complaints.h" |
| 30 | #include "gdb_string.h" |
| 31 | #include "demangle.h" |
| 32 | #include "som.h" |
| 33 | #include "libhppa.h" |
| 34 | #include "psymtab.h" |
| 35 | |
| 36 | #include "solib-som.h" |
| 37 | |
| 38 | /* Read the symbol table of a SOM file. |
| 39 | |
| 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. */ |
| 44 | |
| 45 | static void |
| 46 | som_symtab_read (bfd *abfd, struct objfile *objfile, |
| 47 | struct section_offsets *section_offsets) |
| 48 | { |
| 49 | struct gdbarch *gdbarch = get_objfile_arch (objfile); |
| 50 | unsigned int number_of_symbols; |
| 51 | int val, dynamic; |
| 52 | char *stringtab; |
| 53 | asection *shlib_info; |
| 54 | struct som_external_symbol_dictionary_record *buf, *bufp, *endbufp; |
| 55 | char *symname; |
| 56 | CONST int symsize = sizeof (struct som_external_symbol_dictionary_record); |
| 57 | CORE_ADDR text_offset, data_offset; |
| 58 | |
| 59 | |
| 60 | text_offset = ANOFFSET (section_offsets, 0); |
| 61 | data_offset = ANOFFSET (section_offsets, 1); |
| 62 | |
| 63 | number_of_symbols = bfd_get_symcount (abfd); |
| 64 | |
| 65 | /* Allocate a buffer to read in the debug info. |
| 66 | We avoid using alloca because the memory size could be so large |
| 67 | that we could hit the stack size limit. */ |
| 68 | buf = xmalloc (symsize * number_of_symbols); |
| 69 | make_cleanup (xfree, buf); |
| 70 | bfd_seek (abfd, obj_som_sym_filepos (abfd), SEEK_SET); |
| 71 | val = bfd_bread (buf, symsize * number_of_symbols, abfd); |
| 72 | if (val != symsize * number_of_symbols) |
| 73 | error (_("Couldn't read symbol dictionary!")); |
| 74 | |
| 75 | /* Allocate a buffer to read in the som stringtab section of |
| 76 | the debugging info. Again, we avoid using alloca because |
| 77 | the data could be so large that we could potentially hit |
| 78 | the stack size limitat. */ |
| 79 | stringtab = xmalloc (obj_som_stringtab_size (abfd)); |
| 80 | make_cleanup (xfree, stringtab); |
| 81 | bfd_seek (abfd, obj_som_str_filepos (abfd), SEEK_SET); |
| 82 | val = bfd_bread (stringtab, obj_som_stringtab_size (abfd), abfd); |
| 83 | if (val != obj_som_stringtab_size (abfd)) |
| 84 | error (_("Can't read in HP string table.")); |
| 85 | |
| 86 | /* We need to determine if objfile is a dynamic executable (so we |
| 87 | can do the right thing for ST_ENTRY vs ST_CODE symbols). |
| 88 | |
| 89 | There's nothing in the header which easily allows us to do |
| 90 | this. |
| 91 | |
| 92 | This code used to rely upon the existence of a $SHLIB_INFO$ |
| 93 | section to make this determination. HP claims that it is |
| 94 | more accurate to check for a nonzero text offset, but they |
| 95 | have not provided any information about why that test is |
| 96 | more accurate. */ |
| 97 | dynamic = (text_offset != 0); |
| 98 | |
| 99 | endbufp = buf + number_of_symbols; |
| 100 | for (bufp = buf; bufp < endbufp; ++bufp) |
| 101 | { |
| 102 | enum minimal_symbol_type ms_type; |
| 103 | unsigned int flags = bfd_getb32 (bufp->flags); |
| 104 | unsigned int symbol_type |
| 105 | = (flags >> SOM_SYMBOL_TYPE_SH) & SOM_SYMBOL_TYPE_MASK; |
| 106 | unsigned int symbol_scope |
| 107 | = (flags >> SOM_SYMBOL_SCOPE_SH) & SOM_SYMBOL_SCOPE_MASK; |
| 108 | CORE_ADDR symbol_value = bfd_getb32 (bufp->symbol_value); |
| 109 | |
| 110 | QUIT; |
| 111 | |
| 112 | switch (symbol_scope) |
| 113 | { |
| 114 | case SS_UNIVERSAL: |
| 115 | case SS_EXTERNAL: |
| 116 | switch (symbol_type) |
| 117 | { |
| 118 | case ST_SYM_EXT: |
| 119 | case ST_ARG_EXT: |
| 120 | continue; |
| 121 | |
| 122 | case ST_CODE: |
| 123 | case ST_PRI_PROG: |
| 124 | case ST_SEC_PROG: |
| 125 | case ST_MILLICODE: |
| 126 | symname = bfd_getb32 (bufp->name) + stringtab; |
| 127 | ms_type = mst_text; |
| 128 | symbol_value += text_offset; |
| 129 | symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value); |
| 130 | break; |
| 131 | |
| 132 | case ST_ENTRY: |
| 133 | symname = bfd_getb32 (bufp->name) + stringtab; |
| 134 | /* For a dynamic executable, ST_ENTRY symbols are |
| 135 | the stubs, while the ST_CODE symbol is the real |
| 136 | function. */ |
| 137 | if (dynamic) |
| 138 | ms_type = mst_solib_trampoline; |
| 139 | else |
| 140 | ms_type = mst_text; |
| 141 | symbol_value += text_offset; |
| 142 | symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value); |
| 143 | break; |
| 144 | |
| 145 | case ST_STUB: |
| 146 | symname = bfd_getb32 (bufp->name) + stringtab; |
| 147 | ms_type = mst_solib_trampoline; |
| 148 | symbol_value += text_offset; |
| 149 | symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value); |
| 150 | break; |
| 151 | |
| 152 | case ST_DATA: |
| 153 | symname = bfd_getb32 (bufp->name) + stringtab; |
| 154 | symbol_value += data_offset; |
| 155 | ms_type = mst_data; |
| 156 | break; |
| 157 | default: |
| 158 | continue; |
| 159 | } |
| 160 | break; |
| 161 | |
| 162 | #if 0 |
| 163 | /* SS_GLOBAL and SS_LOCAL are two names for the same thing (!). */ |
| 164 | case SS_GLOBAL: |
| 165 | #endif |
| 166 | case SS_LOCAL: |
| 167 | switch (symbol_type) |
| 168 | { |
| 169 | case ST_SYM_EXT: |
| 170 | case ST_ARG_EXT: |
| 171 | continue; |
| 172 | |
| 173 | case ST_CODE: |
| 174 | symname = bfd_getb32 (bufp->name) + stringtab; |
| 175 | ms_type = mst_file_text; |
| 176 | symbol_value += text_offset; |
| 177 | symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value); |
| 178 | |
| 179 | check_strange_names: |
| 180 | /* Utah GCC 2.5, FSF GCC 2.6 and later generate correct local |
| 181 | label prefixes for stabs, constant data, etc. So we need |
| 182 | only filter out L$ symbols which are left in due to |
| 183 | limitations in how GAS generates SOM relocations. |
| 184 | |
| 185 | When linking in the HPUX C-library the HP linker has |
| 186 | the nasty habit of placing section symbols from the literal |
| 187 | subspaces in the middle of the program's text. Filter |
| 188 | those out as best we can. Check for first and last character |
| 189 | being '$'. |
| 190 | |
| 191 | And finally, the newer HP compilers emit crud like $PIC_foo$N |
| 192 | in some circumstance (PIC code I guess). It's also claimed |
| 193 | that they emit D$ symbols too. What stupidity. */ |
| 194 | if ((symname[0] == 'L' && symname[1] == '$') |
| 195 | || (symname[0] == '$' && symname[strlen (symname) - 1] == '$') |
| 196 | || (symname[0] == 'D' && symname[1] == '$') |
| 197 | || (strncmp (symname, "L0\001", 3) == 0) |
| 198 | || (strncmp (symname, "$PIC", 4) == 0)) |
| 199 | continue; |
| 200 | break; |
| 201 | |
| 202 | case ST_PRI_PROG: |
| 203 | case ST_SEC_PROG: |
| 204 | case ST_MILLICODE: |
| 205 | symname = bfd_getb32 (bufp->name) + stringtab; |
| 206 | ms_type = mst_file_text; |
| 207 | symbol_value += text_offset; |
| 208 | symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value); |
| 209 | break; |
| 210 | |
| 211 | case ST_ENTRY: |
| 212 | symname = bfd_getb32 (bufp->name) + stringtab; |
| 213 | /* SS_LOCAL symbols in a shared library do not have |
| 214 | export stubs, so we do not have to worry about |
| 215 | using mst_file_text vs mst_solib_trampoline here like |
| 216 | we do for SS_UNIVERSAL and SS_EXTERNAL symbols above. */ |
| 217 | ms_type = mst_file_text; |
| 218 | symbol_value += text_offset; |
| 219 | symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value); |
| 220 | break; |
| 221 | |
| 222 | case ST_STUB: |
| 223 | symname = bfd_getb32 (bufp->name) + stringtab; |
| 224 | ms_type = mst_solib_trampoline; |
| 225 | symbol_value += text_offset; |
| 226 | symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value); |
| 227 | break; |
| 228 | |
| 229 | |
| 230 | case ST_DATA: |
| 231 | symname = bfd_getb32 (bufp->name) + stringtab; |
| 232 | symbol_value += data_offset; |
| 233 | ms_type = mst_file_data; |
| 234 | goto check_strange_names; |
| 235 | |
| 236 | default: |
| 237 | continue; |
| 238 | } |
| 239 | break; |
| 240 | |
| 241 | /* This can happen for common symbols when -E is passed to the |
| 242 | final link. No idea _why_ that would make the linker force |
| 243 | common symbols to have an SS_UNSAT scope, but it does. |
| 244 | |
| 245 | This also happens for weak symbols, but their type is |
| 246 | ST_DATA. */ |
| 247 | case SS_UNSAT: |
| 248 | switch (symbol_type) |
| 249 | { |
| 250 | case ST_STORAGE: |
| 251 | case ST_DATA: |
| 252 | symname = bfd_getb32 (bufp->name) + stringtab; |
| 253 | symbol_value += data_offset; |
| 254 | ms_type = mst_data; |
| 255 | break; |
| 256 | |
| 257 | default: |
| 258 | continue; |
| 259 | } |
| 260 | break; |
| 261 | |
| 262 | default: |
| 263 | continue; |
| 264 | } |
| 265 | |
| 266 | if (bfd_getb32 (bufp->name) > obj_som_stringtab_size (abfd)) |
| 267 | error (_("Invalid symbol data; bad HP string table offset: %s"), |
| 268 | plongest (bfd_getb32 (bufp->name))); |
| 269 | |
| 270 | prim_record_minimal_symbol (symname, symbol_value, ms_type, objfile); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | /* Scan and build partial symbols for a symbol file. |
| 275 | We have been initialized by a call to som_symfile_init, which |
| 276 | currently does nothing. |
| 277 | |
| 278 | SECTION_OFFSETS is a set of offsets to apply to relocate the symbols |
| 279 | in each section. This is ignored, as it isn't needed for SOM. |
| 280 | |
| 281 | This function only does the minimum work necessary for letting the |
| 282 | user "name" things symbolically; it does not read the entire symtab. |
| 283 | Instead, it reads the external and static symbols and puts them in partial |
| 284 | symbol tables. When more extensive information is requested of a |
| 285 | file, the corresponding partial symbol table is mutated into a full |
| 286 | fledged symbol table by going back and reading the symbols |
| 287 | for real. |
| 288 | |
| 289 | We look for sections with specific names, to tell us what debug |
| 290 | format to look for: FIXME!!! |
| 291 | |
| 292 | somstab_build_psymtabs() handles STABS symbols. |
| 293 | |
| 294 | Note that SOM files have a "minimal" symbol table, which is vaguely |
| 295 | reminiscent of a COFF symbol table, but has only the minimal information |
| 296 | necessary for linking. We process this also, and use the information to |
| 297 | build gdb's minimal symbol table. This gives us some minimal debugging |
| 298 | capability even for files compiled without -g. */ |
| 299 | |
| 300 | static void |
| 301 | som_symfile_read (struct objfile *objfile, int symfile_flags) |
| 302 | { |
| 303 | bfd *abfd = objfile->obfd; |
| 304 | struct cleanup *back_to; |
| 305 | |
| 306 | init_minimal_symbol_collection (); |
| 307 | back_to = make_cleanup_discard_minimal_symbols (); |
| 308 | |
| 309 | /* Process the normal SOM symbol table first. |
| 310 | This reads in the DNTT and string table, but doesn't |
| 311 | actually scan the DNTT. It does scan the linker symbol |
| 312 | table and thus build up a "minimal symbol table". */ |
| 313 | |
| 314 | som_symtab_read (abfd, objfile, objfile->section_offsets); |
| 315 | |
| 316 | /* Install any minimal symbols that have been collected as the current |
| 317 | minimal symbols for this objfile. |
| 318 | Further symbol-reading is done incrementally, file-by-file, |
| 319 | in a step known as "psymtab-to-symtab" expansion. hp-symtab-read.c |
| 320 | contains the code to do the actual DNTT scanning and symtab building. */ |
| 321 | install_minimal_symbols (objfile); |
| 322 | do_cleanups (back_to); |
| 323 | |
| 324 | /* Now read information from the stabs debug sections. |
| 325 | This is emitted by gcc. */ |
| 326 | stabsect_build_psymtabs (objfile, |
| 327 | "$GDB_SYMBOLS$", "$GDB_STRINGS$", "$TEXT$"); |
| 328 | } |
| 329 | |
| 330 | /* Initialize anything that needs initializing when a completely new symbol |
| 331 | file is specified (not just adding some symbols from another file, e.g. a |
| 332 | shared library). |
| 333 | |
| 334 | We reinitialize buildsym, since we may be reading stabs from a SOM file. */ |
| 335 | |
| 336 | static void |
| 337 | som_new_init (struct objfile *ignore) |
| 338 | { |
| 339 | stabsread_new_init (); |
| 340 | buildsym_new_init (); |
| 341 | } |
| 342 | |
| 343 | /* Perform any local cleanups required when we are done with a particular |
| 344 | objfile. I.e, we are in the process of discarding all symbol information |
| 345 | for an objfile, freeing up all memory held for it, and unlinking the |
| 346 | objfile struct from the global list of known objfiles. */ |
| 347 | |
| 348 | static void |
| 349 | som_symfile_finish (struct objfile *objfile) |
| 350 | { |
| 351 | } |
| 352 | |
| 353 | /* SOM specific initialization routine for reading symbols. */ |
| 354 | |
| 355 | static void |
| 356 | som_symfile_init (struct objfile *objfile) |
| 357 | { |
| 358 | /* SOM objects may be reordered, so set OBJF_REORDERED. If we |
| 359 | find this causes a significant slowdown in gdb then we could |
| 360 | set it in the debug symbol readers only when necessary. */ |
| 361 | objfile->flags |= OBJF_REORDERED; |
| 362 | } |
| 363 | |
| 364 | /* SOM specific parsing routine for section offsets. |
| 365 | |
| 366 | Plain and simple for now. */ |
| 367 | |
| 368 | static void |
| 369 | som_symfile_offsets (struct objfile *objfile, struct section_addr_info *addrs) |
| 370 | { |
| 371 | int i; |
| 372 | CORE_ADDR text_addr; |
| 373 | |
| 374 | objfile->num_sections = bfd_count_sections (objfile->obfd); |
| 375 | objfile->section_offsets = (struct section_offsets *) |
| 376 | obstack_alloc (&objfile->objfile_obstack, |
| 377 | SIZEOF_N_SECTION_OFFSETS (objfile->num_sections)); |
| 378 | |
| 379 | /* FIXME: ezannoni 2000-04-20 The section names in SOM are not |
| 380 | .text, .data, etc, but $TEXT$, $DATA$,... We should initialize |
| 381 | SET_OFF_* from bfd. (See default_symfile_offsets()). But I don't |
| 382 | know the correspondence between SOM sections and GDB's idea of |
| 383 | section names. So for now we default to what is was before these |
| 384 | changes. */ |
| 385 | objfile->sect_index_text = 0; |
| 386 | objfile->sect_index_data = 1; |
| 387 | objfile->sect_index_bss = 2; |
| 388 | objfile->sect_index_rodata = 3; |
| 389 | |
| 390 | /* First see if we're a shared library. If so, get the section |
| 391 | offsets from the library, else get them from addrs. */ |
| 392 | if (!som_solib_section_offsets (objfile, objfile->section_offsets)) |
| 393 | { |
| 394 | /* Note: Here is OK to compare with ".text" because this is the |
| 395 | name that gdb itself gives to that section, not the SOM |
| 396 | name. */ |
| 397 | for (i = 0; i < addrs->num_sections && addrs->other[i].name; i++) |
| 398 | if (strcmp (addrs->other[i].name, ".text") == 0) |
| 399 | break; |
| 400 | text_addr = addrs->other[i].addr; |
| 401 | |
| 402 | for (i = 0; i < objfile->num_sections; i++) |
| 403 | (objfile->section_offsets)->offsets[i] = text_addr; |
| 404 | } |
| 405 | } |
| 406 | \f |
| 407 | |
| 408 | |
| 409 | /* Register that we are able to handle SOM object file formats. */ |
| 410 | |
| 411 | static const struct sym_fns som_sym_fns = |
| 412 | { |
| 413 | bfd_target_som_flavour, |
| 414 | som_new_init, /* init anything gbl to entire symtab */ |
| 415 | som_symfile_init, /* read initial info, setup for sym_read() */ |
| 416 | som_symfile_read, /* read a symbol file into symtab */ |
| 417 | NULL, /* sym_read_psymbols */ |
| 418 | som_symfile_finish, /* finished with file, cleanup */ |
| 419 | som_symfile_offsets, /* Translate ext. to int. relocation */ |
| 420 | default_symfile_segments, /* Get segment information from a file. */ |
| 421 | NULL, |
| 422 | default_symfile_relocate, /* Relocate a debug section. */ |
| 423 | NULL, /* sym_get_probes */ |
| 424 | &psym_functions |
| 425 | }; |
| 426 | |
| 427 | initialize_file_ftype _initialize_somread; |
| 428 | |
| 429 | void |
| 430 | _initialize_somread (void) |
| 431 | { |
| 432 | add_symtab_fns (&som_sym_fns); |
| 433 | } |