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