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.
30 #include "libiberty.h"
33 #include "coff/internal.h"
34 #include "../bfd/libcoff.h"
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
;
53 #define N(x) ((x)->_n._n_nptr[1])
55 static struct coff_ptr_struct
*rawsyms
;
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
*
85 l
= (struct coff_scope
*) (xcalloc (sizeof (struct coff_scope
), 1));
89 static struct coff_symbol
*
92 return (struct coff_symbol
*) (xcalloc (sizeof (struct coff_symbol
), 1));
100 struct coff_scope
*n
= empty_scope ();
105 if (top_scope
->list_tail
)
107 top_scope
->list_tail
->next
= n
;
111 top_scope
->list_head
= n
;
113 top_scope
->list_tail
= n
;
116 n
->parent
= top_scope
;
124 top_scope
= top_scope
->parent
;
128 do_sections_p1 (head
)
129 struct coff_ofile
*head
;
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
++)
141 int i
= section
->target_index
;
145 relsize
= bfd_get_reloc_upper_bound (abfd
, section
);
147 bfd_fatal (bfd_get_filename (abfd
));
150 relpp
= (arelent
**) xmalloc (relsize
);
151 relcount
= bfd_canonicalize_reloc (abfd
, section
, relpp
, syms
);
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;
178 do_sections_p2 (head
)
179 struct coff_ofile
*head
;
182 for (section
= abfd
->sections
; section
; section
= section
->next
)
186 for (j
= 0; j
< section
->reloc_count
; j
++)
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
*
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)
212 switch (sym
->n_sclass
)
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
;
221 where
->where
= coff_where_member_of_enum
;
225 where
->where
= coff_where_member_of_struct
;
229 where
->where
= coff_where_stack
;
235 where
->where
= coff_where_memory
;
236 where
->section
= &ofile
->sections
[sym
->n_scnum
];
240 where
->where
= coff_where_register
;
243 where
->where
= coff_where_entag
;
247 where
->where
= coff_where_strtag
;
250 where
->where
= coff_where_typedef
;
263 char *name ATTRIBUTE_UNUSED
;
265 struct coff_line
*res
= (struct coff_line
*) xcalloc (sizeof (struct coff_line
), 1);
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 */
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
;
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 */
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
;
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
;
321 res
->type
= coff_basic_type
;
322 res
->u
.basic
= type
& 0xf;
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
;
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
;
344 /* Else it could be a function or pointer to void */
358 res
->size
= SHORT_SIZE
;
362 res
->size
= INT_SIZE
;
366 res
->size
= LONG_SIZE
;
369 res
->size
= FLOAT_SIZE
;
372 res
->size
= DOUBLE_SIZE
;
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;
388 /* A definition of a struct */
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
;
399 /* No auxents - it's anonynmous */
400 res
->type
= coff_structref_type
;
401 res
->u
.astructref
.ref
= 0;
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
;
415 /* A definition of an enum */
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
;
426 for (which_dt
= 5; which_dt
>= 0; which_dt
--)
428 switch ((type
>> ((which_dt
* 2) + 4)) & 0x3)
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
]
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
;
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
;
459 struct coff_type
*ptr
460 = (struct coff_type
*) xmalloc (sizeof (struct coff_type
));
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
;
476 static struct coff_visible
*
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
)
489 t
= coff_vis_member_of_struct
;
492 t
= coff_vis_member_of_enum
;
496 t
= coff_vis_regparam
;
500 t
= coff_vis_register
;
510 t
= coff_vis_autoparam
;
519 t
= coff_vis_int_def
;
522 if (sym
->n_scnum
== N_UNDEF
)
527 t
= coff_vis_ext_ref
;
530 t
= coff_vis_ext_def
;
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 */
558 if (ofile
->symbol_list_tail
)
559 ofile
->symbol_list_tail
->next_in_ofile_list
= s
;
561 ofile
->symbol_list_head
= s
;
562 ofile
->symbol_list_tail
= s
;
563 /* And the block list */
566 b
->vars_tail
->next
= s
;
572 s
->type
= do_type (i
);
573 s
->where
= do_where (i
);
574 s
->visible
= do_visible (i
);
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
;
587 is
->low
= s
->where
->offset
;
588 is
->high
= s
->where
->offset
+ s
->type
->size
;
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;
608 struct coff_ofile
*head
=
609 (struct coff_ofile
*) xmalloc (sizeof (struct coff_ofile
));
611 head
->source_head
= 0;
612 head
->source_tail
= 0;
614 head
->symbol_list_tail
= 0;
615 head
->symbol_list_head
= 0;
616 do_sections_p1 (head
);
619 for (i
= 0; i
< rawcount
;)
621 struct internal_syment
*sym
= &rawsyms
[i
].u
.syment
;
622 switch (sym
->n_sclass
)
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);
631 n
->name
= sym
->_n
._n_nptr
[1];
640 file_scope
= n
->scope
= top_scope
;
642 if (head
->source_tail
)
643 head
->source_tail
->next
= n
;
645 head
->source_head
= n
;
646 head
->source_tail
= n
;
648 i
+= sym
->n_numaux
+ 1;
653 char *name
= sym
->_n
._n_nptr
[1];
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
;
664 top_scope
->size
= sym
->n_value
- top_scope
->offset
+ 1;
668 i
+= sym
->n_numaux
+ 1;
674 char *name
= sym
->_n
._n_nptr
[1];
679 top_scope
->sec
= ofile
->sections
+ sym
->n_scnum
;
680 top_scope
->offset
= sym
->n_value
;
685 top_scope
->size
= sym
->n_value
- top_scope
->offset
+ 1;
688 i
+= sym
->n_numaux
+ 1;
693 i
= do_define (i
, last_function_symbol
->type
->u
.function
.parameters
);
698 i
= do_define (i
, last_struct
->u
.astructdef
.elements
);
701 i
= do_define (i
, last_enum
->u
.aenumdef
.elements
);
706 /* Various definition */
707 i
= do_define (i
, top_scope
);
711 i
= do_define (i
, file_scope
);
717 i
= do_define (i
, top_scope
);
722 i
+= sym
->n_numaux
+ 1;
726 do_sections_p2 (head
);
735 struct coff_ofile
*p
;
737 storage
= bfd_get_symtab_upper_bound (abfd
);
740 bfd_fatal (abfd
->filename
);
742 syms
= (asymbol
**) xmalloc (storage
);
743 symcount
= bfd_canonicalize_symtab (abfd
, syms
);
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
));