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