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