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