2 Copyright 1994, 1995, 1997, 1998, 2000, 2001, 2002, 2003, 2004, 2007
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 3 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,
20 MA 02110-1301, USA. */
23 /* Written by Steve Chamberlain (sac@cygnus.com)
25 This module reads a coff file and builds a really simple type tree
26 which can be read by other programs. The first application is a
27 coff->sysroff converter. It can be tested with coffdump.c. */
31 #include "libiberty.h"
33 #include "coff/internal.h"
34 #include "../bfd/libcoff.h"
38 static int lofile
= 1;
39 static struct coff_scope
*top_scope
;
40 static struct coff_scope
*file_scope
;
41 static struct coff_ofile
*ofile
;
43 static struct coff_symbol
*last_function_symbol
;
44 static struct coff_type
*last_function_type
;
45 static struct coff_type
*last_struct
;
46 static struct coff_type
*last_enum
;
47 static struct coff_sfile
*cur_sfile
;
49 static struct coff_symbol
**tindex
;
52 static asymbol
**syms
;
55 #define N(x) ((x)->_n._n_nptr[1])
57 static struct coff_ptr_struct
*rawsyms
;
68 #define INDEXOF(p) ((struct coff_ptr_struct *)(p)-(rawsyms))
70 static struct coff_scope
*empty_scope (void);
71 static struct coff_symbol
*empty_symbol (void);
72 static void push_scope (int);
73 static void pop_scope (void);
74 static void do_sections_p1 (struct coff_ofile
*);
75 static void do_sections_p2 (struct coff_ofile
*);
76 static struct coff_where
*do_where (int);
77 static struct coff_line
*do_lines (int, char *);
78 static struct coff_type
*do_type (int);
79 static struct coff_visible
*do_visible (int);
80 static int do_define (int, struct coff_scope
*);
81 static struct coff_ofile
*doit (void);
83 static struct coff_scope
*
87 l
= (struct coff_scope
*) (xcalloc (sizeof (struct coff_scope
), 1));
91 static struct coff_symbol
*
94 return (struct coff_symbol
*) (xcalloc (sizeof (struct coff_symbol
), 1));
101 struct coff_scope
*n
= empty_scope ();
106 if (top_scope
->list_tail
)
108 top_scope
->list_tail
->next
= n
;
112 top_scope
->list_head
= n
;
114 top_scope
->list_tail
= n
;
117 n
->parent
= top_scope
;
125 top_scope
= top_scope
->parent
;
129 do_sections_p1 (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
= bfd_get_section_size (section
);
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 (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
*
202 struct internal_syment
*sym
= &rawsyms
[i
].u
.syment
;
203 struct coff_where
*where
=
204 (struct coff_where
*) (xmalloc (sizeof (struct coff_where
)));
205 where
->offset
= sym
->n_value
;
207 if (sym
->n_scnum
== -1)
210 switch (sym
->n_sclass
)
213 where
->where
= coff_where_member_of_struct
;
214 where
->offset
= sym
->n_value
/ 8;
215 where
->bitoffset
= sym
->n_value
% 8;
216 where
->bitsize
= rawsyms
[i
+ 1].u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_size
;
219 where
->where
= coff_where_member_of_enum
;
223 where
->where
= coff_where_member_of_struct
;
227 where
->where
= coff_where_stack
;
233 where
->where
= coff_where_memory
;
234 where
->section
= &ofile
->sections
[sym
->n_scnum
];
238 where
->where
= coff_where_register
;
241 where
->where
= coff_where_entag
;
245 where
->where
= coff_where_strtag
;
248 where
->where
= coff_where_typedef
;
259 do_lines (int i
, char *name ATTRIBUTE_UNUSED
)
261 struct coff_line
*res
= (struct coff_line
*) xcalloc (sizeof (struct coff_line
), 1);
265 /* Find out if this function has any line numbers in the table */
266 for (s
= abfd
->sections
; s
; s
= s
->next
)
268 for (l
= 0; l
< s
->lineno_count
; l
++)
270 if (s
->lineno
[l
].line_number
== 0)
272 if (rawsyms
+ i
== ((coff_symbol_type
*) (&(s
->lineno
[l
].u
.sym
[0])))->native
)
274 /* These lines are for this function - so count them and stick them on */
276 /* Find the linenumber of the top of the function, since coff linenumbers
277 are relative to the start of the function. */
278 int start_line
= rawsyms
[i
+ 3].u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_lnno
;
281 for (c
= 0; s
->lineno
[l
+ c
+ 1].line_number
; c
++)
284 /* Add two extra records, one for the prologue and one for the epilogue */
287 res
->lines
= (int *) (xcalloc (sizeof (int), c
));
288 res
->addresses
= (int *) (xcalloc (sizeof (int), c
));
289 res
->lines
[0] = start_line
;
290 res
->addresses
[0] = rawsyms
[i
].u
.syment
.n_value
- s
->vma
;
291 for (c
= 0; s
->lineno
[l
+ c
+ 1].line_number
; c
++)
293 res
->lines
[c
+ 1] = s
->lineno
[l
+ c
].line_number
+ start_line
- 1;
294 res
->addresses
[c
+ 1] = s
->lineno
[l
+ c
].u
.offset
;
308 struct internal_syment
*sym
= &rawsyms
[i
].u
.syment
;
309 union internal_auxent
*aux
= &rawsyms
[i
+ 1].u
.auxent
;
310 struct coff_type
*res
=
311 (struct coff_type
*) xmalloc (sizeof (struct coff_type
));
312 int type
= sym
->n_type
;
316 res
->type
= coff_basic_type
;
317 res
->u
.basic
= type
& 0xf;
323 if (sym
->n_numaux
&& sym
->n_sclass
== C_STAT
)
325 /* This is probably a section definition */
326 res
->type
= coff_secdef_type
;
327 res
->size
= aux
->x_scn
.x_scnlen
;
333 /* Don't know what this is, let's make it a simple int */
334 res
->size
= INT_SIZE
;
335 res
->u
.basic
= T_UINT
;
339 /* Else it could be a function or pointer to void */
353 res
->size
= SHORT_SIZE
;
357 res
->size
= INT_SIZE
;
361 res
->size
= LONG_SIZE
;
364 res
->size
= FLOAT_SIZE
;
367 res
->size
= DOUBLE_SIZE
;
373 if (aux
->x_sym
.x_tagndx
.p
)
375 /* Referring to a struct defined elsewhere */
376 res
->type
= coff_structref_type
;
377 res
->u
.astructref
.ref
= tindex
[INDEXOF (aux
->x_sym
.x_tagndx
.p
)];
378 res
->size
= res
->u
.astructref
.ref
?
379 res
->u
.astructref
.ref
->type
->size
: 0;
383 /* A definition of a struct */
385 res
->type
= coff_structdef_type
;
386 res
->u
.astructdef
.elements
= empty_scope ();
387 res
->u
.astructdef
.idx
= 0;
388 res
->u
.astructdef
.isstruct
= (type
& 0xf) == T_STRUCT
;
389 res
->size
= aux
->x_sym
.x_misc
.x_lnsz
.x_size
;
394 /* No auxents - it's anonymous */
395 res
->type
= coff_structref_type
;
396 res
->u
.astructref
.ref
= 0;
401 if (aux
->x_sym
.x_tagndx
.p
)
403 /* Referring to a enum defined elsewhere */
404 res
->type
= coff_enumref_type
;
405 res
->u
.aenumref
.ref
= tindex
[INDEXOF (aux
->x_sym
.x_tagndx
.p
)];
406 res
->size
= res
->u
.aenumref
.ref
->type
->size
;
410 /* A definition of an enum */
412 res
->type
= coff_enumdef_type
;
413 res
->u
.aenumdef
.elements
= empty_scope ();
414 res
->size
= aux
->x_sym
.x_misc
.x_lnsz
.x_size
;
421 for (which_dt
= 5; which_dt
>= 0; which_dt
--)
423 switch ((type
>> ((which_dt
* 2) + 4)) & 0x3)
429 struct coff_type
*ptr
= ((struct coff_type
*)
430 xmalloc (sizeof (struct coff_type
)));
431 int els
= (dimind
< DIMNUM
432 ? aux
->x_sym
.x_fcnary
.x_ary
.x_dimen
[dimind
]
435 ptr
->type
= coff_array_type
;
436 ptr
->size
= els
* res
->size
;
437 ptr
->u
.array
.dim
= els
;
438 ptr
->u
.array
.array_of
= res
;
444 struct coff_type
*ptr
=
445 (struct coff_type
*) xmalloc (sizeof (struct coff_type
));
446 ptr
->size
= PTR_SIZE
;
447 ptr
->type
= coff_pointer_type
;
448 ptr
->u
.pointer
.points_to
= res
;
454 struct coff_type
*ptr
455 = (struct coff_type
*) xmalloc (sizeof (struct coff_type
));
457 ptr
->type
= coff_function_type
;
458 ptr
->u
.function
.function_returns
= res
;
459 ptr
->u
.function
.parameters
= empty_scope ();
460 ptr
->u
.function
.lines
= do_lines (i
, sym
->_n
._n_nptr
[1]);
461 ptr
->u
.function
.code
= 0;
462 last_function_type
= ptr
;
471 static struct coff_visible
*
474 struct internal_syment
*sym
= &rawsyms
[i
].u
.syment
;
475 struct coff_visible
*visible
=
476 (struct coff_visible
*) (xmalloc (sizeof (struct coff_visible
)));
477 enum coff_vis_type t
;
478 switch (sym
->n_sclass
)
483 t
= coff_vis_member_of_struct
;
486 t
= coff_vis_member_of_enum
;
490 t
= coff_vis_regparam
;
494 t
= coff_vis_register
;
504 t
= coff_vis_autoparam
;
513 t
= coff_vis_int_def
;
516 if (sym
->n_scnum
== N_UNDEF
)
521 t
= coff_vis_ext_ref
;
524 t
= coff_vis_ext_def
;
536 do_define (int i
, struct coff_scope
*b
)
538 static int symbol_index
;
539 struct internal_syment
*sym
= &rawsyms
[i
].u
.syment
;
541 /* Define a symbol and attach to block b */
542 struct coff_symbol
*s
= empty_symbol ();
544 s
->number
= ++symbol_index
;
545 s
->name
= sym
->_n
._n_nptr
[1];
546 s
->sfile
= cur_sfile
;
547 /* Glue onto the ofile list */
550 if (ofile
->symbol_list_tail
)
551 ofile
->symbol_list_tail
->next_in_ofile_list
= s
;
553 ofile
->symbol_list_head
= s
;
554 ofile
->symbol_list_tail
= s
;
555 /* And the block list */
558 b
->vars_tail
->next
= s
;
564 s
->type
= do_type (i
);
565 s
->where
= do_where (i
);
566 s
->visible
= do_visible (i
);
570 /* We remember the lowest address in each section for each source file */
572 if (s
->where
->where
== coff_where_memory
573 && s
->type
->type
== coff_secdef_type
)
575 struct coff_isection
*is
= cur_sfile
->section
+ s
->where
->section
->number
;
579 is
->low
= s
->where
->offset
;
580 is
->high
= s
->where
->offset
+ s
->type
->size
;
582 is
->parent
= s
->where
->section
;
587 if (s
->type
->type
== coff_function_type
)
588 last_function_symbol
= s
;
590 return i
+ sym
->n_numaux
+ 1;
600 struct coff_ofile
*head
=
601 (struct coff_ofile
*) xmalloc (sizeof (struct coff_ofile
));
603 head
->source_head
= 0;
604 head
->source_tail
= 0;
606 head
->symbol_list_tail
= 0;
607 head
->symbol_list_head
= 0;
608 do_sections_p1 (head
);
611 for (i
= 0; i
< rawcount
;)
613 struct internal_syment
*sym
= &rawsyms
[i
].u
.syment
;
614 switch (sym
->n_sclass
)
618 /* new source file announced */
619 struct coff_sfile
*n
=
620 (struct coff_sfile
*) xmalloc (sizeof (struct coff_sfile
));
621 n
->section
= (struct coff_isection
*) xcalloc (sizeof (struct coff_isection
), abfd
->section_count
+ 1);
623 n
->name
= sym
->_n
._n_nptr
[1];
632 file_scope
= n
->scope
= top_scope
;
634 if (head
->source_tail
)
635 head
->source_tail
->next
= n
;
637 head
->source_head
= n
;
638 head
->source_tail
= n
;
640 i
+= sym
->n_numaux
+ 1;
645 char *name
= sym
->_n
._n_nptr
[1];
650 last_function_type
->u
.function
.code
= top_scope
;
651 top_scope
->sec
= ofile
->sections
+ sym
->n_scnum
;
652 top_scope
->offset
= sym
->n_value
;
656 top_scope
->size
= sym
->n_value
- top_scope
->offset
+ 1;
660 i
+= sym
->n_numaux
+ 1;
666 char *name
= sym
->_n
._n_nptr
[1];
671 top_scope
->sec
= ofile
->sections
+ sym
->n_scnum
;
672 top_scope
->offset
= sym
->n_value
;
677 top_scope
->size
= sym
->n_value
- top_scope
->offset
+ 1;
680 i
+= sym
->n_numaux
+ 1;
685 i
= do_define (i
, last_function_symbol
->type
->u
.function
.parameters
);
690 i
= do_define (i
, last_struct
->u
.astructdef
.elements
);
693 i
= do_define (i
, last_enum
->u
.aenumdef
.elements
);
698 /* Various definition */
699 i
= do_define (i
, top_scope
);
703 i
= do_define (i
, file_scope
);
709 i
= do_define (i
, top_scope
);
714 i
+= sym
->n_numaux
+ 1;
718 do_sections_p2 (head
);
723 coff_grok (bfd
*inabfd
)
726 struct coff_ofile
*p
;
728 storage
= bfd_get_symtab_upper_bound (abfd
);
731 bfd_fatal (abfd
->filename
);
733 syms
= (asymbol
**) xmalloc (storage
);
734 symcount
= bfd_canonicalize_symtab (abfd
, syms
);
736 bfd_fatal (abfd
->filename
);
737 rawsyms
= obj_raw_syments (abfd
);
738 rawcount
= obj_raw_syment_count (abfd
);;
739 tindex
= (struct coff_symbol
**) (xcalloc (sizeof (struct coff_symbol
*), rawcount
));