|
|
1.1 root 1: /* Read dbx symbol tables and convert to internal format, for GDB.
2: Copyright (C) 1986, 1987, 1988 Free Software Foundation, Inc.
3:
4: GDB is distributed in the hope that it will be useful, but WITHOUT ANY
5: WARRANTY. No author or distributor accepts responsibility to anyone
6: for the consequences of using it or for whether it serves any
7: particular purpose or works at all, unless he says so in writing.
8: Refer to the GDB General Public License for full details.
9:
10: Everyone is granted permission to copy, modify and redistribute GDB,
11: but only under the conditions described in the GDB General Public
12: License. A copy of this license is supposed to have been given to you
13: along with GDB so you can know your rights and responsibilities. It
14: should be in a file named COPYING. Among other things, the copyright
15: notice and this notice must be preserved on all copies.
16:
17: In other words, go ahead and share GDB, but don't try to stop
18: anyone else from sharing it farther. Help stamp out software hoarding!
19: */
20:
21: #include "param.h"
22:
23: #ifdef READ_DBX_FORMAT
24:
25: #include <a.out.h>
26: #include <stab.h>
27: #include <stdio.h>
28: #include <obstack.h>
29: #include <sys/param.h>
30: #include <sys/file.h>
31: #include <sys/stat.h>
32: #include "defs.h"
33: #include "initialize.h"
34: #include "symtab.h"
35:
36: static void add_symbol_to_list ();
37: static void read_dbx_symtab ();
38: static void process_one_symbol ();
39: static struct type *read_type ();
40: static struct type *read_range_type ();
41: static struct type *read_enum_type ();
42: static struct type *read_struct_type ();
43: static long read_number ();
44: static void finish_block ();
45: static struct blockvector *make_blockvector ();
46: static struct symbol *define_symbol ();
47: static void start_subfile ();
48: static int hashname ();
49: static void hash_symsegs ();
50:
51: extern struct symtab *read_symsegs ();
52: extern void free_all_symtabs ();
53:
54: /* Macro for number of symbol table entries (in usual a.out format).
55: Some machines override this definition. */
56: #ifndef NUMBER_OF_SYMBOLS
57: #define NUMBER_OF_SYMBOLS (hdr.a_syms / sizeof (struct nlist))
58: #endif
59:
60: /* Macro for file-offset of symbol table (in usual a.out format). */
61: #ifndef SYMBOL_TABLE_OFFSET
62: #define SYMBOL_TABLE_OFFSET N_SYMOFF (hdr)
63: #endif
64:
65: /* Macro for file-offset of string table (in usual a.out format). */
66: #ifndef STRING_TABLE_OFFSET
67: #define STRING_TABLE_OFFSET (N_SYMOFF (hdr) + hdr.a_syms)
68: #endif
69:
70: /* Macro to store the length of the string table data in INTO. */
71: #ifndef READ_STRING_TABLE_SIZE
72: #define READ_STRING_TABLE_SIZE(INTO) \
73: { val = myread (desc, &INTO, sizeof INTO); \
74: if (val < 0) perror_with_name (name); }
75: #endif
76:
77: /* Macro to declare variables to hold the file's header data. */
78: #ifndef DECLARE_FILE_HEADERS
79: #define DECLARE_FILE_HEADERS struct exec hdr
80: #endif
81:
82: /* Macro to read the header data from descriptor DESC and validate it.
83: NAME is the file name, for error messages. */
84: #ifndef READ_FILE_HEADERS
85: #define READ_FILE_HEADERS(DESC, NAME) \
86: { val = myread (DESC, &hdr, sizeof hdr); \
87: if (val < 0) perror_with_name (NAME); \
88: if (N_BADMAG (hdr)) \
89: error ("File \"%s\" not in executable format.", NAME); }
90: #endif
91:
92: START_FILE
93:
94: /* Chain of symtabs made from reading the file's symsegs.
95: These symtabs do not go into symtab_list themselves,
96: but the information is copied from them when appropriate
97: to make the symtabs that will exist permanently. */
98:
99: static struct symtab *symseg_chain;
100:
101: /* Symseg symbol table for the file whose data we are now processing.
102: It is one of those in symseg_chain. Or 0, for a compilation that
103: has no symseg. */
104:
105: static struct symtab *current_symseg;
106:
107: /* Name of source file whose symbol data we are now processing.
108: This comes from a symbol of type N_SO. */
109:
110: static char *last_source_file;
111:
112: /* Core address of start of text of current source file.
113: This too comes from the N_SO symbol. */
114:
115: static CORE_ADDR last_source_start_addr;
116:
117: /* End of the text segment of the executable file,
118: as found in the symbol _etext. */
119:
120: static CORE_ADDR end_of_text_addr;
121:
122: /* The list of sub-source-files within the current individual compilation.
123: Each file gets its own symtab with its own linetable and associated info,
124: but they all share one blockvector. */
125:
126: struct subfile
127: {
128: struct subfile *next;
129: char *name;
130: struct linetable *line_vector;
131: int line_vector_length;
132: int line_vector_index;
133: int prev_line_number;
134: };
135:
136: static struct subfile *subfiles;
137:
138: static struct subfile *current_subfile;
139:
140: /* Count symbols as they are processed, for error messages. */
141:
142: static int symnum;
143:
144: /* Vector of types defined so far, indexed by their dbx type numbers.
145: (In newer sun systems, dbx uses a pair of numbers in parens,
146: as in "(SUBFILENUM,NUMWITHINSUBFILE)". Then these numbers must be
147: translated through the type_translations hash table to get
148: the index into the type vector.) */
149:
150: static struct typevector *type_vector;
151:
152: /* Number of elements allocated for type_vector currently. */
153:
154: static int type_vector_length;
155:
156: /* Vector of line number information. */
157:
158: static struct linetable *line_vector;
159:
160: /* Index of next entry to go in line_vector_index. */
161:
162: static int line_vector_index;
163:
164: /* Last line number recorded in the line vector. */
165:
166: static int prev_line_number;
167:
168: /* Number of elements allocated for line_vector currently. */
169:
170: static int line_vector_length;
171:
172: /* Hash table of global symbols whose values are not known yet.
173: They are chained thru the SYMBOL_VALUE, since we don't
174: have the correct data for that slot yet. */
175:
176: #define HASHSIZE 127
177: static struct symbol *global_sym_chain[HASHSIZE];
178:
179: /* Record the symbols defined for each context in a list.
180: We don't create a struct block for the context until we
181: know how long to make it. */
182:
183: #define PENDINGSIZE 100
184:
185: struct pending
186: {
187: struct pending *next;
188: int nsyms;
189: struct symbol *symbol[PENDINGSIZE];
190: };
191:
192: /* List of free `struct pending' structures for reuse. */
193: struct pending *free_pendings;
194:
195: /* Here are the three lists that symbols are put on. */
196:
197: struct pending *file_symbols; /* static at top level, and types */
198:
199: struct pending *global_symbols; /* global functions and variables */
200:
201: struct pending *local_symbols; /* everything local to lexical context */
202:
203: /* Stack representing unclosed lexical contexts
204: (that will become blocks, eventually). */
205:
206: struct context_stack
207: {
208: struct pending *locals;
209: struct pending_block *old_blocks;
210: struct symbol *name;
211: CORE_ADDR start_addr;
212: int depth;
213: };
214:
215: struct context_stack *context_stack;
216:
217: /* Index of first unused entry in context stack. */
218: int context_stack_depth;
219:
220: /* Currently allocated size of context stack. */
221:
222: int context_stack_size;
223:
224: /* Nonzero if within a function (so symbols should be local,
225: if nothing says specifically). */
226:
227: int within_function;
228:
229: /* List of blocks already made (lexical contexts already closed).
230: This is used at the end to make the blockvector. */
231:
232: struct pending_block
233: {
234: struct pending_block *next;
235: struct block *block;
236: };
237:
238: struct pending_block *pending_blocks;
239:
240: extern CORE_ADDR first_object_file_end; /* From blockframe.c */
241:
242: /* File name symbols were loaded from. */
243:
244: static char *symfile;
245:
246: static int
247: xxmalloc (n)
248: {
249: int v = malloc (n);
250: if (v == 0)
251: abort ();
252: return v;
253: }
254:
255: /* Make a copy of the string at PTR with SIZE characters in the symbol obstack
256: (and add a null character at the end in the copy).
257: Returns the address of the copy. */
258:
259: static char *
260: obsavestring (ptr, size)
261: char *ptr;
262: int size;
263: {
264: register char *p = (char *) obstack_alloc (symbol_obstack, size + 1);
265: /* Open-coded bcopy--saves function call time.
266: These strings are usually short. */
267: {
268: register char *p1 = ptr;
269: register char *p2 = p;
270: char *end = ptr + size;
271: while (p1 != end)
272: *p2++ = *p1++;
273: }
274: p[size] = 0;
275: return p;
276: }
277:
278: /* Concatenate strings S1, S2 and S3; return the new string.
279: Space is found in the symbol_obstack. */
280:
281: static char *
282: obconcat (s1, s2, s3)
283: char *s1, *s2, *s3;
284: {
285: register int len = strlen (s1) + strlen (s2) + strlen (s3) + 1;
286: register char *val = (char *) obstack_alloc (symbol_obstack, len);
287: strcpy (val, s1);
288: strcat (val, s2);
289: strcat (val, s3);
290: return val;
291: }
292:
293: /* Support for Sun changes to dbx symbol format */
294:
295: /* For each identified header file, we have a table of types defined
296: in that header file.
297:
298: header_files maps header file names to their type tables.
299: It is a vector of n_header_files elements.
300: Each element describes one header file.
301: It contains a vector of types.
302:
303: Sometimes it can happen that the same header file produces
304: different results when included in different places.
305: This can result from conditionals or from different
306: things done before including the file.
307: When this happens, there are multiple entries for the file in this table,
308: one entry for each distinct set of results.
309: The entries are distinguished by the INSTANCE field.
310: The INSTANCE field appears in the N_BINCL and N_EXCL symbol table and is
311: used to match header-file references to their corresponding data. */
312:
313: struct header_file
314: {
315: char *name; /* Name of header file */
316: int instance; /* Numeric code distinguishing instances
317: of one header file that produced
318: different results when included.
319: It comes from the N_BINCL or N_EXCL. */
320: struct type **vector; /* Pointer to vector of types */
321: int length; /* Allocated length (# elts) of that vector */
322: };
323:
324: static struct header_file *header_files;
325:
326: static int n_header_files;
327:
328: static int n_allocated_header_files;
329:
330: /* Within each object file, various header files are assigned numbers.
331: A type is defined or referred to with a pair of numbers
332: (FILENUM,TYPENUM) where FILENUM is the number of the header file
333: and TYPENUM is the number within that header file.
334: TYPENUM is the index within the vector of types for that header file.
335:
336: FILENUM == 1 is special; it refers to the main source of the object file,
337: and not to any header file. FILENUM != 1 is interpreted by looking it up
338: in the following table, which contains indices in header_files. */
339:
340: static int *this_object_header_files;
341:
342: static int n_this_object_header_files;
343:
344: static int n_allocated_this_object_header_files;
345:
346: /* When a header file is getting special overriding definitions
347: for one source file, record here the header_files index
348: of its normal definition vector.
349: At other times, this is -1. */
350:
351: static int header_file_prev_index;
352:
353: /* At the start of reading dbx symbols, allocate our tables. */
354:
355: static void
356: init_header_files ()
357: {
358: n_allocated_header_files = 10;
359: header_files = (struct header_file *) xxmalloc (10 * sizeof (struct header_file));
360: n_header_files = 0;
361:
362: n_allocated_this_object_header_files = 10;
363: this_object_header_files = (int *) xxmalloc (10 * sizeof (int));
364: }
365:
366: /* At the end of reading dbx symbols, free our tables. */
367:
368: static void
369: free_header_files ()
370: {
371: register int i;
372: for (i = 0; i < n_header_files; i++)
373: free (header_files[i].name);
374: free (header_files);
375: free (this_object_header_files);
376: }
377:
378: /* Called at the start of each object file's symbols.
379: Clear out the mapping of header file numbers to header files. */
380:
381: static void
382: new_object_header_files ()
383: {
384: /* Leave FILENUM of 0 free for builtin types and this file's types. */
385: n_this_object_header_files = 1;
386: header_file_prev_index = -1;
387: }
388:
389: /* Add header file number I for this object file
390: at the next successive FILENUM. */
391:
392: static void
393: add_this_object_header_file (i)
394: int i;
395: {
396: if (n_this_object_header_files == n_allocated_this_object_header_files)
397: {
398: n_allocated_this_object_header_files *= 2;
399: this_object_header_files
400: = (int *) xrealloc (this_object_header_files,
401: n_allocated_this_object_header_files * sizeof (int));
402: }
403:
404: this_object_header_files[n_this_object_header_files++] = i;
405: }
406:
407: /* Add to this file an "old" header file, one already seen in
408: a previous object file. NAME is the header file's name.
409: INSTANCE is its instance code, to select among multiple
410: symbol tables for the same header file. */
411:
412: static void
413: add_old_header_file (name, instance)
414: char *name;
415: int instance;
416: {
417: register struct header_file *p = header_files;
418: register int i;
419:
420: for (i = 0; i < n_header_files; i++)
421: if (!strcmp (p[i].name, name) && instance == p[i].instance)
422: {
423: add_this_object_header_file (i);
424: return;
425: }
426: error ("Invalid symbol data: \"repeated\" header file that hasn't been seen before, at symtab pos %d.",
427: symnum);
428: }
429:
430: /* Add to this file a "new" header file: definitions for its types follow.
431: NAME is the header file's name.
432: Most often this happens only once for each distinct header file,
433: but not necessarily. If it happens more than once, INSTANCE has
434: a different value each time, and references to the header file
435: use INSTANCE values to select among them.
436:
437: dbx output contains "begin" and "end" markers for each new header file,
438: but at this level we just need to know which files there have been;
439: so we record the file when its "begin" is seen and ignore the "end". */
440:
441: static void
442: add_new_header_file (name, instance)
443: char *name;
444: int instance;
445: {
446: register int i;
447: register struct header_file *p = header_files;
448: header_file_prev_index = -1;
449:
450: #if 0
451: /* This code was used before I knew about the instance codes.
452: My first hypothesis is that it is not necessary now
453: that instance codes are handled. */
454:
455: /* Has this header file a previous definition?
456: If so, make a new entry anyway so that this use in this source file
457: gets a separate entry. Later source files get the old entry.
458: Record here the index of the old entry, so that any type indices
459: not previously defined can get defined in the old entry as
460: well as in the new one. */
461:
462: for (i = 0; i < n_header_files; i++)
463: if (!strcmp (p[i].name, name))
464: {
465: header_file_prev_index = i;
466: }
467:
468: #endif
469:
470: /* Make sure there is room for one more header file. */
471:
472: if (n_header_files == n_allocated_header_files)
473: {
474: n_allocated_header_files *= 2;
475: header_files
476: = (struct header_file *) xrealloc (header_files, n_allocated_header_files * sizeof (struct header_file));
477: }
478:
479: /* Create an entry for this header file. */
480:
481: i = n_header_files++;
482: header_files[i].name = name;
483: header_files[i].instance = instance;
484: header_files[i].length = 10;
485: header_files[i].vector
486: = (struct type **) xxmalloc (10 * sizeof (struct type *));
487: bzero (header_files[i].vector, 10 * sizeof (struct type *));
488:
489: add_this_object_header_file (i);
490: }
491:
492: /* Look up a dbx type-number pair. Return the address of the slot
493: where the type for that number-pair is stored.
494: The number-pair is in TYPENUMS.
495:
496: This can be used for finding the type associated with that pair
497: or for associating a new type with the pair. */
498:
499: static struct type **
500: dbx_lookup_type (typenums)
501: int typenums[2];
502: {
503: register int filenum = typenums[0], index = typenums[1];
504:
505: if (filenum < 0 || filenum >= n_this_object_header_files)
506: error ("Invalid symbol data: type number (%d,%d) out of range at symtab pos %d.",
507: filenum, index, symnum);
508:
509: if (filenum == 0)
510: {
511: /* Type is defined outside of header files.
512: Find it in this object file's type vector. */
513: if (index >= type_vector_length)
514: {
515: type_vector_length *= 2;
516: type_vector = (struct typevector *)
517: xrealloc (type_vector, sizeof (struct typevector) + type_vector_length * sizeof (struct type *));
518: bzero (&type_vector->type[type_vector_length / 2],
519: type_vector_length * sizeof (struct type *) / 2);
520: }
521: return &type_vector->type[index];
522: }
523: else
524: {
525: register int real_filenum = this_object_header_files[filenum];
526: register struct header_file *f;
527:
528: if (real_filenum >= n_header_files)
529: abort ();
530:
531: f = &header_files[real_filenum];
532:
533: if (index >= f->length)
534: {
535: f->length *= 2;
536: f->vector = (struct type **)
537: xrealloc (f->vector, f->length * sizeof (struct type *));
538: bzero (&f->vector[f->length / 2],
539: f->length * sizeof (struct type *) / 2);
540: }
541: return &f->vector[index];
542: }
543: }
544:
545: /* Make sure there is a type allocated for type numbers TYPENUMS
546: and return the type object.
547: This can create an empty (zeroed) type object. */
548:
549: static struct type *
550: dbx_alloc_type (typenums)
551: int typenums[2];
552: {
553: register struct type **type_addr = dbx_lookup_type (typenums);
554: register struct type *type = *type_addr;
555:
556: /* If we are referring to a type not known at all yet,
557: allocate an empty type for it.
558: We will fill it in later if we find out how. */
559: if (type == 0)
560: {
561: type = (struct type *) obstack_alloc (symbol_obstack,
562: sizeof (struct type));
563: bzero (type, sizeof (struct type));
564: *type_addr = type;
565: }
566: return type;
567: }
568:
569: #if 0
570: static struct type **
571: explicit_lookup_type (real_filenum, index)
572: int real_filenum, index;
573: {
574: register struct header_file *f = &header_files[real_filenum];
575:
576: if (index >= f->length)
577: {
578: f->length *= 2;
579: f->vector = (struct type **)
580: xrealloc (f->vector, f->length * sizeof (struct type *));
581: bzero (&f->vector[f->length / 2],
582: f->length * sizeof (struct type *) / 2);
583: }
584: return &f->vector[index];
585: }
586: #endif
587:
588: /* maintain the lists of symbols and blocks */
589:
590: /* Add a symbol to one of the lists of symbols. */
591: static void
592: add_symbol_to_list (symbol, listhead)
593: struct symbol *symbol;
594: struct pending **listhead;
595: {
596: /* We keep PENDINGSIZE symbols in each link of the list.
597: If we don't have a link with room in it, add a new link. */
598: if (*listhead == 0 || (*listhead)->nsyms == PENDINGSIZE)
599: {
600: register struct pending *link;
601: if (free_pendings)
602: {
603: link = free_pendings;
604: free_pendings = link->next;
605: }
606: else
607: link = (struct pending *) xxmalloc (sizeof (struct pending));
608:
609: link->next = *listhead;
610: *listhead = link;
611: link->nsyms = 0;
612: }
613:
614: (*listhead)->symbol[(*listhead)->nsyms++] = symbol;
615: }
616:
617: /* At end of reading syms, or in case of quit,
618: really free as many `struct pending's as we can easily find. */
619:
620: static void
621: really_free_pendings ()
622: {
623: struct pending *next, *next1;
624: struct pending_block *bnext, *bnext1;
625:
626: for (next = free_pendings; next; next = next1)
627: {
628: next1 = next->next;
629: free (next);
630: }
631: free_pendings = 0;
632:
633: for (bnext = pending_blocks; bnext; bnext = bnext1)
634: {
635: bnext1 = bnext->next;
636: free (bnext);
637: }
638: pending_blocks = 0;
639:
640: for (next = file_symbols; next; next = next1)
641: {
642: next1 = next->next;
643: free (next);
644: }
645: for (next = global_symbols; next; next = next1)
646: {
647: next1 = next->next;
648: free (next);
649: }
650: }
651:
652: /* Take one of the lists of symbols and make a block from it.
653: Keep the order the symbols have in the list (reversed from the input file).
654: Put the block on the list of pending blocks. */
655:
656: static void
657: finish_block (symbol, listhead, old_blocks, start, end)
658: struct symbol *symbol;
659: struct pending **listhead;
660: struct pending_block *old_blocks;
661: CORE_ADDR start, end;
662: {
663: register struct pending *next, *next1;
664: register struct block *block;
665: register struct pending_block *pblock;
666: struct pending_block *opblock;
667: register int i;
668:
669: /* Count the length of the list of symbols. */
670:
671: for (next = *listhead, i = 0; next; i += next->nsyms, next = next->next);
672:
673: block = (struct block *) obstack_alloc (symbol_obstack,
674: sizeof (struct block) + (i - 1) * sizeof (struct symbol *));
675:
676: /* Copy the symbols into the block. */
677:
678: BLOCK_NSYMS (block) = i;
679: for (next = *listhead; next; next = next->next)
680: {
681: register int j;
682: for (j = next->nsyms - 1; j >= 0; j--)
683: BLOCK_SYM (block, --i) = next->symbol[j];
684: }
685:
686: BLOCK_START (block) = start;
687: BLOCK_END (block) = end;
688: BLOCK_SUPERBLOCK (block) = 0; /* Filled in when containing block is made */
689:
690: /* Put the block in as the value of the symbol that names it. */
691:
692: if (symbol)
693: {
694: SYMBOL_BLOCK_VALUE (symbol) = block;
695: BLOCK_FUNCTION (block) = symbol;
696: }
697: else
698: BLOCK_FUNCTION (block) = 0;
699:
700: /* Now "free" the links of the list, and empty the list. */
701:
702: for (next = *listhead; next; next = next1)
703: {
704: next1 = next->next;
705: next->next = free_pendings;
706: free_pendings = next;
707: }
708: *listhead = 0;
709:
710: /* Install this block as the superblock
711: of all blocks made since the start of this scope
712: that don't have superblocks yet. */
713:
714: opblock = 0;
715: for (pblock = pending_blocks; pblock != old_blocks; pblock = pblock->next)
716: {
717: if (BLOCK_SUPERBLOCK (pblock->block) == 0)
718: BLOCK_SUPERBLOCK (pblock->block) = block;
719: opblock = pblock;
720: }
721:
722: /* Record this block on the list of all blocks in the file.
723: Put it after opblock, or at the beginning if opblock is 0.
724: This puts the block in the list after all its subblocks. */
725:
726: /* Allocate in the symbol_obstack to save time.
727: It wastes a little space. */
728: pblock = (struct pending_block *) obstack_alloc (symbol_obstack,
729: sizeof (struct pending_block));
730: pblock->block = block;
731: if (opblock)
732: {
733: pblock->next = opblock->next;
734: opblock->next = pblock;
735: }
736: else
737: {
738: pblock->next = pending_blocks;
739: pending_blocks = pblock;
740: }
741: }
742:
743: static struct blockvector *
744: make_blockvector ()
745: {
746: register struct pending_block *next, *next1;
747: register struct blockvector *blockvector;
748: register int i;
749:
750: /* Count the length of the list of blocks. */
751:
752: for (next = pending_blocks, i = 0; next; next = next->next, i++);
753:
754: blockvector = (struct blockvector *) obstack_alloc (symbol_obstack, sizeof (struct blockvector) + (i - 1) * sizeof (struct block *));
755:
756: /* Copy the blocks into the blockvector.
757: This is done in reverse order, which happens to put
758: the blocks into the proper order (ascending starting address).
759: finish_block has hair to insert each block into the list
760: after its subblocks in order to make sure this is true. */
761:
762: BLOCKVECTOR_NBLOCKS (blockvector) = i;
763: for (next = pending_blocks; next; next = next->next)
764: BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
765:
766: #if 0 /* Now we make the links in the obstack, so don't free them. */
767: /* Now free the links of the list, and empty the list. */
768:
769: for (next = pending_blocks; next; next = next1)
770: {
771: next1 = next->next;
772: free (next);
773: }
774: #endif
775: pending_blocks = 0;
776:
777: return blockvector;
778: }
779:
780: /* Manage the vector of line numbers. */
781:
782: static
783: record_line (line, pc)
784: int line;
785: CORE_ADDR pc;
786: {
787: /* Ignore the dummy line number in libg.o */
788:
789: if (line == 0xffff)
790: return;
791:
792: /* Make sure line vector is big enough. */
793:
794: if (line_vector_index + 1 >= line_vector_length)
795: {
796: line_vector_length *= 2;
797: line_vector = (struct linetable *)
798: xrealloc (line_vector,
799: sizeof (struct linetable) + line_vector_length * sizeof (int));
800: current_subfile->line_vector = line_vector;
801: }
802:
803: /* If this line is not continguous with previous one recorded,
804: record a line-number entry for it. */
805: if (line != prev_line_number + 1)
806: line_vector->item[line_vector_index++] = - line;
807: prev_line_number = line;
808:
809: /* Record the core address of the line. */
810: line_vector->item[line_vector_index++] = pc;
811: }
812:
813: /* Start a new symtab for a new source file.
814: This is called when a dbx symbol of type N_SO is seen;
815: it indicates the start of data for one original source file. */
816:
817: static void
818: start_symtab (name, start_addr)
819: char *name;
820: CORE_ADDR start_addr;
821: {
822: register struct symtab *s;
823:
824: last_source_file = name;
825: last_source_start_addr = start_addr;
826: file_symbols = 0;
827: global_symbols = 0;
828: within_function = 0;
829:
830: /* Context stack is initially empty, with room for 10 levels. */
831: context_stack
832: = (struct context_stack *) xxmalloc (10 * sizeof (struct context_stack));
833: context_stack_size = 10;
834: context_stack_depth = 0;
835:
836: new_object_header_files ();
837:
838: for (s = symseg_chain; s; s = s->next)
839: if (s->ldsymoff == symnum * sizeof (struct nlist))
840: break;
841: current_symseg = s;
842: if (s != 0)
843: return;
844:
845: type_vector_length = 160;
846: type_vector = (struct typevector *) xxmalloc (sizeof (struct typevector) + type_vector_length * sizeof (struct type *));
847: bzero (type_vector->type, type_vector_length * sizeof (struct type *));
848:
849: /* Initialize the list of sub source files with one entry
850: for this file (the top-level source file). */
851:
852: subfiles = 0;
853: current_subfile = 0;
854: start_subfile (name);
855: }
856:
857: /* Handle an N_SOL symbol, which indicates the start of
858: code that came from an included (or otherwise merged-in)
859: source file with a different name. */
860:
861: static void
862: start_subfile (name)
863: char *name;
864: {
865: register struct subfile *subfile;
866:
867: /* Save the current subfile's line vector data. */
868:
869: if (current_subfile)
870: {
871: current_subfile->line_vector_index = line_vector_index;
872: current_subfile->line_vector_length = line_vector_length;
873: current_subfile->prev_line_number = prev_line_number;
874: }
875:
876: /* See if this subfile is already known as a subfile of the
877: current main source file. */
878:
879: for (subfile = subfiles; subfile; subfile = subfile->next)
880: {
881: if (!strcmp (subfile->name, name))
882: {
883: line_vector = subfile->line_vector;
884: line_vector_index = subfile->line_vector_index;
885: line_vector_length = subfile->line_vector_length;
886: prev_line_number = subfile->prev_line_number;
887: current_subfile = subfile;
888: return;
889: }
890: }
891:
892: /* This subfile is not known. Add an entry for it. */
893:
894: line_vector_index = 0;
895: line_vector_length = 1000;
896: prev_line_number = -2; /* Force first line number to be explicit */
897: line_vector = (struct linetable *)
898: xxmalloc (sizeof (struct linetable) + line_vector_length * sizeof (int));
899:
900: /* Make an entry for this subfile in the list of all subfiles
901: of the current main source file. */
902:
903: subfile = (struct subfile *) xxmalloc (sizeof (struct subfile));
904: subfile->next = subfiles;
905: subfile->name = savestring (name, strlen (name));
906: subfile->line_vector = line_vector;
907: subfiles = subfile;
908: current_subfile = subfile;
909: }
910:
911: /* Finish the symbol definitions for one main source file,
912: close off all the lexical contexts for that file
913: (creating struct block's for them), then make the struct symtab
914: for that file and put it in the list of all such.
915:
916: END_ADDR is the address of the end of the file's text. */
917:
918: static void
919: end_symtab (end_addr)
920: CORE_ADDR end_addr;
921: {
922: register struct symtab *symtab;
923: register struct blockvector *blockvector;
924: register struct subfile *subfile;
925: register struct linetable *lv;
926: struct subfile *nextsub;
927:
928: if (current_symseg != 0)
929: {
930: last_source_file = 0;
931: current_symseg = 0;
932: return;
933: }
934:
935: /* Finish the lexical context of the last function in the file;
936: pop the context stack. */
937:
938: if (context_stack_depth > 0)
939: {
940: register struct context_stack *cstk;
941: context_stack_depth--;
942: cstk = &context_stack[context_stack_depth];
943: /* Make a block for the local symbols within. */
944: finish_block (cstk->name, &local_symbols, cstk->old_blocks,
945: cstk->start_addr, end_addr);
946: }
947:
948: /* Finish defining all the blocks of this symtab. */
949: finish_block (0, &file_symbols, 0, last_source_start_addr, end_addr);
950: finish_block (0, &global_symbols, 0, last_source_start_addr, end_addr);
951: blockvector = make_blockvector ();
952:
953: current_subfile->line_vector_index = line_vector_index;
954:
955: /* Now create the symtab objects proper, one for each subfile. */
956: /* (The main file is one of them.) */
957:
958: for (subfile = subfiles; subfile; subfile = nextsub)
959: {
960: symtab = (struct symtab *) xxmalloc (sizeof (struct symtab));
961: symtab->free_ptr = 0;
962:
963: /* Fill in its components. */
964: symtab->blockvector = blockvector;
965: type_vector->length = type_vector_length;
966: symtab->typevector = type_vector;
967: symtab->free_code = free_linetable;
968: if (subfile->next == 0)
969: symtab->free_ptr = (char *) type_vector;
970:
971: symtab->filename = subfile->name;
972: lv = subfile->line_vector;
973: lv->nitems = subfile->line_vector_index;
974: symtab->linetable = (struct linetable *)
975: xrealloc (lv, sizeof (struct linetable) + lv->nitems * sizeof (int));
976: symtab->nlines = 0;
977: symtab->line_charpos = 0;
978:
979: /* Link the new symtab into the list of such. */
980: symtab->next = symtab_list;
981: symtab_list = symtab;
982:
983: nextsub = subfile->next;
984: free (subfile);
985: }
986:
987: type_vector = 0;
988: type_vector_length = -1;
989: line_vector = 0;
990: line_vector_length = -1;
991: last_source_file = 0;
992: }
993:
994: #ifdef N_BINCL
995:
996: /* Handle the N_BINCL and N_EINCL symbol types
997: that act like N_SOL for switching source files
998: (different subfiles, as we call them) within one object file,
999: but using a stack rather than in an arbitrary order. */
1000:
1001: struct subfile_stack
1002: {
1003: struct subfile_stack *next;
1004: char *name;
1005: int prev_index;
1006: };
1007:
1008: struct subfile_stack *subfile_stack;
1009:
1010: static void
1011: push_subfile ()
1012: {
1013: register struct subfile_stack *tem
1014: = (struct subfile_stack *) xxmalloc (sizeof (struct subfile_stack));
1015:
1016: tem->next = subfile_stack;
1017: subfile_stack = tem;
1018: if (current_subfile == 0 || current_subfile->name == 0)
1019: abort ();
1020: tem->name = current_subfile->name;
1021: tem->prev_index = header_file_prev_index;
1022: }
1023:
1024: static char *
1025: pop_subfile ()
1026: {
1027: register char *name;
1028: register struct subfile_stack *link = subfile_stack;
1029:
1030: if (link == 0)
1031: abort ();
1032:
1033: name = link->name;
1034: subfile_stack = link->next;
1035: header_file_prev_index = link->prev_index;
1036: free (link);
1037:
1038: return name;
1039: }
1040: #endif /* Have N_BINCL */
1041:
1042: /* Accumulate the misc functions in bunches of 127.
1043: At the end, copy them all into one newly allocated structure. */
1044:
1045: #define MISC_BUNCH_SIZE 127
1046:
1047: struct misc_bunch
1048: {
1049: struct misc_bunch *next;
1050: struct misc_function contents[MISC_BUNCH_SIZE];
1051: };
1052:
1053: /* Bunch currently being filled up.
1054: The next field points to chain of filled bunches. */
1055:
1056: static struct misc_bunch *misc_bunch;
1057:
1058: /* Number of slots filled in current bunch. */
1059:
1060: static int misc_bunch_index;
1061:
1062: /* Total number of misc functions recorded so far. */
1063:
1064: static int misc_count;
1065:
1066: static void
1067: init_misc_functions ()
1068: {
1069: misc_count = 0;
1070: misc_bunch = 0;
1071: misc_bunch_index = MISC_BUNCH_SIZE;
1072: }
1073:
1074: static void
1075: record_misc_function (name, address)
1076: char *name;
1077: CORE_ADDR address;
1078: {
1079: register struct misc_bunch *new;
1080:
1081: if (misc_bunch_index == MISC_BUNCH_SIZE)
1082: {
1083: new = (struct misc_bunch *) xxmalloc (sizeof (struct misc_bunch));
1084: misc_bunch_index = 0;
1085: new->next = misc_bunch;
1086: misc_bunch = new;
1087: }
1088: misc_bunch->contents[misc_bunch_index].name = name;
1089: misc_bunch->contents[misc_bunch_index].address = address;
1090: misc_bunch_index++;
1091: misc_count++;
1092: }
1093:
1094: static int
1095: compare_misc_functions (fn1, fn2)
1096: struct misc_function *fn1, *fn2;
1097: {
1098: /* Return a signed result based on unsigned comparisons
1099: so that we sort into unsigned numeric order. */
1100: if (fn1->address < fn2->address)
1101: return -1;
1102: if (fn1->address > fn2->address)
1103: return 1;
1104: return 0;
1105: }
1106:
1107: static void
1108: discard_misc_bunches ()
1109: {
1110: register struct misc_bunch *next;
1111:
1112: while (misc_bunch)
1113: {
1114: next = misc_bunch->next;
1115: free (misc_bunch);
1116: misc_bunch = next;
1117: }
1118: }
1119:
1120: static void
1121: condense_misc_bunches ()
1122: {
1123: register int i, j;
1124: register struct misc_bunch *bunch;
1125: #ifdef NAMES_HAVE_UNDERSCORE
1126: int offset = 1;
1127: #else
1128: int offset = 0;
1129: #endif
1130:
1131: misc_function_vector
1132: = (struct misc_function *)
1133: xxmalloc (misc_count * sizeof (struct misc_function));
1134:
1135: j = 0;
1136: bunch = misc_bunch;
1137: while (bunch)
1138: {
1139: for (i = 0; i < misc_bunch_index; i++)
1140: {
1141: misc_function_vector[j] = bunch->contents[i];
1142: misc_function_vector[j].name
1143: = obconcat (misc_function_vector[j].name
1144: + (misc_function_vector[j].name[0] == '_' ? offset : 0),
1145: "", "");
1146: j++;
1147: }
1148: bunch = bunch->next;
1149: misc_bunch_index = MISC_BUNCH_SIZE;
1150: }
1151:
1152: misc_function_count = j;
1153:
1154: /* Sort the misc functions by address. */
1155:
1156: qsort (misc_function_vector, j, sizeof (struct misc_function),
1157: compare_misc_functions);
1158: }
1159:
1160: /* Call sort_syms to sort alphabetically
1161: the symbols of each block of each symtab. */
1162:
1163: static int
1164: compare_symbols (s1, s2)
1165: struct symbol **s1, **s2;
1166: {
1167: register int namediff;
1168:
1169: /* Compare the initial characters. */
1170: namediff = SYMBOL_NAME (*s1)[0] - SYMBOL_NAME (*s2)[0];
1171: if (namediff != 0) return namediff;
1172:
1173: /* If they match, compare the rest of the names. */
1174: namediff = strcmp (SYMBOL_NAME (*s1), SYMBOL_NAME (*s2));
1175: if (namediff != 0) return namediff;
1176:
1177: /* For symbols of the same name, registers should come first. */
1178: return ((SYMBOL_CLASS (*s2) == LOC_REGISTER)
1179: - (SYMBOL_CLASS (*s1) == LOC_REGISTER));
1180: }
1181:
1182: static void
1183: sort_syms ()
1184: {
1185: register struct symtab *s;
1186: int i, nbl;
1187: register struct blockvector *bv;
1188: register struct block *b;
1189:
1190: for (s = symtab_list; s; s = s->next)
1191: {
1192: bv = BLOCKVECTOR (s);
1193: nbl = BLOCKVECTOR_NBLOCKS (bv);
1194: for (i = 0; i < nbl; i++)
1195: {
1196: b = BLOCKVECTOR_BLOCK (bv, i);
1197: if (BLOCK_SHOULD_SORT (b))
1198: qsort (&BLOCK_SYM (b, 0), BLOCK_NSYMS (b),
1199: sizeof (struct symbol *), compare_symbols);
1200: else
1201: {
1202: int lastindex = BLOCK_NSYMS (b) - 1;
1203: register int j;
1204: for (j = (lastindex - 1) / 2; j >= 0; j--)
1205: {
1206: register struct symbol *sym;
1207: sym = BLOCK_SYM (b, j);
1208: BLOCK_SYM (b, j) = BLOCK_SYM (b, lastindex - j);
1209: BLOCK_SYM (b, lastindex - j) = sym;
1210: }
1211: }
1212: }
1213: }
1214: }
1215:
1216: /* This is the symbol-file command. Read the file, analyze its symbols,
1217: and add a struct symtab to symtab_list. */
1218:
1219: void
1220: symbol_file_command (name)
1221: char *name;
1222: {
1223: register int desc;
1224: DECLARE_FILE_HEADERS;
1225: struct nlist *nlist;
1226: char *stringtab;
1227: long buffer;
1228: register int val;
1229: extern void close ();
1230: struct cleanup *old_chain;
1231: struct symtab *symseg;
1232: struct stat statbuf;
1233:
1234: dont_repeat ();
1235:
1236: if (name == 0)
1237: {
1238: if (symtab_list && !query ("Discard symbol table? ", 0))
1239: error ("Not confirmed.");
1240: free_all_symtabs ();
1241: return;
1242: }
1243:
1244: if (symtab_list && !query ("Load new symbol table from \"%s\"? ", name))
1245: error ("Not confirmed.");
1246:
1247: {
1248: char *absolute_name;
1249: desc = openp (getenv ("PATH"), 1, name, O_RDONLY, 0, &absolute_name);
1250: if (desc < 0)
1251: perror_with_name (name);
1252: else
1253: name = absolute_name;
1254: }
1255:
1256: old_chain = make_cleanup (close, desc);
1257: make_cleanup (free_current_contents, &name);
1258:
1259: READ_FILE_HEADERS (desc, name);
1260:
1261: if (NUMBER_OF_SYMBOLS == 0)
1262: {
1263: if (symfile)
1264: free (symfile);
1265: symfile = 0;
1266: free_all_symtabs ();
1267: printf ("%s has no symbol-table; symbols discarded.\n", name);
1268: fflush (stdout);
1269: return;
1270: }
1271:
1272: printf ("Reading symbol data from %s...", name);
1273: fflush (stdout);
1274:
1275: /* Now read the string table, all at once. */
1276: val = lseek (desc, STRING_TABLE_OFFSET, 0);
1277: if (val < 0)
1278: perror_with_name (name);
1279: stat (name, &statbuf);
1280: READ_STRING_TABLE_SIZE (buffer);
1281: if (buffer >= 0 && buffer < statbuf.st_size)
1282: stringtab = (char *) alloca (buffer);
1283: else
1284: stringtab = NULL;
1285: if (stringtab == NULL)
1286: error ("ridiculous string table size: %d bytes", name, buffer);
1287:
1288: bcopy (&buffer, stringtab, sizeof buffer);
1289: val = myread (desc, stringtab + sizeof buffer, buffer - sizeof buffer);
1290: if (val < 0)
1291: perror_with_name (name);
1292:
1293: /* Throw away the old symbol table. */
1294:
1295: if (symfile)
1296: free (symfile);
1297: symfile = 0;
1298: free_all_symtabs ();
1299:
1300: /* Empty the hash table of global syms looking for values. */
1301: bzero (global_sym_chain, sizeof global_sym_chain);
1302:
1303: #ifdef READ_GDB_SYMSEGS
1304: /* That puts us at the symsegs. Read them. */
1305: symseg_chain = read_symsegs (desc, name);
1306: hash_symsegs ();
1307:
1308: /* Free the symtabs made by read_symsegs, but not their contents,
1309: which have been copied into symtabs on symtab_list. */
1310: for (symseg = symseg_chain; symseg; symseg = symseg->next)
1311: {
1312: int i;
1313: struct sourcevector *sv = (struct sourcevector *) symseg->linetable;
1314:
1315: for (i = 0; i < sv->length; i++)
1316: {
1317: int j;
1318: struct source *source = sv->source[i];
1319: struct symtab *sp1
1320: = (struct symtab *) xxmalloc (sizeof (struct symtab));
1321:
1322: bcopy (symseg, sp1, sizeof (struct symtab));
1323: sp1->filename = savestring (source->name, strlen (source->name));
1324: sp1->linetable = &source->contents;
1325: sp1->free_code = free_nothing;
1326: sp1->free_ptr = (i == 0) ? (char *) symseg : 0;
1327:
1328: sp1->next = symtab_list;
1329: symtab_list = sp1;
1330: }
1331: }
1332: #else
1333: /* Where people are using the 4.2 ld program, must not check for
1334: symsegs, because that ld puts randonm garbage at the end of
1335: the output file and that would trigger an error message. */
1336: symseg_chain = 0;
1337: #endif
1338:
1339: /* Position to read the symbol table. Do not read it all at once. */
1340: val = lseek (desc, SYMBOL_TABLE_OFFSET, 0);
1341: if (val < 0)
1342: perror_with_name (name);
1343:
1344: init_misc_functions ();
1345: make_cleanup (discard_misc_bunches, 0);
1346: init_header_files ();
1347: make_cleanup (free_header_files, 0);
1348: free_pendings = 0;
1349: pending_blocks = 0;
1350: file_symbols = 0;
1351: global_symbols = 0;
1352: make_cleanup (really_free_pendings, 0);
1353:
1354: /* Now that the symbol table data of the executable file are all in core,
1355: process them and define symbols accordingly. Closes desc. */
1356:
1357: read_dbx_symtab (desc, stringtab, NUMBER_OF_SYMBOLS);
1358: close (desc);
1359:
1360: /* Sort symbols alphabetically within each block. */
1361:
1362: sort_syms ();
1363:
1364: /* Go over the misc functions and install them in vector. */
1365:
1366: condense_misc_bunches ();
1367:
1368: /* Don't allow char * to have a typename (else would get caddr_t.) */
1369:
1370: TYPE_NAME (lookup_pointer_type (builtin_type_char)) = 0;
1371:
1372: /* Make a default for file to list. */
1373:
1374: select_source_symtab (symtab_list);
1375:
1376: symfile = savestring (name, strlen (name));
1377:
1378: do_cleanups (old_chain);
1379:
1380: /* Free the symtabs made by read_symsegs, but not their contents,
1381: which have been copied into symtabs on symtab_list. */
1382: while (symseg_chain)
1383: {
1384: register struct symtab *s = symseg_chain->next;
1385: free (symseg_chain);
1386: symseg_chain = s;
1387: }
1388:
1389: printf ("done.\n");
1390: fflush (stdout);
1391: }
1392:
1393: /* Return name of file symbols were loaded from, or 0 if none.. */
1394:
1395: char *
1396: get_sym_file ()
1397: {
1398: return symfile;
1399: }
1400:
1401: /* Buffer for reading the symbol table entries. */
1402: static struct nlist symbuf[2048];
1403: static int symbuf_idx;
1404: static int symbuf_end;
1405:
1406: /* I/O descriptor for reading the symbol table. */
1407: static int symtab_input_desc;
1408:
1409: /* The address of the string table
1410: of the object file we are reading (as copied into core). */
1411: static char *stringtab_global;
1412:
1413: /* Refill the symbol table input buffer
1414: and set the variables that control fetching entries from it.
1415: Reports an error if no data available.
1416: This function can read past the end of the symbol table
1417: (into the string table) but this does no harm. */
1418:
1419: static int
1420: fill_symbuf ()
1421: {
1422: int nbytes = myread (symtab_input_desc, symbuf, sizeof (symbuf));
1423: if (nbytes <= 0)
1424: error ("error or end of file reading symbol table");
1425: symbuf_end = nbytes / sizeof (struct nlist);
1426: symbuf_idx = 0;
1427: return 1;
1428: }
1429:
1430: /* dbx allows the text of a symbol name to be continued into the
1431: next symbol name! When such a continuation is encountered
1432: (a \ at the end of the text of a name)
1433: call this function to get the continuation. */
1434:
1435: static char *
1436: next_symbol_text ()
1437: {
1438: if (symbuf_idx == symbuf_end)
1439: fill_symbuf ();
1440: symnum++;
1441: return symbuf[symbuf_idx++].n_un.n_strx + stringtab_global;
1442: }
1443:
1444: /* Given pointers to a a.out symbol table in core containing dbx style data,
1445: analyze them and create struct symtab's describing the symbols.
1446: NLISTLEN is the number of symbols in the symbol table.
1447: We read them one at a time using stdio.
1448: All symbol names are given as offsets relative to STRINGTAB. */
1449:
1450: static void
1451: read_dbx_symtab (desc, stringtab, nlistlen)
1452: int desc;
1453: register char *stringtab;
1454: register int nlistlen;
1455: {
1456: register char *namestring;
1457: register struct symbol *sym, *prev;
1458: int hash;
1459: int num_object_files = 0;
1460: struct cleanup *old_chain;
1461:
1462: #ifdef N_BINCL
1463: subfile_stack = 0;
1464: #endif
1465:
1466: old_chain = make_cleanup (free_all_symtabs, 0);
1467: stringtab_global = stringtab;
1468: last_source_file = 0;
1469:
1470: #ifdef END_OF_TEXT_DEFAULT
1471: end_of_text_addr = END_OF_TEXT_DEFAULT;
1472: #endif
1473:
1474: symtab_input_desc = desc;
1475: symbuf_end = symbuf_idx = 0;
1476:
1477: for (symnum = 0; symnum < nlistlen; symnum++)
1478: {
1479: struct nlist *bufp;
1480: int type;
1481:
1482: QUIT; /* allow this to be interruptable */
1483: if (symbuf_idx == symbuf_end)
1484: fill_symbuf ();
1485: bufp = &symbuf[symbuf_idx++];
1486: type = bufp->n_type;
1487: namestring = bufp->n_un.n_strx ? bufp->n_un.n_strx + stringtab : "";
1488:
1489: if (type & N_STAB)
1490: process_one_symbol (type, bufp->n_desc,
1491: bufp->n_value, namestring);
1492: /* A static text symbol whose name ends in ".o"
1493: or begins with "-l" means the start of another object file.
1494: So end the symtab of the source file we have been processing.
1495: This is how we avoid counting the libraries as part
1496: or the last source file.
1497: Also this way we find end of first object file (crt0). */
1498: else if (
1499: #ifdef N_NBTEXT
1500: (type == N_NBTEXT)
1501: #else
1502: (type == N_TEXT)
1503: #endif
1504: && (!strcmp (namestring + strlen (namestring) - 2, ".o")
1505: || !strncmp (namestring, "-l", 2)))
1506: {
1507: if (num_object_files++ == 1)
1508: first_object_file_end = bufp->n_value;
1509: if (last_source_file)
1510: end_symtab (bufp->n_value);
1511: }
1512: else if (type & N_EXT || type == N_TEXT
1513: #ifdef N_NBTEXT
1514: || type == N_NBTEXT
1515: #endif
1516: )
1517: {
1518: int used_up = 0;
1519:
1520: /* Record the location of _etext. */
1521: if (type == (N_TEXT | N_EXT)
1522: && !strcmp (namestring, "_etext"))
1523: end_of_text_addr = bufp->n_value;
1524:
1525: /* Global symbol: see if we came across a dbx definition
1526: for a corresponding symbol. If so, store the value.
1527: Remove syms from the chain when their values are stored,
1528: but search the whole chain, as there may be several syms
1529: from different files with the same name. */
1530: if (type & N_EXT)
1531: {
1532: prev = 0;
1533: #ifdef NAMES_HAVE_UNDERSCORE
1534: hash = hashname (namestring + 1);
1535: #else /* not NAMES_HAVE_UNDERSCORE */
1536: hash = hashname (namestring);
1537: #endif /* not NAMES_HAVE_UNDERSCORE */
1538: for (sym = global_sym_chain[hash];
1539: sym;)
1540: {
1541: if (
1542: #ifdef NAMES_HAVE_UNDERSCORE
1543: *namestring == '_'
1544: && namestring[1] == SYMBOL_NAME (sym)[0]
1545: &&
1546: !strcmp (namestring + 2, SYMBOL_NAME (sym) + 1)
1547: #else /* NAMES_HAVE_UNDERSCORE */
1548: namestring[0] == SYMBOL_NAME (sym)[0]
1549: &&
1550: !strcmp (namestring + 1, SYMBOL_NAME (sym) + 1)
1551: #endif /* NAMES_HAVE_UNDERSCORE */
1552: )
1553: {
1554: if (prev)
1555: SYMBOL_VALUE (prev) = SYMBOL_VALUE (sym);
1556: else
1557: global_sym_chain[hash]
1558: = (struct symbol *) SYMBOL_VALUE (sym);
1559: SYMBOL_VALUE (sym) = bufp->n_value;
1560: if (prev)
1561: sym = (struct symbol *) SYMBOL_VALUE (prev);
1562: else
1563: sym = global_sym_chain[hash];
1564:
1565: used_up = 1;
1566: }
1567: else
1568: {
1569: prev = sym;
1570: sym = (struct symbol *) SYMBOL_VALUE (sym);
1571: }
1572: }
1573: }
1574:
1575: /* Defined global or text symbol: record as a misc function
1576: if it didn't give its address to a debugger symbol above. */
1577: if (type <= (N_TYPE | N_EXT)
1578: && type != N_EXT
1579: && ! used_up)
1580: record_misc_function (namestring, bufp->n_value);
1581: }
1582: }
1583:
1584: if (last_source_file)
1585: end_symtab (end_of_text_addr);
1586:
1587: discard_cleanups (old_chain);
1588: }
1589:
1590: static int
1591: hashname (name)
1592: char *name;
1593: {
1594: register char *p = name;
1595: register int total = p[0];
1596: register int c;
1597:
1598: c = p[1];
1599: total += c << 2;
1600: if (c)
1601: {
1602: c = p[2];
1603: total += c << 4;
1604: if (c)
1605: total += p[3] << 6;
1606: }
1607:
1608: /* Ensure result is positive. */
1609: if (total < 0) total += (1000 << 6);
1610: return total % HASHSIZE;
1611: }
1612:
1613: /* Put all appropriate global symbols in the symseg data
1614: onto the hash chains so that their addresses will be stored
1615: when seen later in loader global symbols. */
1616:
1617: static void
1618: hash_symsegs ()
1619: {
1620: /* Look at each symbol in each block in each symseg symtab. */
1621: struct symtab *s;
1622: for (s = symseg_chain; s; s = s->next)
1623: {
1624: register int n;
1625: for (n = BLOCKVECTOR_NBLOCKS (BLOCKVECTOR (s)) - 1; n >= 0; n--)
1626: {
1627: register struct block *b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), n);
1628: register int i;
1629: for (i = BLOCK_NSYMS (b) - 1; i >= 0; i--)
1630: {
1631: register struct symbol *sym = BLOCK_SYM (b, i);
1632:
1633: /* Put the symbol on a chain if its value is an address
1634: that is figured out by the loader. */
1635:
1636: if (SYMBOL_CLASS (sym) == LOC_EXTERNAL)
1637: {
1638: register int hash = hashname (SYMBOL_NAME (sym));
1639: SYMBOL_VALUE (sym) = (int) global_sym_chain[hash];
1640: global_sym_chain[hash] = sym;
1641: SYMBOL_CLASS (sym) = LOC_STATIC;
1642: }
1643: }
1644: }
1645: }
1646: }
1647:
1648: static void
1649: process_one_symbol (type, desc, value, name)
1650: int type, desc;
1651: CORE_ADDR value;
1652: char *name;
1653: {
1654: register struct context_stack *new;
1655:
1656: /* Something is wrong if we see real data before
1657: seeing a source file name. */
1658:
1659: if (last_source_file == 0 && type != N_SO)
1660: {
1661: #ifdef N_NSYMS
1662: /* This code is used on Ultrix; ignore this sym. */
1663: if (type == N_NSYMS)
1664: return;
1665: #endif
1666:
1667: if (type == N_ENTRY)
1668: /* This code appears in libraries on Gould machines. */
1669: return;
1670: error ("Invalid symbol data: does not start by identifying a source file.");
1671: }
1672:
1673: switch (type)
1674: {
1675: case N_FUN:
1676: case N_FNAME:
1677: /* Either of these types of symbols indicates the start of
1678: a new function. We must process its "name" normally for dbx,
1679: but also record the start of a new lexical context, and possibly
1680: also the end of the lexical context for the previous function. */
1681:
1682: within_function = 1;
1683: if (context_stack_depth > 0)
1684: {
1685: new = &context_stack[--context_stack_depth];
1686: /* Make a block for the local symbols within. */
1687: finish_block (new->name, &local_symbols, new->old_blocks,
1688: new->start_addr, value);
1689: }
1690: /* Stack must be empty now. */
1691: if (context_stack_depth != 0)
1692: error ("Invalid symbol data: unmatched N_LBRAC before symtab pos %d.",
1693: symnum);
1694:
1695: new = &context_stack[context_stack_depth++];
1696: new->old_blocks = pending_blocks;
1697: new->start_addr = value;
1698: new->name = define_symbol (value, name, desc);
1699: local_symbols = 0;
1700: break;
1701:
1702: case N_LBRAC:
1703: /* This "symbol" just indicates the start of an inner lexical
1704: context within a function. */
1705:
1706: if (context_stack_depth == context_stack_size)
1707: {
1708: context_stack_size *= 2;
1709: context_stack
1710: = (struct context_stack *) xrealloc (context_stack,
1711: context_stack_size
1712: * sizeof (struct context_stack));
1713: }
1714:
1715: new = &context_stack[context_stack_depth++];
1716: new->depth = desc;
1717: new->locals = local_symbols;
1718: new->old_blocks = pending_blocks;
1719: new->start_addr = value;
1720: new->name = 0;
1721: local_symbols = 0;
1722: break;
1723:
1724: case N_RBRAC:
1725: /* This "symbol" just indicates the end of an inner lexical
1726: context that was started with N_RBRAC. */
1727: new = &context_stack[--context_stack_depth];
1728: if (desc != new->depth)
1729: error ("Invalid symbol data: N_LBRAC/N_RBRAC symbol mismatch, symtab pos %d.", symnum);
1730: local_symbols = new->locals;
1731:
1732: /* If this is not the outermost LBRAC...RBRAC pair in the
1733: function, its local symbols preceded it, and are the ones
1734: just recovered from the context stack. Defined the block for them.
1735:
1736: If this is the outermost LBRAC...RBRAC pair, there is no
1737: need to do anything; leave the symbols that preceded it
1738: to be attached to the function's own block. */
1739: if (local_symbols && context_stack_depth > 1)
1740: {
1741: /* Muzzle a compiler bug that makes end > start. */
1742: if (new->start_addr > value)
1743: new->start_addr = value;
1744: /* Make a block for the local symbols within. */
1745: finish_block (0, &local_symbols, new->old_blocks,
1746: new->start_addr + last_source_start_addr,
1747: value + last_source_start_addr);
1748: }
1749: break;
1750:
1751: case N_FN:
1752: /* This kind of symbol supposedly indicates the start
1753: of an object file. In fact this type does not appear. */
1754: break;
1755:
1756: case N_SO:
1757: /* This type of symbol indicates the start of data
1758: for one source file.
1759: Finish the symbol table of the previous source file
1760: (if any) and start accumulating a new symbol table. */
1761: if (last_source_file)
1762: end_symtab (value);
1763: start_symtab (name, value);
1764: break;
1765:
1766: case N_SOL:
1767: /* This type of symbol indicates the start of data for
1768: a sub-source-file, one whose contents were copied or
1769: included in the compilation of the main source file
1770: (whose name was given in the N_SO symbol.) */
1771: start_subfile (name);
1772: break;
1773:
1774: #ifdef N_BINCL
1775: case N_BINCL:
1776: push_subfile ();
1777: add_new_header_file (name, value);
1778: start_subfile (name);
1779: break;
1780:
1781: case N_EINCL:
1782: start_subfile (pop_subfile ());
1783: break;
1784:
1785: case N_EXCL:
1786: add_old_header_file (name, value);
1787: break;
1788: #endif /* have N_BINCL */
1789:
1790: case N_SLINE:
1791: /* This type of "symbol" really just records
1792: one line-number -- core-address correspondence.
1793: Enter it in the line list for this symbol table. */
1794: record_line (desc, value);
1795: break;
1796:
1797: case N_BCOMM:
1798: case N_ECOMM:
1799: case N_ECOML:
1800: case N_LENG:
1801: break;
1802:
1803: default:
1804: if (name)
1805: define_symbol (value, name, desc);
1806: }
1807: }
1808:
1809: static struct symbol *
1810: define_symbol (value, string, desc)
1811: int value;
1812: char *string;
1813: int desc;
1814: {
1815: register struct symbol *sym
1816: = (struct symbol *) obstack_alloc (symbol_obstack, sizeof (struct symbol));
1817: char *p = (char *) index (string, ':');
1818: int deftype;
1819: register int i;
1820:
1821: /* Ignore syms with empty names. */
1822: if (string[0] == 0)
1823: return 0;
1824:
1825: SYMBOL_NAME (sym)
1826: = (char *) obstack_alloc (symbol_obstack, ((p - string) + 1));
1827: /* Open-coded bcopy--saves function call time. */
1828: {
1829: register char *p1 = string;
1830: register char *p2 = SYMBOL_NAME (sym);
1831: while (p1 != p)
1832: *p2++ = *p1++;
1833: *p2++ = '\0';
1834: }
1835: p++;
1836: /* Determine the type of name being defined. */
1837: if ((*p >= '0' && *p <= '9') || *p == '(')
1838: deftype = 'l';
1839: else
1840: deftype = *p++;
1841:
1842: /* c is a special case, not followed by a type-number.
1843: SYMBOL:c=iVALUE for an integer constant symbol.
1844: SYMBOL:c=rVALUE for a floating constant symbol. */
1845: if (deftype == 'c')
1846: {
1847: if (*p++ != '=')
1848: error ("Invalid symbol data at symtab pos %d.", symnum);
1849: switch (*p++)
1850: {
1851: case 'r':
1852: {
1853: double d = atof (p);
1854: char *value;
1855:
1856: SYMBOL_TYPE (sym) = builtin_type_double;
1857: value = (char *) obstack_alloc (symbol_obstack, sizeof (double));
1858: bcopy (&d, value, sizeof (double));
1859: SYMBOL_VALUE_BYTES (sym) = value;
1860: SYMBOL_CLASS (sym) = LOC_CONST;
1861: }
1862: break;
1863: case 'i':
1864: {
1865: SYMBOL_TYPE (sym) = builtin_type_int;
1866: SYMBOL_VALUE (sym) = atoi (p);
1867: SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
1868: }
1869: break;
1870: default:
1871: error ("Invalid symbol data at symtab pos %d.", symnum);
1872: }
1873: SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1874: add_symbol_to_list (sym, &file_symbols);
1875: return sym;
1876: }
1877:
1878: /* Now usually comes a number that says which data type,
1879: and possibly more stuff to define the type
1880: (all of which is handled by read_type) */
1881:
1882: if (deftype == 'p' && *p == 'F')
1883: /* pF is a two-letter code that means a function parameter in Fortran.
1884: The type-number specifies the type of the return value.
1885: Translate it into a pointer-to-function type. */
1886: {
1887: p++;
1888: SYMBOL_TYPE (sym)
1889: = lookup_pointer_type (lookup_function_type (read_type (&p)));
1890: }
1891: else
1892: {
1893: struct type *type = read_type (&p);
1894:
1895: if ((deftype == 'F' || deftype == 'f')
1896: && TYPE_CODE (type) != TYPE_CODE_FUNC)
1897: SYMBOL_TYPE (sym) = lookup_function_type (type);
1898: else
1899: SYMBOL_TYPE (sym) = type;
1900: }
1901:
1902: switch (deftype)
1903: {
1904: case 'f':
1905: SYMBOL_CLASS (sym) = LOC_BLOCK;
1906: SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1907: add_symbol_to_list (sym, &file_symbols);
1908: break;
1909:
1910: case 'F':
1911: SYMBOL_CLASS (sym) = LOC_BLOCK;
1912: SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1913: add_symbol_to_list (sym, &global_symbols);
1914: break;
1915:
1916: case 'G':
1917: /* For a class G (global) symbol, it appears that the
1918: value is not correct. It is necessary to search for the
1919: corresponding linker definition to find the value.
1920: These definitions appear at the end of the namelist. */
1921: i = hashname (SYMBOL_NAME (sym));
1922: SYMBOL_VALUE (sym) = (int) global_sym_chain[i];
1923: global_sym_chain[i] = sym;
1924: SYMBOL_CLASS (sym) = LOC_STATIC;
1925: SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1926: add_symbol_to_list (sym, &global_symbols);
1927: break;
1928:
1929: /* This case is faked by a conditional above,
1930: when there is no code letter in the dbx data.
1931: Dbx data never actually contains 'l'. */
1932: case 'l':
1933: SYMBOL_CLASS (sym) = LOC_LOCAL;
1934: SYMBOL_VALUE (sym) = value;
1935: SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1936: add_symbol_to_list (sym, &local_symbols);
1937: break;
1938:
1939: case 'p':
1940: SYMBOL_CLASS (sym) = LOC_ARG;
1941: SYMBOL_VALUE (sym) = value;
1942: SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1943: add_symbol_to_list (sym, &local_symbols);
1944: /* DESC == 0 implies compiled with GCC.
1945: In this case, if it says `short', believe it. */
1946: if (desc == 0)
1947: break;
1948: /* If PCC says a parameter is a short or a char,
1949: it is really an int. */
1950: if (SYMBOL_TYPE (sym) == builtin_type_char
1951: || SYMBOL_TYPE (sym) == builtin_type_short)
1952: SYMBOL_TYPE (sym) = builtin_type_int;
1953: else if (SYMBOL_TYPE (sym) == builtin_type_unsigned_char
1954: || SYMBOL_TYPE (sym) == builtin_type_unsigned_short)
1955: SYMBOL_TYPE (sym) = builtin_type_unsigned_int;
1956: break;
1957:
1958: case 'r':
1959: SYMBOL_CLASS (sym) = LOC_REGISTER;
1960: SYMBOL_VALUE (sym) = value;
1961: SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1962: add_symbol_to_list (sym, &local_symbols);
1963: break;
1964:
1965: case 'S':
1966: /* Static symbol at top level of file */
1967: SYMBOL_CLASS (sym) = LOC_STATIC;
1968: SYMBOL_VALUE (sym) = value;
1969: SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1970: add_symbol_to_list (sym, &file_symbols);
1971: break;
1972:
1973: case 't':
1974: SYMBOL_CLASS (sym) = LOC_TYPEDEF;
1975: SYMBOL_VALUE (sym) = value;
1976: SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
1977: if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0
1978: && (TYPE_FLAGS (SYMBOL_TYPE (sym)) & TYPE_FLAG_PERM) == 0)
1979: TYPE_NAME (SYMBOL_TYPE (sym)) =
1980: obsavestring (SYMBOL_NAME (sym),
1981: strlen (SYMBOL_NAME (sym)));
1982: add_symbol_to_list (sym, &file_symbols);
1983: break;
1984:
1985: case 'T':
1986: SYMBOL_CLASS (sym) = LOC_TYPEDEF;
1987: SYMBOL_VALUE (sym) = value;
1988: SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
1989: if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0
1990: && (TYPE_FLAGS (SYMBOL_TYPE (sym)) & TYPE_FLAG_PERM) == 0)
1991: TYPE_NAME (SYMBOL_TYPE (sym))
1992: = obconcat ("",
1993: (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_ENUM
1994: ? "enum "
1995: : (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_STRUCT
1996: ? "struct " : "union ")),
1997: SYMBOL_NAME (sym));
1998: add_symbol_to_list (sym, &file_symbols);
1999: break;
2000:
2001: case 'V':
2002: case 'v':
2003: /* Static symbol of local scope */
2004: SYMBOL_CLASS (sym) = LOC_STATIC;
2005: SYMBOL_VALUE (sym) = value;
2006: SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
2007: add_symbol_to_list (sym, &local_symbols);
2008: break;
2009:
2010: default:
2011: error ("Invalid symbol data: unknown symbol-type code `%c' at symtab pos %d.", deftype, symnum);
2012: }
2013: return sym;
2014: }
2015:
2016: /* Read a number by which a type is referred to in dbx data,
2017: or perhaps read a pair (FILENUM, TYPENUM) in parentheses.
2018: Just a single number N is equivalent to (0,N).
2019: Return the two numbers by storing them in the vector TYPENUMS.
2020: TYPENUMS will then be used as an argument to dbx_lookup_type. */
2021:
2022: static void
2023: read_type_number (pp, typenums)
2024: register char **pp;
2025: register int *typenums;
2026: {
2027: if (**pp == '(')
2028: {
2029: (*pp)++;
2030: typenums[0] = read_number (pp, ',');
2031: typenums[1] = read_number (pp, ')');
2032: }
2033: else
2034: {
2035: typenums[0] = 0;
2036: typenums[1] = read_number (pp, 0);
2037: }
2038: }
2039:
2040: /* Read a dbx type reference or definition;
2041: return the type that is meant.
2042: This can be just a number, in which case it references
2043: a type already defined and placed in type_vector.
2044: Or the number can be followed by an =, in which case
2045: it means to define a new type according to the text that
2046: follows the =. */
2047:
2048: static
2049: struct type *
2050: read_type (pp)
2051: register char **pp;
2052: {
2053: register struct type *type = 0;
2054: register int n;
2055: struct type *type1;
2056: int typenums[2];
2057: int xtypenums[2];
2058:
2059: read_type_number (pp, typenums);
2060:
2061: /* Detect random reference to type not yet defined.
2062: Allocate a type object but leave it zeroed. */
2063: if (**pp != '=')
2064: return dbx_alloc_type (typenums);
2065:
2066: *pp += 2;
2067: switch ((*pp)[-1])
2068: {
2069: case 'x':
2070: type = dbx_alloc_type (typenums);
2071: /* Set the type code according to the following letter. */
2072: switch ((*pp)[0])
2073: {
2074: case 's':
2075: TYPE_CODE (type) = TYPE_CODE_STRUCT;
2076: break;
2077: case 'u':
2078: TYPE_CODE (type) = TYPE_CODE_UNION;
2079: break;
2080: case 'e':
2081: TYPE_CODE (type) = TYPE_CODE_ENUM;
2082: break;
2083: }
2084: /* Skip the name the cross-ref points to. */
2085: *pp = (char *) index (*pp, ',');
2086: /* Just allocate the type and leave it zero if nothing known */
2087: return dbx_alloc_type (typenums);
2088:
2089: case '0':
2090: case '1':
2091: case '2':
2092: case '3':
2093: case '4':
2094: case '5':
2095: case '6':
2096: case '7':
2097: case '8':
2098: case '9':
2099: case '(':
2100: (*pp)--;
2101: read_type_number (pp, xtypenums);
2102: type = *dbx_lookup_type (xtypenums);
2103: if (type == 0)
2104: type = builtin_type_void;
2105: *dbx_lookup_type (typenums) = type;
2106: break;
2107:
2108: case '*':
2109: type = dbx_alloc_type (typenums);
2110: smash_to_pointer_type (type, read_type (pp));
2111: break;
2112:
2113: case 'f':
2114: type = dbx_alloc_type (typenums);
2115: smash_to_function_type (type, read_type (pp));
2116: break;
2117:
2118: case 'r':
2119: type = read_range_type (pp, typenums);
2120: *dbx_lookup_type (typenums) = type;
2121: break;
2122:
2123: case 'e':
2124: type = dbx_alloc_type (typenums);
2125: type = read_enum_type (pp, type);
2126: *dbx_lookup_type (typenums) = type;
2127: break;
2128:
2129: case 's':
2130: type = dbx_alloc_type (typenums);
2131: type = read_struct_type (pp, type);
2132: break;
2133:
2134: case 'u':
2135: type = dbx_alloc_type (typenums);
2136: type = read_struct_type (pp, type);
2137: TYPE_CODE (type) = TYPE_CODE_UNION;
2138: break;
2139:
2140: case 'a':
2141: /* Define an array type. */
2142: type = dbx_alloc_type (typenums);
2143:
2144: /* dbx expresses array types in terms of a range type for the index,
2145: and that range type is specified right inside the array type spec
2146: making ar1;MIN;MAX;VALTYPE */
2147: if (!strncmp (*pp, "r1;0;", 5))
2148: (*pp) += 5;
2149: else if (!strncmp (*pp, "r(0,1);0;", 9))
2150: (*pp) += 9;
2151: else break;
2152:
2153: TYPE_CODE (type) = TYPE_CODE_ARRAY;
2154: /* In Fortran, an upper bound may be T... meaning a parameter specifies
2155: the length of the data. In this case, just pretend the bound is 1.
2156: This happens only for array parameters, which are really passed
2157: as pointers anyway, and we will translate them into such. */
2158: if (**pp == 'T')
2159: {
2160: n = 1;
2161: while (**pp != ';')
2162: (*pp)++;
2163: }
2164: else
2165: n = read_number (pp, ';') + 1;
2166: TYPE_TARGET_TYPE (type) = read_type (pp);
2167: TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type)) * n;
2168: break;
2169:
2170: default:
2171: error ("Invalid symbol data: unrecognized type-code `%c' at symtab pos %d.",
2172: (*pp)[-1], symnum);
2173: }
2174:
2175: if (type == 0)
2176: abort ();
2177:
2178: #if 0
2179: /* If this is an overriding temporary alteration for a header file's
2180: contents, and this type number is unknown in the global definition,
2181: put this type into the global definition at this type number. */
2182: if (header_file_prev_index >= 0)
2183: {
2184: register struct type **tp
2185: = explicit_lookup_type (header_file_prev_index, typenums[1]);
2186: if (*tp == 0)
2187: *tp = type;
2188: }
2189: #endif
2190: return type;
2191: }
2192:
2193: /* This page contains subroutines of read_type. */
2194:
2195: /* Read the description of a structure (or union type)
2196: and return an object describing the type. */
2197:
2198: static struct type *
2199: read_struct_type (pp, type)
2200: char **pp;
2201: register struct type *type;
2202: {
2203: struct nextfield
2204: {
2205: struct nextfield *next;
2206: struct field field;
2207: };
2208:
2209: register struct nextfield *list = 0;
2210: struct nextfield *new;
2211: int totalsize;
2212: char *name;
2213: register char *p;
2214: int nfields = 0;
2215: register int n;
2216:
2217: TYPE_CODE (type) = TYPE_CODE_STRUCT;
2218:
2219: /* First comes the total size in bytes. */
2220:
2221: TYPE_LENGTH (type) = read_number (pp, 0);
2222:
2223: /* Now come the fields, as NAME:TYPENUM,BITPOS,BITSIZE; for each one.
2224: At the end, we see a semicolon instead of a field. */
2225:
2226: while (**pp != ';')
2227: {
2228: /* Check for and handle cretinous dbx symbol name continuation! */
2229: if (**pp == '\\')
2230: *pp = next_symbol_text ();
2231:
2232: /* Get space to record the next field's data. */
2233: new = (struct nextfield *) alloca (sizeof (struct nextfield));
2234: new->next = list;
2235: list = new;
2236:
2237: /* Read the data. */
2238: p = *pp;
2239: while (*p != ':') p++;
2240: list->field.name = obsavestring (*pp, p - *pp);
2241: *pp = p + 1;
2242: list->field.type = read_type (pp);
2243: if (**pp != ',')
2244: error ("Invalid symbol data: bad structure-type format at symtab pos %d.",
2245: symnum);
2246: (*pp)++; /* Skip the comma. */
2247: list->field.bitpos = read_number (pp, ',');
2248: list->field.bitsize = read_number (pp, ';');
2249: /* Detect an unpacked field and mark it as such.
2250: dbx gives a bit size for all fields.
2251: Note that forward refs cannot be packed,
2252: and treat enums as if they had the width of ints. */
2253: if (TYPE_CODE (list->field.type) != TYPE_CODE_INT
2254: && TYPE_CODE (list->field.type) != TYPE_CODE_ENUM)
2255: list->field.bitsize = 0;
2256: if ((list->field.bitsize == 8 * TYPE_LENGTH (list->field.type)
2257: || (TYPE_CODE (list->field.type) == TYPE_CODE_ENUM
2258: && list->field.bitsize == 8 * TYPE_LENGTH (builtin_type_int)))
2259: &&
2260: list->field.bitpos % 8 == 0)
2261: list->field.bitsize = 0;
2262: nfields++;
2263: }
2264:
2265: (*pp)++; /* Skip the terminating ';'. */
2266:
2267: /* Now create the vector of fields, and record how big it is. */
2268:
2269: TYPE_NFIELDS (type) = nfields;
2270: TYPE_FIELDS (type) = (struct field *) obstack_alloc (symbol_obstack,
2271: sizeof (struct field) * nfields);
2272:
2273: /* Copy the saved-up fields into the field vector. */
2274:
2275: for (n = nfields; list; list = list->next)
2276: TYPE_FIELD (type, --n) = list->field;
2277:
2278: return type;
2279: }
2280:
2281: /* Read a definition of an enumeration type,
2282: and create and return a suitable type object.
2283: Also defines the symbols that represent the values of the type. */
2284:
2285: static struct type *
2286: read_enum_type (pp, type)
2287: register char **pp;
2288: register struct type *type;
2289: {
2290: register char *p;
2291: char *name;
2292: register long n;
2293: register struct symbol *sym;
2294: int nsyms = 0;
2295: struct pending **symlist;
2296: struct pending *osyms, *syms;
2297: int o_nsyms;
2298:
2299: if (within_function)
2300: symlist = &local_symbols;
2301: else
2302: symlist = &file_symbols;
2303: osyms = *symlist;
2304: o_nsyms = osyms ? osyms->nsyms : 0;
2305:
2306: /* Read the value-names and their values.
2307: The input syntax is NAME:VALUE,NAME:VALUE, and so on.
2308: A semicolon instead of a NAME means the end. */
2309: while (**pp && **pp != ';')
2310: {
2311: /* Check for and handle cretinous dbx symbol name continuation! */
2312: if (**pp == '\\')
2313: *pp = next_symbol_text ();
2314:
2315: p = *pp;
2316: while (*p != ':') p++;
2317: name = obsavestring (*pp, p - *pp);
2318: *pp = p + 1;
2319: n = read_number (pp, ',');
2320:
2321: sym = (struct symbol *) obstack_alloc (symbol_obstack, sizeof (struct symbol));
2322: bzero (sym, sizeof (struct symbol));
2323: SYMBOL_NAME (sym) = name;
2324: SYMBOL_CLASS (sym) = LOC_CONST;
2325: SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
2326: SYMBOL_VALUE (sym) = n;
2327: add_symbol_to_list (sym, symlist);
2328: nsyms++;
2329: }
2330:
2331: (*pp)++; /* Skip the semicolon. */
2332:
2333: /* Now fill in the fields of the type-structure. */
2334:
2335: TYPE_LENGTH (type) = sizeof (int);
2336: TYPE_CODE (type) = TYPE_CODE_ENUM;
2337: TYPE_NFIELDS (type) = nsyms;
2338: TYPE_FIELDS (type) = (struct field *) obstack_alloc (symbol_obstack, sizeof (struct field) * nsyms);
2339:
2340: /* Find the symbols for the values and put them into the type.
2341: The symbols can be found in the symlist that we put them on
2342: to cause them to be defined. osyms contains the old value
2343: of that symlist; everything up to there was defined by us. */
2344:
2345: for (syms = *symlist, n = nsyms; syms; syms = syms->next)
2346: {
2347: int j = 0;
2348: if (syms == osyms)
2349: j = o_nsyms;
2350: for (; j < syms->nsyms; j++)
2351: {
2352: struct symbol *sym = syms->symbol[j];
2353: SYMBOL_TYPE (sym) = type;
2354: TYPE_FIELD_NAME (type, --n) = SYMBOL_NAME (sym);
2355: TYPE_FIELD_VALUE (type, n) = SYMBOL_VALUE (sym);
2356: TYPE_FIELD_BITPOS (type, n) = 0;
2357: TYPE_FIELD_BITSIZE (type, n) = 0;
2358: }
2359: if (syms == osyms)
2360: break;
2361: }
2362:
2363: return type;
2364: }
2365:
2366: static struct type *
2367: read_range_type (pp, typenums)
2368: char **pp;
2369: int typenums[2];
2370: {
2371: char *errp = *pp;
2372: int rangenums[2];
2373: int n1, n2, n3;
2374:
2375: /* First comes a type we are a subrange of.
2376: In practice it is usually 0, 1 or the type being defined. */
2377: read_type_number (pp, rangenums);
2378: n1 = rangenums[1];
2379:
2380: /* A semicolon should now follow; skip it. */
2381: if (**pp == ';')
2382: (*pp)++;
2383:
2384: /* The remaining two operands are usually lower and upper bounds
2385: of the range. But in some special cases they mean something else. */
2386: n2 = read_number (pp, ';');
2387: n3 = read_number (pp, ';');
2388:
2389: /* A type defined as a subrange of itself, with bounds both 0, is void. */
2390: if (rangenums[0] == typenums[0] && rangenums[1] == typenums[1]
2391: && n2 == 0 && n3 == 0)
2392: return builtin_type_void;
2393:
2394: /* If n3 is zero and n2 is not, we want a floating type,
2395: and n2 is the width in bytes.
2396:
2397: Fortran programs appear to use this for complex types also,
2398: and they give no way to distinguish between double and single-complex!
2399: We don't have complex types, so we would lose on all fortran files!
2400: So return type `double' for all of those. It won't work right
2401: for the complex values, but at least it makes the file loadable. */
2402:
2403: if (n3 == 0 && n2 > 0)
2404: {
2405: if (n2 == sizeof (float))
2406: return builtin_type_float;
2407: return builtin_type_double;
2408: }
2409:
2410: /* If the upper bound is -1, it must really be an unsigned int. */
2411:
2412: else if (n2 == 0 && n3 == -1)
2413: {
2414: if (sizeof (int) == sizeof (long))
2415: return builtin_type_unsigned_int;
2416: else
2417: return builtin_type_unsigned_long;
2418: }
2419:
2420: /* Detect unsigned subranges of int. Int is normally 1.
2421: Note that `char' is usually given bounds of 0 to 127,
2422: and would therefore appear unsigned; but it is described
2423: as a subrange of itself, so we reject it here. */
2424:
2425: else if (n2 == 0 && n1 == 1)
2426: {
2427: /* an unsigned type */
2428: if (n3 == (1 << (8 * sizeof (int))) - 1)
2429: return builtin_type_unsigned_int;
2430: if (n3 == (1 << (8 * sizeof (short))) - 1)
2431: return builtin_type_unsigned_short;
2432: if (n3 == (1 << (8 * sizeof (char))) - 1)
2433: return builtin_type_unsigned_char;
2434: }
2435: else
2436: {
2437: /* a signed type */
2438: if (n3 == (1 << (8 * sizeof (int) - 1)) - 1)
2439: return builtin_type_int;
2440: if (n3 == (1 << (8 * sizeof (long) - 1)) - 1)
2441: return builtin_type_long;
2442: if (n3 == (1 << (8 * sizeof (short) - 1)) - 1)
2443: return builtin_type_short;
2444: if (n3 == (1 << (8 * sizeof (char) - 1)) - 1)
2445: return builtin_type_char;
2446: }
2447: error ("Invalid symbol data: range type spec %s at symtab pos %d.",
2448: errp - 1, symnum);
2449: }
2450:
2451: /* Read a number from the string pointed to by *PP.
2452: The value of *PP is advanced over the number.
2453: If END is nonzero, the character that ends the
2454: number must match END, or an error happens;
2455: and that character is skipped if it does match.
2456: If END is zero, *PP is left pointing to that character. */
2457:
2458: static long
2459: read_number (pp, end)
2460: char **pp;
2461: int end;
2462: {
2463: register char *p = *pp;
2464: register long n = 0;
2465: register int c;
2466: int sign = 1;
2467:
2468: /* Handle an optional leading minus sign. */
2469:
2470: if (*p == '-')
2471: {
2472: sign = -1;
2473: p++;
2474: }
2475:
2476: /* Read the digits, as far as they go. */
2477:
2478: while ((c = *p++) >= '0' && c <= '9')
2479: {
2480: n *= 10;
2481: n += c - '0';
2482: }
2483: if (end)
2484: {
2485: if (c != end)
2486: error ("Invalid symbol data: invalid character \\%03o at symbol pos %d.", c, symnum);
2487: }
2488: else
2489: --p;
2490:
2491: *pp = p;
2492: return n * sign;
2493: }
2494:
2495: static
2496: initialize ()
2497: {
2498: symfile = 0;
2499:
2500: add_com ("symbol-file", class_files, symbol_file_command,
2501: "Load symbol table (in dbx format) from executable file FILE.");
2502: }
2503:
2504: END_FILE
2505:
2506: #endif /* READ_DBX_FORMAT */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.