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