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