]> Git Repo - binutils.git/blob - gdb/symmisc.c
gdb-2.4+.aux.coff
[binutils.git] / gdb / symmisc.c
1 /* Do various things to symbol tables (other than lookup)), for GDB.
2    Copyright (C) 1986, 1987 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
22 #include "defs.h"
23 #include "initialize.h"
24 #include "symtab.h"
25
26 #include <stdio.h>
27 #include <obstack.h>
28
29 static void free_symtab ();
30
31 START_FILE
32 \f
33 /* Free all the symtabs that are currently installed,
34    and all storage associated with them.
35    Leaves us in a consistent state with no symtabs installed.  */
36
37 void
38 free_all_symtabs ()
39 {
40   register struct symtab *s, *snext;
41
42   /* All values will be invalid because their types will be!  */
43
44   clear_value_history ();
45   clear_displays ();
46   clear_internalvars ();
47   clear_breakpoints ();
48   set_default_breakpoint (0, 0, 0, 0);
49
50   current_source_symtab = 0;
51
52   for (s = symtab_list; s; s = snext)
53     {
54       snext = s->next;
55       free_symtab (s);
56     }
57   symtab_list = 0;
58   obstack_free (symbol_obstack, 0);
59   obstack_init (symbol_obstack);
60
61   if (misc_function_vector)
62     free (misc_function_vector);
63   misc_function_count = 0;
64   misc_function_vector = 0;
65 }
66
67 /* Free a struct block <- B and all the symbols defined in that block.  */
68
69 static void
70 free_symtab_block (b)
71      struct block *b;
72 {
73   register int i, n;
74   n = BLOCK_NSYMS (b);
75   for (i = 0; i < n; i++)
76     {
77       free (SYMBOL_NAME (BLOCK_SYM (b, i)));
78       free (BLOCK_SYM (b, i));
79     }
80   free (b);
81 }
82
83 /* Free all the storage associated with the struct symtab <- S.
84    Note that some symtabs have contents malloc'ed structure by structure,
85    while some have contents that all live inside one big block of memory,
86    and some share the contents of another symbol table and so you should
87    not free the contents on their behalf (except sometimes the linetable,
88    which maybe per symtab even when the rest is not).
89    It is s->free_code that says which alternative to use.  */
90
91 static void
92 free_symtab (s)
93      register struct symtab *s;
94 {
95   register int i, n;
96   register struct blockvector *bv;
97   register struct type *type;
98   register struct typevector *tv;
99
100   switch (s->free_code)
101     {
102     case free_nothing:
103       /* All the contents are part of a big block of memory
104          and some other symtab is in charge of freeing that block.
105          Therefore, do nothing.  */
106       break;
107
108     case free_explicit:
109       /* All the contents are part of a big block of memory
110          and that is our `free_ptr' and will be freed below.  */
111       break;
112
113     case free_contents:
114       /* Here all the contents were malloc'ed structure by structure
115          and must be freed that way.  */
116       /* First free the blocks (and their symbols.  */
117       bv = BLOCKVECTOR (s);
118       n = BLOCKVECTOR_NBLOCKS (bv);
119       for (i = 0; i < n; i++)
120         free_symtab_block (BLOCKVECTOR_BLOCK (bv, i));
121       /* Free the blockvector itself.  */
122       free (bv);
123       /* Free the type vector.  */
124       tv = TYPEVECTOR (s);
125       if (tv)           /* FIXME, should this happen?  It does... */
126         free (tv);
127       /* Also free the linetable.  */
128       
129     case free_linetable:
130       /* Everything will be freed either by our `free_ptr'
131          or by some other symbatb, except for our linetable.
132          Free that now.  */
133       free (LINETABLE (s));
134       break;
135     }
136
137   /* If there is a single block of memory to free, free it.  */
138   if (s->free_ptr)
139     free (s->free_ptr);
140
141   if (s->line_charpos)
142     free (s->line_charpos);
143   free (s->filename);
144   free (s);
145 }
146 \f
147 /* Convert a raw symbol-segment to a struct symtab,
148    and relocate its internal pointers so that it is valid.  */
149
150 /* This is how to relocate one pointer, given a name for it.
151    Works independent of the type of object pointed to.  */
152 #define RELOCATE(slot) (slot ? (* (char **) &slot += relocation) : 0)
153
154 /* This is the inverse of RELOCATE.  We use it when storing
155    a core address into a slot that has yet to be relocated.  */
156 #define UNRELOCATE(slot) (slot ? (* (char **) &slot -= relocation) : 0)
157
158 /* During the process of relocation, this holds the amount to relocate by
159    (the address of the file's symtab data, in core in the debugger).  */
160 static int relocation;
161
162 #define CORE_RELOCATE(slot) \
163   ((slot) += (((slot) < data_start) ? text_relocation           \
164               : ((slot) < bss_start) ? data_relocation : bss_relocation))
165
166 #define TEXT_RELOCATE(slot)  ((slot) += text_relocation)
167
168 /* Relocation amounts for addresses in the program's core image.  */
169 static int text_relocation, data_relocation, bss_relocation;
170
171 /* Boundaries that divide program core addresses into text, data and bss;
172    used to determine which relocation amount to use.  */
173 static int data_start, bss_start;
174
175 static void relocate_typevector ();
176 static void relocate_blockvector ();
177 static void relocate_type ();
178 static void relocate_block ();
179 static void relocate_symbol ();
180
181 /* Relocate a file symbol table so that all the pointers
182    are valid C pointers.  Pass the struct symtab for the file
183    and the amount to relocate by.  */
184
185 static struct symtab *
186 relocate_symtab (root)
187      struct symbol_root *root;
188 {
189   struct symtab *sp = (struct symtab *) xmalloc (sizeof (struct symtab));
190   bzero (sp, sizeof (struct symtab));
191
192   relocation = (int) root;
193   text_relocation = root->textrel;
194   data_relocation = root->datarel;
195   bss_relocation = root->bssrel;
196   data_start = root->databeg;
197   bss_start = root->bssbeg;
198
199   sp->filename = root->filename;
200   sp->ldsymoff = root->ldsymoff;
201   sp->language = root->language;
202   sp->compilation = root->compilation;
203   sp->version = root->version;
204   sp->blockvector = root->blockvector;
205   sp->typevector = root->typevector;
206   sp->free_code = free_explicit;
207   sp->free_ptr = (char *) root;
208
209   RELOCATE (TYPEVECTOR (sp));
210   RELOCATE (BLOCKVECTOR (sp));
211   RELOCATE (sp->version);
212   RELOCATE (sp->compilation);
213   RELOCATE (sp->filename);
214
215   relocate_typevector (TYPEVECTOR (sp));
216   relocate_blockvector (BLOCKVECTOR (sp));
217
218   return sp;
219 }
220
221 static void
222 relocate_typevector (tv)
223      struct typevector *tv;
224 {
225   register int ntypes = TYPEVECTOR_NTYPES (tv);
226   register int i;
227
228   for (i = 0; i < ntypes; i++)
229     RELOCATE (TYPEVECTOR_TYPE (tv, i));
230   for (i = 0; i < ntypes; i++)
231     relocate_type (TYPEVECTOR_TYPE (tv, i));
232 }
233
234 static void
235 relocate_blockvector (blp)
236      register struct blockvector *blp;
237 {
238   register int nblocks = BLOCKVECTOR_NBLOCKS (blp);
239   register int i;
240   for (i = 0; i < nblocks; i++)
241     RELOCATE (BLOCKVECTOR_BLOCK (blp, i));
242   for (i = 0; i < nblocks; i++)
243     relocate_block (BLOCKVECTOR_BLOCK (blp, i));
244 }
245
246 static void
247 relocate_block (bp)
248      register struct block *bp;
249 {
250   register int nsyms = BLOCK_NSYMS (bp);
251   register int i;
252
253   TEXT_RELOCATE (BLOCK_START (bp));
254   TEXT_RELOCATE (BLOCK_END (bp));
255
256   /* These two should not be recursively processed.
257      The superblock need not be because all blocks are
258      processed from relocate_blockvector.
259      The function need not be because it will be processed
260      under the block which is its scope.  */
261   RELOCATE (BLOCK_SUPERBLOCK (bp));
262   RELOCATE (BLOCK_FUNCTION (bp));
263
264   for (i = 0; i < nsyms; i++)
265     RELOCATE (BLOCK_SYM (bp, i));
266
267   for (i = 0; i < nsyms; i++)
268     relocate_symbol (BLOCK_SYM (bp, i));
269 }
270
271 static void
272 relocate_symbol (sp)
273      register struct symbol *sp;
274 {
275   RELOCATE (SYMBOL_NAME (sp));
276   if (SYMBOL_CLASS (sp) == LOC_BLOCK)
277     {
278       RELOCATE (SYMBOL_BLOCK_VALUE (sp));
279       /* We can assume the block that belongs to this symbol
280          is not relocated yet, since it comes after
281          the block that contains this symbol.  */
282       BLOCK_FUNCTION (SYMBOL_BLOCK_VALUE (sp)) = sp;
283       UNRELOCATE (BLOCK_FUNCTION (SYMBOL_BLOCK_VALUE (sp)));
284     }
285   else if (SYMBOL_CLASS (sp) == LOC_STATIC)
286     CORE_RELOCATE (SYMBOL_VALUE (sp));
287   else if (SYMBOL_CLASS (sp) == LOC_LABEL)
288     TEXT_RELOCATE (SYMBOL_VALUE (sp));
289   RELOCATE (SYMBOL_TYPE (sp));
290 }
291
292 /* We cannot come up with an a priori spanning tree
293    for the network of types, since types can be used
294    for many symbols and also as components of other types.
295    Therefore, we need to be able to mark types that we
296    already have relocated (or are already in the middle of relocating)
297    as in a garbage collector.  */
298
299 static void
300 relocate_type (tp)
301      register struct type *tp;
302 {
303   register int nfields = TYPE_NFIELDS (tp);
304   register int i;
305
306   RELOCATE (TYPE_NAME (tp));
307   RELOCATE (TYPE_TARGET_TYPE (tp));
308   RELOCATE (TYPE_FIELDS (tp));
309   RELOCATE (TYPE_POINTER_TYPE (tp));
310
311   for (i = 0; i < nfields; i++)
312     {
313       RELOCATE (TYPE_FIELD_TYPE (tp, i));
314       RELOCATE (TYPE_FIELD_NAME (tp, i));
315     }
316 }
317 \f
318 /* Read symsegs from file named NAME open on DESC,
319    make symtabs from them, and return a chain of them.
320    Assumes DESC is prepositioned at the end of the string table,
321    just before the symsegs if there are any.  */
322
323 struct symtab *
324 read_symsegs (desc, name)
325      int desc;
326      char *name;
327 {
328   struct symbol_root root;
329   register char *data;
330   register struct symtab *sp, *chain = 0;
331   register int len;
332
333   while (1)
334     {
335       len = myread (desc, &root, sizeof root);
336       if (len == 0 || root.format == 0)
337         break;
338       if (root.format != 1 ||
339           root.length < sizeof root)
340         error ("Invalid symbol segment format code");
341       data = (char *) xmalloc (root.length);
342       bcopy (&root, data, sizeof root);
343       len = myread (desc, data + sizeof root,
344                     root.length - sizeof root);
345       sp = relocate_symtab (data);
346       sp->next = chain;
347       chain = sp;
348     }
349
350   return chain;
351 }
352 \f
353 static int block_depth ();
354 static void print_spaces ();
355 static void print_symbol ();
356
357 print_symtabs (filename)
358      char *filename;
359 {
360   FILE *outfile;
361   register struct symtab *s;
362   register int i, j;
363   int len, line, blen;
364   register struct linetable *l;
365   struct blockvector *bv;
366   register struct block *b;
367   int depth;
368   struct cleanup *cleanups;
369   extern int fclose();
370
371   if (filename == 0)
372     error_no_arg ("file to write symbol data in");
373   outfile = fopen (filename, "w");
374
375   cleanups = make_cleanup (fclose, outfile);
376   immediate_quit++;
377
378   for (s = symtab_list; s; s = s->next)
379     {
380       /* First print the line table.  */
381       fprintf (outfile, "Symtab for file %s\n\n", s->filename);
382       fprintf (outfile, "Line table:\n\n");
383       l = LINETABLE (s);
384       len = l->nitems;
385       for (i = 0; i < len; i++)
386         {
387           if (l->item[i] < 0)
388             line = - l->item[i] - 1;
389           else
390             fprintf (outfile, " line %d at %x\n", ++line, l->item[i]);
391         }
392       /* Now print the block info.  */
393       fprintf (outfile, "\nBlockvector:\n\n");
394       bv = BLOCKVECTOR (s);
395       len = BLOCKVECTOR_NBLOCKS (bv);
396       for (i = 0; i < len; i++)
397         {
398           b = BLOCKVECTOR_BLOCK (bv, i);
399           depth = block_depth (b) * 2;
400           print_spaces (depth, outfile);
401           fprintf (outfile, "block #%03d (object 0x%x) ", i, b);
402           fprintf (outfile, "[0x%x..0x%x]", BLOCK_START (b), BLOCK_END (b));
403           if (BLOCK_SUPERBLOCK (b))
404             fprintf (outfile, " (under 0x%x)", BLOCK_SUPERBLOCK (b));
405           if (BLOCK_FUNCTION (b))
406             fprintf (outfile, " %s", SYMBOL_NAME (BLOCK_FUNCTION (b)));
407           fputc ('\n', outfile);
408           blen = BLOCK_NSYMS (b);
409           for (j = 0; j < blen; j++)
410             {
411               print_symbol (BLOCK_SYM (b, j), depth + 1, outfile);
412             }
413         }
414
415       fprintf (outfile, "\n\n");
416     }
417
418   immediate_quit--;
419   do_cleanups (cleanups);
420 }
421
422 static void
423 print_symbol (symbol, depth, outfile)
424      struct symbol *symbol;
425      int depth;
426      FILE *outfile;
427 {
428   print_spaces (depth, outfile);
429   if (SYMBOL_NAMESPACE (symbol) == LABEL_NAMESPACE)
430     {
431       fprintf (outfile, "label %s at 0x%x", SYMBOL_NAME (symbol),
432                SYMBOL_VALUE (symbol));
433       return;
434     }
435   if (SYMBOL_NAMESPACE (symbol) == STRUCT_NAMESPACE)
436     {
437       if (TYPE_NAME (SYMBOL_TYPE (symbol)))
438         {
439           type_print_1 (SYMBOL_TYPE (symbol), "", outfile, 1, depth);
440         }
441       else
442         {
443           fprintf (outfile, "%s %s = ",
444                (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_ENUM
445                 ? "enum"
446                 : (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_STRUCT
447                    ? "struct" : "union")),
448                SYMBOL_NAME (symbol));
449           type_print_1 (SYMBOL_TYPE (symbol), "", outfile, 1, depth);
450         }
451       fprintf (outfile, ";\n");
452     }
453   else
454     {
455       if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
456         fprintf (outfile, "typedef ");
457       if (SYMBOL_TYPE (symbol))
458         {
459           type_print_1 (SYMBOL_TYPE (symbol), SYMBOL_NAME (symbol),
460                         outfile, 1, depth);
461           fprintf (outfile, "; ");
462         }
463       else
464         fprintf (outfile, "%s ", SYMBOL_NAME (symbol));
465
466       switch (SYMBOL_CLASS (symbol))
467         {
468         case LOC_CONST:
469           fprintf (outfile, "const %d (0x%x),",
470                    SYMBOL_VALUE (symbol), SYMBOL_VALUE (symbol));
471           break;
472
473         case LOC_CONST_BYTES:
474           fprintf (outfile, "const %d hex bytes:",
475                    TYPE_LENGTH (SYMBOL_TYPE (symbol)));
476           {
477             int i;
478             for (i = 0; i < TYPE_LENGTH (SYMBOL_TYPE (symbol)); i++)
479               fprintf (outfile, " %2x", SYMBOL_VALUE_BYTES (symbol) [i]);
480             fprintf (outfile, ",");
481           }
482           break;
483
484         case LOC_STATIC:
485           fprintf (outfile, "static at 0x%x,", SYMBOL_VALUE (symbol));
486           break;
487
488         case LOC_REGISTER:
489           fprintf (outfile, "register %d,", SYMBOL_VALUE (symbol));
490           break;
491
492         case LOC_ARG:
493           fprintf (outfile, "arg at 0x%x,", SYMBOL_VALUE (symbol));
494           break;
495
496         case LOC_LOCAL:
497           fprintf (outfile, "local at 0x%x,", SYMBOL_VALUE (symbol));
498           break;
499
500         case LOC_TYPEDEF:
501           break;
502
503         case LOC_LABEL:
504           fprintf (outfile, "label at 0x%x", SYMBOL_VALUE (symbol));
505           break;
506
507         case LOC_BLOCK:
508           fprintf (outfile, "block (object 0x%x) starting at 0x%x,",
509                    SYMBOL_VALUE (symbol),
510                    BLOCK_START (SYMBOL_BLOCK_VALUE (symbol)));
511           break;
512         }
513     }
514   fprintf (outfile, "\n");
515 }
516
517 /* Return the nexting depth of a block within other blocks in its symtab.  */
518
519 static int
520 block_depth (block)
521      struct block *block;
522 {
523   register int i = 0;
524   while (block = BLOCK_SUPERBLOCK (block)) i++;
525   return i;
526 }
527 \f
528 static
529 initialize ()
530 {
531   add_com ("printsyms", class_obscure, print_symtabs,
532            "Print dump of current symbol definitions to file OUTFILE.");
533 }
534
535 END_FILE
This page took 0.054628 seconds and 4 git commands to generate.