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