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