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