Tue Nov 5 10:21:02 1996 Michael Snyder <msnyder@cleaver.cygnus.com>
[deliverable/binutils-gdb.git] / gdb / somread.c
CommitLineData
bfe2f12b 1/* Read HP PA/Risc object files for GDB.
436d4143 2 Copyright 1991, 1992, 1996 Free Software Foundation, Inc.
bfe2f12b
JL
3 Written by Fred Fish at Cygnus Support.
4
5This file is part of GDB.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
6c9638b4 19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
bfe2f12b
JL
20
21#include "defs.h"
22#include "bfd.h"
bfe2f12b
JL
23#include <syms.h>
24#include "symtab.h"
25#include "symfile.h"
26#include "objfiles.h"
27#include "buildsym.h"
28#include "stabsread.h"
29#include "gdb-stabs.h"
30#include "complaints.h"
2b576293 31#include "gdb_string.h"
bfe2f12b 32#include "demangle.h"
e618c70e
SG
33#include "som.h"
34#include "libhppa.h"
bfe2f12b 35
bfe2f12b
JL
36/* Various things we might complain about... */
37
38static void
6a86fa48 39som_symfile_init PARAMS ((struct objfile *));
bfe2f12b
JL
40
41static void
42som_new_init PARAMS ((struct objfile *));
43
44static void
45som_symfile_read PARAMS ((struct objfile *, struct section_offsets *, int));
46
47static void
48som_symfile_finish PARAMS ((struct objfile *));
49
50static void
bb140953
JL
51som_symtab_read PARAMS ((bfd *, struct objfile *,
52 struct section_offsets *));
bfe2f12b 53
bfe2f12b
JL
54static struct section_offsets *
55som_symfile_offsets PARAMS ((struct objfile *, CORE_ADDR));
56
3f550b59
FF
57/* FIXME: These should really be in a common header somewhere */
58
59extern void
60hpread_build_psymtabs PARAMS ((struct objfile *, struct section_offsets *, int));
61
62extern void
63hpread_symfile_finish PARAMS ((struct objfile *));
64
65extern void
66hpread_symfile_init PARAMS ((struct objfile *));
67
bfe2f12b
JL
68/*
69
70LOCAL FUNCTION
71
72 som_symtab_read -- read the symbol table of a SOM file
73
74SYNOPSIS
75
bb140953
JL
76 void som_symtab_read (bfd *abfd, struct objfile *objfile,
77 struct section_offsets *section_offsets)
bfe2f12b
JL
78
79DESCRIPTION
80
81 Given an open bfd, a base address to relocate symbols to, and a
82 flag that specifies whether or not this bfd is for an executable
83 or not (may be shared library for example), add all the global
84 function and data symbols to the minimal symbol table.
85*/
86
87static void
bb140953 88som_symtab_read (abfd, objfile, section_offsets)
bfe2f12b 89 bfd *abfd;
bfe2f12b 90 struct objfile *objfile;
bb140953 91 struct section_offsets *section_offsets;
bfe2f12b
JL
92{
93 unsigned int number_of_symbols;
bfe2f12b
JL
94 int val, dynamic;
95 char *stringtab;
96 asection *shlib_info;
97 struct symbol_dictionary_record *buf, *bufp, *endbufp;
98 char *symname;
99 CONST int symsize = sizeof (struct symbol_dictionary_record);
506af7a7 100 CORE_ADDR text_offset, data_offset;
bb140953
JL
101
102
bb140953 103 text_offset = ANOFFSET (section_offsets, 0);
506af7a7 104 data_offset = ANOFFSET (section_offsets, 1);
bfe2f12b
JL
105
106 number_of_symbols = bfd_get_symcount (abfd);
107
108 buf = alloca (symsize * number_of_symbols);
987622b5 109 bfd_seek (abfd, obj_som_sym_filepos (abfd), SEEK_SET);
bfe2f12b
JL
110 val = bfd_read (buf, symsize * number_of_symbols, 1, abfd);
111 if (val != symsize * number_of_symbols)
112 error ("Couldn't read symbol dictionary!");
113
114 stringtab = alloca (obj_som_stringtab_size (abfd));
987622b5 115 bfd_seek (abfd, obj_som_str_filepos (abfd), SEEK_SET);
bfe2f12b
JL
116 val = bfd_read (stringtab, obj_som_stringtab_size (abfd), 1, abfd);
117 if (val != obj_som_stringtab_size (abfd))
118 error ("Can't read in HP string table.");
119
120 /* We need to determine if objfile is a dynamic executable (so we
121 can do the right thing for ST_ENTRY vs ST_CODE symbols).
122
123 There's nothing in the header which easily allows us to do
124 this. The only reliable way I know of is to check for the
125 existance of a $SHLIB_INFO$ section with a non-zero size. */
126 shlib_info = bfd_get_section_by_name (objfile->obfd, "$SHLIB_INFO$");
127 if (shlib_info)
128 dynamic = (bfd_section_size (objfile->obfd, shlib_info) != 0);
129 else
130 dynamic = 0;
131
132 endbufp = buf + number_of_symbols;
133 for (bufp = buf; bufp < endbufp; ++bufp)
134 {
135 enum minimal_symbol_type ms_type;
136
137 QUIT;
138
139 switch (bufp->symbol_scope)
140 {
141 case SS_UNIVERSAL:
142 case SS_EXTERNAL:
143 switch (bufp->symbol_type)
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:
153 symname = bufp->name.n_strx + stringtab;
154 ms_type = mst_text;
bb140953 155 bufp->symbol_value += text_offset;
bfe2f12b
JL
156#ifdef SMASH_TEXT_ADDRESS
157 SMASH_TEXT_ADDRESS (bufp->symbol_value);
158#endif
159 break;
160
161 case ST_ENTRY:
162 symname = bufp->name.n_strx + stringtab;
163 /* For a dynamic executable, ST_ENTRY symbols are
164 the stubs, while the ST_CODE symbol is the real
165 function. */
166 if (dynamic)
167 ms_type = mst_solib_trampoline;
168 else
169 ms_type = mst_text;
bb140953 170 bufp->symbol_value += text_offset;
bfe2f12b
JL
171#ifdef SMASH_TEXT_ADDRESS
172 SMASH_TEXT_ADDRESS (bufp->symbol_value);
173#endif
174 break;
175
176 case ST_STUB:
177 symname = bufp->name.n_strx + stringtab;
178 ms_type = mst_solib_trampoline;
bb140953 179 bufp->symbol_value += text_offset;
bfe2f12b
JL
180#ifdef SMASH_TEXT_ADDRESS
181 SMASH_TEXT_ADDRESS (bufp->symbol_value);
182#endif
183 break;
184
185 case ST_DATA:
186 symname = bufp->name.n_strx + stringtab;
506af7a7 187 bufp->symbol_value += data_offset;
bfe2f12b
JL
188 ms_type = mst_data;
189 break;
190 default:
191 continue;
192 }
193 break;
194
195#if 0
196 /* SS_GLOBAL and SS_LOCAL are two names for the same thing (!). */
197 case SS_GLOBAL:
198#endif
199 case SS_LOCAL:
200 switch (bufp->symbol_type)
201 {
202 case ST_SYM_EXT:
203 case ST_ARG_EXT:
204 continue;
205
206 case ST_CODE:
207 symname = bufp->name.n_strx + stringtab;
208 ms_type = mst_file_text;
bb140953 209 bufp->symbol_value += text_offset;
bfe2f12b
JL
210#ifdef SMASH_TEXT_ADDRESS
211 SMASH_TEXT_ADDRESS (bufp->symbol_value);
212#endif
213
214 check_strange_names:
215 /* Utah GCC 2.5, FSF GCC 2.6 and later generate correct local
216 label prefixes for stabs, constant data, etc. So we need
217 only filter out L$ symbols which are left in due to
218 limitations in how GAS generates SOM relocations.
219
220 When linking in the HPUX C-library the HP linker has
221 the nasty habit of placing section symbols from the literal
222 subspaces in the middle of the program's text. Filter
223 those out as best we can. Check for first and last character
2097152a
JL
224 being '$'.
225
226 And finally, the newer HP compilers emit crud like $PIC_foo$N
227 in some circumstance (PIC code I guess). It's also claimed
228 that they emit D$ symbols too. What stupidity. */
bfe2f12b 229 if ((symname[0] == 'L' && symname[1] == '$')
2097152a
JL
230 || (symname[0] == '$' && symname[strlen(symname) - 1] == '$')
231 || (symname[0] == 'D' && symname[1] == '$')
232 || (strncmp (symname, "$PIC", 4) == 0))
bfe2f12b
JL
233 continue;
234 break;
235
236 case ST_PRI_PROG:
237 case ST_SEC_PROG:
238 case ST_MILLICODE:
239 symname = bufp->name.n_strx + stringtab;
240 ms_type = mst_file_text;
bb140953 241 bufp->symbol_value += text_offset;
bfe2f12b
JL
242#ifdef SMASH_TEXT_ADDRESS
243 SMASH_TEXT_ADDRESS (bufp->symbol_value);
244#endif
245 break;
246
247 case ST_ENTRY:
248 symname = bufp->name.n_strx + stringtab;
249 /* For a dynamic executable, ST_ENTRY symbols are
250 the stubs, while the ST_CODE symbol is the real
251 function. */
252 if (dynamic)
253 ms_type = mst_solib_trampoline;
254 else
255 ms_type = mst_file_text;
bb140953 256 bufp->symbol_value += text_offset;
bfe2f12b
JL
257#ifdef SMASH_TEXT_ADDRESS
258 SMASH_TEXT_ADDRESS (bufp->symbol_value);
259#endif
260 break;
261
262 case ST_STUB:
263 symname = bufp->name.n_strx + stringtab;
264 ms_type = mst_solib_trampoline;
bb140953 265 bufp->symbol_value += text_offset;
bfe2f12b
JL
266#ifdef SMASH_TEXT_ADDRESS
267 SMASH_TEXT_ADDRESS (bufp->symbol_value);
268#endif
269 break;
270
271
272 case ST_DATA:
273 symname = bufp->name.n_strx + stringtab;
506af7a7 274 bufp->symbol_value += data_offset;
bfe2f12b
JL
275 ms_type = mst_file_data;
276 goto check_strange_names;
277
278 default:
279 continue;
280 }
281 break;
282
5dc74605
JL
283 /* This can happen for common symbols when -E is passed to the
284 final link. No idea _why_ that would make the linker force
c149559f
JL
285 common symbols to have an SS_UNSAT scope, but it does.
286
287 This also happens for weak symbols, but their type is
288 ST_DATA. */
5dc74605
JL
289 case SS_UNSAT:
290 switch (bufp->symbol_type)
291 {
292 case ST_STORAGE:
c149559f 293 case ST_DATA:
5dc74605
JL
294 symname = bufp->name.n_strx + stringtab;
295 bufp->symbol_value += data_offset;
296 ms_type = mst_data;
297 break;
298
299 default:
300 continue;
301 }
302 break;
303
bfe2f12b
JL
304 default:
305 continue;
306 }
307
308 if (bufp->name.n_strx > obj_som_stringtab_size (abfd))
309 error ("Invalid symbol data; bad HP string table offset: %d",
310 bufp->name.n_strx);
311
ace4b8d7
FF
312 prim_record_minimal_symbol (symname, bufp->symbol_value, ms_type,
313 objfile);
bfe2f12b 314 }
bfe2f12b
JL
315}
316
317/* Scan and build partial symbols for a symbol file.
318 We have been initialized by a call to som_symfile_init, which
319 currently does nothing.
320
321 SECTION_OFFSETS is a set of offsets to apply to relocate the symbols
322 in each section. This is ignored, as it isn't needed for SOM.
323
324 MAINLINE is true if we are reading the main symbol
325 table (as opposed to a shared lib or dynamically loaded file).
326
327 This function only does the minimum work necessary for letting the
328 user "name" things symbolically; it does not read the entire symtab.
329 Instead, it reads the external and static symbols and puts them in partial
330 symbol tables. When more extensive information is requested of a
331 file, the corresponding partial symbol table is mutated into a full
332 fledged symbol table by going back and reading the symbols
333 for real.
334
335 We look for sections with specific names, to tell us what debug
336 format to look for: FIXME!!!
337
338 somstab_build_psymtabs() handles STABS symbols.
339
340 Note that SOM files have a "minimal" symbol table, which is vaguely
341 reminiscent of a COFF symbol table, but has only the minimal information
342 necessary for linking. We process this also, and use the information to
343 build gdb's minimal symbol table. This gives us some minimal debugging
344 capability even for files compiled without -g. */
345
346static void
347som_symfile_read (objfile, section_offsets, mainline)
348 struct objfile *objfile;
349 struct section_offsets *section_offsets;
350 int mainline;
351{
352 bfd *abfd = objfile->obfd;
353 struct cleanup *back_to;
bfe2f12b
JL
354
355 init_minimal_symbol_collection ();
356 back_to = make_cleanup (discard_minimal_symbols, 0);
357
6a86fa48 358 /* Process the normal SOM symbol table first. */
bfe2f12b 359
bb140953 360 som_symtab_read (abfd, objfile, section_offsets);
bfe2f12b 361
98c0e047 362 /* Now read information from the stabs debug sections. */
6a86fa48
JL
363 stabsect_build_psymtabs (objfile, section_offsets, mainline,
364 "$GDB_SYMBOLS$", "$GDB_STRINGS$", "$TEXT$");
bfe2f12b 365
98c0e047
JL
366 /* Now read the native debug information. */
367 hpread_build_psymtabs (objfile, section_offsets, mainline);
98c0e047 368
6a86fa48
JL
369 /* Install any minimal symbols that have been collected as the current
370 minimal symbols for this objfile. */
371 install_minimal_symbols (objfile);
bfe2f12b 372
31b2518a 373 /* Force hppa-tdep.c to re-read the unwind descriptors. */
c676d827 374 objfile->obj_private = NULL;
6a86fa48 375 do_cleanups (back_to);
bfe2f12b
JL
376}
377
378/* Initialize anything that needs initializing when a completely new symbol
379 file is specified (not just adding some symbols from another file, e.g. a
380 shared library).
381
382 We reinitialize buildsym, since we may be reading stabs from a SOM file. */
383
384static void
385som_new_init (ignore)
386 struct objfile *ignore;
387{
388 stabsread_new_init ();
389 buildsym_new_init ();
390}
391
392/* Perform any local cleanups required when we are done with a particular
393 objfile. I.E, we are in the process of discarding all symbol information
394 for an objfile, freeing up all memory held for it, and unlinking the
395 objfile struct from the global list of known objfiles. */
396
397static void
398som_symfile_finish (objfile)
399 struct objfile *objfile;
400{
401 if (objfile -> sym_stab_info != NULL)
402 {
403 mfree (objfile -> md, objfile -> sym_stab_info);
404 }
98c0e047 405 hpread_symfile_finish (objfile);
bfe2f12b
JL
406}
407
436d4143 408/* SOM specific initialization routine for reading symbols. */
bfe2f12b 409
bfe2f12b 410static void
98c0e047
JL
411som_symfile_init (objfile)
412 struct objfile *objfile;
bfe2f12b 413{
436d4143
JL
414 /* SOM objects may be reordered, so set OBJF_REORDERED. If we
415 find this causes a significant slowdown in gdb then we could
416 set it in the debug symbol readers only when necessary. */
417 objfile->flags |= OBJF_REORDERED;
98c0e047 418 hpread_symfile_init (objfile);
bfe2f12b
JL
419}
420
421/* SOM specific parsing routine for section offsets.
422
423 Plain and simple for now. */
424
425static struct section_offsets *
426som_symfile_offsets (objfile, addr)
427 struct objfile *objfile;
428 CORE_ADDR addr;
429{
430 struct section_offsets *section_offsets;
431 int i;
432
433 objfile->num_sections = SECT_OFF_MAX;
434 section_offsets = (struct section_offsets *)
e74acce4 435 obstack_alloc (&objfile -> psymbol_obstack, SIZEOF_SECTION_OFFSETS);
bfe2f12b 436
506af7a7
JL
437 /* First see if we're a shared library. If so, get the section
438 offsets from the library, else get them from addr. */
439 if (!som_solib_section_offsets (objfile, section_offsets))
440 {
441 for (i = 0; i < SECT_OFF_MAX; i++)
442 ANOFFSET (section_offsets, i) = addr;
443 }
bfe2f12b
JL
444
445 return section_offsets;
446}
447\f
448/* Register that we are able to handle SOM object file formats. */
449
450static struct sym_fns som_sym_fns =
451{
452 bfd_target_som_flavour,
453 som_new_init, /* sym_new_init: init anything gbl to entire symtab */
454 som_symfile_init, /* sym_init: read initial info, setup for sym_read() */
455 som_symfile_read, /* sym_read: read a symbol file into symtab */
456 som_symfile_finish, /* sym_finish: finished with file, cleanup */
457 som_symfile_offsets, /* sym_offsets: Translate ext. to int. relocation */
458 NULL /* next: pointer to next struct sym_fns */
459};
460
461void
462_initialize_somread ()
463{
464 add_symtab_fns (&som_sym_fns);
465}
This page took 0.149618 seconds and 4 git commands to generate.