Support fusion for ELFv2 stubs
[deliverable/binutils-gdb.git] / gdb / somread.c
CommitLineData
c906108c 1/* Read HP PA/Risc object files for GDB.
ecd75fc8 2 Copyright (C) 1991-2014 Free Software Foundation, Inc.
c906108c
SS
3 Written by Fred Fish at Cygnus Support.
4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
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.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
21#include "bfd.h"
b2259038 22#include "som/aout.h"
c906108c
SS
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"
0e9f083f 30#include <string.h>
c906108c
SS
31#include "demangle.h"
32#include "som.h"
33#include "libhppa.h"
ccefe4c4 34#include "psymtab.h"
c906108c 35
17fe2d6e 36#include "solib-som.h"
c906108c 37
7f86f058 38/* Read the symbol table of a SOM file.
c906108c 39
c5aa993b
JM
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
7f86f058 43 function and data symbols to the minimal symbol table. */
c906108c
SS
44
45static void
fba45db2
KB
46som_symtab_read (bfd *abfd, struct objfile *objfile,
47 struct section_offsets *section_offsets)
c906108c 48{
fe48dfb1 49 struct cleanup *cleanup;
5e2b427d 50 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
51 unsigned int number_of_symbols;
52 int val, dynamic;
53 char *stringtab;
54 asection *shlib_info;
b2259038 55 struct som_external_symbol_dictionary_record *buf, *bufp, *endbufp;
c906108c 56 char *symname;
cc2f3c35 57 const int symsize = sizeof (struct som_external_symbol_dictionary_record);
c906108c
SS
58
59
c906108c
SS
60 number_of_symbols = bfd_get_symcount (abfd);
61
f31b3751
JB
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);
fe48dfb1 66 cleanup = make_cleanup (xfree, buf);
c906108c 67 bfd_seek (abfd, obj_som_sym_filepos (abfd), SEEK_SET);
3a42e9d0 68 val = bfd_bread (buf, symsize * number_of_symbols, abfd);
c906108c 69 if (val != symsize * number_of_symbols)
8a3fe4f8 70 error (_("Couldn't read symbol dictionary!"));
c906108c 71
f31b3751
JB
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);
c906108c 78 bfd_seek (abfd, obj_som_str_filepos (abfd), SEEK_SET);
3a42e9d0 79 val = bfd_bread (stringtab, obj_som_stringtab_size (abfd), abfd);
c906108c 80 if (val != obj_som_stringtab_size (abfd))
8a3fe4f8 81 error (_("Can't read in HP string table."));
c906108c
SS
82
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).
85
86 There's nothing in the header which easily allows us to do
3fa41cdb
JL
87 this.
88
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
93 more accurate. */
2273f0ac 94 dynamic = (ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile)) != 0);
c906108c
SS
95
96 endbufp = buf + number_of_symbols;
97 for (bufp = buf; bufp < endbufp; ++bufp)
98 {
99 enum minimal_symbol_type ms_type;
b2259038
TT
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);
36192a8d 106 asection *section = NULL;
c906108c
SS
107
108 QUIT;
109
36192a8d
TT
110 /* Compute the section. */
111 switch (symbol_scope)
112 {
113 case SS_EXTERNAL:
114 if (symbol_type != ST_STORAGE)
115 section = bfd_und_section_ptr;
116 else
117 section = bfd_com_section_ptr;
118 break;
119
120 case SS_UNSAT:
121 if (symbol_type != ST_STORAGE)
122 section = bfd_und_section_ptr;
123 else
124 section = bfd_com_section_ptr;
125 break;
126
127 case SS_UNIVERSAL:
128 section = bfd_section_from_som_symbol (abfd, bufp);
129 break;
130
131 case SS_LOCAL:
132 section = bfd_section_from_som_symbol (abfd, bufp);
133 break;
134 }
135
b2259038 136 switch (symbol_scope)
c906108c
SS
137 {
138 case SS_UNIVERSAL:
139 case SS_EXTERNAL:
b2259038 140 switch (symbol_type)
c906108c
SS
141 {
142 case ST_SYM_EXT:
143 case ST_ARG_EXT:
144 continue;
145
146 case ST_CODE:
147 case ST_PRI_PROG:
148 case ST_SEC_PROG:
149 case ST_MILLICODE:
b2259038 150 symname = bfd_getb32 (bufp->name) + stringtab;
c906108c 151 ms_type = mst_text;
b2259038 152 symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
c906108c
SS
153 break;
154
155 case ST_ENTRY:
b2259038 156 symname = bfd_getb32 (bufp->name) + stringtab;
c906108c 157 /* For a dynamic executable, ST_ENTRY symbols are
c5aa993b
JM
158 the stubs, while the ST_CODE symbol is the real
159 function. */
c906108c
SS
160 if (dynamic)
161 ms_type = mst_solib_trampoline;
162 else
163 ms_type = mst_text;
b2259038 164 symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
c906108c
SS
165 break;
166
167 case ST_STUB:
b2259038 168 symname = bfd_getb32 (bufp->name) + stringtab;
c906108c 169 ms_type = mst_solib_trampoline;
b2259038 170 symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
c906108c
SS
171 break;
172
173 case ST_DATA:
b2259038 174 symname = bfd_getb32 (bufp->name) + stringtab;
c906108c
SS
175 ms_type = mst_data;
176 break;
177 default:
178 continue;
179 }
180 break;
181
182#if 0
183 /* SS_GLOBAL and SS_LOCAL are two names for the same thing (!). */
184 case SS_GLOBAL:
185#endif
186 case SS_LOCAL:
b2259038 187 switch (symbol_type)
c906108c
SS
188 {
189 case ST_SYM_EXT:
190 case ST_ARG_EXT:
191 continue;
192
193 case ST_CODE:
b2259038 194 symname = bfd_getb32 (bufp->name) + stringtab;
c906108c 195 ms_type = mst_file_text;
b2259038 196 symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
c906108c
SS
197
198 check_strange_names:
199 /* Utah GCC 2.5, FSF GCC 2.6 and later generate correct local
c5aa993b
JM
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.
203
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
c378eb4e 208 being '$'.
c5aa993b
JM
209
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. */
c906108c 213 if ((symname[0] == 'L' && symname[1] == '$')
c5aa993b 214 || (symname[0] == '$' && symname[strlen (symname) - 1] == '$')
c906108c 215 || (symname[0] == 'D' && symname[1] == '$')
b887c273 216 || (strncmp (symname, "L0\001", 3) == 0)
c906108c
SS
217 || (strncmp (symname, "$PIC", 4) == 0))
218 continue;
219 break;
220
221 case ST_PRI_PROG:
222 case ST_SEC_PROG:
223 case ST_MILLICODE:
b2259038 224 symname = bfd_getb32 (bufp->name) + stringtab;
c906108c 225 ms_type = mst_file_text;
b2259038 226 symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
c906108c
SS
227 break;
228
229 case ST_ENTRY:
b2259038 230 symname = bfd_getb32 (bufp->name) + stringtab;
3fa41cdb
JL
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;
b2259038 236 symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
c906108c
SS
237 break;
238
239 case ST_STUB:
b2259038 240 symname = bfd_getb32 (bufp->name) + stringtab;
c906108c 241 ms_type = mst_solib_trampoline;
b2259038 242 symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
c906108c
SS
243 break;
244
245
246 case ST_DATA:
b2259038 247 symname = bfd_getb32 (bufp->name) + stringtab;
c906108c
SS
248 ms_type = mst_file_data;
249 goto check_strange_names;
250
251 default:
252 continue;
253 }
254 break;
255
c5aa993b
JM
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.
c906108c 259
c5aa993b
JM
260 This also happens for weak symbols, but their type is
261 ST_DATA. */
c906108c 262 case SS_UNSAT:
b2259038 263 switch (symbol_type)
c906108c 264 {
c5aa993b
JM
265 case ST_STORAGE:
266 case ST_DATA:
b2259038 267 symname = bfd_getb32 (bufp->name) + stringtab;
c5aa993b
JM
268 ms_type = mst_data;
269 break;
270
271 default:
272 continue;
c906108c
SS
273 }
274 break;
275
276 default:
277 continue;
278 }
279
b2259038
TT
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)));
c906108c 283
36192a8d
TT
284 if (bfd_is_const_section (section))
285 {
286 struct obj_section *iter;
287
288 ALL_OBJFILE_OSECTIONS (objfile, iter)
289 {
2273f0ac
TT
290 CORE_ADDR start;
291 CORE_ADDR len;
292
36192a8d
TT
293 if (bfd_is_const_section (iter->the_bfd_section))
294 continue;
295
2273f0ac
TT
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)
36192a8d
TT
300 {
301 section = iter->the_bfd_section;
302 break;
303 }
304 }
305 }
306
307 prim_record_minimal_symbol_and_info (symname, symbol_value, ms_type,
308 gdb_bfd_section_index (objfile->obfd,
309 section),
36192a8d 310 objfile);
c906108c 311 }
fe48dfb1
TT
312
313 do_cleanups (cleanup);
c906108c
SS
314}
315
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.
319
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.
322
c906108c
SS
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
329 for real.
330
331 We look for sections with specific names, to tell us what debug
36192a8d 332 format to look for.
c906108c
SS
333
334 somstab_build_psymtabs() handles STABS symbols.
335
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. */
341
342static void
f4352531 343som_symfile_read (struct objfile *objfile, int symfile_flags)
c906108c
SS
344{
345 bfd *abfd = objfile->obfd;
346 struct cleanup *back_to;
347
c906108c 348 init_minimal_symbol_collection ();
56e290f4 349 back_to = make_cleanup_discard_minimal_symbols ();
c906108c 350
c378eb4e 351 /* Process the normal SOM symbol table first.
c906108c 352 This reads in the DNTT and string table, but doesn't
c378eb4e
MS
353 actually scan the DNTT. It does scan the linker symbol
354 table and thus build up a "minimal symbol table". */
c5aa993b 355
96baa820 356 som_symtab_read (abfd, objfile, objfile->section_offsets);
c906108c 357
7134143f 358 /* Install any minimal symbols that have been collected as the current
c378eb4e 359 minimal symbols for this objfile.
7134143f 360 Further symbol-reading is done incrementally, file-by-file,
c378eb4e
MS
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. */
7134143f
DJ
363 install_minimal_symbols (objfile);
364 do_cleanups (back_to);
365
c906108c 366 /* Now read information from the stabs debug sections.
4897bfb9 367 This is emitted by gcc. */
c67a9c90 368 stabsect_build_psymtabs (objfile,
c906108c 369 "$GDB_SYMBOLS$", "$GDB_STRINGS$", "$TEXT$");
c906108c
SS
370}
371
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
374 shared library).
375
376 We reinitialize buildsym, since we may be reading stabs from a SOM file. */
377
378static void
fba45db2 379som_new_init (struct objfile *ignore)
c906108c
SS
380{
381 stabsread_new_init ();
382 buildsym_new_init ();
383}
384
385/* Perform any local cleanups required when we are done with a particular
c378eb4e 386 objfile. I.e, we are in the process of discarding all symbol information
c906108c 387 for an objfile, freeing up all memory held for it, and unlinking the
c378eb4e 388 objfile struct from the global list of known objfiles. */
c906108c
SS
389
390static void
fba45db2 391som_symfile_finish (struct objfile *objfile)
c906108c 392{
c906108c
SS
393}
394
395/* SOM specific initialization routine for reading symbols. */
396
397static void
fba45db2 398som_symfile_init (struct objfile *objfile)
c906108c
SS
399{
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;
c906108c
SS
404}
405
36192a8d
TT
406/* An object of this type is passed to find_section_offset. */
407
408struct find_section_offset_arg
409{
410 /* The objfile. */
411
412 struct objfile *objfile;
413
414 /* Flags to invert. */
415
416 flagword invert;
417
418 /* Flags to look for. */
419
420 flagword flags;
421
422 /* A text section with non-zero size, if any. */
423
424 asection *best_section;
425
426 /* An empty text section, if any. */
427
428 asection *empty_section;
429};
430
431/* A callback for bfd_map_over_sections that tries to find a section
432 with particular flags in an objfile. */
433
434static void
435find_section_offset (bfd *abfd, asection *sect, void *arg)
436{
437 struct find_section_offset_arg *info = arg;
438 flagword aflag;
439
440 aflag = bfd_get_section_flags (abfd, sect);
441
442 aflag ^= info->invert;
443
444 if ((aflag & info->flags) == info->flags)
445 {
446 if (bfd_section_size (abfd, sect) > 0)
447 {
448 if (info->best_section == NULL)
449 info->best_section = sect;
450 }
451 else
452 {
453 if (info->empty_section == NULL)
454 info->empty_section = sect;
455 }
456 }
457}
458
459/* Set a section index from a BFD. */
460
461static void
462set_section_index (struct objfile *objfile, flagword invert, flagword flags,
463 int *index_ptr)
464{
465 struct find_section_offset_arg info;
466
467 info.objfile = objfile;
468 info.best_section = NULL;
469 info.empty_section = NULL;
470 info.invert = invert;
471 info.flags = flags;
472 bfd_map_over_sections (objfile->obfd, find_section_offset, &info);
473
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;
478}
479
c906108c
SS
480/* SOM specific parsing routine for section offsets.
481
482 Plain and simple for now. */
483
d4f3574e 484static void
66f65e2b
TT
485som_symfile_offsets (struct objfile *objfile,
486 const struct section_addr_info *addrs)
c906108c 487{
c906108c 488 int i;
0aa9cf96 489 CORE_ADDR text_addr;
36192a8d 490 asection *sect;
c906108c 491
a39a16c4 492 objfile->num_sections = bfd_count_sections (objfile->obfd);
d4f3574e 493 objfile->section_offsets = (struct section_offsets *)
8b92e4d5 494 obstack_alloc (&objfile->objfile_obstack,
a39a16c4 495 SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
c906108c 496
36192a8d
TT
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);
b8fbeb18 505
c906108c 506 /* First see if we're a shared library. If so, get the section
2acceee2 507 offsets from the library, else get them from addrs. */
d4f3574e 508 if (!som_solib_section_offsets (objfile, objfile->section_offsets))
c906108c 509 {
b8fbeb18
EZ
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
c378eb4e 512 name. */
d76488d8 513 for (i = 0; i < addrs->num_sections; i++)
0aa9cf96
EZ
514 if (strcmp (addrs->other[i].name, ".text") == 0)
515 break;
516 text_addr = addrs->other[i].addr;
517
a39a16c4 518 for (i = 0; i < objfile->num_sections; i++)
f0a58b0b 519 (objfile->section_offsets)->offsets[i] = text_addr;
c906108c 520 }
c906108c 521}
c5aa993b 522\f
c906108c
SS
523
524
c906108c
SS
525/* Register that we are able to handle SOM object file formats. */
526
00b5771c 527static const struct sym_fns som_sym_fns =
c906108c 528{
3e43a32a
MS
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 */
b11896a5 532 NULL, /* sym_read_psymbols */
3e43a32a
MS
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. */
536 NULL,
537 default_symfile_relocate, /* Relocate a debug section. */
55aa24fb 538 NULL, /* sym_get_probes */
00b5771c 539 &psym_functions
c906108c
SS
540};
541
b2259038
TT
542initialize_file_ftype _initialize_somread;
543
c906108c 544void
fba45db2 545_initialize_somread (void)
c906108c 546{
c256e171 547 add_symtab_fns (bfd_target_som_flavour, &som_sym_fns);
c906108c 548}
This page took 2.140901 seconds and 4 git commands to generate.