Commit | Line | Data |
---|---|---|
bd5635a1 RP |
1 | /* Read a symbol table in MIPS' format (Third-Eye). |
2 | Copyright (C) 1986, 1987, 1989-1991 Free Software Foundation, Inc. | |
3 | Contributed by Alessandro Forin (af@cs.cmu.edu) at CMU | |
4 | ||
5 | This file is part of GDB. | |
6 | ||
7 | GDB is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 1, or (at your option) | |
10 | any later version. | |
11 | ||
12 | GDB 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. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GDB; see the file COPYING. If not, write to | |
19 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
20 | ||
21 | #include <stdio.h> | |
22 | #include "param.h" | |
23 | #include "obstack.h" | |
24 | #include <sys/param.h> | |
25 | #include <sys/file.h> | |
26 | #include <sys/stat.h> | |
27 | #include "defs.h" | |
28 | #include "symtab.h" | |
29 | #include "gdbcore.h" | |
30 | #include "symfile.h" | |
31 | #ifdef CMUCS | |
32 | #include <mips/syms.h> | |
31ef19fc JK |
33 | #else /* not CMUCS */ |
34 | #include <syms.h> | |
35 | #endif /* not CMUCS */ | |
bd5635a1 RP |
36 | |
37 | /* Since these things are defined differently on various systems I'll | |
38 | (re)define here what I really need in this module. I only assume the | |
39 | three standard COFF structure definitions: filehdr, aouthdr, scnhdr */ | |
40 | #define MIPS /* Kludge to get MIPS version of coff */ | |
41 | #include "intel-coff.h" | |
42 | ||
43 | struct coff_exec { | |
44 | struct filehdr f; | |
45 | struct aouthdr a; | |
46 | }; | |
47 | #undef a_magic | |
48 | #undef a_text | |
49 | #undef a_data | |
50 | #undef a_bss | |
51 | #undef a_syms | |
52 | #undef a_entry | |
53 | #define a_magic a.magic /* magic number */ | |
54 | #define a_text a.tsize /* size of text segment */ | |
55 | #define a_data a.dsize /* size of initialized data */ | |
56 | #define a_bss a.bsize /* size of uninitialized data */ | |
57 | #define a_syms f.f_nsyms /* size of symbol table */ | |
58 | #define a_entry a.entry /* entry point */ | |
59 | ||
60 | #undef N_BADMAG | |
61 | #define N_BADMAG(x) \ | |
62 | (((x).a_magic)!=OMAGIC && ((x).a_magic)!=NMAGIC && ((x).a_magic)!=ZMAGIC) | |
63 | ||
64 | /* Things we import explicitly from other modules */ | |
65 | ||
66 | extern int info_verbose; | |
67 | extern struct block *block_for_pc(); | |
68 | extern void sort_symtab_syms(); | |
69 | ||
70 | /* Forward declarations */ | |
71 | ||
72 | static void psymtab_to_symtab_1(); | |
73 | ||
74 | /* Macros and extra defs */ | |
75 | ||
76 | struct complaint unknown_ext_complaint = | |
77 | {"unknown external symbol %s", 0, 0}; | |
78 | ||
79 | /* Already parsed symbols are marked specially */ | |
80 | ||
81 | #define stParsed stType | |
82 | ||
83 | /* Puns: hard to find whether -g was used and how */ | |
84 | ||
85 | #define MIN_GLEVEL GLEVEL_0 | |
86 | #define compare_glevel(a,b) \ | |
87 | (((a) == GLEVEL_3) ? ((b) < GLEVEL_3) : \ | |
88 | ((b) == GLEVEL_3) ? -1 : (int)((b) - (a))) | |
89 | ||
90 | /* When looking at .o files avoid tripping over bad addresses */ | |
91 | ||
92 | #define SAFE_TEXT_ADDR 0x400000 | |
93 | #define SAFE_DATA_ADDR 0x10000000 | |
94 | ||
95 | #define UNSAFE_DATA_ADDR(p) ((unsigned)p < SAFE_DATA_ADDR || (unsigned)p > 2*SAFE_DATA_ADDR) | |
96 | ||
97 | \f | |
98 | /* Things we export to other modules */ | |
99 | ||
100 | ||
101 | /* Lists of partial symbols */ | |
102 | ||
103 | struct psymbol_allocation_list global_psymbols, static_psymbols; | |
104 | ||
105 | /* Address bounds for the signal trampoline in inferior, if any */ | |
106 | ||
107 | CORE_ADDR sigtramp_address, sigtramp_end; | |
108 | ||
109 | ||
110 | /* Functions that we really export */ | |
111 | ||
112 | /* THIS DESCRIPTION IS OBSOLETE POST-BFD; FIXME! */ | |
113 | /* Basically, this module must provide two functions: symbol_file_command() | |
114 | which loads the symbol table from a file, and add_file_command() which | |
115 | adds more symbols to the symbol table (incrementally). | |
116 | ||
117 | These two functions only do the minimum work necessary for letting the | |
118 | user "name" things symbolically, they do not read the entire symtab. | |
119 | Instead, they read in the external symbols and put them in partial | |
120 | symbol tables. When more extensive information is requested of a | |
121 | file the corresponding partial symbol table is mutated into a full | |
122 | fledged symbol table by going back and reading the relative symbols | |
123 | for real. mipscoff_psymtab_to_symtab() is the function that does this */ | |
124 | ||
125 | /* The entry point (starting address) of the file, if it is an executable. */ | |
126 | ||
127 | static CORE_ADDR entry_point; | |
128 | ||
129 | extern CORE_ADDR startup_file_start; /* From blockframe.c */ | |
130 | extern CORE_ADDR startup_file_end; /* From blockframe.c */ | |
131 | ||
132 | void | |
133 | mipscoff_new_init() | |
134 | { | |
135 | } | |
136 | ||
137 | void | |
138 | mipscoff_symfile_init (sf) | |
139 | struct sym_fns *sf; | |
140 | { | |
141 | bfd *abfd = sf->sym_bfd; | |
142 | sf->sym_private = NULL; | |
143 | /* Save startup file's range of PC addresses to help blockframe.c | |
144 | decide where the bottom of the stack is. */ | |
145 | if (bfd_get_file_flags (abfd) & EXEC_P) | |
146 | { | |
147 | /* Executable file -- record its entry point so we'll recognize | |
148 | the startup file because it contains the entry point. */ | |
149 | entry_point = bfd_get_start_address (abfd); | |
150 | } | |
151 | else | |
152 | { | |
153 | /* Examination of non-executable.o files. Short-circuit this stuff. */ | |
154 | /* ~0 will not be in any file, we hope. */ | |
155 | entry_point = ~0; | |
156 | /* set the startup file to be an empty range. */ | |
157 | startup_file_start = 0; | |
158 | startup_file_end = 0; | |
159 | } | |
160 | } | |
161 | ||
162 | void | |
163 | mipscoff_symfile_read(sf, addr, mainline) | |
164 | struct sym_fns *sf; | |
165 | CORE_ADDR addr; | |
166 | int mainline; | |
167 | { | |
168 | struct coff_symfile_info *info = (struct coff_symfile_info *)sf->sym_private; | |
169 | bfd *abfd = sf->sym_bfd; | |
170 | char *name = bfd_get_filename (abfd); | |
171 | int desc; | |
172 | register int val; | |
173 | int num_symbols; | |
174 | int symtab_offset; | |
175 | int stringtab_offset; | |
176 | ||
177 | /* WARNING WILL ROBINSON! ACCESSING BFD-PRIVATE DATA HERE! FIXME! */ | |
178 | desc = fileno ((FILE *)(abfd->iostream)); /* Raw file descriptor */ | |
179 | num_symbols = bfd_get_symcount (abfd); /* How many syms */ | |
180 | /* symtab_offset = obj_sym_filepos (abfd); * Symbol table file offset */ | |
181 | /* stringtab_offset = symtab_offset + num_symbols * SYMESZ; * String tab */ | |
182 | /* End of warning */ | |
183 | ||
184 | #ifdef TDESC | |
185 | debug_info = text_hdr.s_relptr; | |
186 | if (tdesc_handle) | |
187 | { | |
188 | dc_terminate (tdesc_handle); | |
189 | tdesc_handle = 0; | |
190 | } | |
191 | #endif | |
192 | ||
193 | #if 0 | |
194 | /* Read the line number table, all at once. */ | |
195 | info->min_lineno_offset = 0; | |
196 | info->max_lineno_offset = 0; | |
197 | bfd_map_over_sections (abfd, find_linenos, info); | |
198 | ||
199 | val = init_lineno (desc, info->min_lineno_offset, | |
200 | info->max_lineno_offset - info->min_lineno_offset); | |
201 | if (val < 0) | |
202 | error ("\"%s\": error reading line numbers\n", name); | |
203 | ||
204 | /* Now read the string table, all at once. */ | |
205 | ||
206 | val = init_stringtab (desc, stringtab_offset); | |
207 | if (val < 0) | |
208 | { | |
209 | free_all_symtabs (); /* FIXME blows whole symtab */ | |
210 | printf ("\"%s\": can't get string table", name); | |
211 | fflush (stdout); | |
212 | return; | |
213 | } | |
214 | make_cleanup (free_stringtab, 0); | |
215 | #endif | |
216 | /* Position to read the symbol table. Do not read it all at once. */ | |
217 | val = lseek (desc, (long)symtab_offset, 0); | |
218 | if (val < 0) | |
219 | perror_with_name (name); | |
220 | ||
221 | init_misc_bunches (); | |
222 | make_cleanup (discard_misc_bunches, 0); | |
223 | ||
224 | /* Now that the executable file is positioned at symbol table, | |
225 | process it and define symbols accordingly. */ | |
226 | ||
bd5635a1 RP |
227 | read_mips_symtab(desc, 0); |
228 | ||
229 | /* patch_opaque_types ();*/ | |
230 | ||
231 | /* Sort symbols alphabetically within each block. */ | |
232 | ||
233 | sort_all_symtab_syms (); | |
234 | ||
235 | /* Go over the misc symbol bunches and install them in vector. */ | |
236 | ||
237 | condense_misc_bunches (0); | |
238 | ||
239 | /* Make a default for file to list. */ | |
240 | ||
241 | select_source_symtab (0); /* FIXME, this might be too slow, see dbxread */ | |
242 | } | |
243 | ||
244 | void | |
245 | mipscoff_symfile_discard() | |
246 | { | |
247 | } | |
248 | ||
bd5635a1 RP |
249 | /* Exported procedure: Allocate zeroed memory */ |
250 | ||
251 | char *xzalloc(size) | |
252 | { | |
253 | char *p = xmalloc(size); | |
254 | ||
255 | bzero(p, size); | |
256 | return p; | |
257 | } | |
258 | ||
259 | /* Exported procedure: Builds a symtab from the PST partial one. | |
260 | Restores the environment in effect when PST was created, delegates | |
261 | most of the work to an ancillary procedure, and sorts | |
262 | and reorders the symtab list at the end */ | |
263 | ||
264 | /* Forward decls */ | |
265 | static HDRR *cur_hdr; /* MIPS symtab header for the current file */ | |
266 | ||
267 | void | |
268 | mipscoff_psymtab_to_symtab(pst) | |
269 | struct partial_symtab *pst; | |
270 | { | |
271 | struct symtab *ret; | |
272 | int i; | |
273 | ||
274 | if (!pst) | |
275 | return; | |
276 | ||
277 | if (info_verbose) { | |
278 | printf_filtered("Reading in symbols for %s...", pst->filename); | |
279 | fflush(stdout); | |
280 | } | |
281 | /* Restore the header and list of pending typedefs */ | |
282 | cur_hdr = (HDRR *) pst->ldsymlen; | |
283 | ||
284 | psymtab_to_symtab_1(pst); | |
285 | ||
286 | reorder_symtabs(); | |
287 | ||
288 | /* Finish up the debug error message. */ | |
289 | if (info_verbose) | |
290 | printf_filtered("done.\n"); | |
291 | } | |
292 | ||
293 | /* Exported procedure: Is PC in the signal trampoline code */ | |
294 | ||
295 | int in_sigtramp(pc,name) | |
296 | CORE_ADDR pc; | |
297 | { | |
298 | if (sigtramp_address == 0) | |
299 | fixup_sigtramp(); | |
300 | return (pc >= sigtramp_address && pc < sigtramp_end); | |
301 | } | |
302 | ||
303 | \f | |
304 | /* Things that really are local to this module */ | |
305 | ||
306 | /* All allocated symtabs and psymtabs */ | |
307 | ||
308 | static int all_symtabs_count; | |
309 | static int all_psymtabs_count; | |
310 | ||
311 | /* GDB symtable for the current compilation unit */ | |
312 | ||
313 | static struct symtab *cur_stab; | |
314 | ||
315 | /* Header for executable/object file we read symbols from */ | |
316 | ||
317 | static struct coff_exec filhdr; | |
318 | #define END_OF_TEXT_SEGMENT(f) ((f).a.text_start + (f).a.tsize) | |
319 | ||
320 | /* Pointer to current file decriptor record, and its index */ | |
321 | ||
322 | static FDR *cur_fdr; | |
323 | static int cur_fd; | |
324 | ||
325 | /* Index of current symbol */ | |
326 | ||
327 | static int cur_sdx; | |
328 | ||
329 | /* Note how much "debuggable" this image is. We would like | |
330 | to see at least one FDR with full symbols */ | |
331 | ||
332 | static max_gdbinfo; | |
333 | static max_glevel; | |
334 | ||
335 | /* When examining .o files, report on undefined symbols */ | |
336 | ||
337 | static int n_undef_symbols, n_undef_labels, n_undef_vars, n_undef_procs; | |
338 | ||
339 | /* Extra builtin types */ | |
340 | ||
341 | struct type *builtin_type_complex; | |
342 | struct type *builtin_type_double_complex; | |
343 | struct type *builtin_type_fixed_dec; | |
344 | struct type *builtin_type_float_dec; | |
345 | struct type *builtin_type_string; | |
346 | ||
347 | /* Template types */ | |
348 | ||
349 | static struct type *builtin_type_ptr; | |
350 | static struct type *builtin_type_struct; | |
351 | static struct type *builtin_type_union; | |
352 | static struct type *builtin_type_enum; | |
353 | static struct type *builtin_type_range; | |
354 | static struct type *builtin_type_set; | |
355 | ||
356 | ||
357 | /* Forward decls */ | |
358 | ||
359 | static struct symbol *new_symbol(); | |
360 | static struct type *new_type(); | |
361 | static struct field *new_field(); | |
362 | static struct block *new_block(); | |
363 | static struct symtab *new_symtab(); | |
364 | static struct linetable *new_linetable(); | |
365 | static struct blockvector *new_bvect(); | |
366 | ||
367 | static struct type *parse_type(); | |
368 | static struct type *make_type(); | |
369 | static struct symbol *mylookup_symbol(); | |
370 | static struct block *shrink_block(); | |
371 | ||
372 | static int compare_symtabs(); | |
373 | static int compare_psymtabs(); | |
374 | static int compare_blocks(); | |
375 | ||
376 | static struct partial_symtab *new_psymtab(); | |
377 | static struct partial_symbol *new_psymbol(); | |
378 | static struct partial_symtab *parse_fdr(); | |
379 | static int compare_psymbols(); | |
380 | \f | |
381 | /* File-level interface functions */ | |
382 | ||
383 | /* Read the symtab information from file FSYM into memory */ | |
384 | ||
385 | static | |
386 | read_the_mips_symtab(fsym) | |
387 | { | |
388 | int stsize, st_hdrsize; | |
389 | unsigned st_filptr; | |
390 | HDRR st_hdr; | |
391 | ||
392 | /* Find and read the symbol table header */ | |
393 | st_hdrsize = filhdr.f.f_nsyms; | |
394 | st_filptr = filhdr.f.f_symptr; | |
395 | if (st_filptr == 0) | |
396 | return 0; | |
397 | ||
398 | lseek(fsym, st_filptr, L_SET); | |
399 | if (read(fsym, &st_hdr, st_hdrsize) != st_hdrsize) | |
400 | goto readerr; | |
401 | ||
402 | /* Find out how large the symbol table is */ | |
403 | stsize = (st_hdr.cbExtOffset - (st_filptr + st_hdrsize)) | |
404 | + st_hdr.iextMax * cbEXTR; | |
405 | ||
406 | /* Allocate space for the symbol table. Read it in. */ | |
407 | cur_hdr = (HDRR *) xmalloc(stsize + st_hdrsize); | |
408 | ||
409 | bcopy(&st_hdr, cur_hdr, st_hdrsize); | |
410 | if (read(fsym, (char *) cur_hdr + st_hdrsize, stsize) != stsize) | |
411 | goto readerr; | |
412 | ||
413 | /* Fixup file_pointers in it */ | |
414 | fixup_symtab(cur_hdr, (char *) cur_hdr + st_hdrsize, | |
415 | st_filptr + st_hdrsize); | |
416 | ||
417 | return; | |
418 | readerr: | |
419 | error("Short read on %s", symfile); | |
420 | } | |
421 | ||
422 | ||
423 | /* Turn all file-relative pointers in the symtab described by HDR | |
424 | into memory pointers, given that the symtab itself is located | |
425 | at DATA in memory and F_PTR in the file. */ | |
426 | ||
427 | static | |
428 | fixup_symtab( hdr, data, f_ptr) | |
429 | HDRR *hdr; | |
430 | char *data; | |
431 | { | |
432 | int f_idx, s_idx; | |
433 | FDR *fh; | |
434 | SYMR *sh; | |
435 | OPTR *op; | |
436 | PDR *pr; | |
437 | EXTR *esh; | |
438 | ||
439 | /* | |
440 | * These fields are useless (and empty) by now: | |
441 | * hdr->cbDnOffset, hdr->cbOptOffset | |
442 | * We use them for other internal purposes. | |
443 | */ | |
444 | hdr->cbDnOffset = 0; | |
445 | hdr->cbOptOffset = 0; | |
446 | ||
447 | #define FIX(off) \ | |
448 | if (hdr->off) hdr->off = (unsigned int)data + (hdr->off - f_ptr); | |
449 | ||
450 | FIX(cbLineOffset); | |
451 | FIX(cbPdOffset); | |
452 | FIX(cbSymOffset); | |
453 | FIX(cbOptOffset); | |
454 | FIX(cbAuxOffset); | |
455 | FIX(cbSsOffset); | |
456 | FIX(cbSsExtOffset); | |
457 | FIX(cbFdOffset); | |
458 | FIX(cbRfdOffset); | |
459 | FIX(cbExtOffset); | |
460 | #undef FIX | |
461 | ||
462 | ||
463 | /* | |
464 | * Fix all string pointers inside the symtab, and | |
465 | * the FDR records. Also fix other miscellany. | |
466 | */ | |
467 | for (f_idx = 0; f_idx < hdr->ifdMax; f_idx++) { | |
468 | register unsigned code_offset; | |
469 | ||
470 | /* Header itself, and strings */ | |
471 | fh = (FDR *) (hdr->cbFdOffset) + f_idx; | |
472 | fh->issBase += hdr->cbSsOffset; | |
473 | if (fh->rss != -1) | |
474 | fh->rss = (long)fh->rss + fh->issBase; | |
475 | for (s_idx = 0; s_idx < fh->csym; s_idx++) { | |
476 | sh = (SYMR*)(hdr->cbSymOffset) + fh->isymBase + s_idx; | |
477 | sh->iss = (long) sh->iss + fh->issBase; | |
478 | sh->reserved = 0; | |
479 | } | |
480 | ||
481 | cur_fd = f_idx; | |
482 | ||
483 | /* Local symbols */ | |
484 | fh->isymBase = (int)((SYMR*)(hdr->cbSymOffset)+fh->isymBase); | |
485 | ||
486 | /* cannot fix fh->ipdFirst because it is a short */ | |
487 | #define IPDFIRST(h,fh) \ | |
488 | ((long)h->cbPdOffset + fh->ipdFirst * sizeof(PDR)) | |
489 | ||
490 | /* Optional symbols (actually used for partial_symtabs) */ | |
491 | fh->ioptBase = 0; | |
492 | fh->copt = 0; | |
493 | ||
494 | /* Aux symbols */ | |
495 | if (fh->caux) | |
496 | fh->iauxBase = hdr->cbAuxOffset + fh->iauxBase * sizeof(AUXU); | |
497 | /* Relative file descriptor table */ | |
498 | fh->rfdBase = hdr->cbRfdOffset + fh->rfdBase * sizeof(RFDT); | |
499 | ||
500 | /* Line numbers */ | |
501 | if (fh->cbLine) | |
502 | fh->cbLineOffset += hdr->cbLineOffset; | |
503 | ||
504 | /* Procedure symbols. (XXX This should be done later) */ | |
505 | code_offset = fh->adr; | |
506 | for (s_idx = 0; s_idx < fh->cpd; s_idx++) { | |
507 | unsigned name, only_ext; | |
508 | ||
509 | pr = (PDR*)(IPDFIRST(hdr,fh)) + s_idx; | |
510 | ||
511 | /* Simple rule to find files linked "-x" */ | |
512 | only_ext = fh->rss == -1; | |
513 | if (only_ext) { | |
514 | if (pr->isym == -1) { | |
515 | /* static function */ | |
516 | sh = (SYMR*)-1; | |
517 | } else { | |
518 | /* external */ | |
519 | name = hdr->cbExtOffset + pr->isym * sizeof(EXTR); | |
520 | sh = &((EXTR*)name)->asym; | |
521 | } | |
522 | } else { | |
523 | /* Full symbols */ | |
524 | sh = (SYMR*)fh->isymBase + pr->isym; | |
525 | /* Included code ? */ | |
526 | if (s_idx == 0 && pr->adr != 0) | |
527 | code_offset -= pr->adr; | |
528 | } | |
529 | ||
530 | /* Turn index into a pointer */ | |
531 | pr->isym = (long)sh; | |
532 | ||
533 | /* Fix line numbers */ | |
534 | pr->cbLineOffset += fh->cbLineOffset; | |
535 | ||
536 | /* Relocate address */ | |
537 | if (!only_ext) | |
538 | pr->adr += code_offset; | |
539 | } | |
540 | } | |
541 | ||
542 | /* External symbols: fix string */ | |
543 | for (s_idx = 0; s_idx < hdr->iextMax; s_idx++) { | |
544 | esh = (EXTR*)(hdr->cbExtOffset) + s_idx; | |
545 | esh->asym.iss = esh->asym.iss + hdr->cbSsExtOffset; | |
546 | } | |
547 | } | |
548 | ||
549 | ||
550 | /* Find a file descriptor given its index RF relative to a file CF */ | |
551 | ||
552 | static | |
553 | FDR *get_rfd( cf, rf) | |
554 | { | |
555 | register FDR *f; | |
556 | ||
557 | f = (FDR *) (cur_hdr->cbFdOffset) + cf; | |
558 | /* Object files do not have the RFD table, all refs are absolute */ | |
559 | if (f->rfdBase == 0) | |
560 | return (FDR *) (cur_hdr->cbFdOffset) + rf; | |
561 | cf = *((pRFDT) f->rfdBase + rf); | |
562 | return (FDR *) (cur_hdr->cbFdOffset) + cf; | |
563 | } | |
564 | ||
565 | /* Return a safer print NAME for a file descriptor */ | |
566 | ||
567 | static | |
568 | char *fdr_name(name) | |
569 | char *name; | |
570 | { | |
571 | if (name == (char *) -1) | |
572 | return "<stripped file>"; | |
573 | if (UNSAFE_DATA_ADDR(name)) | |
574 | return "<NFY>"; | |
575 | return name; | |
576 | } | |
577 | ||
578 | ||
579 | /* Read in and parse the symtab of the file DESC. INCREMENTAL says | |
580 | whether we are adding to the general symtab or not */ | |
581 | ||
582 | static | |
583 | read_mips_symtab( desc, incremental) | |
584 | { | |
585 | /* | |
586 | * We get here with DESC pointing to the symtab header. But we need | |
587 | * other info from the initial headers | |
588 | */ | |
589 | lseek(desc, 0L, 0); | |
590 | myread(desc, &filhdr, sizeof filhdr); | |
591 | ||
592 | read_the_mips_symtab(desc); | |
593 | ||
594 | parse_partial_symbols(cur_hdr, incremental); | |
595 | cur_hdr = 0; | |
596 | ||
597 | /* | |
598 | * Check to make sure file was compiled with -g. | |
599 | * If not, warn the user of this limitation. | |
600 | */ | |
601 | if (compare_glevel(max_glevel, GLEVEL_2) < 0) { | |
602 | if (max_gdbinfo == 0) | |
603 | printf("\n%s not compiled with -g, debugging support is limited.", symfile); | |
604 | printf("\nYou should compile with -g2 or -g3 for best debugging support.\n"); | |
605 | fflush(stdout); | |
606 | } | |
607 | ||
608 | /* | |
609 | * Dont allow char * to have a typename (else would get | |
610 | * caddr_t.) | |
611 | */ | |
612 | TYPE_NAME(lookup_pointer_type(builtin_type_char)) = 0; | |
613 | } | |
614 | ||
615 | \f | |
616 | /* Local utilities */ | |
617 | ||
618 | ||
619 | /* Map of FDR indexes to partial symtabs */ | |
620 | ||
621 | static struct pst_map { | |
622 | struct partial_symtab *pst; /* the psymtab proper */ | |
623 | int n_globals; /* globals it exports */ | |
624 | } * fdr_to_pst; | |
625 | ||
626 | ||
627 | /* Utility stack, used to nest procedures and blocks properly. | |
628 | It is a doubly linked list, to avoid too many alloc/free. | |
629 | Since we might need it quite a few times it is NOT deallocated | |
630 | after use. */ | |
631 | ||
632 | static struct parse_stack { | |
633 | struct parse_stack *next, *prev; | |
634 | struct symtab *cur_st; /* Current symtab */ | |
635 | struct block *cur_block; /* Block in it */ | |
636 | int blocktype; /* What are we parsing */ | |
637 | int maxsyms; /* Max symbols in this block */ | |
638 | struct type *cur_type; /* Type we parse fields for */ | |
639 | int procadr; /* Start addres of this procedure */ | |
640 | int numargs; /* Its argument count */ | |
641 | } *top_stack; /* Top stack ptr */ | |
642 | ||
643 | ||
644 | /* Enter a new lexical context */ | |
645 | ||
646 | static push_parse_stack() | |
647 | { | |
648 | struct parse_stack *new; | |
649 | ||
650 | /* Reuse frames if possible */ | |
651 | if (top_stack && top_stack->prev) | |
652 | new = top_stack->prev; | |
653 | else | |
654 | new = (struct parse_stack *) xzalloc(sizeof(struct parse_stack)); | |
655 | /* Initialize new frame with previous content */ | |
656 | if (top_stack) { | |
657 | register struct parse_stack *prev = new->prev; | |
658 | ||
659 | *new = *top_stack; | |
660 | top_stack->prev = new; | |
661 | new->prev = prev; | |
662 | new->next = top_stack; | |
663 | } | |
664 | top_stack = new; | |
665 | } | |
666 | ||
667 | /* Exit a lexical context */ | |
668 | ||
669 | static pop_parse_stack() | |
670 | { | |
671 | if (!top_stack) | |
672 | return; | |
673 | if (top_stack->next) | |
674 | top_stack = top_stack->next; | |
675 | } | |
676 | ||
677 | ||
678 | /* Cross-references might be to things we haven't looked at | |
679 | yet, e.g. type references. To avoid too many type | |
680 | duplications we keep a quick fixup table, an array | |
681 | of lists of references indexed by file descriptor */ | |
682 | ||
683 | static struct pending { | |
684 | struct pending *next; /* link */ | |
685 | SYMR *s; /* the symbol */ | |
686 | struct type *t; /* its partial type descriptor */ | |
687 | } **pending_list; | |
688 | ||
689 | ||
690 | /* Check whether we already saw symbol SH in file FH as undefined */ | |
691 | ||
692 | static | |
693 | struct pending *is_pending_symbol(fh, sh) | |
694 | FDR *fh; | |
695 | SYMR *sh; | |
696 | { | |
697 | int f_idx = fh - (FDR *) cur_hdr->cbFdOffset; | |
698 | register struct pending *p; | |
699 | ||
700 | /* Linear search is ok, list is typically no more than 10 deep */ | |
701 | for (p = pending_list[f_idx]; p; p = p->next) | |
702 | if (p->s == sh) | |
703 | break; | |
704 | return p; | |
705 | } | |
706 | ||
707 | /* Check whether we already saw type T in file FH as undefined */ | |
708 | ||
709 | static | |
710 | struct pending *is_pending_type(fh, t) | |
711 | FDR *fh; | |
712 | struct type *t; | |
713 | { | |
714 | int f_idx = fh - (FDR *) cur_hdr->cbFdOffset; | |
715 | register struct pending *p; | |
716 | ||
717 | for (p = pending_list[f_idx]; p; p = p->next) | |
718 | if (p->t == t) | |
719 | break; | |
720 | return p; | |
721 | } | |
722 | ||
723 | /* Add a new undef symbol SH of type T */ | |
724 | ||
725 | static | |
726 | add_pending(fh, sh, t) | |
727 | FDR *fh; | |
728 | SYMR *sh; | |
729 | struct type *t; | |
730 | { | |
731 | int f_idx = fh - (FDR *) cur_hdr->cbFdOffset; | |
732 | struct pending *p = is_pending_symbol(fh, sh); | |
733 | ||
734 | /* Make sure we do not make duplicates */ | |
735 | if (!p) { | |
736 | p = (struct pending *) xmalloc(sizeof(*p)); | |
737 | p->s = sh; | |
738 | p->t = t; | |
739 | p->next = pending_list[f_idx]; | |
740 | pending_list[f_idx] = p; | |
741 | } | |
742 | sh->reserved = 1; /* for quick check */ | |
743 | } | |
744 | ||
745 | /* Throw away undef entries when done with file index F_IDX */ | |
746 | ||
747 | static | |
748 | free_pending(f_idx) | |
749 | { | |
750 | register struct pending *p, *q; | |
751 | ||
752 | for (p = pending_list[f_idx]; p; p = q) { | |
753 | q = p->next; | |
754 | free(p); | |
755 | } | |
756 | pending_list[f_idx] = 0; | |
757 | } | |
758 | ||
759 | /* The number of args to a procedure is not explicit in the symtab, | |
760 | this is the list of all those we know of. | |
761 | This makes parsing more reasonable and avoids extra passes */ | |
762 | ||
763 | static struct numarg { | |
764 | struct numarg *next; /* link */ | |
765 | unsigned adr; /* procedure's start address */ | |
766 | unsigned num; /* arg count */ | |
767 | } *numargs_list; | |
768 | ||
769 | /* Record that the procedure at ADR takes NUM arguments. */ | |
770 | ||
771 | static | |
772 | got_numargs(adr,num) | |
773 | { | |
774 | struct numarg *n = (struct numarg *) xmalloc(sizeof(struct numarg)); | |
775 | ||
776 | n->adr = adr; | |
777 | n->num = num; | |
778 | n->next = numargs_list; | |
779 | numargs_list = n; | |
780 | } | |
781 | ||
782 | /* See if we know how many arguments the procedure at ADR takes */ | |
783 | ||
784 | static | |
785 | lookup_numargs(adr) | |
786 | { | |
787 | struct numarg *n = numargs_list; | |
788 | ||
789 | while (n && n->adr != adr) | |
790 | n = n->next; | |
791 | return (n) ? n->num : -1; | |
792 | } | |
793 | ||
794 | /* Release storage when done with this file */ | |
795 | ||
796 | static | |
797 | free_numargs() | |
798 | { | |
799 | struct numarg *n = numargs_list, *m; | |
800 | ||
801 | while (n) { | |
802 | m = n->next; | |
803 | free(n); | |
804 | n = m; | |
805 | } | |
806 | numargs_list = 0; | |
807 | } | |
808 | ||
809 | \f | |
810 | /* Parsing Routines proper. */ | |
811 | ||
812 | /* Parse a single symbol. Mostly just make up a GDB symbol for it. | |
813 | For blocks, procedures and types we open a new lexical context. | |
814 | This is basically just a big switch on the symbol's type */ | |
815 | ||
816 | static | |
817 | parse_symbol(sh, ax) | |
818 | SYMR *sh; | |
819 | AUXU *ax; | |
820 | { | |
821 | struct symbol *s; | |
822 | struct block *b; | |
823 | struct type *t; | |
824 | struct field *f; | |
825 | /* When a symbol is cross-referenced from other files/symbols | |
826 | we mark it explicitly */ | |
827 | int pend = (sh->reserved == 1); | |
828 | enum address_class class; | |
829 | ||
830 | switch (sh->st) { | |
831 | ||
832 | case stNil: | |
833 | break; | |
834 | ||
835 | case stGlobal: /* external symbol, goes into the primary block */ | |
836 | class = LOC_STATIC; | |
d219db01 JG |
837 | b = BLOCKVECTOR_BLOCK(BLOCKVECTOR(top_stack->cur_st), |
838 | GLOBAL_BLOCK); | |
bd5635a1 RP |
839 | goto data; |
840 | ||
841 | case stStatic: /* static data, goes into the current block. */ | |
842 | class = LOC_STATIC; | |
843 | b = top_stack->cur_block; | |
844 | goto data; | |
845 | ||
846 | case stLocal: /* local variable, goes into the current block */ | |
847 | if (sh->sc == scRegister) { | |
848 | class = LOC_REGISTER; | |
849 | if (sh->value > 31) | |
850 | sh->value += 6; | |
851 | } else | |
852 | class = LOC_LOCAL; | |
853 | b = top_stack->cur_block; | |
854 | ||
855 | data: /* Common code for symbols describing data */ | |
856 | s = new_symbol(sh->iss); | |
857 | SYMBOL_NAMESPACE(s) = VAR_NAMESPACE; | |
858 | SYMBOL_CLASS(s) = class; | |
859 | add_symbol(s, b); | |
860 | ||
861 | /* Type could be missing in a number of cases */ | |
862 | if (sh->sc == scUndefined || sh->sc == scNil || | |
863 | sh->index == 0xfffff) | |
864 | SYMBOL_TYPE(s) = builtin_type_int; /* undefined? */ | |
865 | else | |
866 | SYMBOL_TYPE(s) = parse_type(ax + sh->index, sh, 0); | |
867 | /* Value of a data symbol is its memory address */ | |
868 | SYMBOL_VALUE(s) = sh->value; | |
869 | break; | |
870 | ||
871 | case stParam: /* argument to procedure, goes into current block */ | |
872 | max_gdbinfo++; | |
873 | top_stack->numargs++; | |
874 | s = new_symbol(sh->iss); | |
875 | SYMBOL_NAMESPACE(s) = VAR_NAMESPACE; | |
876 | if (sh->sc == scRegister) { | |
877 | SYMBOL_CLASS(s) = LOC_REGPARM; | |
878 | if (sh->value > 31) | |
879 | sh->value += 6; | |
880 | } else | |
881 | SYMBOL_CLASS(s) = LOC_ARG; | |
882 | SYMBOL_VALUE(s) = sh->value; | |
883 | SYMBOL_TYPE(s) = parse_type(ax + sh->index, sh, 0); | |
884 | add_symbol(s, top_stack->cur_block); | |
885 | break; | |
886 | ||
887 | case stLabel: /* label, we do make a symbol for it */ | |
888 | s = new_symbol(sh->iss); | |
889 | SYMBOL_NAMESPACE(s) = VAR_NAMESPACE; /* so that it can be used */ | |
890 | SYMBOL_CLASS(s) = LOC_LABEL; /* but not misused */ | |
891 | SYMBOL_VALUE(s) = sh->value; | |
892 | SYMBOL_TYPE(s) = builtin_type_int; | |
893 | add_symbol(s, top_stack->cur_block); | |
894 | break; | |
895 | ||
896 | case stProc: /* Procedure */ | |
897 | case stStaticProc: /* Static procedure */ | |
898 | s = new_symbol(sh->iss); | |
899 | SYMBOL_NAMESPACE(s) = VAR_NAMESPACE; | |
900 | SYMBOL_CLASS(s) = LOC_BLOCK; | |
901 | /* Type of the return value */ | |
902 | if (sh->sc == scUndefined || sh->sc == scNil) | |
903 | t = builtin_type_int; | |
904 | else | |
905 | t = parse_type(ax + sh->index, sh, 0); | |
906 | add_symbol(s, top_stack->cur_block); | |
907 | ||
908 | /* Make a type for the procedure itself */ | |
909 | SYMBOL_TYPE(s) = lookup_function_type (t); | |
910 | ||
911 | /* Create and enter a new lexical context */ | |
912 | b = new_block(top_stack->maxsyms); | |
913 | SYMBOL_BLOCK_VALUE(s) = b; | |
914 | BLOCK_FUNCTION(b) = s; | |
915 | BLOCK_START(b) = BLOCK_END(b) = sh->value; | |
916 | BLOCK_SUPERBLOCK(b) = top_stack->cur_block; | |
917 | add_block(b, top_stack->cur_st); | |
918 | ||
919 | /* Not if we only have partial info */ | |
920 | if (sh->sc == scUndefined || sh->sc == scNil) | |
921 | break; | |
922 | ||
923 | push_parse_stack(); | |
924 | top_stack->cur_block = b; | |
925 | top_stack->blocktype = sh->st; | |
926 | top_stack->cur_type = SYMBOL_TYPE(s); | |
927 | top_stack->procadr = sh->value; | |
928 | top_stack->numargs = 0; | |
929 | ||
930 | sh->value = (long) SYMBOL_TYPE(s); | |
931 | break; | |
932 | ||
933 | case stBlock: /* Either a lexical block, or some type */ | |
934 | push_parse_stack(); | |
935 | top_stack->blocktype = stBlock; | |
936 | if (sh->sc == scInfo) { /* structure/union/enum def */ | |
937 | s = new_symbol(sh->iss); | |
938 | SYMBOL_NAMESPACE(s) = STRUCT_NAMESPACE; | |
939 | SYMBOL_CLASS(s) = LOC_TYPEDEF; | |
940 | SYMBOL_VALUE(s) = 0; | |
941 | add_symbol(s, top_stack->cur_block); | |
942 | /* If this type was expected, use its partial definition */ | |
943 | if (pend) { | |
944 | t = is_pending_symbol(cur_fdr, sh)->t; | |
945 | } else { | |
946 | /* Uhmm, can`t decide yet. Smash later */ | |
947 | t = new_type(sh->iss); | |
948 | TYPE_CODE(t) = TYPE_CODE_UNDEF; | |
949 | add_pending(cur_fdr, sh, t); | |
950 | } | |
951 | SYMBOL_TYPE(s) = t; | |
952 | /* make this the current type */ | |
953 | top_stack->cur_type = t; | |
954 | TYPE_LENGTH(t) = sh->value; | |
955 | /* Mark that symbol has a type, and say which one */ | |
956 | sh->value = (long) t; | |
957 | } else { | |
958 | /* beginnning of (code) block. Value of symbol | |
959 | is the displacement from procedure start */ | |
960 | b = new_block(top_stack->maxsyms); | |
961 | BLOCK_START(b) = sh->value + top_stack->procadr; | |
962 | BLOCK_SUPERBLOCK(b) = top_stack->cur_block; | |
963 | top_stack->cur_block = b; | |
964 | add_block(b, top_stack->cur_st); | |
965 | } | |
966 | break; | |
967 | ||
968 | case stEnd: /* end (of anything) */ | |
969 | if (sh->sc == scInfo) { | |
970 | /* Finished with type */ | |
971 | top_stack->cur_type = 0; | |
972 | } else if (sh->sc == scText && | |
973 | (top_stack->blocktype == stProc || | |
974 | top_stack->blocktype == stStaticProc)) { | |
975 | /* Finished with procedure */ | |
976 | struct blockvector *bv = BLOCKVECTOR(top_stack->cur_st); | |
977 | struct block *b; | |
978 | int i; | |
979 | ||
980 | BLOCK_END(top_stack->cur_block) += sh->value; /* size */ | |
981 | got_numargs(top_stack->procadr, top_stack->numargs); | |
982 | /* Reallocate symbols, saving memory */ | |
983 | b = shrink_block(top_stack->cur_block, top_stack->cur_st); | |
984 | ||
985 | /* f77 emits proc-level with address bounds==[0,0], | |
986 | So look for such child blocks, and patch them. */ | |
987 | for (i = 0; i < BLOCKVECTOR_NBLOCKS(bv); i++) { | |
988 | struct block *b_bad = BLOCKVECTOR_BLOCK(bv,i); | |
989 | if (BLOCK_SUPERBLOCK(b_bad) == b | |
990 | && BLOCK_START(b_bad) == top_stack->procadr | |
991 | && BLOCK_END(b_bad) == top_stack->procadr) { | |
992 | BLOCK_START(b_bad) = BLOCK_START(b); | |
993 | BLOCK_END(b_bad) = BLOCK_END(b); | |
994 | } | |
995 | } | |
961b4908 JK |
996 | if (entry_point < BLOCK_END(b) |
997 | && entry_point >= BLOCK_START(b)) { | |
998 | startup_file_start = BLOCK_START(b); | |
999 | startup_file_end = BLOCK_END(b); | |
1000 | } | |
bd5635a1 RP |
1001 | } else if (sh->sc == scText && top_stack->blocktype == stBlock) { |
1002 | /* End of (code) block. The value of the symbol | |
1003 | is the displacement from the procedure`s start | |
1004 | address of the end of this block. */ | |
1005 | BLOCK_END(top_stack->cur_block) = sh->value + top_stack->procadr; | |
1006 | (void) shrink_block(top_stack->cur_block, top_stack->cur_st); | |
1007 | } | |
1008 | pop_parse_stack(); /* restore previous lexical context */ | |
1009 | break; | |
1010 | ||
1011 | case stMember: /* member of struct/union/enum.. */ | |
1012 | f = new_field(top_stack->cur_type, sh->iss); | |
1013 | f->bitpos = sh->value; | |
1014 | f->type = parse_type(ax + sh->index, sh, &f->bitsize); | |
1015 | break; | |
1016 | ||
1017 | case stTypedef: /* type definition */ | |
1018 | s = new_symbol(sh->iss); | |
1019 | SYMBOL_NAMESPACE(s) = VAR_NAMESPACE; | |
1020 | SYMBOL_CLASS(s) = LOC_TYPEDEF; | |
1021 | SYMBOL_BLOCK_VALUE(s) = top_stack->cur_block; | |
1022 | add_symbol(s, top_stack->cur_block); | |
1023 | SYMBOL_TYPE(s) = parse_type(ax + sh->index, sh, 0); | |
1024 | sh->value = (long) SYMBOL_TYPE(s); | |
1025 | break; | |
1026 | ||
1027 | case stFile: /* file name */ | |
1028 | push_parse_stack(); | |
1029 | top_stack->blocktype = sh->st; | |
1030 | break; | |
1031 | ||
1032 | /* I`ve never seen these for C */ | |
1033 | case stRegReloc: | |
1034 | break; /* register relocation */ | |
1035 | case stForward: | |
1036 | break; /* forwarding address */ | |
1037 | case stConstant: | |
1038 | break; /* constant */ | |
1039 | default: | |
1040 | error("Unknown symbol type %x.", sh->st); | |
1041 | } | |
1042 | sh->st = stParsed; | |
1043 | } | |
1044 | ||
1045 | /* Parse the type information provided in the AX entries for | |
1046 | the symbol SH. Return the bitfield size in BS, in case. */ | |
1047 | ||
1048 | static struct type *parse_type(ax, sh, bs) | |
1049 | AUXU *ax; | |
1050 | SYMR *sh; | |
1051 | int *bs; | |
1052 | { | |
1053 | /* Null entries in this map are treated specially */ | |
1054 | static struct type **map_bt[] = | |
1055 | { | |
1056 | &builtin_type_void, /* btNil */ | |
1057 | 0, /* btAdr */ | |
1058 | &builtin_type_char, /* btChar */ | |
1059 | &builtin_type_unsigned_char, /* btUChar */ | |
1060 | &builtin_type_short, /* btShort */ | |
1061 | &builtin_type_unsigned_short, /* btUShort */ | |
1062 | &builtin_type_int, /* btInt */ | |
1063 | &builtin_type_unsigned_int, /* btUInt */ | |
1064 | &builtin_type_long, /* btLong */ | |
1065 | &builtin_type_unsigned_long, /* btULong */ | |
1066 | &builtin_type_float, /* btFloat */ | |
1067 | &builtin_type_double, /* btDouble */ | |
1068 | 0, /* btStruct */ | |
1069 | 0, /* btUnion */ | |
1070 | 0, /* btEnum */ | |
1071 | 0, /* btTypedef */ | |
1072 | 0, /* btRange */ | |
1073 | 0, /* btSet */ | |
1074 | &builtin_type_complex, /* btComplex */ | |
1075 | &builtin_type_double_complex, /* btDComplex */ | |
1076 | 0, /* btIndirect */ | |
1077 | &builtin_type_fixed_dec, /* btFixedDec */ | |
1078 | &builtin_type_float_dec, /* btFloatDec */ | |
1079 | &builtin_type_string, /* btString */ | |
1080 | 0, /* btBit */ | |
1081 | 0, /* btPicture */ | |
1082 | &builtin_type_void, /* btVoid */ | |
1083 | }; | |
1084 | ||
1085 | TIR *t; | |
1086 | struct type *tp = 0, *tp1; | |
1087 | char *fmt = "%s"; | |
1088 | ||
1089 | /* Procedures start off by one */ | |
1090 | if (sh->st == stProc || sh->st == stStaticProc) | |
1091 | ax++; | |
1092 | ||
1093 | /* Undefined ? Should not happen */ | |
1094 | if (ax->rndx.rfd == 0xfff) { | |
1095 | return builtin_type_void; | |
1096 | } | |
1097 | ||
1098 | /* Use aux as a type information record, map its basic type */ | |
1099 | t = &ax->ti; | |
1100 | if (t->bt > 26 || t->bt == btPicture) { | |
1101 | printf_filtered("Internal: cannot map MIPS basic type x%x\n", t->bt); | |
1102 | return builtin_type_int; | |
1103 | } | |
1104 | if (map_bt[t->bt]) | |
1105 | tp = *map_bt[t->bt]; | |
1106 | else { | |
1107 | /* Cannot use builtin types, use templates */ | |
1108 | tp = make_type(TYPE_CODE_VOID, 0, 0, 0); | |
1109 | switch (t->bt) { | |
1110 | case btAdr: | |
1111 | *tp = *builtin_type_ptr; | |
1112 | break; | |
1113 | case btStruct: | |
1114 | *tp = *builtin_type_struct; | |
1115 | fmt = "struct %s"; | |
1116 | break; | |
1117 | case btUnion: | |
1118 | *tp = *builtin_type_union; | |
1119 | fmt = "union %s"; | |
1120 | break; | |
1121 | case btEnum: | |
1122 | *tp = *builtin_type_enum; | |
1123 | fmt = "enum %s"; | |
1124 | break; | |
1125 | case btRange: | |
1126 | *tp = *builtin_type_range; | |
1127 | break; | |
1128 | case btSet: | |
1129 | *tp = *builtin_type_set; | |
1130 | fmt = "set %s"; | |
1131 | break; | |
1132 | } | |
1133 | } | |
1134 | ||
1135 | /* Move on to next aux */ | |
1136 | ax++; | |
1137 | if (t->continued) { | |
1138 | /* This is the way it would work if the compiler worked */ | |
1139 | register TIR *t1 = t; | |
1140 | while (t1->continued) | |
1141 | ax++; | |
1142 | } | |
1143 | ||
1144 | /* For bitfields all we need is the width */ | |
1145 | if (t->fBitfield) { | |
1146 | *bs = ax->width; | |
1147 | return tp; | |
1148 | } | |
1149 | ||
1150 | /* All these types really point to some (common) MIPS type | |
1151 | definition, and only the type-qualifiers fully identify | |
1152 | them. We`ll make the same effort at sharing */ | |
1153 | if (t->bt == btIndirect || | |
1154 | t->bt == btStruct || | |
1155 | t->bt == btUnion || | |
1156 | t->bt == btEnum || | |
1157 | t->bt == btTypedef || | |
1158 | t->bt == btRange || | |
1159 | t->bt == btSet) { | |
1160 | char name[256], *pn; | |
1161 | ||
1162 | /* Try to cross reference this type */ | |
1163 | tp1 = tp; | |
1164 | ax += cross_ref(ax, &tp1, &pn); | |
1165 | /* SOMEONE OUGHT TO FIX DBXREAD TO DROP "STRUCT" */ | |
1166 | sprintf(name, fmt, pn); | |
1167 | ||
1168 | /* reading .o file ? */ | |
1169 | if (UNSAFE_DATA_ADDR(tp1)) | |
1170 | tp1 = tp; | |
1171 | if (TYPE_CODE(tp1) == TYPE_CODE_UNDEF) { | |
1172 | /* | |
1173 | * Type was incompletely defined, now we know. | |
1174 | */ | |
1175 | TYPE_CODE(tp1) = TYPE_CODE(tp); | |
1176 | TYPE_NAME(tp1) = obsavestring(name, strlen(name)); | |
1177 | if (TYPE_CODE(tp1) == TYPE_CODE_ENUM) { | |
1178 | int i; | |
1179 | ||
1180 | for (i = 0; i < TYPE_NFIELDS(tp1); i++) | |
1181 | make_enum_constant(&TYPE_FIELD(tp1,i), tp1); | |
1182 | } | |
1183 | } | |
1184 | if (tp1 != tp) { | |
1185 | /* found as cross ref, rid of our template */ | |
1186 | if ((TYPE_FLAGS(tp) & TYPE_FLAG_PERM) == 0) | |
1187 | free(tp); | |
1188 | tp = tp1; | |
1189 | /* stupid idea of prepending "struct" to type names */ | |
1190 | if (t->bt == btStruct && !index(TYPE_NAME(tp), ' ')) { | |
1191 | sprintf(name, fmt, TYPE_NAME(tp)); | |
1192 | TYPE_NAME(tp) = obsavestring(name, strlen(name)); | |
1193 | } | |
1194 | } else | |
1195 | TYPE_NAME(tp) = savestring(name, strlen(name)); | |
1196 | } | |
1197 | ||
1198 | /* Deal with range types */ | |
1199 | if (t->bt == btRange) { | |
1200 | struct field *f; | |
1201 | ||
1202 | f = new_field(tp, "Low"); | |
1203 | f->bitpos = ax->dnLow; | |
1204 | ax++; | |
1205 | f = new_field(tp, "High"); | |
1206 | f->bitpos = ax->dnHigh; | |
1207 | ax++; | |
1208 | } | |
1209 | ||
1210 | /* Parse all the type qualifiers now. If there are more | |
1211 | than 6 the game will continue in the next aux */ | |
1212 | ||
1213 | #define PARSE_TQ(tq) \ | |
1214 | if (t->tq != tqNil) ax += upgrade_type(&tp, t->tq, ax, sh); | |
1215 | ||
1216 | again: PARSE_TQ(tq0); | |
1217 | PARSE_TQ(tq1); | |
1218 | PARSE_TQ(tq2); | |
1219 | PARSE_TQ(tq3); | |
1220 | PARSE_TQ(tq4); | |
1221 | PARSE_TQ(tq5); | |
1222 | #undef PARSE_TQ | |
1223 | ||
1224 | if (t->continued) { | |
1225 | t++; | |
1226 | goto again; | |
1227 | } | |
1228 | return tp; | |
1229 | } | |
1230 | ||
1231 | /* Make up a complex type from a basic one. Type is passed by | |
1232 | reference in TPP and side-effected as necessary. The type | |
1233 | qualifier TQ says how to handle the aux symbols at AX for | |
1234 | the symbol SX we are currently analyzing. | |
1235 | Returns the number of aux symbols we parsed. */ | |
1236 | ||
1237 | static | |
1238 | upgrade_type(tpp, tq, ax, sh) | |
1239 | struct type **tpp; | |
1240 | AUXU *ax; | |
1241 | SYMR *sh; | |
1242 | { | |
1243 | int off = 0; | |
1244 | int ret = 0; | |
1245 | struct type *t; | |
1246 | ||
1247 | if (tq == tqPtr) { | |
1248 | t = lookup_pointer_type (*tpp); | |
1249 | } else if (tq == tqProc) { | |
1250 | t = lookup_function_type (*tpp); | |
1251 | } else if (tq == tqArray) { | |
1252 | int rf, id; | |
1253 | FDR *fh; | |
1254 | struct field *f; | |
1255 | SYMR ss; | |
1256 | ||
1257 | t = make_type(TYPE_CODE_ARRAY, 0, 0, 0); | |
1258 | TYPE_TARGET_TYPE(t) = *tpp; | |
1259 | ||
1260 | /* Pointer to domain type (type of index) */ | |
1261 | id = ax->rndx.index; | |
1262 | if ((rf = ax->rndx.rfd) == 0xfff) | |
1263 | rf = (++ax)->isym, off++; | |
1264 | ||
1265 | fh = get_rfd(cur_fd, rf); | |
1266 | f = new_field(t, 0); | |
1267 | bzero(&ss, sizeof ss); | |
1268 | /* XXX */ f->type = parse_type(fh->iauxBase + id * sizeof(AUXU), | |
1269 | &ss, &f->bitsize); | |
1270 | ||
1271 | /* | |
1272 | * This seems to be a pointer to the end of the Block defining | |
1273 | * the type. Why it is here is magic for me, and I have no | |
1274 | * good use for it anyways. | |
1275 | */ | |
1276 | if (off == 0) { | |
1277 | off++; | |
1278 | id = (++ax)->rndx.index; | |
1279 | if ((rf = ax->rndx.rfd) == 0xfff) | |
1280 | rf = (++ax)->isym, off++; | |
1281 | } | |
1282 | f->bitpos = (++ax)->dnLow; /* ?? */ | |
1283 | f->bitsize = (++ax)->dnHigh; /* ?? */ | |
1284 | rf = (++ax)->width - 1; /* bit alignment */ | |
1285 | id = TYPE_LENGTH(TYPE_TARGET_TYPE(t)) << 3; /* bitsize */ | |
1286 | ||
1287 | if (id == 0) { | |
1288 | /* Most likely an undefined type */ | |
1289 | id = rf + 1; | |
1290 | TYPE_LENGTH(TYPE_TARGET_TYPE(t)) = id >> 3; | |
1291 | } | |
1292 | TYPE_LENGTH(t) = (f->bitsize < 0) ? 0 : | |
1293 | (f->bitsize - f->bitpos + 1) * (id >> 3); | |
1294 | ret = 4 + off; | |
1295 | } else { | |
1296 | if (tq != tqVol) | |
1297 | printf_filtered("Internal: unknown type qualifier %x\n", tq); | |
1298 | return ret; | |
1299 | } | |
1300 | ||
1301 | *tpp = t; | |
1302 | return ret; | |
1303 | } | |
1304 | ||
1305 | ||
1306 | /* Parse a procedure descriptor record PR. Note that the procedure | |
1307 | is parsed _after_ the local symbols, now we just make up the | |
1308 | extra information we need into a special symbol that we insert | |
1309 | in the procedure's main block. Note also that images that | |
1310 | have been partially stripped (ld -x) have been deprived | |
1311 | of local symbols, and we have to cope with them here. | |
1312 | The procedure's code ends at BOUND */ | |
1313 | ||
1314 | static | |
1315 | parse_procedure(pr, bound) | |
1316 | PDR *pr; | |
1317 | { | |
1318 | struct symbol *s, *i; | |
1319 | SYMR *sh = (SYMR*)pr->isym; | |
1320 | struct block *b; | |
1321 | struct mips_extra_func_info *e; | |
1322 | char name[100]; | |
1323 | char *sh_name; | |
1324 | ||
1325 | /* Reuse the MIPS record */ | |
1326 | e = (struct mips_extra_func_info *) pr; | |
1327 | e->numargs = lookup_numargs(pr->adr); | |
1328 | ||
1329 | /* Make up our special symbol */ | |
1330 | i = new_symbol(".gdbinfo."); | |
1331 | SYMBOL_VALUE(i) = (int)e; | |
1332 | SYMBOL_NAMESPACE(i) = LABEL_NAMESPACE; | |
1333 | SYMBOL_CLASS(i) = LOC_CONST; | |
1334 | SYMBOL_TYPE(i) = builtin_type_void; | |
1335 | ||
1336 | /* Make up a name for static procedures. Sigh. */ | |
1337 | if (sh == (SYMR*)-1) { | |
1338 | sprintf(name,".static_procedure@%x",pr->adr); | |
1339 | sh_name = savestring(name, strlen(name)); | |
1340 | s = NULL; | |
1341 | } | |
1342 | else { | |
1343 | sh_name = (char*)sh->iss; | |
1344 | s = mylookup_symbol(sh_name, top_stack->cur_block, | |
1345 | VAR_NAMESPACE, LOC_BLOCK); | |
1346 | } | |
1347 | if (s != 0) { | |
1348 | b = SYMBOL_BLOCK_VALUE(s); | |
1349 | } else { | |
1350 | s = new_symbol(sh_name); | |
1351 | SYMBOL_NAMESPACE(s) = VAR_NAMESPACE; | |
1352 | SYMBOL_CLASS(s) = LOC_BLOCK; | |
1353 | /* Donno its type, hope int is ok */ | |
1354 | SYMBOL_TYPE(s) = lookup_function_type (builtin_type_int); | |
1355 | add_symbol(s, top_stack->cur_block); | |
1356 | /* Wont have symbols for this one */ | |
1357 | b = new_block(2); | |
1358 | SYMBOL_BLOCK_VALUE(s) = b; | |
1359 | BLOCK_FUNCTION(b) = s; | |
1360 | BLOCK_START(b) = pr->adr; | |
1361 | BLOCK_END(b) = bound; | |
1362 | BLOCK_SUPERBLOCK(b) = top_stack->cur_block; | |
1363 | add_block(b, top_stack->cur_st); | |
1364 | } | |
1365 | e->isym = (long)s; | |
1366 | add_symbol(i,b); | |
1367 | } | |
1368 | ||
1369 | /* Parse the external symbol ES. Just call parse_symbol() after | |
1370 | making sure we know where the aux are for it. For procedures, | |
1371 | parsing of the PDRs has already provided all the needed | |
1372 | information, we only parse them if SKIP_PROCEDURES is false, | |
1373 | and only if this causes no symbol duplication */ | |
1374 | ||
1375 | static | |
1376 | parse_external(es, skip_procedures) | |
1377 | EXTR *es; | |
1378 | { | |
1379 | AUXU *ax; | |
1380 | ||
1381 | if (es->ifd != ifdNil) { | |
1382 | cur_fd = es->ifd; | |
1383 | cur_fdr = (FDR*)(cur_hdr->cbFdOffset) + cur_fd; | |
1384 | ax = (AUXU*)cur_fdr->iauxBase; | |
1385 | } else { | |
1386 | cur_fdr = (FDR*)(cur_hdr->cbFdOffset); | |
1387 | ax = 0; | |
1388 | } | |
1389 | top_stack->cur_st = cur_stab; | |
d219db01 JG |
1390 | top_stack->cur_block = BLOCKVECTOR_BLOCK(BLOCKVECTOR(top_stack->cur_st), |
1391 | GLOBAL_BLOCK); | |
bd5635a1 RP |
1392 | |
1393 | /* Reading .o files */ | |
1394 | if (es->asym.sc == scUndefined || es->asym.sc == scNil) { | |
1395 | char *what; | |
1396 | switch (es->asym.st) { | |
1397 | case stStaticProc: | |
1398 | case stProc: what = "Procedure"; n_undef_procs++; break; | |
1399 | case stGlobal: what = "Variable"; n_undef_vars++; break; | |
1400 | case stLabel: what = "Label"; n_undef_labels++; break; | |
1401 | default : what = "Symbol"; break; | |
1402 | } | |
1403 | n_undef_symbols++; | |
1404 | if (info_verbose) | |
1405 | printf_filtered("Warning: %s %s is undefined (in %s)\n", what, | |
1406 | es->asym.iss, fdr_name(cur_fdr->rss)); | |
1407 | return; | |
1408 | } | |
1409 | ||
1410 | switch (es->asym.st) { | |
1411 | case stProc: | |
1412 | /* If we have full symbols we do not need more */ | |
1413 | if (skip_procedures) | |
1414 | return; | |
1415 | if (mylookup_symbol (es->asym.iss, top_stack->cur_block, | |
1416 | VAR_NAMESPACE, LOC_BLOCK)) | |
1417 | break; | |
1418 | /* fall through */ | |
1419 | case stGlobal: | |
1420 | case stLabel: | |
1421 | /* | |
1422 | * Note that the case of a symbol with indexNil | |
1423 | * must be handled anyways by parse_symbol(). | |
1424 | */ | |
1425 | parse_symbol(&es->asym, ax); | |
1426 | break; | |
1427 | default: | |
1428 | break; | |
1429 | } | |
1430 | } | |
1431 | ||
1432 | /* Parse the line number info for file descriptor FH into | |
1433 | GDB's linetable LT. MIPS' encoding requires a little bit | |
1434 | of magic to get things out. Note also that MIPS' line | |
1435 | numbers can go back and forth, apparently we can live | |
1436 | with that and do not need to reorder our linetables */ | |
1437 | ||
1438 | static | |
1439 | parse_lines(fh, lt) | |
1440 | FDR *fh; | |
1441 | struct linetable *lt; | |
1442 | { | |
1443 | char *base = (char*)fh->cbLineOffset; | |
1444 | int i, j, k; | |
1445 | int delta, count, lineno = 0; | |
1446 | PDR *pr; | |
1447 | ||
1448 | if (base == 0) | |
1449 | return; | |
1450 | ||
1451 | /* Scan by procedure descriptors */ | |
1452 | i = 0; j = 0, k = 0; | |
1453 | for (pr = (PDR*)IPDFIRST(cur_hdr,fh); j < fh->cpd; j++, pr++) { | |
1454 | int l, halt; | |
1455 | ||
1456 | /* No code for this one */ | |
1457 | if (pr->iline == ilineNil || | |
1458 | pr->lnLow == -1 || pr->lnHigh == -1) | |
1459 | continue; | |
1460 | /* | |
1461 | * Aurgh! To know where to stop expanding we | |
1462 | * must look-ahead. | |
1463 | */ | |
1464 | for (l = 1; l < (fh->cpd - j); l++) | |
1465 | if (pr[l].iline != -1) | |
1466 | break; | |
1467 | if (l == (fh->cpd - j)) | |
1468 | halt = fh->cline; | |
1469 | else | |
1470 | halt = pr[l].iline; | |
1471 | /* | |
1472 | * When procedures are moved around the linenumbers | |
1473 | * are attributed to the next procedure up | |
1474 | */ | |
1475 | if (pr->iline >= halt) continue; | |
1476 | ||
1477 | base = (char*)pr->cbLineOffset; | |
1478 | l = pr->adr >> 2; /* in words */ | |
1479 | halt += (pr->adr >> 2) - pr->iline; | |
1480 | for (lineno = pr->lnLow; l < halt;) { | |
1481 | count = *base & 0x0f; | |
1482 | delta = *base++ >> 4; | |
1483 | if (delta == -8) { | |
1484 | delta = (base[0] << 8) | (base[1] & 0xff); | |
1485 | base += 2; | |
1486 | } | |
1487 | lineno += delta;/* first delta is 0 */ | |
1488 | k = add_line(lt, lineno, l, k); | |
1489 | l += count + 1; | |
1490 | } | |
1491 | } | |
1492 | } | |
1493 | ||
1494 | ||
1495 | /* Parse the symbols of the file described by FH, whose index is F_IDX. | |
1496 | BOUND is the highest core address of this file's procedures */ | |
1497 | ||
1498 | static | |
1499 | parse_one_file(fh, f_idx, bound) | |
1500 | FDR *fh; | |
1501 | { | |
1502 | register int s_idx; | |
1503 | SYMR *sh; | |
1504 | PDR *pr; | |
1505 | ||
1506 | /* Parse local symbols first */ | |
1507 | ||
1508 | for (s_idx = 0; s_idx < fh->csym; s_idx++) { | |
1509 | sh = (SYMR *) (fh->isymBase) + s_idx; | |
1510 | cur_sdx = s_idx; | |
1511 | parse_symbol(sh, fh->iauxBase); | |
1512 | } | |
1513 | ||
1514 | /* Procedures next, note we need to look-ahead to | |
1515 | find out where the procedure's code ends */ | |
1516 | ||
1517 | for (s_idx = 0; s_idx < fh->cpd-1; s_idx++) { | |
1518 | pr = (PDR *) (IPDFIRST(cur_hdr, fh)) + s_idx; | |
1519 | parse_procedure(pr, pr[1].adr); /* next proc up */ | |
1520 | } | |
1521 | if (fh->cpd) { | |
1522 | pr = (PDR *) (IPDFIRST(cur_hdr, fh)) + s_idx; | |
1523 | parse_procedure(pr, bound); /* next file up */ | |
1524 | } | |
1525 | ||
1526 | /* Linenumbers. At the end, check if we can save memory */ | |
1527 | parse_lines(fh, LINETABLE(cur_stab)); | |
1528 | if (LINETABLE(cur_stab)->nitems < fh->cline) | |
1529 | shrink_linetable(cur_stab); | |
1530 | } | |
1531 | ||
1532 | ||
1533 | /* Master parsing procedure. Parses the symtab described by the | |
1534 | symbolic header HDR. If INCREMENTAL is true we are called | |
1535 | by add-file and must preserve the old symtabs */ | |
1536 | static | |
1537 | parse_partial_symbols(hdr, incremental) | |
1538 | HDRR *hdr; | |
1539 | { | |
1540 | int f_idx, s_idx, h_max; | |
1541 | CORE_ADDR dummy, *prevhigh; | |
1542 | /* Running pointers */ | |
1543 | FDR *fh; | |
1544 | RFDT *rh; | |
1545 | register EXTR *esh; | |
1546 | ||
1547 | /* | |
1548 | * Big plan: | |
1549 | * | |
1550 | * Only parse the External symbols, and the Relative FDR. | |
1551 | * Fixup enough of the loader symtab to be able to use it. | |
1552 | * Allocate space only for the file`s portions we need to | |
1553 | * look at. (XXX) | |
1554 | */ | |
1555 | ||
1556 | cur_hdr = hdr; | |
1557 | max_gdbinfo = 0; | |
1558 | max_glevel = MIN_GLEVEL; | |
1559 | ||
1560 | /* Allocate the map FDR -> PST. | |
1561 | Minor hack: -O3 images might claim some global data belongs | |
1562 | to FDR -1. We`ll go along with that */ | |
1563 | fdr_to_pst = (struct pst_map *)xzalloc((hdr->ifdMax+1) * sizeof *fdr_to_pst); | |
1564 | fdr_to_pst++; | |
1565 | { | |
1566 | struct partial_symtab * pst = new_psymtab(""); | |
1567 | fdr_to_pst[-1].pst = pst; | |
1568 | pst->ldsymoff = -1; | |
1569 | } | |
1570 | ||
1571 | /* Now scan the FDRs, mostly for dependencies */ | |
1572 | for (f_idx = 0; f_idx < hdr->ifdMax; f_idx++) | |
1573 | (void) parse_fdr(f_idx, 1); | |
1574 | ||
1575 | /* Take a good guess at how many symbols we might ever need */ | |
1576 | h_max = hdr->iextMax; | |
1577 | ||
1578 | /* Parse externals: two passes because they can be ordered | |
1579 | in any way */ | |
1580 | ||
1581 | /* Pass 1: Presize and partition the list */ | |
1582 | for (s_idx = 0; s_idx < hdr->iextMax; s_idx++) { | |
1583 | esh = (EXTR *) (hdr->cbExtOffset) + s_idx; | |
1584 | fdr_to_pst[esh->ifd].n_globals++; | |
1585 | } | |
1586 | ||
1587 | if (global_psymbols.list) { | |
1588 | global_psymbols.list = (struct partial_symbol *) | |
1589 | xrealloc( global_psymbols.list, (h_max + | |
1590 | global_psymbols.size) * sizeof(struct partial_symbol)); | |
1591 | global_psymbols.next = global_psymbols.list + global_psymbols.size; | |
1592 | global_psymbols.size += h_max; | |
1593 | } else { | |
1594 | global_psymbols.list = (struct partial_symbol *) | |
1595 | xmalloc( h_max * sizeof(struct partial_symbol)); | |
1596 | global_psymbols.size = h_max; | |
1597 | global_psymbols.next = global_psymbols.list; | |
1598 | } | |
1599 | ||
1600 | s_idx = global_psymbols.next - global_psymbols.list; | |
1601 | for (f_idx = -1; f_idx < hdr->ifdMax; f_idx++) { | |
1602 | fdr_to_pst[f_idx].pst->globals_offset = s_idx; | |
1603 | s_idx += fdr_to_pst[f_idx].n_globals; | |
1604 | } | |
1605 | ||
1606 | /* Pass 2: fill in symbols */ | |
1607 | for (s_idx = 0; s_idx < hdr->iextMax; s_idx++) { | |
1608 | register struct partial_symbol *p; | |
1609 | enum misc_function_type misc_type = mf_text; | |
1610 | esh = (EXTR *) (hdr->cbExtOffset) + s_idx; | |
1611 | ||
1612 | if (esh->asym.sc == scUndefined || esh->asym.sc == scNil) | |
1613 | continue; | |
1614 | p = new_psymbol(&global_psymbols, esh->asym.iss, esh->ifd); | |
1615 | SYMBOL_VALUE(p) = esh->asym.value; | |
1616 | SYMBOL_NAMESPACE(p) = VAR_NAMESPACE; | |
1617 | ||
1618 | switch (esh->asym.st) { | |
1619 | case stProc: | |
1620 | SYMBOL_CLASS(p) = LOC_BLOCK; | |
1621 | break; | |
1622 | case stGlobal: | |
1623 | SYMBOL_CLASS(p) = LOC_STATIC; | |
1624 | misc_type = mf_data; | |
1625 | break; | |
1626 | case stLabel: | |
1627 | SYMBOL_CLASS(p) = LOC_LABEL; | |
1628 | break; | |
1629 | default: | |
1630 | misc_type = mf_unknown; | |
1631 | complain (&unknown_ext_complaint, SYMBOL_NAME(p)); | |
1632 | } | |
1633 | prim_record_misc_function (SYMBOL_NAME(p), | |
1634 | SYMBOL_VALUE(p), | |
1635 | misc_type); | |
1636 | } | |
1637 | ||
1638 | ||
1639 | /* The array (of lists) of globals must be sorted. | |
1640 | Take care, since we are at it, of pst->texthigh. | |
1641 | ||
1642 | NOTE: The way we handle textlow/high is incorrect, but good | |
1643 | enough for a first approximation. The case we fail is on a | |
1644 | file "foo.c" that looks like | |
1645 | proc1() {...} | |
1646 | #include "bar.c" -- this contains proc2() | |
1647 | proc3() {...} | |
1648 | where proc3() is attributed to bar.c. But since this is a | |
1649 | dependent file it will cause loading of foo.c as well, so | |
1650 | everything will be fine at the end. */ | |
1651 | ||
1652 | prevhigh = &dummy; | |
1653 | for (f_idx = 0; f_idx < hdr->ifdMax; f_idx++) { | |
1654 | struct partial_symtab *pst = fdr_to_pst[f_idx].pst; | |
1655 | if (pst->n_global_syms > 1) | |
1656 | qsort (global_psymbols.list + pst->globals_offset, | |
1657 | pst->n_global_syms, sizeof (struct partial_symbol), | |
1658 | compare_psymbols); | |
1659 | if (pst->textlow) { | |
1660 | *prevhigh = pst->textlow; | |
1661 | prevhigh = &pst->texthigh; | |
1662 | } | |
1663 | } | |
1664 | ||
1665 | /* Mark the last code address, and remember it for later */ | |
1666 | *prevhigh = END_OF_TEXT_SEGMENT(filhdr); | |
1667 | hdr->cbDnOffset = END_OF_TEXT_SEGMENT(filhdr); | |
1668 | ||
1669 | reorder_psymtabs(); | |
1670 | free(&fdr_to_pst[-1]); | |
1671 | fdr_to_pst = 0; | |
1672 | } | |
1673 | ||
1674 | ||
1675 | /* Do the initial analisys of the F_IDX-th file descriptor. | |
1676 | Allocates a partial symtab for it, and builds the list | |
1677 | of dependent files by recursion. LEV says at which level | |
1678 | of recursion we are called (to pretty up debug traces) */ | |
1679 | ||
1680 | static struct partial_symtab * | |
1681 | parse_fdr(f_idx, lev) | |
1682 | int f_idx; | |
1683 | { | |
1684 | register FDR *fh; | |
1685 | register struct partial_symtab *pst; | |
1686 | int s_idx, s_id0; | |
1687 | ||
1688 | fh = (FDR *) (cur_hdr->cbFdOffset) + f_idx; | |
1689 | ||
1690 | /* Use this to indicate into which symtab this file was parsed */ | |
1691 | if (fh->ioptBase) | |
1692 | return (struct partial_symtab *) fh->ioptBase; | |
1693 | ||
1694 | /* Debuggability level */ | |
1695 | if (compare_glevel(max_glevel, fh->glevel) < 0) | |
1696 | max_glevel = fh->glevel; | |
1697 | ||
1698 | /* Make a new partial_symtab */ | |
1699 | pst = new_psymtab(fh->rss); | |
1700 | if (fh->cpd == 0){ | |
1701 | pst->textlow = 0; | |
1702 | pst->texthigh = 0; | |
1703 | } else { | |
1704 | pst->textlow = fh->adr; | |
1705 | pst->texthigh = fh->cpd; /* To be fixed later */ | |
1706 | } | |
1707 | /* Reverse mapping PST -> FDR */ | |
1708 | pst->ldsymoff = f_idx; | |
1709 | ||
1710 | fdr_to_pst[f_idx].pst = pst; | |
1711 | fh->ioptBase = (int)pst; | |
1712 | ||
1713 | /* Analyze its dependencies */ | |
1714 | if (fh->crfd <= 1) | |
1715 | return pst; | |
1716 | ||
1717 | s_id0 = 0; | |
1718 | if (fh->cpd == 0) { /* If there are no functions defined here ... */ | |
1719 | /* ...then presumably a .h file: drop reverse depends .h->.c */ | |
1720 | for (; s_id0 < fh->crfd; s_id0++) { | |
1721 | RFDT *rh = (RFDT *) (fh->rfdBase) + s_id0; | |
1722 | if (*rh == f_idx) { | |
1723 | s_id0++; /* Skip self-dependency */ | |
1724 | break; | |
1725 | } | |
1726 | } | |
1727 | } | |
1728 | pst->number_of_dependencies = fh->crfd - s_id0; | |
1729 | pst->dependencies = (struct partial_symtab **) | |
1730 | obstack_alloc (psymbol_obstack, | |
1731 | pst->number_of_dependencies * sizeof(char*)); | |
1732 | for (s_idx = s_id0; s_idx < fh->crfd; s_idx++) { | |
1733 | RFDT *rh = (RFDT *) (fh->rfdBase) + s_idx; | |
1734 | ||
1735 | pst->dependencies[s_idx-s_id0] = parse_fdr(*rh, lev+1); | |
1736 | ||
1737 | } | |
1738 | ||
1739 | return pst; | |
1740 | } | |
1741 | ||
1742 | ||
1743 | /* Ancillary function to psymtab_to_symtab(). Does all the work | |
1744 | for turning the partial symtab PST into a symtab, recurring | |
1745 | first on all dependent psymtabs */ | |
1746 | ||
1747 | static void psymtab_to_symtab_1(pst) | |
1748 | struct partial_symtab *pst; | |
1749 | { | |
1750 | int i, f_max; | |
1751 | struct symtab *st; | |
1752 | FDR *fh; | |
1753 | ||
1754 | if (pst->readin) | |
1755 | return; | |
1756 | pst->readin = 1; | |
1757 | ||
1758 | pending_list = (struct pending **) cur_hdr->cbOptOffset; | |
1759 | if (pending_list == 0) { | |
1760 | pending_list = (struct pending **) | |
1761 | xzalloc(cur_hdr->ifdMax * sizeof(struct pending *)); | |
1762 | cur_hdr->cbOptOffset = (int)pending_list; | |
1763 | } | |
1764 | ||
1765 | /* How many symbols will we need */ | |
1766 | f_max = pst->n_global_syms + pst->n_static_syms; | |
1767 | if (pst->ldsymoff == -1) { | |
1768 | fh = 0; | |
1769 | st = new_symtab( "unknown", f_max, 0); | |
1770 | } else { | |
1771 | fh = (FDR *) (cur_hdr->cbFdOffset) + pst->ldsymoff; | |
1772 | f_max += fh->csym + fh->cpd; | |
1773 | st = new_symtab(pst->filename, 2 * f_max, 2 * fh->cline); | |
1774 | } | |
1775 | ||
1776 | /* | |
1777 | * Read in all partial symbtabs on which this one is dependent. | |
1778 | * NOTE that we do have circular dependencies, sigh. | |
1779 | */ | |
1780 | for (i = 0; i < pst->number_of_dependencies; i++) | |
1781 | if (!pst->dependencies[i]->readin) { | |
1782 | /* | |
1783 | * DO NOT inform about additional files that need to | |
1784 | * be read in, it would only annoy the user. | |
1785 | */ | |
1786 | psymtab_to_symtab_1(pst->dependencies[i]); | |
1787 | } | |
1788 | ||
1789 | /* Now read the symbols for this symtab */ | |
1790 | ||
1791 | cur_fd = pst->ldsymoff; | |
1792 | cur_fdr = fh; | |
1793 | cur_stab = st; | |
1794 | ||
1795 | /* Get a new lexical context */ | |
1796 | ||
1797 | push_parse_stack(); | |
1798 | top_stack->cur_st = cur_stab; | |
d219db01 JG |
1799 | top_stack->cur_block = BLOCKVECTOR_BLOCK(BLOCKVECTOR(cur_stab), |
1800 | GLOBAL_BLOCK); | |
bd5635a1 RP |
1801 | BLOCK_START(top_stack->cur_block) = fh ? fh->adr : 0; |
1802 | BLOCK_END(top_stack->cur_block) = 0; | |
1803 | top_stack->blocktype = stFile; | |
1804 | top_stack->maxsyms = f_max; | |
1805 | top_stack->cur_type = 0; | |
1806 | top_stack->procadr = 0; | |
1807 | top_stack->numargs = 0; | |
1808 | ||
1809 | /* Parse locals and procedures */ | |
1810 | if (fh) | |
1811 | parse_one_file(fh, cur_fd, (cur_fd == (cur_hdr->ifdMax - 1)) ? | |
1812 | cur_hdr->cbDnOffset : fh[1].adr); | |
1813 | ||
1814 | /* .. and our share of externals. | |
1815 | XXX use the global list to speed up things here. how ? */ | |
1816 | top_stack->blocktype = stFile; | |
1817 | top_stack->maxsyms = cur_hdr->isymMax + cur_hdr->ipdMax + cur_hdr->iextMax; | |
1818 | for (i = 0; i < cur_hdr->iextMax; i++) { | |
1819 | register EXTR *esh = (EXTR *) (cur_hdr->cbExtOffset) + i; | |
1820 | if (esh->ifd == cur_fd) | |
1821 | parse_external(esh, 1); | |
1822 | } | |
1823 | ||
1824 | /* If there are undefined, tell the user */ | |
1825 | if (n_undef_symbols) { | |
1826 | printf_filtered("File %s contains %d unresolved references:", | |
1827 | st->filename, n_undef_symbols); | |
1828 | printf_filtered("\n\t%4d variables\n\t%4d procedures\n\t%4d labels\n", | |
1829 | n_undef_vars, n_undef_procs, n_undef_labels); | |
1830 | n_undef_symbols = n_undef_labels = n_undef_vars = n_undef_procs = 0; | |
1831 | } | |
1832 | ||
1833 | pop_parse_stack(); | |
1834 | ||
1835 | /* | |
1836 | * Sort the symbol table now, we are done adding symbols to it. | |
1837 | */ | |
1838 | sort_symtab_syms(st); | |
1839 | } | |
1840 | ||
1841 | ||
1842 | ||
1843 | \f | |
1844 | /* Ancillary parsing procedures. */ | |
1845 | ||
1846 | /* Lookup the type at relative index RN. Return it in TPP | |
1847 | if found and in any event come up with its name PNAME. | |
1848 | Return value says how many aux symbols we ate */ | |
1849 | ||
1850 | static | |
1851 | cross_ref(rn, tpp, pname) | |
1852 | RNDXR *rn; | |
1853 | struct type **tpp; | |
1854 | char **pname; | |
1855 | { | |
1856 | unsigned rf; | |
1857 | ||
1858 | /* Escape index means 'the next one' */ | |
1859 | if (rn->rfd == 0xfff) | |
1860 | rf = *(unsigned *) (rn + 1); | |
1861 | else | |
1862 | rf = rn->rfd; | |
1863 | ||
1864 | if (rf == -1) { | |
1865 | /* Ooops */ | |
1866 | *pname = "<undefined>"; | |
1867 | } else { | |
1868 | /* | |
1869 | * Find the relative file descriptor and the symbol in it | |
1870 | */ | |
1871 | FDR *fh = get_rfd(cur_fd, rf); | |
1872 | SYMR *sh; | |
1873 | struct type *t; | |
1874 | ||
1875 | /* | |
1876 | * If we have processed this symbol then we left a forwarding | |
1877 | * pointer to the corresponding GDB symbol. If not, we`ll put | |
1878 | * it in a list of pending symbols, to be processed later when | |
1879 | * the file f will be. In any event, we collect the name for | |
1880 | * the type here. Which is why we made a first pass at | |
1881 | * strings. | |
1882 | */ | |
1883 | sh = (SYMR *) (fh->isymBase) + rn->index; | |
1884 | ||
1885 | /* Careful, we might be looking at .o files */ | |
1886 | *pname = (UNSAFE_DATA_ADDR(sh->iss)) ? "<undefined>" : | |
1887 | (char *) sh->iss; | |
1888 | ||
1889 | /* Have we parsed it ? */ | |
1890 | if ((!UNSAFE_DATA_ADDR(sh->value)) && (sh->st == stParsed)) { | |
1891 | t = (struct type *) sh->value; | |
1892 | *tpp = t; | |
1893 | } else { | |
1894 | struct pending *p; | |
1895 | ||
1896 | /* Avoid duplicates */ | |
1897 | p = is_pending_symbol(fh, sh); | |
1898 | ||
1899 | if (p) | |
1900 | *tpp = p->t; | |
1901 | else | |
1902 | add_pending(fh, sh, *tpp); | |
1903 | } | |
1904 | } | |
1905 | return (rn->rfd == 0xfff); | |
1906 | } | |
1907 | ||
1908 | ||
1909 | /* Quick&dirty lookup procedure, to avoid the MI ones that require | |
1910 | keeping the symtab sorted */ | |
1911 | ||
1912 | static struct symbol * | |
1913 | mylookup_symbol (name, block, namespace, class) | |
1914 | char *name; | |
1915 | register struct block *block; | |
1916 | enum namespace namespace; | |
1917 | enum address_class class; | |
1918 | { | |
1919 | register int bot, top, inc; | |
1920 | register struct symbol *sym; | |
1921 | ||
1922 | bot = 0; | |
1923 | top = BLOCK_NSYMS(block); | |
1924 | inc = name[0]; | |
1925 | while (bot < top) { | |
1926 | sym = BLOCK_SYM(block, bot); | |
1927 | if (SYMBOL_NAME(sym)[0] == inc | |
1928 | && SYMBOL_NAMESPACE(sym) == namespace | |
1929 | && SYMBOL_CLASS(sym) == class | |
1930 | && !strcmp(SYMBOL_NAME(sym), name)) | |
1931 | return sym; | |
1932 | bot++; | |
1933 | } | |
1934 | if (block = BLOCK_SUPERBLOCK (block)) | |
1935 | return mylookup_symbol (name, block, namespace, class); | |
1936 | return 0; | |
1937 | } | |
1938 | ||
1939 | ||
1940 | /* Add a new symbol S to a block B */ | |
1941 | ||
1942 | static | |
1943 | add_symbol(s,b) | |
1944 | struct symbol *s; | |
1945 | struct block *b; | |
1946 | { | |
1947 | BLOCK_SYM(b,BLOCK_NSYMS(b)++) = s; | |
1948 | if (b == top_stack->cur_block && | |
1949 | BLOCK_NSYMS(b) > top_stack->maxsyms) | |
1950 | printf_filtered("Internal: block at @%x overfilled (by %d)\n", | |
1951 | b, BLOCK_NSYMS(b) - top_stack->maxsyms); | |
1952 | } | |
1953 | ||
1954 | /* Add a new block B to a symtab S */ | |
1955 | ||
1956 | static | |
1957 | add_block(b,s) | |
1958 | struct block *b; | |
1959 | struct symtab *s; | |
1960 | { | |
1961 | struct blockvector *bv = BLOCKVECTOR(s); | |
1962 | ||
1963 | bv = (struct blockvector *)xrealloc(bv, sizeof(struct blockvector) + | |
1964 | BLOCKVECTOR_NBLOCKS(bv) * sizeof(bv->block)); | |
1965 | if (bv != BLOCKVECTOR(s)) | |
1966 | BLOCKVECTOR(s) = bv; | |
1967 | ||
1968 | BLOCKVECTOR_BLOCK(bv, BLOCKVECTOR_NBLOCKS(bv)++) = b; | |
1969 | } | |
1970 | ||
1971 | /* Add a new linenumber entry (LINENO,ADR) to a linevector LT. | |
1972 | MIPS' linenumber encoding might need more than one byte | |
1973 | to describe it, LAST is used to detect these continuation lines */ | |
1974 | ||
1975 | static | |
1976 | add_line(lt, lineno, adr, last) | |
1977 | struct linetable *lt; | |
1978 | CORE_ADDR adr; | |
1979 | { | |
1980 | if (last == 0) | |
1981 | last = -2; /* make sure we record first line */ | |
1982 | ||
1983 | if (last == lineno) /* skip continuation lines */ | |
1984 | return lineno; | |
1985 | ||
1986 | lt->item[lt->nitems].line = lineno; | |
1987 | lt->item[lt->nitems++].pc = adr << 2; | |
1988 | return lineno; | |
1989 | } | |
1990 | ||
1991 | ||
1992 | \f | |
1993 | /* Comparison functions, used when sorting things */ | |
1994 | ||
1995 | /* Symtabs must be ordered viz the code segments they cover */ | |
1996 | ||
1997 | static int | |
1998 | compare_symtabs( s1, s2) | |
1999 | struct symtab **s1, **s2; | |
2000 | { | |
2001 | /* "most specific" first */ | |
2002 | ||
2003 | register struct block *b1, *b2; | |
d219db01 JG |
2004 | b1 = BLOCKVECTOR_BLOCK(BLOCKVECTOR(*s1),GLOBAL_BLOCK); |
2005 | b2 = BLOCKVECTOR_BLOCK(BLOCKVECTOR(*s2),GLOBAL_BLOCK); | |
bd5635a1 RP |
2006 | if (BLOCK_END(b1) == BLOCK_END(b2)) |
2007 | return BLOCK_START(b1) - BLOCK_START(b2); | |
2008 | return BLOCK_END(b1) - BLOCK_END(b2); | |
2009 | } | |
2010 | ||
2011 | ||
2012 | /* Partial Symtabs, same */ | |
2013 | ||
2014 | static int | |
2015 | compare_psymtabs( s1, s2) | |
2016 | struct partial_symtab **s1, **s2; | |
2017 | { | |
2018 | /* Perf twist: put the ones with no code at the end */ | |
2019 | ||
2020 | register int a = (*s1)->textlow; | |
2021 | register int b = (*s2)->textlow; | |
2022 | if (a == 0) | |
2023 | return b; | |
2024 | if (b == 0) | |
2025 | return -a; | |
2026 | return a - b; | |
2027 | } | |
2028 | ||
2029 | ||
2030 | /* Partial symbols are compared lexicog by their print names */ | |
2031 | ||
2032 | static int | |
2033 | compare_psymbols (s1, s2) | |
2034 | register struct partial_symbol *s1, *s2; | |
2035 | { | |
2036 | register char | |
2037 | *st1 = SYMBOL_NAME(s1), | |
2038 | *st2 = SYMBOL_NAME(s2); | |
2039 | ||
2040 | return (st1[0] - st2[0] ? st1[0] - st2[0] : | |
2041 | strcmp(st1 + 1, st2 + 1)); | |
2042 | } | |
2043 | ||
2044 | /* Blocks with a smaller low bound should come first */ | |
2045 | ||
2046 | static int compare_blocks(b1,b2) | |
2047 | struct block **b1, **b2; | |
2048 | { | |
2049 | register int addr_diff; | |
2050 | ||
2051 | addr_diff = (BLOCK_START((*b1))) - (BLOCK_START((*b2))); | |
2052 | if (addr_diff == 0) | |
2053 | return (BLOCK_END((*b1))) - (BLOCK_END((*b2))); | |
2054 | return addr_diff; | |
2055 | } | |
2056 | ||
2057 | \f | |
2058 | /* Sorting and reordering procedures */ | |
2059 | ||
2060 | /* Sort the blocks of a symtab S. | |
2061 | Reorder the blocks in the blockvector by code-address, | |
2062 | as required by some MI search routines */ | |
2063 | ||
2064 | static | |
2065 | sort_blocks(s) | |
2066 | struct symtab *s; | |
2067 | { | |
2068 | struct blockvector *bv = BLOCKVECTOR(s); | |
2069 | ||
2070 | if (BLOCKVECTOR_NBLOCKS(bv) <= 2) { | |
2071 | /* Cosmetic */ | |
d219db01 JG |
2072 | if (BLOCK_END(BLOCKVECTOR_BLOCK(bv,GLOBAL_BLOCK)) == 0) |
2073 | BLOCK_START(BLOCKVECTOR_BLOCK(bv,GLOBAL_BLOCK)) = 0; | |
bd5635a1 RP |
2074 | return; |
2075 | } | |
2076 | /* | |
2077 | * This is very unfortunate: normally all functions are compiled in | |
2078 | * the order they are found, but if the file is compiled -O3 things | |
2079 | * are very different. It would be nice to find a reliable test | |
2080 | * to detect -O3 images in advance. | |
2081 | */ | |
2082 | if (BLOCKVECTOR_NBLOCKS(bv) > 3) | |
d219db01 JG |
2083 | qsort(&BLOCKVECTOR_BLOCK(bv,FIRST_LOCAL_BLOCK), |
2084 | BLOCKVECTOR_NBLOCKS(bv) - FIRST_LOCAL_BLOCK, | |
bd5635a1 RP |
2085 | sizeof(struct block *), |
2086 | compare_blocks); | |
2087 | ||
2088 | { | |
2089 | register CORE_ADDR high = 0; | |
2090 | register int i, j = BLOCKVECTOR_NBLOCKS(bv); | |
2091 | ||
d219db01 | 2092 | for (i = FIRST_LOCAL_BLOCK; i < j; i++) |
bd5635a1 RP |
2093 | if (high < BLOCK_END(BLOCKVECTOR_BLOCK(bv,i))) |
2094 | high = BLOCK_END(BLOCKVECTOR_BLOCK(bv,i)); | |
d219db01 | 2095 | BLOCK_END(BLOCKVECTOR_BLOCK(bv,GLOBAL_BLOCK)) = high; |
bd5635a1 RP |
2096 | } |
2097 | ||
d219db01 JG |
2098 | BLOCK_START(BLOCKVECTOR_BLOCK(bv,GLOBAL_BLOCK)) = |
2099 | BLOCK_START(BLOCKVECTOR_BLOCK(bv,FIRST_LOCAL_BLOCK)); | |
bd5635a1 | 2100 | |
d219db01 JG |
2101 | BLOCK_START(BLOCKVECTOR_BLOCK(bv,STATIC_BLOCK)) = |
2102 | BLOCK_START(BLOCKVECTOR_BLOCK(bv,GLOBAL_BLOCK)); | |
2103 | BLOCK_END (BLOCKVECTOR_BLOCK(bv,STATIC_BLOCK)) = | |
2104 | BLOCK_END (BLOCKVECTOR_BLOCK(bv,GLOBAL_BLOCK)); | |
bd5635a1 RP |
2105 | } |
2106 | ||
2107 | /* Sort the symtab list, as required by some search procedures. | |
2108 | We want files ordered to make them look right to users, and for | |
2109 | searching (see block_for_pc). */ | |
2110 | ||
2111 | static | |
2112 | reorder_symtabs() | |
2113 | { | |
2114 | register int i; | |
2115 | struct symtab *stab; | |
2116 | struct symtab **all_symtabs = (struct symtab **) | |
2117 | obstack_alloc (psymbol_obstack, | |
2118 | all_symtabs_count * sizeof (struct symtab *)); | |
2119 | ||
2120 | /* Create an array of pointers to all the symtabs. */ | |
2121 | for (i = 0, stab = symtab_list; | |
2122 | i < all_symtabs_count; | |
2123 | i++, stab = stab->next) { | |
2124 | all_symtabs[i] = stab; | |
2125 | /* FIXME: Only do this for new symtabs ??? */ | |
2126 | sort_blocks(all_symtabs[i]); | |
2127 | } | |
2128 | ||
2129 | qsort(all_symtabs, all_symtabs_count, | |
2130 | sizeof(struct symtab *), compare_symtabs); | |
2131 | ||
2132 | /* Re-construct the symtab list, but now it is sorted. */ | |
2133 | for (i = 0; i < all_symtabs_count-1; i++) | |
2134 | all_symtabs[i]->next = all_symtabs[i+1]; | |
2135 | all_symtabs[i]->next = 0; | |
2136 | symtab_list = all_symtabs[0]; | |
2137 | obstack_free (psymbol_obstack, all_symtabs); | |
2138 | } | |
2139 | ||
2140 | /* Sort the partial symtab list, as required by some search procedures */ | |
2141 | ||
2142 | static reorder_psymtabs() | |
2143 | { | |
2144 | register int i; | |
2145 | struct partial_symtab *pstab; | |
2146 | ||
2147 | /* | |
2148 | * PC lookups stop at the first psymtab such that | |
2149 | * textlow <= PC < texthigh | |
2150 | */ | |
2151 | /* Create an array of pointers to all the partial_symtabs. */ | |
2152 | struct partial_symtab **all_psymtabs = (struct partial_symtab **) | |
2153 | obstack_alloc (psymbol_obstack, | |
2154 | all_psymtabs_count*sizeof(struct partial_symtab*)); | |
2155 | for (i = 0, pstab = partial_symtab_list; | |
2156 | i < all_psymtabs_count; | |
2157 | i++, pstab = pstab->next) | |
2158 | all_psymtabs[i] = pstab; | |
2159 | ||
2160 | qsort(all_psymtabs, all_psymtabs_count, | |
2161 | sizeof(struct partial_symtab *), compare_psymtabs); | |
2162 | ||
2163 | /* Re-construct the partial_symtab_list, but now it is sorted. */ | |
2164 | ||
2165 | for (i = 0; i < all_psymtabs_count-1; i++) | |
2166 | all_psymtabs[i]->next = all_psymtabs[i+1]; | |
2167 | all_psymtabs[i]->next = 0; | |
2168 | partial_symtab_list = all_psymtabs[0]; | |
2169 | ||
2170 | obstack_free (psymbol_obstack, all_psymtabs); | |
2171 | } | |
2172 | ||
2173 | ||
2174 | \f | |
2175 | /* Constructor/restructor/destructor procedures */ | |
2176 | ||
2177 | /* Allocate a new symtab for NAME. Needs an estimate of how many symbols | |
2178 | MAXSYMS and linenumbers MAXLINES we'll put in it */ | |
2179 | ||
2180 | static | |
2181 | struct symtab * | |
2182 | new_symtab(name, maxsyms, maxlines) | |
2183 | char *name; | |
2184 | { | |
2185 | struct symtab *s = (struct symtab *) xzalloc(sizeof(struct symtab)); | |
2186 | int i; | |
2187 | ||
2188 | LINETABLE(s) = new_linetable(maxlines); | |
2189 | ||
2190 | s->filename = name; | |
2191 | ||
2192 | /* All symtabs must have at least two blocks */ | |
2193 | BLOCKVECTOR(s) = new_bvect(2); | |
d219db01 JG |
2194 | BLOCKVECTOR_BLOCK(BLOCKVECTOR(s), GLOBAL_BLOCK) = new_block(maxsyms); |
2195 | BLOCKVECTOR_BLOCK(BLOCKVECTOR(s), STATIC_BLOCK) = new_block(maxsyms); | |
2196 | BLOCK_SUPERBLOCK( BLOCKVECTOR_BLOCK(BLOCKVECTOR(s),STATIC_BLOCK)) = | |
2197 | BLOCKVECTOR_BLOCK(BLOCKVECTOR(s), GLOBAL_BLOCK); | |
bd5635a1 RP |
2198 | |
2199 | s->free_code = free_linetable; | |
2200 | ||
2201 | /* Link the new symtab into the list of such. */ | |
2202 | s->next = symtab_list; | |
2203 | symtab_list = s; | |
2204 | ||
2205 | all_symtabs_count++; | |
2206 | ||
2207 | return s; | |
2208 | } | |
2209 | ||
2210 | /* Cleanup before loading a fresh image */ | |
2211 | ||
2212 | static destroy_all_symtabs() | |
2213 | { | |
2214 | if (symfile) | |
2215 | free(symfile); | |
2216 | symfile = 0; | |
2217 | ||
2218 | free_all_symtabs(); | |
2219 | all_symtabs_count = 0; | |
2220 | current_source_symtab = 0; | |
2221 | /* psymtabs! */ | |
2222 | } | |
2223 | ||
2224 | /* Allocate a new partial_symtab NAME */ | |
2225 | ||
2226 | static struct partial_symtab * | |
2227 | new_psymtab(name) | |
2228 | char *name; | |
2229 | { | |
2230 | struct partial_symtab *pst; | |
2231 | ||
2232 | pst = (struct partial_symtab *) | |
2233 | obstack_alloc (psymbol_obstack, sizeof (*pst)); | |
2234 | bzero (pst, sizeof (*pst)); | |
2235 | ||
2236 | if (name == (char*)-1) /* FIXME -- why not null here? */ | |
2237 | pst->filename = "<no name>"; | |
2238 | else | |
2239 | pst->filename = name; | |
2240 | ||
2241 | pst->next = partial_symtab_list; | |
2242 | partial_symtab_list = pst; | |
2243 | all_psymtabs_count++; | |
2244 | ||
2245 | /* Keep a backpointer to the file`s symbols */ | |
2246 | pst->ldsymlen = (int)cur_hdr; | |
2247 | ||
2248 | /* The way to turn this into a symtab is to call... */ | |
2249 | pst->read_symtab = mipscoff_psymtab_to_symtab; | |
2250 | ||
2251 | return pst; | |
2252 | } | |
2253 | ||
2254 | ||
2255 | /* Allocate a new NAME psymbol from LIST, extending it if necessary. | |
2256 | The psymbol belongs to the psymtab at index PST_IDX */ | |
2257 | ||
2258 | static struct partial_symbol * | |
2259 | new_psymbol(list, name, pst_idx) | |
2260 | struct psymbol_allocation_list *list; | |
2261 | char *name; | |
2262 | { | |
2263 | struct partial_symbol *p; | |
2264 | struct partial_symtab *pst = fdr_to_pst[pst_idx].pst; | |
2265 | ||
2266 | /* Lists are pre-sized, we won`t overflow */ | |
2267 | ||
2268 | p = list->list + pst->globals_offset + pst->n_global_syms++; | |
2269 | SYMBOL_NAME(p) = name; | |
2270 | return p; | |
2271 | } | |
2272 | ||
2273 | ||
2274 | /* Allocate a linetable array of the given SIZE */ | |
2275 | ||
2276 | static | |
2277 | struct linetable *new_linetable(size) | |
2278 | { | |
2279 | struct linetable *l; | |
2280 | ||
2281 | size = size * sizeof(l->item) + sizeof(struct linetable); | |
2282 | l = (struct linetable *)xmalloc(size); | |
2283 | l->nitems = 0; | |
2284 | return l; | |
2285 | } | |
2286 | ||
2287 | /* Oops, too big. Shrink it. This was important with the 2.4 linetables, | |
2288 | I am not so sure about the 3.4 ones */ | |
2289 | ||
2290 | static shrink_linetable(s) | |
2291 | struct symtab *s; | |
2292 | { | |
2293 | struct linetable *l = new_linetable(LINETABLE(s)->nitems); | |
2294 | ||
2295 | bcopy(LINETABLE(s), l, | |
2296 | LINETABLE(s)->nitems * sizeof(l->item) + sizeof(struct linetable)); | |
2297 | free (LINETABLE(s)); | |
2298 | LINETABLE(s) = l; | |
2299 | } | |
2300 | ||
2301 | /* Allocate and zero a new blockvector of NBLOCKS blocks. */ | |
2302 | ||
2303 | static | |
2304 | struct blockvector *new_bvect(nblocks) | |
2305 | { | |
2306 | struct blockvector *bv; | |
2307 | int size; | |
2308 | ||
2309 | size = sizeof(struct blockvector) + nblocks * sizeof(struct block*); | |
2310 | bv = (struct blockvector *) xzalloc(size); | |
2311 | ||
2312 | BLOCKVECTOR_NBLOCKS(bv) = nblocks; | |
2313 | ||
2314 | return bv; | |
2315 | } | |
2316 | ||
2317 | /* Allocate and zero a new block of MAXSYMS symbols */ | |
2318 | ||
2319 | static | |
2320 | struct block *new_block(maxsyms) | |
2321 | { | |
2322 | int size = sizeof(struct block) + (maxsyms-1) * sizeof(struct symbol *); | |
2323 | struct block *b = (struct block *)xzalloc(size); | |
2324 | ||
2325 | return b; | |
2326 | } | |
2327 | ||
2328 | /* Ooops, too big. Shrink block B in symtab S to its minimal size */ | |
2329 | ||
2330 | static struct block * | |
2331 | shrink_block(b, s) | |
2332 | struct block *b; | |
2333 | struct symtab *s; | |
2334 | { | |
2335 | struct block *new; | |
2336 | struct blockvector *bv = BLOCKVECTOR(s); | |
2337 | int i; | |
2338 | ||
2339 | /* Just get a new one, copy, and fix references to the old one */ | |
2340 | ||
2341 | new = (struct block *)xmalloc(sizeof(struct block) + | |
2342 | (BLOCK_NSYMS(b)-1) * sizeof(struct symbol *)); | |
2343 | ||
2344 | bcopy(b, new, sizeof(*new) + (BLOCK_NSYMS(b) - 1) * sizeof(struct symbol*)); | |
2345 | ||
2346 | /* Should chase pointers to old one. Fortunately, that`s just | |
2347 | the block`s function and inferior blocks */ | |
2348 | if (BLOCK_FUNCTION(b) && SYMBOL_BLOCK_VALUE(BLOCK_FUNCTION(b)) == b) | |
2349 | SYMBOL_BLOCK_VALUE(BLOCK_FUNCTION(b)) = new; | |
2350 | for (i = 0; i < BLOCKVECTOR_NBLOCKS(bv); i++) | |
2351 | if (BLOCKVECTOR_BLOCK(bv,i) == b) | |
2352 | BLOCKVECTOR_BLOCK(bv,i) = new; | |
2353 | else if (BLOCK_SUPERBLOCK(BLOCKVECTOR_BLOCK(bv,i)) == b) | |
2354 | BLOCK_SUPERBLOCK(BLOCKVECTOR_BLOCK(bv,i)) = new; | |
2355 | free(b); | |
2356 | return new; | |
2357 | } | |
2358 | ||
2359 | /* Create a new symbol with printname NAME */ | |
2360 | ||
2361 | static | |
2362 | struct symbol * | |
2363 | new_symbol(name) | |
2364 | char *name; | |
2365 | { | |
2366 | struct symbol *s = (struct symbol *) | |
2367 | obstack_alloc (symbol_obstack, sizeof (struct symbol)); | |
2368 | ||
2369 | bzero (s, sizeof (*s)); | |
2370 | SYMBOL_NAME(s) = name; | |
2371 | return s; | |
2372 | } | |
2373 | ||
2374 | /* Create a new type with printname NAME */ | |
2375 | ||
2376 | static | |
2377 | struct type * | |
2378 | new_type(name) | |
2379 | char *name; | |
2380 | { | |
2381 | struct type *t = (struct type *) | |
2382 | obstack_alloc (symbol_obstack, sizeof (struct type)); | |
2383 | ||
2384 | bzero (t, sizeof (*t)); | |
2385 | TYPE_NAME(t) = name; | |
2386 | return t; | |
2387 | } | |
2388 | ||
2389 | /* Create and initialize a new type with printname NAME. | |
2390 | CODE and LENGTH are the initial info we put in, | |
2391 | UNS says whether the type is unsigned or not. */ | |
2392 | ||
2393 | static | |
2394 | struct type * | |
2395 | make_type(code, length, uns, name) | |
2396 | enum type_code code; | |
2397 | int length, uns; | |
2398 | char *name; | |
2399 | { | |
2400 | register struct type *type; | |
2401 | ||
2402 | type = (struct type *) xzalloc(sizeof(struct type)); | |
2403 | TYPE_CODE(type) = code; | |
2404 | TYPE_LENGTH(type) = length; | |
2405 | TYPE_FLAGS(type) = uns ? TYPE_FLAG_UNSIGNED : 0; | |
2406 | TYPE_NAME(type) = name; | |
2407 | ||
2408 | return type; | |
2409 | } | |
2410 | ||
2411 | /* Allocate a new field named NAME to the type TYPE */ | |
2412 | ||
2413 | static | |
2414 | struct field *new_field(type,name) | |
2415 | struct type *type; | |
2416 | char *name; | |
2417 | { | |
2418 | struct field *f; | |
2419 | ||
2420 | /* Fields are kept in an array */ | |
2421 | if (TYPE_NFIELDS(type)) | |
2422 | TYPE_FIELDS(type) = (struct field*)xrealloc(TYPE_FIELDS(type), | |
2423 | (TYPE_NFIELDS(type)+1) * sizeof(struct field)); | |
2424 | else | |
2425 | TYPE_FIELDS(type) = (struct field*)xzalloc(2*sizeof(struct field)); | |
2426 | f = &(TYPE_FIELD(type,TYPE_NFIELDS(type)++)); | |
2427 | bzero(f, sizeof(struct field)); | |
2428 | if (name) | |
2429 | f->name = name; | |
2430 | return f; | |
2431 | } | |
2432 | ||
2433 | /* Make an enum constant for a member F of an enumerated type T */ | |
2434 | ||
2435 | static | |
2436 | make_enum_constant(f,t) | |
2437 | struct field *f; | |
2438 | struct type *t; | |
2439 | { | |
2440 | struct symbol *s; | |
2441 | /* | |
2442 | * This is awful, but that`s the way it is supposed to be | |
2443 | * (BTW, no need to free the real 'type', it's a builtin) | |
2444 | */ | |
2445 | f->type = (struct type *) f->bitpos; | |
2446 | ||
2447 | s = new_symbol(f->name); | |
2448 | SYMBOL_NAMESPACE(s) = VAR_NAMESPACE; | |
2449 | SYMBOL_CLASS(s) = LOC_CONST; | |
2450 | SYMBOL_TYPE(s) = t; | |
2451 | SYMBOL_VALUE(s) = f->bitpos; | |
2452 | add_symbol(s, top_stack->cur_block); | |
2453 | } | |
2454 | ||
2455 | ||
2456 | \f | |
2457 | /* Things used for calling functions in the inferior. | |
2458 | These functions are exported to our companion | |
2459 | mips-dep.c file and are here because they play | |
2460 | with the symbol-table explicitly. */ | |
2461 | ||
2462 | #if 0 | |
2463 | /* Need to make a new symbol on the fly for the dummy | |
2464 | frame we put on the stack. Which goes in the.. */ | |
2465 | ||
2466 | static struct symtab *dummy_symtab; | |
2467 | ||
2468 | /* Make up a dummy symbol for the code we put at END_PC, | |
2469 | of size SIZE, invoking a function with NARGS arguments | |
2470 | and using a frame of FRAMESIZE bytes */ | |
2471 | ||
2472 | mips_create_dummy_symbol(end_pc, size, nargs, framesize) | |
2473 | { | |
2474 | struct block *bl; | |
2475 | struct symbol *g; | |
2476 | struct mips_extra_func_info *gdbinfo; | |
2477 | ||
2478 | /* Allocate symtab if not done already */ | |
2479 | if (dummy_symtab == 0) | |
2480 | dummy_symtab = new_symtab(".dummy_symtab.", 100, 0); | |
2481 | ||
2482 | /* Make a new block. Only needs one symbol */ | |
2483 | bl = new_block(1); | |
2484 | BLOCK_START(bl) = end_pc - size; | |
2485 | BLOCK_END(bl) = end_pc; | |
2486 | ||
d219db01 JG |
2487 | BLOCK_SUPERBLOCK(bl) = |
2488 | BLOCKVECTOR_BLOCK(BLOCKVECTOR(dummy_symtab),GLOBAL_BLOCK); | |
bd5635a1 RP |
2489 | add_block(bl, dummy_symtab); |
2490 | sort_blocks(dummy_symtab); | |
2491 | ||
2492 | BLOCK_FUNCTION(bl) = new_symbol("??"); | |
2493 | SYMBOL_BLOCK_VALUE(BLOCK_FUNCTION(bl)) = bl; | |
2494 | g = new_symbol(".gdbinfo."); | |
2495 | BLOCK_SYM(bl,BLOCK_NSYMS(bl)++) = g; | |
2496 | ||
2497 | SYMBOL_NAMESPACE(g) = LABEL_NAMESPACE; | |
2498 | SYMBOL_CLASS(g) = LOC_CONST; | |
2499 | SYMBOL_TYPE(g) = builtin_type_void; | |
2500 | gdbinfo = (struct mips_extra_func_info *) | |
2501 | xzalloc(sizeof(struct mips_extra_func_info)); | |
2502 | ||
2503 | SYMBOL_VALUE(g) = (long) gdbinfo; | |
2504 | ||
2505 | gdbinfo->numargs = nargs; | |
2506 | gdbinfo->framesize = framesize; | |
2507 | gdbinfo->framereg = 29; | |
2508 | gdbinfo->pcreg = 31; | |
2509 | gdbinfo->regmask = -2; | |
2510 | gdbinfo->regoffset = -4; | |
2511 | gdbinfo->fregmask = 0; /* XXX */ | |
2512 | gdbinfo->fregoffset = 0; /* XXX */ | |
2513 | } | |
2514 | ||
2515 | /* We just returned from the dummy code at END_PC, drop its symbol */ | |
2516 | ||
2517 | mips_destroy_dummy_symbol(end_pc) | |
2518 | { | |
2519 | struct block *bl; | |
2520 | struct blockvector *bv = BLOCKVECTOR(dummy_symtab); | |
2521 | int i; | |
2522 | ||
2523 | bl = block_for_pc(end_pc); | |
2524 | free(BLOCK_FUNCTION(bl)); | |
2525 | free(SYMBOL_VALUE(BLOCK_SYM(bl,0))); | |
2526 | free(BLOCK_SYM(bl,0)); | |
2527 | ||
d219db01 | 2528 | for (i = FIRST_LOCAL_BLOCK; i < BLOCKVECTOR_NBLOCKS(bv); i++) |
bd5635a1 RP |
2529 | if (BLOCKVECTOR_BLOCK(bv,i) == bl) |
2530 | break; | |
2531 | for (; i < BLOCKVECTOR_NBLOCKS(bv) - 1; i++) | |
2532 | BLOCKVECTOR_BLOCK(bv,i) = BLOCKVECTOR_BLOCK(bv,i+1); | |
2533 | BLOCKVECTOR_NBLOCKS(bv)--; | |
2534 | sort_blocks(dummy_symtab); | |
2535 | free(bl); | |
2536 | } | |
2537 | #endif | |
2538 | ||
2539 | /* Sigtramp: make sure we have all the necessary information | |
2540 | about the signal trampoline code. Since the official code | |
2541 | from MIPS does not do so, we make up that information ourselves. | |
2542 | If they fix the library (unlikely) this code will neutralize itself. */ | |
2543 | ||
2544 | static | |
2545 | fixup_sigtramp() | |
2546 | { | |
2547 | struct symbol *s; | |
2548 | struct symtab *st; | |
2549 | struct block *b, *b0; | |
2550 | ||
2551 | sigtramp_address = -1; | |
2552 | ||
2553 | /* We know it is sold as sigvec */ | |
2554 | s = lookup_symbol("sigvec", 0, VAR_NAMESPACE, 0, NULL); | |
2555 | ||
2556 | /* Most programs do not play with signals */ | |
2557 | if (s == 0) | |
2558 | return; | |
2559 | ||
2560 | b0 = SYMBOL_BLOCK_VALUE(s); | |
2561 | ||
2562 | /* A label of sigvec, to be more precise */ | |
2563 | s = lookup_symbol("sigtramp", b0, VAR_NAMESPACE, 0, NULL); | |
2564 | ||
2565 | /* But maybe this program uses its own version of sigvec */ | |
2566 | if (s == 0) | |
2567 | return; | |
2568 | ||
2569 | sigtramp_address = SYMBOL_VALUE(s); | |
2570 | sigtramp_end = sigtramp_address + 0x88; /* black magic */ | |
2571 | ||
2572 | /* Did we or MIPSco fix the library ? */ | |
2573 | if (SYMBOL_CLASS(s) == LOC_BLOCK) | |
2574 | return; | |
2575 | ||
2576 | /* But what symtab does it live in ? */ | |
2577 | st = find_pc_symtab(SYMBOL_VALUE(s)); | |
2578 | ||
2579 | /* | |
2580 | * Ok, there goes the fix: turn it into a procedure, with all the | |
2581 | * needed info. Note we make it a nested procedure of sigvec, | |
2582 | * which is the way the (assembly) code is actually written. | |
2583 | */ | |
2584 | SYMBOL_NAMESPACE(s) = VAR_NAMESPACE; | |
2585 | SYMBOL_CLASS(s) = LOC_BLOCK; | |
2586 | SYMBOL_TYPE(s) = make_type(TYPE_CODE_FUNC, 4, 0, 0); | |
2587 | TYPE_TARGET_TYPE(SYMBOL_TYPE(s)) = builtin_type_void; | |
2588 | ||
2589 | /* Need a block to allocate .gdbinfo. in */ | |
2590 | b = new_block(1); | |
2591 | SYMBOL_BLOCK_VALUE(s) = b; | |
2592 | BLOCK_START(b) = sigtramp_address; | |
2593 | BLOCK_END(b) = sigtramp_end; | |
2594 | BLOCK_FUNCTION(b) = s; | |
2595 | BLOCK_SUPERBLOCK(b) = BLOCK_SUPERBLOCK(b0); | |
2596 | add_block(b, st); | |
2597 | sort_blocks(st); | |
2598 | ||
2599 | /* Make a .gdbinfo. for it */ | |
2600 | { | |
2601 | struct mips_extra_func_info *e = | |
2602 | (struct mips_extra_func_info *) | |
2603 | xzalloc(sizeof(struct mips_extra_func_info)); | |
2604 | ||
2605 | e->numargs = 0; /* the kernel thinks otherwise */ | |
2606 | /* align_longword(sigcontext + SIGFRAME) */ | |
2607 | e->framesize = 0x150; | |
2608 | e->framereg = SP_REGNUM; | |
2609 | e->pcreg = 31; | |
2610 | e->regmask = -2; | |
2611 | e->regoffset = -(41 * sizeof(int)); | |
2612 | e->fregmask = -1; | |
2613 | e->fregoffset = -(37 * sizeof(int)); | |
2614 | e->isym = (long)s; | |
2615 | ||
2616 | s = new_symbol(".gdbinfo."); | |
2617 | SYMBOL_VALUE(s) = (int) e; | |
2618 | SYMBOL_NAMESPACE(s) = LABEL_NAMESPACE; | |
2619 | SYMBOL_CLASS(s) = LOC_CONST; | |
2620 | SYMBOL_TYPE(s) = builtin_type_void; | |
2621 | } | |
2622 | ||
2623 | BLOCK_SYM(b,BLOCK_NSYMS(b)++) = s; | |
2624 | } | |
2625 | ||
2626 | \f | |
2627 | /* Initialization */ | |
2628 | ||
2629 | static struct sym_fns ecoff_sym_fns = {"ecoff", 5, | |
2630 | mipscoff_new_init, mipscoff_symfile_init, | |
2631 | mipscoff_symfile_read, mipscoff_symfile_discard}; | |
2632 | ||
2633 | _initialize_mipsread () | |
2634 | { | |
2635 | add_symtab_fns (&ecoff_sym_fns); | |
2636 | ||
2637 | bzero (&global_psymbols, sizeof (global_psymbols)); | |
2638 | bzero (&static_psymbols, sizeof (static_psymbols)); | |
2639 | ||
bd5635a1 RP |
2640 | /* Missing basic types */ |
2641 | builtin_type_string = make_type(TYPE_CODE_PASCAL_ARRAY, | |
2642 | 1, 0, "string"); | |
2643 | builtin_type_complex = make_type(TYPE_CODE_FLT, | |
2644 | 2 * sizeof(float), 0, "complex"); | |
2645 | builtin_type_double_complex = make_type(TYPE_CODE_FLT, | |
2646 | 2 * sizeof(double), 0, "double_complex"); | |
2647 | builtin_type_fixed_dec = make_type(TYPE_CODE_INT, sizeof(int), | |
2648 | 0, "fixed_decimal"); | |
2649 | builtin_type_float_dec = make_type(TYPE_CODE_FLT, sizeof(double), | |
2650 | 0, "floating_decimal"); | |
2651 | ||
2652 | /* Templates types */ | |
2653 | builtin_type_ptr = lookup_pointer_type (builtin_type_void); | |
2654 | builtin_type_struct = make_type(TYPE_CODE_STRUCT, 0, 0, 0); | |
2655 | builtin_type_union = make_type(TYPE_CODE_UNION, 0, 0, 0); | |
2656 | builtin_type_enum = make_type(TYPE_CODE_ENUM, 0, 0, 0); | |
2657 | builtin_type_range = make_type(TYPE_CODE_RANGE, 0, 0, 0); | |
2658 | builtin_type_set = make_type(TYPE_CODE_SET, 0, 0, 0); | |
2659 | } |