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