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