Add Cirrus Maverick support to arm simulator
[binutils.git] / binutils / coffgrok.c
blob8cab8d57eb19c828caa34c31af6f009ac145d6d9
1 /* coffgrok.c
2 Copyright 1994, 1995, 1997, 1998, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of GNU Binutils.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* Written by Steve Chamberlain (sac@cygnus.com)
23 This module reads a coff file and builds a really simple type tree
24 which can be read by other programs. The first application is a
25 coff->sysroff converter. It can be tested with coffdump.c.
29 #include "bfd.h"
30 #include "libiberty.h"
31 #include "bucomm.h"
33 #include "coff/internal.h"
34 #include "../bfd/libcoff.h"
35 #include "coffgrok.h"
36 int lofile = 1;
37 static struct coff_scope *top_scope;
38 static struct coff_scope *file_scope;
39 static struct coff_ofile *ofile;
41 struct coff_symbol *last_function_symbol;
42 struct coff_type *last_function_type;
43 struct coff_type *last_struct;
44 struct coff_type *last_enum;
45 struct coff_sfile *cur_sfile;
47 static struct coff_symbol **tindex;
50 static asymbol **syms;
51 static long symcount;
53 #define N(x) ((x)->_n._n_nptr[1])
55 static struct coff_ptr_struct *rawsyms;
56 static int rawcount;
57 static bfd *abfd;
59 #define PTR_SIZE 4
60 #define SHORT_SIZE 2
61 #define INT_SIZE 4
62 #define LONG_SIZE 4
63 #define FLOAT_SIZE 4
64 #define DOUBLE_SIZE 8
66 #define INDEXOF(p) ((struct coff_ptr_struct *)(p)-(rawsyms))
68 static struct coff_scope *empty_scope PARAMS ((void));
69 static struct coff_symbol *empty_symbol PARAMS ((void));
70 static void push_scope PARAMS ((int));
71 static void pop_scope PARAMS ((void));
72 static void do_sections_p1 PARAMS ((struct coff_ofile *));
73 static void do_sections_p2 PARAMS ((struct coff_ofile *));
74 static struct coff_where *do_where PARAMS ((int));
75 static struct coff_line *do_lines PARAMS ((int, char *));
76 static struct coff_type *do_type PARAMS ((int));
77 static struct coff_visible *do_visible PARAMS ((int));
78 static int do_define PARAMS ((int, struct coff_scope *));
79 static struct coff_ofile *doit PARAMS ((void));
81 static struct coff_scope *
82 empty_scope ()
84 struct coff_scope *l;
85 l = (struct coff_scope *) (xcalloc (sizeof (struct coff_scope), 1));
86 return l;
89 static struct coff_symbol *
90 empty_symbol ()
92 return (struct coff_symbol *) (xcalloc (sizeof (struct coff_symbol), 1));
95 /*int l;*/
96 static void
97 push_scope (link)
98 int link;
100 struct coff_scope *n = empty_scope ();
101 if (link)
103 if (top_scope)
105 if (top_scope->list_tail)
107 top_scope->list_tail->next = n;
109 else
111 top_scope->list_head = n;
113 top_scope->list_tail = n;
116 n->parent = top_scope;
118 top_scope = n;
121 static void
122 pop_scope ()
124 top_scope = top_scope->parent;
127 static void
128 do_sections_p1 (head)
129 struct coff_ofile *head;
131 asection *section;
132 int idx;
133 struct coff_section *all = (struct coff_section *) (xcalloc (abfd->section_count + 1,
134 sizeof (struct coff_section)));
135 head->nsections = abfd->section_count + 1;
136 head->sections = all;
138 for (idx = 0, section = abfd->sections; section; section = section->next, idx++)
140 long relsize;
141 int i = section->target_index;
142 arelent **relpp;
143 long relcount;
145 relsize = bfd_get_reloc_upper_bound (abfd, section);
146 if (relsize < 0)
147 bfd_fatal (bfd_get_filename (abfd));
148 if (relsize == 0)
149 continue;
150 relpp = (arelent **) xmalloc (relsize);
151 relcount = bfd_canonicalize_reloc (abfd, section, relpp, syms);
152 if (relcount < 0)
153 bfd_fatal (bfd_get_filename (abfd));
155 head->sections[i].name = (char *) (section->name);
156 head->sections[i].code = section->flags & SEC_CODE;
157 head->sections[i].data = section->flags & SEC_DATA;
158 if (strcmp (section->name, ".bss") == 0)
159 head->sections[i].data = 1;
160 head->sections[i].address = section->lma;
161 head->sections[i].size = section->_raw_size;
162 head->sections[i].number = idx;
163 head->sections[i].nrelocs = section->reloc_count;
164 head->sections[i].relocs =
165 (struct coff_reloc *) (xcalloc (section->reloc_count,
166 sizeof (struct coff_reloc)));
167 head->sections[i].bfd_section = section;
169 head->sections[0].name = "ABSOLUTE";
170 head->sections[0].code = 0;
171 head->sections[0].data = 0;
172 head->sections[0].address = 0;
173 head->sections[0].size = 0;
174 head->sections[0].number = 0;
177 static void
178 do_sections_p2 (head)
179 struct coff_ofile *head;
181 asection *section;
182 for (section = abfd->sections; section; section = section->next)
184 unsigned int j;
186 for (j = 0; j < section->reloc_count; j++)
188 int idx;
189 int i = section->target_index;
190 struct coff_reloc *r = head->sections[i].relocs + j;
191 arelent *sr = section->relocation + j;
192 r->offset = sr->address;
193 r->addend = sr->addend;
194 idx = ((coff_symbol_type *) (sr->sym_ptr_ptr[0]))->native - rawsyms;
195 r->symbol = tindex[idx];
200 static struct coff_where *
201 do_where (i)
202 int i;
204 struct internal_syment *sym = &rawsyms[i].u.syment;
205 struct coff_where *where =
206 (struct coff_where *) (xmalloc (sizeof (struct coff_where)));
207 where->offset = sym->n_value;
209 if (sym->n_scnum == -1)
210 sym->n_scnum = 0;
212 switch (sym->n_sclass)
214 case C_FIELD:
215 where->where = coff_where_member_of_struct;
216 where->offset = sym->n_value / 8;
217 where->bitoffset = sym->n_value % 8;
218 where->bitsize = rawsyms[i + 1].u.auxent.x_sym.x_misc.x_lnsz.x_size;
219 break;
220 case C_MOE:
221 where->where = coff_where_member_of_enum;
222 break;
223 case C_MOS:
224 case C_MOU:
225 where->where = coff_where_member_of_struct;
226 break;
227 case C_AUTO:
228 case C_ARG:
229 where->where = coff_where_stack;
230 break;
231 case C_EXT:
232 case C_STAT:
233 case C_EXTDEF:
234 case C_LABEL:
235 where->where = coff_where_memory;
236 where->section = &ofile->sections[sym->n_scnum];
237 break;
238 case C_REG:
239 case C_REGPARM:
240 where->where = coff_where_register;
241 break;
242 case C_ENTAG:
243 where->where = coff_where_entag;
244 break;
245 case C_STRTAG:
246 case C_UNTAG:
247 where->where = coff_where_strtag;
248 break;
249 case C_TPDEF:
250 where->where = coff_where_typedef;
251 break;
252 default:
253 abort ();
254 break;
256 return where;
259 static
260 struct coff_line *
261 do_lines (i, name)
262 int i;
263 char *name ATTRIBUTE_UNUSED;
265 struct coff_line *res = (struct coff_line *) xcalloc (sizeof (struct coff_line), 1);
266 asection *s;
267 unsigned int l;
269 /* Find out if this function has any line numbers in the table */
270 for (s = abfd->sections; s; s = s->next)
272 for (l = 0; l < s->lineno_count; l++)
274 if (s->lineno[l].line_number == 0)
276 if (rawsyms + i == ((coff_symbol_type *) (&(s->lineno[l].u.sym[0])))->native)
278 /* These lines are for this function - so count them and stick them on */
279 int c = 0;
280 /* Find the linenumber of the top of the function, since coff linenumbers
281 are relative to the start of the function. */
282 int start_line = rawsyms[i + 3].u.auxent.x_sym.x_misc.x_lnsz.x_lnno;
284 l++;
285 for (c = 0; s->lineno[l + c + 1].line_number; c++)
288 /* Add two extra records, one for the prologue and one for the epilogue */
289 c += 1;
290 res->nlines = c;
291 res->lines = (int *) (xcalloc (sizeof (int), c));
292 res->addresses = (int *) (xcalloc (sizeof (int), c));
293 res->lines[0] = start_line;
294 res->addresses[0] = rawsyms[i].u.syment.n_value - s->vma;
295 for (c = 0; s->lineno[l + c + 1].line_number; c++)
297 res->lines[c + 1] = s->lineno[l + c].line_number + start_line - 1;
298 res->addresses[c + 1] = s->lineno[l + c].u.offset;
300 return res;
305 return res;
308 static
309 struct coff_type *
310 do_type (i)
311 int i;
313 struct internal_syment *sym = &rawsyms[i].u.syment;
314 union internal_auxent *aux = &rawsyms[i + 1].u.auxent;
315 struct coff_type *res =
316 (struct coff_type *) xmalloc (sizeof (struct coff_type));
317 int type = sym->n_type;
318 int which_dt = 0;
319 int dimind = 0;
321 res->type = coff_basic_type;
322 res->u.basic = type & 0xf;
324 switch (type & 0xf)
326 case T_NULL:
327 case T_VOID:
328 if (sym->n_numaux && sym->n_sclass == C_STAT)
330 /* This is probably a section definition */
331 res->type = coff_secdef_type;
332 res->size = aux->x_scn.x_scnlen;
334 else
336 if (type == 0)
338 /* Don't know what this is, let's make it a simple int */
339 res->size = INT_SIZE;
340 res->u.basic = T_UINT;
342 else
344 /* Else it could be a function or pointer to void */
345 res->size = 0;
348 break;
351 break;
352 case T_UCHAR:
353 case T_CHAR:
354 res->size = 1;
355 break;
356 case T_USHORT:
357 case T_SHORT:
358 res->size = SHORT_SIZE;
359 break;
360 case T_UINT:
361 case T_INT:
362 res->size = INT_SIZE;
363 break;
364 case T_ULONG:
365 case T_LONG:
366 res->size = LONG_SIZE;
367 break;
368 case T_FLOAT:
369 res->size = FLOAT_SIZE;
370 break;
371 case T_DOUBLE:
372 res->size = DOUBLE_SIZE;
373 break;
374 case T_STRUCT:
375 case T_UNION:
376 if (sym->n_numaux)
378 if (aux->x_sym.x_tagndx.p)
380 /* Refering to a struct defined elsewhere */
381 res->type = coff_structref_type;
382 res->u.astructref.ref = tindex[INDEXOF (aux->x_sym.x_tagndx.p)];
383 res->size = res->u.astructref.ref ?
384 res->u.astructref.ref->type->size : 0;
386 else
388 /* A definition of a struct */
389 last_struct = res;
390 res->type = coff_structdef_type;
391 res->u.astructdef.elements = empty_scope ();
392 res->u.astructdef.idx = 0;
393 res->u.astructdef.isstruct = (type & 0xf) == T_STRUCT;
394 res->size = aux->x_sym.x_misc.x_lnsz.x_size;
397 else
399 /* No auxents - it's anonynmous */
400 res->type = coff_structref_type;
401 res->u.astructref.ref = 0;
402 res->size = 0;
404 break;
405 case T_ENUM:
406 if (aux->x_sym.x_tagndx.p)
408 /* Refering to a enum defined elsewhere */
409 res->type = coff_enumref_type;
410 res->u.aenumref.ref = tindex[INDEXOF (aux->x_sym.x_tagndx.p)];
411 res->size = res->u.aenumref.ref->type->size;
413 else
415 /* A definition of an enum */
416 last_enum = res;
417 res->type = coff_enumdef_type;
418 res->u.aenumdef.elements = empty_scope ();
419 res->size = aux->x_sym.x_misc.x_lnsz.x_size;
421 break;
422 case T_MOE:
423 break;
426 for (which_dt = 5; which_dt >= 0; which_dt--)
428 switch ((type >> ((which_dt * 2) + 4)) & 0x3)
430 case 0:
431 break;
432 case DT_ARY:
434 struct coff_type *ptr = ((struct coff_type *)
435 xmalloc (sizeof (struct coff_type)));
436 int els = (dimind < DIMNUM
437 ? aux->x_sym.x_fcnary.x_ary.x_dimen[dimind]
438 : 0);
439 ++dimind;
440 ptr->type = coff_array_type;
441 ptr->size = els * res->size;
442 ptr->u.array.dim = els;
443 ptr->u.array.array_of = res;
444 res = ptr;
445 break;
447 case DT_PTR:
449 struct coff_type *ptr =
450 (struct coff_type *) xmalloc (sizeof (struct coff_type));
451 ptr->size = PTR_SIZE;
452 ptr->type = coff_pointer_type;
453 ptr->u.pointer.points_to = res;
454 res = ptr;
455 break;
457 case DT_FCN:
459 struct coff_type *ptr
460 = (struct coff_type *) xmalloc (sizeof (struct coff_type));
461 ptr->size = 0;
462 ptr->type = coff_function_type;
463 ptr->u.function.function_returns = res;
464 ptr->u.function.parameters = empty_scope ();
465 ptr->u.function.lines = do_lines (i, sym->_n._n_nptr[1]);
466 ptr->u.function.code = 0;
467 last_function_type = ptr;
468 res = ptr;
469 break;
473 return res;
476 static struct coff_visible *
477 do_visible (i)
478 int i;
480 struct internal_syment *sym = &rawsyms[i].u.syment;
481 struct coff_visible *visible =
482 (struct coff_visible *) (xmalloc (sizeof (struct coff_visible)));
483 enum coff_vis_type t;
484 switch (sym->n_sclass)
486 case C_MOS:
487 case C_MOU:
488 case C_FIELD:
489 t = coff_vis_member_of_struct;
490 break;
491 case C_MOE:
492 t = coff_vis_member_of_enum;
493 break;
495 case C_REGPARM:
496 t = coff_vis_regparam;
497 break;
499 case C_REG:
500 t = coff_vis_register;
501 break;
502 case C_STRTAG:
503 case C_UNTAG:
504 case C_ENTAG:
505 case C_TPDEF:
506 t = coff_vis_tag;
507 break;
508 case C_AUTOARG:
509 case C_ARG:
510 t = coff_vis_autoparam;
511 break;
512 case C_AUTO:
515 t = coff_vis_auto;
516 break;
517 case C_LABEL:
518 case C_STAT:
519 t = coff_vis_int_def;
520 break;
521 case C_EXT:
522 if (sym->n_scnum == N_UNDEF)
524 if (sym->n_value)
525 t = coff_vis_common;
526 else
527 t = coff_vis_ext_ref;
529 else
530 t = coff_vis_ext_def;
531 break;
532 default:
533 abort ();
534 break;
537 visible->type = t;
538 return visible;
541 static int
542 do_define (i, b)
543 int i;
544 struct coff_scope *b;
546 static int symbol_index;
547 struct internal_syment *sym = &rawsyms[i].u.syment;
549 /* Define a symbol and attach to block b */
550 struct coff_symbol *s = empty_symbol ();
552 s->number = ++symbol_index;
553 s->name = sym->_n._n_nptr[1];
554 s->sfile = cur_sfile;
555 /* Glue onto the ofile list */
556 if (lofile >= 0)
558 if (ofile->symbol_list_tail)
559 ofile->symbol_list_tail->next_in_ofile_list = s;
560 else
561 ofile->symbol_list_head = s;
562 ofile->symbol_list_tail = s;
563 /* And the block list */
565 if (b->vars_tail)
566 b->vars_tail->next = s;
567 else
568 b->vars_head = s;
570 b->vars_tail = s;
571 b->nvars++;
572 s->type = do_type (i);
573 s->where = do_where (i);
574 s->visible = do_visible (i);
576 tindex[i] = s;
578 /* We remember the lowest address in each section for each source file */
580 if (s->where->where == coff_where_memory
581 && s->type->type == coff_secdef_type)
583 struct coff_isection *is = cur_sfile->section + s->where->section->number;
585 if (!is->init)
587 is->low = s->where->offset;
588 is->high = s->where->offset + s->type->size;
589 is->init = 1;
590 is->parent = s->where->section;
595 if (s->type->type == coff_function_type)
596 last_function_symbol = s;
598 return i + sym->n_numaux + 1;
602 static
603 struct coff_ofile *
604 doit ()
606 int i;
607 int infile = 0;
608 struct coff_ofile *head =
609 (struct coff_ofile *) xmalloc (sizeof (struct coff_ofile));
610 ofile = head;
611 head->source_head = 0;
612 head->source_tail = 0;
613 head->nsources = 0;
614 head->symbol_list_tail = 0;
615 head->symbol_list_head = 0;
616 do_sections_p1 (head);
617 push_scope (1);
619 for (i = 0; i < rawcount;)
621 struct internal_syment *sym = &rawsyms[i].u.syment;
622 switch (sym->n_sclass)
624 case C_FILE:
626 /* new source file announced */
627 struct coff_sfile *n =
628 (struct coff_sfile *) xmalloc (sizeof (struct coff_sfile));
629 n->section = (struct coff_isection *) xcalloc (sizeof (struct coff_isection), abfd->section_count + 1);
630 cur_sfile = n;
631 n->name = sym->_n._n_nptr[1];
632 n->next = 0;
634 if (infile)
636 pop_scope ();
638 infile = 1;
639 push_scope (1);
640 file_scope = n->scope = top_scope;
642 if (head->source_tail)
643 head->source_tail->next = n;
644 else
645 head->source_head = n;
646 head->source_tail = n;
647 head->nsources++;
648 i += sym->n_numaux + 1;
650 break;
651 case C_FCN:
653 char *name = sym->_n._n_nptr[1];
654 if (name[1] == 'b')
656 /* Function start */
657 push_scope (0);
658 last_function_type->u.function.code = top_scope;
659 top_scope->sec = ofile->sections + sym->n_scnum;
660 top_scope->offset = sym->n_value;
662 else
664 top_scope->size = sym->n_value - top_scope->offset + 1;
665 pop_scope ();
668 i += sym->n_numaux + 1;
670 break;
672 case C_BLOCK:
674 char *name = sym->_n._n_nptr[1];
675 if (name[1] == 'b')
677 /* Block start */
678 push_scope (1);
679 top_scope->sec = ofile->sections + sym->n_scnum;
680 top_scope->offset = sym->n_value;
683 else
685 top_scope->size = sym->n_value - top_scope->offset + 1;
686 pop_scope ();
688 i += sym->n_numaux + 1;
690 break;
691 case C_REGPARM:
692 case C_ARG:
693 i = do_define (i, last_function_symbol->type->u.function.parameters);
694 break;
695 case C_MOS:
696 case C_MOU:
697 case C_FIELD:
698 i = do_define (i, last_struct->u.astructdef.elements);
699 break;
700 case C_MOE:
701 i = do_define (i, last_enum->u.aenumdef.elements);
702 break;
703 case C_STRTAG:
704 case C_ENTAG:
705 case C_UNTAG:
706 /* Various definition */
707 i = do_define (i, top_scope);
708 break;
709 case C_EXT:
710 case C_LABEL:
711 i = do_define (i, file_scope);
712 break;
713 case C_STAT:
714 case C_TPDEF:
715 case C_AUTO:
716 case C_REG:
717 i = do_define (i, top_scope);
718 break;
719 default:
720 abort ();
721 case C_EOS:
722 i += sym->n_numaux + 1;
723 break;
726 do_sections_p2 (head);
727 return head;
730 struct coff_ofile *
731 coff_grok (inabfd)
732 bfd *inabfd;
734 long storage;
735 struct coff_ofile *p;
736 abfd = inabfd;
737 storage = bfd_get_symtab_upper_bound (abfd);
739 if (storage < 0)
740 bfd_fatal (abfd->filename);
742 syms = (asymbol **) xmalloc (storage);
743 symcount = bfd_canonicalize_symtab (abfd, syms);
744 if (symcount < 0)
745 bfd_fatal (abfd->filename);
746 rawsyms = obj_raw_syments (abfd);
747 rawcount = obj_raw_syment_count (abfd);;
748 tindex = (struct coff_symbol **) (xcalloc (sizeof (struct coff_symbol *), rawcount));
750 p = doit ();
751 return p;