range stepping: gdbserver (x86 GNU/Linux)
[deliverable/binutils-gdb.git] / gdb / somread.c
CommitLineData
c906108c 1/* Read HP PA/Risc object files for GDB.
28e7fd62 2 Copyright (C) 1991-2013 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"
30#include "gdb_string.h"
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{
5e2b427d 49 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
50 unsigned int number_of_symbols;
51 int val, dynamic;
52 char *stringtab;
53 asection *shlib_info;
b2259038 54 struct som_external_symbol_dictionary_record *buf, *bufp, *endbufp;
c906108c 55 char *symname;
b2259038 56 CONST int symsize = sizeof (struct som_external_symbol_dictionary_record);
c906108c
SS
57
58
36192a8d
TT
59#define text_offset ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile))
60#define data_offset ANOFFSET (section_offsets, SECT_OFF_DATA (objfile))
c906108c
SS
61
62 number_of_symbols = bfd_get_symcount (abfd);
63
f31b3751
JB
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);
c906108c 69 bfd_seek (abfd, obj_som_sym_filepos (abfd), SEEK_SET);
3a42e9d0 70 val = bfd_bread (buf, symsize * number_of_symbols, abfd);
c906108c 71 if (val != symsize * number_of_symbols)
8a3fe4f8 72 error (_("Couldn't read symbol dictionary!"));
c906108c 73
f31b3751
JB
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);
c906108c 80 bfd_seek (abfd, obj_som_str_filepos (abfd), SEEK_SET);
3a42e9d0 81 val = bfd_bread (stringtab, obj_som_stringtab_size (abfd), abfd);
c906108c 82 if (val != obj_som_stringtab_size (abfd))
8a3fe4f8 83 error (_("Can't read in HP string table."));
c906108c
SS
84
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).
87
88 There's nothing in the header which easily allows us to do
3fa41cdb
JL
89 this.
90
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
95 more accurate. */
c906108c
SS
96 dynamic = (text_offset != 0);
97
98 endbufp = buf + number_of_symbols;
99 for (bufp = buf; bufp < endbufp; ++bufp)
100 {
101 enum minimal_symbol_type ms_type;
b2259038
TT
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);
36192a8d 108 asection *section = NULL;
c906108c
SS
109
110 QUIT;
111
36192a8d
TT
112 /* Compute the section. */
113 switch (symbol_scope)
114 {
115 case SS_EXTERNAL:
116 if (symbol_type != ST_STORAGE)
117 section = bfd_und_section_ptr;
118 else
119 section = bfd_com_section_ptr;
120 break;
121
122 case SS_UNSAT:
123 if (symbol_type != ST_STORAGE)
124 section = bfd_und_section_ptr;
125 else
126 section = bfd_com_section_ptr;
127 break;
128
129 case SS_UNIVERSAL:
130 section = bfd_section_from_som_symbol (abfd, bufp);
131 break;
132
133 case SS_LOCAL:
134 section = bfd_section_from_som_symbol (abfd, bufp);
135 break;
136 }
137
b2259038 138 switch (symbol_scope)
c906108c
SS
139 {
140 case SS_UNIVERSAL:
141 case SS_EXTERNAL:
b2259038 142 switch (symbol_type)
c906108c
SS
143 {
144 case ST_SYM_EXT:
145 case ST_ARG_EXT:
146 continue;
147
148 case ST_CODE:
149 case ST_PRI_PROG:
150 case ST_SEC_PROG:
151 case ST_MILLICODE:
b2259038 152 symname = bfd_getb32 (bufp->name) + stringtab;
c906108c 153 ms_type = mst_text;
b2259038
TT
154 symbol_value += text_offset;
155 symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
c906108c
SS
156 break;
157
158 case ST_ENTRY:
b2259038 159 symname = bfd_getb32 (bufp->name) + stringtab;
c906108c 160 /* For a dynamic executable, ST_ENTRY symbols are
c5aa993b
JM
161 the stubs, while the ST_CODE symbol is the real
162 function. */
c906108c
SS
163 if (dynamic)
164 ms_type = mst_solib_trampoline;
165 else
166 ms_type = mst_text;
b2259038
TT
167 symbol_value += text_offset;
168 symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
c906108c
SS
169 break;
170
171 case ST_STUB:
b2259038 172 symname = bfd_getb32 (bufp->name) + stringtab;
c906108c 173 ms_type = mst_solib_trampoline;
b2259038
TT
174 symbol_value += text_offset;
175 symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
c906108c
SS
176 break;
177
178 case ST_DATA:
b2259038
TT
179 symname = bfd_getb32 (bufp->name) + stringtab;
180 symbol_value += data_offset;
c906108c
SS
181 ms_type = mst_data;
182 break;
183 default:
184 continue;
185 }
186 break;
187
188#if 0
189 /* SS_GLOBAL and SS_LOCAL are two names for the same thing (!). */
190 case SS_GLOBAL:
191#endif
192 case SS_LOCAL:
b2259038 193 switch (symbol_type)
c906108c
SS
194 {
195 case ST_SYM_EXT:
196 case ST_ARG_EXT:
197 continue;
198
199 case ST_CODE:
b2259038 200 symname = bfd_getb32 (bufp->name) + stringtab;
c906108c 201 ms_type = mst_file_text;
b2259038
TT
202 symbol_value += text_offset;
203 symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
c906108c
SS
204
205 check_strange_names:
206 /* Utah GCC 2.5, FSF GCC 2.6 and later generate correct local
c5aa993b
JM
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.
210
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
c378eb4e 215 being '$'.
c5aa993b
JM
216
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. */
c906108c 220 if ((symname[0] == 'L' && symname[1] == '$')
c5aa993b 221 || (symname[0] == '$' && symname[strlen (symname) - 1] == '$')
c906108c 222 || (symname[0] == 'D' && symname[1] == '$')
b887c273 223 || (strncmp (symname, "L0\001", 3) == 0)
c906108c
SS
224 || (strncmp (symname, "$PIC", 4) == 0))
225 continue;
226 break;
227
228 case ST_PRI_PROG:
229 case ST_SEC_PROG:
230 case ST_MILLICODE:
b2259038 231 symname = bfd_getb32 (bufp->name) + stringtab;
c906108c 232 ms_type = mst_file_text;
b2259038
TT
233 symbol_value += text_offset;
234 symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
c906108c
SS
235 break;
236
237 case ST_ENTRY:
b2259038 238 symname = bfd_getb32 (bufp->name) + stringtab;
3fa41cdb
JL
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;
b2259038
TT
244 symbol_value += text_offset;
245 symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
c906108c
SS
246 break;
247
248 case ST_STUB:
b2259038 249 symname = bfd_getb32 (bufp->name) + stringtab;
c906108c 250 ms_type = mst_solib_trampoline;
b2259038
TT
251 symbol_value += text_offset;
252 symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
c906108c
SS
253 break;
254
255
256 case ST_DATA:
b2259038
TT
257 symname = bfd_getb32 (bufp->name) + stringtab;
258 symbol_value += data_offset;
c906108c
SS
259 ms_type = mst_file_data;
260 goto check_strange_names;
261
262 default:
263 continue;
264 }
265 break;
266
c5aa993b
JM
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.
c906108c 270
c5aa993b
JM
271 This also happens for weak symbols, but their type is
272 ST_DATA. */
c906108c 273 case SS_UNSAT:
b2259038 274 switch (symbol_type)
c906108c 275 {
c5aa993b
JM
276 case ST_STORAGE:
277 case ST_DATA:
b2259038
TT
278 symname = bfd_getb32 (bufp->name) + stringtab;
279 symbol_value += data_offset;
c5aa993b
JM
280 ms_type = mst_data;
281 break;
282
283 default:
284 continue;
c906108c
SS
285 }
286 break;
287
288 default:
289 continue;
290 }
291
b2259038
TT
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)));
c906108c 295
36192a8d
TT
296 if (bfd_is_const_section (section))
297 {
298 struct obj_section *iter;
299
300 ALL_OBJFILE_OSECTIONS (objfile, iter)
301 {
302 if (bfd_is_const_section (iter->the_bfd_section))
303 continue;
304
305 if (obj_section_addr (iter) <= symbol_value
306 && symbol_value < obj_section_endaddr (iter))
307 {
308 section = iter->the_bfd_section;
309 break;
310 }
311 }
312 }
313
314 prim_record_minimal_symbol_and_info (symname, symbol_value, ms_type,
315 gdb_bfd_section_index (objfile->obfd,
316 section),
36192a8d 317 objfile);
c906108c
SS
318 }
319}
320
321/* Scan and build partial symbols for a symbol file.
322 We have been initialized by a call to som_symfile_init, which
323 currently does nothing.
324
325 SECTION_OFFSETS is a set of offsets to apply to relocate the symbols
326 in each section. This is ignored, as it isn't needed for SOM.
327
c906108c
SS
328 This function only does the minimum work necessary for letting the
329 user "name" things symbolically; it does not read the entire symtab.
330 Instead, it reads the external and static symbols and puts them in partial
331 symbol tables. When more extensive information is requested of a
332 file, the corresponding partial symbol table is mutated into a full
333 fledged symbol table by going back and reading the symbols
334 for real.
335
336 We look for sections with specific names, to tell us what debug
36192a8d 337 format to look for.
c906108c
SS
338
339 somstab_build_psymtabs() handles STABS symbols.
340
341 Note that SOM files have a "minimal" symbol table, which is vaguely
342 reminiscent of a COFF symbol table, but has only the minimal information
343 necessary for linking. We process this also, and use the information to
344 build gdb's minimal symbol table. This gives us some minimal debugging
345 capability even for files compiled without -g. */
346
347static void
f4352531 348som_symfile_read (struct objfile *objfile, int symfile_flags)
c906108c
SS
349{
350 bfd *abfd = objfile->obfd;
351 struct cleanup *back_to;
352
c906108c 353 init_minimal_symbol_collection ();
56e290f4 354 back_to = make_cleanup_discard_minimal_symbols ();
c906108c 355
c378eb4e 356 /* Process the normal SOM symbol table first.
c906108c 357 This reads in the DNTT and string table, but doesn't
c378eb4e
MS
358 actually scan the DNTT. It does scan the linker symbol
359 table and thus build up a "minimal symbol table". */
c5aa993b 360
96baa820 361 som_symtab_read (abfd, objfile, objfile->section_offsets);
c906108c 362
7134143f 363 /* Install any minimal symbols that have been collected as the current
c378eb4e 364 minimal symbols for this objfile.
7134143f 365 Further symbol-reading is done incrementally, file-by-file,
c378eb4e
MS
366 in a step known as "psymtab-to-symtab" expansion. hp-symtab-read.c
367 contains the code to do the actual DNTT scanning and symtab building. */
7134143f
DJ
368 install_minimal_symbols (objfile);
369 do_cleanups (back_to);
370
c906108c 371 /* Now read information from the stabs debug sections.
4897bfb9 372 This is emitted by gcc. */
c67a9c90 373 stabsect_build_psymtabs (objfile,
c906108c 374 "$GDB_SYMBOLS$", "$GDB_STRINGS$", "$TEXT$");
c906108c
SS
375}
376
377/* Initialize anything that needs initializing when a completely new symbol
378 file is specified (not just adding some symbols from another file, e.g. a
379 shared library).
380
381 We reinitialize buildsym, since we may be reading stabs from a SOM file. */
382
383static void
fba45db2 384som_new_init (struct objfile *ignore)
c906108c
SS
385{
386 stabsread_new_init ();
387 buildsym_new_init ();
388}
389
390/* Perform any local cleanups required when we are done with a particular
c378eb4e 391 objfile. I.e, we are in the process of discarding all symbol information
c906108c 392 for an objfile, freeing up all memory held for it, and unlinking the
c378eb4e 393 objfile struct from the global list of known objfiles. */
c906108c
SS
394
395static void
fba45db2 396som_symfile_finish (struct objfile *objfile)
c906108c 397{
c906108c
SS
398}
399
400/* SOM specific initialization routine for reading symbols. */
401
402static void
fba45db2 403som_symfile_init (struct objfile *objfile)
c906108c
SS
404{
405 /* SOM objects may be reordered, so set OBJF_REORDERED. If we
406 find this causes a significant slowdown in gdb then we could
407 set it in the debug symbol readers only when necessary. */
408 objfile->flags |= OBJF_REORDERED;
c906108c
SS
409}
410
36192a8d
TT
411/* An object of this type is passed to find_section_offset. */
412
413struct find_section_offset_arg
414{
415 /* The objfile. */
416
417 struct objfile *objfile;
418
419 /* Flags to invert. */
420
421 flagword invert;
422
423 /* Flags to look for. */
424
425 flagword flags;
426
427 /* A text section with non-zero size, if any. */
428
429 asection *best_section;
430
431 /* An empty text section, if any. */
432
433 asection *empty_section;
434};
435
436/* A callback for bfd_map_over_sections that tries to find a section
437 with particular flags in an objfile. */
438
439static void
440find_section_offset (bfd *abfd, asection *sect, void *arg)
441{
442 struct find_section_offset_arg *info = arg;
443 flagword aflag;
444
445 aflag = bfd_get_section_flags (abfd, sect);
446
447 aflag ^= info->invert;
448
449 if ((aflag & info->flags) == info->flags)
450 {
451 if (bfd_section_size (abfd, sect) > 0)
452 {
453 if (info->best_section == NULL)
454 info->best_section = sect;
455 }
456 else
457 {
458 if (info->empty_section == NULL)
459 info->empty_section = sect;
460 }
461 }
462}
463
464/* Set a section index from a BFD. */
465
466static void
467set_section_index (struct objfile *objfile, flagword invert, flagword flags,
468 int *index_ptr)
469{
470 struct find_section_offset_arg info;
471
472 info.objfile = objfile;
473 info.best_section = NULL;
474 info.empty_section = NULL;
475 info.invert = invert;
476 info.flags = flags;
477 bfd_map_over_sections (objfile->obfd, find_section_offset, &info);
478
479 if (info.best_section)
480 *index_ptr = info.best_section->index;
481 else if (info.empty_section)
482 *index_ptr = info.empty_section->index;
483}
484
c906108c
SS
485/* SOM specific parsing routine for section offsets.
486
487 Plain and simple for now. */
488
d4f3574e 489static void
66f65e2b
TT
490som_symfile_offsets (struct objfile *objfile,
491 const struct section_addr_info *addrs)
c906108c 492{
c906108c 493 int i;
0aa9cf96 494 CORE_ADDR text_addr;
36192a8d 495 asection *sect;
c906108c 496
a39a16c4 497 objfile->num_sections = bfd_count_sections (objfile->obfd);
d4f3574e 498 objfile->section_offsets = (struct section_offsets *)
8b92e4d5 499 obstack_alloc (&objfile->objfile_obstack,
a39a16c4 500 SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
c906108c 501
36192a8d
TT
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);
b8fbeb18 510
c906108c 511 /* First see if we're a shared library. If so, get the section
2acceee2 512 offsets from the library, else get them from addrs. */
d4f3574e 513 if (!som_solib_section_offsets (objfile, objfile->section_offsets))
c906108c 514 {
b8fbeb18
EZ
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
c378eb4e 517 name. */
d76488d8 518 for (i = 0; i < addrs->num_sections; i++)
0aa9cf96
EZ
519 if (strcmp (addrs->other[i].name, ".text") == 0)
520 break;
521 text_addr = addrs->other[i].addr;
522
a39a16c4 523 for (i = 0; i < objfile->num_sections; i++)
f0a58b0b 524 (objfile->section_offsets)->offsets[i] = text_addr;
c906108c 525 }
c906108c 526}
c5aa993b 527\f
c906108c
SS
528
529
c906108c
SS
530/* Register that we are able to handle SOM object file formats. */
531
00b5771c 532static const struct sym_fns som_sym_fns =
c906108c
SS
533{
534 bfd_target_som_flavour,
3e43a32a
MS
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 */
b11896a5 538 NULL, /* sym_read_psymbols */
3e43a32a
MS
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. */
542 NULL,
543 default_symfile_relocate, /* Relocate a debug section. */
55aa24fb 544 NULL, /* sym_get_probes */
00b5771c 545 &psym_functions
c906108c
SS
546};
547
b2259038
TT
548initialize_file_ftype _initialize_somread;
549
c906108c 550void
fba45db2 551_initialize_somread (void)
c906108c
SS
552{
553 add_symtab_fns (&som_sym_fns);
554}
This page took 1.97763 seconds and 4 git commands to generate.