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