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