The list of changes is too long to fit in the cvs log (since it truncates!).
[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 void
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 /* ARGSUSED */
620 static void
621 find_linenos (abfd, asect, vpinfo)
622 bfd *abfd;
623 sec_ptr asect;
624 void *vpinfo;
625 {
626 struct coff_symfile_info *info;
627 int size, count;
628 file_ptr offset, maxoff;
629
630 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
631 count = asect->lineno_count;
632 /* End of warning */
633
634 if (count == 0)
635 return;
636 #if !defined (LINESZ)
637 /* Just in case, you never know what to expect from those
638 COFF header files. */
639 #define LINESZ (sizeof (struct lineno))
640 #endif /* No LINESZ. */
641 size = count * LINESZ;
642
643 info = (struct coff_symfile_info *)vpinfo;
644 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
645 offset = asect->line_filepos;
646 /* End of warning */
647
648 if (offset < info->min_lineno_offset || info->min_lineno_offset == 0)
649 info->min_lineno_offset = offset;
650
651 maxoff = offset + size;
652 if (maxoff > info->max_lineno_offset)
653 info->max_lineno_offset = maxoff;
654 }
655
656
657 /* The BFD for this file -- only good while we're actively reading
658 symbols into a psymtab or a symtab. */
659
660 static bfd *symfile_bfd;
661
662 /* Read a symbol file, after initialization by coff_symfile_init. */
663 /* FIXME! Addr and Mainline are not used yet -- this will not work for
664 shared libraries or add_file! */
665
666 void
667 coff_symfile_read (sf, addr, mainline)
668 struct sym_fns *sf;
669 CORE_ADDR addr;
670 int mainline;
671 {
672 struct coff_symfile_info *info = (struct coff_symfile_info *)sf->sym_private;
673 bfd *abfd = sf->sym_bfd;
674 char *name = bfd_get_filename (abfd);
675 int desc;
676 register int val;
677 int num_symbols;
678 int symtab_offset;
679 int stringtab_offset;
680
681 symfile_bfd = abfd; /* Kludge for swap routines */
682
683 /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */
684 desc = fileno ((FILE *)(abfd->iostream)); /* File descriptor */
685 num_symbols = bfd_get_symcount (abfd); /* How many syms */
686 symtab_offset = obj_sym_filepos (abfd); /* Symbol table file offset */
687 stringtab_offset = symtab_offset + num_symbols * SYMESZ; /* String tab */
688 /* End of warning */
689
690 #ifdef TDESC
691 debug_info = text_hdr.s_relptr;
692 if (tdesc_handle)
693 {
694 dc_terminate (tdesc_handle);
695 tdesc_handle = 0;
696 }
697 #endif
698
699 /* Read the line number table, all at once. */
700 info->min_lineno_offset = 0;
701 info->max_lineno_offset = 0;
702 bfd_map_over_sections (abfd, find_linenos, info);
703
704 val = init_lineno (desc, info->min_lineno_offset,
705 info->max_lineno_offset - info->min_lineno_offset);
706 if (val < 0)
707 error ("\"%s\": error reading line numbers\n", name);
708
709 /* Now read the string table, all at once. */
710
711 val = init_stringtab (desc, stringtab_offset);
712 if (val < 0)
713 {
714 free_all_symtabs (); /* FIXME blows whole symtab */
715 printf ("\"%s\": can't get string table", name);
716 fflush (stdout);
717 return;
718 }
719 make_cleanup (free_stringtab, 0);
720
721 /* Position to read the symbol table. Do not read it all at once. */
722 val = lseek (desc, (long)symtab_offset, 0);
723 if (val < 0)
724 perror_with_name (name);
725
726 init_misc_bunches ();
727 make_cleanup (discard_misc_bunches, 0);
728
729 /* Now that the executable file is positioned at symbol table,
730 process it and define symbols accordingly. */
731
732 read_coff_symtab (desc, num_symbols);
733
734 patch_opaque_types ();
735
736 /* Sort symbols alphabetically within each block. */
737
738 sort_all_symtab_syms ();
739
740 /* Go over the misc symbol bunches and install them in vector. */
741
742 condense_misc_bunches (0);
743
744 /* Make a default for file to list. */
745
746 select_source_symtab (0); /* FIXME, this might be too slow, see dbxread */
747 }
748
749 void
750 coff_symfile_discard ()
751 {
752 /* There seems to be nothing to do here. */
753 }
754
755 void
756 coff_new_init ()
757 {
758 /* There seems to be nothing to do except free_all_symtabs and set
759 symfile to zero, which is done by our caller. */
760 }
761 \f
762 /* Simplified internal version of coff symbol table information */
763
764 struct coff_symbol {
765 char *c_name;
766 int c_symnum; /* symbol number of this entry */
767 int c_nsyms; /* 1 if syment only, 2 if syment + auxent, etc */
768 long c_value;
769 int c_sclass;
770 int c_secnum;
771 unsigned int c_type;
772 };
773
774 /* Given pointers to a symbol table in coff style exec file,
775 analyze them and create struct symtab's describing the symbols.
776 NSYMS is the number of symbols in the symbol table.
777 We read them one at a time using read_one_sym (). */
778
779 static void
780 read_coff_symtab (desc, nsyms)
781 int desc;
782 int nsyms;
783 {
784 int newfd; /* Avoid multiple closes on same desc */
785 FILE *stream;
786 register struct context_stack *new;
787 struct coff_symbol coff_symbol;
788 register struct coff_symbol *cs = &coff_symbol;
789 static SYMENT main_sym;
790 static AUXENT main_aux;
791 struct coff_symbol fcn_cs_saved;
792 static SYMENT fcn_sym_saved;
793 static AUXENT fcn_aux_saved;
794
795 /* A .file is open. */
796 int in_source_file = 0;
797 int num_object_files = 0;
798 int next_file_symnum = -1;
799
800 /* Name of the current file. */
801 char *filestring = "";
802 int depth;
803 int fcn_first_line;
804 int fcn_last_line;
805 int fcn_start_addr;
806 long fcn_line_ptr;
807 struct cleanup *old_chain;
808
809
810 newfd = dup (desc);
811 if (newfd == -1)
812 fatal ("Too many open files");
813 stream = fdopen (newfd, "r");
814
815 old_chain = make_cleanup (free_all_symtabs, 0);
816 make_cleanup (fclose, stream);
817 nlist_stream_global = stream;
818 nlist_nsyms_global = nsyms;
819 last_source_file = 0;
820 bzero (opaque_type_chain, sizeof opaque_type_chain);
821
822 type_vector_length = 160;
823 type_vector = (struct typevector *)
824 xmalloc (sizeof (struct typevector)
825 + type_vector_length * sizeof (struct type *));
826 bzero (type_vector->type, type_vector_length * sizeof (struct type *));
827
828 start_symtab ();
829
830 symnum = 0;
831 while (symnum < nsyms)
832 {
833 QUIT; /* Make this command interruptable. */
834 read_one_sym (cs, &main_sym, &main_aux);
835
836 #ifdef SEM
837 temp_sem_val = cs->c_name[0] << 24 | cs->c_name[1] << 16 |
838 cs->c_name[2] << 8 | cs->c_name[3];
839 if (int_sem_val == temp_sem_val)
840 last_coffsem = (int) strtol (cs->c_name+4, (char **) NULL, 10);
841 #endif
842
843 if (cs->c_symnum == next_file_symnum && cs->c_sclass != C_FILE)
844 {
845 if (last_source_file)
846 end_symtab ();
847
848 start_symtab ();
849 complete_symtab ("_globals_", 0, first_object_file_end);
850 /* done with all files, everything from here on out is globals */
851 }
852
853 /* Special case for file with type declarations only, no text. */
854 if (!last_source_file && SDB_TYPE (cs->c_type)
855 && cs->c_secnum == N_DEBUG)
856 complete_symtab (filestring, 0, 0);
857
858 /* Typedefs should not be treated as symbol definitions. */
859 if (ISFCN (cs->c_type) && cs->c_sclass != C_TPDEF)
860 {
861 /* record as misc function. if we get '.bf' next,
862 * then we undo this step
863 */
864 record_misc_function (cs->c_name, cs->c_value);
865
866 fcn_line_ptr = main_aux.x_sym.x_fcnary.x_fcn.x_lnnoptr;
867 fcn_start_addr = cs->c_value;
868 fcn_cs_saved = *cs;
869 fcn_sym_saved = main_sym;
870 fcn_aux_saved = main_aux;
871 continue;
872 }
873
874 switch (cs->c_sclass)
875 {
876 case C_EFCN:
877 case C_EXTDEF:
878 case C_ULABEL:
879 case C_USTATIC:
880 case C_LINE:
881 case C_ALIAS:
882 case C_HIDDEN:
883 printf ("Bad n_sclass = %d\n", cs->c_sclass);
884 break;
885
886 case C_FILE:
887 /*
888 * c_value field contains symnum of next .file entry in table
889 * or symnum of first global after last .file.
890 */
891 next_file_symnum = cs->c_value;
892 filestring = getfilename (&main_aux);
893 /*
894 * Complete symbol table for last object file
895 * containing debugging information.
896 */
897 if (last_source_file)
898 {
899 end_symtab ();
900 start_symtab ();
901 }
902 in_source_file = 1;
903 break;
904
905 case C_STAT:
906 if (cs->c_name[0] == '.') {
907 if (strcmp (cs->c_name, _TEXT) == 0) {
908 if (++num_object_files == 1) {
909 /* last address of startup file */
910 first_object_file_end = cs->c_value +
911 main_aux.x_scn.x_scnlen;
912 }
913 /* Check for in_source_file deals with case of
914 a file with debugging symbols
915 followed by a later file with no symbols. */
916 if (in_source_file)
917 complete_symtab (filestring, cs->c_value,
918 main_aux.x_scn.x_scnlen);
919 in_source_file = 0;
920 }
921 /* flush rest of '.' symbols */
922 break;
923 }
924 else if (!SDB_TYPE (cs->c_type)
925 && cs->c_name[0] == 'L'
926 && (strncmp (cs->c_name, "LI%", 3) == 0
927 || strncmp (cs->c_name, "LF%", 3) == 0
928 || strncmp (cs->c_name,"LC%",3) == 0
929 || strncmp (cs->c_name,"LP%",3) == 0
930 || strncmp (cs->c_name,"LPB%",4) == 0
931 || strncmp (cs->c_name,"LBB%",4) == 0
932 || strncmp (cs->c_name,"LBE%",4) == 0
933 || strncmp (cs->c_name,"LPBX%",5) == 0))
934 /* At least on a 3b1, gcc generates swbeg and string labels
935 that look like this. Ignore them. */
936 break;
937 /* fall in for static symbols that don't start with '.' */
938 case C_EXT:
939 if (cs->c_sclass == C_EXT &&
940 cs->c_secnum == N_ABS &&
941 strcmp (cs->c_name, _ETEXT) == 0)
942 end_of_text_addr = cs->c_value;
943 if (!SDB_TYPE (cs->c_type)) {
944 if (cs->c_secnum <= 1) { /* text or abs */
945 record_misc_function (cs->c_name, cs->c_value);
946 break;
947 } else {
948 cs->c_type = T_INT;
949 }
950 }
951 (void) process_coff_symbol (cs, &main_aux);
952 break;
953
954 case C_FCN:
955 if (strcmp (cs->c_name, ".bf") == 0)
956 {
957 within_function = 1;
958
959 /* value contains address of first non-init type code */
960 /* main_aux.x_sym.x_misc.x_lnsz.x_lnno
961 contains line number of '{' } */
962 fcn_first_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
963
964 new = (struct context_stack *)
965 xmalloc (sizeof (struct context_stack));
966 new->depth = depth = 0;
967 new->next = 0;
968 context_stack = new;
969 new->locals = 0;
970 new->old_blocks = pending_blocks;
971 new->start_addr = fcn_start_addr;
972 fcn_cs_saved.c_name = getsymname (&fcn_sym_saved);
973 new->name = process_coff_symbol (&fcn_cs_saved,
974 &fcn_aux_saved);
975 }
976 else if (strcmp (cs->c_name, ".ef") == 0)
977 {
978 /* the value of .ef is the address of epilogue code;
979 * not useful for gdb
980 */
981 /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
982 contains number of lines to '}' */
983 new = context_stack;
984 if (new == 0)
985 {
986 complain (&ef_complaint, cs->c_symnum);
987 within_function = 0;
988 break;
989 }
990 fcn_last_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
991 enter_linenos (fcn_line_ptr, fcn_first_line, fcn_last_line);
992
993 finish_block (new->name, &local_symbols, new->old_blocks,
994 new->start_addr,
995 #if defined (FUNCTION_EPILOGUE_SIZE)
996 /* This macro should be defined only on
997 machines where the
998 fcn_aux_saved.x_sym.x_misc.x_fsize
999 field is always zero.
1000 So use the .bf record information that
1001 points to the epilogue and add the size
1002 of the epilogue. */
1003 cs->c_value + FUNCTION_EPILOGUE_SIZE
1004 #else
1005 fcn_cs_saved.c_value +
1006 fcn_aux_saved.x_sym.x_misc.x_fsize
1007 #endif
1008 );
1009 context_stack = 0;
1010 within_function = 0;
1011 free (new);
1012 }
1013 break;
1014
1015 case C_BLOCK:
1016 if (strcmp (cs->c_name, ".bb") == 0)
1017 {
1018 new = (struct context_stack *)
1019 xmalloc (sizeof (struct context_stack));
1020 depth++;
1021 new->depth = depth;
1022 new->next = context_stack;
1023 context_stack = new;
1024 new->locals = local_symbols;
1025 new->old_blocks = pending_blocks;
1026 new->start_addr = cs->c_value;
1027 new->name = 0;
1028 local_symbols = 0;
1029 }
1030 else if (strcmp (cs->c_name, ".eb") == 0)
1031 {
1032 new = context_stack;
1033 if (new == 0 || depth != new->depth)
1034 error ("Invalid symbol data: .bb/.eb symbol mismatch at symbol %d.",
1035 symnum);
1036 if (local_symbols && context_stack->next)
1037 {
1038 /* Make a block for the local symbols within. */
1039 finish_block (0, &local_symbols, new->old_blocks,
1040 new->start_addr, cs->c_value);
1041 }
1042 depth--;
1043 local_symbols = new->locals;
1044 context_stack = new->next;
1045 free (new);
1046 }
1047 break;
1048 #ifdef TDESC
1049 case C_VERSION:
1050 if (strcmp (cs->c_name, ".coffsyn") == 0)
1051 last_coffsyn = cs->c_value;
1052 else if ((strcmp (cs->c_name, ".coffsem") == 0) &&
1053 (cs->c_value != 0))
1054 last_coffsem = cs->c_value;
1055 break;
1056 #endif
1057
1058 default:
1059 #ifdef TDESC
1060 if ((strcmp (cs->c_name, ".coffsem") == 0) &&
1061 (cs->c_value != 0))
1062 last_coffsem = cs->c_value;
1063 else
1064 #endif
1065 (void) process_coff_symbol (cs, &main_aux);
1066 break;
1067 }
1068 }
1069
1070 if (last_source_file)
1071 end_symtab ();
1072 fclose (stream);
1073 discard_cleanups (old_chain);
1074 }
1075 \f
1076 /* Routines for reading headers and symbols from executable. */
1077
1078 #ifdef FIXME
1079 /* Move these XXXMAGIC symbol defns into BFD! */
1080
1081 /* Read COFF file header, check magic number,
1082 and return number of symbols. */
1083 read_file_hdr (chan, file_hdr)
1084 int chan;
1085 FILHDR *file_hdr;
1086 {
1087 lseek (chan, 0L, 0);
1088 if (myread (chan, (char *)file_hdr, FILHSZ) < 0)
1089 return -1;
1090
1091 switch (file_hdr->f_magic)
1092 {
1093 #ifdef MC68MAGIC
1094 case MC68MAGIC:
1095 #endif
1096 #ifdef NS32GMAGIC
1097 case NS32GMAGIC:
1098 case NS32SMAGIC:
1099 #endif
1100 #ifdef I386MAGIC
1101 case I386MAGIC:
1102 #endif
1103 #ifdef CLIPPERMAGIC
1104 case CLIPPERMAGIC:
1105 #endif
1106 #if defined (MC68KWRMAGIC) \
1107 && (!defined (MC68MAGIC) || MC68KWRMAGIC != MC68MAGIC)
1108 case MC68KWRMAGIC:
1109 #endif
1110 #ifdef MC68KROMAGIC
1111 case MC68KROMAGIC:
1112 case MC68KPGMAGIC:
1113 #endif
1114 #ifdef MC88DGMAGIC
1115 case MC88DGMAGIC:
1116 #endif
1117 #ifdef MC88MAGIC
1118 case MC88MAGIC:
1119 #endif
1120 #ifdef I960ROMAGIC
1121 case I960ROMAGIC: /* Intel 960 */
1122 #endif
1123 #ifdef I960RWMAGIC
1124 case I960RWMAGIC: /* Intel 960 */
1125 #endif
1126 return file_hdr->f_nsyms;
1127
1128 default:
1129 #ifdef BADMAG
1130 if (BADMAG(file_hdr))
1131 return -1;
1132 else
1133 return file_hdr->f_nsyms;
1134 #else
1135 return -1;
1136 #endif
1137 }
1138 }
1139 #endif
1140
1141
1142 static void
1143 read_one_sym (cs, sym, aux)
1144 register struct coff_symbol *cs;
1145 register SYMENT *sym;
1146 register AUXENT *aux;
1147 {
1148 AUXENT temp_aux;
1149 int i;
1150
1151 cs->c_symnum = symnum;
1152 fread ((char *)sym, SYMESZ, 1, nlist_stream_global);
1153 bfd_coff_swap_sym (symfile_bfd, sym);
1154 cs->c_nsyms = (sym->n_numaux & 0xff) + 1;
1155 if (cs->c_nsyms >= 2)
1156 {
1157 fread ((char *)aux, AUXESZ, 1, nlist_stream_global);
1158 bfd_coff_swap_aux (symfile_bfd, aux, sym->n_type, sym->n_sclass);
1159 /* If more than one aux entry, read past it (only the first aux
1160 is important). */
1161 for (i = 2; i < cs->c_nsyms; i++)
1162 fread ((char *)&temp_aux, AUXESZ, 1, nlist_stream_global);
1163 }
1164 cs->c_name = getsymname (sym);
1165 cs->c_value = sym->n_value;
1166 cs->c_sclass = (sym->n_sclass & 0xff);
1167 cs->c_secnum = sym->n_scnum;
1168 cs->c_type = (unsigned) sym->n_type;
1169 if (!SDB_TYPE (cs->c_type))
1170 cs->c_type = 0;
1171
1172 symnum += cs->c_nsyms;
1173 }
1174 \f
1175 /* Support for string table handling */
1176
1177 static char *stringtab = NULL;
1178
1179 static int
1180 init_stringtab (chan, offset)
1181 int chan;
1182 long offset;
1183 {
1184 long length;
1185 int val;
1186 unsigned char lengthbuf[4];
1187
1188 if (stringtab)
1189 {
1190 free (stringtab);
1191 stringtab = NULL;
1192 }
1193
1194 if (lseek (chan, offset, 0) < 0)
1195 return -1;
1196
1197 val = myread (chan, (char *)lengthbuf, sizeof lengthbuf);
1198 length = bfd_h_getlong (symfile_bfd, lengthbuf);
1199
1200 /* If no string table is needed, then the file may end immediately
1201 after the symbols. Just return with `stringtab' set to null. */
1202 if (val != sizeof length || length < sizeof length)
1203 return 0;
1204
1205 stringtab = (char *) xmalloc (length);
1206 if (stringtab == NULL)
1207 return -1;
1208
1209 bcopy (&length, stringtab, sizeof length);
1210 if (length == sizeof length) /* Empty table -- just the count */
1211 return 0;
1212
1213 val = myread (chan, stringtab + sizeof length, length - sizeof length);
1214 if (val != length - sizeof length || stringtab[length - 1] != '\0')
1215 return -1;
1216
1217 return 0;
1218 }
1219
1220 static void
1221 free_stringtab ()
1222 {
1223 if (stringtab)
1224 free (stringtab);
1225 stringtab = NULL;
1226 }
1227
1228 static char *
1229 getsymname (symbol_entry)
1230 SYMENT *symbol_entry;
1231 {
1232 static char buffer[SYMNMLEN+1];
1233 char *result;
1234
1235 if (symbol_entry->n_zeroes == 0)
1236 {
1237 result = stringtab + symbol_entry->n_offset;
1238 }
1239 else
1240 {
1241 strncpy (buffer, symbol_entry->n_name, SYMNMLEN);
1242 buffer[SYMNMLEN] = '\0';
1243 result = buffer;
1244 }
1245 return result;
1246 }
1247
1248 static char *
1249 getfilename (aux_entry)
1250 AUXENT *aux_entry;
1251 {
1252 static char buffer[BUFSIZ];
1253 register char *temp;
1254 char *result;
1255 extern char *rindex ();
1256
1257 #ifndef COFF_NO_LONG_FILE_NAMES
1258 if (aux_entry->x_file.x_n.x_zeroes == 0)
1259 strcpy (buffer, stringtab + aux_entry->x_file.x_n.x_offset);
1260 else
1261 #endif /* COFF_NO_LONG_FILE_NAMES */
1262 {
1263 #if defined (x_name)
1264 /* Data General. */
1265 strncpy (buffer, aux_entry->x_name, FILNMLEN);
1266 #else
1267 strncpy (buffer, aux_entry->x_file.x_fname, FILNMLEN);
1268 #endif
1269 buffer[FILNMLEN] = '\0';
1270 }
1271 result = buffer;
1272 if ((temp = rindex (result, '/')) != NULL)
1273 result = temp + 1;
1274 return (result);
1275 }
1276 \f
1277 /* Support for line number handling */
1278 static char *linetab = NULL;
1279 static long linetab_offset;
1280 static unsigned long linetab_size;
1281
1282 /* Read in all the line numbers for fast lookups later. */
1283
1284 static int
1285 init_lineno (chan, offset, size)
1286 int chan;
1287 long offset;
1288 int size;
1289 {
1290 int val;
1291 register char *p, *q;
1292
1293 if (lseek (chan, offset, 0) < 0)
1294 return -1;
1295
1296 linetab = (char *) xmalloc (size);
1297
1298 val = myread (chan, linetab, size);
1299 if (val != size)
1300 return -1;
1301
1302 /* Swap all entries */
1303 q = linetab + size;
1304 for (p = linetab; p < q; p += LINESZ)
1305 bfd_coff_swap_lineno (symfile_bfd, (LINENO *)p);
1306
1307 linetab_offset = offset;
1308 linetab_size = size;
1309 make_cleanup (free, linetab); /* Be sure it gets de-allocated. */
1310 return 0;
1311 }
1312
1313 #if !defined (L_LNNO32)
1314 #define L_LNNO32(lp) ((lp)->l_lnno)
1315 #endif
1316
1317 static void
1318 enter_linenos (file_offset, first_line, last_line)
1319 long file_offset;
1320 register int first_line;
1321 register int last_line;
1322 {
1323 register char *rawptr;
1324 struct lineno lptr;
1325
1326 if (file_offset < linetab_offset)
1327 {
1328 complain (lineno_complaint, file_offset);
1329 if (file_offset > linetab_size) /* Too big to be an offset? */
1330 return;
1331 file_offset += linetab_offset; /* Try reading at that linetab offset */
1332 }
1333
1334 rawptr = &linetab[file_offset - linetab_offset];
1335
1336 /* skip first line entry for each function */
1337 rawptr += LINESZ;
1338 /* line numbers start at one for the first line of the function */
1339 first_line--;
1340
1341 /* Bcopy since occaisionally rawptr isn't pointing at long
1342 boundaries. */
1343 for (bcopy (rawptr, &lptr, LINESZ);
1344 L_LNNO32 (&lptr) && L_LNNO32 (&lptr) <= last_line;
1345 rawptr += LINESZ, bcopy (rawptr, &lptr, LINESZ))
1346 {
1347 record_line (first_line + L_LNNO32 (&lptr), lptr.l_addr.l_paddr);
1348 }
1349 }
1350 \f
1351 static int
1352 hashname (name)
1353 char *name;
1354 {
1355 register char *p = name;
1356 register int total = p[0];
1357 register int c;
1358
1359 c = p[1];
1360 total += c << 2;
1361 if (c)
1362 {
1363 c = p[2];
1364 total += c << 4;
1365 if (c)
1366 total += p[3] << 6;
1367 }
1368
1369 return total % HASHSIZE;
1370 }
1371
1372 static void
1373 patch_type (type, real_type)
1374 struct type *type;
1375 struct type *real_type;
1376 {
1377 register struct type *target = TYPE_TARGET_TYPE (type);
1378 register struct type *real_target = TYPE_TARGET_TYPE (real_type);
1379 int field_size = TYPE_NFIELDS (real_target) * sizeof (struct field);
1380
1381 TYPE_LENGTH (target) = TYPE_LENGTH (real_target);
1382 TYPE_NFIELDS (target) = TYPE_NFIELDS (real_target);
1383 TYPE_FIELDS (target) = (struct field *)
1384 obstack_alloc (symbol_obstack, field_size);
1385
1386 bcopy (TYPE_FIELDS (real_target), TYPE_FIELDS (target), field_size);
1387
1388 if (TYPE_NAME (real_target))
1389 {
1390 if (TYPE_NAME (target))
1391 free (TYPE_NAME (target));
1392 TYPE_NAME (target) = concat (TYPE_NAME (real_target), "", "");
1393 }
1394 }
1395
1396 /* Patch up all appropriate typdef symbols in the opaque_type_chains
1397 so that they can be used to print out opaque data structures properly */
1398
1399 static void
1400 patch_opaque_types ()
1401 {
1402 struct symtab *s;
1403
1404 /* Look at each symbol in the per-file block of each symtab. */
1405 for (s = symtab_list; s; s = s->next)
1406 {
1407 register struct block *b;
1408 register int i;
1409
1410 /* Go through the per-file symbols only */
1411 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
1412 for (i = BLOCK_NSYMS (b) - 1; i >= 0; i--)
1413 {
1414 register struct symbol *real_sym;
1415
1416 /* Find completed typedefs to use to fix opaque ones.
1417 Remove syms from the chain when their types are stored,
1418 but search the whole chain, as there may be several syms
1419 from different files with the same name. */
1420 real_sym = BLOCK_SYM (b, i);
1421 if (SYMBOL_CLASS (real_sym) == LOC_TYPEDEF &&
1422 SYMBOL_NAMESPACE (real_sym) == VAR_NAMESPACE &&
1423 TYPE_CODE (SYMBOL_TYPE (real_sym)) == TYPE_CODE_PTR &&
1424 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (real_sym))) != 0)
1425 {
1426 register char *name = SYMBOL_NAME (real_sym);
1427 register int hash = hashname (name);
1428 register struct symbol *sym, *prev;
1429
1430 prev = 0;
1431 for (sym = opaque_type_chain[hash]; sym;)
1432 {
1433 if (name[0] == SYMBOL_NAME (sym)[0] &&
1434 !strcmp (name + 1, SYMBOL_NAME (sym) + 1))
1435 {
1436 if (prev)
1437 SYMBOL_VALUE_CHAIN (prev) = SYMBOL_VALUE_CHAIN (sym);
1438 else
1439 opaque_type_chain[hash] = SYMBOL_VALUE_CHAIN (sym);
1440
1441 patch_type (SYMBOL_TYPE (sym), SYMBOL_TYPE (real_sym));
1442
1443 if (prev)
1444 sym = SYMBOL_VALUE_CHAIN (prev);
1445 else
1446 sym = opaque_type_chain[hash];
1447 }
1448 else
1449 {
1450 prev = sym;
1451 sym = SYMBOL_VALUE_CHAIN (sym);
1452 }
1453 }
1454 }
1455 }
1456 }
1457 }
1458 \f
1459 #if defined (clipper)
1460 #define BELIEVE_PCC_PROMOTION 1
1461 #endif
1462
1463 static struct symbol *
1464 process_coff_symbol (cs, aux)
1465 register struct coff_symbol *cs;
1466 register AUXENT *aux;
1467 {
1468 register struct symbol *sym
1469 = (struct symbol *) obstack_alloc (symbol_obstack, sizeof (struct symbol));
1470 char *name;
1471 #ifdef NAMES_HAVE_UNDERSCORE
1472 int offset = 1;
1473 #else
1474 int offset = 0;
1475 #endif
1476
1477 bzero (sym, sizeof (struct symbol));
1478 name = cs->c_name;
1479 name = (name[0] == '_' ? name + offset : name);
1480 SYMBOL_NAME (sym) = obstack_copy0 (symbol_obstack, name, strlen (name));
1481
1482 /* default assumptions */
1483 SYMBOL_VALUE (sym) = cs->c_value;
1484 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1485
1486 if (ISFCN (cs->c_type))
1487 {
1488 SYMBOL_TYPE (sym) =
1489 lookup_function_type (decode_function_type (cs, cs->c_type, aux));
1490 SYMBOL_CLASS (sym) = LOC_BLOCK;
1491 if (cs->c_sclass == C_STAT)
1492 add_symbol_to_list (sym, &file_symbols);
1493 else if (cs->c_sclass == C_EXT)
1494 add_symbol_to_list (sym, &global_symbols);
1495 }
1496 else
1497 {
1498 SYMBOL_TYPE (sym) = decode_type (cs, cs->c_type, aux);
1499 switch (cs->c_sclass)
1500 {
1501 case C_NULL:
1502 break;
1503
1504 case C_AUTO:
1505 SYMBOL_CLASS (sym) = LOC_LOCAL;
1506 add_symbol_to_list (sym, &local_symbols);
1507 break;
1508
1509 case C_EXT:
1510 SYMBOL_CLASS (sym) = LOC_STATIC;
1511 SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
1512 add_symbol_to_list (sym, &global_symbols);
1513 break;
1514
1515 case C_STAT:
1516 SYMBOL_CLASS (sym) = LOC_STATIC;
1517 SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
1518 if (within_function) {
1519 /* Static symbol of local scope */
1520 add_symbol_to_list (sym, &local_symbols);
1521 }
1522 else {
1523 /* Static symbol at top level of file */
1524 add_symbol_to_list (sym, &file_symbols);
1525 }
1526 break;
1527
1528 case C_REG:
1529 SYMBOL_CLASS (sym) = LOC_REGISTER;
1530 add_symbol_to_list (sym, &local_symbols);
1531 break;
1532
1533 case C_LABEL:
1534 break;
1535
1536 case C_ARG:
1537 SYMBOL_CLASS (sym) = LOC_ARG;
1538 add_symbol_to_list (sym, &local_symbols);
1539 #if !defined (BELIEVE_PCC_PROMOTION)
1540 /* If PCC says a parameter is a short or a char,
1541 it is really an int. */
1542 if (SYMBOL_TYPE (sym) == builtin_type_char
1543 || SYMBOL_TYPE (sym) == builtin_type_short)
1544 SYMBOL_TYPE (sym) = builtin_type_int;
1545 else if (SYMBOL_TYPE (sym) == builtin_type_unsigned_char
1546 || SYMBOL_TYPE (sym) == builtin_type_unsigned_short)
1547 SYMBOL_TYPE (sym) = builtin_type_unsigned_int;
1548 #endif
1549 break;
1550
1551 case C_REGPARM:
1552 SYMBOL_CLASS (sym) = LOC_REGPARM;
1553 add_symbol_to_list (sym, &local_symbols);
1554 #if !defined (BELIEVE_PCC_PROMOTION)
1555 /* If PCC says a parameter is a short or a char,
1556 it is really an int. */
1557 if (SYMBOL_TYPE (sym) == builtin_type_char
1558 || SYMBOL_TYPE (sym) == builtin_type_short)
1559 SYMBOL_TYPE (sym) = builtin_type_int;
1560 else if (SYMBOL_TYPE (sym) == builtin_type_unsigned_char
1561 || SYMBOL_TYPE (sym) == builtin_type_unsigned_short)
1562 SYMBOL_TYPE (sym) = builtin_type_unsigned_int;
1563 #endif
1564 break;
1565
1566 case C_TPDEF:
1567 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
1568 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1569
1570 /* If type has no name, give it one */
1571 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0
1572 && (TYPE_FLAGS (SYMBOL_TYPE (sym)) & TYPE_FLAG_PERM) == 0)
1573 TYPE_NAME (SYMBOL_TYPE (sym))
1574 = concat (SYMBOL_NAME (sym), "", "");
1575
1576 /* Keep track of any type which points to empty structured type,
1577 so it can be filled from a definition from another file */
1578 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_PTR &&
1579 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym))) == 0)
1580 {
1581 register int i = hashname (SYMBOL_NAME (sym));
1582
1583 SYMBOL_VALUE_CHAIN (sym) = opaque_type_chain[i];
1584 opaque_type_chain[i] = sym;
1585 }
1586 add_symbol_to_list (sym, &file_symbols);
1587 break;
1588
1589 case C_STRTAG:
1590 case C_UNTAG:
1591 case C_ENTAG:
1592 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
1593 SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
1594 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0
1595 && (TYPE_FLAGS (SYMBOL_TYPE (sym)) & TYPE_FLAG_PERM) == 0)
1596 TYPE_NAME (SYMBOL_TYPE (sym))
1597 = concat ("",
1598 (cs->c_sclass == C_ENTAG
1599 ? "enum "
1600 : (cs->c_sclass == C_STRTAG
1601 ? "struct " : "union ")),
1602 SYMBOL_NAME (sym));
1603 add_symbol_to_list (sym, &file_symbols);
1604 break;
1605
1606 default:
1607 break;
1608 }
1609 }
1610 return sym;
1611 }
1612 \f
1613 /* Decode a coff type specifier;
1614 return the type that is meant. */
1615
1616 static
1617 struct type *
1618 decode_type (cs, c_type, aux)
1619 register struct coff_symbol *cs;
1620 unsigned int c_type;
1621 register AUXENT *aux;
1622 {
1623 register struct type *type = 0;
1624 unsigned int new_c_type;
1625
1626 if (c_type & ~N_BTMASK)
1627 {
1628 new_c_type = DECREF (c_type);
1629 if (ISPTR (c_type))
1630 {
1631 type = decode_type (cs, new_c_type, aux);
1632 type = lookup_pointer_type (type);
1633 }
1634 else if (ISFCN (c_type))
1635 {
1636 type = decode_type (cs, new_c_type, aux);
1637 type = lookup_function_type (type);
1638 }
1639 else if (ISARY (c_type))
1640 {
1641 int i, n;
1642 register unsigned short *dim;
1643 struct type *base_type;
1644
1645 /* Define an array type. */
1646 /* auxent refers to array, not base type */
1647 if (aux->x_sym.x_tagndx == 0)
1648 cs->c_nsyms = 1;
1649
1650 /* shift the indices down */
1651 dim = &aux->x_sym.x_fcnary.x_ary.x_dimen[0];
1652 i = 1;
1653 n = dim[0];
1654 for (i = 0; *dim && i < DIMNUM - 1; i++, dim++)
1655 *dim = *(dim + 1);
1656 *dim = 0;
1657
1658 type = (struct type *)
1659 obstack_alloc (symbol_obstack, sizeof (struct type));
1660 bzero (type, sizeof (struct type));
1661
1662 base_type = decode_type (cs, new_c_type, aux);
1663
1664 TYPE_CODE (type) = TYPE_CODE_ARRAY;
1665 TYPE_TARGET_TYPE (type) = base_type;
1666 TYPE_LENGTH (type) = n * TYPE_LENGTH (base_type);
1667 }
1668 return type;
1669 }
1670
1671 /* Reference to existing type */
1672 if (cs->c_nsyms > 1 && aux->x_sym.x_tagndx != 0)
1673 {
1674 type = coff_alloc_type (aux->x_sym.x_tagndx);
1675 return type;
1676 }
1677
1678 return decode_base_type (cs, BTYPE (c_type), aux);
1679 }
1680
1681 /* Decode a coff type specifier for function definition;
1682 return the type that the function returns. */
1683
1684 static
1685 struct type *
1686 decode_function_type (cs, c_type, aux)
1687 register struct coff_symbol *cs;
1688 unsigned int c_type;
1689 register AUXENT *aux;
1690 {
1691 if (aux->x_sym.x_tagndx == 0)
1692 cs->c_nsyms = 1; /* auxent refers to function, not base type */
1693
1694 return decode_type (cs, DECREF (c_type), aux);
1695 }
1696 \f
1697 /* basic C types */
1698
1699 static
1700 struct type *
1701 decode_base_type (cs, c_type, aux)
1702 register struct coff_symbol *cs;
1703 unsigned int c_type;
1704 register AUXENT *aux;
1705 {
1706 struct type *type;
1707
1708 switch (c_type)
1709 {
1710 case T_NULL:
1711 /* shows up with "void (*foo)();" structure members */
1712 return builtin_type_void;
1713
1714 #ifdef T_ARG
1715 case T_ARG:
1716 /* Shows up in DGUX, I think. Not sure where. */
1717 return builtin_type_void; /* shouldn't show up here */
1718 #endif
1719
1720 #ifdef T_VOID
1721 case T_VOID:
1722 /* Intel 960 COFF has this symbol and meaning. */
1723 return builtin_type_void;
1724 #endif
1725
1726 case T_CHAR:
1727 return builtin_type_char;
1728
1729 case T_SHORT:
1730 return builtin_type_short;
1731
1732 case T_INT:
1733 return builtin_type_int;
1734
1735 case T_LONG:
1736 return builtin_type_long;
1737
1738 case T_FLOAT:
1739 return builtin_type_float;
1740
1741 case T_DOUBLE:
1742 return builtin_type_double;
1743
1744 case T_STRUCT:
1745 if (cs->c_nsyms != 2)
1746 {
1747 /* anonymous structure type */
1748 type = coff_alloc_type (cs->c_symnum);
1749 TYPE_CODE (type) = TYPE_CODE_STRUCT;
1750 TYPE_NAME (type) = concat ("struct ", "<opaque>", "");
1751 TYPE_LENGTH (type) = 0;
1752 TYPE_FIELDS (type) = 0;
1753 TYPE_NFIELDS (type) = 0;
1754 }
1755 else
1756 {
1757 type = read_struct_type (cs->c_symnum,
1758 aux->x_sym.x_misc.x_lnsz.x_size,
1759 aux->x_sym.x_fcnary.x_fcn.x_endndx);
1760 }
1761 return type;
1762
1763 case T_UNION:
1764 if (cs->c_nsyms != 2)
1765 {
1766 /* anonymous union type */
1767 type = coff_alloc_type (cs->c_symnum);
1768 TYPE_NAME (type) = concat ("union ", "<opaque>", "");
1769 TYPE_LENGTH (type) = 0;
1770 TYPE_FIELDS (type) = 0;
1771 TYPE_NFIELDS (type) = 0;
1772 }
1773 else
1774 {
1775 type = read_struct_type (cs->c_symnum,
1776 aux->x_sym.x_misc.x_lnsz.x_size,
1777 aux->x_sym.x_fcnary.x_fcn.x_endndx);
1778 }
1779 TYPE_CODE (type) = TYPE_CODE_UNION;
1780 return type;
1781
1782 case T_ENUM:
1783 return read_enum_type (cs->c_symnum,
1784 aux->x_sym.x_misc.x_lnsz.x_size,
1785 aux->x_sym.x_fcnary.x_fcn.x_endndx);
1786
1787 case T_MOE:
1788 /* shouldn't show up here */
1789 break;
1790
1791 case T_UCHAR:
1792 return builtin_type_unsigned_char;
1793
1794 case T_USHORT:
1795 return builtin_type_unsigned_short;
1796
1797 case T_UINT:
1798 return builtin_type_unsigned_int;
1799
1800 case T_ULONG:
1801 return builtin_type_unsigned_long;
1802 }
1803 printf ("unexpected type %d at symnum %d\n", c_type, cs->c_symnum);
1804 return builtin_type_void;
1805 }
1806 \f
1807 /* This page contains subroutines of read_type. */
1808
1809 /* Read the description of a structure (or union type)
1810 and return an object describing the type. */
1811
1812 static struct type *
1813 read_struct_type (index, length, lastsym)
1814 int index;
1815 int length;
1816 int lastsym;
1817 {
1818 struct nextfield
1819 {
1820 struct nextfield *next;
1821 struct field field;
1822 };
1823
1824 register struct type *type;
1825 register struct nextfield *list = 0;
1826 struct nextfield *new;
1827 int nfields = 0;
1828 register int n;
1829 char *name;
1830 #ifdef NAMES_HAVE_UNDERSCORE
1831 int offset = 1;
1832 #else
1833 int offset = 0;
1834 #endif
1835 struct coff_symbol member_sym;
1836 register struct coff_symbol *ms = &member_sym;
1837 SYMENT sub_sym;
1838 AUXENT sub_aux;
1839 int done = 0;
1840
1841 type = coff_alloc_type (index);
1842 TYPE_CODE (type) = TYPE_CODE_STRUCT;
1843 TYPE_LENGTH (type) = length;
1844
1845 while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
1846 {
1847 read_one_sym (ms, &sub_sym, &sub_aux);
1848 name = ms->c_name;
1849 name = (name[0] == '_' ? name + offset : name);
1850
1851 switch (ms->c_sclass)
1852 {
1853 case C_MOS:
1854 case C_MOU:
1855
1856 /* Get space to record the next field's data. */
1857 new = (struct nextfield *) alloca (sizeof (struct nextfield));
1858 new->next = list;
1859 list = new;
1860
1861 /* Save the data. */
1862 list->field.name = savestring (name, strlen (name));
1863 list->field.type = decode_type (ms, ms->c_type, &sub_aux);
1864 list->field.bitpos = 8 * ms->c_value;
1865 list->field.bitsize = 0;
1866 nfields++;
1867 break;
1868
1869 case C_FIELD:
1870
1871 /* Get space to record the next field's data. */
1872 new = (struct nextfield *) alloca (sizeof (struct nextfield));
1873 new->next = list;
1874 list = new;
1875
1876 /* Save the data. */
1877 list->field.name = savestring (name, strlen (name));
1878 list->field.type = decode_type (ms, ms->c_type, &sub_aux);
1879 list->field.bitpos = ms->c_value;
1880 list->field.bitsize = sub_aux.x_sym.x_misc.x_lnsz.x_size;
1881 nfields++;
1882 break;
1883
1884 case C_EOS:
1885 done = 1;
1886 break;
1887 }
1888 }
1889 /* Now create the vector of fields, and record how big it is. */
1890
1891 TYPE_NFIELDS (type) = nfields;
1892 TYPE_FIELDS (type) = (struct field *)
1893 obstack_alloc (symbol_obstack, sizeof (struct field) * nfields);
1894
1895 /* Copy the saved-up fields into the field vector. */
1896
1897 for (n = nfields; list; list = list->next)
1898 TYPE_FIELD (type, --n) = list->field;
1899
1900 return type;
1901 }
1902 \f
1903 /* Read a definition of an enumeration type,
1904 and create and return a suitable type object.
1905 Also defines the symbols that represent the values of the type. */
1906 /* Currently assumes it's sizeof (int) and doesn't use length. */
1907
1908 static struct type *
1909 read_enum_type (index, length, lastsym)
1910 int index;
1911 int length;
1912 int lastsym;
1913 {
1914 register struct symbol *sym;
1915 register struct type *type;
1916 int nsyms = 0;
1917 int done = 0;
1918 struct pending **symlist;
1919 struct coff_symbol member_sym;
1920 register struct coff_symbol *ms = &member_sym;
1921 SYMENT sub_sym;
1922 AUXENT sub_aux;
1923 struct pending *osyms, *syms;
1924 register int n;
1925 char *name;
1926 #ifdef NAMES_HAVE_UNDERSCORE
1927 int offset = 1;
1928 #else
1929 int offset = 0;
1930 #endif
1931
1932 type = coff_alloc_type (index);
1933 if (within_function)
1934 symlist = &local_symbols;
1935 else
1936 symlist = &file_symbols;
1937 osyms = *symlist;
1938
1939 while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
1940 {
1941 read_one_sym (ms, &sub_sym, &sub_aux);
1942 name = ms->c_name;
1943 name = (name[0] == '_' ? name + offset : name);
1944
1945 switch (ms->c_sclass)
1946 {
1947 case C_MOE:
1948 sym = (struct symbol *) xmalloc (sizeof (struct symbol));
1949 bzero (sym, sizeof (struct symbol));
1950
1951 SYMBOL_NAME (sym) = savestring (name, strlen (name));
1952 SYMBOL_CLASS (sym) = LOC_CONST;
1953 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1954 SYMBOL_VALUE (sym) = ms->c_value;
1955 add_symbol_to_list (sym, symlist);
1956 nsyms++;
1957 break;
1958
1959 case C_EOS:
1960 /* Sometimes the linker (on 386/ix 2.0.2 at least) screws
1961 up the count of how many symbols to read. So stop
1962 on .eos. */
1963 done = 1;
1964 break;
1965 }
1966 }
1967
1968 /* Now fill in the fields of the type-structure. */
1969
1970 /* FIXME: Should be sizeof (int) on target, not host. */
1971 TYPE_LENGTH (type) = sizeof (int);
1972 TYPE_CODE (type) = TYPE_CODE_ENUM;
1973 TYPE_NFIELDS (type) = nsyms;
1974 TYPE_FIELDS (type) = (struct field *)
1975 obstack_alloc (symbol_obstack, sizeof (struct field) * nsyms);
1976
1977 /* Find the symbols for the values and put them into the type.
1978 The symbols can be found in the symlist that we put them on
1979 to cause them to be defined. osyms contains the old value
1980 of that symlist; everything up to there was defined by us. */
1981
1982 for (syms = *symlist, n = nsyms; syms != osyms; syms = syms->next)
1983 {
1984 SYMBOL_TYPE (syms->symbol) = type;
1985 TYPE_FIELD_NAME (type, --n) = SYMBOL_NAME (syms->symbol);
1986 TYPE_FIELD_VALUE (type, n) = 0;
1987 TYPE_FIELD_BITPOS (type, n) = SYMBOL_VALUE (syms->symbol);
1988 TYPE_FIELD_BITSIZE (type, n) = 0;
1989 }
1990 return type;
1991 }
1992
1993 /* Register our ability to parse symbols for coff BFD files */
1994
1995 static struct sym_fns coff_sym_fns =
1996 {
1997 "coff", 4,
1998 coff_new_init, coff_symfile_init,
1999 coff_symfile_read, coff_symfile_discard
2000 };
2001
2002 void
2003 _initialize_coffread ()
2004 {
2005 add_symtab_fns(&coff_sym_fns);
2006 }
This page took 0.067557 seconds and 5 git commands to generate.