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