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