X-Git-Url: https://git.libre-soc.org/?a=blobdiff_plain;f=binutils%2Fcoffgrok.c;h=97574e7d0ca475ec856ef0ea4c6b23fe180dd098;hb=ad34eb2f7c3120f83d22cf2d5371673fc95040b4;hp=b202a0ecf15e284700cd274f8d783acfcc099ab6;hpb=958998b7aacbe2a7da7928add10a20e0b0e7424f;p=binutils-gdb.git diff --git a/binutils/coffgrok.c b/binutils/coffgrok.c index b202a0ecf15..97574e7d0ca 100644 --- a/binutils/coffgrok.c +++ b/binutils/coffgrok.c @@ -1,50 +1,50 @@ - - /* coffgrok.c + Copyright 1994, 1995, 1997, 1998, 2000, 2001, 2002, 2003, 2004, 2005, + 2007, 2009 Free Software Foundation, Inc. - Copyright (C) 1994 Free Software Foundation, Inc. + This file is part of GNU Binutils. -This file is part of GNU Binutils. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, + MA 02110-1301, USA. */ -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ /* Written by Steve Chamberlain (sac@cygnus.com) This module reads a coff file and builds a really simple type tree which can be read by other programs. The first application is a - coff->sysroff converter. It can be tested with coffdump.c. + coff->sysroff converter. It can be tested with coffdump.c. */ -*/ - -#include -#include -#include "bucomm.h" +#include "sysdep.h" +#include "bfd.h" +#include "libiberty.h" #include "coff/internal.h" #include "../bfd/libcoff.h" +#include "bucomm.h" #include "coffgrok.h" -int lofile = 1; + +static int lofile = 1; static struct coff_scope *top_scope; static struct coff_scope *file_scope; static struct coff_ofile *ofile; -struct coff_symbol *last_function_symbol; -struct coff_type *last_function_type; -struct coff_type *last_struct; -struct coff_type *last_enum; -struct coff_sfile *cur_sfile; +static struct coff_symbol *last_function_symbol; +static struct coff_type *last_function_type; +static struct coff_type *last_struct; +static struct coff_type *last_enum; +static struct coff_sfile *cur_sfile; static struct coff_symbol **tindex; @@ -57,18 +57,31 @@ static long symcount; static struct coff_ptr_struct *rawsyms; static int rawcount; static bfd *abfd; -extern char *xcalloc (); -#define PTR_SIZE 4 -#define SHORT_SIZE 2 -#define INT_SIZE 4 -#define LONG_SIZE 4 -#define FLOAT_SIZE 4 -#define DOUBLE_SIZE 8 + +#define PTR_SIZE 4 +#define SHORT_SIZE 2 +#define INT_SIZE 4 +#define LONG_SIZE 4 +#define FLOAT_SIZE 4 +#define DOUBLE_SIZE 8 #define INDEXOF(p) ((struct coff_ptr_struct *)(p)-(rawsyms)) +static struct coff_scope *empty_scope (void); +static struct coff_symbol *empty_symbol (void); +static void push_scope (int); +static void pop_scope (void); +static void do_sections_p1 (struct coff_ofile *); +static void do_sections_p2 (struct coff_ofile *); +static struct coff_where *do_where (int); +static struct coff_line *do_lines (int, char *); +static struct coff_type *do_type (int); +static struct coff_visible *do_visible (int); +static int do_define (int, struct coff_scope *); +static struct coff_ofile *doit (void); + static struct coff_scope * -empty_scope () +empty_scope (void) { struct coff_scope *l; l = (struct coff_scope *) (xcalloc (sizeof (struct coff_scope), 1)); @@ -76,18 +89,18 @@ empty_scope () } static struct coff_symbol * -empty_symbol () +empty_symbol (void) { return (struct coff_symbol *) (xcalloc (sizeof (struct coff_symbol), 1)); } /*int l;*/ static void -push_scope (link) - int link; +push_scope (int slink) { struct coff_scope *n = empty_scope (); - if (link) + + if (slink) { if (top_scope) { @@ -108,14 +121,13 @@ push_scope (link) } static void -pop_scope () +pop_scope (void) { top_scope = top_scope->parent; } static void -do_sections_p1 (head) - struct coff_ofile *head; +do_sections_p1 (struct coff_ofile *head) { asection *section; int idx; @@ -134,6 +146,8 @@ do_sections_p1 (head) relsize = bfd_get_reloc_upper_bound (abfd, section); if (relsize < 0) bfd_fatal (bfd_get_filename (abfd)); + if (relsize == 0) + continue; relpp = (arelent **) xmalloc (relsize); relcount = bfd_canonicalize_reloc (abfd, section, relpp, syms); if (relcount < 0) @@ -144,8 +158,8 @@ do_sections_p1 (head) head->sections[i].data = section->flags & SEC_DATA; if (strcmp (section->name, ".bss") == 0) head->sections[i].data = 1; - head->sections[i].address = section->vma; - head->sections[i].size = section->_raw_size; + head->sections[i].address = section->lma; + head->sections[i].size = bfd_get_section_size (section); head->sections[i].number = idx; head->sections[i].nrelocs = section->reloc_count; head->sections[i].relocs = @@ -162,13 +176,13 @@ do_sections_p1 (head) } static void -do_sections_p2 (head) - struct coff_ofile *head; +do_sections_p2 (struct coff_ofile *head) { asection *section; for (section = abfd->sections; section; section = section->next) { - int j; + unsigned int j; + for (j = 0; j < section->reloc_count; j++) { int idx; @@ -184,12 +198,11 @@ do_sections_p2 (head) } static struct coff_where * -do_where (i) - int i; +do_where (int i) { struct internal_syment *sym = &rawsyms[i].u.syment; - struct coff_where *where - = (struct coff_where *) (malloc (sizeof (struct coff_where))); + struct coff_where *where = + (struct coff_where *) (xmalloc (sizeof (struct coff_where))); where->offset = sym->n_value; if (sym->n_scnum == -1) @@ -244,13 +257,12 @@ do_where (i) static struct coff_line * -do_lines (i, name) - int i; - char *name; +do_lines (int i, char *name ATTRIBUTE_UNUSED) { struct coff_line *res = (struct coff_line *) xcalloc (sizeof (struct coff_line), 1); asection *s; - int l; + unsigned int l; + /* Find out if this function has any line numbers in the table */ for (s = abfd->sections; s; s = s->next) { @@ -263,7 +275,7 @@ do_lines (i, name) /* These lines are for this function - so count them and stick them on */ int c = 0; /* Find the linenumber of the top of the function, since coff linenumbers - are relative to the start of the function. */ + are relative to the start of the function. */ int start_line = rawsyms[i + 3].u.auxent.x_sym.x_misc.x_lnsz.x_lnno; l++; @@ -292,14 +304,15 @@ do_lines (i, name) static struct coff_type * -do_type (i) - int i; +do_type (int i) { struct internal_syment *sym = &rawsyms[i].u.syment; union internal_auxent *aux = &rawsyms[i + 1].u.auxent; - struct coff_type *res = (struct coff_type *) malloc (sizeof (struct coff_type)); + struct coff_type *res = + (struct coff_type *) xmalloc (sizeof (struct coff_type)); int type = sym->n_type; int which_dt = 0; + int dimind = 0; res->type = coff_basic_type; res->u.basic = type & 0xf; @@ -360,7 +373,7 @@ do_type (i) { if (aux->x_sym.x_tagndx.p) { - /* Refering to a struct defined elsewhere */ + /* Referring to a struct defined elsewhere */ res->type = coff_structref_type; res->u.astructref.ref = tindex[INDEXOF (aux->x_sym.x_tagndx.p)]; res->size = res->u.astructref.ref ? @@ -379,7 +392,7 @@ do_type (i) } else { - /* No auxents - it's anonynmous */ + /* No auxents - it's anonymous */ res->type = coff_structref_type; res->u.astructref.ref = 0; res->size = 0; @@ -388,7 +401,7 @@ do_type (i) case T_ENUM: if (aux->x_sym.x_tagndx.p) { - /* Refering to a enum defined elsewhere */ + /* Referring to a enum defined elsewhere */ res->type = coff_enumref_type; res->u.aenumref.ref = tindex[INDEXOF (aux->x_sym.x_tagndx.p)]; res->size = res->u.aenumref.ref->type->size; @@ -414,24 +427,23 @@ do_type (i) break; case DT_ARY: { - - /* Look in the auxent for the dimensions and build lots of dims */ - int els = aux->x_sym.x_fcnary.x_ary.x_dimen[which_dt]; - if (els) - { - struct coff_type *ptr = (struct coff_type *) malloc (sizeof (struct coff_type)); - ptr->type = coff_array_type; - ptr->size = els * res->size; - ptr->u.array.dim = els; - ptr->u.array.array_of = res; - res = ptr; - } + struct coff_type *ptr = ((struct coff_type *) + xmalloc (sizeof (struct coff_type))); + int els = (dimind < DIMNUM + ? aux->x_sym.x_fcnary.x_ary.x_dimen[dimind] + : 0); + ++dimind; + ptr->type = coff_array_type; + ptr->size = els * res->size; + ptr->u.array.dim = els; + ptr->u.array.array_of = res; + res = ptr; + break; } - - break; case DT_PTR: { - struct coff_type *ptr = (struct coff_type *) malloc (sizeof (struct coff_type)); + struct coff_type *ptr = + (struct coff_type *) xmalloc (sizeof (struct coff_type)); ptr->size = PTR_SIZE; ptr->type = coff_pointer_type; ptr->u.pointer.points_to = res; @@ -440,7 +452,8 @@ do_type (i) } case DT_FCN: { - struct coff_type *ptr = (struct coff_type *) malloc (sizeof (struct coff_type)); + struct coff_type *ptr + = (struct coff_type *) xmalloc (sizeof (struct coff_type)); ptr->size = 0; ptr->type = coff_function_type; ptr->u.function.function_returns = res; @@ -457,11 +470,11 @@ do_type (i) } static struct coff_visible * -do_visible (i) - int i; +do_visible (int i) { struct internal_syment *sym = &rawsyms[i].u.syment; - struct coff_visible *visible = (struct coff_visible *) (malloc (sizeof (struct coff_visible))); + struct coff_visible *visible = + (struct coff_visible *) (xmalloc (sizeof (struct coff_visible))); enum coff_vis_type t; switch (sym->n_sclass) { @@ -521,9 +534,7 @@ do_visible (i) } static int -do_define (i, b) - int i; - struct coff_scope *b; +do_define (int i, struct coff_scope *b) { static int symbol_index; struct internal_syment *sym = &rawsyms[i].u.syment; @@ -583,12 +594,12 @@ do_define (i, b) static struct coff_ofile * -doit () +doit (void) { int i; int infile = 0; struct coff_ofile *head = - (struct coff_ofile *) xmalloc (sizeof (struct coff_ofile)); + (struct coff_ofile *) xmalloc (sizeof (struct coff_ofile)); ofile = head; head->source_head = 0; head->source_tail = 0; @@ -606,7 +617,8 @@ doit () case C_FILE: { /* new source file announced */ - struct coff_sfile *n = (struct coff_sfile *) malloc (sizeof (struct coff_sfile)); + struct coff_sfile *n = + (struct coff_sfile *) xmalloc (sizeof (struct coff_sfile)); n->section = (struct coff_isection *) xcalloc (sizeof (struct coff_isection), abfd->section_count + 1); cur_sfile = n; n->name = sym->_n._n_nptr[1]; @@ -709,8 +721,7 @@ doit () } struct coff_ofile * -coff_grok (inabfd) - bfd *inabfd; +coff_grok (bfd *inabfd) { long storage; struct coff_ofile *p;