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