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