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)
170 for (j = 0; j < section->reloc_count; j++)
173 int i = section->target_index;
174 struct coff_reloc *r = head->sections[i].relocs + j;
175 arelent *sr = section->relocation + j;
176 r->offset = sr->address;
177 r->addend = sr->addend;
178 idx = ((coff_symbol_type *) (sr->sym_ptr_ptr[0]))->native - rawsyms;
179 r->symbol = tindex[idx];
184 static struct coff_where *
188 struct internal_syment *sym = &rawsyms[i].u.syment;
189 struct coff_where *where =
190 (struct coff_where *) (xmalloc (sizeof (struct coff_where)));
191 where->offset = sym->n_value;
193 if (sym->n_scnum == -1)
196 switch (sym->n_sclass)
199 where->where = coff_where_member_of_struct;
200 where->offset = sym->n_value / 8;
201 where->bitoffset = sym->n_value % 8;
202 where->bitsize = rawsyms[i + 1].u.auxent.x_sym.x_misc.x_lnsz.x_size;
205 where->where = coff_where_member_of_enum;
209 where->where = coff_where_member_of_struct;
213 where->where = coff_where_stack;
219 where->where = coff_where_memory;
220 where->section = &ofile->sections[sym->n_scnum];
224 where->where = coff_where_register;
227 where->where = coff_where_entag;
231 where->where = coff_where_strtag;
234 where->where = coff_where_typedef;
249 struct coff_line *res = (struct coff_line *) xcalloc (sizeof (struct coff_line), 1);
252 /* Find out if this function has any line numbers in the table */
253 for (s = abfd->sections; s; s = s->next)
255 for (l = 0; l < s->lineno_count; l++)
257 if (s->lineno[l].line_number == 0)
259 if (rawsyms + i == ((coff_symbol_type *) (&(s->lineno[l].u.sym[0])))->native)
261 /* These lines are for this function - so count them and stick them on */
263 /* Find the linenumber of the top of the function, since coff linenumbers
264 are relative to the start of the function. */
265 int start_line = rawsyms[i + 3].u.auxent.x_sym.x_misc.x_lnsz.x_lnno;
268 for (c = 0; s->lineno[l + c + 1].line_number; c++)
271 /* Add two extra records, one for the prologue and one for the epilogue */
274 res->lines = (int *) (xcalloc (sizeof (int), c));
275 res->addresses = (int *) (xcalloc (sizeof (int), c));
276 res->lines[0] = start_line;
277 res->addresses[0] = rawsyms[i].u.syment.n_value - s->vma;
278 for (c = 0; s->lineno[l + c + 1].line_number; c++)
280 res->lines[c + 1] = s->lineno[l + c].line_number + start_line - 1;
281 res->addresses[c + 1] = s->lineno[l + c].u.offset;
296 struct internal_syment *sym = &rawsyms[i].u.syment;
297 union internal_auxent *aux = &rawsyms[i + 1].u.auxent;
298 struct coff_type *res =
299 (struct coff_type *) xmalloc (sizeof (struct coff_type));
300 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)
417 struct coff_type *ptr = ((struct coff_type *)
418 xmalloc (sizeof (struct coff_type)));
419 int els = (dimind < DIMNUM
420 ? aux->x_sym.x_fcnary.x_ary.x_dimen[dimind]
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;
432 struct coff_type *ptr =
433 (struct coff_type *) xmalloc (sizeof (struct coff_type));
434 ptr->size = PTR_SIZE;
435 ptr->type = coff_pointer_type;
436 ptr->u.pointer.points_to = res;
442 struct coff_type *ptr
443 = (struct coff_type *) xmalloc (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 =
465 (struct coff_visible *) (xmalloc (sizeof (struct coff_visible)));
466 enum coff_vis_type t;
467 switch (sym->n_sclass)
472 t = coff_vis_member_of_struct;
475 t = coff_vis_member_of_enum;
479 t = coff_vis_regparam;
483 t = coff_vis_register;
493 t = coff_vis_autoparam;
502 t = coff_vis_int_def;
505 if (sym->n_scnum == N_UNDEF)
510 t = coff_vis_ext_ref;
513 t = coff_vis_ext_def;
527 struct coff_scope *b;
529 static int symbol_index;
530 struct internal_syment *sym = &rawsyms[i].u.syment;
532 /* Define a symbol and attach to block b */
533 struct coff_symbol *s = empty_symbol ();
535 s->number = ++symbol_index;
536 s->name = sym->_n._n_nptr[1];
537 s->sfile = cur_sfile;
538 /* Glue onto the ofile list */
541 if (ofile->symbol_list_tail)
542 ofile->symbol_list_tail->next_in_ofile_list = s;
544 ofile->symbol_list_head = s;
545 ofile->symbol_list_tail = s;
546 /* And the block list */
549 b->vars_tail->next = s;
555 s->type = do_type (i);
556 s->where = do_where (i);
557 s->visible = do_visible (i);
561 /* We remember the lowest address in each section for each source file */
563 if (s->where->where == coff_where_memory
564 && s->type->type == coff_secdef_type)
566 struct coff_isection *is = cur_sfile->section + s->where->section->number;
570 is->low = s->where->offset;
571 is->high = s->where->offset + s->type->size;
573 is->parent = s->where->section;
578 if (s->type->type == coff_function_type)
579 last_function_symbol = s;
581 return i + sym->n_numaux + 1;
591 struct coff_ofile *head =
592 (struct coff_ofile *) xmalloc (sizeof (struct coff_ofile));
594 head->source_head = 0;
595 head->source_tail = 0;
597 head->symbol_list_tail = 0;
598 head->symbol_list_head = 0;
599 do_sections_p1 (head);
602 for (i = 0; i < rawcount;)
604 struct internal_syment *sym = &rawsyms[i].u.syment;
605 switch (sym->n_sclass)
609 /* new source file announced */
610 struct coff_sfile *n =
611 (struct coff_sfile *) xmalloc (sizeof (struct coff_sfile));
612 n->section = (struct coff_isection *) xcalloc (sizeof (struct coff_isection), abfd->section_count + 1);
614 n->name = sym->_n._n_nptr[1];
623 file_scope = n->scope = top_scope;
625 if (head->source_tail)
626 head->source_tail->next = n;
628 head->source_head = n;
629 head->source_tail = n;
631 i += sym->n_numaux + 1;
636 char *name = sym->_n._n_nptr[1];
641 last_function_type->u.function.code = top_scope;
642 top_scope->sec = ofile->sections + sym->n_scnum;
643 top_scope->offset = sym->n_value;
647 top_scope->size = sym->n_value - top_scope->offset + 1;
651 i += sym->n_numaux + 1;
657 char *name = sym->_n._n_nptr[1];
662 top_scope->sec = ofile->sections + sym->n_scnum;
663 top_scope->offset = sym->n_value;
668 top_scope->size = sym->n_value - top_scope->offset + 1;
671 i += sym->n_numaux + 1;
676 i = do_define (i, last_function_symbol->type->u.function.parameters);
681 i = do_define (i, last_struct->u.astructdef.elements);
684 i = do_define (i, last_enum->u.aenumdef.elements);
689 /* Various definition */
690 i = do_define (i, top_scope);
694 i = do_define (i, file_scope);
700 i = do_define (i, top_scope);
705 i += sym->n_numaux + 1;
709 do_sections_p2 (head);
718 struct coff_ofile *p;
720 storage = bfd_get_symtab_upper_bound (abfd);
723 bfd_fatal (abfd->filename);
725 syms = (asymbol **) xmalloc (storage);
726 symcount = bfd_canonicalize_symtab (abfd, syms);
728 bfd_fatal (abfd->filename);
729 rawsyms = obj_raw_syments (abfd);
730 rawcount = obj_raw_syment_count (abfd);;
731 tindex = (struct coff_symbol **) (xcalloc (sizeof (struct coff_symbol *), rawcount));