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