]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/somread.c
New common function "startswith"
[thirdparty/binutils-gdb.git] / gdb / somread.c
1 /* Read HP PA/Risc object files for GDB.
2 Copyright (C) 1991-2015 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 "demangle.h"
31 #include "som.h"
32 #include "libhppa.h"
33 #include "psymtab.h"
34
35 #include "solib-som.h"
36
37 /* Read the symbol table of a SOM file.
38
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
42 function and data symbols to the minimal symbol table. */
43
44 static void
45 som_symtab_read (bfd *abfd, struct objfile *objfile,
46 struct section_offsets *section_offsets)
47 {
48 struct cleanup *cleanup;
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
58
59 number_of_symbols = bfd_get_symcount (abfd);
60
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);
65 cleanup = make_cleanup (xfree, buf);
66 bfd_seek (abfd, obj_som_sym_filepos (abfd), SEEK_SET);
67 val = bfd_bread (buf, symsize * number_of_symbols, abfd);
68 if (val != symsize * number_of_symbols)
69 error (_("Couldn't read symbol dictionary!"));
70
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);
77 bfd_seek (abfd, obj_som_str_filepos (abfd), SEEK_SET);
78 val = bfd_bread (stringtab, obj_som_stringtab_size (abfd), abfd);
79 if (val != obj_som_stringtab_size (abfd))
80 error (_("Can't read in HP string table."));
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
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. */
93 dynamic = (ANOFFSET (section_offsets, SECT_OFF_TEXT (objfile)) != 0);
94
95 endbufp = buf + number_of_symbols;
96 for (bufp = buf; bufp < endbufp; ++bufp)
97 {
98 enum minimal_symbol_type ms_type;
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);
105 asection *section = NULL;
106
107 QUIT;
108
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
135 switch (symbol_scope)
136 {
137 case SS_UNIVERSAL:
138 case SS_EXTERNAL:
139 switch (symbol_type)
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:
149 symname = bfd_getb32 (bufp->name) + stringtab;
150 ms_type = mst_text;
151 symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
152 break;
153
154 case ST_ENTRY:
155 symname = bfd_getb32 (bufp->name) + stringtab;
156 /* For a dynamic executable, ST_ENTRY symbols are
157 the stubs, while the ST_CODE symbol is the real
158 function. */
159 if (dynamic)
160 ms_type = mst_solib_trampoline;
161 else
162 ms_type = mst_text;
163 symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
164 break;
165
166 case ST_STUB:
167 symname = bfd_getb32 (bufp->name) + stringtab;
168 ms_type = mst_solib_trampoline;
169 symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
170 break;
171
172 case ST_DATA:
173 symname = bfd_getb32 (bufp->name) + stringtab;
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:
186 switch (symbol_type)
187 {
188 case ST_SYM_EXT:
189 case ST_ARG_EXT:
190 continue;
191
192 case ST_CODE:
193 symname = bfd_getb32 (bufp->name) + stringtab;
194 ms_type = mst_file_text;
195 symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
196
197 check_strange_names:
198 /* Utah GCC 2.5, FSF GCC 2.6 and later generate correct local
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
207 being '$'.
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. */
212 if ((symname[0] == 'L' && symname[1] == '$')
213 || (symname[0] == '$' && symname[strlen (symname) - 1] == '$')
214 || (symname[0] == 'D' && symname[1] == '$')
215 || (startswith (symname, "L0\001"))
216 || (startswith (symname, "$PIC")))
217 continue;
218 break;
219
220 case ST_PRI_PROG:
221 case ST_SEC_PROG:
222 case ST_MILLICODE:
223 symname = bfd_getb32 (bufp->name) + stringtab;
224 ms_type = mst_file_text;
225 symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
226 break;
227
228 case ST_ENTRY:
229 symname = bfd_getb32 (bufp->name) + stringtab;
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;
235 symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
236 break;
237
238 case ST_STUB:
239 symname = bfd_getb32 (bufp->name) + stringtab;
240 ms_type = mst_solib_trampoline;
241 symbol_value = gdbarch_addr_bits_remove (gdbarch, symbol_value);
242 break;
243
244
245 case ST_DATA:
246 symname = bfd_getb32 (bufp->name) + stringtab;
247 ms_type = mst_file_data;
248 goto check_strange_names;
249
250 default:
251 continue;
252 }
253 break;
254
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.
258
259 This also happens for weak symbols, but their type is
260 ST_DATA. */
261 case SS_UNSAT:
262 switch (symbol_type)
263 {
264 case ST_STORAGE:
265 case ST_DATA:
266 symname = bfd_getb32 (bufp->name) + stringtab;
267 ms_type = mst_data;
268 break;
269
270 default:
271 continue;
272 }
273 break;
274
275 default:
276 continue;
277 }
278
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)));
282
283 if (bfd_is_const_section (section))
284 {
285 struct obj_section *iter;
286
287 ALL_OBJFILE_OSECTIONS (objfile, iter)
288 {
289 CORE_ADDR start;
290 CORE_ADDR len;
291
292 if (bfd_is_const_section (iter->the_bfd_section))
293 continue;
294
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)
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),
309 objfile);
310 }
311
312 do_cleanups (cleanup);
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
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
331 format to look for.
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
341 static void
342 som_symfile_read (struct objfile *objfile, int symfile_flags)
343 {
344 bfd *abfd = objfile->obfd;
345 struct cleanup *back_to;
346
347 init_minimal_symbol_collection ();
348 back_to = make_cleanup_discard_minimal_symbols ();
349
350 /* Process the normal SOM symbol table first.
351 This reads in the DNTT and string table, but doesn't
352 actually scan the DNTT. It does scan the linker symbol
353 table and thus build up a "minimal symbol table". */
354
355 som_symtab_read (abfd, objfile, objfile->section_offsets);
356
357 /* Install any minimal symbols that have been collected as the current
358 minimal symbols for this objfile.
359 Further symbol-reading is done incrementally, file-by-file,
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. */
362 install_minimal_symbols (objfile);
363 do_cleanups (back_to);
364
365 /* Now read information from the stabs debug sections.
366 This is emitted by gcc. */
367 stabsect_build_psymtabs (objfile,
368 "$GDB_SYMBOLS$", "$GDB_STRINGS$", "$TEXT$");
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
377 static void
378 som_new_init (struct objfile *ignore)
379 {
380 stabsread_new_init ();
381 buildsym_new_init ();
382 }
383
384 /* Perform any local cleanups required when we are done with a particular
385 objfile. I.e, we are in the process of discarding all symbol information
386 for an objfile, freeing up all memory held for it, and unlinking the
387 objfile struct from the global list of known objfiles. */
388
389 static void
390 som_symfile_finish (struct objfile *objfile)
391 {
392 }
393
394 /* SOM specific initialization routine for reading symbols. */
395
396 static void
397 som_symfile_init (struct objfile *objfile)
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;
403 }
404
405 /* An object of this type is passed to find_section_offset. */
406
407 struct 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
433 static void
434 find_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
460 static void
461 set_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
479 /* SOM specific parsing routine for section offsets.
480
481 Plain and simple for now. */
482
483 static void
484 som_symfile_offsets (struct objfile *objfile,
485 const struct section_addr_info *addrs)
486 {
487 int i;
488 CORE_ADDR text_addr;
489 asection *sect;
490
491 objfile->num_sections = bfd_count_sections (objfile->obfd);
492 objfile->section_offsets = (struct section_offsets *)
493 obstack_alloc (&objfile->objfile_obstack,
494 SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
495
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);
504
505 /* First see if we're a shared library. If so, get the section
506 offsets from the library, else get them from addrs. */
507 if (!som_solib_section_offsets (objfile, objfile->section_offsets))
508 {
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
511 name. */
512 for (i = 0; i < addrs->num_sections; i++)
513 if (strcmp (addrs->other[i].name, ".text") == 0)
514 break;
515 text_addr = addrs->other[i].addr;
516
517 for (i = 0; i < objfile->num_sections; i++)
518 (objfile->section_offsets)->offsets[i] = text_addr;
519 }
520 }
521 \f
522
523
524 /* Register that we are able to handle SOM object file formats. */
525
526 static const struct sym_fns som_sym_fns =
527 {
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 */
531 NULL, /* sym_read_psymbols */
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. */
537 NULL, /* sym_get_probes */
538 &psym_functions
539 };
540
541 initialize_file_ftype _initialize_somread;
542
543 void
544 _initialize_somread (void)
545 {
546 add_symtab_fns (bfd_target_som_flavour, &som_sym_fns);
547 }