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