Home | History | Annotate | Line # | Download | only in binutils
      1 /* coffgrok.c
      2    Copyright (C) 1994-2025 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 3 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., 51 Franklin Street - Fifth Floor, Boston,
     19    MA 02110-1301, USA.  */
     20 
     21 
     22 /* Written by Steve Chamberlain (sac (at) cygnus.com)
     23 
     24    This module reads a coff file and builds a really simple type tree
     25    which can be read by other programs.  The first application is a
     26    coff->sysroff converter.  It can be tested with coffdump.c.  */
     27 
     28 #include "sysdep.h"
     29 #include "bfd.h"
     30 #include "libiberty.h"
     31 #include "coff/internal.h"
     32 #include "../bfd/libcoff.h"
     33 #include "bucomm.h"
     34 #include "coffgrok.h"
     35 
     36 static int                      lofile = 1;
     37 
     38 static struct coff_scope *      top_scope;
     39 static struct coff_scope *      file_scope;
     40 static struct coff_ofile *      ofile;
     41 static struct coff_symbol *     last_function_symbol;
     42 static struct coff_type *       last_function_type;
     43 static struct coff_type *       last_struct;
     44 static struct coff_type *       last_enum;
     45 static struct coff_sfile *      cur_sfile;
     46 static struct coff_symbol **    tindex;
     47 static asymbol **               syms;
     48 static long                     symcount;
     49 static struct coff_ptr_struct * rawsyms;
     50 static unsigned int             rawcount;
     51 static bfd *                    abfd;
     52 
     53 #define N(x) ((x)->_n._n_nptr[1])
     54 
     55 #define PTR_SIZE	4
     56 #define SHORT_SIZE	2
     57 #define INT_SIZE	4
     58 #define LONG_SIZE	4
     59 #define FLOAT_SIZE	4
     60 #define DOUBLE_SIZE	8
     61 
     62 #define INDEXOF(p)  ((struct coff_ptr_struct *)(p)-(rawsyms))
     63 
     64 
     65 static struct coff_scope *
     66 empty_scope (void)
     67 {
     68   return (struct coff_scope *) (xcalloc (1, sizeof (struct coff_scope)));
     69 }
     70 
     71 static struct coff_symbol *
     72 empty_symbol (void)
     73 {
     74   return (struct coff_symbol *) (xcalloc (1, sizeof (struct coff_symbol)));
     75 }
     76 
     77 static void
     78 push_scope (int slink)
     79 {
     80   struct coff_scope *n = empty_scope ();
     81 
     82   if (slink)
     83     {
     84       if (top_scope)
     85 	{
     86 	  if (top_scope->list_tail)
     87 	    {
     88 	      top_scope->list_tail->next = n;
     89 	    }
     90 	  else
     91 	    {
     92 	      top_scope->list_head = n;
     93 	    }
     94 	  top_scope->list_tail = n;
     95 	}
     96     }
     97   n->parent = top_scope;
     98 
     99   top_scope = n;
    100 }
    101 
    102 static void
    103 pop_scope (void)
    104 {
    105   /* PR 17512: file: 809933ac.  */
    106   if (top_scope == NULL)
    107     fatal (_("Out of context scope change encountered"));
    108   top_scope = top_scope->parent;
    109 }
    110 
    111 static void
    112 do_sections_p1 (struct coff_ofile *head)
    113 {
    114   asection *section;
    115   int idx;
    116   struct coff_section *all = (struct coff_section *) (xcalloc (abfd->section_count + 1,
    117 					     sizeof (struct coff_section)));
    118   head->nsections = abfd->section_count + 1;
    119   head->sections = all;
    120 
    121   for (idx = 0, section = abfd->sections; section; section = section->next, idx++)
    122     {
    123       long relsize;
    124       unsigned int i = section->target_index;
    125       arelent **relpp;
    126       long relcount;
    127 
    128       /* PR 17512: file: 2d6effca.  */
    129       if (i > abfd->section_count)
    130 	fatal (_("Invalid section target index: %u"), i);
    131 
    132       relsize = bfd_get_reloc_upper_bound (abfd, section);
    133       if (relsize < 0)
    134 	bfd_fatal (bfd_get_filename (abfd));
    135       if (relsize == 0)
    136 	continue;
    137       relpp = (arelent **) xmalloc (relsize);
    138       relcount = bfd_canonicalize_reloc (abfd, section, relpp, syms);
    139       if (relcount < 0)
    140 	bfd_fatal (bfd_get_filename (abfd));
    141 
    142       head->sections[i].name = (char *) (section->name);
    143       head->sections[i].code = section->flags & SEC_CODE;
    144       head->sections[i].data = section->flags & SEC_DATA;
    145       if (strcmp (section->name, ".bss") == 0)
    146 	head->sections[i].data = 1;
    147       head->sections[i].address = section->lma;
    148       head->sections[i].size = bfd_section_size (section);
    149       head->sections[i].number = idx;
    150       head->sections[i].nrelocs = section->reloc_count;
    151       head->sections[i].relocs =
    152 	(struct coff_reloc *) (xcalloc (section->reloc_count,
    153 					sizeof (struct coff_reloc)));
    154       head->sections[i].bfd_section = section;
    155     }
    156   head->sections[0].name = "ABSOLUTE";
    157   head->sections[0].code = 0;
    158   head->sections[0].data = 0;
    159   head->sections[0].address = 0;
    160   head->sections[0].size = 0;
    161   head->sections[0].number = 0;
    162 }
    163 
    164 static void
    165 do_sections_p2 (struct coff_ofile *head)
    166 {
    167   asection *section;
    168 
    169   for (section = abfd->sections; section; section = section->next)
    170     {
    171       unsigned int j;
    172 
    173       /* PR 17512: file: 7c1a36e8.
    174 	 A corrupt COFF binary might have a reloc count but no relocs.
    175 	 Handle this here.  */
    176       if (section->relocation == NULL)
    177 	continue;
    178 
    179       for (j = 0; j < section->reloc_count; j++)
    180 	{
    181 	  unsigned int idx;
    182 	  int i = section->target_index;
    183 	  struct coff_reloc *r;
    184 	  arelent *sr = section->relocation + j;
    185 
    186 	  if (i > head->nsections)
    187 	    fatal (_("Invalid section target index: %d"), i);
    188 	  /* PR 17512: file: db850ff4.  */
    189 	  if (j >= head->sections[i].nrelocs)
    190 	    fatal (_("Target section has insufficient relocs"));
    191 	  r = head->sections[i].relocs + j;
    192 	  r->offset = sr->address;
    193 	  r->addend = sr->addend;
    194 	  idx = ((coff_symbol_type *) (sr->sym_ptr_ptr[0]))->native - rawsyms;
    195 	  if (idx >= rawcount)
    196 	    {
    197 	      if (rawcount == 0)
    198 		fatal (_("Symbol index %u encountered when there are no symbols"), idx);
    199 	      non_fatal (_("Invalid symbol index %u encountered"), idx);
    200 	      idx = 0;
    201 	    }
    202 	  r->symbol = tindex[idx];
    203 	}
    204     }
    205 }
    206 
    207 static struct coff_where *
    208 do_where (unsigned int i)
    209 {
    210   struct internal_syment *sym;
    211   struct coff_where *where =
    212     (struct coff_where *) (xmalloc (sizeof (struct coff_where)));
    213 
    214   if (i >= rawcount)
    215     fatal ("Invalid symbol index: %d\n", i);
    216 
    217   sym = &rawsyms[i].u.syment;
    218   where->offset = sym->n_value;
    219 
    220   if (sym->n_scnum == -1)
    221     sym->n_scnum = 0;
    222 
    223   switch (sym->n_sclass)
    224     {
    225     case C_FIELD:
    226       where->where = coff_where_member_of_struct;
    227       where->offset = sym->n_value / 8;
    228       where->bitoffset = sym->n_value % 8;
    229       where->bitsize = rawsyms[i + 1].u.auxent.x_sym.x_misc.x_lnsz.x_size;
    230       break;
    231     case C_MOE:
    232       where->where = coff_where_member_of_enum;
    233       break;
    234     case C_MOS:
    235     case C_MOU:
    236       where->where = coff_where_member_of_struct;
    237       break;
    238     case C_AUTO:
    239     case C_ARG:
    240       where->where = coff_where_stack;
    241       break;
    242     case C_EXT:
    243     case C_STAT:
    244     case C_EXTDEF:
    245     case C_LABEL:
    246       where->where = coff_where_memory;
    247       /* PR 17512: file: 07a37c40.  */
    248       /* PR 17512: file: 0c2eb101.  */
    249       if (sym->n_scnum >= ofile->nsections || sym->n_scnum < 0)
    250 	{
    251 	  non_fatal (_("Invalid section number (%d) encountered"),
    252 		     sym->n_scnum);
    253 	  where->section = ofile->sections;
    254 	}
    255       else
    256 	where->section = &ofile->sections[sym->n_scnum];
    257       break;
    258     case C_REG:
    259     case C_REGPARM:
    260       where->where = coff_where_register;
    261       break;
    262     case C_ENTAG:
    263       where->where = coff_where_entag;
    264       break;
    265     case C_STRTAG:
    266     case C_UNTAG:
    267       where->where = coff_where_strtag;
    268       break;
    269     case C_TPDEF:
    270       where->where = coff_where_typedef;
    271       break;
    272     default:
    273       fatal (_("Unrecognized symbol class: %d"), sym->n_sclass);
    274       break;
    275     }
    276   return where;
    277 }
    278 
    279 static struct coff_line *
    280 do_lines (int i, char *name ATTRIBUTE_UNUSED)
    281 {
    282   struct coff_line *res = (struct coff_line *) xcalloc (1, sizeof (struct coff_line));
    283   asection *s;
    284   unsigned int l;
    285 
    286   /* Find out if this function has any line numbers in the table.  */
    287   for (s = abfd->sections; s; s = s->next)
    288     {
    289       /* PR 17512: file: 07a37c40.
    290 	 A corrupt COFF binary can have a linenumber count in the header
    291 	 but no line number table.  This should be reported elsewhere, but
    292 	 do not rely upon this.  */
    293       if (s->lineno == NULL)
    294 	continue;
    295 
    296       for (l = 0; l < s->lineno_count; l++)
    297 	{
    298 	  if (s->lineno[l].line_number == 0)
    299 	    {
    300 	      if (rawsyms + i == ((coff_symbol_type *) (&(s->lineno[l].u.sym[0])))->native)
    301 		{
    302 		  /* These lines are for this function - so count them and stick them on.  */
    303 		  int c = 0;
    304 		  /* Find the linenumber of the top of the function, since coff linenumbers
    305 		     are relative to the start of the function.  */
    306 		  int start_line = rawsyms[i + 3].u.auxent.x_sym.x_misc.x_lnsz.x_lnno;
    307 
    308 		  l++;
    309 		  for (c = 0;
    310 		       /* PR 17512: file: c2825452.  */
    311 		       l + c + 1 < s->lineno_count
    312 			 && s->lineno[l + c + 1].line_number;
    313 		       c++)
    314 		    ;
    315 
    316 		  /* Add two extra records, one for the prologue and one for the epilogue.  */
    317 		  c += 1;
    318 		  res->nlines = c;
    319 		  res->lines = (int *) (xcalloc (c, sizeof (int)));
    320 		  res->addresses = (int *) (xcalloc (c, sizeof (int)));
    321 		  res->lines[0] = start_line;
    322 		  res->addresses[0] = rawsyms[i].u.syment.n_value - s->vma;
    323 		  for (c = 0;
    324 		       /* PR 17512: file: c2825452.  */
    325 		       l + c + 1 < s->lineno_count
    326 			 && s->lineno[l + c + 1].line_number;
    327 		       c++)
    328 		    {
    329 		      res->lines[c + 1] = s->lineno[l + c].line_number + start_line - 1;
    330 		      res->addresses[c + 1] = s->lineno[l + c].u.offset;
    331 		    }
    332 		  return res;
    333 		}
    334 	    }
    335 	}
    336     }
    337   return res;
    338 }
    339 
    340 static struct coff_type *
    341 do_type (unsigned int i)
    342 {
    343   struct internal_syment *sym;
    344   combined_entry_type *aux;
    345   struct coff_type *res = (struct coff_type *) xmalloc (sizeof (struct coff_type));
    346   int type;
    347   int which_dt = 0;
    348   int dimind = 0;
    349 
    350   if (i >= rawcount)
    351     fatal (_("Type entry %u does not have enough symbolic information"), i);
    352 
    353   if (!rawsyms[i].is_sym)
    354     fatal (_("Type entry %u does not refer to a symbol"), i);
    355   sym = &rawsyms[i].u.syment;
    356 
    357   if (sym->n_numaux == 0 || i >= rawcount -1 || rawsyms[i + 1].is_sym)
    358     aux = NULL;
    359   else
    360     aux = &rawsyms[i + 1];
    361 
    362   type = sym->n_type;
    363 
    364   res->type = coff_basic_type;
    365   res->u.basic = type & 0xf;
    366 
    367   switch (type & 0xf)
    368     {
    369     case T_NULL:
    370     case T_VOID:
    371       if (sym->n_numaux && sym->n_sclass == C_STAT)
    372 	{
    373 	  /* This is probably a section definition.  */
    374 	  res->type = coff_secdef_type;
    375 	  if (aux == NULL)
    376 	    fatal (_("Section definition needs a section length"));
    377 	  res->size = aux->u.auxent.x_scn.x_scnlen;
    378 
    379 	  /* PR 17512: file: 081c955d.
    380 	     Fill in the asecdef structure as well.  */
    381 	  res->u.asecdef.address = 0;
    382 	  res->u.asecdef.size = 0;
    383 	}
    384       else
    385 	{
    386 	  if (type == 0)
    387 	    {
    388 	      /* Don't know what this is, let's make it a simple int.  */
    389 	      res->size = INT_SIZE;
    390 	      res->u.basic = T_UINT;
    391 	    }
    392 	  else
    393 	    {
    394 	      /* Else it could be a function or pointer to void.  */
    395 	      res->size = 0;
    396 	    }
    397 	}
    398       break;
    399 
    400     case T_UCHAR:
    401     case T_CHAR:
    402       res->size = 1;
    403       break;
    404     case T_USHORT:
    405     case T_SHORT:
    406       res->size = SHORT_SIZE;
    407       break;
    408     case T_UINT:
    409     case T_INT:
    410       res->size = INT_SIZE;
    411       break;
    412     case T_ULONG:
    413     case T_LONG:
    414       res->size = LONG_SIZE;
    415       break;
    416     case T_FLOAT:
    417       res->size = FLOAT_SIZE;
    418       break;
    419     case T_DOUBLE:
    420       res->size = DOUBLE_SIZE;
    421       break;
    422     case T_STRUCT:
    423     case T_UNION:
    424       if (sym->n_numaux)
    425 	{
    426 	  if (aux == NULL)
    427 	    fatal (_("Aggregate definition needs auxiliary information"));
    428 
    429 	  if (aux->fix_tag)
    430 	    {
    431 	      unsigned int idx = INDEXOF (aux->u.auxent.x_sym.x_tagndx.p);
    432 
    433 	      /* Referring to a struct defined elsewhere.  */
    434 	      res->type = coff_structref_type;
    435 	      res->u.astructref.ref = tindex[idx];
    436 	      res->size = res->u.astructref.ref ?
    437 		res->u.astructref.ref->type->size : 0;
    438 	    }
    439 	  else
    440 	    {
    441 	      /* A definition of a struct.  */
    442 	      last_struct = res;
    443 	      res->type = coff_structdef_type;
    444 	      res->u.astructdef.elements = empty_scope ();
    445 	      res->u.astructdef.idx = 0;
    446 	      res->u.astructdef.isstruct = (type & 0xf) == T_STRUCT;
    447 	      res->size = aux->u.auxent.x_sym.x_misc.x_lnsz.x_size;
    448 	    }
    449 	}
    450       else
    451 	{
    452 	  /* No auxents - it's anonymous.  */
    453 	  res->type = coff_structref_type;
    454 	  res->u.astructref.ref = 0;
    455 	  res->size = 0;
    456 	}
    457       break;
    458     case T_ENUM:
    459       if (aux == NULL)
    460 	fatal (_("Enum definition needs auxiliary information"));
    461       if (aux->fix_tag)
    462 	{
    463 	  unsigned int idx = INDEXOF (aux->u.auxent.x_sym.x_tagndx.p);
    464 
    465 	  /* Referring to a enum defined elsewhere.  */
    466 	  res->type = coff_enumref_type;
    467 	  res->u.aenumref.ref = tindex[idx];
    468 	  /* PR 17512: file: b85b67e8.  */
    469 	  if (res->u.aenumref.ref)
    470 	    res->size = res->u.aenumref.ref->type->size;
    471 	  else
    472 	    res->size = 0;
    473 	}
    474       else
    475 	{
    476 	  /* A definition of an enum.  */
    477 	  last_enum = res;
    478 	  res->type = coff_enumdef_type;
    479 	  res->u.aenumdef.elements = empty_scope ();
    480 	  res->size = aux->u.auxent.x_sym.x_misc.x_lnsz.x_size;
    481 	}
    482       break;
    483     case T_MOE:
    484       break;
    485     }
    486 
    487   for (which_dt = 5; which_dt >= 0; which_dt--)
    488     {
    489       switch ((type >> ((which_dt * 2) + 4)) & 0x3)
    490 	{
    491 	case 0:
    492 	  break;
    493 	case DT_ARY:
    494 	  {
    495 	    struct coff_type *ptr = ((struct coff_type *)
    496 				     xmalloc (sizeof (struct coff_type)));
    497 	    int els;
    498 
    499 	    if (aux == NULL)
    500 	      fatal (_("Array definition needs auxiliary information"));
    501 	    els = (dimind < DIMNUM
    502 		   ? aux->u.auxent.x_sym.x_fcnary.x_ary.x_dimen[dimind]
    503 		   : 0);
    504 
    505 	    ++dimind;
    506 	    ptr->type = coff_array_type;
    507 	    /* PR 17512: file: ae1971e2.
    508 	       Check for integer overflow.  */
    509 	    {
    510 	      long long a, z;
    511 	      a = els;
    512 	      z = res->size;
    513 	      a *= z;
    514 	      ptr->size = (int) a;
    515 	      if (ptr->size != a)
    516 		non_fatal (_("Out of range sum for els (%#x) * size (%#x)"), els, res->size);
    517 	    }
    518 	    ptr->u.array.dim = els;
    519 	    ptr->u.array.array_of = res;
    520 	    res = ptr;
    521 	    break;
    522 	  }
    523 	case DT_PTR:
    524 	  {
    525 	    struct coff_type *ptr =
    526 	      (struct coff_type *) xmalloc (sizeof (struct coff_type));
    527 
    528 	    ptr->size = PTR_SIZE;
    529 	    ptr->type = coff_pointer_type;
    530 	    ptr->u.pointer.points_to = res;
    531 	    res = ptr;
    532 	    break;
    533 	  }
    534 	case DT_FCN:
    535 	  {
    536 	    struct coff_type *ptr
    537 	      = (struct coff_type *) xmalloc (sizeof (struct coff_type));
    538 
    539 	    ptr->size = 0;
    540 	    ptr->type = coff_function_type;
    541 	    ptr->u.function.function_returns = res;
    542 	    ptr->u.function.parameters = empty_scope ();
    543 	    ptr->u.function.lines = do_lines (i, N(sym));
    544 	    ptr->u.function.code = 0;
    545 	    last_function_type = ptr;
    546 	    res = ptr;
    547 	    break;
    548 	  }
    549 	}
    550     }
    551   return res;
    552 }
    553 
    554 static struct coff_visible *
    555 do_visible (int i)
    556 {
    557   struct internal_syment *sym = &rawsyms[i].u.syment;
    558   struct coff_visible *visible =
    559     (struct coff_visible *) (xmalloc (sizeof (struct coff_visible)));
    560   enum coff_vis_type t;
    561 
    562   switch (sym->n_sclass)
    563     {
    564     case C_MOS:
    565     case C_MOU:
    566     case C_FIELD:
    567       t = coff_vis_member_of_struct;
    568       break;
    569     case C_MOE:
    570       t = coff_vis_member_of_enum;
    571       break;
    572     case C_REGPARM:
    573       t = coff_vis_regparam;
    574       break;
    575     case C_REG:
    576       t = coff_vis_register;
    577       break;
    578     case C_STRTAG:
    579     case C_UNTAG:
    580     case C_ENTAG:
    581     case C_TPDEF:
    582       t = coff_vis_tag;
    583       break;
    584     case C_AUTOARG:
    585     case C_ARG:
    586       t = coff_vis_autoparam;
    587       break;
    588     case C_AUTO:
    589       t = coff_vis_auto;
    590       break;
    591     case C_LABEL:
    592     case C_STAT:
    593       t = coff_vis_int_def;
    594       break;
    595     case C_EXT:
    596       if (sym->n_scnum == N_UNDEF)
    597 	{
    598 	  if (sym->n_value)
    599 	    t = coff_vis_common;
    600 	  else
    601 	    t = coff_vis_ext_ref;
    602 	}
    603       else
    604 	t = coff_vis_ext_def;
    605       break;
    606     default:
    607       fatal (_("Unrecognised symbol class: %d"), sym->n_sclass);
    608       break;
    609     }
    610   visible->type = t;
    611   return visible;
    612 }
    613 
    614 /* Define a symbol and attach to block B.  */
    615 
    616 static int
    617 do_define (unsigned int i, struct coff_scope *b)
    618 {
    619   static int symbol_index;
    620   struct internal_syment *sym;
    621   struct coff_symbol *s = empty_symbol ();
    622 
    623   if (b == NULL)
    624     fatal (_("ICE: do_define called without a block"));
    625   if (i >= rawcount)
    626     fatal (_("Out of range symbol index: %u"), i);
    627 
    628   sym = &rawsyms[i].u.syment;
    629   s->number = ++symbol_index;
    630   s->name = N(sym);
    631   s->sfile = cur_sfile;
    632   /* Glue onto the ofile list.  */
    633   if (lofile >= 0)
    634     {
    635       if (ofile->symbol_list_tail)
    636 	ofile->symbol_list_tail->next_in_ofile_list = s;
    637       else
    638 	ofile->symbol_list_head = s;
    639       ofile->symbol_list_tail = s;
    640       /* And the block list.  */
    641     }
    642   if (b->vars_tail)
    643     b->vars_tail->next = s;
    644   else
    645     b->vars_head = s;
    646 
    647   b->vars_tail = s;
    648   b->nvars++;
    649   s->type = do_type (i);
    650   s->where = do_where (i);
    651   s->visible = do_visible (i);
    652 
    653   tindex[i] = s;
    654 
    655   /* We remember the lowest address in each section for each source file.  */
    656   if (s->where->where == coff_where_memory
    657       && s->type->type == coff_secdef_type)
    658     {
    659       struct coff_isection *is;
    660 
    661       /* PR 17512: file: 4676c97f.  */
    662       if (cur_sfile == NULL)
    663 	non_fatal (_("Section referenced before any file is defined"));
    664       else
    665 	{
    666 	  is = cur_sfile->section + s->where->section->number;
    667 
    668 	  if (!is->init)
    669 	    {
    670 	      is->low = s->where->offset;
    671 	      /* PR 17512: file: 37e7a80d.
    672 		 Check for integer overflow computing low + size.  */
    673 	      {
    674 		long long a, z;
    675 
    676 		a = s->where->offset;
    677 		z = s->type->size;
    678 		a += z;
    679 		is->high = (int) a;
    680 		if (a != is->high)
    681 		  non_fatal (_("Out of range sum for offset (%#x) + size (%#x)"),
    682 			     is->low, s->type->size);
    683 	      }
    684 	      /* PR 17512: file: 37e7a80d.  */
    685 	      if (is->high < s->where->offset)
    686 		fatal (_("Out of range type size: %u"), s->type->size);
    687 	      is->init = 1;
    688 	      is->parent = s->where->section;
    689 	    }
    690 	}
    691     }
    692 
    693   if (s->type->type == coff_function_type)
    694     last_function_symbol = s;
    695 
    696   return i + sym->n_numaux + 1;
    697 }
    698 
    699 static struct coff_ofile *
    700 doit (void)
    701 {
    702   unsigned int i;
    703   bool infile = false;
    704   struct coff_ofile *head =
    705     (struct coff_ofile *) xmalloc (sizeof (struct coff_ofile));
    706 
    707   ofile = head;
    708   head->source_head = 0;
    709   head->source_tail = 0;
    710   head->nsources = 0;
    711   head->symbol_list_tail = 0;
    712   head->symbol_list_head = 0;
    713   do_sections_p1 (head);
    714   push_scope (1);
    715 
    716   for (i = 0; i < rawcount;)
    717     {
    718       struct internal_syment *sym = &rawsyms[i].u.syment;
    719 
    720       switch (sym->n_sclass)
    721 	{
    722 	case C_FILE:
    723 	  {
    724 	    /* New source file announced.  */
    725 	    struct coff_sfile *n =
    726 	      (struct coff_sfile *) xmalloc (sizeof (struct coff_sfile));
    727 
    728 	    n->section = (struct coff_isection *) xcalloc (abfd->section_count + 1, sizeof (struct coff_isection));
    729 	    cur_sfile = n;
    730 	    n->name = N(sym);
    731 	    n->next = 0;
    732 
    733 	    if (infile)
    734 	      pop_scope ();
    735 	    else
    736 	      infile = true;
    737 
    738 	    push_scope (1);
    739 	    file_scope = n->scope = top_scope;
    740 
    741 	    if (head->source_tail)
    742 	      head->source_tail->next = n;
    743 	    else
    744 	      head->source_head = n;
    745 	    head->source_tail = n;
    746 	    head->nsources++;
    747 	    i += sym->n_numaux + 1;
    748 	  }
    749 	  break;
    750 	case C_FCN:
    751 	  {
    752 	    char *name = N(sym);
    753 
    754 	    if (name[1] == 'b')
    755 	      {
    756 		/* Function start.  */
    757 		push_scope (0);
    758 		/* PR 17512: file: 0ef7fbaf.  */
    759 		if (last_function_type)
    760 		  last_function_type->u.function.code = top_scope;
    761 		/* PR 17512: file: 22908266.  */
    762 		if (sym->n_scnum < ofile->nsections && sym->n_scnum >= 0)
    763 		  top_scope->sec = ofile->sections + sym->n_scnum;
    764 		else
    765 		  top_scope->sec = NULL;
    766 		top_scope->offset = sym->n_value;
    767 	      }
    768 	    else
    769 	      {
    770 		/* PR 17512: file: e92e42e1.  */
    771 		if (top_scope == NULL)
    772 		  fatal (_("Function start encountered without a top level scope."));
    773 		top_scope->size = sym->n_value - top_scope->offset + 1;
    774 		pop_scope ();
    775 	      }
    776 	    i += sym->n_numaux + 1;
    777 	  }
    778 	  break;
    779 
    780 	case C_BLOCK:
    781 	  {
    782 	    char *name = N(sym);
    783 
    784 	    if (name[1] == 'b')
    785 	      {
    786 		/* Block start.  */
    787 		push_scope (1);
    788 		/* PR 17512: file: af7e8e83.  */
    789 		if (sym->n_scnum < ofile->nsections && sym->n_scnum >= 0)
    790 		  top_scope->sec = ofile->sections + sym->n_scnum;
    791 		else
    792 		  top_scope->sec = NULL;
    793 		top_scope->offset = sym->n_value;
    794 	      }
    795 	    else
    796 	      {
    797 		if (top_scope == NULL)
    798 		  fatal (_("Block start encountered without a scope for it."));
    799 		top_scope->size = sym->n_value - top_scope->offset + 1;
    800 		pop_scope ();
    801 	      }
    802 	    i += sym->n_numaux + 1;
    803 	  }
    804 	  break;
    805 	case C_REGPARM:
    806 	case C_ARG:
    807 	  if (last_function_symbol == NULL)
    808 	    fatal (_("Function arguments encountered without a function definition"));
    809 	  i = do_define (i, last_function_symbol->type->u.function.parameters);
    810 	  break;
    811 	case C_MOS:
    812 	case C_MOU:
    813 	case C_FIELD:
    814 	  /* PR 17512: file: 43ab21f4.  */
    815 	  if (last_struct == NULL)
    816 	    fatal (_("Structure element encountered without a structure definition"));
    817 	  i = do_define (i, last_struct->u.astructdef.elements);
    818 	  break;
    819 	case C_MOE:
    820 	  if (last_enum == NULL)
    821 	    fatal (_("Enum element encountered without an enum definition"));
    822 	  i = do_define (i, last_enum->u.aenumdef.elements);
    823 	  break;
    824 	case C_STRTAG:
    825 	case C_ENTAG:
    826 	case C_UNTAG:
    827 	  /* Various definition.  */
    828 	  if (top_scope == NULL)
    829 	    fatal (_("Aggregate definition encountered without a scope"));
    830 	  i = do_define (i, top_scope);
    831 	  break;
    832 	case C_EXT:
    833 	case C_LABEL:
    834 	  if (file_scope == NULL)
    835 	    fatal (_("Label definition encountered without a file scope"));
    836 	  i = do_define (i, file_scope);
    837 	  break;
    838 	case C_STAT:
    839 	case C_TPDEF:
    840 	case C_AUTO:
    841 	case C_REG:
    842 	  if (top_scope == NULL)
    843 	    fatal (_("Variable definition encountered without a scope"));
    844 	  i = do_define (i, top_scope);
    845 	  break;
    846 	case C_EOS:
    847 	  i += sym->n_numaux + 1;
    848 	  break;
    849 	default:
    850 	  fatal (_("Unrecognised symbol class: %d"), sym->n_sclass);
    851 	}
    852     }
    853   do_sections_p2 (head);
    854   return head;
    855 }
    856 
    857 struct coff_ofile *
    858 coff_grok (bfd *inabfd)
    859 {
    860   long storage;
    861   struct coff_ofile *p;
    862   abfd = inabfd;
    863 
    864   if (! bfd_family_coff (abfd))
    865     {
    866       non_fatal (_("%s: is not a COFF format file"), bfd_get_filename (abfd));
    867       return NULL;
    868     }
    869 
    870   storage = bfd_get_symtab_upper_bound (abfd);
    871 
    872   if (storage < 0)
    873     bfd_fatal (bfd_get_filename (abfd));
    874 
    875   syms = (asymbol **) xmalloc (storage);
    876   symcount = bfd_canonicalize_symtab (abfd, syms);
    877   if (symcount < 0)
    878     bfd_fatal (bfd_get_filename (abfd));
    879   rawsyms = obj_raw_syments (abfd);
    880   rawcount = obj_raw_syment_count (abfd);
    881   tindex = (struct coff_symbol **) (xcalloc (rawcount,
    882 					     sizeof (struct coff_symbol *)));
    883 
    884   p = doit ();
    885   return p;
    886 }
    887