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