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