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