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