5 Copyright (C) 1994 Free Software Foundation, Inc.
7 This file is part of GNU Binutils.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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.
34 #include "coff/internal.h"
35 #include "../bfd/libcoff.h"
38 static struct coff_scope
*top_scope
;
39 static struct coff_scope
*file_scope
;
40 static struct coff_ofile
*ofile
;
42 struct coff_symbol
*last_function_symbol
;
43 struct coff_type
*last_function_type
;
44 struct coff_type
*last_struct
;
45 struct coff_type
*last_enum
;
46 struct coff_sfile
*cur_sfile
;
48 static struct coff_symbol
**tindex
;
51 static asymbol
**syms
;
54 #define N(x) ((x)->_n._n_nptr[1])
56 static struct coff_ptr_struct
*rawsyms
;
59 extern char *xcalloc ();
60 extern char *xmalloc ();
68 #define INDEXOF(p) ((struct coff_ptr_struct *)(p)-(rawsyms))
70 static struct coff_scope
*
74 l
= (struct coff_scope
*) (xcalloc (sizeof (struct coff_scope
), 1));
78 static struct coff_symbol
*
81 return (struct coff_symbol
*) (xcalloc (sizeof (struct coff_symbol
), 1));
88 struct coff_scope
*n
= empty_scope ();
93 if (top_scope
->list_tail
)
95 top_scope
->list_tail
->next
= n
;
99 top_scope
->list_head
= n
;
101 top_scope
->list_tail
= n
;
104 n
->parent
= top_scope
;
112 top_scope
= top_scope
->parent
;
116 do_sections_p1 (head
)
117 struct coff_ofile
*head
;
121 struct coff_section
*all
= (struct coff_section
*) (xcalloc (abfd
->section_count
+ 1,
122 sizeof (struct coff_section
)));
123 head
->nsections
= abfd
->section_count
+ 1;
124 head
->sections
= all
;
126 for (idx
= 0, section
= abfd
->sections
; section
; section
= section
->next
, idx
++)
129 int i
= section
->target_index
;
133 relsize
= bfd_get_reloc_upper_bound (abfd
, section
);
135 bfd_fatal (bfd_get_filename (abfd
));
136 relpp
= (arelent
**) xmalloc (relsize
);
137 relcount
= bfd_canonicalize_reloc (abfd
, section
, relpp
, syms
);
139 bfd_fatal (bfd_get_filename (abfd
));
141 head
->sections
[i
].name
= (char *) (section
->name
);
142 head
->sections
[i
].code
= section
->flags
& SEC_CODE
;
143 head
->sections
[i
].data
= section
->flags
& SEC_DATA
;
144 if (strcmp (section
->name
, ".bss") == 0)
145 head
->sections
[i
].data
= 1;
146 head
->sections
[i
].address
= section
->vma
;
147 head
->sections
[i
].size
= section
->_raw_size
;
148 head
->sections
[i
].number
= idx
;
149 head
->sections
[i
].nrelocs
= section
->reloc_count
;
150 head
->sections
[i
].relocs
=
151 (struct coff_reloc
*) (xcalloc (section
->reloc_count
,
152 sizeof (struct coff_reloc
)));
153 head
->sections
[i
].bfd_section
= section
;
155 head
->sections
[0].name
= "ABSOLUTE";
156 head
->sections
[0].code
= 0;
157 head
->sections
[0].data
= 0;
158 head
->sections
[0].address
= 0;
159 head
->sections
[0].size
= 0;
160 head
->sections
[0].number
= 0;
164 do_sections_p2 (head
)
165 struct coff_ofile
*head
;
168 for (section
= abfd
->sections
; section
; section
= section
->next
)
171 for (j
= 0; j
< section
->reloc_count
; j
++)
174 int i
= section
->target_index
;
175 struct coff_reloc
*r
= head
->sections
[i
].relocs
+ j
;
176 arelent
*sr
= section
->relocation
+ j
;
177 r
->offset
= sr
->address
;
178 r
->addend
= sr
->addend
;
179 idx
= ((coff_symbol_type
*) (sr
->sym_ptr_ptr
[0]))->native
- rawsyms
;
180 r
->symbol
= tindex
[idx
];
185 static struct coff_where
*
189 struct internal_syment
*sym
= &rawsyms
[i
].u
.syment
;
190 struct coff_where
*where
191 = (struct coff_where
*) (malloc (sizeof (struct coff_where
)));
192 where
->offset
= sym
->n_value
;
194 if (sym
->n_scnum
== -1)
197 switch (sym
->n_sclass
)
200 where
->where
= coff_where_member_of_struct
;
201 where
->offset
= sym
->n_value
/ 8;
202 where
->bitoffset
= sym
->n_value
% 8;
203 where
->bitsize
= rawsyms
[i
+ 1].u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_size
;
206 where
->where
= coff_where_member_of_enum
;
210 where
->where
= coff_where_member_of_struct
;
214 where
->where
= coff_where_stack
;
220 where
->where
= coff_where_memory
;
221 where
->section
= &ofile
->sections
[sym
->n_scnum
];
225 where
->where
= coff_where_register
;
228 where
->where
= coff_where_entag
;
232 where
->where
= coff_where_strtag
;
235 where
->where
= coff_where_typedef
;
250 struct coff_line
*res
= (struct coff_line
*) xcalloc (sizeof (struct coff_line
), 1);
253 /* Find out if this function has any line numbers in the table */
254 for (s
= abfd
->sections
; s
; s
= s
->next
)
256 for (l
= 0; l
< s
->lineno_count
; l
++)
258 if (s
->lineno
[l
].line_number
== 0)
260 if (rawsyms
+ i
== ((coff_symbol_type
*) (&(s
->lineno
[l
].u
.sym
[0])))->native
)
262 /* These lines are for this function - so count them and stick them on */
264 /* Find the linenumber of the top of the function, since coff linenumbers
265 are relative to the start of the function. */
266 int start_line
= rawsyms
[i
+ 3].u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_lnno
;
269 for (c
= 0; s
->lineno
[l
+ c
+ 1].line_number
; c
++)
272 /* Add two extra records, one for the prologue and one for the epilogue */
275 res
->lines
= (int *) (xcalloc (sizeof (int), c
));
276 res
->addresses
= (int *) (xcalloc (sizeof (int), c
));
277 res
->lines
[0] = start_line
;
278 res
->addresses
[0] = rawsyms
[i
].u
.syment
.n_value
- s
->vma
;
279 for (c
= 0; s
->lineno
[l
+ c
+ 1].line_number
; c
++)
281 res
->lines
[c
+ 1] = s
->lineno
[l
+ c
].line_number
+ start_line
- 1;
282 res
->addresses
[c
+ 1] = s
->lineno
[l
+ c
].u
.offset
;
297 struct internal_syment
*sym
= &rawsyms
[i
].u
.syment
;
298 union internal_auxent
*aux
= &rawsyms
[i
+ 1].u
.auxent
;
299 struct coff_type
*res
= (struct coff_type
*) malloc (sizeof (struct coff_type
));
300 int type
= sym
->n_type
;
303 res
->type
= coff_basic_type
;
304 res
->u
.basic
= type
& 0xf;
310 if (sym
->n_numaux
&& sym
->n_sclass
== C_STAT
)
312 /* This is probably a section definition */
313 res
->type
= coff_secdef_type
;
314 res
->size
= aux
->x_scn
.x_scnlen
;
320 /* Don't know what this is, let's make it a simple int */
321 res
->size
= INT_SIZE
;
322 res
->u
.basic
= T_UINT
;
326 /* Else it could be a function or pointer to void */
340 res
->size
= SHORT_SIZE
;
344 res
->size
= INT_SIZE
;
348 res
->size
= LONG_SIZE
;
351 res
->size
= FLOAT_SIZE
;
354 res
->size
= DOUBLE_SIZE
;
360 if (aux
->x_sym
.x_tagndx
.p
)
362 /* Refering to a struct defined elsewhere */
363 res
->type
= coff_structref_type
;
364 res
->u
.astructref
.ref
= tindex
[INDEXOF (aux
->x_sym
.x_tagndx
.p
)];
365 res
->size
= res
->u
.astructref
.ref
?
366 res
->u
.astructref
.ref
->type
->size
: 0;
370 /* A definition of a struct */
372 res
->type
= coff_structdef_type
;
373 res
->u
.astructdef
.elements
= empty_scope ();
374 res
->u
.astructdef
.isstruct
= (type
& 0xf) == T_STRUCT
;
375 res
->size
= aux
->x_sym
.x_misc
.x_lnsz
.x_size
;
380 /* No auxents - it's anonynmous */
381 res
->type
= coff_structref_type
;
382 res
->u
.astructref
.ref
= 0;
387 if (aux
->x_sym
.x_tagndx
.p
)
389 /* Refering to a enum defined elsewhere */
390 res
->type
= coff_enumref_type
;
391 res
->u
.aenumref
.ref
= tindex
[INDEXOF (aux
->x_sym
.x_tagndx
.p
)];
392 res
->size
= res
->u
.aenumref
.ref
->type
->size
;
396 /* A definition of an enum */
398 res
->type
= coff_enumdef_type
;
399 res
->u
.aenumdef
.elements
= empty_scope ();
400 res
->size
= aux
->x_sym
.x_misc
.x_lnsz
.x_size
;
407 for (which_dt
= 5; which_dt
>= 0; which_dt
--)
409 switch ((type
>> ((which_dt
* 2) + 4)) & 0x3)
416 /* Look in the auxent for the dimensions and build lots of dims */
417 int els
= aux
->x_sym
.x_fcnary
.x_ary
.x_dimen
[which_dt
];
420 struct coff_type
*ptr
= (struct coff_type
*) malloc (sizeof (struct coff_type
));
421 ptr
->type
= coff_array_type
;
422 ptr
->size
= els
* res
->size
;
423 ptr
->u
.array
.dim
= els
;
424 ptr
->u
.array
.array_of
= res
;
432 struct coff_type
*ptr
= (struct coff_type
*) malloc (sizeof (struct coff_type
));
433 ptr
->size
= PTR_SIZE
;
434 ptr
->type
= coff_pointer_type
;
435 ptr
->u
.pointer
.points_to
= res
;
441 struct coff_type
*ptr
= (struct coff_type
*) malloc (sizeof (struct coff_type
));
443 ptr
->type
= coff_function_type
;
444 ptr
->u
.function
.function_returns
= res
;
445 ptr
->u
.function
.parameters
= empty_scope ();
446 ptr
->u
.function
.lines
= do_lines (i
, sym
->_n
._n_nptr
[1]);
447 ptr
->u
.function
.code
= 0;
448 last_function_type
= ptr
;
457 static struct coff_visible
*
461 struct internal_syment
*sym
= &rawsyms
[i
].u
.syment
;
462 struct coff_visible
*visible
= (struct coff_visible
*) (malloc (sizeof (struct coff_visible
)));
463 enum coff_vis_type t
;
464 switch (sym
->n_sclass
)
469 t
= coff_vis_member_of_struct
;
472 t
= coff_vis_member_of_enum
;
476 t
= coff_vis_regparam
;
480 t
= coff_vis_register
;
490 t
= coff_vis_autoparam
;
499 t
= coff_vis_int_def
;
502 if (sym
->n_scnum
== N_UNDEF
)
507 t
= coff_vis_ext_ref
;
510 t
= coff_vis_ext_def
;
524 struct coff_scope
*b
;
526 static int symbol_index
;
527 struct internal_syment
*sym
= &rawsyms
[i
].u
.syment
;
529 /* Define a symbol and attach to block b */
530 struct coff_symbol
*s
= empty_symbol ();
532 s
->number
= ++symbol_index
;
533 s
->name
= sym
->_n
._n_nptr
[1];
534 s
->sfile
= cur_sfile
;
535 /* Glue onto the ofile list */
538 if (ofile
->symbol_list_tail
)
539 ofile
->symbol_list_tail
->next_in_ofile_list
= s
;
541 ofile
->symbol_list_head
= s
;
542 ofile
->symbol_list_tail
= s
;
543 /* And the block list */
546 b
->vars_tail
->next
= s
;
552 s
->type
= do_type (i
);
553 s
->where
= do_where (i
);
554 s
->visible
= do_visible (i
);
558 /* We remember the lowest address in each section for each source file */
560 if (s
->where
->where
== coff_where_memory
561 && s
->type
->type
== coff_secdef_type
)
563 struct coff_isection
*is
= cur_sfile
->section
+ s
->where
->section
->number
;
567 is
->low
= s
->where
->offset
;
568 is
->high
= s
->where
->offset
+ s
->type
->size
;
570 is
->parent
= s
->where
->section
;
575 if (s
->type
->type
== coff_function_type
)
576 last_function_symbol
= s
;
578 return i
+ sym
->n_numaux
+ 1;
588 struct coff_ofile
*head
= (struct coff_ofile
*) malloc (sizeof (struct coff_ofile
));
590 head
->source_head
= 0;
591 head
->source_tail
= 0;
593 do_sections_p1 (head
);
596 for (i
= 0; i
< rawcount
;)
598 struct internal_syment
*sym
= &rawsyms
[i
].u
.syment
;
599 switch (sym
->n_sclass
)
603 /* new source file announced */
604 struct coff_sfile
*n
= (struct coff_sfile
*) malloc (sizeof (struct coff_sfile
));
605 n
->section
= (struct coff_isection
*) xcalloc (sizeof (struct coff_isection
), abfd
->section_count
+ 1);
607 n
->name
= sym
->_n
._n_nptr
[1];
616 file_scope
= n
->scope
= top_scope
;
618 if (head
->source_tail
)
619 head
->source_tail
->next
= n
;
621 head
->source_head
= n
;
622 head
->source_tail
= n
;
624 i
+= sym
->n_numaux
+ 1;
629 char *name
= sym
->_n
._n_nptr
[1];
634 last_function_type
->u
.function
.code
= top_scope
;
635 top_scope
->sec
= ofile
->sections
+ sym
->n_scnum
;
636 top_scope
->offset
= sym
->n_value
;
640 top_scope
->size
= sym
->n_value
- top_scope
->offset
+ 1;
644 i
+= sym
->n_numaux
+ 1;
650 char *name
= sym
->_n
._n_nptr
[1];
655 top_scope
->sec
= ofile
->sections
+ sym
->n_scnum
;
656 top_scope
->offset
= sym
->n_value
;
661 top_scope
->size
= sym
->n_value
- top_scope
->offset
+ 1;
664 i
+= sym
->n_numaux
+ 1;
669 i
= do_define (i
, last_function_symbol
->type
->u
.function
.parameters
);
674 i
= do_define (i
, last_struct
->u
.astructdef
.elements
);
677 i
= do_define (i
, last_enum
->u
.aenumdef
.elements
);
682 /* Various definition */
683 i
= do_define (i
, top_scope
);
687 i
= do_define (i
, file_scope
);
693 i
= do_define (i
, top_scope
);
698 i
+= sym
->n_numaux
+ 1;
702 do_sections_p2 (head
);
711 struct coff_ofile
*p
;
713 storage
= bfd_get_symtab_upper_bound (abfd
);
716 bfd_fatal (abfd
->filename
);
718 syms
= (asymbol
**) xmalloc (storage
);
719 symcount
= bfd_canonicalize_symtab (abfd
, syms
);
721 bfd_fatal (abfd
->filename
);
722 rawsyms
= obj_raw_syments (abfd
);
723 rawcount
= obj_raw_syment_count (abfd
);;
724 tindex
= (struct coff_symbol
**) (xcalloc (sizeof (struct coff_symbol
*), rawcount
));