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