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