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