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