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