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