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