* hppa-dis.c (print_insn_hppa): Don't print '%' before register names.
[binutils.git] / binutils / coffgrok.c
bloba6c05b14d2ab059e083267cada5cfe7f9c0f8a6d
1 /* coffgrok.c
2 Copyright 1994, 1995, 1997, 1998, 2000, 2001, 2002, 2003, 2004
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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, 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 static int lofile = 1;
37 static struct coff_scope *top_scope;
38 static struct coff_scope *file_scope;
39 static struct coff_ofile *ofile;
41 static struct coff_symbol *last_function_symbol;
42 static struct coff_type *last_function_type;
43 static struct coff_type *last_struct;
44 static struct coff_type *last_enum;
45 static 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 (void);
69 static struct coff_symbol *empty_symbol (void);
70 static void push_scope (int);
71 static void pop_scope (void);
72 static void do_sections_p1 (struct coff_ofile *);
73 static void do_sections_p2 (struct coff_ofile *);
74 static struct coff_where *do_where (int);
75 static struct coff_line *do_lines (int, char *);
76 static struct coff_type *do_type (int);
77 static struct coff_visible *do_visible (int);
78 static int do_define (int, struct coff_scope *);
79 static struct coff_ofile *doit (void);
81 static struct coff_scope *
82 empty_scope (void)
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 (void)
92 return (struct coff_symbol *) (xcalloc (sizeof (struct coff_symbol), 1));
95 /*int l;*/
96 static void
97 push_scope (int link)
99 struct coff_scope *n = empty_scope ();
100 if (link)
102 if (top_scope)
104 if (top_scope->list_tail)
106 top_scope->list_tail->next = n;
108 else
110 top_scope->list_head = n;
112 top_scope->list_tail = n;
115 n->parent = top_scope;
117 top_scope = n;
120 static void
121 pop_scope (void)
123 top_scope = top_scope->parent;
126 static void
127 do_sections_p1 (struct coff_ofile *head)
129 asection *section;
130 int idx;
131 struct coff_section *all = (struct coff_section *) (xcalloc (abfd->section_count + 1,
132 sizeof (struct coff_section)));
133 head->nsections = abfd->section_count + 1;
134 head->sections = all;
136 for (idx = 0, section = abfd->sections; section; section = section->next, idx++)
138 long relsize;
139 int i = section->target_index;
140 arelent **relpp;
141 long relcount;
143 relsize = bfd_get_reloc_upper_bound (abfd, section);
144 if (relsize < 0)
145 bfd_fatal (bfd_get_filename (abfd));
146 if (relsize == 0)
147 continue;
148 relpp = (arelent **) xmalloc (relsize);
149 relcount = bfd_canonicalize_reloc (abfd, section, relpp, syms);
150 if (relcount < 0)
151 bfd_fatal (bfd_get_filename (abfd));
153 head->sections[i].name = (char *) (section->name);
154 head->sections[i].code = section->flags & SEC_CODE;
155 head->sections[i].data = section->flags & SEC_DATA;
156 if (strcmp (section->name, ".bss") == 0)
157 head->sections[i].data = 1;
158 head->sections[i].address = section->lma;
159 head->sections[i].size = bfd_get_section_size (section);
160 head->sections[i].number = idx;
161 head->sections[i].nrelocs = section->reloc_count;
162 head->sections[i].relocs =
163 (struct coff_reloc *) (xcalloc (section->reloc_count,
164 sizeof (struct coff_reloc)));
165 head->sections[i].bfd_section = section;
167 head->sections[0].name = "ABSOLUTE";
168 head->sections[0].code = 0;
169 head->sections[0].data = 0;
170 head->sections[0].address = 0;
171 head->sections[0].size = 0;
172 head->sections[0].number = 0;
175 static void
176 do_sections_p2 (struct coff_ofile *head)
178 asection *section;
179 for (section = abfd->sections; section; section = section->next)
181 unsigned int j;
183 for (j = 0; j < section->reloc_count; j++)
185 int idx;
186 int i = section->target_index;
187 struct coff_reloc *r = head->sections[i].relocs + j;
188 arelent *sr = section->relocation + j;
189 r->offset = sr->address;
190 r->addend = sr->addend;
191 idx = ((coff_symbol_type *) (sr->sym_ptr_ptr[0]))->native - rawsyms;
192 r->symbol = tindex[idx];
197 static struct coff_where *
198 do_where (int i)
200 struct internal_syment *sym = &rawsyms[i].u.syment;
201 struct coff_where *where =
202 (struct coff_where *) (xmalloc (sizeof (struct coff_where)));
203 where->offset = sym->n_value;
205 if (sym->n_scnum == -1)
206 sym->n_scnum = 0;
208 switch (sym->n_sclass)
210 case C_FIELD:
211 where->where = coff_where_member_of_struct;
212 where->offset = sym->n_value / 8;
213 where->bitoffset = sym->n_value % 8;
214 where->bitsize = rawsyms[i + 1].u.auxent.x_sym.x_misc.x_lnsz.x_size;
215 break;
216 case C_MOE:
217 where->where = coff_where_member_of_enum;
218 break;
219 case C_MOS:
220 case C_MOU:
221 where->where = coff_where_member_of_struct;
222 break;
223 case C_AUTO:
224 case C_ARG:
225 where->where = coff_where_stack;
226 break;
227 case C_EXT:
228 case C_STAT:
229 case C_EXTDEF:
230 case C_LABEL:
231 where->where = coff_where_memory;
232 where->section = &ofile->sections[sym->n_scnum];
233 break;
234 case C_REG:
235 case C_REGPARM:
236 where->where = coff_where_register;
237 break;
238 case C_ENTAG:
239 where->where = coff_where_entag;
240 break;
241 case C_STRTAG:
242 case C_UNTAG:
243 where->where = coff_where_strtag;
244 break;
245 case C_TPDEF:
246 where->where = coff_where_typedef;
247 break;
248 default:
249 abort ();
250 break;
252 return where;
255 static
256 struct coff_line *
257 do_lines (int i, char *name ATTRIBUTE_UNUSED)
259 struct coff_line *res = (struct coff_line *) xcalloc (sizeof (struct coff_line), 1);
260 asection *s;
261 unsigned int l;
263 /* Find out if this function has any line numbers in the table */
264 for (s = abfd->sections; s; s = s->next)
266 for (l = 0; l < s->lineno_count; l++)
268 if (s->lineno[l].line_number == 0)
270 if (rawsyms + i == ((coff_symbol_type *) (&(s->lineno[l].u.sym[0])))->native)
272 /* These lines are for this function - so count them and stick them on */
273 int c = 0;
274 /* Find the linenumber of the top of the function, since coff linenumbers
275 are relative to the start of the function. */
276 int start_line = rawsyms[i + 3].u.auxent.x_sym.x_misc.x_lnsz.x_lnno;
278 l++;
279 for (c = 0; s->lineno[l + c + 1].line_number; c++)
282 /* Add two extra records, one for the prologue and one for the epilogue */
283 c += 1;
284 res->nlines = c;
285 res->lines = (int *) (xcalloc (sizeof (int), c));
286 res->addresses = (int *) (xcalloc (sizeof (int), c));
287 res->lines[0] = start_line;
288 res->addresses[0] = rawsyms[i].u.syment.n_value - s->vma;
289 for (c = 0; s->lineno[l + c + 1].line_number; c++)
291 res->lines[c + 1] = s->lineno[l + c].line_number + start_line - 1;
292 res->addresses[c + 1] = s->lineno[l + c].u.offset;
294 return res;
299 return res;
302 static
303 struct coff_type *
304 do_type (int i)
306 struct internal_syment *sym = &rawsyms[i].u.syment;
307 union internal_auxent *aux = &rawsyms[i + 1].u.auxent;
308 struct coff_type *res =
309 (struct coff_type *) xmalloc (sizeof (struct coff_type));
310 int type = sym->n_type;
311 int which_dt = 0;
312 int dimind = 0;
314 res->type = coff_basic_type;
315 res->u.basic = type & 0xf;
317 switch (type & 0xf)
319 case T_NULL:
320 case T_VOID:
321 if (sym->n_numaux && sym->n_sclass == C_STAT)
323 /* This is probably a section definition */
324 res->type = coff_secdef_type;
325 res->size = aux->x_scn.x_scnlen;
327 else
329 if (type == 0)
331 /* Don't know what this is, let's make it a simple int */
332 res->size = INT_SIZE;
333 res->u.basic = T_UINT;
335 else
337 /* Else it could be a function or pointer to void */
338 res->size = 0;
341 break;
344 break;
345 case T_UCHAR:
346 case T_CHAR:
347 res->size = 1;
348 break;
349 case T_USHORT:
350 case T_SHORT:
351 res->size = SHORT_SIZE;
352 break;
353 case T_UINT:
354 case T_INT:
355 res->size = INT_SIZE;
356 break;
357 case T_ULONG:
358 case T_LONG:
359 res->size = LONG_SIZE;
360 break;
361 case T_FLOAT:
362 res->size = FLOAT_SIZE;
363 break;
364 case T_DOUBLE:
365 res->size = DOUBLE_SIZE;
366 break;
367 case T_STRUCT:
368 case T_UNION:
369 if (sym->n_numaux)
371 if (aux->x_sym.x_tagndx.p)
373 /* Referring to a struct defined elsewhere */
374 res->type = coff_structref_type;
375 res->u.astructref.ref = tindex[INDEXOF (aux->x_sym.x_tagndx.p)];
376 res->size = res->u.astructref.ref ?
377 res->u.astructref.ref->type->size : 0;
379 else
381 /* A definition of a struct */
382 last_struct = res;
383 res->type = coff_structdef_type;
384 res->u.astructdef.elements = empty_scope ();
385 res->u.astructdef.idx = 0;
386 res->u.astructdef.isstruct = (type & 0xf) == T_STRUCT;
387 res->size = aux->x_sym.x_misc.x_lnsz.x_size;
390 else
392 /* No auxents - it's anonymous */
393 res->type = coff_structref_type;
394 res->u.astructref.ref = 0;
395 res->size = 0;
397 break;
398 case T_ENUM:
399 if (aux->x_sym.x_tagndx.p)
401 /* Referring to a enum defined elsewhere */
402 res->type = coff_enumref_type;
403 res->u.aenumref.ref = tindex[INDEXOF (aux->x_sym.x_tagndx.p)];
404 res->size = res->u.aenumref.ref->type->size;
406 else
408 /* A definition of an enum */
409 last_enum = res;
410 res->type = coff_enumdef_type;
411 res->u.aenumdef.elements = empty_scope ();
412 res->size = aux->x_sym.x_misc.x_lnsz.x_size;
414 break;
415 case T_MOE:
416 break;
419 for (which_dt = 5; which_dt >= 0; which_dt--)
421 switch ((type >> ((which_dt * 2) + 4)) & 0x3)
423 case 0:
424 break;
425 case DT_ARY:
427 struct coff_type *ptr = ((struct coff_type *)
428 xmalloc (sizeof (struct coff_type)));
429 int els = (dimind < DIMNUM
430 ? aux->x_sym.x_fcnary.x_ary.x_dimen[dimind]
431 : 0);
432 ++dimind;
433 ptr->type = coff_array_type;
434 ptr->size = els * res->size;
435 ptr->u.array.dim = els;
436 ptr->u.array.array_of = res;
437 res = ptr;
438 break;
440 case DT_PTR:
442 struct coff_type *ptr =
443 (struct coff_type *) xmalloc (sizeof (struct coff_type));
444 ptr->size = PTR_SIZE;
445 ptr->type = coff_pointer_type;
446 ptr->u.pointer.points_to = res;
447 res = ptr;
448 break;
450 case DT_FCN:
452 struct coff_type *ptr
453 = (struct coff_type *) xmalloc (sizeof (struct coff_type));
454 ptr->size = 0;
455 ptr->type = coff_function_type;
456 ptr->u.function.function_returns = res;
457 ptr->u.function.parameters = empty_scope ();
458 ptr->u.function.lines = do_lines (i, sym->_n._n_nptr[1]);
459 ptr->u.function.code = 0;
460 last_function_type = ptr;
461 res = ptr;
462 break;
466 return res;
469 static struct coff_visible *
470 do_visible (int i)
472 struct internal_syment *sym = &rawsyms[i].u.syment;
473 struct coff_visible *visible =
474 (struct coff_visible *) (xmalloc (sizeof (struct coff_visible)));
475 enum coff_vis_type t;
476 switch (sym->n_sclass)
478 case C_MOS:
479 case C_MOU:
480 case C_FIELD:
481 t = coff_vis_member_of_struct;
482 break;
483 case C_MOE:
484 t = coff_vis_member_of_enum;
485 break;
487 case C_REGPARM:
488 t = coff_vis_regparam;
489 break;
491 case C_REG:
492 t = coff_vis_register;
493 break;
494 case C_STRTAG:
495 case C_UNTAG:
496 case C_ENTAG:
497 case C_TPDEF:
498 t = coff_vis_tag;
499 break;
500 case C_AUTOARG:
501 case C_ARG:
502 t = coff_vis_autoparam;
503 break;
504 case C_AUTO:
507 t = coff_vis_auto;
508 break;
509 case C_LABEL:
510 case C_STAT:
511 t = coff_vis_int_def;
512 break;
513 case C_EXT:
514 if (sym->n_scnum == N_UNDEF)
516 if (sym->n_value)
517 t = coff_vis_common;
518 else
519 t = coff_vis_ext_ref;
521 else
522 t = coff_vis_ext_def;
523 break;
524 default:
525 abort ();
526 break;
529 visible->type = t;
530 return visible;
533 static int
534 do_define (int i, struct coff_scope *b)
536 static int symbol_index;
537 struct internal_syment *sym = &rawsyms[i].u.syment;
539 /* Define a symbol and attach to block b */
540 struct coff_symbol *s = empty_symbol ();
542 s->number = ++symbol_index;
543 s->name = sym->_n._n_nptr[1];
544 s->sfile = cur_sfile;
545 /* Glue onto the ofile list */
546 if (lofile >= 0)
548 if (ofile->symbol_list_tail)
549 ofile->symbol_list_tail->next_in_ofile_list = s;
550 else
551 ofile->symbol_list_head = s;
552 ofile->symbol_list_tail = s;
553 /* And the block list */
555 if (b->vars_tail)
556 b->vars_tail->next = s;
557 else
558 b->vars_head = s;
560 b->vars_tail = s;
561 b->nvars++;
562 s->type = do_type (i);
563 s->where = do_where (i);
564 s->visible = do_visible (i);
566 tindex[i] = s;
568 /* We remember the lowest address in each section for each source file */
570 if (s->where->where == coff_where_memory
571 && s->type->type == coff_secdef_type)
573 struct coff_isection *is = cur_sfile->section + s->where->section->number;
575 if (!is->init)
577 is->low = s->where->offset;
578 is->high = s->where->offset + s->type->size;
579 is->init = 1;
580 is->parent = s->where->section;
585 if (s->type->type == coff_function_type)
586 last_function_symbol = s;
588 return i + sym->n_numaux + 1;
592 static
593 struct coff_ofile *
594 doit (void)
596 int i;
597 int infile = 0;
598 struct coff_ofile *head =
599 (struct coff_ofile *) xmalloc (sizeof (struct coff_ofile));
600 ofile = head;
601 head->source_head = 0;
602 head->source_tail = 0;
603 head->nsources = 0;
604 head->symbol_list_tail = 0;
605 head->symbol_list_head = 0;
606 do_sections_p1 (head);
607 push_scope (1);
609 for (i = 0; i < rawcount;)
611 struct internal_syment *sym = &rawsyms[i].u.syment;
612 switch (sym->n_sclass)
614 case C_FILE:
616 /* new source file announced */
617 struct coff_sfile *n =
618 (struct coff_sfile *) xmalloc (sizeof (struct coff_sfile));
619 n->section = (struct coff_isection *) xcalloc (sizeof (struct coff_isection), abfd->section_count + 1);
620 cur_sfile = n;
621 n->name = sym->_n._n_nptr[1];
622 n->next = 0;
624 if (infile)
626 pop_scope ();
628 infile = 1;
629 push_scope (1);
630 file_scope = n->scope = top_scope;
632 if (head->source_tail)
633 head->source_tail->next = n;
634 else
635 head->source_head = n;
636 head->source_tail = n;
637 head->nsources++;
638 i += sym->n_numaux + 1;
640 break;
641 case C_FCN:
643 char *name = sym->_n._n_nptr[1];
644 if (name[1] == 'b')
646 /* Function start */
647 push_scope (0);
648 last_function_type->u.function.code = top_scope;
649 top_scope->sec = ofile->sections + sym->n_scnum;
650 top_scope->offset = sym->n_value;
652 else
654 top_scope->size = sym->n_value - top_scope->offset + 1;
655 pop_scope ();
658 i += sym->n_numaux + 1;
660 break;
662 case C_BLOCK:
664 char *name = sym->_n._n_nptr[1];
665 if (name[1] == 'b')
667 /* Block start */
668 push_scope (1);
669 top_scope->sec = ofile->sections + sym->n_scnum;
670 top_scope->offset = sym->n_value;
673 else
675 top_scope->size = sym->n_value - top_scope->offset + 1;
676 pop_scope ();
678 i += sym->n_numaux + 1;
680 break;
681 case C_REGPARM:
682 case C_ARG:
683 i = do_define (i, last_function_symbol->type->u.function.parameters);
684 break;
685 case C_MOS:
686 case C_MOU:
687 case C_FIELD:
688 i = do_define (i, last_struct->u.astructdef.elements);
689 break;
690 case C_MOE:
691 i = do_define (i, last_enum->u.aenumdef.elements);
692 break;
693 case C_STRTAG:
694 case C_ENTAG:
695 case C_UNTAG:
696 /* Various definition */
697 i = do_define (i, top_scope);
698 break;
699 case C_EXT:
700 case C_LABEL:
701 i = do_define (i, file_scope);
702 break;
703 case C_STAT:
704 case C_TPDEF:
705 case C_AUTO:
706 case C_REG:
707 i = do_define (i, top_scope);
708 break;
709 default:
710 abort ();
711 case C_EOS:
712 i += sym->n_numaux + 1;
713 break;
716 do_sections_p2 (head);
717 return head;
720 struct coff_ofile *
721 coff_grok (bfd *inabfd)
723 long storage;
724 struct coff_ofile *p;
725 abfd = inabfd;
726 storage = bfd_get_symtab_upper_bound (abfd);
728 if (storage < 0)
729 bfd_fatal (abfd->filename);
731 syms = (asymbol **) xmalloc (storage);
732 symcount = bfd_canonicalize_symtab (abfd, syms);
733 if (symcount < 0)
734 bfd_fatal (abfd->filename);
735 rawsyms = obj_raw_syments (abfd);
736 rawcount = obj_raw_syment_count (abfd);;
737 tindex = (struct coff_symbol **) (xcalloc (sizeof (struct coff_symbol *), rawcount));
739 p = doit ();
740 return p;