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