1 /* PEF support for BFD.
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
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 #include "safe-ctype.h"
24 #include "pef-traceback.h"
30 #include "libiberty.h"
33 #define BFD_IO_FUNCS 0
36 #define bfd_pef_close_and_cleanup _bfd_generic_close_and_cleanup
37 #define bfd_pef_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
38 #define bfd_pef_new_section_hook _bfd_generic_new_section_hook
39 #define bfd_pef_bfd_is_local_label_name bfd_generic_is_local_label_name
40 #define bfd_pef_get_lineno _bfd_nosymbols_get_lineno
41 #define bfd_pef_find_nearest_line _bfd_nosymbols_find_nearest_line
42 #define bfd_pef_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
43 #define bfd_pef_read_minisymbols _bfd_generic_read_minisymbols
44 #define bfd_pef_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
45 #define bfd_pef_get_reloc_upper_bound _bfd_norelocs_get_reloc_upper_bound
46 #define bfd_pef_canonicalize_reloc _bfd_norelocs_canonicalize_reloc
47 #define bfd_pef_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
48 #define bfd_pef_set_arch_mach _bfd_generic_set_arch_mach
49 #define bfd_pef_get_section_contents _bfd_generic_get_section_contents
50 #define bfd_pef_set_section_contents _bfd_generic_set_section_contents
51 #define bfd_pef_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
52 #define bfd_pef_bfd_relax_section bfd_generic_relax_section
53 #define bfd_pef_bfd_gc_sections bfd_generic_gc_sections
54 #define bfd_pef_bfd_merge_sections bfd_generic_merge_sections
55 #define bfd_pef_bfd_is_group_section bfd_generic_is_group_section
56 #define bfd_pef_bfd_discard_group bfd_generic_discard_group
57 #define bfd_pef_section_already_linked _bfd_generic_section_already_linked
58 #define bfd_pef_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
59 #define bfd_pef_bfd_link_hash_table_free _bfd_generic_link_hash_table_free
60 #define bfd_pef_bfd_link_add_symbols _bfd_generic_link_add_symbols
61 #define bfd_pef_bfd_link_just_syms _bfd_generic_link_just_syms
62 #define bfd_pef_bfd_final_link _bfd_generic_final_link
63 #define bfd_pef_bfd_link_split_section _bfd_generic_link_split_section
64 #define bfd_pef_get_section_contents_in_window _bfd_generic_get_section_contents_in_window
66 static void bfd_pef_print_symbol
PARAMS ((bfd
*, PTR
, asymbol
*, bfd_print_symbol_type
));
67 static void bfd_pef_convert_architecture
PARAMS ((unsigned long, enum bfd_architecture
*, unsigned long *));
68 static bfd_boolean bfd_pef_mkobject
PARAMS ((bfd
*));
69 static int bfd_pef_parse_traceback_table
PARAMS ((bfd
*, asection
*, unsigned char *, size_t, size_t, asymbol
*, FILE *));
70 static const char *bfd_pef_section_name
PARAMS ((bfd_pef_section
*));
71 static unsigned long bfd_pef_section_flags
PARAMS ((bfd_pef_section
*));
72 static asection
*bfd_pef_make_bfd_section
PARAMS ((bfd
*, bfd_pef_section
*));
73 static int bfd_pef_read_header
PARAMS ((bfd
*, bfd_pef_header
*));
74 static const bfd_target
*bfd_pef_object_p
PARAMS ((bfd
*));
75 static int bfd_pef_parse_traceback_tables
PARAMS ((bfd
*, asection
*, unsigned char *, size_t, long *, asymbol
**));
76 static int bfd_pef_parse_function_stub
PARAMS ((bfd
*, unsigned char *, size_t, unsigned long *));
77 static int bfd_pef_parse_function_stubs
PARAMS ((bfd
*, asection
*, unsigned char *, size_t, unsigned char *, size_t, unsigned long *, asymbol
**));
78 static long bfd_pef_parse_symbols
PARAMS ((bfd
*, asymbol
**));
79 static long bfd_pef_count_symbols
PARAMS ((bfd
*));
80 static long bfd_pef_get_symtab_upper_bound
PARAMS ((bfd
*));
81 static long bfd_pef_canonicalize_symtab
PARAMS ((bfd
*, asymbol
**));
82 static asymbol
*bfd_pef_make_empty_symbol
PARAMS ((bfd
*));
83 static void bfd_pef_get_symbol_info
PARAMS ((bfd
*, asymbol
*, symbol_info
*));
84 static int bfd_pef_sizeof_headers
PARAMS ((bfd
*, bfd_boolean
));
85 static int bfd_pef_xlib_read_header
PARAMS ((bfd
*, bfd_pef_xlib_header
*));
86 static int bfd_pef_xlib_scan
PARAMS ((bfd
*, bfd_pef_xlib_header
*));
87 static const bfd_target
*bfd_pef_xlib_object_p
PARAMS ((bfd
*));
90 bfd_pef_print_symbol (abfd
, afile
, symbol
, how
)
94 bfd_print_symbol_type how
;
96 FILE *file
= (FILE *) afile
;
100 case bfd_print_symbol_name
:
101 fprintf (file
, "%s", symbol
->name
);
104 bfd_print_symbol_vandf (abfd
, (PTR
) file
, symbol
);
105 fprintf (file
, " %-5s %s", symbol
->section
->name
, symbol
->name
);
106 if (strncmp (symbol
->name
, "__traceback_", strlen ("__traceback_")) == 0)
108 char *buf
= alloca (symbol
->udata
.i
);
109 size_t offset
= symbol
->value
+ 4;
110 size_t len
= symbol
->udata
.i
;
113 bfd_get_section_contents (abfd
, symbol
->section
, buf
, offset
, len
);
114 ret
= bfd_pef_parse_traceback_table (abfd
, symbol
->section
, buf
,
117 fprintf (file
, " [ERROR]");
123 bfd_pef_convert_architecture (architecture
, type
, subtype
)
124 unsigned long architecture
;
125 enum bfd_architecture
*type
;
126 unsigned long *subtype
;
128 const unsigned long ARCH_POWERPC
= 0x70777063; /* 'pwpc' */
129 const unsigned long ARCH_M68K
= 0x6d36386b; /* 'm68k' */
131 *subtype
= bfd_arch_unknown
;
132 *type
= bfd_arch_unknown
;
134 if (architecture
== ARCH_POWERPC
)
135 *type
= bfd_arch_powerpc
;
136 else if (architecture
== ARCH_M68K
)
137 *type
= bfd_arch_m68k
;
141 bfd_pef_mkobject (abfd
)
142 bfd
*abfd ATTRIBUTE_UNUSED
;
148 bfd_pef_parse_traceback_table (abfd
, section
, buf
, len
, pos
, sym
, file
)
157 struct traceback_table table
;
168 sym
->section
= section
;
172 /* memcpy is fine since all fields are unsigned char. */
176 memcpy (&table
, buf
+ pos
, 8);
178 /* Calling code relies on returned symbols having a name and
181 if ((table
.lang
!= TB_C
) && (table
.lang
!= TB_CPLUSPLUS
))
184 if (! (table
.flags2
& TB_NAME_PRESENT
))
187 if (! table
.flags1
& TB_HAS_TBOFF
)
192 if ((table
.flags5
& TB_FLOATPARAMS
) || (table
.fixedparams
))
195 if (table
.flags1
& TB_HAS_TBOFF
)
197 struct traceback_table_tboff off
;
199 if ((pos
+ offset
+ 4) > len
)
201 off
.tb_offset
= bfd_getb32 (buf
+ pos
+ offset
);
204 /* Need to subtract 4 because the offset includes the 0x0L
205 preceding the table. */
208 fprintf (file
, " [offset = 0x%lx]", off
.tb_offset
);
210 if ((file
== NULL
) && ((off
.tb_offset
+ 4) > (pos
+ offset
)))
213 sym
->value
= pos
- off
.tb_offset
- 4;
216 if (table
.flags2
& TB_INT_HNDL
)
219 if (table
.flags1
& TB_HAS_CTL
)
221 struct traceback_table_anchors anchors
;
223 if ((pos
+ offset
+ 4) > len
)
225 anchors
.ctl_info
= bfd_getb32 (buf
+ pos
+ offset
);
228 if (anchors
.ctl_info
> 1024)
231 offset
+= anchors
.ctl_info
* 4;
234 if (table
.flags2
& TB_NAME_PRESENT
)
236 struct traceback_table_routine name
;
239 if ((pos
+ offset
+ 2) > len
)
241 name
.name_len
= bfd_getb16 (buf
+ pos
+ offset
);
244 if (name
.name_len
> 4096)
247 if ((pos
+ offset
+ name
.name_len
) > len
)
250 namebuf
= (char *) bfd_alloc (abfd
, name
.name_len
+ 1);
254 memcpy (namebuf
, buf
+ pos
+ offset
, name
.name_len
);
255 namebuf
[name
.name_len
] = '\0';
257 /* Strip leading period inserted by compiler. */
258 if (namebuf
[0] == '.')
259 memmove (namebuf
, namebuf
+ 1, name
.name_len
+ 1);
263 for (s
= sym
->name
; (*s
!= '\0'); s
++)
267 offset
+= name
.name_len
;
270 if (table
.flags2
& TB_USES_ALLOCA
)
273 if (table
.flags4
& TB_HAS_VEC_INFO
)
277 fprintf (file
, " [length = 0x%lx]", (long) offset
);
282 static const char *bfd_pef_section_name (section
)
283 bfd_pef_section
*section
;
285 switch (section
->section_kind
)
287 case BFD_PEF_SECTION_CODE
: return "code";
288 case BFD_PEF_SECTION_UNPACKED_DATA
: return "unpacked-data";
289 case BFD_PEF_SECTION_PACKED_DATA
: return "packed-data";
290 case BFD_PEF_SECTION_CONSTANT
: return "constant";
291 case BFD_PEF_SECTION_LOADER
: return "loader";
292 case BFD_PEF_SECTION_DEBUG
: return "debug";
293 case BFD_PEF_SECTION_EXEC_DATA
: return "exec-data";
294 case BFD_PEF_SECTION_EXCEPTION
: return "exception";
295 case BFD_PEF_SECTION_TRACEBACK
: return "traceback";
296 default: return "unknown";
300 static unsigned long bfd_pef_section_flags (section
)
301 bfd_pef_section
*section
;
303 switch (section
->section_kind
)
305 case BFD_PEF_SECTION_CODE
:
306 return SEC_HAS_CONTENTS
| SEC_LOAD
| SEC_ALLOC
| SEC_CODE
;
307 case BFD_PEF_SECTION_UNPACKED_DATA
:
308 case BFD_PEF_SECTION_PACKED_DATA
:
309 case BFD_PEF_SECTION_CONSTANT
:
310 case BFD_PEF_SECTION_LOADER
:
311 case BFD_PEF_SECTION_DEBUG
:
312 case BFD_PEF_SECTION_EXEC_DATA
:
313 case BFD_PEF_SECTION_EXCEPTION
:
314 case BFD_PEF_SECTION_TRACEBACK
:
316 return SEC_HAS_CONTENTS
| SEC_LOAD
| SEC_ALLOC
;
321 bfd_pef_make_bfd_section (abfd
, section
)
323 bfd_pef_section
*section
;
326 const char *name
= bfd_pef_section_name (section
);
328 bfdsec
= bfd_make_section_anyway (abfd
, name
);
332 bfdsec
->vma
= section
->default_address
+ section
->container_offset
;
333 bfdsec
->lma
= section
->default_address
+ section
->container_offset
;
334 bfdsec
->size
= section
->container_length
;
335 bfdsec
->filepos
= section
->container_offset
;
336 bfdsec
->alignment_power
= section
->alignment
;
338 bfdsec
->flags
= bfd_pef_section_flags (section
);
343 int bfd_pef_parse_loader_header (abfd
, buf
, len
, header
)
344 bfd
*abfd ATTRIBUTE_UNUSED
;
347 bfd_pef_loader_header
*header
;
349 BFD_ASSERT (len
== 56);
351 header
->main_section
= bfd_getb32 (buf
);
352 header
->main_offset
= bfd_getb32 (buf
+ 4);
353 header
->init_section
= bfd_getb32 (buf
+ 8);
354 header
->init_offset
= bfd_getb32 (buf
+ 12);
355 header
->term_section
= bfd_getb32 (buf
+ 16);
356 header
->term_offset
= bfd_getb32 (buf
+ 20);
357 header
->imported_library_count
= bfd_getb32 (buf
+ 24);
358 header
->total_imported_symbol_count
= bfd_getb32 (buf
+ 28);
359 header
->reloc_section_count
= bfd_getb32 (buf
+ 32);
360 header
->reloc_instr_offset
= bfd_getb32 (buf
+ 36);
361 header
->loader_strings_offset
= bfd_getb32 (buf
+ 40);
362 header
->export_hash_offset
= bfd_getb32 (buf
+ 44);
363 header
->export_hash_table_power
= bfd_getb32 (buf
+ 48);
364 header
->exported_symbol_count
= bfd_getb32 (buf
+ 52);
369 int bfd_pef_parse_imported_library (abfd
, buf
, len
, header
)
370 bfd
*abfd ATTRIBUTE_UNUSED
;
373 bfd_pef_imported_library
*header
;
375 BFD_ASSERT (len
== 24);
377 header
->name_offset
= bfd_getb32 (buf
);
378 header
->old_implementation_version
= bfd_getb32 (buf
+ 4);
379 header
->current_version
= bfd_getb32 (buf
+ 8);
380 header
->imported_symbol_count
= bfd_getb32 (buf
+ 12);
381 header
->first_imported_symbol
= bfd_getb32 (buf
+ 16);
382 header
->options
= buf
[20];
383 header
->reserved_a
= buf
[21];
384 header
->reserved_b
= bfd_getb16 (buf
+ 22);
389 int bfd_pef_parse_imported_symbol (abfd
, buf
, len
, symbol
)
390 bfd
*abfd ATTRIBUTE_UNUSED
;
393 bfd_pef_imported_symbol
*symbol
;
397 BFD_ASSERT (len
== 4);
399 value
= bfd_getb32 (buf
);
400 symbol
->class = value
>> 24;
401 symbol
->name
= value
& 0x00ffffff;
406 int bfd_pef_scan_section (abfd
, section
)
408 bfd_pef_section
*section
;
410 unsigned char buf
[28];
412 bfd_seek (abfd
, section
->header_offset
, SEEK_SET
);
413 if (bfd_bread ((PTR
) buf
, 28, abfd
) != 28)
416 section
->name_offset
= bfd_h_get_32 (abfd
, buf
);
417 section
->default_address
= bfd_h_get_32 (abfd
, buf
+ 4);
418 section
->total_length
= bfd_h_get_32 (abfd
, buf
+ 8);
419 section
->unpacked_length
= bfd_h_get_32 (abfd
, buf
+ 12);
420 section
->container_length
= bfd_h_get_32 (abfd
, buf
+ 16);
421 section
->container_offset
= bfd_h_get_32 (abfd
, buf
+ 20);
422 section
->section_kind
= buf
[24];
423 section
->share_kind
= buf
[25];
424 section
->alignment
= buf
[26];
425 section
->reserved
= buf
[27];
427 section
->bfd_section
= bfd_pef_make_bfd_section (abfd
, section
);
428 if (section
->bfd_section
== NULL
)
435 bfd_pef_print_loader_header (abfd
, header
, file
)
436 bfd
*abfd ATTRIBUTE_UNUSED
;
437 bfd_pef_loader_header
*header
;
440 fprintf (file
, "main_section: %ld\n", header
->main_section
);
441 fprintf (file
, "main_offset: %lu\n", header
->main_offset
);
442 fprintf (file
, "init_section: %ld\n", header
->init_section
);
443 fprintf (file
, "init_offset: %lu\n", header
->init_offset
);
444 fprintf (file
, "term_section: %ld\n", header
->term_section
);
445 fprintf (file
, "term_offset: %lu\n", header
->term_offset
);
446 fprintf (file
, "imported_library_count: %lu\n",
447 header
->imported_library_count
);
448 fprintf (file
, "total_imported_symbol_count: %lu\n",
449 header
->total_imported_symbol_count
);
450 fprintf (file
, "reloc_section_count: %lu\n", header
->reloc_section_count
);
451 fprintf (file
, "reloc_instr_offset: %lu\n", header
->reloc_instr_offset
);
452 fprintf (file
, "loader_strings_offset: %lu\n",
453 header
->loader_strings_offset
);
454 fprintf (file
, "export_hash_offset: %lu\n", header
->export_hash_offset
);
455 fprintf (file
, "export_hash_table_power: %lu\n",
456 header
->export_hash_table_power
);
457 fprintf (file
, "exported_symbol_count: %lu\n",
458 header
->exported_symbol_count
);
462 bfd_pef_print_loader_section (abfd
, file
)
466 bfd_pef_loader_header header
;
467 asection
*loadersec
= NULL
;
468 unsigned char *loaderbuf
= NULL
;
469 size_t loaderlen
= 0;
472 loadersec
= bfd_get_section_by_name (abfd
, "loader");
473 if (loadersec
== NULL
)
476 loaderlen
= loadersec
->size
;
477 loaderbuf
= (unsigned char *) bfd_malloc (loaderlen
);
478 if (bfd_seek (abfd
, loadersec
->filepos
, SEEK_SET
) < 0)
483 if (bfd_bread ((PTR
) loaderbuf
, loaderlen
, abfd
) != loaderlen
)
494 ret
= bfd_pef_parse_loader_header (abfd
, loaderbuf
, 56, &header
);
501 bfd_pef_print_loader_header (abfd
, &header
, file
);
506 bfd_pef_scan_start_address (abfd
)
509 bfd_pef_loader_header header
;
512 asection
*loadersec
= NULL
;
513 unsigned char *loaderbuf
= NULL
;
514 size_t loaderlen
= 0;
517 loadersec
= bfd_get_section_by_name (abfd
, "loader");
518 if (loadersec
== NULL
)
521 loaderlen
= loadersec
->size
;
522 loaderbuf
= (unsigned char *) bfd_malloc (loaderlen
);
523 if (bfd_seek (abfd
, loadersec
->filepos
, SEEK_SET
) < 0)
525 if (bfd_bread ((PTR
) loaderbuf
, loaderlen
, abfd
) != loaderlen
)
530 ret
= bfd_pef_parse_loader_header (abfd
, loaderbuf
, 56, &header
);
534 if (header
.main_section
< 0)
537 for (section
= abfd
->sections
; section
!= NULL
; section
= section
->next
)
538 if ((section
->index
+ 1) == header
.main_section
)
544 abfd
->start_address
= section
->vma
+ header
.main_offset
;
547 if (loaderbuf
!= NULL
)
552 if (loaderbuf
!= NULL
)
558 bfd_pef_scan (abfd
, header
, mdata
)
560 bfd_pef_header
*header
;
561 bfd_pef_data_struct
*mdata
;
564 enum bfd_architecture cputype
;
565 unsigned long cpusubtype
;
567 mdata
->header
= *header
;
569 bfd_pef_convert_architecture (header
->architecture
, &cputype
, &cpusubtype
);
570 if (cputype
== bfd_arch_unknown
)
572 fprintf (stderr
, "bfd_pef_scan: unknown architecture 0x%lx\n",
573 header
->architecture
);
576 bfd_set_arch_mach (abfd
, cputype
, cpusubtype
);
578 mdata
->header
= *header
;
580 abfd
->flags
= (abfd
->xvec
->object_flags
581 | (abfd
->flags
& (BFD_IN_MEMORY
| BFD_IO_FUNCS
)));
583 if (header
->section_count
!= 0)
587 bfd_alloc (abfd
, header
->section_count
* sizeof (bfd_pef_section
)));
589 if (mdata
->sections
== NULL
)
592 for (i
= 0; i
< header
->section_count
; i
++)
594 bfd_pef_section
*cur
= &mdata
->sections
[i
];
595 cur
->header_offset
= 40 + (i
* 28);
596 if (bfd_pef_scan_section (abfd
, cur
) < 0)
601 if (bfd_pef_scan_start_address (abfd
) < 0)
604 fprintf (stderr
, "bfd_pef_scan: unable to scan start address: %s\n",
605 bfd_errmsg (bfd_get_error ()));
610 abfd
->tdata
.pef_data
= mdata
;
616 bfd_pef_read_header (abfd
, header
)
618 bfd_pef_header
*header
;
620 unsigned char buf
[40];
622 bfd_seek (abfd
, 0, SEEK_SET
);
624 if (bfd_bread ((PTR
) buf
, 40, abfd
) != 40)
627 header
->tag1
= bfd_getb32 (buf
);
628 header
->tag2
= bfd_getb32 (buf
+ 4);
629 header
->architecture
= bfd_getb32 (buf
+ 8);
630 header
->format_version
= bfd_getb32 (buf
+ 12);
631 header
->timestamp
= bfd_getb32 (buf
+ 16);
632 header
->old_definition_version
= bfd_getb32 (buf
+ 20);
633 header
->old_implementation_version
= bfd_getb32 (buf
+ 24);
634 header
->current_version
= bfd_getb32 (buf
+ 28);
635 header
->section_count
= bfd_getb32 (buf
+ 32) + 1;
636 header
->instantiated_section_count
= bfd_getb32 (buf
+ 34);
637 header
->reserved
= bfd_getb32 (buf
+ 36);
642 static const bfd_target
*
643 bfd_pef_object_p (abfd
)
646 struct bfd_preserve preserve
;
647 bfd_pef_header header
;
649 preserve
.marker
= NULL
;
650 if (bfd_pef_read_header (abfd
, &header
) != 0)
653 if (header
.tag1
!= BFD_PEF_TAG1
|| header
.tag2
!= BFD_PEF_TAG2
)
656 preserve
.marker
= bfd_zalloc (abfd
, sizeof (bfd_pef_data_struct
));
657 if (preserve
.marker
== NULL
658 || !bfd_preserve_save (abfd
, &preserve
))
661 if (bfd_pef_scan (abfd
, &header
,
662 (bfd_pef_data_struct
*) preserve
.marker
) != 0)
665 bfd_preserve_finish (abfd
, &preserve
);
669 bfd_set_error (bfd_error_wrong_format
);
672 if (preserve
.marker
!= NULL
)
673 bfd_preserve_restore (abfd
, &preserve
);
677 static int bfd_pef_parse_traceback_tables (abfd
, sec
, buf
, len
, nsym
, csym
)
690 const char *const tbprefix
= "__traceback_";
694 unsigned long count
= 0;
699 /* We're reading symbols two at a time. */
700 if (csym
&& ((csym
[count
] == NULL
) || (csym
[count
+ 1] == NULL
)))
706 while ((pos
+ 4) <= len
)
708 if (bfd_getb32 (buf
+ pos
) == 0)
716 ret
= bfd_pef_parse_traceback_table (abfd
, sec
, buf
, len
, pos
+ 4,
720 /* Skip over 0x0L to advance to next possible traceback table. */
725 BFD_ASSERT (function
.name
!= NULL
);
727 /* Don't bother to compute the name if we are just
732 tbnamelen
= strlen (tbprefix
) + strlen (function
.name
);
733 name
= bfd_alloc (abfd
, tbnamelen
+ 1);
736 bfd_release (abfd
, (PTR
) function
.name
);
737 function
.name
= NULL
;
740 snprintf (name
, tbnamelen
+ 1, "%s%s", tbprefix
, function
.name
);
741 traceback
.name
= name
;
742 traceback
.value
= pos
;
743 traceback
.the_bfd
= abfd
;
744 traceback
.section
= sec
;
746 traceback
.udata
.i
= ret
;
748 *(csym
[count
]) = function
;
749 *(csym
[count
+ 1]) = traceback
;
760 static int bfd_pef_parse_function_stub (abfd
, buf
, len
, offset
)
761 bfd
*abfd ATTRIBUTE_UNUSED
;
764 unsigned long *offset
;
766 BFD_ASSERT (len
== 24);
768 if ((bfd_getb32 (buf
) & 0xffff0000) != 0x81820000)
770 if (bfd_getb32 (buf
+ 4) != 0x90410014)
772 if (bfd_getb32 (buf
+ 8) != 0x800c0000)
774 if (bfd_getb32 (buf
+ 12) != 0x804c0004)
776 if (bfd_getb32 (buf
+ 16) != 0x7c0903a6)
778 if (bfd_getb32 (buf
+ 20) != 0x4e800420)
782 *offset
= (bfd_getb32 (buf
) & 0x0000ffff) / 4;
787 static int bfd_pef_parse_function_stubs (abfd
, codesec
, codebuf
, codelen
,
788 loaderbuf
, loaderlen
, nsym
, csym
)
791 unsigned char *codebuf
;
793 unsigned char *loaderbuf
;
798 const char *const sprefix
= "__stub_";
801 unsigned long count
= 0;
803 bfd_pef_loader_header header
;
804 bfd_pef_imported_library
*libraries
= NULL
;
805 bfd_pef_imported_symbol
*imports
= NULL
;
813 ret
= bfd_pef_parse_loader_header (abfd
, loaderbuf
, 56, &header
);
817 libraries
= (bfd_pef_imported_library
*) bfd_malloc
818 (header
.imported_library_count
* sizeof (bfd_pef_imported_library
));
819 imports
= (bfd_pef_imported_symbol
*) bfd_malloc
820 (header
.total_imported_symbol_count
* sizeof (bfd_pef_imported_symbol
));
822 if (loaderlen
< (56 + (header
.imported_library_count
* 24)))
824 for (i
= 0; i
< header
.imported_library_count
; i
++)
826 ret
= bfd_pef_parse_imported_library
827 (abfd
, loaderbuf
+ 56 + (i
* 24), 24, &libraries
[i
]);
832 if (loaderlen
< (56 + (header
.imported_library_count
* 24)
833 + (header
.total_imported_symbol_count
* 4)))
835 for (i
= 0; i
< header
.total_imported_symbol_count
; i
++)
837 ret
= (bfd_pef_parse_imported_symbol
839 loaderbuf
+ 56 + (header
.imported_library_count
* 24) + (i
* 4),
855 if (csym
&& (csym
[count
] == NULL
))
859 codepos
-= (codepos
% 4);
861 while ((codepos
+ 4) <= codelen
)
863 if ((bfd_getb32 (codebuf
+ codepos
) & 0xffff0000) == 0x81820000)
868 if ((codepos
+ 4) > codelen
)
871 ret
= bfd_pef_parse_function_stub (abfd
, codebuf
+ codepos
, 24, &index
);
878 if (index
>= header
.total_imported_symbol_count
)
888 if (loaderlen
< (header
.loader_strings_offset
+ imports
[index
].name
))
891 max
= loaderlen
- (header
.loader_strings_offset
+ imports
[index
].name
);
892 symname
= loaderbuf
+ header
.loader_strings_offset
+ imports
[index
].name
;
894 for (s
= symname
; s
< (symname
+ max
); s
++)
905 name
= bfd_alloc (abfd
, strlen (sprefix
) + namelen
+ 1);
909 snprintf (name
, strlen (sprefix
) + namelen
+ 1, "%s%s",
916 sym
.section
= codesec
;
923 *(csym
[count
]) = sym
;
931 if (libraries
!= NULL
)
939 if (libraries
!= NULL
)
947 static long bfd_pef_parse_symbols (abfd
, csym
)
951 unsigned long count
= 0;
953 asection
*codesec
= NULL
;
954 unsigned char *codebuf
= NULL
;
957 asection
*loadersec
= NULL
;
958 unsigned char *loaderbuf
= NULL
;
959 size_t loaderlen
= 0;
961 codesec
= bfd_get_section_by_name (abfd
, "code");
964 codelen
= codesec
->size
;
965 codebuf
= (unsigned char *) bfd_malloc (codelen
);
966 if (bfd_seek (abfd
, codesec
->filepos
, SEEK_SET
) < 0)
968 if (bfd_bread ((PTR
) codebuf
, codelen
, abfd
) != codelen
)
972 loadersec
= bfd_get_section_by_name (abfd
, "loader");
973 if (loadersec
!= NULL
)
975 loaderlen
= loadersec
->size
;
976 loaderbuf
= (unsigned char *) bfd_malloc (loaderlen
);
977 if (bfd_seek (abfd
, loadersec
->filepos
, SEEK_SET
) < 0)
979 if (bfd_bread ((PTR
) loaderbuf
, loaderlen
, abfd
) != loaderlen
)
986 unsigned long ncount
= 0;
987 bfd_pef_parse_traceback_tables (abfd
, codesec
, codebuf
, codelen
,
992 if ((codesec
!= NULL
) && (loadersec
!= NULL
))
994 unsigned long ncount
= 0;
995 bfd_pef_parse_function_stubs
996 (abfd
, codesec
, codebuf
, codelen
, loaderbuf
, loaderlen
, &ncount
,
997 (csym
!= NULL
) ? (csym
+ count
) : NULL
);
1005 if (codebuf
!= NULL
)
1008 if (loaderbuf
!= NULL
)
1015 bfd_pef_count_symbols (abfd
)
1018 return bfd_pef_parse_symbols (abfd
, NULL
);
1022 bfd_pef_get_symtab_upper_bound (abfd
)
1025 long nsyms
= bfd_pef_count_symbols (abfd
);
1028 return ((nsyms
+ 1) * sizeof (asymbol
*));
1032 bfd_pef_canonicalize_symtab (abfd
, alocation
)
1034 asymbol
**alocation
;
1040 long nsyms
= bfd_pef_count_symbols (abfd
);
1044 syms
= bfd_alloc (abfd
, nsyms
* sizeof (asymbol
));
1048 for (i
= 0; i
< nsyms
; i
++)
1049 alocation
[i
] = &syms
[i
];
1051 alocation
[nsyms
] = NULL
;
1053 ret
= bfd_pef_parse_symbols (abfd
, alocation
);
1061 bfd_pef_make_empty_symbol (abfd
)
1064 return (asymbol
*) bfd_alloc (abfd
, sizeof (asymbol
));
1068 bfd_pef_get_symbol_info (abfd
, symbol
, ret
)
1069 bfd
*abfd ATTRIBUTE_UNUSED
;
1073 bfd_symbol_info (symbol
, ret
);
1077 bfd_pef_sizeof_headers (abfd
, exec
)
1078 bfd
*abfd ATTRIBUTE_UNUSED
;
1079 bfd_boolean exec ATTRIBUTE_UNUSED
;
1084 const bfd_target pef_vec
=
1087 bfd_target_pef_flavour
, /* flavour */
1088 BFD_ENDIAN_BIG
, /* byteorder */
1089 BFD_ENDIAN_BIG
, /* header_byteorder */
1090 (HAS_RELOC
| EXEC_P
| /* object flags */
1091 HAS_LINENO
| HAS_DEBUG
|
1092 HAS_SYMS
| HAS_LOCALS
| DYNAMIC
| WP_TEXT
| D_PAGED
),
1093 (SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
| SEC_CODE
| SEC_DATA
1094 | SEC_ROM
| SEC_HAS_CONTENTS
), /* section_flags */
1095 0, /* symbol_leading_char */
1096 ' ', /* ar_pad_char */
1097 16, /* ar_max_namelen */
1098 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
1099 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
1100 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* data */
1101 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
1102 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
1103 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* hdrs */
1104 { /* bfd_check_format */
1106 bfd_pef_object_p
, /* bfd_check_format */
1110 { /* bfd_set_format */
1116 { /* bfd_write_contents */
1123 BFD_JUMP_TABLE_GENERIC (bfd_pef
),
1124 BFD_JUMP_TABLE_COPY (_bfd_generic
),
1125 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
1126 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive
),
1127 BFD_JUMP_TABLE_SYMBOLS (bfd_pef
),
1128 BFD_JUMP_TABLE_RELOCS (bfd_pef
),
1129 BFD_JUMP_TABLE_WRITE (bfd_pef
),
1130 BFD_JUMP_TABLE_LINK (bfd_pef
),
1131 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),
1138 #define bfd_pef_xlib_close_and_cleanup _bfd_generic_close_and_cleanup
1139 #define bfd_pef_xlib_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
1140 #define bfd_pef_xlib_new_section_hook _bfd_generic_new_section_hook
1141 #define bfd_pef_xlib_get_section_contents _bfd_generic_get_section_contents
1142 #define bfd_pef_xlib_set_section_contents _bfd_generic_set_section_contents
1143 #define bfd_pef_xlib_get_section_contents_in_window _bfd_generic_get_section_contents_in_window
1144 #define bfd_pef_xlib_set_section_contents_in_window _bfd_generic_set_section_contents_in_window
1147 bfd_pef_xlib_read_header (abfd
, header
)
1149 bfd_pef_xlib_header
*header
;
1151 unsigned char buf
[76];
1153 bfd_seek (abfd
, 0, SEEK_SET
);
1155 if (bfd_bread ((PTR
) buf
, 76, abfd
) != 76)
1158 header
->tag1
= bfd_getb32 (buf
);
1159 header
->tag2
= bfd_getb32 (buf
+ 4);
1160 header
->current_format
= bfd_getb32 (buf
+ 8);
1161 header
->container_strings_offset
= bfd_getb32 (buf
+ 12);
1162 header
->export_hash_offset
= bfd_getb32 (buf
+ 16);
1163 header
->export_key_offset
= bfd_getb32 (buf
+ 20);
1164 header
->export_symbol_offset
= bfd_getb32 (buf
+ 24);
1165 header
->export_names_offset
= bfd_getb32 (buf
+ 28);
1166 header
->export_hash_table_power
= bfd_getb32 (buf
+ 32);
1167 header
->exported_symbol_count
= bfd_getb32 (buf
+ 36);
1168 header
->frag_name_offset
= bfd_getb32 (buf
+ 40);
1169 header
->frag_name_length
= bfd_getb32 (buf
+ 44);
1170 header
->dylib_path_offset
= bfd_getb32 (buf
+ 48);
1171 header
->dylib_path_length
= bfd_getb32 (buf
+ 52);
1172 header
->cpu_family
= bfd_getb32 (buf
+ 56);
1173 header
->cpu_model
= bfd_getb32 (buf
+ 60);
1174 header
->date_time_stamp
= bfd_getb32 (buf
+ 64);
1175 header
->current_version
= bfd_getb32 (buf
+ 68);
1176 header
->old_definition_version
= bfd_getb32 (buf
+ 72);
1177 header
->old_implementation_version
= bfd_getb32 (buf
+ 76);
1183 bfd_pef_xlib_scan (abfd
, header
)
1185 bfd_pef_xlib_header
*header
;
1187 bfd_pef_xlib_data_struct
*mdata
= NULL
;
1189 mdata
= ((bfd_pef_xlib_data_struct
*)
1190 bfd_alloc (abfd
, sizeof (bfd_pef_xlib_data_struct
)));
1194 mdata
->header
= *header
;
1196 abfd
->flags
= (abfd
->xvec
->object_flags
1197 | (abfd
->flags
& (BFD_IN_MEMORY
| BFD_IO_FUNCS
)));
1199 abfd
->tdata
.pef_xlib_data
= mdata
;
1204 static const bfd_target
*
1205 bfd_pef_xlib_object_p (abfd
)
1208 struct bfd_preserve preserve
;
1209 bfd_pef_xlib_header header
;
1211 if (bfd_pef_xlib_read_header (abfd
, &header
) != 0)
1213 bfd_set_error (bfd_error_wrong_format
);
1217 if ((header
.tag1
!= BFD_PEF_XLIB_TAG1
)
1218 || ((header
.tag2
!= BFD_PEF_VLIB_TAG2
)
1219 && (header
.tag2
!= BFD_PEF_BLIB_TAG2
)))
1221 bfd_set_error (bfd_error_wrong_format
);
1225 if (! bfd_preserve_save (abfd
, &preserve
))
1227 bfd_set_error (bfd_error_wrong_format
);
1231 if (bfd_pef_xlib_scan (abfd
, &header
) != 0)
1233 bfd_preserve_restore (abfd
, &preserve
);
1234 bfd_set_error (bfd_error_wrong_format
);
1238 bfd_preserve_finish (abfd
, &preserve
);
1242 const bfd_target pef_xlib_vec
=
1244 "pef-xlib", /* name */
1245 bfd_target_pef_xlib_flavour
, /* flavour */
1246 BFD_ENDIAN_BIG
, /* byteorder */
1247 BFD_ENDIAN_BIG
, /* header_byteorder */
1248 (HAS_RELOC
| EXEC_P
| /* object flags */
1249 HAS_LINENO
| HAS_DEBUG
|
1250 HAS_SYMS
| HAS_LOCALS
| DYNAMIC
| WP_TEXT
| D_PAGED
),
1251 (SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
| SEC_CODE
| SEC_DATA
1252 | SEC_ROM
| SEC_HAS_CONTENTS
), /* section_flags */
1253 0, /* symbol_leading_char */
1254 ' ', /* ar_pad_char */
1255 16, /* ar_max_namelen */
1256 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
1257 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
1258 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* data */
1259 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
1260 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
1261 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* hdrs */
1262 { /* bfd_check_format */
1264 bfd_pef_xlib_object_p
, /* bfd_check_format */
1268 { /* bfd_set_format */
1274 { /* bfd_write_contents */
1281 BFD_JUMP_TABLE_GENERIC (bfd_pef_xlib
),
1282 BFD_JUMP_TABLE_COPY (_bfd_generic
),
1283 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
1284 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive
),
1285 BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols
),
1286 BFD_JUMP_TABLE_RELOCS (_bfd_norelocs
),
1287 BFD_JUMP_TABLE_WRITE (_bfd_nowrite
),
1288 BFD_JUMP_TABLE_LINK (_bfd_nolink
),
1289 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),