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