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