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