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