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