gdb-2.4+.aux.coff
[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 Free Software Foundation, Inc.
6
7 GDB is distributed in the hope that it will be useful, but WITHOUT ANY
8 WARRANTY. No author or distributor accepts responsibility to anyone
9 for the consequences of using it or for whether it serves any
10 particular purpose or works at all, unless he says so in writing.
11 Refer to the GDB General Public License for full details.
12
13 Everyone is granted permission to copy, modify and redistribute GDB,
14 but only under the conditions described in the GDB General Public
15 License. A copy of this license is supposed to have been given to you
16 along with GDB so you can know your rights and responsibilities. It
17 should be in a file named COPYING. Among other things, the copyright
18 notice and this notice must be preserved on all copies.
19
20 In other words, go ahead and share GDB, but don't try to stop
21 anyone else from sharing it farther. Help stamp out software hoarding!
22 */
23 \f
24 #include "defs.h"
25 #include "param.h"
26 #ifdef COFF_FORMAT
27 #include "initialize.h"
28 #include "symtab.h"
29
30 #include <a.out.h>
31 #include <stdio.h>
32 #include <obstack.h>
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/file.h>
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
55 START_FILE
56
57 /* Name of source file whose symbol data we are now processing.
58 This comes from a symbol named ".file". */
59
60 static char *last_source_file;
61
62 /* Core address of start and end of text of current source file.
63 This comes from a ".text" symbol where x_nlinno > 0. */
64
65 static CORE_ADDR cur_src_start_addr;
66 static CORE_ADDR cur_src_end_addr;
67
68 /* End of the text segment of the executable file,
69 as found in the symbol _etext. */
70
71 static CORE_ADDR end_of_text_addr;
72
73 /* The addresses of the symbol table stream and number of symbols
74 of the object file we are reading (as copied into core). */
75
76 static FILE *nlist_stream_global;
77 static int nlist_nsyms_global;
78
79 /* The file and text section headers of the symbol file */
80
81 static FILHDR file_hdr;
82 static SCNHDR text_hdr;
83
84 /* The index in the symbol table of the last coff symbol that was processed. */
85
86 static int symnum;
87
88 /* Vector of types defined so far, indexed by their coff symnum. */
89
90 static struct typevector *type_vector;
91
92 /* Number of elements allocated for type_vector currently. */
93
94 static int type_vector_length;
95
96 /* Vector of line number information. */
97
98 static struct linetable *line_vector;
99
100 /* Index of next entry to go in line_vector_index. */
101
102 static int line_vector_index;
103
104 /* Last line number recorded in the line vector. */
105
106 static int prev_line_number;
107
108 /* Number of elements allocated for line_vector currently. */
109
110 static int line_vector_length;
111
112 /* Chain of typedefs of pointers to empty struct/union types.
113 They are chained thru the SYMBOL_VALUE. */
114
115 #define HASHSIZE 127
116 static struct symbol *opaque_type_chain[HASHSIZE];
117
118 /* Record the symbols defined for each context in a list.
119 We don't create a struct block for the context until we
120 know how long to make it. */
121
122 struct pending
123 {
124 struct pending *next;
125 struct symbol *symbol;
126 };
127
128 /* Here are the three lists that symbols are put on. */
129
130 struct pending *file_symbols; /* static at top level, and types */
131
132 struct pending *global_symbols; /* global functions and variables */
133
134 struct pending *local_symbols; /* everything local to lexical context */
135
136 /* List of unclosed lexical contexts
137 (that will become blocks, eventually). */
138
139 struct context_stack
140 {
141 struct context_stack *next;
142 struct pending *locals;
143 struct pending_block *old_blocks;
144 struct symbol *name;
145 CORE_ADDR start_addr;
146 int depth;
147 };
148
149 struct context_stack *context_stack;
150
151 /* Nonzero if within a function (so symbols should be local,
152 if nothing says specifically). */
153
154 int within_function;
155
156 /* List of blocks already made (lexical contexts already closed).
157 This is used at the end to make the blockvector. */
158
159 struct pending_block
160 {
161 struct pending_block *next;
162 struct block *block;
163 };
164
165 struct pending_block *pending_blocks;
166
167 extern CORE_ADDR first_object_file_end; /* From blockframe.c */
168
169 /* File name symbols were loaded from. */
170
171 static char *symfile;
172
173 int debug = 1;
174
175 \f
176 /* Look up a coff type-number index. Return the address of the slot
177 where the type for that index is stored.
178 The type-number is in INDEX.
179
180 This can be used for finding the type associated with that index
181 or for associating a new type with the index. */
182
183 static struct type **
184 coff_lookup_type (index)
185 register int index;
186 {
187 if (index >= type_vector_length)
188 {
189 type_vector_length *= 2;
190 type_vector = (struct typevector *)
191 xrealloc (type_vector, sizeof (struct typevector)
192 + type_vector_length * sizeof (struct type *));
193 bzero (&type_vector->type[type_vector_length / 2],
194 type_vector_length * sizeof (struct type *) / 2);
195 }
196 return &type_vector->type[index];
197 }
198
199 /* Make sure there is a type allocated for type number index
200 and return the type object.
201 This can create an empty (zeroed) type object. */
202
203 static struct type *
204 coff_alloc_type (index)
205 int index;
206 {
207 register struct type **type_addr = coff_lookup_type (index);
208 register struct type *type = *type_addr;
209
210 /* If we are referring to a type not known at all yet,
211 allocate an empty type for it.
212 We will fill it in later if we find out how. */
213 if (type == 0)
214 {
215 type = (struct type *) obstack_alloc (symbol_obstack,
216 sizeof (struct type));
217 bzero (type, sizeof (struct type));
218 *type_addr = type;
219 }
220 return type;
221 }
222 \f
223 /* maintain the lists of symbols and blocks */
224
225 /* Add a symbol to one of the lists of symbols. */
226 static void
227 add_symbol_to_list (symbol, listhead)
228 struct symbol *symbol;
229 struct pending **listhead;
230 {
231 register struct pending *link
232 = (struct pending *) xmalloc (sizeof (struct pending));
233
234 link->next = *listhead;
235 link->symbol = symbol;
236 *listhead = link;
237 }
238
239 /* Take one of the lists of symbols and make a block from it.
240 Put the block on the list of pending blocks. */
241
242 static void
243 finish_block (symbol, listhead, old_blocks, start, end)
244 struct symbol *symbol;
245 struct pending **listhead;
246 struct pending_block *old_blocks;
247 CORE_ADDR start, end;
248 {
249 register struct pending *next, *next1;
250 register struct block *block;
251 register struct pending_block *pblock;
252 struct pending_block *opblock;
253 register int i;
254
255 /* Count the length of the list of symbols. */
256
257 for (next = *listhead, i = 0; next; next = next->next, i++);
258
259 block = (struct block *) xmalloc (sizeof (struct block) + (i - 1) * sizeof (struct symbol *));
260
261 /* Copy the symbols into the block. */
262
263 BLOCK_NSYMS (block) = i;
264 for (next = *listhead; next; next = next->next)
265 BLOCK_SYM (block, --i) = next->symbol;
266
267 BLOCK_START (block) = start;
268 BLOCK_END (block) = end;
269 BLOCK_SUPERBLOCK (block) = 0; /* Filled in when containing block is made */
270
271 /* Put the block in as the value of the symbol that names it. */
272
273 if (symbol)
274 {
275 SYMBOL_BLOCK_VALUE (symbol) = block;
276 BLOCK_FUNCTION (block) = symbol;
277 }
278 else
279 BLOCK_FUNCTION (block) = 0;
280
281 /* Now free the links of the list, and empty the list. */
282
283 for (next = *listhead; next; next = next1)
284 {
285 next1 = next->next;
286 free (next);
287 }
288 *listhead = 0;
289
290 /* Install this block as the superblock
291 of all blocks made since the start of this scope
292 that don't have superblocks yet. */
293
294 opblock = 0;
295 for (pblock = pending_blocks; pblock != old_blocks; pblock = pblock->next)
296 {
297 if (BLOCK_SUPERBLOCK (pblock->block) == 0)
298 BLOCK_SUPERBLOCK (pblock->block) = block;
299 opblock = pblock;
300 }
301
302 /* Record this block on the list of all blocks in the file.
303 Put it after opblock, or at the beginning if opblock is 0.
304 This puts the block in the list after all its subblocks. */
305
306 pblock = (struct pending_block *) xmalloc (sizeof (struct pending_block));
307 pblock->block = block;
308 if (opblock)
309 {
310 pblock->next = opblock->next;
311 opblock->next = pblock;
312 }
313 else
314 {
315 pblock->next = pending_blocks;
316 pending_blocks = pblock;
317 }
318 }
319
320 static struct blockvector *
321 make_blockvector ()
322 {
323 register struct pending_block *next, *next1;
324 register struct blockvector *blockvector;
325 register int i;
326
327 /* Count the length of the list of blocks. */
328
329 for (next = pending_blocks, i = 0; next; next = next->next, i++);
330
331 blockvector = (struct blockvector *) xmalloc (sizeof (struct blockvector) + (i - 1) * sizeof (struct block *));
332
333 /* Copy the blocks into the blockvector.
334 This is done in reverse order, which happens to put
335 the blocks into the proper order (ascending starting address).
336 finish_block has hair to insert each block into the list
337 after its subblocks in order to make sure this is true. */
338
339 BLOCKVECTOR_NBLOCKS (blockvector) = i;
340 for (next = pending_blocks; next; next = next->next)
341 BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
342
343 /* Now free the links of the list, and empty the list. */
344
345 for (next = pending_blocks; next; next = next1)
346 {
347 next1 = next->next;
348 free (next);
349 }
350 pending_blocks = 0;
351
352 return blockvector;
353 }
354
355 /* Manage the vector of line numbers. */
356
357 static
358 record_line (line, pc)
359 int line;
360 CORE_ADDR pc;
361 {
362 /* Make sure line vector is big enough. */
363
364 if (line_vector_index + 2 >= line_vector_length)
365 {
366 line_vector_length *= 2;
367 line_vector = (struct linetable *)
368 xrealloc (line_vector, sizeof (struct linetable)
369 + line_vector_length * sizeof (int));
370 }
371
372 /* If this line is not continguous with previous one recorded,
373 all lines between subsequent line and current one are same pc.
374 Add one item to line vector, and if more than one line skipped,
375 record a line-number entry for it. */
376 if (prev_line_number > 0 && line != prev_line_number + 1)
377 line_vector->item[line_vector_index++] = pc;
378 if (prev_line_number < 0 || line > prev_line_number + 2)
379 line_vector->item[line_vector_index++] = - line;
380 prev_line_number = line;
381
382 /* Record the core address of the line. */
383 line_vector->item[line_vector_index++] = pc;
384 }
385 \f
386 /* Start a new symtab for a new source file.
387 This is called when a COFF ".file" symbol is seen;
388 it indicates the start of data for one original source file. */
389
390 static void
391 start_symtab ()
392 {
393 file_symbols = 0;
394 global_symbols = 0;
395 context_stack = 0;
396 within_function = 0;
397 last_source_file = 0;
398
399 /* Initialize the source file information for this file. */
400
401 line_vector_index = 0;
402 line_vector_length = 1000;
403 prev_line_number = -2; /* Force first line number to be explicit */
404 line_vector = (struct linetable *)
405 xmalloc (sizeof (struct linetable) + line_vector_length * sizeof (int));
406 }
407
408 /* Save the vital information for use when closing off the current file.
409 NAME is the file name the symbols came from, START_ADDR is the first
410 text address for the file, and SIZE is the number of bytes of text. */
411
412 static void
413 complete_symtab (name, start_addr, size)
414 char *name;
415 CORE_ADDR start_addr;
416 unsigned int size;
417 {
418 last_source_file = savestring (name, strlen (name));
419 cur_src_start_addr = start_addr;
420 cur_src_end_addr = start_addr + size;
421 }
422
423 /* Finish the symbol definitions for one main source file,
424 close off all the lexical contexts for that file
425 (creating struct block's for them), then make the
426 struct symtab for that file and put it in the list of all such. */
427
428 static void
429 end_symtab ()
430 {
431 register struct symtab *symtab;
432 register struct context_stack *cstk;
433 register struct blockvector *blockvector;
434 register struct linetable *lv;
435
436 /* Finish the lexical context of the last function in the file. */
437
438 if (context_stack)
439 {
440 cstk = context_stack;
441 /* Make a block for the local symbols within. */
442 finish_block (cstk->name, &local_symbols, cstk->old_blocks,
443 cstk->start_addr, cur_src_end_addr);
444 free (cstk);
445 }
446
447 finish_block (0, &file_symbols, 0, cur_src_start_addr, cur_src_end_addr);
448 finish_block (0, &global_symbols, 0, cur_src_start_addr, cur_src_end_addr);
449 blockvector = make_blockvector ();
450
451 /* Now create the symtab objects proper this source file. */
452
453 symtab = (struct symtab *) xmalloc (sizeof (struct symtab));
454 /* Fill in its components. */
455 symtab->blockvector = blockvector;
456 symtab->free_code = free_contents;
457 symtab->free_ptr = 0;
458 symtab->filename = last_source_file;
459 lv = line_vector;
460 lv->nitems = line_vector_index;
461 symtab->linetable = (struct linetable *)
462 xrealloc (lv, sizeof (struct linetable) + lv->nitems * sizeof (int));
463 symtab->nlines = 0;
464 symtab->line_charpos = 0;
465
466 /* Link the new symtab into the list of such. */
467 symtab->next = symtab_list;
468 symtab_list = symtab;
469
470 line_vector = 0;
471 line_vector_length = -1;
472 last_source_file = 0;
473 }
474 \f
475 /* Accumulate the misc functions in bunches of 127.
476 At the end, copy them all into one newly allocated structure. */
477
478 #define MISC_BUNCH_SIZE 127
479
480 struct misc_bunch
481 {
482 struct misc_bunch *next;
483 struct misc_function contents[MISC_BUNCH_SIZE];
484 };
485
486 /* Bunch currently being filled up.
487 The next field points to chain of filled bunches. */
488
489 static struct misc_bunch *misc_bunch;
490
491 /* Number of slots filled in current bunch. */
492
493 static int misc_bunch_index;
494
495 /* Total number of misc functions recorded so far. */
496
497 static int misc_count;
498
499 static void
500 init_misc_functions ()
501 {
502 misc_count = 0;
503 misc_bunch = 0;
504 misc_bunch_index = MISC_BUNCH_SIZE;
505 }
506
507 static void
508 record_misc_function (name, address)
509 char *name;
510 CORE_ADDR address;
511 {
512 register struct misc_bunch *new;
513
514 if (misc_bunch_index == MISC_BUNCH_SIZE)
515 {
516 new = (struct misc_bunch *) xmalloc (sizeof (struct misc_bunch));
517 misc_bunch_index = 0;
518 new->next = misc_bunch;
519 misc_bunch = new;
520 }
521 misc_bunch->contents[misc_bunch_index].name = savestring (name, strlen (name));
522 misc_bunch->contents[misc_bunch_index].address = address;
523 misc_bunch_index++;
524 misc_count++;
525 }
526
527 static int
528 compare_misc_functions (fn1, fn2)
529 struct misc_function *fn1, *fn2;
530 {
531 /* Return a signed result based on unsigned comparisons
532 so that we sort into unsigned numeric order. */
533 if (fn1->address < fn2->address)
534 return -1;
535 if (fn1->address > fn2->address)
536 return 1;
537 return 0;
538 }
539
540 static void
541 discard_misc_bunches ()
542 {
543 register struct misc_bunch *next;
544
545 while (misc_bunch)
546 {
547 next = misc_bunch->next;
548 free (misc_bunch);
549 misc_bunch = next;
550 }
551 }
552
553 static void
554 condense_misc_bunches ()
555 {
556 register int i, j;
557 register struct misc_bunch *bunch;
558 #ifdef NAMES_HAVE_UNDERSCORE
559 int offset = 1;
560 #else
561 int offset = 0;
562 #endif
563
564 misc_function_vector
565 = (struct misc_function *)
566 xmalloc (misc_count * sizeof (struct misc_function));
567
568 j = 0;
569 bunch = misc_bunch;
570 while (bunch)
571 {
572 for (i = 0; i < misc_bunch_index; i++)
573 {
574 register char *tmp;
575
576 misc_function_vector[j] = bunch->contents[i];
577 tmp = misc_function_vector[j].name;
578 misc_function_vector[j].name = (tmp[0] == '_' ? tmp + offset : tmp);
579 j++;
580 }
581 bunch = bunch->next;
582 misc_bunch_index = MISC_BUNCH_SIZE;
583 }
584
585 misc_function_count = j;
586
587 /* Sort the misc functions by address. */
588
589 qsort (misc_function_vector, j, sizeof (struct misc_function),
590 compare_misc_functions);
591 }
592
593 /* Call sort_syms to sort alphabetically
594 the symbols of each block of each symtab. */
595
596 static int
597 compare_symbols (s1, s2)
598 struct symbol **s1, **s2;
599 {
600 /* Names that are less should come first. */
601 register int namediff = strcmp (SYMBOL_NAME (*s1), SYMBOL_NAME (*s2));
602 if (namediff != 0) return namediff;
603 /* For symbols of the same name, registers should come first. */
604 return ((SYMBOL_CLASS (*s2) == LOC_REGISTER)
605 - (SYMBOL_CLASS (*s1) == LOC_REGISTER));
606 }
607
608 static void
609 sort_syms ()
610 {
611 register struct symtab *s;
612 register int i, nbl;
613 register struct blockvector *bv;
614 register struct block *b;
615
616 for (s = symtab_list; s; s = s->next)
617 {
618 bv = BLOCKVECTOR (s);
619 nbl = BLOCKVECTOR_NBLOCKS (bv);
620 for (i = 0; i < nbl; i++)
621 {
622 b = BLOCKVECTOR_BLOCK (bv, i);
623 qsort (&BLOCK_SYM (b, 0), BLOCK_NSYMS (b),
624 sizeof (struct symbol *), compare_symbols);
625 }
626 }
627 }
628 \f
629 /* This is the symbol-file command. Read the file, analyze its symbols,
630 and add a struct symtab to symtab_list. */
631
632 void
633 symbol_file_command (name)
634 char *name;
635 {
636 int desc;
637 int num_symbols;
638 int num_sections;
639 int symtab_offset;
640 extern void close ();
641 register int val;
642 struct cleanup *old_chain;
643
644 dont_repeat ();
645
646 if (name == 0)
647 {
648 if (symtab_list && !query ("Discard symbol table? ", 0))
649 error ("Not confirmed.");
650 free_all_symtabs ();
651 return;
652 }
653
654 if (symtab_list && !query ("Load new symbol table from \"%s\"? ", name))
655 error ("Not confirmed.");
656
657 if (symfile)
658 free (symfile);
659 symfile = 0;
660
661 {
662 char *absolute_name;
663
664 desc = openp (getenv ("PATH"), 1, name, O_RDONLY, 0, &absolute_name);
665 if (desc < 0)
666 perror_with_name (name);
667 else
668 name = absolute_name;
669 }
670
671 old_chain = make_cleanup (close, desc);
672 make_cleanup (free_current_contents, &name);
673
674 if ((num_symbols = read_file_hdr (desc, &file_hdr)) < 0)
675 error ("File \"%s\" not in executable format.", name);
676
677 if (num_symbols == 0)
678 {
679 free_all_symtabs ();
680 printf ("%s does not have a symbol-table.\n", name);
681 fflush (stdout);
682 return;
683 }
684
685 num_sections = file_hdr.f_nscns;
686 symtab_offset = file_hdr.f_symptr;
687
688 if (read_section_hdr (desc, _TEXT, &text_hdr, num_sections) < 0)
689 error ("\"%s\": can't read text section header", name);
690
691 /* Read the line number table, all at once. */
692
693 val = init_lineno (desc, text_hdr.s_lnnoptr, text_hdr.s_nlnno);
694 if (val < 0)
695 error ("\"%s\": error reading line numbers\n", name);
696
697 /* Now read the string table, all at once. */
698
699 val = init_stringtab (desc, symtab_offset + num_symbols * SYMESZ);
700 if (val < 0)
701 error ("\"%s\": can't get string table", name);
702 make_cleanup (free_stringtab, 0);
703
704 /* Position to read the symbol table. Do not read it all at once. */
705 val = lseek (desc, (long)symtab_offset, 0);
706 if (val < 0)
707 perror_with_name (name);
708
709 printf ("Reading symbol data from %s...", name);
710 fflush (stdout);
711
712 /* Throw away the old symbol table. */
713
714 free_all_symtabs ();
715
716 init_misc_functions ();
717 make_cleanup (discard_misc_bunches, 0);
718
719 /* Now that the executable file is positioned at symbol table,
720 process it and define symbols accordingly. */
721
722 read_coff_symtab (desc, num_symbols);
723
724 patch_opaque_types ();
725
726 /* Sort symbols alphabetically within each block. */
727
728 sort_syms ();
729
730 /* Go over the misc functions and install them in vector. */
731
732 condense_misc_bunches ();
733
734 /* Don't allow char * to have a typename (else would get caddr_t.) */
735
736 TYPE_NAME (lookup_pointer_type (builtin_type_char)) = 0;
737
738 /* Make a default for file to list. */
739
740 select_source_symtab (symtab_list);
741
742 symfile = savestring (name, strlen (name));
743
744 do_cleanups (old_chain);
745
746 printf ("done.\n");
747 fflush (stdout);
748 }
749
750 /* Return name of file symbols were loaded from, or 0 if none.. */
751
752 char *
753 get_sym_file ()
754 {
755 return symfile;
756 }
757 \f
758 /* Simplified internal version of coff symbol table information */
759
760 struct coff_symbol {
761 char *c_name;
762 int c_symnum; /* symbol number of this entry */
763 int c_nsyms; /* 1 if syment only, 2 if syment + auxent */
764 long c_value;
765 int c_sclass;
766 int c_secnum;
767 unsigned int c_type;
768 };
769
770 /* Given pointers to a symbol table in coff style exec file,
771 analyze them and create struct symtab's describing the symbols.
772 NSYMS is the number of symbols in the symbol table.
773 We read them one at a time using read_one_sym (). */
774
775 static void
776 read_coff_symtab (desc, nsyms)
777 int desc;
778 int nsyms;
779 {
780 FILE *stream = fdopen (desc, "r");
781 register struct context_stack *new;
782 struct coff_symbol coff_symbol;
783 register struct coff_symbol *cs = &coff_symbol;
784 static SYMENT main_sym;
785 static AUXENT main_aux;
786
787 int num_object_files = 0;
788 int next_file_symnum = 0;
789 char *filestring;
790 int depth;
791 int fcn_first_line;
792 int fcn_last_line;
793 long fcn_line_ptr;
794
795 nlist_stream_global = stream;
796 nlist_nsyms_global = nsyms;
797 last_source_file = 0;
798 bzero (opaque_type_chain, sizeof opaque_type_chain);
799
800 type_vector_length = 160;
801 type_vector = (struct typevector *)
802 xmalloc (sizeof (struct typevector)
803 + type_vector_length * sizeof (struct type *));
804 bzero (type_vector->type, type_vector_length * sizeof (struct type *));
805
806 start_symtab ();
807
808 symnum = 0;
809 while (symnum < nsyms)
810 {
811 read_one_sym (cs, &main_sym, &main_aux);
812
813 /*
814 * If we are finished with the previous file's symbols, and the
815 * next thing is not a C_FILE, then we have hit the global symbols.
816 */
817 if (cs->c_symnum == next_file_symnum && cs->c_sclass != C_FILE)
818 {
819 CORE_ADDR last_file_end = cur_src_end_addr;
820
821 if (last_source_file)
822 end_symtab ();
823
824 start_symtab ();
825 complete_symtab ("_globals_", 0, first_object_file_end);
826 /* done with all files, everything from here on out is globals */
827 }
828
829 if (ISFCN (cs->c_type))
830 {
831 /*
832 * gdb expects all functions to also be in misc_function
833 * list -- why not...
834 */
835 record_misc_function (cs->c_name, cs->c_value);
836
837 fcn_line_ptr = main_aux.x_sym.x_fcnary.x_fcn.x_lnnoptr;
838 within_function = 1;
839
840 new = (struct context_stack *)
841 xmalloc (sizeof (struct context_stack));
842 new->depth = depth = 0;
843 new->next = 0;
844 context_stack = new;
845 new->locals = 0;
846 new->old_blocks = pending_blocks;
847 new->start_addr = cs->c_value;
848 new->name = process_coff_symbol (cs, &main_aux);
849 continue;
850 }
851
852 switch (cs->c_sclass)
853 {
854 case C_EFCN:
855 case C_EXTDEF:
856 case C_ULABEL:
857 case C_USTATIC:
858 case C_LINE:
859 case C_ALIAS:
860 case C_HIDDEN:
861 printf ("Bad n_sclass = %d\n", cs->c_sclass);
862 break;
863
864 case C_FILE:
865 /*
866 * c_value field contains symnum of next .file entry in table
867 * or symnum of first global after last .file.
868 */
869 next_file_symnum = cs->c_value;
870 filestring = getfilename (&main_aux);
871 /*
872 * Complete symbol table for last object file
873 * containing debugging information.
874 */
875 if (last_source_file)
876 {
877 end_symtab ();
878 start_symtab ();
879 }
880 complete_symtab (filestring, 0, 0); /* FIXME, 0 0 is wrong */
881 num_object_files++;
882 break;
883
884 case C_EXT:
885 if (cs->c_secnum == N_ABS && strcmp (cs->c_name, _ETEXT) == 0)
886 {
887 end_of_text_addr = cs->c_value;
888 }
889 if (cs->c_type == T_NULL)
890 {
891 if (cs->c_secnum <= 1) /* text or abs */
892 {
893 record_misc_function (cs->c_name, cs->c_value);
894 break;
895 }
896 else
897 cs->c_type = T_INT;
898 }
899 (void) process_coff_symbol (cs, &main_aux);
900 break;
901
902 case C_STAT:
903 if (cs->c_type == T_NULL && cs->c_secnum > N_UNDEF)
904 {
905 /* These ".text", ".data", ".bss" entries don't seem to
906 * appear in A/UX COFF output. -- gnu@toad.com 4Apr88
907 */
908 if (strcmp (cs->c_name, _TEXT) == 0)
909 {
910 /* We have a ".text" symbol */
911 if (num_object_files == 1)
912 {
913 /* Record end address of first file, crt0.s */
914 first_object_file_end =
915 cs->c_value + main_aux.x_scn.x_scnlen;
916 }
917 /*
918 * Fill in missing information for debugged
919 * object file only if we have line number info.
920 */
921 if (main_aux.x_scn.x_nlinno > 0)
922 {
923 complete_symtab (filestring, cs->c_value,
924 main_aux.x_scn.x_scnlen);
925 }
926 break;
927 }
928 else if (strcmp (cs->c_name, _DATA) == 0)
929 break;
930 else if (strcmp (cs->c_name, _BSS) == 0)
931 break;
932
933 /* get rid of assembly labels here */
934 /* record_misc_function (cs->c_name, cs->c_value); */
935 break;
936 }
937 (void) process_coff_symbol (cs, &main_aux);
938 break;
939
940 case C_FCN:
941 if (strcmp (cs->c_name, ".bf") == 0)
942 {
943 /* value contains address of first non-init type code */
944 /* main_aux.x_sym.x_misc.x_lnsz.x_lnno
945 contains line number of '{' } */
946 fcn_first_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
947 }
948 else if (strcmp (cs->c_name, ".ef") == 0)
949 {
950 /* value contains address of exit/return from function */
951 /* round it up to next multiple of 16 */
952 cs->c_value = (cs->c_value + 15) & -16;
953 /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
954 contains number of lines to '}' */
955 fcn_last_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
956 enter_linenos (fcn_line_ptr, fcn_first_line, fcn_last_line);
957
958 new = context_stack;
959 finish_block (new->name, &local_symbols, new->old_blocks,
960 new->start_addr, cs->c_value);
961 context_stack = 0;
962 within_function = 0;
963 free (new);
964 }
965 break;
966
967 case C_BLOCK:
968 if (strcmp (cs->c_name, ".bb") == 0)
969 {
970 new = (struct context_stack *)
971 xmalloc (sizeof (struct context_stack));
972 depth++;
973 new->depth = depth;
974 new->next = context_stack;
975 context_stack = new;
976 new->locals = local_symbols;
977 new->old_blocks = pending_blocks;
978 new->start_addr = cs->c_value;
979 new->name = 0;
980 local_symbols = 0;
981 }
982 else if (strcmp (cs->c_name, ".eb") == 0)
983 {
984 new = context_stack;
985 if (new == 0 || depth != new->depth)
986 error ("Invalid symbol data: .bb/.eb symbol mismatch.");
987 if (local_symbols && context_stack->next)
988 {
989 /* Make a block for the local symbols within. */
990 finish_block (0, &local_symbols, new->old_blocks,
991 new->start_addr, cs->c_value);
992 }
993 depth--;
994 local_symbols = new->locals;
995 context_stack = new->next;
996 free (new);
997 }
998 break;
999
1000 default:
1001 (void) process_coff_symbol (cs, &main_aux);
1002 break;
1003 }
1004 }
1005
1006 if (last_source_file)
1007 end_symtab ();
1008 fclose (stream);
1009 }
1010 \f
1011 /* Routines for reading headers and symbols from executable. */
1012
1013 /* Read COFF file header, check magic number,
1014 and return number of symbols. */
1015 read_file_hdr (chan, file_hdr)
1016 int chan;
1017 FILHDR *file_hdr;
1018 {
1019 lseek (chan, 0L, 0);
1020 if (myread (chan, (char *)file_hdr, FILHSZ) < 0)
1021 return -1;
1022
1023 if (BADMAG(file_hdr))
1024 return -1; /* Non understood file */
1025 return file_hdr->f_nsyms; /* OK magic number, return # syms */
1026 }
1027
1028 read_aout_hdr (chan, aout_hdr, size)
1029 int chan;
1030 AOUTHDR *aout_hdr;
1031 int size;
1032 {
1033 lseek (chan, (long)FILHSZ, 0);
1034 if (size != sizeof (AOUTHDR))
1035 return -1;
1036 if (myread (chan, (char *)aout_hdr, size) != size)
1037 return -1;
1038 return 0;
1039 }
1040
1041 read_section_hdr (chan, section_name, section_hdr, nsects)
1042 register int chan;
1043 register char *section_name;
1044 SCNHDR *section_hdr;
1045 register int nsects;
1046 {
1047 register int i;
1048
1049 if (lseek (chan, FILHSZ + sizeof (AOUTHDR), 0) < 0)
1050 return -1;
1051
1052 for (i = 0; i < nsects; i++)
1053 {
1054 if (myread (chan, (char *)section_hdr, SCNHSZ) < 0)
1055 return -1;
1056 if (strncmp (section_hdr->s_name, section_name, 8) == 0)
1057 return 0;
1058 }
1059 return -1;
1060 }
1061
1062 read_one_sym (cs, sym, aux)
1063 register struct coff_symbol *cs;
1064 register SYMENT *sym;
1065 register AUXENT *aux;
1066 {
1067 cs->c_symnum = symnum;
1068 fread ((char *)sym, SYMESZ, 1, nlist_stream_global);
1069 cs->c_nsyms = (sym->n_numaux & 0xff) + 1;
1070 if (cs->c_nsyms == 2)
1071 {
1072 /* doc for coff says there is either no aux entry or just one */
1073 fread ((char *)aux, AUXESZ, 1, nlist_stream_global);
1074 }
1075 else if (cs->c_nsyms > 2)
1076 error ("more than one aux symbol table entry at symnum=%d\n", symnum);
1077
1078 cs->c_name = getsymname (sym);
1079 cs->c_value = sym->n_value;
1080 cs->c_sclass = (sym->n_sclass & 0xff);
1081 cs->c_secnum = sym->n_scnum;
1082 cs->c_type = (unsigned) sym->n_type;
1083
1084 #ifdef DEBUG
1085 if (debug) {
1086 fprintf(stderr, "sym %3x: %2x %s %x %x %x", cs->c_symnum,
1087 cs->c_sclass, cs->c_name, cs->c_value, cs->c_secnum, cs->c_type);
1088 if (cs->c_nsyms > 1)
1089 fprintf(stderr, " +aux %s\n", (char *)aux);
1090 else
1091 fprintf(stderr, "\n");
1092 }
1093 #endif
1094
1095 symnum += cs->c_nsyms;
1096 }
1097 \f
1098 /* Support for string table handling */
1099
1100 static char *stringtab = NULL;
1101
1102 static int
1103 init_stringtab (chan, offset)
1104 int chan;
1105 long offset;
1106 {
1107 long buffer;
1108 int val;
1109
1110 if (lseek (chan, offset, 0) < 0)
1111 return -1;
1112
1113 val = myread (chan, (char *)&buffer, sizeof buffer);
1114
1115 /* If no string table, we get 0 bytes back from the read. That's OK. */
1116 if (val == 0) {
1117 free_stringtab();
1118 return 0;
1119 }
1120
1121 if (val != sizeof buffer)
1122 return -1;
1123
1124 if (stringtab)
1125 free (stringtab);
1126 stringtab = (char *) xmalloc (buffer);
1127 bcopy (&buffer, stringtab, sizeof buffer);
1128
1129 val = myread (chan, stringtab + sizeof buffer, buffer - sizeof buffer);
1130 if (val != buffer - sizeof buffer || stringtab[buffer - 1] != '\0')
1131 return -1;
1132
1133 return 0;
1134 }
1135
1136 static void
1137 free_stringtab ()
1138 {
1139 if (stringtab)
1140 free (stringtab);
1141 stringtab = NULL;
1142 }
1143
1144 static char *
1145 getsymname (symbol_entry)
1146 SYMENT *symbol_entry;
1147 {
1148 static char buffer[SYMNMLEN+1];
1149 char *result;
1150
1151 if (symbol_entry->n_zeroes == 0)
1152 {
1153 if (!stringtab)
1154 error("Symbol entry references nonexistent string table");
1155 result = stringtab + symbol_entry->n_offset;
1156 }
1157 else
1158 {
1159 strncpy (buffer, symbol_entry->n_name, SYMNMLEN);
1160 buffer[SYMNMLEN] = '\0';
1161 result = buffer;
1162 }
1163 return result;
1164 }
1165
1166 static char *
1167 getfilename (aux_entry)
1168 AUXENT *aux_entry;
1169 {
1170 static char buffer[BUFSIZ];
1171 register char *temp;
1172 char *result;
1173 extern char *rindex ();
1174
1175 #ifndef mac_aux
1176 if (aux_entry->x_file.x_foff != 0)
1177 strcpy (buffer, stringtab + aux_entry->x_file.x_foff);
1178 else
1179 #endif
1180 {
1181 strncpy (buffer, aux_entry->x_file.x_fname, FILNMLEN);
1182 buffer[FILNMLEN] = '\0';
1183 }
1184 result = buffer;
1185 if ((temp = rindex (result, '/')) != NULL)
1186 result = temp + 1;
1187 return (result);
1188 }
1189
1190 /* Support for line number handling */
1191 static char *linetab = NULL;
1192 static long linetab_offset;
1193 static int linetab_count;
1194
1195 static int
1196 init_lineno (chan, offset, count)
1197 int chan;
1198 long offset;
1199 int count;
1200 {
1201 int val;
1202
1203 if (lseek (chan, offset, 0) < 0)
1204 return -1;
1205
1206 if (linetab)
1207 free (linetab);
1208 linetab = (char *) xmalloc (count * LINESZ);
1209
1210 val = myread (chan, linetab, count * LINESZ);
1211 if (val != count * LINESZ)
1212 return -1;
1213
1214 linetab_offset = offset;
1215 linetab_count = count;
1216 return 0;
1217 }
1218
1219 static void
1220 enter_linenos (file_offset, first_line, last_line)
1221 long file_offset;
1222 register int first_line;
1223 register int last_line;
1224 {
1225 register char *rawptr = &linetab[file_offset - linetab_offset];
1226 register struct lineno *lptr;
1227
1228 /* skip first line entry for each function */
1229 rawptr += LINESZ;
1230 /* line numbers start at one for the first line of the function */
1231 first_line--;
1232
1233 for (lptr = (struct lineno *)rawptr;
1234 lptr->l_lnno && lptr->l_lnno <= last_line;
1235 rawptr += LINESZ, lptr = (struct lineno *)rawptr)
1236 {
1237 record_line (first_line + lptr->l_lnno, lptr->l_addr.l_paddr);
1238 }
1239 }
1240 \f
1241 static int
1242 hashname (name)
1243 char *name;
1244 {
1245 register char *p = name;
1246 register int total = p[0];
1247 register int c;
1248
1249 c = p[1];
1250 total += c << 2;
1251 if (c)
1252 {
1253 c = p[2];
1254 total += c << 4;
1255 if (c)
1256 total += p[3] << 6;
1257 }
1258
1259 return total % HASHSIZE;
1260 }
1261
1262 static void
1263 patch_type (type, real_type)
1264 struct type *type;
1265 struct type *real_type;
1266 {
1267 register struct type *target = TYPE_TARGET_TYPE (type);
1268 register struct type *real_target = TYPE_TARGET_TYPE (real_type);
1269 int field_size = TYPE_NFIELDS (real_target) * sizeof (struct field);
1270
1271 TYPE_LENGTH (target) = TYPE_LENGTH (real_target);
1272 TYPE_NFIELDS (target) = TYPE_NFIELDS (real_target);
1273 TYPE_FIELDS (target) = (struct field *)
1274 obstack_alloc (symbol_obstack, field_size);
1275
1276 bcopy (TYPE_FIELDS (real_target), TYPE_FIELDS (target), field_size);
1277
1278 if (TYPE_NAME (real_target))
1279 {
1280 if (TYPE_NAME (target))
1281 free (TYPE_NAME (target));
1282 TYPE_NAME (target) = concat (TYPE_NAME (real_target), "", "");
1283 }
1284 }
1285
1286 /* Patch up all appropriate typdef symbols in the opaque_type_chains
1287 so that they can be used to print out opaque data structures properly */
1288
1289 static void
1290 patch_opaque_types ()
1291 {
1292 struct symtab *s;
1293
1294 /* Look at each symbol in the per-file block of each symtab. */
1295 for (s = symtab_list; s; s = s->next)
1296 {
1297 register struct block *b;
1298 register int i;
1299
1300 /* Go through the per-file symbols only */
1301 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), 1);
1302 for (i = BLOCK_NSYMS (b) - 1; i >= 0; i--)
1303 {
1304 register struct symbol *real_sym;
1305
1306 /* Find completed typedefs to use to fix opaque ones.
1307 Remove syms from the chain when their types are stored,
1308 but search the whole chain, as there may be several syms
1309 from different files with the same name. */
1310 real_sym = BLOCK_SYM (b, i);
1311 if (SYMBOL_CLASS (real_sym) == LOC_TYPEDEF &&
1312 SYMBOL_NAMESPACE (real_sym) == VAR_NAMESPACE &&
1313 TYPE_CODE (SYMBOL_TYPE (real_sym)) == TYPE_CODE_PTR &&
1314 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (real_sym))) != 0)
1315 {
1316 register char *name = SYMBOL_NAME (real_sym);
1317 register int hash = hashname (name);
1318 register struct symbol *sym, *prev;
1319
1320 prev = 0;
1321 for (sym = opaque_type_chain[hash]; sym;)
1322 {
1323 if (name[0] == SYMBOL_NAME (sym)[0] &&
1324 !strcmp (name + 1, SYMBOL_NAME (sym) + 1))
1325 {
1326 if (prev)
1327 SYMBOL_VALUE (prev) = SYMBOL_VALUE (sym);
1328 else
1329 opaque_type_chain[hash]
1330 = (struct symbol *) SYMBOL_VALUE (sym);
1331
1332 patch_type (SYMBOL_TYPE (sym), SYMBOL_TYPE (real_sym));
1333
1334 if (prev)
1335 sym = (struct symbol *) SYMBOL_VALUE (prev);
1336 else
1337 sym = opaque_type_chain[hash];
1338 }
1339 else
1340 {
1341 prev = sym;
1342 sym = (struct symbol *) SYMBOL_VALUE (sym);
1343 }
1344 }
1345 }
1346 }
1347 }
1348 }
1349 \f
1350 static struct symbol *
1351 process_coff_symbol (cs, aux)
1352 register struct coff_symbol *cs;
1353 register AUXENT *aux;
1354 {
1355 register struct symbol *sym = (struct symbol *)
1356 xmalloc (sizeof (struct symbol));
1357 char *name;
1358 char *dot;
1359 #ifdef NAMES_HAVE_UNDERSCORE
1360 int offset = 1;
1361 #else
1362 int offset = 0;
1363 #endif
1364
1365 bzero (sym, sizeof (struct symbol));
1366 name = cs->c_name;
1367 name = (name[0] == '_' ? name + offset : name);
1368 SYMBOL_NAME (sym) = savestring (name, strlen (name));
1369
1370 /* default assumptions */
1371 SYMBOL_VALUE (sym) = cs->c_value;
1372 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1373
1374 if (ISFCN (cs->c_type))
1375 {
1376 SYMBOL_TYPE (sym) = decode_function_type (cs, cs->c_type, aux);
1377 SYMBOL_CLASS (sym) = LOC_BLOCK;
1378 if (cs->c_sclass == C_STAT)
1379 add_symbol_to_list (sym, &file_symbols);
1380 else if (cs->c_sclass == C_EXT)
1381 add_symbol_to_list (sym, &global_symbols);
1382 }
1383 else
1384 {
1385 SYMBOL_TYPE (sym) = decode_type (cs, cs->c_type, aux);
1386 switch (cs->c_sclass)
1387 {
1388 case C_NULL:
1389 break;
1390
1391 case C_AUTO:
1392 SYMBOL_CLASS (sym) = LOC_LOCAL;
1393 add_symbol_to_list (sym, &local_symbols);
1394 break;
1395
1396 case C_EXT:
1397 SYMBOL_CLASS (sym) = LOC_STATIC;
1398 add_symbol_to_list (sym, &global_symbols);
1399 break;
1400
1401 case C_STAT:
1402 SYMBOL_CLASS (sym) = LOC_STATIC;
1403 if (within_function) {
1404 /* Static symbol of local scope */
1405 add_symbol_to_list (sym, &local_symbols);
1406 }
1407 else {
1408 /* Static symbol at top level of file */
1409 add_symbol_to_list (sym, &file_symbols);
1410 }
1411 break;
1412
1413 case C_REG:
1414 case C_REGPARM:
1415 SYMBOL_CLASS (sym) = LOC_REGISTER;
1416 add_symbol_to_list (sym, &local_symbols);
1417 break;
1418
1419 case C_LABEL:
1420 break;
1421
1422 case C_ARG:
1423 SYMBOL_CLASS (sym) = LOC_ARG;
1424 add_symbol_to_list (sym, &local_symbols);
1425 /* If PCC says a parameter is a short or a char,
1426 it is really an int. */
1427 if (SYMBOL_TYPE (sym) == builtin_type_char
1428 || SYMBOL_TYPE (sym) == builtin_type_short)
1429 SYMBOL_TYPE (sym) = builtin_type_int;
1430 else if (SYMBOL_TYPE (sym) == builtin_type_unsigned_char
1431 || SYMBOL_TYPE (sym) == builtin_type_unsigned_short)
1432 SYMBOL_TYPE (sym) = builtin_type_unsigned_int;
1433 break;
1434
1435 case C_TPDEF:
1436 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
1437 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1438
1439 /* If type has no name, give it one */
1440 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0
1441 && (TYPE_FLAGS (SYMBOL_TYPE (sym)) & TYPE_FLAG_PERM) == 0)
1442 TYPE_NAME (SYMBOL_TYPE (sym))
1443 = concat (SYMBOL_NAME (sym), "", "");
1444
1445 /* Keep track of any type which points to empty structured type,
1446 so it can be filled from a definition from another file */
1447 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_PTR &&
1448 TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym))) == 0)
1449 {
1450 register int i = hashname (SYMBOL_NAME (sym));
1451
1452 SYMBOL_VALUE (sym) = (int) opaque_type_chain[i];
1453 opaque_type_chain[i] = sym;
1454 }
1455 add_symbol_to_list (sym, &file_symbols);
1456 break;
1457
1458 case C_STRTAG:
1459 case C_UNTAG:
1460 case C_ENTAG:
1461 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
1462 SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
1463 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0
1464 && (TYPE_FLAGS (SYMBOL_TYPE (sym)) & TYPE_FLAG_PERM) == 0)
1465 TYPE_NAME (SYMBOL_TYPE (sym))
1466 = concat ("",
1467 (cs->c_sclass == C_ENTAG
1468 ? "enum "
1469 : (cs->c_sclass == C_STRTAG
1470 ? "struct " : "union ")),
1471 SYMBOL_NAME (sym));
1472 add_symbol_to_list (sym, &file_symbols);
1473 break;
1474
1475 default:
1476 break;
1477 }
1478 }
1479 return sym;
1480 }
1481 \f
1482 /* Decode a coff type specifier;
1483 return the type that is meant. */
1484
1485 static
1486 struct type *
1487 decode_type (cs, c_type, aux)
1488 register struct coff_symbol *cs;
1489 unsigned int c_type;
1490 register AUXENT *aux;
1491 {
1492 register struct type *type = 0;
1493 register int n;
1494 unsigned int new_c_type;
1495
1496 if (c_type & ~N_BTMASK)
1497 {
1498 new_c_type = DECREF (c_type);
1499 if (ISPTR (c_type))
1500 {
1501 type = decode_type (cs, new_c_type, aux);
1502 type = lookup_pointer_type (type);
1503 }
1504 else if (ISFCN (c_type))
1505 {
1506 type = decode_type (cs, new_c_type, aux);
1507 type = lookup_function_type (type);
1508 }
1509 else if (ISARY (c_type))
1510 {
1511 int i, n;
1512 register unsigned short *dim;
1513 struct type *base_type;
1514
1515 /* Define an array type. */
1516 /* auxent refers to array, not base type */
1517 if (aux->x_sym.x_tagndx == 0)
1518 cs->c_nsyms = 1;
1519
1520 /* shift the indices down */
1521 dim = &aux->x_sym.x_fcnary.x_ary.x_dimen[0];
1522 i = 1;
1523 n = dim[0];
1524 for (i = 0; *dim && i < DIMNUM - 1; i++, dim++)
1525 *dim = *(dim + 1);
1526 *dim = 0;
1527
1528 type = (struct type *)
1529 obstack_alloc (symbol_obstack, sizeof (struct type));
1530 bzero (type, sizeof (struct type));
1531
1532 base_type = decode_type (cs, new_c_type, aux);
1533
1534 TYPE_CODE (type) = TYPE_CODE_ARRAY;
1535 TYPE_TARGET_TYPE (type) = base_type;
1536 TYPE_LENGTH (type) = n * TYPE_LENGTH (base_type);
1537 }
1538 return type;
1539 }
1540
1541 /* Reference to existing type */
1542 if (cs->c_nsyms > 1 && aux->x_sym.x_tagndx != 0)
1543 {
1544 type = coff_alloc_type (aux->x_sym.x_tagndx);
1545 return type;
1546 }
1547
1548 return decode_base_type (cs, BTYPE (c_type), aux);
1549 }
1550
1551 /* Decode a coff type specifier for function definition;
1552 return the type that the function returns. */
1553
1554 static
1555 struct type *
1556 decode_function_type (cs, c_type, aux)
1557 register struct coff_symbol *cs;
1558 unsigned int c_type;
1559 register AUXENT *aux;
1560 {
1561 if (aux->x_sym.x_tagndx == 0)
1562 cs->c_nsyms = 1; /* auxent refers to function, not base type */
1563
1564 return decode_type (cs, DECREF (cs->c_type), aux);
1565 }
1566 \f
1567 /* basic C types */
1568
1569 static
1570 struct type *
1571 decode_base_type (cs, c_type, aux)
1572 register struct coff_symbol *cs;
1573 unsigned int c_type;
1574 register AUXENT *aux;
1575 {
1576 struct type *type;
1577
1578 switch (c_type)
1579 {
1580 case T_NULL:
1581 /* NULL seems to be used as the basic type of void functions */
1582 return builtin_type_void;
1583 break;
1584
1585 case T_ARG:
1586 /* shouldn't show up here */
1587 break;
1588
1589 case T_CHAR:
1590 return builtin_type_char;
1591
1592 case T_SHORT:
1593 return builtin_type_short;
1594
1595 case T_INT:
1596 return builtin_type_int;
1597
1598 case T_LONG:
1599 return builtin_type_long;
1600
1601 case T_FLOAT:
1602 return builtin_type_float;
1603
1604 case T_DOUBLE:
1605 return builtin_type_double;
1606
1607 case T_STRUCT:
1608 if (cs->c_nsyms != 2)
1609 {
1610 /* anonymous structure type */
1611 type = coff_alloc_type (cs->c_symnum);
1612 TYPE_CODE (type) = TYPE_CODE_STRUCT;
1613 TYPE_NAME (type) = concat ("struct ", "<opaque>", "");
1614 TYPE_LENGTH (type) = 0;
1615 TYPE_FIELDS (type) = 0;
1616 TYPE_NFIELDS (type) = 0;
1617 }
1618 else
1619 {
1620 type = read_struct_type (cs->c_symnum,
1621 aux->x_sym.x_misc.x_lnsz.x_size,
1622 aux->x_sym.x_fcnary.x_fcn.x_endndx);
1623 }
1624 return type;
1625
1626 case T_UNION:
1627 if (cs->c_nsyms != 2)
1628 {
1629 /* anonymous union type */
1630 type = coff_alloc_type (cs->c_symnum);
1631 TYPE_NAME (type) = concat ("union ", "<opaque>", "");
1632 TYPE_LENGTH (type) = 0;
1633 TYPE_FIELDS (type) = 0;
1634 TYPE_NFIELDS (type) = 0;
1635 }
1636 else
1637 {
1638 type = read_struct_type (cs->c_symnum,
1639 aux->x_sym.x_misc.x_lnsz.x_size,
1640 aux->x_sym.x_fcnary.x_fcn.x_endndx);
1641 }
1642 TYPE_CODE (type) = TYPE_CODE_UNION;
1643 return type;
1644
1645 case T_ENUM:
1646 return read_enum_type (cs->c_symnum,
1647 aux->x_sym.x_misc.x_lnsz.x_size,
1648 aux->x_sym.x_fcnary.x_fcn.x_endndx);
1649
1650 case T_MOE:
1651 /* shouldn't show up here */
1652 break;
1653
1654 case T_UCHAR:
1655 return builtin_type_unsigned_char;
1656
1657 case T_USHORT:
1658 return builtin_type_unsigned_short;
1659
1660 case T_UINT:
1661 return builtin_type_unsigned_int;
1662
1663 case T_ULONG:
1664 return builtin_type_unsigned_long;
1665 }
1666 printf ("unexpected type %d at symnum %d, name %s\n", c_type, cs->c_symnum,
1667 cs->c_name);
1668 return builtin_type_void;
1669 }
1670 \f
1671 /* This page contains subroutines of read_type. */
1672
1673 /* Read the description of a structure (or union type)
1674 and return an object describing the type. */
1675
1676 static struct type *
1677 read_struct_type (index, length, lastsym)
1678 int index;
1679 int length;
1680 int lastsym;
1681 {
1682 struct nextfield
1683 {
1684 struct nextfield *next;
1685 struct field field;
1686 };
1687
1688 register struct type *type;
1689 register struct nextfield *list = 0;
1690 struct nextfield *new;
1691 int nfields = 0;
1692 register int n;
1693 char *name;
1694 #ifdef NAMES_HAVE_UNDERSCORE
1695 int offset = 1;
1696 #else
1697 int offset = 0;
1698 #endif
1699 struct coff_symbol member_sym;
1700 register struct coff_symbol *ms = &member_sym;
1701 SYMENT sub_sym;
1702 AUXENT sub_aux;
1703
1704 type = coff_alloc_type (index);
1705 TYPE_CODE (type) = TYPE_CODE_STRUCT;
1706 TYPE_LENGTH (type) = length;
1707
1708 while (symnum < lastsym && symnum < nlist_nsyms_global)
1709 {
1710 read_one_sym (ms, &sub_sym, &sub_aux);
1711 name = ms->c_name;
1712 name = (name[0] == '_' ? name + offset : name);
1713
1714 switch (ms->c_sclass)
1715 {
1716 case C_MOS:
1717 case C_MOU:
1718
1719 /* Get space to record the next field's data. */
1720 new = (struct nextfield *) alloca (sizeof (struct nextfield));
1721 new->next = list;
1722 list = new;
1723
1724 /* Save the data. */
1725 list->field.name = savestring (name, strlen (name));
1726 list->field.type = decode_type (ms, ms->c_type, &sub_aux);
1727 list->field.bitpos = 8 * ms->c_value;
1728 list->field.bitsize = 0;
1729 nfields++;
1730 break;
1731
1732 case C_FIELD:
1733
1734 /* Get space to record the next field's data. */
1735 new = (struct nextfield *) alloca (sizeof (struct nextfield));
1736 new->next = list;
1737 list = new;
1738
1739 /* Save the data. */
1740 list->field.name = savestring (name, strlen (name));
1741 list->field.type = decode_type (ms, ms->c_type, &sub_aux);
1742 list->field.bitpos = ms->c_value;
1743 list->field.bitsize = sub_aux.x_sym.x_misc.x_lnsz.x_size;
1744 nfields++;
1745 break;
1746
1747 case C_EOS:
1748 break;
1749 }
1750 }
1751 /* Now create the vector of fields, and record how big it is. */
1752
1753 TYPE_NFIELDS (type) = nfields;
1754 TYPE_FIELDS (type) = (struct field *)
1755 obstack_alloc (symbol_obstack, sizeof (struct field) * nfields);
1756
1757 /* Copy the saved-up fields into the field vector. */
1758
1759 for (n = nfields; list; list = list->next)
1760 TYPE_FIELD (type, --n) = list->field;
1761
1762 return type;
1763 }
1764 \f
1765 /* Read a definition of an enumeration type,
1766 and create and return a suitable type object.
1767 Also defines the symbols that represent the values of the type. */
1768
1769 static struct type *
1770 read_enum_type (index, length, lastsym)
1771 int index;
1772 int length;
1773 int lastsym;
1774 {
1775 register struct symbol *sym;
1776 register struct type *type;
1777 int nsyms = 0;
1778 struct pending **symlist;
1779 struct coff_symbol member_sym;
1780 register struct coff_symbol *ms = &member_sym;
1781 SYMENT sub_sym;
1782 AUXENT sub_aux;
1783 struct pending *osyms, *syms;
1784 register int n;
1785 char *name;
1786 #ifdef NAMES_HAVE_UNDERSCORE
1787 int offset = 1;
1788 #else
1789 int offset = 0;
1790 #endif
1791
1792 type = coff_alloc_type (index);
1793 if (within_function)
1794 symlist = &local_symbols;
1795 else
1796 symlist = &file_symbols;
1797 osyms = *symlist;
1798
1799 while (symnum < lastsym && symnum < nlist_nsyms_global)
1800 {
1801 read_one_sym (ms, &sub_sym, &sub_aux);
1802 name = ms->c_name;
1803 name = (name[0] == '_' ? name + offset : name);
1804
1805 switch (ms->c_sclass)
1806 {
1807 case C_MOE:
1808 sym = (struct symbol *) xmalloc (sizeof (struct symbol));
1809 bzero (sym, sizeof (struct symbol));
1810
1811 SYMBOL_NAME (sym) = savestring (name, strlen (name));
1812 SYMBOL_CLASS (sym) = LOC_CONST;
1813 SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1814 SYMBOL_VALUE (sym) = ms->c_value;
1815 add_symbol_to_list (sym, symlist);
1816 nsyms++;
1817 break;
1818
1819 case C_EOS:
1820 break;
1821 }
1822 }
1823
1824 /* Now fill in the fields of the type-structure. */
1825
1826 TYPE_LENGTH (type) = sizeof (int);
1827 TYPE_CODE (type) = TYPE_CODE_ENUM;
1828 TYPE_NFIELDS (type) = nsyms;
1829 TYPE_FIELDS (type) = (struct field *)
1830 obstack_alloc (symbol_obstack, sizeof (struct field) * nsyms);
1831
1832 /* Find the symbols for the values and put them into the type.
1833 The symbols can be found in the symlist that we put them on
1834 to cause them to be defined. osyms contains the old value
1835 of that symlist; everything up to there was defined by us. */
1836
1837 for (syms = *symlist, n = nsyms; syms != osyms; syms = syms->next)
1838 {
1839 SYMBOL_TYPE (syms->symbol) = type;
1840 TYPE_FIELD_NAME (type, --n) = SYMBOL_NAME (syms->symbol);
1841 TYPE_FIELD_VALUE (type, n) = SYMBOL_VALUE (syms->symbol);
1842 TYPE_FIELD_BITPOS (type, n) = 0;
1843 TYPE_FIELD_BITSIZE (type, n) = 0;
1844 }
1845 return type;
1846 }
1847
1848 static
1849 initialize ()
1850 {
1851 symfile = 0;
1852
1853 add_com ("symbol-file", class_files, symbol_file_command,
1854 "Load symbol table (in coff format) from executable file FILE.");
1855 }
1856
1857 END_FILE
1858
1859 #endif /* COFF_FORMAT */
1860
This page took 0.067922 seconds and 4 git commands to generate.