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