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