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