OSDN Git Service

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