* coffgrok.c (do_where): Make data with no type 'int'.
[external/binutils.git] / binutils / coffgrok.c
1
2
3 /* coffgrok.c
4
5    Copyright (C) 1994 Free Software Foundation, Inc.
6
7 This file is part of GNU Binutils.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
22
23 /* Written by Steve Chamberlain (sac@cygnus.com)
24
25    This module reads a coff file and builds a really simple type tree
26    which can be read by other programs.  The first application is a
27    coff->sysroff converter.  It can be tested with coffdump.c.
28
29 */
30
31 #include <bfd.h>
32 #include <stdio.h>
33
34 #include "coff/internal.h"
35 #include "../bfd/libcoff.h"
36 #include "coffgrok.h"
37 int lofile = 1;
38 static struct coff_scope *top_scope;
39 static struct coff_scope *file_scope;
40 static struct coff_ofile *ofile;
41
42 struct coff_symbol *last_function_symbol;
43 struct coff_type *last_function_type;
44 struct coff_type *last_struct;
45 struct coff_type *last_enum;
46 struct coff_sfile *cur_sfile;
47
48 static struct coff_symbol **tindex;
49
50
51 static asymbol **syms;
52 static long symcount;
53
54 #define N(x) ((x)->_n._n_nptr[1])
55
56 static struct coff_ptr_struct *rawsyms;
57 static int rawcount;
58 static bfd *abfd;
59 extern char *xcalloc ();
60 extern char *xmalloc ();
61 #define PTR_SIZE        4
62 #define SHORT_SIZE      2
63 #define INT_SIZE        4
64 #define LONG_SIZE       4
65 #define FLOAT_SIZE      4
66 #define DOUBLE_SIZE     8
67
68 #define INDEXOF(p)  ((struct coff_ptr_struct *)(p)-(rawsyms))
69
70 static struct coff_scope *
71 empty_scope ()
72 {
73   struct coff_scope *l;
74   l = (struct coff_scope *) (xcalloc (sizeof (struct coff_scope), 1));
75   return l;
76 }
77
78 static struct coff_symbol *
79 empty_symbol ()
80 {
81   return (struct coff_symbol *) (xcalloc (sizeof (struct coff_symbol), 1));
82 }
83
84 /*int l;*/
85 static void
86 push_scope (link)
87 {
88   struct coff_scope *n = empty_scope ();
89   if (link)
90     {
91       if (top_scope)
92         {
93           if (top_scope->list_tail)
94             {
95               top_scope->list_tail->next = n;
96             }
97           else
98             {
99               top_scope->list_head = n;
100             }
101           top_scope->list_tail = n;
102         }
103     }
104   n->parent = top_scope;
105
106   top_scope = n;
107 }
108
109 static void
110 pop_scope ()
111 {
112   top_scope = top_scope->parent;
113 }
114
115 static void
116 do_sections_p1 (head)
117      struct coff_ofile *head;
118 {
119   asection *section;
120   int idx;
121   struct coff_section *all = (struct coff_section *) (xcalloc (abfd->section_count + 1,
122                                              sizeof (struct coff_section)));
123   head->nsections = abfd->section_count + 1;
124   head->sections = all;
125
126   for (idx = 0, section = abfd->sections; section; section = section->next, idx++)
127     {
128       long relsize;
129       int i = section->target_index;
130       arelent **relpp;
131       long relcount;
132
133       relsize = bfd_get_reloc_upper_bound (abfd, section);
134       if (relsize < 0)
135         bfd_fatal (bfd_get_filename (abfd));
136       relpp = (arelent **) xmalloc (relsize);
137       relcount = bfd_canonicalize_reloc (abfd, section, relpp, syms);
138       if (relcount < 0)
139         bfd_fatal (bfd_get_filename (abfd));
140
141       head->sections[i].name = (char *) (section->name);
142       head->sections[i].code = section->flags & SEC_CODE;
143       head->sections[i].data = section->flags & SEC_DATA;
144       if (strcmp (section->name, ".bss") == 0)
145         head->sections[i].data = 1;
146       head->sections[i].address = section->vma;
147       head->sections[i].size = section->_raw_size;
148       head->sections[i].number = idx;
149       head->sections[i].nrelocs = section->reloc_count;
150       head->sections[i].relocs =
151         (struct coff_reloc *) (xcalloc (section->reloc_count,
152                                         sizeof (struct coff_reloc)));
153       head->sections[i].bfd_section = section;
154     }
155   head->sections[0].name = "ABSOLUTE";
156   head->sections[0].code = 0;
157   head->sections[0].data = 0;
158   head->sections[0].address = 0;
159   head->sections[0].size = 0;
160   head->sections[0].number = 0;
161 }
162
163 static void
164 do_sections_p2 (head)
165      struct coff_ofile *head;
166 {
167   asection *section;
168   for (section = abfd->sections; section; section = section->next)
169     {
170       int j;
171       for (j = 0; j < section->reloc_count; j++)
172         {
173           int idx;
174           int i = section->target_index;
175           struct coff_reloc *r = head->sections[i].relocs + j;
176           arelent *sr = section->relocation + j;
177           r->offset = sr->address;
178           r->addend = sr->addend;
179           idx = ((coff_symbol_type *) (sr->sym_ptr_ptr[0]))->native - rawsyms;
180           r->symbol = tindex[idx];
181         }
182     }
183 }
184
185 static struct coff_where *
186 do_where (i)
187      int i;
188 {
189   struct internal_syment *sym = &rawsyms[i].u.syment;
190   struct coff_where *where
191   = (struct coff_where *) (malloc (sizeof (struct coff_where)));
192   where->offset = sym->n_value;
193
194   if (sym->n_scnum == -1)
195     sym->n_scnum = 0;
196
197   switch (sym->n_sclass)
198     {
199     case C_FIELD:
200       where->where = coff_where_member_of_struct;
201       where->offset = sym->n_value / 8;
202       where->bitoffset = sym->n_value % 8;
203       where->bitsize = rawsyms[i + 1].u.auxent.x_sym.x_misc.x_lnsz.x_size;
204       break;
205     case C_MOE:
206       where->where = coff_where_member_of_enum;
207       break;
208     case C_MOS:
209     case C_MOU:
210       where->where = coff_where_member_of_struct;
211       break;
212     case C_AUTO:
213     case C_ARG:
214       where->where = coff_where_stack;
215       break;
216     case C_EXT:
217     case C_STAT:
218     case C_EXTDEF:
219     case C_LABEL:
220       where->where = coff_where_memory;
221       where->section = &ofile->sections[sym->n_scnum];
222       break;
223     case C_REG:
224     case C_REGPARM:
225       where->where = coff_where_register;
226       break;
227     case C_ENTAG:
228       where->where = coff_where_entag;
229       break;
230     case C_STRTAG:
231     case C_UNTAG:
232       where->where = coff_where_strtag;
233       break;
234     case C_TPDEF:
235       where->where = coff_where_typedef;
236       break;
237     default:
238       abort ();
239       break;
240     }
241   return where;
242 }
243
244 static
245 struct coff_line *
246 do_lines (i, name)
247      int i;
248      char *name;
249 {
250   struct coff_line *res = (struct coff_line *) xcalloc (sizeof (struct coff_line), 1);
251   asection *s;
252   int l;
253   /* Find out if this function has any line numbers in the table */
254   for (s = abfd->sections; s; s = s->next)
255     {
256       for (l = 0; l < s->lineno_count; l++)
257         {
258           if (s->lineno[l].line_number == 0)
259             {
260               if (rawsyms + i == ((coff_symbol_type *) (&(s->lineno[l].u.sym[0])))->native)
261                 {
262                   /* These lines are for this function - so count them and stick them on */
263                   int c = 0;
264                   /* Find the linenumber of the top of the function, since coff linenumbers
265                      are relative to the start of the function. */
266                   int start_line = rawsyms[i + 3].u.auxent.x_sym.x_misc.x_lnsz.x_lnno;
267
268                   l++;
269                   for (c = 0; s->lineno[l + c + 1].line_number; c++)
270                     ;
271
272                   /* Add two extra records, one for the prologue and one for the epilogue */
273                   c += 1;
274                   res->nlines = c;
275                   res->lines = (int *) (xcalloc (sizeof (int), c));
276                   res->addresses = (int *) (xcalloc (sizeof (int), c));
277                   res->lines[0] = start_line;
278                   res->addresses[0] = rawsyms[i].u.syment.n_value - s->vma;
279                   for (c = 0; s->lineno[l + c + 1].line_number; c++)
280                     {
281                       res->lines[c + 1] = s->lineno[l + c].line_number + start_line - 1;
282                       res->addresses[c + 1] = s->lineno[l + c].u.offset;
283                     }
284                   return res;
285                 }
286             }
287         }
288     }
289   return res;
290 }
291
292 static
293 struct coff_type *
294 do_type (i)
295      int i;
296 {
297   struct internal_syment *sym = &rawsyms[i].u.syment;
298   union internal_auxent *aux = &rawsyms[i + 1].u.auxent;
299   struct coff_type *res = (struct coff_type *) malloc (sizeof (struct coff_type));
300   int type = sym->n_type;
301   int which_dt = 0;
302
303   res->type = coff_basic_type;
304   res->u.basic = type & 0xf;
305
306   switch (type & 0xf)
307     {
308     case T_NULL:
309     case T_VOID:
310       if (sym->n_numaux && sym->n_sclass == C_STAT)
311         {
312           /* This is probably a section definition */
313           res->type = coff_secdef_type;
314           res->size = aux->x_scn.x_scnlen;
315         }
316       else
317         {
318           if (type == 0)
319             {
320               /* Don't know what this is, let's make it a simple int */
321               res->size = INT_SIZE;
322               res->u.basic = T_UINT;
323             }
324           else
325             {
326               /* Else it could be a function or pointer to void */
327               res->size = 0;
328             }
329         }
330       break;
331
332
333       break;
334     case T_UCHAR:
335     case T_CHAR:
336       res->size = 1;
337       break;
338     case T_USHORT:
339     case T_SHORT:
340       res->size = SHORT_SIZE;
341       break;
342     case T_UINT:
343     case T_INT:
344       res->size = INT_SIZE;
345       break;
346     case T_ULONG:
347     case T_LONG:
348       res->size = LONG_SIZE;
349       break;
350     case T_FLOAT:
351       res->size = FLOAT_SIZE;
352       break;
353     case T_DOUBLE:
354       res->size = DOUBLE_SIZE;
355       break;
356     case T_STRUCT:
357     case T_UNION:
358       if (sym->n_numaux)
359         {
360           if (aux->x_sym.x_tagndx.p)
361             {
362               /* Refering to a struct defined elsewhere */
363               res->type = coff_structref_type;
364               res->u.astructref.ref = tindex[INDEXOF (aux->x_sym.x_tagndx.p)];
365               res->size = res->u.astructref.ref ?
366                 res->u.astructref.ref->type->size : 0;
367             }
368           else
369             {
370               /* A definition of a struct */
371               last_struct = res;
372               res->type = coff_structdef_type;
373               res->u.astructdef.elements = empty_scope ();
374               res->u.astructdef.isstruct = (type & 0xf) == T_STRUCT;
375               res->size = aux->x_sym.x_misc.x_lnsz.x_size;
376             }
377         }
378       else
379         {
380           /* No auxents - it's anonynmous */
381           res->type = coff_structref_type;
382           res->u.astructref.ref = 0;
383           res->size = 0;
384         }
385       break;
386     case T_ENUM:
387       if (aux->x_sym.x_tagndx.p)
388         {
389           /* Refering to a enum defined elsewhere */
390           res->type = coff_enumref_type;
391           res->u.aenumref.ref = tindex[INDEXOF (aux->x_sym.x_tagndx.p)];
392           res->size = res->u.aenumref.ref->type->size;
393         }
394       else
395         {
396           /* A definition of an enum */
397           last_enum = res;
398           res->type = coff_enumdef_type;
399           res->u.aenumdef.elements = empty_scope ();
400           res->size = aux->x_sym.x_misc.x_lnsz.x_size;
401         }
402       break;
403     case T_MOE:
404       break;
405     }
406
407   for (which_dt = 5; which_dt >= 0; which_dt--)
408     {
409       switch ((type >> ((which_dt * 2) + 4)) & 0x3)
410         {
411         case 0:
412           break;
413         case DT_ARY:
414           {
415
416             /* Look in the auxent for the dimensions and build lots of dims */
417             int els = aux->x_sym.x_fcnary.x_ary.x_dimen[which_dt];
418             if (els)
419               {
420                 struct coff_type *ptr = (struct coff_type *) malloc (sizeof (struct coff_type));
421                 ptr->type = coff_array_type;
422                 ptr->size = els * res->size;
423                 ptr->u.array.dim = els;
424                 ptr->u.array.array_of = res;
425                 res = ptr;
426               }
427           }
428
429           break;
430         case DT_PTR:
431           {
432             struct coff_type *ptr = (struct coff_type *) malloc (sizeof (struct coff_type));
433             ptr->size = PTR_SIZE;
434             ptr->type = coff_pointer_type;
435             ptr->u.pointer.points_to = res;
436             res = ptr;
437             break;
438           }
439         case DT_FCN:
440           {
441             struct coff_type *ptr = (struct coff_type *) malloc (sizeof (struct coff_type));
442             ptr->size = 0;
443             ptr->type = coff_function_type;
444             ptr->u.function.function_returns = res;
445             ptr->u.function.parameters = empty_scope ();
446             ptr->u.function.lines = do_lines (i, sym->_n._n_nptr[1]);
447             ptr->u.function.code = 0;
448             last_function_type = ptr;
449             res = ptr;
450             break;
451           }
452         }
453     }
454   return res;
455 }
456
457 static struct coff_visible *
458 do_visible (i)
459      int i;
460 {
461   struct internal_syment *sym = &rawsyms[i].u.syment;
462   struct coff_visible *visible = (struct coff_visible *) (malloc (sizeof (struct coff_visible)));
463   enum coff_vis_type t;
464   switch (sym->n_sclass)
465     {
466     case C_MOS:
467     case C_MOU:
468     case C_FIELD:
469       t = coff_vis_member_of_struct;
470       break;
471     case C_MOE:
472       t = coff_vis_member_of_enum;
473       break;
474
475     case C_REGPARM:
476       t = coff_vis_regparam;
477       break;
478
479     case C_REG:
480       t = coff_vis_register;
481       break;
482     case C_STRTAG:
483     case C_UNTAG:
484     case C_ENTAG:
485     case C_TPDEF:
486       t = coff_vis_tag;
487       break;
488     case C_AUTOARG:
489     case C_ARG:
490       t = coff_vis_autoparam;
491       break;
492     case C_AUTO:
493
494
495       t = coff_vis_auto;
496       break;
497     case C_LABEL:
498     case C_STAT:
499       t = coff_vis_int_def;
500       break;
501     case C_EXT:
502       if (sym->n_scnum == N_UNDEF)
503         {
504           if (sym->n_value)
505             t = coff_vis_common;
506           else
507             t = coff_vis_ext_ref;
508         }
509       else
510         t = coff_vis_ext_def;
511       break;
512     default:
513       abort ();
514       break;
515
516     }
517   visible->type = t;
518   return visible;
519 }
520
521 static int
522 do_define (i, b)
523      int i;
524      struct coff_scope *b;
525 {
526   static int symbol_index;
527   struct internal_syment *sym = &rawsyms[i].u.syment;
528
529   /* Define a symbol and attach to block b */
530   struct coff_symbol *s = empty_symbol ();
531
532   s->number = ++symbol_index;
533   s->name = sym->_n._n_nptr[1];
534   s->sfile = cur_sfile;
535   /* Glue onto the ofile list */
536   if (lofile >= 0)
537     {
538       if (ofile->symbol_list_tail)
539         ofile->symbol_list_tail->next_in_ofile_list = s;
540       else
541         ofile->symbol_list_head = s;
542       ofile->symbol_list_tail = s;
543       /* And the block list */
544     }
545   if (b->vars_tail)
546     b->vars_tail->next = s;
547   else
548     b->vars_head = s;
549
550   b->vars_tail = s;
551   b->nvars++;
552   s->type = do_type (i);
553   s->where = do_where (i);
554   s->visible = do_visible (i);
555
556   tindex[i] = s;
557
558   /* We remember the lowest address in each section for each source file */
559
560   if (s->where->where == coff_where_memory
561       && s->type->type == coff_secdef_type)
562     {
563       struct coff_isection *is = cur_sfile->section + s->where->section->number;
564
565       if (!is->init)
566         {
567           is->low = s->where->offset;
568           is->high = s->where->offset + s->type->size;
569           is->init = 1;
570           is->parent = s->where->section;
571         }
572
573     }
574
575   if (s->type->type == coff_function_type)
576     last_function_symbol = s;
577
578   return i + sym->n_numaux + 1;
579 }
580
581
582 static
583 struct coff_ofile *
584 doit ()
585 {
586   int i;
587   int infile = 0;
588   struct coff_ofile *head = (struct coff_ofile *) malloc (sizeof (struct coff_ofile));
589   ofile = head;
590   head->source_head = 0;
591   head->source_tail = 0;
592
593   do_sections_p1 (head);
594   push_scope (1);
595
596   for (i = 0; i < rawcount;)
597     {
598       struct internal_syment *sym = &rawsyms[i].u.syment;
599       switch (sym->n_sclass)
600         {
601         case C_FILE:
602           {
603             /* new source file announced */
604             struct coff_sfile *n = (struct coff_sfile *) malloc (sizeof (struct coff_sfile));
605             n->section = (struct coff_isection *) xcalloc (sizeof (struct coff_isection), abfd->section_count + 1);
606             cur_sfile = n;
607             n->name = sym->_n._n_nptr[1];
608             n->next = 0;
609
610             if (infile)
611               {
612                 pop_scope ();
613               }
614             infile = 1;
615             push_scope (1);
616             file_scope = n->scope = top_scope;
617
618             if (head->source_tail)
619               head->source_tail->next = n;
620             else
621               head->source_head = n;
622             head->source_tail = n;
623             head->nsources++;
624             i += sym->n_numaux + 1;
625           }
626           break;
627         case C_FCN:
628           {
629             char *name = sym->_n._n_nptr[1];
630             if (name[1] == 'b')
631               {
632                 /* Function start */
633                 push_scope (0);
634                 last_function_type->u.function.code = top_scope;
635                 top_scope->sec = ofile->sections + sym->n_scnum;
636                 top_scope->offset = sym->n_value;
637               }
638             else
639               {
640                 top_scope->size = sym->n_value - top_scope->offset + 1;
641                 pop_scope ();
642
643               }
644             i += sym->n_numaux + 1;
645           }
646           break;
647
648         case C_BLOCK:
649           {
650             char *name = sym->_n._n_nptr[1];
651             if (name[1] == 'b')
652               {
653                 /* Block start */
654                 push_scope (1);
655                 top_scope->sec = ofile->sections + sym->n_scnum;
656                 top_scope->offset = sym->n_value;
657
658               }
659             else
660               {
661                 top_scope->size = sym->n_value - top_scope->offset + 1;
662                 pop_scope ();
663               }
664             i += sym->n_numaux + 1;
665           }
666           break;
667         case C_REGPARM:
668         case C_ARG:
669           i = do_define (i, last_function_symbol->type->u.function.parameters);
670           break;
671         case C_MOS:
672         case C_MOU:
673         case C_FIELD:
674           i = do_define (i, last_struct->u.astructdef.elements);
675           break;
676         case C_MOE:
677           i = do_define (i, last_enum->u.aenumdef.elements);
678           break;
679         case C_STRTAG:
680         case C_ENTAG:
681         case C_UNTAG:
682           /* Various definition */
683           i = do_define (i, top_scope);
684           break;
685         case C_EXT:
686         case C_LABEL:
687           i = do_define (i, file_scope);
688           break;
689         case C_STAT:
690         case C_TPDEF:
691         case C_AUTO:
692         case C_REG:
693           i = do_define (i, top_scope);
694           break;
695         default:
696           abort ();
697         case C_EOS:
698           i += sym->n_numaux + 1;
699           break;
700         }
701     }
702   do_sections_p2 (head);
703   return head;
704 }
705
706 struct coff_ofile *
707 coff_grok (inabfd)
708      bfd *inabfd;
709 {
710   long storage;
711   struct coff_ofile *p;
712   abfd = inabfd;
713   storage = bfd_get_symtab_upper_bound (abfd);
714
715   if (storage < 0)
716     bfd_fatal (abfd->filename);
717
718   syms = (asymbol **) xmalloc (storage);
719   symcount = bfd_canonicalize_symtab (abfd, syms);
720   if (symcount < 0)
721     bfd_fatal (abfd->filename);
722   rawsyms = obj_raw_syments (abfd);
723   rawcount = obj_raw_syment_count (abfd);;
724   tindex = (struct coff_symbol **) (xcalloc (sizeof (struct coff_symbol *), rawcount));
725
726   p = doit (abfd);
727   return p;
728 }