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