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