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