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