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