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