2 Copyright (C) 1994, 95, 1997 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
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.
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.
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. */
20 /* Written by Steve Chamberlain (sac@cygnus.com)
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.
31 #include "coff/internal.h"
32 #include "../bfd/libcoff.h"
35 static struct coff_scope *top_scope;
36 static struct coff_scope *file_scope;
37 static struct coff_ofile *ofile;
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;
45 static struct coff_symbol **tindex;
48 static asymbol **syms;
51 #define N(x) ((x)->_n._n_nptr[1])
53 static struct coff_ptr_struct *rawsyms;
56 extern char *xcalloc ();
64 #define INDEXOF(p) ((struct coff_ptr_struct *)(p)-(rawsyms))
66 static struct coff_scope *
70 l = (struct coff_scope *) (xcalloc (sizeof (struct coff_scope), 1));
74 static struct coff_symbol *
77 return (struct coff_symbol *) (xcalloc (sizeof (struct coff_symbol), 1));
85 struct coff_scope *n = empty_scope ();
90 if (top_scope->list_tail)
92 top_scope->list_tail->next = n;
96 top_scope->list_head = n;
98 top_scope->list_tail = n;
101 n->parent = top_scope;
109 top_scope = top_scope->parent;
113 do_sections_p1 (head)
114 struct coff_ofile *head;
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;
123 for (idx = 0, section = abfd->sections; section; section = section->next, idx++)
126 int i = section->target_index;
130 relsize = bfd_get_reloc_upper_bound (abfd, section);
132 bfd_fatal (bfd_get_filename (abfd));
135 relpp = (arelent **) xmalloc (relsize);
136 relcount = bfd_canonicalize_reloc (abfd, section, relpp, syms);
138 bfd_fatal (bfd_get_filename (abfd));
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;
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;
163 do_sections_p2 (head)
164 struct coff_ofile *head;
167 for (section = abfd->sections; section; section = section->next)
171 for (j = 0; j < section->reloc_count; j++)
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];
185 static struct coff_where *
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;
194 if (sym->n_scnum == -1)
197 switch (sym->n_sclass)
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;
206 where->where = coff_where_member_of_enum;
210 where->where = coff_where_member_of_struct;
214 where->where = coff_where_stack;
220 where->where = coff_where_memory;
221 where->section = &ofile->sections[sym->n_scnum];
225 where->where = coff_where_register;
228 where->where = coff_where_entag;
232 where->where = coff_where_strtag;
235 where->where = coff_where_typedef;
250 struct coff_line *res = (struct coff_line *) xcalloc (sizeof (struct coff_line), 1);
254 /* Find out if this function has any line numbers in the table */
255 for (s = abfd->sections; s; s = s->next)
257 for (l = 0; l < s->lineno_count; l++)
259 if (s->lineno[l].line_number == 0)
261 if (rawsyms + i == ((coff_symbol_type *) (&(s->lineno[l].u.sym[0])))->native)
263 /* These lines are for this function - so count them and stick them on */
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;
270 for (c = 0; s->lineno[l + c + 1].line_number; c++)
273 /* Add two extra records, one for the prologue and one for the epilogue */
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++)
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;
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;
306 res->type = coff_basic_type;
307 res->u.basic = type & 0xf;
313 if (sym->n_numaux && sym->n_sclass == C_STAT)
315 /* This is probably a section definition */
316 res->type = coff_secdef_type;
317 res->size = aux->x_scn.x_scnlen;
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;
329 /* Else it could be a function or pointer to void */
343 res->size = SHORT_SIZE;
347 res->size = INT_SIZE;
351 res->size = LONG_SIZE;
354 res->size = FLOAT_SIZE;
357 res->size = DOUBLE_SIZE;
363 if (aux->x_sym.x_tagndx.p)
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;
373 /* A definition of a struct */
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;
384 /* No auxents - it's anonynmous */
385 res->type = coff_structref_type;
386 res->u.astructref.ref = 0;
391 if (aux->x_sym.x_tagndx.p)
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;
400 /* A definition of an enum */
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;
411 for (which_dt = 5; which_dt >= 0; which_dt--)
413 switch ((type >> ((which_dt * 2) + 4)) & 0x3)
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]
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;
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;
444 struct coff_type *ptr
445 = (struct coff_type *) xmalloc (sizeof (struct coff_type));
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;
461 static struct coff_visible *
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)
474 t = coff_vis_member_of_struct;
477 t = coff_vis_member_of_enum;
481 t = coff_vis_regparam;
485 t = coff_vis_register;
495 t = coff_vis_autoparam;
504 t = coff_vis_int_def;
507 if (sym->n_scnum == N_UNDEF)
512 t = coff_vis_ext_ref;
515 t = coff_vis_ext_def;
529 struct coff_scope *b;
531 static int symbol_index;
532 struct internal_syment *sym = &rawsyms[i].u.syment;
534 /* Define a symbol and attach to block b */
535 struct coff_symbol *s = empty_symbol ();
537 s->number = ++symbol_index;
538 s->name = sym->_n._n_nptr[1];
539 s->sfile = cur_sfile;
540 /* Glue onto the ofile list */
543 if (ofile->symbol_list_tail)
544 ofile->symbol_list_tail->next_in_ofile_list = s;
546 ofile->symbol_list_head = s;
547 ofile->symbol_list_tail = s;
548 /* And the block list */
551 b->vars_tail->next = s;
557 s->type = do_type (i);
558 s->where = do_where (i);
559 s->visible = do_visible (i);
563 /* We remember the lowest address in each section for each source file */
565 if (s->where->where == coff_where_memory
566 && s->type->type == coff_secdef_type)
568 struct coff_isection *is = cur_sfile->section + s->where->section->number;
572 is->low = s->where->offset;
573 is->high = s->where->offset + s->type->size;
575 is->parent = s->where->section;
580 if (s->type->type == coff_function_type)
581 last_function_symbol = s;
583 return i + sym->n_numaux + 1;
593 struct coff_ofile *head =
594 (struct coff_ofile *) xmalloc (sizeof (struct coff_ofile));
596 head->source_head = 0;
597 head->source_tail = 0;
599 head->symbol_list_tail = 0;
600 head->symbol_list_head = 0;
601 do_sections_p1 (head);
604 for (i = 0; i < rawcount;)
606 struct internal_syment *sym = &rawsyms[i].u.syment;
607 switch (sym->n_sclass)
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);
616 n->name = sym->_n._n_nptr[1];
625 file_scope = n->scope = top_scope;
627 if (head->source_tail)
628 head->source_tail->next = n;
630 head->source_head = n;
631 head->source_tail = n;
633 i += sym->n_numaux + 1;
638 char *name = sym->_n._n_nptr[1];
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;
649 top_scope->size = sym->n_value - top_scope->offset + 1;
653 i += sym->n_numaux + 1;
659 char *name = sym->_n._n_nptr[1];
664 top_scope->sec = ofile->sections + sym->n_scnum;
665 top_scope->offset = sym->n_value;
670 top_scope->size = sym->n_value - top_scope->offset + 1;
673 i += sym->n_numaux + 1;
678 i = do_define (i, last_function_symbol->type->u.function.parameters);
683 i = do_define (i, last_struct->u.astructdef.elements);
686 i = do_define (i, last_enum->u.aenumdef.elements);
691 /* Various definition */
692 i = do_define (i, top_scope);
696 i = do_define (i, file_scope);
702 i = do_define (i, top_scope);
707 i += sym->n_numaux + 1;
711 do_sections_p2 (head);
720 struct coff_ofile *p;
722 storage = bfd_get_symtab_upper_bound (abfd);
725 bfd_fatal (abfd->filename);
727 syms = (asymbol **) xmalloc (storage);
728 symcount = bfd_canonicalize_symtab (abfd, syms);
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));