2 Copyright 1994, 1995, 1997, 1998, 2000, 2001 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.
29 #include <libiberty.h>
32 #include "coff/internal.h"
33 #include "../bfd/libcoff.h"
36 static struct coff_scope
*top_scope
;
37 static struct coff_scope
*file_scope
;
38 static struct coff_ofile
*ofile
;
40 struct coff_symbol
*last_function_symbol
;
41 struct coff_type
*last_function_type
;
42 struct coff_type
*last_struct
;
43 struct coff_type
*last_enum
;
44 struct coff_sfile
*cur_sfile
;
46 static struct coff_symbol
**tindex
;
49 static asymbol
**syms
;
52 #define N(x) ((x)->_n._n_nptr[1])
54 static struct coff_ptr_struct
*rawsyms
;
65 #define INDEXOF(p) ((struct coff_ptr_struct *)(p)-(rawsyms))
67 static struct coff_scope
*empty_scope
PARAMS ((void));
68 static struct coff_symbol
*empty_symbol
PARAMS ((void));
69 static void push_scope
PARAMS ((int));
70 static void pop_scope
PARAMS ((void));
71 static void do_sections_p1
PARAMS ((struct coff_ofile
*));
72 static void do_sections_p2
PARAMS ((struct coff_ofile
*));
73 static struct coff_where
*do_where
PARAMS ((int));
74 static struct coff_line
*do_lines
PARAMS ((int, char *));
75 static struct coff_type
*do_type
PARAMS ((int));
76 static struct coff_visible
*do_visible
PARAMS ((int));
77 static int do_define
PARAMS ((int, struct coff_scope
*));
78 static struct coff_ofile
*doit
PARAMS ((void));
80 static struct coff_scope
*
84 l
= (struct coff_scope
*) (xcalloc (sizeof (struct coff_scope
), 1));
88 static struct coff_symbol
*
91 return (struct coff_symbol
*) (xcalloc (sizeof (struct coff_symbol
), 1));
99 struct coff_scope
*n
= empty_scope ();
104 if (top_scope
->list_tail
)
106 top_scope
->list_tail
->next
= n
;
110 top_scope
->list_head
= n
;
112 top_scope
->list_tail
= n
;
115 n
->parent
= top_scope
;
123 top_scope
= top_scope
->parent
;
127 do_sections_p1 (head
)
128 struct coff_ofile
*head
;
132 struct coff_section
*all
= (struct coff_section
*) (xcalloc (abfd
->section_count
+ 1,
133 sizeof (struct coff_section
)));
134 head
->nsections
= abfd
->section_count
+ 1;
135 head
->sections
= all
;
137 for (idx
= 0, section
= abfd
->sections
; section
; section
= section
->next
, idx
++)
140 int i
= section
->target_index
;
144 relsize
= bfd_get_reloc_upper_bound (abfd
, section
);
146 bfd_fatal (bfd_get_filename (abfd
));
149 relpp
= (arelent
**) xmalloc (relsize
);
150 relcount
= bfd_canonicalize_reloc (abfd
, section
, relpp
, syms
);
152 bfd_fatal (bfd_get_filename (abfd
));
154 head
->sections
[i
].name
= (char *) (section
->name
);
155 head
->sections
[i
].code
= section
->flags
& SEC_CODE
;
156 head
->sections
[i
].data
= section
->flags
& SEC_DATA
;
157 if (strcmp (section
->name
, ".bss") == 0)
158 head
->sections
[i
].data
= 1;
159 head
->sections
[i
].address
= section
->lma
;
160 head
->sections
[i
].size
= section
->_raw_size
;
161 head
->sections
[i
].number
= idx
;
162 head
->sections
[i
].nrelocs
= section
->reloc_count
;
163 head
->sections
[i
].relocs
=
164 (struct coff_reloc
*) (xcalloc (section
->reloc_count
,
165 sizeof (struct coff_reloc
)));
166 head
->sections
[i
].bfd_section
= section
;
168 head
->sections
[0].name
= "ABSOLUTE";
169 head
->sections
[0].code
= 0;
170 head
->sections
[0].data
= 0;
171 head
->sections
[0].address
= 0;
172 head
->sections
[0].size
= 0;
173 head
->sections
[0].number
= 0;
177 do_sections_p2 (head
)
178 struct coff_ofile
*head
;
181 for (section
= abfd
->sections
; section
; section
= section
->next
)
185 for (j
= 0; j
< section
->reloc_count
; j
++)
188 int i
= section
->target_index
;
189 struct coff_reloc
*r
= head
->sections
[i
].relocs
+ j
;
190 arelent
*sr
= section
->relocation
+ j
;
191 r
->offset
= sr
->address
;
192 r
->addend
= sr
->addend
;
193 idx
= ((coff_symbol_type
*) (sr
->sym_ptr_ptr
[0]))->native
- rawsyms
;
194 r
->symbol
= tindex
[idx
];
199 static struct coff_where
*
203 struct internal_syment
*sym
= &rawsyms
[i
].u
.syment
;
204 struct coff_where
*where
=
205 (struct coff_where
*) (xmalloc (sizeof (struct coff_where
)));
206 where
->offset
= sym
->n_value
;
208 if (sym
->n_scnum
== -1)
211 switch (sym
->n_sclass
)
214 where
->where
= coff_where_member_of_struct
;
215 where
->offset
= sym
->n_value
/ 8;
216 where
->bitoffset
= sym
->n_value
% 8;
217 where
->bitsize
= rawsyms
[i
+ 1].u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_size
;
220 where
->where
= coff_where_member_of_enum
;
224 where
->where
= coff_where_member_of_struct
;
228 where
->where
= coff_where_stack
;
234 where
->where
= coff_where_memory
;
235 where
->section
= &ofile
->sections
[sym
->n_scnum
];
239 where
->where
= coff_where_register
;
242 where
->where
= coff_where_entag
;
246 where
->where
= coff_where_strtag
;
249 where
->where
= coff_where_typedef
;
262 char *name ATTRIBUTE_UNUSED
;
264 struct coff_line
*res
= (struct coff_line
*) xcalloc (sizeof (struct coff_line
), 1);
268 /* Find out if this function has any line numbers in the table */
269 for (s
= abfd
->sections
; s
; s
= s
->next
)
271 for (l
= 0; l
< s
->lineno_count
; l
++)
273 if (s
->lineno
[l
].line_number
== 0)
275 if (rawsyms
+ i
== ((coff_symbol_type
*) (&(s
->lineno
[l
].u
.sym
[0])))->native
)
277 /* These lines are for this function - so count them and stick them on */
279 /* Find the linenumber of the top of the function, since coff linenumbers
280 are relative to the start of the function. */
281 int start_line
= rawsyms
[i
+ 3].u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_lnno
;
284 for (c
= 0; s
->lineno
[l
+ c
+ 1].line_number
; c
++)
287 /* Add two extra records, one for the prologue and one for the epilogue */
290 res
->lines
= (int *) (xcalloc (sizeof (int), c
));
291 res
->addresses
= (int *) (xcalloc (sizeof (int), c
));
292 res
->lines
[0] = start_line
;
293 res
->addresses
[0] = rawsyms
[i
].u
.syment
.n_value
- s
->vma
;
294 for (c
= 0; s
->lineno
[l
+ c
+ 1].line_number
; c
++)
296 res
->lines
[c
+ 1] = s
->lineno
[l
+ c
].line_number
+ start_line
- 1;
297 res
->addresses
[c
+ 1] = s
->lineno
[l
+ c
].u
.offset
;
312 struct internal_syment
*sym
= &rawsyms
[i
].u
.syment
;
313 union internal_auxent
*aux
= &rawsyms
[i
+ 1].u
.auxent
;
314 struct coff_type
*res
=
315 (struct coff_type
*) xmalloc (sizeof (struct coff_type
));
316 int type
= sym
->n_type
;
320 res
->type
= coff_basic_type
;
321 res
->u
.basic
= type
& 0xf;
327 if (sym
->n_numaux
&& sym
->n_sclass
== C_STAT
)
329 /* This is probably a section definition */
330 res
->type
= coff_secdef_type
;
331 res
->size
= aux
->x_scn
.x_scnlen
;
337 /* Don't know what this is, let's make it a simple int */
338 res
->size
= INT_SIZE
;
339 res
->u
.basic
= T_UINT
;
343 /* Else it could be a function or pointer to void */
357 res
->size
= SHORT_SIZE
;
361 res
->size
= INT_SIZE
;
365 res
->size
= LONG_SIZE
;
368 res
->size
= FLOAT_SIZE
;
371 res
->size
= DOUBLE_SIZE
;
377 if (aux
->x_sym
.x_tagndx
.p
)
379 /* Refering to a struct defined elsewhere */
380 res
->type
= coff_structref_type
;
381 res
->u
.astructref
.ref
= tindex
[INDEXOF (aux
->x_sym
.x_tagndx
.p
)];
382 res
->size
= res
->u
.astructref
.ref
?
383 res
->u
.astructref
.ref
->type
->size
: 0;
387 /* A definition of a struct */
389 res
->type
= coff_structdef_type
;
390 res
->u
.astructdef
.elements
= empty_scope ();
391 res
->u
.astructdef
.idx
= 0;
392 res
->u
.astructdef
.isstruct
= (type
& 0xf) == T_STRUCT
;
393 res
->size
= aux
->x_sym
.x_misc
.x_lnsz
.x_size
;
398 /* No auxents - it's anonynmous */
399 res
->type
= coff_structref_type
;
400 res
->u
.astructref
.ref
= 0;
405 if (aux
->x_sym
.x_tagndx
.p
)
407 /* Refering to a enum defined elsewhere */
408 res
->type
= coff_enumref_type
;
409 res
->u
.aenumref
.ref
= tindex
[INDEXOF (aux
->x_sym
.x_tagndx
.p
)];
410 res
->size
= res
->u
.aenumref
.ref
->type
->size
;
414 /* A definition of an enum */
416 res
->type
= coff_enumdef_type
;
417 res
->u
.aenumdef
.elements
= empty_scope ();
418 res
->size
= aux
->x_sym
.x_misc
.x_lnsz
.x_size
;
425 for (which_dt
= 5; which_dt
>= 0; which_dt
--)
427 switch ((type
>> ((which_dt
* 2) + 4)) & 0x3)
433 struct coff_type
*ptr
= ((struct coff_type
*)
434 xmalloc (sizeof (struct coff_type
)));
435 int els
= (dimind
< DIMNUM
436 ? aux
->x_sym
.x_fcnary
.x_ary
.x_dimen
[dimind
]
439 ptr
->type
= coff_array_type
;
440 ptr
->size
= els
* res
->size
;
441 ptr
->u
.array
.dim
= els
;
442 ptr
->u
.array
.array_of
= res
;
448 struct coff_type
*ptr
=
449 (struct coff_type
*) xmalloc (sizeof (struct coff_type
));
450 ptr
->size
= PTR_SIZE
;
451 ptr
->type
= coff_pointer_type
;
452 ptr
->u
.pointer
.points_to
= res
;
458 struct coff_type
*ptr
459 = (struct coff_type
*) xmalloc (sizeof (struct coff_type
));
461 ptr
->type
= coff_function_type
;
462 ptr
->u
.function
.function_returns
= res
;
463 ptr
->u
.function
.parameters
= empty_scope ();
464 ptr
->u
.function
.lines
= do_lines (i
, sym
->_n
._n_nptr
[1]);
465 ptr
->u
.function
.code
= 0;
466 last_function_type
= ptr
;
475 static struct coff_visible
*
479 struct internal_syment
*sym
= &rawsyms
[i
].u
.syment
;
480 struct coff_visible
*visible
=
481 (struct coff_visible
*) (xmalloc (sizeof (struct coff_visible
)));
482 enum coff_vis_type t
;
483 switch (sym
->n_sclass
)
488 t
= coff_vis_member_of_struct
;
491 t
= coff_vis_member_of_enum
;
495 t
= coff_vis_regparam
;
499 t
= coff_vis_register
;
509 t
= coff_vis_autoparam
;
518 t
= coff_vis_int_def
;
521 if (sym
->n_scnum
== N_UNDEF
)
526 t
= coff_vis_ext_ref
;
529 t
= coff_vis_ext_def
;
543 struct coff_scope
*b
;
545 static int symbol_index
;
546 struct internal_syment
*sym
= &rawsyms
[i
].u
.syment
;
548 /* Define a symbol and attach to block b */
549 struct coff_symbol
*s
= empty_symbol ();
551 s
->number
= ++symbol_index
;
552 s
->name
= sym
->_n
._n_nptr
[1];
553 s
->sfile
= cur_sfile
;
554 /* Glue onto the ofile list */
557 if (ofile
->symbol_list_tail
)
558 ofile
->symbol_list_tail
->next_in_ofile_list
= s
;
560 ofile
->symbol_list_head
= s
;
561 ofile
->symbol_list_tail
= s
;
562 /* And the block list */
565 b
->vars_tail
->next
= s
;
571 s
->type
= do_type (i
);
572 s
->where
= do_where (i
);
573 s
->visible
= do_visible (i
);
577 /* We remember the lowest address in each section for each source file */
579 if (s
->where
->where
== coff_where_memory
580 && s
->type
->type
== coff_secdef_type
)
582 struct coff_isection
*is
= cur_sfile
->section
+ s
->where
->section
->number
;
586 is
->low
= s
->where
->offset
;
587 is
->high
= s
->where
->offset
+ s
->type
->size
;
589 is
->parent
= s
->where
->section
;
594 if (s
->type
->type
== coff_function_type
)
595 last_function_symbol
= s
;
597 return i
+ sym
->n_numaux
+ 1;
607 struct coff_ofile
*head
=
608 (struct coff_ofile
*) xmalloc (sizeof (struct coff_ofile
));
610 head
->source_head
= 0;
611 head
->source_tail
= 0;
613 head
->symbol_list_tail
= 0;
614 head
->symbol_list_head
= 0;
615 do_sections_p1 (head
);
618 for (i
= 0; i
< rawcount
;)
620 struct internal_syment
*sym
= &rawsyms
[i
].u
.syment
;
621 switch (sym
->n_sclass
)
625 /* new source file announced */
626 struct coff_sfile
*n
=
627 (struct coff_sfile
*) xmalloc (sizeof (struct coff_sfile
));
628 n
->section
= (struct coff_isection
*) xcalloc (sizeof (struct coff_isection
), abfd
->section_count
+ 1);
630 n
->name
= sym
->_n
._n_nptr
[1];
639 file_scope
= n
->scope
= top_scope
;
641 if (head
->source_tail
)
642 head
->source_tail
->next
= n
;
644 head
->source_head
= n
;
645 head
->source_tail
= n
;
647 i
+= sym
->n_numaux
+ 1;
652 char *name
= sym
->_n
._n_nptr
[1];
657 last_function_type
->u
.function
.code
= top_scope
;
658 top_scope
->sec
= ofile
->sections
+ sym
->n_scnum
;
659 top_scope
->offset
= sym
->n_value
;
663 top_scope
->size
= sym
->n_value
- top_scope
->offset
+ 1;
667 i
+= sym
->n_numaux
+ 1;
673 char *name
= sym
->_n
._n_nptr
[1];
678 top_scope
->sec
= ofile
->sections
+ sym
->n_scnum
;
679 top_scope
->offset
= sym
->n_value
;
684 top_scope
->size
= sym
->n_value
- top_scope
->offset
+ 1;
687 i
+= sym
->n_numaux
+ 1;
692 i
= do_define (i
, last_function_symbol
->type
->u
.function
.parameters
);
697 i
= do_define (i
, last_struct
->u
.astructdef
.elements
);
700 i
= do_define (i
, last_enum
->u
.aenumdef
.elements
);
705 /* Various definition */
706 i
= do_define (i
, top_scope
);
710 i
= do_define (i
, file_scope
);
716 i
= do_define (i
, top_scope
);
721 i
+= sym
->n_numaux
+ 1;
725 do_sections_p2 (head
);
734 struct coff_ofile
*p
;
736 storage
= bfd_get_symtab_upper_bound (abfd
);
739 bfd_fatal (abfd
->filename
);
741 syms
= (asymbol
**) xmalloc (storage
);
742 symcount
= bfd_canonicalize_symtab (abfd
, syms
);
744 bfd_fatal (abfd
->filename
);
745 rawsyms
= obj_raw_syments (abfd
);
746 rawcount
= obj_raw_syment_count (abfd
);;
747 tindex
= (struct coff_symbol
**) (xcalloc (sizeof (struct coff_symbol
*), rawcount
));