* symtab.h (GLOBAL_BLOCK, STATIC_BLOCK, FIRST_LOCAL_BLOCK): New
[binutils-gdb.git] / gdb / coffread.c
1 /* Read coff symbol tables and convert to internal format, for GDB.
2 Design and support routines derived from dbxread.c, and UMAX COFF
3 specific routines written 9/1/87 by David D. Johnson, Brown University.
4 Revised 11/27/87 ddj@cs.brown.edu
5 Copyright (C) 1987-1991 Free Software Foundation, Inc.
6
7 This file is part of GDB.
8
9 GDB is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 1, or (at your option)
12 any later version.
13
14 GDB is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GDB; see the file COPYING. If not, write to
21 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 \f
23 #include <stdio.h>
24 #include "defs.h"
25 #include "param.h"
26 #include "symtab.h"
27 #include "breakpoint.h"
28 #include "bfd.h"
29 #include "libcoff.h" /* FIXME secret internal data from BFD */
30 #include "symfile.h"
31
32 #include <intel-coff.h>
33 #include <obstack.h>
34 #include <string.h>
35
36 static void add_symbol_to_list ();
37 static void read_coff_symtab ();
38 static void patch_opaque_types ();
39 static struct type *decode_function_type ();
40 static struct type *decode_type ();
41 static struct type *decode_base_type ();
42 static struct type *read_enum_type ();
43 static struct type *read_struct_type ();
44 static void finish_block ();
45 static struct blockvector *make_blockvector ();
46 static struct symbol *process_coff_symbol ();
47 static int init_stringtab ();
48 static void free_stringtab ();
49 static char *getfilename ();
50 static char *getsymname ();
51 static int init_lineno ();
52 static void enter_linenos ();
53 static void read_one_sym ();
54
55 extern int fclose ();
56 extern void free_all_symtabs ();
57 extern void free_all_psymtabs ();
58
59 /* external routines from the BFD library -- undocumented interface used
60 by GDB to read symbols. Move to libcoff.h. FIXME-SOMEDAY! */
61 extern void bfd_coff_swap_sym (/* symfile_bfd, &sym */);
62 extern void bfd_coff_swap_aux (/* symfile_bfd, &aux, type, sclass */);
63 extern void bfd_coff_swap_lineno (/* symfile_bfd, &lineno */);
64
65
66 /* Name of source file whose symbol data we are now processing.
67 This comes from a symbol named ".file". */
68
69 static char *last_source_file;
70
71 /* Core address of start and end of text of current source file.
72 This comes from a ".text" symbol where x_nlinno > 0. */
73
74 static CORE_ADDR cur_src_start_addr;
75 static CORE_ADDR cur_src_end_addr;
76
77 /* Core address of the end of the first object file. */
78 static CORE_ADDR first_object_file_end;
79
80 /* End of the text segment of the executable file,
81 as found in the symbol _etext. */
82
83 static CORE_ADDR end_of_text_addr;
84
85 /* The addresses of the symbol table stream and number of symbols
86 of the object file we are reading (as copied into core). */
87
88 static FILE *nlist_stream_global;
89 static int nlist_nsyms_global;
90
91 /* The entry point (starting address) of the file, if it is an executable. */
92
93 static CORE_ADDR entry_point;
94
95 /* The index in the symbol table of the last coff symbol that was processed. */
96
97 static int symnum;
98
99 /* Vector of types defined so far, indexed by their coff symnum. */
100
101 static struct typevector *type_vector;
102
103 /* Number of elements allocated for type_vector currently. */
104
105 static int type_vector_length;
106
107 /* Vector of line number information. */
108
109 static struct linetable *line_vector;
110
111 /* Index of next entry to go in line_vector_index. */
112
113 static int line_vector_index;
114
115 /* Last line number recorded in the line vector. */
116
117 static int prev_line_number;
118
119 /* Number of elements allocated for line_vector currently. */
120
121 static int line_vector_length;
122
123 #ifdef TDESC
124 #define SEM
125 int int_sem_val = 's' << 24 | 'e' << 16 | 'm' << 8 | '.';
126 int temp_sem_val;
127 int last_coffsem = 2;
128 int last_coffsyn = 0;
129 int debug_info = 0; /*used by tdesc */
130 extern int tdesc_handle;
131 extern int safe_to_init_tdesc_context;
132 #endif
133
134 /* Chain of typedefs of pointers to empty struct/union types.
135 They are chained thru the SYMBOL_VALUE_CHAIN. */
136
137 #define HASHSIZE 127
138 static struct symbol *opaque_type_chain[HASHSIZE];
139
140 /* Record the symbols defined for each context in a list.
141 We don't create a struct block for the context until we
142 know how long to make it. */
143
144 struct pending
145 {
146 struct pending *next;
147 struct symbol *symbol;
148 };
149
150 /* Here are the three lists that symbols are put on. */
151
152 struct pending *file_symbols; /* static at top level, and types */
153
154 struct pending *global_symbols; /* global functions and variables */
155
156 struct pending *local_symbols; /* everything local to lexical context */
157
158 /* List of unclosed lexical contexts
159 (that will become blocks, eventually). */
160
161 struct context_stack
162 {
163 struct context_stack *next;
164 struct pending *locals;
165 struct pending_block *old_blocks;
166 struct symbol *name;
167 CORE_ADDR start_addr;
168 int depth;
169 };
170
171 struct context_stack *context_stack;
172
173 /* Nonzero if within a function (so symbols should be local,
174 if nothing says specifically). */
175
176 int within_function;
177
178 /* List of blocks already made (lexical contexts already closed).
179 This is used at the end to make the blockvector. */
180
181 struct pending_block
182 {
183 struct pending_block *next;
184 struct block *block;
185 };
186
187 struct pending_block *pending_blocks;
188
189 extern CORE_ADDR startup_file_start; /* From blockframe.c */
190 extern CORE_ADDR startup_file_end; /* From blockframe.c */
191
192 /* Complaints about various problems in the file being read */
193
194 struct complaint ef_complaint =
195 {"Unmatched .ef symbol(s) ignored starting at symnum %d", 0, 0};
196
197 \f
198 /* Look up a coff type-number index. Return the address of the slot
199 where the type for that index is stored.
200 The type-number is in INDEX.
201
202 This can be used for finding the type associated with that index
203 or for associating a new type with the index. */
204
205 static struct type **
206 coff_lookup_type (index)
207 register int index;
208 {
209 if (index >= type_vector_length)
210 {
211 int old_vector_length = type_vector_length;
212
213 type_vector_length *= 2;
214 if (type_vector_length < index) {
215 type_vector_length = index * 2;
216 }
217 type_vector = (struct typevector *)
218 xrealloc (type_vector, sizeof (struct typevector)
219 + type_vector_length * sizeof (struct type *));
220 bzero (&type_vector->type[ old_vector_length ],
221 (type_vector_length - old_vector_length) * sizeof(struct type *));
222 }
223 return &type_vector->type[index];
224 }
225
226 /* Make sure there is a type allocated for type number index
227 and return the type object.
228 This can create an empty (zeroed) type object. */
229
230 static struct type *
231 coff_alloc_type (index)
232 int index;
233 {
234 register struct type **type_addr = coff_lookup_type (index);
235 register struct type *type = *type_addr;
236
237 /* If we are referring to a type not known at all yet,
238 allocate an empty type for it.
239 We will fill it in later if we find out how. */
240 if (type == 0)
241 {
242 type = (struct type *) obstack_alloc (symbol_obstack,
243 sizeof (struct type));
244 bzero (type, sizeof (struct type));
245 *type_addr = type;
246 }
247 return type;
248 }
249 \f
250 /* maintain the lists of symbols and blocks */
251
252 /* Add a symbol to one of the lists of symbols. */
253 static void
254 add_symbol_to_list (symbol, listhead)
255 struct symbol *symbol;
256 struct pending **listhead;
257 {
258 register struct pending *link
259 = (struct pending *) xmalloc (sizeof (struct pending));
260
261 link->next = *listhead;
262 link->symbol = symbol;
263 *listhead = link;
264 }
265
266 /* Take one of the lists of symbols and make a block from it.
267 Put the block on the list of pending blocks. */
268
269 static void
270 finish_block (symbol, listhead, old_blocks, start, end)
271 struct symbol *symbol;
272 struct pending **listhead;
273 struct pending_block *old_blocks;
274 CORE_ADDR start, end;
275 {
276 register struct pending *next, *next1;
277 register struct block *block;
278 register struct pending_block *pblock;
279 struct pending_block *opblock;
280 register int i;
281
282 /* Count the length of the list of symbols. */
283
284 for (next = *listhead, i = 0; next; next = next->next, i++);
285
286 block = (struct block *)
287 obstack_alloc (symbol_obstack, sizeof (struct block) + (i - 1) * sizeof (struct symbol *));
288
289 /* Copy the symbols into the block. */
290
291 BLOCK_NSYMS (block) = i;
292 for (next = *listhead; next; next = next->next)
293 BLOCK_SYM (block, --i) = next->symbol;
294
295 BLOCK_START (block) = start;
296 BLOCK_END (block) = end;
297 BLOCK_SUPERBLOCK (block) = 0; /* Filled in when containing block is made */
298
299 /* Put the block in as the value of the symbol that names it. */
300
301 if (symbol)
302 {
303 SYMBOL_BLOCK_VALUE (symbol) = block;
304 BLOCK_FUNCTION (block) = symbol;
305 }
306 else
307 BLOCK_FUNCTION (block) = 0;
308
309 /* Now free the links of the list, and empty the list. */
310
311 for (next = *listhead; next; next = next1)
312 {
313 next1 = next->next;
314 free (next);
315 }
316 *listhead = 0;
317
318 /* Install this block as the superblock
319 of all blocks made since the start of this scope
320 that don't have superblocks yet. */
321
322 opblock = 0;
323 for (pblock = pending_blocks; pblock != old_blocks; pblock = pblock->next)
324 {
325 if (BLOCK_SUPERBLOCK (pblock->block) == 0)
326 BLOCK_SUPERBLOCK (pblock->block) = block;
327 opblock = pblock;
328 }
329
330 /* Record this block on the list of all blocks in the file.
331 Put it after opblock, or at the beginning if opblock is 0.
332 This puts the block in the list after all its subblocks. */
333
334 pblock = (struct pending_block *) xmalloc (sizeof (struct pending_block));
335 pblock->block = block;
336 if (opblock)
337 {
338 pblock->next = opblock->next;
339 opblock->next = pblock;
340 }
341 else
342 {
343 pblock->next = pending_blocks;
344 pending_blocks = pblock;
345 }
346 }
347
348 static struct blockvector *
349 make_blockvector ()
350 {
351 register struct pending_block *next, *next1;
352 register struct blockvector *blockvector;
353 register int i;
354
355 /* Count the length of the list of blocks. */
356
357 for (next = pending_blocks, i = 0; next; next = next->next, i++);
358
359 blockvector = (struct blockvector *)
360 obstack_alloc (symbol_obstack, sizeof (struct blockvector) + (i - 1) * sizeof (struct block *));
361
362 /* Copy the blocks into the blockvector.
363 This is done in reverse order, which happens to put
364 the blocks into the proper order (ascending starting address).
365 finish_block has hair to insert each block into the list
366 after its subblocks in order to make sure this is true. */
367
368 BLOCKVECTOR_NBLOCKS (blockvector) = i;
369 for (next = pending_blocks; next; next = next->next)
370 BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
371
372 /* Now free the links of the list, and empty the list. */
373
374 for (next = pending_blocks; next; next = next1)
375 {
376 next1 = next->next;
377 free (next);
378 }
379 pending_blocks = 0;
380
381 return blockvector;
382 }
383
384 /* Manage the vector of line numbers. */
385
386 static
387 record_line (line, pc)
388 int line;
389 CORE_ADDR pc;
390 {
391 struct linetable_entry *e;
392 /* Make sure line vector is big enough. */
393
394 if (line_vector_index + 2 >= line_vector_length)
395 {
396 line_vector_length *= 2;
397 line_vector = (struct linetable *)
398 xrealloc (line_vector, sizeof (struct linetable)
399 + (line_vector_length
400 * sizeof (struct linetable_entry)));
401 }
402
403 e = line_vector->item + line_vector_index++;
404 e->line = line; e->pc = pc;
405 }
406 \f
407 /* Start a new symtab for a new source file.
408 This is called when a COFF ".file" symbol is seen;
409 it indicates the start of data for one original source file. */
410
411 static void
412 start_symtab ()
413 {
414 file_symbols = 0;
415 global_symbols = 0;
416 context_stack = 0;
417 within_function = 0;
418 last_source_file = 0;
419 #ifdef TDESC
420 last_coffsem = 2;
421 last_coffsyn = 0;
422 #endif
423
424 /* Initialize the source file information for this file. */
425
426 line_vector_index = 0;
427 line_vector_length = 1000;
428 prev_line_number = -2; /* Force first line number to be explicit */
429 line_vector = (struct linetable *)
430 xmalloc (sizeof (struct linetable)
431 + line_vector_length * sizeof (struct linetable_entry));
432 }
433
434 /* Save the vital information from when starting to read a file,
435 for use when closing off the current file.
436 NAME is the file name the symbols came from, START_ADDR is the first
437 text address for the file, and SIZE is the number of bytes of text. */
438
439 static void
440 complete_symtab (name, start_addr, size)
441 char *name;
442 CORE_ADDR start_addr;
443 unsigned int size;
444 {
445 last_source_file = savestring (name, strlen (name));
446 cur_src_start_addr = start_addr;
447 cur_src_end_addr = start_addr + size;
448
449 if (entry_point < cur_src_end_addr
450 && entry_point >= cur_src_start_addr)
451 {
452 startup_file_start = cur_src_start_addr;
453 startup_file_end = cur_src_end_addr;
454 }
455 }
456
457 /* Finish the symbol definitions for one main source file,
458 close off all the lexical contexts for that file
459 (creating struct block's for them), then make the
460 struct symtab for that file and put it in the list of all such. */
461
462 static void
463 end_symtab ()
464 {
465 register struct symtab *symtab;
466 register struct context_stack *cstk;
467 register struct blockvector *blockvector;
468 register struct linetable *lv;
469
470 /* Finish the lexical context of the last function in the file. */
471
472 if (context_stack)
473 {
474 cstk = context_stack;
475 context_stack = 0;
476 /* Make a block for the local symbols within. */
477 finish_block (cstk->name, &local_symbols, cstk->old_blocks,
478 cstk->start_addr, cur_src_end_addr);
479 free (cstk);
480 }
481
482 /* Ignore a file that has no functions with real debugging info. */
483 if (pending_blocks == 0 && file_symbols == 0 && global_symbols == 0)
484 {
485 free (line_vector);
486 line_vector = 0;
487 line_vector_length = -1;
488 last_source_file = 0;
489 return;
490 }
491
492 /* Create the two top-level blocks for this file. */
493 finish_block (0, &file_symbols, 0, cur_src_start_addr, cur_src_end_addr);
494 finish_block (0, &global_symbols, 0, cur_src_start_addr, cur_src_end_addr);
495
496 /* Create the blockvector that points to all the file's blocks. */
497 blockvector = make_blockvector ();
498
499 /* Now create the symtab object for this source file. */
500 symtab = (struct symtab *) xmalloc (sizeof (struct symtab));
501 symtab->free_ptr = 0;
502
503 /* Fill in its components. */
504 symtab->blockvector = blockvector;
505 symtab->free_code = free_linetable;
506 symtab->filename = last_source_file;
507 lv = line_vector;
508 lv->nitems = line_vector_index;
509 symtab->linetable = (struct linetable *)
510 xrealloc (lv, (sizeof (struct linetable)
511 + lv->nitems * sizeof (struct linetable_entry)));
512 symtab->nlines = 0;
513 symtab->line_charpos = 0;
514
515 #ifdef TDESC
516 symtab->coffsem = last_coffsem;
517 symtab->coffsyn = last_coffsyn;
518 #endif
519
520 /* Link the new symtab into the list of such. */
521 symtab->next = symtab_list;
522 symtab_list = symtab;
523
524 /* Reinitialize for beginning of new file. */
525 line_vector = 0;
526 line_vector_length = -1;
527 last_source_file = 0;
528 }
529 \f
530 static void
531 record_misc_function (name, address)
532 char *name;
533 CORE_ADDR address;
534 {
535 #ifdef TDESC
536 /* We don't want TDESC entry points on the misc_function_vector */
537 if (name[0] == '@') return;
538 #endif
539 /* mf_text isn't true, but apparently COFF doesn't tell us what it really
540 is, so this guess is more useful than mf_unknown. */
541 prim_record_misc_function (savestring (name, strlen (name)),
542 address,
543 (int)mf_text);
544 }
545 \f
546 /* coff_symfile_init ()
547 is the coff-specific initialization routine for reading symbols.
548 It is passed a struct sym_fns which contains, among other things,
549 the BFD for the file whose symbols are being read, and a slot for
550 a pointer to "private data" which we fill with cookies and other
551 treats for coff_symfile_read ().
552
553 We will only be called if this is a COFF or COFF-like file.
554 BFD handles figuring out the format of the file, and code in symtab.c
555 uses BFD's determination to vector to us.
556
557 The ultimate result is a new symtab (or, FIXME, eventually a psymtab). */
558
559 struct coff_symfile_info {
560 file_ptr min_lineno_offset; /* Where in file lowest line#s are */
561 file_ptr max_lineno_offset; /* 1+last byte of line#s in file */
562 };
563
564 void
565 coff_symfile_init (sf)
566 struct sym_fns *sf;
567 {
568 bfd *abfd = sf->sym_bfd;
569
570 /* Allocate struct to keep track of the symfile */
571 /* FIXME memory leak */
572 sf->sym_private = xmalloc (sizeof (struct coff_symfile_info));
573
574 #if defined (TDESC)
575 safe_to_init_tdesc_context = 0;
576 #endif
577
578 /* Save startup file's range of PC addresses to help blockframe.c
579 decide where the bottom of the stack is. */
580 if (bfd_get_file_flags (abfd) & EXEC_P)
581 {
582 /* Executable file -- record its entry point so we'll recognize
583 the startup file because it contains the entry point. */
584 entry_point = bfd_get_start_address (abfd);
585 }
586 else
587 {
588 /* Examination of non-executable.o files. Short-circuit this stuff. */
589 /* ~0 will not be in any file, we hope. */
590 entry_point = ~0;
591 /* set the startup file to be an empty range. */
592 startup_file_start = 0;
593 startup_file_end = 0;
594 }
595 }
596
597 /* This function is called for every section; it finds the outer limits
598 of the line table (minimum and maximum file offset) so that the
599 mainline code can read the whole thing for efficiency. */
600
601 static void
602 find_linenos (abfd, asect, vpinfo)
603 bfd *abfd;
604 sec_ptr asect;
605 void *vpinfo;
606 {
607 struct coff_symfile_info *info;
608 int size, count;
609 file_ptr offset, maxoff;
610
611 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
612 count = asect->lineno_count;
613 /* End of warning */
614
615 if (count == 0)
616 return;
617 size = count * sizeof (struct lineno);
618
619 info = (struct coff_symfile_info *)vpinfo;
620 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
621 offset = asect->line_filepos;
622 /* End of warning */
623
624 if (offset < info->min_lineno_offset || info->min_lineno_offset == 0)
625 info->min_lineno_offset = offset;
626
627 maxoff = offset + size;
628 if (maxoff > info->max_lineno_offset)
629 info->max_lineno_offset = maxoff;
630 }
631
632
633 /* Read a symbol file, after initialization by coff_symfile_init. */
634 /* FIXME! Addr and Mainline are not used yet -- this will not work for
635 shared libraries or add_file! */
636
637 void
638 coff_symfile_read (sf, addr, mainline)
639 struct sym_fns *sf;
640 CORE_ADDR addr;
641 int mainline;
642 {
643 struct coff_symfile_info *info = (struct coff_symfile_info *)sf->sym_private;
644 bfd *abfd = sf->sym_bfd;
645 char *name = bfd_get_filename (abfd);
646 int desc;
647 register int val;
648 int num_symbols;
649 int symtab_offset;
650 int stringtab_offset;
651
652 symfile_bfd = abfd; /* Kludge for swap routines */
653
654 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
655 desc = fileno ((FILE *)(abfd->iostream)); /* File descriptor */
656 num_symbols = bfd_get_symcount (abfd); /* How many syms */
657 symtab_offset = obj_sym_filepos (abfd); /* Symbol table file offset */
658 stringtab_offset = symtab_offset + num_symbols * SYMESZ; /* String tab */
659 /* End of warning */
660
661 #ifdef TDESC
662 debug_info = text_hdr.s_relptr;
663 if (tdesc_handle)
664 {
665 dc_terminate (tdesc_handle);
666 tdesc_handle = 0;
667 }
668 #endif
669
670 /* Read the line number table, all at once. */
671 info->min_lineno_offset = 0;
672 info->max_lineno_offset = 0;
673 bfd_map_over_sections (abfd, find_linenos, info);
674
675 val = init_lineno (desc, info->min_lineno_offset,
676 info->max_lineno_offset - info->min_lineno_offset);
677 if (val < 0)
678 error ("\"%s\": error reading line numbers\n", name);
679
680 /* Now read the string table, all at once. */
681
682 val = init_stringtab (desc, stringtab_offset);
683 if (val < 0)
684 {
685 free_all_symtabs (); /* FIXME blows whole symtab */
686 printf ("\"%s\": can't get string table", name);
687 fflush (stdout);
688 return;
689 }
690 make_cleanup (free_stringtab, 0);
691
692 /* Position to read the symbol table. Do not read it all at once. */
693 val = lseek (desc, (long)symtab_offset, 0);
694 if (val < 0)
695 perror_with_name (name);
696
697 init_misc_bunches ();
698 make_cleanup (discard_misc_bunches, 0);
699
700 /* Now that the executable file is positioned at symbol table,
701 process it and define symbols accordingly. */
702
703 read_coff_symtab (desc, num_symbols);
704
705 patch_opaque_types ();
706
707 /* Sort symbols alphabetically within each block. */
708
709 sort_all_symtab_syms ();
710
711 /* Go over the misc symbol bunches and install them in vector. */
712
713 condense_misc_bunches (0);
714
715 /* Make a default for file to list. */
716
717 select_source_symtab (0); /* FIXME, this might be too slow, see dbxread */
718 }
719
720 void
721 coff_symfile_discard ()
722 {
723 /* There seems to be nothing to do here. */
724 }
725
726 void
727 coff_new_init ()
728 {
729 /* There seems to be nothing to do except free_all_symtabs and set
730 symfile to zero, which is done by our caller. */
731 }
732 \f
733 /* Simplified internal version of coff symbol table information */
734
735 struct coff_symbol {
736 char *c_name;
737 int c_symnum; /* symbol number of this entry */
738 int c_nsyms; /* 1 if syment only, 2 if syment + auxent, etc */
739 long c_value;
740 int c_sclass;
741 int c_secnum;
742 unsigned int c_type;
743 };
744
745 /* Given pointers to a symbol table in coff style exec file,
746 analyze them and create struct symtab's describing the symbols.
747 NSYMS is the number of symbols in the symbol table.
748 We read them one at a time using read_one_sym (). */
749
750 static void
751 read_coff_symtab (desc, nsyms)
752 int desc;
753 int nsyms;
754 {
755 int newfd; /* Avoid multiple closes on same desc */
756 FILE *stream;
757 register struct context_stack *new;
758 struct coff_symbol coff_symbol;
759 register struct coff_symbol *cs = &coff_symbol;
760 static SYMENT main_sym;
761 static AUXENT main_aux;
762 struct coff_symbol fcn_cs_saved;
763 static SYMENT fcn_sym_saved;
764 static AUXENT fcn_aux_saved;
765
766 /* A .file is open. */
767 int in_source_file = 0;
768 int num_object_files = 0;
769 int next_file_symnum = -1;
770
771 /* Name of the current file. */
772 char *filestring = "";
773 int depth;
774 int fcn_first_line;
775 int fcn_last_line;
776 int fcn_start_addr;
777 long fcn_line_ptr;
778 struct cleanup *old_chain;
779
780
781 newfd = dup (desc);
782 if (newfd == -1)
783 fatal ("Too many open files");
784 stream = fdopen (newfd, "r");
785
786 old_chain = make_cleanup (free_all_symtabs, 0);
787 make_cleanup (fclose, stream);
788 nlist_stream_global = stream;
789 nlist_nsyms_global = nsyms;
790 last_source_file = 0;
791 bzero (opaque_type_chain, sizeof opaque_type_chain);
792
793 type_vector_length = 160;
794 type_vector = (struct typevector *)
795 xmalloc (sizeof (struct typevector)
796 + type_vector_length * sizeof (struct type *));
797 bzero (type_vector->type, type_vector_length * sizeof (struct type *));
798
799 start_symtab ();
800
801 symnum = 0;
802 while (symnum < nsyms)
803 {
804 QUIT; /* Make this command interruptable. */
805 read_one_sym (cs, &main_sym, &main_aux);
806
807 #ifdef SEM
808 temp_sem_val = cs->c_name[0] << 24 | cs->c_name[1] << 16 |
809 cs->c_name[2] << 8 | cs->c_name[3];
810 if (int_sem_val == temp_sem_val)
811 last_coffsem = (int) strtol (cs->c_name+4, (char **) NULL, 10);
812 #endif
813
814 if (cs->c_symnum == next_file_symnum && cs->c_sclass != C_FILE)
815 {
816 if (last_source_file)
817 end_symtab ();
818
819 start_symtab ();
820 complete_symtab ("_globals_", 0, first_object_file_end);
821 /* done with all files, everything from here on out is globals */
822 }
823
824 /* Special case for file with type declarations only, no text. */
825 if (!last_source_file && cs->c_type != T_NULL && cs->c_secnum == N_DEBUG)
826 complete_symtab (filestring, 0, 0);
827
828 /* Typedefs should not be treated as symbol definitions. */
829 if (ISFCN (cs->c_type) && cs->c_sclass != C_TPDEF)
830 {
831 /* record as misc function. if we get '.bf' next,
832 * then we undo this step
833 */
834 record_misc_function (cs->c_name, cs->c_value);
835
836 fcn_line_ptr = main_aux.x_sym.x_fcnary.x_fcn.x_lnnoptr;
837 fcn_start_addr = cs->c_value;
838 fcn_cs_saved = *cs;
839 fcn_sym_saved = main_sym;
840 fcn_aux_saved = main_aux;
841 continue;
842 }
843
844 switch (cs->c_sclass)
845 {
846 case C_EFCN:
847 case C_EXTDEF:
848 case C_ULABEL:
849 case C_USTATIC:
850 case C_LINE:
851 case C_ALIAS:
852 case C_HIDDEN:
853 printf ("Bad n_sclass = %d\n", cs->c_sclass);
854 break;
855
856 case C_FILE:
857 /*
858 * c_value field contains symnum of next .file entry in table
859 * or symnum of first global after last .file.
860 */
861 next_file_symnum = cs->c_value;
862 filestring = getfilename (&main_aux);
863 /*
864 * Complete symbol table for last object file
865 * containing debugging information.
866 */
867 if (last_source_file)
868 {
869 end_symtab ();
870 start_symtab ();
871 }
872 in_source_file = 1;
873 break;
874
875 case C_STAT:
876 if (cs->c_name[0] == '.') {
877 if (strcmp (cs->c_name, _TEXT) == 0) {
878 if (++num_object_files == 1) {
879 /* last address of startup file */
880 first_object_file_end = cs->c_value +
881 main_aux.x_scn.x_scnlen;
882 }
883 /* Check for in_source_file deals with case of
884 a file with debugging symbols
885 followed by a later file with no symbols. */
886 if (in_source_file)
887 complete_symtab (filestring, cs->c_value,
888 main_aux.x_scn.x_scnlen);
889 in_source_file = 0;
890 }
891 /* flush rest of '.' symbols */
892 break;
893 }
894 else if (cs->c_type == T_NULL
895 && cs->c_name[0] == 'L'
896 && (strncmp (cs->c_name, "LI%", 3) == 0
897 || strncmp (cs->c_name,"LC%",3) == 0
898 || strncmp (cs->c_name,"LP%",3) == 0
899 || strncmp (cs->c_name,"LPB%",4) == 0
900 || strncmp (cs->c_name,"LBB%",4) == 0
901 || strncmp (cs->c_name,"LBE%",4) == 0
902 || strncmp (cs->c_name,"LPBX%",5) == 0))
903 /* At least on a 3b1, gcc generates swbeg and string labels
904 that look like this. Ignore them. */
905 break;
906 /* fall in for static symbols that don't start with '.' */
907 case C_EXT:
908 if (cs->c_sclass == C_EXT &&
909 cs->c_secnum == N_ABS &&
910 strcmp (cs->c_name, _ETEXT) == 0)
911 end_of_text_addr = cs->c_value;
912 if (cs->c_type == T_NULL) {
913 if (cs->c_secnum <= 1) { /* text or abs */
914 record_misc_function (cs->c_name, cs->c_value);
915 break;
916 } else {
917 cs->c_type = T_INT;
918 }
919 }
920 (void) process_coff_symbol (cs, &main_aux);
921 break;
922
923 case C_FCN:
924 if (strcmp (cs->c_name, ".bf") == 0)
925 {
926 within_function = 1;
927
928 /* value contains address of first non-init type code */
929 /* main_aux.x_sym.x_misc.x_lnsz.x_lnno
930 contains line number of '{' } */
931 fcn_first_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
932
933 new = (struct context_stack *)
934 xmalloc (sizeof (struct context_stack));
935 new->depth = depth = 0;
936 new->next = 0;
937 context_stack = new;
938 new->locals = 0;
939 new->old_blocks = pending_blocks;
940 new->start_addr = fcn_start_addr;
941 fcn_cs_saved.c_name = getsymname (&fcn_sym_saved);
942 new->name = process_coff_symbol (&fcn_cs_saved,
943 &fcn_aux_saved);
944 }
945 else if (strcmp (cs->c_name, ".ef") == 0)
946 {
947 /* the value of .ef is the address of epilogue code;
948 * not useful for gdb
949 */
950 /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
951 contains number of lines to '}' */
952 new = context_stack;
953 if (new == 0)
954 {
955 complain (&ef_complaint, cs->c_symnum);
956 within_function = 0;
957 break;
958 }
959 fcn_last_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
960 enter_linenos (fcn_line_ptr, fcn_first_line, fcn_last_line);
961
962 finish_block (new->name, &local_symbols, new->old_blocks,
963 new->start_addr,
964 #if defined (FUNCTION_EPILOGUE_SIZE)
965 /* This macro should be defined only on
966 machines where the
967 fcn_aux_saved.x_sym.x_misc.x_fsize
968 field is always zero.
969 So use the .bf record information that
970 points to the epilogue and add the size
971 of the epilogue. */
972 cs->c_value + FUNCTION_EPILOGUE_SIZE
973 #else
974 fcn_cs_saved.c_value +
975 fcn_aux_saved.x_sym.x_misc.x_fsize
976 #endif
977 );
978 context_stack = 0;
979 within_function = 0;
980 free (new);
981 }
982 break;
983
984 case C_BLOCK:
985 if (strcmp (cs->c_name, ".bb") == 0)
986 {
987 new = (struct context_stack *)
988 xmalloc (sizeof (struct context_stack));
989 depth++;
990 new->depth = depth;
991 new->next = context_stack;
992 context_stack = new;
993 new->locals = local_symbols;
994 new->old_blocks = pending_blocks;
995 new->start_addr = cs->c_value;
996 new->name = 0;
997 local_symbols = 0;
998 }
999 else if (strcmp (cs->c_name, ".eb") == 0)
1000 {
1001 new = context_stack;
1002 if (new == 0 || depth != new->depth)
1003 error ("Invalid symbol data: .bb/.eb symbol mismatch at symbol %d.",
1004 symnum);
1005 if (local_symbols && context_stack->next)
1006 {
1007 /* Make a block for the local symbols within. */
1008 finish_block (0, &local_symbols, new->old_blocks,
1009 new->start_addr, cs->c_value);
1010 }
1011 depth--;
1012 local_symbols = new->locals;
1013 context_stack = new->next;
1014 free (new);
1015 }
1016 break;
1017 #ifdef TDESC
1018 case C_VERSION:
1019 if (strcmp (cs->c_name, ".coffsyn") == 0)
1020 last_coffsyn = cs->c_value;
1021 else if ((strcmp (cs->c_name, ".coffsem") == 0) &&
1022 (cs->c_value != 0))
1023 last_coffsem = cs->c_value;
1024 break;
1025 #endif
1026
1027 default:
1028 #ifdef TDESC
1029 if ((strcmp (cs->c_name, ".coffsem") == 0) &&
1030 (cs->c_value != 0))
1031 last_coffsem = cs->c_value;
1032 else
1033 #endif
1034 (void) process_coff_symbol (cs, &main_aux);
1035 break;
1036 }
1037 }
1038
1039 if (last_source_file)
1040 end_symtab ();
1041 fclose (stream);
1042 discard_cleanups (old_chain);
1043 }
1044 \f
1045 /* Routines for reading headers and symbols from executable. */
1046
1047 #ifdef FIXME
1048 /* Move these XXXMAGIC symbol defns into BFD! */
1049
1050 /* Read COFF file header, check magic number,
1051 and return number of symbols. */
1052 read_file_hdr (chan, file_hdr)
1053 int chan;
1054 FILHDR *file_hdr;
1055 {
1056 lseek (chan, 0L, 0);
1057 if (myread (chan, (char *)file_hdr, FILHSZ) < 0)
1058 return -1;
1059
1060 switch (file_hdr->f_magic)
1061 {
1062 #ifdef MC68MAGIC
1063 case MC68MAGIC:
1064 #endif
1065 #ifdef NS32GMAGIC
1066 case NS32GMAGIC:
1067 case NS32SMAGIC:
1068 #endif
1069 #ifdef I386MAGIC
1070 case I386MAGIC:
1071 #endif
1072 #ifdef CLIPPERMAGIC
1073 case CLIPPERMAGIC:
1074 #endif
1075 #if defined (MC68KWRMAGIC) \
1076 && (!defined (MC68MAGIC) || MC68KWRMAGIC != MC68MAGIC)
1077 case MC68KWRMAGIC:
1078 #endif
1079 #ifdef MC68KROMAGIC
1080 case MC68KROMAGIC:
1081 case MC68KPGMAGIC:
1082 #endif
1083 #ifdef MC88DGMAGIC
1084 case MC88DGMAGIC:
1085 #endif
1086 #ifdef MC88MAGIC
1087 case MC88MAGIC:
1088 #endif
1089 #ifdef I960ROMAGIC
1090 case I960ROMAGIC: /* Intel 960 */
1091 #endif
1092 #ifdef I960RWMAGIC
1093 case I960RWMAGIC: /* Intel 960 */
1094 #endif
1095 return file_hdr->f_nsyms;
1096
1097 default:
1098 #ifdef BADMAG
1099 if (BADMAG(file_hdr))
1100 return -1;
1101 else
1102 return file_hdr->f_nsyms;
1103 #else
1104 return -1;
1105 #endif
1106 }
1107 }
1108 #endif
1109
1110
1111 static void
1112 read_one_sym (cs, sym, aux)
1113 register struct coff_symbol *cs;
1114 register SYMENT *sym;
1115 register AUXENT *aux;
1116 {
1117 AUXENT temp_aux;
1118 int i;
1119
1120 cs->c_symnum = symnum;
1121 fread ((char *)sym, SYMESZ, 1, nlist_stream_global);
1122 bfd_coff_swap_sym (symfile_bfd, sym);
1123 cs->c_nsyms = (sym->n_numaux & 0xff) + 1;
1124 if (cs->c_nsyms >= 2)
1125 {
1126 fread ((char *)aux, AUXESZ, 1, nlist_stream_global);
1127 bfd_coff_swap_aux (symfile_bfd, aux, sym->n_type, sym->n_sclass);
1128 /* If more than one aux entry, read past it (only the first aux
1129 is important). */
1130 for (i = 2; i < cs->c_nsyms; i++)
1131 fread ((char *)&temp_aux, AUXESZ, 1, nlist_stream_global);
1132 }
1133 cs->c_name = getsymname (sym);
1134 cs->c_value = sym->n_value;
1135 cs->c_sclass = (sym->n_sclass & 0xff);
1136 cs->c_secnum = sym->n_scnum;
1137 cs->c_type = (unsigned) sym->n_type;
1138
1139 symnum += cs->c_nsyms;
1140 }
1141 \f
1142 /* Support for string table handling */
1143
1144 static char *stringtab = NULL;
1145
1146 static int
1147 init_stringtab (chan, offset)
1148 int chan;
1149 long offset;
1150 {
1151 long length;
1152 int val;
1153 unsigned char lengthbuf[4];
1154
1155 if (stringtab)
1156 {
1157 free (stringtab);
1158 stringtab = NULL;
1159 }
1160
1161 if (lseek (chan, offset, 0) < 0)
1162 return -1;
1163
1164 val = myread (chan, (char *)lengthbuf, sizeof lengthbuf);
1165 length = bfd_h_getlong (symfile_bfd, lengthbuf);
1166
1167 /* If no string table is needed, then the file may end immediately
1168 after the symbols. Just return with `stringtab' set to null. */
1169 if (val != sizeof length || length < sizeof length)
1170 return 0;
1171
1172 stringtab = (char *) xmalloc (length);
1173 if (stringtab == NULL)
1174 return -1;
1175
1176 bcopy (&length, stringtab, sizeof length);
1177 if (length == sizeof length) /* Empty table -- just the count */
1178 return 0;
1179
1180 val = myread (chan, stringtab + sizeof length, length - sizeof length);
1181 if (val != length - sizeof length || stringtab[length - 1] != '\0')
1182 return -1;
1183
1184 return 0;
1185 }
1186
1187 static void
1188 free_stringtab ()
1189 {
1190 if (stringtab)
1191 free (stringtab);
1192 stringtab = NULL;
1193 }
1194
1195 static char *
1196 getsymname (symbol_entry)
1197 SYMENT *symbol_entry;
1198 {
1199 static char buffer[SYMNMLEN+1];
1200 char *result;
1201
1202 if (symbol_entry->n_zeroes == 0)
1203 {
1204 result = stringtab + symbol_entry->n_offset;
1205 }
1206 else
1207 {
1208 strncpy (buffer, symbol_entry->n_name, SYMNMLEN);
1209 buffer[SYMNMLEN] = '\0';
1210 result = buffer;
1211 }
1212 return result;
1213 }
1214
1215 static char *
1216 getfilename (aux_entry)
1217 AUXENT *aux_entry;
1218 {
1219 static char buffer[BUFSIZ];
1220 register char *temp;
1221 char *result;
1222 extern char *rindex ();
1223
1224 #ifndef COFF_NO_LONG_FILE_NAMES
1225 if (aux_entry->x_file.x_n.x_zeroes == 0)
1226 strcpy (buffer, stringtab + aux_entry->x_file.x_n.x_offset);
1227 else
1228 #endif /* COFF_NO_LONG_FILE_NAMES */
1229 {
1230 #if defined (x_name)
1231 /* Data General. */
1232 strncpy (buffer, aux_entry->x_name, FILNMLEN);
1233 #else
1234 strncpy (buffer, aux_entry->x_file.x_fname, FILNMLEN);
1235 #endif
1236 buffer[FILNMLEN] = '\0';
1237 }
1238 result = buffer;
1239 if ((temp = rindex (result, '/')) != NULL)
1240 result = temp + 1;
1241 return (result);
1242 }
1243 \f
1244 /* Support for line number handling */
1245 static char *linetab = NULL;
1246 static long linetab_offset;
1247 static unsigned long linetab_size;
1248
1249 /* Read in all the line numbers for fast lookups later. */
1250
1251 static int
1252 init_lineno (chan, offset, size)
1253 int chan;
1254 long offset;
1255 int size;
1256 {
1257 int val;
1258 register char *p, *q;
1259
1260 if (lseek (chan, offset, 0) < 0)
1261 return -1;
1262
1263 linetab = (char *) xmalloc (size);
1264
1265 val = myread (chan, linetab, size);
1266 if (val != size)
1267 return -1;
1268
1269 /* Swap all entries */
1270 q = linetab + size;
1271 for (p = linetab; p < q; p += LINESZ)
1272 bfd_coff_swap_lineno (symfile_bfd, (LINENO *)p);
1273
1274 linetab_offset = offset;
1275 linetab_size = size;
1276 make_cleanup (free, linetab); /* Be sure it gets de-allocated. */
1277 return 0;
1278 }
1279
1280 #if !defined (L_LNNO32)
1281 #define L_LNNO32(lp) ((lp)->l_lnno)
1282 #endif
1283
1284 static void
1285 enter_linenos (file_offset, first_line, last_line)
1286 long file_offset;
1287 register int first_line;
1288 register int last_line;
1289 {
1290 register char *rawptr;
1291 struct lineno lptr;
1292
1293 if (file_offset < linetab_offset)
1294 {
1295 fprintf (stderr, "\nInvalid symbol file: file_offset < linetab_offset.");
1296 return;
1297 }
1298
1299 rawptr = &linetab[file_offset - linetab_offset];
1300
1301 /* skip first line entry for each function */
1302 rawptr += LINESZ;
1303 /* line numbers start at one for the first line of the function */
1304 first_line--;
1305
1306 /* Bcopy since occaisionally rawptr isn't pointing at long
1307 boundaries. FIXME we need to byteswap here!!! */
1308 for (bcopy (rawptr, &lptr, LINESZ);
1309 L_LNNO32 (&lptr) && L_LNNO32 (&lptr) <= last_line;
1310 rawptr += LINESZ, bcopy (rawptr, &lptr, LINESZ))
1311 {
1312 record_line (first_line + L_LNNO32 (&lptr), lptr.l_addr.l_paddr);
1313 }
1314 }
1315 \f
1316 static int
1317 hashname (name)
1318 char *name;
1319 {
1320 register char *p = name;
1321 register int total = p[0];
1322 register int c;
1323
1324 c = p[1];
1325 total += c << 2;
1326 if (c)
1327 {
1328 c = p[2];
1329 total += c << 4;
1330 if (c)
1331 total += p[3] << 6;
1332 }
1333
1334 return total % HASHSIZE;
1335 }
1336
1337 static void
1338 patch_type (type, real_type)
1339 struct type *type;
1340 struct type *real_type;
1341 {
1342 register struct type *target = TYPE_TARGET_TYPE (type);
1343 register struct type *real_target = TYPE_TARGET_TYPE (real_type);
1344 int field_size = TYPE_NFIELDS (real_target) * sizeof (struct field);
1345
1346 TYPE_LENGTH (target) = TYPE_LENGTH (real_target);
1347 TYPE_NFIELDS (target) = TYPE_NFIELDS (real_target);
1348 TYPE_FIELDS (target) = (struct field *)
1349 obstack_alloc (symbol_obstack, field_size);
1350
1351 bcopy (TYPE_FIELDS (real_target), TYPE_FIELDS (target), field_size);
1352
1353 if (TYPE_NAME (real_target))
1354 {
1355 if (TYPE_NAME (target))
1356 free (TYPE_NAME (target));
1357 TYPE_NAME (target) = concat (TYPE_NAME (real_target), "", "");
1358 }
1359 }
1360
1361 /* Patch up all appropriate typdef symbols in the opaque_type_chains
1362 so that they can be used to print out opaque data structures properly */
1363
1364 static void
1365 patch_opaque_types ()
1366 {
1367 struct symtab *s;
1368
1369 /* Look at each symbol in the per-file block of each symtab. */
1370 for (s = symtab_list; s; s = s->next)
1371 {
1372 register struct block *b;
1373 register int i;
1374
1375 /* Go through the per-file symbols only */
1376 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), 1);
1377 for (i = BLOCK_NSYMS (b) - 1; i >= 0; i--)
1378 {
1379 register struct symbol *real_sym;
1380
1381 /* Find completed typedefs to use to fix opaque ones.
1382 Remove syms from the chain when their types are stored,
1383 but search the whole chain, as there may be several syms
1384 from different files with the same name. */
1385 real_sym = BLOCK_SYM (b, i);
1386 if (SYMBOL_CLASS (real_sym) == LOC_TYPEDEF &&
1387 SYMBOL_NAMESPACE (real_sym) == VAR_NAMESPACE &&
1388 TYPE_CODE (SYMBOL_TYPE (real_sym)) == TYPE_CODE_PTR &&
1389 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (real_sym))) != 0)
1390 {
1391 register char *name = SYMBOL_NAME (real_sym);
1392 register int hash = hashname (name);
1393 register struct symbol *sym, *prev;
1394
1395 prev = 0;
1396 for (sym = opaque_type_chain[hash]; sym;)
1397 {
1398 if (name[0] == SYMBOL_NAME (sym)[0] &&
1399 !strcmp (name + 1, SYMBOL_NAME (sym) + 1))
1400 {
1401 if (prev)
1402 SYMBOL_VALUE_CHAIN (prev) = SYMBOL_VALUE_CHAIN (sym);
1403 else
1404 opaque_type_chain[hash] = SYMBOL_VALUE_CHAIN (sym);
1405
1406 patch_type (SYMBOL_TYPE (sym), SYMBOL_TYPE (real_sym));
1407
1408 if (prev)
1409 sym = SYMBOL_VALUE_CHAIN (prev);
1410 else
1411 sym = opaque_type_chain[hash];
1412 }
1413 else
1414 {
1415 prev = sym;
1416 sym = SYMBOL_VALUE_CHAIN (sym);
1417 }
1418 }
1419 }
1420 }
1421 }
1422 }
1423 \f
1424 #if defined (clipper)
1425 #define BELIEVE_PCC_PROMOTION 1
1426 #endif
1427
1428 static struct symbol *
1429 process_coff_symbol (cs, aux)
1430 register struct coff_symbol *cs;
1431 register AUXENT *aux;
1432 {
1433 register struct symbol *sym
1434 = (struct symbol *) obstack_alloc (symbol_obstack, sizeof (struct symbol));
1435 char *name;
1436 #ifdef NAMES_HAVE_UNDERSCORE
1437 int offset = 1;
1438 #else
1439 int offset = 0;
1440 #endif
1441
1442 bzero (sym, sizeof (struct symbol));
1443 name = cs->c_name;
1444 name = (name[0] == '_' ? name + offset : name);
1445 SYMBOL_NAME (sym) = obstack_copy0 (symbol_obstack, name, strlen (name));
1446
1447 /* default assumptions */
1448 SYMBOL_VALUE (sym) = cs->c_value;
1449 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1450
1451 if (ISFCN (cs->c_type))
1452 {
1453 SYMBOL_TYPE (sym) =
1454 lookup_function_type (decode_function_type (cs, cs->c_type, aux));
1455 SYMBOL_CLASS (sym) = LOC_BLOCK;
1456 if (cs->c_sclass == C_STAT)
1457 add_symbol_to_list (sym, &file_symbols);
1458 else if (cs->c_sclass == C_EXT)
1459 add_symbol_to_list (sym, &global_symbols);
1460 }
1461 else
1462 {
1463 SYMBOL_TYPE (sym) = decode_type (cs, cs->c_type, aux);
1464 switch (cs->c_sclass)
1465 {
1466 case C_NULL:
1467 break;
1468
1469 case C_AUTO:
1470 SYMBOL_CLASS (sym) = LOC_LOCAL;
1471 add_symbol_to_list (sym, &local_symbols);
1472 break;
1473
1474 case C_EXT:
1475 SYMBOL_CLASS (sym) = LOC_STATIC;
1476 SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
1477 add_symbol_to_list (sym, &global_symbols);
1478 break;
1479
1480 case C_STAT:
1481 SYMBOL_CLASS (sym) = LOC_STATIC;
1482 SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
1483 if (within_function) {
1484 /* Static symbol of local scope */
1485 add_symbol_to_list (sym, &local_symbols);
1486 }
1487 else {
1488 /* Static symbol at top level of file */
1489 add_symbol_to_list (sym, &file_symbols);
1490 }
1491 break;
1492
1493 case C_REG:
1494 SYMBOL_CLASS (sym) = LOC_REGISTER;
1495 add_symbol_to_list (sym, &local_symbols);
1496 break;
1497
1498 case C_LABEL:
1499 break;
1500
1501 case C_ARG:
1502 SYMBOL_CLASS (sym) = LOC_ARG;
1503 add_symbol_to_list (sym, &local_symbols);
1504 #if !defined (BELIEVE_PCC_PROMOTION)
1505 /* If PCC says a parameter is a short or a char,
1506 it is really an int. */
1507 if (SYMBOL_TYPE (sym) == builtin_type_char
1508 || SYMBOL_TYPE (sym) == builtin_type_short)
1509 SYMBOL_TYPE (sym) = builtin_type_int;
1510 else if (SYMBOL_TYPE (sym) == builtin_type_unsigned_char
1511 || SYMBOL_TYPE (sym) == builtin_type_unsigned_short)
1512 SYMBOL_TYPE (sym) = builtin_type_unsigned_int;
1513 #endif
1514 break;
1515
1516 case C_REGPARM:
1517 SYMBOL_CLASS (sym) = LOC_REGPARM;
1518 add_symbol_to_list (sym, &local_symbols);
1519 #if !defined (BELIEVE_PCC_PROMOTION)
1520 /* If PCC says a parameter is a short or a char,
1521 it is really an int. */
1522 if (SYMBOL_TYPE (sym) == builtin_type_char
1523 || SYMBOL_TYPE (sym) == builtin_type_short)
1524 SYMBOL_TYPE (sym) = builtin_type_int;
1525 else if (SYMBOL_TYPE (sym) == builtin_type_unsigned_char
1526 || SYMBOL_TYPE (sym) == builtin_type_unsigned_short)
1527 SYMBOL_TYPE (sym) = builtin_type_unsigned_int;
1528 #endif
1529 break;
1530
1531 case C_TPDEF:
1532 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
1533 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1534
1535 /* If type has no name, give it one */
1536 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0
1537 && (TYPE_FLAGS (SYMBOL_TYPE (sym)) & TYPE_FLAG_PERM) == 0)
1538 TYPE_NAME (SYMBOL_TYPE (sym))
1539 = concat (SYMBOL_NAME (sym), "", "");
1540
1541 /* Keep track of any type which points to empty structured type,
1542 so it can be filled from a definition from another file */
1543 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_PTR &&
1544 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym))) == 0)
1545 {
1546 register int i = hashname (SYMBOL_NAME (sym));
1547
1548 SYMBOL_VALUE_CHAIN (sym) = opaque_type_chain[i];
1549 opaque_type_chain[i] = sym;
1550 }
1551 add_symbol_to_list (sym, &file_symbols);
1552 break;
1553
1554 case C_STRTAG:
1555 case C_UNTAG:
1556 case C_ENTAG:
1557 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
1558 SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
1559 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0
1560 && (TYPE_FLAGS (SYMBOL_TYPE (sym)) & TYPE_FLAG_PERM) == 0)
1561 TYPE_NAME (SYMBOL_TYPE (sym))
1562 = concat ("",
1563 (cs->c_sclass == C_ENTAG
1564 ? "enum "
1565 : (cs->c_sclass == C_STRTAG
1566 ? "struct " : "union ")),
1567 SYMBOL_NAME (sym));
1568 add_symbol_to_list (sym, &file_symbols);
1569 break;
1570
1571 default:
1572 break;
1573 }
1574 }
1575 return sym;
1576 }
1577 \f
1578 /* Decode a coff type specifier;
1579 return the type that is meant. */
1580
1581 static
1582 struct type *
1583 decode_type (cs, c_type, aux)
1584 register struct coff_symbol *cs;
1585 unsigned int c_type;
1586 register AUXENT *aux;
1587 {
1588 register struct type *type = 0;
1589 unsigned int new_c_type;
1590
1591 if (c_type & ~N_BTMASK)
1592 {
1593 new_c_type = DECREF (c_type);
1594 if (ISPTR (c_type))
1595 {
1596 type = decode_type (cs, new_c_type, aux);
1597 type = lookup_pointer_type (type);
1598 }
1599 else if (ISFCN (c_type))
1600 {
1601 type = decode_type (cs, new_c_type, aux);
1602 type = lookup_function_type (type);
1603 }
1604 else if (ISARY (c_type))
1605 {
1606 int i, n;
1607 register unsigned short *dim;
1608 struct type *base_type;
1609
1610 /* Define an array type. */
1611 /* auxent refers to array, not base type */
1612 if (aux->x_sym.x_tagndx == 0)
1613 cs->c_nsyms = 1;
1614
1615 /* shift the indices down */
1616 dim = &aux->x_sym.x_fcnary.x_ary.x_dimen[0];
1617 i = 1;
1618 n = dim[0];
1619 for (i = 0; *dim && i < DIMNUM - 1; i++, dim++)
1620 *dim = *(dim + 1);
1621 *dim = 0;
1622
1623 type = (struct type *)
1624 obstack_alloc (symbol_obstack, sizeof (struct type));
1625 bzero (type, sizeof (struct type));
1626
1627 base_type = decode_type (cs, new_c_type, aux);
1628
1629 TYPE_CODE (type) = TYPE_CODE_ARRAY;
1630 TYPE_TARGET_TYPE (type) = base_type;
1631 TYPE_LENGTH (type) = n * TYPE_LENGTH (base_type);
1632 }
1633 return type;
1634 }
1635
1636 /* Reference to existing type */
1637 if (cs->c_nsyms > 1 && aux->x_sym.x_tagndx != 0)
1638 {
1639 type = coff_alloc_type (aux->x_sym.x_tagndx);
1640 return type;
1641 }
1642
1643 return decode_base_type (cs, BTYPE (c_type), aux);
1644 }
1645
1646 /* Decode a coff type specifier for function definition;
1647 return the type that the function returns. */
1648
1649 static
1650 struct type *
1651 decode_function_type (cs, c_type, aux)
1652 register struct coff_symbol *cs;
1653 unsigned int c_type;
1654 register AUXENT *aux;
1655 {
1656 if (aux->x_sym.x_tagndx == 0)
1657 cs->c_nsyms = 1; /* auxent refers to function, not base type */
1658
1659 return decode_type (cs, DECREF (c_type), aux);
1660 }
1661 \f
1662 /* basic C types */
1663
1664 static
1665 struct type *
1666 decode_base_type (cs, c_type, aux)
1667 register struct coff_symbol *cs;
1668 unsigned int c_type;
1669 register AUXENT *aux;
1670 {
1671 struct type *type;
1672
1673 switch (c_type)
1674 {
1675 case T_NULL:
1676 /* shows up with "void (*foo)();" structure members */
1677 return builtin_type_void;
1678
1679 #ifdef T_ARG
1680 case T_ARG:
1681 /* Shows up in DGUX, I think. Not sure where. */
1682 return builtin_type_void; /* shouldn't show up here */
1683 #endif
1684
1685 #ifdef T_VOID
1686 case T_VOID:
1687 /* Intel 960 COFF has this symbol and meaning. */
1688 return builtin_type_void;
1689 #endif
1690
1691 case T_CHAR:
1692 return builtin_type_char;
1693
1694 case T_SHORT:
1695 return builtin_type_short;
1696
1697 case T_INT:
1698 return builtin_type_int;
1699
1700 case T_LONG:
1701 return builtin_type_long;
1702
1703 case T_FLOAT:
1704 return builtin_type_float;
1705
1706 case T_DOUBLE:
1707 return builtin_type_double;
1708
1709 case T_STRUCT:
1710 if (cs->c_nsyms != 2)
1711 {
1712 /* anonymous structure type */
1713 type = coff_alloc_type (cs->c_symnum);
1714 TYPE_CODE (type) = TYPE_CODE_STRUCT;
1715 TYPE_NAME (type) = concat ("struct ", "<opaque>", "");
1716 TYPE_LENGTH (type) = 0;
1717 TYPE_FIELDS (type) = 0;
1718 TYPE_NFIELDS (type) = 0;
1719 }
1720 else
1721 {
1722 type = read_struct_type (cs->c_symnum,
1723 aux->x_sym.x_misc.x_lnsz.x_size,
1724 aux->x_sym.x_fcnary.x_fcn.x_endndx);
1725 }
1726 return type;
1727
1728 case T_UNION:
1729 if (cs->c_nsyms != 2)
1730 {
1731 /* anonymous union type */
1732 type = coff_alloc_type (cs->c_symnum);
1733 TYPE_NAME (type) = concat ("union ", "<opaque>", "");
1734 TYPE_LENGTH (type) = 0;
1735 TYPE_FIELDS (type) = 0;
1736 TYPE_NFIELDS (type) = 0;
1737 }
1738 else
1739 {
1740 type = read_struct_type (cs->c_symnum,
1741 aux->x_sym.x_misc.x_lnsz.x_size,
1742 aux->x_sym.x_fcnary.x_fcn.x_endndx);
1743 }
1744 TYPE_CODE (type) = TYPE_CODE_UNION;
1745 return type;
1746
1747 case T_ENUM:
1748 return read_enum_type (cs->c_symnum,
1749 aux->x_sym.x_misc.x_lnsz.x_size,
1750 aux->x_sym.x_fcnary.x_fcn.x_endndx);
1751
1752 case T_MOE:
1753 /* shouldn't show up here */
1754 break;
1755
1756 case T_UCHAR:
1757 return builtin_type_unsigned_char;
1758
1759 case T_USHORT:
1760 return builtin_type_unsigned_short;
1761
1762 case T_UINT:
1763 return builtin_type_unsigned_int;
1764
1765 case T_ULONG:
1766 return builtin_type_unsigned_long;
1767 }
1768 printf ("unexpected type %d at symnum %d\n", c_type, cs->c_symnum);
1769 return builtin_type_void;
1770 }
1771 \f
1772 /* This page contains subroutines of read_type. */
1773
1774 /* Read the description of a structure (or union type)
1775 and return an object describing the type. */
1776
1777 static struct type *
1778 read_struct_type (index, length, lastsym)
1779 int index;
1780 int length;
1781 int lastsym;
1782 {
1783 struct nextfield
1784 {
1785 struct nextfield *next;
1786 struct field field;
1787 };
1788
1789 register struct type *type;
1790 register struct nextfield *list = 0;
1791 struct nextfield *new;
1792 int nfields = 0;
1793 register int n;
1794 char *name;
1795 #ifdef NAMES_HAVE_UNDERSCORE
1796 int offset = 1;
1797 #else
1798 int offset = 0;
1799 #endif
1800 struct coff_symbol member_sym;
1801 register struct coff_symbol *ms = &member_sym;
1802 SYMENT sub_sym;
1803 AUXENT sub_aux;
1804 int done = 0;
1805
1806 type = coff_alloc_type (index);
1807 TYPE_CODE (type) = TYPE_CODE_STRUCT;
1808 TYPE_LENGTH (type) = length;
1809
1810 while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
1811 {
1812 read_one_sym (ms, &sub_sym, &sub_aux);
1813 name = ms->c_name;
1814 name = (name[0] == '_' ? name + offset : name);
1815
1816 switch (ms->c_sclass)
1817 {
1818 case C_MOS:
1819 case C_MOU:
1820
1821 /* Get space to record the next field's data. */
1822 new = (struct nextfield *) alloca (sizeof (struct nextfield));
1823 new->next = list;
1824 list = new;
1825
1826 /* Save the data. */
1827 list->field.name = savestring (name, strlen (name));
1828 list->field.type = decode_type (ms, ms->c_type, &sub_aux);
1829 list->field.bitpos = 8 * ms->c_value;
1830 list->field.bitsize = 0;
1831 nfields++;
1832 break;
1833
1834 case C_FIELD:
1835
1836 /* Get space to record the next field's data. */
1837 new = (struct nextfield *) alloca (sizeof (struct nextfield));
1838 new->next = list;
1839 list = new;
1840
1841 /* Save the data. */
1842 list->field.name = savestring (name, strlen (name));
1843 list->field.type = decode_type (ms, ms->c_type, &sub_aux);
1844 list->field.bitpos = ms->c_value;
1845 list->field.bitsize = sub_aux.x_sym.x_misc.x_lnsz.x_size;
1846 nfields++;
1847 break;
1848
1849 case C_EOS:
1850 done = 1;
1851 break;
1852 }
1853 }
1854 /* Now create the vector of fields, and record how big it is. */
1855
1856 TYPE_NFIELDS (type) = nfields;
1857 TYPE_FIELDS (type) = (struct field *)
1858 obstack_alloc (symbol_obstack, sizeof (struct field) * nfields);
1859
1860 /* Copy the saved-up fields into the field vector. */
1861
1862 for (n = nfields; list; list = list->next)
1863 TYPE_FIELD (type, --n) = list->field;
1864
1865 return type;
1866 }
1867 \f
1868 /* Read a definition of an enumeration type,
1869 and create and return a suitable type object.
1870 Also defines the symbols that represent the values of the type. */
1871
1872 static struct type *
1873 read_enum_type (index, length, lastsym)
1874 int index;
1875 int length;
1876 int lastsym;
1877 {
1878 register struct symbol *sym;
1879 register struct type *type;
1880 int nsyms = 0;
1881 int done = 0;
1882 struct pending **symlist;
1883 struct coff_symbol member_sym;
1884 register struct coff_symbol *ms = &member_sym;
1885 SYMENT sub_sym;
1886 AUXENT sub_aux;
1887 struct pending *osyms, *syms;
1888 register int n;
1889 char *name;
1890 #ifdef NAMES_HAVE_UNDERSCORE
1891 int offset = 1;
1892 #else
1893 int offset = 0;
1894 #endif
1895
1896 type = coff_alloc_type (index);
1897 if (within_function)
1898 symlist = &local_symbols;
1899 else
1900 symlist = &file_symbols;
1901 osyms = *symlist;
1902
1903 while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
1904 {
1905 read_one_sym (ms, &sub_sym, &sub_aux);
1906 name = ms->c_name;
1907 name = (name[0] == '_' ? name + offset : name);
1908
1909 switch (ms->c_sclass)
1910 {
1911 case C_MOE:
1912 sym = (struct symbol *) xmalloc (sizeof (struct symbol));
1913 bzero (sym, sizeof (struct symbol));
1914
1915 SYMBOL_NAME (sym) = savestring (name, strlen (name));
1916 SYMBOL_CLASS (sym) = LOC_CONST;
1917 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1918 SYMBOL_VALUE (sym) = ms->c_value;
1919 add_symbol_to_list (sym, symlist);
1920 nsyms++;
1921 break;
1922
1923 case C_EOS:
1924 /* Sometimes the linker (on 386/ix 2.0.2 at least) screws
1925 up the count of how many symbols to read. So stop
1926 on .eos. */
1927 done = 1;
1928 break;
1929 }
1930 }
1931
1932 /* Now fill in the fields of the type-structure. */
1933
1934 TYPE_LENGTH (type) = sizeof (int);
1935 TYPE_CODE (type) = TYPE_CODE_ENUM;
1936 TYPE_NFIELDS (type) = nsyms;
1937 TYPE_FIELDS (type) = (struct field *)
1938 obstack_alloc (symbol_obstack, sizeof (struct field) * nsyms);
1939
1940 /* Find the symbols for the values and put them into the type.
1941 The symbols can be found in the symlist that we put them on
1942 to cause them to be defined. osyms contains the old value
1943 of that symlist; everything up to there was defined by us. */
1944
1945 for (syms = *symlist, n = nsyms; syms != osyms; syms = syms->next)
1946 {
1947 SYMBOL_TYPE (syms->symbol) = type;
1948 TYPE_FIELD_NAME (type, --n) = SYMBOL_NAME (syms->symbol);
1949 TYPE_FIELD_VALUE (type, n) = 0;
1950 TYPE_FIELD_BITPOS (type, n) = SYMBOL_VALUE (syms->symbol);
1951 TYPE_FIELD_BITSIZE (type, n) = 0;
1952 }
1953 return type;
1954 }
1955
1956 /* Register our ability to parse symbols for coff BFD files */
1957
1958 static struct sym_fns coff_sym_fns =
1959 {
1960 "coff", 4,
1961 coff_new_init, coff_symfile_init,
1962 coff_symfile_read, coff_symfile_discard
1963 };
1964
1965 void
1966 _initialize_coffread ()
1967 {
1968 add_symtab_fns(&coff_sym_fns);
1969 }