2 Copyright 1994, 1995, 1997, 1998, 2000, 2001, 2002, 2003
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 (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
*
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));
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 (struct coff_ofile
*head
)
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
++)
139 int i
= section
->target_index
;
143 relsize
= bfd_get_reloc_upper_bound (abfd
, section
);
145 bfd_fatal (bfd_get_filename (abfd
));
148 relpp
= (arelent
**) xmalloc (relsize
);
149 relcount
= bfd_canonicalize_reloc (abfd
, section
, relpp
, syms
);
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
= section
->_raw_size
;
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;
176 do_sections_p2 (struct coff_ofile
*head
)
179 for (section
= abfd
->sections
; section
; section
= section
->next
)
183 for (j
= 0; j
< section
->reloc_count
; j
++)
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
*
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)
208 switch (sym
->n_sclass
)
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
;
217 where
->where
= coff_where_member_of_enum
;
221 where
->where
= coff_where_member_of_struct
;
225 where
->where
= coff_where_stack
;
231 where
->where
= coff_where_memory
;
232 where
->section
= &ofile
->sections
[sym
->n_scnum
];
236 where
->where
= coff_where_register
;
239 where
->where
= coff_where_entag
;
243 where
->where
= coff_where_strtag
;
246 where
->where
= coff_where_typedef
;
257 do_lines (int i
, char *name ATTRIBUTE_UNUSED
)
259 struct coff_line
*res
= (struct coff_line
*) xcalloc (sizeof (struct coff_line
), 1);
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 */
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
;
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 */
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
;
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
;
314 res
->type
= coff_basic_type
;
315 res
->u
.basic
= type
& 0xf;
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
;
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
;
337 /* Else it could be a function or pointer to void */
351 res
->size
= SHORT_SIZE
;
355 res
->size
= INT_SIZE
;
359 res
->size
= LONG_SIZE
;
362 res
->size
= FLOAT_SIZE
;
365 res
->size
= DOUBLE_SIZE
;
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;
381 /* A definition of a struct */
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
;
392 /* No auxents - it's anonymous */
393 res
->type
= coff_structref_type
;
394 res
->u
.astructref
.ref
= 0;
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
;
408 /* A definition of an enum */
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
;
419 for (which_dt
= 5; which_dt
>= 0; which_dt
--)
421 switch ((type
>> ((which_dt
* 2) + 4)) & 0x3)
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
]
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
;
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
;
452 struct coff_type
*ptr
453 = (struct coff_type
*) xmalloc (sizeof (struct coff_type
));
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
;
469 static struct coff_visible
*
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
)
481 t
= coff_vis_member_of_struct
;
484 t
= coff_vis_member_of_enum
;
488 t
= coff_vis_regparam
;
492 t
= coff_vis_register
;
502 t
= coff_vis_autoparam
;
511 t
= coff_vis_int_def
;
514 if (sym
->n_scnum
== N_UNDEF
)
519 t
= coff_vis_ext_ref
;
522 t
= coff_vis_ext_def
;
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 */
548 if (ofile
->symbol_list_tail
)
549 ofile
->symbol_list_tail
->next_in_ofile_list
= s
;
551 ofile
->symbol_list_head
= s
;
552 ofile
->symbol_list_tail
= s
;
553 /* And the block list */
556 b
->vars_tail
->next
= s
;
562 s
->type
= do_type (i
);
563 s
->where
= do_where (i
);
564 s
->visible
= do_visible (i
);
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
;
577 is
->low
= s
->where
->offset
;
578 is
->high
= s
->where
->offset
+ s
->type
->size
;
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;
598 struct coff_ofile
*head
=
599 (struct coff_ofile
*) xmalloc (sizeof (struct coff_ofile
));
601 head
->source_head
= 0;
602 head
->source_tail
= 0;
604 head
->symbol_list_tail
= 0;
605 head
->symbol_list_head
= 0;
606 do_sections_p1 (head
);
609 for (i
= 0; i
< rawcount
;)
611 struct internal_syment
*sym
= &rawsyms
[i
].u
.syment
;
612 switch (sym
->n_sclass
)
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);
621 n
->name
= sym
->_n
._n_nptr
[1];
630 file_scope
= n
->scope
= top_scope
;
632 if (head
->source_tail
)
633 head
->source_tail
->next
= n
;
635 head
->source_head
= n
;
636 head
->source_tail
= n
;
638 i
+= sym
->n_numaux
+ 1;
643 char *name
= sym
->_n
._n_nptr
[1];
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
;
654 top_scope
->size
= sym
->n_value
- top_scope
->offset
+ 1;
658 i
+= sym
->n_numaux
+ 1;
664 char *name
= sym
->_n
._n_nptr
[1];
669 top_scope
->sec
= ofile
->sections
+ sym
->n_scnum
;
670 top_scope
->offset
= sym
->n_value
;
675 top_scope
->size
= sym
->n_value
- top_scope
->offset
+ 1;
678 i
+= sym
->n_numaux
+ 1;
683 i
= do_define (i
, last_function_symbol
->type
->u
.function
.parameters
);
688 i
= do_define (i
, last_struct
->u
.astructdef
.elements
);
691 i
= do_define (i
, last_enum
->u
.aenumdef
.elements
);
696 /* Various definition */
697 i
= do_define (i
, top_scope
);
701 i
= do_define (i
, file_scope
);
707 i
= do_define (i
, top_scope
);
712 i
+= sym
->n_numaux
+ 1;
716 do_sections_p2 (head
);
721 coff_grok (bfd
*inabfd
)
724 struct coff_ofile
*p
;
726 storage
= bfd_get_symtab_upper_bound (abfd
);
729 bfd_fatal (abfd
->filename
);
731 syms
= (asymbol
**) xmalloc (storage
);
732 symcount
= bfd_canonicalize_symtab (abfd
, syms
);
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
));