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