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