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