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