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