]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/coffread.c
Add missing changelog entry
[thirdparty/binutils-gdb.git] / gdb / coffread.c
CommitLineData
c906108c 1/* Read coff symbol tables and convert to internal format, for GDB.
42a4f53d 2 Copyright (C) 1987-2019 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;
225 name = bfd_get_section_name (abfd, sectp);
7ecb6532 226 if (strcmp (name, ".text") == 0)
c906108c
SS
227 {
228 csi->textaddr = bfd_section_vma (abfd, sectp);
229 csi->textsize += bfd_section_size (abfd, sectp);
230 }
61012eef 231 else if (startswith (name, ".text"))
c906108c
SS
232 {
233 csi->textsize += bfd_section_size (abfd, sectp);
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)
81b9b86e 310 addr = bfd_get_section_vma (abfd, 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{
420 /* The following is a bit of a heuristic using the characterictics
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
459 reverse lookup; this manifests itselfs in backtraces, or any
460 other commands that prints symbolic addresses. Just pretend
461 these symbols do not exist. */
462 return NULL;
463 }
464
3db066bc
TT
465 return reader.record_full (cs->c_name, strlen (cs->c_name), true, address,
466 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);
b926417a 541 char *filename = bfd_get_filename (abfd);
52f0bd74 542 int val;
745b8ca0 543 unsigned int num_symbols;
c906108c
SS
544 int symtab_offset;
545 int stringtab_offset;
c906108c 546 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,
e2a03548
TT
614 info->max_lineno_offset - info->min_lineno_offset,
615 &linetab_storage);
ebeb39fe 616 if (val < 0)
b926417a 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 {
efd66ac6 644 const char *name = MSYMBOL_LINKAGE_NAME (msym);
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. */
20d35291 650 if (MSYMBOL_TYPE (msym) == 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
663 if (lead != '\0' && *name1 == lead)
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
672 && MSYMBOL_TYPE (found.minsym) == mst_text)
673 MSYMBOL_TYPE (found.minsym) = mst_solib_trampoline;
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
c5aa993b 692 bfd_get_section_contents? */
c906108c
SS
693 bfd_seek (abfd, abfd->where, 0);
694
695 stabstrsize = bfd_section_size (abfd, info->stabstrsect);
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. */
f29dff0a 705 dwarf2_build_psymtabs (objfile);
42a076f0 706 }
c906108c 707
fea25152
BF
708 dwarf2_build_frame_info (objfile);
709
9cce227f
TG
710 /* Try to add separate debug file if no symbols table found. */
711 if (!objfile_has_partial_symbols (objfile))
712 {
a8dbfd58 713 std::string debugfile = find_separate_debug_file_by_buildid (objfile);
9cce227f 714
a8dbfd58 715 if (debugfile.empty ())
c74f7d1c 716 debugfile = find_separate_debug_file_by_debuglink (objfile);
9cce227f 717
a8dbfd58 718 if (!debugfile.empty ())
9cce227f 719 {
b926417a 720 gdb_bfd_ref_ptr debug_bfd (symfile_bfd_open (debugfile.c_str ()));
c5504eaf 721
b926417a 722 symbol_file_add_separate (debug_bfd.get (), debugfile.c_str (),
a8dbfd58 723 symfile_flags, objfile);
9cce227f
TG
724 }
725 }
c906108c
SS
726}
727
728static void
fba45db2 729coff_new_init (struct objfile *ignore)
c906108c
SS
730{
731}
732
aff410f1
MS
733/* Perform any local cleanups required when we are done with a
734 particular objfile. I.E, we are in the process of discarding all
735 symbol information for an objfile, freeing up all memory held for
736 it, and unlinking the objfile struct from the global list of known
737 objfiles. */
c906108c
SS
738
739static void
fba45db2 740coff_symfile_finish (struct objfile *objfile)
c906108c 741{
aff410f1 742 /* Let stabs reader clean up. */
7be570e7 743 stabsread_clear_cache ();
c906108c 744}
c906108c 745\f
c5aa993b 746
c906108c
SS
747/* Given pointers to a symbol table in coff style exec file,
748 analyze them and create struct symtab's describing the symbols.
749 NSYMS is the number of symbols in the symbol table.
750 We read them one at a time using read_one_sym (). */
751
752static void
8dddcb8f
TT
753coff_symtab_read (minimal_symbol_reader &reader,
754 long symtab_offset, unsigned int nsyms,
fba45db2 755 struct objfile *objfile)
c906108c 756{
5e2b427d 757 struct gdbarch *gdbarch = get_objfile_arch (objfile);
875e5398 758 struct context_stack *newobj = nullptr;
c906108c 759 struct coff_symbol coff_symbol;
52f0bd74 760 struct coff_symbol *cs = &coff_symbol;
c906108c
SS
761 static struct internal_syment main_sym;
762 static union internal_auxent main_aux;
763 struct coff_symbol fcn_cs_saved;
764 static struct internal_syment fcn_sym_saved;
765 static union internal_auxent fcn_aux_saved;
c906108c
SS
766 /* A .file is open. */
767 int in_source_file = 0;
768 int next_file_symnum = -1;
769 /* Name of the current file. */
9f37bbcc 770 const char *filestring = "";
c906108c
SS
771 int depth = 0;
772 int fcn_first_line = 0;
b9179dbc 773 CORE_ADDR fcn_first_line_addr = 0;
c906108c
SS
774 int fcn_last_line = 0;
775 int fcn_start_addr = 0;
776 long fcn_line_ptr = 0;
777 int val;
778 CORE_ADDR tmpaddr;
05cfdb42 779 struct minimal_symbol *msym;
c906108c 780
4735f0ed
TT
781 scoped_free_pendings free_pending;
782
c906108c 783 /* Work around a stdio bug in SunOS4.1.1 (this makes me nervous....
aff410f1
MS
784 it's hard to know I've really worked around it. The fix should
785 be harmless, anyway). The symptom of the bug is that the first
c906108c
SS
786 fread (in read_one_sym), will (in my example) actually get data
787 from file offset 268, when the fseek was to 264 (and ftell shows
788 264). This causes all hell to break loose. I was unable to
789 reproduce this on a short test program which operated on the same
790 file, performing (I think) the same sequence of operations.
791
792 It stopped happening when I put in this (former) rewind().
793
794 FIXME: Find out if this has been reported to Sun, whether it has
795 been fixed in a later release, etc. */
796
797 bfd_seek (objfile->obfd, 0, 0);
798
aff410f1 799 /* Position to read the symbol table. */
c906108c
SS
800 val = bfd_seek (objfile->obfd, (long) symtab_offset, 0);
801 if (val < 0)
4262abfb 802 perror_with_name (objfile_name (objfile));
c906108c 803
dd707e8e 804 coffread_objfile = objfile;
c906108c
SS
805 nlist_bfd_global = objfile->obfd;
806 nlist_nsyms_global = nsyms;
46212e0b 807 set_last_source_file (NULL);
c906108c
SS
808 memset (opaque_type_chain, 0, sizeof opaque_type_chain);
809
aff410f1 810 if (type_vector) /* Get rid of previous one. */
b8c9b27d 811 xfree (type_vector);
fc474241 812 type_vector_length = INITIAL_TYPE_VECTOR_LENGTH;
8d749320 813 type_vector = XCNEWVEC (struct type *, type_vector_length);
c906108c 814
4d663531 815 coff_start_symtab (objfile, "");
c906108c
SS
816
817 symnum = 0;
818 while (symnum < nsyms)
819 {
820 QUIT; /* Make this command interruptable. */
821
822 read_one_sym (cs, &main_sym, &main_aux);
823
824 if (cs->c_symnum == next_file_symnum && cs->c_sclass != C_FILE)
825 {
46212e0b 826 if (get_last_source_file ())
c906108c
SS
827 coff_end_symtab (objfile);
828
4d663531 829 coff_start_symtab (objfile, "_globals_");
969107c5
EZ
830 /* coff_start_symtab will set the language of this symtab to
831 language_unknown, since such a ``file name'' is not
832 recognized. Override that with the minimal language to
833 allow printing values in this symtab. */
3c65e5b3 834 get_current_subfile ()->language = language_minimal;
c906108c 835 complete_symtab ("_globals_", 0, 0);
aff410f1
MS
836 /* Done with all files, everything from here on out is
837 globals. */
c906108c
SS
838 }
839
aff410f1
MS
840 /* Special case for file with type declarations only, no
841 text. */
46212e0b 842 if (!get_last_source_file () && SDB_TYPE (cs->c_type)
c906108c
SS
843 && cs->c_secnum == N_DEBUG)
844 complete_symtab (filestring, 0, 0);
845
846 /* Typedefs should not be treated as symbol definitions. */
847 if (ISFCN (cs->c_type) && cs->c_sclass != C_TPDEF)
848 {
aff410f1
MS
849 /* Record all functions -- external and static -- in
850 minsyms. */
fbcebcb1 851 int section = cs_to_section (cs, objfile);
c5504eaf 852
2273f0ac 853 tmpaddr = cs->c_value;
156f2366
EZ
854 /* Don't record unresolved symbols. */
855 if (!(cs->c_secnum <= 0 && cs->c_value == 0))
856 record_minimal_symbol (reader, cs, tmpaddr, mst_text,
857 section, objfile);
c906108c
SS
858
859 fcn_line_ptr = main_aux.x_sym.x_fcnary.x_fcn.x_lnnoptr;
860 fcn_start_addr = tmpaddr;
861 fcn_cs_saved = *cs;
862 fcn_sym_saved = main_sym;
863 fcn_aux_saved = main_aux;
864 continue;
865 }
866
867 switch (cs->c_sclass)
868 {
c5aa993b
JM
869 case C_EFCN:
870 case C_EXTDEF:
871 case C_ULABEL:
872 case C_USTATIC:
873 case C_LINE:
874 case C_ALIAS:
875 case C_HIDDEN:
b98664d3 876 complaint (_("Bad n_sclass for symbol %s"),
23136709 877 cs->c_name);
c5aa993b 878 break;
c906108c 879
c5aa993b 880 case C_FILE:
aff410f1
MS
881 /* c_value field contains symnum of next .file entry in
882 table or symnum of first global after last .file. */
c5aa993b
JM
883 next_file_symnum = cs->c_value;
884 if (cs->c_naux > 0)
885 filestring = coff_getfilename (&main_aux);
886 else
887 filestring = "";
888
889 /* Complete symbol table for last object file
890 containing debugging information. */
46212e0b 891 if (get_last_source_file ())
c5aa993b
JM
892 {
893 coff_end_symtab (objfile);
4d663531 894 coff_start_symtab (objfile, filestring);
c5aa993b
JM
895 }
896 in_source_file = 1;
897 break;
c906108c 898
aff410f1
MS
899 /* C_LABEL is used for labels and static functions.
900 Including it here allows gdb to see static functions when
901 no debug info is available. */
c5aa993b 902 case C_LABEL:
aff410f1
MS
903 /* However, labels within a function can make weird
904 backtraces, so filter them out (from phdm@macqel.be). */
c5aa993b
JM
905 if (within_function)
906 break;
565e0eda 907 /* Fall through. */
c5aa993b
JM
908 case C_STAT:
909 case C_THUMBLABEL:
910 case C_THUMBSTAT:
911 case C_THUMBSTATFUNC:
912 if (cs->c_name[0] == '.')
913 {
7ecb6532 914 if (strcmp (cs->c_name, ".text") == 0)
c5aa993b 915 {
aff410f1
MS
916 /* FIXME: don't wire in ".text" as section name or
917 symbol name! */
918 /* Check for in_source_file deals with case of a
919 file with debugging symbols followed by a later
920 file with no symbols. */
c906108c
SS
921 if (in_source_file)
922 complete_symtab (filestring,
aff410f1
MS
923 cs->c_value + ANOFFSET (objfile->section_offsets,
924 SECT_OFF_TEXT (objfile)),
c906108c
SS
925 main_aux.x_scn.x_scnlen);
926 in_source_file = 0;
927 }
aff410f1 928 /* Flush rest of '.' symbols. */
c906108c 929 break;
c5aa993b
JM
930 }
931 else if (!SDB_TYPE (cs->c_type)
932 && cs->c_name[0] == 'L'
61012eef
GB
933 && (startswith (cs->c_name, "LI%")
934 || startswith (cs->c_name, "LF%")
935 || startswith (cs->c_name, "LC%")
936 || startswith (cs->c_name, "LP%")
937 || startswith (cs->c_name, "LPB%")
938 || startswith (cs->c_name, "LBB%")
939 || startswith (cs->c_name, "LBE%")
940 || startswith (cs->c_name, "LPBX%")))
c5aa993b
JM
941 /* At least on a 3b1, gcc generates swbeg and string labels
942 that look like this. Ignore them. */
943 break;
86a73007
TT
944 /* For static symbols that don't start with '.'... */
945 /* Fall through. */
c5aa993b
JM
946 case C_THUMBEXT:
947 case C_THUMBEXTFUNC:
948 case C_EXT:
949 {
950 /* Record it in the minimal symbols regardless of
951 SDB_TYPE. This parallels what we do for other debug
952 formats, and probably is needed to make
953 print_address_symbolic work right without the (now
954 gone) "set fast-symbolic-addr off" kludge. */
c906108c 955
c5aa993b
JM
956 enum minimal_symbol_type ms_type;
957 int sec;
2273f0ac 958 CORE_ADDR offset = 0;
c906108c 959
c5aa993b
JM
960 if (cs->c_secnum == N_UNDEF)
961 {
d4862372
JB
962 /* This is a common symbol. We used to rely on
963 the target to tell us whether it knows where
964 the symbol has been relocated to, but none of
965 the target implementations actually provided
966 that operation. So we just ignore the symbol,
967 the same way we would do if we had a target-side
968 symbol lookup which returned no match. */
969 break;
c5aa993b 970 }
182d43bc
EZ
971 else if (cs->c_secnum == N_ABS)
972 {
973 /* Use the correct minimal symbol type (and don't
aff410f1 974 relocate) for absolute values. */
182d43bc
EZ
975 ms_type = mst_abs;
976 sec = cs_to_section (cs, objfile);
977 tmpaddr = cs->c_value;
978 }
c5aa993b
JM
979 else
980 {
05cfdb42 981 asection *bfd_section = cs_to_bfd_section (cs, objfile);
c5504eaf 982
c5aa993b
JM
983 sec = cs_to_section (cs, objfile);
984 tmpaddr = cs->c_value;
aff410f1 985 /* Statics in a PE file also get relocated. */
182d43bc
EZ
986 if (cs->c_sclass == C_EXT
987 || cs->c_sclass == C_THUMBEXTFUNC
988 || cs->c_sclass == C_THUMBEXT
989 || (pe_file && (cs->c_sclass == C_STAT)))
2273f0ac 990 offset = ANOFFSET (objfile->section_offsets, sec);
c906108c 991
05cfdb42 992 if (bfd_section->flags & SEC_CODE)
c5aa993b 993 {
c5aa993b
JM
994 ms_type =
995 cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXTFUNC
996 || cs->c_sclass == C_THUMBEXT ?
997 mst_text : mst_file_text;
85ddcc70 998 tmpaddr = gdbarch_addr_bits_remove (gdbarch, tmpaddr);
b8fbeb18 999 }
05cfdb42
DJ
1000 else if (bfd_section->flags & SEC_ALLOC
1001 && bfd_section->flags & SEC_LOAD)
34e924c0 1002 {
c5aa993b 1003 ms_type =
aff410f1
MS
1004 cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXT
1005 ? mst_data : mst_file_data;
34e924c0 1006 }
05cfdb42 1007 else if (bfd_section->flags & SEC_ALLOC)
34e924c0 1008 {
c5aa993b 1009 ms_type =
aff410f1
MS
1010 cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXT
1011 ? mst_bss : mst_file_bss;
34e924c0
EZ
1012 }
1013 else
1014 ms_type = mst_unknown;
c5aa993b 1015 }
c906108c 1016
8dddcb8f 1017 msym = record_minimal_symbol (reader, cs, tmpaddr, ms_type,
aff410f1 1018 sec, objfile);
05cfdb42 1019 if (msym)
aff410f1
MS
1020 gdbarch_coff_make_msymbol_special (gdbarch,
1021 cs->c_sclass, msym);
fbcebcb1 1022
c5aa993b
JM
1023 if (SDB_TYPE (cs->c_type))
1024 {
1025 struct symbol *sym;
c5504eaf 1026
c5aa993b 1027 sym = process_coff_symbol
96baa820 1028 (cs, &main_aux, objfile);
2273f0ac 1029 SYMBOL_VALUE (sym) = tmpaddr + offset;
c5aa993b
JM
1030 SYMBOL_SECTION (sym) = sec;
1031 }
1032 }
1033 break;
1034
1035 case C_FCN:
7ecb6532 1036 if (strcmp (cs->c_name, ".bf") == 0)
c5aa993b
JM
1037 {
1038 within_function = 1;
1039
aff410f1
MS
1040 /* Value contains address of first non-init type
1041 code. */
c5aa993b 1042 /* main_aux.x_sym.x_misc.x_lnsz.x_lnno
aff410f1 1043 contains line number of '{' }. */
c5aa993b 1044 if (cs->c_naux != 1)
b98664d3 1045 complaint (_("`.bf' symbol %d has no aux entry"),
aff410f1 1046 cs->c_symnum);
c5aa993b
JM
1047 fcn_first_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
1048 fcn_first_line_addr = cs->c_value;
1049
1050 /* Might want to check that locals are 0 and
1051 context_stack_depth is zero, and complain if not. */
1052
1053 depth = 0;
fe978cb0 1054 newobj = push_context (depth, fcn_start_addr);
c5aa993b 1055 fcn_cs_saved.c_name = getsymname (&fcn_sym_saved);
fe978cb0 1056 newobj->name =
aff410f1
MS
1057 process_coff_symbol (&fcn_cs_saved,
1058 &fcn_aux_saved, objfile);
c5aa993b 1059 }
7ecb6532 1060 else if (strcmp (cs->c_name, ".ef") == 0)
c5aa993b 1061 {
b9179dbc 1062 if (!within_function)
8a3fe4f8 1063 error (_("Bad coff function information."));
aff410f1 1064 /* The value of .ef is the address of epilogue code;
c5aa993b
JM
1065 not useful for gdb. */
1066 /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
1067 contains number of lines to '}' */
1068
edb0470b 1069 if (outermost_context_p ())
aff410f1 1070 { /* We attempted to pop an empty context stack. */
b98664d3 1071 complaint (_("`.ef' symbol without matching `.bf' "
3e43a32a 1072 "symbol ignored starting at symnum %d"),
23136709 1073 cs->c_symnum);
c5aa993b
JM
1074 within_function = 0;
1075 break;
c906108c 1076 }
c5aa993b 1077
a60f3166 1078 struct context_stack cstk = pop_context ();
c5aa993b 1079 /* Stack must be empty now. */
edb0470b 1080 if (!outermost_context_p () || newobj == NULL)
c906108c 1081 {
b98664d3 1082 complaint (_("Unmatched .ef symbol(s) ignored "
3e43a32a 1083 "starting at symnum %d"),
23136709 1084 cs->c_symnum);
c5aa993b
JM
1085 within_function = 0;
1086 break;
c906108c 1087 }
c5aa993b
JM
1088 if (cs->c_naux != 1)
1089 {
b98664d3 1090 complaint (_("`.ef' symbol %d has no aux entry"),
aff410f1 1091 cs->c_symnum);
c5aa993b
JM
1092 fcn_last_line = 0x7FFFFFFF;
1093 }
1094 else
1095 {
1096 fcn_last_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
1097 }
1098 /* fcn_first_line is the line number of the opening '{'.
1099 Do not record it - because it would affect gdb's idea
aff410f1
MS
1100 of the line number of the first statement of the
1101 function - except for one-line functions, for which
1102 it is also the line number of all the statements and
1103 of the closing '}', and for which we do not have any
1104 other statement-line-number. */
c5aa993b 1105 if (fcn_last_line == 1)
3c65e5b3 1106 record_line (get_current_subfile (), fcn_first_line,
fbf65064
UW
1107 gdbarch_addr_bits_remove (gdbarch,
1108 fcn_first_line_addr));
c5aa993b 1109 else
aff410f1
MS
1110 enter_linenos (fcn_line_ptr, fcn_first_line,
1111 fcn_last_line, objfile);
c906108c 1112
c233e9c6 1113 finish_block (cstk.name, cstk.old_blocks,
a60f3166 1114 NULL, cstk.start_addr,
c5aa993b
JM
1115 fcn_cs_saved.c_value
1116 + fcn_aux_saved.x_sym.x_misc.x_fsize
aff410f1 1117 + ANOFFSET (objfile->section_offsets,
4d663531 1118 SECT_OFF_TEXT (objfile)));
c5aa993b
JM
1119 within_function = 0;
1120 }
1121 break;
c906108c 1122
c5aa993b 1123 case C_BLOCK:
7ecb6532 1124 if (strcmp (cs->c_name, ".bb") == 0)
c5aa993b
JM
1125 {
1126 tmpaddr = cs->c_value;
aff410f1
MS
1127 tmpaddr += ANOFFSET (objfile->section_offsets,
1128 SECT_OFF_TEXT (objfile));
c5aa993b
JM
1129 push_context (++depth, tmpaddr);
1130 }
7ecb6532 1131 else if (strcmp (cs->c_name, ".eb") == 0)
c5aa993b 1132 {
edb0470b 1133 if (outermost_context_p ())
0963b4bd 1134 { /* We attempted to pop an empty context stack. */
b98664d3 1135 complaint (_("`.eb' symbol without matching `.bb' "
3e43a32a 1136 "symbol ignored starting at symnum %d"),
23136709 1137 cs->c_symnum);
c5aa993b
JM
1138 break;
1139 }
c906108c 1140
a60f3166
TT
1141 struct context_stack cstk = pop_context ();
1142 if (depth-- != cstk.depth)
c5aa993b 1143 {
b98664d3 1144 complaint (_("Mismatched .eb symbol ignored "
3e43a32a 1145 "starting at symnum %d"),
23136709 1146 symnum);
c5aa993b
JM
1147 break;
1148 }
e148f09d 1149 if (*get_local_symbols () && !outermost_context_p ())
c5aa993b
JM
1150 {
1151 tmpaddr =
aff410f1
MS
1152 cs->c_value + ANOFFSET (objfile->section_offsets,
1153 SECT_OFF_TEXT (objfile));
c5aa993b 1154 /* Make a block for the local symbols within. */
c233e9c6 1155 finish_block (0, cstk.old_blocks, NULL,
a60f3166 1156 cstk.start_addr, tmpaddr);
c5aa993b
JM
1157 }
1158 /* Now pop locals of block just finished. */
e148f09d 1159 *get_local_symbols () = cstk.locals;
c5aa993b
JM
1160 }
1161 break;
c906108c 1162
c5aa993b 1163 default:
96baa820 1164 process_coff_symbol (cs, &main_aux, objfile);
c5aa993b 1165 break;
c906108c
SS
1166 }
1167 }
1168
1b6bc7e0
CF
1169 if ((nsyms == 0) && (pe_file))
1170 {
c2f20dd6 1171 /* We've got no debugging symbols, but it's a portable
aff410f1 1172 executable, so try to read the export table. */
8dddcb8f 1173 read_pe_exported_syms (reader, objfile);
1b6bc7e0
CF
1174 }
1175
46212e0b 1176 if (get_last_source_file ())
c906108c
SS
1177 coff_end_symtab (objfile);
1178
1179 /* Patch up any opaque types (references to types that are not defined
1180 in the file where they are referenced, e.g. "struct foo *bar"). */
43f3e411 1181 {
b669c953 1182 for (compunit_symtab *cu : objfile->compunits ())
d5da8b3c
TT
1183 {
1184 for (symtab *s : compunit_filetabs (cu))
1185 patch_opaque_types (s);
1186 }
43f3e411 1187 }
c906108c 1188
dd707e8e 1189 coffread_objfile = NULL;
c906108c
SS
1190}
1191\f
1192/* Routines for reading headers and symbols from executable. */
1193
aff410f1
MS
1194/* Read the next symbol, swap it, and return it in both
1195 internal_syment form, and coff_symbol form. Also return its first
1196 auxent, if any, in internal_auxent form, and skip any other
1197 auxents. */
c906108c
SS
1198
1199static void
aa1ee363
AC
1200read_one_sym (struct coff_symbol *cs,
1201 struct internal_syment *sym,
1202 union internal_auxent *aux)
c906108c
SS
1203{
1204 int i;
3b016d57 1205 bfd_size_type bytes;
c906108c
SS
1206
1207 cs->c_symnum = symnum;
3b016d57
DJ
1208 bytes = bfd_bread (temp_sym, local_symesz, nlist_bfd_global);
1209 if (bytes != local_symesz)
4262abfb 1210 error (_("%s: error reading symbols"), objfile_name (coffread_objfile));
c5aa993b 1211 bfd_coff_swap_sym_in (symfile_bfd, temp_sym, (char *) sym);
c906108c
SS
1212 cs->c_naux = sym->n_numaux & 0xff;
1213 if (cs->c_naux >= 1)
1214 {
3b016d57
DJ
1215 bytes = bfd_bread (temp_aux, local_auxesz, nlist_bfd_global);
1216 if (bytes != local_auxesz)
4262abfb 1217 error (_("%s: error reading symbols"), objfile_name (coffread_objfile));
aff410f1
MS
1218 bfd_coff_swap_aux_in (symfile_bfd, temp_aux,
1219 sym->n_type, sym->n_sclass,
c5aa993b
JM
1220 0, cs->c_naux, (char *) aux);
1221 /* If more than one aux entry, read past it (only the first aux
aff410f1 1222 is important). */
c5aa993b 1223 for (i = 1; i < cs->c_naux; i++)
3b016d57
DJ
1224 {
1225 bytes = bfd_bread (temp_aux, local_auxesz, nlist_bfd_global);
1226 if (bytes != local_auxesz)
4262abfb
JK
1227 error (_("%s: error reading symbols"),
1228 objfile_name (coffread_objfile));
3b016d57 1229 }
c906108c
SS
1230 }
1231 cs->c_name = getsymname (sym);
1232 cs->c_value = sym->n_value;
1233 cs->c_sclass = (sym->n_sclass & 0xff);
1234 cs->c_secnum = sym->n_scnum;
1235 cs->c_type = (unsigned) sym->n_type;
1236 if (!SDB_TYPE (cs->c_type))
1237 cs->c_type = 0;
1238
1239#if 0
1240 if (cs->c_sclass & 128)
3d263c1d 1241 printf (_("thumb symbol %s, class 0x%x\n"), cs->c_name, cs->c_sclass);
c906108c
SS
1242#endif
1243
1244 symnum += 1 + cs->c_naux;
1245
1246 /* The PE file format stores symbol values as offsets within the
1247 section, rather than as absolute addresses. We correct that
1248 here, if the symbol has an appropriate storage class. FIXME: We
1249 should use BFD to read the symbols, rather than duplicating the
1250 work here. */
1251 if (pe_file)
1252 {
1253 switch (cs->c_sclass)
1254 {
1255 case C_EXT:
1256 case C_THUMBEXT:
1257 case C_THUMBEXTFUNC:
1258 case C_SECTION:
1259 case C_NT_WEAK:
1260 case C_STAT:
1261 case C_THUMBSTAT:
1262 case C_THUMBSTATFUNC:
1263 case C_LABEL:
1264 case C_THUMBLABEL:
1265 case C_BLOCK:
1266 case C_FCN:
1267 case C_EFCN:
1268 if (cs->c_secnum != 0)
1269 cs->c_value += cs_section_address (cs, symfile_bfd);
1270 break;
1271 }
1272 }
1273}
1274\f
aff410f1 1275/* Support for string table handling. */
c906108c 1276
c906108c 1277static int
e2a03548 1278init_stringtab (bfd *abfd, long offset, gdb::unique_xmalloc_ptr<char> *storage)
c906108c
SS
1279{
1280 long length;
1281 int val;
1282 unsigned char lengthbuf[4];
1283
c906108c 1284 /* If the file is stripped, the offset might be zero, indicating no
aff410f1 1285 string table. Just return with `stringtab' set to null. */
c906108c
SS
1286 if (offset == 0)
1287 return 0;
1288
1289 if (bfd_seek (abfd, offset, 0) < 0)
1290 return -1;
1291
3a42e9d0 1292 val = bfd_bread ((char *) lengthbuf, sizeof lengthbuf, abfd);
c906108c 1293 length = bfd_h_get_32 (symfile_bfd, lengthbuf);
c5aa993b 1294
c906108c 1295 /* If no string table is needed, then the file may end immediately
aff410f1 1296 after the symbols. Just return with `stringtab' set to null. */
c906108c
SS
1297 if (val != sizeof lengthbuf || length < sizeof lengthbuf)
1298 return 0;
1299
e2a03548
TT
1300 storage->reset ((char *) xmalloc (length));
1301 stringtab = storage->get ();
aff410f1
MS
1302 /* This is in target format (probably not very useful, and not
1303 currently used), not host format. */
c906108c 1304 memcpy (stringtab, lengthbuf, sizeof lengthbuf);
aff410f1 1305 if (length == sizeof length) /* Empty table -- just the count. */
c906108c
SS
1306 return 0;
1307
aff410f1
MS
1308 val = bfd_bread (stringtab + sizeof lengthbuf,
1309 length - sizeof lengthbuf, abfd);
c906108c
SS
1310 if (val != length - sizeof lengthbuf || stringtab[length - 1] != '\0')
1311 return -1;
1312
1313 return 0;
1314}
1315
c906108c 1316static char *
fba45db2 1317getsymname (struct internal_syment *symbol_entry)
c906108c 1318{
c5aa993b 1319 static char buffer[SYMNMLEN + 1];
c906108c
SS
1320 char *result;
1321
1322 if (symbol_entry->_n._n_n._n_zeroes == 0)
1323 {
1324 /* FIXME: Probably should be detecting corrupt symbol files by
c5aa993b 1325 seeing whether offset points to within the stringtab. */
c906108c
SS
1326 result = stringtab + symbol_entry->_n._n_n._n_offset;
1327 }
1328 else
1329 {
1330 strncpy (buffer, symbol_entry->_n._n_name, SYMNMLEN);
1331 buffer[SYMNMLEN] = '\0';
1332 result = buffer;
1333 }
1334 return result;
1335}
1336
aff410f1
MS
1337/* Extract the file name from the aux entry of a C_FILE symbol.
1338 Return only the last component of the name. Result is in static
1339 storage and is only good for temporary use. */
c906108c 1340
9f37bbcc 1341static const char *
fba45db2 1342coff_getfilename (union internal_auxent *aux_entry)
c906108c
SS
1343{
1344 static char buffer[BUFSIZ];
9f37bbcc 1345 const char *result;
c906108c
SS
1346
1347 if (aux_entry->x_file.x_n.x_zeroes == 0)
9e91a352
MS
1348 {
1349 if (strlen (stringtab + aux_entry->x_file.x_n.x_offset) >= BUFSIZ)
1350 internal_error (__FILE__, __LINE__, _("coff file name too long"));
1351 strcpy (buffer, stringtab + aux_entry->x_file.x_n.x_offset);
1352 }
c906108c
SS
1353 else
1354 {
1355 strncpy (buffer, aux_entry->x_file.x_fname, FILNMLEN);
1356 buffer[FILNMLEN] = '\0';
1357 }
1358 result = buffer;
1359
1360 /* FIXME: We should not be throwing away the information about what
1361 directory. It should go into dirname of the symtab, or some such
1362 place. */
9f37bbcc 1363 result = lbasename (result);
c906108c
SS
1364 return (result);
1365}
1366\f
1367/* Support for line number handling. */
1368
c906108c
SS
1369/* Read in all the line numbers for fast lookups later. Leave them in
1370 external (unswapped) format in memory; we'll swap them as we enter
1371 them into GDB's data structures. */
c5aa993b 1372
c906108c 1373static int
e2a03548
TT
1374init_lineno (bfd *abfd, long offset, int size,
1375 gdb::unique_xmalloc_ptr<char> *storage)
c906108c
SS
1376{
1377 int val;
1378
1379 linetab_offset = offset;
1380 linetab_size = size;
1381
c906108c
SS
1382 if (size == 0)
1383 return 0;
1384
1385 if (bfd_seek (abfd, offset, 0) < 0)
1386 return -1;
c5aa993b 1387
aff410f1 1388 /* Allocate the desired table, plus a sentinel. */
e2a03548
TT
1389 storage->reset ((char *) xmalloc (size + local_linesz));
1390 linetab = storage->get ();
c906108c 1391
e2a03548 1392 val = bfd_bread (storage->get (), size, abfd);
c906108c
SS
1393 if (val != size)
1394 return -1;
1395
aff410f1 1396 /* Terminate it with an all-zero sentinel record. */
c906108c
SS
1397 memset (linetab + size, 0, local_linesz);
1398
1399 return 0;
1400}
1401
c906108c
SS
1402#if !defined (L_LNNO32)
1403#define L_LNNO32(lp) ((lp)->l_lnno)
1404#endif
1405
1406static void
aa1ee363
AC
1407enter_linenos (long file_offset, int first_line,
1408 int last_line, struct objfile *objfile)
c906108c 1409{
fbf65064 1410 struct gdbarch *gdbarch = get_objfile_arch (objfile);
52f0bd74 1411 char *rawptr;
c906108c
SS
1412 struct internal_lineno lptr;
1413
1414 if (!linetab)
c5aa993b 1415 return;
c906108c
SS
1416 if (file_offset < linetab_offset)
1417 {
b98664d3 1418 complaint (_("Line number pointer %ld lower than start of line numbers"),
23136709 1419 file_offset);
aff410f1 1420 if (file_offset > linetab_size) /* Too big to be an offset? */
c906108c 1421 return;
aff410f1
MS
1422 file_offset += linetab_offset; /* Try reading at that linetab
1423 offset. */
c906108c 1424 }
c5aa993b 1425
c906108c
SS
1426 rawptr = &linetab[file_offset - linetab_offset];
1427
aff410f1 1428 /* Skip first line entry for each function. */
c906108c 1429 rawptr += local_linesz;
aff410f1 1430 /* Line numbers start at one for the first line of the function. */
c906108c
SS
1431 first_line--;
1432
e6a8a7d2
EZ
1433 /* If the line number table is full (e.g. 64K lines in COFF debug
1434 info), the next function's L_LNNO32 might not be zero, so don't
1435 overstep the table's end in any case. */
1436 while (rawptr <= &linetab[0] + linetab_size)
c5aa993b
JM
1437 {
1438 bfd_coff_swap_lineno_in (symfile_bfd, rawptr, &lptr);
1439 rawptr += local_linesz;
e6a8a7d2 1440 /* The next function, or the sentinel, will have L_LNNO32 zero;
aff410f1 1441 we exit. */
c5aa993b 1442 if (L_LNNO32 (&lptr) && L_LNNO32 (&lptr) <= last_line)
fbf65064
UW
1443 {
1444 CORE_ADDR addr = lptr.l_addr.l_paddr;
aff410f1
MS
1445 addr += ANOFFSET (objfile->section_offsets,
1446 SECT_OFF_TEXT (objfile));
3c65e5b3 1447 record_line (get_current_subfile (),
aff410f1 1448 first_line + L_LNNO32 (&lptr),
fbf65064
UW
1449 gdbarch_addr_bits_remove (gdbarch, addr));
1450 }
c5aa993b
JM
1451 else
1452 break;
1453 }
c906108c
SS
1454}
1455\f
1456static void
fba45db2 1457patch_type (struct type *type, struct type *real_type)
c906108c 1458{
52f0bd74
AC
1459 struct type *target = TYPE_TARGET_TYPE (type);
1460 struct type *real_target = TYPE_TARGET_TYPE (real_type);
c906108c
SS
1461 int field_size = TYPE_NFIELDS (real_target) * sizeof (struct field);
1462
1463 TYPE_LENGTH (target) = TYPE_LENGTH (real_target);
1464 TYPE_NFIELDS (target) = TYPE_NFIELDS (real_target);
aff410f1
MS
1465 TYPE_FIELDS (target) = (struct field *) TYPE_ALLOC (target,
1466 field_size);
c906108c 1467
aff410f1
MS
1468 memcpy (TYPE_FIELDS (target),
1469 TYPE_FIELDS (real_target),
1470 field_size);
c906108c
SS
1471
1472 if (TYPE_NAME (real_target))
1473 {
0d5cff50
DE
1474 /* The previous copy of TYPE_NAME is allocated by
1475 process_coff_symbol. */
c906108c 1476 if (TYPE_NAME (target))
0d5cff50
DE
1477 xfree ((char*) TYPE_NAME (target));
1478 TYPE_NAME (target) = xstrdup (TYPE_NAME (real_target));
c906108c
SS
1479 }
1480}
1481
1482/* Patch up all appropriate typedef symbols in the opaque_type_chains
aff410f1
MS
1483 so that they can be used to print out opaque data structures
1484 properly. */
c906108c
SS
1485
1486static void
fba45db2 1487patch_opaque_types (struct symtab *s)
c906108c 1488{
582942f4 1489 const struct block *b;
8157b174 1490 struct block_iterator iter;
52f0bd74 1491 struct symbol *real_sym;
c5aa993b 1492
aff410f1 1493 /* Go through the per-file symbols only. */
439247b6 1494 b = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (s), STATIC_BLOCK);
de4f826b 1495 ALL_BLOCK_SYMBOLS (b, iter, real_sym)
c906108c
SS
1496 {
1497 /* Find completed typedefs to use to fix opaque ones.
c5aa993b
JM
1498 Remove syms from the chain when their types are stored,
1499 but search the whole chain, as there may be several syms
1500 from different files with the same name. */
5aafa1cc
PM
1501 if (SYMBOL_CLASS (real_sym) == LOC_TYPEDEF
1502 && SYMBOL_DOMAIN (real_sym) == VAR_DOMAIN
1503 && TYPE_CODE (SYMBOL_TYPE (real_sym)) == TYPE_CODE_PTR
1504 && TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (real_sym))) != 0)
c906108c 1505 {
0d5cff50 1506 const char *name = SYMBOL_LINKAGE_NAME (real_sym);
aa1ee363
AC
1507 int hash = hashname (name);
1508 struct symbol *sym, *prev;
c5aa993b 1509
c906108c
SS
1510 prev = 0;
1511 for (sym = opaque_type_chain[hash]; sym;)
1512 {
5aafa1cc
PM
1513 if (name[0] == SYMBOL_LINKAGE_NAME (sym)[0]
1514 && strcmp (name + 1, SYMBOL_LINKAGE_NAME (sym) + 1) == 0)
c906108c
SS
1515 {
1516 if (prev)
1517 {
1518 SYMBOL_VALUE_CHAIN (prev) = SYMBOL_VALUE_CHAIN (sym);
1519 }
1520 else
1521 {
1522 opaque_type_chain[hash] = SYMBOL_VALUE_CHAIN (sym);
1523 }
c5aa993b 1524
c906108c 1525 patch_type (SYMBOL_TYPE (sym), SYMBOL_TYPE (real_sym));
c5aa993b 1526
c906108c
SS
1527 if (prev)
1528 {
1529 sym = SYMBOL_VALUE_CHAIN (prev);
1530 }
1531 else
1532 {
1533 sym = opaque_type_chain[hash];
1534 }
1535 }
1536 else
1537 {
1538 prev = sym;
1539 sym = SYMBOL_VALUE_CHAIN (sym);
1540 }
1541 }
1542 }
1543 }
1544}
1545\f
768a979c
UW
1546static int
1547coff_reg_to_regnum (struct symbol *sym, struct gdbarch *gdbarch)
1548{
1549 return gdbarch_sdb_reg_to_regnum (gdbarch, SYMBOL_VALUE (sym));
1550}
1551
1552static const struct symbol_register_ops coff_register_funcs = {
1553 coff_reg_to_regnum
1554};
1555
f1e6e072
TT
1556/* The "aclass" index for computed COFF symbols. */
1557
1558static int coff_register_index;
1559
c906108c 1560static struct symbol *
aa1ee363
AC
1561process_coff_symbol (struct coff_symbol *cs,
1562 union internal_auxent *aux,
fba45db2 1563 struct objfile *objfile)
c906108c 1564{
e623cf5d 1565 struct symbol *sym = allocate_symbol (objfile);
c906108c
SS
1566 char *name;
1567
c906108c
SS
1568 name = cs->c_name;
1569 name = EXTERNAL_NAME (name, objfile->obfd);
3c65e5b3 1570 SYMBOL_SET_LANGUAGE (sym, get_current_subfile ()->language,
f85f34ed 1571 &objfile->objfile_obstack);
04a679b8 1572 SYMBOL_SET_NAMES (sym, name, strlen (name), 1, objfile);
c906108c
SS
1573
1574 /* default assumptions */
1575 SYMBOL_VALUE (sym) = cs->c_value;
176620f1 1576 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
c906108c
SS
1577 SYMBOL_SECTION (sym) = cs_to_section (cs, objfile);
1578
1579 if (ISFCN (cs->c_type))
1580 {
aff410f1
MS
1581 SYMBOL_VALUE (sym) += ANOFFSET (objfile->section_offsets,
1582 SECT_OFF_TEXT (objfile));
c5aa993b 1583 SYMBOL_TYPE (sym) =
aff410f1
MS
1584 lookup_function_type (decode_function_type (cs, cs->c_type,
1585 aux, objfile));
c906108c 1586
f1e6e072 1587 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
c906108c
SS
1588 if (cs->c_sclass == C_STAT || cs->c_sclass == C_THUMBSTAT
1589 || cs->c_sclass == C_THUMBSTATFUNC)
e148f09d 1590 add_symbol_to_list (sym, get_file_symbols ());
c906108c
SS
1591 else if (cs->c_sclass == C_EXT || cs->c_sclass == C_THUMBEXT
1592 || cs->c_sclass == C_THUMBEXTFUNC)
e148f09d 1593 add_symbol_to_list (sym, get_global_symbols ());
c906108c
SS
1594 }
1595 else
1596 {
5e2b427d 1597 SYMBOL_TYPE (sym) = decode_type (cs, cs->c_type, aux, objfile);
c906108c
SS
1598 switch (cs->c_sclass)
1599 {
c5aa993b
JM
1600 case C_NULL:
1601 break;
c906108c 1602
c5aa993b 1603 case C_AUTO:
f1e6e072 1604 SYMBOL_ACLASS_INDEX (sym) = LOC_LOCAL;
e148f09d 1605 add_symbol_to_list (sym, get_local_symbols ());
c5aa993b 1606 break;
c906108c 1607
c5aa993b
JM
1608 case C_THUMBEXT:
1609 case C_THUMBEXTFUNC:
1610 case C_EXT:
f1e6e072 1611 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
c5aa993b 1612 SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
aff410f1
MS
1613 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
1614 SECT_OFF_TEXT (objfile));
e148f09d 1615 add_symbol_to_list (sym, get_global_symbols ());
c5aa993b 1616 break;
c906108c 1617
c5aa993b
JM
1618 case C_THUMBSTAT:
1619 case C_THUMBSTATFUNC:
1620 case C_STAT:
f1e6e072 1621 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
c5aa993b 1622 SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
aff410f1
MS
1623 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
1624 SECT_OFF_TEXT (objfile));
c5aa993b
JM
1625 if (within_function)
1626 {
aff410f1 1627 /* Static symbol of local scope. */
e148f09d 1628 add_symbol_to_list (sym, get_local_symbols ());
c906108c 1629 }
c5aa993b
JM
1630 else
1631 {
aff410f1 1632 /* Static symbol at top level of file. */
e148f09d 1633 add_symbol_to_list (sym, get_file_symbols ());
c906108c 1634 }
c5aa993b 1635 break;
c906108c
SS
1636
1637#ifdef C_GLBLREG /* AMD coff */
c5aa993b 1638 case C_GLBLREG:
c906108c 1639#endif
c5aa993b 1640 case C_REG:
f1e6e072 1641 SYMBOL_ACLASS_INDEX (sym) = coff_register_index;
768a979c 1642 SYMBOL_VALUE (sym) = cs->c_value;
e148f09d 1643 add_symbol_to_list (sym, get_local_symbols ());
c5aa993b 1644 break;
c906108c 1645
c5aa993b
JM
1646 case C_THUMBLABEL:
1647 case C_LABEL:
1648 break;
c906108c 1649
c5aa993b 1650 case C_ARG:
f1e6e072 1651 SYMBOL_ACLASS_INDEX (sym) = LOC_ARG;
2a2d4dc3 1652 SYMBOL_IS_ARGUMENT (sym) = 1;
e148f09d 1653 add_symbol_to_list (sym, get_local_symbols ());
c5aa993b 1654 break;
c906108c 1655
c5aa993b 1656 case C_REGPARM:
f1e6e072 1657 SYMBOL_ACLASS_INDEX (sym) = coff_register_index;
2a2d4dc3 1658 SYMBOL_IS_ARGUMENT (sym) = 1;
768a979c 1659 SYMBOL_VALUE (sym) = cs->c_value;
e148f09d 1660 add_symbol_to_list (sym, get_local_symbols ());
c5aa993b 1661 break;
c906108c 1662
c5aa993b 1663 case C_TPDEF:
f1e6e072 1664 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 1665 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
c5aa993b 1666
0963b4bd 1667 /* If type has no name, give it one. */
c5aa993b
JM
1668 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
1669 {
1670 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_PTR
1671 || TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_FUNC)
1672 {
aff410f1
MS
1673 /* If we are giving a name to a type such as
1674 "pointer to foo" or "function returning foo", we
1675 better not set the TYPE_NAME. If the program
1676 contains "typedef char *caddr_t;", we don't want
1677 all variables of type char * to print as caddr_t.
1678 This is not just a consequence of GDB's type
1679 management; CC and GCC (at least through version
1680 2.4) both output variables of either type char *
1681 or caddr_t with the type refering to the C_TPDEF
1682 symbol for caddr_t. If a future compiler cleans
1683 this up it GDB is not ready for it yet, but if it
1684 becomes ready we somehow need to disable this
1685 check (without breaking the PCC/GCC2.4 case).
c5aa993b
JM
1686
1687 Sigh.
1688
1689 Fortunately, this check seems not to be necessary
1690 for anything except pointers or functions. */
1691 ;
1692 }
1693 else
1694 TYPE_NAME (SYMBOL_TYPE (sym)) =
0d5cff50 1695 xstrdup (SYMBOL_LINKAGE_NAME (sym));
c5aa993b 1696 }
c906108c 1697
aff410f1
MS
1698 /* Keep track of any type which points to empty structured
1699 type, so it can be filled from a definition from another
1700 file. A simple forward reference (TYPE_CODE_UNDEF) is
1701 not an empty structured type, though; the forward
1702 references work themselves out via the magic of
1703 coff_lookup_type. */
5aafa1cc
PM
1704 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_PTR
1705 && TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym))) == 0
1706 && TYPE_CODE (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym)))
1707 != TYPE_CODE_UNDEF)
c5aa993b 1708 {
3567439c 1709 int i = hashname (SYMBOL_LINKAGE_NAME (sym));
c906108c 1710
c5aa993b
JM
1711 SYMBOL_VALUE_CHAIN (sym) = opaque_type_chain[i];
1712 opaque_type_chain[i] = sym;
1713 }
e148f09d 1714 add_symbol_to_list (sym, get_file_symbols ());
c5aa993b 1715 break;
c906108c 1716
c5aa993b
JM
1717 case C_STRTAG:
1718 case C_UNTAG:
1719 case C_ENTAG:
f1e6e072 1720 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 1721 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c5aa993b
JM
1722
1723 /* Some compilers try to be helpful by inventing "fake"
1724 names for anonymous enums, structures, and unions, like
aff410f1 1725 "~0fake" or ".0fake". Thanks, but no thanks... */
e86ca25f 1726 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
3567439c
DJ
1727 if (SYMBOL_LINKAGE_NAME (sym) != NULL
1728 && *SYMBOL_LINKAGE_NAME (sym) != '~'
1729 && *SYMBOL_LINKAGE_NAME (sym) != '.')
e86ca25f 1730 TYPE_NAME (SYMBOL_TYPE (sym)) =
395f9c91 1731 xstrdup (SYMBOL_LINKAGE_NAME (sym));
c5aa993b 1732
e148f09d 1733 add_symbol_to_list (sym, get_file_symbols ());
c5aa993b 1734 break;
c906108c 1735
c5aa993b
JM
1736 default:
1737 break;
c906108c
SS
1738 }
1739 }
1740 return sym;
1741}
1742\f
1743/* Decode a coff type specifier; return the type that is meant. */
1744
1745static struct type *
aa1ee363 1746decode_type (struct coff_symbol *cs, unsigned int c_type,
5e2b427d 1747 union internal_auxent *aux, struct objfile *objfile)
c906108c 1748{
52f0bd74 1749 struct type *type = 0;
c906108c
SS
1750 unsigned int new_c_type;
1751
1752 if (c_type & ~N_BTMASK)
1753 {
1754 new_c_type = DECREF (c_type);
1755 if (ISPTR (c_type))
1756 {
5e2b427d 1757 type = decode_type (cs, new_c_type, aux, objfile);
c906108c
SS
1758 type = lookup_pointer_type (type);
1759 }
1760 else if (ISFCN (c_type))
1761 {
5e2b427d 1762 type = decode_type (cs, new_c_type, aux, objfile);
c906108c
SS
1763 type = lookup_function_type (type);
1764 }
1765 else if (ISARY (c_type))
1766 {
1767 int i, n;
aa1ee363 1768 unsigned short *dim;
c906108c
SS
1769 struct type *base_type, *index_type, *range_type;
1770
1771 /* Define an array type. */
aff410f1 1772 /* auxent refers to array, not base type. */
c906108c
SS
1773 if (aux->x_sym.x_tagndx.l == 0)
1774 cs->c_naux = 0;
1775
aff410f1 1776 /* Shift the indices down. */
c906108c
SS
1777 dim = &aux->x_sym.x_fcnary.x_ary.x_dimen[0];
1778 i = 1;
1779 n = dim[0];
1780 for (i = 0; *dim && i < DIMNUM - 1; i++, dim++)
1781 *dim = *(dim + 1);
1782 *dim = 0;
1783
5e2b427d 1784 base_type = decode_type (cs, new_c_type, aux, objfile);
46bf5051 1785 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 1786 range_type
cafb3438 1787 = create_static_range_type (NULL, index_type, 0, n - 1);
c906108c 1788 type =
cafb3438 1789 create_array_type (NULL, base_type, range_type);
c906108c
SS
1790 }
1791 return type;
1792 }
1793
aff410f1
MS
1794 /* Reference to existing type. This only occurs with the struct,
1795 union, and enum types. EPI a29k coff fakes us out by producing
1796 aux entries with a nonzero x_tagndx for definitions of structs,
1797 unions, and enums, so we have to check the c_sclass field. SCO
1798 3.2v4 cc gets confused with pointers to pointers to defined
1799 structs, and generates negative x_tagndx fields. */
c906108c
SS
1800 if (cs->c_naux > 0 && aux->x_sym.x_tagndx.l != 0)
1801 {
1802 if (cs->c_sclass != C_STRTAG
1803 && cs->c_sclass != C_UNTAG
1804 && cs->c_sclass != C_ENTAG
1805 && aux->x_sym.x_tagndx.l >= 0)
1806 {
1807 type = coff_alloc_type (aux->x_sym.x_tagndx.l);
1808 return type;
1809 }
1810 else
1811 {
b98664d3 1812 complaint (_("Symbol table entry for %s has bad tagndx value"),
23136709 1813 cs->c_name);
aff410f1 1814 /* And fall through to decode_base_type... */
c906108c
SS
1815 }
1816 }
1817
5e2b427d 1818 return decode_base_type (cs, BTYPE (c_type), aux, objfile);
c906108c
SS
1819}
1820
1821/* Decode a coff type specifier for function definition;
1822 return the type that the function returns. */
1823
1824static struct type *
aff410f1
MS
1825decode_function_type (struct coff_symbol *cs,
1826 unsigned int c_type,
1827 union internal_auxent *aux,
1828 struct objfile *objfile)
c906108c
SS
1829{
1830 if (aux->x_sym.x_tagndx.l == 0)
aff410f1
MS
1831 cs->c_naux = 0; /* auxent refers to function, not base
1832 type. */
c906108c 1833
5e2b427d 1834 return decode_type (cs, DECREF (c_type), aux, objfile);
c906108c
SS
1835}
1836\f
aff410f1 1837/* Basic C types. */
c906108c
SS
1838
1839static struct type *
aff410f1
MS
1840decode_base_type (struct coff_symbol *cs,
1841 unsigned int c_type,
1842 union internal_auxent *aux,
1843 struct objfile *objfile)
c906108c 1844{
5e2b427d 1845 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
1846 struct type *type;
1847
1848 switch (c_type)
1849 {
c5aa993b 1850 case T_NULL:
aff410f1 1851 /* Shows up with "void (*foo)();" structure members. */
46bf5051 1852 return objfile_type (objfile)->builtin_void;
c906108c 1853
c906108c 1854#ifdef T_VOID
c5aa993b
JM
1855 case T_VOID:
1856 /* Intel 960 COFF has this symbol and meaning. */
46bf5051 1857 return objfile_type (objfile)->builtin_void;
c906108c
SS
1858#endif
1859
c5aa993b 1860 case T_CHAR:
46bf5051 1861 return objfile_type (objfile)->builtin_char;
c906108c 1862
c5aa993b 1863 case T_SHORT:
46bf5051 1864 return objfile_type (objfile)->builtin_short;
c906108c 1865
c5aa993b 1866 case T_INT:
46bf5051 1867 return objfile_type (objfile)->builtin_int;
c906108c 1868
c5aa993b
JM
1869 case T_LONG:
1870 if (cs->c_sclass == C_FIELD
9a76efb6 1871 && aux->x_sym.x_misc.x_lnsz.x_size
5e2b427d 1872 > gdbarch_long_bit (gdbarch))
46bf5051 1873 return objfile_type (objfile)->builtin_long_long;
c5aa993b 1874 else
46bf5051 1875 return objfile_type (objfile)->builtin_long;
c906108c 1876
c5aa993b 1877 case T_FLOAT:
46bf5051 1878 return objfile_type (objfile)->builtin_float;
c906108c 1879
c5aa993b 1880 case T_DOUBLE:
46bf5051 1881 return objfile_type (objfile)->builtin_double;
c906108c 1882
c5aa993b 1883 case T_LNGDBL:
46bf5051 1884 return objfile_type (objfile)->builtin_long_double;
c906108c 1885
c5aa993b
JM
1886 case T_STRUCT:
1887 if (cs->c_naux != 1)
1888 {
aff410f1 1889 /* Anonymous structure type. */
c5aa993b
JM
1890 type = coff_alloc_type (cs->c_symnum);
1891 TYPE_CODE (type) = TYPE_CODE_STRUCT;
1892 TYPE_NAME (type) = NULL;
c5aa993b
JM
1893 INIT_CPLUS_SPECIFIC (type);
1894 TYPE_LENGTH (type) = 0;
1895 TYPE_FIELDS (type) = 0;
1896 TYPE_NFIELDS (type) = 0;
1897 }
1898 else
1899 {
1900 type = coff_read_struct_type (cs->c_symnum,
1901 aux->x_sym.x_misc.x_lnsz.x_size,
5e2b427d
UW
1902 aux->x_sym.x_fcnary.x_fcn.x_endndx.l,
1903 objfile);
c5aa993b
JM
1904 }
1905 return type;
c906108c 1906
c5aa993b
JM
1907 case T_UNION:
1908 if (cs->c_naux != 1)
1909 {
aff410f1 1910 /* Anonymous union type. */
c5aa993b
JM
1911 type = coff_alloc_type (cs->c_symnum);
1912 TYPE_NAME (type) = NULL;
c5aa993b
JM
1913 INIT_CPLUS_SPECIFIC (type);
1914 TYPE_LENGTH (type) = 0;
1915 TYPE_FIELDS (type) = 0;
1916 TYPE_NFIELDS (type) = 0;
1917 }
1918 else
1919 {
1920 type = coff_read_struct_type (cs->c_symnum,
c906108c 1921 aux->x_sym.x_misc.x_lnsz.x_size,
5e2b427d
UW
1922 aux->x_sym.x_fcnary.x_fcn.x_endndx.l,
1923 objfile);
c5aa993b
JM
1924 }
1925 TYPE_CODE (type) = TYPE_CODE_UNION;
1926 return type;
c906108c 1927
c5aa993b
JM
1928 case T_ENUM:
1929 if (cs->c_naux != 1)
1930 {
aff410f1 1931 /* Anonymous enum type. */
c5aa993b
JM
1932 type = coff_alloc_type (cs->c_symnum);
1933 TYPE_CODE (type) = TYPE_CODE_ENUM;
1934 TYPE_NAME (type) = NULL;
c5aa993b
JM
1935 TYPE_LENGTH (type) = 0;
1936 TYPE_FIELDS (type) = 0;
1937 TYPE_NFIELDS (type) = 0;
1938 }
1939 else
1940 {
1941 type = coff_read_enum_type (cs->c_symnum,
1942 aux->x_sym.x_misc.x_lnsz.x_size,
5e2b427d
UW
1943 aux->x_sym.x_fcnary.x_fcn.x_endndx.l,
1944 objfile);
c5aa993b
JM
1945 }
1946 return type;
1947
1948 case T_MOE:
aff410f1 1949 /* Shouldn't show up here. */
c5aa993b 1950 break;
c906108c 1951
c5aa993b 1952 case T_UCHAR:
46bf5051 1953 return objfile_type (objfile)->builtin_unsigned_char;
c906108c 1954
c5aa993b 1955 case T_USHORT:
46bf5051 1956 return objfile_type (objfile)->builtin_unsigned_short;
c906108c 1957
c5aa993b 1958 case T_UINT:
46bf5051 1959 return objfile_type (objfile)->builtin_unsigned_int;
c906108c 1960
c5aa993b
JM
1961 case T_ULONG:
1962 if (cs->c_sclass == C_FIELD
9a76efb6 1963 && aux->x_sym.x_misc.x_lnsz.x_size
5e2b427d 1964 > gdbarch_long_bit (gdbarch))
46bf5051 1965 return objfile_type (objfile)->builtin_unsigned_long_long;
c5aa993b 1966 else
46bf5051 1967 return objfile_type (objfile)->builtin_unsigned_long;
c906108c 1968 }
b98664d3 1969 complaint (_("Unexpected type for symbol %s"), cs->c_name);
46bf5051 1970 return objfile_type (objfile)->builtin_void;
c906108c
SS
1971}
1972\f
1973/* This page contains subroutines of read_type. */
1974
1975/* Read the description of a structure (or union type) and return an
1976 object describing the type. */
1977
1978static struct type *
5e2b427d
UW
1979coff_read_struct_type (int index, int length, int lastsym,
1980 struct objfile *objfile)
c906108c
SS
1981{
1982 struct nextfield
1983 {
1984 struct nextfield *next;
1985 struct field field;
1986 };
1987
52f0bd74
AC
1988 struct type *type;
1989 struct nextfield *list = 0;
fe978cb0 1990 struct nextfield *newobj;
c906108c 1991 int nfields = 0;
52f0bd74 1992 int n;
c906108c
SS
1993 char *name;
1994 struct coff_symbol member_sym;
52f0bd74 1995 struct coff_symbol *ms = &member_sym;
c906108c
SS
1996 struct internal_syment sub_sym;
1997 union internal_auxent sub_aux;
1998 int done = 0;
1999
2000 type = coff_alloc_type (index);
2001 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c5aa993b 2002 INIT_CPLUS_SPECIFIC (type);
c906108c
SS
2003 TYPE_LENGTH (type) = length;
2004
2005 while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
2006 {
2007 read_one_sym (ms, &sub_sym, &sub_aux);
2008 name = ms->c_name;
5e2b427d 2009 name = EXTERNAL_NAME (name, objfile->obfd);
c906108c
SS
2010
2011 switch (ms->c_sclass)
2012 {
c5aa993b
JM
2013 case C_MOS:
2014 case C_MOU:
2015
2016 /* Get space to record the next field's data. */
8d749320 2017 newobj = XALLOCA (struct nextfield);
fe978cb0
PA
2018 newobj->next = list;
2019 list = newobj;
c5aa993b
JM
2020
2021 /* Save the data. */
224c3ddb
SM
2022 list->field.name
2023 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10f0c4bb 2024 name, strlen (name));
aff410f1
MS
2025 FIELD_TYPE (list->field) = decode_type (ms, ms->c_type,
2026 &sub_aux, objfile);
d6a843b5 2027 SET_FIELD_BITPOS (list->field, 8 * ms->c_value);
c5aa993b
JM
2028 FIELD_BITSIZE (list->field) = 0;
2029 nfields++;
2030 break;
c906108c 2031
c5aa993b
JM
2032 case C_FIELD:
2033
2034 /* Get space to record the next field's data. */
8d749320 2035 newobj = XALLOCA (struct nextfield);
fe978cb0
PA
2036 newobj->next = list;
2037 list = newobj;
c5aa993b
JM
2038
2039 /* Save the data. */
224c3ddb
SM
2040 list->field.name
2041 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
10f0c4bb 2042 name, strlen (name));
aff410f1
MS
2043 FIELD_TYPE (list->field) = decode_type (ms, ms->c_type,
2044 &sub_aux, objfile);
d6a843b5 2045 SET_FIELD_BITPOS (list->field, ms->c_value);
c5aa993b
JM
2046 FIELD_BITSIZE (list->field) = sub_aux.x_sym.x_misc.x_lnsz.x_size;
2047 nfields++;
2048 break;
c906108c 2049
c5aa993b
JM
2050 case C_EOS:
2051 done = 1;
2052 break;
c906108c
SS
2053 }
2054 }
2055 /* Now create the vector of fields, and record how big it is. */
2056
2057 TYPE_NFIELDS (type) = nfields;
2058 TYPE_FIELDS (type) = (struct field *)
2059 TYPE_ALLOC (type, sizeof (struct field) * nfields);
2060
2061 /* Copy the saved-up fields into the field vector. */
2062
2063 for (n = nfields; list; list = list->next)
2064 TYPE_FIELD (type, --n) = list->field;
2065
2066 return type;
2067}
2068\f
2069/* Read a definition of an enumeration type,
2070 and create and return a suitable type object.
2071 Also defines the symbols that represent the values of the type. */
2072
c906108c 2073static struct type *
5e2b427d
UW
2074coff_read_enum_type (int index, int length, int lastsym,
2075 struct objfile *objfile)
c906108c 2076{
5e2b427d 2077 struct gdbarch *gdbarch = get_objfile_arch (objfile);
52f0bd74
AC
2078 struct symbol *sym;
2079 struct type *type;
c906108c
SS
2080 int nsyms = 0;
2081 int done = 0;
2082 struct pending **symlist;
2083 struct coff_symbol member_sym;
52f0bd74 2084 struct coff_symbol *ms = &member_sym;
c906108c
SS
2085 struct internal_syment sub_sym;
2086 union internal_auxent sub_aux;
2087 struct pending *osyms, *syms;
2088 int o_nsyms;
52f0bd74 2089 int n;
c906108c
SS
2090 char *name;
2091 int unsigned_enum = 1;
2092
2093 type = coff_alloc_type (index);
2094 if (within_function)
e148f09d 2095 symlist = get_local_symbols ();
c906108c 2096 else
e148f09d 2097 symlist = get_file_symbols ();
c906108c
SS
2098 osyms = *symlist;
2099 o_nsyms = osyms ? osyms->nsyms : 0;
2100
2101 while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
2102 {
2103 read_one_sym (ms, &sub_sym, &sub_aux);
2104 name = ms->c_name;
5e2b427d 2105 name = EXTERNAL_NAME (name, objfile->obfd);
c906108c
SS
2106
2107 switch (ms->c_sclass)
2108 {
c5aa993b 2109 case C_MOE:
e623cf5d 2110 sym = allocate_symbol (objfile);
c5aa993b 2111
224c3ddb
SM
2112 name = (char *) obstack_copy0 (&objfile->objfile_obstack, name,
2113 strlen (name));
2114 SYMBOL_SET_LINKAGE_NAME (sym, name);
f1e6e072 2115 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
176620f1 2116 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
c5aa993b
JM
2117 SYMBOL_VALUE (sym) = ms->c_value;
2118 add_symbol_to_list (sym, symlist);
2119 nsyms++;
2120 break;
c906108c 2121
c5aa993b
JM
2122 case C_EOS:
2123 /* Sometimes the linker (on 386/ix 2.0.2 at least) screws
2124 up the count of how many symbols to read. So stop
2125 on .eos. */
2126 done = 1;
2127 break;
c906108c
SS
2128 }
2129 }
2130
2131 /* Now fill in the fields of the type-structure. */
2132
2133 if (length > 0)
2134 TYPE_LENGTH (type) = length;
9a76efb6 2135 else /* Assume ints. */
5e2b427d 2136 TYPE_LENGTH (type) = gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT;
c906108c
SS
2137 TYPE_CODE (type) = TYPE_CODE_ENUM;
2138 TYPE_NFIELDS (type) = nsyms;
2139 TYPE_FIELDS (type) = (struct field *)
2140 TYPE_ALLOC (type, sizeof (struct field) * nsyms);
2141
2142 /* Find the symbols for the values and put them into the type.
2143 The symbols can be found in the symlist that we put them on
2144 to cause them to be defined. osyms contains the old value
2145 of that symlist; everything up to there was defined by us. */
2146 /* Note that we preserve the order of the enum constants, so
2147 that in something like "enum {FOO, LAST_THING=FOO}" we print
2148 FOO, not LAST_THING. */
2149
2150 for (syms = *symlist, n = 0; syms; syms = syms->next)
2151 {
2152 int j = 0;
2153
2154 if (syms == osyms)
2155 j = o_nsyms;
c5aa993b 2156 for (; j < syms->nsyms; j++, n++)
c906108c
SS
2157 {
2158 struct symbol *xsym = syms->symbol[j];
c5504eaf 2159
c906108c 2160 SYMBOL_TYPE (xsym) = type;
3567439c 2161 TYPE_FIELD_NAME (type, n) = SYMBOL_LINKAGE_NAME (xsym);
14e75d8e 2162 SET_FIELD_ENUMVAL (TYPE_FIELD (type, n), SYMBOL_VALUE (xsym));
c906108c
SS
2163 if (SYMBOL_VALUE (xsym) < 0)
2164 unsigned_enum = 0;
2165 TYPE_FIELD_BITSIZE (type, n) = 0;
2166 }
2167 if (syms == osyms)
2168 break;
2169 }
2170
2171 if (unsigned_enum)
876cecd0 2172 TYPE_UNSIGNED (type) = 1;
c906108c
SS
2173
2174 return type;
2175}
2176
aff410f1 2177/* Register our ability to parse symbols for coff BFD files. */
c906108c 2178
00b5771c 2179static const struct sym_fns coff_sym_fns =
c906108c 2180{
aff410f1
MS
2181 coff_new_init, /* sym_new_init: init anything gbl to
2182 entire symtab */
2183 coff_symfile_init, /* sym_init: read initial info, setup
2184 for sym_read() */
2185 coff_symfile_read, /* sym_read: read a symbol file into
2186 symtab */
b11896a5 2187 NULL, /* sym_read_psymbols */
aff410f1
MS
2188 coff_symfile_finish, /* sym_finish: finished with file,
2189 cleanup */
2190 default_symfile_offsets, /* sym_offsets: xlate external to
2191 internal form */
2192 default_symfile_segments, /* sym_segments: Get segment
2193 information from a file */
c295b2e5 2194 NULL, /* sym_read_linetable */
aff410f1
MS
2195
2196 default_symfile_relocate, /* sym_relocate: Relocate a debug
2197 section. */
55aa24fb 2198 NULL, /* sym_probe_fns */
00b5771c 2199 &psym_functions
c906108c
SS
2200};
2201
2202void
fba45db2 2203_initialize_coffread (void)
c906108c 2204{
c256e171 2205 add_symtab_fns (bfd_target_coff_flavour, &coff_sym_fns);
b8b98ad1 2206
f1e6e072
TT
2207 coff_register_index
2208 = register_symbol_register_impl (LOC_REGISTER, &coff_register_funcs);
c906108c 2209}