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