*** empty log message ***
[deliverable/binutils-gdb.git] / gdb / somread.c
CommitLineData
c906108c 1/* Read HP PA/Risc object files for GDB.
197e01b6 2 Copyright (C) 1991, 1992, 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002,
8b92e4d5 3 2004 Free Software Foundation, Inc.
c906108c
SS
4 Written by Fred Fish at Cygnus Support.
5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
c906108c 12
c5aa993b
JM
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
c906108c 17
c5aa993b
JM
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
197e01b6
EZ
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
c906108c
SS
22
23#include "defs.h"
24#include "bfd.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 "som.h"
36#include "libhppa.h"
37
17fe2d6e 38#include "solib-som.h"
c906108c 39
17fe2d6e 40/* Prototypes for local functions. */
65e82032
AC
41static int init_import_symbols (struct objfile *objfile);
42
c906108c
SS
43/* FIXME: These should really be in a common header somewhere */
44
a14ed312 45extern void hpread_build_psymtabs (struct objfile *, int);
c906108c 46
a14ed312 47extern void hpread_symfile_finish (struct objfile *);
c906108c 48
a14ed312 49extern void hpread_symfile_init (struct objfile *);
c906108c 50
a14ed312 51extern void do_pxdb (bfd *);
c906108c
SS
52
53/*
54
c5aa993b 55 LOCAL FUNCTION
c906108c 56
c5aa993b 57 som_symtab_read -- read the symbol table of a SOM file
c906108c 58
c5aa993b 59 SYNOPSIS
c906108c 60
c5aa993b
JM
61 void som_symtab_read (bfd *abfd, struct objfile *objfile,
62 struct section_offsets *section_offsets)
c906108c 63
c5aa993b 64 DESCRIPTION
c906108c 65
c5aa993b
JM
66 Given an open bfd, a base address to relocate symbols to, and a
67 flag that specifies whether or not this bfd is for an executable
68 or not (may be shared library for example), add all the global
69 function and data symbols to the minimal symbol table.
70 */
c906108c
SS
71
72static void
fba45db2
KB
73som_symtab_read (bfd *abfd, struct objfile *objfile,
74 struct section_offsets *section_offsets)
c906108c
SS
75{
76 unsigned int number_of_symbols;
77 int val, dynamic;
78 char *stringtab;
79 asection *shlib_info;
80 struct symbol_dictionary_record *buf, *bufp, *endbufp;
81 char *symname;
82 CONST int symsize = sizeof (struct symbol_dictionary_record);
83 CORE_ADDR text_offset, data_offset;
84
85
86 text_offset = ANOFFSET (section_offsets, 0);
87 data_offset = ANOFFSET (section_offsets, 1);
88
89 number_of_symbols = bfd_get_symcount (abfd);
90
34c0bd93 91 /* FIXME (alloca): could be quite large. */
c906108c
SS
92 buf = alloca (symsize * number_of_symbols);
93 bfd_seek (abfd, obj_som_sym_filepos (abfd), SEEK_SET);
3a42e9d0 94 val = bfd_bread (buf, symsize * number_of_symbols, abfd);
c906108c 95 if (val != symsize * number_of_symbols)
8a3fe4f8 96 error (_("Couldn't read symbol dictionary!"));
c906108c 97
34c0bd93 98 /* FIXME (alloca): could be quite large. */
c906108c
SS
99 stringtab = alloca (obj_som_stringtab_size (abfd));
100 bfd_seek (abfd, obj_som_str_filepos (abfd), SEEK_SET);
3a42e9d0 101 val = bfd_bread (stringtab, obj_som_stringtab_size (abfd), abfd);
c906108c 102 if (val != obj_som_stringtab_size (abfd))
8a3fe4f8 103 error (_("Can't read in HP string table."));
c906108c
SS
104
105 /* We need to determine if objfile is a dynamic executable (so we
106 can do the right thing for ST_ENTRY vs ST_CODE symbols).
107
108 There's nothing in the header which easily allows us to do
3fa41cdb
JL
109 this.
110
111 This code used to rely upon the existence of a $SHLIB_INFO$
112 section to make this determination. HP claims that it is
113 more accurate to check for a nonzero text offset, but they
114 have not provided any information about why that test is
115 more accurate. */
c906108c
SS
116 dynamic = (text_offset != 0);
117
118 endbufp = buf + number_of_symbols;
119 for (bufp = buf; bufp < endbufp; ++bufp)
120 {
121 enum minimal_symbol_type ms_type;
122
123 QUIT;
124
125 switch (bufp->symbol_scope)
126 {
127 case SS_UNIVERSAL:
128 case SS_EXTERNAL:
129 switch (bufp->symbol_type)
130 {
131 case ST_SYM_EXT:
132 case ST_ARG_EXT:
133 continue;
134
135 case ST_CODE:
136 case ST_PRI_PROG:
137 case ST_SEC_PROG:
138 case ST_MILLICODE:
139 symname = bufp->name.n_strx + stringtab;
140 ms_type = mst_text;
141 bufp->symbol_value += text_offset;
181c1381 142 bufp->symbol_value = SMASH_TEXT_ADDRESS (bufp->symbol_value);
c906108c
SS
143 break;
144
145 case ST_ENTRY:
146 symname = bufp->name.n_strx + stringtab;
147 /* For a dynamic executable, ST_ENTRY symbols are
c5aa993b
JM
148 the stubs, while the ST_CODE symbol is the real
149 function. */
c906108c
SS
150 if (dynamic)
151 ms_type = mst_solib_trampoline;
152 else
153 ms_type = mst_text;
154 bufp->symbol_value += text_offset;
181c1381 155 bufp->symbol_value = SMASH_TEXT_ADDRESS (bufp->symbol_value);
c906108c
SS
156 break;
157
158 case ST_STUB:
159 symname = bufp->name.n_strx + stringtab;
160 ms_type = mst_solib_trampoline;
161 bufp->symbol_value += text_offset;
181c1381 162 bufp->symbol_value = SMASH_TEXT_ADDRESS (bufp->symbol_value);
c906108c
SS
163 break;
164
165 case ST_DATA:
166 symname = bufp->name.n_strx + stringtab;
167 bufp->symbol_value += data_offset;
168 ms_type = mst_data;
169 break;
170 default:
171 continue;
172 }
173 break;
174
175#if 0
176 /* SS_GLOBAL and SS_LOCAL are two names for the same thing (!). */
177 case SS_GLOBAL:
178#endif
179 case SS_LOCAL:
180 switch (bufp->symbol_type)
181 {
182 case ST_SYM_EXT:
183 case ST_ARG_EXT:
184 continue;
185
186 case ST_CODE:
187 symname = bufp->name.n_strx + stringtab;
188 ms_type = mst_file_text;
189 bufp->symbol_value += text_offset;
181c1381 190 bufp->symbol_value = SMASH_TEXT_ADDRESS (bufp->symbol_value);
c906108c
SS
191
192 check_strange_names:
193 /* Utah GCC 2.5, FSF GCC 2.6 and later generate correct local
c5aa993b
JM
194 label prefixes for stabs, constant data, etc. So we need
195 only filter out L$ symbols which are left in due to
196 limitations in how GAS generates SOM relocations.
197
198 When linking in the HPUX C-library the HP linker has
199 the nasty habit of placing section symbols from the literal
200 subspaces in the middle of the program's text. Filter
201 those out as best we can. Check for first and last character
202 being '$'.
203
204 And finally, the newer HP compilers emit crud like $PIC_foo$N
205 in some circumstance (PIC code I guess). It's also claimed
206 that they emit D$ symbols too. What stupidity. */
c906108c 207 if ((symname[0] == 'L' && symname[1] == '$')
c5aa993b 208 || (symname[0] == '$' && symname[strlen (symname) - 1] == '$')
c906108c 209 || (symname[0] == 'D' && symname[1] == '$')
b887c273 210 || (strncmp (symname, "L0\001", 3) == 0)
c906108c
SS
211 || (strncmp (symname, "$PIC", 4) == 0))
212 continue;
213 break;
214
215 case ST_PRI_PROG:
216 case ST_SEC_PROG:
217 case ST_MILLICODE:
218 symname = bufp->name.n_strx + stringtab;
219 ms_type = mst_file_text;
220 bufp->symbol_value += text_offset;
181c1381 221 bufp->symbol_value = SMASH_TEXT_ADDRESS (bufp->symbol_value);
c906108c
SS
222 break;
223
224 case ST_ENTRY:
225 symname = bufp->name.n_strx + stringtab;
3fa41cdb
JL
226 /* SS_LOCAL symbols in a shared library do not have
227 export stubs, so we do not have to worry about
228 using mst_file_text vs mst_solib_trampoline here like
229 we do for SS_UNIVERSAL and SS_EXTERNAL symbols above. */
230 ms_type = mst_file_text;
c906108c 231 bufp->symbol_value += text_offset;
181c1381 232 bufp->symbol_value = SMASH_TEXT_ADDRESS (bufp->symbol_value);
c906108c
SS
233 break;
234
235 case ST_STUB:
236 symname = bufp->name.n_strx + stringtab;
237 ms_type = mst_solib_trampoline;
238 bufp->symbol_value += text_offset;
181c1381 239 bufp->symbol_value = SMASH_TEXT_ADDRESS (bufp->symbol_value);
c906108c
SS
240 break;
241
242
243 case ST_DATA:
244 symname = bufp->name.n_strx + stringtab;
245 bufp->symbol_value += data_offset;
246 ms_type = mst_file_data;
247 goto check_strange_names;
248
249 default:
250 continue;
251 }
252 break;
253
c5aa993b
JM
254 /* This can happen for common symbols when -E is passed to the
255 final link. No idea _why_ that would make the linker force
256 common symbols to have an SS_UNSAT scope, but it does.
c906108c 257
c5aa993b
JM
258 This also happens for weak symbols, but their type is
259 ST_DATA. */
c906108c
SS
260 case SS_UNSAT:
261 switch (bufp->symbol_type)
262 {
c5aa993b
JM
263 case ST_STORAGE:
264 case ST_DATA:
265 symname = bufp->name.n_strx + stringtab;
266 bufp->symbol_value += data_offset;
267 ms_type = mst_data;
268 break;
269
270 default:
271 continue;
c906108c
SS
272 }
273 break;
274
275 default:
276 continue;
277 }
278
279 if (bufp->name.n_strx > obj_som_stringtab_size (abfd))
8a3fe4f8 280 error (_("Invalid symbol data; bad HP string table offset: %d"),
c906108c
SS
281 bufp->name.n_strx);
282
c5aa993b 283 prim_record_minimal_symbol (symname, bufp->symbol_value, ms_type,
c906108c
SS
284 objfile);
285 }
286}
287
288/* Scan and build partial symbols for a symbol file.
289 We have been initialized by a call to som_symfile_init, which
290 currently does nothing.
291
292 SECTION_OFFSETS is a set of offsets to apply to relocate the symbols
293 in each section. This is ignored, as it isn't needed for SOM.
294
295 MAINLINE is true if we are reading the main symbol
296 table (as opposed to a shared lib or dynamically loaded file).
297
298 This function only does the minimum work necessary for letting the
299 user "name" things symbolically; it does not read the entire symtab.
300 Instead, it reads the external and static symbols and puts them in partial
301 symbol tables. When more extensive information is requested of a
302 file, the corresponding partial symbol table is mutated into a full
303 fledged symbol table by going back and reading the symbols
304 for real.
305
306 We look for sections with specific names, to tell us what debug
307 format to look for: FIXME!!!
308
309 somstab_build_psymtabs() handles STABS symbols.
310
311 Note that SOM files have a "minimal" symbol table, which is vaguely
312 reminiscent of a COFF symbol table, but has only the minimal information
313 necessary for linking. We process this also, and use the information to
314 build gdb's minimal symbol table. This gives us some minimal debugging
315 capability even for files compiled without -g. */
316
317static void
fba45db2 318som_symfile_read (struct objfile *objfile, int mainline)
c906108c
SS
319{
320 bfd *abfd = objfile->obfd;
321 struct cleanup *back_to;
322
323 do_pxdb (symfile_bfd_open (objfile->name));
324
325 init_minimal_symbol_collection ();
56e290f4 326 back_to = make_cleanup_discard_minimal_symbols ();
c906108c
SS
327
328 /* Read in the import list and the export list. Currently
329 the export list isn't used; the import list is used in
330 hp-symtab-read.c to handle static vars declared in other
331 shared libraries. */
332 init_import_symbols (objfile);
c5aa993b 333#if 0 /* Export symbols not used today 1997-08-05 */
c906108c
SS
334 init_export_symbols (objfile);
335#else
336 objfile->export_list = NULL;
337 objfile->export_list_size = 0;
338#endif
339
340 /* Process the normal SOM symbol table first.
341 This reads in the DNTT and string table, but doesn't
342 actually scan the DNTT. It does scan the linker symbol
343 table and thus build up a "minimal symbol table". */
c5aa993b 344
96baa820 345 som_symtab_read (abfd, objfile, objfile->section_offsets);
c906108c 346
7134143f
DJ
347 /* Install any minimal symbols that have been collected as the current
348 minimal symbols for this objfile.
349 Further symbol-reading is done incrementally, file-by-file,
350 in a step known as "psymtab-to-symtab" expansion. hp-symtab-read.c
351 contains the code to do the actual DNTT scanning and symtab building. */
352 install_minimal_symbols (objfile);
353 do_cleanups (back_to);
354
c906108c 355 /* Now read information from the stabs debug sections.
4897bfb9 356 This is emitted by gcc. */
96baa820 357 stabsect_build_psymtabs (objfile, mainline,
c906108c
SS
358 "$GDB_SYMBOLS$", "$GDB_STRINGS$", "$TEXT$");
359
360 /* Now read the native debug information.
361 This builds the psymtab. This used to be done via a scan of
362 the DNTT, but is now done via the PXDB-built quick-lookup tables
363 together with a scan of the GNTT. See hp-psymtab-read.c. */
d4f3574e 364 hpread_build_psymtabs (objfile, mainline);
c906108c
SS
365}
366
367/* Initialize anything that needs initializing when a completely new symbol
368 file is specified (not just adding some symbols from another file, e.g. a
369 shared library).
370
371 We reinitialize buildsym, since we may be reading stabs from a SOM file. */
372
373static void
fba45db2 374som_new_init (struct objfile *ignore)
c906108c
SS
375{
376 stabsread_new_init ();
377 buildsym_new_init ();
378}
379
380/* Perform any local cleanups required when we are done with a particular
381 objfile. I.E, we are in the process of discarding all symbol information
382 for an objfile, freeing up all memory held for it, and unlinking the
383 objfile struct from the global list of known objfiles. */
384
385static void
fba45db2 386som_symfile_finish (struct objfile *objfile)
c906108c 387{
0a6ddd08 388 if (objfile->deprecated_sym_stab_info != NULL)
c906108c 389 {
0a6ddd08 390 xfree (objfile->deprecated_sym_stab_info);
c906108c
SS
391 }
392 hpread_symfile_finish (objfile);
393}
394
395/* SOM specific initialization routine for reading symbols. */
396
397static void
fba45db2 398som_symfile_init (struct objfile *objfile)
c906108c
SS
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
d4f3574e 411static void
fba45db2 412som_symfile_offsets (struct objfile *objfile, struct section_addr_info *addrs)
c906108c 413{
c906108c 414 int i;
0aa9cf96 415 CORE_ADDR text_addr;
c906108c 416
a39a16c4 417 objfile->num_sections = bfd_count_sections (objfile->obfd);
d4f3574e 418 objfile->section_offsets = (struct section_offsets *)
8b92e4d5 419 obstack_alloc (&objfile->objfile_obstack,
a39a16c4 420 SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
c906108c 421
b8fbeb18
EZ
422 /* FIXME: ezannoni 2000-04-20 The section names in SOM are not
423 .text, .data, etc, but $TEXT$, $DATA$,... We should initialize
424 SET_OFF_* from bfd. (See default_symfile_offsets()). But I don't
425 know the correspondence between SOM sections and GDB's idea of
426 section names. So for now we default to what is was before these
427 changes.*/
428 objfile->sect_index_text = 0;
429 objfile->sect_index_data = 1;
430 objfile->sect_index_bss = 2;
431 objfile->sect_index_rodata = 3;
432
c906108c 433 /* First see if we're a shared library. If so, get the section
2acceee2 434 offsets from the library, else get them from addrs. */
d4f3574e 435 if (!som_solib_section_offsets (objfile, objfile->section_offsets))
c906108c 436 {
b8fbeb18
EZ
437 /* Note: Here is OK to compare with ".text" because this is the
438 name that gdb itself gives to that section, not the SOM
439 name. */
8d498949 440 for (i = 0; i < addrs->num_sections && addrs->other[i].name; i++)
0aa9cf96
EZ
441 if (strcmp (addrs->other[i].name, ".text") == 0)
442 break;
443 text_addr = addrs->other[i].addr;
444
a39a16c4 445 for (i = 0; i < objfile->num_sections; i++)
f0a58b0b 446 (objfile->section_offsets)->offsets[i] = text_addr;
c906108c 447 }
c906108c
SS
448}
449
c906108c
SS
450/* Read in and initialize the SOM import list which is present
451 for all executables and shared libraries. The import list
452 consists of the symbols that are referenced in OBJFILE but
453 not defined there. (Variables that are imported are dealt
454 with as "loc_indirect" vars.)
455 Return value = number of import symbols read in. */
65e82032 456static int
fba45db2 457init_import_symbols (struct objfile *objfile)
c906108c
SS
458{
459 unsigned int import_list;
460 unsigned int import_list_size;
461 unsigned int string_table;
462 unsigned int string_table_size;
c5aa993b 463 char *string_buffer;
52f0bd74
AC
464 int i;
465 int j;
466 int k;
c5aa993b
JM
467 asection *text_section; /* section handle */
468 unsigned int dl_header[12]; /* SOM executable header */
c906108c
SS
469
470 /* A struct for an entry in the SOM import list */
c5aa993b
JM
471 typedef struct
472 {
473 int name; /* index into the string table */
474 short dont_care1; /* we don't use this */
475 unsigned char type; /* 0 = NULL, 2 = Data, 3 = Code, 7 = Storage, 13 = Plabel */
476 unsigned int reserved2:8; /* not used */
477 }
478 SomImportEntry;
479
480 /* We read 100 entries in at a time from the disk file. */
481#define SOM_READ_IMPORTS_NUM 100
482#define SOM_READ_IMPORTS_CHUNK_SIZE (sizeof (SomImportEntry) * SOM_READ_IMPORTS_NUM)
c906108c 483 SomImportEntry buffer[SOM_READ_IMPORTS_NUM];
c5aa993b 484
c906108c
SS
485 /* Initialize in case we error out */
486 objfile->import_list = NULL;
487 objfile->import_list_size = 0;
488
c906108c 489 /* It doesn't work, for some reason, to read in space $TEXT$;
c5aa993b 490 the subspace $SHLIB_INFO$ has to be used. Some BFD quirk? pai/1997-08-05 */
c906108c
SS
491 text_section = bfd_get_section_by_name (objfile->obfd, "$SHLIB_INFO$");
492 if (!text_section)
493 return 0;
c5aa993b 494 /* Get the SOM executable header */
c906108c
SS
495 bfd_get_section_contents (objfile->obfd, text_section, dl_header, 0, 12 * sizeof (int));
496
497 /* Check header version number for 10.x HP-UX */
498 /* Currently we deal only with 10.x systems; on 9.x the version # is 89060912.
c5aa993b 499 FIXME: Change for future HP-UX releases and mods to the SOM executable format */
c906108c
SS
500 if (dl_header[0] != 93092112)
501 return 0;
c5aa993b
JM
502
503 import_list = dl_header[4];
c906108c
SS
504 import_list_size = dl_header[5];
505 if (!import_list_size)
506 return 0;
c5aa993b 507 string_table = dl_header[10];
c906108c
SS
508 string_table_size = dl_header[11];
509 if (!string_table_size)
510 return 0;
511
c5aa993b 512 /* Suck in SOM string table */
c906108c
SS
513 string_buffer = (char *) xmalloc (string_table_size);
514 bfd_get_section_contents (objfile->obfd, text_section, string_buffer,
c5aa993b 515 string_table, string_table_size);
c906108c
SS
516
517 /* Allocate import list in the psymbol obstack; this has nothing
518 to do with psymbols, just a matter of convenience. We want the
c5aa993b 519 import list to be freed when the objfile is deallocated */
c906108c 520 objfile->import_list
8b92e4d5 521 = (ImportEntry *) obstack_alloc (&objfile->objfile_obstack,
c5aa993b 522 import_list_size * sizeof (ImportEntry));
c906108c 523
c5aa993b
JM
524 /* Read in the import entries, a bunch at a time */
525 for (j = 0, k = 0;
c906108c
SS
526 j < (import_list_size / SOM_READ_IMPORTS_NUM);
527 j++)
528 {
529 bfd_get_section_contents (objfile->obfd, text_section, buffer,
c5aa993b
JM
530 import_list + j * SOM_READ_IMPORTS_CHUNK_SIZE,
531 SOM_READ_IMPORTS_CHUNK_SIZE);
532 for (i = 0; i < SOM_READ_IMPORTS_NUM; i++, k++)
533 {
534 if (buffer[i].type != (unsigned char) 0)
535 {
536 objfile->import_list[k]
8b92e4d5 537 = (char *) obstack_alloc (&objfile->objfile_obstack, strlen (string_buffer + buffer[i].name) + 1);
c5aa993b
JM
538 strcpy (objfile->import_list[k], string_buffer + buffer[i].name);
539 /* Some day we might want to record the type and other information too */
540 }
541 else /* null type */
542 objfile->import_list[k] = NULL;
543
544 }
c906108c
SS
545 }
546
c5aa993b 547 /* Get the leftovers */
c906108c
SS
548 if (k < import_list_size)
549 bfd_get_section_contents (objfile->obfd, text_section, buffer,
c5aa993b
JM
550 import_list + k * sizeof (SomImportEntry),
551 (import_list_size - k) * sizeof (SomImportEntry));
552 for (i = 0; k < import_list_size; i++, k++)
c906108c
SS
553 {
554 if (buffer[i].type != (unsigned char) 0)
c5aa993b
JM
555 {
556 objfile->import_list[k]
8b92e4d5 557 = (char *) obstack_alloc (&objfile->objfile_obstack, strlen (string_buffer + buffer[i].name) + 1);
c5aa993b
JM
558 strcpy (objfile->import_list[k], string_buffer + buffer[i].name);
559 /* Some day we might want to record the type and other information too */
560 }
c906108c 561 else
c5aa993b 562 objfile->import_list[k] = NULL;
c906108c
SS
563 }
564
565 objfile->import_list_size = import_list_size;
b8c9b27d 566 xfree (string_buffer);
c906108c
SS
567 return import_list_size;
568}
569
570/* Read in and initialize the SOM export list which is present
571 for all executables and shared libraries. The import list
572 consists of the symbols that are referenced in OBJFILE but
573 not defined there. (Variables that are imported are dealt
574 with as "loc_indirect" vars.)
575 Return value = number of import symbols read in. */
576int
fba45db2 577init_export_symbols (struct objfile *objfile)
c906108c
SS
578{
579 unsigned int export_list;
580 unsigned int export_list_size;
581 unsigned int string_table;
582 unsigned int string_table_size;
c5aa993b 583 char *string_buffer;
52f0bd74
AC
584 int i;
585 int j;
586 int k;
c5aa993b
JM
587 asection *text_section; /* section handle */
588 unsigned int dl_header[12]; /* SOM executable header */
c906108c
SS
589
590 /* A struct for an entry in the SOM export list */
c5aa993b
JM
591 typedef struct
592 {
593 int next; /* for hash table use -- we don't use this */
594 int name; /* index into string table */
595 int value; /* offset or plabel */
596 int dont_care1; /* not used */
597 unsigned char type; /* 0 = NULL, 2 = Data, 3 = Code, 7 = Storage, 13 = Plabel */
598 char dont_care2; /* not used */
599 short dont_care3; /* not used */
600 }
601 SomExportEntry;
602
603 /* We read 100 entries in at a time from the disk file. */
604#define SOM_READ_EXPORTS_NUM 100
605#define SOM_READ_EXPORTS_CHUNK_SIZE (sizeof (SomExportEntry) * SOM_READ_EXPORTS_NUM)
c906108c
SS
606 SomExportEntry buffer[SOM_READ_EXPORTS_NUM];
607
608 /* Initialize in case we error out */
609 objfile->export_list = NULL;
610 objfile->export_list_size = 0;
611
c906108c 612 /* It doesn't work, for some reason, to read in space $TEXT$;
c5aa993b 613 the subspace $SHLIB_INFO$ has to be used. Some BFD quirk? pai/1997-08-05 */
c906108c
SS
614 text_section = bfd_get_section_by_name (objfile->obfd, "$SHLIB_INFO$");
615 if (!text_section)
616 return 0;
c5aa993b 617 /* Get the SOM executable header */
c906108c
SS
618 bfd_get_section_contents (objfile->obfd, text_section, dl_header, 0, 12 * sizeof (int));
619
620 /* Check header version number for 10.x HP-UX */
621 /* Currently we deal only with 10.x systems; on 9.x the version # is 89060912.
c5aa993b 622 FIXME: Change for future HP-UX releases and mods to the SOM executable format */
c906108c
SS
623 if (dl_header[0] != 93092112)
624 return 0;
c5aa993b
JM
625
626 export_list = dl_header[8];
627 export_list_size = dl_header[9];
c906108c
SS
628 if (!export_list_size)
629 return 0;
c5aa993b 630 string_table = dl_header[10];
c906108c
SS
631 string_table_size = dl_header[11];
632 if (!string_table_size)
633 return 0;
634
c5aa993b 635 /* Suck in SOM string table */
c906108c
SS
636 string_buffer = (char *) xmalloc (string_table_size);
637 bfd_get_section_contents (objfile->obfd, text_section, string_buffer,
c5aa993b 638 string_table, string_table_size);
c906108c
SS
639
640 /* Allocate export list in the psymbol obstack; this has nothing
641 to do with psymbols, just a matter of convenience. We want the
c5aa993b 642 export list to be freed when the objfile is deallocated */
c906108c 643 objfile->export_list
8b92e4d5 644 = (ExportEntry *) obstack_alloc (&objfile->objfile_obstack,
c5aa993b 645 export_list_size * sizeof (ExportEntry));
c906108c 646
c5aa993b
JM
647 /* Read in the export entries, a bunch at a time */
648 for (j = 0, k = 0;
c906108c
SS
649 j < (export_list_size / SOM_READ_EXPORTS_NUM);
650 j++)
651 {
652 bfd_get_section_contents (objfile->obfd, text_section, buffer,
c5aa993b
JM
653 export_list + j * SOM_READ_EXPORTS_CHUNK_SIZE,
654 SOM_READ_EXPORTS_CHUNK_SIZE);
655 for (i = 0; i < SOM_READ_EXPORTS_NUM; i++, k++)
656 {
657 if (buffer[i].type != (unsigned char) 0)
658 {
659 objfile->export_list[k].name
8b92e4d5 660 = (char *) obstack_alloc (&objfile->objfile_obstack, strlen (string_buffer + buffer[i].name) + 1);
c5aa993b
JM
661 strcpy (objfile->export_list[k].name, string_buffer + buffer[i].name);
662 objfile->export_list[k].address = buffer[i].value;
663 /* Some day we might want to record the type and other information too */
664 }
665 else
666 /* null type */
667 {
668 objfile->export_list[k].name = NULL;
669 objfile->export_list[k].address = 0;
670 }
671 }
c906108c
SS
672 }
673
c5aa993b 674 /* Get the leftovers */
c906108c
SS
675 if (k < export_list_size)
676 bfd_get_section_contents (objfile->obfd, text_section, buffer,
c5aa993b
JM
677 export_list + k * sizeof (SomExportEntry),
678 (export_list_size - k) * sizeof (SomExportEntry));
679 for (i = 0; k < export_list_size; i++, k++)
c906108c
SS
680 {
681 if (buffer[i].type != (unsigned char) 0)
c5aa993b
JM
682 {
683 objfile->export_list[k].name
8b92e4d5 684 = (char *) obstack_alloc (&objfile->objfile_obstack, strlen (string_buffer + buffer[i].name) + 1);
c5aa993b
JM
685 strcpy (objfile->export_list[k].name, string_buffer + buffer[i].name);
686 /* Some day we might want to record the type and other information too */
687 objfile->export_list[k].address = buffer[i].value;
688 }
c906108c 689 else
c5aa993b
JM
690 {
691 objfile->export_list[k].name = NULL;
692 objfile->export_list[k].address = 0;
693 }
c906108c
SS
694 }
695
696 objfile->export_list_size = export_list_size;
b8c9b27d 697 xfree (string_buffer);
c906108c
SS
698 return export_list_size;
699}
c5aa993b 700\f
c906108c
SS
701
702
c906108c
SS
703/* Register that we are able to handle SOM object file formats. */
704
705static struct sym_fns som_sym_fns =
706{
707 bfd_target_som_flavour,
c5aa993b
JM
708 som_new_init, /* sym_new_init: init anything gbl to entire symtab */
709 som_symfile_init, /* sym_init: read initial info, setup for sym_read() */
710 som_symfile_read, /* sym_read: read a symbol file into symtab */
711 som_symfile_finish, /* sym_finish: finished with file, cleanup */
712 som_symfile_offsets, /* sym_offsets: Translate ext. to int. relocation */
713 NULL /* next: pointer to next struct sym_fns */
c906108c
SS
714};
715
716void
fba45db2 717_initialize_somread (void)
c906108c
SS
718{
719 add_symtab_fns (&som_sym_fns);
720}
This page took 0.649107 seconds and 4 git commands to generate.