5 Copyright (C) 1994 Free Software Foundation, Inc.
7 This file is part of GNU Binutils.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
25 This module reads a coff file and builds a really simple type tree
26 which can be read by other programs. The first application is a
27 coff->sysroff converter. It can be tested with coffdump.c.
35 #include "coff/internal.h"
36 #include "../bfd/libcoff.h"
39 static struct coff_scope *top_scope;
40 static struct coff_scope *file_scope;
41 static struct coff_ofile *ofile;
43 struct coff_symbol *last_function_symbol;
44 struct coff_type *last_function_type;
45 struct coff_type *last_struct;
46 struct coff_type *last_enum;
47 struct coff_sfile *cur_sfile;
49 static struct coff_symbol **tindex;
52 static asymbol **syms;
55 #define N(x) ((x)->_n._n_nptr[1])
57 static struct coff_ptr_struct *rawsyms;
60 extern char *xcalloc ();
68 #define INDEXOF(p) ((struct coff_ptr_struct *)(p)-(rawsyms))
70 static struct coff_scope *
74 l = (struct coff_scope *) (xcalloc (sizeof (struct coff_scope), 1));
78 static struct coff_symbol *
81 return (struct coff_symbol *) (xcalloc (sizeof (struct coff_symbol), 1));
89 struct coff_scope *n = empty_scope ();
94 if (top_scope->list_tail)
96 top_scope->list_tail->next = n;
100 top_scope->list_head = n;
102 top_scope->list_tail = n;
105 n->parent = top_scope;
113 top_scope = top_scope->parent;
117 do_sections_p1 (head)
118 struct coff_ofile *head;
122 struct coff_section *all = (struct coff_section *) (xcalloc (abfd->section_count + 1,
123 sizeof (struct coff_section)));
124 head->nsections = abfd->section_count + 1;
125 head->sections = all;
127 for (idx = 0, section = abfd->sections; section; section = section->next, idx++)
130 int i = section->target_index;
134 relsize = bfd_get_reloc_upper_bound (abfd, section);
136 bfd_fatal (bfd_get_filename (abfd));
137 relpp = (arelent **) xmalloc (relsize);
138 relcount = bfd_canonicalize_reloc (abfd, section, relpp, syms);
140 bfd_fatal (bfd_get_filename (abfd));
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->vma;
148 head->sections[i].size = section->_raw_size;
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;
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;
165 do_sections_p2 (head)
166 struct coff_ofile *head;
169 for (section = abfd->sections; section; section = section->next)
172 for (j = 0; j < section->reloc_count; j++)
175 int i = section->target_index;
176 struct coff_reloc *r = head->sections[i].relocs + j;
177 arelent *sr = section->relocation + j;
178 r->offset = sr->address;
179 r->addend = sr->addend;
180 idx = ((coff_symbol_type *) (sr->sym_ptr_ptr[0]))->native - rawsyms;
181 r->symbol = tindex[idx];
186 static struct coff_where *
190 struct internal_syment *sym = &rawsyms[i].u.syment;
191 struct coff_where *where
192 = (struct coff_where *) (malloc (sizeof (struct coff_where)));
193 where->offset = sym->n_value;
195 if (sym->n_scnum == -1)
198 switch (sym->n_sclass)
201 where->where = coff_where_member_of_struct;
202 where->offset = sym->n_value / 8;
203 where->bitoffset = sym->n_value % 8;
204 where->bitsize = rawsyms[i + 1].u.auxent.x_sym.x_misc.x_lnsz.x_size;
207 where->where = coff_where_member_of_enum;
211 where->where = coff_where_member_of_struct;
215 where->where = coff_where_stack;
221 where->where = coff_where_memory;
222 where->section = &ofile->sections[sym->n_scnum];
226 where->where = coff_where_register;
229 where->where = coff_where_entag;
233 where->where = coff_where_strtag;
236 where->where = coff_where_typedef;
251 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 = (struct coff_type *) malloc (sizeof (struct coff_type));
301 int type = sym->n_type;
304 res->type = coff_basic_type;
305 res->u.basic = type & 0xf;
311 if (sym->n_numaux && sym->n_sclass == C_STAT)
313 /* This is probably a section definition */
314 res->type = coff_secdef_type;
315 res->size = aux->x_scn.x_scnlen;
321 /* Don't know what this is, let's make it a simple int */
322 res->size = INT_SIZE;
323 res->u.basic = T_UINT;
327 /* Else it could be a function or pointer to void */
341 res->size = SHORT_SIZE;
345 res->size = INT_SIZE;
349 res->size = LONG_SIZE;
352 res->size = FLOAT_SIZE;
355 res->size = DOUBLE_SIZE;
361 if (aux->x_sym.x_tagndx.p)
363 /* Refering to a struct defined elsewhere */
364 res->type = coff_structref_type;
365 res->u.astructref.ref = tindex[INDEXOF (aux->x_sym.x_tagndx.p)];
366 res->size = res->u.astructref.ref ?
367 res->u.astructref.ref->type->size : 0;
371 /* A definition of a struct */
373 res->type = coff_structdef_type;
374 res->u.astructdef.elements = empty_scope ();
375 res->u.astructdef.idx = 0;
376 res->u.astructdef.isstruct = (type & 0xf) == T_STRUCT;
377 res->size = aux->x_sym.x_misc.x_lnsz.x_size;
382 /* No auxents - it's anonynmous */
383 res->type = coff_structref_type;
384 res->u.astructref.ref = 0;
389 if (aux->x_sym.x_tagndx.p)
391 /* Refering to a enum defined elsewhere */
392 res->type = coff_enumref_type;
393 res->u.aenumref.ref = tindex[INDEXOF (aux->x_sym.x_tagndx.p)];
394 res->size = res->u.aenumref.ref->type->size;
398 /* A definition of an enum */
400 res->type = coff_enumdef_type;
401 res->u.aenumdef.elements = empty_scope ();
402 res->size = aux->x_sym.x_misc.x_lnsz.x_size;
409 for (which_dt = 5; which_dt >= 0; which_dt--)
411 switch ((type >> ((which_dt * 2) + 4)) & 0x3)
418 /* Look in the auxent for the dimensions and build lots of dims */
419 int els = aux->x_sym.x_fcnary.x_ary.x_dimen[which_dt];
422 struct coff_type *ptr = (struct coff_type *) malloc (sizeof (struct coff_type));
423 ptr->type = coff_array_type;
424 ptr->size = els * res->size;
425 ptr->u.array.dim = els;
426 ptr->u.array.array_of = res;
434 struct coff_type *ptr = (struct coff_type *) malloc (sizeof (struct coff_type));
435 ptr->size = PTR_SIZE;
436 ptr->type = coff_pointer_type;
437 ptr->u.pointer.points_to = res;
443 struct coff_type *ptr = (struct coff_type *) malloc (sizeof (struct coff_type));
445 ptr->type = coff_function_type;
446 ptr->u.function.function_returns = res;
447 ptr->u.function.parameters = empty_scope ();
448 ptr->u.function.lines = do_lines (i, sym->_n._n_nptr[1]);
449 ptr->u.function.code = 0;
450 last_function_type = ptr;
459 static struct coff_visible *
463 struct internal_syment *sym = &rawsyms[i].u.syment;
464 struct coff_visible *visible = (struct coff_visible *) (malloc (sizeof (struct coff_visible)));
465 enum coff_vis_type t;
466 switch (sym->n_sclass)
471 t = coff_vis_member_of_struct;
474 t = coff_vis_member_of_enum;
478 t = coff_vis_regparam;
482 t = coff_vis_register;
492 t = coff_vis_autoparam;
501 t = coff_vis_int_def;
504 if (sym->n_scnum == N_UNDEF)
509 t = coff_vis_ext_ref;
512 t = coff_vis_ext_def;
526 struct coff_scope *b;
528 static int symbol_index;
529 struct internal_syment *sym = &rawsyms[i].u.syment;
531 /* Define a symbol and attach to block b */
532 struct coff_symbol *s = empty_symbol ();
534 s->number = ++symbol_index;
535 s->name = sym->_n._n_nptr[1];
536 s->sfile = cur_sfile;
537 /* Glue onto the ofile list */
540 if (ofile->symbol_list_tail)
541 ofile->symbol_list_tail->next_in_ofile_list = s;
543 ofile->symbol_list_head = s;
544 ofile->symbol_list_tail = s;
545 /* And the block list */
548 b->vars_tail->next = s;
554 s->type = do_type (i);
555 s->where = do_where (i);
556 s->visible = do_visible (i);
560 /* We remember the lowest address in each section for each source file */
562 if (s->where->where == coff_where_memory
563 && s->type->type == coff_secdef_type)
565 struct coff_isection *is = cur_sfile->section + s->where->section->number;
569 is->low = s->where->offset;
570 is->high = s->where->offset + s->type->size;
572 is->parent = s->where->section;
577 if (s->type->type == coff_function_type)
578 last_function_symbol = s;
580 return i + sym->n_numaux + 1;
590 struct coff_ofile *head =
591 (struct coff_ofile *) xmalloc (sizeof (struct coff_ofile));
593 head->source_head = 0;
594 head->source_tail = 0;
596 head->symbol_list_tail = 0;
597 head->symbol_list_head = 0;
598 do_sections_p1 (head);
601 for (i = 0; i < rawcount;)
603 struct internal_syment *sym = &rawsyms[i].u.syment;
604 switch (sym->n_sclass)
608 /* new source file announced */
609 struct coff_sfile *n = (struct coff_sfile *) malloc (sizeof (struct coff_sfile));
610 n->section = (struct coff_isection *) xcalloc (sizeof (struct coff_isection), abfd->section_count + 1);
612 n->name = sym->_n._n_nptr[1];
621 file_scope = n->scope = top_scope;
623 if (head->source_tail)
624 head->source_tail->next = n;
626 head->source_head = n;
627 head->source_tail = n;
629 i += sym->n_numaux + 1;
634 char *name = sym->_n._n_nptr[1];
639 last_function_type->u.function.code = top_scope;
640 top_scope->sec = ofile->sections + sym->n_scnum;
641 top_scope->offset = sym->n_value;
645 top_scope->size = sym->n_value - top_scope->offset + 1;
649 i += sym->n_numaux + 1;
655 char *name = sym->_n._n_nptr[1];
660 top_scope->sec = ofile->sections + sym->n_scnum;
661 top_scope->offset = sym->n_value;
666 top_scope->size = sym->n_value - top_scope->offset + 1;
669 i += sym->n_numaux + 1;
674 i = do_define (i, last_function_symbol->type->u.function.parameters);
679 i = do_define (i, last_struct->u.astructdef.elements);
682 i = do_define (i, last_enum->u.aenumdef.elements);
687 /* Various definition */
688 i = do_define (i, top_scope);
692 i = do_define (i, file_scope);
698 i = do_define (i, top_scope);
703 i += sym->n_numaux + 1;
707 do_sections_p2 (head);
716 struct coff_ofile *p;
718 storage = bfd_get_symtab_upper_bound (abfd);
721 bfd_fatal (abfd->filename);
723 syms = (asymbol **) xmalloc (storage);
724 symcount = bfd_canonicalize_symtab (abfd, syms);
726 bfd_fatal (abfd->filename);
727 rawsyms = obj_raw_syments (abfd);
728 rawcount = obj_raw_syment_count (abfd);;
729 tindex = (struct coff_symbol **) (xcalloc (sizeof (struct coff_symbol *), rawcount));