* corelow.c, exec.c, inftarg.c, m3-nat.c, op50-rom.c, procfs.c,
[deliverable/binutils-gdb.git] / gdb / coffread.c
CommitLineData
bd5635a1 1/* Read coff symbol tables and convert to internal format, for GDB.
7f4c8595
SS
2 Copyright 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994
3 Free Software Foundation, Inc.
d8ce1326 4 Contributed by David D. Johnson, Brown University (ddj@cs.brown.edu).
bd5635a1
RP
5
6This file is part of GDB.
7
99a7de40 8This program is free software; you can redistribute it and/or modify
bd5635a1 9it under the terms of the GNU General Public License as published by
99a7de40
JG
10the Free Software Foundation; either version 2 of the License, or
11(at your option) any later version.
bd5635a1 12
99a7de40 13This program is distributed in the hope that it will be useful,
bd5635a1
RP
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
99a7de40
JG
19along with this program; if not, write to the Free Software
20Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
f2c365f5 21
bd5635a1 22#include "defs.h"
bd5635a1 23#include "symtab.h"
318bf84f 24#include "gdbtypes.h"
bd5635a1 25#include "breakpoint.h"
27ad511f 26
bd5635a1 27#include "bfd.h"
bd5635a1 28#include <obstack.h>
318bf84f 29
bd5635a1
RP
30#include <string.h>
31
f5f0679a 32#include "coff/internal.h" /* Internal format of COFF symbols in BFD */
63989338
JG
33#include "libcoff.h" /* FIXME secret internal data from BFD */
34
27ad511f
JK
35#include "symfile.h"
36#include "objfiles.h"
37#include "buildsym.h"
38#include "gdb-stabs.h"
39#include "stabsread.h"
40#include "complaints.h"
3a470454 41#include "target.h"
27ad511f 42
b5b186a2 43struct coff_symfile_info {
8f6960c9
JK
44 file_ptr min_lineno_offset; /* Where in file lowest line#s are */
45 file_ptr max_lineno_offset; /* 1+last byte of line#s in file */
b5b186a2
SS
46
47 asection *stabsect; /* Section pointer for .stab section */
48 asection *stabstrsect; /* Section pointer for .stab section */
49 asection *stabindexsect; /* Section pointer for .stab.index section */
50 char *stabstrdata;
b5b186a2
SS
51};
52
de9bef49
JG
53/* Translate an external name string into a user-visible name. */
54#define EXTERNAL_NAME(string, abfd) \
55 (string[0] == bfd_get_symbol_leading_char(abfd)? string+1: string)
56
8714ff35
JK
57/* To be an sdb debug type, type must have at least a basic or primary
58 derived type. Using this rather than checking against T_NULL is
ab8f22a9
JK
59 said to prevent core dumps if we try to operate on Michael Bloom
60 dbx-in-coff file. */
8714ff35
JK
61
62#define SDB_TYPE(type) (BTYPE(type) | (type & N_TMASK))
63
7f4c8595
SS
64/* Convert from an sdb register number to an internal gdb register number.
65 This should be defined in tm.h, if REGISTER_NAMES is not set up
66 to map one to one onto the sdb register numbers. */
67
d8ce1326
JG
68#ifndef SDB_REG_TO_REGNUM
69# define SDB_REG_TO_REGNUM(value) (value)
70#endif
71
bd5635a1
RP
72/* Core address of start and end of text of current source file.
73 This comes from a ".text" symbol where x_nlinno > 0. */
74
7f4c8595
SS
75static CORE_ADDR current_source_start_addr;
76static CORE_ADDR current_source_end_addr;
bd5635a1 77
bd5635a1
RP
78/* The addresses of the symbol table stream and number of symbols
79 of the object file we are reading (as copied into core). */
80
7f4c8595 81static bfd *nlist_bfd_global;
bd5635a1
RP
82static int nlist_nsyms_global;
83
bd5635a1
RP
84/* Vector of line number information. */
85
86static struct linetable *line_vector;
87
88/* Index of next entry to go in line_vector_index. */
89
90static int line_vector_index;
91
92/* Last line number recorded in the line vector. */
93
94static int prev_line_number;
95
96/* Number of elements allocated for line_vector currently. */
97
98static int line_vector_length;
99
d8ce1326
JG
100/* Pointers to scratch storage, used for reading raw symbols and auxents. */
101
102static char *temp_sym;
103static char *temp_aux;
104
105/* Local variables that hold the shift and mask values for the
106 COFF file that we are currently reading. These come back to us
107 from BFD, and are referenced by their macro names, as well as
108 internally to the BTYPE, ISPTR, ISFCN, ISARY, ISTAG, and DECREF
7f4c8595 109 macros from include/coff/internal.h . */
d8ce1326
JG
110
111static unsigned local_n_btmask;
112static unsigned local_n_btshft;
113static unsigned local_n_tmask;
114static unsigned local_n_tshift;
115
116#define N_BTMASK local_n_btmask
117#define N_BTSHFT local_n_btshft
118#define N_TMASK local_n_tmask
119#define N_TSHIFT local_n_tshift
120
121/* Local variables that hold the sizes in the file of various COFF structures.
122 (We only need to know this to read them from the file -- BFD will then
123 translate the data in them, into `internal_xxx' structs in the right
124 byte order, alignment, etc.) */
125
126static unsigned local_linesz;
127static unsigned local_symesz;
128static unsigned local_auxesz;
129
bd5635a1
RP
130/* Chain of typedefs of pointers to empty struct/union types.
131 They are chained thru the SYMBOL_VALUE_CHAIN. */
132
bd5635a1
RP
133static struct symbol *opaque_type_chain[HASHSIZE];
134
d8ce1326
JG
135#if 0
136/* The type of the function we are currently reading in. This is
137 used by define_symbol to record the type of arguments to a function. */
138
139struct type *in_function_type;
140#endif
141
bd5635a1
RP
142/* Complaints about various problems in the file being read */
143
144struct complaint ef_complaint =
145 {"Unmatched .ef symbol(s) ignored starting at symnum %d", 0, 0};
146
7e258d18
PB
147struct complaint bf_no_aux_complaint =
148 {"`.bf' symbol %d has no aux entry", 0, 0};
149
150struct complaint ef_no_aux_complaint =
151 {"`.ef' symbol %d has no aux entry", 0, 0};
e64fbb3a 152
63989338
JG
153struct complaint lineno_complaint =
154 {"Line number pointer %d lower than start of line numbers", 0, 0};
155
7e258d18
PB
156struct complaint unexpected_type_complaint =
157 {"Unexpected type for symbol %s", 0, 0};
158
159struct complaint bad_sclass_complaint =
160 {"Bad n_sclass for symbol %s", 0, 0};
318bf84f 161
f70be3e4
JG
162struct complaint misordered_blocks_complaint =
163 {"Blocks out of order at address %x", 0, 0};
164
165struct complaint tagndx_bad_complaint =
166 {"Symbol table entry for %s has bad tagndx value", 0, 0};
167
de9bef49
JG
168struct complaint eb_complaint =
169 {"Mismatched .eb symbol ignored starting at symnum %d", 0, 0};
170
318bf84f
FF
171/* Simplified internal version of coff symbol table information */
172
173struct coff_symbol {
174 char *c_name;
175 int c_symnum; /* symbol number of this entry */
176 int c_naux; /* 0 if syment only, 1 if syment + auxent, etc */
177 long c_value;
178 int c_sclass;
179 int c_secnum;
180 unsigned int c_type;
181};
182
7f4c8595 183static struct type *coff_read_struct_type PARAMS ((int, int, int));
318bf84f 184
7f4c8595
SS
185static struct type *decode_base_type PARAMS ((struct coff_symbol *,
186 unsigned int,
187 union internal_auxent *));
318bf84f 188
7f4c8595
SS
189static struct type *decode_type PARAMS ((struct coff_symbol *, unsigned int,
190 union internal_auxent *));
318bf84f 191
7f4c8595
SS
192static struct type *decode_function_type PARAMS ((struct coff_symbol *,
193 unsigned int,
194 union internal_auxent *));
318bf84f 195
7f4c8595 196static struct type *coff_read_enum_type PARAMS ((int, int, int));
318bf84f 197
7f4c8595
SS
198static struct symbol *process_coff_symbol PARAMS ((struct coff_symbol *,
199 union internal_auxent *,
200 struct section_offsets *,
201 struct objfile *));
318bf84f 202
7f4c8595 203static void patch_opaque_types PARAMS ((struct symtab *));
318bf84f 204
7f4c8595 205static void patch_type PARAMS ((struct type *, struct type *));
318bf84f 206
7f4c8595 207static void enter_linenos PARAMS ((long, int, int, struct section_offsets *));
318bf84f 208
7f4c8595 209static void free_linetab PARAMS ((void));
e09c5e26 210
7f4c8595 211static int init_lineno PARAMS ((bfd *, long, int));
318bf84f 212
7f4c8595 213static char *getsymname PARAMS ((struct internal_syment *));
318bf84f 214
7f4c8595 215static void free_stringtab PARAMS ((void));
318bf84f 216
7f4c8595 217static int init_stringtab PARAMS ((bfd *, long));
318bf84f 218
7f4c8595
SS
219static void read_one_sym PARAMS ((struct coff_symbol *,
220 struct internal_syment *,
221 union internal_auxent *));
318bf84f 222
7f4c8595
SS
223static void coff_symtab_read PARAMS ((long, int, struct section_offsets *,
224 struct objfile *));
318bf84f 225
7f4c8595 226static void find_linenos PARAMS ((bfd *, sec_ptr, PTR));
318bf84f 227
7f4c8595 228static void coff_symfile_init PARAMS ((struct objfile *));
318bf84f 229
7f4c8595 230static void coff_new_init PARAMS ((struct objfile *));
318bf84f 231
7f4c8595
SS
232static void coff_symfile_read PARAMS ((struct objfile *,
233 struct section_offsets *, int));
80d68b1d 234
7f4c8595 235static void coff_symfile_finish PARAMS ((struct objfile *));
318bf84f 236
7f4c8595
SS
237static void record_minimal_symbol PARAMS ((char *, CORE_ADDR,
238 enum minimal_symbol_type,
239 struct objfile *));
318bf84f 240
7f4c8595 241static void coff_end_symtab PARAMS ((struct objfile *));
318bf84f 242
7f4c8595 243static void complete_symtab PARAMS ((char *, CORE_ADDR, unsigned int));
318bf84f 244
7f4c8595 245static void coff_start_symtab PARAMS ((void));
318bf84f 246
7f4c8595 247static void coff_record_line PARAMS ((int, CORE_ADDR));
318bf84f 248
7f4c8595 249static struct type *coff_alloc_type PARAMS ((int));
318bf84f 250
7f4c8595 251static struct type **coff_lookup_type PARAMS ((int));
318bf84f 252
7f4c8595 253static void coff_locate_sections PARAMS ((bfd *, asection *, PTR));
bd5635a1 254\f
b5b186a2
SS
255/* We are called once per section from coff_symfile_read. We
256 need to examine each section we are passed, check to see
257 if it is something we are interested in processing, and
258 if so, stash away some access information for the section.
259
0ccbaadc 260 FIXME: The section names should not be hardwired strings (what
8f6960c9
JK
261 should they be? I don't think most object file formats have enough
262 section flags to specify what kind of debug section it is
263 -kingdon). */
b5b186a2
SS
264
265static void
266coff_locate_sections (ignore_abfd, sectp, csip)
267 bfd *ignore_abfd;
268 asection *sectp;
269 PTR csip;
270{
271 register struct coff_symfile_info *csi;
272
273 csi = (struct coff_symfile_info *) csip;
274 if (STREQ (sectp->name, ".stab"))
275 {
276 csi->stabsect = sectp;
277 }
278 else if (STREQ (sectp->name, ".stabstr"))
279 {
280 csi->stabstrsect = sectp;
281 }
282 else if (STREQ (sectp->name, ".stab.index"))
283 {
284 csi->stabindexsect = sectp;
285 }
286}
287
e7b5942b
JK
288/* Return the section_offsets* that CS points to. */
289static int cs_to_section PARAMS ((struct coff_symbol *, struct objfile *));
290
291struct find_targ_sec_arg {
292 int targ_index;
293 int *resultp;
294};
295
296static void find_targ_sec PARAMS ((bfd *, asection *, void *));
297
298static void find_targ_sec (abfd, sect, obj)
299 bfd *abfd;
300 asection *sect;
301 PTR obj;
302{
303 struct find_targ_sec_arg *args = (struct find_targ_sec_arg *)obj;
304 if (sect->target_index == args->targ_index)
305 {
306 /* This is the section. Figure out what SECT_OFF_* code it is. */
307 if (bfd_get_section_flags (abfd, sect) & SEC_CODE)
308 *args->resultp = SECT_OFF_TEXT;
309 else if (bfd_get_section_flags (abfd, sect) & SEC_LOAD)
310 *args->resultp = SECT_OFF_DATA;
311 else
312 *args->resultp = SECT_OFF_BSS;
313 }
314}
315
316/* Return the section number (SECT_OFF_*) that CS points to. */
317static int
318cs_to_section (cs, objfile)
319 struct coff_symbol *cs;
320 struct objfile *objfile;
321{
322 int off = SECT_OFF_TEXT;
323 struct find_targ_sec_arg args;
324 args.targ_index = cs->c_secnum;
325 args.resultp = &off;
326 bfd_map_over_sections (objfile->obfd, find_targ_sec, &args);
327 return off;
328}
329
bd5635a1
RP
330/* Look up a coff type-number index. Return the address of the slot
331 where the type for that index is stored.
332 The type-number is in INDEX.
333
334 This can be used for finding the type associated with that index
335 or for associating a new type with the index. */
336
337static struct type **
338coff_lookup_type (index)
339 register int index;
340{
341 if (index >= type_vector_length)
342 {
343 int old_vector_length = type_vector_length;
344
345 type_vector_length *= 2;
7f4c8595 346 if (index /* is still */ >= type_vector_length)
bd5635a1 347 type_vector_length = index * 2;
7f4c8595 348
159a075e 349 type_vector = (struct type **)
318bf84f
FF
350 xrealloc ((char *) type_vector,
351 type_vector_length * sizeof (struct type *));
4ed3a9ea 352 memset (&type_vector[old_vector_length], 0,
bd5635a1
RP
353 (type_vector_length - old_vector_length) * sizeof(struct type *));
354 }
159a075e 355 return &type_vector[index];
bd5635a1
RP
356}
357
358/* Make sure there is a type allocated for type number index
359 and return the type object.
360 This can create an empty (zeroed) type object. */
361
362static struct type *
363coff_alloc_type (index)
364 int index;
365{
366 register struct type **type_addr = coff_lookup_type (index);
367 register struct type *type = *type_addr;
368
369 /* If we are referring to a type not known at all yet,
370 allocate an empty type for it.
371 We will fill it in later if we find out how. */
318bf84f 372 if (type == NULL)
bd5635a1 373 {
318bf84f 374 type = alloc_type (current_objfile);
bd5635a1
RP
375 *type_addr = type;
376 }
377 return type;
378}
379\f
0ccbaadc
JK
380/* Record a line number entry for line LINE at address PC.
381 FIXME: Use record_line instead. */
382
e1ce8aa5 383static void
318bf84f 384coff_record_line (line, pc)
bd5635a1
RP
385 int line;
386 CORE_ADDR pc;
387{
388 struct linetable_entry *e;
389 /* Make sure line vector is big enough. */
390
391 if (line_vector_index + 2 >= line_vector_length)
392 {
393 line_vector_length *= 2;
394 line_vector = (struct linetable *)
318bf84f 395 xrealloc ((char *) line_vector, sizeof (struct linetable)
bd5635a1
RP
396 + (line_vector_length
397 * sizeof (struct linetable_entry)));
398 }
399
400 e = line_vector->item + line_vector_index++;
401 e->line = line; e->pc = pc;
402}
403\f
404/* Start a new symtab for a new source file.
405 This is called when a COFF ".file" symbol is seen;
406 it indicates the start of data for one original source file. */
407
408static void
318bf84f 409coff_start_symtab ()
bd5635a1 410{
c438b3af 411 start_symtab (
ab93997d
JK
412 /* We fill in the filename later. start_symtab puts
413 this pointer into last_source_file and we put it in
414 subfiles->name, which end_symtab frees; that's why
415 it must be malloc'd. */
3ffb74b9 416 savestring ("", 0),
c438b3af
JK
417 /* We never know the directory name for COFF. */
418 NULL,
419 /* The start address is irrelevant, since we set
420 last_source_start_addr in coff_end_symtab. */
421 0);
bd5635a1 422
cadbb07a 423 /* Initialize the source file line number information for this file. */
bd5635a1 424
cadbb07a 425 if (line_vector) /* Unlikely, but maybe possible? */
f70be3e4 426 free ((PTR)line_vector);
bd5635a1
RP
427 line_vector_index = 0;
428 line_vector_length = 1000;
429 prev_line_number = -2; /* Force first line number to be explicit */
430 line_vector = (struct linetable *)
431 xmalloc (sizeof (struct linetable)
432 + line_vector_length * sizeof (struct linetable_entry));
433}
434
435/* Save the vital information from when starting to read a file,
436 for use when closing off the current file.
437 NAME is the file name the symbols came from, START_ADDR is the first
438 text address for the file, and SIZE is the number of bytes of text. */
439
440static void
441complete_symtab (name, start_addr, size)
442 char *name;
443 CORE_ADDR start_addr;
444 unsigned int size;
445{
ab93997d
JK
446 if (last_source_file != NULL)
447 free (last_source_file);
bd5635a1 448 last_source_file = savestring (name, strlen (name));
7f4c8595
SS
449 current_source_start_addr = start_addr;
450 current_source_end_addr = start_addr + size;
bd5635a1 451
7f4c8595
SS
452 if (current_objfile -> ei.entry_point >= current_source_start_addr &&
453 current_objfile -> ei.entry_point < current_source_end_addr)
bd5635a1 454 {
7f4c8595
SS
455 current_objfile -> ei.entry_file_lowpc = current_source_start_addr;
456 current_objfile -> ei.entry_file_highpc = current_source_end_addr;
bd5635a1
RP
457 }
458}
459
460/* Finish the symbol definitions for one main source file,
461 close off all the lexical contexts for that file
462 (creating struct block's for them), then make the
463 struct symtab for that file and put it in the list of all such. */
464
465static void
318bf84f 466coff_end_symtab (objfile)
a048c8f5 467 struct objfile *objfile;
bd5635a1 468{
c438b3af 469 struct symtab *symtab;
bd5635a1 470
7f4c8595 471 last_source_start_addr = current_source_start_addr;
bd5635a1 472
fc773653
JK
473 /* For no good reason, this file stores the number of entries in a
474 separate variable instead of in line_vector->nitems. Fix it. */
475 if (line_vector)
476 line_vector->nitems = line_vector_index;
477
c438b3af
JK
478 /* For COFF, we only have one subfile, so we can just look at
479 subfiles and not worry about there being other elements in the
480 chain. We fill in various fields now because we didn't know them
481 before (or because doing it now is simply an artifact of how this
482 file used to be written). */
483 subfiles->line_vector = line_vector;
484 subfiles->name = last_source_file;
bd5635a1 485
c438b3af
JK
486 /* sort_pending is needed for amdcoff, at least.
487 sort_linevec is needed for the SCO compiler. */
7f4c8595 488 symtab = end_symtab (current_source_end_addr, 1, 1, objfile, 0);
bd5635a1 489
c438b3af
JK
490 if (symtab != NULL)
491 free_named_symtabs (symtab->filename);
a6e2b424 492
bd5635a1
RP
493 /* Reinitialize for beginning of new file. */
494 line_vector = 0;
495 line_vector_length = -1;
85f0a848 496 last_source_file = NULL;
bd5635a1
RP
497}
498\f
499static void
af8b7906 500record_minimal_symbol (name, address, type, objfile)
bd5635a1
RP
501 char *name;
502 CORE_ADDR address;
f70be3e4 503 enum minimal_symbol_type type;
af8b7906 504 struct objfile *objfile;
bd5635a1 505{
318bf84f 506 /* We don't want TDESC entry points in the minimal symbol table */
bd5635a1 507 if (name[0] == '@') return;
e140f1da 508
ab93997d
JK
509 prim_record_minimal_symbol
510 (obsavestring (name, strlen (name), &objfile->symbol_obstack),
511 address, type,
512 objfile);
bd5635a1
RP
513}
514\f
515/* coff_symfile_init ()
516 is the coff-specific initialization routine for reading symbols.
3624c875 517 It is passed a struct objfile which contains, among other things,
bd5635a1
RP
518 the BFD for the file whose symbols are being read, and a slot for
519 a pointer to "private data" which we fill with cookies and other
520 treats for coff_symfile_read ().
521
522 We will only be called if this is a COFF or COFF-like file.
523 BFD handles figuring out the format of the file, and code in symtab.c
524 uses BFD's determination to vector to us.
525
526 The ultimate result is a new symtab (or, FIXME, eventually a psymtab). */
527
9bba3334 528static void
80d68b1d
FF
529coff_symfile_init (objfile)
530 struct objfile *objfile;
bd5635a1 531{
80d68b1d 532 bfd *abfd = objfile->obfd;
bd5635a1 533
8f6960c9
JK
534 /* Allocate struct to keep track of stab reading. */
535 objfile->sym_stab_info = (PTR)
536 xmmalloc (objfile -> md, sizeof (struct dbx_symfile_info));
537
538 memset ((PTR) objfile->sym_stab_info, 0, sizeof (struct dbx_symfile_info));
539
bd5635a1 540 /* Allocate struct to keep track of the symfile */
80d68b1d
FF
541 objfile -> sym_private = xmmalloc (objfile -> md,
542 sizeof (struct coff_symfile_info));
bd5635a1 543
b5b186a2
SS
544 memset (objfile->sym_private, 0, sizeof (struct coff_symfile_info));
545
3624c875 546 init_entry_point_info (objfile);
bd5635a1
RP
547}
548
549/* This function is called for every section; it finds the outer limits
550 of the line table (minimum and maximum file offset) so that the
551 mainline code can read the whole thing for efficiency. */
552
e1ce8aa5 553/* ARGSUSED */
bd5635a1
RP
554static void
555find_linenos (abfd, asect, vpinfo)
556 bfd *abfd;
557 sec_ptr asect;
318bf84f 558 PTR vpinfo;
bd5635a1
RP
559{
560 struct coff_symfile_info *info;
561 int size, count;
562 file_ptr offset, maxoff;
563
564/* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
565 count = asect->lineno_count;
566/* End of warning */
567
568 if (count == 0)
569 return;
d8ce1326 570 size = count * local_linesz;
bd5635a1
RP
571
572 info = (struct coff_symfile_info *)vpinfo;
573/* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
574 offset = asect->line_filepos;
575/* End of warning */
576
577 if (offset < info->min_lineno_offset || info->min_lineno_offset == 0)
578 info->min_lineno_offset = offset;
579
580 maxoff = offset + size;
581 if (maxoff > info->max_lineno_offset)
582 info->max_lineno_offset = maxoff;
583}
584
585
e1ce8aa5
JK
586/* The BFD for this file -- only good while we're actively reading
587 symbols into a psymtab or a symtab. */
588
589static bfd *symfile_bfd;
590
bd5635a1 591/* Read a symbol file, after initialization by coff_symfile_init. */
bd5635a1 592
61a7292f 593/* ARGSUSED */
9bba3334 594static void
85f0a848 595coff_symfile_read (objfile, section_offsets, mainline)
80d68b1d 596 struct objfile *objfile;
85f0a848 597 struct section_offsets *section_offsets;
bd5635a1
RP
598 int mainline;
599{
80d68b1d 600 struct coff_symfile_info *info;
8f6960c9 601 struct dbx_symfile_info *dbxinfo;
80d68b1d 602 bfd *abfd = objfile->obfd;
d8ce1326 603 coff_data_type *cdata = coff_data (abfd);
bd5635a1 604 char *name = bfd_get_filename (abfd);
bd5635a1
RP
605 register int val;
606 int num_symbols;
607 int symtab_offset;
608 int stringtab_offset;
e09c5e26 609 struct cleanup *back_to;
b5b186a2 610 int stabsize, stabstrsize;
bd5635a1 611
80d68b1d 612 info = (struct coff_symfile_info *) objfile -> sym_private;
8f6960c9 613 dbxinfo = (struct dbx_symfile_info *) objfile->sym_stab_info;
bd5635a1
RP
614 symfile_bfd = abfd; /* Kludge for swap routines */
615
616/* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
bd5635a1 617 num_symbols = bfd_get_symcount (abfd); /* How many syms */
d8ce1326
JG
618 symtab_offset = cdata->sym_filepos; /* Symbol table file offset */
619 stringtab_offset = symtab_offset + /* String table file offset */
620 num_symbols * cdata->local_symesz;
621
622 /* Set a few file-statics that give us specific information about
623 the particular COFF file format we're reading. */
624 local_linesz = cdata->local_linesz;
625 local_n_btmask = cdata->local_n_btmask;
626 local_n_btshft = cdata->local_n_btshft;
627 local_n_tmask = cdata->local_n_tmask;
628 local_n_tshift = cdata->local_n_tshift;
629 local_linesz = cdata->local_linesz;
630 local_symesz = cdata->local_symesz;
631 local_auxesz = cdata->local_auxesz;
632
633 /* Allocate space for raw symbol and aux entries, based on their
634 space requirements as reported by BFD. */
635 temp_sym = (char *) xmalloc
636 (cdata->local_symesz + cdata->local_auxesz);
637 temp_aux = temp_sym + cdata->local_symesz;
e09c5e26 638 back_to = make_cleanup (free_current_contents, &temp_sym);
bd5635a1
RP
639/* End of warning */
640
bd5635a1
RP
641 /* Read the line number table, all at once. */
642 info->min_lineno_offset = 0;
643 info->max_lineno_offset = 0;
b5b186a2 644 bfd_map_over_sections (abfd, find_linenos, (PTR) info);
bd5635a1 645
e09c5e26 646 make_cleanup (free_linetab, 0);
7f4c8595 647 val = init_lineno (abfd, info->min_lineno_offset,
bd5635a1
RP
648 info->max_lineno_offset - info->min_lineno_offset);
649 if (val < 0)
650 error ("\"%s\": error reading line numbers\n", name);
651
652 /* Now read the string table, all at once. */
653
e09c5e26 654 make_cleanup (free_stringtab, 0);
7f4c8595 655 val = init_stringtab (abfd, stringtab_offset);
bd5635a1 656 if (val < 0)
a048c8f5 657 error ("\"%s\": can't get string table", name);
bd5635a1 658
318bf84f
FF
659 init_minimal_symbol_collection ();
660 make_cleanup (discard_minimal_symbols, 0);
bd5635a1
RP
661
662 /* Now that the executable file is positioned at symbol table,
663 process it and define symbols accordingly. */
664
7f4c8595
SS
665 coff_symtab_read ((long) symtab_offset, num_symbols, section_offsets,
666 objfile);
bd5635a1
RP
667
668 /* Sort symbols alphabetically within each block. */
669
9c38eb65
JK
670 {
671 struct symtab *s;
7f4c8595 672
9c38eb65 673 for (s = objfile -> symtabs; s != NULL; s = s -> next)
7f4c8595 674 sort_symtab_syms (s);
9c38eb65 675 }
bd5635a1 676
318bf84f 677 /* Install any minimal symbols that have been collected as the current
7f4c8595 678 minimal symbols for this objfile. */
bd5635a1 679
80d68b1d 680 install_minimal_symbols (objfile);
e09c5e26 681
b5b186a2
SS
682 bfd_map_over_sections (abfd, coff_locate_sections, (PTR) info);
683
684 if (info->stabsect)
685 {
0ccbaadc
JK
686 /* FIXME: dubious. Why can't we use something normal like
687 bfd_get_section_contents? */
7f4c8595 688 bfd_seek (abfd, abfd->where, 0);
b5b186a2
SS
689
690 stabsize = bfd_section_size (abfd, info->stabsect);
691 stabstrsize = bfd_section_size (abfd, info->stabstrsect);
692
693 coffstab_build_psymtabs (objfile,
694 section_offsets,
695 mainline,
696 info->stabsect->filepos, stabsize,
697 info->stabstrsect->filepos, stabstrsize);
698 }
699
e09c5e26 700 do_cleanups (back_to);
bd5635a1
RP
701}
702
9bba3334 703static void
f70be3e4
JG
704coff_new_init (ignore)
705 struct objfile *ignore;
bd5635a1 706{
bd5635a1 707}
80d68b1d
FF
708
709/* Perform any local cleanups required when we are done with a particular
710 objfile. I.E, we are in the process of discarding all symbol information
711 for an objfile, freeing up all memory held for it, and unlinking the
712 objfile struct from the global list of known objfiles. */
713
714static void
715coff_symfile_finish (objfile)
716 struct objfile *objfile;
717{
718 if (objfile -> sym_private != NULL)
719 {
720 mfree (objfile -> md, objfile -> sym_private);
721 }
722}
723
bd5635a1 724\f
bd5635a1
RP
725/* Given pointers to a symbol table in coff style exec file,
726 analyze them and create struct symtab's describing the symbols.
727 NSYMS is the number of symbols in the symbol table.
728 We read them one at a time using read_one_sym (). */
729
730static void
7f4c8595 731coff_symtab_read (symtab_offset, nsyms, section_offsets, objfile)
f70be3e4 732 long symtab_offset;
bd5635a1 733 int nsyms;
7f4c8595 734 struct section_offsets *section_offsets;
a048c8f5 735 struct objfile *objfile;
bd5635a1 736{
c438b3af 737 register struct context_stack *new;
bd5635a1
RP
738 struct coff_symbol coff_symbol;
739 register struct coff_symbol *cs = &coff_symbol;
dcc35536
JG
740 static struct internal_syment main_sym;
741 static union internal_auxent main_aux;
bd5635a1 742 struct coff_symbol fcn_cs_saved;
dcc35536
JG
743 static struct internal_syment fcn_sym_saved;
744 static union internal_auxent fcn_aux_saved;
f70be3e4 745 struct symtab *s;
bd5635a1
RP
746 /* A .file is open. */
747 int in_source_file = 0;
bd5635a1 748 int next_file_symnum = -1;
bd5635a1
RP
749 /* Name of the current file. */
750 char *filestring = "";
f70be3e4
JG
751 int depth = 0;
752 int fcn_first_line = 0;
753 int fcn_last_line = 0;
754 int fcn_start_addr = 0;
755 long fcn_line_ptr = 0;
f70be3e4 756 int val;
7f4c8595 757 CORE_ADDR tmpaddr;
bd5635a1 758
f2c365f5 759 /* Work around a stdio bug in SunOS4.1.1 (this makes me nervous....
fc312cc6
JK
760 it's hard to know I've really worked around it. The fix should be
761 harmless, anyway). The symptom of the bug is that the first
762 fread (in read_one_sym), will (in my example) actually get data
763 from file offset 268, when the fseek was to 264 (and ftell shows
764 264). This causes all hell to break loose. I was unable to
765 reproduce this on a short test program which operated on the same
766 file, performing (I think) the same sequence of operations.
767
7f4c8595 768 It stopped happening when I put in this (former) rewind().
fc312cc6
JK
769
770 FIXME: Find out if this has been reported to Sun, whether it has
771 been fixed in a later release, etc. */
772
7f4c8595 773 bfd_seek (objfile->obfd, 0, 0);
f2c365f5 774
f70be3e4 775 /* Position to read the symbol table. */
7f4c8595 776 val = bfd_seek (objfile->obfd, (long) symtab_offset, 0);
f70be3e4
JG
777 if (val < 0)
778 perror_with_name (objfile->name);
bd5635a1 779
318bf84f 780 current_objfile = objfile;
7f4c8595 781 nlist_bfd_global = objfile->obfd;
bd5635a1 782 nlist_nsyms_global = nsyms;
85f0a848 783 last_source_file = NULL;
4ed3a9ea 784 memset (opaque_type_chain, 0, sizeof opaque_type_chain);
bd5635a1 785
cadbb07a 786 if (type_vector) /* Get rid of previous one */
7f4c8595 787 free ((PTR) type_vector);
bd5635a1 788 type_vector_length = 160;
159a075e 789 type_vector = (struct type **)
7f4c8595 790 xmalloc (type_vector_length * sizeof (struct type *));
4ed3a9ea 791 memset (type_vector, 0, type_vector_length * sizeof (struct type *));
bd5635a1 792
318bf84f 793 coff_start_symtab ();
bd5635a1
RP
794
795 symnum = 0;
796 while (symnum < nsyms)
797 {
798 QUIT; /* Make this command interruptable. */
7f4c8595 799
bd5635a1
RP
800 read_one_sym (cs, &main_sym, &main_aux);
801
802#ifdef SEM
803 temp_sem_val = cs->c_name[0] << 24 | cs->c_name[1] << 16 |
804 cs->c_name[2] << 8 | cs->c_name[3];
805 if (int_sem_val == temp_sem_val)
806 last_coffsem = (int) strtol (cs->c_name+4, (char **) NULL, 10);
807#endif
808
809 if (cs->c_symnum == next_file_symnum && cs->c_sclass != C_FILE)
810 {
811 if (last_source_file)
318bf84f 812 coff_end_symtab (objfile);
bd5635a1 813
318bf84f 814 coff_start_symtab ();
af8b7906 815 complete_symtab ("_globals_", 0, 0);
bd5635a1
RP
816 /* done with all files, everything from here on out is globals */
817 }
818
819 /* Special case for file with type declarations only, no text. */
8714ff35
JK
820 if (!last_source_file && SDB_TYPE (cs->c_type)
821 && cs->c_secnum == N_DEBUG)
bd5635a1
RP
822 complete_symtab (filestring, 0, 0);
823
824 /* Typedefs should not be treated as symbol definitions. */
825 if (ISFCN (cs->c_type) && cs->c_sclass != C_TPDEF)
826 {
e09c5e26 827 /* Record all functions -- external and static -- in minsyms. */
7f4c8595
SS
828 tmpaddr = cs->c_value + ANOFFSET (section_offsets, SECT_OFF_TEXT);
829 record_minimal_symbol (cs->c_name, tmpaddr, mst_text, objfile);
bd5635a1
RP
830
831 fcn_line_ptr = main_aux.x_sym.x_fcnary.x_fcn.x_lnnoptr;
7f4c8595 832 fcn_start_addr = tmpaddr;
bd5635a1
RP
833 fcn_cs_saved = *cs;
834 fcn_sym_saved = main_sym;
835 fcn_aux_saved = main_aux;
836 continue;
837 }
838
839 switch (cs->c_sclass)
840 {
841 case C_EFCN:
842 case C_EXTDEF:
843 case C_ULABEL:
844 case C_USTATIC:
845 case C_LINE:
846 case C_ALIAS:
847 case C_HIDDEN:
7e258d18 848 complain (&bad_sclass_complaint, cs->c_name);
bd5635a1
RP
849 break;
850
851 case C_FILE:
7f4c8595
SS
852 /* c_value field contains symnum of next .file entry in table
853 or symnum of first global after last .file. */
bd5635a1 854 next_file_symnum = cs->c_value;
f0fcf00a 855 if (cs->c_naux > 0)
af8b7906 856 filestring = coff_getfilename (&main_aux);
f0fcf00a
SG
857 else
858 filestring = "";
859
7f4c8595
SS
860 /* Complete symbol table for last object file
861 containing debugging information. */
bd5635a1
RP
862 if (last_source_file)
863 {
318bf84f
FF
864 coff_end_symtab (objfile);
865 coff_start_symtab ();
bd5635a1
RP
866 }
867 in_source_file = 1;
868 break;
869
870 case C_STAT:
7f4c8595
SS
871 if (cs->c_name[0] == '.')
872 {
873 if (STREQ (cs->c_name, ".text")) {
874 /* FIXME: don't wire in ".text" as section name
875 or symbol name! */
876 /* Check for in_source_file deals with case of
877 a file with debugging symbols
878 followed by a later file with no symbols. */
879 if (in_source_file)
880 complete_symtab (filestring,
881 cs->c_value + ANOFFSET (section_offsets, SECT_OFF_TEXT),
882 main_aux.x_scn.x_scnlen);
883 in_source_file = 0;
884 }
885 /* flush rest of '.' symbols */
886 break;
887 }
8714ff35 888 else if (!SDB_TYPE (cs->c_type)
bd5635a1
RP
889 && cs->c_name[0] == 'L'
890 && (strncmp (cs->c_name, "LI%", 3) == 0
8714ff35 891 || strncmp (cs->c_name, "LF%", 3) == 0
bd5635a1
RP
892 || strncmp (cs->c_name,"LC%",3) == 0
893 || strncmp (cs->c_name,"LP%",3) == 0
894 || strncmp (cs->c_name,"LPB%",4) == 0
895 || strncmp (cs->c_name,"LBB%",4) == 0
896 || strncmp (cs->c_name,"LBE%",4) == 0
897 || strncmp (cs->c_name,"LPBX%",5) == 0))
898 /* At least on a 3b1, gcc generates swbeg and string labels
899 that look like this. Ignore them. */
900 break;
901 /* fall in for static symbols that don't start with '.' */
902 case C_EXT:
e7b5942b
JK
903 {
904 /* Record it in the minimal symbols regardless of
905 SDB_TYPE. This parallels what we do for other debug
906 formats, and probably is needed to make
907 print_address_symbolic work right without the (now
908 gone) "set fast-symbolic-addr off" kludge. */
909
910 /* FIXME: should use mst_abs, and not relocate, if absolute. */
911 enum minimal_symbol_type ms_type;
3a470454
JK
912 int sec;
913
914 if (cs->c_secnum == N_UNDEF)
915 {
916 /* This is a common symbol. See if the target
917 environment knows where it has been relocated to. */
918 CORE_ADDR reladdr;
919 if (target_lookup_symbol (cs->c_name, &reladdr))
920 {
921 /* Error in lookup; ignore symbol. */
922 break;
923 }
924 tmpaddr = reladdr;
62fad389
JK
925 /* The address has already been relocated; make sure that
926 objfile_relocate doesn't relocate it again. */
927 sec = -2;
928 ms_type = cs->c_sclass == C_STAT ? mst_file_bss : mst_bss;
3a470454
JK
929 }
930 else
931 {
932 sec = cs_to_section (cs, objfile);
933 tmpaddr = cs->c_value;
934 if (cs->c_sclass != C_STAT)
935 tmpaddr += ANOFFSET (section_offsets, sec);
62fad389
JK
936
937 switch (sec)
938 {
939 case SECT_OFF_TEXT:
940 case SECT_OFF_RODATA:
941 ms_type =
942 cs->c_sclass == C_STAT ? mst_file_text : mst_text;
943 break;
944 case SECT_OFF_DATA:
945 ms_type =
946 cs->c_sclass == C_STAT ? mst_file_data : mst_data;
947 break;
948 case SECT_OFF_BSS:
949 ms_type =
950 cs->c_sclass == C_STAT ? mst_file_bss : mst_bss;
951 break;
952 default:
953 ms_type = mst_unknown;
954 break;
955 }
3a470454
JK
956 }
957
62fad389
JK
958 if (cs->c_name[0] != '@' /* Skip tdesc symbols */)
959 prim_record_minimal_symbol_and_info
960 (obsavestring (cs->c_name, strlen (cs->c_name),
961 &objfile->symbol_obstack),
962 tmpaddr,
963 ms_type,
964 NULL,
965 sec,
966 objfile);
967
968 if (SDB_TYPE (cs->c_type))
e7b5942b 969 {
62fad389
JK
970 struct symbol *sym;
971 sym = process_coff_symbol
972 (cs, &main_aux, section_offsets, objfile);
973 SYMBOL_VALUE (sym) = tmpaddr;
974 SYMBOL_SECTION (sym) = sec;
e7b5942b 975 }
e7b5942b 976 }
bd5635a1
RP
977 break;
978
979 case C_FCN:
c653bc6d 980 if (STREQ (cs->c_name, ".bf"))
bd5635a1
RP
981 {
982 within_function = 1;
983
984 /* value contains address of first non-init type code */
985 /* main_aux.x_sym.x_misc.x_lnsz.x_lnno
986 contains line number of '{' } */
e64fbb3a 987 if (cs->c_naux != 1)
51b80b00 988 complain (&bf_no_aux_complaint, cs->c_symnum);
bd5635a1
RP
989 fcn_first_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
990
c438b3af
JK
991 /* Might want to check that locals are 0 and
992 context_stack_depth is zero, and complain if not. */
993
994 depth = 0;
995 new = push_context (depth, fcn_start_addr);
bd5635a1 996 fcn_cs_saved.c_name = getsymname (&fcn_sym_saved);
7f4c8595
SS
997 new->name =
998 process_coff_symbol (&fcn_cs_saved, &fcn_aux_saved,
999 section_offsets, objfile);
bd5635a1 1000 }
c653bc6d 1001 else if (STREQ (cs->c_name, ".ef"))
bd5635a1 1002 {
c438b3af
JK
1003 /* the value of .ef is the address of epilogue code;
1004 not useful for gdb. */
bd5635a1
RP
1005 /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
1006 contains number of lines to '}' */
c438b3af
JK
1007 new = pop_context ();
1008 /* Stack must be empty now. */
1009 if (context_stack_depth > 0 || new == NULL)
bd5635a1 1010 {
51b80b00 1011 complain (&ef_complaint, cs->c_symnum);
bd5635a1
RP
1012 within_function = 0;
1013 break;
1014 }
c438b3af
JK
1015 if (cs->c_naux != 1)
1016 {
1017 complain (&ef_no_aux_complaint, cs->c_symnum);
1018 fcn_last_line = 0x7FFFFFFF;
1019 }
1020 else
1021 {
1022 fcn_last_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
1023 }
7f4c8595
SS
1024 enter_linenos (fcn_line_ptr, fcn_first_line, fcn_last_line,
1025 section_offsets);
bd5635a1 1026
c438b3af 1027 finish_block (new->name, &local_symbols, new->old_blocks,
bd5635a1
RP
1028 new->start_addr,
1029#if defined (FUNCTION_EPILOGUE_SIZE)
1030 /* This macro should be defined only on
1031 machines where the
1032 fcn_aux_saved.x_sym.x_misc.x_fsize
1033 field is always zero.
1034 So use the .bf record information that
1035 points to the epilogue and add the size
1036 of the epilogue. */
7f4c8595
SS
1037 cs->c_value
1038 + FUNCTION_EPILOGUE_SIZE
1039 + ANOFFSET (section_offsets, SECT_OFF_TEXT),
bd5635a1 1040#else
7f4c8595
SS
1041 fcn_cs_saved.c_value
1042 + fcn_aux_saved.x_sym.x_misc.x_fsize
1043 + ANOFFSET (section_offsets, SECT_OFF_TEXT),
bd5635a1 1044#endif
318bf84f 1045 objfile
bd5635a1 1046 );
bd5635a1 1047 within_function = 0;
bd5635a1
RP
1048 }
1049 break;
1050
1051 case C_BLOCK:
c653bc6d 1052 if (STREQ (cs->c_name, ".bb"))
bd5635a1 1053 {
7f4c8595
SS
1054 tmpaddr = cs->c_value;
1055 tmpaddr += ANOFFSET (section_offsets, SECT_OFF_TEXT);
1056 push_context (++depth, tmpaddr);
bd5635a1 1057 }
c653bc6d 1058 else if (STREQ (cs->c_name, ".eb"))
bd5635a1 1059 {
c438b3af
JK
1060 new = pop_context ();
1061 if (depth-- != new->depth)
de9bef49 1062 {
5573d7d4 1063 complain (&eb_complaint, symnum);
de9bef49
JG
1064 break;
1065 }
c438b3af 1066 if (local_symbols && context_stack_depth > 0)
bd5635a1 1067 {
7f4c8595
SS
1068 tmpaddr =
1069 cs->c_value + ANOFFSET (section_offsets, SECT_OFF_TEXT);
bd5635a1 1070 /* Make a block for the local symbols within. */
c438b3af 1071 finish_block (0, &local_symbols, new->old_blocks,
7f4c8595 1072 new->start_addr, tmpaddr, objfile);
bd5635a1 1073 }
c438b3af
JK
1074 /* Now pop locals of block just finished. */
1075 local_symbols = new->locals;
bd5635a1
RP
1076 }
1077 break;
bd5635a1
RP
1078
1079 default:
7f4c8595 1080 process_coff_symbol (cs, &main_aux, section_offsets, objfile);
bd5635a1
RP
1081 break;
1082 }
1083 }
1084
1085 if (last_source_file)
318bf84f 1086 coff_end_symtab (objfile);
f70be3e4
JG
1087
1088 /* Patch up any opaque types (references to types that are not defined
1089 in the file where they are referenced, e.g. "struct foo *bar"). */
1090 ALL_OBJFILE_SYMTABS (objfile, s)
1091 patch_opaque_types (s);
1092
318bf84f 1093 current_objfile = NULL;
bd5635a1
RP
1094}
1095\f
1096/* Routines for reading headers and symbols from executable. */
1097
dcc35536
JG
1098/* Read the next symbol, swap it, and return it in both internal_syment
1099 form, and coff_symbol form. Also return its first auxent, if any,
1100 in internal_auxent form, and skip any other auxents. */
bd5635a1
RP
1101
1102static void
1103read_one_sym (cs, sym, aux)
1104 register struct coff_symbol *cs;
dcc35536
JG
1105 register struct internal_syment *sym;
1106 register union internal_auxent *aux;
bd5635a1 1107{
bd5635a1
RP
1108 int i;
1109
1110 cs->c_symnum = symnum;
7f4c8595 1111 bfd_read (temp_sym, local_symesz, 1, nlist_bfd_global);
d8ce1326 1112 bfd_coff_swap_sym_in (symfile_bfd, temp_sym, (char *)sym);
e64fbb3a
JG
1113 cs->c_naux = sym->n_numaux & 0xff;
1114 if (cs->c_naux >= 1)
bd5635a1 1115 {
7f4c8595 1116 bfd_read (temp_aux, local_auxesz, 1, nlist_bfd_global);
d8ce1326 1117 bfd_coff_swap_aux_in (symfile_bfd, temp_aux, sym->n_type, sym->n_sclass,
bf8d9d28 1118 0, cs->c_naux, (char *)aux);
bd5635a1
RP
1119 /* If more than one aux entry, read past it (only the first aux
1120 is important). */
e64fbb3a 1121 for (i = 1; i < cs->c_naux; i++)
7f4c8595 1122 bfd_read (temp_aux, local_auxesz, 1, nlist_bfd_global);
bd5635a1
RP
1123 }
1124 cs->c_name = getsymname (sym);
1125 cs->c_value = sym->n_value;
1126 cs->c_sclass = (sym->n_sclass & 0xff);
1127 cs->c_secnum = sym->n_scnum;
1128 cs->c_type = (unsigned) sym->n_type;
8714ff35
JK
1129 if (!SDB_TYPE (cs->c_type))
1130 cs->c_type = 0;
bd5635a1 1131
e64fbb3a 1132 symnum += 1 + cs->c_naux;
bd5635a1
RP
1133}
1134\f
1135/* Support for string table handling */
1136
1137static char *stringtab = NULL;
1138
1139static int
7f4c8595
SS
1140init_stringtab (abfd, offset)
1141 bfd *abfd;
bd5635a1
RP
1142 long offset;
1143{
1144 long length;
1145 int val;
1146 unsigned char lengthbuf[4];
1147
e09c5e26 1148 free_stringtab ();
bd5635a1 1149
7f4c8595
SS
1150 /* If the file is stripped, the offset might be zero, indicating no
1151 string table. Just return with `stringtab' set to null. */
1152 if (offset == 0)
1153 return 0;
1154
1155 if (bfd_seek (abfd, offset, 0) < 0)
bd5635a1
RP
1156 return -1;
1157
7f4c8595 1158 val = bfd_read ((char *)lengthbuf, sizeof lengthbuf, 1, abfd);
dcc35536 1159 length = bfd_h_get_32 (symfile_bfd, lengthbuf);
bd5635a1
RP
1160
1161 /* If no string table is needed, then the file may end immediately
1162 after the symbols. Just return with `stringtab' set to null. */
e09c5e26 1163 if (val != sizeof lengthbuf || length < sizeof lengthbuf)
bd5635a1
RP
1164 return 0;
1165
1166 stringtab = (char *) xmalloc (length);
602d41e5
JK
1167 /* This is in target format (probably not very useful, and not currently
1168 used), not host format. */
1169 memcpy (stringtab, lengthbuf, sizeof lengthbuf);
bd5635a1
RP
1170 if (length == sizeof length) /* Empty table -- just the count */
1171 return 0;
1172
7f4c8595 1173 val = bfd_read (stringtab + sizeof lengthbuf, length - sizeof lengthbuf, 1, abfd);
e09c5e26 1174 if (val != length - sizeof lengthbuf || stringtab[length - 1] != '\0')
bd5635a1
RP
1175 return -1;
1176
1177 return 0;
1178}
1179
1180static void
1181free_stringtab ()
1182{
1183 if (stringtab)
1184 free (stringtab);
1185 stringtab = NULL;
1186}
1187
1188static char *
1189getsymname (symbol_entry)
dcc35536 1190 struct internal_syment *symbol_entry;
bd5635a1
RP
1191{
1192 static char buffer[SYMNMLEN+1];
1193 char *result;
1194
dcc35536 1195 if (symbol_entry->_n._n_n._n_zeroes == 0)
bd5635a1 1196 {
602d41e5
JK
1197 /* FIXME: Probably should be detecting corrupt symbol files by
1198 seeing whether offset points to within the stringtab. */
dcc35536 1199 result = stringtab + symbol_entry->_n._n_n._n_offset;
bd5635a1
RP
1200 }
1201 else
1202 {
dcc35536 1203 strncpy (buffer, symbol_entry->_n._n_name, SYMNMLEN);
bd5635a1
RP
1204 buffer[SYMNMLEN] = '\0';
1205 result = buffer;
1206 }
1207 return result;
1208}
1209
c653bc6d
JG
1210/* Extract the file name from the aux entry of a C_FILE symbol. Return
1211 only the last component of the name. Result is in static storage and
1212 is only good for temporary use. */
1213
dbb0fae2 1214char *
af8b7906 1215coff_getfilename (aux_entry)
dcc35536 1216 union internal_auxent *aux_entry;
bd5635a1
RP
1217{
1218 static char buffer[BUFSIZ];
1219 register char *temp;
1220 char *result;
bd5635a1 1221
bd5635a1
RP
1222 if (aux_entry->x_file.x_n.x_zeroes == 0)
1223 strcpy (buffer, stringtab + aux_entry->x_file.x_n.x_offset);
1224 else
bd5635a1 1225 {
bd5635a1 1226 strncpy (buffer, aux_entry->x_file.x_fname, FILNMLEN);
bd5635a1
RP
1227 buffer[FILNMLEN] = '\0';
1228 }
1229 result = buffer;
ab93997d
JK
1230
1231 /* FIXME: We should not be throwing away the information about what
1232 directory. It should go into dirname of the symtab, or some such
1233 place. */
318bf84f 1234 if ((temp = strrchr (result, '/')) != NULL)
bd5635a1
RP
1235 result = temp + 1;
1236 return (result);
1237}
1238\f
7f4c8595
SS
1239/* Support for line number handling. */
1240
bd5635a1
RP
1241static char *linetab = NULL;
1242static long linetab_offset;
1243static unsigned long linetab_size;
1244
dcc35536
JG
1245/* Read in all the line numbers for fast lookups later. Leave them in
1246 external (unswapped) format in memory; we'll swap them as we enter
1247 them into GDB's data structures. */
bd5635a1
RP
1248
1249static int
7f4c8595
SS
1250init_lineno (abfd, offset, size)
1251 bfd *abfd;
bd5635a1
RP
1252 long offset;
1253 int size;
1254{
1255 int val;
bd5635a1 1256
61a7292f
SG
1257 linetab_offset = offset;
1258 linetab_size = size;
1259
e09c5e26
JK
1260 free_linetab();
1261
61a7292f
SG
1262 if (size == 0)
1263 return 0;
1264
7f4c8595 1265 if (bfd_seek (abfd, offset, 0) < 0)
bd5635a1
RP
1266 return -1;
1267
e64fbb3a
JG
1268 /* Allocate the desired table, plus a sentinel */
1269 linetab = (char *) xmalloc (size + local_linesz);
bd5635a1 1270
7f4c8595 1271 val = bfd_read (linetab, size, 1, abfd);
bd5635a1
RP
1272 if (val != size)
1273 return -1;
1274
e64fbb3a 1275 /* Terminate it with an all-zero sentinel record */
4ed3a9ea 1276 memset (linetab + size, 0, local_linesz);
e64fbb3a 1277
bd5635a1
RP
1278 return 0;
1279}
1280
e09c5e26
JK
1281static void
1282free_linetab ()
1283{
1284 if (linetab)
1285 free (linetab);
1286 linetab = NULL;
1287}
1288
bd5635a1
RP
1289#if !defined (L_LNNO32)
1290#define L_LNNO32(lp) ((lp)->l_lnno)
1291#endif
1292
1293static void
7f4c8595
SS
1294enter_linenos (file_offset, first_line, last_line, section_offsets)
1295 long file_offset;
1296 register int first_line;
1297 register int last_line;
1298 struct section_offsets *section_offsets;
bd5635a1
RP
1299{
1300 register char *rawptr;
dcc35536 1301 struct internal_lineno lptr;
bd5635a1
RP
1302
1303 if (file_offset < linetab_offset)
1304 {
51b80b00 1305 complain (&lineno_complaint, file_offset);
63989338
JG
1306 if (file_offset > linetab_size) /* Too big to be an offset? */
1307 return;
1308 file_offset += linetab_offset; /* Try reading at that linetab offset */
bd5635a1
RP
1309 }
1310
1311 rawptr = &linetab[file_offset - linetab_offset];
1312
1313 /* skip first line entry for each function */
d8ce1326 1314 rawptr += local_linesz;
bd5635a1
RP
1315 /* line numbers start at one for the first line of the function */
1316 first_line--;
1317
dcc35536 1318 for (;;) {
d8ce1326
JG
1319 bfd_coff_swap_lineno_in (symfile_bfd, rawptr, &lptr);
1320 rawptr += local_linesz;
e64fbb3a 1321 /* The next function, or the sentinel, will have L_LNNO32 zero; we exit. */
dcc35536 1322 if (L_LNNO32 (&lptr) && L_LNNO32 (&lptr) <= last_line)
7f4c8595
SS
1323 coff_record_line (first_line + L_LNNO32 (&lptr),
1324 lptr.l_addr.l_paddr
1325 + ANOFFSET (section_offsets, SECT_OFF_TEXT));
dcc35536
JG
1326 else
1327 break;
1328 }
bd5635a1
RP
1329}
1330\f
bd5635a1
RP
1331static void
1332patch_type (type, real_type)
1333 struct type *type;
1334 struct type *real_type;
1335{
1336 register struct type *target = TYPE_TARGET_TYPE (type);
1337 register struct type *real_target = TYPE_TARGET_TYPE (real_type);
1338 int field_size = TYPE_NFIELDS (real_target) * sizeof (struct field);
1339
1340 TYPE_LENGTH (target) = TYPE_LENGTH (real_target);
1341 TYPE_NFIELDS (target) = TYPE_NFIELDS (real_target);
85f0a848 1342 TYPE_FIELDS (target) = (struct field *) TYPE_ALLOC (target, field_size);
bd5635a1 1343
4ed3a9ea 1344 memcpy (TYPE_FIELDS (target), TYPE_FIELDS (real_target), field_size);
bd5635a1
RP
1345
1346 if (TYPE_NAME (real_target))
1347 {
1348 if (TYPE_NAME (target))
1349 free (TYPE_NAME (target));
58ae87f6 1350 TYPE_NAME (target) = concat (TYPE_NAME (real_target), NULL);
bd5635a1
RP
1351 }
1352}
1353
7e258d18 1354/* Patch up all appropriate typedef symbols in the opaque_type_chains
f70be3e4 1355 so that they can be used to print out opaque data structures properly. */
bd5635a1 1356
f70be3e4
JG
1357static void
1358patch_opaque_types (s)
318bf84f 1359 struct symtab *s;
318bf84f
FF
1360{
1361 register struct block *b;
1362 register int i;
1363 register struct symbol *real_sym;
1364
1365 /* Go through the per-file symbols only */
1366 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
1367 for (i = BLOCK_NSYMS (b) - 1; i >= 0; i--)
bd5635a1 1368 {
318bf84f
FF
1369 /* Find completed typedefs to use to fix opaque ones.
1370 Remove syms from the chain when their types are stored,
1371 but search the whole chain, as there may be several syms
1372 from different files with the same name. */
1373 real_sym = BLOCK_SYM (b, i);
1374 if (SYMBOL_CLASS (real_sym) == LOC_TYPEDEF &&
1375 SYMBOL_NAMESPACE (real_sym) == VAR_NAMESPACE &&
1376 TYPE_CODE (SYMBOL_TYPE (real_sym)) == TYPE_CODE_PTR &&
1377 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (real_sym))) != 0)
bd5635a1 1378 {
318bf84f
FF
1379 register char *name = SYMBOL_NAME (real_sym);
1380 register int hash = hashname (name);
1381 register struct symbol *sym, *prev;
1382
1383 prev = 0;
1384 for (sym = opaque_type_chain[hash]; sym;)
bd5635a1 1385 {
318bf84f 1386 if (name[0] == SYMBOL_NAME (sym)[0] &&
c653bc6d 1387 STREQ (name + 1, SYMBOL_NAME (sym) + 1))
bd5635a1 1388 {
318bf84f 1389 if (prev)
bd5635a1 1390 {
318bf84f 1391 SYMBOL_VALUE_CHAIN (prev) = SYMBOL_VALUE_CHAIN (sym);
bd5635a1
RP
1392 }
1393 else
1394 {
318bf84f 1395 opaque_type_chain[hash] = SYMBOL_VALUE_CHAIN (sym);
bd5635a1 1396 }
318bf84f
FF
1397
1398 patch_type (SYMBOL_TYPE (sym), SYMBOL_TYPE (real_sym));
1399
1400 if (prev)
1401 {
1402 sym = SYMBOL_VALUE_CHAIN (prev);
1403 }
1404 else
1405 {
1406 sym = opaque_type_chain[hash];
1407 }
1408 }
1409 else
1410 {
1411 prev = sym;
1412 sym = SYMBOL_VALUE_CHAIN (sym);
bd5635a1
RP
1413 }
1414 }
1415 }
1416 }
1417}
1418\f
bd5635a1 1419static struct symbol *
7f4c8595 1420process_coff_symbol (cs, aux, section_offsets, objfile)
bd5635a1 1421 register struct coff_symbol *cs;
dcc35536 1422 register union internal_auxent *aux;
7f4c8595 1423 struct section_offsets *section_offsets;
318bf84f 1424 struct objfile *objfile;
bd5635a1
RP
1425{
1426 register struct symbol *sym
c653bc6d
JG
1427 = (struct symbol *) obstack_alloc (&objfile->symbol_obstack,
1428 sizeof (struct symbol));
bd5635a1 1429 char *name;
bd5635a1 1430
4ed3a9ea 1431 memset (sym, 0, sizeof (struct symbol));
bd5635a1 1432 name = cs->c_name;
de9bef49 1433 name = EXTERNAL_NAME (name, objfile->obfd);
c653bc6d
JG
1434 SYMBOL_NAME (sym) = obstack_copy0 (&objfile->symbol_obstack, name,
1435 strlen (name));
bd5635a1
RP
1436
1437 /* default assumptions */
1438 SYMBOL_VALUE (sym) = cs->c_value;
1439 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
e7b5942b 1440 SYMBOL_SECTION (sym) = cs_to_section (cs, objfile);
bd5635a1
RP
1441
1442 if (ISFCN (cs->c_type))
1443 {
7f4c8595 1444 SYMBOL_VALUE (sym) += ANOFFSET (section_offsets, SECT_OFF_TEXT);
d8ce1326
JG
1445#if 0
1446 /* FIXME: This has NOT been tested. The DBX version has.. */
1447 /* Generate a template for the type of this function. The
1448 types of the arguments will be added as we read the symbol
1449 table. */
1450 struct type *new = (struct type *)
318bf84f 1451 obstack_alloc (&objfile->symbol_obstack, sizeof (struct type));
d8ce1326 1452
4ed3a9ea 1453 memcpy (new, lookup_function_type (decode_function_type (cs, cs->c_type, aux)),
51b57ded 1454 sizeof(struct type));
d8ce1326
JG
1455 SYMBOL_TYPE (sym) = new;
1456 in_function_type = SYMBOL_TYPE(sym);
1457#else
1458 SYMBOL_TYPE(sym) =
1459 lookup_function_type (decode_function_type (cs, cs->c_type, aux));
1460#endif
1461
bd5635a1
RP
1462 SYMBOL_CLASS (sym) = LOC_BLOCK;
1463 if (cs->c_sclass == C_STAT)
c438b3af 1464 add_symbol_to_list (sym, &file_symbols);
bd5635a1 1465 else if (cs->c_sclass == C_EXT)
c438b3af 1466 add_symbol_to_list (sym, &global_symbols);
bd5635a1
RP
1467 }
1468 else
1469 {
1470 SYMBOL_TYPE (sym) = decode_type (cs, cs->c_type, aux);
1471 switch (cs->c_sclass)
1472 {
1473 case C_NULL:
1474 break;
1475
1476 case C_AUTO:
1477 SYMBOL_CLASS (sym) = LOC_LOCAL;
c438b3af 1478 add_symbol_to_list (sym, &local_symbols);
bd5635a1
RP
1479 break;
1480
1481 case C_EXT:
1482 SYMBOL_CLASS (sym) = LOC_STATIC;
1483 SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
7f4c8595 1484 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (section_offsets, SECT_OFF_TEXT);
c438b3af 1485 add_symbol_to_list (sym, &global_symbols);
bd5635a1
RP
1486 break;
1487
1488 case C_STAT:
1489 SYMBOL_CLASS (sym) = LOC_STATIC;
1490 SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
7f4c8595 1491 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (section_offsets, SECT_OFF_TEXT);
bd5635a1
RP
1492 if (within_function) {
1493 /* Static symbol of local scope */
c438b3af 1494 add_symbol_to_list (sym, &local_symbols);
bd5635a1
RP
1495 }
1496 else {
1497 /* Static symbol at top level of file */
c438b3af 1498 add_symbol_to_list (sym, &file_symbols);
bd5635a1
RP
1499 }
1500 break;
1501
d8ce1326
JG
1502#ifdef C_GLBLREG /* AMD coff */
1503 case C_GLBLREG:
1504#endif
bd5635a1
RP
1505 case C_REG:
1506 SYMBOL_CLASS (sym) = LOC_REGISTER;
d8ce1326 1507 SYMBOL_VALUE (sym) = SDB_REG_TO_REGNUM(cs->c_value);
c438b3af 1508 add_symbol_to_list (sym, &local_symbols);
bd5635a1
RP
1509 break;
1510
1511 case C_LABEL:
1512 break;
1513
1514 case C_ARG:
1515 SYMBOL_CLASS (sym) = LOC_ARG;
d8ce1326 1516#if 0
7e258d18 1517 /* FIXME: This has not been tested. */
d8ce1326
JG
1518 /* Add parameter to function. */
1519 add_param_to_type(&in_function_type,sym);
1520#endif
c438b3af 1521 add_symbol_to_list (sym, &local_symbols);
e7b5942b
JK
1522#if !defined (BELIEVE_PCC_PROMOTION)
1523 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
1524 {
1525 /* If PCC says a parameter is a short or a char,
1526 aligned on an int boundary, realign it to the
1527 "little end" of the int. */
1528 struct type *temptype;
1529 temptype = lookup_fundamental_type (current_objfile,
1530 FT_INTEGER);
1531 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) < TYPE_LENGTH (temptype)
1532 && TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_INT
1533 && 0 == SYMBOL_VALUE (sym) % TYPE_LENGTH (temptype))
1534 {
1535 SYMBOL_VALUE (sym) +=
1536 TYPE_LENGTH (temptype)
1537 - TYPE_LENGTH (SYMBOL_TYPE (sym));
1538 }
1539 }
bd5635a1
RP
1540#endif
1541 break;
1542
1543 case C_REGPARM:
1544 SYMBOL_CLASS (sym) = LOC_REGPARM;
d8ce1326 1545 SYMBOL_VALUE (sym) = SDB_REG_TO_REGNUM(cs->c_value);
c438b3af 1546 add_symbol_to_list (sym, &local_symbols);
bd5635a1 1547#if !defined (BELIEVE_PCC_PROMOTION)
7f4c8595
SS
1548 /* FIXME: This should retain the current type, since it's just
1549 a register value. gnu@adobe, 26Feb93 */
1c95d7ab 1550 {
7f4c8595 1551 /* If PCC says a parameter is a short or a char,
1c95d7ab
JK
1552 it is really an int. */
1553 struct type *temptype;
1554 temptype =
1555 lookup_fundamental_type (current_objfile, FT_INTEGER);
1556 if (TYPE_LENGTH (SYMBOL_TYPE (sym)) < TYPE_LENGTH (temptype)
1557 && TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_INT)
1558 {
1559 SYMBOL_TYPE (sym) =
1560 (TYPE_UNSIGNED (SYMBOL_TYPE (sym))
1561 ? lookup_fundamental_type (current_objfile,
1562 FT_UNSIGNED_INTEGER)
1563 : temptype);
1564 }
7f4c8595 1565 }
bd5635a1
RP
1566#endif
1567 break;
1568
1569 case C_TPDEF:
1570 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
1571 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1572
1573 /* If type has no name, give it one */
318bf84f 1574 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
1c95d7ab
JK
1575 {
1576 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_PTR
1577 || TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_FUNC)
1578 {
1579 /* If we are giving a name to a type such as "pointer to
1580 foo" or "function returning foo", we better not set
1581 the TYPE_NAME. If the program contains "typedef char
1582 *caddr_t;", we don't want all variables of type char
1583 * to print as caddr_t. This is not just a
1584 consequence of GDB's type management; CC and GCC (at
1585 least through version 2.4) both output variables of
1586 either type char * or caddr_t with the type
1587 refering to the C_TPDEF symbol for caddr_t. If a future
1588 compiler cleans this up it GDB is not ready for it
1589 yet, but if it becomes ready we somehow need to
1590 disable this check (without breaking the PCC/GCC2.4
1591 case).
1592
1593 Sigh.
1594
1595 Fortunately, this check seems not to be necessary
1596 for anything except pointers or functions. */
1597 ;
1598 }
1599 else
1600 TYPE_NAME (SYMBOL_TYPE (sym)) =
1601 concat (SYMBOL_NAME (sym), NULL);
1602 }
7f4c8595 1603#ifdef CXUX_TARGET
e1118f15 1604 /* Ignore vendor section for Harris CX/UX targets. */
7f4c8595
SS
1605 else if (cs->c_name[0] == '$')
1606 break;
1607#endif /* CXUX_TARGET */
bd5635a1
RP
1608
1609 /* Keep track of any type which points to empty structured type,
f70be3e4
JG
1610 so it can be filled from a definition from another file. A
1611 simple forward reference (TYPE_CODE_UNDEF) is not an
1612 empty structured type, though; the forward references
1613 work themselves out via the magic of coff_lookup_type. */
bd5635a1 1614 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_PTR &&
f70be3e4
JG
1615 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym))) == 0 &&
1616 TYPE_CODE (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym))) !=
1617 TYPE_CODE_UNDEF)
bd5635a1
RP
1618 {
1619 register int i = hashname (SYMBOL_NAME (sym));
1620
1621 SYMBOL_VALUE_CHAIN (sym) = opaque_type_chain[i];
1622 opaque_type_chain[i] = sym;
1623 }
c438b3af 1624 add_symbol_to_list (sym, &file_symbols);
bd5635a1
RP
1625 break;
1626
1627 case C_STRTAG:
1628 case C_UNTAG:
1629 case C_ENTAG:
1630 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
1631 SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
e09c5e26
JK
1632
1633 /* Some compilers try to be helpful by inventing "fake"
1634 names for anonymous enums, structures, and unions, like
1635 "~0fake" or ".0fake". Thanks, but no thanks... */
1636 if (TYPE_TAG_NAME (SYMBOL_TYPE (sym)) == 0)
1637 if (SYMBOL_NAME(sym) != NULL
1638 && *SYMBOL_NAME(sym) != '~'
1639 && *SYMBOL_NAME(sym) != '.')
1640 TYPE_TAG_NAME (SYMBOL_TYPE (sym)) =
1641 concat (SYMBOL_NAME (sym), NULL);
1642
c438b3af 1643 add_symbol_to_list (sym, &file_symbols);
bd5635a1
RP
1644 break;
1645
1646 default:
1647 break;
1648 }
1649 }
1650 return sym;
1651}
1652\f
7f4c8595 1653/* Decode a coff type specifier; return the type that is meant. */
bd5635a1 1654
7f4c8595 1655static struct type *
bd5635a1
RP
1656decode_type (cs, c_type, aux)
1657 register struct coff_symbol *cs;
1658 unsigned int c_type;
dcc35536 1659 register union internal_auxent *aux;
bd5635a1
RP
1660{
1661 register struct type *type = 0;
1662 unsigned int new_c_type;
1663
1664 if (c_type & ~N_BTMASK)
1665 {
1666 new_c_type = DECREF (c_type);
1667 if (ISPTR (c_type))
1668 {
1669 type = decode_type (cs, new_c_type, aux);
1670 type = lookup_pointer_type (type);
1671 }
1672 else if (ISFCN (c_type))
1673 {
1674 type = decode_type (cs, new_c_type, aux);
1675 type = lookup_function_type (type);
1676 }
1677 else if (ISARY (c_type))
1678 {
1679 int i, n;
1680 register unsigned short *dim;
a8a69e63 1681 struct type *base_type, *index_type, *range_type;
bd5635a1
RP
1682
1683 /* Define an array type. */
1684 /* auxent refers to array, not base type */
6988f5c0 1685 if (aux->x_sym.x_tagndx.l == 0)
e64fbb3a 1686 cs->c_naux = 0;
bd5635a1
RP
1687
1688 /* shift the indices down */
1689 dim = &aux->x_sym.x_fcnary.x_ary.x_dimen[0];
1690 i = 1;
1691 n = dim[0];
1692 for (i = 0; *dim && i < DIMNUM - 1; i++, dim++)
1693 *dim = *(dim + 1);
1694 *dim = 0;
1695
bd5635a1 1696 base_type = decode_type (cs, new_c_type, aux);
85f0a848 1697 index_type = lookup_fundamental_type (current_objfile, FT_INTEGER);
a8a69e63
FF
1698 range_type =
1699 create_range_type ((struct type *) NULL, index_type, 0, n - 1);
1700 type =
1701 create_array_type ((struct type *) NULL, base_type, range_type);
bd5635a1
RP
1702 }
1703 return type;
1704 }
1705
f70be3e4
JG
1706 /* Reference to existing type. This only occurs with the
1707 struct, union, and enum types. EPI a29k coff
1708 fakes us out by producing aux entries with a nonzero
1709 x_tagndx for definitions of structs, unions, and enums, so we
117f631e
ILT
1710 have to check the c_sclass field. SCO 3.2v4 cc gets confused
1711 with pointers to pointers to defined structs, and generates
1712 negative x_tagndx fields. */
e64fbb3a 1713 if (cs->c_naux > 0 && aux->x_sym.x_tagndx.l != 0)
bd5635a1 1714 {
117f631e
ILT
1715 if (cs->c_sclass != C_STRTAG
1716 && cs->c_sclass != C_UNTAG
1717 && cs->c_sclass != C_ENTAG
1718 && aux->x_sym.x_tagndx.l >= 0)
f70be3e4
JG
1719 {
1720 type = coff_alloc_type (aux->x_sym.x_tagndx.l);
1721 return type;
7f4c8595
SS
1722 }
1723 else
1724 {
f70be3e4
JG
1725 complain (&tagndx_bad_complaint, cs->c_name);
1726 /* And fall through to decode_base_type... */
1727 }
bd5635a1
RP
1728 }
1729
1730 return decode_base_type (cs, BTYPE (c_type), aux);
1731}
1732
1733/* Decode a coff type specifier for function definition;
1734 return the type that the function returns. */
1735
7f4c8595 1736static struct type *
bd5635a1
RP
1737decode_function_type (cs, c_type, aux)
1738 register struct coff_symbol *cs;
1739 unsigned int c_type;
dcc35536 1740 register union internal_auxent *aux;
bd5635a1 1741{
6988f5c0 1742 if (aux->x_sym.x_tagndx.l == 0)
e64fbb3a 1743 cs->c_naux = 0; /* auxent refers to function, not base type */
bd5635a1
RP
1744
1745 return decode_type (cs, DECREF (c_type), aux);
1746}
1747\f
1748/* basic C types */
1749
7f4c8595 1750static struct type *
bd5635a1
RP
1751decode_base_type (cs, c_type, aux)
1752 register struct coff_symbol *cs;
1753 unsigned int c_type;
dcc35536 1754 register union internal_auxent *aux;
bd5635a1
RP
1755{
1756 struct type *type;
1757
1758 switch (c_type)
1759 {
1760 case T_NULL:
1761 /* shows up with "void (*foo)();" structure members */
318bf84f 1762 return lookup_fundamental_type (current_objfile, FT_VOID);
bd5635a1 1763
8aa13b87
JK
1764#if 0
1765/* DGUX actually defines both T_ARG and T_VOID to the same value. */
bd5635a1
RP
1766#ifdef T_ARG
1767 case T_ARG:
1768 /* Shows up in DGUX, I think. Not sure where. */
318bf84f 1769 return lookup_fundamental_type (current_objfile, FT_VOID); /* shouldn't show up here */
bd5635a1 1770#endif
8aa13b87 1771#endif /* 0 */
bd5635a1
RP
1772
1773#ifdef T_VOID
1774 case T_VOID:
1775 /* Intel 960 COFF has this symbol and meaning. */
318bf84f 1776 return lookup_fundamental_type (current_objfile, FT_VOID);
bd5635a1
RP
1777#endif
1778
1779 case T_CHAR:
318bf84f 1780 return lookup_fundamental_type (current_objfile, FT_CHAR);
bd5635a1
RP
1781
1782 case T_SHORT:
318bf84f 1783 return lookup_fundamental_type (current_objfile, FT_SHORT);
bd5635a1
RP
1784
1785 case T_INT:
318bf84f 1786 return lookup_fundamental_type (current_objfile, FT_INTEGER);
bd5635a1
RP
1787
1788 case T_LONG:
318bf84f 1789 return lookup_fundamental_type (current_objfile, FT_LONG);
bd5635a1
RP
1790
1791 case T_FLOAT:
318bf84f 1792 return lookup_fundamental_type (current_objfile, FT_FLOAT);
bd5635a1
RP
1793
1794 case T_DOUBLE:
318bf84f 1795 return lookup_fundamental_type (current_objfile, FT_DBL_PREC_FLOAT);
bd5635a1
RP
1796
1797 case T_STRUCT:
e64fbb3a 1798 if (cs->c_naux != 1)
bd5635a1
RP
1799 {
1800 /* anonymous structure type */
1801 type = coff_alloc_type (cs->c_symnum);
1802 TYPE_CODE (type) = TYPE_CODE_STRUCT;
e09c5e26
JK
1803 TYPE_NAME (type) = NULL;
1804 /* This used to set the tag to "<opaque>". But I think setting it
1805 to NULL is right, and the printing code can print it as
1806 "struct {...}". */
1807 TYPE_TAG_NAME (type) = NULL;
be8303da 1808 INIT_CPLUS_SPECIFIC(type);
bd5635a1
RP
1809 TYPE_LENGTH (type) = 0;
1810 TYPE_FIELDS (type) = 0;
1811 TYPE_NFIELDS (type) = 0;
1812 }
1813 else
1814 {
318bf84f 1815 type = coff_read_struct_type (cs->c_symnum,
bd5635a1 1816 aux->x_sym.x_misc.x_lnsz.x_size,
be8303da 1817 aux->x_sym.x_fcnary.x_fcn.x_endndx.l);
bd5635a1
RP
1818 }
1819 return type;
1820
1821 case T_UNION:
e64fbb3a 1822 if (cs->c_naux != 1)
bd5635a1
RP
1823 {
1824 /* anonymous union type */
1825 type = coff_alloc_type (cs->c_symnum);
e09c5e26
JK
1826 TYPE_NAME (type) = NULL;
1827 /* This used to set the tag to "<opaque>". But I think setting it
1828 to NULL is right, and the printing code can print it as
1829 "union {...}". */
1830 TYPE_TAG_NAME (type) = NULL;
be8303da 1831 INIT_CPLUS_SPECIFIC(type);
f1d77e90 1832 TYPE_LENGTH (type) = 0;
bd5635a1
RP
1833 TYPE_FIELDS (type) = 0;
1834 TYPE_NFIELDS (type) = 0;
1835 }
1836 else
1837 {
318bf84f 1838 type = coff_read_struct_type (cs->c_symnum,
bd5635a1 1839 aux->x_sym.x_misc.x_lnsz.x_size,
be8303da 1840 aux->x_sym.x_fcnary.x_fcn.x_endndx.l);
bd5635a1
RP
1841 }
1842 TYPE_CODE (type) = TYPE_CODE_UNION;
1843 return type;
1844
1845 case T_ENUM:
e09c5e26
JK
1846 if (cs->c_naux != 1)
1847 {
1848 /* anonymous enum type */
1849 type = coff_alloc_type (cs->c_symnum);
1850 TYPE_CODE (type) = TYPE_CODE_ENUM;
1851 TYPE_NAME (type) = NULL;
1852 /* This used to set the tag to "<opaque>". But I think setting it
1853 to NULL is right, and the printing code can print it as
1854 "enum {...}". */
1855 TYPE_TAG_NAME (type) = NULL;
1856 TYPE_LENGTH (type) = 0;
1857 TYPE_FIELDS (type) = 0;
1858 TYPE_NFIELDS(type) = 0;
1859 }
1860 else
1861 {
1862 type = coff_read_enum_type (cs->c_symnum,
1863 aux->x_sym.x_misc.x_lnsz.x_size,
1864 aux->x_sym.x_fcnary.x_fcn.x_endndx.l);
1865 }
1866 return type;
bd5635a1
RP
1867
1868 case T_MOE:
1869 /* shouldn't show up here */
1870 break;
1871
1872 case T_UCHAR:
318bf84f 1873 return lookup_fundamental_type (current_objfile, FT_UNSIGNED_CHAR);
bd5635a1
RP
1874
1875 case T_USHORT:
318bf84f 1876 return lookup_fundamental_type (current_objfile, FT_UNSIGNED_SHORT);
bd5635a1
RP
1877
1878 case T_UINT:
318bf84f 1879 return lookup_fundamental_type (current_objfile, FT_UNSIGNED_INTEGER);
bd5635a1
RP
1880
1881 case T_ULONG:
318bf84f 1882 return lookup_fundamental_type (current_objfile, FT_UNSIGNED_LONG);
bd5635a1 1883 }
7e258d18 1884 complain (&unexpected_type_complaint, cs->c_name);
318bf84f 1885 return lookup_fundamental_type (current_objfile, FT_VOID);
bd5635a1
RP
1886}
1887\f
1888/* This page contains subroutines of read_type. */
1889
7f4c8595
SS
1890/* Read the description of a structure (or union type) and return an
1891 object describing the type. */
bd5635a1
RP
1892
1893static struct type *
318bf84f 1894coff_read_struct_type (index, length, lastsym)
bd5635a1
RP
1895 int index;
1896 int length;
1897 int lastsym;
1898{
1899 struct nextfield
1900 {
1901 struct nextfield *next;
1902 struct field field;
1903 };
1904
1905 register struct type *type;
1906 register struct nextfield *list = 0;
1907 struct nextfield *new;
1908 int nfields = 0;
1909 register int n;
1910 char *name;
bd5635a1
RP
1911 struct coff_symbol member_sym;
1912 register struct coff_symbol *ms = &member_sym;
dcc35536
JG
1913 struct internal_syment sub_sym;
1914 union internal_auxent sub_aux;
bd5635a1
RP
1915 int done = 0;
1916
1917 type = coff_alloc_type (index);
1918 TYPE_CODE (type) = TYPE_CODE_STRUCT;
be8303da 1919 INIT_CPLUS_SPECIFIC(type);
bd5635a1
RP
1920 TYPE_LENGTH (type) = length;
1921
1922 while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
1923 {
1924 read_one_sym (ms, &sub_sym, &sub_aux);
1925 name = ms->c_name;
de9bef49 1926 name = EXTERNAL_NAME (name, current_objfile->obfd);
bd5635a1
RP
1927
1928 switch (ms->c_sclass)
1929 {
1930 case C_MOS:
1931 case C_MOU:
1932
1933 /* Get space to record the next field's data. */
1934 new = (struct nextfield *) alloca (sizeof (struct nextfield));
1935 new->next = list;
1936 list = new;
1937
1938 /* Save the data. */
ab93997d
JK
1939 list->field.name =
1940 obsavestring (name,
1941 strlen (name),
1942 &current_objfile->symbol_obstack);
bd5635a1
RP
1943 list->field.type = decode_type (ms, ms->c_type, &sub_aux);
1944 list->field.bitpos = 8 * ms->c_value;
1945 list->field.bitsize = 0;
1946 nfields++;
1947 break;
1948
1949 case C_FIELD:
1950
1951 /* Get space to record the next field's data. */
1952 new = (struct nextfield *) alloca (sizeof (struct nextfield));
1953 new->next = list;
1954 list = new;
1955
1956 /* Save the data. */
ab93997d
JK
1957 list->field.name =
1958 obsavestring (name,
1959 strlen (name),
1960 &current_objfile->symbol_obstack);
bd5635a1
RP
1961 list->field.type = decode_type (ms, ms->c_type, &sub_aux);
1962 list->field.bitpos = ms->c_value;
1963 list->field.bitsize = sub_aux.x_sym.x_misc.x_lnsz.x_size;
1964 nfields++;
1965 break;
1966
1967 case C_EOS:
1968 done = 1;
1969 break;
1970 }
1971 }
1972 /* Now create the vector of fields, and record how big it is. */
1973
1974 TYPE_NFIELDS (type) = nfields;
1975 TYPE_FIELDS (type) = (struct field *)
85f0a848 1976 TYPE_ALLOC (type, sizeof (struct field) * nfields);
bd5635a1
RP
1977
1978 /* Copy the saved-up fields into the field vector. */
1979
1980 for (n = nfields; list; list = list->next)
1981 TYPE_FIELD (type, --n) = list->field;
1982
1983 return type;
1984}
1985\f
1986/* Read a definition of an enumeration type,
1987 and create and return a suitable type object.
1988 Also defines the symbols that represent the values of the type. */
1989
61a7292f 1990/* ARGSUSED */
bd5635a1 1991static struct type *
318bf84f 1992coff_read_enum_type (index, length, lastsym)
bd5635a1
RP
1993 int index;
1994 int length;
1995 int lastsym;
1996{
1997 register struct symbol *sym;
1998 register struct type *type;
1999 int nsyms = 0;
2000 int done = 0;
c438b3af 2001 struct pending **symlist;
bd5635a1
RP
2002 struct coff_symbol member_sym;
2003 register struct coff_symbol *ms = &member_sym;
dcc35536
JG
2004 struct internal_syment sub_sym;
2005 union internal_auxent sub_aux;
c438b3af
JK
2006 struct pending *osyms, *syms;
2007 int o_nsyms;
bd5635a1
RP
2008 register int n;
2009 char *name;
bd5635a1
RP
2010
2011 type = coff_alloc_type (index);
2012 if (within_function)
c438b3af 2013 symlist = &local_symbols;
bd5635a1 2014 else
c438b3af 2015 symlist = &file_symbols;
bd5635a1 2016 osyms = *symlist;
c438b3af 2017 o_nsyms = osyms ? osyms->nsyms : 0;
bd5635a1
RP
2018
2019 while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
2020 {
2021 read_one_sym (ms, &sub_sym, &sub_aux);
2022 name = ms->c_name;
de9bef49 2023 name = EXTERNAL_NAME (name, current_objfile->obfd);
bd5635a1
RP
2024
2025 switch (ms->c_sclass)
2026 {
2027 case C_MOE:
ab93997d
JK
2028 sym = (struct symbol *) obstack_alloc
2029 (&current_objfile->symbol_obstack,
2030 sizeof (struct symbol));
4ed3a9ea 2031 memset (sym, 0, sizeof (struct symbol));
bd5635a1 2032
ab93997d
JK
2033 SYMBOL_NAME (sym) =
2034 obsavestring (name, strlen (name),
2035 &current_objfile->symbol_obstack);
bd5635a1
RP
2036 SYMBOL_CLASS (sym) = LOC_CONST;
2037 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
2038 SYMBOL_VALUE (sym) = ms->c_value;
c438b3af 2039 add_symbol_to_list (sym, symlist);
bd5635a1
RP
2040 nsyms++;
2041 break;
2042
2043 case C_EOS:
2044 /* Sometimes the linker (on 386/ix 2.0.2 at least) screws
2045 up the count of how many symbols to read. So stop
2046 on .eos. */
2047 done = 1;
2048 break;
2049 }
2050 }
2051
2052 /* Now fill in the fields of the type-structure. */
2053
f2c365f5 2054 if (length > 0)
7f4c8595 2055 TYPE_LENGTH (type) = length;
f2c365f5 2056 else
7f4c8595 2057 TYPE_LENGTH (type) = TARGET_INT_BIT / TARGET_CHAR_BIT; /* Assume ints */
bd5635a1
RP
2058 TYPE_CODE (type) = TYPE_CODE_ENUM;
2059 TYPE_NFIELDS (type) = nsyms;
2060 TYPE_FIELDS (type) = (struct field *)
85f0a848 2061 TYPE_ALLOC (type, sizeof (struct field) * nsyms);
bd5635a1
RP
2062
2063 /* Find the symbols for the values and put them into the type.
2064 The symbols can be found in the symlist that we put them on
2065 to cause them to be defined. osyms contains the old value
2066 of that symlist; everything up to there was defined by us. */
c438b3af
JK
2067 /* Note that we preserve the order of the enum constants, so
2068 that in something like "enum {FOO, LAST_THING=FOO}" we print
2069 FOO, not LAST_THING. */
bd5635a1 2070
c438b3af 2071 for (syms = *symlist, n = 0; syms; syms = syms->next)
bd5635a1 2072 {
c438b3af 2073 int j = 0;
7f4c8595 2074
c438b3af
JK
2075 if (syms == osyms)
2076 j = o_nsyms;
2077 for (; j < syms->nsyms; j++,n++)
2078 {
2079 struct symbol *xsym = syms->symbol[j];
2080 SYMBOL_TYPE (xsym) = type;
2081 TYPE_FIELD_NAME (type, n) = SYMBOL_NAME (xsym);
2082 TYPE_FIELD_VALUE (type, n) = 0;
2083 TYPE_FIELD_BITPOS (type, n) = SYMBOL_VALUE (xsym);
2084 TYPE_FIELD_BITSIZE (type, n) = 0;
2085 }
2086 if (syms == osyms)
2087 break;
bd5635a1 2088 }
c438b3af 2089
bd5635a1
RP
2090 return type;
2091}
2092
85f0a848
FF
2093struct section_offsets *
2094coff_symfile_offsets (objfile, addr)
2095 struct objfile *objfile;
2096 CORE_ADDR addr;
2097{
b5b186a2
SS
2098 struct section_offsets *section_offsets;
2099 int i;
4d57c599
JK
2100
2101 objfile->num_sections = SECT_OFF_MAX;
b5b186a2
SS
2102 section_offsets = (struct section_offsets *)
2103 obstack_alloc (&objfile -> psymbol_obstack,
4d57c599
JK
2104 sizeof (struct section_offsets)
2105 + sizeof (section_offsets->offsets) * (SECT_OFF_MAX-1));
b5b186a2
SS
2106
2107 for (i = 0; i < SECT_OFF_MAX; i++)
2108 ANOFFSET (section_offsets, i) = addr;
2109
2110 return section_offsets;
85f0a848
FF
2111}
2112
7f4c8595 2113/* Register our ability to parse symbols for coff BFD files. */
bd5635a1
RP
2114
2115static struct sym_fns coff_sym_fns =
2116{
0eed42de 2117 bfd_target_coff_flavour,
80d68b1d
FF
2118 coff_new_init, /* sym_new_init: init anything gbl to entire symtab */
2119 coff_symfile_init, /* sym_init: read initial info, setup for sym_read() */
2120 coff_symfile_read, /* sym_read: read a symbol file into symtab */
2121 coff_symfile_finish, /* sym_finish: finished with file, cleanup */
85f0a848 2122 coff_symfile_offsets, /* sym_offsets: xlate external to internal form */
80d68b1d 2123 NULL /* next: pointer to next struct sym_fns */
bd5635a1
RP
2124};
2125
2126void
2127_initialize_coffread ()
2128{
7f4c8595 2129 add_symtab_fns (&coff_sym_fns);
bd5635a1 2130}
This page took 0.271286 seconds and 4 git commands to generate.