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